<commit_before>#include "xchainer/python/array.h"

#include <algorithm>
#include <cstdint>

#include <pybind11/numpy.h>
#include <pybind11/operators.h>

#include "xchainer/array.h"
#include "xchainer/dtype.h"
#include "xchainer/error.h"

#include "xchainer/python/common.h"

namespace xchainer {

namespace py = pybind11;

namespace {

using ArrayBodyPtr = std::shared_ptr<internal::ArrayBody>;
using ConstArrayBodyPtr = std::shared_ptr<const internal::ArrayBody>;

Dtype NumpyDtypeToDtype(const py::dtype& npdtype) {
    switch (npdtype.kind()) {
        case 'b':
            return Dtype::kBool;
        case 'i':
            switch (npdtype.itemsize()) {
                case 1:
                    return Dtype::kInt8;
                case 2:
                    return Dtype::kInt16;
                case 4:
                    return Dtype::kInt32;
                case 8:
                    return Dtype::kInt64;
                default:
                    break;
            }
            break;
        case 'u':
            switch (npdtype.itemsize()) {
                case 1:
                    return Dtype::kUInt8;
                default:
                    break;
            }
            break;
        case 'f':
            switch (npdtype.itemsize()) {
                case 4:
                    return Dtype::kFloat32;
                case 8:
                    return Dtype::kFloat64;
                default:
                    break;
            }
            break;
        default:
            break;
    }
    throw DtypeError("unsupported NumPy dtype");
}

ArrayBodyPtr MakeArray(const Shape& shape, Dtype dtype, py::list list) {
    auto total_size = shape.total_size();
    auto bytes = GetElementSize(dtype) * total_size;
    if (static_cast<size_t>(total_size) != list.size()) {
        throw DimensionError("Invalid data length");
    }

    // Allocate a buffer and copy data
    std::shared_ptr<void> ptr = std::make_unique<uint8_t[]>(bytes);
    VisitDtype(dtype, [&](auto pt) {
        using T = typename decltype(pt)::type;
        std::transform(list.begin(), list.end(), static_cast<T*>(ptr.get()), [](auto& item) { return py::cast<T>(item); });
    });
    return Array::FromBuffer(shape, dtype, ptr).move_body();
}

ArrayBodyPtr MakeArray(py::array array) {
    if ((array.flags() & py::array::c_style) == 0) {
        throw DimensionError("cannot convert non-contiguous NumPy array to Array");
    }

    Dtype dtype = NumpyDtypeToDtype(array.dtype());
    py::buffer_info info = array.request();
    Shape shape(info.shape);

    // data holds the copy of py::array which in turn references the NumPy array and the buffer is therefore not released
    std::shared_ptr<void> data(std::make_shared<py::array>(std::move(array)), array.mutable_data());

    return Array::FromBuffer(shape, dtype, data).move_body();
}

py::buffer_info MakeNumpyArrayFromArray(internal::ArrayBody& self) {
    // Used as a temporary accessor
    Array array{std::move(ArrayBodyPtr(&self, [](internal::ArrayBody* ptr) {
        (void)ptr;  // unused
    }))};

    if (!array.is_contiguous()) {
        throw DimensionError("cannot convert non-contiguous Array to NumPy array");
    }

    int64_t itemsize{GetElementSize(array.dtype())};
    const Shape& shape = array.shape();

    // compute C-contiguous strides
    size_t ndim = array.ndim();
    std::vector<size_t> strides(ndim);
    if (ndim > 0) {
        std::partial_sum(shape.crbegin(), shape.crend() - 1, strides.rbegin() + 1, std::multiplies<size_t>());
        strides.back() = 1;
        std::transform(strides.crbegin(), strides.crend(), strides.rbegin(), [&itemsize](size_t item) { return item * itemsize; });
    }

    return py::buffer_info(array.data().get(), itemsize, std::string(1, GetCharCode(array.dtype())), ndim, shape, strides);
}

}  // namespace

void InitXchainerArray(pybind11::module& m) {
    py::class_<internal::ArrayBody, ArrayBodyPtr>{m, "Array", py::buffer_protocol()}
        .def(py::init(py::overload_cast<const Shape&, Dtype, py::list>(&MakeArray)))
        .def(py::init(py::overload_cast<py::array>(&MakeArray)))
        .def_buffer(&MakeNumpyArrayFromArray)
        .def("view",
             [](const ArrayBodyPtr& self) {
                 // Duplicate the array body
                 return std::make_shared<internal::ArrayBody>(*self);
             })
        .def("__iadd__", [](const ArrayBodyPtr& self, const ArrayBodyPtr& rhs) { return (Array{self} += Array{rhs}).move_body(); })
        .def("__imul__", [](const ArrayBodyPtr& self, const ArrayBodyPtr& rhs) { return (Array{self} *= Array{rhs}).move_body(); })
        .def("__add__", [](const ArrayBodyPtr& self, const ArrayBodyPtr& rhs) { return (Array{self} + Array{rhs}).move_body(); })
        .def("__mul__", [](const ArrayBodyPtr& self, const ArrayBodyPtr& rhs) { return (Array{self} * Array{rhs}).move_body(); })
        .def("__repr__", [](const ArrayBodyPtr& self) { return Array{self}.ToString(); })
        .def("copy", [](const ArrayBodyPtr& self) { return Array{self}.Copy().move_body(); })
        .def_property("requires_grad", [](const ArrayBodyPtr& self) { return Array{self}.IsGradRequired(); },
                      [](const ArrayBodyPtr& self, bool value) {
                          auto array = Array{self};
                          // TODO(hvy): requires_grad should not be a boolean property but a method that takes a graph id argument, aligning
                          // to the c++ interface. Currently, this property is broken in the sense that once the required_grad flag is set
                          // to true (and an ArrayNode is created internally) it cannot be unset.
                          if (value && !array.HasNode()) {
                              array.RequireGrad();
                          }
                      })
        .def_property("grad",
                      [](const ArrayBodyPtr& self) -> ConstArrayBodyPtr {
                          auto array = Array{self};
                          if (array.HasNode()) {
                              return array.GetGrad()->body();
                          } else {
                              return nullptr;
                          }
                      },
                      [](const ArrayBodyPtr& self, const ArrayBodyPtr& grad) {
                          auto array = Array{self};
                          if (grad) {
                              if (array.HasNode()) {
                                  array.SetGrad(Array{grad});
                              } else {
                                  array.RequireGrad().SetGrad(Array{grad});
                              }
                          } else {
                              array.ClearGrad();
                          }
                      })
        .def_property_readonly("dtype", [](const ArrayBodyPtr& self) { return Array{self}.dtype(); })
        .def_property_readonly("element_bytes", [](const ArrayBodyPtr& self) { return Array{self}.element_bytes(); })
        .def_property_readonly("is_contiguous", [](const ArrayBodyPtr& self) { return Array{self}.is_contiguous(); })
        .def_property_readonly("ndim", [](const ArrayBodyPtr& self) { return Array{self}.ndim(); })
        .def_property_readonly("offset", [](const ArrayBodyPtr& self) { return Array{self}.offset(); })
        .def_property_readonly("shape", [](const ArrayBodyPtr& self) { return Array{self}.shape(); })
        .def_property_readonly("total_bytes", [](const ArrayBodyPtr& self) { return Array{self}.total_bytes(); })
        .def_property_readonly("total_size", [](const ArrayBodyPtr& self) { return Array{self}.total_size(); })
        .def_property_readonly("_debug_data_memory_address",  // These methods starting with `_debug_` are stubs for testing
                               [](const ArrayBodyPtr& self) { return reinterpret_cast<std::uintptr_t>(Array{self}.data().get()); })
        .def_property_readonly("_debug_flat_data", [](const ArrayBodyPtr& self) {
            py::list list;
            Array array{self};

            // Copy data into the list
            VisitDtype(array.dtype(), [&array, &list](auto pt) {
                using T = typename decltype(pt)::type;
                auto size = array.total_size();
                const T& data = *std::static_pointer_cast<const T>(array.data());
                for (int64_t i = 0; i < size; ++i) {
                    list.append((&data)[i]);
                }
            });

            return list;
        });
}

}  // namespace xchainer
<commit_msg>Use IsGradRequired() instead of HasNode()<commit_after>#include "xchainer/python/array.h"

#include <algorithm>
#include <cstdint>

#include <pybind11/numpy.h>
#include <pybind11/operators.h>

#include "xchainer/array.h"
#include "xchainer/dtype.h"
#include "xchainer/error.h"

#include "xchainer/python/common.h"

