diff --git a/.travis.yml b/.travis.yml index 06d72a03..c5f16fa9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,75 +1,206 @@ language: cpp -cache: - - apt -before_install: - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:h-rayflood/llvm-upper; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update -qq; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update; fi +sudo: false + install: - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -qq gcc-4.8-multilib g++-4.8-multilib; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install --allow-unauthenticated -qq clang-3.6; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 90; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 90; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y lib32gcc1; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y libc6-i386; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y lib32z1-dev; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y lib32stdc++6; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y bzip2; fi - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -y libc6-dbg; fi - - wget https://github.com/google/googletest/archive/release-1.7.0.zip -O googletest-release-1.7.0.zip - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then wget http://valgrind.org/downloads/valgrind-3.12.0.tar.bz2 && tar xjf valgrind-3.12.0.tar.bz2 && cd valgrind-3.12.0 && ./configure && make && sudo make install && cd ..; fi - - - if [ "$TRAVIS_OS_NAME" == "osx" ]; then brew unlink boost; fi - - if [ "$BOOST" == "boost" ]; then wget http://sourceforge.net/projects/boost/files/boost/1.62.0/boost_1_62_0.zip && unzip -q boost_1_62_0.zip && cd boost_1_62_0 && ./bootstrap.sh && ./b2 --with-timer --with-chrono address-model="$ARCH" > /dev/null && sudo ./b2 --with-timer --with-chrono address-model="$ARCH" install > /dev/null && cd ..; fi - - - if [ "$TRAVIS_OS_NAME" == "linux" ]; then unzip -q googletest-release-1.7.0.zip && cd googletest-release-1.7.0 && sudo cp -r include/gtest /usr/local/include && g++ src/gtest-all.cc -I. -Iinclude -c && g++ src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mv *.a /usr/local/lib && g++ -m32 src/gtest-all.cc -I. -Iinclude -c && g++ -m32 src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mkdir /usr/local/lib32 && sudo mv *.a /usr/local/lib32 && cd ..; fi - - if [ "$TRAVIS_OS_NAME" == "osx" ]; then unzip -q googletest-release-1.7.0.zip && cd googletest-release-1.7.0 && sudo cp -r include/gtest /usr/local/include && clang++ src/gtest-all.cc -I. -Iinclude -c && g++ src/gtest_main.cc -I. -Iinclude -c && ar -rv libgtest.a gtest-all.o && ar -rv libgtest_main.a gtest_main.o && sudo mv *.a /usr/local/lib && cd ..; fi - -before_script: - - export PATH=/usr/local/bin:$PATH && rm -rf install + - export BASE=`pwd` + - mkdir ${BASE}/usr + - if [ "$TRAVIS_OS_NAME" == "linux" ]; then wget --no-check-certificate https://cmake.org/files/v3.7/cmake-3.7.1-Linux-x86_64.sh -O cmake-3.7.1-Linux-x86_64.sh; fi + - if [ "$TRAVIS_OS_NAME" == "linux" ]; then chmod a+x cmake-3.7.1-Linux-x86_64.sh; fi + - if [ "$TRAVIS_OS_NAME" == "linux" ]; then ./cmake-3.7.1-Linux-x86_64.sh --prefix=${BASE}/usr --skip-license; fi + - if [ "$TRAVIS_OS_NAME" == "linux" ]; then export PATH="${BASE}/usr/bin:$PATH"; fi + - if [ "$TRAVIS_OS_NAME" == "linux" ]; then export LD_LIBRARY_PATH="${BASE}/usr/lib:$LD_LIBRARY_PATH"; fi + - if [ "$CXX" = "g++" ]; then export CXX="g++-6" CC="gcc-6"; fi + - if [ "$CXX" = "clang++" ] && [ "$TRAVIS_OS_NAME" == "linux" ]; then export CXX="clang++-3.7" CC="clang-3.7"; fi +#gtest + - wget https://github.com/google/googletest/archive/release-1.7.0.zip -O googletest-release-1.7.0.zip + - unzip -q googletest-release-1.7.0.zip + - cd googletest-release-1.7.0 + - $CXX -m${ARCH} src/gtest-all.cc -I. -Iinclude -c + - $CXX -m${ARCH} src/gtest_main.cc -I. -Iinclude -c + - ar -rv libgtest.a gtest-all.o + - ar -rv libgtest_main.a gtest_main.o + - mkdir -p ${BASE}/usr/include + - cp -r include/gtest ${BASE}/usr/include + - mkdir -p ${BASE}/usr/lib + - mv *.a ${BASE}/usr/lib + - cd .. +# valgrind + - if [ "$TRAVIS_OS_NAME" == "linux" ]; then wget http://valgrind.org/downloads/valgrind-3.12.0.tar.bz2 && tar xjf valgrind-3.12.0.tar.bz2 && cd valgrind-3.12.0 && ./configure --prefix=${BASE}/usr > /dev/null && make -j3 > /dev/null && make install > /dev/null && cd ..; fi +# boost + - if [ "$BOOST" == "ON" ]; then wget http://sourceforge.net/projects/boost/files/boost/1.63.0/boost_1_63_0.zip && unzip -q boost_1_63_0.zip && cd boost_1_63_0 && ./bootstrap.sh && ./b2 -j3 --prefix=${BASE}/usr --with-timer --with-chrono address-model=${ARCH} install > /dev/null && cd ..; fi matrix: include: - os: osx compiler: clang - env: ACTION="ci/build_cmake.sh" VERSION="cpp03" ARCH="64" LIBPATH="/usr/local/lib" BOOST="boost" BOOST_INC="/usr/local/boost" CHAR_SIGN="unsigned" API_VERSION="1" + env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="1" - os: osx compiler: clang - env: ACTION="ci/build_cmake.sh" VERSION="cpp11" ARCH="64" LIBPATH="/usr/local/lib" BOOST="boost" BOOST_INC="/usr/local/boost" CHAR_SIGN="signed" API_VERSION="2" + env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2" X3_PARSE="ON" - os: osx compiler: clang - env: ACTION="ci/build_cmake.sh" VERSION="cpp11" ARCH="64" LIBPATH="/usr/local/lib" BOOST="" BOOST_INC="" CHAR_SIGN="signed" API_VERSION="2" + env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2" - os: osx compiler: clang - env: ACTION="ci/build_cmake.sh" VERSION="cpp03" ARCH="64" LIBPATH="/usr/local/lib" BOOST="" BOOST_INC="" CHAR_SIGN="unsigned" API_VERSION="2" + env: ACTION="ci/build_cmake.sh" ARCH="64" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="2" - os: linux compiler: clang - env: ACTION="ci/build_cmake.sh" VERSION="cpp03" ARCH="64" LIBPATH="/usr/local/lib" BOOST="boost" BOOST_INC="/usr/local/boost" CHAR_SIGN="unsigned" API_VERSION="1" + env: ACTION="ci/build_cmake.sh" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="1" + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.7 + - llvm-toolchain-precise + packages: + - g++-multilib + - gcc-multilib + - gcc-5-multilib + - g++-5-multilib + - bzip2 + - clang-3.7 + - libc6-dbg - os: linux compiler: clang - env: ACTION="ci/build_cmake.sh" VERSION="cpp03" ARCH="32" LIBPATH="/usr/local/lib32" BOOST="" BOOST_INC="" CHAR_SIGN="signed" API_VERSION="2" + env: ACTION="ci/build_cmake.sh" ARCH="32" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2" + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.7 + - llvm-toolchain-precise + packages: + - g++-multilib + - gcc-multilib + - gcc-5-multilib + - g++-5-multilib + - lib32stdc++6-6-dbg + - lib32gcc1 + - libc6-i386 + - lib32z1-dev + - bzip2 + - clang-3.7 + - libc6-dbg - os: linux compiler: clang - env: ACTION="ci/build_cmake.sh" VERSION="cpp11" ARCH="64" LIBPATH="/usr/local/lib" BOOST="boost" BOOST_INC="/usr/local/boost" CHAR_SIGN="signed" API_VERSION="2" + env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" BOOST="ON" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2" X3_PARSE="ON" + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.7 + - llvm-toolchain-precise + packages: + - g++-multilib + - gcc-multilib + - gcc-5-multilib + - g++-5-multilib + - bzip2 + - clang-3.7 + - libc6-dbg - os: linux compiler: clang - env: ACTION="ci/build_cmake.sh" VERSION="cpp11" ARCH="32" LIBPATH="/usr/local/lib32" BOOST="" BOOST_INC="" SHARED="OFF" CHAR_SIGN="unsigned" API_VERSION="2" + env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="32" SHARED="OFF" CHAR_SIGN="unsigned" API_VERSION="2" + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-precise-3.7 + - llvm-toolchain-precise + packages: + - g++-multilib + - gcc-multilib + - gcc-5-multilib + - g++-5-multilib + - lib32stdc++6-6-dbg + - lib32gcc1 + - libc6-i386 + - lib32z1-dev + - bzip2 + - clang-3.7 + - libc6-dbg - os: linux compiler: gcc - env: ACTION="ci/build_cmake.sh" VERSION="cpp11" ARCH="64" LIBPATH="/usr/local/lib" BOOST="" BOOST_INC="" CHAR_SIGN="signed" API_VERSION="2" + env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="64" SHARED="ON" CHAR_SIGN="signed" API_VERSION="2" + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-multilib + - gcc-multilib + - gcc-6-multilib + - g++-6-multilib + - bzip2 + - libc6-dbg - os: linux compiler: gcc - env: ACTION="ci/build_cmake.sh" VERSION="cpp11" ARCH="32" LIBPATH="/usr/local/lib32" BOOST="boost" BOOST_INC="/usr/local/boost" CHAR_SIGN="unsigned" API_VERSION="2" + env: ACTION="ci/build_cmake.sh" CXX11="ON" ARCH="32" BOOST="ON" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="2" X3_PARSE="ON" + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-multilib + - gcc-multilib + - gcc-6-multilib + - g++-6-multilib + - lib32stdc++6-6-dbg + - lib32gcc1 + - libc6-i386 + - lib32z1-dev + - bzip2 + - libc6-dbg - os: linux compiler: gcc - env: ACTION="ci/build_cmake.sh" VERSION="cpp03" ARCH="64" LIBPATH="/usr/local/lib" BOOST="" BOOST_INC="" CHAR_SIGN="unsigned" API_VERSION="2" + env: ACTION="ci/build_cmake.sh" ARCH="64" SHARED="ON" CHAR_SIGN="unsigned" API_VERSION="2" + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-multilib + - gcc-multilib + - gcc-6-multilib + - g++-6-multilib + - bzip2 + - libc6-dbg - os: linux compiler: gcc - env: ACTION="ci/build_cmake.sh" VERSION="cpp03" ARCH="32" LIBPATH="/usr/local/lib32" BOOST="boost" BOOST_INC="/usr/local/boost" SHARED="OFF" CHAR_SIGN="signed" API_VERSION="1" + env: ACTION="ci/build_cmake.sh" ARCH="32" BOOST="ON" SHARED="OFF" CHAR_SIGN="signed" API_VERSION="1" + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-multilib + - gcc-multilib + - gcc-6-multilib + - g++-6-multilib + - lib32stdc++6-6-dbg + - lib32gcc1 + - libc6-i386 + - lib32z1-dev + - bzip2 + - libc6-dbg - os: linux compiler: gcc - env: ACTION="ci/build_cmake.sh" VERSION="cpp03" ARCH="32" LIBPATH="/usr/local/lib32" BOOST="boost" BOOST_INC="/usr/local/boost" SHARED="OFF" CHAR_SIGN="signed" API_VERSION="2" + env: ACTION="ci/build_cmake.sh" ARCH="32" BOOST="ON" SHARED="OFF" CHAR_SIGN="signed" API_VERSION="2" + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-multilib + - gcc-multilib + - gcc-6-multilib + - g++-6-multilib + - lib32stdc++6-6-dbg + - lib32gcc1 + - libc6-i386 + - lib32z1-dev + - bzip2 + - libc6-dbg script: - - git clean -xdf && CMAKE_LIBRARY_PATH=${LIBPATH} ${ACTION} ${VERSION} ${ARCH} ${BOOST} ${BOOST_INC} ${CHAR_SIGN} + - CMAKE_CXX_COMPILER="${CXX}" CMAKE_C_COMPILER="${CC}" CMAKE_LIBRARY_PATH="${BASE}/usr/lib:${BASE}/build" GTEST_ROOT="${BASE}/usr" BOOST_ROOT="${BASE}/usr" ${ACTION} diff --git a/CMakeLists.txt b/CMakeLists.txt index d239cb5e..5c4dd5b4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,27 +38,40 @@ IF (APPLE) ENDIF () ENDIF () -IF (MSGPACK_CXX11) +IF (MSGPACK_USE_X3_PARSE) IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - SET (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}") + SET (CMAKE_CXX_FLAGS "-DMSGPACK_USE_X3_PARSE -std=c++14 ${CMAKE_CXX_FLAGS}") ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - SET (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}") + SET (CMAKE_CXX_FLAGS "-DMSGPACK_USE_X3_PARSE -std=c++14 ${CMAKE_CXX_FLAGS}") ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + SET (CMAKE_CXX_FLAGS "-DMSGPACK_USE_X3_PARSE ${CMAKE_CXX_FLAGS}") IF (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19) - MESSAGE ( FATAL_ERROR "MSVC doesn't support C++11.") + MESSAGE ( FATAL_ERROR "MSVC doesn't support C++14.") ENDIF () ENDIF () ELSE () - IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - IF ((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER ${GNUCXX_STD_SUPPORT_VERSION}) OR - (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL ${GNUCXX_STD_SUPPORT_VERSION})) - SET (CMAKE_CXX_FLAGS "-std=c++98 ${CMAKE_CXX_FLAGS}") + IF (MSGPACK_CXX11) + IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + SET (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}") + ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + SET (CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}") + ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + IF (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19) + MESSAGE ( FATAL_ERROR "MSVC doesn't support C++11.") + ENDIF () ENDIF () - ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - SET (CMAKE_CXX_FLAGS "-std=c++98 ${CMAKE_CXX_FLAGS}") - ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") - IF (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 18) - SET (CMAKE_CXX_FLAGS "-DMSGPACK_USE_CPP03 ${CMAKE_CXX_FLAGS}") + ELSE () + IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + IF ((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER ${GNUCXX_STD_SUPPORT_VERSION}) OR + (CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL ${GNUCXX_STD_SUPPORT_VERSION})) + SET (CMAKE_CXX_FLAGS "-std=c++98 ${CMAKE_CXX_FLAGS}") + ENDIF () + ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + SET (CMAKE_CXX_FLAGS "-std=c++98 ${CMAKE_CXX_FLAGS}") + ELSEIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + IF (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 18) + SET (CMAKE_CXX_FLAGS "-DMSGPACK_USE_CPP03 ${CMAKE_CXX_FLAGS}") + ENDIF () ENDIF () ENDIF () ENDIF () @@ -75,15 +88,25 @@ IF (MSGPACK_32BIT) ENDIF () ENDIF () +OPTION (MSGPACK_BUILD_EXAMPLES "Build msgpack examples." ON) + IF (MSGPACK_BOOST) SET (CMAKE_CXX_FLAGS "-DMSGPACK_USE_BOOST ${CMAKE_CXX_FLAGS}") - SET (Boost_USE_STATIC_LIBS ON) # only find static libs - SET (Boost_USE_MULTITHREADED ON) - SET (Boost_USE_STATIC_RUNTIME OFF) - FIND_PACKAGE (Boost COMPONENTS chrono timer system) - INCLUDE_DIRECTORIES ( - ${MSGPACK_BOOST_DIR} - ) +ENDIF () + +SET (Boost_USE_STATIC_LIBS ON) # only find static libs +SET (Boost_USE_MULTITHREADED ON) +SET (Boost_USE_STATIC_RUNTIME OFF) +FIND_PACKAGE (Boost COMPONENTS chrono context timer system) +IF (Boost_INCLUDE_DIRS) + INCLUDE_DIRECTORIES ( + ${Boost_INCLUDE_DIRS} + ) +ENDIF () +IF (MSGPACK_BOOST_DIR) + INCLUDE_DIRECTORIES ( + ${MSGPACK_BOOST_DIR} + ) ENDIF () IF (MSGPACK_CHAR_SIGN) @@ -123,7 +146,6 @@ FIND_PACKAGE (Threads) IF (GTEST_FOUND AND ZLIB_FOUND AND THREADS_FOUND) OPTION (MSGPACK_BUILD_TESTS "Build msgpack tests." ON) ENDIF () -OPTION (MSGPACK_BUILD_EXAMPLES "Build msgpack examples." ON) OPTION (MSGPACK_ENABLE_CXX "Enable C++ interface." ON) OPTION (MSGPACK_ENABLE_SHARED "Build shared libaries in addition to static libraries." ON) diff --git a/Files.cmake b/Files.cmake index dbfbdbe2..bdd316db 100644 --- a/Files.cmake +++ b/Files.cmake @@ -226,6 +226,7 @@ IF (MSGPACK_ENABLE_CXX) include/msgpack/object_fwd_decl.hpp include/msgpack/pack.hpp include/msgpack/pack_decl.hpp + include/msgpack/parse_return.hpp include/msgpack/preprocessor.hpp include/msgpack/preprocessor/arithmetic.hpp include/msgpack/preprocessor/arithmetic/add.hpp @@ -500,6 +501,7 @@ IF (MSGPACK_ENABLE_CXX) include/msgpack/type.hpp include/msgpack/unpack.hpp include/msgpack/unpack_decl.hpp + include/msgpack/unpack_exception.hpp include/msgpack/v1/adaptor/adaptor_base.hpp include/msgpack/v1/adaptor/adaptor_base_decl.hpp include/msgpack/v1/adaptor/array_ref.hpp @@ -586,11 +588,13 @@ IF (MSGPACK_ENABLE_CXX) include/msgpack/v1/object_fwd_decl.hpp include/msgpack/v1/pack.hpp include/msgpack/v1/pack_decl.hpp + include/msgpack/v1/parse_return.hpp include/msgpack/v1/preprocessor.hpp include/msgpack/v1/sbuffer.hpp include/msgpack/v1/sbuffer_decl.hpp include/msgpack/v1/unpack.hpp include/msgpack/v1/unpack_decl.hpp + include/msgpack/v1/unpack_exception.hpp include/msgpack/v1/version.hpp include/msgpack/v1/versioning.hpp include/msgpack/v1/vrefbuffer.hpp @@ -621,20 +625,26 @@ IF (MSGPACK_ENABLE_CXX) include/msgpack/v2/adaptor/size_equal_only_decl.hpp include/msgpack/v2/adaptor/v4raw_decl.hpp include/msgpack/v2/cpp_config_decl.hpp + include/msgpack/v2/create_object_visitor.hpp include/msgpack/v2/detail/cpp03_zone_decl.hpp include/msgpack/v2/detail/cpp11_zone_decl.hpp include/msgpack/v2/fbuffer_decl.hpp include/msgpack/v2/iterator_decl.hpp include/msgpack/v2/meta_decl.hpp + include/msgpack/v2/null_visitor.hpp include/msgpack/v2/object.hpp include/msgpack/v2/object_decl.hpp include/msgpack/v2/object_fwd.hpp include/msgpack/v2/object_fwd_decl.hpp include/msgpack/v2/pack_decl.hpp + include/msgpack/v2/parse.hpp + include/msgpack/v2/parse_return.hpp include/msgpack/v2/sbuffer_decl.hpp include/msgpack/v2/unpack.hpp include/msgpack/v2/unpack_decl.hpp include/msgpack/v2/vrefbuffer_decl.hpp + include/msgpack/v2/x3_parse.hpp + include/msgpack/v2/x3_unpack.hpp include/msgpack/v2/zbuffer_decl.hpp include/msgpack/v2/zone_decl.hpp include/msgpack/version.hpp diff --git a/appveyor.yml b/appveyor.yml index 477b6c7e..e18dde8d 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -33,10 +33,10 @@ build_script: - cmake --build . --config Release - cd .. - cd .. -- appveyor DownloadFile http://zlib.net/zlib-1.2.9.tar.gz -FileName zlib-1.2.9.tar.gz -- 7z x zlib-1.2.9.tar.gz > NUL -- 7z x zlib-1.2.9.tar > NUL -- cd zlib-1.2.9 +- appveyor DownloadFile http://zlib.net/zlib-1.2.10.tar.gz -FileName zlib-1.2.10.tar.gz +- 7z x zlib-1.2.10.tar.gz > NUL +- 7z x zlib-1.2.10.tar > NUL +- cd zlib-1.2.10 - md build - cd build - cmake -G %msvc% .. @@ -46,9 +46,9 @@ build_script: - cd .. - md build - cd build -- cmake -G %msvc% %cpp11% %boost% -DMSGPACK_BOOST_DIR=C:\Libraries\\boost_1_60_0 -DGTEST_LIBRARY=%APPVEYOR_BUILD_FOLDER%\googletest-release-1.7.0\build\Release\gtest.lib -DGTEST_MAIN_LIBRARY=%APPVEYOR_BUILD_FOLDER%\googletest-release-1.7.0\build\Release\gtest_main.lib -DGTEST_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%\googletest-release-1.7.0\include -DZLIB_LIBRARY=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.9\build\Release\zlib.lib -DZLIB_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.9 -DCMAKE_CXX_FLAGS='"/D_VARIADIC_MAX=10 /EHsc"' .. +- cmake -G %msvc% %cpp11% %boost% %x3_parse% -DMSGPACK_BOOST_DIR=C:\Libraries\\boost_1_60_0 -DGTEST_LIBRARY=%APPVEYOR_BUILD_FOLDER%\googletest-release-1.7.0\build\Release\gtest.lib -DGTEST_MAIN_LIBRARY=%APPVEYOR_BUILD_FOLDER%\googletest-release-1.7.0\build\Release\gtest_main.lib -DGTEST_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%\googletest-release-1.7.0\include -DZLIB_LIBRARY=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.10\build\Release\zlib.lib -DZLIB_INCLUDE_DIR=%APPVEYOR_BUILD_FOLDER%\zlib-1.2.10 -DCMAKE_CXX_FLAGS='"/D_VARIADIC_MAX=10 /EHsc"' .. - cmake --build . --config Release test_script: -- set PATH=%PATH%;%APPVEYOR_BUILD_FOLDER%\googletest-release-1.7.0\build\Release;%APPVEYOR_BUILD_FOLDER%\zlib-1.2.9\build\Release;%APPVEYOR_BUILD_FOLDER%\build\release +- set PATH=%PATH%;%APPVEYOR_BUILD_FOLDER%\googletest-release-1.7.0\build\Release;%APPVEYOR_BUILD_FOLDER%\zlib-1.2.10\build\Release;%APPVEYOR_BUILD_FOLDER%\build\release - ctest -V diff --git a/ci/build_cmake.sh b/ci/build_cmake.sh index 608c222c..9dd03066 100755 --- a/ci/build_cmake.sh +++ b/ci/build_cmake.sh @@ -16,42 +16,14 @@ then exit $ret fi -if [ $1 = "cpp11" ] +if [ "${ARCH}" == "32" ] then - cpp11="-DMSGPACK_CXX11=ON" + export BIT32="ON" else - cpp11="" + export BIT32="OFF" fi -if [ $2 = "32" ] -then - bit32="-DMSGPACK_32BIT=ON" -else - bit32="" -fi - -if [ $3 = "boost" ] -then - boost="-DMSGPACK_BOOST=ON" -else - boost="" -fi - -if [ "$4" != "" ] -then - boost_dir="-DMSGPACK_BOOST_DIR=$4" -else - boost_dir="" -fi - -if [ "$5" = "OFF" ] -then - shared="-DMSGPACK_ENABLE_SHARED=OFF" -else - shared="" -fi - -cmake $cpp11 $bit32 $boost $boost_dir $shared -DMSGPACK_CHAR_SIGN=${CHAR_SIGN} -DMSGPACK_DEFAULT_API_VERSION=${API_VERSION} .. +cmake -DMSGPACK_CXX11=${CXX11} -DMSGPACK_32BIT=${BIT32} -DMSGPACK_BOOST=${BOOST} -DMSGPACK_ENABLE_SHARED=${SHARED} -DMSGPACK_CHAR_SIGN=${CHAR_SIGN} -DMSGPACK_DEFAULT_API_VERSION=${API_VERSION} -DMSGPACK_USE_X3_PARSE=${X3_PARSE} .. ret=$? if [ $ret -ne 0 ] @@ -83,7 +55,7 @@ then exit $ret fi -if [ "$2" != "32" ] && [ `uname` = "Linux" ] +if [ "${ARCH}" != "32" ] && [ `uname` = "Linux" ] then ctest -T memcheck | tee memcheck.log diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index e447d1a2..ea3c7db2 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -2,3 +2,4 @@ ADD_SUBDIRECTORY (c) ADD_SUBDIRECTORY (cpp03) ADD_SUBDIRECTORY (cpp11) ADD_SUBDIRECTORY (boost) +ADD_SUBDIRECTORY (x3) diff --git a/example/c/CMakeLists.txt b/example/c/CMakeLists.txt index 91c90c28..dea2c1d8 100644 --- a/example/c/CMakeLists.txt +++ b/example/c/CMakeLists.txt @@ -1,6 +1,9 @@ -INCLUDE_DIRECTORIES ( - ../include -) +IF (MSGPACK_ENABLE_SHARED) + SET (MSGPACK_LIB msgpackc) +ELSE () + SET (MSGPACK_LIB msgpackc-static) +ENDIF () + LIST (APPEND exec_PROGRAMS lib_buffer_unpack.c @@ -17,7 +20,7 @@ FOREACH (source_file ${exec_PROGRAMS}) ${source_file} ) TARGET_LINK_LIBRARIES (${source_file_we} - msgpackc + ${MSGPACK_LIB} ) IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -g -O3") diff --git a/example/cpp03/CMakeLists.txt b/example/cpp03/CMakeLists.txt index 6fee0cc2..1fa2ade4 100644 --- a/example/cpp03/CMakeLists.txt +++ b/example/cpp03/CMakeLists.txt @@ -22,13 +22,11 @@ IF (NOT MSVC) ) ENDIF () -IF (MSGPACK_BOOST) - IF (NOT MSVC) - LIST (APPEND with_boost_lib_PROGRAMS - speed_test.cpp - speed_test_nested_array.cpp - ) - ENDIF () +IF (Boost_TIMER_LIBRARY AND Boost_CHRONO_LIBRARY AND Boost_SYSTEM_LIBRARY) + LIST (APPEND with_boost_lib_PROGRAMS + speed_test.cpp + speed_test_nested_array.cpp + ) ENDIF () FOREACH (source_file ${exec_PROGRAMS}) @@ -56,10 +54,10 @@ FOREACH (source_file ${with_pthread_PROGRAMS}) ${source_file} ) TARGET_LINK_LIBRARIES (${source_file_we} - pthread + ${CMAKE_THREAD_LIBS_INIT} ) IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") - SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -g -O3 -pthread") + SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -g -O3") ENDIF () IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]") diff --git a/example/cpp11/CMakeLists.txt b/example/cpp11/CMakeLists.txt index 43816cbc..46c263e6 100644 --- a/example/cpp11/CMakeLists.txt +++ b/example/cpp11/CMakeLists.txt @@ -6,9 +6,14 @@ IF (MSGPACK_CXX11) LIST (APPEND exec_PROGRAMS container.cpp non_def_con_class.cpp - socket_stream_example.cpp ) + IF ("${MSGPACK_DEFAULT_API_VERSION}" GREATER "1") + LIST (APPEND exec_PROGRAMS + socket_stream_example.cpp + ) + ENDIF () + FOREACH (source_file ${exec_PROGRAMS}) GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE) ADD_EXECUTABLE ( diff --git a/example/x3/CMakeLists.txt b/example/x3/CMakeLists.txt new file mode 100644 index 00000000..f4b944f1 --- /dev/null +++ b/example/x3/CMakeLists.txt @@ -0,0 +1,64 @@ +IF (MSGPACK_USE_X3_PARSE) + INCLUDE_DIRECTORIES ( + ../include + ) + + LIST (APPEND exec_PROGRAMS + unpack.cpp + parse.cpp + ) + IF (Boost_CONTEXT_LIBRARY AND Boost_SYSTEM_LIBRARY AND CMAKE_THREAD_LIBS_INIT) + LIST (APPEND with_boost_PROGRAMS + stream_unpack.cpp + ) + ENDIF () + FOREACH (source_file ${exec_PROGRAMS}) + GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE) + ADD_EXECUTABLE ( + ${source_file_we} + ${source_file} + ) + IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -g ") + ENDIF () + + IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-mismatched-tags") + ENDIF () + + IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]") + STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + ELSE () + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX") + ENDIF () + ENDIF () + ENDFOREACH () + FOREACH (source_file ${with_boost_PROGRAMS}) + GET_FILENAME_COMPONENT (source_file_we ${source_file} NAME_WE) + ADD_EXECUTABLE ( + ${source_file_we} + ${source_file} + ) + TARGET_LINK_LIBRARIES (${source_file_we} + ${Boost_CONTEXT_LIBRARY} + ${Boost_SYSTEM_LIBRARY} + ${CMAKE_THREAD_LIBS_INIT} + ) + IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS "-Wall -Wextra -Werror -g ") + ENDIF () + + IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") + SET_PROPERTY (TARGET ${source_file_we} APPEND_STRING PROPERTY COMPILE_FLAGS " -Wno-mismatched-tags") + ENDIF () + + IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") + IF (CMAKE_CXX_FLAGS MATCHES "/W[0-4]") + STRING(REGEX REPLACE "/W[0-4]" "/W3 /WX" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + ELSE () + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3 /WX") + ENDIF () + ENDIF () + ENDFOREACH () +ENDIF () diff --git a/example/x3/parse.cpp b/example/x3/parse.cpp new file mode 100644 index 00000000..6ee1b3e8 --- /dev/null +++ b/example/x3/parse.cpp @@ -0,0 +1,119 @@ +// MessagePack for C++ example +// +// Copyright (C) 2017 KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#include +#include +#include + +// MSGPACK_USE_X3_PARSE should be defined before including msgpack.hpp +// It usually defined as a compiler option as -DMSGPACK_USE_X3_PARSE. + +#include + +struct json_like_visitor : msgpack::v2::null_visitor { + json_like_visitor(std::string& s):m_s(s) {} + + bool visit_nil() { + m_s += "null"; + return true; + } + bool visit_boolean(bool v) { + if (v) m_s += "true"; + else m_s += "false"; + return true; + } + bool visit_positive_integer(uint64_t v) { + std::stringstream ss; + ss << v; + m_s += ss.str(); + return true; + } + bool visit_negative_integer(int64_t v) { + std::stringstream ss; + ss << v; + m_s += ss.str(); + return true; + } + bool visit_float(double v) { + std::stringstream ss; + ss << v; + m_s += ss.str(); + return true; + } + bool visit_str(const char* v, uint32_t size) { + m_s += '"' + std::string(v, size) + '"'; + return true; + } + bool start_array(uint32_t /*num_elements*/) { + m_s += "["; + return true; + } + bool end_array_item() { + m_s += ","; + return true; + } + bool end_array() { + m_s.erase(m_s.size() - 1, 1); // remove the last ',' + m_s += "]"; + return true; + } + bool start_map(uint32_t /*num_kv_pairs*/) { + m_s += "{"; + return true; + } + bool end_map_key() { + m_s += ":"; + return true; + } + bool end_map_value() { + m_s += ","; + return true; + } + bool end_map() { + m_s.erase(m_s.size() - 1, 1); // remove the last ',' + m_s += "}"; + return true; + } + void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) { + } + void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) { + } + std::string& m_s; +}; + +int main() { + std::stringstream ss; + std::map> v1 { + { "ABC", { 1, 2, 3 } }, + { "DEFG", { 4, 5 } } + }; + std::vector v2 { + "HIJ", "KLM", "NOP" + }; + msgpack::pack(ss, v1); + msgpack::pack(ss, v2); + + std::string const& buf = ss.str(); + auto it = buf.begin(); + auto end = buf.end(); + { + std::string str; + bool ret = msgpack::parse(it, end, json_like_visitor(str)); + // it is updated here. + assert(ret); + std::cout << str << std::endl; + } + { + std::string str; + bool ret = msgpack::parse(it, end, json_like_visitor(str)); + // it is updated here. + assert(ret); + std::cout << str << std::endl; + } +} diff --git a/example/x3/stream_unpack.cpp b/example/x3/stream_unpack.cpp new file mode 100644 index 00000000..ccfd9afe --- /dev/null +++ b/example/x3/stream_unpack.cpp @@ -0,0 +1,239 @@ +// MessagePack for C++ example +// +// Copyright (C) 2017 KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#include +#include +#include +#include + +// MSGPACK_USE_X3_PARSE should be defined before including msgpack.hpp +// It usually defined as a compiler option as -DMSGPACK_USE_X3_PARSE. + +//#define MSGPACK_USE_X3_PARSE + +#include + +#include +#include +#include + + +namespace as = boost::asio; +namespace x3 = boost::spirit::x3; +namespace coro2 = boost::coroutines2; + +using pull_type = coro2::asymmetric_coroutine>>::pull_type; + +// iterator fetching data from coroutine2. +class buffered_iterator : public std::iterator { +public: + using pointer_t = typename iterator::pointer; + using reference_t = typename iterator::reference; + + explicit buffered_iterator(pull_type& source) noexcept + : source_{ &source } { + fetch_(); + } + buffered_iterator() = default; + + bool operator==(buffered_iterator const& other) const noexcept { + if (!other.source_ && !source_ && !other.buf_ && !buf_) return true; + return other.it_ == it_; + } + + bool operator!=(buffered_iterator const& other) const noexcept { + return !(other == *this); + } + + buffered_iterator & operator++() { + increment_(); + return * this; + } + + buffered_iterator operator++(int) = delete; + + reference_t operator*() noexcept { + return *it_; + } + + pointer_t operator->() noexcept { + return std::addressof(*it_); + } + +private: + void fetch_() noexcept { + BOOST_ASSERT( nullptr != source_); + if (*source_) { + buf_ = source_->get(); + it_ = buf_->begin(); + } + else { + source_ = nullptr; + buf_.reset(); + } + } + + void increment_() { + BOOST_ASSERT( nullptr != source_); + BOOST_ASSERT(*source_); + if (++it_ == buf_->end()) { + (*source_)(); + fetch_(); + } + } + +private: + pull_type* source_{ nullptr }; + std::shared_ptr> buf_; + std::vector::iterator it_; +}; + +// session class that corresponding to each client +class session : public std::enable_shared_from_this { +public: + session(as::ip::tcp::socket socket) + : socket_(std::move(socket)) { + } + + void start() { + sink_ = std::make_shared>>::push_type>( + [&, this](pull_type& source) { + // *1 is started when the first sink is called. + + std::cout << "session started" << std::endl; + do_read(); + source(); + + // use buffered_iterator here + // b is incremented in msgpack::unpack() and fetch data from sink + // via coroutine2 mechanism + auto b = boost::spirit::make_default_multi_pass(buffered_iterator(source)); + auto e = boost::spirit::make_default_multi_pass(buffered_iterator()); + + // This is usually an infinity look, but for test, loop is finished when + // two message pack data is processed. + for (int i = 0; i != 2; ++i) { + auto oh = msgpack::unpack(b, e); + std::cout << oh.get() << std::endl; + } + } + ); + // send dummy data to start *1 + (*sink_)({}); + } + +private: + void do_read() { + std::cout << "session do_read() is called" << std::endl; + auto self(shared_from_this()); + auto data = std::make_shared>(static_cast(max_length)); + socket_.async_read_some( + boost::asio::buffer(*data), + [this, self, data] + (boost::system::error_code ec, std::size_t length) { + if (!ec) { + data->resize(length); + (*sink_)(data); + do_read(); + } + } + ); + } + + as::ip::tcp::socket socket_; + static constexpr std::size_t const max_length = 1024; + std::shared_ptr>>::push_type> sink_; +}; + +class server { +public: + server( + as::io_service& ios, + std::uint16_t port) + : acceptor_(ios, as::ip::tcp::endpoint(as::ip::tcp::v4(), port)), + socket_(ios) { + do_accept(); + std::cout << "server start accept" << std::endl; + ios.run(); + } + +private: + void do_accept() { + acceptor_.async_accept( + socket_, + [this](boost::system::error_code ec) { + if (!ec) { + std::make_shared(std::move(socket_))->start(); + } + // for test, only one session is accepted. + // do_accept(); + } + ); + } + + as::ip::tcp::acceptor acceptor_; + as::ip::tcp::socket socket_; +}; + +int main() { + std::thread srv( + []{ + boost::asio::io_service ios; + server s(ios, 12345); + } + ); + + std::thread cli( + []{ + std::this_thread::sleep_for(std::chrono::seconds(1)); + std::cout << "client start" << std::endl; + + std::stringstream ss; + std::map> v1 { + { "ABC", { 1, 2, 3 } }, + { "DEFG", { 4, 5 } } + }; + std::vector v2 { + "HIJ", "KLM", "NOP" + }; + msgpack::pack(ss, v1); + msgpack::pack(ss, v2); + + auto send_data = ss.str(); + + boost::asio::io_service ios; + as::ip::tcp::resolver::query q("127.0.0.1", "12345"); + as::ip::tcp::resolver r(ios); + auto it = r.resolve(q); + + std::cout << "client connect" << std::endl; + as::ip::tcp::socket s(ios); + as::connect(s, it); + + + std::size_t const size = 5; + std::size_t rest = send_data.size(); + std::size_t index = 0; + while (rest != 0) { + std::cout << "client send data" << std::endl; + auto send_size = size < rest ? size : rest; + as::write(s, as::buffer(&send_data[index], send_size)); + rest -= send_size; + index += send_size; + std::cout << "client wait" << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + } + ); + + cli.join(); + std::cout << "client joinded" << std::endl; + srv.join(); + std::cout << "server joinded" << std::endl; +} diff --git a/example/x3/unpack.cpp b/example/x3/unpack.cpp new file mode 100644 index 00000000..8200c663 --- /dev/null +++ b/example/x3/unpack.cpp @@ -0,0 +1,43 @@ +// MessagePack for C++ example +// +// Copyright (C) 2017 KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#include +#include +#include + +// MSGPACK_USE_X3_PARSE should be defined before including msgpack.hpp +// It usually defined as a compiler option as -DMSGPACK_USE_X3_PARSE. + +#include + +int main() { + std::stringstream ss; + std::map> v1 { + { "ABC", { 1, 2, 3 } }, + { "DEFG", { 4, 5 } } + }; + std::vector v2 { + "HIJ", "KLM", "NOP" + }; + msgpack::pack(ss, v1); + msgpack::pack(ss, v2); + + std::string const& buf = ss.str(); + auto it = buf.begin(); + auto end = buf.end(); + { + auto oh = msgpack::unpack(it, end); + // it is updated here. + assert(v1 == (oh.get().as>>())); + } + { + auto oh = msgpack::unpack(it, end); + assert(v2 == oh.get().as>()); + } +} diff --git a/include/msgpack/parse_return.hpp b/include/msgpack/parse_return.hpp new file mode 100644 index 00000000..33ddcd7b --- /dev/null +++ b/include/msgpack/parse_return.hpp @@ -0,0 +1,16 @@ +// +// MessagePack for C++ deserializing routine +// +// Copyright (C) 2017 KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#ifndef MSGPACK_PARSE_RETURN_HPP +#define MSGPACK_PARSE_RETURN_HPP + +#include "msgpack/v1/parse_return.hpp" +#include "msgpack/v2/parse_return.hpp" + +#endif // MSGPACK_PARSE_RETURN_HPP diff --git a/include/msgpack/unpack.hpp b/include/msgpack/unpack.hpp index 8cd02602..7ea44086 100644 --- a/include/msgpack/unpack.hpp +++ b/include/msgpack/unpack.hpp @@ -14,5 +14,6 @@ #include "msgpack/v1/unpack.hpp" #include "msgpack/v2/unpack.hpp" +#include "msgpack/v2/x3_unpack.hpp" #endif // MSGPACK_UNPACK_HPP diff --git a/include/msgpack/unpack_exception.hpp b/include/msgpack/unpack_exception.hpp new file mode 100644 index 00000000..fa28f749 --- /dev/null +++ b/include/msgpack/unpack_exception.hpp @@ -0,0 +1,15 @@ +// +// MessagePack for C++ deserializing routine +// +// Copyright (C) 2017 KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#ifndef MSGPACK_UNPACK_EXCEPTION_HPP +#define MSGPACK_UNPACK_EXCEPTION_HPP + +#include "msgpack/v1/unpack_exception.hpp" + +#endif // MSGPACK_UNPACK_EXCEPTION_HPP diff --git a/include/msgpack/v1/parse_return.hpp b/include/msgpack/v1/parse_return.hpp new file mode 100644 index 00000000..4fb0249a --- /dev/null +++ b/include/msgpack/v1/parse_return.hpp @@ -0,0 +1,36 @@ +// +// MessagePack for C++ deserializing routine +// +// Copyright (C) 2008-2016 FURUHASHI Sadayuki and KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#ifndef MSGPACK_V1_PARSE_RETURN_HPP +#define MSGPACK_V1_PARSE_RETURN_HPP + +#include "msgpack/versioning.hpp" + +namespace msgpack { + +/// @cond +MSGPACK_API_VERSION_NAMESPACE(v1) { +/// @endcond + + +// for internal use +typedef enum { + PARSE_SUCCESS = 2, + PARSE_EXTRA_BYTES = 1, + PARSE_CONTINUE = 0, + PARSE_PARSE_ERROR = -1 +} parse_return; + +/// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v1) +/// @endcond + +} // namespace msgpack + +#endif // MSGPACK_V1_PARSE_RETURN_HPP diff --git a/include/msgpack/v1/unpack.hpp b/include/msgpack/v1/unpack.hpp index 07d1dc99..1eb55820 100644 --- a/include/msgpack/v1/unpack.hpp +++ b/include/msgpack/v1/unpack.hpp @@ -14,12 +14,12 @@ #include "msgpack/unpack_decl.hpp" #include "msgpack/object.hpp" #include "msgpack/zone.hpp" +#include "msgpack/unpack_exception.hpp" #include "msgpack/unpack_define.h" #include "msgpack/cpp_config.hpp" #include "msgpack/sysdep.h" #include -#include #if !defined(MSGPACK_USE_CPP03) #include @@ -39,96 +39,6 @@ namespace msgpack { MSGPACK_API_VERSION_NAMESPACE(v1) { /// @endcond -struct unpack_error : public std::runtime_error { - explicit unpack_error(const std::string& msg) - :std::runtime_error(msg) {} -#if !defined(MSGPACK_USE_CPP03) - explicit unpack_error(const char* msg): - std::runtime_error(msg) {} -#endif // !defined(MSGPACK_USE_CPP03) -}; - -struct parse_error : public unpack_error { - explicit parse_error(const std::string& msg) - :unpack_error(msg) {} -#if !defined(MSGPACK_USE_CPP03) - explicit parse_error(const char* msg) - :unpack_error(msg) {} -#endif // !defined(MSGPACK_USE_CPP03) -}; - -struct insufficient_bytes : public unpack_error { - explicit insufficient_bytes(const std::string& msg) - :unpack_error(msg) {} -#if !defined(MSGPACK_USE_CPP03) - explicit insufficient_bytes(const char* msg) - :unpack_error(msg) {} -#endif // !defined(MSGPACK_USE_CPP03) -}; - -struct size_overflow : public unpack_error { - explicit size_overflow(const std::string& msg) - :unpack_error(msg) {} -#if !defined(MSGPACK_USE_CPP03) - explicit size_overflow(const char* msg) - :unpack_error(msg) {} -#endif -}; - -struct array_size_overflow : public size_overflow { - array_size_overflow(const std::string& msg) - :size_overflow(msg) {} -#if !defined(MSGPACK_USE_CPP03) - array_size_overflow(const char* msg) - :size_overflow(msg) {} -#endif -}; - -struct map_size_overflow : public size_overflow { - map_size_overflow(const std::string& msg) - :size_overflow(msg) {} -#if !defined(MSGPACK_USE_CPP03) - map_size_overflow(const char* msg) - :size_overflow(msg) {} -#endif -}; - -struct str_size_overflow : public size_overflow { - str_size_overflow(const std::string& msg) - :size_overflow(msg) {} -#if !defined(MSGPACK_USE_CPP03) - str_size_overflow(const char* msg) - :size_overflow(msg) {} -#endif -}; - -struct bin_size_overflow : public size_overflow { - bin_size_overflow(const std::string& msg) - :size_overflow(msg) {} -#if !defined(MSGPACK_USE_CPP03) - bin_size_overflow(const char* msg) - :size_overflow(msg) {} -#endif -}; - -struct ext_size_overflow : public size_overflow { - ext_size_overflow(const std::string& msg) - :size_overflow(msg) {} -#if !defined(MSGPACK_USE_CPP03) - ext_size_overflow(const char* msg) - :size_overflow(msg) {} -#endif -}; - -struct depth_size_overflow : public size_overflow { - depth_size_overflow(const std::string& msg) - :size_overflow(msg) {} -#if !defined(MSGPACK_USE_CPP03) - depth_size_overflow(const char* msg) - :size_overflow(msg) {} -#endif -}; - namespace detail { class unpack_user { @@ -1425,7 +1335,7 @@ inline void unpacker::remove_nonparsed_buffer() namespace detail { -inline unpack_return +inline parse_return unpack_imp(const char* data, std::size_t len, std::size_t& off, msgpack::zone& result_zone, msgpack::object& result, bool& referenced, unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, @@ -1435,7 +1345,7 @@ unpack_imp(const char* data, std::size_t len, std::size_t& off, if(len <= noff) { // FIXME - return UNPACK_CONTINUE; + return PARSE_CONTINUE; } detail::context ctx(f, user_data, limit); @@ -1447,23 +1357,23 @@ unpack_imp(const char* data, std::size_t len, std::size_t& off, int e = ctx.execute(data, len, noff); if(e < 0) { - return UNPACK_PARSE_ERROR; + return PARSE_PARSE_ERROR; } referenced = ctx.user().referenced(); off = noff; if(e == 0) { - return UNPACK_CONTINUE; + return PARSE_CONTINUE; } result = ctx.data(); if(noff < len) { - return UNPACK_EXTRA_BYTES; + return PARSE_EXTRA_BYTES; } - return UNPACK_SUCCESS; + return PARSE_SUCCESS; } } // detail @@ -1480,19 +1390,19 @@ inline msgpack::object_handle unpack( msgpack::unique_ptr z(new msgpack::zone); referenced = false; std::size_t noff = off; - unpack_return ret = detail::unpack_imp( + parse_return ret = detail::unpack_imp( data, len, noff, *z, obj, referenced, f, user_data, limit); switch(ret) { - case UNPACK_SUCCESS: + case PARSE_SUCCESS: off = noff; return msgpack::object_handle(obj, msgpack::move(z)); - case UNPACK_EXTRA_BYTES: + case PARSE_EXTRA_BYTES: off = noff; return msgpack::object_handle(obj, msgpack::move(z)); - case UNPACK_CONTINUE: + case PARSE_CONTINUE: throw msgpack::insufficient_bytes("insufficient bytes"); - case UNPACK_PARSE_ERROR: + case PARSE_PARSE_ERROR: default: throw msgpack::parse_error("parse error"); } @@ -1537,23 +1447,23 @@ inline void unpack( msgpack::unique_ptr z(new msgpack::zone); referenced = false; std::size_t noff = off; - unpack_return ret = detail::unpack_imp( + parse_return ret = detail::unpack_imp( data, len, noff, *z, obj, referenced, f, user_data, limit); switch(ret) { - case UNPACK_SUCCESS: + case PARSE_SUCCESS: off = noff; result.set(obj); result.zone() = msgpack::move(z); return; - case UNPACK_EXTRA_BYTES: + case PARSE_EXTRA_BYTES: off = noff; result.set(obj); result.zone() = msgpack::move(z); return; - case UNPACK_CONTINUE: + case PARSE_CONTINUE: throw msgpack::insufficient_bytes("insufficient bytes"); - case UNPACK_PARSE_ERROR: + case PARSE_PARSE_ERROR: default: throw msgpack::parse_error("parse error"); } @@ -1600,19 +1510,19 @@ inline msgpack::object unpack( msgpack::object obj; std::size_t noff = off; referenced = false; - unpack_return ret = detail::unpack_imp( + parse_return ret = detail::unpack_imp( data, len, noff, z, obj, referenced, f, user_data, limit); switch(ret) { - case UNPACK_SUCCESS: + case PARSE_SUCCESS: off = noff; return obj; - case UNPACK_EXTRA_BYTES: + case PARSE_EXTRA_BYTES: off = noff; return obj; - case UNPACK_CONTINUE: + case PARSE_CONTINUE: throw msgpack::insufficient_bytes("insufficient bytes"); - case UNPACK_PARSE_ERROR: + case PARSE_PARSE_ERROR: default: throw msgpack::parse_error("parse error"); } diff --git a/include/msgpack/v1/unpack_decl.hpp b/include/msgpack/v1/unpack_decl.hpp index f8fe2919..99896fa4 100644 --- a/include/msgpack/v1/unpack_decl.hpp +++ b/include/msgpack/v1/unpack_decl.hpp @@ -16,6 +16,7 @@ #include "msgpack/zone.hpp" #include "msgpack/cpp_config.hpp" #include "msgpack/sysdep.h" +#include "msgpack/parse_return.hpp" #include #include @@ -433,17 +434,9 @@ void unpack( unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, unpack_limit const& limit = unpack_limit()); -// for internal use -typedef enum { - UNPACK_SUCCESS = 2, - UNPACK_EXTRA_BYTES = 1, - UNPACK_CONTINUE = 0, - UNPACK_PARSE_ERROR = -1 -} unpack_return; - namespace detail { -unpack_return +parse_return unpack_imp(const char* data, std::size_t len, std::size_t& off, msgpack::zone& result_zone, msgpack::object& result, bool& referenced, unpack_reference_func f, void* user_data, diff --git a/include/msgpack/v1/unpack_exception.hpp b/include/msgpack/v1/unpack_exception.hpp new file mode 100644 index 00000000..c676a620 --- /dev/null +++ b/include/msgpack/v1/unpack_exception.hpp @@ -0,0 +1,122 @@ +// +// MessagePack for C++ deserializing routine +// +// Copyright (C) 2008-2016 FURUHASHI Sadayuki and KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#ifndef MSGPACK_V1_UNPACK_EXCEPTION_HPP +#define MSGPACK_V1_UNPACK_EXCEPTION_HPP + +#include "msgpack/versioning.hpp" + +#include +#include + + +namespace msgpack { + +/// @cond +MSGPACK_API_VERSION_NAMESPACE(v1) { +/// @endcond + +struct unpack_error : public std::runtime_error { + explicit unpack_error(const std::string& msg) + :std::runtime_error(msg) {} +#if !defined(MSGPACK_USE_CPP03) + explicit unpack_error(const char* msg): + std::runtime_error(msg) {} +#endif // !defined(MSGPACK_USE_CPP03) +}; + +struct parse_error : public unpack_error { + explicit parse_error(const std::string& msg) + :unpack_error(msg) {} +#if !defined(MSGPACK_USE_CPP03) + explicit parse_error(const char* msg) + :unpack_error(msg) {} +#endif // !defined(MSGPACK_USE_CPP03) +}; + +struct insufficient_bytes : public unpack_error { + explicit insufficient_bytes(const std::string& msg) + :unpack_error(msg) {} +#if !defined(MSGPACK_USE_CPP03) + explicit insufficient_bytes(const char* msg) + :unpack_error(msg) {} +#endif // !defined(MSGPACK_USE_CPP03) +}; + +struct size_overflow : public unpack_error { + explicit size_overflow(const std::string& msg) + :unpack_error(msg) {} +#if !defined(MSGPACK_USE_CPP03) + explicit size_overflow(const char* msg) + :unpack_error(msg) {} +#endif +}; + +struct array_size_overflow : public size_overflow { + array_size_overflow(const std::string& msg) + :size_overflow(msg) {} +#if !defined(MSGPACK_USE_CPP03) + array_size_overflow(const char* msg) + :size_overflow(msg) {} +#endif +}; + +struct map_size_overflow : public size_overflow { + map_size_overflow(const std::string& msg) + :size_overflow(msg) {} +#if !defined(MSGPACK_USE_CPP03) + map_size_overflow(const char* msg) + :size_overflow(msg) {} +#endif +}; + +struct str_size_overflow : public size_overflow { + str_size_overflow(const std::string& msg) + :size_overflow(msg) {} +#if !defined(MSGPACK_USE_CPP03) + str_size_overflow(const char* msg) + :size_overflow(msg) {} +#endif +}; + +struct bin_size_overflow : public size_overflow { + bin_size_overflow(const std::string& msg) + :size_overflow(msg) {} +#if !defined(MSGPACK_USE_CPP03) + bin_size_overflow(const char* msg) + :size_overflow(msg) {} +#endif +}; + +struct ext_size_overflow : public size_overflow { + ext_size_overflow(const std::string& msg) + :size_overflow(msg) {} +#if !defined(MSGPACK_USE_CPP03) + ext_size_overflow(const char* msg) + :size_overflow(msg) {} +#endif +}; + +struct depth_size_overflow : public size_overflow { + depth_size_overflow(const std::string& msg) + :size_overflow(msg) {} +#if !defined(MSGPACK_USE_CPP03) + depth_size_overflow(const char* msg) + :size_overflow(msg) {} +#endif +}; + +/// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v1) +/// @endcond + +} // namespace msgpack + + +#endif // MSGPACK_V1_UNPACK_EXCEPTION_HPP diff --git a/include/msgpack/v2/create_object_visitor.hpp b/include/msgpack/v2/create_object_visitor.hpp new file mode 100644 index 00000000..8c7cd4b4 --- /dev/null +++ b/include/msgpack/v2/create_object_visitor.hpp @@ -0,0 +1,243 @@ +// +// MessagePack for C++ deserializing routine +// +// Copyright (C) 2017 KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#ifndef MSGPACK_V2_CREATE_OBJECT_VISITOR_HPP +#define MSGPACK_V2_CREATE_OBJECT_VISITOR_HPP + +#include "msgpack/unpack_decl.hpp" +#include "msgpack/unpack_exception.hpp" +#include "msgpack/v2/null_visitor.hpp" + +namespace msgpack { + +/// @cond +MSGPACK_API_VERSION_NAMESPACE(v2) { +/// @endcond + +namespace detail { + +class create_object_visitor : public msgpack::v2::null_visitor { +public: + create_object_visitor(unpack_reference_func f, void* user_data, unpack_limit const& limit) + :m_func(f), m_user_data(user_data), m_limit(limit) { + m_stack.reserve(MSGPACK_EMBED_STACK_SIZE); + m_stack.push_back(&m_obj); + } + +#if !defined(MSGPACK_USE_CPP03) + create_object_visitor(create_object_visitor&& other) + :m_func(other.m_func), + m_user_data(other.m_user_data), + m_limit(std::move(other.m_limit)), + m_stack(std::move(other.m_stack)), + m_zone(other.m_zone), + m_referenced(other.m_referenced) { + other.m_zone = MSGPACK_NULLPTR; + m_stack[0] = &m_obj; + } + create_object_visitor& operator=(create_object_visitor&& other) { + this->~create_object_visitor(); + new (this) create_object_visitor(std::move(other)); + return *this; + } +#endif // !defined(MSGPACK_USE_CPP03) + + void init() { + m_stack.resize(1); + m_obj = msgpack::object(); + m_stack[0] = &m_obj; + } + msgpack::object const& data() const + { + return m_obj; + } + msgpack::zone const& zone() const { return *m_zone; } + msgpack::zone& zone() { return *m_zone; } + void set_zone(msgpack::zone& zone) { m_zone = &zone; } + bool referenced() const { return m_referenced; } + void set_referenced(bool referenced) { m_referenced = referenced; } + // visit functions + bool visit_nil() { + msgpack::object* obj = m_stack.back(); + obj->type = msgpack::type::NIL; + return true; + } + bool visit_boolean(bool v) { + msgpack::object* obj = m_stack.back(); + obj->type = msgpack::type::BOOLEAN; + obj->via.boolean = v; + return true; + } + bool visit_positive_integer(uint64_t v) { + msgpack::object* obj = m_stack.back(); + obj->type = msgpack::type::POSITIVE_INTEGER; + obj->via.u64 = v; + return true; + } + bool visit_negative_integer(int64_t v) { + msgpack::object* obj = m_stack.back(); + if(v >= 0) { + obj->type = msgpack::type::POSITIVE_INTEGER; + obj->via.u64 = v; + } + else { + obj->type = msgpack::type::NEGATIVE_INTEGER; + obj->via.i64 = v; + } + return true; + } + bool visit_float(double v) { + msgpack::object* obj = m_stack.back(); + obj->type = msgpack::type::FLOAT; + obj->via.f64 = v; + return true; + } + bool visit_str(const char* v, uint32_t size) { + if (size > m_limit.str()) throw msgpack::str_size_overflow("str size overflow"); + msgpack::object* obj = m_stack.back(); + obj->type = msgpack::type::STR; + if (m_func && m_func(obj->type, size, m_user_data)) { + obj->via.str.ptr = v; + set_referenced(true); + } + else { + char* tmp = static_cast(zone().allocate_align(size)); + std::memcpy(tmp, v, size); + obj->via.str.ptr = tmp; + } + obj->via.str.size = size; + return true; + } + bool visit_bin(const char* v, uint32_t size) { + if (size > m_limit.bin()) throw msgpack::bin_size_overflow("bin size overflow"); + msgpack::object* obj = m_stack.back(); + obj->type = msgpack::type::BIN; + if (m_func && m_func(obj->type, size, m_user_data)) { + obj->via.bin.ptr = v; + set_referenced(true); + } + else { + char* tmp = static_cast(zone().allocate_align(size)); + std::memcpy(tmp, v, size); + obj->via.bin.ptr = tmp; + } + obj->via.bin.size = size; + return true; + } + bool visit_ext(const char* v, uint32_t size) { + if (size > m_limit.ext()) throw msgpack::ext_size_overflow("ext size overflow"); + msgpack::object* obj = m_stack.back(); + obj->type = msgpack::type::EXT; + if (m_func && m_func(obj->type, size, m_user_data)) { + obj->via.ext.ptr = v; + set_referenced(true); + } + else { + char* tmp = static_cast(zone().allocate_align(size)); + std::memcpy(tmp, v, size); + obj->via.ext.ptr = tmp; + } + obj->via.ext.size = static_cast(size - 1); + return true; + } + bool start_array(uint32_t num_elements) { + if (num_elements > m_limit.array()) throw msgpack::array_size_overflow("array size overflow"); + if (m_stack.size() > m_limit.depth()) throw msgpack::depth_size_overflow("depth size overflow"); + msgpack::object* obj = m_stack.back(); + obj->type = msgpack::type::ARRAY; + obj->via.array.size = num_elements; + if (num_elements == 0) { + obj->via.array.ptr = MSGPACK_NULLPTR; + } + else { + size_t size = num_elements*sizeof(msgpack::object); + if (size / sizeof(msgpack::object) != num_elements) { + throw msgpack::array_size_overflow("array size overflow"); + } + obj->via.array.ptr = + static_cast(m_zone->allocate_align(size)); + } + m_stack.push_back(obj->via.array.ptr); + return true; + } + bool start_array_item() { + return true; + } + bool end_array_item() { + ++m_stack.back(); + return true; + } + bool end_array() { + m_stack.pop_back(); + return true; + } + bool start_map(uint32_t num_kv_pairs) { + if (num_kv_pairs > m_limit.map()) throw msgpack::map_size_overflow("map size overflow"); + if (m_stack.size() > m_limit.depth()) throw msgpack::depth_size_overflow("depth size overflow"); + msgpack::object* obj = m_stack.back(); + obj->type = msgpack::type::MAP; + obj->via.map.size = num_kv_pairs; + if (num_kv_pairs == 0) { + obj->via.map.ptr = MSGPACK_NULLPTR; + } + else { + size_t size = num_kv_pairs*sizeof(msgpack::object_kv); + if (size / sizeof(msgpack::object_kv) != num_kv_pairs) { + throw msgpack::map_size_overflow("map size overflow"); + } + obj->via.map.ptr = + static_cast(m_zone->allocate_align(size)); + } + m_stack.push_back(reinterpret_cast(obj->via.map.ptr)); + return true; + } + bool start_map_key() { + return true; + } + bool end_map_key() { + ++m_stack.back(); + return true; + } + bool start_map_value() { + return true; + } + bool end_map_value() { + ++m_stack.back(); + return true; + } + bool end_map() { + m_stack.pop_back(); + return true; + } + void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) { + throw msgpack::parse_error("parse error"); + } + void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) { + throw msgpack::insufficient_bytes("insufficient bytes"); + } +private: +public: + unpack_reference_func m_func; + void* m_user_data; + unpack_limit m_limit; + msgpack::object m_obj; + std::vector m_stack; + msgpack::zone* m_zone; + bool m_referenced; +}; + +} // detail + +/// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v2) +/// @endcond + +} // namespace msgpack + +#endif // MSGPACK_V2_CREATE_OBJECT_VISITOR_HPP diff --git a/include/msgpack/v2/null_visitor.hpp b/include/msgpack/v2/null_visitor.hpp new file mode 100644 index 00000000..a28702bb --- /dev/null +++ b/include/msgpack/v2/null_visitor.hpp @@ -0,0 +1,94 @@ +// +// MessagePack for C++ deserializing routine +// +// Copyright (C) 2017 KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#ifndef MSGPACK_V2_NULL_VISITOR_HPP +#define MSGPACK_V2_NULL_VISITOR_HPP + +#include "msgpack/versioning.hpp" +#include "msgpack/cpp_config.hpp" + +namespace msgpack { + +/// @cond +MSGPACK_API_VERSION_NAMESPACE(v2) { +/// @endcond + +struct null_visitor { + bool visit_nil() { + return true; + } + bool visit_boolean(bool /*v*/) { + return true; + } + bool visit_positive_integer(uint64_t /*v*/) { + return true; + } + bool visit_negative_integer(int64_t /*v*/) { + return true; + } + bool visit_float(double /*v*/) { + return true; + } + bool visit_str(const char* /*v*/, uint32_t /*size*/) { + return true; + } + bool visit_bin(const char* /*v*/, uint32_t /*size*/) { + return true; + } + bool visit_ext(const char* /*v*/, uint32_t /*size*/) { + return true; + } + bool start_array(uint32_t /*num_elements*/) { + return true; + } + bool start_array_item() { + return true; + } + bool end_array_item() { + return true; + } + bool end_array() { + return true; + } + bool start_map(uint32_t /*num_kv_pairs*/) { + return true; + } + bool start_map_key() { + return true; + } + bool end_map_key() { + return true; + } + bool start_map_value() { + return true; + } + bool end_map_value() { + return true; + } + bool end_map() { + return true; + } + void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) { + } + void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) { + } + bool referenced() const { + return false; + } + void set_referenced(bool /*referenced*/) { + } +}; + +/// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v2) +/// @endcond + +} // namespace msgpack + +#endif // MSGPACK_V2_NULL_VISITOR_HPP diff --git a/include/msgpack/v2/parse.hpp b/include/msgpack/v2/parse.hpp new file mode 100644 index 00000000..fdc97341 --- /dev/null +++ b/include/msgpack/v2/parse.hpp @@ -0,0 +1,1054 @@ +// +// MessagePack for C++ deserializing routine +// +// Copyright (C) 2016-2017 KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#ifndef MSGPACK_V2_PARSE_HPP +#define MSGPACK_V2_PARSE_HPP + +#include "msgpack/unpack_decl.hpp" +#include "msgpack/v2/create_object_visitor.hpp" + +namespace msgpack { + +/// @cond +MSGPACK_API_VERSION_NAMESPACE(v2) { +/// @endcond + +namespace detail { + +template +class context { +public: + context() + :m_trail(0), m_cs(MSGPACK_CS_HEADER) + { + } + + void init() + { + m_cs = MSGPACK_CS_HEADER; + m_trail = 0; + m_stack.clear(); + holder().visitor().init(); + } + + parse_return execute(const char* data, std::size_t len, std::size_t& off); + +private: + template + static uint32_t next_cs(T p) + { + return static_cast(*p) & 0x1f; + } + + VisitorHolder& holder() { + return static_cast(*this); + } + + template + parse_return start_aggregate( + StartVisitor const& sv, + EndVisitor const& ev, + const char* load_pos, + std::size_t& off) { + typename value::type size; + load(size, load_pos); + ++m_current; + if (size == 0) { + if (!sv(size)) { + off = m_current - m_start; + return PARSE_STOP_VISITOR; + } + if (!ev()) { + off = m_current - m_start; + return PARSE_STOP_VISITOR; + } + parse_return ret = m_stack.consume(holder()); + if (ret != PARSE_CONTINUE) { + off = m_current - m_start; + return ret; + } + } + else { + m_stack.push(sv.type(), static_cast(size)); + if (!sv(size)) { + off = m_current - m_start; + return PARSE_STOP_VISITOR; + } + } + m_cs = MSGPACK_CS_HEADER; + return PARSE_CONTINUE; + } + + parse_return after_visit_proc(bool visit_result, std::size_t& off) { + ++m_current; + if (!visit_result) { + off = m_current - m_start; + return PARSE_STOP_VISITOR; + } + parse_return ret = m_stack.consume(holder()); + if (ret == PARSE_CONTINUE) { + m_cs = MSGPACK_CS_HEADER; + } + else { + off = m_current - m_start; + } + return ret; + } + + struct array_sv { + array_sv(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {} + bool operator()(uint32_t size) const { + return m_visitor_holder.visitor().start_array(size); + } + msgpack_container_type type() const { return MSGPACK_CT_ARRAY_ITEM; } + private: + VisitorHolder& m_visitor_holder; + }; + struct array_ev { + array_ev(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {} + bool operator()() const { + return m_visitor_holder.visitor().end_array(); + } + private: + VisitorHolder& m_visitor_holder; + }; + struct map_sv { + map_sv(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {} + bool operator()(uint32_t size) const { + return m_visitor_holder.visitor().start_map(size); + } + msgpack_container_type type() const { return MSGPACK_CT_MAP_KEY; } + private: + VisitorHolder& m_visitor_holder; + }; + struct map_ev { + map_ev(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {} + bool operator()() const { + return m_visitor_holder.visitor().end_map(); + } + private: + VisitorHolder& m_visitor_holder; + }; + + struct unpack_stack { + struct stack_elem { + stack_elem(msgpack_container_type type, uint32_t rest):m_type(type), m_rest(rest) {} + msgpack_container_type m_type; + uint32_t m_rest; + }; + unpack_stack() { + m_stack.reserve(MSGPACK_EMBED_STACK_SIZE); + } + void push(msgpack_container_type type, uint32_t rest) { + m_stack.push_back(stack_elem(type, rest)); + } + parse_return consume(VisitorHolder& visitor_holder) { + while (!m_stack.empty()) { + stack_elem& e = m_stack.back(); + switch (e.m_type) { + case MSGPACK_CT_ARRAY_ITEM: + if (!visitor_holder.visitor().end_array_item()) return PARSE_STOP_VISITOR; + if (--e.m_rest == 0) { + m_stack.pop_back(); + if (!visitor_holder.visitor().end_array()) return PARSE_STOP_VISITOR; + } + else { + if (!visitor_holder.visitor().start_array_item()) return PARSE_STOP_VISITOR; + return PARSE_CONTINUE; + } + break; + case MSGPACK_CT_MAP_KEY: + if (!visitor_holder.visitor().end_map_key()) return PARSE_STOP_VISITOR; + if (!visitor_holder.visitor().start_map_value()) return PARSE_STOP_VISITOR; + e.m_type = MSGPACK_CT_MAP_VALUE; + return PARSE_CONTINUE; + case MSGPACK_CT_MAP_VALUE: + if (!visitor_holder.visitor().end_map_value()) return PARSE_STOP_VISITOR; + if (--e.m_rest == 0) { + m_stack.pop_back(); + if (!visitor_holder.visitor().end_map()) return PARSE_STOP_VISITOR; + } + else { + e.m_type = MSGPACK_CT_MAP_KEY; + if (!visitor_holder.visitor().start_map_key()) return PARSE_STOP_VISITOR; + return PARSE_CONTINUE; + } + break; + } + } + return PARSE_SUCCESS; + } + bool empty() const { return m_stack.empty(); } + void clear() { m_stack.clear(); } + private: + std::vector m_stack; + }; + + char const* m_start; + char const* m_current; + + std::size_t m_trail; + uint32_t m_cs; + uint32_t m_num_elements; + unpack_stack m_stack; +}; + +template +inline void check_ext_size(std::size_t /*size*/) { +} + +template <> +inline void check_ext_size<4>(std::size_t size) { + if (size == 0xffffffff) throw msgpack::ext_size_overflow("ext size overflow"); +} + +template +inline parse_return context::execute(const char* data, std::size_t len, std::size_t& off) +{ + assert(len >= off); + + m_start = data; + m_current = data + off; + const char* const pe = data + len; + const char* n = MSGPACK_NULLPTR; + + msgpack::object obj; + + if(m_current == pe) { + off = m_current - m_start; + return PARSE_CONTINUE; + } + bool fixed_trail_again = false; + do { + if (m_cs == MSGPACK_CS_HEADER) { + fixed_trail_again = false; + int selector = *reinterpret_cast(m_current); + if (0x00 <= selector && selector <= 0x7f) { // Positive Fixnum + uint8_t tmp = *reinterpret_cast(m_current); + bool visret = holder().visitor().visit_positive_integer(tmp); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } else if(0xe0 <= selector && selector <= 0xff) { // Negative Fixnum + int8_t tmp = *reinterpret_cast(m_current); + bool visret = holder().visitor().visit_negative_integer(tmp); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } else if (0xc4 <= selector && selector <= 0xdf) { + const uint32_t trail[] = { + 1, // bin 8 0xc4 + 2, // bin 16 0xc5 + 4, // bin 32 0xc6 + 1, // ext 8 0xc7 + 2, // ext 16 0xc8 + 4, // ext 32 0xc9 + 4, // float 32 0xca + 8, // float 64 0xcb + 1, // uint 8 0xcc + 2, // uint 16 0xcd + 4, // uint 32 0xce + 8, // uint 64 0xcf + 1, // int 8 0xd0 + 2, // int 16 0xd1 + 4, // int 32 0xd2 + 8, // int 64 0xd3 + 2, // fixext 1 0xd4 + 3, // fixext 2 0xd5 + 5, // fixext 4 0xd6 + 9, // fixext 8 0xd7 + 17,// fixext 16 0xd8 + 1, // str 8 0xd9 + 2, // str 16 0xda + 4, // str 32 0xdb + 2, // array 16 0xdc + 4, // array 32 0xdd + 2, // map 16 0xde + 4, // map 32 0xdf + }; + m_trail = trail[selector - 0xc4]; + m_cs = next_cs(m_current); + fixed_trail_again = true; + } else if(0xa0 <= selector && selector <= 0xbf) { // FixStr + m_trail = static_cast(*m_current) & 0x1f; + if(m_trail == 0) { + bool visret = holder().visitor().visit_str(n, static_cast(m_trail)); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } + else { + m_cs = MSGPACK_ACS_STR_VALUE; + fixed_trail_again = true; + } + } else if(0x90 <= selector && selector <= 0x9f) { // FixArray + parse_return ret = start_aggregate(array_sv(holder()), array_ev(holder()), m_current, off); + if (ret != PARSE_CONTINUE) return ret; + if (!holder().visitor().start_array_item()) return PARSE_STOP_VISITOR; + } else if(0x80 <= selector && selector <= 0x8f) { // FixMap + parse_return ret = start_aggregate(map_sv(holder()), map_ev(holder()), m_current, off); + if (ret != PARSE_CONTINUE) return ret; + if (!holder().visitor().start_map_key()) return PARSE_STOP_VISITOR; + } else if(selector == 0xc2) { // false + bool visret = holder().visitor().visit_boolean(false); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } else if(selector == 0xc3) { // true + bool visret = holder().visitor().visit_boolean(true); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } else if(selector == 0xc0) { // nil + bool visret = holder().visitor().visit_nil(); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } else { + off = m_current - m_start; + holder().visitor().parse_error(off - 1, off); + return PARSE_PARSE_ERROR; + } + // end MSGPACK_CS_HEADER + } + if (m_cs != MSGPACK_CS_HEADER || fixed_trail_again) { + if (fixed_trail_again) { + ++m_current; + fixed_trail_again = false; + } + if(static_cast(pe - m_current) < m_trail) { + off = m_current - m_start; + return PARSE_CONTINUE; + } + n = m_current; + m_current += m_trail - 1; + switch(m_cs) { + //case MSGPACK_CS_ + //case MSGPACK_CS_ + case MSGPACK_CS_FLOAT: { + union { uint32_t i; float f; } mem; + load(mem.i, n); + bool visret = holder().visitor().visit_float(mem.f); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_DOUBLE: { + union { uint64_t i; double f; } mem; + load(mem.i, n); +#if defined(TARGET_OS_IPHONE) + // ok +#elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi + // https://github.com/msgpack/msgpack-perl/pull/1 + mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL); +#endif + bool visret = holder().visitor().visit_float(mem.f); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_UINT_8: { + uint8_t tmp; + load(tmp, n); + bool visret = holder().visitor().visit_positive_integer(tmp); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_UINT_16: { + uint16_t tmp; + load(tmp, n); + bool visret = holder().visitor().visit_positive_integer(tmp); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_UINT_32: { + uint32_t tmp; + load(tmp, n); + bool visret = holder().visitor().visit_positive_integer(tmp); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_UINT_64: { + uint64_t tmp; + load(tmp, n); + bool visret = holder().visitor().visit_positive_integer(tmp); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_INT_8: { + int8_t tmp; + load(tmp, n); + bool visret = holder().visitor().visit_negative_integer(tmp); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_INT_16: { + int16_t tmp; + load(tmp, n); + bool visret = holder().visitor().visit_negative_integer(tmp); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_INT_32: { + int32_t tmp; + load(tmp, n); + bool visret = holder().visitor().visit_negative_integer(tmp); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_INT_64: { + int64_t tmp; + load(tmp, n); + bool visret = holder().visitor().visit_negative_integer(tmp); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_FIXEXT_1: { + bool visret = holder().visitor().visit_ext(n, 1+1); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_FIXEXT_2: { + bool visret = holder().visitor().visit_ext(n, 2+1); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_FIXEXT_4: { + bool visret = holder().visitor().visit_ext(n, 4+1); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_FIXEXT_8: { + bool visret = holder().visitor().visit_ext(n, 8+1); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_FIXEXT_16: { + bool visret = holder().visitor().visit_ext(n, 16+1); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_STR_8: { + uint8_t tmp; + load(tmp, n); + m_trail = tmp; + if(m_trail == 0) { + bool visret = holder().visitor().visit_str(n, static_cast(m_trail)); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } + else { + m_cs = MSGPACK_ACS_STR_VALUE; + fixed_trail_again = true; + } + } break; + case MSGPACK_CS_BIN_8: { + uint8_t tmp; + load(tmp, n); + m_trail = tmp; + if(m_trail == 0) { + bool visret = holder().visitor().visit_bin(n, static_cast(m_trail)); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } + else { + m_cs = MSGPACK_ACS_BIN_VALUE; + fixed_trail_again = true; + } + } break; + case MSGPACK_CS_EXT_8: { + uint8_t tmp; + load(tmp, n); + m_trail = tmp + 1; + if(m_trail == 0) { + bool visret = holder().visitor().visit_ext(n, m_trail); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } + else { + m_cs = MSGPACK_ACS_EXT_VALUE; + fixed_trail_again = true; + } + } break; + case MSGPACK_CS_STR_16: { + uint16_t tmp; + load(tmp, n); + m_trail = tmp; + if(m_trail == 0) { + bool visret = holder().visitor().visit_str(n, static_cast(m_trail)); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } + else { + m_cs = MSGPACK_ACS_STR_VALUE; + fixed_trail_again = true; + } + } break; + case MSGPACK_CS_BIN_16: { + uint16_t tmp; + load(tmp, n); + m_trail = tmp; + if(m_trail == 0) { + bool visret = holder().visitor().visit_bin(n, static_cast(m_trail)); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } + else { + m_cs = MSGPACK_ACS_BIN_VALUE; + fixed_trail_again = true; + } + } break; + case MSGPACK_CS_EXT_16: { + uint16_t tmp; + load(tmp, n); + m_trail = tmp + 1; + if(m_trail == 0) { + bool visret = holder().visitor().visit_ext(n, m_trail); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } + else { + m_cs = MSGPACK_ACS_EXT_VALUE; + fixed_trail_again = true; + } + } break; + case MSGPACK_CS_STR_32: { + uint32_t tmp; + load(tmp, n); + m_trail = tmp; + if(m_trail == 0) { + bool visret = holder().visitor().visit_str(n, static_cast(m_trail)); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } + else { + m_cs = MSGPACK_ACS_STR_VALUE; + fixed_trail_again = true; + } + } break; + case MSGPACK_CS_BIN_32: { + uint32_t tmp; + load(tmp, n); + m_trail = tmp; + if(m_trail == 0) { + bool visret = holder().visitor().visit_bin(n, static_cast(m_trail)); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } + else { + m_cs = MSGPACK_ACS_BIN_VALUE; + fixed_trail_again = true; + } + } break; + case MSGPACK_CS_EXT_32: { + uint32_t tmp; + load(tmp, n); + check_ext_size(tmp); + m_trail = tmp; + ++m_trail; + if(m_trail == 0) { + bool visret = holder().visitor().visit_ext(n, m_trail); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } + else { + m_cs = MSGPACK_ACS_EXT_VALUE; + fixed_trail_again = true; + } + } break; + case MSGPACK_ACS_STR_VALUE: { + bool visret = holder().visitor().visit_str(n, m_trail); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_ACS_BIN_VALUE: { + bool visret = holder().visitor().visit_bin(n, static_cast(m_trail)); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_ACS_EXT_VALUE: { + bool visret = holder().visitor().visit_ext(n, m_trail); + parse_return upr = after_visit_proc(visret, off); + if (upr != PARSE_CONTINUE) return upr; + } break; + case MSGPACK_CS_ARRAY_16: { + parse_return ret = start_aggregate(array_sv(holder()), array_ev(holder()), n, off); + if (ret != PARSE_CONTINUE) return ret; + if (!holder().visitor().start_array_item()) return PARSE_STOP_VISITOR; + + } break; + case MSGPACK_CS_ARRAY_32: { + parse_return ret = start_aggregate(array_sv(holder()), array_ev(holder()), n, off); + if (ret != PARSE_CONTINUE) return ret; + if (!holder().visitor().start_array_item()) return PARSE_STOP_VISITOR; + } break; + case MSGPACK_CS_MAP_16: { + parse_return ret = start_aggregate(map_sv(holder()), map_ev(holder()), n, off); + if (ret != PARSE_CONTINUE) return ret; + if (!holder().visitor().start_map_key()) return PARSE_STOP_VISITOR; + } break; + case MSGPACK_CS_MAP_32: { + parse_return ret = start_aggregate(map_sv(holder()), map_ev(holder()), n, off); + if (ret != PARSE_CONTINUE) return ret; + if (!holder().visitor().start_map_key()) return PARSE_STOP_VISITOR; + } break; + default: + off = m_current - m_start; + holder().visitor().parse_error(n - m_start - 1, n - m_start); + return PARSE_PARSE_ERROR; + } + } + } while(m_current != pe); + + off = m_current - m_start; + return PARSE_CONTINUE; +} + +} // detail + + +/// Parsing class for a stream deserialization. + +template +class parser : public detail::context { + typedef parser this_type; + typedef detail::context context_type; +public: + /// Constructor + /** + * @param referenced If the unpacked object contains reference of the buffer, then set as true, otherwise false. + * @param f A judging function that msgpack::object refer to the buffer. + * @param user_data This parameter is passed to f. + * @param initial_buffer_size The memory size to allocate when unpacker is constructed. + * @param limit The size limit information of msgpack::object. + * + */ + parser(ReferencedBufferHook& hook, + std::size_t initial_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE); + +#if !defined(MSGPACK_USE_CPP03) + parser(this_type&& other); + this_type& operator=(this_type&& other); +#endif // !defined(MSGPACK_USE_CPP03) + + ~parser(); + +public: + /// Reserve a buffer memory. + /** + * @param size The size of allocating memory. + * + * After returning this function, buffer_capacity() returns at least 'size'. + * See: + * https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_unpacker#msgpack-controls-a-buffer + */ + void reserve_buffer(std::size_t size = MSGPACK_UNPACKER_RESERVE_SIZE); + + /// Get buffer pointer. + /** + * You need to care about the memory is enable between buffer() and buffer() + buffer_capacity() + * See: + * https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_unpacker#msgpack-controls-a-buffer + */ + char* buffer(); + + /// Get buffer capacity. + /** + * @return The memory size that you can write. + * + * See: + * https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_unpacker#msgpack-controls-a-buffer + */ + std::size_t buffer_capacity() const; + + /// Notify a buffer consumed information to msgpack::unpacker. + /** + * @param size The size of memory that you consumed. + * + * After copying the data to the memory that is pointed by buffer(), you need to call the + * function to notify how many bytes are consumed. Then you can call next() functions. + * + * See: + * https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_unpacker#msgpack-controls-a-buffer + */ + void buffer_consumed(std::size_t size); + + /// Unpack one msgpack::object. + /** + * + * + * @return If one msgpack::object is unpacked, then return true, if msgpack::object is incomplete + * and additional data is required, then return false. If data format is invalid, throw + * msgpack::parse_error. + * + * See: + * https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_unpacker#msgpack-controls-a-buffer + */ + bool next(); + + /// Get message size. + /** + * @return Returns parsed_size() + nonparsed_size() + */ + std::size_t message_size() const; + +public: + /// Get parsed message size. + /** + * @return Parsed message size. + * + * This function is usable when non-MessagePack message follows after + * MessagePack message. + */ + std::size_t parsed_size() const; + + /// Get the address that is not parsed in the buffer. + /** + * @return Address of the buffer that is not parsed + * + * This function is usable when non-MessagePack message follows after + * MessagePack message. + */ + char* nonparsed_buffer(); + + /// Get the size of the buffer that is not parsed. + /** + * @return Size of the buffer that is not parsed + * + * This function is usable when non-MessagePack message follows after + * MessagePack message. + */ + std::size_t nonparsed_size() const; + + /// Skip the specified size of non-parsed buffer. + /** + * @param size to skip + * + * Note that the `size' argument must be smaller than nonparsed_size(). + * This function is usable when non-MessagePack message follows after + * MessagePack message. + */ + void skip_nonparsed_buffer(std::size_t size); + + /// Remove nonparsed buffer and reset the current position as a new start point. + /** + * This function is usable when non-MessagePack message follows after + * MessagePack message. + */ + void remove_nonparsed_buffer(); + + void reset(); + +protected: + char* get_raw_buffer() { + return m_buffer; + } +private: + void expand_buffer(std::size_t size); + parse_return execute_imp(); + +private: + char* m_buffer; + std::size_t m_used; + std::size_t m_free; + std::size_t m_off; + std::size_t m_parsed; + std::size_t m_initial_buffer_size; + ReferencedBufferHook& m_referenced_buffer_hook; + +#if defined(MSGPACK_USE_CPP03) +private: + parser(const this_type&); + this_type& operator=(const this_type&); +#else // defined(MSGPACK_USE_CPP03) +public: + parser(const this_type&) = delete; + this_type& operator=(const this_type&) = delete; +#endif // defined(MSGPACK_USE_CPP03) +}; + +template +inline parser::parser( + ReferencedBufferHook& hook, + std::size_t initial_buffer_size) + :m_referenced_buffer_hook(hook) +{ + if(initial_buffer_size < COUNTER_SIZE) { + initial_buffer_size = COUNTER_SIZE; + } + + char* buffer = static_cast(::malloc(initial_buffer_size)); + if(!buffer) { + throw std::bad_alloc(); + } + + m_buffer = buffer; + m_used = COUNTER_SIZE; + m_free = initial_buffer_size - m_used; + m_off = COUNTER_SIZE; + m_parsed = 0; + m_initial_buffer_size = initial_buffer_size; + + detail::init_count(m_buffer); +} + +#if !defined(MSGPACK_USE_CPP03) +// Move constructor and move assignment operator + +template +inline parser::parser(this_type&& other) + :context_type(std::move(other)), + m_buffer(other.m_buffer), + m_used(other.m_used), + m_free(other.m_free), + m_off(other.m_off), + m_parsed(other.m_parsed), + m_initial_buffer_size(other.m_initial_buffer_size), + m_referenced_buffer_hook(other.m_referenced_buffer_hook) { + other.m_buffer = MSGPACK_NULLPTR; + other.m_used = 0; + other.m_free = 0; + other.m_off = 0; + other.m_parsed = 0; +} + +template +inline parser& parser::operator=(this_type&& other) { + this->~parser(); + new (this) this_type(std::move(other)); + return *this; +} + +#endif // !defined(MSGPACK_USE_CPP03) + + +template +inline parser::~parser() +{ + // These checks are required for move operations. + if (m_buffer) detail::decr_count(m_buffer); +} + + +template +inline void parser::reserve_buffer(std::size_t size) +{ + if(m_free >= size) return; + expand_buffer(size); +} + +template +inline void parser::expand_buffer(std::size_t size) +{ + if(m_used == m_off && detail::get_count(m_buffer) == 1 + && static_cast(*this).visitor().referenced()) { + // rewind buffer + m_free += m_used - COUNTER_SIZE; + m_used = COUNTER_SIZE; + m_off = COUNTER_SIZE; + + if(m_free >= size) return; + } + + if(m_off == COUNTER_SIZE) { + std::size_t next_size = (m_used + m_free) * 2; // include COUNTER_SIZE + while(next_size < size + m_used) { + std::size_t tmp_next_size = next_size * 2; + if (tmp_next_size <= next_size) { + next_size = size + m_used; + break; + } + next_size = tmp_next_size; + } + + char* tmp = static_cast(::realloc(m_buffer, next_size)); + if(!tmp) { + throw std::bad_alloc(); + } + + m_buffer = tmp; + m_free = next_size - m_used; + + } else { + std::size_t next_size = m_initial_buffer_size; // include COUNTER_SIZE + std::size_t not_parsed = m_used - m_off; + while(next_size < size + not_parsed + COUNTER_SIZE) { + std::size_t tmp_next_size = next_size * 2; + if (tmp_next_size <= next_size) { + next_size = size + not_parsed + COUNTER_SIZE; + break; + } + next_size = tmp_next_size; + } + + char* tmp = static_cast(::malloc(next_size)); + if(!tmp) { + throw std::bad_alloc(); + } + + detail::init_count(tmp); + + std::memcpy(tmp+COUNTER_SIZE, m_buffer + m_off, not_parsed); + + if(static_cast(*this).referenced()) { + try { + m_referenced_buffer_hook(m_buffer); + } + catch (...) { + ::free(tmp); + throw; + } + static_cast(*this).set_referenced(false); + } else { + detail::decr_count(m_buffer); + } + + m_buffer = tmp; + m_used = not_parsed + COUNTER_SIZE; + m_free = next_size - m_used; + m_off = COUNTER_SIZE; + } +} + +template +inline char* parser::buffer() +{ + return m_buffer + m_used; +} + +template +inline std::size_t parser::buffer_capacity() const +{ + return m_free; +} + +template +inline void parser::buffer_consumed(std::size_t size) +{ + m_used += size; + m_free -= size; +} + +template + inline bool parser::next() +{ + parse_return ret = execute_imp(); + return ret == PARSE_SUCCESS; +} + +template +inline parse_return parser::execute_imp() +{ + std::size_t off = m_off; + parse_return ret = context_type::execute(m_buffer, m_used, m_off); + if(m_off > off) { + m_parsed += m_off - off; + } + return ret; +} + +template +inline void parser::reset() +{ + context_type::init(); + // don't reset referenced flag + m_parsed = 0; +} + +template +inline std::size_t parser::message_size() const +{ + return m_parsed - m_off + m_used; +} + +template +inline std::size_t parser::parsed_size() const +{ + return m_parsed; +} + +template +inline char* parser::nonparsed_buffer() +{ + return m_buffer + m_off; +} + +template +inline std::size_t parser::nonparsed_size() const +{ + return m_used - m_off; +} + +template +inline void parser::skip_nonparsed_buffer(std::size_t size) +{ + m_off += size; +} + +template +inline void parser::remove_nonparsed_buffer() +{ + m_used = m_off; +} + +template +inline bool parse(const char* data, size_t len, size_t& off, Visitor& v) { + parse_return ret = detail::parse_imp(data, len, off, v); + return ret == PARSE_SUCCESS || ret == PARSE_EXTRA_BYTES; +} + +template +inline bool parse(const char* data, size_t len, Visitor& v) { + std::size_t off = 0; + return parse(data, len, off, v); +} + +namespace detail { + +template +struct parse_helper : context > { + parse_helper(Visitor& v):m_visitor(v) {} + parse_return execute(const char* data, std::size_t len, std::size_t& off) { + return context >::execute(data, len, off); + } + Visitor& visitor() const { return m_visitor; } + Visitor& m_visitor; +}; + +template +inline parse_return +parse_imp(const char* data, size_t len, size_t& off, Visitor& v) { + std::size_t noff = off; + + if(len <= noff) { + // FIXME + v.insufficient_bytes(noff, noff); + return PARSE_CONTINUE; + } + detail::parse_helper h(v); + parse_return ret = h.execute(data, len, noff); + switch (ret) { + case PARSE_CONTINUE: + off = noff; + v.insufficient_bytes(noff - 1, noff); + return ret; + case PARSE_SUCCESS: + off = noff; + if(noff < len) { + return PARSE_EXTRA_BYTES; + } + return ret; + default: + return ret; + } +} + +} // detail + + +/// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v2) +/// @endcond + +} // namespace msgpack + + +#endif // MSGPACK_V2_PARSE_HPP diff --git a/include/msgpack/v2/parse_return.hpp b/include/msgpack/v2/parse_return.hpp new file mode 100644 index 00000000..9acd73e7 --- /dev/null +++ b/include/msgpack/v2/parse_return.hpp @@ -0,0 +1,37 @@ +// +// MessagePack for C++ deserializing routine +// +// Copyright (C) 2008-2016 FURUHASHI Sadayuki and KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#ifndef MSGPACK_V2_PARSE_RETURN_HPP +#define MSGPACK_V2_PARSE_RETURN_HPP + +#include "msgpack/v1/parse_return.hpp" + +namespace msgpack { + +/// @cond +MSGPACK_API_VERSION_NAMESPACE(v2) { +/// @endcond + + +// for internal use +typedef enum { + PARSE_SUCCESS = v1::PARSE_SUCCESS, + PARSE_EXTRA_BYTES = v1::PARSE_EXTRA_BYTES, + PARSE_CONTINUE = v1::PARSE_CONTINUE, + PARSE_PARSE_ERROR = v1::PARSE_PARSE_ERROR, + PARSE_STOP_VISITOR = -2 +} parse_return; + +/// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v2) +/// @endcond + +} // namespace msgpack + +#endif // MSGPACK_V2_PARSE_RETURN_HPP diff --git a/include/msgpack/v2/unpack.hpp b/include/msgpack/v2/unpack.hpp index bef9ec1f..c2820079 100644 --- a/include/msgpack/v2/unpack.hpp +++ b/include/msgpack/v2/unpack.hpp @@ -11,6 +11,8 @@ #define MSGPACK_V2_UNPACK_HPP #include "msgpack/unpack_decl.hpp" +#include "msgpack/v2/create_object_visitor.hpp" +#include "msgpack/v2/parse.hpp" namespace msgpack { @@ -18,1247 +20,6 @@ namespace msgpack { MSGPACK_API_VERSION_NAMESPACE(v2) { /// @endcond -struct null_visitor { - bool visit_nil() { - return true; - } - bool visit_boolean(bool /*v*/) { - return true; - } - bool visit_positive_integer(uint64_t /*v*/) { - return true; - } - bool visit_negative_integer(int64_t /*v*/) { - return true; - } - bool visit_float(double /*v*/) { - return true; - } - bool visit_str(const char* /*v*/, uint32_t /*size*/) { - return true; - } - bool visit_bin(const char* /*v*/, uint32_t /*size*/) { - return true; - } - bool visit_ext(const char* /*v*/, uint32_t /*size*/) { - return true; - } - bool start_array(uint32_t /*num_elements*/) { - return true; - } - bool start_array_item() { - return true; - } - bool end_array_item() { - return true; - } - bool end_array() { - return true; - } - bool start_map(uint32_t /*num_kv_pairs*/) { - return true; - } - bool start_map_key() { - return true; - } - bool end_map_key() { - return true; - } - bool start_map_value() { - return true; - } - bool end_map_value() { - return true; - } - bool end_map() { - return true; - } - void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) { - } - void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) { - } -}; - -namespace detail { - -class create_object_visitor { -public: - create_object_visitor(unpack_reference_func f, void* user_data, unpack_limit const& limit) - :m_func(f), m_user_data(user_data), m_limit(limit) { - m_stack.reserve(MSGPACK_EMBED_STACK_SIZE); - m_stack.push_back(&m_obj); - } - -#if !defined(MSGPACK_USE_CPP03) - create_object_visitor(create_object_visitor&& other) - :m_func(other.m_func), - m_user_data(other.m_user_data), - m_limit(std::move(other.m_limit)), - m_stack(std::move(other.m_stack)), - m_zone(other.m_zone), - m_referenced(other.m_referenced) { - other.m_zone = MSGPACK_NULLPTR; - m_stack[0] = &m_obj; - } - create_object_visitor& operator=(create_object_visitor&& other) { - this->~create_object_visitor(); - new (this) create_object_visitor(std::move(other)); - return *this; - } -#endif // !defined(MSGPACK_USE_CPP03) - - void init() { - m_stack.resize(1); - m_obj = msgpack::object(); - m_stack[0] = &m_obj; - } - msgpack::object const& data() const - { - return m_obj; - } - msgpack::zone const& zone() const { return *m_zone; } - msgpack::zone& zone() { return *m_zone; } - void set_zone(msgpack::zone& zone) { m_zone = &zone; } - bool referenced() const { return m_referenced; } - void set_referenced(bool referenced) { m_referenced = referenced; } - // visit functions - bool visit_nil() { - msgpack::object* obj = m_stack.back(); - obj->type = msgpack::type::NIL; - return true; - } - bool visit_boolean(bool v) { - msgpack::object* obj = m_stack.back(); - obj->type = msgpack::type::BOOLEAN; - obj->via.boolean = v; - return true; - } - bool visit_positive_integer(uint64_t v) { - msgpack::object* obj = m_stack.back(); - obj->type = msgpack::type::POSITIVE_INTEGER; - obj->via.u64 = v; - return true; - } - bool visit_negative_integer(int64_t v) { - msgpack::object* obj = m_stack.back(); - if(v >= 0) { - obj->type = msgpack::type::POSITIVE_INTEGER; - obj->via.u64 = v; - } - else { - obj->type = msgpack::type::NEGATIVE_INTEGER; - obj->via.i64 = v; - } - return true; - } - bool visit_float(double v) { - msgpack::object* obj = m_stack.back(); - obj->type = msgpack::type::FLOAT; - obj->via.f64 = v; - return true; - } - bool visit_str(const char* v, uint32_t size) { - if (size > m_limit.str()) throw msgpack::str_size_overflow("str size overflow"); - msgpack::object* obj = m_stack.back(); - obj->type = msgpack::type::STR; - if (m_func && m_func(obj->type, size, m_user_data)) { - obj->via.str.ptr = v; - set_referenced(true); - } - else { - char* tmp = static_cast(zone().allocate_align(size)); - std::memcpy(tmp, v, size); - obj->via.str.ptr = tmp; - } - obj->via.str.size = size; - return true; - } - bool visit_bin(const char* v, uint32_t size) { - if (size > m_limit.bin()) throw msgpack::bin_size_overflow("bin size overflow"); - msgpack::object* obj = m_stack.back(); - obj->type = msgpack::type::BIN; - if (m_func && m_func(obj->type, size, m_user_data)) { - obj->via.bin.ptr = v; - set_referenced(true); - } - else { - char* tmp = static_cast(zone().allocate_align(size)); - std::memcpy(tmp, v, size); - obj->via.bin.ptr = tmp; - } - obj->via.bin.size = size; - return true; - } - bool visit_ext(const char* v, uint32_t size) { - if (size > m_limit.ext()) throw msgpack::ext_size_overflow("ext size overflow"); - msgpack::object* obj = m_stack.back(); - obj->type = msgpack::type::EXT; - if (m_func && m_func(obj->type, size, m_user_data)) { - obj->via.ext.ptr = v; - set_referenced(true); - } - else { - char* tmp = static_cast(zone().allocate_align(size)); - std::memcpy(tmp, v, size); - obj->via.ext.ptr = tmp; - } - obj->via.ext.size = static_cast(size - 1); - return true; - } - bool start_array(uint32_t num_elements) { - if (num_elements > m_limit.array()) throw msgpack::array_size_overflow("array size overflow"); - if (m_stack.size() > m_limit.depth()) throw msgpack::depth_size_overflow("depth size overflow"); - msgpack::object* obj = m_stack.back(); - obj->type = msgpack::type::ARRAY; - obj->via.array.size = num_elements; - if (num_elements == 0) { - obj->via.array.ptr = MSGPACK_NULLPTR; - } - else { - size_t size = num_elements*sizeof(msgpack::object); - if (size / sizeof(msgpack::object) != num_elements) { - throw msgpack::array_size_overflow("array size overflow"); - } - obj->via.array.ptr = - static_cast(m_zone->allocate_align(size)); - } - m_stack.push_back(obj->via.array.ptr); - return true; - } - bool start_array_item() { - return true; - } - bool end_array_item() { - ++m_stack.back(); - return true; - } - bool end_array() { - m_stack.pop_back(); - return true; - } - bool start_map(uint32_t num_kv_pairs) { - if (num_kv_pairs > m_limit.map()) throw msgpack::map_size_overflow("map size overflow"); - if (m_stack.size() > m_limit.depth()) throw msgpack::depth_size_overflow("depth size overflow"); - msgpack::object* obj = m_stack.back(); - obj->type = msgpack::type::MAP; - obj->via.map.size = num_kv_pairs; - if (num_kv_pairs == 0) { - obj->via.map.ptr = MSGPACK_NULLPTR; - } - else { - size_t size = num_kv_pairs*sizeof(msgpack::object_kv); - if (size / sizeof(msgpack::object_kv) != num_kv_pairs) { - throw msgpack::map_size_overflow("map size overflow"); - } - obj->via.map.ptr = - static_cast(m_zone->allocate_align(size)); - } - m_stack.push_back(reinterpret_cast(obj->via.map.ptr)); - return true; - } - bool start_map_key() { - return true; - } - bool end_map_key() { - ++m_stack.back(); - return true; - } - bool start_map_value() { - return true; - } - bool end_map_value() { - ++m_stack.back(); - return true; - } - bool end_map() { - m_stack.pop_back(); - return true; - } - void parse_error(size_t /*parsed_offset*/, size_t /*error_offset*/) { - throw msgpack::parse_error("parse error"); - } - void insufficient_bytes(size_t /*parsed_offset*/, size_t /*error_offset*/) { - throw msgpack::insufficient_bytes("insufficient bytes"); - } -private: -public: - unpack_reference_func m_func; - void* m_user_data; - unpack_limit m_limit; - msgpack::object m_obj; - std::vector m_stack; - msgpack::zone* m_zone; - bool m_referenced; -}; - -template -class context { -public: - context() - :m_trail(0), m_cs(MSGPACK_CS_HEADER) - { - } - - void init() - { - m_cs = MSGPACK_CS_HEADER; - m_trail = 0; - m_stack.clear(); - holder().visitor().init(); - } - - unpack_return execute(const char* data, std::size_t len, std::size_t& off); - -private: - template - static uint32_t next_cs(T p) - { - return static_cast(*p) & 0x1f; - } - - VisitorHolder& holder() { - return static_cast(*this); - } - - template - unpack_return start_aggregate( - StartVisitor const& sv, - EndVisitor const& ev, - const char* load_pos, - std::size_t& off) { - typename value::type size; - load(size, load_pos); - ++m_current; - if (size == 0) { - if (!sv(size)) { - off = m_current - m_start; - return UNPACK_STOP_VISITOR; - } - if (!ev()) { - off = m_current - m_start; - return UNPACK_STOP_VISITOR; - } - unpack_return ret = m_stack.consume(holder()); - if (ret != UNPACK_CONTINUE) { - off = m_current - m_start; - return ret; - } - } - else { - m_stack.push(sv.type(), static_cast(size)); - if (!sv(size)) { - off = m_current - m_start; - return UNPACK_STOP_VISITOR; - } - } - m_cs = MSGPACK_CS_HEADER; - return UNPACK_CONTINUE; - } - - unpack_return after_visit_proc(bool visit_result, std::size_t& off) { - ++m_current; - if (!visit_result) { - off = m_current - m_start; - return UNPACK_STOP_VISITOR; - } - unpack_return ret = m_stack.consume(holder()); - if (ret == UNPACK_CONTINUE) { - m_cs = MSGPACK_CS_HEADER; - } - else { - off = m_current - m_start; - } - return ret; - } - - struct array_sv { - array_sv(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {} - bool operator()(uint32_t size) const { - return m_visitor_holder.visitor().start_array(size); - } - msgpack_container_type type() const { return MSGPACK_CT_ARRAY_ITEM; } - private: - VisitorHolder& m_visitor_holder; - }; - struct array_ev { - array_ev(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {} - bool operator()() const { - return m_visitor_holder.visitor().end_array(); - } - private: - VisitorHolder& m_visitor_holder; - }; - struct map_sv { - map_sv(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {} - bool operator()(uint32_t size) const { - return m_visitor_holder.visitor().start_map(size); - } - msgpack_container_type type() const { return MSGPACK_CT_MAP_KEY; } - private: - VisitorHolder& m_visitor_holder; - }; - struct map_ev { - map_ev(VisitorHolder& visitor_holder):m_visitor_holder(visitor_holder) {} - bool operator()() const { - return m_visitor_holder.visitor().end_map(); - } - private: - VisitorHolder& m_visitor_holder; - }; - - struct unpack_stack { - struct stack_elem { - stack_elem(msgpack_container_type type, uint32_t rest):m_type(type), m_rest(rest) {} - msgpack_container_type m_type; - uint32_t m_rest; - }; - unpack_stack() { - m_stack.reserve(MSGPACK_EMBED_STACK_SIZE); - } - void push(msgpack_container_type type, uint32_t rest) { - m_stack.push_back(stack_elem(type, rest)); - } - unpack_return consume(VisitorHolder& visitor_holder) { - while (!m_stack.empty()) { - stack_elem& e = m_stack.back(); - switch (e.m_type) { - case MSGPACK_CT_ARRAY_ITEM: - if (!visitor_holder.visitor().end_array_item()) return UNPACK_STOP_VISITOR; - if (--e.m_rest == 0) { - m_stack.pop_back(); - if (!visitor_holder.visitor().end_array()) return UNPACK_STOP_VISITOR; - } - else { - if (!visitor_holder.visitor().start_array_item()) return UNPACK_STOP_VISITOR; - return UNPACK_CONTINUE; - } - break; - case MSGPACK_CT_MAP_KEY: - if (!visitor_holder.visitor().end_map_key()) return UNPACK_STOP_VISITOR; - if (!visitor_holder.visitor().start_map_value()) return UNPACK_STOP_VISITOR; - e.m_type = MSGPACK_CT_MAP_VALUE; - return UNPACK_CONTINUE; - case MSGPACK_CT_MAP_VALUE: - if (!visitor_holder.visitor().end_map_value()) return UNPACK_STOP_VISITOR; - if (--e.m_rest == 0) { - m_stack.pop_back(); - if (!visitor_holder.visitor().end_map()) return UNPACK_STOP_VISITOR; - } - else { - e.m_type = MSGPACK_CT_MAP_KEY; - if (!visitor_holder.visitor().start_map_key()) return UNPACK_STOP_VISITOR; - return UNPACK_CONTINUE; - } - break; - } - } - return UNPACK_SUCCESS; - } - bool empty() const { return m_stack.empty(); } - void clear() { m_stack.clear(); } - private: - std::vector m_stack; - }; - - char const* m_start; - char const* m_current; - - std::size_t m_trail; - uint32_t m_cs; - uint32_t m_num_elements; - unpack_stack m_stack; -}; - -template -inline void check_ext_size(std::size_t /*size*/) { -} - -template <> -inline void check_ext_size<4>(std::size_t size) { - if (size == 0xffffffff) throw msgpack::ext_size_overflow("ext size overflow"); -} - -template -inline unpack_return context::execute(const char* data, std::size_t len, std::size_t& off) -{ - assert(len >= off); - - m_start = data; - m_current = data + off; - const char* const pe = data + len; - const char* n = MSGPACK_NULLPTR; - - msgpack::object obj; - - if(m_current == pe) { - off = m_current - m_start; - return UNPACK_CONTINUE; - } - bool fixed_trail_again = false; - do { - if (m_cs == MSGPACK_CS_HEADER) { - fixed_trail_again = false; - int selector = *reinterpret_cast(m_current); - if (0x00 <= selector && selector <= 0x7f) { // Positive Fixnum - uint8_t tmp = *reinterpret_cast(m_current); - bool visret = holder().visitor().visit_positive_integer(tmp); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } else if(0xe0 <= selector && selector <= 0xff) { // Negative Fixnum - int8_t tmp = *reinterpret_cast(m_current); - bool visret = holder().visitor().visit_negative_integer(tmp); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } else if (0xc4 <= selector && selector <= 0xdf) { - const uint32_t trail[] = { - 1, // bin 8 0xc4 - 2, // bin 16 0xc5 - 4, // bin 32 0xc6 - 1, // ext 8 0xc7 - 2, // ext 16 0xc8 - 4, // ext 32 0xc9 - 4, // float 32 0xca - 8, // float 64 0xcb - 1, // uint 8 0xcc - 2, // uint 16 0xcd - 4, // uint 32 0xce - 8, // uint 64 0xcf - 1, // int 8 0xd0 - 2, // int 16 0xd1 - 4, // int 32 0xd2 - 8, // int 64 0xd3 - 2, // fixext 1 0xd4 - 3, // fixext 2 0xd5 - 5, // fixext 4 0xd6 - 9, // fixext 8 0xd7 - 17,// fixext 16 0xd8 - 1, // str 8 0xd9 - 2, // str 16 0xda - 4, // str 32 0xdb - 2, // array 16 0xdc - 4, // array 32 0xdd - 2, // map 16 0xde - 4, // map 32 0xdf - }; - m_trail = trail[selector - 0xc4]; - m_cs = next_cs(m_current); - fixed_trail_again = true; - } else if(0xa0 <= selector && selector <= 0xbf) { // FixStr - m_trail = static_cast(*m_current) & 0x1f; - if(m_trail == 0) { - bool visret = holder().visitor().visit_str(n, static_cast(m_trail)); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } - else { - m_cs = MSGPACK_ACS_STR_VALUE; - fixed_trail_again = true; - } - } else if(0x90 <= selector && selector <= 0x9f) { // FixArray - unpack_return ret = start_aggregate(array_sv(holder()), array_ev(holder()), m_current, off); - if (ret != UNPACK_CONTINUE) return ret; - if (!holder().visitor().start_array_item()) return UNPACK_STOP_VISITOR; - } else if(0x80 <= selector && selector <= 0x8f) { // FixMap - unpack_return ret = start_aggregate(map_sv(holder()), map_ev(holder()), m_current, off); - if (ret != UNPACK_CONTINUE) return ret; - if (!holder().visitor().start_map_key()) return UNPACK_STOP_VISITOR; - } else if(selector == 0xc2) { // false - bool visret = holder().visitor().visit_boolean(false); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } else if(selector == 0xc3) { // true - bool visret = holder().visitor().visit_boolean(true); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } else if(selector == 0xc0) { // nil - bool visret = holder().visitor().visit_nil(); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } else { - off = m_current - m_start; - holder().visitor().parse_error(off - 1, off); - return UNPACK_PARSE_ERROR; - } - // end MSGPACK_CS_HEADER - } - if (m_cs != MSGPACK_CS_HEADER || fixed_trail_again) { - if (fixed_trail_again) { - ++m_current; - fixed_trail_again = false; - } - if(static_cast(pe - m_current) < m_trail) { - off = m_current - m_start; - return UNPACK_CONTINUE; - } - n = m_current; - m_current += m_trail - 1; - switch(m_cs) { - //case MSGPACK_CS_ - //case MSGPACK_CS_ - case MSGPACK_CS_FLOAT: { - union { uint32_t i; float f; } mem; - load(mem.i, n); - bool visret = holder().visitor().visit_float(mem.f); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_DOUBLE: { - union { uint64_t i; double f; } mem; - load(mem.i, n); -#if defined(TARGET_OS_IPHONE) - // ok -#elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi - // https://github.com/msgpack/msgpack-perl/pull/1 - mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL); -#endif - bool visret = holder().visitor().visit_float(mem.f); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_UINT_8: { - uint8_t tmp; - load(tmp, n); - bool visret = holder().visitor().visit_positive_integer(tmp); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_UINT_16: { - uint16_t tmp; - load(tmp, n); - bool visret = holder().visitor().visit_positive_integer(tmp); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_UINT_32: { - uint32_t tmp; - load(tmp, n); - bool visret = holder().visitor().visit_positive_integer(tmp); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_UINT_64: { - uint64_t tmp; - load(tmp, n); - bool visret = holder().visitor().visit_positive_integer(tmp); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_INT_8: { - int8_t tmp; - load(tmp, n); - bool visret = holder().visitor().visit_negative_integer(tmp); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_INT_16: { - int16_t tmp; - load(tmp, n); - bool visret = holder().visitor().visit_negative_integer(tmp); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_INT_32: { - int32_t tmp; - load(tmp, n); - bool visret = holder().visitor().visit_negative_integer(tmp); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_INT_64: { - int64_t tmp; - load(tmp, n); - bool visret = holder().visitor().visit_negative_integer(tmp); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_FIXEXT_1: { - bool visret = holder().visitor().visit_ext(n, 1+1); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_FIXEXT_2: { - bool visret = holder().visitor().visit_ext(n, 2+1); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_FIXEXT_4: { - bool visret = holder().visitor().visit_ext(n, 4+1); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_FIXEXT_8: { - bool visret = holder().visitor().visit_ext(n, 8+1); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_FIXEXT_16: { - bool visret = holder().visitor().visit_ext(n, 16+1); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_STR_8: { - uint8_t tmp; - load(tmp, n); - m_trail = tmp; - if(m_trail == 0) { - bool visret = holder().visitor().visit_str(n, static_cast(m_trail)); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } - else { - m_cs = MSGPACK_ACS_STR_VALUE; - fixed_trail_again = true; - } - } break; - case MSGPACK_CS_BIN_8: { - uint8_t tmp; - load(tmp, n); - m_trail = tmp; - if(m_trail == 0) { - bool visret = holder().visitor().visit_bin(n, static_cast(m_trail)); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } - else { - m_cs = MSGPACK_ACS_BIN_VALUE; - fixed_trail_again = true; - } - } break; - case MSGPACK_CS_EXT_8: { - uint8_t tmp; - load(tmp, n); - m_trail = tmp + 1; - if(m_trail == 0) { - bool visret = holder().visitor().visit_ext(n, m_trail); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } - else { - m_cs = MSGPACK_ACS_EXT_VALUE; - fixed_trail_again = true; - } - } break; - case MSGPACK_CS_STR_16: { - uint16_t tmp; - load(tmp, n); - m_trail = tmp; - if(m_trail == 0) { - bool visret = holder().visitor().visit_str(n, static_cast(m_trail)); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } - else { - m_cs = MSGPACK_ACS_STR_VALUE; - fixed_trail_again = true; - } - } break; - case MSGPACK_CS_BIN_16: { - uint16_t tmp; - load(tmp, n); - m_trail = tmp; - if(m_trail == 0) { - bool visret = holder().visitor().visit_bin(n, static_cast(m_trail)); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } - else { - m_cs = MSGPACK_ACS_BIN_VALUE; - fixed_trail_again = true; - } - } break; - case MSGPACK_CS_EXT_16: { - uint16_t tmp; - load(tmp, n); - m_trail = tmp + 1; - if(m_trail == 0) { - bool visret = holder().visitor().visit_ext(n, m_trail); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } - else { - m_cs = MSGPACK_ACS_EXT_VALUE; - fixed_trail_again = true; - } - } break; - case MSGPACK_CS_STR_32: { - uint32_t tmp; - load(tmp, n); - m_trail = tmp; - if(m_trail == 0) { - bool visret = holder().visitor().visit_str(n, static_cast(m_trail)); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } - else { - m_cs = MSGPACK_ACS_STR_VALUE; - fixed_trail_again = true; - } - } break; - case MSGPACK_CS_BIN_32: { - uint32_t tmp; - load(tmp, n); - m_trail = tmp; - if(m_trail == 0) { - bool visret = holder().visitor().visit_bin(n, static_cast(m_trail)); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } - else { - m_cs = MSGPACK_ACS_BIN_VALUE; - fixed_trail_again = true; - } - } break; - case MSGPACK_CS_EXT_32: { - uint32_t tmp; - load(tmp, n); - check_ext_size(tmp); - m_trail = tmp; - ++m_trail; - if(m_trail == 0) { - bool visret = holder().visitor().visit_ext(n, m_trail); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } - else { - m_cs = MSGPACK_ACS_EXT_VALUE; - fixed_trail_again = true; - } - } break; - case MSGPACK_ACS_STR_VALUE: { - bool visret = holder().visitor().visit_str(n, m_trail); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_ACS_BIN_VALUE: { - bool visret = holder().visitor().visit_bin(n, static_cast(m_trail)); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_ACS_EXT_VALUE: { - bool visret = holder().visitor().visit_ext(n, m_trail); - unpack_return upr = after_visit_proc(visret, off); - if (upr != UNPACK_CONTINUE) return upr; - } break; - case MSGPACK_CS_ARRAY_16: { - unpack_return ret = start_aggregate(array_sv(holder()), array_ev(holder()), n, off); - if (ret != UNPACK_CONTINUE) return ret; - if (!holder().visitor().start_array_item()) return UNPACK_STOP_VISITOR; - - } break; - case MSGPACK_CS_ARRAY_32: { - unpack_return ret = start_aggregate(array_sv(holder()), array_ev(holder()), n, off); - if (ret != UNPACK_CONTINUE) return ret; - if (!holder().visitor().start_array_item()) return UNPACK_STOP_VISITOR; - } break; - case MSGPACK_CS_MAP_16: { - unpack_return ret = start_aggregate(map_sv(holder()), map_ev(holder()), n, off); - if (ret != UNPACK_CONTINUE) return ret; - if (!holder().visitor().start_map_key()) return UNPACK_STOP_VISITOR; - } break; - case MSGPACK_CS_MAP_32: { - unpack_return ret = start_aggregate(map_sv(holder()), map_ev(holder()), n, off); - if (ret != UNPACK_CONTINUE) return ret; - if (!holder().visitor().start_map_key()) return UNPACK_STOP_VISITOR; - } break; - default: - off = m_current - m_start; - holder().visitor().parse_error(n - m_start - 1, n - m_start); - return UNPACK_PARSE_ERROR; - } - } - } while(m_current != pe); - - off = m_current - m_start; - return UNPACK_CONTINUE; -} - -} // detail - - -/// Unpacking class for a stream deserialization. - -template -class parser : public detail::context { - typedef parser this_type; - typedef detail::context context_type; -public: - /// Constructor - /** - * @param referenced If the unpacked object contains reference of the buffer, then set as true, otherwise false. - * @param f A judging function that msgpack::object refer to the buffer. - * @param user_data This parameter is passed to f. - * @param initial_buffer_size The memory size to allocate when unpacker is constructed. - * @param limit The size limit information of msgpack::object. - * - */ - parser(ReferencedBufferHook& hook, - std::size_t initial_buffer_size = MSGPACK_UNPACKER_INIT_BUFFER_SIZE); - -#if !defined(MSGPACK_USE_CPP03) - parser(this_type&& other); - this_type& operator=(this_type&& other); -#endif // !defined(MSGPACK_USE_CPP03) - - ~parser(); - -public: - /// Reserve a buffer memory. - /** - * @param size The size of allocating memory. - * - * After returning this function, buffer_capacity() returns at least 'size'. - * See: - * https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_unpacker#msgpack-controls-a-buffer - */ - void reserve_buffer(std::size_t size = MSGPACK_UNPACKER_RESERVE_SIZE); - - /// Get buffer pointer. - /** - * You need to care about the memory is enable between buffer() and buffer() + buffer_capacity() - * See: - * https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_unpacker#msgpack-controls-a-buffer - */ - char* buffer(); - - /// Get buffer capacity. - /** - * @return The memory size that you can write. - * - * See: - * https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_unpacker#msgpack-controls-a-buffer - */ - std::size_t buffer_capacity() const; - - /// Notify a buffer consumed information to msgpack::unpacker. - /** - * @param size The size of memory that you consumed. - * - * After copying the data to the memory that is pointed by buffer(), you need to call the - * function to notify how many bytes are consumed. Then you can call next() functions. - * - * See: - * https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_unpacker#msgpack-controls-a-buffer - */ - void buffer_consumed(std::size_t size); - - /// Unpack one msgpack::object. - /** - * - * - * @return If one msgpack::object is unpacked, then return true, if msgpack::object is incomplete - * and additional data is required, then return false. If data format is invalid, throw - * msgpack::parse_error. - * - * See: - * https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_unpacker#msgpack-controls-a-buffer - */ - bool next(); - - /// Get message size. - /** - * @return Returns parsed_size() + nonparsed_size() - */ - std::size_t message_size() const; - -public: - /// Get parsed message size. - /** - * @return Parsed message size. - * - * This function is usable when non-MessagePack message follows after - * MessagePack message. - */ - std::size_t parsed_size() const; - - /// Get the address that is not parsed in the buffer. - /** - * @return Address of the buffer that is not parsed - * - * This function is usable when non-MessagePack message follows after - * MessagePack message. - */ - char* nonparsed_buffer(); - - /// Get the size of the buffer that is not parsed. - /** - * @return Size of the buffer that is not parsed - * - * This function is usable when non-MessagePack message follows after - * MessagePack message. - */ - std::size_t nonparsed_size() const; - - /// Skip the specified size of non-parsed buffer. - /** - * @param size to skip - * - * Note that the `size' argument must be smaller than nonparsed_size(). - * This function is usable when non-MessagePack message follows after - * MessagePack message. - */ - void skip_nonparsed_buffer(std::size_t size); - - /// Remove nonparsed buffer and reset the current position as a new start point. - /** - * This function is usable when non-MessagePack message follows after - * MessagePack message. - */ - void remove_nonparsed_buffer(); - - void reset(); - -protected: - char* get_raw_buffer() { - return m_buffer; - } -private: - void expand_buffer(std::size_t size); - unpack_return execute_imp(); - -private: - char* m_buffer; - std::size_t m_used; - std::size_t m_free; - std::size_t m_off; - std::size_t m_parsed; - std::size_t m_initial_buffer_size; - ReferencedBufferHook& m_referenced_buffer_hook; - -#if defined(MSGPACK_USE_CPP03) -private: - parser(const this_type&); - this_type& operator=(const this_type&); -#else // defined(MSGPACK_USE_CPP03) -public: - parser(const this_type&) = delete; - this_type& operator=(const this_type&) = delete; -#endif // defined(MSGPACK_USE_CPP03) -}; - -template -inline parser::parser( - ReferencedBufferHook& hook, - std::size_t initial_buffer_size) - :m_referenced_buffer_hook(hook) -{ - if(initial_buffer_size < COUNTER_SIZE) { - initial_buffer_size = COUNTER_SIZE; - } - - char* buffer = static_cast(::malloc(initial_buffer_size)); - if(!buffer) { - throw std::bad_alloc(); - } - - m_buffer = buffer; - m_used = COUNTER_SIZE; - m_free = initial_buffer_size - m_used; - m_off = COUNTER_SIZE; - m_parsed = 0; - m_initial_buffer_size = initial_buffer_size; - - detail::init_count(m_buffer); -} - -#if !defined(MSGPACK_USE_CPP03) -// Move constructor and move assignment operator - -template -inline parser::parser(this_type&& other) - :context_type(std::move(other)), - m_buffer(other.m_buffer), - m_used(other.m_used), - m_free(other.m_free), - m_off(other.m_off), - m_parsed(other.m_parsed), - m_initial_buffer_size(other.m_initial_buffer_size), - m_referenced_buffer_hook(other.m_referenced_buffer_hook) { - other.m_buffer = MSGPACK_NULLPTR; - other.m_used = 0; - other.m_free = 0; - other.m_off = 0; - other.m_parsed = 0; -} - -template -inline parser& parser::operator=(this_type&& other) { - this->~parser(); - new (this) this_type(std::move(other)); - return *this; -} - -#endif // !defined(MSGPACK_USE_CPP03) - - -template -inline parser::~parser() -{ - // These checks are required for move operations. - if (m_buffer) detail::decr_count(m_buffer); -} - - -template -inline void parser::reserve_buffer(std::size_t size) -{ - if(m_free >= size) return; - expand_buffer(size); -} - -template -inline void parser::expand_buffer(std::size_t size) -{ - if(m_used == m_off && detail::get_count(m_buffer) == 1 - && static_cast(*this).visitor().referenced()) { - // rewind buffer - m_free += m_used - COUNTER_SIZE; - m_used = COUNTER_SIZE; - m_off = COUNTER_SIZE; - - if(m_free >= size) return; - } - - if(m_off == COUNTER_SIZE) { - std::size_t next_size = (m_used + m_free) * 2; // include COUNTER_SIZE - while(next_size < size + m_used) { - std::size_t tmp_next_size = next_size * 2; - if (tmp_next_size <= next_size) { - next_size = size + m_used; - break; - } - next_size = tmp_next_size; - } - - char* tmp = static_cast(::realloc(m_buffer, next_size)); - if(!tmp) { - throw std::bad_alloc(); - } - - m_buffer = tmp; - m_free = next_size - m_used; - - } else { - std::size_t next_size = m_initial_buffer_size; // include COUNTER_SIZE - std::size_t not_parsed = m_used - m_off; - while(next_size < size + not_parsed + COUNTER_SIZE) { - std::size_t tmp_next_size = next_size * 2; - if (tmp_next_size <= next_size) { - next_size = size + not_parsed + COUNTER_SIZE; - break; - } - next_size = tmp_next_size; - } - - char* tmp = static_cast(::malloc(next_size)); - if(!tmp) { - throw std::bad_alloc(); - } - - detail::init_count(tmp); - - std::memcpy(tmp+COUNTER_SIZE, m_buffer + m_off, not_parsed); - - if(static_cast(*this).referenced()) { - try { - m_referenced_buffer_hook(m_buffer); - } - catch (...) { - ::free(tmp); - throw; - } - static_cast(*this).set_referenced(false); - } else { - detail::decr_count(m_buffer); - } - - m_buffer = tmp; - m_used = not_parsed + COUNTER_SIZE; - m_free = next_size - m_used; - m_off = COUNTER_SIZE; - } -} - -template -inline char* parser::buffer() -{ - return m_buffer + m_used; -} - -template -inline std::size_t parser::buffer_capacity() const -{ - return m_free; -} - -template -inline void parser::buffer_consumed(std::size_t size) -{ - m_used += size; - m_free -= size; -} - -template - inline bool parser::next() -{ - unpack_return ret = execute_imp(); - return ret == UNPACK_SUCCESS; -} - -template -inline unpack_return parser::execute_imp() -{ - std::size_t off = m_off; - unpack_return ret = context_type::execute(m_buffer, m_used, m_off); - if(m_off > off) { - m_parsed += m_off - off; - } - return ret; -} - -template -inline void parser::reset() -{ - context_type::init(); - // don't reset referenced flag - m_parsed = 0; -} - -template -inline std::size_t parser::message_size() const -{ - return m_parsed - m_off + m_used; -} - -template -inline std::size_t parser::parsed_size() const -{ - return m_parsed; -} - -template -inline char* parser::nonparsed_buffer() -{ - return m_buffer + m_off; -} - -template -inline std::size_t parser::nonparsed_size() const -{ - return m_used - m_off; -} - -template -inline void parser::skip_nonparsed_buffer(std::size_t size) -{ - m_off += size; -} - -template -inline void parser::remove_nonparsed_buffer() -{ - m_used = m_off; -} - struct zone_push_finalizer { zone_push_finalizer(msgpack::zone& z):m_z(&z) {} void set_zone(msgpack::zone& z) { m_z = &z; } @@ -1391,14 +152,14 @@ inline msgpack::object_handle unpack( msgpack::unique_ptr z(new msgpack::zone); referenced = false; std::size_t noff = off; - unpack_return ret = detail::unpack_imp( + parse_return ret = detail::unpack_imp( data, len, noff, *z, obj, referenced, f, user_data, limit); switch(ret) { - case UNPACK_SUCCESS: + case PARSE_SUCCESS: off = noff; return msgpack::object_handle(obj, msgpack::move(z)); - case UNPACK_EXTRA_BYTES: + case PARSE_EXTRA_BYTES: off = noff; return msgpack::object_handle(obj, msgpack::move(z)); default: @@ -1445,16 +206,16 @@ inline void unpack( msgpack::unique_ptr z(new msgpack::zone); referenced = false; std::size_t noff = off; - unpack_return ret = detail::unpack_imp( + parse_return ret = detail::unpack_imp( data, len, noff, *z, obj, referenced, f, user_data, limit); switch(ret) { - case UNPACK_SUCCESS: + case PARSE_SUCCESS: off = noff; result.set(obj); result.zone() = msgpack::move(z); return; - case UNPACK_EXTRA_BYTES: + case PARSE_EXTRA_BYTES: off = noff; result.set(obj); result.zone() = msgpack::move(z); @@ -1505,14 +266,14 @@ inline msgpack::object unpack( msgpack::object obj; std::size_t noff = off; referenced = false; - unpack_return ret = detail::unpack_imp( + parse_return ret = detail::unpack_imp( data, len, noff, z, obj, referenced, f, user_data, limit); switch(ret) { - case UNPACK_SUCCESS: + case PARSE_SUCCESS: off = noff; return obj; - case UNPACK_EXTRA_BYTES: + case PARSE_EXTRA_BYTES: off = noff; return obj; default: @@ -1552,59 +313,9 @@ inline msgpack::object unpack( return msgpack::v2::unpack(z, data, len, off, referenced, f, user_data, limit); } -template -inline bool parse(const char* data, size_t len, size_t& off, Visitor& v) { - unpack_return ret = detail::parse_imp(data, len, off, v); - return ret == UNPACK_SUCCESS || ret == UNPACK_EXTRA_BYTES; -} - -template -inline bool parse(const char* data, size_t len, Visitor& v) { - std::size_t off = 0; - return parse(data, len, off, v); -} - namespace detail { -template -struct parse_helper : context > { - parse_helper(Visitor& v):m_visitor(v) {} - unpack_return execute(const char* data, std::size_t len, std::size_t& off) { - return context >::execute(data, len, off); - } - Visitor& visitor() const { return m_visitor; } - Visitor& m_visitor; -}; - -template -inline unpack_return -parse_imp(const char* data, size_t len, size_t& off, Visitor& v) { - std::size_t noff = off; - - if(len <= noff) { - // FIXME - v.insufficient_bytes(noff, noff); - return UNPACK_CONTINUE; - } - detail::parse_helper h(v); - unpack_return ret = h.execute(data, len, noff); - switch (ret) { - case UNPACK_CONTINUE: - off = noff; - v.insufficient_bytes(noff - 1, noff); - return ret; - case UNPACK_SUCCESS: - off = noff; - if(noff < len) { - return UNPACK_EXTRA_BYTES; - } - return ret; - default: - return ret; - } -} - -inline unpack_return +inline parse_return unpack_imp(const char* data, std::size_t len, std::size_t& off, msgpack::zone& result_zone, msgpack::object& result, bool& referenced, unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, @@ -1614,13 +325,13 @@ unpack_imp(const char* data, std::size_t len, std::size_t& off, v.set_zone(result_zone); referenced = false; v.set_referenced(referenced); - unpack_return ret = parse_imp(data, len, off, v); + parse_return ret = parse_imp(data, len, off, v); referenced = v.referenced(); result = v.data(); return ret; } -} // detail +} // namespace detail /// @cond diff --git a/include/msgpack/v2/unpack_decl.hpp b/include/msgpack/v2/unpack_decl.hpp index 61bdf036..0e3ceda2 100644 --- a/include/msgpack/v2/unpack_decl.hpp +++ b/include/msgpack/v2/unpack_decl.hpp @@ -87,14 +87,6 @@ class unpacker; template class basic_unpacker; -typedef enum unpack_return { - UNPACK_SUCCESS = v1::UNPACK_SUCCESS, - UNPACK_EXTRA_BYTES = v1::UNPACK_EXTRA_BYTES, - UNPACK_CONTINUE = v1::UNPACK_CONTINUE, - UNPACK_PARSE_ERROR = v1::UNPACK_PARSE_ERROR, - UNPACK_STOP_VISITOR = -2 -} unpack_return; - /// Unpack msgpack::object from a buffer. /** * @param data The pointer to the buffer. @@ -327,14 +319,14 @@ bool parse(const char* data, size_t len, Visitor& v); namespace detail { -unpack_return +parse_return unpack_imp(const char* data, std::size_t len, std::size_t& off, msgpack::zone& result_zone, msgpack::object& result, bool& referenced, unpack_reference_func f, void* user_data, unpack_limit const& limit); template -unpack_return +parse_return parse_imp(const char* data, size_t len, size_t& off, UnpackVisitor& v); } // detail diff --git a/include/msgpack/v2/x3_parse.hpp b/include/msgpack/v2/x3_parse.hpp new file mode 100644 index 00000000..e824c727 --- /dev/null +++ b/include/msgpack/v2/x3_parse.hpp @@ -0,0 +1,874 @@ +// +// MessagePack for C++ deserializing routine +// +// Copyright (C) 2017 KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#ifndef MSGPACK_V2_X3_PARSE_HPP +#define MSGPACK_V2_X3_PARSE_HPP + +#if defined(MSGPACK_USE_X3_PARSE) + +#include + +#if BOOST_VERSION >= 106100 + +#include "msgpack/versioning.hpp" + +#if __GNUC__ >= 4 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif // __GNUC__ >= 4 + +#include +#include +#include + +namespace msgpack { + +/// @cond +MSGPACK_API_VERSION_NAMESPACE(v2) { +/// @endcond + +namespace detail { + +namespace x3 = boost::spirit::x3; + +using x3::byte_; + +// byte range utility +const auto byte_range = [](const std::uint8_t from, const std::uint8_t to) { + const auto check = [from, to](auto& ctx) + { + const std::uint8_t value = x3::_attr(ctx); + x3::_val(ctx) = value; + x3::_pass(ctx) = from <= value && value <= to; + }; + return x3::byte_ [check]; +}; + +// MessagePack rule +const auto mp_positive_fixint = byte_range(0x00, 0x7f); +const auto mp_fixmap = byte_range(0x80, 0x8f); +const auto mp_fixarray = byte_range(0x90, 0x9f); +const auto mp_fixstr = byte_range(0xa0, 0xbf); +const auto mp_nil = x3::byte_(0xc0); +const auto mp_false = x3::byte_(0xc2); +const auto mp_true = x3::byte_(0xc3); +const auto mp_bin8 = x3::byte_(0xc4); +const auto mp_bin16 = x3::byte_(0xc5); +const auto mp_bin32 = x3::byte_(0xc6); +const auto mp_ext8 = x3::byte_(0xc7); +const auto mp_ext16 = x3::byte_(0xc8); +const auto mp_ext32 = x3::byte_(0xc9); +const auto mp_float32 = x3::byte_(0xca); +const auto mp_float64 = x3::byte_(0xcb); +const auto mp_uint8 = x3::byte_(0xcc); +const auto mp_uint16 = x3::byte_(0xcd); +const auto mp_uint32 = x3::byte_(0xce); +const auto mp_uint64 = x3::byte_(0xcf); +const auto mp_int8 = x3::byte_(0xd0); +const auto mp_int16 = x3::byte_(0xd1); +const auto mp_int32 = x3::byte_(0xd2); +const auto mp_int64 = x3::byte_(0xd3); +const auto mp_fixext1 = x3::byte_(0xd4); +const auto mp_fixext2 = x3::byte_(0xd5); +const auto mp_fixext4 = x3::byte_(0xd6); +const auto mp_fixext8 = x3::byte_(0xd7); +const auto mp_fixext16 = x3::byte_(0xd8); +const auto mp_str8 = x3::byte_(0xd9); +const auto mp_str16 = x3::byte_(0xda); +const auto mp_str32 = x3::byte_(0xdb); +const auto mp_array16 = x3::byte_(0xdc); +const auto mp_array32 = x3::byte_(0xdd); +const auto mp_map16 = x3::byte_(0xde); +const auto mp_map32 = x3::byte_(0xdf); +const auto mp_negative_fixint = byte_range(0xe0, 0xff); + +const auto mp_d_uint8 = x3::byte_; +const auto mp_d_uint16 = x3::big_word; +const auto mp_d_uint32 = x3::big_dword; +const auto mp_d_uint64 = x3::big_qword; + +const auto mp_d_int8 = x3::byte_; +const auto mp_d_int16 = x3::big_word; +const auto mp_d_int32 = x3::big_dword; +const auto mp_d_int64 = x3::big_qword; + +x3::rule const mp_object("mp_object"); +x3::rule const array_item("array_item"); +x3::rule const map_item("map_item"); +x3::rule const kv("kv"); + +struct tag_app_specific {}; +struct index_size { + enum struct type_t { + array, + map, + other + }; + index_size(std::size_t size, type_t type = type_t::other):size(size), type(type) {} + std::size_t index = 0; + std::size_t size; + type_t type; +}; + +template +struct app_specific { + template + app_specific(Vis&& vis):vis(vis) {} + std::vector index_sizes; + Visitor vis; +}; + +template +app_specific make_app_specific(Visitor&& vis) { + return app_specific(std::forward(vis)); +} + +const auto more = [](auto &ctx) { + auto& app_specific = x3::get(ctx).get(); + _pass(ctx) = app_specific.index_sizes.back().index++ < app_specific.index_sizes.back().size; +}; + +const auto done = [](auto &ctx) { + auto& app_specific = x3::get(ctx).get(); + if (app_specific.index_sizes.back().index == app_specific.index_sizes.back().size + 1) { + _pass(ctx) = true; + switch (app_specific.index_sizes.back().type) { + case index_size::type_t::array: + app_specific.vis.end_array(); + break; + case index_size::type_t::map: + app_specific.vis.end_map(); + break; + case index_size::type_t::other: + break; + } + app_specific.index_sizes.pop_back(); + } + else { + _pass(ctx) = false; + } +}; + +const auto mp_object_def = + // ----------------------------------------------- + mp_nil [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.visit_nil(); + } + ) + ] + | + // ----------------------------------------------- + mp_true [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.visit_boolean(true); + } + ) + ] + | + // ----------------------------------------------- + mp_false [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.visit_boolean(false); + } + ) + ] + | + // ----------------------------------------------- + mp_positive_fixint [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.visit_positive_integer(_attr(ctx)); + } + ) + ] + | + // ----------------------------------------------- + mp_negative_fixint [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::int8_t val = _attr(ctx); + app_specific.vis.visit_negative_integer(val); + } + ) + ] + | + // ----------------------------------------------- + mp_uint8 >> mp_d_uint8 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.visit_negative_integer(_attr(ctx)); + } + ) + ] + | + // ----------------------------------------------- + mp_uint16 >> mp_d_uint16 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.visit_positive_integer(_attr(ctx)); + } + ) + ] + | + // ----------------------------------------------- + mp_uint32 >> mp_d_uint32 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.visit_positive_integer(_attr(ctx)); + } + ) + ] + | + // ----------------------------------------------- + mp_uint64 >> mp_d_uint64 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.visit_positive_integer(_attr(ctx)); + } + ) + ] + | + // ----------------------------------------------- + mp_int8 >> mp_d_int8 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::int8_t val = _attr(ctx); + app_specific.vis.visit_negative_integer(val); + } + ) + ] + | + // ----------------------------------------------- + mp_int16 >> mp_d_int16 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::int16_t val = _attr(ctx); + app_specific.vis.visit_negative_integer(val); + } + ) + ] + | + // ----------------------------------------------- + mp_int32 >> mp_d_int32 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::int32_t val = _attr(ctx); + app_specific.vis.visit_negative_integer(val); + } + ) + ] + | + // ----------------------------------------------- + mp_int64 >> mp_d_int64 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::int64_t val = _attr(ctx); + app_specific.vis.visit_negative_integer(val); + } + ) + ] + | + // ----------------------------------------------- + mp_float32 >> mp_d_uint32 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + union { uint32_t i; float f; } mem; + mem.i = _attr(ctx); + app_specific.vis.visit_float(mem.f); + } + ) + ] + | + // ----------------------------------------------- + mp_float64 >> mp_d_uint64 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + union { uint64_t i; double f; } mem; + mem.i = _attr(ctx); +#if defined(TARGET_OS_IPHONE) + // ok +#elif defined(__arm__) && !(__ARM_EABI__) // arm-oabi + // https://github.com/msgpack/msgpack-perl/pull/1 + mem.i = (mem.i & 0xFFFFFFFFUL) << 32UL | (mem.i >> 32UL); +#endif + app_specific.vis.visit_float(mem.f); + } + ) + ] + | + // ----------------------------------------------- + mp_fixstr [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::size_t size = _attr(ctx) & 0b00011111; + app_specific.index_sizes.emplace_back(size); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& str = _attr(ctx); + std::size_t size = std::distance(str.begin(), str.end()); + app_specific.vis.visit_str(size ? &str.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_str8 >> mp_d_uint8 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(_attr(ctx)); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& str = _attr(ctx); + std::size_t size = std::distance(str.begin(), str.end()); + app_specific.vis.visit_str(size ? &str.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_str16 >> mp_d_uint16 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(_attr(ctx)); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& str = _attr(ctx); + std::size_t size = std::distance(str.begin(), str.end()); + app_specific.vis.visit_str(size ? &str.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_str32 >> mp_d_uint32 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(_attr(ctx)); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& str = _attr(ctx); + std::size_t size = std::distance(str.begin(), str.end()); + app_specific.vis.visit_str(size ? &str.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_bin8 >> mp_d_uint8 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(_attr(ctx)); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& bin = _attr(ctx); + std::size_t size = std::distance(bin.begin(), bin.end()); + app_specific.vis.visit_bin(size ? &bin.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_bin16 >> mp_d_uint16 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(_attr(ctx)); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& bin = _attr(ctx); + std::size_t size = std::distance(bin.begin(), bin.end()); + app_specific.vis.visit_bin(size ? &bin.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_bin32 >> mp_d_uint32 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(_attr(ctx)); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& bin = _attr(ctx); + std::size_t size = std::distance(bin.begin(), bin.end()); + app_specific.vis.visit_bin(size ? &bin.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_fixarray [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::size_t size = _attr(ctx) & 0b00001111; + app_specific.index_sizes.emplace_back(size, index_size::type_t::array); + app_specific.vis.start_array(size); + } + ) + ] + >> *(x3::eps [more] >> array_item) + >> x3::eps [done] + | + // ----------------------------------------------- + mp_array16 >> mp_d_uint16 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::size_t size = _attr(ctx); + app_specific.index_sizes.emplace_back(size, index_size::type_t::array); + app_specific.vis.start_array(size); + } + ) + ] + >> *(x3::eps [more] >> array_item) + >> x3::eps [done] + | + // ----------------------------------------------- + mp_array32 >> mp_d_uint32 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::size_t size = _attr(ctx); + app_specific.index_sizes.emplace_back(size, index_size::type_t::array); + app_specific.vis.start_array(size); + } + ) + ] + >> *(x3::eps [more] >> array_item) + >> x3::eps [done] + | + // ----------------------------------------------- + mp_fixmap [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::size_t size = _attr(ctx) & 0b00001111; + app_specific.index_sizes.emplace_back(size, index_size::type_t::map); + app_specific.vis.start_map(size); + } + ) + ] + >> *(x3::eps [more] >> map_item) + >> x3::eps [done] + | + // ----------------------------------------------- + mp_map16 >> mp_d_uint16 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::size_t size = _attr(ctx); + app_specific.index_sizes.emplace_back(size, index_size::type_t::map); + app_specific.vis.start_map(size); + } + ) + ] + >> *(x3::eps [more] >> map_item) + >> x3::eps [done] + | + // ----------------------------------------------- + mp_map32 >> mp_d_uint32 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + std::size_t size = _attr(ctx); + app_specific.index_sizes.emplace_back(size, index_size::type_t::map); + app_specific.vis.start_map(size); + } + ) + ] + >> *(x3::eps [more] >> map_item) + >> x3::eps [done] + | + // ----------------------------------------------- + mp_fixext1 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(1+1); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& ext = _attr(ctx); + std::size_t size = std::distance(ext.begin(), ext.end()); + app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_fixext2 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(2+1); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& ext = _attr(ctx); + std::size_t size = std::distance(ext.begin(), ext.end()); + app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_fixext4 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(4+1); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& ext = _attr(ctx); + std::size_t size = std::distance(ext.begin(), ext.end()); + app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_fixext8 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(8+1); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& ext = _attr(ctx); + std::size_t size = std::distance(ext.begin(), ext.end()); + app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_fixext16 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(16+1); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& ext = _attr(ctx); + std::size_t size = std::distance(ext.begin(), ext.end()); + app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_ext8 >> mp_d_uint8 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(_attr(ctx)+1); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& ext = _attr(ctx); + std::size_t size = std::distance(ext.begin(), ext.end()); + app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_ext16 >> mp_d_uint16 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(_attr(ctx)+1); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& ext = _attr(ctx); + std::size_t size = std::distance(ext.begin(), ext.end()); + app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size); + } + ) + ] + | + // ----------------------------------------------- + mp_ext32 >> mp_d_uint32 [ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.index_sizes.emplace_back(_attr(ctx)+1); + } + ) + ] + >> + x3::raw [ + *(x3::eps [more] >> x3::char_) + >> x3::eps [done] + ][ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + auto const& ext = _attr(ctx); + std::size_t size = std::distance(ext.begin(), ext.end()); + app_specific.vis.visit_ext(size ? &ext.front() : nullptr, size); + } + ) + ]; + +const auto array_item_def = + x3::eps[ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.start_array_item(); + _pass(ctx) = true; + } + ) + ] + >> + mp_object + >> + x3::eps[ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.end_array_item(); + _pass(ctx) = true; + } + ) + ]; + +const auto map_item_def = kv; +const auto kv_def = + x3::eps[ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.start_map_key(); + _pass(ctx) = true; + } + ) + ] + >> + mp_object + >> + x3::eps[ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.end_map_key(); + _pass(ctx) = true; + } + ) + ] + >> + x3::eps[ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.start_map_value(); + _pass(ctx) = true; + } + ) + ] + >> + mp_object + >> + x3::eps[ + ( + [](auto& ctx){ + auto& app_specific = x3::get(ctx).get(); + app_specific.vis.end_map_value(); + _pass(ctx) = true; + } + ) + ]; + +BOOST_SPIRIT_DEFINE( + mp_object, array_item, map_item, kv +); + +const auto rule = mp_object; + +} // namespace detail + +template +inline bool parse(Iterator&& begin, Iterator&& end, Visitor&& vis) { + auto data = detail::make_app_specific(std::forward(vis)); + return detail::x3::parse( + std::forward(begin), + std::forward(end), + detail::x3::with(std::ref(data))[detail::rule] + ); +} + +/// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v2) +/// @endcond + +} // namespace msgpack + +#if __GNUC__ >= 4 +#pragma GCC diagnostic pop +#endif // __GNUC__ >= 4 + +#else // BOOST_VERSION >= 106100 + +#error Boost 1.61.0 or later is required to use x3 parse + +#endif // BOOST_VERSION >= 106100 + +#endif // defined(MSGPACK_USE_X3_PARSE) + +#endif // MSGPACK_V2_X3_PARSE_HPP diff --git a/include/msgpack/v2/x3_unpack.hpp b/include/msgpack/v2/x3_unpack.hpp new file mode 100644 index 00000000..16576ac2 --- /dev/null +++ b/include/msgpack/v2/x3_unpack.hpp @@ -0,0 +1,119 @@ +// +// MessagePack for C++ deserializing routine +// +// Copyright (C) 2017 KONDO Takatoshi +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +#ifndef MSGPACK_V2_X3_UNPACK_HPP +#define MSGPACK_V2_X3_UNPACK_HPP + +#if defined(MSGPACK_USE_X3_PARSE) + +#include + +#if BOOST_VERSION >= 106100 + +#include "msgpack/versioning.hpp" +#include "msgpack/v2/create_object_visitor.hpp" +#include "msgpack/v2/x3_parse.hpp" + +namespace msgpack { + +/// @cond +MSGPACK_API_VERSION_NAMESPACE(v2) { +/// @endcond + +namespace detail { + +template +inline void +unpack_imp(Iterator&& begin, Iterator&& end, + msgpack::zone& result_zone, msgpack::object& result, bool& referenced, + unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, + unpack_limit const& limit = unpack_limit()) +{ + create_object_visitor v(f, user_data, limit); + v.set_zone(result_zone); + referenced = false; + v.set_referenced(referenced); + if (!parse(std::forward(begin), std::forward(end), v)) { + throw msgpack::parse_error("parse error"); + } + referenced = v.referenced(); + result = v.data(); +} + +} // namespace detail + + +template +inline msgpack::object_handle unpack( + Iterator&& begin, Iterator&& end, + bool& referenced, + unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, + unpack_limit const& limit = unpack_limit()) +{ + msgpack::object obj; + msgpack::unique_ptr z(new msgpack::zone); + referenced = false; + detail::unpack_imp( + std::forward(begin), std::forward(end), *z, obj, referenced, f, user_data, limit); + return msgpack::object_handle(obj, msgpack::move(z)); +} + +template +inline msgpack::object_handle unpack( + Iterator&& begin, Iterator&& end, + unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, + unpack_limit const& limit = unpack_limit()) +{ + bool referenced; + return unpack(std::forward(begin), std::forward(end), referenced, f, user_data, limit); +} + +template +inline msgpack::object unpack( + msgpack::zone& z, + Iterator&& begin, Iterator&& end, + bool& referenced, + unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, + unpack_limit const& limit = unpack_limit()) +{ + msgpack::object obj; + referenced = false; + detail::unpack_imp( + std::forward(begin), std::forward(end), z, obj, referenced, f, user_data, limit); + return obj; +} + +template +inline msgpack::object unpack( + msgpack::zone& z, + Iterator&& begin, Iterator&& end, + unpack_reference_func f = MSGPACK_NULLPTR, void* user_data = MSGPACK_NULLPTR, + unpack_limit const& limit = unpack_limit()) +{ + bool referenced; + return unpack( + z, std::forward(begin), std::forward(end), referenced, f, user_data, limit); +} + + +/// @cond +} // MSGPACK_API_VERSION_NAMESPACE(v2) +/// @endcond + +} // namespace msgpack + +#else // BOOST_VERSION >= 106100 + +#error Boost 1.61.0 or later is required to use x3 parse + +#endif // BOOST_VERSION >= 106100 + +#endif // defined(MSGPACK_USE_X3_PARSE) + +#endif // MSGPACK_V2_X3_UNPACK_HPP diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fb490b6c..8f51a0ca 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -48,6 +48,12 @@ IF (MSGPACK_BOOST) ) ENDIF () +IF (MSGPACK_USE_X3_PARSE) + LIST (APPEND check_PROGRAMS + msgpack_x3_parse.cpp + ) +ENDIF () + IF (MSGPACK_CXX11) LIST (APPEND check_PROGRAMS iterator_cpp11.cpp diff --git a/test/msgpack_x3_parse.cpp b/test/msgpack_x3_parse.cpp new file mode 100644 index 00000000..d01d5c18 --- /dev/null +++ b/test/msgpack_x3_parse.cpp @@ -0,0 +1,829 @@ +#include "msgpack.hpp" + +#include +#include + +#if defined(MSGPACK_USE_X3_PARSE) + +#include + +using namespace std; + +const double kEPS = 1e-10; + +TEST(MSGPACK_X3_PARSE, nil_t) +{ + msgpack::type::nil_t v; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_TRUE(oh.get().is_nil()); +} + +TEST(MSGPACK_X3_PARSE, bool_false) +{ + bool v = false; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, bool_true) +{ + bool v = true; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, positive_fixint_1) +{ + uint8_t v = 0; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, positive_fixint_2) +{ + uint8_t v = 127; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, negative_fixint_1) +{ + int8_t v = -1; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, negative_fixint_2) +{ + int8_t v = -32; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, uint8_1) +{ + uint8_t v = 128U; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, uint8_2) +{ + uint8_t v = 0xffU; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, uint16_1) +{ + uint16_t v = 0x100U; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, uint16_2) +{ + uint16_t v = 0xffffU; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, uint32_1) +{ + uint32_t v = 0x10000UL; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, uint32_2) +{ + uint32_t v = 0xffffffffUL; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, uint64_1) +{ + uint64_t v = 0x100000000ULL; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, uint64_2) +{ + uint64_t v = 0xffffffffffffffffULL; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, int8_1) +{ + int8_t v = 0b11011111; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, int8_2) +{ + int8_t v = 0b10000000; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, int16_1) +{ + int16_t v = 0xff00; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, int16_2) +{ + int16_t v = 0x8000; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, int32_1) +{ + int32_t v = 0xff000000L; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, int32_2) +{ + int32_t v = 0x80000000L; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, int64_1) +{ + int64_t v = 0xff00000000000000LL; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, int64_2) +{ + int64_t v = 0x8000000000000000LL; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, array_1) +{ + std::vector v; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as >()); +} + +TEST(MSGPACK_X3_PARSE, array_2) +{ + std::vector v; + std::stringstream ss; + for (int i = 0; i != 0xffU; ++i) v.push_back(i); + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as >()); +} + +TEST(MSGPACK_X3_PARSE, array_3) +{ + std::vector v; + std::stringstream ss; + for (int i = 0; i != 0xffU+1U; ++i) v.push_back(i); + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as >()); +} + +TEST(MSGPACK_X3_PARSE, array_4) +{ + std::vector v; + std::stringstream ss; + for (int i = 0; i != 0xffffU; ++i) v.push_back(i); + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as >()); +} + +TEST(MSGPACK_X3_PARSE, array_5) +{ + std::vector v; + std::stringstream ss; + for (uint32_t i = 0; i != 0xffffU+1U; ++i) v.push_back(i); + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as >()); +} + +TEST(MSGPACK_X3_PARSE, map_1) +{ + std::map v; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, (oh.get().as >())); +} + +TEST(MSGPACK_X3_PARSE, map_2) +{ + std::map v; + std::stringstream ss; + for (int i = 0; i != 0xffU; ++i) v.emplace(i, i); + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, (oh.get().as >())); +} + +TEST(MSGPACK_X3_PARSE, map_3) +{ + std::map v; + std::stringstream ss; + for (int i = 0; i != 0xffU+1U; ++i) v.emplace(i, i); + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, (oh.get().as >())); +} + +TEST(MSGPACK_X3_PARSE, map_4) +{ + std::map v; + std::stringstream ss; + for (int i = 0; i != 0xffffU; ++i) v.emplace(i, i); + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, (oh.get().as >())); +} + +TEST(MSGPACK_X3_PARSE, map_5) +{ + std::map v; + std::stringstream ss; + for (uint32_t i = 0; i != 0xffffU+1U; ++i) v.emplace(i, i); + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, (oh.get().as >())); +} + +TEST(MSGPACK_X3_PARSE, float_1) +{ + std::vector v; + v.push_back(0.0); + v.push_back(-0.0); + v.push_back(1.0); + v.push_back(-1.0); + v.push_back(numeric_limits::min()); + v.push_back(numeric_limits::max()); + v.push_back(nanf("tag")); + if (numeric_limits::has_infinity) { + v.push_back(numeric_limits::infinity()); + v.push_back(-numeric_limits::infinity()); + } + if (numeric_limits::has_quiet_NaN) { + v.push_back(numeric_limits::quiet_NaN()); + } + if (numeric_limits::has_signaling_NaN) { + v.push_back(numeric_limits::signaling_NaN()); + } + + for (unsigned int i = 0; i < v.size() ; i++) { + std::stringstream ss; + float val1 = v[i]; + msgpack::pack(ss, val1); + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + float val2 = oh.get().as(); + + if (std::isnan(val1)) + EXPECT_TRUE(std::isnan(val2)); + else if (std::isinf(val1)) + EXPECT_TRUE(std::isinf(val2)); + else + EXPECT_TRUE(fabs(val2 - val1) <= kEPS); + } +} + +TEST(MSGPACK_X3_PARSE, double_1) +{ + std::vector v; + v.push_back(0.0); + v.push_back(-0.0); + v.push_back(1.0); + v.push_back(-1.0); + v.push_back(numeric_limits::min()); + v.push_back(numeric_limits::max()); + v.push_back(nanf("tag")); + if (numeric_limits::has_infinity) { + v.push_back(numeric_limits::infinity()); + v.push_back(-numeric_limits::infinity()); + } + if (numeric_limits::has_quiet_NaN) { + v.push_back(numeric_limits::quiet_NaN()); + } + if (numeric_limits::has_signaling_NaN) { + v.push_back(numeric_limits::signaling_NaN()); + } + + for (unsigned int i = 0; i < v.size() ; i++) { + std::stringstream ss; + double val1 = v[i]; + msgpack::pack(ss, val1); + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + double val2 = oh.get().as(); + + if (std::isnan(val1)) + EXPECT_TRUE(std::isnan(val2)); + else if (std::isinf(val1)) + EXPECT_TRUE(std::isinf(val2)); + else + EXPECT_TRUE(fabs(val2 - val1) <= kEPS); + } +} + +TEST(MSGPACK_X3_PARSE, string_1) +{ + std::string v; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, string_2) +{ + std::string v; + + for (uint64_t i = 0; i != 0x1fU; ++i) v.push_back('0'+(i%10)); + + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, string_3) +{ + std::string v; + + for (uint64_t i = 0; i != 0xffU; ++i) v.push_back('0'+(i%10)); + + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, string_4) +{ + std::string v; + + for (uint64_t i = 0; i != 0xffU+1U; ++i) v.push_back('0'+(i%10)); + + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, string_5) +{ + std::string v; + + for (uint64_t i = 0; i != 0xffffU; ++i) v.push_back('0'+(i%10)); + + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, string_6) +{ + std::string v; + + for (uint64_t i = 0; i != 0xffffUL + 1UL; ++i) v.push_back('0'+(i%10)); + + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, bin_1) +{ + std::vector v; + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as>()); +} + +TEST(MSGPACK_X3_PARSE, bin_2) +{ + std::vector v; + + for (uint64_t i = 0; i != 0x1fU; ++i) v.push_back(i%0xff); + + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as>()); +} + +TEST(MSGPACK_X3_PARSE, bin_3) +{ + std::vector v; + + for (uint64_t i = 0; i != 0xffU; ++i) v.push_back(i%0xff); + + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as>()); +} + +TEST(MSGPACK_X3_PARSE, bin_4) +{ + std::vector v; + + for (uint64_t i = 0; i != 0xffU+1U; ++i) v.push_back(i%0xff); + + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as>()); +} + +TEST(MSGPACK_X3_PARSE, bin_5) +{ + std::vector v; + + for (uint64_t i = 0; i != 0xffffU; ++i) v.push_back(i%0xff); + + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as>()); +} + +TEST(MSGPACK_X3_PARSE, bin_6) +{ + std::vector v; + + for (uint64_t i = 0; i != 0xffffUL + 1UL; ++i) v.push_back(i%0xff); + + std::stringstream ss; + msgpack::pack(ss, v); + + auto oh = msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, oh.get().as>()); +} + +TEST(MSGPACK_X3_PARSE, fixext1) +{ + std::stringstream ss; + msgpack::packer packer(ss); + char const buf [] = { 2 }; + + packer.pack_ext(sizeof(buf), 1); + packer.pack_ext_body(buf, sizeof(buf)); + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(1ul, oh.get().via.ext.size); + EXPECT_EQ(1, oh.get().via.ext.type()); + EXPECT_EQ(2, oh.get().via.ext.data()[0]); +} + +TEST(MSGPACK_X3_PARSE, fixext2) +{ + std::stringstream ss; + msgpack::packer packer(ss); + char const buf [] = { 2, 3 }; + + packer.pack_ext(sizeof(buf), 0); + packer.pack_ext_body(buf, sizeof(buf)); + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(2ul, oh.get().via.ext.size); + EXPECT_EQ(0, oh.get().via.ext.type()); + EXPECT_TRUE( + std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data())); +} + +TEST(MSGPACK_X3_PARSE, fixext4) +{ + std::stringstream ss; + msgpack::packer packer(ss); + char const buf [] = { 2, 3, 4, 5 }; + + packer.pack_ext(sizeof(buf), 1); + packer.pack_ext_body(buf, sizeof(buf)); + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(4ul, oh.get().via.ext.size); + EXPECT_EQ(1, oh.get().via.ext.type()); + EXPECT_TRUE( + std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data())); +} + +TEST(MSGPACK_X3_PARSE, fixext8) +{ + std::stringstream ss; + msgpack::packer packer(ss); + char const buf [] = { 2, 3, 4, 5, 6, 7, 8, 9 }; + + packer.pack_ext(sizeof(buf), 1); + packer.pack_ext_body(buf, sizeof(buf)); + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(8ul, oh.get().via.ext.size); + EXPECT_EQ(1, oh.get().via.ext.type()); + EXPECT_TRUE( + std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data())); +} + +TEST(MSGPACK_X3_PARSE, fixext16) +{ + std::stringstream ss; + msgpack::packer packer(ss); + char const buf [] = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 }; + + packer.pack_ext(sizeof(buf), 1); + packer.pack_ext_body(buf, sizeof(buf)); + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(16ul, oh.get().via.ext.size); + EXPECT_EQ(1, oh.get().via.ext.type()); + EXPECT_TRUE( + std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data())); +} + +TEST(MSGPACK_X3_PARSE, ext_0) +{ + std::size_t const size = 0; + std::stringstream ss; + msgpack::packer packer(ss); + + packer.pack_ext(size, 77); + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(size, oh.get().via.ext.size); + EXPECT_EQ(77, oh.get().via.ext.type()); +} + +TEST(MSGPACK_X3_PARSE, ext_255) +{ + std::size_t const size = 255; + std::stringstream ss; + msgpack::packer packer(ss); + char buf[size]; + for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast(i); + packer.pack_ext(sizeof(buf), 77); + packer.pack_ext_body(buf, sizeof(buf)); + + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(size, oh.get().via.ext.size); + EXPECT_EQ(77, oh.get().via.ext.type()); + EXPECT_TRUE( + std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data())); +} + +TEST(MSGPACK_X3_PARSE, ext_256) +{ + std::size_t const size = 256; + std::stringstream ss; + msgpack::packer packer(ss); + char buf[size]; + for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast(i); + packer.pack_ext(sizeof(buf), 77); + packer.pack_ext_body(buf, sizeof(buf)); + + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(size, oh.get().via.ext.size); + EXPECT_EQ(77, oh.get().via.ext.type()); + EXPECT_TRUE( + std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data())); +} + +TEST(MSGPACK_X3_PARSE, ext_65535) +{ + std::size_t const size = 65535; + std::stringstream ss; + msgpack::packer packer(ss); + char buf[size]; + for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast(i); + packer.pack_ext(sizeof(buf), 77); + packer.pack_ext_body(buf, sizeof(buf)); + + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(size, oh.get().via.ext.size); + EXPECT_EQ(77, oh.get().via.ext.type()); + EXPECT_TRUE( + std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data())); +} + +TEST(MSGPACK_X3_PARSE, ext_65536) +{ + std::size_t const size = 65536; + std::stringstream ss; + msgpack::packer packer(ss); + char buf[size]; + for (std::size_t i = 0; i != size; ++i) buf[i] = static_cast(i); + packer.pack_ext(sizeof(buf), 77); + packer.pack_ext_body(buf, sizeof(buf)); + + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end()); + EXPECT_EQ(size, oh.get().via.ext.size); + EXPECT_EQ(77, oh.get().via.ext.type()); + EXPECT_TRUE( + std::equal(buf, buf + sizeof(buf), oh.get().via.ext.data())); +} + +TEST(MSGPACK_X3_PARSE, unpack_referenced_1) +{ + std::string v = "ABC"; + std::stringstream ss; + msgpack::pack(ss, v); + + bool r; + msgpack::object_handle oh = + msgpack::unpack(ss.str().begin(), ss.str().end(), r); + EXPECT_FALSE(r); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, unpack_referenced_2) +{ + std::string v = "ABC"; + std::stringstream ss; + msgpack::pack(ss, v); + + // copy is required because ss.str() returns temporary object. + std::string str = ss.str(); + bool r; + msgpack::object_handle oh = + msgpack::unpack( + str.begin(), + str.end(), + r, + [](msgpack::type::object_type, std::size_t, void*) { + return true; + } + ); + EXPECT_TRUE(r); + EXPECT_EQ(v, oh.get().as()); +} + +TEST(MSGPACK_X3_PARSE, unpack_zone_1) +{ + std::string v = "ABC"; + std::stringstream ss; + msgpack::pack(ss, v); + + msgpack::zone z; + msgpack::object obj = + msgpack::unpack(z, ss.str().begin(), ss.str().end()); + EXPECT_EQ(v, obj.as()); +} + +TEST(MSGPACK_X3_PARSE, unpack_zone_2) +{ + std::string v = "ABC"; + std::stringstream ss; + msgpack::pack(ss, v); + + msgpack::zone z; + bool r; + msgpack::object obj = + msgpack::unpack(z, ss.str().begin(), ss.str().end(), r); + EXPECT_EQ(v, obj.as()); + + EXPECT_FALSE(r); + EXPECT_EQ(v, obj.as()); +} + +TEST(MSGPACK_X3_PARSE, unpack_zone_3) +{ + std::string v = "ABC"; + std::stringstream ss; + msgpack::pack(ss, v); + + // copy is required because ss.str() returns temporary object. + std::string str = ss.str(); + msgpack::zone z; + bool r; + msgpack::object obj = + msgpack::unpack( + z, + str.begin(), + str.end(), + r, + [](msgpack::type::object_type, std::size_t, void*) { + return true; + } + ); + EXPECT_TRUE(r); + EXPECT_EQ(v, obj.as()); +} + +#endif // defined(MSGPACK_USE_X3_PARSE)