diff --git a/.travis.yml b/.travis.yml index 90a8f8514..4ecc0dab7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,28 +13,16 @@ matrix: before_install: - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get update; fi - - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get install -y build-essential checkinstall cmake libavutil-dev libavcodec-dev libavformat-dev libbz2-dev libcurl4-openssl-dev libcv-dev libopencv-highgui-dev libhighgui-dev libgnome-keyring-dev libgstreamer-plugins-base0.10-dev libgstreamer0.10-dev libjasper-dev libjpeg-dev libmicrohttpd-dev libopencv-dev libprotobuf-dev libqt4-dev libspeex-dev libspeexdsp-dev libsqlite3-dev libssl-dev libswscale-dev libtbb-dev libtiff4-dev libupnp-dev libv4l-dev libxine-dev libxslt1-dev libxss-dev pkg-config protobuf-compiler python-dev qtmobility-dev gdb ; fi + - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get install -y build-essential libssl-dev libsqlcipher-dev libbz2-dev libmicrohttpd-dev libsqlite3-dev libupnp-dev pkg-config qt5-default libxss-dev qtmultimedia5-dev libqt5x11extras5-dev libqt5designer5 qttools5-dev; fi - -# - if [ $TRAVIS_OS_NAME == osx ]; then xcode-select --install ; fi - if [ $TRAVIS_OS_NAME == osx ]; then brew update ; fi -# - if [ $TRAVIS_OS_NAME == osx ]; then brew install qt55 openssl miniupnpc libmicrohttpd speex homebrew/science/opencv ffmpeg sqlcipher ; fi - - if [ $TRAVIS_OS_NAME == osx ]; then brew install qt55 openssl miniupnpc libmicrohttpd speex speexdsp ffmpeg sqlcipher ; fi + - if [ $TRAVIS_OS_NAME == osx ]; then brew install qt55 openssl miniupnpc libmicrohttpd sqlcipher; fi - if [ $TRAVIS_OS_NAME == osx ]; then brew link --force qt55 ; fi -#Fix for opencv and numpy already installed by system - - if [ $TRAVIS_OS_NAME == osx ]; then rm '/usr/local/bin/f2py'; fi - - if [ $TRAVIS_OS_NAME == osx ]; then rm -r '/usr/local/lib/python2.7/site-packages/numpy'; fi - - if [ $TRAVIS_OS_NAME == osx ]; then brew install opencv; fi -# FIX: Now openssl is not linked in /usr/local/include and lib - - if [ $TRAVIS_OS_NAME == osx ]; then ln -s /usr/local/opt/openssl/include/* /usr/local/include/; fi - - if [ $TRAVIS_OS_NAME == osx ]; then ln -s /usr/local/opt/openssl/lib/*.a /usr/local/lib/; fi - - if [ $TRAVIS_OS_NAME == osx ]; then ln -s /usr/local/opt/openssl/lib/*.dylib /usr/local/lib/; fi - -# - if [ $TRAVIS_OS_NAME == linux ]; then sudo apt-get update && sudo apt-get install -y llvm-3.4 llvm-3.4-dev; fi -# - rvm use $RVM --install --binary --fuzzy -# - gem update --system -# - gem --version + - wget https://github.com/Tencent/rapidjson/archive/v1.1.0.tar.gz + - tar -xf v1.1.0.tar.gz + - if [ $TRAVIS_OS_NAME == osx ]; then cp -r rapidjson-1.1.0/include/rapidjson/ /usr/local/include/rapidjson ; fi + - if [ $TRAVIS_OS_NAME == linux ]; then sudo cp -r rapidjson-1.1.0/include/rapidjson/ /usr/include/rapidjson ; fi env: global: @@ -48,16 +36,16 @@ addons: name: "RetroShare/RetroShare" description: "RetroShare Build submitted via Travis CI" build_command_prepend: "qmake CONFIG+=no_sqlcipher; make clean" - build_command: "make -j 4" + build_command: "make -j4" branch_pattern: coverity_scan before_script: - - if [ $TRAVIS_OS_NAME == linux ]; then qmake QMAKE_CC=$CC QMAKE_CXX=$CXX CONFIG+=no_sqlcipher CONFIG+=tests ; fi - - if [ $TRAVIS_OS_NAME == osx ]; then qmake QMAKE_CC=$CC QMAKE_CXX=$CXX CONFIG+=no_sqlcipher CONFIG+=tests CONFIG+=rs_macos10.12 ; fi + - if [ $TRAVIS_OS_NAME == linux ]; then qmake QMAKE_CC=$CC QMAKE_CXX=$CXX; fi + - if [ $TRAVIS_OS_NAME == osx ]; then qmake QMAKE_CC=$CC QMAKE_CXX=$CXX CONFIG+=rs_macos10.12 INCLUDEPATH+=/usr/local/opt/openssl/include/ QMAKE_LIBDIR+=/usr/local/opt/openssl/lib/; fi script: - - if [ $TRAVIS_OS_NAME == linux ] && [ "${COVERITY_SCAN_BRANCH}" != 1 ]; then make && tests/unittests/unittests >/dev/null 2>&1 ; fi - - if [ $TRAVIS_OS_NAME == osx ] && [ "${COVERITY_SCAN_BRANCH}" != 1 ]; then make -j 4 ; fi + - if [ $TRAVIS_OS_NAME == linux ] && [ "${COVERITY_SCAN_BRANCH}" != 1 ]; then make -j2; fi + - if [ $TRAVIS_OS_NAME == osx ] && [ "${COVERITY_SCAN_BRANCH}" != 1 ]; then make -j2; fi #after_success: diff --git a/README.md b/README.md index 8902b43b8..c726983e5 100644 --- a/README.md +++ b/README.md @@ -32,19 +32,19 @@ Compilation on Linux libqt4-dev libssl-dev libxss-dev libgnome-keyring-dev libbz2-dev \ libqt4-opengl-dev libqtmultimediakit1 qtmobility-dev libsqlcipher-dev \ libspeex-dev libspeexdsp-dev libxslt1-dev libcurl4-openssl-dev \ - libopencv-dev tcl8.5 libmicrohttpd-dev + libopencv-dev tcl8.5 libmicrohttpd-dev rapidjson-dev ``` * openSUSE ```bash sudo zypper install gcc-c++ libqt4-devel libgnome-keyring-devel \ glib2-devel speex-devel libssh-devel protobuf-devel libcurl-devel \ libxml2-devel libxslt-devel sqlcipher-devel libmicrohttpd-devel \ - opencv-devel speexdsp-devel libupnp-devel libavcodec-devel + opencv-devel speexdsp-devel libupnp-devel libavcodec-devel rapidjson ``` * Arch Linux ```bash pacman -S base-devel libgnome-keyring libmicrohttpd libupnp libxslt \ - libxss opencv qt4 speex speexdsp sqlcipher + libxss opencv qt4 speex speexdsp sqlcipher rapidjson ``` 2. Checkout the source code diff --git a/RetroShare.pro b/RetroShare.pro index 9a6f24d5f..d124ce94d 100644 --- a/RetroShare.pro +++ b/RetroShare.pro @@ -1,13 +1,12 @@ !include("retroshare.pri"): error("Could not include file retroshare.pri") TEMPLATE = subdirs -#CONFIG += tests SUBDIRS += openpgpsdk openpgpsdk.file = openpgpsdk/src/openpgpsdk.pro retrotor { - libretroshare.depends = openpgpsdk + libretroshare.depends = openpgpsdk } else { SUBDIRS += libbitdht libbitdht.file = libbitdht/src/libbitdht.pro diff --git a/android-prepare-toolchain.sh b/android-prepare-toolchain.sh index 7d8337add..100b426ff 100755 --- a/android-prepare-toolchain.sh +++ b/android-prepare-toolchain.sh @@ -248,8 +248,8 @@ build_sqlite() make -j${HOST_NUM_CPU} make install rm -f ${SYSROOT}/usr/lib/libsqlite3.so* - ${CC} -shared -o libsqlite3.so -fPIC sqlite3.o -ldl - cp libsqlite3.so "${SYSROOT}/usr/lib" +# ${CC} -shared -o libsqlite3.so -fPIC sqlite3.o -ldl +# cp libsqlite3.so "${SYSROOT}/usr/lib" cd .. } @@ -313,6 +313,14 @@ build_libmicrohttpd() cd .. } +build_rapidjson() +{ + B_dir="rapidjson-1.1.0" + [ -f $B_dir.tar.gz ] || wget -O $B_dir.tar.gz https://github.com/Tencent/rapidjson/archive/v1.1.0.tar.gz + tar -xf $B_dir.tar.gz + cp -r rapidjson-1.1.0/include/rapidjson/ "${SYSROOT}/usr/include/rapidjson" +} + build_toolchain [ "${INSTALL_QT_ANDROID}X" == "trueX" ] && install_qt_android build_bzlib @@ -320,5 +328,6 @@ build_openssl build_sqlite build_sqlcipher build_libupnp +build_rapidjson echo NATIVE_LIBS_TOOLCHAIN_PATH=${NATIVE_LIBS_TOOLCHAIN_PATH} diff --git a/appveyor.yml b/appveyor.yml index cc1cbef95..6f26a1c67 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -12,7 +12,7 @@ #---------------------------------# # version format -version: RetroShare 0.6.0.{build}-{branch} +version: RetroShare-git-{branch}-{build} # you can use {branch} name in version format too # version: 1.0.{build}-{branch} @@ -52,7 +52,7 @@ on_finish: #- ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1')) # clone directory -clone_folder: c:\projects\RetroShare +clone_folder: C:\projects\RetroShare # fetch repository as zip archive #shallow_clone: true # default is "false" @@ -62,19 +62,18 @@ clone_depth: 1 # clone entire repository history if not de environment: global: - #Qt: https://www.appveyor.com/docs/installed-software#qt - QTDIR: C:\Qt\5.4\mingw491_32 +## Qt: https://www.appveyor.com/docs/installed-software#qt +# QTDIR: C:\Qt\5.10.1\mingw53_32 MSYS2_ARCH: i686 TARGET: i686_32-pc-msys + MINGW_PREFIX: C:\msys64\mingw32 + RS_DEPLOY: RetroShare_deploy # build cache to preserve files/folders between builds -cache: - - c:\projects\libs -# - packages -> **\packages.config # preserve "packages" directory in the root of build folder but will reset it if packages.config is modified -# - projectA\libs -# - node_modules # local npm modules -# - %APPDATA%\npm-cache # npm cache +#cache: +# Disabled because it's bigger then supported by appveyor free plan +# - C:\msys64\var\cache\pacman\pkg # scripts that run after cloning repository #install: @@ -89,23 +88,12 @@ install: # Configuring MSys2 - set PATH=C:\msys64\usr\bin;%PATH% - set PATH=C:\msys64\mingw32\bin;%PATH% + - pacman --noconfirm -S mingw-w64-i686-qt5 mingw-w64-i686-miniupnpc mingw-w64-i686-sqlcipher mingw-w64-i686-libmicrohttpd + #- pacman --noconfirm -S mingw-w64-i686-qt5-static mingw-w64-i686-miniupnpc mingw-w64-i686-sqlcipher mingw-w64-i686-libmicrohttpd + #- set PATH=C:\msys64\mingw32\qt5-static\bin\;%PATH% + # Configuring Qt - - set PATH=%QTDIR%\bin;C:\Qt\Tools\mingw491_32\bin;%PATH% - # Install all default programms - #- C:\msys64\usr\bin\bash -lc "pacman --noconfirm -Sy base-devel git mercurial cvs wget p7zip gcc perl ruby python2" #Already installed - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -Sy openssl-devel" - # Install toolchain - #- C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-toolchain mingw-w64-x86_64-toolchain" #Already installed - # Install other binutils - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-curl mingw-w64-x86_64-curl" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-miniupnpc mingw-w64-x86_64-miniupnpc" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-sqlite3 mingw-w64-x86_64-sqlite3" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-speex mingw-w64-x86_64-speex" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-speexdsp mingw-w64-x86_64-speexdsp" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-opencv mingw-w64-x86_64-opencv" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-ffmpeg mingw-w64-x86_64-ffmpeg" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-libmicrohttpd mingw-w64-x86_64-libmicrohttpd" - - C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw-w64-i686-libxslt mingw-w64-x86_64-libxslt" +# - set PATH=%QTDIR%\bin;C:\Qt\Tools\mingw491_32\bin;%PATH% # Hack for new MSys2 - copy C:\msys64\mingw32\i686-w64-mingw32\bin\ar.exe C:\msys64\mingw32\bin\i686-w64-mingw32-ar.exe @@ -114,10 +102,6 @@ install: - copy C:\msys64\mingw64\x86_64-w64-mingw32\bin\ar.exe C:\msys64\mingw64\bin\x86_64-w64-mingw32-ar.exe - copy C:\msys64\mingw64\x86_64-w64-mingw32\bin\ranlib.exe C:\msys64\mingw64\bin\x86_64-w64-mingw32-ranlib.exe - copy C:\msys64\mingw64\bin\windres.exe C:\msys64\mingw64\bin\x86_64-w64-mingw32-windres.exe - # Build missing Libs - #- C:\msys64\mingw32.exe -lc "cd /c/projects/RetroShare/msys2_build_libs/ && make" - # Clone RetroShare - #- git clone -q --branch={branch} https://github.com/RetroShare/RetroShare.git C:\projects\RetroShare #---------------------------------# @@ -142,18 +126,112 @@ configuration: Release # scripts to run before build before_build: + - cd C:\projects\RetroShare +# - find C:\ > filelist.txt # scripts to run *after* solution is built and *before* automatic packaging occurs (web apps, NuGet packages, Azure Cloud Services) before_package: -# scripts to run after build -after_build: - # to run your custom scripts instead of automatic MSBuild build_script: - - cd C:\projects\RetroShare - - qmake CONFIG+=no_sqlcipher - - make + - qmake -Wall -spec win32-g++ "CONFIG=debug" + - mingw32-make -j3 + +# scripts to run after build +after_build: + - mkdir %RS_DEPLOY% + - copy retroshare-nogui\src\retroshare-nogui.exe %RS_DEPLOY%\ + - copy retroshare-gui\src\retroshare.exe %RS_DEPLOY%\ + +## In Debug build winedeplyqt forget the non debug Qt libs + - copy C:\msys64\mingw32\bin\Qt5Svg.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Core.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Multimedia.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Widgets.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Xml.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5PrintSupport.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Gui.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\Qt5Network.dll %RS_DEPLOY%\ + + - mkdir %RS_DEPLOY%\playlistformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\playlistformats\qtmultimedia_m3u.dll %RS_DEPLOY%\playlistformats + + - mkdir %RS_DEPLOY%\printsupport\ + - copy C:\msys64\mingw32\share\qt5\plugins\printsupport\windowsprintersupport.dll %RS_DEPLOY%\printsupport\ + + - mkdir %RS_DEPLOY%\iconengines\ + - copy C:\msys64\mingw32\share\qt5\plugins\iconengines\qsvgicon.dll %RS_DEPLOY%\iconengines\ + + - mkdir %RS_DEPLOY%\bearer\ + - copy C:\msys64\mingw32\share\qt5\plugins\bearer\qgenericbearer.dll %RS_DEPLOY%\bearer\ + + - mkdir %RS_DEPLOY%\mediaservice\ + - copy C:\msys64\mingw32\share\qt5\plugins\mediaservice\qtmedia_audioengine.dll %RS_DEPLOY%\mediaservice\ + - copy C:\msys64\mingw32\share\qt5\plugins\mediaservice\dsengine.dll %RS_DEPLOY%\mediaservice\ + + - mkdir %RS_DEPLOY%\styles\ + - copy C:\msys64\mingw32\share\qt5\plugins\styles\qwindowsvistastyle.dll %RS_DEPLOY%\styles\ + + - mkdir %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qwebp.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qicns.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qjpeg.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qtiff.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qtga.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qjp2.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qico.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qwbmp.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qicns.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qgif.dll %RS_DEPLOY%\imageformats\ + - copy C:\msys64\mingw32\share\qt5\plugins\imageformats\qsvg.dll %RS_DEPLOY%\imageformats\ + + - mkdir %RS_DEPLOY%\platforms\ + - copy C:\msys64\mingw32\share\qt5\plugins\platforms\qwindows.dll %RS_DEPLOY%\platforms\ + + - mkdir %RS_DEPLOY%\audio\ + - copy C:\msys64\mingw32\share\qt5\plugins\audio\qtaudio_windows.dll %RS_DEPLOY%\audio\ + + - windeployqt %RS_DEPLOY%\retroshare.exe + + - copy C:\msys64\mingw32\bin\libbz2*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libeay32.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libminiupnpc.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libsqlcipher*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libsqlite3*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\ssleay32.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\zlib*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libgcc_s_dw2*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libstdc*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libwinpthread*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libicu*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libpcre*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libharfbuzz*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libpng*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libfreetype*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libglib*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libgraphite2.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libintl*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libiconv*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libjasper*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libjpeg*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libtiff*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libwebp*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libwebpdemux*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\liblzma*.dll %RS_DEPLOY%\ + +## Needed for libresapi http + - copy C:\msys64\mingw32\bin\libmicrohttpd*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libgnutls*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libgmp*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libhogweed*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libidn2*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libnettle*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libp11-kit*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libtasn1*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libunistring*.dll %RS_DEPLOY%\ + - copy C:\msys64\mingw32\bin\libffi*.dll %RS_DEPLOY%\ + + - find C:\projects\RetroShare >> filelist.txt # to disable automatic builds #build: off @@ -162,7 +240,10 @@ build_script: # artifacts configuration # #---------------------------------# -#artifacts: +artifacts: + - path: $(RS_DEPLOY) + - path: '**\*.exe' + - path: filelist.txt # # # pushing a single file # - path: test.zip diff --git a/build_scripts/Debian+Ubuntu/changelog b/build_scripts/Debian+Ubuntu/changelog index 57ed5bd3e..7bdac642a 100644 --- a/build_scripts/Debian+Ubuntu/changelog +++ b/build_scripts/Debian+Ubuntu/changelog @@ -1,5 +1,125 @@ retroshare (ZZZZZZ-1.XXXXXX~YYYYYY) YYYYYY; urgency=low + f663600 csoler Fri, 1 Jun 2018 22:27:59 +0200 fixed makeSourcePackage.sh + 9b16e33 csoler Fri, 11 May 2018 20:53:29 +0200 removed conflicts to retroshare06 in debian control file + 57dde55 csoler Sat, 26 May 2018 11:23:33 +0200 Merge pull request #1269 from PhenomRetroShare/Add_NeverAskMeForExternalLinkActivated + 33fe823 csoler Sat, 26 May 2018 11:22:47 +0200 Merge pull request #1271 from sehraf/pr_compile-fix-unused-services + 65beb2c csoler Sat, 26 May 2018 11:09:41 +0200 Merge pull request #1275 from sehraf/pr_fix-IDdialog-chat-link + d8b3fc0 csoler Sat, 26 May 2018 11:08:46 +0200 Merge pull request #1274 from PhenomRetroShare/Fix_AppVeyorCompil + f53e5e8 sehraf Fri, 25 May 2018 23:59:02 +0200 Fix displayed chat link name in id dialog before (decimal): Message in chat room 5327517029776505601 after (proper id): Message in chat room LFD6E08C33A98C658 + 428b331 sehraf Fri, 25 May 2018 23:12:35 +0200 fix for Qt 5.11 + 7c77e93 Phenom Fri, 25 May 2018 17:32:36 +0200 Fix AppVeyor Compilation + de65cb6 csoler Tue, 22 May 2018 23:59:26 +0200 Merge pull request #1272 from G10h4ck/net_little_fixes + 1ad1fdc Gioacc Tue, 22 May 2018 19:17:37 +0200 p3NetMgrIMPL::checkNetAddress() notify if port change + 0bf02e2 Gioacc Tue, 22 May 2018 19:14:25 +0200 pqissludp::Initiate_Connection() check remote_addr to be IPv4 before use + 0c19a56 sehraf Mon, 21 May 2018 14:55:10 +0200 compile fix gxsphoto + 92b21d7 sehraf Mon, 21 May 2018 14:26:46 +0200 compile fix wikipoos + 8f107cc Phenom Sat, 19 May 2018 16:37:29 +0200 Add Never Ask Me For External Link Activated + 186617f Gioacc Thu, 17 May 2018 16:43:14 +0200 Fix missing include + 28981b6 csoler Wed, 16 May 2018 13:37:02 +0200 Merge pull request #1251 from PhenomRetroShare/Fix_Cppcheck(duplInheritedMember)InRsItem + d11f88c csoler Wed, 16 May 2018 13:33:25 +0200 Merge pull request #1252 from PhenomRetroShare/Fix_Cppcheck(duplInheritedMember)InbdNode + 2145911 csoler Wed, 16 May 2018 13:30:56 +0200 Merge pull request #1262 from PhenomRetroShare/Fix_PartialDirCheck + e232000 csoler Wed, 16 May 2018 13:28:23 +0200 Merge pull request #1266 from csoler/v0.6-FT + 3f88e3e csoler Wed, 16 May 2018 13:24:52 +0200 added check to avoid hashing files that are currently being modified + 8451550 csoler Wed, 16 May 2018 11:40:39 +0200 added missing licence to file_tree.h + 291c86e csoler Wed, 16 May 2018 11:17:22 +0200 Merge pull request #1265 from csoler/v0.6-GxsFix + 4661329 csoler Wed, 16 May 2018 11:15:18 +0200 fixed bug causing new forums and identities to not show up immediately + 9f37b63 G10h4c Wed, 16 May 2018 10:49:28 +0200 Merge pull request #1264 from G10h4ck/channel_json_api_v2 + 181d99e Gioacc Wed, 16 May 2018 10:38:47 +0200 Give proper error message if Qt version is too old + f12cd57 Phenom Sat, 12 May 2018 14:31:17 +0200 Fix Partial Dir Check + 57cff61 csoler Fri, 11 May 2018 20:28:53 +0200 fixed merge + ecbd115 csoler Fri, 11 May 2018 20:24:24 +0200 added missing include path for libretroshare/src in libresapi + fe5ea9e csoler Fri, 11 May 2018 17:37:15 +0200 additional fixes to debian packaging files + 2173aab csoler Fri, 11 May 2018 17:02:58 +0200 fixed rules file again + 84a02cb csoler Fri, 11 May 2018 16:39:49 +0200 added missing targets in rules file + 4b6304e csoler Fri, 11 May 2018 16:33:19 +0200 few fixes according to lintian complaints + c2492fe csoler Thu, 10 May 2018 23:10:16 +0200 fixed a few lintian errors in debian/control + 9f5409b csoler Thu, 10 May 2018 22:04:11 +0200 fixed email to match mentors login + 45fdbb4 csoler Thu, 10 May 2018 22:03:39 +0200 fixed email to match mentors login + b4ada80 csoler Wed, 9 May 2018 13:54:40 +0200 removed > 3.4.1 in debian/control, because pbuilder would not build + e81c82e csoler Wed, 9 May 2018 13:37:25 +0200 added notes file for debian release + 4d287d6 csoler Wed, 9 May 2018 13:27:16 +0200 fixed weird mistake in grouter which causes an issue only in gcc > 8 + 889c277 csoler Wed, 9 May 2018 10:15:32 +0200 fixed packaging script for debian release + f406d9a csoler Wed, 9 May 2018 10:15:02 +0200 updated debian/control to be the control file for stretch. Updated copyright notice + 186ff0f csoler Wed, 9 May 2018 10:12:34 +0200 created proper debian changelog using dch and updated project url + 6d67936 csoler Tue, 8 May 2018 15:34:44 +0200 Merge pull request #1253 from PhenomRetroShare/Add_LastPostColInGroupTreeWidget + 2ebacf3 Phenom Tue, 8 May 2018 12:08:08 +0200 Add Last Post Column in GroupTreeWidget. + 4d748bd Phenom Mon, 7 May 2018 18:05:07 +0200 Fix CppCheck duplInheritedMember warning in bdNode + f39fd06 Phenom Mon, 7 May 2018 17:29:11 +0200 Fix CppCheck duplInheritedMember warning in RsItem + 1352631 thunde Mon, 7 May 2018 06:42:52 +0200 Updated Windows build environment + 38ac234 thunde Mon, 7 May 2018 07:30:29 +0200 Fixed Windows compile with pre-compiled libraries Added new variable EXTERNAL_LIB_DIR to specify path of external libraries + d9a75a9 thunde Sun, 29 Apr 2018 20:40:05 +0200 Added RapidJSON to Windows build environment + 5b607ad csoler Sun, 6 May 2018 23:53:45 +0200 Merge pull request #1245 from PhenomRetroShare/AddContextMenuForGxsIdInTextChatBrowser + a6821f4 csoler Sun, 6 May 2018 23:02:18 +0200 Merge pull request #1249 from sehraf/pr_libsecret + c20436e csoler Sun, 6 May 2018 22:59:20 +0200 Merge pull request #1250 from PhenomRetroShare/Fix_CppCheckerWarningInFtController + d8dc2a8 csoler Sun, 6 May 2018 22:57:57 +0200 Merge pull request #1246 from PhenomRetroShare/Add_GxsIdInRestoredChatMsg + dca33da Phenom Sun, 6 May 2018 18:53:29 +0200 Fix CppCheck in ftcontroller.cc + c89e36a sehraf Fri, 4 May 2018 20:52:31 +0200 add auto selection of libsecret with fallback to libgnome-keyring + 1129bcb sehraf Fri, 4 May 2018 20:47:41 +0200 Add support for libsecret + 10badf5 sehraf Fri, 4 May 2018 20:43:40 +0200 fix retroshare-nogui.pro autologin + 676c070 csoler Thu, 3 May 2018 15:55:21 +0200 extended the rapid_json trick to plugins + 5f12b60 csoler Thu, 3 May 2018 15:22:39 +0200 added -lgnome-keyring to unix LIBS when rs_autologin is set + bfe8e40 csoler Thu, 3 May 2018 15:22:03 +0200 updated ubuntu changelog + 98f0052 Phenom Thu, 3 May 2018 15:04:22 +0200 Add GxsId in Restored Chat Message. + ecba4c2 Phenom Wed, 2 May 2018 20:31:52 +0200 Add Context Menu for GxsId in lobby textBrowser. + 91ed367 csoler Thu, 3 May 2018 13:45:44 +0200 added packaging for ubuntu bionic + 56e8134 cyril soler Thu, 3 May 2018 11:32:24 +0200 removed sqlite3 lib from ld when using sqlcipher. + 1366f61 csoler Wed, 2 May 2018 23:51:46 +0200 Merge pull request #1241 from RetroPooh/chatimgattprev1 + 8e111c2 csoler Wed, 2 May 2018 22:46:27 +0200 added rapidjson-1.1.0 code hard-coded in the source directory to allow everyone to compile without the need to tweak too much. When v1.1.0 is mainstream (espcially on ubuntu) we can revert back to an external dependency + 48c4b4c G10h4c Sat, 28 Apr 2018 09:34:14 +0200 Merge pull request #1242 from beardog108/master + ccede9d G10h4c Sat, 28 Apr 2018 09:33:14 +0200 Merge pull request #1243 from sehraf/pr_fix-make-install + 10daf3b sehraf Sat, 28 Apr 2018 09:06:10 +0200 Fix 'make install' + 82a00c2 KevinF Fri, 27 Apr 2018 15:34:43 -0500 added rapidjson to package installation command on Debian/OpenSUSE/Arch + be75e89 Gioacc Fri, 27 Apr 2018 20:55:38 +0200 Fix compialtion after merge + f3ae61b Gioacc Fri, 27 Apr 2018 18:17:09 +0200 Merge branch 'json_experiments' + + -- Retroshare Dev Team Thu, 03 May 2018 12:00:00 +0100 + +retroshare (0.6.4-1.20180503.676c0701~xenial) xenial; urgency=low + + 2dc69cb RetroP Fri, 27 Apr 2018 16:50:00 +0300 embed preview for images on file attach in chat + c199199 Gioacc Sun, 8 Apr 2018 12:37:41 +0200 pqissl silence extra debug message + 8245d74 csoler Sat, 7 Apr 2018 14:33:58 +0200 Merge pull request #1230 from csoler/master + 2782494 csoler Sat, 7 Apr 2018 14:29:23 +0200 removed debug info + e2b0e27 csoler Sat, 7 Apr 2018 00:56:07 +0200 fixed costly polling in RsGenExchange + cc091cc Gioacc Sat, 7 Apr 2018 12:48:01 +0200 Fixed hidden nodes listening failure + 73c6dee csoler Tue, 27 Mar 2018 20:54:31 +0200 Merge pull request #1191 from G10h4ck/IPv6-v3 + 4be73b7 csoler Sat, 24 Mar 2018 10:18:37 +0100 Merge pull request #1216 from beardog108/security + 599c3d4 Kevin Fri, 23 Mar 2018 23:00:27 -0500 fixed clickjacking attack with x-frame-options + 24d1f5d csoler Sat, 17 Mar 2018 18:40:32 +0100 Merge pull request #1190 from PhenomRetroShare/Add_ShowEmptySubMenuRemoteTree + 09de680 csoler Sat, 17 Mar 2018 18:30:21 +0100 Merge pull request #1210 from PhenomRetroShare/Add_BackgroundInProgressBarText + 1432104 csoler Sat, 17 Mar 2018 18:25:47 +0100 Merge pull request #1214 from csoler/v0.6-FT + e1482dd Phenom Sat, 17 Mar 2018 00:00:05 +0100 Add Rounded and Gradient Background to xprogressbar text. + 7da73b3 Phenom Mon, 5 Mar 2018 20:31:39 +0100 Add differents views depends ProgressBar width + 311b190 Phenom Sun, 4 Mar 2018 20:06:33 +0100 Add Background to xprogressbar text for more readability. + e1ad21c csoler Thu, 15 Mar 2018 13:11:19 +0100 fixed wrong file count in RsCollectionDialog when downloading files + b3653d1 csoler Thu, 15 Mar 2018 11:32:55 +0100 enabled aggressive re-request of pending slices at end of transfer, thus fixing the long delay to finish files with mixed fast/slow sources + 6e8305a csoler Wed, 14 Mar 2018 20:56:30 +0100 Merge pull request #1211 from PhenomRetroShare/Fix_AvatarStatusOverlay + c7549a2 csoler Wed, 14 Mar 2018 20:49:42 +0100 Merge pull request #1208 from PhenomRetroShare/Fix_ChangeChatTextColorWhenChangeStyle + 11cfa7b csoler Wed, 14 Mar 2018 20:34:52 +0100 Merge pull request #1209 from PhenomRetroShare/Add_DescriptionGxsGroupToolTip + 3fc6314 csoler Wed, 14 Mar 2018 20:33:34 +0100 Merge pull request #1206 from PhenomRetroShare/Fix_AboutIcon + 0e6d27a csoler Tue, 13 Mar 2018 20:25:38 +0100 fixed bug in FT causing transfer lines to only update when the mouse moves over the widget + 91634ba thunde Thu, 8 Mar 2018 17:45:24 +0100 Added build of Retrotor to Windows build environment + e9b49e1 Gioacc Mon, 12 Mar 2018 17:06:01 +0100 Update Android quirks documentation + ce56a20 Gioacc Mon, 12 Mar 2018 16:22:42 +0100 Added sources verification in android toolchain + e7078b5 Gioacc Mon, 12 Mar 2018 14:27:59 +0100 Update Android documentation + 514b31e Gioacc Sun, 11 Mar 2018 20:46:07 +0100 Add Qt installation in android preparation script + 6f30aa2 Gioacc Sun, 11 Mar 2018 20:45:40 +0100 Merge branch 'master' of git://github.com/RetroShare/RetroShare + 13b18e3 csoler Sat, 10 Mar 2018 16:05:52 +0100 added missing decoration icon on transfer sources + 4dd2776 Gioacc Sat, 10 Mar 2018 13:15:53 +0100 Remove android JNI .class from source + 2b300eb Gioacc Sat, 10 Mar 2018 00:44:24 +0100 Update version in Android Manifest + f5a3b26 Gioacc Fri, 9 Mar 2018 20:26:29 +0100 More omogeneous variable naming in android build tools + c92b860 thunde Sun, 4 Mar 2018 20:08:15 +0100 Windows build environment: - Updated external libraries - Added compile of plugins to build.bat - Added copy of Qt style DLL to pack.bat and Windows Installer - Removed "CONFIG=console" for release build + 8b42873 Phenom Sun, 4 Mar 2018 22:45:11 +0100 Fix Friend Avatar status overlay no depends size scale. + 6877589 Phenom Sun, 4 Mar 2018 18:19:18 +0100 Add description in GroupTreeWidget tooltip. + 51cc94b Phenom Sun, 4 Mar 2018 17:31:40 +0100 Fix ChatWidget current text edit color when changing appearance style. + 4e6f2ae Phenom Sun, 4 Mar 2018 14:31:16 +0100 Fix Icon for About page. + a89ab8f Phenom Sat, 24 Feb 2018 14:07:32 +0100 Add ShowEmpty sub menu in Tree Remote SharedFilesDialog. + + -- Retroshare Dev Team Thu, 03 May 2018 12:00:00 +0100 + +retroshare (0.6.4-1.20180313.0e6d27ad~xenial) xenial; urgency=low + 919417a csoler Sat, 3 Mar 2018 19:04:54 +0100 make sure tor executable from config path can be reached b587ac8 csoler Sat, 3 Mar 2018 16:06:49 +0100 Merge pull request #1192 from PhenomRetroShare/Add_WarningMaxFlatViewFile 9bcff07 csoler Sat, 3 Mar 2018 15:58:11 +0100 Merge pull request #1203 from PhenomRetroShare/Fix_SharedFileDialogShowColumn diff --git a/build_scripts/Debian+Ubuntu/control.bionic b/build_scripts/Debian+Ubuntu/control.bionic new file mode 100644 index 000000000..80b452523 --- /dev/null +++ b/build_scripts/Debian+Ubuntu/control.bionic @@ -0,0 +1,46 @@ +Source: retroshare +Section: devel +Priority: standard +Maintainer: Cyril Soler +Build-Depends: debhelper (>= 7), libglib2.0-dev, libupnp-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.6, libsqlcipher-dev, libmicrohttpd-dev, libavcodec-dev, qtmultimedia5-dev, qttools5-dev, libqt5x11extras5-dev, qt5-default +Standards-Version: 3.9.6 +Homepage: http://retroshare.sourceforge.net + +Package: retroshare-voip-plugin +Architecture: any +Conflicts: retroshare06-voip-plugin +Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare, libspeex1, libspeexdsp1, libqt5multimedia5 +Description: RetroShare VOIP plugin + This package provides a plugin for RetroShare, a secured Friend-to-Friend communication + plateform. The plugin adds voice-over-IP functionality to the private chat window. Both + friends chatting together need the plugin installed to be able to talk together. + +Package: retroshare-feedreader-plugin +Architecture: any +Conflicts: retroshare06-feedreader-plugin +Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare +Description: RetroShare FeedReader plugin + This package provides a plugin for RetroShare, a secured Friend-to-Friend communication + plateform. The plugin adds a RSS feed reader tab to retroshare. + +Package: retroshare-nogui +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends}, gnome-keyring +Conflicts: retroshare,retroshare06-nogui +Description: Secure communication with friends + This is the command-line client for RetroShare network. This client + can be contacted and talked-to using SSL. Clients exist for portable + devices running e.g. Android. + +Package: retroshare +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends}, gnome-keyring +Conflicts: retroshare-nogui,retroshare06 +Description: Secure communication with friends + RetroShare is a Open Source cross-platform, private and secure decentralised + commmunication platform. It lets you to securely chat and share files with your + friends and family, using a web-of-trust to authenticate peers and OpenSSL to + encrypt all communication. RetroShare provides filesharing, chat, messages, + forums and channels. + + diff --git a/build_scripts/Debian+Ubuntu/debian/changelog b/build_scripts/Debian+Ubuntu/debian/changelog new file mode 100644 index 000000000..5d7671fc3 --- /dev/null +++ b/build_scripts/Debian+Ubuntu/debian/changelog @@ -0,0 +1,5 @@ +retroshare (0.6.4) UNRELEASED; urgency=medium + + * Initial release for Debian. (Closes: #659069) + + -- Cyril Soler Wed, 09 May 2018 10:11:31 +0200 diff --git a/build_scripts/Debian+Ubuntu/debian/compat b/build_scripts/Debian+Ubuntu/debian/compat index 7f8f011eb..f599e28b8 100644 --- a/build_scripts/Debian+Ubuntu/debian/compat +++ b/build_scripts/Debian+Ubuntu/debian/compat @@ -1 +1 @@ -7 +10 diff --git a/build_scripts/Debian+Ubuntu/debian/control b/build_scripts/Debian+Ubuntu/debian/control index 3d1aaec9d..f93af67e9 100644 --- a/build_scripts/Debian+Ubuntu/debian/control +++ b/build_scripts/Debian+Ubuntu/debian/control @@ -2,45 +2,40 @@ Source: retroshare Section: devel Priority: standard Maintainer: Cyril Soler -Build-Depends: debhelper (>= 7), libglib2.0-dev, libupnp-dev, qt4-dev-tools, libqt4-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libqt4-opengl-dev, libqtmultimediakit1, qtmobility-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.5, libsqlcipher-dev, libmicrohttpd-dev, libavcodec-dev -Standards-Version: 3.9.3 +Build-Depends: debhelper (>= 9), libglib2.0-dev, libupnp-dev, libssl-dev, libxss-dev, libgnome-keyring-dev, libbz2-dev, libspeex-dev, libspeexdsp-dev, libxslt1-dev, cmake, libcurl4-openssl-dev, libopencv-dev, tcl8.6, libsqlcipher-dev, libmicrohttpd-dev, libavcodec-dev, qtmultimedia5-dev, qttools5-dev, libqt5x11extras5-dev, qtbase5-dev, qt5-qmake, qtbase5-dev-tools +Standards-Version: 4.1.4 Homepage: http://retroshare.sourceforge.net Package: retroshare-voip-plugin Architecture: any -Conflicts: retroshare06-voip-plugin -Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare, libspeex1, libspeexdsp1, libqtmultimediakit1 +Conflicts: +Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare, libspeex1, libspeexdsp1, libqt5multimedia5 Description: RetroShare VOIP plugin - This package provides a plugin for RetroShare, a secured Friend-to-Friend communication + This package provides a plugin for RetroShare, a secured Friend-to-Friend communication plateform. The plugin adds voice-over-IP functionality to the private chat window. Both friends chatting together need the plugin installed to be able to talk together. Package: retroshare-feedreader-plugin Architecture: any -Conflicts: retroshare06-feedreader-plugin +Conflicts: Depends: ${shlibs:Depends}, ${misc:Depends}, retroshare Description: RetroShare FeedReader plugin - This package provides a plugin for RetroShare, a secured Friend-to-Friend communication - plateform. The plugin adds a RSS feed reader tab to retroshare. + Plugin for Retroshare, adding a RSS feed reader tab to retroshare. Package: retroshare-nogui Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, gnome-keyring -Conflicts: retroshare,retroshare06-nogui -Description: Secure communication with friends - This is the command-line client for RetroShare network. This client - can be contacted and talked-to using SSL. Clients exist for portable - devices running e.g. Android. +Conflicts: retroshare +Description: headless version of Retroshare + Headless version of the Retroshare platform. Package: retroshare Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends}, gnome-keyring -Conflicts: retroshare-nogui,retroshare06 +Conflicts: retroshare-nogui Description: Secure communication with friends - RetroShare is a Open Source cross-platform, private and secure decentralised - commmunication platform. It lets you to securely chat and share files with your - friends and family, using a web-of-trust to authenticate peers and OpenSSL to - encrypt all communication. RetroShare provides filesharing, chat, messages, - forums and channels. + RetroShare is a Open Source, private and secure decentralised + commmunication platform. It creates mesh of computers linked with TLS connections, + on top of which it provides file transfer, asynchronous email, forums, channels and chat. diff --git a/build_scripts/Debian+Ubuntu/debian/rules b/build_scripts/Debian+Ubuntu/debian/rules index ef740237e..2e59d918e 100755 --- a/build_scripts/Debian+Ubuntu/debian/rules +++ b/build_scripts/Debian+Ubuntu/debian/rules @@ -7,11 +7,12 @@ configure-stamp: touch $@ -build: build-stamp +build: build-arch build-indep + build-stamp: configure-stamp dh_testdir # Add here commands to compile the package. - # cd libssh-0.6.4 && mkdir -p build && cd build && cmake -DWITH_STATIC_LIB=ON .. && make + # cd libssh-0.6.4 && mkdir -p build && cd build && cmake -DWITH_STATIC_LIB=ON .. && make # cd sqlcipher && ./configure --disable-shared --enable-static --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" LDFLAGS="-lcrypto" && make # mkdir lib # cp -r libssh-0.6.4 lib/ @@ -20,18 +21,24 @@ build-stamp: configure-stamp cd src && $(MAKE) touch $@ +build-indep: build-stamp + +build-arch: build-stamp + clean: dh_testdir dh_testroot rm -f configure-stamp build-stamp # Add here commands to clean up after the build process. [ ! -f src/Makefile ] || (cd src && $(MAKE) distclean) + dh_prep dh_clean install: build dh_testdir dh_testroot - dh_clean -k + dh_prep + dh_clean #dh_installdirs cd src && $(MAKE) INSTALL_ROOT=$(CURDIR)/debian/tmp install diff --git a/build_scripts/Debian+Ubuntu/debian/source/format b/build_scripts/Debian+Ubuntu/debian/source/format new file mode 100644 index 000000000..d3827e75a --- /dev/null +++ b/build_scripts/Debian+Ubuntu/debian/source/format @@ -0,0 +1 @@ +1.0 diff --git a/build_scripts/Debian+Ubuntu/debian_release_howto.txt b/build_scripts/Debian+Ubuntu/debian_release_howto.txt new file mode 100644 index 000000000..f77a9dd3e --- /dev/null +++ b/build_scripts/Debian+Ubuntu/debian_release_howto.txt @@ -0,0 +1,32 @@ +Creation of a new Debian changelog: + + dch --create --package retroshare --newversion 0.6.4-1 + + Note: dch reads email in $DEBMAIL or $USER@$HOSTNAME, so it should be made correct in debian/changlog + If the email does not match the email in mentors, the package will be rejected. + +dget command to retrieve source package: + + dget -u https://launchpad.net/~retroshare/+archive/ubuntu/stable/+files/retroshare_0.6.4-1.20180313.0e6d27ad~xenial.dsc + + (-u means don't check PGP signature) + +When ready: + * updload the package in a place that can be used to dget the package on mentors.debian.net. + + dput mentors retroshare_0.6.4-1_source.changes + +Checkign with lintian: + lintian --pedantic --profile debian retroshare_0.6.4-1_source.changes + +Todo + * make a sid binary package. + * test in sid using pbuilder chroot system (pbuilder login) + * upload to mentors + * request for sponsorship + +Getting help: + https://webchat.oftc.net/ + +Bug creation/report + reportbug -B debian diff --git a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh index 26ebd651b..007a52790 100755 --- a/build_scripts/Debian+Ubuntu/makeSourcePackage.sh +++ b/build_scripts/Debian+Ubuntu/makeSourcePackage.sh @@ -3,8 +3,8 @@ ###################### PARAMETERS #################### gitpath="https://github.com/RetroShare/RetroShare.git" branch="master" -#branch="v0.6.3-official_release" -#bubba3="Y" # comment out to compile for bubba3 +#branch="v0.6.4-official_release" +#bubba3="Y" # comment out to compile for bubba3 ###################################################### RS_MAJOR_VERSION=`fgrep RS_MAJOR_VERSION ../../libretroshare/src/retroshare/rsversion.h | cut -d\\ -f3- | sed -e s\/\ \/\/g | cut -c1` @@ -33,7 +33,7 @@ gpgkey="0932399B" date=`git log --pretty=format:"%ai" | head -1 | cut -d\ -f1 | sed -e s/-//g` time=`git log --pretty=format:"%aD" | head -1 | cut -d\ -f5 | sed -e s/://g` -hhsh=`git log --pretty=format:"%H" | head -1 | cut -c1-8` +hhsh=`git log --pretty=format:"%H" | head -1 | cut -c1-8` rev=${date}.${hhsh} useretrotor="false" @@ -45,7 +45,7 @@ while [ ${#} -gt 0 ]; do shift ;; "-retrotor") shift - useretrotor="true" + useretrotor="true" ;; "-distribution") shift dist=${1} @@ -68,16 +68,16 @@ while [ ${#} -gt 0 ]; do done if test "${useretrotor}" = "true"; then - if ! test "${dist}" = "trusty"; then - echo ERROR: retro-tor can only be packaged for trusty for now. - exit 1; - fi - #gitpath="https://github.com/csoler/RetroShare.git" - #branch="v0.6-TorOnly" + if ! test "${dist}" = "trusty"; then + echo ERROR: retro-tor can only be packaged for trusty for now. + exit 1; + fi + #gitpath="https://github.com/csoler/RetroShare.git" + #branch="v0.6-TorOnly" fi if test "${dist}" = "" ; then - dist="trusty xenial artful" + dist="trusty xenial artful bionic" fi echo Attempting to get revision number... @@ -96,7 +96,7 @@ echo " Using distributions:"${dist} echo " Using PGP key id :"${gpgkey} if test ${useretrotor} = "true"; then - echo " "Specific flags : retrotor + echo " "Specific flags : retrotor fi echo Done. @@ -115,15 +115,15 @@ cd ${workdir}/src git clone --depth 1 ${gitpath} --single-branch --branch $branch . # if ! test "$hhsh" = "" ; then -# echo Checking out revision $hhsh -# git checkout $hhsh +# echo Checking out revision $hhsh +# git checkout $hhsh # fi cd - if ! test -d ${workdir}/src/libretroshare/; then - echo Git clone failed. - exit + echo Git clone failed. + exit fi cp -r debian ${workdir}/debian @@ -131,6 +131,12 @@ cp -r debian ${workdir}/debian # VOIP tweak cp ${workdir}/src/retroshare-gui/src/gui/chat/PopupChatDialog.ui ${workdir}/src/plugins/VOIP/gui/PopupChatDialog.ui +# remove unised qml code, only needed on Android + +rm -rf ${workdir}/src/retroshare-qml-app/ +rm -rf ${workdir}/src/build_scripts/ +rm ${workdir}/debian/*~ + # Cloning sqlcipher # git clone https://github.com/sqlcipher/sqlcipher.git @@ -147,21 +153,26 @@ echo Cleaning... echo Calling debuild... for i in ${dist}; do - echo copying changelog for ${i} - sed -e s/XXXXXX/"${rev}"/g -e s/YYYYYY/"${i}"/g -e s/ZZZZZZ/"${version_number}"/g ../changelog > debian/changelog - if test ${useretrotor} = "true"; then - cp ../rules.retrotor debian/rules - cp ../control.trusty_retrotor debian/control - elif test -f ../control."${i}" ; then - echo \/\!\\ Using specific control file for distribution "${i}" - cp ../control."${i}" debian/control + if ! test "${i}" = "debian"; then + echo copying changelog for ${i} + sed -e s/XXXXXX/"${rev}"/g -e s/YYYYYY/"${i}"/g -e s/ZZZZZZ/"${version_number}"/g ../changelog > debian/changelog + + if test ${useretrotor} = "true"; then + cp ../rules.retrotor debian/rules + cp ../control.trusty_retrotor debian/control + elif test -f ../control."${i}" ; then + echo \/\!\\ Using specific control file for distribution "${i}" + cp ../control."${i}" debian/control + else + echo Using standard control file control."${i}" for distribution "${i}" + cp ../debian/control debian/control + fi else - echo Using standard control file control."${i}" for distribution "${i}" - cp ../debian/control debian/control - fi + echo creating official debian release. Using built-in changelog and control files + fi - debuild -S -k${gpgkey} + debuild -S -k${gpgkey} --lintian-opts +pedantic -EviIL done cd - diff --git a/build_scripts/Windows/build-libs/Makefile b/build_scripts/Windows/build-libs/Makefile index 4d7339149..6ed67ed56 100755 --- a/build_scripts/Windows/build-libs/Makefile +++ b/build_scripts/Windows/build-libs/Makefile @@ -12,14 +12,13 @@ TCL_VERSION=8.6.2 SQLCIPHER_VERSION=2.2.1 LIBMICROHTTPD_VERSION=0.9.59 FFMPEG_VERSION=3.4 - -MAKEFILE_PATH=$(dir $(MAKEFILE_LIST)) -LIBS_PATH?=$(MAKEFILE_PATH)../../../../libs +RAPIDJSON_VERSION=1.1.0 DOWNLOAD_PATH?=download -COPY_ANSWER?= +BUILD_PATH=build +LIBS_PATH?=libs -all: dirs zlib bzip2 miniupnpc openssl speex speexdsp opencv libxml2 libxslt curl sqlcipher libmicrohttpd ffmpeg copylibs +all: dirs zlib bzip2 miniupnpc openssl speex speexdsp opencv libxml2 libxslt curl sqlcipher libmicrohttpd ffmpeg rapidjson copylibs download: \ $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz \ @@ -35,234 +34,246 @@ download: \ $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz \ $(DOWNLOAD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tar.gz \ $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz \ - $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz + $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz \ + $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz clean: - rm -r -f libs + rm -r -f $(BUILD_PATH) + rm -r -f $(LIBS_PATH) dirs: mkdir -p $(DOWNLOAD_PATH) - mkdir -p libs - gcc --version | head --lines 1 | tr ' ' '\n' | tail -1 >libs/gcc-version + mkdir -p $(BUILD_PATH) + gcc --version | head --lines 1 | tr ' ' '\n' | tail -1 >$(BUILD_PATH)/gcc-version $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz: wget --no-check-certificate http://sourceforge.net/projects/libpng/files/zlib/$(ZLIB_VERSION)/zlib-$(ZLIB_VERSION).tar.gz/download -O $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz -zlib: libs/zlib-$(ZLIB_VERSION) +zlib: $(BUILD_PATH)/zlib-$(ZLIB_VERSION) -libs/zlib-$(ZLIB_VERSION): $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz +$(BUILD_PATH)/zlib-$(ZLIB_VERSION): $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz # prepare - rm -r -f libs/zlib-* + rm -r -f $(BUILD_PATH)/zlib-* tar xvf $(DOWNLOAD_PATH)/zlib-$(ZLIB_VERSION).tar.gz # build cd zlib-$(ZLIB_VERSION) && ./configure - #cd zlib-$(ZLIB_VERSION) && make install prefix="`pwd`/../libs" + #cd zlib-$(ZLIB_VERSION) && make install prefix="`pwd`/../$(BUILD_PATH)" cd zlib-$(ZLIB_VERSION) && make # copy files - mkdir -p libs/zlib-$(ZLIB_VERSION).tmp/include - cp zlib-$(ZLIB_VERSION)/zlib.h libs/zlib-$(ZLIB_VERSION).tmp/include/ - cp zlib-$(ZLIB_VERSION)/zconf.h libs/zlib-$(ZLIB_VERSION).tmp/include/ - mkdir -p libs/zlib-$(ZLIB_VERSION).tmp/lib - cp zlib-$(ZLIB_VERSION)/libz.a libs/zlib-$(ZLIB_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp/include + cp zlib-$(ZLIB_VERSION)/zlib.h $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp/include/ + cp zlib-$(ZLIB_VERSION)/zconf.h $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp/include/ + mkdir -p $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp/lib + cp zlib-$(ZLIB_VERSION)/libz.a $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp/lib/ + # cleanup rm -r -f zlib-$(ZLIB_VERSION) - mv libs/zlib-$(ZLIB_VERSION).tmp libs/zlib-$(ZLIB_VERSION) + mv $(BUILD_PATH)/zlib-$(ZLIB_VERSION).tmp $(BUILD_PATH)/zlib-$(ZLIB_VERSION) -bzip2: libs/bzip2-$(BZIP2_VERSION) +bzip2: $(BUILD_PATH)/bzip2-$(BZIP2_VERSION) $(DOWNLOAD_PATH)/bzip2-$(BZIP2_VERSION).tar.gz: wget http://bzip.org/$(BZIP2_VERSION)/bzip2-$(BZIP2_VERSION).tar.gz -O $(DOWNLOAD_PATH)/bzip2-$(BZIP2_VERSION).tar.gz -libs/bzip2-$(BZIP2_VERSION): $(DOWNLOAD_PATH)/bzip2-$(BZIP2_VERSION).tar.gz +$(BUILD_PATH)/bzip2-$(BZIP2_VERSION): $(DOWNLOAD_PATH)/bzip2-$(BZIP2_VERSION).tar.gz # prepare - rm -r -f libs/bzip2-* + rm -r -f $(BUILD_PATH)/bzip2-* tar xvf $(DOWNLOAD_PATH)/bzip2-$(BZIP2_VERSION).tar.gz # build - #cd bzip2-$(BZIP2_VERSION) && make install PREFIX="`pwd`/../libs" + #cd bzip2-$(BZIP2_VERSION) && make install PREFIX="`pwd`/../$(BUILD_PATH)" cd bzip2-$(BZIP2_VERSION) && make # copy files - mkdir -p libs/bzip2-$(BZIP2_VERSION).tmp/include - cp bzip2-$(BZIP2_VERSION)/bzlib.h libs/bzip2-$(BZIP2_VERSION).tmp/include/ - mkdir -p libs/bzip2-$(BZIP2_VERSION).tmp/lib - cp bzip2-$(BZIP2_VERSION)/libbz2.a libs/bzip2-$(BZIP2_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/bzip2-$(BZIP2_VERSION).tmp/include + cp bzip2-$(BZIP2_VERSION)/bzlib.h $(BUILD_PATH)/bzip2-$(BZIP2_VERSION).tmp/include/ + mkdir -p $(BUILD_PATH)/bzip2-$(BZIP2_VERSION).tmp/lib + cp bzip2-$(BZIP2_VERSION)/libbz2.a $(BUILD_PATH)/bzip2-$(BZIP2_VERSION).tmp/lib/ + # cleanup rm -r -f bzip2-$(BZIP2_VERSION) - mv libs/bzip2-$(BZIP2_VERSION).tmp libs/bzip2-$(BZIP2_VERSION) + mv $(BUILD_PATH)/bzip2-$(BZIP2_VERSION).tmp $(BUILD_PATH)/bzip2-$(BZIP2_VERSION) -miniupnpc: libs/miniupnpc-$(MINIUPNPC_VERSION) +miniupnpc: $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION) $(DOWNLOAD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tar.gz: wget http://miniupnp.free.fr/files/download.php?file=miniupnpc-$(MINIUPNPC_VERSION).tar.gz -O $(DOWNLOAD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tar.gz -libs/miniupnpc-$(MINIUPNPC_VERSION): $(DOWNLOAD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tar.gz +$(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION): $(DOWNLOAD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tar.gz # prepare - rm -r -f libs/miniupnpc-* + rm -r -f $(BUILD_PATH)/miniupnpc-* tar xvf $(DOWNLOAD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tar.gz # build cd miniupnpc-$(MINIUPNPC_VERSION) && export CC=gcc && make -f Makefile.mingw init libminiupnpc.a miniupnpc.dll # copy files - mkdir -p libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/include/miniupnpc - cp miniupnpc-$(MINIUPNPC_VERSION)/*.h libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/include/miniupnpc/ - mkdir -p libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/lib - cp miniupnpc-$(MINIUPNPC_VERSION)/miniupnpc.lib libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/lib/ - mkdir -p libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/bin - cp miniupnpc-$(MINIUPNPC_VERSION)/miniupnpc.dll libs/miniupnpc-$(MINIUPNPC_VERSION).tmp/bin/ + mkdir -p $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/include/miniupnpc + cp miniupnpc-$(MINIUPNPC_VERSION)/*.h $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/include/miniupnpc/ + mkdir -p $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/lib + cp miniupnpc-$(MINIUPNPC_VERSION)/miniupnpc.lib $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/bin + cp miniupnpc-$(MINIUPNPC_VERSION)/miniupnpc.dll $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp/bin/ + # cleanup rm -r -f miniupnpc-$(MINIUPNPC_VERSION) - mv libs/miniupnpc-$(MINIUPNPC_VERSION).tmp libs/miniupnpc-$(MINIUPNPC_VERSION) + mv $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION).tmp $(BUILD_PATH)/miniupnpc-$(MINIUPNPC_VERSION) -openssl: libs/openssl-$(OPENSSL_VERSION) +openssl: $(BUILD_PATH)/openssl-$(OPENSSL_VERSION) $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar.gz: wget --no-check-certificate https://www.openssl.org/source/openssl-$(OPENSSL_VERSION).tar.gz -O $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar.gz -libs/openssl-$(OPENSSL_VERSION): $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar.gz +$(BUILD_PATH)/openssl-$(OPENSSL_VERSION): $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar.gz # prepare - rm -r -f libs/openssl-* + rm -r -f $(BUILD_PATH)/openssl-* tar xvf $(DOWNLOAD_PATH)/openssl-$(OPENSSL_VERSION).tar.gz # build - #cd openssl-$(OPENSSL_VERSION) && ./config --prefix="`pwd`/../libs" + #cd openssl-$(OPENSSL_VERSION) && ./config --prefix="`pwd`/../$(BUILD_PATH)" #cd openssl-$(OPENSSL_VERSION) && make install cd openssl-$(OPENSSL_VERSION) && ./config shared cd openssl-$(OPENSSL_VERSION) && make # copy files - mkdir -p libs/openssl-$(OPENSSL_VERSION).tmp/include/openssl - cp openssl-$(OPENSSL_VERSION)/include/openssl/*.h libs/openssl-$(OPENSSL_VERSION).tmp/include/openssl/ - mkdir -p libs/openssl-$(OPENSSL_VERSION).tmp/bin - cp openssl-$(OPENSSL_VERSION)/libeay32.dll libs/openssl-$(OPENSSL_VERSION).tmp/bin/ - cp openssl-$(OPENSSL_VERSION)/ssleay32.dll libs/openssl-$(OPENSSL_VERSION).tmp/bin/ - mkdir -p libs/openssl-$(OPENSSL_VERSION).tmp/lib - cp openssl-$(OPENSSL_VERSION)/libcrypto.dll.a libs/openssl-$(OPENSSL_VERSION).tmp/lib/ - cp openssl-$(OPENSSL_VERSION)/libssl.dll.a libs/openssl-$(OPENSSL_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/include/openssl + cp openssl-$(OPENSSL_VERSION)/include/openssl/*.h $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/include/openssl/ + mkdir -p $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/bin + cp openssl-$(OPENSSL_VERSION)/libeay32.dll $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/bin/ + cp openssl-$(OPENSSL_VERSION)/ssleay32.dll $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/bin/ + mkdir -p $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/lib + cp openssl-$(OPENSSL_VERSION)/libcrypto.dll.a $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/lib/ + cp openssl-$(OPENSSL_VERSION)/libssl.dll.a $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp/lib/ + # cleanup rm -r -f openssl-$(OPENSSL_VERSION) - mv libs/openssl-$(OPENSSL_VERSION).tmp libs/openssl-$(OPENSSL_VERSION) + mv $(BUILD_PATH)/openssl-$(OPENSSL_VERSION).tmp $(BUILD_PATH)/openssl-$(OPENSSL_VERSION) -speex: libs/speex-$(SPEEX_VERSION) +speex: $(BUILD_PATH)/speex-$(SPEEX_VERSION) $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz: wget --no-check-certificate http://downloads.xiph.org/releases/speex/speex-$(SPEEX_VERSION).tar.gz -O $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz -libs/speex-$(SPEEX_VERSION): $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz +$(BUILD_PATH)/speex-$(SPEEX_VERSION): $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz # prepare - rm -r -f libs/speex-* + rm -r -f $(BUILD_PATH)/speex-* tar xvf $(DOWNLOAD_PATH)/speex-$(SPEEX_VERSION).tar.gz # build cd speex-$(SPEEX_VERSION) && ./configure - #cd speex-$(SPEEX_VERSION) && make install exec_prefix="`pwd`/../libs" + #cd speex-$(SPEEX_VERSION) && make install exec_prefix="`pwd`/../$(BUILD_PATH)" cd speex-$(SPEEX_VERSION) && make # copy files - mkdir -p libs/speex-$(SPEEX_VERSION).tmp/include/speex - cp speex-$(SPEEX_VERSION)/include/speex/*.h libs/speex-$(SPEEX_VERSION).tmp/include/speex/ - mkdir -p libs/speex-$(SPEEX_VERSION).tmp/lib - cp speex-$(SPEEX_VERSION)/libspeex/.libs/libspeex.a libs/speex-$(SPEEX_VERSION).tmp/lib + mkdir -p $(BUILD_PATH)/speex-$(SPEEX_VERSION).tmp/include/speex + cp speex-$(SPEEX_VERSION)/include/speex/*.h $(BUILD_PATH)/speex-$(SPEEX_VERSION).tmp/include/speex/ + mkdir -p $(BUILD_PATH)/speex-$(SPEEX_VERSION).tmp/lib + cp speex-$(SPEEX_VERSION)/libspeex/.libs/libspeex.a $(BUILD_PATH)/speex-$(SPEEX_VERSION).tmp/lib + # cleanup rm -r -f speex-$(SPEEX_VERSION) - mv libs/speex-$(SPEEX_VERSION).tmp libs/speex-$(SPEEX_VERSION) + mv $(BUILD_PATH)/speex-$(SPEEX_VERSION).tmp $(BUILD_PATH)/speex-$(SPEEX_VERSION) -speexdsp: libs/speexdsp-$(SPEEXDSP_VERSION) +speexdsp: $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION) $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz: wget --no-check-certificate http://downloads.xiph.org/releases/speex/speexdsp-$(SPEEXDSP_VERSION).tar.gz -O $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz -libs/speexdsp-$(SPEEXDSP_VERSION): $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz +$(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION): $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz # prepare - rm -r -f libs/speexdsp-* + rm -r -f $(BUILD_PATH)/speexdsp-* tar xvf $(DOWNLOAD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tar.gz # build cd speexdsp-$(SPEEXDSP_VERSION) && ./configure cd speexdsp-$(SPEEXDSP_VERSION) && make # copy files - mkdir -p libs/speexdsp-$(SPEEXDSP_VERSION).tmp/include/speex - cp speexdsp-$(SPEEXDSP_VERSION)/include/speex/*.h libs/speexdsp-$(SPEEXDSP_VERSION).tmp/include/speex/ - mkdir -p libs/speexdsp-$(SPEEXDSP_VERSION).tmp/lib - cp speexdsp-$(SPEEXDSP_VERSION)/libspeexdsp/.libs/libspeexdsp.a libs/speexdsp-$(SPEEXDSP_VERSION).tmp/lib + mkdir -p $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tmp/include/speex + cp speexdsp-$(SPEEXDSP_VERSION)/include/speex/*.h $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tmp/include/speex/ + mkdir -p $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tmp/lib + cp speexdsp-$(SPEEXDSP_VERSION)/libspeexdsp/.libs/libspeexdsp.a $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tmp/lib + # cleanup rm -r -f speexdsp-$(SPEEXDSP_VERSION) - mv libs/speexdsp-$(SPEEXDSP_VERSION).tmp libs/speexdsp-$(SPEEXDSP_VERSION) + mv $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION).tmp $(BUILD_PATH)/speexdsp-$(SPEEXDSP_VERSION) -opencv: libs/opencv-$(OPENCV_VERSION) +opencv: $(BUILD_PATH)/opencv-$(OPENCV_VERSION) $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz: wget --no-check-certificate https://github.com/opencv/opencv/archive/$(OPENCV_VERSION).tar.gz -O $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz -libs/opencv-$(OPENCV_VERSION): $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz +$(BUILD_PATH)/opencv-$(OPENCV_VERSION): $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz # prepare - rm -r -f libs/opencv-* + rm -r -f $(BUILD_PATH)/opencv-* tar xvf $(DOWNLOAD_PATH)/opencv-$(OPENCV_VERSION).tar.gz # build mkdir -p opencv-$(OPENCV_VERSION)/build - #cd opencv-$(OPENCV_VERSION)/build && cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="`pwd`/../../libs" + #cd opencv-$(OPENCV_VERSION)/build && cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="`pwd`/../../build" cd opencv-$(OPENCV_VERSION)/build && cmake .. -G"MSYS Makefiles" -DCMAKE_BUILD_TYPE=Release -DBUILD_PERF_TESTS=OFF -DBUILD_TESTS=OFF -DBUILD_SHARED_LIBS=OFF -DENABLE_CXX11=ON -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS} -DSTRSAFE_NO_DEPRECATE" -DCMAKE_INSTALL_PREFIX="`pwd`/install" cd opencv-$(OPENCV_VERSION)/build && make install # copy files - mkdir -p libs/opencv-$(OPENCV_VERSION).tmp/include - cp -r opencv-$(OPENCV_VERSION)/build/install/include/* libs/opencv-$(OPENCV_VERSION).tmp/include/ - mkdir -p libs/opencv-$(OPENCV_VERSION).tmp/lib/opencv - cp -r opencv-$(OPENCV_VERSION)/build/install/x86/mingw/staticlib/* libs/opencv-$(OPENCV_VERSION).tmp/lib/opencv/ + mkdir -p $(BUILD_PATH)/opencv-$(OPENCV_VERSION).tmp/include + cp -r opencv-$(OPENCV_VERSION)/build/install/include/* $(BUILD_PATH)/opencv-$(OPENCV_VERSION).tmp/include/ + mkdir -p $(BUILD_PATH)/opencv-$(OPENCV_VERSION).tmp/lib/opencv + cp -r opencv-$(OPENCV_VERSION)/build/install/x86/mingw/staticlib/* $(BUILD_PATH)/opencv-$(OPENCV_VERSION).tmp/lib/opencv/ + # cleanup rm -r -f opencv-$(OPENCV_VERSION) - mv libs/opencv-$(OPENCV_VERSION).tmp libs/opencv-$(OPENCV_VERSION) + mv $(BUILD_PATH)/opencv-$(OPENCV_VERSION).tmp $(BUILD_PATH)/opencv-$(OPENCV_VERSION) -libxml2: libs/libxml2-$(LIBXML2_VERSION) +libxml2: $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION) $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz: wget ftp://xmlsoft.org/libxml2/libxml2-$(LIBXML2_VERSION).tar.gz -O $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz -libs/libxml2-$(LIBXML2_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz +$(BUILD_PATH)/libxml2-$(LIBXML2_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz # prepare - rm -r -f libs/libxml2-* + rm -r -f $(BUILD_PATH)/libxml2-* tar xvf $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz # build cd libxml2-$(LIBXML2_VERSION) && ./configure --without-iconv -enable-shared=no - #cd libxml2-$(LIBXML2_VERSION) && make install exec_prefix="`pwd`/../libs" + #cd libxml2-$(LIBXML2_VERSION) && make install exec_prefix="`pwd`/../$(BUILD_PATH)" cd libxml2-$(LIBXML2_VERSION) && make # copy files - mkdir -p libs/libxml2-$(LIBXML2_VERSION).tmp/include/libxml - cp libxml2-$(LIBXML2_VERSION)/include/libxml/*.h libs/libxml2-$(LIBXML2_VERSION).tmp/include/libxml/ - mkdir -p libs/libxml2-$(LIBXML2_VERSION).tmp/lib - cp libxml2-$(LIBXML2_VERSION)/.libs/libxml2.a libs/libxml2-$(LIBXML2_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION).tmp/include/libxml + cp libxml2-$(LIBXML2_VERSION)/include/libxml/*.h $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION).tmp/include/libxml/ + mkdir -p $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION).tmp/lib + cp libxml2-$(LIBXML2_VERSION)/.libs/libxml2.a $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION).tmp/lib/ + # cleanup #rm -r -f libxml2-$(LIBXML2_VERSION) # see libxslt - mv libs/libxml2-$(LIBXML2_VERSION).tmp libs/libxml2-$(LIBXML2_VERSION) + mv $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION).tmp $(BUILD_PATH)/libxml2-$(LIBXML2_VERSION) -libxslt: libs/libxslt-$(LIBXSLT_VERSION) +libxslt: $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION) $(DOWNLOAD_PATH)/libxslt-$(LIBXSLT_VERSION).tar.gz: wget ftp://xmlsoft.org/libxml2/libxslt-$(LIBXSLT_VERSION).tar.gz -O $(DOWNLOAD_PATH)/libxslt-$(LIBXSLT_VERSION).tar.gz -libs/libxslt-$(LIBXSLT_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz $(DOWNLOAD_PATH)/libxslt-$(LIBXSLT_VERSION).tar.gz +$(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION): $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz $(DOWNLOAD_PATH)/libxslt-$(LIBXSLT_VERSION).tar.gz # prepare - rm -r -f libs/libxslt-* + rm -r -f $(BUILD_PATH)/libxslt-* tar xvf $(DOWNLOAD_PATH)/libxml2-$(LIBXML2_VERSION).tar.gz tar xvf $(DOWNLOAD_PATH)/libxslt-$(LIBXSLT_VERSION).tar.gz # build cd libxslt-$(LIBXSLT_VERSION) && ./configure --with-libxml-src=../libxml2-$(LIBXML2_VERSION) -enable-shared=no CFLAGS=-DLIBXML_STATIC cd libxslt-$(LIBXSLT_VERSION) && make # copy files - mkdir -p libs/libxslt-$(LIBXSLT_VERSION).tmp/include/libxslt - cp libxslt-$(LIBXSLT_VERSION)/libxslt/*.h libs/libxslt-$(LIBXSLT_VERSION).tmp/include/libxslt/ - mkdir -p libs/libxslt-$(LIBXSLT_VERSION).tmp/lib - cp libxslt-$(LIBXSLT_VERSION)/libxslt/.libs/libxslt.a libs/libxslt-$(LIBXSLT_VERSION).tmp/lib/ - cp libxslt-$(LIBXSLT_VERSION)/libexslt/.libs/libexslt.a libs/libxslt-$(LIBXSLT_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp/include/libxslt + cp libxslt-$(LIBXSLT_VERSION)/libxslt/*.h $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp/include/libxslt/ + mkdir -p $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp/lib + cp libxslt-$(LIBXSLT_VERSION)/libxslt/.libs/libxslt.a $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp/lib/ + cp libxslt-$(LIBXSLT_VERSION)/libexslt/.libs/libexslt.a $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp/lib/ + # cleanup rm -r -f libxml2-$(LIBXML2_VERSION) rm -r -f libxslt-$(LIBXSLT_VERSION) - mv libs/libxslt-$(LIBXSLT_VERSION).tmp libs/libxslt-$(LIBXSLT_VERSION) + mv $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION).tmp $(BUILD_PATH)/libxslt-$(LIBXSLT_VERSION) -curl: libs/curl-$(CURL_VERSION) +curl: $(BUILD_PATH)/curl-$(CURL_VERSION) $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz: wget --no-check-certificate http://curl.haxx.se/download/curl-$(CURL_VERSION).tar.gz -O $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz -libs/curl-$(CURL_VERSION): $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz +$(BUILD_PATH)/curl-$(CURL_VERSION): $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz # prepare - rm -r -f libs/curl-* + rm -r -f $(BUILD_PATH)/curl-* tar xvf $(DOWNLOAD_PATH)/curl-$(CURL_VERSION).tar.gz # build - cd curl-$(CURL_VERSION) && ./configure --disable-shared --with-ssl="`pwd`/../libs/openssl-$(OPENSSL_VERSION)" - #cd curl-$(CURL_VERSION) && make install exec_prefix="`pwd`/../libs" + cd curl-$(CURL_VERSION) && ./configure --disable-shared --with-ssl="`pwd`/../$(BUILD_PATH)/openssl-$(OPENSSL_VERSION)" + #cd curl-$(CURL_VERSION) && make install exec_prefix="`pwd`/../$(BUILD_PATH)" cd curl-$(CURL_VERSION) && make # copy files - mkdir -p libs/curl-$(CURL_VERSION).tmp/include/curl - cp curl-$(CURL_VERSION)/include/curl/*.h libs/curl-$(CURL_VERSION).tmp/include/curl/ - mkdir -p libs/curl-$(CURL_VERSION).tmp/lib - cp curl-$(CURL_VERSION)/lib/.libs/libcurl.a libs/curl-$(CURL_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/curl-$(CURL_VERSION).tmp/include/curl + cp curl-$(CURL_VERSION)/include/curl/*.h $(BUILD_PATH)/curl-$(CURL_VERSION).tmp/include/curl/ + mkdir -p $(BUILD_PATH)/curl-$(CURL_VERSION).tmp/lib + cp curl-$(CURL_VERSION)/lib/.libs/libcurl.a $(BUILD_PATH)/curl-$(CURL_VERSION).tmp/lib/ + # cleanup rm -r -f curl-$(CURL_VERSION) - mv libs/curl-$(CURL_VERSION).tmp libs/curl-$(CURL_VERSION) + mv $(BUILD_PATH)/curl-$(CURL_VERSION).tmp $(BUILD_PATH)/curl-$(CURL_VERSION) -sqlcipher: libs/sqlcipher-$(SQLCIPHER_VERSION) +sqlcipher: $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION) $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz: wget --no-check-certificate http://downloads.sourceforge.net/project/tcl/Tcl/$(TCL_VERSION)/tcl$(TCL_VERSION)-src.tar.gz -O $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz @@ -270,9 +281,9 @@ $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz: $(DOWNLOAD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tar.gz: wget --no-check-certificate https://github.com/sqlcipher/sqlcipher/archive/v$(SQLCIPHER_VERSION).tar.gz -O $(DOWNLOAD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tar.gz -libs/sqlcipher-$(SQLCIPHER_VERSION): $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz $(DOWNLOAD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tar.gz +$(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION): $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz $(DOWNLOAD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tar.gz # prepare - rm -r -f libs/sqlcipher-* + rm -r -f $(BUILD_PATH)/sqlcipher-* # tcl tar xvf $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar.gz mkdir -p tcl$(TCL_VERSION)/build @@ -284,59 +295,73 @@ libs/sqlcipher-$(SQLCIPHER_VERSION): $(DOWNLOAD_PATH)/tcl$(TCL_VERSION)-src.tar. mkdir -p tcl$(TCL_VERSION)/lib ln -s `pwd`/tcl$(TCL_VERSION)/library `pwd`/tcl$(TCL_VERSION)/lib/tcl8.6 # build - cd sqlcipher-$(SQLCIPHER_VERSION) && PATH=.:$$PATH:`pwd`/../tcl$(TCL_VERSION)/build && export LIBS="-L`pwd`/../libs/openssl-$(OPENSSL_VERSION)/lib -lgdi32 $$LIBS" && ./configure --disable-shared --enable-static --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC -I`pwd`/../libs/openssl-$(OPENSSL_VERSION)/include -I`pwd`/../tcl$(TCL_VERSION)/generic" LDFLAGS="-L`pwd`/../libs/openssl-$(OPENSSL_VERSION)/lib -lcrypto -lgdi32" --with-tcl="`pwd`/../tcl$(TCL_VERSION)/build" && make install prefix="`pwd`/install" + cd sqlcipher-$(SQLCIPHER_VERSION) && PATH=.:$$PATH:`pwd`/../tcl$(TCL_VERSION)/build && export LIBS="-L`pwd`/../$(BUILD_PATH)/openssl-$(OPENSSL_VERSION)/lib -lgdi32 $$LIBS" && ./configure --disable-shared --enable-static --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC -I`pwd`/../$(BUILD_PATH)/openssl-$(OPENSSL_VERSION)/include -I`pwd`/../tcl$(TCL_VERSION)/generic" LDFLAGS="-L`pwd`/../$(BUILD_PATH)/openssl-$(OPENSSL_VERSION)/lib -lcrypto -lgdi32" --with-tcl="`pwd`/../tcl$(TCL_VERSION)/build" && make install prefix="`pwd`/install" # copy files - mkdir -p libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/include - cp -r sqlcipher-$(SQLCIPHER_VERSION)/install/include/* libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/include/ - mkdir -p libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/lib - cp sqlcipher-$(SQLCIPHER_VERSION)/install/lib/libsqlcipher.a libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/lib/ - mkdir -p libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/bin - cp sqlcipher-$(SQLCIPHER_VERSION)/install/bin/sqlcipher.exe libs/sqlcipher-$(SQLCIPHER_VERSION).tmp/bin/ + mkdir -p $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/include + cp -r sqlcipher-$(SQLCIPHER_VERSION)/install/include/* $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/include/ + mkdir -p $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/lib + cp sqlcipher-$(SQLCIPHER_VERSION)/install/lib/libsqlcipher.a $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/lib/ + mkdir -p $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/bin + cp sqlcipher-$(SQLCIPHER_VERSION)/install/bin/sqlcipher.exe $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp/bin/ + # cleanup rm -r -f sqlcipher-$(SQLCIPHER_VERSION) rm -r -f tcl$(TCL_VERSION) - mv libs/sqlcipher-$(SQLCIPHER_VERSION).tmp libs/sqlcipher-$(SQLCIPHER_VERSION) + mv $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION).tmp $(BUILD_PATH)/sqlcipher-$(SQLCIPHER_VERSION) -libmicrohttpd: libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION) +libmicrohttpd: $(BUILD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION) $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz: wget --no-check-certificate http://ftp.gnu.org/gnu/libmicrohttpd/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz -O $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz -libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION): $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz +$(BUILD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION): $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz # prepare - rm -r -f libs/libmicrohttpd-* + rm -r -f $(BUILD_PATH)/libmicrohttpd-* tar xvf $(DOWNLOAD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tar.gz # build - cd libmicrohttpd-$(LIBMICROHTTPD_VERSION) && ./configure --disable-shared --enable-static --prefix="`pwd`/../libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tmp" + cd libmicrohttpd-$(LIBMICROHTTPD_VERSION) && ./configure --disable-shared --enable-static --prefix="`pwd`/../$(BUILD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tmp" cd libmicrohttpd-$(LIBMICROHTTPD_VERSION) && make install - # copy files + # cleanup rm -r -f libmicrohttpd-$(LIBMICROHTTPD_VERSION) - mv libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tmp libs/libmicrohttpd-$(LIBMICROHTTPD_VERSION) + mv $(BUILD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION).tmp $(BUILD_PATH)/libmicrohttpd-$(LIBMICROHTTPD_VERSION) -ffmpeg: libs/ffmpeg-$(FFMPEG_VERSION) +ffmpeg: $(BUILD_PATH)/ffmpeg-$(FFMPEG_VERSION) $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz: wget --no-check-certificate https://ffmpeg.org/releases/ffmpeg-$(FFMPEG_VERSION).tar.gz -O $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz -libs/ffmpeg-$(FFMPEG_VERSION): $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz +$(BUILD_PATH)/ffmpeg-$(FFMPEG_VERSION): $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz # prepare - rm -r -f libs/ffmpeg-* + rm -r -f $(BUILD_PATH)/ffmpeg-* tar xvf $(DOWNLOAD_PATH)/ffmpeg-$(FFMPEG_VERSION).tar.gz # build - cd ffmpeg-$(FFMPEG_VERSION) && ./configure --disable-shared --enable-static --disable-programs --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc --disable-htmlpages --disable-manpages --disable-podpages --disable-txtpages --disable-yasm --disable-everything --enable-encoder=mpeg4 --enable-decoder=mpeg4 --prefix="`pwd`/../libs/ffmpeg-$(FFMPEG_VERSION).tmp" + cd ffmpeg-$(FFMPEG_VERSION) && ./configure --disable-shared --enable-static --disable-programs --disable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver --disable-doc --disable-htmlpages --disable-manpages --disable-podpages --disable-txtpages --disable-yasm --disable-everything --enable-encoder=mpeg4 --enable-decoder=mpeg4 --prefix="`pwd`/../$(BUILD_PATH)/ffmpeg-$(FFMPEG_VERSION).tmp" cd ffmpeg-$(FFMPEG_VERSION) && make install - # copy files + # cleanup rm -r -f ffmpeg-$(FFMPEG_VERSION) - mv libs/ffmpeg-$(FFMPEG_VERSION).tmp libs/ffmpeg-$(FFMPEG_VERSION) + mv $(BUILD_PATH)/ffmpeg-$(FFMPEG_VERSION).tmp $(BUILD_PATH)/ffmpeg-$(FFMPEG_VERSION) + +rapidjson: $(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION) + +$(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz: + wget --no-check-certificate https://github.com/Tencent/rapidjson/archive/v$(RAPIDJSON_VERSION).tar.gz -O $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz + +$(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION): $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz + # prepare + rm -r -f $(BUILD_PATH)/rapidjson-* + tar xvf $(DOWNLOAD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tar.gz + # build + #mkdir -p rapidjson-$(RAPIDJSON_VERSION)/build + #cd rapidjson-$(RAPIDJSON_VERSION)/build && cmake .. -G"MSYS Makefiles" + #cd rapidjson-$(RAPIDJSON_VERSION)/build && make + # copy files + mkdir -p $(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tmp/include + cp -r rapidjson-$(RAPIDJSON_VERSION)/include/* $(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tmp/include/ + # cleanup + rm -r -f rapidjson-$(RAPIDJSON_VERSION) + mv $(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION).tmp $(BUILD_PATH)/rapidjson-$(RAPIDJSON_VERSION) copylibs: - if [ "$(COPY_ANSWER)" = "" ] ; then \ - read -p "Do you want to copy libs to retroshare? (y|n)" answer; \ - else \ - answer=$(COPY_ANSWER) ; \ - fi ; \ - if [ "$$answer" = "y" ] ; then \ - rm -r -f $(LIBS_PATH) ; \ - mkdir -p $(LIBS_PATH) ; \ - cp ./libs/gcc-version $(LIBS_PATH) ; \ - find ./libs -mindepth 1 -maxdepth 1 -type d -not -name "*.tmp" -print -exec cp -r {}/. $(LIBS_PATH) \; ; \ - fi + rm -r -f $(LIBS_PATH) ; \ + mkdir -p $(LIBS_PATH) ; \ + cp $(BUILD_PATH)/gcc-version $(LIBS_PATH) ; \ + find $(BUILD_PATH) -mindepth 1 -maxdepth 1 -type d -not -name "*.tmp" -print -exec cp -r {}/. $(LIBS_PATH) \; ; \ diff --git a/build_scripts/Windows/build-libs/build-libs.bat b/build_scripts/Windows/build-libs/build-libs.bat index 58b400a2f..e0c5af09b 100644 --- a/build_scripts/Windows/build-libs/build-libs.bat +++ b/build_scripts/Windows/build-libs/build-libs.bat @@ -1,5 +1,5 @@ :: Usage: -:: call build-libs.bat [auto-copy] [make tasks] +:: call build-libs.bat [make tasks] @echo off @@ -7,7 +7,6 @@ setlocal :: Parameter set MakeParam="DOWNLOAD_PATH=../download" -if "%~1"=="auto-copy" set MakeParam=%MakeParam% "COPY_ANSWER=y"& shift /1 set MakeTask= :param_loop @@ -30,6 +29,9 @@ if not exist "%EnvMSYSSH%" %cecho% error "Please install MSYS first." & exit /B call "%~dp0env.bat" if errorlevel 1 goto error_env +:: Add tools path to PATH environment +set PATH=%EnvToolsPath%;%PATH% + call "%ToolsPath%\msys-path.bat" "%~dp0" MSYSCurPath call "%ToolsPath%\msys-path.bat" "%BuildLibsPath%" MSYSBuildLibsPath diff --git a/build_scripts/Windows/build-retrotor.bat b/build_scripts/Windows/build-retrotor.bat index cbf21e1c0..f00014187 100644 --- a/build_scripts/Windows/build-retrotor.bat +++ b/build_scripts/Windows/build-retrotor.bat @@ -9,7 +9,7 @@ call "%EnvPath%\env.bat" if errorlevel 1 goto error_env %cecho% info "Build libraries" -call "%~dp0build-libs\build-libs.bat" auto-copy +call "%~dp0build-libs\build-libs.bat" if errorlevel 1 %cecho% error "Failed to build libraries." & exit /B %ERRORLEVEL% %cecho% info "Build %SourceName%" diff --git a/build_scripts/Windows/build.bat b/build_scripts/Windows/build.bat index 22052a3e1..642f21454 100644 --- a/build_scripts/Windows/build.bat +++ b/build_scripts/Windows/build.bat @@ -9,7 +9,7 @@ call "%EnvPath%\env.bat" if errorlevel 1 goto error_env %cecho% info "Build libraries" -call "%~dp0build-libs\build-libs.bat" auto-copy +call "%~dp0build-libs\build-libs.bat" if errorlevel 1 %cecho% error "Failed to build libraries." & exit /B %ERRORLEVEL% %cecho% info "Build %SourceName%" diff --git a/build_scripts/Windows/build/build-installer.bat b/build_scripts/Windows/build/build-installer.bat index 40c408c62..c986adbba 100644 --- a/build_scripts/Windows/build/build-installer.bat +++ b/build_scripts/Windows/build/build-installer.bat @@ -8,22 +8,19 @@ if errorlevel 1 goto error_env call "%EnvPath%\env.bat" if errorlevel 1 goto error_env -:: Get gcc versions -call "%ToolsPath%\get-gcc-version.bat" GCCVersion -if "%GCCVersion%"=="" echo Cannot get gcc version.& exit /B 1 - -:: Check external libraries -if not exist "%RootPath%\libs" echo Please build external libraries first.& exit /B 1 - -:: Check gcc version of external libraries -if not exist "%RootPath%\libs\gcc-version" echo Cannot get gcc version of external libraries.& exit /B 1 -set /P LibsGCCVersion=<"%RootPath%\libs\gcc-version" -if "%LibsGCCVersion%" NEQ "%GCCVersion%" echo Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%).& exit /B 1 - :: Initialize environment call "%~dp0env.bat" standard +if errorlevel 2 exit /B 2 if errorlevel 1 goto error_env +:: Check external libraries +if not exist "%BuildLibsPath%\libs" %cecho% error "Please build external libraries first." & exit /B 1 + +:: Check gcc version of external libraries +if not exist "%BuildLibsPath%\libs\gcc-version" %cecho% error "Cannot get gcc version of external libraries." & exit /B 1 +set /P LibsGCCVersion=<"%BuildLibsPath%\libs\gcc-version" +if "%LibsGCCVersion%" NEQ "%GCCVersion%" %cecho% error "Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%)." & exit /B 1 + :: Build defines for script set NSIS_PARAM= diff --git a/build_scripts/Windows/build/build.bat b/build_scripts/Windows/build/build.bat index 68e00235b..3fa8fb49a 100644 --- a/build_scripts/Windows/build/build.bat +++ b/build_scripts/Windows/build/build.bat @@ -8,22 +8,19 @@ if errorlevel 1 goto error_env call "%EnvPath%\env.bat" if errorlevel 1 goto error_env -:: Get gcc versions -call "%ToolsPath%\get-gcc-version.bat" GCCVersion -if "%GCCVersion%"=="" echo Cannot get gcc version.& exit /B 1 - -:: Check external libraries -if not exist "%RootPath%\libs" echo Please build external libraries first.& exit /B 1 - -:: Check gcc version of external libraries -if not exist "%RootPath%\libs\gcc-version" echo Cannot get gcc version of external libraries.& exit /B 1 -set /P LibsGCCVersion=<"%RootPath%\libs\gcc-version" -if "%LibsGCCVersion%" NEQ "%GCCVersion%" echo Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%).& exit /B 1 - :: Initialize environment call "%~dp0env.bat" %* +if errorlevel 2 exit /B 2 if errorlevel 1 goto error_env +:: Check external libraries +if not exist "%BuildLibsPath%\libs" %cecho% error "Please build external libraries first." & exit /B 1 + +:: Check gcc version of external libraries +if not exist "%BuildLibsPath%\libs\gcc-version" %cecho% error "Cannot get gcc version of external libraries." & exit /B 1 +set /P LibsGCCVersion=<"%BuildLibsPath%\libs\gcc-version" +if "%LibsGCCVersion%" NEQ "%GCCVersion%" %cecho% error "Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%)." & exit /B 1 + :: Check git executable set GitPath= call "%ToolsPath%\find-in-path.bat" GitPath git.exe @@ -55,7 +52,7 @@ title Build - %SourceName%%RsType%-%RsBuildConfig% [qmake] set RS_QMAKE_CONFIG=%RsBuildConfig% version_detail_bash_script rs_autologin retroshare_plugins if "%RsRetroTor%"=="1" set RS_QMAKE_CONFIG=%RS_QMAKE_CONFIG% retrotor -qmake "%SourcePath%\RetroShare.pro" -r "CONFIG+=%RS_QMAKE_CONFIG%" +qmake "%SourcePath%\RetroShare.pro" -r -spec win32-g++ "CONFIG+=%RS_QMAKE_CONFIG%" "EXTERNAL_LIB_DIR=%BuildLibsPath%\libs" if errorlevel 1 goto error echo. diff --git a/build_scripts/Windows/build/env.bat b/build_scripts/Windows/build/env.bat index 094b1b686..b0c765e03 100644 --- a/build_scripts/Windows/build/env.bat +++ b/build_scripts/Windows/build/env.bat @@ -9,7 +9,7 @@ if "%~1"=="standard" ( echo. echo Usage: standard^|retrotor echo. - exit /B 1 + exit /B 2 ) ) @@ -22,16 +22,22 @@ if not exist "%DeployPath%" mkdir "%DeployPath%" :: Check Qt environment set QtPath= call "%ToolsPath%\find-in-path.bat" QtPath qmake.exe -if "%QtPath%"=="" echo Please run command in the Qt Command Prompt.& exit /B 1 +if "%QtPath%"=="" %cecho% error "Please run command in the Qt Command Prompt." & exit /B 1 :: Check MinGW environment set MinGWPath= call "%ToolsPath%\find-in-path.bat" MinGWPath gcc.exe -if "%MinGWPath%"=="" echo Please run command in the Qt Command Prompt.& exit /B 1 +if "%MinGWPath%"=="" %cecho% error "Please run command in the Qt Command Prompt." & exit /B 1 :: Get Qt version call "%ToolsPath%\get-qt-version.bat" QtVersion -if "%QtVersion%"=="" echo Cannot get Qt version.& exit /B 1 +if "%QtVersion%"=="" %cecho% error "Cannot get Qt version." & exit /B 1 + +:: Get gcc versions +call "%ToolsPath%\get-gcc-version.bat" GCCVersion +if "%GCCVersion%"=="" %cecho% error "Cannot get gcc version." & exit /B 1 + +set BuildLibsPath=%EnvRootPath%\build-libs\gcc-%GCCVersion% set RsBuildConfig=release set RsBuildPath=%BuildPath%\Qt-%QtVersion%%RsType%-%RsBuildConfig% diff --git a/build_scripts/Windows/build/pack.bat b/build_scripts/Windows/build/pack.bat index 215b3c402..8122cc2e0 100644 --- a/build_scripts/Windows/build/pack.bat +++ b/build_scripts/Windows/build/pack.bat @@ -10,22 +10,19 @@ if errorlevel 1 goto error_env call "%EnvPath%\env.bat" if errorlevel 1 goto error_env -:: Get gcc versions -call "%ToolsPath%\get-gcc-version.bat" GCCVersion -if "%GCCVersion%"=="" echo Cannot get gcc version.& exit /B 1 - -:: Check external libraries -if not exist "%RootPath%\libs" echo Please build external libraries first.& exit /B 1 - -:: Check gcc version of external libraries -if not exist "%RootPath%\libs\gcc-version" echo Cannot get gcc version of external libraries.& exit /B 1 -set /P LibsGCCVersion=<"%RootPath%\libs\gcc-version" -if "%LibsGCCVersion%" NEQ "%GCCVersion%" echo Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%).& exit /B 1 - :: Initialize environment call "%~dp0env.bat" %* +if errorlevel 2 exit /B 2 if errorlevel 1 goto error_env +:: Check external libraries +if not exist "%BuildLibsPath%\libs" %cecho% error "Please build external libraries first." & exit /B 1 + +:: Check gcc version of external libraries +if not exist "%BuildLibsPath%\libs\gcc-version" %cecho% error "Cannot get gcc version of external libraries." & exit /B 1 +set /P LibsGCCVersion=<"%BuildLibsPath%\libs\gcc-version" +if "%LibsGCCVersion%" NEQ "%GCCVersion%" %cecho% error "Please use correct version of external libraries. (gcc %GCCVersion% ^<^> libs %LibsGCCVersion%)." & exit /B 1 + :: Remove deploy path if exist "%RsDeployPath%" rmdir /S /Q "%RsDeployPath%" diff --git a/build_scripts/Windows/env/tools/prepare-msys.bat b/build_scripts/Windows/env/tools/prepare-msys.bat index af9b9bf78..23dd391f4 100644 --- a/build_scripts/Windows/env/tools/prepare-msys.bat +++ b/build_scripts/Windows/env/tools/prepare-msys.bat @@ -52,7 +52,8 @@ mingw-get.exe install msys-autoconf mingw-get.exe install msys-automake mingw-get.exe install msys-autogen mingw-get.exe install msys-mktemp -mingw-get.exe install msys-wget +rem Use own wget binary, because MSYS version of wget is to old +rem mingw-get.exe install msys-wget popd %cecho% info "Unpack CMake" diff --git a/build_scripts/Windows/env/tools/prepare-tools.bat b/build_scripts/Windows/env/tools/prepare-tools.bat index 2295d0ca3..587327f0e 100644 --- a/build_scripts/Windows/env/tools/prepare-tools.bat +++ b/build_scripts/Windows/env/tools/prepare-tools.bat @@ -4,8 +4,8 @@ if "%EnvRootPath%"=="" exit /B 1 set CEchoUrl=https://github.com/lordmulder/cecho/releases/download/2015-10-10/cecho.2015-10-10.zip set CEchoInstall=cecho.2015-10-10.zip -set SevenZipUrl=http://7-zip.org/a/7z1602.msi -set SevenZipInstall=7z1602.msi +set SevenZipUrl=https://sourceforge.net/projects/sevenzip/files/7-Zip/18.05/7z1805.msi/download +set SevenZipInstall=7z1805.msi ::set CurlUrl=https://bintray.com/artifact/download/vszakats/generic/curl-7.50.1-win32-mingw.7z ::set CurlInstall=curl-7.50.1-win32-mingw.7z set WgetUrl=https://eternallybored.org/misc/wget/1.19.4/32/wget.exe @@ -20,13 +20,23 @@ set NSISUrl=http://prdownloads.sourceforge.net/nsis/nsis-3.0-setup.exe?download set NSISInstall=nsis-3.0-setup.exe set NSISInstallPath=%EnvToolsPath%\NSIS +if not exist "%EnvToolsPath%\wget.exe" ( + echo Download Wget installation + + if not exist "%EnvDownloadPath%\%WgetInstall%" call "%ToolsPath%\winhttpjs.bat" %WgetUrl% -saveTo "%EnvDownloadPath%\%WgetInstall%" + if not exist "%EnvDownloadPath%\%WgetInstall%" %cecho% error "Cannot download Wget installation" & goto error + + echo Copy Wget + copy "%EnvDownloadPath%\wget.exe" "%EnvToolsPath%" +) + if not exist "%EnvToolsPath%\7z.exe" ( call "%ToolsPath%\remove-dir.bat" "%EnvTempPath%" mkdir "%EnvTempPath%" echo Download 7z installation - if not exist "%EnvDownloadPath%\%SevenZipInstall%" call "%ToolsPath%\winhttpjs.bat" %SevenZipUrl% -saveTo "%EnvDownloadPath%\%SevenZipInstall%" + if not exist "%EnvDownloadPath%\%SevenZipInstall%" call "%ToolsPath%\download-file.bat" "%SevenZipUrl%" "%EnvDownloadPath%\%SevenZipInstall%" if not exist "%EnvDownloadPath%\%SevenZipInstall%" echo Cannot download 7z installation& goto error echo Unpack 7z @@ -69,16 +79,6 @@ if not exist "%EnvToolsPath%\cecho.exe" ( :: call "%ToolsPath%\remove-dir.bat" "%EnvTempPath%" ::) -if not exist "%EnvToolsPath%\wget.exe" ( - %cecho% info "Download Wget installation" - - if not exist "%EnvDownloadPath%\%WgetInstall%" call "%ToolsPath%\winhttpjs.bat" %WgetUrl% -saveTo "%EnvDownloadPath%\%WgetInstall%" - if not exist "%EnvDownloadPath%\%WgetInstall%" %cecho% error "Cannot download Wget installation" & goto error - - %cecho% info "Copy Wget" - copy "%EnvDownloadPath%\wget.exe" "%EnvToolsPath%" -) - if not exist "%EnvToolsPath%\jom.exe" ( call "%ToolsPath%\remove-dir.bat" "%EnvTempPath%" mkdir "%EnvTempPath%" diff --git a/build_scripts/Windows/tools/download-file.bat b/build_scripts/Windows/tools/download-file.bat index d42349ccb..b0e93be5f 100644 --- a/build_scripts/Windows/tools/download-file.bat +++ b/build_scripts/Windows/tools/download-file.bat @@ -8,6 +8,6 @@ if "%~2"=="" ( ) ::"%EnvCurlExe%" -L -k "%~1" -o "%~2" -"%EnvWgetExe%" --continue "%~1" --output-document="%~2" +"%EnvWgetExe%" --no-check-certificate --continue "%~1" --output-document="%~2" exit /B %ERRORLEVEL% diff --git a/libbitdht/src/bitdht/bdmanager.cc b/libbitdht/src/bitdht/bdmanager.cc index 4fb1e4709..52ac66e9a 100644 --- a/libbitdht/src/bitdht/bdmanager.cc +++ b/libbitdht/src/bitdht/bdmanager.cc @@ -72,7 +72,7 @@ bdNodeManager::bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string b :bdNode(id, dhtVersion, bootfile, filterfile, fns, this) { mMode = BITDHT_MGR_STATE_OFF; - mFns = fns; + mDhtFns = fns; mModeTS = 0 ; mStartTS = 0; mSearchingDone = false; @@ -87,7 +87,7 @@ bdNodeManager::bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string b #ifdef DEBUG_MGR std::cerr << "bdNodeManager::bdNodeManager() ID: "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << std::endl; #endif @@ -188,7 +188,7 @@ void bdNodeManager::addFindNode(bdNodeId *id, uint32_t qflags) { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::addFindNode() "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << std::endl; #endif /* check if exists already */ @@ -257,7 +257,7 @@ void bdNodeManager::removeFindNode(bdNodeId *id) { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::removeFindNode() "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << std::endl; #endif std::map::iterator it; @@ -525,7 +525,7 @@ int bdNodeManager::QueryRandomLocalNet() else { /* calculate mid point */ - mFns->bdRandomMidId(&mOwnId, &(id.id), &targetNodeId); + mDhtFns->bdRandomMidId(&mOwnId, &(id.id), &targetNodeId); } /* do standard find_peer message */ @@ -534,13 +534,13 @@ int bdNodeManager::QueryRandomLocalNet() #ifdef DEBUG_MGR std::cerr << "bdNodeManager::QueryRandomLocalNet() Querying : "; - mFns->bdPrintId(std::cerr, &id); + mDhtFns->bdPrintId(std::cerr, &id); std::cerr << " searching for : "; - mFns->bdPrintNodeId(std::cerr, &targetNodeId); + mDhtFns->bdPrintNodeId(std::cerr, &targetNodeId); bdMetric dist; - mFns->bdDistance(&targetNodeId, &(mOwnId), &dist); - int bucket = mFns->bdBucketDistance(&dist); + mDhtFns->bdDistance(&targetNodeId, &(mOwnId), &dist); + int bucket = mDhtFns->bdBucketDistance(&dist); std::cerr << " in Bucket: " << bucket; std::cerr << std::endl; #endif @@ -593,7 +593,7 @@ void bdNodeManager::SearchForLocalNet() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::SearchForLocalNet() Existing Internal Search: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif @@ -630,7 +630,7 @@ void bdNodeManager::SearchForLocalNet() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::SearchForLocalNet() " << i << " Attempts to find OkNode: "; - mFns->bdPrintNodeId(std::cerr, &targetNodeId); + mDhtFns->bdPrintNodeId(std::cerr, &targetNodeId); std::cerr << std::endl; #endif } @@ -638,7 +638,7 @@ void bdNodeManager::SearchForLocalNet() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::SearchForLocalNet() Failed to Find FilterOk this time: "; - mFns->bdPrintNodeId(std::cerr, &targetNodeId); + mDhtFns->bdPrintNodeId(std::cerr, &targetNodeId); std::cerr << std::endl; #endif } @@ -649,7 +649,7 @@ void bdNodeManager::SearchForLocalNet() #ifdef DEBUG_MGR std::cerr << "bdNodeManager::SearchForLocalNet() Adding New Internal Search: "; - mFns->bdPrintNodeId(std::cerr, &(targetNodeId)); + mDhtFns->bdPrintNodeId(std::cerr, &(targetNodeId)); std::cerr << std::endl; #endif } @@ -709,7 +709,7 @@ int bdNodeManager::checkStatus() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() Query in Progress id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif } @@ -719,7 +719,7 @@ int bdNodeManager::checkStatus() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() Query Failed: id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif // BAD. @@ -733,7 +733,7 @@ int bdNodeManager::checkStatus() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() Found Closest: id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif @@ -747,7 +747,7 @@ int bdNodeManager::checkStatus() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() the Peer Online but Unreachable: id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif @@ -761,7 +761,7 @@ int bdNodeManager::checkStatus() { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() Found Query: id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif //foundId = @@ -803,7 +803,7 @@ int bdNodeManager::checkStatus() doCallback = false; #ifdef DEBUG_MGR std::cerr << "bdNodeManager::checkStatus() Internal: no cb for id: "; - mFns->bdPrintNodeId(std::cerr, &(it->first)); + mDhtFns->bdPrintNodeId(std::cerr, &(it->first)); std::cerr << std::endl; #endif } @@ -1024,7 +1024,7 @@ int bdNodeManager::getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &fro { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::getDhtPeerAddress() Id: "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << " ... ? TODO" << std::endl; #else (void) id; @@ -1034,7 +1034,7 @@ int bdNodeManager::getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &fro pit = mActivePeers.find(*id); std::cerr << "bdNodeManager::getDhtPeerAddress() Id: "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << std::endl; if (pit != mActivePeers.end()) @@ -1061,7 +1061,7 @@ int bdNodeManager::getDhtValue(const bdNodeId *id, std::string key, std::string { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::getDhtValue() Id: "; - mFns->bdPrintNodeId(std::cerr, id); + mDhtFns->bdPrintNodeId(std::cerr, id); std::cerr << " key: " << key; std::cerr << " ... ? TODO" << std::endl; #else @@ -1144,7 +1144,7 @@ void bdNodeManager::doNodeCallback(const bdId *id, uint32_t peerflags) { #ifdef DEBUG_MGR std::cerr << "bdNodeManager::doNodeCallback() "; - mFns->bdPrintId(std::cerr, id); + mDhtFns->bdPrintId(std::cerr, id); std::cerr << "peerflags: " << peerflags; std::cerr << std::endl; #endif @@ -1163,7 +1163,7 @@ void bdNodeManager::doPeerCallback(const bdId *id, uint32_t status) #ifdef DEBUG_MGR std::cerr << "bdNodeManager::doPeerCallback()"; - mFns->bdPrintId(std::cerr, id); + mDhtFns->bdPrintId(std::cerr, id); std::cerr << "status: " << status; std::cerr << std::endl; #endif diff --git a/libbitdht/src/bitdht/bdmanager.h b/libbitdht/src/bitdht/bdmanager.h index d3ddb587f..c494b2bef 100644 --- a/libbitdht/src/bitdht/bdmanager.h +++ b/libbitdht/src/bitdht/bdmanager.h @@ -184,7 +184,7 @@ void SearchForLocalNet(); time_t mSearchTS; bool mSearchingDone; - bdDhtFunctions *mFns; + bdDhtFunctions *mDhtFns; uint32_t mNetworkSize; uint32_t mBdNetworkSize; diff --git a/libbitdht/src/bitdht/bdnode.cc b/libbitdht/src/bitdht/bdnode.cc index f7ae5e220..95d1d1c7e 100644 --- a/libbitdht/src/bitdht/bdnode.cc +++ b/libbitdht/src/bitdht/bdnode.cc @@ -518,7 +518,7 @@ void bdNode::checkPotentialPeer(bdId *id, bdId *src) #ifndef DISABLE_BAD_PEER_FILTER std::cerr << "bdNode::checkPotentialPeer("; mFns->bdPrintId(std::cerr, id); - std::cerr << ") MASQARADING AS KNOWN PEER - FLAGGING AS BAD"; + std::cerr << ") MASQUERADING AS KNOWN PEER - FLAGGING AS BAD"; std::cerr << std::endl; // Stores in queue for later callback and desemination around the network. @@ -603,7 +603,7 @@ void bdNode::addPeer(const bdId *id, uint32_t peerflags) std::cerr << "bdNode::addPeer("; mFns->bdPrintId(std::cerr, id); std::cerr << ", " << std::hex << peerflags << std::dec; - std::cerr << ") MASQARADING AS KNOWN PEER - FLAGGING AS BAD"; + std::cerr << ") MASQUERADING AS KNOWN PEER - FLAGGING AS BAD"; std::cerr << std::endl; diff --git a/libbitdht/src/libbitdht.pro b/libbitdht/src/libbitdht.pro index ad6986f32..d9e5816c4 100644 --- a/libbitdht/src/libbitdht.pro +++ b/libbitdht/src/libbitdht.pro @@ -6,23 +6,10 @@ CONFIG -= qt TARGET = bitdht DESTDIR = lib +!include("use_libbitdht.pri"):error("Including") + QMAKE_CXXFLAGS *= -Wall -DBE_DEBUG -profiling { - QMAKE_CXXFLAGS -= -fomit-frame-pointer - QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer -} - -release { - # not much here yet. -} - -#CONFIG += debug -debug { - QMAKE_CXXFLAGS -= -O2 -fomit-frame-pointer - QMAKE_CXXFLAGS *= -g -fno-omit-frame-pointer -} - # treat warnings as error for better removing #QMAKE_CFLAGS += -Werror #QMAKE_CXXFLAGS += -Werror diff --git a/libbitdht/src/use_libbitdht.pri b/libbitdht/src/use_libbitdht.pri new file mode 100644 index 000000000..bbaf1d4d5 --- /dev/null +++ b/libbitdht/src/use_libbitdht.pri @@ -0,0 +1,5 @@ +DEPENDPATH *= $$system_path($$clean_path($${PWD}/../../libbitdht/src)) +INCLUDEPATH *= $$system_path($$clean_path($${PWD}/../../libbitdht/src)) +LIBS *= -L$$system_path($$clean_path($${OUT_PWD}/../../libbitdht/src/lib/)) -lbitdht + +!equals(TARGET, bitdht):PRE_TARGETDEPS *= $$system_path($$clean_path($${OUT_PWD}/../../libbitdht/src/lib/libbitdht.a)) diff --git a/libresapi/src/api/ApiServer.cpp b/libresapi/src/api/ApiServer.cpp index db8b75d16..cc78f7eb8 100644 --- a/libresapi/src/api/ApiServer.cpp +++ b/libresapi/src/api/ApiServer.cpp @@ -17,7 +17,7 @@ #include "ChannelsHandler.h" #include "StatsHandler.h" -#ifdef LIBRESAPI_QT +#ifdef LIBRESAPI_SETTINGS #include "SettingsHandler.h" #endif @@ -240,9 +240,9 @@ public: mTransfersHandler(sts, ifaces.mFiles, ifaces.mPeers, *ifaces.mNotify), mChatHandler(sts, ifaces.mNotify, ifaces.mMsgs, ifaces.mPeers, ifaces.mIdentity, &mPeersHandler), mApiPluginHandler(sts, ifaces), - mChannelsHandler(ifaces.mGxsChannels), + mChannelsHandler(*ifaces.mGxsChannels), mStatsHandler() -#ifdef LIBRESAPI_QT +#ifdef LIBRESAPI_SETTINGS ,mSettingsHandler(sts) #endif { @@ -272,7 +272,7 @@ public: &ChannelsHandler::handleRequest); router.addResourceHandler("stats", dynamic_cast(&mStatsHandler), &StatsHandler::handleRequest); -#ifdef LIBRESAPI_QT +#ifdef LIBRESAPI_SETTINGS router.addResourceHandler("settings", dynamic_cast(&mSettingsHandler), &SettingsHandler::handleRequest); #endif @@ -290,7 +290,7 @@ public: ChannelsHandler mChannelsHandler; StatsHandler mStatsHandler; -#ifdef LIBRESAPI_QT +#ifdef LIBRESAPI_SETTINGS SettingsHandler mSettingsHandler; #endif }; diff --git a/libresapi/src/api/ApiServerMHD.cpp b/libresapi/src/api/ApiServerMHD.cpp index 273010d8b..6b77db59a 100644 --- a/libresapi/src/api/ApiServerMHD.cpp +++ b/libresapi/src/api/ApiServerMHD.cpp @@ -369,6 +369,9 @@ static void secure_queue_response(MHD_Connection *connection, unsigned int statu // tell Internet Explorer to not do content sniffing MHD_add_response_header(response, "X-Content-Type-Options", "nosniff"); + // Prevent clickjacking attacks (also prevented by CSP, but not in all browsers, including FireFox) + MHD_add_response_header(response, "X-Frame-Options", "SAMEORIGIN"); + // Content security policy header, its a new technology and not implemented everywhere // get own host name as the browser sees it diff --git a/libresapi/src/api/ChannelsHandler.cpp b/libresapi/src/api/ChannelsHandler.cpp index 27cc02e56..c79d693b1 100644 --- a/libresapi/src/api/ChannelsHandler.cpp +++ b/libresapi/src/api/ChannelsHandler.cpp @@ -1,8 +1,29 @@ +/* + * RetroShare JSON API + * Copyright (C) 2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + #include "ChannelsHandler.h" #include #include +#include #include +#include + #include "Operators.h" namespace resource_api @@ -21,22 +42,414 @@ StreamBase& operator << (StreamBase& left, RsGxsFile& file) { double size = 0; left << makeKeyValueReference("size", size); - file.mSize = size; + file.mSize = size; } return left; } -ChannelsHandler::ChannelsHandler(RsGxsChannels *channels): - mChannels(channels) +ChannelsHandler::ChannelsHandler(RsGxsChannels& channels): mChannels(channels) { - addResourceHandler("create_post", this, &ChannelsHandler::handleCreatePost); + addResourceHandler("list_channels", this, + &ChannelsHandler::handleListChannels); + addResourceHandler("get_channel_info", this, &ChannelsHandler::handleGetChannelInfo); + addResourceHandler("get_channel_content", this, &ChannelsHandler::handleGetChannelContent); + addResourceHandler("toggle_subscribe", this, &ChannelsHandler::handleToggleSubscription); + addResourceHandler("toggle_auto_download", this, &ChannelsHandler::handleToggleAutoDownload); + addResourceHandler("toggle_read", this, &ChannelsHandler::handleTogglePostRead); + addResourceHandler("create_channel", this, &ChannelsHandler::handleCreateChannel); + addResourceHandler("create_post", this, &ChannelsHandler::handleCreatePost); } -ResponseTask* ChannelsHandler::handleCreatePost(Request &req, Response &resp) +void ChannelsHandler::handleListChannels(Request& /*req*/, Response& resp) +{ + RsTokReqOptions opts; + opts.mReqType = GXS_REQUEST_TYPE_GROUP_META; + uint32_t token; + + RsTokenService& tChannels = *mChannels.getTokenService(); + + tChannels.requestGroupInfo(token, RS_DEPRECATED_TOKREQ_ANSTYPE, opts); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + std::list grps; + if( tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE + && mChannels.getGroupSummary(token, grps) ) + { + for( RsGroupMetaData& grp : grps ) + { + KeyValueReference id("channel_id", grp.mGroupId); + KeyValueReference vis_msg("visible_msg_count", grp.mVisibleMsgCount); + bool own = (grp.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN); + bool subscribed = IS_GROUP_SUBSCRIBED(grp.mSubscribeFlags); + std::string lastPostTsStr = std::to_string(grp.mLastPost); + std::string publishTsStr = std::to_string(grp.mPublishTs); + resp.mDataStream.getStreamToMember() + << id + << makeKeyValueReference("name", grp.mGroupName) + << makeKeyValueReference("last_post_ts", lastPostTsStr) + << makeKeyValueReference("popularity", grp.mPop) + << makeKeyValueReference("publish_ts", publishTsStr) + << vis_msg + << makeKeyValueReference("group_status", grp.mGroupStatus) + << makeKeyValueReference("author_id", grp.mAuthorId) + << makeKeyValueReference("parent_grp_id", grp.mParentGrpId) + << makeKeyValueReference("own", own) + << makeKeyValueReference("subscribed", subscribed); + } + + resp.setOk(); + } + else resp.setFail("Cant get data from GXS!"); +} + +void ChannelsHandler::handleGetChannelInfo(Request& req, Response& resp) +{ + std::string chanIdStr; + req.mStream << makeKeyValueReference("channel_id", chanIdStr); + if(chanIdStr.empty()) + { + resp.setFail("channel_id required!"); + return; + } + + RsGxsGroupId chanId(chanIdStr); + if(chanId.isNull()) + { + resp.setFail("Invalid channel_id:" + chanIdStr); + return; + } + + bool wantThumbnail = true; + req.mStream << makeKeyValueReference("want_thumbnail", wantThumbnail); + + std::list groupIds; groupIds.push_back(chanId); + RsTokReqOptions opts; + opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA; + uint32_t token; + + RsTokenService& tChannels = *mChannels.getTokenService(); + tChannels.requestGroupInfo( token, RS_DEPRECATED_TOKREQ_ANSTYPE, + opts, groupIds ); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + std::vector grps; + if( tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE + && mChannels.getGroupData(token, grps) ) + { + for( RsGxsChannelGroup& grp : grps ) + { + KeyValueReference id("channel_id", grp.mMeta.mGroupId); + KeyValueReference vis_msg("visible_msg_count", grp.mMeta.mVisibleMsgCount); + bool own = (grp.mMeta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN); + bool subscribed = IS_GROUP_SUBSCRIBED(grp.mMeta.mSubscribeFlags); + std::string lastPostTsStr = std::to_string(grp.mMeta.mLastPost); + std::string publishTsStr = std::to_string(grp.mMeta.mPublishTs); + StreamBase& rgrp(resp.mDataStream.getStreamToMember()); + rgrp << id + << makeKeyValueReference("name", grp.mMeta.mGroupName) + << makeKeyValueReference("last_post_ts", lastPostTsStr) + << makeKeyValueReference("popularity", grp.mMeta.mPop) + << makeKeyValueReference("publish_ts", publishTsStr) + << vis_msg + << makeKeyValueReference("group_status", grp.mMeta.mGroupStatus) + << makeKeyValueReference("author_id", grp.mMeta.mAuthorId) + << makeKeyValueReference("parent_grp_id", grp.mMeta.mParentGrpId) + << makeKeyValueReference("description", grp.mDescription) + << makeKeyValueReference("own", own) + << makeKeyValueReference("subscribed", subscribed) + << makeKeyValueReference("auto_download", grp.mAutoDownload); + + if(wantThumbnail) + { + std::string thumbnail_base64; + Radix64::encode(grp.mImage.mData, grp.mImage.mSize, thumbnail_base64); + rgrp << makeKeyValueReference("thumbnail_base64_png", thumbnail_base64); + } + } + + resp.setOk(); + } + else resp.setFail("Cant get data from GXS!"); +} + +void ChannelsHandler::handleGetChannelContent(Request& req, Response& resp) +{ + std::string chanIdStr; + req.mStream << makeKeyValueReference("channel_id", chanIdStr); + if(chanIdStr.empty()) + { + resp.setFail("channel_id required!"); + return; + } + + RsGxsGroupId chanId(chanIdStr); + if(chanId.isNull()) + { + resp.setFail("Invalid channel_id:" + chanIdStr); + return; + } + + std::list groupIds; groupIds.push_back(chanId); + uint32_t token; + RsTokReqOptions opts; + opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; + + if(! mChannels.getTokenService()-> + requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, groupIds) ) + { + resp.setFail("Unknown GXS error!"); + return; + } + + time_t start = time(NULL); + while((mChannels.getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(mChannels.getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + std::vector posts; + std::vector comments; + if( mChannels.getTokenService()->requestStatus(token) == + RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE && + mChannels.getPostData(token, posts, comments) ) + { + for( std::vector::iterator vit = posts.begin(); + vit != posts.end(); ++vit ) + { + RsGxsChannelPost& post = *vit; + RsMsgMetaData& pMeta = post.mMeta; + resp.mDataStream.getStreamToMember() + << makeKeyValueReference("channel_id", pMeta.mGroupId) + << makeKeyValueReference("name", pMeta.mMsgName) + << makeKeyValueReference("post_id", pMeta.mMsgId) + << makeKeyValueReference("parent_id", pMeta.mParentId) + << makeKeyValueReference("author_id", pMeta.mAuthorId) + << makeKeyValueReference("orig_msg_id", pMeta.mOrigMsgId) + << makeKeyValueReference("thread_id", pMeta.mThreadId) + << makeKeyValueReference("message", post.mMsg); + } + + for( std::vector::iterator vit = comments.begin(); + vit != comments.end(); ++vit ) + { + RsGxsComment& comment = *vit; + RsMsgMetaData& cMeta = comment.mMeta; + std::string scoreStr = std::to_string(comment.mScore); + resp.mDataStream.getStreamToMember() + << makeKeyValueReference("channel_id", cMeta.mGroupId) + << makeKeyValueReference("name", cMeta.mMsgName) + << makeKeyValueReference("comment_id", cMeta.mMsgId) + << makeKeyValueReference("parent_id", cMeta.mParentId) + << makeKeyValueReference("author_id", cMeta.mAuthorId) + << makeKeyValueReference("orig_msg_id", cMeta.mOrigMsgId) + << makeKeyValueReference("thread_id", cMeta.mThreadId) + << makeKeyValueReference("score", scoreStr) + << makeKeyValueReference("message", comment.mComment); + } + + resp.setOk(); + } + else resp.setFail("Cant get data from GXS!"); +} + +void ChannelsHandler::handleToggleSubscription(Request& req, Response& resp) +{ + std::string chanIdStr; + bool subscribe = true; + + req.mStream << makeKeyValueReference("channel_id", chanIdStr) + << makeKeyValueReference("subscribe", subscribe); + + if(chanIdStr.empty()) + { + resp.setFail("channel_id required!"); + return; + } + + RsGxsGroupId chanId(chanIdStr); + if(chanId.isNull()) + { + resp.setFail("Invalid channel_id:" + chanIdStr); + return; + } + + uint32_t token; + if(mChannels.subscribeToGroup(token, chanId, subscribe)) + { + RsTokenService& tChannels = *mChannels.getTokenService(); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + if(tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + resp.setOk(); + else resp.setFail("Unknown GXS error!"); + } + else resp.setFail("Unknown GXS error!"); +} + +void ChannelsHandler::handleCreateChannel(Request& req, Response& resp) +{ + RsGxsChannelGroup chan; + RsGroupMetaData& cMeta = chan.mMeta; + std::string authorIdStr; + std::string thumbnail_base64; + + req.mStream << makeKeyValueReference("author_id", authorIdStr) + << makeKeyValueReference("name", cMeta.mGroupName) + << makeKeyValueReference("description", chan.mDescription) + << makeKeyValueReference("thumbnail_base64_png", thumbnail_base64); + + if(cMeta.mGroupName.empty()) + { + resp.setFail("Channel name required!"); + return; + } + + if(thumbnail_base64.empty()) chan.mImage.clear(); + else + { + std::vector png_data = Radix64::decode(thumbnail_base64); + if(!png_data.empty()) + { + if(png_data.size() < 8) + { + resp.setFail("Decoded thumbnail_base64_png is smaller than 8 byte. This can't be a valid png file!"); + return; + } + uint8_t png_magic_number[] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a}; + if(!std::equal(&png_magic_number[0],&png_magic_number[8],png_data.begin())) + { + resp.setFail("Decoded thumbnail_base64_png does not seem to be a png file. (Header is missing magic number)"); + return; + } + chan.mImage.copy(png_data.data(), png_data.size()); + } + } + + if(!authorIdStr.empty()) cMeta.mAuthorId = RsGxsId(authorIdStr); + + // ATM supports creating only public channels + cMeta.mGroupFlags = GXS_SERV::FLAG_PRIVACY_PUBLIC; + + // I am not sure about those flags I have reversed them with the debugger + // that gives 520 as value of this member when a channel with default + // options is created from Qt Gui + cMeta.mSignFlags = GXS_SERV::MSG_AUTHEN_CHILD_AUTHOR_SIGN | + GXS_SERV::FLAG_AUTHOR_AUTHENTICATION_REQUIRED; + + uint32_t token; + if(mChannels.createGroup(token, chan)) + { + RsTokenService& tChannels = *mChannels.getTokenService(); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + if(tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + resp.setOk(); + else resp.setFail("Unknown GXS error!"); + } + else resp.setFail("Unkown GXS error!"); +} + +void ChannelsHandler::handleToggleAutoDownload(Request& req, Response& resp) +{ + + std::string chanIdStr; + bool autoDownload = true; + + req.mStream << makeKeyValueReference("channel_id", chanIdStr) + << makeKeyValueReference("auto_download", autoDownload); + + if(chanIdStr.empty()) + { + resp.setFail("channel_id required!"); + return; + } + + RsGxsGroupId chanId(chanIdStr); + if(chanId.isNull()) + { + resp.setFail("Invalid channel_id:" + chanIdStr); + return; + } + + if(mChannels.setChannelAutoDownload(chanId, autoDownload)) + resp.setOk(); + else resp.setFail(); +} + +void ChannelsHandler::handleTogglePostRead(Request& req, Response& resp) +{ + std::string chanIdStr; + std::string postIdStr; + bool read = true; + + req.mStream << makeKeyValueReference("channel_id", chanIdStr) + << makeKeyValueReference("post_id", postIdStr) + << makeKeyValueReference("read", read); + + if(chanIdStr.empty()) + { + resp.setFail("channel_id required!"); + return; + } + + RsGxsGroupId chanId(chanIdStr); + if(chanId.isNull()) + { + resp.setFail("Invalid channel_id:" + chanIdStr); + return; + } + + if(postIdStr.empty()) + { + resp.setFail("post_id required!"); + return; + } + + RsGxsMessageId postId(postIdStr); + if(postId.isNull()) + { + resp.setFail("Invalid post_id:" + postIdStr); + return; + } + + std::cerr << __PRETTY_FUNCTION__ << " " << chanIdStr << " " << postIdStr + << " " << read << std::endl; + + uint32_t token; + mChannels.setMessageReadStatus(token, std::make_pair(chanId,postId), read); + + RsTokenService& tChannels = *mChannels.getTokenService(); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + if(tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + resp.setOk(); + else resp.setFail("Unknown GXS error!"); +} + +void ChannelsHandler::handleCreatePost(Request &req, Response &resp) { RsGxsChannelPost post; - req.mStream << makeKeyValueReference("group_id", post.mMeta.mGroupId); + req.mStream << makeKeyValueReference("channel_id", post.mMeta.mGroupId); req.mStream << makeKeyValueReference("subject", post.mMeta.mMsgName); req.mStream << makeKeyValueReference("message", post.mMsg); @@ -53,36 +466,36 @@ ResponseTask* ChannelsHandler::handleCreatePost(Request &req, Response &resp) if(post.mMeta.mGroupId.isNull()) { - resp.setFail("groupd_id is null"); - return 0; + resp.setFail("groupd_id is null"); + return; } if(post.mMeta.mMsgName.empty()) { - resp.setFail("subject is empty"); - return 0; + resp.setFail("subject is empty"); + return; } if(post.mMsg.empty()) { - resp.setFail("msg text is empty"); - return 0; + resp.setFail("msg text is empty"); + return; } // empty file list is ok, but files have to be valid for(std::list::iterator lit = post.mFiles.begin(); lit != post.mFiles.end(); ++lit) { if(lit->mHash.isNull()) { - resp.setFail("at least one file hash is empty"); - return 0; + resp.setFail("at least one file hash is empty"); + return; } if(lit->mName.empty()) { - resp.setFail("at leats one file name is empty"); - return 0; + resp.setFail("at leats one file name is empty"); + return; } if(lit->mSize == 0) { - resp.setFail("at least one file size is empty"); - return 0; + resp.setFail("at least one file size is empty"); + return; } } @@ -91,22 +504,33 @@ ResponseTask* ChannelsHandler::handleCreatePost(Request &req, Response &resp) { if(png_data.size() < 8) { - resp.setFail("Decoded thumbnail_base64_png is smaller than 8 byte. This can't be a valid png file!"); - return 0; + resp.setFail("Decoded thumbnail_base64_png is smaller than 8 byte. This can't be a valid png file!"); + return; } uint8_t png_magic_number[] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a}; if(!std::equal(&png_magic_number[0],&png_magic_number[8],png_data.begin())) { - resp.setFail("Decoded thumbnail_base64_png does not seem to be a png file. (Header is missing magic number)"); - return 0; + resp.setFail("Decoded thumbnail_base64_png does not seem to be a png file. (Header is missing magic number)"); + return; } post.mThumbnail.copy(png_data.data(), png_data.size()); } uint32_t token; - mChannels->createPost(token, post); - // TODO: grp creation acknowledge - return 0; + if(mChannels.createPost(token, post)) + { + RsTokenService& tChannels = *mChannels.getTokenService(); + + time_t start = time(NULL); + while((tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + &&(tChannels.requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED) + &&((time(NULL) < (start+10)))) rstime::rs_usleep(500*1000); + + if(tChannels.requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE) + resp.setOk(); + else resp.setFail("Unknown GXS error!"); + } + else resp.setFail("Unknown GXS error!"); } } // namespace resource_api diff --git a/libresapi/src/api/ChannelsHandler.h b/libresapi/src/api/ChannelsHandler.h index 1d8559f52..55c5fec2d 100644 --- a/libresapi/src/api/ChannelsHandler.h +++ b/libresapi/src/api/ChannelsHandler.h @@ -1,4 +1,21 @@ #pragma once +/* + * RetroShare JSON API + * Copyright (C) 2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ #include "ResourceRouter.h" @@ -7,15 +24,21 @@ class RsGxsChannels; namespace resource_api { -class ChannelsHandler : public ResourceRouter +struct ChannelsHandler : ResourceRouter { -public: - ChannelsHandler(RsGxsChannels* channels); + ChannelsHandler(RsGxsChannels& channels); private: - ResponseTask* handleCreatePost(Request& req, Response& resp); + void handleListChannels(Request& req, Response& resp); + void handleGetChannelInfo(Request& req, Response& resp); + void handleGetChannelContent(Request& req, Response& resp); + void handleToggleSubscription(Request& req, Response& resp); + void handleCreateChannel(Request& req, Response& resp); + void handleToggleAutoDownload(Request& req, Response& resp); + void handleTogglePostRead(Request& req, Response& resp); + void handleCreatePost(Request& req, Response& resp); - RsGxsChannels* mChannels; + RsGxsChannels& mChannels; }; } // namespace resource_api diff --git a/libresapi/src/api/FileSharingHandler.cpp b/libresapi/src/api/FileSharingHandler.cpp index a6296973d..44c94cfad 100644 --- a/libresapi/src/api/FileSharingHandler.cpp +++ b/libresapi/src/api/FileSharingHandler.cpp @@ -37,6 +37,16 @@ FileSharingHandler::FileSharingHandler(StateTokenServer *sts, RsFiles *files, addResourceHandler("get_dir_parent", this, &FileSharingHandler::handleGetDirectoryParent); addResourceHandler("get_dir_childs", this, &FileSharingHandler::handleGetDirectoryChilds); + addResourceHandler( "get_download_directory", this, + &FileSharingHandler::handleGetDownloadDirectory ); + addResourceHandler( "set_download_directory", this, + &FileSharingHandler::handleSetDownloadDirectory ); + + addResourceHandler( "get_partials_directory", this, + &FileSharingHandler::handleGetPartialsDirectory ); + addResourceHandler( "set_partials_directory", this, + &FileSharingHandler::handleSetPartialsDirectory ); + addResourceHandler("is_dl_dir_shared", this, &FileSharingHandler::handleIsDownloadDirShared); addResourceHandler("share_dl_dir", this, &FileSharingHandler::handleShareDownloadDirectory); @@ -513,4 +523,48 @@ void FileSharingHandler::handleDownload(Request& req, Response& resp) resp.setFail("Couldn't download file"); } +void FileSharingHandler::handleGetDownloadDirectory( Request& /*req*/, + Response& resp ) +{ + std::string dlDir = mRsFiles->getDownloadDirectory(); + resp.mDataStream << makeKeyValueReference("download_directory", dlDir); + resp.setOk(); +} + +void FileSharingHandler::handleSetDownloadDirectory( Request& req, + Response& resp ) +{ + std::string dlDir; + req.mStream << makeKeyValueReference("download_directory", dlDir); + + if(dlDir.empty()) resp.setFail("missing download_directory"); + else + { + mRsFiles->setDownloadDirectory(dlDir); + resp.setOk(); + } +} + +void FileSharingHandler::handleGetPartialsDirectory( Request& /*req*/, + Response& resp ) +{ + std::string partialsDir = mRsFiles->getPartialsDirectory(); + resp.mDataStream << makeKeyValueReference("partials_directory", partialsDir); + resp.setOk(); +} + +void FileSharingHandler::handleSetPartialsDirectory( Request& req, + Response& resp ) +{ + std::string partialsDir; + req.mStream << makeKeyValueReference("partials_directory", partialsDir); + + if(partialsDir.empty()) resp.setFail("missing partials_directory"); + else + { + mRsFiles->setPartialsDirectory(partialsDir); + resp.setOk(); + } +} + } // namespace resource_api diff --git a/libresapi/src/api/FileSharingHandler.h b/libresapi/src/api/FileSharingHandler.h index 0eb67764e..cd78f338f 100644 --- a/libresapi/src/api/FileSharingHandler.h +++ b/libresapi/src/api/FileSharingHandler.h @@ -57,6 +57,12 @@ private: void handleDownload(Request& req, Response& resp); + void handleGetDownloadDirectory(Request& req, Response& resp); + void handleSetDownloadDirectory(Request& req, Response& resp); + + void handleGetPartialsDirectory(Request& req, Response& resp); + void handleSetPartialsDirectory(Request& req, Response& resp); + /// Token indicating change in local shared files StateToken mLocalDirStateToken; diff --git a/libresapi/src/api/TransfersHandler.cpp b/libresapi/src/api/TransfersHandler.cpp index c696411da..dc28e9596 100644 --- a/libresapi/src/api/TransfersHandler.cpp +++ b/libresapi/src/api/TransfersHandler.cpp @@ -15,6 +15,14 @@ TransfersHandler::TransfersHandler(StateTokenServer *sts, RsFiles *files, RsPeer addResourceHandler("downloads", this, &TransfersHandler::handleDownloads); addResourceHandler("uploads", this, &TransfersHandler::handleUploads); addResourceHandler("control_download", this, &TransfersHandler::handleControlDownload); + + addResourceHandler( "set_file_destination_directory", this, + &TransfersHandler::handleSetFileDestinationDirectory ); + addResourceHandler( "set_file_destination_name", this, + &TransfersHandler::handleSetFileDestinationName ); + addResourceHandler( "set_file_chunk_strategy", this, + &TransfersHandler::handleSetFileChunkStrategy ); + mStateToken = mStateTokenServer->getNewToken(); mStateTokenServer->registerTickClient(this); mNotify.registerNotifyClient(this); @@ -288,4 +296,58 @@ void TransfersHandler::handleUploads(Request & /* req */, Response &resp) resp.setOk(); } +void TransfersHandler::handleSetFileDestinationDirectory( Request& req, + Response& resp ) +{ + mStateTokenServer->replaceToken(mStateToken); + + std::string hashString; + std::string newPath; + req.mStream << makeKeyValueReference("path", newPath); + req.mStream << makeKeyValueReference("hash", hashString); + RsFileHash hash(hashString); + + if (mFiles->setDestinationDirectory(hash, newPath)) resp.setOk(); + else resp.setFail(); +} + +void TransfersHandler::handleSetFileDestinationName( Request& req, + Response& resp ) +{ + mStateTokenServer->replaceToken(mStateToken); + + std::string hashString; + std::string newName; + req.mStream << makeKeyValueReference("name", newName); + req.mStream << makeKeyValueReference("hash", hashString); + RsFileHash hash(hashString); + + if (mFiles->setDestinationName(hash, newName)) resp.setOk(); + else resp.setFail(); +} + +void TransfersHandler::handleSetFileChunkStrategy(Request& req, Response& resp) +{ + mStateTokenServer->replaceToken(mStateToken); + + std::string hashString; + std::string newChunkStrategyStr; + req.mStream << makeKeyValueReference("chuck_stategy", newChunkStrategyStr); + req.mStream << makeKeyValueReference("hash", hashString); + + RsFileHash hash(hashString); + FileChunksInfo::ChunkStrategy newStrategy = + FileChunksInfo::CHUNK_STRATEGY_PROGRESSIVE; + + if ( newChunkStrategyStr == "streaming" ) + newStrategy = FileChunksInfo::CHUNK_STRATEGY_STREAMING; + else if ( newChunkStrategyStr == "random" ) + newStrategy = FileChunksInfo::CHUNK_STRATEGY_RANDOM; + else if ( newChunkStrategyStr == "progressive" ) + newStrategy = FileChunksInfo::CHUNK_STRATEGY_PROGRESSIVE; + + if (mFiles->setChunkStrategy(hash, newStrategy)) resp.setOk(); + else resp.setFail(); +} + } // namespace resource_api diff --git a/libresapi/src/api/TransfersHandler.h b/libresapi/src/api/TransfersHandler.h index 7d07a7198..7e43c3614 100644 --- a/libresapi/src/api/TransfersHandler.h +++ b/libresapi/src/api/TransfersHandler.h @@ -30,6 +30,9 @@ private: void handleControlDownload(Request& req, Response& resp); void handleDownloads(Request& req, Response& resp); void handleUploads(Request& req, Response& resp); + void handleSetFileDestinationDirectory(Request& req, Response& resp); + void handleSetFileDestinationName(Request& req, Response& resp); + void handleSetFileChunkStrategy(Request& req, Response& resp); StateTokenServer* mStateTokenServer; RsFiles* mFiles; diff --git a/libresapi/src/libresapi.pro b/libresapi/src/libresapi.pro index ea2daa082..161ce9bf7 100644 --- a/libresapi/src/libresapi.pro +++ b/libresapi/src/libresapi.pro @@ -2,50 +2,30 @@ TEMPLATE = lib CONFIG += staticlib -CONFIG += create_prl CONFIG -= qt TARGET = resapi TARGET_PRL = libresapi DESTDIR = lib -DEPENDPATH += ../../libretroshare/src/ +!include(use_libresapi.pri):error("Including") + +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 INCLUDEPATH += ../../libretroshare/src +libresapilocalserver { + SOURCES *= api/ApiServerLocal.cpp + HEADERS *= api/ApiServerLocal.h +} -retroshare_android_service { - win32 { - OBJECTS_DIR = temp/obj - - LIBS_DIR = $$PWD/../../libs/lib - LIBS += $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a - LIBS += $$OUT_PWD/../../openpgpsdk/src/lib/libops.a - - for(lib, LIB_DIR):LIBS += -L"$$lib" - for(bin, BIN_DIR):LIBS += -L"$$bin" - - - LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz -lws2_32 - LIBS += -luuid -lole32 -liphlpapi -lcrypt32 -lgdi32 - LIBS += -lwinmm - - DEFINES *= WINDOWS_SYS WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T - - DEPENDPATH += . $$INC_DIR - INCLUDEPATH += . $$INC_DIR - - greaterThan(QT_MAJOR_VERSION, 4) { - # Qt 5 - RC_INCLUDEPATH += $$_PRO_FILE_PWD_/../../libretroshare/src - } else { - # Qt 4 - QMAKE_RC += --include-dir=$$_PRO_FILE_PWD_/../../libretroshare/src - } - } +libresapi_settings { + SOURCES += api/SettingsHandler.cpp + HEADERS += api/SettingsHandler.h } libresapihttpserver { - CONFIG += libmicrohttpd - unix { webui_files.path = "$${DATA_DIR}/webui" @@ -121,20 +101,13 @@ libresapihttpserver { DEFINES *= WINDOWS_SYS INCLUDEPATH += . $$INC_DIR - greaterThan(QT_MAJOR_VERSION, 4) { - # Qt 5 - PRO_PATH=$$shell_path($$_PRO_FILE_PWD_) - MAKE_SRC=$$shell_path($$PRO_PATH/webui-src/make-src) - } else { - # Qt 4 - PRO_PATH=$$replace(_PRO_FILE_PWD_, /, \\) - MAKE_SRC=$$PRO_PATH\\webui-src\\make-src - } + PRO_PATH=$$shell_path($$_PRO_FILE_PWD_) + MAKE_SRC=$$shell_path($$PRO_PATH/webui-src/make-src) - #create_webfiles.commands = $$MAKE_SRC\\build.bat $$PRO_PATH - #QMAKE_EXTRA_TARGETS += create_webfiles - #PRE_TARGETDEPS += create_webfiles - QMAKE_POST_LINK=$$MAKE_SRC\\build.bat $$PRO_PATH + #create_webfiles.commands = $$MAKE_SRC\\build.bat $$PRO_PATH + #QMAKE_EXTRA_TARGETS += create_webfiles + #PRE_TARGETDEPS += create_webfiles + QMAKE_POST_LINK=$$MAKE_SRC\\build.bat $$PRO_PATH # create dummy files system($$MAKE_SRC\\init.bat .) @@ -211,18 +184,3 @@ HEADERS += \ api/ChannelsHandler.h \ api/StatsHandler.h \ api/FileSharingHandler.h - -libresapilocalserver { - CONFIG *= qt - QT *= network - SOURCES *= api/ApiServerLocal.cpp - HEADERS *= api/ApiServerLocal.h -} - -qt_dependencies { - CONFIG *= qt - QT *= core - - SOURCES += api/SettingsHandler.cpp - HEADERS += api/SettingsHandler.h -} diff --git a/libresapi/src/use_libresapi.pri b/libresapi/src/use_libresapi.pri new file mode 100644 index 000000000..ee4c87d46 --- /dev/null +++ b/libresapi/src/use_libresapi.pri @@ -0,0 +1,37 @@ +DEPENDPATH *= $$system_path($$clean_path($$PWD/../../libresapi/src)) +INCLUDEPATH *= $$system_path($$clean_path($${PWD}/../../libresapi/src)) +LIBS *= -L$$system_path($$clean_path($${OUT_PWD}/../../libresapi/src/lib/)) -lresapi + +!equals(TARGET, resapi):PRE_TARGETDEPS *= $$system_path($$clean_path($${OUT_PWD}/../../libresapi/src/lib/libresapi.a)) + +!include("../../libretroshare/src/use_libretroshare.pri"):error("Including") + +sLibs = +mLibs = +dLibs = + +libresapilocalserver { + CONFIG *= qt + QT *= network +} + +libresapi_settings { + CONFIG *= qt + QT *= core +} + +libresapihttpserver { + mLibs *= microhttpd +} + +static { + sLibs *= $$mLibs +} else { + dLibs *= $$mLibs +} + +LIBS += $$linkStaticLibs(sLibs) +PRE_TARGETDEPS += $$pretargetStaticLibs(sLibs) + +LIBS += $$linkDynamicLibs(dLibs) + diff --git a/libretroshare/src/chat/distributedchat.h b/libretroshare/src/chat/distributedchat.h index ed5a438f9..ca7155dd2 100644 --- a/libretroshare/src/chat/distributedchat.h +++ b/libretroshare/src/chat/distributedchat.h @@ -30,7 +30,7 @@ typedef RsPeerId ChatLobbyVirtualPeerId ; -class RsItem ; +struct RsItem; class p3HistoryMgr ; class p3IdService ; class p3ServiceControl; diff --git a/libretroshare/src/chat/rschatitems.cc b/libretroshare/src/chat/rschatitems.cc index 24aef56f9..7ae680105 100644 --- a/libretroshare/src/chat/rschatitems.cc +++ b/libretroshare/src/chat/rschatitems.cc @@ -205,11 +205,8 @@ void RsPrivateChatMsgConfigItem::get(RsChatMsgItem *ci) ci->recvTime = recvTime; } -/* Necessary to serialize `store` that is an STL container with RsChatMsgItem - * inside which is a subtype of RsItem */ -RS_REGISTER_ITEM_TYPE(RsChatMsgItem) void PrivateOugoingMapItem::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) -{ RS_REGISTER_SERIAL_MEMBER(store); } +{ RS_SERIAL_PROCESS(store); } diff --git a/libretroshare/src/dht/p3bitdht_peers.cc b/libretroshare/src/dht/p3bitdht_peers.cc index 0543b91b2..cf2d2af97 100644 --- a/libretroshare/src/dht/p3bitdht_peers.cc +++ b/libretroshare/src/dht/p3bitdht_peers.cc @@ -4,6 +4,7 @@ * BitDht interface for RetroShare. * * Copyright 2009-2010 by Robert Fernie. + * Copyright (C) 2015-2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -184,18 +185,24 @@ bool p3BitDht::dropPeer(const RsPeerId& pid) ********************************* Basic Peer Details ************************************* ******************************************************************************************/ -int p3BitDht::addBadPeer(const struct sockaddr_storage &addr, uint32_t /*reason*/, uint32_t /*flags*/, uint32_t /*age*/) +int p3BitDht::addBadPeer( const sockaddr_storage &addr, uint32_t /*reason*/, + uint32_t /*flags*/, uint32_t /*age*/ ) { //mUdpBitDht->updateKnownPeer(&id, 0, bdflags); - struct sockaddr_in addrv4; - if (addr.ss_family != AF_INET) + sockaddr_in addrv4; + sockaddr_storage tmpaddr; + sockaddr_storage_copy(addr, tmpaddr); + if(!sockaddr_storage_ipv6_to_ipv4(tmpaddr)) { - std::cerr << "p3BitDht::addBadPeer() cannot handle IPV6 Yet, aborting"; - std::cerr << std::endl; - abort(); + std::cerr << __PRETTY_FUNCTION__ << " Error: got non IPv4 address!" + << std::endl; + sockaddr_storage_dump(addr); + print_stacktrace(); + return -EINVAL; } - struct sockaddr_in *ap = (struct sockaddr_in *) &addr; + + struct sockaddr_in *ap = (struct sockaddr_in *) &tmpaddr; // convert. addrv4.sin_family = ap->sin_family; @@ -216,39 +223,30 @@ int p3BitDht::addBadPeer(const struct sockaddr_storage &addr, uint32_t /*reason* } -int p3BitDht::addKnownPeer(const RsPeerId &pid, const struct sockaddr_storage &addr, uint32_t flags) +int p3BitDht::addKnownPeer( const RsPeerId &pid, + const sockaddr_storage &addr, uint32_t flags ) { - struct sockaddr_in addrv4; - sockaddr_clear(&addrv4); + sockaddr_in addrv4; + sockaddr_clear(&addrv4); - if (addr.ss_family != AF_INET) - { - if(addr.ss_family != AF_UNSPEC) - { - std::cerr << "p3BitDht::addKnownPeer() Warning! Non IPv4 Address - Cannot handle IPV6 Yet. addr.ss_family=" << addr.ss_family; - std::cerr << std::endl; - } - - if (flags & NETASSIST_KNOWN_PEER_ONLINE) - { - std::cerr << "p3BitDht::addKnownPeer() Non IPv4 Address & ONLINE. Abort()ing."; - std::cerr << std::endl; - abort(); - } - } - else + sockaddr_storage tmpaddr; + sockaddr_storage_copy(addr, tmpaddr); + if( !sockaddr_storage_isnull(addr) && + !sockaddr_storage_ipv6_to_ipv4(tmpaddr) ) { - - // convert. - struct sockaddr_in *ap = (struct sockaddr_in *) &addr; - - addrv4.sin_family = ap->sin_family; - addrv4.sin_addr = ap->sin_addr; - addrv4.sin_port = ap->sin_port; + std::cerr << __PRETTY_FUNCTION__ << " Error: got non IPv4 address!" + << std::endl; + sockaddr_storage_dump(addr); + print_stacktrace(); + return -EINVAL; } - - + // convert. + struct sockaddr_in *ap = (struct sockaddr_in *) &tmpaddr; + addrv4.sin_family = ap->sin_family; + addrv4.sin_addr = ap->sin_addr; + addrv4.sin_port = ap->sin_port; + int p3type = 0; int bdflags = 0; bdId id; @@ -295,7 +293,7 @@ int p3BitDht::addKnownPeer(const RsPeerId &pid, const struct sockaddr_storage &a if (!isOwnId) { - RsStackMutex stack(dhtMtx); /********* LOCKED *********/ + RS_STACK_MUTEX(dhtMtx); DhtPeerDetails *dpd = addInternalPeer_locked(pid, p3type); diff --git a/libretroshare/src/file_sharing/directory_storage.cc b/libretroshare/src/file_sharing/directory_storage.cc index eb3c58626..d39401e06 100644 --- a/libretroshare/src/file_sharing/directory_storage.cc +++ b/libretroshare/src/file_sharing/directory_storage.cc @@ -623,7 +623,7 @@ bool LocalDirectoryStorage::locked_getFileSharingPermissions(const EntryIndex& i if(it == mLocalDirs.end()) { - std::cerr << "(EE) very weird bug: base directory \"" << base_dir << "\" not found in shared dir list." << std::endl; + std::cerr << "(II) base directory \"" << base_dir << "\" not found in shared dir list." << std::endl; return false ; } diff --git a/libretroshare/src/file_sharing/directory_updater.cc b/libretroshare/src/file_sharing/directory_updater.cc index 6d95961ab..dffef92c7 100644 --- a/libretroshare/src/file_sharing/directory_updater.cc +++ b/libretroshare/src/file_sharing/directory_updater.cc @@ -73,10 +73,23 @@ void LocalDirectoryUpdater::data_tick() { if(now > mDelayBetweenDirectoryUpdates + mLastSweepTime) { - if(sweepSharedDirectories()) + bool some_files_not_ready = false ; + + if(sweepSharedDirectories(some_files_not_ready)) { - mNeedsFullRecheck = false; - mLastSweepTime = now ; + if(some_files_not_ready) + { + mNeedsFullRecheck = true ; + mLastSweepTime = now - mDelayBetweenDirectoryUpdates + 60 ; // retry 20 secs from now + + std::cerr << "(II) some files being modified. Will re-scan in 60 secs." << std::endl; + } + else + { + mNeedsFullRecheck = false ; + mLastSweepTime = now ; + } + mSharedDirectories->notifyTSChanged(); mForceUpdate = false ; } @@ -111,7 +124,7 @@ void LocalDirectoryUpdater::forceUpdate() mHashCache->togglePauseHashingProcess(); } -bool LocalDirectoryUpdater::sweepSharedDirectories() +bool LocalDirectoryUpdater::sweepSharedDirectories(bool& some_files_not_ready) { if(mHashSalt.isNull()) { @@ -158,8 +171,8 @@ bool LocalDirectoryUpdater::sweepSharedDirectories() #endif existing_dirs.insert(RsDirUtil::removeSymLinks(stored_dir_it.name())); - recursUpdateSharedDir(stored_dir_it.name(), *stored_dir_it,existing_dirs,1) ; // here we need to use the list that was stored, instead of the shared dir list, because the two - // are not necessarily in the same order. + recursUpdateSharedDir(stored_dir_it.name(), *stored_dir_it,existing_dirs,1,some_files_not_ready) ; // here we need to use the list that was stored, instead of the shared dir list, because the two + // are not necessarily in the same order. } RsServer::notify()->notifyListChange(NOTIFY_LIST_DIRLIST_LOCAL, 0); @@ -168,7 +181,7 @@ bool LocalDirectoryUpdater::sweepSharedDirectories() return true ; } -void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx,std::set& existing_directories,uint32_t current_depth) +void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx,std::set& existing_directories,uint32_t current_depth,bool& some_files_not_ready) { #ifdef DEBUG_LOCAL_DIR_UPDATER std::cerr << "[directory storage] parsing directory " << cumulated_path << ", index=" << indx << std::endl; @@ -187,6 +200,8 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_p return; } + time_t now = time(NULL) ; + if(mNeedsFullRecheck || dirIt.dir_modtime() > dir_local_mod_time) // the > is because we may have changed the virtual name, and therefore the TS wont match. // we only want to detect when the directory has changed on the disk { @@ -200,11 +215,23 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_p { switch(dirIt.file_type()) { - case librs::util::FolderIterator::TYPE_FILE: subfiles[dirIt.file_name()].modtime = dirIt.file_modtime() ; - subfiles[dirIt.file_name()].size = dirIt.file_size(); + case librs::util::FolderIterator::TYPE_FILE: + + if(dirIt.file_modtime() + MIN_TIME_AFTER_LAST_MODIFICATION < now) + { + subfiles[dirIt.file_name()].modtime = dirIt.file_modtime() ; + subfiles[dirIt.file_name()].size = dirIt.file_size(); #ifdef DEBUG_LOCAL_DIR_UPDATER - std::cerr << " adding sub-file \"" << dirIt.file_name() << "\"" << std::endl; + std::cerr << " adding sub-file \"" << dirIt.file_name() << "\"" << std::endl; #endif + } + else + { + some_files_not_ready = true ; + + std::cerr << "(WW) file " << dirIt.file_fullpath() << " is probably being modified. Keeping it for later." << std::endl; + } + break; case librs::util::FolderIterator::TYPE_DIR: @@ -276,7 +303,7 @@ void LocalDirectoryUpdater::recursUpdateSharedDir(const std::string& cumulated_p #ifdef DEBUG_LOCAL_DIR_UPDATER std::cerr << " recursing into " << stored_dir_it.name() << std::endl; #endif - recursUpdateSharedDir(cumulated_path + "/" + stored_dir_it.name(), *stored_dir_it,existing_directories,current_depth+1) ; + recursUpdateSharedDir(cumulated_path + "/" + stored_dir_it.name(), *stored_dir_it,existing_directories,current_depth+1,some_files_not_ready) ; } } diff --git a/libretroshare/src/file_sharing/directory_updater.h b/libretroshare/src/file_sharing/directory_updater.h index e4adb28b4..4c6e05649 100644 --- a/libretroshare/src/file_sharing/directory_updater.h +++ b/libretroshare/src/file_sharing/directory_updater.h @@ -70,8 +70,8 @@ protected: virtual void hash_callback(uint32_t client_param, const std::string& name, const RsFileHash& hash, uint64_t size); virtual bool hash_confirm(uint32_t client_param) ; - void recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx, std::set& existing_directories, uint32_t current_depth); - bool sweepSharedDirectories(); + void recursUpdateSharedDir(const std::string& cumulated_path, DirectoryStorage::EntryIndex indx, std::set& existing_directories, uint32_t current_depth,bool& files_not_ready); + bool sweepSharedDirectories(bool &some_files_not_ready); private: bool filterFile(const std::string& fname) const ; // reponds true if the file passes the ignore lists test. diff --git a/libretroshare/src/file_sharing/file_sharing_defaults.h b/libretroshare/src/file_sharing/file_sharing_defaults.h index 9e2dd6241..8de4b31c8 100644 --- a/libretroshare/src/file_sharing/file_sharing_defaults.h +++ b/libretroshare/src/file_sharing/file_sharing_defaults.h @@ -34,16 +34,16 @@ static const uint32_t DELAY_BETWEEN_REMOTE_DIRECTORIES_SWEEP = 60 ; // 60 se static const uint32_t DELAY_BEFORE_DELETE_NON_EMPTY_REMOTE_DIR = 60*24*86400 ; // delete non empty remoe directories after 60 days of inactivity static const uint32_t DELAY_BEFORE_DELETE_EMPTY_REMOTE_DIR = 5*24*86400 ; // delete empty remote directories after 5 days of inactivity -static const std::string HASH_CACHE_DURATION_SS = "HASH_CACHE_DURATION" ; // key string to store hash remembering time -static const std::string WATCH_FILE_DURATION_SS = "WATCH_FILES_DELAY" ; // key to store delay before re-checking for new files -static const std::string WATCH_FILE_ENABLED_SS = "WATCH_FILES_ENABLED"; // key to store ON/OFF flags for file whatch -static const std::string FOLLOW_SYMLINKS_SS = "FOLLOW_SYMLINKS"; // dereference symbolic links, or just ignore them. -static const std::string IGNORE_DUPLICATES = "IGNORE_DUPLICATES"; // do not index files that are referenced multiple times because of links -static const std::string WATCH_HASH_SALT_SS = "WATCH_HASH_SALT"; // Salt that is used to hash directory names -static const std::string IGNORED_PREFIXES_SS = "IGNORED_PREFIXES"; // ignore file prefixes -static const std::string IGNORED_SUFFIXES_SS = "IGNORED_SUFFIXES"; // ignore file suffixes -static const std::string IGNORE_LIST_FLAGS_SS = "IGNORED_FLAGS"; // ignore file flags -static const std::string MAX_SHARE_DEPTH = "MAX_SHARE_DEPTH"; // maximum depth of shared directories +static const std::string HASH_CACHE_DURATION_SS = "HASH_CACHE_DURATION" ; // key string to store hash remembering time +static const std::string WATCH_FILE_DURATION_SS = "WATCH_FILES_DELAY" ; // key to store delay before re-checking for new files +static const std::string WATCH_FILE_ENABLED_SS = "WATCH_FILES_ENABLED"; // key to store ON/OFF flags for file whatch +static const std::string FOLLOW_SYMLINKS_SS = "FOLLOW_SYMLINKS"; // dereference symbolic links, or just ignore them. +static const std::string IGNORE_DUPLICATES = "IGNORE_DUPLICATES"; // do not index files that are referenced multiple times because of links +static const std::string WATCH_HASH_SALT_SS = "WATCH_HASH_SALT"; // Salt that is used to hash directory names +static const std::string IGNORED_PREFIXES_SS = "IGNORED_PREFIXES"; // ignore file prefixes +static const std::string IGNORED_SUFFIXES_SS = "IGNORED_SUFFIXES"; // ignore file suffixes +static const std::string IGNORE_LIST_FLAGS_SS = "IGNORED_FLAGS"; // ignore file flags +static const std::string MAX_SHARE_DEPTH = "MAX_SHARE_DEPTH"; // maximum depth of shared directories static const std::string FILE_SHARING_DIR_NAME = "file_sharing" ; // hard-coded directory name to store friend file lists, hash cache, etc. static const std::string HASH_CACHE_FILE_NAME = "hash_cache.bin" ; // hard-coded directory name to store encrypted hash cache. @@ -51,6 +51,7 @@ static const std::string LOCAL_SHARED_DIRS_FILE_NAME = "local_dir_hierarchy.bin" static const uint32_t MIN_INTERVAL_BETWEEN_HASH_CACHE_SAVE = 20 ; // never save hash cache more often than every 20 secs. static const uint32_t MIN_INTERVAL_BETWEEN_REMOTE_DIRECTORY_SAVE = 23 ; // never save remote directories more often than this +static const uint32_t MIN_TIME_AFTER_LAST_MODIFICATION = 20 ; // never hash a file that is just being modified, otherwise we end up with a corrupted hash static const uint32_t MAX_DIR_SYNC_RESPONSE_DATA_SIZE = 20000 ; // Maximum RsItem data size in bytes for serialised directory transmission static const uint32_t DEFAULT_HASH_STORAGE_DURATION_DAYS = 30 ; // remember deleted/inaccessible files for 30 days diff --git a/libretroshare/src/file_sharing/file_tree.h b/libretroshare/src/file_sharing/file_tree.h index 8aaa9dff6..0505ceed5 100644 --- a/libretroshare/src/file_sharing/file_tree.h +++ b/libretroshare/src/file_sharing/file_tree.h @@ -1,3 +1,28 @@ +/* + * RetroShare C++ File lists IO methods. + * + * file_sharing/file_tree.h + * + * Copyright 2017 by csoler + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 2 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "retroshare.project@gmail.com". + * + */ + #include "retroshare/rsfiles.h" class FileTreeImpl: public FileTree diff --git a/libretroshare/src/ft/ftchunkmap.cc b/libretroshare/src/ft/ftchunkmap.cc index 3fd64892b..800408ec6 100644 --- a/libretroshare/src/ft/ftchunkmap.cc +++ b/libretroshare/src/ft/ftchunkmap.cc @@ -39,6 +39,7 @@ static const uint32_t SOURCE_CHUNK_MAP_UPDATE_PERIOD = 60 ; //! TTL for chunkmap info static const uint32_t INACTIVE_CHUNK_TIME_LAPSE = 3600 ; //! TTL for an inactive chunk static const uint32_t FT_CHUNKMAP_MAX_CHUNK_JUMP = 50 ; //! Maximum chunk jump in progressive DL mode +static const uint32_t FT_CHUNKMAP_MAX_SLICE_REASK_DELAY = 10 ; //! Maximum time to re-ask a slice to another peer at end of transfer std::ostream& operator<<(std::ostream& o,const ftChunk& c) { @@ -120,7 +121,7 @@ void ChunkMap::setAvailabilityMap(const CompressedChunkMap& map) } } -void ChunkMap::dataReceived(const ftChunk::ChunkId& cid) +void ChunkMap::dataReceived(const ftChunk::OffsetInFile& cid) { // 1 - find which chunk contains the received data. // @@ -139,7 +140,7 @@ void ChunkMap::dataReceived(const ftChunk::ChunkId& cid) return ; } - std::map::iterator it(itc->second._slices.find(cid)) ; + std::map::iterator it(itc->second._slices.find(cid)) ; if(it == itc->second._slices.end()) { @@ -150,8 +151,8 @@ void ChunkMap::dataReceived(const ftChunk::ChunkId& cid) return ; } - _total_downloaded += it->second ; - itc->second._remains -= it->second ; + _total_downloaded += it->second.size ; + itc->second._remains -= it->second.size ; itc->second._slices.erase(it) ; itc->second._last_data_received = time(NULL) ; // update time stamp @@ -256,6 +257,38 @@ void ChunkMap::setChunkCheckingResult(uint32_t chunk_number,bool check_succeeded } } +bool ChunkMap::reAskPendingChunk( const RsPeerId& peer_id, + uint32_t /*size_hint*/, + uint64_t& offset, uint32_t& size) +{ + // make sure that we're at the end of the file. No need to be too greedy in the middle of it. + + for(uint32_t i=0;i<_map.size();++i) + if(_map[i] == FileChunksInfo::CHUNK_OUTSTANDING) + return false ; + + time_t now = time(NULL); + + for(std::map::iterator it(_slices_to_download.begin());it!=_slices_to_download.end();++it) + for(std::map::iterator it2(it->second._slices.begin());it2!=it->second._slices.end();++it2) + if(it2->second.request_time + FT_CHUNKMAP_MAX_SLICE_REASK_DELAY < now && it2->second.peers.end()==it2->second.peers.find(peer_id)) + { + offset = it2->first; + size = it2->second.size ; + +#ifdef DEBUG_FTCHUNK + std::cerr << "*** ChunkMap::reAskPendingChunk: re-asking slice (" << offset << ", " << size << ") to peer " << peer_id << std::endl; +#endif + + it2->second.request_time = now ; + it2->second.peers.insert(peer_id) ; + + return true ; + } + + return false ; +} + // Warning: a chunk may be empty, but still being downloaded, so asking new slices from it // will produce slices of size 0. This happens at the end of each chunk. // --> I need to get slices from the next chunk, in such a case. @@ -295,7 +328,7 @@ bool ChunkMap::getDataChunk(const RsPeerId& peer_id,uint32_t size_hint,ftChunk& ChunkDownloadInfo& cdi(_slices_to_download[c]) ; falsafe_it = pit ; // let's keep this one just in case. - if(cdi._slices.rbegin() != cdi._slices.rend() && cdi._slices.rbegin()->second*0.7 <= (float)size_hint) + if(cdi._slices.rbegin() != cdi._slices.rend() && cdi._slices.rbegin()->second.size*0.7 <= (float)size_hint) { it = pit ; #ifdef DEBUG_FTCHUNK @@ -348,9 +381,14 @@ bool ChunkMap::getDataChunk(const RsPeerId& peer_id,uint32_t size_hint,ftChunk& // Get the first slice of the chunk, that is at most of length size // it->second.getSlice(size_hint,chunk) ; - _slices_to_download[chunk.offset/_chunk_size]._slices[chunk.id] = chunk.size ; _slices_to_download[chunk.offset/_chunk_size]._last_data_received = time(NULL) ; + ChunkDownloadInfo::SliceRequestInfo& r(_slices_to_download[chunk.offset/_chunk_size]._slices[chunk.id]); + + r.size = chunk.size ; + r.request_time = time(NULL); + r.peers.insert(peer_id); + chunk.peer_id = peer_id ; #ifdef DEBUG_FTCHUNK @@ -362,7 +400,7 @@ bool ChunkMap::getDataChunk(const RsPeerId& peer_id,uint32_t size_hint,ftChunk& return true ; } -void ChunkMap::removeInactiveChunks(std::vector& to_remove) +void ChunkMap::removeInactiveChunks(std::vector& to_remove) { to_remove.clear() ; time_t now = time(NULL) ; @@ -377,7 +415,7 @@ void ChunkMap::removeInactiveChunks(std::vector& to_remove) // std::map::iterator tmp(it) ; - for(std::map::const_iterator it2(it->second._slices.begin());it2!=it->second._slices.end();++it2) + for(std::map::const_iterator it2(it->second._slices.begin());it2!=it->second._slices.end();++it2) to_remove.push_back(it2->first) ; _map[it->first] = FileChunksInfo::CHUNK_OUTSTANDING ; // reset the chunk diff --git a/libretroshare/src/ft/ftchunkmap.h b/libretroshare/src/ft/ftchunkmap.h index 3ee4a1234..f7c696b9a 100644 --- a/libretroshare/src/ft/ftchunkmap.h +++ b/libretroshare/src/ft/ftchunkmap.h @@ -32,7 +32,7 @@ class ftController ; class ftChunk { public: - typedef uint64_t ChunkId ; + typedef uint64_t OffsetInFile ; ftChunk():offset(0), size(0), id(0), ts(0),ref_cnt(NULL) {} @@ -40,7 +40,7 @@ class ftChunk uint64_t offset; // current offset of the slice uint64_t size; // size remaining to download - ChunkId id ; // id of the chunk. Equal to the starting offset of the chunk + OffsetInFile id ; // id of the chunk. Equal to the starting offset of the chunk time_t ts; // time of last data received int *ref_cnt; // shared counter of number of sub-blocks. Used when a slice gets split. RsPeerId peer_id ; @@ -70,10 +70,17 @@ class Chunk uint64_t _end ; // const }; -class ChunkDownloadInfo +struct ChunkDownloadInfo { public: - std::map _slices ; + struct SliceRequestInfo + { + uint32_t size ; // size of the slice + time_t request_time ; // last request time + std::set peers ; // peers the slice was requested to. Normally only one, except at the end of the file. + }; + + std::map _slices ; uint32_t _remains ; time_t _last_data_received ; }; @@ -109,24 +116,29 @@ class ChunkMap /// destructor virtual ~ChunkMap() {} - /// Returns an slice of data to be asked to the peer within a chunk. + /// Returns an slice of data to be asked to the peer within a chunk. /// If a chunk is already been downloaded by this peer, take a slice at /// the beginning of this chunk, or at least where it starts. - /// If not, randomly/streamly select a new chunk depending on the strategy. - /// adds an entry in the chunk_ids map, and sets up 1 interval for it. - /// the chunk should be available from the designated peer. + /// If not, randomly/streamly select a new chunk depending on the strategy. + /// adds an entry in the chunk_ids map, and sets up 1 interval for it. + /// the chunk should be available from the designated peer. /// On return: /// - source_chunk_map_needed = true if the source map should be asked virtual bool getDataChunk(const RsPeerId& peer_id,uint32_t size_hint,ftChunk& chunk,bool& source_chunk_map_needed) ; - /// Notify received a slice of data. This needs to - /// - carve in the map of chunks what is received, what is not. - /// - tell which chunks are finished. For this, each interval must know what chunk number it has been attributed - /// when the interval is split in the middle, the number of intervals for the chunk is increased. If the interval is - /// completely covered by the data, the interval number is decreased. + /// Returns an already pending slice that was being downloaded but hasn't arrived yet. This is mostly used at the end of the file + /// in order to re-ask pendign slices to active peers while slow peers take a lot of time to send their remaining slices. + /// + bool reAskPendingChunk(const RsPeerId& peer_id,uint32_t size_hint,uint64_t& offset,uint32_t& size); - virtual void dataReceived(const ftChunk::ChunkId& c_id) ; + /// Notify received a slice of data. This needs to + /// - carve in the map of chunks what is received, what is not. + /// - tell which chunks are finished. For this, each interval must know what chunk number it has been attributed + /// when the interval is split in the middle, the number of intervals for the chunk is increased. If the interval is + /// completely covered by the data, the interval number is decreased. + + virtual void dataReceived(const ftChunk::OffsetInFile& c_id) ; /// Decides how chunks are selected. /// STREAMING: the 1st chunk is always returned @@ -163,7 +175,7 @@ class ChunkMap /// Remove active chunks that have not received any data for the last 60 seconds, and return /// the list of slice numbers that should be canceled. - void removeInactiveChunks(std::vector& to_remove) ; + void removeInactiveChunks(std::vector& to_remove) ; /// Updates the peer's availablility map // @@ -214,7 +226,7 @@ class ChunkMap uint32_t _chunk_size ; //! Size of chunks. Common to all chunks. FileChunksInfo::ChunkStrategy _strategy ; //! how do we allocate new chunks std::map _active_chunks_feed ; //! vector of chunks being downloaded. Exactly 1 chunk per peer. - std::map _slices_to_download ; //! list of (slice id,slice size) + std::map _slices_to_download ; //! list of (slice offset,slice size) currently being downloaded std::vector _map ; //! vector of chunk state over the whole file std::map _peers_chunks_availability ; //! what does each source peer have uint64_t _total_downloaded ; //! completion for the file diff --git a/libretroshare/src/ft/ftcontroller.cc b/libretroshare/src/ft/ftcontroller.cc index 0cb236358..9abec040f 100644 --- a/libretroshare/src/ft/ftcontroller.cc +++ b/libretroshare/src/ft/ftcontroller.cc @@ -88,16 +88,13 @@ ftFileControl::ftFileControl() return; } -ftFileControl::ftFileControl(std::string fname, - std::string tmppath, std::string dest, - uint64_t size, const RsFileHash &hash, TransferRequestFlags flags, - ftFileCreator *fc, ftTransferModule *tm) - :mName(fname), mCurrentPath(tmppath), mDestination(dest), - mTransfer(tm), mCreator(fc), mState(DOWNLOADING), mHash(hash), - mSize(size), mFlags(flags), mCreateTime(0), mQueuePriority(0), mQueuePosition(0) -{ - return; -} +ftFileControl::ftFileControl(const std::string& fname, const std::string& tmppath, const std::string& dest + , uint64_t size, const RsFileHash &hash, TransferRequestFlags flags + , ftFileCreator *fc, ftTransferModule *tm) + : mName(fname), mCurrentPath(tmppath), mDestination(dest) + , mTransfer(tm), mCreator(fc), mState(DOWNLOADING), mHash(hash) + , mSize(size), mFlags(flags), mCreateTime(0), mQueuePriority(0), mQueuePosition(0) +{} ftController::ftController(ftDataMultiplex *dm, p3ServiceControl *sc, uint32_t ftServiceId) : p3Config(), @@ -1475,24 +1472,24 @@ bool ftController::setPartialsDirectory(std::string path) /* check it is not a subdir of download / shared directories (BAD) - TODO */ { - RsStackMutex stack(ctrlMutex); + RsStackMutex stack(ctrlMutex); - path = RsDirUtil::convertPathToUnix(path); + path = RsDirUtil::convertPathToUnix(path); - if (!path.find(mDownloadPath)) { - return false; - } + if (path.find(mDownloadPath) != std::string::npos) { + return false; + } - if (rsFiles) { - std::list::iterator it; - std::list dirs; - rsFiles->getSharedDirectories(dirs); - for (it = dirs.begin(); it != dirs.end(); ++it) { - if (!path.find((*it).filename)) { - return false; - } - } - } + if (rsFiles) { + std::list::iterator it; + std::list dirs; + rsFiles->getSharedDirectories(dirs); + for (it = dirs.begin(); it != dirs.end(); ++it) { + if (path.find((*it).filename) != std::string::npos) { + return false; + } + } + } } /* check if it exists */ diff --git a/libretroshare/src/ft/ftcontroller.h b/libretroshare/src/ft/ftcontroller.h index 67b080843..564660879 100644 --- a/libretroshare/src/ft/ftcontroller.h +++ b/libretroshare/src/ft/ftcontroller.h @@ -73,9 +73,9 @@ class ftFileControl }; ftFileControl(); - ftFileControl(std::string fname, std::string tmppath, std::string dest, - uint64_t size, const RsFileHash& hash, TransferRequestFlags flags, - ftFileCreator *fc, ftTransferModule *tm); + ftFileControl( const std::string& fname, const std::string& tmppath, const std::string& dest + , uint64_t size, const RsFileHash& hash, TransferRequestFlags flags + , ftFileCreator *fc, ftTransferModule *tm); std::string mName; std::string mCurrentPath; /* current full path (including name) */ @@ -175,8 +175,8 @@ class ftController: public RsTickingThread, public pqiServiceMonitor, public p3C void FileDownloads(std::list &hashs); /* Directory Handling */ - bool setDownloadDirectory(std::string path); - bool setPartialsDirectory(std::string path); + bool setDownloadDirectory(std::string path); + bool setPartialsDirectory(std::string path); std::string getDownloadDirectory(); std::string getPartialsDirectory(); bool FileDetails(const RsFileHash &hash, FileInfo &info); diff --git a/libretroshare/src/ft/ftfilecreator.cc b/libretroshare/src/ft/ftfilecreator.cc index c47cc1c4c..56bf78bf7 100644 --- a/libretroshare/src/ft/ftfilecreator.cc +++ b/libretroshare/src/ft/ftfilecreator.cc @@ -240,7 +240,7 @@ void ftFileCreator::removeInactiveChunks() #ifdef FILE_DEBUG std::cerr << "ftFileCreator::removeInactiveChunks(): looking for old chunks." << std::endl ; #endif - std::vector to_remove ; + std::vector to_remove ; chunkMap.removeInactiveChunks(to_remove) ; @@ -421,7 +421,9 @@ int ftFileCreator::locked_notifyReceived(uint64_t offset, uint32_t chunk_size) if(!found) { +#ifdef FILE_DEBUG std::cerr << "ftFileCreator::locked_notifyReceived(): failed to find an active slice for " << offset << "+" << chunk_size << ", hash = " << hash << ": dropping data." << std::endl; +#endif return 0; /* ignoring */ } } @@ -531,7 +533,14 @@ bool ftFileCreator::getMissingChunk(const RsPeerId& peer_id,uint32_t size_hint,u ftChunk chunk ; if(!chunkMap.getDataChunk(peer_id,size_hint,chunk,source_chunk_map_needed)) + { + // No chunks are available. We brutally re-ask an ongoing chunk to another peer. + + if(chunkMap.reAskPendingChunk(peer_id,size_hint,offset,size)) + return true ; + return false ; + } #ifdef FILE_DEBUG std::cerr << "ffc::getMissingChunk() Retrieved new chunk: " << chunk << std::endl ; diff --git a/libretroshare/src/ft/ftserver.cc b/libretroshare/src/ft/ftserver.cc index a454be33f..2c628dd89 100644 --- a/libretroshare/src/ft/ftserver.cc +++ b/libretroshare/src/ft/ftserver.cc @@ -23,35 +23,40 @@ * */ -#include -#include -#include "util/rsdebug.h" -#include "util/rsdir.h" -#include "util/rsprint.h" #include "crypto/chacha20.h" -#include "retroshare/rstypes.h" -#include "retroshare/rspeers.h" //const int ftserverzone = 29539; #include "file_sharing/p3filelists.h" -#include "ft/ftturtlefiletransferitem.h" -#include "ft/ftserver.h" -#include "ft/ftextralist.h" -#include "ft/ftfilesearch.h" #include "ft/ftcontroller.h" -#include "ft/ftfileprovider.h" #include "ft/ftdatamultiplex.h" //#include "ft/ftdwlqueue.h" -#include "turtle/p3turtle.h" -#include "pqi/p3notify.h" -#include "rsserver/p3face.h" +#include "ft/ftextralist.h" +#include "ft/ftfileprovider.h" +#include "ft/ftfilesearch.h" +#include "ft/ftserver.h" +#include "ft/ftturtlefiletransferitem.h" -#include "pqi/pqi.h" #include "pqi/p3linkmgr.h" +#include "pqi/p3notify.h" +#include "pqi/pqi.h" + +#include "retroshare/rstypes.h" +#include "retroshare/rspeers.h" #include "rsitems/rsfiletransferitems.h" #include "rsitems/rsserviceids.h" +#include "rsserver/p3face.h" +#include "rsserver/rsaccounts.h" +#include "turtle/p3turtle.h" + +#include "util/rsdebug.h" +#include "util/rsdir.h" +#include "util/rsprint.h" + +#include +#include + /*** * #define SERVER_DEBUG 1 * #define SERVER_DEBUG_CACHE 1 @@ -145,9 +150,19 @@ void ftServer::SetupFtServer() /* make Controller */ mFtController = new ftController(mFtDataplex, mServiceCtrl, getServiceInfo().mServiceType); mFtController -> setFtSearchNExtra(mFtSearch, mFtExtra); - std::string tmppath = "."; - mFtController->setPartialsDirectory(tmppath); - mFtController->setDownloadDirectory(tmppath); + + std::string emergencySaveDir = rsAccounts->PathAccountDirectory(); + std::string emergencyPartialsDir = rsAccounts->PathAccountDirectory(); + if (emergencySaveDir != "") + { + emergencySaveDir += "/"; + emergencyPartialsDir += "/"; + } + emergencySaveDir += "Downloads"; + emergencyPartialsDir += "Partials"; + + mFtController->setDownloadDirectory(emergencySaveDir); + mFtController->setPartialsDirectory(emergencyPartialsDir); /* complete search setup */ mFtSearch->addSearchMode(mFtExtra, RS_FILE_HINTS_EXTRA); @@ -412,9 +427,9 @@ void ftServer::requestDirUpdate(void *ref) } /* Directory Handling */ -void ftServer::setDownloadDirectory(std::string path) +bool ftServer::setDownloadDirectory(std::string path) { - mFtController->setDownloadDirectory(path); + return mFtController->setDownloadDirectory(path); } std::string ftServer::getDownloadDirectory() @@ -422,9 +437,9 @@ std::string ftServer::getDownloadDirectory() return mFtController->getDownloadDirectory(); } -void ftServer::setPartialsDirectory(std::string path) +bool ftServer::setPartialsDirectory(std::string path) { - mFtController->setPartialsDirectory(path); + return mFtController->setPartialsDirectory(path); } std::string ftServer::getPartialsDirectory() diff --git a/libretroshare/src/ft/ftserver.h b/libretroshare/src/ft/ftserver.h index cd0101933..eefe2b44a 100644 --- a/libretroshare/src/ft/ftserver.h +++ b/libretroshare/src/ft/ftserver.h @@ -208,8 +208,8 @@ public: * Directory Handling ***/ virtual void requestDirUpdate(void *ref) ; // triggers the update of the given reference. Used when browsing. - virtual void setDownloadDirectory(std::string path); - virtual void setPartialsDirectory(std::string path); + virtual bool setDownloadDirectory(std::string path); + virtual bool setPartialsDirectory(std::string path); virtual std::string getDownloadDirectory(); virtual std::string getPartialsDirectory(); diff --git a/libretroshare/src/grouter/grouterclientservice.h b/libretroshare/src/grouter/grouterclientservice.h index a6bef29e1..35c22614a 100644 --- a/libretroshare/src/grouter/grouterclientservice.h +++ b/libretroshare/src/grouter/grouterclientservice.h @@ -34,7 +34,7 @@ #include #include -class RsItem ; +struct RsItem; static const uint32_t GROUTER_CLIENT_SERVICE_DATA_STATUS_UNKNOWN = 0x0000 ; // unused. static const uint32_t GROUTER_CLIENT_SERVICE_DATA_STATUS_RECEIVED = 0x0001 ; // sent when data has been received and a receipt is available. diff --git a/libretroshare/src/grouter/groutermatrix.h b/libretroshare/src/grouter/groutermatrix.h index 99c9ae1bd..cb567b9bc 100644 --- a/libretroshare/src/grouter/groutermatrix.h +++ b/libretroshare/src/grouter/groutermatrix.h @@ -31,7 +31,7 @@ #include "retroshare/rsgrouter.h" #include "groutertypes.h" -class RsItem ; +struct RsItem; // The routing matrix records the event clues received from each friend // diff --git a/libretroshare/src/grouter/p3grouter.cc b/libretroshare/src/grouter/p3grouter.cc index 656da0f9c..2555883ec 100644 --- a/libretroshare/src/grouter/p3grouter.cc +++ b/libretroshare/src/grouter/p3grouter.cc @@ -541,7 +541,7 @@ if(itm == NULL) void GRouterTunnelInfo::removeVirtualPeer(const TurtleVirtualPeerId& vpid) { - std::set::iterator it = virtual_peers.find(vpid) ; + std::set::iterator it = virtual_peers.find(vpid) ; if(it == virtual_peers.end()) { diff --git a/libretroshare/src/gxs/rsdataservice.cc b/libretroshare/src/gxs/rsdataservice.cc index 5fddf1897..0b4d0f1da 100644 --- a/libretroshare/src/gxs/rsdataservice.cc +++ b/libretroshare/src/gxs/rsdataservice.cc @@ -952,6 +952,8 @@ void RsDataService::locked_updateGrpMetaCache(const RsGxsGrpMetaData& meta) if(it != mGrpMetaDataCache.end()) *(it->second) = meta ; + else + mGrpMetaDataCache[meta.mGroupId] = new RsGxsGrpMetaData(meta) ; } void RsDataService::locked_clearGrpMetaCache(const RsGxsGroupId& gid) @@ -1215,7 +1217,7 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b const RsGxsGroupId& grpId = mit->first; // if vector empty then request all messages - const std::vector& msgIdV = mit->second; + const std::set& msgIdV = mit->second; std::vector msgSet; if(msgIdV.empty()){ @@ -1233,7 +1235,7 @@ int RsDataService::retrieveNxsMsgs(const GxsMsgReq &reqIds, GxsMsgResult &msg, b }else{ // request each grp - std::vector::const_iterator sit = msgIdV.begin(); + std::set::const_iterator sit = msgIdV.begin(); for(; sit!=msgIdV.end();++sit){ const RsGxsMessageId& msgId = *sit; @@ -1303,7 +1305,7 @@ int RsDataService::retrieveGxsMsgMetaData(const GxsMsgReq& reqIds, GxsMsgMetaRes const RsGxsGroupId& grpId = mit->first; // if vector empty then request all messages - const std::vector& msgIdV = mit->second; + const std::set& msgIdV = mit->second; std::vector metaSet; if(msgIdV.empty()){ @@ -1319,7 +1321,7 @@ int RsDataService::retrieveGxsMsgMetaData(const GxsMsgReq& reqIds, GxsMsgMetaRes }else{ // request each grp - std::vector::const_iterator sit = msgIdV.begin(); + std::set::const_iterator sit = msgIdV.begin(); for(; sit!=msgIdV.end(); ++sit){ const RsGxsMessageId& msgId = *sit; @@ -1559,7 +1561,7 @@ int RsDataService::removeMsgs(const GxsMsgReq& msgIds) for(; mit != msgIds.end(); ++mit) { - const std::vector& msgIdV = mit->second; + const std::set& msgIdV = mit->second; const RsGxsGroupId& grpId = mit->first; // delete messages @@ -1620,7 +1622,7 @@ int RsDataService::retrieveGroupIds(std::vector &grpIds) return 1; } -int RsDataService::retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_vector& msgIds) +int RsDataService::retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_set& msgIds) { #ifdef RS_DATA_SERVICE_DEBUG_TIME rstime::RsScopeTimer timer(""); @@ -1641,7 +1643,7 @@ int RsDataService::retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std if(c->columnCount() != 1) std::cerr << "(EE) ********* not retrieving all columns!!" << std::endl; - msgIds.push_back(RsGxsMessageId(msgId)); + msgIds.insert(RsGxsMessageId(msgId)); valid = c->moveToNext(); #ifdef RS_DATA_SERVICE_DEBUG_TIME @@ -1672,8 +1674,8 @@ bool RsDataService::locked_removeMessageEntries(const GxsMsgReq& msgIds) for(; mit != msgIds.end(); ++mit) { const RsGxsGroupId& grpId = mit->first; - const std::vector& msgsV = mit->second; - std::vector::const_iterator vit = msgsV.begin(); + const std::set& msgsV = mit->second; + std::set::const_iterator vit = msgsV.begin(); for(; vit != msgsV.end(); ++vit) { diff --git a/libretroshare/src/gxs/rsdataservice.h b/libretroshare/src/gxs/rsdataservice.h index c669ee016..cc137b320 100644 --- a/libretroshare/src/gxs/rsdataservice.h +++ b/libretroshare/src/gxs/rsdataservice.h @@ -110,7 +110,7 @@ public: * @param msgId msgsids retrieved * @return error code */ - int retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_vector& msgId); + int retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_set& msgId); /*! * @return the cache size set for this RsGeneralDataService in bytes diff --git a/libretroshare/src/gxs/rsgds.h b/libretroshare/src/gxs/rsgds.h index e20d0ed04..78987ac66 100644 --- a/libretroshare/src/gxs/rsgds.h +++ b/libretroshare/src/gxs/rsgds.h @@ -208,7 +208,7 @@ public: * @param msgId msgsids retrieved * @return error code */ - virtual int retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_vector& msgId) = 0; + virtual int retrieveMsgIds(const RsGxsGroupId& grpId, RsGxsMessageId::std_set& msgId) = 0; /*! * @return the cache size set for this RsGeneralDataService in bytes diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index f7609a8f9..8da9e606a 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -217,7 +217,7 @@ void RsGenExchange::tick() RS_STACK_MUTEX(mGenMtx) ; std::list grpIds; - std::map > msgIds; + std::map > msgIds; mIntegrityCheck->getDeletedIds(grpIds, msgIds); if (!grpIds.empty()) @@ -1078,23 +1078,19 @@ bool RsGenExchange::checkAuthenFlag(const PrivacyBitPos& pos, const uint8_t& fla } } -static void addMessageChanged(std::map > &msgs, const std::map > &msgChanged) +static void addMessageChanged(std::map > &msgs, const std::map > &msgChanged) { if (msgs.empty()) { msgs = msgChanged; } else { - std::map >::const_iterator mapIt; - for (mapIt = msgChanged.begin(); mapIt != msgChanged.end(); ++mapIt) { + for (auto mapIt = msgChanged.begin(); mapIt != msgChanged.end(); ++mapIt) + { const RsGxsGroupId &grpId = mapIt->first; - const std::vector &srcMsgIds = mapIt->second; - std::vector &destMsgIds = msgs[grpId]; + const std::set &srcMsgIds = mapIt->second; + std::set &destMsgIds = msgs[grpId]; - std::vector::const_iterator msgIt; - for (msgIt = srcMsgIds.begin(); msgIt != srcMsgIds.end(); ++msgIt) { - if (std::find(destMsgIds.begin(), destMsgIds.end(), *msgIt) == destMsgIds.end()) { - destMsgIds.push_back(*msgIt); - } - } + for (auto msgIt = srcMsgIds.begin(); msgIt != srcMsgIds.end(); ++msgIt) + destMsgIds.insert(*msgIt) ; } } } @@ -1796,8 +1792,8 @@ void RsGenExchange::deleteMsgs(uint32_t& token, const GxsMsgReq& msgs) if(mNetService != NULL) for(GxsMsgReq::const_iterator it(msgs.begin());it!=msgs.end();++it) - for(uint32_t i=0;isecond.size();++i) - mNetService->rejectMessage(it->second[i]) ; + for(auto it2(it->second.begin());it2!=it->second.end();++it2) + mNetService->rejectMessage(*it2); } void RsGenExchange::publishMsg(uint32_t& token, RsGxsMsgItem *msgItem) @@ -1977,8 +1973,8 @@ void RsGenExchange::processMsgMetaChanges() if(m.val.getAsInt32(RsGeneralDataService::MSG_META_STATUS+GXS_MASK, mask)) { GxsMsgReq req; - std::vector msgIdV; - msgIdV.push_back(m.msgId.second); + std::set msgIdV; + msgIdV.insert(m.msgId.second); req.insert(std::make_pair(m.msgId.first, msgIdV)); GxsMsgMetaResult result; mDataStore->retrieveGxsMsgMetaData(req, result); @@ -2010,7 +2006,7 @@ void RsGenExchange::processMsgMetaChanges() mDataAccess->updatePublicRequestStatus(token, RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE); if (changed) { - msgIds[m.msgId.first].push_back(m.msgId.second); + msgIds[m.msgId.first].insert(m.msgId.second); } } else @@ -2026,7 +2022,7 @@ void RsGenExchange::processMsgMetaChanges() if (!msgIds.empty()) { RS_STACK_MUTEX(mGenMtx); - RsGxsMsgChange* c = new RsGxsMsgChange(RsGxsNotify::TYPE_PROCESSED, true); + RsGxsMsgChange* c = new RsGxsMsgChange(RsGxsNotify::TYPE_PROCESSED, false); c->msgChangeMap = msgIds; mNotifications.push_back(c); } @@ -2161,7 +2157,7 @@ void RsGenExchange::publishMsgs() mMsgsToPublish.insert(std::make_pair(sign_it->first, item.mItem)); } - std::map > msgChangeMap; + std::map > msgChangeMap; std::map::iterator mit = mMsgsToPublish.begin(); for(; mit != mMsgsToPublish.end(); ++mit) @@ -2289,7 +2285,7 @@ void RsGenExchange::publishMsgs() mDataAccess->addMsgData(msg); delete msg ; - msgChangeMap[grpId].push_back(msgId); + msgChangeMap[grpId].insert(msgId); delete[] metaDataBuff; @@ -2988,10 +2984,10 @@ void RsGenExchange::processRecvdMessages() msg->metaData->mMsgStatus = GXS_SERV::GXS_MSG_STATUS_UNPROCESSED | GXS_SERV::GXS_MSG_STATUS_GUI_NEW | GXS_SERV::GXS_MSG_STATUS_GUI_UNREAD; msgs_to_store.push_back(msg); - std::vector &msgv = msgIds[msg->grpId]; - - if (std::find(msgv.begin(), msgv.end(), msg->msgId) == msgv.end()) - msgv.push_back(msg->msgId); + msgIds[msg->grpId].insert(msg->msgId); + // std::vector &msgv = msgIds[msg->grpId]; + // if (std::find(msgv.begin(), msgv.end(), msg->msgId) == msgv.end()) + // msgv.push_back(msg->msgId); computeHash(msg->msg, msg->metaData->mHash); msg->metaData->recvTS = time(NULL); @@ -3346,7 +3342,7 @@ void RsGenExchange::removeDeleteExistingMessages( std::list& msgs, Gx //RsGxsGroupId::std_list grpIds(mGrpIdsUnique.begin(), mGrpIdsUnique.end()); //RsGxsGroupId::std_list::const_iterator it = grpIds.begin(); - typedef std::map MsgIdReq; + typedef std::map MsgIdReq; MsgIdReq msgIdReq; // now get a list of all msgs ids for each group @@ -3355,18 +3351,18 @@ void RsGenExchange::removeDeleteExistingMessages( std::list& msgs, Gx mDataStore->retrieveMsgIds(*it, msgIdReq[*it]); #ifdef GEN_EXCH_DEBUG - const std::vector& vec(msgIdReq[*it]) ; + const std::set& vec(msgIdReq[*it]) ; std::cerr << " retrieved " << vec.size() << " message ids for group " << *it << std::endl; - for(uint32_t i=0;i::iterator cit2 = msgs.begin(); cit2 != msgs.end();) { - const RsGxsMessageId::std_vector& msgIds = msgIdReq[(*cit2)->metaData->mGroupId]; + const RsGxsMessageId::std_set& msgIds = msgIdReq[(*cit2)->metaData->mGroupId]; #ifdef GEN_EXCH_DEBUG std::cerr << " grpid=" << (*cit2)->grpId << ", msgid=" << (*cit2)->msgId ; @@ -3374,12 +3370,13 @@ void RsGenExchange::removeDeleteExistingMessages( std::list& msgs, Gx // Avoid storing messages that are already in the database, as well as messages that are too old (or generally do not pass the database storage test) // - if(std::find(msgIds.begin(), msgIds.end(), (*cit2)->metaData->mMsgId) != msgIds.end() || !messagePublicationTest( *(*cit2)->metaData)) + if(msgIds.find((*cit2)->metaData->mMsgId) != msgIds.end() || !messagePublicationTest( *(*cit2)->metaData)) { // msg exist in retrieved index. We should use a std::set here instead of a vector. - RsGxsMessageId::std_vector& notifyIds = msgIdsNotify[ (*cit2)->metaData->mGroupId]; - RsGxsMessageId::std_vector::iterator it2 = std::find(notifyIds.begin(), notifyIds.end(), (*cit2)->metaData->mMsgId); + RsGxsMessageId::std_set& notifyIds = msgIdsNotify[ (*cit2)->metaData->mGroupId]; + RsGxsMessageId::std_set::iterator it2 = notifyIds.find((*cit2)->metaData->mMsgId); + if(it2 != notifyIds.end()) { notifyIds.erase(it2); diff --git a/libretroshare/src/gxs/rsgxsdataaccess.cc b/libretroshare/src/gxs/rsgxsdataaccess.cc index 5d36bd909..77c0d05d9 100644 --- a/libretroshare/src/gxs/rsgxsdataaccess.cc +++ b/libretroshare/src/gxs/rsgxsdataaccess.cc @@ -225,7 +225,7 @@ bool RsGxsDataAccess::requestMsgInfo(uint32_t &token, uint32_t ansType, MsgMetaReq* mmr = new MsgMetaReq(); for(; lit != grpIds.end(); ++lit) - mmr->mMsgIds[*lit] = std::vector(); + mmr->mMsgIds[*lit] = std::set(); req = mmr; }else if(reqType & GXS_REQUEST_TYPE_MSG_DATA) @@ -233,7 +233,7 @@ bool RsGxsDataAccess::requestMsgInfo(uint32_t &token, uint32_t ansType, MsgDataReq* mdr = new MsgDataReq(); for(; lit != grpIds.end(); ++lit) - mdr->mMsgIds[*lit] = std::vector(); + mdr->mMsgIds[*lit] = std::set(); req = mdr; }else if(reqType & GXS_REQUEST_TYPE_MSG_IDS) @@ -241,7 +241,7 @@ bool RsGxsDataAccess::requestMsgInfo(uint32_t &token, uint32_t ansType, MsgIdReq* mir = new MsgIdReq(); for(; lit != grpIds.end(); ++lit) - mir->mMsgIds[*lit] = std::vector(); + mir->mMsgIds[*lit] = std::set(); req = mir; } @@ -1191,7 +1191,7 @@ bool RsGxsDataAccess::getMsgList(const GxsMsgReq& msgIds, const RsTokReqOptions& // Add the discovered Latest Msgs. for(oit = origMsgTs.begin(); oit != origMsgTs.end(); ++oit) { - msgIdsOut[grpId].push_back(oit->second.first); + msgIdsOut[grpId].insert(oit->second.first); } } @@ -1228,7 +1228,7 @@ bool RsGxsDataAccess::getMsgList(const GxsMsgReq& msgIds, const RsTokReqOptions& if (add) { - msgIdsOut[grpId].push_back(msgMeta->mMsgId); + msgIdsOut[grpId].insert(msgMeta->mMsgId); metaFilter[grpId].insert(std::make_pair(msgMeta->mMsgId, msgMeta)); } @@ -1373,7 +1373,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) // get meta data for all in group GxsMsgMetaResult result; GxsMsgReq msgIds; - msgIds.insert(std::make_pair(grpMsgIdPair.first, std::vector())); + msgIds.insert(std::make_pair(grpMsgIdPair.first, std::set())); mDataStore->retrieveGxsMsgMetaData(msgIds, result); std::vector& metaV = result[grpMsgIdPair.first]; std::vector::iterator vit_meta; @@ -1382,7 +1382,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) const RsGxsMessageId& msgId = grpMsgIdPair.second; const RsGxsGroupId& grpId = grpMsgIdPair.first; - std::vector outMsgIds; + std::set outMsgIds; RsGxsMsgMetaData* origMeta = NULL; for(vit_meta = metaV.begin(); vit_meta != metaV.end(); ++vit_meta) @@ -1477,7 +1477,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) // Add the discovered Latest Msgs. for(oit = origMsgTs.begin(); oit != origMsgTs.end(); ++oit) { - outMsgIds.push_back(oit->second.first); + outMsgIds.insert(oit->second.first); } } else @@ -1502,7 +1502,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) } } } - outMsgIds.push_back(latestMsgId); + outMsgIds.insert(latestMsgId); metaMap.insert(std::make_pair(latestMsgId, latestMeta)); } } @@ -1514,7 +1514,7 @@ bool RsGxsDataAccess::getMsgRelatedInfo(MsgRelatedInfoReq *req) if (meta->mOrigMsgId == origMsgId) { - outMsgIds.push_back(meta->mMsgId); + outMsgIds.insert(meta->mMsgId); metaMap.insert(std::make_pair(meta->mMsgId, meta)); } } @@ -1556,7 +1556,7 @@ bool RsGxsDataAccess::getGroupStatistic(GroupStatisticRequest *req) { // filter based on options GxsMsgIdResult metaReq; - metaReq[req->mGrpId] = std::vector(); + metaReq[req->mGrpId] = std::set(); GxsMsgMetaResult metaResult; mDataStore->retrieveGxsMsgMetaData(metaReq, metaResult); @@ -1672,7 +1672,7 @@ bool RsGxsDataAccess::getMsgList(MsgIdReq* req) for(; vit != vit_end; ++vit) { RsGxsMsgMetaData* meta = *vit; - req->mMsgIdResult[grpId].push_back(meta->mMsgId); + req->mMsgIdResult[grpId].insert(meta->mMsgId); delete meta; // discard meta data mem } } @@ -1718,8 +1718,8 @@ void RsGxsDataAccess::filterMsgList(GxsMsgIdResult& msgIds, const RsTokReqOption if(cit == msgMetas.end()) continue; - std::vector& msgs = mit->second; - std::vector::iterator vit = msgs.begin(); + std::set& msgs = mit->second; + std::set::iterator vit = msgs.begin(); const std::map& meta = cit->second; std::map::const_iterator cit2; diff --git a/libretroshare/src/gxs/rsgxsnetservice.cc b/libretroshare/src/gxs/rsgxsnetservice.cc index e5dfbd05a..f456f4d7e 100644 --- a/libretroshare/src/gxs/rsgxsnetservice.cc +++ b/libretroshare/src/gxs/rsgxsnetservice.cc @@ -940,7 +940,7 @@ void RsGxsNetService::handleRecvSyncGrpStatistics(RsNxsSyncGrpStatsItem *grs) // now count available messages GxsMsgReq reqIds; - reqIds[grs->grpId] = std::vector(); + reqIds[grs->grpId] = std::set(); GxsMsgMetaResult result; #ifdef NXS_NET_DEBUG_6 @@ -2945,7 +2945,7 @@ void RsGxsNetService::locked_genReqMsgTransaction(NxsTransaction* tr) #endif GxsMsgReq reqIds; - reqIds[grpId] = std::vector(); + reqIds[grpId] = std::set(); GxsMsgMetaResult result; mDataStore->retrieveGxsMsgMetaData(reqIds, result); std::vector &msgMetaV = result[grpId]; @@ -3497,7 +3497,7 @@ void RsGxsNetService::locked_genSendMsgsTransaction(NxsTransaction* tr) RsNxsSyncMsgItem* item = dynamic_cast(*lit); if (item) { - msgIds[item->grpId].push_back(item->msgId); + msgIds[item->grpId].insert(item->msgId); if(grpId.isNull()) grpId = item->grpId; @@ -4351,7 +4351,7 @@ void RsGxsNetService::handleRecvSyncMessage(RsNxsSyncMsgReqItem *item,bool item_ } GxsMsgReq req; - req[item->grpId] = std::vector(); + req[item->grpId] = std::set(); GxsMsgMetaResult metaResult; mDataStore->retrieveGxsMsgMetaData(req, metaResult); diff --git a/libretroshare/src/gxs/rsgxsutil.cc b/libretroshare/src/gxs/rsgxsutil.cc index abf02aef2..bb0b1fb05 100644 --- a/libretroshare/src/gxs/rsgxsutil.cc +++ b/libretroshare/src/gxs/rsgxsutil.cc @@ -67,7 +67,7 @@ bool RsGxsMessageCleanUp::clean() GxsMsgReq req; GxsMsgMetaResult result; - req[grpId] = std::vector(); + req[grpId] = std::set(); mDs->retrieveGxsMsgMetaData(req, result); GxsMsgMetaResult::iterator mit = result.begin(); @@ -117,7 +117,7 @@ bool RsGxsMessageCleanUp::clean() if( remove ) { - req[grpId].push_back(meta->mMsgId); + req[grpId].insert(meta->mMsgId); #ifdef DEBUG_GXSUTIL std::cerr << " Scheduling for removal." << std::endl; @@ -241,9 +241,9 @@ bool RsGxsIntegrityCheck::check() for (msgIdsIt = msgIds.begin(); msgIdsIt != msgIds.end(); ++msgIdsIt) { const RsGxsGroupId& grpId = msgIdsIt->first; - std::vector &msgIdV = msgIdsIt->second; + std::set &msgIdV = msgIdsIt->second; - std::vector::iterator msgIdIt; + std::set::iterator msgIdIt; for (msgIdIt = msgIdV.begin(); msgIdIt != msgIdV.end(); ++msgIdIt) { const RsGxsMessageId& msgId = *msgIdIt; @@ -261,7 +261,7 @@ bool RsGxsIntegrityCheck::check() if (nxsMsgIt == nxsMsgV.end()) { - msgsToDel[grpId].push_back(msgId); + msgsToDel[grpId].insert(msgId); } } } @@ -284,7 +284,7 @@ bool RsGxsIntegrityCheck::check() if(msg->metaData == NULL || currHash != msg->metaData->mHash) { std::cerr << "(EE) deleting message data with wrong hash or null meta data. meta=" << (void*)msg->metaData << std::endl; - msgsToDel[msg->grpId].push_back(msg->msgId); + msgsToDel[msg->grpId].insert(msg->msgId); } else if(!msg->metaData->mAuthorId.isNull() && subscribed_groups.find(msg->metaData->mGroupId)!=subscribed_groups.end()) { @@ -377,7 +377,7 @@ bool RsGxsIntegrityCheck::isDone() return mDone; } -void RsGxsIntegrityCheck::getDeletedIds(std::list& grpIds, std::map >& msgIds) +void RsGxsIntegrityCheck::getDeletedIds(std::list& grpIds, std::map >& msgIds) { RsStackMutex stack(mIntegrityMutex); diff --git a/libretroshare/src/gxs/rsgxsutil.h b/libretroshare/src/gxs/rsgxsutil.h index b169e00b5..70e832c3e 100644 --- a/libretroshare/src/gxs/rsgxsutil.h +++ b/libretroshare/src/gxs/rsgxsutil.h @@ -208,7 +208,7 @@ public: void run(); - void getDeletedIds(std::list& grpIds, std::map >& msgIds); + void getDeletedIds(std::list& grpIds, std::map > &msgIds); private: @@ -217,7 +217,7 @@ private: bool mDone; RsMutex mIntegrityMutex; std::list mDeletedGrps; - std::map > mDeletedMsgs; + std::map > mDeletedMsgs; RsGixs *mGixs ; }; diff --git a/libretroshare/src/gxstrans/p3gxstrans.cc b/libretroshare/src/gxstrans/p3gxstrans.cc index 05c37cdb5..9505dfdb4 100644 --- a/libretroshare/src/gxstrans/p3gxstrans.cc +++ b/libretroshare/src/gxstrans/p3gxstrans.cc @@ -455,7 +455,7 @@ void p3GxsTrans::GxsTransIntegrityCleanupThread::run() if(stored_msgs.end() != it2) { - msgsToDel[it2->second.first].push_back(it2->second.second); + msgsToDel[it2->second.first].insert(it2->second.second); #ifdef DEBUG_GXSTRANS std::cerr << " scheduling msg " << std::hex << it2->second.first << "," << it2->second.second << " for deletion." << std::endl; diff --git a/libretroshare/src/gxstrans/p3gxstransitems.cc b/libretroshare/src/gxstrans/p3gxstransitems.cc index cebbd9e69..5d28ec582 100644 --- a/libretroshare/src/gxstrans/p3gxstransitems.cc +++ b/libretroshare/src/gxstrans/p3gxstransitems.cc @@ -1,6 +1,6 @@ /* * GXS Mailing Service - * Copyright (C) 2016-2017 Gioacchino Mazzurco + * Copyright (C) 2016-2018 Gioacchino Mazzurco * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -38,30 +38,28 @@ OutgoingRecord::OutgoingRecord( RsGxsId rec, GxsTransSubServices cs, memcpy(&mailData[0], data, size); } - -RS_REGISTER_ITEM_TYPE(RsGxsTransMailItem) // for mailItem -RS_REGISTER_ITEM_TYPE(RsNxsTransPresignedReceipt) // for presignedReceipt - -void OutgoingRecord_deprecated::serial_process(RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx) +void OutgoingRecord_deprecated::serial_process( + RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) { - RS_REGISTER_SERIAL_MEMBER_TYPED(status, uint8_t); - RS_REGISTER_SERIAL_MEMBER(recipient); - RS_REGISTER_SERIAL_MEMBER(mailItem); - RS_REGISTER_SERIAL_MEMBER(mailData); - RS_REGISTER_SERIAL_MEMBER_TYPED(clientService, uint16_t); - RS_REGISTER_SERIAL_MEMBER(presignedReceipt); + RS_SERIAL_PROCESS(status); + RS_SERIAL_PROCESS(recipient); + RS_SERIAL_PROCESS(mailItem); + RS_SERIAL_PROCESS(mailData); + RS_SERIAL_PROCESS(clientService); + RS_SERIAL_PROCESS(presignedReceipt); } void OutgoingRecord::serial_process(RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx) { - RS_REGISTER_SERIAL_MEMBER_TYPED(status, uint8_t); - RS_REGISTER_SERIAL_MEMBER(recipient); - RS_REGISTER_SERIAL_MEMBER(author); - RS_REGISTER_SERIAL_MEMBER(group_id); - RS_REGISTER_SERIAL_MEMBER(sent_ts); - RS_REGISTER_SERIAL_MEMBER(mailItem); - RS_REGISTER_SERIAL_MEMBER(mailData); - RS_REGISTER_SERIAL_MEMBER_TYPED(clientService, uint16_t); - RS_REGISTER_SERIAL_MEMBER(presignedReceipt); + RS_SERIAL_PROCESS(status); + RS_SERIAL_PROCESS(recipient); + RS_SERIAL_PROCESS(author); + RS_SERIAL_PROCESS(group_id); + RS_SERIAL_PROCESS(sent_ts); + RS_SERIAL_PROCESS(mailItem); + RS_SERIAL_PROCESS(mailData); + RS_SERIAL_PROCESS(clientService); + RS_SERIAL_PROCESS(presignedReceipt); } diff --git a/libretroshare/src/gxstrans/p3gxstransitems.h b/libretroshare/src/gxstrans/p3gxstransitems.h index f158d0bdd..bd13c9e71 100644 --- a/libretroshare/src/gxstrans/p3gxstransitems.h +++ b/libretroshare/src/gxstrans/p3gxstransitems.h @@ -55,7 +55,7 @@ public: void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) - { RS_REGISTER_SERIAL_MEMBER_TYPED(mailId, uint64_t); } + { RS_SERIAL_PROCESS(mailId); } }; class RsGxsTransPresignedReceipt : public RsGxsTransBaseMsgItem @@ -140,9 +140,9 @@ public: RsGenericSerializer::SerializeContext& ctx ) { RsGxsTransBaseMsgItem::serial_process(j, ctx); - RS_REGISTER_SERIAL_MEMBER_TYPED(cryptoType, uint8_t); - RS_REGISTER_SERIAL_MEMBER(recipientHint); - RS_REGISTER_SERIAL_MEMBER(payload); + RS_SERIAL_PROCESS(cryptoType); + RS_SERIAL_PROCESS(recipientHint); + RS_SERIAL_PROCESS(payload); } void clear() diff --git a/libretroshare/src/gxstunnel/rsgxstunnelitems.cc b/libretroshare/src/gxstunnel/rsgxstunnelitems.cc index 8e488c190..d653b03ea 100644 --- a/libretroshare/src/gxstunnel/rsgxstunnelitems.cc +++ b/libretroshare/src/gxstunnel/rsgxstunnelitems.cc @@ -102,6 +102,9 @@ template<> void RsTypeSerializer::print_data(const std::string& name,BIGNUM std::cerr << "[BIGNUM] : " << name << std::endl; } +RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(BIGNUM*) +RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(BIGNUM*) + void RsGxsTunnelStatusItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { RsTypeSerializer::serial_process(j,ctx,status,"status") ; diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 7e1c3e069..e34410ed2 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -2,34 +2,26 @@ TEMPLATE = lib CONFIG += staticlib -CONFIG += create_prl CONFIG -= qt TARGET = retroshare TARGET_PRL = libretroshare DESTDIR = lib -#CONFIG += dsdv - -retrotor { - DEFINES *= RETROTOR - CONFIG -= bitdht -} else { - CONFIG += bitdht -} +!include("use_libretroshare.pri"):error("Including") # the dht stunner is used to obtain RS external ip addr. when it is natted # this system is unreliable and rs supports a newer and better one (asking connected peers) # CONFIG += useDhtStunner -profiling { - QMAKE_CXXFLAGS -= -fomit-frame-pointer - QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer -} - # treat warnings as error for better removing #QMAKE_CFLAGS += -Werror #QMAKE_CXXFLAGS += -Werror +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 + debug { # DEFINES *= DEBUG # DEFINES *= OPENDHT_DEBUG DHT_DEBUG CONN_DEBUG DEBUG_UDP_SORTER P3DISC_DEBUG DEBUG_UDP_LAYER FT_DEBUG EXTADDRSEARCH_DEBUG @@ -203,21 +195,11 @@ linux-* { PKGCONFIG *= libssl libupnp PKGCONFIG *= libcrypto zlib - LIBS *= -lpthread -ldl -} + no_sqlcipher:PKGCONFIG *= sqlite3 + LIBS *= -ldl -linux-* { DEFINES *= PLUGIN_DIR=\"\\\"$${PLUGIN_DIR}\\\"\" DEFINES *= DATA_DIR=\"\\\"$${DATA_DIR}\\\"\" - - ## where to put the librarys interface - #include_rsiface.path = "$${INC_DIR}" - #include_rsiface.files = $$PUBLIC_HEADERS - #INSTALLS += include_rsiface - - ## where to put the shared library itself - #target.path = "$$LIB_DIR" - #INSTALLS *= target } linux-g++ { @@ -234,7 +216,7 @@ version_detail_bash_script { PRE_TARGETDEPS = write_version_detail write_version_detail.commands = $$PWD/version_detail.sh } - win32 { + win32-* { QMAKE_EXTRA_TARGETS += write_version_detail PRE_TARGETDEPS = write_version_detail write_version_detail.commands = $$PWD/version_detail.bat @@ -263,13 +245,11 @@ win32-x-g++ { } ################################# Windows ########################################## -win32 { +win32-g++ { QMAKE_CC = $${QMAKE_CXX} OBJECTS_DIR = temp/obj MOC_DIR = temp/moc - DEFINES *= WINDOWS_SYS WIN32 STATICLIB MINGW WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T - # This defines the platform to be WinXP or later and is needed for getaddrinfo (_WIN32_WINNT_WINXP) - DEFINES *= WINVER=0x0501 + DEFINES *= STATICLIB # Switch on extra warnings QMAKE_CFLAGS += -Wextra @@ -287,17 +267,8 @@ win32 { DEFINES += USE_CMD_ARGS - CONFIG += upnp_miniupnpc - - no_sqlcipher { - PKGCONFIG *= sqlite3 - LIBS += -lsqlite3 - } else { - LIBS += -lsqlcipher - } - - DEPENDPATH += . $$INC_DIR - INCLUDEPATH += . $$INC_DIR + wLibs = ws2_32 gdi32 uuid iphlpapi crypt32 ole32 winmm + LIBS += $$linkDynamicLibs(wLibs) } ################################# MacOSX ########################################## @@ -306,22 +277,10 @@ mac { QMAKE_CC = $${QMAKE_CXX} OBJECTS_DIR = temp/obj MOC_DIR = temp/moc - #DEFINES = WINDOWS_SYS WIN32 STATICLIB MINGW - #DEFINES *= MINIUPNPC_VERSION=13 - - CONFIG += upnp_miniupnpc - CONFIG += c++11 - - # zeroconf disabled at the end of libretroshare.pro (but need the code) - #CONFIG += zeroconf - #CONFIG += zcnatassist # Beautiful Hack to fix 64bit file access. QMAKE_CXXFLAGS *= -Dfseeko64=fseeko -Dftello64=ftello -Dfopen64=fopen -Dvstatfs64=vstatfs - #GPG_ERROR_DIR = ../../../../libgpg-error-1.7 - #GPGME_DIR = ../../../../gpgme-1.1.8 - for(lib, LIB_DIR):LIBS += -L"$$lib" for(bin, BIN_DIR):LIBS += -L"$$bin" @@ -561,7 +520,7 @@ SOURCES += ft/ftchunkmap.cc \ ft/ftfilesearch.cc \ ft/ftserver.cc \ ft/fttransfermodule.cc \ - ft/ftturtlefiletransferitem.cc + ft/ftturtlefiletransferitem.cc SOURCES += crypto/chacha20.cpp \ crypto/hashstream.cc @@ -655,6 +614,7 @@ SOURCES += serialiser/rsbaseserial.cc \ rsitems/rsgxsupdateitems.cc \ rsitems/rsserviceinfoitems.cc \ + SOURCES += services/autoproxy/rsautoproxymonitor.cc \ services/autoproxy/p3i2pbob.cc \ services/p3msgservice.cc \ @@ -697,13 +657,16 @@ SOURCES += util/folderiterator.cc \ util/rsrecogn.cc \ util/rstime.cc - -upnp_miniupnpc { - HEADERS += upnp/upnputil.h upnp/upnphandler_miniupnp.h - SOURCES += upnp/upnputil.c upnp/upnphandler_miniupnp.cc +## Added for retrocompatibility remove ASAP +isEmpty(RS_UPNP_LIB) { + upnp_miniupnpc:RS_UPNP_LIB=miniupnpc + upnp_libupnp:RS_UPNP_LIB="upnp ixml threadutil" } -upnp_libupnp { +equals(RS_UPNP_LIB, miniupnpc) { + HEADERS += upnp/upnputil.h upnp/upnphandler_miniupnp.h + SOURCES += upnp/upnputil.c upnp/upnphandler_miniupnp.cc +} else { HEADERS += upnp/UPnPBase.h upnp/upnphandler_linux.h SOURCES += upnp/UPnPBase.cpp upnp/upnphandler_linux.cc DEFINES *= RS_USE_LIBUPNP @@ -795,7 +758,8 @@ SOURCES += gxstunnel/p3gxstunnel.cc \ gxstunnel/rsgxstunnelitems.cc # new serialization code -HEADERS += serialiser/rsserializer.h \ +HEADERS += serialiser/rsserializable.h \ + serialiser/rsserializer.h \ serialiser/rstypeserializer.h SOURCES += serialiser/rsserializer.cc \ @@ -936,19 +900,13 @@ android-* { DEFINES *= "fopen64=fopen" DEFINES *= "fseeko64=fseeko" DEFINES *= "ftello64=ftello" - LIBS *= -lbz2 -lupnp -lixml -lthreadutil -lsqlite3 -## Static library are verysensible to order in command line, has to be in the -## end of file for this reason +## Static library are very susceptible to order in command line + sLibs = bz2 $$RS_UPNP_LIB $$RS_SQL_LIB ssl crypto - LIBS += -L$$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/ -lsqlcipher - PRE_TARGETDEPS += $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/libsqlcipher.a - - LIBS += -L$$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/ -lssl - PRE_TARGETDEPS += $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/libssl.a - - LIBS += -L$$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/ -lcrypto - PRE_TARGETDEPS += $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/libcrypto.a + LIBS += $$linkStaticLibs(sLibs) + PRE_TARGETDEPS += $$pretargetStaticLibs(sLibs) HEADERS += util/androiddebug.h } + diff --git a/libretroshare/src/pgp/rscertificate.cc b/libretroshare/src/pgp/rscertificate.cc index 3297024ad..70a00328f 100644 --- a/libretroshare/src/pgp/rscertificate.cc +++ b/libretroshare/src/pgp/rscertificate.cc @@ -174,24 +174,24 @@ RsCertificate::RsCertificate(const RsPeerDetails& Detail, const unsigned char *b try { - scan_ip(Detail.localAddr,Detail.localPort,ipv4_internal_ip_and_port) ; + scan_ip(Detail.localAddr,Detail.localPort,ipv4_internal_ip_and_port); } catch(...) { - std::cerr << "RsCertificate::Invalid LocalAddress"; - std::cerr << std::endl; - memset(ipv4_internal_ip_and_port,0,6) ; + std::cerr << "RsCertificate::Invalid LocalAddress: " + << Detail.localAddr << std::endl; + memset(ipv4_internal_ip_and_port,0,6); } try { - scan_ip(Detail.extAddr,Detail.extPort,ipv4_external_ip_and_port) ; + scan_ip(Detail.extAddr,Detail.extPort,ipv4_external_ip_and_port); } catch(...) { - std::cerr << "RsCertificate::Invalid ExternalAddress"; - std::cerr << std::endl; + std::cerr << "RsCertificate::Invalid ExternalAddress: " + << Detail.extAddr << std::endl; memset(ipv4_external_ip_and_port,0,6) ; } diff --git a/libretroshare/src/pqi/p3historymgr.cc b/libretroshare/src/pqi/p3historymgr.cc index 39c2960a3..e1acb0888 100644 --- a/libretroshare/src/pqi/p3historymgr.cc +++ b/libretroshare/src/pqi/p3historymgr.cc @@ -67,7 +67,7 @@ p3HistoryMgr::~p3HistoryMgr() /***** p3HistoryMgr *****/ -//void p3HistoryMgr::addMessage(bool incoming, const RsPeerId &chatPeerId, const RsPeerId &peerId, const RsChatMsgItem *chatItem) +//void p3HistoryMgr::addMessage(bool incoming, const RsPeerId &chatPeerId, const RsPeerId &msgPeerId, const RsChatMsgItem *chatItem) void p3HistoryMgr::addMessage(const ChatMessage& cm) { uint32_t addMsgId = 0; @@ -84,48 +84,48 @@ void p3HistoryMgr::addMessage(const ChatMessage& cm) RsStackMutex stack(mHistoryMtx); /********** STACK LOCKED MTX ******/ - RsPeerId peerId; // id of sending peer - RsPeerId chatPeerId; // id of chat endpoint - std::string peerName; //name of sending peer + RsPeerId msgPeerId; // id of sending peer + RsPeerId chatPeerId; // id of chat endpoint + std::string peerName; //name of sending peer - bool enabled = false; - if (cm.chat_id.isBroadcast() && mPublicEnable == true) { - peerName = rsPeers->getPeerName(cm.broadcast_peer_id); - enabled = true; + bool enabled = false; + if (cm.chat_id.isBroadcast() && mPublicEnable == true) { + peerName = rsPeers->getPeerName(cm.broadcast_peer_id); + enabled = true; + } + if (cm.chat_id.isPeerId() && mPrivateEnable == true) { + msgPeerId = cm.incoming ? cm.chat_id.toPeerId() : rsPeers->getOwnId(); + peerName = rsPeers->getPeerName(msgPeerId); + enabled = true; + } + if (cm.chat_id.isLobbyId() && mLobbyEnable == true) { + peerName = cm.lobby_peer_gxs_id.toStdString(); + enabled = true; } - if (cm.chat_id.isPeerId() && mPrivateEnable == true) { - peerId = cm.incoming ? cm.chat_id.toPeerId() : rsPeers->getOwnId(); - peerName = rsPeers->getPeerName(peerId); - enabled = true; - } - if (cm.chat_id.isLobbyId() && mLobbyEnable == true) { - peerName = cm.lobby_peer_gxs_id.toStdString(); - enabled = true; - } - if(cm.chat_id.isDistantChatId()) - { - DistantChatPeerInfo dcpinfo; - if (rsMsgs->getDistantChatStatus(cm.chat_id.toDistantChatId(), dcpinfo)) - peerName = cm.chat_id.toPeerId().toStdString(); - enabled = true; - } + if(cm.chat_id.isDistantChatId()) + { + DistantChatPeerInfo dcpinfo; + if (rsMsgs->getDistantChatStatus(cm.chat_id.toDistantChatId(), dcpinfo)) + peerName = cm.chat_id.toPeerId().toStdString(); + enabled = true; + } - if(enabled == false) - return; + if(enabled == false) + return; - if(!chatIdToVirtualPeerId(cm.chat_id, chatPeerId)) - return; + if(!chatIdToVirtualPeerId(cm.chat_id, chatPeerId)) + return; RsHistoryMsgItem* item = new RsHistoryMsgItem; item->chatPeerId = chatPeerId; - item->incoming = cm.incoming; - item->peerId = peerId; - item->peerName = peerName; - item->sendTime = cm.sendTime; - item->recvTime = cm.recvTime; + item->incoming = cm.incoming; + item->msgPeerId = msgPeerId; + item->peerName = peerName; + item->sendTime = cm.sendTime; + item->recvTime = cm.recvTime; - item->message = cm.msg ; + item->message = cm.msg ; //librs::util::ConvertUtf16ToUtf8(chatItem->message, item->message); std::map >::iterator mit = mMessages.find(item->chatPeerId); @@ -138,7 +138,7 @@ void p3HistoryMgr::addMessage(const ChatMessage& cm) uint32_t limit; if (chatPeerId.isNull()) limit = mPublicSaveCount; - else if (cm.chat_id.isLobbyId()) + else if (cm.chat_id.isLobbyId()) limit = mLobbySaveCount; else limit = mPrivateSaveCount; @@ -424,7 +424,7 @@ static void convertMsg(const RsHistoryMsgItem* item, HistoryMsg &msg) msg.msgId = item->msgId; msg.chatPeerId = item->chatPeerId; msg.incoming = item->incoming; - msg.peerId = item->peerId; + msg.peerId = item->msgPeerId; msg.peerName = item->peerName; msg.sendTime = item->sendTime; msg.recvTime = item->recvTime; diff --git a/libretroshare/src/pqi/p3linkmgr.cc b/libretroshare/src/pqi/p3linkmgr.cc index c84b8ea4f..302efd255 100644 --- a/libretroshare/src/pqi/p3linkmgr.cc +++ b/libretroshare/src/pqi/p3linkmgr.cc @@ -3,7 +3,8 @@ * * 3P/PQI network interface for RetroShare. * - * Copyright 2007-2011 by Robert Fernie. + * Copyright (C) 2007-2011 Robert Fernie. + * Copyright (C) 2015-2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -56,6 +57,7 @@ static struct RsLog::logInfo p3connectzoneInfo = {RsLog::Default, "p3connect"}; /**** * #define LINKMGR_DEBUG 1 + * #define LINKMGR_DEBUG_LOG 1 * #define LINKMGR_DEBUG_CONNFAIL 1 * #define LINKMGR_DEBUG_ACTIONS 1 * #define LINKMGR_DEBUG_LINKTYPE 1 @@ -620,7 +622,9 @@ bool p3LinkMgrIMPL::connectAttempt(const RsPeerId &id, struct sockaddr_storage & } +#ifdef LINKMGR_DEBUG_LOG rslog(RSL_WARNING, p3connectzone, "p3LinkMgrIMPL::connectAttempt() called id: " + id.toStdString()); +#endif it->second.lastattempt = time(NULL); it->second.inConnAttempt = true; @@ -823,7 +827,9 @@ bool p3LinkMgrIMPL::connectResult(const RsPeerId &id, bool success, bool isIncom out += " FAILED ATTEMPT (Not Connected)"; } } +#ifdef LINKMGR_DEBUG_LOG rslog(RSL_WARNING, p3connectzone, out); +#endif } @@ -1344,8 +1350,10 @@ void p3LinkMgrIMPL::peerStatus(const RsPeerId& id, const pqiIpAddrSet &addrs, } /* This has become very unwieldy - as extra arguments are required for UDP connections */ -void p3LinkMgrIMPL::peerConnectRequest(const RsPeerId& id, const struct sockaddr_storage &raddr, const struct sockaddr_storage &proxyaddr, const struct sockaddr_storage &srcaddr, - uint32_t source, uint32_t flags, uint32_t delay, uint32_t bandwidth) +void p3LinkMgrIMPL::peerConnectRequest( + const RsPeerId& id, const sockaddr_storage &raddr, + const sockaddr_storage &proxyaddr, const sockaddr_storage &srcaddr, + uint32_t source, uint32_t flags, uint32_t delay, uint32_t bandwidth ) { #ifdef LINKMGR_DEBUG std::cerr << "p3LinkMgrIMPL::peerConnectRequest() id: " << id; @@ -2057,8 +2065,9 @@ bool p3LinkMgrIMPL::locked_ConnectAttempt_Complete(peerConnectState *peer) int p3LinkMgrIMPL::addFriend(const RsPeerId &id, bool isVisible) { +#ifdef LINKMGR_DEBUG_LOG rslog(RSL_WARNING, p3connectzone, "p3LinkMgr::addFriend() id: " + id.toStdString()); - +#endif { RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/ diff --git a/libretroshare/src/pqi/p3netmgr.cc b/libretroshare/src/pqi/p3netmgr.cc index 7dad79bb2..be119fd45 100644 --- a/libretroshare/src/pqi/p3netmgr.cc +++ b/libretroshare/src/pqi/p3netmgr.cc @@ -692,11 +692,11 @@ void p3NetMgrIMPL::netExtCheck() bool netSetupDone = false; { - RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ + RS_STACK_MUTEX(mNetMtx); bool isStable = false; - struct sockaddr_storage tmpip ; + sockaddr_storage tmpip; - std::map address_votes ; + std::map address_votes; /* check for External Address */ /* in order of importance */ @@ -705,38 +705,30 @@ void p3NetMgrIMPL::netExtCheck() #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) std::cerr << "p3NetMgrIMPL::netExtCheck() Ext Not Ok" << std::endl; #endif - /* net Assist */ - if (netAssistExtAddress(tmpip)) + if ( netAssistExtAddress(tmpip) && + sockaddr_storage_isValidNet(tmpip) && + sockaddr_storage_ipv6_to_ipv4(tmpip) ) { -#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) - std::cerr << "p3NetMgrIMPL::netExtCheck() Ext supplied from netAssistExternalAddress()" << std::endl; -#endif - if(sockaddr_storage_isValidNet(tmpip)) + if( !rsBanList || + rsBanList->isAddressAccepted( + tmpip, RSBANLIST_CHECKING_FLAGS_BLACKLIST ) ) { - if( (rsBanList==NULL) || rsBanList->isAddressAccepted(tmpip,RSBANLIST_CHECKING_FLAGS_BLACKLIST)) - { - // must be stable??? - isStable = true; - //mNetFlags.mExtAddr = tmpip; - mNetFlags.mExtAddrOk = true; - mNetFlags.mExtAddrStableOk = isStable; - - address_votes[tmpip].n++ ; - - std::cerr << "NetAssistAddress reported external address " << sockaddr_storage_iptostring(tmpip) << std::endl; - } - else - std::cerr << "(SS) netAssisExternalAddress returned banned own IP " << sockaddr_storage_iptostring(tmpip) << " (banned). Rejecting." << std::endl; + // must be stable??? + isStable = true; + mNetFlags.mExtAddrOk = true; + mNetFlags.mExtAddrStableOk = isStable; + address_votes[tmpip].n++ ; + std::cerr << __PRETTY_FUNCTION__ << " NetAssistAddress " + << " reported external address " + << sockaddr_storage_iptostring(tmpip) + << std::endl; } -#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) else - { - std::cerr << "p3NetMgrIMPL::netExtCheck() Bad Address supplied from netAssistExternalAddress()" << std::endl; - } -#endif + std::cerr << "(SS) netAssisExternalAddress returned banned " + << "own IP " << sockaddr_storage_iptostring(tmpip) + << " (banned). Rejecting." << std::endl; } - } #ifdef ALLOW_DHT_STUNNER @@ -780,7 +772,7 @@ void p3NetMgrIMPL::netExtCheck() } #endif - /* otherwise ask ExtAddrFinder */ + /* ask ExtAddrFinder */ { /* ExtAddrFinder */ if (mUseExtAddrFinder) @@ -789,7 +781,7 @@ void p3NetMgrIMPL::netExtCheck() std::cerr << "p3NetMgrIMPL::netExtCheck() checking ExtAddrFinder" << std::endl; #endif bool extFinderOk = mExtAddrFinder->hasValidIP(tmpip); - if (extFinderOk) + if (extFinderOk && sockaddr_storage_ipv6_to_ipv4(tmpip)) { #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) std::cerr << "p3NetMgrIMPL::netExtCheck() Ext supplied by ExtAddrFinder" << std::endl; @@ -803,38 +795,41 @@ void p3NetMgrIMPL::netExtCheck() std::cerr << std::endl; #endif - //mNetFlags.mExtAddr = tmpip; mNetFlags.mExtAddrOk = true; address_votes[tmpip].n++ ; - /* XXX HACK TO FIX */ -#warning drbob: ALLOWING ExtAddrFinder -> ExtAddrStableOk = true (which it is not normally) + /* XXX HACK TO FIX drbob: ALLOWING + * ExtAddrFinder -> ExtAddrStableOk = true + * (which it is not normally) */ mNetFlags.mExtAddrStableOk = true; - - std::cerr << "ExtAddrFinder reported external address " << sockaddr_storage_iptostring(tmpip) << std::endl; + std::cerr << __PRETTY_FUNCTION__ << " ExtAddrFinder " + << " reported external address " + << sockaddr_storage_iptostring(tmpip) + << std::endl; } } } - - /* also ask peer mgr. */ - + + /* also ask peer mgr. */ if (mPeerMgr) { #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) std::cerr << "p3NetMgrIMPL::netExtCheck() checking mPeerMgr" << std::endl; #endif - uint8_t isstable ; // unused - sockaddr_storage tmpaddr ; + uint8_t isstable; // unused + sockaddr_storage tmpaddr; - if (mPeerMgr->getExtAddressReportedByFriends(tmpaddr, isstable)) + if ( mPeerMgr->getExtAddressReportedByFriends(tmpaddr, isstable) && + sockaddr_storage_ipv6_to_ipv4(tmpaddr) ) { #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) std::cerr << "p3NetMgrIMPL::netExtCheck() Ext supplied by ExtAddrFinder" << std::endl; #endif /* best guess at port */ - sockaddr_storage_setport(tmpaddr, sockaddr_storage_port(mLocalAddr)); + sockaddr_storage_setport( tmpaddr, + sockaddr_storage_port(mLocalAddr) ); #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) std::cerr << "p3NetMgrIMPL::netExtCheck() "; @@ -842,20 +837,21 @@ void p3NetMgrIMPL::netExtCheck() std::cerr << std::endl; #endif - //mNetFlags.mExtAddr = tmpaddr; mNetFlags.mExtAddrOk = true; mNetFlags.mExtAddrStableOk = isstable; - address_votes[tmpaddr].n++ ; - - std::cerr << "PeerMgr reported external address " << sockaddr_storage_iptostring(tmpaddr) << std::endl; + address_votes[tmpaddr].n++; + + std::cerr << __PRETTY_FUNCTION__ << " PeerMgr reported external" + << " address " + << sockaddr_storage_iptostring(tmpaddr) << std::endl; } #if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) - else - std::cerr << " No reliable address returned." << std::endl; + else + std::cerr << " No reliable address returned." << std::endl; #endif } - + /* any other sources ??? */ /* finalise address */ @@ -950,14 +946,6 @@ void p3NetMgrIMPL::netExtCheck() #endif netAssistSetAddress(mNetFlags.mLocalAddr, mNetFlags.mExtAddr, mNetMode); } -#if 0 - else - { - std::cerr << "p3NetMgrIMPL::netExtCheck() setting ERR netAssistSetAddress(0)" << std::endl; - /* mode = 0 for error */ - netAssistSetAddress(mNetFlags.mLocalAddr, mNetFlags.mExtAddr, mNetMode); - } -#endif /* flag unreachables! */ if ((mNetFlags.mExtAddrOk) && (!mNetFlags.mExtAddrStableOk)) @@ -966,9 +954,6 @@ void p3NetMgrIMPL::netExtCheck() std::cerr << "p3NetMgrIMPL::netExtCheck() Ext Unstable - Unreachable Check" << std::endl; #endif } - - - } if (netSetupDone) @@ -988,9 +973,9 @@ void p3NetMgrIMPL::netExtCheck() RsPeerId fakeId; netAssistKnownPeer(fakeId, mExtAddr, NETASSIST_KNOWN_PEER_SELF | NETASSIST_KNOWN_PEER_ONLINE); - rslog(RSL_WARNING, p3netmgrzone, "p3NetMgr::netExtCheck() Network Setup Complete"); + std::cerr << __PRETTY_FUNCTION__ << " Network Setup Complete" + << std::endl; } - } /********************************************************************************************** @@ -1002,8 +987,8 @@ bool p3NetMgrIMPL::checkNetAddress() bool addrChanged = false; bool validAddr = false; - struct sockaddr_storage prefAddr; - struct sockaddr_storage oldAddr; + sockaddr_storage prefAddr; + sockaddr_storage oldAddr; if (mNetMode & RS_NET_MODE_TRY_LOOPBACK) { @@ -1011,7 +996,7 @@ bool p3NetMgrIMPL::checkNetAddress() std::cerr << "p3NetMgrIMPL::checkNetAddress() LOOPBACK ... forcing to 127.0.0.1"; std::cerr << std::endl; #endif - sockaddr_storage_ipv4_aton(prefAddr, "127.0.0.1"); + sockaddr_storage_ipv4_aton(prefAddr, "127.0.0.1"); validAddr = true; } else @@ -1021,15 +1006,19 @@ bool p3NetMgrIMPL::checkNetAddress() * possible. It will require complete reenginering of the network layer * code. */ + /* For retro-compatibility strictly accept only IPv4 addresses here, + * IPv6 addresses are handled in a retro-compatible manner in + * p3PeerMgrIMPL::UpdateOwnAddress */ std::vector addrs; if (getLocalAddresses(addrs)) { - for (auto it = addrs.begin(); it!=addrs.end(); ++it) + for (auto it = addrs.begin(); it != addrs.end(); ++it) { sockaddr_storage& addr(*it); if( sockaddr_storage_isValidNet(addr) && !sockaddr_storage_isLoopbackNet(addr) && - !sockaddr_storage_isLinkLocalNet(addr)) + !sockaddr_storage_isLinkLocalNet(addr) && + sockaddr_storage_ipv6_to_ipv4(addr) ) { prefAddr = addr; validAddr = true; @@ -1043,7 +1032,8 @@ bool p3NetMgrIMPL::checkNetAddress() { sockaddr_storage& addr(*it); if( sockaddr_storage_isValidNet(addr) && - !sockaddr_storage_isLoopbackNet(addr) ) + !sockaddr_storage_isLoopbackNet(addr) && + sockaddr_storage_ipv6_to_ipv4(addr) ) { prefAddr = addr; validAddr = true; @@ -1069,9 +1059,9 @@ bool p3NetMgrIMPL::checkNetAddress() /* check addresses */ { - RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ - - oldAddr = mLocalAddr; + RS_STACK_MUTEX(mNetMtx); + + sockaddr_storage_copy(mLocalAddr, oldAddr); addrChanged = !sockaddr_storage_sameip(prefAddr, mLocalAddr); #ifdef NETMGR_DEBUG_TICK @@ -1097,7 +1087,7 @@ bool p3NetMgrIMPL::checkNetAddress() // update address. sockaddr_storage_copyip(mLocalAddr, prefAddr); - mNetFlags.mLocalAddr = mLocalAddr; + sockaddr_storage_copy(mLocalAddr, mNetFlags.mLocalAddr); if(sockaddr_storage_isLoopbackNet(mLocalAddr)) { @@ -1144,9 +1134,13 @@ bool p3NetMgrIMPL::checkNetAddress() * are the same (modify server)... this mismatch can * occur when the local port is changed.... */ - if (sockaddr_storage_sameip(mLocalAddr, mExtAddr)) + if (sockaddr_storage_sameip(mLocalAddr, mExtAddr) && sockaddr_storage_port(mLocalAddr) != sockaddr_storage_port(mExtAddr)) { +#ifdef NETMGR_DEBUG_RESET + std::cerr << "p3NetMgrIMPL::checkNetAddress() local and external ports are not the same. Setting external port to " << sockaddr_storage_port(mLocalAddr) << std::endl; +#endif sockaddr_storage_setport(mExtAddr, sockaddr_storage_port(mLocalAddr)); + addrChanged = true; } // ensure that address family is set, otherwise windows Barfs. @@ -1171,8 +1165,10 @@ bool p3NetMgrIMPL::checkNetAddress() { mPeerMgr->UpdateOwnAddress(mLocalAddr, mExtAddr); } - - rslog(RSL_WARNING, p3netmgrzone, "p3NetMgr::checkNetAddress() local address changed, resetting network"); + + std::cerr << __PRETTY_FUNCTION__ + << " local address changed, resetting network" << std::endl; + netReset(); } diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index 9862838aa..d1e9e4e97 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -4,6 +4,7 @@ * 3P/PQI network interface for RetroShare. * * Copyright 2007-2011 by Robert Fernie. + * Copyright (C) 2015-2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -23,6 +24,9 @@ * */ +#include // for std::vector +#include // for std::random_shuffle + #include "rsserver/p3face.h" #include "util/rsnet.h" #include "pqi/authgpg.h" @@ -32,6 +36,7 @@ #include "pqi/p3linkmgr.h" #include "pqi/p3netmgr.h" #include "pqi/p3historymgr.h" +#include "pqi/pqinetwork.h" // for getLocalAddresses //#include "pqi/p3dhtmgr.h" // Only need it for constants. //#include "tcponudp/tou.h" @@ -69,6 +74,7 @@ static struct RsLog::logInfo p3peermgrzoneInfo = {RsLog::Default, "p3peermgr"}; /**** * #define PEER_DEBUG 1 + * #define PEER_DEBUG_LOG 1 ***/ #define MAX_AVAIL_PERIOD 230 //times a peer stay in available state when not connected @@ -326,8 +332,8 @@ const RsPeerId& p3PeerMgrIMPL::getOwnId() bool p3PeerMgrIMPL::getOwnNetStatus(peerState &state) { - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ - state = mOwnState; + RS_STACK_MUTEX(mPeerMtx); + state = mOwnState; return true; } @@ -812,15 +818,12 @@ int p3PeerMgrIMPL::getFriendCount(bool ssl, bool online) bool p3PeerMgrIMPL::getFriendNetStatus(const RsPeerId &id, peerState &state) { - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + RS_STACK_MUTEX(mPeerMtx); /* check for existing */ std::map::iterator it; it = mFriendList.find(id); - if (it == mFriendList.end()) - { - return false; - } + if (it == mFriendList.end()) return false; state = it->second; return true; @@ -829,27 +832,24 @@ bool p3PeerMgrIMPL::getFriendNetStatus(const RsPeerId &id, peerState &state) bool p3PeerMgrIMPL::getOthersNetStatus(const RsPeerId &id, peerState &state) { - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + RS_STACK_MUTEX(mPeerMtx); /* check for existing */ std::map::iterator it; it = mOthersList.find(id); - if (it == mOthersList.end()) - { - return false; - } + if (it == mOthersList.end()) return false; state = it->second; return true; } -int p3PeerMgrIMPL::getConnectAddresses(const RsPeerId &id, - struct sockaddr_storage &lAddr, struct sockaddr_storage &eAddr, - pqiIpAddrSet &histAddrs, std::string &dyndns) +int p3PeerMgrIMPL::getConnectAddresses( + const RsPeerId &id, sockaddr_storage &lAddr, sockaddr_storage &eAddr, + pqiIpAddrSet &histAddrs, std::string &dyndns ) { - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ - + RS_STACK_MUTEX(mPeerMtx); + /* check for existing */ std::map::iterator it; it = mFriendList.find(id); @@ -860,7 +860,7 @@ int p3PeerMgrIMPL::getConnectAddresses(const RsPeerId &id, std::cerr << std::endl; return 0; } - + lAddr = it->second.localaddr; eAddr = it->second.serveraddr; histAddrs = it->second.ipAddrs; @@ -910,8 +910,9 @@ bool p3PeerMgrIMPL::addFriend(const RsPeerId& input_id, const RsPgpId& input_gpg RsPeerId id = input_id ; RsPgpId gpg_id = input_gpg_id ; +#ifdef PEER_DEBUG_LOG rslog(RSL_WARNING, p3peermgrzone, "p3PeerMgr::addFriend() id: " + id.toStdString()); - +#endif { RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ @@ -1227,45 +1228,101 @@ void p3PeerMgrIMPL::printPeerLists(std::ostream &out) * as it doesn't call back to there. */ -bool p3PeerMgrIMPL::UpdateOwnAddress(const struct sockaddr_storage &localAddr, const struct sockaddr_storage &extAddr) +bool p3PeerMgrIMPL::UpdateOwnAddress( const sockaddr_storage& pLocalAddr, + const sockaddr_storage& pExtAddr ) { -#ifdef PEER_DEBUG - std::cerr << "p3PeerMgrIMPL::UpdateOwnAddress("; - std::cerr << sockaddr_storage_tostring(localAddr); - std::cerr << ", "; - std::cerr << sockaddr_storage_tostring(extAddr); - std::cerr << ")" << std::endl; -#endif + sockaddr_storage localAddr; + sockaddr_storage_copy(pLocalAddr, localAddr); + sockaddr_storage_ipv6_to_ipv4(localAddr); - if((rsBanList != NULL) && !rsBanList->isAddressAccepted(localAddr, RSBANLIST_CHECKING_FLAGS_BLACKLIST)) - { - std::cerr << "(SS) Trying to set own IP to a banned IP " << sockaddr_storage_iptostring(localAddr) << ". This probably means that a friend in under traffic re-routing attack." << std::endl; - return false ; - } + sockaddr_storage extAddr; + sockaddr_storage_copy(pExtAddr, extAddr); + sockaddr_storage_ipv6_to_ipv4(extAddr); - { - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ +//#ifdef PEER_DEBUG + std::cerr << "p3PeerMgrIMPL::UpdateOwnAddress(" + << sockaddr_storage_tostring(localAddr) << ", " + << sockaddr_storage_tostring(extAddr) << ")" << std::endl; +//#endif - //update ip address list - pqiIpAddress ipAddressTimed; - ipAddressTimed.mAddr = localAddr; - ipAddressTimed.mSeenTime = time(NULL); - ipAddressTimed.mSrc = 0 ; - mOwnState.ipAddrs.updateLocalAddrs(ipAddressTimed); + if( rsBanList && + !rsBanList->isAddressAccepted(localAddr, + RSBANLIST_CHECKING_FLAGS_BLACKLIST) ) + { + std::cerr << "(SS) Trying to set own IP to a banned IP " + << sockaddr_storage_iptostring(localAddr) << ". This probably" + << "means that a friend in under traffic re-routing attack." + << std::endl; + return false; + } - mOwnState.localaddr = localAddr; - } + { + RS_STACK_MUTEX(mPeerMtx); + + //update ip address list + pqiIpAddress ipAddressTimed; + sockaddr_storage_copy(localAddr, ipAddressTimed.mAddr); + ipAddressTimed.mSeenTime = time(NULL); + ipAddressTimed.mSrc = 0; + mOwnState.ipAddrs.updateLocalAddrs(ipAddressTimed); + + if(!mOwnState.hiddenNode) + { + /* Workaround to spread multiple local ip addresses when presents. + * This is needed because RS wrongly assumes that there is just one + * active local ip address at time. */ + std::vector addrs; + if(getLocalAddresses(addrs)) + { + /* To work around MAX_ADDRESS_LIST_SIZE addresses limitation, + * let's shuffle the list of local addresses in the hope that + * with enough time every local address is advertised to + * trusted nodes so they may try to connect to all of them + * including the most convenient if a local connection exists. + */ + std::random_shuffle(addrs.begin(), addrs.end()); + + for (auto it = addrs.begin(); it!=addrs.end(); ++it) + { + sockaddr_storage& addr(*it); + if( sockaddr_storage_isValidNet(addr) && + !sockaddr_storage_isLoopbackNet(addr) && + /* Avoid IPv6 link local addresses as we don't have + * implemented the logic needed to handle sin6_scope_id. + * To properly handle sin6_scope_id it would probably + * require deep reenginering of the RetroShare + * networking stack */ + !sockaddr_storage_ipv6_isLinkLocalNet(addr) ) + { + sockaddr_storage_ipv6_to_ipv4(addr); + pqiIpAddress pqiIp; + sockaddr_storage_clear(pqiIp.mAddr); + pqiIp.mAddr.ss_family = addr.ss_family; + sockaddr_storage_copyip(pqiIp.mAddr, addr); + sockaddr_storage_setport( + pqiIp.mAddr, + sockaddr_storage_port(localAddr) ); + pqiIp.mSeenTime = time(nullptr); + pqiIp.mSrc = 0; + mOwnState.ipAddrs.updateLocalAddrs(pqiIp); + } + } + } + } + + sockaddr_storage_copy(localAddr, mOwnState.localaddr); + } - { - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + { + RS_STACK_MUTEX(mPeerMtx); - //update ip address list - pqiIpAddress ipAddressTimed; - ipAddressTimed.mAddr = extAddr; - ipAddressTimed.mSeenTime = time(NULL); - ipAddressTimed.mSrc = 0 ; - mOwnState.ipAddrs.updateExtAddrs(ipAddressTimed); + //update ip address list + pqiIpAddress ipAddressTimed; + sockaddr_storage_copy(extAddr, ipAddressTimed.mAddr); + ipAddressTimed.mSeenTime = time(NULL); + ipAddressTimed.mSrc = 0; + mOwnState.ipAddrs.updateExtAddrs(ipAddressTimed); /* Attempted Fix to MANUAL FORWARD Mode.... * don't update the server address - if we are in this mode @@ -1285,8 +1342,8 @@ bool p3PeerMgrIMPL::UpdateOwnAddress(const struct sockaddr_storage &localAddr, std::cerr << std::endl; } else if (mOwnState.netMode & RS_NET_MODE_EXT) - { - sockaddr_storage_copyip(mOwnState.serveraddr,extAddr); + { + sockaddr_storage_copyip(mOwnState.serveraddr, extAddr); std::cerr << "p3PeerMgrIMPL::UpdateOwnAddress() Disabling Update of Server Port "; std::cerr << " as MANUAL FORWARD Mode"; @@ -1296,8 +1353,8 @@ bool p3PeerMgrIMPL::UpdateOwnAddress(const struct sockaddr_storage &localAddr, std::cerr << std::endl; } else - { - mOwnState.serveraddr = extAddr; + { + sockaddr_storage_copy(extAddr, mOwnState.serveraddr); } } @@ -1371,21 +1428,25 @@ bool p3PeerMgrIMPL::setLocalAddress(const RsPeerId &id, const struct sockaddr return changed; } -bool p3PeerMgrIMPL::setExtAddress(const RsPeerId &id, const struct sockaddr_storage &addr) +bool p3PeerMgrIMPL::setExtAddress( const RsPeerId &id, + const sockaddr_storage &addr ) { - bool changed = false; - uint32_t check_res = 0 ; + bool changed = false; + uint32_t check_res = 0; - if(rsBanList!=NULL && !rsBanList->isAddressAccepted(addr,RSBANLIST_CHECKING_FLAGS_BLACKLIST,&check_res)) - { - std::cerr << "(SS) trying to set external contact address for peer " << id << " to a banned address " << sockaddr_storage_iptostring(addr )<< std::endl; - return false ; - } + if( rsBanList!=NULL && !rsBanList->isAddressAccepted( + addr, RSBANLIST_CHECKING_FLAGS_BLACKLIST, &check_res) ) + { + std::cerr << "(SS) trying to set external contact address for peer " + << id << " to a banned address " + << sockaddr_storage_iptostring(addr) << std::endl; + return false; + } if (id == AuthSSL::getAuthSSL()->OwnId()) { { - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + RS_STACK_MUTEX(mPeerMtx); if (!sockaddr_storage_same(mOwnState.serveraddr, addr)) { mOwnState.serveraddr = addr; @@ -1398,7 +1459,7 @@ bool p3PeerMgrIMPL::setExtAddress(const RsPeerId &id, const struct sockaddr_s return changed; } - RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/ + RS_STACK_MUTEX(mPeerMtx); /* check if it is a friend */ std::map::iterator it; if (mFriendList.end() == (it = mFriendList.find(id))) @@ -1406,7 +1467,9 @@ bool p3PeerMgrIMPL::setExtAddress(const RsPeerId &id, const struct sockaddr_s if (mOthersList.end() == (it = mOthersList.find(id))) { #ifdef PEER_DEBUG - std::cerr << "p3PeerMgrIMPL::setLocalAddress() cannot add addres info : peer id not found in friend list id: " << id << std::endl; + std::cerr << "p3PeerMgrIMPL::setLocalAddress() cannot add addres " + << "info : peer id not found in friend list id: " << id + << std::endl; #endif return false; } @@ -1957,7 +2020,7 @@ bool p3PeerMgrIMPL::saveList(bool &cleanup, std::list& saveData) RsPeerNetItem *item = new RsPeerNetItem(); item->clear(); - item->peerId = getOwnId(); + item->nodePeerId = getOwnId(); item->pgpId = mOwnState.gpg_id; item->location = mOwnState.location; @@ -2008,7 +2071,7 @@ bool p3PeerMgrIMPL::saveList(bool &cleanup, std::list& saveData) item = new RsPeerNetItem(); item->clear(); - item->peerId = it->first; + item->nodePeerId = it->first; item->pgpId = (it->second).gpg_id; item->location = (it->second).location; item->netMode = (it->second).netMode; @@ -2208,7 +2271,7 @@ bool p3PeerMgrIMPL::loadList(std::list& load) RsPeerNetItem *pitem = dynamic_cast(*it); if (pitem) { - RsPeerId peer_id = pitem->peerId ; + RsPeerId peer_id = pitem->nodePeerId ; RsPgpId peer_pgp_id = pitem->pgpId ; if (peer_id == ownId) @@ -2235,7 +2298,7 @@ bool p3PeerMgrIMPL::loadList(std::list& load) /* ************* */ // permission flags is used as a mask for the existing perms, so we set it to 0xffff addFriend(peer_id, peer_pgp_id, pitem->netMode, pitem->vs_disc, pitem->vs_dht, pitem->lastContact, RS_NODE_PERM_ALL); - setLocation(pitem->peerId, pitem->location); + setLocation(pitem->nodePeerId, pitem->location); } if (pitem->netMode == RS_NET_MODE_HIDDEN) diff --git a/libretroshare/src/pqi/p3peermgr.h b/libretroshare/src/pqi/p3peermgr.h index 8fa73cf78..8a0ac5235 100644 --- a/libretroshare/src/pqi/p3peermgr.h +++ b/libretroshare/src/pqi/p3peermgr.h @@ -119,17 +119,20 @@ class p3NetMgrIMPL; class p3PeerMgr { - public: +public: - p3PeerMgr() { return; } -virtual ~p3PeerMgr() { return; } + p3PeerMgr() {} + virtual ~p3PeerMgr() {} -virtual bool addFriend(const RsPeerId &ssl_id, const RsPgpId &gpg_id, uint32_t netMode = RS_NET_MODE_UDP, - uint16_t vsDisc = RS_VS_DISC_FULL, uint16_t vsDht = RS_VS_DHT_FULL, - time_t lastContact = 0,ServicePermissionFlags = ServicePermissionFlags(RS_NODE_PERM_DEFAULT)) = 0; -virtual bool removeFriend(const RsPeerId &ssl_id, bool removePgpId) = 0; + virtual bool addFriend( const RsPeerId &ssl_id, const RsPgpId &gpg_id, + uint32_t netMode = RS_NET_MODE_UDP, + uint16_t vsDisc = RS_VS_DISC_FULL, + uint16_t vsDht = RS_VS_DHT_FULL, + time_t lastContact = 0, + ServicePermissionFlags = ServicePermissionFlags(RS_NODE_PERM_DEFAULT) ) = 0; -virtual bool isFriend(const RsPeerId& ssl_id) = 0; + virtual bool removeFriend(const RsPeerId &ssl_id, bool removePgpId) = 0; + virtual bool isFriend(const RsPeerId& ssl_id) = 0; virtual bool getAssociatedPeers(const RsPgpId &gpg_id, std::list &ids) = 0; virtual bool removeAllFriendLocations(const RsPgpId &gpgid) = 0; @@ -146,7 +149,7 @@ virtual bool getGroupInfoByName(const std::string& groupName, RsGroupInfo &gr virtual bool getGroupInfoList(std::list &groupInfoList) = 0; virtual bool assignPeersToGroup(const RsNodeGroupId &groupId, const std::list &peerIds, bool assign) = 0; - virtual bool resetOwnExternalAddressList() = 0 ; + virtual bool resetOwnExternalAddressList() = 0 ; virtual ServicePermissionFlags servicePermissionFlags(const RsPgpId& gpg_id) =0; virtual ServicePermissionFlags servicePermissionFlags(const RsPeerId& ssl_id) =0; @@ -354,9 +357,9 @@ public: bool setOwnNetworkMode(uint32_t netMode); bool setOwnVisState(uint16_t vs_disc, uint16_t vs_dht); - int getConnectAddresses(const RsPeerId &id, - struct sockaddr_storage &lAddr, struct sockaddr_storage &eAddr, - pqiIpAddrSet &histAddrs, std::string &dyndns); + int getConnectAddresses( const RsPeerId &id, sockaddr_storage &lAddr, + sockaddr_storage &eAddr, pqiIpAddrSet &histAddrs, + std::string &dyndns ); protected: diff --git a/libretroshare/src/pqi/pqihandler.h b/libretroshare/src/pqi/pqihandler.h index cf1f19429..c285c4ef7 100644 --- a/libretroshare/src/pqi/pqihandler.h +++ b/libretroshare/src/pqi/pqihandler.h @@ -38,7 +38,7 @@ class PQInterface; class RSTrafficClue; class RsBwRates; -class RsItem; +struct RsItem; class RsRawItem; class SearchModule diff --git a/libretroshare/src/pqi/pqiloopback.cc b/libretroshare/src/pqi/pqiloopback.cc index ba4078ce2..8a8ba342b 100644 --- a/libretroshare/src/pqi/pqiloopback.cc +++ b/libretroshare/src/pqi/pqiloopback.cc @@ -27,7 +27,7 @@ #include // for NULL -class RsItem; +struct RsItem; /*** #define LOOPBACK_DEBUG 1 diff --git a/libretroshare/src/pqi/pqiloopback.h b/libretroshare/src/pqi/pqiloopback.h index 7186dd440..82c3923d8 100644 --- a/libretroshare/src/pqi/pqiloopback.h +++ b/libretroshare/src/pqi/pqiloopback.h @@ -31,7 +31,7 @@ #include "pqi/pqi_base.h" // for NetInterface (ptr only), PQInterface #include "retroshare/rstypes.h" // for RsPeerId -class RsItem; +struct RsItem; class pqiloopback: public PQInterface { diff --git a/libretroshare/src/pqi/pqinetwork.cc b/libretroshare/src/pqi/pqinetwork.cc index c302123a4..8f843cfeb 100644 --- a/libretroshare/src/pqi/pqinetwork.cc +++ b/libretroshare/src/pqi/pqinetwork.cc @@ -41,6 +41,7 @@ #include "util/rsdebug.h" #include "util/rsstring.h" #include "util/rsnet.h" +#include "util/stacktrace.h" static struct RsLog::logInfo pqinetzoneInfo = {RsLog::Default, "pqinet"}; #define pqinetzone &pqinetzoneInfo @@ -335,15 +336,17 @@ bool getLocalAddresses(std::vector& addrs) struct ifaddrs *ifsaddrs, *ifa; if(getifaddrs(&ifsaddrs) != 0) { - std::cerr << "FATAL ERROR: getLocalAddresses failed!" << std::endl; - return false ; + std::cerr << __PRETTY_FUNCTION__ << " FATAL ERROR: " << errno << " " + << strerror(errno) << std::endl; + print_stacktrace(); + return false; } for ( ifa = ifsaddrs; ifa; ifa = ifa->ifa_next ) if ( ifa->ifa_addr && (ifa->ifa_flags & IFF_UP) ) { sockaddr_storage tmp; sockaddr_storage_clear(tmp); - if (sockaddr_storage_copyip(tmp, * reinterpret_cast(ifa->ifa_addr))) + if (sockaddr_storage_copyip(tmp, *reinterpret_cast(ifa->ifa_addr))) addrs.push_back(tmp); } freeifaddrs(ifsaddrs); @@ -438,43 +441,17 @@ int unix_fcntl_nonblock(int fd) } -int unix_connect(int fd, const struct sockaddr *serv_addr, socklen_t socklen) +int unix_connect(int fd, const sockaddr_storage &serv_addr) { #ifdef NET_DEBUG - std::cerr << "unix_connect()"; - std::cerr << std::endl; + std::cerr << __PRETTY_FUNCTION__ << std::endl; #endif - const struct sockaddr_storage *ss_addr = (struct sockaddr_storage *) serv_addr; - socklen_t len = socklen; - - switch (ss_addr->ss_family) - { - case AF_INET: - len = sizeof(struct sockaddr_in); - break; - case AF_INET6: - len = sizeof(struct sockaddr_in6); - break; - } - - if (len > socklen) - { - std::cerr << "unix_connect() ERROR len > socklen"; - std::cerr << std::endl; - - len = socklen; - //return EINVAL; - } - - int ret = connect(fd, serv_addr, len); + int ret = connect( fd, (const struct sockaddr *) &serv_addr, + sizeof(struct sockaddr_in6) ); /******************* WINDOWS SPECIFIC PART ******************/ #ifdef WINDOWS_SYS // WINDOWS - -#ifdef NET_DEBUG - std::cerr << "unix_connect()" << std::endl; -#endif if (ret != 0) { errno = WinToUnixError(WSAGetLastError()); diff --git a/libretroshare/src/pqi/pqinetwork.h b/libretroshare/src/pqi/pqinetwork.h index 21d8ee81c..e974f68cf 100644 --- a/libretroshare/src/pqi/pqinetwork.h +++ b/libretroshare/src/pqi/pqinetwork.h @@ -103,7 +103,7 @@ bool getLocalAddresses(std::vector & addrs); int unix_close(int sockfd); int unix_socket(int domain, int type, int protocol); int unix_fcntl_nonblock(int sockfd); -int unix_connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen); +int unix_connect(int sockfd, const sockaddr_storage& serv_addr); int unix_getsockopt_error(int sockfd, int *err); #ifdef WINDOWS_SYS // WINDOWS diff --git a/libretroshare/src/pqi/pqipersongrp.cc b/libretroshare/src/pqi/pqipersongrp.cc index a8afa2b84..3df48fdb3 100644 --- a/libretroshare/src/pqi/pqipersongrp.cc +++ b/libretroshare/src/pqi/pqipersongrp.cc @@ -45,6 +45,7 @@ static std::list waitingIds; /**** *#define PGRP_DEBUG 1 + *#define PGRP_DEBUG_LOG 1 ****/ #define DEFAULT_DOWNLOAD_KB_RATE (200.0) @@ -420,7 +421,9 @@ int pqipersongrp::addPeer(const RsPeerId& id) sm -> pqi = pqip; // reset it to start it working. +#ifdef PGRP_DEBUG_LOG pqioutput(PQL_WARNING, pqipersongrpzone, "pqipersongrp::addPeer() => reset() called to initialise new person"); +#endif pqip -> reset(); pqip -> listen(); diff --git a/libretroshare/src/pqi/pqissl.cc b/libretroshare/src/pqi/pqissl.cc index 898080262..47da19b93 100644 --- a/libretroshare/src/pqi/pqissl.cc +++ b/libretroshare/src/pqi/pqissl.cc @@ -4,6 +4,7 @@ * 3P/PQI network interface for RetroShare. * * Copyright 2004-2006 by Robert Fernie. + * Copyright (C) 2015-2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -59,14 +60,15 @@ static struct RsLog::logInfo pqisslzoneInfo = {RsLog::Default, "pqisslzone"}; #define PQISSL_PASSIVE 0x00 #define PQISSL_ACTIVE 0x01 -#define PQISSL_DEBUG 1 -#define PQISSL_LOG_DEBUG 1 - const int PQISSL_LOCAL_FLAG = 0x01; const int PQISSL_REMOTE_FLAG = 0x02; const int PQISSL_UDP_FLAG = 0x02; ***********/ +//#define PQISSL_DEBUG 1 +//#define PQISSL_LOG_DEBUG 1 +//#define PQISSL_LOG_DEBUG2 1 + static const int PQISSL_MAX_READ_ZERO_COUNT = 20; static const time_t PQISSL_MAX_READ_ZERO_TIME = 15; // 15 seconds of no data => reset. (atm HeartBeat pkt sent 5 secs) @@ -96,44 +98,14 @@ static const int PQISSL_SSL_CONNECT_TIMEOUT = 30; * */ -pqissl::pqissl(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm) - :NetBinInterface(parent, parent->PeerId()), - mLinkMgr(lm), pqil(l), - mSslMtx("pqissl"), - active(false), certvalid(false), waiting(WAITING_NOT), - sslmode(PQISSL_ACTIVE), ssl_connection(NULL), sockfd(-1), - readpkt(NULL), pktlen(0), total_len(0), - attempt_ts(0), - n_read_zero(0), mReadZeroTS(0), ssl_connect_timeout(0), - mConnectDelay(0), mConnectTS(0), - mConnectTimeout(0), mTimeoutTS(0) -{ - RsStackMutex stack(mSslMtx); /**** LOCKED MUTEX ****/ - - /* set address to zero */ - sockaddr_storage_clear(remote_addr); - -#ifdef PQISSL_LOG_DEBUG - rslog(RSL_DEBUG_BASIC, pqisslzone, "pqissl for PeerId: " + PeerId()); -#endif - -#if 0 - if (!(AuthSSL::getAuthSSL()->isAuthenticated(PeerId()))) - { - rslog(RSL_ALERT, pqisslzone, - "pqissl::Warning Certificate Not Approved!"); - - rslog(RSL_ALERT, pqisslzone, - "\t pqissl will not initialise...."); - - } -#else - rslog(RSL_DEBUG_BASIC, pqisslzone, - "pqissl::Warning SSL Certificate Approval Not CHECKED??"); -#endif - - return; -} +pqissl::pqissl(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm) : + NetBinInterface(parent, parent->PeerId()), + mLinkMgr(lm), pqil(l), mSslMtx("pqissl"), active(false), certvalid(false), + waiting(WAITING_NOT), sslmode(PQISSL_ACTIVE), ssl_connection(NULL), + sockfd(-1), readpkt(NULL), pktlen(0), total_len(0), attempt_ts(0), + n_read_zero(0), mReadZeroTS(0), ssl_connect_timeout(0), mConnectDelay(0), + mConnectTS(0), mConnectTimeout(0), mTimeoutTS(0) +{ sockaddr_storage_clear(remote_addr); } pqissl::~pqissl() { @@ -151,11 +123,9 @@ pqissl::pqissl(pqissllistener *l, PQInterface *parent, p3LinkMgr *lm) int pqissl::connect(const struct sockaddr_storage &raddr) { - RsStackMutex stack(mSslMtx); /**** LOCKED MUTEX ****/ + RS_STACK_MUTEX(mSslMtx); - // reset failures remote_addr = raddr; - return ConnectAttempt(); } @@ -203,12 +173,11 @@ int pqissl::close() // put back on the listening queue. int pqissl::reset() { - RsStackMutex stack(mSslMtx); /**** LOCKED MUTEX ****/ - + RS_STACK_MUTEX(mSslMtx); return reset_locked(); } -int pqissl::reset_locked() +int pqissl::reset_locked() { std::string outLog; bool neededReset = false; @@ -269,7 +238,9 @@ int pqissl::reset_locked() #endif } +#ifdef PQISSL_LOG_DEBUG2 rslog(RSL_ALERT, pqisslzone, outLog); +#endif // notify people of problem! // but only if we really shut something down. @@ -387,7 +358,7 @@ int pqissl::status() out += " active: \n"; // print out connection. - out += "Connected TO : " + PeerId() + "\n"; + out += "Connected TO : " + PeerId().toStdString() + "\n"; // print out cipher. rs_sprintf_append(out, "\t\tSSL Cipher:%s", SSL_get_cipher(ssl_connection)); rs_sprintf_append(out, " (%d:%d)", SSL_get_cipher_bits(ssl_connection, &alg), alg); @@ -583,7 +554,7 @@ int pqissl::Delay_Connection() #ifdef PQISSL_LOG_DEBUG { std::string out; - rs_sprintf(out, "pqissl::Delay_Connection() Delaying Connection to %s for %lu seconds", PeerId().c_str(), mConnectDelay); + rs_sprintf(out, "pqissl::Delay_Connection() Delaying Connection to %s for %lu seconds", PeerId().toStdString(), mConnectDelay); rslog(RSL_DEBUG_BASIC, pqisslzone, out); } #endif @@ -596,7 +567,7 @@ int pqissl::Delay_Connection() #ifdef PQISSL_LOG_DEBUG { std::string out; - rs_sprintf(out, "pqissl::Delay_Connection() Connection to %s starting in %ld seconds", PeerId().c_str(), mConnectTS - time(NULL)); + rs_sprintf(out, "pqissl::Delay_Connection() Connection to %s starting in %ld seconds", PeerId().toStdString(), mConnectTS - time(NULL)); rslog(RSL_DEBUG_BASIC, pqisslzone, out); } #endif @@ -608,35 +579,30 @@ int pqissl::Delay_Connection() } rslog(RSL_WARNING, pqisslzone, - "pqissl::Initiate_Connection() Already Attempt in Progress!"); + "pqissl::Delay_Connection() Already Attempt in Progress!"); return -1; } -int pqissl::Initiate_Connection() +int pqissl::Initiate_Connection() { - int err; - struct sockaddr_storage addr = remote_addr; - -#ifdef PQISSL_LOG_DEBUG - rslog(RSL_DEBUG_BASIC, pqisslzone, - "pqissl::Initiate_Connection() Attempting Outgoing Connection...."); +#ifdef PQISSL_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " " + << sockaddr_storage_tostring(remote_addr) << std::endl; #endif - if (waiting != WAITING_DELAY) + int err; + sockaddr_storage addr; sockaddr_storage_copy(remote_addr, addr); + + if(waiting != WAITING_DELAY) { - rslog(RSL_WARNING, pqisslzone, - "pqissl::Initiate_Connection() Already Attempt in Progress!"); + std::cerr << __PRETTY_FUNCTION__ << " Already Attempt in Progress!" + << std::endl; return -1; } -#ifdef PQISSL_LOG_DEBUG - rslog(RSL_DEBUG_BASIC, pqisslzone, - "pqissl::Initiate_Connection() Opening Socket"); -#endif - // open socket connection to addr. - int osock = unix_socket(PF_INET, SOCK_STREAM, 0); + int osock = unix_socket(PF_INET6, SOCK_STREAM, 0); #ifdef PQISSL_LOG_DEBUG { @@ -662,7 +628,7 @@ int pqissl::Initiate_Connection() "pqissl::Initiate_Connection() Making Non-Blocking"); #endif - err = unix_fcntl_nonblock(osock); + err = unix_fcntl_nonblock(osock); if (err < 0) { std::string out; @@ -674,13 +640,6 @@ int pqissl::Initiate_Connection() return -1; } - { - std::string out; - rs_sprintf(out, "pqissl::Initiate_Connection() Connecting To: %s via: ", PeerId().toStdString().c_str()); - out += sockaddr_storage_tostring(addr); - rslog(RSL_WARNING, pqisslzone, out); - } - if (sockaddr_storage_isnull(addr)) { rslog(RSL_WARNING, pqisslzone, "pqissl::Initiate_Connection() Invalid (0.0.0.0) Remote Address, Aborting Connect."); @@ -736,66 +695,55 @@ int pqissl::Initiate_Connection() #endif #endif // WINDOWS_SYS + /* Systems that supports dual stack sockets defines IPV6_V6ONLY and some set + * it to 1 by default. This enable dual stack socket on such systems. + * Systems which don't support dual stack (only Windows older then XP SP3) + * will support IPv6 only and not IPv4 */ +#ifdef IPV6_V6ONLY + int no = 0; + err = rs_setsockopt( osock, IPPROTO_IPV6, IPV6_V6ONLY, + reinterpret_cast(&no), sizeof(no) ); +#ifdef PQISSL_DEBUG + if (err) std::cerr << __PRETTY_FUNCTION__ + << " Error setting IPv6 socket dual stack: " + << errno << " " << strerror(errno) << std::endl; + else std::cerr << __PRETTY_FUNCTION__ + << " Setting IPv6 socket dual stack" << std::endl; +#endif // PQISSL_DEBUG +#endif // IPV6_V6ONLY + mTimeoutTS = time(NULL) + mConnectTimeout; //std::cerr << "Setting Connect Timeout " << mConnectTimeout << " Seconds into Future " << std::endl; - if (0 != (err = unix_connect(osock, (struct sockaddr *) &addr, sizeof(addr)))) + sockaddr_storage_ipv4_to_ipv6(addr); +#ifdef PQISSL_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " Connecting To: " + << PeerId().toStdString() <<" via: " + << sockaddr_storage_tostring(addr) << std::endl; +#endif + + if (0 != (err = unix_connect(osock, addr))) { - std::string out; - rs_sprintf(out, "pqissl::Initiate_Connection() connect returns:%d -> errno: %d error: %s\n", err, errno, socket_errorType(errno).c_str()); - - if (errno == EINPROGRESS) + switch (errno) { - // set state to waiting..... + case EINPROGRESS: waiting = WAITING_SOCK_CONNECT; sockfd = osock; - -#ifdef PQISSL_LOG_DEBUG - out += " EINPROGRESS Waiting for Socket Connection"; - rslog(RSL_DEBUG_BASIC, pqisslzone, out); -#endif - return 0; - } - else if ((errno == ENETUNREACH) || (errno == ETIMEDOUT)) - { - out += "ENETUNREACHABLE: cert: " + PeerId().toStdString(); - rslog(RSL_WARNING, pqisslzone, out); + default: +#ifdef PQISSL_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " Failure connect " + << sockaddr_storage_tostring(addr) + << " returns: " + << err << " -> errno: " << errno << " " + << socket_errorType(errno) << std::endl; +#endif - // Then send unreachable message. net_internal_close(osock); - osock=-1; - //reset(); - + osock = -1; waiting = WAITING_FAIL_INTERFACE; - return -1; } - - /* IF we get here ---- we Failed for some other reason. - * Should abandon this interface - * Known reasons to get here: EINVAL (bad address) - */ - - rs_sprintf_append(out, "Error: Connection Failed: %d - %s", errno, socket_errorType(errno).c_str()); - - net_internal_close(osock); - osock=-1; - waiting = WAITING_FAIL_INTERFACE; - - rslog(RSL_WARNING, pqisslzone, out); - - // extra output for the moment. - std::cerr << out; - - return -1; - } - else - { -#ifdef PQISSL_LOG_DEBUG - rslog(RSL_DEBUG_BASIC, pqisslzone, - "pqissl::Init_Connection() connect returned 0"); -#endif } waiting = WAITING_SOCK_CONNECT; @@ -832,10 +780,14 @@ bool pqissl::CheckConnectionTimeout() std::string out; rs_sprintf(out, "pqissl::Basic_Connection_Complete() Connection Timed Out. Peer: %s Period: %lu", PeerId().toStdString().c_str(), mConnectTimeout); +#ifdef PQISSL_LOG_DEBUG2 rslog(RSL_WARNING, pqisslzone, out); +#endif /* as sockfd is valid, this should close it all up */ +#ifdef PQISSL_LOG_DEBUG2 rslog(RSL_ALERT, pqisslzone, "pqissl::Basic_Connection_Complete() -> calling reset()"); +#endif reset_locked(); return true; } @@ -974,7 +926,9 @@ int pqissl::Basic_Connection_Complete() { std::string out; rs_sprintf(out, "pqissl::Basic_Connection_Complete() TCP Connection Complete: cert: %s on osock: ", PeerId().toStdString().c_str(), sockfd); +#ifdef PQISSL_LOG_DEBUG2 rslog(RSL_WARNING, pqisslzone, out); +#endif } return 1; } @@ -1105,7 +1059,7 @@ int pqissl::Initiate_SSL_Connection() return 1; } -int pqissl::SSL_Connection_Complete() +int pqissl::SSL_Connection_Complete() { #ifdef PQISSL_LOG_DEBUG rslog(RSL_DEBUG_BASIC, pqisslzone, @@ -1247,27 +1201,21 @@ int pqissl::Extract_Failed_SSL_Certificate() -int pqissl::Authorise_SSL_Connection() +int pqissl::Authorise_SSL_Connection() { -#ifdef PQISSL_LOG_DEBUG - rslog(RSL_DEBUG_BASIC, pqisslzone, - "pqissl::Authorise_SSL_Connection()"); +#ifdef PQISSL_DEBUG + std::cerr << __PRETTY_FUNCTION__ << std::endl; #endif - if (time(NULL) > ssl_connect_timeout) - { - rslog(RSL_WARNING, pqisslzone, - "pqissl::Authorise_SSL_Connection() Connection Timed Out!"); - /* as sockfd is valid, this should close it all up */ - rslog(RSL_ALERT, pqisslzone, "pqissl::Authorise_Connection_Complete() -> calling reset()"); - reset_locked(); + if (time(NULL) > ssl_connect_timeout) + { + std::cerr << __PRETTY_FUNCTION__ << " Connection timed out reset!" + << std::endl; + reset_locked(); } int err; - if (0 >= (err = SSL_Connection_Complete())) - { - return err; - } + if (0 >= (err = SSL_Connection_Complete())) return err; #ifdef PQISSL_LOG_DEBUG rslog(RSL_DEBUG_BASIC, pqisslzone, @@ -1352,36 +1300,54 @@ int pqissl::Authorise_SSL_Connection() /* This function is public, and callable from pqilistener - so must be mutex protected */ -int pqissl::accept(SSL *ssl, int fd, const struct sockaddr_storage &foreign_addr) // initiate incoming connection. +int pqissl::accept( SSL *ssl, int fd, + const sockaddr_storage &foreign_addr) { #ifdef PQISSL_DEBUG - std::cerr << "pqissl::accept()"; - std::cerr << std::endl; + std::cerr << __PRETTY_FUNCTION__ << std::endl; #endif - RsStackMutex stack(mSslMtx); /**** LOCKED MUTEX ****/ - + RS_STACK_MUTEX(mSslMtx); return accept_locked(ssl, fd, foreign_addr); } -int pqissl::accept_locked(SSL *ssl, int fd, const struct sockaddr_storage &foreign_addr) // initiate incoming connection. +int pqissl::accept_locked( SSL *ssl, int fd, + const sockaddr_storage &foreign_addr ) { - uint32_t check_result; - uint32_t checking_flags = RSBANLIST_CHECKING_FLAGS_BLACKLIST; - if (rsPeers->servicePermissionFlags(PeerId()) & RS_NODE_PERM_REQUIRE_WL) - checking_flags |= RSBANLIST_CHECKING_FLAGS_WHITELIST; +#ifdef PQISSL_DEBUG + std::cerr << __PRETTY_FUNCTION__ << std::endl; +#endif - if(rsBanList!=NULL && !rsBanList->isAddressAccepted(foreign_addr,checking_flags,&check_result)) - { - std::cerr << "(SS) refusing incoming SSL connection from blacklisted foreign address " << sockaddr_storage_iptostring(foreign_addr) - << ". Reason: " << check_result << "." << std::endl; - RsServer::notify()->AddFeedItem(RS_FEED_ITEM_SEC_IP_BLACKLISTED, PeerId().toStdString(), sockaddr_storage_iptostring(foreign_addr), "", "", check_result); + uint32_t check_result; + uint32_t checking_flags = RSBANLIST_CHECKING_FLAGS_BLACKLIST; + + if (rsPeers->servicePermissionFlags(PeerId()) & RS_NODE_PERM_REQUIRE_WL) + checking_flags |= RSBANLIST_CHECKING_FLAGS_WHITELIST; + + if( rsBanList && !rsBanList->isAddressAccepted( foreign_addr, + checking_flags, + &check_result ) ) + { + std::cerr << __PRETTY_FUNCTION__ + << " (SS) refusing incoming SSL connection from blacklisted " + << "foreign address " + << sockaddr_storage_iptostring(foreign_addr) + << ". Reason: " << check_result << "." << std::endl; + + RsServer::notify()->AddFeedItem( + RS_FEED_ITEM_SEC_IP_BLACKLISTED, + PeerId().toStdString(), + sockaddr_storage_iptostring(foreign_addr), "", "", + check_result); reset_locked(); - return -1; - } + return -1; + } + if (waiting != WAITING_NOT) { - rslog(RSL_WARNING, pqisslzone, "pqissl::accept() Peer: " + PeerId().toStdString() + " - Two connections in progress - Shut 1 down!"); + std::cerr << __PRETTY_FUNCTION__ << " Peer: " << PeerId().toStdString() + << " - Two connections in progress - Shut 1 down!" + << std::endl; // outgoing connection in progress. // shut this baby down. @@ -1392,70 +1358,51 @@ int pqissl::accept_locked(SSL *ssl, int fd, const struct sockaddr_storage &forei switch(waiting) { - case WAITING_SOCK_CONNECT: - -#ifdef PQISSL_LOG_DEBUG - rslog(RSL_DEBUG_BASIC, pqisslzone, - "pqissl::accept() STATE = Waiting Sock Connect - close the socket"); +#ifdef PQISSL_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " STATE = Waiting Sock Connect " + << "- close the socket" << std::endl; #endif - break; - case WAITING_SSL_CONNECTION: - -#ifdef PQISSL_LOG_DEBUG - rslog(RSL_DEBUG_BASIC, pqisslzone, - "pqissl::accept() STATE = Waiting SSL Connection - close sockfd + ssl_conn"); +#ifdef PQISSL_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " STATE = Waiting SSL " + << "Connection - close sockfd + ssl_conn" << std::endl; #endif - break; - case WAITING_SSL_AUTHORISE: - -#ifdef PQISSL_LOG_DEBUG - rslog(RSL_DEBUG_BASIC, pqisslzone, - "pqissl::accept() STATE = Waiting SSL Authorise - close sockfd + ssl_conn"); +#ifdef PQISSL_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " STATE = Waiting SSL Authorise" + << " - close sockfd + ssl_conn" << std::endl; #endif - break; - case WAITING_FAIL_INTERFACE: - -#ifdef PQISSL_LOG_DEBUG - rslog(RSL_DEBUG_BASIC, pqisslzone, - "pqissl::accept() STATE = Failed, ignore?"); +#ifdef PQISSL_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " STATE = Failed, ignore?" + << std::endl; #endif - break; - - default: - rslog(RSL_ALERT, pqisslzone, - "pqissl::accept() STATE = Unknown - ignore?"); - - rslog(RSL_ALERT, pqisslzone, "pqissl::accept() -> calling reset()"); + std::cerr << __PRETTY_FUNCTION__ << " STATE = Unknown - resetting!" + << std::endl; reset_locked(); break; } - - //waiting = WAITING_FAIL_INTERFACE; - //return -1; } /* shutdown existing - in all cases use the new one */ if ((ssl_connection) && (ssl_connection != ssl)) { - rslog(RSL_ALERT, pqisslzone, - "pqissl::accept() closing Previous/Existing ssl_connection"); + std::cerr << __PRETTY_FUNCTION__ + << " closing Previous/Existing ssl_connection" << std::endl; SSL_shutdown(ssl_connection); SSL_free (ssl_connection); } if ((sockfd > -1) && (sockfd != fd)) { - rslog(RSL_ALERT, pqisslzone, - "pqissl::accept() closing Previous/Existing sockfd"); + std::cerr << __PRETTY_FUNCTION__ << " closing Previous/Existing sockfd" + << std::endl; net_internal_close(sockfd); } @@ -1468,54 +1415,39 @@ int pqissl::accept_locked(SSL *ssl, int fd, const struct sockaddr_storage &forei /* if we connected - then just writing the same over, * but if from ssllistener then we need to save the address. */ - remote_addr = foreign_addr; + sockaddr_storage_copy(foreign_addr, remote_addr); - /* check whether it is on the same LAN */ - - struct sockaddr_storage localaddr; - mLinkMgr->getLocalAddress(localaddr); + std::cerr << __PRETTY_FUNCTION__ << " SUCCESSFUL connection to: " + << PeerId().toStdString() << " remoteaddr: " + << sockaddr_storage_iptostring(remote_addr) << std::endl; +#ifdef PQISSL_DEBUG { - std::string out = "pqissl::accept() SUCCESSFUL connection to: " + PeerId().toStdString(); - out += " localaddr: " + sockaddr_storage_iptostring(localaddr); - out += " remoteaddr: " + sockaddr_storage_iptostring(remote_addr); - - rslog(RSL_WARNING, pqisslzone, out); - } - - // establish the ssl details. - // cipher name. - int err; - -#ifdef PQISSL_LOG_DEBUG - { - int alg; - std::string out; - rs_sprintf(out, "SSL Cipher:%s\n", SSL_get_cipher(ssl)); - rs_sprintf_append(out, "SSL Cipher Bits:%d - %d\n", SSL_get_cipher_bits(ssl, &alg), alg); - rs_sprintf_append(out, "SSL Cipher Version:%s\n", SSL_get_cipher_version(ssl)); - rslog(RSL_DEBUG_BASIC, pqisslzone, out); + int alg; + std::cerr << __PRETTY_FUNCTION__ << "SSL Cipher: " + << SSL_get_cipher(ssl) << std::endl << "SSL Cipher Bits: " + << SSL_get_cipher_bits(ssl, &alg) << " - " << alg + << std::endl; } #endif // make non-blocking / or check..... - if ((err = net_internal_fcntl_nonblock(sockfd)) < 0) + int err; + if ((err = net_internal_fcntl_nonblock(sockfd)) < 0) { - rslog(RSL_ALERT, pqisslzone, "Error: Cannot make socket NON-Blocking: "); + std::cerr << __PRETTY_FUNCTION__ << "Cannot make socket NON-Blocking " + << "reset!" << std::endl; active = false; - waiting = WAITING_FAIL_INTERFACE; - // failed completely. - rslog(RSL_ALERT, pqisslzone, "pqissl::accept() -> calling reset()"); + waiting = WAITING_FAIL_INTERFACE; // failed completely. + reset_locked(); return -1; } - else - { -#ifdef PQISSL_LOG_DEBUG - rslog(RSL_DEBUG_BASIC, pqisslzone, "pqissl::accept() Socket Made Non-Blocking!"); +#ifdef PQISSL_DEBUG + else std::cerr << __PRETTY_FUNCTION__ << " Socket made non-nlocking!" + << std::endl; #endif - } // we want to continue listening - incase this socket is crap, and they try again. //stoplistening(); @@ -1524,15 +1456,16 @@ int pqissl::accept_locked(SSL *ssl, int fd, const struct sockaddr_storage &forei waiting = WAITING_NOT; #ifdef PQISSL_DEBUG - std::cerr << "pqissl::accept_locked() connection complete - notifying parent"; - std::cerr << std::endl; + std::cerr << __PRETTY_FUNCTION__ << "connection complete - notifying parent" + << std::endl; #endif // Notify the pqiperson.... (Both Connect/Receive) if (parent()) { - struct sockaddr_storage addr = remote_addr; - parent() -> notifyEvent(this, NET_CONNECT_SUCCESS, addr); + // Is the copy necessary? + sockaddr_storage addr; sockaddr_storage_copy(remote_addr, addr); + parent()->notifyEvent(this, NET_CONNECT_SUCCESS, addr); } return 1; } @@ -1626,31 +1559,35 @@ int pqissl::senddata(void *data, int len) return tmppktlen; } -int pqissl::readdata(void *data, int len) +int pqissl::readdata(void *data, int len) { - RsStackMutex stack(mSslMtx); /**** LOCKED MUTEX ****/ + RS_STACK_MUTEX(mSslMtx); #ifdef PQISSL_DEBUG - std::cout << "Reading data thread=" << pthread_self() << ", ssl=" << (void*)this << std::endl ; + std::cout << "Reading data thread=" << pthread_self() << ", ssl=" + << (void*)this << std::endl; #endif + // Safety check. Apparently this avoids some SIGSEGV. - // - if (ssl_connection == NULL) - return -1 ; + if (ssl_connection == NULL) return -1; // There is a do, because packets can be splitted into multiple ssl buffers // when they are larger than 16384 bytes. Such packets have to be read in // multiple slices. do { - int tmppktlen ; + int tmppktlen; #ifdef PQISSL_DEBUG - std::cerr << "calling SSL_read. len=" << len << ", total_len=" << total_len << std::endl ; + std::cerr << "calling SSL_read. len=" << len << ", total_len=" + << total_len << std::endl; #endif - ERR_clear_error() ; - - tmppktlen = SSL_read(ssl_connection, (void*)( &(((uint8_t*)data)[total_len])), len-total_len) ; + ERR_clear_error(); + + tmppktlen = SSL_read(ssl_connection, + (void*)( &(((uint8_t*)data)[total_len])), + len-total_len); + #ifdef PQISSL_DEBUG std::cerr << "have read " << tmppktlen << " bytes" << std::endl ; std::cerr << "data[0] = " @@ -1665,7 +1602,6 @@ int pqissl::readdata(void *data, int len) #endif // Need to catch errors..... - // if (tmppktlen <= 0) // probably needs a reset. { std::string out; diff --git a/libretroshare/src/pqi/pqissl.h b/libretroshare/src/pqi/pqissl.h index b4cd267d6..e14e3928d 100644 --- a/libretroshare/src/pqi/pqissl.h +++ b/libretroshare/src/pqi/pqissl.h @@ -4,6 +4,7 @@ * 3P/PQI network interface for RetroShare. * * Copyright 2004-2006 by Robert Fernie. + * Copyright (C) 2015-2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -114,16 +115,13 @@ virtual bool cansend(uint32_t usec); virtual int close(); /* BinInterface version of reset() */ virtual RsFileHash gethash(); /* not used here */ -virtual bool bandwidthLimited() { return true ; } // replace by !sameLAN to avoid bandwidth limiting on LAN +virtual bool bandwidthLimited() { return true ; } public: -/* Completion of the SSL connection, - * this is public, so it can be called by - * the listener (should make friends??) - */ +/// initiate incoming connection. +int accept(SSL *ssl, int fd, const struct sockaddr_storage &foreign_addr); -int accept(SSL *ssl, int fd, const struct sockaddr_storage &foreign_addr); void getCryptoParams(RsPeerCryptoParams& params) ; bool actAsServer(); @@ -139,7 +137,10 @@ protected: RsMutex mSslMtx; /**** MUTEX protects data and fn below ****/ virtual int reset_locked(); -int accept_locked(SSL *ssl, int fd, const struct sockaddr_storage &foreign_addr); + + /// initiate incoming connection. + int accept_locked( SSL *ssl, int fd, + const sockaddr_storage& foreign_addr ); // A little bit of information to describe // the SSL state, this is needed diff --git a/libretroshare/src/pqi/pqissllistener.cc b/libretroshare/src/pqi/pqissllistener.cc index 975791eec..c81c96cd2 100644 --- a/libretroshare/src/pqi/pqissllistener.cc +++ b/libretroshare/src/pqi/pqissllistener.cc @@ -4,6 +4,7 @@ * 3P/PQI network interface for RetroShare. * * Copyright 2004-2006 by Robert Fernie. + * Copyright (C) 2015-2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -59,8 +60,10 @@ static struct RsLog::logInfo pqissllistenzoneInfo = {RsLog::Default, "p3peermgr" pqissllistenbase::pqissllistenbase(const sockaddr_storage &addr, p3PeerMgr *pm) - : laddr(addr), mPeerMgr(pm), active(false) + : mPeerMgr(pm), active(false) { + sockaddr_storage_copy(addr, laddr); + if (!(AuthSSL::getAuthSSL()-> active())) { pqioutput(PQL_ALERT, pqissllistenzone, @@ -103,17 +106,29 @@ int pqissllistenbase::status() return 1; } -int pqissllistenbase::setuplisten() +int pqissllistenbase::setuplisten() { - int err; - if (active) - return -1; + int err; + if (active) return -1; + + lsock = socket(PF_INET6, SOCK_STREAM, 0); + +#ifdef IPV6_V6ONLY + int no = 0; + err = rs_setsockopt(lsock, IPPROTO_IPV6, IPV6_V6ONLY, + reinterpret_cast(&no), sizeof(no)); + if (err) std::cerr << __PRETTY_FUNCTION__ + << ": Error setting IPv6 socket dual stack" << std::endl; +#ifdef DEBUG_LISTENNER + else std::cerr << __PRETTY_FUNCTION__ + << ": Success setting IPv6 socket dual stack" << std::endl; +#endif +#endif // IPV6_V6ONLY - lsock = socket(PF_INET, SOCK_STREAM, 0); /********************************** WINDOWS/UNIX SPECIFIC PART ******************/ #ifndef WINDOWS_SYS // ie UNIX - if (lsock < 0) - { + if (lsock < 0) + { pqioutput(PQL_ALERT, pqissllistenzone, "pqissllistenbase::setuplisten() Cannot Open Socket!"); @@ -197,22 +212,20 @@ int pqissllistenbase::setuplisten() } } -#ifdef OPEN_UNIVERSAL_PORT - struct sockaddr_storage tmpaddr = laddr; + struct sockaddr_storage tmpaddr; + sockaddr_storage_copy(laddr, tmpaddr); + sockaddr_storage_ipv4_to_ipv6(tmpaddr); if (!mPeerMgr->isHidden()) sockaddr_storage_zeroip(tmpaddr); - if (0 != (err = universal_bind(lsock, (struct sockaddr *) &tmpaddr, sizeof(tmpaddr)))) -#else - if (0 != (err = universal_bind(lsock, (struct sockaddr *) &laddr, sizeof(laddr)))) -#endif + + if (0 != (err = rs_bind(lsock, tmpaddr))) { std::string out = "pqissllistenbase::setuplisten() Cannot Bind to Local Address!\n"; showSocketError(out); pqioutput(PQL_ALERT, pqissllistenzone, out); - std::cerr << out << std::endl; - std::cerr << "laddr: " << sockaddr_storage_tostring(laddr) << std::endl; -#ifdef OPEN_UNIVERSAL_PORT - if (!mPeerMgr->isHidden()) std::cerr << "Zeroed tmpaddr: " << sockaddr_storage_tostring(tmpaddr) << std::endl; -#endif + std::cerr << out << std::endl + << "tmpaddr: " << sockaddr_storage_tostring(tmpaddr) + << std::endl; + print_stacktrace(); return -1; } diff --git a/libretroshare/src/pqi/pqisslproxy.cc b/libretroshare/src/pqi/pqisslproxy.cc index 90444bcf9..500c6a7d8 100644 --- a/libretroshare/src/pqi/pqisslproxy.cc +++ b/libretroshare/src/pqi/pqisslproxy.cc @@ -42,6 +42,7 @@ static struct RsLog::logInfo pqisslproxyzoneInfo = {RsLog::Default, "pqisslproxy #define pqisslproxyzone &pqisslproxyzoneInfo // #define PROXY_DEBUG 1 +// #define PROXY_DEBUG_LOG 1 #define PROXY_STATE_FAILED 0 #define PROXY_STATE_INIT 1 @@ -593,8 +594,9 @@ bool pqisslproxy::connect_parameter(uint32_t type, const std::string &value) { std::string out; rs_sprintf(out, "pqisslproxy::connect_parameter() Peer: %s DOMAIN_ADDRESS: %s", PeerId().toStdString().c_str(), value.c_str()); +#ifdef PROXY_DEBUG_LOG rslog(RSL_WARNING, pqisslproxyzone, out); - +#endif mDomainAddress = value; #ifdef PROXY_DEBUG std::cerr << out << std::endl; @@ -615,8 +617,9 @@ bool pqisslproxy::connect_parameter(uint32_t type, uint32_t value) { std::string out; rs_sprintf(out, "pqisslproxy::connect_parameter() Peer: %s REMOTE_PORT: %lu", PeerId().toStdString().c_str(), value); +#ifdef PROXY_DEBUG_LOG rslog(RSL_WARNING, pqisslproxyzone, out); - +#endif mRemotePort = value; #ifdef PROXY_DEBUG std::cerr << out << std::endl; diff --git a/libretroshare/src/pqi/pqissludp.cc b/libretroshare/src/pqi/pqissludp.cc index f14fb82e5..dbde145df 100644 --- a/libretroshare/src/pqi/pqissludp.cc +++ b/libretroshare/src/pqi/pqissludp.cc @@ -227,6 +227,15 @@ int pqissludp::Initiate_Connection() return -1; } + if(!sockaddr_storage_ipv6_to_ipv4(remote_addr)) + { + std::cerr << __PRETTY_FUNCTION__ << "Error: remote_addr is not " + << "valid IPv4!" << std::endl; + sockaddr_storage_dump(remote_addr); + print_stacktrace(); + return -EINVAL; + } + mTimeoutTS = time(NULL) + mConnectTimeout; //std::cerr << "Setting Connect Timeout " << mConnectTimeout << " Seconds into Future " << std::endl; //std::cerr << " Connect Period is:" << mConnectPeriod << std::endl; @@ -250,22 +259,28 @@ int pqissludp::Initiate_Connection() { - std::cerr << "CONVERTING ALL ADDRESSES TO IPV4: TODO make IPV6"; - std::cerr << std::endl; - struct sockaddr_in srcaddr; struct sockaddr_in proxyaddr; struct sockaddr_in remoteaddr; - - if ((mConnectSrcAddr.ss_family != AF_INET) || - (mConnectProxyAddr.ss_family != AF_INET) || - (remote_addr.ss_family != AF_INET)) + + if(!sockaddr_storage_ipv6_to_ipv4(mConnectSrcAddr)) { - std::cerr << "Error One Address is not IPv4. aborting"; - std::cerr << std::endl; - abort(); + std::cerr << __PRETTY_FUNCTION__ << "Error: mConnectSrcAddr is " + << "not valid IPv4!" << std::endl; + sockaddr_storage_dump(mConnectSrcAddr); + print_stacktrace(); + return -EINVAL; } - + if(!sockaddr_storage_ipv6_to_ipv4(mConnectProxyAddr)) + { + std::cerr << __PRETTY_FUNCTION__ << "Error: mConnectProxyAddr " + << "is not valid IPv4!" << std::endl; + sockaddr_storage_dump(mConnectProxyAddr); + print_stacktrace(); + return -EINVAL; + + } + struct sockaddr_in *rap = (struct sockaddr_in *) &remote_addr; struct sockaddr_in *pap = (struct sockaddr_in *) &mConnectProxyAddr; struct sockaddr_in *sap = (struct sockaddr_in *) &mConnectSrcAddr; @@ -285,7 +300,6 @@ int pqissludp::Initiate_Connection() err = tou_connect_via_relay(sockfd, &srcaddr, &proxyaddr, &remoteaddr); } - /*** It seems that the UDP Layer sees x 1.2 the traffic of the SSL layer. * We need to compensate somewhere... we drop the maximum traffic to 75% of limit diff --git a/libretroshare/src/pqi/pqistreamer.h b/libretroshare/src/pqi/pqistreamer.h index fe2754854..9908651b2 100644 --- a/libretroshare/src/pqi/pqistreamer.h +++ b/libretroshare/src/pqi/pqistreamer.h @@ -38,7 +38,7 @@ #include "retroshare/rstypes.h" // for RsPeerId #include "util/rsthreads.h" // for RsMutex -class RsItem; +struct RsItem; class RsSerialiser; struct PartialPacketRecord diff --git a/libretroshare/src/retroshare/rsexpr.h b/libretroshare/src/retroshare/rsexpr.h index 06aba7e20..72c63d463 100644 --- a/libretroshare/src/retroshare/rsexpr.h +++ b/libretroshare/src/retroshare/rsexpr.h @@ -182,7 +182,7 @@ private: class StringExpression: public Expression { public: - StringExpression(enum StringOperator op, std::list &t, bool ic): Op(op),terms(t), IgnoreCase(ic){} + StringExpression(enum StringOperator op, const std::list &t, bool ic): Op(op),terms(t), IgnoreCase(ic){} virtual void linearize(LinearizedExpression& e) const ; virtual std::string toStdString(const std::string& varstr) const; @@ -275,7 +275,7 @@ Some implementations of StringExpressions. class NameExpression: public StringExpression { public: - NameExpression(enum StringOperator op, std::list &t, bool ic): + NameExpression(enum StringOperator op, const std::list &t, bool ic): StringExpression(op,t,ic) {} bool eval(const ExpFileEntry& file); @@ -290,7 +290,7 @@ public: class PathExpression: public StringExpression { public: - PathExpression(enum StringOperator op, std::list &t, bool ic): + PathExpression(enum StringOperator op, const std::list &t, bool ic): StringExpression(op,t,ic) {} bool eval(const ExpFileEntry& file); @@ -305,7 +305,7 @@ public: class ExtExpression: public StringExpression { public: - ExtExpression(enum StringOperator op, std::list &t, bool ic): + ExtExpression(enum StringOperator op, const std::list &t, bool ic): StringExpression(op,t,ic) {} bool eval(const ExpFileEntry& file); @@ -320,7 +320,7 @@ public: class HashExpression: public StringExpression { public: - HashExpression(enum StringOperator op, std::list &t): + HashExpression(enum StringOperator op, const std::list &t): StringExpression(op,t, true) {} bool eval(const ExpFileEntry& file); diff --git a/libretroshare/src/retroshare/rsfiles.h b/libretroshare/src/retroshare/rsfiles.h index 001328956..43197ecde 100644 --- a/libretroshare/src/retroshare/rsfiles.h +++ b/libretroshare/src/retroshare/rsfiles.h @@ -169,10 +169,9 @@ public: class RsFiles { - public: - - RsFiles() { return; } - virtual ~RsFiles() { return; } +public: + RsFiles() {} + virtual ~RsFiles() {} /** * Provides file data for the gui: media streaming or rpc clients. @@ -281,8 +280,8 @@ class RsFiles ***/ virtual void requestDirUpdate(void *ref) =0 ; // triggers the update of the given reference. Used when browsing. - virtual void setDownloadDirectory(std::string path) = 0; - virtual void setPartialsDirectory(std::string path) = 0; + virtual bool setDownloadDirectory(std::string path) = 0; + virtual bool setPartialsDirectory(std::string path) = 0; virtual std::string getDownloadDirectory() = 0; virtual std::string getPartialsDirectory() = 0; diff --git a/libretroshare/src/retroshare/rsgxscommon.h b/libretroshare/src/retroshare/rsgxscommon.h index 6d4b1655e..b1579e406 100644 --- a/libretroshare/src/retroshare/rsgxscommon.h +++ b/libretroshare/src/retroshare/rsgxscommon.h @@ -124,24 +124,27 @@ class RsGxsComment }; -class RsGxsCommentService +struct RsGxsCommentService { - public: + RsGxsCommentService() {} + virtual ~RsGxsCommentService() {} - RsGxsCommentService() { return; } -virtual ~RsGxsCommentService() { return; } + /** Get previously requested comment data with token */ + virtual bool getCommentData( uint32_t token, + std::vector &comments ) = 0; + virtual bool getRelatedComments( uint32_t token, + std::vector &comments ) = 0; -virtual bool getCommentData(const uint32_t &token, std::vector &comments) = 0; -virtual bool getRelatedComments(const uint32_t &token, std::vector &comments) = 0; + virtual bool createComment(uint32_t &token, RsGxsComment &comment) = 0; + virtual bool createVote(uint32_t &token, RsGxsVote &vote) = 0; -//virtual bool getDetailedCommentData(const uint32_t &token, std::vector &comments); - -virtual bool createComment(uint32_t &token, RsGxsComment &comment) = 0; -virtual bool createVote(uint32_t &token, RsGxsVote &vote) = 0; - -virtual bool acknowledgeComment(const uint32_t& token, std::pair& msgId) = 0; -virtual bool acknowledgeVote(const uint32_t& token, std::pair& msgId) = 0; + virtual bool acknowledgeComment( + uint32_t token, + std::pair& msgId ) = 0; + virtual bool acknowledgeVote( + uint32_t token, + std::pair& msgId ) = 0; }; diff --git a/libretroshare/src/retroshare/rsgxsiface.h b/libretroshare/src/retroshare/rsgxsiface.h index 3e2ae55ae..36c660ef8 100644 --- a/libretroshare/src/retroshare/rsgxsiface.h +++ b/libretroshare/src/retroshare/rsgxsiface.h @@ -40,8 +40,8 @@ class RsGxsChanges public: RsGxsChanges(): mService(0){} RsTokenService *mService; - std::map > mMsgs; - std::map > mMsgsMeta; + std::map > mMsgs; + std::map > mMsgsMeta; std::list mGrps; std::list mGrpsMeta; }; diff --git a/libretroshare/src/retroshare/rsgxsifacetypes.h b/libretroshare/src/retroshare/rsgxsifacetypes.h index fd477fc9f..071b6bd46 100644 --- a/libretroshare/src/retroshare/rsgxsifacetypes.h +++ b/libretroshare/src/retroshare/rsgxsifacetypes.h @@ -1,3 +1,23 @@ +/* + * rsgxsifacetypes.h + * + * Copyright (C) 2013 crispy + * Copyright (C) 2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + /* * rsgxsifacetypes.h * @@ -13,18 +33,20 @@ #include #include -#include -#include +#include "retroshare/rstypes.h" +#include "retroshare/rsids.h" +#include "serialiser/rsserializable.h" +#include "serialiser/rstypeserializer.h" typedef GXSGroupId RsGxsGroupId; typedef Sha1CheckSum RsGxsMessageId; typedef GXSId RsGxsId; typedef GXSCircleId RsGxsCircleId; -typedef std::map > GxsMsgIdResult; +typedef std::map > GxsMsgIdResult; typedef std::pair RsGxsGrpMsgIdPair; -typedef std::map > MsgRelatedIdResult; -typedef std::map > GxsMsgReq; +typedef std::map > MsgRelatedIdResult; +typedef std::map > GxsMsgReq; struct RsMsgMetaData; @@ -34,7 +56,7 @@ typedef std::map > MsgMetaResult; class RsGxsGrpMetaData; class RsGxsMsgMetaData; -struct RsGroupMetaData +struct RsGroupMetaData : RsSerializable { // (csoler) The correct default value to be used in mCircleType is GXS_CIRCLE_TYPE_PUBLIC, which is defined in rsgxscircles.h, // but because of a loop in the includes, I cannot include it here. So I replaced with its current value 0x0001. @@ -73,6 +95,30 @@ struct RsGroupMetaData std::string mServiceString; // Service Specific Free-Form extra storage. RsPeerId mOriginator; RsGxsCircleId mInternalCircle; + + /// @see RsSerializable + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { + RS_SERIAL_PROCESS(mGroupId); + RS_SERIAL_PROCESS(mGroupName); + RS_SERIAL_PROCESS(mGroupFlags); + RS_SERIAL_PROCESS(mSignFlags); + RS_SERIAL_PROCESS(mPublishTs); + RS_SERIAL_PROCESS(mAuthorId); + RS_SERIAL_PROCESS(mCircleId); + RS_SERIAL_PROCESS(mCircleType); + RS_SERIAL_PROCESS(mAuthenFlags); + RS_SERIAL_PROCESS(mParentGrpId); + RS_SERIAL_PROCESS(mSubscribeFlags); + RS_SERIAL_PROCESS(mPop); + RS_SERIAL_PROCESS(mVisibleMsgCount); + RS_SERIAL_PROCESS(mLastPost); + RS_SERIAL_PROCESS(mGroupStatus); + RS_SERIAL_PROCESS(mServiceString); + RS_SERIAL_PROCESS(mOriginator); + RS_SERIAL_PROCESS(mInternalCircle); + } }; diff --git a/libretroshare/src/retroshare/rsgxsservice.h b/libretroshare/src/retroshare/rsgxsservice.h index 83f73887b..affbeea50 100644 --- a/libretroshare/src/retroshare/rsgxsservice.h +++ b/libretroshare/src/retroshare/rsgxsservice.h @@ -58,7 +58,7 @@ class RsGxsMsgChange : public RsGxsNotify { public: RsGxsMsgChange(NotifyType type, bool metaChange) : NOTIFY_TYPE(type), mMetaChange(metaChange) {} - std::map > msgChangeMap; + std::map > msgChangeMap; NotifyType getType(){ return NOTIFY_TYPE;} bool metaChange() { return mMetaChange; } private: diff --git a/libretroshare/src/retroshare/rsidentity.h b/libretroshare/src/retroshare/rsidentity.h index bf650d462..47da42de4 100644 --- a/libretroshare/src/retroshare/rsidentity.h +++ b/libretroshare/src/retroshare/rsidentity.h @@ -6,7 +6,8 @@ * * RetroShare C++ Interface. * - * Copyright 2012-2012 by Robert Fernie. + * Copyright (C) 2012 Robert Fernie. + * Copyright (C) 2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -36,9 +37,12 @@ #include "retroshare/rsids.h" #include "serialiser/rstlvimage.h" #include "retroshare/rsgxscommon.h" +#include "serialiser/rsserializable.h" +#include "serialiser/rstypeserializer.h" +#include "util/rsdeprecate.h" /* The Main Interface Class - for information about your Peers */ -class RsIdentity; +struct RsIdentity; extern RsIdentity *rsIdentity; @@ -63,6 +67,7 @@ extern RsIdentity *rsIdentity; #define RSID_RELATION_OTHER 0x0008 #define RSID_RELATION_UNKNOWN 0x0010 +/// @deprecated remove toghether with RsGxsIdGroup::mRecognTags #define RSRECOGN_MAX_TAGINFO 5 // Unicode symbols. NOT utf-8 bytes, because of multi byte characters @@ -77,27 +82,36 @@ static const uint32_t RS_IDENTITY_FLAGS_PGP_KNOWN = 0x0004; static const uint32_t RS_IDENTITY_FLAGS_IS_OWN_ID = 0x0008; static const uint32_t RS_IDENTITY_FLAGS_IS_DEPRECATED= 0x0010; // used to denote keys with deprecated fingerprint format. -class GxsReputation +struct GxsReputation : RsSerializable { - public: - GxsReputation(); + GxsReputation(); - bool updateIdScore(bool pgpLinked, bool pgpKnown); - bool update(); // checks ranges and calculates overall score. - int mOverallScore; - int mIdScore; // PGP, Known, etc. - int mOwnOpinion; - int mPeerOpinion; + bool updateIdScore(bool pgpLinked, bool pgpKnown); + bool update(); /// checks ranges and calculates overall score. + + int32_t mOverallScore; + int32_t mIdScore; /// PGP, Known, etc. + int32_t mOwnOpinion; + int32_t mPeerOpinion; + + /// @see RsSerializable + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { + RS_SERIAL_PROCESS(mOverallScore); + RS_SERIAL_PROCESS(mIdScore); + RS_SERIAL_PROCESS(mOwnOpinion); + RS_SERIAL_PROCESS(mPeerOpinion); + } }; -struct RsGxsIdGroup +struct RsGxsIdGroup : RsSerializable { RsGxsIdGroup() : mLastUsageTS(0), mPgpKnown(false), mIsAContact(false) {} ~RsGxsIdGroup() {} - RsGroupMetaData mMeta; // In GroupMetaData. @@ -120,7 +134,7 @@ struct RsGxsIdGroup std::string mPgpIdSign; // Recognition Strings. MAX# defined above. - std::list mRecognTags; + RS_DEPRECATED std::list mRecognTags; // Avatar RsGxsImage mImage ; @@ -131,8 +145,11 @@ struct RsGxsIdGroup bool mIsAContact; // change that into flags one day RsPgpId mPgpId; GxsReputation mReputation; -}; + /// @see RsSerializable + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ); +}; std::ostream &operator<<(std::ostream &out, const RsGxsIdGroup &group); @@ -149,12 +166,11 @@ class RsRecognTag }; -class RsRecognTagDetails +struct RsRecognTagDetails { - public: - RsRecognTagDetails() - :valid_from(0), valid_to(0), tag_class(0), tag_type(0), - is_valid(false), is_pending(false) { return; } + RsRecognTagDetails() : + valid_from(0), valid_to(0), tag_class(0), tag_type(0), is_valid(false), + is_pending(false) {} time_t valid_from; time_t valid_to; @@ -167,105 +183,166 @@ class RsRecognTagDetails bool is_pending; }; -class RsIdOpinion -{ - public: - RsGxsId id; - int rating; -}; - -class RsIdentityParameters +struct RsIdentityParameters { - public: - RsIdentityParameters(): isPgpLinked(false) { return; } + RsIdentityParameters() : + isPgpLinked(false) {} + bool isPgpLinked; - std::string nickname; - RsGxsImage mImage ; + std::string nickname; + RsGxsImage mImage; }; -class RsIdentityUsage +struct RsIdentityUsage : RsSerializable { -public: - enum UsageCode { UNKNOWN_USAGE = 0x00, - GROUP_ADMIN_SIGNATURE_CREATION = 0x01, // These 2 are normally not normal GXS identities, but nothing prevents it to happen either. - GROUP_ADMIN_SIGNATURE_VALIDATION = 0x02, - GROUP_AUTHOR_SIGNATURE_CREATION = 0x03, // not typically used, since most services do not require group author signatures - GROUP_AUTHOR_SIGNATURE_VALIDATION = 0x04, - MESSAGE_AUTHOR_SIGNATURE_CREATION = 0x05, // most common use case. Messages are signed by authors in e.g. forums. - MESSAGE_AUTHOR_SIGNATURE_VALIDATION = 0x06, - GROUP_AUTHOR_KEEP_ALIVE = 0x07, // Identities are stamped regularly by crawlign the set of messages for all groups. That helps keepign the useful identities in hand. - MESSAGE_AUTHOR_KEEP_ALIVE = 0x08, // Identities are stamped regularly by crawlign the set of messages for all groups. That helps keepign the useful identities in hand. - CHAT_LOBBY_MSG_VALIDATION = 0x09, // Chat lobby msgs are signed, so each time one comes, or a chat lobby event comes, a signature verificaiton happens. - GLOBAL_ROUTER_SIGNATURE_CHECK = 0x0a, // Global router message validation - GLOBAL_ROUTER_SIGNATURE_CREATION = 0x0b, // Global router message signature - GXS_TUNNEL_DH_SIGNATURE_CHECK = 0x0c, // - GXS_TUNNEL_DH_SIGNATURE_CREATION = 0x0d, // - IDENTITY_DATA_UPDATE = 0x0e, // Group update on that identity data. Can be avatar, name, etc. - IDENTITY_GENERIC_SIGNATURE_CHECK = 0x0f, // Any signature verified for that identity - IDENTITY_GENERIC_SIGNATURE_CREATION = 0x10, // Any signature made by that identity - IDENTITY_GENERIC_ENCRYPTION = 0x11, - IDENTITY_GENERIC_DECRYPTION = 0x12, - CIRCLE_MEMBERSHIP_CHECK = 0x13 - } ; + enum UsageCode : uint8_t + { + UNKNOWN_USAGE = 0x00, - explicit RsIdentityUsage(uint16_t service,const RsIdentityUsage::UsageCode& code,const RsGxsGroupId& gid=RsGxsGroupId(),const RsGxsMessageId& mid=RsGxsMessageId(),uint64_t additional_id=0,const std::string& comment = std::string()); + /** These 2 are normally not normal GXS identities, but nothing prevents + * it to happen either. */ + GROUP_ADMIN_SIGNATURE_CREATION = 0x01, + GROUP_ADMIN_SIGNATURE_VALIDATION = 0x02, - uint16_t mServiceId; // Id of the service using that identity, as understood by rsServiceControl - UsageCode mUsageCode; // Specific code to use. Will allow forming the correct translated message in the GUI if necessary. - RsGxsGroupId mGrpId; // Group ID using the identity + /** Not typically used, since most services do not require group author + * signatures */ + GROUP_AUTHOR_SIGNATURE_CREATION = 0x03, + GROUP_AUTHOR_SIGNATURE_VALIDATION = 0x04, - RsGxsMessageId mMsgId; // Message ID using the identity - uint64_t mAdditionalId; // Some additional ID. Can be used for e.g. chat lobbies. - std::string mComment ; // additional comment to be used mainly for debugging, but not GUI display + /// most common use case. Messages are signed by authors in e.g. forums. + MESSAGE_AUTHOR_SIGNATURE_CREATION = 0x05, + MESSAGE_AUTHOR_SIGNATURE_VALIDATION = 0x06, - bool operator<(const RsIdentityUsage& u) const - { - return mHash < u.mHash ; - } - RsFileHash mHash ; + /** Identities are stamped regularly by crawlign the set of messages for + * all groups. That helps keepign the useful identities in hand. */ + GROUP_AUTHOR_KEEP_ALIVE = 0x07, + MESSAGE_AUTHOR_KEEP_ALIVE = 0x08, + + /** Chat lobby msgs are signed, so each time one comes, or a chat lobby + * event comes, a signature verificaiton happens. */ + CHAT_LOBBY_MSG_VALIDATION = 0x09, + + /// Global router message validation + GLOBAL_ROUTER_SIGNATURE_CHECK = 0x0a, + + /// Global router message signature + GLOBAL_ROUTER_SIGNATURE_CREATION = 0x0b, + + GXS_TUNNEL_DH_SIGNATURE_CHECK = 0x0c, + GXS_TUNNEL_DH_SIGNATURE_CREATION = 0x0d, + + /// Group update on that identity data. Can be avatar, name, etc. + IDENTITY_DATA_UPDATE = 0x0e, + + /// Any signature verified for that identity + IDENTITY_GENERIC_SIGNATURE_CHECK = 0x0f, + + /// Any signature made by that identity + IDENTITY_GENERIC_SIGNATURE_CREATION = 0x10, + + IDENTITY_GENERIC_ENCRYPTION = 0x11, + IDENTITY_GENERIC_DECRYPTION = 0x12, + CIRCLE_MEMBERSHIP_CHECK = 0x13 + } ; + + RsIdentityUsage( uint16_t service, const RsIdentityUsage::UsageCode& code, + const RsGxsGroupId& gid = RsGxsGroupId(), + const RsGxsMessageId& mid = RsGxsMessageId(), + uint64_t additional_id=0, + const std::string& comment = std::string() ); + + /// Id of the service using that identity, as understood by rsServiceControl + uint16_t mServiceId; + + /** Specific code to use. Will allow forming the correct translated message + * in the GUI if necessary. */ + UsageCode mUsageCode; + + /// Group ID using the identity + RsGxsGroupId mGrpId; + + /// Message ID using the identity + RsGxsMessageId mMsgId; + + /// Some additional ID. Can be used for e.g. chat lobbies. + uint64_t mAdditionalId; + + /// additional comment to be used mainly for debugging, but not GUI display + std::string mComment; + + bool operator<(const RsIdentityUsage& u) const { return mHash < u.mHash; } + RsFileHash mHash ; + + /// @see RsSerializable + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { + RS_SERIAL_PROCESS(mServiceId); + RS_SERIAL_PROCESS(mUsageCode); + RS_SERIAL_PROCESS(mGrpId); + RS_SERIAL_PROCESS(mMsgId); + RS_SERIAL_PROCESS(mAdditionalId); + RS_SERIAL_PROCESS(mComment); + RS_SERIAL_PROCESS(mHash); + } + + friend struct RsTypeSerializer; +private: + /** Accessible only to friend class RsTypeSerializer needed for + * deserialization */ + RsIdentityUsage(); }; -class RsIdentityDetails + +struct RsIdentityDetails : RsSerializable { -public: - RsIdentityDetails() - : mFlags(0), mLastUsageTS(0) { return; } + RsIdentityDetails() : mFlags(0), mLastUsageTS(0) {} RsGxsId mId; - // identity details. std::string mNickname; - uint32_t mFlags ; + uint32_t mFlags; - // PGP Stuff. - RsPgpId mPgpId; + RsPgpId mPgpId; - // Recogn details. - std::list mRecognTags; + /// @deprecated Recogn details. + RS_DEPRECATED std::list mRecognTags; - // Cyril: Reputation details. At some point we might want to merge information - // between the two into a single global score. Since the old reputation system - // is not finished yet, I leave this in place. We should decide what to do with it. - RsReputations::ReputationInfo mReputation; + /** Cyril: Reputation details. At some point we might want to merge + * information between the two into a single global score. Since the old + * reputation system is not finished yet, I leave this in place. We should + * decide what to do with it. + */ + RsReputations::ReputationInfo mReputation; - // avatar - RsGxsImage mAvatar ; + RsGxsImage mAvatar; - // last usage - time_t mLastUsageTS ; - std::map mUseCases ; + time_t mLastUsageTS; + + std::map mUseCases; + + /// @see RsSerializable + virtual void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) + { + RS_SERIAL_PROCESS(mId); + RS_SERIAL_PROCESS(mNickname); + RS_SERIAL_PROCESS(mFlags); + RS_SERIAL_PROCESS(mPgpId); + //RS_SERIAL_PROCESS(mReputation); + //RS_SERIAL_PROCESS(mAvatar); + RS_SERIAL_PROCESS(mLastUsageTS); + RS_SERIAL_PROCESS(mUseCases); + } }; -class RsIdentity: public RsGxsIfaceHelper +struct RsIdentity : RsGxsIfaceHelper { - -public: - explicit RsIdentity(RsGxsIface *gxs): RsGxsIfaceHelper(gxs) {} virtual ~RsIdentity() {} diff --git a/libretroshare/src/retroshare/rstokenservice.h b/libretroshare/src/retroshare/rstokenservice.h index bcb273dd5..079f34023 100644 --- a/libretroshare/src/retroshare/rstokenservice.h +++ b/libretroshare/src/retroshare/rstokenservice.h @@ -61,14 +61,17 @@ /* TODO CLEANUP: RS_TOKREQ_ANSTYPE_* values are meaningless and not used by * RsTokenService or its implementation, and may be arbitrarly defined by each - * GXS client as they are of no usage, their use is deprecated */ + * GXS client as they are of no usage, their use is deprecated, up until the + * definitive cleanup is done new code must use RS_DEPRECATED_TOKREQ_ANSTYPE for + * easier cleanup. */ #ifndef RS_NO_WARN_DEPRECATED # warning RS_TOKREQ_ANSTYPE_* macros are deprecated! #endif -#define RS_TOKREQ_ANSTYPE_LIST 0x0001 -#define RS_TOKREQ_ANSTYPE_SUMMARY 0x0002 -#define RS_TOKREQ_ANSTYPE_DATA 0x0003 -#define RS_TOKREQ_ANSTYPE_ACK 0x0004 +#define RS_DEPRECATED_TOKREQ_ANSTYPE 0x0000 +#define RS_TOKREQ_ANSTYPE_LIST 0x0001 +#define RS_TOKREQ_ANSTYPE_SUMMARY 0x0002 +#define RS_TOKREQ_ANSTYPE_DATA 0x0003 +#define RS_TOKREQ_ANSTYPE_ACK 0x0004 /*! diff --git a/libretroshare/src/retroshare/rstypes.h b/libretroshare/src/retroshare/rstypes.h index cc04d2312..dcad22795 100644 --- a/libretroshare/src/retroshare/rstypes.h +++ b/libretroshare/src/retroshare/rstypes.h @@ -36,6 +36,8 @@ #include #include +#include +#include #define USE_NEW_CHUNK_CHECKING_CODE @@ -118,13 +120,21 @@ class Condition std::string name; }; -class PeerBandwidthLimits +struct PeerBandwidthLimits : RsSerializable { -public: - PeerBandwidthLimits() : max_up_rate_kbs(0), max_dl_rate_kbs(0) {} - - uint32_t max_up_rate_kbs ; - uint32_t max_dl_rate_kbs ; + PeerBandwidthLimits() : max_up_rate_kbs(0), max_dl_rate_kbs(0) {} + + uint32_t max_up_rate_kbs; + uint32_t max_dl_rate_kbs; + + + /// @see RsSerializable + void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) + { + RS_SERIAL_PROCESS(max_up_rate_kbs); + RS_SERIAL_PROCESS(max_dl_rate_kbs); + } }; //class SearchRequest // unused stuff. @@ -295,7 +305,7 @@ class FileChunksInfo std::map > pending_slices ; }; -class CompressedChunkMap +class CompressedChunkMap : public RsSerializable { public: CompressedChunkMap() {} @@ -345,10 +355,16 @@ class CompressedChunkMap inline void set(uint32_t j) { _map[j >> 5] |= (1 << (j & 31)) ; } inline void reset(uint32_t j) { _map[j >> 5] &= ~(1 << (j & 31)) ; } - /// compressed map, one bit per chunk - std::vector _map ; + /// compressed map, one bit per chunk + std::vector _map; + + /// @see RsSerializable + void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) + { RS_SERIAL_PROCESS(_map); } }; + template class t_CRCMap { public: diff --git a/libretroshare/src/rsitems/rsbanlistitems.cc b/libretroshare/src/rsitems/rsbanlistitems.cc index 358d20961..44f908dee 100644 --- a/libretroshare/src/rsitems/rsbanlistitems.cc +++ b/libretroshare/src/rsitems/rsbanlistitems.cc @@ -48,8 +48,8 @@ void RsBanListItem::serial_process(RsGenericSerializer::SerializeJob j,RsGeneric void RsBanListConfigItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { - RsTypeSerializer::serial_process(j,ctx,type,"type") ; - RsTypeSerializer::serial_process (j,ctx,peerId,"peerId") ; + RsTypeSerializer::serial_process(j,ctx,banListType,"type") ; + RsTypeSerializer::serial_process (j,ctx,banListPeerId,"peerId") ; RsTypeSerializer::serial_process (j,ctx,update_time,"update_time") ; RsTypeSerializer::serial_process (j,ctx,banned_peers,"banned_peers") ; } diff --git a/libretroshare/src/rsitems/rsbanlistitems.h b/libretroshare/src/rsitems/rsbanlistitems.h index e5660e55c..6aec4b115 100644 --- a/libretroshare/src/rsitems/rsbanlistitems.h +++ b/libretroshare/src/rsitems/rsbanlistitems.h @@ -60,18 +60,20 @@ class RsBanListItem: public RsItem class RsBanListConfigItem: public RsItem { public: - RsBanListConfigItem() - :RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_BANLIST, RS_PKT_SUBTYPE_BANLIST_CONFIG_ITEM) {} + RsBanListConfigItem() + : RsItem(RS_PKT_VERSION_SERVICE, RS_SERVICE_TYPE_BANLIST, RS_PKT_SUBTYPE_BANLIST_CONFIG_ITEM) + , banListType(0), update_time(0) + {} - virtual ~RsBanListConfigItem(){} - virtual void clear() { banned_peers.TlvClear() ; } + virtual ~RsBanListConfigItem(){} + virtual void clear() { banned_peers.TlvClear() ; } void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx); - uint32_t type ; - RsPeerId peerId ; - time_t update_time ; - RsTlvBanList banned_peers; + uint32_t banListType ; + RsPeerId banListPeerId ; + time_t update_time ; + RsTlvBanList banned_peers; }; class RsBanListSerialiser: public RsServiceSerializer diff --git a/libretroshare/src/rsitems/rsconfigitems.cc b/libretroshare/src/rsitems/rsconfigitems.cc index f6296bd04..17edb7962 100644 --- a/libretroshare/src/rsitems/rsconfigitems.cc +++ b/libretroshare/src/rsitems/rsconfigitems.cc @@ -28,6 +28,7 @@ #include "rsitems/rsconfigitems.h" #include "retroshare/rspeers.h" // Needed for RsGroupInfo. +#include "serialiser/rsserializable.h" #include "serialiser/rstypeserializer.h" /*** * #define RSSERIAL_DEBUG 1 @@ -89,7 +90,7 @@ void RsFileTransfer::serial_process(RsGenericSerializer::SerializeJob j,RsGeneri RsTypeSerializer::serial_process (j,ctx,flags,"flags") ; RsTypeSerializer::serial_process (j,ctx,chunk_strategy,"chunk_strategy") ; - RsTypeSerializer::serial_process (j,ctx,compressed_chunk_map,"compressed_chunk_map") ; + RS_SERIAL_PROCESS(compressed_chunk_map); } void RsFileConfigItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) @@ -136,9 +137,9 @@ RsItem *RsPeerConfigSerialiser::create_item(uint8_t item_type,uint8_t item_subty void RsPeerNetItem::clear() { - peerId.clear(); - pgpId.clear(); - location.clear(); + nodePeerId.clear(); + pgpId.clear(); + location.clear(); netMode = 0; vs_disc = 0; vs_dht = 0; @@ -159,7 +160,7 @@ void RsPeerNetItem::clear() } void RsPeerNetItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { - RsTypeSerializer::serial_process(j,ctx,peerId,"peerId") ; + RsTypeSerializer::serial_process(j,ctx,nodePeerId,"peerId") ; RsTypeSerializer::serial_process(j,ctx,pgpId,"pgpId") ; RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_LOCATION,location,"location") ; @@ -192,31 +193,6 @@ void RsPeerStunItem::serial_process(RsGenericSerializer::SerializeJob j,RsGeneri RsTypeSerializer::serial_process(j,ctx,stunList,"stunList") ; } -template<> uint32_t RsTypeSerializer::serial_size(const PeerBandwidthLimits& /*s*/) -{ - return 4+4 ; -} - -template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset,const PeerBandwidthLimits& s) -{ - bool ok = true ; - ok = ok && setRawUInt32(data,size,&offset,s.max_up_rate_kbs); - ok = ok && setRawUInt32(data,size,&offset,s.max_dl_rate_kbs); - return ok; -} - -template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size,uint32_t& offset,PeerBandwidthLimits& s) -{ - bool ok = true ; - ok = ok && getRawUInt32(data,size,&offset,&s.max_up_rate_kbs); - ok = ok && getRawUInt32(data,size,&offset,&s.max_dl_rate_kbs); - return ok; -} - -template<> void RsTypeSerializer::print_data(const std::string& /*n*/, const PeerBandwidthLimits& s) -{ - std::cerr << " [Peer BW limit] " << s.max_up_rate_kbs << " / " << s.max_dl_rate_kbs << std::endl; -} RsNodeGroupItem::RsNodeGroupItem(const RsGroupInfo& g) :RsItem(RS_PKT_VERSION1, RS_PKT_CLASS_CONFIG, RS_PKT_TYPE_PEER_CONFIG, RS_PKT_SUBTYPE_NODE_GROUP) diff --git a/libretroshare/src/rsitems/rsconfigitems.h b/libretroshare/src/rsitems/rsconfigitems.h index bc7240e99..a22f907d6 100644 --- a/libretroshare/src/rsitems/rsconfigitems.h +++ b/libretroshare/src/rsitems/rsconfigitems.h @@ -84,7 +84,7 @@ public: virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx); /* networking information */ - RsPeerId peerId; /* Mandatory */ + RsPeerId nodePeerId; /* Mandatory */ RsPgpId pgpId; /* Mandatory */ std::string location; /* Mandatory */ uint32_t netMode; /* Mandatory */ diff --git a/libretroshare/src/rsitems/rsfiletransferitems.cc b/libretroshare/src/rsitems/rsfiletransferitems.cc index 7579c9560..4241081a7 100644 --- a/libretroshare/src/rsitems/rsfiletransferitems.cc +++ b/libretroshare/src/rsitems/rsfiletransferitems.cc @@ -91,48 +91,6 @@ void RsFileTransferSingleChunkCrcItem::serial_process(RsGenericSerializer::Seria RsTypeSerializer::serial_process (j,ctx,check_sum, "check_sum") ; } -//===================================================================================================// -// CompressedChunkMap // -//===================================================================================================// - -template<> uint32_t RsTypeSerializer::serial_size(const CompressedChunkMap& s) -{ - return 4 + 4*s._map.size() ; -} - -template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset,const CompressedChunkMap& s) -{ - bool ok = true ; - - ok &= setRawUInt32(data, size, &offset, s._map.size()); - - for(uint32_t i=0;i bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size,uint32_t& offset,CompressedChunkMap& s) -{ - uint32_t S =0; - bool ok = getRawUInt32(data, size, &offset, &S); - - if(ok) - { - s._map.resize(S) ; - - for(uint32_t i=0;i void RsTypeSerializer::print_data(const std::string& n, const CompressedChunkMap& s) -{ - std::cerr << " [Compressed chunk map] " << n << " : length=" << s._map.size() << std::endl; -} - //===================================================================================================// // Serializer // //===================================================================================================// diff --git a/libretroshare/src/rsitems/rsgxsitems.cc b/libretroshare/src/rsitems/rsgxsitems.cc index 141f22167..967c20824 100644 --- a/libretroshare/src/rsitems/rsgxsitems.cc +++ b/libretroshare/src/rsitems/rsgxsitems.cc @@ -127,3 +127,7 @@ void RsTurtleGxsSearchResultGroupDataItem::serial_process(RsGenericSerializer::S RsTypeSerializer::TlvMemBlock_proxy prox(encrypted_nxs_group_data,encrypted_nxs_group_data_len) ; RsTypeSerializer::serial_process(j,ctx,prox,"encrypted_nxs_data") ; } + +RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(TurtleGxsInfo) +RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(TurtleGxsInfo) + diff --git a/libretroshare/src/rsitems/rsgxsupdateitems.h b/libretroshare/src/rsitems/rsgxsupdateitems.h index ec21a14c8..13e10fbfd 100644 --- a/libretroshare/src/rsitems/rsgxsupdateitems.h +++ b/libretroshare/src/rsitems/rsgxsupdateitems.h @@ -28,19 +28,12 @@ -#if 0 -#include -#include "rsitems/rsserviceids.h" -#include "serialiser/rsserial.h" -#include "serialiser/rstlvbase.h" -#include "serialiser/rstlvtypes.h" -#include "serialiser/rstlvkeys.h" -#endif - #include "gxs/rsgxs.h" #include "gxs/rsgxsdata.h" #include "gxs/rsgxsnettunnel.h" #include "serialiser/rstlvidset.h" +#include "serialiser/rstypeserializer.h" +#include "serialiser/rsserializable.h" const uint8_t RS_PKT_SUBTYPE_GXS_GRP_UPDATE = 0x01; @@ -142,17 +135,26 @@ public: class RsGxsMsgUpdate { public: - struct MsgUpdateInfo + struct MsgUpdateInfo : RsSerializable { MsgUpdateInfo(): time_stamp(0), message_count(0) {} uint32_t time_stamp ; uint32_t message_count ; + + /// @see RsSerializable + void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) + { + RS_SERIAL_PROCESS(time_stamp); + RS_SERIAL_PROCESS(message_count); + } }; std::map msgUpdateInfos; }; + class RsGxsMsgUpdateItem : public RsGxsNetServiceItem, public RsGxsMsgUpdate { public: diff --git a/libretroshare/src/rsitems/rshistoryitems.cc b/libretroshare/src/rsitems/rshistoryitems.cc index a66299b51..1f16e976f 100644 --- a/libretroshare/src/rsitems/rshistoryitems.cc +++ b/libretroshare/src/rsitems/rshistoryitems.cc @@ -44,7 +44,7 @@ void RsHistoryMsgItem::serial_process(RsGenericSerializer::SerializeJob j,RsGene RsTypeSerializer::serial_process(j,ctx,version,"version") ; RsTypeSerializer::serial_process (j,ctx,chatPeerId,"chatPeerId") ; RsTypeSerializer::serial_process (j,ctx,incoming,"incoming") ; - RsTypeSerializer::serial_process (j,ctx,peerId,"peerId") ; + RsTypeSerializer::serial_process (j,ctx,msgPeerId,"peerId") ; RsTypeSerializer::serial_process (j,ctx,TLV_TYPE_STR_NAME,peerName,"peerName") ; RsTypeSerializer::serial_process(j,ctx,sendTime,"sendTime") ; RsTypeSerializer::serial_process(j,ctx,recvTime,"recvTime") ; diff --git a/libretroshare/src/rsitems/rshistoryitems.h b/libretroshare/src/rsitems/rshistoryitems.h index 802754e91..7f65f3d19 100644 --- a/libretroshare/src/rsitems/rshistoryitems.h +++ b/libretroshare/src/rsitems/rshistoryitems.h @@ -46,7 +46,7 @@ public: RsPeerId chatPeerId; // empty for global chat bool incoming; - RsPeerId peerId; + RsPeerId msgPeerId; std::string peerName; uint32_t sendTime; uint32_t recvTime; diff --git a/libretroshare/src/rsitems/rsitem.h b/libretroshare/src/rsitems/rsitem.h index 056bbe084..545870631 100644 --- a/libretroshare/src/rsitems/rsitem.h +++ b/libretroshare/src/rsitems/rsitem.h @@ -1,76 +1,96 @@ #pragma once +/* + * RetroShare Serialiser. + * Copyright (C) 2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ #include // for typeid #include "util/smallobject.h" #include "retroshare/rstypes.h" #include "serialiser/rsserializer.h" +#include "serialiser/rsserializable.h" #include "util/stacktrace.h" #include -class RsItem: public RsMemoryManagement::SmallObject +struct RsItem : RsMemoryManagement::SmallObject, RsSerializable { - public: - explicit RsItem(uint32_t t); - RsItem(uint8_t ver, uint8_t cls, uint8_t t, uint8_t subtype); + explicit RsItem(uint32_t t); + RsItem(uint8_t ver, uint8_t cls, uint8_t t, uint8_t subtype); #ifdef DO_STATISTICS - void *operator new(size_t s) ; - void operator delete(void *,size_t s) ; + void *operator new(size_t s) ; + void operator delete(void *,size_t s) ; #endif - virtual ~RsItem(); + virtual ~RsItem(); - /// TODO: Do this make sense with the new serialization system? - virtual void clear() = 0; + /// TODO: Do this make sense with the new serialization system? + virtual void clear() = 0; - virtual std::ostream &print(std::ostream &out, uint16_t /* indent */ = 0) - { - RsGenericSerializer::SerializeContext ctx(NULL,0,RsGenericSerializer::FORMAT_BINARY,RsGenericSerializer::SERIALIZATION_FLAG_NONE); - serial_process(RsGenericSerializer::PRINT,ctx) ; - return out; - } + virtual std::ostream &print(std::ostream &out, uint16_t /* indent */ = 0) + { + RsGenericSerializer::SerializeContext ctx( + NULL, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE ); + serial_process(RsGenericSerializer::PRINT,ctx); + return out; + } - void print_string(std::string &out, uint16_t indent = 0); + void print_string(std::string &out, uint16_t indent = 0); - /* source / destination id */ - const RsPeerId& PeerId() const { return peerId; } - void PeerId(const RsPeerId& id) { peerId = id; } + /// source / destination id + const RsPeerId& PeerId() const { return peerId; } + void PeerId(const RsPeerId& id) { peerId = id; } - /* complete id */ - uint32_t PacketId() const; + /// complete id + uint32_t PacketId() const; - /* id parts */ - uint8_t PacketVersion(); - uint8_t PacketClass(); - uint8_t PacketType(); - uint8_t PacketSubType() const; + /// id parts + uint8_t PacketVersion(); + uint8_t PacketClass(); + uint8_t PacketType(); + uint8_t PacketSubType() const; - /* For Service Packets */ - RsItem(uint8_t ver, uint16_t service, uint8_t subtype); - uint16_t PacketService() const; /* combined Packet class/type (mid 16bits) */ - void setPacketService(uint16_t service); + /// For Service Packets + RsItem(uint8_t ver, uint16_t service, uint8_t subtype); + uint16_t PacketService() const; /* combined Packet class/type (mid 16bits) */ + void setPacketService(uint16_t service); - inline uint8_t priority_level() const { return _priority_level ;} - inline void setPriorityLevel(uint8_t l) { _priority_level = l ;} + inline uint8_t priority_level() const { return _priority_level ;} + inline void setPriorityLevel(uint8_t l) { _priority_level = l ;} - /** - * TODO: This should be made pure virtual as soon as all the codebase - * is ported to the new serialization system - */ - virtual void serial_process(RsGenericSerializer::SerializeJob, - RsGenericSerializer::SerializeContext&)// = 0; - { - std::cerr << "(EE) RsItem::serial_process() called by an item using" - << "new serialization classes, but not derived! Class is " - << typeid(*this).name() << std::endl; - print_stacktrace(); - } + /** + * TODO: This default implementation should be removed and childs structs + * implement ::serial_process(...) as soon as all the codebase is ported to + * the new serialization system + */ + virtual void serial_process(RsGenericSerializer::SerializeJob, + RsGenericSerializer::SerializeContext&)// = 0; + { + std::cerr << "(EE) RsItem::serial_process(...) called by an item using" + << "new serialization classes, but not derived! Class is " + << typeid(*this).name() << std::endl; + print_stacktrace(); + } - protected: - uint32_t type; - RsPeerId peerId; - uint8_t _priority_level ; +protected: + uint32_t type; + RsPeerId peerId; + uint8_t _priority_level; }; /// TODO: Do this make sense with the new serialization system? diff --git a/libretroshare/src/rsitems/rsnxsitems.cc b/libretroshare/src/rsitems/rsnxsitems.cc index 91c35ce72..8c6775275 100644 --- a/libretroshare/src/rsitems/rsnxsitems.cc +++ b/libretroshare/src/rsitems/rsnxsitems.cc @@ -73,12 +73,12 @@ void RsNxsSyncMsgItem::serial_process(RsGenericSerializer::SerializeJob j,RsGene void RsNxsMsg::serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) { - RS_REGISTER_SERIAL_MEMBER_TYPED(transactionNumber, uint32_t); - RS_REGISTER_SERIAL_MEMBER_TYPED(pos, uint8_t); - RS_REGISTER_SERIAL_MEMBER(msgId); - RS_REGISTER_SERIAL_MEMBER(grpId); - RS_REGISTER_SERIAL_MEMBER_TYPED(msg, RsTlvItem); - RS_REGISTER_SERIAL_MEMBER_TYPED(meta, RsTlvItem); + RS_SERIAL_PROCESS(transactionNumber); + RS_SERIAL_PROCESS(pos); + RS_SERIAL_PROCESS(msgId); + RS_SERIAL_PROCESS(grpId); + RS_SERIAL_PROCESS(msg); + RS_SERIAL_PROCESS(meta); } void RsNxsGrp::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) diff --git a/libretroshare/src/rsitems/rsphotoitems.cc b/libretroshare/src/rsitems/rsphotoitems.cc index 9c9ff2d02..3a5ad1ead 100644 --- a/libretroshare/src/rsitems/rsphotoitems.cc +++ b/libretroshare/src/rsitems/rsphotoitems.cc @@ -61,24 +61,23 @@ void RsGxsPhotoAlbumItem::serial_process(RsGenericSerializer::SerializeJob j,RsG RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_LOCATION, album.mWhere, "mWhere"); RsTypeSerializer::serial_process(j,ctx,TLV_TYPE_STR_PIC_TYPE, album.mThumbnail.type,"mThumbnail.type"); - RsTlvBinaryDataRef b(RS_SERVICE_GXS_TYPE_PHOTO, album.mThumbnail.data,album.mThumbnail.size); - - RsTypeSerializer::serial_process(j,ctx,b,"thumbnail binary data") ; + RsTlvBinaryDataRef b(RS_SERVICE_GXS_TYPE_PHOTO, album.mThumbnail.data, album.mThumbnail.size); + RsTypeSerializer::serial_process(j, ctx, b, "thumbnail binary data") ; } void RsGxsPhotoPhotoItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_CAPTION, photo.mCaption); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_CATEGORY, photo.mCategory); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_DESCR, photo.mDescription); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_HASH_TAG, photo.mHashTags); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_MSG, photo.mOther); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_PIC_AUTH, photo.mPhotographer); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_DATE, photo.mWhen); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_LOCATION, photo.mWhere); - RsTypeSerializer::serial_process(j,ctx, TLV_TYPE_STR_PIC_TYPE, photo.mThumbnail.type); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_CAPTION, photo.mCaption, "mCaption"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_CATEGORY, photo.mCategory, "mCategory"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_DESCR, photo.mDescription, "mDescription"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_HASH_TAG, photo.mHashTags, "mHashTags"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_MSG, photo.mOther, "mOther"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_PIC_AUTH, photo.mPhotographer, "mPhotographer"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_DATE, photo.mWhen, "mWhen"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_LOCATION, photo.mWhere, "mWhere"); + RsTypeSerializer::serial_process(j, ctx, TLV_TYPE_STR_PIC_TYPE, photo.mThumbnail.type, "mThumbnail.type"); - RsTlvBinaryDataRef b(RS_SERVICE_GXS_TYPE_PHOTO,photo.mThumbnail.data, photo.mThumbnail.size); - RsTypeSerializer::serial_process(j,ctx, b, "mThumbnail") ; + RsTlvBinaryDataRef b(RS_SERVICE_GXS_TYPE_PHOTO, photo.mThumbnail.data, photo.mThumbnail.size); + RsTypeSerializer::serial_process(j, ctx, b, "mThumbnail") ; } void RsGxsPhotoCommentItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { diff --git a/libretroshare/src/rsitems/rsphotoitems.h b/libretroshare/src/rsitems/rsphotoitems.h index 3819edb88..cf809ce8e 100644 --- a/libretroshare/src/rsitems/rsphotoitems.h +++ b/libretroshare/src/rsitems/rsphotoitems.h @@ -50,7 +50,7 @@ public: virtual ~RsGxsPhotoAlbumItem() { return;} void clear(); - std::ostream &print(std::ostream &out, uint16_t indent = 0); +// std::ostream &print(std::ostream &out, uint16_t indent = 0); virtual void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx); diff --git a/libretroshare/src/rsserver/p3peers.cc b/libretroshare/src/rsserver/p3peers.cc index 9292e1562..c6d686469 100644 --- a/libretroshare/src/rsserver/p3peers.cc +++ b/libretroshare/src/rsserver/p3peers.cc @@ -4,6 +4,7 @@ * RetroShare C++ Interface. * * Copyright 2004-2008 by Robert Fernie. + * Copyright (C) 2015-2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -332,27 +333,29 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d) d.hiddenNodePort = 0; d.hiddenType = RS_HIDDEN_TYPE_NONE; - if (sockaddr_storage_isnull(ps.localaddr)) + if(!sockaddr_storage_isnull(ps.localaddr)) + { + sockaddr_storage_ipv6_to_ipv4(ps.localaddr); + d.localAddr = sockaddr_storage_iptostring(ps.localaddr); + d.localPort = sockaddr_storage_port(ps.localaddr); + } + else { d.localAddr = "INVALID_IP"; d.localPort = 0; } - else - { - d.localAddr = sockaddr_storage_iptostring(ps.localaddr); - d.localPort = sockaddr_storage_port(ps.localaddr); - } - if (sockaddr_storage_isnull(ps.serveraddr)) + if(!sockaddr_storage_isnull(ps.serveraddr)) + { + sockaddr_storage_ipv6_to_ipv4(ps.serveraddr); + d.extAddr = sockaddr_storage_iptostring(ps.serveraddr); + d.extPort = sockaddr_storage_port(ps.serveraddr); + } + else { d.extAddr = "INVALID_IP"; d.extPort = 0; } - else - { - d.extAddr = sockaddr_storage_iptostring(ps.serveraddr); - d.extPort = sockaddr_storage_port(ps.serveraddr); - } d.dyndns = ps.dyndns; @@ -360,16 +363,16 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d) for(it = ps.ipAddrs.mLocal.mAddrs.begin(); it != ps.ipAddrs.mLocal.mAddrs.end(); ++it) { + sockaddr_storage_ipv6_to_ipv4(it->mAddr); std::string toto; - toto += "L:"; toto += sockaddr_storage_tostring(it->mAddr); rs_sprintf_append(toto, " %ld sec", time(NULL) - it->mSeenTime); d.ipAddressList.push_back(toto); } for(it = ps.ipAddrs.mExt.mAddrs.begin(); it != ps.ipAddrs.mExt.mAddrs.end(); ++it) { + sockaddr_storage_ipv6_to_ipv4(it->mAddr); std::string toto; - toto += "E:"; toto += sockaddr_storage_tostring(it->mAddr); rs_sprintf_append(toto, " %ld sec", time(NULL) - it->mSeenTime); d.ipAddressList.push_back(toto); @@ -416,6 +419,7 @@ bool p3Peers::getPeerDetails(const RsPeerId& id, RsPeerDetails &d) if (pcs.state & RS_PEER_S_CONNECTED) { + sockaddr_storage_ipv6_to_ipv4(pcs.connectaddr); d.connectAddr = sockaddr_storage_iptostring(pcs.connectaddr); d.connectPort = sockaddr_storage_port(pcs.connectaddr); } @@ -900,68 +904,33 @@ bool p3Peers::setHiddenNode(const RsPeerId &id, const std::string &address, uin return true; } -bool p3Peers::setLocalAddress(const RsPeerId &id, const std::string &addr_str, uint16_t port) +bool p3Peers::setLocalAddress(const RsPeerId &id, + const std::string &addr_str, uint16_t port) { #ifdef P3PEERS_DEBUG - std::cerr << "p3Peers::setLocalAddress() " << id << std::endl; + std::cerr << __PRETTY_FUNCTION__ << " " << id << " " << addr_str << " " + << port << std::endl; #endif - if(port < 1024) - { - std::cerr << "(EE) attempt to use a port that is reserved to the system: " << port << std::endl; - return false ; - } - - struct sockaddr_storage addr; - struct sockaddr_in *addrv4p = (struct sockaddr_in *) &addr; - addrv4p->sin_family = AF_INET; - addrv4p->sin_port = htons(port); - - int ret = 1; -/********************************** WINDOWS/UNIX SPECIFIC PART *******************/ -#ifndef WINDOWS_SYS - if (ret && (0 != inet_aton(addr_str.c_str(), &(addrv4p->sin_addr)))) -#else - addrv4p->sin_addr.s_addr = inet_addr(addr_str.c_str()); - if (ret) -#endif -/********************************** WINDOWS/UNIX SPECIFIC PART *******************/ - { - return mPeerMgr->setLocalAddress(id, addr); - } + sockaddr_storage addr; + if (sockaddr_storage_inet_pton(addr, addr_str)) + if (sockaddr_storage_setport(addr, port)) + return mPeerMgr->setLocalAddress(id, addr); return false; } -bool p3Peers::setExtAddress(const RsPeerId &id, const std::string &addr_str, uint16_t port) +bool p3Peers::setExtAddress(const RsPeerId &id, + const std::string &addr_str, uint16_t port) { #ifdef P3PEERS_DEBUG - std::cerr << "p3Peers::setExtAddress() " << id << std::endl; + std::cerr << __PRETTY_FUNCTION__ << " " << id << " " << addr_str << " " + << port << std::endl; #endif - if(port < 1024) - { - std::cerr << "(EE) attempt to use a port that is reserved to the system: " << port << std::endl; - return false ; - } - - // NOTE THIS IS IPV4 FOR NOW. - struct sockaddr_storage addr; - struct sockaddr_in *addrv4p = (struct sockaddr_in *) &addr; - addrv4p->sin_family = AF_INET; - addrv4p->sin_port = htons(port); - - int ret = 1; -/********************************** WINDOWS/UNIX SPECIFIC PART *******************/ -#ifndef WINDOWS_SYS - if (ret && (0 != inet_aton(addr_str.c_str(), &(addrv4p->sin_addr)))) -#else - addrv4p->sin_addr.s_addr = inet_addr(addr_str.c_str()); - if (ret) -#endif -/********************************** WINDOWS/UNIX SPECIFIC PART *******************/ - { - return mPeerMgr->setExtAddress(id, addr); - } + sockaddr_storage addr; + if (sockaddr_storage_inet_pton(addr, addr_str)) + if (sockaddr_storage_setport(addr, port)) + return mPeerMgr->setExtAddress(id, addr); return false; } @@ -1085,22 +1054,22 @@ std::string p3Peers::getPGPKey(const RsPgpId& pgp_id,bool include_signatures) unsigned char *mem_block = NULL; size_t mem_block_size = 0; - if(!AuthGPG::getAuthGPG()->exportPublicKey(RsPgpId(pgp_id),mem_block,mem_block_size,false,include_signatures)) + if( !AuthGPG::getAuthGPG()->exportPublicKey( + RsPgpId(pgp_id), mem_block, mem_block_size, + false, include_signatures ) ) { - std::cerr << "Cannot output certificate for id \"" << pgp_id << "\". Sorry." << std::endl; + std::cerr << "Cannot output certificate for id \"" << pgp_id + << "\". Sorry." << std::endl; return "" ; } - RsPeerDetails Detail ; + RsPeerDetails Detail; + if(!getGPGDetails(pgp_id,Detail)) return ""; - if(!getGPGDetails(pgp_id,Detail) ) - return "" ; + RsCertificate cert( Detail,mem_block,mem_block_size ); + delete[] mem_block ; - RsCertificate cert( Detail,mem_block,mem_block_size ) ; - - delete[] mem_block ; - - return cert.armouredPGPKey() ; + return cert.armouredPGPKey(); } diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 34b68ef11..8ec0d7e8c 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -1358,11 +1358,11 @@ int RsServer::StartupRetroShare() p3Wiki *mWiki = new p3Wiki(wiki_ds, NULL, mGxsIdService); // create GXS wiki service - RsGxsNetService* wiki_ns = new RsGxsNetService( - RS_SERVICE_GXS_TYPE_WIKI, wiki_ds, nxsMgr, - mWiki, mWiki->getServiceInfo(), - mGxsIdService, mGxsCircles, - pgpAuxUtils); + RsGxsNetService* wiki_ns = new RsGxsNetService( + RS_SERVICE_GXS_TYPE_WIKI, wiki_ds, nxsMgr, + mWiki, mWiki->getServiceInfo(), + mReputations, mGxsCircles, mGxsIdService, + pgpAuxUtils); mWiki->setNetworkExchangeService(wiki_ns) ; #endif diff --git a/libretroshare/src/rsserver/rsloginhandler.cc b/libretroshare/src/rsserver/rsloginhandler.cc index 453f5442c..ed952791f 100644 --- a/libretroshare/src/rsserver/rsloginhandler.cc +++ b/libretroshare/src/rsserver/rsloginhandler.cc @@ -109,6 +109,19 @@ GnomeKeyringPasswordSchema my_schema = { NULL, NULL }; +#elif defined(HAS_LIBSECRET) +#include +const SecretSchema *libsecret_get_schema(void) +{ + static const SecretSchema the_schema = { + "org.Retroshare.Password", SECRET_SCHEMA_NONE, + { + { "RetroShare SSL Id", SECRET_SCHEMA_ATTRIBUTE_STRING }, + { "NULL", (SecretSchemaAttributeType)0 }, + } + }; + return &the_schema; +} #endif @@ -209,7 +222,7 @@ bool RsLoginHandler::tryAutoLogin(const RsPeerId& ssl_id,std::string& ssl_passwd #endif if( gnome_keyring_find_password_sync(&my_schema, &passwd,"RetroShare SSL Id",ssl_id.toStdString().c_str(),NULL) == GNOME_KEYRING_RESULT_OK ) { - std::cerr << "Got SSL passwd ********************" /*<< passwd*/ << " from gnome keyring" << std::endl; + std::cout << "Got SSL passwd ********************" /*<< passwd*/ << " from gnome keyring" << std::endl; ssl_passwd = std::string(passwd); return true ; } @@ -220,7 +233,41 @@ bool RsLoginHandler::tryAutoLogin(const RsPeerId& ssl_id,std::string& ssl_passwd #endif return false ; } +#elif defined(HAS_LIBSECRET) + // do synchronous lookup +#ifdef DEBUG_RSLOGINHANDLER + std::cerr << "Using attribute: " << ssl_id << std::endl; +#endif + + GError *error = NULL; + gchar *password = secret_password_lookup_sync (libsecret_get_schema(), NULL, &error, + "RetroShare SSL Id", ssl_id.toStdString().c_str(), + NULL); + + if (error != NULL) { + g_error_free (error); +#ifdef DEBUG_RSLOGINHANDLER + std::cerr << "Could not get passwd using libsecret: error" << std::endl; +#endif + return false; + } else if (password == NULL) { + /* password will be null, if no matching password found */ +#ifdef DEBUG_RSLOGINHANDLER + std::cerr << "Could not get passwd using libsecret: not found" << std::endl; +#endif + return false; + } else { + std::cout << "Got SSL passwd ********************" /*<< passwd*/ << " using libsecret" << std::endl; + ssl_passwd = std::string(password); + + secret_password_free (password); + return true; + } +#ifdef DEBUG_RSLOGINHANDLER + std::cerr << "Could not get passwd from gnome keyring: unknown" << std::endl; +#endif + return false; #else /******************** OSX KeyChain stuff *****************************/ #ifdef __APPLE__ @@ -281,7 +328,7 @@ bool RsLoginHandler::tryAutoLogin(const RsPeerId& ssl_id,std::string& ssl_passwd /******************** OSX KeyChain stuff *****************************/ #endif // APPLE -#endif // HAS_GNOME_KEYRING +#endif // HAS_GNOME_KEYRING / HAS_LIBSECRET #else /******* WINDOWS BELOW *****/ /* try to load from file */ @@ -405,9 +452,9 @@ bool RsLoginHandler::enableAutoLogin(const RsPeerId& ssl_id,const std::string& s #ifndef __HAIKU__ #ifndef WINDOWS_SYS /* UNIX */ #if defined(HAS_GNOME_KEYRING) || defined(__FreeBSD__) || defined(__OpenBSD__) - if(GNOME_KEYRING_RESULT_OK == gnome_keyring_store_password_sync(&my_schema, NULL, (gchar*)("RetroShare password for SSL Id "+ssl_id.toStdString()).c_str(),(gchar*)ssl_passwd.c_str(),"RetroShare SSL Id",ssl_id.toStdString().c_str(),NULL)) + if(GNOME_KEYRING_RESULT_OK == gnome_keyring_store_password_sync(&my_schema, NULL, (gchar*)("RetroShare password for SSL Id "+ssl_id.toStdString()).c_str(),(gchar*)ssl_passwd.c_str(),"RetroShare SSL Id",ssl_id.toStdString().c_str(),NULL)) { - std::cerr << "Stored passwd " << "************************" << " into gnome keyring" << std::endl; + std::cout << "Stored passwd " << "************************" << " into gnome keyring" << std::endl; return true ; } else @@ -415,6 +462,24 @@ bool RsLoginHandler::enableAutoLogin(const RsPeerId& ssl_id,const std::string& s std::cerr << "Could not store passwd into gnome keyring" << std::endl; return false ; } +#elif defined(HAS_LIBSECRET) + // do synchronous store + + GError *error = NULL; + secret_password_store_sync (libsecret_get_schema(), SECRET_COLLECTION_DEFAULT, + (gchar*)("RetroShare password for SSL Id " + ssl_id.toStdString()).c_str(), + (gchar*)ssl_passwd.c_str(), + NULL, &error, + "RetroShare SSL Id", ssl_id.toStdString().c_str(), + NULL); + + if (error != NULL) { + g_error_free (error); + std::cerr << "Could not store passwd using libsecret" << std::endl; + return false; + } + std::cout << "Stored passwd " << "************************" << " using libsecret" << std::endl; + return true; #else #ifdef __APPLE__ /***************** OSX KEYCHAIN ****************/ @@ -484,7 +549,7 @@ bool RsLoginHandler::enableAutoLogin(const RsPeerId& ssl_id,const std::string& s return true; #endif // TODO_CODE_ROTTEN #endif // __APPLE__ -#endif // HAS_GNOME_KEYRING. +#endif // HAS_GNOME_KEYRING / HAS_LIBSECRET #else /* windows */ /* store password encrypted in a file */ @@ -571,7 +636,7 @@ bool RsLoginHandler::clearAutoLogin(const RsPeerId& ssl_id) #ifdef HAS_GNOME_KEYRING if(GNOME_KEYRING_RESULT_OK == gnome_keyring_delete_password_sync(&my_schema,"RetroShare SSL Id", ssl_id.toStdString().c_str(),NULL)) { - std::cerr << "Successfully Cleared gnome keyring passwd for SSLID " << ssl_id << std::endl; + std::cout << "Successfully Cleared gnome keyring passwd for SSLID " << ssl_id << std::endl; return true ; } else @@ -579,7 +644,26 @@ bool RsLoginHandler::clearAutoLogin(const RsPeerId& ssl_id) std::cerr << "Could not clear gnome keyring passwd for SSLID " << ssl_id << std::endl; return false ; } -#else +#elif defined(HAS_LIBSECRET) + // do synchronous clear + + GError *error = NULL; + gboolean removed = secret_password_clear_sync (libsecret_get_schema(), NULL, &error, + "RetroShare SSL Id", ssl_id.toStdString().c_str(), + NULL); + + if (error != NULL) { + g_error_free (error); + std::cerr << "Could not clearpasswd for SSLID " << ssl_id << " using libsecret: error" << std::endl; + return false ; + } else if (removed == FALSE) { + std::cerr << "Could not clearpasswd for SSLID " << ssl_id << " using libsecret: false" << std::endl; + return false ; + } + + std::cout << "Successfully Cleared passwd for SSLID " << ssl_id << " using libsecret" << std::endl; + return true ; +#else // HAS_GNOME_KEYRING / HAS_LIBSECRET #ifdef __APPLE__ std::cerr << "clearAutoLogin() OSX Version" << std::endl; diff --git a/libretroshare/src/rsserver/rstypes.cc b/libretroshare/src/rsserver/rstypes.cc index 209a7da88..4574c48e8 100644 --- a/libretroshare/src/rsserver/rstypes.cc +++ b/libretroshare/src/rsserver/rstypes.cc @@ -75,5 +75,3 @@ std::ostream &operator<<(std::ostream &out, const FileInfo &info) out << "Hash: " << info.hash; return out; } - - diff --git a/libretroshare/src/serialiser/rsbaseserial.cc b/libretroshare/src/serialiser/rsbaseserial.cc index 44bd249d1..ef862db5c 100644 --- a/libretroshare/src/serialiser/rsbaseserial.cc +++ b/libretroshare/src/serialiser/rsbaseserial.cc @@ -238,8 +238,7 @@ uint32_t getRawStringSize(const std::string &outStr) bool getRawString(const void *data, uint32_t size, uint32_t *offset, std::string &outStr) { -#warning Gio: "I had to change this. It seems like a bug to not clear the string. Should make sure it's not introducing any side effect." - outStr.clear(); + outStr.clear(); uint32_t len = 0; if (!getRawUInt32(data, size, offset, &len)) diff --git a/libretroshare/src/serialiser/rsserial.h b/libretroshare/src/serialiser/rsserial.h index 206f69608..ee3a79537 100644 --- a/libretroshare/src/serialiser/rsserial.h +++ b/libretroshare/src/serialiser/rsserial.h @@ -64,7 +64,7 @@ const uint8_t RS_PKT_CLASS_CONFIG = 0x02; const uint8_t RS_PKT_SUBTYPE_DEFAULT = 0x01; /* if only one subtype */ -class RsItem ; +struct RsItem; class RsSerialType ; diff --git a/libretroshare/src/serialiser/rsserializable.h b/libretroshare/src/serialiser/rsserializable.h new file mode 100644 index 000000000..c717afc4b --- /dev/null +++ b/libretroshare/src/serialiser/rsserializable.h @@ -0,0 +1,48 @@ +#pragma once +/* + * RetroShare Serialiser. + * Copyright (C) 2016-2018 Gioacchino Mazzurco + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include "serialiser/rsserializer.h" + + +/** @brief Minimal ancestor for all serializable structs in RetroShare. + * If you want your struct to be easly serializable you should inherit from this + * struct. + */ +struct RsSerializable +{ + /** Process struct members to serialize in this method taking advantage of + * the helper macro @see RS_SERIAL_PROCESS(I) + */ + virtual void serial_process(RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx) = 0; +}; + +/** @def RS_SERIAL_PROCESS(I) + * Use this macro to process the members of `YourSerializable` for serial + * processing inside `YourSerializable::serial_process(j, ctx)` + * + * Pay special attention for member of enum type which must be declared + * specifying the underlying type otherwise the serialization format may differ + * in an uncompatible way depending on the compiler/platform. + * + * Inspired by http://stackoverflow.com/a/39345864 + */ +#define RS_SERIAL_PROCESS(I) do { \ + RsTypeSerializer::serial_process(j, ctx, I, #I ); \ + } while(0) diff --git a/libretroshare/src/serialiser/rsserializer.h b/libretroshare/src/serialiser/rsserializer.h index 2cae93ab7..35dc091c3 100644 --- a/libretroshare/src/serialiser/rsserializer.h +++ b/libretroshare/src/serialiser/rsserializer.h @@ -3,7 +3,8 @@ * * RetroShare Serialiser. * - * Copyright 2016 by Cyril Soler + * Copyright (C) 2016 Cyril Soler + * Copyright (C) 2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -153,11 +154,13 @@ #include #include #include +#include #include "retroshare/rsflags.h" #include "serialiser/rsserial.h" +#include "util/rsdeprecate.h" -class RsItem ; +struct RsItem; #define SERIALIZE_ERROR() std::cerr << __PRETTY_FUNCTION__ << " : " @@ -194,106 +197,132 @@ class RsRawSerialiser: public RsSerialType virtual RsItem * deserialise(void *data, uint32_t *size); }; -// Top class for all services and config serializers. +typedef rapidjson::Document RsJson; -class RsGenericSerializer: public RsSerialType +/// Top class for all services and config serializers. +struct RsGenericSerializer : RsSerialType { -public: - typedef enum { SIZE_ESTIMATE = 0x01, SERIALIZE = 0x02, DESERIALIZE = 0x03, PRINT=0x04 } SerializeJob ; - typedef enum { FORMAT_BINARY = 0x01, FORMAT_JSON = 0x02 } SerializationFormat ; - - class SerializeContext - { - public: + typedef enum + { + SIZE_ESTIMATE = 0x01, + SERIALIZE = 0x02, + DESERIALIZE = 0x03, + PRINT = 0x04, + TO_JSON, + FROM_JSON + } SerializeJob; - SerializeContext(uint8_t *data,uint32_t size,SerializationFormat format,SerializationFlags flags) - : mData(data),mSize(size),mOffset(0),mOk(true),mFormat(format),mFlags(flags) {} + /** @deprecated use SerializeJob instead */ + RS_DEPRECATED typedef enum + { + FORMAT_BINARY = 0x01, + FORMAT_JSON = 0x02 + } SerializationFormat; - unsigned char *mData ; - uint32_t mSize ; - uint32_t mOffset ; - bool mOk ; - SerializationFormat mFormat ; - SerializationFlags mFlags ; - }; + struct SerializeContext + { + /** Allow shared allocator usage to avoid costly JSON deepcopy for + * nested RsSerializable */ + SerializeContext( + uint8_t *data, uint32_t size, + SerializationFlags flags = SERIALIZATION_FLAG_NONE, + RsJson::AllocatorType* allocator = nullptr) : + mData(data), mSize(size), mOffset(0), mOk(true), mFlags(flags), + mJson(rapidjson::kObjectType, allocator) {} - // These are convenience flags to be used by the items when processing the data. The names of the flags - // are not very important. What matters is that the serial_process() method of each item correctly - // deals with the data when it sees the flags, if the serialiser sets them. By default the flags are not - // set and shouldn't be handled. - // When deriving a new serializer, the user can set his own flags, using compatible values + RS_DEPRECATED SerializeContext( + uint8_t *data, uint32_t size, SerializationFormat format, + SerializationFlags flags, + RsJson::AllocatorType* allocator = nullptr) : + mData(data), mSize(size), mOffset(0), mOk(true), mFormat(format), + mFlags(flags), mJson(rapidjson::kObjectType, allocator) {} - static const SerializationFlags SERIALIZATION_FLAG_NONE ; // 0x0000 - static const SerializationFlags SERIALIZATION_FLAG_CONFIG ; // 0x0001 - static const SerializationFlags SERIALIZATION_FLAG_SIGNATURE ; // 0x0002 - static const SerializationFlags SERIALIZATION_FLAG_SKIP_HEADER ; // 0x0004 + unsigned char *mData; + uint32_t mSize; + uint32_t mOffset; + bool mOk; + RS_DEPRECATED SerializationFormat mFormat; + SerializationFlags mFlags; + RsJson mJson; + }; - // The following functions overload RsSerialType. They *should not* need to be further overloaded. + /** These are convenience flags to be used by the items when processing the + * data. The names of the flags are not very important. What matters is that + * the serial_process() method of each item correctly deals with the data + * when it sees the flags, if the serialiser sets them. + * By default the flags are not set and shouldn't be handled. + * When deriving a new serializer, the user can set his own flags, using + * compatible values + */ + static const SerializationFlags SERIALIZATION_FLAG_NONE; // 0x0000 + static const SerializationFlags SERIALIZATION_FLAG_CONFIG; // 0x0001 + static const SerializationFlags SERIALIZATION_FLAG_SIGNATURE; // 0x0002 + static const SerializationFlags SERIALIZATION_FLAG_SKIP_HEADER; // 0x0004 - RsItem *deserialise(void *data,uint32_t *size) =0; - bool serialise(RsItem *item,void *data,uint32_t *size) ; - uint32_t size(RsItem *item) ; - void print(RsItem *item) ; + /** + * The following functions overload RsSerialType. + * They *should not* need to be further overloaded. + */ + RsItem *deserialise(void *data,uint32_t *size) = 0; + bool serialise(RsItem *item,void *data,uint32_t *size); + uint32_t size(RsItem *item); + void print(RsItem *item); protected: - RsGenericSerializer(uint8_t serial_class, - uint8_t serial_type, - SerializationFormat format, - SerializationFlags flags ) - : RsSerialType(RS_PKT_VERSION1,serial_class,serial_type), mFormat(format),mFlags(flags) - {} + RsGenericSerializer( + uint8_t serial_class, uint8_t serial_type, + SerializationFormat format, SerializationFlags flags ) : + RsSerialType( RS_PKT_VERSION1, serial_class, serial_type), + mFormat(format), mFlags(flags) {} - RsGenericSerializer(uint16_t service, - SerializationFormat format, - SerializationFlags flags ) - : RsSerialType(RS_PKT_VERSION_SERVICE,service), mFormat(format),mFlags(flags) - {} - - SerializationFormat mFormat ; - SerializationFlags mFlags ; + RsGenericSerializer( + uint16_t service, SerializationFormat format, + SerializationFlags flags ) : + RsSerialType( RS_PKT_VERSION_SERVICE, service ), mFormat(format), + mFlags(flags) {} + SerializationFormat mFormat; + SerializationFlags mFlags; }; -// Top class for service serializers. Derive your on service serializer from this class and overload creat_item(). -class RsServiceSerializer: public RsGenericSerializer +/** Top class for service serializers. + * Derive your on service serializer from this class and overload creat_item(). + */ +struct RsServiceSerializer : RsGenericSerializer { -public: - RsServiceSerializer(uint16_t service_id, - SerializationFormat format = FORMAT_BINARY, - SerializationFlags flags = SERIALIZATION_FLAG_NONE) + RsServiceSerializer( + uint16_t service_id, SerializationFormat format = FORMAT_BINARY, + SerializationFlags flags = SERIALIZATION_FLAG_NONE ) : + RsGenericSerializer(service_id, format, flags) {} - : RsGenericSerializer(service_id,format,flags) {} + /*! should be overloaded to create the correct type of item depending on the + * data */ + virtual RsItem *create_item( uint16_t /* service */, + uint8_t /* item_sub_id */ ) const = 0; - /*! create_item - * should be overloaded to create the correct type of item depending on the data - */ - virtual RsItem *create_item(uint16_t /* service */, uint8_t /* item_sub_id */) const=0; - - RsItem *deserialise(void *data,uint32_t *size) ; + RsItem *deserialise(void *data, uint32_t *size); }; -// Top class for config serializers. Config serializers are only used internally by RS core. The development of new services or plugins do not need this. -class RsConfigSerializer: public RsGenericSerializer +/** Top class for config serializers. + * Config serializers are only used internally by RS core. + * The development of new services or plugins do not need this. + */ +struct RsConfigSerializer : RsGenericSerializer { -public: RsConfigSerializer(uint8_t config_class, uint8_t config_type, SerializationFormat format = FORMAT_BINARY, - SerializationFlags flags = SERIALIZATION_FLAG_NONE) + SerializationFlags flags = SERIALIZATION_FLAG_NONE) : + RsGenericSerializer(config_class,config_type,format,flags) {} - : RsGenericSerializer(config_class,config_type,format,flags) {} + /*! should be overloaded to create the correct type of item depending on the + * data */ + virtual RsItem *create_item(uint8_t /* item_type */, + uint8_t /* item_sub_type */) const = 0; - /*! create_item - * should be overloaded to create the correct type of item depending on the data - */ - virtual RsItem *create_item(uint8_t /* item_type */, uint8_t /* item_sub_type */) const=0; - - RsItem *deserialise(void *data,uint32_t *size) ; + RsItem *deserialise(void *data,uint32_t *size); }; - - - diff --git a/libretroshare/src/serialiser/rstypeserializer.cc b/libretroshare/src/serialiser/rstypeserializer.cc index 8be8660ae..e7faa5b52 100644 --- a/libretroshare/src/serialiser/rstypeserializer.cc +++ b/libretroshare/src/serialiser/rstypeserializer.cc @@ -3,7 +3,8 @@ * * RetroShare Serialiser. * - * Copyright 2017 by Cyril Soler + * Copyright (C) 2017 Cyril Soler + * Copyright (C) 2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -26,27 +27,49 @@ #include "serialiser/rstypeserializer.h" #include "serialiser/rsbaseserial.h" #include "serialiser/rstlvkeys.h" - -#include "rsitems/rsitem.h" +#include "serialiser/rsserializable.h" #include "util/rsprint.h" #include -#include #include +#include +#include // for typeid +#include +#include //static const uint32_t MAX_SERIALIZED_ARRAY_SIZE = 500 ; static const uint32_t MAX_SERIALIZED_CHUNK_SIZE = 10*1024*1024 ; // 10 MB. -//=================================================================================================// -// Integer types // -//=================================================================================================// +#define SAFE_GET_JSON_V() \ + const char* mName = memberName.c_str(); \ + bool ret = jDoc.HasMember(mName); \ + if(!ret) \ + { \ + std::cerr << __PRETTY_FUNCTION__ << " \"" << memberName \ + << "\" not found in JSON:" << std::endl \ + << jDoc << std::endl << std::endl; \ + print_stacktrace(); \ + return false; \ + } \ + rapidjson::Value& v = jDoc[mName] + + +//============================================================================// +// Integer types // +//============================================================================// template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const bool& member) { return setRawUInt8(data,size,&offset,member); } +template<> bool RsTypeSerializer::serialize(uint8_t /*data*/[], uint32_t /*size*/, uint32_t& /*offset*/, const int32_t& /*member*/) +{ + std::cerr << __PRETTY_FUNCTION__ << " Not implemented!" << std::endl; + print_stacktrace(); + return false; +} template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset, const uint8_t& member) { return setRawUInt8(data,size,&offset,member); @@ -70,10 +93,16 @@ template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint3 template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, bool& member) { - uint8_t m ; + uint8_t m; bool ok = getRawUInt8(data,size,&offset,&m); - member = m ; - return ok; + member = m; + return ok; +} +template<> bool RsTypeSerializer::deserialize(const uint8_t /*data*/[], uint32_t /*size*/, uint32_t& /*offset*/, int32_t& /*member*/) +{ + std::cerr << __PRETTY_FUNCTION__ << " Not implemented!" << std::endl; + print_stacktrace(); + return false; } template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size, uint32_t &offset, uint8_t& member) { @@ -100,6 +129,12 @@ template<> uint32_t RsTypeSerializer::serial_size(const bool& /* member*/) { return 1; } +template<> uint32_t RsTypeSerializer::serial_size(const int32_t& /* member*/) +{ + std::cerr << __PRETTY_FUNCTION__ << " Not implemented!" << std::endl; + print_stacktrace(); + return 0; +} template<> uint32_t RsTypeSerializer::serial_size(const uint8_t& /* member*/) { return 1; @@ -125,6 +160,10 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const bool & { std::cerr << " [bool ] " << n << ": " << V << std::endl; } +template<> void RsTypeSerializer::print_data(const std::string& n, const int32_t& V) +{ + std::cerr << " [int32_t ] " << n << ": " << V << std::endl; +} template<> void RsTypeSerializer::print_data(const std::string& n, const uint8_t & V) { std::cerr << " [uint8_t ] " << n << ": " << V << std::endl; @@ -146,10 +185,121 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const time_t& std::cerr << " [time_t ] " << n << ": " << V << " (" << time(NULL)-V << " secs ago)" << std::endl; } +#define SIMPLE_TO_JSON_DEF(T) \ +template<> bool RsTypeSerializer::to_JSON( const std::string& memberName, \ + const T& member, RsJson& jDoc ) \ +{ \ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); \ + \ + rapidjson::Value key; \ + key.SetString(memberName.c_str(), memberName.length(), allocator); \ + \ + rapidjson::Value value(member); \ + \ + jDoc.AddMember(key, value, allocator); \ + \ + return true; \ +} -//=================================================================================================// -// FLoats // -//=================================================================================================// +SIMPLE_TO_JSON_DEF(bool) +SIMPLE_TO_JSON_DEF(int32_t) + +template<> bool RsTypeSerializer::to_JSON( const std::string& memberName, + const time_t& member, RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + // without this compilation may break depending on how time_t is defined + int64_t tValue = member; + rapidjson::Value value(tValue); + + jDoc.AddMember(key, value, allocator); + + return true; +} + +SIMPLE_TO_JSON_DEF(uint8_t) +SIMPLE_TO_JSON_DEF(uint16_t) +SIMPLE_TO_JSON_DEF(uint32_t) +SIMPLE_TO_JSON_DEF(uint64_t) + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, bool& member, + RsJson& jDoc ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsBool(); + if(ret) member = v.GetBool(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + int32_t& member, RsJson& jDoc ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsInt(); + if(ret) member = v.GetInt(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, time_t& member, + RsJson& jDoc ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsInt(); + if(ret) member = v.GetInt(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint8_t& member, RsJson& jDoc ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsUint(); + if(ret) member = v.GetUint(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint16_t& member, RsJson& jDoc ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsUint(); + if(ret) member = v.GetUint(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint32_t& member, RsJson& jDoc ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsUint(); + if(ret) member = v.GetUint(); + return ret; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint64_t& member, RsJson& jDoc ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsUint(); + if(ret) member = v.GetUint(); + return ret; +} + + +//============================================================================// +// Floats // +//============================================================================// template<> uint32_t RsTypeSerializer::serial_size(const float&){ return 4; } @@ -166,10 +316,75 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const float& std::cerr << " [float ] " << n << ": " << V << std::endl; } +SIMPLE_TO_JSON_DEF(float) -//=================================================================================================// -// TlvString with subtype // -//=================================================================================================// +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + float& member, RsJson& jDoc ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsFloat(); + if(ret) member = v.GetFloat(); + return ret; +} + + +//============================================================================// +// std::string // +//============================================================================// + +template<> uint32_t RsTypeSerializer::serial_size(const std::string& str) +{ + return getRawStringSize(str); +} +template<> bool RsTypeSerializer::serialize( uint8_t data[], uint32_t size, + uint32_t& offset, + const std::string& str ) +{ + return setRawString(data, size, &offset, str); +} +template<> bool RsTypeSerializer::deserialize( const uint8_t data[], + uint32_t size, uint32_t &offset, + std::string& str ) +{ + return getRawString(data, size, &offset, str); +} +template<> void RsTypeSerializer::print_data( const std::string& n, + const std::string& str ) +{ + std::cerr << " [std::string] " << n << ": " << str << std::endl; +} +template<> /*static*/ +bool RsTypeSerializer::to_JSON( const std::string& membername, + const std::string& member, RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(membername.c_str(), membername.length(), allocator); + + rapidjson::Value value;; + value.SetString(member.c_str(), member.length(), allocator); + + jDoc.AddMember(key, value, allocator); + + return true; +} +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + std::string& member, RsJson& jDoc ) +{ + SAFE_GET_JSON_V(); + ret = ret && v.IsString(); + if(ret) member = v.GetString(); + return ret; +} + + + +//============================================================================// +// TlvString with subtype // +//============================================================================// template<> uint32_t RsTypeSerializer::serial_size(uint16_t /* type_subtype */,const std::string& s) { @@ -188,46 +403,131 @@ template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t siz template<> void RsTypeSerializer::print_data(const std::string& n, uint16_t type_substring,const std::string& V) { - std::cerr << " [TlvString ] " << n << ": type=" << std::hex < uint32_t RsTypeSerializer::serial_size(uint16_t /* type_subtype */,const uint32_t& /*s*/) +template<> /*static*/ +bool RsTypeSerializer::to_JSON( const std::string& memberName, + uint16_t /*sub_type*/, + const std::string& member, RsJson& jDoc ) { - return GetTlvUInt32Size() ; + return to_JSON(memberName, member, jDoc); } -template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset,uint16_t sub_type,const uint32_t& s) +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint16_t /*sub_type*/, + std::string& member, RsJson& jDoc ) { - return SetTlvUInt32(data,size,&offset,sub_type,s) ; + return from_JSON(memberName, member, jDoc); } -template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size,uint32_t& offset,uint16_t sub_type,uint32_t& s) +//============================================================================// +// TlvInt with subtype // +//============================================================================// + +template<> uint32_t RsTypeSerializer::serial_size( uint16_t /* type_subtype */, + const uint32_t& /*s*/ ) { - return GetTlvUInt32((void*)data,size,&offset,sub_type,&s) ; + return GetTlvUInt32Size(); +} + +template<> bool RsTypeSerializer::serialize( uint8_t data[], uint32_t size, + uint32_t &offset,uint16_t sub_type, + const uint32_t& s) +{ + return SetTlvUInt32(data,size,&offset,sub_type,s); +} + +template<> bool RsTypeSerializer::deserialize( const uint8_t data[], + uint32_t size, uint32_t& offset, + uint16_t sub_type, uint32_t& s) +{ + return GetTlvUInt32((void*)data, size, &offset, sub_type, &s); } template<> void RsTypeSerializer::print_data(const std::string& n, uint16_t sub_type,const uint32_t& V) { - std::cerr << " [TlvUInt32 ] " << n << ": type=" << std::hex < void RsTypeSerializer::print_data(const std::string& n, const std::string& V) +template<> /*static*/ +bool RsTypeSerializer::to_JSON( const std::string& memberName, + uint16_t /*sub_type*/, + const uint32_t& member, RsJson& jDoc ) { - std::cerr << " [std::string] " << n << ": " << V << std::endl; + return to_JSON(memberName, member, jDoc); } -//=================================================================================================// -// Binary blocks // -//=================================================================================================// +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& memberName, + uint16_t /*sub_type*/, + uint32_t& member, RsJson& jDoc ) +{ + return from_JSON(memberName, member, jDoc); +} + + +//============================================================================// +// TlvItems // +//============================================================================// + +template<> uint32_t RsTypeSerializer::serial_size(const RsTlvItem& s) +{ + return s.TlvSize(); +} + +template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, + uint32_t &offset,const RsTlvItem& s) +{ + return s.SetTlv(data,size,&offset); +} + +template<> bool RsTypeSerializer::deserialize(const uint8_t data[], + uint32_t size,uint32_t& offset, + RsTlvItem& s) +{ + return s.GetTlv((void*)data,size,&offset) ; +} + +template<> void RsTypeSerializer::print_data( const std::string& n, + const RsTlvItem& s ) +{ + std::cerr << " [" << typeid(s).name() << "] " << n << std::endl; +} + +template<> /*static*/ +bool RsTypeSerializer::to_JSON( const std::string& memberName, + const RsTlvItem& member, RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + rapidjson::Value value; + const char* tName = typeid(member).name(); + value.SetString(tName, allocator); + + jDoc.AddMember(key, value, allocator); + + return true; +} + +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& /*memberName*/, + RsTlvItem& member, RsJson& /*jDoc*/) +{ + member.TlvClear(); + return true; +} + + +//============================================================================// +// Binary blocks // +//============================================================================// template<> uint32_t RsTypeSerializer::serial_size(const RsTypeSerializer::TlvMemBlock_proxy& r) { return 4 + r.second ; } @@ -286,82 +586,40 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const RsTypeS std::cerr << " [Binary data] " << n << ", length=" << s.second << " data=" << RsUtil::BinToHex((uint8_t*)s.first,std::min(50u,s.second)) << ((s.second>50)?"...":"") << std::endl; } -//=================================================================================================// -// TlvItems // -//=================================================================================================// - -template<> uint32_t RsTypeSerializer::serial_size(const RsTlvItem& s) +template<> /*static*/ +bool RsTypeSerializer::to_JSON( + const std::string& memberName, + const RsTypeSerializer::TlvMemBlock_proxy& member, RsJson& jDoc ) { - return s.TlvSize() ; -} + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); -template<> bool RsTypeSerializer::serialize(uint8_t data[], uint32_t size, uint32_t &offset,const RsTlvItem& s) -{ - return s.SetTlv(data,size,&offset) ; -} + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); -template<> bool RsTypeSerializer::deserialize(const uint8_t data[], uint32_t size,uint32_t& offset,RsTlvItem& s) -{ - return s.GetTlv((void*)data,size,&offset) ; -} + rapidjson::Value value; + const char* tName = typeid(member).name(); + value.SetString(tName, allocator); -template<> void RsTypeSerializer::print_data(const std::string& n, const RsTlvItem& s) -{ - // can we call TlvPrint inside this? + jDoc.AddMember(key, value, allocator); - std::cerr << " [" << typeid(s).name() << "] " << n << std::endl; -} - -//============================================================================// -// RsItem and derivated // -//============================================================================// - -template<> uint32_t RsTypeSerializer::serial_size(const RsItem& s) -{ - RsGenericSerializer::SerializeContext ctx( - NULL, 0, RsGenericSerializer::FORMAT_BINARY, - RsGenericSerializer::SERIALIZATION_FLAG_NONE ); - - ctx.mOffset = 8; // header size - const_cast(s).serial_process(RsGenericSerializer::SIZE_ESTIMATE, - ctx); - - return ctx.mOffset; -} - -template<> bool RsTypeSerializer::serialize( uint8_t data[], uint32_t size, - uint32_t &offset, const RsItem& s ) -{ - RsGenericSerializer::SerializeContext ctx( - data, size, RsGenericSerializer::FORMAT_BINARY, - RsGenericSerializer::SERIALIZATION_FLAG_NONE ); - ctx.mOffset = offset; - const_cast(s).serial_process(RsGenericSerializer::SERIALIZE, - ctx); return true; } -template<> bool RsTypeSerializer::deserialize( const uint8_t data[], - uint32_t size, uint32_t& offset, - RsItem& s ) -{ - RsGenericSerializer::SerializeContext ctx( - const_cast(data), size, - RsGenericSerializer::FORMAT_BINARY, - RsGenericSerializer::SERIALIZATION_FLAG_NONE ); - ctx.mOffset = offset; - const_cast(s).serial_process(RsGenericSerializer::DESERIALIZE, - ctx); - return true; -} +template<> /*static*/ +bool RsTypeSerializer::from_JSON( const std::string& /*memberName*/, + RsTypeSerializer::TlvMemBlock_proxy&, + RsJson& /*jDoc*/) +{ return true; } -template<> void RsTypeSerializer::print_data( const std::string& /*n*/, - const RsItem& s ) + +//============================================================================// +// RsJson std:ostream support // +//============================================================================// + +std::ostream &operator<<(std::ostream &out, const RsJson &jDoc) { - RsGenericSerializer::SerializeContext ctx( - NULL, 0, - RsGenericSerializer::FORMAT_BINARY, - RsGenericSerializer::SERIALIZATION_FLAG_NONE ); - const_cast(s).serial_process(RsGenericSerializer::PRINT, - ctx); + rapidjson::StringBuffer buffer; buffer.Clear(); + rapidjson::PrettyWriter writer(buffer); + jDoc.Accept(writer); + return out << buffer.GetString(); } diff --git a/libretroshare/src/serialiser/rstypeserializer.h b/libretroshare/src/serialiser/rstypeserializer.h index f4867f10f..3e3c0b251 100644 --- a/libretroshare/src/serialiser/rstypeserializer.h +++ b/libretroshare/src/serialiser/rstypeserializer.h @@ -3,7 +3,8 @@ * * RetroShare Serialiser. * - * Copyright 2017 by Cyril Soler + * Copyright (C) 2017 Cyril Soler + * Copyright (C) 2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -32,83 +33,95 @@ #include "retroshare/rsids.h" #include "serialiser/rsserializer.h" +#include "serialiser/rsserializable.h" -/** @def RS_REGISTER_SERIAL_MEMBER(I) - * Use this macro to register the members of `YourItem` for serial processing - * inside `YourItem::serial_process(j, ctx)` - * - * Inspired by http://stackoverflow.com/a/39345864 +#include +#include // for typeid +#include +#include + + +/** INTERNAL ONLY helper to avoid copy paste code for std::{vector,list,set} + * Can't use a template function because T is needed for const_cast */ +#define RsTypeSerializer_PRIVATE_TO_JSON_ARRAY() do \ +{ \ + using namespace rapidjson; \ +\ + Document::AllocatorType& allocator = ctx.mJson.GetAllocator(); \ +\ + Value arrKey; arrKey.SetString(memberName.c_str(), allocator); \ +\ + Value arr(kArrayType); \ +\ + for (auto& el : v) \ + { \ + /* Use same allocator to avoid deep copy */\ + RsGenericSerializer::SerializeContext elCtx(\ + nullptr, 0, RsGenericSerializer::FORMAT_BINARY,\ + RsGenericSerializer::SERIALIZATION_FLAG_NONE,\ + &allocator );\ +\ + /* If el is const the default serial_process template is matched */ \ + /* also when specialization is necessary so the compilation break */ \ + serial_process(j, elCtx, const_cast(el), memberName); \ +\ + elCtx.mOk = elCtx.mOk && elCtx.mJson.HasMember(arrKey);\ + if(elCtx.mOk) arr.PushBack(elCtx.mJson[arrKey], allocator);\ + else\ + {\ + ctx.mOk = false;\ + break;\ + }\ + }\ +\ + ctx.mJson.AddMember(arrKey, arr, allocator);\ +} while (false) + +/** INTERNAL ONLY helper to avoid copy paste code for std::{vector,list,set} + * Can't use a template function because std::{vector,list,set} has different + * name for insert/push_back function */ -#define RS_REGISTER_SERIAL_MEMBER(I) \ - do { RsTypeSerializer::serial_process(j, ctx, I, #I); } while(0) +#define RsTypeSerializer_PRIVATE_FROM_JSON_ARRAY(INSERT_FUN) do\ +{\ + using namespace rapidjson;\ +\ + bool& ok(ctx.mOk);\ + Document& jDoc(ctx.mJson);\ + Document::AllocatorType& allocator = jDoc.GetAllocator();\ +\ + Value arrKey;\ + arrKey.SetString(memberName.c_str(), memberName.length());\ +\ + ok = ok && jDoc.IsObject();\ + ok = ok && jDoc.HasMember(arrKey);\ +\ + if(ok && jDoc[arrKey].IsArray())\ + {\ + for (auto&& arrEl : jDoc[arrKey].GetArray())\ + {\ + RsGenericSerializer::SerializeContext elCtx(\ + nullptr, 0, RsGenericSerializer::FORMAT_BINARY,\ + RsGenericSerializer::SERIALIZATION_FLAG_NONE,\ + &allocator );\ + elCtx.mJson.AddMember(arrKey, arrEl, allocator);\ +\ + T el;\ + serial_process(j, elCtx, el, memberName); \ + ok = ok && elCtx.mOk;\ +\ + if(ok) v.INSERT_FUN(el);\ + else break;\ + }\ + }\ +} while(false) -/** @def RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) - * This macro usage is similar to @see RS_REGISTER_SERIAL_MEMBER(I) but it - * permit to force serialization/deserialization type, it is expecially useful - * with enum class members or RsTlvItem derivative members, be very careful with - * the type you pass, as reinterpret_cast on a reference is used that is - * expecially permissive so you can shot your feet if not carefull enough. - * - * If you are using this with an RsItem derivative (so passing RsItem as T) - * consider to register your item type with @see RS_REGISTER_ITEM_TYPE(T) in - * association with @see RS_REGISTER_SERIAL_MEMBER(I) that rely on template - * function generation, as in this particular case - * RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) would cause the serial code rely on - * C++ dynamic dispatching that may have a noticeable impact on runtime - * performances. - */ -#pragma GCC diagnostic ignored "-Wstrict-aliasing" -#define RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) do {\ - RsTypeSerializer::serial_process(j, ctx, reinterpret_cast(I), #I);\ - } while(0) -#pragma GCC diagnostic pop - -/** @def RS_REGISTER_ITEM_TYPE(T) - * Use this macro into `youritem.cc` only if you need to process members of - * subtypes of RsItem. - * - * The usage of this macro is strictly needed only in some cases, for example if - * you are registering for serialization a member of a container that contains - * items of subclasses of RsItem like * `std::map` - * - * @code{.cpp} -struct PrivateOugoingMapItem : RsChatItem -{ - PrivateOugoingMapItem() : RsChatItem(RS_PKT_SUBTYPE_OUTGOING_MAP) {} - - void serial_process( RsGenericSerializer::SerializeJob j, - RsGenericSerializer::SerializeContext& ctx ); - - std::map store; -}; - -RS_REGISTER_ITEM_TYPE(RsChatMsgItem) - -void PrivateOugoingMapItem::serial_process( - RsGenericSerializer::SerializeJob j, - RsGenericSerializer::SerializeContext& ctx ) -{ - // store is of type - RS_REGISTER_SERIAL_MEMBER(store); -} - * @endcode - * - * If you use this macro with a lot of different item types this can cause the - * generated binary grow in size, consider the usage of - * @see RS_REGISTER_SERIAL_MEMBER_TYPED(I, T) passing RsItem as type in that - * case. - */ -#define RS_REGISTER_ITEM_TYPE(T) template<> \ - void RsTypeSerializer::serial_process( \ - RsGenericSerializer::SerializeJob j,\ - RsGenericSerializer::SerializeContext& ctx, T& item,\ - const std::string& /*name*/) { item.serial_process(j, ctx); } +std::ostream &operator<<(std::ostream &out, const RsJson &jDoc); struct RsTypeSerializer { /** This type should be used to pass a parameter to drive the serialisation * if needed */ - struct TlvMemBlock_proxy: public std::pair + struct TlvMemBlock_proxy : std::pair { TlvMemBlock_proxy(void*& p, uint32_t& s) : std::pair(p,s) {} @@ -118,9 +131,10 @@ struct RsTypeSerializer /// Generic types template - static void serial_process( RsGenericSerializer::SerializeJob j, + typename std::enable_if::value || !(std::is_base_of::value || std::is_enum::value || std::is_base_of::value)>::type + static /*void*/ serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, - T& member,const std::string& member_name) + T& member, const std::string& member_name) { switch(j) { @@ -138,9 +152,17 @@ struct RsTypeSerializer case RsGenericSerializer::PRINT: print_data(member_name,member); break; + case RsGenericSerializer::TO_JSON: + ctx.mOk = ctx.mOk && to_JSON(member_name, member, ctx.mJson); + break; + case RsGenericSerializer::FROM_JSON: + ctx.mOk = ctx.mOk && from_JSON(member_name, member, ctx.mJson); + break; default: - ctx.mOk = false; - throw std::runtime_error("Unknown serial job"); + std::cerr << __PRETTY_FUNCTION__ << " Unknown serial job: " + << static_cast::type>(j) + << std::endl; + exit(EINVAL); } } @@ -165,11 +187,21 @@ struct RsTypeSerializer serialize(ctx.mData,ctx.mSize,ctx.mOffset,type_id,member); break; case RsGenericSerializer::PRINT: - print_data(member_name,type_id,member); + print_data(member_name, member); + break; + case RsGenericSerializer::TO_JSON: + ctx.mOk = ctx.mOk && + to_JSON(member_name, type_id, member, ctx.mJson); + break; + case RsGenericSerializer::FROM_JSON: + ctx.mOk = ctx.mOk && + from_JSON(member_name, type_id, member, ctx.mJson); break; default: - ctx.mOk = false; - throw std::runtime_error("Unknown serial job"); + std::cerr << __PRETTY_FUNCTION__ << " Unknown serial job: " + << static_cast::type>(j) + << std::endl; + exit(EINVAL); } } @@ -178,7 +210,7 @@ struct RsTypeSerializer static void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, std::map& v, - const std::string& member_name ) + const std::string& memberName ) { switch(j) { @@ -225,11 +257,11 @@ struct RsTypeSerializer case RsGenericSerializer::PRINT: { if(v.empty()) - std::cerr << " Empty map \"" << member_name << "\"" + std::cerr << " Empty map \"" << memberName << "\"" << std::endl; else std::cerr << " std::map of " << v.size() << " elements: \"" - << member_name << "\"" << std::endl; + << memberName << "\"" << std::endl; for(typename std::map::iterator it(v.begin());it!=v.end();++it) { @@ -242,6 +274,95 @@ struct RsTypeSerializer } break; } + case RsGenericSerializer::TO_JSON: + { + using namespace rapidjson; + + Document::AllocatorType& allocator = ctx.mJson.GetAllocator(); + Value arrKey; arrKey.SetString(memberName.c_str(), + memberName.length(), allocator); + Value arr(kArrayType); + + for (auto& kv : v) + { + // Use same allocator to avoid deep copy + RsGenericSerializer::SerializeContext kCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE, + &allocator ); + serial_process(j, kCtx, const_cast(kv.first), "key"); + + RsGenericSerializer::SerializeContext vCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE, + &allocator ); + serial_process(j, vCtx, const_cast(kv.second), "value"); + + if(kCtx.mOk && vCtx.mOk) + { + Value el(kObjectType); + el.AddMember("key", kCtx.mJson["key"], allocator); + el.AddMember("value", vCtx.mJson["value"], allocator); + + arr.PushBack(el, allocator); + } + } + + ctx.mJson.AddMember(arrKey, arr, allocator); + + break; + } + case RsGenericSerializer::FROM_JSON: + { + using namespace rapidjson; + + bool& ok(ctx.mOk); + Document& jDoc(ctx.mJson); + Document::AllocatorType& allocator = jDoc.GetAllocator(); + + Value arrKey; + arrKey.SetString(memberName.c_str(), memberName.length()); + + ok = ok && jDoc.IsObject(); + ok = ok && jDoc.HasMember(arrKey); + + if(ok && jDoc[arrKey].IsArray()) + { + for (auto&& kvEl : jDoc[arrKey].GetArray()) + { + ok = ok && kvEl.IsObject(); + ok = ok && kvEl.HasMember("key"); + ok = ok && kvEl.HasMember("value"); + if (!ok) break; + + RsGenericSerializer::SerializeContext kCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE, + &allocator ); + if(ok) + kCtx.mJson.AddMember("key", kvEl["key"], allocator); + + T key; + ok = ok && (serial_process(j, kCtx, key, "key"), kCtx.mOk); + + RsGenericSerializer::SerializeContext vCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE, + &allocator ); + if(ok) + vCtx.mJson.AddMember("value", kvEl["value"], allocator); + + U value; + ok = ok && ( serial_process(j, vCtx, value, "value"), + vCtx.mOk ); + + if(ok) v.insert(std::pair(key,value)); + else break; + } + } + + break; + } default: break; } } @@ -251,7 +372,7 @@ struct RsTypeSerializer static void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, std::vector& v, - const std::string& member_name ) + const std::string& memberName ) { switch(j) { @@ -259,7 +380,7 @@ struct RsTypeSerializer { ctx.mOffset += 4; for(uint32_t i=0;i static void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, - std::set& v, const std::string& member_name ) + std::set& v, const std::string& memberName ) { switch(j) { @@ -311,7 +438,7 @@ struct RsTypeSerializer for(typename std::set::iterator it(v.begin());it!=v.end();++it) // the const cast here is a hack to avoid serial_process to // instantiate serialise(const T&) - serial_process(j,ctx,const_cast(*it) ,member_name); + serial_process(j,ctx,const_cast(*it) ,memberName); break; } case RsGenericSerializer::DESERIALIZE: @@ -321,7 +448,7 @@ struct RsTypeSerializer for(uint32_t i=0; i(j,ctx,tmp,member_name); + serial_process(j,ctx,tmp,memberName); v.insert(tmp); } break; @@ -333,7 +460,7 @@ struct RsTypeSerializer for(typename std::set::iterator it(v.begin());it!=v.end();++it) // the const cast here is a hack to avoid serial_process to // instantiate serialise(const T&) - serial_process(j,ctx,const_cast(*it) ,member_name); + serial_process(j,ctx,const_cast(*it) ,memberName); break; } case RsGenericSerializer::PRINT: @@ -343,6 +470,12 @@ struct RsTypeSerializer << std::endl; break; } + case RsGenericSerializer::TO_JSON: + RsTypeSerializer_PRIVATE_TO_JSON_ARRAY(); + break; + case RsGenericSerializer::FROM_JSON: + RsTypeSerializer_PRIVATE_FROM_JSON_ARRAY(insert); + break; default: break; } } @@ -352,7 +485,7 @@ struct RsTypeSerializer static void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, std::list& v, - const std::string& member_name ) + const std::string& memberName ) { switch(j) { @@ -360,7 +493,7 @@ struct RsTypeSerializer { ctx.mOffset += 4; for(typename std::list::iterator it(v.begin());it!=v.end();++it) - serial_process(j,ctx,*it ,member_name); + serial_process(j,ctx,*it ,memberName); break; } case RsGenericSerializer::DESERIALIZE: @@ -370,7 +503,7 @@ struct RsTypeSerializer for(uint32_t i=0;i(j,ctx,tmp,member_name); + serial_process(j,ctx,tmp,memberName); v.push_back(tmp); } break; @@ -380,7 +513,7 @@ struct RsTypeSerializer uint32_t n=v.size(); serial_process(j,ctx,n,"temporary size"); for(typename std::list::iterator it(v.begin());it!=v.end();++it) - serial_process(j,ctx,*it ,member_name); + serial_process(j,ctx,*it ,memberName); break; } case RsGenericSerializer::PRINT: @@ -390,6 +523,12 @@ struct RsTypeSerializer << std::endl; break; } + case RsGenericSerializer::TO_JSON: + RsTypeSerializer_PRIVATE_TO_JSON_ARRAY(); + break; + case RsGenericSerializer::FROM_JSON: + RsTypeSerializer_PRIVATE_FROM_JSON_ARRAY(push_back); + break; default: break; } } @@ -399,7 +538,7 @@ struct RsTypeSerializer static void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx, t_RsFlags32& v, - const std::string& /*member_name*/) + const std::string& memberName ) { switch(j) { @@ -421,16 +560,139 @@ struct RsTypeSerializer std::cerr << " Flags of type " << std::hex << N << " : " << v.toUInt32() << std::endl; break; + case RsGenericSerializer::TO_JSON: + ctx.mOk = to_JSON(memberName, v.toUInt32(), ctx.mJson); + break; + case RsGenericSerializer::FROM_JSON: + { + uint32_t f; + ctx.mOk = from_JSON(memberName, f, ctx.mJson); + v = t_RsFlags32(f); + break; + } + default: break; } } -/** TODO - * Serialization format is inside context, but context is not passed to - * following functions, that need to know the format to do the job, actually - * RsGenericSerializer::FORMAT_BINARY is assumed in all of them!! - */ + /** + * @brief serial process enum types + * + * On declaration of your member of enum type you must specify the + * underlying type otherwise the serialization format may differ in an + * uncompatible way depending on the compiler/platform. + */ + template + typename std::enable_if::value>::type + static /*void*/ serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx, + E& member, + const std::string& memberName ) + { +#ifdef RSSERIAL_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " processing enum: " + << typeid(E).name() << " as " + << typeid(typename std::underlying_type::type).name() + << std::endl; +#endif + serial_process( + j, ctx, + reinterpret_cast::type&>(member), + memberName ); + } + + /// RsSerializable and derivatives + template + typename std::enable_if::value>::type + static /*void*/ serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx, + T& member, + const std::string& memberName ) + { + switch(j) + { + case RsGenericSerializer::SIZE_ESTIMATE: // fallthrough + case RsGenericSerializer::DESERIALIZE: // fallthrough + case RsGenericSerializer::SERIALIZE: // fallthrough + case RsGenericSerializer::PRINT: + member.serial_process(j, ctx); + break; + case RsGenericSerializer::TO_JSON: + { + rapidjson::Document& jDoc(ctx.mJson); + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + // Reuse allocator to avoid deep copy later + RsGenericSerializer::SerializeContext lCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE, + &allocator ); + + member.serial_process(j, lCtx); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + /* Because the passed allocator is reused it doesn't go out of scope and + * there is no need of deep copy and we can take advantage of the much + * faster rapidjson move semantic */ + jDoc.AddMember(key, lCtx.mJson, allocator); + + ctx.mOk = ctx.mOk && lCtx.mOk; + break; + } + case RsGenericSerializer::FROM_JSON: + { + rapidjson::Document& jDoc(ctx.mJson); + const char* mName = memberName.c_str(); + ctx.mOk = ctx.mOk && jDoc.HasMember(mName); + + if(!ctx.mOk) + { + std::cerr << __PRETTY_FUNCTION__ << " \"" << memberName + << "\" not found in JSON:" << std::endl + << jDoc << std::endl << std::endl; + print_stacktrace(); + break; + } + + rapidjson::Value& v = jDoc[mName]; + + RsGenericSerializer::SerializeContext lCtx( + nullptr, 0, RsGenericSerializer::FORMAT_BINARY, + RsGenericSerializer::SERIALIZATION_FLAG_NONE ); + lCtx.mJson.SetObject() = v; // Beware of move semantic!! + + member.serial_process(j, lCtx); + ctx.mOk = ctx.mOk && lCtx.mOk; + + break; + } + default: + std::cerr << __PRETTY_FUNCTION__ << " Unknown serial job: " + << static_cast::type>(j) + << std::endl; + exit(EINVAL); + break; + } + } + + /// RsTlvItem derivatives only + template + typename std::enable_if::value && !std::is_same::value>::type + static /*void*/ serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx, + T& member, + const std::string& memberName ) + { + serial_process(j, ctx, static_cast(member), memberName); + } protected: + +//============================================================================// +// Generic types declarations // +//============================================================================// + template static bool serialize( uint8_t data[], uint32_t size, uint32_t &offset, const T& member ); @@ -442,17 +704,41 @@ protected: template static void print_data( const std::string& name, const T& member); + template static bool to_JSON( const std::string& membername, + const T& member, RsJson& jDoc ); + + template static bool from_JSON( const std::string& memberName, + T& member, RsJson& jDoc ); + +//============================================================================// +// Generic types + type_id declarations // +//============================================================================// template static bool serialize( uint8_t data[], uint32_t size, uint32_t &offset, uint16_t type_id, const T& member ); + template static bool deserialize( const uint8_t data[], uint32_t size, uint32_t &offset, uint16_t type_id, T& member ); + template static uint32_t serial_size( uint16_t type_id,const T& member ); - template static void print_data( - const std::string& name,uint16_t type_id,const T& member ); + + template static void print_data( const std::string& n, + uint16_t type_id,const T& member ); + + template static bool to_JSON( const std::string& membername, + uint16_t type_id, + const T& member, RsJson& jVal ); + + template static bool from_JSON( const std::string& memberName, + uint16_t type_id, + T& member, RsJson& jDoc ); + +//============================================================================// +// t_RsGenericId<...> declarations // +//============================================================================// template static bool serialize( @@ -466,13 +752,29 @@ protected: template static uint32_t serial_size( - const t_RsGenericIdType& member ); + const t_RsGenericIdType< + ID_SIZE_IN_BYTES,UPPER_CASE,UNIQUE_IDENTIFIER>& member ); template static void print_data( const std::string& name, const t_RsGenericIdType& member ); + template + static bool to_JSON( + const std::string& membername, + const t_RsGenericIdType& member, + RsJson& jVal ); + + template + static bool from_JSON( + const std::string& memberName, + t_RsGenericIdType& member, + RsJson& jDoc ); + +//============================================================================// +// t_RsTlvList<...> declarations // +//============================================================================// template static bool serialize( @@ -491,16 +793,33 @@ protected: static void print_data( const std::string& name, const t_RsTlvList& member); + + template + static bool to_JSON( const std::string& membername, + const t_RsTlvList& member, + RsJson& jVal ); + + template + static bool from_JSON( const std::string& memberName, + t_RsTlvList& member, + RsJson& jDoc ); }; +//============================================================================// +// t_RsGenericId<...> // +//============================================================================// -// t_RsGenericId<> template bool RsTypeSerializer::serialize ( uint8_t data[], uint32_t size, uint32_t &offset, - const t_RsGenericIdType& member ) -{ return (*const_cast *>(&member)).serialise(data,size,offset); } + const t_RsGenericIdType< + ID_SIZE_IN_BYTES,UPPER_CASE,UNIQUE_IDENTIFIER>& member ) +{ + return (*const_cast *>(&member) + ).serialise(data,size,offset); +} template bool RsTypeSerializer::deserialize( @@ -522,8 +841,45 @@ void RsTypeSerializer::print_data( << member << std::endl; } +template +bool RsTypeSerializer::to_JSON( const std::string& memberName, + const t_RsGenericIdType& member, + RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + const std::string vStr = member.toStdString(); + rapidjson::Value value; + value.SetString(vStr.c_str(), vStr.length(), allocator); + + jDoc.AddMember(key, value, allocator); + + return true; +} + +template +bool RsTypeSerializer::from_JSON( const std::string& membername, + t_RsGenericIdType& member, + RsJson& jVal ) +{ + const char* mName = membername.c_str(); + bool ret = jVal.HasMember(mName); + if(ret) + { + rapidjson::Value& v = jVal[mName]; + ret = ret && v.IsString(); + if(ret) member = t_RsGenericIdType(std::string(v.GetString())); + } + return ret; +} + +//============================================================================// +// t_RsTlvList<...> // +//============================================================================// -// t_RsTlvList<> template bool RsTypeSerializer::serialize( uint8_t data[], uint32_t size, uint32_t &offset, @@ -553,3 +909,86 @@ void RsTypeSerializer::print_data( std::cerr << " [t_RsTlvString<" << std::hex << TLV_TYPE << ">] : size=" << member.mList.size() << std::endl; } + +template /* static */ +bool RsTypeSerializer::to_JSON( const std::string& memberName, + const t_RsTlvList& member, + RsJson& jDoc ) +{ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); + + rapidjson::Value key; + key.SetString(memberName.c_str(), memberName.length(), allocator); + + rapidjson::Value value; + const char* tName = typeid(member).name(); + value.SetString(tName, allocator); + + jDoc.AddMember(key, value, allocator); + + std::cerr << __PRETTY_FUNCTION__ << " JSON serialization for type " + << typeid(member).name() << " " << memberName + << " not available." << std::endl; + print_stacktrace(); + return true; +} + +template +bool RsTypeSerializer::from_JSON( const std::string& memberName, + t_RsTlvList& member, + RsJson& /*jVal*/ ) +{ + std::cerr << __PRETTY_FUNCTION__ << " JSON deserialization for type " + << typeid(member).name() << " " << memberName + << " not available." << std::endl; + print_stacktrace(); + return true; +} + + +//============================================================================// +// Not implemented types macros // +//============================================================================// + +/** + * @def RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(T) + * @def RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(T) + * Helper macros for types that has not yet implemented to/from JSON + * should be deleted from the code as soon as they are not needed anymore + */ +#define RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(T) \ +template<> /*static*/ \ +bool RsTypeSerializer::to_JSON(const std::string& memberName, T const& member, \ + RsJson& jDoc ) \ +{ \ + rapidjson::Document::AllocatorType& allocator = jDoc.GetAllocator(); \ + \ + rapidjson::Value key; \ + key.SetString(memberName.c_str(), memberName.length(), allocator); \ + \ + rapidjson::Value value; \ + const char* tName = typeid(member).name(); \ + value.SetString(tName, allocator); \ + \ + jDoc.AddMember(key, value, allocator); \ + \ + std::cerr << __FILE__ << __LINE__ << __PRETTY_FUNCTION__ \ + << " JSON serialization for type " \ + << typeid(member).name() << " " << memberName \ + << " not available." << std::endl; \ + print_stacktrace(); \ + return true; \ +} + +#define RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(T) \ +template<> /*static*/ \ +bool RsTypeSerializer::from_JSON( const std::string& memberName, \ + T& member, RsJson& /*jDoc*/ ) \ +{ \ + std::cerr << __FILE__ << __LINE__ << __PRETTY_FUNCTION__ \ + << " JSON deserialization for type " \ + << typeid(member).name() << " " << memberName \ + << " not available." << std::endl; \ + print_stacktrace(); \ + return true; \ +} diff --git a/libretroshare/src/services/autoproxy/p3i2pbob.cc b/libretroshare/src/services/autoproxy/p3i2pbob.cc index 917b8a8a7..36e5604c7 100644 --- a/libretroshare/src/services/autoproxy/p3i2pbob.cc +++ b/libretroshare/src/services/autoproxy/p3i2pbob.cc @@ -890,7 +890,7 @@ bool p3I2pBob::connectI2P() } // create socket - mSocket = unix_socket(AF_INET, SOCK_STREAM, 0); + mSocket = unix_socket(PF_INET, SOCK_STREAM, 0); if (mSocket < 0) { rslog(RsLog::Warning, &i2pBobLogInfo, "connectI2P_locked Failed to open socket! Socket Error: " + socket_errorType(errno)); @@ -898,7 +898,7 @@ bool p3I2pBob::connectI2P() } // connect - int err = unix_connect(mSocket, (struct sockaddr *)&mI2PProxyAddr, sizeof(mI2PProxyAddr)); + int err = unix_connect(mSocket, mI2PProxyAddr); if (err != 0) { rslog(RsLog::Warning, &i2pBobLogInfo, "connectI2P_locked Failed to connect to BOB! Socket Error: " + socket_errorType(errno)); return false; diff --git a/libretroshare/src/services/p3banlist.cc b/libretroshare/src/services/p3banlist.cc index 6a061500c..b1fc07113 100644 --- a/libretroshare/src/services/p3banlist.cc +++ b/libretroshare/src/services/p3banlist.cc @@ -306,16 +306,14 @@ bool p3BanList::acceptedBanRanges_locked(const BanListPeer& blp) } return false ; } -bool p3BanList::isAddressAccepted(const sockaddr_storage &addr, uint32_t checking_flags,uint32_t *check_result) +bool p3BanList::isAddressAccepted(const sockaddr_storage &dAddr, uint32_t checking_flags,uint32_t *check_result) { - if(check_result != NULL) - *check_result = RSBANLIST_CHECK_RESULT_NOCHECK ; + sockaddr_storage addr; sockaddr_storage_copy(dAddr, addr); - if(sockaddr_storage_isLoopbackNet(addr)) - return true ; - - if(!mIPFilteringEnabled) - return true ; + if(!mIPFilteringEnabled) return true; + if(check_result != NULL) *check_result = RSBANLIST_CHECK_RESULT_NOCHECK; + if(!sockaddr_storage_ipv6_to_ipv4(addr)) return true; + if(sockaddr_storage_isLoopbackNet(addr)) return true; #ifdef DEBUG_BANLIST std::cerr << "isAddressAccepted(): tested addr=" << sockaddr_storage_iptostring(addr) << ", checking flags=" << checking_flags ; @@ -453,9 +451,20 @@ void p3BanList::getBannedIps(std::list &lst) lst.push_back(it->second) ; } -bool p3BanList::removeIpRange(const struct sockaddr_storage& addr,int masked_bytes,uint32_t list_type) +bool p3BanList::removeIpRange( const struct sockaddr_storage& dAddr, + int masked_bytes, uint32_t list_type ) { - RS_STACK_MUTEX(mBanMtx) ; + sockaddr_storage addr; sockaddr_storage_copy(dAddr, addr); + if(!sockaddr_storage_ipv6_to_ipv4(addr)) + { + std::cerr << __PRETTY_FUNCTION__ << " Cannot handle " + << sockaddr_storage_tostring(dAddr) + << " IPv6 not implemented yet!" + << std::endl; + return false; + } + + RS_STACK_MUTEX(mBanMtx); bool changed = false; std::map::iterator it ; @@ -485,9 +494,20 @@ bool p3BanList::removeIpRange(const struct sockaddr_storage& addr,int masked_byt return changed; } -bool p3BanList::addIpRange(const sockaddr_storage &addr, int masked_bytes,uint32_t list_type,const std::string& comment) +bool p3BanList::addIpRange( const sockaddr_storage &dAddr, int masked_bytes, + uint32_t list_type, const std::string& comment ) { - RS_STACK_MUTEX(mBanMtx) ; + sockaddr_storage addr; sockaddr_storage_copy(dAddr, addr); + if(!sockaddr_storage_ipv6_to_ipv4(addr)) + { + std::cerr << __PRETTY_FUNCTION__ << " Cannot handle " + << sockaddr_storage_tostring(dAddr) + << " IPv6 not implemented yet!" + << std::endl; + return false; + } + + RS_STACK_MUTEX(mBanMtx); if(getBitRange(addr) > uint32_t(masked_bytes)) { @@ -668,20 +688,31 @@ bool p3BanList::recvBanItem(RsBanListItem *item) } /* overloaded from pqiNetAssistSharePeer */ -void p3BanList::updatePeer(const RsPeerId& /*id*/, const struct sockaddr_storage &addr, int /*type*/, int /*reason*/, int time_stamp) +void p3BanList::updatePeer( const RsPeerId& /*id*/, + const sockaddr_storage &dAddr, + int /*type*/, int /*reason*/, int time_stamp ) { - RsPeerId ownId = mServiceCtrl->getOwnId(); + sockaddr_storage addr; sockaddr_storage_copy(dAddr, addr); + if(!sockaddr_storage_ipv6_to_ipv4(addr)) + { + std::cerr << __PRETTY_FUNCTION__ << " Cannot handle " + << sockaddr_storage_tostring(dAddr) + << " IPv6 not implemented yet!" + << std::endl; + return; + } - int int_reason = RSBANLIST_REASON_DHT; + RsPeerId ownId = mServiceCtrl->getOwnId(); - addBanEntry(ownId, addr, RSBANLIST_ORIGIN_SELF, int_reason, time_stamp); + int int_reason = RSBANLIST_REASON_DHT; - /* process */ - { - RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/ + addBanEntry(ownId, addr, RSBANLIST_ORIGIN_SELF, int_reason, time_stamp); - condenseBanSources_locked(); - } + /* process */ + { + RS_STACK_MUTEX(mBanMtx); + condenseBanSources_locked(); + } } RsSerialiser *p3BanList::setupSerialiser() @@ -704,9 +735,9 @@ bool p3BanList::saveList(bool &cleanup, std::list& itemlist) { RsBanListConfigItem *item = new RsBanListConfigItem ; - item->type = RSBANLIST_TYPE_PEERLIST ; - item->peerId = it->second.mPeerId ; - item->update_time = it->second.mLastUpdate ; + item->banListType = RSBANLIST_TYPE_PEERLIST ; + item->banListPeerId = it->second.mPeerId ; + item->update_time = it->second.mLastUpdate ; item->banned_peers.TlvClear() ; for(std::map::const_iterator it2 = it->second.mBanPeers.begin();it2!=it->second.mBanPeers.end();++it2) @@ -723,8 +754,8 @@ bool p3BanList::saveList(bool &cleanup, std::list& itemlist) // Add whitelist RsBanListConfigItem *item = new RsBanListConfigItem ; - item->type = RSBANLIST_TYPE_WHITELIST ; - item->peerId.clear() ; + item->banListType = RSBANLIST_TYPE_WHITELIST ; + item->banListPeerId.clear() ; item->update_time = 0 ; item->banned_peers.TlvClear() ; @@ -742,8 +773,8 @@ bool p3BanList::saveList(bool &cleanup, std::list& itemlist) item = new RsBanListConfigItem ; - item->type = RSBANLIST_TYPE_BLACKLIST ; - item->peerId.clear(); + item->banListType = RSBANLIST_TYPE_BLACKLIST ; + item->banListPeerId.clear(); item->update_time = 0 ; item->banned_peers.TlvClear() ; @@ -819,11 +850,11 @@ bool p3BanList::loadList(std::list& load) if(citem != NULL) { - if(citem->type == RSBANLIST_TYPE_PEERLIST) + if(citem->banListType == RSBANLIST_TYPE_PEERLIST) { - BanList& bl(mBanSources[citem->peerId]) ; + BanList& bl(mBanSources[citem->banListPeerId]) ; - bl.mPeerId = citem->peerId ; + bl.mPeerId = citem->banListPeerId ; bl.mLastUpdate = citem->update_time ; bl.mBanPeers.clear() ; @@ -839,7 +870,7 @@ bool p3BanList::loadList(std::list& load) std::cerr << "(WW) removed wrong address " << sockaddr_storage_iptostring(blp.addr) << std::endl; } } - else if(citem->type == RSBANLIST_TYPE_BLACKLIST) + else if(citem->banListType == RSBANLIST_TYPE_BLACKLIST) { mBanRanges.clear() ; @@ -854,7 +885,7 @@ bool p3BanList::loadList(std::list& load) std::cerr << "(WW) removed wrong address " << sockaddr_storage_iptostring(blp.addr) << std::endl; } } - else if(citem->type == RSBANLIST_TYPE_WHITELIST) + else if(citem->banListType == RSBANLIST_TYPE_WHITELIST) { mWhiteListedRanges.clear() ; @@ -872,7 +903,7 @@ bool p3BanList::loadList(std::list& load) } } else - std::cerr << "(EE) BanList item unknown type " << citem->type << ". This is a bug." << std::endl; + std::cerr << "(EE) BanList item unknown type " << citem->banListType << ". This is a bug." << std::endl; } delete *it ; @@ -882,10 +913,21 @@ bool p3BanList::loadList(std::list& load) return true ; } -bool p3BanList::addBanEntry(const RsPeerId &peerId, const struct sockaddr_storage &addr, - int level, uint32_t reason, time_t time_stamp) +bool p3BanList::addBanEntry( const RsPeerId &peerId, + const sockaddr_storage &dAddr, + int level, uint32_t reason, time_t time_stamp ) { - RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/ + sockaddr_storage addr; sockaddr_storage_copy(dAddr, addr); + if(!sockaddr_storage_ipv6_to_ipv4(addr)) + { + std::cerr << __PRETTY_FUNCTION__ << " Cannot handle " + << sockaddr_storage_tostring(dAddr) + << " IPv6 not implemented yet!" + << std::endl; + return false; + } + + RS_STACK_MUTEX(mBanMtx); time_t now = time(NULL); bool updated = false; diff --git a/libretroshare/src/services/p3banlist.h b/libretroshare/src/services/p3banlist.h index 5bb251b5a..34d4bc50c 100644 --- a/libretroshare/src/services/p3banlist.h +++ b/libretroshare/src/services/p3banlist.h @@ -47,27 +47,31 @@ class BanList std::map mBanPeers; }; -//!The RS BanList service. - /** - * - * Exchange list of Banned IP addresses with peers. - */ - +/** + * The RS BanList service. + * Exchange list of Banned IPv4 addresses with peers. + * + * @warning IPv4 only, IPv6 not supported yet! + */ class p3BanList: public RsBanList, public p3Service, public pqiNetAssistPeerShare, public p3Config /*, public pqiMonitor */ { public: p3BanList(p3ServiceControl *sc, p3NetMgr *nm); virtual RsServiceInfo getServiceInfo(); - /***** overloaded from RsBanList *****/ + /***** overloaded from RsBanList *****/ - virtual bool isAddressAccepted(const struct sockaddr_storage& addr, uint32_t checking_flags,uint32_t *check_result=NULL) ; + virtual bool isAddressAccepted( const sockaddr_storage& addr, + uint32_t checking_flags, + uint32_t *check_result=NULL ); virtual void getBannedIps(std::list& list) ; virtual void getWhiteListedIps(std::list& list) ; - virtual bool addIpRange(const struct sockaddr_storage& addr,int masked_bytes,uint32_t list_type,const std::string& comment) ; - virtual bool removeIpRange(const sockaddr_storage &addr, int masked_bytes, uint32_t list_type); + virtual bool addIpRange( const sockaddr_storage& addr, int masked_bytes, + uint32_t list_type, const std::string& comment ); + virtual bool removeIpRange( const sockaddr_storage &addr, int masked_bytes, + uint32_t list_type ); virtual void enableIPFiltering(bool b) ; virtual bool ipFilteringEnabled() ; @@ -86,7 +90,8 @@ public: /***** overloaded from pqiNetAssistPeerShare *****/ - virtual void updatePeer(const RsPeerId& id, const struct sockaddr_storage &addr, int type, int reason, int time_stamp); + virtual void updatePeer( const RsPeerId& id, const sockaddr_storage &addr, + int type, int reason, int time_stamp ); /*********************** p3config ******************************/ virtual RsSerialiser *setupSerialiser(); @@ -108,8 +113,10 @@ public: int sendPackets(); bool processIncoming(); - bool recvBanItem(RsBanListItem *item); - bool addBanEntry(const RsPeerId &peerId, const struct sockaddr_storage &addr, int level, uint32_t reason, time_t time_stamp); + bool recvBanItem(RsBanListItem *item); + bool addBanEntry( const RsPeerId &peerId, + const sockaddr_storage &addr, int level, uint32_t reason, + time_t time_stamp ); void sendBanLists(); int sendBanSet(const RsPeerId& peerid); diff --git a/libretroshare/src/services/p3discovery2.cc b/libretroshare/src/services/p3discovery2.cc index 286848a6f..3eacf2548 100644 --- a/libretroshare/src/services/p3discovery2.cc +++ b/libretroshare/src/services/p3discovery2.cc @@ -26,15 +26,11 @@ #include "services/p3discovery2.h" #include "pqi/p3peermgr.h" -#include "pqi/pqinetwork.h" // for getLocalAddresses #include "util/rsversioninfo.h" #include "retroshare/rsiface.h" #include "rsserver/p3face.h" -#include // for std::vector -#include // for std::random_shuffle - // Interface pointer. RsDisc *rsDisc = NULL; @@ -350,41 +346,6 @@ void p3discovery2::sendOwnContactInfo(const SSLID &sslid) peerState detail; if (mPeerMgr->getOwnNetStatus(detail)) { - /* Workaround to spread multiple local ip addresses when presents. This - * is needed because RS wrongly assumes that there is just one active - * local ip address at time. */ - std::vector addrs; - if(!detail.hiddenNode && getLocalAddresses(addrs)) - { - /* To work around MAX_ADDRESS_LIST_SIZE addresses limitation, - * let's shuffle the list of - * local addresses in the hope that with enough time every local - * address is advertised to trusted nodes so they may try to - * connect to all of them including the most convenient if a local - * connection exists.*/ - std::random_shuffle(addrs.begin(), addrs.end()); - - for (auto it = addrs.begin(); it!=addrs.end(); ++it) - { - sockaddr_storage& addr(*it); - if( sockaddr_storage_isValidNet(addr) && - !sockaddr_storage_isLoopbackNet(addr) && - !sockaddr_storage_sameip(addr, detail.localaddr) ) - { - pqiIpAddress pqiIp; - sockaddr_storage_clear(pqiIp.mAddr); - pqiIp.mAddr.ss_family = addr.ss_family; - sockaddr_storage_copyip(pqiIp.mAddr, addr); - sockaddr_storage_setport( - pqiIp.mAddr, - sockaddr_storage_port(detail.localaddr) ); - pqiIp.mSeenTime = time(nullptr); - pqiIp.mSrc = 0; - detail.ipAddrs.updateLocalAddrs(pqiIp); - } - } - } - RsDiscContactItem *pkt = new RsDiscContactItem(); /* Cyril: we dont send our own IP to an hidden node. It will not use it * anyway. */ @@ -513,13 +474,13 @@ void p3discovery2::updatePeerAddressList(const RsDiscContactItem *item) * permission matrix. Disabling this instead will make life more * difficult for average user, that moreover whould have no way to * revert an hardcoded policy. */ - - pqiIpAddrSet addrsFromPeer; + pqiIpAddrSet addrsFromPeer; addrsFromPeer.mLocal.extractFromTlv(item->localAddrList); addrsFromPeer.mExt.extractFromTlv(item->extAddrList); #ifdef P3DISC_DEBUG - std::cerr << "Setting address list to peer " << item->sslId << ", to be:" << std::endl ; + std::cerr << "Setting address list to peer " << item->sslId + << ", to be:" << std::endl ; std::string addrstr; addrsFromPeer.printAddrs(addrstr); diff --git a/libretroshare/src/services/p3gxschannels.cc b/libretroshare/src/services/p3gxschannels.cc index 221825e9b..de4bb1fe6 100644 --- a/libretroshare/src/services/p3gxschannels.cc +++ b/libretroshare/src/services/p3gxschannels.cc @@ -132,10 +132,10 @@ struct RsGxsForumNotifyRecordsItem: public RsItem virtual ~RsGxsForumNotifyRecordsItem() {} - void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) - { - RS_REGISTER_SERIAL_MEMBER(records); - } + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { RS_SERIAL_PROCESS(records); } + void clear() {} std::map records; @@ -218,8 +218,7 @@ RsGenExchange::ServiceCreate_Return p3GxsChannels::service_CreateGroup(RsGxsGrpI void p3GxsChannels::notifyChanges(std::vector &changes) { #ifdef GXSCHANNELS_DEBUG - std::cerr << "p3GxsChannels::notifyChanges()"; - std::cerr << std::endl; + std::cerr << "p3GxsChannels::notifyChanges() : " << changes.size() << "changes to notify" << std::endl; #endif p3Notify *notify = NULL; @@ -242,16 +241,12 @@ void p3GxsChannels::notifyChanges(std::vector &changes) /* message received */ if (notify) { - std::map > &msgChangeMap = msgChange->msgChangeMap; - std::map >::iterator mit; - for (mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) - { - std::vector::iterator mit1; - for (mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) + std::map > &msgChangeMap = msgChange->msgChangeMap; + for (auto mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) + for (auto mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) { notify->AddFeedItem(RS_FEED_ITEM_CHANNEL_MSG, mit->first.toStdString(), mit1->toStdString()); } - } } } @@ -262,9 +257,8 @@ void p3GxsChannels::notifyChanges(std::vector &changes) std::cerr << std::endl; #endif - std::map > &msgChangeMap = msgChange->msgChangeMap; - std::map >::iterator mit; - for(mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) + std::map > &msgChangeMap = msgChange->msgChangeMap; + for(auto mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) { #ifdef GXSCHANNELS_DEBUG std::cerr << "p3GxsChannels::notifyChanges() Msgs for Group: " << mit->first; @@ -809,10 +803,7 @@ void p3GxsChannels::request_SpecificUnprocessedPosts(std::list >::iterator it; for(it = ids.begin(); it != ids.end(); ++it) - { - std::vector &vect_msgIds = msgIds[it->first]; - vect_msgIds.push_back(it->second); - } + msgIds[it->first].insert(it->second); RsGenExchange::getTokenService()->requestMsgInfo(token, ansType, opts, msgIds); GxsTokenQueue::queueRequest(token, GXSCHANNELS_UNPROCESSED_SPECIFIC); @@ -1180,7 +1171,9 @@ void p3GxsChannels::setMessageProcessedStatus(uint32_t& token, const RsGxsGrpMsg setMsgStatusFlags(token, msgId, status, mask); } -void p3GxsChannels::setMessageReadStatus(uint32_t& token, const RsGxsGrpMsgIdPair& msgId, bool read) +void p3GxsChannels::setMessageReadStatus( uint32_t& token, + const RsGxsGrpMsgIdPair& msgId, + bool read ) { #ifdef GXSCHANNELS_DEBUG std::cerr << "p3GxsChannels::setMessageReadStatus()"; @@ -1190,10 +1183,8 @@ void p3GxsChannels::setMessageReadStatus(uint32_t& token, const RsGxsGrpMsgIdPai /* Always remove status unprocessed */ uint32_t mask = GXS_SERV::GXS_MSG_STATUS_GUI_NEW | GXS_SERV::GXS_MSG_STATUS_GUI_UNREAD; uint32_t status = GXS_SERV::GXS_MSG_STATUS_GUI_UNREAD; - if (read) - { - status = 0; - } + if (read) status = 0; + setMsgStatusFlags(token, msgId, status, mask); } diff --git a/libretroshare/src/services/p3gxschannels.h b/libretroshare/src/services/p3gxschannels.h index 685439211..961c05fa6 100644 --- a/libretroshare/src/services/p3gxschannels.h +++ b/libretroshare/src/services/p3gxschannels.h @@ -111,15 +111,12 @@ virtual bool setChannelDownloadDirectory(const RsGxsGroupId &groupId, const std: virtual bool getChannelDownloadDirectory(const RsGxsGroupId &groupId, std::string& directory); /* Comment service - Provide RsGxsCommentService - redirect to p3GxsCommentService */ -virtual bool getCommentData(const uint32_t &token, std::vector &msgs) - { - return mCommentService->getGxsCommentData(token, msgs); - } + virtual bool getCommentData(uint32_t token, std::vector &msgs) + { return mCommentService->getGxsCommentData(token, msgs); } -virtual bool getRelatedComments(const uint32_t &token, std::vector &msgs) - { - return mCommentService->getGxsRelatedComments(token, msgs); - } + virtual bool getRelatedComments( uint32_t token, + std::vector &msgs ) + { return mCommentService->getGxsRelatedComments(token, msgs); } virtual bool createComment(uint32_t &token, RsGxsComment &msg) { @@ -131,13 +128,13 @@ virtual bool createVote(uint32_t &token, RsGxsVote &msg) return mCommentService->createGxsVote(token, msg); } -virtual bool acknowledgeComment(const uint32_t& token, std::pair& msgId) +virtual bool acknowledgeComment(uint32_t token, std::pair& msgId) { return acknowledgeMsg(token, msgId); } -virtual bool acknowledgeVote(const uint32_t& token, std::pair& msgId) +virtual bool acknowledgeVote(uint32_t token, std::pair& msgId) { if (mCommentService->acknowledgeVote(token, msgId)) { diff --git a/libretroshare/src/services/p3gxscircles.cc b/libretroshare/src/services/p3gxscircles.cc index ac8e3a3a5..5da6bfacb 100644 --- a/libretroshare/src/services/p3gxscircles.cc +++ b/libretroshare/src/services/p3gxscircles.cc @@ -212,14 +212,15 @@ void p3GxsCircles::notifyChanges(std::vector &changes) #ifdef DEBUG_CIRCLES std::cerr << " Found circle Message Change Notification" << std::endl; #endif - for(std::map >::iterator mit = msgChange->msgChangeMap.begin(); mit != msgChange->msgChangeMap.end(); ++mit) + for(auto mit = msgChange->msgChangeMap.begin(); mit != msgChange->msgChangeMap.end(); ++mit) { #ifdef DEBUG_CIRCLES std::cerr << " Msgs for Group: " << mit->first << std::endl; #endif force_cache_reload(RsGxsCircleId(mit->first)); + if (notify && (c->getType() == RsGxsNotify::TYPE_RECEIVED_NEW) ) - for (std::vector::const_iterator msgIdIt(mit->second.begin()), end(mit->second.end()); msgIdIt != end; ++msgIdIt) + for (auto msgIdIt(mit->second.begin()), end(mit->second.end()); msgIdIt != end; ++msgIdIt) { const RsGxsMessageId& msgId = *msgIdIt; notify->AddFeedItem(RS_FEED_ITEM_CIRCLE_MEMB_REQ,RsGxsCircleId(mit->first).toStdString(),msgId.toStdString()); @@ -2120,7 +2121,7 @@ bool p3GxsCircles::processMembershipRequests(uint32_t token) #ifdef DEBUG_CIRCLES std::cerr << " Older than last known (" << time(NULL)-info.last_subscription_TS << " seconds ago): deleting." << std::endl; #endif - messages_to_delete[RsGxsGroupId(cid)].push_back(it->second[i]->meta.mMsgId) ; + messages_to_delete[RsGxsGroupId(cid)].insert(it->second[i]->meta.mMsgId) ; } } diff --git a/libretroshare/src/services/p3gxscommon.cc b/libretroshare/src/services/p3gxscommon.cc index 5793e58ff..e1db6750d 100644 --- a/libretroshare/src/services/p3gxscommon.cc +++ b/libretroshare/src/services/p3gxscommon.cc @@ -502,8 +502,8 @@ bool p3GxsCommentService::createGxsVote(uint32_t &token, RsGxsVote &vote) opts.mReqType = GXS_REQUEST_TYPE_MSG_META; GxsMsgReq msgIds; - std::vector &vect_msgIds = msgIds[parentId.first]; - vect_msgIds.push_back(parentId.second); + std::set &vect_msgIds = msgIds[parentId.first]; + vect_msgIds.insert(parentId.second); uint32_t int_token; mExchange->getTokenService()->requestMsgInfo(int_token, RS_TOKREQ_ANSTYPE_SUMMARY, opts, msgIds); diff --git a/libretroshare/src/services/p3gxsforums.cc b/libretroshare/src/services/p3gxsforums.cc index 2fa829c1c..db701714d 100644 --- a/libretroshare/src/services/p3gxsforums.cc +++ b/libretroshare/src/services/p3gxsforums.cc @@ -107,10 +107,10 @@ struct RsGxsForumNotifyRecordsItem: public RsItem virtual ~RsGxsForumNotifyRecordsItem() {} - void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) - { - RS_REGISTER_SERIAL_MEMBER(records); - } + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { RS_SERIAL_PROCESS(records); } + void clear() {} std::map records; @@ -206,15 +206,12 @@ void p3GxsForums::notifyChanges(std::vector &changes) RsGxsMsgChange *msgChange = dynamic_cast(c); if (msgChange) { - std::map > &msgChangeMap = msgChange->msgChangeMap; - std::map >::iterator mit; - for (mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) + std::map > &msgChangeMap = msgChange->msgChangeMap; + + for (auto mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) { - std::vector::iterator mit1; - for (mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) - { + for (auto mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) notify->AddFeedItem(RS_FEED_ITEM_FORUM_MSG, mit->first.toStdString(), mit1->toStdString()); - } } break; } diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index eff0f5cb5..42f149661 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -3,7 +3,8 @@ * * Id interface for RetroShare. * - * Copyright 2012-2012 by Robert Fernie. + * Copyright (C) 2012 Robert Fernie + * Copyright (C) 2018 Gioacchino Mazzurco * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -561,8 +562,8 @@ void p3IdService::notifyChanges(std::vector &changes) std::cerr << std::endl; #endif - std::map > &msgChangeMap = msgChange->msgChangeMap; - std::map >::iterator mit; + std::map > &msgChangeMap = msgChange->msgChangeMap; + std::map >::iterator mit; for(mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) { #ifdef DEBUG_IDS @@ -4505,11 +4506,30 @@ void p3IdService::handle_event(uint32_t event_type, const std::string &/*elabel* } } -RsIdentityUsage::RsIdentityUsage(uint16_t service,const RsIdentityUsage::UsageCode& code,const RsGxsGroupId& gid,const RsGxsMessageId& mid,uint64_t additional_id,const std::string& comment) - : mServiceId(service), mUsageCode(code), mGrpId(gid), mMsgId(mid),mAdditionalId(additional_id),mComment(comment) +void RsGxsIdGroup::serial_process( + RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) +{ + RS_SERIAL_PROCESS(mMeta); + RS_SERIAL_PROCESS(mPgpIdHash); + //RS_SERIAL_PROCESS(mPgpIdSign); + RS_SERIAL_PROCESS(mRecognTags); + //RS_SERIAL_PROCESS(mImage); + RS_SERIAL_PROCESS(mLastUsageTS); + RS_SERIAL_PROCESS(mPgpKnown); + RS_SERIAL_PROCESS(mIsAContact); + RS_SERIAL_PROCESS(mPgpId); + RS_SERIAL_PROCESS(mReputation); +} + +RsIdentityUsage::RsIdentityUsage( + uint16_t service, const RsIdentityUsage::UsageCode& code, + const RsGxsGroupId& gid, const RsGxsMessageId& mid, + uint64_t additional_id,const std::string& comment ) : + mServiceId(service), mUsageCode(code), mGrpId(gid), mMsgId(mid), + mAdditionalId(additional_id), mComment(comment) { #ifdef DEBUG_IDS - // This is a hack, since it will hash also mHash, but because it is initialized to 0, and only computed in the constructor here, it should be ok. std::cerr << "New identity usage: " << std::endl; std::cerr << " service=" << std::hex << service << std::endl; std::cerr << " code =" << std::hex << code << std::endl; @@ -4519,6 +4539,9 @@ RsIdentityUsage::RsIdentityUsage(uint16_t service,const RsIdentityUsage::UsageCo std::cerr << " commnt =\"" << std::hex << comment << "\"" << std::endl; #endif + /* This is a hack, since it will hash also mHash, but because it is + * initialized to 0, and only computed in the constructor here, it should + * be ok. */ librs::crypto::HashStream hs(librs::crypto::HashStream::SHA1) ; hs << (uint32_t)service ; @@ -4535,5 +4558,5 @@ RsIdentityUsage::RsIdentityUsage(uint16_t service,const RsIdentityUsage::UsageCo #endif } - - +RsIdentityUsage::RsIdentityUsage() : + mServiceId(0), mUsageCode(UNKNOWN_USAGE), mAdditionalId(0) {} diff --git a/libretroshare/src/services/p3postbase.cc b/libretroshare/src/services/p3postbase.cc index 5aaa6d88f..8b14e98b2 100644 --- a/libretroshare/src/services/p3postbase.cc +++ b/libretroshare/src/services/p3postbase.cc @@ -110,9 +110,8 @@ void p3PostBase::notifyChanges(std::vector &changes) std::cerr << std::endl; #endif - std::map > &msgChangeMap = msgChange->msgChangeMap; - std::map >::iterator mit; - for(mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) + std::map > &msgChangeMap = msgChange->msgChangeMap; + for(auto mit = msgChangeMap.begin(); mit != msgChangeMap.end(); ++mit) { #ifdef POSTBASE_DEBUG std::cerr << "p3PostBase::notifyChanges() Msgs for Group: " << mit->first; @@ -125,8 +124,7 @@ void p3PostBase::notifyChanges(std::vector &changes) if (notify && msgChange->getType() == RsGxsNotify::TYPE_RECEIVED_NEW) { - std::vector::iterator mit1; - for (mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) + for (auto mit1 = mit->second.begin(); mit1 != mit->second.end(); ++mit1) { notify->AddFeedItem(RS_FEED_ITEM_POSTED_MSG, mit->first.toStdString(), mit1->toStdString()); } @@ -431,7 +429,7 @@ void p3PostBase::background_loadMsgs(const uint32_t &token, bool unprocessed) mBgIncremental = unprocessed; } - std::map > postMap; + std::map > postMap; // generate vector of changes to push to the GUI. std::vector changes; @@ -487,7 +485,7 @@ void p3PostBase::background_loadMsgs(const uint32_t &token, bool unprocessed) #endif /* but we need to notify GUI about them */ - msgChanges->msgChangeMap[mit->first].push_back((*vit)->meta.mMsgId); + msgChanges->msgChangeMap[mit->first].insert((*vit)->meta.mMsgId); } else if (NULL != (commentItem = dynamic_cast(*vit))) { @@ -546,7 +544,7 @@ void p3PostBase::background_loadMsgs(const uint32_t &token, bool unprocessed) if (sit == mBgStatsMap.end()) { // add to map of ones to update. - postMap[groupId].push_back(threadId); + postMap[groupId].insert(threadId); mBgStatsMap[threadId] = PostStats(0,0,0); sit = mBgStatsMap.find(threadId); @@ -704,7 +702,7 @@ void p3PostBase::background_updateVoteCounts(const uint32_t &token) #endif stats.increment(it->second); - msgChanges->msgChangeMap[mit->first].push_back(vit->mMsgId); + msgChanges->msgChangeMap[mit->first].insert(vit->mMsgId); } else { diff --git a/libretroshare/src/services/p3posted.h b/libretroshare/src/services/p3posted.h index 0cb2bcd74..aa4bbc8ee 100644 --- a/libretroshare/src/services/p3posted.h +++ b/libretroshare/src/services/p3posted.h @@ -83,16 +83,14 @@ virtual void setMessageReadStatus(uint32_t& token, const RsGxsGrpMsgIdPair& msgI } - /* Comment service - Provide RsGxsCommentService - redirect to p3GxsCommentService */ -virtual bool getCommentData(const uint32_t &token, std::vector &msgs) - { - return mCommentService->getGxsCommentData(token, msgs); - } + /** Comment service - Provide RsGxsCommentService - + * redirect to p3GxsCommentService */ + virtual bool getCommentData(uint32_t token, std::vector &msgs) + { return mCommentService->getGxsCommentData(token, msgs); } -virtual bool getRelatedComments(const uint32_t &token, std::vector &msgs) - { - return mCommentService->getGxsRelatedComments(token, msgs); - } + virtual bool getRelatedComments( uint32_t token, + std::vector &msgs ) + { return mCommentService->getGxsRelatedComments(token, msgs); } virtual bool createComment(uint32_t &token, RsGxsComment &msg) { @@ -104,17 +102,14 @@ virtual bool createVote(uint32_t &token, RsGxsVote &msg) return mCommentService->createGxsVote(token, msg); } -virtual bool acknowledgeComment(const uint32_t& token, std::pair& msgId) - { - return acknowledgeMsg(token, msgId); - } + virtual bool acknowledgeComment( + uint32_t token, std::pair& msgId ) + { return acknowledgeMsg(token, msgId); } -virtual bool acknowledgeVote(const uint32_t& token, std::pair& msgId) + virtual bool acknowledgeVote( + uint32_t token, std::pair& msgId ) { - if (mCommentService->acknowledgeVote(token, msgId)) - { - return true; - } + if (mCommentService->acknowledgeVote(token, msgId)) return true; return acknowledgeMsg(token, msgId); } }; diff --git a/libretroshare/src/tcponudp/rsudpstack.h b/libretroshare/src/tcponudp/rsudpstack.h index e2dec61b0..ac1408a05 100644 --- a/libretroshare/src/tcponudp/rsudpstack.h +++ b/libretroshare/src/tcponudp/rsudpstack.h @@ -50,34 +50,37 @@ virtual bool resetAddress(struct sockaddr_in &local) { return false; } /*******************************************************/ #include "pqi/pqimonitor.h" +#include "util/rsnet.h" +#include "util/stacktrace.h" + #include class rsUdpStack: public UdpStack, public pqiNetListener { - public: - rsUdpStack(struct sockaddr_in &local) - :UdpStack(local) { return; } +public: + rsUdpStack(struct sockaddr_in &local) : UdpStack(local) {} - rsUdpStack(int testmode, struct sockaddr_in &local) - :UdpStack(testmode, local) { return; } + rsUdpStack(int testmode, struct sockaddr_in &local) : + UdpStack(testmode, local) {} - /* from pqiNetListener */ -virtual bool resetListener(const struct sockaddr_storage &local) + /// @see pqiNetListener + virtual bool resetListener(const sockaddr_storage& local) { - //std::cerr << "rsUdpStack::resetListener(" << sockaddr_storage_tostring(local) << ")"; - //std::cerr << std::endl; + sockaddr_storage temp; + sockaddr_storage_copy(local, temp); - if (local.ss_family != AF_INET) + if (!sockaddr_storage_ipv6_to_ipv4(temp)) { - std::cerr << "rsUdpStack::resetListener() NOT IPv4 ERROR"; - std::cerr << std::endl; - abort(); + std::cerr << __PRETTY_FUNCTION__ << " Got non IPv4 address ERROR" + << std::endl; + sockaddr_storage_dump(local); + print_stacktrace(); + return -EINVAL; } - struct sockaddr_in *addr = (struct sockaddr_in *) &local; + sockaddr_in *addr = reinterpret_cast(&temp); return resetAddress(*addr); } - }; class rsFixedUdpStack: public UdpStack, public pqiNetListener diff --git a/libretroshare/src/turtle/rsturtleitem.cc b/libretroshare/src/turtle/rsturtleitem.cc index d600b4877..20e9666f1 100644 --- a/libretroshare/src/turtle/rsturtleitem.cc +++ b/libretroshare/src/turtle/rsturtleitem.cc @@ -160,6 +160,9 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const RsRegul std::cerr << " [RegExpr ] " << n << ", tokens=" << expr._tokens.size() << " ints=" << expr._ints.size() << " strings=" << expr._strings.size() << std::endl; } +RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(RsRegularExpression::LinearizedExpression) +RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(RsRegularExpression::LinearizedExpression) + void RsTurtleFTSearchResultItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { RsTypeSerializer::serial_process(j,ctx,request_id,"request_id") ; @@ -230,6 +233,9 @@ template<> void RsTypeSerializer::print_data(const std::string& n, const TurtleF std::cerr << " [FileInfo ] " << n << " size=" << i.size << " hash=" << i.hash << ", name=" << i.name << std::endl; } +RS_TYPE_SERIALIZER_TO_JSON_NOT_IMPLEMENTED_DEF(TurtleFileInfo) +RS_TYPE_SERIALIZER_FROM_JSON_NOT_IMPLEMENTED_DEF(TurtleFileInfo) + void RsTurtleOpenTunnelItem::serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) { RsTypeSerializer::serial_process (j,ctx,file_hash ,"file_hash") ; diff --git a/libretroshare/src/turtle/turtleclientservice.h b/libretroshare/src/turtle/turtleclientservice.h index 9bdd15ad4..b3aa37af1 100644 --- a/libretroshare/src/turtle/turtleclientservice.h +++ b/libretroshare/src/turtle/turtleclientservice.h @@ -36,7 +36,7 @@ #include #include -class RsItem ; +struct RsItem; class p3turtle ; class RsTurtleClientService diff --git a/libretroshare/src/use_libretroshare.pri b/libretroshare/src/use_libretroshare.pri new file mode 100644 index 000000000..d17e36739 --- /dev/null +++ b/libretroshare/src/use_libretroshare.pri @@ -0,0 +1,33 @@ +DEPENDPATH *= $$system_path($$clean_path($${PWD}/../../libretroshare/src/)) +INCLUDEPATH *= $$system_path($$clean_path($${PWD}/../../libretroshare/src)) +LIBS *= -L$$system_path($$clean_path($${OUT_PWD}/../../libretroshare/src/lib/)) -lretroshare + +equals(TARGET, retroshare):equals(TEMPLATE, lib){ +} else { + PRE_TARGETDEPS *= $$system_path($$clean_path($$OUT_PWD/../../libretroshare/src/lib/libretroshare.a)) +} + +!include("../../openpgpsdk/src/use_openpgpsdk.pri"):error("Including") + +bitdht { + !include("../../libbitdht/src/use_libbitdht.pri"):error("Including") +} + +sLibs = +mLibs = $$RS_SQL_LIB ssl crypto $$RS_THREAD_LIB $$RS_UPNP_LIB +dLibs = + +linux-* { + mLibs += dl +} + +static { + sLibs *= $$mLibs +} else { + dLibs *= $$mLibs +} + +LIBS += $$linkStaticLibs(sLibs) +PRE_TARGETDEPS += $$pretargetStaticLibs(sLibs) + +LIBS += $$linkDynamicLibs(dLibs) diff --git a/libretroshare/src/util/extaddrfinder.cc b/libretroshare/src/util/extaddrfinder.cc index e01e37211..308a8ea48 100644 --- a/libretroshare/src/util/extaddrfinder.cc +++ b/libretroshare/src/util/extaddrfinder.cc @@ -94,7 +94,10 @@ static void getPage(const std::string& server_name,std::string& page) #endif std::cerr << "ExtAddrFinder: resolved hostname " << server_name << " to " << rs_inet_ntoa(in) << std::endl; - if(unix_connect(sockfd,(struct sockaddr *)&serveur, sizeof(serveur)) == -1) + sockaddr_storage server; + sockaddr_storage_setipv4(server, &serveur); + sockaddr_storage_setport(server, 80); + if(unix_connect(sockfd, server) == -1) { std::cerr << "ExtAddrFinder: Connection error to " << server_name << std::endl ; unix_close(sockfd); diff --git a/libretroshare/src/util/rsnet.h b/libretroshare/src/util/rsnet.h index ffd34ffb7..a5381702c 100644 --- a/libretroshare/src/util/rsnet.h +++ b/libretroshare/src/util/rsnet.h @@ -58,6 +58,15 @@ int inet_aton(const char *name, struct in_addr *addr); #endif /********************************** WINDOWS/UNIX SPECIFIC PART ******************/ +/** + * Workaround for binary compatibility between Windows XP (which miss + * IPV6_V6ONLY define), and newer Windows that has it. + * @see http://lua-users.org/lists/lua-l/2013-04/msg00191.html + */ +#ifndef IPV6_V6ONLY +# define IPV6_V6ONLY 27 +#endif + /* 64 bit conversions */ #ifndef ntohll uint64_t ntohll(uint64_t x); @@ -88,14 +97,26 @@ std::string rs_inet_ntoa(struct in_addr in); /***************************/ // sockaddr_storage fns. -// Standard bind, on OSX anyway will not accept a longer socklen for IPv4. -// so hidding details behind function. -int universal_bind(int fd, const struct sockaddr *addr, socklen_t socklen); +int rs_bind(int fd, const sockaddr_storage& addr); void sockaddr_storage_clear(struct sockaddr_storage &addr); // mods. bool sockaddr_storage_zeroip(struct sockaddr_storage &addr); + +/** + * @brief Use this function to copy sockaddr_storage. + * + * POSIX does not require that objects of type sockaddr_storage can be copied + * as aggregates thus it is unsafe to aggregate copy ( operator = ) + * sockaddr_storage and unexpected behaviors may happens due to padding + * and alignment. + * + * @see https://sourceware.org/bugzilla/show_bug.cgi?id=20111 + * @see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71120 + */ +bool sockaddr_storage_copy(const sockaddr_storage& src, sockaddr_storage& dst); + bool sockaddr_storage_copyip(struct sockaddr_storage &dst, const struct sockaddr_storage &src); uint16_t sockaddr_storage_port(const struct sockaddr_storage &addr); bool sockaddr_storage_setport(struct sockaddr_storage &addr, uint16_t port); @@ -103,9 +124,14 @@ bool sockaddr_storage_setport(struct sockaddr_storage &addr, uint16_t port); bool sockaddr_storage_setipv4(struct sockaddr_storage &addr, const sockaddr_in *addr_ipv4); bool sockaddr_storage_setipv6(struct sockaddr_storage &addr, const sockaddr_in6 *addr_ipv6); +bool sockaddr_storage_inet_pton( sockaddr_storage &addr, + const std::string& ipStr ); bool sockaddr_storage_ipv4_aton(struct sockaddr_storage &addr, const char *name); + bool sockaddr_storage_ipv4_setport(struct sockaddr_storage &addr, const uint16_t port); +bool sockaddr_storage_ipv4_to_ipv6(sockaddr_storage &addr); +bool sockaddr_storage_ipv6_to_ipv4(sockaddr_storage &addr); // comparisons. bool operator<(const struct sockaddr_storage &a, const struct sockaddr_storage &b); @@ -131,8 +157,12 @@ bool sockaddr_storage_isValidNet(const struct sockaddr_storage &addr); bool sockaddr_storage_isLoopbackNet(const struct sockaddr_storage &addr); bool sockaddr_storage_isPrivateNet(const struct sockaddr_storage &addr); bool sockaddr_storage_isLinkLocalNet(const struct sockaddr_storage &addr); +bool sockaddr_storage_ipv6_isLinkLocalNet(const sockaddr_storage &addr); bool sockaddr_storage_isExternalNet(const struct sockaddr_storage &addr); -bool rs_inet_ntop(const sockaddr_storage &addr, std::string &dst); +bool sockaddr_storage_inet_ntop(const sockaddr_storage &addr, std::string &dst); + +int rs_setsockopt( int sockfd, int level, int optname, + const uint8_t *optval, uint32_t optlen ); #endif /* RS_UNIVERSAL_NETWORK_HEADER */ diff --git a/libretroshare/src/util/rsnet_ss.cc b/libretroshare/src/util/rsnet_ss.cc index 1d94aa882..581fd06e9 100644 --- a/libretroshare/src/util/rsnet_ss.cc +++ b/libretroshare/src/util/rsnet_ss.cc @@ -26,6 +26,24 @@ #include #include +#include + +#ifdef WINDOWS_SYS +# include +/** Provides Linux like accessor for in6_addr.s6_addr16 for Windows. + * Yet Windows doesn't provide 32 bits accessors so there is no way to use + * in6_addr.s6_addr32 crossplatform. + */ +# define s6_addr16 u.Word +#else +# include +# include +# include +# ifdef __APPLE__ +/// Provides Linux like accessor for in6_addr.s6_addr16 for Mac. +# define s6_addr16 __u6_addr.__u6_addr16 +#endif // __APPLE__ +#endif // WINDOWS_SYS #include "util/rsnet.h" #include "util/rsstring.h" @@ -90,17 +108,14 @@ bool sockaddr_storage_ipv6_isExternalNet(const struct sockaddr_storage &addr); /******************************** Socket Fns ***********************************/ // Standard bind, on OSX anyway will not accept a longer socklen for IPv4. // so hidding details behind function. -int universal_bind(int fd, const struct sockaddr *addr, socklen_t socklen) +int rs_bind(int fd, const sockaddr_storage& addr) { #ifdef SS_DEBUG - std::cerr << "universal_bind()"; - std::cerr << std::endl; + std::cerr << __PRETTY_FUNCTION__ << std::endl; #endif - const struct sockaddr_storage *ss_addr = (struct sockaddr_storage *) addr; - socklen_t len = socklen; - - switch (ss_addr->ss_family) + socklen_t len = 0; + switch (addr.ss_family) { case AF_INET: len = sizeof(struct sockaddr_in); @@ -110,13 +125,7 @@ int universal_bind(int fd, const struct sockaddr *addr, socklen_t socklen) break; } - if (len > socklen) - { - std::cerr << "universal_bind() ERROR len > socklen" << std::endl; - len = socklen; - } - - return bind(fd, addr, len); + return bind(fd, reinterpret_cast(&addr), len); } @@ -252,6 +261,58 @@ bool sockaddr_storage_setipv6(struct sockaddr_storage &addr, const sockaddr_in6 return true; } +#ifdef WINDOWS_SYS +int inet_pton(int af, const char *src, void *dst) +{ + sockaddr_storage ss; + int size = sizeof(ss); + char src_copy[INET6_ADDRSTRLEN+1]; + + ZeroMemory(&ss, sizeof(ss)); + /* stupid non-const API */ + strncpy (src_copy, src, INET6_ADDRSTRLEN+1); + src_copy[INET6_ADDRSTRLEN] = 0; + + if (WSAStringToAddressA(src_copy, af, NULL, (sockaddr *)&ss, &size) == 0) + { + switch(af) + { + case AF_INET: + *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr; + return 1; + case AF_INET6: + *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr; + return 1; + } + } + return 0; +} +#endif + +bool sockaddr_storage_inet_pton( sockaddr_storage &addr, + const std::string& ipStr ) +{ +#ifdef SS_DEBUG + std::cerr << __PRETTY_FUNCTION__ << std::endl; +#endif + + struct sockaddr_in6 * addrv6p = (struct sockaddr_in6 *) &addr; + struct sockaddr_in * addrv4p = (struct sockaddr_in *) &addr; + + if ( 1 == inet_pton(AF_INET6, ipStr.c_str(), &(addrv6p->sin6_addr)) ) + { + addr.ss_family = AF_INET6; + return true; + } + else if ( 1 == inet_pton(AF_INET, ipStr.c_str(), &(addrv4p->sin_addr)) ) + { + addr.ss_family = AF_INET; + return sockaddr_storage_ipv4_to_ipv6(addr); + } + + return false; +} + bool sockaddr_storage_ipv4_aton(struct sockaddr_storage &addr, const char *name) { @@ -265,6 +326,71 @@ bool sockaddr_storage_ipv4_aton(struct sockaddr_storage &addr, const char *name) return (1 == inet_aton(name, &(ipv4_ptr->sin_addr))); } +bool sockaddr_storage_ipv4_to_ipv6(sockaddr_storage &addr) +{ +#ifdef SS_DEBUG + std::cerr << __PRETTY_FUNCTION__ << std::endl; +#endif + + if ( addr.ss_family == AF_INET6 ) return true; + + if ( addr.ss_family == AF_INET ) + { + sockaddr_in & addr_ipv4 = (sockaddr_in &) addr; + sockaddr_in6 & addr_ipv6 = (sockaddr_in6 &) addr; + + uint32_t ip = addr_ipv4.sin_addr.s_addr; + uint16_t port = addr_ipv4.sin_port; + + sockaddr_storage_clear(addr); + addr_ipv6.sin6_family = AF_INET6; + addr_ipv6.sin6_port = port; + addr_ipv6.sin6_addr.s6_addr16[5] = htons(0xffff); + memmove( reinterpret_cast(&(addr_ipv6.sin6_addr.s6_addr16[6])), + reinterpret_cast(&ip), 4 ); + return true; + } + + return false; +} + +bool sockaddr_storage_ipv6_to_ipv4(sockaddr_storage &addr) +{ +#ifdef SS_DEBUG + std::cerr << __PRETTY_FUNCTION__ << std::endl; +#endif + + if ( addr.ss_family == AF_INET ) return true; + + if ( addr.ss_family == AF_INET6 ) + { + sockaddr_in6 & addr_ipv6 = (sockaddr_in6 &) addr; + bool ipv4m = addr_ipv6.sin6_addr.s6_addr16[5] == htons(0xffff); + for ( int i = 0; ipv4m && i < 5 ; ++i ) + ipv4m &= addr_ipv6.sin6_addr.s6_addr16[i] == htons(0x0000); + + if(ipv4m) + { + uint32_t ip; + memmove( reinterpret_cast(&ip), + reinterpret_cast(&(addr_ipv6.sin6_addr.s6_addr16[6])), + 4 ); + uint16_t port = addr_ipv6.sin6_port; + + sockaddr_in & addr_ipv4 = (sockaddr_in &) addr; + + sockaddr_storage_clear(addr); + addr_ipv4.sin_family = AF_INET; + addr_ipv4.sin_port = port; + addr_ipv4.sin_addr.s_addr = ip; + + return true; + } + } + + return false; +} + /******************************** Comparisions **********************************/ @@ -370,15 +496,20 @@ std::string sockaddr_storage_tostring(const struct sockaddr_storage &addr) switch(addr.ss_family) { - case AF_INET: - case AF_INET6: - output += "="; - output += sockaddr_storage_iptostring(addr); - output += ":"; - output += sockaddr_storage_porttostring(addr); - break; - default: - break; + case AF_INET: + output += "="; + output += sockaddr_storage_iptostring(addr); + output += ":"; + output += sockaddr_storage_porttostring(addr); + break; + case AF_INET6: + output += "=["; + output += sockaddr_storage_iptostring(addr); + output += "]:"; + output += sockaddr_storage_porttostring(addr); + break; + default: + break; } return output; } @@ -406,7 +537,7 @@ void sockaddr_storage_dump(const sockaddr_storage & addr, std::string * outputSt { const sockaddr_in6 * in6 = (const sockaddr_in6 *) & addr; std::string addrStr = "INVALID_IPV6"; - rs_inet_ntop(addr, addrStr); + sockaddr_storage_inet_ntop(addr, addrStr); output << "addr.ss_family = AF_INET6"; output << " in6->sin6_addr = "; output << addrStr; @@ -422,7 +553,8 @@ void sockaddr_storage_dump(const sockaddr_storage & addr, std::string * outputSt const uint8_t * buf = reinterpret_cast(&addr); for( uint32_t i = 0; i < sizeof(addr); ++i ) output << std::setw(2) << std::setfill('0') << std::hex << +buf[i]; - // The unary +buf[i] operation forces a no-op type conversion to an int with the correct sign + /* The unary +buf[i] operation forces a no-op type conversion to an int + * with the correct sign */ } } @@ -450,6 +582,12 @@ std::string sockaddr_storage_familytostring(const struct sockaddr_storage &addr) break; default: output = "AF_INVALID"; +#ifdef SS_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " Got invalid address!" + << std::endl; + sockaddr_storage_dump(addr); + print_stacktrace(); +#endif break; } return output; @@ -460,15 +598,18 @@ std::string sockaddr_storage_iptostring(const struct sockaddr_storage &addr) std::string output; switch(addr.ss_family) { - case AF_INET: - output = sockaddr_storage_ipv4_iptostring(addr); - break; - case AF_INET6: - output = sockaddr_storage_ipv6_iptostring(addr); - break; - default: - output = "INVALID_IP"; - break; + case AF_INET: + output = sockaddr_storage_ipv4_iptostring(addr); + break; + case AF_INET6: + output = sockaddr_storage_ipv6_iptostring(addr); + break; + default: + output = "INVALID_IP"; + std::cerr << __PRETTY_FUNCTION__ << " Got invalid IP:" << std::endl; + sockaddr_storage_dump(addr); + print_stacktrace(); + break; } return output; } @@ -592,9 +733,7 @@ bool sockaddr_storage_isLinkLocalNet(const struct sockaddr_storage &addr) case AF_INET: return isLinkLocalNet(&(to_const_ipv4_ptr(addr)->sin_addr)); case AF_INET6: - std::cerr << __PRETTY_FUNCTION__ << " for AF_INET6 not implemented" - << std::endl; - break; + return sockaddr_storage_ipv6_isLinkLocalNet(addr); default: #ifdef SS_DEBUG std::cerr << __PRETTY_FUNCTION__ <<" INVALID Family:" << std::endl; @@ -606,6 +745,16 @@ bool sockaddr_storage_isLinkLocalNet(const struct sockaddr_storage &addr) return false; } +bool sockaddr_storage_ipv6_isLinkLocalNet(const sockaddr_storage &addr) +{ + if(addr.ss_family != AF_INET6) return false; + + const sockaddr_in6 * addr6 = (const sockaddr_in6 *) &addr; + uint16_t mask = htons(0xffc0); + uint16_t llPrefix = htons(0xfe80); + return ((addr6->sin6_addr.s6_addr16[0] & mask ) == llPrefix); +} + bool sockaddr_storage_isExternalNet(const struct sockaddr_storage &addr) { @@ -668,6 +817,51 @@ const struct sockaddr_in6 *to_const_ipv6_ptr(const struct sockaddr_storage &addr /******************************** Set / Clear ***********************************/ +bool sockaddr_storage_copy(const sockaddr_storage& src, sockaddr_storage& dst) +{ + if(&src == &dst) return true; + + switch(src.ss_family) + { + case AF_INET: + { + sockaddr_storage_clear(dst); + const sockaddr_in& ins(reinterpret_cast(src)); + sockaddr_in& ind(reinterpret_cast(dst)); + + ind.sin_family = AF_INET; + ind.sin_addr.s_addr = ins.sin_addr.s_addr; + ind.sin_port = ins.sin_port; + + return true; + } + case AF_INET6: + { + sockaddr_storage_clear(dst); + const sockaddr_in6& ins6(reinterpret_cast(src)); + sockaddr_in6& ind6(reinterpret_cast(dst)); + + ind6.sin6_family = AF_INET6; + for(int i=0; i<8; ++i) + ind6.sin6_addr.s6_addr16[i] = ins6.sin6_addr.s6_addr16[i]; + ind6.sin6_flowinfo = ins6.sin6_flowinfo; + ind6.sin6_port = ins6.sin6_port; + ind6.sin6_scope_id = ins6.sin6_scope_id; + + return true; + } + default: +#ifdef SS_DEBUG + std::cerr << __PRETTY_FUNCTION__ << " Attempt to copy unknown family! " + << src.ss_family << " defaulting to memmove!" << std::endl; + sockaddr_storage_dump(src); + print_stacktrace(); +#endif // SS_DEBUG + memmove(&dst, &src, sizeof(sockaddr_storage)); + return true; + } +} + bool sockaddr_storage_ipv4_zeroip(struct sockaddr_storage &addr) { #ifdef SS_DEBUG @@ -890,7 +1084,7 @@ std::string sockaddr_storage_ipv4_iptostring(const struct sockaddr_storage &addr std::string sockaddr_storage_ipv6_iptostring(const struct sockaddr_storage & addr) { std::string addrStr; - rs_inet_ntop(addr, addrStr); + sockaddr_storage_inet_ntop(addr, addrStr); return addrStr; } @@ -979,62 +1173,58 @@ bool sockaddr_storage_ipv4_isExternalNet(const struct sockaddr_storage &addr) } -bool sockaddr_storage_ipv6_isnull(const struct sockaddr_storage &/*addr*/) +bool sockaddr_storage_ipv6_isnull(const struct sockaddr_storage& addr) { -#ifdef SS_DEBUG - std::cerr << "sockaddr_storage_ipv6_isnull() TODO"; - std::cerr << std::endl; -#endif + const sockaddr_in6& addr6 = reinterpret_cast(addr); - return false; + uint16_t nZero = htons(0); // anyway 0 should be the same in host and net + bool isZero = (addr6.sin6_addr.s6_addr16[7] == nZero); + for (int i=0; isZero && i<7; ++i) + isZero &= (addr6.sin6_addr.s6_addr16[i] == nZero); + + return nZero; } -bool sockaddr_storage_ipv6_isValidNet(const struct sockaddr_storage &/*addr*/) +bool sockaddr_storage_ipv6_isValidNet(const struct sockaddr_storage& addr) { -#ifdef SS_DEBUG - std::cerr << "sockaddr_storage_ipv6_isValidNet() TODO"; - std::cerr << std::endl; -#endif - - return false; + return !sockaddr_storage_ipv6_isnull(addr); } -bool sockaddr_storage_ipv6_isLoopbackNet(const struct sockaddr_storage &/*addr*/) +bool sockaddr_storage_ipv6_isLoopbackNet(const struct sockaddr_storage& addr) { + const sockaddr_in6& addr6 = reinterpret_cast(addr); + bool isLoopBack = (addr6.sin6_addr.s6_addr16[7] == htons(0x0001)); + uint16_t nZero = htons(0); // anyway 0 should be the same in host and net + for (int i=0; isLoopBack && i<7; ++i) + isLoopBack &= (addr6.sin6_addr.s6_addr16[i] == nZero); + #ifdef SS_DEBUG - std::cerr << "sockaddr_storage_ipv6_isLoopbackNet() TODO"; - std::cerr << std::endl; + std::cerr << __PRETTY_FUNCTION__ << " " << sockaddr_storage_tostring(addr) + << " " << isLoopBack << std::endl; #endif - return false; + return isLoopBack; } bool sockaddr_storage_ipv6_isPrivateNet(const struct sockaddr_storage &/*addr*/) { -#ifdef SS_DEBUG - std::cerr << "sockaddr_storage_ipv6_isPrivateNet() TODO"; - std::cerr << std::endl; -#endif + /* It is unlikely that we end up connecting to an IPv6 address behind NAT + * W.R.T. RS it is probably better to consider all IPv6 as internal/local + * addresses as direct connection should be always possible. */ - return false; + return true; } bool sockaddr_storage_ipv6_isExternalNet(const struct sockaddr_storage &/*addr*/) { -#ifdef SS_DEBUG - std::cerr << "sockaddr_storage_ipv6_isExternalNet() TODO"; - std::cerr << std::endl; -#endif + /* It is unlikely that we end up connecting to an IPv6 address behind NAT + * W.R.T. RS it is probably better to consider all IPv6 as internal/local + * addresses as direct connection should be always possible. */ return false; } -#ifdef WINDOWS_SYS -#include -#include -#endif - -bool rs_inet_ntop (const sockaddr_storage &addr, std::string &dst) +bool sockaddr_storage_inet_ntop (const sockaddr_storage &addr, std::string &dst) { bool success = false; char ipStr[255]; @@ -1071,3 +1261,15 @@ bool rs_inet_ntop (const sockaddr_storage &addr, std::string &dst) dst = ipStr; return success; } + +int rs_setsockopt( int sockfd, int level, int optname, + const uint8_t *optval, uint32_t optlen ) +{ +#ifdef WINDOWS_SYS + return setsockopt( sockfd, level, optname, + reinterpret_cast(optval), optlen ); +#else + return setsockopt( sockfd, level, optname, + reinterpret_cast(optval), optlen ); +#endif // WINDOWS_SYS +} diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index 39eca594a..65fcad4b7 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -169,11 +169,14 @@ void RsTickingThread::fullstop() void RsThread::start(const std::string &threadName) { - if(isRunning()) - { - std::cerr << "(EE) RsThread \"" << threadName << "\" is already running. Will not start twice!" << std::endl; - return ; - } + if(isRunning()) + { + std::cerr << "(EE) RsThread \"" << threadName + << "\" is already running. Will not start twice!" + << std::endl; + print_stacktrace(); + return; + } pthread_t tid; void *data = (void *)this ; diff --git a/openpgpsdk/src/openpgpsdk.pro b/openpgpsdk/src/openpgpsdk.pro index eaf4ef2e6..f667a0d23 100644 --- a/openpgpsdk/src/openpgpsdk.pro +++ b/openpgpsdk/src/openpgpsdk.pro @@ -9,8 +9,8 @@ QMAKE_CXXFLAGS *= -Wall -Werror -W TARGET = ops DESTDIR = lib -DEPENDPATH += . $$INC_DIR -INCLUDEPATH += . $$INC_DIR + +!include(use_openpgpsdk.pri):error("Including") #################################### Windows ##################################### @@ -18,7 +18,11 @@ linux-* { OBJECTS_DIR = temp/linux/obj } -win32 { +win32-g++ { + + HEADERS += openpgpsdk/opsstring.h + SOURCES += openpgpsdk/opsstring.c + DEFINES *= WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T # Switch off optimization for release version @@ -27,9 +31,13 @@ win32 { QMAKE_CFLAGS_RELEASE -= -O2 QMAKE_CFLAGS_RELEASE += -O0 - # Switch on optimization for debug version - #QMAKE_CXXFLAGS_DEBUG += -O2 - #QMAKE_CFLAGS_DEBUG += -O2 + mLibs = bz2 z ssl crypto + static { + LIBS += $$linkStaticLibs(mLibs) + PRE_TARGETDEPS += $$pretargetStaticLibs(mLibs) + } else { + LIBS += $$linkDynamicLibs(mLibs) + } } @@ -74,9 +82,6 @@ HEADERS += openpgpsdk/writer.h \ openpgpsdk/parse_local.h \ openpgpsdk/keyring_local.h \ openpgpsdk/opsdir.h -win32{ -HEADERS += openpgpsdk/opsstring.h -} SOURCES += openpgpsdk/accumulate.c \ openpgpsdk/compress.c \ @@ -116,9 +121,7 @@ SOURCES += openpgpsdk/accumulate.c \ openpgpsdk/writer_skey_checksum.c \ openpgpsdk/writer_stream_encrypt_se_ip.c \ openpgpsdk/opsdir.c -win32{ -SOURCES += openpgpsdk/opsstring.c -} + ################################# Android ##################################### diff --git a/openpgpsdk/src/use_openpgpsdk.pri b/openpgpsdk/src/use_openpgpsdk.pri new file mode 100644 index 000000000..adf10f1ca --- /dev/null +++ b/openpgpsdk/src/use_openpgpsdk.pri @@ -0,0 +1,20 @@ +DEPENDPATH *= $$system_path($$clean_path($${PWD}/../../openpgpsdk/src)) +INCLUDEPATH *= $$system_path($$clean_path($${PWD}/../../openpgpsdk/src)) +LIBS *= -L$$system_path($$clean_path($${OUT_PWD}/../../openpgpsdk/src/lib/)) -lops + +!equals(TARGET, ops):PRE_TARGETDEPS *= $$system_path($$clean_path($${OUT_PWD}/../../openpgpsdk/src/lib/libops.a)) + +sLibs = +mLibs = ssl crypto z bz2 +dLibs = + +static { + sLibs *= $$mLibs +} else { + dLibs *= $$mLibs +} + +LIBS += $$linkStaticLibs(sLibs) +PRE_TARGETDEPS += $$pretargetStaticLibs(sLibs) + +LIBS += $$linkDynamicLibs(dLibs) diff --git a/plugins/FeedReader/FeedReader.pro b/plugins/FeedReader/FeedReader.pro index 7e7e40fdd..c60348551 100644 --- a/plugins/FeedReader/FeedReader.pro +++ b/plugins/FeedReader/FeedReader.pro @@ -80,6 +80,12 @@ TRANSLATIONS += \ lang/FeedReader_tr.ts \ lang/FeedReader_zh_CN.ts +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 + + linux-* { CONFIG += link_pkgconfig diff --git a/plugins/VOIP/VOIP.pro b/plugins/VOIP/VOIP.pro index ccf7f00f9..a1102b78e 100644 --- a/plugins/VOIP/VOIP.pro +++ b/plugins/VOIP/VOIP.pro @@ -16,6 +16,12 @@ MOBILITY = multimedia DEPENDPATH += $$PWD/../../retroshare-gui/src/temp/ui INCLUDEPATH += $$PWD/../../retroshare-gui/src/temp/ui +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 + + #################################### Linux ##################################### linux-* { @@ -37,7 +43,7 @@ win32 { OPENCV_VERSION = "341" USE_PRECOMPILED_LIBS = - for(lib, LIB_DIR) { + for(lib, RS_LIB_DIR) { #message(Scanning $$lib) exists( $$lib/opencv/libopencv_core$${OPENCV_VERSION}.a) { isEmpty(USE_PRECOMPILED_LIBS) { diff --git a/rapidjson-1.1.0/license.txt b/rapidjson-1.1.0/license.txt new file mode 100644 index 000000000..7ccc161c8 --- /dev/null +++ b/rapidjson-1.1.0/license.txt @@ -0,0 +1,57 @@ +Tencent is pleased to support the open source community by making RapidJSON available. + +Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. + +If you have downloaded a copy of the RapidJSON binary from Tencent, please note that the RapidJSON binary is licensed under the MIT License. +If you have downloaded a copy of the RapidJSON source code from Tencent, please note that RapidJSON source code is licensed under the MIT License, except for the third-party components listed below which are subject to different license terms. Your integration of RapidJSON into your own projects may require compliance with the MIT License, as well as the other licenses applicable to the third-party components included within RapidJSON. To avoid the problematic JSON license in your own projects, it's sufficient to exclude the bin/jsonchecker/ directory, as it's the only code under the JSON license. +A copy of the MIT License is included in this file. + +Other dependencies and licenses: + +Open Source Software Licensed Under the BSD License: +-------------------------------------------------------------------- + +The msinttypes r29 +Copyright (c) 2006-2013 Alexander Chemeris +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +* Neither the name of copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Open Source Software Licensed Under the JSON License: +-------------------------------------------------------------------- + +json.org +Copyright (c) 2002 JSON.org +All Rights Reserved. + +JSON_checker +Copyright (c) 2002 JSON.org +All Rights Reserved. + + +Terms of the JSON License: +--------------------------------------------------- + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +The Software shall be used for Good, not Evil. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +Terms of the MIT License: +-------------------------------------------------------------------- + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/rapidjson-1.1.0/rapid_json/allocators.h b/rapidjson-1.1.0/rapid_json/allocators.h new file mode 100644 index 000000000..98affe03f --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/allocators.h @@ -0,0 +1,271 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ALLOCATORS_H_ +#define RAPIDJSON_ALLOCATORS_H_ + +#include "rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Allocator + +/*! \class rapidjson::Allocator + \brief Concept for allocating, resizing and freeing memory block. + + Note that Malloc() and Realloc() are non-static but Free() is static. + + So if an allocator need to support Free(), it needs to put its pointer in + the header of memory block. + +\code +concept Allocator { + static const bool kNeedFree; //!< Whether this allocator needs to call Free(). + + // Allocate a memory block. + // \param size of the memory block in bytes. + // \returns pointer to the memory block. + void* Malloc(size_t size); + + // Resize a memory block. + // \param originalPtr The pointer to current memory block. Null pointer is permitted. + // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.) + // \param newSize the new size in bytes. + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); + + // Free a memory block. + // \param pointer to the memory block. Null pointer is permitted. + static void Free(void *ptr); +}; +\endcode +*/ + +/////////////////////////////////////////////////////////////////////////////// +// CrtAllocator + +//! C-runtime library allocator. +/*! This class is just wrapper for standard C library memory routines. + \note implements Allocator concept +*/ +class CrtAllocator { +public: + static const bool kNeedFree = true; + void* Malloc(size_t size) { + if (size) // behavior of malloc(0) is implementation defined. + return std::malloc(size); + else + return NULL; // standardize to returning NULL. + } + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { + (void)originalSize; + if (newSize == 0) { + std::free(originalPtr); + return NULL; + } + return std::realloc(originalPtr, newSize); + } + static void Free(void *ptr) { std::free(ptr); } +}; + +/////////////////////////////////////////////////////////////////////////////// +// MemoryPoolAllocator + +//! Default memory allocator used by the parser and DOM. +/*! This allocator allocate memory blocks from pre-allocated memory chunks. + + It does not free memory blocks. And Realloc() only allocate new memory. + + The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default. + + User may also supply a buffer as the first chunk. + + If the user-buffer is full then additional chunks are allocated by BaseAllocator. + + The user-buffer is not deallocated by this allocator. + + \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator. + \note implements Allocator concept +*/ +template +class MemoryPoolAllocator { +public: + static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator) + + //! Constructor with chunkSize. + /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. + \param baseAllocator The allocator for allocating memory chunks. + */ + MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : + chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) + { + } + + //! Constructor with user-supplied buffer. + /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size. + + The user buffer will not be deallocated when this allocator is destructed. + + \param buffer User supplied buffer. + \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader). + \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. + \param baseAllocator The allocator for allocating memory chunks. + */ + MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : + chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0) + { + RAPIDJSON_ASSERT(buffer != 0); + RAPIDJSON_ASSERT(size > sizeof(ChunkHeader)); + chunkHead_ = reinterpret_cast(buffer); + chunkHead_->capacity = size - sizeof(ChunkHeader); + chunkHead_->size = 0; + chunkHead_->next = 0; + } + + //! Destructor. + /*! This deallocates all memory chunks, excluding the user-supplied buffer. + */ + ~MemoryPoolAllocator() { + Clear(); + RAPIDJSON_DELETE(ownBaseAllocator_); + } + + //! Deallocates all memory chunks, excluding the user-supplied buffer. + void Clear() { + while (chunkHead_ && chunkHead_ != userBuffer_) { + ChunkHeader* next = chunkHead_->next; + baseAllocator_->Free(chunkHead_); + chunkHead_ = next; + } + if (chunkHead_ && chunkHead_ == userBuffer_) + chunkHead_->size = 0; // Clear user buffer + } + + //! Computes the total capacity of allocated memory chunks. + /*! \return total capacity in bytes. + */ + size_t Capacity() const { + size_t capacity = 0; + for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) + capacity += c->capacity; + return capacity; + } + + //! Computes the memory blocks allocated. + /*! \return total used bytes. + */ + size_t Size() const { + size_t size = 0; + for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) + size += c->size; + return size; + } + + //! Allocates a memory block. (concept Allocator) + void* Malloc(size_t size) { + if (!size) + return NULL; + + size = RAPIDJSON_ALIGN(size); + if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity) + if (!AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size)) + return NULL; + + void *buffer = reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size; + chunkHead_->size += size; + return buffer; + } + + //! Resizes a memory block (concept Allocator) + void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { + if (originalPtr == 0) + return Malloc(newSize); + + if (newSize == 0) + return NULL; + + originalSize = RAPIDJSON_ALIGN(originalSize); + newSize = RAPIDJSON_ALIGN(newSize); + + // Do not shrink if new size is smaller than original + if (originalSize >= newSize) + return originalPtr; + + // Simply expand it if it is the last allocation and there is sufficient space + if (originalPtr == reinterpret_cast(chunkHead_) + RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + chunkHead_->size - originalSize) { + size_t increment = static_cast(newSize - originalSize); + if (chunkHead_->size + increment <= chunkHead_->capacity) { + chunkHead_->size += increment; + return originalPtr; + } + } + + // Realloc process: allocate and copy memory, do not free original buffer. + if (void* newBuffer = Malloc(newSize)) { + if (originalSize) + std::memcpy(newBuffer, originalPtr, originalSize); + return newBuffer; + } + else + return NULL; + } + + //! Frees a memory block (concept Allocator) + static void Free(void *ptr) { (void)ptr; } // Do nothing + +private: + //! Copy constructor is not permitted. + MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */; + //! Copy assignment operator is not permitted. + MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */; + + //! Creates a new chunk. + /*! \param capacity Capacity of the chunk in bytes. + \return true if success. + */ + bool AddChunk(size_t capacity) { + if (!baseAllocator_) + ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); + if (ChunkHeader* chunk = reinterpret_cast(baseAllocator_->Malloc(RAPIDJSON_ALIGN(sizeof(ChunkHeader)) + capacity))) { + chunk->capacity = capacity; + chunk->size = 0; + chunk->next = chunkHead_; + chunkHead_ = chunk; + return true; + } + else + return false; + } + + static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity. + + //! Chunk header for perpending to each chunk. + /*! Chunks are stored as a singly linked list. + */ + struct ChunkHeader { + size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself). + size_t size; //!< Current size of allocated memory in bytes. + ChunkHeader *next; //!< Next chunk in the linked list. + }; + + ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation. + size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated. + void *userBuffer_; //!< User supplied buffer. + BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks. + BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object. +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ENCODINGS_H_ diff --git a/rapidjson-1.1.0/rapid_json/document.h b/rapidjson-1.1.0/rapid_json/document.h new file mode 100644 index 000000000..e3e20dfbd --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/document.h @@ -0,0 +1,2575 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_DOCUMENT_H_ +#define RAPIDJSON_DOCUMENT_H_ + +/*! \file document.h */ + +#include "reader.h" +#include "internal/meta.h" +#include "internal/strfunc.h" +#include "memorystream.h" +#include "encodedstream.h" +#include // placement new +#include + +RAPIDJSON_DIAG_PUSH +#ifdef _MSC_VER +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_OFF(effc++) +#if __GNUC__ >= 6 +RAPIDJSON_DIAG_OFF(terminate) // ignore throwing RAPIDJSON_ASSERT in RAPIDJSON_NOEXCEPT functions +#endif +#endif // __GNUC__ + +#ifndef RAPIDJSON_NOMEMBERITERATORCLASS +#include // std::iterator, std::random_access_iterator_tag +#endif + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +#include // std::move +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +// Forward declaration. +template +class GenericValue; + +template +class GenericDocument; + +//! Name-value pair in a JSON object value. +/*! + This class was internal to GenericValue. It used to be a inner struct. + But a compiler (IBM XL C/C++ for AIX) have reported to have problem with that so it moved as a namespace scope struct. + https://code.google.com/p/rapidjson/issues/detail?id=64 +*/ +template +struct GenericMember { + GenericValue name; //!< name of member (must be a string) + GenericValue value; //!< value of member. +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericMemberIterator + +#ifndef RAPIDJSON_NOMEMBERITERATORCLASS + +//! (Constant) member iterator for a JSON object value +/*! + \tparam Const Is this a constant iterator? + \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) + \tparam Allocator Allocator type for allocating memory of object, array and string. + + This class implements a Random Access Iterator for GenericMember elements + of a GenericValue, see ISO/IEC 14882:2003(E) C++ standard, 24.1 [lib.iterator.requirements]. + + \note This iterator implementation is mainly intended to avoid implicit + conversions from iterator values to \c NULL, + e.g. from GenericValue::FindMember. + + \note Define \c RAPIDJSON_NOMEMBERITERATORCLASS to fall back to a + pointer-based implementation, if your platform doesn't provide + the C++ header. + + \see GenericMember, GenericValue::MemberIterator, GenericValue::ConstMemberIterator + */ +template +class GenericMemberIterator + : public std::iterator >::Type> { + + friend class GenericValue; + template friend class GenericMemberIterator; + + typedef GenericMember PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef std::iterator BaseType; + +public: + //! Iterator type itself + typedef GenericMemberIterator Iterator; + //! Constant iterator type + typedef GenericMemberIterator ConstIterator; + //! Non-constant iterator type + typedef GenericMemberIterator NonConstIterator; + + //! Pointer to (const) GenericMember + typedef typename BaseType::pointer Pointer; + //! Reference to (const) GenericMember + typedef typename BaseType::reference Reference; + //! Signed integer type (e.g. \c ptrdiff_t) + typedef typename BaseType::difference_type DifferenceType; + + //! Default constructor (singular value) + /*! Creates an iterator pointing to no element. + \note All operations, except for comparisons, are undefined on such values. + */ + GenericMemberIterator() : ptr_() {} + + //! Iterator conversions to more const + /*! + \param it (Non-const) iterator to copy from + + Allows the creation of an iterator from another GenericMemberIterator + that is "less const". Especially, creating a non-constant iterator + from a constant iterator are disabled: + \li const -> non-const (not ok) + \li const -> const (ok) + \li non-const -> const (ok) + \li non-const -> non-const (ok) + + \note If the \c Const template parameter is already \c false, this + constructor effectively defines a regular copy-constructor. + Otherwise, the copy constructor is implicitly defined. + */ + GenericMemberIterator(const NonConstIterator & it) : ptr_(it.ptr_) {} + Iterator& operator=(const NonConstIterator & it) { ptr_ = it.ptr_; return *this; } + + //! @name stepping + //@{ + Iterator& operator++(){ ++ptr_; return *this; } + Iterator& operator--(){ --ptr_; return *this; } + Iterator operator++(int){ Iterator old(*this); ++ptr_; return old; } + Iterator operator--(int){ Iterator old(*this); --ptr_; return old; } + //@} + + //! @name increment/decrement + //@{ + Iterator operator+(DifferenceType n) const { return Iterator(ptr_+n); } + Iterator operator-(DifferenceType n) const { return Iterator(ptr_-n); } + + Iterator& operator+=(DifferenceType n) { ptr_+=n; return *this; } + Iterator& operator-=(DifferenceType n) { ptr_-=n; return *this; } + //@} + + //! @name relations + //@{ + bool operator==(ConstIterator that) const { return ptr_ == that.ptr_; } + bool operator!=(ConstIterator that) const { return ptr_ != that.ptr_; } + bool operator<=(ConstIterator that) const { return ptr_ <= that.ptr_; } + bool operator>=(ConstIterator that) const { return ptr_ >= that.ptr_; } + bool operator< (ConstIterator that) const { return ptr_ < that.ptr_; } + bool operator> (ConstIterator that) const { return ptr_ > that.ptr_; } + //@} + + //! @name dereference + //@{ + Reference operator*() const { return *ptr_; } + Pointer operator->() const { return ptr_; } + Reference operator[](DifferenceType n) const { return ptr_[n]; } + //@} + + //! Distance + DifferenceType operator-(ConstIterator that) const { return ptr_-that.ptr_; } + +private: + //! Internal constructor from plain pointer + explicit GenericMemberIterator(Pointer p) : ptr_(p) {} + + Pointer ptr_; //!< raw pointer +}; + +#else // RAPIDJSON_NOMEMBERITERATORCLASS + +// class-based member iterator implementation disabled, use plain pointers + +template +struct GenericMemberIterator; + +//! non-const GenericMemberIterator +template +struct GenericMemberIterator { + //! use plain pointer as iterator type + typedef GenericMember* Iterator; +}; +//! const GenericMemberIterator +template +struct GenericMemberIterator { + //! use plain const pointer as iterator type + typedef const GenericMember* Iterator; +}; + +#endif // RAPIDJSON_NOMEMBERITERATORCLASS + +/////////////////////////////////////////////////////////////////////////////// +// GenericStringRef + +//! Reference to a constant string (not taking a copy) +/*! + \tparam CharType character type of the string + + This helper class is used to automatically infer constant string + references for string literals, especially from \c const \b (!) + character arrays. + + The main use is for creating JSON string values without copying the + source string via an \ref Allocator. This requires that the referenced + string pointers have a sufficient lifetime, which exceeds the lifetime + of the associated GenericValue. + + \b Example + \code + Value v("foo"); // ok, no need to copy & calculate length + const char foo[] = "foo"; + v.SetString(foo); // ok + + const char* bar = foo; + // Value x(bar); // not ok, can't rely on bar's lifetime + Value x(StringRef(bar)); // lifetime explicitly guaranteed by user + Value y(StringRef(bar, 3)); // ok, explicitly pass length + \endcode + + \see StringRef, GenericValue::SetString +*/ +template +struct GenericStringRef { + typedef CharType Ch; //!< character type of the string + + //! Create string reference from \c const character array +#ifndef __clang__ // -Wdocumentation + /*! + This constructor implicitly creates a constant string reference from + a \c const character array. It has better performance than + \ref StringRef(const CharType*) by inferring the string \ref length + from the array length, and also supports strings containing null + characters. + + \tparam N length of the string, automatically inferred + + \param str Constant character array, lifetime assumed to be longer + than the use of the string in e.g. a GenericValue + + \post \ref s == str + + \note Constant complexity. + \note There is a hidden, private overload to disallow references to + non-const character arrays to be created via this constructor. + By this, e.g. function-scope arrays used to be filled via + \c snprintf are excluded from consideration. + In such cases, the referenced string should be \b copied to the + GenericValue instead. + */ +#endif + template + GenericStringRef(const CharType (&str)[N]) RAPIDJSON_NOEXCEPT + : s(str), length(N-1) {} + + //! Explicitly create string reference from \c const character pointer +#ifndef __clang__ // -Wdocumentation + /*! + This constructor can be used to \b explicitly create a reference to + a constant string pointer. + + \see StringRef(const CharType*) + + \param str Constant character pointer, lifetime assumed to be longer + than the use of the string in e.g. a GenericValue + + \post \ref s == str + + \note There is a hidden, private overload to disallow references to + non-const character arrays to be created via this constructor. + By this, e.g. function-scope arrays used to be filled via + \c snprintf are excluded from consideration. + In such cases, the referenced string should be \b copied to the + GenericValue instead. + */ +#endif + explicit GenericStringRef(const CharType* str) + : s(str), length(internal::StrLen(str)){ RAPIDJSON_ASSERT(s != 0); } + + //! Create constant string reference from pointer and length +#ifndef __clang__ // -Wdocumentation + /*! \param str constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \param len length of the string, excluding the trailing NULL terminator + + \post \ref s == str && \ref length == len + \note Constant complexity. + */ +#endif + GenericStringRef(const CharType* str, SizeType len) + : s(str), length(len) { RAPIDJSON_ASSERT(s != 0); } + + GenericStringRef(const GenericStringRef& rhs) : s(rhs.s), length(rhs.length) {} + + GenericStringRef& operator=(const GenericStringRef& rhs) { s = rhs.s; length = rhs.length; } + + //! implicit conversion to plain CharType pointer + operator const Ch *() const { return s; } + + const Ch* const s; //!< plain CharType pointer + const SizeType length; //!< length of the string (excluding the trailing NULL terminator) + +private: + //! Disallow construction from non-const array + template + GenericStringRef(CharType (&str)[N]) /* = delete */; +}; + +//! Mark a character pointer as constant string +/*! Mark a plain character pointer as a "string literal". This function + can be used to avoid copying a character string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + \tparam CharType Character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \return GenericStringRef string reference object + \relatesalso GenericStringRef + + \see GenericValue::GenericValue(StringRefType), GenericValue::operator=(StringRefType), GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType, Allocator&), GenericValue::AddMember +*/ +template +inline GenericStringRef StringRef(const CharType* str) { + return GenericStringRef(str, internal::StrLen(str)); +} + +//! Mark a character pointer as constant string +/*! Mark a plain character pointer as a "string literal". This function + can be used to avoid copying a character string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + + This version has better performance with supplied length, and also + supports string containing null characters. + + \tparam CharType character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \param length The length of source string. + \return GenericStringRef string reference object + \relatesalso GenericStringRef +*/ +template +inline GenericStringRef StringRef(const CharType* str, size_t length) { + return GenericStringRef(str, SizeType(length)); +} + +#if RAPIDJSON_HAS_STDSTRING +//! Mark a string object as constant string +/*! Mark a string object (e.g. \c std::string) as a "string literal". + This function can be used to avoid copying a string to be referenced as a + value in a JSON GenericValue object, if the string's lifetime is known + to be valid long enough. + + \tparam CharType character type of the string + \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue + \return GenericStringRef string reference object + \relatesalso GenericStringRef + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. +*/ +template +inline GenericStringRef StringRef(const std::basic_string& str) { + return GenericStringRef(str.data(), SizeType(str.size())); +} +#endif + +/////////////////////////////////////////////////////////////////////////////// +// GenericValue type traits +namespace internal { + +template +struct IsGenericValueImpl : FalseType {}; + +// select candidates according to nested encoding and allocator types +template struct IsGenericValueImpl::Type, typename Void::Type> + : IsBaseOf, T>::Type {}; + +// helper to match arbitrary GenericValue instantiations, including derived classes +template struct IsGenericValue : IsGenericValueImpl::Type {}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// TypeHelper + +namespace internal { + +template +struct TypeHelper {}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsBool(); } + static bool Get(const ValueType& v) { return v.GetBool(); } + static ValueType& Set(ValueType& v, bool data) { return v.SetBool(data); } + static ValueType& Set(ValueType& v, bool data, typename ValueType::AllocatorType&) { return v.SetBool(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsInt(); } + static int Get(const ValueType& v) { return v.GetInt(); } + static ValueType& Set(ValueType& v, int data) { return v.SetInt(data); } + static ValueType& Set(ValueType& v, int data, typename ValueType::AllocatorType&) { return v.SetInt(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsUint(); } + static unsigned Get(const ValueType& v) { return v.GetUint(); } + static ValueType& Set(ValueType& v, unsigned data) { return v.SetUint(data); } + static ValueType& Set(ValueType& v, unsigned data, typename ValueType::AllocatorType&) { return v.SetUint(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsInt64(); } + static int64_t Get(const ValueType& v) { return v.GetInt64(); } + static ValueType& Set(ValueType& v, int64_t data) { return v.SetInt64(data); } + static ValueType& Set(ValueType& v, int64_t data, typename ValueType::AllocatorType&) { return v.SetInt64(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsUint64(); } + static uint64_t Get(const ValueType& v) { return v.GetUint64(); } + static ValueType& Set(ValueType& v, uint64_t data) { return v.SetUint64(data); } + static ValueType& Set(ValueType& v, uint64_t data, typename ValueType::AllocatorType&) { return v.SetUint64(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsDouble(); } + static double Get(const ValueType& v) { return v.GetDouble(); } + static ValueType& Set(ValueType& v, double data) { return v.SetDouble(data); } + static ValueType& Set(ValueType& v, double data, typename ValueType::AllocatorType&) { return v.SetDouble(data); } +}; + +template +struct TypeHelper { + static bool Is(const ValueType& v) { return v.IsFloat(); } + static float Get(const ValueType& v) { return v.GetFloat(); } + static ValueType& Set(ValueType& v, float data) { return v.SetFloat(data); } + static ValueType& Set(ValueType& v, float data, typename ValueType::AllocatorType&) { return v.SetFloat(data); } +}; + +template +struct TypeHelper { + typedef const typename ValueType::Ch* StringType; + static bool Is(const ValueType& v) { return v.IsString(); } + static StringType Get(const ValueType& v) { return v.GetString(); } + static ValueType& Set(ValueType& v, const StringType data) { return v.SetString(typename ValueType::StringRefType(data)); } + static ValueType& Set(ValueType& v, const StringType data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); } +}; + +#if RAPIDJSON_HAS_STDSTRING +template +struct TypeHelper > { + typedef std::basic_string StringType; + static bool Is(const ValueType& v) { return v.IsString(); } + static StringType Get(const ValueType& v) { return StringType(v.GetString(), v.GetStringLength()); } + static ValueType& Set(ValueType& v, const StringType& data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); } +}; +#endif + +template +struct TypeHelper { + typedef typename ValueType::Array ArrayType; + static bool Is(const ValueType& v) { return v.IsArray(); } + static ArrayType Get(ValueType& v) { return v.GetArray(); } + static ValueType& Set(ValueType& v, ArrayType data) { return v = data; } + static ValueType& Set(ValueType& v, ArrayType data, typename ValueType::AllocatorType&) { return v = data; } +}; + +template +struct TypeHelper { + typedef typename ValueType::ConstArray ArrayType; + static bool Is(const ValueType& v) { return v.IsArray(); } + static ArrayType Get(const ValueType& v) { return v.GetArray(); } +}; + +template +struct TypeHelper { + typedef typename ValueType::Object ObjectType; + static bool Is(const ValueType& v) { return v.IsObject(); } + static ObjectType Get(ValueType& v) { return v.GetObject(); } + static ValueType& Set(ValueType& v, ObjectType data) { return v = data; } + static ValueType& Set(ValueType& v, ObjectType data, typename ValueType::AllocatorType&) { v = data; } +}; + +template +struct TypeHelper { + typedef typename ValueType::ConstObject ObjectType; + static bool Is(const ValueType& v) { return v.IsObject(); } + static ObjectType Get(const ValueType& v) { return v.GetObject(); } +}; + +} // namespace internal + +// Forward declarations +template class GenericArray; +template class GenericObject; + +/////////////////////////////////////////////////////////////////////////////// +// GenericValue + +//! Represents a JSON value. Use Value for UTF8 encoding and default allocator. +/*! + A JSON value can be one of 7 types. This class is a variant type supporting + these types. + + Use the Value if UTF8 and default allocator + + \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) + \tparam Allocator Allocator type for allocating memory of object, array and string. +*/ +template > +class GenericValue { +public: + //! Name-value pair in an object. + typedef GenericMember Member; + typedef Encoding EncodingType; //!< Encoding type from template parameter. + typedef Allocator AllocatorType; //!< Allocator type from template parameter. + typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. + typedef GenericStringRef StringRefType; //!< Reference to a constant string + typedef typename GenericMemberIterator::Iterator MemberIterator; //!< Member iterator for iterating in object. + typedef typename GenericMemberIterator::Iterator ConstMemberIterator; //!< Constant member iterator for iterating in object. + typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array. + typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array. + typedef GenericValue ValueType; //!< Value type of itself. + typedef GenericArray Array; + typedef GenericArray ConstArray; + typedef GenericObject Object; + typedef GenericObject ConstObject; + + //!@name Constructors and destructor. + //@{ + + //! Default constructor creates a null value. + GenericValue() RAPIDJSON_NOEXCEPT : data_() { data_.f.flags = kNullFlag; } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericValue(GenericValue&& rhs) RAPIDJSON_NOEXCEPT : data_(rhs.data_) { + rhs.data_.f.flags = kNullFlag; // give up contents + } +#endif + +private: + //! Copy constructor is not permitted. + GenericValue(const GenericValue& rhs); + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Moving from a GenericDocument is not permitted. + template + GenericValue(GenericDocument&& rhs); + + //! Move assignment from a GenericDocument is not permitted. + template + GenericValue& operator=(GenericDocument&& rhs); +#endif + +public: + + //! Constructor with JSON value type. + /*! This creates a Value of specified type with default content. + \param type Type of the value. + \note Default content for number is zero. + */ + explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_() { + static const uint16_t defaultFlags[7] = { + kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag, + kNumberAnyFlag + }; + RAPIDJSON_ASSERT(type <= kNumberType); + data_.f.flags = defaultFlags[type]; + + // Use ShortString to store empty string. + if (type == kStringType) + data_.ss.SetLength(0); + } + + //! Explicit copy constructor (with allocator) + /*! Creates a copy of a Value by using the given Allocator + \tparam SourceAllocator allocator of \c rhs + \param rhs Value to copy from (read-only) + \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator(). + \see CopyFrom() + */ + template< typename SourceAllocator > + GenericValue(const GenericValue& rhs, Allocator & allocator); + + //! Constructor for boolean value. + /*! \param b Boolean value + \note This constructor is limited to \em real boolean values and rejects + implicitly converted types like arbitrary pointers. Use an explicit cast + to \c bool, if you want to construct a boolean JSON value in such cases. + */ +#ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen + template + explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame))) RAPIDJSON_NOEXCEPT // See #472 +#else + explicit GenericValue(bool b) RAPIDJSON_NOEXCEPT +#endif + : data_() { + // safe-guard against failing SFINAE + RAPIDJSON_STATIC_ASSERT((internal::IsSame::Value)); + data_.f.flags = b ? kTrueFlag : kFalseFlag; + } + + //! Constructor for int value. + explicit GenericValue(int i) RAPIDJSON_NOEXCEPT : data_() { + data_.n.i64 = i; + data_.f.flags = (i >= 0) ? (kNumberIntFlag | kUintFlag | kUint64Flag) : kNumberIntFlag; + } + + //! Constructor for unsigned value. + explicit GenericValue(unsigned u) RAPIDJSON_NOEXCEPT : data_() { + data_.n.u64 = u; + data_.f.flags = (u & 0x80000000) ? kNumberUintFlag : (kNumberUintFlag | kIntFlag | kInt64Flag); + } + + //! Constructor for int64_t value. + explicit GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT : data_() { + data_.n.i64 = i64; + data_.f.flags = kNumberInt64Flag; + if (i64 >= 0) { + data_.f.flags |= kNumberUint64Flag; + if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) + data_.f.flags |= kUintFlag; + if (!(static_cast(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + else if (i64 >= static_cast(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + + //! Constructor for uint64_t value. + explicit GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT : data_() { + data_.n.u64 = u64; + data_.f.flags = kNumberUint64Flag; + if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000))) + data_.f.flags |= kInt64Flag; + if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) + data_.f.flags |= kUintFlag; + if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) + data_.f.flags |= kIntFlag; + } + + //! Constructor for double value. + explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = d; data_.f.flags = kNumberDoubleFlag; } + + //! Constructor for constant string (i.e. do not make a copy of string) + GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(StringRef(s, length)); } + + //! Constructor for constant string (i.e. do not make a copy of string) + explicit GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(s); } + + //! Constructor for copy-string (i.e. do make a copy of string) + GenericValue(const Ch* s, SizeType length, Allocator& allocator) : data_() { SetStringRaw(StringRef(s, length), allocator); } + + //! Constructor for copy-string (i.e. do make a copy of string) + GenericValue(const Ch*s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); } + +#if RAPIDJSON_HAS_STDSTRING + //! Constructor for copy-string from a string object (i.e. do make a copy of string) + /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + GenericValue(const std::basic_string& s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); } +#endif + + //! Constructor for Array. + /*! + \param a An array obtained by \c GetArray(). + \note \c Array is always pass-by-value. + \note the source array is moved into this value and the sourec array becomes empty. + */ + GenericValue(Array a) RAPIDJSON_NOEXCEPT : data_(a.value_.data_) { + a.value_.data_ = Data(); + a.value_.data_.f.flags = kArrayFlag; + } + + //! Constructor for Object. + /*! + \param o An object obtained by \c GetObject(). + \note \c Object is always pass-by-value. + \note the source object is moved into this value and the sourec object becomes empty. + */ + GenericValue(Object o) RAPIDJSON_NOEXCEPT : data_(o.value_.data_) { + o.value_.data_ = Data(); + o.value_.data_.f.flags = kObjectFlag; + } + + //! Destructor. + /*! Need to destruct elements of array, members of object, or copy-string. + */ + ~GenericValue() { + if (Allocator::kNeedFree) { // Shortcut by Allocator's trait + switch(data_.f.flags) { + case kArrayFlag: + { + GenericValue* e = GetElementsPointer(); + for (GenericValue* v = e; v != e + data_.a.size; ++v) + v->~GenericValue(); + Allocator::Free(e); + } + break; + + case kObjectFlag: + for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) + m->~Member(); + Allocator::Free(GetMembersPointer()); + break; + + case kCopyStringFlag: + Allocator::Free(const_cast(GetStringPointer())); + break; + + default: + break; // Do nothing for other types. + } + } + } + + //@} + + //!@name Assignment operators + //@{ + + //! Assignment with move semantics. + /*! \param rhs Source of the assignment. It will become a null value after assignment. + */ + GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT { + RAPIDJSON_ASSERT(this != &rhs); + this->~GenericValue(); + RawAssign(rhs); + return *this; + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move assignment in C++11 + GenericValue& operator=(GenericValue&& rhs) RAPIDJSON_NOEXCEPT { + return *this = rhs.Move(); + } +#endif + + //! Assignment of constant string reference (no copy) + /*! \param str Constant string reference to be assigned + \note This overload is needed to avoid clashes with the generic primitive type assignment overload below. + \see GenericStringRef, operator=(T) + */ + GenericValue& operator=(StringRefType str) RAPIDJSON_NOEXCEPT { + GenericValue s(str); + return *this = s; + } + + //! Assignment with primitive types. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param value The value to be assigned. + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref SetString(const Ch*, Allocator&) (for copying) or + \ref StringRef() (to explicitly mark the pointer as constant) instead. + All other pointer types would implicitly convert to \c bool, + use \ref SetBool() instead. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::IsPointer), (GenericValue&)) + operator=(T value) { + GenericValue v(value); + return *this = v; + } + + //! Deep-copy assignment from Value + /*! Assigns a \b copy of the Value to the current Value object + \tparam SourceAllocator Allocator type of \c rhs + \param rhs Value to copy from (read-only) + \param allocator Allocator to use for copying + */ + template + GenericValue& CopyFrom(const GenericValue& rhs, Allocator& allocator) { + RAPIDJSON_ASSERT(static_cast(this) != static_cast(&rhs)); + this->~GenericValue(); + new (this) GenericValue(rhs, allocator); + return *this; + } + + //! Exchange the contents of this value with those of other. + /*! + \param other Another value. + \note Constant complexity. + */ + GenericValue& Swap(GenericValue& other) RAPIDJSON_NOEXCEPT { + GenericValue temp; + temp.RawAssign(*this); + RawAssign(other); + other.RawAssign(temp); + return *this; + } + + //! free-standing swap function helper + /*! + Helper function to enable support for common swap implementation pattern based on \c std::swap: + \code + void swap(MyClass& a, MyClass& b) { + using std::swap; + swap(a.value, b.value); + // ... + } + \endcode + \see Swap() + */ + friend inline void swap(GenericValue& a, GenericValue& b) RAPIDJSON_NOEXCEPT { a.Swap(b); } + + //! Prepare Value for move semantics + /*! \return *this */ + GenericValue& Move() RAPIDJSON_NOEXCEPT { return *this; } + //@} + + //!@name Equal-to and not-equal-to operators + //@{ + //! Equal-to operator + /*! + \note If an object contains duplicated named member, comparing equality with any object is always \c false. + \note Linear time complexity (number of all values in the subtree and total lengths of all strings). + */ + template + bool operator==(const GenericValue& rhs) const { + typedef GenericValue RhsType; + if (GetType() != rhs.GetType()) + return false; + + switch (GetType()) { + case kObjectType: // Warning: O(n^2) inner-loop + if (data_.o.size != rhs.data_.o.size) + return false; + for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) { + typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name); + if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value) + return false; + } + return true; + + case kArrayType: + if (data_.a.size != rhs.data_.a.size) + return false; + for (SizeType i = 0; i < data_.a.size; i++) + if ((*this)[i] != rhs[i]) + return false; + return true; + + case kStringType: + return StringEqual(rhs); + + case kNumberType: + if (IsDouble() || rhs.IsDouble()) { + double a = GetDouble(); // May convert from integer to double. + double b = rhs.GetDouble(); // Ditto + return a >= b && a <= b; // Prevent -Wfloat-equal + } + else + return data_.n.u64 == rhs.data_.n.u64; + + default: + return true; + } + } + + //! Equal-to operator with const C-string pointer + bool operator==(const Ch* rhs) const { return *this == GenericValue(StringRef(rhs)); } + +#if RAPIDJSON_HAS_STDSTRING + //! Equal-to operator with string object + /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + bool operator==(const std::basic_string& rhs) const { return *this == GenericValue(StringRef(rhs)); } +#endif + + //! Equal-to operator with primitive types + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c true, \c false + */ + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr,internal::IsGenericValue >), (bool)) operator==(const T& rhs) const { return *this == GenericValue(rhs); } + + //! Not-equal-to operator + /*! \return !(*this == rhs) + */ + template + bool operator!=(const GenericValue& rhs) const { return !(*this == rhs); } + + //! Not-equal-to operator with const C-string pointer + bool operator!=(const Ch* rhs) const { return !(*this == rhs); } + + //! Not-equal-to operator with arbitrary types + /*! \return !(*this == rhs) + */ + template RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& rhs) const { return !(*this == rhs); } + + //! Equal-to operator with arbitrary types (symmetric version) + /*! \return (rhs == lhs) + */ + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator==(const T& lhs, const GenericValue& rhs) { return rhs == lhs; } + + //! Not-Equal-to operator with arbitrary types (symmetric version) + /*! \return !(rhs == lhs) + */ + template friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue), (bool)) operator!=(const T& lhs, const GenericValue& rhs) { return !(rhs == lhs); } + //@} + + //!@name Type + //@{ + + Type GetType() const { return static_cast(data_.f.flags & kTypeMask); } + bool IsNull() const { return data_.f.flags == kNullFlag; } + bool IsFalse() const { return data_.f.flags == kFalseFlag; } + bool IsTrue() const { return data_.f.flags == kTrueFlag; } + bool IsBool() const { return (data_.f.flags & kBoolFlag) != 0; } + bool IsObject() const { return data_.f.flags == kObjectFlag; } + bool IsArray() const { return data_.f.flags == kArrayFlag; } + bool IsNumber() const { return (data_.f.flags & kNumberFlag) != 0; } + bool IsInt() const { return (data_.f.flags & kIntFlag) != 0; } + bool IsUint() const { return (data_.f.flags & kUintFlag) != 0; } + bool IsInt64() const { return (data_.f.flags & kInt64Flag) != 0; } + bool IsUint64() const { return (data_.f.flags & kUint64Flag) != 0; } + bool IsDouble() const { return (data_.f.flags & kDoubleFlag) != 0; } + bool IsString() const { return (data_.f.flags & kStringFlag) != 0; } + + // Checks whether a number can be losslessly converted to a double. + bool IsLosslessDouble() const { + if (!IsNumber()) return false; + if (IsUint64()) { + uint64_t u = GetUint64(); + volatile double d = static_cast(u); + return (d >= 0.0) + && (d < static_cast(std::numeric_limits::max())) + && (u == static_cast(d)); + } + if (IsInt64()) { + int64_t i = GetInt64(); + volatile double d = static_cast(i); + return (d >= static_cast(std::numeric_limits::min())) + && (d < static_cast(std::numeric_limits::max())) + && (i == static_cast(d)); + } + return true; // double, int, uint are always lossless + } + + // Checks whether a number is a float (possible lossy). + bool IsFloat() const { + if ((data_.f.flags & kDoubleFlag) == 0) + return false; + double d = GetDouble(); + return d >= -3.4028234e38 && d <= 3.4028234e38; + } + // Checks whether a number can be losslessly converted to a float. + bool IsLosslessFloat() const { + if (!IsNumber()) return false; + double a = GetDouble(); + if (a < static_cast(-std::numeric_limits::max()) + || a > static_cast(std::numeric_limits::max())) + return false; + double b = static_cast(static_cast(a)); + return a >= b && a <= b; // Prevent -Wfloat-equal + } + + //@} + + //!@name Null + //@{ + + GenericValue& SetNull() { this->~GenericValue(); new (this) GenericValue(); return *this; } + + //@} + + //!@name Bool + //@{ + + bool GetBool() const { RAPIDJSON_ASSERT(IsBool()); return data_.f.flags == kTrueFlag; } + //!< Set boolean value + /*! \post IsBool() == true */ + GenericValue& SetBool(bool b) { this->~GenericValue(); new (this) GenericValue(b); return *this; } + + //@} + + //!@name Object + //@{ + + //! Set this value as an empty object. + /*! \post IsObject() == true */ + GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; } + + //! Get the number of members in the object. + SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; } + + //! Check whether the object is empty. + bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; } + + //! Get a value from an object associated with the name. + /*! \pre IsObject() == true + \tparam T Either \c Ch or \c const \c Ch (template used for disambiguation with \ref operator[](SizeType)) + \note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7. + Since 0.2, if the name is not correct, it will assert. + If user is unsure whether a member exists, user should use HasMember() first. + A better approach is to use FindMember(). + \note Linear time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >),(GenericValue&)) operator[](T* name) { + GenericValue n(StringRef(name)); + return (*this)[n]; + } + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >),(const GenericValue&)) operator[](T* name) const { return const_cast(*this)[name]; } + + //! Get a value from an object associated with the name. + /*! \pre IsObject() == true + \tparam SourceAllocator Allocator of the \c name value + + \note Compared to \ref operator[](T*), this version is faster because it does not need a StrLen(). + And it can also handle strings with embedded null characters. + + \note Linear time complexity. + */ + template + GenericValue& operator[](const GenericValue& name) { + MemberIterator member = FindMember(name); + if (member != MemberEnd()) + return member->value; + else { + RAPIDJSON_ASSERT(false); // see above note + + // This will generate -Wexit-time-destructors in clang + // static GenericValue NullValue; + // return NullValue; + + // Use static buffer and placement-new to prevent destruction + static char buffer[sizeof(GenericValue)]; + return *new (buffer) GenericValue(); + } + } + template + const GenericValue& operator[](const GenericValue& name) const { return const_cast(*this)[name]; } + +#if RAPIDJSON_HAS_STDSTRING + //! Get a value from an object associated with name (string object). + GenericValue& operator[](const std::basic_string& name) { return (*this)[GenericValue(StringRef(name))]; } + const GenericValue& operator[](const std::basic_string& name) const { return (*this)[GenericValue(StringRef(name))]; } +#endif + + //! Const member iterator + /*! \pre IsObject() == true */ + ConstMemberIterator MemberBegin() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(GetMembersPointer()); } + //! Const \em past-the-end member iterator + /*! \pre IsObject() == true */ + ConstMemberIterator MemberEnd() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(GetMembersPointer() + data_.o.size); } + //! Member iterator + /*! \pre IsObject() == true */ + MemberIterator MemberBegin() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer()); } + //! \em Past-the-end member iterator + /*! \pre IsObject() == true */ + MemberIterator MemberEnd() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer() + data_.o.size); } + + //! Check whether a member exists in the object. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + bool HasMember(const Ch* name) const { return FindMember(name) != MemberEnd(); } + +#if RAPIDJSON_HAS_STDSTRING + //! Check whether a member exists in the object with string object. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + bool HasMember(const std::basic_string& name) const { return FindMember(name) != MemberEnd(); } +#endif + + //! Check whether a member exists in the object with GenericValue name. + /*! + This version is faster because it does not need a StrLen(). It can also handle string with null character. + \param name Member name to be searched. + \pre IsObject() == true + \return Whether a member with that name exists. + \note It is better to use FindMember() directly if you need the obtain the value as well. + \note Linear time complexity. + */ + template + bool HasMember(const GenericValue& name) const { return FindMember(name) != MemberEnd(); } + + //! Find member by name. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + + \note Earlier versions of Rapidjson returned a \c NULL pointer, in case + the requested member doesn't exist. For consistency with e.g. + \c std::map, this has been changed to MemberEnd() now. + \note Linear time complexity. + */ + MemberIterator FindMember(const Ch* name) { + GenericValue n(StringRef(name)); + return FindMember(n); + } + + ConstMemberIterator FindMember(const Ch* name) const { return const_cast(*this).FindMember(name); } + + //! Find member by name. + /*! + This version is faster because it does not need a StrLen(). It can also handle string with null character. + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + + \note Earlier versions of Rapidjson returned a \c NULL pointer, in case + the requested member doesn't exist. For consistency with e.g. + \c std::map, this has been changed to MemberEnd() now. + \note Linear time complexity. + */ + template + MemberIterator FindMember(const GenericValue& name) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(name.IsString()); + MemberIterator member = MemberBegin(); + for ( ; member != MemberEnd(); ++member) + if (name.StringEqual(member->name)) + break; + return member; + } + template ConstMemberIterator FindMember(const GenericValue& name) const { return const_cast(*this).FindMember(name); } + +#if RAPIDJSON_HAS_STDSTRING + //! Find member by string object name. + /*! + \param name Member name to be searched. + \pre IsObject() == true + \return Iterator to member, if it exists. + Otherwise returns \ref MemberEnd(). + */ + MemberIterator FindMember(const std::basic_string& name) { return FindMember(GenericValue(StringRef(name))); } + ConstMemberIterator FindMember(const std::basic_string& name) const { return FindMember(GenericValue(StringRef(name))); } +#endif + + //! Add a member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value Value of any type. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note The ownership of \c name and \c value will be transferred to this object on success. + \pre IsObject() && name.IsString() + \post name.IsNull() && value.IsNull() + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, GenericValue& value, Allocator& allocator) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(name.IsString()); + + ObjectData& o = data_.o; + if (o.size >= o.capacity) { + if (o.capacity == 0) { + o.capacity = kDefaultObjectCapacity; + SetMembersPointer(reinterpret_cast(allocator.Malloc(o.capacity * sizeof(Member)))); + } + else { + SizeType oldCapacity = o.capacity; + o.capacity += (oldCapacity + 1) / 2; // grow by factor 1.5 + SetMembersPointer(reinterpret_cast(allocator.Realloc(GetMembersPointer(), oldCapacity * sizeof(Member), o.capacity * sizeof(Member)))); + } + } + Member* members = GetMembersPointer(); + members[o.size].name.RawAssign(name); + members[o.size].value.RawAssign(value); + o.size++; + return *this; + } + + //! Add a constant string value as member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, StringRefType value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Add a string object as member (name-value pair) to the object. + /*! \param name A string value as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(GenericValue& name, std::basic_string& value, Allocator& allocator) { + GenericValue v(value, allocator); + return AddMember(name, v, allocator); + } +#endif + + //! Add any primitive value as member (name-value pair) to the object. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param name A string value as name of member. + \param value Value of primitive type \c T as value of member + \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref + AddMember(StringRefType, StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized Constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + AddMember(GenericValue& name, T value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericValue& AddMember(GenericValue&& name, GenericValue&& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(GenericValue&& name, GenericValue& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(GenericValue& name, GenericValue&& value, Allocator& allocator) { + return AddMember(name, value, allocator); + } + GenericValue& AddMember(StringRefType name, GenericValue&& value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + + + //! Add a member (name-value pair) to the object. + /*! \param name A constant string reference as name of member. + \param value Value of any type. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note The ownership of \c value will be transferred to this object on success. + \pre IsObject() + \post value.IsNull() + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(StringRefType name, GenericValue& value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } + + //! Add a constant string value as member (name-value pair) to the object. + /*! \param name A constant string reference as name of member. + \param value constant string reference as value of member. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + \note This overload is needed to avoid clashes with the generic primitive type AddMember(StringRefType,T,Allocator&) overload below. + \note Amortized Constant time complexity. + */ + GenericValue& AddMember(StringRefType name, StringRefType value, Allocator& allocator) { + GenericValue v(value); + return AddMember(name, v, allocator); + } + + //! Add any primitive value as member (name-value pair) to the object. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param name A constant string reference as name of member. + \param value Value of primitive type \c T as value of member + \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \pre IsObject() + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref + AddMember(StringRefType, StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized Constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + AddMember(StringRefType name, T value, Allocator& allocator) { + GenericValue n(name); + return AddMember(n, value, allocator); + } + + //! Remove all members in the object. + /*! This function do not deallocate memory in the object, i.e. the capacity is unchanged. + \note Linear time complexity. + */ + void RemoveAllMembers() { + RAPIDJSON_ASSERT(IsObject()); + for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) + m->~Member(); + data_.o.size = 0; + } + + //! Remove a member in object by its name. + /*! \param name Name of member to be removed. + \return Whether the member existed. + \note This function may reorder the object members. Use \ref + EraseMember(ConstMemberIterator) if you need to preserve the + relative order of the remaining members. + \note Linear time complexity. + */ + bool RemoveMember(const Ch* name) { + GenericValue n(StringRef(name)); + return RemoveMember(n); + } + +#if RAPIDJSON_HAS_STDSTRING + bool RemoveMember(const std::basic_string& name) { return RemoveMember(GenericValue(StringRef(name))); } +#endif + + template + bool RemoveMember(const GenericValue& name) { + MemberIterator m = FindMember(name); + if (m != MemberEnd()) { + RemoveMember(m); + return true; + } + else + return false; + } + + //! Remove a member in object by iterator. + /*! \param m member iterator (obtained by FindMember() or MemberBegin()). + \return the new iterator after removal. + \note This function may reorder the object members. Use \ref + EraseMember(ConstMemberIterator) if you need to preserve the + relative order of the remaining members. + \note Constant time complexity. + */ + MemberIterator RemoveMember(MemberIterator m) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(data_.o.size > 0); + RAPIDJSON_ASSERT(GetMembersPointer() != 0); + RAPIDJSON_ASSERT(m >= MemberBegin() && m < MemberEnd()); + + MemberIterator last(GetMembersPointer() + (data_.o.size - 1)); + if (data_.o.size > 1 && m != last) + *m = *last; // Move the last one to this place + else + m->~Member(); // Only one left, just destroy + --data_.o.size; + return m; + } + + //! Remove a member from an object by iterator. + /*! \param pos iterator to the member to remove + \pre IsObject() == true && \ref MemberBegin() <= \c pos < \ref MemberEnd() + \return Iterator following the removed element. + If the iterator \c pos refers to the last element, the \ref MemberEnd() iterator is returned. + \note This function preserves the relative order of the remaining object + members. If you do not need this, use the more efficient \ref RemoveMember(MemberIterator). + \note Linear time complexity. + */ + MemberIterator EraseMember(ConstMemberIterator pos) { + return EraseMember(pos, pos +1); + } + + //! Remove members in the range [first, last) from an object. + /*! \param first iterator to the first member to remove + \param last iterator following the last member to remove + \pre IsObject() == true && \ref MemberBegin() <= \c first <= \c last <= \ref MemberEnd() + \return Iterator following the last removed element. + \note This function preserves the relative order of the remaining object + members. + \note Linear time complexity. + */ + MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) { + RAPIDJSON_ASSERT(IsObject()); + RAPIDJSON_ASSERT(data_.o.size > 0); + RAPIDJSON_ASSERT(GetMembersPointer() != 0); + RAPIDJSON_ASSERT(first >= MemberBegin()); + RAPIDJSON_ASSERT(first <= last); + RAPIDJSON_ASSERT(last <= MemberEnd()); + + MemberIterator pos = MemberBegin() + (first - MemberBegin()); + for (MemberIterator itr = pos; itr != last; ++itr) + itr->~Member(); + std::memmove(&*pos, &*last, static_cast(MemberEnd() - last) * sizeof(Member)); + data_.o.size -= static_cast(last - first); + return pos; + } + + //! Erase a member in object by its name. + /*! \param name Name of member to be removed. + \return Whether the member existed. + \note Linear time complexity. + */ + bool EraseMember(const Ch* name) { + GenericValue n(StringRef(name)); + return EraseMember(n); + } + +#if RAPIDJSON_HAS_STDSTRING + bool EraseMember(const std::basic_string& name) { return EraseMember(GenericValue(StringRef(name))); } +#endif + + template + bool EraseMember(const GenericValue& name) { + MemberIterator m = FindMember(name); + if (m != MemberEnd()) { + EraseMember(m); + return true; + } + else + return false; + } + + Object GetObject() { RAPIDJSON_ASSERT(IsObject()); return Object(*this); } + ConstObject GetObject() const { RAPIDJSON_ASSERT(IsObject()); return ConstObject(*this); } + + //@} + + //!@name Array + //@{ + + //! Set this value as an empty array. + /*! \post IsArray == true */ + GenericValue& SetArray() { this->~GenericValue(); new (this) GenericValue(kArrayType); return *this; } + + //! Get the number of elements in array. + SizeType Size() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size; } + + //! Get the capacity of array. + SizeType Capacity() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.capacity; } + + //! Check whether the array is empty. + bool Empty() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size == 0; } + + //! Remove all elements in the array. + /*! This function do not deallocate memory in the array, i.e. the capacity is unchanged. + \note Linear time complexity. + */ + void Clear() { + RAPIDJSON_ASSERT(IsArray()); + GenericValue* e = GetElementsPointer(); + for (GenericValue* v = e; v != e + data_.a.size; ++v) + v->~GenericValue(); + data_.a.size = 0; + } + + //! Get an element from array by index. + /*! \pre IsArray() == true + \param index Zero-based index of element. + \see operator[](T*) + */ + GenericValue& operator[](SizeType index) { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(index < data_.a.size); + return GetElementsPointer()[index]; + } + const GenericValue& operator[](SizeType index) const { return const_cast(*this)[index]; } + + //! Element iterator + /*! \pre IsArray() == true */ + ValueIterator Begin() { RAPIDJSON_ASSERT(IsArray()); return GetElementsPointer(); } + //! \em Past-the-end element iterator + /*! \pre IsArray() == true */ + ValueIterator End() { RAPIDJSON_ASSERT(IsArray()); return GetElementsPointer() + data_.a.size; } + //! Constant element iterator + /*! \pre IsArray() == true */ + ConstValueIterator Begin() const { return const_cast(*this).Begin(); } + //! Constant \em past-the-end element iterator + /*! \pre IsArray() == true */ + ConstValueIterator End() const { return const_cast(*this).End(); } + + //! Request the array to have enough capacity to store elements. + /*! \param newCapacity The capacity that the array at least need to have. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \note Linear time complexity. + */ + GenericValue& Reserve(SizeType newCapacity, Allocator &allocator) { + RAPIDJSON_ASSERT(IsArray()); + if (newCapacity > data_.a.capacity) { + SetElementsPointer(reinterpret_cast(allocator.Realloc(GetElementsPointer(), data_.a.capacity * sizeof(GenericValue), newCapacity * sizeof(GenericValue)))); + data_.a.capacity = newCapacity; + } + return *this; + } + + //! Append a GenericValue at the end of the array. + /*! \param value Value to be appended. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \post value.IsNull() == true + \return The value itself for fluent API. + \note The ownership of \c value will be transferred to this array on success. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + \note Amortized constant time complexity. + */ + GenericValue& PushBack(GenericValue& value, Allocator& allocator) { + RAPIDJSON_ASSERT(IsArray()); + if (data_.a.size >= data_.a.capacity) + Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : (data_.a.capacity + (data_.a.capacity + 1) / 2), allocator); + GetElementsPointer()[data_.a.size++].RawAssign(value); + return *this; + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericValue& PushBack(GenericValue&& value, Allocator& allocator) { + return PushBack(value, allocator); + } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + + //! Append a constant string reference at the end of the array. + /*! \param value Constant string reference to be appended. + \param allocator Allocator for reallocating memory. It must be the same one used previously. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \return The value itself for fluent API. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + \note Amortized constant time complexity. + \see GenericStringRef + */ + GenericValue& PushBack(StringRefType value, Allocator& allocator) { + return (*this).template PushBack(value, allocator); + } + + //! Append a primitive value at the end of the array. + /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t + \param value Value of primitive type T to be appended. + \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). + \pre IsArray() == true + \return The value itself for fluent API. + \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. + + \note The source type \c T explicitly disallows all pointer types, + especially (\c const) \ref Ch*. This helps avoiding implicitly + referencing character strings with insufficient lifetime, use + \ref PushBack(GenericValue&, Allocator&) or \ref + PushBack(StringRefType, Allocator&). + All other pointer types would implicitly convert to \c bool, + use an explicit cast instead, if needed. + \note Amortized constant time complexity. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericValue&)) + PushBack(T value, Allocator& allocator) { + GenericValue v(value); + return PushBack(v, allocator); + } + + //! Remove the last element in the array. + /*! + \note Constant time complexity. + */ + GenericValue& PopBack() { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(!Empty()); + GetElementsPointer()[--data_.a.size].~GenericValue(); + return *this; + } + + //! Remove an element of array by iterator. + /*! + \param pos iterator to the element to remove + \pre IsArray() == true && \ref Begin() <= \c pos < \ref End() + \return Iterator following the removed element. If the iterator pos refers to the last element, the End() iterator is returned. + \note Linear time complexity. + */ + ValueIterator Erase(ConstValueIterator pos) { + return Erase(pos, pos + 1); + } + + //! Remove elements in the range [first, last) of the array. + /*! + \param first iterator to the first element to remove + \param last iterator following the last element to remove + \pre IsArray() == true && \ref Begin() <= \c first <= \c last <= \ref End() + \return Iterator following the last removed element. + \note Linear time complexity. + */ + ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) { + RAPIDJSON_ASSERT(IsArray()); + RAPIDJSON_ASSERT(data_.a.size > 0); + RAPIDJSON_ASSERT(GetElementsPointer() != 0); + RAPIDJSON_ASSERT(first >= Begin()); + RAPIDJSON_ASSERT(first <= last); + RAPIDJSON_ASSERT(last <= End()); + ValueIterator pos = Begin() + (first - Begin()); + for (ValueIterator itr = pos; itr != last; ++itr) + itr->~GenericValue(); + std::memmove(pos, last, static_cast(End() - last) * sizeof(GenericValue)); + data_.a.size -= static_cast(last - first); + return pos; + } + + Array GetArray() { RAPIDJSON_ASSERT(IsArray()); return Array(*this); } + ConstArray GetArray() const { RAPIDJSON_ASSERT(IsArray()); return ConstArray(*this); } + + //@} + + //!@name Number + //@{ + + int GetInt() const { RAPIDJSON_ASSERT(data_.f.flags & kIntFlag); return data_.n.i.i; } + unsigned GetUint() const { RAPIDJSON_ASSERT(data_.f.flags & kUintFlag); return data_.n.u.u; } + int64_t GetInt64() const { RAPIDJSON_ASSERT(data_.f.flags & kInt64Flag); return data_.n.i64; } + uint64_t GetUint64() const { RAPIDJSON_ASSERT(data_.f.flags & kUint64Flag); return data_.n.u64; } + + //! Get the value as double type. + /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessDouble() to check whether the converison is lossless. + */ + double GetDouble() const { + RAPIDJSON_ASSERT(IsNumber()); + if ((data_.f.flags & kDoubleFlag) != 0) return data_.n.d; // exact type, no conversion. + if ((data_.f.flags & kIntFlag) != 0) return data_.n.i.i; // int -> double + if ((data_.f.flags & kUintFlag) != 0) return data_.n.u.u; // unsigned -> double + if ((data_.f.flags & kInt64Flag) != 0) return static_cast(data_.n.i64); // int64_t -> double (may lose precision) + RAPIDJSON_ASSERT((data_.f.flags & kUint64Flag) != 0); return static_cast(data_.n.u64); // uint64_t -> double (may lose precision) + } + + //! Get the value as float type. + /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessFloat() to check whether the converison is lossless. + */ + float GetFloat() const { + return static_cast(GetDouble()); + } + + GenericValue& SetInt(int i) { this->~GenericValue(); new (this) GenericValue(i); return *this; } + GenericValue& SetUint(unsigned u) { this->~GenericValue(); new (this) GenericValue(u); return *this; } + GenericValue& SetInt64(int64_t i64) { this->~GenericValue(); new (this) GenericValue(i64); return *this; } + GenericValue& SetUint64(uint64_t u64) { this->~GenericValue(); new (this) GenericValue(u64); return *this; } + GenericValue& SetDouble(double d) { this->~GenericValue(); new (this) GenericValue(d); return *this; } + GenericValue& SetFloat(float f) { this->~GenericValue(); new (this) GenericValue(f); return *this; } + + //@} + + //!@name String + //@{ + + const Ch* GetString() const { RAPIDJSON_ASSERT(IsString()); return (data_.f.flags & kInlineStrFlag) ? data_.ss.str : GetStringPointer(); } + + //! Get the length of string. + /*! Since rapidjson permits "\\u0000" in the json string, strlen(v.GetString()) may not equal to v.GetStringLength(). + */ + SizeType GetStringLength() const { RAPIDJSON_ASSERT(IsString()); return ((data_.f.flags & kInlineStrFlag) ? (data_.ss.GetLength()) : data_.s.length); } + + //! Set this value as a string without copying source string. + /*! This version has better performance with supplied length, and also support string containing null character. + \param s source string pointer. + \param length The length of source string, excluding the trailing null terminator. + \return The value itself for fluent API. + \post IsString() == true && GetString() == s && GetStringLength() == length + \see SetString(StringRefType) + */ + GenericValue& SetString(const Ch* s, SizeType length) { return SetString(StringRef(s, length)); } + + //! Set this value as a string without copying source string. + /*! \param s source string reference + \return The value itself for fluent API. + \post IsString() == true && GetString() == s && GetStringLength() == s.length + */ + GenericValue& SetString(StringRefType s) { this->~GenericValue(); SetStringRaw(s); return *this; } + + //! Set this value as a string by copying from source string. + /*! This version has better performance with supplied length, and also support string containing null character. + \param s source string. + \param length The length of source string, excluding the trailing null terminator. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { this->~GenericValue(); SetStringRaw(StringRef(s, length), allocator); return *this; } + + //! Set this value as a string by copying from source string. + /*! \param s source string. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length + */ + GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(s, internal::StrLen(s), allocator); } + +#if RAPIDJSON_HAS_STDSTRING + //! Set this value as a string by copying from source string. + /*! \param s source string. + \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). + \return The value itself for fluent API. + \post IsString() == true && GetString() != s.data() && strcmp(GetString(),s.data() == 0 && GetStringLength() == s.size() + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + GenericValue& SetString(const std::basic_string& s, Allocator& allocator) { return SetString(s.data(), SizeType(s.size()), allocator); } +#endif + + //@} + + //!@name Array + //@{ + + //! Templated version for checking whether this value is type T. + /*! + \tparam T Either \c bool, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c float, \c const \c char*, \c std::basic_string + */ + template + bool Is() const { return internal::TypeHelper::Is(*this); } + + template + T Get() const { return internal::TypeHelper::Get(*this); } + + template + T Get() { return internal::TypeHelper::Get(*this); } + + template + ValueType& Set(const T& data) { return internal::TypeHelper::Set(*this, data); } + + template + ValueType& Set(const T& data, AllocatorType& allocator) { return internal::TypeHelper::Set(*this, data, allocator); } + + //@} + + //! Generate events of this value to a Handler. + /*! This function adopts the GoF visitor pattern. + Typical usage is to output this JSON value as JSON text via Writer, which is a Handler. + It can also be used to deep clone this value via GenericDocument, which is also a Handler. + \tparam Handler type of handler. + \param handler An object implementing concept Handler. + */ + template + bool Accept(Handler& handler) const { + switch(GetType()) { + case kNullType: return handler.Null(); + case kFalseType: return handler.Bool(false); + case kTrueType: return handler.Bool(true); + + case kObjectType: + if (RAPIDJSON_UNLIKELY(!handler.StartObject())) + return false; + for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) { + RAPIDJSON_ASSERT(m->name.IsString()); // User may change the type of name by MemberIterator. + if (RAPIDJSON_UNLIKELY(!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.data_.f.flags & kCopyFlag) != 0))) + return false; + if (RAPIDJSON_UNLIKELY(!m->value.Accept(handler))) + return false; + } + return handler.EndObject(data_.o.size); + + case kArrayType: + if (RAPIDJSON_UNLIKELY(!handler.StartArray())) + return false; + for (const GenericValue* v = Begin(); v != End(); ++v) + if (RAPIDJSON_UNLIKELY(!v->Accept(handler))) + return false; + return handler.EndArray(data_.a.size); + + case kStringType: + return handler.String(GetString(), GetStringLength(), (data_.f.flags & kCopyFlag) != 0); + + default: + RAPIDJSON_ASSERT(GetType() == kNumberType); + if (IsDouble()) return handler.Double(data_.n.d); + else if (IsInt()) return handler.Int(data_.n.i.i); + else if (IsUint()) return handler.Uint(data_.n.u.u); + else if (IsInt64()) return handler.Int64(data_.n.i64); + else return handler.Uint64(data_.n.u64); + } + } + +private: + template friend class GenericValue; + template friend class GenericDocument; + + enum { + kBoolFlag = 0x0008, + kNumberFlag = 0x0010, + kIntFlag = 0x0020, + kUintFlag = 0x0040, + kInt64Flag = 0x0080, + kUint64Flag = 0x0100, + kDoubleFlag = 0x0200, + kStringFlag = 0x0400, + kCopyFlag = 0x0800, + kInlineStrFlag = 0x1000, + + // Initial flags of different types. + kNullFlag = kNullType, + kTrueFlag = kTrueType | kBoolFlag, + kFalseFlag = kFalseType | kBoolFlag, + kNumberIntFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag, + kNumberUintFlag = kNumberType | kNumberFlag | kUintFlag | kUint64Flag | kInt64Flag, + kNumberInt64Flag = kNumberType | kNumberFlag | kInt64Flag, + kNumberUint64Flag = kNumberType | kNumberFlag | kUint64Flag, + kNumberDoubleFlag = kNumberType | kNumberFlag | kDoubleFlag, + kNumberAnyFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag | kUintFlag | kUint64Flag | kDoubleFlag, + kConstStringFlag = kStringType | kStringFlag, + kCopyStringFlag = kStringType | kStringFlag | kCopyFlag, + kShortStringFlag = kStringType | kStringFlag | kCopyFlag | kInlineStrFlag, + kObjectFlag = kObjectType, + kArrayFlag = kArrayType, + + kTypeMask = 0x07 + }; + + static const SizeType kDefaultArrayCapacity = 16; + static const SizeType kDefaultObjectCapacity = 16; + + struct Flag { +#if RAPIDJSON_48BITPOINTER_OPTIMIZATION + char payload[sizeof(SizeType) * 2 + 6]; // 2 x SizeType + lower 48-bit pointer +#elif RAPIDJSON_64BIT + char payload[sizeof(SizeType) * 2 + sizeof(void*) + 6]; // 6 padding bytes +#else + char payload[sizeof(SizeType) * 2 + sizeof(void*) + 2]; // 2 padding bytes +#endif + uint16_t flags; + }; + + struct String { + SizeType length; + SizeType hashcode; //!< reserved + const Ch* str; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + // implementation detail: ShortString can represent zero-terminated strings up to MaxSize chars + // (excluding the terminating zero) and store a value to determine the length of the contained + // string in the last character str[LenPos] by storing "MaxSize - length" there. If the string + // to store has the maximal length of MaxSize then str[LenPos] will be 0 and therefore act as + // the string terminator as well. For getting the string length back from that value just use + // "MaxSize - str[LenPos]". + // This allows to store 13-chars strings in 32-bit mode, 21-chars strings in 64-bit mode, + // 13-chars strings for RAPIDJSON_48BITPOINTER_OPTIMIZATION=1 inline (for `UTF8`-encoded strings). + struct ShortString { + enum { MaxChars = sizeof(static_cast(0)->payload) / sizeof(Ch), MaxSize = MaxChars - 1, LenPos = MaxSize }; + Ch str[MaxChars]; + + inline static bool Usable(SizeType len) { return (MaxSize >= len); } + inline void SetLength(SizeType len) { str[LenPos] = static_cast(MaxSize - len); } + inline SizeType GetLength() const { return static_cast(MaxSize - str[LenPos]); } + }; // at most as many bytes as "String" above => 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + // By using proper binary layout, retrieval of different integer types do not need conversions. + union Number { +#if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN + struct I { + int i; + char padding[4]; + }i; + struct U { + unsigned u; + char padding2[4]; + }u; +#else + struct I { + char padding[4]; + int i; + }i; + struct U { + char padding2[4]; + unsigned u; + }u; +#endif + int64_t i64; + uint64_t u64; + double d; + }; // 8 bytes + + struct ObjectData { + SizeType size; + SizeType capacity; + Member* members; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + struct ArrayData { + SizeType size; + SizeType capacity; + GenericValue* elements; + }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode + + union Data { + String s; + ShortString ss; + Number n; + ObjectData o; + ArrayData a; + Flag f; + }; // 16 bytes in 32-bit mode, 24 bytes in 64-bit mode, 16 bytes in 64-bit with RAPIDJSON_48BITPOINTER_OPTIMIZATION + + RAPIDJSON_FORCEINLINE const Ch* GetStringPointer() const { return RAPIDJSON_GETPOINTER(Ch, data_.s.str); } + RAPIDJSON_FORCEINLINE const Ch* SetStringPointer(const Ch* str) { return RAPIDJSON_SETPOINTER(Ch, data_.s.str, str); } + RAPIDJSON_FORCEINLINE GenericValue* GetElementsPointer() const { return RAPIDJSON_GETPOINTER(GenericValue, data_.a.elements); } + RAPIDJSON_FORCEINLINE GenericValue* SetElementsPointer(GenericValue* elements) { return RAPIDJSON_SETPOINTER(GenericValue, data_.a.elements, elements); } + RAPIDJSON_FORCEINLINE Member* GetMembersPointer() const { return RAPIDJSON_GETPOINTER(Member, data_.o.members); } + RAPIDJSON_FORCEINLINE Member* SetMembersPointer(Member* members) { return RAPIDJSON_SETPOINTER(Member, data_.o.members, members); } + + // Initialize this value as array with initial data, without calling destructor. + void SetArrayRaw(GenericValue* values, SizeType count, Allocator& allocator) { + data_.f.flags = kArrayFlag; + if (count) { + GenericValue* e = static_cast(allocator.Malloc(count * sizeof(GenericValue))); + SetElementsPointer(e); + std::memcpy(e, values, count * sizeof(GenericValue)); + } + else + SetElementsPointer(0); + data_.a.size = data_.a.capacity = count; + } + + //! Initialize this value as object with initial data, without calling destructor. + void SetObjectRaw(Member* members, SizeType count, Allocator& allocator) { + data_.f.flags = kObjectFlag; + if (count) { + Member* m = static_cast(allocator.Malloc(count * sizeof(Member))); + SetMembersPointer(m); + std::memcpy(m, members, count * sizeof(Member)); + } + else + SetMembersPointer(0); + data_.o.size = data_.o.capacity = count; + } + + //! Initialize this value as constant string, without calling destructor. + void SetStringRaw(StringRefType s) RAPIDJSON_NOEXCEPT { + data_.f.flags = kConstStringFlag; + SetStringPointer(s); + data_.s.length = s.length; + } + + //! Initialize this value as copy string with initial data, without calling destructor. + void SetStringRaw(StringRefType s, Allocator& allocator) { + Ch* str = 0; + if (ShortString::Usable(s.length)) { + data_.f.flags = kShortStringFlag; + data_.ss.SetLength(s.length); + str = data_.ss.str; + } else { + data_.f.flags = kCopyStringFlag; + data_.s.length = s.length; + str = static_cast(allocator.Malloc((s.length + 1) * sizeof(Ch))); + SetStringPointer(str); + } + std::memcpy(str, s, s.length * sizeof(Ch)); + str[s.length] = '\0'; + } + + //! Assignment without calling destructor + void RawAssign(GenericValue& rhs) RAPIDJSON_NOEXCEPT { + data_ = rhs.data_; + // data_.f.flags = rhs.data_.f.flags; + rhs.data_.f.flags = kNullFlag; + } + + template + bool StringEqual(const GenericValue& rhs) const { + RAPIDJSON_ASSERT(IsString()); + RAPIDJSON_ASSERT(rhs.IsString()); + + const SizeType len1 = GetStringLength(); + const SizeType len2 = rhs.GetStringLength(); + if(len1 != len2) { return false; } + + const Ch* const str1 = GetString(); + const Ch* const str2 = rhs.GetString(); + if(str1 == str2) { return true; } // fast path for constant string + + return (std::memcmp(str1, str2, sizeof(Ch) * len1) == 0); + } + + Data data_; +}; + +//! GenericValue with UTF8 encoding +typedef GenericValue > Value; + +/////////////////////////////////////////////////////////////////////////////// +// GenericDocument + +//! A document for parsing JSON text as DOM. +/*! + \note implements Handler concept + \tparam Encoding Encoding for both parsing and string storage. + \tparam Allocator Allocator for allocating memory for the DOM + \tparam StackAllocator Allocator for allocating memory for stack during parsing. + \warning Although GenericDocument inherits from GenericValue, the API does \b not provide any virtual functions, especially no virtual destructor. To avoid memory leaks, do not \c delete a GenericDocument object via a pointer to a GenericValue. +*/ +template , typename StackAllocator = CrtAllocator> +class GenericDocument : public GenericValue { +public: + typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. + typedef GenericValue ValueType; //!< Value type of the document. + typedef Allocator AllocatorType; //!< Allocator type from template parameter. + + //! Constructor + /*! Creates an empty document of specified type. + \param type Mandatory type of object to create. + \param allocator Optional allocator for allocating memory. + \param stackCapacity Optional initial capacity of stack in bytes. + \param stackAllocator Optional allocator for allocating memory for stack. + */ + explicit GenericDocument(Type type, Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) : + GenericValue(type), allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + } + + //! Constructor + /*! Creates an empty document which type is Null. + \param allocator Optional allocator for allocating memory. + \param stackCapacity Optional initial capacity of stack in bytes. + \param stackAllocator Optional allocator for allocating memory for stack. + */ + GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) : + allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericDocument(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT + : ValueType(std::forward(rhs)), // explicit cast to avoid prohibited move from Document + allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + stack_(std::move(rhs.stack_)), + parseResult_(rhs.parseResult_) + { + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.parseResult_ = ParseResult(); + } +#endif + + ~GenericDocument() { + Destroy(); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move assignment in C++11 + GenericDocument& operator=(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT + { + // The cast to ValueType is necessary here, because otherwise it would + // attempt to call GenericValue's templated assignment operator. + ValueType::operator=(std::forward(rhs)); + + // Calling the destructor here would prematurely call stack_'s destructor + Destroy(); + + allocator_ = rhs.allocator_; + ownAllocator_ = rhs.ownAllocator_; + stack_ = std::move(rhs.stack_); + parseResult_ = rhs.parseResult_; + + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.parseResult_ = ParseResult(); + + return *this; + } +#endif + + //! Exchange the contents of this document with those of another. + /*! + \param rhs Another document. + \note Constant complexity. + \see GenericValue::Swap + */ + GenericDocument& Swap(GenericDocument& rhs) RAPIDJSON_NOEXCEPT { + ValueType::Swap(rhs); + stack_.Swap(rhs.stack_); + internal::Swap(allocator_, rhs.allocator_); + internal::Swap(ownAllocator_, rhs.ownAllocator_); + internal::Swap(parseResult_, rhs.parseResult_); + return *this; + } + + //! free-standing swap function helper + /*! + Helper function to enable support for common swap implementation pattern based on \c std::swap: + \code + void swap(MyClass& a, MyClass& b) { + using std::swap; + swap(a.doc, b.doc); + // ... + } + \endcode + \see Swap() + */ + friend inline void swap(GenericDocument& a, GenericDocument& b) RAPIDJSON_NOEXCEPT { a.Swap(b); } + + //! Populate this document by a generator which produces SAX events. + /*! \tparam Generator A functor with bool f(Handler) prototype. + \param g Generator functor which sends SAX events to the parameter. + \return The document itself for fluent API. + */ + template + GenericDocument& Populate(Generator& g) { + ClearStackOnExit scope(*this); + if (g(*this)) { + RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object + ValueType::operator=(*stack_.template Pop(1));// Move value from stack to document + } + return *this; + } + + //!@name Parse from stream + //!@{ + + //! Parse JSON text from an input stream (with Encoding conversion) + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam SourceEncoding Encoding of input stream + \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + GenericReader reader( + stack_.HasAllocator() ? &stack_.GetAllocator() : 0); + ClearStackOnExit scope(*this); + parseResult_ = reader.template Parse(is, *this); + if (parseResult_) { + RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object + ValueType::operator=(*stack_.template Pop(1));// Move value from stack to document + } + return *this; + } + + //! Parse JSON text from an input stream + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + return ParseStream(is); + } + + //! Parse JSON text from an input stream (with \ref kParseDefaultFlags) + /*! \tparam InputStream Type of input stream, implementing Stream concept + \param is Input stream to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseStream(InputStream& is) { + return ParseStream(is); + } + //!@} + + //!@name Parse in-place from mutable string + //!@{ + + //! Parse JSON text from a mutable string + /*! \tparam parseFlags Combination of \ref ParseFlag. + \param str Mutable zero-terminated string to be parsed. + \return The document itself for fluent API. + */ + template + GenericDocument& ParseInsitu(Ch* str) { + GenericInsituStringStream s(str); + return ParseStream(s); + } + + //! Parse JSON text from a mutable string (with \ref kParseDefaultFlags) + /*! \param str Mutable zero-terminated string to be parsed. + \return The document itself for fluent API. + */ + GenericDocument& ParseInsitu(Ch* str) { + return ParseInsitu(str); + } + //!@} + + //!@name Parse from read-only string + //!@{ + + //! Parse JSON text from a read-only string (with Encoding conversion) + /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). + \tparam SourceEncoding Transcoding from input Encoding + \param str Read-only zero-terminated string to be parsed. + */ + template + GenericDocument& Parse(const typename SourceEncoding::Ch* str) { + RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag)); + GenericStringStream s(str); + return ParseStream(s); + } + + //! Parse JSON text from a read-only string + /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). + \param str Read-only zero-terminated string to be parsed. + */ + template + GenericDocument& Parse(const Ch* str) { + return Parse(str); + } + + //! Parse JSON text from a read-only string (with \ref kParseDefaultFlags) + /*! \param str Read-only zero-terminated string to be parsed. + */ + GenericDocument& Parse(const Ch* str) { + return Parse(str); + } + + template + GenericDocument& Parse(const typename SourceEncoding::Ch* str, size_t length) { + RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag)); + MemoryStream ms(static_cast(str), length * sizeof(typename SourceEncoding::Ch)); + EncodedInputStream is(ms); + ParseStream(is); + return *this; + } + + template + GenericDocument& Parse(const Ch* str, size_t length) { + return Parse(str, length); + } + + GenericDocument& Parse(const Ch* str, size_t length) { + return Parse(str, length); + } + +#if RAPIDJSON_HAS_STDSTRING + template + GenericDocument& Parse(const std::basic_string& str) { + // c_str() is constant complexity according to standard. Should be faster than Parse(const char*, size_t) + return Parse(str.c_str()); + } + + template + GenericDocument& Parse(const std::basic_string& str) { + return Parse(str.c_str()); + } + + GenericDocument& Parse(const std::basic_string& str) { + return Parse(str); + } +#endif // RAPIDJSON_HAS_STDSTRING + + //!@} + + //!@name Handling parse errors + //!@{ + + //! Whether a parse error has occured in the last parsing. + bool HasParseError() const { return parseResult_.IsError(); } + + //! Get the \ref ParseErrorCode of last parsing. + ParseErrorCode GetParseError() const { return parseResult_.Code(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorOffset() const { return parseResult_.Offset(); } + + //! Implicit conversion to get the last parse result +#ifndef __clang // -Wdocumentation + /*! \return \ref ParseResult of the last parse operation + + \code + Document doc; + ParseResult ok = doc.Parse(json); + if (!ok) + printf( "JSON parse error: %s (%u)\n", GetParseError_En(ok.Code()), ok.Offset()); + \endcode + */ +#endif + operator ParseResult() const { return parseResult_; } + //!@} + + //! Get the allocator of this document. + Allocator& GetAllocator() { + RAPIDJSON_ASSERT(allocator_); + return *allocator_; + } + + //! Get the capacity of stack in bytes. + size_t GetStackCapacity() const { return stack_.GetCapacity(); } + +private: + // clear stack on any exit from ParseStream, e.g. due to exception + struct ClearStackOnExit { + explicit ClearStackOnExit(GenericDocument& d) : d_(d) {} + ~ClearStackOnExit() { d_.ClearStack(); } + private: + ClearStackOnExit(const ClearStackOnExit&); + ClearStackOnExit& operator=(const ClearStackOnExit&); + GenericDocument& d_; + }; + + // callers of the following private Handler functions + // template friend class GenericReader; // for parsing + template friend class GenericValue; // for deep copying + +public: + // Implementation of Handler + bool Null() { new (stack_.template Push()) ValueType(); return true; } + bool Bool(bool b) { new (stack_.template Push()) ValueType(b); return true; } + bool Int(int i) { new (stack_.template Push()) ValueType(i); return true; } + bool Uint(unsigned i) { new (stack_.template Push()) ValueType(i); return true; } + bool Int64(int64_t i) { new (stack_.template Push()) ValueType(i); return true; } + bool Uint64(uint64_t i) { new (stack_.template Push()) ValueType(i); return true; } + bool Double(double d) { new (stack_.template Push()) ValueType(d); return true; } + + bool RawNumber(const Ch* str, SizeType length, bool copy) { + if (copy) + new (stack_.template Push()) ValueType(str, length, GetAllocator()); + else + new (stack_.template Push()) ValueType(str, length); + return true; + } + + bool String(const Ch* str, SizeType length, bool copy) { + if (copy) + new (stack_.template Push()) ValueType(str, length, GetAllocator()); + else + new (stack_.template Push()) ValueType(str, length); + return true; + } + + bool StartObject() { new (stack_.template Push()) ValueType(kObjectType); return true; } + + bool Key(const Ch* str, SizeType length, bool copy) { return String(str, length, copy); } + + bool EndObject(SizeType memberCount) { + typename ValueType::Member* members = stack_.template Pop(memberCount); + stack_.template Top()->SetObjectRaw(members, memberCount, GetAllocator()); + return true; + } + + bool StartArray() { new (stack_.template Push()) ValueType(kArrayType); return true; } + + bool EndArray(SizeType elementCount) { + ValueType* elements = stack_.template Pop(elementCount); + stack_.template Top()->SetArrayRaw(elements, elementCount, GetAllocator()); + return true; + } + +private: + //! Prohibit copying + GenericDocument(const GenericDocument&); + //! Prohibit assignment + GenericDocument& operator=(const GenericDocument&); + + void ClearStack() { + if (Allocator::kNeedFree) + while (stack_.GetSize() > 0) // Here assumes all elements in stack array are GenericValue (Member is actually 2 GenericValue objects) + (stack_.template Pop(1))->~ValueType(); + else + stack_.Clear(); + stack_.ShrinkToFit(); + } + + void Destroy() { + RAPIDJSON_DELETE(ownAllocator_); + } + + static const size_t kDefaultStackCapacity = 1024; + Allocator* allocator_; + Allocator* ownAllocator_; + internal::Stack stack_; + ParseResult parseResult_; +}; + +//! GenericDocument with UTF8 encoding +typedef GenericDocument > Document; + +// defined here due to the dependency on GenericDocument +template +template +inline +GenericValue::GenericValue(const GenericValue& rhs, Allocator& allocator) +{ + switch (rhs.GetType()) { + case kObjectType: + case kArrayType: { // perform deep copy via SAX Handler + GenericDocument d(&allocator); + rhs.Accept(d); + RawAssign(*d.stack_.template Pop(1)); + } + break; + case kStringType: + if (rhs.data_.f.flags == kConstStringFlag) { + data_.f.flags = rhs.data_.f.flags; + data_ = *reinterpret_cast(&rhs.data_); + } else { + SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator); + } + break; + default: + data_.f.flags = rhs.data_.f.flags; + data_ = *reinterpret_cast(&rhs.data_); + break; + } +} + +//! Helper class for accessing Value of array type. +/*! + Instance of this helper class is obtained by \c GenericValue::GetArray(). + In addition to all APIs for array type, it provides range-based for loop if \c RAPIDJSON_HAS_CXX11_RANGE_FOR=1. +*/ +template +class GenericArray { +public: + typedef GenericArray ConstArray; + typedef GenericArray Array; + typedef ValueT PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef ValueType* ValueIterator; // This may be const or non-const iterator + typedef const ValueT* ConstValueIterator; + typedef typename ValueType::AllocatorType AllocatorType; + typedef typename ValueType::StringRefType StringRefType; + + template + friend class GenericValue; + + GenericArray(const GenericArray& rhs) : value_(rhs.value_) {} + GenericArray& operator=(const GenericArray& rhs) { value_ = rhs.value_; return *this; } + ~GenericArray() {} + + SizeType Size() const { return value_.Size(); } + SizeType Capacity() const { return value_.Capacity(); } + bool Empty() const { return value_.Empty(); } + void Clear() const { value_.Clear(); } + ValueType& operator[](SizeType index) const { return value_[index]; } + ValueIterator Begin() const { return value_.Begin(); } + ValueIterator End() const { return value_.End(); } + GenericArray Reserve(SizeType newCapacity, AllocatorType &allocator) const { value_.Reserve(newCapacity, allocator); return *this; } + GenericArray PushBack(ValueType& value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericArray PushBack(ValueType&& value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericArray PushBack(StringRefType value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (const GenericArray&)) PushBack(T value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; } + GenericArray PopBack() const { value_.PopBack(); return *this; } + ValueIterator Erase(ConstValueIterator pos) const { return value_.Erase(pos); } + ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) const { return value_.Erase(first, last); } + +#if RAPIDJSON_HAS_CXX11_RANGE_FOR + ValueIterator begin() const { return value_.Begin(); } + ValueIterator end() const { return value_.End(); } +#endif + +private: + GenericArray(); + GenericArray(ValueType& value) : value_(value) {} + ValueType& value_; +}; + +//! Helper class for accessing Value of object type. +/*! + Instance of this helper class is obtained by \c GenericValue::GetObject(). + In addition to all APIs for array type, it provides range-based for loop if \c RAPIDJSON_HAS_CXX11_RANGE_FOR=1. +*/ +template +class GenericObject { +public: + typedef GenericObject ConstObject; + typedef GenericObject Object; + typedef ValueT PlainType; + typedef typename internal::MaybeAddConst::Type ValueType; + typedef GenericMemberIterator MemberIterator; // This may be const or non-const iterator + typedef GenericMemberIterator ConstMemberIterator; + typedef typename ValueType::AllocatorType AllocatorType; + typedef typename ValueType::StringRefType StringRefType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename ValueType::Ch Ch; + + template + friend class GenericValue; + + GenericObject(const GenericObject& rhs) : value_(rhs.value_) {} + GenericObject& operator=(const GenericObject& rhs) { value_ = rhs.value_; return *this; } + ~GenericObject() {} + + SizeType MemberCount() const { return value_.MemberCount(); } + bool ObjectEmpty() const { return value_.ObjectEmpty(); } + template ValueType& operator[](T* name) const { return value_[name]; } + template ValueType& operator[](const GenericValue& name) const { return value_[name]; } +#if RAPIDJSON_HAS_STDSTRING + ValueType& operator[](const std::basic_string& name) const { return value_[name]; } +#endif + MemberIterator MemberBegin() const { return value_.MemberBegin(); } + MemberIterator MemberEnd() const { return value_.MemberEnd(); } + bool HasMember(const Ch* name) const { return value_.HasMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool HasMember(const std::basic_string& name) const { return value_.HasMember(name); } +#endif + template bool HasMember(const GenericValue& name) const { return value_.HasMember(name); } + MemberIterator FindMember(const Ch* name) const { return value_.FindMember(name); } + template MemberIterator FindMember(const GenericValue& name) const { return value_.FindMember(name); } +#if RAPIDJSON_HAS_STDSTRING + MemberIterator FindMember(const std::basic_string& name) const { return value_.FindMember(name); } +#endif + GenericObject AddMember(ValueType& name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType& name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#if RAPIDJSON_HAS_STDSTRING + GenericObject AddMember(ValueType& name, std::basic_string& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#endif + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) AddMember(ValueType& name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericObject AddMember(ValueType&& name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType&& name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(ValueType& name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(StringRefType name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericObject AddMember(StringRefType name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + GenericObject AddMember(StringRefType name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + template RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (GenericObject)) AddMember(StringRefType name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; } + void RemoveAllMembers() { return value_.RemoveAllMembers(); } + bool RemoveMember(const Ch* name) const { return value_.RemoveMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool RemoveMember(const std::basic_string& name) const { return value_.RemoveMember(name); } +#endif + template bool RemoveMember(const GenericValue& name) const { return value_.RemoveMember(name); } + MemberIterator RemoveMember(MemberIterator m) const { return value_.RemoveMember(m); } + MemberIterator EraseMember(ConstMemberIterator pos) const { return value_.EraseMember(pos); } + MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) const { return value_.EraseMember(first, last); } + bool EraseMember(const Ch* name) const { return value_.EraseMember(name); } +#if RAPIDJSON_HAS_STDSTRING + bool EraseMember(const std::basic_string& name) const { return EraseMember(ValueType(StringRef(name))); } +#endif + template bool EraseMember(const GenericValue& name) const { return value_.EraseMember(name); } + +#if RAPIDJSON_HAS_CXX11_RANGE_FOR + MemberIterator begin() const { return value_.MemberBegin(); } + MemberIterator end() const { return value_.MemberEnd(); } +#endif + +private: + GenericObject(); + GenericObject(ValueType& value) : value_(value) {} + ValueType& value_; +}; + +RAPIDJSON_NAMESPACE_END +RAPIDJSON_DIAG_POP + +#endif // RAPIDJSON_DOCUMENT_H_ diff --git a/rapidjson-1.1.0/rapid_json/encodedstream.h b/rapidjson-1.1.0/rapid_json/encodedstream.h new file mode 100644 index 000000000..145068386 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/encodedstream.h @@ -0,0 +1,299 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ENCODEDSTREAM_H_ +#define RAPIDJSON_ENCODEDSTREAM_H_ + +#include "stream.h" +#include "memorystream.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Input byte stream wrapper with a statically bound encoding. +/*! + \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. + \tparam InputByteStream Type of input byte stream. For example, FileReadStream. +*/ +template +class EncodedInputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); +public: + typedef typename Encoding::Ch Ch; + + EncodedInputStream(InputByteStream& is) : is_(is) { + current_ = Encoding::TakeBOM(is_); + } + + Ch Peek() const { return current_; } + Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; } + size_t Tell() const { return is_.Tell(); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + EncodedInputStream(const EncodedInputStream&); + EncodedInputStream& operator=(const EncodedInputStream&); + + InputByteStream& is_; + Ch current_; +}; + +//! Specialized for UTF8 MemoryStream. +template <> +class EncodedInputStream, MemoryStream> { +public: + typedef UTF8<>::Ch Ch; + + EncodedInputStream(MemoryStream& is) : is_(is) { + if (static_cast(is_.Peek()) == 0xEFu) is_.Take(); + if (static_cast(is_.Peek()) == 0xBBu) is_.Take(); + if (static_cast(is_.Peek()) == 0xBFu) is_.Take(); + } + Ch Peek() const { return is_.Peek(); } + Ch Take() { return is_.Take(); } + size_t Tell() const { return is_.Tell(); } + + // Not implemented + void Put(Ch) {} + void Flush() {} + Ch* PutBegin() { return 0; } + size_t PutEnd(Ch*) { return 0; } + + MemoryStream& is_; + +private: + EncodedInputStream(const EncodedInputStream&); + EncodedInputStream& operator=(const EncodedInputStream&); +}; + +//! Output byte stream wrapper with statically bound encoding. +/*! + \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. + \tparam OutputByteStream Type of input byte stream. For example, FileWriteStream. +*/ +template +class EncodedOutputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); +public: + typedef typename Encoding::Ch Ch; + + EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) { + if (putBOM) + Encoding::PutBOM(os_); + } + + void Put(Ch c) { Encoding::Put(os_, c); } + void Flush() { os_.Flush(); } + + // Not implemented + Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;} + Ch Take() { RAPIDJSON_ASSERT(false); return 0;} + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + EncodedOutputStream(const EncodedOutputStream&); + EncodedOutputStream& operator=(const EncodedOutputStream&); + + OutputByteStream& os_; +}; + +#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x + +//! Input stream wrapper with dynamically bound encoding and automatic encoding detection. +/*! + \tparam CharType Type of character for reading. + \tparam InputByteStream type of input byte stream to be wrapped. +*/ +template +class AutoUTFInputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); +public: + typedef CharType Ch; + + //! Constructor. + /*! + \param is input stream to be wrapped. + \param type UTF encoding type if it is not detected from the stream. + */ + AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); + DetectType(); + static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) }; + takeFunc_ = f[type_]; + current_ = takeFunc_(*is_); + } + + UTFType GetType() const { return type_; } + bool HasBOM() const { return hasBOM_; } + + Ch Peek() const { return current_; } + Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; } + size_t Tell() const { return is_->Tell(); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + AutoUTFInputStream(const AutoUTFInputStream&); + AutoUTFInputStream& operator=(const AutoUTFInputStream&); + + // Detect encoding type with BOM or RFC 4627 + void DetectType() { + // BOM (Byte Order Mark): + // 00 00 FE FF UTF-32BE + // FF FE 00 00 UTF-32LE + // FE FF UTF-16BE + // FF FE UTF-16LE + // EF BB BF UTF-8 + + const unsigned char* c = reinterpret_cast(is_->Peek4()); + if (!c) + return; + + unsigned bom = static_cast(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24)); + hasBOM_ = false; + if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } + else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } + else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); } + else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); } + else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); } + + // RFC 4627: Section 3 + // "Since the first two characters of a JSON text will always be ASCII + // characters [RFC0020], it is possible to determine whether an octet + // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking + // at the pattern of nulls in the first four octets." + // 00 00 00 xx UTF-32BE + // 00 xx 00 xx UTF-16BE + // xx 00 00 00 UTF-32LE + // xx 00 xx 00 UTF-16LE + // xx xx xx xx UTF-8 + + if (!hasBOM_) { + unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0); + switch (pattern) { + case 0x08: type_ = kUTF32BE; break; + case 0x0A: type_ = kUTF16BE; break; + case 0x01: type_ = kUTF32LE; break; + case 0x05: type_ = kUTF16LE; break; + case 0x0F: type_ = kUTF8; break; + default: break; // Use type defined by user. + } + } + + // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. + if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); + if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); + } + + typedef Ch (*TakeFunc)(InputByteStream& is); + InputByteStream* is_; + UTFType type_; + Ch current_; + TakeFunc takeFunc_; + bool hasBOM_; +}; + +//! Output stream wrapper with dynamically bound encoding and automatic encoding detection. +/*! + \tparam CharType Type of character for writing. + \tparam OutputByteStream type of output byte stream to be wrapped. +*/ +template +class AutoUTFOutputStream { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); +public: + typedef CharType Ch; + + //! Constructor. + /*! + \param os output stream to be wrapped. + \param type UTF encoding type. + \param putBOM Whether to write BOM at the beginning of the stream. + */ + AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) { + RAPIDJSON_ASSERT(type >= kUTF8 && type <= kUTF32BE); + + // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. + if (type_ == kUTF16LE || type_ == kUTF16BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 2); + if (type_ == kUTF32LE || type_ == kUTF32BE) RAPIDJSON_ASSERT(sizeof(Ch) >= 4); + + static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) }; + putFunc_ = f[type_]; + + if (putBOM) + PutBOM(); + } + + UTFType GetType() const { return type_; } + + void Put(Ch c) { putFunc_(*os_, c); } + void Flush() { os_->Flush(); } + + // Not implemented + Ch Peek() const { RAPIDJSON_ASSERT(false); return 0;} + Ch Take() { RAPIDJSON_ASSERT(false); return 0;} + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + AutoUTFOutputStream(const AutoUTFOutputStream&); + AutoUTFOutputStream& operator=(const AutoUTFOutputStream&); + + void PutBOM() { + typedef void (*PutBOMFunc)(OutputByteStream&); + static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) }; + f[type_](*os_); + } + + typedef void (*PutFunc)(OutputByteStream&, Ch); + + OutputByteStream* os_; + UTFType type_; + PutFunc putFunc_; +}; + +#undef RAPIDJSON_ENCODINGS_FUNC + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/rapidjson-1.1.0/rapid_json/encodings.h b/rapidjson-1.1.0/rapid_json/encodings.h new file mode 100644 index 000000000..baa7c2b17 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/encodings.h @@ -0,0 +1,716 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ENCODINGS_H_ +#define RAPIDJSON_ENCODINGS_H_ + +#include "rapidjson.h" + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4244) // conversion from 'type1' to 'type2', possible loss of data +RAPIDJSON_DIAG_OFF(4702) // unreachable code +#elif defined(__GNUC__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +RAPIDJSON_DIAG_OFF(overflow) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Encoding + +/*! \class rapidjson::Encoding + \brief Concept for encoding of Unicode characters. + +\code +concept Encoding { + typename Ch; //! Type of character. A "character" is actually a code unit in unicode's definition. + + enum { supportUnicode = 1 }; // or 0 if not supporting unicode + + //! \brief Encode a Unicode codepoint to an output stream. + //! \param os Output stream. + //! \param codepoint An unicode codepoint, ranging from 0x0 to 0x10FFFF inclusively. + template + static void Encode(OutputStream& os, unsigned codepoint); + + //! \brief Decode a Unicode codepoint from an input stream. + //! \param is Input stream. + //! \param codepoint Output of the unicode codepoint. + //! \return true if a valid codepoint can be decoded from the stream. + template + static bool Decode(InputStream& is, unsigned* codepoint); + + //! \brief Validate one Unicode codepoint from an encoded stream. + //! \param is Input stream to obtain codepoint. + //! \param os Output for copying one codepoint. + //! \return true if it is valid. + //! \note This function just validating and copying the codepoint without actually decode it. + template + static bool Validate(InputStream& is, OutputStream& os); + + // The following functions are deal with byte streams. + + //! Take a character from input byte stream, skip BOM if exist. + template + static CharType TakeBOM(InputByteStream& is); + + //! Take a character from input byte stream. + template + static Ch Take(InputByteStream& is); + + //! Put BOM to output byte stream. + template + static void PutBOM(OutputByteStream& os); + + //! Put a character to output byte stream. + template + static void Put(OutputByteStream& os, Ch c); +}; +\endcode +*/ + +/////////////////////////////////////////////////////////////////////////////// +// UTF8 + +//! UTF-8 encoding. +/*! http://en.wikipedia.org/wiki/UTF-8 + http://tools.ietf.org/html/rfc3629 + \tparam CharType Code unit for storing 8-bit UTF-8 data. Default is char. + \note implements Encoding concept +*/ +template +struct UTF8 { + typedef CharType Ch; + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + if (codepoint <= 0x7F) + os.Put(static_cast(codepoint & 0xFF)); + else if (codepoint <= 0x7FF) { + os.Put(static_cast(0xC0 | ((codepoint >> 6) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint & 0x3F)))); + } + else if (codepoint <= 0xFFFF) { + os.Put(static_cast(0xE0 | ((codepoint >> 12) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + os.Put(static_cast(0x80 | (codepoint & 0x3F))); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + os.Put(static_cast(0xF0 | ((codepoint >> 18) & 0xFF))); + os.Put(static_cast(0x80 | ((codepoint >> 12) & 0x3F))); + os.Put(static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + os.Put(static_cast(0x80 | (codepoint & 0x3F))); + } + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + if (codepoint <= 0x7F) + PutUnsafe(os, static_cast(codepoint & 0xFF)); + else if (codepoint <= 0x7FF) { + PutUnsafe(os, static_cast(0xC0 | ((codepoint >> 6) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint & 0x3F)))); + } + else if (codepoint <= 0xFFFF) { + PutUnsafe(os, static_cast(0xE0 | ((codepoint >> 12) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | (codepoint & 0x3F))); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + PutUnsafe(os, static_cast(0xF0 | ((codepoint >> 18) & 0xFF))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 12) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | ((codepoint >> 6) & 0x3F))); + PutUnsafe(os, static_cast(0x80 | (codepoint & 0x3F))); + } + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { +#define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | (static_cast(c) & 0x3Fu) +#define TRANS(mask) result &= ((GetRange(static_cast(c)) & mask) != 0) +#define TAIL() COPY(); TRANS(0x70) + typename InputStream::Ch c = is.Take(); + if (!(c & 0x80)) { + *codepoint = static_cast(c); + return true; + } + + unsigned char type = GetRange(static_cast(c)); + if (type >= 32) { + *codepoint = 0; + } else { + *codepoint = (0xFF >> type) & static_cast(c); + } + bool result = true; + switch (type) { + case 2: TAIL(); return result; + case 3: TAIL(); TAIL(); return result; + case 4: COPY(); TRANS(0x50); TAIL(); return result; + case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; + case 6: TAIL(); TAIL(); TAIL(); return result; + case 10: COPY(); TRANS(0x20); TAIL(); return result; + case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; + default: return false; + } +#undef COPY +#undef TRANS +#undef TAIL + } + + template + static bool Validate(InputStream& is, OutputStream& os) { +#define COPY() os.Put(c = is.Take()) +#define TRANS(mask) result &= ((GetRange(static_cast(c)) & mask) != 0) +#define TAIL() COPY(); TRANS(0x70) + Ch c; + COPY(); + if (!(c & 0x80)) + return true; + + bool result = true; + switch (GetRange(static_cast(c))) { + case 2: TAIL(); return result; + case 3: TAIL(); TAIL(); return result; + case 4: COPY(); TRANS(0x50); TAIL(); return result; + case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; + case 6: TAIL(); TAIL(); TAIL(); return result; + case 10: COPY(); TRANS(0x20); TAIL(); return result; + case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; + default: return false; + } +#undef COPY +#undef TRANS +#undef TAIL + } + + static unsigned char GetRange(unsigned char c) { + // Referring to DFA of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ + // With new mapping 1 -> 0x10, 7 -> 0x20, 9 -> 0x40, such that AND operation can test multiple types. + static const unsigned char type[] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10, + 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, + 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, + 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, + 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, + }; + return type[c]; + } + + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + typename InputByteStream::Ch c = Take(is); + if (static_cast(c) != 0xEFu) return c; + c = is.Take(); + if (static_cast(c) != 0xBBu) return c; + c = is.Take(); + if (static_cast(c) != 0xBFu) return c; + c = is.Take(); + return c; + } + + template + static Ch Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + return static_cast(is.Take()); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xEFu)); + os.Put(static_cast(0xBBu)); + os.Put(static_cast(0xBFu)); + } + + template + static void Put(OutputByteStream& os, Ch c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// UTF16 + +//! UTF-16 encoding. +/*! http://en.wikipedia.org/wiki/UTF-16 + http://tools.ietf.org/html/rfc2781 + \tparam CharType Type for storing 16-bit UTF-16 data. Default is wchar_t. C++11 may use char16_t instead. + \note implements Encoding concept + + \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. + For streaming, use UTF16LE and UTF16BE, which handle endianness. +*/ +template +struct UTF16 { + typedef CharType Ch; + RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 2); + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + if (codepoint <= 0xFFFF) { + RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair + os.Put(static_cast(codepoint)); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + unsigned v = codepoint - 0x10000; + os.Put(static_cast((v >> 10) | 0xD800)); + os.Put((v & 0x3FF) | 0xDC00); + } + } + + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + if (codepoint <= 0xFFFF) { + RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair + PutUnsafe(os, static_cast(codepoint)); + } + else { + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + unsigned v = codepoint - 0x10000; + PutUnsafe(os, static_cast((v >> 10) | 0xD800)); + PutUnsafe(os, (v & 0x3FF) | 0xDC00); + } + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); + typename InputStream::Ch c = is.Take(); + if (c < 0xD800 || c > 0xDFFF) { + *codepoint = static_cast(c); + return true; + } + else if (c <= 0xDBFF) { + *codepoint = (static_cast(c) & 0x3FF) << 10; + c = is.Take(); + *codepoint |= (static_cast(c) & 0x3FF); + *codepoint += 0x10000; + return c >= 0xDC00 && c <= 0xDFFF; + } + return false; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); + typename InputStream::Ch c; + os.Put(static_cast(c = is.Take())); + if (c < 0xD800 || c > 0xDFFF) + return true; + else if (c <= 0xDBFF) { + os.Put(c = is.Take()); + return c >= 0xDC00 && c <= 0xDFFF; + } + return false; + } +}; + +//! UTF-16 little endian encoding. +template +struct UTF16LE : UTF16 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0xFEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(is.Take()); + c |= static_cast(static_cast(is.Take())) << 8; + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFFu)); + os.Put(static_cast(0xFEu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(static_cast(c) & 0xFFu)); + os.Put(static_cast((static_cast(c) >> 8) & 0xFFu)); + } +}; + +//! UTF-16 big endian encoding. +template +struct UTF16BE : UTF16 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0xFEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(static_cast(is.Take())) << 8; + c |= static_cast(is.Take()); + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0xFFu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast((static_cast(c) >> 8) & 0xFFu)); + os.Put(static_cast(static_cast(c) & 0xFFu)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// UTF32 + +//! UTF-32 encoding. +/*! http://en.wikipedia.org/wiki/UTF-32 + \tparam CharType Type for storing 32-bit UTF-32 data. Default is unsigned. C++11 may use char32_t instead. + \note implements Encoding concept + + \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. + For streaming, use UTF32LE and UTF32BE, which handle endianness. +*/ +template +struct UTF32 { + typedef CharType Ch; + RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 4); + + enum { supportUnicode = 1 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + os.Put(codepoint); + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); + RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); + PutUnsafe(os, codepoint); + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); + Ch c = is.Take(); + *codepoint = c; + return c <= 0x10FFFF; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); + Ch c; + os.Put(c = is.Take()); + return c <= 0x10FFFF; + } +}; + +//! UTF-32 little endian enocoding. +template +struct UTF32LE : UTF32 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0x0000FEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(is.Take()); + c |= static_cast(static_cast(is.Take())) << 8; + c |= static_cast(static_cast(is.Take())) << 16; + c |= static_cast(static_cast(is.Take())) << 24; + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0xFFu)); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0x00u)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c & 0xFFu)); + os.Put(static_cast((c >> 8) & 0xFFu)); + os.Put(static_cast((c >> 16) & 0xFFu)); + os.Put(static_cast((c >> 24) & 0xFFu)); + } +}; + +//! UTF-32 big endian encoding. +template +struct UTF32BE : UTF32 { + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + CharType c = Take(is); + return static_cast(c) == 0x0000FEFFu ? Take(is) : c; + } + + template + static CharType Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + unsigned c = static_cast(static_cast(is.Take())) << 24; + c |= static_cast(static_cast(is.Take())) << 16; + c |= static_cast(static_cast(is.Take())) << 8; + c |= static_cast(static_cast(is.Take())); + return static_cast(c); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0x00u)); + os.Put(static_cast(0xFEu)); + os.Put(static_cast(0xFFu)); + } + + template + static void Put(OutputByteStream& os, CharType c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast((c >> 24) & 0xFFu)); + os.Put(static_cast((c >> 16) & 0xFFu)); + os.Put(static_cast((c >> 8) & 0xFFu)); + os.Put(static_cast(c & 0xFFu)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// ASCII + +//! ASCII encoding. +/*! http://en.wikipedia.org/wiki/ASCII + \tparam CharType Code unit for storing 7-bit ASCII data. Default is char. + \note implements Encoding concept +*/ +template +struct ASCII { + typedef CharType Ch; + + enum { supportUnicode = 0 }; + + template + static void Encode(OutputStream& os, unsigned codepoint) { + RAPIDJSON_ASSERT(codepoint <= 0x7F); + os.Put(static_cast(codepoint & 0xFF)); + } + + template + static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + RAPIDJSON_ASSERT(codepoint <= 0x7F); + PutUnsafe(os, static_cast(codepoint & 0xFF)); + } + + template + static bool Decode(InputStream& is, unsigned* codepoint) { + uint8_t c = static_cast(is.Take()); + *codepoint = c; + return c <= 0X7F; + } + + template + static bool Validate(InputStream& is, OutputStream& os) { + uint8_t c = static_cast(is.Take()); + os.Put(static_cast(c)); + return c <= 0x7F; + } + + template + static CharType TakeBOM(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + uint8_t c = static_cast(Take(is)); + return static_cast(c); + } + + template + static Ch Take(InputByteStream& is) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); + return static_cast(is.Take()); + } + + template + static void PutBOM(OutputByteStream& os) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + (void)os; + } + + template + static void Put(OutputByteStream& os, Ch c) { + RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); + os.Put(static_cast(c)); + } +}; + +/////////////////////////////////////////////////////////////////////////////// +// AutoUTF + +//! Runtime-specified UTF encoding type of a stream. +enum UTFType { + kUTF8 = 0, //!< UTF-8. + kUTF16LE = 1, //!< UTF-16 little endian. + kUTF16BE = 2, //!< UTF-16 big endian. + kUTF32LE = 3, //!< UTF-32 little endian. + kUTF32BE = 4 //!< UTF-32 big endian. +}; + +//! Dynamically select encoding according to stream's runtime-specified UTF encoding type. +/*! \note This class can be used with AutoUTFInputtStream and AutoUTFOutputStream, which provides GetType(). +*/ +template +struct AutoUTF { + typedef CharType Ch; + + enum { supportUnicode = 1 }; + +#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8::x, UTF16LE::x, UTF16BE::x, UTF32LE::x, UTF32BE::x + + template + RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoint) { + typedef void (*EncodeFunc)(OutputStream&, unsigned); + static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Encode) }; + (*f[os.GetType()])(os, codepoint); + } + + template + RAPIDJSON_FORCEINLINE static void EncodeUnsafe(OutputStream& os, unsigned codepoint) { + typedef void (*EncodeFunc)(OutputStream&, unsigned); + static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(EncodeUnsafe) }; + (*f[os.GetType()])(os, codepoint); + } + + template + RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) { + typedef bool (*DecodeFunc)(InputStream&, unsigned*); + static const DecodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Decode) }; + return (*f[is.GetType()])(is, codepoint); + } + + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + typedef bool (*ValidateFunc)(InputStream&, OutputStream&); + static const ValidateFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Validate) }; + return (*f[is.GetType()])(is, os); + } + +#undef RAPIDJSON_ENCODINGS_FUNC +}; + +/////////////////////////////////////////////////////////////////////////////// +// Transcoder + +//! Encoding conversion. +template +struct Transcoder { + //! Take one Unicode codepoint from source encoding, convert it to target encoding and put it to the output stream. + template + RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { + unsigned codepoint; + if (!SourceEncoding::Decode(is, &codepoint)) + return false; + TargetEncoding::Encode(os, codepoint); + return true; + } + + template + RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) { + unsigned codepoint; + if (!SourceEncoding::Decode(is, &codepoint)) + return false; + TargetEncoding::EncodeUnsafe(os, codepoint); + return true; + } + + //! Validate one Unicode codepoint from an encoded stream. + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + return Transcode(is, os); // Since source/target encoding is different, must transcode. + } +}; + +// Forward declaration. +template +inline void PutUnsafe(Stream& stream, typename Stream::Ch c); + +//! Specialization of Transcoder with same source and target encoding. +template +struct Transcoder { + template + RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { + os.Put(is.Take()); // Just copy one code unit. This semantic is different from primary template class. + return true; + } + + template + RAPIDJSON_FORCEINLINE static bool TranscodeUnsafe(InputStream& is, OutputStream& os) { + PutUnsafe(os, is.Take()); // Just copy one code unit. This semantic is different from primary template class. + return true; + } + + template + RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { + return Encoding::Validate(is, os); // source/target encoding are the same + } +}; + +RAPIDJSON_NAMESPACE_END + +#if defined(__GNUC__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ENCODINGS_H_ diff --git a/rapidjson-1.1.0/rapid_json/error/en.h b/rapidjson-1.1.0/rapid_json/error/en.h new file mode 100644 index 000000000..2db838bff --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/error/en.h @@ -0,0 +1,74 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ERROR_EN_H_ +#define RAPIDJSON_ERROR_EN_H_ + +#include "error.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(switch-enum) +RAPIDJSON_DIAG_OFF(covered-switch-default) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Maps error code of parsing into error message. +/*! + \ingroup RAPIDJSON_ERRORS + \param parseErrorCode Error code obtained in parsing. + \return the error message. + \note User can make a copy of this function for localization. + Using switch-case is safer for future modification of error codes. +*/ +inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErrorCode) { + switch (parseErrorCode) { + case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error."); + + case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty."); + case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not be followed by other values."); + + case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value."); + + case kParseErrorObjectMissName: return RAPIDJSON_ERROR_STRING("Missing a name for object member."); + case kParseErrorObjectMissColon: return RAPIDJSON_ERROR_STRING("Missing a colon after a name of object member."); + case kParseErrorObjectMissCommaOrCurlyBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or '}' after an object member."); + + case kParseErrorArrayMissCommaOrSquareBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or ']' after an array element."); + + case kParseErrorStringUnicodeEscapeInvalidHex: return RAPIDJSON_ERROR_STRING("Incorrect hex digit after \\u escape in string."); + case kParseErrorStringUnicodeSurrogateInvalid: return RAPIDJSON_ERROR_STRING("The surrogate pair in string is invalid."); + case kParseErrorStringEscapeInvalid: return RAPIDJSON_ERROR_STRING("Invalid escape character in string."); + case kParseErrorStringMissQuotationMark: return RAPIDJSON_ERROR_STRING("Missing a closing quotation mark in string."); + case kParseErrorStringInvalidEncoding: return RAPIDJSON_ERROR_STRING("Invalid encoding in string."); + + case kParseErrorNumberTooBig: return RAPIDJSON_ERROR_STRING("Number too big to be stored in double."); + case kParseErrorNumberMissFraction: return RAPIDJSON_ERROR_STRING("Miss fraction part in number."); + case kParseErrorNumberMissExponent: return RAPIDJSON_ERROR_STRING("Miss exponent in number."); + + case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error."); + case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error."); + + default: return RAPIDJSON_ERROR_STRING("Unknown error."); + } +} + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ERROR_EN_H_ diff --git a/rapidjson-1.1.0/rapid_json/error/error.h b/rapidjson-1.1.0/rapid_json/error/error.h new file mode 100644 index 000000000..95cb31a72 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/error/error.h @@ -0,0 +1,155 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ERROR_ERROR_H_ +#define RAPIDJSON_ERROR_ERROR_H_ + +#include "../rapidjson.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +/*! \file error.h */ + +/*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */ + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ERROR_CHARTYPE + +//! Character type of error messages. +/*! \ingroup RAPIDJSON_ERRORS + The default character type is \c char. + On Windows, user can define this macro as \c TCHAR for supporting both + unicode/non-unicode settings. +*/ +#ifndef RAPIDJSON_ERROR_CHARTYPE +#define RAPIDJSON_ERROR_CHARTYPE char +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ERROR_STRING + +//! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[]. +/*! \ingroup RAPIDJSON_ERRORS + By default this conversion macro does nothing. + On Windows, user can define this macro as \c _T(x) for supporting both + unicode/non-unicode settings. +*/ +#ifndef RAPIDJSON_ERROR_STRING +#define RAPIDJSON_ERROR_STRING(x) x +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// ParseErrorCode + +//! Error code of parsing. +/*! \ingroup RAPIDJSON_ERRORS + \see GenericReader::Parse, GenericReader::GetParseErrorCode +*/ +enum ParseErrorCode { + kParseErrorNone = 0, //!< No error. + + kParseErrorDocumentEmpty, //!< The document is empty. + kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values. + + kParseErrorValueInvalid, //!< Invalid value. + + kParseErrorObjectMissName, //!< Missing a name for object member. + kParseErrorObjectMissColon, //!< Missing a colon after a name of object member. + kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member. + + kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element. + + kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string. + kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid. + kParseErrorStringEscapeInvalid, //!< Invalid escape character in string. + kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string. + kParseErrorStringInvalidEncoding, //!< Invalid encoding in string. + + kParseErrorNumberTooBig, //!< Number too big to be stored in double. + kParseErrorNumberMissFraction, //!< Miss fraction part in number. + kParseErrorNumberMissExponent, //!< Miss exponent in number. + + kParseErrorTermination, //!< Parsing was terminated. + kParseErrorUnspecificSyntaxError //!< Unspecific syntax error. +}; + +//! Result of parsing (wraps ParseErrorCode) +/*! + \ingroup RAPIDJSON_ERRORS + \code + Document doc; + ParseResult ok = doc.Parse("[42]"); + if (!ok) { + fprintf(stderr, "JSON parse error: %s (%u)", + GetParseError_En(ok.Code()), ok.Offset()); + exit(EXIT_FAILURE); + } + \endcode + \see GenericReader::Parse, GenericDocument::Parse +*/ +struct ParseResult { +public: + //! Default constructor, no error. + ParseResult() : code_(kParseErrorNone), offset_(0) {} + //! Constructor to set an error. + ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {} + + //! Get the error code. + ParseErrorCode Code() const { return code_; } + //! Get the error offset, if \ref IsError(), 0 otherwise. + size_t Offset() const { return offset_; } + + //! Conversion to \c bool, returns \c true, iff !\ref IsError(). + operator bool() const { return !IsError(); } + //! Whether the result is an error. + bool IsError() const { return code_ != kParseErrorNone; } + + bool operator==(const ParseResult& that) const { return code_ == that.code_; } + bool operator==(ParseErrorCode code) const { return code_ == code; } + friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; } + + //! Reset error code. + void Clear() { Set(kParseErrorNone); } + //! Update error code and offset. + void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; } + +private: + ParseErrorCode code_; + size_t offset_; +}; + +//! Function pointer type of GetParseError(). +/*! \ingroup RAPIDJSON_ERRORS + + This is the prototype for \c GetParseError_X(), where \c X is a locale. + User can dynamically change locale in runtime, e.g.: +\code + GetParseErrorFunc GetParseError = GetParseError_En; // or whatever + const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode()); +\endcode +*/ +typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode); + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_ERROR_ERROR_H_ diff --git a/rapidjson-1.1.0/rapid_json/filereadstream.h b/rapidjson-1.1.0/rapid_json/filereadstream.h new file mode 100644 index 000000000..b56ea13b3 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/filereadstream.h @@ -0,0 +1,99 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FILEREADSTREAM_H_ +#define RAPIDJSON_FILEREADSTREAM_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(unreachable-code) +RAPIDJSON_DIAG_OFF(missing-noreturn) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! File byte stream for input using fread(). +/*! + \note implements Stream concept +*/ +class FileReadStream { +public: + typedef char Ch; //!< Character type (byte). + + //! Constructor. + /*! + \param fp File pointer opened for read. + \param buffer user-supplied buffer. + \param bufferSize size of buffer in bytes. Must >=4 bytes. + */ + FileReadStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { + RAPIDJSON_ASSERT(fp_ != 0); + RAPIDJSON_ASSERT(bufferSize >= 4); + Read(); + } + + Ch Peek() const { return *current_; } + Ch Take() { Ch c = *current_; Read(); return c; } + size_t Tell() const { return count_ + static_cast(current_ - buffer_); } + + // Not implemented + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + return (current_ + 4 <= bufferLast_) ? current_ : 0; + } + +private: + void Read() { + if (current_ < bufferLast_) + ++current_; + else if (!eof_) { + count_ += readCount_; + readCount_ = fread(buffer_, 1, bufferSize_, fp_); + bufferLast_ = buffer_ + readCount_ - 1; + current_ = buffer_; + + if (readCount_ < bufferSize_) { + buffer_[readCount_] = '\0'; + ++bufferLast_; + eof_ = true; + } + } + } + + std::FILE* fp_; + Ch *buffer_; + size_t bufferSize_; + Ch *bufferLast_; + Ch *current_; + size_t readCount_; + size_t count_; //!< Number of characters read + bool eof_; +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/rapidjson-1.1.0/rapid_json/filewritestream.h b/rapidjson-1.1.0/rapid_json/filewritestream.h new file mode 100644 index 000000000..6378dd60e --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/filewritestream.h @@ -0,0 +1,104 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FILEWRITESTREAM_H_ +#define RAPIDJSON_FILEWRITESTREAM_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(unreachable-code) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of C file stream for input using fread(). +/*! + \note implements Stream concept +*/ +class FileWriteStream { +public: + typedef char Ch; //!< Character type. Only support char. + + FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { + RAPIDJSON_ASSERT(fp_ != 0); + } + + void Put(char c) { + if (current_ >= bufferEnd_) + Flush(); + + *current_++ = c; + } + + void PutN(char c, size_t n) { + size_t avail = static_cast(bufferEnd_ - current_); + while (n > avail) { + std::memset(current_, c, avail); + current_ += avail; + Flush(); + n -= avail; + avail = static_cast(bufferEnd_ - current_); + } + + if (n > 0) { + std::memset(current_, c, n); + current_ += n; + } + } + + void Flush() { + if (current_ != buffer_) { + size_t result = fwrite(buffer_, 1, static_cast(current_ - buffer_), fp_); + if (result < static_cast(current_ - buffer_)) { + // failure deliberately ignored at this time + // added to avoid warn_unused_result build errors + } + current_ = buffer_; + } + } + + // Not implemented + char Peek() const { RAPIDJSON_ASSERT(false); return 0; } + char Take() { RAPIDJSON_ASSERT(false); return 0; } + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + // Prohibit copy constructor & assignment operator. + FileWriteStream(const FileWriteStream&); + FileWriteStream& operator=(const FileWriteStream&); + + std::FILE* fp_; + char *buffer_; + char *bufferEnd_; + char *current_; +}; + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(FileWriteStream& stream, char c, size_t n) { + stream.PutN(c, n); +} + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_FILESTREAM_H_ diff --git a/rapidjson-1.1.0/rapid_json/fwd.h b/rapidjson-1.1.0/rapid_json/fwd.h new file mode 100644 index 000000000..e8104e841 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/fwd.h @@ -0,0 +1,151 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_FWD_H_ +#define RAPIDJSON_FWD_H_ + +#include "rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN + +// encodings.h + +template struct UTF8; +template struct UTF16; +template struct UTF16BE; +template struct UTF16LE; +template struct UTF32; +template struct UTF32BE; +template struct UTF32LE; +template struct ASCII; +template struct AutoUTF; + +template +struct Transcoder; + +// allocators.h + +class CrtAllocator; + +template +class MemoryPoolAllocator; + +// stream.h + +template +struct GenericStringStream; + +typedef GenericStringStream > StringStream; + +template +struct GenericInsituStringStream; + +typedef GenericInsituStringStream > InsituStringStream; + +// stringbuffer.h + +template +class GenericStringBuffer; + +typedef GenericStringBuffer, CrtAllocator> StringBuffer; + +// filereadstream.h + +class FileReadStream; + +// filewritestream.h + +class FileWriteStream; + +// memorybuffer.h + +template +struct GenericMemoryBuffer; + +typedef GenericMemoryBuffer MemoryBuffer; + +// memorystream.h + +struct MemoryStream; + +// reader.h + +template +struct BaseReaderHandler; + +template +class GenericReader; + +typedef GenericReader, UTF8, CrtAllocator> Reader; + +// writer.h + +template +class Writer; + +// prettywriter.h + +template +class PrettyWriter; + +// document.h + +template +struct GenericMember; + +template +class GenericMemberIterator; + +template +struct GenericStringRef; + +template +class GenericValue; + +typedef GenericValue, MemoryPoolAllocator > Value; + +template +class GenericDocument; + +typedef GenericDocument, MemoryPoolAllocator, CrtAllocator> Document; + +// pointer.h + +template +class GenericPointer; + +typedef GenericPointer Pointer; + +// schema.h + +template +class IGenericRemoteSchemaDocumentProvider; + +template +class GenericSchemaDocument; + +typedef GenericSchemaDocument SchemaDocument; +typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProvider; + +template < + typename SchemaDocumentType, + typename OutputHandler, + typename StateAllocator> +class GenericSchemaValidator; + +typedef GenericSchemaValidator, void>, CrtAllocator> SchemaValidator; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_RAPIDJSONFWD_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/biginteger.h b/rapidjson-1.1.0/rapid_json/internal/biginteger.h new file mode 100644 index 000000000..9d3e88c99 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/biginteger.h @@ -0,0 +1,290 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_BIGINTEGER_H_ +#define RAPIDJSON_BIGINTEGER_H_ + +#include "../rapidjson.h" + +#if defined(_MSC_VER) && defined(_M_AMD64) +#include // for _umul128 +#pragma intrinsic(_umul128) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +class BigInteger { +public: + typedef uint64_t Type; + + BigInteger(const BigInteger& rhs) : count_(rhs.count_) { + std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); + } + + explicit BigInteger(uint64_t u) : count_(1) { + digits_[0] = u; + } + + BigInteger(const char* decimals, size_t length) : count_(1) { + RAPIDJSON_ASSERT(length > 0); + digits_[0] = 0; + size_t i = 0; + const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19 + while (length >= kMaxDigitPerIteration) { + AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration); + length -= kMaxDigitPerIteration; + i += kMaxDigitPerIteration; + } + + if (length > 0) + AppendDecimal64(decimals + i, decimals + i + length); + } + + BigInteger& operator=(const BigInteger &rhs) + { + if (this != &rhs) { + count_ = rhs.count_; + std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); + } + return *this; + } + + BigInteger& operator=(uint64_t u) { + digits_[0] = u; + count_ = 1; + return *this; + } + + BigInteger& operator+=(uint64_t u) { + Type backup = digits_[0]; + digits_[0] += u; + for (size_t i = 0; i < count_ - 1; i++) { + if (digits_[i] >= backup) + return *this; // no carry + backup = digits_[i + 1]; + digits_[i + 1] += 1; + } + + // Last carry + if (digits_[count_ - 1] < backup) + PushBack(1); + + return *this; + } + + BigInteger& operator*=(uint64_t u) { + if (u == 0) return *this = 0; + if (u == 1) return *this; + if (*this == 1) return *this = u; + + uint64_t k = 0; + for (size_t i = 0; i < count_; i++) { + uint64_t hi; + digits_[i] = MulAdd64(digits_[i], u, k, &hi); + k = hi; + } + + if (k > 0) + PushBack(k); + + return *this; + } + + BigInteger& operator*=(uint32_t u) { + if (u == 0) return *this = 0; + if (u == 1) return *this; + if (*this == 1) return *this = u; + + uint64_t k = 0; + for (size_t i = 0; i < count_; i++) { + const uint64_t c = digits_[i] >> 32; + const uint64_t d = digits_[i] & 0xFFFFFFFF; + const uint64_t uc = u * c; + const uint64_t ud = u * d; + const uint64_t p0 = ud + k; + const uint64_t p1 = uc + (p0 >> 32); + digits_[i] = (p0 & 0xFFFFFFFF) | (p1 << 32); + k = p1 >> 32; + } + + if (k > 0) + PushBack(k); + + return *this; + } + + BigInteger& operator<<=(size_t shift) { + if (IsZero() || shift == 0) return *this; + + size_t offset = shift / kTypeBit; + size_t interShift = shift % kTypeBit; + RAPIDJSON_ASSERT(count_ + offset <= kCapacity); + + if (interShift == 0) { + std::memmove(&digits_[count_ - 1 + offset], &digits_[count_ - 1], count_ * sizeof(Type)); + count_ += offset; + } + else { + digits_[count_] = 0; + for (size_t i = count_; i > 0; i--) + digits_[i + offset] = (digits_[i] << interShift) | (digits_[i - 1] >> (kTypeBit - interShift)); + digits_[offset] = digits_[0] << interShift; + count_ += offset; + if (digits_[count_]) + count_++; + } + + std::memset(digits_, 0, offset * sizeof(Type)); + + return *this; + } + + bool operator==(const BigInteger& rhs) const { + return count_ == rhs.count_ && std::memcmp(digits_, rhs.digits_, count_ * sizeof(Type)) == 0; + } + + bool operator==(const Type rhs) const { + return count_ == 1 && digits_[0] == rhs; + } + + BigInteger& MultiplyPow5(unsigned exp) { + static const uint32_t kPow5[12] = { + 5, + 5 * 5, + 5 * 5 * 5, + 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, + 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 + }; + if (exp == 0) return *this; + for (; exp >= 27; exp -= 27) *this *= RAPIDJSON_UINT64_C2(0X6765C793, 0XFA10079D); // 5^27 + for (; exp >= 13; exp -= 13) *this *= static_cast(1220703125u); // 5^13 + if (exp > 0) *this *= kPow5[exp - 1]; + return *this; + } + + // Compute absolute difference of this and rhs. + // Assume this != rhs + bool Difference(const BigInteger& rhs, BigInteger* out) const { + int cmp = Compare(rhs); + RAPIDJSON_ASSERT(cmp != 0); + const BigInteger *a, *b; // Makes a > b + bool ret; + if (cmp < 0) { a = &rhs; b = this; ret = true; } + else { a = this; b = &rhs; ret = false; } + + Type borrow = 0; + for (size_t i = 0; i < a->count_; i++) { + Type d = a->digits_[i] - borrow; + if (i < b->count_) + d -= b->digits_[i]; + borrow = (d > a->digits_[i]) ? 1 : 0; + out->digits_[i] = d; + if (d != 0) + out->count_ = i + 1; + } + + return ret; + } + + int Compare(const BigInteger& rhs) const { + if (count_ != rhs.count_) + return count_ < rhs.count_ ? -1 : 1; + + for (size_t i = count_; i-- > 0;) + if (digits_[i] != rhs.digits_[i]) + return digits_[i] < rhs.digits_[i] ? -1 : 1; + + return 0; + } + + size_t GetCount() const { return count_; } + Type GetDigit(size_t index) const { RAPIDJSON_ASSERT(index < count_); return digits_[index]; } + bool IsZero() const { return count_ == 1 && digits_[0] == 0; } + +private: + void AppendDecimal64(const char* begin, const char* end) { + uint64_t u = ParseUint64(begin, end); + if (IsZero()) + *this = u; + else { + unsigned exp = static_cast(end - begin); + (MultiplyPow5(exp) <<= exp) += u; // *this = *this * 10^exp + u + } + } + + void PushBack(Type digit) { + RAPIDJSON_ASSERT(count_ < kCapacity); + digits_[count_++] = digit; + } + + static uint64_t ParseUint64(const char* begin, const char* end) { + uint64_t r = 0; + for (const char* p = begin; p != end; ++p) { + RAPIDJSON_ASSERT(*p >= '0' && *p <= '9'); + r = r * 10u + static_cast(*p - '0'); + } + return r; + } + + // Assume a * b + k < 2^128 + static uint64_t MulAdd64(uint64_t a, uint64_t b, uint64_t k, uint64_t* outHigh) { +#if defined(_MSC_VER) && defined(_M_AMD64) + uint64_t low = _umul128(a, b, outHigh) + k; + if (low < k) + (*outHigh)++; + return low; +#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) + __extension__ typedef unsigned __int128 uint128; + uint128 p = static_cast(a) * static_cast(b); + p += k; + *outHigh = static_cast(p >> 64); + return static_cast(p); +#else + const uint64_t a0 = a & 0xFFFFFFFF, a1 = a >> 32, b0 = b & 0xFFFFFFFF, b1 = b >> 32; + uint64_t x0 = a0 * b0, x1 = a0 * b1, x2 = a1 * b0, x3 = a1 * b1; + x1 += (x0 >> 32); // can't give carry + x1 += x2; + if (x1 < x2) + x3 += (static_cast(1) << 32); + uint64_t lo = (x1 << 32) + (x0 & 0xFFFFFFFF); + uint64_t hi = x3 + (x1 >> 32); + + lo += k; + if (lo < k) + hi++; + *outHigh = hi; + return lo; +#endif + } + + static const size_t kBitCount = 3328; // 64bit * 54 > 10^1000 + static const size_t kCapacity = kBitCount / sizeof(Type); + static const size_t kTypeBit = sizeof(Type) * 8; + + Type digits_[kCapacity]; + size_t count_; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_BIGINTEGER_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/diyfp.h b/rapidjson-1.1.0/rapid_json/internal/diyfp.h new file mode 100644 index 000000000..c9fefdc61 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/diyfp.h @@ -0,0 +1,258 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +// This is a C++ header-only implementation of Grisu2 algorithm from the publication: +// Loitsch, Florian. "Printing floating-point numbers quickly and accurately with +// integers." ACM Sigplan Notices 45.6 (2010): 233-243. + +#ifndef RAPIDJSON_DIYFP_H_ +#define RAPIDJSON_DIYFP_H_ + +#include "../rapidjson.h" + +#if defined(_MSC_VER) && defined(_M_AMD64) +#include +#pragma intrinsic(_BitScanReverse64) +#pragma intrinsic(_umul128) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +struct DiyFp { + DiyFp() : f(), e() {} + + DiyFp(uint64_t fp, int exp) : f(fp), e(exp) {} + + explicit DiyFp(double d) { + union { + double d; + uint64_t u64; + } u = { d }; + + int biased_e = static_cast((u.u64 & kDpExponentMask) >> kDpSignificandSize); + uint64_t significand = (u.u64 & kDpSignificandMask); + if (biased_e != 0) { + f = significand + kDpHiddenBit; + e = biased_e - kDpExponentBias; + } + else { + f = significand; + e = kDpMinExponent + 1; + } + } + + DiyFp operator-(const DiyFp& rhs) const { + return DiyFp(f - rhs.f, e); + } + + DiyFp operator*(const DiyFp& rhs) const { +#if defined(_MSC_VER) && defined(_M_AMD64) + uint64_t h; + uint64_t l = _umul128(f, rhs.f, &h); + if (l & (uint64_t(1) << 63)) // rounding + h++; + return DiyFp(h, e + rhs.e + 64); +#elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && defined(__x86_64__) + __extension__ typedef unsigned __int128 uint128; + uint128 p = static_cast(f) * static_cast(rhs.f); + uint64_t h = static_cast(p >> 64); + uint64_t l = static_cast(p); + if (l & (uint64_t(1) << 63)) // rounding + h++; + return DiyFp(h, e + rhs.e + 64); +#else + const uint64_t M32 = 0xFFFFFFFF; + const uint64_t a = f >> 32; + const uint64_t b = f & M32; + const uint64_t c = rhs.f >> 32; + const uint64_t d = rhs.f & M32; + const uint64_t ac = a * c; + const uint64_t bc = b * c; + const uint64_t ad = a * d; + const uint64_t bd = b * d; + uint64_t tmp = (bd >> 32) + (ad & M32) + (bc & M32); + tmp += 1U << 31; /// mult_round + return DiyFp(ac + (ad >> 32) + (bc >> 32) + (tmp >> 32), e + rhs.e + 64); +#endif + } + + DiyFp Normalize() const { +#if defined(_MSC_VER) && defined(_M_AMD64) + unsigned long index; + _BitScanReverse64(&index, f); + return DiyFp(f << (63 - index), e - (63 - index)); +#elif defined(__GNUC__) && __GNUC__ >= 4 + int s = __builtin_clzll(f); + return DiyFp(f << s, e - s); +#else + DiyFp res = *this; + while (!(res.f & (static_cast(1) << 63))) { + res.f <<= 1; + res.e--; + } + return res; +#endif + } + + DiyFp NormalizeBoundary() const { + DiyFp res = *this; + while (!(res.f & (kDpHiddenBit << 1))) { + res.f <<= 1; + res.e--; + } + res.f <<= (kDiySignificandSize - kDpSignificandSize - 2); + res.e = res.e - (kDiySignificandSize - kDpSignificandSize - 2); + return res; + } + + void NormalizedBoundaries(DiyFp* minus, DiyFp* plus) const { + DiyFp pl = DiyFp((f << 1) + 1, e - 1).NormalizeBoundary(); + DiyFp mi = (f == kDpHiddenBit) ? DiyFp((f << 2) - 1, e - 2) : DiyFp((f << 1) - 1, e - 1); + mi.f <<= mi.e - pl.e; + mi.e = pl.e; + *plus = pl; + *minus = mi; + } + + double ToDouble() const { + union { + double d; + uint64_t u64; + }u; + const uint64_t be = (e == kDpDenormalExponent && (f & kDpHiddenBit) == 0) ? 0 : + static_cast(e + kDpExponentBias); + u.u64 = (f & kDpSignificandMask) | (be << kDpSignificandSize); + return u.d; + } + + static const int kDiySignificandSize = 64; + static const int kDpSignificandSize = 52; + static const int kDpExponentBias = 0x3FF + kDpSignificandSize; + static const int kDpMaxExponent = 0x7FF - kDpExponentBias; + static const int kDpMinExponent = -kDpExponentBias; + static const int kDpDenormalExponent = -kDpExponentBias + 1; + static const uint64_t kDpExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); + static const uint64_t kDpSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); + static const uint64_t kDpHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); + + uint64_t f; + int e; +}; + +inline DiyFp GetCachedPowerByIndex(size_t index) { + // 10^-348, 10^-340, ..., 10^340 + static const uint64_t kCachedPowers_F[] = { + RAPIDJSON_UINT64_C2(0xfa8fd5a0, 0x081c0288), RAPIDJSON_UINT64_C2(0xbaaee17f, 0xa23ebf76), + RAPIDJSON_UINT64_C2(0x8b16fb20, 0x3055ac76), RAPIDJSON_UINT64_C2(0xcf42894a, 0x5dce35ea), + RAPIDJSON_UINT64_C2(0x9a6bb0aa, 0x55653b2d), RAPIDJSON_UINT64_C2(0xe61acf03, 0x3d1a45df), + RAPIDJSON_UINT64_C2(0xab70fe17, 0xc79ac6ca), RAPIDJSON_UINT64_C2(0xff77b1fc, 0xbebcdc4f), + RAPIDJSON_UINT64_C2(0xbe5691ef, 0x416bd60c), RAPIDJSON_UINT64_C2(0x8dd01fad, 0x907ffc3c), + RAPIDJSON_UINT64_C2(0xd3515c28, 0x31559a83), RAPIDJSON_UINT64_C2(0x9d71ac8f, 0xada6c9b5), + RAPIDJSON_UINT64_C2(0xea9c2277, 0x23ee8bcb), RAPIDJSON_UINT64_C2(0xaecc4991, 0x4078536d), + RAPIDJSON_UINT64_C2(0x823c1279, 0x5db6ce57), RAPIDJSON_UINT64_C2(0xc2109436, 0x4dfb5637), + RAPIDJSON_UINT64_C2(0x9096ea6f, 0x3848984f), RAPIDJSON_UINT64_C2(0xd77485cb, 0x25823ac7), + RAPIDJSON_UINT64_C2(0xa086cfcd, 0x97bf97f4), RAPIDJSON_UINT64_C2(0xef340a98, 0x172aace5), + RAPIDJSON_UINT64_C2(0xb23867fb, 0x2a35b28e), RAPIDJSON_UINT64_C2(0x84c8d4df, 0xd2c63f3b), + RAPIDJSON_UINT64_C2(0xc5dd4427, 0x1ad3cdba), RAPIDJSON_UINT64_C2(0x936b9fce, 0xbb25c996), + RAPIDJSON_UINT64_C2(0xdbac6c24, 0x7d62a584), RAPIDJSON_UINT64_C2(0xa3ab6658, 0x0d5fdaf6), + RAPIDJSON_UINT64_C2(0xf3e2f893, 0xdec3f126), RAPIDJSON_UINT64_C2(0xb5b5ada8, 0xaaff80b8), + RAPIDJSON_UINT64_C2(0x87625f05, 0x6c7c4a8b), RAPIDJSON_UINT64_C2(0xc9bcff60, 0x34c13053), + RAPIDJSON_UINT64_C2(0x964e858c, 0x91ba2655), RAPIDJSON_UINT64_C2(0xdff97724, 0x70297ebd), + RAPIDJSON_UINT64_C2(0xa6dfbd9f, 0xb8e5b88f), RAPIDJSON_UINT64_C2(0xf8a95fcf, 0x88747d94), + RAPIDJSON_UINT64_C2(0xb9447093, 0x8fa89bcf), RAPIDJSON_UINT64_C2(0x8a08f0f8, 0xbf0f156b), + RAPIDJSON_UINT64_C2(0xcdb02555, 0x653131b6), RAPIDJSON_UINT64_C2(0x993fe2c6, 0xd07b7fac), + RAPIDJSON_UINT64_C2(0xe45c10c4, 0x2a2b3b06), RAPIDJSON_UINT64_C2(0xaa242499, 0x697392d3), + RAPIDJSON_UINT64_C2(0xfd87b5f2, 0x8300ca0e), RAPIDJSON_UINT64_C2(0xbce50864, 0x92111aeb), + RAPIDJSON_UINT64_C2(0x8cbccc09, 0x6f5088cc), RAPIDJSON_UINT64_C2(0xd1b71758, 0xe219652c), + RAPIDJSON_UINT64_C2(0x9c400000, 0x00000000), RAPIDJSON_UINT64_C2(0xe8d4a510, 0x00000000), + RAPIDJSON_UINT64_C2(0xad78ebc5, 0xac620000), RAPIDJSON_UINT64_C2(0x813f3978, 0xf8940984), + RAPIDJSON_UINT64_C2(0xc097ce7b, 0xc90715b3), RAPIDJSON_UINT64_C2(0x8f7e32ce, 0x7bea5c70), + RAPIDJSON_UINT64_C2(0xd5d238a4, 0xabe98068), RAPIDJSON_UINT64_C2(0x9f4f2726, 0x179a2245), + RAPIDJSON_UINT64_C2(0xed63a231, 0xd4c4fb27), RAPIDJSON_UINT64_C2(0xb0de6538, 0x8cc8ada8), + RAPIDJSON_UINT64_C2(0x83c7088e, 0x1aab65db), RAPIDJSON_UINT64_C2(0xc45d1df9, 0x42711d9a), + RAPIDJSON_UINT64_C2(0x924d692c, 0xa61be758), RAPIDJSON_UINT64_C2(0xda01ee64, 0x1a708dea), + RAPIDJSON_UINT64_C2(0xa26da399, 0x9aef774a), RAPIDJSON_UINT64_C2(0xf209787b, 0xb47d6b85), + RAPIDJSON_UINT64_C2(0xb454e4a1, 0x79dd1877), RAPIDJSON_UINT64_C2(0x865b8692, 0x5b9bc5c2), + RAPIDJSON_UINT64_C2(0xc83553c5, 0xc8965d3d), RAPIDJSON_UINT64_C2(0x952ab45c, 0xfa97a0b3), + RAPIDJSON_UINT64_C2(0xde469fbd, 0x99a05fe3), RAPIDJSON_UINT64_C2(0xa59bc234, 0xdb398c25), + RAPIDJSON_UINT64_C2(0xf6c69a72, 0xa3989f5c), RAPIDJSON_UINT64_C2(0xb7dcbf53, 0x54e9bece), + RAPIDJSON_UINT64_C2(0x88fcf317, 0xf22241e2), RAPIDJSON_UINT64_C2(0xcc20ce9b, 0xd35c78a5), + RAPIDJSON_UINT64_C2(0x98165af3, 0x7b2153df), RAPIDJSON_UINT64_C2(0xe2a0b5dc, 0x971f303a), + RAPIDJSON_UINT64_C2(0xa8d9d153, 0x5ce3b396), RAPIDJSON_UINT64_C2(0xfb9b7cd9, 0xa4a7443c), + RAPIDJSON_UINT64_C2(0xbb764c4c, 0xa7a44410), RAPIDJSON_UINT64_C2(0x8bab8eef, 0xb6409c1a), + RAPIDJSON_UINT64_C2(0xd01fef10, 0xa657842c), RAPIDJSON_UINT64_C2(0x9b10a4e5, 0xe9913129), + RAPIDJSON_UINT64_C2(0xe7109bfb, 0xa19c0c9d), RAPIDJSON_UINT64_C2(0xac2820d9, 0x623bf429), + RAPIDJSON_UINT64_C2(0x80444b5e, 0x7aa7cf85), RAPIDJSON_UINT64_C2(0xbf21e440, 0x03acdd2d), + RAPIDJSON_UINT64_C2(0x8e679c2f, 0x5e44ff8f), RAPIDJSON_UINT64_C2(0xd433179d, 0x9c8cb841), + RAPIDJSON_UINT64_C2(0x9e19db92, 0xb4e31ba9), RAPIDJSON_UINT64_C2(0xeb96bf6e, 0xbadf77d9), + RAPIDJSON_UINT64_C2(0xaf87023b, 0x9bf0ee6b) + }; + static const int16_t kCachedPowers_E[] = { + -1220, -1193, -1166, -1140, -1113, -1087, -1060, -1034, -1007, -980, + -954, -927, -901, -874, -847, -821, -794, -768, -741, -715, + -688, -661, -635, -608, -582, -555, -529, -502, -475, -449, + -422, -396, -369, -343, -316, -289, -263, -236, -210, -183, + -157, -130, -103, -77, -50, -24, 3, 30, 56, 83, + 109, 136, 162, 189, 216, 242, 269, 295, 322, 348, + 375, 402, 428, 455, 481, 508, 534, 561, 588, 614, + 641, 667, 694, 720, 747, 774, 800, 827, 853, 880, + 907, 933, 960, 986, 1013, 1039, 1066 + }; + return DiyFp(kCachedPowers_F[index], kCachedPowers_E[index]); +} + +inline DiyFp GetCachedPower(int e, int* K) { + + //int k = static_cast(ceil((-61 - e) * 0.30102999566398114)) + 374; + double dk = (-61 - e) * 0.30102999566398114 + 347; // dk must be positive, so can do ceiling in positive + int k = static_cast(dk); + if (dk - k > 0.0) + k++; + + unsigned index = static_cast((k >> 3) + 1); + *K = -(-348 + static_cast(index << 3)); // decimal exponent no need lookup table + + return GetCachedPowerByIndex(index); +} + +inline DiyFp GetCachedPower10(int exp, int *outExp) { + unsigned index = (static_cast(exp) + 348u) / 8u; + *outExp = -348 + static_cast(index) * 8; + return GetCachedPowerByIndex(index); + } + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +RAPIDJSON_DIAG_OFF(padded) +#endif + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_DIYFP_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/dtoa.h b/rapidjson-1.1.0/rapid_json/internal/dtoa.h new file mode 100644 index 000000000..8d6350e62 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/dtoa.h @@ -0,0 +1,245 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +// This is a C++ header-only implementation of Grisu2 algorithm from the publication: +// Loitsch, Florian. "Printing floating-point numbers quickly and accurately with +// integers." ACM Sigplan Notices 45.6 (2010): 233-243. + +#ifndef RAPIDJSON_DTOA_ +#define RAPIDJSON_DTOA_ + +#include "itoa.h" // GetDigitsLut() +#include "diyfp.h" +#include "ieee754.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +RAPIDJSON_DIAG_OFF(array-bounds) // some gcc versions generate wrong warnings https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59124 +#endif + +inline void GrisuRound(char* buffer, int len, uint64_t delta, uint64_t rest, uint64_t ten_kappa, uint64_t wp_w) { + while (rest < wp_w && delta - rest >= ten_kappa && + (rest + ten_kappa < wp_w || /// closer + wp_w - rest > rest + ten_kappa - wp_w)) { + buffer[len - 1]--; + rest += ten_kappa; + } +} + +inline unsigned CountDecimalDigit32(uint32_t n) { + // Simple pure C++ implementation was faster than __builtin_clz version in this situation. + if (n < 10) return 1; + if (n < 100) return 2; + if (n < 1000) return 3; + if (n < 10000) return 4; + if (n < 100000) return 5; + if (n < 1000000) return 6; + if (n < 10000000) return 7; + if (n < 100000000) return 8; + // Will not reach 10 digits in DigitGen() + //if (n < 1000000000) return 9; + //return 10; + return 9; +} + +inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) { + static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; + const DiyFp one(uint64_t(1) << -Mp.e, Mp.e); + const DiyFp wp_w = Mp - W; + uint32_t p1 = static_cast(Mp.f >> -one.e); + uint64_t p2 = Mp.f & (one.f - 1); + unsigned kappa = CountDecimalDigit32(p1); // kappa in [0, 9] + *len = 0; + + while (kappa > 0) { + uint32_t d = 0; + switch (kappa) { + case 9: d = p1 / 100000000; p1 %= 100000000; break; + case 8: d = p1 / 10000000; p1 %= 10000000; break; + case 7: d = p1 / 1000000; p1 %= 1000000; break; + case 6: d = p1 / 100000; p1 %= 100000; break; + case 5: d = p1 / 10000; p1 %= 10000; break; + case 4: d = p1 / 1000; p1 %= 1000; break; + case 3: d = p1 / 100; p1 %= 100; break; + case 2: d = p1 / 10; p1 %= 10; break; + case 1: d = p1; p1 = 0; break; + default:; + } + if (d || *len) + buffer[(*len)++] = static_cast('0' + static_cast(d)); + kappa--; + uint64_t tmp = (static_cast(p1) << -one.e) + p2; + if (tmp <= delta) { + *K += kappa; + GrisuRound(buffer, *len, delta, tmp, static_cast(kPow10[kappa]) << -one.e, wp_w.f); + return; + } + } + + // kappa = 0 + for (;;) { + p2 *= 10; + delta *= 10; + char d = static_cast(p2 >> -one.e); + if (d || *len) + buffer[(*len)++] = static_cast('0' + d); + p2 &= one.f - 1; + kappa--; + if (p2 < delta) { + *K += kappa; + int index = -static_cast(kappa); + GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[-static_cast(kappa)] : 0)); + return; + } + } +} + +inline void Grisu2(double value, char* buffer, int* length, int* K) { + const DiyFp v(value); + DiyFp w_m, w_p; + v.NormalizedBoundaries(&w_m, &w_p); + + const DiyFp c_mk = GetCachedPower(w_p.e, K); + const DiyFp W = v.Normalize() * c_mk; + DiyFp Wp = w_p * c_mk; + DiyFp Wm = w_m * c_mk; + Wm.f++; + Wp.f--; + DigitGen(W, Wp, Wp.f - Wm.f, buffer, length, K); +} + +inline char* WriteExponent(int K, char* buffer) { + if (K < 0) { + *buffer++ = '-'; + K = -K; + } + + if (K >= 100) { + *buffer++ = static_cast('0' + static_cast(K / 100)); + K %= 100; + const char* d = GetDigitsLut() + K * 2; + *buffer++ = d[0]; + *buffer++ = d[1]; + } + else if (K >= 10) { + const char* d = GetDigitsLut() + K * 2; + *buffer++ = d[0]; + *buffer++ = d[1]; + } + else + *buffer++ = static_cast('0' + static_cast(K)); + + return buffer; +} + +inline char* Prettify(char* buffer, int length, int k, int maxDecimalPlaces) { + const int kk = length + k; // 10^(kk-1) <= v < 10^kk + + if (0 <= k && kk <= 21) { + // 1234e7 -> 12340000000 + for (int i = length; i < kk; i++) + buffer[i] = '0'; + buffer[kk] = '.'; + buffer[kk + 1] = '0'; + return &buffer[kk + 2]; + } + else if (0 < kk && kk <= 21) { + // 1234e-2 -> 12.34 + std::memmove(&buffer[kk + 1], &buffer[kk], static_cast(length - kk)); + buffer[kk] = '.'; + if (0 > k + maxDecimalPlaces) { + // When maxDecimalPlaces = 2, 1.2345 -> 1.23, 1.102 -> 1.1 + // Remove extra trailing zeros (at least one) after truncation. + for (int i = kk + maxDecimalPlaces; i > kk + 1; i--) + if (buffer[i] != '0') + return &buffer[i + 1]; + return &buffer[kk + 2]; // Reserve one zero + } + else + return &buffer[length + 1]; + } + else if (-6 < kk && kk <= 0) { + // 1234e-6 -> 0.001234 + const int offset = 2 - kk; + std::memmove(&buffer[offset], &buffer[0], static_cast(length)); + buffer[0] = '0'; + buffer[1] = '.'; + for (int i = 2; i < offset; i++) + buffer[i] = '0'; + if (length - kk > maxDecimalPlaces) { + // When maxDecimalPlaces = 2, 0.123 -> 0.12, 0.102 -> 0.1 + // Remove extra trailing zeros (at least one) after truncation. + for (int i = maxDecimalPlaces + 1; i > 2; i--) + if (buffer[i] != '0') + return &buffer[i + 1]; + return &buffer[3]; // Reserve one zero + } + else + return &buffer[length + offset]; + } + else if (kk < -maxDecimalPlaces) { + // Truncate to zero + buffer[0] = '0'; + buffer[1] = '.'; + buffer[2] = '0'; + return &buffer[3]; + } + else if (length == 1) { + // 1e30 + buffer[1] = 'e'; + return WriteExponent(kk - 1, &buffer[2]); + } + else { + // 1234e30 -> 1.234e33 + std::memmove(&buffer[2], &buffer[1], static_cast(length - 1)); + buffer[1] = '.'; + buffer[length + 1] = 'e'; + return WriteExponent(kk - 1, &buffer[0 + length + 2]); + } +} + +inline char* dtoa(double value, char* buffer, int maxDecimalPlaces = 324) { + RAPIDJSON_ASSERT(maxDecimalPlaces >= 1); + Double d(value); + if (d.IsZero()) { + if (d.Sign()) + *buffer++ = '-'; // -0.0, Issue #289 + buffer[0] = '0'; + buffer[1] = '.'; + buffer[2] = '0'; + return &buffer[3]; + } + else { + if (value < 0) { + *buffer++ = '-'; + value = -value; + } + int length, K; + Grisu2(value, buffer, &length, &K); + return Prettify(buffer, length, K, maxDecimalPlaces); + } +} + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_DTOA_ diff --git a/rapidjson-1.1.0/rapid_json/internal/ieee754.h b/rapidjson-1.1.0/rapid_json/internal/ieee754.h new file mode 100644 index 000000000..82bb0b99e --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/ieee754.h @@ -0,0 +1,78 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_IEEE754_ +#define RAPIDJSON_IEEE754_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +class Double { +public: + Double() {} + Double(double d) : d_(d) {} + Double(uint64_t u) : u_(u) {} + + double Value() const { return d_; } + uint64_t Uint64Value() const { return u_; } + + double NextPositiveDouble() const { + RAPIDJSON_ASSERT(!Sign()); + return Double(u_ + 1).Value(); + } + + bool Sign() const { return (u_ & kSignMask) != 0; } + uint64_t Significand() const { return u_ & kSignificandMask; } + int Exponent() const { return static_cast(((u_ & kExponentMask) >> kSignificandSize) - kExponentBias); } + + bool IsNan() const { return (u_ & kExponentMask) == kExponentMask && Significand() != 0; } + bool IsInf() const { return (u_ & kExponentMask) == kExponentMask && Significand() == 0; } + bool IsNanOrInf() const { return (u_ & kExponentMask) == kExponentMask; } + bool IsNormal() const { return (u_ & kExponentMask) != 0 || Significand() == 0; } + bool IsZero() const { return (u_ & (kExponentMask | kSignificandMask)) == 0; } + + uint64_t IntegerSignificand() const { return IsNormal() ? Significand() | kHiddenBit : Significand(); } + int IntegerExponent() const { return (IsNormal() ? Exponent() : kDenormalExponent) - kSignificandSize; } + uint64_t ToBias() const { return (u_ & kSignMask) ? ~u_ + 1 : u_ | kSignMask; } + + static unsigned EffectiveSignificandSize(int order) { + if (order >= -1021) + return 53; + else if (order <= -1074) + return 0; + else + return static_cast(order) + 1074; + } + +private: + static const int kSignificandSize = 52; + static const int kExponentBias = 0x3FF; + static const int kDenormalExponent = 1 - kExponentBias; + static const uint64_t kSignMask = RAPIDJSON_UINT64_C2(0x80000000, 0x00000000); + static const uint64_t kExponentMask = RAPIDJSON_UINT64_C2(0x7FF00000, 0x00000000); + static const uint64_t kSignificandMask = RAPIDJSON_UINT64_C2(0x000FFFFF, 0xFFFFFFFF); + static const uint64_t kHiddenBit = RAPIDJSON_UINT64_C2(0x00100000, 0x00000000); + + union { + double d_; + uint64_t u_; + }; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_IEEE754_ diff --git a/rapidjson-1.1.0/rapid_json/internal/itoa.h b/rapidjson-1.1.0/rapid_json/internal/itoa.h new file mode 100644 index 000000000..01a4e7e72 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/itoa.h @@ -0,0 +1,304 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ITOA_ +#define RAPIDJSON_ITOA_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +inline const char* GetDigitsLut() { + static const char cDigitsLut[200] = { + '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9', + '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9', + '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9', + '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9', + '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9', + '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9', + '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9', + '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9', + '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9', + '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9' + }; + return cDigitsLut; +} + +inline char* u32toa(uint32_t value, char* buffer) { + const char* cDigitsLut = GetDigitsLut(); + + if (value < 10000) { + const uint32_t d1 = (value / 100) << 1; + const uint32_t d2 = (value % 100) << 1; + + if (value >= 1000) + *buffer++ = cDigitsLut[d1]; + if (value >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else if (value < 100000000) { + // value = bbbbcccc + const uint32_t b = value / 10000; + const uint32_t c = value % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + else { + // value = aabbbbcccc in decimal + + const uint32_t a = value / 100000000; // 1 to 42 + value %= 100000000; + + if (a >= 10) { + const unsigned i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else + *buffer++ = static_cast('0' + static_cast(a)); + + const uint32_t b = value / 10000; // 0 to 9999 + const uint32_t c = value % 10000; // 0 to 9999 + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + return buffer; +} + +inline char* i32toa(int32_t value, char* buffer) { + uint32_t u = static_cast(value); + if (value < 0) { + *buffer++ = '-'; + u = ~u + 1; + } + + return u32toa(u, buffer); +} + +inline char* u64toa(uint64_t value, char* buffer) { + const char* cDigitsLut = GetDigitsLut(); + const uint64_t kTen8 = 100000000; + const uint64_t kTen9 = kTen8 * 10; + const uint64_t kTen10 = kTen8 * 100; + const uint64_t kTen11 = kTen8 * 1000; + const uint64_t kTen12 = kTen8 * 10000; + const uint64_t kTen13 = kTen8 * 100000; + const uint64_t kTen14 = kTen8 * 1000000; + const uint64_t kTen15 = kTen8 * 10000000; + const uint64_t kTen16 = kTen8 * kTen8; + + if (value < kTen8) { + uint32_t v = static_cast(value); + if (v < 10000) { + const uint32_t d1 = (v / 100) << 1; + const uint32_t d2 = (v % 100) << 1; + + if (v >= 1000) + *buffer++ = cDigitsLut[d1]; + if (v >= 100) + *buffer++ = cDigitsLut[d1 + 1]; + if (v >= 10) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + } + else { + // value = bbbbcccc + const uint32_t b = v / 10000; + const uint32_t c = v % 10000; + + const uint32_t d1 = (b / 100) << 1; + const uint32_t d2 = (b % 100) << 1; + + const uint32_t d3 = (c / 100) << 1; + const uint32_t d4 = (c % 100) << 1; + + if (value >= 10000000) + *buffer++ = cDigitsLut[d1]; + if (value >= 1000000) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= 100000) + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + } + } + else if (value < kTen16) { + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + if (value >= kTen15) + *buffer++ = cDigitsLut[d1]; + if (value >= kTen14) + *buffer++ = cDigitsLut[d1 + 1]; + if (value >= kTen13) + *buffer++ = cDigitsLut[d2]; + if (value >= kTen12) + *buffer++ = cDigitsLut[d2 + 1]; + if (value >= kTen11) + *buffer++ = cDigitsLut[d3]; + if (value >= kTen10) + *buffer++ = cDigitsLut[d3 + 1]; + if (value >= kTen9) + *buffer++ = cDigitsLut[d4]; + if (value >= kTen8) + *buffer++ = cDigitsLut[d4 + 1]; + + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + else { + const uint32_t a = static_cast(value / kTen16); // 1 to 1844 + value %= kTen16; + + if (a < 10) + *buffer++ = static_cast('0' + static_cast(a)); + else if (a < 100) { + const uint32_t i = a << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else if (a < 1000) { + *buffer++ = static_cast('0' + static_cast(a / 100)); + + const uint32_t i = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + } + else { + const uint32_t i = (a / 100) << 1; + const uint32_t j = (a % 100) << 1; + *buffer++ = cDigitsLut[i]; + *buffer++ = cDigitsLut[i + 1]; + *buffer++ = cDigitsLut[j]; + *buffer++ = cDigitsLut[j + 1]; + } + + const uint32_t v0 = static_cast(value / kTen8); + const uint32_t v1 = static_cast(value % kTen8); + + const uint32_t b0 = v0 / 10000; + const uint32_t c0 = v0 % 10000; + + const uint32_t d1 = (b0 / 100) << 1; + const uint32_t d2 = (b0 % 100) << 1; + + const uint32_t d3 = (c0 / 100) << 1; + const uint32_t d4 = (c0 % 100) << 1; + + const uint32_t b1 = v1 / 10000; + const uint32_t c1 = v1 % 10000; + + const uint32_t d5 = (b1 / 100) << 1; + const uint32_t d6 = (b1 % 100) << 1; + + const uint32_t d7 = (c1 / 100) << 1; + const uint32_t d8 = (c1 % 100) << 1; + + *buffer++ = cDigitsLut[d1]; + *buffer++ = cDigitsLut[d1 + 1]; + *buffer++ = cDigitsLut[d2]; + *buffer++ = cDigitsLut[d2 + 1]; + *buffer++ = cDigitsLut[d3]; + *buffer++ = cDigitsLut[d3 + 1]; + *buffer++ = cDigitsLut[d4]; + *buffer++ = cDigitsLut[d4 + 1]; + *buffer++ = cDigitsLut[d5]; + *buffer++ = cDigitsLut[d5 + 1]; + *buffer++ = cDigitsLut[d6]; + *buffer++ = cDigitsLut[d6 + 1]; + *buffer++ = cDigitsLut[d7]; + *buffer++ = cDigitsLut[d7 + 1]; + *buffer++ = cDigitsLut[d8]; + *buffer++ = cDigitsLut[d8 + 1]; + } + + return buffer; +} + +inline char* i64toa(int64_t value, char* buffer) { + uint64_t u = static_cast(value); + if (value < 0) { + *buffer++ = '-'; + u = ~u + 1; + } + + return u64toa(u, buffer); +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ITOA_ diff --git a/rapidjson-1.1.0/rapid_json/internal/meta.h b/rapidjson-1.1.0/rapid_json/internal/meta.h new file mode 100644 index 000000000..5a9aaa428 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/meta.h @@ -0,0 +1,181 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_META_H_ +#define RAPIDJSON_INTERNAL_META_H_ + +#include "../rapidjson.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif +#if defined(_MSC_VER) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(6334) +#endif + +#if RAPIDJSON_HAS_CXX11_TYPETRAITS +#include +#endif + +//@cond RAPIDJSON_INTERNAL +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +// Helper to wrap/convert arbitrary types to void, useful for arbitrary type matching +template struct Void { typedef void Type; }; + +/////////////////////////////////////////////////////////////////////////////// +// BoolType, TrueType, FalseType +// +template struct BoolType { + static const bool Value = Cond; + typedef BoolType Type; +}; +typedef BoolType TrueType; +typedef BoolType FalseType; + + +/////////////////////////////////////////////////////////////////////////////// +// SelectIf, BoolExpr, NotExpr, AndExpr, OrExpr +// + +template struct SelectIfImpl { template struct Apply { typedef T1 Type; }; }; +template <> struct SelectIfImpl { template struct Apply { typedef T2 Type; }; }; +template struct SelectIfCond : SelectIfImpl::template Apply {}; +template struct SelectIf : SelectIfCond {}; + +template struct AndExprCond : FalseType {}; +template <> struct AndExprCond : TrueType {}; +template struct OrExprCond : TrueType {}; +template <> struct OrExprCond : FalseType {}; + +template struct BoolExpr : SelectIf::Type {}; +template struct NotExpr : SelectIf::Type {}; +template struct AndExpr : AndExprCond::Type {}; +template struct OrExpr : OrExprCond::Type {}; + + +/////////////////////////////////////////////////////////////////////////////// +// AddConst, MaybeAddConst, RemoveConst +template struct AddConst { typedef const T Type; }; +template struct MaybeAddConst : SelectIfCond {}; +template struct RemoveConst { typedef T Type; }; +template struct RemoveConst { typedef T Type; }; + + +/////////////////////////////////////////////////////////////////////////////// +// IsSame, IsConst, IsMoreConst, IsPointer +// +template struct IsSame : FalseType {}; +template struct IsSame : TrueType {}; + +template struct IsConst : FalseType {}; +template struct IsConst : TrueType {}; + +template +struct IsMoreConst + : AndExpr::Type, typename RemoveConst::Type>, + BoolType::Value >= IsConst::Value> >::Type {}; + +template struct IsPointer : FalseType {}; +template struct IsPointer : TrueType {}; + +/////////////////////////////////////////////////////////////////////////////// +// IsBaseOf +// +#if RAPIDJSON_HAS_CXX11_TYPETRAITS + +template struct IsBaseOf + : BoolType< ::std::is_base_of::value> {}; + +#else // simplified version adopted from Boost + +template struct IsBaseOfImpl { + RAPIDJSON_STATIC_ASSERT(sizeof(B) != 0); + RAPIDJSON_STATIC_ASSERT(sizeof(D) != 0); + + typedef char (&Yes)[1]; + typedef char (&No) [2]; + + template + static Yes Check(const D*, T); + static No Check(const B*, int); + + struct Host { + operator const B*() const; + operator const D*(); + }; + + enum { Value = (sizeof(Check(Host(), 0)) == sizeof(Yes)) }; +}; + +template struct IsBaseOf + : OrExpr, BoolExpr > >::Type {}; + +#endif // RAPIDJSON_HAS_CXX11_TYPETRAITS + + +////////////////////////////////////////////////////////////////////////// +// EnableIf / DisableIf +// +template struct EnableIfCond { typedef T Type; }; +template struct EnableIfCond { /* empty */ }; + +template struct DisableIfCond { typedef T Type; }; +template struct DisableIfCond { /* empty */ }; + +template +struct EnableIf : EnableIfCond {}; + +template +struct DisableIf : DisableIfCond {}; + +// SFINAE helpers +struct SfinaeTag {}; +template struct RemoveSfinaeTag; +template struct RemoveSfinaeTag { typedef T Type; }; + +#define RAPIDJSON_REMOVEFPTR_(type) \ + typename ::RAPIDJSON_NAMESPACE::internal::RemoveSfinaeTag \ + < ::RAPIDJSON_NAMESPACE::internal::SfinaeTag&(*) type>::Type + +#define RAPIDJSON_ENABLEIF(cond) \ + typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ + ::Type * = NULL + +#define RAPIDJSON_DISABLEIF(cond) \ + typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ + ::Type * = NULL + +#define RAPIDJSON_ENABLEIF_RETURN(cond,returntype) \ + typename ::RAPIDJSON_NAMESPACE::internal::EnableIf \ + ::Type + +#define RAPIDJSON_DISABLEIF_RETURN(cond,returntype) \ + typename ::RAPIDJSON_NAMESPACE::internal::DisableIf \ + ::Type + +} // namespace internal +RAPIDJSON_NAMESPACE_END +//@endcond + +#if defined(__GNUC__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_META_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/pow10.h b/rapidjson-1.1.0/rapid_json/internal/pow10.h new file mode 100644 index 000000000..02f475d70 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/pow10.h @@ -0,0 +1,55 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_POW10_ +#define RAPIDJSON_POW10_ + +#include "../rapidjson.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Computes integer powers of 10 in double (10.0^n). +/*! This function uses lookup table for fast and accurate results. + \param n non-negative exponent. Must <= 308. + \return 10.0^n +*/ +inline double Pow10(int n) { + static const double e[] = { // 1e-0...1e308: 309 * 8 bytes = 2472 bytes + 1e+0, + 1e+1, 1e+2, 1e+3, 1e+4, 1e+5, 1e+6, 1e+7, 1e+8, 1e+9, 1e+10, 1e+11, 1e+12, 1e+13, 1e+14, 1e+15, 1e+16, 1e+17, 1e+18, 1e+19, 1e+20, + 1e+21, 1e+22, 1e+23, 1e+24, 1e+25, 1e+26, 1e+27, 1e+28, 1e+29, 1e+30, 1e+31, 1e+32, 1e+33, 1e+34, 1e+35, 1e+36, 1e+37, 1e+38, 1e+39, 1e+40, + 1e+41, 1e+42, 1e+43, 1e+44, 1e+45, 1e+46, 1e+47, 1e+48, 1e+49, 1e+50, 1e+51, 1e+52, 1e+53, 1e+54, 1e+55, 1e+56, 1e+57, 1e+58, 1e+59, 1e+60, + 1e+61, 1e+62, 1e+63, 1e+64, 1e+65, 1e+66, 1e+67, 1e+68, 1e+69, 1e+70, 1e+71, 1e+72, 1e+73, 1e+74, 1e+75, 1e+76, 1e+77, 1e+78, 1e+79, 1e+80, + 1e+81, 1e+82, 1e+83, 1e+84, 1e+85, 1e+86, 1e+87, 1e+88, 1e+89, 1e+90, 1e+91, 1e+92, 1e+93, 1e+94, 1e+95, 1e+96, 1e+97, 1e+98, 1e+99, 1e+100, + 1e+101,1e+102,1e+103,1e+104,1e+105,1e+106,1e+107,1e+108,1e+109,1e+110,1e+111,1e+112,1e+113,1e+114,1e+115,1e+116,1e+117,1e+118,1e+119,1e+120, + 1e+121,1e+122,1e+123,1e+124,1e+125,1e+126,1e+127,1e+128,1e+129,1e+130,1e+131,1e+132,1e+133,1e+134,1e+135,1e+136,1e+137,1e+138,1e+139,1e+140, + 1e+141,1e+142,1e+143,1e+144,1e+145,1e+146,1e+147,1e+148,1e+149,1e+150,1e+151,1e+152,1e+153,1e+154,1e+155,1e+156,1e+157,1e+158,1e+159,1e+160, + 1e+161,1e+162,1e+163,1e+164,1e+165,1e+166,1e+167,1e+168,1e+169,1e+170,1e+171,1e+172,1e+173,1e+174,1e+175,1e+176,1e+177,1e+178,1e+179,1e+180, + 1e+181,1e+182,1e+183,1e+184,1e+185,1e+186,1e+187,1e+188,1e+189,1e+190,1e+191,1e+192,1e+193,1e+194,1e+195,1e+196,1e+197,1e+198,1e+199,1e+200, + 1e+201,1e+202,1e+203,1e+204,1e+205,1e+206,1e+207,1e+208,1e+209,1e+210,1e+211,1e+212,1e+213,1e+214,1e+215,1e+216,1e+217,1e+218,1e+219,1e+220, + 1e+221,1e+222,1e+223,1e+224,1e+225,1e+226,1e+227,1e+228,1e+229,1e+230,1e+231,1e+232,1e+233,1e+234,1e+235,1e+236,1e+237,1e+238,1e+239,1e+240, + 1e+241,1e+242,1e+243,1e+244,1e+245,1e+246,1e+247,1e+248,1e+249,1e+250,1e+251,1e+252,1e+253,1e+254,1e+255,1e+256,1e+257,1e+258,1e+259,1e+260, + 1e+261,1e+262,1e+263,1e+264,1e+265,1e+266,1e+267,1e+268,1e+269,1e+270,1e+271,1e+272,1e+273,1e+274,1e+275,1e+276,1e+277,1e+278,1e+279,1e+280, + 1e+281,1e+282,1e+283,1e+284,1e+285,1e+286,1e+287,1e+288,1e+289,1e+290,1e+291,1e+292,1e+293,1e+294,1e+295,1e+296,1e+297,1e+298,1e+299,1e+300, + 1e+301,1e+302,1e+303,1e+304,1e+305,1e+306,1e+307,1e+308 + }; + RAPIDJSON_ASSERT(n >= 0 && n <= 308); + return e[n]; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_POW10_ diff --git a/rapidjson-1.1.0/rapid_json/internal/regex.h b/rapidjson-1.1.0/rapid_json/internal/regex.h new file mode 100644 index 000000000..422a5240b --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/regex.h @@ -0,0 +1,701 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_REGEX_H_ +#define RAPIDJSON_INTERNAL_REGEX_H_ + +#include "../allocators.h" +#include "../stream.h" +#include "stack.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +RAPIDJSON_DIAG_OFF(implicit-fallthrough) +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +#ifndef RAPIDJSON_REGEX_VERBOSE +#define RAPIDJSON_REGEX_VERBOSE 0 +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +/////////////////////////////////////////////////////////////////////////////// +// GenericRegex + +static const SizeType kRegexInvalidState = ~SizeType(0); //!< Represents an invalid index in GenericRegex::State::out, out1 +static const SizeType kRegexInvalidRange = ~SizeType(0); + +//! Regular expression engine with subset of ECMAscript grammar. +/*! + Supported regular expression syntax: + - \c ab Concatenation + - \c a|b Alternation + - \c a? Zero or one + - \c a* Zero or more + - \c a+ One or more + - \c a{3} Exactly 3 times + - \c a{3,} At least 3 times + - \c a{3,5} 3 to 5 times + - \c (ab) Grouping + - \c ^a At the beginning + - \c a$ At the end + - \c . Any character + - \c [abc] Character classes + - \c [a-c] Character class range + - \c [a-z0-9_] Character class combination + - \c [^abc] Negated character classes + - \c [^a-c] Negated character class range + - \c [\b] Backspace (U+0008) + - \c \\| \\\\ ... Escape characters + - \c \\f Form feed (U+000C) + - \c \\n Line feed (U+000A) + - \c \\r Carriage return (U+000D) + - \c \\t Tab (U+0009) + - \c \\v Vertical tab (U+000B) + + \note This is a Thompson NFA engine, implemented with reference to + Cox, Russ. "Regular Expression Matching Can Be Simple And Fast (but is slow in Java, Perl, PHP, Python, Ruby,...).", + https://swtch.com/~rsc/regexp/regexp1.html +*/ +template +class GenericRegex { +public: + typedef typename Encoding::Ch Ch; + + GenericRegex(const Ch* source, Allocator* allocator = 0) : + states_(allocator, 256), ranges_(allocator, 256), root_(kRegexInvalidState), stateCount_(), rangeCount_(), + stateSet_(), state0_(allocator, 0), state1_(allocator, 0), anchorBegin_(), anchorEnd_() + { + GenericStringStream ss(source); + DecodedStream > ds(ss); + Parse(ds); + } + + ~GenericRegex() { + Allocator::Free(stateSet_); + } + + bool IsValid() const { + return root_ != kRegexInvalidState; + } + + template + bool Match(InputStream& is) const { + return SearchWithAnchoring(is, true, true); + } + + bool Match(const Ch* s) const { + GenericStringStream is(s); + return Match(is); + } + + template + bool Search(InputStream& is) const { + return SearchWithAnchoring(is, anchorBegin_, anchorEnd_); + } + + bool Search(const Ch* s) const { + GenericStringStream is(s); + return Search(is); + } + +private: + enum Operator { + kZeroOrOne, + kZeroOrMore, + kOneOrMore, + kConcatenation, + kAlternation, + kLeftParenthesis + }; + + static const unsigned kAnyCharacterClass = 0xFFFFFFFF; //!< For '.' + static const unsigned kRangeCharacterClass = 0xFFFFFFFE; + static const unsigned kRangeNegationFlag = 0x80000000; + + struct Range { + unsigned start; // + unsigned end; + SizeType next; + }; + + struct State { + SizeType out; //!< Equals to kInvalid for matching state + SizeType out1; //!< Equals to non-kInvalid for split + SizeType rangeStart; + unsigned codepoint; + }; + + struct Frag { + Frag(SizeType s, SizeType o, SizeType m) : start(s), out(o), minIndex(m) {} + SizeType start; + SizeType out; //!< link-list of all output states + SizeType minIndex; + }; + + template + class DecodedStream { + public: + DecodedStream(SourceStream& ss) : ss_(ss), codepoint_() { Decode(); } + unsigned Peek() { return codepoint_; } + unsigned Take() { + unsigned c = codepoint_; + if (c) // No further decoding when '\0' + Decode(); + return c; + } + + private: + void Decode() { + if (!Encoding::Decode(ss_, &codepoint_)) + codepoint_ = 0; + } + + SourceStream& ss_; + unsigned codepoint_; + }; + + State& GetState(SizeType index) { + RAPIDJSON_ASSERT(index < stateCount_); + return states_.template Bottom()[index]; + } + + const State& GetState(SizeType index) const { + RAPIDJSON_ASSERT(index < stateCount_); + return states_.template Bottom()[index]; + } + + Range& GetRange(SizeType index) { + RAPIDJSON_ASSERT(index < rangeCount_); + return ranges_.template Bottom()[index]; + } + + const Range& GetRange(SizeType index) const { + RAPIDJSON_ASSERT(index < rangeCount_); + return ranges_.template Bottom()[index]; + } + + template + void Parse(DecodedStream& ds) { + Allocator allocator; + Stack operandStack(&allocator, 256); // Frag + Stack operatorStack(&allocator, 256); // Operator + Stack atomCountStack(&allocator, 256); // unsigned (Atom per parenthesis) + + *atomCountStack.template Push() = 0; + + unsigned codepoint; + while (ds.Peek() != 0) { + switch (codepoint = ds.Take()) { + case '^': + anchorBegin_ = true; + break; + + case '$': + anchorEnd_ = true; + break; + + case '|': + while (!operatorStack.Empty() && *operatorStack.template Top() < kAlternation) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + *operatorStack.template Push() = kAlternation; + *atomCountStack.template Top() = 0; + break; + + case '(': + *operatorStack.template Push() = kLeftParenthesis; + *atomCountStack.template Push() = 0; + break; + + case ')': + while (!operatorStack.Empty() && *operatorStack.template Top() != kLeftParenthesis) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + if (operatorStack.Empty()) + return; + operatorStack.template Pop(1); + atomCountStack.template Pop(1); + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '?': + if (!Eval(operandStack, kZeroOrOne)) + return; + break; + + case '*': + if (!Eval(operandStack, kZeroOrMore)) + return; + break; + + case '+': + if (!Eval(operandStack, kOneOrMore)) + return; + break; + + case '{': + { + unsigned n, m; + if (!ParseUnsigned(ds, &n)) + return; + + if (ds.Peek() == ',') { + ds.Take(); + if (ds.Peek() == '}') + m = kInfinityQuantifier; + else if (!ParseUnsigned(ds, &m) || m < n) + return; + } + else + m = n; + + if (!EvalQuantifier(operandStack, n, m) || ds.Peek() != '}') + return; + ds.Take(); + } + break; + + case '.': + PushOperand(operandStack, kAnyCharacterClass); + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '[': + { + SizeType range; + if (!ParseRange(ds, &range)) + return; + SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, kRangeCharacterClass); + GetState(s).rangeStart = range; + *operandStack.template Push() = Frag(s, s, s); + } + ImplicitConcatenation(atomCountStack, operatorStack); + break; + + case '\\': // Escape character + if (!CharacterEscape(ds, &codepoint)) + return; // Unsupported escape character + // fall through to default + + default: // Pattern character + PushOperand(operandStack, codepoint); + ImplicitConcatenation(atomCountStack, operatorStack); + } + } + + while (!operatorStack.Empty()) + if (!Eval(operandStack, *operatorStack.template Pop(1))) + return; + + // Link the operand to matching state. + if (operandStack.GetSize() == sizeof(Frag)) { + Frag* e = operandStack.template Pop(1); + Patch(e->out, NewState(kRegexInvalidState, kRegexInvalidState, 0)); + root_ = e->start; + +#if RAPIDJSON_REGEX_VERBOSE + printf("root: %d\n", root_); + for (SizeType i = 0; i < stateCount_ ; i++) { + State& s = GetState(i); + printf("[%2d] out: %2d out1: %2d c: '%c'\n", i, s.out, s.out1, (char)s.codepoint); + } + printf("\n"); +#endif + } + + // Preallocate buffer for SearchWithAnchoring() + RAPIDJSON_ASSERT(stateSet_ == 0); + if (stateCount_ > 0) { + stateSet_ = static_cast(states_.GetAllocator().Malloc(GetStateSetSize())); + state0_.template Reserve(stateCount_); + state1_.template Reserve(stateCount_); + } + } + + SizeType NewState(SizeType out, SizeType out1, unsigned codepoint) { + State* s = states_.template Push(); + s->out = out; + s->out1 = out1; + s->codepoint = codepoint; + s->rangeStart = kRegexInvalidRange; + return stateCount_++; + } + + void PushOperand(Stack& operandStack, unsigned codepoint) { + SizeType s = NewState(kRegexInvalidState, kRegexInvalidState, codepoint); + *operandStack.template Push() = Frag(s, s, s); + } + + void ImplicitConcatenation(Stack& atomCountStack, Stack& operatorStack) { + if (*atomCountStack.template Top()) + *operatorStack.template Push() = kConcatenation; + (*atomCountStack.template Top())++; + } + + SizeType Append(SizeType l1, SizeType l2) { + SizeType old = l1; + while (GetState(l1).out != kRegexInvalidState) + l1 = GetState(l1).out; + GetState(l1).out = l2; + return old; + } + + void Patch(SizeType l, SizeType s) { + for (SizeType next; l != kRegexInvalidState; l = next) { + next = GetState(l).out; + GetState(l).out = s; + } + } + + bool Eval(Stack& operandStack, Operator op) { + switch (op) { + case kConcatenation: + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag) * 2); + { + Frag e2 = *operandStack.template Pop(1); + Frag e1 = *operandStack.template Pop(1); + Patch(e1.out, e2.start); + *operandStack.template Push() = Frag(e1.start, e2.out, Min(e1.minIndex, e2.minIndex)); + } + return true; + + case kAlternation: + if (operandStack.GetSize() >= sizeof(Frag) * 2) { + Frag e2 = *operandStack.template Pop(1); + Frag e1 = *operandStack.template Pop(1); + SizeType s = NewState(e1.start, e2.start, 0); + *operandStack.template Push() = Frag(s, Append(e1.out, e2.out), Min(e1.minIndex, e2.minIndex)); + return true; + } + return false; + + case kZeroOrOne: + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + *operandStack.template Push() = Frag(s, Append(e.out, s), e.minIndex); + return true; + } + return false; + + case kZeroOrMore: + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + Patch(e.out, s); + *operandStack.template Push() = Frag(s, s, e.minIndex); + return true; + } + return false; + + default: + RAPIDJSON_ASSERT(op == kOneOrMore); + if (operandStack.GetSize() >= sizeof(Frag)) { + Frag e = *operandStack.template Pop(1); + SizeType s = NewState(kRegexInvalidState, e.start, 0); + Patch(e.out, s); + *operandStack.template Push() = Frag(e.start, s, e.minIndex); + return true; + } + return false; + } + } + + bool EvalQuantifier(Stack& operandStack, unsigned n, unsigned m) { + RAPIDJSON_ASSERT(n <= m); + RAPIDJSON_ASSERT(operandStack.GetSize() >= sizeof(Frag)); + + if (n == 0) { + if (m == 0) // a{0} not support + return false; + else if (m == kInfinityQuantifier) + Eval(operandStack, kZeroOrMore); // a{0,} -> a* + else { + Eval(operandStack, kZeroOrOne); // a{0,5} -> a? + for (unsigned i = 0; i < m - 1; i++) + CloneTopOperand(operandStack); // a{0,5} -> a? a? a? a? a? + for (unsigned i = 0; i < m - 1; i++) + Eval(operandStack, kConcatenation); // a{0,5} -> a?a?a?a?a? + } + return true; + } + + for (unsigned i = 0; i < n - 1; i++) // a{3} -> a a a + CloneTopOperand(operandStack); + + if (m == kInfinityQuantifier) + Eval(operandStack, kOneOrMore); // a{3,} -> a a a+ + else if (m > n) { + CloneTopOperand(operandStack); // a{3,5} -> a a a a + Eval(operandStack, kZeroOrOne); // a{3,5} -> a a a a? + for (unsigned i = n; i < m - 1; i++) + CloneTopOperand(operandStack); // a{3,5} -> a a a a? a? + for (unsigned i = n; i < m; i++) + Eval(operandStack, kConcatenation); // a{3,5} -> a a aa?a? + } + + for (unsigned i = 0; i < n - 1; i++) + Eval(operandStack, kConcatenation); // a{3} -> aaa, a{3,} -> aaa+, a{3.5} -> aaaa?a? + + return true; + } + + static SizeType Min(SizeType a, SizeType b) { return a < b ? a : b; } + + void CloneTopOperand(Stack& operandStack) { + const Frag src = *operandStack.template Top(); // Copy constructor to prevent invalidation + SizeType count = stateCount_ - src.minIndex; // Assumes top operand contains states in [src->minIndex, stateCount_) + State* s = states_.template Push(count); + memcpy(s, &GetState(src.minIndex), count * sizeof(State)); + for (SizeType j = 0; j < count; j++) { + if (s[j].out != kRegexInvalidState) + s[j].out += count; + if (s[j].out1 != kRegexInvalidState) + s[j].out1 += count; + } + *operandStack.template Push() = Frag(src.start + count, src.out + count, src.minIndex + count); + stateCount_ += count; + } + + template + bool ParseUnsigned(DecodedStream& ds, unsigned* u) { + unsigned r = 0; + if (ds.Peek() < '0' || ds.Peek() > '9') + return false; + while (ds.Peek() >= '0' && ds.Peek() <= '9') { + if (r >= 429496729 && ds.Peek() > '5') // 2^32 - 1 = 4294967295 + return false; // overflow + r = r * 10 + (ds.Take() - '0'); + } + *u = r; + return true; + } + + template + bool ParseRange(DecodedStream& ds, SizeType* range) { + bool isBegin = true; + bool negate = false; + int step = 0; + SizeType start = kRegexInvalidRange; + SizeType current = kRegexInvalidRange; + unsigned codepoint; + while ((codepoint = ds.Take()) != 0) { + if (isBegin) { + isBegin = false; + if (codepoint == '^') { + negate = true; + continue; + } + } + + switch (codepoint) { + case ']': + if (start == kRegexInvalidRange) + return false; // Error: nothing inside [] + if (step == 2) { // Add trailing '-' + SizeType r = NewRange('-'); + RAPIDJSON_ASSERT(current != kRegexInvalidRange); + GetRange(current).next = r; + } + if (negate) + GetRange(start).start |= kRangeNegationFlag; + *range = start; + return true; + + case '\\': + if (ds.Peek() == 'b') { + ds.Take(); + codepoint = 0x0008; // Escape backspace character + } + else if (!CharacterEscape(ds, &codepoint)) + return false; + // fall through to default + + default: + switch (step) { + case 1: + if (codepoint == '-') { + step++; + break; + } + // fall through to step 0 for other characters + + case 0: + { + SizeType r = NewRange(codepoint); + if (current != kRegexInvalidRange) + GetRange(current).next = r; + if (start == kRegexInvalidRange) + start = r; + current = r; + } + step = 1; + break; + + default: + RAPIDJSON_ASSERT(step == 2); + GetRange(current).end = codepoint; + step = 0; + } + } + } + return false; + } + + SizeType NewRange(unsigned codepoint) { + Range* r = ranges_.template Push(); + r->start = r->end = codepoint; + r->next = kRegexInvalidRange; + return rangeCount_++; + } + + template + bool CharacterEscape(DecodedStream& ds, unsigned* escapedCodepoint) { + unsigned codepoint; + switch (codepoint = ds.Take()) { + case '^': + case '$': + case '|': + case '(': + case ')': + case '?': + case '*': + case '+': + case '.': + case '[': + case ']': + case '{': + case '}': + case '\\': + *escapedCodepoint = codepoint; return true; + case 'f': *escapedCodepoint = 0x000C; return true; + case 'n': *escapedCodepoint = 0x000A; return true; + case 'r': *escapedCodepoint = 0x000D; return true; + case 't': *escapedCodepoint = 0x0009; return true; + case 'v': *escapedCodepoint = 0x000B; return true; + default: + return false; // Unsupported escape character + } + } + + template + bool SearchWithAnchoring(InputStream& is, bool anchorBegin, bool anchorEnd) const { + RAPIDJSON_ASSERT(IsValid()); + DecodedStream ds(is); + + state0_.Clear(); + Stack *current = &state0_, *next = &state1_; + const size_t stateSetSize = GetStateSetSize(); + std::memset(stateSet_, 0, stateSetSize); + + bool matched = AddState(*current, root_); + unsigned codepoint; + while (!current->Empty() && (codepoint = ds.Take()) != 0) { + std::memset(stateSet_, 0, stateSetSize); + next->Clear(); + matched = false; + for (const SizeType* s = current->template Bottom(); s != current->template End(); ++s) { + const State& sr = GetState(*s); + if (sr.codepoint == codepoint || + sr.codepoint == kAnyCharacterClass || + (sr.codepoint == kRangeCharacterClass && MatchRange(sr.rangeStart, codepoint))) + { + matched = AddState(*next, sr.out) || matched; + if (!anchorEnd && matched) + return true; + } + if (!anchorBegin) + AddState(*next, root_); + } + internal::Swap(current, next); + } + + return matched; + } + + size_t GetStateSetSize() const { + return (stateCount_ + 31) / 32 * 4; + } + + // Return whether the added states is a match state + bool AddState(Stack& l, SizeType index) const { + RAPIDJSON_ASSERT(index != kRegexInvalidState); + + const State& s = GetState(index); + if (s.out1 != kRegexInvalidState) { // Split + bool matched = AddState(l, s.out); + return AddState(l, s.out1) || matched; + } + else if (!(stateSet_[index >> 5] & (1 << (index & 31)))) { + stateSet_[index >> 5] |= (1 << (index & 31)); + *l.template PushUnsafe() = index; + } + return s.out == kRegexInvalidState; // by using PushUnsafe() above, we can ensure s is not validated due to reallocation. + } + + bool MatchRange(SizeType rangeIndex, unsigned codepoint) const { + bool yes = (GetRange(rangeIndex).start & kRangeNegationFlag) == 0; + while (rangeIndex != kRegexInvalidRange) { + const Range& r = GetRange(rangeIndex); + if (codepoint >= (r.start & ~kRangeNegationFlag) && codepoint <= r.end) + return yes; + rangeIndex = r.next; + } + return !yes; + } + + Stack states_; + Stack ranges_; + SizeType root_; + SizeType stateCount_; + SizeType rangeCount_; + + static const unsigned kInfinityQuantifier = ~0u; + + // For SearchWithAnchoring() + uint32_t* stateSet_; // allocated by states_.GetAllocator() + mutable Stack state0_; + mutable Stack state1_; + bool anchorBegin_; + bool anchorEnd_; +}; + +typedef GenericRegex > Regex; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_REGEX_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/stack.h b/rapidjson-1.1.0/rapid_json/internal/stack.h new file mode 100644 index 000000000..022c9aab4 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/stack.h @@ -0,0 +1,230 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_STACK_H_ +#define RAPIDJSON_INTERNAL_STACK_H_ + +#include "../allocators.h" +#include "swap.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +/////////////////////////////////////////////////////////////////////////////// +// Stack + +//! A type-unsafe stack for storing different types of data. +/*! \tparam Allocator Allocator for allocating stack memory. +*/ +template +class Stack { +public: + // Optimization note: Do not allocate memory for stack_ in constructor. + // Do it lazily when first Push() -> Expand() -> Resize(). + Stack(Allocator* allocator, size_t stackCapacity) : allocator_(allocator), ownAllocator_(0), stack_(0), stackTop_(0), stackEnd_(0), initialCapacity_(stackCapacity) { + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Stack(Stack&& rhs) + : allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + stack_(rhs.stack_), + stackTop_(rhs.stackTop_), + stackEnd_(rhs.stackEnd_), + initialCapacity_(rhs.initialCapacity_) + { + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.stack_ = 0; + rhs.stackTop_ = 0; + rhs.stackEnd_ = 0; + rhs.initialCapacity_ = 0; + } +#endif + + ~Stack() { + Destroy(); + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + Stack& operator=(Stack&& rhs) { + if (&rhs != this) + { + Destroy(); + + allocator_ = rhs.allocator_; + ownAllocator_ = rhs.ownAllocator_; + stack_ = rhs.stack_; + stackTop_ = rhs.stackTop_; + stackEnd_ = rhs.stackEnd_; + initialCapacity_ = rhs.initialCapacity_; + + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + rhs.stack_ = 0; + rhs.stackTop_ = 0; + rhs.stackEnd_ = 0; + rhs.initialCapacity_ = 0; + } + return *this; + } +#endif + + void Swap(Stack& rhs) RAPIDJSON_NOEXCEPT { + internal::Swap(allocator_, rhs.allocator_); + internal::Swap(ownAllocator_, rhs.ownAllocator_); + internal::Swap(stack_, rhs.stack_); + internal::Swap(stackTop_, rhs.stackTop_); + internal::Swap(stackEnd_, rhs.stackEnd_); + internal::Swap(initialCapacity_, rhs.initialCapacity_); + } + + void Clear() { stackTop_ = stack_; } + + void ShrinkToFit() { + if (Empty()) { + // If the stack is empty, completely deallocate the memory. + Allocator::Free(stack_); + stack_ = 0; + stackTop_ = 0; + stackEnd_ = 0; + } + else + Resize(GetSize()); + } + + // Optimization note: try to minimize the size of this function for force inline. + // Expansion is run very infrequently, so it is moved to another (probably non-inline) function. + template + RAPIDJSON_FORCEINLINE void Reserve(size_t count = 1) { + // Expand the stack if needed + if (RAPIDJSON_UNLIKELY(stackTop_ + sizeof(T) * count > stackEnd_)) + Expand(count); + } + + template + RAPIDJSON_FORCEINLINE T* Push(size_t count = 1) { + Reserve(count); + return PushUnsafe(count); + } + + template + RAPIDJSON_FORCEINLINE T* PushUnsafe(size_t count = 1) { + RAPIDJSON_ASSERT(stackTop_ + sizeof(T) * count <= stackEnd_); + T* ret = reinterpret_cast(stackTop_); + stackTop_ += sizeof(T) * count; + return ret; + } + + template + T* Pop(size_t count) { + RAPIDJSON_ASSERT(GetSize() >= count * sizeof(T)); + stackTop_ -= count * sizeof(T); + return reinterpret_cast(stackTop_); + } + + template + T* Top() { + RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); + return reinterpret_cast(stackTop_ - sizeof(T)); + } + + template + const T* Top() const { + RAPIDJSON_ASSERT(GetSize() >= sizeof(T)); + return reinterpret_cast(stackTop_ - sizeof(T)); + } + + template + T* End() { return reinterpret_cast(stackTop_); } + + template + const T* End() const { return reinterpret_cast(stackTop_); } + + template + T* Bottom() { return reinterpret_cast(stack_); } + + template + const T* Bottom() const { return reinterpret_cast(stack_); } + + bool HasAllocator() const { + return allocator_ != 0; + } + + Allocator& GetAllocator() { + RAPIDJSON_ASSERT(allocator_); + return *allocator_; + } + + bool Empty() const { return stackTop_ == stack_; } + size_t GetSize() const { return static_cast(stackTop_ - stack_); } + size_t GetCapacity() const { return static_cast(stackEnd_ - stack_); } + +private: + template + void Expand(size_t count) { + // Only expand the capacity if the current stack exists. Otherwise just create a stack with initial capacity. + size_t newCapacity; + if (stack_ == 0) { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + newCapacity = initialCapacity_; + } else { + newCapacity = GetCapacity(); + newCapacity += (newCapacity + 1) / 2; + } + size_t newSize = GetSize() + sizeof(T) * count; + if (newCapacity < newSize) + newCapacity = newSize; + + Resize(newCapacity); + } + + void Resize(size_t newCapacity) { + const size_t size = GetSize(); // Backup the current size + stack_ = static_cast(allocator_->Realloc(stack_, GetCapacity(), newCapacity)); + stackTop_ = stack_ + size; + stackEnd_ = stack_ + newCapacity; + } + + void Destroy() { + Allocator::Free(stack_); + RAPIDJSON_DELETE(ownAllocator_); // Only delete if it is owned by the stack + } + + // Prohibit copy constructor & assignment operator. + Stack(const Stack&); + Stack& operator=(const Stack&); + + Allocator* allocator_; + Allocator* ownAllocator_; + char *stack_; + char *stackTop_; + char *stackEnd_; + size_t initialCapacity_; +}; + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_STACK_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/strfunc.h b/rapidjson-1.1.0/rapid_json/internal/strfunc.h new file mode 100644 index 000000000..2edfae526 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/strfunc.h @@ -0,0 +1,55 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_STRFUNC_H_ +#define RAPIDJSON_INTERNAL_STRFUNC_H_ + +#include "../stream.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Custom strlen() which works on different character types. +/*! \tparam Ch Character type (e.g. char, wchar_t, short) + \param s Null-terminated input string. + \return Number of characters in the string. + \note This has the same semantics as strlen(), the return value is not number of Unicode codepoints. +*/ +template +inline SizeType StrLen(const Ch* s) { + const Ch* p = s; + while (*p) ++p; + return SizeType(p - s); +} + +//! Returns number of code points in a encoded string. +template +bool CountStringCodePoint(const typename Encoding::Ch* s, SizeType length, SizeType* outCount) { + GenericStringStream is(s); + const typename Encoding::Ch* end = s + length; + SizeType count = 0; + while (is.src_ < end) { + unsigned codepoint; + if (!Encoding::Decode(is, &codepoint)) + return false; + count++; + } + *outCount = count; + return true; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_INTERNAL_STRFUNC_H_ diff --git a/rapidjson-1.1.0/rapid_json/internal/strtod.h b/rapidjson-1.1.0/rapid_json/internal/strtod.h new file mode 100644 index 000000000..289c413b0 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/strtod.h @@ -0,0 +1,269 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_STRTOD_ +#define RAPIDJSON_STRTOD_ + +#include "ieee754.h" +#include "biginteger.h" +#include "diyfp.h" +#include "pow10.h" + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +inline double FastPath(double significand, int exp) { + if (exp < -308) + return 0.0; + else if (exp >= 0) + return significand * internal::Pow10(exp); + else + return significand / internal::Pow10(-exp); +} + +inline double StrtodNormalPrecision(double d, int p) { + if (p < -308) { + // Prevent expSum < -308, making Pow10(p) = 0 + d = FastPath(d, -308); + d = FastPath(d, p + 308); + } + else + d = FastPath(d, p); + return d; +} + +template +inline T Min3(T a, T b, T c) { + T m = a; + if (m > b) m = b; + if (m > c) m = c; + return m; +} + +inline int CheckWithinHalfULP(double b, const BigInteger& d, int dExp) { + const Double db(b); + const uint64_t bInt = db.IntegerSignificand(); + const int bExp = db.IntegerExponent(); + const int hExp = bExp - 1; + + int dS_Exp2 = 0, dS_Exp5 = 0, bS_Exp2 = 0, bS_Exp5 = 0, hS_Exp2 = 0, hS_Exp5 = 0; + + // Adjust for decimal exponent + if (dExp >= 0) { + dS_Exp2 += dExp; + dS_Exp5 += dExp; + } + else { + bS_Exp2 -= dExp; + bS_Exp5 -= dExp; + hS_Exp2 -= dExp; + hS_Exp5 -= dExp; + } + + // Adjust for binary exponent + if (bExp >= 0) + bS_Exp2 += bExp; + else { + dS_Exp2 -= bExp; + hS_Exp2 -= bExp; + } + + // Adjust for half ulp exponent + if (hExp >= 0) + hS_Exp2 += hExp; + else { + dS_Exp2 -= hExp; + bS_Exp2 -= hExp; + } + + // Remove common power of two factor from all three scaled values + int common_Exp2 = Min3(dS_Exp2, bS_Exp2, hS_Exp2); + dS_Exp2 -= common_Exp2; + bS_Exp2 -= common_Exp2; + hS_Exp2 -= common_Exp2; + + BigInteger dS = d; + dS.MultiplyPow5(static_cast(dS_Exp5)) <<= static_cast(dS_Exp2); + + BigInteger bS(bInt); + bS.MultiplyPow5(static_cast(bS_Exp5)) <<= static_cast(bS_Exp2); + + BigInteger hS(1); + hS.MultiplyPow5(static_cast(hS_Exp5)) <<= static_cast(hS_Exp2); + + BigInteger delta(0); + dS.Difference(bS, &delta); + + return delta.Compare(hS); +} + +inline bool StrtodFast(double d, int p, double* result) { + // Use fast path for string-to-double conversion if possible + // see http://www.exploringbinary.com/fast-path-decimal-to-floating-point-conversion/ + if (p > 22 && p < 22 + 16) { + // Fast Path Cases In Disguise + d *= internal::Pow10(p - 22); + p = 22; + } + + if (p >= -22 && p <= 22 && d <= 9007199254740991.0) { // 2^53 - 1 + *result = FastPath(d, p); + return true; + } + else + return false; +} + +// Compute an approximation and see if it is within 1/2 ULP +inline bool StrtodDiyFp(const char* decimals, size_t length, size_t decimalPosition, int exp, double* result) { + uint64_t significand = 0; + size_t i = 0; // 2^64 - 1 = 18446744073709551615, 1844674407370955161 = 0x1999999999999999 + for (; i < length; i++) { + if (significand > RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || + (significand == RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) && decimals[i] > '5')) + break; + significand = significand * 10u + static_cast(decimals[i] - '0'); + } + + if (i < length && decimals[i] >= '5') // Rounding + significand++; + + size_t remaining = length - i; + const unsigned kUlpShift = 3; + const unsigned kUlp = 1 << kUlpShift; + int64_t error = (remaining == 0) ? 0 : kUlp / 2; + + DiyFp v(significand, 0); + v = v.Normalize(); + error <<= -v.e; + + const int dExp = static_cast(decimalPosition) - static_cast(i) + exp; + + int actualExp; + DiyFp cachedPower = GetCachedPower10(dExp, &actualExp); + if (actualExp != dExp) { + static const DiyFp kPow10[] = { + DiyFp(RAPIDJSON_UINT64_C2(0xa0000000, 00000000), -60), // 10^1 + DiyFp(RAPIDJSON_UINT64_C2(0xc8000000, 00000000), -57), // 10^2 + DiyFp(RAPIDJSON_UINT64_C2(0xfa000000, 00000000), -54), // 10^3 + DiyFp(RAPIDJSON_UINT64_C2(0x9c400000, 00000000), -50), // 10^4 + DiyFp(RAPIDJSON_UINT64_C2(0xc3500000, 00000000), -47), // 10^5 + DiyFp(RAPIDJSON_UINT64_C2(0xf4240000, 00000000), -44), // 10^6 + DiyFp(RAPIDJSON_UINT64_C2(0x98968000, 00000000), -40) // 10^7 + }; + int adjustment = dExp - actualExp - 1; + RAPIDJSON_ASSERT(adjustment >= 0 && adjustment < 7); + v = v * kPow10[adjustment]; + if (length + static_cast(adjustment)> 19u) // has more digits than decimal digits in 64-bit + error += kUlp / 2; + } + + v = v * cachedPower; + + error += kUlp + (error == 0 ? 0 : 1); + + const int oldExp = v.e; + v = v.Normalize(); + error <<= oldExp - v.e; + + const unsigned effectiveSignificandSize = Double::EffectiveSignificandSize(64 + v.e); + unsigned precisionSize = 64 - effectiveSignificandSize; + if (precisionSize + kUlpShift >= 64) { + unsigned scaleExp = (precisionSize + kUlpShift) - 63; + v.f >>= scaleExp; + v.e += scaleExp; + error = (error >> scaleExp) + 1 + static_cast(kUlp); + precisionSize -= scaleExp; + } + + DiyFp rounded(v.f >> precisionSize, v.e + static_cast(precisionSize)); + const uint64_t precisionBits = (v.f & ((uint64_t(1) << precisionSize) - 1)) * kUlp; + const uint64_t halfWay = (uint64_t(1) << (precisionSize - 1)) * kUlp; + if (precisionBits >= halfWay + static_cast(error)) { + rounded.f++; + if (rounded.f & (DiyFp::kDpHiddenBit << 1)) { // rounding overflows mantissa (issue #340) + rounded.f >>= 1; + rounded.e++; + } + } + + *result = rounded.ToDouble(); + + return halfWay - static_cast(error) >= precisionBits || precisionBits >= halfWay + static_cast(error); +} + +inline double StrtodBigInteger(double approx, const char* decimals, size_t length, size_t decimalPosition, int exp) { + const BigInteger dInt(decimals, length); + const int dExp = static_cast(decimalPosition) - static_cast(length) + exp; + Double a(approx); + int cmp = CheckWithinHalfULP(a.Value(), dInt, dExp); + if (cmp < 0) + return a.Value(); // within half ULP + else if (cmp == 0) { + // Round towards even + if (a.Significand() & 1) + return a.NextPositiveDouble(); + else + return a.Value(); + } + else // adjustment + return a.NextPositiveDouble(); +} + +inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t length, size_t decimalPosition, int exp) { + RAPIDJSON_ASSERT(d >= 0.0); + RAPIDJSON_ASSERT(length >= 1); + + double result; + if (StrtodFast(d, p, &result)) + return result; + + // Trim leading zeros + while (*decimals == '0' && length > 1) { + length--; + decimals++; + decimalPosition--; + } + + // Trim trailing zeros + while (decimals[length - 1] == '0' && length > 1) { + length--; + decimalPosition--; + exp++; + } + + // Trim right-most digits + const int kMaxDecimalDigit = 780; + if (static_cast(length) > kMaxDecimalDigit) { + int delta = (static_cast(length) - kMaxDecimalDigit); + exp += delta; + decimalPosition -= static_cast(delta); + length = kMaxDecimalDigit; + } + + // If too small, underflow to zero + if (int(length) + exp < -324) + return 0.0; + + if (StrtodDiyFp(decimals, length, decimalPosition, exp, &result)) + return result; + + // Use approximation from StrtodDiyFp and make adjustment with BigInteger comparison + return StrtodBigInteger(result, decimals, length, decimalPosition, exp); +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_STRTOD_ diff --git a/rapidjson-1.1.0/rapid_json/internal/swap.h b/rapidjson-1.1.0/rapid_json/internal/swap.h new file mode 100644 index 000000000..666e49f97 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/internal/swap.h @@ -0,0 +1,46 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_INTERNAL_SWAP_H_ +#define RAPIDJSON_INTERNAL_SWAP_H_ + +#include "../rapidjson.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN +namespace internal { + +//! Custom swap() to avoid dependency on C++ header +/*! \tparam T Type of the arguments to swap, should be instantiated with primitive C++ types only. + \note This has the same semantics as std::swap(). +*/ +template +inline void Swap(T& a, T& b) RAPIDJSON_NOEXCEPT { + T tmp = a; + a = b; + b = tmp; +} + +} // namespace internal +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_INTERNAL_SWAP_H_ diff --git a/rapidjson-1.1.0/rapid_json/istreamwrapper.h b/rapidjson-1.1.0/rapid_json/istreamwrapper.h new file mode 100644 index 000000000..f5fe28977 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/istreamwrapper.h @@ -0,0 +1,115 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_ISTREAMWRAPPER_H_ +#define RAPIDJSON_ISTREAMWRAPPER_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of \c std::basic_istream into RapidJSON's Stream concept. +/*! + The classes can be wrapped including but not limited to: + + - \c std::istringstream + - \c std::stringstream + - \c std::wistringstream + - \c std::wstringstream + - \c std::ifstream + - \c std::fstream + - \c std::wifstream + - \c std::wfstream + + \tparam StreamType Class derived from \c std::basic_istream. +*/ + +template +class BasicIStreamWrapper { +public: + typedef typename StreamType::char_type Ch; + BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {} + + Ch Peek() const { + typename StreamType::int_type c = stream_.peek(); + return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast(c) : '\0'; + } + + Ch Take() { + typename StreamType::int_type c = stream_.get(); + if (RAPIDJSON_LIKELY(c != StreamType::traits_type::eof())) { + count_++; + return static_cast(c); + } + else + return '\0'; + } + + // tellg() may return -1 when failed. So we count by ourself. + size_t Tell() const { return count_; } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream. + int i; + bool hasError = false; + for (i = 0; i < 4; ++i) { + typename StreamType::int_type c = stream_.get(); + if (c == StreamType::traits_type::eof()) { + hasError = true; + stream_.clear(); + break; + } + peekBuffer_[i] = static_cast(c); + } + for (--i; i >= 0; --i) + stream_.putback(peekBuffer_[i]); + return !hasError ? peekBuffer_ : 0; + } + +private: + BasicIStreamWrapper(const BasicIStreamWrapper&); + BasicIStreamWrapper& operator=(const BasicIStreamWrapper&); + + StreamType& stream_; + size_t count_; //!< Number of characters read. Note: + mutable Ch peekBuffer_[4]; +}; + +typedef BasicIStreamWrapper IStreamWrapper; +typedef BasicIStreamWrapper WIStreamWrapper; + +#if defined(__clang__) || defined(_MSC_VER) +RAPIDJSON_DIAG_POP +#endif + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_ISTREAMWRAPPER_H_ diff --git a/rapidjson-1.1.0/rapid_json/memorybuffer.h b/rapidjson-1.1.0/rapid_json/memorybuffer.h new file mode 100644 index 000000000..39bee1dec --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/memorybuffer.h @@ -0,0 +1,70 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_MEMORYBUFFER_H_ +#define RAPIDJSON_MEMORYBUFFER_H_ + +#include "stream.h" +#include "internal/stack.h" + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory output byte stream. +/*! + This class is mainly for being wrapped by EncodedOutputStream or AutoUTFOutputStream. + + It is similar to FileWriteBuffer but the destination is an in-memory buffer instead of a file. + + Differences between MemoryBuffer and StringBuffer: + 1. StringBuffer has Encoding but MemoryBuffer is only a byte buffer. + 2. StringBuffer::GetString() returns a null-terminated string. MemoryBuffer::GetBuffer() returns a buffer without terminator. + + \tparam Allocator type for allocating memory buffer. + \note implements Stream concept +*/ +template +struct GenericMemoryBuffer { + typedef char Ch; // byte + + GenericMemoryBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} + + void Put(Ch c) { *stack_.template Push() = c; } + void Flush() {} + + void Clear() { stack_.Clear(); } + void ShrinkToFit() { stack_.ShrinkToFit(); } + Ch* Push(size_t count) { return stack_.template Push(count); } + void Pop(size_t count) { stack_.template Pop(count); } + + const Ch* GetBuffer() const { + return stack_.template Bottom(); + } + + size_t GetSize() const { return stack_.GetSize(); } + + static const size_t kDefaultCapacity = 256; + mutable internal::Stack stack_; +}; + +typedef GenericMemoryBuffer<> MemoryBuffer; + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(MemoryBuffer& memoryBuffer, char c, size_t n) { + std::memset(memoryBuffer.stack_.Push(n), c, n * sizeof(c)); +} + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_MEMORYBUFFER_H_ diff --git a/rapidjson-1.1.0/rapid_json/memorystream.h b/rapidjson-1.1.0/rapid_json/memorystream.h new file mode 100644 index 000000000..1d71d8a4f --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/memorystream.h @@ -0,0 +1,71 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_MEMORYSTREAM_H_ +#define RAPIDJSON_MEMORYSTREAM_H_ + +#include "stream.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(unreachable-code) +RAPIDJSON_DIAG_OFF(missing-noreturn) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory input byte stream. +/*! + This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream. + + It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file. + + Differences between MemoryStream and StringStream: + 1. StringStream has encoding but MemoryStream is a byte stream. + 2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source. + 3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4(). + \note implements Stream concept +*/ +struct MemoryStream { + typedef char Ch; // byte + + MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {} + + Ch Peek() const { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_; } + Ch Take() { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_++; } + size_t Tell() const { return static_cast(src_ - begin_); } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + // For encoding detection only. + const Ch* Peek4() const { + return Tell() + 4 <= size_ ? src_ : 0; + } + + const Ch* src_; //!< Current read position. + const Ch* begin_; //!< Original head of the string. + const Ch* end_; //!< End of stream. + size_t size_; //!< Size of the stream. +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_MEMORYBUFFER_H_ diff --git a/rapidjson-1.1.0/rapid_json/ostreamwrapper.h b/rapidjson-1.1.0/rapid_json/ostreamwrapper.h new file mode 100644 index 000000000..6f4667c08 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/ostreamwrapper.h @@ -0,0 +1,81 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_OSTREAMWRAPPER_H_ +#define RAPIDJSON_OSTREAMWRAPPER_H_ + +#include "stream.h" +#include + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Wrapper of \c std::basic_ostream into RapidJSON's Stream concept. +/*! + The classes can be wrapped including but not limited to: + + - \c std::ostringstream + - \c std::stringstream + - \c std::wpstringstream + - \c std::wstringstream + - \c std::ifstream + - \c std::fstream + - \c std::wofstream + - \c std::wfstream + + \tparam StreamType Class derived from \c std::basic_ostream. +*/ + +template +class BasicOStreamWrapper { +public: + typedef typename StreamType::char_type Ch; + BasicOStreamWrapper(StreamType& stream) : stream_(stream) {} + + void Put(Ch c) { + stream_.put(c); + } + + void Flush() { + stream_.flush(); + } + + // Not implemented + char Peek() const { RAPIDJSON_ASSERT(false); return 0; } + char Take() { RAPIDJSON_ASSERT(false); return 0; } + size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } + char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } + +private: + BasicOStreamWrapper(const BasicOStreamWrapper&); + BasicOStreamWrapper& operator=(const BasicOStreamWrapper&); + + StreamType& stream_; +}; + +typedef BasicOStreamWrapper OStreamWrapper; +typedef BasicOStreamWrapper WOStreamWrapper; + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_OSTREAMWRAPPER_H_ diff --git a/rapidjson-1.1.0/rapid_json/pointer.h b/rapidjson-1.1.0/rapid_json/pointer.h new file mode 100644 index 000000000..0206ac1c8 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/pointer.h @@ -0,0 +1,1358 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_POINTER_H_ +#define RAPIDJSON_POINTER_H_ + +#include "document.h" +#include "internal/itoa.h" + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(switch-enum) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +static const SizeType kPointerInvalidIndex = ~SizeType(0); //!< Represents an invalid index in GenericPointer::Token + +//! Error code of parsing. +/*! \ingroup RAPIDJSON_ERRORS + \see GenericPointer::GenericPointer, GenericPointer::GetParseErrorCode +*/ +enum PointerParseErrorCode { + kPointerParseErrorNone = 0, //!< The parse is successful + + kPointerParseErrorTokenMustBeginWithSolidus, //!< A token must begin with a '/' + kPointerParseErrorInvalidEscape, //!< Invalid escape + kPointerParseErrorInvalidPercentEncoding, //!< Invalid percent encoding in URI fragment + kPointerParseErrorCharacterMustPercentEncode //!< A character must percent encoded in URI fragment +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericPointer + +//! Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator. +/*! + This class implements RFC 6901 "JavaScript Object Notation (JSON) Pointer" + (https://tools.ietf.org/html/rfc6901). + + A JSON pointer is for identifying a specific value in a JSON document + (GenericDocument). It can simplify coding of DOM tree manipulation, because it + can access multiple-level depth of DOM tree with single API call. + + After it parses a string representation (e.g. "/foo/0" or URI fragment + representation (e.g. "#/foo/0") into its internal representation (tokens), + it can be used to resolve a specific value in multiple documents, or sub-tree + of documents. + + Contrary to GenericValue, Pointer can be copy constructed and copy assigned. + Apart from assignment, a Pointer cannot be modified after construction. + + Although Pointer is very convenient, please aware that constructing Pointer + involves parsing and dynamic memory allocation. A special constructor with user- + supplied tokens eliminates these. + + GenericPointer depends on GenericDocument and GenericValue. + + \tparam ValueType The value type of the DOM tree. E.g. GenericValue > + \tparam Allocator The allocator type for allocating memory for internal representation. + + \note GenericPointer uses same encoding of ValueType. + However, Allocator of GenericPointer is independent of Allocator of Value. +*/ +template +class GenericPointer { +public: + typedef typename ValueType::EncodingType EncodingType; //!< Encoding type from Value + typedef typename ValueType::Ch Ch; //!< Character type from Value + + //! A token is the basic units of internal representation. + /*! + A JSON pointer string representation "/foo/123" is parsed to two tokens: + "foo" and 123. 123 will be represented in both numeric form and string form. + They are resolved according to the actual value type (object or array). + + For token that are not numbers, or the numeric value is out of bound + (greater than limits of SizeType), they are only treated as string form + (i.e. the token's index will be equal to kPointerInvalidIndex). + + This struct is public so that user can create a Pointer without parsing and + allocation, using a special constructor. + */ + struct Token { + const Ch* name; //!< Name of the token. It has null character at the end but it can contain null character. + SizeType length; //!< Length of the name. + SizeType index; //!< A valid array index, if it is not equal to kPointerInvalidIndex. + }; + + //!@name Constructors and destructor. + //@{ + + //! Default constructor. + GenericPointer(Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) {} + + //! Constructor that parses a string or URI fragment representation. + /*! + \param source A null-terminated, string or URI fragment representation of JSON pointer. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + */ + explicit GenericPointer(const Ch* source, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source, internal::StrLen(source)); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Constructor that parses a string or URI fragment representation. + /*! + \param source A string or URI fragment representation of JSON pointer. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. + */ + explicit GenericPointer(const std::basic_string& source, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source.c_str(), source.size()); + } +#endif + + //! Constructor that parses a string or URI fragment representation, with length of the source string. + /*! + \param source A string or URI fragment representation of JSON pointer. + \param length Length of source. + \param allocator User supplied allocator for this pointer. If no allocator is provided, it creates a self-owned one. + \note Slightly faster than the overload without length. + */ + GenericPointer(const Ch* source, size_t length, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + Parse(source, length); + } + + //! Constructor with user-supplied tokens. + /*! + This constructor let user supplies const array of tokens. + This prevents the parsing process and eliminates allocation. + This is preferred for memory constrained environments. + + \param tokens An constant array of tokens representing the JSON pointer. + \param tokenCount Number of tokens. + + \b Example + \code + #define NAME(s) { s, sizeof(s) / sizeof(s[0]) - 1, kPointerInvalidIndex } + #define INDEX(i) { #i, sizeof(#i) - 1, i } + + static const Pointer::Token kTokens[] = { NAME("foo"), INDEX(123) }; + static const Pointer p(kTokens, sizeof(kTokens) / sizeof(kTokens[0])); + // Equivalent to static const Pointer p("/foo/123"); + + #undef NAME + #undef INDEX + \endcode + */ + GenericPointer(const Token* tokens, size_t tokenCount) : allocator_(), ownAllocator_(), nameBuffer_(), tokens_(const_cast(tokens)), tokenCount_(tokenCount), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) {} + + //! Copy constructor. + GenericPointer(const GenericPointer& rhs, Allocator* allocator = 0) : allocator_(allocator), ownAllocator_(), nameBuffer_(), tokens_(), tokenCount_(), parseErrorOffset_(), parseErrorCode_(kPointerParseErrorNone) { + *this = rhs; + } + + //! Destructor. + ~GenericPointer() { + if (nameBuffer_) // If user-supplied tokens constructor is used, nameBuffer_ is nullptr and tokens_ are not deallocated. + Allocator::Free(tokens_); + RAPIDJSON_DELETE(ownAllocator_); + } + + //! Assignment operator. + GenericPointer& operator=(const GenericPointer& rhs) { + if (this != &rhs) { + // Do not delete ownAllcator + if (nameBuffer_) + Allocator::Free(tokens_); + + tokenCount_ = rhs.tokenCount_; + parseErrorOffset_ = rhs.parseErrorOffset_; + parseErrorCode_ = rhs.parseErrorCode_; + + if (rhs.nameBuffer_) + CopyFromRaw(rhs); // Normally parsed tokens. + else { + tokens_ = rhs.tokens_; // User supplied const tokens. + nameBuffer_ = 0; + } + } + return *this; + } + + //@} + + //!@name Append token + //@{ + + //! Append a token and return a new Pointer + /*! + \param token Token to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const Token& token, Allocator* allocator = 0) const { + GenericPointer r; + r.allocator_ = allocator; + Ch *p = r.CopyFromRaw(*this, 1, token.length + 1); + std::memcpy(p, token.name, (token.length + 1) * sizeof(Ch)); + r.tokens_[tokenCount_].name = p; + r.tokens_[tokenCount_].length = token.length; + r.tokens_[tokenCount_].index = token.index; + return r; + } + + //! Append a name token with length, and return a new Pointer + /*! + \param name Name to be appended. + \param length Length of name. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const Ch* name, SizeType length, Allocator* allocator = 0) const { + Token token = { name, length, kPointerInvalidIndex }; + return Append(token, allocator); + } + + //! Append a name token without length, and return a new Pointer + /*! + \param name Name (const Ch*) to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr::Type, Ch> >), (GenericPointer)) + Append(T* name, Allocator* allocator = 0) const { + return Append(name, StrLen(name), allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Append a name token, and return a new Pointer + /*! + \param name Name to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const std::basic_string& name, Allocator* allocator = 0) const { + return Append(name.c_str(), static_cast(name.size()), allocator); + } +#endif + + //! Append a index token, and return a new Pointer + /*! + \param index Index to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(SizeType index, Allocator* allocator = 0) const { + char buffer[21]; + char* end = sizeof(SizeType) == 4 ? internal::u32toa(index, buffer) : internal::u64toa(index, buffer); + SizeType length = static_cast(end - buffer); + buffer[length] = '\0'; + + if (sizeof(Ch) == 1) { + Token token = { reinterpret_cast(buffer), length, index }; + return Append(token, allocator); + } + else { + Ch name[21]; + for (size_t i = 0; i <= length; i++) + name[i] = buffer[i]; + Token token = { name, length, index }; + return Append(token, allocator); + } + } + + //! Append a token by value, and return a new Pointer + /*! + \param token token to be appended. + \param allocator Allocator for the newly return Pointer. + \return A new Pointer with appended token. + */ + GenericPointer Append(const ValueType& token, Allocator* allocator = 0) const { + if (token.IsString()) + return Append(token.GetString(), token.GetStringLength(), allocator); + else { + RAPIDJSON_ASSERT(token.IsUint64()); + RAPIDJSON_ASSERT(token.GetUint64() <= SizeType(~0)); + return Append(static_cast(token.GetUint64()), allocator); + } + } + + //!@name Handling Parse Error + //@{ + + //! Check whether this is a valid pointer. + bool IsValid() const { return parseErrorCode_ == kPointerParseErrorNone; } + + //! Get the parsing error offset in code unit. + size_t GetParseErrorOffset() const { return parseErrorOffset_; } + + //! Get the parsing error code. + PointerParseErrorCode GetParseErrorCode() const { return parseErrorCode_; } + + //@} + + //! Get the allocator of this pointer. + Allocator& GetAllocator() { return *allocator_; } + + //!@name Tokens + //@{ + + //! Get the token array (const version only). + const Token* GetTokens() const { return tokens_; } + + //! Get the number of tokens. + size_t GetTokenCount() const { return tokenCount_; } + + //@} + + //!@name Equality/inequality operators + //@{ + + //! Equality operator. + /*! + \note When any pointers are invalid, always returns false. + */ + bool operator==(const GenericPointer& rhs) const { + if (!IsValid() || !rhs.IsValid() || tokenCount_ != rhs.tokenCount_) + return false; + + for (size_t i = 0; i < tokenCount_; i++) { + if (tokens_[i].index != rhs.tokens_[i].index || + tokens_[i].length != rhs.tokens_[i].length || + (tokens_[i].length != 0 && std::memcmp(tokens_[i].name, rhs.tokens_[i].name, sizeof(Ch)* tokens_[i].length) != 0)) + { + return false; + } + } + + return true; + } + + //! Inequality operator. + /*! + \note When any pointers are invalid, always returns true. + */ + bool operator!=(const GenericPointer& rhs) const { return !(*this == rhs); } + + //@} + + //!@name Stringify + //@{ + + //! Stringify the pointer into string representation. + /*! + \tparam OutputStream Type of output stream. + \param os The output stream. + */ + template + bool Stringify(OutputStream& os) const { + return Stringify(os); + } + + //! Stringify the pointer into URI fragment representation. + /*! + \tparam OutputStream Type of output stream. + \param os The output stream. + */ + template + bool StringifyUriFragment(OutputStream& os) const { + return Stringify(os); + } + + //@} + + //!@name Create value + //@{ + + //! Create a value in a subtree. + /*! + If the value is not exist, it creates all parent values and a JSON Null value. + So it always succeed and return the newly created or existing value. + + Remind that it may change types of parents according to tokens, so it + potentially removes previously stored values. For example, if a document + was an array, and "/foo" is used to create a value, then the document + will be changed to an object, and all existing array elements are lost. + + \param root Root value of a DOM subtree to be resolved. It can be any value other than document root. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \param alreadyExist If non-null, it stores whether the resolved value is already exist. + \return The resolved newly created (a JSON Null value), or already exists value. + */ + ValueType& Create(ValueType& root, typename ValueType::AllocatorType& allocator, bool* alreadyExist = 0) const { + RAPIDJSON_ASSERT(IsValid()); + ValueType* v = &root; + bool exist = true; + for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + if (v->IsArray() && t->name[0] == '-' && t->length == 1) { + v->PushBack(ValueType().Move(), allocator); + v = &((*v)[v->Size() - 1]); + exist = false; + } + else { + if (t->index == kPointerInvalidIndex) { // must be object name + if (!v->IsObject()) + v->SetObject(); // Change to Object + } + else { // object name or array index + if (!v->IsArray() && !v->IsObject()) + v->SetArray(); // Change to Array + } + + if (v->IsArray()) { + if (t->index >= v->Size()) { + v->Reserve(t->index + 1, allocator); + while (t->index >= v->Size()) + v->PushBack(ValueType().Move(), allocator); + exist = false; + } + v = &((*v)[t->index]); + } + else { + typename ValueType::MemberIterator m = v->FindMember(GenericStringRef(t->name, t->length)); + if (m == v->MemberEnd()) { + v->AddMember(ValueType(t->name, t->length, allocator).Move(), ValueType().Move(), allocator); + v = &(--v->MemberEnd())->value; // Assumes AddMember() appends at the end + exist = false; + } + else + v = &m->value; + } + } + } + + if (alreadyExist) + *alreadyExist = exist; + + return *v; + } + + //! Creates a value in a document. + /*! + \param document A document to be resolved. + \param alreadyExist If non-null, it stores whether the resolved value is already exist. + \return The resolved newly created, or already exists value. + */ + template + ValueType& Create(GenericDocument& document, bool* alreadyExist = 0) const { + return Create(document, document.GetAllocator(), alreadyExist); + } + + //@} + + //!@name Query value + //@{ + + //! Query a value in a subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param unresolvedTokenIndex If the pointer cannot resolve a token in the pointer, this parameter can obtain the index of unresolved token. + \return Pointer to the value if it can be resolved. Otherwise null. + + \note + There are only 3 situations when a value cannot be resolved: + 1. A value in the path is not an array nor object. + 2. An object value does not contain the token. + 3. A token is out of range of an array value. + + Use unresolvedTokenIndex to retrieve the token index. + */ + ValueType* Get(ValueType& root, size_t* unresolvedTokenIndex = 0) const { + RAPIDJSON_ASSERT(IsValid()); + ValueType* v = &root; + for (const Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + switch (v->GetType()) { + case kObjectType: + { + typename ValueType::MemberIterator m = v->FindMember(GenericStringRef(t->name, t->length)); + if (m == v->MemberEnd()) + break; + v = &m->value; + } + continue; + case kArrayType: + if (t->index == kPointerInvalidIndex || t->index >= v->Size()) + break; + v = &((*v)[t->index]); + continue; + default: + break; + } + + // Error: unresolved token + if (unresolvedTokenIndex) + *unresolvedTokenIndex = static_cast(t - tokens_); + return 0; + } + return v; + } + + //! Query a const value in a const subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \return Pointer to the value if it can be resolved. Otherwise null. + */ + const ValueType* Get(const ValueType& root, size_t* unresolvedTokenIndex = 0) const { + return Get(const_cast(root), unresolvedTokenIndex); + } + + //@} + + //!@name Query a value with default + //@{ + + //! Query a value in a subtree with default value. + /*! + Similar to Get(), but if the specified value do not exists, it creates all parents and clone the default value. + So that this function always succeed. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param defaultValue Default value to be cloned if the value was not exists. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& GetWithDefault(ValueType& root, const ValueType& defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + Value& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.CopyFrom(defaultValue, allocator); + } + + //! Query a value in a subtree with default null-terminated string. + ValueType& GetWithDefault(ValueType& root, const Ch* defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + Value& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.SetString(defaultValue, allocator); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Query a value in a subtree with default std::basic_string. + ValueType& GetWithDefault(ValueType& root, const std::basic_string& defaultValue, typename ValueType::AllocatorType& allocator) const { + bool alreadyExist; + Value& v = Create(root, allocator, &alreadyExist); + return alreadyExist ? v : v.SetString(defaultValue, allocator); + } +#endif + + //! Query a value in a subtree with default primitive value. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + GetWithDefault(ValueType& root, T defaultValue, typename ValueType::AllocatorType& allocator) const { + return GetWithDefault(root, ValueType(defaultValue).Move(), allocator); + } + + //! Query a value in a document with default value. + template + ValueType& GetWithDefault(GenericDocument& document, const ValueType& defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + + //! Query a value in a document with default null-terminated string. + template + ValueType& GetWithDefault(GenericDocument& document, const Ch* defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Query a value in a document with default std::basic_string. + template + ValueType& GetWithDefault(GenericDocument& document, const std::basic_string& defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } +#endif + + //! Query a value in a document with default primitive value. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + GetWithDefault(GenericDocument& document, T defaultValue) const { + return GetWithDefault(document, defaultValue, document.GetAllocator()); + } + + //@} + + //!@name Set a value + //@{ + + //! Set a value in a subtree, with move semantics. + /*! + It creates all parents if they are not exist or types are different to the tokens. + So this function always succeeds but potentially remove existing values. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param value Value to be set. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& Set(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = value; + } + + //! Set a value in a subtree, with copy semantics. + ValueType& Set(ValueType& root, const ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator).CopyFrom(value, allocator); + } + + //! Set a null-terminated string in a subtree. + ValueType& Set(ValueType& root, const Ch* value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value, allocator).Move(); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Set a std::basic_string in a subtree. + ValueType& Set(ValueType& root, const std::basic_string& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value, allocator).Move(); + } +#endif + + //! Set a primitive value in a subtree. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + Set(ValueType& root, T value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator) = ValueType(value).Move(); + } + + //! Set a value in a document, with move semantics. + template + ValueType& Set(GenericDocument& document, ValueType& value) const { + return Create(document) = value; + } + + //! Set a value in a document, with copy semantics. + template + ValueType& Set(GenericDocument& document, const ValueType& value) const { + return Create(document).CopyFrom(value, document.GetAllocator()); + } + + //! Set a null-terminated string in a document. + template + ValueType& Set(GenericDocument& document, const Ch* value) const { + return Create(document) = ValueType(value, document.GetAllocator()).Move(); + } + +#if RAPIDJSON_HAS_STDSTRING + //! Sets a std::basic_string in a document. + template + ValueType& Set(GenericDocument& document, const std::basic_string& value) const { + return Create(document) = ValueType(value, document.GetAllocator()).Move(); + } +#endif + + //! Set a primitive value in a document. + /*! + \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c bool + */ + template + RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (ValueType&)) + Set(GenericDocument& document, T value) const { + return Create(document) = value; + } + + //@} + + //!@name Swap a value + //@{ + + //! Swap a value with a value in a subtree. + /*! + It creates all parents if they are not exist or types are different to the tokens. + So this function always succeeds but potentially remove existing values. + + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \param value Value to be swapped. + \param allocator Allocator for creating the values if the specified value or its parents are not exist. + \see Create() + */ + ValueType& Swap(ValueType& root, ValueType& value, typename ValueType::AllocatorType& allocator) const { + return Create(root, allocator).Swap(value); + } + + //! Swap a value with a value in a document. + template + ValueType& Swap(GenericDocument& document, ValueType& value) const { + return Create(document).Swap(value); + } + + //@} + + //! Erase a value in a subtree. + /*! + \param root Root value of a DOM sub-tree to be resolved. It can be any value other than document root. + \return Whether the resolved value is found and erased. + + \note Erasing with an empty pointer \c Pointer(""), i.e. the root, always fail and return false. + */ + bool Erase(ValueType& root) const { + RAPIDJSON_ASSERT(IsValid()); + if (tokenCount_ == 0) // Cannot erase the root + return false; + + ValueType* v = &root; + const Token* last = tokens_ + (tokenCount_ - 1); + for (const Token *t = tokens_; t != last; ++t) { + switch (v->GetType()) { + case kObjectType: + { + typename ValueType::MemberIterator m = v->FindMember(GenericStringRef(t->name, t->length)); + if (m == v->MemberEnd()) + return false; + v = &m->value; + } + break; + case kArrayType: + if (t->index == kPointerInvalidIndex || t->index >= v->Size()) + return false; + v = &((*v)[t->index]); + break; + default: + return false; + } + } + + switch (v->GetType()) { + case kObjectType: + return v->EraseMember(GenericStringRef(last->name, last->length)); + case kArrayType: + if (last->index == kPointerInvalidIndex || last->index >= v->Size()) + return false; + v->Erase(v->Begin() + last->index); + return true; + default: + return false; + } + } + +private: + //! Clone the content from rhs to this. + /*! + \param rhs Source pointer. + \param extraToken Extra tokens to be allocated. + \param extraNameBufferSize Extra name buffer size (in number of Ch) to be allocated. + \return Start of non-occupied name buffer, for storing extra names. + */ + Ch* CopyFromRaw(const GenericPointer& rhs, size_t extraToken = 0, size_t extraNameBufferSize = 0) { + if (!allocator_) // allocator is independently owned. + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + + size_t nameBufferSize = rhs.tokenCount_; // null terminators for tokens + for (Token *t = rhs.tokens_; t != rhs.tokens_ + rhs.tokenCount_; ++t) + nameBufferSize += t->length; + + tokenCount_ = rhs.tokenCount_ + extraToken; + tokens_ = static_cast(allocator_->Malloc(tokenCount_ * sizeof(Token) + (nameBufferSize + extraNameBufferSize) * sizeof(Ch))); + nameBuffer_ = reinterpret_cast(tokens_ + tokenCount_); + if (rhs.tokenCount_ > 0) { + std::memcpy(tokens_, rhs.tokens_, rhs.tokenCount_ * sizeof(Token)); + } + if (nameBufferSize > 0) { + std::memcpy(nameBuffer_, rhs.nameBuffer_, nameBufferSize * sizeof(Ch)); + } + + // Adjust pointers to name buffer + std::ptrdiff_t diff = nameBuffer_ - rhs.nameBuffer_; + for (Token *t = tokens_; t != tokens_ + rhs.tokenCount_; ++t) + t->name += diff; + + return nameBuffer_ + nameBufferSize; + } + + //! Check whether a character should be percent-encoded. + /*! + According to RFC 3986 2.3 Unreserved Characters. + \param c The character (code unit) to be tested. + */ + bool NeedPercentEncode(Ch c) const { + return !((c >= '0' && c <= '9') || (c >= 'A' && c <='Z') || (c >= 'a' && c <= 'z') || c == '-' || c == '.' || c == '_' || c =='~'); + } + + //! Parse a JSON String or its URI fragment representation into tokens. +#ifndef __clang__ // -Wdocumentation + /*! + \param source Either a JSON Pointer string, or its URI fragment representation. Not need to be null terminated. + \param length Length of the source string. + \note Source cannot be JSON String Representation of JSON Pointer, e.g. In "/\u0000", \u0000 will not be unescaped. + */ +#endif + void Parse(const Ch* source, size_t length) { + RAPIDJSON_ASSERT(source != NULL); + RAPIDJSON_ASSERT(nameBuffer_ == 0); + RAPIDJSON_ASSERT(tokens_ == 0); + + // Create own allocator if user did not supply. + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + + // Count number of '/' as tokenCount + tokenCount_ = 0; + for (const Ch* s = source; s != source + length; s++) + if (*s == '/') + tokenCount_++; + + Token* token = tokens_ = static_cast(allocator_->Malloc(tokenCount_ * sizeof(Token) + length * sizeof(Ch))); + Ch* name = nameBuffer_ = reinterpret_cast(tokens_ + tokenCount_); + size_t i = 0; + + // Detect if it is a URI fragment + bool uriFragment = false; + if (source[i] == '#') { + uriFragment = true; + i++; + } + + if (i != length && source[i] != '/') { + parseErrorCode_ = kPointerParseErrorTokenMustBeginWithSolidus; + goto error; + } + + while (i < length) { + RAPIDJSON_ASSERT(source[i] == '/'); + i++; // consumes '/' + + token->name = name; + bool isNumber = true; + + while (i < length && source[i] != '/') { + Ch c = source[i]; + if (uriFragment) { + // Decoding percent-encoding for URI fragment + if (c == '%') { + PercentDecodeStream is(&source[i], source + length); + GenericInsituStringStream os(name); + Ch* begin = os.PutBegin(); + if (!Transcoder, EncodingType>().Validate(is, os) || !is.IsValid()) { + parseErrorCode_ = kPointerParseErrorInvalidPercentEncoding; + goto error; + } + size_t len = os.PutEnd(begin); + i += is.Tell() - 1; + if (len == 1) + c = *name; + else { + name += len; + isNumber = false; + i++; + continue; + } + } + else if (NeedPercentEncode(c)) { + parseErrorCode_ = kPointerParseErrorCharacterMustPercentEncode; + goto error; + } + } + + i++; + + // Escaping "~0" -> '~', "~1" -> '/' + if (c == '~') { + if (i < length) { + c = source[i]; + if (c == '0') c = '~'; + else if (c == '1') c = '/'; + else { + parseErrorCode_ = kPointerParseErrorInvalidEscape; + goto error; + } + i++; + } + else { + parseErrorCode_ = kPointerParseErrorInvalidEscape; + goto error; + } + } + + // First check for index: all of characters are digit + if (c < '0' || c > '9') + isNumber = false; + + *name++ = c; + } + token->length = static_cast(name - token->name); + if (token->length == 0) + isNumber = false; + *name++ = '\0'; // Null terminator + + // Second check for index: more than one digit cannot have leading zero + if (isNumber && token->length > 1 && token->name[0] == '0') + isNumber = false; + + // String to SizeType conversion + SizeType n = 0; + if (isNumber) { + for (size_t j = 0; j < token->length; j++) { + SizeType m = n * 10 + static_cast(token->name[j] - '0'); + if (m < n) { // overflow detection + isNumber = false; + break; + } + n = m; + } + } + + token->index = isNumber ? n : kPointerInvalidIndex; + token++; + } + + RAPIDJSON_ASSERT(name <= nameBuffer_ + length); // Should not overflow buffer + parseErrorCode_ = kPointerParseErrorNone; + return; + + error: + Allocator::Free(tokens_); + nameBuffer_ = 0; + tokens_ = 0; + tokenCount_ = 0; + parseErrorOffset_ = i; + return; + } + + //! Stringify to string or URI fragment representation. + /*! + \tparam uriFragment True for stringifying to URI fragment representation. False for string representation. + \tparam OutputStream type of output stream. + \param os The output stream. + */ + template + bool Stringify(OutputStream& os) const { + RAPIDJSON_ASSERT(IsValid()); + + if (uriFragment) + os.Put('#'); + + for (Token *t = tokens_; t != tokens_ + tokenCount_; ++t) { + os.Put('/'); + for (size_t j = 0; j < t->length; j++) { + Ch c = t->name[j]; + if (c == '~') { + os.Put('~'); + os.Put('0'); + } + else if (c == '/') { + os.Put('~'); + os.Put('1'); + } + else if (uriFragment && NeedPercentEncode(c)) { + // Transcode to UTF8 sequence + GenericStringStream source(&t->name[j]); + PercentEncodeStream target(os); + if (!Transcoder >().Validate(source, target)) + return false; + j += source.Tell() - 1; + } + else + os.Put(c); + } + } + return true; + } + + //! A helper stream for decoding a percent-encoded sequence into code unit. + /*! + This stream decodes %XY triplet into code unit (0-255). + If it encounters invalid characters, it sets output code unit as 0 and + mark invalid, and to be checked by IsValid(). + */ + class PercentDecodeStream { + public: + typedef typename ValueType::Ch Ch; + + //! Constructor + /*! + \param source Start of the stream + \param end Past-the-end of the stream. + */ + PercentDecodeStream(const Ch* source, const Ch* end) : src_(source), head_(source), end_(end), valid_(true) {} + + Ch Take() { + if (*src_ != '%' || src_ + 3 > end_) { // %XY triplet + valid_ = false; + return 0; + } + src_++; + Ch c = 0; + for (int j = 0; j < 2; j++) { + c = static_cast(c << 4); + Ch h = *src_; + if (h >= '0' && h <= '9') c = static_cast(c + h - '0'); + else if (h >= 'A' && h <= 'F') c = static_cast(c + h - 'A' + 10); + else if (h >= 'a' && h <= 'f') c = static_cast(c + h - 'a' + 10); + else { + valid_ = false; + return 0; + } + src_++; + } + return c; + } + + size_t Tell() const { return static_cast(src_ - head_); } + bool IsValid() const { return valid_; } + + private: + const Ch* src_; //!< Current read position. + const Ch* head_; //!< Original head of the string. + const Ch* end_; //!< Past-the-end position. + bool valid_; //!< Whether the parsing is valid. + }; + + //! A helper stream to encode character (UTF-8 code unit) into percent-encoded sequence. + template + class PercentEncodeStream { + public: + PercentEncodeStream(OutputStream& os) : os_(os) {} + void Put(char c) { // UTF-8 must be byte + unsigned char u = static_cast(c); + static const char hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + os_.Put('%'); + os_.Put(hexDigits[u >> 4]); + os_.Put(hexDigits[u & 15]); + } + private: + OutputStream& os_; + }; + + Allocator* allocator_; //!< The current allocator. It is either user-supplied or equal to ownAllocator_. + Allocator* ownAllocator_; //!< Allocator owned by this Pointer. + Ch* nameBuffer_; //!< A buffer containing all names in tokens. + Token* tokens_; //!< A list of tokens. + size_t tokenCount_; //!< Number of tokens in tokens_. + size_t parseErrorOffset_; //!< Offset in code unit when parsing fail. + PointerParseErrorCode parseErrorCode_; //!< Parsing error code. +}; + +//! GenericPointer for Value (UTF-8, default allocator). +typedef GenericPointer Pointer; + +//!@name Helper functions for GenericPointer +//@{ + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& CreateValueByPointer(T& root, const GenericPointer& pointer, typename T::AllocatorType& a) { + return pointer.Create(root, a); +} + +template +typename T::ValueType& CreateValueByPointer(T& root, const CharType(&source)[N], typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Create(root, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& CreateValueByPointer(DocumentType& document, const GenericPointer& pointer) { + return pointer.Create(document); +} + +template +typename DocumentType::ValueType& CreateValueByPointer(DocumentType& document, const CharType(&source)[N]) { + return GenericPointer(source, N - 1).Create(document); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType* GetValueByPointer(T& root, const GenericPointer& pointer, size_t* unresolvedTokenIndex = 0) { + return pointer.Get(root, unresolvedTokenIndex); +} + +template +const typename T::ValueType* GetValueByPointer(const T& root, const GenericPointer& pointer, size_t* unresolvedTokenIndex = 0) { + return pointer.Get(root, unresolvedTokenIndex); +} + +template +typename T::ValueType* GetValueByPointer(T& root, const CharType (&source)[N], size_t* unresolvedTokenIndex = 0) { + return GenericPointer(source, N - 1).Get(root, unresolvedTokenIndex); +} + +template +const typename T::ValueType* GetValueByPointer(const T& root, const CharType(&source)[N], size_t* unresolvedTokenIndex = 0) { + return GenericPointer(source, N - 1).Get(root, unresolvedTokenIndex); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const typename T::ValueType& defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const typename T::Ch* defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, const std::basic_string& defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +GetValueByPointerWithDefault(T& root, const GenericPointer& pointer, T2 defaultValue, typename T::AllocatorType& a) { + return pointer.GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const typename T::ValueType& defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const typename T::Ch* defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& GetValueByPointerWithDefault(T& root, const CharType(&source)[N], const std::basic_string& defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +GetValueByPointerWithDefault(T& root, const CharType(&source)[N], T2 defaultValue, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).GetWithDefault(root, defaultValue, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::ValueType& defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::Ch* defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, const std::basic_string& defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +GetValueByPointerWithDefault(DocumentType& document, const GenericPointer& pointer, T2 defaultValue) { + return pointer.GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const typename DocumentType::ValueType& defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const typename DocumentType::Ch* defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], const std::basic_string& defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +GetValueByPointerWithDefault(DocumentType& document, const CharType(&source)[N], T2 defaultValue) { + return GenericPointer(source, N - 1).GetWithDefault(document, defaultValue); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const typename T::Ch* value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& SetValueByPointer(T& root, const GenericPointer& pointer, const std::basic_string& value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +SetValueByPointer(T& root, const GenericPointer& pointer, T2 value, typename T::AllocatorType& a) { + return pointer.Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const typename T::Ch* value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename T::ValueType& SetValueByPointer(T& root, const CharType(&source)[N], const std::basic_string& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename T::ValueType&)) +SetValueByPointer(T& root, const CharType(&source)[N], T2 value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Set(root, value, a); +} + +// No allocator parameter + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, typename DocumentType::ValueType& value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::ValueType& value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const typename DocumentType::Ch* value) { + return pointer.Set(document, value); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const GenericPointer& pointer, const std::basic_string& value) { + return pointer.Set(document, value); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +SetValueByPointer(DocumentType& document, const GenericPointer& pointer, T2 value) { + return pointer.Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const typename DocumentType::Ch* value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +#if RAPIDJSON_HAS_STDSTRING +template +typename DocumentType::ValueType& SetValueByPointer(DocumentType& document, const CharType(&source)[N], const std::basic_string& value) { + return GenericPointer(source, N - 1).Set(document, value); +} +#endif + +template +RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr, internal::IsGenericValue >), (typename DocumentType::ValueType&)) +SetValueByPointer(DocumentType& document, const CharType(&source)[N], T2 value) { + return GenericPointer(source, N - 1).Set(document, value); +} + +////////////////////////////////////////////////////////////////////////////// + +template +typename T::ValueType& SwapValueByPointer(T& root, const GenericPointer& pointer, typename T::ValueType& value, typename T::AllocatorType& a) { + return pointer.Swap(root, value, a); +} + +template +typename T::ValueType& SwapValueByPointer(T& root, const CharType(&source)[N], typename T::ValueType& value, typename T::AllocatorType& a) { + return GenericPointer(source, N - 1).Swap(root, value, a); +} + +template +typename DocumentType::ValueType& SwapValueByPointer(DocumentType& document, const GenericPointer& pointer, typename DocumentType::ValueType& value) { + return pointer.Swap(document, value); +} + +template +typename DocumentType::ValueType& SwapValueByPointer(DocumentType& document, const CharType(&source)[N], typename DocumentType::ValueType& value) { + return GenericPointer(source, N - 1).Swap(document, value); +} + +////////////////////////////////////////////////////////////////////////////// + +template +bool EraseValueByPointer(T& root, const GenericPointer& pointer) { + return pointer.Erase(root); +} + +template +bool EraseValueByPointer(T& root, const CharType(&source)[N]) { + return GenericPointer(source, N - 1).Erase(root); +} + +//@} + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_POINTER_H_ diff --git a/rapidjson-1.1.0/rapid_json/prettywriter.h b/rapidjson-1.1.0/rapid_json/prettywriter.h new file mode 100644 index 000000000..0dcb0fee9 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/prettywriter.h @@ -0,0 +1,255 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_PRETTYWRITER_H_ +#define RAPIDJSON_PRETTYWRITER_H_ + +#include "writer.h" + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Combination of PrettyWriter format flags. +/*! \see PrettyWriter::SetFormatOptions + */ +enum PrettyFormatOptions { + kFormatDefault = 0, //!< Default pretty formatting. + kFormatSingleLineArray = 1 //!< Format arrays on a single line. +}; + +//! Writer with indentation and spacing. +/*! + \tparam OutputStream Type of ouptut os. + \tparam SourceEncoding Encoding of source string. + \tparam TargetEncoding Encoding of output stream. + \tparam StackAllocator Type of allocator for allocating memory of stack. +*/ +template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags> +class PrettyWriter : public Writer { +public: + typedef Writer Base; + typedef typename Base::Ch Ch; + + //! Constructor + /*! \param os Output stream. + \param allocator User supplied allocator. If it is null, it will create a private one. + \param levelDepth Initial capacity of stack. + */ + explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : + Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {} + + + explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) : + Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {} + + //! Set custom indentation. + /*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r'). + \param indentCharCount Number of indent characters for each indentation level. + \note The default indentation is 4 spaces. + */ + PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) { + RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r'); + indentChar_ = indentChar; + indentCharCount_ = indentCharCount; + return *this; + } + + //! Set pretty writer formatting options. + /*! \param options Formatting options. + */ + PrettyWriter& SetFormatOptions(PrettyFormatOptions options) { + formatOptions_ = options; + return *this; + } + + /*! @name Implementation of Handler + \see Handler + */ + //@{ + + bool Null() { PrettyPrefix(kNullType); return Base::WriteNull(); } + bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::WriteBool(b); } + bool Int(int i) { PrettyPrefix(kNumberType); return Base::WriteInt(i); } + bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::WriteUint(u); } + bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::WriteInt64(i64); } + bool Uint64(uint64_t u64) { PrettyPrefix(kNumberType); return Base::WriteUint64(u64); } + bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); } + + bool RawNumber(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + PrettyPrefix(kNumberType); + return Base::WriteString(str, length); + } + + bool String(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + PrettyPrefix(kStringType); + return Base::WriteString(str, length); + } + +#if RAPIDJSON_HAS_STDSTRING + bool String(const std::basic_string& str) { + return String(str.data(), SizeType(str.size())); + } +#endif + + bool StartObject() { + PrettyPrefix(kObjectType); + new (Base::level_stack_.template Push()) typename Base::Level(false); + return Base::WriteStartObject(); + } + + bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + +#if RAPIDJSON_HAS_STDSTRING + bool Key(const std::basic_string& str) { + return Key(str.data(), SizeType(str.size())); + } +#endif + + bool EndObject(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); + RAPIDJSON_ASSERT(!Base::level_stack_.template Top()->inArray); + bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; + + if (!empty) { + Base::os_->Put('\n'); + WriteIndent(); + } + bool ret = Base::WriteEndObject(); + (void)ret; + RAPIDJSON_ASSERT(ret == true); + if (Base::level_stack_.Empty()) // end of json text + Base::os_->Flush(); + return true; + } + + bool StartArray() { + PrettyPrefix(kArrayType); + new (Base::level_stack_.template Push()) typename Base::Level(true); + return Base::WriteStartArray(); + } + + bool EndArray(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level)); + RAPIDJSON_ASSERT(Base::level_stack_.template Top()->inArray); + bool empty = Base::level_stack_.template Pop(1)->valueCount == 0; + + if (!empty && !(formatOptions_ & kFormatSingleLineArray)) { + Base::os_->Put('\n'); + WriteIndent(); + } + bool ret = Base::WriteEndArray(); + (void)ret; + RAPIDJSON_ASSERT(ret == true); + if (Base::level_stack_.Empty()) // end of json text + Base::os_->Flush(); + return true; + } + + //@} + + /*! @name Convenience extensions */ + //@{ + + //! Simpler but slower overload. + bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } + + //@} + + //! Write a raw JSON value. + /*! + For user to write a stringified JSON as a value. + + \param json A well-formed JSON value. It should not contain null character within [0, length - 1] range. + \param length Length of the json. + \param type Type of the root of json. + \note When using PrettyWriter::RawValue(), the result json may not be indented correctly. + */ + bool RawValue(const Ch* json, size_t length, Type type) { PrettyPrefix(type); return Base::WriteRawValue(json, length); } + +protected: + void PrettyPrefix(Type type) { + (void)type; + if (Base::level_stack_.GetSize() != 0) { // this value is not at root + typename Base::Level* level = Base::level_stack_.template Top(); + + if (level->inArray) { + if (level->valueCount > 0) { + Base::os_->Put(','); // add comma if it is not the first element in array + if (formatOptions_ & kFormatSingleLineArray) + Base::os_->Put(' '); + } + + if (!(formatOptions_ & kFormatSingleLineArray)) { + Base::os_->Put('\n'); + WriteIndent(); + } + } + else { // in object + if (level->valueCount > 0) { + if (level->valueCount % 2 == 0) { + Base::os_->Put(','); + Base::os_->Put('\n'); + } + else { + Base::os_->Put(':'); + Base::os_->Put(' '); + } + } + else + Base::os_->Put('\n'); + + if (level->valueCount % 2 == 0) + WriteIndent(); + } + if (!level->inArray && level->valueCount % 2 == 0) + RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name + level->valueCount++; + } + else { + RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root. + Base::hasRoot_ = true; + } + } + + void WriteIndent() { + size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_; + PutN(*Base::os_, static_cast(indentChar_), count); + } + + Ch indentChar_; + unsigned indentCharCount_; + PrettyFormatOptions formatOptions_; + +private: + // Prohibit copy constructor & assignment operator. + PrettyWriter(const PrettyWriter&); + PrettyWriter& operator=(const PrettyWriter&); +}; + +RAPIDJSON_NAMESPACE_END + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/rapidjson-1.1.0/rapid_json/rapidjson.h b/rapidjson-1.1.0/rapid_json/rapidjson.h new file mode 100644 index 000000000..053b2ce43 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/rapidjson.h @@ -0,0 +1,615 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_RAPIDJSON_H_ +#define RAPIDJSON_RAPIDJSON_H_ + +/*!\file rapidjson.h + \brief common definitions and configuration + + \see RAPIDJSON_CONFIG + */ + +/*! \defgroup RAPIDJSON_CONFIG RapidJSON configuration + \brief Configuration macros for library features + + Some RapidJSON features are configurable to adapt the library to a wide + variety of platforms, environments and usage scenarios. Most of the + features can be configured in terms of overriden or predefined + preprocessor macros at compile-time. + + Some additional customization is available in the \ref RAPIDJSON_ERRORS APIs. + + \note These macros should be given on the compiler command-line + (where applicable) to avoid inconsistent values when compiling + different translation units of a single application. + */ + +#include // malloc(), realloc(), free(), size_t +#include // memset(), memcpy(), memmove(), memcmp() + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_VERSION_STRING +// +// ALWAYS synchronize the following 3 macros with corresponding variables in /CMakeLists.txt. +// + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +// token stringification +#define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x) +#define RAPIDJSON_DO_STRINGIFY(x) #x +//!@endcond + +/*! \def RAPIDJSON_MAJOR_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Major version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_MINOR_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Minor version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_PATCH_VERSION + \ingroup RAPIDJSON_CONFIG + \brief Patch version of RapidJSON in integer. +*/ +/*! \def RAPIDJSON_VERSION_STRING + \ingroup RAPIDJSON_CONFIG + \brief Version of RapidJSON in ".." string format. +*/ +#define RAPIDJSON_MAJOR_VERSION 1 +#define RAPIDJSON_MINOR_VERSION 1 +#define RAPIDJSON_PATCH_VERSION 0 +#define RAPIDJSON_VERSION_STRING \ + RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION) + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NAMESPACE_(BEGIN|END) +/*! \def RAPIDJSON_NAMESPACE + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace + + In order to avoid symbol clashes and/or "One Definition Rule" errors + between multiple inclusions of (different versions of) RapidJSON in + a single binary, users can customize the name of the main RapidJSON + namespace. + + In case of a single nesting level, defining \c RAPIDJSON_NAMESPACE + to a custom name (e.g. \c MyRapidJSON) is sufficient. If multiple + levels are needed, both \ref RAPIDJSON_NAMESPACE_BEGIN and \ref + RAPIDJSON_NAMESPACE_END need to be defined as well: + + \code + // in some .cpp file + #define RAPIDJSON_NAMESPACE my::rapidjson + #define RAPIDJSON_NAMESPACE_BEGIN namespace my { namespace rapidjson { + #define RAPIDJSON_NAMESPACE_END } } + #include "rapidjson/..." + \endcode + + \see rapidjson + */ +/*! \def RAPIDJSON_NAMESPACE_BEGIN + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace (opening expression) + \see RAPIDJSON_NAMESPACE +*/ +/*! \def RAPIDJSON_NAMESPACE_END + \ingroup RAPIDJSON_CONFIG + \brief provide custom rapidjson namespace (closing expression) + \see RAPIDJSON_NAMESPACE +*/ +#ifndef RAPIDJSON_NAMESPACE +#define RAPIDJSON_NAMESPACE rapidjson +#endif +#ifndef RAPIDJSON_NAMESPACE_BEGIN +#define RAPIDJSON_NAMESPACE_BEGIN namespace RAPIDJSON_NAMESPACE { +#endif +#ifndef RAPIDJSON_NAMESPACE_END +#define RAPIDJSON_NAMESPACE_END } +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_HAS_STDSTRING + +#ifndef RAPIDJSON_HAS_STDSTRING +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_HAS_STDSTRING 1 // force generation of documentation +#else +#define RAPIDJSON_HAS_STDSTRING 0 // no std::string support by default +#endif +/*! \def RAPIDJSON_HAS_STDSTRING + \ingroup RAPIDJSON_CONFIG + \brief Enable RapidJSON support for \c std::string + + By defining this preprocessor symbol to \c 1, several convenience functions for using + \ref rapidjson::GenericValue with \c std::string are enabled, especially + for construction and comparison. + + \hideinitializer +*/ +#endif // !defined(RAPIDJSON_HAS_STDSTRING) + +#if RAPIDJSON_HAS_STDSTRING +#include +#endif // RAPIDJSON_HAS_STDSTRING + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NO_INT64DEFINE + +/*! \def RAPIDJSON_NO_INT64DEFINE + \ingroup RAPIDJSON_CONFIG + \brief Use external 64-bit integer types. + + RapidJSON requires the 64-bit integer types \c int64_t and \c uint64_t types + to be available at global scope. + + If users have their own definition, define RAPIDJSON_NO_INT64DEFINE to + prevent RapidJSON from defining its own types. +*/ +#ifndef RAPIDJSON_NO_INT64DEFINE +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#if defined(_MSC_VER) && (_MSC_VER < 1800) // Visual Studio 2013 +#include "msinttypes/stdint.h" +#include "msinttypes/inttypes.h" +#else +// Other compilers should have this. +#include +#include +#endif +//!@endcond +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_NO_INT64DEFINE +#endif +#endif // RAPIDJSON_NO_INT64TYPEDEF + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_FORCEINLINE + +#ifndef RAPIDJSON_FORCEINLINE +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#if defined(_MSC_VER) && defined(NDEBUG) +#define RAPIDJSON_FORCEINLINE __forceinline +#elif defined(__GNUC__) && __GNUC__ >= 4 && defined(NDEBUG) +#define RAPIDJSON_FORCEINLINE __attribute__((always_inline)) +#else +#define RAPIDJSON_FORCEINLINE +#endif +//!@endcond +#endif // RAPIDJSON_FORCEINLINE + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ENDIAN +#define RAPIDJSON_LITTLEENDIAN 0 //!< Little endian machine +#define RAPIDJSON_BIGENDIAN 1 //!< Big endian machine + +//! Endianness of the machine. +/*! + \def RAPIDJSON_ENDIAN + \ingroup RAPIDJSON_CONFIG + + GCC 4.6 provided macro for detecting endianness of the target machine. But other + compilers may not have this. User can define RAPIDJSON_ENDIAN to either + \ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN. + + Default detection implemented with reference to + \li https://gcc.gnu.org/onlinedocs/gcc-4.6.0/cpp/Common-Predefined-Macros.html + \li http://www.boost.org/doc/libs/1_42_0/boost/detail/endian.hpp +*/ +#ifndef RAPIDJSON_ENDIAN +// Detect with GCC 4.6's macro +# ifdef __BYTE_ORDER__ +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif // __BYTE_ORDER__ +// Detect with GLIBC's endian.h +# elif defined(__GLIBC__) +# include +# if (__BYTE_ORDER == __LITTLE_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif (__BYTE_ORDER == __BIG_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif // __GLIBC__ +// Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro +# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +// Detect with architecture macros +# elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__) +# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN +# elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__bfin__) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(_MSC_VER) && defined(_M_ARM) +# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN +# elif defined(RAPIDJSON_DOXYGEN_RUNNING) +# define RAPIDJSON_ENDIAN +# else +# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN. +# endif +#endif // RAPIDJSON_ENDIAN + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_64BIT + +//! Whether using 64-bit architecture +#ifndef RAPIDJSON_64BIT +#if defined(__LP64__) || (defined(__x86_64__) && defined(__ILP32__)) || defined(_WIN64) || defined(__EMSCRIPTEN__) +#define RAPIDJSON_64BIT 1 +#else +#define RAPIDJSON_64BIT 0 +#endif +#endif // RAPIDJSON_64BIT + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ALIGN + +//! Data alignment of the machine. +/*! \ingroup RAPIDJSON_CONFIG + \param x pointer to align + + Some machines require strict data alignment. Currently the default uses 4 bytes + alignment on 32-bit platforms and 8 bytes alignment for 64-bit platforms. + User can customize by defining the RAPIDJSON_ALIGN function macro. +*/ +#ifndef RAPIDJSON_ALIGN +#if RAPIDJSON_64BIT == 1 +#define RAPIDJSON_ALIGN(x) (((x) + static_cast(7u)) & ~static_cast(7u)) +#else +#define RAPIDJSON_ALIGN(x) (((x) + 3u) & ~3u) +#endif +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_UINT64_C2 + +//! Construct a 64-bit literal by a pair of 32-bit integer. +/*! + 64-bit literal with or without ULL suffix is prone to compiler warnings. + UINT64_C() is C macro which cause compilation problems. + Use this macro to define 64-bit constants by a pair of 32-bit integer. +*/ +#ifndef RAPIDJSON_UINT64_C2 +#define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast(high32) << 32) | static_cast(low32)) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_48BITPOINTER_OPTIMIZATION + +//! Use only lower 48-bit address for some pointers. +/*! + \ingroup RAPIDJSON_CONFIG + + This optimization uses the fact that current X86-64 architecture only implement lower 48-bit virtual address. + The higher 16-bit can be used for storing other data. + \c GenericValue uses this optimization to reduce its size form 24 bytes to 16 bytes in 64-bit architecture. +*/ +#ifndef RAPIDJSON_48BITPOINTER_OPTIMIZATION +#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64) +#define RAPIDJSON_48BITPOINTER_OPTIMIZATION 1 +#else +#define RAPIDJSON_48BITPOINTER_OPTIMIZATION 0 +#endif +#endif // RAPIDJSON_48BITPOINTER_OPTIMIZATION + +#if RAPIDJSON_48BITPOINTER_OPTIMIZATION == 1 +#if RAPIDJSON_64BIT != 1 +#error RAPIDJSON_48BITPOINTER_OPTIMIZATION can only be set to 1 when RAPIDJSON_64BIT=1 +#endif +#define RAPIDJSON_SETPOINTER(type, p, x) (p = reinterpret_cast((reinterpret_cast(p) & static_cast(RAPIDJSON_UINT64_C2(0xFFFF0000, 0x00000000))) | reinterpret_cast(reinterpret_cast(x)))) +#define RAPIDJSON_GETPOINTER(type, p) (reinterpret_cast(reinterpret_cast(p) & static_cast(RAPIDJSON_UINT64_C2(0x0000FFFF, 0xFFFFFFFF)))) +#else +#define RAPIDJSON_SETPOINTER(type, p, x) (p = (x)) +#define RAPIDJSON_GETPOINTER(type, p) (p) +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD + +/*! \def RAPIDJSON_SIMD + \ingroup RAPIDJSON_CONFIG + \brief Enable SSE2/SSE4.2 optimization. + + RapidJSON supports optimized implementations for some parsing operations + based on the SSE2 or SSE4.2 SIMD extensions on modern Intel-compatible + processors. + + To enable these optimizations, two different symbols can be defined; + \code + // Enable SSE2 optimization. + #define RAPIDJSON_SSE2 + + // Enable SSE4.2 optimization. + #define RAPIDJSON_SSE42 + \endcode + + \c RAPIDJSON_SSE42 takes precedence, if both are defined. + + If any of these symbols is defined, RapidJSON defines the macro + \c RAPIDJSON_SIMD to indicate the availability of the optimized code. +*/ +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \ + || defined(RAPIDJSON_DOXYGEN_RUNNING) +#define RAPIDJSON_SIMD +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_NO_SIZETYPEDEFINE + +#ifndef RAPIDJSON_NO_SIZETYPEDEFINE +/*! \def RAPIDJSON_NO_SIZETYPEDEFINE + \ingroup RAPIDJSON_CONFIG + \brief User-provided \c SizeType definition. + + In order to avoid using 32-bit size types for indexing strings and arrays, + define this preprocessor symbol and provide the type rapidjson::SizeType + before including RapidJSON: + \code + #define RAPIDJSON_NO_SIZETYPEDEFINE + namespace rapidjson { typedef ::std::size_t SizeType; } + #include "rapidjson/..." + \endcode + + \see rapidjson::SizeType +*/ +#ifdef RAPIDJSON_DOXYGEN_RUNNING +#define RAPIDJSON_NO_SIZETYPEDEFINE +#endif +RAPIDJSON_NAMESPACE_BEGIN +//! Size type (for string lengths, array sizes, etc.) +/*! RapidJSON uses 32-bit array/string indices even on 64-bit platforms, + instead of using \c size_t. Users may override the SizeType by defining + \ref RAPIDJSON_NO_SIZETYPEDEFINE. +*/ +typedef unsigned SizeType; +RAPIDJSON_NAMESPACE_END +#endif + +// always import std::size_t to rapidjson namespace +RAPIDJSON_NAMESPACE_BEGIN +using std::size_t; +RAPIDJSON_NAMESPACE_END + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_ASSERT + +//! Assertion. +/*! \ingroup RAPIDJSON_CONFIG + By default, rapidjson uses C \c assert() for internal assertions. + User can override it by defining RAPIDJSON_ASSERT(x) macro. + + \note Parsing errors are handled and can be customized by the + \ref RAPIDJSON_ERRORS APIs. +*/ +#ifndef RAPIDJSON_ASSERT +#include +#define RAPIDJSON_ASSERT(x) assert(x) +#endif // RAPIDJSON_ASSERT + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_STATIC_ASSERT + +// Adopt from boost +#ifndef RAPIDJSON_STATIC_ASSERT +#ifndef __clang__ +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#endif +RAPIDJSON_NAMESPACE_BEGIN +template struct STATIC_ASSERTION_FAILURE; +template <> struct STATIC_ASSERTION_FAILURE { enum { value = 1 }; }; +template struct StaticAssertTest {}; +RAPIDJSON_NAMESPACE_END + +#define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y) +#define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y) +#define RAPIDJSON_DO_JOIN2(X, Y) X##Y + +#if defined(__GNUC__) +#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused)) +#else +#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE +#endif +#ifndef __clang__ +//!@endcond +#endif + +/*! \def RAPIDJSON_STATIC_ASSERT + \brief (Internal) macro to check for conditions at compile-time + \param x compile-time condition + \hideinitializer + */ +#define RAPIDJSON_STATIC_ASSERT(x) \ + typedef ::RAPIDJSON_NAMESPACE::StaticAssertTest< \ + sizeof(::RAPIDJSON_NAMESPACE::STATIC_ASSERTION_FAILURE)> \ + RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE +#endif + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_LIKELY, RAPIDJSON_UNLIKELY + +//! Compiler branching hint for expression with high probability to be true. +/*! + \ingroup RAPIDJSON_CONFIG + \param x Boolean expression likely to be true. +*/ +#ifndef RAPIDJSON_LIKELY +#if defined(__GNUC__) || defined(__clang__) +#define RAPIDJSON_LIKELY(x) __builtin_expect(!!(x), 1) +#else +#define RAPIDJSON_LIKELY(x) (x) +#endif +#endif + +//! Compiler branching hint for expression with low probability to be true. +/*! + \ingroup RAPIDJSON_CONFIG + \param x Boolean expression unlikely to be true. +*/ +#ifndef RAPIDJSON_UNLIKELY +#if defined(__GNUC__) || defined(__clang__) +#define RAPIDJSON_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +#define RAPIDJSON_UNLIKELY(x) (x) +#endif +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Helpers + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN + +#define RAPIDJSON_MULTILINEMACRO_BEGIN do { +#define RAPIDJSON_MULTILINEMACRO_END \ +} while((void)0, 0) + +// adopted from Boost +#define RAPIDJSON_VERSION_CODE(x,y,z) \ + (((x)*100000) + ((y)*100) + (z)) + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF + +#if defined(__GNUC__) +#define RAPIDJSON_GNUC \ + RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__) +#endif + +#if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,2,0)) + +#define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x)) +#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x) +#define RAPIDJSON_DIAG_OFF(x) \ + RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x))) + +// push/pop support in Clang and GCC>=4.6 +#if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) +#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) +#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) +#else // GCC >= 4.2, < 4.6 +#define RAPIDJSON_DIAG_PUSH /* ignored */ +#define RAPIDJSON_DIAG_POP /* ignored */ +#endif + +#elif defined(_MSC_VER) + +// pragma (MSVC specific) +#define RAPIDJSON_PRAGMA(x) __pragma(x) +#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x)) + +#define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x) +#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push) +#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop) + +#else + +#define RAPIDJSON_DIAG_OFF(x) /* ignored */ +#define RAPIDJSON_DIAG_PUSH /* ignored */ +#define RAPIDJSON_DIAG_POP /* ignored */ + +#endif // RAPIDJSON_DIAG_* + +/////////////////////////////////////////////////////////////////////////////// +// C++11 features + +#ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS +#if defined(__clang__) +#if __has_feature(cxx_rvalue_references) && \ + (defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306) +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 +#else +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0 +#endif +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ + (defined(_MSC_VER) && _MSC_VER >= 1600) + +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1 +#else +#define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0 +#endif +#endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS + +#ifndef RAPIDJSON_HAS_CXX11_NOEXCEPT +#if defined(__clang__) +#define RAPIDJSON_HAS_CXX11_NOEXCEPT __has_feature(cxx_noexcept) +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) +// (defined(_MSC_VER) && _MSC_VER >= ????) // not yet supported +#define RAPIDJSON_HAS_CXX11_NOEXCEPT 1 +#else +#define RAPIDJSON_HAS_CXX11_NOEXCEPT 0 +#endif +#endif +#if RAPIDJSON_HAS_CXX11_NOEXCEPT +#define RAPIDJSON_NOEXCEPT noexcept +#else +#define RAPIDJSON_NOEXCEPT /* noexcept */ +#endif // RAPIDJSON_HAS_CXX11_NOEXCEPT + +// no automatic detection, yet +#ifndef RAPIDJSON_HAS_CXX11_TYPETRAITS +#define RAPIDJSON_HAS_CXX11_TYPETRAITS 0 +#endif + +#ifndef RAPIDJSON_HAS_CXX11_RANGE_FOR +#if defined(__clang__) +#define RAPIDJSON_HAS_CXX11_RANGE_FOR __has_feature(cxx_range_for) +#elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \ + (defined(_MSC_VER) && _MSC_VER >= 1700) +#define RAPIDJSON_HAS_CXX11_RANGE_FOR 1 +#else +#define RAPIDJSON_HAS_CXX11_RANGE_FOR 0 +#endif +#endif // RAPIDJSON_HAS_CXX11_RANGE_FOR + +//!@endcond + +/////////////////////////////////////////////////////////////////////////////// +// new/delete + +#ifndef RAPIDJSON_NEW +///! customization point for global \c new +#define RAPIDJSON_NEW(x) new x +#endif +#ifndef RAPIDJSON_DELETE +///! customization point for global \c delete +#define RAPIDJSON_DELETE(x) delete x +#endif + +/////////////////////////////////////////////////////////////////////////////// +// Type + +/*! \namespace rapidjson + \brief main RapidJSON namespace + \see RAPIDJSON_NAMESPACE +*/ +RAPIDJSON_NAMESPACE_BEGIN + +//! Type of JSON value +enum Type { + kNullType = 0, //!< null + kFalseType = 1, //!< false + kTrueType = 2, //!< true + kObjectType = 3, //!< object + kArrayType = 4, //!< array + kStringType = 5, //!< string + kNumberType = 6 //!< number +}; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/rapidjson-1.1.0/rapid_json/reader.h b/rapidjson-1.1.0/rapid_json/reader.h new file mode 100644 index 000000000..19f8849b1 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/reader.h @@ -0,0 +1,1879 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_READER_H_ +#define RAPIDJSON_READER_H_ + +/*! \file reader.h */ + +#include "allocators.h" +#include "stream.h" +#include "encodedstream.h" +#include "internal/meta.h" +#include "internal/stack.h" +#include "internal/strtod.h" +#include + +#if defined(RAPIDJSON_SIMD) && defined(_MSC_VER) +#include +#pragma intrinsic(_BitScanForward) +#endif +#ifdef RAPIDJSON_SSE42 +#include +#elif defined(RAPIDJSON_SSE2) +#include +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +RAPIDJSON_DIAG_OFF(4702) // unreachable code +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(old-style-cast) +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(switch-enum) +#endif + +#ifdef __GNUC__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(effc++) +#endif + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define RAPIDJSON_NOTHING /* deliberately empty */ +#ifndef RAPIDJSON_PARSE_ERROR_EARLY_RETURN +#define RAPIDJSON_PARSE_ERROR_EARLY_RETURN(value) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + if (RAPIDJSON_UNLIKELY(HasParseError())) { return value; } \ + RAPIDJSON_MULTILINEMACRO_END +#endif +#define RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID \ + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(RAPIDJSON_NOTHING) +//!@endcond + +/*! \def RAPIDJSON_PARSE_ERROR_NORETURN + \ingroup RAPIDJSON_ERRORS + \brief Macro to indicate a parse error. + \param parseErrorCode \ref rapidjson::ParseErrorCode of the error + \param offset position of the error in JSON input (\c size_t) + + This macros can be used as a customization point for the internal + error handling mechanism of RapidJSON. + + A common usage model is to throw an exception instead of requiring the + caller to explicitly check the \ref rapidjson::GenericReader::Parse's + return value: + + \code + #define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode,offset) \ + throw ParseException(parseErrorCode, #parseErrorCode, offset) + + #include // std::runtime_error + #include "rapidjson/error/error.h" // rapidjson::ParseResult + + struct ParseException : std::runtime_error, rapidjson::ParseResult { + ParseException(rapidjson::ParseErrorCode code, const char* msg, size_t offset) + : std::runtime_error(msg), ParseResult(code, offset) {} + }; + + #include "rapidjson/reader.h" + \endcode + + \see RAPIDJSON_PARSE_ERROR, rapidjson::GenericReader::Parse + */ +#ifndef RAPIDJSON_PARSE_ERROR_NORETURN +#define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + RAPIDJSON_ASSERT(!HasParseError()); /* Error can only be assigned once */ \ + SetParseError(parseErrorCode, offset); \ + RAPIDJSON_MULTILINEMACRO_END +#endif + +/*! \def RAPIDJSON_PARSE_ERROR + \ingroup RAPIDJSON_ERRORS + \brief (Internal) macro to indicate and handle a parse error. + \param parseErrorCode \ref rapidjson::ParseErrorCode of the error + \param offset position of the error in JSON input (\c size_t) + + Invokes RAPIDJSON_PARSE_ERROR_NORETURN and stops the parsing. + + \see RAPIDJSON_PARSE_ERROR_NORETURN + \hideinitializer + */ +#ifndef RAPIDJSON_PARSE_ERROR +#define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset) \ + RAPIDJSON_MULTILINEMACRO_BEGIN \ + RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset); \ + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; \ + RAPIDJSON_MULTILINEMACRO_END +#endif + +#include "error/error.h" // ParseErrorCode, ParseResult + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// ParseFlag + +/*! \def RAPIDJSON_PARSE_DEFAULT_FLAGS + \ingroup RAPIDJSON_CONFIG + \brief User-defined kParseDefaultFlags definition. + + User can define this as any \c ParseFlag combinations. +*/ +#ifndef RAPIDJSON_PARSE_DEFAULT_FLAGS +#define RAPIDJSON_PARSE_DEFAULT_FLAGS kParseNoFlags +#endif + +//! Combination of parseFlags +/*! \see Reader::Parse, Document::Parse, Document::ParseInsitu, Document::ParseStream + */ +enum ParseFlag { + kParseNoFlags = 0, //!< No flags are set. + kParseInsituFlag = 1, //!< In-situ(destructive) parsing. + kParseValidateEncodingFlag = 2, //!< Validate encoding of JSON strings. + kParseIterativeFlag = 4, //!< Iterative(constant complexity in terms of function call stack size) parsing. + kParseStopWhenDoneFlag = 8, //!< After parsing a complete JSON root from stream, stop further processing the rest of stream. When this flag is used, parser will not generate kParseErrorDocumentRootNotSingular error. + kParseFullPrecisionFlag = 16, //!< Parse number in full precision (but slower). + kParseCommentsFlag = 32, //!< Allow one-line (//) and multi-line (/**/) comments. + kParseNumbersAsStringsFlag = 64, //!< Parse all numbers (ints/doubles) as strings. + kParseTrailingCommasFlag = 128, //!< Allow trailing commas at the end of objects and arrays. + kParseNanAndInfFlag = 256, //!< Allow parsing NaN, Inf, Infinity, -Inf and -Infinity as doubles. + kParseDefaultFlags = RAPIDJSON_PARSE_DEFAULT_FLAGS //!< Default parse flags. Can be customized by defining RAPIDJSON_PARSE_DEFAULT_FLAGS +}; + +/////////////////////////////////////////////////////////////////////////////// +// Handler + +/*! \class rapidjson::Handler + \brief Concept for receiving events from GenericReader upon parsing. + The functions return true if no error occurs. If they return false, + the event publisher should terminate the process. +\code +concept Handler { + typename Ch; + + bool Null(); + bool Bool(bool b); + bool Int(int i); + bool Uint(unsigned i); + bool Int64(int64_t i); + bool Uint64(uint64_t i); + bool Double(double d); + /// enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length) + bool RawNumber(const Ch* str, SizeType length, bool copy); + bool String(const Ch* str, SizeType length, bool copy); + bool StartObject(); + bool Key(const Ch* str, SizeType length, bool copy); + bool EndObject(SizeType memberCount); + bool StartArray(); + bool EndArray(SizeType elementCount); +}; +\endcode +*/ +/////////////////////////////////////////////////////////////////////////////// +// BaseReaderHandler + +//! Default implementation of Handler. +/*! This can be used as base class of any reader handler. + \note implements Handler concept +*/ +template, typename Derived = void> +struct BaseReaderHandler { + typedef typename Encoding::Ch Ch; + + typedef typename internal::SelectIf, BaseReaderHandler, Derived>::Type Override; + + bool Default() { return true; } + bool Null() { return static_cast(*this).Default(); } + bool Bool(bool) { return static_cast(*this).Default(); } + bool Int(int) { return static_cast(*this).Default(); } + bool Uint(unsigned) { return static_cast(*this).Default(); } + bool Int64(int64_t) { return static_cast(*this).Default(); } + bool Uint64(uint64_t) { return static_cast(*this).Default(); } + bool Double(double) { return static_cast(*this).Default(); } + /// enabled via kParseNumbersAsStringsFlag, string is not null-terminated (use length) + bool RawNumber(const Ch* str, SizeType len, bool copy) { return static_cast(*this).String(str, len, copy); } + bool String(const Ch*, SizeType, bool) { return static_cast(*this).Default(); } + bool StartObject() { return static_cast(*this).Default(); } + bool Key(const Ch* str, SizeType len, bool copy) { return static_cast(*this).String(str, len, copy); } + bool EndObject(SizeType) { return static_cast(*this).Default(); } + bool StartArray() { return static_cast(*this).Default(); } + bool EndArray(SizeType) { return static_cast(*this).Default(); } +}; + +/////////////////////////////////////////////////////////////////////////////// +// StreamLocalCopy + +namespace internal { + +template::copyOptimization> +class StreamLocalCopy; + +//! Do copy optimization. +template +class StreamLocalCopy { +public: + StreamLocalCopy(Stream& original) : s(original), original_(original) {} + ~StreamLocalCopy() { original_ = s; } + + Stream s; + +private: + StreamLocalCopy& operator=(const StreamLocalCopy&) /* = delete */; + + Stream& original_; +}; + +//! Keep reference. +template +class StreamLocalCopy { +public: + StreamLocalCopy(Stream& original) : s(original) {} + + Stream& s; + +private: + StreamLocalCopy& operator=(const StreamLocalCopy&) /* = delete */; +}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// SkipWhitespace + +//! Skip the JSON white spaces in a stream. +/*! \param is A input stream for skipping white spaces. + \note This function has SSE2/SSE4.2 specialization. +*/ +template +void SkipWhitespace(InputStream& is) { + internal::StreamLocalCopy copy(is); + InputStream& s(copy.s); + + typename InputStream::Ch c; + while ((c = s.Peek()) == ' ' || c == '\n' || c == '\r' || c == '\t') + s.Take(); +} + +inline const char* SkipWhitespace(const char* p, const char* end) { + while (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + return p; +} + +#ifdef RAPIDJSON_SSE42 +//! Skip whitespace with SSE 4.2 pcmpistrm instruction, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // The rest of string using SIMD + static const char whitespace[16] = " \n\r\t"; + const __m128i w = _mm_loadu_si128(reinterpret_cast(&whitespace[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const int r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY)); + if (r != 0) { // some of characters is non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } +} + +inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { + // Fast return for single non-whitespace + if (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + else + return p; + + // The middle of string using SIMD + static const char whitespace[16] = " \n\r\t"; + const __m128i w = _mm_loadu_si128(reinterpret_cast(&whitespace[0])); + + for (; p <= end - 16; p += 16) { + const __m128i s = _mm_loadu_si128(reinterpret_cast(p)); + const int r = _mm_cvtsi128_si32(_mm_cmpistrm(w, s, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK | _SIDD_NEGATIVE_POLARITY)); + if (r != 0) { // some of characters is non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } + + return SkipWhitespace(p, end); +} + +#elif defined(RAPIDJSON_SSE2) + +//! Skip whitespace with SSE2 instructions, testing 16 8-byte characters at once. +inline const char *SkipWhitespace_SIMD(const char* p) { + // Fast return for single non-whitespace + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // 16-byte align to the next boundary + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t') + ++p; + else + return p; + + // The rest of string + #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c } + static const char whitespaces[4][16] = { C16(' '), C16('\n'), C16('\r'), C16('\t') }; + #undef C16 + + const __m128i w0 = _mm_loadu_si128(reinterpret_cast(&whitespaces[0][0])); + const __m128i w1 = _mm_loadu_si128(reinterpret_cast(&whitespaces[1][0])); + const __m128i w2 = _mm_loadu_si128(reinterpret_cast(&whitespaces[2][0])); + const __m128i w3 = _mm_loadu_si128(reinterpret_cast(&whitespaces[3][0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + __m128i x = _mm_cmpeq_epi8(s, w0); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3)); + unsigned short r = static_cast(~_mm_movemask_epi8(x)); + if (r != 0) { // some of characters may be non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } +} + +inline const char *SkipWhitespace_SIMD(const char* p, const char* end) { + // Fast return for single non-whitespace + if (p != end && (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t')) + ++p; + else + return p; + + // The rest of string + #define C16(c) { c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c } + static const char whitespaces[4][16] = { C16(' '), C16('\n'), C16('\r'), C16('\t') }; + #undef C16 + + const __m128i w0 = _mm_loadu_si128(reinterpret_cast(&whitespaces[0][0])); + const __m128i w1 = _mm_loadu_si128(reinterpret_cast(&whitespaces[1][0])); + const __m128i w2 = _mm_loadu_si128(reinterpret_cast(&whitespaces[2][0])); + const __m128i w3 = _mm_loadu_si128(reinterpret_cast(&whitespaces[3][0])); + + for (; p <= end - 16; p += 16) { + const __m128i s = _mm_loadu_si128(reinterpret_cast(p)); + __m128i x = _mm_cmpeq_epi8(s, w0); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w1)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w2)); + x = _mm_or_si128(x, _mm_cmpeq_epi8(s, w3)); + unsigned short r = static_cast(~_mm_movemask_epi8(x)); + if (r != 0) { // some of characters may be non-whitespace +#ifdef _MSC_VER // Find the index of first non-whitespace + unsigned long offset; + _BitScanForward(&offset, r); + return p + offset; +#else + return p + __builtin_ffs(r) - 1; +#endif + } + } + + return SkipWhitespace(p, end); +} + +#endif // RAPIDJSON_SSE2 + +#ifdef RAPIDJSON_SIMD +//! Template function specialization for InsituStringStream +template<> inline void SkipWhitespace(InsituStringStream& is) { + is.src_ = const_cast(SkipWhitespace_SIMD(is.src_)); +} + +//! Template function specialization for StringStream +template<> inline void SkipWhitespace(StringStream& is) { + is.src_ = SkipWhitespace_SIMD(is.src_); +} + +template<> inline void SkipWhitespace(EncodedInputStream, MemoryStream>& is) { + is.is_.src_ = SkipWhitespace_SIMD(is.is_.src_, is.is_.end_); +} +#endif // RAPIDJSON_SIMD + +/////////////////////////////////////////////////////////////////////////////// +// GenericReader + +//! SAX-style JSON parser. Use \ref Reader for UTF8 encoding and default allocator. +/*! GenericReader parses JSON text from a stream, and send events synchronously to an + object implementing Handler concept. + + It needs to allocate a stack for storing a single decoded string during + non-destructive parsing. + + For in-situ parsing, the decoded string is directly written to the source + text string, no temporary buffer is required. + + A GenericReader object can be reused for parsing multiple JSON text. + + \tparam SourceEncoding Encoding of the input stream. + \tparam TargetEncoding Encoding of the parse output. + \tparam StackAllocator Allocator type for stack. +*/ +template +class GenericReader { +public: + typedef typename SourceEncoding::Ch Ch; //!< SourceEncoding character type + + //! Constructor. + /*! \param stackAllocator Optional allocator for allocating stack memory. (Only use for non-destructive parsing) + \param stackCapacity stack capacity in bytes for storing a single decoded string. (Only use for non-destructive parsing) + */ + GenericReader(StackAllocator* stackAllocator = 0, size_t stackCapacity = kDefaultStackCapacity) : stack_(stackAllocator, stackCapacity), parseResult_() {} + + //! Parse JSON text. + /*! \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept. + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + ParseResult Parse(InputStream& is, Handler& handler) { + if (parseFlags & kParseIterativeFlag) + return IterativeParse(is, handler); + + parseResult_.Clear(); + + ClearStackOnExit scope(*this); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (RAPIDJSON_UNLIKELY(is.Peek() == '\0')) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentEmpty, is.Tell()); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + else { + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (!(parseFlags & kParseStopWhenDoneFlag)) { + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + + if (RAPIDJSON_UNLIKELY(is.Peek() != '\0')) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorDocumentRootNotSingular, is.Tell()); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + } + } + + return parseResult_; + } + + //! Parse JSON text (with \ref kParseDefaultFlags) + /*! \tparam InputStream Type of input stream, implementing Stream concept + \tparam Handler Type of handler, implementing Handler concept. + \param is Input stream to be parsed. + \param handler The handler to receive events. + \return Whether the parsing is successful. + */ + template + ParseResult Parse(InputStream& is, Handler& handler) { + return Parse(is, handler); + } + + //! Whether a parse error has occured in the last parsing. + bool HasParseError() const { return parseResult_.IsError(); } + + //! Get the \ref ParseErrorCode of last parsing. + ParseErrorCode GetParseErrorCode() const { return parseResult_.Code(); } + + //! Get the position of last parsing error in input, 0 otherwise. + size_t GetErrorOffset() const { return parseResult_.Offset(); } + +protected: + void SetParseError(ParseErrorCode code, size_t offset) { parseResult_.Set(code, offset); } + +private: + // Prohibit copy constructor & assignment operator. + GenericReader(const GenericReader&); + GenericReader& operator=(const GenericReader&); + + void ClearStack() { stack_.Clear(); } + + // clear stack on any exit from ParseStream, e.g. due to exception + struct ClearStackOnExit { + explicit ClearStackOnExit(GenericReader& r) : r_(r) {} + ~ClearStackOnExit() { r_.ClearStack(); } + private: + GenericReader& r_; + ClearStackOnExit(const ClearStackOnExit&); + ClearStackOnExit& operator=(const ClearStackOnExit&); + }; + + template + void SkipWhitespaceAndComments(InputStream& is) { + SkipWhitespace(is); + + if (parseFlags & kParseCommentsFlag) { + while (RAPIDJSON_UNLIKELY(Consume(is, '/'))) { + if (Consume(is, '*')) { + while (true) { + if (RAPIDJSON_UNLIKELY(is.Peek() == '\0')) + RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); + else if (Consume(is, '*')) { + if (Consume(is, '/')) + break; + } + else + is.Take(); + } + } + else if (RAPIDJSON_LIKELY(Consume(is, '/'))) + while (is.Peek() != '\0' && is.Take() != '\n'); + else + RAPIDJSON_PARSE_ERROR(kParseErrorUnspecificSyntaxError, is.Tell()); + + SkipWhitespace(is); + } + } + } + + // Parse object: { string : value, ... } + template + void ParseObject(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == '{'); + is.Take(); // Skip '{' + + if (RAPIDJSON_UNLIKELY(!handler.StartObject())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, '}')) { + if (RAPIDJSON_UNLIKELY(!handler.EndObject(0))) // empty object + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + + for (SizeType memberCount = 0;;) { + if (RAPIDJSON_UNLIKELY(is.Peek() != '"')) + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); + + ParseString(is, handler, true); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (RAPIDJSON_UNLIKELY(!Consume(is, ':'))) + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ++memberCount; + + switch (is.Peek()) { + case ',': + is.Take(); + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + break; + case '}': + is.Take(); + if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + default: + RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); break; // This useless break is only for making warning and coverage happy + } + + if (parseFlags & kParseTrailingCommasFlag) { + if (is.Peek() == '}') { + if (RAPIDJSON_UNLIKELY(!handler.EndObject(memberCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + is.Take(); + return; + } + } + } + } + + // Parse array: [ value, ... ] + template + void ParseArray(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == '['); + is.Take(); // Skip '[' + + if (RAPIDJSON_UNLIKELY(!handler.StartArray())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, ']')) { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(0))) // empty array + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + + for (SizeType elementCount = 0;;) { + ParseValue(is, handler); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + ++elementCount; + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + + if (Consume(is, ',')) { + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + } + else if (Consume(is, ']')) { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(elementCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + return; + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); + + if (parseFlags & kParseTrailingCommasFlag) { + if (is.Peek() == ']') { + if (RAPIDJSON_UNLIKELY(!handler.EndArray(elementCount))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + is.Take(); + return; + } + } + } + } + + template + void ParseNull(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 'n'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'u') && Consume(is, 'l') && Consume(is, 'l'))) { + if (RAPIDJSON_UNLIKELY(!handler.Null())) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + void ParseTrue(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 't'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'r') && Consume(is, 'u') && Consume(is, 'e'))) { + if (RAPIDJSON_UNLIKELY(!handler.Bool(true))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + void ParseFalse(InputStream& is, Handler& handler) { + RAPIDJSON_ASSERT(is.Peek() == 'f'); + is.Take(); + + if (RAPIDJSON_LIKELY(Consume(is, 'a') && Consume(is, 'l') && Consume(is, 's') && Consume(is, 'e'))) { + if (RAPIDJSON_UNLIKELY(!handler.Bool(false))) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, is.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); + } + + template + RAPIDJSON_FORCEINLINE static bool Consume(InputStream& is, typename InputStream::Ch expect) { + if (RAPIDJSON_LIKELY(is.Peek() == expect)) { + is.Take(); + return true; + } + else + return false; + } + + // Helper function to parse four hexidecimal digits in \uXXXX in ParseString(). + template + unsigned ParseHex4(InputStream& is, size_t escapeOffset) { + unsigned codepoint = 0; + for (int i = 0; i < 4; i++) { + Ch c = is.Peek(); + codepoint <<= 4; + codepoint += static_cast(c); + if (c >= '0' && c <= '9') + codepoint -= '0'; + else if (c >= 'A' && c <= 'F') + codepoint -= 'A' - 10; + else if (c >= 'a' && c <= 'f') + codepoint -= 'a' - 10; + else { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorStringUnicodeEscapeInvalidHex, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(0); + } + is.Take(); + } + return codepoint; + } + + template + class StackStream { + public: + typedef CharType Ch; + + StackStream(internal::Stack& stack) : stack_(stack), length_(0) {} + RAPIDJSON_FORCEINLINE void Put(Ch c) { + *stack_.template Push() = c; + ++length_; + } + + RAPIDJSON_FORCEINLINE void* Push(SizeType count) { + length_ += count; + return stack_.template Push(count); + } + + size_t Length() const { return length_; } + + Ch* Pop() { + return stack_.template Pop(length_); + } + + private: + StackStream(const StackStream&); + StackStream& operator=(const StackStream&); + + internal::Stack& stack_; + SizeType length_; + }; + + // Parse string and generate String event. Different code paths for kParseInsituFlag. + template + void ParseString(InputStream& is, Handler& handler, bool isKey = false) { + internal::StreamLocalCopy copy(is); + InputStream& s(copy.s); + + RAPIDJSON_ASSERT(s.Peek() == '\"'); + s.Take(); // Skip '\"' + + bool success = false; + if (parseFlags & kParseInsituFlag) { + typename InputStream::Ch *head = s.PutBegin(); + ParseStringToStream(s, s); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + size_t length = s.PutEnd(head) - 1; + RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); + const typename TargetEncoding::Ch* const str = reinterpret_cast(head); + success = (isKey ? handler.Key(str, SizeType(length), false) : handler.String(str, SizeType(length), false)); + } + else { + StackStream stackStream(stack_); + ParseStringToStream(s, stackStream); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + SizeType length = static_cast(stackStream.Length()) - 1; + const typename TargetEncoding::Ch* const str = stackStream.Pop(); + success = (isKey ? handler.Key(str, length, true) : handler.String(str, length, true)); + } + if (RAPIDJSON_UNLIKELY(!success)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, s.Tell()); + } + + // Parse string to an output is + // This function handles the prefix/suffix double quotes, escaping, and optional encoding validation. + template + RAPIDJSON_FORCEINLINE void ParseStringToStream(InputStream& is, OutputStream& os) { +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 + static const char escape[256] = { + Z16, Z16, 0, 0,'\"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'/', + Z16, Z16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, + 0, 0,'\b', 0, 0, 0,'\f', 0, 0, 0, 0, 0, 0, 0,'\n', 0, + 0, 0,'\r', 0,'\t', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 + }; +#undef Z16 +//!@endcond + + for (;;) { + // Scan and copy string before "\\\"" or < 0x20. This is an optional optimzation. + if (!(parseFlags & kParseValidateEncodingFlag)) + ScanCopyUnescapedString(is, os); + + Ch c = is.Peek(); + if (RAPIDJSON_UNLIKELY(c == '\\')) { // Escape + size_t escapeOffset = is.Tell(); // For invalid escaping, report the inital '\\' as error offset + is.Take(); + Ch e = is.Peek(); + if ((sizeof(Ch) == 1 || unsigned(e) < 256) && RAPIDJSON_LIKELY(escape[static_cast(e)])) { + is.Take(); + os.Put(static_cast(escape[static_cast(e)])); + } + else if (RAPIDJSON_LIKELY(e == 'u')) { // Unicode + is.Take(); + unsigned codepoint = ParseHex4(is, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + if (RAPIDJSON_UNLIKELY(codepoint >= 0xD800 && codepoint <= 0xDBFF)) { + // Handle UTF-16 surrogate pair + if (RAPIDJSON_UNLIKELY(!Consume(is, '\\') || !Consume(is, 'u'))) + RAPIDJSON_PARSE_ERROR(kParseErrorStringUnicodeSurrogateInvalid, escapeOffset); + unsigned codepoint2 = ParseHex4(is, escapeOffset); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN_VOID; + if (RAPIDJSON_UNLIKELY(codepoint2 < 0xDC00 || codepoint2 > 0xDFFF)) + RAPIDJSON_PARSE_ERROR(kParseErrorStringUnicodeSurrogateInvalid, escapeOffset); + codepoint = (((codepoint - 0xD800) << 10) | (codepoint2 - 0xDC00)) + 0x10000; + } + TEncoding::Encode(os, codepoint); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorStringEscapeInvalid, escapeOffset); + } + else if (RAPIDJSON_UNLIKELY(c == '"')) { // Closing double quote + is.Take(); + os.Put('\0'); // null-terminate the string + return; + } + else if (RAPIDJSON_UNLIKELY(static_cast(c) < 0x20)) { // RFC 4627: unescaped = %x20-21 / %x23-5B / %x5D-10FFFF + if (c == '\0') + RAPIDJSON_PARSE_ERROR(kParseErrorStringMissQuotationMark, is.Tell()); + else + RAPIDJSON_PARSE_ERROR(kParseErrorStringEscapeInvalid, is.Tell()); + } + else { + size_t offset = is.Tell(); + if (RAPIDJSON_UNLIKELY((parseFlags & kParseValidateEncodingFlag ? + !Transcoder::Validate(is, os) : + !Transcoder::Transcode(is, os)))) + RAPIDJSON_PARSE_ERROR(kParseErrorStringInvalidEncoding, offset); + } + } + } + + template + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InputStream&, OutputStream&) { + // Do nothing for generic version + } + +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) + // StringStream -> StackStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(StringStream& is, StackStream& os) { + const char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + return; + } + else + os.Put(*p++); + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + SizeType length; + #ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; + #else + length = static_cast(__builtin_ffs(r) - 1); + #endif + char* q = reinterpret_cast(os.Push(length)); + for (size_t i = 0; i < length; i++) + q[i] = p[i]; + + p += length; + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(os.Push(16)), s); + } + + is.src_ = p; + } + + // InsituStringStream -> InsituStringStream + static RAPIDJSON_FORCEINLINE void ScanCopyUnescapedString(InsituStringStream& is, InsituStringStream& os) { + RAPIDJSON_ASSERT(&is == &os); + (void)os; + + if (is.src_ == is.dst_) { + SkipUnescapedString(is); + return; + } + + char* p = is.src_; + char *q = is.dst_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + while (p != nextAligned) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = p; + is.dst_ = q; + return; + } + else + *q++ = *p++; + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16, q += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + size_t length; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; +#else + length = static_cast(__builtin_ffs(r) - 1); +#endif + for (const char* pend = p + length; p != pend; ) + *q++ = *p++; + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(q), s); + } + + is.src_ = p; + is.dst_ = q; + } + + // When read/write pointers are the same for insitu stream, just skip unescaped characters + static RAPIDJSON_FORCEINLINE void SkipUnescapedString(InsituStringStream& is) { + RAPIDJSON_ASSERT(is.src_ == is.dst_); + char* p = is.src_; + + // Scan one by one until alignment (unaligned load may cross page boundary and cause crash) + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + for (; p != nextAligned; p++) + if (RAPIDJSON_UNLIKELY(*p == '\"') || RAPIDJSON_UNLIKELY(*p == '\\') || RAPIDJSON_UNLIKELY(static_cast(*p) < 0x20)) { + is.src_ = is.dst_ = p; + return; + } + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (;; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + size_t length; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + length = offset; +#else + length = static_cast(__builtin_ffs(r) - 1); +#endif + p += length; + break; + } + } + + is.src_ = is.dst_ = p; + } +#endif + + template + class NumberStream; + + template + class NumberStream { + public: + typedef typename InputStream::Ch Ch; + + NumberStream(GenericReader& reader, InputStream& s) : is(s) { (void)reader; } + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch Peek() const { return is.Peek(); } + RAPIDJSON_FORCEINLINE Ch TakePush() { return is.Take(); } + RAPIDJSON_FORCEINLINE Ch Take() { return is.Take(); } + RAPIDJSON_FORCEINLINE void Push(char) {} + + size_t Tell() { return is.Tell(); } + size_t Length() { return 0; } + const char* Pop() { return 0; } + + protected: + NumberStream& operator=(const NumberStream&); + + InputStream& is; + }; + + template + class NumberStream : public NumberStream { + typedef NumberStream Base; + public: + NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is), stackStream(reader.stack_) {} + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch TakePush() { + stackStream.Put(static_cast(Base::is.Peek())); + return Base::is.Take(); + } + + RAPIDJSON_FORCEINLINE void Push(char c) { + stackStream.Put(c); + } + + size_t Length() { return stackStream.Length(); } + + const char* Pop() { + stackStream.Put('\0'); + return stackStream.Pop(); + } + + private: + StackStream stackStream; + }; + + template + class NumberStream : public NumberStream { + typedef NumberStream Base; + public: + NumberStream(GenericReader& reader, InputStream& is) : Base(reader, is) {} + ~NumberStream() {} + + RAPIDJSON_FORCEINLINE Ch Take() { return Base::TakePush(); } + }; + + template + void ParseNumber(InputStream& is, Handler& handler) { + internal::StreamLocalCopy copy(is); + NumberStream s(*this, copy.s); + + size_t startOffset = s.Tell(); + double d = 0.0; + bool useNanOrInf = false; + + // Parse minus + bool minus = Consume(s, '-'); + + // Parse int: zero / ( digit1-9 *DIGIT ) + unsigned i = 0; + uint64_t i64 = 0; + bool use64bit = false; + int significandDigit = 0; + if (RAPIDJSON_UNLIKELY(s.Peek() == '0')) { + i = 0; + s.TakePush(); + } + else if (RAPIDJSON_LIKELY(s.Peek() >= '1' && s.Peek() <= '9')) { + i = static_cast(s.TakePush() - '0'); + + if (minus) + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i >= 214748364)) { // 2^31 = 2147483648 + if (RAPIDJSON_LIKELY(i != 214748364 || s.Peek() > '8')) { + i64 = i; + use64bit = true; + break; + } + } + i = i * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + else + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i >= 429496729)) { // 2^32 - 1 = 4294967295 + if (RAPIDJSON_LIKELY(i != 429496729 || s.Peek() > '5')) { + i64 = i; + use64bit = true; + break; + } + } + i = i * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + } + // Parse NaN or Infinity here + else if ((parseFlags & kParseNanAndInfFlag) && RAPIDJSON_LIKELY((s.Peek() == 'I' || s.Peek() == 'N'))) { + useNanOrInf = true; + if (RAPIDJSON_LIKELY(Consume(s, 'N') && Consume(s, 'a') && Consume(s, 'N'))) { + d = std::numeric_limits::quiet_NaN(); + } + else if (RAPIDJSON_LIKELY(Consume(s, 'I') && Consume(s, 'n') && Consume(s, 'f'))) { + d = (minus ? -std::numeric_limits::infinity() : std::numeric_limits::infinity()); + if (RAPIDJSON_UNLIKELY(s.Peek() == 'i' && !(Consume(s, 'i') && Consume(s, 'n') + && Consume(s, 'i') && Consume(s, 't') && Consume(s, 'y')))) + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, s.Tell()); + + // Parse 64bit int + bool useDouble = false; + if (use64bit) { + if (minus) + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC))) // 2^63 = 9223372036854775808 + if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x0CCCCCCC, 0xCCCCCCCC) || s.Peek() > '8')) { + d = static_cast(i64); + useDouble = true; + break; + } + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + else + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(i64 >= RAPIDJSON_UINT64_C2(0x19999999, 0x99999999))) // 2^64 - 1 = 18446744073709551615 + if (RAPIDJSON_LIKELY(i64 != RAPIDJSON_UINT64_C2(0x19999999, 0x99999999) || s.Peek() > '5')) { + d = static_cast(i64); + useDouble = true; + break; + } + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + significandDigit++; + } + } + + // Force double for big integer + if (useDouble) { + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (RAPIDJSON_UNLIKELY(d >= 1.7976931348623157e307)) // DBL_MAX / 10.0 + RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, startOffset); + d = d * 10 + (s.TakePush() - '0'); + } + } + + // Parse frac = decimal-point 1*DIGIT + int expFrac = 0; + size_t decimalPosition; + if (Consume(s, '.')) { + decimalPosition = s.Length(); + + if (RAPIDJSON_UNLIKELY(!(s.Peek() >= '0' && s.Peek() <= '9'))) + RAPIDJSON_PARSE_ERROR(kParseErrorNumberMissFraction, s.Tell()); + + if (!useDouble) { +#if RAPIDJSON_64BIT + // Use i64 to store significand in 64-bit architecture + if (!use64bit) + i64 = i; + + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (i64 > RAPIDJSON_UINT64_C2(0x1FFFFF, 0xFFFFFFFF)) // 2^53 - 1 for fast path + break; + else { + i64 = i64 * 10 + static_cast(s.TakePush() - '0'); + --expFrac; + if (i64 != 0) + significandDigit++; + } + } + + d = static_cast(i64); +#else + // Use double to store significand in 32-bit architecture + d = static_cast(use64bit ? i64 : i); +#endif + useDouble = true; + } + + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + if (significandDigit < 17) { + d = d * 10.0 + (s.TakePush() - '0'); + --expFrac; + if (RAPIDJSON_LIKELY(d > 0.0)) + significandDigit++; + } + else + s.TakePush(); + } + } + else + decimalPosition = s.Length(); // decimal position at the end of integer. + + // Parse exp = e [ minus / plus ] 1*DIGIT + int exp = 0; + if (Consume(s, 'e') || Consume(s, 'E')) { + if (!useDouble) { + d = static_cast(use64bit ? i64 : i); + useDouble = true; + } + + bool expMinus = false; + if (Consume(s, '+')) + ; + else if (Consume(s, '-')) + expMinus = true; + + if (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = static_cast(s.Take() - '0'); + if (expMinus) { + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = exp * 10 + static_cast(s.Take() - '0'); + if (exp >= 214748364) { // Issue #313: prevent overflow exponent + while (RAPIDJSON_UNLIKELY(s.Peek() >= '0' && s.Peek() <= '9')) // Consume the rest of exponent + s.Take(); + } + } + } + else { // positive exp + int maxExp = 308 - expFrac; + while (RAPIDJSON_LIKELY(s.Peek() >= '0' && s.Peek() <= '9')) { + exp = exp * 10 + static_cast(s.Take() - '0'); + if (RAPIDJSON_UNLIKELY(exp > maxExp)) + RAPIDJSON_PARSE_ERROR(kParseErrorNumberTooBig, startOffset); + } + } + } + else + RAPIDJSON_PARSE_ERROR(kParseErrorNumberMissExponent, s.Tell()); + + if (expMinus) + exp = -exp; + } + + // Finish parsing, call event according to the type of number. + bool cont = true; + + if (parseFlags & kParseNumbersAsStringsFlag) { + if (parseFlags & kParseInsituFlag) { + s.Pop(); // Pop stack no matter if it will be used or not. + typename InputStream::Ch* head = is.PutBegin(); + const size_t length = s.Tell() - startOffset; + RAPIDJSON_ASSERT(length <= 0xFFFFFFFF); + // unable to insert the \0 character here, it will erase the comma after this number + const typename TargetEncoding::Ch* const str = reinterpret_cast(head); + cont = handler.RawNumber(str, SizeType(length), false); + } + else { + SizeType numCharsToCopy = static_cast(s.Length()); + StringStream srcStream(s.Pop()); + StackStream dstStream(stack_); + while (numCharsToCopy--) { + Transcoder, TargetEncoding>::Transcode(srcStream, dstStream); + } + dstStream.Put('\0'); + const typename TargetEncoding::Ch* str = dstStream.Pop(); + const SizeType length = static_cast(dstStream.Length()) - 1; + cont = handler.RawNumber(str, SizeType(length), true); + } + } + else { + size_t length = s.Length(); + const char* decimal = s.Pop(); // Pop stack no matter if it will be used or not. + + if (useDouble) { + int p = exp + expFrac; + if (parseFlags & kParseFullPrecisionFlag) + d = internal::StrtodFullPrecision(d, p, decimal, length, decimalPosition, exp); + else + d = internal::StrtodNormalPrecision(d, p); + + cont = handler.Double(minus ? -d : d); + } + else if (useNanOrInf) { + cont = handler.Double(d); + } + else { + if (use64bit) { + if (minus) + cont = handler.Int64(static_cast(~i64 + 1)); + else + cont = handler.Uint64(i64); + } + else { + if (minus) + cont = handler.Int(static_cast(~i + 1)); + else + cont = handler.Uint(i); + } + } + } + if (RAPIDJSON_UNLIKELY(!cont)) + RAPIDJSON_PARSE_ERROR(kParseErrorTermination, startOffset); + } + + // Parse any JSON value + template + void ParseValue(InputStream& is, Handler& handler) { + switch (is.Peek()) { + case 'n': ParseNull (is, handler); break; + case 't': ParseTrue (is, handler); break; + case 'f': ParseFalse (is, handler); break; + case '"': ParseString(is, handler); break; + case '{': ParseObject(is, handler); break; + case '[': ParseArray (is, handler); break; + default : + ParseNumber(is, handler); + break; + + } + } + + // Iterative Parsing + + // States + enum IterativeParsingState { + IterativeParsingStartState = 0, + IterativeParsingFinishState, + IterativeParsingErrorState, + + // Object states + IterativeParsingObjectInitialState, + IterativeParsingMemberKeyState, + IterativeParsingKeyValueDelimiterState, + IterativeParsingMemberValueState, + IterativeParsingMemberDelimiterState, + IterativeParsingObjectFinishState, + + // Array states + IterativeParsingArrayInitialState, + IterativeParsingElementState, + IterativeParsingElementDelimiterState, + IterativeParsingArrayFinishState, + + // Single value state + IterativeParsingValueState + }; + + enum { cIterativeParsingStateCount = IterativeParsingValueState + 1 }; + + // Tokens + enum Token { + LeftBracketToken = 0, + RightBracketToken, + + LeftCurlyBracketToken, + RightCurlyBracketToken, + + CommaToken, + ColonToken, + + StringToken, + FalseToken, + TrueToken, + NullToken, + NumberToken, + + kTokenCount + }; + + RAPIDJSON_FORCEINLINE Token Tokenize(Ch c) { + +//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN +#define N NumberToken +#define N16 N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N + // Maps from ASCII to Token + static const unsigned char tokenMap[256] = { + N16, // 00~0F + N16, // 10~1F + N, N, StringToken, N, N, N, N, N, N, N, N, N, CommaToken, N, N, N, // 20~2F + N, N, N, N, N, N, N, N, N, N, ColonToken, N, N, N, N, N, // 30~3F + N16, // 40~4F + N, N, N, N, N, N, N, N, N, N, N, LeftBracketToken, N, RightBracketToken, N, N, // 50~5F + N, N, N, N, N, N, FalseToken, N, N, N, N, N, N, N, NullToken, N, // 60~6F + N, N, N, N, TrueToken, N, N, N, N, N, N, LeftCurlyBracketToken, N, RightCurlyBracketToken, N, N, // 70~7F + N16, N16, N16, N16, N16, N16, N16, N16 // 80~FF + }; +#undef N +#undef N16 +//!@endcond + + if (sizeof(Ch) == 1 || static_cast(c) < 256) + return static_cast(tokenMap[static_cast(c)]); + else + return NumberToken; + } + + RAPIDJSON_FORCEINLINE IterativeParsingState Predict(IterativeParsingState state, Token token) { + // current state x one lookahead token -> new state + static const char G[cIterativeParsingStateCount][kTokenCount] = { + // Start + { + IterativeParsingArrayInitialState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingValueState, // String + IterativeParsingValueState, // False + IterativeParsingValueState, // True + IterativeParsingValueState, // Null + IterativeParsingValueState // Number + }, + // Finish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Error(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // ObjectInitial + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberKeyState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // MemberKey + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingKeyValueDelimiterState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // KeyValueDelimiter + { + IterativeParsingArrayInitialState, // Left bracket(push MemberValue state) + IterativeParsingErrorState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push MemberValue state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberValueState, // String + IterativeParsingMemberValueState, // False + IterativeParsingMemberValueState, // True + IterativeParsingMemberValueState, // Null + IterativeParsingMemberValueState // Number + }, + // MemberValue + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingMemberDelimiterState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // MemberDelimiter + { + IterativeParsingErrorState, // Left bracket + IterativeParsingErrorState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingObjectFinishState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingMemberKeyState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // ObjectFinish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // ArrayInitial + { + IterativeParsingArrayInitialState, // Left bracket(push Element state) + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push Element state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingElementState, // String + IterativeParsingElementState, // False + IterativeParsingElementState, // True + IterativeParsingElementState, // Null + IterativeParsingElementState // Number + }, + // Element + { + IterativeParsingErrorState, // Left bracket + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingErrorState, // Left curly bracket + IterativeParsingErrorState, // Right curly bracket + IterativeParsingElementDelimiterState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingErrorState, // String + IterativeParsingErrorState, // False + IterativeParsingErrorState, // True + IterativeParsingErrorState, // Null + IterativeParsingErrorState // Number + }, + // ElementDelimiter + { + IterativeParsingArrayInitialState, // Left bracket(push Element state) + IterativeParsingArrayFinishState, // Right bracket + IterativeParsingObjectInitialState, // Left curly bracket(push Element state) + IterativeParsingErrorState, // Right curly bracket + IterativeParsingErrorState, // Comma + IterativeParsingErrorState, // Colon + IterativeParsingElementState, // String + IterativeParsingElementState, // False + IterativeParsingElementState, // True + IterativeParsingElementState, // Null + IterativeParsingElementState // Number + }, + // ArrayFinish(sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + }, + // Single Value (sink state) + { + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, IterativeParsingErrorState, + IterativeParsingErrorState + } + }; // End of G + + return static_cast(G[state][token]); + } + + // Make an advance in the token stream and state based on the candidate destination state which was returned by Transit(). + // May return a new state on state pop. + template + RAPIDJSON_FORCEINLINE IterativeParsingState Transit(IterativeParsingState src, Token token, IterativeParsingState dst, InputStream& is, Handler& handler) { + (void)token; + + switch (dst) { + case IterativeParsingErrorState: + return dst; + + case IterativeParsingObjectInitialState: + case IterativeParsingArrayInitialState: + { + // Push the state(Element or MemeberValue) if we are nested in another array or value of member. + // In this way we can get the correct state on ObjectFinish or ArrayFinish by frame pop. + IterativeParsingState n = src; + if (src == IterativeParsingArrayInitialState || src == IterativeParsingElementDelimiterState) + n = IterativeParsingElementState; + else if (src == IterativeParsingKeyValueDelimiterState) + n = IterativeParsingMemberValueState; + // Push current state. + *stack_.template Push(1) = n; + // Initialize and push the member/element count. + *stack_.template Push(1) = 0; + // Call handler + bool hr = (dst == IterativeParsingObjectInitialState) ? handler.StartObject() : handler.StartArray(); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return dst; + } + } + + case IterativeParsingMemberKeyState: + ParseString(is, handler, true); + if (HasParseError()) + return IterativeParsingErrorState; + else + return dst; + + case IterativeParsingKeyValueDelimiterState: + RAPIDJSON_ASSERT(token == ColonToken); + is.Take(); + return dst; + + case IterativeParsingMemberValueState: + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return dst; + + case IterativeParsingElementState: + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return dst; + + case IterativeParsingMemberDelimiterState: + case IterativeParsingElementDelimiterState: + is.Take(); + // Update member/element count. + *stack_.template Top() = *stack_.template Top() + 1; + return dst; + + case IterativeParsingObjectFinishState: + { + // Transit from delimiter is only allowed when trailing commas are enabled + if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingMemberDelimiterState) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorObjectMissName, is.Tell()); + return IterativeParsingErrorState; + } + // Get member count. + SizeType c = *stack_.template Pop(1); + // If the object is not empty, count the last member. + if (src == IterativeParsingMemberValueState) + ++c; + // Restore the state. + IterativeParsingState n = static_cast(*stack_.template Pop(1)); + // Transit to Finish state if this is the topmost scope. + if (n == IterativeParsingStartState) + n = IterativeParsingFinishState; + // Call handler + bool hr = handler.EndObject(c); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return n; + } + } + + case IterativeParsingArrayFinishState: + { + // Transit from delimiter is only allowed when trailing commas are enabled + if (!(parseFlags & kParseTrailingCommasFlag) && src == IterativeParsingElementDelimiterState) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorValueInvalid, is.Tell()); + return IterativeParsingErrorState; + } + // Get element count. + SizeType c = *stack_.template Pop(1); + // If the array is not empty, count the last element. + if (src == IterativeParsingElementState) + ++c; + // Restore the state. + IterativeParsingState n = static_cast(*stack_.template Pop(1)); + // Transit to Finish state if this is the topmost scope. + if (n == IterativeParsingStartState) + n = IterativeParsingFinishState; + // Call handler + bool hr = handler.EndArray(c); + // On handler short circuits the parsing. + if (!hr) { + RAPIDJSON_PARSE_ERROR_NORETURN(kParseErrorTermination, is.Tell()); + return IterativeParsingErrorState; + } + else { + is.Take(); + return n; + } + } + + default: + // This branch is for IterativeParsingValueState actually. + // Use `default:` rather than + // `case IterativeParsingValueState:` is for code coverage. + + // The IterativeParsingStartState is not enumerated in this switch-case. + // It is impossible for that case. And it can be caught by following assertion. + + // The IterativeParsingFinishState is not enumerated in this switch-case either. + // It is a "derivative" state which cannot triggered from Predict() directly. + // Therefore it cannot happen here. And it can be caught by following assertion. + RAPIDJSON_ASSERT(dst == IterativeParsingValueState); + + // Must be non-compound value. Or it would be ObjectInitial or ArrayInitial state. + ParseValue(is, handler); + if (HasParseError()) { + return IterativeParsingErrorState; + } + return IterativeParsingFinishState; + } + } + + template + void HandleError(IterativeParsingState src, InputStream& is) { + if (HasParseError()) { + // Error flag has been set. + return; + } + + switch (src) { + case IterativeParsingStartState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentEmpty, is.Tell()); return; + case IterativeParsingFinishState: RAPIDJSON_PARSE_ERROR(kParseErrorDocumentRootNotSingular, is.Tell()); return; + case IterativeParsingObjectInitialState: + case IterativeParsingMemberDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissName, is.Tell()); return; + case IterativeParsingMemberKeyState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissColon, is.Tell()); return; + case IterativeParsingMemberValueState: RAPIDJSON_PARSE_ERROR(kParseErrorObjectMissCommaOrCurlyBracket, is.Tell()); return; + case IterativeParsingKeyValueDelimiterState: + case IterativeParsingArrayInitialState: + case IterativeParsingElementDelimiterState: RAPIDJSON_PARSE_ERROR(kParseErrorValueInvalid, is.Tell()); return; + default: RAPIDJSON_ASSERT(src == IterativeParsingElementState); RAPIDJSON_PARSE_ERROR(kParseErrorArrayMissCommaOrSquareBracket, is.Tell()); return; + } + } + + template + ParseResult IterativeParse(InputStream& is, Handler& handler) { + parseResult_.Clear(); + ClearStackOnExit scope(*this); + IterativeParsingState state = IterativeParsingStartState; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + while (is.Peek() != '\0') { + Token t = Tokenize(is.Peek()); + IterativeParsingState n = Predict(state, t); + IterativeParsingState d = Transit(state, t, n, is, handler); + + if (d == IterativeParsingErrorState) { + HandleError(state, is); + break; + } + + state = d; + + // Do not further consume streams if a root JSON has been parsed. + if ((parseFlags & kParseStopWhenDoneFlag) && state == IterativeParsingFinishState) + break; + + SkipWhitespaceAndComments(is); + RAPIDJSON_PARSE_ERROR_EARLY_RETURN(parseResult_); + } + + // Handle the end of file. + if (state != IterativeParsingFinishState) + HandleError(state, is); + + return parseResult_; + } + + static const size_t kDefaultStackCapacity = 256; //!< Default stack capacity in bytes for storing a single decoded string. + internal::Stack stack_; //!< A stack for storing decoded string temporarily during non-destructive parsing. + ParseResult parseResult_; +}; // class GenericReader + +//! Reader with UTF8 encoding and default allocator. +typedef GenericReader, UTF8<> > Reader; + +RAPIDJSON_NAMESPACE_END + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + + +#ifdef __GNUC__ +RAPIDJSON_DIAG_POP +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_READER_H_ diff --git a/rapidjson-1.1.0/rapid_json/schema.h b/rapidjson-1.1.0/rapid_json/schema.h new file mode 100644 index 000000000..b182aa27f --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/schema.h @@ -0,0 +1,2006 @@ +// Tencent is pleased to support the open source community by making RapidJSON available-> +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip-> All rights reserved-> +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License-> You may obtain a copy of the License at +// +// http://opensource->org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied-> See the License for the +// specific language governing permissions and limitations under the License-> + +#ifndef RAPIDJSON_SCHEMA_H_ +#define RAPIDJSON_SCHEMA_H_ + +#include "document.h" +#include "pointer.h" +#include // abs, floor + +#if !defined(RAPIDJSON_SCHEMA_USE_INTERNALREGEX) +#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 1 +#else +#define RAPIDJSON_SCHEMA_USE_INTERNALREGEX 0 +#endif + +#if !RAPIDJSON_SCHEMA_USE_INTERNALREGEX && !defined(RAPIDJSON_SCHEMA_USE_STDREGEX) && (__cplusplus >=201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)) +#define RAPIDJSON_SCHEMA_USE_STDREGEX 1 +#else +#define RAPIDJSON_SCHEMA_USE_STDREGEX 0 +#endif + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX +#include "internal/regex.h" +#elif RAPIDJSON_SCHEMA_USE_STDREGEX +#include +#endif + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX || RAPIDJSON_SCHEMA_USE_STDREGEX +#define RAPIDJSON_SCHEMA_HAS_REGEX 1 +#else +#define RAPIDJSON_SCHEMA_HAS_REGEX 0 +#endif + +#ifndef RAPIDJSON_SCHEMA_VERBOSE +#define RAPIDJSON_SCHEMA_VERBOSE 0 +#endif + +#if RAPIDJSON_SCHEMA_VERBOSE +#include "stringbuffer.h" +#endif + +RAPIDJSON_DIAG_PUSH + +#if defined(__GNUC__) +RAPIDJSON_DIAG_OFF(effc++) +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_OFF(weak-vtables) +RAPIDJSON_DIAG_OFF(exit-time-destructors) +RAPIDJSON_DIAG_OFF(c++98-compat-pedantic) +RAPIDJSON_DIAG_OFF(variadic-macros) +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Verbose Utilities + +#if RAPIDJSON_SCHEMA_VERBOSE + +namespace internal { + +inline void PrintInvalidKeyword(const char* keyword) { + printf("Fail keyword: %s\n", keyword); +} + +inline void PrintInvalidKeyword(const wchar_t* keyword) { + wprintf(L"Fail keyword: %ls\n", keyword); +} + +inline void PrintInvalidDocument(const char* document) { + printf("Fail document: %s\n\n", document); +} + +inline void PrintInvalidDocument(const wchar_t* document) { + wprintf(L"Fail document: %ls\n\n", document); +} + +inline void PrintValidatorPointers(unsigned depth, const char* s, const char* d) { + printf("S: %*s%s\nD: %*s%s\n\n", depth * 4, " ", s, depth * 4, " ", d); +} + +inline void PrintValidatorPointers(unsigned depth, const wchar_t* s, const wchar_t* d) { + wprintf(L"S: %*ls%ls\nD: %*ls%ls\n\n", depth * 4, L" ", s, depth * 4, L" ", d); +} + +} // namespace internal + +#endif // RAPIDJSON_SCHEMA_VERBOSE + +/////////////////////////////////////////////////////////////////////////////// +// RAPIDJSON_INVALID_KEYWORD_RETURN + +#if RAPIDJSON_SCHEMA_VERBOSE +#define RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword) internal::PrintInvalidKeyword(keyword) +#else +#define RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword) +#endif + +#define RAPIDJSON_INVALID_KEYWORD_RETURN(keyword)\ +RAPIDJSON_MULTILINEMACRO_BEGIN\ + context.invalidKeyword = keyword.GetString();\ + RAPIDJSON_INVALID_KEYWORD_VERBOSE(keyword.GetString());\ + return false;\ +RAPIDJSON_MULTILINEMACRO_END + +/////////////////////////////////////////////////////////////////////////////// +// Forward declarations + +template +class GenericSchemaDocument; + +namespace internal { + +template +class Schema; + +/////////////////////////////////////////////////////////////////////////////// +// ISchemaValidator + +class ISchemaValidator { +public: + virtual ~ISchemaValidator() {} + virtual bool IsValid() const = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// ISchemaStateFactory + +template +class ISchemaStateFactory { +public: + virtual ~ISchemaStateFactory() {} + virtual ISchemaValidator* CreateSchemaValidator(const SchemaType&) = 0; + virtual void DestroySchemaValidator(ISchemaValidator* validator) = 0; + virtual void* CreateHasher() = 0; + virtual uint64_t GetHashCode(void* hasher) = 0; + virtual void DestroryHasher(void* hasher) = 0; + virtual void* MallocState(size_t size) = 0; + virtual void FreeState(void* p) = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// Hasher + +// For comparison of compound value +template +class Hasher { +public: + typedef typename Encoding::Ch Ch; + + Hasher(Allocator* allocator = 0, size_t stackCapacity = kDefaultSize) : stack_(allocator, stackCapacity) {} + + bool Null() { return WriteType(kNullType); } + bool Bool(bool b) { return WriteType(b ? kTrueType : kFalseType); } + bool Int(int i) { Number n; n.u.i = i; n.d = static_cast(i); return WriteNumber(n); } + bool Uint(unsigned u) { Number n; n.u.u = u; n.d = static_cast(u); return WriteNumber(n); } + bool Int64(int64_t i) { Number n; n.u.i = i; n.d = static_cast(i); return WriteNumber(n); } + bool Uint64(uint64_t u) { Number n; n.u.u = u; n.d = static_cast(u); return WriteNumber(n); } + bool Double(double d) { + Number n; + if (d < 0) n.u.i = static_cast(d); + else n.u.u = static_cast(d); + n.d = d; + return WriteNumber(n); + } + + bool RawNumber(const Ch* str, SizeType len, bool) { + WriteBuffer(kNumberType, str, len * sizeof(Ch)); + return true; + } + + bool String(const Ch* str, SizeType len, bool) { + WriteBuffer(kStringType, str, len * sizeof(Ch)); + return true; + } + + bool StartObject() { return true; } + bool Key(const Ch* str, SizeType len, bool copy) { return String(str, len, copy); } + bool EndObject(SizeType memberCount) { + uint64_t h = Hash(0, kObjectType); + uint64_t* kv = stack_.template Pop(memberCount * 2); + for (SizeType i = 0; i < memberCount; i++) + h ^= Hash(kv[i * 2], kv[i * 2 + 1]); // Use xor to achieve member order insensitive + *stack_.template Push() = h; + return true; + } + + bool StartArray() { return true; } + bool EndArray(SizeType elementCount) { + uint64_t h = Hash(0, kArrayType); + uint64_t* e = stack_.template Pop(elementCount); + for (SizeType i = 0; i < elementCount; i++) + h = Hash(h, e[i]); // Use hash to achieve element order sensitive + *stack_.template Push() = h; + return true; + } + + bool IsValid() const { return stack_.GetSize() == sizeof(uint64_t); } + + uint64_t GetHashCode() const { + RAPIDJSON_ASSERT(IsValid()); + return *stack_.template Top(); + } + +private: + static const size_t kDefaultSize = 256; + struct Number { + union U { + uint64_t u; + int64_t i; + }u; + double d; + }; + + bool WriteType(Type type) { return WriteBuffer(type, 0, 0); } + + bool WriteNumber(const Number& n) { return WriteBuffer(kNumberType, &n, sizeof(n)); } + + bool WriteBuffer(Type type, const void* data, size_t len) { + // FNV-1a from http://isthe.com/chongo/tech/comp/fnv/ + uint64_t h = Hash(RAPIDJSON_UINT64_C2(0x84222325, 0xcbf29ce4), type); + const unsigned char* d = static_cast(data); + for (size_t i = 0; i < len; i++) + h = Hash(h, d[i]); + *stack_.template Push() = h; + return true; + } + + static uint64_t Hash(uint64_t h, uint64_t d) { + static const uint64_t kPrime = RAPIDJSON_UINT64_C2(0x00000100, 0x000001b3); + h ^= d; + h *= kPrime; + return h; + } + + Stack stack_; +}; + +/////////////////////////////////////////////////////////////////////////////// +// SchemaValidationContext + +template +struct SchemaValidationContext { + typedef Schema SchemaType; + typedef ISchemaStateFactory SchemaValidatorFactoryType; + typedef typename SchemaType::ValueType ValueType; + typedef typename ValueType::Ch Ch; + + enum PatternValidatorType { + kPatternValidatorOnly, + kPatternValidatorWithProperty, + kPatternValidatorWithAdditionalProperty + }; + + SchemaValidationContext(SchemaValidatorFactoryType& f, const SchemaType* s) : + factory(f), + schema(s), + valueSchema(), + invalidKeyword(), + hasher(), + arrayElementHashCodes(), + validators(), + validatorCount(), + patternPropertiesValidators(), + patternPropertiesValidatorCount(), + patternPropertiesSchemas(), + patternPropertiesSchemaCount(), + valuePatternValidatorType(kPatternValidatorOnly), + propertyExist(), + inArray(false), + valueUniqueness(false), + arrayUniqueness(false) + { + } + + ~SchemaValidationContext() { + if (hasher) + factory.DestroryHasher(hasher); + if (validators) { + for (SizeType i = 0; i < validatorCount; i++) + factory.DestroySchemaValidator(validators[i]); + factory.FreeState(validators); + } + if (patternPropertiesValidators) { + for (SizeType i = 0; i < patternPropertiesValidatorCount; i++) + factory.DestroySchemaValidator(patternPropertiesValidators[i]); + factory.FreeState(patternPropertiesValidators); + } + if (patternPropertiesSchemas) + factory.FreeState(patternPropertiesSchemas); + if (propertyExist) + factory.FreeState(propertyExist); + } + + SchemaValidatorFactoryType& factory; + const SchemaType* schema; + const SchemaType* valueSchema; + const Ch* invalidKeyword; + void* hasher; // Only validator access + void* arrayElementHashCodes; // Only validator access this + ISchemaValidator** validators; + SizeType validatorCount; + ISchemaValidator** patternPropertiesValidators; + SizeType patternPropertiesValidatorCount; + const SchemaType** patternPropertiesSchemas; + SizeType patternPropertiesSchemaCount; + PatternValidatorType valuePatternValidatorType; + PatternValidatorType objectPatternValidatorType; + SizeType arrayElementIndex; + bool* propertyExist; + bool inArray; + bool valueUniqueness; + bool arrayUniqueness; +}; + +/////////////////////////////////////////////////////////////////////////////// +// Schema + +template +class Schema { +public: + typedef typename SchemaDocumentType::ValueType ValueType; + typedef typename SchemaDocumentType::AllocatorType AllocatorType; + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename EncodingType::Ch Ch; + typedef SchemaValidationContext Context; + typedef Schema SchemaType; + typedef GenericValue SValue; + friend class GenericSchemaDocument; + + Schema(SchemaDocumentType* schemaDocument, const PointerType& p, const ValueType& value, const ValueType& document, AllocatorType* allocator) : + allocator_(allocator), + enum_(), + enumCount_(), + not_(), + type_((1 << kTotalSchemaType) - 1), // typeless + validatorCount_(), + properties_(), + additionalPropertiesSchema_(), + patternProperties_(), + patternPropertyCount_(), + propertyCount_(), + minProperties_(), + maxProperties_(SizeType(~0)), + additionalProperties_(true), + hasDependencies_(), + hasRequired_(), + hasSchemaDependencies_(), + additionalItemsSchema_(), + itemsList_(), + itemsTuple_(), + itemsTupleCount_(), + minItems_(), + maxItems_(SizeType(~0)), + additionalItems_(true), + uniqueItems_(false), + pattern_(), + minLength_(0), + maxLength_(~SizeType(0)), + exclusiveMinimum_(false), + exclusiveMaximum_(false) + { + typedef typename SchemaDocumentType::ValueType ValueType; + typedef typename ValueType::ConstValueIterator ConstValueIterator; + typedef typename ValueType::ConstMemberIterator ConstMemberIterator; + + if (!value.IsObject()) + return; + + if (const ValueType* v = GetMember(value, GetTypeString())) { + type_ = 0; + if (v->IsString()) + AddType(*v); + else if (v->IsArray()) + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr) + AddType(*itr); + } + + if (const ValueType* v = GetMember(value, GetEnumString())) + if (v->IsArray() && v->Size() > 0) { + enum_ = static_cast(allocator_->Malloc(sizeof(uint64_t) * v->Size())); + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr) { + typedef Hasher > EnumHasherType; + char buffer[256 + 24]; + MemoryPoolAllocator<> hasherAllocator(buffer, sizeof(buffer)); + EnumHasherType h(&hasherAllocator, 256); + itr->Accept(h); + enum_[enumCount_++] = h.GetHashCode(); + } + } + + if (schemaDocument) { + AssignIfExist(allOf_, *schemaDocument, p, value, GetAllOfString(), document); + AssignIfExist(anyOf_, *schemaDocument, p, value, GetAnyOfString(), document); + AssignIfExist(oneOf_, *schemaDocument, p, value, GetOneOfString(), document); + } + + if (const ValueType* v = GetMember(value, GetNotString())) { + schemaDocument->CreateSchema(¬_, p.Append(GetNotString(), allocator_), *v, document); + notValidatorIndex_ = validatorCount_; + validatorCount_++; + } + + // Object + + const ValueType* properties = GetMember(value, GetPropertiesString()); + const ValueType* required = GetMember(value, GetRequiredString()); + const ValueType* dependencies = GetMember(value, GetDependenciesString()); + { + // Gather properties from properties/required/dependencies + SValue allProperties(kArrayType); + + if (properties && properties->IsObject()) + for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr) + AddUniqueElement(allProperties, itr->name); + + if (required && required->IsArray()) + for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr) + if (itr->IsString()) + AddUniqueElement(allProperties, *itr); + + if (dependencies && dependencies->IsObject()) + for (ConstMemberIterator itr = dependencies->MemberBegin(); itr != dependencies->MemberEnd(); ++itr) { + AddUniqueElement(allProperties, itr->name); + if (itr->value.IsArray()) + for (ConstValueIterator i = itr->value.Begin(); i != itr->value.End(); ++i) + if (i->IsString()) + AddUniqueElement(allProperties, *i); + } + + if (allProperties.Size() > 0) { + propertyCount_ = allProperties.Size(); + properties_ = static_cast(allocator_->Malloc(sizeof(Property) * propertyCount_)); + for (SizeType i = 0; i < propertyCount_; i++) { + new (&properties_[i]) Property(); + properties_[i].name = allProperties[i]; + properties_[i].schema = GetTypeless(); + } + } + } + + if (properties && properties->IsObject()) { + PointerType q = p.Append(GetPropertiesString(), allocator_); + for (ConstMemberIterator itr = properties->MemberBegin(); itr != properties->MemberEnd(); ++itr) { + SizeType index; + if (FindPropertyIndex(itr->name, &index)) + schemaDocument->CreateSchema(&properties_[index].schema, q.Append(itr->name, allocator_), itr->value, document); + } + } + + if (const ValueType* v = GetMember(value, GetPatternPropertiesString())) { + PointerType q = p.Append(GetPatternPropertiesString(), allocator_); + patternProperties_ = static_cast(allocator_->Malloc(sizeof(PatternProperty) * v->MemberCount())); + patternPropertyCount_ = 0; + + for (ConstMemberIterator itr = v->MemberBegin(); itr != v->MemberEnd(); ++itr) { + new (&patternProperties_[patternPropertyCount_]) PatternProperty(); + patternProperties_[patternPropertyCount_].pattern = CreatePattern(itr->name); + schemaDocument->CreateSchema(&patternProperties_[patternPropertyCount_].schema, q.Append(itr->name, allocator_), itr->value, document); + patternPropertyCount_++; + } + } + + if (required && required->IsArray()) + for (ConstValueIterator itr = required->Begin(); itr != required->End(); ++itr) + if (itr->IsString()) { + SizeType index; + if (FindPropertyIndex(*itr, &index)) { + properties_[index].required = true; + hasRequired_ = true; + } + } + + if (dependencies && dependencies->IsObject()) { + PointerType q = p.Append(GetDependenciesString(), allocator_); + hasDependencies_ = true; + for (ConstMemberIterator itr = dependencies->MemberBegin(); itr != dependencies->MemberEnd(); ++itr) { + SizeType sourceIndex; + if (FindPropertyIndex(itr->name, &sourceIndex)) { + if (itr->value.IsArray()) { + properties_[sourceIndex].dependencies = static_cast(allocator_->Malloc(sizeof(bool) * propertyCount_)); + std::memset(properties_[sourceIndex].dependencies, 0, sizeof(bool)* propertyCount_); + for (ConstValueIterator targetItr = itr->value.Begin(); targetItr != itr->value.End(); ++targetItr) { + SizeType targetIndex; + if (FindPropertyIndex(*targetItr, &targetIndex)) + properties_[sourceIndex].dependencies[targetIndex] = true; + } + } + else if (itr->value.IsObject()) { + hasSchemaDependencies_ = true; + schemaDocument->CreateSchema(&properties_[sourceIndex].dependenciesSchema, q.Append(itr->name, allocator_), itr->value, document); + properties_[sourceIndex].dependenciesValidatorIndex = validatorCount_; + validatorCount_++; + } + } + } + } + + if (const ValueType* v = GetMember(value, GetAdditionalPropertiesString())) { + if (v->IsBool()) + additionalProperties_ = v->GetBool(); + else if (v->IsObject()) + schemaDocument->CreateSchema(&additionalPropertiesSchema_, p.Append(GetAdditionalPropertiesString(), allocator_), *v, document); + } + + AssignIfExist(minProperties_, value, GetMinPropertiesString()); + AssignIfExist(maxProperties_, value, GetMaxPropertiesString()); + + // Array + if (const ValueType* v = GetMember(value, GetItemsString())) { + PointerType q = p.Append(GetItemsString(), allocator_); + if (v->IsObject()) // List validation + schemaDocument->CreateSchema(&itemsList_, q, *v, document); + else if (v->IsArray()) { // Tuple validation + itemsTuple_ = static_cast(allocator_->Malloc(sizeof(const Schema*) * v->Size())); + SizeType index = 0; + for (ConstValueIterator itr = v->Begin(); itr != v->End(); ++itr, index++) + schemaDocument->CreateSchema(&itemsTuple_[itemsTupleCount_++], q.Append(index, allocator_), *itr, document); + } + } + + AssignIfExist(minItems_, value, GetMinItemsString()); + AssignIfExist(maxItems_, value, GetMaxItemsString()); + + if (const ValueType* v = GetMember(value, GetAdditionalItemsString())) { + if (v->IsBool()) + additionalItems_ = v->GetBool(); + else if (v->IsObject()) + schemaDocument->CreateSchema(&additionalItemsSchema_, p.Append(GetAdditionalItemsString(), allocator_), *v, document); + } + + AssignIfExist(uniqueItems_, value, GetUniqueItemsString()); + + // String + AssignIfExist(minLength_, value, GetMinLengthString()); + AssignIfExist(maxLength_, value, GetMaxLengthString()); + + if (const ValueType* v = GetMember(value, GetPatternString())) + pattern_ = CreatePattern(*v); + + // Number + if (const ValueType* v = GetMember(value, GetMinimumString())) + if (v->IsNumber()) + minimum_.CopyFrom(*v, *allocator_); + + if (const ValueType* v = GetMember(value, GetMaximumString())) + if (v->IsNumber()) + maximum_.CopyFrom(*v, *allocator_); + + AssignIfExist(exclusiveMinimum_, value, GetExclusiveMinimumString()); + AssignIfExist(exclusiveMaximum_, value, GetExclusiveMaximumString()); + + if (const ValueType* v = GetMember(value, GetMultipleOfString())) + if (v->IsNumber() && v->GetDouble() > 0.0) + multipleOf_.CopyFrom(*v, *allocator_); + } + + ~Schema() { + if (allocator_) { + allocator_->Free(enum_); + } + if (properties_) { + for (SizeType i = 0; i < propertyCount_; i++) + properties_[i].~Property(); + AllocatorType::Free(properties_); + } + if (patternProperties_) { + for (SizeType i = 0; i < patternPropertyCount_; i++) + patternProperties_[i].~PatternProperty(); + AllocatorType::Free(patternProperties_); + } + AllocatorType::Free(itemsTuple_); +#if RAPIDJSON_SCHEMA_HAS_REGEX + if (pattern_) { + pattern_->~RegexType(); + allocator_->Free(pattern_); + } +#endif + } + + bool BeginValue(Context& context) const { + if (context.inArray) { + if (uniqueItems_) + context.valueUniqueness = true; + + if (itemsList_) + context.valueSchema = itemsList_; + else if (itemsTuple_) { + if (context.arrayElementIndex < itemsTupleCount_) + context.valueSchema = itemsTuple_[context.arrayElementIndex]; + else if (additionalItemsSchema_) + context.valueSchema = additionalItemsSchema_; + else if (additionalItems_) + context.valueSchema = GetTypeless(); + else + RAPIDJSON_INVALID_KEYWORD_RETURN(GetItemsString()); + } + else + context.valueSchema = GetTypeless(); + + context.arrayElementIndex++; + } + return true; + } + + RAPIDJSON_FORCEINLINE bool EndValue(Context& context) const { + if (context.patternPropertiesValidatorCount > 0) { + bool otherValid = false; + SizeType count = context.patternPropertiesValidatorCount; + if (context.objectPatternValidatorType != Context::kPatternValidatorOnly) + otherValid = context.patternPropertiesValidators[--count]->IsValid(); + + bool patternValid = true; + for (SizeType i = 0; i < count; i++) + if (!context.patternPropertiesValidators[i]->IsValid()) { + patternValid = false; + break; + } + + if (context.objectPatternValidatorType == Context::kPatternValidatorOnly) { + if (!patternValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString()); + } + else if (context.objectPatternValidatorType == Context::kPatternValidatorWithProperty) { + if (!patternValid || !otherValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString()); + } + else if (!patternValid && !otherValid) // kPatternValidatorWithAdditionalProperty) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternPropertiesString()); + } + + if (enum_) { + const uint64_t h = context.factory.GetHashCode(context.hasher); + for (SizeType i = 0; i < enumCount_; i++) + if (enum_[i] == h) + goto foundEnum; + RAPIDJSON_INVALID_KEYWORD_RETURN(GetEnumString()); + foundEnum:; + } + + if (allOf_.schemas) + for (SizeType i = allOf_.begin; i < allOf_.begin + allOf_.count; i++) + if (!context.validators[i]->IsValid()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetAllOfString()); + + if (anyOf_.schemas) { + for (SizeType i = anyOf_.begin; i < anyOf_.begin + anyOf_.count; i++) + if (context.validators[i]->IsValid()) + goto foundAny; + RAPIDJSON_INVALID_KEYWORD_RETURN(GetAnyOfString()); + foundAny:; + } + + if (oneOf_.schemas) { + bool oneValid = false; + for (SizeType i = oneOf_.begin; i < oneOf_.begin + oneOf_.count; i++) + if (context.validators[i]->IsValid()) { + if (oneValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetOneOfString()); + else + oneValid = true; + } + if (!oneValid) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetOneOfString()); + } + + if (not_ && context.validators[notValidatorIndex_]->IsValid()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetNotString()); + + return true; + } + + bool Null(Context& context) const { + if (!(type_ & (1 << kNullSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + return CreateParallelValidator(context); + } + + bool Bool(Context& context, bool) const { + if (!(type_ & (1 << kBooleanSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + return CreateParallelValidator(context); + } + + bool Int(Context& context, int i) const { + if (!CheckInt(context, i)) + return false; + return CreateParallelValidator(context); + } + + bool Uint(Context& context, unsigned u) const { + if (!CheckUint(context, u)) + return false; + return CreateParallelValidator(context); + } + + bool Int64(Context& context, int64_t i) const { + if (!CheckInt(context, i)) + return false; + return CreateParallelValidator(context); + } + + bool Uint64(Context& context, uint64_t u) const { + if (!CheckUint(context, u)) + return false; + return CreateParallelValidator(context); + } + + bool Double(Context& context, double d) const { + if (!(type_ & (1 << kNumberSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (!minimum_.IsNull() && !CheckDoubleMinimum(context, d)) + return false; + + if (!maximum_.IsNull() && !CheckDoubleMaximum(context, d)) + return false; + + if (!multipleOf_.IsNull() && !CheckDoubleMultipleOf(context, d)) + return false; + + return CreateParallelValidator(context); + } + + bool String(Context& context, const Ch* str, SizeType length, bool) const { + if (!(type_ & (1 << kStringSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (minLength_ != 0 || maxLength_ != SizeType(~0)) { + SizeType count; + if (internal::CountStringCodePoint(str, length, &count)) { + if (count < minLength_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinLengthString()); + if (count > maxLength_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxLengthString()); + } + } + + if (pattern_ && !IsPatternMatch(pattern_, str, length)) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetPatternString()); + + return CreateParallelValidator(context); + } + + bool StartObject(Context& context) const { + if (!(type_ & (1 << kObjectSchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (hasDependencies_ || hasRequired_) { + context.propertyExist = static_cast(context.factory.MallocState(sizeof(bool) * propertyCount_)); + std::memset(context.propertyExist, 0, sizeof(bool) * propertyCount_); + } + + if (patternProperties_) { // pre-allocate schema array + SizeType count = patternPropertyCount_ + 1; // extra for valuePatternValidatorType + context.patternPropertiesSchemas = static_cast(context.factory.MallocState(sizeof(const SchemaType*) * count)); + context.patternPropertiesSchemaCount = 0; + std::memset(context.patternPropertiesSchemas, 0, sizeof(SchemaType*) * count); + } + + return CreateParallelValidator(context); + } + + bool Key(Context& context, const Ch* str, SizeType len, bool) const { + if (patternProperties_) { + context.patternPropertiesSchemaCount = 0; + for (SizeType i = 0; i < patternPropertyCount_; i++) + if (patternProperties_[i].pattern && IsPatternMatch(patternProperties_[i].pattern, str, len)) + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = patternProperties_[i].schema; + } + + SizeType index; + if (FindPropertyIndex(ValueType(str, len).Move(), &index)) { + if (context.patternPropertiesSchemaCount > 0) { + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = properties_[index].schema; + context.valueSchema = GetTypeless(); + context.valuePatternValidatorType = Context::kPatternValidatorWithProperty; + } + else + context.valueSchema = properties_[index].schema; + + if (context.propertyExist) + context.propertyExist[index] = true; + + return true; + } + + if (additionalPropertiesSchema_) { + if (additionalPropertiesSchema_ && context.patternPropertiesSchemaCount > 0) { + context.patternPropertiesSchemas[context.patternPropertiesSchemaCount++] = additionalPropertiesSchema_; + context.valueSchema = GetTypeless(); + context.valuePatternValidatorType = Context::kPatternValidatorWithAdditionalProperty; + } + else + context.valueSchema = additionalPropertiesSchema_; + return true; + } + else if (additionalProperties_) { + context.valueSchema = GetTypeless(); + return true; + } + + if (context.patternPropertiesSchemaCount == 0) // patternProperties are not additional properties + RAPIDJSON_INVALID_KEYWORD_RETURN(GetAdditionalPropertiesString()); + + return true; + } + + bool EndObject(Context& context, SizeType memberCount) const { + if (hasRequired_) + for (SizeType index = 0; index < propertyCount_; index++) + if (properties_[index].required) + if (!context.propertyExist[index]) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetRequiredString()); + + if (memberCount < minProperties_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinPropertiesString()); + + if (memberCount > maxProperties_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxPropertiesString()); + + if (hasDependencies_) { + for (SizeType sourceIndex = 0; sourceIndex < propertyCount_; sourceIndex++) + if (context.propertyExist[sourceIndex]) { + if (properties_[sourceIndex].dependencies) { + for (SizeType targetIndex = 0; targetIndex < propertyCount_; targetIndex++) + if (properties_[sourceIndex].dependencies[targetIndex] && !context.propertyExist[targetIndex]) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetDependenciesString()); + } + else if (properties_[sourceIndex].dependenciesSchema) + if (!context.validators[properties_[sourceIndex].dependenciesValidatorIndex]->IsValid()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetDependenciesString()); + } + } + + return true; + } + + bool StartArray(Context& context) const { + if (!(type_ & (1 << kArraySchemaType))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + context.arrayElementIndex = 0; + context.inArray = true; + + return CreateParallelValidator(context); + } + + bool EndArray(Context& context, SizeType elementCount) const { + context.inArray = false; + + if (elementCount < minItems_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinItemsString()); + + if (elementCount > maxItems_) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaxItemsString()); + + return true; + } + + // Generate functions for string literal according to Ch +#define RAPIDJSON_STRING_(name, ...) \ + static const ValueType& Get##name##String() {\ + static const Ch s[] = { __VA_ARGS__, '\0' };\ + static const ValueType v(s, sizeof(s) / sizeof(Ch) - 1);\ + return v;\ + } + + RAPIDJSON_STRING_(Null, 'n', 'u', 'l', 'l') + RAPIDJSON_STRING_(Boolean, 'b', 'o', 'o', 'l', 'e', 'a', 'n') + RAPIDJSON_STRING_(Object, 'o', 'b', 'j', 'e', 'c', 't') + RAPIDJSON_STRING_(Array, 'a', 'r', 'r', 'a', 'y') + RAPIDJSON_STRING_(String, 's', 't', 'r', 'i', 'n', 'g') + RAPIDJSON_STRING_(Number, 'n', 'u', 'm', 'b', 'e', 'r') + RAPIDJSON_STRING_(Integer, 'i', 'n', 't', 'e', 'g', 'e', 'r') + RAPIDJSON_STRING_(Type, 't', 'y', 'p', 'e') + RAPIDJSON_STRING_(Enum, 'e', 'n', 'u', 'm') + RAPIDJSON_STRING_(AllOf, 'a', 'l', 'l', 'O', 'f') + RAPIDJSON_STRING_(AnyOf, 'a', 'n', 'y', 'O', 'f') + RAPIDJSON_STRING_(OneOf, 'o', 'n', 'e', 'O', 'f') + RAPIDJSON_STRING_(Not, 'n', 'o', 't') + RAPIDJSON_STRING_(Properties, 'p', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(Required, 'r', 'e', 'q', 'u', 'i', 'r', 'e', 'd') + RAPIDJSON_STRING_(Dependencies, 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'i', 'e', 's') + RAPIDJSON_STRING_(PatternProperties, 'p', 'a', 't', 't', 'e', 'r', 'n', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(AdditionalProperties, 'a', 'd', 'd', 'i', 't', 'i', 'o', 'n', 'a', 'l', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(MinProperties, 'm', 'i', 'n', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(MaxProperties, 'm', 'a', 'x', 'P', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's') + RAPIDJSON_STRING_(Items, 'i', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MinItems, 'm', 'i', 'n', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MaxItems, 'm', 'a', 'x', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(AdditionalItems, 'a', 'd', 'd', 'i', 't', 'i', 'o', 'n', 'a', 'l', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(UniqueItems, 'u', 'n', 'i', 'q', 'u', 'e', 'I', 't', 'e', 'm', 's') + RAPIDJSON_STRING_(MinLength, 'm', 'i', 'n', 'L', 'e', 'n', 'g', 't', 'h') + RAPIDJSON_STRING_(MaxLength, 'm', 'a', 'x', 'L', 'e', 'n', 'g', 't', 'h') + RAPIDJSON_STRING_(Pattern, 'p', 'a', 't', 't', 'e', 'r', 'n') + RAPIDJSON_STRING_(Minimum, 'm', 'i', 'n', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(Maximum, 'm', 'a', 'x', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(ExclusiveMinimum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'i', 'n', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(ExclusiveMaximum, 'e', 'x', 'c', 'l', 'u', 's', 'i', 'v', 'e', 'M', 'a', 'x', 'i', 'm', 'u', 'm') + RAPIDJSON_STRING_(MultipleOf, 'm', 'u', 'l', 't', 'i', 'p', 'l', 'e', 'O', 'f') + +#undef RAPIDJSON_STRING_ + +private: + enum SchemaValueType { + kNullSchemaType, + kBooleanSchemaType, + kObjectSchemaType, + kArraySchemaType, + kStringSchemaType, + kNumberSchemaType, + kIntegerSchemaType, + kTotalSchemaType + }; + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX + typedef internal::GenericRegex RegexType; +#elif RAPIDJSON_SCHEMA_USE_STDREGEX + typedef std::basic_regex RegexType; +#else + typedef char RegexType; +#endif + + struct SchemaArray { + SchemaArray() : schemas(), count() {} + ~SchemaArray() { AllocatorType::Free(schemas); } + const SchemaType** schemas; + SizeType begin; // begin index of context.validators + SizeType count; + }; + + static const SchemaType* GetTypeless() { + static SchemaType typeless(0, PointerType(), ValueType(kObjectType).Move(), ValueType(kObjectType).Move(), 0); + return &typeless; + } + + template + void AddUniqueElement(V1& a, const V2& v) { + for (typename V1::ConstValueIterator itr = a.Begin(); itr != a.End(); ++itr) + if (*itr == v) + return; + V1 c(v, *allocator_); + a.PushBack(c, *allocator_); + } + + static const ValueType* GetMember(const ValueType& value, const ValueType& name) { + typename ValueType::ConstMemberIterator itr = value.FindMember(name); + return itr != value.MemberEnd() ? &(itr->value) : 0; + } + + static void AssignIfExist(bool& out, const ValueType& value, const ValueType& name) { + if (const ValueType* v = GetMember(value, name)) + if (v->IsBool()) + out = v->GetBool(); + } + + static void AssignIfExist(SizeType& out, const ValueType& value, const ValueType& name) { + if (const ValueType* v = GetMember(value, name)) + if (v->IsUint64() && v->GetUint64() <= SizeType(~0)) + out = static_cast(v->GetUint64()); + } + + void AssignIfExist(SchemaArray& out, SchemaDocumentType& schemaDocument, const PointerType& p, const ValueType& value, const ValueType& name, const ValueType& document) { + if (const ValueType* v = GetMember(value, name)) { + if (v->IsArray() && v->Size() > 0) { + PointerType q = p.Append(name, allocator_); + out.count = v->Size(); + out.schemas = static_cast(allocator_->Malloc(out.count * sizeof(const Schema*))); + memset(out.schemas, 0, sizeof(Schema*)* out.count); + for (SizeType i = 0; i < out.count; i++) + schemaDocument.CreateSchema(&out.schemas[i], q.Append(i, allocator_), (*v)[i], document); + out.begin = validatorCount_; + validatorCount_ += out.count; + } + } + } + +#if RAPIDJSON_SCHEMA_USE_INTERNALREGEX + template + RegexType* CreatePattern(const ValueType& value) { + if (value.IsString()) { + RegexType* r = new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString()); + if (!r->IsValid()) { + r->~RegexType(); + AllocatorType::Free(r); + r = 0; + } + return r; + } + return 0; + } + + static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType) { + return pattern->Search(str); + } +#elif RAPIDJSON_SCHEMA_USE_STDREGEX + template + RegexType* CreatePattern(const ValueType& value) { + if (value.IsString()) + try { + return new (allocator_->Malloc(sizeof(RegexType))) RegexType(value.GetString(), std::size_t(value.GetStringLength()), std::regex_constants::ECMAScript); + } + catch (const std::regex_error&) { + } + return 0; + } + + static bool IsPatternMatch(const RegexType* pattern, const Ch *str, SizeType length) { + std::match_results r; + return std::regex_search(str, str + length, r, *pattern); + } +#else + template + RegexType* CreatePattern(const ValueType&) { return 0; } + + static bool IsPatternMatch(const RegexType*, const Ch *, SizeType) { return true; } +#endif // RAPIDJSON_SCHEMA_USE_STDREGEX + + void AddType(const ValueType& type) { + if (type == GetNullString() ) type_ |= 1 << kNullSchemaType; + else if (type == GetBooleanString()) type_ |= 1 << kBooleanSchemaType; + else if (type == GetObjectString() ) type_ |= 1 << kObjectSchemaType; + else if (type == GetArrayString() ) type_ |= 1 << kArraySchemaType; + else if (type == GetStringString() ) type_ |= 1 << kStringSchemaType; + else if (type == GetIntegerString()) type_ |= 1 << kIntegerSchemaType; + else if (type == GetNumberString() ) type_ |= (1 << kNumberSchemaType) | (1 << kIntegerSchemaType); + } + + bool CreateParallelValidator(Context& context) const { + if (enum_ || context.arrayUniqueness) + context.hasher = context.factory.CreateHasher(); + + if (validatorCount_) { + RAPIDJSON_ASSERT(context.validators == 0); + context.validators = static_cast(context.factory.MallocState(sizeof(ISchemaValidator*) * validatorCount_)); + context.validatorCount = validatorCount_; + + if (allOf_.schemas) + CreateSchemaValidators(context, allOf_); + + if (anyOf_.schemas) + CreateSchemaValidators(context, anyOf_); + + if (oneOf_.schemas) + CreateSchemaValidators(context, oneOf_); + + if (not_) + context.validators[notValidatorIndex_] = context.factory.CreateSchemaValidator(*not_); + + if (hasSchemaDependencies_) { + for (SizeType i = 0; i < propertyCount_; i++) + if (properties_[i].dependenciesSchema) + context.validators[properties_[i].dependenciesValidatorIndex] = context.factory.CreateSchemaValidator(*properties_[i].dependenciesSchema); + } + } + + return true; + } + + void CreateSchemaValidators(Context& context, const SchemaArray& schemas) const { + for (SizeType i = 0; i < schemas.count; i++) + context.validators[schemas.begin + i] = context.factory.CreateSchemaValidator(*schemas.schemas[i]); + } + + // O(n) + bool FindPropertyIndex(const ValueType& name, SizeType* outIndex) const { + SizeType len = name.GetStringLength(); + const Ch* str = name.GetString(); + for (SizeType index = 0; index < propertyCount_; index++) + if (properties_[index].name.GetStringLength() == len && + (std::memcmp(properties_[index].name.GetString(), str, sizeof(Ch) * len) == 0)) + { + *outIndex = index; + return true; + } + return false; + } + + bool CheckInt(Context& context, int64_t i) const { + if (!(type_ & ((1 << kIntegerSchemaType) | (1 << kNumberSchemaType)))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (!minimum_.IsNull()) { + if (minimum_.IsInt64()) { + if (exclusiveMinimum_ ? i <= minimum_.GetInt64() : i < minimum_.GetInt64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); + } + else if (minimum_.IsUint64()) { + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); // i <= max(int64_t) < minimum.GetUint64() + } + else if (!CheckDoubleMinimum(context, static_cast(i))) + return false; + } + + if (!maximum_.IsNull()) { + if (maximum_.IsInt64()) { + if (exclusiveMaximum_ ? i >= maximum_.GetInt64() : i > maximum_.GetInt64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); + } + else if (maximum_.IsUint64()) + /* do nothing */; // i <= max(int64_t) < maximum_.GetUint64() + else if (!CheckDoubleMaximum(context, static_cast(i))) + return false; + } + + if (!multipleOf_.IsNull()) { + if (multipleOf_.IsUint64()) { + if (static_cast(i >= 0 ? i : -i) % multipleOf_.GetUint64() != 0) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString()); + } + else if (!CheckDoubleMultipleOf(context, static_cast(i))) + return false; + } + + return true; + } + + bool CheckUint(Context& context, uint64_t i) const { + if (!(type_ & ((1 << kIntegerSchemaType) | (1 << kNumberSchemaType)))) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetTypeString()); + + if (!minimum_.IsNull()) { + if (minimum_.IsUint64()) { + if (exclusiveMinimum_ ? i <= minimum_.GetUint64() : i < minimum_.GetUint64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); + } + else if (minimum_.IsInt64()) + /* do nothing */; // i >= 0 > minimum.Getint64() + else if (!CheckDoubleMinimum(context, static_cast(i))) + return false; + } + + if (!maximum_.IsNull()) { + if (maximum_.IsUint64()) { + if (exclusiveMaximum_ ? i >= maximum_.GetUint64() : i > maximum_.GetUint64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); + } + else if (maximum_.IsInt64()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); // i >= 0 > maximum_ + else if (!CheckDoubleMaximum(context, static_cast(i))) + return false; + } + + if (!multipleOf_.IsNull()) { + if (multipleOf_.IsUint64()) { + if (i % multipleOf_.GetUint64() != 0) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString()); + } + else if (!CheckDoubleMultipleOf(context, static_cast(i))) + return false; + } + + return true; + } + + bool CheckDoubleMinimum(Context& context, double d) const { + if (exclusiveMinimum_ ? d <= minimum_.GetDouble() : d < minimum_.GetDouble()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMinimumString()); + return true; + } + + bool CheckDoubleMaximum(Context& context, double d) const { + if (exclusiveMaximum_ ? d >= maximum_.GetDouble() : d > maximum_.GetDouble()) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMaximumString()); + return true; + } + + bool CheckDoubleMultipleOf(Context& context, double d) const { + double a = std::abs(d), b = std::abs(multipleOf_.GetDouble()); + double q = std::floor(a / b); + double r = a - q * b; + if (r > 0.0) + RAPIDJSON_INVALID_KEYWORD_RETURN(GetMultipleOfString()); + return true; + } + + struct Property { + Property() : schema(), dependenciesSchema(), dependenciesValidatorIndex(), dependencies(), required(false) {} + ~Property() { AllocatorType::Free(dependencies); } + SValue name; + const SchemaType* schema; + const SchemaType* dependenciesSchema; + SizeType dependenciesValidatorIndex; + bool* dependencies; + bool required; + }; + + struct PatternProperty { + PatternProperty() : schema(), pattern() {} + ~PatternProperty() { + if (pattern) { + pattern->~RegexType(); + AllocatorType::Free(pattern); + } + } + const SchemaType* schema; + RegexType* pattern; + }; + + AllocatorType* allocator_; + uint64_t* enum_; + SizeType enumCount_; + SchemaArray allOf_; + SchemaArray anyOf_; + SchemaArray oneOf_; + const SchemaType* not_; + unsigned type_; // bitmask of kSchemaType + SizeType validatorCount_; + SizeType notValidatorIndex_; + + Property* properties_; + const SchemaType* additionalPropertiesSchema_; + PatternProperty* patternProperties_; + SizeType patternPropertyCount_; + SizeType propertyCount_; + SizeType minProperties_; + SizeType maxProperties_; + bool additionalProperties_; + bool hasDependencies_; + bool hasRequired_; + bool hasSchemaDependencies_; + + const SchemaType* additionalItemsSchema_; + const SchemaType* itemsList_; + const SchemaType** itemsTuple_; + SizeType itemsTupleCount_; + SizeType minItems_; + SizeType maxItems_; + bool additionalItems_; + bool uniqueItems_; + + RegexType* pattern_; + SizeType minLength_; + SizeType maxLength_; + + SValue minimum_; + SValue maximum_; + SValue multipleOf_; + bool exclusiveMinimum_; + bool exclusiveMaximum_; +}; + +template +struct TokenHelper { + RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) { + *documentStack.template Push() = '/'; + char buffer[21]; + size_t length = static_cast((sizeof(SizeType) == 4 ? u32toa(index, buffer) : u64toa(index, buffer)) - buffer); + for (size_t i = 0; i < length; i++) + *documentStack.template Push() = buffer[i]; + } +}; + +// Partial specialized version for char to prevent buffer copying. +template +struct TokenHelper { + RAPIDJSON_FORCEINLINE static void AppendIndexToken(Stack& documentStack, SizeType index) { + if (sizeof(SizeType) == 4) { + char *buffer = documentStack.template Push(1 + 10); // '/' + uint + *buffer++ = '/'; + const char* end = internal::u32toa(index, buffer); + documentStack.template Pop(static_cast(10 - (end - buffer))); + } + else { + char *buffer = documentStack.template Push(1 + 20); // '/' + uint64 + *buffer++ = '/'; + const char* end = internal::u64toa(index, buffer); + documentStack.template Pop(static_cast(20 - (end - buffer))); + } + } +}; + +} // namespace internal + +/////////////////////////////////////////////////////////////////////////////// +// IGenericRemoteSchemaDocumentProvider + +template +class IGenericRemoteSchemaDocumentProvider { +public: + typedef typename SchemaDocumentType::Ch Ch; + + virtual ~IGenericRemoteSchemaDocumentProvider() {} + virtual const SchemaDocumentType* GetRemoteDocument(const Ch* uri, SizeType length) = 0; +}; + +/////////////////////////////////////////////////////////////////////////////// +// GenericSchemaDocument + +//! JSON schema document. +/*! + A JSON schema document is a compiled version of a JSON schema. + It is basically a tree of internal::Schema. + + \note This is an immutable class (i.e. its instance cannot be modified after construction). + \tparam ValueT Type of JSON value (e.g. \c Value ), which also determine the encoding. + \tparam Allocator Allocator type for allocating memory of this document. +*/ +template +class GenericSchemaDocument { +public: + typedef ValueT ValueType; + typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProviderType; + typedef Allocator AllocatorType; + typedef typename ValueType::EncodingType EncodingType; + typedef typename EncodingType::Ch Ch; + typedef internal::Schema SchemaType; + typedef GenericPointer PointerType; + friend class internal::Schema; + template + friend class GenericSchemaValidator; + + //! Constructor. + /*! + Compile a JSON document into schema document. + + \param document A JSON document as source. + \param remoteProvider An optional remote schema document provider for resolving remote reference. Can be null. + \param allocator An optional allocator instance for allocating memory. Can be null. + */ + explicit GenericSchemaDocument(const ValueType& document, IRemoteSchemaDocumentProviderType* remoteProvider = 0, Allocator* allocator = 0) : + remoteProvider_(remoteProvider), + allocator_(allocator), + ownAllocator_(), + root_(), + schemaMap_(allocator, kInitialSchemaMapSize), + schemaRef_(allocator, kInitialSchemaRefSize) + { + if (!allocator_) + ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); + + // Generate root schema, it will call CreateSchema() to create sub-schemas, + // And call AddRefSchema() if there are $ref. + CreateSchemaRecursive(&root_, PointerType(), document, document); + + // Resolve $ref + while (!schemaRef_.Empty()) { + SchemaRefEntry* refEntry = schemaRef_.template Pop(1); + if (const SchemaType* s = GetSchema(refEntry->target)) { + if (refEntry->schema) + *refEntry->schema = s; + + // Create entry in map if not exist + if (!GetSchema(refEntry->source)) { + new (schemaMap_.template Push()) SchemaEntry(refEntry->source, const_cast(s), false, allocator_); + } + } + refEntry->~SchemaRefEntry(); + } + + RAPIDJSON_ASSERT(root_ != 0); + + schemaRef_.ShrinkToFit(); // Deallocate all memory for ref + } + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + //! Move constructor in C++11 + GenericSchemaDocument(GenericSchemaDocument&& rhs) RAPIDJSON_NOEXCEPT : + remoteProvider_(rhs.remoteProvider_), + allocator_(rhs.allocator_), + ownAllocator_(rhs.ownAllocator_), + root_(rhs.root_), + schemaMap_(std::move(rhs.schemaMap_)), + schemaRef_(std::move(rhs.schemaRef_)) + { + rhs.remoteProvider_ = 0; + rhs.allocator_ = 0; + rhs.ownAllocator_ = 0; + } +#endif + + //! Destructor + ~GenericSchemaDocument() { + while (!schemaMap_.Empty()) + schemaMap_.template Pop(1)->~SchemaEntry(); + + RAPIDJSON_DELETE(ownAllocator_); + } + + //! Get the root schema. + const SchemaType& GetRoot() const { return *root_; } + +private: + //! Prohibit copying + GenericSchemaDocument(const GenericSchemaDocument&); + //! Prohibit assignment + GenericSchemaDocument& operator=(const GenericSchemaDocument&); + + struct SchemaRefEntry { + SchemaRefEntry(const PointerType& s, const PointerType& t, const SchemaType** outSchema, Allocator *allocator) : source(s, allocator), target(t, allocator), schema(outSchema) {} + PointerType source; + PointerType target; + const SchemaType** schema; + }; + + struct SchemaEntry { + SchemaEntry(const PointerType& p, SchemaType* s, bool o, Allocator* allocator) : pointer(p, allocator), schema(s), owned(o) {} + ~SchemaEntry() { + if (owned) { + schema->~SchemaType(); + Allocator::Free(schema); + } + } + PointerType pointer; + SchemaType* schema; + bool owned; + }; + + void CreateSchemaRecursive(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) { + if (schema) + *schema = SchemaType::GetTypeless(); + + if (v.GetType() == kObjectType) { + const SchemaType* s = GetSchema(pointer); + if (!s) + CreateSchema(schema, pointer, v, document); + + for (typename ValueType::ConstMemberIterator itr = v.MemberBegin(); itr != v.MemberEnd(); ++itr) + CreateSchemaRecursive(0, pointer.Append(itr->name, allocator_), itr->value, document); + } + else if (v.GetType() == kArrayType) + for (SizeType i = 0; i < v.Size(); i++) + CreateSchemaRecursive(0, pointer.Append(i, allocator_), v[i], document); + } + + void CreateSchema(const SchemaType** schema, const PointerType& pointer, const ValueType& v, const ValueType& document) { + RAPIDJSON_ASSERT(pointer.IsValid()); + if (v.IsObject()) { + if (!HandleRefSchema(pointer, schema, v, document)) { + SchemaType* s = new (allocator_->Malloc(sizeof(SchemaType))) SchemaType(this, pointer, v, document, allocator_); + new (schemaMap_.template Push()) SchemaEntry(pointer, s, true, allocator_); + if (schema) + *schema = s; + } + } + } + + bool HandleRefSchema(const PointerType& source, const SchemaType** schema, const ValueType& v, const ValueType& document) { + static const Ch kRefString[] = { '$', 'r', 'e', 'f', '\0' }; + static const ValueType kRefValue(kRefString, 4); + + typename ValueType::ConstMemberIterator itr = v.FindMember(kRefValue); + if (itr == v.MemberEnd()) + return false; + + if (itr->value.IsString()) { + SizeType len = itr->value.GetStringLength(); + if (len > 0) { + const Ch* s = itr->value.GetString(); + SizeType i = 0; + while (i < len && s[i] != '#') // Find the first # + i++; + + if (i > 0) { // Remote reference, resolve immediately + if (remoteProvider_) { + if (const GenericSchemaDocument* remoteDocument = remoteProvider_->GetRemoteDocument(s, i - 1)) { + PointerType pointer(&s[i], len - i, allocator_); + if (pointer.IsValid()) { + if (const SchemaType* sc = remoteDocument->GetSchema(pointer)) { + if (schema) + *schema = sc; + return true; + } + } + } + } + } + else if (s[i] == '#') { // Local reference, defer resolution + PointerType pointer(&s[i], len - i, allocator_); + if (pointer.IsValid()) { + if (const ValueType* nv = pointer.Get(document)) + if (HandleRefSchema(source, schema, *nv, document)) + return true; + + new (schemaRef_.template Push()) SchemaRefEntry(source, pointer, schema, allocator_); + return true; + } + } + } + } + return false; + } + + const SchemaType* GetSchema(const PointerType& pointer) const { + for (const SchemaEntry* target = schemaMap_.template Bottom(); target != schemaMap_.template End(); ++target) + if (pointer == target->pointer) + return target->schema; + return 0; + } + + PointerType GetPointer(const SchemaType* schema) const { + for (const SchemaEntry* target = schemaMap_.template Bottom(); target != schemaMap_.template End(); ++target) + if (schema == target->schema) + return target->pointer; + return PointerType(); + } + + static const size_t kInitialSchemaMapSize = 64; + static const size_t kInitialSchemaRefSize = 64; + + IRemoteSchemaDocumentProviderType* remoteProvider_; + Allocator *allocator_; + Allocator *ownAllocator_; + const SchemaType* root_; //!< Root schema. + internal::Stack schemaMap_; // Stores created Pointer -> Schemas + internal::Stack schemaRef_; // Stores Pointer from $ref and schema which holds the $ref +}; + +//! GenericSchemaDocument using Value type. +typedef GenericSchemaDocument SchemaDocument; +//! IGenericRemoteSchemaDocumentProvider using SchemaDocument. +typedef IGenericRemoteSchemaDocumentProvider IRemoteSchemaDocumentProvider; + +/////////////////////////////////////////////////////////////////////////////// +// GenericSchemaValidator + +//! JSON Schema Validator. +/*! + A SAX style JSON schema validator. + It uses a \c GenericSchemaDocument to validate SAX events. + It delegates the incoming SAX events to an output handler. + The default output handler does nothing. + It can be reused multiple times by calling \c Reset(). + + \tparam SchemaDocumentType Type of schema document. + \tparam OutputHandler Type of output handler. Default handler does nothing. + \tparam StateAllocator Allocator for storing the internal validation states. +*/ +template < + typename SchemaDocumentType, + typename OutputHandler = BaseReaderHandler, + typename StateAllocator = CrtAllocator> +class GenericSchemaValidator : + public internal::ISchemaStateFactory, + public internal::ISchemaValidator +{ +public: + typedef typename SchemaDocumentType::SchemaType SchemaType; + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename SchemaType::EncodingType EncodingType; + typedef typename EncodingType::Ch Ch; + + //! Constructor without output handler. + /*! + \param schemaDocument The schema document to conform to. + \param allocator Optional allocator for storing internal validation states. + \param schemaStackCapacity Optional initial capacity of schema path stack. + \param documentStackCapacity Optional initial capacity of document path stack. + */ + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(schemaDocument.GetRoot()), + outputHandler_(GetNullHandler()), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + valid_(true) +#if RAPIDJSON_SCHEMA_VERBOSE + , depth_(0) +#endif + { + } + + //! Constructor with output handler. + /*! + \param schemaDocument The schema document to conform to. + \param allocator Optional allocator for storing internal validation states. + \param schemaStackCapacity Optional initial capacity of schema path stack. + \param documentStackCapacity Optional initial capacity of document path stack. + */ + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + OutputHandler& outputHandler, + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(schemaDocument.GetRoot()), + outputHandler_(outputHandler), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + valid_(true) +#if RAPIDJSON_SCHEMA_VERBOSE + , depth_(0) +#endif + { + } + + //! Destructor. + ~GenericSchemaValidator() { + Reset(); + RAPIDJSON_DELETE(ownStateAllocator_); + } + + //! Reset the internal states. + void Reset() { + while (!schemaStack_.Empty()) + PopSchema(); + documentStack_.Clear(); + valid_ = true; + } + + //! Checks whether the current state is valid. + // Implementation of ISchemaValidator + virtual bool IsValid() const { return valid_; } + + //! Gets the JSON pointer pointed to the invalid schema. + PointerType GetInvalidSchemaPointer() const { + return schemaStack_.Empty() ? PointerType() : schemaDocument_->GetPointer(&CurrentSchema()); + } + + //! Gets the keyword of invalid schema. + const Ch* GetInvalidSchemaKeyword() const { + return schemaStack_.Empty() ? 0 : CurrentContext().invalidKeyword; + } + + //! Gets the JSON pointer pointed to the invalid value. + PointerType GetInvalidDocumentPointer() const { + return documentStack_.Empty() ? PointerType() : PointerType(documentStack_.template Bottom(), documentStack_.GetSize() / sizeof(Ch)); + } + +#if RAPIDJSON_SCHEMA_VERBOSE +#define RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_() \ +RAPIDJSON_MULTILINEMACRO_BEGIN\ + *documentStack_.template Push() = '\0';\ + documentStack_.template Pop(1);\ + internal::PrintInvalidDocument(documentStack_.template Bottom());\ +RAPIDJSON_MULTILINEMACRO_END +#else +#define RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_() +#endif + +#define RAPIDJSON_SCHEMA_HANDLE_BEGIN_(method, arg1)\ + if (!valid_) return false; \ + if (!BeginValue() || !CurrentSchema().method arg1) {\ + RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_();\ + return valid_ = false;\ + } + +#define RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(method, arg2)\ + for (Context* context = schemaStack_.template Bottom(); context != schemaStack_.template End(); context++) {\ + if (context->hasher)\ + static_cast(context->hasher)->method arg2;\ + if (context->validators)\ + for (SizeType i_ = 0; i_ < context->validatorCount; i_++)\ + static_cast(context->validators[i_])->method arg2;\ + if (context->patternPropertiesValidators)\ + for (SizeType i_ = 0; i_ < context->patternPropertiesValidatorCount; i_++)\ + static_cast(context->patternPropertiesValidators[i_])->method arg2;\ + } + +#define RAPIDJSON_SCHEMA_HANDLE_END_(method, arg2)\ + return valid_ = EndValue() && outputHandler_.method arg2 + +#define RAPIDJSON_SCHEMA_HANDLE_VALUE_(method, arg1, arg2) \ + RAPIDJSON_SCHEMA_HANDLE_BEGIN_ (method, arg1);\ + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(method, arg2);\ + RAPIDJSON_SCHEMA_HANDLE_END_ (method, arg2) + + bool Null() { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Null, (CurrentContext() ), ( )); } + bool Bool(bool b) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Bool, (CurrentContext(), b), (b)); } + bool Int(int i) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Int, (CurrentContext(), i), (i)); } + bool Uint(unsigned u) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Uint, (CurrentContext(), u), (u)); } + bool Int64(int64_t i) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Int64, (CurrentContext(), i), (i)); } + bool Uint64(uint64_t u) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Uint64, (CurrentContext(), u), (u)); } + bool Double(double d) { RAPIDJSON_SCHEMA_HANDLE_VALUE_(Double, (CurrentContext(), d), (d)); } + bool RawNumber(const Ch* str, SizeType length, bool copy) + { RAPIDJSON_SCHEMA_HANDLE_VALUE_(String, (CurrentContext(), str, length, copy), (str, length, copy)); } + bool String(const Ch* str, SizeType length, bool copy) + { RAPIDJSON_SCHEMA_HANDLE_VALUE_(String, (CurrentContext(), str, length, copy), (str, length, copy)); } + + bool StartObject() { + RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartObject, (CurrentContext())); + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartObject, ()); + return valid_ = outputHandler_.StartObject(); + } + + bool Key(const Ch* str, SizeType len, bool copy) { + if (!valid_) return false; + AppendToken(str, len); + if (!CurrentSchema().Key(CurrentContext(), str, len, copy)) return valid_ = false; + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(Key, (str, len, copy)); + return valid_ = outputHandler_.Key(str, len, copy); + } + + bool EndObject(SizeType memberCount) { + if (!valid_) return false; + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(EndObject, (memberCount)); + if (!CurrentSchema().EndObject(CurrentContext(), memberCount)) return valid_ = false; + RAPIDJSON_SCHEMA_HANDLE_END_(EndObject, (memberCount)); + } + + bool StartArray() { + RAPIDJSON_SCHEMA_HANDLE_BEGIN_(StartArray, (CurrentContext())); + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(StartArray, ()); + return valid_ = outputHandler_.StartArray(); + } + + bool EndArray(SizeType elementCount) { + if (!valid_) return false; + RAPIDJSON_SCHEMA_HANDLE_PARALLEL_(EndArray, (elementCount)); + if (!CurrentSchema().EndArray(CurrentContext(), elementCount)) return valid_ = false; + RAPIDJSON_SCHEMA_HANDLE_END_(EndArray, (elementCount)); + } + +#undef RAPIDJSON_SCHEMA_HANDLE_BEGIN_VERBOSE_ +#undef RAPIDJSON_SCHEMA_HANDLE_BEGIN_ +#undef RAPIDJSON_SCHEMA_HANDLE_PARALLEL_ +#undef RAPIDJSON_SCHEMA_HANDLE_VALUE_ + + // Implementation of ISchemaStateFactory + virtual ISchemaValidator* CreateSchemaValidator(const SchemaType& root) { + return new (GetStateAllocator().Malloc(sizeof(GenericSchemaValidator))) GenericSchemaValidator(*schemaDocument_, root, +#if RAPIDJSON_SCHEMA_VERBOSE + depth_ + 1, +#endif + &GetStateAllocator()); + } + + virtual void DestroySchemaValidator(ISchemaValidator* validator) { + GenericSchemaValidator* v = static_cast(validator); + v->~GenericSchemaValidator(); + StateAllocator::Free(v); + } + + virtual void* CreateHasher() { + return new (GetStateAllocator().Malloc(sizeof(HasherType))) HasherType(&GetStateAllocator()); + } + + virtual uint64_t GetHashCode(void* hasher) { + return static_cast(hasher)->GetHashCode(); + } + + virtual void DestroryHasher(void* hasher) { + HasherType* h = static_cast(hasher); + h->~HasherType(); + StateAllocator::Free(h); + } + + virtual void* MallocState(size_t size) { + return GetStateAllocator().Malloc(size); + } + + virtual void FreeState(void* p) { + return StateAllocator::Free(p); + } + +private: + typedef typename SchemaType::Context Context; + typedef GenericValue, StateAllocator> HashCodeArray; + typedef internal::Hasher HasherType; + + GenericSchemaValidator( + const SchemaDocumentType& schemaDocument, + const SchemaType& root, +#if RAPIDJSON_SCHEMA_VERBOSE + unsigned depth, +#endif + StateAllocator* allocator = 0, + size_t schemaStackCapacity = kDefaultSchemaStackCapacity, + size_t documentStackCapacity = kDefaultDocumentStackCapacity) + : + schemaDocument_(&schemaDocument), + root_(root), + outputHandler_(GetNullHandler()), + stateAllocator_(allocator), + ownStateAllocator_(0), + schemaStack_(allocator, schemaStackCapacity), + documentStack_(allocator, documentStackCapacity), + valid_(true) +#if RAPIDJSON_SCHEMA_VERBOSE + , depth_(depth) +#endif + { + } + + StateAllocator& GetStateAllocator() { + if (!stateAllocator_) + stateAllocator_ = ownStateAllocator_ = RAPIDJSON_NEW(StateAllocator()); + return *stateAllocator_; + } + + bool BeginValue() { + if (schemaStack_.Empty()) + PushSchema(root_); + else { + if (CurrentContext().inArray) + internal::TokenHelper, Ch>::AppendIndexToken(documentStack_, CurrentContext().arrayElementIndex); + + if (!CurrentSchema().BeginValue(CurrentContext())) + return false; + + SizeType count = CurrentContext().patternPropertiesSchemaCount; + const SchemaType** sa = CurrentContext().patternPropertiesSchemas; + typename Context::PatternValidatorType patternValidatorType = CurrentContext().valuePatternValidatorType; + bool valueUniqueness = CurrentContext().valueUniqueness; + if (CurrentContext().valueSchema) + PushSchema(*CurrentContext().valueSchema); + + if (count > 0) { + CurrentContext().objectPatternValidatorType = patternValidatorType; + ISchemaValidator**& va = CurrentContext().patternPropertiesValidators; + SizeType& validatorCount = CurrentContext().patternPropertiesValidatorCount; + va = static_cast(MallocState(sizeof(ISchemaValidator*) * count)); + for (SizeType i = 0; i < count; i++) + va[validatorCount++] = CreateSchemaValidator(*sa[i]); + } + + CurrentContext().arrayUniqueness = valueUniqueness; + } + return true; + } + + bool EndValue() { + if (!CurrentSchema().EndValue(CurrentContext())) + return false; + +#if RAPIDJSON_SCHEMA_VERBOSE + GenericStringBuffer sb; + schemaDocument_->GetPointer(&CurrentSchema()).Stringify(sb); + + *documentStack_.template Push() = '\0'; + documentStack_.template Pop(1); + internal::PrintValidatorPointers(depth_, sb.GetString(), documentStack_.template Bottom()); +#endif + + uint64_t h = CurrentContext().arrayUniqueness ? static_cast(CurrentContext().hasher)->GetHashCode() : 0; + + PopSchema(); + + if (!schemaStack_.Empty()) { + Context& context = CurrentContext(); + if (context.valueUniqueness) { + HashCodeArray* a = static_cast(context.arrayElementHashCodes); + if (!a) + CurrentContext().arrayElementHashCodes = a = new (GetStateAllocator().Malloc(sizeof(HashCodeArray))) HashCodeArray(kArrayType); + for (typename HashCodeArray::ConstValueIterator itr = a->Begin(); itr != a->End(); ++itr) + if (itr->GetUint64() == h) + RAPIDJSON_INVALID_KEYWORD_RETURN(SchemaType::GetUniqueItemsString()); + a->PushBack(h, GetStateAllocator()); + } + } + + // Remove the last token of document pointer + while (!documentStack_.Empty() && *documentStack_.template Pop(1) != '/') + ; + + return true; + } + + void AppendToken(const Ch* str, SizeType len) { + documentStack_.template Reserve(1 + len * 2); // worst case all characters are escaped as two characters + *documentStack_.template PushUnsafe() = '/'; + for (SizeType i = 0; i < len; i++) { + if (str[i] == '~') { + *documentStack_.template PushUnsafe() = '~'; + *documentStack_.template PushUnsafe() = '0'; + } + else if (str[i] == '/') { + *documentStack_.template PushUnsafe() = '~'; + *documentStack_.template PushUnsafe() = '1'; + } + else + *documentStack_.template PushUnsafe() = str[i]; + } + } + + RAPIDJSON_FORCEINLINE void PushSchema(const SchemaType& schema) { new (schemaStack_.template Push()) Context(*this, &schema); } + + RAPIDJSON_FORCEINLINE void PopSchema() { + Context* c = schemaStack_.template Pop(1); + if (HashCodeArray* a = static_cast(c->arrayElementHashCodes)) { + a->~HashCodeArray(); + StateAllocator::Free(a); + } + c->~Context(); + } + + const SchemaType& CurrentSchema() const { return *schemaStack_.template Top()->schema; } + Context& CurrentContext() { return *schemaStack_.template Top(); } + const Context& CurrentContext() const { return *schemaStack_.template Top(); } + + static OutputHandler& GetNullHandler() { + static OutputHandler nullHandler; + return nullHandler; + } + + static const size_t kDefaultSchemaStackCapacity = 1024; + static const size_t kDefaultDocumentStackCapacity = 256; + const SchemaDocumentType* schemaDocument_; + const SchemaType& root_; + OutputHandler& outputHandler_; + StateAllocator* stateAllocator_; + StateAllocator* ownStateAllocator_; + internal::Stack schemaStack_; //!< stack to store the current path of schema (BaseSchemaType *) + internal::Stack documentStack_; //!< stack to store the current path of validating document (Ch) + bool valid_; +#if RAPIDJSON_SCHEMA_VERBOSE + unsigned depth_; +#endif +}; + +typedef GenericSchemaValidator SchemaValidator; + +/////////////////////////////////////////////////////////////////////////////// +// SchemaValidatingReader + +//! A helper class for parsing with validation. +/*! + This helper class is a functor, designed as a parameter of \ref GenericDocument::Populate(). + + \tparam parseFlags Combination of \ref ParseFlag. + \tparam InputStream Type of input stream, implementing Stream concept. + \tparam SourceEncoding Encoding of the input stream. + \tparam SchemaDocumentType Type of schema document. + \tparam StackAllocator Allocator type for stack. +*/ +template < + unsigned parseFlags, + typename InputStream, + typename SourceEncoding, + typename SchemaDocumentType = SchemaDocument, + typename StackAllocator = CrtAllocator> +class SchemaValidatingReader { +public: + typedef typename SchemaDocumentType::PointerType PointerType; + typedef typename InputStream::Ch Ch; + + //! Constructor + /*! + \param is Input stream. + \param sd Schema document. + */ + SchemaValidatingReader(InputStream& is, const SchemaDocumentType& sd) : is_(is), sd_(sd), invalidSchemaKeyword_(), isValid_(true) {} + + template + bool operator()(Handler& handler) { + GenericReader reader; + GenericSchemaValidator validator(sd_, handler); + parseResult_ = reader.template Parse(is_, validator); + + isValid_ = validator.IsValid(); + if (isValid_) { + invalidSchemaPointer_ = PointerType(); + invalidSchemaKeyword_ = 0; + invalidDocumentPointer_ = PointerType(); + } + else { + invalidSchemaPointer_ = validator.GetInvalidSchemaPointer(); + invalidSchemaKeyword_ = validator.GetInvalidSchemaKeyword(); + invalidDocumentPointer_ = validator.GetInvalidDocumentPointer(); + } + + return parseResult_; + } + + const ParseResult& GetParseResult() const { return parseResult_; } + bool IsValid() const { return isValid_; } + const PointerType& GetInvalidSchemaPointer() const { return invalidSchemaPointer_; } + const Ch* GetInvalidSchemaKeyword() const { return invalidSchemaKeyword_; } + const PointerType& GetInvalidDocumentPointer() const { return invalidDocumentPointer_; } + +private: + InputStream& is_; + const SchemaDocumentType& sd_; + + ParseResult parseResult_; + PointerType invalidSchemaPointer_; + const Ch* invalidSchemaKeyword_; + PointerType invalidDocumentPointer_; + bool isValid_; +}; + +RAPIDJSON_NAMESPACE_END +RAPIDJSON_DIAG_POP + +#endif // RAPIDJSON_SCHEMA_H_ diff --git a/rapidjson-1.1.0/rapid_json/stream.h b/rapidjson-1.1.0/rapid_json/stream.h new file mode 100644 index 000000000..fef82c252 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/stream.h @@ -0,0 +1,179 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#include "rapidjson.h" + +#ifndef RAPIDJSON_STREAM_H_ +#define RAPIDJSON_STREAM_H_ + +#include "encodings.h" + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// Stream + +/*! \class rapidjson::Stream + \brief Concept for reading and writing characters. + + For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd(). + + For write-only stream, only need to implement Put() and Flush(). + +\code +concept Stream { + typename Ch; //!< Character type of the stream. + + //! Read the current character from stream without moving the read cursor. + Ch Peek() const; + + //! Read the current character from stream and moving the read cursor to next character. + Ch Take(); + + //! Get the current read cursor. + //! \return Number of characters read from start. + size_t Tell(); + + //! Begin writing operation at the current read pointer. + //! \return The begin writer pointer. + Ch* PutBegin(); + + //! Write a character. + void Put(Ch c); + + //! Flush the buffer. + void Flush(); + + //! End the writing operation. + //! \param begin The begin write pointer returned by PutBegin(). + //! \return Number of characters written. + size_t PutEnd(Ch* begin); +} +\endcode +*/ + +//! Provides additional information for stream. +/*! + By using traits pattern, this type provides a default configuration for stream. + For custom stream, this type can be specialized for other configuration. + See TEST(Reader, CustomStringStream) in readertest.cpp for example. +*/ +template +struct StreamTraits { + //! Whether to make local copy of stream for optimization during parsing. + /*! + By default, for safety, streams do not use local copy optimization. + Stream that can be copied fast should specialize this, like StreamTraits. + */ + enum { copyOptimization = 0 }; +}; + +//! Reserve n characters for writing to a stream. +template +inline void PutReserve(Stream& stream, size_t count) { + (void)stream; + (void)count; +} + +//! Write character to a stream, presuming buffer is reserved. +template +inline void PutUnsafe(Stream& stream, typename Stream::Ch c) { + stream.Put(c); +} + +//! Put N copies of a character to a stream. +template +inline void PutN(Stream& stream, Ch c, size_t n) { + PutReserve(stream, n); + for (size_t i = 0; i < n; i++) + PutUnsafe(stream, c); +} + +/////////////////////////////////////////////////////////////////////////////// +// StringStream + +//! Read-only string stream. +/*! \note implements Stream concept +*/ +template +struct GenericStringStream { + typedef typename Encoding::Ch Ch; + + GenericStringStream(const Ch *src) : src_(src), head_(src) {} + + Ch Peek() const { return *src_; } + Ch Take() { return *src_++; } + size_t Tell() const { return static_cast(src_ - head_); } + + Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } + void Put(Ch) { RAPIDJSON_ASSERT(false); } + void Flush() { RAPIDJSON_ASSERT(false); } + size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } + + const Ch* src_; //!< Current read position. + const Ch* head_; //!< Original head of the string. +}; + +template +struct StreamTraits > { + enum { copyOptimization = 1 }; +}; + +//! String stream with UTF8 encoding. +typedef GenericStringStream > StringStream; + +/////////////////////////////////////////////////////////////////////////////// +// InsituStringStream + +//! A read-write string stream. +/*! This string stream is particularly designed for in-situ parsing. + \note implements Stream concept +*/ +template +struct GenericInsituStringStream { + typedef typename Encoding::Ch Ch; + + GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {} + + // Read + Ch Peek() { return *src_; } + Ch Take() { return *src_++; } + size_t Tell() { return static_cast(src_ - head_); } + + // Write + void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; } + + Ch* PutBegin() { return dst_ = src_; } + size_t PutEnd(Ch* begin) { return static_cast(dst_ - begin); } + void Flush() {} + + Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; } + void Pop(size_t count) { dst_ -= count; } + + Ch* src_; + Ch* dst_; + Ch* head_; +}; + +template +struct StreamTraits > { + enum { copyOptimization = 1 }; +}; + +//! Insitu string stream with UTF8 encoding. +typedef GenericInsituStringStream > InsituStringStream; + +RAPIDJSON_NAMESPACE_END + +#endif // RAPIDJSON_STREAM_H_ diff --git a/rapidjson-1.1.0/rapid_json/stringbuffer.h b/rapidjson-1.1.0/rapid_json/stringbuffer.h new file mode 100644 index 000000000..78f34d209 --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/stringbuffer.h @@ -0,0 +1,117 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_STRINGBUFFER_H_ +#define RAPIDJSON_STRINGBUFFER_H_ + +#include "stream.h" +#include "internal/stack.h" + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS +#include // std::move +#endif + +#include "internal/stack.h" + +#if defined(__clang__) +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(c++98-compat) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +//! Represents an in-memory output stream. +/*! + \tparam Encoding Encoding of the stream. + \tparam Allocator type for allocating memory buffer. + \note implements Stream concept +*/ +template +class GenericStringBuffer { +public: + typedef typename Encoding::Ch Ch; + + GenericStringBuffer(Allocator* allocator = 0, size_t capacity = kDefaultCapacity) : stack_(allocator, capacity) {} + +#if RAPIDJSON_HAS_CXX11_RVALUE_REFS + GenericStringBuffer(GenericStringBuffer&& rhs) : stack_(std::move(rhs.stack_)) {} + GenericStringBuffer& operator=(GenericStringBuffer&& rhs) { + if (&rhs != this) + stack_ = std::move(rhs.stack_); + return *this; + } +#endif + + void Put(Ch c) { *stack_.template Push() = c; } + void PutUnsafe(Ch c) { *stack_.template PushUnsafe() = c; } + void Flush() {} + + void Clear() { stack_.Clear(); } + void ShrinkToFit() { + // Push and pop a null terminator. This is safe. + *stack_.template Push() = '\0'; + stack_.ShrinkToFit(); + stack_.template Pop(1); + } + + void Reserve(size_t count) { stack_.template Reserve(count); } + Ch* Push(size_t count) { return stack_.template Push(count); } + Ch* PushUnsafe(size_t count) { return stack_.template PushUnsafe(count); } + void Pop(size_t count) { stack_.template Pop(count); } + + const Ch* GetString() const { + // Push and pop a null terminator. This is safe. + *stack_.template Push() = '\0'; + stack_.template Pop(1); + + return stack_.template Bottom(); + } + + size_t GetSize() const { return stack_.GetSize(); } + + static const size_t kDefaultCapacity = 256; + mutable internal::Stack stack_; + +private: + // Prohibit copy constructor & assignment operator. + GenericStringBuffer(const GenericStringBuffer&); + GenericStringBuffer& operator=(const GenericStringBuffer&); +}; + +//! String buffer with UTF8 encoding +typedef GenericStringBuffer > StringBuffer; + +template +inline void PutReserve(GenericStringBuffer& stream, size_t count) { + stream.Reserve(count); +} + +template +inline void PutUnsafe(GenericStringBuffer& stream, typename Encoding::Ch c) { + stream.PutUnsafe(c); +} + +//! Implement specialized version of PutN() with memset() for better performance. +template<> +inline void PutN(GenericStringBuffer >& stream, char c, size_t n) { + std::memset(stream.stack_.Push(n), c, n * sizeof(c)); +} + +RAPIDJSON_NAMESPACE_END + +#if defined(__clang__) +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_STRINGBUFFER_H_ diff --git a/rapidjson-1.1.0/rapid_json/writer.h b/rapidjson-1.1.0/rapid_json/writer.h new file mode 100644 index 000000000..94f22dd5f --- /dev/null +++ b/rapidjson-1.1.0/rapid_json/writer.h @@ -0,0 +1,610 @@ +// Tencent is pleased to support the open source community by making RapidJSON available. +// +// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. +// +// Licensed under the MIT License (the "License"); you may not use this file except +// in compliance with the License. You may obtain a copy of the License at +// +// http://opensource.org/licenses/MIT +// +// Unless required by applicable law or agreed to in writing, software distributed +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR +// CONDITIONS OF ANY KIND, either express or implied. See the License for the +// specific language governing permissions and limitations under the License. + +#ifndef RAPIDJSON_WRITER_H_ +#define RAPIDJSON_WRITER_H_ + +#include "stream.h" +#include "internal/stack.h" +#include "internal/strfunc.h" +#include "internal/dtoa.h" +#include "internal/itoa.h" +#include "stringbuffer.h" +#include // placement new + +#if defined(RAPIDJSON_SIMD) && defined(_MSC_VER) +#include +#pragma intrinsic(_BitScanForward) +#endif +#ifdef RAPIDJSON_SSE42 +#include +#elif defined(RAPIDJSON_SSE2) +#include +#endif + +#ifdef _MSC_VER +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_PUSH +RAPIDJSON_DIAG_OFF(padded) +RAPIDJSON_DIAG_OFF(unreachable-code) +#endif + +RAPIDJSON_NAMESPACE_BEGIN + +/////////////////////////////////////////////////////////////////////////////// +// WriteFlag + +/*! \def RAPIDJSON_WRITE_DEFAULT_FLAGS + \ingroup RAPIDJSON_CONFIG + \brief User-defined kWriteDefaultFlags definition. + + User can define this as any \c WriteFlag combinations. +*/ +#ifndef RAPIDJSON_WRITE_DEFAULT_FLAGS +#define RAPIDJSON_WRITE_DEFAULT_FLAGS kWriteNoFlags +#endif + +//! Combination of writeFlags +enum WriteFlag { + kWriteNoFlags = 0, //!< No flags are set. + kWriteValidateEncodingFlag = 1, //!< Validate encoding of JSON strings. + kWriteNanAndInfFlag = 2, //!< Allow writing of Infinity, -Infinity and NaN. + kWriteDefaultFlags = RAPIDJSON_WRITE_DEFAULT_FLAGS //!< Default write flags. Can be customized by defining RAPIDJSON_WRITE_DEFAULT_FLAGS +}; + +//! JSON writer +/*! Writer implements the concept Handler. + It generates JSON text by events to an output os. + + User may programmatically calls the functions of a writer to generate JSON text. + + On the other side, a writer can also be passed to objects that generates events, + + for example Reader::Parse() and Document::Accept(). + + \tparam OutputStream Type of output stream. + \tparam SourceEncoding Encoding of source string. + \tparam TargetEncoding Encoding of output stream. + \tparam StackAllocator Type of allocator for allocating memory of stack. + \note implements Handler concept +*/ +template, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags> +class Writer { +public: + typedef typename SourceEncoding::Ch Ch; + + static const int kDefaultMaxDecimalPlaces = 324; + + //! Constructor + /*! \param os Output stream. + \param stackAllocator User supplied allocator. If it is null, it will create a private one. + \param levelDepth Initial capacity of stack. + */ + explicit + Writer(OutputStream& os, StackAllocator* stackAllocator = 0, size_t levelDepth = kDefaultLevelDepth) : + os_(&os), level_stack_(stackAllocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {} + + explicit + Writer(StackAllocator* allocator = 0, size_t levelDepth = kDefaultLevelDepth) : + os_(0), level_stack_(allocator, levelDepth * sizeof(Level)), maxDecimalPlaces_(kDefaultMaxDecimalPlaces), hasRoot_(false) {} + + //! Reset the writer with a new stream. + /*! + This function reset the writer with a new stream and default settings, + in order to make a Writer object reusable for output multiple JSONs. + + \param os New output stream. + \code + Writer writer(os1); + writer.StartObject(); + // ... + writer.EndObject(); + + writer.Reset(os2); + writer.StartObject(); + // ... + writer.EndObject(); + \endcode + */ + void Reset(OutputStream& os) { + os_ = &os; + hasRoot_ = false; + level_stack_.Clear(); + } + + //! Checks whether the output is a complete JSON. + /*! + A complete JSON has a complete root object or array. + */ + bool IsComplete() const { + return hasRoot_ && level_stack_.Empty(); + } + + int GetMaxDecimalPlaces() const { + return maxDecimalPlaces_; + } + + //! Sets the maximum number of decimal places for double output. + /*! + This setting truncates the output with specified number of decimal places. + + For example, + + \code + writer.SetMaxDecimalPlaces(3); + writer.StartArray(); + writer.Double(0.12345); // "0.123" + writer.Double(0.0001); // "0.0" + writer.Double(1.234567890123456e30); // "1.234567890123456e30" (do not truncate significand for positive exponent) + writer.Double(1.23e-4); // "0.0" (do truncate significand for negative exponent) + writer.EndArray(); + \endcode + + The default setting does not truncate any decimal places. You can restore to this setting by calling + \code + writer.SetMaxDecimalPlaces(Writer::kDefaultMaxDecimalPlaces); + \endcode + */ + void SetMaxDecimalPlaces(int maxDecimalPlaces) { + maxDecimalPlaces_ = maxDecimalPlaces; + } + + /*!@name Implementation of Handler + \see Handler + */ + //@{ + + bool Null() { Prefix(kNullType); return EndValue(WriteNull()); } + bool Bool(bool b) { Prefix(b ? kTrueType : kFalseType); return EndValue(WriteBool(b)); } + bool Int(int i) { Prefix(kNumberType); return EndValue(WriteInt(i)); } + bool Uint(unsigned u) { Prefix(kNumberType); return EndValue(WriteUint(u)); } + bool Int64(int64_t i64) { Prefix(kNumberType); return EndValue(WriteInt64(i64)); } + bool Uint64(uint64_t u64) { Prefix(kNumberType); return EndValue(WriteUint64(u64)); } + + //! Writes the given \c double value to the stream + /*! + \param d The value to be written. + \return Whether it is succeed. + */ + bool Double(double d) { Prefix(kNumberType); return EndValue(WriteDouble(d)); } + + bool RawNumber(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + Prefix(kNumberType); + return EndValue(WriteString(str, length)); + } + + bool String(const Ch* str, SizeType length, bool copy = false) { + (void)copy; + Prefix(kStringType); + return EndValue(WriteString(str, length)); + } + +#if RAPIDJSON_HAS_STDSTRING + bool String(const std::basic_string& str) { + return String(str.data(), SizeType(str.size())); + } +#endif + + bool StartObject() { + Prefix(kObjectType); + new (level_stack_.template Push()) Level(false); + return WriteStartObject(); + } + + bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); } + + bool EndObject(SizeType memberCount = 0) { + (void)memberCount; + RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); + RAPIDJSON_ASSERT(!level_stack_.template Top()->inArray); + level_stack_.template Pop(1); + return EndValue(WriteEndObject()); + } + + bool StartArray() { + Prefix(kArrayType); + new (level_stack_.template Push()) Level(true); + return WriteStartArray(); + } + + bool EndArray(SizeType elementCount = 0) { + (void)elementCount; + RAPIDJSON_ASSERT(level_stack_.GetSize() >= sizeof(Level)); + RAPIDJSON_ASSERT(level_stack_.template Top()->inArray); + level_stack_.template Pop(1); + return EndValue(WriteEndArray()); + } + //@} + + /*! @name Convenience extensions */ + //@{ + + //! Simpler but slower overload. + bool String(const Ch* str) { return String(str, internal::StrLen(str)); } + bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); } + + //@} + + //! Write a raw JSON value. + /*! + For user to write a stringified JSON as a value. + + \param json A well-formed JSON value. It should not contain null character within [0, length - 1] range. + \param length Length of the json. + \param type Type of the root of json. + */ + bool RawValue(const Ch* json, size_t length, Type type) { Prefix(type); return EndValue(WriteRawValue(json, length)); } + +protected: + //! Information for each nested level + struct Level { + Level(bool inArray_) : valueCount(0), inArray(inArray_) {} + size_t valueCount; //!< number of values in this level + bool inArray; //!< true if in array, otherwise in object + }; + + static const size_t kDefaultLevelDepth = 32; + + bool WriteNull() { + PutReserve(*os_, 4); + PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 'l'); return true; + } + + bool WriteBool(bool b) { + if (b) { + PutReserve(*os_, 4); + PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'r'); PutUnsafe(*os_, 'u'); PutUnsafe(*os_, 'e'); + } + else { + PutReserve(*os_, 5); + PutUnsafe(*os_, 'f'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'l'); PutUnsafe(*os_, 's'); PutUnsafe(*os_, 'e'); + } + return true; + } + + bool WriteInt(int i) { + char buffer[11]; + const char* end = internal::i32toa(i, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteUint(unsigned u) { + char buffer[10]; + const char* end = internal::u32toa(u, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteInt64(int64_t i64) { + char buffer[21]; + const char* end = internal::i64toa(i64, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (const char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteUint64(uint64_t u64) { + char buffer[20]; + char* end = internal::u64toa(u64, buffer); + PutReserve(*os_, static_cast(end - buffer)); + for (char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteDouble(double d) { + if (internal::Double(d).IsNanOrInf()) { + if (!(writeFlags & kWriteNanAndInfFlag)) + return false; + if (internal::Double(d).IsNan()) { + PutReserve(*os_, 3); + PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); + return true; + } + if (internal::Double(d).Sign()) { + PutReserve(*os_, 9); + PutUnsafe(*os_, '-'); + } + else + PutReserve(*os_, 8); + PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); + PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); + return true; + } + + char buffer[25]; + char* end = internal::dtoa(d, buffer, maxDecimalPlaces_); + PutReserve(*os_, static_cast(end - buffer)); + for (char* p = buffer; p != end; ++p) + PutUnsafe(*os_, static_cast(*p)); + return true; + } + + bool WriteString(const Ch* str, SizeType length) { + static const typename TargetEncoding::Ch hexDigits[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + static const char escape[256] = { +#define Z16 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 + //0 1 2 3 4 5 6 7 8 9 A B C D E F + 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'b', 't', 'n', 'u', 'f', 'r', 'u', 'u', // 00 + 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', 'u', // 10 + 0, 0, '"', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20 + Z16, Z16, // 30~4F + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,'\\', 0, 0, 0, // 50 + Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16, Z16 // 60~FF +#undef Z16 + }; + + if (TargetEncoding::supportUnicode) + PutReserve(*os_, 2 + length * 6); // "\uxxxx..." + else + PutReserve(*os_, 2 + length * 12); // "\uxxxx\uyyyy..." + + PutUnsafe(*os_, '\"'); + GenericStringStream is(str); + while (ScanWriteUnescapedString(is, length)) { + const Ch c = is.Peek(); + if (!TargetEncoding::supportUnicode && static_cast(c) >= 0x80) { + // Unicode escaping + unsigned codepoint; + if (RAPIDJSON_UNLIKELY(!SourceEncoding::Decode(is, &codepoint))) + return false; + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, 'u'); + if (codepoint <= 0xD7FF || (codepoint >= 0xE000 && codepoint <= 0xFFFF)) { + PutUnsafe(*os_, hexDigits[(codepoint >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(codepoint ) & 15]); + } + else { + RAPIDJSON_ASSERT(codepoint >= 0x010000 && codepoint <= 0x10FFFF); + // Surrogate pair + unsigned s = codepoint - 0x010000; + unsigned lead = (s >> 10) + 0xD800; + unsigned trail = (s & 0x3FF) + 0xDC00; + PutUnsafe(*os_, hexDigits[(lead >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(lead >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(lead >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(lead ) & 15]); + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, 'u'); + PutUnsafe(*os_, hexDigits[(trail >> 12) & 15]); + PutUnsafe(*os_, hexDigits[(trail >> 8) & 15]); + PutUnsafe(*os_, hexDigits[(trail >> 4) & 15]); + PutUnsafe(*os_, hexDigits[(trail ) & 15]); + } + } + else if ((sizeof(Ch) == 1 || static_cast(c) < 256) && RAPIDJSON_UNLIKELY(escape[static_cast(c)])) { + is.Take(); + PutUnsafe(*os_, '\\'); + PutUnsafe(*os_, static_cast(escape[static_cast(c)])); + if (escape[static_cast(c)] == 'u') { + PutUnsafe(*os_, '0'); + PutUnsafe(*os_, '0'); + PutUnsafe(*os_, hexDigits[static_cast(c) >> 4]); + PutUnsafe(*os_, hexDigits[static_cast(c) & 0xF]); + } + } + else if (RAPIDJSON_UNLIKELY(!(writeFlags & kWriteValidateEncodingFlag ? + Transcoder::Validate(is, *os_) : + Transcoder::TranscodeUnsafe(is, *os_)))) + return false; + } + PutUnsafe(*os_, '\"'); + return true; + } + + bool ScanWriteUnescapedString(GenericStringStream& is, size_t length) { + return RAPIDJSON_LIKELY(is.Tell() < length); + } + + bool WriteStartObject() { os_->Put('{'); return true; } + bool WriteEndObject() { os_->Put('}'); return true; } + bool WriteStartArray() { os_->Put('['); return true; } + bool WriteEndArray() { os_->Put(']'); return true; } + + bool WriteRawValue(const Ch* json, size_t length) { + PutReserve(*os_, length); + for (size_t i = 0; i < length; i++) { + RAPIDJSON_ASSERT(json[i] != '\0'); + PutUnsafe(*os_, json[i]); + } + return true; + } + + void Prefix(Type type) { + (void)type; + if (RAPIDJSON_LIKELY(level_stack_.GetSize() != 0)) { // this value is not at root + Level* level = level_stack_.template Top(); + if (level->valueCount > 0) { + if (level->inArray) + os_->Put(','); // add comma if it is not the first element in array + else // in object + os_->Put((level->valueCount % 2 == 0) ? ',' : ':'); + } + if (!level->inArray && level->valueCount % 2 == 0) + RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name + level->valueCount++; + } + else { + RAPIDJSON_ASSERT(!hasRoot_); // Should only has one and only one root. + hasRoot_ = true; + } + } + + // Flush the value if it is the top level one. + bool EndValue(bool ret) { + if (RAPIDJSON_UNLIKELY(level_stack_.Empty())) // end of json text + os_->Flush(); + return ret; + } + + OutputStream* os_; + internal::Stack level_stack_; + int maxDecimalPlaces_; + bool hasRoot_; + +private: + // Prohibit copy constructor & assignment operator. + Writer(const Writer&); + Writer& operator=(const Writer&); +}; + +// Full specialization for StringStream to prevent memory copying + +template<> +inline bool Writer::WriteInt(int i) { + char *buffer = os_->Push(11); + const char* end = internal::i32toa(i, buffer); + os_->Pop(static_cast(11 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteUint(unsigned u) { + char *buffer = os_->Push(10); + const char* end = internal::u32toa(u, buffer); + os_->Pop(static_cast(10 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteInt64(int64_t i64) { + char *buffer = os_->Push(21); + const char* end = internal::i64toa(i64, buffer); + os_->Pop(static_cast(21 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteUint64(uint64_t u) { + char *buffer = os_->Push(20); + const char* end = internal::u64toa(u, buffer); + os_->Pop(static_cast(20 - (end - buffer))); + return true; +} + +template<> +inline bool Writer::WriteDouble(double d) { + if (internal::Double(d).IsNanOrInf()) { + // Note: This code path can only be reached if (RAPIDJSON_WRITE_DEFAULT_FLAGS & kWriteNanAndInfFlag). + if (!(kWriteDefaultFlags & kWriteNanAndInfFlag)) + return false; + if (internal::Double(d).IsNan()) { + PutReserve(*os_, 3); + PutUnsafe(*os_, 'N'); PutUnsafe(*os_, 'a'); PutUnsafe(*os_, 'N'); + return true; + } + if (internal::Double(d).Sign()) { + PutReserve(*os_, 9); + PutUnsafe(*os_, '-'); + } + else + PutReserve(*os_, 8); + PutUnsafe(*os_, 'I'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'f'); + PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 'n'); PutUnsafe(*os_, 'i'); PutUnsafe(*os_, 't'); PutUnsafe(*os_, 'y'); + return true; + } + + char *buffer = os_->Push(25); + char* end = internal::dtoa(d, buffer, maxDecimalPlaces_); + os_->Pop(static_cast(25 - (end - buffer))); + return true; +} + +#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) +template<> +inline bool Writer::ScanWriteUnescapedString(StringStream& is, size_t length) { + if (length < 16) + return RAPIDJSON_LIKELY(is.Tell() < length); + + if (!RAPIDJSON_LIKELY(is.Tell() < length)) + return false; + + const char* p = is.src_; + const char* end = is.head_ + length; + const char* nextAligned = reinterpret_cast((reinterpret_cast(p) + 15) & static_cast(~15)); + const char* endAligned = reinterpret_cast(reinterpret_cast(end) & static_cast(~15)); + if (nextAligned > end) + return true; + + while (p != nextAligned) + if (*p < 0x20 || *p == '\"' || *p == '\\') { + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); + } + else + os_->PutUnsafe(*p++); + + // The rest of string using SIMD + static const char dquote[16] = { '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"', '\"' }; + static const char bslash[16] = { '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\', '\\' }; + static const char space[16] = { 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19, 0x19 }; + const __m128i dq = _mm_loadu_si128(reinterpret_cast(&dquote[0])); + const __m128i bs = _mm_loadu_si128(reinterpret_cast(&bslash[0])); + const __m128i sp = _mm_loadu_si128(reinterpret_cast(&space[0])); + + for (; p != endAligned; p += 16) { + const __m128i s = _mm_load_si128(reinterpret_cast(p)); + const __m128i t1 = _mm_cmpeq_epi8(s, dq); + const __m128i t2 = _mm_cmpeq_epi8(s, bs); + const __m128i t3 = _mm_cmpeq_epi8(_mm_max_epu8(s, sp), sp); // s < 0x20 <=> max(s, 0x19) == 0x19 + const __m128i x = _mm_or_si128(_mm_or_si128(t1, t2), t3); + unsigned short r = static_cast(_mm_movemask_epi8(x)); + if (RAPIDJSON_UNLIKELY(r != 0)) { // some of characters is escaped + SizeType len; +#ifdef _MSC_VER // Find the index of first escaped + unsigned long offset; + _BitScanForward(&offset, r); + len = offset; +#else + len = static_cast(__builtin_ffs(r) - 1); +#endif + char* q = reinterpret_cast(os_->PushUnsafe(len)); + for (size_t i = 0; i < len; i++) + q[i] = p[i]; + + p += len; + break; + } + _mm_storeu_si128(reinterpret_cast<__m128i *>(os_->PushUnsafe(16)), s); + } + + is.src_ = p; + return RAPIDJSON_LIKELY(is.Tell() < length); +} +#endif // defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) + +RAPIDJSON_NAMESPACE_END + +#ifdef _MSC_VER +RAPIDJSON_DIAG_POP +#endif + +#ifdef __clang__ +RAPIDJSON_DIAG_POP +#endif + +#endif // RAPIDJSON_RAPIDJSON_H_ diff --git a/retroshare-android-service/src/retroshare-android-service.pro b/retroshare-android-service/src/retroshare-android-service.pro index bcafa7725..f83392456 100644 --- a/retroshare-android-service/src/retroshare-android-service.pro +++ b/retroshare-android-service/src/retroshare-android-service.pro @@ -11,43 +11,8 @@ android-*:CONFIG += dll android-*:TEMPLATE = lib !android-*:TEMPLATE = app -DEPENDPATH *= ../../libresapi/src -INCLUDEPATH *= ../../libresapi/src -PRE_TARGETDEPS *= ../../libresapi/src/lib/libresapi.a -LIBS *= ../../libresapi/src/lib/libresapi.a +!include("../../libresapi/src/use_libresapi.pri"):error("Including") -DEPENDPATH *= ../../libretroshare/src -INCLUDEPATH *= ../../libretroshare/src -PRE_TARGETDEPS *= ../../libretroshare/src/lib/libretroshare.a -LIBS *= ../../libretroshare/src/lib/libretroshare.a - -win32 { - OBJECTS_DIR = temp/obj - - LIBS_DIR = $$PWD/../../libs/lib - LIBS += $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a - LIBS += $$OUT_PWD/../../openpgpsdk/src/lib/libops.a - - for(lib, LIB_DIR):LIBS += -L"$$lib" - for(bin, BIN_DIR):LIBS += -L"$$bin" - - - LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz -lws2_32 - LIBS += -luuid -lole32 -liphlpapi -lcrypt32 -lgdi32 - LIBS += -lwinmm - - DEFINES *= WINDOWS_SYS WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T - - DEPENDPATH += . $$INC_DIR - INCLUDEPATH += . $$INC_DIR - - greaterThan(QT_MAJOR_VERSION, 4) { - # Qt 5 - RC_INCLUDEPATH += $$_PRO_FILE_PWD_/../../libretroshare/src - } else { - # Qt 4 - QMAKE_RC += --include-dir=$$_PRO_FILE_PWD_/../../libretroshare/src - } -} +!include("../../libretroshare/src/use_libretroshare.pri"):error("Including") SOURCES += service.cpp diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp index 30da45723..240839a2e 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.cpp @@ -36,6 +36,8 @@ #include "gui/settings/rsharesettings.h" #include "util/QtVersion.h" #include "util/RsAction.h" +#include "util/misc.h" +#include "util/rstime.h" #include #include @@ -81,6 +83,8 @@ #define MAX_SEARCH_RESULTS 3000 +#define DISABLE_SEARCH_WHILE_TYPING 1 + // Define to avoid using the search in treeview, because it is really slow for now. // //#define DONT_USE_SEARCH_IN_TREE_VIEW 1 @@ -146,7 +150,7 @@ public: /** Constructor */ SharedFilesDialog::SharedFilesDialog(RetroshareDirModel *_tree_model,RetroshareDirModel *_flat_model,QWidget *parent) -: RsAutoUpdatePage(1000,parent),model(NULL) + : RsAutoUpdatePage(1000,parent), model(NULL) { /* Invoke the Qt Designer generated object setup routine */ ui.setupUi(this); @@ -165,17 +169,24 @@ SharedFilesDialog::SharedFilesDialog(RetroshareDirModel *_tree_model,RetroshareD flat_model = _flat_model ; connect(flat_model, SIGNAL(layoutChanged()), this, SLOT(updateDirTreeView()) ); + // For filtering items we use a trick: the underlying model will use this FilterRole role to highlight selected items + // while the filterProxyModel will select them using the pre-chosen string "filtered". + tree_proxyModel = new SFDSortFilterProxyModel(tree_model, this); tree_proxyModel->setSourceModel(tree_model); tree_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); tree_proxyModel->setSortRole(RetroshareDirModel::SortRole); tree_proxyModel->sort(COLUMN_NAME); + tree_proxyModel->setFilterRole(RetroshareDirModel::FilterRole); + tree_proxyModel->setFilterRegExp(QRegExp(QString(RETROSHARE_DIR_MODEL_FILTER_STRING))) ; flat_proxyModel = new SFDSortFilterProxyModel(flat_model, this); flat_proxyModel->setSourceModel(flat_model); flat_proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive); flat_proxyModel->setSortRole(RetroshareDirModel::SortRole); flat_proxyModel->sort(COLUMN_NAME); + flat_proxyModel->setFilterRole(RetroshareDirModel::FilterRole); + flat_proxyModel->setFilterRegExp(QRegExp(QString(RETROSHARE_DIR_MODEL_FILTER_STRING))) ; // Mr.Alice: I removed this because it causes a crash for some obscur reason. Apparently when the model is changed, the proxy model cannot // deal with the change by itself. Should I call something specific? I've no idea. Removing this does not seem to cause any harm either. @@ -186,7 +197,14 @@ SharedFilesDialog::SharedFilesDialog(RetroshareDirModel *_tree_model,RetroshareD connect(ui.filterClearButton, SIGNAL(clicked()), this, SLOT(clearFilter())); connect(ui.filterStartButton, SIGNAL(clicked()), this, SLOT(startFilter())); connect(ui.filterPatternLineEdit, SIGNAL(returnPressed()), this, SLOT(startFilter())); - connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(filterRegExpChanged())); + connect(ui.filterPatternLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(onFilterTextEdited())); + //Hidden by default, shown on onFilterTextEdited + ui.filterClearButton->hide(); + ui.filterStartButton->hide(); + +// mFilterTimer = new RsProtectedTimer( this ); +// mFilterTimer->setSingleShot( true ); // Ensure the timer will fire only once after it was started +// connect(mFilterTimer, SIGNAL(timeout()), this, SLOT(filterRegExpChanged())); /* Set header resize modes and initial section sizes */ QHeaderView * header = ui.dirTreeView->header () ; @@ -410,7 +428,7 @@ void SharedFilesDialog::changeCurrentViewModel(int viewTypeIndex) ui.dirTreeView->header()->headerDataChanged(Qt::Horizontal, COLUMN_NAME, COLUMN_WN_VISU_DIR) ; // recursRestoreExpandedItems(ui.dirTreeView->rootIndex(),expanded_indexes); - FilterItems(); + FilterItems(); } void LocalSharedFilesDialog::showProperColumns() @@ -491,53 +509,63 @@ void RemoteSharedFilesDialog::spawnCustomPopupMenu( QPoint point ) { if (!rsPeers) return; /* not ready yet! */ + QMenu *contextMenu = new QMenu(this); + QModelIndex idx = ui.dirTreeView->indexAt(point) ; - if (!idx.isValid()) return; - - QModelIndex midx = proxyModel->mapToSource(idx) ; - if (!midx.isValid()) return; - - currentFile = model->data(midx, RetroshareDirModel::FileNameRole).toString() ; - int type = model->getType(midx) ; - if (type != DIR_TYPE_DIR && type != DIR_TYPE_FILE) return; - - - QMenu contextMnu( this ) ; - - collCreateAct->setEnabled(true); - collOpenAct->setEnabled(true); - - QMenu collectionMenu(tr("Collection"), this); - collectionMenu.setIcon(QIcon(IMAGE_LIBRARY)); - collectionMenu.addAction(collCreateAct); - collectionMenu.addAction(collOpenAct); - - QModelIndexList list = ui.dirTreeView->selectionModel()->selectedRows() ; - - if(type == DIR_TYPE_DIR || list.size() > 1) + if (idx.isValid()) { - QAction *downloadActI = new QAction(QIcon(IMAGE_DOWNLOAD), tr( "Download..." ), &contextMnu ) ; - connect( downloadActI , SIGNAL( triggered() ), this, SLOT( downloadRemoteSelectedInteractive() ) ) ; - contextMnu.addAction( downloadActI) ; - } - else - { - QAction *downloadAct = new QAction(QIcon(IMAGE_DOWNLOAD), tr( "Download" ), &contextMnu ) ; - connect( downloadAct , SIGNAL( triggered() ), this, SLOT( downloadRemoteSelected() ) ) ; - contextMnu.addAction( downloadAct) ; + + QModelIndex midx = proxyModel->mapToSource(idx) ; + if (midx.isValid()) + { + + currentFile = model->data(midx, RetroshareDirModel::FileNameRole).toString() ; + int type = model->getType(midx) ; + if ( (type == DIR_TYPE_DIR) || (type == DIR_TYPE_FILE) ) + { + collCreateAct->setEnabled(true); + collOpenAct->setEnabled(true); + + QModelIndexList list = ui.dirTreeView->selectionModel()->selectedRows() ; + + if(type == DIR_TYPE_DIR || list.size() > 1) + { + QAction *downloadActI = new QAction(QIcon(IMAGE_DOWNLOAD), tr( "Download..." ), contextMenu ) ; + connect( downloadActI , SIGNAL( triggered() ), this, SLOT( downloadRemoteSelectedInteractive() ) ) ; + contextMenu->addAction( downloadActI) ; + } + else + { + QAction *downloadAct = new QAction(QIcon(IMAGE_DOWNLOAD), tr( "Download" ), contextMenu ) ; + connect( downloadAct , SIGNAL( triggered() ), this, SLOT( downloadRemoteSelected() ) ) ; + contextMenu->addAction( downloadAct) ; + } + + contextMenu->addSeparator() ;//------------------------------------ + contextMenu->addAction( copylinkAct) ; + contextMenu->addAction( sendlinkAct) ; + contextMenu->addSeparator() ;//------------------------------------ + contextMenu->addAction(QIcon(IMAGE_MSG), tr("Recommend in a message to..."), this, SLOT(recommendFilesToMsg())) ; + + contextMenu->addSeparator() ;//------------------------------------ + + QMenu collectionMenu(tr("Collection"), this); + collectionMenu.setIcon(QIcon(IMAGE_LIBRARY)); + collectionMenu.addAction(collCreateAct); + collectionMenu.addAction(collOpenAct); + contextMenu->addMenu(&collectionMenu) ; + + } + + } } - contextMnu.addSeparator() ;//------------------------------------ - contextMnu.addAction( copylinkAct) ; - contextMnu.addAction( sendlinkAct) ; - contextMnu.addSeparator() ;//------------------------------------ - contextMnu.addAction(QIcon(IMAGE_MSG), tr("Recommend in a message to..."), this, SLOT(recommendFilesToMsg())) ; + contextMenu = model->getContextMenu(contextMenu); + if (!contextMenu->children().isEmpty()) + contextMenu->exec(QCursor::pos()) ; - contextMnu.addSeparator() ;//------------------------------------ - contextMnu.addMenu(&collectionMenu) ; - - contextMnu.exec(QCursor::pos()) ; + delete contextMenu; } QModelIndexList SharedFilesDialog::getSelected() @@ -908,11 +936,44 @@ void SharedFilesDialog::restoreExpandedPathsAndSelection(const std::setmodel()->index(row,0).data(Qt::DisplayRole).toString().toStdString(); recursRestoreExpandedItems(ui.dirTreeView->model()->index(row,0),path,expanded_indexes,hidden_indexes,selected_indexes); } - QItemSelection selection ; + //QItemSelection selection ; ui.dirTreeView->blockSignals(false) ; } +void SharedFilesDialog::expandAll() +{ + if(ui.dirTreeView->model() == NULL || ui.dirTreeView->model() == flat_proxyModel) // this method causes infinite loops on flat models + return ; + + ui.dirTreeView->blockSignals(true) ; + +#ifdef DEBUG_SHARED_FILES_DIALOG + std::cerr << "Restoring expanded items. " << std::endl; +#endif + for(int row = 0; row < ui.dirTreeView->model()->rowCount(); ++row) + { + std::string path = ui.dirTreeView->model()->index(row,0).data(Qt::DisplayRole).toString().toStdString(); + recursExpandAll(ui.dirTreeView->model()->index(row,0)); + } + + ui.dirTreeView->blockSignals(false) ; + +} + +void SharedFilesDialog::recursExpandAll(const QModelIndex& index) +{ + ui.dirTreeView->setExpanded(index,true) ; + + for(int row=0;rowmodel()->rowCount(index);++row) + { + QModelIndex idx(index.child(row,0)) ; + + if(ui.dirTreeView->model()->rowCount(idx) > 0) + recursExpandAll(idx) ; + } +} + void SharedFilesDialog::recursSaveExpandedItems(const QModelIndex& index,const std::string& path,std::set& exp, std::set& vis, std::set& sel @@ -964,8 +1025,8 @@ void SharedFilesDialog::recursRestoreExpandedItems(const QModelIndex& index, con bool invisible = vis.find(local_path) != vis.end(); ui.dirTreeView->setRowHidden(index.row(),index.parent(),invisible ) ; - if(invisible) - mHiddenIndexes.push_back(proxyModel->mapToSource(index)); +// if(invisible) +// mHiddenIndexes.push_back(proxyModel->mapToSource(index)); if(!invisible && exp.find(local_path) != exp.end()) { @@ -997,12 +1058,12 @@ void SharedFilesDialog::postModDirectories(bool local) flat_model->postMods(); ui.dirTreeView->update() ; - if (ui.filterPatternLineEdit->text().isEmpty() == false) + if (ui.filterPatternLineEdit->text().isEmpty() == false) FilterItems(); - ui.dirTreeView->setSortingEnabled(true); + ui.dirTreeView->setSortingEnabled(true); - restoreExpandedPathsAndSelection(expanded_indexes,hidden_indexes,selected_indexes) ; + restoreExpandedPathsAndSelection(expanded_indexes,hidden_indexes,selected_indexes) ; #ifdef DEBUG_SHARED_FILES_DIALOG std::cerr << "****** updated directories! Re-enabling sorting ******" << std::endl; @@ -1236,34 +1297,44 @@ void SharedFilesDialog::indicatorChanged(int index) updateDisplay() ; } -void SharedFilesDialog::filterRegExpChanged() +void SharedFilesDialog::onFilterTextEdited() { - QString text = ui.filterPatternLineEdit->text(); + QString text = ui.filterPatternLineEdit->text(); - if (text.isEmpty()) { - ui.filterClearButton->hide(); - } else { - ui.filterClearButton->show(); - } + if (text.isEmpty()) { + ui.filterClearButton->hide(); + } else { + ui.filterClearButton->show(); + } - if (text == lastFilterString) { - ui.filterStartButton->hide(); - } else { - ui.filterStartButton->show(); - } - - //bool valid = false ; - //QColor color ; + if (text == lastFilterString) { + ui.filterStartButton->hide(); + } else { + ui.filterStartButton->show(); + } if(text.length() > 0 && text.length() < 3) { - //valid = false; - ui.filterStartButton->setEnabled(false) ; ui.filterPatternFrame->setToolTip(tr("Search string should be at least 3 characters long.")) ; return ; } + ui.filterStartButton->setEnabled(true) ; + ui.filterPatternFrame->setToolTip(QString()); + + //FilterItems(); +#ifndef DISABLE_SEARCH_WHILE_TYPING + mFilterTimer->start( 500 ); // This will fire filterRegExpChanged after 500 ms. + // If the user types something before it fires, the timer restarts counting +#endif +} + +#ifdef DEPRECATED_CODE +void SharedFilesDialog::filterRegExpChanged() +{ + QString text = ui.filterPatternLineEdit->text(); + if(text.length() > 0 && proxyModel == tree_proxyModel) { std::list result_list ; @@ -1296,17 +1367,8 @@ void SharedFilesDialog::filterRegExpChanged() ui.filterStartButton->setEnabled(true) ; ui.filterPatternFrame->setToolTip(QString()); - - /* unpolish widget to clear the stylesheet's palette cache */ - // ui.filterPatternFrame->style()->unpolish(ui.filterPatternFrame); - - // QPalette palette = ui.filterPatternLineEdit->palette(); - // palette.setColor(ui.filterPatternLineEdit->backgroundRole(), color); - // ui.filterPatternLineEdit->setPalette(palette); - - // //ui.searchLineFrame->setProperty("valid", valid); - // Rshare::refreshStyleSheet(ui.filterPatternFrame, false); } +#endif /* clear Filter */ void SharedFilesDialog::clearFilter() @@ -1320,10 +1382,10 @@ void SharedFilesDialog::clearFilter() /* clear Filter */ void SharedFilesDialog::startFilter() { - ui.filterStartButton->hide(); - lastFilterString = ui.filterPatternLineEdit->text(); + ui.filterStartButton->hide(); + lastFilterString = ui.filterPatternLineEdit->text(); - FilterItems(); + FilterItems(); } void SharedFilesDialog::updateDirTreeView() @@ -1341,13 +1403,14 @@ void SharedFilesDialog::updateDirTreeView() ui.dirTreeView->setToolTip(""); } +//#define DEBUG_SHARED_FILES_DIALOG + +#ifdef DEPRECATED_CODE // This macro make the search expand all items that contain the searched text. // A bug however, makes RS expand everything when nothing is selected, which is a pain. #define EXPAND_WHILE_SEARCHING 1 -//#define DEBUG_SHARED_FILES_DIALOG - void recursMakeVisible(QTreeView *tree,const QSortFilterProxyModel *proxyModel,const QModelIndex& indx,uint32_t depth,const std::vector >& pointers,QList& hidden_list) { #ifdef DEBUG_SHARED_FILES_DIALOG @@ -1403,6 +1466,8 @@ void recursMakeVisible(QTreeView *tree,const QSortFilterProxyModel *proxyModel,c void SharedFilesDialog::restoreInvisibleItems() { + std::cerr << "Restoring " << mHiddenIndexes.size() << " invisible indexes" << std::endl; + for(QList::const_iterator it(mHiddenIndexes.begin());it!=mHiddenIndexes.end();++it) { QModelIndex indx = proxyModel->mapFromSource(*it); @@ -1413,6 +1478,7 @@ void SharedFilesDialog::restoreInvisibleItems() mHiddenIndexes.clear(); } +#endif class QCursorContextBlocker { @@ -1445,115 +1511,60 @@ void SharedFilesDialog::FilterItems() if(mLastFilterText == text) // do not filter again if we already did. This is an optimization { +#ifdef DEBUG_SHARED_FILES_DIALOG std::cerr << "Last text is equal to text. skipping" << std::endl; +#endif return ; } - std::cerr << "New last text. Performing the filter" << std::endl; +#ifdef DEBUG_SHARED_FILES_DIALOG + std::cerr << "New last text. Performing the filter on string \"" << text.toStdString() << "\"" << std::endl; +#endif mLastFilterText = text ; - model->update() ; - restoreInvisibleItems(); QCursorContextBlocker q(ui.dirTreeView) ; - if(proxyModel == tree_proxyModel) + QCoreApplication::processEvents() ; + + std::list result_list ; + uint32_t found = 0 ; + + if(text == "") { - QCoreApplication::processEvents() ; - - std::list keywords ; - std::list result_list ; - - if(text == "") - return ; - - if(text.length() < 3) - return ; - - FileSearchFlags flags = isRemote()?RS_FILE_HINTS_REMOTE:RS_FILE_HINTS_LOCAL; - QStringList lst = text.split(" ",QString::SkipEmptyParts) ; - - for(auto it(lst.begin());it!=lst.end();++it) - keywords.push_back((*it).toStdString()); - - if(keywords.size() > 1) - { - RsRegularExpression::NameExpression exp(RsRegularExpression::ContainsAllStrings,keywords,true); - rsFiles->SearchBoolExp(&exp,result_list, flags) ; - } - else - rsFiles->SearchKeywords(keywords,result_list, flags) ; - -#ifdef DEBUG_SHARED_FILES_DIALOG - std::cerr << "Found " << result_list.size() << " results" << std::endl; -#endif - - if(result_list.size() > MAX_SEARCH_RESULTS) - return ; -#ifdef DEBUG_SHARED_FILES_DIALOG - std::cerr << "Found this result: " << std::endl; -#endif - std::vector > pointers(2,std::set()); // at least two levels need to be here. - - // Then show only the ones we need - for(auto it(result_list.begin());it!=result_list.end();++it) - { -#ifdef DEBUG_SHARED_FILES_DIALOG - std::cerr << (void*)(*it).ref << " parents: " ; -#endif - - DirDetails& det(*it) ; - void *p = NULL; - std::list lst ; - - lst.push_back(det.ref) ; - - while(det.type == DIR_TYPE_FILE || det.type == DIR_TYPE_DIR) - { - p = det.parent ; - rsFiles->RequestDirDetails( p, det, flags); - -#ifdef DEBUG_SHARED_FILES_DIALOG - std::cerr << " " << (void*)p << "(" << (int)det.type << ")"; -#endif - - lst.push_front(p) ; - } - -#ifdef DEBUG_SHARED_FILES_DIALOG - std::cerr << std::endl; -#endif - - uint32_t u=0; - for(auto it2(lst.begin());it2!=lst.end();++it2,++u) - { - if(pointers.size() <= u) - pointers.resize(u+5) ; - - pointers[u].insert(*it2) ; - } - } - - int rowCount = ui.dirTreeView->model()->rowCount(); - for (int row = 0; row < rowCount; ++row) - recursMakeVisible(ui.dirTreeView,proxyModel,ui.dirTreeView->model()->index(row, COLUMN_NAME),0,pointers,mHiddenIndexes); + model->filterItems(std::list(),found) ; + model->update() ; + return ; } + + if(text.length() < 3) + return ; + + FileSearchFlags flags = isRemote()?RS_FILE_HINTS_REMOTE:RS_FILE_HINTS_LOCAL; + QStringList lst = text.split(" ",QString::SkipEmptyParts) ; + std::list keywords ; + + for(auto it(lst.begin());it!=lst.end();++it) + keywords.push_back((*it).toStdString()); + + model->filterItems(keywords,found) ; + model->update() ; + + if(found > 0) + expandAll(); + + if(found == 0) + ui.filterPatternFrame->setToolTip(tr("No result.")) ; + else if(found > MAX_SEARCH_RESULTS) + ui.filterPatternFrame->setToolTip(tr("More than %1 results. Add more/longer search words to select less.").arg(MAX_SEARCH_RESULTS)) ; else - { - int rowCount = ui.dirTreeView->model()->rowCount(); - for (int row = 0; row < rowCount; ++row) - flat_FilterItem(ui.dirTreeView->model()->index(row, COLUMN_NAME), text, 0); - } + ui.filterPatternFrame->setToolTip(tr("Found %1 results.").arg(found)) ; -#ifdef DEPRECATED_CODE - int rowCount = ui.dirTreeView->model()->rowCount(); - for (int row = 0; row < rowCount; ++row) - if(proxyModel == tree_proxyModel) - tree_FilterItem(ui.dirTreeView->model()->index(row, COLUMN_NAME), text, 0); - else - flat_FilterItem(ui.dirTreeView->model()->index(row, COLUMN_NAME), text, 0); +#ifdef DEBUG_SHARED_FILES_DIALOG + std::cerr << found << " results found by search." << std::endl; #endif } +#ifdef DEPRECATED_CODE bool SharedFilesDialog::flat_FilterItem(const QModelIndex &index, const QString &text, int /*level*/) { if(index.data(RetroshareDirModel::FileNameRole).toString().contains(text, Qt::CaseInsensitive)) @@ -1603,4 +1614,4 @@ bool SharedFilesDialog::tree_FilterItem(const QModelIndex &index, const QString return (visible || visibleChildCount); } - +#endif diff --git a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h index 6c48ea4c0..0d35260ee 100644 --- a/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h +++ b/retroshare-gui/src/gui/FileTransfer/SharedFilesDialog.h @@ -22,10 +22,13 @@ #ifndef _SHAREDFILESDIALOG_H #define _SHAREDFILESDIALOG_H -#include +#include "ui_SharedFilesDialog.h" + #include "RsAutoUpdatePage.h" #include "gui/RetroShareLink.h" -#include "ui_SharedFilesDialog.h" +#include "util/RsProtectedTimer.h" + +#include class RetroshareDirModel; class QSortFilterProxyModel; @@ -73,7 +76,8 @@ private slots: void indicatorChanged(int index); - void filterRegExpChanged(); + void onFilterTextEdited(); + //void filterRegExpChanged(); void clearFilter(); void startFilter(); @@ -93,6 +97,8 @@ protected: void recursSaveExpandedItems(const QModelIndex& index, const std::string &path, std::set &exp,std::set& vis, std::set& sel); void saveExpandedPathsAndSelection(std::set& paths,std::set& visible_indexes, std::set& selected_indexes) ; void restoreExpandedPathsAndSelection(const std::set& paths,const std::set& visible_indexes, const std::set& selected_indexes) ; + void recursExpandAll(const QModelIndex& index); + void expandAll(); protected: //now context menu are created again every time theu are called ( in some @@ -109,8 +115,6 @@ protected: bool tree_FilterItem(const QModelIndex &index, const QString &text, int level); bool flat_FilterItem(const QModelIndex &index, const QString &text, int level); - void restoreInvisibleItems(); - QModelIndexList getSelected(); /** Defines the actions for the context menu for QTreeWidget */ @@ -137,8 +141,7 @@ protected: QString lastFilterString; QString mLastFilterText ; - - QList mHiddenIndexes; + RsProtectedTimer* mFilterTimer; }; class LocalSharedFilesDialog : public SharedFilesDialog @@ -195,7 +198,7 @@ class RemoteSharedFilesDialog : public SharedFilesDialog private slots: void downloadRemoteSelected(); void downloadRemoteSelectedInteractive(); - void expanded(const QModelIndex& indx); + void expanded(const QModelIndex& indx); }; #endif diff --git a/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp b/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp index 5a3f2a8ef..f1eb7f3f2 100644 --- a/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp +++ b/retroshare-gui/src/gui/FileTransfer/xprogressbar.cpp @@ -233,7 +233,9 @@ void xProgressBar::paint() uint32_t ss = _pinfo.nb_chunks ; - if(ss > 1) // for small files we use a more progressive display + QRect bounding = painter->boundingRect(rect, Qt::AlignCenter, QLocale().toString(_pinfo.progress, 'f', 2) + "%"); + + if((ss > 1) && (rect.width() > (1.5*bounding.width()))) // for small files we use a more progressive display { if(!_pinfo.cmap._map.empty()) { @@ -303,7 +305,7 @@ void xProgressBar::paint() overPaintSelectedChunks( _pinfo.chunks_in_progress , QColor(170, 20,9), QColor(223,121,123), width,ss) ; overPaintSelectedChunks( _pinfo.chunks_in_checking , QColor(186,143,0), QColor(223,196, 61), width,ss) ; } - else + else if ((rect.width() < bounding.width()) || !displayText) { // calculate progress value int preWidth = static_cast((rect.width() - 1 - hSpan)*(_pinfo.progress/100.0f)); @@ -318,11 +320,20 @@ void xProgressBar::paint() // paint text? - if (displayText) + if (displayText && (rect.width() >= bounding.width())) { - QLocale locale; + QColor colorInt (255-textColor.red(), 255-textColor.green(), 255-textColor.blue(), 127); + QColor colorBor (255-textColor.red(), 255-textColor.green(), 255-textColor.blue(), 63); + QRadialGradient radialGrad(bounding.x()+(bounding.width()/2), bounding.y()+(bounding.height()/2),bounding.width()/2); + radialGrad.setColorAt(0.0, colorInt); + radialGrad.setColorAt(1.0, colorBor); + radialGrad.setSpread(QGradient::ReflectSpread); + painter->setPen(colorBor); + painter->setBrush(radialGrad); + painter->drawRoundedRect(bounding.adjusted(-2,2,2,-3),4.0,4.0); + painter->setPen(textColor); - painter->drawText(rect, Qt::AlignCenter, locale.toString(_pinfo.progress, 'f', 2) + "%"); + painter->drawText(rect, Qt::AlignCenter, QLocale().toString(_pinfo.progress, 'f', 2) + "%"); } backgroundColor.setRgb(255, 255, 255); diff --git a/retroshare-gui/src/gui/Identity/IdDialog.cpp b/retroshare-gui/src/gui/Identity/IdDialog.cpp index 71bf7be40..5c9bce46b 100644 --- a/retroshare-gui/src/gui/Identity/IdDialog.cpp +++ b/retroshare-gui/src/gui/Identity/IdDialog.cpp @@ -1994,7 +1994,8 @@ QString IdDialog::createUsageString(const RsIdentityUsage& u) const } case RsIdentityUsage::CHAT_LOBBY_MSG_VALIDATION: // Chat lobby msgs are signed, so each time one comes, or a chat lobby event comes, a signature verificaiton happens. { - RetroShareLink l = RetroShareLink::createChatRoom(ChatId(ChatLobbyId(u.mAdditionalId)),QString::number(u.mAdditionalId)); + ChatId id = ChatId(ChatLobbyId(u.mAdditionalId)); + RetroShareLink l = RetroShareLink::createChatRoom(id, QString::fromStdString(id.toStdString())); return tr("Message in chat room %1").arg(l.toHtml()) ; } case RsIdentityUsage::GLOBAL_ROUTER_SIGNATURE_CHECK: // Global router message validation @@ -2124,19 +2125,19 @@ void IdDialog::updateDisplay(bool complete) if (complete) { /* Fill complete */ requestIdList(); - requestIdDetails(); + //requestIdDetails(); requestRepList(); return; } requestCircleGroupMeta(); - std::list grpIds; + std::set grpIds; getAllGrpIds(grpIds); if (!getGrpIds().empty()) { requestIdList(); - if (!mId.isNull() && std::find(grpIds.begin(), grpIds.end(), mId) != grpIds.end()) { + if (!mId.isNull() && grpIds.find(mId)!=grpIds.end()) { requestIdDetails(); requestRepList(); } diff --git a/retroshare-gui/src/gui/MainWindow.cpp b/retroshare-gui/src/gui/MainWindow.cpp index 6219575b5..e00417080 100644 --- a/retroshare-gui/src/gui/MainWindow.cpp +++ b/retroshare-gui/src/gui/MainWindow.cpp @@ -107,10 +107,13 @@ #include "gui/common/RsCollection.h" #include "settings/rsettingswin.h" #include "settings/rsharesettings.h" -#include "settings/WebuiPage.h" #include "common/StatusDefs.h" #include "gui/notifyqt.h" +#ifdef ENABLE_WEBUI +# include "settings/WebuiPage.h" +#endif + #include #include @@ -730,16 +733,16 @@ void MainWindow::updateStatus() if (ratesstatus) ratesstatus->getRatesStatus(downKb, upKb); - if(torstatus) - torstatus->getTorStatus(); + if(torstatus) + torstatus->getTorStatus(); if(!hiddenmode) { - if (natstatus) - natstatus->getNATStatus(); - - if (dhtstatus) - dhtstatus->getDHTStatus(); + if (natstatus) + natstatus->getNATStatus(); + + if (dhtstatus) + dhtstatus->getDHTStatus(); } if (discstatus) { @@ -1431,27 +1434,37 @@ void MainWindow::settingsChanged() void MainWindow::externalLinkActivated(const QUrl &url) { static bool already_warned = false ; + bool never_ask_me = Settings->value("NeverAskMeForExternalLinkActivated",false).toBool(); - if(!already_warned) + if(!already_warned && !never_ask_me) { QMessageBox mb(QObject::tr("Confirmation"), QObject::tr("Do you want this link to be handled by your system?")+"

"+ url.toString()+"

"+tr("Make sure this link has not been forged to drag you to a malicious website."), QMessageBox::Question, QMessageBox::Yes,QMessageBox::No, 0); - QCheckBox *checkbox = new QCheckBox(tr("Don't ask me again")) ; + QCheckBox *dontAsk_CB = new QCheckBox(tr("Don't ask me again")); + QCheckBox *neverAsk_CB = new QCheckBox(tr("Never ask me again")); + dontAsk_CB->setToolTip(tr("This will be saved only for this session.")); + neverAsk_CB->setToolTip(tr("This will be saved permanently. You'll need to clean RetroShare.conf to revert.")); QGridLayout* layout = qobject_cast(mb.layout()); if (layout) { - layout->addWidget(checkbox,layout->rowCount(),0,1, layout->columnCount(), Qt::AlignLeft); + layout->addWidget(dontAsk_CB,layout->rowCount(),0,1, layout->columnCount(), Qt::AlignLeft); + layout->addWidget(neverAsk_CB,layout->rowCount(),0,1, layout->columnCount(), Qt::AlignLeft); } else { //Not QGridLayout so add at end - mb.layout()->addWidget(checkbox) ; + mb.layout()->addWidget(dontAsk_CB); + mb.layout()->addWidget(neverAsk_CB); } int res = mb.exec() ; if (res == QMessageBox::No) return ; - else if(checkbox->isChecked()) + + if(dontAsk_CB->isChecked()) already_warned = true ; + + if(neverAsk_CB->isChecked()) + Settings->setValue("NeverAskMeForExternalLinkActivated",true); } QDesktopServices::openUrl(url) ; diff --git a/retroshare-gui/src/gui/NewsFeed.cpp b/retroshare-gui/src/gui/NewsFeed.cpp index 45c8c1cb6..a9c599389 100644 --- a/retroshare-gui/src/gui/NewsFeed.cpp +++ b/retroshare-gui/src/gui/NewsFeed.cpp @@ -366,8 +366,8 @@ void NewsFeed::updateDisplay() opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; GxsMsgReq msgIds; - std::vector &vect_msgIds = msgIds[grpId]; - vect_msgIds.push_back(msgId); + std::set &vect_msgIds = msgIds[grpId]; + vect_msgIds.insert(msgId); uint32_t token; mTokenQueueCircle->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, TOKEN_TYPE_MESSAGE); diff --git a/retroshare-gui/src/gui/Posted/PostedItem.cpp b/retroshare-gui/src/gui/Posted/PostedItem.cpp index 7d70b3e15..8fc1cc686 100644 --- a/retroshare-gui/src/gui/Posted/PostedItem.cpp +++ b/retroshare-gui/src/gui/Posted/PostedItem.cpp @@ -22,6 +22,7 @@ */ #include +#include #include "rshare.h" #include "PostedItem.h" diff --git a/retroshare-gui/src/gui/RemoteDirModel.cpp b/retroshare-gui/src/gui/RemoteDirModel.cpp index 94cf7c6bf..22fb0af5b 100644 --- a/retroshare-gui/src/gui/RemoteDirModel.cpp +++ b/retroshare-gui/src/gui/RemoteDirModel.cpp @@ -30,6 +30,7 @@ #include "retroshare/rsfiles.h" #include "retroshare/rspeers.h" #include "util/misc.h" +#include "retroshare/rsexpr.h" #include #include @@ -43,6 +44,7 @@ /***** * #define RDM_DEBUG + * #define RDM_SEARCH_DEBUG ****/ static const uint32_t FLAT_VIEW_MAX_REFS_PER_SECOND = 2000 ; @@ -50,21 +52,40 @@ static const size_t FLAT_VIEW_MAX_REFS_TABLE_SIZE = 10000 ; // static const uint32_t FLAT_VIEW_MIN_DELAY_BETWEEN_UPDATES = 120 ; // dont rebuild ref list more than every 2 mins. RetroshareDirModel::RetroshareDirModel(bool mode, QObject *parent) - : QAbstractItemModel(parent), - ageIndicator(IND_ALWAYS), - RemoteMode(mode), nIndex(1), indexSet(1) /* ass zero index cant be used */ + : QAbstractItemModel(parent), _visible(false) + , ageIndicator(IND_ALWAYS) + , RemoteMode(mode)//, nIndex(1), indexSet(1) /* ass zero index cant be used */ + , mLastRemote(false), mLastReq(0), mUpdating(false) { - _visible = false ; #if QT_VERSION < QT_VERSION_CHECK (5, 0, 0) setSupportedDragActions(Qt::CopyAction); #endif treeStyle(); - mDirDetails.ref = (void*)intptr_t(0xffffffff) ; - mLastRemote = false ; - mUpdating = false; + mDirDetails.ref = (void*)intptr_t(0xffffffff) ; } +TreeStyle_RDM::TreeStyle_RDM(bool mode) + : RetroshareDirModel(mode), _showEmpty(false) +{ + _showEmptyAct = new QAction(QIcon(), tr("Show Empty"), this); + _showEmptyAct->setCheckable(true); + _showEmptyAct->setChecked(_showEmpty); + connect(_showEmptyAct, SIGNAL(toggled(bool)), this, SLOT(showEmpty(bool))); +} + +FlatStyle_RDM::FlatStyle_RDM(bool mode) + : RetroshareDirModel(mode), _ref_mutex("Flat file list") +{ + _needs_update = true ; + + { + RS_STACK_MUTEX(_ref_mutex) ; + _last_update = 0 ; + } +} + + // QAbstractItemModel::setSupportedDragActions() was replaced by virtual QAbstractItemModel::supportedDragActions() #if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0) Qt::DropActions RetroshareDirModel::supportedDragActions() const @@ -78,7 +99,6 @@ static bool isNewerThanEpoque(uint32_t ts) return ts > 0 ; // this should be conservative enough } - void RetroshareDirModel::treeStyle() { categoryIcon.addPixmap(QPixmap(":/images/folder16.png"), @@ -97,6 +117,21 @@ void TreeStyle_RDM::updateRef(const QModelIndex& indx) const rsFiles->requestDirUpdate(indx.internalPointer()) ; } +QMenu* TreeStyle_RDM::getContextMenu(QMenu* contextMenu) +{ + if (!contextMenu){ + contextMenu = new QMenu(); + } else { + if (RemoteMode) + contextMenu->addSeparator(); + } + + if (RemoteMode) + contextMenu->addAction( _showEmptyAct) ; + + return contextMenu; +} + bool TreeStyle_RDM::hasChildren(const QModelIndex &parent) const { @@ -137,7 +172,7 @@ bool TreeStyle_RDM::hasChildren(const QModelIndex &parent) const } /* PERSON/DIR*/ #ifdef RDM_DEBUG - std::cerr << "lookup PER/DIR #" << details->count; + std::cerr << "lookup PER/DIR #" << details.count; std::cerr << std::endl; #endif return (details.count > 0); /* do we have children? */ @@ -171,9 +206,13 @@ int TreeStyle_RDM::rowCount(const QModelIndex &parent) const void *ref = (parent.isValid())? parent.internalPointer() : NULL ; - DirDetails details ; + if ((!ref) && RemoteMode) + _parentRow.clear(); //Only clear it when asking root child number and in remote mode. - if (! requestDirDetails(ref, RemoteMode,details)) + + DirDetails details ; + + if (! requestDirDetails(ref, RemoteMode,details)) { #ifdef RDM_DEBUG std::cerr << "lookup failed -> 0"; @@ -181,7 +220,7 @@ int TreeStyle_RDM::rowCount(const QModelIndex &parent) const #endif return 0; } - if (details.type == DIR_TYPE_FILE) + if (details.type == DIR_TYPE_FILE) { #ifdef RDM_DEBUG std::cerr << "lookup FILE: 0"; @@ -192,12 +231,24 @@ int TreeStyle_RDM::rowCount(const QModelIndex &parent) const /* else PERSON/DIR*/ #ifdef RDM_DEBUG - std::cerr << "lookup PER/DIR #" << details->count; + std::cerr << "lookup PER/DIR #" << details.count; std::cerr << std::endl; #endif - return details.count; + if ((details.type == DIR_TYPE_ROOT) && !_showEmpty && RemoteMode) + { + DirDetails childDetails; + //Scan all children to know if they are empty. + //And save their real row index + //Prefer do like that than modify requestDirDetails with a new flag (rsFiles->RequestDirDetails) + for(uint64_t i = 0; i < details.count; ++i) + { + if (requestDirDetails(details.children[i].ref, RemoteMode,childDetails) && (childDetails.count > 0)) + _parentRow.push_back(i); + } + return _parentRow.size(); + } + return details.count; } - int FlatStyle_RDM::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); @@ -210,6 +261,7 @@ int FlatStyle_RDM::rowCount(const QModelIndex &parent) const return _ref_entries.size() ; } + int TreeStyle_RDM::columnCount(const QModelIndex &/*parent*/) const { return COLUMN_COUNT; @@ -218,6 +270,7 @@ int FlatStyle_RDM::columnCount(const QModelIndex &/*parent*/) const { return COLUMN_COUNT; } + QString RetroshareDirModel::getFlagsString(FileStorageFlags flags) { char str[11] = "- - -" ; @@ -253,7 +306,6 @@ QString RetroshareDirModel::getGroupsString(FileStorageFlags flags,const std::li return groups_str ; } - QString RetroshareDirModel::getAgeIndicatorString(const DirDetails &details) const { QString ret(""); @@ -317,11 +369,19 @@ const QIcon& RetroshareDirModel::getFlagsIcon(FileStorageFlags flags) static_icons[n] = new QIcon(pix); - std::cerr << "Generated icon for flags " << std::hex << flags << std::endl; + std::cerr << "Generated icon for flags " << std::hex << flags << std::dec << std::endl; } return *static_icons[n] ; } +QVariant RetroshareDirModel::filterRole(const DirDetails& details, int /*coln*/) const +{ + if(mFilteredPointers.empty() || mFilteredPointers.find(details.ref) != mFilteredPointers.end()) + return QString(RETROSHARE_DIR_MODEL_FILTER_STRING); + else + return QVariant(); +} + QVariant RetroshareDirModel::decorationRole(const DirDetails& details,int coln) const { if(coln == COLUMN_FRIEND_ACCESS) @@ -496,17 +556,6 @@ QVariant TreeStyle_RDM::displayRole(const DirDetails& details,int coln) const return QVariant(); } /* end of DisplayRole */ -FlatStyle_RDM::FlatStyle_RDM(bool mode) - : RetroshareDirModel(mode), _ref_mutex("Flat file list") -{ - _needs_update = true ; - - { - RS_STACK_MUTEX(_ref_mutex) ; - _last_update = 0 ; - } -} - void FlatStyle_RDM::update() { if(_needs_update) @@ -650,7 +699,6 @@ QVariant TreeStyle_RDM::sortRole(const QModelIndex& /*index*/,const DirDetails& } return QVariant(); } - QVariant FlatStyle_RDM::sortRole(const QModelIndex& /*index*/,const DirDetails& details,int coln) const { /* @@ -678,9 +726,6 @@ QVariant FlatStyle_RDM::sortRole(const QModelIndex& /*index*/,const DirDetails& return QVariant(); } /* end of SortRole */ - - - QVariant RetroshareDirModel::data(const QModelIndex &index, int role) const { #ifdef RDM_DEBUG @@ -768,9 +813,15 @@ QVariant RetroshareDirModel::data(const QModelIndex &index, int role) const if (role == SortRole) return sortRole(index,details,coln) ; + if (role == FilterRole) + return filterRole(details,coln) ; + return QVariant(); } + + +/**** //void RetroshareDirModel::getAgeIndicatorRec(const DirDetails &details, QString &ret) const { // if (details.type == DIR_TYPE_FILE) { // ret = getAgeIndicatorString(details); @@ -786,6 +837,7 @@ QVariant RetroshareDirModel::data(const QModelIndex &index, int role) const // } // } //} +****/ QVariant TreeStyle_RDM::headerData(int section, Qt::Orientation orientation, int role) const { @@ -895,8 +947,8 @@ QModelIndex TreeStyle_RDM::index(int row, int column, const QModelIndex & parent std::cerr << ": row:" << row << " col:" << column << " "; #endif - // This function is used extensively. There's no way we can use requestDirDetails() in it, which would - // cause far too much overhead. So we use a dedicated function that only grabs the required information. + // This function is used extensively. There's no way we can use requestDirDetails() in it, which would + // cause far too much overhead. So we use a dedicated function that only grabs the required information. if(row < 0) return QModelIndex() ; @@ -910,15 +962,19 @@ QModelIndex TreeStyle_RDM::index(int row, int column, const QModelIndex & parent } ********/ + //If on root and don't show empty child, get real parent row + if ((!ref) && (!_showEmpty) && RemoteMode && ((size_t)row >= _parentRow.size())) + return QModelIndex(); - void *result ; + int parentRow = ((!ref) && (!_showEmpty) && RemoteMode) ? _parentRow[row] : row ; - if(rsFiles->findChildPointer(ref, row, result, ((RemoteMode) ? RS_FILE_HINTS_REMOTE : RS_FILE_HINTS_LOCAL))) - return createIndex(row, column, result) ; - else + void *result ; + + if(rsFiles->findChildPointer(ref, parentRow, result, ((RemoteMode) ? RS_FILE_HINTS_REMOTE : RS_FILE_HINTS_LOCAL))) + return createIndex(row, column, result) ; + else return QModelIndex(); } - QModelIndex FlatStyle_RDM::index(int row, int column, const QModelIndex & parent) const { Q_UNUSED(parent); @@ -964,10 +1020,10 @@ QModelIndex TreeStyle_RDM::parent( const QModelIndex & index ) const } void *ref = index.internalPointer(); - DirDetails details ; + DirDetails details ; - if (! requestDirDetails(ref, RemoteMode,details)) - { + if (! requestDirDetails(ref, RemoteMode,details)) + { #ifdef RDM_DEBUG std::cerr << "Failed Lookup -> invalid"; std::cerr << std::endl; @@ -975,7 +1031,7 @@ QModelIndex TreeStyle_RDM::parent( const QModelIndex & index ) const return QModelIndex(); } - if (!(details.parent)) + if (!(details.parent)) { #ifdef RDM_DEBUG std::cerr << "success. parent is Root/NULL --> invalid"; @@ -985,12 +1041,13 @@ QModelIndex TreeStyle_RDM::parent( const QModelIndex & index ) const } #ifdef RDM_DEBUG - std::cerr << "success index(" << details->prow << ",0," << details->parent << ")"; + std::cerr << "success index(" << details.prow << ",0," << details.parent << ")"; std::cerr << std::endl; - std::cerr << "Creating index 3 row=" << details.prow << ", column=" << 0 << ", ref=" << (void*)details.parent << std::endl; + std::cerr << "Creating index 3 row=" << details.prow << ", column=" << 0 << ", ref=" << (void*)details.parent << std::endl; #endif - return createIndex(details.prow, COLUMN_NAME, details.parent); + + return createIndex(details.prow, COLUMN_NAME, details.parent); } QModelIndex FlatStyle_RDM::parent( const QModelIndex & index ) const { @@ -1039,7 +1096,7 @@ Qt::ItemFlags RetroshareDirModel::flags( const QModelIndex & index ) const -/* Callback from */ +/* Callback from Core*/ void RetroshareDirModel::preMods() { emit layoutAboutToBeChanged(); @@ -1056,7 +1113,7 @@ void RetroshareDirModel::preMods() #endif } -/* Callback from */ +/* Callback from Core*/ void RetroshareDirModel::postMods() { // emit layoutAboutToBeChanged(); @@ -1075,6 +1132,30 @@ void RetroshareDirModel::postMods() emit layoutChanged(); } +void FlatStyle_RDM::postMods() +{ + time_t now = time(NULL); + + if(_last_update + FLAT_VIEW_MIN_DELAY_BETWEEN_UPDATES > now) + return ; + + if(visible()) + { + emit layoutAboutToBeChanged(); + + { + RS_STACK_MUTEX(_ref_mutex) ; + _ref_stack.clear() ; + _ref_stack.push_back(NULL) ; // init the stack with the topmost parent directory + _ref_entries.clear(); + _last_update = now; + } + QTimer::singleShot(100,this,SLOT(updateRefs())) ; + } + else + _needs_update = true ; +} + bool RetroshareDirModel::requestDirDetails(void *ref, bool remote,DirDetails& d) const { #ifdef RDM_DEBUG @@ -1273,10 +1354,10 @@ void RetroshareDirModel::getFileInfoFromIndexList(const QModelIndexList& list, s #ifdef RDM_DEBUG std::cerr << "::::::::::::FileRecommend:::: " << std::endl; - std::cerr << "Name: " << details->name << std::endl; - std::cerr << "Hash: " << details->hash << std::endl; - std::cerr << "Size: " << details->count << std::endl; - std::cerr << "Path: " << details->path << std::endl; + std::cerr << "Name: " << details.name << std::endl; + std::cerr << "Hash: " << details.hash << std::endl; + std::cerr << "Size: " << details.count << std::endl; + std::cerr << "Path: " << details.path << std::endl; #endif // Note: for directories, the returned hash, is the peer id, so if we collect // dirs, we need to be a bit more conservative for the @@ -1383,6 +1464,72 @@ void RetroshareDirModel::getFilePaths(const QModelIndexList &list, std::list& keywords,uint32_t& found) +{ + FileSearchFlags flags = RemoteMode?RS_FILE_HINTS_REMOTE:RS_FILE_HINTS_LOCAL; + + std::list result_list ; + found = 0 ; + + if(keywords.empty()) + { + mFilteredPointers.clear(); + return ; + } + else if(keywords.size() > 1) + { + RsRegularExpression::NameExpression exp(RsRegularExpression::ContainsAllStrings,keywords,true); + rsFiles->SearchBoolExp(&exp,result_list, flags) ; + } + else + rsFiles->SearchKeywords(keywords,result_list, flags) ; + +#ifdef RDM_SEARCH_DEBUG + std::cerr << "Found " << result_list.size() << " results" << std::endl; +#endif + + if(result_list.empty()) // in this case we dont clear the list of filtered items, so that we can keep the old filter list + return ; + + mFilteredPointers.clear(); + +#ifdef RDM_SEARCH_DEBUG + std::cerr << "Found this result: " << std::endl; +#endif + + // Then show only the ones we need + + for(auto it(result_list.begin());it!=result_list.end();++it) + { + DirDetails& det(*it) ; +#ifdef RDM_SEARCH_DEBUG + std::cerr << (void*)(*it).ref << " name=\"" << det.name << "\" parents: " ; +#endif + void *p = det.ref ; + mFilteredPointers.insert(p) ; + ++found ; + + while(det.type == DIR_TYPE_FILE || det.type == DIR_TYPE_DIR) + { + p = det.parent ; + rsFiles->RequestDirDetails( p, det, flags); + +#ifdef RDM_SEARCH_DEBUG + std::cerr << " " << (void*)p << "(" << (int)det.type << ")"; +#endif + mFilteredPointers.insert(p) ; + } + +#ifdef RDM_SEARCH_DEBUG + std::cerr << std::endl; +#endif + } +#ifdef RDM_SEARCH_DEBUG + std::cerr << mFilteredPointers.size() << " pointers in filter set." << std::endl; +#endif +} + + /* Drag and Drop Functionality */ QMimeData * RetroshareDirModel::mimeData ( const QModelIndexList & indexes ) const { @@ -1402,10 +1549,10 @@ QMimeData * RetroshareDirModel::mimeData ( const QModelIndexList & indexes ) con #ifdef RDM_DEBUG std::cerr << "::::::::::::FileDrag:::: " << std::endl; - std::cerr << "Name: " << details->name << std::endl; - std::cerr << "Hash: " << details->hash << std::endl; - std::cerr << "Size: " << details->count << std::endl; - std::cerr << "Path: " << details->path << std::endl; + std::cerr << "Name: " << details.name << std::endl; + std::cerr << "Hash: " << details.hash << std::endl; + std::cerr << "Size: " << details.count << std::endl; + std::cerr << "Path: " << details.path << std::endl; #endif if (details.type != DIR_TYPE_FILE) @@ -1475,35 +1622,11 @@ int RetroshareDirModel::getType ( const QModelIndex & index ) const return rsFiles->getType(index.internalPointer(),flags); } -FlatStyle_RDM::~FlatStyle_RDM() -{ -} - TreeStyle_RDM::~TreeStyle_RDM() { } -void FlatStyle_RDM::postMods() +FlatStyle_RDM::~FlatStyle_RDM() { - time_t now = time(NULL); - - if(_last_update + FLAT_VIEW_MIN_DELAY_BETWEEN_UPDATES > now) - return ; - - if(visible()) - { - emit layoutAboutToBeChanged(); - - { - RS_STACK_MUTEX(_ref_mutex) ; - _ref_stack.clear() ; - _ref_stack.push_back(NULL) ; // init the stack with the topmost parent directory - _ref_entries.clear(); - _last_update = now; - } - QTimer::singleShot(100,this,SLOT(updateRefs())) ; - } - else - _needs_update = true ; } void FlatStyle_RDM::updateRefs() @@ -1563,3 +1686,8 @@ void FlatStyle_RDM::updateRefs() RetroshareDirModel::postMods() ; } +void TreeStyle_RDM::showEmpty(const bool value) +{ + _showEmpty = value; + update(); +} diff --git a/retroshare-gui/src/gui/RemoteDirModel.h b/retroshare-gui/src/gui/RemoteDirModel.h index fb0272ae3..b7ff971dc 100644 --- a/retroshare-gui/src/gui/RemoteDirModel.h +++ b/retroshare-gui/src/gui/RemoteDirModel.h @@ -25,7 +25,9 @@ #include #include +#include #include +#include #include #include @@ -37,6 +39,7 @@ #define COLUMN_FRIEND_ACCESS 4 #define COLUMN_WN_VISU_DIR 5 #define COLUMN_COUNT 6 +#define RETROSHARE_DIR_MODEL_FILTER_STRING "filtered" class DirDetails; @@ -58,12 +61,12 @@ class RetroshareDirModel : public QAbstractItemModel Q_OBJECT public: - enum Roles{ FileNameRole = Qt::UserRole+1, SortRole = Qt::UserRole+2 }; + enum Roles{ FileNameRole = Qt::UserRole+1, SortRole = Qt::UserRole+2, FilterRole = Qt::UserRole+3 }; RetroshareDirModel(bool mode, QObject *parent = 0); virtual ~RetroshareDirModel() {} - Qt::ItemFlags flags ( const QModelIndex & index ) const; + virtual Qt::ItemFlags flags ( const QModelIndex & index ) const; /* Callback from Core */ virtual void preMods(); @@ -82,18 +85,22 @@ class RetroshareDirModel : public QAbstractItemModel void getFileInfoFromIndexList(const QModelIndexList& list, std::list& files_info) ; void openSelected(const QModelIndexList &list); void getFilePaths(const QModelIndexList &list, std::list &fullpaths); - void getFilePath(const QModelIndex& index, std::string& fullpath); - void changeAgeIndicator(uint32_t indicator) { ageIndicator = indicator; } + void getFilePath(const QModelIndex& index, std::string& fullpath); + void changeAgeIndicator(uint32_t indicator) { ageIndicator = indicator; } + + bool requestDirDetails(void *ref, bool remote,DirDetails& d) const; - bool requestDirDetails(void *ref, bool remote,DirDetails& d) const; virtual void update() {} + virtual void updateRef(const QModelIndex&) const =0; - virtual void updateRef(const QModelIndex&) const =0; + virtual QMenu* getContextMenu(QMenu* contextMenu) {return contextMenu;} - public: - virtual QMimeData * mimeData ( const QModelIndexList & indexes ) const; + void filterItems(const std::list& keywords, uint32_t& found) ; + + //Overloaded from QAbstractItemModel + virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; virtual QStringList mimeTypes () const; - virtual QVariant data(const QModelIndex &index, int role) const; + virtual QMimeData * mimeData ( const QModelIndexList & indexes ) const; #if QT_VERSION >= QT_VERSION_CHECK (5, 0, 0) virtual Qt::DropActions supportedDragActions() const; #endif @@ -104,61 +111,62 @@ class RetroshareDirModel : public QAbstractItemModel void treeStyle(); void downloadDirectory(const DirDetails & details, int prefixLen); static QString getFlagsString(FileStorageFlags f) ; - static QString getGroupsString(FileStorageFlags flags, const std::list &) ; + static QString getGroupsString(FileStorageFlags flags, const std::list &) ; QString getAgeIndicatorString(const DirDetails &) const; // void getAgeIndicatorRec(const DirDetails &details, QString &ret) const; - static const QIcon& getFlagsIcon(FileStorageFlags flags) ; + static const QIcon& getFlagsIcon(FileStorageFlags flags) ; virtual QVariant displayRole(const DirDetails&,int) const = 0 ; virtual QVariant sortRole(const QModelIndex&,const DirDetails&,int) const =0; QVariant decorationRole(const DirDetails&,int) const ; + QVariant filterRole(const DirDetails& details,int coln) const; uint32_t ageIndicator; QIcon categoryIcon; QIcon peerIcon; - class RemoteIndex - { - public: - RemoteIndex() {} - RemoteIndex(std::string in_person, - std::string in_path, - int in_idx, - int in_row, - int in_column, - std::string in_name, - int in_size, - int in_type, - int in_ts, int in_rank) - :id(in_person), path(in_path), parent(in_idx), - row(in_row), column(in_column), - name(in_name), size(in_size), - type(in_type), timestamp(in_ts), rank(in_rank) - { - return; - } - - std::string id; - std::string path; - int parent; - int row; - int column; - - /* display info */ - std::string name; - int size; - int type; - int timestamp; - int rank; - - }; + //class RemoteIndex + //{ + // public: + // RemoteIndex() {} + // RemoteIndex(std::string in_person, + // std::string in_path, + // int in_idx, + // int in_row, + // int in_column, + // std::string in_name, + // int in_size, + // int in_type, + // int in_ts, int in_rank) + // :id(in_person), path(in_path), parent(in_idx), + // row(in_row), column(in_column), + // name(in_name), size(in_size), + // type(in_type), timestamp(in_ts), rank(in_rank) + // { + // return; + // } + // + // std::string id; + // std::string path; + // int parent; + // int row; + // int column; + // + // /* display info */ + // std::string name; + // int size; + // int type; + // int timestamp; + // int rank; + // + //}; bool RemoteMode; - mutable int nIndex; - mutable std::vector indexSet; + //mutable int nIndex; + //mutable std::vector indexSet; // This material attempts to keep last request in cache, with no search cost. @@ -167,6 +175,8 @@ class RetroshareDirModel : public QAbstractItemModel mutable time_t mLastReq; bool mUpdating ; + + std::set mFilteredPointers ; }; // This class shows the classical hierarchical directory view of shared files @@ -178,28 +188,36 @@ class TreeStyle_RDM: public RetroshareDirModel Q_OBJECT public: - TreeStyle_RDM(bool mode) - : RetroshareDirModel(mode) - { - } - + TreeStyle_RDM(bool mode); virtual ~TreeStyle_RDM() ; protected: - virtual void updateRef(const QModelIndex&) const ; + + //Overloaded from RetroshareDirModel virtual void update() ; - - /* These are all overloaded Virtual Functions */ - virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; - virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; - - virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + virtual void updateRef(const QModelIndex&) const ; + virtual QMenu* getContextMenu(QMenu* contextMenu) ; virtual QVariant displayRole(const DirDetails&,int) const ; virtual QVariant sortRole(const QModelIndex&,const DirDetails&,int) const ; + //Overloaded from QAbstractItemModel virtual QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex() ) const; virtual QModelIndex parent ( const QModelIndex & index ) const; + + virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; + virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; virtual bool hasChildren(const QModelIndex & parent = QModelIndex()) const; + + virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + + private slots: + void showEmpty(const bool value); + + private: + QAction *_showEmptyAct; + bool _showEmpty; + protected: + mutable std::vector _parentRow ; // used to store the real parent row for non empty child }; // This class shows a flat list of all shared files @@ -212,9 +230,9 @@ class FlatStyle_RDM: public RetroshareDirModel public: FlatStyle_RDM(bool mode); - virtual ~FlatStyle_RDM() ; + //Overloaded from RetroshareDirModel virtual void update() ; bool isMaxRefsTableSize(size_t* maxSize = NULL); @@ -223,27 +241,30 @@ class FlatStyle_RDM: public RetroshareDirModel void updateRefs() ; protected: - virtual void updateRef(const QModelIndex&) const {} - virtual void postMods(); - - virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; - virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; - - virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + //Overloaded from RetroshareDirModel + virtual void postMods();/* Callback from Core */ + virtual void updateRef(const QModelIndex&) const {} virtual QVariant displayRole(const DirDetails&,int) const ; virtual QVariant sortRole(const QModelIndex&,const DirDetails&,int) const ; + //Overloaded from QAbstractItemModel virtual QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex() ) const; virtual QModelIndex parent ( const QModelIndex & index ) const; + + virtual int rowCount(const QModelIndex &parent = QModelIndex()) const; + virtual int columnCount(const QModelIndex &parent = QModelIndex()) const; virtual bool hasChildren(const QModelIndex & parent = QModelIndex()) const; + virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + + QString computeDirectoryPath(const DirDetails& details) const ; - mutable RsMutex _ref_mutex ; - std::vector _ref_entries ;// used to store the refs to display - std::vector _ref_stack ; // used to store the refs to update + mutable RsMutex _ref_mutex ; + std::vector _ref_entries ; // used to store the refs to display + std::vector _ref_stack ; // used to store the refs to update bool _needs_update ; - time_t _last_update ; + time_t _last_update ; }; diff --git a/retroshare-gui/src/gui/WikiPoos/WikiEditDialog.cpp b/retroshare-gui/src/gui/WikiPoos/WikiEditDialog.cpp index 709d0262c..667e55fa7 100644 --- a/retroshare-gui/src/gui/WikiPoos/WikiEditDialog.cpp +++ b/retroshare-gui/src/gui/WikiPoos/WikiEditDialog.cpp @@ -715,7 +715,7 @@ void WikiEditDialog::loadBaseHistory(const uint32_t &token) std::cerr << " ParentId: " << page.mMeta.mParentId; std::cerr << std::endl; - GxsIdRSTreeWidgetItem *modItem = new GxsIdRSTreeWidgetItem(mThreadCompareRole); + GxsIdRSTreeWidgetItem *modItem = new GxsIdRSTreeWidgetItem(mThreadCompareRole, GxsIdDetails::ICON_TYPE_AVATAR); modItem->setData(WET_DATA_COLUMN, WET_ROLE_ORIGPAGEID, QString::fromStdString(page.mMeta.mOrigMsgId.toStdString())); modItem->setData(WET_DATA_COLUMN, WET_ROLE_PAGEID, QString::fromStdString(page.mMeta.mMsgId.toStdString())); @@ -830,7 +830,7 @@ void WikiEditDialog::loadEditTreeData(const uint32_t &token) } /* create an Entry */ - GxsIdRSTreeWidgetItem *modItem = new GxsIdRSTreeWidgetItem(mThreadCompareRole); + GxsIdRSTreeWidgetItem *modItem = new GxsIdRSTreeWidgetItem(mThreadCompareRole, GxsIdDetails::ICON_TYPE_AVATAR); modItem->setData(WET_DATA_COLUMN, WET_ROLE_ORIGPAGEID, QString::fromStdString(snapshot.mMeta.mOrigMsgId.toStdString())); modItem->setData(WET_DATA_COLUMN, WET_ROLE_PAGEID, QString::fromStdString(snapshot.mMeta.mMsgId.toStdString())); modItem->setData(WET_DATA_COLUMN, WET_ROLE_PARENTID, QString::fromStdString(snapshot.mMeta.mParentId.toStdString())); diff --git a/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp b/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp index 918939917..e490af1b8 100644 --- a/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp +++ b/retroshare-gui/src/gui/chat/ChatLobbyDialog.cpp @@ -33,6 +33,7 @@ #include "gui/ChatLobbyWidget.h" #include "gui/FriendsDialog.h" #include "gui/MainWindow.h" +#include "gui/chat/ChatWidget.h" #include "gui/common/html.h" #include "gui/common/FriendSelectionDialog.h" #include "gui/common/RSTreeWidgetItem.h" @@ -97,7 +98,7 @@ ChatLobbyDialog::ChatLobbyDialog(const ChatLobbyId& lid, QWidget *parent, Qt::Wi votePositiveAct = new QAction(QIcon(":/icons/png/thumbs-up.png"), tr("Give positive opinion"), this); distantChatAct = new QAction(QIcon(":/images/chat_24.png"), tr("Start private chat"), this); sendMessageAct = new QAction(QIcon(":/images/mail_new.png"), tr("Send Message"), this); - showinpeopleAct = new QAction(QIcon(), tr("Show author in people tab"), this); + showInPeopleAct = new QAction(QIcon(), tr("Show author in people tab"), this); QActionGroup *sortgrp = new QActionGroup(this); actionSortByName = new QAction(QIcon(), tr("Sort by Name"), this); @@ -111,17 +112,13 @@ ChatLobbyDialog::ChatLobbyDialog(const ChatLobbyId& lid, QWidget *parent, Qt::Wi actionSortByActivity->setActionGroup(sortgrp); - connect(muteAct, SIGNAL(triggered()), this, SLOT(changePartipationState())); + connect(muteAct, SIGNAL(triggered()), this, SLOT(changeParticipationState())); connect(distantChatAct, SIGNAL(triggered()), this, SLOT(distantChatParticipant())); connect(sendMessageAct, SIGNAL(triggered()), this, SLOT(sendMessage())); connect(votePositiveAct, SIGNAL(triggered()), this, SLOT(voteParticipant())); connect(voteNeutralAct, SIGNAL(triggered()), this, SLOT(voteParticipant())); connect(voteNegativeAct, SIGNAL(triggered()), this, SLOT(voteParticipant())); - connect(showinpeopleAct, SIGNAL(triggered()), this, SLOT(showInPeopleTab())); - - votePositiveAct->setData(RsReputations::OPINION_POSITIVE); - voteNeutralAct->setData(RsReputations::OPINION_NEUTRAL); - voteNegativeAct->setData(RsReputations::OPINION_NEGATIVE); + connect(showInPeopleAct, SIGNAL(triggered()), this, SLOT(showInPeopleTab())); connect(actionSortByName, SIGNAL(triggered()), this, SLOT(sortParcipants())); connect(actionSortByActivity, SIGNAL(triggered()), this, SLOT(sortParcipants())); @@ -172,7 +169,8 @@ ChatLobbyDialog::ChatLobbyDialog(const ChatLobbyId& lid, QWidget *parent, Qt::Wi ui.chatWidget->addToolsAction(checkableAction); //getChatWidget()->addChatBarWidget(ownIdChooser); - + connect(ui.chatWidget, SIGNAL(textBrowserAskContextMenu(QMenu*,QString,QPoint)), this, SLOT(textBrowserAskContextMenu(QMenu*,QString,QPoint))); + connect(ownIdChooser,SIGNAL(currentIndexChanged(int)),this,SLOT(changeNickname())) ; @@ -226,33 +224,76 @@ void ChatLobbyDialog::inviteFriends() void ChatLobbyDialog::participantsTreeWidgetCustomPopupMenu(QPoint) { QList selectedItems = ui.participantsList->selectedItems(); + QList idList; + QList::iterator item; + for (item = selectedItems.begin(); item != selectedItems.end(); ++item) + { + RsGxsId gxs_id ; + dynamic_cast(*item)->getId(gxs_id) ; + idList.append(gxs_id); + } QMenu contextMnu(this); + contextMnu.addAction(actionSortByActivity); + contextMnu.addAction(actionSortByName); - contextMnu.addAction(distantChatAct); - contextMnu.addAction(sendMessageAct); - contextMnu.addSeparator(); - contextMnu.addAction(actionSortByActivity); - contextMnu.addAction(actionSortByName); - contextMnu.addSeparator(); - contextMnu.addAction(muteAct); - contextMnu.addAction(votePositiveAct); - contextMnu.addAction(voteNeutralAct); - contextMnu.addAction(voteNegativeAct); - contextMnu.addAction(showinpeopleAct); + contextMnu.addSeparator(); + + initParticipantsContextMenu(&contextMnu, idList); + + contextMnu.exec(QCursor::pos()); +} + +void ChatLobbyDialog::textBrowserAskContextMenu(QMenu* contextMnu, QString anchorForPosition, const QPoint /*point*/) +{ + if (anchorForPosition.startsWith(PERSONID)){ + QString strId = anchorForPosition.replace(PERSONID,""); + if (strId.contains(" ")) + strId.truncate(strId.indexOf(" ")); + + contextMnu->addSeparator(); + + QList idList; + idList.append(RsGxsId(strId.toStdString())); + initParticipantsContextMenu(contextMnu, idList); + } +} + +void ChatLobbyDialog::initParticipantsContextMenu(QMenu *contextMnu, QList idList) +{ + if (!contextMnu) + return; + + contextMnu->addAction(distantChatAct); + contextMnu->addAction(sendMessageAct); + contextMnu->addSeparator(); + contextMnu->addAction(muteAct); + contextMnu->addAction(votePositiveAct); + contextMnu->addAction(voteNeutralAct); + contextMnu->addAction(voteNegativeAct); + contextMnu->addAction(showInPeopleAct); distantChatAct->setEnabled(false); - sendMessageAct->setEnabled(selectedItems.count()==1); + sendMessageAct->setEnabled(true); + muteAct->setEnabled(false); muteAct->setCheckable(true); - muteAct->setEnabled(false); - muteAct->setChecked(false); - votePositiveAct->setEnabled(false); - voteNeutralAct->setEnabled(false); - voteNegativeAct->setEnabled(false); - showinpeopleAct->setEnabled(selectedItems.count()==1); - if(selectedItems.count()==1) - { - RsGxsId gxsid(selectedItems.at(0)->text(COLUMN_ID).toStdString()); + muteAct->setChecked(false); + votePositiveAct->setEnabled(false); + voteNeutralAct->setEnabled(false); + voteNegativeAct->setEnabled(false); + showInPeopleAct->setEnabled(idList.count()==1); + + distantChatAct->setData(QVariant::fromValue(idList)); + sendMessageAct->setData(QVariant::fromValue(idList)); + muteAct->setData(QVariant::fromValue(idList)); + votePositiveAct->setData(QVariant::fromValue(idList)); + voteNeutralAct->setData(QVariant::fromValue(idList)); + voteNegativeAct->setData(QVariant::fromValue(idList)); + showInPeopleAct->setData(QVariant::fromValue(idList)); + + if(idList.count()==1) + { + RsGxsId gxsid = idList.at(0); if(!gxsid.isNull() && !rsIdentity->isOwnId(gxsid)) { @@ -263,49 +304,56 @@ void ChatLobbyDialog::participantsTreeWidgetCustomPopupMenu(QPoint) muteAct->setEnabled(true); muteAct->setChecked(isParticipantMuted(gxsid)); } - } - contextMnu.exec(QCursor::pos()); + } } void ChatLobbyDialog::voteParticipant() { - QList selectedItems = ui.participantsList->selectedItems(); - if (selectedItems.isEmpty()) - return; - QList::iterator item; - - QAction *act = dynamic_cast(sender()) ; - if(!act) - { - std::cerr << "No sender! Some bug in the code." << std::endl; - return ; - } - - RsReputations::Opinion op = RsReputations::Opinion(act->data().toUInt()) ; - - for (item = selectedItems.begin(); item != selectedItems.end(); ++item) + QAction *act = dynamic_cast(sender()) ; + if(!act) { - RsGxsId nickname; - dynamic_cast(*item)->getId(nickname) ; + std::cerr << "No sender! Some bug in the code." << std::endl; + return ; + } - rsReputations->setOwnOpinion(nickname, op); - std::cerr << "Giving opinion to GXS id " << nickname << " to " << op<< std::endl; - dynamic_cast(*item)->forceUpdate(); - } + QList idList = act->data().value>(); + + RsReputations::Opinion op = RsReputations::OPINION_NEUTRAL ; + if (act == votePositiveAct) + op = RsReputations::OPINION_POSITIVE; + if (act == voteNegativeAct) + op = RsReputations::OPINION_NEGATIVE; + + for (QList::iterator item = idList.begin(); item != idList.end(); ++item) + { + rsReputations->setOwnOpinion(*item, op); + std::cerr << "Giving opinion to GXS id " << *item << " to " << op << std::endl; + } + + updateParticipantsList(); } void ChatLobbyDialog::showInPeopleTab() { - QList selectedItems = ui.participantsList->selectedItems(); - if (selectedItems.count()!=1) - return; - RsGxsId nickname; - dynamic_cast(*selectedItems.begin())->getId(nickname); - IdDialog *idDialog = dynamic_cast(MainWindow::getPage(MainWindow::People)); - if (!idDialog) - return ; - MainWindow::showWindow(MainWindow::People); - idDialog->navigate(nickname); + QAction *act = dynamic_cast(sender()) ; + if(!act) + { + std::cerr << "No sender! Some bug in the code." << std::endl; + return ; + } + + QList idList = act->data().value>(); + if (idList.count() != 1) + return; + + RsGxsId nickname = idList.at(0); + + IdDialog *idDialog = dynamic_cast(MainWindow::getPage(MainWindow::People)); + if (!idDialog) + return ; + MainWindow::showWindow(MainWindow::People); + + idDialog->navigate(nickname); } void ChatLobbyDialog::init(const ChatId &/*id*/, const QString &/*title*/) @@ -586,34 +634,28 @@ void ChatLobbyDialog::updateParticipantsList() } /** - * Called when a Participant in QTree get Clicked / Changed + * Called when a Participant get Clicked / Changed * * Check if the Checkbox altered and Mute User - * - * @todo auf rsid - * - * @param QTreeWidgetItem Participant to check */ -void ChatLobbyDialog::changePartipationState() +void ChatLobbyDialog::changeParticipationState() { - QList selectedItems = ui.participantsList->selectedItems(); - - if (selectedItems.isEmpty()) { - return; + QAction *act = dynamic_cast(sender()) ; + if(!act) + { + std::cerr << "No sender! Some bug in the code." << std::endl; + return ; } - QList::iterator item; - for (item = selectedItems.begin(); item != selectedItems.end(); ++item) { + QList idList = act->data().value>(); - RsGxsId gxs_id ; - dynamic_cast(*item)->getId(gxs_id) ; - - std::cerr << "check Partipation status for '" << gxs_id << std::endl; - - if (muteAct->isChecked()) { - muteParticipant(gxs_id); + for (QList::iterator item = idList.begin(); item != idList.end(); ++item) + { + std::cerr << "check Partipation status for '" << *item << std::endl; + if (act->isChecked()) { + muteParticipant(*item); } else { - unMuteParticipant(gxs_id); + unMuteParticipant(*item); } } @@ -650,75 +692,68 @@ void ChatLobbyDialog::participantsTreeWidgetDoubleClicked(QTreeWidgetItem *item, void ChatLobbyDialog::distantChatParticipant() { - std::cerr << " initiating distant chat" << std::endl; + QAction *act = dynamic_cast(sender()) ; + if(!act) + { + std::cerr << "No sender! Some bug in the code." << std::endl; + return ; + } - QList selectedItems = ui.participantsList->selectedItems(); + std::cerr << " initiating distant chat" << std::endl; - if (selectedItems.isEmpty()) - return; + QList idList = act->data().value>(); + if (idList.count() != 1) + return; - if(selectedItems.size() != 1) - return ; + RsGxsId gxs_id = idList.at(0); + if (gxs_id.isNull()) + return; - GxsIdRSTreeWidgetItem *item = dynamic_cast(selectedItems.front()); + RsGxsId own_id; + rsMsgs->getIdentityForChatLobby(lobbyId, own_id); - if(!item) - return ; + DistantChatPeerId tunnel_id; + uint32_t error_code ; - RsGxsId gxs_id ; - item->getId(gxs_id) ; - RsGxsId own_id; - - rsMsgs->getIdentityForChatLobby(lobbyId, own_id); - - uint32_t error_code ; - DistantChatPeerId tunnel_id; - - if(! rsMsgs->initiateDistantChatConnexion(gxs_id,own_id,tunnel_id,error_code)) - { - QString error_str ; - switch(error_code) - { - case RS_DISTANT_CHAT_ERROR_DECRYPTION_FAILED : error_str = tr("Decryption failed.") ; break ; - case RS_DISTANT_CHAT_ERROR_SIGNATURE_MISMATCH : error_str = tr("Signature mismatch") ; break ; - case RS_DISTANT_CHAT_ERROR_UNKNOWN_KEY : error_str = tr("Unknown key") ; break ; - case RS_DISTANT_CHAT_ERROR_UNKNOWN_HASH : error_str = tr("Unknown hash") ; break ; - default: - error_str = tr("Unknown error.") ; - } - QMessageBox::warning(NULL,tr("Cannot start distant chat"),tr("Distant chat cannot be initiated:")+" "+error_str - +QString::number(error_code)) ; - } + if(! rsMsgs->initiateDistantChatConnexion(gxs_id,own_id,tunnel_id,error_code)) + { + QString error_str ; + switch(error_code) + { + case RS_DISTANT_CHAT_ERROR_DECRYPTION_FAILED : error_str = tr("Decryption failed.") ; break ; + case RS_DISTANT_CHAT_ERROR_SIGNATURE_MISMATCH : error_str = tr("Signature mismatch") ; break ; + case RS_DISTANT_CHAT_ERROR_UNKNOWN_KEY : error_str = tr("Unknown key") ; break ; + case RS_DISTANT_CHAT_ERROR_UNKNOWN_HASH : error_str = tr("Unknown hash") ; break ; + default: + error_str = tr("Unknown error.") ; + } + QMessageBox::warning(NULL,tr("Cannot start distant chat"),tr("Distant chat cannot be initiated:")+" "+error_str + +QString::number(error_code)) ; + } } void ChatLobbyDialog::sendMessage() { + QAction *act = dynamic_cast(sender()) ; + if(!act) + { + std::cerr << "No sender! Some bug in the code." << std::endl; + return ; + } - QList selectedItems = ui.participantsList->selectedItems(); + QList idList = act->data().value>(); - if (selectedItems.isEmpty()) - return; + MessageComposer *nMsgDialog = MessageComposer::newMsg(); + if (nMsgDialog == NULL) + return; - QList::iterator item; - for (item = selectedItems.begin(); item != selectedItems.end(); ++item) { + for (QList::iterator item = idList.begin(); item != idList.end(); ++item) + nMsgDialog->addRecipient(MessageComposer::TO, *item); - RsGxsId gxs_id ; - dynamic_cast(*item)->getId(gxs_id) ; - - - MessageComposer *nMsgDialog = MessageComposer::newMsg(); - if (nMsgDialog == NULL) { - return; - } - - nMsgDialog->addRecipient(MessageComposer::TO, RsGxsId(gxs_id)); - nMsgDialog->show(); - nMsgDialog->activateWindow(); - - /* window will destroy itself! */ - - } + nMsgDialog->show(); + nMsgDialog->activateWindow(); + /* window will destroy itself! */ } diff --git a/retroshare-gui/src/gui/chat/ChatLobbyDialog.h b/retroshare-gui/src/gui/chat/ChatLobbyDialog.h index dbe178678..6f3f52096 100644 --- a/retroshare-gui/src/gui/chat/ChatLobbyDialog.h +++ b/retroshare-gui/src/gui/chat/ChatLobbyDialog.h @@ -27,6 +27,9 @@ #include "gui/common/RSTreeWidgetItem.h" #include "ChatDialog.h" +Q_DECLARE_METATYPE(RsGxsId) +Q_DECLARE_METATYPE(QList) + class GxsIdChooser ; class QToolButton; class QWidgetAction; @@ -51,6 +54,7 @@ public: private slots: void participantsTreeWidgetCustomPopupMenu( QPoint point ); + void textBrowserAskContextMenu(QMenu* contextMnu, QString anchorForPosition, const QPoint point); void inviteFriends() ; void leaveLobby() ; void filterChanged(const QString &text); @@ -77,7 +81,7 @@ protected: protected slots: void changeNickname(); - void changePartipationState(); + void changeParticipationState(); void distantChatParticipant(); void participantsTreeWidgetDoubleClicked(QTreeWidgetItem *item, int column); void sendMessage(); @@ -85,6 +89,7 @@ protected slots: private: void updateParticipantsList(); + void initParticipantsContextMenu(QMenu* contextMnu, QList idList); void filterIds(); @@ -117,8 +122,8 @@ private: QAction *actionSortByActivity; QWidgetAction *checkableAction; QAction *sendMessageAct; - QAction *showinpeopleAct; - + QAction *showInPeopleAct; + GxsIdChooser *ownIdChooser ; //icons cache QIcon bullet_red_128, bullet_grey_128, bullet_green_128, bullet_yellow_128; diff --git a/retroshare-gui/src/gui/chat/ChatTabWidget.cpp b/retroshare-gui/src/gui/chat/ChatTabWidget.cpp index a965bbb32..6dd9c2757 100644 --- a/retroshare-gui/src/gui/chat/ChatTabWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatTabWidget.cpp @@ -20,6 +20,8 @@ * Boston, MA 02110-1301, USA. ****************************************************************/ +#include + #include "ChatTabWidget.h" #include "ui_ChatTabWidget.h" #include "ChatDialog.h" diff --git a/retroshare-gui/src/gui/chat/ChatWidget.cpp b/retroshare-gui/src/gui/chat/ChatWidget.cpp index 2859527cd..3ed194789 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.cpp +++ b/retroshare-gui/src/gui/chat/ChatWidget.cpp @@ -66,8 +66,6 @@ #define FMM 2.5//fontMetricsMultiplicator -#define PERSONID "PersonId:" - /***** * #define CHAT_DEBUG 1 *****/ @@ -389,7 +387,7 @@ void ChatWidget::init(const ChatId &chat_id, const QString &title) name = QString::fromUtf8(historyIt->peerName.c_str()); } - addChatMsg(historyIt->incoming, name, QDateTime::fromTime_t(historyIt->sendTime), QDateTime::fromTime_t(historyIt->recvTime), QString::fromUtf8(historyIt->message.c_str()), MSGTYPE_HISTORY); + addChatMsg(historyIt->incoming, name, RsGxsId(historyIt->peerName.c_str()), QDateTime::fromTime_t(historyIt->sendTime), QDateTime::fromTime_t(historyIt->recvTime), QString::fromUtf8(historyIt->message.c_str()), MSGTYPE_HISTORY); } } } @@ -594,31 +592,15 @@ bool ChatWidget::eventFilter(QObject *obj, QEvent *event) if (event->type() == QEvent::ToolTip) { QHelpEvent* helpEvent = static_cast(event); - QTextCursor cursor = ui->textBrowser->cursorForPosition(helpEvent->pos()); - cursor.select(QTextCursor::WordUnderCursor); - QString toolTipText = ""; - if (!cursor.selectedText().isEmpty()){ - QRegExp rx("textBrowser->getShowImages()){ - QString imageStr; - if (ui->textBrowser->checkImage(helpEvent->pos(), imageStr)) { - toolTipText = imageStr; - } - } else if (toolTipText.startsWith(PERSONID)){ - toolTipText = toolTipText.replace(PERSONID, tr("Person id: ") ); - toolTipText = toolTipText.append(tr("\nDouble click on it to add his name on text writer.") ); + QString toolTipText = ui->textBrowser->anchorForPosition(helpEvent->pos()); + if (toolTipText.isEmpty() && !ui->textBrowser->getShowImages()){ + QString imageStr; + if (ui->textBrowser->checkImage(helpEvent->pos(), imageStr)) { + toolTipText = imageStr; } + } else if (toolTipText.startsWith(PERSONID)){ + toolTipText = toolTipText.replace(PERSONID, tr("Person id: ") ); + toolTipText = toolTipText.append(tr("\nDouble click on it to add his name on text writer.") ); } if (!toolTipText.isEmpty()){ QToolTip::showText(helpEvent->globalPos(), toolTipText); @@ -710,32 +692,19 @@ bool ChatWidget::eventFilter(QObject *obj, QEvent *event) if (event->type() == QEvent::MouseButtonDblClick) { QMouseEvent* mouseEvent = static_cast(event); - QTextCursor cursor = ui->textBrowser->cursorForPosition(mouseEvent->pos()); - cursor.select(QTextCursor::WordUnderCursor); - if (!cursor.selectedText().isEmpty()){ - QRegExp rx("textBrowser->anchorForPosition(mouseEvent->pos()); + if (!anchor.isEmpty()){ + if (anchor.startsWith(PERSONID)){ + QString strId = anchor.replace(PERSONID,""); + if (strId.contains(" ")) + strId.truncate(strId.indexOf(" ")); - if (!anchors.isEmpty()){ - if (anchors.at(0).startsWith(PERSONID)){ - QString strId = QString(anchors.at(0)).replace(PERSONID,""); - if (strId.contains(" ")) - strId.truncate(strId.indexOf(" ")); - - RsGxsId mId = RsGxsId(strId.toStdString()); - if(!mId.isNull()) { - RsIdentityDetails details; - if (rsIdentity->getIdDetails(mId, details)){ - QString text = QString("@").append(GxsIdDetails::getName(details)).append(" "); - ui->chatTextEdit->textCursor().insertText(text); - } + RsGxsId mId = RsGxsId(strId.toStdString()); + if(!mId.isNull()) { + RsIdentityDetails details; + if (rsIdentity->getIdDetails(mId, details)){ + QString text = QString("@").append(GxsIdDetails::getName(details)).append(" "); + ui->chatTextEdit->textCursor().insertText(text); } } } @@ -743,6 +712,13 @@ bool ChatWidget::eventFilter(QObject *obj, QEvent *event) } } + } else { + if (event->type() == QEvent::WindowActivate) { + if (isVisible() && (window() == NULL || window()->isActiveWindow())) { + newMessages = false; + emit infoChanged(this); + } + } } // pass the event on to the parent class return QWidget::eventFilter(obj, event); @@ -1131,6 +1107,9 @@ void ChatWidget::contextMenuTextBrowser(QPoint point) contextMnu->addAction(ui->actionSave_image); } + QString anchor = ui->textBrowser->anchorForPosition(point); + emit textBrowserAskContextMenu(contextMnu, anchor, point); + contextMnu->exec(ui->textBrowser->viewport()->mapToGlobal(point)); delete(contextMnu); } @@ -1598,7 +1577,7 @@ void ChatWidget::fileHashingFinished(QList hashedFiles) QList::iterator it; for (it = hashedFiles.begin(); it != hashedFiles.end(); ++it) { HashedFile& hashedFile = *it; - //QString ext = QFileInfo(hashedFile.filename).suffix(); + QString ext = QFileInfo(hashedFile.filename).suffix().toUpper(); RetroShareLink link; @@ -1610,9 +1589,23 @@ void ChatWidget::fileHashingFinished(QList hashedFiles) message += QString("").arg(hashedFile.filepath); message+="
"; } else { - QString image = FilesDefs::getImageFromFilename(hashedFile.filename, false); - if (!image.isEmpty()) { - message += QString("").arg(image); + bool preview = false; + if(hashedFiles.size()==1 && (ext == "JPG" || ext == "PNG" || ext == "JPEG" || ext == "GIF")) + { + QString encodedImage; + uint32_t maxMessageSize = this->maxMessageSize(); + if (RsHtml::makeEmbeddedImage(hashedFile.filepath, encodedImage, 640*480, maxMessageSize - 200 - link.toHtmlSize().length())) + { QTextDocumentFragment fragment = QTextDocumentFragment::fromHtml(encodedImage); + ui->chatTextEdit->textCursor().insertFragment(fragment); + preview=true; + } + } + if(!preview) + { + QString image = FilesDefs::getImageFromFilename(hashedFile.filename, false); + if (!image.isEmpty()) { + message += QString("").arg(image); + } } } message += link.toHtmlSize(); @@ -1791,7 +1784,7 @@ void ChatWidget::updatePeersCustomStateString(const QString& /*peer_id*/, const void ChatWidget::updateStatusString(const QString &statusMask, const QString &statusString, bool permanent) { - ui->typingLabel->setText(QString(statusMask).arg(tr(statusString.toUtf8()))); // displays info for 5 secs. + ui->typingLabel->setText(QString(statusMask).arg(trUtf8(statusString.toUtf8()))); // displays info for 5 secs. ui->typingPixmapLabel->setPixmap(QPixmap(":images/typing.png") ); if (statusString == "is typing...") { diff --git a/retroshare-gui/src/gui/chat/ChatWidget.h b/retroshare-gui/src/gui/chat/ChatWidget.h index 30f2e8427..8de38620e 100644 --- a/retroshare-gui/src/gui/chat/ChatWidget.h +++ b/retroshare-gui/src/gui/chat/ChatWidget.h @@ -37,6 +37,9 @@ #include #include +//For PersonId anchor. +#define PERSONID "PersonId:" + class QAction; class QTextEdit; class QPushButton; @@ -137,6 +140,7 @@ signals: void infoChanged(ChatWidget*); void newMessage(ChatWidget*); void statusChanged(int); + void textBrowserAskContextMenu(QMenu* contextMnu, QString anchorForPosition, const QPoint point); protected: bool eventFilter(QObject *obj, QEvent *event); diff --git a/retroshare-gui/src/gui/chat/PopupDistantChatDialog.cpp b/retroshare-gui/src/gui/chat/PopupDistantChatDialog.cpp index f457e11d9..da662fcb6 100644 --- a/retroshare-gui/src/gui/chat/PopupDistantChatDialog.cpp +++ b/retroshare-gui/src/gui/chat/PopupDistantChatDialog.cpp @@ -135,7 +135,7 @@ void PopupDistantChatDialog::updateDisplay() break ; case RS_DISTANT_CHAT_STATUS_TUNNEL_DN: //std::cerr << "Tunnel asked. Waiting for reponse. " << std::endl; - _status_label->setIcon(QIcon(IMAGE_RED_LED)); + _status_label->setIcon(QIcon(IMAGE_YEL_LED)); msg = QObject::tr( "Tunnel is pending... Messages will be delivered as" " soon as possible" ); _status_label->setToolTip(msg); diff --git a/retroshare-gui/src/gui/common/GroupTreeWidget.cpp b/retroshare-gui/src/gui/common/GroupTreeWidget.cpp index f5fd77daf..4a88eee48 100644 --- a/retroshare-gui/src/gui/common/GroupTreeWidget.cpp +++ b/retroshare-gui/src/gui/common/GroupTreeWidget.cpp @@ -45,7 +45,8 @@ #define COLUMN_NAME 0 #define COLUMN_UNREAD 1 #define COLUMN_POPULARITY 2 -#define COLUMN_COUNT 3 +#define COLUMN_LAST_POST 3 +#define COLUMN_COUNT 4 #define COLUMN_DATA COLUMN_NAME #define ROLE_ID Qt::UserRole @@ -104,9 +105,12 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) : /* Initialize tree widget */ ui->treeWidget->setColumnCount(COLUMN_COUNT); + ui->treeWidget->enableColumnCustomize(true); + ui->treeWidget->setColumnCustomizable(COLUMN_NAME, false); int S = QFontMetricsF(font()).height() ; int W = QFontMetricsF(font()).width("999") ; + int D = QFontMetricsF(font()).width("9999-99-99[]") ; /* Set header resize modes and initial section sizes */ QHeaderView *header = ui->treeWidget->header (); @@ -117,6 +121,15 @@ GroupTreeWidget::GroupTreeWidget(QWidget *parent) : header->resizeSection(COLUMN_UNREAD, W+4) ; QHeaderView_setSectionResizeModeColumn(header, COLUMN_POPULARITY, QHeaderView::Fixed); header->resizeSection(COLUMN_POPULARITY, 2*S) ; + QHeaderView_setSectionResizeModeColumn(header, COLUMN_LAST_POST, QHeaderView::Fixed); + header->resizeSection(COLUMN_LAST_POST, D+4) ; + header->setSectionHidden(COLUMN_LAST_POST, true); + + QTreeWidgetItem *headerItem = ui->treeWidget->headerItem(); + headerItem->setText(COLUMN_NAME, tr("Name")); + headerItem->setText(COLUMN_UNREAD, tr("Unread")); + headerItem->setText(COLUMN_POPULARITY, tr("Popularity")); + headerItem->setText(COLUMN_LAST_POST, tr("Last Post")); /* add filter actions */ ui->filterLineEdit->addFilter(QIcon(), tr("Title"), FILTER_NAME_INDEX , tr("Search Title")); @@ -200,9 +213,10 @@ void GroupTreeWidget::addToolButton(QToolButton *toolButton) ui->titleBarFrame->layout()->addWidget(toolButton); } -void GroupTreeWidget::processSettings(RshareSettings *settings, bool load) +// Load and save settings (group must be started from the caller) +void GroupTreeWidget::processSettings(bool load) { - if (settings == NULL) { + if (Settings == NULL) { return; } @@ -212,16 +226,18 @@ void GroupTreeWidget::processSettings(RshareSettings *settings, bool load) const int SORTBY_POSTS = 4; const int SORTBY_UNREAD = 5; + ui->treeWidget->processSettings(load); + if (load) { - // load settings + // load Settings // state of order - bool ascSort = settings->value("GroupAscSort", true).toBool(); + bool ascSort = Settings->value("GroupAscSort", true).toBool(); actionSortAscending->setChecked(ascSort); actionSortDescending->setChecked(!ascSort); // state of sort - int sortby = settings->value("GroupSortBy").toInt(); + int sortby = Settings->value("GroupSortBy").toInt(); switch (sortby) { case SORTBY_NAME: if (actionSortByName) { @@ -250,10 +266,10 @@ void GroupTreeWidget::processSettings(RshareSettings *settings, bool load) break; } } else { - // save settings + // save Settings // state of order - settings->setValue("GroupAscSort", !(actionSortDescending && actionSortDescending->isChecked())); //True by default + Settings->setValue("GroupAscSort", !(actionSortDescending && actionSortDescending->isChecked())); //True by default // state of sort int sortby = SORTBY_NAME; @@ -268,7 +284,7 @@ void GroupTreeWidget::processSettings(RshareSettings *settings, bool load) } else if (actionSortByUnread && actionSortByUnread->isChecked()) { sortby = SORTBY_UNREAD; } - settings->setValue("GroupSortBy", sortby); + Settings->setValue("GroupSortBy", sortby); } } @@ -487,6 +503,11 @@ void GroupTreeWidget::fillGroupItems(QTreeWidgetItem *categoryItem, const QList< /* Set last post */ qlonglong lastPost = itemInfo.lastpost.toTime_t(); item->setData(COLUMN_DATA, ROLE_LASTPOST, -lastPost); // negative for correct sorting + if(itemInfo.lastpost == QDateTime::fromTime_t(0)) + item->setText(COLUMN_LAST_POST, tr("Never")); + else + item->setText(COLUMN_LAST_POST, itemInfo.lastpost.toString(Qt::ISODate).replace("T"," ")); + /* Set visible posts */ item->setData(COLUMN_DATA, ROLE_POSTS, -itemInfo.max_visible_posts);// negative for correct sorting diff --git a/retroshare-gui/src/gui/common/GroupTreeWidget.h b/retroshare-gui/src/gui/common/GroupTreeWidget.h index 6b782cb7c..4ad19f0e1 100644 --- a/retroshare-gui/src/gui/common/GroupTreeWidget.h +++ b/retroshare-gui/src/gui/common/GroupTreeWidget.h @@ -77,8 +77,8 @@ public: // Add a tool button to the tool area void addToolButton(QToolButton *toolButton); - // Load and save settings (group must be startet from the caller) - void processSettings(RshareSettings *settings, bool load); + // Load and save settings (group must be started from the caller) + void processSettings(bool load); // Add a new category item QTreeWidgetItem *addCategoryItem(const QString &name, const QIcon &icon, bool expand); diff --git a/retroshare-gui/src/gui/common/RSTextBrowser.cpp b/retroshare-gui/src/gui/common/RSTextBrowser.cpp index 8a18b459f..03fbfe3de 100644 --- a/retroshare-gui/src/gui/common/RSTextBrowser.cpp +++ b/retroshare-gui/src/gui/common/RSTextBrowser.cpp @@ -243,3 +243,30 @@ bool RSTextBrowser::checkImage(QPoint pos, QString &imageStr) } return false; } + +/** + * @brief RSTextBrowser::anchorForPosition Replace anchorAt that doesn't works as expected. + * @param pos Where to get anchor from text + * @return anchor If text at pos is inside anchor, else empty string. + */ +QString RSTextBrowser::anchorForPosition(const QPoint &pos) const +{ + QTextCursor cursor = cursorForPosition(pos); + cursor.select(QTextCursor::WordUnderCursor); + QString anchor = ""; + if (!cursor.selectedText().isEmpty()){ + QRegExp rx("
setText(QString::number(getRootItem()->data(COLUMN_FILEC,ROLE_SELFILEC).toULongLong())) ; - ui._totalSize_TL->setText(misc::friendlyUnit(getRootItem()->data(COLUMN_SIZE,ROLE_SELSIZE).toULongLong())) ; + uint64_t total_size = 0 ; + uint32_t total_count = 0 ; + + for(uint32_t i=0;itopLevelItemCount();++i) + { + total_size += ui._fileEntriesTW->topLevelItem(i)->data(COLUMN_SIZE ,ROLE_SELSIZE ).toULongLong(); + total_count += ui._fileEntriesTW->topLevelItem(i)->data(COLUMN_FILEC,ROLE_SELFILEC).toULongLong(); + } + + ui._selectedFiles_TL->setText(QString::number(total_count)); + ui._totalSize_TL->setText(misc::friendlyUnit(total_size)); } /** diff --git a/retroshare-gui/src/gui/feeds/GxsChannelPostItem.cpp b/retroshare-gui/src/gui/feeds/GxsChannelPostItem.cpp index d154408b7..72317640c 100644 --- a/retroshare-gui/src/gui/feeds/GxsChannelPostItem.cpp +++ b/retroshare-gui/src/gui/feeds/GxsChannelPostItem.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "rshare.h" #include "GxsChannelPostItem.h" diff --git a/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp b/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp index 97ac9dd75..b02afa336 100644 --- a/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp +++ b/retroshare-gui/src/gui/feeds/GxsForumMsgItem.cpp @@ -23,6 +23,7 @@ #include #include +#include #include "rshare.h" #include "GxsForumMsgItem.h" @@ -437,8 +438,8 @@ void GxsForumMsgItem::requestParentMessage(const RsGxsMessageId &msgId) opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; GxsMsgReq msgIds; - std::vector &vect_msgIds = msgIds[groupId()]; - vect_msgIds.push_back(msgId); + std::set &vect_msgIds = msgIds[groupId()]; + vect_msgIds.insert(msgId); uint32_t token; mLoadQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeParentMessage); diff --git a/retroshare-gui/src/gui/gxs/GxsFeedItem.cpp b/retroshare-gui/src/gui/gxs/GxsFeedItem.cpp index f795768f6..2f036f900 100644 --- a/retroshare-gui/src/gui/gxs/GxsFeedItem.cpp +++ b/retroshare-gui/src/gui/gxs/GxsFeedItem.cpp @@ -93,19 +93,15 @@ void GxsFeedItem::fillDisplay(RsGxsUpdateBroadcastBase *updateBroadcastBase, boo { GxsGroupFeedItem::fillDisplay(updateBroadcastBase, complete); - std::map > msgs; + std::map > msgs; updateBroadcastBase->getAllMsgIds(msgs); if (!msgs.empty()) { - std::map >::const_iterator mit = msgs.find(groupId()); - if (mit != msgs.end()) - { - const std::vector &msgIds = mit->second; - if (std::find(msgIds.begin(), msgIds.end(), messageId()) != msgIds.end()) { + std::map >::const_iterator mit = msgs.find(groupId()); + + if (mit != msgs.end() && mit->second.find(messageId()) != mit->second.end()) requestMessage(); - } - } } } @@ -129,8 +125,8 @@ void GxsFeedItem::requestMessage() opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; GxsMsgReq msgIds; - std::vector &vect_msgIds = msgIds[groupId()]; - vect_msgIds.push_back(mMessageId); + std::set &vect_msgIds = msgIds[groupId()]; + vect_msgIds.insert(mMessageId); uint32_t token; mLoadQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeMessage); diff --git a/retroshare-gui/src/gui/gxs/GxsGroupFeedItem.cpp b/retroshare-gui/src/gui/gxs/GxsGroupFeedItem.cpp index 9682ab987..1b3d0990e 100644 --- a/retroshare-gui/src/gui/gxs/GxsGroupFeedItem.cpp +++ b/retroshare-gui/src/gui/gxs/GxsGroupFeedItem.cpp @@ -170,12 +170,11 @@ void GxsGroupFeedItem::fillDisplaySlot(bool complete) void GxsGroupFeedItem::fillDisplay(RsGxsUpdateBroadcastBase *updateBroadcastBase, bool /*complete*/) { - std::list grpIds; + std::set grpIds; updateBroadcastBase->getAllGrpIds(grpIds); - if (std::find(grpIds.begin(), grpIds.end(), groupId()) != grpIds.end()) { + if (grpIds.find(groupId()) != grpIds.end()) requestGroup(); - } } /***********************************************************/ diff --git a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp index 04e64258b..470ffbaab 100644 --- a/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp +++ b/retroshare-gui/src/gui/gxs/GxsGroupFrameDialog.cpp @@ -196,7 +196,7 @@ void GxsGroupFrameDialog::processSettings(bool load) Settings->setValue("Splitter", ui->splitter->saveState()); } - ui->groupTreeWidget->processSettings(Settings, load); + ui->groupTreeWidget->processSettings(load); Settings->endGroup(); } @@ -238,11 +238,10 @@ void GxsGroupFrameDialog::updateDisplay(bool complete) requestGroupSummary(); } else { /* Update all groups of changed messages */ - std::map > msgIds; + std::map > msgIds; getAllMsgIds(msgIds); - std::map >::iterator msgIt; - for (msgIt = msgIds.begin(); msgIt != msgIds.end(); ++msgIt) { + for (auto msgIt = msgIds.begin(); msgIt != msgIds.end(); ++msgIt) { updateMessageSummaryList(msgIt->first); } } @@ -280,19 +279,19 @@ void GxsGroupFrameDialog::removeAllSearches() } void GxsGroupFrameDialog::groupTreeCustomPopupMenu(QPoint point) { - // First separately handle the case of search top level items + // First separately handle the case of search top level items - TurtleRequestId search_request_id = 0 ; + TurtleRequestId search_request_id = 0 ; - if(ui->groupTreeWidget->isSearchRequestItem(point,search_request_id)) - { + if(ui->groupTreeWidget->isSearchRequestItem(point,search_request_id)) + { QMenu contextMnu(this); contextMnu.addAction(QIcon(IMAGE_DELETE), tr("Remove this search"), this, SLOT(removeCurrentSearch()))->setData(search_request_id); contextMnu.addAction(QIcon(IMAGE_DELETE), tr("Remove all searches"), this, SLOT(removeAllSearches())); contextMnu.exec(QCursor::pos()); - return ; - } + return ; + } QString id = ui->groupTreeWidget->itemIdAt(point); if (id.isEmpty()) return; @@ -305,9 +304,8 @@ void GxsGroupFrameDialog::groupTreeCustomPopupMenu(QPoint point) bool isSubscribed = IS_GROUP_SUBSCRIBED(subscribeFlags); QMenu contextMnu(this); - QAction *action; - + if (mMessageWidget) { action = contextMnu.addAction(QIcon(IMAGE_TABNEW), tr("Open in new tab"), this, SLOT(openInNewTab())); if (mGroupId.isNull() || messageWidget(mGroupId, true)) { @@ -333,14 +331,14 @@ void GxsGroupFrameDialog::groupTreeCustomPopupMenu(QPoint point) action = contextMnu.addAction(QIcon(IMAGE_EDIT), tr("Edit Details"), this, SLOT(editGroupDetails())); action->setEnabled (!mGroupId.isNull() && isAdmin); - uint32_t current_store_time = mInterface->getStoragePeriod(mGroupId)/86400 ; - uint32_t current_sync_time = mInterface->getSyncPeriod(mGroupId)/86400 ; + uint32_t current_store_time = mInterface->getStoragePeriod(mGroupId)/86400 ; + uint32_t current_sync_time = mInterface->getSyncPeriod(mGroupId)/86400 ; - std::cerr << "Got sync=" << current_sync_time << ". store=" << current_store_time << std::endl; - QAction *actnn = NULL; + std::cerr << "Got sync=" << current_sync_time << ". store=" << current_store_time << std::endl; + QAction *actnn = NULL; QMenu *ctxMenu2 = contextMnu.addMenu(tr("Synchronise posts of last...")) ; - actnn = ctxMenu2->addAction(tr(" 5 days" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 5)) ; if(current_sync_time == 5) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 5 days" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 5)) ; if(current_sync_time == 5) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} actnn = ctxMenu2->addAction(tr(" 2 weeks" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 15)) ; if(current_sync_time == 15) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} actnn = ctxMenu2->addAction(tr(" 1 month" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 30)) ; if(current_sync_time == 30) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} actnn = ctxMenu2->addAction(tr(" 3 months" ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 90)) ; if(current_sync_time == 90) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} @@ -348,18 +346,18 @@ void GxsGroupFrameDialog::groupTreeCustomPopupMenu(QPoint point) actnn = ctxMenu2->addAction(tr(" 1 year " ),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant(372)) ; if(current_sync_time ==372) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} actnn = ctxMenu2->addAction(tr(" Indefinitly"),this,SLOT(setSyncPostsDelay())) ; actnn->setData(QVariant( 0)) ; if(current_sync_time == 0) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - ctxMenu2 = contextMnu.addMenu(tr("Store posts for at most...")) ; - actnn = ctxMenu2->addAction(tr(" 5 days" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 5)) ; if(current_store_time == 5) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 2 weeks" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 15)) ; if(current_store_time == 15) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 1 month" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 30)) ; if(current_store_time == 30) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 3 months" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 90)) ; if(current_store_time == 90) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 6 months" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant(180)) ; if(current_store_time ==180) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" 1 year " ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant(372)) ; if(current_store_time ==372) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} - actnn = ctxMenu2->addAction(tr(" Indefinitly"),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 0)) ; if(current_store_time == 0) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + ctxMenu2 = contextMnu.addMenu(tr("Store posts for at most...")) ; + actnn = ctxMenu2->addAction(tr(" 5 days" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 5)) ; if(current_store_time == 5) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 2 weeks" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 15)) ; if(current_store_time == 15) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 1 month" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 30)) ; if(current_store_time == 30) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 3 months" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 90)) ; if(current_store_time == 90) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 6 months" ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant(180)) ; if(current_store_time ==180) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" 1 year " ),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant(372)) ; if(current_store_time ==372) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} + actnn = ctxMenu2->addAction(tr(" Indefinitly"),this,SLOT(setStorePostsDelay())) ; actnn->setData(QVariant( 0)) ; if(current_store_time == 0) { actnn->setEnabled(false);actnn->setIcon(QIcon(":/images/start.png"));} if (shareKeyType()) { - action = contextMnu.addAction(QIcon(IMAGE_SHARE), tr("Share publish permissions"), this, SLOT(sharePublishKey())); - action->setEnabled(!mGroupId.isNull() && isPublisher); + action = contextMnu.addAction(QIcon(IMAGE_SHARE), tr("Share publish permissions"), this, SLOT(sharePublishKey())); + action->setEnabled(!mGroupId.isNull() && isPublisher); } if (getLinkType() != RetroShareLink::TYPE_UNKNOWN) { @@ -383,6 +381,9 @@ void GxsGroupFrameDialog::groupTreeCustomPopupMenu(QPoint point) contextMnu.addActions(actions); } + //Add Standard Menu + ui->groupTreeWidget->treeWidget()->createStandardContextMenu(&contextMnu); + contextMnu.exec(QCursor::pos()); } diff --git a/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.cpp b/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.cpp index df5442bcd..f23dc83f2 100644 --- a/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.cpp +++ b/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.cpp @@ -109,21 +109,22 @@ void GxsMessageFramePostWidget::updateDisplay(bool complete) } bool updateGroup = false; - const std::list &grpIdsMeta = getGrpIdsMeta(); - if (std::find(grpIdsMeta.begin(), grpIdsMeta.end(), groupId()) != grpIdsMeta.end()) { - updateGroup = true; - } + const std::set &grpIdsMeta = getGrpIdsMeta(); - const std::list &grpIds = getGrpIds(); - if (!groupId().isNull() && std::find(grpIds.begin(), grpIds.end(), groupId()) != grpIds.end()) { + if(grpIdsMeta.find(groupId())!=grpIdsMeta.end()) + updateGroup = true; + + const std::set &grpIds = getGrpIds(); + if (!groupId().isNull() && grpIds.find(groupId())!=grpIds.end()) + { updateGroup = true; /* Do we need to fill all posts? */ requestAllPosts(); } else { - std::map > msgs; + std::map > msgs; getAllMsgIds(msgs); if (!msgs.empty()) { - std::map >::const_iterator mit = msgs.find(groupId()); + auto mit = msgs.find(groupId()); if (mit != msgs.end()) { requestPosts(mit->second); } @@ -341,7 +342,7 @@ void GxsMessageFramePostWidget::loadAllPosts(const uint32_t &token) emit groupChanged(this); } -void GxsMessageFramePostWidget::requestPosts(const std::vector &msgIds) +void GxsMessageFramePostWidget::requestPosts(const std::set &msgIds) { #ifdef ENABLE_DEBUG std::cerr << "GxsMessageFramePostWidget::requestPosts()"; diff --git a/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.h b/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.h index d01a0ca42..2a42cb772 100644 --- a/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.h +++ b/retroshare-gui/src/gui/gxs/GxsMessageFramePostWidget.h @@ -72,7 +72,7 @@ protected: void loadAllPosts(const uint32_t &token); virtual void insertAllPosts(const uint32_t &token, GxsMessageFramePostThread *thread) = 0; - void requestPosts(const std::vector &msgIds); + void requestPosts(const std::set &msgIds); void loadPosts(const uint32_t &token); virtual void insertPosts(const uint32_t &token) = 0; diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp index b80180c9a..ffbd6147e 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.cpp @@ -15,7 +15,7 @@ RsGxsUpdateBroadcastBase::RsGxsUpdateBroadcastBase(RsGxsIfaceHelper *ifaceImpl, mUpdateBroadcast = RsGxsUpdateBroadcast::get(ifaceImpl); connect(mUpdateBroadcast, SIGNAL(changed()), this, SLOT(updateBroadcastChanged())); connect(mUpdateBroadcast, SIGNAL(grpsChanged(std::list, std::list)), this, SLOT(updateBroadcastGrpsChanged(std::list,std::list))); - connect(mUpdateBroadcast, SIGNAL(msgsChanged(std::map >, std::map >)), this, SLOT(updateBroadcastMsgsChanged(std::map >,std::map >))); + connect(mUpdateBroadcast, SIGNAL(msgsChanged(std::map >, std::map >)), this, SLOT(updateBroadcastMsgsChanged(std::map >,std::map >))); } RsGxsUpdateBroadcastBase::~RsGxsUpdateBroadcastBase() @@ -67,6 +67,17 @@ void RsGxsUpdateBroadcastBase::updateBroadcastChanged() /* Update only update when the widget is visible. */ if (mUpdateWhenInvisible || !widget || widget->isVisible()) { + + // (cyril) Re-load the entire group is new messages are here, or if group metadata has changed (e.g. visibility permissions, admin rights, etc). + // Do not re-load if Msg data has changed, which means basically the READ flag has changed, because this action is done in the UI in the + // first place so there's no need to re-update the UI once this is done. + // + // The question to whether we should re=load when mGrpIds is not empty is still open. It's not harmful anyway. + // This should probably be decided by the service itself. + + if (!mGrpIds.empty() || !mGrpIdsMeta.empty() /*|| !mMsgIds.empty()*/ || !mMsgIdsMeta.empty()) + mFillComplete = true ; + securedUpdateDisplay(); } } @@ -74,79 +85,36 @@ void RsGxsUpdateBroadcastBase::updateBroadcastChanged() void RsGxsUpdateBroadcastBase::updateBroadcastGrpsChanged(const std::list &grpIds, const std::list &grpIdsMeta) { std::list::const_iterator it; - for (it = grpIds.begin(); it != grpIds.end(); ++it) { - if (std::find(mGrpIds.begin(), mGrpIds.end(), *it) == mGrpIds.end()) { - mGrpIds.push_back(*it); - } - } - for (it = grpIdsMeta.begin(); it != grpIdsMeta.end(); ++it) { - if (std::find(mGrpIdsMeta.begin(), mGrpIdsMeta.end(), *it) == mGrpIdsMeta.end()) { - mGrpIdsMeta.push_back(*it); - } - } + for (it = grpIds.begin(); it != grpIds.end(); ++it) + mGrpIds.insert(*it) ; + + for (it = grpIdsMeta.begin(); it != grpIdsMeta.end(); ++it) + mGrpIdsMeta.insert(*it); } -void RsGxsUpdateBroadcastBase::updateBroadcastMsgsChanged(const std::map > &msgIds, const std::map > &msgIdsMeta) +template void merge(std::set& dst,const std::set& src) { for(auto it(src.begin());it!=src.end();++it) dst.insert(*it) ; } + +void RsGxsUpdateBroadcastBase::updateBroadcastMsgsChanged(const std::map > &msgIds, const std::map > &msgIdsMeta) { - std::map >::const_iterator mapIt; - for (mapIt = msgIds.begin(); mapIt != msgIds.end(); ++mapIt) { - const RsGxsGroupId &grpId = mapIt->first; - const std::vector &srcMsgIds = mapIt->second; - std::vector &destMsgIds = mMsgIds[grpId]; + for (auto mapIt = msgIds.begin(); mapIt != msgIds.end(); ++mapIt) + merge(mMsgIds[mapIt->first],mapIt->second) ; - std::vector::const_iterator msgIt; - for (msgIt = srcMsgIds.begin(); msgIt != srcMsgIds.end(); ++msgIt) { - if (std::find(destMsgIds.begin(), destMsgIds.end(), *msgIt) == destMsgIds.end()) { - destMsgIds.push_back(*msgIt); - } - } - } - for (mapIt = msgIdsMeta.begin(); mapIt != msgIdsMeta.end(); ++mapIt) { - const RsGxsGroupId &grpId = mapIt->first; - const std::vector &srcMsgIds = mapIt->second; - std::vector &destMsgIds = mMsgIdsMeta[grpId]; - - std::vector::const_iterator msgIt; - for (msgIt = srcMsgIds.begin(); msgIt != srcMsgIds.end(); ++msgIt) { - if (std::find(destMsgIds.begin(), destMsgIds.end(), *msgIt) == destMsgIds.end()) { - destMsgIds.push_back(*msgIt); - } - } - } + for (auto mapIt = msgIdsMeta.begin(); mapIt != msgIdsMeta.end(); ++mapIt) + merge(mMsgIdsMeta[mapIt->first],mapIt->second) ; } -void RsGxsUpdateBroadcastBase::getAllGrpIds(std::list &grpIds) +void RsGxsUpdateBroadcastBase::getAllGrpIds(std::set &grpIds) { - std::list::const_iterator it; - for (it = mGrpIds.begin(); it != mGrpIds.end(); ++it) { - if (std::find(grpIds.begin(), grpIds.end(), *it) == grpIds.end()) { - grpIds.push_back(*it); - } - } - for (it = mGrpIdsMeta.begin(); it != mGrpIdsMeta.end(); ++it) { - if (std::find(grpIds.begin(), grpIds.end(), *it) == grpIds.end()) { - grpIds.push_back(*it); - } - } + grpIds = mGrpIds; + merge(grpIds,mGrpIdsMeta) ; } -void RsGxsUpdateBroadcastBase::getAllMsgIds(std::map > &msgIds) +void RsGxsUpdateBroadcastBase::getAllMsgIds(std::map > &msgIds) { /* Copy first map */ msgIds = mMsgIds; /* Append second map */ - std::map >::const_iterator mapIt; - for (mapIt = mMsgIdsMeta.begin(); mapIt != mMsgIdsMeta.end(); ++mapIt) { - const RsGxsGroupId &grpId = mapIt->first; - const std::vector &srcMsgIds = mapIt->second; - std::vector &destMsgIds = msgIds[grpId]; - - std::vector::const_iterator msgIt; - for (msgIt = srcMsgIds.begin(); msgIt != srcMsgIds.end(); ++msgIt) { - if (std::find(destMsgIds.begin(), destMsgIds.end(), *msgIt) == destMsgIds.end()) { - destMsgIds.push_back(*msgIt); - } - } - } + for (auto mapIt = mMsgIdsMeta.begin(); mapIt != mMsgIdsMeta.end(); ++mapIt) + merge(msgIds[mapIt->first],mapIt->second); } diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.h b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.h index 9ffa8ff72..90dd3f507 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.h +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastBase.h @@ -19,12 +19,12 @@ public: RsGxsUpdateBroadcastBase(RsGxsIfaceHelper* ifaceImpl, QWidget *parent = NULL); virtual ~RsGxsUpdateBroadcastBase(); - const std::list &getGrpIds() { return mGrpIds; } - const std::list &getGrpIdsMeta() { return mGrpIdsMeta; } - void getAllGrpIds(std::list &grpIds); - const std::map > &getMsgIds() { return mMsgIds; } - const std::map > &getMsgIdsMeta() { return mMsgIdsMeta; } - void getAllMsgIds(std::map > &msgIds); + const std::set &getGrpIds() { return mGrpIds; } + const std::set &getGrpIdsMeta() { return mGrpIdsMeta; } + void getAllGrpIds(std::set &grpIds); + const std::map > &getMsgIds() { return mMsgIds; } + const std::map > &getMsgIdsMeta() { return mMsgIdsMeta; } + void getAllMsgIds(std::map > &msgIds); protected: void fillComplete(); @@ -38,15 +38,15 @@ signals: private slots: void updateBroadcastChanged(); void updateBroadcastGrpsChanged(const std::list& grpIds, const std::list &grpIdsMeta); - void updateBroadcastMsgsChanged(const std::map >& msgIds, const std::map >& msgIdsMeta); + void updateBroadcastMsgsChanged(const std::map >& msgIds, const std::map >& msgIdsMeta); void securedUpdateDisplay(); private: RsGxsUpdateBroadcast *mUpdateBroadcast; bool mFillComplete; bool mUpdateWhenInvisible; // Update also when not visible - std::list mGrpIds; - std::list mGrpIdsMeta; - std::map > mMsgIds; - std::map > mMsgIdsMeta; + std::set mGrpIds; + std::set mGrpIdsMeta; + std::map > mMsgIds; + std::map > mMsgIdsMeta; }; diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.cpp index 5eba14b89..629148688 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.cpp @@ -22,32 +22,32 @@ void RsGxsUpdateBroadcastPage::setUpdateWhenInvisible(bool update) mBase->setUpdateWhenInvisible(update); } -const std::list &RsGxsUpdateBroadcastPage::getGrpIdsMeta() +const std::set &RsGxsUpdateBroadcastPage::getGrpIdsMeta() { return mBase->getGrpIdsMeta(); } -void RsGxsUpdateBroadcastPage::getAllGrpIds(std::list &grpIds) +void RsGxsUpdateBroadcastPage::getAllGrpIds(std::set &grpIds) { mBase->getAllGrpIds(grpIds); } -const std::list &RsGxsUpdateBroadcastPage::getGrpIds() +const std::set &RsGxsUpdateBroadcastPage::getGrpIds() { return mBase->getGrpIds(); } -const std::map > &RsGxsUpdateBroadcastPage::getMsgIdsMeta() +const std::map > &RsGxsUpdateBroadcastPage::getMsgIdsMeta() { return mBase->getMsgIdsMeta(); } -void RsGxsUpdateBroadcastPage::getAllMsgIds(std::map > &msgIds) +void RsGxsUpdateBroadcastPage::getAllMsgIds(std::map > &msgIds) { mBase->getAllMsgIds(msgIds); } -const std::map > &RsGxsUpdateBroadcastPage::getMsgIds() +const std::map > &RsGxsUpdateBroadcastPage::getMsgIds() { return mBase->getMsgIds(); } diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h index 299e7e2a7..0c67f58ff 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastPage.h @@ -24,12 +24,12 @@ public: void fillComplete(); void setUpdateWhenInvisible(bool update); - const std::list &getGrpIds(); - const std::list &getGrpIdsMeta(); - void getAllGrpIds(std::list &grpIds); - const std::map > &getMsgIds(); - const std::map > &getMsgIdsMeta(); - void getAllMsgIds(std::map > &msgIds); + const std::set &getGrpIds(); + const std::set &getGrpIdsMeta(); + void getAllGrpIds(std::set &grpIds); + const std::map > &getMsgIds(); + const std::map > &getMsgIdsMeta(); + void getAllMsgIds(std::map > &msgIds); protected: virtual void showEvent(QShowEvent *event); @@ -37,7 +37,7 @@ protected: // This is overloaded in subclasses. virtual void updateDisplay(bool complete) = 0; -private slots: +public slots: void fillDisplay(bool complete); private: diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp index d2450e719..a6e3e5705 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.cpp @@ -24,32 +24,32 @@ void RsGxsUpdateBroadcastWidget::setUpdateWhenInvisible(bool update) mBase->setUpdateWhenInvisible(update); } -const std::list &RsGxsUpdateBroadcastWidget::getGrpIds() +const std::set &RsGxsUpdateBroadcastWidget::getGrpIds() { return mBase->getGrpIds(); } -const std::list &RsGxsUpdateBroadcastWidget::getGrpIdsMeta() +const std::set &RsGxsUpdateBroadcastWidget::getGrpIdsMeta() { return mBase->getGrpIdsMeta(); } -void RsGxsUpdateBroadcastWidget::getAllGrpIds(std::list &grpIds) +void RsGxsUpdateBroadcastWidget::getAllGrpIds(std::set &grpIds) { mBase->getAllGrpIds(grpIds); } -const std::map > &RsGxsUpdateBroadcastWidget::getMsgIds() +const std::map > &RsGxsUpdateBroadcastWidget::getMsgIds() { return mBase->getMsgIds(); } -const std::map > &RsGxsUpdateBroadcastWidget::getMsgIdsMeta() +const std::map > &RsGxsUpdateBroadcastWidget::getMsgIdsMeta() { return mBase->getMsgIdsMeta(); } -void RsGxsUpdateBroadcastWidget::getAllMsgIds(std::map > &msgIds) +void RsGxsUpdateBroadcastWidget::getAllMsgIds(std::map > &msgIds) { mBase->getAllMsgIds(msgIds); } diff --git a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.h b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.h index 33fcd06bf..f0ab07742 100644 --- a/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.h +++ b/retroshare-gui/src/gui/gxs/RsGxsUpdateBroadcastWidget.h @@ -24,12 +24,12 @@ public: void fillComplete(); void setUpdateWhenInvisible(bool update); - const std::list &getGrpIds(); - const std::list &getGrpIdsMeta(); - void getAllGrpIds(std::list &grpIds); - const std::map > &getMsgIds(); - const std::map > &getMsgIdsMeta(); - void getAllMsgIds(std::map > &msgIds); + const std::set &getGrpIds(); + const std::set &getGrpIdsMeta(); + void getAllGrpIds(std::set &grpIds); + const std::map > &getMsgIds(); + const std::map > &getMsgIdsMeta(); + void getAllMsgIds(std::map > &msgIds); RsGxsIfaceHelper *interfaceHelper() { return mInterfaceHelper; } diff --git a/retroshare-gui/src/gui/gxschannels/CreateGxsChannelMsg.cpp b/retroshare-gui/src/gui/gxschannels/CreateGxsChannelMsg.cpp index cc93d5627..b4470ac5c 100644 --- a/retroshare-gui/src/gui/gxschannels/CreateGxsChannelMsg.cpp +++ b/retroshare-gui/src/gui/gxschannels/CreateGxsChannelMsg.cpp @@ -604,7 +604,7 @@ void CreateGxsChannelMsg::newChannelMsg() GxsMsgReq message_ids; opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; - message_ids[mChannelId].push_back(mOrigPostId); + message_ids[mChannelId].insert(mOrigPostId); mChannelQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_SUMMARY, opts, message_ids, CREATEMSG_CHANNEL_POST_INFO); } } diff --git a/retroshare-gui/src/gui/gxsforums/CreateGxsForumMsg.cpp b/retroshare-gui/src/gui/gxsforums/CreateGxsForumMsg.cpp index 60f34221f..23d827ac3 100644 --- a/retroshare-gui/src/gui/gxsforums/CreateGxsForumMsg.cpp +++ b/retroshare-gui/src/gui/gxsforums/CreateGxsForumMsg.cpp @@ -185,8 +185,8 @@ void CreateGxsForumMsg::newMsg() opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; GxsMsgReq msgIds; - std::vector &vect = msgIds[mForumId]; - vect.push_back(mParentId); + std::set &vect = msgIds[mForumId]; + vect.insert(mParentId); //std::cerr << "ForumsV2Dialog::newMsg() Requesting Parent Summary(" << mParentId << ")"; //std::cerr << std::endl; @@ -205,8 +205,8 @@ void CreateGxsForumMsg::newMsg() opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA; GxsMsgReq msgIds; - std::vector &vect = msgIds[mForumId]; - vect.push_back(mOrigMsgId); + std::set &vect = msgIds[mForumId]; + vect.insert(mOrigMsgId); //std::cerr << "ForumsV2Dialog::newMsg() Requesting Parent Summary(" << mParentId << ")"; //std::cerr << std::endl; diff --git a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp index 5209ca184..7b9ff9183 100644 --- a/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp +++ b/retroshare-gui/src/gui/gxsforums/GxsForumThreadWidget.cpp @@ -404,25 +404,25 @@ void GxsForumThreadWidget::changeEvent(QEvent *e) } } -static void removeMessages(std::map > &msgIds, QList &removeMsgId) +static void removeMessages(std::map > &msgIds, QList &removeMsgId) { QList removedMsgId; - std::map >::iterator grpIt; - for (grpIt = msgIds.begin(); grpIt != msgIds.end(); ) { - std::vector &msgs = grpIt->second; + for (auto grpIt = msgIds.begin(); grpIt != msgIds.end(); ) + { + std::set &msgs = grpIt->second; QList::const_iterator removeMsgIt; for (removeMsgIt = removeMsgId.begin(); removeMsgIt != removeMsgId.end(); ++removeMsgIt) { - std::vector::iterator msgIt = std::find(msgs.begin(), msgs.end(), *removeMsgIt); - if (msgIt != msgs.end()) { + if(msgs.find(*removeMsgIt) != msgs.end()) + { removedMsgId.push_back(*removeMsgIt); - msgs.erase(msgIt); + msgs.erase(*removeMsgIt); } } if (msgs.empty()) { - std::map >::iterator grpItErase = grpIt++; + std::map >::iterator grpItErase = grpIt++; msgIds.erase(grpItErase); } else { ++grpIt; @@ -452,18 +452,18 @@ void GxsForumThreadWidget::updateDisplay(bool complete) } bool updateGroup = false; - const std::list &grpIdsMeta = getGrpIdsMeta(); - if (std::find(grpIdsMeta.begin(), grpIdsMeta.end(), groupId()) != grpIdsMeta.end()) { - updateGroup = true; - } + const std::set &grpIdsMeta = getGrpIdsMeta(); - const std::list &grpIds = getGrpIds(); - if (std::find(grpIds.begin(), grpIds.end(), groupId()) != grpIds.end()) { + if(grpIdsMeta.find(groupId())!=grpIdsMeta.end()) + updateGroup = true; + + const std::set &grpIds = getGrpIds(); + if (grpIds.find(groupId())!=grpIds.end()){ updateGroup = true; /* Update threads */ insertThreads(); } else { - std::map > msgIds; + std::map > msgIds; getAllMsgIds(msgIds); if (!mIgnoredMsgId.empty()) { @@ -2111,8 +2111,8 @@ void GxsForumThreadWidget::flagperson() #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[postId.first]; - vect.push_back(postId.second); + std::set &vect = msgIds[postId.first]; + vect.insert(postId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, token_type); @@ -2413,8 +2413,8 @@ void GxsForumThreadWidget::requestMessageData(const RsGxsGrpMsgIdPair &msgId) #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[msgId.first]; - vect.push_back(msgId.second); + std::set &vect = msgIds[msgId.first]; + vect.insert(msgId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeMessageData); @@ -2464,8 +2464,8 @@ void GxsForumThreadWidget::requestMsgData_ReplyWithPrivateMessage(const RsGxsGrp #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[msgId.first]; - vect.push_back(msgId.second); + std::set &vect = msgIds[msgId.first]; + vect.insert(msgId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeReplyMessage); @@ -2482,8 +2482,8 @@ void GxsForumThreadWidget::requestMsgData_ShowAuthorInPeople(const RsGxsGrpMsgId #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[msgId.first]; - vect.push_back(msgId.second); + std::set &vect = msgIds[msgId.first]; + vect.insert(msgId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeShowAuthorInPeople); @@ -2499,8 +2499,8 @@ void GxsForumThreadWidget::requestMsgData_EditForumMessage(const RsGxsGrpMsgIdPa #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[msgId.first]; - vect.push_back(msgId.second); + std::set &vect = msgIds[msgId.first]; + vect.insert(msgId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeEditForumMessage); @@ -2516,8 +2516,8 @@ void GxsForumThreadWidget::requestMsgData_ReplyForumMessage(const RsGxsGrpMsgIdP #endif GxsMsgReq msgIds; - std::vector &vect = msgIds[msgId.first]; - vect.push_back(msgId.second); + std::set &vect = msgIds[msgId.first]; + vect.insert(msgId.second); uint32_t token; mTokenQueue->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, msgIds, mTokenTypeReplyForumMessage); diff --git a/retroshare-gui/src/gui/qss/stylesheet/qss.default b/retroshare-gui/src/gui/qss/stylesheet/qss.default index 8be8eb2e1..3e08e4d14 100644 --- a/retroshare-gui/src/gui/qss/stylesheet/qss.default +++ b/retroshare-gui/src/gui/qss/stylesheet/qss.default @@ -244,9 +244,15 @@ OpModeStatus[opMode="Minimal"] { background: #FFCCCC; } +/*Property Values at end to overwrite other settings*/ + [new=false] { background: #F8F8F8; } [new=true] { background: #DCECFD; } + +[WrongValue="true"] { + background-color: #FF8080; +} diff --git a/retroshare-gui/src/gui/settings/ServerPage.cpp b/retroshare-gui/src/gui/settings/ServerPage.cpp index 6a019990a..dcfe29426 100755 --- a/retroshare-gui/src/gui/settings/ServerPage.cpp +++ b/retroshare-gui/src/gui/settings/ServerPage.cpp @@ -197,6 +197,8 @@ ServerPage::ServerPage(QWidget * parent, Qt::WindowFlags flags) std::cerr << std::endl; #endif + + connect(ui.discComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(saveAddresses())); connect(ui.netModeComboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(saveAddresses())); connect(ui.localAddress, SIGNAL(textChanged(QString)),this,SLOT(saveAddresses())); connect(ui.extAddress, SIGNAL(textChanged(QString)),this,SLOT(saveAddresses())); @@ -844,10 +846,15 @@ void ServerPage::updateStatus() return; } - /* load up configuration from rsPeers */ - RsPeerDetails detail; - if (!rsPeers->getPeerDetails(rsPeers->getOwnId(), detail)) - return; + /* load up configuration from rsPeers */ + RsPeerDetails detail; + if (!rsPeers->getPeerDetails(rsPeers->getOwnId(), detail)) + { + std::cerr << __PRETTY_FUNCTION__ << " getPeerDetails(...) failed!" + << " This is unexpected report to developers please." + << std::endl; + return; + } /* only update if can't edit */ if (!ui.localPort->isEnabled()) @@ -1005,7 +1012,7 @@ void ServerPage::saveRates() void ServerPage::tabChanged(int page) { - if(page == 2) + if(page == TAB_HIDDEN_SERVICE) updateOutProxyIndicator(); } diff --git a/retroshare-gui/src/gui/settings/ServerPage.ui b/retroshare-gui/src/gui/settings/ServerPage.ui index 36e114a8f..f8f55423f 100755 --- a/retroshare-gui/src/gui/settings/ServerPage.ui +++ b/retroshare-gui/src/gui/settings/ServerPage.ui @@ -26,7 +26,7 @@ - 2 + 0 diff --git a/retroshare-gui/src/gui/settings/TransferPage.cpp b/retroshare-gui/src/gui/settings/TransferPage.cpp index a4c6a1b9d..7899f8422 100644 --- a/retroshare-gui/src/gui/settings/TransferPage.cpp +++ b/retroshare-gui/src/gui/settings/TransferPage.cpp @@ -22,15 +22,17 @@ #include "TransferPage.h" #include "rshare.h" +#include "gui/ShareManager.h" +#include "util/misc.h" + +#include "retroshare/rsiface.h" +#include "retroshare/rsfiles.h" +#include "retroshare/rspeers.h" + +#include #include -#include -#include -#include -#include -#include - TransferPage::TransferPage(QWidget * parent, Qt::WindowFlags flags) : ConfigPage(parent, flags) { @@ -225,11 +227,23 @@ void TransferPage::setIncomingDirectory() return; } - ui.incomingDir->setText(qdir); - std::string dir = ui.incomingDir->text().toUtf8().constData(); - - if(!dir.empty()) - rsFiles->setDownloadDirectory(dir); + std::string dir = qdir.toUtf8().constData(); + if(!dir.empty()) + { + if (!rsFiles->setDownloadDirectory(dir)) + { + ui.incomingDir->setToolTip( tr("Invalid Input. Have you got the right to write on it?") ); + ui.incomingDir->setProperty("WrongValue", true); + } + else + { + ui.incomingDir->setToolTip( "" ); + ui.incomingDir->setProperty("WrongValue", false); + } + } + ui.incomingDir->style()->unpolish(ui.incomingDir); + ui.incomingDir->style()->polish( ui.incomingDir); + whileBlocking(ui.incomingDir)->setText(QString::fromUtf8(rsFiles->getDownloadDirectory().c_str())); } void TransferPage::setPartialsDirectory() @@ -239,11 +253,25 @@ void TransferPage::setPartialsDirectory() return; } - ui.partialsDir->setText(qdir); - std::string dir = ui.partialsDir->text().toUtf8().constData(); + std::string dir = qdir.toUtf8().constData(); if (!dir.empty()) - rsFiles->setPartialsDirectory(dir); + { + if (!rsFiles->setPartialsDirectory(dir)) + { + ui.partialsDir->setToolTip( tr("Invalid Input. It can't be an already shared directory.") ); + ui.partialsDir->setProperty("WrongValue", true); + } + else + { + ui.partialsDir->setToolTip( "" ); + ui.partialsDir->setProperty("WrongValue", false); + } + } + ui.partialsDir->style()->unpolish(ui.partialsDir); + ui.partialsDir->style()->polish( ui.partialsDir); + whileBlocking(ui.partialsDir)->setText(QString::fromUtf8(rsFiles->getPartialsDirectory().c_str())); } + void TransferPage::toggleAutoCheckDirectories(bool b) { ui.autoCheckDirectoriesDelay_SB->setEnabled(b); diff --git a/retroshare-gui/src/gui/settings/rsettingswin.cpp b/retroshare-gui/src/gui/settings/rsettingswin.cpp index 3a7df34c9..0a2b348f0 100644 --- a/retroshare-gui/src/gui/settings/rsettingswin.cpp +++ b/retroshare-gui/src/gui/settings/rsettingswin.cpp @@ -41,12 +41,15 @@ #include "PostedPage.h" #include "PluginsPage.h" #include "ServicePermissionsPage.h" -#include "WebuiPage.h" #include "rsharesettings.h" #include "gui/notifyqt.h" #include "gui/common/FloatingHelpBrowser.h" #include "gui/common/RSElidedItemDelegate.h" +#ifdef ENABLE_WEBUI +# include "WebuiPage.h" +#endif + #define IMAGE_GENERAL ":/images/kcmsystem24.png" #define ITEM_SPACING 2 diff --git a/retroshare-gui/src/gui/settings/rsharesettings.cpp b/retroshare-gui/src/gui/settings/rsharesettings.cpp index 5955cd421..e37216e39 100644 --- a/retroshare-gui/src/gui/settings/rsharesettings.cpp +++ b/retroshare-gui/src/gui/settings/rsharesettings.cpp @@ -34,8 +34,8 @@ #include #include -#if defined(Q_OS_WIN) -#include +#ifdef Q_OS_WIN +# include #endif /* Retroshare's Settings */ diff --git a/retroshare-gui/src/main.cpp b/retroshare-gui/src/main.cpp index 54979f983..acd56d9ef 100644 --- a/retroshare-gui/src/main.cpp +++ b/retroshare-gui/src/main.cpp @@ -41,16 +41,19 @@ #include "gui/FileTransfer/TransfersDialog.h" #include "gui/settings/RsharePeerSettings.h" #include "gui/settings/rsharesettings.h" -#include "gui/settings/WebuiPage.h" #include "idle/idle.h" #include "lang/languagesupport.h" #include "util/RsGxsUpdateBroadcast.h" #include "util/rsdir.h" #include "util/rstime.h" +#ifdef ENABLE_WEBUI +# include "gui/settings/WebuiPage.h" +#endif + #ifdef RETROTOR -#include "TorControl/TorManager.h" -#include "TorControl/TorControlWindow.h" +# include "TorControl/TorManager.h" +# include "TorControl/TorControlWindow.h" #endif #include "retroshare/rsidentity.h" diff --git a/retroshare-gui/src/qss/blacknight.qss b/retroshare-gui/src/qss/blacknight.qss index 1b89a5f4c..3d56385ab 100644 --- a/retroshare-gui/src/qss/blacknight.qss +++ b/retroshare-gui/src/qss/blacknight.qss @@ -293,9 +293,16 @@ OpModeStatus[opMode="Minimal"] { background: #700000; } +/*Property Values at end to overwrite other settings*/ + [new=false] { background: #202020; } [new=true] { background: #005000; } + +[WrongValue=true] { + background-color: #702020; +} + diff --git a/retroshare-gui/src/qss/qdarkstyle.qss b/retroshare-gui/src/qss/qdarkstyle.qss index ccb0d47f7..6c325dc1a 100644 --- a/retroshare-gui/src/qss/qdarkstyle.qss +++ b/retroshare-gui/src/qss/qdarkstyle.qss @@ -1126,9 +1126,15 @@ OpModeStatus[opMode="Minimal"] { background: #700000; } +/*Property Values at end to overwrite other settings*/ + [new=false] { background: #202020; } [new=true] { background: #005000; } + +[WrongValue="true"] { + background-color: #702020; +} diff --git a/retroshare-gui/src/retroshare-gui.pro b/retroshare-gui/src/retroshare-gui.pro index 7a6d8d2a9..48e10a377 100644 --- a/retroshare-gui/src/retroshare-gui.pro +++ b/retroshare-gui/src/retroshare-gui.pro @@ -2,28 +2,29 @@ TEMPLATE = app QT += network xml -CONFIG += qt gui uic qrc resources idle bitdht -CONFIG += link_prl +CONFIG += qt gui uic qrc resources idle CONFIG += console TARGET = retroshare DEFINES += TARGET=\\\"$${TARGET}\\\" -# Plz never commit the .pro with these flags enabled. -# Use this flag when developping new features only. -# -#CONFIG += unfinished -#CONFIG += debug -#DEFINES *= SIGFPE_DEBUG +DEPENDPATH *= $${PWD} $${RS_INCLUDE_DIR} retroshare-gui +INCLUDEPATH *= $${PWD} retroshare-gui -profiling { - QMAKE_CXXFLAGS -= -fomit-frame-pointer - QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer - QMAKE_LFLAGS *= -pg +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 + +libresapihttpserver { + !include("../../libresapi/src/use_libresapi.pri"):error("Including") + HEADERS *= gui/settings/WebuiPage.h + SOURCES *= gui/settings/WebuiPage.cpp + FORMS *= gui/settings/WebuiPage.ui } -retrotor { - DEFINES *= RETROTOR +!include("../../libretroshare/src/use_libretroshare.pri"):error("Including") +retrotor { FORMS += TorControl/TorControlWindow.ui SOURCES += TorControl/TorControlWindow.cpp HEADERS += TorControl/TorControlWindow.h @@ -59,22 +60,6 @@ RCC_DIR = temp/qrc UI_DIR = temp/ui MOC_DIR = temp/moc -#CONFIG += debug -debug { - QMAKE_CFLAGS += -g - QMAKE_CXXFLAGS -= -O2 - QMAKE_CXXFLAGS += -O0 - QMAKE_CFLAGS -= -O2 - QMAKE_CFLAGS += -O0 -} - -DEPENDPATH *= retroshare-gui -INCLUDEPATH *= retroshare-gui - -# treat warnings as error for better removing -#QMAKE_CFLAGS += -Werror -#QMAKE_CXXFLAGS += -Werror - ################################# Linux ########################################## # Put lib dir in QMAKE_LFLAGS so it appears before -L/usr/lib linux-* { @@ -133,7 +118,7 @@ version_detail_bash_script { PRE_TARGETDEPS = write_version_detail write_version_detail.commands = $$PWD/version_detail.sh } - win32 { + win32-* { QMAKE_EXTRA_TARGETS += write_version_detail PRE_TARGETDEPS = write_version_detail write_version_detail.commands = $$PWD/version_detail.bat @@ -165,7 +150,7 @@ win32-x-g++ { #################################### Windows ##################################### -win32 { +win32-g++ { CONFIG(debug, debug|release) { # show console output CONFIG += console @@ -185,8 +170,9 @@ win32 { QMAKE_LFLAGS += -Wl,-nxcompat } - # solve linker warnings because of the order of the libraries - QMAKE_LFLAGS += -Wl,--start-group + # Fix linking error (ld.exe: Error: export ordinal too large) due to too + # many exported symbols. + QMAKE_LFLAGS+=-Wl,--exclude-libs,ALL # Switch off optimization for release version QMAKE_CXXFLAGS_RELEASE -= -O2 @@ -199,15 +185,10 @@ win32 { #QMAKE_CFLAGS_DEBUG += -O2 OBJECTS_DIR = temp/obj - #LIBS += -L"D/Qt/2009.03/qt/plugins/imageformats" - #QTPLUGIN += qjpeg - for(lib, LIB_DIR):LIBS += -L"$$lib" - for(bin, BIN_DIR):LIBS += -L"$$bin" + dLib = ws2_32 gdi32 uuid ole32 iphlpapi crypt32 winmm + LIBS *= $$linkDynamicLibs(dLib) - LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz -lws2_32 - LIBS += -luuid -lole32 -liphlpapi -lcrypt32 -lgdi32 - LIBS += -lwinmm RC_FILE = gui/images/retroshare_win.rc # export symbols for the plugins @@ -220,11 +201,6 @@ win32 { QMAKE_PRE_LINK = $(CHK_DIR_EXISTS) lib || $(MKDIR) lib } - DEFINES *= WINDOWS_SYS WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T - - DEPENDPATH += . $$INC_DIR - INCLUDEPATH += . $$INC_DIR - greaterThan(QT_MAJOR_VERSION, 4) { # Qt 5 RC_INCLUDEPATH += $$_PRO_FILE_PWD_/../../libretroshare/src @@ -309,32 +285,11 @@ openbsd-* { LIBS *= -rdynamic } - - -############################## Common stuff ###################################### - -# On Linux systems that alredy have libssl and libcrypto it is advisable -# to rename the patched version of SSL to something like libsslxpgp.a and libcryptoxpg.a - -# ########################################### - -DEPENDPATH += . $$PWD/../../libretroshare/src/ -INCLUDEPATH += $$PWD/../../libretroshare/src/ - -PRE_TARGETDEPS *= $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a -LIBS *= $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a - wikipoos { PRE_TARGETDEPS *= $$OUT_PWD/../../supportlibs/pegmarkdown/lib/libpegmarkdown.a LIBS *= $$OUT_PWD/../../supportlibs/pegmarkdown/lib/libpegmarkdown.a } -# webinterface -DEPENDPATH += $$PWD/../../libresapi/src -INCLUDEPATH += $$PWD/../../libresapi/src -PRE_TARGETDEPS *= $$OUT_PWD/../../libresapi/src/lib/libresapi.a -LIBS += $$OUT_PWD/../../libresapi/src/lib/libresapi.a - retrotor { HEADERS += TorControl/AddOnionCommand.h \ TorControl/AuthenticateCommand.h \ @@ -439,7 +394,6 @@ HEADERS += rshare.h \ util/stringutil.h \ util/RsNetUtil.h \ util/DateTime.h \ - util/win32.h \ util/RetroStyleLabel.h \ util/dllexport.h \ util/NonCopyable.h \ @@ -619,7 +573,8 @@ HEADERS += rshare.h \ util/imageutil.h \ gui/NetworkDialog/pgpid_item_model.h \ gui/NetworkDialog/pgpid_item_proxy.h \ - gui/common/RsCollection.h + gui/common/RsCollection.h \ + util/retroshareWin32.h # gui/ForumsDialog.h \ # gui/forums/ForumDetails.h \ # gui/forums/EditForumDetails.h \ @@ -803,7 +758,6 @@ SOURCES += main.cpp \ util/stringutil.cpp \ util/RsNetUtil.cpp \ util/DateTime.cpp \ - util/win32.cpp \ util/RetroStyleLabel.cpp \ util/WidgetBackgroundImage.cpp \ util/NonCopyable.cpp \ @@ -981,7 +935,8 @@ SOURCES += main.cpp \ util/imageutil.cpp \ gui/NetworkDialog/pgpid_item_model.cpp \ gui/NetworkDialog/pgpid_item_proxy.cpp \ - gui/common/RsCollection.cpp + gui/common/RsCollection.cpp \ + util/retroshareWin32.cpp # gui/ForumsDialog.cpp \ # gui/forums/ForumDetails.cpp \ # gui/forums/EditForumDetails.cpp \ @@ -1419,9 +1374,3 @@ gxsgui { } - -libresapihttpserver { - HEADERS *= gui/settings/WebuiPage.h - SOURCES *= gui/settings/WebuiPage.cpp - FORMS *= gui/settings/WebuiPage.ui -} diff --git a/retroshare-gui/src/rshare.h b/retroshare-gui/src/rshare.h index fd17b0f13..2d154a361 100644 --- a/retroshare-gui/src/rshare.h +++ b/retroshare-gui/src/rshare.h @@ -27,7 +27,7 @@ #if defined(Q_OS_WIN) #include -#include +#include "util/retroshareWin32.h" #endif #include diff --git a/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp b/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp index cbda167cb..0fdf63d65 100644 --- a/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp +++ b/retroshare-gui/src/util/RsGxsUpdateBroadcast.cpp @@ -50,15 +50,16 @@ void RsGxsUpdateBroadcast::onChangesReceived(const RsGxsChanges& changes) { std::cerr << "Received changes for service " << (void*)changes.mService << ", expecting service " << (void*)mIfaceImpl->getTokenService() << std::endl; - std::cerr << " changes content: " << std::endl; - for(std::list::const_iterator it(changes.mGrps.begin());it!=changes.mGrps.end();++it) std::cerr << " grp id: " << *it << std::endl; - for(std::list::const_iterator it(changes.mGrpsMeta.begin());it!=changes.mGrpsMeta.end();++it) std::cerr << " grp meta: " << *it << std::endl; + for(std::list::const_iterator it(changes.mGrps.begin());it!=changes.mGrps.end();++it) + std::cerr << "[GRP CHANGE] grp id: " << *it << std::endl; + for(std::list::const_iterator it(changes.mGrpsMeta.begin());it!=changes.mGrpsMeta.end();++it) + std::cerr << "[GRP CHANGE] grp meta: " << *it << std::endl; for(std::map >::const_iterator it(changes.mMsgs.begin());it!=changes.mMsgs.end();++it) for(uint32_t i=0;isecond.size();++i) - std::cerr << " grp id: " << it->first << ". Msg ID " << it->second[i] << std::endl; + std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg ID " << it->second[i] << std::endl; for(std::map >::const_iterator it(changes.mMsgsMeta.begin());it!=changes.mMsgsMeta.end();++it) for(uint32_t i=0;isecond.size();++i) - std::cerr << " grp id: " << it->first << ". Msg Meta " << it->second[i] << std::endl; + std::cerr << "[MSG CHANGE] grp id: " << it->first << ". Msg Meta " << it->second[i] << std::endl; } #endif if(changes.mService != mIfaceImpl->getTokenService()) diff --git a/retroshare-gui/src/util/RsGxsUpdateBroadcast.h b/retroshare-gui/src/util/RsGxsUpdateBroadcast.h index d9d90db1f..1622ca606 100644 --- a/retroshare-gui/src/util/RsGxsUpdateBroadcast.h +++ b/retroshare-gui/src/util/RsGxsUpdateBroadcast.h @@ -19,7 +19,7 @@ public: signals: void changed(); - void msgsChanged(const std::map >& msgIds, const std::map >& msgIdsMeta); + void msgsChanged(const std::map >& msgIds, const std::map >& msgIdsMeta); void grpsChanged(const std::list& grpIds, const std::list& grpIdsMeta); private slots: diff --git a/retroshare-gui/src/util/win32.cpp b/retroshare-gui/src/util/retroshareWin32.cpp similarity index 99% rename from retroshare-gui/src/util/win32.cpp rename to retroshare-gui/src/util/retroshareWin32.cpp index b928653fb..5a7f66b8c 100644 --- a/retroshare-gui/src/util/win32.cpp +++ b/retroshare-gui/src/util/retroshareWin32.cpp @@ -33,7 +33,7 @@ #endif #include -#include "win32.h" +#include "retroshareWin32.h" /** Finds the location of the "special" Windows folder using the given CSIDL diff --git a/retroshare-gui/src/util/win32.h b/retroshare-gui/src/util/retroshareWin32.h similarity index 97% rename from retroshare-gui/src/util/win32.h rename to retroshare-gui/src/util/retroshareWin32.h index 5b0dcdca3..8dbb351f3 100644 --- a/retroshare-gui/src/util/win32.h +++ b/retroshare-gui/src/util/retroshareWin32.h @@ -1,7 +1,8 @@ +#pragma once /**************************************************************** - * This file is distributed under the following license: - * - * Copyright (c) 2006-2007, crypton + * This file is distributed under the following license: + * + * Copyright (c) 2006-2007, crypton * Copyright (c) 2006, Matt Edman, Justin Hipple * * This program is free software; you can redistribute it and/or @@ -20,10 +21,6 @@ * Boston, MA 02110-1301, USA. ****************************************************************/ - -#ifndef _WIN32_H -#define _WIN32_H - #include #include @@ -41,6 +38,3 @@ void win32_registry_set_key_value(QString keyLocation, QString keyName, QString /** Removes the key from the registry if it exists */ void win32_registry_remove_key(QString keyLocation, QString keyName); - -#endif - diff --git a/retroshare-nogui/src/retroshare-nogui.pro b/retroshare-nogui/src/retroshare-nogui.pro index 1fdb27b71..6c95d278c 100644 --- a/retroshare-nogui/src/retroshare-nogui.pro +++ b/retroshare-nogui/src/retroshare-nogui.pro @@ -2,24 +2,28 @@ TEMPLATE = app TARGET = retroshare-nogui -CONFIG += bitdht -#CONFIG += introserver CONFIG -= qt xml gui -CONFIG += link_prl -#CONFIG += debug -debug { - QMAKE_CFLAGS -= -O2 - QMAKE_CFLAGS += -O0 - QMAKE_CFLAGS += -g +DEPENDPATH *= $${PWD} $${RS_INCLUDE_DIR} +INCLUDEPATH *= $${PWD} - QMAKE_CXXFLAGS -= -O2 - QMAKE_CXXFLAGS += -O0 - QMAKE_CXXFLAGS += -g +libresapihttpserver { + !include("../../libresapi/src/use_libresapi.pri"):error("Including") + + HEADERS += TerminalApiClient.h + SOURCES += TerminalApiClient.cpp } +!include("../../libretroshare/src/use_libretroshare.pri"):error("Including") + +# when rapidjson is mainstream on all distribs, we will not need the sources anymore +# in the meantime, they are part of the RS directory so that it is always possible to find them + +INCLUDEPATH += ../../rapidjson-1.1.0 + ################################# Linux ########################################## linux-* { + CONFIG += link_pkgconfig #CONFIG += version_detail_bash_script QMAKE_CXXFLAGS *= -D_FILE_OFFSET_BITS=64 @@ -53,42 +57,32 @@ win32-x-g++ { LIBS += -lole32 -lwinmm RC_FILE = gui/images/retroshare_win.rc - - DEFINES *= WIN32 } #################################### Windows ##################################### -win32 { +win32-g++ { CONFIG += console OBJECTS_DIR = temp/obj RCC_DIR = temp/qrc UI_DIR = temp/ui MOC_DIR = temp/moc - # solve linker warnings because of the order of the libraries - QMAKE_LFLAGS += -Wl,--start-group + ## solve linker warnings because of the order of the libraries + #QMAKE_LFLAGS += -Wl,--start-group - CONFIG(debug, debug|release) { - } else { + CONFIG(debug, debug|release) { + } else { # Tell linker to use ASLR protection QMAKE_LFLAGS += -Wl,-dynamicbase # Tell linker to use DEP protection QMAKE_LFLAGS += -Wl,-nxcompat } - for(lib, LIB_DIR):LIBS += -L"$$lib" - LIBS += -lssl -lcrypto -lpthread -lminiupnpc -lz - LIBS += -lcrypto -lws2_32 -lgdi32 - LIBS += -luuid -lole32 -liphlpapi -lcrypt32 - LIBS += -lole32 -lwinmm + dLib = ws2_32 gdi32 uuid ole32 iphlpapi crypt32 winmm + LIBS *= $$linkDynamicLibs(dLib) RC_FILE = resources/retroshare_win.rc - - DEFINES *= WINDOWS_SYS _USE_32BIT_TIME_T - - DEPENDPATH += . $$INC_DIR - INCLUDEPATH += . $$INC_DIR } ##################################### MacOS ###################################### @@ -156,11 +150,6 @@ haiku-* { ############################## Common stuff ###################################### -DEPENDPATH += . $$PWD/../../libretroshare/src -INCLUDEPATH += . $$PWD/../../libretroshare/src - -PRE_TARGETDEPS *= $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a -LIBS *= $$OUT_PWD/../../libretroshare/src/lib/libretroshare.a # Input HEADERS += notifytxt.h @@ -168,19 +157,9 @@ SOURCES += notifytxt.cc \ retroshare.cc introserver { +## Introserver is broken (doesn't compile) should be either fixed or removed + HEADERS += introserver.h SOURCES += introserver.cc DEFINES *= RS_INTRO_SERVER } - -libresapihttpserver { - DEFINES *= ENABLE_WEBUI - PRE_TARGETDEPS *= $$OUT_PWD/../../libresapi/src/lib/libresapi.a - LIBS += $$OUT_PWD/../../libresapi/src/lib/libresapi.a - DEPENDPATH += $$PWD/../../libresapi/src - INCLUDEPATH += $$PWD/../../libresapi/src - HEADERS += \ - TerminalApiClient.h - SOURCES += \ - TerminalApiClient.cpp -} diff --git a/retroshare-qml-app/src/retroshare-qml-app.pro b/retroshare-qml-app/src/retroshare-qml-app.pro index 15054102c..04b8d12ec 100644 --- a/retroshare-qml-app/src/retroshare-qml-app.pro +++ b/retroshare-qml-app/src/retroshare-qml-app.pro @@ -22,7 +22,6 @@ android-* { HEADERS += NativeCalls.h androidplatforminteracions.h SOURCES += NativeCalls.cpp androidplatforminteracions.cpp - ANDROID_EXTRA_LIBS *= $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/libsqlite3.so ANDROID_PACKAGE_SOURCE_DIR = $$PWD/android diff --git a/retroshare.pri b/retroshare.pri index 21c469aa2..405dc83da 100644 --- a/retroshare.pri +++ b/retroshare.pri @@ -1,14 +1,15 @@ +################################################################################ +## Documented build options (CONFIG) goes here as all the rest depend on them ## +## CONFIG must not be edited in other .pro files, aka if CONFIG need do be ##### +## programatically modified depending on platform or from CONFIG itself it ##### +## can be done ONLY inside this file (retroshare.pri) ########################## +################################################################################ + # To disable RetroShare-gui append the following # assignation to qmake command line "CONFIG+=no_retroshare_gui" CONFIG *= retroshare_gui no_retroshare_gui:CONFIG -= retroshare_gui -# To build the RetroTor executable, just uncomment the following option. -# RetroTor is a version of RS that automatically configures Tor for its own usage -# using only hidden nodes. It will not start if Tor is not working. - -# CONFIG *= retrotor - CONFIG *= gxsdistsync # To disable RetroShare-nogui append the following @@ -43,10 +44,10 @@ retroshare_qml_app:CONFIG -= no_retroshare_qml_app CONFIG *= no_libresapilocalserver libresapilocalserver:CONFIG -= no_libresapilocalserver -# To enable Qt dependencies in libresapi append the following -# assignation to qmake command line "CONFIG+=qt_dependencies" -CONFIG *= no_qt_dependencies -qt_dependencies:CONFIG -= no_qt_dependencies +# To enable libresapi settings handler in libresapi append the following +# assignation to qmake command line "CONFIG+=libresapi_settings" +CONFIG *= no_libresapi_settings +libresapi_settings:CONFIG -= no_libresapi_settings # To disable libresapi via HTTP (based on libmicrohttpd) append the following # assignation to qmake command line "CONFIG+=no_libresapihttpserver" @@ -64,6 +65,15 @@ no_sqlcipher:CONFIG -= sqlcipher CONFIG *= no_rs_autologin rs_autologin:CONFIG -= no_rs_autologin +# To build RetroShare Tor only version with automatic hidden node setup append +# the following assignation to qmake command line "CONFIG+=retrotor" +CONFIG *= no_retrotor +retrotor { + CONFIG -= no_retrotor + CONFIG *= rs_onlyhiddennode + DEFINES *= RETROTOR +} + # To have only hidden node generation append the following assignation # to qmake command line "CONFIG+=rs_onlyhiddennode" CONFIG *= no_rs_onlyhiddennode @@ -94,6 +104,11 @@ CONFIG *= rs_gxs_trans CONFIG *= no_rs_async_chat rs_async_chat:CONFIG -= no_rs_async_chat +# To disable bitdht append the following assignation to qmake command line +# "CONFIG+=no_bitdht" +CONFIG *= bitdht +no_bitdht:CONFIG -= bitdht + # To select your MacOsX version append the following assignation to qmake # command line "CONFIG+=rs_macos10.11" where 10.11 depends your version macx:CONFIG *= rs_macos10.11 @@ -102,19 +117,248 @@ rs_macos10.9:CONFIG -= rs_macos10.11 rs_macos10.10:CONFIG -= rs_macos10.11 rs_macos10.12:CONFIG -= rs_macos10.11 +########################################################################################################################################################### +# +# V07_NON_BACKWARD_COMPATIBLE_CHANGE_001: +# +# What: Computes the node id by performing a sha256 hash of the certificate's PGP signature, instead of simply picking up the last 20 bytes of it. +# +# Why: There is no real risk in forging a certificate with the same ID as the authentication is performed over the PGP signature of the certificate +# which hashes the full SSL certificate (i.e. the full serialized CERT_INFO structure). However the possibility to +# create two certificates with the same IDs is a problem, as it can be used to cause disturbance in the software. +# +# Backward compat: connexions impossible with non patched peers older than Nov 2017, probably because the SSL id that is computed is not the same on both side, +# and in particular unpatched peers see a cerficate with ID different (because computed with the old method) than the ID that was +# submitted when making friends. +# +# Note: the advantage of basing the ID on the signature rather than the public key is not very clear, given that the signature is based on a hash +# of the public key (and the rest of the certificate info). +# +# V07_NON_BACKWARD_COMPATIBLE_CHANGE_002: +# +# What: Use RSA+SHA256 instead of RSA+SHA1 for PGP certificate signatures +# +# Why: Sha1 is likely to be prone to primary collisions anytime soon, so it is urgent to turn to a more secure solution. +# +# Backward compat: unpatched peers after Nov 2017 are able to verify signatures since openpgp-sdk already handle it. +# +# V07_NON_BACKWARD_COMPATIBLE_CHANGE_003: +# +# What: Do not hash PGP certificate twice when signing +# +# Why: hasing twice is not per se a security issue, but it makes it harder to change the settings for hashing. +# +# Backward compat: patched peers cannot connect to non patched peers older than Nov 2017. +########################################################################################################################################################### + +#CONFIG += rs_v07_changes +rs_v07_changes { + DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_001 + DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_002 + DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_003 +} + +################################################################################ +## RetroShare qmake functions goes here as all the rest may use them ########### +################################################################################ + +## Qt versions older the 5 are not supported anymore, check if the user is +## attempting use them and fail accordingly with a proper error message +lessThan(QT_MAJOR_VERSION, 5) { + error(Qt 5.0.0 or newer is needed to build RetroShare) +} + +## This function is useful to look for the location of a file in a list of paths +## like the which command on linux, first paramether is the file name, +## second parameter is the name of a variable containing the list of folders +## where to look for. First match is returned. +defineReplace(findFileInPath) { + fileName=$$1 + pathList=$$2 + + for(mDir, $$pathList) { + attempt = $$clean_path($$mDir/$$fileName) + exists($$attempt) { + return($$system_path($$attempt)) + } + } + return() +} + +## This function return linker option to link statically the libraries contained +## in the variable given as paramether. +## Be carefull static library are very susceptible to order +defineReplace(linkStaticLibs) { + libsVarName = $$1 + retSlib = + + for(mLib, $$libsVarName) { + attemptPath=$$findFileInPath(lib$${mLib}.a, QMAKE_LIBDIR) + isEmpty(attemptPath):error(lib$${mLib}.a not found in [$${QMAKE_LIBDIR}]) + + retSlib += -L$$dirname(attemptPath) -l$$mLib + } + + return($$retSlib) +} + +## This function return pretarget deps for the static the libraries contained in +## the variable given as paramether. +defineReplace(pretargetStaticLibs) { + libsVarName = $$1 + + retPreTarget = + + for(mLib, $$libsVarName) { + attemptPath=$$findFileInPath(lib$${mLib}.a, QMAKE_LIBDIR) + isEmpty(attemptPath):error(lib$${mLib}.a not found in [$${QMAKE_LIBDIR}]) + + retPreTarget += $$attemptPath + } + + return($$retPreTarget) +} + +## This function return linker option to link dynamically the libraries +## contained in the variable given as paramether. +defineReplace(linkDynamicLibs) { + libsVarName = $$1 + retDlib = + + for(mLib, $$libsVarName) { + retDlib += -l$$mLib + } + + return($$retDlib) +} + + +################################################################################ +## Statements and variables that depends on build options (CONFIG) goes here ### +################################################################################ +## +## Defining the following variables may be needed depending on platform and +## build options (CONFIG) +## +## PREFIX String variable containing the directory considered as prefix set +## with = operator. +## QMAKE_LIBDIR, INCLUDEPATH Lists variables where qmake will look for includes +## and libraries. Add values using *= operator. +## RS_BIN_DIR, RS_LIB_DIR, RS_INCLUDE_DIR, RS_DATA_DIR, RS_PLUGIN_DIR String +## variables of directories where RetroShare components will be installed, on +## most platforms they are automatically calculated from PREFIX or in other +## ways. +## RS_SQL_LIB String viariable containing the name of the SQL library to use +## ("sqlcipher sqlite3", sqlite3) it is usually precalculated depending on +## CONFIG. +## RS_UPNP_LIB String viariable containing the name of the UPNP library to use +## (miniupnpc, "upnp ixml threadutil") it usually depend on platform. +## RS_THREAD_LIB String viariable containing the name of the multi threading +## library to use (pthread, "") it usually depend on platform. + +gxsdistsync:DEFINES *= RS_USE_GXS_DISTANT_SYNC +wikipoos:DEFINES *= RS_USE_WIKI +rs_gxs:DEFINES *= RS_ENABLE_GXS +libresapilocalserver:DEFINES *= LIBRESAPI_LOCAL_SERVER +libresapi_settings:DEFINES *= LIBRESAPI_SETTINGS +libresapihttpserver:DEFINES *= ENABLE_WEBUI +RS_THREAD_LIB=pthread +RS_UPNP_LIB = upnp ixml threadutil + +sqlcipher { + DEFINES -= NO_SQLCIPHER + RS_SQL_LIB = sqlcipher +} +no_sqlcipher { + DEFINES *= NO_SQLCIPHER + RS_SQL_LIB = sqlite3 +} + +rs_autologin { + DEFINES *= RS_AUTOLOGIN + warning("You have enabled RetroShare auto-login, this is discouraged. The usage of auto-login on some linux distributions may allow someone having access to your session to steal the SSL keys of your node location and therefore compromise your security") +} + +rs_onlyhiddennode { + DEFINES *= RS_ONLYHIDDENNODE + CONFIG -= bitdht + CONFIG *= no_bitdht + message("QMAKE: You have enabled only hidden node.") +} + +no_rs_deprecatedwarning { + QMAKE_CXXFLAGS += -Wno-deprecated + QMAKE_CXXFLAGS += -Wno-deprecated-declarations + DEFINES *= RS_NO_WARN_DEPRECATED + message("QMAKE: You have disabled deprecated warnings.") +} + +no_rs_cppwarning { + QMAKE_CXXFLAGS += -Wno-cpp + DEFINES *= RS_NO_WARN_CPP + message("QMAKE: You have disabled C preprocessor warnings.") +} + +rs_gxs_trans { + DEFINES *= RS_GXS_TRANS + greaterThan(QT_MAJOR_VERSION, 4) { + CONFIG += c++11 + } else { + QMAKE_CXXFLAGS += -std=c++0x + } +} + +rs_async_chat { + DEFINES *= RS_ASYNC_CHAT +} + +rs_chatserver { + DEFINES *= RS_CHATSERVER +} + +debug { + QMAKE_CXXFLAGS -= -O2 -fomit-frame-pointer + QMAKE_CFLAGS -= -O2 -fomit-frame-pointer + + QMAKE_CXXFLAGS *= -O0 -g -fno-omit-frame-pointer + QMAKE_CFLAGS *= -O0 -g -fno-omit-frame-pointer +} + +profiling { + QMAKE_CXXFLAGS -= -fomit-frame-pointer + QMAKE_CFLAGS -= -fomit-frame-pointer + + QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer + QMAKE_CFLAGS *= -pg -g -fno-omit-frame-pointer + + QMAKE_LFLAGS *= -pg +} + +################################################################################ +## Last goes platform specific statements common to all RetroShare subprojects # +################################################################################ linux-* { - isEmpty(PREFIX) { PREFIX = "/usr" } - isEmpty(BIN_DIR) { BIN_DIR = "$${PREFIX}/bin" } - isEmpty(INC_DIR) { INC_DIR = "$${PREFIX}/include/retroshare" } - isEmpty(LIB_DIR) { LIB_DIR = "$${PREFIX}/lib" } - isEmpty(DATA_DIR) { DATA_DIR = "$${PREFIX}/share/retroshare" } - isEmpty(PLUGIN_DIR) { PLUGIN_DIR = "$${LIB_DIR}/retroshare/extensions6" } + isEmpty(PREFIX) : PREFIX = "/usr" + isEmpty(RS_BIN_DIR) : RS_BIN_DIR = "$${PREFIX}/bin" + isEmpty(RS_INCLUDE_DIR): RS_INCLUDE_DIR = "$${PREFIX}/include" + isEmpty(RS_LIB_DIR) : RS_LIB_DIR = "$${PREFIX}/lib" + isEmpty(RS_DATA_DIR) : RS_DATA_DIR = "$${PREFIX}/share/retroshare" + isEmpty(RS_PLUGIN_DIR) : RS_PLUGIN_DIR = "$${RS_LIB_DIR}/retroshare/extensions6" + + QMAKE_LIBDIR *= "$$RS_LIB_DIR" rs_autologin { - !macx { + # try libsecret first since it is not limited to gnome keyring and libgnome-keyring is deprecated + LIBSECRET_AVAILABLE = $$system(pkg-config --exists libsecret-1 && echo yes) + isEmpty(LIBSECRET_AVAILABLE) { + message("using libgnome-keyring for auto login") DEFINES *= HAS_GNOME_KEYRING PKGCONFIG *= gnome-keyring-1 + } else { + message("using libsecret for auto login") + DEFINES *= HAS_LIBSECRET + PKGCONFIG *= libsecret-1 } } } @@ -127,46 +371,71 @@ android-* { CONFIG -= no_retroshare_android_notify_service CONFIG *= retroshare_android_notify_service } - CONFIG *= no_libresapihttpserver upnp_libupnp - CONFIG -= libresapihttpserver upnp_miniupnpc + CONFIG *= no_libresapihttpserver + CONFIG -= libresapihttpserver QT *= androidextras - INCLUDEPATH += $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/include - LIBS *= -L$$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/ + INCLUDEPATH *= $$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/include + QMAKE_LIBDIR *= "$$NATIVE_LIBS_TOOLCHAIN_PATH/sysroot/usr/lib/" + + # The android libc, bionic, provides built-in support for pthreads, + # additional linking (-lpthreads) break linking. + # See https://stackoverflow.com/a/31277163 + RS_THREAD_LIB = } -win32 { - message(***retroshare.pri:Win32) - exists($$PWD/../libs) { - message(Get pre-compiled libraries.) - isEmpty(PREFIX) { PREFIX = "$$PWD/../libs" } - isEmpty(BIN_DIR) { BIN_DIR = "$${PREFIX}/bin" } - isEmpty(INC_DIR) { INC_DIR = "$${PREFIX}/include" } - isEmpty(LIB_DIR) { LIB_DIR = "$${PREFIX}/lib" } - } +win32-g++ { + !isEmpty(EXTERNAL_LIB_DIR) { + message(Use pre-compiled libraries in $${EXTERNAL_LIB_DIR}.) + PREFIX = $$system_path($$EXTERNAL_LIB_DIR) + } - # Check for msys2 - PREFIX_MSYS2 = $$(MINGW_PREFIX) - isEmpty(PREFIX_MSYS2) { - exists(C:/msys32/mingw32/include) { - message(MINGW_PREFIX is empty. Set it in your environment variables.) - message(Found it here:C:\msys32\mingw32) - PREFIX_MSYS2 = "C:\msys32\mingw32" - } - exists(C:/msys64/mingw32/include) { - message(MINGW_PREFIX is empty. Set it in your environment variables.) - message(Found it here:C:\msys64\mingw32) - PREFIX_MSYS2 = "C:\msys64\mingw32" - } - } - !isEmpty(PREFIX_MSYS2) { - message(msys2 is installed.) - BIN_DIR += "$${PREFIX_MSYS2}/bin" - INC_DIR += "$${PREFIX_MSYS2}/include" - LIB_DIR += "$${PREFIX_MSYS2}/lib" - } + PREFIX_MSYS2 = $$(MINGW_PREFIX) + isEmpty(PREFIX_MSYS2) { + message("MINGW_PREFIX is not set, attempting MSYS2 autodiscovery.") + + TEMPTATIVE_MSYS2=$$system_path(C:\\msys32\\mingw32) + exists($$clean_path($${TEMPTATIVE_MSYS2}/include)) { + PREFIX_MSYS2=$${TEMPTATIVE_MSYS2} + } + + TEMPTATIVE_MSYS2=$$system_path(C:\\msys64\\mingw32) + exists($$clean_path($${TEMPTATIVE_MSYS2}/include)) { + PREFIX_MSYS2=$${TEMPTATIVE_MSYS2} + } + + !isEmpty(PREFIX_MSYS2):message(Found MSYS2: $${PREFIX_MSYS2}) + } + + isEmpty(PREFIX):!isEmpty(PREFIX_MSYS2) { + PREFIX = $$system_path($${PREFIX_MSYS2}) + } + + isEmpty(PREFIX) { + error(PREFIX is not set. Set either EXTERNAL_LIB_DIR or PREFIX_MSYS2.) + } + + INCLUDEPATH *= $$system_path($${PREFIX}/include) + !isEmpty(PREFIX_MSYS2) : INCLUDEPATH *= $$system_path($${PREFIX_MSYS2}/include) + + QMAKE_LIBDIR *= $$system_path($${PREFIX}/lib) + !isEmpty(PREFIX_MSYS2) : QMAKE_LIBDIR *= $$system_path($${PREFIX_MSYS2}/lib) + + RS_BIN_DIR = $$system_path($${PREFIX}/bin) + RS_INCLUDE_DIR = $$system_path($${PREFIX}/include) + RS_LIB_DIR = $$system_path($${PREFIX}/lib) + + RS_UPNP_LIB = miniupnpc + + DEFINES *= NOGDI WIN32 WIN32_LEAN_AND_MEAN WINDOWS_SYS _USE_32BIT_TIME_T + + # This defines the platform to be WinXP or later and is needed for + # getaddrinfo (_WIN32_WINNT_WINXP) + DEFINES *= WINVER=0x0501 + + message(***retroshare.pri:Win32 PREFIX $$PREFIX INCLUDEPATH $$INCLUDEPATH QMAKE_LIBDIR $$QMAKE_LIBDIR DEFINES $$DEFINES) } -macx { +macx-* { rs_macos10.8 { message(***retroshare.pri: Set Target and SDK to MacOS 10.8 ) QMAKE_MACOSX_DEPLOYMENT_TARGET=10.8 @@ -207,104 +476,13 @@ macx { LIB_DIR += "/usr/local/lib" LIB_DIR += "/opt/local/lib" CONFIG += c++11 + RS_UPNP_LIB = miniupnpc } -unfinished { - CONFIG += gxscircles - CONFIG += gxsthewire - CONFIG += gxsphotoshare - CONFIG += wikipoos -} -gxsdistsync:DEFINES *= RS_USE_GXS_DISTANT_SYNC -wikipoos:DEFINES *= RS_USE_WIKI -rs_gxs:DEFINES *= RS_ENABLE_GXS -libresapilocalserver:DEFINES *= LIBRESAPI_LOCAL_SERVER -qt_dependencies:DEFINES *= LIBRESAPI_QT -libresapihttpserver:DEFINES *= ENABLE_WEBUI -sqlcipher:DEFINES -= NO_SQLCIPHER -no_sqlcipher:DEFINES *= NO_SQLCIPHER -rs_autologin { - DEFINES *= RS_AUTOLOGIN - warning("You have enabled RetroShare auto-login, this is discouraged. The usage of auto-login on some linux distributions may allow someone having access to your session to steal the SSL keys of your node location and therefore compromise your security") -} - -retrotor { - CONFIG *= rs_onlyhiddennode -} - -rs_onlyhiddennode { - DEFINES *= RS_ONLYHIDDENNODE - warning("QMAKE: You have enabled only hidden node.") -} - -no_rs_deprecatedwarning { - QMAKE_CXXFLAGS += -Wno-deprecated - QMAKE_CXXFLAGS += -Wno-deprecated-declarations - DEFINES *= RS_NO_WARN_DEPRECATED - warning("QMAKE: You have disabled deprecated warnings.") -} - -no_rs_cppwarning { - QMAKE_CXXFLAGS += -Wno-cpp - DEFINES *= RS_NO_WARN_CPP - warning("QMAKE: You have disabled C preprocessor warnings.") -} - -rs_gxs_trans { - DEFINES *= RS_GXS_TRANS - greaterThan(QT_MAJOR_VERSION, 4) { - CONFIG += c++11 - } else { - QMAKE_CXXFLAGS += -std=c++0x - } -} - -rs_async_chat { - DEFINES *= RS_ASYNC_CHAT -} - -rs_chatserver { - DEFINES *= RS_CHATSERVER -} - -########################################################################################################################################################### -# -# V07_NON_BACKWARD_COMPATIBLE_CHANGE_001: -# -# What: Computes the node id by performing a sha256 hash of the certificate's PGP signature, instead of simply picking up the last 20 bytes of it. -# -# Why: There is no real risk in forging a certificate with the same ID as the authentication is performed over the PGP signature of the certificate -# which hashes the full SSL certificate (i.e. the full serialized CERT_INFO structure). However the possibility to -# create two certificates with the same IDs is a problem, as it can be used to cause disturbance in the software. -# -# Backward compat: connexions impossible with non patched peers older than Nov 2017, probably because the SSL id that is computed is not the same on both side, -# and in particular unpatched peers see a cerficate with ID different (because computed with the old method) than the ID that was -# submitted when making friends. -# -# Note: the advantage of basing the ID on the signature rather than the public key is not very clear, given that the signature is based on a hash -# of the public key (and the rest of the certificate info). -# -# V07_NON_BACKWARD_COMPATIBLE_CHANGE_002: -# -# What: Use RSA+SHA256 instead of RSA+SHA1 for PGP certificate signatures -# -# Why: Sha1 is likely to be prone to primary collisions anytime soon, so it is urgent to turn to a more secure solution. -# -# Backward compat: unpatched peers after Nov 2017 are able to verify signatures since openpgp-sdk already handle it. -# -# V07_NON_BACKWARD_COMPATIBLE_CHANGE_003: -# -# What: Do not hash PGP certificate twice when signing -# -# Why: hasing twice is not per se a security issue, but it makes it harder to change the settings for hashing. -# -# Backward compat: patched peers cannot connect to non patched peers older than Nov 2017. -########################################################################################################################################################### - -#CONFIG += rs_v07_changes -rs_v07_changes { - DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_001 - DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_002 - DEFINES += V07_NON_BACKWARD_COMPATIBLE_CHANGE_003 -} +## Retrocompatibility assignations, get rid of this ASAP +isEmpty(BIN_DIR) : BIN_DIR = $${RS_BIN_DIR} +isEmpty(INC_DIR) : INC_DIR = $${RS_INCLUDE_DIR} +isEmpty(LIBDIR) : LIBDIR = $${QMAKE_LIBDIR} +isEmpty(DATA_DIR) : DATA_DIR = $${RS_DATA_DIR} +isEmpty(PLUGIN_DIR): PLUGIN_DIR= $${RS_PLUGIN_DIR} diff --git a/tests/librssimulator/testing/IsolatedServiceTester.h b/tests/librssimulator/testing/IsolatedServiceTester.h index aca9695ed..3fa5e2afc 100644 --- a/tests/librssimulator/testing/IsolatedServiceTester.h +++ b/tests/librssimulator/testing/IsolatedServiceTester.h @@ -4,7 +4,7 @@ #include "retroshare/rsids.h" #include "pqi/p3linkmgr.h" -class RsItem; +struct RsItem; class PeerNode; class RsSerialType; class RsSerialiser; diff --git a/tests/librssimulator/testing/SetFilter.h b/tests/librssimulator/testing/SetFilter.h index 9b9a8b15d..50df26955 100644 --- a/tests/librssimulator/testing/SetFilter.h +++ b/tests/librssimulator/testing/SetFilter.h @@ -4,7 +4,7 @@ #include "retroshare/rsids.h" #include "SetPacket.h" -class RsItem; +struct RsItem; class SetFilter { diff --git a/tests/librssimulator/testing/SetPacket.h b/tests/librssimulator/testing/SetPacket.h index ae529f37b..1e377191e 100644 --- a/tests/librssimulator/testing/SetPacket.h +++ b/tests/librssimulator/testing/SetPacket.h @@ -5,7 +5,7 @@ #include #include "retroshare/rsids.h" -class RsItem; +struct RsItem; class SetPacket { diff --git a/tests/librssimulator/testing/SetServiceTester.h b/tests/librssimulator/testing/SetServiceTester.h index ad03aed4e..862fc43e0 100644 --- a/tests/librssimulator/testing/SetServiceTester.h +++ b/tests/librssimulator/testing/SetServiceTester.h @@ -8,7 +8,7 @@ #include "SetPacket.h" #include "SetFilter.h" -class RsItem; +struct RsItem; class PeerNode; class RsSerialType; class RsSerialiser; diff --git a/tests/unittests/libretroshare/serialiser/rsconfigitem_test.cc b/tests/unittests/libretroshare/serialiser/rsconfigitem_test.cc index e74a215c6..9d270a345 100644 --- a/tests/unittests/libretroshare/serialiser/rsconfigitem_test.cc +++ b/tests/unittests/libretroshare/serialiser/rsconfigitem_test.cc @@ -29,7 +29,7 @@ RsSerialType* init_item(RsPeerNetItem& rpn) { randString(SHORT_STR, rpn.dyndns); - rpn.peerId.random(); + rpn.nodePeerId.random(); rpn.pgpId.random(); randString(SHORT_STR, rpn.location); @@ -165,7 +165,7 @@ bool operator==(const RsPeerNetItem& left, const RsPeerNetItem& right) if(left.dyndns != right.dyndns) return false; if(left.pgpId != right.pgpId) return false; if(left.location != right.location) return false; - if(left.peerId != right.peerId) return false; + if(left.nodePeerId != right.nodePeerId) return false; if(left.lastContact != right.lastContact) return false; if(left.netMode != right.netMode) return false; if(left.visState != right.visState) return false; diff --git a/tests/unittests/unittests.pro b/tests/unittests/unittests.pro index 790ab1c6d..c17be5b77 100644 --- a/tests/unittests/unittests.pro +++ b/tests/unittests/unittests.pro @@ -1,6 +1,6 @@ !include("../../retroshare.pri"): error("Could not include file ../../retroshare.pri") -QT += network xml script +QT += network xml CONFIG += bitdht CONFIG += gxs debug