namespace xchainer {

namespace py = pybind11;

namespace {

using ArrayBodyPtr = std::shared_ptr<internal::ArrayBody>;
using ConstArrayBodyPtr = std::shared_ptr<const internal::ArrayBody>;

Dtype NumpyDtypeToDtype(const py::dtype& npdtype) {
    switch (npdtype.kind()) {
        case 'b':
            return Dtype::kBool;
        case 'i':
            switch (npdtype.itemsize()) {
                case 1:
                    return Dtype::kInt8;
                case 2:
                    return Dtype::kInt16;
                case 4:
                    return Dtype::kInt32;
                case 8:
                    return Dtype::kInt64;
                default:
                    break;
            }
            break;
        case 'u':
            switch (npdtype.itemsize()) {
                case 1:
                    return Dtype::kUInt8;
                default:
                    break;
            }
            break;
        case 'f':
            switch (npdtype.itemsize()) {
                case 4:
                    return Dtype::kFloat32;
                case 8:
                    return Dtype::kFloat64;
                default:
                    break;
            }
            break;
        default:
            break;
    }
    throw DtypeError("unsupported NumPy dtype");
}

ArrayBodyPtr MakeArray(const Shape& shape, Dtype dtype, py::list list) {
    auto total_size = shape.total_size();
    auto bytes = GetElementSize(dtype) * total_size;
    if (static_cast<size_t>(total_size) != list.size()) {
        throw DimensionError("Invalid data length");
    }

    // Allocate a buffer and copy data
    std::shared_ptr<void> ptr = std::make_unique<uint8_t[]>(bytes);
    VisitDtype(dtype, [&](auto pt) {
        using T = typename decltype(pt)::type;
        std::transform(list.begin(), list.end(), static_cast<T*>(ptr.get()), [](auto& item) { return py::cast<T>(item); });
    });
    return Array::FromBuffer(shape, dtype, ptr).move_body();
}

ArrayBodyPtr MakeArray(py::array array) {
    if ((array.flags() & py::array::c_style) == 0) {
        throw DimensionError("cannot convert non-contiguous NumPy array to Array");
    }

    Dtype dtype = NumpyDtypeToDtype(array.dtype());
    py::buffer_info info = array.request();
    Shape shape(info.shape);

    // data holds the copy of py::array which in turn references the NumPy array and the buffer is therefore not released
    std::shared_ptr<void> data(std::make_shared<py::array>(std::move(array)), array.mutable_data());

    return Array::FromBuffer(shape, dtype, data).move_body();
}

py::buffer_info MakeNumpyArrayFromArray(internal::ArrayBody& self) {
    // Used as a temporary accessor
    Array array{std::move(ArrayBodyPtr(&self, [](internal::ArrayBody* ptr) {
        (void)ptr;  // unused
    }))};

    if (!array.is_contiguous()) {
        throw DimensionError("cannot convert non-contiguous Array to NumPy array");
    }

    int64_t itemsize{GetElementSize(array.dtype())};
    const Shape& shape = array.shape();

    // compute C-contiguous strides
    size_t ndim = array.ndim();
    std::vector<size_t> strides(ndim);
    if (ndim > 0) {
        std::partial_sum(shape.crbegin(), shape.crend() - 1, strides.rbegin() + 1, std::multiplies<size_t>());
        strides.back() = 1;
        std::transform(strides.crbegin(), strides.crend(), strides.rbegin(), [&itemsize](size_t item) { return item * itemsize; });
    }

    return py::buffer_info(array.data().get(), itemsize, std::string(1, GetCharCode(array.dtype())), ndim, shape, strides);
}

}  // namespace

void InitXchainerArray(pybind11::module& m) {
    py::class_<internal::ArrayBody, ArrayBodyPtr>{m, "Array", py::buffer_protocol()}
        .def(py::init(py::overload_cast<const Shape&, Dtype, py::list>(&MakeArray)))
        .def(py::init(py::overload_cast<py::array>(&MakeArray)))
        .def_buffer(&MakeNumpyArrayFromArray)
        .def("view",
             [](const ArrayBodyPtr& self) {
                 // Duplicate the array body
                 return std::make_shared<internal::ArrayBody>(*self);
             })
        .def("__iadd__", [](const ArrayBodyPtr& self, const ArrayBodyPtr& rhs) { return (Array{self} += Array{rhs}).move_body(); })
        .def("__imul__", [](const ArrayBodyPtr& self, const ArrayBodyPtr& rhs) { return (Array{self} *= Array{rhs}).move_body(); })
        .def("__add__", [](const ArrayBodyPtr& self, const ArrayBodyPtr& rhs) { return (Array{self} + Array{rhs}).move_body(); })
        .def("__mul__", [](const ArrayBodyPtr& self, const ArrayBodyPtr& rhs) { return (Array{self} * Array{rhs}).move_body(); })
        .def("__repr__", [](const ArrayBodyPtr& self) { return Array{self}.ToString(); })
        .def("copy", [](const ArrayBodyPtr& self) { return Array{self}.Copy().move_body(); })
        .def_property("requires_grad", [](const ArrayBodyPtr& self) { return Array{self}.IsGradRequired(); },
                      [](const ArrayBodyPtr& self, bool value) {
                          auto array = Array{self};
                          // TODO(hvy): requires_grad should not be a boolean property but a method that takes a graph id argument, aligning
                          // to the c++ interface. Currently, this property is broken in the sense that once the required_grad flag is set
                          // to true (and an ArrayNode is created internally) it cannot be unset.
                          if (value && !array.IsGradRequired()) {
                              array.RequireGrad();
                          }
                      })
        .def_property("grad",
                      [](const ArrayBodyPtr& self) -> ConstArrayBodyPtr {
                          auto array = Array{self};
                          if (array.IsGradRequired()) {
                              return array.GetGrad()->body();
                          } else {
                              return nullptr;
                          }
                      },
                      [](const ArrayBodyPtr& self, const ArrayBodyPtr& grad) {
                          auto array = Array{self};
                          if (grad) {
                              if (array.IsGradRequired()) {
                                  array.SetGrad(Array{grad});
                              } else {
                                  array.RequireGrad().SetGrad(Array{grad});
                              }
                          } else {
                              array.ClearGrad();
                          }
                      })
        .def_property_readonly("dtype", [](const ArrayBodyPtr& self) { return Array{self}.dtype(); })
        .def_property_readonly("element_bytes", [](const ArrayBodyPtr& self) { return Array{self}.element_bytes(); })
        .def_property_readonly("is_contiguous", [](const ArrayBodyPtr& self) { return Array{self}.is_contiguous(); })
        .def_property_readonly("ndim", [](const ArrayBodyPtr& self) { return Array{self}.ndim(); })
        .def_property_readonly("offset", [](const ArrayBodyPtr& self) { return Array{self}.offset(); })
        .def_property_readonly("shape", [](const ArrayBodyPtr& self) { return Array{self}.shape(); })
        .def_property_readonly("total_bytes", [](const ArrayBodyPtr& self) { return Array{self}.total_bytes(); })
        .def_property_readonly("total_size", [](const ArrayBodyPtr& self) { return Array{self}.total_size(); })
        .def_property_readonly("_debug_data_memory_address",  // These methods starting with `_debug_` are stubs for testing
                               [](const ArrayBodyPtr& self) { return reinterpret_cast<std::uintptr_t>(Array{self}.data().get()); })
        .def_property_readonly("_debug_flat_data", [](const ArrayBodyPtr& self) {
            py::list list;
            Array array{self};

            // Copy data into the list
            VisitDtype(array.dtype(), [&array, &list](auto pt) {
                using T = typename decltype(pt)::type;
                auto size = array.total_size();
                const T& data = *std::static_pointer_cast<const T>(array.data());
                for (int64_t i = 0; i < size; ++i) {
                    list.append((&data)[i]);
                }
            });

            return list;
        });
}

}  // namespace xchainer
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  $RCSfile: imexp.cxx,v $
 *
 *  $Revision: 1.15 $
 *
 *  last change: $Author: dbo $ $Date: 2001-09-24 12:46:22 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#include <stdio.h>
#include <rtl/memory.h>

#include <rtl/ustrbuf.hxx>

#include <xmlscript/xmldlg_imexp.hxx>
#include <xmlscript/xml_helper.hxx>

#include <cppuhelper/servicefactory.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <cppuhelper/implbase2.hxx>

#include <comphelper/processfactory.hxx>
#include <comphelper/regpathhelper.hxx>

#include <tools/debug.hxx>
#include <vcl/svapp.hxx>
#include <svtools/unoiface.hxx> // InitExtToolkit

#include <com/sun/star/io/XActiveDataSource.hpp>

#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>

#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/registry/XImplementationRegistration.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>

#include <com/sun/star/awt/XToolkit.hpp>
#include <com/sun/star/awt/XControlModel.hpp>

#include <com/sun/star/container/XNameContainer.hpp>

using namespace ::rtl;
using namespace ::cppu;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;

static void reg(
    Reference< registry::XImplementationRegistration > const & xReg,
    char const * lib,
    bool upd = false )
{
    OUStringBuffer buf( 32 );
#ifndef SAL_W32
    buf.appendAsciiL( RTL_CONSTASCII_STRINGPARAM("lib") );
#endif
    buf.appendAscii( lib );

    if (upd)
    {
        buf.append( (sal_Int32)SUPD );
#ifdef SAL_W32
        buf.appendAscii( "mi" );
#else
#ifdef SOLARIS
        buf.appendAscii( "ss" );
#else
        buf.appendAscii( "li" );
#endif
#endif
    }
#ifndef SAL_W32
    buf.appendAscii( ".so" );
#endif

    xReg->registerImplementation(
        OUString::createFromAscii( "com.sun.star.loader.SharedLibrary" ),
        buf.makeStringAndClear(), Reference< registry::XSimpleRegistry >() );
}

Reference< XComponentContext > createInitialComponentContext()
{
    Reference< XComponentContext > xContext;

    try
    {
        ::rtl::OUString localRegistry = OUString::createFromAscii( "xmlscript.rdb" ); //"::comphelper::getPathToUserRegistry();
        ::rtl::OUString systemRegistry = ::comphelper::getPathToSystemRegistry();

        Reference< registry::XSimpleRegistry > xLocalRegistry( ::cppu::createSimpleRegistry() );
        Reference< registry::XSimpleRegistry > xSystemRegistry( ::cppu::createSimpleRegistry() );
        if ( xLocalRegistry.is() && (localRegistry.getLength() > 0) )
        {
            try
            {
                xLocalRegistry->open( localRegistry, sal_False, sal_True);
            }
            catch ( registry::InvalidRegistryException& )
            {
            }

            if ( !xLocalRegistry->isValid() )
                xLocalRegistry->open(localRegistry, sal_True, sal_True);
        }

        if ( xSystemRegistry.is() && (systemRegistry.getLength() > 0) )
            xSystemRegistry->open( systemRegistry, sal_True, sal_False);

        if ( (xLocalRegistry.is() && xLocalRegistry->isValid()) &&
             (xSystemRegistry.is() && xSystemRegistry->isValid()) )
        {
            Reference < registry::XSimpleRegistry > xReg( ::cppu::createNestedRegistry() );
            Sequence< Any > seqAnys(2);
            seqAnys[0] <<= xLocalRegistry ;
            seqAnys[1] <<= xSystemRegistry ;
            Reference< lang::XInitialization > xInit( xReg, UNO_QUERY );
            xInit->initialize( seqAnys );

            xContext = ::cppu::bootstrap_InitialComponentContext( xReg );
        }
        else
        {
            throw Exception(
                OUString( RTL_CONSTASCII_USTRINGPARAM("no registry!") ),
                Reference< XInterface >() );
        }

        Reference< XInterface > x( xContext->getServiceManager()->createInstanceWithContext(
            OUString::createFromAscii( "com.sun.star.xml.sax.Parser" ), xContext ) );
        if (! x.is()) // register only once
        {
            Reference < registry::XImplementationRegistration > xReg(
                xContext->getServiceManager()->createInstanceWithContext(
                    OUString::createFromAscii( "com.sun.star.registry.ImplementationRegistration" ), xContext ), UNO_QUERY );

            reg( xReg, "sax" );
            reg( xReg, "tk", true );
            reg( xReg, "svt", true );
            reg( xReg, "i18n", true );
        }
    }

    catch( Exception& rExc )
    {
        OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
        OSL_ENSURE( 0, aStr.getStr() );
    }

    return xContext;
}

// -----------------------------------------------------------------------

Reference< container::XNameContainer > importFile(
    char const * fname,
    Reference< XComponentContext > const & xContext )
{
    // create the input stream
    FILE *f = ::fopen( fname, "rb" );
    if (f)
    {
        ::fseek( f, 0 ,SEEK_END );
        int nLength = ::ftell( f );
        ::fseek( f, 0, SEEK_SET );

        ByteSequence bytes( nLength );
        ::fread( bytes.getArray(), nLength, 1, f );
        ::fclose( f );

        Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
            OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ), xContext ), UNO_QUERY );
        ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );

        return xModel;
    }
    else
    {
        throw Exception( OUString( RTL_CONSTASCII_USTRINGPARAM("### Cannot read file!") ),
                         Reference< XInterface >() );
    }
}

void exportToFile(
    char const * fname,
    Reference< container::XNameContainer > const & xModel,
    Reference< XComponentContext > const & xContext )
{
    Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
    Reference< io::XInputStream > xStream( xProvider->createInputStream() );

    Sequence< sal_Int8 > bytes;
    sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
    for (;;)
    {
        Sequence< sal_Int8 > readBytes;
        nRead = xStream->readBytes( readBytes, 1024 );
        if (! nRead)
            break;
        OSL_ASSERT( readBytes.getLength() >= nRead );

        sal_Int32 nPos = bytes.getLength();
        bytes.realloc( nPos + nRead );
        ::rtl_copyMemory( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
    }

    FILE * f = ::fopen( fname, "w" );
    ::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
    ::fflush( f );
    ::fclose( f );
}

class MyApp : public Application
{
public:
    void Main();
};

MyApp aMyApp;

// -----------------------------------------------------------------------

void MyApp::Main()
{
    if (GetCommandLineParamCount() < 1)
    {
        OSL_ENSURE( 0, "usage: imexp inputfile [outputfile]\n" );
        return;
    }

    Reference< XComponentContext > xContext( createInitialComponentContext() );
    Reference< lang::XMultiServiceFactory >  xMSF( xContext->getServiceManager(), UNO_QUERY );

    try
    {
        ::comphelper::setProcessServiceFactory( xMSF );

        Reference< awt::XToolkit> xToolkit( xMSF->createInstance(
            OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.ExtToolkit" ) ) ), UNO_QUERY );

        // import dialogs
        OString aParam1( OUStringToOString( OUString( GetCommandLineParam( 0 ) ), RTL_TEXTENCODING_ASCII_US ) );
        Reference< container::XNameContainer > xModel( importFile( aParam1.getStr(), xContext ) );
        OSL_ASSERT( xModel.is() );

        Reference< awt::XControl > xDlg( xMSF->createInstance(
            OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialog" ) ) ), UNO_QUERY );
        xDlg->setModel( Reference< awt::XControlModel >::query( xModel ) );
        xDlg->createPeer( xToolkit, 0 );
        Reference< awt::XDialog > xD( xDlg, UNO_QUERY );
        xD->execute();

        if (GetCommandLineParamCount() == 2)
        {
            // write modified dialogs
            OString aParam2( OUStringToOString( OUString( GetCommandLineParam( 1 ) ), RTL_TEXTENCODING_ASCII_US ) );
            exportToFile( aParam2.getStr(), xModel, xContext );
        }
    }
    catch (xml::sax::SAXException & rExc)
    {
        OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
        uno::Exception exc;
        if (rExc.WrappedException >>= exc)
        {
            aStr += OString( " >>> " );
            aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
        }
        OSL_ENSURE( 0, aStr.getStr() );
    }
    catch (uno::Exception & rExc)
    {
        OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
        OSL_ENSURE( 0, aStr.getStr() );
    }

    Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
    if (xComp.is())
    {
        xComp->dispose();
    }
}

<commit_msg>INTEGRATION: CWS uno2 (1.15.42); FILE MERGED 2003/04/08 14:01:19 dbo 1.15.42.1: #107991# UNO shared library component renaming<commit_after>/*************************************************************************
 *
 *  $RCSfile: imexp.cxx,v $
 *
 *  $Revision: 1.16 $
 *
 *  last change: $Author: rt $ $Date: 2003-04-23 16:54:52 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#include <stdio.h>
#include <rtl/memory.h>

#include <rtl/ustrbuf.hxx>

#include <xmlscript/xmldlg_imexp.hxx>
#include <xmlscript/xml_helper.hxx>

#include <cppuhelper/servicefactory.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <cppuhelper/implbase2.hxx>

#include <comphelper/processfactory.hxx>
#include <comphelper/regpathhelper.hxx>

#include <tools/debug.hxx>
#include <vcl/svapp.hxx>

#include <com/sun/star/io/XActiveDataSource.hpp>

#include <com/sun/star/lang/XComponent.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>

#include <com/sun/star/registry/XSimpleRegistry.hpp>
#include <com/sun/star/registry/XImplementationRegistration.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>

#include <com/sun/star/awt/XToolkit.hpp>
#include <com/sun/star/awt/XControlModel.hpp>
#include <com/sun/star/awt/XControl.hpp>
#include <com/sun/star/awt/XDialog.hpp>

#include <com/sun/star/container/XNameContainer.hpp>

using namespace ::rtl;
using namespace ::cppu;
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;

static void reg(
    Reference< registry::XImplementationRegistration > const & xReg,
    char const * lib,
    bool upd = false )
{
    OUStringBuffer buf( 32 );
#ifndef SAL_W32
    buf.appendAsciiL( RTL_CONSTASCII_STRINGPARAM("lib") );
#endif
    buf.appendAscii( lib );

    if (upd)
    {
        buf.append( (sal_Int32)SUPD );
#ifdef SAL_W32
        buf.appendAscii( "mi" );
#else
#ifdef SOLARIS
        buf.appendAscii( "ss" );
#else
        buf.appendAscii( "li" );
#endif
#endif
    }
#ifndef SAL_W32
    buf.appendAscii( ".so" );
#endif

    xReg->registerImplementation(
        OUString::createFromAscii( "com.sun.star.loader.SharedLibrary" ),
        buf.makeStringAndClear(), Reference< registry::XSimpleRegistry >() );
}

Reference< XComponentContext > createInitialComponentContext()
{
    Reference< XComponentContext > xContext;

    try
    {
        ::rtl::OUString localRegistry = OUString::createFromAscii( "xmlscript.rdb" ); //"::comphelper::getPathToUserRegistry();
        ::rtl::OUString systemRegistry = ::comphelper::getPathToSystemRegistry();

        Reference< registry::XSimpleRegistry > xLocalRegistry( ::cppu::createSimpleRegistry() );
        Reference< registry::XSimpleRegistry > xSystemRegistry( ::cppu::createSimpleRegistry() );
        if ( xLocalRegistry.is() && (localRegistry.getLength() > 0) )
        {
            try
            {
                xLocalRegistry->open( localRegistry, sal_False, sal_True);
            }
            catch ( registry::InvalidRegistryException& )
            {
            }

            if ( !xLocalRegistry->isValid() )
                xLocalRegistry->open(localRegistry, sal_True, sal_True);
        }

        if ( xSystemRegistry.is() && (systemRegistry.getLength() > 0) )
            xSystemRegistry->open( systemRegistry, sal_True, sal_False);

        if ( (xLocalRegistry.is() && xLocalRegistry->isValid()) &&
             (xSystemRegistry.is() && xSystemRegistry->isValid()) )
        {
            Reference < registry::XSimpleRegistry > xReg( ::cppu::createNestedRegistry() );
            Sequence< Any > seqAnys(2);
            seqAnys[0] <<= xLocalRegistry ;
            seqAnys[1] <<= xSystemRegistry ;
            Reference< lang::XInitialization > xInit( xReg, UNO_QUERY );
            xInit->initialize( seqAnys );

            xContext = ::cppu::bootstrap_InitialComponentContext( xReg );
        }
        else
        {
            throw Exception(
                OUString( RTL_CONSTASCII_USTRINGPARAM("no registry!") ),
                Reference< XInterface >() );
        }

        Reference< XInterface > x( xContext->getServiceManager()->createInstanceWithContext(
            OUString::createFromAscii( "com.sun.star.xml.sax.Parser" ), xContext ) );
        if (! x.is()) // register only once
        {
            Reference < registry::XImplementationRegistration > xReg(
                xContext->getServiceManager()->createInstanceWithContext(
                    OUString::createFromAscii( "com.sun.star.registry.ImplementationRegistration" ), xContext ), UNO_QUERY );

            xReg->registerImplementation(
                OUString::createFromAscii("com.sun.star.loader.SharedLibrary"),
                OUString::createFromAscii("sax.uno" SAL_DLLEXTENSION),
                Reference< registry::XSimpleRegistry >() );
            reg( xReg, "sax" );
            reg( xReg, "tk", true );
            reg( xReg, "svt", true );
            reg( xReg, "i18n", true );
        }
    }

    catch( Exception& rExc )
    {
        OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
        OSL_ENSURE( 0, aStr.getStr() );
    }

    return xContext;
}

// -----------------------------------------------------------------------

Reference< container::XNameContainer > importFile(
    char const * fname,
    Reference< XComponentContext > const & xContext )
{
    // create the input stream
    FILE *f = ::fopen( fname, "rb" );
    if (f)
    {
        ::fseek( f, 0 ,SEEK_END );
        int nLength = ::ftell( f );
        ::fseek( f, 0, SEEK_SET );

        ByteSequence bytes( nLength );
        ::fread( bytes.getArray(), nLength, 1, f );
        ::fclose( f );

        Reference< container::XNameContainer > xModel( xContext->getServiceManager()->createInstanceWithContext(
            OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialogModel" ) ), xContext ), UNO_QUERY );
        ::xmlscript::importDialogModel( ::xmlscript::createInputStream( bytes ), xModel, xContext );

        return xModel;
    }
    else
    {
        throw Exception( OUString( RTL_CONSTASCII_USTRINGPARAM("### Cannot read file!") ),
                         Reference< XInterface >() );
    }
}

void exportToFile(
    char const * fname,
    Reference< container::XNameContainer > const & xModel,
    Reference< XComponentContext > const & xContext )
{
    Reference< io::XInputStreamProvider > xProvider( ::xmlscript::exportDialogModel( xModel, xContext ) );
    Reference< io::XInputStream > xStream( xProvider->createInputStream() );

    Sequence< sal_Int8 > bytes;
    sal_Int32 nRead = xStream->readBytes( bytes, xStream->available() );
    for (;;)
    {
        Sequence< sal_Int8 > readBytes;
        nRead = xStream->readBytes( readBytes, 1024 );
        if (! nRead)
            break;
        OSL_ASSERT( readBytes.getLength() >= nRead );

        sal_Int32 nPos = bytes.getLength();
        bytes.realloc( nPos + nRead );
        ::rtl_copyMemory( bytes.getArray() + nPos, readBytes.getConstArray(), (sal_uInt32)nRead );
    }

    FILE * f = ::fopen( fname, "w" );
    ::fwrite( bytes.getConstArray(), 1, bytes.getLength(), f );
    ::fflush( f );
    ::fclose( f );
}

class MyApp : public Application
{
public:
    void Main();
};

MyApp aMyApp;

// -----------------------------------------------------------------------

void MyApp::Main()
{
    if (GetCommandLineParamCount() < 1)
    {
        OSL_ENSURE( 0, "usage: imexp inputfile [outputfile]\n" );
        return;
    }

    Reference< XComponentContext > xContext( createInitialComponentContext() );
    Reference< lang::XMultiServiceFactory >  xMSF( xContext->getServiceManager(), UNO_QUERY );

    try
    {
        ::comphelper::setProcessServiceFactory( xMSF );

        Reference< awt::XToolkit> xToolkit( xMSF->createInstance(
            OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.ExtToolkit" ) ) ), UNO_QUERY );

        // import dialogs
        OString aParam1( OUStringToOString( OUString( GetCommandLineParam( 0 ) ), RTL_TEXTENCODING_ASCII_US ) );
        Reference< container::XNameContainer > xModel( importFile( aParam1.getStr(), xContext ) );
        OSL_ASSERT( xModel.is() );

        Reference< awt::XControl > xDlg( xMSF->createInstance(
            OUString(RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControlDialog" ) ) ), UNO_QUERY );
        xDlg->setModel( Reference< awt::XControlModel >::query( xModel ) );
        xDlg->createPeer( xToolkit, 0 );
        Reference< awt::XDialog > xD( xDlg, UNO_QUERY );
        xD->execute();

        if (GetCommandLineParamCount() == 2)
        {
            // write modified dialogs
            OString aParam2( OUStringToOString( OUString( GetCommandLineParam( 1 ) ), RTL_TEXTENCODING_ASCII_US ) );
            exportToFile( aParam2.getStr(), xModel, xContext );
        }
    }
    catch (xml::sax::SAXException & rExc)
    {
        OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
        uno::Exception exc;
        if (rExc.WrappedException >>= exc)
        {
            aStr += OString( " >>> " );
            aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
        }
        OSL_ENSURE( 0, aStr.getStr() );
    }
    catch (uno::Exception & rExc)
    {
        OString aStr( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
        OSL_ENSURE( 0, aStr.getStr() );
    }

    Reference< lang::XComponent > xComp( xContext, UNO_QUERY );
    if (xComp.is())
    {
        xComp->dispose();
    }
}

<|endoftext|>


<commit_before>#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
using namespace cv;

#include "header/negative.h"
#include "header/contrast.h"
#include "header/threshold.h"
#include "header/crop.h"

printBefore(Mat img){
    namedWindow("Before",CV_WINDOW_AUTOSIZE);
    imshow("Before", img);
}

printAfter(Mat img){
    namedWindow("After",CV_WINDOW_AUTOSIZE);
    imshow("After", img);
}

int main(){
    Mat img;

    int answer;
    do{
        cout<<"What do you want?\n\
        1. Face Detection\n\
        2. Negative Image\n\
        3. Change Contrast\n\
        4. Cropping\n\
        0. Quit\n";

        cin>>answer;

        if(answer==1){
            img = imread("picture/1.png");
            Mat img2 = img;
            printBefore(img);

            img = threshold(img);
            namedWindow("128",CV_WINDOW_AUTOSIZE);
            imshow("128", img);

            img2 = thresholdOtsu(img2);
            namedWindow("Otsu",CV_WINDOW_AUTOSIZE);
            imshow("Otsu", img2);

            cout<<"Thank you"<<endl;
        }else if(answer==2){
            img = imread("picture/1.png");
            printBefore(img);

            img = negative(img);
            printAfter(img);
            cout<<"Thank you"<<endl;
        }else if(answer==3){
            img = imread("picture/3.jpg");
            printBefore(img);

            img = contrast(img);
            printAfter(img);
            cout<<"Thank you"<<endl;
        }else if(answer==4){
            img = imread("picture/4.jpg");
            printBefore(img);

            img = crop(img);
            printAfter(img);
            cout<<"Thank you"<<endl;
        }
        waitKey();
    }while (answer!=0);
    return 0;
}
<commit_msg>- fix bug (img clone) - make more simple with utils<commit_after>#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
using namespace cv;

#include "header/utils.h"
#include "header/negative.h"
#include "header/contrast.h"
#include "header/threshold.h"
#include "header/crop.h"

int main(){
    Mat img,img2;
    int answer;
    do{
        cout<<"What do you want?\n\
        1. Face Detection\n\
        2. Negative Image\n\
        3. Change Contrast\n\
        4. Cropping\n\
        0. Quit\n";

        cin>>answer;

        if(answer==1){
            img = imread("picture/1.png");
            img2 = img.clone();
            printMat("Before",img);

            img = threshold(img);
            printMat("128",img);

            img2 = thresholdOtsu(img2);
            printMat("Otsu",img2);

            cout<<"Thank you"<<endl;
        }else if(answer==2){
            img = imread("picture/1.png");
            printMat("Before",img);

            img = negative(img);
            printMat("After",img);

            cout<<"Thank you"<<endl;
        }else if(answer==3){
            img = imread("picture/3.jpg");
            printMat("Before",img);

            img = contrast(img);
            printMat("After",img);

            cout<<"Thank you"<<endl;
        }else if(answer==4){
            img = imread("picture/4.jpg");
            img2 = img.clone();
            printMat("Before",img);

            img = cropSilhouette(img);
            printMat("Crop Silhouette",img);

            img2 = cropSubtract(img2);
            printMat("Crop Substract",img2);

            printMat("Object",getObject());

            cout<<"Thank you"<<endl;
        }else{
            cout<<"Sorry, your answer in not in our list. Please select again."<<endl;

        }
        img.release();
        img2.release();
        waitKey();
    }while (answer!=0);
    return 0;
}
<|endoftext|>


<commit_before>
// This program if for checking the kinematics class is producing sensible results.
// It should be turned into a full unit test.

#include "dogbot/LegKinematics.hh"
#include "dogbot/Util.hh"
#include <iostream>
#include <math.h>

DogBotN::LegKinematicsC g_legKinematics;

static float deg2rad(float deg)
{
  return ((float) deg *2.0 * M_PI)/ 360.0;
}

static float rad2deg(float deg)
{
  return ((float) deg * 360.0) / (2.0 * M_PI);
}

float Diff(const DogBotN::LegKinematicsC &legKinematics,float theta) {
  float delta = 1e-4;
  float psi = legKinematics.Linkage4BarForward(theta);
  float psiD = legKinematics.Linkage4BarForward(theta+delta);
  return delta/(psiD-psi);
  //return (psiD-psi)/delta;
}

int CheckLinkageAngles() {

  for(int i = 0;i < 360;i+=10) {
    float theta = deg2rad((float) i);
    float psi = g_legKinematics.Linkage4BarForward(theta);
    float back = 0;
    float back2 = 0;
    bool ok = g_legKinematics.Linkage4BarBack(psi,back,false);
    g_legKinematics.Linkage4BarBack(psi,back2,true);
    float ratio1 = g_legKinematics.LinkageSpeedRatio(back,psi);
    float ratio2 = g_legKinematics.LinkageSpeedRatio(back2,psi);
    float diff1 = Diff(g_legKinematics,back);
    float diff2 = Diff(g_legKinematics,back2);
    std::cout << i << " Fwd:" << rad2deg(psi) << " Inv:" << rad2deg(back)  << " (" << ratio1 << " " << diff1 << ") "
                                                         << rad2deg(back2) << " (" << ratio2 << " " << diff2 << ") " << "  [" << ok << "]" << std::endl;
  }

  return 0;
}

int TestFixedAngles()
{
  {
    std::cout << "Zero:" << std::endl;
    Eigen::Vector3f pos;
    Eigen::Vector3f angles = {0,M_PI/2.0,M_PI/2};
    if(!g_legKinematics.ForwardVirtual(angles,pos)) {
      std::cerr << "Forward kinematics failed." << std::endl;
      return 1;
    }
    std::cout << " Angles:" << DogBotN::Rad2Deg(angles[0]) << " " << DogBotN::Rad2Deg(angles[1]) << " " << DogBotN::Rad2Deg(angles[2]) << " " << std::endl;
    std::cout << "     At: "<< pos[0] << " " << pos[1] << " " << pos[2] << " " << std::endl;
  }
  {
    Eigen::Vector3f pos;
    Eigen::Vector3f angles2 = {0,0,0};
    if(!g_legKinematics.ForwardVirtual(angles2,pos)) {
      std::cerr << "Forward kinematics failed." << std::endl;
      return 1;
    }
    std::cout <<" 0,0,0 at: "<< pos[0] << " " << pos[1] << " " << pos[2] << " " << std::endl;
  }
  return 0;
}

int CheckTargetVirtual(Eigen::Vector3f target) {
  DogBotN::LegKinematicsC legKinematics;
  Eigen::Vector3f angles;
  Eigen::Vector3f pos;

  if(!g_legKinematics.InverseVirtual(target,angles)) {
    std::cerr << "Failed." << std::endl;
    return __LINE__;
  }
  std::cout << " Virtual Angles:" << DogBotN::Rad2Deg(angles[0]) << " " << DogBotN::Rad2Deg(angles[1]) << " " << DogBotN::Rad2Deg(angles[2]) << " " << std::endl;

  if(!g_legKinematics.ForwardVirtual(angles,pos)) {
    std::cerr << "Forward kinematics failed." << std::endl;
    return __LINE__;
  }

  float dist = (target - pos).norm();
  std::cout <<" Target: "<< target[0] << " " << target[1] << " " << target[2] << " " << std::endl;
  std::cout <<" At: "<< pos[0] << " " << pos[1] << " " << pos[2] << "   Distance:" << dist << std::endl;
  if(dist > 1e-6) {
    return __LINE__;
  }
  return 0;
}

int CheckTargetDirect(Eigen::Vector3f target) {
  DogBotN::LegKinematicsC legKinematics;
  Eigen::Vector3f angles;
  Eigen::Vector3f pos;

  if(!g_legKinematics.InverseDirect(target,angles)) {
    std::cerr << "Failed to find solution for target :" << target << " " << std::endl;
    return __LINE__;
  }
  std::cout << " Direct Angles:" << DogBotN::Rad2Deg(angles[0]) << " " << DogBotN::Rad2Deg(angles[1]) << " " << DogBotN::Rad2Deg(angles[2]) << " " << std::endl;

  if(!g_legKinematics.ForwardDirect(angles,pos)) {
    std::cerr << "Forward kinematics failed." << std::endl;
    return __LINE__;
  }

  float dist = (target - pos).norm();
  std::cout <<" Target: "<< target[0] << " " << target[1] << " " << target[2] << " " << std::endl;
  std::cout <<" At: "<< pos[0] << " " << pos[1] << " " << pos[2] << "   Distance:" << dist << std::endl;
  if(dist > 1e-6) {
    return __LINE__;
  }
  return 0;
}

int TestReachableTargets()
{

  {
    int ln = 0;
    Eigen::Vector3f target = {0.15,0.1,0.3};
    if((ln = CheckTargetDirect(target)) != 0) {
      std::cerr << "Check target failed at " << ln << std::endl;
      return __LINE__;
    }
  }

  {
    int ln = 0;
    float maxReach = g_legKinematics.MaxExtension() * 0.99;
    Eigen::Vector3f target = {0,0,maxReach};
    if((ln = CheckTargetDirect(target)) != 0) {
      std::cerr << "Check target failed at " << ln << std::endl;
      return __LINE__;
    }
  }

  {
    int ln = 0;
    float minReach = g_legKinematics.MinExtension() * 1.01;
    Eigen::Vector3f target = {0,0,minReach};
    if((ln = CheckTargetDirect(target)) != 0) {
      std::cerr << "Check target failed at " << ln << std::endl;
      return __LINE__;
    }
  }

  return 0;
}

int main() {
  int ln = 0;
  if((ln = CheckLinkageAngles()) != 0) {
    std::cerr << "Test failed at " << ln << " " << std::endl;
    return 1;
  }
  if((ln = TestFixedAngles()) != 0) {
    std::cerr << "Test failed at " << ln << " " << std::endl;
    return 1;
  }
  if((ln = TestReachableTargets()) != 0) {
    std::cerr << "Test failed at " << ln << " " << std::endl;
    return 1;
  }
  std::cerr << "Test passed." << std::endl;
  return 0;
}
<commit_msg>No need to pull in limits.<commit_after>
// This program if for checking the kinematics class is producing sensible results.
// It should be turned into a full unit test.

#include "dogbot/LegKinematics.hh"
#include "dogbot/Util.hh"
#include <iostream>
#include <math.h>

DogBotN::LegKinematicsC g_legKinematics;

static float deg2rad(float deg)
{
  return ((float) deg *2.0 * M_PI)/ 360.0;
}

static float rad2deg(float deg)
{
  return ((float) deg * 360.0) / (2.0 * M_PI);
}

float Diff(const DogBotN::LegKinematicsC &legKinematics,float theta) {
  float delta = 1e-4;
  float psi = legKinematics.Linkage4BarForward(theta);
  float psiD = legKinematics.Linkage4BarForward(theta+delta);
  return delta/(psiD-psi);
  //return (psiD-psi)/delta;
}

int CheckLinkageAngles() {

  for(int i = 0;i < 360;i+=10) {
    float theta = deg2rad((float) i);
    float psi = g_legKinematics.Linkage4BarForward(theta);
    float back = 0;
    float back2 = 0;
    bool ok = g_legKinematics.Linkage4BarBack(psi,back,false);
    g_legKinematics.Linkage4BarBack(psi,back2,true);
    float ratio1 = g_legKinematics.LinkageSpeedRatio(back,psi);
    float ratio2 = g_legKinematics.LinkageSpeedRatio(back2,psi);
    float diff1 = Diff(g_legKinematics,back);
    float diff2 = Diff(g_legKinematics,back2);
    std::cout << i << " Fwd:" << rad2deg(psi) << " Inv:" << rad2deg(back)  << " (" << ratio1 << " " << diff1 << ") "
                                                         << rad2deg(back2) << " (" << ratio2 << " " << diff2 << ") " << "  [" << ok << "]" << std::endl;
  }

  return 0;
}

int TestFixedAngles()
{
  {
    std::cout << "Zero:" << std::endl;
    Eigen::Vector3f pos;
    Eigen::Vector3f angles = {0,M_PI/2.0,M_PI/2};
    if(!g_legKinematics.ForwardVirtual(angles,pos)) {
      std::cerr << "Forward kinematics failed." << std::endl;
      return 1;
    }
    std::cout << " Angles:" << DogBotN::Rad2Deg(angles[0]) << " " << DogBotN::Rad2Deg(angles[1]) << " " << DogBotN::Rad2Deg(angles[2]) << " " << std::endl;
    std::cout << "     At: "<< pos[0] << " " << pos[1] << " " << pos[2] << " " << std::endl;
  }
  {
    Eigen::Vector3f pos;
    Eigen::Vector3f angles2 = {0,0,0};
    if(!g_legKinematics.ForwardVirtual(angles2,pos)) {
      std::cerr << "Forward kinematics failed." << std::endl;
      return 1;
    }
    std::cout <<" 0,0,0 at: "<< pos[0] << " " << pos[1] << " " << pos[2] << " " << std::endl;
  }
  return 0;
}

int CheckTargetVirtual(Eigen::Vector3f target) {
  DogBotN::LegKinematicsC legKinematics;
  Eigen::Vector3f angles;
  Eigen::Vector3f pos;

  if(!g_legKinematics.InverseVirtual(target,angles)) {
    std::cerr << "Failed." << std::endl;
    return __LINE__;
  }
  std::cout << " Virtual Angles:" << DogBotN::Rad2Deg(angles[0]) << " " << DogBotN::Rad2Deg(angles[1]) << " " << DogBotN::Rad2Deg(angles[2]) << " " << std::endl;

  if(!g_legKinematics.ForwardVirtual(angles,pos)) {
    std::cerr << "Forward kinematics failed." << std::endl;
    return __LINE__;
  }

  float dist = (target - pos).norm();
  std::cout <<" Target: "<< target[0] << " " << target[1] << " " << target[2] << " " << std::endl;
  std::cout <<" At: "<< pos[0] << " " << pos[1] << " " << pos[2] << "   Distance:" << dist << std::endl;
  if(dist > 1e-6) {
    return __LINE__;
  }
  return 0;
}

int CheckTargetDirect(Eigen::Vector3f target) {
  DogBotN::LegKinematicsC legKinematics;
  Eigen::Vector3f angles;
  Eigen::Vector3f pos;

  if(!g_legKinematics.InverseDirect(target,angles)) {
    std::cerr << "Failed to find solution for target :" << target << " " << std::endl;
    return __LINE__;
  }
  std::cout << " Direct Angles:" << DogBotN::Rad2Deg(angles[0]) << " " << DogBotN::Rad2Deg(angles[1]) << " " << DogBotN::Rad2Deg(angles[2]) << " " << std::endl;

  if(!g_legKinematics.ForwardDirect(angles,pos)) {
    std::cerr << "Forward kinematics failed." << std::endl;
    return __LINE__;
  }

  float dist = (target - pos).norm();
  std::cout <<" Target: "<< target[0] << " " << target[1] << " " << target[2] << " " << std::endl;
  std::cout <<" At: "<< pos[0] << " " << pos[1] << " " << pos[2] << "   Distance:" << dist << std::endl;
  if(dist > 1e-6) {
    return __LINE__;
  }
  return 0;
}

int TestReachableTargets()
{

  {
    int ln = 0;
    Eigen::Vector3f target = {0.15,0.1,0.3};
    if((ln = CheckTargetDirect(target)) != 0) {
      std::cerr << "Check target failed at " << ln << std::endl;
      return __LINE__;
    }
  }

  {
    int ln = 0;
    float maxReach = g_legKinematics.MaxExtension();
    Eigen::Vector3f target = {0,0,maxReach};
    if((ln = CheckTargetDirect(target)) != 0) {
      std::cerr << "Check target failed at " << ln << std::endl;
      return __LINE__;
    }
  }

  {
    int ln = 0;
    float minReach = g_legKinematics.MinExtension();
    Eigen::Vector3f target = {0,0,minReach};
    if((ln = CheckTargetDirect(target)) != 0) {
      std::cerr << "Check target failed at " << ln << std::endl;
      return __LINE__;
    }
  }

  return 0;
}

int main() {
  int ln = 0;
  if((ln = CheckLinkageAngles()) != 0) {
    std::cerr << "Test failed at " << ln << " " << std::endl;
    return 1;
  }
  if((ln = TestFixedAngles()) != 0) {
    std::cerr << "Test failed at " << ln << " " << std::endl;
    return 1;
  }
  if((ln = TestReachableTargets()) != 0) {
    std::cerr << "Test failed at " << ln << " " << std::endl;
    return 1;
  }
  std::cerr << "Test passed." << std::endl;
  return 0;
}
<|endoftext|>


<commit_before>#ifndef _SNARKFRONT_ADVANCED_ENCRYPTION_STD_HPP_
#define _SNARKFRONT_ADVANCED_ENCRYPTION_STD_HPP_

#include <array>

#include <snarkfront/AES_Cipher.hpp>
#include <snarkfront/AES_InvCipher.hpp>
#include <snarkfront/Alg.hpp>
#include <snarkfront/BitwiseAST.hpp>
#include <snarkfront/BitwiseINT.hpp>

namespace snarkfront {

////////////////////////////////////////////////////////////////////////////////
// AES variants
//

template <typename VAR, typename T, typename U, typename BITWISE>
class AES_All
{
public:
    AES_All() = default;

    typedef VAR VarType;
    typedef AES_Cipher<VAR, T, U, BITWISE> Encrypt;
    typedef AES_InvCipher<VAR, T, U, BITWISE> Decrypt;
    typedef Encrypt Algo;
    typedef Decrypt InvAlgo;

    typedef typename Encrypt::BlockType BlockType;
    typedef typename Encrypt::KeyExpansion KeyExpansion;
};

template <typename VAR, typename T, typename U, typename BITWISE>
class UNAES_All
{
public:
    UNAES_All() = default;

    typedef VAR VarType;
    typedef AES_Cipher<VAR, T, U, BITWISE> Encrypt;
    typedef AES_InvCipher<VAR, T, U, BITWISE> Decrypt;
    typedef Decrypt Algo;
    typedef Encrypt InvAlgo;

    typedef typename Decrypt::BlockType BlockType;
    typedef typename Decrypt::KeyExpansion KeyExpansion;
};

template <typename VAR, typename T, typename U, typename BITWISE>
class AES_128
{
public:
    AES_128() = default;

    typedef VAR VarType;
    typedef AES_Cipher<VAR, T, U, BITWISE> Encrypt;
    typedef AES_InvCipher<VAR, T, U, BITWISE> Decrypt;
    typedef Encrypt Algo;
    typedef Decrypt InvAlgo;

    typedef typename Encrypt::BlockType BlockType;
    typedef typename Encrypt::KeyExpansion KeyExpansion;
    typedef typename KeyExpansion::Key128Type KeyType;
    typedef typename KeyExpansion::Schedule128Type ScheduleType;
};

template <typename VAR, typename T, typename U, typename BITWISE>
class UNAES_128
{
public:
    UNAES_128() = default;

    typedef VAR VarType;
    typedef AES_Cipher<VAR, T, U, BITWISE> Encrypt;
    typedef AES_InvCipher<VAR, T, U, BITWISE> Decrypt;
    typedef Decrypt Algo;
    typedef Encrypt InvAlgo;

    typedef typename Decrypt::BlockType BlockType;
    typedef typename Decrypt::KeyExpansion KeyExpansion;
    typedef typename KeyExpansion::Key128Type KeyType;
    typedef typename KeyExpansion::Schedule128Type ScheduleType;
};

template <typename VAR, typename T, typename U, typename BITWISE>
class AES_192
{
public:
    AES_192() = default;

    typedef VAR VarType;
    typedef AES_Cipher<VAR, T, U, BITWISE> Encrypt;
    typedef AES_InvCipher<VAR, T, U, BITWISE> Decrypt;
    typedef Encrypt Algo;
    typedef Decrypt InvAlgo;

    typedef typename Encrypt::BlockType BlockType;
    typedef typename Encrypt::KeyExpansion KeyExpansion;
    typedef typename KeyExpansion::Key192Type KeyType;
    typedef typename KeyExpansion::Schedule192Type ScheduleType;
};

template <typename VAR, typename T, typename U, typename BITWISE>
class UNAES_192
{
public:
    UNAES_192() = default;

    typedef VAR VarType;
    typedef AES_Cipher<VAR, T, U, BITWISE> Encrypt;
    typedef AES_InvCipher<VAR, T, U, BITWISE> Decrypt;
    typedef Decrypt Algo;
    typedef Encrypt InvAlgo;

    typedef typename Decrypt::BlockType BlockType;
    typedef typename Decrypt::KeyExpansion KeyExpansion;
    typedef typename KeyExpansion::Key192Type KeyType;
    typedef typename KeyExpansion::Schedule192Type ScheduleType;
};

template <typename VAR, typename T, typename U, typename BITWISE>
class AES_256
{
public:
    AES_256() = default;

    typedef VAR VarType;
    typedef AES_Cipher<VAR, T, U, BITWISE> Encrypt;
    typedef AES_InvCipher<VAR, T, U, BITWISE> Decrypt;
    typedef Encrypt Algo;
    typedef Decrypt InvAlgo;

    typedef typename Encrypt::BlockType BlockType;
    typedef typename Encrypt::KeyExpansion KeyExpansion;
    typedef typename KeyExpansion::Key256Type KeyType;
    typedef typename KeyExpansion::Schedule256Type ScheduleType;
};

template <typename VAR, typename T, typename U, typename BITWISE>
class UNAES_256
{
public:
    UNAES_256() = default;

    typedef VAR VarType;
    typedef AES_Cipher<VAR, T, U, BITWISE> Encrypt;
    typedef AES_InvCipher<VAR, T, U, BITWISE> Decrypt;
    typedef Decrypt Algo;
    typedef Encrypt InvAlgo;

    typedef typename Decrypt::BlockType BlockType;
    typedef typename Decrypt::KeyExpansion KeyExpansion;
    typedef typename KeyExpansion::Key256Type KeyType;
    typedef typename KeyExpansion::Schedule256Type ScheduleType;
};

////////////////////////////////////////////////////////////////////////////////
// typedefs
//

namespace zk {
    template <typename FR> using
    AES = AES_All<
        AST_Var<Alg_uint8<FR>>, AST_Node<Alg_uint8<FR>>, AST_Op<Alg_uint8<FR>>,
        BitwiseAST<Alg_uint8<FR>>>;

    template <typename FR> using
    UNAES = UNAES_All<
        AST_Var<Alg_uint8<FR>>, AST_Node<Alg_uint8<FR>>, AST_Op<Alg_uint8<FR>>,
        BitwiseAST<Alg_uint8<FR>>>;

    template <typename FR> using
    AES128 = AES_128<
        AST_Var<Alg_uint8<FR>>, AST_Node<Alg_uint8<FR>>, AST_Op<Alg_uint8<FR>>,
        BitwiseAST<Alg_uint8<FR>>>;

    template <typename FR> using
    UNAES128 = UNAES_128<
        AST_Var<Alg_uint8<FR>>, AST_Node<Alg_uint8<FR>>, AST_Op<Alg_uint8<FR>>,
        BitwiseAST<Alg_uint8<FR>>>;

    template <typename FR> using
    AES192 = AES_192<
        AST_Var<Alg_uint8<FR>>, AST_Node<Alg_uint8<FR>>, AST_Op<Alg_uint8<FR>>,
        BitwiseAST<Alg_uint8<FR>>>;

    template <typename FR> using
    UNAES192 = UNAES_192<
        AST_Var<Alg_uint8<FR>>, AST_Node<Alg_uint8<FR>>, AST_Op<Alg_uint8<FR>>,
        BitwiseAST<Alg_uint8<FR>>>;

    template <typename FR> using
    AES256 = AES_256<
        AST_Var<Alg_uint8<FR>>, AST_Node<Alg_uint8<FR>>, AST_Op<Alg_uint8<FR>>,
        BitwiseAST<Alg_uint8<FR>>>;

    template <typename FR> using
    UNAES256 = UNAES_256<
        AST_Var<Alg_uint8<FR>>, AST_Node<Alg_uint8<FR>>, AST_Op<Alg_uint8<FR>>,
        BitwiseAST<Alg_uint8<FR>>>;
} // namespace zk

namespace eval {
    typedef AES_All<
        std::uint8_t, std::uint8_t, std::uint8_t, BitwiseINT<std::uint8_t>>
        AES;

    typedef UNAES_All<
        std::uint8_t, std::uint8_t, std::uint8_t, BitwiseINT<std::uint8_t>>
        UNAES;

    typedef AES_128<
        std::uint8_t, std::uint8_t, std::uint8_t, BitwiseINT<std::uint8_t>>
        AES128;

    typedef UNAES_128<
        std::uint8_t, std::uint8_t, std::uint8_t, BitwiseINT<std::uint8_t>>
        UNAES128;

    typedef AES_192<
        std::uint8_t, std::uint8_t, std::uint8_t, BitwiseINT<std::uint8_t>>
        AES192;

    typedef UNAES_192<
        std::uint8_t, std::uint8_t, std::uint8_t, BitwiseINT<std::uint8_t>>
        UNAES192;

    typedef AES_256<
        std::uint8_t, std::uint8_t, std::uint8_t, BitwiseINT<std::uint8_t>>
        AES256;

    typedef UNAES_256<
        std::uint8_t, std::uint8_t, std::uint8_t, BitwiseINT<std::uint8_t>>
        UNAES256;
} // namespace eval

} // namespace snarkfront

#endif
<commit_msg>Delete AdvancedEncryptionStd.hpp<commit_after><|endoftext|>


<commit_before>// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! [all code]

//! [bigtable includes] [START dependencies]
#include "google/cloud/bigtable/table.h"
#include "google/cloud/bigtable/table_admin.h"
//! [bigtable includes] [END dependencies]

int main(int argc, char* argv[]) try {
  if (argc != 4) {
    std::string const cmd = argv[0];
    auto last_slash = std::string(cmd).find_last_of('/');
    std::cerr << "Usage: " << cmd.substr(last_slash + 1)
              << " <project_id> <instance_id> <table_id>\n";
    return 1;
  }

  std::string const project_id = argv[1];
  std::string const instance_id = argv[2];
  std::string const table_id = argv[3];

  // Connect to the Cloud Bigtable Admin API.
  //! [connect admin] [START connecting_to_bigtable]
  google::cloud::bigtable::TableAdmin table_admin(
      google::cloud::bigtable::CreateDefaultAdminClient(
          project_id, google::cloud::bigtable::ClientOptions()),
      instance_id);
  //! [connect admin] [END connecting_to_bigtable]

  //! [create table] [START creating_a_table]
  // Define the desired schema for the Table.
  auto gc_rule = google::cloud::bigtable::GcRule::MaxNumVersions(1);
  google::cloud::bigtable::TableConfig schema({{"family", gc_rule}}, {});

  // Create a table.
  auto returned_schema = table_admin.CreateTable(table_id, schema);
  //! [create table] [END creating_a_table]

  // Create an object to access the Cloud Bigtable Data API.
  //! [connect data] [START connecting_to_bigtable]
  google::cloud::bigtable::Table table(
      google::cloud::bigtable::CreateDefaultDataClient(
          project_id, instance_id, google::cloud::bigtable::ClientOptions()),
      table_id);
  //! [connect data] [END connecting_to_bigtable]

  // Modify (and create if necessary) a row.
  //! [write rows] [START writing_rows]
  std::vector<std::string> greetings{"Hello World!", "Hello Cloud Bigtable!",
                                     "Hello C++!"};
  int i = 0;
  for (auto const& greeting : greetings) {
    // Each row has a unique row key.
    //
    // Note: This example uses sequential numeric IDs for simplicity, but
    // this can result in poor performance in a production application.
    // Since rows are stored in sorted order by key, sequential keys can
    // result in poor distribution of operations across nodes.
    //
    // For more information about how to design a Bigtable schema for the
    // best performance, see the documentation:
    //
    //     https://cloud.google.com/bigtable/docs/schema-design
    std::string row_key = "key-" + std::to_string(i);
    google::cloud::Status status =
        table.Apply(google::cloud::bigtable::SingleRowMutation(
            std::move(row_key),
            google::cloud::bigtable::SetCell("family", "c0", greeting)));

    if (!status.ok()) {
      throw std::runtime_error(status.message());
    }
    ++i;
  }
  //! [write rows] [END writing_rows]

  //! [create filter] [START creating_a_filter]
  auto filter =
      google::cloud::bigtable::Filter::ColumnRangeClosed("family", "c0", "c0");
  //! [create filter] [END creating_a_filter]

  // Read a single row.
  //! [read row] [START getting_a_row]
  auto result = table.ReadRow("key-0", filter);
  if (!result) {
    throw std::runtime_error(result.status().message());
  }
  if (!result->first) {
    std::cout << "Cannot find row 'key-0' in the table: " << table.table_name()
              << "\n";
    return 0;
  }
  auto const& cell = result->second.cells().front();
  std::cout << cell.family_name() << ":" << cell.column_qualifier() << "    @ "
            << cell.timestamp().count() << "us\n"
            << '"' << cell.value() << '"' << "\n";
  //! [read row] [END getting_a_row]

  // Read all rows.
  //! [scan all] [START scanning_all_rows]
  for (auto& row :
       table.ReadRows(google::cloud::bigtable::RowRange::InfiniteRange(),
                      google::cloud::bigtable::Filter::PassAllFilter())) {
    if (!row) {
      throw std::runtime_error(row.status().message());
    }
    std::cout << row->row_key() << ":\n";
    for (auto& cell : row->cells()) {
      std::cout << "\t" << cell.family_name() << ":" << cell.column_qualifier()
                << "    @ " << cell.timestamp().count() << "us\n"
                << "\t\"" << cell.value() << '"' << "\n";
    }
  }
  //! [scan all] [END scanning_all_rows]

  // Delete the table
  //! [delete table] [START deleting_a_table]
  google::cloud::Status status = table_admin.DeleteTable(table_id);
  if (!status.ok()) {
    throw std::runtime_error(status.message());
  }
  //! [delete table] [END deleting_a_table]

  return 0;
} catch (std::exception const& ex) {
  std::cerr << "Standard C++ exception raised: " << ex.what() << "\n";
  return 1;
}
//! [all code]
<commit_msg>Fix region tags for bigtable_hello_world. (#2437)<commit_after>// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! [all code]

//! [bigtable includes] [START bigtable_hw_imports]
#include "google/cloud/bigtable/table.h"
#include "google/cloud/bigtable/table_admin.h"
//! [bigtable includes] [END bigtable_hw_imports]

int main(int argc, char* argv[]) try {
  if (argc != 4) {
    std::string const cmd = argv[0];
    auto last_slash = std::string(cmd).find_last_of('/');
    std::cerr << "Usage: " << cmd.substr(last_slash + 1)
              << " <project_id> <instance_id> <table_id>\n";
    return 1;
  }

  std::string const project_id = argv[1];
  std::string const instance_id = argv[2];
  std::string const table_id = argv[3];

  // Connect to the Cloud Bigtable Admin API.
  //! [connect admin] [START bigtable_hw_connect]
  google::cloud::bigtable::TableAdmin table_admin(
      google::cloud::bigtable::CreateDefaultAdminClient(
          project_id, google::cloud::bigtable::ClientOptions()),
      instance_id);
  //! [connect admin] [END bigtable_hw_connect]

  //! [create table] [START bigtable_hw_create_table]
  // Define the desired schema for the Table.
  auto gc_rule = google::cloud::bigtable::GcRule::MaxNumVersions(1);
  google::cloud::bigtable::TableConfig schema({{"family", gc_rule}}, {});

  // Create a table.
  auto returned_schema = table_admin.CreateTable(table_id, schema);
  //! [create table]

  // Create an object to access the Cloud Bigtable Data API.
  //! [connect data]
  google::cloud::bigtable::Table table(
      google::cloud::bigtable::CreateDefaultDataClient(
          project_id, instance_id, google::cloud::bigtable::ClientOptions()),
      table_id);
  //! [connect data] [END bigtable_hw_create_table]

  // Modify (and create if necessary) a row.
  //! [write rows] [START bigtable_hw_write_rows]
  std::vector<std::string> greetings{"Hello World!", "Hello Cloud Bigtable!",
                                     "Hello C++!"};
  int i = 0;
  for (auto const& greeting : greetings) {
    // Each row has a unique row key.
    //
    // Note: This example uses sequential numeric IDs for simplicity, but
    // this can result in poor performance in a production application.
    // Since rows are stored in sorted order by key, sequential keys can
    // result in poor distribution of operations across nodes.
    //
    // For more information about how to design a Bigtable schema for the
    // best performance, see the documentation:
    //
    //     https://cloud.google.com/bigtable/docs/schema-design
    std::string row_key = "key-" + std::to_string(i);
    google::cloud::Status status =
        table.Apply(google::cloud::bigtable::SingleRowMutation(
            std::move(row_key),
            google::cloud::bigtable::SetCell("family", "c0", greeting)));

    if (!status.ok()) {
      throw std::runtime_error(status.message());
    }
    ++i;
  }
  //! [write rows] [END bigtable_hw_write_rows]

  //! [create filter] [START bigtable_hw_create_filter]
  auto filter =
      google::cloud::bigtable::Filter::ColumnRangeClosed("family", "c0", "c0");
  //! [create filter] [END bigtable_hw_create_filter]

  // Read a single row.
  //! [read row] [START bigtable_hw_get_with_filter]
  auto result = table.ReadRow("key-0", filter);
  if (!result) {
    throw std::runtime_error(result.status().message());
  }
  if (!result->first) {
    std::cout << "Cannot find row 'key-0' in the table: " << table.table_name()
              << "\n";
    return 0;
  }
  auto const& cell = result->second.cells().front();
  std::cout << cell.family_name() << ":" << cell.column_qualifier() << "    @ "
            << cell.timestamp().count() << "us\n"
            << '"' << cell.value() << '"' << "\n";
  //! [read row] [END bigtable_hw_get_with_filter]

  // Read all rows.
  //! [scan all] [START bigtable_hw_scan_with_filter]
  for (auto& row :
       table.ReadRows(google::cloud::bigtable::RowRange::InfiniteRange(),
                      google::cloud::bigtable::Filter::PassAllFilter())) {
    if (!row) {
      throw std::runtime_error(row.status().message());
    }
    std::cout << row->row_key() << ":\n";
    for (auto& cell : row->cells()) {
      std::cout << "\t" << cell.family_name() << ":" << cell.column_qualifier()
                << "    @ " << cell.timestamp().count() << "us\n"
                << "\t\"" << cell.value() << '"' << "\n";
    }
  }
  //! [scan all] [END bigtable_hw_scan_with_filter]

  // Delete the table
  //! [delete table]
  google::cloud::Status status = table_admin.DeleteTable(table_id);
  if (!status.ok()) {
    throw std::runtime_error(status.message());
  }
  //! [delete table]

  return 0;
} catch (std::exception const& ex) {
  std::cerr << "Standard C++ exception raised: " << ex.what() << "\n";
  return 1;
}
//! [all code]
<|endoftext|>


<commit_before>/*
 *  Copyright (c) 2014, Oculus VR, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 *
 */

#include "NativeFeatureIncludes.h"
#if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1

#include "PacketizedTCP.h"
#include "NativeTypes.h"
#include "BitStream.h"
#include "MessageIdentifiers.h"
#include "RakAlloca.h"

using namespace RakNet;

typedef uint32_t PTCPHeader;

STATIC_FACTORY_DEFINITIONS(PacketizedTCP,PacketizedTCP);

PacketizedTCP::PacketizedTCP()
{

}
PacketizedTCP::~PacketizedTCP()
{
	ClearAllConnections();
}

void PacketizedTCP::Stop(void)
{
	unsigned int i;
	TCPInterface::Stop();
	for (i=0; i < waitingPackets.Size(); i++)
		DeallocatePacket(waitingPackets[i]);
	ClearAllConnections();
}

void PacketizedTCP::Send( const char *data, unsigned length, const SystemAddress &systemAddress, bool broadcast )
{
	PTCPHeader dataLength;
	dataLength=length;
#ifndef __BITSTREAM_NATIVE_END
	if (RakNet::BitStream::DoEndianSwap())
		RakNet::BitStream::ReverseBytes((unsigned char*) &length,(unsigned char*) &dataLength,sizeof(dataLength));
#else
		dataLength=length;
#endif

	unsigned int lengthsArray[2];
	const char *dataArray[2];
	dataArray[0]=(char*) &dataLength;
	dataArray[1]=data;
	lengthsArray[0]=sizeof(dataLength);
	lengthsArray[1]=length;
	TCPInterface::SendList(dataArray,lengthsArray,2,systemAddress,broadcast);
}
bool PacketizedTCP::SendList( const char **data, const unsigned int *lengths, const int numParameters, const SystemAddress &systemAddress, bool broadcast )
{
	if (isStarted.GetValue()==0)
		return false;
	if (data==0)
		return false;
	if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS && broadcast==false)
		return false;
	PTCPHeader totalLengthOfUserData=0;
	int i;
	for (i=0; i < numParameters; i++)
	{
		if (lengths[i]>0)
			totalLengthOfUserData+=lengths[i];
	}
	if (totalLengthOfUserData==0)
		return false;

	PTCPHeader dataLength;
#ifndef __BITSTREAM_NATIVE_END
	if (RakNet::BitStream::DoEndianSwap())
		RakNet::BitStream::ReverseBytes((unsigned char*) &totalLengthOfUserData,(unsigned char*) &dataLength,sizeof(dataLength));
#else
	dataLength=totalLengthOfUserData;
#endif

	unsigned int lengthsArray[512];
	const char *dataArray[512];
	dataArray[0]=(char*) &dataLength;
	lengthsArray[0]=sizeof(dataLength);
	for (int i=0; i < 512 && i < numParameters; i++)
	{
		dataArray[i+1]=data[i];
		lengthsArray[i+1]=lengths[i];
	}
	return TCPInterface::SendList(dataArray,lengthsArray,numParameters+1,systemAddress,broadcast);
}
void PacketizedTCP::PushNotificationsToQueues(void)
{
	SystemAddress sa;
	sa = TCPInterface::HasNewIncomingConnection();
	if (sa!=UNASSIGNED_SYSTEM_ADDRESS)
	{
		_newIncomingConnections.Push(sa, _FILE_AND_LINE_ );
		AddToConnectionList(sa);
	}

	sa = TCPInterface::HasFailedConnectionAttempt();
	if (sa!=UNASSIGNED_SYSTEM_ADDRESS)
	{
		_failedConnectionAttempts.Push(sa, _FILE_AND_LINE_ );
	}

	sa = TCPInterface::HasLostConnection();
	if (sa!=UNASSIGNED_SYSTEM_ADDRESS)
	{
		_lostConnections.Push(sa, _FILE_AND_LINE_ );
		RemoveFromConnectionList(sa);
	}

	sa = TCPInterface::HasCompletedConnectionAttempt();
	if (sa!=UNASSIGNED_SYSTEM_ADDRESS)
	{
		_completedConnectionAttempts.Push(sa, _FILE_AND_LINE_ );
		AddToConnectionList(sa);
	}
}
Packet* PacketizedTCP::Receive( void )
{
	PushNotificationsToQueues();

	unsigned int i;
	for (i=0; i < messageHandlerList.Size(); i++)
		messageHandlerList[i]->Update();

	Packet *outgoingPacket=ReturnOutgoingPacket();
	if (outgoingPacket)
		return outgoingPacket;

	Packet *incomingPacket;
	incomingPacket = TCPInterface::ReceiveInt();
	unsigned int index;

	while (incomingPacket)
	{
		if (connections.Has(incomingPacket->systemAddress))
			index = connections.GetIndexAtKey(incomingPacket->systemAddress);
		else
			index=(unsigned int) -1;
		if ((unsigned int)index==(unsigned int)-1)
		{
			DeallocatePacket(incomingPacket);
			incomingPacket = TCPInterface::ReceiveInt();
			continue;
		}

		if (incomingPacket->deleteData==true)
		{
			// Came from network
			SystemAddress systemAddressFromPacket;
			if (index < connections.Size())
			{
				DataStructures::ByteQueue *bq = connections[index];
				// Buffer data
				bq->WriteBytes((const char*) incomingPacket->data,incomingPacket->length, _FILE_AND_LINE_);
				systemAddressFromPacket=incomingPacket->systemAddress;
				PTCPHeader dataLength;

				// Peek the header to see if a full message is waiting
				bq->ReadBytes((char*) &dataLength,sizeof(PTCPHeader),true);
				if (RakNet::BitStream::DoEndianSwap())
					RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &dataLength,sizeof(dataLength));
				// Header indicates packet length. If enough data is available, read out and return one packet
				if (bq->GetBytesWritten()>=dataLength+sizeof(PTCPHeader))
				{
					do
					{
						bq->IncrementReadOffset(sizeof(PTCPHeader));
						outgoingPacket = RakNet::OP_NEW<Packet>(_FILE_AND_LINE_);
						outgoingPacket->length=dataLength;
						outgoingPacket->bitSize=BYTES_TO_BITS(dataLength);
						outgoingPacket->guid=UNASSIGNED_RAKNET_GUID;
						outgoingPacket->systemAddress=systemAddressFromPacket;
						outgoingPacket->deleteData=false; // Did not come from the network
						outgoingPacket->data=(unsigned char*) rakMalloc_Ex(dataLength, _FILE_AND_LINE_);
						if (outgoingPacket->data==0)
						{
							notifyOutOfMemory(_FILE_AND_LINE_);
							RakNet::OP_DELETE(outgoingPacket,_FILE_AND_LINE_);
							return 0;
						}
						bq->ReadBytes((char*) outgoingPacket->data,dataLength,false);

						waitingPackets.Push(outgoingPacket, _FILE_AND_LINE_ );

						// Peek the header to see if a full message is waiting
						if (bq->ReadBytes((char*) &dataLength,sizeof(PTCPHeader),true))
						{
							if (RakNet::BitStream::DoEndianSwap())
								RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &dataLength,sizeof(dataLength));
						}
						else
							break;
					} while (bq->GetBytesWritten()>=dataLength+sizeof(PTCPHeader));
				}
				else
				{

					unsigned int oldWritten = bq->GetBytesWritten()-incomingPacket->length;
					unsigned int newWritten = bq->GetBytesWritten();

					// Return ID_DOWNLOAD_PROGRESS
					if (newWritten/65536!=oldWritten/65536)
					{
						outgoingPacket = RakNet::OP_NEW<Packet>(_FILE_AND_LINE_);
						outgoingPacket->length=sizeof(MessageID) +
							sizeof(unsigned int)*2 +
							sizeof(unsigned int) +
							65536;
						outgoingPacket->bitSize=BYTES_TO_BITS(incomingPacket->length);
						outgoingPacket->guid=UNASSIGNED_RAKNET_GUID;
						outgoingPacket->systemAddress=incomingPacket->systemAddress;
						outgoingPacket->deleteData=false;
						outgoingPacket->data=(unsigned char*) rakMalloc_Ex(outgoingPacket->length, _FILE_AND_LINE_);
						if (outgoingPacket->data==0)
						{
							notifyOutOfMemory(_FILE_AND_LINE_);
							RakNet::OP_DELETE(outgoingPacket,_FILE_AND_LINE_);
							return 0;
						}

						outgoingPacket->data[0]=(MessageID)ID_DOWNLOAD_PROGRESS;
						unsigned int totalParts=dataLength/65536;
						unsigned int partIndex=newWritten/65536;
						unsigned int oneChunkSize=65536;
						memcpy(outgoingPacket->data+sizeof(MessageID), &partIndex, sizeof(unsigned int));
						memcpy(outgoingPacket->data+sizeof(MessageID)+sizeof(unsigned int)*1, &totalParts, sizeof(unsigned int));
						memcpy(outgoingPacket->data+sizeof(MessageID)+sizeof(unsigned int)*2, &oneChunkSize, sizeof(unsigned int));
						bq->IncrementReadOffset(sizeof(PTCPHeader));
						bq->ReadBytes((char*) outgoingPacket->data+sizeof(MessageID)+sizeof(unsigned int)*3,oneChunkSize,true);
						bq->DecrementReadOffset(sizeof(PTCPHeader));

						waitingPackets.Push(outgoingPacket, _FILE_AND_LINE_ );
					}
				}

			}

			DeallocatePacket(incomingPacket);
			incomingPacket=0;
		}
		else
			waitingPackets.Push(incomingPacket, _FILE_AND_LINE_ );

		incomingPacket = TCPInterface::ReceiveInt();
	}

	return ReturnOutgoingPacket();
}
Packet *PacketizedTCP::ReturnOutgoingPacket(void)
{
	Packet *outgoingPacket=0;
	unsigned int i;
	while (outgoingPacket==0 && waitingPackets.IsEmpty()==false)
	{
		outgoingPacket=waitingPackets.Pop();
		PluginReceiveResult pluginResult;
		for (i=0; i < messageHandlerList.Size(); i++)
		{
			pluginResult=messageHandlerList[i]->OnReceive(outgoingPacket);
			if (pluginResult==RR_STOP_PROCESSING_AND_DEALLOCATE)
			{
				DeallocatePacket( outgoingPacket );
				outgoingPacket=0; // Will do the loop again and get another packet
				break; // break out of the enclosing for
			}
			else if (pluginResult==RR_STOP_PROCESSING)
			{
				outgoingPacket=0;
				break;
			}
		}
	}

	return outgoingPacket;
}
void PacketizedTCP::CloseConnection( SystemAddress systemAddress )
{
	RemoveFromConnectionList(systemAddress);
	TCPInterface::CloseConnection(systemAddress);
}
void PacketizedTCP::RemoveFromConnectionList(const SystemAddress &sa)
{
	if (sa==UNASSIGNED_SYSTEM_ADDRESS)
		return;
	if (connections.Has(sa))
	{
		unsigned int index = connections.GetIndexAtKey(sa);
		if (index!=(unsigned int)-1)
		{
			RakNet::OP_DELETE(connections[index],_FILE_AND_LINE_);
			connections.RemoveAtIndex(index);
		}
	}
}
void PacketizedTCP::AddToConnectionList(const SystemAddress &sa)
{
	if (sa==UNASSIGNED_SYSTEM_ADDRESS)
		return;
	connections.SetNew(sa, RakNet::OP_NEW<DataStructures::ByteQueue>(_FILE_AND_LINE_));
}
void PacketizedTCP::ClearAllConnections(void)
{
	unsigned int i;
	for (i=0; i < connections.Size(); i++)
		RakNet::OP_DELETE(connections[i],_FILE_AND_LINE_);
	connections.Clear();
}
SystemAddress PacketizedTCP::HasCompletedConnectionAttempt(void)
{
	PushNotificationsToQueues();

	if (_completedConnectionAttempts.IsEmpty()==false)
		return _completedConnectionAttempts.Pop();
	return UNASSIGNED_SYSTEM_ADDRESS;
}
SystemAddress PacketizedTCP::HasFailedConnectionAttempt(void)
{
	PushNotificationsToQueues();

	if (_failedConnectionAttempts.IsEmpty()==false)
		return _failedConnectionAttempts.Pop();
	return UNASSIGNED_SYSTEM_ADDRESS;
}
SystemAddress PacketizedTCP::HasNewIncomingConnection(void)
{
	PushNotificationsToQueues();

	if (_newIncomingConnections.IsEmpty()==false)
		return _newIncomingConnections.Pop();
	return UNASSIGNED_SYSTEM_ADDRESS;
}
SystemAddress PacketizedTCP::HasLostConnection(void)
{
	PushNotificationsToQueues();

	if (_lostConnections.IsEmpty()==false)
		return _lostConnections.Pop();
	return UNASSIGNED_SYSTEM_ADDRESS;
}

#endif // _RAKNET_SUPPORT_*
<commit_msg>fixing buffer overflow: accessing arrays at index 512<commit_after>/*
 *  Copyright (c) 2014, Oculus VR, Inc.
 *  All rights reserved.
 *
 *  This source code is licensed under the BSD-style license found in the
 *  LICENSE file in the root directory of this source tree. An additional grant
 *  of patent rights can be found in the PATENTS file in the same directory.
 *
 */

#include "NativeFeatureIncludes.h"
#if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1

#include "PacketizedTCP.h"
#include "NativeTypes.h"
#include "BitStream.h"
#include "MessageIdentifiers.h"
#include "RakAlloca.h"

using namespace RakNet;

typedef uint32_t PTCPHeader;

STATIC_FACTORY_DEFINITIONS(PacketizedTCP,PacketizedTCP);

PacketizedTCP::PacketizedTCP()
{

}
PacketizedTCP::~PacketizedTCP()
{
	ClearAllConnections();
}

void PacketizedTCP::Stop(void)
{
	unsigned int i;
	TCPInterface::Stop();
	for (i=0; i < waitingPackets.Size(); i++)
		DeallocatePacket(waitingPackets[i]);
	ClearAllConnections();
}

void PacketizedTCP::Send( const char *data, unsigned length, const SystemAddress &systemAddress, bool broadcast )
{
	PTCPHeader dataLength;
	dataLength=length;
#ifndef __BITSTREAM_NATIVE_END
	if (RakNet::BitStream::DoEndianSwap())
		RakNet::BitStream::ReverseBytes((unsigned char*) &length,(unsigned char*) &dataLength,sizeof(dataLength));
#else
		dataLength=length;
#endif

	unsigned int lengthsArray[2];
	const char *dataArray[2];
	dataArray[0]=(char*) &dataLength;
	dataArray[1]=data;
	lengthsArray[0]=sizeof(dataLength);
	lengthsArray[1]=length;
	TCPInterface::SendList(dataArray,lengthsArray,2,systemAddress,broadcast);
}
bool PacketizedTCP::SendList( const char **data, const unsigned int *lengths, const int numParameters, const SystemAddress &systemAddress, bool broadcast )
{
	if (isStarted.GetValue()==0)
		return false;
	if (data==0)
		return false;
	if (systemAddress==UNASSIGNED_SYSTEM_ADDRESS && broadcast==false)
		return false;
	PTCPHeader totalLengthOfUserData=0;
	int i;
	for (i=0; i < numParameters; i++)
	{
		if (lengths[i]>0)
			totalLengthOfUserData+=lengths[i];
	}
	if (totalLengthOfUserData==0)
		return false;

	PTCPHeader dataLength;
#ifndef __BITSTREAM_NATIVE_END
	if (RakNet::BitStream::DoEndianSwap())
		RakNet::BitStream::ReverseBytes((unsigned char*) &totalLengthOfUserData,(unsigned char*) &dataLength,sizeof(dataLength));
#else
	dataLength=totalLengthOfUserData;
#endif

	unsigned int lengthsArray[513];
	const char *dataArray[513];
	dataArray[0]=(char*) &dataLength;
	lengthsArray[0]=sizeof(dataLength);
	for (int i=0; i < 512 && i < numParameters; i++)
	{
		dataArray[i+1]=data[i];
		lengthsArray[i+1]=lengths[i];
	}
	return TCPInterface::SendList(dataArray,lengthsArray,numParameters+1,systemAddress,broadcast);
}
void PacketizedTCP::PushNotificationsToQueues(void)
{
	SystemAddress sa;
	sa = TCPInterface::HasNewIncomingConnection();
	if (sa!=UNASSIGNED_SYSTEM_ADDRESS)
	{
		_newIncomingConnections.Push(sa, _FILE_AND_LINE_ );
		AddToConnectionList(sa);
	}

	sa = TCPInterface::HasFailedConnectionAttempt();
	if (sa!=UNASSIGNED_SYSTEM_ADDRESS)
	{
		_failedConnectionAttempts.Push(sa, _FILE_AND_LINE_ );
	}

	sa = TCPInterface::HasLostConnection();
	if (sa!=UNASSIGNED_SYSTEM_ADDRESS)
	{
		_lostConnections.Push(sa, _FILE_AND_LINE_ );
		RemoveFromConnectionList(sa);
	}

	sa = TCPInterface::HasCompletedConnectionAttempt();
	if (sa!=UNASSIGNED_SYSTEM_ADDRESS)
	{
		_completedConnectionAttempts.Push(sa, _FILE_AND_LINE_ );
		AddToConnectionList(sa);
	}
}
Packet* PacketizedTCP::Receive( void )
{
	PushNotificationsToQueues();

	unsigned int i;
	for (i=0; i < messageHandlerList.Size(); i++)
		messageHandlerList[i]->Update();

	Packet *outgoingPacket=ReturnOutgoingPacket();
	if (outgoingPacket)
		return outgoingPacket;

	Packet *incomingPacket;
	incomingPacket = TCPInterface::ReceiveInt();
	unsigned int index;

	while (incomingPacket)
	{
		if (connections.Has(incomingPacket->systemAddress))
			index = connections.GetIndexAtKey(incomingPacket->systemAddress);
		else
			index=(unsigned int) -1;
		if ((unsigned int)index==(unsigned int)-1)
		{
			DeallocatePacket(incomingPacket);
			incomingPacket = TCPInterface::ReceiveInt();
			continue;
		}

		if (incomingPacket->deleteData==true)
		{
			// Came from network
			SystemAddress systemAddressFromPacket;
			if (index < connections.Size())
			{
				DataStructures::ByteQueue *bq = connections[index];
				// Buffer data
				bq->WriteBytes((const char*) incomingPacket->data,incomingPacket->length, _FILE_AND_LINE_);
				systemAddressFromPacket=incomingPacket->systemAddress;
				PTCPHeader dataLength;

				// Peek the header to see if a full message is waiting
				bq->ReadBytes((char*) &dataLength,sizeof(PTCPHeader),true);
				if (RakNet::BitStream::DoEndianSwap())
					RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &dataLength,sizeof(dataLength));
				// Header indicates packet length. If enough data is available, read out and return one packet
				if (bq->GetBytesWritten()>=dataLength+sizeof(PTCPHeader))
				{
					do
					{
						bq->IncrementReadOffset(sizeof(PTCPHeader));
						outgoingPacket = RakNet::OP_NEW<Packet>(_FILE_AND_LINE_);
						outgoingPacket->length=dataLength;
						outgoingPacket->bitSize=BYTES_TO_BITS(dataLength);
						outgoingPacket->guid=UNASSIGNED_RAKNET_GUID;
						outgoingPacket->systemAddress=systemAddressFromPacket;
						outgoingPacket->deleteData=false; // Did not come from the network
						outgoingPacket->data=(unsigned char*) rakMalloc_Ex(dataLength, _FILE_AND_LINE_);
						if (outgoingPacket->data==0)
						{
							notifyOutOfMemory(_FILE_AND_LINE_);
							RakNet::OP_DELETE(outgoingPacket,_FILE_AND_LINE_);
							return 0;
						}
						bq->ReadBytes((char*) outgoingPacket->data,dataLength,false);

						waitingPackets.Push(outgoingPacket, _FILE_AND_LINE_ );

						// Peek the header to see if a full message is waiting
						if (bq->ReadBytes((char*) &dataLength,sizeof(PTCPHeader),true))
						{
							if (RakNet::BitStream::DoEndianSwap())
								RakNet::BitStream::ReverseBytesInPlace((unsigned char*) &dataLength,sizeof(dataLength));
						}
						else
							break;
					} while (bq->GetBytesWritten()>=dataLength+sizeof(PTCPHeader));
				}
				else
				{

					unsigned int oldWritten = bq->GetBytesWritten()-incomingPacket->length;
					unsigned int newWritten = bq->GetBytesWritten();

					// Return ID_DOWNLOAD_PROGRESS
					if (newWritten/65536!=oldWritten/65536)
					{
						outgoingPacket = RakNet::OP_NEW<Packet>(_FILE_AND_LINE_);
						outgoingPacket->length=sizeof(MessageID) +
							sizeof(unsigned int)*2 +
							sizeof(unsigned int) +
							65536;
						outgoingPacket->bitSize=BYTES_TO_BITS(incomingPacket->length);
						outgoingPacket->guid=UNASSIGNED_RAKNET_GUID;
						outgoingPacket->systemAddress=incomingPacket->systemAddress;
						outgoingPacket->deleteData=false;
						outgoingPacket->data=(unsigned char*) rakMalloc_Ex(outgoingPacket->length, _FILE_AND_LINE_);
						if (outgoingPacket->data==0)
						{
							notifyOutOfMemory(_FILE_AND_LINE_);
							RakNet::OP_DELETE(outgoingPacket,_FILE_AND_LINE_);
							return 0;
						}

						outgoingPacket->data[0]=(MessageID)ID_DOWNLOAD_PROGRESS;
						unsigned int totalParts=dataLength/65536;
						unsigned int partIndex=newWritten/65536;
						unsigned int oneChunkSize=65536;
						memcpy(outgoingPacket->data+sizeof(MessageID), &partIndex, sizeof(unsigned int));
						memcpy(outgoingPacket->data+sizeof(MessageID)+sizeof(unsigned int)*1, &totalParts, sizeof(unsigned int));
						memcpy(outgoingPacket->data+sizeof(MessageID)+sizeof(unsigned int)*2, &oneChunkSize, sizeof(unsigned int));
						bq->IncrementReadOffset(sizeof(PTCPHeader));
						bq->ReadBytes((char*) outgoingPacket->data+sizeof(MessageID)+sizeof(unsigned int)*3,oneChunkSize,true);
						bq->DecrementReadOffset(sizeof(PTCPHeader));

						waitingPackets.Push(outgoingPacket, _FILE_AND_LINE_ );
					}
				}

			}

			DeallocatePacket(incomingPacket);
			incomingPacket=0;
		}
		else
			waitingPackets.Push(incomingPacket, _FILE_AND_LINE_ );

		incomingPacket = TCPInterface::ReceiveInt();
	}

	return ReturnOutgoingPacket();
}
Packet *PacketizedTCP::ReturnOutgoingPacket(void)
{
	Packet *outgoingPacket=0;
	unsigned int i;
	while (outgoingPacket==0 && waitingPackets.IsEmpty()==false)
	{
		outgoingPacket=waitingPackets.Pop();
		PluginReceiveResult pluginResult;
		for (i=0; i < messageHandlerList.Size(); i++)
		{
			pluginResult=messageHandlerList[i]->OnReceive(outgoingPacket);
			if (pluginResult==RR_STOP_PROCESSING_AND_DEALLOCATE)
			{
				DeallocatePacket( outgoingPacket );
				outgoingPacket=0; // Will do the loop again and get another packet
				break; // break out of the enclosing for
			}
			else if (pluginResult==RR_STOP_PROCESSING)
			{
				outgoingPacket=0;
				break;
			}
		}
	}

	return outgoingPacket;
}
void PacketizedTCP::CloseConnection( SystemAddress systemAddress )
{
	RemoveFromConnectionList(systemAddress);
	TCPInterface::CloseConnection(systemAddress);
}
void PacketizedTCP::RemoveFromConnectionList(const SystemAddress &sa)
{
	if (sa==UNASSIGNED_SYSTEM_ADDRESS)
		return;
	if (connections.Has(sa))
	{
		unsigned int index = connections.GetIndexAtKey(sa);
		if (index!=(unsigned int)-1)
		{
			RakNet::OP_DELETE(connections[index],_FILE_AND_LINE_);
			connections.RemoveAtIndex(index);
		}
	}
}
void PacketizedTCP::AddToConnectionList(const SystemAddress &sa)
{
	if (sa==UNASSIGNED_SYSTEM_ADDRESS)
		return;
	connections.SetNew(sa, RakNet::OP_NEW<DataStructures::ByteQueue>(_FILE_AND_LINE_));
}
void PacketizedTCP::ClearAllConnections(void)
{
	unsigned int i;
	for (i=0; i < connections.Size(); i++)
		RakNet::OP_DELETE(connections[i],_FILE_AND_LINE_);
	connections.Clear();
}
SystemAddress PacketizedTCP::HasCompletedConnectionAttempt(void)
{
	PushNotificationsToQueues();

	if (_completedConnectionAttempts.IsEmpty()==false)
		return _completedConnectionAttempts.Pop();
	return UNASSIGNED_SYSTEM_ADDRESS;
}
SystemAddress PacketizedTCP::HasFailedConnectionAttempt(void)
{
	PushNotificationsToQueues();

	if (_failedConnectionAttempts.IsEmpty()==false)
		return _failedConnectionAttempts.Pop();
	return UNASSIGNED_SYSTEM_ADDRESS;
}
SystemAddress PacketizedTCP::HasNewIncomingConnection(void)
{
	PushNotificationsToQueues();

	if (_newIncomingConnections.IsEmpty()==false)
		return _newIncomingConnections.Pop();
	return UNASSIGNED_SYSTEM_ADDRESS;
}
SystemAddress PacketizedTCP::HasLostConnection(void)
{
	PushNotificationsToQueues();

	if (_lostConnections.IsEmpty()==false)
		return _lostConnections.Pop();
	return UNASSIGNED_SYSTEM_ADDRESS;
}

#endif // _RAKNET_SUPPORT_*
<|endoftext|>


<commit_before><commit_msg>added extra template parameters<commit_after><|endoftext|>


<commit_before><commit_msg>Simple fix for out-of-bounds parameter issues.<commit_after><|endoftext|>