diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 576f6c90..00000000 --- a/.dockerignore +++ /dev/null @@ -1,7 +0,0 @@ -replica.key -ssl/** -config/** -data/** -build/** -samples/** -venv/** diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 40470f0d..00000000 --- a/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -*.zip filter=lfs diff=lfs merge=lfs -text -*.pdf filter=lfs diff=lfs merge=lfs -text diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml new file mode 100644 index 00000000..cee0ec51 --- /dev/null +++ b/.github/workflows/cicd.yml @@ -0,0 +1,203 @@ +name: Rust CI + +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + build: + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, macos-13, windows-latest] + python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"] + + runs-on: ${{ matrix.os }} + + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Set up Rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + + - name: Install Dependencies (Ubuntu) + if: ${{ matrix.os == 'ubuntu-latest' }} + run: | + sudo apt-get update + sudo apt-get install -y build-essential pkg-config libssl-dev make + + - name: Install Dependencies (macOS) + if: startsWith(matrix.os, 'macos-') + run: | + brew update + brew install openssl + + - name: Enable Developer Command Prompt (Windows) + if: ${{ matrix.os == 'windows-latest' }} + uses: ilammy/msvc-dev-cmd@v1.7.0 + + - name: Install Python Build Tools + run: | + python -m pip install --upgrade pip + python -m pip install setuptools wheel virtualenv + + - name: Run Unit Tests + run: cargo test + + - name: Build Project (Ubuntu) + if: ${{ matrix.os == 'ubuntu-latest' }} + run: make deb + + - name: Build Project (macOS) + if: startsWith(matrix.os, 'macos-') + run: cargo build --release + + - name: Build Project (Windows) + if: ${{ matrix.os == 'windows-latest' }} + run: cargo build --release + + - name: Build Python Wheels (Ubuntu) + if: ${{ matrix.os == 'ubuntu-latest' }} + run: | + python -m venv venv + . venv/bin/activate + cd src/bindings/python/ + pip install maturin[patchelf] + maturin build --release + + - name: Build Python Wheels (MacOS) + if: startsWith(matrix.os, 'macos-') + run: | + python -m venv venv + . venv/bin/activate + cd src/bindings/python/ + pip install maturin + maturin build --release + + - name: Build Python Wheels (Windows) + if: ${{ matrix.os == 'windows-latest' }} + run: | + python -m venv venv + .\venv\Scripts\activate + cd src/bindings/python/ + pip install maturin + maturin build --release + + - name: Create Dist Directory + run: mkdir dist + + - name: Copy Binary (Windows) + if: ${{ matrix.os == 'windows-latest' }} + run: | + copy target\release\binlex.exe dist\ + copy target\release\blyara.exe dist\ + copy target\release\blpdb.exe dist\ + copy target\release\blelfsym.exe dist\ + copy target\release\blmachosym.exe dist\ + copy target\release\blrizin.exe dist\ + copy target\release\blimage.exe dist\ + copy target\release\blhash.exe dist\ + copy target\release\blscaler.exe dist\ + copy target\wheels\*.whl dist\ + + - name: Copy Binary (macOS) + if: startsWith(matrix.os, 'macos-') + run: | + cp target/release/binlex dist/ + cp target/release/blyara dist/ + cp target/release/blpdb dist/ + cp target/release/blelfsym dist/ + cp target/release/blmachosym dist/ + cp target/release/blrizin dist/ + cp target/release/blimage dist/ + cp target/release/blhash dist/ + cp target/release/blscaler dist/ + cp target/wheels/*.whl dist/ + + - name: Copy Binary (Ubuntu) + if: ${{ matrix.os == 'ubuntu-latest' }} + run: | + cp target/release/binlex dist/ + cp target/release/blyara dist/ + cp target/release/blpdb dist/ + cp target/release/blelfsym dist/ + cp target/release/blmachosym dist/ + cp target/release/blrizin dist/ + cp target/release/blimage dist/ + cp target/release/blhash dist/ + cp target/release/blscaler dist/ + cp target/debian/*.deb dist/ + cp target/wheels/*.whl dist/ + + - name: Upload Artifacts + uses: actions/upload-artifact@v3 + with: + name: ${{ matrix.os }}-artifacts + path: dist/ + + archlinux: + runs-on: ubuntu-latest + container: + image: archlinux:latest + steps: + - name: Install Arch Dependencies + run: | + pacman -Syu --noconfirm + pacman -S --noconfirm base-devel curl git + echo '%wheel ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers + useradd -m -G wheel builder + echo 'builder ALL=(ALL) NOPASSWD: /usr/bin/make' >> /etc/sudoers + + - name: Setup Rust + run: | + sudo -u builder bash -c 'curl --proto "=https" --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y' + sudo -u builder bash -c 'echo "export PATH=\\"$PATH:/home/builder/.cargo/bin\\"" >> /home/builder/.bashrc' + sudo -u builder bash -c 'echo "source /home/builder/.cargo/env" >> /home/builder/.bashrc' + + - name: Checkout Code to Builder Home + run: | + sudo -u builder mkdir -p /home/builder/repo + sudo -u builder bash -c "git clone https://github.com/${GITHUB_REPOSITORY}.git /home/builder/repo" + env: + GITHUB_REPOSITORY: ${{ github.repository }} + + - name: Build Project (Arch Linux) + run: | + sudo -u builder bash -c 'cd /home/builder/repo/ && . /home/builder/.cargo/env && make zst' + + - name: Run Unit Tests + run: | + sudo -u builder bash -c 'cd /home/builder/repo/ && . /home/builder/.cargo/env && cargo test' + + - name: Copy Binary (Arch Linux) + run: | + mkdir dist + cp /home/builder/repo/target/release/binlex dist/ + cp /home/builder/repo/target/release/blyara dist/ + cp /home/builder/repo/target/release/blpdb dist/ + cp /home/builder/repo/target/release/blelfsym dist/ + cp /home/builder/repo/target/release/blmachosym dist/ + cp /home/builder/repo/target/release/blrizin dist/ + cp /home/builder/repo/target/release/blimage dist/ + cp /home/builder/repo/target/release/blhash dist/ + cp /home/builder/repo/target/release/blscaler dist/ + cp /home/builder/repo/target/zst/*.zst dist/ + + - name: Upload Artifacts + uses: actions/upload-artifact@v3 + with: + name: archlinux-artifacts + path: dist/ diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml deleted file mode 100644 index 782a043d..00000000 --- a/.github/workflows/cmake.yml +++ /dev/null @@ -1,126 +0,0 @@ -name: CMake - -on: [push, pull_request] - -jobs: - build: - if: ${{ github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository) }} - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - target: - - ubuntu-20.04 - - ubuntu-22.04 - - windows - - macos - include: - - target: ubuntu-20.04 - os: ubuntu-20.04 - build-type: Release - extra-cmake-flags: >- - "-DBUILD_PYTHON_BINDINGS=ON" - - target: ubuntu-22.04 - os: ubuntu-22.04 - build-type: Release - extra-cmake-flags: >- - "-DBUILD_PYTHON_BINDINGS=ON" - - target: windows - os: windows-latest - build-type: Release - extra-cmake-flags: >- - "-DBUILD_PYTHON_BINDINGS=ON" - - target: macos - os: macOS-latest - build-type: Release - extra-cmake-flags: >- - "-DBUILD_PYTHON_BINDINGS=ON" - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - lfs: true - submodules: true - - - name: Install ninja-build tool - uses: seanmiddleditch/gha-setup-ninja@v3 - - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: | - 3.8 - 3.9 - 3.10 - - - name: Enable Developer Command Prompt - if: ${{ runner.os == 'Windows' }} - uses: ilammy/msvc-dev-cmd@v1.7.0 - - - name: Set up GCC - if: ${{ runner.os == 'Linux' }} - uses: egor-tensin/setup-gcc@v1 - with: - version: 9 - - - name: Build Deps - run: | - cmake -B deps/build -S deps - cmake --build deps/build --config ${{ matrix.build-type }} --parallel 4 - - - name: Build - run: | - cmake -B build ${{ matrix.extra-cmake-flags }} - cmake --build build --config ${{ matrix.build-type }} --parallel 4 - cmake --install build --prefix build/install --config ${{ matrix.build-type }} - - - name: Build Python Unix - if: ${{ runner.os != 'Windows' }} - run: | - python3.8 -m pip wheel -v -w ${{ github.workspace }}/build/ . - python3.9 -m pip wheel -v -w ${{ github.workspace }}/build/ . - python3.10 -m pip wheel -v -w ${{ github.workspace }}/build/ . - - - name: Build Python Windows - if: ${{ runner.os == 'Windows' }} - run: | - py -3.8 -m pip wheel -v -w ${{ github.workspace }}/build/ . - py -3.9 -m pip wheel -v -w ${{ github.workspace }}/build/ . - py -3.10 -m pip wheel -v -w ${{ github.workspace }}/build/ . - - - name: Package - run: | - cd build/ && cpack - - - name: Upload artifacts - uses: actions/upload-artifact@v2 - with: - name: ${{ github.event.repository.name }}-${{ matrix.target }} - path: | - build/binlex - build/blyara - build/*.so - build/*.deb - build/*.exe - build/*.dll - build/*.lib - build/*.tar.gz - build/*.rpm - build/*.a - build/pybinlex*.whl - build/*.dmg - - - name: Compress artifacts - uses: vimtor/action-zip@v1 - with: - files: build/install/bin/ - dest: ${{ github.event.repository.name }}-${{ matrix.target }}.zip - - - name: Release - uses: softprops/action-gh-release@v1 - if: ${{ startsWith(github.ref, 'refs/tags/') }} - with: - prerelease: ${{ !startsWith(github.ref, 'refs/tags/v') || contains(github.ref, '-pre') }} - files: ${{ github.event.repository.name }}-${{ matrix.target }}.zip - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 04788d96..b98a4fbb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,31 +1,15 @@ -docs -*~ -build/** -deps/build/** -dist/** -samples/** -windows/** -dump.bin -__pycache__/ +venv/ +dist/ +target/ +project/ +Cargo.lock +samples/ *.yara -*.traits -dump.json -.vscode/** -venv/** -pybinlex.egg-info/** +*.exe +*.dll *.so -*.whl -.idea/ -data/** -docker-compose.yml -ssl/** -*.key -config/** -docker-compose.yml -test/** -.ipynb_checkpoints/** -# Apple crap -.DS_Store -.AppleDouble -.LSOverride -.cache +*.svg +pkg/ +*.tar +*.tar.gz +*.pkg.tar.zst diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 1930ed68..00000000 --- a/.gitmodules +++ /dev/null @@ -1,6 +0,0 @@ -[submodule "docs/theme"] - path = docs/theme - url = https://github.com/jothepro/doxygen-awesome-css.git -[submodule "bindings/python/pybind11"] - path = bindings/python/pybind11 - url = https://github.com/pybind/pybind11.git diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 183db9f9..00000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,189 +0,0 @@ -cmake_minimum_required(VERSION 3.5) - -set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/") - -set(CMAKE_INSTALL_INCLUDEDIR include) - -option(BUILD_PYTHON_BINDINGS "Build python bindings (pybinlex)" OFF) - -# Linking a pybind11 module with a static library without -fPIC will error -if(BUILD_PYTHON_BINDINGS) - set(CMAKE_POSITION_INDEPENDENT_CODE ON) - if(NOT PYBIND11_PYTHON_VERSION) - execute_process( - COMMAND python3 -c "import platform; print(platform.python_version(), end='')" - OUTPUT_VARIABLE PYTHON_VERSION) - set(PYBIND11_PYTHON_VERSION "${PYTHON_VERSION}") - endif() -endif() - -if(WIN32) - # TODO: this can be supported with https://cmake.org/cmake/help/latest/module/GenerateExportHeader.html - set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE) -else() - # NOTE: mutually exclusive with python bindings - option(BUILD_SHARED_LIBS "Build binlex as a shared library (linux only)" OFF) -endif() - -# Enable folder support -set_property(GLOBAL PROPERTY USE_FOLDERS ON) - -project(binlex - VERSION 1.1.1 - DESCRIPTION "A Binary Genetic Traits Lexer and C++ Library" -) - -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - -if(CMAKE_COMPILER_IS_GNUCC) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") -endif() - -include(ExternalProject) -include(ProcessorCount) -ProcessorCount(N) - -file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/tests/tests.py - DESTINATION ${CMAKE_BINARY_DIR} -) - -find_package(CAPSTONE REQUIRED) -find_package(TLSH REQUIRED) -find_package(LIEF REQUIRED) - -add_library(binlex - src/args.cpp - src/raw.cpp - src/common.cpp - src/blelf.cpp - src/auto.cpp - src/disassemblerbase.cpp - src/disassembler.cpp - src/pe-dotnet.cpp - src/cil.cpp - src/pe.cpp - src/file.cpp - src/sha256.c -) - -set_target_properties(binlex PROPERTIES SOVERSION ${PROJECT_VERSION}) - -find_package(Threads REQUIRED) - -target_link_libraries(binlex PUBLIC - lief_static - capstone_static - tlsh_static - Threads::Threads -) - -target_compile_features(binlex PUBLIC cxx_std_11) - -add_library(binlex::library ALIAS binlex) - -if(MSVC) - target_compile_options(binlex PUBLIC /FIiso646.h) -endif() - -target_include_directories(binlex PUBLIC include) - -add_executable(binlex-bin - src/binlex.cpp -) - -target_link_libraries(binlex-bin PRIVATE - binlex::library -) - -set_target_properties(binlex-bin PROPERTIES - OUTPUT_NAME binlex - ARCHIVE_OUTPUT_NAME binlex-bin # TODO: the executable shouldn't have any exports - PDB_NAME binlex-bin -) - -install(TARGETS binlex-bin DESTINATION bin) - -install(TARGETS binlex - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) - -install(DIRECTORY include/ - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) - -set(SOURCES_BLYARA - src/blyara.cpp -) - -add_executable(blyara ${SOURCES_BLYARA}) - -target_include_directories(blyara PRIVATE include) - -target_compile_features(blyara PRIVATE cxx_std_11) - -install(TARGETS blyara DESTINATION bin) - -if (BUILD_PYTHON_BINDINGS) - add_subdirectory(bindings/python/pybind11) - pybind11_add_module(pybinlex MODULE - bindings/python/blelf.cpp - bindings/python/common.cpp - bindings/python/file.cpp - bindings/python/pe.cpp - bindings/python/raw.cpp - bindings/python/disassembler.cpp - bindings/python/pybinlex.cpp - ) - target_link_libraries(pybinlex PRIVATE - binlex::library - ) - install(TARGETS pybinlex DESTINATION bin) -endif() - -add_custom_target(uninstall - "${CMAKE_COMMAND}" -P "${CMAKE_MODULE_PATH}/uninstall.cmake" -) - -set(CPACK_PACKAGE_NAME binlex) -set(PKG_NAME "${CPACK_PACKAGE_NAME}-${PROJECT_VERSION}") -set(CPACK_PACKAGE_FILE_NAME "${PKG_NAME}") -set(CPACK_RESOURCE_FILE_LICENSE "") -set(CPACK_PACKAGE_CONTACT "c3rb3ru5d3d53c@gmail.com") -set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE") -set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md") - -if (UNIX AND NOT APPLE) - set(CPACK_GENERATOR DEB RPM) - set(CPACK_DEBIAN_PACKAGE_MAINTAINER "@c3rb3ru5d3d53c") - set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT) -endif() - -if (APPLE) - set(CPACK_GENERATOR DragNDrop) -endif() - -if (WIN32) - set(CPACK_GENERATOR NSIS64) - set(CPACK_NSIS_MODIFY_PATH ON) - set(CPACK_NSIS_URL_INFO_ABOUT "https://github.com/c3rb3ru5d3d53c/binlex") -endif() - -set(CPACK_SOURCE_GENERATOR TGZ ZIP) -set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PKG_NAME}") -set(CPACK_SOURCE_IGNORE_FILES - "\.git/" - ".*~$" - "\.gitmodules" - "\.gitattributes" - "\.appveyor.yml" - "docker/data/" - "samples/" - "tests/" - "pybinlex.egg-info/" - "*.whl" - "*.so" - "venv/" - "${CMAKE_CURRENT_BINARY_DIR}" - "${CPACK_SOURCE_IGNORE_FILES}" -) - -include(CPack) diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 00000000..3b19ccf7 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,82 @@ +[package] +name = "binlex" +version = "2.0.0" +edition = "2021" +license = "LGPL" +description = "A Binary Genetic Trait Lexer Framework" +authors = ["c3rb3ru5d3d53c"] + +[target.'cfg(target_os = "windows")'.dependencies] +winapi = { version = "0.3.9", features = ["winnt"] } + +[dependencies] +clap = { version = "4.5.20", features = ["derive"] } +lief = "0.15.1" +capstone = "0.12.0" +fast-tlsh = "0.1.6" +ring = "0.17.8" +serde = { version = "1.0.213", features = ["derive"] } +serde_json = "1.0.132" +twox-hash = "2.0.0" +rand = { version = "0.8.5", features = ["small_rng"] } +once_cell = "1.20.2" +rayon = "1.10.0" +crossbeam = "0.8.4" +crossbeam-skiplist = "0.1.3" +lz4 = "1.28.0" +pdb = "0.8.0" +memmap2 = "0.9.5" +dirs = "5.0.1" +toml = "0.8.19" +glob = "0.3.1" + +[workspace] +members = [ + "src/bindings/python/", +] + +[profile.release] +opt-level = 3 +lto = true +codegen-units = 1 + +[[bin]] +name = "binlex" +path = "src/bin/binlex.rs" + +[[bin]] +name = "blyara" +path = "src/bin/blyara.rs" + +[[bin]] +name = "blpdb" +path = "src/bin/blpdb.rs" + +[[bin]] +name = "blscaler" +path = "src/bin/blscaler.rs" + +[[bin]] +name = "blrizin" +path = "src/bin/blrizin.rs" + +[[bin]] +name = "blimage" +path = "src/bin/blimage.rs" + +[[bin]] +name = "blhash" +path = "src/bin/blhash.rs" + +[[bin]] +name = "blcompare" +path = "src/bin/blcompare.rs" + +[[bin]] +name = "blelfsym" +path = "src/bin/blelfsym.rs" + + +[[bin]] +name = "blmachosym" +path = "src/bin/blmachosym.rs" diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 8b1be9dc..00000000 --- a/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM python:3.8 - -ENV HOST "0.0.0.0" -ENV PORT "8080" -ENV THREADS "1" -ENV LOG_LEVEL "info" -ENV WORKER_CONNECTIONS "1000" -ENV TIMEOUT "0" - -COPY . /opt/binlex/ - -WORKDIR /opt/binlex/ - -RUN apt-get -qq -y update && \ - apt-get install -qq -y build-essential make cmake git - -RUN pip install -v . - -CMD ["sh", "-c", "gunicorn -b ${HOST}:${PORT} --timeout ${TIMEOUT} --threads ${THREADS} --worker-connections ${WORKER_CONNECTIONS} --log-level ${LOG_LEVEL} 'libpybinlex.server:app'"] diff --git a/Doxyfile b/Doxyfile deleted file mode 100644 index 7a50b674..00000000 --- a/Doxyfile +++ /dev/null @@ -1,385 +0,0 @@ -# Doxyfile 1.8.17 - -#--------------------------------------------------------------------------- -# Project related configuration options -#--------------------------------------------------------------------------- -DOXYFILE_ENCODING = UTF-8 -PROJECT_NAME = "binlex" -PROJECT_NUMBER = -PROJECT_BRIEF = -PROJECT_LOGO = -OUTPUT_DIRECTORY = build/docs/ -CREATE_SUBDIRS = NO -ALLOW_UNICODE_NAMES = NO -OUTPUT_LANGUAGE = English -OUTPUT_TEXT_DIRECTION = None -BRIEF_MEMBER_DESC = YES -REPEAT_BRIEF = YES -ABBREVIATE_BRIEF = "The $name class" \ - "The $name widget" \ - "The $name file" \ - is \ - provides \ - specifies \ - contains \ - represents \ - a \ - an \ - the -ALWAYS_DETAILED_SEC = NO -INLINE_INHERITED_MEMB = NO -FULL_PATH_NAMES = YES -STRIP_FROM_PATH = -STRIP_FROM_INC_PATH = -SHORT_NAMES = NO -JAVADOC_AUTOBRIEF = NO -JAVADOC_BANNER = NO -QT_AUTOBRIEF = NO -MULTILINE_CPP_IS_BRIEF = NO -INHERIT_DOCS = YES -SEPARATE_MEMBER_PAGES = NO -TAB_SIZE = 4 -ALIASES = -TCL_SUBST = -OPTIMIZE_OUTPUT_FOR_C = NO -OPTIMIZE_OUTPUT_JAVA = NO -OPTIMIZE_FOR_FORTRAN = NO -OPTIMIZE_OUTPUT_VHDL = NO -OPTIMIZE_OUTPUT_SLICE = NO -EXTENSION_MAPPING = -MARKDOWN_SUPPORT = YES -TOC_INCLUDE_HEADINGS = 5 -AUTOLINK_SUPPORT = YES -BUILTIN_STL_SUPPORT = NO -CPP_CLI_SUPPORT = NO -SIP_SUPPORT = NO -IDL_PROPERTY_SUPPORT = YES -DISTRIBUTE_GROUP_DOC = NO -GROUP_NESTED_COMPOUNDS = NO -SUBGROUPING = YES -INLINE_GROUPED_CLASSES = NO -INLINE_SIMPLE_STRUCTS = NO -TYPEDEF_HIDES_STRUCT = NO -LOOKUP_CACHE_SIZE = 0 -#--------------------------------------------------------------------------- -# Build related configuration options -#--------------------------------------------------------------------------- -EXTRACT_ALL = NO -EXTRACT_PRIVATE = NO -EXTRACT_PRIV_VIRTUAL = NO -EXTRACT_PACKAGE = NO -EXTRACT_STATIC = NO -EXTRACT_LOCAL_CLASSES = YES -EXTRACT_LOCAL_METHODS = NO -EXTRACT_ANON_NSPACES = NO -HIDE_UNDOC_MEMBERS = NO -HIDE_UNDOC_CLASSES = NO -HIDE_FRIEND_COMPOUNDS = NO -HIDE_IN_BODY_DOCS = NO -INTERNAL_DOCS = NO -CASE_SENSE_NAMES = YES -HIDE_SCOPE_NAMES = NO -HIDE_COMPOUND_REFERENCE= NO -SHOW_INCLUDE_FILES = YES -SHOW_GROUPED_MEMB_INC = NO -FORCE_LOCAL_INCLUDES = NO -INLINE_INFO = YES -SORT_MEMBER_DOCS = YES -SORT_BRIEF_DOCS = NO -SORT_MEMBERS_CTORS_1ST = NO -SORT_GROUP_NAMES = NO -SORT_BY_SCOPE_NAME = NO -STRICT_PROTO_MATCHING = NO -GENERATE_TODOLIST = YES -GENERATE_TESTLIST = YES -GENERATE_BUGLIST = YES -GENERATE_DEPRECATEDLIST= YES -ENABLED_SECTIONS = -MAX_INITIALIZER_LINES = 30 -SHOW_USED_FILES = YES -SHOW_FILES = YES -SHOW_NAMESPACES = YES -FILE_VERSION_FILTER = -LAYOUT_FILE = -CITE_BIB_FILES = -#--------------------------------------------------------------------------- -# Configuration options related to warning and progress messages -#--------------------------------------------------------------------------- -QUIET = NO -WARNINGS = YES -WARN_IF_UNDOCUMENTED = YES -WARN_IF_DOC_ERROR = YES -WARN_NO_PARAMDOC = NO -WARN_AS_ERROR = NO -WARN_FORMAT = "$file:$line: $text" -WARN_LOGFILE = -#--------------------------------------------------------------------------- -# Configuration options related to the input files -#--------------------------------------------------------------------------- -INPUT = README.md include/ -INPUT_ENCODING = UTF-8 -FILE_PATTERNS = *.c \ - *.cc \ - *.cxx \ - *.cpp \ - *.c++ \ - *.java \ - *.ii \ - *.ixx \ - *.ipp \ - *.i++ \ - *.inl \ - *.idl \ - *.ddl \ - *.odl \ - *.h \ - *.hh \ - *.hxx \ - *.hpp \ - *.h++ \ - *.cs \ - *.d \ - *.php \ - *.php4 \ - *.php5 \ - *.phtml \ - *.inc \ - *.m \ - *.markdown \ - *.md \ - *.mm \ - *.dox \ - *.doc \ - *.txt \ - *.py \ - *.pyw \ - *.f90 \ - *.f95 \ - *.f03 \ - *.f08 \ - *.f \ - *.for \ - *.tcl \ - *.vhd \ - *.vhdl \ - *.ucf \ - *.qsf \ - *.ice -RECURSIVE = NO -EXCLUDE = include/json.h -EXCLUDE_SYMLINKS = NO -EXCLUDE_PATTERNS = -EXCLUDE_SYMBOLS = -EXAMPLE_PATH = -EXAMPLE_PATTERNS = * -EXAMPLE_RECURSIVE = NO -IMAGE_PATH = -INPUT_FILTER = -FILTER_PATTERNS = -FILTER_SOURCE_FILES = NO -FILTER_SOURCE_PATTERNS = -USE_MDFILE_AS_MAINPAGE = README.md -#--------------------------------------------------------------------------- -# Configuration options related to source browsing -#--------------------------------------------------------------------------- -SOURCE_BROWSER = NO -INLINE_SOURCES = NO -STRIP_CODE_COMMENTS = YES -REFERENCED_BY_RELATION = NO -REFERENCES_RELATION = NO -REFERENCES_LINK_SOURCE = YES -SOURCE_TOOLTIPS = YES -USE_HTAGS = NO -VERBATIM_HEADERS = YES -CLANG_ASSISTED_PARSING = NO -CLANG_OPTIONS = -CLANG_DATABASE_PATH = -#--------------------------------------------------------------------------- -# Configuration options related to the alphabetical class index -#--------------------------------------------------------------------------- -ALPHABETICAL_INDEX = YES -COLS_IN_ALPHA_INDEX = 5 -IGNORE_PREFIX = -#--------------------------------------------------------------------------- -# Configuration options related to the HTML output -#--------------------------------------------------------------------------- -GENERATE_HTML = YES -HTML_OUTPUT = html -HTML_FILE_EXTENSION = .html -HTML_HEADER = -HTML_FOOTER = -HTML_STYLESHEET = -HTML_EXTRA_STYLESHEET = docs/theme/doxygen-awesome.css -HTML_EXTRA_FILES = -HTML_COLORSTYLE_HUE = 220 -HTML_COLORSTYLE_SAT = 100 -HTML_COLORSTYLE_GAMMA = 80 -HTML_TIMESTAMP = NO -HTML_DYNAMIC_MENUS = YES -HTML_DYNAMIC_SECTIONS = NO -HTML_INDEX_NUM_ENTRIES = 100 -GENERATE_DOCSET = NO -DOCSET_FEEDNAME = "Doxygen generated docs" -DOCSET_BUNDLE_ID = org.doxygen.Project -DOCSET_PUBLISHER_ID = org.doxygen.Publisher -DOCSET_PUBLISHER_NAME = Publisher -GENERATE_HTMLHELP = NO -CHM_FILE = -HHC_LOCATION = -GENERATE_CHI = NO -CHM_INDEX_ENCODING = -BINARY_TOC = NO -TOC_EXPAND = NO -GENERATE_QHP = NO -QCH_FILE = -QHP_NAMESPACE = org.doxygen.Project -QHP_VIRTUAL_FOLDER = doc -QHP_CUST_FILTER_NAME = -QHP_CUST_FILTER_ATTRS = -QHP_SECT_FILTER_ATTRS = -QHG_LOCATION = -GENERATE_ECLIPSEHELP = NO -ECLIPSE_DOC_ID = org.doxygen.Project -DISABLE_INDEX = NO -GENERATE_TREEVIEW = YES -ENUM_VALUES_PER_LINE = 4 -TREEVIEW_WIDTH = 250 -EXT_LINKS_IN_WINDOW = NO -FORMULA_FONTSIZE = 10 -FORMULA_TRANSPARENT = YES -FORMULA_MACROFILE = -USE_MATHJAX = NO -MATHJAX_FORMAT = HTML-CSS -MATHJAX_RELPATH = https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/ -MATHJAX_EXTENSIONS = -MATHJAX_CODEFILE = -SEARCHENGINE = YES -SERVER_BASED_SEARCH = NO -EXTERNAL_SEARCH = NO -SEARCHENGINE_URL = -SEARCHDATA_FILE = searchdata.xml -EXTERNAL_SEARCH_ID = -EXTRA_SEARCH_MAPPINGS = -#--------------------------------------------------------------------------- -# Configuration options related to the LaTeX output -#--------------------------------------------------------------------------- -GENERATE_LATEX = NO -LATEX_OUTPUT = latex -LATEX_CMD_NAME = -MAKEINDEX_CMD_NAME = makeindex -LATEX_MAKEINDEX_CMD = makeindex -COMPACT_LATEX = NO -PAPER_TYPE = a4 -EXTRA_PACKAGES = -LATEX_HEADER = -LATEX_FOOTER = -LATEX_EXTRA_STYLESHEET = -LATEX_EXTRA_FILES = -PDF_HYPERLINKS = YES -USE_PDFLATEX = YES -LATEX_BATCHMODE = NO -LATEX_HIDE_INDICES = NO -LATEX_SOURCE_CODE = NO -LATEX_BIB_STYLE = plain -LATEX_TIMESTAMP = NO -LATEX_EMOJI_DIRECTORY = -#--------------------------------------------------------------------------- -# Configuration options related to the RTF output -#--------------------------------------------------------------------------- -GENERATE_RTF = NO -RTF_OUTPUT = rtf -COMPACT_RTF = NO -RTF_HYPERLINKS = NO -RTF_STYLESHEET_FILE = -RTF_EXTENSIONS_FILE = -RTF_SOURCE_CODE = NO -#--------------------------------------------------------------------------- -# Configuration options related to the man page output -#--------------------------------------------------------------------------- -GENERATE_MAN = NO -MAN_OUTPUT = man -MAN_EXTENSION = .3 -MAN_SUBDIR = -MAN_LINKS = NO -#--------------------------------------------------------------------------- -# Configuration options related to the XML output -#--------------------------------------------------------------------------- -GENERATE_XML = NO -XML_OUTPUT = xml -XML_PROGRAMLISTING = YES -XML_NS_MEMB_FILE_SCOPE = NO -#--------------------------------------------------------------------------- -# Configuration options related to the DOCBOOK output -#--------------------------------------------------------------------------- -GENERATE_DOCBOOK = NO -DOCBOOK_OUTPUT = docbook -DOCBOOK_PROGRAMLISTING = NO -#--------------------------------------------------------------------------- -# Configuration options for the AutoGen Definitions output -#--------------------------------------------------------------------------- -GENERATE_AUTOGEN_DEF = NO -#--------------------------------------------------------------------------- -# Configuration options related to the Perl module output -#--------------------------------------------------------------------------- -GENERATE_PERLMOD = NO -PERLMOD_LATEX = NO -PERLMOD_PRETTY = YES -PERLMOD_MAKEVAR_PREFIX = -#--------------------------------------------------------------------------- -# Configuration options related to the preprocessor -#--------------------------------------------------------------------------- -ENABLE_PREPROCESSING = YES -MACRO_EXPANSION = NO -EXPAND_ONLY_PREDEF = NO -SEARCH_INCLUDES = YES -INCLUDE_PATH = -INCLUDE_FILE_PATTERNS = -PREDEFINED = -EXPAND_AS_DEFINED = -SKIP_FUNCTION_MACROS = YES -#--------------------------------------------------------------------------- -# Configuration options related to external references -#--------------------------------------------------------------------------- -TAGFILES = -GENERATE_TAGFILE = -ALLEXTERNALS = NO -EXTERNAL_GROUPS = YES -EXTERNAL_PAGES = YES -#--------------------------------------------------------------------------- -# Configuration options related to the dot tool -#--------------------------------------------------------------------------- -CLASS_DIAGRAMS = YES -DIA_PATH = -HIDE_UNDOC_RELATIONS = YES -HAVE_DOT = NO -DOT_NUM_THREADS = 0 -DOT_FONTNAME = Helvetica -DOT_FONTSIZE = 10 -DOT_FONTPATH = -CLASS_GRAPH = YES -COLLABORATION_GRAPH = YES -GROUP_GRAPHS = YES -UML_LOOK = NO -UML_LIMIT_NUM_FIELDS = 10 -TEMPLATE_RELATIONS = NO -INCLUDE_GRAPH = YES -INCLUDED_BY_GRAPH = YES -CALL_GRAPH = NO -CALLER_GRAPH = NO -GRAPHICAL_HIERARCHY = YES -DIRECTORY_GRAPH = YES -DOT_IMAGE_FORMAT = png -INTERACTIVE_SVG = NO -DOT_PATH = -DOTFILE_DIRS = -MSCFILE_DIRS = -DIAFILE_DIRS = -PLANTUML_JAR_PATH = -PLANTUML_CFG_FILE = -PLANTUML_INCLUDE_PATH = -DOT_GRAPH_MAX_NODES = 50 -MAX_DOT_GRAPH_DEPTH = 0 -DOT_TRANSPARENT = NO -DOT_MULTI_TARGETS = NO -GENERATE_LEGEND = YES -DOT_CLEANUP = YES diff --git a/LICENSE b/LICENSE index fdddb29a..0a041280 100644 --- a/LICENSE +++ b/LICENSE @@ -1,24 +1,165 @@ -This is free and unencumbered software released into the public domain. - -Anyone is free to copy, modify, publish, use, compile, sell, or -distribute this software, either in source code form or as a compiled -binary, for any purpose, commercial or non-commercial, and by any -means. - -In jurisdictions that recognize copyright laws, the author or authors -of this software dedicate any and all copyright interest in the -software to the public domain. We make this dedication for the benefit -of the public at large and to the detriment of our heirs and -successors. We intend this dedication to be an overt act of -relinquishment in perpetuity of all present and future rights to this -software under copyright law. - -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 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. - -For more information, please refer to + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/Makefile b/Makefile index b85c5f3e..0f36e3ba 100644 --- a/Makefile +++ b/Makefile @@ -1,65 +1,28 @@ -.PHONY: all -.PHONY: docs - -threads=1 -config=Release -PWD=$(shell pwd) - -all: build docs - -check-config: - @echo "---check-config---" - @if [ -z `echo ${config} | grep 'Release\|Debug'` ]; then \ - echo "[x] config parameter ${config} is invalid" 1>&2; \ - exit 1; \ - fi - -build: check-config - cmake -B build ${args} - cmake --build build --config ${config} --parallel - cmake --install build --prefix build/install --config ${config} - -python-whl: - python3 -m pip wheel -v -w ${PWD}/build/ . - -docs: - mkdir -p build/docs/html/docs/ - cp -r docs/img/ build/docs/html/docs/ - (cat Doxyfile; echo "NUM_PROC_THREADS=${threads}") | doxygen - - -docs-update: - rm -rf docs/html/ - cp -r build/docs/html/ docs/ - -pkg: - cd build/ && \ - cpack - -dist: - cd build/ && \ - make package_source - -install: - cd build/ && \ - make install && \ - ldconfig - -uninstall: - cd build/ && \ - make uninstall +OUTPUT_DIRECTORY = target + +all: + @cargo build --release + +zst: + @cargo build --release + @makepkg + @mkdir -p $(OUTPUT_DIRECTORY)/zst/ + @for file in *.pkg.tar.zst; do \ + echo "Moving $$file to $(OUTPUT_DIRECTORY)/zst/..."; \ + mv "$$file" $(OUTPUT_DIRECTORY)/zst/; \ + done + +deb: + @cargo install cargo-deb + @cargo deb + +wheel: + virtualenv -p python3 venv/ + . venv/bin/activate && \ + cd src/bindings/python/ && \ + pip install maturin[patchelf] && \ + maturin build --release clean: - rm -rf build/ - rm -rf deps/build/ - rm -rf dist/ - rm -rf pybinlex.egg-info/ - rm -f *.so - rm -f *.whl - rm -f docker-compose.yml - rm -rf config/ - rm -rf venv/ - -clean-docker: - @docker stop $(shell docker ps -a -q) 2>/dev/null || echo > /dev/null - @docker rm $(shell docker ps -a -q) 2>/dev/null || echo > /dev/null - @docker rmi $(shell docker images -a -q) 2>/dev/null || echo > /dev/null + @rm -rf pkg/ + @cargo clean diff --git a/PKGBUILD b/PKGBUILD new file mode 100644 index 00000000..aef265af --- /dev/null +++ b/PKGBUILD @@ -0,0 +1,22 @@ +pkgname=binlex +pkgver=$(grep '^version =' Cargo.toml | awk -F\" '{print $2}') +pkgrel=1 +pkgdesc="A Binary Genetic Trait Lexer Framework" +arch=('x86_64') +license=('LGPL') + +build() { + local builddir="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" +} + +package() { + local builddir="$(dirname "$(realpath "${BASH_SOURCE[0]}")")" + install -Dm755 "$builddir/target/release/binlex" "$pkgdir/usr/bin/binlex" + install -Dm755 "$builddir/target/release/blhash" "$pkgdir/usr/bin/blhash" + install -Dm755 "$builddir/target/release/blimage" "$pkgdir/usr/bin/blimage" + install -Dm755 "$builddir/target/release/blmachosym" "$pkgdir/usr/bin/blmachosym" + install -Dm755 "$builddir/target/release/blpdb" "$pkgdir/usr/bin/blpdb" + install -Dm755 "$builddir/target/release/blrizin" "$pkgdir/usr/bin/blrizin" + install -Dm755 "$builddir/target/release/blscaler" "$pkgdir/usr/bin/blscaler" + install -Dm755 "$builddir/target/release/blyara" "$pkgdir/usr/bin/blyara" +} diff --git a/README.md b/README.md index 0aa887aa..b6141302 100644 --- a/README.md +++ b/README.md @@ -1,479 +1,989 @@ -# binlex - -

A Genetic Binary Trait Lexer Library and Utility

- -The purpose of `binlex` is to extract basic blocks and functions as traits from binaries for malware research, hunting and detection. - -Most projects attempting this use Python to generate traits, but it is very slow. - -The design philosophy behind `binlex` is it to keep it simple and extendable. - -The simple command-line interface allows malware researchers and analysts to hunt traits across hundreds or thousands of potentially similar malware saving time and money in production environments. - -While the C++ API allows developers to get creative with their own detection solutions, completely unencumbered by license limitations. - -To help combat malware, we firmly commit our work to the public domain for the greater good of the world. - ![build](https://github.com/c3rb3ru5d3d53c/binlex/actions/workflows/cmake.yml/badge.svg?branch=master) ![OS Linux](https://img.shields.io/badge/os-linux-brightgreen) ![OS Windows](https://img.shields.io/badge/os-windows-brightgreen) ![OS MacOS](https://img.shields.io/badge/os-macos-brightgreen) [![GitHub stars](https://img.shields.io/github/stars/c3rb3ru5d3d53c/binlex)](https://github.com/c3rb3ru5d3d53c/binlex/stargazers) -[![GitHub forks](https://img.shields.io/github/forks/c3rb3ru5d3d53c/binlex)](https://github.com/c3rb3ru5d3d53c/binlex/network) [![Discord Status](https://img.shields.io/discord/915569998469144636?logo=discord)](https://discord.gg/UDBfRpxV3B) [![GitHub license](https://img.shields.io/github/license/c3rb3ru5d3d53c/binlex)](https://github.com/c3rb3ru5d3d53c/binlex/blob/master/LICENSE) ![GitHub all releases](https://img.shields.io/github/downloads/c3rb3ru5d3d53c/binlex/total) + + + + + +
Binlex logo +

Binlex - A Binary Genetic Trait Lexer Framework

+
+ +The purpose of **binlex** is to extract basic blocks and functions as **genomes** from binaries for **malware research**, **hunting**, and **detection**. πŸ¦ πŸ” + +Most projects attempting this use pure Python to generate traits, but it’s often **slow** 🐒. + +The design philosophy behind **binlex** is to keep it **simple** and **extendable**, with an ecosystem of helpful tools and library code. βš™οΈ -## Demos +The simple **command-line interface** allows malware researchers and analysts to hunt for traits across **hundreds** or **thousands** of potentially similar malware samples, saving **time** ⏳ and **money** πŸ’° in production environments. -

- animated -

+The **Rust API** and **Python bindings** let developers create their own detection solutions with minimal **license limitations**. πŸ”“ -

- - Introduction Video - -

+To help combat malware, we provide our work for the greater good. 🌍 -Get slides [here](docs/oalabs.pdf). +No installation needed, just **download the binaries** from the **release page**! πŸ“₯ -## Use Cases -- YARA Signature Creation/Automation -- Identifying Code-Reuse -- Threat Hunting -- Building Goodware Trait Corpus -- Building Malware Trait Corpus -- Genetic Programming -- Machine Learning Malware Detection +## πŸš€ Features -## Installation +The latest version of **binlex** provides the following amazing features! -This part of the guide will show you how to install and use `binlex`. +| Feature | Description | +|---------------------------------|-------------------------------------------------------------------------------------------------| +| 🌐 **Platforms** | - Windows πŸͺŸ
- MacOS 🍏
- Linux 🐧 | +| 🌐 **Formats** | - PE
- MachO
- ELF | +| 🌐 **Architectures** | - AMD64
- I386
- CIL | +| 🧡 **Multi-Threading** | - πŸ”’ Thread-Safe Disassembler Queuing
- πŸš„ Multi-Threaded Tooling for Maximum Efficiency | +| βš™οΈ **Customizable Performance** | Toggle features on/off to optimize for your use case | +| πŸ“‰ **JSON String Compression** | Save memory with JSON compression | +| 🧩 **Similarity Hashing** | - πŸ” Minhash
- πŸ”’ TLSH
- πŸ” SHA256 | +| 🧩 **Function Symbols** | - Pass function symbols to **binlex** as standard input using **blpdb**, **blelfsym** or **blmachosym** or your own tooling | +| 🏷️ **Tagging** | Tagging for easy organization | +| 🎯 **Wildcarding** | Perfect for generating YARA rules and now at a resolution of nibbles! | +| **API** | - πŸ¦€ Rust API
-Python API | +| πŸ€– **Machine Learning Features** | - πŸ“Š Normalized Features for Consistency
- πŸ“ Feature Scaler Utility
- πŸ” Trait Filtering
- πŸ“š Onnx Sample Training
- 🧠 Sample Classification | +| πŸ“‚ **Virtual Imaging** | - Efficient mapping cache for virtual images
- πŸ—„οΈ Compatible with ZFS / BTRFS
- Speeds up repetitive tasks and filtering
- Lightening speed ⚑ | -### Dependencies +By caching virtual images, **binlex** is able to perform at increased speeds, making repeat runs faster and more efficient. -To get started you will need the following dependencies for `binlex`. +## Building -#### Linux +To build **binlex** you will need Rust. + +### Linux, MacOS and Windows + +Installation is straight foward on Linux and MacOS. ```bash -sudo apt install -y git build-essential \ - cmake make parallel \ - doxygen git-lfs rpm \ - python3 python3-dev +cargo build --release ``` -#### macOS - +### Python Bindings ```bash -brew install cmake parallel doxygen git-lfs +cd src/bindings/python/ +virtualenv -p python3 venv/ +source venv/bin/activate +pip install maturin[patchelf] +maturin develop +python +>> import binlex ``` -#### All Platforms +### Packaging + +To build packages for various platforms use the `Makefile`. ```bash -git clone --recursive https://github.com/c3rb3ru5d3d53c/binlex.git -cd binlex/ +make zst # Make Arch Linux Package +make deb # Make Debian Package +make wheel # Make Python Wheel ``` -*NOTE: that `binlex` requires `cmake` >= 3.5, `make` >= 4.2.1 and `ubuntu` >= 20.04.* +The resulting packages will be in the `target/` directory. -Once you have installed, cloned and changed your directory to the project directory, we can continue with installation. +### Documentation -### From Source +```bash +cargo doc +``` -If you want to compile and install use `cmake` with the following commands: +You can also open the docs. ```bash -cmake -B deps/build -S deps -cmake --build deps/build --config Release --parallel 8 -cmake -B build -DBUILD_PYTHON_BINDINGS=ON -cmake --build build --config Release --parallel 8 - -build/binlex -m auto -i tests/elf/elf.x86 +cargo doc --open ``` -### Binary Releases +## Binary Genomes, Chromosomes, Allele Pairs and Genes -See the [`releases`](https://github.com/c3rb3ru5d3d53c/binlex/releases) page. +In **binlex**, a hierarchy of genetic-inspired terms is used to describe and symbolize the structure and traits of binary code. This terminology reflects the relationships between different abstractions and their genetic analogies: -If you need the bleeding edge binaries you can download them from our GitHub Actions [`here`](https://github.com/c3rb3ru5d3d53c/binlex/actions). +- **Genome**: Represents the each object being analyzed, such as a function or block. It encapsulates all the information, including metadata, chromosomes, and other attributes. -*NOTE: bleeding edge binaries are subject to bugs, if you encounter one, please let us know!* +- **Chromosome**: Represents the core patterns or sequences extracted from a block or function. A chromosome acts as the blueprint for identifying key characteristics of the binary without memory addressing as indicated by wildcards like `?`, where a single wildcard represents a single gene. -### Building Packages +- **AllelePair**: A unit within the chromosome consisting of **two genes**. Allele pairs are the building blocks of the chromosome, combining genes into meaningful pairs. -Additionally, another option is to build Debian binary packages for and install those. +- **Gene**: The smallest unit of genetic information, representing a single nibble of data (half a byte). -To build packages use `cpack`, which comes with `cmake`. +The relationship between these abstractions can be visualized as follows: -```bash -cmake -B deps/build -S deps -cmake --build deps/build --config Release --parallel 8 -cmake -B build -DBUILD_PYTHON_BINDINGS=ON -cmake --build build --config Release --parallel 8 -sudo apt install ./build/binlex_1.1.1_amd64.deb -binlex -m elf:x86 -i tests/elf/elf.x86 +```text +Genome (function / block) + └── Chromosome (pattern / sequence) + └── AllelePair (two genes / single byte / two nibbles) + └── Gene (single nibble) ``` -You will then be provided with `.deb`, `.rpm` and `.tar.gz` packages for `binlex`. +### Genome Example -### Building Python Bindings +```JSON +{ + "type": "block", + "architecture": "amd64", + "address": 6442934577, + "next": null, + "to": [], + "edges": 0, + "prologue": false, + "conditional": false, + "chromosome": { + "pattern": "4c8b47??498bc0", + "feature": [4,12,8,11,4,7,4,9,8,11,12,0], + "entropy": 2.2516291673878226, + "sha256": "1f227bf409b0d9fbc576e747de70139a48e42edec60a18fe1e6efdacb598f551", + "minhash": "09b8b1ad1142924519f601854444c6c904a3063942cda4da445721dd0703f290208f3e32451bf5d52741e381a13f12f9142b5de21828a00b2cf90cf77948aac4138443c60bf77ec31199247042694ebb2e4e14a41369eddc7d9f84351be34bcf61458425383a03a55f80cbad420bb6e638550c15876fd0c6208da7b50816847e62d72b2c13a896f4849aa6a36188be1d4a5333865eab570e3939fab1359cbd16758f36fa290164d0259f83c07333df535b2e38f148298db255ac05612cae04d60bb0dd810a91b80a7df9615381e9dc242969dd052691d044287ac2992f9092fa0a75d970100d48362f62b58f7f1d9ec594babdf52f58180c30f4cfca142e76bf", + "tlsh": null + }, + "size": 7, + "bytes": "4c8b4708498bc0", + "functions": {}, + "number_of_instructions": 3, + "entropy": 2.5216406363433186, + "sha256": "84d4485bfd833565fdf41be46c1a499c859f0a5f04c8c99ea9c34404729fd999", + "minhash": "20c995de6a15c8a524fa7e325a6e42b217b636ab03b00812732f877f4739eeee41d7dde92ceac73525e541f9091d8dc928f6425b84a6f44b3f01d17912ec6e8c6f913a760229f685088d2528447e40c768c06d680afe63cb219a1b77a097f679122804dd5a1b9d990aa2579e75f8ef201eeb20d5650da5660efa3a281983a37f28004f9f2a57af8f81728c7d1b02949609c7ad5a30125ff836d8cc3106f2531f306e679a11cabf992556802a3cb2a75a7fe3773e37e3d5ab107a23bf22754aee15a5f41056859b06120f86cb5d39071425855ec90628687741aa0402030d73e04bc60adb0bd2430560442c4309ae258517fc1605438c95485ac4c8621026a1bb", + "tlsh": null, + "contiguous": true, + "attributes": [ + { + "type": "tag", + "value": "corpus:malware" + }, + { + "type": "tag", + "value": "malware:lummastealer" + }, + { + "entropy": 6.55061550644311, + "sha256": "ec1426109420445df8e9799ac21a4c13364dc12229fb16197e428803bece1140", + "size": 725696, + "tlsh": "T17AF48C12AF990595E9BBC23DD1974637FAB2B445232047CF426489BD0E1BBE4B73E381", + "type": "file" + } + ] +} +``` + +Given this JSON genome example. +- **Genome**: The JSON object describing the block, including its metadata, chromosome, and attributes. +- **Chromosome**: as described by the pattern `"4c8b47??498bc0"` +- **AllelePair**: `"4c"` or `"8b"` +- **Gene**: `"4"` or `"c"` + +Using the **binlex** API it is possible to mutate these chromosomes, their allelepairs and genes to facilitate genetic programming. + +Genetic programming in this context can have several benifits including but not limited to: +- Hunting for novel samples given a dataset +- YARA rule generation + +## Command-Line + +The simplest way to get started is with the command-line, leveraging a JSON filtering tool like `jq`. + +The following command disassembles `sample.dll` with `16` threads, the relevant traits are JSON objects, one per line and are piped into `jq` for filtering and beautifying. + +To see what options are available when using the **binlex** command-line use `-h` or `--help`. -To get started using `pybinlex`: ```bash -virtualenv -p python3 venv -source venv/bin/activate -# Install Library -pip install -v . -# Build Wheel Package -pip wheel -v -w build/ . -python3 ->>> import pybinlex +A Binary Pattern Lexer + +Version: 1.0.0 + +Usage: binlex [OPTIONS] --input + +Options: + -i, --input + -o, --output + -c, --config + -t, --threads + --tags + --minimal + -d, --debug + --disable-hashing + --disable-disassembler-sweep + --disable-heuristics + --enable-mmap-cache + --mmap-directory + -h, --help Print help + -V, --version Print version + +Author: @c3rb3ru5d3d53c ``` -If you wish to compile the bindings with `cmake`: +A simple example of using the command-line is provided below. + ```bash -make config=Release threads=4 args=-DBUILD_PYTHON_BINDINGS=ON +binlex -i sample.dll --threads 16 | jq ``` -*NOTE: we use `pybind11` and support for `python3.9` is experimental.* +Please note that **binlex** will detect the file format fort you and currently supports `PE`, `ELF` and `MACHO` binary formats. -Examples of how to use `pybinlex` can be found in `tests/tests.py`. +### Configuration -### Binlex Web API +Upon your first execution of **binlex** it will store the configuration file in your configuration directory in `binlex/binlex.toml`. -```bash -docker build -t binlex:latest . -docker run --rm -p 8080:8080 -e LOG_LEVEL='debug' -it binlex:latest -``` +This **binlex** finds the default configuration directory based on your operating system as indicated in the table below for its configuration. -Browse to `http://127.0.0.1:8080` to view the web API documentation. +| OS | Environment Variable | Example Binlex Configuration Path | +|----------|---------------------------------------|----------------------------------------------------------------| +| Linux | `$XDG_CONFIG_HOME` or `$HOME/.config` | `/home/alice/.config/binlex/binlex.toml` | +| macOS | `$HOME/Library/Application Support` | `/Users/Alice/Library/Application Support/binlex/binlex.toml` | +| Windows | `{FOLDERID_RoamingAppData}` | `C:\Users\Alice\AppData\Roaming\binlex\binlex.toml` | -#### Example Requests +The default configuration name `binlex.toml` for **binlex** is provided below. -```bash -# Get Modes -curl http://127.0.0.1/api/v1/modes +```toml +[general] +threads = 1 +minimal = false +debug = false -# Get Traits -curl -X POST http://127.0.0.1:8080/api/v1/// --upload-file -``` +[formats.file.hashing.sha256] +enabled = true -#### Python Web API Wrapper Example +[formats.file.hashing.tlsh] +enabled = true +minimum_byte_size = 50 -```python -#!/usr/bin/env python - -import json -from libpybinlex.webapi import WebAPIv1 - -api = WebAPIv1(url='http://127.0.0.1:8080') -data = open('sample.bin', 'rb').read() -response = api.get_traits( - data=data, - corpus='default', - mode='pe:x86', - tags=['foo', 'bar']) -traits = json.loads(response.content) -print(json.dumps(traits, indent=4)) +[formats.file.hashing.minhash] +enabled = true +number_of_hashes = 64 +shingle_size = 4 +maximum_byte_size = 50 +seed = 0 + +[formats.file.heuristics.features] +enabled = true + +[formats.file.heuristics.normalized] +enabled = false + +[formats.file.heuristics.entropy] +enabled = true + +[blocks.hashing.sha256] +enabled = true + +[blocks.hashing.tlsh] +enabled = true +minimum_byte_size = 50 + +[blocks.hashing.minhash] +enabled = true +number_of_hashes = 64 +shingle_size = 4 +maximum_byte_size = 50 +seed = 0 + +[blocks.heuristics.features] +enabled = true + +[blocks.heuristics.normalized] +enabled = false + +[blocks.heuristics.entropy] +enabled = true + +[functions.hashing.sha256] +enabled = true + +[functions.hashing.tlsh] +enabled = true +minimum_byte_size = 50 + +[functions.hashing.minhash] +enabled = true +number_of_hashes = 64 +shingle_size = 4 +maximum_byte_size = 50 +seed = 0 + +[functions.heuristics.features] +enabled = true + +[functions.heuristics.normalized] +enabled = false + +[functions.heuristics.entropy] +enabled = true + +[chromosomes.hashing.sha256] +enabled = true + +[chromosomes.hashing.tlsh] +enabled = true +minimum_byte_size = 50 + +[chromosomes.hashing.minhash] +enabled = true +number_of_hashes = 64 +shingle_size = 4 +maximum_byte_size = 50 +seed = 0 + +[chromosomes.heuristics.features] +enabled = true + +[chromosomes.heuristics.normalized] +enabled = false + +[chromosomes.heuristics.entropy] +enabled = true + +[mmap] +directory = "/tmp/binlex" + +[mmap.cache] +enabled = false + +[disassembler.sweep] +enabled = true ``` -## Test Files +If the command-line options are not enough the configuration file provides the most granular control of all options. + +If you wish to override the default configuration file and specify another configuration file use the command-line parameter. + +```bash +binlex -c config.toml -i sample.dll +``` -- To download all the test samples do the command `git lfs fetch` -- ZIP files in the `tests/` directory can then be extracted using the password `infected` +When you run **binlex**, it uses the configuration file and overrides any settings when the respective command-line parameter is used. -*NOTE: The `tests/` directory contains malware, we assume you know what you are doing.* +### Making a YARA Rule -To download individual `git-lfs` files from a relative path, you can use the following `git` alias in `~/.gitconfig`: +Here is a general workflow getting started with making YARA rules, where we get 10 unique wildcarded YARA hex strings from a given sample. -```ini -[alias] -download = "!ROOT=$(git rev-parse --show-toplevel); cd $ROOT; git lfs pull --include $GIT_PREFIX$1; cd $ROOT/$GIT_PREFIX" +```bash +binlex -i sample.dll --threads 16 | jq -r 'select(.size >= 16 and .size <= 32 and .signature.pattern != null) | .signature.pattern' | sort | uniq | head -10 +016b??8b4b??8bc74c6bd858433b4c0b2c0f83c5?????? +01835404????c6836a0400????837e04?? +03c04c8d05????????4863c8420fb60401460fb64401018942??85c074?? +03c38bf0488d140033c9ff15????????488bd84885c075?? +03c6488d55??41ffc58945a?41b804000000418bcce8b8fd01??eb?? +03c6488d55??41ffc58945a?41b804000000418bcce8e3fb01??eb?? +03f7488d05????????4883c310483bd87c?? +03fb4c8bc6498bd7498bcc448d0c7d04000000e89409????8bd84885f6 +03fe448bc6488bd3418bcee8d8e501??85ed +03fe897c24??397c24??0f867301???? ``` -You will then be able to do the following: +To take this a step further you can run it through the `blyara` tool to make a quick YARA signature. ```bash -git download tests/pe/pe.zip +binlex -i sample.dll --threads 16 | jq -r 'select(.size >= 16 and .size <= 32 and .signature.pattern != null) | .signature.pattern' | sort | uniq | head -10 | blyara -n example +rule example { + strings: + $trait_0 = {016b??8b4b??8bc74c6bd858433b4c0b2c0f83c5??????} + $trait_1 = {01835404????c6836a0400????837e04??} + $trait_2 = {03c04c8d05????????4863c8420fb60401460fb64401018942??85c074??} + $trait_3 = {03c38bf0488d140033c9ff15????????488bd84885c075??} + $trait_4 = {03c6488d55??41ffc58945a?41b804000000418bcce8b8fd01??eb??} + $trait_5 = {03c6488d55??41ffc58945a?41b804000000418bcce8e3fb01??eb??} + $trait_6 = {03f7488d05????????4883c310483bd87c??} + $trait_7 = {03fb4c8bc6498bd7498bcc448d0c7d04000000e89409????8bd84885f6} + $trait_8 = {03fe448bc6488bd3418bcee8d8e501??85ed} + $trait_9 = {03fe897c24??397c24??0f867301????} + condition: + 1 of them ``` -## CLI Usage +### Comparing Traits Based on Similarity + +In **binlex** comparisons can be done using the tool `blcompare`, at this time it only supports TLSH similarity hashing. + +All comparisons are one to many, as for every left hand side trait is compared to every right hand side trait. + +These are symbolized as `lhs` and `rhs` respectively. + +The command-line help for `blcompare` is provided below. ```text -binlex v1.1.1 - A Binary Genetic Traits Lexer - -i --input input file (required) - -m --mode set mode (optional) - -lm --list-modes list modes (optional) - -c --corpus corpus name (optional) - -g --tag add a tag (optional) - (can be specified multiple times) - -t --threads number of threads (optional) - -to --timeout execution timeout in s (optional) - -h --help display help (optional) - -o --output output file (optional) - -p --pretty pretty output (optional) - -d --debug print debug info (optional) - -v --version display version (optional) +A Binlex Trait Comparison Tool + +Version: 1.0.0 + +Usage: blcompare [OPTIONS] --input-rhs + +Options: + --input-lhs Input file or wildcard pattern for LHS (Left-Hand Side) + --input-rhs Input file or wildcard pattern for RHS (Right-Hand Side) + -t, --threads Number of threads to use [default: 1] + -r, --recursive Enable recursive wildcard expansion + -h, --help Print help + -V, --version Print version + Author: @c3rb3ru5d3d53c ``` -### Supported Modes +Using this tool you can compare full directories using globbing file searches of lhs traits vs rhs traits. + +This also accepts standard input so you can process something from **binlex** as lhs and then specify a rhs file path glob recursive or not for compairson. + +The output can then be filtered using `jq`. -- `elf:x86` -- `elf:x86_64` -- `pe:x86` -- `pe:x86_64` -- `pe:cil` -- `raw:x86` -- `raw:x86_64` -- `raw:cil` -- `auto` +This tool supports multiple threads and processes one file at a time, but the one to many comparison is multi-threaded. -*NOTE: The `raw` modes can be used on shellcode.* +NOTE: Comparisons should be use sparingly to decide what to keep. By default `blcompare` keeps everything which can be many GB of data. As such it is recommended to perform filtering to only keep similairty within a threshold using `jq`. -*NOTE: The `auto` mode cannot be used on shellcode.* +### Using Ghidra with Binlex -### Advanced +To use **binlex** with ghidra use the `blghidra/blghidra.py` script in the scripts directory. -If you are hunting using `binlex` you can use `jq` to your advantage for advanced searches. +To leverage function names and virtual addresses from your `Ghidra` projects and provide them to **binlex** use the `analyzeHeadless` script in your `Ghidra` install directory. ```bash -build/binlex -m auto -i tests/pe/pe.x86 | jq -r 'select((.size > 8 and .size < 16) and (.bytes_sha256 != .traits.sha256)) | .trait' | head -10 -8b 48 ?? 03 c8 81 39 50 45 00 00 75 12 -0f b7 41 ?? 3d 0b 01 00 00 74 1f -83 b9 ?? ?? ?? ?? ?? 76 f2 -33 c0 39 b9 ?? ?? ?? ?? eb 0e -83 4d ?? ?? b8 ff 00 00 00 e9 ba 00 00 00 -89 75 ?? 66 83 3e 22 75 45 -03 f3 89 75 ?? 66 8b 06 66 3b c7 74 06 -03 f3 89 75 ?? 66 8b 06 66 3b c7 74 06 -56 ff 15 ?? ?? ?? ?? ff 15 ?? ?? ?? ?? eb 2d -55 8b ec 51 56 33 f6 66 89 33 8a 07 eb 29 +./analyzeHeadless \ + \ + \ + -process sample.dll \ + -noanalysis \ + -postscript blghidra.py 2>/dev/null | grep -P "^{\"type" | binlex -i sample.dll ``` -Here are examples of additional queries. +Please note that `analyzeHeadless` prints log messages to `stdout` and other log output to `stderr` that is of no use interoperability with other command-line utilities. + +As such, to collect the output of the script it must be filtered with `2>/dev/null | grep -P "^{\"type"`. + +### Using Rizin with Binlex + +To leverage the power of Rizin function detection and function naming in **binlex**, run `rizin` on your project using `aflj` to list the functions in JSON format. + +Then pipe this output to `blrizin`, which parses `rizin` JSON to a format **binlex** undestands. + +Additionally, you can combine this with other tools like `blpdb` to parse PDB symbols to get function addresses and names. + +You can then do any parsing as you generally would using `jq`, in this example we count the functions processed by **binlex** to see if we are detecting more of them. ```bash -# Block traits with a size between 0 and 32 bytes -jq -r 'select(.type == "block" and .size < 32 and .size > 0)' -# Function traits with a cyclomatic complexity greater than 32 (maybe obfuscation) -jq -r 'select(.type == "function" and .cyclomatic_complexity > 32)' -# Traits where bytes have high entropy -jq -r 'select(.bytes_entropy > 7)' -# Output all trait strings only -jq -r '.trait' -# Output only trait hashes -jq -r '.trait_sha256' +rizin -c 'aaa;aflj;' -q sample.dll | \ + blrizin | \ + blpdb -i sample.pdb | \ + binlex -i sample.dll | \ + jq 'select(.type == "function") | .address' | wc -l ``` -If you output just traits you want to `stdout` you can do build a `yara` signature on the fly with the included tool `blyara`: +**NOTE**: At this time `blrizin` is also compatiable with the output from `radare2` using `blrizin`. + +### Collecting Machine Learning Features + +If you are would like to do some machine learning, you can get features representing the nibbles without memory addressing from binlex like this. ```bash -build/binlex -m raw:x86 -i tests/raw/raw.x86 | jq -r 'select(.size > 16 and .size < 32) | .trait' | build/blyara --name example_0 -m author example -m tlp white -c 1 -rule example_0 { - metadata: - author = "example" - tlp = "white" - strings: - trait_0 = {52 57 8b 52 ?? 8b 42 ?? 01 d0 8b 40 ?? 85 c0 74 4c} - trait_1 = {49 8b 34 8b 01 d6 31 ff 31 c0 c1 cf ?? ac 01 c7 38 e0 75 f4} - trait_2 = {e8 67 00 00 00 6a 00 6a ?? 56 57 68 ?? ?? ?? ?? ff d5 83 f8 00 7e 36} - condition: - 1 of them -} +binlex -i sample.dll --threads 16 | jq -r -c 'select(.size >= 16 and .size <= 32 and .signature.feature != null)| .signature.feature' | head -10 +[4,9,8,11,12,0,4,1,11,9,0,3,0,0,1,15,0,0,4,5,3,3,12,0,8,5,13,2,4,8,8,11,13,0,4,1,0,15,9,5,12,0,4,8,15,15,2,5] +[4,4,8,11,5,1,4,5,3,3,12,0,3,3,12,0,4,8,8,3,12,1,3,0,4,1,0,15,10,3,12,2] +[4,8,8,3,14,12,4,12,8,11,12,10,4,4,8,9,4,4,2,4,11,2,0,1,4,4,0,15,11,7,12,1,8,10,12,10,14,8,5,11,4,8,8,3,12,4,12,3] +[4,8,8,3,14,12,4,4,8,9,4,4,2,4,4,12,8,11,12,10,4,4,0,15,11,7,12,1,11,2,0,1,3,3,12,9,14,8,0,11,4,8,8,3,12,4,12,3] +[4,0,5,3,4,8,8,3,14,12,15,15,1,5,8,11,12,8,8,11,13,8,15,15,1,5,8,11,12,3,4,8,8,3,12,4,5,11,12,3] +[11,9,2,0,0,3,15,14,7,15,4,8,8,11,8,11,0,4,2,5,4,8,0,15,10,15,12,1,4,8,12,1,14,8,1,8,12,3] +[8,11,0,12,2,5,11,8,2,0,0,3,15,14,7,15,4,8,12,1,14,1,2,0,4,8,8,11,4,8,12,1,14,0,0,8,4,8,15,7,14,1,4,8,8,11,12,2,12,3] +[4,8,8,11,0,5,4,8,8,5,12,0,7,5,12,3,4,8,15,15,2,5] +[4,8,8,11,0,13,3,3,12,0,3,8,8,1,11,0,0,8,0,15,9,5,12,0,12,3] +[4,8,8,11,0,5,4,8,8,5,12,0,7,5,12,3,4,8,15,15,2,5] ``` -You can also use the switch `--pretty` to output `json` to identify more properies to query. +If you would like to refine this for your machine learning model by normalizing them between 0 and 1 float values binlex has you covered with the `blscaler` tool. ```bash -build/binlex -m auto -i tests/pe/pe.emotet.x86 -c malware -g malware:emotet -g malware:loader | head -1 | jq -{ - "average_instructions_per_block": 29, - "blocks": 1, - "bytes": "55 8b ec 83 ec 1c 83 65 f0 00 33 d2 c7 45 e4 68 5d df 00 c7 45 e8 43 c4 cb 00 c7 45 ec 8f 08 46 00 c7 45 f8 06 3b 43 00 81 45 f8 25 7a ff ff 81 75 f8 30 f4 44 00 c7 45 fc 22 51 53 00 8b 45 fc 6a 3f 59 f7 f1 6a 1c 89 45 fc 33 d2 8b 45 fc 59 f7 f1 89 45 fc 81 75 fc 3c 95 0e 00 c7 45 f4 0b 16 11 00 81 45 f4 e1 21 ff ff 81 75 f4 79 bd 15 00 ff 4d 0c 75 21", - "bytes_entropy": 5.333979606628418, - "bytes_sha256": "13e0463c5837bc5ce110990d69397662b82b8de8a9971f77b237f2a6dd2d8982", - "corpus": "malware", - "cyclomatic_complexity": 3, - "edges": 2, - "file_sha256": "7b01c7c835552b17f17ad85b8f900c006dd8811d708781b5f49f231448aaccd3", - "file_tlsh": "42E34A10F3D341F7DC9608F219B6B22F9F791E023124DFA987981F57ADB5246A2B981C", - "instructions": 29, - "invalid_instructions": 0, - "mode": "pe:x86", - "offset": 49711, - "size": 118, - "tags": [ - "malware:emotet", - "malware:loader" - ], - "trait": "55 8b ec 83 ec 1c 83 65 ?? ?? 33 d2 c7 45 ?? ?? ?? ?? ?? c7 45 ?? ?? ?? ?? ?? c7 45 ?? ?? ?? ?? ?? c7 45 ?? ?? ?? ?? ?? 81 45 ?? ?? ?? ?? ?? 81 75 ?? ?? ?? ?? ?? c7 45 ?? ?? ?? ?? ?? 8b 45 ?? 6a 3f 59 f7 f1 6a 1c 89 45 ?? 33 d2 8b 45 ?? 59 f7 f1 89 45 ?? 81 75 ?? ?? ?? ?? ?? c7 45 ?? ?? ?? ?? ?? 81 45 ?? ?? ?? ?? ?? 81 75 ?? ?? ?? ?? ?? ff 4d ?? 75 21", - "trait_entropy": 3.9699645042419434, - "trait_sha256": "7b04c2dbcc3cf23abfdd457b592b4517e4d98b5c83e692c836cde5b91899dd68", - "type": "block" -} +binlex -i sample.dll --threads 16 | jq -r -c 'select(.size >= 16 and .size <= 32 and .signature.feature != null)' | blscaler --threads 16 | jq -c -r '.signature.feature' | head -1 +[0.26666666666666666,0.6,0.5333333333333333,0.7333333333333333,0.8,0.0,0.26666666666666666,0.06666666666666667,0.7333333333333333,0.6,0.0,0.2,0.0,0.0,0.06666666666666667,1.0,0.0,0.0,0.26666666666666666,0.3333333333333333,0.2,0.2,0.8,0.0,0.5333333333333333,0.3333333333333333,0.8666666666666667,0.13333333333333333,0.26666666666666666,0.5333333333333333,0.5333333333333333,0.7333333333333333,0.8666666666666667,0.0,0.26666666666666666,0.06666666666666667,0.0,1.0,0.6,0.3333333333333333,0.8,0.0,0.26666666666666666,0.5333333333333333,1.0,1.0,0.13333333333333333,0.3333333333333333] ``` -With `binlex` it is up to you to remove goodware traits from your extracted traits. - -There have been many questions about removing "library code", there is a make target shown below to help you with this. +### Virtual Image File Mapping Cache with Compression +To leverage the powerful feature of filemapping to reduce memory usage but still benifit from virtual images. ```bash -make traits-clean remove=goodware.traits source=sample.traits dest=malware.traits +# Install BTRFS +sudo pacman -S btrfs-progs compsize +# Enable the Kernel Module on Boot +echo "btrfs" | sudo tee /etc/modules-load.d/btrfs.conf +# Reboot +reboot +# Create Virtual Image Cache Storage Pool +dd if=/dev/zero of=btrfs.img bs=1M count=2048 +# Make it BTRFS +mkfs.btrfs btrfs.img +# Make a Cache Directory in /tmp/ +mkdir -p /tmp/binlex/ +# Mount the Cache (Multiple Compression Options Available) +sudo mount -o compress=lzo btrfs.img /tmp/binlex/ +# Run Binlex +binlex -i sample.dll --threads 16 --enable-file-mapping --file-mapping-directory /tmp/binlex/ --enable-file-mapping-cache +sudo compsize ec1426109420445df8e9799ac21a4c13364dc12229fb16197e428803bece1140 +# Virtual Image 6GB vs Stored Size of 192MB +# Processed 1 file, 49156 regular extents (49156 refs), 0 inline. +# Type Perc Disk Usage Uncompressed Referenced +# TOTAL 3% 192M 6.0G 6.0G +# none 100% 384K 384K 384K +# lzo 3% 192M 6.0G 6.0G ``` -With `binlex` the power is in your hands, "With great power comes great responsibility", it is up to you! +This can set this up to be on disk or if `/tmp/` directory is mapped to RAM. -## Plugins +When mapped to RAM, we are taking advantage of virtual image disassembling but without the additional RAM penalty where repetitive tasks almost double in processing speed. -There has been some interest in making IDA, Ghidra and Cutter plugins for `binlex`. +Since `btrfs` abstracts the access to the mapped file in kernel we are able to access it as we would any mapped file but with the benefit of compression. -This is something that will be started soon as we finish the HTTP API endpoints. +To save yourself time if you choose this option, make the mounting of the `btrfs` pool happen on boot and have your **binlex** configuration file set to prefer virtual image caching in the mounted pool directory. This approach ensures that you need not rely on the command-line parameters each time. -This `README.md` will be updated when they are ready to use. +## Binlex API -## General Usage Information +The philophsy of the **binlex** project is focused on security, simplicity, speed and extendability. -Binlex is designed to do one thing and one thing only, extract genetic traits from executable code in files. This means it is up to you "the researcher" / "the data scientist" to determine which traits are good and which traits are bad. To accomplish this, you need to use your own [fitness function](https://en.wikipedia.org/wiki/Fitness_function). I encourage you to read about [genetic programming](https://en.wikipedia.org/wiki/Genetic_programming) to gain a better understanding of this in practice. Perhaps watching [this](https://www.youtube.com/watch?v=qiKW1qX97qA) introductory video will help your understanding. +Part of this is providing an API for developers to write their own detection and hunting logic. -Again, **it's up to you to implement your own algorithms for detection based on the genetic traits you extract**. +At this time, **binlex** provides both Rust and Python bindings. -## Trait Format +### Rust API -Traits will contain binary code represented in hexadecimal form and will use `??` as wild cards for memory operands or other operands subject to change. +The Rust, API makes is easy to get started -They will also contain additional properties about the trait including its `offset`, `edges`, `blocks`, `cyclomatic_complexity`, `average_instruction_per_block`, `bytes`, `trait`, `trait_sha256`, `bytes_sha256`, `trait_entropy`, `bytes_entropy`, `type`, `size`, `invalid_instructions` and `instructions`. +#### Native PE -```json -{ - "average_instructions_per_block": 6, - "blocks": 1, - "bytes": "8b 45 08 a3 10 52 02 10 8b 45 f8 e8 fb d7 00 00 85 c0 74 0d", - "bytes_entropy": 3.9219279289245605, - "bytes_sha256": "435cb166701006282e457d441ca793e795e38790cacc5b250d4bc418a28961c3", - "corpus": "malware", - "cyclomatic_complexity": 3, - "edges": 2, - "file_sha256": "7b01c7c835552b17f17ad85b8f900c006dd8811d708781b5f49f231448aaccd3", - "file_tlsh": "42E34A10F3D341F7DC9608F219B6B22F9F791E023124DFA987981F57ADB5246A2B981C", - "instructions": 6, - "invalid_instructions": 0, - "mode": "pe:x86", - "offset": 49829, - "size": 20, - "tags": [ - "malware:emotet", - "malware:loader" - ], - "trait": "8b 45 ?? a3 ?? ?? ?? ?? 8b 45 ?? e8 fb d7 00 00 85 c0 74 0d", - "trait_entropy": 3.3787841796875, - "trait_sha256": "fe3b057a28b40a02ac9dd2db6c3208f96f7151fb912fb3c562a7b4581bb7f7a0", - "type": "block" +```rs +use std::process; +use binlex::Config; +use binlex::formats::PE; +use binlex::disassemblers::capstone::Disassembler; +use binlex::controlflow::Graph; + +// Get Default Configuration +let mut config = Config(); + +// Use 16 Threads for Multi-Threaded Operations +config.general.threads = 16; + +// Read PE File +let pe = PE.new("./sample.dll", config) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + +// To check if DotNet PE use pe.is_dotnet() + +// Get Memory Mapped File +let mapped_file = pe.image() + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1) + }); + +// Get Mapped File Virtual Image +let image = mapped_file + .mmap() + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + +// Create Disassembler +let disassembler = Disassembler(pe.architecture(), &image, pe.executable_virtual_address_ranges()) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + +// Create Control Flow Graph +let cfg = Graph(pe.architecture(), config); + +// Disassemble Control Flow +disassembler.disassemble_controlflow(pe.entrypoints(), &mut cfg); +``` + +#### .NET (MSIL/CIL) PE + +```rs +use std::process; +use binlex::Config; +use binlex::formats::PE; +use binlex::disassemblers::custom::cil::Disassembler; +use binlex::controlflow::Graph; + +// Get Default Configuration +let mut config = Config(); + +// Use 16 Threads for Multi-Threaded Operations +config.general.threads = 16; + +// Read PE File +let pe = PE.new("./sample.exe", config) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + +// To check if DotNet PE use pe.is_dotnet() + +// Get Memory Mapped File +let mapped_file = pe.image() + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1) + }); + +// Get Mapped File Virtual Image +let image = mapped_file + .mmap() + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + +// Create Disassembler +let disassembler = Disassembler(pe.architecture(), &image, pe.dotnet_executable_virtual_address_ranges()) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + +// Create Control Flow Graph +let cfg = Graph(pe.architecture(), config); + +// Disassemble Control Flow +disassembler.disassemble_controlflow(pe.dotnet_entrypoints(), &mut cfg); +``` + +#### ELF + +```rs +use std::process; +use binlex::Config; +use binlex::formats::ELF; +use binlex::disassemblers::custom::cil::Disassembler; +use binlex::controlflow::Graph; + +// Get Default Configuration +let mut config = Config(); + +// Use 16 Threads for Multi-Threaded Operations +config.general.threads = 16; + +// Read PE File +let elf = ELF.new("./sample.exe", config) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + +// Get Memory Mapped File +let mapped_file = elf.image() + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1) + }); + +// Get Mapped File Virtual Image +let image = mapped_file + .mmap() + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + +// Create Disassembler +let disassembler = Disassembler(elf.architecture(), &image, elf.executable_virtual_address_ranges()) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + +// Create Control Flow Graph +let cfg = Graph(elf.architecture(), config); + +// Disassemble Control Flow +disassembler.disassemble_controlflow(elf.entrypoints(), &mut cfg); +``` + +#### MACHO + +```rs +use std::process; +use binlex::Config; +use binlex::formats::MACHO; +use binlex::disassemblers::custom::cil::Disassembler; +use binlex::controlflow::Graph; + +// Get Default Configuration +let mut config = Config(); + +// Use 16 Threads for Multi-Threaded Operations +config.general.threads = 16; + +// Read PE File +let macho = MACHO.new("./sample.app", config) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + +// Iterate the MACHO Fat Binary Slices +for index in macho.number_of_slices() { + // Get Memory Mapped File + let mapped_file = macho.image(index) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1) + }); + + // Get Mapped File Virtual Image + let image = mapped_file + .mmap() + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + // Create Disassembler + let disassembler = Disassembler(macho.architecture(index), &image, macho.executable_virtual_address_ranges(index)) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + // Create Control Flow Graph + let cfg = Graph(macho.architecture(index), config); + + // Disassemble Control Flow + disassembler.disassemble_controlflow(macho.entrypoints(index), &mut cfg); } + ``` -## Documentation +#### Accessing Genetic Traits -Public documentation on `binlex` can be viewed [here](https://c3rb3ru5d3d53c.github.io/binlex/html/index.html). +```rs +use binlex::controlflow::Instruction; +use binlex::controlflow::Block; +use binlex::controlflow::Function; -### Building Docs +for address in cfg.instructions.valid_addresses() { + // Read Instruction from Control Flow + instruction = Instruction(address, &cfg); -You can access the C++ API Documentation and everything else by building the documents using `doxygen`. + // Print Instruction from Control Flow + instruction.print(); +} -```bash -make docs threads=4 +for address in cfg.blocks.valid_addresses() { + // Read Block from Control Flow + block = Block(address, &cfg); + + // Print Block from Control Flow + block.print(); +} + +for address in cfg.functions.valid_addresses() { + // Read Function from Control Flow + function = Function(address, &cfg); + + // Print Function from Control Flow + function.print(); +} ``` -The documents will be available at `build/docs/html/index.html`. +### Python API -## Example Library Code +The binlex Python API is now designed to abstract the disassembler and the controlflow graph. -As you may already know `binlex` can be used as a `C++` and `Python` library API. +To disassemble a PE memory mapped image use the following examples. -This allows you to write your own detection logic surrounding the data `binlex` extracts. +#### Native PE -### C++ API Example Code +```python +from binlex.formats import PE +from binlex.disassemblers.capstone import Disassembler +from binlex.controlflow import Graph +from binlex import Config -The power of detection is in your hands, `binlex` is a framework, leverage the C++ API. +# Get Default Configuration +config = Config() -```cpp -#include -#include +# Use 16 Threads for Multi-Threaded Operations +config.general.threads = 16 -using namespace binlex; +# Open the PE File +pe = PE('./sample.exe', config) -int main(int argc, char **argv){ - PE pe; - if (pe.ReadFile("example.exe") == false){ - return EXIT_FAILURE; - } - Disassembler disassembler(pe32); - disassembler.Disassemble(); - disassembler.WriteTraits(); - return EXIT_SUCCESS; -} +# To check if a DotNet PE use ps.is_dotnet() + +# Get the Memory Mapped File +mapped_file = pe.image() + +# Get the Memory Map +image = mapped_file.as_memoryview() + +# Create Disassembler on Mapped PE Image and PE Architecture +disassembler = Disassembler(pe.architecture(), image, pe.executable_virtual_address_ranges()) + +# Create the Controlflow Graph +cfg = Graph(pe.architecture(), config) + +# Disassemble the PE Image Entrypoints Recursively +disassembler.disassemble_controlflow(pe.entrypoints(), cfg) ``` -### Python API Example Code +#### .NET (MSIL/CIL) PE +```python +from binlex.formats import PE +from binlex.disassemblers.custom.cil import Disassembler +from binlex.controlflow import Graph +from binlex import Config + + +# Get Default Configuration +config = Config() + +# Use 16 Threads for Multi-Threaded Operations +config.general.threads = 16 + +# Open the PE File +pe = PE('./sample.exe', config) -The power of detection is in your hands, `binlex` is a framework, leverage the C++ API. +# To check if a DotNet PE use ps.is_dotnet() + +# Get the Memory Mapped File +mapped_file = pe.image() + +# Get the Memory Map +image = mapped_file.as_memoryview() + +# Create Disassembler on Mapped PE Image and PE Architecture +disassembler = Disassembler(pe.architecture(), image, pe.dotnet_executable_virtual_address_ranges()) + +# Create the Controlflow Graph +cfg = Graph(pe.architecture(), config) + +# Disassemble the PE Image Entrypoints Recursively +disassembler.disassemble_controlflow(pe.dotnet_entrypoints(), cfg) +``` + +#### ELF ```python -#!/usr/bin/env python - -import sys -import pybinlex - -pe = pybinlex.PE() -result = pe.read_file('example.exe') -if result is False: sys.exit(1) -disassembler = pybinlex.Disassembler(pe) -disassembler.disassemble() -traits = disassembler.get_traits() -print(json.dumps(traits, indent=4)) +from binlex.formats import ELF +from binlex.disassemblers.capstone import Disassembler +from binlex.controlflow import Graph +from binlex import Config + +# Get Default Configuration +config = Config() + +# Use 16 Threads for Multi-Threaded Operations +config.general.threads = 16 + +# Open the ELF File +elf = ELF('./sample.so', config) + +# Get the Memory Mapped File +mapped_file = pe.image() + +# Get the Memory Map +image = mapped_file.as_memoryview() + +# Create Disassembler on Mapped PE Image and PE Architecture +disassembler = Disassembler(elf.architecture(), image, elf.executable_virtual_address_ranges()) + +# Create the Controlflow Graph +cfg = Graph(elf.architecture(), config) + +# Disassemble the PE Image Entrypoints Recursively +disassembler.disassemble_controlflow(elf.entrypoints(), cfg) ``` -We hope this encourages people to build their own detection solutions based on binary genetic traits. +#### MACHO + +```python +from binlex.formats import MACHO +from binlex.disassemblers.capstone import Disassembler +from binlex.controlflow import Graph +from binlex import Config + +# Get Default Configuration +config = Config() + +# Use 16 Threads for Multi-Threaded Operations +config.general.threads = 16 + +# Open the ELF File +macho = MACHO('./sample.app', config) -## Tips -- If you are hunting be sure to use `jq` to improve your searches -- Does not support PE files that are VB6 if you run against these you will get errors -- Don't mix packed and unpacked malware, or you will taint your dataset (seen this in academics all the time) -- When comparing samples to identify if their code is similar, DO NOT mix their architectures in your comparison - - Comparing CIL byte-code or .NET to x86 machine code may yield interesting properties, but they are invalid -- Verify the samples you are collecting into a group using skilled analysts -- These traits are best used with a hybrid approach (supervised) +# MachO Fat Binary Can Support Multiple Architectures +for index in macho.number_of_slices(): -## Example Fitness Model + # Get the Memory Mapped File + mapped_file = macho.image(index) -Traits will be compared amongst their common malware family, any traits not common to all samples will be discarded. + # Get the Memory Map + image = mapped_file.as_memoryview() -Once completed, all remaining traits will be compared to traits from a goodware set, any traits that match the goodware set will be discarded. + # Create Disassembler on Mapped PE Image and PE Architecture + disassembler = Disassembler(macho.architecture(index), image, macho.executable_virtual_address_ranges(index)) -To further differ the traits from other malware families, the remaining population will be compared to other malware families, any that match will be discarded. + # Create the Controlflow Graph + cfg = Graph(macho.architecture(index), config) -The remaining population of traits will be unique to the malware family tested and not legitimate binaries or other malware families. + # Disassemble the PE Image Entrypoints Recursively + disassembler.disassemble_controlflow(macho.entrypoints(index), cfg) +``` + +#### Accessing Genetic Traits +```python +from binlex.controlflow import Instruction +from binlex.controlflow import Block +from binlex.controlflow import Function + +# Iterate Valid Instructions +for address in cfg.instructions.valid_addresses(): + # Read Instruction from Control Flow + instruction = Instruction(address, cfg) + # Print Instruction from Control Flow + instruction.print() + +# Iterate Valid Blocks +for address in cfg.blocks.valid_addresses(): + # Read Block from Control Flow + block = Block(address, cfg) + # Print Block from Control Flow + block.print() + +# Iterate Valid Functions +for address in cfg.functions.valid_addresses(): + # Read Function from Control Flow + function = Function(address, cfg) + # Print Function from Control Flow + function.print() +``` + +Please note that although the Python bindings also take advantage of Rust multi-threading. -This fitness model allows for accurate classification of the tested malware family. +There are limitations in Python that will affect speed and memory performance due to how Python is designed. -## Future Work -- Java byte-code Support `raw:jvm`, `java:jvm` -- Python byte-code Support `raw:pyc`, `python:pyc` -- More API Endpoints -- Cutter, Ghidra and IDA Plugins -- Mac-O Support `macho:x86_64`, `macho:x86` +Mainly, this is due to requiring additional locking and unlocking in Python for thread safety. -## Contributing +As such, if you need lightening fast performance consider using the Rust API instead. + +## Citation + +If you are using **binlex** in a journal publication, or an open-source AI model use the following citation. + +```bibtex +@misc{binlex, + author = {c3rb3ru5d3d53c}, + title = {binlex: A Binary Genetic Trait Lexer Framework}, + year = {2024}, + note = {Available at \url{https://github.com/c3rb3ru5d3d53c/binlex-rs}} +} +``` -If you wish to contribute to Binlex DM me on Twitter [here](https://twitter.com/c3rb3ru5d3d53c). +If the use of **binlex** is for corporate, personal purposes, or for generating outputs that are not open-source AI models, no citation is needed. -You can also join our Discord [here](https://discord.gg/UDBfRpxV3B). +For example, if you use **binlex** to create YARA rules, no citation is needed. -Currently looking for help on: -- MacOS Developer (Parse Mach-O) -- Plugin Developers (Python) -- Front-End Developers (Python) +This ensures that **binlex** stays relevant but also ensures permissive corporate and personal use. diff --git a/assets/binlex.png b/assets/binlex.png new file mode 100644 index 00000000..b66bf824 Binary files /dev/null and b/assets/binlex.png differ diff --git a/bindings/python/blelf.cpp b/bindings/python/blelf.cpp deleted file mode 100644 index 84f9e02c..00000000 --- a/bindings/python/blelf.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include "blelf.h" -#include - -namespace py = pybind11; - -void init_elf(py::module &handle){ - py::class_(handle, "ELF", "Binlex ELF Module") - .def(py::init<>()) - .def("set_architecture", &binlex::ELF::SetArchitecture) - .def("read_file", &binlex::ELF::ReadFile) - .def("get_sections", [](binlex::ELF &module){ - auto result = py::list(); - for (int i = 0; i < BINARY_MAX_SECTIONS; i++){ - if (module.sections[i].data != NULL){ - auto dict = py::dict(); - dict["size"] = module.sections[i].size; - dict["data"] = py::bytes((char *)module.sections[i].data, module.sections[i].size); - dict["offset"] = module.sections[i].offset; - dict["functions"] = module.sections[i].functions; - result.append(dict); - } - } - return result; - }) - .def("read_buffer", [](binlex::ELF &module, py::bytes data){ - py::buffer_info info(py::buffer(data).request()); - return module.ReadBuffer(info.ptr, info.size); - }); - py::enum_(handle, "ARCH") - .value("EM_386", LIEF::ELF::ARCH::EM_386) - .value("EM_X86_64", LIEF::ELF::ARCH::EM_X86_64) - .value("EM_NONE", LIEF::ELF::ARCH::EM_NONE); -} diff --git a/bindings/python/common.cpp b/bindings/python/common.cpp deleted file mode 100644 index 4f28b599..00000000 --- a/bindings/python/common.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include "common.h" -#include - -namespace py = pybind11; - -void init_common(py::module &handle){ - py::class_(handle, "Common", "Binlex Common Module") - .def(py::init<>()) - .def_static("sha256", &binlex::Common::SHA256) - .def_static("remove_wildcards", &binlex::Common::RemoveWildcards) - .def_static("get_byte_size", &binlex::Common::GetByteSize) - .def_static("remove_spaces", &binlex::Common::RemoveSpaces) - .def_static("wildcard_trait", &binlex::Common::WildcardTrait) - .def_static("trim_right", &binlex::Common::TrimRight) - .def_static("wildcards", &binlex::Common::Wildcards) - .def_static("entropy", &binlex::Common::Entropy) - .def_static("hexdump", &binlex::Common::Hexdump) - .def_static("hexdump_be", &binlex::Common::HexdumpBE); - py::enum_(handle, "BINARY_ARCH") - .value("BINARY_ARCH_X86", BINARY_ARCH_X86) - .value("BINARY_ARCH_X86_64", BINARY_ARCH_X86_64) - .value("BINARY_ARCH_UNKNOWN", BINARY_ARCH_UNKNOWN); - py::enum_(handle, "BINARY_MODE") - .value("BINARY_MODE_32", BINARY_MODE_32) - .value("BINARY_MODE_64", BINARY_MODE_64) - .value("BINARY_MODE_CIL", BINARY_MODE_CIL) - .value("BINARY_MODE_UNKNOWN", BINARY_MODE_UNKNOWN); -} diff --git a/bindings/python/disassembler.cpp b/bindings/python/disassembler.cpp deleted file mode 100644 index 80c6f9aa..00000000 --- a/bindings/python/disassembler.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include -#include -#include -#include -#include "disassembler.h" -#include -#include -#include -#include - -using namespace std; -namespace py = pybind11; - -void init_disassembler(py::module &handle){ - py::class_(handle, "Disassembler", "Binlex Disassembler Module") - .def(py::init()) - .def("set_threads", &binlex::Disassembler::py_SetThreads) - .def("set_corpus", &binlex::Disassembler::py_SetCorpus) - .def("set_tags", &binlex::Disassembler::py_SetTags) - .def("set_mode", &binlex::Disassembler::py_SetMode) - .def("disassemble", &binlex::Disassembler::Disassemble) - .def("get_traits", [](binlex::Disassembler &module){ - py::module_ json = py::module_::import("json"); - ostringstream jsonstr; - jsonstr << module.GetTraits(); - return json.attr("loads")(jsonstr.str()); - }) - .def("append_queue", &binlex::Disassembler::AppendQueue) - .def("write_traits", &binlex::Disassembler::WriteTraits); -} diff --git a/bindings/python/file.cpp b/bindings/python/file.cpp deleted file mode 100644 index f8395e97..00000000 --- a/bindings/python/file.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include -#include "file.h" -#include - -namespace py = pybind11; - -void init_file(py::module &handle){ - py::class_(handle, "File", "Binlex File (Base) Module"); -} diff --git a/bindings/python/pe.cpp b/bindings/python/pe.cpp deleted file mode 100644 index 0a4f256b..00000000 --- a/bindings/python/pe.cpp +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include -#include "pe.h" -#include - -namespace py = pybind11; - -void init_pe(py::module &handle){ - py::class_(handle, "File", "Binlex File (Base)"); - py::class_(handle, "PE", "Binlex PE Module") - .def(py::init<>()) - .def("set_architecture", &binlex::PE::SetArchitecture) - .def("read_file", &binlex::PE::ReadFile) - .def("get_sections", [](binlex::PE &module){ - auto result = py::list(); - for (int i = 0; i < BINARY_MAX_SECTIONS; i++){ - if (module.sections[i].data != NULL){ - auto dict = py::dict(); - dict["size"] = module.sections[i].size; - dict["data"] = py::bytes((char *)module.sections[i].data, module.sections[i].size); - dict["offset"] = module.sections[i].offset; - dict["functions"] = module.sections[i].functions; - result.append(dict); - } - } - return result; - }) - .def("read_buffer", [](binlex::PE &module, py::bytes data){ - py::buffer_info info(py::buffer(data).request()); - return module.ReadBuffer(info.ptr, info.size); - }); - py::enum_(handle, "MACHINE_TYPES") - .value("IMAGE_FILE_MACHINE_I386", LIEF::PE::MACHINE_TYPES::IMAGE_FILE_MACHINE_I386) - .value("IMAGE_FILE_MACHINE_AMD64", LIEF::PE::MACHINE_TYPES::IMAGE_FILE_MACHINE_AMD64) - .value("IMAGE_FILE_MACHINE_UNKNOWN", LIEF::PE::MACHINE_TYPES::IMAGE_FILE_MACHINE_UNKNOWN); -} diff --git a/bindings/python/pybind11 b/bindings/python/pybind11 deleted file mode 160000 index 1bbaeb34..00000000 --- a/bindings/python/pybind11 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 1bbaeb3462dcb12ce0b8aeabc56f8dd7a413dbbb diff --git a/bindings/python/pybinlex.cpp b/bindings/python/pybinlex.cpp deleted file mode 100644 index 6fd69c79..00000000 --- a/bindings/python/pybinlex.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include -#include -#include "pe.h" -#include "blelf.h" -#include "common.h" -#include -#include - -namespace py = pybind11; - -void init_pe(py::module &handle); -void init_elf(py::module &handle); -void init_common(py::module &handle); -void init_raw(py::module &handle); -void init_disassembler(py::module &handle); - -PYBIND11_MODULE(pybinlex, handle){ - LIEF::logging::disable(); - handle.doc() = "Binlex - A Binary Genetic Traits Lexer Library and Utility"; - handle.attr("__version__") = "1.1.1"; - init_pe(handle); - init_elf(handle); - init_common(handle); - init_raw(handle); - init_disassembler(handle); -} diff --git a/bindings/python/raw.cpp b/bindings/python/raw.cpp deleted file mode 100644 index 3f28c8c3..00000000 --- a/bindings/python/raw.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include -#include -#include -#include "raw.h" -#include -#include - -using namespace std; -namespace py = pybind11; - -void init_raw(py::module &handle){ - py::class_(handle, "Raw", "Binlex Raw Module") - .def(py::init<>()) - .def("set_architecture", &binlex::Raw::SetArchitecture) - .def("get_sections", [](binlex::Raw &module){ - auto result = py::list(); - for (int i = 0; i < BINARY_MAX_SECTIONS; i++){ - if (module.sections[i].data != NULL){ - auto dict = py::dict(); - dict["size"] = module.sections[i].size; - dict["data"] = py::bytes((char *)module.sections[i].data, module.sections[i].size); - dict["offset"] = module.sections[i].offset; - dict["functions"] = module.sections[i].functions; - result.append(dict); - } - } - return result; - }) - .def("read_buffer", [](binlex::Raw &module, py::bytes data){ - py::buffer_info info(py::buffer(data).request()); - return module.ReadBuffer(info.ptr, info.size); - }) - .def("read_file", &binlex::Raw::ReadFile); -} diff --git a/cmake/FindCAPSTONE.cmake b/cmake/FindCAPSTONE.cmake deleted file mode 100644 index e6947b3f..00000000 --- a/cmake/FindCAPSTONE.cmake +++ /dev/null @@ -1,11 +0,0 @@ -set(CAPSTONE_ROOT "${CMAKE_SOURCE_DIR}/deps/build/capstone") -set(CAPSTONE_INCLUDE_DIRS "${CAPSTONE_ROOT}/include") -file(MAKE_DIRECTORY ${CAPSTONE_INCLUDE_DIRS}) - -add_library(capstone_static STATIC IMPORTED) -set_target_properties(capstone_static PROPERTIES - IMPORTED_LOCATION ${CAPSTONE_ROOT}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}capstone${CMAKE_STATIC_LIBRARY_SUFFIX} -) -add_dependencies(capstone_static capstone) - -target_include_directories(capstone_static INTERFACE ${CAPSTONE_INCLUDE_DIRS}) diff --git a/cmake/FindLIEF.cmake b/cmake/FindLIEF.cmake deleted file mode 100644 index 105dbbba..00000000 --- a/cmake/FindLIEF.cmake +++ /dev/null @@ -1,10 +0,0 @@ -set(LIEF_PREFIX "${CMAKE_SOURCE_DIR}/deps/build/LIEF") -set(LIEF_INCLUDE_DIRS "${LIEF_PREFIX}/include") -file(MAKE_DIRECTORY ${LIEF_INCLUDE_DIRS}) - -add_library(lief_static STATIC IMPORTED) -set_target_properties(lief_static PROPERTIES - IMPORTED_LOCATION ${LIEF_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}LIEF${CMAKE_STATIC_LIBRARY_SUFFIX} -) -add_dependencies(lief_static LIEF) -target_include_directories(lief_static INTERFACE ${LIEF_INCLUDE_DIRS}) diff --git a/cmake/FindTLSH.cmake b/cmake/FindTLSH.cmake deleted file mode 100644 index 7f66de5f..00000000 --- a/cmake/FindTLSH.cmake +++ /dev/null @@ -1,16 +0,0 @@ -set(TLSH_ROOT "${CMAKE_SOURCE_DIR}/deps/build/tlsh") -set(TLSH_INCLUDE_DIRS "${TLSH_ROOT}/src/tlsh/include") -file(MAKE_DIRECTORY ${TLSH_INCLUDE_DIRS}) - -add_library(tlsh_static STATIC IMPORTED) -set_target_properties( - tlsh_static PROPERTIES IMPORTED_LOCATION - ${TLSH_ROOT}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}tlsh${CMAKE_STATIC_LIBRARY_SUFFIX} -) -add_dependencies(tlsh_static tlsh) - -target_include_directories(tlsh_static INTERFACE ${TLSH_INCLUDE_DIRS}) - -if(WIN32) - target_compile_definitions(tlsh_static INTERFACE TLSH_WINDOWS) -endif() diff --git a/cmake/uninstall.cmake b/cmake/uninstall.cmake deleted file mode 100644 index 5ac7ceb6..00000000 --- a/cmake/uninstall.cmake +++ /dev/null @@ -1,24 +0,0 @@ -set(MANIFEST "${CMAKE_CURRENT_BINARY_DIR}/install_manifest.txt") - -if(NOT EXISTS ${MANIFEST}) - message(FATAL_ERROR "Cannot find install manifest: '${MANIFEST}'") -endif() - -file(STRINGS ${MANIFEST} files) -foreach(file ${files}) - if(EXISTS ${file}) - message(STATUS "Removing file: '${file}'") - - exec_program( - ${CMAKE_COMMAND} ARGS "-E remove ${file}" - OUTPUT_VARIABLE stdout - RETURN_VALUE result - ) - - if(NOT "${result}" STREQUAL 0) - message(FATAL_ERROR "Failed to remove file: '${file}'.") - endif() - else() - MESSAGE(STATUS "File '${file}' does not exist.") - endif() -endforeach(file) diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt deleted file mode 100644 index b28e2168..00000000 --- a/deps/CMakeLists.txt +++ /dev/null @@ -1,119 +0,0 @@ -cmake_minimum_required(VERSION 3.5) - -project(deps) - -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - -include(ExternalProject) - -if(MSVC) - add_definitions(-DNOMINMAX) - # HACK: be compatible with the ExternalProject's that are built in Release mode - string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") -endif() - -set(CAPSTONE_ROOT "${CMAKE_BINARY_DIR}/capstone/") -set(CAPSTONE_INCLUDE_DIRS "${CAPSTONE_ROOT}/include/") -set(CAPSTONE_GIT_URL "https://github.com/capstone-engine/capstone.git") -set(CAPSTONE_GIT_TAG "4.0.2") - -ExternalProject_Add( - capstone - PREFIX "${CAPSTONE_ROOT}" - INSTALL_DIR "${CAPSTONE_ROOT}" - GIT_REPOSITORY "${CAPSTONE_GIT_URL}" - GIT_TAG "${CAPSTONE_GIT_TAG}" - GIT_SHALLOW ON - CMAKE_ARGS -DCMAKE_INSTALL_PREFIX:PATH= - -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - -DCAPSTONE_BUILD_SHARED=OFF - -DCAPSTONE_BUILD_TESTS=OFF - -DCAPSTONE_MIPS_SUPPORT=OFF - -DCAPSTONE_ARM_SUPPORT=OFF - -DCAPSTONE_ARM64_SUPPORT=OFF - -DCAPSTONE_M68K_SUPPORT=OFF - -DCAPSTONE_TMS320C64X_SUPPORT=OFF - -DCAPSTONE_M680X_SUPPORT=OFF - -DCAPSTONE_EVM_SUPPORT=OFF - -DCAPSTONE_PPC_SUPPORT=OFF - -DCAPSTONE_SPARC_SUPPORT=OFF - -DCAPSTONE_SYSZ_SUPPORT=OFF - -DCAPSTONE_XCORE_SUPPORT=OFF - -DCAPSTONE_X86_SUPPORT=ON - -DCMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT} - -DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES} - -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} - -DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG} - -DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE} - -DCMAKE_C_FLAGS_MINSIZEREL=${CMAKE_C_FLAGS_MINSIZEREL} - -DCMAKE_C_FLAGS_RELWITHDEBINFO=${CMAKE_C_FLAGS_RELWITHDEBINFO} - -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} - -DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG} - -DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE} - -DCMAKE_CXX_FLAGS_MINSIZEREL=${CMAKE_CXX_FLAGS_MINSIZEREL} - -DCMAKE_CXX_FLAGS_RELWITHDEBINFO=${CMAKE_CXX_FLAGS_RELWITHDEBINFO} - -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON -) -file(MAKE_DIRECTORY ${CAPSTONE_INCLUDE_DIRS}) - -set(TLSH_GIT_URL "https://github.com/mrexodia/tlsh.git") -set(TLSH_ROOT "${CMAKE_BINARY_DIR}/tlsh") -set(TLSH_INCLUDE_DIRS "${TLSH_ROOT}/src/tlsh/include") -set(TLSH_GIT_TAG "24d5c0b7fa2ed4d77d9c5dd0c7e1cbf4cd31b42f") - -set(TLSH_CMAKE_ARGS - -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - -DCMAKE_BUILD_TYPE=Release - -DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE} - -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} - -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} - -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON - -DCMAKE_INSTALL_PREFIX= -) - -ExternalProject_Add( - tlsh - PREFIX "${TLSH_ROOT}" - INSTALL_DIR "${TLSH_ROOT}" - GIT_REPOSITORY "${TLSH_GIT_URL}" - GIT_TAG "${TLSH_GIT_TAG}" - GIT_SHALLOW ON - CMAKE_ARGS ${TLSH_CMAKE_ARGS} -) - -file(MAKE_DIRECTORY ${TLSH_INCLUDE_DIRS}) - -set(LIEF_PREFIX "${CMAKE_BINARY_DIR}/LIEF") -set(LIEF_INSTALL_DIR "${LIEF_PREFIX}") -set(LIEF_INCLUDE_DIRS "${LIEF_PREFIX}/include") -set(LIEF_GIT_URL "https://github.com/lief-project/LIEF.git") -set(LIEF_VERSION "0.12.2") - -set(LIEF_CMAKE_ARGS - -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - -DCMAKE_INSTALL_PREFIX= - -DCMAKE_BUILD_TYPE=Release - -DLIEF_DOC=off - -DLIEF_PYTHON_API=off - -DLIEF_EXAMPLES=off - -DLIEF_PE=on - -DLIEF_ELF=on - -DLIEF_MACHO=off - -DLIEF_OAT=off - -DLIEF_DEX=off - -DLIEF_VDEX=off - -DLIEF_ART=off - -DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE} - -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} - -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -) - -ExternalProject_Add(LIEF - PREFIX ${LIEF_PREFIX} - GIT_REPOSITORY ${LIEF_GIT_URL} - GIT_TAG ${LIEF_VERSION} - INSTALL_DIR ${LIEF_INSTALL_DIR} - CMAKE_ARGS ${LIEF_CMAKE_ARGS} -) - -file(MAKE_DIRECTORY ${LIEF_INCLUDE_DIRS}) diff --git a/docs/html/annotated.html b/docs/html/annotated.html deleted file mode 100644 index 38b3a68e..00000000 --- a/docs/html/annotated.html +++ /dev/null @@ -1,151 +0,0 @@ - - - - - - - -binlex: Class List - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
Class List
-
-
-
Here are the classes, structs, unions and interfaces with brief descriptions:
-
-
- - - - diff --git a/docs/html/annotated_dup.js b/docs/html/annotated_dup.js deleted file mode 100644 index ac4b125e..00000000 --- a/docs/html/annotated_dup.js +++ /dev/null @@ -1,31 +0,0 @@ -var annotated_dup = -[ - [ "binlex", "namespacebinlex.html", "namespacebinlex" ], - [ "dotnet", null, [ - [ "BlobHeapIndex", "classdotnet_1_1BlobHeapIndex.html", "classdotnet_1_1BlobHeapIndex" ], - [ "COR20_HEADER", "structdotnet_1_1COR20__HEADER.html", "structdotnet_1_1COR20__HEADER" ], - [ "COR20_STORAGE_HEADER", "structdotnet_1_1COR20__STORAGE__HEADER.html", "structdotnet_1_1COR20__STORAGE__HEADER" ], - [ "COR20_STORAGE_SIGNATURE", "structdotnet_1_1COR20__STORAGE__SIGNATURE.html", "structdotnet_1_1COR20__STORAGE__SIGNATURE" ], - [ "COR20_STREAM_HEADER", "structdotnet_1_1COR20__STREAM__HEADER.html", "structdotnet_1_1COR20__STREAM__HEADER" ], - [ "Cor20MetadataTable", "classdotnet_1_1Cor20MetadataTable.html", "classdotnet_1_1Cor20MetadataTable" ], - [ "FatHeader", "classdotnet_1_1FatHeader.html", null ], - [ "FieldEntry", "classdotnet_1_1FieldEntry.html", "classdotnet_1_1FieldEntry" ], - [ "FieldPtrEntry", "classdotnet_1_1FieldPtrEntry.html", "classdotnet_1_1FieldPtrEntry" ], - [ "GuidHeapIndex", "classdotnet_1_1GuidHeapIndex.html", "classdotnet_1_1GuidHeapIndex" ], - [ "Method", "classdotnet_1_1Method.html", null ], - [ "MethodDefEntry", "classdotnet_1_1MethodDefEntry.html", "classdotnet_1_1MethodDefEntry" ], - [ "MethodHeader", "classdotnet_1_1MethodHeader.html", null ], - [ "MethodPtrEntry", "classdotnet_1_1MethodPtrEntry.html", "classdotnet_1_1MethodPtrEntry" ], - [ "ModuleEntry", "classdotnet_1_1ModuleEntry.html", "classdotnet_1_1ModuleEntry" ], - [ "MultiTableIndex", "classdotnet_1_1MultiTableIndex.html", "classdotnet_1_1MultiTableIndex" ], - [ "ResolutionScopeIndex", "classdotnet_1_1ResolutionScopeIndex.html", null ], - [ "SimpleTableIndex", "classdotnet_1_1SimpleTableIndex.html", "classdotnet_1_1SimpleTableIndex" ], - [ "StringHeapIndex", "classdotnet_1_1StringHeapIndex.html", "classdotnet_1_1StringHeapIndex" ], - [ "TableEntry", "classdotnet_1_1TableEntry.html", "classdotnet_1_1TableEntry" ], - [ "TinyHeader", "classdotnet_1_1TinyHeader.html", null ], - [ "TypeDefEntry", "classdotnet_1_1TypeDefEntry.html", "classdotnet_1_1TypeDefEntry" ], - [ "TypeDefOrRefIndex", "classdotnet_1_1TypeDefOrRefIndex.html", null ], - [ "TypeRefEntry", "classdotnet_1_1TypeRefEntry.html", "classdotnet_1_1TypeRefEntry" ] - ] ], - [ "SHA256_CTX", "structSHA256__CTX.html", "structSHA256__CTX" ] -]; \ No newline at end of file diff --git a/docs/html/args_8h_source.html b/docs/html/args_8h_source.html deleted file mode 100644 index e3502339..00000000 --- a/docs/html/args_8h_source.html +++ /dev/null @@ -1,181 +0,0 @@ - - - - - - - -binlex: include/args.h Source File - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
args.h
-
-
-
1 #ifndef ARGS_H
-
2 #define ARGS_H
-
3 
-
4 #include <set>
-
5 #include <string>
-
6 #include <sstream>
-
7 #include <stdio.h>
-
8 #include <stdlib.h>
-
9 #include <string.h>
-
10 #include <sys/types.h>
-
11 #include <sys/stat.h>
-
12 
-
13 #ifndef _WIN32
-
14 #include <unistd.h>
-
15 #else
-
16 #include <windows.h>
-
17 #endif
-
18 
-
19 #ifdef _WIN32
-
20 #define BINLEX_EXPORT __declspec(dllexport)
-
21 #else
-
22 #define BINLEX_EXPORT
-
23 #endif
-
24 
-
25 #define ARGS_MODE_COUNT 9
-
26 
-
27 #define ARGS_IO_TYPE_UNKNOWN 0
-
28 #define ARGS_IO_TYPE_FILE 1
-
29 #define ARGS_IO_TYPE_DIR 2
-
30 
-
31 #ifdef _WIN32
-
32 typedef unsigned int uint;
-
33 typedef uint useconds_t;
-
34 #endif
-
35 
-
40 namespace binlex{
-
41  class Args {
-
42  public:
-
43  char version[7] = "v1.1.1";
-
44  const char *modes[ARGS_MODE_COUNT] = {"elf:x86", "elf:x86_64", "pe:x86", "pe:x86_64", "raw:x86", "raw:x86_64", "raw:cil", "pe:cil", "auto"};
-
45  struct{
-
46  char *input;
-
47  int io_type;
-
48  char *output;
-
49  uint timeout;
-
50  uint threads;
-
51  bool help;
-
52  bool list_modes;
-
53  bool instructions;
-
54  std::string mode;
-
55  std::string corpus;
-
56  bool pretty;
-
57  bool debug;
-
58  std::set<std::string> tags;
-
59  } options;
-
60  BINLEX_EXPORT Args();
-
65  BINLEX_EXPORT void SetDefault();
-
71  BINLEX_EXPORT bool check_mode(const char *mode);
-
77  BINLEX_EXPORT int is_file(const char *path);
-
83  BINLEX_EXPORT int is_dir(const char *path);
-
89  BINLEX_EXPORT void set_io_type(char *input);
-
94  BINLEX_EXPORT void print_help();
-
99  BINLEX_EXPORT void parse(int argc, char **argv);
-
104  BINLEX_EXPORT std::string get_tags_as_str();
-
105  BINLEX_EXPORT ~Args();
-
106  };
-
107 }
-
108 
-
109 #endif
-
-
-
BINLEX_EXPORT void SetDefault()
-
BINLEX_EXPORT void parse(int argc, char **argv)
-
BINLEX_EXPORT int is_file(const char *path)
-
BINLEX_EXPORT std::string get_tags_as_str()
-
BINLEX_EXPORT int is_dir(const char *path)
-
std::set< std::string > tags
Set for storing the tags.
Definition: args.h:58
-
Definition: args.h:41
-
BINLEX_EXPORT bool check_mode(const char *mode)
-
BINLEX_EXPORT void set_io_type(char *input)
-
BINLEX_EXPORT void print_help()
-
the binlex namespace
- - - - diff --git a/docs/html/bc_s.png b/docs/html/bc_s.png deleted file mode 100644 index 224b29aa..00000000 Binary files a/docs/html/bc_s.png and /dev/null differ diff --git a/docs/html/bdwn.png b/docs/html/bdwn.png deleted file mode 100644 index 940a0b95..00000000 Binary files a/docs/html/bdwn.png and /dev/null differ diff --git a/docs/html/blelf_8h_source.html b/docs/html/blelf_8h_source.html deleted file mode 100644 index 104fb553..00000000 --- a/docs/html/blelf_8h_source.html +++ /dev/null @@ -1,148 +0,0 @@ - - - - - - - -binlex: include/blelf.h Source File - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
blelf.h
-
-
-
1 #ifndef ELF_H
-
2 #define ELF_H
-
3 
-
4 #ifdef _WIN32
-
5 #include <Windows.h>
-
6 #endif
-
7 
-
8 #include <iostream>
-
9 #include <memory>
-
10 #include <vector>
-
11 #include <set>
-
12 #include <LIEF/ELF.hpp>
-
13 #include <exception>
-
14 #include <stdexcept>
-
15 #include <cassert>
-
16 #include "file.h"
-
17 
-
18 #ifdef _WIN32
-
19 #define BINLEX_EXPORT __declspec(dllexport)
-
20 #else
-
21 #define BINLEX_EXPORT
-
22 #endif
-
23 
-
24 using namespace std;
-
25 using namespace LIEF::ELF;
-
26 
-
27 namespace binlex{
-
28  class ELF : public File{
-
32  private:
-
37  bool ParseSections();
-
38  public:
-
39  ARCH mode = ARCH::EM_NONE;
-
40  unique_ptr<LIEF::ELF::Binary> binary;
-
41  struct Section sections[BINARY_MAX_SECTIONS];
-
42  uint32_t total_exec_sections;
-
43  BINLEX_EXPORT ELF();
-
49  BINLEX_EXPORT bool Setup(ARCH input_mode);
-
55  virtual bool ReadVector(const std::vector<uint8_t> &data);
-
56  BINLEX_EXPORT ~ELF();
-
57  };
-
58 };
-
59 
-
60 #endif
-
-
-
Definition: blelf.h:28
-
Definition: file.h:21
-
Definition: file.h:14
-
the binlex namespace
- - - - diff --git a/docs/html/cil_8h_source.html b/docs/html/cil_8h_source.html deleted file mode 100644 index 08cb9d21..00000000 --- a/docs/html/cil_8h_source.html +++ /dev/null @@ -1,468 +0,0 @@ - - - - - - - -binlex: include/cil.h Source File - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
cil.h
-
-
-
1 #ifndef CIL_H
-
2 #define CIL_H
-
3 
-
4 #include <iostream>
-
5 #include <fstream>
-
6 #include <stdio.h>
-
7 #include <string.h>
-
8 #include <stdlib.h>
-
9 #include <errno.h>
-
10 #include <inttypes.h>
-
11 #include <assert.h>
-
12 #include <byteswap.h>
-
13 #include <ctype.h>
-
14 #include <capstone/capstone.h>
-
15 #include <queue>
-
16 #include <vector>
-
17 #include <set>
-
18 #include <map>
-
19 #include "decompilerbase.h"
-
20 #include "json.h"
-
21 
-
22 // Bytecode Reference: https://en.wikipedia.org/wiki/List_of_CIL_instructions
-
23 #define INVALID_OP 0xFFFFFFFF
-
24 
-
25 // CIL Decompiler Types
-
26 #define CIL_DECOMPILER_TYPE_FUNCS 0
-
27 #define CIL_DECOMPILER_TYPE_BLCKS 1
-
28 #define CIL_DECOMPILER_TYPE_UNSET 2
-
29 #define CIL_DECOMPILER_TYPE_ALL 3
-
30 
-
31 #define CIL_DECOMPILER_MAX_SECTIONS 256
-
32 #define CIL_DECOMPILER_MAX_INSN 16384
-
33 
-
34 // CIL Instructions
-
35 #define CIL_INS_ADD 0x58
-
36 #define CIL_INS_ADD_OVF 0xD6
-
37 #define CIL_INS_ADD_OVF_UN 0xD7
-
38 #define CIL_INS_AND 0x5F
-
39 #define CIL_INS_BEQ 0x3B
-
40 #define CIL_INS_BEQ_S 0x2E
-
41 #define CIL_INS_BGE 0x3C
-
42 #define CIL_INS_BGE_S 0x2F
-
43 #define CIL_INS_BGE_UN 0x41
-
44 #define CIL_INS_BGE_UN_S 0x34
-
45 #define CIL_INS_BGT 0x3D
-
46 #define CIL_INS_BGT_S 0x30
-
47 #define CIL_INS_BGT_UN 0x42
-
48 #define CIL_INS_BGT_UN_S 0x35
-
49 #define CIL_INS_BLE 0x3E
-
50 #define CIL_INS_BLE_S 0x31
-
51 #define CIL_INS_BLE_UN 0x43
-
52 #define CIL_INS_BLE_UN_S 0x36
-
53 #define CIL_INS_BLT 0x3F
-
54 #define CIL_INS_BLT_S 0x32
-
55 #define CIL_INS_BLT_UN 0x44
-
56 #define CIL_INS_BLT_UN_S 0x37
-
57 #define CIL_INS_BNE_UN 0x40
-
58 #define CIL_INS_BNE_UN_S 0x33
-
59 #define CIL_INS_BOX 0x8C
-
60 #define CIL_INS_BR 0x38
-
61 #define CIL_INS_BR_S 0x2B
-
62 #define CIL_INS_BREAK 0x01
-
63 #define CIL_INS_BRFALSE 0x39
-
64 #define CIL_INS_BRFALSE_S 0x2C
-
65 #define CIL_INS_BRINST 0x3A
-
66 #define CIL_INS_BRINST_S 0x2D
-
67 #define CIL_INS_BRNULL 0x39
-
68 #define CIL_INS_BRNULL_S 0x2C
-
69 #define CIL_INS_BRTRUE 0x3A
-
70 #define CIL_INS_BRTRUE_S 0x2D
-
71 #define CIL_INS_BRZERO 0x39
-
72 #define CIL_INS_BRZERO_S 0x2C
-
73 #define CIL_INS_CALL 0x28
-
74 #define CIL_INS_CALLI 0x29
-
75 #define CIL_INS_CALLVIRT 0x6F
-
76 #define CIL_INS_CASTCLASS 0x74
-
77 #define CIL_INS_CKINITE 0xC3
-
78 #define CIL_INS_CONV_I 0xD3
-
79 #define CIL_INS_CONV_I1 0x67
-
80 #define CIL_INS_CONV_I2 0x68
-
81 #define CIL_INS_CONV_I4 0x69
-
82 #define CIL_INS_CONV_I8 0x6A
-
83 #define CIL_INS_CONV_OVF_i 0xD4
-
84 #define CIL_INS_CONV_OVF_I_UN 0x8A
-
85 #define CIL_INS_CONV_OVF_I1 0xB3
-
86 #define CIL_INS_CONV_OVF_I1_UN 0x82
-
87 #define CIL_INS_CONV_OVF_I2 0xB5
-
88 #define CIL_INS_CONV_OVF_I2_UN 0x83
-
89 #define CIL_INS_CONV_OVF_I4 0xB7
-
90 #define CIL_INS_CONV_OVF_I4_UN 0x84
-
91 #define CIL_INS_CONV_OVF_I8 0xB9
-
92 #define CIL_INS_CONV_OVF_I8_UN 0x85
-
93 #define CIL_INS_CONV_OVF_U 0xD5
-
94 #define CIL_INS_CONV_OVF_U_UN 0x8B
-
95 #define CIL_INS_CONV_OVF_U1 0xB4
-
96 #define CIL_INS_CONV_OVF_U1_UN 0x86
-
97 #define CIL_INS_CONV_OVF_U2 0xB6
-
98 #define CIL_INS_CONV_OVF_U2_UN 0x87
-
99 #define CIL_INS_CONV_OVF_U4 0xB8
-
100 #define CIL_INS_CONV_OVF_U4_UN 0x88
-
101 #define CIL_INS_CONV_OVF_U8 0xBA
-
102 #define CIL_INS_CONV_OVF_U8_UN 0x89
-
103 #define CIL_INS_CONV_R_UN 0x76
-
104 #define CIL_INS_CONV_R4 0x6B
-
105 #define CIL_INS_CONV_R8 0x6C
-
106 #define CIL_INS_CONV_U 0xE0
-
107 #define CIL_INS_CONV_U1 0xD2
-
108 #define CIL_INS_CONV_U2 0xD1
-
109 #define CIL_INS_CONV_U4 0x6D
-
110 #define CIL_INS_CONV_U8 0x6E
-
111 #define CIL_INS_CPOBJ 0x70
-
112 #define CIL_INS_DIV 0x5B
-
113 #define CIL_INS_DIV_UN 0x5C
-
114 #define CIL_INS_DUP 0x25
-
115 #define CIL_INS_ENDFAULT 0xDC
-
116 #define CIL_INS_ENDFINALLY 0xDC
-
117 #define CIL_INS_ISINST 0x75
-
118 #define CIL_INS_JMP 0x27
-
119 #define CIL_INS_LDARG_0 0x02
-
120 #define CIL_INS_LDARG_1 0x03
-
121 #define CIL_INS_LDARG_2 0x04
-
122 #define CIL_INS_LDARG_3 0x05
-
123 #define CIL_INS_LDARG_S 0x0E
-
124 #define CIL_INS_LDARGA_S 0x0F
-
125 #define CIL_INS_LDC_I4 0x20
-
126 #define CIL_INS_LDC_I4_0 0x16
-
127 #define CIL_INS_LDC_I4_1 0x17
-
128 #define CIL_INS_LDC_I4_2 0x18
-
129 #define CIL_INS_LDC_I4_3 0x19
-
130 #define CIL_INS_LDC_I4_4 0x1A
-
131 #define CIL_INS_LDC_I4_5 0x1B
-
132 #define CIL_INS_LDC_I4_6 0x1C
-
133 #define CIL_INS_LDC_I4_7 0x1D
-
134 #define CIL_INS_LDC_I4_8 0x1E
-
135 #define CIL_INS_LDC_I4_M1 0x15
-
136 #define CIL_INS_LDC_I4_S 0x1F
-
137 #define CIL_INS_LDC_I8 0x21
-
138 #define CIL_INS_LDC_R4 0x22
-
139 #define CIL_INS_LDC_R8 0x23
-
140 #define CIL_INS_LDELM 0xA3
-
141 #define CIL_INS_LDELM_I 0x97
-
142 #define CIL_INS_LDELM_I1 0x90
-
143 #define CIL_INS_LDELM_I2 0x92
-
144 #define CIL_INS_LDELM_I4 0x94
-
145 #define CIL_INS_LDELM_I8 0x96
-
146 #define CIL_INS_LDELM_R4 0x98
-
147 #define CIL_INS_LDELM_R8 0x99
-
148 #define CIL_INS_LDELM_REF 0x9A
-
149 #define CIL_INS_LDELM_U1 0x91
-
150 #define CIL_INS_LDELM_U2 0x93
-
151 #define CIL_INS_LDELM_U4 0x95
-
152 #define CIL_INS_LDELM_U8 0x96
-
153 #define CIL_INS_LDELMA 0x8F
-
154 #define CIL_INS_LDFLD 0x7B
-
155 #define CIL_INS_LDFLDA 0x7C
-
156 #define CIL_INS_LDIND_I 0x4D
-
157 #define CIL_INS_LDIND_I1 0x46
-
158 #define CIL_INS_LDIND_I2 0x48
-
159 #define CIL_INS_LDIND_I4 0x4A
-
160 #define CIL_INS_LDIND_I8 0x4C
-
161 #define CIL_INS_LDIND_R4 0x4E
-
162 #define CIL_INS_LDIND_R8 0x4F
-
163 #define CIL_INS_LDIND_REF 0x50
-
164 #define CIL_INS_LDIND_U1 0x47
-
165 #define CIL_INS_LDIND_U2 0x49
-
166 #define CIL_INS_LDIND_U4 0x4B
-
167 #define CIL_INS_LDIND_U8 0x4C
-
168 #define CIL_INS_LDLEN 0x8E
-
169 #define CIL_INS_LDLOC_0 0x06
-
170 #define CIL_INS_LDLOC_1 0x07
-
171 #define CIL_INS_LDLOC_2 0x08
-
172 #define CIL_INS_LDLOC_3 0x09
-
173 #define CIL_INS_LDLOC_S 0x11
-
174 #define CIL_INS_LDLOCA_S 0x12
-
175 #define CIL_INS_LDNULL 0x14
-
176 #define CIL_INS_LDOBJ 0x71
-
177 #define CIL_INS_LDSFLD 0x7E
-
178 #define CIL_INS_LDSFLDA 0x7F
-
179 #define CIL_INS_LDSTR 0x72
-
180 #define CIL_INS_LDTOKEN 0xD0
-
181 #define CIL_INS_LEAVE 0xDD
-
182 #define CIL_INS_LEAVE_S 0xDE
-
183 #define CIL_INS_MKREFANY 0xC6
-
184 #define CIL_INS_MUL 0x5A
-
185 #define CIL_INS_MUL_OVF 0xD8
-
186 #define CIL_INS_MUL_OVF_UN 0xD9
-
187 #define CIL_INS_NEG 0x65
-
188 #define CIL_INS_NEWARR 0x8D
-
189 #define CIL_INS_NEWOBJ 0x73
-
190 #define CIL_INS_NOP 0x00
-
191 #define CIL_INS_NOT 0x66
-
192 #define CIL_INS_OR 0x60
-
193 #define CIL_INS_POP 0x26
-
194 #define CIL_INS_REFANYVAL 0xC2
-
195 #define CIL_INS_REM 0x5D
-
196 #define CIL_INS_REM_UN 0x5E
-
197 #define CIL_INS_RET 0x2A
-
198 #define CIL_INS_SHL 0x62
-
199 #define CIL_INS_SHR 0x63
-
200 #define CIL_INS_SHR_UN 0x64
-
201 #define CIL_INS_STARG_S 0x10
-
202 #define CIL_INS_STELEM 0xA4
-
203 #define CIL_INS_STELEM_I 0x9B
-
204 #define CIL_INS_STELEM_I1 0x9C
-
205 #define CIL_INS_STELEM_I2 0x9D
-
206 #define CIL_INS_STELEM_I4 0x9E
-
207 #define CIL_INS_STELEM_I8 0x9F
-
208 #define CIL_INS_STELEM_R4 0xA0
-
209 #define CIL_INS_STELEM_R8 0xA1
-
210 #define CIL_INS_STELEM_REF 0xA2
-
211 #define CIL_INS_STFLD 0x7D
-
212 #define CIL_INS_STIND_I 0xDF
-
213 #define CIL_INS_STIND_I1 0x52
-
214 #define CIL_INS_STIND_I2 0x53
-
215 #define CIL_INS_STIND_I4 0x54
-
216 #define CIL_INS_STIND_I8 0x55
-
217 #define CIL_INS_STIND_R4 0x56
-
218 #define CIL_INS_STIND_R8 0x57
-
219 #define CIL_INS_STIND_REF 0x51
-
220 #define CIL_INS_STLOC_0 0x0A
-
221 #define CIL_INS_STLOC_1 0x0B
-
222 #define CIL_INS_STLOC_2 0x0C
-
223 #define CIL_INS_STLOC_3 0x0D
-
224 #define CIL_INS_STOBJ 0x81
-
225 #define CIL_INS_STSFLD 0x80
-
226 #define CIL_INS_SUB 0x59
-
227 #define CIL_INS_SUB_OVF 0xDA
-
228 #define CIL_INS_SUB_OVF_UN 0xDB
-
229 #define CIL_INS_SWITCH 0x45
-
230 #define CIL_INS_THROW 0x7A
-
231 #define CIL_INS_UNBOX 0x79
-
232 #define CIL_INS_UNBOX_ANY 0xA5
-
233 #define CIL_INS_XOR 0x61
-
234 #define CIL_INS_STLOC_S 0x13
-
235 
-
236 // CIL Prefix Instructions
-
237 #define CIL_INS_PREFIX 0xFE
-
238 #define CIL_INS_ARGLIST 0x00
-
239 #define CIL_INS_CEQ 0x01
-
240 #define CIL_INS_CGT 0x02
-
241 #define CIL_INS_CGT_UN 0x03
-
242 #define CIL_INS_CLT 0x04
-
243 #define CIL_INS_CLT_UN 0x05
-
244 #define CIL_INS_CONSTRAINED 0x16
-
245 #define CIL_INS_CPBLK 0x17
-
246 #define CIL_INS_ENDFILTER 0x11
-
247 #define CIL_INS_INITBLK 0x18
-
248 #define CIL_INS_INITOBJ 0x15
-
249 #define CIL_INS_LDARG 0x09
-
250 #define CIL_INS_LDARGA 0x0A
-
251 #define CIL_INS_LDFTN 0x06
-
252 #define CIL_INS_LDLOC 0x0C
-
253 #define CIL_INS_LDLOCA 0x0D
-
254 #define CIL_INS_LDVIRTFTN 0x07
-
255 #define CIL_INS_LOCALLOC 0x0F
-
256 #define CIL_INS_NO 0x19
-
257 #define CIL_INS_READONLY 0x1E
-
258 #define CIL_INS_REFANYTYPE 0x1D
-
259 #define CIL_INS_RETHROW 0x1A
-
260 #define CIL_INS_SIZEOF 0x1C
-
261 #define CIL_INS_STARG 0x0B
-
262 #define CIL_INS_STLOC 0x0E
-
263 #define CIL_INS_TAIL 0x14
-
264 #define CIL_INS_UNALIGNED 0x12
-
265 #define CIL_INS_VOLATILE 0x13
-
266 
-
267 using namespace std;
-
268 
-
269 namespace binlex {
-
270  class CILDecompiler : public DecompilerBase {
-
271  /*
-
272  This class is used to decompile CIL/.NET bytecode.
-
273  */
-
274  private:
-
275  int type = CIL_DECOMPILER_TYPE_UNSET;
-
276  int update_offset(int operand_size, int i);
-
277  typedef struct worker {
-
278  csh handle;
-
279  cs_err error;
-
280  uint64_t pc;
-
281  const uint8_t *code;
-
282  size_t code_size;
-
283  } worker;
-
284  typedef struct{
-
285  uint index;
-
286  void *sections;
-
287  } worker_args;
-
288  public:
-
289  CILDecompiler(const binlex::File &firef);
-
290  struct Instruction {
-
291  unsigned char instruction;
-
292  uint operand_size;
-
293  uint offset;
-
294  };
-
295  struct Trait {
-
296  string corpus;
-
297  string type;
-
298  string architecture;
-
299  string tmp_bytes;
-
300  string bytes;
-
301  string trait;
-
302  uint edges;
-
303  uint blocks;
-
304  vector< Instruction* >* instructions;
-
305  uint num_instructions;
-
306  uint size;
-
307  uint offset;
-
308  uint invalid_instructions;
-
309  uint cyclomatic_complexity;
-
310  uint average_instructions_per_block;
-
311  float bytes_entropy;
-
312  float trait_entropy;
-
313  string trait_sha256;
-
314  string bytes_sha256;
-
315  };
-
316  struct Section {
-
317  vector <Trait*> function_traits;
-
318  vector <Trait*> block_traits;
-
319  char *trait;
-
320  cs_arch arch;
-
321  cs_mode mode;
-
322  char *arch_str;
-
323  char *cpu;
-
324  string corpus;
-
325  uint threads;
-
326  bool instructions;
-
327  uint thread_cycles;
-
328  useconds_t thread_sleep;
-
329  uint offset;
-
330  uint ntraits;
-
331  struct Trait **traits;
-
332  void *data;
-
333  size_t data_size;
-
334  set<uint64_t> coverage;
-
335  map<uint64_t, uint> addresses;
-
336  map<uint64_t, int> visited;
-
337  queue<uint64_t> discovered;
-
338  };
-
339  struct Section sections[CIL_DECOMPILER_MAX_SECTIONS];
-
340  //Map containing prefix instructions and their operand sizes
-
341  map<int, int> prefixInstrMap;
-
342  //Map containing conditional instructions and their operand sizes
-
343  map<int, int> condInstrMap;
-
344  //Map for all remaining instruction types that don't need special
-
345  //treatment
-
346  map<int, int> miscInstrMap;
-
347  bool Setup(int input_type);
-
348  bool Decompile(void *data, int data_size, int index);
-
353  vector<json> GetTraits();
-
358  json GetTrait(struct Trait *trait);
-
363  string ConvTraitBytes(vector< Instruction* > instructions);
-
369  string ConvBytes(vector< Instruction* > allinst, void *data, int data_size);
-
374  bool IsConditionalInsn(Instruction *insn);
-
379  bool IsPrefixInstr(Instruction *insn);
-
384  uint SizeOfTrait(vector< Instruction* > allinst);
-
385  ~CILDecompiler();
-
386  };
-
387 };
-
388 
-
389 #endif
-
-
-
Definition: cil.h:270
-
Definition: decompilerbase.h:14
- -
Definition: cil.h:316
-
Definition: cil.h:295
-
Definition: file.h:14
-
the binlex namespace
- - - - diff --git a/docs/html/classbinlex_1_1Args-members.html b/docs/html/classbinlex_1_1Args-members.html deleted file mode 100644 index af465e70..00000000 --- a/docs/html/classbinlex_1_1Args-members.html +++ /dev/null @@ -1,129 +0,0 @@ - - - - - - - -binlex: Member List - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
binlex::Args Member List
-
-
- -

This is the complete list of members for binlex::Args, including all inherited members.

- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Args() (defined in binlex::Args)binlex::Args
check_mode(const char *mode)binlex::Args
corpus (defined in binlex::Args)binlex::Args
debug (defined in binlex::Args)binlex::Args
get_tags_as_str()binlex::Args
help (defined in binlex::Args)binlex::Args
input (defined in binlex::Args)binlex::Args
instructions (defined in binlex::Args)binlex::Args
io_type (defined in binlex::Args)binlex::Args
is_dir(const char *path)binlex::Args
is_file(const char *path)binlex::Args
list_modes (defined in binlex::Args)binlex::Args
mode (defined in binlex::Args)binlex::Args
modes (defined in binlex::Args)binlex::Args
options (defined in binlex::Args)binlex::Args
output (defined in binlex::Args)binlex::Args
parse(int argc, char **argv)binlex::Args
pretty (defined in binlex::Args)binlex::Args
print_help()binlex::Args
set_io_type(char *input)binlex::Args
SetDefault()binlex::Args
tagsbinlex::Args
threads (defined in binlex::Args)binlex::Args
timeout (defined in binlex::Args)binlex::Args
version (defined in binlex::Args)binlex::Args
~Args() (defined in binlex::Args)binlex::Args
-
- - - - diff --git a/docs/html/classbinlex_1_1Args.html b/docs/html/classbinlex_1_1Args.html deleted file mode 100644 index dd8a8f73..00000000 --- a/docs/html/classbinlex_1_1Args.html +++ /dev/null @@ -1,365 +0,0 @@ - - - - - - - -binlex: binlex::Args Class Reference - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
- -
-
binlex::Args Class Reference
-
-
- - - - - - - - - - - - - - - - - - -

-Public Member Functions

BINLEX_EXPORT void SetDefault ()
 
BINLEX_EXPORT bool check_mode (const char *mode)
 
BINLEX_EXPORT int is_file (const char *path)
 
BINLEX_EXPORT int is_dir (const char *path)
 
BINLEX_EXPORT void set_io_type (char *input)
 
BINLEX_EXPORT void print_help ()
 
BINLEX_EXPORT void parse (int argc, char **argv)
 
BINLEX_EXPORT std::string get_tags_as_str ()
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

-Public Attributes

-char version [7] = "v1.1.1"
 
-const char * modes [ARGS_MODE_COUNT] = {"elf:x86", "elf:x86_64", "pe:x86", "pe:x86_64", "raw:x86", "raw:x86_64", "raw:cil", "pe:cil", "auto"}
 
-struct {
-   char *   input
 
-   int   io_type
 
-   char *   output
 
-   uint   timeout
 
-   uint   threads
 
-   bool   help
 
-   bool   list_modes
 
-   bool   instructions
 
-   std::string   mode
 
-   std::string   corpus
 
-   bool   pretty
 
-   bool   debug
 
-   std::set< std::string >   tags
 Set for storing the tags.
 
options
 
-

Member Function Documentation

- -

◆ check_mode()

- -
-
- - - - - - - - -
BINLEX_EXPORT bool binlex::Args::check_mode (const char * mode)
-
-

Check the CLI mode provided by the user.

Parameters
- - -
modethe mode provided
-
-
-
Returns
bool
- -
-
- -

◆ get_tags_as_str()

- -
-
- - - - - - - -
BINLEX_EXPORT std::string binlex::Args::get_tags_as_str ()
-
-

Get tags from CLI as string.

Returns
std::string
- -
-
- -

◆ is_dir()

- -
-
- - - - - - - - -
BINLEX_EXPORT int binlex::Args::is_dir (const char * path)
-
-

Check if path is a directory.

Parameters
- - -
pathpath to a directory
-
-
-
Returns
int result
- -
-
- -

◆ is_file()

- -
-
- - - - - - - - -
BINLEX_EXPORT int binlex::Args::is_file (const char * path)
-
-

Check if path to file is valid.

Parameters
- - -
pathfile path
-
-
-
Returns
int result
- -
-
- -

◆ parse()

- -
-
- - - - - - - - - - - - - - - - - - -
BINLEX_EXPORT void binlex::Args::parse (int argc,
char ** argv 
)
-
-

Parse CLI arguments.

Returns
void
- -
-
- -

◆ print_help()

- -
-
- - - - - - - -
BINLEX_EXPORT void binlex::Args::print_help ()
-
-

Print CLI help menu.

Returns
void
- -
-
- -

◆ set_io_type()

- -
-
- - - - - - - - -
BINLEX_EXPORT void binlex::Args::set_io_type (char * input)
-
-

Set input type.

Parameters
- - -
inputfile path or directory
-
-
-
Returns
void
- -
-
- -

◆ SetDefault()

- -
-
- - - - - - - -
BINLEX_EXPORT void binlex::Args::SetDefault ()
-
-

Set the default CLI parameters.

Returns
void
- -
-
-
The documentation for this class was generated from the following file: -
-
- - - - diff --git a/docs/html/classbinlex_1_1Args.js b/docs/html/classbinlex_1_1Args.js deleted file mode 100644 index 1c1751fd..00000000 --- a/docs/html/classbinlex_1_1Args.js +++ /dev/null @@ -1,29 +0,0 @@ -var classbinlex_1_1Args = -[ - [ "Args", "classbinlex_1_1Args.html#a5c4302c4c75021d0cc0d1dba832a745d", null ], - [ "~Args", "classbinlex_1_1Args.html#ae40a05f2c800642262f54dbc0261e022", null ], - [ "check_mode", "classbinlex_1_1Args.html#ad39861cfaf635961de187ecccb8c8768", null ], - [ "get_tags_as_str", "classbinlex_1_1Args.html#a07ab8a9c6d5a9578516a0b2de7999233", null ], - [ "is_dir", "classbinlex_1_1Args.html#a4123f3468f9460122c5c57c3ee6ca3e6", null ], - [ "is_file", "classbinlex_1_1Args.html#ad2f4242136bc00eeaed9417652306334", null ], - [ "parse", "classbinlex_1_1Args.html#ad98e40d517d1f5f00ea0957c91a565bc", null ], - [ "print_help", "classbinlex_1_1Args.html#abf121fddbd8c005159add8135fc81786", null ], - [ "set_io_type", "classbinlex_1_1Args.html#a0589f1ceb485b09037e248c5867f0644", null ], - [ "SetDefault", "classbinlex_1_1Args.html#a315af4879832687b962c26c559d9fb4d", null ], - [ "corpus", "classbinlex_1_1Args.html#a185913fcf8b3010892df72a5369adf36", null ], - [ "debug", "classbinlex_1_1Args.html#a219943a7e192e5ed28d49d35d6f9d207", null ], - [ "help", "classbinlex_1_1Args.html#a2491b08ed8671ee298f81a4bb8a55edc", null ], - [ "input", "classbinlex_1_1Args.html#a58f6c480ae8b6252daa8ab5d9b6424a9", null ], - [ "instructions", "classbinlex_1_1Args.html#a3df7beb1118fb21b054ec86ef5314b34", null ], - [ "io_type", "classbinlex_1_1Args.html#a1081246280cb0c7db1996e7ec8ad1a4f", null ], - [ "list_modes", "classbinlex_1_1Args.html#a43a0dfcdb95d728359660a3fc2511bf9", null ], - [ "mode", "classbinlex_1_1Args.html#aa355ba71e7459d8175217787a2ab9355", null ], - [ "modes", "classbinlex_1_1Args.html#a68075815fcdbf18dcbf8ab2060bc14cb", null ], - [ "options", "classbinlex_1_1Args.html#ad7903e030130436a79d5e47dbac90a95", null ], - [ "output", "classbinlex_1_1Args.html#a7e087780eeeda9b14de02c0e271b92af", null ], - [ "pretty", "classbinlex_1_1Args.html#ae3622028422ff83cece80da50842ea14", null ], - [ "tags", "classbinlex_1_1Args.html#a518028a031f2d399dc14c4e280fc14cd", null ], - [ "threads", "classbinlex_1_1Args.html#af852011c5cdef9d5503970276552d659", null ], - [ "timeout", "classbinlex_1_1Args.html#a6a683acd46aa88bd1d62ed7f74c460f7", null ], - [ "version", "classbinlex_1_1Args.html#ac612b207d3378af1b0a30d701091bc8a", null ] -]; \ No newline at end of file diff --git a/docs/html/classbinlex_1_1Common-members.html b/docs/html/classbinlex_1_1Common-members.html deleted file mode 100644 index 8e50e679..00000000 --- a/docs/html/classbinlex_1_1Common-members.html +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - -binlex: Member List - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
binlex::Common Member List
-
-
- -

This is the complete list of members for binlex::Common, including all inherited members.

- - - - - - - - - - - - - - - - - - -
Entropy(string trait)binlex::Commonstatic
GetByteSize(string s)binlex::Commonstatic
GetFileSHA256(char *file_path)binlex::Commonstatic
GetFileTLSH(const char *file_path)binlex::Commonstatic
GetSHA256(const uint8_t *data, size_t len)binlex::Commonstatic
GetTLSH(const uint8_t *data, size_t len)binlex::Commonstatic
Hexdump(const char *desc, const void *addr, const int len)binlex::Commonstatic
HexdumpBE(const void *data, size_t size)binlex::Commonstatic
RemoveSpaces(string s)binlex::Commonstatic
RemoveWildcards(string trait)binlex::Commonstatic
SHA256(char *trait)binlex::Commonstatic
TraitToChar(string trait)binlex::Commonstatic
TraitToData(string trait)binlex::Commonstatic
TraitToTLSH(string bytes)binlex::Commonstatic
TrimRight(const std::string &s)binlex::Commonstatic
Wildcards(uint count)binlex::Commonstatic
WildcardTrait(string trait, string bytes)binlex::Commonstatic
-
- - - - diff --git a/docs/html/classbinlex_1_1Common.html b/docs/html/classbinlex_1_1Common.html deleted file mode 100644 index 88ca41c7..00000000 --- a/docs/html/classbinlex_1_1Common.html +++ /dev/null @@ -1,802 +0,0 @@ - - - - - - - -binlex: binlex::Common Class Reference - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
- -
-
binlex::Common Class Reference
-
-
-
-Inheritance diagram for binlex::Common:
-
-
- - -binlex::DecompilerBase -binlex::File -binlex::CILDecompiler -binlex::Decompiler -binlex::ELF -binlex::PE -binlex::Raw -binlex::DOTNET - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

-Static Public Member Functions

static BINLEX_EXPORT string GetTLSH (const uint8_t *data, size_t len)
 
static BINLEX_EXPORT string GetFileTLSH (const char *file_path)
 
static BINLEX_EXPORT string SHA256 (char *trait)
 
static BINLEX_EXPORT string GetFileSHA256 (char *file_path)
 
static BINLEX_EXPORT string GetSHA256 (const uint8_t *data, size_t len)
 
static BINLEX_EXPORT vector< char > TraitToChar (string trait)
 
static BINLEX_EXPORT string RemoveWildcards (string trait)
 
static BINLEX_EXPORT uint GetByteSize (string s)
 
static BINLEX_EXPORT string RemoveSpaces (string s)
 
static BINLEX_EXPORT string WildcardTrait (string trait, string bytes)
 
static BINLEX_EXPORT string TrimRight (const std::string &s)
 
static BINLEX_EXPORT string HexdumpBE (const void *data, size_t size)
 
static BINLEX_EXPORT string TraitToTLSH (string bytes)
 
static BINLEX_EXPORT vector< uint8_t > TraitToData (string trait)
 
static BINLEX_EXPORT string Wildcards (uint count)
 
static BINLEX_EXPORT float Entropy (string trait)
 
static BINLEX_EXPORT void Hexdump (const char *desc, const void *addr, const int len)
 
-

Member Function Documentation

- -

◆ Entropy()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT float binlex::Common::Entropy (string trait)
-
-static
-
-

Calculate the entropy of a trait.

Parameters
- - -
traittrait hex string
-
-
-
Returns
entropy float
- -
-
- -

◆ GetByteSize()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT uint binlex::Common::GetByteSize (string s)
-
-static
-
-

This method gets the size in bytes of a trait string (includes wildcards).

Parameters
- - -
traitinput trait string.
-
-
-
Returns
Returns uint size of bytes
- -
-
- -

◆ GetFileSHA256()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT string binlex::Common::GetFileSHA256 (char * file_path)
-
-static
-
-

This method reads a file returns its sha256 hash.

Parameters
- - -
file_pathpath to the file to read
-
-
-
Returns
Returns the sha256 hash of the file
-
Exceptions
- - -
std::runtime_errorif file operation fails
-
-
- -
-
- -

◆ GetFileTLSH()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT string binlex::Common::GetFileTLSH (const char * file_path)
-
-static
-
-

This method reads a file returns its tlsh hash.

Parameters
- - -
file_pathpath to the file to read
-
-
-
Returns
Returns the tlsh hash of the file
-
Exceptions
- - -
std::runtime_errorif file operation fails
-
-
- -
-
- -

◆ GetSHA256()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT string binlex::Common::GetSHA256 (const uint8_t * data,
size_t len 
)
-
-static
-
-

This method reads a file returns its sha256 hash.

Parameters
- - - -
datainput string
lenlength of data
-
-
-
Returns
Returns the sha256 hash of the file
-
Exceptions
- - -
std::runtime_errorif file operation fails
-
-
- -
-
- -

◆ GetTLSH()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT string binlex::Common::GetTLSH (const uint8_t * data,
size_t len 
)
-
-static
-
-

This class contains methods common to binlex. This method takes a (binary) input string and returns its tlsh hash.

Parameters
- - - -
datainput string
lenlength of data
-
-
-
Returns
Returns the tlsh hash of the trait string
- -
-
- -

◆ Hexdump()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT void binlex::Common::Hexdump (const char * desc,
const void * addr,
const int len 
)
-
-static
-
-

This method prints hexdump to stdout.

Parameters
- - - - -
descA description of the data.
dataA pointer to the data
sizeThe size of the data to collect
-
-
- -
-
- -

◆ HexdumpBE()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT string binlex::Common::HexdumpBE (const void * data,
size_t size 
)
-
-static
-
-

This method creates a byte string based on a pointer and its size.

Parameters
- - - -
dataA pointer to the data
sizeThe size of the data to collect
-
-
-
Returns
Returns a byte string of the selected data
- -
-
- -

◆ RemoveSpaces()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT string binlex::Common::RemoveSpaces (string s)
-
-static
-
-

This method removes spaces from a string.

Parameters
- - -
sinput string
-
-
-
Returns
Returns string without spaces
- -
-
- -

◆ RemoveWildcards()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT string binlex::Common::RemoveWildcards (string trait)
-
-static
-
-

This method removes wildcards from a trait string.

Parameters
- - -
traitinput trait string.
-
-
-
Returns
Returns trait without wildcards
- -
-
- -

◆ SHA256()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT string binlex::Common::SHA256 (char * trait)
-
-static
-
-

This method takes an input string and returns its sha256 hash.

Parameters
- - -
traitinput string.
-
-
-
Returns
Returns the sha256 hash of the trait string
- -
-
- -

◆ TraitToChar()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT vector<char> binlex::Common::TraitToChar (string trait)
-
-static
-
-

This method takes an input trait string and returns a char vector of bytes (ignores wildcards).

Parameters
- - -
traitinput string.
-
-
-
Returns
Returns char vector of bytes
- -
-
- -

◆ TraitToData()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT vector<uint8_t> binlex::Common::TraitToData (string trait)
-
-static
-
-

This method converts a trait to a uint_8 vector

Parameters
- - -
traitstring
-
-
-
Returns
uint8_t vector of data
- -
-
- -

◆ TraitToTLSH()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT string binlex::Common::TraitToTLSH (string bytes)
-
-static
-
-

This method converts bytes hex string to TLSH of the bytes.

Parameters
- - -
byteshex string of bytes
-
-
-
Returns
TLSH hash string
- -
-
- -

◆ TrimRight()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT string binlex::Common::TrimRight (const std::string & s)
-
-static
-
-

This method removes whitespace on the right.

Parameters
- - -
sinput string
-
-
-
Returns
Returns string with whitespace on right trimmed
- -
-
- -

◆ Wildcards()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT string binlex::Common::Wildcards (uint count)
-
-static
-
-

Generate Number of Wildcards

Parameters
- - -
countnumber of wildcard bytes to create
-
-
-
Returns
string
- -
-
- -

◆ WildcardTrait()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT string binlex::Common::WildcardTrait (string trait,
string bytes 
)
-
-static
-
-

This method wildcards byte strings for traits.

Parameters
- - - -
traitinput trait string
bytesbyte string to wildcard
-
-
-
Returns
Returns wildcarded trait string
- -
-
-
The documentation for this class was generated from the following file: -
-
- - - - diff --git a/docs/html/classbinlex_1_1Common.png b/docs/html/classbinlex_1_1Common.png deleted file mode 100644 index c8facf39..00000000 Binary files a/docs/html/classbinlex_1_1Common.png and /dev/null differ diff --git a/docs/html/classbinlex_1_1Decompiler-members.html b/docs/html/classbinlex_1_1Decompiler-members.html deleted file mode 100644 index bff0e3b7..00000000 --- a/docs/html/classbinlex_1_1Decompiler-members.html +++ /dev/null @@ -1,162 +0,0 @@ - - - - - - - -binlex: Member List - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
binlex::Decompiler Member List
-
-
- -

This is the complete list of members for binlex::Decompiler, including all inherited members.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
AddDiscoveredBlock(uint64_t address, struct Section *sections, uint index)binlex::Decompilerstatic
AppendQueue(set< uint64_t > &addresses, uint operand_type, uint index) (defined in binlex::Decompiler)binlex::Decompiler
AppendTrait(struct Trait *trait, struct Section *sections, uint index)binlex::Decompilerstatic
arch (defined in binlex::Decompiler)binlex::Decompilerstatic
ClearTrait(struct Trait *trait)binlex::Decompilerstatic
CollectInsn(cs_insn *insn, struct Section *sections, uint index)binlex::Decompilerstatic
CollectOperands(cs_insn *insn, int operand_type, struct Section *sections, uint index)binlex::Decompilerstatic
CreateTraitsForSection(uint index)binlex::Decompiler
Decompile(void *data, size_t data_size, size_t offset, uint index)binlex::Decompiler
Decompiler(const binlex::File &firef) (defined in binlex::Decompiler)binlex::Decompiler
DecompilerBase(const binlex::File &firef)binlex::DecompilerBase
Entropy(string trait)binlex::Commonstatic
file_referencebinlex::DecompilerBaseprotected
FinalizeTrait(struct Trait &trait) (defined in binlex::Decompiler)binlex::Decompilerstatic
FreeTraits(uint index) (defined in binlex::Decompiler)binlex::Decompiler
GetByteSize(string s)binlex::Commonstatic
GetFileSHA256(char *file_path)binlex::Commonstatic
GetFileTLSH(const char *file_path)binlex::Commonstatic
GetSHA256(const uint8_t *data, size_t len)binlex::Commonstatic
GetTLSH(const uint8_t *data, size_t len)binlex::Commonstatic
GetTrait(struct Trait &trait)binlex::Decompiler
GetTraits()binlex::Decompilervirtual
Hexdump(const char *desc, const void *addr, const int len)binlex::Commonstatic
HexdumpBE(const void *data, size_t size)binlex::Commonstatic
IsAddress(map< uint64_t, uint > &addresses, uint64_t address)binlex::Decompiler
IsBlock(map< uint64_t, uint > &addresses, uint64_t address)binlex::Decompilerstatic
IsConditionalInsn(cs_insn *insn)binlex::Decompilerstatic
IsEndInsn(cs_insn *insn)binlex::Decompilerstatic
IsFunction(map< uint64_t, uint > &addresses, uint64_t address)binlex::Decompilerstatic
IsNopInsn(cs_insn *ins)binlex::Decompilerstatic
IsPrivInsn(cs_insn *ins)binlex::Decompilerstatic
IsSemanticNopInsn(cs_insn *ins)binlex::Decompilerstatic
IsTrapInsn(cs_insn *ins)binlex::Decompilerstatic
IsVisited(map< uint64_t, int > &visited, uint64_t address)binlex::Decompilerstatic
IsWildcardInsn(cs_insn *insn)binlex::Decompilerstatic
LinearDisassemble(void *data, size_t data_size, size_t offset, uint index)binlex::Decompiler
MaxAddress(set< uint64_t > coverage)binlex::Decompilerstatic
mode (defined in binlex::Decompiler)binlex::Decompilerstatic
py_SetCorpus(const char *corpus)binlex::DecompilerBase
py_SetInstructions(bool instructions)binlex::DecompilerBase
py_SetMode(string mode)binlex::DecompilerBase
py_SetTags(const vector< string > &tags)binlex::DecompilerBase
py_SetThreads(uint threads)binlex::DecompilerBase
RemoveSpaces(string s)binlex::Commonstatic
RemoveWildcards(string trait)binlex::Commonstatic
sections (defined in binlex::Decompiler)binlex::Decompiler
SetInstructions(bool instructions, uint index)binlex::Decompiler
Setup(cs_arch architecture, cs_mode mode_type)binlex::Decompiler
SHA256(char *trait)binlex::Commonstatic
TraitToChar(string trait)binlex::Commonstatic
TraitToData(string trait)binlex::Commonstatic
TraitToTLSH(string bytes)binlex::Commonstatic
TraitWorker(void *args) (defined in binlex::Decompiler)binlex::Decompilerstatic
TrimRight(const std::string &s)binlex::Commonstatic
WildcardInsn(cs_insn *insn)binlex::Decompilerstatic
Wildcards(uint count)binlex::Commonstatic
WildcardTrait(string trait, string bytes)binlex::Commonstatic
WriteTraits()binlex::DecompilerBase
~Decompiler() (defined in binlex::Decompiler)binlex::Decompiler
-
- - - - diff --git a/docs/html/classbinlex_1_1Decompiler.html b/docs/html/classbinlex_1_1Decompiler.html deleted file mode 100644 index c5e3f677..00000000 --- a/docs/html/classbinlex_1_1Decompiler.html +++ /dev/null @@ -1,1221 +0,0 @@ - - - - - - - -binlex: binlex::Decompiler Class Reference - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- - -
-
-Inheritance diagram for binlex::Decompiler:
-
-
- - -binlex::DecompilerBase -binlex::Common - -
- - - - - - -

-Classes

struct  Section
 
struct  Trait
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

-Public Member Functions

-BINLEX_EXPORT Decompiler (const binlex::File &firef)
 
BINLEX_EXPORT bool Setup (cs_arch architecture, cs_mode mode_type)
 
BINLEX_EXPORT void SetInstructions (bool instructions, uint index)
 
BINLEX_EXPORT void * CreateTraitsForSection (uint index)
 
BINLEX_EXPORT void LinearDisassemble (void *data, size_t data_size, size_t offset, uint index)
 
BINLEX_EXPORT void Decompile (void *data, size_t data_size, size_t offset, uint index)
 
-BINLEX_EXPORT void FreeTraits (uint index)
 
BINLEX_EXPORT bool IsAddress (map< uint64_t, uint > &addresses, uint64_t address)
 
BINLEX_EXPORT json GetTrait (struct Trait &trait)
 
vector< json > GetTraits ()
 
-BINLEX_EXPORT void AppendQueue (set< uint64_t > &addresses, uint operand_type, uint index)
 
- Public Member Functions inherited from binlex::DecompilerBase
 DecompilerBase (const binlex::File &firef)
 
BINLEX_EXPORT void WriteTraits ()
 
BINLEX_EXPORT void py_SetThreads (uint threads)
 
BINLEX_EXPORT void py_SetCorpus (const char *corpus)
 
BINLEX_EXPORT void py_SetInstructions (bool instructions)
 
BINLEX_EXPORT void py_SetTags (const vector< string > &tags)
 
BINLEX_EXPORT void py_SetMode (string mode)
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

-Static Public Member Functions

static BINLEX_EXPORT void AddDiscoveredBlock (uint64_t address, struct Section *sections, uint index)
 
static BINLEX_EXPORT void CollectOperands (cs_insn *insn, int operand_type, struct Section *sections, uint index)
 
static BINLEX_EXPORT uint CollectInsn (cs_insn *insn, struct Section *sections, uint index)
 
static BINLEX_EXPORT void AppendTrait (struct Trait *trait, struct Section *sections, uint index)
 
static BINLEX_EXPORT bool IsNopInsn (cs_insn *ins)
 
static BINLEX_EXPORT bool IsSemanticNopInsn (cs_insn *ins)
 
static BINLEX_EXPORT bool IsTrapInsn (cs_insn *ins)
 
static BINLEX_EXPORT bool IsPrivInsn (cs_insn *ins)
 
static BINLEX_EXPORT bool IsEndInsn (cs_insn *insn)
 
static BINLEX_EXPORT uint IsConditionalInsn (cs_insn *insn)
 
static BINLEX_EXPORT uint64_t MaxAddress (set< uint64_t > coverage)
 
static BINLEX_EXPORT bool IsFunction (map< uint64_t, uint > &addresses, uint64_t address)
 
static BINLEX_EXPORT bool IsBlock (map< uint64_t, uint > &addresses, uint64_t address)
 
static BINLEX_EXPORT bool IsVisited (map< uint64_t, int > &visited, uint64_t address)
 
static BINLEX_EXPORT bool IsWildcardInsn (cs_insn *insn)
 
static BINLEX_EXPORT string WildcardInsn (cs_insn *insn)
 
static BINLEX_EXPORT void ClearTrait (struct Trait *trait)
 
-static BINLEX_EXPORT void * TraitWorker (void *args)
 
-static BINLEX_EXPORT void * FinalizeTrait (struct Trait &trait)
 
- Static Public Member Functions inherited from binlex::Common
static BINLEX_EXPORT string GetTLSH (const uint8_t *data, size_t len)
 
static BINLEX_EXPORT string GetFileTLSH (const char *file_path)
 
static BINLEX_EXPORT string SHA256 (char *trait)
 
static BINLEX_EXPORT string GetFileSHA256 (char *file_path)
 
static BINLEX_EXPORT string GetSHA256 (const uint8_t *data, size_t len)
 
static BINLEX_EXPORT vector< char > TraitToChar (string trait)
 
static BINLEX_EXPORT string RemoveWildcards (string trait)
 
static BINLEX_EXPORT uint GetByteSize (string s)
 
static BINLEX_EXPORT string RemoveSpaces (string s)
 
static BINLEX_EXPORT string WildcardTrait (string trait, string bytes)
 
static BINLEX_EXPORT string TrimRight (const std::string &s)
 
static BINLEX_EXPORT string HexdumpBE (const void *data, size_t size)
 
static BINLEX_EXPORT string TraitToTLSH (string bytes)
 
static BINLEX_EXPORT vector< uint8_t > TraitToData (string trait)
 
static BINLEX_EXPORT string Wildcards (uint count)
 
static BINLEX_EXPORT float Entropy (string trait)
 
static BINLEX_EXPORT void Hexdump (const char *desc, const void *addr, const int len)
 
- - - -

-Public Attributes

-struct Section sections [DECOMPILER_MAX_SECTIONS]
 
- - - - - -

-Static Public Attributes

-static cs_arch arch
 
-static cs_mode mode
 
- - - - -

-Additional Inherited Members

- Protected Attributes inherited from binlex::DecompilerBase
const binlex::Filefile_reference
 
-

Member Function Documentation

- -

◆ AddDiscoveredBlock()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT void binlex::Decompiler::AddDiscoveredBlock (uint64_t address,
struct Sectionsections,
uint index 
)
-
-static
-
-

Add discovered block address to queue

Parameters
- - - - -
addressthe block address
operand_typethe operand type
indexthe section index
-
-
-
Returns
bool
- -
-
- -

◆ AppendTrait()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT void binlex::Decompiler::AppendTrait (struct Traittrait,
struct Sectionsections,
uint index 
)
-
-static
-
-

Append Additional Traits

Parameters
- - - - -
traittrait to append
sectionssections pointer
indexthe section index
-
-
-
Returns
bool
- -
-
- -

◆ ClearTrait()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT void binlex::Decompiler::ClearTrait (struct Traittrait)
-
-static
-
-

Clear Trait Values Except Type

Parameters
- - -
traitthe trait struct address
-
-
- -
-
- -

◆ CollectInsn()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT uint binlex::Decompiler::CollectInsn (cs_insn * insn,
struct Sectionsections,
uint index 
)
-
-static
-
-

Collect Instructions for Processing

Parameters
- - - -
insnthe instruction
indexthe section index
-
-
-
Returns
operand type
- -
-
- -

◆ CollectOperands()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT void binlex::Decompiler::CollectOperands (cs_insn * insn,
int operand_type,
struct Sectionsections,
uint index 
)
-
-static
-
-

Collect Function and Conditional Operands for Processing

Parameters
- - - - -
insnthe instruction
operand_typethe operand type
indexthe section index
-
-
-
Returns
bool
- -
-
- -

◆ CreateTraitsForSection()

- -
-
- - - - - - - - -
BINLEX_EXPORT void* binlex::Decompiler::CreateTraitsForSection (uint index)
-
-

Create traits from data contains in binary sections

Parameters
- - -
argspointer to worker arguments
-
-
-
Returns
NULL
- -
-
- -

◆ Decompile()

- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BINLEX_EXPORT void binlex::Decompiler::Decompile (void * data,
size_t data_size,
size_t offset,
uint index 
)
-
-

Decompiles Target Data

Parameters
- - - - - -
datapointer to data
data_sizesize of data
offsetinclude section offset
indexthe section index
-
-
- -
-
- -

◆ GetTrait()

- -
-
- - - - - - - - -
BINLEX_EXPORT json binlex::Decompiler::GetTrait (struct Traittrait)
-
-

Gets Trait as JSON

Parameters
- - -
traitpointer to trait structure
-
-
-
Returns
json string
- -
-
- -

◆ GetTraits()

- -
-
- - - - - -
- - - - - - - -
vector<json> binlex::Decompiler::GetTraits ()
-
-virtual
-
-

Get Traits as JSON

Returns
list of traits json objects
- -

Implements binlex::DecompilerBase.

- -
-
- -

◆ IsAddress()

- -
-
- - - - - - - - - - - - - - - - - - -
BINLEX_EXPORT bool binlex::Decompiler::IsAddress (map< uint64_t, uint > & addresses,
uint64_t address 
)
-
-

Check if Function or Block Address Collected

Parameters
- - -
addressthe address to check
-
-
-
Returns
bool
- -
-
- -

◆ IsBlock()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT bool binlex::Decompiler::IsBlock (map< uint64_t, uint > & addresses,
uint64_t address 
)
-
-static
-
-

Checks if Address if Function

Parameters
- - -
addressaddress to check
-
-
-
Returns
bool
- -
-
- -

◆ IsConditionalInsn()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT uint binlex::Decompiler::IsConditionalInsn (cs_insn * insn)
-
-static
-
-

Checks if Instruction is Conditional

Parameters
- - -
insnthe instruction
-
-
-
Returns
edges if > 0; then is conditional
- -
-
- -

◆ IsEndInsn()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT bool binlex::Decompiler::IsEndInsn (cs_insn * insn)
-
-static
-
-

Checks if the Instruction is an Ending Instruction

Parameters
- - -
insnthe instruction
-
-
-
Returns
bool
- -
-
- -

◆ IsFunction()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT bool binlex::Decompiler::IsFunction (map< uint64_t, uint > & addresses,
uint64_t address 
)
-
-static
-
-

Checks if Address if Function

Parameters
- - -
addressaddress to check
-
-
-
Returns
bool
- -
-
- -

◆ IsNopInsn()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT bool binlex::Decompiler::IsNopInsn (cs_insn * ins)
-
-static
-
-

Checks if the Instruction is a nop

Parameters
- - -
insnthe instruction
-
-
-
Returns
bool
- -
-
- -

◆ IsPrivInsn()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT bool binlex::Decompiler::IsPrivInsn (cs_insn * ins)
-
-static
-
-

Checks if the Instruction is privileged

Parameters
- - -
insnthe instruction
-
-
-
Returns
bool
- -
-
- -

◆ IsSemanticNopInsn()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT bool binlex::Decompiler::IsSemanticNopInsn (cs_insn * ins)
-
-static
-
-

Checks if the Instruction is a semantic nop (padding)

Parameters
- - -
insnthe instruction
-
-
-
Returns
bool
- -
-
- -

◆ IsTrapInsn()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT bool binlex::Decompiler::IsTrapInsn (cs_insn * ins)
-
-static
-
-

Checks if the Instruction is a trap

Parameters
- - -
insnthe instruction
-
-
-
Returns
bool
- -
-
- -

◆ IsVisited()

- -
-
- - - - - -
- - - - - - - - - - - - - - - - - - -
static BINLEX_EXPORT bool binlex::Decompiler::IsVisited (map< uint64_t, int > & visited,
uint64_t address 
)
-
-static
-
-

Checks if Address was Already Visited

Parameters
- - -
addressaddress to check
-
-
-
Returns
bool
- -
-
- -

◆ IsWildcardInsn()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT bool binlex::Decompiler::IsWildcardInsn (cs_insn * insn)
-
-static
-
-

Checks if Instruction is Wildcard Instruction

Parameters
- - -
insnthe instruction
-
-
-
Returns
bool
- -
-
- -

◆ LinearDisassemble()

- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
BINLEX_EXPORT void binlex::Decompiler::LinearDisassemble (void * data,
size_t data_size,
size_t offset,
uint index 
)
-
-

Performs a linear disassembly of the data

Parameters
- - - - - -
datapointer to data
data_sizesize of data
offsetinclude section offset
indexthe section index
-
-
- -
-
- -

◆ MaxAddress()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT uint64_t binlex::Decompiler::MaxAddress (set< uint64_t > coverage)
-
-static
-
-

Checks Code Coverage for Max Address

Parameters
- - -
coverageset of addresses decompiled
-
-
-
Returns
the maximum address from the set
- -
-
- -

◆ SetInstructions()

- -
-
- - - - - - - - - - - - - - - - - - -
BINLEX_EXPORT void binlex::Decompiler::SetInstructions (bool instructions,
uint index 
)
-
-
Parameters
- - - -
instructionsbool to collect instructions traits or not
indexthe section index
-
-
- -
-
- -

◆ Setup()

- -
-
- - - - - - - - - - - - - - - - - - -
BINLEX_EXPORT bool binlex::Decompiler::Setup (cs_arch architecture,
cs_mode mode_type 
)
-
-

Set up Capstone Decompiler Architecure and Mode

Parameters
- - - -
architectureCapstone Decompiler Architecure
mode_typeCapstone Mode
-
-
-
Returns
bool
- -
-
- -

◆ WildcardInsn()

- -
-
- - - - - -
- - - - - - - - -
static BINLEX_EXPORT string binlex::Decompiler::WildcardInsn (cs_insn * insn)
-
-static
-
-

Wildcard Instruction

Parameters
- - -
insnthe instruction
-
-
-
Returns
trait wildcard byte string
- -
-
-
The documentation for this class was generated from the following file: -
-
- - - - diff --git a/docs/html/classbinlex_1_1Decompiler.js b/docs/html/classbinlex_1_1Decompiler.js deleted file mode 100644 index 883a6b66..00000000 --- a/docs/html/classbinlex_1_1Decompiler.js +++ /dev/null @@ -1,18 +0,0 @@ -var classbinlex_1_1Decompiler = -[ - [ "Section", "structbinlex_1_1Decompiler_1_1Section.html", "structbinlex_1_1Decompiler_1_1Section" ], - [ "Trait", "structbinlex_1_1Decompiler_1_1Trait.html", "structbinlex_1_1Decompiler_1_1Trait" ], - [ "Decompiler", "classbinlex_1_1Decompiler.html#a995fcc2cfec37edbd3f359a849f338ff", null ], - [ "~Decompiler", "classbinlex_1_1Decompiler.html#a08c4e24059b89499a9926f793c468f22", null ], - [ "AppendQueue", "classbinlex_1_1Decompiler.html#adf563022c163cdb2afbedca90b38769b", null ], - [ "CreateTraitsForSection", "classbinlex_1_1Decompiler.html#ae1a9193c009a4adcfd20441dc9896efe", null ], - [ "Decompile", "classbinlex_1_1Decompiler.html#acf647c06620257b5dc942c02c674e54f", null ], - [ "FreeTraits", "classbinlex_1_1Decompiler.html#a22fe3fbb20dd55fbaa4b64f28afaddbc", null ], - [ "GetTrait", "classbinlex_1_1Decompiler.html#a1f766992f51fb6a39f26e0a111a3fd06", null ], - [ "GetTraits", "classbinlex_1_1Decompiler.html#afc5ee0847582e4df55b12bdb36e5ac1b", null ], - [ "IsAddress", "classbinlex_1_1Decompiler.html#a4b98cc138ec7b506f54abf66cedd058d", null ], - [ "LinearDisassemble", "classbinlex_1_1Decompiler.html#a0045a92875a089edb6e862387851bb4d", null ], - [ "SetInstructions", "classbinlex_1_1Decompiler.html#a48c0658768bd8fc5683b964141103e68", null ], - [ "Setup", "classbinlex_1_1Decompiler.html#a87c28dc8b055a327f8c0502ed84c02f8", null ], - [ "sections", "classbinlex_1_1Decompiler.html#afcba0f93a71ab4ce1237bac70a4b43d9", null ] -]; \ No newline at end of file diff --git a/docs/html/classbinlex_1_1Decompiler.png b/docs/html/classbinlex_1_1Decompiler.png deleted file mode 100644 index ac2e21ba..00000000 Binary files a/docs/html/classbinlex_1_1Decompiler.png and /dev/null differ diff --git a/docs/html/classbinlex_1_1ELF-members.html b/docs/html/classbinlex_1_1ELF-members.html deleted file mode 100644 index 64c5420a..00000000 --- a/docs/html/classbinlex_1_1ELF-members.html +++ /dev/null @@ -1,135 +0,0 @@ - - - - - - - -binlex: Member List - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
binlex::ELF Member List
-
-
- -

This is the complete list of members for binlex::ELF, including all inherited members.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
binary (defined in binlex::ELF)binlex::ELF
CalculateFileHashes(char *file_path)binlex::File
CalculateFileHashes(const vector< uint8_t > &data)binlex::File
ELF() (defined in binlex::ELF)binlex::ELF
Entropy(string trait)binlex::Commonstatic
GetByteSize(string s)binlex::Commonstatic
GetFileSHA256(char *file_path)binlex::Commonstatic
GetFileTLSH(const char *file_path)binlex::Commonstatic
GetSHA256(const uint8_t *data, size_t len)binlex::Commonstatic
GetTLSH(const uint8_t *data, size_t len)binlex::Commonstatic
Hexdump(const char *desc, const void *addr, const int len)binlex::Commonstatic
HexdumpBE(const void *data, size_t size)binlex::Commonstatic
mode (defined in binlex::ELF)binlex::ELF
ReadBuffer(void *data, size_t size)binlex::File
ReadFile(const char *file_path)binlex::File
ReadFileIntoVector(const char *file_path)binlex::File
ReadVector(const std::vector< uint8_t > &data)binlex::ELFvirtual
RemoveSpaces(string s)binlex::Commonstatic
RemoveWildcards(string trait)binlex::Commonstatic
sections (defined in binlex::ELF)binlex::ELF
Setup(ARCH input_mode)binlex::ELF
SHA256(char *trait)binlex::Commonstatic
sha256binlex::File
tlsh (defined in binlex::File)binlex::File
total_exec_sections (defined in binlex::ELF)binlex::ELF
TraitToChar(string trait)binlex::Commonstatic
TraitToData(string trait)binlex::Commonstatic
TraitToTLSH(string bytes)binlex::Commonstatic
TrimRight(const std::string &s)binlex::Commonstatic
Wildcards(uint count)binlex::Commonstatic
WildcardTrait(string trait, string bytes)binlex::Commonstatic
~ELF() (defined in binlex::ELF)binlex::ELF
-
- - - - diff --git a/docs/html/classbinlex_1_1ELF.html b/docs/html/classbinlex_1_1ELF.html deleted file mode 100644 index 3079ff5e..00000000 --- a/docs/html/classbinlex_1_1ELF.html +++ /dev/null @@ -1,257 +0,0 @@ - - - - - - - -binlex: binlex::ELF Class Reference - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
- -
-
binlex::ELF Class Reference
-
-
-
-Inheritance diagram for binlex::ELF:
-
-
- - -binlex::File -binlex::Common - -
- - - - - - - - - - - - - - - - - -

-Public Member Functions

BINLEX_EXPORT bool Setup (ARCH input_mode)
 
virtual bool ReadVector (const std::vector< uint8_t > &data)
 
- Public Member Functions inherited from binlex::File
void CalculateFileHashes (char *file_path)
 
void CalculateFileHashes (const vector< uint8_t > &data)
 
vector< uint8_t > ReadFileIntoVector (const char *file_path)
 
bool ReadFile (const char *file_path)
 
bool ReadBuffer (void *data, size_t size)
 
- - - - - - - - - - - - - - -

-Public Attributes

-ARCH mode = ARCH::EM_NONE
 
-unique_ptr< LIEF::ELF::Binary > binary
 
-struct Section sections [BINARY_MAX_SECTIONS]
 
-uint32_t total_exec_sections
 
- Public Attributes inherited from binlex::File
string sha256
 
-string tlsh
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

-Additional Inherited Members

- Static Public Member Functions inherited from binlex::Common
static BINLEX_EXPORT string GetTLSH (const uint8_t *data, size_t len)
 
static BINLEX_EXPORT string GetFileTLSH (const char *file_path)
 
static BINLEX_EXPORT string SHA256 (char *trait)
 
static BINLEX_EXPORT string GetFileSHA256 (char *file_path)
 
static BINLEX_EXPORT string GetSHA256 (const uint8_t *data, size_t len)
 
static BINLEX_EXPORT vector< char > TraitToChar (string trait)
 
static BINLEX_EXPORT string RemoveWildcards (string trait)
 
static BINLEX_EXPORT uint GetByteSize (string s)
 
static BINLEX_EXPORT string RemoveSpaces (string s)
 
static BINLEX_EXPORT string WildcardTrait (string trait, string bytes)
 
static BINLEX_EXPORT string TrimRight (const std::string &s)
 
static BINLEX_EXPORT string HexdumpBE (const void *data, size_t size)
 
static BINLEX_EXPORT string TraitToTLSH (string bytes)
 
static BINLEX_EXPORT vector< uint8_t > TraitToData (string trait)
 
static BINLEX_EXPORT string Wildcards (uint count)
 
static BINLEX_EXPORT float Entropy (string trait)
 
static BINLEX_EXPORT void Hexdump (const char *desc, const void *addr, const int len)
 
-

Member Function Documentation

- -

◆ ReadVector()

- -
-
- - - - - -
- - - - - - - - -
virtual bool binlex::ELF::ReadVector (const std::vector< uint8_t > & data)
-
-virtual
-
-

This method reads an ELF file from a buffer.

Parameters
- - -
datavector
-
-
-
Returns
bool
- -

Implements binlex::File.

- -
-
- -

◆ Setup()

- -
-
- - - - - - - - -
BINLEX_EXPORT bool binlex::ELF::Setup (ARCH input_mode)
-
-

This method sets the architecture of the ELF file you wish to read.

Parameters
- - -
input_modearchitecure of the file
-
-
-
Returns
bool
- -
-
-
The documentation for this class was generated from the following file: -
-
- - - - diff --git a/docs/html/classbinlex_1_1ELF.js b/docs/html/classbinlex_1_1ELF.js deleted file mode 100644 index fed34f70..00000000 --- a/docs/html/classbinlex_1_1ELF.js +++ /dev/null @@ -1,11 +0,0 @@ -var classbinlex_1_1ELF = -[ - [ "ELF", "classbinlex_1_1ELF.html#a0394eac47e29f35f1c5f0964b6f64617", null ], - [ "~ELF", "classbinlex_1_1ELF.html#a814c6a2ae989464ad9a42dec0c8e25df", null ], - [ "ReadVector", "classbinlex_1_1ELF.html#a4674a8acf19db9a120ec0af10ed25538", null ], - [ "Setup", "classbinlex_1_1ELF.html#a1fedaeda9047ad1b92d720d7564d88e1", null ], - [ "binary", "classbinlex_1_1ELF.html#a222bd1da73be1d76e1edac5046445e23", null ], - [ "mode", "classbinlex_1_1ELF.html#a3943072eb43b4bdd5f00fd23707cb3ea", null ], - [ "sections", "classbinlex_1_1ELF.html#aa4381fcf4eae5275b684c1a0cbf4a724", null ], - [ "total_exec_sections", "classbinlex_1_1ELF.html#ad1a3e3c992bd38474aa0946b3378cb64", null ] -]; \ No newline at end of file diff --git a/docs/html/classbinlex_1_1ELF.png b/docs/html/classbinlex_1_1ELF.png deleted file mode 100644 index b19c24c5..00000000 Binary files a/docs/html/classbinlex_1_1ELF.png and /dev/null differ diff --git a/docs/html/classbinlex_1_1PE-members.html b/docs/html/classbinlex_1_1PE-members.html deleted file mode 100644 index 2c94ed29..00000000 --- a/docs/html/classbinlex_1_1PE-members.html +++ /dev/null @@ -1,137 +0,0 @@ - - - - - - - -binlex: Member List - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
binlex::PE Member List
-
-
- -

This is the complete list of members for binlex::PE, including all inherited members.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
binary (defined in binlex::PE)binlex::PE
CalculateFileHashes(char *file_path)binlex::File
CalculateFileHashes(const vector< uint8_t > &data)binlex::File
Entropy(string trait)binlex::Commonstatic
GetByteSize(string s)binlex::Commonstatic
GetFileSHA256(char *file_path)binlex::Commonstatic
GetFileTLSH(const char *file_path)binlex::Commonstatic
GetSHA256(const uint8_t *data, size_t len)binlex::Commonstatic
GetTLSH(const uint8_t *data, size_t len)binlex::Commonstatic
HasLimitations()binlex::PE
Hexdump(const char *desc, const void *addr, const int len)binlex::Commonstatic
HexdumpBE(const void *data, size_t size)binlex::Commonstatic
IsDotNet()binlex::PE
mode (defined in binlex::PE)binlex::PE
PE() (defined in binlex::PE)binlex::PE
ReadBuffer(void *data, size_t size)binlex::File
ReadFile(const char *file_path)binlex::File
ReadFileIntoVector(const char *file_path)binlex::File
ReadVector(const std::vector< uint8_t > &data)binlex::PEvirtual
RemoveSpaces(string s)binlex::Commonstatic
RemoveWildcards(string trait)binlex::Commonstatic
sections (defined in binlex::PE)binlex::PE
Setup(MACHINE_TYPES input_mode)binlex::PE
sha256binlex::File
SHA256(char *trait)binlex::Commonstatic
tlsh (defined in binlex::File)binlex::File
total_exec_sections (defined in binlex::PE)binlex::PE
TraitToChar(string trait)binlex::Commonstatic
TraitToData(string trait)binlex::Commonstatic
TraitToTLSH(string bytes)binlex::Commonstatic
TrimRight(const std::string &s)binlex::Commonstatic
Wildcards(uint count)binlex::Commonstatic
WildcardTrait(string trait, string bytes)binlex::Commonstatic
~PE() (defined in binlex::PE)binlex::PE
-
- - - - diff --git a/docs/html/classbinlex_1_1PE.html b/docs/html/classbinlex_1_1PE.html deleted file mode 100644 index 136cf7da..00000000 --- a/docs/html/classbinlex_1_1PE.html +++ /dev/null @@ -1,300 +0,0 @@ - - - - - - - -binlex: binlex::PE Class Reference - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
- -
-
binlex::PE Class Reference
-
-
-
-Inheritance diagram for binlex::PE:
-
-
- - -binlex::File -binlex::Common -binlex::DOTNET - -
- - - - - - - - - - - - - - - - - - - - - -

-Public Member Functions

BINLEX_EXPORT bool Setup (MACHINE_TYPES input_mode)
 
BINLEX_EXPORT bool IsDotNet ()
 
BINLEX_EXPORT bool HasLimitations ()
 
virtual bool ReadVector (const std::vector< uint8_t > &data)
 
- Public Member Functions inherited from binlex::File
void CalculateFileHashes (char *file_path)
 
void CalculateFileHashes (const vector< uint8_t > &data)
 
vector< uint8_t > ReadFileIntoVector (const char *file_path)
 
bool ReadFile (const char *file_path)
 
bool ReadBuffer (void *data, size_t size)
 
- - - - - - - - - - - - - - -

-Public Attributes

-MACHINE_TYPES mode = MACHINE_TYPES::IMAGE_FILE_MACHINE_UNKNOWN
 
-unique_ptr< LIEF::PE::Binary > binary
 
-struct Section sections [BINARY_MAX_SECTIONS]
 
-uint32_t total_exec_sections
 
- Public Attributes inherited from binlex::File
string sha256
 
-string tlsh
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

-Additional Inherited Members

- Static Public Member Functions inherited from binlex::Common
static BINLEX_EXPORT string GetTLSH (const uint8_t *data, size_t len)
 
static BINLEX_EXPORT string GetFileTLSH (const char *file_path)
 
static BINLEX_EXPORT string SHA256 (char *trait)
 
static BINLEX_EXPORT string GetFileSHA256 (char *file_path)
 
static BINLEX_EXPORT string GetSHA256 (const uint8_t *data, size_t len)
 
static BINLEX_EXPORT vector< char > TraitToChar (string trait)
 
static BINLEX_EXPORT string RemoveWildcards (string trait)
 
static BINLEX_EXPORT uint GetByteSize (string s)
 
static BINLEX_EXPORT string RemoveSpaces (string s)
 
static BINLEX_EXPORT string WildcardTrait (string trait, string bytes)
 
static BINLEX_EXPORT string TrimRight (const std::string &s)
 
static BINLEX_EXPORT string HexdumpBE (const void *data, size_t size)
 
static BINLEX_EXPORT string TraitToTLSH (string bytes)
 
static BINLEX_EXPORT vector< uint8_t > TraitToData (string trait)
 
static BINLEX_EXPORT string Wildcards (uint count)
 
static BINLEX_EXPORT float Entropy (string trait)
 
static BINLEX_EXPORT void Hexdump (const char *desc, const void *addr, const int len)
 
-

Member Function Documentation

- -

◆ HasLimitations()

- -
-
- - - - - - - -
BINLEX_EXPORT bool binlex::PE::HasLimitations ()
-
-

Check if the file has limitations that may result in invalid traits.

Returns
bool
- -
-
- -

◆ IsDotNet()

- -
-
- - - - - - - -
BINLEX_EXPORT bool binlex::PE::IsDotNet ()
-
-

Check if the PE file is a .NET file

Returns
bool
- -
-
- -

◆ ReadVector()

- -
-
- - - - - -
- - - - - - - - -
virtual bool binlex::PE::ReadVector (const std::vector< uint8_t > & data)
-
-virtual
-
-

Read data vector pointer.

Parameters
- - -
datavector of uint8_t data.
-
-
-
Returns
bool
- -

Implements binlex::File.

- -

Reimplemented in binlex::DOTNET.

- -
-
- -

◆ Setup()

- -
-
- - - - - - - - -
BINLEX_EXPORT bool binlex::PE::Setup (MACHINE_TYPES input_mode)
-
-

Setup to Read Specific PE Format

Parameters
- - -
input_modeMACHINE_TYPES::IMAGE_FILE_MACHINE_<arch>
-
-
-
Returns
bool
- -
-
-
The documentation for this class was generated from the following file: -
-
- - - - diff --git a/docs/html/classbinlex_1_1PE.js b/docs/html/classbinlex_1_1PE.js deleted file mode 100644 index 1ac21b2c..00000000 --- a/docs/html/classbinlex_1_1PE.js +++ /dev/null @@ -1,13 +0,0 @@ -var classbinlex_1_1PE = -[ - [ "PE", "classbinlex_1_1PE.html#a3c55b3f95f760907c695ac4cf5f620d1", null ], - [ "~PE", "classbinlex_1_1PE.html#a50d93d36adff55df2ee9659f1539b362", null ], - [ "HasLimitations", "classbinlex_1_1PE.html#afc5cd018a384c015a298fe97d3658a3e", null ], - [ "IsDotNet", "classbinlex_1_1PE.html#a7d59d7af11c403d379d4b748065517d6", null ], - [ "ReadVector", "classbinlex_1_1PE.html#a4722bee8f84fa658852eb429d7e2399c", null ], - [ "Setup", "classbinlex_1_1PE.html#aaf2fa4d79bc0fc9449eadd366b870d6b", null ], - [ "binary", "classbinlex_1_1PE.html#a819ef05e9bb17dc5fac3443dd5e4328d", null ], - [ "mode", "classbinlex_1_1PE.html#a5bce348267c07fa8b1c9f195a4525ed6", null ], - [ "sections", "classbinlex_1_1PE.html#a4f66c24dddeba3c650c0158ba3e4a220", null ], - [ "total_exec_sections", "classbinlex_1_1PE.html#a399768f4c205f01df0c1172898731d5e", null ] -]; \ No newline at end of file diff --git a/docs/html/classbinlex_1_1Raw-members.html b/docs/html/classbinlex_1_1Raw-members.html deleted file mode 100644 index c9c64346..00000000 --- a/docs/html/classbinlex_1_1Raw-members.html +++ /dev/null @@ -1,132 +0,0 @@ - - - - - - - -binlex: Member List - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
binlex::Raw Member List
-
-
- -

This is the complete list of members for binlex::Raw, including all inherited members.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CalculateFileHashes(char *file_path)binlex::File
CalculateFileHashes(const vector< uint8_t > &data)binlex::File
Entropy(string trait)binlex::Commonstatic
GetByteSize(string s)binlex::Commonstatic
GetFileSHA256(char *file_path)binlex::Commonstatic
GetFileSize(FILE *fd)binlex::Raw
GetFileTLSH(const char *file_path)binlex::Commonstatic
GetSHA256(const uint8_t *data, size_t len)binlex::Commonstatic
GetTLSH(const uint8_t *data, size_t len)binlex::Commonstatic
Hexdump(const char *desc, const void *addr, const int len)binlex::Commonstatic
HexdumpBE(const void *data, size_t size)binlex::Commonstatic
Raw() (defined in binlex::Raw)binlex::Raw
ReadBuffer(void *data, size_t size)binlex::File
ReadFile(const char *file_path)binlex::File
ReadFileIntoVector(const char *file_path)binlex::File
ReadVector(const std::vector< uint8_t > &data)binlex::Rawvirtual
RemoveSpaces(string s)binlex::Commonstatic
RemoveWildcards(string trait)binlex::Commonstatic
sections (defined in binlex::Raw)binlex::Raw
sha256binlex::File
SHA256(char *trait)binlex::Commonstatic
tlsh (defined in binlex::File)binlex::File
TraitToChar(string trait)binlex::Commonstatic
TraitToData(string trait)binlex::Commonstatic
TraitToTLSH(string bytes)binlex::Commonstatic
TrimRight(const std::string &s)binlex::Commonstatic
Wildcards(uint count)binlex::Commonstatic
WildcardTrait(string trait, string bytes)binlex::Commonstatic
~Raw() (defined in binlex::Raw)binlex::Raw
-
- - - - diff --git a/docs/html/classbinlex_1_1Raw.html b/docs/html/classbinlex_1_1Raw.html deleted file mode 100644 index 09ad4549..00000000 --- a/docs/html/classbinlex_1_1Raw.html +++ /dev/null @@ -1,254 +0,0 @@ - - - - - - - -binlex: binlex::Raw Class Reference - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
- -
-
binlex::Raw Class Reference
-
-
-
-Inheritance diagram for binlex::Raw:
-
-
- - -binlex::File -binlex::Common - -
- - - - -

-Classes

struct  Section
 
- - - - - - - - - - - - - - - - -

-Public Member Functions

int GetFileSize (FILE *fd)
 
virtual BINLEX_EXPORT bool ReadVector (const std::vector< uint8_t > &data)
 
- Public Member Functions inherited from binlex::File
void CalculateFileHashes (char *file_path)
 
void CalculateFileHashes (const vector< uint8_t > &data)
 
vector< uint8_t > ReadFileIntoVector (const char *file_path)
 
bool ReadFile (const char *file_path)
 
bool ReadBuffer (void *data, size_t size)
 
- - - - - - - - -

-Public Attributes

-struct Section sections [BINARY_MAX_SECTIONS]
 
- Public Attributes inherited from binlex::File
string sha256
 
-string tlsh
 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

-Additional Inherited Members

- Static Public Member Functions inherited from binlex::Common
static BINLEX_EXPORT string GetTLSH (const uint8_t *data, size_t len)
 
static BINLEX_EXPORT string GetFileTLSH (const char *file_path)
 
static BINLEX_EXPORT string SHA256 (char *trait)
 
static BINLEX_EXPORT string GetFileSHA256 (char *file_path)
 
static BINLEX_EXPORT string GetSHA256 (const uint8_t *data, size_t len)
 
static BINLEX_EXPORT vector< char > TraitToChar (string trait)
 
static BINLEX_EXPORT string RemoveWildcards (string trait)
 
static BINLEX_EXPORT uint GetByteSize (string s)
 
static BINLEX_EXPORT string RemoveSpaces (string s)
 
static BINLEX_EXPORT string WildcardTrait (string trait, string bytes)
 
static BINLEX_EXPORT string TrimRight (const std::string &s)
 
static BINLEX_EXPORT string HexdumpBE (const void *data, size_t size)
 
static BINLEX_EXPORT string TraitToTLSH (string bytes)
 
static BINLEX_EXPORT vector< uint8_t > TraitToData (string trait)
 
static BINLEX_EXPORT string Wildcards (uint count)
 
static BINLEX_EXPORT float Entropy (string trait)
 
static BINLEX_EXPORT void Hexdump (const char *desc, const void *addr, const int len)
 
-

Member Function Documentation

- -

◆ GetFileSize()

- -
-
- - - - - - - - -
int binlex::Raw::GetFileSize (FILE * fd)
-
-

This class is used to read raw data. Get the size of a file.

Parameters
- - -
fdfile descriptor
-
-
-
Returns
int result
- -
-
- -

◆ ReadVector()

- -
-
- - - - - -
- - - - - - - - -
virtual BINLEX_EXPORT bool binlex::Raw::ReadVector (const std::vector< uint8_t > & data)
-
-virtual
-
-

Read data.

Parameters
- - -
datapointer to uint8_t vector
-
-
-
Returns
bool
- -

Implements binlex::File.

- -
-
-
The documentation for this class was generated from the following file: -
-
- - - - diff --git a/docs/html/classbinlex_1_1Raw.js b/docs/html/classbinlex_1_1Raw.js deleted file mode 100644 index b8b31dda..00000000 --- a/docs/html/classbinlex_1_1Raw.js +++ /dev/null @@ -1,9 +0,0 @@ -var classbinlex_1_1Raw = -[ - [ "Section", "structbinlex_1_1Raw_1_1Section.html", "structbinlex_1_1Raw_1_1Section" ], - [ "Raw", "classbinlex_1_1Raw.html#aeb96c20f3441d24d2acf022e8e10c3e0", null ], - [ "~Raw", "classbinlex_1_1Raw.html#aa2470a5e5d318e357a8cd37d6edb46ae", null ], - [ "GetFileSize", "classbinlex_1_1Raw.html#a8aacf571ea478ede2feb0dbfbd3e9756", null ], - [ "ReadVector", "classbinlex_1_1Raw.html#a8c13d914c33d29f1df3d01e4cdc0273b", null ], - [ "sections", "classbinlex_1_1Raw.html#a9339c297c9c7c0c0f013e7689844cf8f", null ] -]; \ No newline at end of file diff --git a/docs/html/classes.html b/docs/html/classes.html deleted file mode 100644 index 3cf1c57e..00000000 --- a/docs/html/classes.html +++ /dev/null @@ -1,193 +0,0 @@ - - - - - - - -binlex: Class Index - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
Class Index
-
-
-
a | b | c | d | e | f | g | i | m | p | r | s | t
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  a  
-
Cor20MetadataTable (dotnet)   
  g  
-
PE (binlex)   TimedCode (binlex)   
  d  
-
  r  
-
TinyHeader (dotnet)   
Args (binlex)   GuidHeapIndex (dotnet)   Decompiler::Trait (binlex)   
AutoLex (binlex)   Decompiler (binlex)   
  i  
-
Raw (binlex)   CILDecompiler::Trait (binlex)   
  b  
-
DecompilerBase (binlex)   ResolutionScopeIndex (dotnet)   TypeDefEntry (dotnet)   
DOTNET (binlex)   CILDecompiler::Instruction (binlex)   
  s  
-
TypeDefOrRefIndex (dotnet)   
BlobHeapIndex (dotnet)   
  e  
-
  m  
-
TypeRefEntry (dotnet)   
  c  
-
Decompiler::Section (binlex)   
ELF (binlex)   Method (dotnet)   Raw::Section (binlex)   
CILDecompiler (binlex)   
  f  
-
MethodDefEntry (dotnet)   File::Section (binlex)   
Common (binlex)   MethodHeader (dotnet)   CILDecompiler::Section (binlex)   
COR20_HEADER (dotnet)   FatHeader (dotnet)   MethodPtrEntry (dotnet)   SHA256_CTX   
COR20_STORAGE_HEADER (dotnet)   FieldEntry (dotnet)   ModuleEntry (dotnet)   SimpleTableIndex (dotnet)   
COR20_STORAGE_SIGNATURE (dotnet)   FieldPtrEntry (dotnet)   MultiTableIndex (dotnet)   StringHeapIndex (dotnet)   
COR20_STREAM_HEADER (dotnet)   File (binlex)   
  p  
-
  t  
-
TableEntry::ParseArgs (dotnet)   TableEntry (dotnet)   
-
a | b | c | d | e | f | g | i | m | p | r | s | t
-
-
- - - - diff --git a/docs/html/closed.png b/docs/html/closed.png deleted file mode 100644 index 98cc2c90..00000000 Binary files a/docs/html/closed.png and /dev/null differ diff --git a/docs/html/common_8h_source.html b/docs/html/common_8h_source.html deleted file mode 100644 index b01e9fc3..00000000 --- a/docs/html/common_8h_source.html +++ /dev/null @@ -1,230 +0,0 @@ - - - - - - - -binlex: include/common.h Source File - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
common.h
-
-
-
1 #ifndef COMMON_H
-
2 #define COMMON_H
-
3 
-
4 #include <iostream>
-
5 #include <iomanip>
-
6 #include <vector>
-
7 #include <queue>
-
8 #include <set>
-
9 #include <map>
-
10 #include <math.h>
-
11 #include <stdio.h>
-
12 #include <string.h>
-
13 #include <stdlib.h>
-
14 #include <errno.h>
-
15 #include <sstream>
-
16 #include <algorithm>
-
17 #include <vector>
-
18 #include <iomanip>
-
19 #include <capstone/capstone.h>
-
20 #include <tlsh.h>
-
21 #include <stdexcept>
-
22 #include <chrono>
-
23 #include "args.h"
-
24 
-
25 extern "C" {
-
26  #include "sha256.h"
-
27 }
-
28 
-
29 #ifdef _WIN32
-
30 #define BINLEX_EXPORT __declspec(dllexport)
-
31 #else
-
32 #define BINLEX_EXPORT
-
33 #endif
-
34 
-
35 #ifdef _WIN32
-
36 #pragma comment(lib, "capstone")
-
37 #pragma comment(lib, "LIEF")
-
38 #endif
-
39 
-
40 using std::set;
-
41 using std::map;
-
42 using std::queue;
-
43 using std::set;
-
44 using std::vector;
-
45 using std::ofstream;
-
46 using std::stringstream;
-
47 using std::string;
-
48 using std::cin;
-
49 using std::cout;
-
50 using std::cerr;
-
51 using std::endl;
-
52 using std::hex;
-
53 using std::setfill;
-
54 using std::setw;
-
55 
-
56 #define BINARY_MAX_SECTIONS 256
-
57 
-
58 #ifdef _WIN32
-
59 typedef unsigned int uint;
-
60 typedef uint useconds_t;
-
61 #endif
-
62 
-
63 extern binlex::Args g_args;
-
64 
-
65 // Debug
-
66 #define PRINT_DEBUG(...) {if (g_args.options.debug) fprintf(stderr, __VA_ARGS__); }
-
67 #define PRINT_ERROR_AND_EXIT(...) { fprintf(stderr, __VA_ARGS__); exit(EXIT_FAILURE); }
-
68 void print_data(string title, void *data, uint32_t size);
-
69 #define PRINT_DATA(title, data, size) { print_data(title, data, size); }
-
70 
-
71 #define PERF_START(tag) { binlex::TimedCode *tc = new binlex::TimedCode(tag);
-
72 #define PERF_END tc->Print(); }
-
73 
-
74 namespace binlex {
-
75  class Common{
-
79  public:
-
86  BINLEX_EXPORT static string GetTLSH(const uint8_t *data, size_t len);
-
93  BINLEX_EXPORT static string GetFileTLSH(const char *file_path);
-
99  BINLEX_EXPORT static string SHA256(char *trait);
-
106  BINLEX_EXPORT static string GetFileSHA256(char *file_path);
-
114  BINLEX_EXPORT static string GetSHA256(const uint8_t *data, size_t len);
-
120  BINLEX_EXPORT static vector<char> TraitToChar(string trait);
-
126  BINLEX_EXPORT static string RemoveWildcards(string trait);
-
132  BINLEX_EXPORT static uint GetByteSize(string s);
-
138  BINLEX_EXPORT static string RemoveSpaces(string s);
-
145  BINLEX_EXPORT static string WildcardTrait(string trait, string bytes);
-
151  BINLEX_EXPORT static string TrimRight(const std::string &s);
-
158  BINLEX_EXPORT static string HexdumpBE(const void *data, size_t size);
-
164  BINLEX_EXPORT static string TraitToTLSH(string bytes);
-
170  BINLEX_EXPORT static vector<uint8_t> TraitToData(string trait);
-
176  BINLEX_EXPORT static string Wildcards(uint count);
-
182  BINLEX_EXPORT static float Entropy(string trait);
-
189  BINLEX_EXPORT static void Hexdump(const char * desc, const void * addr, const int len);
-
190  };
-
191 
-
192 
-
193  class TimedCode {
-
197  private:
-
198  std::chrono::steady_clock::time_point start;
-
199  const char *print_tag;
-
200  public:
-
201  TimedCode(const char *tag);
-
202  void Print();
-
203  ~TimedCode() {};
-
204  };
-
205 }
-
206 
-
207 #endif
-
-
-
static BINLEX_EXPORT string GetFileTLSH(const char *file_path)
-
static BINLEX_EXPORT string RemoveSpaces(string s)
-
Definition: common.h:193
-
static BINLEX_EXPORT string SHA256(char *trait)
-
static BINLEX_EXPORT string GetFileSHA256(char *file_path)
-
static BINLEX_EXPORT void Hexdump(const char *desc, const void *addr, const int len)
-
static BINLEX_EXPORT vector< uint8_t > TraitToData(string trait)
-
Definition: common.h:75
-
static BINLEX_EXPORT string RemoveWildcards(string trait)
-
static BINLEX_EXPORT string GetTLSH(const uint8_t *data, size_t len)
-
static BINLEX_EXPORT string TrimRight(const std::string &s)
-
static BINLEX_EXPORT string WildcardTrait(string trait, string bytes)
-
static BINLEX_EXPORT uint GetByteSize(string s)
-
Definition: args.h:41
-
static BINLEX_EXPORT float Entropy(string trait)
-
static BINLEX_EXPORT vector< char > TraitToChar(string trait)
-
static BINLEX_EXPORT string TraitToTLSH(string bytes)
-
static BINLEX_EXPORT string Wildcards(uint count)
-
static BINLEX_EXPORT string HexdumpBE(const void *data, size_t size)
-
static BINLEX_EXPORT string GetSHA256(const uint8_t *data, size_t len)
-
the binlex namespace
- - - - diff --git a/docs/html/decompiler_8h_source.html b/docs/html/decompiler_8h_source.html deleted file mode 100644 index 88de2b8d..00000000 --- a/docs/html/decompiler_8h_source.html +++ /dev/null @@ -1,269 +0,0 @@ - - - - - - - -binlex: include/decompiler.h Source File - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
decompiler.h
-
-
-
1 #ifndef DECOMPILER_H
-
2 #define DECOMPILER_H
-
3 
-
4 #include <stdint.h>
-
5 #include <stdio.h>
-
6 #include <string.h>
-
7 #include <stdlib.h>
-
8 #include <errno.h>
-
9 #include <fstream>
-
10 #include <iostream>
-
11 #include <algorithm>
-
12 #include <vector>
-
13 #include <iomanip>
-
14 #include <math.h>
-
15 #include <capstone/capstone.h>
-
16 #include "common.h"
-
17 #include "json.h"
-
18 #include "decompilerbase.h"
-
19 
-
20 #if defined(__linux__) || defined(__APPLE__)
-
21 #include <pthread.h>
-
22 #include <unistd.h>
-
23 #elif _WIN32
-
24 #include <windows.h>
-
25 #include <wincrypt.h>
-
26 #endif
-
27 
-
28 #ifdef _WIN32
-
29 #define BINLEX_EXPORT __declspec(dllexport)
-
30 #else
-
31 #define BINLEX_EXPORT
-
32 #endif
-
33 
-
34 #define DECOMPILER_MAX_SECTIONS 256
-
35 #define SHA256_PRINTABLE_SIZE 65 /* including NULL terminator */
-
36 
-
37 #define DECOMPILER_VISITED_QUEUED 0
-
38 #define DECOMPILER_VISITED_ANALYZED 1
-
39 
-
40 #define DECOMPILER_GPU_MODE_CUDA 0
-
41 #define DECOMPILER_GPU_MODE_OPENCL 1
-
42 
-
43 typedef enum DECOMPILER_OPERAND_TYPE {
-
44  DECOMPILER_OPERAND_TYPE_BLOCK = 0,
-
45  DECOMPILER_OPERAND_TYPE_FUNCTION = 1,
-
46  DECOMPILER_OPERAND_TYPE_UNSET = 2
-
47 } DECOMPILER_OPERAND_TYPE;
-
48 
-
49 using json = nlohmann::json;
-
50 
-
51 namespace binlex {
-
52  class Decompiler : public DecompilerBase {
-
53  private:
-
54  typedef struct worker {
-
55  csh handle;
-
56  cs_err error;
-
57  uint64_t pc;
-
58  const uint8_t *code;
-
59  size_t code_size;
-
60  } worker;
-
61  typedef struct{
-
62  uint index;
-
63  cs_arch arch;
-
64  cs_mode mode;
-
65  void *sections;
-
66  } worker_args;
-
67  public:
-
68  struct Trait {
-
69  string type;
-
70  string tmp_bytes;
-
71  string bytes;
-
72  string tmp_trait;
-
73  string trait;
-
74  uint edges;
-
75  uint blocks;
-
76  uint instructions;
-
77  uint size;
-
78  uint offset;
-
79  uint invalid_instructions;
-
80  uint cyclomatic_complexity;
-
81  uint average_instructions_per_block;
-
82  float bytes_entropy;
-
83  float trait_entropy;
-
84  char bytes_sha256[SHA256_PRINTABLE_SIZE];
-
85  char trait_sha256[SHA256_PRINTABLE_SIZE];
-
86  };
-
87  struct Section {
-
88  char *cpu;
-
89  bool instructions;
-
90  uint offset;
-
91  vector<struct Trait> traits;
-
92  void *data;
-
93  size_t data_size;
-
94  set<uint64_t> coverage;
-
95  map<uint64_t, uint> addresses;
-
96  map<uint64_t, int> visited;
-
97  queue<uint64_t> discovered;
-
98  };
-
99  static cs_arch arch;
-
100  static cs_mode mode;
-
101  struct Section sections[DECOMPILER_MAX_SECTIONS];
-
102  BINLEX_EXPORT Decompiler(const binlex::File &firef);
-
109  BINLEX_EXPORT bool Setup(cs_arch architecture, cs_mode mode_type);
-
114  BINLEX_EXPORT void SetInstructions(bool instructions, uint index);
-
120  BINLEX_EXPORT void* CreateTraitsForSection(uint index);
-
128  BINLEX_EXPORT static void AddDiscoveredBlock(uint64_t address, struct Section *sections, uint index);
-
136  BINLEX_EXPORT static void CollectOperands(cs_insn* insn, int operand_type, struct Section *sections, uint index);
-
143  BINLEX_EXPORT static uint CollectInsn(cs_insn* insn, struct Section *sections, uint index);
-
151  BINLEX_EXPORT void LinearDisassemble(void* data, size_t data_size, size_t offset, uint index);
-
159  BINLEX_EXPORT void Decompile(void* data, size_t data_size, size_t offset, uint index);
-
160  //void Seek(uint64_t address, size_t data_size, uint index);
-
168  BINLEX_EXPORT static void AppendTrait(struct Trait *trait, struct Section *sections, uint index);
-
169  BINLEX_EXPORT void FreeTraits(uint index);
-
175  BINLEX_EXPORT static bool IsNopInsn(cs_insn *ins);
-
181  BINLEX_EXPORT static bool IsSemanticNopInsn(cs_insn *ins);
-
187  BINLEX_EXPORT static bool IsTrapInsn(cs_insn *ins);
-
193  BINLEX_EXPORT static bool IsPrivInsn(cs_insn *ins);
-
199  BINLEX_EXPORT static bool IsEndInsn(cs_insn *insn);
-
205  BINLEX_EXPORT static uint IsConditionalInsn(cs_insn *insn);
-
211  BINLEX_EXPORT static uint64_t MaxAddress(set<uint64_t> coverage);
-
217  BINLEX_EXPORT static bool IsFunction(map<uint64_t, uint> &addresses, uint64_t address);
-
223  BINLEX_EXPORT static bool IsBlock(map<uint64_t, uint> &addresses, uint64_t address);
-
229  BINLEX_EXPORT static bool IsVisited(map<uint64_t, int> &visited, uint64_t address);
-
235  BINLEX_EXPORT bool IsAddress(map<uint64_t, uint> &addresses, uint64_t address);
-
241  BINLEX_EXPORT static bool IsWildcardInsn(cs_insn *insn);
-
247  BINLEX_EXPORT static string WildcardInsn(cs_insn *insn);
-
252  BINLEX_EXPORT static void ClearTrait(struct Trait *trait);
-
258  BINLEX_EXPORT json GetTrait(struct Trait &trait);
-
263  vector<json> GetTraits();
-
264  BINLEX_EXPORT static void * TraitWorker(void *args);
-
265  BINLEX_EXPORT static void * FinalizeTrait(struct Trait &trait);
-
266  BINLEX_EXPORT void AppendQueue(set<uint64_t> &addresses, uint operand_type, uint index);
-
267  //void Seek(uint offset, uint index);
-
268  BINLEX_EXPORT ~Decompiler();
-
269  };
-
270 }
-
271 #endif
-
-
-
static BINLEX_EXPORT uint CollectInsn(cs_insn *insn, struct Section *sections, uint index)
-
BINLEX_EXPORT bool Setup(cs_arch architecture, cs_mode mode_type)
-
static BINLEX_EXPORT bool IsBlock(map< uint64_t, uint > &addresses, uint64_t address)
-
static BINLEX_EXPORT bool IsPrivInsn(cs_insn *ins)
-
Definition: decompiler.h:87
-
static BINLEX_EXPORT bool IsEndInsn(cs_insn *insn)
-
static BINLEX_EXPORT bool IsWildcardInsn(cs_insn *insn)
-
static BINLEX_EXPORT uint64_t MaxAddress(set< uint64_t > coverage)
-
BINLEX_EXPORT void Decompile(void *data, size_t data_size, size_t offset, uint index)
-
static BINLEX_EXPORT void AddDiscoveredBlock(uint64_t address, struct Section *sections, uint index)
-
static BINLEX_EXPORT void CollectOperands(cs_insn *insn, int operand_type, struct Section *sections, uint index)
-
Definition: decompiler.h:52
-
Definition: decompiler.h:68
-
vector< json > GetTraits()
-
BINLEX_EXPORT bool IsAddress(map< uint64_t, uint > &addresses, uint64_t address)
-
BINLEX_EXPORT void SetInstructions(bool instructions, uint index)
-
static BINLEX_EXPORT bool IsSemanticNopInsn(cs_insn *ins)
-
static BINLEX_EXPORT uint IsConditionalInsn(cs_insn *insn)
-
Definition: decompilerbase.h:14
-
static BINLEX_EXPORT bool IsFunction(map< uint64_t, uint > &addresses, uint64_t address)
-
BINLEX_EXPORT void * CreateTraitsForSection(uint index)
-
BINLEX_EXPORT json GetTrait(struct Trait &trait)
-
Definition: file.h:14
-
static BINLEX_EXPORT bool IsTrapInsn(cs_insn *ins)
-
BINLEX_EXPORT void LinearDisassemble(void *data, size_t data_size, size_t offset, uint index)
-
static BINLEX_EXPORT bool IsVisited(map< uint64_t, int > &visited, uint64_t address)
-
static BINLEX_EXPORT bool IsNopInsn(cs_insn *ins)
-
static BINLEX_EXPORT void ClearTrait(struct Trait *trait)
-
the binlex namespace
-
static BINLEX_EXPORT string WildcardInsn(cs_insn *insn)
-
static BINLEX_EXPORT void AppendTrait(struct Trait *trait, struct Section *sections, uint index)
- - - - diff --git a/docs/html/dir_d44c64559bbebec7f509842c48db8b23.html b/docs/html/dir_d44c64559bbebec7f509842c48db8b23.html deleted file mode 100644 index 1d3b26aa..00000000 --- a/docs/html/dir_d44c64559bbebec7f509842c48db8b23.html +++ /dev/null @@ -1,101 +0,0 @@ - - - - - - - -binlex: include Directory Reference - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
include Directory Reference
-
-
-
-
- - - - diff --git a/docs/html/dir_d44c64559bbebec7f509842c48db8b23.js b/docs/html/dir_d44c64559bbebec7f509842c48db8b23.js deleted file mode 100644 index b4e88acd..00000000 --- a/docs/html/dir_d44c64559bbebec7f509842c48db8b23.js +++ /dev/null @@ -1,16 +0,0 @@ -var dir_d44c64559bbebec7f509842c48db8b23 = -[ - [ "args.h", "args_8h_source.html", null ], - [ "auto.h", "auto_8h_source.html", null ], - [ "blelf.h", "blelf_8h_source.html", null ], - [ "cil.h", "cil_8h_source.html", null ], - [ "common.h", "common_8h_source.html", null ], - [ "decompiler.h", "decompiler_8h_source.html", null ], - [ "decompilerbase.h", "decompilerbase_8h_source.html", null ], - [ "file.h", "file_8h_source.html", null ], - [ "jvm.h", "jvm_8h_source.html", null ], - [ "pe-dotnet.h", "pe-dotnet_8h_source.html", null ], - [ "pe.h", "pe_8h_source.html", null ], - [ "raw.h", "raw_8h_source.html", null ], - [ "sha256.h", "sha256_8h_source.html", null ] -]; \ No newline at end of file diff --git a/docs/html/doc.png b/docs/html/doc.png deleted file mode 100644 index 17edabff..00000000 Binary files a/docs/html/doc.png and /dev/null differ diff --git a/docs/html/docs/img/demo_0.gif b/docs/html/docs/img/demo_0.gif deleted file mode 100644 index a8553c4c..00000000 Binary files a/docs/html/docs/img/demo_0.gif and /dev/null differ diff --git a/docs/html/doxygen-awesome.css b/docs/html/doxygen-awesome.css deleted file mode 100644 index 83929e2e..00000000 --- a/docs/html/doxygen-awesome.css +++ /dev/null @@ -1,1505 +0,0 @@ -/** - -Doxygen Awesome -https://github.com/jothepro/doxygen-awesome-css - -MIT License - -Copyright (c) 2021 jothepro - -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. - -*/ - -html { - /* primary theme color. This will affect the entire websites color scheme: links, arrows, labels, ... */ - --primary-color: #1779c4; - --primary-dark-color: #00559f; - --primary-light-color: #7aabd6; - --primary-lighter-color: #cae1f1; - --primary-lightest-color: #e9f1f8; - - /* page base colors */ - --page-background-color: white; - --page-foreground-color: #2c3e50; - --page-secondary-foreground-color: #67727e; - - /* color for all separators on the website: hr, borders, ... */ - --separator-color: #dedede; - - /* border radius for all rounded components. Will affect many components, like dropdowns, memitems, codeblocks, ... */ - --border-radius-large: 8px; - --border-radius-small: 4px; - --border-radius-medium: 6px; - - /* default spacings. Most compontest reference these values for spacing, to provide uniform spacing on the page. */ - --spacing-small: 5px; - --spacing-medium: 10px; - --spacing-large: 16px; - - /* default box shadow used for raising an element above the normal content. Used in dropdowns, Searchresult, ... */ - --box-shadow: 0 2px 10px 0 rgba(0,0,0,.1); - - --odd-color: rgba(0,0,0,.03); - - /* font-families. will affect all text on the website - * font-family: the normal font for text, headlines, menus - * font-family-monospace: used for preformatted text in memtitle, code, fragments - */ - --font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif; - --font-family-monospace: source-code-pro,Menlo,Monaco,Consolas,Courier New,monospace; - - /* font sizes */ - --page-font-size: 15.6px; - --navigation-font-size: 14.4px; - --code-font-size: 14.4px; /* affects code, fragment */ - --title-font-size: 22px; - - /* content text properties. These only affect the page content, not the navigation or any other ui elements */ - --content-line-height: 27px; - /* The content is centered and constraint in it's width. To make the content fill the whole page, set the variable to auto.*/ - --content-maxwidth: 1000px; - - /* colors for various content boxes: @warning, @note, @deprecated @bug */ - --warning-color: #fca49b; - --warning-color-dark: #b61825; - --warning-color-darker: #75070f; - --note-color: rgba(255,229,100,.3); - --note-color-dark: #c39900; - --note-color-darker: #8d7400; - --deprecated-color: rgb(214, 216, 224); - --deprecated-color-dark: #5b6269; - --deprecated-color-darker: #43454a; - --bug-color: rgb(246, 208, 178); - --bug-color-dark: #a53a00; - --bug-color-darker: #5b1d00; - --invariant-color: #b7f8d0; - --invariant-color-dark: #00ba44; - --invariant-color-darker: #008622; - - /* blockquote colors */ - --blockquote-background: #f5f5f5; - --blockquote-foreground: #727272; - - /* table colors */ - --tablehead-background: #f1f1f1; - --tablehead-foreground: var(--page-foreground-color); - - /* menu-display: block | none - * Visibility of the top navigation on screens >= 768px. On smaller screen the menu is always visible. - * `GENERATE_TREEVIEW` MUST be enabled! - */ - --menu-display: block; - - --menu-focus-foreground: var(--page-background-color); - --menu-focus-background: var(--primary-color); - --menu-selected-background: rgba(0,0,0,.05); - - - --header-background: var(--page-background-color); - --header-foreground: var(--page-foreground-color); - - /* searchbar colors */ - --searchbar-background: var(--side-nav-background); - --searchbar-foreground: var(--page-foreground-color); - - /* searchbar size - * (`searchbar-width` is only applied on screens >= 768px. - * on smaller screens the searchbar will always fill the entire screen width) */ - --searchbar-height: 33px; - --searchbar-width: 210px; - - /* code block colors */ - --code-background: #f5f5f5; - --code-foreground: var(--page-foreground-color); - - /* fragment colors */ - --fragment-background: #282c34; - --fragment-foreground: #ffffff; - --fragment-keyword: #cc99cd; - --fragment-keywordtype: #ab99cd; - --fragment-keywordflow: #e08000; - --fragment-token: #7ec699; - --fragment-comment: #999999; - --fragment-link: #98c0e3; - --fragment-preprocessor: #65cabe; - --fragment-linenumber-color: #cccccc; - --fragment-linenumber-background: #35393c; - --fragment-linenumber-border: #1f1f1f; - --fragment-lineheight: 20px; - - /* sidebar navigation (treeview) colors */ - --side-nav-background: #fbfbfb; - --side-nav-foreground: var(--page-foreground-color); - --side-nav-arrow-opacity: 0; - --side-nav-arrow-hover-opacity: 0.9; - - /* height of an item in any tree / collapsable table */ - --tree-item-height: 30px; - - --darkmode-toggle-button-icon: 'β˜€οΈ' -} - -@media screen and (max-width: 767px) { - html { - --page-font-size: 16px; - --navigation-font-size: 16px; - --code-font-size: 15px; /* affects code, fragment */ - --title-font-size: 22px; - } -} - -@media (prefers-color-scheme: dark) { - html:not(.light-mode) { - color-scheme: dark; - - --primary-color: #1982d2; - --primary-dark-color: #5ca8e2; - --primary-light-color: #4779ac; - --primary-lighter-color: #191e21; - --primary-lightest-color: #191a1c; - - --box-shadow: 0 2px 10px 0 rgba(0,0,0,.35); - - --odd-color: rgba(0,0,0,.1); - - --menu-selected-background: rgba(0,0,0,.4); - - --page-background-color: #1C1D1F; - --page-foreground-color: #d2dbde; - --page-secondary-foreground-color: #859399; - --separator-color: #000000; - --side-nav-background: #252628; - - --code-background: #2a2c2f; - - --tablehead-background: #2a2c2f; - - --blockquote-background: #1f2022; - --blockquote-foreground: #77848a; - - --warning-color: #b61825; - --warning-color-dark: #510a02; - --warning-color-darker: #f5b1aa; - --note-color: rgb(255, 183, 0); - --note-color-dark: #9f7300; - --note-color-darker: #645b39; - --deprecated-color: rgb(88, 90, 96); - --deprecated-color-dark: #262e37; - --deprecated-color-darker: #a0a5b0; - --bug-color: rgb(248, 113, 0); - --bug-color-dark: #812a00; - --bug-color-darker: #ffd3be; - - --darkmode-toggle-button-icon: 'πŸŒ›'; - } -} - -/* dark mode variables are defined twice, to support both the dark-mode without and with doxygen-awesome-darkmode-toggle.js */ -html.dark-mode { - color-scheme: dark; - - --primary-color: #1982d2; - --primary-dark-color: #5ca8e2; - --primary-light-color: #4779ac; - --primary-lighter-color: #191e21; - --primary-lightest-color: #191a1c; - - --box-shadow: 0 2px 10px 0 rgba(0,0,0,.35); - - --odd-color: rgba(0,0,0,.1); - - --menu-selected-background: rgba(0,0,0,.4); - - --page-background-color: #1C1D1F; - --page-foreground-color: #d2dbde; - --page-secondary-foreground-color: #859399; - --separator-color: #000000; - --side-nav-background: #252628; - - --code-background: #2a2c2f; - - --tablehead-background: #2a2c2f; - - --blockquote-background: #1f2022; - --blockquote-foreground: #77848a; - - --warning-color: #b61825; - --warning-color-dark: #510a02; - --warning-color-darker: #f5b1aa; - --note-color: rgb(255, 183, 0); - --note-color-dark: #9f7300; - --note-color-darker: #645b39; - --deprecated-color: rgb(88, 90, 96); - --deprecated-color-dark: #262e37; - --deprecated-color-darker: #a0a5b0; - --bug-color: rgb(248, 113, 0); - --bug-color-dark: #812a00; - --bug-color-darker: #ffd3be; - - --darkmode-toggle-button-icon: 'πŸŒ›'; -} - -body { - color: var(--page-foreground-color); - background-color: var(--page-background-color); - font-size: var(--page-font-size); -} - -body, table, div, p, dl, #nav-tree .label, .title, .sm-dox a, .sm-dox a:hover, .sm-dox a:focus, #projectname, .SelectItem, #MSearchField, .navpath li.navelem a, .navpath li.navelem a:hover { - font-family: var(--font-family); -} - -h1, h2, h3, h4, h5 { - margin-top: .9em; - font-weight: 600; - line-height: initial; -} - -p, div, table, dl { - font-size: var(--page-font-size); -} - -a:link, a:visited, a:hover, a:focus, a:active { - color: var(--primary-color) !important; - font-weight: 500; -} - -/* - Title and top navigation - */ - -#top { - background: var(--header-background); - border-bottom: 1px solid var(--separator-color); -} - -@media screen and (min-width: 768px) { - #top { - display: flex; - flex-wrap: wrap; - justify-content: space-between; - align-items: center; - } -} - -#main-nav { - flex-grow: 5; - padding: var(--spacing-small) var(--spacing-medium); -} - -#titlearea { - width: auto; - padding: var(--spacing-medium) var(--spacing-large); - background: none; - color: var(--header-foreground); - border-bottom: none; -} - -@media screen and (max-width: 767px) { - #titlearea { - padding-bottom: var(--spacing-small); - } -} - -#titlearea table tbody tr { - height: auto !important; -} - -#projectname { - font-size: var(--title-font-size); - font-weight: 600; -} - -#projectnumber { - font-family: inherit; - font-size: 60%; -} - -#projectbrief { - font-family: inherit; - font-size: 80%; -} - -#projectlogo { - vertical-align: middle; -} - -#projectlogo img { - max-height: calc(var(--title-font-size) * 2); - margin-right: var(--spacing-small); -} - -.sm-dox, .tabs, .tabs2, .tabs3 { - background: none; - padding: 0; -} - -.tabs, .tabs2, .tabs3 { - border-bottom: 1px solid var(--separator-color); - margin-bottom: -1px; -} - -@media screen and (max-width: 767px) { - .sm-dox a span.sub-arrow { - background: var(--code-background); - } -} - -@media screen and (min-width: 768px) { - .sm-dox li, .tablist li { - display: var(--menu-display); - } - - .sm-dox a span.sub-arrow { - border-color: var(--header-foreground) transparent transparent transparent; - } - - .sm-dox a:hover span.sub-arrow { - border-color: var(--menu-focus-foreground) transparent transparent transparent; - } - - .sm-dox ul a span.sub-arrow { - border-color: transparent transparent transparent var(--page-foreground-color); - } - - .sm-dox ul a:hover span.sub-arrow { - border-color: transparent transparent transparent var(--menu-focus-foreground); - } -} - -.sm-dox ul { - background: var(--page-background-color); - box-shadow: var(--box-shadow); - border: 1px solid var(--separator-color); - border-radius: var(--border-radius-medium) !important; - padding: var(--spacing-small); - animation: ease-out 150ms slideInMenu; -} - -@keyframes slideInMenu { - from { - opacity: 0; - transform: translate(0px, -2px); - } - - to { - opacity: 1; - transform: translate(0px, 0px); - } -} - -.sm-dox ul a { - color: var(--page-foreground-color) !important; - background: var(--page-background-color); - font-size: var(--navigation-font-size); -} - -.sm-dox>li>ul:after { - border-bottom-color: var(--page-background-color) !important; -} - -.sm-dox>li>ul:before { - border-bottom-color: var(--separator-color) !important; -} - -.sm-dox ul a:hover, .sm-dox ul a:active, .sm-dox ul a:focus { - font-size: var(--navigation-font-size) !important; - color: var(--menu-focus-foreground) !important; - text-shadow: none; - background-color: var(--menu-focus-background); - border-radius: var(--border-radius-small) !important; -} - -.sm-dox a, .sm-dox a:focus, .tablist li, .tablist li a, .tablist li.current a { - text-shadow: none; - background: transparent; - background-image: none !important; - color: var(--header-foreground) !important; - font-weight: normal; - font-size: var(--navigation-font-size); -} - -.sm-dox a:focus { - outline: auto; -} - -.sm-dox a:hover, .sm-dox a:active, .tablist li a:hover { - text-shadow: none; - font-weight: normal; - background: var(--menu-focus-background); - color: var(--menu-focus-foreground) !important; - border-radius: var(--border-radius-small) !important; - font-size: var(--navigation-font-size); -} - -.tablist li.current { - border-radius: var(--border-radius-small); - background: var(--menu-selected-background); -} - -.tablist li { - margin: var(--spacing-small) 0 var(--spacing-small) var(--spacing-small); -} - -.tablist a { - padding: 0 var(--spacing-large); -} - - -/* - Search box - */ - -#MSearchBox { - height: var(--searchbar-height); - background: var(--searchbar-background); - border-radius: var(--searchbar-height); - border: 1px solid var(--separator-color); - overflow: hidden; - width: var(--searchbar-width); - position: relative; - box-shadow: none; - display: block; - margin-top: 0; -} - -.left #MSearchSelect { - left: 0; -} - -.tabs .left #MSearchSelect { - padding-left: 0; -} - -.tabs #MSearchBox { - position: absolute; - right: var(--spacing-medium); -} - -@media screen and (max-width: 767px) { - .tabs #MSearchBox { - position: relative; - right: 0; - margin-left: var(--spacing-medium); - margin-top: 0; - } -} - -#MSearchSelectWindow, #MSearchResultsWindow { - z-index: 9999; -} - -#MSearchBox.MSearchBoxActive { - border-color: var(--primary-color); - box-shadow: inset 0 0 0 1px var(--primary-color); -} - -#main-menu > li:last-child { - margin-right: 0; -} - -@media screen and (max-width: 767px) { - #main-menu > li:last-child { - height: 50px; - } -} - -#MSearchField { - font-size: var(--navigation-font-size); - height: calc(var(--searchbar-height) - 2px); - background: transparent; - width: calc(var(--searchbar-width) - 64px); -} - -.MSearchBoxActive #MSearchField { - color: var(--searchbar-foreground); -} - -#MSearchSelect { - top: calc(calc(var(--searchbar-height) / 2) - 11px); -} - -.left #MSearchSelect { - padding-left: 8px; -} - -#MSearchBox span.left, #MSearchBox span.right { - background: none; -} - -#MSearchBox span.right { - padding-top: calc(calc(var(--searchbar-height) / 2) - 12px); - position: absolute; - right: var(--spacing-small); -} - -.tabs #MSearchBox span.right { - top: calc(calc(var(--searchbar-height) / 2) - 12px); -} - -@keyframes slideInSearchResults { - from { - opacity: 0; - transform: translate(0, 15px); - } - - to { - opacity: 1; - transform: translate(0, 20px); - } -} - -#MSearchResultsWindow { - left: auto !important; - right: var(--spacing-medium); - border-radius: var(--border-radius-large); - border: 1px solid var(--separator-color); - transform: translate(0, 20px); - box-shadow: var(--box-shadow); - animation: ease-out 280ms slideInSearchResults; - background: var(--page-background-color); -} - -iframe#MSearchResults { - margin: 4px; -} - -iframe { - color-scheme: normal; -} - -@media (prefers-color-scheme: dark) { - html:not(.light-mode) iframe#MSearchResults { - filter: invert() hue-rotate(180deg); - } -} - -html.dark-mode iframe#MSearchResults { - filter: invert() hue-rotate(180deg); -} - -#MSearchSelectWindow { - border: 1px solid var(--separator-color); - border-radius: var(--border-radius-medium); - box-shadow: var(--box-shadow); - background: var(--page-background-color); -} - -#MSearchSelectWindow a.SelectItem { - font-size: var(--navigation-font-size); - line-height: var(--content-line-height); - margin: 0 var(--spacing-small); - border-radius: var(--border-radius-small); - color: var(--page-foreground-color) !important; - font-weight: normal; -} - -#MSearchSelectWindow a.SelectItem:hover { - background: var(--menu-focus-background); - color: var(--menu-focus-foreground) !important; -} - -@media screen and (max-width: 767px) { - #MSearchBox { - margin-top: var(--spacing-medium); - margin-bottom: var(--spacing-medium); - width: calc(100vw - 30px); - } - - #main-menu > li:last-child { - float: none !important; - } - - #MSearchField { - width: calc(100vw - 110px); - } - - @keyframes slideInSearchResultsMobile { - from { - opacity: 0; - transform: translate(0, 15px); - } - - to { - opacity: 1; - transform: translate(0, 20px); - } - } - - #MSearchResultsWindow { - left: var(--spacing-medium) !important; - right: var(--spacing-medium); - overflow: auto; - transform: translate(0, 20px); - animation: ease-out 280ms slideInSearchResultsMobile; - } - - /* - * Overwrites for fixing the searchbox on mobile in doxygen 1.9.2 - */ - label.main-menu-btn ~ #searchBoxPos1 { - top: 3px !important; - right: 6px !important; - left: 45px; - display: flex; - } - - label.main-menu-btn ~ #searchBoxPos1 > #MSearchBox { - margin-top: 0; - margin-bottom: 0; - flex-grow: 2; - float: left; - } -} - -/* - Tree view - */ - -#side-nav { - padding: 0 !important; - background: var(--side-nav-background); -} - -@media screen and (max-width: 767px) { - #side-nav { - display: none; - } - - #doc-content { - margin-left: 0 !important; - height: auto !important; - padding-bottom: calc(2 * var(--spacing-large)); - } -} - -#nav-tree { - background: transparent; -} - -#nav-tree .label { - font-size: var(--navigation-font-size); -} - -#nav-tree .item { - height: var(--tree-item-height); - line-height: var(--tree-item-height); -} - -#nav-sync { - top: 12px !important; - right: 12px; -} - -#nav-tree .selected { - text-shadow: none; - background-image: none; - background-color: transparent; - box-shadow: inset 4px 0 0 0 var(--primary-color); -} - -#nav-tree a { - color: var(--side-nav-foreground) !important; - font-weight: normal; -} - -#nav-tree a:focus { - outline-style: auto; -} - -#nav-tree .arrow { - opacity: var(--side-nav-arrow-opacity); -} - -.arrow { - color: inherit; - cursor: pointer; - font-size: 45%; - vertical-align: middle; - margin-right: 2px; - font-family: serif; - height: auto; - text-align: right; -} - -#nav-tree div.item:hover .arrow, #nav-tree a:focus .arrow { - opacity: var(--side-nav-arrow-hover-opacity); -} - -#nav-tree .selected a { - color: var(--primary-color) !important; - font-weight: bolder; - font-weight: 600; -} - -.ui-resizable-e { - background: var(--separator-color); - width: 1px; -} - -/* - Contents - */ - -div.header { - border-bottom: 1px solid var(--separator-color); - background-color: var(--page-background-color); - background-image: none; -} - -div.contents, div.header .title, div.header .summary { - max-width: var(--content-maxwidth); -} - -div.contents, div.header .title { - line-height: initial; - margin: calc(var(--spacing-medium) + .2em) auto var(--spacing-medium) auto; -} - -div.header .summary { - margin: var(--spacing-medium) auto 0 auto; -} - -div.headertitle { - padding: 0; -} - -div.header .title { - font-weight: 600; - font-size: 210%; - padding: var(--spacing-medium) var(--spacing-large); - word-break: break-word; -} - -div.header .summary { - width: auto; - display: block; - float: none; - padding: 0 var(--spacing-large); -} - -td.memSeparator { - border-color: var(--separator-color); -} - -.mdescLeft, .mdescRight, .memItemLeft, .memItemRight, .memTemplItemLeft, .memTemplItemRight, .memTemplParams { - background: var(--code-background); -} - -.mdescRight { - color: var(--page-secondary-foreground-color); -} - -span.mlabel { - background: var(--primary-color); - border: none; - padding: 4px 9px; - border-radius: 12px; - margin-right: var(--spacing-medium); -} - -span.mlabel:last-of-type { - margin-right: 2px; -} - -div.contents { - padding: 0 var(--spacing-large); -} - -div.contents p, div.contents li { - line-height: var(--content-line-height); -} - -div.contents div.dyncontent { - margin: var(--spacing-medium) 0; -} - -@media (prefers-color-scheme: dark) { - html:not(.light-mode) div.contents div.dyncontent img, - html:not(.light-mode) div.contents center img, - html:not(.light-mode) div.contents table img, - html:not(.light-mode) div.contents div.dyncontent iframe, - html:not(.light-mode) div.contents center iframe, - html:not(.light-mode) div.contents table iframe { - filter: hue-rotate(180deg) invert(); - } -} - -html.dark-mode div.contents div.dyncontent img, -html.dark-mode div.contents center img, -html.dark-mode div.contents table img, -html.dark-mode div.contents div.dyncontent iframe, -html.dark-mode div.contents center iframe, -html.dark-mode div.contents table iframe { - filter: hue-rotate(180deg) invert(); -} - -h2.groupheader { - border-bottom: 1px solid var(--separator-color); - color: var(--page-foreground-color); -} - -blockquote { - padding: var(--spacing-small) var(--spacing-medium); - background: var(--blockquote-background); - color: var(--blockquote-foreground); - border-left: 2px solid var(--blockquote-foreground); - margin: 0; -} - -blockquote p { - margin: var(--spacing-small) 0 var(--spacing-medium) 0; -} -.paramname { - font-weight: 600; - color: var(--primary-dark-color); -} - -.glow { - text-shadow: 0 0 15px var(--primary-light-color) !important; -} - -.alphachar a { - color: var(--page-foreground-color); -} - -/* - Table of Contents - */ - -div.toc { - background-color: var(--side-nav-background); - border: 1px solid var(--separator-color); - border-radius: var(--border-radius-medium); - box-shadow: var(--box-shadow); - padding: 0 var(--spacing-large); - margin: 0 0 var(--spacing-medium) var(--spacing-medium); -} - -div.toc h3 { - color: var(--side-nav-foreground); - font-size: var(--navigation-font-size); - margin: var(--spacing-large) 0; -} - -div.toc li { - font-size: var(--navigation-font-size); - padding: 0; - background: none; -} - -div.toc li:before { - content: '↓'; - font-weight: 800; - font-family: var(--font-family); - margin-right: var(--spacing-small); - color: var(--side-nav-foreground); - opacity: .4; -} - -div.toc ul li.level1 { - margin: 0; -} - -div.toc ul li.level2, div.toc ul li.level3 { - margin-top: 0; -} - - -@media screen and (max-width: 767px) { - div.toc { - float: none; - width: auto; - margin: 0 0 var(--spacing-medium) 0; - } -} - -/* - Code & Fragments - */ - -code, div.fragment, pre.fragment { - border-radius: var(--border-radius-small); - border: none; - overflow: hidden; -} - -code { - display: inline; - background: var(--code-background); - color: var(--code-foreground); - padding: 2px 6px; - word-break: break-word; -} - -div.fragment, pre.fragment { - margin: var(--spacing-medium) 0; - padding: 14px 16px; - background: var(--fragment-background); - color: var(--fragment-foreground); - overflow-x: auto; -} - -@media screen and (max-width: 767px) { - div.fragment, pre.fragment { - border-top-right-radius: 0; - border-bottom-right-radius: 0; - } - - .contents > div.fragment, .textblock > div.fragment, .textblock > pre.fragment { - margin: var(--spacing-medium) calc(0px - var(--spacing-large)); - border-radius: 0; - } - - .textblock li > .fragment { - margin: var(--spacing-medium) calc(0px - var(--spacing-large)); - } - - .memdoc li > .fragment { - margin: var(--spacing-medium) calc(0px - var(--spacing-medium)); - } - - .memdoc > div.fragment, .memdoc > pre.fragment, dl dd > div.fragment, dl dd pre.fragment { - margin: var(--spacing-medium) calc(0px - var(--spacing-medium)); - border-radius: 0; - } -} - -code, code a, pre.fragment, div.fragment, div.fragment .line, div.fragment span, div.fragment .line a, div.fragment .line span { - font-family: var(--font-family-monospace); - font-size: var(--code-font-size) !important; -} - -div.line:after { - margin-right: var(--spacing-medium); -} - -div.fragment .line, pre.fragment { - white-space: pre; - word-wrap: initial; - line-height: var(--fragment-lineheight); -} - -div.fragment span.keyword { - color: var(--fragment-keyword); -} - -div.fragment span.keywordtype { - color: var(--fragment-keywordtype); -} - -div.fragment span.keywordflow { - color: var(--fragment-keywordflow); -} - -div.fragment span.stringliteral { - color: var(--fragment-token) -} - -div.fragment span.comment { - color: var(--fragment-comment); -} - -div.fragment a.code { - color: var(--fragment-link) !important; -} - -div.fragment span.preprocessor { - color: var(--fragment-preprocessor); -} - -div.fragment span.lineno { - display: inline-block; - width: 27px; - border-right: none; - background: var(--fragment-linenumber-background); - color: var(--fragment-linenumber-color); -} - -div.fragment span.lineno a { - background: none; - color: var(--fragment-link) !important; -} - -div.fragment .line:first-child .lineno { - box-shadow: -999999px 0px 0 999999px var(--fragment-linenumber-background), -999998px 0px 0 999999px var(--fragment-linenumber-border); -} - -/* - dl warning, attention, note, deprecated, bug, ... - */ - -dl.warning, dl.attention, dl.note, dl.deprecated, dl.bug, dl.invariant, dl.pre { - padding: var(--spacing-medium); - margin: var(--spacing-medium) 0; - color: var(--page-background-color); - overflow: hidden; - margin-left: 0; - border-radius: var(--border-radius-small); -} - -dl.section dd { - margin-bottom: 2px; -} - -dl.warning, dl.attention { - background: var(--warning-color); - border-left: 8px solid var(--warning-color-dark); - color: var(--warning-color-darker); -} - -dl.warning dt, dl.attention dt { - color: var(--warning-color-dark); -} - -dl.note { - background: var(--note-color); - border-left: 8px solid var(--note-color-dark); - color: var(--note-color-darker); -} - -dl.note dt { - color: var(--note-color-dark); -} - -dl.bug { - background: var(--bug-color); - border-left: 8px solid var(--bug-color-dark); - color: var(--bug-color-darker); -} - -dl.bug dt a { - color: var(--bug-color-dark) !important; -} - -dl.deprecated { - background: var(--deprecated-color); - border-left: 8px solid var(--deprecated-color-dark); - color: var(--deprecated-color-darker); -} - -dl.deprecated dt a { - color: var(--deprecated-color-dark) !important; -} - -dl.section dd, dl.bug dd, dl.deprecated dd { - margin-inline-start: 0px; -} - -dl.invariant, dl.pre { - background: var(--invariant-color); - border-left: 8px solid var(--invariant-color-dark); - color: var(--invariant-color-darker); -} - -/* - memitem - */ - -div.memdoc, div.memproto, h2.memtitle { - box-shadow: none; - background-image: none; - border: none; -} - -div.memdoc { - padding: 0 var(--spacing-medium); - background: var(--page-background-color); -} - -h2.memtitle, div.memitem { - border: 1px solid var(--separator-color); -} - -div.memproto, h2.memtitle { - background: var(--code-background); - text-shadow: none; -} - -h2.memtitle { - font-weight: 500; - font-family: monospace, fixed; - border-bottom: none; - border-top-left-radius: var(--border-radius-medium); - border-top-right-radius: var(--border-radius-medium); - word-break: break-all; -} - -a:target + h2.memtitle, a:target + h2.memtitle + div.memitem { - border-color: var(--primary-light-color); -} - -a:target + h2.memtitle { - box-shadow: -3px -3px 3px 0 var(--primary-lightest-color), 3px -3px 3px 0 var(--primary-lightest-color); -} - -a:target + h2.memtitle + div.memitem { - box-shadow: 0 0 10px 0 var(--primary-lighter-color); -} - -div.memitem { - border-top-right-radius: var(--border-radius-medium); - border-bottom-right-radius: var(--border-radius-medium); - border-bottom-left-radius: var(--border-radius-medium); - overflow: hidden; - display: block !important; -} - -div.memdoc { - border-radius: 0; -} - -div.memproto { - border-radius: 0 var(--border-radius-small) 0 0; - overflow: auto; - border-bottom: 1px solid var(--separator-color); - padding: var(--spacing-medium); - margin-bottom: -1px; -} - -div.memtitle { - border-top-right-radius: var(--border-radius-medium); - border-top-left-radius: var(--border-radius-medium); -} - -div.memproto table.memname { - font-family: monospace, fixed; - color: var(--page-foreground-color); -} - -table.mlabels, table.mlabels > tbody { - display: block; -} - -td.mlabels-left { - width: auto; -} - -table.mlabels > tbody > tr:first-child { - display: flex; - justify-content: space-between; - flex-wrap: wrap; -} - -.memname, .memitem span.mlabels { - margin: 0 -} - -/* - reflist - */ - -dl.reflist { - box-shadow: var(--box-shadow); - border-radius: var(--border-radius-medium); - border: 1px solid var(--separator-color); - overflow: hidden; - padding: 0; -} - - -dl.reflist dt, dl.reflist dd { - box-shadow: none; - text-shadow: none; - background-image: none; - border: none; - padding: 12px; -} - - -dl.reflist dt { - font-weight: 500; - border-radius: 0; - background: var(--code-background); - border-bottom: 1px solid var(--separator-color); - color: var(--page-foreground-color) -} - - -dl.reflist dd { - background: none; -} - -/* - Table - */ - -table.markdownTable, table.fieldtable { - width: 100%; - border: 1px solid var(--separator-color); - margin: var(--spacing-medium) 0; -} - -table.fieldtable { - box-shadow: none; - border-radius: var(--border-radius-small); -} - -th.markdownTableHeadLeft, th.markdownTableHeadRight, th.markdownTableHeadCenter, th.markdownTableHeadNone { - background: var(--tablehead-background); - color: var(--tablehead-foreground); - font-weight: 600; - font-size: var(--page-font-size); -} - -table.markdownTable td, table.markdownTable th, table.fieldtable dt { - border: 1px solid var(--separator-color); - padding: var(--spacing-small) var(--spacing-medium); -} - -table.fieldtable th { - font-size: var(--page-font-size); - font-weight: 600; - background-image: none; - background-color: var(--tablehead-background); - color: var(--tablehead-foreground); - border-bottom: 1px solid var(--separator-color); -} - -.fieldtable td.fieldtype, .fieldtable td.fieldname { - border-bottom: 1px solid var(--separator-color); - border-right: 1px solid var(--separator-color); -} - -.fieldtable td.fielddoc { - border-bottom: 1px solid var(--separator-color); -} - -.memberdecls td.glow, .fieldtable tr.glow { - background-color: var(--primary-light-color); - box-shadow: 0 0 15px var(--primary-lighter-color); -} - -table.memberdecls { - display: block; - overflow-x: auto; - overflow-y: hidden; -} - - -/* - Horizontal Rule - */ - -hr { - margin-top: var(--spacing-large); - margin-bottom: var(--spacing-large); - border-top:1px solid var(--separator-color); -} - -.contents hr { - box-shadow: var(--content-maxwidth) 0 0 0 var(--separator-color), calc(0px - var(--content-maxwidth)) 0 0 0 var(--separator-color); -} - -.contents img, .contents .center, .contents center { - max-width: 100%; - overflow: scroll; -} - -/* - Directories - */ -div.directory { - border-top: 1px solid var(--separator-color); - border-bottom: 1px solid var(--separator-color); - width: auto; -} - -table.directory { - font-family: var(--font-family); - font-size: var(--page-font-size); - font-weight: normal; -} - -.directory td.entry { - padding: var(--spacing-small); - display: flex; - align-items: center; -} - -.directory tr.even { - background-color: var(--odd-color); -} - -.icona { - width: auto; - height: auto; - margin: 0 var(--spacing-small); -} - -.icon { - background: var(--primary-color); - width: 18px; - height: 18px; - line-height: 18px; -} - -.iconfopen, .icondoc, .iconfclosed { - background-position: center; - margin-bottom: 0; -} - -.icondoc { - filter: saturate(0.2); -} - -@media screen and (max-width: 767px) { - div.directory { - margin-left: calc(0px - var(--spacing-medium)); - margin-right: calc(0px - var(--spacing-medium)); - } -} - -@media (prefers-color-scheme: dark) { - html:not(.light-mode) .iconfopen, html:not(.light-mode) .iconfclosed { - filter: hue-rotate(180deg) invert(); - } -} - -html.dark-mode .iconfopen, html.dark-mode .iconfclosed { - filter: hue-rotate(180deg) invert(); -} - -/* - Class list - */ - -.classindex dl.odd { - background: var(--odd-color); - border-radius: var(--border-radius-small); -} - -@media screen and (max-width: 767px) { - .classindex { - margin: 0 calc(0px - var(--spacing-small)); - } -} - -/* - Footer and nav-path - */ - -#nav-path { - margin-bottom: -1px; - width: 100%; -} - -#nav-path ul { - background-image: none; - background: var(--page-background-color); - border: none; - border-top: 1px solid var(--separator-color); - border-bottom: 1px solid var(--separator-color); - font-size: var(--navigation-font-size); -} - -img.footer { - width: 60px; -} - -.navpath li.footer { - color: var(--page-secondary-foreground-color); -} - -address.footer { - margin-bottom: var(--spacing-large); -} - -#nav-path li.navelem { - background-image: none; - display: flex; - align-items: center; -} - -.navpath li.navelem a { - text-shadow: none; - display: inline-block; - color: var(--primary-color) !important; -} - -.navpath li.navelem b { - color: var(--primary-dark-color); - font-weight: 500; -} - -li.navelem { - padding: 0; - margin-left: -8px; -} - -li.navelem:first-child { - margin-left: var(--spacing-large); -} - -li.navelem:first-child:before { - display: none; -} - -#nav-path li.navelem:after { - content: ''; - border: 5px solid var(--page-background-color); - border-bottom-color: transparent; - border-right-color: transparent; - border-top-color: transparent; - transform: scaleY(4.2); - z-index: 10; - margin-left: 6px; -} - -#nav-path li.navelem:before { - content: ''; - border: 5px solid var(--separator-color); - border-bottom-color: transparent; - border-right-color: transparent; - border-top-color: transparent; - transform: scaleY(3.2); - margin-right: var(--spacing-small); -} - -.navpath li.navelem a:hover { - color: var(--primary-color); -} - -/* - Optional Dark mode toggle button -*/ - -doxygen-awesome-dark-mode-toggle { - display: inline-block; - margin: 0 0 0 var(--spacing-small); - padding: 0; - width: var(--searchbar-height); - height: var(--searchbar-height); - background: none; - border: none; - font-size: 23px; - border-radius: var(--border-radius-medium); - vertical-align: middle; - text-align: center; - line-height: var(--searchbar-height); -} - -doxygen-awesome-dark-mode-toggle:hover { - background: var(--separator-color); -} - -doxygen-awesome-dark-mode-toggle:after { - content: var(--darkmode-toggle-button-icon) -} diff --git a/docs/html/doxygen.css b/docs/html/doxygen.css deleted file mode 100644 index 73ecbb2c..00000000 --- a/docs/html/doxygen.css +++ /dev/null @@ -1,1771 +0,0 @@ -/* The standard CSS for doxygen 1.8.17 */ - -body, table, div, p, dl { - font: 400 14px/22px Roboto,sans-serif; -} - -p.reference, p.definition { - font: 400 14px/22px Roboto,sans-serif; -} - -/* @group Heading Levels */ - -h1.groupheader { - font-size: 150%; -} - -.title { - font: 400 14px/28px Roboto,sans-serif; - font-size: 150%; - font-weight: bold; - margin: 10px 2px; -} - -h2.groupheader { - border-bottom: 1px solid #879ECB; - color: #354C7B; - font-size: 150%; - font-weight: normal; - margin-top: 1.75em; - padding-top: 8px; - padding-bottom: 4px; - width: 100%; -} - -h3.groupheader { - font-size: 100%; -} - -h1, h2, h3, h4, h5, h6 { - -webkit-transition: text-shadow 0.5s linear; - -moz-transition: text-shadow 0.5s linear; - -ms-transition: text-shadow 0.5s linear; - -o-transition: text-shadow 0.5s linear; - transition: text-shadow 0.5s linear; - margin-right: 15px; -} - -h1.glow, h2.glow, h3.glow, h4.glow, h5.glow, h6.glow { - text-shadow: 0 0 15px cyan; -} - -dt { - font-weight: bold; -} - -ul.multicol { - -moz-column-gap: 1em; - -webkit-column-gap: 1em; - column-gap: 1em; - -moz-column-count: 3; - -webkit-column-count: 3; - column-count: 3; -} - -p.startli, p.startdd { - margin-top: 2px; -} - -th p.starttd, p.intertd, p.endtd { - font-size: 100%; - font-weight: 700; -} - -p.starttd { - margin-top: 0px; -} - -p.endli { - margin-bottom: 0px; -} - -p.enddd { - margin-bottom: 4px; -} - -p.endtd { - margin-bottom: 2px; -} - -p.interli { -} - -p.interdd { -} - -p.intertd { -} - -/* @end */ - -caption { - font-weight: bold; -} - -span.legend { - font-size: 70%; - text-align: center; -} - -h3.version { - font-size: 90%; - text-align: center; -} - -div.qindex, div.navtab{ - background-color: #EBEFF6; - border: 1px solid #A3B4D7; - text-align: center; -} - -div.qindex, div.navpath { - width: 100%; - line-height: 140%; -} - -div.navtab { - margin-right: 15px; -} - -/* @group Link Styling */ - -a { - color: #3D578C; - font-weight: normal; - text-decoration: none; -} - -.contents a:visited { - color: #4665A2; -} - -a:hover { - text-decoration: underline; -} - -a.qindex { - font-weight: bold; -} - -a.qindexHL { - font-weight: bold; - background-color: #9CAFD4; - color: #FFFFFF; - border: 1px double #869DCA; -} - -.contents a.qindexHL:visited { - color: #FFFFFF; -} - -a.el { - font-weight: bold; -} - -a.elRef { -} - -a.code, a.code:visited, a.line, a.line:visited { - color: #4665A2; -} - -a.codeRef, a.codeRef:visited, a.lineRef, a.lineRef:visited { - color: #4665A2; -} - -/* @end */ - -dl.el { - margin-left: -1cm; -} - -ul { - overflow: hidden; /*Fixed: list item bullets overlap floating elements*/ -} - -#side-nav ul { - overflow: visible; /* reset ul rule for scroll bar in GENERATE_TREEVIEW window */ -} - -#main-nav ul { - overflow: visible; /* reset ul rule for the navigation bar drop down lists */ -} - -.fragment { - text-align: left; - direction: ltr; - overflow-x: auto; /*Fixed: fragment lines overlap floating elements*/ - overflow-y: hidden; -} - -pre.fragment { - border: 1px solid #C4CFE5; - background-color: #FBFCFD; - padding: 4px 6px; - margin: 4px 8px 4px 2px; - overflow: auto; - word-wrap: break-word; - font-size: 9pt; - line-height: 125%; - font-family: monospace, fixed; - font-size: 105%; -} - -div.fragment { - padding: 0 0 1px 0; /*Fixed: last line underline overlap border*/ - margin: 4px 8px 4px 2px; - background-color: #FBFCFD; - border: 1px solid #C4CFE5; -} - -div.line { - font-family: monospace, fixed; - font-size: 13px; - min-height: 13px; - line-height: 1.0; - text-wrap: unrestricted; - white-space: -moz-pre-wrap; /* Moz */ - white-space: -pre-wrap; /* Opera 4-6 */ - white-space: -o-pre-wrap; /* Opera 7 */ - white-space: pre-wrap; /* CSS3 */ - word-wrap: break-word; /* IE 5.5+ */ - text-indent: -53px; - padding-left: 53px; - padding-bottom: 0px; - margin: 0px; - -webkit-transition-property: background-color, box-shadow; - -webkit-transition-duration: 0.5s; - -moz-transition-property: background-color, box-shadow; - -moz-transition-duration: 0.5s; - -ms-transition-property: background-color, box-shadow; - -ms-transition-duration: 0.5s; - -o-transition-property: background-color, box-shadow; - -o-transition-duration: 0.5s; - transition-property: background-color, box-shadow; - transition-duration: 0.5s; -} - -div.line:after { - content:"\000A"; - white-space: pre; -} - -div.line.glow { - background-color: cyan; - box-shadow: 0 0 10px cyan; -} - - -span.lineno { - padding-right: 4px; - text-align: right; - border-right: 2px solid #0F0; - background-color: #E8E8E8; - white-space: pre; -} -span.lineno a { - background-color: #D8D8D8; -} - -span.lineno a:hover { - background-color: #C8C8C8; -} - -.lineno { - -webkit-touch-callout: none; - -webkit-user-select: none; - -khtml-user-select: none; - -moz-user-select: none; - -ms-user-select: none; - user-select: none; -} - -div.ah, span.ah { - background-color: black; - font-weight: bold; - color: #FFFFFF; - margin-bottom: 3px; - margin-top: 3px; - padding: 0.2em; - border: solid thin #333; - border-radius: 0.5em; - -webkit-border-radius: .5em; - -moz-border-radius: .5em; - box-shadow: 2px 2px 3px #999; - -webkit-box-shadow: 2px 2px 3px #999; - -moz-box-shadow: rgba(0, 0, 0, 0.15) 2px 2px 2px; - background-image: -webkit-gradient(linear, left top, left bottom, from(#eee), to(#000),color-stop(0.3, #444)); - background-image: -moz-linear-gradient(center top, #eee 0%, #444 40%, #000 110%); -} - -div.classindex ul { - list-style: none; - padding-left: 0; -} - -div.classindex span.ai { - display: inline-block; -} - -div.groupHeader { - margin-left: 16px; - margin-top: 12px; - font-weight: bold; -} - -div.groupText { - margin-left: 16px; - font-style: italic; -} - -body { - background-color: white; - color: black; - margin: 0; -} - -div.contents { - margin-top: 10px; - margin-left: 12px; - margin-right: 8px; -} - -td.indexkey { - background-color: #EBEFF6; - font-weight: bold; - border: 1px solid #C4CFE5; - margin: 2px 0px 2px 0; - padding: 2px 10px; - white-space: nowrap; - vertical-align: top; -} - -td.indexvalue { - background-color: #EBEFF6; - border: 1px solid #C4CFE5; - padding: 2px 10px; - margin: 2px 0px; -} - -tr.memlist { - background-color: #EEF1F7; -} - -p.formulaDsp { - text-align: center; -} - -img.formulaDsp { - -} - -img.formulaInl, img.inline { - vertical-align: middle; -} - -div.center { - text-align: center; - margin-top: 0px; - margin-bottom: 0px; - padding: 0px; -} - -div.center img { - border: 0px; -} - -address.footer { - text-align: right; - padding-right: 12px; -} - -img.footer { - border: 0px; - vertical-align: middle; -} - -/* @group Code Colorization */ - -span.keyword { - color: #008000 -} - -span.keywordtype { - color: #604020 -} - -span.keywordflow { - color: #e08000 -} - -span.comment { - color: #800000 -} - -span.preprocessor { - color: #806020 -} - -span.stringliteral { - color: #002080 -} - -span.charliteral { - color: #008080 -} - -span.vhdldigit { - color: #ff00ff -} - -span.vhdlchar { - color: #000000 -} - -span.vhdlkeyword { - color: #700070 -} - -span.vhdllogic { - color: #ff0000 -} - -blockquote { - background-color: #F7F8FB; - border-left: 2px solid #9CAFD4; - margin: 0 24px 0 4px; - padding: 0 12px 0 16px; -} - -blockquote.DocNodeRTL { - border-left: 0; - border-right: 2px solid #9CAFD4; - margin: 0 4px 0 24px; - padding: 0 16px 0 12px; -} - -/* @end */ - -/* -.search { - color: #003399; - font-weight: bold; -} - -form.search { - margin-bottom: 0px; - margin-top: 0px; -} - -input.search { - font-size: 75%; - color: #000080; - font-weight: normal; - background-color: #e8eef2; -} -*/ - -td.tiny { - font-size: 75%; -} - -.dirtab { - padding: 4px; - border-collapse: collapse; - border: 1px solid #A3B4D7; -} - -th.dirtab { - background: #EBEFF6; - font-weight: bold; -} - -hr { - height: 0px; - border: none; - border-top: 1px solid #4A6AAA; -} - -hr.footer { - height: 1px; -} - -/* @group Member Descriptions */ - -table.memberdecls { - border-spacing: 0px; - padding: 0px; -} - -.memberdecls td, .fieldtable tr { - -webkit-transition-property: background-color, box-shadow; - -webkit-transition-duration: 0.5s; - -moz-transition-property: background-color, box-shadow; - -moz-transition-duration: 0.5s; - -ms-transition-property: background-color, box-shadow; - -ms-transition-duration: 0.5s; - -o-transition-property: background-color, box-shadow; - -o-transition-duration: 0.5s; - transition-property: background-color, box-shadow; - transition-duration: 0.5s; -} - -.memberdecls td.glow, .fieldtable tr.glow { - background-color: cyan; - box-shadow: 0 0 15px cyan; -} - -.mdescLeft, .mdescRight, -.memItemLeft, .memItemRight, -.memTemplItemLeft, .memTemplItemRight, .memTemplParams { - background-color: #F9FAFC; - border: none; - margin: 4px; - padding: 1px 0 0 8px; -} - -.mdescLeft, .mdescRight { - padding: 0px 8px 4px 8px; - color: #555; -} - -.memSeparator { - border-bottom: 1px solid #DEE4F0; - line-height: 1px; - margin: 0px; - padding: 0px; -} - -.memItemLeft, .memTemplItemLeft { - white-space: nowrap; -} - -.memItemRight, .memTemplItemRight { - width: 100%; -} - -.memTemplParams { - color: #4665A2; - white-space: nowrap; - font-size: 80%; -} - -/* @end */ - -/* @group Member Details */ - -/* Styles for detailed member documentation */ - -.memtitle { - padding: 8px; - border-top: 1px solid #A8B8D9; - border-left: 1px solid #A8B8D9; - border-right: 1px solid #A8B8D9; - border-top-right-radius: 4px; - border-top-left-radius: 4px; - margin-bottom: -1px; - background-image: url('nav_f.png'); - background-repeat: repeat-x; - background-color: #E2E8F2; - line-height: 1.25; - font-weight: 300; - float:left; -} - -.permalink -{ - font-size: 65%; - display: inline-block; - vertical-align: middle; -} - -.memtemplate { - font-size: 80%; - color: #4665A2; - font-weight: normal; - margin-left: 9px; -} - -.memnav { - background-color: #EBEFF6; - border: 1px solid #A3B4D7; - text-align: center; - margin: 2px; - margin-right: 15px; - padding: 2px; -} - -.mempage { - width: 100%; -} - -.memitem { - padding: 0; - margin-bottom: 10px; - margin-right: 5px; - -webkit-transition: box-shadow 0.5s linear; - -moz-transition: box-shadow 0.5s linear; - -ms-transition: box-shadow 0.5s linear; - -o-transition: box-shadow 0.5s linear; - transition: box-shadow 0.5s linear; - display: table !important; - width: 100%; -} - -.memitem.glow { - box-shadow: 0 0 15px cyan; -} - -.memname { - font-weight: 400; - margin-left: 6px; -} - -.memname td { - vertical-align: bottom; -} - -.memproto, dl.reflist dt { - border-top: 1px solid #A8B8D9; - border-left: 1px solid #A8B8D9; - border-right: 1px solid #A8B8D9; - padding: 6px 0px 6px 0px; - color: #253555; - font-weight: bold; - text-shadow: 0px 1px 1px rgba(255, 255, 255, 0.9); - background-color: #DFE5F1; - /* opera specific markup */ - box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.15); - border-top-right-radius: 4px; - /* firefox specific markup */ - -moz-box-shadow: rgba(0, 0, 0, 0.15) 5px 5px 5px; - -moz-border-radius-topright: 4px; - /* webkit specific markup */ - -webkit-box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.15); - -webkit-border-top-right-radius: 4px; - -} - -.overload { - font-family: "courier new",courier,monospace; - font-size: 65%; -} - -.memdoc, dl.reflist dd { - border-bottom: 1px solid #A8B8D9; - border-left: 1px solid #A8B8D9; - border-right: 1px solid #A8B8D9; - padding: 6px 10px 2px 10px; - background-color: #FBFCFD; - border-top-width: 0; - background-image:url('nav_g.png'); - background-repeat:repeat-x; - background-color: #FFFFFF; - /* opera specific markup */ - border-bottom-left-radius: 4px; - border-bottom-right-radius: 4px; - box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.15); - /* firefox specific markup */ - -moz-border-radius-bottomleft: 4px; - -moz-border-radius-bottomright: 4px; - -moz-box-shadow: rgba(0, 0, 0, 0.15) 5px 5px 5px; - /* webkit specific markup */ - -webkit-border-bottom-left-radius: 4px; - -webkit-border-bottom-right-radius: 4px; - -webkit-box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.15); -} - -dl.reflist dt { - padding: 5px; -} - -dl.reflist dd { - margin: 0px 0px 10px 0px; - padding: 5px; -} - -.paramkey { - text-align: right; -} - -.paramtype { - white-space: nowrap; -} - -.paramname { - color: #602020; - white-space: nowrap; -} -.paramname em { - font-style: normal; -} -.paramname code { - line-height: 14px; -} - -.params, .retval, .exception, .tparams { - margin-left: 0px; - padding-left: 0px; -} - -.params .paramname, .retval .paramname, .tparams .paramname, .exception .paramname { - font-weight: bold; - vertical-align: top; -} - -.params .paramtype, .tparams .paramtype { - font-style: italic; - vertical-align: top; -} - -.params .paramdir, .tparams .paramdir { - font-family: "courier new",courier,monospace; - vertical-align: top; -} - -table.mlabels { - border-spacing: 0px; -} - -td.mlabels-left { - width: 100%; - padding: 0px; -} - -td.mlabels-right { - vertical-align: bottom; - padding: 0px; - white-space: nowrap; -} - -span.mlabels { - margin-left: 8px; -} - -span.mlabel { - background-color: #728DC1; - border-top:1px solid #5373B4; - border-left:1px solid #5373B4; - border-right:1px solid #C4CFE5; - border-bottom:1px solid #C4CFE5; - text-shadow: none; - color: white; - margin-right: 4px; - padding: 2px 3px; - border-radius: 3px; - font-size: 7pt; - white-space: nowrap; - vertical-align: middle; -} - - - -/* @end */ - -/* these are for tree view inside a (index) page */ - -div.directory { - margin: 10px 0px; - border-top: 1px solid #9CAFD4; - border-bottom: 1px solid #9CAFD4; - width: 100%; -} - -.directory table { - border-collapse:collapse; -} - -.directory td { - margin: 0px; - padding: 0px; - vertical-align: top; -} - -.directory td.entry { - white-space: nowrap; - padding-right: 6px; - padding-top: 3px; -} - -.directory td.entry a { - outline:none; -} - -.directory td.entry a img { - border: none; -} - -.directory td.desc { - width: 100%; - padding-left: 6px; - padding-right: 6px; - padding-top: 3px; - border-left: 1px solid rgba(0,0,0,0.05); -} - -.directory tr.even { - padding-left: 6px; - background-color: #F7F8FB; -} - -.directory img { - vertical-align: -30%; -} - -.directory .levels { - white-space: nowrap; - width: 100%; - text-align: right; - font-size: 9pt; -} - -.directory .levels span { - cursor: pointer; - padding-left: 2px; - padding-right: 2px; - color: #3D578C; -} - -.arrow { - color: #9CAFD4; - -webkit-user-select: none; - -khtml-user-select: none; - -moz-user-select: none; - -ms-user-select: none; - user-select: none; - cursor: pointer; - font-size: 80%; - display: inline-block; - width: 16px; - height: 22px; -} - -.icon { - font-family: Arial, Helvetica; - font-weight: bold; - font-size: 12px; - height: 14px; - width: 16px; - display: inline-block; - background-color: #728DC1; - color: white; - text-align: center; - border-radius: 4px; - margin-left: 2px; - margin-right: 2px; -} - -.icona { - width: 24px; - height: 22px; - display: inline-block; -} - -.iconfopen { - width: 24px; - height: 18px; - margin-bottom: 4px; - background-image:url('folderopen.png'); - background-position: 0px -4px; - background-repeat: repeat-y; - vertical-align:top; - display: inline-block; -} - -.iconfclosed { - width: 24px; - height: 18px; - margin-bottom: 4px; - background-image:url('folderclosed.png'); - background-position: 0px -4px; - background-repeat: repeat-y; - vertical-align:top; - display: inline-block; -} - -.icondoc { - width: 24px; - height: 18px; - margin-bottom: 4px; - background-image:url('doc.png'); - background-position: 0px -4px; - background-repeat: repeat-y; - vertical-align:top; - display: inline-block; -} - -table.directory { - font: 400 14px Roboto,sans-serif; -} - -/* @end */ - -div.dynheader { - margin-top: 8px; - -webkit-touch-callout: none; - -webkit-user-select: none; - -khtml-user-select: none; - -moz-user-select: none; - -ms-user-select: none; - user-select: none; -} - -address { - font-style: normal; - color: #2A3D61; -} - -table.doxtable caption { - caption-side: top; -} - -table.doxtable { - border-collapse:collapse; - margin-top: 4px; - margin-bottom: 4px; -} - -table.doxtable td, table.doxtable th { - border: 1px solid #2D4068; - padding: 3px 7px 2px; -} - -table.doxtable th { - background-color: #374F7F; - color: #FFFFFF; - font-size: 110%; - padding-bottom: 4px; - padding-top: 5px; -} - -table.fieldtable { - /*width: 100%;*/ - margin-bottom: 10px; - border: 1px solid #A8B8D9; - border-spacing: 0px; - -moz-border-radius: 4px; - -webkit-border-radius: 4px; - border-radius: 4px; - -moz-box-shadow: rgba(0, 0, 0, 0.15) 2px 2px 2px; - -webkit-box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.15); - box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.15); -} - -.fieldtable td, .fieldtable th { - padding: 3px 7px 2px; -} - -.fieldtable td.fieldtype, .fieldtable td.fieldname { - white-space: nowrap; - border-right: 1px solid #A8B8D9; - border-bottom: 1px solid #A8B8D9; - vertical-align: top; -} - -.fieldtable td.fieldname { - padding-top: 3px; -} - -.fieldtable td.fielddoc { - border-bottom: 1px solid #A8B8D9; - /*width: 100%;*/ -} - -.fieldtable td.fielddoc p:first-child { - margin-top: 0px; -} - -.fieldtable td.fielddoc p:last-child { - margin-bottom: 2px; -} - -.fieldtable tr:last-child td { - border-bottom: none; -} - -.fieldtable th { - background-image:url('nav_f.png'); - background-repeat:repeat-x; - background-color: #E2E8F2; - font-size: 90%; - color: #253555; - padding-bottom: 4px; - padding-top: 5px; - text-align:left; - font-weight: 400; - -moz-border-radius-topleft: 4px; - -moz-border-radius-topright: 4px; - -webkit-border-top-left-radius: 4px; - -webkit-border-top-right-radius: 4px; - border-top-left-radius: 4px; - border-top-right-radius: 4px; - border-bottom: 1px solid #A8B8D9; -} - - -.tabsearch { - top: 0px; - left: 10px; - height: 36px; - background-image: url('tab_b.png'); - z-index: 101; - overflow: hidden; - font-size: 13px; -} - -.navpath ul -{ - font-size: 11px; - background-image:url('tab_b.png'); - background-repeat:repeat-x; - background-position: 0 -5px; - height:30px; - line-height:30px; - color:#8AA0CC; - border:solid 1px #C2CDE4; - overflow:hidden; - margin:0px; - padding:0px; -} - -.navpath li -{ - list-style-type:none; - float:left; - padding-left:10px; - padding-right:15px; - background-image:url('bc_s.png'); - background-repeat:no-repeat; - background-position:right; - color:#364D7C; -} - -.navpath li.navelem a -{ - height:32px; - display:block; - text-decoration: none; - outline: none; - color: #283A5D; - font-family: 'Lucida Grande',Geneva,Helvetica,Arial,sans-serif; - text-shadow: 0px 1px 1px rgba(255, 255, 255, 0.9); - text-decoration: none; -} - -.navpath li.navelem a:hover -{ - color:#6884BD; -} - -.navpath li.footer -{ - list-style-type:none; - float:right; - padding-left:10px; - padding-right:15px; - background-image:none; - background-repeat:no-repeat; - background-position:right; - color:#364D7C; - font-size: 8pt; -} - - -div.summary -{ - float: right; - font-size: 8pt; - padding-right: 5px; - width: 50%; - text-align: right; -} - -div.summary a -{ - white-space: nowrap; -} - -table.classindex -{ - margin: 10px; - white-space: nowrap; - margin-left: 3%; - margin-right: 3%; - width: 94%; - border: 0; - border-spacing: 0; - padding: 0; -} - -div.ingroups -{ - font-size: 8pt; - width: 50%; - text-align: left; -} - -div.ingroups a -{ - white-space: nowrap; -} - -div.header -{ - background-image:url('nav_h.png'); - background-repeat:repeat-x; - background-color: #F9FAFC; - margin: 0px; - border-bottom: 1px solid #C4CFE5; -} - -div.headertitle -{ - padding: 5px 5px 5px 10px; -} - -.PageDocRTL-title div.headertitle { - text-align: right; - direction: rtl; -} - -dl { - padding: 0 0 0 0; -} - -/* dl.note, dl.warning, dl.attention, dl.pre, dl.post, dl.invariant, dl.deprecated, dl.todo, dl.test, dl.bug, dl.examples */ -dl.section { - margin-left: 0px; - padding-left: 0px; -} - -dl.section.DocNodeRTL { - margin-right: 0px; - padding-right: 0px; -} - -dl.note { - margin-left: -7px; - padding-left: 3px; - border-left: 4px solid; - border-color: #D0C000; -} - -dl.note.DocNodeRTL { - margin-left: 0; - padding-left: 0; - border-left: 0; - margin-right: -7px; - padding-right: 3px; - border-right: 4px solid; - border-color: #D0C000; -} - -dl.warning, dl.attention { - margin-left: -7px; - padding-left: 3px; - border-left: 4px solid; - border-color: #FF0000; -} - -dl.warning.DocNodeRTL, dl.attention.DocNodeRTL { - margin-left: 0; - padding-left: 0; - border-left: 0; - margin-right: -7px; - padding-right: 3px; - border-right: 4px solid; - border-color: #FF0000; -} - -dl.pre, dl.post, dl.invariant { - margin-left: -7px; - padding-left: 3px; - border-left: 4px solid; - border-color: #00D000; -} - -dl.pre.DocNodeRTL, dl.post.DocNodeRTL, dl.invariant.DocNodeRTL { - margin-left: 0; - padding-left: 0; - border-left: 0; - margin-right: -7px; - padding-right: 3px; - border-right: 4px solid; - border-color: #00D000; -} - -dl.deprecated { - margin-left: -7px; - padding-left: 3px; - border-left: 4px solid; - border-color: #505050; -} - -dl.deprecated.DocNodeRTL { - margin-left: 0; - padding-left: 0; - border-left: 0; - margin-right: -7px; - padding-right: 3px; - border-right: 4px solid; - border-color: #505050; -} - -dl.todo { - margin-left: -7px; - padding-left: 3px; - border-left: 4px solid; - border-color: #00C0E0; -} - -dl.todo.DocNodeRTL { - margin-left: 0; - padding-left: 0; - border-left: 0; - margin-right: -7px; - padding-right: 3px; - border-right: 4px solid; - border-color: #00C0E0; -} - -dl.test { - margin-left: -7px; - padding-left: 3px; - border-left: 4px solid; - border-color: #3030E0; -} - -dl.test.DocNodeRTL { - margin-left: 0; - padding-left: 0; - border-left: 0; - margin-right: -7px; - padding-right: 3px; - border-right: 4px solid; - border-color: #3030E0; -} - -dl.bug { - margin-left: -7px; - padding-left: 3px; - border-left: 4px solid; - border-color: #C08050; -} - -dl.bug.DocNodeRTL { - margin-left: 0; - padding-left: 0; - border-left: 0; - margin-right: -7px; - padding-right: 3px; - border-right: 4px solid; - border-color: #C08050; -} - -dl.section dd { - margin-bottom: 6px; -} - - -#projectlogo -{ - text-align: center; - vertical-align: bottom; - border-collapse: separate; -} - -#projectlogo img -{ - border: 0px none; -} - -#projectalign -{ - vertical-align: middle; -} - -#projectname -{ - font: 300% Tahoma, Arial,sans-serif; - margin: 0px; - padding: 2px 0px; -} - -#projectbrief -{ - font: 120% Tahoma, Arial,sans-serif; - margin: 0px; - padding: 0px; -} - -#projectnumber -{ - font: 50% Tahoma, Arial,sans-serif; - margin: 0px; - padding: 0px; -} - -#titlearea -{ - padding: 0px; - margin: 0px; - width: 100%; - border-bottom: 1px solid #5373B4; -} - -.image -{ - text-align: center; -} - -.dotgraph -{ - text-align: center; -} - -.mscgraph -{ - text-align: center; -} - -.plantumlgraph -{ - text-align: center; -} - -.diagraph -{ - text-align: center; -} - -.caption -{ - font-weight: bold; -} - -div.zoom -{ - border: 1px solid #90A5CE; -} - -dl.citelist { - margin-bottom:50px; -} - -dl.citelist dt { - color:#334975; - float:left; - font-weight:bold; - margin-right:10px; - padding:5px; -} - -dl.citelist dd { - margin:2px 0; - padding:5px 0; -} - -div.toc { - padding: 14px 25px; - background-color: #F4F6FA; - border: 1px solid #D8DFEE; - border-radius: 7px 7px 7px 7px; - float: right; - height: auto; - margin: 0 8px 10px 10px; - width: 200px; -} - -.PageDocRTL-title div.toc { - float: left !important; - text-align: right; -} - -div.toc li { - background: url("bdwn.png") no-repeat scroll 0 5px transparent; - font: 10px/1.2 Verdana,DejaVu Sans,Geneva,sans-serif; - margin-top: 5px; - padding-left: 10px; - padding-top: 2px; -} - -.PageDocRTL-title div.toc li { - background-position-x: right !important; - padding-left: 0 !important; - padding-right: 10px; -} - -div.toc h3 { - font: bold 12px/1.2 Arial,FreeSans,sans-serif; - color: #4665A2; - border-bottom: 0 none; - margin: 0; -} - -div.toc ul { - list-style: none outside none; - border: medium none; - padding: 0px; -} - -div.toc li.level1 { - margin-left: 0px; -} - -div.toc li.level2 { - margin-left: 15px; -} - -div.toc li.level3 { - margin-left: 30px; -} - -div.toc li.level4 { - margin-left: 45px; -} - -.PageDocRTL-title div.toc li.level1 { - margin-left: 0 !important; - margin-right: 0; -} - -.PageDocRTL-title div.toc li.level2 { - margin-left: 0 !important; - margin-right: 15px; -} - -.PageDocRTL-title div.toc li.level3 { - margin-left: 0 !important; - margin-right: 30px; -} - -.PageDocRTL-title div.toc li.level4 { - margin-left: 0 !important; - margin-right: 45px; -} - -.inherit_header { - font-weight: bold; - color: gray; - cursor: pointer; - -webkit-touch-callout: none; - -webkit-user-select: none; - -khtml-user-select: none; - -moz-user-select: none; - -ms-user-select: none; - user-select: none; -} - -.inherit_header td { - padding: 6px 0px 2px 5px; -} - -.inherit { - display: none; -} - -tr.heading h2 { - margin-top: 12px; - margin-bottom: 4px; -} - -/* tooltip related style info */ - -.ttc { - position: absolute; - display: none; -} - -#powerTip { - cursor: default; - white-space: nowrap; - background-color: white; - border: 1px solid gray; - border-radius: 4px 4px 4px 4px; - box-shadow: 1px 1px 7px gray; - display: none; - font-size: smaller; - max-width: 80%; - opacity: 0.9; - padding: 1ex 1em 1em; - position: absolute; - z-index: 2147483647; -} - -#powerTip div.ttdoc { - color: grey; - font-style: italic; -} - -#powerTip div.ttname a { - font-weight: bold; -} - -#powerTip div.ttname { - font-weight: bold; -} - -#powerTip div.ttdeci { - color: #006318; -} - -#powerTip div { - margin: 0px; - padding: 0px; - font: 12px/16px Roboto,sans-serif; -} - -#powerTip:before, #powerTip:after { - content: ""; - position: absolute; - margin: 0px; -} - -#powerTip.n:after, #powerTip.n:before, -#powerTip.s:after, #powerTip.s:before, -#powerTip.w:after, #powerTip.w:before, -#powerTip.e:after, #powerTip.e:before, -#powerTip.ne:after, #powerTip.ne:before, -#powerTip.se:after, #powerTip.se:before, -#powerTip.nw:after, #powerTip.nw:before, -#powerTip.sw:after, #powerTip.sw:before { - border: solid transparent; - content: " "; - height: 0; - width: 0; - position: absolute; -} - -#powerTip.n:after, #powerTip.s:after, -#powerTip.w:after, #powerTip.e:after, -#powerTip.nw:after, #powerTip.ne:after, -#powerTip.sw:after, #powerTip.se:after { - border-color: rgba(255, 255, 255, 0); -} - -#powerTip.n:before, #powerTip.s:before, -#powerTip.w:before, #powerTip.e:before, -#powerTip.nw:before, #powerTip.ne:before, -#powerTip.sw:before, #powerTip.se:before { - border-color: rgba(128, 128, 128, 0); -} - -#powerTip.n:after, #powerTip.n:before, -#powerTip.ne:after, #powerTip.ne:before, -#powerTip.nw:after, #powerTip.nw:before { - top: 100%; -} - -#powerTip.n:after, #powerTip.ne:after, #powerTip.nw:after { - border-top-color: #FFFFFF; - border-width: 10px; - margin: 0px -10px; -} -#powerTip.n:before { - border-top-color: #808080; - border-width: 11px; - margin: 0px -11px; -} -#powerTip.n:after, #powerTip.n:before { - left: 50%; -} - -#powerTip.nw:after, #powerTip.nw:before { - right: 14px; -} - -#powerTip.ne:after, #powerTip.ne:before { - left: 14px; -} - -#powerTip.s:after, #powerTip.s:before, -#powerTip.se:after, #powerTip.se:before, -#powerTip.sw:after, #powerTip.sw:before { - bottom: 100%; -} - -#powerTip.s:after, #powerTip.se:after, #powerTip.sw:after { - border-bottom-color: #FFFFFF; - border-width: 10px; - margin: 0px -10px; -} - -#powerTip.s:before, #powerTip.se:before, #powerTip.sw:before { - border-bottom-color: #808080; - border-width: 11px; - margin: 0px -11px; -} - -#powerTip.s:after, #powerTip.s:before { - left: 50%; -} - -#powerTip.sw:after, #powerTip.sw:before { - right: 14px; -} - -#powerTip.se:after, #powerTip.se:before { - left: 14px; -} - -#powerTip.e:after, #powerTip.e:before { - left: 100%; -} -#powerTip.e:after { - border-left-color: #FFFFFF; - border-width: 10px; - top: 50%; - margin-top: -10px; -} -#powerTip.e:before { - border-left-color: #808080; - border-width: 11px; - top: 50%; - margin-top: -11px; -} - -#powerTip.w:after, #powerTip.w:before { - right: 100%; -} -#powerTip.w:after { - border-right-color: #FFFFFF; - border-width: 10px; - top: 50%; - margin-top: -10px; -} -#powerTip.w:before { - border-right-color: #808080; - border-width: 11px; - top: 50%; - margin-top: -11px; -} - -@media print -{ - #top { display: none; } - #side-nav { display: none; } - #nav-path { display: none; } - body { overflow:visible; } - h1, h2, h3, h4, h5, h6 { page-break-after: avoid; } - .summary { display: none; } - .memitem { page-break-inside: avoid; } - #doc-content - { - margin-left:0 !important; - height:auto !important; - width:auto !important; - overflow:inherit; - display:inline; - } -} - -/* @group Markdown */ - -/* -table.markdownTable { - border-collapse:collapse; - margin-top: 4px; - margin-bottom: 4px; -} - -table.markdownTable td, table.markdownTable th { - border: 1px solid #2D4068; - padding: 3px 7px 2px; -} - -table.markdownTableHead tr { -} - -table.markdownTableBodyLeft td, table.markdownTable th { - border: 1px solid #2D4068; - padding: 3px 7px 2px; -} - -th.markdownTableHeadLeft th.markdownTableHeadRight th.markdownTableHeadCenter th.markdownTableHeadNone { - background-color: #374F7F; - color: #FFFFFF; - font-size: 110%; - padding-bottom: 4px; - padding-top: 5px; -} - -th.markdownTableHeadLeft { - text-align: left -} - -th.markdownTableHeadRight { - text-align: right -} - -th.markdownTableHeadCenter { - text-align: center -} -*/ - -table.markdownTable { - border-collapse:collapse; - margin-top: 4px; - margin-bottom: 4px; -} - -table.markdownTable td, table.markdownTable th { - border: 1px solid #2D4068; - padding: 3px 7px 2px; -} - -table.markdownTable tr { -} - -th.markdownTableHeadLeft, th.markdownTableHeadRight, th.markdownTableHeadCenter, th.markdownTableHeadNone { - background-color: #374F7F; - color: #FFFFFF; - font-size: 110%; - padding-bottom: 4px; - padding-top: 5px; -} - -th.markdownTableHeadLeft, td.markdownTableBodyLeft { - text-align: left -} - -th.markdownTableHeadRight, td.markdownTableBodyRight { - text-align: right -} - -th.markdownTableHeadCenter, td.markdownTableBodyCenter { - text-align: center -} - -.DocNodeRTL { - text-align: right; - direction: rtl; -} - -.DocNodeLTR { - text-align: left; - direction: ltr; -} - -table.DocNodeRTL { - width: auto; - margin-right: 0; - margin-left: auto; -} - -table.DocNodeLTR { - width: auto; - margin-right: auto; - margin-left: 0; -} - -tt, code, kbd, samp -{ - display: inline-block; - direction:ltr; -} -/* @end */ - -u { - text-decoration: underline; -} - diff --git a/docs/html/dynsections.js b/docs/html/dynsections.js deleted file mode 100644 index ea0a7b39..00000000 --- a/docs/html/dynsections.js +++ /dev/null @@ -1,120 +0,0 @@ -/* - @licstart The following is the entire license notice for the - JavaScript code in this file. - - Copyright (C) 1997-2017 by Dimitri van Heesch - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 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 General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - @licend The above is the entire license notice - for the JavaScript code in this file - */ -function toggleVisibility(linkObj) -{ - var base = $(linkObj).attr('id'); - var summary = $('#'+base+'-summary'); - var content = $('#'+base+'-content'); - var trigger = $('#'+base+'-trigger'); - var src=$(trigger).attr('src'); - if (content.is(':visible')===true) { - content.hide(); - summary.show(); - $(linkObj).addClass('closed').removeClass('opened'); - $(trigger).attr('src',src.substring(0,src.length-8)+'closed.png'); - } else { - content.show(); - summary.hide(); - $(linkObj).removeClass('closed').addClass('opened'); - $(trigger).attr('src',src.substring(0,src.length-10)+'open.png'); - } - return false; -} - -function updateStripes() -{ - $('table.directory tr'). - removeClass('even').filter(':visible:even').addClass('even'); -} - -function toggleLevel(level) -{ - $('table.directory tr').each(function() { - var l = this.id.split('_').length-1; - var i = $('#img'+this.id.substring(3)); - var a = $('#arr'+this.id.substring(3)); - if (l - - - - - - -binlex: File List - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
File List
-
-
-
Here is a list of all documented files with brief descriptions:
-
[detail level 12]
- - - - - - - - - - - - - - -
  include
 args.h
 auto.h
 blelf.h
 cil.h
 common.h
 decompiler.h
 decompilerbase.h
 file.h
 jvm.h
 pe-dotnet.h
 pe.h
 raw.h
 sha256.h
-
-
-
- - - - diff --git a/docs/html/files_dup.js b/docs/html/files_dup.js deleted file mode 100644 index f1749d90..00000000 --- a/docs/html/files_dup.js +++ /dev/null @@ -1,4 +0,0 @@ -var files_dup = -[ - [ "include", "dir_d44c64559bbebec7f509842c48db8b23.html", "dir_d44c64559bbebec7f509842c48db8b23" ] -]; \ No newline at end of file diff --git a/docs/html/folderclosed.png b/docs/html/folderclosed.png deleted file mode 100644 index bb8ab35e..00000000 Binary files a/docs/html/folderclosed.png and /dev/null differ diff --git a/docs/html/folderopen.png b/docs/html/folderopen.png deleted file mode 100644 index d6c7f676..00000000 Binary files a/docs/html/folderopen.png and /dev/null differ diff --git a/docs/html/functions.html b/docs/html/functions.html deleted file mode 100644 index e90fed37..00000000 --- a/docs/html/functions.html +++ /dev/null @@ -1,385 +0,0 @@ - - - - - - - -binlex: Class Members - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
Here is a list of all documented class members with links to the class documentation for each member:
- -

- a -

- - -

- c -

- - -

- d -

- - -

- e -

- - -

- f -

- - -

- g -

- - -

- h -

- - -

- i -

- - -

- l -

- - -

- m -

- - -

- p -

- - -

- r -

- - -

- s -

- - -

- t -

- - -

- w -

-
-
- - - - diff --git a/docs/html/functions_func.html b/docs/html/functions_func.html deleted file mode 100644 index 715727f1..00000000 --- a/docs/html/functions_func.html +++ /dev/null @@ -1,372 +0,0 @@ - - - - - - - -binlex: Class Members - Functions - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-  - -

- a -

- - -

- c -

- - -

- d -

- - -

- e -

- - -

- g -

- - -

- h -

- - -

- i -

- - -

- l -

- - -

- m -

- - -

- p -

- - -

- r -

- - -

- s -

- - -

- t -

- - -

- w -

-
-
- - - - diff --git a/docs/html/hierarchy.html b/docs/html/hierarchy.html deleted file mode 100644 index 36065b73..00000000 --- a/docs/html/hierarchy.html +++ /dev/null @@ -1,149 +0,0 @@ - - - - - - - -binlex: Class Hierarchy - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- - - - - - diff --git a/docs/html/hierarchy.js b/docs/html/hierarchy.js deleted file mode 100644 index 61a735c0..00000000 --- a/docs/html/hierarchy.js +++ /dev/null @@ -1,55 +0,0 @@ -var hierarchy = -[ - [ "binlex::Args", "classbinlex_1_1Args.html", null ], - [ "binlex::AutoLex", "classbinlex_1_1AutoLex.html", null ], - [ "dotnet::BlobHeapIndex", "classdotnet_1_1BlobHeapIndex.html", null ], - [ "binlex::Common", "classbinlex_1_1Common.html", [ - [ "binlex::DecompilerBase", "classbinlex_1_1DecompilerBase.html", [ - [ "binlex::CILDecompiler", "classbinlex_1_1CILDecompiler.html", null ], - [ "binlex::Decompiler", "classbinlex_1_1Decompiler.html", null ] - ] ], - [ "binlex::File", "classbinlex_1_1File.html", [ - [ "binlex::ELF", "classbinlex_1_1ELF.html", null ], - [ "binlex::PE", "classbinlex_1_1PE.html", [ - [ "binlex::DOTNET", "classbinlex_1_1DOTNET.html", null ] - ] ], - [ "binlex::Raw", "classbinlex_1_1Raw.html", null ] - ] ] - ] ], - [ "dotnet::COR20_HEADER", "structdotnet_1_1COR20__HEADER.html", null ], - [ "dotnet::COR20_STORAGE_HEADER", "structdotnet_1_1COR20__STORAGE__HEADER.html", null ], - [ "dotnet::COR20_STORAGE_SIGNATURE", "structdotnet_1_1COR20__STORAGE__SIGNATURE.html", null ], - [ "dotnet::COR20_STREAM_HEADER", "structdotnet_1_1COR20__STREAM__HEADER.html", null ], - [ "dotnet::Cor20MetadataTable", "classdotnet_1_1Cor20MetadataTable.html", null ], - [ "dotnet::GuidHeapIndex", "classdotnet_1_1GuidHeapIndex.html", null ], - [ "binlex::CILDecompiler::Instruction", "structbinlex_1_1CILDecompiler_1_1Instruction.html", null ], - [ "dotnet::Method", "classdotnet_1_1Method.html", null ], - [ "dotnet::MethodHeader", "classdotnet_1_1MethodHeader.html", [ - [ "dotnet::FatHeader", "classdotnet_1_1FatHeader.html", null ], - [ "dotnet::TinyHeader", "classdotnet_1_1TinyHeader.html", null ] - ] ], - [ "dotnet::MultiTableIndex", "classdotnet_1_1MultiTableIndex.html", [ - [ "dotnet::ResolutionScopeIndex", "classdotnet_1_1ResolutionScopeIndex.html", null ], - [ "dotnet::TypeDefOrRefIndex", "classdotnet_1_1TypeDefOrRefIndex.html", null ] - ] ], - [ "dotnet::TableEntry::ParseArgs", "structdotnet_1_1TableEntry_1_1ParseArgs.html", null ], - [ "binlex::Decompiler::Section", "structbinlex_1_1Decompiler_1_1Section.html", null ], - [ "binlex::Raw::Section", "structbinlex_1_1Raw_1_1Section.html", null ], - [ "binlex::File::Section", "structbinlex_1_1File_1_1Section.html", null ], - [ "binlex::CILDecompiler::Section", "structbinlex_1_1CILDecompiler_1_1Section.html", null ], - [ "SHA256_CTX", "structSHA256__CTX.html", null ], - [ "dotnet::SimpleTableIndex", "classdotnet_1_1SimpleTableIndex.html", null ], - [ "dotnet::StringHeapIndex", "classdotnet_1_1StringHeapIndex.html", null ], - [ "dotnet::TableEntry", "classdotnet_1_1TableEntry.html", [ - [ "dotnet::FieldEntry", "classdotnet_1_1FieldEntry.html", null ], - [ "dotnet::FieldPtrEntry", "classdotnet_1_1FieldPtrEntry.html", null ], - [ "dotnet::MethodDefEntry", "classdotnet_1_1MethodDefEntry.html", null ], - [ "dotnet::MethodPtrEntry", "classdotnet_1_1MethodPtrEntry.html", null ], - [ "dotnet::ModuleEntry", "classdotnet_1_1ModuleEntry.html", null ], - [ "dotnet::TypeDefEntry", "classdotnet_1_1TypeDefEntry.html", null ], - [ "dotnet::TypeRefEntry", "classdotnet_1_1TypeRefEntry.html", null ] - ] ], - [ "binlex::TimedCode", "classbinlex_1_1TimedCode.html", null ], - [ "binlex::Decompiler::Trait", "structbinlex_1_1Decompiler_1_1Trait.html", null ], - [ "binlex::CILDecompiler::Trait", "structbinlex_1_1CILDecompiler_1_1Trait.html", null ] -]; \ No newline at end of file diff --git a/docs/html/index.html b/docs/html/index.html deleted file mode 100644 index a4fa934b..00000000 --- a/docs/html/index.html +++ /dev/null @@ -1,466 +0,0 @@ - - - - - - - -binlex: binlex - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
binlex
-
-
-

A Genetic Binary Trait Lexer Library and Utility

-

The purpose of binlex is to extract basic blocks and functions as traits from binaries for malware research, hunting and detection.

-

Most projects attempting this use Python to generate traits, but it is very slow.

-

The design philosophy behind binlex is it to keep it simple and extendable.

-

The simple command-line interface allows malware researchers and analysts to hunt traits across hundreds or thousands of potentially similar malware saving time and money in production environments.

-

While the C++ API allows developers to get creative with their own detection solutions, completely unencumbered by license limitations.

-

To help combat malware, we firmly commit our work to the public domain for the greater good of the world.

-

Build status OS Linux OS Windows GitHub stars GitHub forks Discord Status GitHub license GitHub all releases

-

-Demos

-

animated

-

Introduction Video

-

Get slides here.

-

-Use Cases

-
    -
  • YARA Signature Creation/Automation
  • -
  • Identifying Code-Reuse
  • -
  • Threat Hunting
  • -
  • Building Goodware Trait Corpus
  • -
  • Building Malware Trait Corpus
  • -
  • Genetic Programming
  • -
  • Machine Learning Malware Detection
  • -
-

-Installation

-

Dependencies:

-

To get started you will need the following dependencies for binlex.

-
sudo apt install -y git build-essential \
-
cmake make parallel \
-
doxygen git-lfs rpm \
-
python3 python3-dev
-
git clone --recursive https://github.com/c3rb3ru5d3d53c/binlex.git
-
cd binlex/
-

NOTE: that binlex requires cmake >= 3.5, make >= 4.2.1 and ubuntu >= 20.04.

-

Once you have installed, cloned and changed your directory to the project directory, we can continue with installation.

-

From Source:

-

If you want to compile and install via make install run the following commands:

-
make threads=4
-
sudo make install
-
-
# Test your Install
-
binlex -m auto -i tests/elf/elf.x86
-

Binary Releases:

-

See the releases page.

-

If you need the bleeding edge binaries you can download them from our AppVeyor CI/CD here.

-

NOTE: bleeding edge binaries are subject to bugs, if you encounter one, please let us know!

-

Test Files:

-
    -
  • To download all the test samples do the command git lfs fetch
  • -
  • ZIP files in the tests/ directory can then be extracted using the password infected
  • -
-

NOTE: The tests/ directory contains malware, we assume you know what you are doing.

-

To download individual git-lfs files from a relative path, you can use the following git alias in ~/.gitconfig:

-
[alias]
-
download = "!ROOT=$(git rev-parse --show-toplevel); cd $ROOT; git lfs pull --include $GIT_PREFIX$1; cd $ROOT/$GIT_PREFIX"
-

You will then be able to do the following:

-
git download tests/pe/pe.zip
-

Building Packages:

-

Additionally, another option is to build Debian binary packages for and install those.

-

To build packages use cpack, which comes with cmake.

-
make threads=4
-
make pkg # builds binary packages
-
make dist # builds source packages
-
sudo apt install ./build/binlex_1.1.1_amd64.deb
-
binlex -m elf:x86 -i tests/elf/elf.x86
-

You will then be provided with .deb, .rpm and .tar.gz packages for binlex.

-

Building Python Bindings:

-

To get started using pybinlex:

virtualenv -p python3 venv
-
source venv/bin/activate
-
# Install Library
-
pip install -v .
-
# Build Wheel Package
-
pip wheel -v -w build/ .
-
python3
-
>>> import pybinlex
-

If you wish to compile the bindings with cmake:

make python
-

NOTE: we use pybind11 and support for python3.9 is experimental.

-

Examples of how to use pybinlex can be found in tests/tests.py.

-

-Building Binlex Platform:

-

Install Dependencies:

-
sudo apt install docker.io make
-
sudo usermod -a -G docker $USER
-
sudo systemctl enable docker
-
reboot # ensures your user is added to the docker group
-

Building Containers:

-
make docker # generate docker-compose.yml and config files
-
# Your generated credentials will be printed to the screen and saved in config/credentials.txt
-
make docker-build # build the images (can take a long time, go get a coffee!)
-
make docker-start # start the containers
-
make docker-init # initialize all databases and generated configurations
-
make docker-logs # tail all logs
-

If you wish to change the auto-generated initial username and passwords, you can run ./docker.sh with additional parameters.

-

To see what parameters are available to you, run ./docker.sh --help.

-

Platform URLs:

-
    -
  • HTTP API https://127.0.0.1:8443 (API Docs)
  • -
  • RabbitMQ https://127.0.0.1:15672/ (Messaging)
  • -
  • MinIO https://127.0.0.1:9001/ (Object Store)
  • -
  • MongoDB mongodb://<user>:<pass>@127.0.0.1 (MongoDB)
      -
    • User mongodb://binlex:<generated-password>@127.0.0.1/?authSource=binlex (for trait collection)
    • -
    • Admin mongodb://admin:<generated-password>@127.0.0.1 (for administration)
    • -
    -
  • -
-

Example HTTP API Requests:

# Get Version
-
curl --insecure -H "X-API-Key: <key>" https://127.0.0.1:8443/binlex/api/v1/version
-
-
# Upload Sample
-
curl -X POST --insecure -H "X-API-Key: <key>" --upload-file <file> https://127.0.0.1:8443/binlex/api/v1/samples/<corpus>/<mode>
-
-
# List Corpra
-
curl -X GET --insecure -H "X-API-Key: <key>" https://127.0.0.1:8443/binlex/api/v1/corpra
-
-
# Get Supported Modes
-
curl -X GET --insecure -H "X-API-Key: <key>" https://127.0.0.1:8443/binlex/api/v1/modes
-
-
# Download Sample by SHA256
-
curl -X GET --insecure -H "X-API-Key: <key>" https://127.0.0.1:8443/binlex/api/v1/samples/<sha256>
-

If you work with a team of malware analysts or malware researchers, you create read-only accounts for them.

-

This will ensure they can do advanced queries to hunt and write detection signatures.

-

Adding New Read-Only Users to MongoDB:

cd scripts/
-
./mongodb-createuser.sh mongodb-router1 <username> <password>
-

If you have a VERY large team, you can script creation of these accounts.

-

-CLI Usage

-
binlex v1.1.1 - A Binary Genetic Traits Lexer
-
-i --input input file (required)
-
-m --mode set mode (optional)
-
-lm --list-modes list modes (optional)
-
--instructions include insn traits (optional)
-
-c --corpus corpus name (optional)
-
-g --tag add a tag (optional)
-
(can be specified multiple times)
-
-t --threads number of threads (optional)
-
-to --timeout execution timeout in s (optional)
-
-h --help display help (optional)
-
-o --output output file (optional)
-
-p --pretty pretty output (optional)
-
-d --debug print debug info (optional)
-
-v --version display version (optional)
-
Author: @c3rb3ru5d3d53c
-

Supported Modes

-
    -
  • elf:x86
  • -
  • elf:x86_64
  • -
  • pe:x86
  • -
  • pe:x86_64
  • -
  • pe:cil
  • -
  • raw:x86
  • -
  • raw:x86_64
  • -
  • raw:cil
  • -
  • auto
  • -
-

NOTE: The raw modes can be used on shellcode.

-

NOTE: The auto mode cannot be used on shellcode.

-

Advanced

-

If you are hunting using binlex you can use jq to your advantage for advanced searches.

-
build/binlex -m auto -i tests/pe/pe.x86 | jq -r 'select((.size > 8 and .size < 16) and (.bytes_sha256 != .traits.sha256)) | .trait' | head -10
-
8b 48 ?? 03 c8 81 39 50 45 00 00 75 12
-
0f b7 41 ?? 3d 0b 01 00 00 74 1f
-
83 b9 ?? ?? ?? ?? ?? 76 f2
-
33 c0 39 b9 ?? ?? ?? ?? eb 0e
-
83 4d ?? ?? b8 ff 00 00 00 e9 ba 00 00 00
-
89 75 ?? 66 83 3e 22 75 45
-
03 f3 89 75 ?? 66 8b 06 66 3b c7 74 06
-
03 f3 89 75 ?? 66 8b 06 66 3b c7 74 06
-
56 ff 15 ?? ?? ?? ?? ff 15 ?? ?? ?? ?? eb 2d
-
55 8b ec 51 56 33 f6 66 89 33 8a 07 eb 29
-

Here are examples of additional queries.

-
# Block traits with a size between 0 and 32 bytes
-
jq -r 'select(.type == "block" and .size < 32 and .size > 0)'
-
# Function traits with a cyclomatic complexity greater than 32 (maybe obfuscation)
-
jq -r 'select(.type == "function" and .cyclomatic_complexity > 32)'
-
# Traits where bytes have high entropy
-
jq -r 'select(.bytes_entropy > 7)'
-
# Output all trait strings only
-
jq -r '.trait'
-
# Output only trait hashes
-
jq -r '.trait_sha256'
-

If you output just traits you want to stdout you can do build a yara signature on the fly with the included tool blyara:

-
build/binlex -m raw:x86 -i tests/raw/raw.x86 | jq -r 'select(.size > 16 and .size < 32) | .trait' | build/blyara --name example_0 -m author example -m tlp white -c 1
-
rule example_0 {
-
metadata:
-
author = "example"
-
tlp = "white"
-
strings:
-
trait_0 = {52 57 8b 52 ?? 8b 42 ?? 01 d0 8b 40 ?? 85 c0 74 4c}
-
trait_1 = {49 8b 34 8b 01 d6 31 ff 31 c0 c1 cf ?? ac 01 c7 38 e0 75 f4}
-
trait_2 = {e8 67 00 00 00 6a 00 6a ?? 56 57 68 ?? ?? ?? ?? ff d5 83 f8 00 7e 36}
-
condition:
-
1 of them
-
}
-

You can also use the switch --pretty to output json to identify more properies to query.

-
build/binlex -m auto -i tests/pe/pe.emotet.x86 -c malware -g malware:emotet -g malware:loader | head -1 | jq
-
{
-
"average_instructions_per_block": 29,
-
"blocks": 1,
-
"bytes": "55 8b ec 83 ec 1c 83 65 f0 00 33 d2 c7 45 e4 68 5d df 00 c7 45 e8 43 c4 cb 00 c7 45 ec 8f 08 46 00 c7 45 f8 06 3b 43 00 81 45 f8 25 7a ff ff 81 75 f8 30 f4 44 00 c7 45 fc 22 51 53 00 8b 45 fc 6a 3f 59 f7 f1 6a 1c 89 45 fc 33 d2 8b 45 fc 59 f7 f1 89 45 fc 81 75 fc 3c 95 0e 00 c7 45 f4 0b 16 11 00 81 45 f4 e1 21 ff ff 81 75 f4 79 bd 15 00 ff 4d 0c 75 21",
-
"bytes_entropy": 5.333979606628418,
-
"bytes_sha256": "13e0463c5837bc5ce110990d69397662b82b8de8a9971f77b237f2a6dd2d8982",
-
"corpus": "malware",
-
"cyclomatic_complexity": 3,
-
"edges": 2,
-
"file_sha256": "7b01c7c835552b17f17ad85b8f900c006dd8811d708781b5f49f231448aaccd3",
-
"file_tlsh": "42E34A10F3D341F7DC9608F219B6B22F9F791E023124DFA987981F57ADB5246A2B981C",
-
"instructions": 29,
-
"invalid_instructions": 0,
-
"mode": "pe:x86",
-
"offset": 49711,
-
"size": 118,
-
"tags": [
-
"malware:emotet",
-
"malware:loader"
-
],
-
"trait": "55 8b ec 83 ec 1c 83 65 ?? ?? 33 d2 c7 45 ?? ?? ?? ?? ?? c7 45 ?? ?? ?? ?? ?? c7 45 ?? ?? ?? ?? ?? c7 45 ?? ?? ?? ?? ?? 81 45 ?? ?? ?? ?? ?? 81 75 ?? ?? ?? ?? ?? c7 45 ?? ?? ?? ?? ?? 8b 45 ?? 6a 3f 59 f7 f1 6a 1c 89 45 ?? 33 d2 8b 45 ?? 59 f7 f1 89 45 ?? 81 75 ?? ?? ?? ?? ?? c7 45 ?? ?? ?? ?? ?? 81 45 ?? ?? ?? ?? ?? 81 75 ?? ?? ?? ?? ?? ff 4d ?? 75 21",
-
"trait_entropy": 3.9699645042419434,
-
"trait_sha256": "7b04c2dbcc3cf23abfdd457b592b4517e4d98b5c83e692c836cde5b91899dd68",
-
"type": "block"
-
}
-

If you have terabytes of executable files, we can leverage the power of parallel to generate traits for us.

-
make traits source=samples/malware/pe/x32/ dest=dist/ type=malware format=pe arch=x86 threads=4
-
make traits-combine source=dist/ dest=dist/ type=malware format=pe arch=x86 threads=4
-

It also allows you to name your type of dataset, i.e. goodware/malware/riskware/pua etc...

-

With binlex it is up to you to remove goodware traits from your extracted traits.

-

There have been many questions about removing "library code", there is a make target shown below to help you with this.

-
make traits-clean remove=goodware.traits source=sample.traits dest=malware.traits
-

With binlex the power is in your hands, "With great power comes great responsibility", it is up to you!

-

Plugins:

-

There has been some interest in making IDA, Ghidra and Cutter plugins for binlex.

-

This is something that will be started soon as we finish the HTTP API endpoints.

-

This README.md will be updated when they are ready to use.

-

General Usage Information:

-

Binlex is designed to do one thing and one thing only, extract genetic traits from executable code in files. This means it is up to you "the researcher" / "the data scientist" to determine which traits are good and which traits are bad. To accomplish this, you need to use your own fitness function. I encourage you to read about genetic programming to gain a better understanding of this in practice. Perhaps watching this introductory video will help your understanding.

-

Again, it's up to you to implement your own algorithms for detection based on the genetic traits you extract.

-

-Trait Format

-

Traits will contain binary code represented in hexadecimal form and will use ?? as wild cards for memory operands or other operands subject to change.

-

They will also contain additional properties about the trait including its offset, edges, blocks, cyclomatic_complexity, average_instruction_per_block, bytes, trait, trait_sha256, bytes_sha256, trait_entropy, bytes_entropy, type, size, invalid_instructions and instructions.

-
{
-
"average_instructions_per_block": 6,
-
"blocks": 1,
-
"bytes": "8b 45 08 a3 10 52 02 10 8b 45 f8 e8 fb d7 00 00 85 c0 74 0d",
-
"bytes_entropy": 3.9219279289245605,
-
"bytes_sha256": "435cb166701006282e457d441ca793e795e38790cacc5b250d4bc418a28961c3",
-
"corpus": "malware",
-
"cyclomatic_complexity": 3,
-
"edges": 2,
-
"file_sha256": "7b01c7c835552b17f17ad85b8f900c006dd8811d708781b5f49f231448aaccd3",
-
"file_tlsh": "42E34A10F3D341F7DC9608F219B6B22F9F791E023124DFA987981F57ADB5246A2B981C",
-
"instructions": 6,
-
"invalid_instructions": 0,
-
"mode": "pe:x86",
-
"offset": 49829,
-
"size": 20,
-
"tags": [
-
"malware:emotet",
-
"malware:loader"
-
],
-
"trait": "8b 45 ?? a3 ?? ?? ?? ?? 8b 45 ?? e8 fb d7 00 00 85 c0 74 0d",
-
"trait_entropy": 3.3787841796875,
-
"trait_sha256": "fe3b057a28b40a02ac9dd2db6c3208f96f7151fb912fb3c562a7b4581bb7f7a0",
-
"type": "block"
-
}
-

-Documentation

-

Public documentation on binlex can be viewed here.

-

-Building Docs

-

You can access the C++ API Documentation and everything else by building the documents using doxygen.

-
make docs threads=4
-

The documents will be available at build/docs/html/index.html.

-

-C++ API Example Code

-

The power of detection is in your hands, binlex is a framework, leverage the C++ API.

-
#include <binlex/pe.h>
-
#include <binlex/decompiler.h>
-
-
using namespace binlex;
-
-
int main(int argc, char **argv){
-
PE pe32;
-
if (pe32.Setup(MACHINE_TYPES::IMAGE_FILE_MACHINE_I386) == false){
-
return EXIT_FAILURE;
-
}
-
if (pe32.ReadFile(argv[1]) == false){
-
return EXIT_FAILURE;
-
}
-
Decompiler decompiler(pe32);
-
decompiler.Setup(CS_ARCH_X86, CS_MODE_32);
-
for (uint32_t i = 0; i < pe32.total_exec_sections; i++){
-
decompiler.AppendQueue(pe32.sections[i].functions, DECOMPILER_OPERAND_TYPE_FUNCTION, i);
-
decompiler.Decompile(pe32.sections[i].data, pe32.sections[i].size, pe32.sections[i].offset, i);
-
}
-
decompiler.WriteTraits();
-
return 0;
-
}
-

-Python API Example Code

-

The power of detection is in your hands, binlex is a framework, leverage the C++ API.

-
#!/usr/bin/env python
-
-
import pybinlex
-
from hashlib import sha256
-
-
pe = pybinlex.PE()
-
pe.setup(pybinlex.MACHINE_TYPES.IMAGE_FILE_MACHINE_I386)
-
result = pe.read_file('../tests/pe/pe.x86')
-
if result is False:
-
print("[x] failed to read pe.x86")
-
sys.exit(1)
-
pe_sections = pe.get_sections()
-
-
decompiler = pybinlex.Decompiler(pe)
-
decompiler.setup(pybinlex.cs_arch.CS_ARCH_X86, pybinlex.cs_mode.CS_MODE_32)
-
for i in range(0, len(pe_sections)):
-
decompiler.append_queue(pe_sections[i]['functions'], pybinlex.DECOMPILER_OPERAND_TYPE.DECOMPILER_OPERAND_TYPE_FUNCTION, i)
-
decompiler.decompile(pe_sections[i]['data'], pe_sections[i]['offset'], i)
-
traits = decompiler.get_traits()
-
print(json.dumps(traits, indent=4))
-

We hope this encourages people to build their own detection solutions based on binary genetic traits.

-

-Tips

-
    -
  • If you are hunting be sure to use jq to improve your searches
  • -
  • Does not support PE files that are VB6 if you run against these you will get errors
  • -
  • Don't mix packed and unpacked malware or you will taint your dataset (seen this in academics all the time)
  • -
  • When comparing samples to identify if their code is similar, DO NOT mix their architectures in your comparison
      -
    • Comparing CIL byte-code or .NET to x86 machine code may yield interesting properties, but they are invalid
    • -
    -
  • -
  • Verify the samples you are collecting into a group using skilled analysts
  • -
  • These traits are best used with a hybrid approach (supervised)
  • -
-

-Example Fitness Model

-

Traits will be compared amongst their common malware family, any traits not common to all samples will be discarded.

-

Once completed, all remaining traits will be compared to traits from a goodware set, any traits that match the goodware set will be discarded.

-

To further differ the traits from other malware families, the remaining population will be compared to other malware families, any that match will be discarded.

-

The remaining population of traits will be unique to the malware family tested and not legitimate binaries or other malware families.

-

This fitness model allows for accurate classification of the tested malware family.

-

-Future Work

-
    -
  • Java byte-code Support raw:jvm, java:jvm
  • -
  • Python byte-code Support raw:pyc, python:pyc
  • -
  • More API Endpoints
  • -
  • Cutter, Ghidra and IDA Plugins
  • -
  • Mac-O Support macho:x86_64, macho:x86
  • -
-

-Contributing

-

If you wish to contribute to Binlex DM me on Twitter here.

-

You can also join our Discord here.

-

Currently looking for help on:

    -
  • MacOS Developer (Parse Mach-O)
  • -
  • Plugin Developers (Python)
  • -
  • Front-End Developers (Python)
  • -
-
-
-
-
Definition: decompiler.h:52
-
Definition: pe.h:28
-
BINLEX_EXPORT bool Setup(MACHINE_TYPES input_mode)
-
bool ReadFile(const char *file_path)
-
the binlex namespace
- - - - diff --git a/docs/html/jquery.js b/docs/html/jquery.js deleted file mode 100644 index 103c32d7..00000000 --- a/docs/html/jquery.js +++ /dev/null @@ -1,35 +0,0 @@ -/*! jQuery v3.4.1 | (c) JS Foundation and other contributors | jquery.org/license */ -!function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(C,e){"use strict";var t=[],E=C.document,r=Object.getPrototypeOf,s=t.slice,g=t.concat,u=t.push,i=t.indexOf,n={},o=n.toString,v=n.hasOwnProperty,a=v.toString,l=a.call(Object),y={},m=function(e){return"function"==typeof e&&"number"!=typeof e.nodeType},x=function(e){return null!=e&&e===e.window},c={type:!0,src:!0,nonce:!0,noModule:!0};function b(e,t,n){var r,i,o=(n=n||E).createElement("script");if(o.text=e,t)for(r in c)(i=t[r]||t.getAttribute&&t.getAttribute(r))&&o.setAttribute(r,i);n.head.appendChild(o).parentNode.removeChild(o)}function w(e){return null==e?e+"":"object"==typeof e||"function"==typeof e?n[o.call(e)]||"object":typeof e}var f="3.4.1",k=function(e,t){return new k.fn.init(e,t)},p=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;function d(e){var t=!!e&&"length"in e&&e.length,n=w(e);return!m(e)&&!x(e)&&("array"===n||0===t||"number"==typeof t&&0+~]|"+M+")"+M+"*"),U=new RegExp(M+"|>"),X=new RegExp($),V=new RegExp("^"+I+"$"),G={ID:new RegExp("^#("+I+")"),CLASS:new RegExp("^\\.("+I+")"),TAG:new RegExp("^("+I+"|[*])"),ATTR:new RegExp("^"+W),PSEUDO:new RegExp("^"+$),CHILD:new RegExp("^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\("+M+"*(even|odd|(([+-]|)(\\d*)n|)"+M+"*(?:([+-]|)"+M+"*(\\d+)|))"+M+"*\\)|)","i"),bool:new RegExp("^(?:"+R+")$","i"),needsContext:new RegExp("^"+M+"*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\("+M+"*((?:-\\d)?\\d*)"+M+"*\\)|)(?=[^-]|$)","i")},Y=/HTML$/i,Q=/^(?:input|select|textarea|button)$/i,J=/^h\d$/i,K=/^[^{]+\{\s*\[native \w/,Z=/^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,ee=/[+~]/,te=new RegExp("\\\\([\\da-f]{1,6}"+M+"?|("+M+")|.)","ig"),ne=function(e,t,n){var r="0x"+t-65536;return r!=r||n?t:r<0?String.fromCharCode(r+65536):String.fromCharCode(r>>10|55296,1023&r|56320)},re=/([\0-\x1f\x7f]|^-?\d)|^-$|[^\0-\x1f\x7f-\uFFFF\w-]/g,ie=function(e,t){return t?"\0"===e?"\ufffd":e.slice(0,-1)+"\\"+e.charCodeAt(e.length-1).toString(16)+" ":"\\"+e},oe=function(){T()},ae=be(function(e){return!0===e.disabled&&"fieldset"===e.nodeName.toLowerCase()},{dir:"parentNode",next:"legend"});try{H.apply(t=O.call(m.childNodes),m.childNodes),t[m.childNodes.length].nodeType}catch(e){H={apply:t.length?function(e,t){L.apply(e,O.call(t))}:function(e,t){var n=e.length,r=0;while(e[n++]=t[r++]);e.length=n-1}}}function se(t,e,n,r){var i,o,a,s,u,l,c,f=e&&e.ownerDocument,p=e?e.nodeType:9;if(n=n||[],"string"!=typeof t||!t||1!==p&&9!==p&&11!==p)return n;if(!r&&((e?e.ownerDocument||e:m)!==C&&T(e),e=e||C,E)){if(11!==p&&(u=Z.exec(t)))if(i=u[1]){if(9===p){if(!(a=e.getElementById(i)))return n;if(a.id===i)return n.push(a),n}else if(f&&(a=f.getElementById(i))&&y(e,a)&&a.id===i)return n.push(a),n}else{if(u[2])return H.apply(n,e.getElementsByTagName(t)),n;if((i=u[3])&&d.getElementsByClassName&&e.getElementsByClassName)return H.apply(n,e.getElementsByClassName(i)),n}if(d.qsa&&!A[t+" "]&&(!v||!v.test(t))&&(1!==p||"object"!==e.nodeName.toLowerCase())){if(c=t,f=e,1===p&&U.test(t)){(s=e.getAttribute("id"))?s=s.replace(re,ie):e.setAttribute("id",s=k),o=(l=h(t)).length;while(o--)l[o]="#"+s+" "+xe(l[o]);c=l.join(","),f=ee.test(t)&&ye(e.parentNode)||e}try{return H.apply(n,f.querySelectorAll(c)),n}catch(e){A(t,!0)}finally{s===k&&e.removeAttribute("id")}}}return g(t.replace(B,"$1"),e,n,r)}function ue(){var r=[];return function e(t,n){return r.push(t+" ")>b.cacheLength&&delete e[r.shift()],e[t+" "]=n}}function le(e){return e[k]=!0,e}function ce(e){var t=C.createElement("fieldset");try{return!!e(t)}catch(e){return!1}finally{t.parentNode&&t.parentNode.removeChild(t),t=null}}function fe(e,t){var n=e.split("|"),r=n.length;while(r--)b.attrHandle[n[r]]=t}function pe(e,t){var n=t&&e,r=n&&1===e.nodeType&&1===t.nodeType&&e.sourceIndex-t.sourceIndex;if(r)return r;if(n)while(n=n.nextSibling)if(n===t)return-1;return e?1:-1}function de(t){return function(e){return"input"===e.nodeName.toLowerCase()&&e.type===t}}function he(n){return function(e){var t=e.nodeName.toLowerCase();return("input"===t||"button"===t)&&e.type===n}}function ge(t){return function(e){return"form"in e?e.parentNode&&!1===e.disabled?"label"in e?"label"in e.parentNode?e.parentNode.disabled===t:e.disabled===t:e.isDisabled===t||e.isDisabled!==!t&&ae(e)===t:e.disabled===t:"label"in e&&e.disabled===t}}function ve(a){return le(function(o){return o=+o,le(function(e,t){var n,r=a([],e.length,o),i=r.length;while(i--)e[n=r[i]]&&(e[n]=!(t[n]=e[n]))})})}function ye(e){return e&&"undefined"!=typeof e.getElementsByTagName&&e}for(e in d=se.support={},i=se.isXML=function(e){var t=e.namespaceURI,n=(e.ownerDocument||e).documentElement;return!Y.test(t||n&&n.nodeName||"HTML")},T=se.setDocument=function(e){var t,n,r=e?e.ownerDocument||e:m;return r!==C&&9===r.nodeType&&r.documentElement&&(a=(C=r).documentElement,E=!i(C),m!==C&&(n=C.defaultView)&&n.top!==n&&(n.addEventListener?n.addEventListener("unload",oe,!1):n.attachEvent&&n.attachEvent("onunload",oe)),d.attributes=ce(function(e){return e.className="i",!e.getAttribute("className")}),d.getElementsByTagName=ce(function(e){return e.appendChild(C.createComment("")),!e.getElementsByTagName("*").length}),d.getElementsByClassName=K.test(C.getElementsByClassName),d.getById=ce(function(e){return a.appendChild(e).id=k,!C.getElementsByName||!C.getElementsByName(k).length}),d.getById?(b.filter.ID=function(e){var t=e.replace(te,ne);return function(e){return e.getAttribute("id")===t}},b.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&E){var n=t.getElementById(e);return n?[n]:[]}}):(b.filter.ID=function(e){var n=e.replace(te,ne);return function(e){var t="undefined"!=typeof e.getAttributeNode&&e.getAttributeNode("id");return t&&t.value===n}},b.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&E){var n,r,i,o=t.getElementById(e);if(o){if((n=o.getAttributeNode("id"))&&n.value===e)return[o];i=t.getElementsByName(e),r=0;while(o=i[r++])if((n=o.getAttributeNode("id"))&&n.value===e)return[o]}return[]}}),b.find.TAG=d.getElementsByTagName?function(e,t){return"undefined"!=typeof t.getElementsByTagName?t.getElementsByTagName(e):d.qsa?t.querySelectorAll(e):void 0}:function(e,t){var n,r=[],i=0,o=t.getElementsByTagName(e);if("*"===e){while(n=o[i++])1===n.nodeType&&r.push(n);return r}return o},b.find.CLASS=d.getElementsByClassName&&function(e,t){if("undefined"!=typeof t.getElementsByClassName&&E)return t.getElementsByClassName(e)},s=[],v=[],(d.qsa=K.test(C.querySelectorAll))&&(ce(function(e){a.appendChild(e).innerHTML="",e.querySelectorAll("[msallowcapture^='']").length&&v.push("[*^$]="+M+"*(?:''|\"\")"),e.querySelectorAll("[selected]").length||v.push("\\["+M+"*(?:value|"+R+")"),e.querySelectorAll("[id~="+k+"-]").length||v.push("~="),e.querySelectorAll(":checked").length||v.push(":checked"),e.querySelectorAll("a#"+k+"+*").length||v.push(".#.+[+~]")}),ce(function(e){e.innerHTML="";var t=C.createElement("input");t.setAttribute("type","hidden"),e.appendChild(t).setAttribute("name","D"),e.querySelectorAll("[name=d]").length&&v.push("name"+M+"*[*^$|!~]?="),2!==e.querySelectorAll(":enabled").length&&v.push(":enabled",":disabled"),a.appendChild(e).disabled=!0,2!==e.querySelectorAll(":disabled").length&&v.push(":enabled",":disabled"),e.querySelectorAll("*,:x"),v.push(",.*:")})),(d.matchesSelector=K.test(c=a.matches||a.webkitMatchesSelector||a.mozMatchesSelector||a.oMatchesSelector||a.msMatchesSelector))&&ce(function(e){d.disconnectedMatch=c.call(e,"*"),c.call(e,"[s!='']:x"),s.push("!=",$)}),v=v.length&&new RegExp(v.join("|")),s=s.length&&new RegExp(s.join("|")),t=K.test(a.compareDocumentPosition),y=t||K.test(a.contains)?function(e,t){var n=9===e.nodeType?e.documentElement:e,r=t&&t.parentNode;return e===r||!(!r||1!==r.nodeType||!(n.contains?n.contains(r):e.compareDocumentPosition&&16&e.compareDocumentPosition(r)))}:function(e,t){if(t)while(t=t.parentNode)if(t===e)return!0;return!1},D=t?function(e,t){if(e===t)return l=!0,0;var n=!e.compareDocumentPosition-!t.compareDocumentPosition;return n||(1&(n=(e.ownerDocument||e)===(t.ownerDocument||t)?e.compareDocumentPosition(t):1)||!d.sortDetached&&t.compareDocumentPosition(e)===n?e===C||e.ownerDocument===m&&y(m,e)?-1:t===C||t.ownerDocument===m&&y(m,t)?1:u?P(u,e)-P(u,t):0:4&n?-1:1)}:function(e,t){if(e===t)return l=!0,0;var n,r=0,i=e.parentNode,o=t.parentNode,a=[e],s=[t];if(!i||!o)return e===C?-1:t===C?1:i?-1:o?1:u?P(u,e)-P(u,t):0;if(i===o)return pe(e,t);n=e;while(n=n.parentNode)a.unshift(n);n=t;while(n=n.parentNode)s.unshift(n);while(a[r]===s[r])r++;return r?pe(a[r],s[r]):a[r]===m?-1:s[r]===m?1:0}),C},se.matches=function(e,t){return se(e,null,null,t)},se.matchesSelector=function(e,t){if((e.ownerDocument||e)!==C&&T(e),d.matchesSelector&&E&&!A[t+" "]&&(!s||!s.test(t))&&(!v||!v.test(t)))try{var n=c.call(e,t);if(n||d.disconnectedMatch||e.document&&11!==e.document.nodeType)return n}catch(e){A(t,!0)}return 0":{dir:"parentNode",first:!0}," ":{dir:"parentNode"},"+":{dir:"previousSibling",first:!0},"~":{dir:"previousSibling"}},preFilter:{ATTR:function(e){return e[1]=e[1].replace(te,ne),e[3]=(e[3]||e[4]||e[5]||"").replace(te,ne),"~="===e[2]&&(e[3]=" "+e[3]+" "),e.slice(0,4)},CHILD:function(e){return e[1]=e[1].toLowerCase(),"nth"===e[1].slice(0,3)?(e[3]||se.error(e[0]),e[4]=+(e[4]?e[5]+(e[6]||1):2*("even"===e[3]||"odd"===e[3])),e[5]=+(e[7]+e[8]||"odd"===e[3])):e[3]&&se.error(e[0]),e},PSEUDO:function(e){var t,n=!e[6]&&e[2];return G.CHILD.test(e[0])?null:(e[3]?e[2]=e[4]||e[5]||"":n&&X.test(n)&&(t=h(n,!0))&&(t=n.indexOf(")",n.length-t)-n.length)&&(e[0]=e[0].slice(0,t),e[2]=n.slice(0,t)),e.slice(0,3))}},filter:{TAG:function(e){var t=e.replace(te,ne).toLowerCase();return"*"===e?function(){return!0}:function(e){return e.nodeName&&e.nodeName.toLowerCase()===t}},CLASS:function(e){var t=p[e+" "];return t||(t=new RegExp("(^|"+M+")"+e+"("+M+"|$)"))&&p(e,function(e){return t.test("string"==typeof e.className&&e.className||"undefined"!=typeof e.getAttribute&&e.getAttribute("class")||"")})},ATTR:function(n,r,i){return function(e){var t=se.attr(e,n);return null==t?"!="===r:!r||(t+="","="===r?t===i:"!="===r?t!==i:"^="===r?i&&0===t.indexOf(i):"*="===r?i&&-1:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i;function j(e,n,r){return m(n)?k.grep(e,function(e,t){return!!n.call(e,t,e)!==r}):n.nodeType?k.grep(e,function(e){return e===n!==r}):"string"!=typeof n?k.grep(e,function(e){return-1)[^>]*|#([\w-]+))$/;(k.fn.init=function(e,t,n){var r,i;if(!e)return this;if(n=n||q,"string"==typeof e){if(!(r="<"===e[0]&&">"===e[e.length-1]&&3<=e.length?[null,e,null]:L.exec(e))||!r[1]&&t)return!t||t.jquery?(t||n).find(e):this.constructor(t).find(e);if(r[1]){if(t=t instanceof k?t[0]:t,k.merge(this,k.parseHTML(r[1],t&&t.nodeType?t.ownerDocument||t:E,!0)),D.test(r[1])&&k.isPlainObject(t))for(r in t)m(this[r])?this[r](t[r]):this.attr(r,t[r]);return this}return(i=E.getElementById(r[2]))&&(this[0]=i,this.length=1),this}return e.nodeType?(this[0]=e,this.length=1,this):m(e)?void 0!==n.ready?n.ready(e):e(k):k.makeArray(e,this)}).prototype=k.fn,q=k(E);var H=/^(?:parents|prev(?:Until|All))/,O={children:!0,contents:!0,next:!0,prev:!0};function P(e,t){while((e=e[t])&&1!==e.nodeType);return e}k.fn.extend({has:function(e){var t=k(e,this),n=t.length;return this.filter(function(){for(var e=0;e\x20\t\r\n\f]*)/i,he=/^$|^module$|\/(?:java|ecma)script/i,ge={option:[1,""],thead:[1,"","
"],col:[2,"","
"],tr:[2,"","
"],td:[3,"","
"],_default:[0,"",""]};function ve(e,t){var n;return n="undefined"!=typeof e.getElementsByTagName?e.getElementsByTagName(t||"*"):"undefined"!=typeof e.querySelectorAll?e.querySelectorAll(t||"*"):[],void 0===t||t&&A(e,t)?k.merge([e],n):n}function ye(e,t){for(var n=0,r=e.length;nx",y.noCloneChecked=!!me.cloneNode(!0).lastChild.defaultValue;var Te=/^key/,Ce=/^(?:mouse|pointer|contextmenu|drag|drop)|click/,Ee=/^([^.]*)(?:\.(.+)|)/;function ke(){return!0}function Se(){return!1}function Ne(e,t){return e===function(){try{return E.activeElement}catch(e){}}()==("focus"===t)}function Ae(e,t,n,r,i,o){var a,s;if("object"==typeof t){for(s in"string"!=typeof n&&(r=r||n,n=void 0),t)Ae(e,s,n,r,t[s],o);return e}if(null==r&&null==i?(i=n,r=n=void 0):null==i&&("string"==typeof n?(i=r,r=void 0):(i=r,r=n,n=void 0)),!1===i)i=Se;else if(!i)return e;return 1===o&&(a=i,(i=function(e){return k().off(e),a.apply(this,arguments)}).guid=a.guid||(a.guid=k.guid++)),e.each(function(){k.event.add(this,t,i,r,n)})}function De(e,i,o){o?(Q.set(e,i,!1),k.event.add(e,i,{namespace:!1,handler:function(e){var t,n,r=Q.get(this,i);if(1&e.isTrigger&&this[i]){if(r.length)(k.event.special[i]||{}).delegateType&&e.stopPropagation();else if(r=s.call(arguments),Q.set(this,i,r),t=o(this,i),this[i](),r!==(n=Q.get(this,i))||t?Q.set(this,i,!1):n={},r!==n)return e.stopImmediatePropagation(),e.preventDefault(),n.value}else r.length&&(Q.set(this,i,{value:k.event.trigger(k.extend(r[0],k.Event.prototype),r.slice(1),this)}),e.stopImmediatePropagation())}})):void 0===Q.get(e,i)&&k.event.add(e,i,ke)}k.event={global:{},add:function(t,e,n,r,i){var o,a,s,u,l,c,f,p,d,h,g,v=Q.get(t);if(v){n.handler&&(n=(o=n).handler,i=o.selector),i&&k.find.matchesSelector(ie,i),n.guid||(n.guid=k.guid++),(u=v.events)||(u=v.events={}),(a=v.handle)||(a=v.handle=function(e){return"undefined"!=typeof k&&k.event.triggered!==e.type?k.event.dispatch.apply(t,arguments):void 0}),l=(e=(e||"").match(R)||[""]).length;while(l--)d=g=(s=Ee.exec(e[l])||[])[1],h=(s[2]||"").split(".").sort(),d&&(f=k.event.special[d]||{},d=(i?f.delegateType:f.bindType)||d,f=k.event.special[d]||{},c=k.extend({type:d,origType:g,data:r,handler:n,guid:n.guid,selector:i,needsContext:i&&k.expr.match.needsContext.test(i),namespace:h.join(".")},o),(p=u[d])||((p=u[d]=[]).delegateCount=0,f.setup&&!1!==f.setup.call(t,r,h,a)||t.addEventListener&&t.addEventListener(d,a)),f.add&&(f.add.call(t,c),c.handler.guid||(c.handler.guid=n.guid)),i?p.splice(p.delegateCount++,0,c):p.push(c),k.event.global[d]=!0)}},remove:function(e,t,n,r,i){var o,a,s,u,l,c,f,p,d,h,g,v=Q.hasData(e)&&Q.get(e);if(v&&(u=v.events)){l=(t=(t||"").match(R)||[""]).length;while(l--)if(d=g=(s=Ee.exec(t[l])||[])[1],h=(s[2]||"").split(".").sort(),d){f=k.event.special[d]||{},p=u[d=(r?f.delegateType:f.bindType)||d]||[],s=s[2]&&new RegExp("(^|\\.)"+h.join("\\.(?:.*\\.|)")+"(\\.|$)"),a=o=p.length;while(o--)c=p[o],!i&&g!==c.origType||n&&n.guid!==c.guid||s&&!s.test(c.namespace)||r&&r!==c.selector&&("**"!==r||!c.selector)||(p.splice(o,1),c.selector&&p.delegateCount--,f.remove&&f.remove.call(e,c));a&&!p.length&&(f.teardown&&!1!==f.teardown.call(e,h,v.handle)||k.removeEvent(e,d,v.handle),delete u[d])}else for(d in u)k.event.remove(e,d+t[l],n,r,!0);k.isEmptyObject(u)&&Q.remove(e,"handle events")}},dispatch:function(e){var t,n,r,i,o,a,s=k.event.fix(e),u=new Array(arguments.length),l=(Q.get(this,"events")||{})[s.type]||[],c=k.event.special[s.type]||{};for(u[0]=s,t=1;t\x20\t\r\n\f]*)[^>]*)\/>/gi,qe=/\s*$/g;function Oe(e,t){return A(e,"table")&&A(11!==t.nodeType?t:t.firstChild,"tr")&&k(e).children("tbody")[0]||e}function Pe(e){return e.type=(null!==e.getAttribute("type"))+"/"+e.type,e}function Re(e){return"true/"===(e.type||"").slice(0,5)?e.type=e.type.slice(5):e.removeAttribute("type"),e}function Me(e,t){var n,r,i,o,a,s,u,l;if(1===t.nodeType){if(Q.hasData(e)&&(o=Q.access(e),a=Q.set(t,o),l=o.events))for(i in delete a.handle,a.events={},l)for(n=0,r=l[i].length;n")},clone:function(e,t,n){var r,i,o,a,s,u,l,c=e.cloneNode(!0),f=oe(e);if(!(y.noCloneChecked||1!==e.nodeType&&11!==e.nodeType||k.isXMLDoc(e)))for(a=ve(c),r=0,i=(o=ve(e)).length;r").attr(n.scriptAttrs||{}).prop({charset:n.scriptCharset,src:n.url}).on("load error",i=function(e){r.remove(),i=null,e&&t("error"===e.type?404:200,e.type)}),E.head.appendChild(r[0])},abort:function(){i&&i()}}});var Vt,Gt=[],Yt=/(=)\?(?=&|$)|\?\?/;k.ajaxSetup({jsonp:"callback",jsonpCallback:function(){var e=Gt.pop()||k.expando+"_"+kt++;return this[e]=!0,e}}),k.ajaxPrefilter("json jsonp",function(e,t,n){var r,i,o,a=!1!==e.jsonp&&(Yt.test(e.url)?"url":"string"==typeof e.data&&0===(e.contentType||"").indexOf("application/x-www-form-urlencoded")&&Yt.test(e.data)&&"data");if(a||"jsonp"===e.dataTypes[0])return r=e.jsonpCallback=m(e.jsonpCallback)?e.jsonpCallback():e.jsonpCallback,a?e[a]=e[a].replace(Yt,"$1"+r):!1!==e.jsonp&&(e.url+=(St.test(e.url)?"&":"?")+e.jsonp+"="+r),e.converters["script json"]=function(){return o||k.error(r+" was not called"),o[0]},e.dataTypes[0]="json",i=C[r],C[r]=function(){o=arguments},n.always(function(){void 0===i?k(C).removeProp(r):C[r]=i,e[r]&&(e.jsonpCallback=t.jsonpCallback,Gt.push(r)),o&&m(i)&&i(o[0]),o=i=void 0}),"script"}),y.createHTMLDocument=((Vt=E.implementation.createHTMLDocument("").body).innerHTML="
",2===Vt.childNodes.length),k.parseHTML=function(e,t,n){return"string"!=typeof e?[]:("boolean"==typeof t&&(n=t,t=!1),t||(y.createHTMLDocument?((r=(t=E.implementation.createHTMLDocument("")).createElement("base")).href=E.location.href,t.head.appendChild(r)):t=E),o=!n&&[],(i=D.exec(e))?[t.createElement(i[1])]:(i=we([e],t,o),o&&o.length&&k(o).remove(),k.merge([],i.childNodes)));var r,i,o},k.fn.load=function(e,t,n){var r,i,o,a=this,s=e.indexOf(" ");return-1").append(k.parseHTML(e)).find(r):e)}).always(n&&function(e,t){a.each(function(){n.apply(this,o||[e.responseText,t,e])})}),this},k.each(["ajaxStart","ajaxStop","ajaxComplete","ajaxError","ajaxSuccess","ajaxSend"],function(e,t){k.fn[t]=function(e){return this.on(t,e)}}),k.expr.pseudos.animated=function(t){return k.grep(k.timers,function(e){return t===e.elem}).length},k.offset={setOffset:function(e,t,n){var r,i,o,a,s,u,l=k.css(e,"position"),c=k(e),f={};"static"===l&&(e.style.position="relative"),s=c.offset(),o=k.css(e,"top"),u=k.css(e,"left"),("absolute"===l||"fixed"===l)&&-1<(o+u).indexOf("auto")?(a=(r=c.position()).top,i=r.left):(a=parseFloat(o)||0,i=parseFloat(u)||0),m(t)&&(t=t.call(e,n,k.extend({},s))),null!=t.top&&(f.top=t.top-s.top+a),null!=t.left&&(f.left=t.left-s.left+i),"using"in t?t.using.call(e,f):c.css(f)}},k.fn.extend({offset:function(t){if(arguments.length)return void 0===t?this:this.each(function(e){k.offset.setOffset(this,t,e)});var e,n,r=this[0];return r?r.getClientRects().length?(e=r.getBoundingClientRect(),n=r.ownerDocument.defaultView,{top:e.top+n.pageYOffset,left:e.left+n.pageXOffset}):{top:0,left:0}:void 0},position:function(){if(this[0]){var e,t,n,r=this[0],i={top:0,left:0};if("fixed"===k.css(r,"position"))t=r.getBoundingClientRect();else{t=this.offset(),n=r.ownerDocument,e=r.offsetParent||n.documentElement;while(e&&(e===n.body||e===n.documentElement)&&"static"===k.css(e,"position"))e=e.parentNode;e&&e!==r&&1===e.nodeType&&((i=k(e).offset()).top+=k.css(e,"borderTopWidth",!0),i.left+=k.css(e,"borderLeftWidth",!0))}return{top:t.top-i.top-k.css(r,"marginTop",!0),left:t.left-i.left-k.css(r,"marginLeft",!0)}}},offsetParent:function(){return this.map(function(){var e=this.offsetParent;while(e&&"static"===k.css(e,"position"))e=e.offsetParent;return e||ie})}}),k.each({scrollLeft:"pageXOffset",scrollTop:"pageYOffset"},function(t,i){var o="pageYOffset"===i;k.fn[t]=function(e){return _(this,function(e,t,n){var r;if(x(e)?r=e:9===e.nodeType&&(r=e.defaultView),void 0===n)return r?r[i]:e[t];r?r.scrollTo(o?r.pageXOffset:n,o?n:r.pageYOffset):e[t]=n},t,e,arguments.length)}}),k.each(["top","left"],function(e,n){k.cssHooks[n]=ze(y.pixelPosition,function(e,t){if(t)return t=_e(e,n),$e.test(t)?k(e).position()[n]+"px":t})}),k.each({Height:"height",Width:"width"},function(a,s){k.each({padding:"inner"+a,content:s,"":"outer"+a},function(r,o){k.fn[o]=function(e,t){var n=arguments.length&&(r||"boolean"!=typeof e),i=r||(!0===e||!0===t?"margin":"border");return _(this,function(e,t,n){var r;return x(e)?0===o.indexOf("outer")?e["inner"+a]:e.document.documentElement["client"+a]:9===e.nodeType?(r=e.documentElement,Math.max(e.body["scroll"+a],r["scroll"+a],e.body["offset"+a],r["offset"+a],r["client"+a])):void 0===n?k.css(e,t,i):k.style(e,t,n,i)},s,n?e:void 0,n)}})}),k.each("blur focus focusin focusout resize scroll click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup contextmenu".split(" "),function(e,n){k.fn[n]=function(e,t){return 0a;a++)for(i in o[a])n=o[a][i],o[a].hasOwnProperty(i)&&void 0!==n&&(e[i]=t.isPlainObject(n)?t.isPlainObject(e[i])?t.widget.extend({},e[i],n):t.widget.extend({},n):n);return e},t.widget.bridge=function(e,i){var n=i.prototype.widgetFullName||e;t.fn[e]=function(o){var a="string"==typeof o,r=s.call(arguments,1),h=this;return a?this.length||"instance"!==o?this.each(function(){var i,s=t.data(this,n);return"instance"===o?(h=s,!1):s?t.isFunction(s[o])&&"_"!==o.charAt(0)?(i=s[o].apply(s,r),i!==s&&void 0!==i?(h=i&&i.jquery?h.pushStack(i.get()):i,!1):void 0):t.error("no such method '"+o+"' for "+e+" widget instance"):t.error("cannot call methods on "+e+" prior to initialization; "+"attempted to call method '"+o+"'")}):h=void 0:(r.length&&(o=t.widget.extend.apply(null,[o].concat(r))),this.each(function(){var e=t.data(this,n);e?(e.option(o||{}),e._init&&e._init()):t.data(this,n,new i(o,this))})),h}},t.Widget=function(){},t.Widget._childConstructors=[],t.Widget.prototype={widgetName:"widget",widgetEventPrefix:"",defaultElement:"
",options:{classes:{},disabled:!1,create:null},_createWidget:function(e,s){s=t(s||this.defaultElement||this)[0],this.element=t(s),this.uuid=i++,this.eventNamespace="."+this.widgetName+this.uuid,this.bindings=t(),this.hoverable=t(),this.focusable=t(),this.classesElementLookup={},s!==this&&(t.data(s,this.widgetFullName,this),this._on(!0,this.element,{remove:function(t){t.target===s&&this.destroy()}}),this.document=t(s.style?s.ownerDocument:s.document||s),this.window=t(this.document[0].defaultView||this.document[0].parentWindow)),this.options=t.widget.extend({},this.options,this._getCreateOptions(),e),this._create(),this.options.disabled&&this._setOptionDisabled(this.options.disabled),this._trigger("create",null,this._getCreateEventData()),this._init()},_getCreateOptions:function(){return{}},_getCreateEventData:t.noop,_create:t.noop,_init:t.noop,destroy:function(){var e=this;this._destroy(),t.each(this.classesElementLookup,function(t,i){e._removeClass(i,t)}),this.element.off(this.eventNamespace).removeData(this.widgetFullName),this.widget().off(this.eventNamespace).removeAttr("aria-disabled"),this.bindings.off(this.eventNamespace)},_destroy:t.noop,widget:function(){return this.element},option:function(e,i){var s,n,o,a=e;if(0===arguments.length)return t.widget.extend({},this.options);if("string"==typeof e)if(a={},s=e.split("."),e=s.shift(),s.length){for(n=a[e]=t.widget.extend({},this.options[e]),o=0;s.length-1>o;o++)n[s[o]]=n[s[o]]||{},n=n[s[o]];if(e=s.pop(),1===arguments.length)return void 0===n[e]?null:n[e];n[e]=i}else{if(1===arguments.length)return void 0===this.options[e]?null:this.options[e];a[e]=i}return this._setOptions(a),this},_setOptions:function(t){var e;for(e in t)this._setOption(e,t[e]);return this},_setOption:function(t,e){return"classes"===t&&this._setOptionClasses(e),this.options[t]=e,"disabled"===t&&this._setOptionDisabled(e),this},_setOptionClasses:function(e){var i,s,n;for(i in e)n=this.classesElementLookup[i],e[i]!==this.options.classes[i]&&n&&n.length&&(s=t(n.get()),this._removeClass(n,i),s.addClass(this._classes({element:s,keys:i,classes:e,add:!0})))},_setOptionDisabled:function(t){this._toggleClass(this.widget(),this.widgetFullName+"-disabled",null,!!t),t&&(this._removeClass(this.hoverable,null,"ui-state-hover"),this._removeClass(this.focusable,null,"ui-state-focus"))},enable:function(){return this._setOptions({disabled:!1})},disable:function(){return this._setOptions({disabled:!0})},_classes:function(e){function i(i,o){var a,r;for(r=0;i.length>r;r++)a=n.classesElementLookup[i[r]]||t(),a=e.add?t(t.unique(a.get().concat(e.element.get()))):t(a.not(e.element).get()),n.classesElementLookup[i[r]]=a,s.push(i[r]),o&&e.classes[i[r]]&&s.push(e.classes[i[r]])}var s=[],n=this;return e=t.extend({element:this.element,classes:this.options.classes||{}},e),this._on(e.element,{remove:"_untrackClassesElement"}),e.keys&&i(e.keys.match(/\S+/g)||[],!0),e.extra&&i(e.extra.match(/\S+/g)||[]),s.join(" ")},_untrackClassesElement:function(e){var i=this;t.each(i.classesElementLookup,function(s,n){-1!==t.inArray(e.target,n)&&(i.classesElementLookup[s]=t(n.not(e.target).get()))})},_removeClass:function(t,e,i){return this._toggleClass(t,e,i,!1)},_addClass:function(t,e,i){return this._toggleClass(t,e,i,!0)},_toggleClass:function(t,e,i,s){s="boolean"==typeof s?s:i;var n="string"==typeof t||null===t,o={extra:n?e:i,keys:n?t:e,element:n?this.element:t,add:s};return o.element.toggleClass(this._classes(o),s),this},_on:function(e,i,s){var n,o=this;"boolean"!=typeof e&&(s=i,i=e,e=!1),s?(i=n=t(i),this.bindings=this.bindings.add(i)):(s=i,i=this.element,n=this.widget()),t.each(s,function(s,a){function r(){return e||o.options.disabled!==!0&&!t(this).hasClass("ui-state-disabled")?("string"==typeof a?o[a]:a).apply(o,arguments):void 0}"string"!=typeof a&&(r.guid=a.guid=a.guid||r.guid||t.guid++);var h=s.match(/^([\w:-]*)\s*(.*)$/),l=h[1]+o.eventNamespace,c=h[2];c?n.on(l,c,r):i.on(l,r)})},_off:function(e,i){i=(i||"").split(" ").join(this.eventNamespace+" ")+this.eventNamespace,e.off(i).off(i),this.bindings=t(this.bindings.not(e).get()),this.focusable=t(this.focusable.not(e).get()),this.hoverable=t(this.hoverable.not(e).get())},_delay:function(t,e){function i(){return("string"==typeof t?s[t]:t).apply(s,arguments)}var s=this;return setTimeout(i,e||0)},_hoverable:function(e){this.hoverable=this.hoverable.add(e),this._on(e,{mouseenter:function(e){this._addClass(t(e.currentTarget),null,"ui-state-hover")},mouseleave:function(e){this._removeClass(t(e.currentTarget),null,"ui-state-hover")}})},_focusable:function(e){this.focusable=this.focusable.add(e),this._on(e,{focusin:function(e){this._addClass(t(e.currentTarget),null,"ui-state-focus")},focusout:function(e){this._removeClass(t(e.currentTarget),null,"ui-state-focus")}})},_trigger:function(e,i,s){var n,o,a=this.options[e];if(s=s||{},i=t.Event(i),i.type=(e===this.widgetEventPrefix?e:this.widgetEventPrefix+e).toLowerCase(),i.target=this.element[0],o=i.originalEvent)for(n in o)n in i||(i[n]=o[n]);return this.element.trigger(i,s),!(t.isFunction(a)&&a.apply(this.element[0],[i].concat(s))===!1||i.isDefaultPrevented())}},t.each({show:"fadeIn",hide:"fadeOut"},function(e,i){t.Widget.prototype["_"+e]=function(s,n,o){"string"==typeof n&&(n={effect:n});var a,r=n?n===!0||"number"==typeof n?i:n.effect||i:e;n=n||{},"number"==typeof n&&(n={duration:n}),a=!t.isEmptyObject(n),n.complete=o,n.delay&&s.delay(n.delay),a&&t.effects&&t.effects.effect[r]?s[e](n):r!==e&&s[r]?s[r](n.duration,n.easing,o):s.queue(function(i){t(this)[e](),o&&o.call(s[0]),i()})}}),t.widget,function(){function e(t,e,i){return[parseFloat(t[0])*(u.test(t[0])?e/100:1),parseFloat(t[1])*(u.test(t[1])?i/100:1)]}function i(e,i){return parseInt(t.css(e,i),10)||0}function s(e){var i=e[0];return 9===i.nodeType?{width:e.width(),height:e.height(),offset:{top:0,left:0}}:t.isWindow(i)?{width:e.width(),height:e.height(),offset:{top:e.scrollTop(),left:e.scrollLeft()}}:i.preventDefault?{width:0,height:0,offset:{top:i.pageY,left:i.pageX}}:{width:e.outerWidth(),height:e.outerHeight(),offset:e.offset()}}var n,o=Math.max,a=Math.abs,r=/left|center|right/,h=/top|center|bottom/,l=/[\+\-]\d+(\.[\d]+)?%?/,c=/^\w+/,u=/%$/,d=t.fn.position;t.position={scrollbarWidth:function(){if(void 0!==n)return n;var e,i,s=t("
"),o=s.children()[0];return t("body").append(s),e=o.offsetWidth,s.css("overflow","scroll"),i=o.offsetWidth,e===i&&(i=s[0].clientWidth),s.remove(),n=e-i},getScrollInfo:function(e){var i=e.isWindow||e.isDocument?"":e.element.css("overflow-x"),s=e.isWindow||e.isDocument?"":e.element.css("overflow-y"),n="scroll"===i||"auto"===i&&e.widthi?"left":e>0?"right":"center",vertical:0>r?"top":s>0?"bottom":"middle"};l>p&&p>a(e+i)&&(u.horizontal="center"),c>f&&f>a(s+r)&&(u.vertical="middle"),u.important=o(a(e),a(i))>o(a(s),a(r))?"horizontal":"vertical",n.using.call(this,t,u)}),h.offset(t.extend(D,{using:r}))})},t.ui.position={fit:{left:function(t,e){var i,s=e.within,n=s.isWindow?s.scrollLeft:s.offset.left,a=s.width,r=t.left-e.collisionPosition.marginLeft,h=n-r,l=r+e.collisionWidth-a-n;e.collisionWidth>a?h>0&&0>=l?(i=t.left+h+e.collisionWidth-a-n,t.left+=h-i):t.left=l>0&&0>=h?n:h>l?n+a-e.collisionWidth:n:h>0?t.left+=h:l>0?t.left-=l:t.left=o(t.left-r,t.left)},top:function(t,e){var i,s=e.within,n=s.isWindow?s.scrollTop:s.offset.top,a=e.within.height,r=t.top-e.collisionPosition.marginTop,h=n-r,l=r+e.collisionHeight-a-n;e.collisionHeight>a?h>0&&0>=l?(i=t.top+h+e.collisionHeight-a-n,t.top+=h-i):t.top=l>0&&0>=h?n:h>l?n+a-e.collisionHeight:n:h>0?t.top+=h:l>0?t.top-=l:t.top=o(t.top-r,t.top)}},flip:{left:function(t,e){var i,s,n=e.within,o=n.offset.left+n.scrollLeft,r=n.width,h=n.isWindow?n.scrollLeft:n.offset.left,l=t.left-e.collisionPosition.marginLeft,c=l-h,u=l+e.collisionWidth-r-h,d="left"===e.my[0]?-e.elemWidth:"right"===e.my[0]?e.elemWidth:0,p="left"===e.at[0]?e.targetWidth:"right"===e.at[0]?-e.targetWidth:0,f=-2*e.offset[0];0>c?(i=t.left+d+p+f+e.collisionWidth-r-o,(0>i||a(c)>i)&&(t.left+=d+p+f)):u>0&&(s=t.left-e.collisionPosition.marginLeft+d+p+f-h,(s>0||u>a(s))&&(t.left+=d+p+f))},top:function(t,e){var i,s,n=e.within,o=n.offset.top+n.scrollTop,r=n.height,h=n.isWindow?n.scrollTop:n.offset.top,l=t.top-e.collisionPosition.marginTop,c=l-h,u=l+e.collisionHeight-r-h,d="top"===e.my[1],p=d?-e.elemHeight:"bottom"===e.my[1]?e.elemHeight:0,f="top"===e.at[1]?e.targetHeight:"bottom"===e.at[1]?-e.targetHeight:0,m=-2*e.offset[1];0>c?(s=t.top+p+f+m+e.collisionHeight-r-o,(0>s||a(c)>s)&&(t.top+=p+f+m)):u>0&&(i=t.top-e.collisionPosition.marginTop+p+f+m-h,(i>0||u>a(i))&&(t.top+=p+f+m))}},flipfit:{left:function(){t.ui.position.flip.left.apply(this,arguments),t.ui.position.fit.left.apply(this,arguments)},top:function(){t.ui.position.flip.top.apply(this,arguments),t.ui.position.fit.top.apply(this,arguments)}}}}(),t.ui.position,t.extend(t.expr[":"],{data:t.expr.createPseudo?t.expr.createPseudo(function(e){return function(i){return!!t.data(i,e)}}):function(e,i,s){return!!t.data(e,s[3])}}),t.fn.extend({disableSelection:function(){var t="onselectstart"in document.createElement("div")?"selectstart":"mousedown";return function(){return this.on(t+".ui-disableSelection",function(t){t.preventDefault()})}}(),enableSelection:function(){return this.off(".ui-disableSelection")}}),t.ui.focusable=function(i,s){var n,o,a,r,h,l=i.nodeName.toLowerCase();return"area"===l?(n=i.parentNode,o=n.name,i.href&&o&&"map"===n.nodeName.toLowerCase()?(a=t("img[usemap='#"+o+"']"),a.length>0&&a.is(":visible")):!1):(/^(input|select|textarea|button|object)$/.test(l)?(r=!i.disabled,r&&(h=t(i).closest("fieldset")[0],h&&(r=!h.disabled))):r="a"===l?i.href||s:s,r&&t(i).is(":visible")&&e(t(i)))},t.extend(t.expr[":"],{focusable:function(e){return t.ui.focusable(e,null!=t.attr(e,"tabindex"))}}),t.ui.focusable,t.fn.form=function(){return"string"==typeof this[0].form?this.closest("form"):t(this[0].form)},t.ui.formResetMixin={_formResetHandler:function(){var e=t(this);setTimeout(function(){var i=e.data("ui-form-reset-instances");t.each(i,function(){this.refresh()})})},_bindFormResetHandler:function(){if(this.form=this.element.form(),this.form.length){var t=this.form.data("ui-form-reset-instances")||[];t.length||this.form.on("reset.ui-form-reset",this._formResetHandler),t.push(this),this.form.data("ui-form-reset-instances",t)}},_unbindFormResetHandler:function(){if(this.form.length){var e=this.form.data("ui-form-reset-instances");e.splice(t.inArray(this,e),1),e.length?this.form.data("ui-form-reset-instances",e):this.form.removeData("ui-form-reset-instances").off("reset.ui-form-reset")}}},"1.7"===t.fn.jquery.substring(0,3)&&(t.each(["Width","Height"],function(e,i){function s(e,i,s,o){return t.each(n,function(){i-=parseFloat(t.css(e,"padding"+this))||0,s&&(i-=parseFloat(t.css(e,"border"+this+"Width"))||0),o&&(i-=parseFloat(t.css(e,"margin"+this))||0)}),i}var n="Width"===i?["Left","Right"]:["Top","Bottom"],o=i.toLowerCase(),a={innerWidth:t.fn.innerWidth,innerHeight:t.fn.innerHeight,outerWidth:t.fn.outerWidth,outerHeight:t.fn.outerHeight};t.fn["inner"+i]=function(e){return void 0===e?a["inner"+i].call(this):this.each(function(){t(this).css(o,s(this,e)+"px")})},t.fn["outer"+i]=function(e,n){return"number"!=typeof e?a["outer"+i].call(this,e):this.each(function(){t(this).css(o,s(this,e,!0,n)+"px")})}}),t.fn.addBack=function(t){return this.add(null==t?this.prevObject:this.prevObject.filter(t))}),t.ui.keyCode={BACKSPACE:8,COMMA:188,DELETE:46,DOWN:40,END:35,ENTER:13,ESCAPE:27,HOME:36,LEFT:37,PAGE_DOWN:34,PAGE_UP:33,PERIOD:190,RIGHT:39,SPACE:32,TAB:9,UP:38},t.ui.escapeSelector=function(){var t=/([!"#$%&'()*+,./:;<=>?@[\]^`{|}~])/g;return function(e){return e.replace(t,"\\$1")}}(),t.fn.labels=function(){var e,i,s,n,o;return this[0].labels&&this[0].labels.length?this.pushStack(this[0].labels):(n=this.eq(0).parents("label"),s=this.attr("id"),s&&(e=this.eq(0).parents().last(),o=e.add(e.length?e.siblings():this.siblings()),i="label[for='"+t.ui.escapeSelector(s)+"']",n=n.add(o.find(i).addBack(i))),this.pushStack(n))},t.fn.scrollParent=function(e){var i=this.css("position"),s="absolute"===i,n=e?/(auto|scroll|hidden)/:/(auto|scroll)/,o=this.parents().filter(function(){var e=t(this);return s&&"static"===e.css("position")?!1:n.test(e.css("overflow")+e.css("overflow-y")+e.css("overflow-x"))}).eq(0);return"fixed"!==i&&o.length?o:t(this[0].ownerDocument||document)},t.extend(t.expr[":"],{tabbable:function(e){var i=t.attr(e,"tabindex"),s=null!=i;return(!s||i>=0)&&t.ui.focusable(e,s)}}),t.fn.extend({uniqueId:function(){var t=0;return function(){return this.each(function(){this.id||(this.id="ui-id-"+ ++t)})}}(),removeUniqueId:function(){return this.each(function(){/^ui-id-\d+$/.test(this.id)&&t(this).removeAttr("id")})}}),t.ui.ie=!!/msie [\w.]+/.exec(navigator.userAgent.toLowerCase());var n=!1;t(document).on("mouseup",function(){n=!1}),t.widget("ui.mouse",{version:"1.12.1",options:{cancel:"input, textarea, button, select, option",distance:1,delay:0},_mouseInit:function(){var e=this;this.element.on("mousedown."+this.widgetName,function(t){return e._mouseDown(t)}).on("click."+this.widgetName,function(i){return!0===t.data(i.target,e.widgetName+".preventClickEvent")?(t.removeData(i.target,e.widgetName+".preventClickEvent"),i.stopImmediatePropagation(),!1):void 0}),this.started=!1},_mouseDestroy:function(){this.element.off("."+this.widgetName),this._mouseMoveDelegate&&this.document.off("mousemove."+this.widgetName,this._mouseMoveDelegate).off("mouseup."+this.widgetName,this._mouseUpDelegate)},_mouseDown:function(e){if(!n){this._mouseMoved=!1,this._mouseStarted&&this._mouseUp(e),this._mouseDownEvent=e;var i=this,s=1===e.which,o="string"==typeof this.options.cancel&&e.target.nodeName?t(e.target).closest(this.options.cancel).length:!1;return s&&!o&&this._mouseCapture(e)?(this.mouseDelayMet=!this.options.delay,this.mouseDelayMet||(this._mouseDelayTimer=setTimeout(function(){i.mouseDelayMet=!0},this.options.delay)),this._mouseDistanceMet(e)&&this._mouseDelayMet(e)&&(this._mouseStarted=this._mouseStart(e)!==!1,!this._mouseStarted)?(e.preventDefault(),!0):(!0===t.data(e.target,this.widgetName+".preventClickEvent")&&t.removeData(e.target,this.widgetName+".preventClickEvent"),this._mouseMoveDelegate=function(t){return i._mouseMove(t)},this._mouseUpDelegate=function(t){return i._mouseUp(t)},this.document.on("mousemove."+this.widgetName,this._mouseMoveDelegate).on("mouseup."+this.widgetName,this._mouseUpDelegate),e.preventDefault(),n=!0,!0)):!0}},_mouseMove:function(e){if(this._mouseMoved){if(t.ui.ie&&(!document.documentMode||9>document.documentMode)&&!e.button)return this._mouseUp(e);if(!e.which)if(e.originalEvent.altKey||e.originalEvent.ctrlKey||e.originalEvent.metaKey||e.originalEvent.shiftKey)this.ignoreMissingWhich=!0;else if(!this.ignoreMissingWhich)return this._mouseUp(e)}return(e.which||e.button)&&(this._mouseMoved=!0),this._mouseStarted?(this._mouseDrag(e),e.preventDefault()):(this._mouseDistanceMet(e)&&this._mouseDelayMet(e)&&(this._mouseStarted=this._mouseStart(this._mouseDownEvent,e)!==!1,this._mouseStarted?this._mouseDrag(e):this._mouseUp(e)),!this._mouseStarted)},_mouseUp:function(e){this.document.off("mousemove."+this.widgetName,this._mouseMoveDelegate).off("mouseup."+this.widgetName,this._mouseUpDelegate),this._mouseStarted&&(this._mouseStarted=!1,e.target===this._mouseDownEvent.target&&t.data(e.target,this.widgetName+".preventClickEvent",!0),this._mouseStop(e)),this._mouseDelayTimer&&(clearTimeout(this._mouseDelayTimer),delete this._mouseDelayTimer),this.ignoreMissingWhich=!1,n=!1,e.preventDefault()},_mouseDistanceMet:function(t){return Math.max(Math.abs(this._mouseDownEvent.pageX-t.pageX),Math.abs(this._mouseDownEvent.pageY-t.pageY))>=this.options.distance},_mouseDelayMet:function(){return this.mouseDelayMet},_mouseStart:function(){},_mouseDrag:function(){},_mouseStop:function(){},_mouseCapture:function(){return!0}}),t.ui.plugin={add:function(e,i,s){var n,o=t.ui[e].prototype;for(n in s)o.plugins[n]=o.plugins[n]||[],o.plugins[n].push([i,s[n]])},call:function(t,e,i,s){var n,o=t.plugins[e];if(o&&(s||t.element[0].parentNode&&11!==t.element[0].parentNode.nodeType))for(n=0;o.length>n;n++)t.options[o[n][0]]&&o[n][1].apply(t.element,i)}},t.widget("ui.resizable",t.ui.mouse,{version:"1.12.1",widgetEventPrefix:"resize",options:{alsoResize:!1,animate:!1,animateDuration:"slow",animateEasing:"swing",aspectRatio:!1,autoHide:!1,classes:{"ui-resizable-se":"ui-icon ui-icon-gripsmall-diagonal-se"},containment:!1,ghost:!1,grid:!1,handles:"e,s,se",helper:!1,maxHeight:null,maxWidth:null,minHeight:10,minWidth:10,zIndex:90,resize:null,start:null,stop:null},_num:function(t){return parseFloat(t)||0},_isNumber:function(t){return!isNaN(parseFloat(t))},_hasScroll:function(e,i){if("hidden"===t(e).css("overflow"))return!1;var s=i&&"left"===i?"scrollLeft":"scrollTop",n=!1;return e[s]>0?!0:(e[s]=1,n=e[s]>0,e[s]=0,n)},_create:function(){var e,i=this.options,s=this;this._addClass("ui-resizable"),t.extend(this,{_aspectRatio:!!i.aspectRatio,aspectRatio:i.aspectRatio,originalElement:this.element,_proportionallyResizeElements:[],_helper:i.helper||i.ghost||i.animate?i.helper||"ui-resizable-helper":null}),this.element[0].nodeName.match(/^(canvas|textarea|input|select|button|img)$/i)&&(this.element.wrap(t("
").css({position:this.element.css("position"),width:this.element.outerWidth(),height:this.element.outerHeight(),top:this.element.css("top"),left:this.element.css("left")})),this.element=this.element.parent().data("ui-resizable",this.element.resizable("instance")),this.elementIsWrapper=!0,e={marginTop:this.originalElement.css("marginTop"),marginRight:this.originalElement.css("marginRight"),marginBottom:this.originalElement.css("marginBottom"),marginLeft:this.originalElement.css("marginLeft")},this.element.css(e),this.originalElement.css("margin",0),this.originalResizeStyle=this.originalElement.css("resize"),this.originalElement.css("resize","none"),this._proportionallyResizeElements.push(this.originalElement.css({position:"static",zoom:1,display:"block"})),this.originalElement.css(e),this._proportionallyResize()),this._setupHandles(),i.autoHide&&t(this.element).on("mouseenter",function(){i.disabled||(s._removeClass("ui-resizable-autohide"),s._handles.show())}).on("mouseleave",function(){i.disabled||s.resizing||(s._addClass("ui-resizable-autohide"),s._handles.hide())}),this._mouseInit()},_destroy:function(){this._mouseDestroy();var e,i=function(e){t(e).removeData("resizable").removeData("ui-resizable").off(".resizable").find(".ui-resizable-handle").remove()};return this.elementIsWrapper&&(i(this.element),e=this.element,this.originalElement.css({position:e.css("position"),width:e.outerWidth(),height:e.outerHeight(),top:e.css("top"),left:e.css("left")}).insertAfter(e),e.remove()),this.originalElement.css("resize",this.originalResizeStyle),i(this.originalElement),this},_setOption:function(t,e){switch(this._super(t,e),t){case"handles":this._removeHandles(),this._setupHandles();break;default:}},_setupHandles:function(){var e,i,s,n,o,a=this.options,r=this;if(this.handles=a.handles||(t(".ui-resizable-handle",this.element).length?{n:".ui-resizable-n",e:".ui-resizable-e",s:".ui-resizable-s",w:".ui-resizable-w",se:".ui-resizable-se",sw:".ui-resizable-sw",ne:".ui-resizable-ne",nw:".ui-resizable-nw"}:"e,s,se"),this._handles=t(),this.handles.constructor===String)for("all"===this.handles&&(this.handles="n,e,s,w,se,sw,ne,nw"),s=this.handles.split(","),this.handles={},i=0;s.length>i;i++)e=t.trim(s[i]),n="ui-resizable-"+e,o=t("
"),this._addClass(o,"ui-resizable-handle "+n),o.css({zIndex:a.zIndex}),this.handles[e]=".ui-resizable-"+e,this.element.append(o);this._renderAxis=function(e){var i,s,n,o;e=e||this.element;for(i in this.handles)this.handles[i].constructor===String?this.handles[i]=this.element.children(this.handles[i]).first().show():(this.handles[i].jquery||this.handles[i].nodeType)&&(this.handles[i]=t(this.handles[i]),this._on(this.handles[i],{mousedown:r._mouseDown})),this.elementIsWrapper&&this.originalElement[0].nodeName.match(/^(textarea|input|select|button)$/i)&&(s=t(this.handles[i],this.element),o=/sw|ne|nw|se|n|s/.test(i)?s.outerHeight():s.outerWidth(),n=["padding",/ne|nw|n/.test(i)?"Top":/se|sw|s/.test(i)?"Bottom":/^e$/.test(i)?"Right":"Left"].join(""),e.css(n,o),this._proportionallyResize()),this._handles=this._handles.add(this.handles[i])},this._renderAxis(this.element),this._handles=this._handles.add(this.element.find(".ui-resizable-handle")),this._handles.disableSelection(),this._handles.on("mouseover",function(){r.resizing||(this.className&&(o=this.className.match(/ui-resizable-(se|sw|ne|nw|n|e|s|w)/i)),r.axis=o&&o[1]?o[1]:"se")}),a.autoHide&&(this._handles.hide(),this._addClass("ui-resizable-autohide"))},_removeHandles:function(){this._handles.remove()},_mouseCapture:function(e){var i,s,n=!1;for(i in this.handles)s=t(this.handles[i])[0],(s===e.target||t.contains(s,e.target))&&(n=!0);return!this.options.disabled&&n},_mouseStart:function(e){var i,s,n,o=this.options,a=this.element;return this.resizing=!0,this._renderProxy(),i=this._num(this.helper.css("left")),s=this._num(this.helper.css("top")),o.containment&&(i+=t(o.containment).scrollLeft()||0,s+=t(o.containment).scrollTop()||0),this.offset=this.helper.offset(),this.position={left:i,top:s},this.size=this._helper?{width:this.helper.width(),height:this.helper.height()}:{width:a.width(),height:a.height()},this.originalSize=this._helper?{width:a.outerWidth(),height:a.outerHeight()}:{width:a.width(),height:a.height()},this.sizeDiff={width:a.outerWidth()-a.width(),height:a.outerHeight()-a.height()},this.originalPosition={left:i,top:s},this.originalMousePosition={left:e.pageX,top:e.pageY},this.aspectRatio="number"==typeof o.aspectRatio?o.aspectRatio:this.originalSize.width/this.originalSize.height||1,n=t(".ui-resizable-"+this.axis).css("cursor"),t("body").css("cursor","auto"===n?this.axis+"-resize":n),this._addClass("ui-resizable-resizing"),this._propagate("start",e),!0},_mouseDrag:function(e){var i,s,n=this.originalMousePosition,o=this.axis,a=e.pageX-n.left||0,r=e.pageY-n.top||0,h=this._change[o];return this._updatePrevProperties(),h?(i=h.apply(this,[e,a,r]),this._updateVirtualBoundaries(e.shiftKey),(this._aspectRatio||e.shiftKey)&&(i=this._updateRatio(i,e)),i=this._respectSize(i,e),this._updateCache(i),this._propagate("resize",e),s=this._applyChanges(),!this._helper&&this._proportionallyResizeElements.length&&this._proportionallyResize(),t.isEmptyObject(s)||(this._updatePrevProperties(),this._trigger("resize",e,this.ui()),this._applyChanges()),!1):!1},_mouseStop:function(e){this.resizing=!1;var i,s,n,o,a,r,h,l=this.options,c=this;return this._helper&&(i=this._proportionallyResizeElements,s=i.length&&/textarea/i.test(i[0].nodeName),n=s&&this._hasScroll(i[0],"left")?0:c.sizeDiff.height,o=s?0:c.sizeDiff.width,a={width:c.helper.width()-o,height:c.helper.height()-n},r=parseFloat(c.element.css("left"))+(c.position.left-c.originalPosition.left)||null,h=parseFloat(c.element.css("top"))+(c.position.top-c.originalPosition.top)||null,l.animate||this.element.css(t.extend(a,{top:h,left:r})),c.helper.height(c.size.height),c.helper.width(c.size.width),this._helper&&!l.animate&&this._proportionallyResize()),t("body").css("cursor","auto"),this._removeClass("ui-resizable-resizing"),this._propagate("stop",e),this._helper&&this.helper.remove(),!1},_updatePrevProperties:function(){this.prevPosition={top:this.position.top,left:this.position.left},this.prevSize={width:this.size.width,height:this.size.height}},_applyChanges:function(){var t={};return this.position.top!==this.prevPosition.top&&(t.top=this.position.top+"px"),this.position.left!==this.prevPosition.left&&(t.left=this.position.left+"px"),this.size.width!==this.prevSize.width&&(t.width=this.size.width+"px"),this.size.height!==this.prevSize.height&&(t.height=this.size.height+"px"),this.helper.css(t),t},_updateVirtualBoundaries:function(t){var e,i,s,n,o,a=this.options;o={minWidth:this._isNumber(a.minWidth)?a.minWidth:0,maxWidth:this._isNumber(a.maxWidth)?a.maxWidth:1/0,minHeight:this._isNumber(a.minHeight)?a.minHeight:0,maxHeight:this._isNumber(a.maxHeight)?a.maxHeight:1/0},(this._aspectRatio||t)&&(e=o.minHeight*this.aspectRatio,s=o.minWidth/this.aspectRatio,i=o.maxHeight*this.aspectRatio,n=o.maxWidth/this.aspectRatio,e>o.minWidth&&(o.minWidth=e),s>o.minHeight&&(o.minHeight=s),o.maxWidth>i&&(o.maxWidth=i),o.maxHeight>n&&(o.maxHeight=n)),this._vBoundaries=o},_updateCache:function(t){this.offset=this.helper.offset(),this._isNumber(t.left)&&(this.position.left=t.left),this._isNumber(t.top)&&(this.position.top=t.top),this._isNumber(t.height)&&(this.size.height=t.height),this._isNumber(t.width)&&(this.size.width=t.width)},_updateRatio:function(t){var e=this.position,i=this.size,s=this.axis;return this._isNumber(t.height)?t.width=t.height*this.aspectRatio:this._isNumber(t.width)&&(t.height=t.width/this.aspectRatio),"sw"===s&&(t.left=e.left+(i.width-t.width),t.top=null),"nw"===s&&(t.top=e.top+(i.height-t.height),t.left=e.left+(i.width-t.width)),t},_respectSize:function(t){var e=this._vBoundaries,i=this.axis,s=this._isNumber(t.width)&&e.maxWidth&&e.maxWidtht.width,a=this._isNumber(t.height)&&e.minHeight&&e.minHeight>t.height,r=this.originalPosition.left+this.originalSize.width,h=this.originalPosition.top+this.originalSize.height,l=/sw|nw|w/.test(i),c=/nw|ne|n/.test(i);return o&&(t.width=e.minWidth),a&&(t.height=e.minHeight),s&&(t.width=e.maxWidth),n&&(t.height=e.maxHeight),o&&l&&(t.left=r-e.minWidth),s&&l&&(t.left=r-e.maxWidth),a&&c&&(t.top=h-e.minHeight),n&&c&&(t.top=h-e.maxHeight),t.width||t.height||t.left||!t.top?t.width||t.height||t.top||!t.left||(t.left=null):t.top=null,t},_getPaddingPlusBorderDimensions:function(t){for(var e=0,i=[],s=[t.css("borderTopWidth"),t.css("borderRightWidth"),t.css("borderBottomWidth"),t.css("borderLeftWidth")],n=[t.css("paddingTop"),t.css("paddingRight"),t.css("paddingBottom"),t.css("paddingLeft")];4>e;e++)i[e]=parseFloat(s[e])||0,i[e]+=parseFloat(n[e])||0;return{height:i[0]+i[2],width:i[1]+i[3]}},_proportionallyResize:function(){if(this._proportionallyResizeElements.length)for(var t,e=0,i=this.helper||this.element;this._proportionallyResizeElements.length>e;e++)t=this._proportionallyResizeElements[e],this.outerDimensions||(this.outerDimensions=this._getPaddingPlusBorderDimensions(t)),t.css({height:i.height()-this.outerDimensions.height||0,width:i.width()-this.outerDimensions.width||0})},_renderProxy:function(){var e=this.element,i=this.options;this.elementOffset=e.offset(),this._helper?(this.helper=this.helper||t("
"),this._addClass(this.helper,this._helper),this.helper.css({width:this.element.outerWidth(),height:this.element.outerHeight(),position:"absolute",left:this.elementOffset.left+"px",top:this.elementOffset.top+"px",zIndex:++i.zIndex}),this.helper.appendTo("body").disableSelection()):this.helper=this.element -},_change:{e:function(t,e){return{width:this.originalSize.width+e}},w:function(t,e){var i=this.originalSize,s=this.originalPosition;return{left:s.left+e,width:i.width-e}},n:function(t,e,i){var s=this.originalSize,n=this.originalPosition;return{top:n.top+i,height:s.height-i}},s:function(t,e,i){return{height:this.originalSize.height+i}},se:function(e,i,s){return t.extend(this._change.s.apply(this,arguments),this._change.e.apply(this,[e,i,s]))},sw:function(e,i,s){return t.extend(this._change.s.apply(this,arguments),this._change.w.apply(this,[e,i,s]))},ne:function(e,i,s){return t.extend(this._change.n.apply(this,arguments),this._change.e.apply(this,[e,i,s]))},nw:function(e,i,s){return t.extend(this._change.n.apply(this,arguments),this._change.w.apply(this,[e,i,s]))}},_propagate:function(e,i){t.ui.plugin.call(this,e,[i,this.ui()]),"resize"!==e&&this._trigger(e,i,this.ui())},plugins:{},ui:function(){return{originalElement:this.originalElement,element:this.element,helper:this.helper,position:this.position,size:this.size,originalSize:this.originalSize,originalPosition:this.originalPosition}}}),t.ui.plugin.add("resizable","animate",{stop:function(e){var i=t(this).resizable("instance"),s=i.options,n=i._proportionallyResizeElements,o=n.length&&/textarea/i.test(n[0].nodeName),a=o&&i._hasScroll(n[0],"left")?0:i.sizeDiff.height,r=o?0:i.sizeDiff.width,h={width:i.size.width-r,height:i.size.height-a},l=parseFloat(i.element.css("left"))+(i.position.left-i.originalPosition.left)||null,c=parseFloat(i.element.css("top"))+(i.position.top-i.originalPosition.top)||null;i.element.animate(t.extend(h,c&&l?{top:c,left:l}:{}),{duration:s.animateDuration,easing:s.animateEasing,step:function(){var s={width:parseFloat(i.element.css("width")),height:parseFloat(i.element.css("height")),top:parseFloat(i.element.css("top")),left:parseFloat(i.element.css("left"))};n&&n.length&&t(n[0]).css({width:s.width,height:s.height}),i._updateCache(s),i._propagate("resize",e)}})}}),t.ui.plugin.add("resizable","containment",{start:function(){var e,i,s,n,o,a,r,h=t(this).resizable("instance"),l=h.options,c=h.element,u=l.containment,d=u instanceof t?u.get(0):/parent/.test(u)?c.parent().get(0):u;d&&(h.containerElement=t(d),/document/.test(u)||u===document?(h.containerOffset={left:0,top:0},h.containerPosition={left:0,top:0},h.parentData={element:t(document),left:0,top:0,width:t(document).width(),height:t(document).height()||document.body.parentNode.scrollHeight}):(e=t(d),i=[],t(["Top","Right","Left","Bottom"]).each(function(t,s){i[t]=h._num(e.css("padding"+s))}),h.containerOffset=e.offset(),h.containerPosition=e.position(),h.containerSize={height:e.innerHeight()-i[3],width:e.innerWidth()-i[1]},s=h.containerOffset,n=h.containerSize.height,o=h.containerSize.width,a=h._hasScroll(d,"left")?d.scrollWidth:o,r=h._hasScroll(d)?d.scrollHeight:n,h.parentData={element:d,left:s.left,top:s.top,width:a,height:r}))},resize:function(e){var i,s,n,o,a=t(this).resizable("instance"),r=a.options,h=a.containerOffset,l=a.position,c=a._aspectRatio||e.shiftKey,u={top:0,left:0},d=a.containerElement,p=!0;d[0]!==document&&/static/.test(d.css("position"))&&(u=h),l.left<(a._helper?h.left:0)&&(a.size.width=a.size.width+(a._helper?a.position.left-h.left:a.position.left-u.left),c&&(a.size.height=a.size.width/a.aspectRatio,p=!1),a.position.left=r.helper?h.left:0),l.top<(a._helper?h.top:0)&&(a.size.height=a.size.height+(a._helper?a.position.top-h.top:a.position.top),c&&(a.size.width=a.size.height*a.aspectRatio,p=!1),a.position.top=a._helper?h.top:0),n=a.containerElement.get(0)===a.element.parent().get(0),o=/relative|absolute/.test(a.containerElement.css("position")),n&&o?(a.offset.left=a.parentData.left+a.position.left,a.offset.top=a.parentData.top+a.position.top):(a.offset.left=a.element.offset().left,a.offset.top=a.element.offset().top),i=Math.abs(a.sizeDiff.width+(a._helper?a.offset.left-u.left:a.offset.left-h.left)),s=Math.abs(a.sizeDiff.height+(a._helper?a.offset.top-u.top:a.offset.top-h.top)),i+a.size.width>=a.parentData.width&&(a.size.width=a.parentData.width-i,c&&(a.size.height=a.size.width/a.aspectRatio,p=!1)),s+a.size.height>=a.parentData.height&&(a.size.height=a.parentData.height-s,c&&(a.size.width=a.size.height*a.aspectRatio,p=!1)),p||(a.position.left=a.prevPosition.left,a.position.top=a.prevPosition.top,a.size.width=a.prevSize.width,a.size.height=a.prevSize.height)},stop:function(){var e=t(this).resizable("instance"),i=e.options,s=e.containerOffset,n=e.containerPosition,o=e.containerElement,a=t(e.helper),r=a.offset(),h=a.outerWidth()-e.sizeDiff.width,l=a.outerHeight()-e.sizeDiff.height;e._helper&&!i.animate&&/relative/.test(o.css("position"))&&t(this).css({left:r.left-n.left-s.left,width:h,height:l}),e._helper&&!i.animate&&/static/.test(o.css("position"))&&t(this).css({left:r.left-n.left-s.left,width:h,height:l})}}),t.ui.plugin.add("resizable","alsoResize",{start:function(){var e=t(this).resizable("instance"),i=e.options;t(i.alsoResize).each(function(){var e=t(this);e.data("ui-resizable-alsoresize",{width:parseFloat(e.width()),height:parseFloat(e.height()),left:parseFloat(e.css("left")),top:parseFloat(e.css("top"))})})},resize:function(e,i){var s=t(this).resizable("instance"),n=s.options,o=s.originalSize,a=s.originalPosition,r={height:s.size.height-o.height||0,width:s.size.width-o.width||0,top:s.position.top-a.top||0,left:s.position.left-a.left||0};t(n.alsoResize).each(function(){var e=t(this),s=t(this).data("ui-resizable-alsoresize"),n={},o=e.parents(i.originalElement[0]).length?["width","height"]:["width","height","top","left"];t.each(o,function(t,e){var i=(s[e]||0)+(r[e]||0);i&&i>=0&&(n[e]=i||null)}),e.css(n)})},stop:function(){t(this).removeData("ui-resizable-alsoresize")}}),t.ui.plugin.add("resizable","ghost",{start:function(){var e=t(this).resizable("instance"),i=e.size;e.ghost=e.originalElement.clone(),e.ghost.css({opacity:.25,display:"block",position:"relative",height:i.height,width:i.width,margin:0,left:0,top:0}),e._addClass(e.ghost,"ui-resizable-ghost"),t.uiBackCompat!==!1&&"string"==typeof e.options.ghost&&e.ghost.addClass(this.options.ghost),e.ghost.appendTo(e.helper)},resize:function(){var e=t(this).resizable("instance");e.ghost&&e.ghost.css({position:"relative",height:e.size.height,width:e.size.width})},stop:function(){var e=t(this).resizable("instance");e.ghost&&e.helper&&e.helper.get(0).removeChild(e.ghost.get(0))}}),t.ui.plugin.add("resizable","grid",{resize:function(){var e,i=t(this).resizable("instance"),s=i.options,n=i.size,o=i.originalSize,a=i.originalPosition,r=i.axis,h="number"==typeof s.grid?[s.grid,s.grid]:s.grid,l=h[0]||1,c=h[1]||1,u=Math.round((n.width-o.width)/l)*l,d=Math.round((n.height-o.height)/c)*c,p=o.width+u,f=o.height+d,m=s.maxWidth&&p>s.maxWidth,g=s.maxHeight&&f>s.maxHeight,_=s.minWidth&&s.minWidth>p,v=s.minHeight&&s.minHeight>f;s.grid=h,_&&(p+=l),v&&(f+=c),m&&(p-=l),g&&(f-=c),/^(se|s|e)$/.test(r)?(i.size.width=p,i.size.height=f):/^(ne)$/.test(r)?(i.size.width=p,i.size.height=f,i.position.top=a.top-d):/^(sw)$/.test(r)?(i.size.width=p,i.size.height=f,i.position.left=a.left-u):((0>=f-c||0>=p-l)&&(e=i._getPaddingPlusBorderDimensions(this)),f-c>0?(i.size.height=f,i.position.top=a.top-d):(f=c-e.height,i.size.height=f,i.position.top=a.top+o.height-f),p-l>0?(i.size.width=p,i.position.left=a.left-u):(p=l-e.width,i.size.width=p,i.position.left=a.left+o.width-p))}}),t.ui.resizable});/** - * Copyright (c) 2007 Ariel Flesler - aflesler β—‹ gmail β€’ com | https://github.com/flesler - * Licensed under MIT - * @author Ariel Flesler - * @version 2.1.2 - */ -;(function(f){"use strict";"function"===typeof define&&define.amd?define(["jquery"],f):"undefined"!==typeof module&&module.exports?module.exports=f(require("jquery")):f(jQuery)})(function($){"use strict";function n(a){return!a.nodeName||-1!==$.inArray(a.nodeName.toLowerCase(),["iframe","#document","html","body"])}function h(a){return $.isFunction(a)||$.isPlainObject(a)?a:{top:a,left:a}}var p=$.scrollTo=function(a,d,b){return $(window).scrollTo(a,d,b)};p.defaults={axis:"xy",duration:0,limit:!0};$.fn.scrollTo=function(a,d,b){"object"=== typeof d&&(b=d,d=0);"function"===typeof b&&(b={onAfter:b});"max"===a&&(a=9E9);b=$.extend({},p.defaults,b);d=d||b.duration;var u=b.queue&&1=f[g]?0:Math.min(f[g],n));!a&&1-1){targetElements.on(evt+EVENT_NAMESPACE,function elementToggle(event){$.powerTip.toggle(this,event)})}else{targetElements.on(evt+EVENT_NAMESPACE,function elementOpen(event){$.powerTip.show(this,event)})}});$.each(options.closeEvents,function(idx,evt){if($.inArray(evt,options.openEvents)<0){targetElements.on(evt+EVENT_NAMESPACE,function elementClose(event){$.powerTip.hide(this,!isMouseEvent(event))})}});targetElements.on("keydown"+EVENT_NAMESPACE,function elementKeyDown(event){if(event.keyCode===27){$.powerTip.hide(this,true)}})}return targetElements};$.fn.powerTip.defaults={fadeInTime:200,fadeOutTime:100,followMouse:false,popupId:"powerTip",popupClass:null,intentSensitivity:7,intentPollInterval:100,closeDelay:100,placement:"n",smartPlacement:false,offset:10,mouseOnToPopup:false,manual:false,openEvents:["mouseenter","focus"],closeEvents:["mouseleave","blur"]};$.fn.powerTip.smartPlacementLists={n:["n","ne","nw","s"],e:["e","ne","se","w","nw","sw","n","s","e"],s:["s","se","sw","n"],w:["w","nw","sw","e","ne","se","n","s","w"],nw:["nw","w","sw","n","s","se","nw"],ne:["ne","e","se","n","s","sw","ne"],sw:["sw","w","nw","s","n","ne","sw"],se:["se","e","ne","s","n","nw","se"],"nw-alt":["nw-alt","n","ne-alt","sw-alt","s","se-alt","w","e"],"ne-alt":["ne-alt","n","nw-alt","se-alt","s","sw-alt","e","w"],"sw-alt":["sw-alt","s","se-alt","nw-alt","n","ne-alt","w","e"],"se-alt":["se-alt","s","sw-alt","ne-alt","n","nw-alt","e","w"]};$.powerTip={show:function apiShowTip(element,event){if(isMouseEvent(event)){trackMouse(event);session.previousX=event.pageX;session.previousY=event.pageY;$(element).data(DATA_DISPLAYCONTROLLER).show()}else{$(element).first().data(DATA_DISPLAYCONTROLLER).show(true,true)}return element},reposition:function apiResetPosition(element){$(element).first().data(DATA_DISPLAYCONTROLLER).resetPosition();return element},hide:function apiCloseTip(element,immediate){var displayController;immediate=element?immediate:true;if(element){displayController=$(element).first().data(DATA_DISPLAYCONTROLLER)}else if(session.activeHover){displayController=session.activeHover.data(DATA_DISPLAYCONTROLLER)}if(displayController){displayController.hide(immediate)}return element},toggle:function apiToggle(element,event){if(session.activeHover&&session.activeHover.is(element)){$.powerTip.hide(element,!isMouseEvent(event))}else{$.powerTip.show(element,event)}return element}};$.powerTip.showTip=$.powerTip.show;$.powerTip.closeTip=$.powerTip.hide;function CSSCoordinates(){var me=this;me.top="auto";me.left="auto";me.right="auto";me.bottom="auto";me.set=function(property,value){if($.isNumeric(value)){me[property]=Math.round(value)}}}function DisplayController(element,options,tipController){var hoverTimer=null,myCloseDelay=null;function openTooltip(immediate,forceOpen){cancelTimer();if(!element.data(DATA_HASACTIVEHOVER)){if(!immediate){session.tipOpenImminent=true;hoverTimer=setTimeout(function intentDelay(){hoverTimer=null;checkForIntent()},options.intentPollInterval)}else{if(forceOpen){element.data(DATA_FORCEDOPEN,true)}closeAnyDelayed();tipController.showTip(element)}}else{cancelClose()}}function closeTooltip(disableDelay){if(myCloseDelay){myCloseDelay=session.closeDelayTimeout=clearTimeout(myCloseDelay);session.delayInProgress=false}cancelTimer();session.tipOpenImminent=false;if(element.data(DATA_HASACTIVEHOVER)){element.data(DATA_FORCEDOPEN,false);if(!disableDelay){session.delayInProgress=true;session.closeDelayTimeout=setTimeout(function closeDelay(){session.closeDelayTimeout=null;tipController.hideTip(element);session.delayInProgress=false;myCloseDelay=null},options.closeDelay);myCloseDelay=session.closeDelayTimeout}else{tipController.hideTip(element)}}}function checkForIntent(){var xDifference=Math.abs(session.previousX-session.currentX),yDifference=Math.abs(session.previousY-session.currentY),totalDifference=xDifference+yDifference;if(totalDifference",{id:options.popupId});if($body.length===0){$body=$("body")}$body.append(tipElement);session.tooltips=session.tooltips?session.tooltips.add(tipElement):tipElement}if(options.followMouse){if(!tipElement.data(DATA_HASMOUSEMOVE)){$document.on("mousemove"+EVENT_NAMESPACE,positionTipOnCursor);$window.on("scroll"+EVENT_NAMESPACE,positionTipOnCursor);tipElement.data(DATA_HASMOUSEMOVE,true)}}function beginShowTip(element){element.data(DATA_HASACTIVEHOVER,true);tipElement.queue(function queueTipInit(next){showTip(element);next()})}function showTip(element){var tipContent;if(!element.data(DATA_HASACTIVEHOVER)){return}if(session.isTipOpen){if(!session.isClosing){hideTip(session.activeHover)}tipElement.delay(100).queue(function queueTipAgain(next){showTip(element);next()});return}element.trigger("powerTipPreRender");tipContent=getTooltipContent(element);if(tipContent){tipElement.empty().append(tipContent)}else{return}element.trigger("powerTipRender");session.activeHover=element;session.isTipOpen=true;tipElement.data(DATA_MOUSEONTOTIP,options.mouseOnToPopup);tipElement.addClass(options.popupClass);if(!options.followMouse||element.data(DATA_FORCEDOPEN)){positionTipOnElement(element);session.isFixedTipOpen=true}else{positionTipOnCursor()}if(!element.data(DATA_FORCEDOPEN)&&!options.followMouse){$document.on("click"+EVENT_NAMESPACE,function documentClick(event){var target=event.target;if(target!==element[0]){if(options.mouseOnToPopup){if(target!==tipElement[0]&&!$.contains(tipElement[0],target)){$.powerTip.hide()}}else{$.powerTip.hide()}}})}if(options.mouseOnToPopup&&!options.manual){tipElement.on("mouseenter"+EVENT_NAMESPACE,function tipMouseEnter(){if(session.activeHover){session.activeHover.data(DATA_DISPLAYCONTROLLER).cancel()}});tipElement.on("mouseleave"+EVENT_NAMESPACE,function tipMouseLeave(){if(session.activeHover){session.activeHover.data(DATA_DISPLAYCONTROLLER).hide()}})}tipElement.fadeIn(options.fadeInTime,function fadeInCallback(){if(!session.desyncTimeout){session.desyncTimeout=setInterval(closeDesyncedTip,500)}element.trigger("powerTipOpen")})}function hideTip(element){session.isClosing=true;session.isTipOpen=false;session.desyncTimeout=clearInterval(session.desyncTimeout);element.data(DATA_HASACTIVEHOVER,false);element.data(DATA_FORCEDOPEN,false);$document.off("click"+EVENT_NAMESPACE);tipElement.off(EVENT_NAMESPACE);tipElement.fadeOut(options.fadeOutTime,function fadeOutCallback(){var coords=new CSSCoordinates;session.activeHover=null;session.isClosing=false;session.isFixedTipOpen=false;tipElement.removeClass();coords.set("top",session.currentY+options.offset);coords.set("left",session.currentX+options.offset);tipElement.css(coords);element.trigger("powerTipClose")})}function positionTipOnCursor(){var tipWidth,tipHeight,coords,collisions,collisionCount;if(!session.isFixedTipOpen&&(session.isTipOpen||session.tipOpenImminent&&tipElement.data(DATA_HASMOUSEMOVE))){tipWidth=tipElement.outerWidth();tipHeight=tipElement.outerHeight();coords=new CSSCoordinates;coords.set("top",session.currentY+options.offset);coords.set("left",session.currentX+options.offset);collisions=getViewportCollisions(coords,tipWidth,tipHeight);if(collisions!==Collision.none){collisionCount=countFlags(collisions);if(collisionCount===1){if(collisions===Collision.right){coords.set("left",session.scrollLeft+session.windowWidth-tipWidth)}else if(collisions===Collision.bottom){coords.set("top",session.scrollTop+session.windowHeight-tipHeight)}}else{coords.set("left",session.currentX-tipWidth-options.offset);coords.set("top",session.currentY-tipHeight-options.offset)}}tipElement.css(coords)}}function positionTipOnElement(element){var priorityList,finalPlacement;if(options.smartPlacement||options.followMouse&&element.data(DATA_FORCEDOPEN)){priorityList=$.fn.powerTip.smartPlacementLists[options.placement];$.each(priorityList,function(idx,pos){var collisions=getViewportCollisions(placeTooltip(element,pos),tipElement.outerWidth(),tipElement.outerHeight());finalPlacement=pos;return collisions!==Collision.none})}else{placeTooltip(element,options.placement);finalPlacement=options.placement}tipElement.removeClass("w nw sw e ne se n s w se-alt sw-alt ne-alt nw-alt");tipElement.addClass(finalPlacement)}function placeTooltip(element,placement){var iterationCount=0,tipWidth,tipHeight,coords=new CSSCoordinates;coords.set("top",0);coords.set("left",0);tipElement.css(coords);do{tipWidth=tipElement.outerWidth();tipHeight=tipElement.outerHeight();coords=placementCalculator.compute(element,placement,tipWidth,tipHeight,options.offset);tipElement.css(coords)}while(++iterationCount<=5&&(tipWidth!==tipElement.outerWidth()||tipHeight!==tipElement.outerHeight()));return coords}function closeDesyncedTip(){var isDesynced=false,hasDesyncableCloseEvent=$.grep(["mouseleave","mouseout","blur","focusout"],function(eventType){return $.inArray(eventType,options.closeEvents)!==-1}).length>0;if(session.isTipOpen&&!session.isClosing&&!session.delayInProgress&&hasDesyncableCloseEvent){if(session.activeHover.data(DATA_HASACTIVEHOVER)===false||session.activeHover.is(":disabled")){isDesynced=true}else if(!isMouseOver(session.activeHover)&&!session.activeHover.is(":focus")&&!session.activeHover.data(DATA_FORCEDOPEN)){if(tipElement.data(DATA_MOUSEONTOTIP)){if(!isMouseOver(tipElement)){isDesynced=true}}else{isDesynced=true}}if(isDesynced){hideTip(session.activeHover)}}}this.showTip=beginShowTip;this.hideTip=hideTip;this.resetPosition=positionTipOnElement}function isSvgElement(element){return Boolean(window.SVGElement&&element[0]instanceof SVGElement)}function isMouseEvent(event){return Boolean(event&&$.inArray(event.type,MOUSE_EVENTS)>-1&&typeof event.pageX==="number")}function initTracking(){if(!session.mouseTrackingActive){session.mouseTrackingActive=true;getViewportDimensions();$(getViewportDimensions);$document.on("mousemove"+EVENT_NAMESPACE,trackMouse);$window.on("resize"+EVENT_NAMESPACE,trackResize);$window.on("scroll"+EVENT_NAMESPACE,trackScroll)}}function getViewportDimensions(){session.scrollLeft=$window.scrollLeft();session.scrollTop=$window.scrollTop();session.windowWidth=$window.width();session.windowHeight=$window.height()}function trackResize(){session.windowWidth=$window.width();session.windowHeight=$window.height()}function trackScroll(){var x=$window.scrollLeft(),y=$window.scrollTop();if(x!==session.scrollLeft){session.currentX+=x-session.scrollLeft;session.scrollLeft=x}if(y!==session.scrollTop){session.currentY+=y-session.scrollTop;session.scrollTop=y}}function trackMouse(event){session.currentX=event.pageX;session.currentY=event.pageY}function isMouseOver(element){var elementPosition=element.offset(),elementBox=element[0].getBoundingClientRect(),elementWidth=elementBox.right-elementBox.left,elementHeight=elementBox.bottom-elementBox.top;return session.currentX>=elementPosition.left&&session.currentX<=elementPosition.left+elementWidth&&session.currentY>=elementPosition.top&&session.currentY<=elementPosition.top+elementHeight}function getTooltipContent(element){var tipText=element.data(DATA_POWERTIP),tipObject=element.data(DATA_POWERTIPJQ),tipTarget=element.data(DATA_POWERTIPTARGET),targetElement,content;if(tipText){if($.isFunction(tipText)){tipText=tipText.call(element[0])}content=tipText}else if(tipObject){if($.isFunction(tipObject)){tipObject=tipObject.call(element[0])}if(tipObject.length>0){content=tipObject.clone(true,true)}}else if(tipTarget){targetElement=$("#"+tipTarget);if(targetElement.length>0){content=targetElement.html()}}return content}function getViewportCollisions(coords,elementWidth,elementHeight){var viewportTop=session.scrollTop,viewportLeft=session.scrollLeft,viewportBottom=viewportTop+session.windowHeight,viewportRight=viewportLeft+session.windowWidth,collisions=Collision.none;if(coords.topviewportBottom||Math.abs(coords.bottom-session.windowHeight)>viewportBottom){collisions|=Collision.bottom}if(coords.leftviewportRight){collisions|=Collision.left}if(coords.left+elementWidth>viewportRight||coords.right1)){a.preventDefault();var c=a.originalEvent.changedTouches[0],d=document.createEvent("MouseEvents");d.initMouseEvent(b,!0,!0,window,1,c.screenX,c.screenY,c.clientX,c.clientY,!1,!1,!1,!1,0,null),a.target.dispatchEvent(d)}}if(a.support.touch="ontouchend"in document,a.support.touch){var e,b=a.ui.mouse.prototype,c=b._mouseInit,d=b._mouseDestroy;b._touchStart=function(a){var b=this;!e&&b._mouseCapture(a.originalEvent.changedTouches[0])&&(e=!0,b._touchMoved=!1,f(a,"mouseover"),f(a,"mousemove"),f(a,"mousedown"))},b._touchMove=function(a){e&&(this._touchMoved=!0,f(a,"mousemove"))},b._touchEnd=function(a){e&&(f(a,"mouseup"),f(a,"mouseout"),this._touchMoved||f(a,"click"),e=!1)},b._mouseInit=function(){var b=this;b.element.bind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),c.call(b)},b._mouseDestroy=function(){var b=this;b.element.unbind({touchstart:a.proxy(b,"_touchStart"),touchmove:a.proxy(b,"_touchMove"),touchend:a.proxy(b,"_touchEnd")}),d.call(b)}}}(jQuery);/*! SmartMenus jQuery Plugin - v1.1.0 - September 17, 2017 - * http://www.smartmenus.org/ - * Copyright Vasil Dinkov, Vadikom Web Ltd. http://vadikom.com; Licensed MIT */(function(t){"function"==typeof define&&define.amd?define(["jquery"],t):"object"==typeof module&&"object"==typeof module.exports?module.exports=t(require("jquery")):t(jQuery)})(function($){function initMouseDetection(t){var e=".smartmenus_mouse";if(mouseDetectionEnabled||t)mouseDetectionEnabled&&t&&($(document).off(e),mouseDetectionEnabled=!1);else{var i=!0,s=null,o={mousemove:function(t){var e={x:t.pageX,y:t.pageY,timeStamp:(new Date).getTime()};if(s){var o=Math.abs(s.x-e.x),a=Math.abs(s.y-e.y);if((o>0||a>0)&&2>=o&&2>=a&&300>=e.timeStamp-s.timeStamp&&(mouse=!0,i)){var n=$(t.target).closest("a");n.is("a")&&$.each(menuTrees,function(){return $.contains(this.$root[0],n[0])?(this.itemEnter({currentTarget:n[0]}),!1):void 0}),i=!1}}s=e}};o[touchEvents?"touchstart":"pointerover pointermove pointerout MSPointerOver MSPointerMove MSPointerOut"]=function(t){isTouchEvent(t.originalEvent)&&(mouse=!1)},$(document).on(getEventsNS(o,e)),mouseDetectionEnabled=!0}}function isTouchEvent(t){return!/^(4|mouse)$/.test(t.pointerType)}function getEventsNS(t,e){e||(e="");var i={};for(var s in t)i[s.split(" ").join(e+" ")+e]=t[s];return i}var menuTrees=[],mouse=!1,touchEvents="ontouchstart"in window,mouseDetectionEnabled=!1,requestAnimationFrame=window.requestAnimationFrame||function(t){return setTimeout(t,1e3/60)},cancelAnimationFrame=window.cancelAnimationFrame||function(t){clearTimeout(t)},canAnimate=!!$.fn.animate;return $.SmartMenus=function(t,e){this.$root=$(t),this.opts=e,this.rootId="",this.accessIdPrefix="",this.$subArrow=null,this.activatedItems=[],this.visibleSubMenus=[],this.showTimeout=0,this.hideTimeout=0,this.scrollTimeout=0,this.clickActivated=!1,this.focusActivated=!1,this.zIndexInc=0,this.idInc=0,this.$firstLink=null,this.$firstSub=null,this.disabled=!1,this.$disableOverlay=null,this.$touchScrollingSub=null,this.cssTransforms3d="perspective"in t.style||"webkitPerspective"in t.style,this.wasCollapsible=!1,this.init()},$.extend($.SmartMenus,{hideAll:function(){$.each(menuTrees,function(){this.menuHideAll()})},destroy:function(){for(;menuTrees.length;)menuTrees[0].destroy();initMouseDetection(!0)},prototype:{init:function(t){var e=this;if(!t){menuTrees.push(this),this.rootId=((new Date).getTime()+Math.random()+"").replace(/\D/g,""),this.accessIdPrefix="sm-"+this.rootId+"-",this.$root.hasClass("sm-rtl")&&(this.opts.rightToLeftSubMenus=!0);var i=".smartmenus";this.$root.data("smartmenus",this).attr("data-smartmenus-id",this.rootId).dataSM("level",1).on(getEventsNS({"mouseover focusin":$.proxy(this.rootOver,this),"mouseout focusout":$.proxy(this.rootOut,this),keydown:$.proxy(this.rootKeyDown,this)},i)).on(getEventsNS({mouseenter:$.proxy(this.itemEnter,this),mouseleave:$.proxy(this.itemLeave,this),mousedown:$.proxy(this.itemDown,this),focus:$.proxy(this.itemFocus,this),blur:$.proxy(this.itemBlur,this),click:$.proxy(this.itemClick,this)},i),"a"),i+=this.rootId,this.opts.hideOnClick&&$(document).on(getEventsNS({touchstart:$.proxy(this.docTouchStart,this),touchmove:$.proxy(this.docTouchMove,this),touchend:$.proxy(this.docTouchEnd,this),click:$.proxy(this.docClick,this)},i)),$(window).on(getEventsNS({"resize orientationchange":$.proxy(this.winResize,this)},i)),this.opts.subIndicators&&(this.$subArrow=$("").addClass("sub-arrow"),this.opts.subIndicatorsText&&this.$subArrow.html(this.opts.subIndicatorsText)),initMouseDetection()}if(this.$firstSub=this.$root.find("ul").each(function(){e.menuInit($(this))}).eq(0),this.$firstLink=this.$root.find("a").eq(0),this.opts.markCurrentItem){var s=/(index|default)\.[^#\?\/]*/i,o=/#.*/,a=window.location.href.replace(s,""),n=a.replace(o,"");this.$root.find("a").each(function(){var t=this.href.replace(s,""),i=$(this);(t==a||t==n)&&(i.addClass("current"),e.opts.markCurrentTree&&i.parentsUntil("[data-smartmenus-id]","ul").each(function(){$(this).dataSM("parent-a").addClass("current")}))})}this.wasCollapsible=this.isCollapsible()},destroy:function(t){if(!t){var e=".smartmenus";this.$root.removeData("smartmenus").removeAttr("data-smartmenus-id").removeDataSM("level").off(e),e+=this.rootId,$(document).off(e),$(window).off(e),this.opts.subIndicators&&(this.$subArrow=null)}this.menuHideAll();var i=this;this.$root.find("ul").each(function(){var t=$(this);t.dataSM("scroll-arrows")&&t.dataSM("scroll-arrows").remove(),t.dataSM("shown-before")&&((i.opts.subMenusMinWidth||i.opts.subMenusMaxWidth)&&t.css({width:"",minWidth:"",maxWidth:""}).removeClass("sm-nowrap"),t.dataSM("scroll-arrows")&&t.dataSM("scroll-arrows").remove(),t.css({zIndex:"",top:"",left:"",marginLeft:"",marginTop:"",display:""})),0==(t.attr("id")||"").indexOf(i.accessIdPrefix)&&t.removeAttr("id")}).removeDataSM("in-mega").removeDataSM("shown-before").removeDataSM("scroll-arrows").removeDataSM("parent-a").removeDataSM("level").removeDataSM("beforefirstshowfired").removeAttr("role").removeAttr("aria-hidden").removeAttr("aria-labelledby").removeAttr("aria-expanded"),this.$root.find("a.has-submenu").each(function(){var t=$(this);0==t.attr("id").indexOf(i.accessIdPrefix)&&t.removeAttr("id")}).removeClass("has-submenu").removeDataSM("sub").removeAttr("aria-haspopup").removeAttr("aria-controls").removeAttr("aria-expanded").closest("li").removeDataSM("sub"),this.opts.subIndicators&&this.$root.find("span.sub-arrow").remove(),this.opts.markCurrentItem&&this.$root.find("a.current").removeClass("current"),t||(this.$root=null,this.$firstLink=null,this.$firstSub=null,this.$disableOverlay&&(this.$disableOverlay.remove(),this.$disableOverlay=null),menuTrees.splice($.inArray(this,menuTrees),1))},disable:function(t){if(!this.disabled){if(this.menuHideAll(),!t&&!this.opts.isPopup&&this.$root.is(":visible")){var e=this.$root.offset();this.$disableOverlay=$('
').css({position:"absolute",top:e.top,left:e.left,width:this.$root.outerWidth(),height:this.$root.outerHeight(),zIndex:this.getStartZIndex(!0),opacity:0}).appendTo(document.body)}this.disabled=!0}},docClick:function(t){return this.$touchScrollingSub?(this.$touchScrollingSub=null,void 0):((this.visibleSubMenus.length&&!$.contains(this.$root[0],t.target)||$(t.target).closest("a").length)&&this.menuHideAll(),void 0)},docTouchEnd:function(){if(this.lastTouch){if(!(!this.visibleSubMenus.length||void 0!==this.lastTouch.x2&&this.lastTouch.x1!=this.lastTouch.x2||void 0!==this.lastTouch.y2&&this.lastTouch.y1!=this.lastTouch.y2||this.lastTouch.target&&$.contains(this.$root[0],this.lastTouch.target))){this.hideTimeout&&(clearTimeout(this.hideTimeout),this.hideTimeout=0);var t=this;this.hideTimeout=setTimeout(function(){t.menuHideAll()},350)}this.lastTouch=null}},docTouchMove:function(t){if(this.lastTouch){var e=t.originalEvent.touches[0];this.lastTouch.x2=e.pageX,this.lastTouch.y2=e.pageY}},docTouchStart:function(t){var e=t.originalEvent.touches[0];this.lastTouch={x1:e.pageX,y1:e.pageY,target:e.target}},enable:function(){this.disabled&&(this.$disableOverlay&&(this.$disableOverlay.remove(),this.$disableOverlay=null),this.disabled=!1)},getClosestMenu:function(t){for(var e=$(t).closest("ul");e.dataSM("in-mega");)e=e.parent().closest("ul");return e[0]||null},getHeight:function(t){return this.getOffset(t,!0)},getOffset:function(t,e){var i;"none"==t.css("display")&&(i={position:t[0].style.position,visibility:t[0].style.visibility},t.css({position:"absolute",visibility:"hidden"}).show());var s=t[0].getBoundingClientRect&&t[0].getBoundingClientRect(),o=s&&(e?s.height||s.bottom-s.top:s.width||s.right-s.left);return o||0===o||(o=e?t[0].offsetHeight:t[0].offsetWidth),i&&t.hide().css(i),o},getStartZIndex:function(t){var e=parseInt(this[t?"$root":"$firstSub"].css("z-index"));return!t&&isNaN(e)&&(e=parseInt(this.$root.css("z-index"))),isNaN(e)?1:e},getTouchPoint:function(t){return t.touches&&t.touches[0]||t.changedTouches&&t.changedTouches[0]||t},getViewport:function(t){var e=t?"Height":"Width",i=document.documentElement["client"+e],s=window["inner"+e];return s&&(i=Math.min(i,s)),i},getViewportHeight:function(){return this.getViewport(!0)},getViewportWidth:function(){return this.getViewport()},getWidth:function(t){return this.getOffset(t)},handleEvents:function(){return!this.disabled&&this.isCSSOn()},handleItemEvents:function(t){return this.handleEvents()&&!this.isLinkInMegaMenu(t)},isCollapsible:function(){return"static"==this.$firstSub.css("position")},isCSSOn:function(){return"inline"!=this.$firstLink.css("display")},isFixed:function(){var t="fixed"==this.$root.css("position");return t||this.$root.parentsUntil("body").each(function(){return"fixed"==$(this).css("position")?(t=!0,!1):void 0}),t},isLinkInMegaMenu:function(t){return $(this.getClosestMenu(t[0])).hasClass("mega-menu")},isTouchMode:function(){return!mouse||this.opts.noMouseOver||this.isCollapsible()},itemActivate:function(t,e){var i=t.closest("ul"),s=i.dataSM("level");if(s>1&&(!this.activatedItems[s-2]||this.activatedItems[s-2][0]!=i.dataSM("parent-a")[0])){var o=this;$(i.parentsUntil("[data-smartmenus-id]","ul").get().reverse()).add(i).each(function(){o.itemActivate($(this).dataSM("parent-a"))})}if((!this.isCollapsible()||e)&&this.menuHideSubMenus(this.activatedItems[s-1]&&this.activatedItems[s-1][0]==t[0]?s:s-1),this.activatedItems[s-1]=t,this.$root.triggerHandler("activate.smapi",t[0])!==!1){var a=t.dataSM("sub");a&&(this.isTouchMode()||!this.opts.showOnClick||this.clickActivated)&&this.menuShow(a)}},itemBlur:function(t){var e=$(t.currentTarget);this.handleItemEvents(e)&&this.$root.triggerHandler("blur.smapi",e[0])},itemClick:function(t){var e=$(t.currentTarget);if(this.handleItemEvents(e)){if(this.$touchScrollingSub&&this.$touchScrollingSub[0]==e.closest("ul")[0])return this.$touchScrollingSub=null,t.stopPropagation(),!1;if(this.$root.triggerHandler("click.smapi",e[0])===!1)return!1;var i=$(t.target).is(".sub-arrow"),s=e.dataSM("sub"),o=s?2==s.dataSM("level"):!1,a=this.isCollapsible(),n=/toggle$/.test(this.opts.collapsibleBehavior),r=/link$/.test(this.opts.collapsibleBehavior),h=/^accordion/.test(this.opts.collapsibleBehavior);if(s&&!s.is(":visible")){if((!r||!a||i)&&(this.opts.showOnClick&&o&&(this.clickActivated=!0),this.itemActivate(e,h),s.is(":visible")))return this.focusActivated=!0,!1}else if(a&&(n||i))return this.itemActivate(e,h),this.menuHide(s),n&&(this.focusActivated=!1),!1;return this.opts.showOnClick&&o||e.hasClass("disabled")||this.$root.triggerHandler("select.smapi",e[0])===!1?!1:void 0}},itemDown:function(t){var e=$(t.currentTarget);this.handleItemEvents(e)&&e.dataSM("mousedown",!0)},itemEnter:function(t){var e=$(t.currentTarget);if(this.handleItemEvents(e)){if(!this.isTouchMode()){this.showTimeout&&(clearTimeout(this.showTimeout),this.showTimeout=0);var i=this;this.showTimeout=setTimeout(function(){i.itemActivate(e)},this.opts.showOnClick&&1==e.closest("ul").dataSM("level")?1:this.opts.showTimeout)}this.$root.triggerHandler("mouseenter.smapi",e[0])}},itemFocus:function(t){var e=$(t.currentTarget);this.handleItemEvents(e)&&(!this.focusActivated||this.isTouchMode()&&e.dataSM("mousedown")||this.activatedItems.length&&this.activatedItems[this.activatedItems.length-1][0]==e[0]||this.itemActivate(e,!0),this.$root.triggerHandler("focus.smapi",e[0]))},itemLeave:function(t){var e=$(t.currentTarget);this.handleItemEvents(e)&&(this.isTouchMode()||(e[0].blur(),this.showTimeout&&(clearTimeout(this.showTimeout),this.showTimeout=0)),e.removeDataSM("mousedown"),this.$root.triggerHandler("mouseleave.smapi",e[0]))},menuHide:function(t){if(this.$root.triggerHandler("beforehide.smapi",t[0])!==!1&&(canAnimate&&t.stop(!0,!0),"none"!=t.css("display"))){var e=function(){t.css("z-index","")};this.isCollapsible()?canAnimate&&this.opts.collapsibleHideFunction?this.opts.collapsibleHideFunction.call(this,t,e):t.hide(this.opts.collapsibleHideDuration,e):canAnimate&&this.opts.hideFunction?this.opts.hideFunction.call(this,t,e):t.hide(this.opts.hideDuration,e),t.dataSM("scroll")&&(this.menuScrollStop(t),t.css({"touch-action":"","-ms-touch-action":"","-webkit-transform":"",transform:""}).off(".smartmenus_scroll").removeDataSM("scroll").dataSM("scroll-arrows").hide()),t.dataSM("parent-a").removeClass("highlighted").attr("aria-expanded","false"),t.attr({"aria-expanded":"false","aria-hidden":"true"});var i=t.dataSM("level");this.activatedItems.splice(i-1,1),this.visibleSubMenus.splice($.inArray(t,this.visibleSubMenus),1),this.$root.triggerHandler("hide.smapi",t[0])}},menuHideAll:function(){this.showTimeout&&(clearTimeout(this.showTimeout),this.showTimeout=0);for(var t=this.opts.isPopup?1:0,e=this.visibleSubMenus.length-1;e>=t;e--)this.menuHide(this.visibleSubMenus[e]);this.opts.isPopup&&(canAnimate&&this.$root.stop(!0,!0),this.$root.is(":visible")&&(canAnimate&&this.opts.hideFunction?this.opts.hideFunction.call(this,this.$root):this.$root.hide(this.opts.hideDuration))),this.activatedItems=[],this.visibleSubMenus=[],this.clickActivated=!1,this.focusActivated=!1,this.zIndexInc=0,this.$root.triggerHandler("hideAll.smapi")},menuHideSubMenus:function(t){for(var e=this.activatedItems.length-1;e>=t;e--){var i=this.activatedItems[e].dataSM("sub");i&&this.menuHide(i)}},menuInit:function(t){if(!t.dataSM("in-mega")){t.hasClass("mega-menu")&&t.find("ul").dataSM("in-mega",!0);for(var e=2,i=t[0];(i=i.parentNode.parentNode)!=this.$root[0];)e++;var s=t.prevAll("a").eq(-1);s.length||(s=t.prevAll().find("a").eq(-1)),s.addClass("has-submenu").dataSM("sub",t),t.dataSM("parent-a",s).dataSM("level",e).parent().dataSM("sub",t);var o=s.attr("id")||this.accessIdPrefix+ ++this.idInc,a=t.attr("id")||this.accessIdPrefix+ ++this.idInc;s.attr({id:o,"aria-haspopup":"true","aria-controls":a,"aria-expanded":"false"}),t.attr({id:a,role:"group","aria-hidden":"true","aria-labelledby":o,"aria-expanded":"false"}),this.opts.subIndicators&&s[this.opts.subIndicatorsPos](this.$subArrow.clone())}},menuPosition:function(t){var e,i,s=t.dataSM("parent-a"),o=s.closest("li"),a=o.parent(),n=t.dataSM("level"),r=this.getWidth(t),h=this.getHeight(t),u=s.offset(),l=u.left,c=u.top,d=this.getWidth(s),m=this.getHeight(s),p=$(window),f=p.scrollLeft(),v=p.scrollTop(),b=this.getViewportWidth(),S=this.getViewportHeight(),g=a.parent().is("[data-sm-horizontal-sub]")||2==n&&!a.hasClass("sm-vertical"),M=this.opts.rightToLeftSubMenus&&!o.is("[data-sm-reverse]")||!this.opts.rightToLeftSubMenus&&o.is("[data-sm-reverse]"),w=2==n?this.opts.mainMenuSubOffsetX:this.opts.subMenusSubOffsetX,T=2==n?this.opts.mainMenuSubOffsetY:this.opts.subMenusSubOffsetY;if(g?(e=M?d-r-w:w,i=this.opts.bottomToTopSubMenus?-h-T:m+T):(e=M?w-r:d-w,i=this.opts.bottomToTopSubMenus?m-T-h:T),this.opts.keepInViewport){var y=l+e,I=c+i;if(M&&f>y?e=g?f-y+e:d-w:!M&&y+r>f+b&&(e=g?f+b-r-y+e:w-r),g||(S>h&&I+h>v+S?i+=v+S-h-I:(h>=S||v>I)&&(i+=v-I)),g&&(I+h>v+S+.49||v>I)||!g&&h>S+.49){var x=this;t.dataSM("scroll-arrows")||t.dataSM("scroll-arrows",$([$('')[0],$('')[0]]).on({mouseenter:function(){t.dataSM("scroll").up=$(this).hasClass("scroll-up"),x.menuScroll(t)},mouseleave:function(e){x.menuScrollStop(t),x.menuScrollOut(t,e)},"mousewheel DOMMouseScroll":function(t){t.preventDefault()}}).insertAfter(t));var A=".smartmenus_scroll";if(t.dataSM("scroll",{y:this.cssTransforms3d?0:i-m,step:1,itemH:m,subH:h,arrowDownH:this.getHeight(t.dataSM("scroll-arrows").eq(1))}).on(getEventsNS({mouseover:function(e){x.menuScrollOver(t,e)},mouseout:function(e){x.menuScrollOut(t,e)},"mousewheel DOMMouseScroll":function(e){x.menuScrollMousewheel(t,e)}},A)).dataSM("scroll-arrows").css({top:"auto",left:"0",marginLeft:e+(parseInt(t.css("border-left-width"))||0),width:r-(parseInt(t.css("border-left-width"))||0)-(parseInt(t.css("border-right-width"))||0),zIndex:t.css("z-index")}).eq(g&&this.opts.bottomToTopSubMenus?0:1).show(),this.isFixed()){var C={};C[touchEvents?"touchstart touchmove touchend":"pointerdown pointermove pointerup MSPointerDown MSPointerMove MSPointerUp"]=function(e){x.menuScrollTouch(t,e)},t.css({"touch-action":"none","-ms-touch-action":"none"}).on(getEventsNS(C,A))}}}t.css({top:"auto",left:"0",marginLeft:e,marginTop:i-m})},menuScroll:function(t,e,i){var s,o=t.dataSM("scroll"),a=t.dataSM("scroll-arrows"),n=o.up?o.upEnd:o.downEnd;if(!e&&o.momentum){if(o.momentum*=.92,s=o.momentum,.5>s)return this.menuScrollStop(t),void 0}else s=i||(e||!this.opts.scrollAccelerate?this.opts.scrollStep:Math.floor(o.step));var r=t.dataSM("level");if(this.activatedItems[r-1]&&this.activatedItems[r-1].dataSM("sub")&&this.activatedItems[r-1].dataSM("sub").is(":visible")&&this.menuHideSubMenus(r-1),o.y=o.up&&o.y>=n||!o.up&&n>=o.y?o.y:Math.abs(n-o.y)>s?o.y+(o.up?s:-s):n,t.css(this.cssTransforms3d?{"-webkit-transform":"translate3d(0, "+o.y+"px, 0)",transform:"translate3d(0, "+o.y+"px, 0)"}:{marginTop:o.y}),mouse&&(o.up&&o.y>o.downEnd||!o.up&&o.y0;t.dataSM("scroll-arrows").eq(i?0:1).is(":visible")&&(t.dataSM("scroll").up=i,this.menuScroll(t,!0))}e.preventDefault()},menuScrollOut:function(t,e){mouse&&(/^scroll-(up|down)/.test((e.relatedTarget||"").className)||(t[0]==e.relatedTarget||$.contains(t[0],e.relatedTarget))&&this.getClosestMenu(e.relatedTarget)==t[0]||t.dataSM("scroll-arrows").css("visibility","hidden"))},menuScrollOver:function(t,e){if(mouse&&!/^scroll-(up|down)/.test(e.target.className)&&this.getClosestMenu(e.target)==t[0]){this.menuScrollRefreshData(t);var i=t.dataSM("scroll"),s=$(window).scrollTop()-t.dataSM("parent-a").offset().top-i.itemH;t.dataSM("scroll-arrows").eq(0).css("margin-top",s).end().eq(1).css("margin-top",s+this.getViewportHeight()-i.arrowDownH).end().css("visibility","visible")}},menuScrollRefreshData:function(t){var e=t.dataSM("scroll"),i=$(window).scrollTop()-t.dataSM("parent-a").offset().top-e.itemH;this.cssTransforms3d&&(i=-(parseFloat(t.css("margin-top"))-i)),$.extend(e,{upEnd:i,downEnd:i+this.getViewportHeight()-e.subH})},menuScrollStop:function(t){return this.scrollTimeout?(cancelAnimationFrame(this.scrollTimeout),this.scrollTimeout=0,t.dataSM("scroll").step=1,!0):void 0},menuScrollTouch:function(t,e){if(e=e.originalEvent,isTouchEvent(e)){var i=this.getTouchPoint(e);if(this.getClosestMenu(i.target)==t[0]){var s=t.dataSM("scroll");if(/(start|down)$/i.test(e.type))this.menuScrollStop(t)?(e.preventDefault(),this.$touchScrollingSub=t):this.$touchScrollingSub=null,this.menuScrollRefreshData(t),$.extend(s,{touchStartY:i.pageY,touchStartTime:e.timeStamp});else if(/move$/i.test(e.type)){var o=void 0!==s.touchY?s.touchY:s.touchStartY;if(void 0!==o&&o!=i.pageY){this.$touchScrollingSub=t;var a=i.pageY>o;void 0!==s.up&&s.up!=a&&$.extend(s,{touchStartY:i.pageY,touchStartTime:e.timeStamp}),$.extend(s,{up:a,touchY:i.pageY}),this.menuScroll(t,!0,Math.abs(i.pageY-o))}e.preventDefault()}else void 0!==s.touchY&&((s.momentum=15*Math.pow(Math.abs(i.pageY-s.touchStartY)/(e.timeStamp-s.touchStartTime),2))&&(this.menuScrollStop(t),this.menuScroll(t),e.preventDefault()),delete s.touchY)}}},menuShow:function(t){if((t.dataSM("beforefirstshowfired")||(t.dataSM("beforefirstshowfired",!0),this.$root.triggerHandler("beforefirstshow.smapi",t[0])!==!1))&&this.$root.triggerHandler("beforeshow.smapi",t[0])!==!1&&(t.dataSM("shown-before",!0),canAnimate&&t.stop(!0,!0),!t.is(":visible"))){var e=t.dataSM("parent-a"),i=this.isCollapsible();if((this.opts.keepHighlighted||i)&&e.addClass("highlighted"),i)t.removeClass("sm-nowrap").css({zIndex:"",width:"auto",minWidth:"",maxWidth:"",top:"",left:"",marginLeft:"",marginTop:""});else{if(t.css("z-index",this.zIndexInc=(this.zIndexInc||this.getStartZIndex())+1),(this.opts.subMenusMinWidth||this.opts.subMenusMaxWidth)&&(t.css({width:"auto",minWidth:"",maxWidth:""}).addClass("sm-nowrap"),this.opts.subMenusMinWidth&&t.css("min-width",this.opts.subMenusMinWidth),this.opts.subMenusMaxWidth)){var s=this.getWidth(t);t.css("max-width",this.opts.subMenusMaxWidth),s>this.getWidth(t)&&t.removeClass("sm-nowrap").css("width",this.opts.subMenusMaxWidth)}this.menuPosition(t)}var o=function(){t.css("overflow","")};i?canAnimate&&this.opts.collapsibleShowFunction?this.opts.collapsibleShowFunction.call(this,t,o):t.show(this.opts.collapsibleShowDuration,o):canAnimate&&this.opts.showFunction?this.opts.showFunction.call(this,t,o):t.show(this.opts.showDuration,o),e.attr("aria-expanded","true"),t.attr({"aria-expanded":"true","aria-hidden":"false"}),this.visibleSubMenus.push(t),this.$root.triggerHandler("show.smapi",t[0])}},popupHide:function(t){this.hideTimeout&&(clearTimeout(this.hideTimeout),this.hideTimeout=0);var e=this;this.hideTimeout=setTimeout(function(){e.menuHideAll()},t?1:this.opts.hideTimeout)},popupShow:function(t,e){if(!this.opts.isPopup)return alert('SmartMenus jQuery Error:\n\nIf you want to show this menu via the "popupShow" method, set the isPopup:true option.'),void 0;if(this.hideTimeout&&(clearTimeout(this.hideTimeout),this.hideTimeout=0),this.$root.dataSM("shown-before",!0),canAnimate&&this.$root.stop(!0,!0),!this.$root.is(":visible")){this.$root.css({left:t,top:e});var i=this,s=function(){i.$root.css("overflow","")};canAnimate&&this.opts.showFunction?this.opts.showFunction.call(this,this.$root,s):this.$root.show(this.opts.showDuration,s),this.visibleSubMenus[0]=this.$root}},refresh:function(){this.destroy(!0),this.init(!0)},rootKeyDown:function(t){if(this.handleEvents())switch(t.keyCode){case 27:var e=this.activatedItems[0];if(e){this.menuHideAll(),e[0].focus();var i=e.dataSM("sub");i&&this.menuHide(i)}break;case 32:var s=$(t.target);if(s.is("a")&&this.handleItemEvents(s)){var i=s.dataSM("sub");i&&!i.is(":visible")&&(this.itemClick({currentTarget:t.target}),t.preventDefault())}}},rootOut:function(t){if(this.handleEvents()&&!this.isTouchMode()&&t.target!=this.$root[0]&&(this.hideTimeout&&(clearTimeout(this.hideTimeout),this.hideTimeout=0),!this.opts.showOnClick||!this.opts.hideOnClick)){var e=this;this.hideTimeout=setTimeout(function(){e.menuHideAll()},this.opts.hideTimeout)}},rootOver:function(t){this.handleEvents()&&!this.isTouchMode()&&t.target!=this.$root[0]&&this.hideTimeout&&(clearTimeout(this.hideTimeout),this.hideTimeout=0)},winResize:function(t){if(this.handleEvents()){if(!("onorientationchange"in window)||"orientationchange"==t.type){var e=this.isCollapsible();this.wasCollapsible&&e||(this.activatedItems.length&&this.activatedItems[this.activatedItems.length-1][0].blur(),this.menuHideAll()),this.wasCollapsible=e}}else if(this.$disableOverlay){var i=this.$root.offset();this.$disableOverlay.css({top:i.top,left:i.left,width:this.$root.outerWidth(),height:this.$root.outerHeight()})}}}}),$.fn.dataSM=function(t,e){return e?this.data(t+"_smartmenus",e):this.data(t+"_smartmenus")},$.fn.removeDataSM=function(t){return this.removeData(t+"_smartmenus")},$.fn.smartmenus=function(options){if("string"==typeof options){var args=arguments,method=options;return Array.prototype.shift.call(args),this.each(function(){var t=$(this).data("smartmenus");t&&t[method]&&t[method].apply(t,args)})}return this.each(function(){var dataOpts=$(this).data("sm-options")||null;if(dataOpts)try{dataOpts=eval("("+dataOpts+")")}catch(e){dataOpts=null,alert('ERROR\n\nSmartMenus jQuery init:\nInvalid "data-sm-options" attribute value syntax.')}new $.SmartMenus(this,$.extend({},$.fn.smartmenus.defaults,options,dataOpts))})},$.fn.smartmenus.defaults={isPopup:!1,mainMenuSubOffsetX:0,mainMenuSubOffsetY:0,subMenusSubOffsetX:0,subMenusSubOffsetY:0,subMenusMinWidth:"10em",subMenusMaxWidth:"20em",subIndicators:!0,subIndicatorsPos:"append",subIndicatorsText:"",scrollStep:30,scrollAccelerate:!0,showTimeout:250,hideTimeout:500,showDuration:0,showFunction:null,hideDuration:0,hideFunction:function(t,e){t.fadeOut(200,e)},collapsibleShowDuration:0,collapsibleShowFunction:function(t,e){t.slideDown(200,e)},collapsibleHideDuration:0,collapsibleHideFunction:function(t,e){t.slideUp(200,e)},showOnClick:!1,hideOnClick:!0,noMouseOver:!1,keepInViewport:!0,keepHighlighted:!0,markCurrentItem:!1,markCurrentTree:!0,rightToLeftSubMenus:!1,bottomToTopSubMenus:!1,collapsibleBehavior:"default"},$}); \ No newline at end of file diff --git a/docs/html/jvm_8h_source.html b/docs/html/jvm_8h_source.html deleted file mode 100644 index 631d46d7..00000000 --- a/docs/html/jvm_8h_source.html +++ /dev/null @@ -1,101 +0,0 @@ - - - - - - - -binlex: include/jvm.h Source File - - - - - - - - - - - - - - -
-
- - - - - - -
-
binlex -
-
-
- - - - - - - -
-
- -
-
-
- -
- -
-
- - -
- -
- -
-
-
jvm.h
-
-
-
-
- - - - diff --git a/docs/html/menu.js b/docs/html/menu.js deleted file mode 100644 index 433c15b8..00000000 --- a/docs/html/menu.js +++ /dev/null @@ -1,50 +0,0 @@ -/* - @licstart The following is the entire license notice for the - JavaScript code in this file. - - Copyright (C) 1997-2017 by Dimitri van Heesch - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 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 General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - @licend The above is the entire license notice - for the JavaScript code in this file - */ -function initMenu(relPath,searchEnabled,serverSide,searchPage,search) { - function makeTree(data,relPath) { - var result=''; - if ('children' in data) { - result+=''; - } - return result; - } - - $('#main-nav').append(makeTree(menudata,relPath)); - $('#main-nav').children(':first').addClass('sm sm-dox').attr('id','main-menu'); - if (searchEnabled) { - if (serverSide) { - $('#main-menu').append('
  • '); - } else { - $('#main-menu').append('
  • '); - } - } - $('#main-menu').smartmenus(); -} -/* @license-end */ diff --git a/docs/html/menudata.js b/docs/html/menudata.js deleted file mode 100644 index b7abd1fe..00000000 --- a/docs/html/menudata.js +++ /dev/null @@ -1,65 +0,0 @@ -/* -@licstart The following is the entire license notice for the -JavaScript code in this file. - -Copyright (C) 1997-2019 by Dimitri van Heesch - -This program is free software; you can redistribute it and/or modify -it under the terms of version 2 of the GNU General Public License as published by -the Free Software Foundation - -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 General Public License for more details. - -You should have received a copy of the GNU General Public License along -with this program; if not, write to the Free Software Foundation, Inc., -51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - -@licend The above is the entire license notice -for the JavaScript code in this file -*/ -var menudata={children:[ -{text:"Main Page",url:"index.html"}, -{text:"Namespaces",url:"namespaces.html",children:[ -{text:"Namespace List",url:"namespaces.html"}]}, -{text:"Classes",url:"annotated.html",children:[ -{text:"Class List",url:"annotated.html"}, -{text:"Class Index",url:"classes.html"}, -{text:"Class Hierarchy",url:"hierarchy.html"}, -{text:"Class Members",url:"functions.html",children:[ -{text:"All",url:"functions.html",children:[ -{text:"a",url:"functions.html#index_a"}, -{text:"c",url:"functions.html#index_c"}, -{text:"d",url:"functions.html#index_d"}, -{text:"e",url:"functions.html#index_e"}, -{text:"f",url:"functions.html#index_f"}, -{text:"g",url:"functions.html#index_g"}, -{text:"h",url:"functions.html#index_h"}, -{text:"i",url:"functions.html#index_i"}, -{text:"l",url:"functions.html#index_l"}, -{text:"m",url:"functions.html#index_m"}, -{text:"p",url:"functions.html#index_p"}, -{text:"r",url:"functions.html#index_r"}, -{text:"s",url:"functions.html#index_s"}, -{text:"t",url:"functions.html#index_t"}, -{text:"w",url:"functions.html#index_w"}]}, -{text:"Functions",url:"functions_func.html",children:[ -{text:"a",url:"functions_func.html#index_a"}, -{text:"c",url:"functions_func.html#index_c"}, -{text:"d",url:"functions_func.html#index_d"}, -{text:"e",url:"functions_func.html#index_e"}, -{text:"g",url:"functions_func.html#index_g"}, -{text:"h",url:"functions_func.html#index_h"}, -{text:"i",url:"functions_func.html#index_i"}, -{text:"l",url:"functions_func.html#index_l"}, -{text:"m",url:"functions_func.html#index_m"}, -{text:"p",url:"functions_func.html#index_p"}, -{text:"r",url:"functions_func.html#index_r"}, -{text:"s",url:"functions_func.html#index_s"}, -{text:"t",url:"functions_func.html#index_t"}, -{text:"w",url:"functions_func.html#index_w"}]}, -{text:"Variables",url:"functions_vars.html"}]}]}, -{text:"Files",url:"files.html",children:[ -{text:"File List",url:"files.html"}]}]} diff --git a/docs/html/namespacebinlex.html b/docs/html/namespacebinlex.html deleted file mode 100644 index c478b956..00000000 --- a/docs/html/namespacebinlex.html +++ /dev/null @@ -1,136 +0,0 @@ - - - - - - - -binlex: binlex Namespace Reference - - - - - - - - - - - - - - -
    -
    - - - - - - -
    -
    binlex -
    -
    -
    - - - - - - - -
    -
    - -
    -
    -
    - -
    - -
    -
    - - -
    - -
    - -
    - -
    -
    binlex Namespace Reference
    -
    -
    - -

    the binlex namespace -More...

    - - - - - - - - - - - - - - - - - - - - - - - - - - -

    -Classes

    class  Args
     
    class  AutoLex
     
    class  CILDecompiler
     
    class  Common
     
    class  Decompiler
     
    class  DecompilerBase
     
    class  DOTNET
     
    class  ELF
     
    class  File
     
    class  PE
     
    class  Raw
     
    class  TimedCode
     
    -

    Detailed Description

    -

    the binlex namespace

    -
    -
    - - - - diff --git a/docs/html/namespacebinlex.js b/docs/html/namespacebinlex.js deleted file mode 100644 index 7aa02dd2..00000000 --- a/docs/html/namespacebinlex.js +++ /dev/null @@ -1,15 +0,0 @@ -var namespacebinlex = -[ - [ "Args", "classbinlex_1_1Args.html", "classbinlex_1_1Args" ], - [ "AutoLex", "classbinlex_1_1AutoLex.html", "classbinlex_1_1AutoLex" ], - [ "CILDecompiler", "classbinlex_1_1CILDecompiler.html", "classbinlex_1_1CILDecompiler" ], - [ "Common", "classbinlex_1_1Common.html", null ], - [ "Decompiler", "classbinlex_1_1Decompiler.html", "classbinlex_1_1Decompiler" ], - [ "DecompilerBase", "classbinlex_1_1DecompilerBase.html", "classbinlex_1_1DecompilerBase" ], - [ "DOTNET", "classbinlex_1_1DOTNET.html", "classbinlex_1_1DOTNET" ], - [ "ELF", "classbinlex_1_1ELF.html", "classbinlex_1_1ELF" ], - [ "File", "classbinlex_1_1File.html", "classbinlex_1_1File" ], - [ "PE", "classbinlex_1_1PE.html", "classbinlex_1_1PE" ], - [ "Raw", "classbinlex_1_1Raw.html", "classbinlex_1_1Raw" ], - [ "TimedCode", "classbinlex_1_1TimedCode.html", "classbinlex_1_1TimedCode" ] -]; \ No newline at end of file diff --git a/docs/html/namespaces.html b/docs/html/namespaces.html deleted file mode 100644 index 6d16d116..00000000 --- a/docs/html/namespaces.html +++ /dev/null @@ -1,105 +0,0 @@ - - - - - - - -binlex: Namespace List - - - - - - - - - - - - - - -
    -
    - - - - - - -
    -
    binlex -
    -
    -
    - - - - - - - -
    -
    - -
    -
    -
    - -
    - -
    -
    - - -
    - -
    - -
    -
    -
    Namespace List
    -
    -
    -
    Here is a list of all documented namespaces with brief descriptions:
    - - -
     NbinlexBinlex namespace
    -
    -
    -
    - - - - diff --git a/docs/html/namespaces_dup.js b/docs/html/namespaces_dup.js deleted file mode 100644 index 0cf80e3e..00000000 --- a/docs/html/namespaces_dup.js +++ /dev/null @@ -1,4 +0,0 @@ -var namespaces_dup = -[ - [ "binlex", "namespacebinlex.html", null ] -]; \ No newline at end of file diff --git a/docs/html/nav_f.png b/docs/html/nav_f.png deleted file mode 100644 index 72a58a52..00000000 Binary files a/docs/html/nav_f.png and /dev/null differ diff --git a/docs/html/nav_g.png b/docs/html/nav_g.png deleted file mode 100644 index 2093a237..00000000 Binary files a/docs/html/nav_g.png and /dev/null differ diff --git a/docs/html/nav_h.png b/docs/html/nav_h.png deleted file mode 100644 index 33389b10..00000000 Binary files a/docs/html/nav_h.png and /dev/null differ diff --git a/docs/html/navtree.css b/docs/html/navtree.css deleted file mode 100644 index 33341a67..00000000 --- a/docs/html/navtree.css +++ /dev/null @@ -1,146 +0,0 @@ -#nav-tree .children_ul { - margin:0; - padding:4px; -} - -#nav-tree ul { - list-style:none outside none; - margin:0px; - padding:0px; -} - -#nav-tree li { - white-space:nowrap; - margin:0px; - padding:0px; -} - -#nav-tree .plus { - margin:0px; -} - -#nav-tree .selected { - background-image: url('tab_a.png'); - background-repeat:repeat-x; - color: #fff; - text-shadow: 0px 1px 1px rgba(0, 0, 0, 1.0); -} - -#nav-tree img { - margin:0px; - padding:0px; - border:0px; - vertical-align: middle; -} - -#nav-tree a { - text-decoration:none; - padding:0px; - margin:0px; - outline:none; -} - -#nav-tree .label { - margin:0px; - padding:0px; - font: 12px 'Lucida Grande',Geneva,Helvetica,Arial,sans-serif; -} - -#nav-tree .label a { - padding:2px; -} - -#nav-tree .selected a { - text-decoration:none; - color:#fff; -} - -#nav-tree .children_ul { - margin:0px; - padding:0px; -} - -#nav-tree .item { - margin:0px; - padding:0px; -} - -#nav-tree { - padding: 0px 0px; - background-color: #FAFAFF; - font-size:14px; - overflow:auto; -} - -#doc-content { - overflow:auto; - display:block; - padding:0px; - margin:0px; - -webkit-overflow-scrolling : touch; /* iOS 5+ */ -} - -#side-nav { - padding:0 6px 0 0; - margin: 0px; - display:block; - position: absolute; - left: 0px; - width: 250px; -} - -.ui-resizable .ui-resizable-handle { - display:block; -} - -.ui-resizable-e { - background-image:url("splitbar.png"); - background-size:100%; - background-repeat:repeat-y; - background-attachment: scroll; - cursor:ew-resize; - height:100%; - right:0; - top:0; - width:6px; -} - -.ui-resizable-handle { - display:none; - font-size:0.1px; - position:absolute; - z-index:1; -} - -#nav-tree-contents { - margin: 6px 0px 0px 0px; -} - -#nav-tree { - background-image:url('nav_h.png'); - background-repeat:repeat-x; - background-color: #F9FAFC; - -webkit-overflow-scrolling : touch; /* iOS 5+ */ -} - -#nav-sync { - position:absolute; - top:5px; - right:24px; - z-index:0; -} - -#nav-sync img { - opacity:0.3; -} - -#nav-sync img:hover { - opacity:0.9; -} - -@media print -{ - #nav-tree { display: none; } - div.ui-resizable-handle { display: none; position: relative; } -} - diff --git a/docs/html/navtree.js b/docs/html/navtree.js deleted file mode 100644 index edc31efc..00000000 --- a/docs/html/navtree.js +++ /dev/null @@ -1,544 +0,0 @@ -/* - @licstart The following is the entire license notice for the - JavaScript code in this file. - - Copyright (C) 1997-2019 by Dimitri van Heesch - - This program is free software; you can redistribute it and/or modify - it under the terms of version 2 of the GNU General Public License as - published by the Free Software Foundation. - - 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 General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - @licend The above is the entire license notice - for the JavaScript code in this file - */ -var navTreeSubIndices = new Array(); -var arrowDown = '▼'; -var arrowRight = '►'; - -function getData(varName) -{ - var i = varName.lastIndexOf('/'); - var n = i>=0 ? varName.substring(i+1) : varName; - return eval(n.replace(/\-/g,'_')); -} - -function stripPath(uri) -{ - return uri.substring(uri.lastIndexOf('/')+1); -} - -function stripPath2(uri) -{ - var i = uri.lastIndexOf('/'); - var s = uri.substring(i+1); - var m = uri.substring(0,i+1).match(/\/d\w\/d\w\w\/$/); - return m ? uri.substring(i-6) : s; -} - -function hashValue() -{ - return $(location).attr('hash').substring(1).replace(/[^\w\-]/g,''); -} - -function hashUrl() -{ - return '#'+hashValue(); -} - -function pathName() -{ - return $(location).attr('pathname').replace(/[^-A-Za-z0-9+&@#/%?=~_|!:,.;\(\)]/g, ''); -} - -function localStorageSupported() -{ - try { - return 'localStorage' in window && window['localStorage'] !== null && window.localStorage.getItem; - } - catch(e) { - return false; - } -} - -function storeLink(link) -{ - if (!$("#nav-sync").hasClass('sync') && localStorageSupported()) { - window.localStorage.setItem('navpath',link); - } -} - -function deleteLink() -{ - if (localStorageSupported()) { - window.localStorage.setItem('navpath',''); - } -} - -function cachedLink() -{ - if (localStorageSupported()) { - return window.localStorage.getItem('navpath'); - } else { - return ''; - } -} - -function getScript(scriptName,func,show) -{ - var head = document.getElementsByTagName("head")[0]; - var script = document.createElement('script'); - script.id = scriptName; - script.type = 'text/javascript'; - script.onload = func; - script.src = scriptName+'.js'; - head.appendChild(script); -} - -function createIndent(o,domNode,node,level) -{ - var level=-1; - var n = node; - while (n.parentNode) { level++; n=n.parentNode; } - if (node.childrenData) { - var imgNode = document.createElement("span"); - imgNode.className = 'arrow'; - imgNode.style.paddingLeft=(16*level).toString()+'px'; - imgNode.innerHTML=arrowRight; - node.plus_img = imgNode; - node.expandToggle = document.createElement("a"); - node.expandToggle.href = "javascript:void(0)"; - node.expandToggle.onclick = function() { - if (node.expanded) { - $(node.getChildrenUL()).slideUp("fast"); - node.plus_img.innerHTML=arrowRight; - node.expanded = false; - } else { - expandNode(o, node, false, false); - } - } - node.expandToggle.appendChild(imgNode); - domNode.appendChild(node.expandToggle); - } else { - var span = document.createElement("span"); - span.className = 'arrow'; - span.style.width = 16*(level+1)+'px'; - span.innerHTML = ' '; - domNode.appendChild(span); - } -} - -var animationInProgress = false; - -function gotoAnchor(anchor,aname,updateLocation) -{ - var pos, docContent = $('#doc-content'); - var ancParent = $(anchor.parent()); - if (ancParent.hasClass('memItemLeft') || - ancParent.hasClass('memtitle') || - ancParent.hasClass('fieldname') || - ancParent.hasClass('fieldtype') || - ancParent.is(':header')) - { - pos = ancParent.position().top; - } else if (anchor.position()) { - pos = anchor.position().top; - } - if (pos) { - var dist = Math.abs(Math.min( - pos-docContent.offset().top, - docContent[0].scrollHeight- - docContent.height()-docContent.scrollTop())); - animationInProgress=true; - docContent.animate({ - scrollTop: pos + docContent.scrollTop() - docContent.offset().top - },Math.max(50,Math.min(500,dist)),function(){ - if (updateLocation) window.location.href=aname; - animationInProgress=false; - }); - } -} - -function newNode(o, po, text, link, childrenData, lastNode) -{ - var node = new Object(); - node.children = Array(); - node.childrenData = childrenData; - node.depth = po.depth + 1; - node.relpath = po.relpath; - node.isLast = lastNode; - - node.li = document.createElement("li"); - po.getChildrenUL().appendChild(node.li); - node.parentNode = po; - - node.itemDiv = document.createElement("div"); - node.itemDiv.className = "item"; - - node.labelSpan = document.createElement("span"); - node.labelSpan.className = "label"; - - createIndent(o,node.itemDiv,node,0); - node.itemDiv.appendChild(node.labelSpan); - node.li.appendChild(node.itemDiv); - - var a = document.createElement("a"); - node.labelSpan.appendChild(a); - node.label = document.createTextNode(text); - node.expanded = false; - a.appendChild(node.label); - if (link) { - var url; - if (link.substring(0,1)=='^') { - url = link.substring(1); - link = url; - } else { - url = node.relpath+link; - } - a.className = stripPath(link.replace('#',':')); - if (link.indexOf('#')!=-1) { - var aname = '#'+link.split('#')[1]; - var srcPage = stripPath(pathName()); - var targetPage = stripPath(link.split('#')[0]); - a.href = srcPage!=targetPage ? url : "javascript:void(0)"; - a.onclick = function(){ - storeLink(link); - if (!$(a).parent().parent().hasClass('selected')) - { - $('.item').removeClass('selected'); - $('.item').removeAttr('id'); - $(a).parent().parent().addClass('selected'); - $(a).parent().parent().attr('id','selected'); - } - var anchor = $(aname); - gotoAnchor(anchor,aname,true); - }; - } else { - a.href = url; - a.onclick = function() { storeLink(link); } - } - } else { - if (childrenData != null) - { - a.className = "nolink"; - a.href = "javascript:void(0)"; - a.onclick = node.expandToggle.onclick; - } - } - - node.childrenUL = null; - node.getChildrenUL = function() { - if (!node.childrenUL) { - node.childrenUL = document.createElement("ul"); - node.childrenUL.className = "children_ul"; - node.childrenUL.style.display = "none"; - node.li.appendChild(node.childrenUL); - } - return node.childrenUL; - }; - - return node; -} - -function showRoot() -{ - var headerHeight = $("#top").height(); - var footerHeight = $("#nav-path").height(); - var windowHeight = $(window).height() - headerHeight - footerHeight; - (function (){ // retry until we can scroll to the selected item - try { - var navtree=$('#nav-tree'); - navtree.scrollTo('#selected',100,{offset:-windowHeight/2}); - } catch (err) { - setTimeout(arguments.callee, 0); - } - })(); -} - -function expandNode(o, node, imm, showRoot) -{ - if (node.childrenData && !node.expanded) { - if (typeof(node.childrenData)==='string') { - var varName = node.childrenData; - getScript(node.relpath+varName,function(){ - node.childrenData = getData(varName); - expandNode(o, node, imm, showRoot); - }, showRoot); - } else { - if (!node.childrenVisited) { - getNode(o, node); - } - $(node.getChildrenUL()).slideDown("fast"); - node.plus_img.innerHTML = arrowDown; - node.expanded = true; - } - } -} - -function glowEffect(n,duration) -{ - n.addClass('glow').delay(duration).queue(function(next){ - $(this).removeClass('glow');next(); - }); -} - -function highlightAnchor() -{ - var aname = hashUrl(); - var anchor = $(aname); - if (anchor.parent().attr('class')=='memItemLeft'){ - var rows = $('.memberdecls tr[class$="'+hashValue()+'"]'); - glowEffect(rows.children(),300); // member without details - } else if (anchor.parent().attr('class')=='fieldname'){ - glowEffect(anchor.parent().parent(),1000); // enum value - } else if (anchor.parent().attr('class')=='fieldtype'){ - glowEffect(anchor.parent().parent(),1000); // struct field - } else if (anchor.parent().is(":header")) { - glowEffect(anchor.parent(),1000); // section header - } else { - glowEffect(anchor.next(),1000); // normal member - } -} - -function selectAndHighlight(hash,n) -{ - var a; - if (hash) { - var link=stripPath(pathName())+':'+hash.substring(1); - a=$('.item a[class$="'+link+'"]'); - } - if (a && a.length) { - a.parent().parent().addClass('selected'); - a.parent().parent().attr('id','selected'); - highlightAnchor(); - } else if (n) { - $(n.itemDiv).addClass('selected'); - $(n.itemDiv).attr('id','selected'); - } - if ($('#nav-tree-contents .item:first').hasClass('selected')) { - $('#nav-sync').css('top','30px'); - } else { - $('#nav-sync').css('top','5px'); - } - showRoot(); -} - -function showNode(o, node, index, hash) -{ - if (node && node.childrenData) { - if (typeof(node.childrenData)==='string') { - var varName = node.childrenData; - getScript(node.relpath+varName,function(){ - node.childrenData = getData(varName); - showNode(o,node,index,hash); - },true); - } else { - if (!node.childrenVisited) { - getNode(o, node); - } - $(node.getChildrenUL()).css({'display':'block'}); - node.plus_img.innerHTML = arrowDown; - node.expanded = true; - var n = node.children[o.breadcrumbs[index]]; - if (index+11) hash = '#'+parts[1].replace(/[^\w\-]/g,''); - else hash=''; - } - if (hash.match(/^#l\d+$/)) { - var anchor=$('a[name='+hash.substring(1)+']'); - glowEffect(anchor.parent(),1000); // line number - hash=''; // strip line number anchors - } - var url=root+hash; - var i=-1; - while (NAVTREEINDEX[i+1]<=url) i++; - if (i==-1) { i=0; root=NAVTREE[0][1]; } // fallback: show index - if (navTreeSubIndices[i]) { - gotoNode(o,i,root,hash,relpath) - } else { - getScript(relpath+'navtreeindex'+i,function(){ - navTreeSubIndices[i] = eval('NAVTREEINDEX'+i); - if (navTreeSubIndices[i]) { - gotoNode(o,i,root,hash,relpath); - } - },true); - } -} - -function showSyncOff(n,relpath) -{ - n.html(''); -} - -function showSyncOn(n,relpath) -{ - n.html(''); -} - -function toggleSyncButton(relpath) -{ - var navSync = $('#nav-sync'); - if (navSync.hasClass('sync')) { - navSync.removeClass('sync'); - showSyncOff(navSync,relpath); - storeLink(stripPath2(pathName())+hashUrl()); - } else { - navSync.addClass('sync'); - showSyncOn(navSync,relpath); - deleteLink(); - } -} - -var loadTriggered = false; -var readyTriggered = false; -var loadObject,loadToRoot,loadUrl,loadRelPath; - -$(window).on('load',function(){ - if (readyTriggered) { // ready first - navTo(loadObject,loadToRoot,loadUrl,loadRelPath); - showRoot(); - } - loadTriggered=true; -}); - -function initNavTree(toroot,relpath) -{ - var o = new Object(); - o.toroot = toroot; - o.node = new Object(); - o.node.li = document.getElementById("nav-tree-contents"); - o.node.childrenData = NAVTREE; - o.node.children = new Array(); - o.node.childrenUL = document.createElement("ul"); - o.node.getChildrenUL = function() { return o.node.childrenUL; }; - o.node.li.appendChild(o.node.childrenUL); - o.node.depth = 0; - o.node.relpath = relpath; - o.node.expanded = false; - o.node.isLast = true; - o.node.plus_img = document.createElement("span"); - o.node.plus_img.className = 'arrow'; - o.node.plus_img.innerHTML = arrowRight; - - if (localStorageSupported()) { - var navSync = $('#nav-sync'); - if (cachedLink()) { - showSyncOff(navSync,relpath); - navSync.removeClass('sync'); - } else { - showSyncOn(navSync,relpath); - } - navSync.click(function(){ toggleSyncButton(relpath); }); - } - - if (loadTriggered) { // load before ready - navTo(o,toroot,hashUrl(),relpath); - showRoot(); - } else { // ready before load - loadObject = o; - loadToRoot = toroot; - loadUrl = hashUrl(); - loadRelPath = relpath; - readyTriggered=true; - } - - $(window).bind('hashchange', function(){ - if (window.location.hash && window.location.hash.length>1){ - var a; - if ($(location).attr('hash')){ - var clslink=stripPath(pathName())+':'+hashValue(); - a=$('.item a[class$="'+clslink.replace(/ - - - - - - -binlex: include/pe.h Source File - - - - - - - - - - - - - - -
    -
    - - - - - - -
    -
    binlex -
    -
    -
    - - - - - - - -
    -
    - -
    -
    -
    - -
    - -
    -
    - - -
    - -
    - -
    -
    -
    pe.h
    -
    -
    -
    1 #ifndef PE_H
    -
    2 #define PE_H
    -
    3 
    -
    4 #ifdef _WIN32
    -
    5 #include <Windows.h>
    -
    6 #include <stdexcept>
    -
    7 #endif
    -
    8 
    -
    9 #include <iostream>
    -
    10 #include <memory>
    -
    11 #include <set>
    -
    12 #include <LIEF/PE.hpp>
    -
    13 #include "common.h"
    -
    14 #include "file.h"
    -
    15 #include <vector>
    -
    16 #include <cassert>
    -
    17 
    -
    18 #ifdef _WIN32
    -
    19 #define BINLEX_EXPORT __declspec(dllexport)
    -
    20 #else
    -
    21 #define BINLEX_EXPORT
    -
    22 #endif
    -
    23 
    -
    24 using namespace std;
    -
    25 using namespace LIEF::PE;
    -
    26 
    -
    27 namespace binlex {
    -
    28  class PE : public File{
    -
    32  private:
    -
    33  bool ParseSections();
    -
    34  public:
    -
    35  #ifndef _WIN32
    -
    36  MACHINE_TYPES mode = MACHINE_TYPES::IMAGE_FILE_MACHINE_UNKNOWN;
    -
    37  #else
    -
    38  MACHINE_TYPES mode = MACHINE_TYPES::IMAGE_FILE_MACHINE_UNKNOWN;
    -
    39  #endif
    -
    40  unique_ptr<LIEF::PE::Binary> binary;
    -
    41  BINLEX_EXPORT PE();
    -
    42  struct Section sections[BINARY_MAX_SECTIONS];
    -
    43  uint32_t total_exec_sections;
    -
    49  BINLEX_EXPORT bool Setup(MACHINE_TYPES input_mode);
    -
    54  BINLEX_EXPORT bool IsDotNet();
    -
    59  BINLEX_EXPORT bool HasLimitations();
    -
    65  virtual bool ReadVector(const std::vector<uint8_t> &data);
    -
    66  BINLEX_EXPORT ~PE();
    -
    67  };
    -
    68 };
    -
    69 
    -
    70 #endif
    -
    -
    -
    Definition: pe.h:28
    -
    Definition: file.h:14
    -
    the binlex namespace
    - - - - diff --git a/docs/html/raw_8h_source.html b/docs/html/raw_8h_source.html deleted file mode 100644 index d80c7da0..00000000 --- a/docs/html/raw_8h_source.html +++ /dev/null @@ -1,148 +0,0 @@ - - - - - - - -binlex: include/raw.h Source File - - - - - - - - - - - - - - -
    -
    - - - - - - -
    -
    binlex -
    -
    -
    - - - - - - - -
    -
    - -
    -
    -
    - -
    - -
    -
    - - -
    - -
    - -
    -
    -
    raw.h
    -
    -
    -
    1 #ifndef RAW_H
    -
    2 #define RAW_H
    -
    3 
    -
    4 #include <string.h>
    -
    5 #include <stdio.h>
    -
    6 #include <stdlib.h>
    -
    7 #include <unistd.h>
    -
    8 #include <stdexcept>
    -
    9 #include "file.h"
    -
    10 
    -
    11 #ifdef _WIN32
    -
    12 #define BINLEX_EXPORT __declspec(dllexport)
    -
    13 #else
    -
    14 #define BINLEX_EXPORT
    -
    15 #endif
    -
    16 
    -
    17 #ifdef _WIN32
    -
    18 typedef unsigned int uint;
    -
    19 #endif
    -
    20 
    -
    21 #ifdef _WIN32
    -
    22 typedef unsigned int uint;
    -
    23 #endif
    -
    24 
    -
    25 namespace binlex{
    -
    26  class Raw : public File{
    -
    30  public:
    -
    36  int GetFileSize(FILE *fd);
    -
    37  struct Section {
    -
    38  void *data;
    -
    39  int size;
    -
    40  uint offset;
    -
    41  };
    -
    42  struct Section sections[BINARY_MAX_SECTIONS];
    -
    43  BINLEX_EXPORT Raw();
    -
    49  BINLEX_EXPORT virtual bool ReadVector(const std::vector<uint8_t> &data);
    -
    50  BINLEX_EXPORT ~Raw();
    -
    51  };
    -
    52 }
    -
    53 
    -
    54 #endif
    -
    -
    -
    int GetFileSize(FILE *fd)
    -
    Definition: raw.h:26
    -
    Definition: raw.h:37
    -
    virtual BINLEX_EXPORT bool ReadVector(const std::vector< uint8_t > &data)
    -
    Definition: file.h:14
    -
    the binlex namespace
    - - - - diff --git a/docs/html/resize.js b/docs/html/resize.js deleted file mode 100644 index a0bb5f45..00000000 --- a/docs/html/resize.js +++ /dev/null @@ -1,137 +0,0 @@ -/* - @licstart The following is the entire license notice for the - JavaScript code in this file. - - Copyright (C) 1997-2017 by Dimitri van Heesch - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 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 General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - @licend The above is the entire license notice - for the JavaScript code in this file - */ -function initResizable() -{ - var cookie_namespace = 'doxygen'; - var sidenav,navtree,content,header,collapsed,collapsedWidth=0,barWidth=6,desktop_vp=768,titleHeight; - - function readCookie(cookie) - { - var myCookie = cookie_namespace+"_"+cookie+"="; - if (document.cookie) { - var index = document.cookie.indexOf(myCookie); - if (index != -1) { - var valStart = index + myCookie.length; - var valEnd = document.cookie.indexOf(";", valStart); - if (valEnd == -1) { - valEnd = document.cookie.length; - } - var val = document.cookie.substring(valStart, valEnd); - return val; - } - } - return 0; - } - - function writeCookie(cookie, val, expiration) - { - if (val==undefined) return; - if (expiration == null) { - var date = new Date(); - date.setTime(date.getTime()+(10*365*24*60*60*1000)); // default expiration is one week - expiration = date.toGMTString(); - } - document.cookie = cookie_namespace + "_" + cookie + "=" + val + "; expires=" + expiration+"; path=/"; - } - - function resizeWidth() - { - var windowWidth = $(window).width() + "px"; - var sidenavWidth = $(sidenav).outerWidth(); - content.css({marginLeft:parseInt(sidenavWidth)+"px"}); - writeCookie('width',sidenavWidth-barWidth, null); - } - - function restoreWidth(navWidth) - { - var windowWidth = $(window).width() + "px"; - content.css({marginLeft:parseInt(navWidth)+barWidth+"px"}); - sidenav.css({width:navWidth + "px"}); - } - - function resizeHeight() - { - var headerHeight = header.outerHeight(); - var footerHeight = footer.outerHeight(); - var windowHeight = $(window).height() - headerHeight - footerHeight; - content.css({height:windowHeight + "px"}); - navtree.css({height:windowHeight + "px"}); - sidenav.css({height:windowHeight + "px"}); - var width=$(window).width(); - if (width!=collapsedWidth) { - if (width=desktop_vp) { - if (!collapsed) { - collapseExpand(); - } - } else if (width>desktop_vp && collapsedWidth0) { - restoreWidth(0); - collapsed=true; - } - else { - var width = readCookie('width'); - if (width>200 && width<$(window).width()) { restoreWidth(width); } else { restoreWidth(200); } - collapsed=false; - } - } - - header = $("#top"); - sidenav = $("#side-nav"); - content = $("#doc-content"); - navtree = $("#nav-tree"); - footer = $("#nav-path"); - $(".side-nav-resizable").resizable({resize: function(e, ui) { resizeWidth(); } }); - $(sidenav).resizable({ minWidth: 0 }); - $(window).resize(function() { resizeHeight(); }); - var device = navigator.userAgent.toLowerCase(); - var touch_device = device.match(/(iphone|ipod|ipad|android)/); - if (touch_device) { /* wider split bar for touch only devices */ - $(sidenav).css({ paddingRight:'20px' }); - $('.ui-resizable-e').css({ width:'20px' }); - $('#nav-sync').css({ right:'34px' }); - barWidth=20; - } - var width = readCookie('width'); - if (width) { restoreWidth(width); } else { resizeWidth(); } - resizeHeight(); - var url = location.href; - var i=url.indexOf("#"); - if (i>=0) window.location.hash=url.substr(i); - var _preventDefault = function(evt) { evt.preventDefault(); }; - $("#splitbar").bind("dragstart", _preventDefault).bind("selectstart", _preventDefault); - $(".ui-resizable-handle").dblclick(collapseExpand); - $(window).on('load',resizeHeight); -} -/* @license-end */ diff --git a/docs/html/search/all_0.html b/docs/html/search/all_0.html deleted file mode 100644 index 26dd244f..00000000 --- a/docs/html/search/all_0.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_0.js b/docs/html/search/all_0.js deleted file mode 100644 index 379c987e..00000000 --- a/docs/html/search/all_0.js +++ /dev/null @@ -1,7 +0,0 @@ -var searchData= -[ - ['adddiscoveredblock_0',['AddDiscoveredBlock',['../classbinlex_1_1Decompiler.html#aac547ccd10ac7acefea742c37e05d69b',1,'binlex::Decompiler']]], - ['appendtrait_1',['AppendTrait',['../classbinlex_1_1Decompiler.html#a1af533c4e46a327d004c37bb609bb528',1,'binlex::Decompiler']]], - ['args_2',['Args',['../classbinlex_1_1Args.html',1,'binlex']]], - ['autolex_3',['AutoLex',['../classbinlex_1_1AutoLex.html',1,'binlex']]] -]; diff --git a/docs/html/search/all_1.html b/docs/html/search/all_1.html deleted file mode 100644 index 8eb215b9..00000000 --- a/docs/html/search/all_1.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_1.js b/docs/html/search/all_1.js deleted file mode 100644 index 16285268..00000000 --- a/docs/html/search/all_1.js +++ /dev/null @@ -1,6 +0,0 @@ -var searchData= -[ - ['binlex_4',['binlex',['../namespacebinlex.html',1,'']]], - ['blobheapindex_5',['BlobHeapIndex',['../classdotnet_1_1BlobHeapIndex.html',1,'dotnet']]], - ['binlex_6',['binlex',['../index.html',1,'']]] -]; diff --git a/docs/html/search/all_2.html b/docs/html/search/all_2.html deleted file mode 100644 index b26d9165..00000000 --- a/docs/html/search/all_2.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_2.js b/docs/html/search/all_2.js deleted file mode 100644 index 563f6376..00000000 --- a/docs/html/search/all_2.js +++ /dev/null @@ -1,18 +0,0 @@ -var searchData= -[ - ['calculatefilehashes_7',['CalculateFileHashes',['../classbinlex_1_1File.html#a744ec07afb83c38432ab836cedbfc15b',1,'binlex::File::CalculateFileHashes(char *file_path)'],['../classbinlex_1_1File.html#ada874bdcd50ff6426c1bd479cc2be290',1,'binlex::File::CalculateFileHashes(const vector< uint8_t > &data)']]], - ['check_5fmode_8',['check_mode',['../classbinlex_1_1Args.html#ad39861cfaf635961de187ecccb8c8768',1,'binlex::Args']]], - ['cildecompiler_9',['CILDecompiler',['../classbinlex_1_1CILDecompiler.html',1,'binlex']]], - ['cleartrait_10',['ClearTrait',['../classbinlex_1_1Decompiler.html#a98a6fe2d51d40a3c4763855e51b339d1',1,'binlex::Decompiler']]], - ['collectinsn_11',['CollectInsn',['../classbinlex_1_1Decompiler.html#a6792a979d3ed4881c63996d67bef6969',1,'binlex::Decompiler']]], - ['collectoperands_12',['CollectOperands',['../classbinlex_1_1Decompiler.html#a9ab3b7775ccb2aa1e3c8898960d44702',1,'binlex::Decompiler']]], - ['common_13',['Common',['../classbinlex_1_1Common.html',1,'binlex']]], - ['convbytes_14',['ConvBytes',['../classbinlex_1_1CILDecompiler.html#a2ed417089f22d5f86a94666768c54122',1,'binlex::CILDecompiler']]], - ['convtraitbytes_15',['ConvTraitBytes',['../classbinlex_1_1CILDecompiler.html#a480b9d36c9e0ee600294cb9e5ecd10a3',1,'binlex::CILDecompiler']]], - ['cor20_5fheader_16',['COR20_HEADER',['../structdotnet_1_1COR20__HEADER.html',1,'dotnet']]], - ['cor20_5fstorage_5fheader_17',['COR20_STORAGE_HEADER',['../structdotnet_1_1COR20__STORAGE__HEADER.html',1,'dotnet']]], - ['cor20_5fstorage_5fsignature_18',['COR20_STORAGE_SIGNATURE',['../structdotnet_1_1COR20__STORAGE__SIGNATURE.html',1,'dotnet']]], - ['cor20_5fstream_5fheader_19',['COR20_STREAM_HEADER',['../structdotnet_1_1COR20__STREAM__HEADER.html',1,'dotnet']]], - ['cor20metadatatable_20',['Cor20MetadataTable',['../classdotnet_1_1Cor20MetadataTable.html',1,'dotnet']]], - ['createtraitsforsection_21',['CreateTraitsForSection',['../classbinlex_1_1Decompiler.html#ae1a9193c009a4adcfd20441dc9896efe',1,'binlex::Decompiler']]] -]; diff --git a/docs/html/search/all_3.html b/docs/html/search/all_3.html deleted file mode 100644 index b61b96f8..00000000 --- a/docs/html/search/all_3.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_3.js b/docs/html/search/all_3.js deleted file mode 100644 index 4d269288..00000000 --- a/docs/html/search/all_3.js +++ /dev/null @@ -1,7 +0,0 @@ -var searchData= -[ - ['decompile_22',['Decompile',['../classbinlex_1_1Decompiler.html#acf647c06620257b5dc942c02c674e54f',1,'binlex::Decompiler']]], - ['decompiler_23',['Decompiler',['../classbinlex_1_1Decompiler.html',1,'binlex']]], - ['decompilerbase_24',['DecompilerBase',['../classbinlex_1_1DecompilerBase.html',1,'binlex::DecompilerBase'],['../classbinlex_1_1DecompilerBase.html#a0192d82e24d70b26ab2d86329c4e7591',1,'binlex::DecompilerBase::DecompilerBase()']]], - ['dotnet_25',['DOTNET',['../classbinlex_1_1DOTNET.html',1,'binlex']]] -]; diff --git a/docs/html/search/all_4.html b/docs/html/search/all_4.html deleted file mode 100644 index 06de1550..00000000 --- a/docs/html/search/all_4.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_4.js b/docs/html/search/all_4.js deleted file mode 100644 index 97ea2740..00000000 --- a/docs/html/search/all_4.js +++ /dev/null @@ -1,5 +0,0 @@ -var searchData= -[ - ['elf_26',['ELF',['../classbinlex_1_1ELF.html',1,'binlex']]], - ['entropy_27',['Entropy',['../classbinlex_1_1Common.html#a581cc519cdd00312a55fe0649a4b168d',1,'binlex::Common']]] -]; diff --git a/docs/html/search/all_5.html b/docs/html/search/all_5.html deleted file mode 100644 index 2544c4e5..00000000 --- a/docs/html/search/all_5.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_5.js b/docs/html/search/all_5.js deleted file mode 100644 index d2ca4fcd..00000000 --- a/docs/html/search/all_5.js +++ /dev/null @@ -1,8 +0,0 @@ -var searchData= -[ - ['fatheader_28',['FatHeader',['../classdotnet_1_1FatHeader.html',1,'dotnet']]], - ['fieldentry_29',['FieldEntry',['../classdotnet_1_1FieldEntry.html',1,'dotnet']]], - ['fieldptrentry_30',['FieldPtrEntry',['../classdotnet_1_1FieldPtrEntry.html',1,'dotnet']]], - ['file_31',['File',['../classbinlex_1_1File.html',1,'binlex']]], - ['file_5freference_32',['file_reference',['../classbinlex_1_1DecompilerBase.html#ae2691d857fd14f773f4a075c5225acd9',1,'binlex::DecompilerBase']]] -]; diff --git a/docs/html/search/all_6.html b/docs/html/search/all_6.html deleted file mode 100644 index 43f14eab..00000000 --- a/docs/html/search/all_6.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_6.js b/docs/html/search/all_6.js deleted file mode 100644 index b56b63d7..00000000 --- a/docs/html/search/all_6.js +++ /dev/null @@ -1,13 +0,0 @@ -var searchData= -[ - ['get_5ftags_5fas_5fstr_33',['get_tags_as_str',['../classbinlex_1_1Args.html#a07ab8a9c6d5a9578516a0b2de7999233',1,'binlex::Args']]], - ['getbytesize_34',['GetByteSize',['../classbinlex_1_1Common.html#afcb68aa2cb7c96abedd55b3fc77fabdd',1,'binlex::Common']]], - ['getfilesha256_35',['GetFileSHA256',['../classbinlex_1_1Common.html#acaf355a97857c4563c3b8e6162187e90',1,'binlex::Common']]], - ['getfilesize_36',['GetFileSize',['../classbinlex_1_1Raw.html#a8aacf571ea478ede2feb0dbfbd3e9756',1,'binlex::Raw']]], - ['getfiletlsh_37',['GetFileTLSH',['../classbinlex_1_1Common.html#a0c426523358896f7594a836cb286fca2',1,'binlex::Common']]], - ['getsha256_38',['GetSHA256',['../classbinlex_1_1Common.html#a85b33e0d5148b97b1db40d2f59b2679c',1,'binlex::Common']]], - ['gettlsh_39',['GetTLSH',['../classbinlex_1_1Common.html#adf666341ef78e3cba311e576e525fbf4',1,'binlex::Common']]], - ['gettrait_40',['GetTrait',['../classbinlex_1_1CILDecompiler.html#a6907fc607dfc77e0aabf1df12557923b',1,'binlex::CILDecompiler::GetTrait()'],['../classbinlex_1_1Decompiler.html#a1f766992f51fb6a39f26e0a111a3fd06',1,'binlex::Decompiler::GetTrait()']]], - ['gettraits_41',['GetTraits',['../classbinlex_1_1CILDecompiler.html#aedf8801582485d3abacd7d4c3769b7dc',1,'binlex::CILDecompiler::GetTraits()'],['../classbinlex_1_1Decompiler.html#afc5ee0847582e4df55b12bdb36e5ac1b',1,'binlex::Decompiler::GetTraits()'],['../classbinlex_1_1DecompilerBase.html#a436f937767ebbd74eb47e9d76fd849e2',1,'binlex::DecompilerBase::GetTraits()']]], - ['guidheapindex_42',['GuidHeapIndex',['../classdotnet_1_1GuidHeapIndex.html',1,'dotnet']]] -]; diff --git a/docs/html/search/all_7.html b/docs/html/search/all_7.html deleted file mode 100644 index af52f82a..00000000 --- a/docs/html/search/all_7.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_7.js b/docs/html/search/all_7.js deleted file mode 100644 index 998cfe09..00000000 --- a/docs/html/search/all_7.js +++ /dev/null @@ -1,6 +0,0 @@ -var searchData= -[ - ['haslimitations_43',['HasLimitations',['../classbinlex_1_1PE.html#afc5cd018a384c015a298fe97d3658a3e',1,'binlex::PE']]], - ['hexdump_44',['Hexdump',['../classbinlex_1_1Common.html#a654dea4accfca1f897e7a7bc1587f343',1,'binlex::Common']]], - ['hexdumpbe_45',['HexdumpBE',['../classbinlex_1_1Common.html#a92eb94689731b4cf6a376b34eca54480',1,'binlex::Common']]] -]; diff --git a/docs/html/search/all_8.html b/docs/html/search/all_8.html deleted file mode 100644 index cf2b5df9..00000000 --- a/docs/html/search/all_8.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_8.js b/docs/html/search/all_8.js deleted file mode 100644 index 4d97a5c0..00000000 --- a/docs/html/search/all_8.js +++ /dev/null @@ -1,19 +0,0 @@ -var searchData= -[ - ['instruction_46',['Instruction',['../structbinlex_1_1CILDecompiler_1_1Instruction.html',1,'binlex::CILDecompiler']]], - ['is_5fdir_47',['is_dir',['../classbinlex_1_1Args.html#a4123f3468f9460122c5c57c3ee6ca3e6',1,'binlex::Args']]], - ['is_5ffile_48',['is_file',['../classbinlex_1_1Args.html#ad2f4242136bc00eeaed9417652306334',1,'binlex::Args']]], - ['isaddress_49',['IsAddress',['../classbinlex_1_1Decompiler.html#a4b98cc138ec7b506f54abf66cedd058d',1,'binlex::Decompiler']]], - ['isblock_50',['IsBlock',['../classbinlex_1_1Decompiler.html#ae18d074f16e507dcd6ec5b683c116570',1,'binlex::Decompiler']]], - ['isconditionalinsn_51',['IsConditionalInsn',['../classbinlex_1_1CILDecompiler.html#a988e9c0d563b2a24468e321ec015a8ba',1,'binlex::CILDecompiler::IsConditionalInsn()'],['../classbinlex_1_1Decompiler.html#a21cbd642799beb361b065e431fb5b4df',1,'binlex::Decompiler::IsConditionalInsn()']]], - ['isdotnet_52',['IsDotNet',['../classbinlex_1_1PE.html#a7d59d7af11c403d379d4b748065517d6',1,'binlex::PE']]], - ['isendinsn_53',['IsEndInsn',['../classbinlex_1_1Decompiler.html#aa28e4ef73f38f9fa1367570821a37864',1,'binlex::Decompiler']]], - ['isfunction_54',['IsFunction',['../classbinlex_1_1Decompiler.html#a78c24e43540d1e46aacc6fc10d3f95b2',1,'binlex::Decompiler']]], - ['isnopinsn_55',['IsNopInsn',['../classbinlex_1_1Decompiler.html#a3b0ba77bf8438ac544b232a3f00c3375',1,'binlex::Decompiler']]], - ['isprefixinstr_56',['IsPrefixInstr',['../classbinlex_1_1CILDecompiler.html#a365ed814ac434f9f55bc76e3d0a107c6',1,'binlex::CILDecompiler']]], - ['isprivinsn_57',['IsPrivInsn',['../classbinlex_1_1Decompiler.html#ad8006d35aaf95f840c249e28ed1f08b5',1,'binlex::Decompiler']]], - ['issemanticnopinsn_58',['IsSemanticNopInsn',['../classbinlex_1_1Decompiler.html#abeaa67c7f228d53db239d86e82fd5444',1,'binlex::Decompiler']]], - ['istrapinsn_59',['IsTrapInsn',['../classbinlex_1_1Decompiler.html#af3525ec47db26c08d6bdeaf9728a7ac7',1,'binlex::Decompiler']]], - ['isvisited_60',['IsVisited',['../classbinlex_1_1Decompiler.html#a22cafe99445d2c34cf60e5f62f44cdf7',1,'binlex::Decompiler']]], - ['iswildcardinsn_61',['IsWildcardInsn',['../classbinlex_1_1Decompiler.html#a88eb75d131555324252d66dcaeb44fa9',1,'binlex::Decompiler']]] -]; diff --git a/docs/html/search/all_9.html b/docs/html/search/all_9.html deleted file mode 100644 index 690785a5..00000000 --- a/docs/html/search/all_9.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_9.js b/docs/html/search/all_9.js deleted file mode 100644 index 312d2002..00000000 --- a/docs/html/search/all_9.js +++ /dev/null @@ -1,4 +0,0 @@ -var searchData= -[ - ['lineardisassemble_62',['LinearDisassemble',['../classbinlex_1_1Decompiler.html#a0045a92875a089edb6e862387851bb4d',1,'binlex::Decompiler']]] -]; diff --git a/docs/html/search/all_a.html b/docs/html/search/all_a.html deleted file mode 100644 index f2f3d3a3..00000000 --- a/docs/html/search/all_a.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_a.js b/docs/html/search/all_a.js deleted file mode 100644 index 48023c6b..00000000 --- a/docs/html/search/all_a.js +++ /dev/null @@ -1,10 +0,0 @@ -var searchData= -[ - ['maxaddress_63',['MaxAddress',['../classbinlex_1_1Decompiler.html#a0df46873b8d487ed00bc252d59fb9aaa',1,'binlex::Decompiler']]], - ['method_64',['Method',['../classdotnet_1_1Method.html',1,'dotnet']]], - ['methoddefentry_65',['MethodDefEntry',['../classdotnet_1_1MethodDefEntry.html',1,'dotnet']]], - ['methodheader_66',['MethodHeader',['../classdotnet_1_1MethodHeader.html',1,'dotnet']]], - ['methodptrentry_67',['MethodPtrEntry',['../classdotnet_1_1MethodPtrEntry.html',1,'dotnet']]], - ['moduleentry_68',['ModuleEntry',['../classdotnet_1_1ModuleEntry.html',1,'dotnet']]], - ['multitableindex_69',['MultiTableIndex',['../classdotnet_1_1MultiTableIndex.html',1,'dotnet']]] -]; diff --git a/docs/html/search/all_b.html b/docs/html/search/all_b.html deleted file mode 100644 index 14f34036..00000000 --- a/docs/html/search/all_b.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_b.js b/docs/html/search/all_b.js deleted file mode 100644 index 4ff686b6..00000000 --- a/docs/html/search/all_b.js +++ /dev/null @@ -1,13 +0,0 @@ -var searchData= -[ - ['parse_70',['parse',['../classbinlex_1_1Args.html#ad98e40d517d1f5f00ea0957c91a565bc',1,'binlex::Args']]], - ['parseargs_71',['ParseArgs',['../structdotnet_1_1TableEntry_1_1ParseArgs.html',1,'dotnet::TableEntry']]], - ['pe_72',['PE',['../classbinlex_1_1PE.html',1,'binlex']]], - ['print_5fhelp_73',['print_help',['../classbinlex_1_1Args.html#abf121fddbd8c005159add8135fc81786',1,'binlex::Args']]], - ['processfile_74',['ProcessFile',['../classbinlex_1_1AutoLex.html#a3b8efd8b134bddb06956041230d4c7b9',1,'binlex::AutoLex']]], - ['py_5fsetcorpus_75',['py_SetCorpus',['../classbinlex_1_1DecompilerBase.html#a83d585e6e625f33db9f0411f137eec91',1,'binlex::DecompilerBase']]], - ['py_5fsetinstructions_76',['py_SetInstructions',['../classbinlex_1_1DecompilerBase.html#a05c7b3139af23eae016cc7a39bf52e6b',1,'binlex::DecompilerBase']]], - ['py_5fsetmode_77',['py_SetMode',['../classbinlex_1_1DecompilerBase.html#ac56114b8f7f1abc3e633aa27eedaf60a',1,'binlex::DecompilerBase']]], - ['py_5fsettags_78',['py_SetTags',['../classbinlex_1_1DecompilerBase.html#a0a0d7ff04acbf54166a42efa2eb89369',1,'binlex::DecompilerBase']]], - ['py_5fsetthreads_79',['py_SetThreads',['../classbinlex_1_1DecompilerBase.html#a46cc962e01cd3ba61b63ed5b259efbc8',1,'binlex::DecompilerBase']]] -]; diff --git a/docs/html/search/all_c.html b/docs/html/search/all_c.html deleted file mode 100644 index da60ab8d..00000000 --- a/docs/html/search/all_c.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_c.js b/docs/html/search/all_c.js deleted file mode 100644 index 5b160bc0..00000000 --- a/docs/html/search/all_c.js +++ /dev/null @@ -1,11 +0,0 @@ -var searchData= -[ - ['raw_80',['Raw',['../classbinlex_1_1Raw.html',1,'binlex']]], - ['readbuffer_81',['ReadBuffer',['../classbinlex_1_1File.html#a7d80d86e8c30d68e11a7f024518c9172',1,'binlex::File']]], - ['readfile_82',['ReadFile',['../classbinlex_1_1File.html#af69581745439422e597e9c8ab2dcb27c',1,'binlex::File']]], - ['readfileintovector_83',['ReadFileIntoVector',['../classbinlex_1_1File.html#a2194b1dc5be789889ebe10099a778935',1,'binlex::File']]], - ['readvector_84',['ReadVector',['../classbinlex_1_1ELF.html#a4674a8acf19db9a120ec0af10ed25538',1,'binlex::ELF::ReadVector()'],['../classbinlex_1_1File.html#a29099a68d0b4f22be01c8818ec83d8a8',1,'binlex::File::ReadVector()'],['../classbinlex_1_1DOTNET.html#a1c59921ce0752ad78c176229a664b27c',1,'binlex::DOTNET::ReadVector()'],['../classbinlex_1_1PE.html#a4722bee8f84fa658852eb429d7e2399c',1,'binlex::PE::ReadVector()'],['../classbinlex_1_1Raw.html#a8c13d914c33d29f1df3d01e4cdc0273b',1,'binlex::Raw::ReadVector()']]], - ['removespaces_85',['RemoveSpaces',['../classbinlex_1_1Common.html#ad5ca8c8e5a5177c3bab1dafe1e9d17c0',1,'binlex::Common']]], - ['removewildcards_86',['RemoveWildcards',['../classbinlex_1_1Common.html#a35dbc25fdb59cf2d65eb35fcb1126c69',1,'binlex::Common']]], - ['resolutionscopeindex_87',['ResolutionScopeIndex',['../classdotnet_1_1ResolutionScopeIndex.html',1,'dotnet']]] -]; diff --git a/docs/html/search/all_d.html b/docs/html/search/all_d.html deleted file mode 100644 index bc376fec..00000000 --- a/docs/html/search/all_d.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/all_d.js b/docs/html/search/all_d.js deleted file mode 100644 index fddf0f94..00000000 --- a/docs/html/search/all_d.js +++ /dev/null @@ -1,13 +0,0 @@ -var searchData= -[ - ['section_88',['Section',['../structbinlex_1_1Decompiler_1_1Section.html',1,'binlex::Decompiler::Section'],['../structbinlex_1_1Raw_1_1Section.html',1,'binlex::Raw::Section'],['../structbinlex_1_1File_1_1Section.html',1,'binlex::File::Section'],['../structbinlex_1_1CILDecompiler_1_1Section.html',1,'binlex::CILDecompiler::Section']]], - ['set_5fio_5ftype_89',['set_io_type',['../classbinlex_1_1Args.html#a0589f1ceb485b09037e248c5867f0644',1,'binlex::Args']]], - ['setdefault_90',['SetDefault',['../classbinlex_1_1Args.html#a315af4879832687b962c26c559d9fb4d',1,'binlex::Args']]], - ['setinstructions_91',['SetInstructions',['../classbinlex_1_1Decompiler.html#a48c0658768bd8fc5683b964141103e68',1,'binlex::Decompiler']]], - ['setup_92',['Setup',['../classbinlex_1_1ELF.html#a1fedaeda9047ad1b92d720d7564d88e1',1,'binlex::ELF::Setup()'],['../classbinlex_1_1Decompiler.html#a87c28dc8b055a327f8c0502ed84c02f8',1,'binlex::Decompiler::Setup()'],['../classbinlex_1_1PE.html#aaf2fa4d79bc0fc9449eadd366b870d6b',1,'binlex::PE::Setup()']]], - ['sha256_93',['SHA256',['../classbinlex_1_1Common.html#a0c3eb3179bd7d87b3180387be76573e7',1,'binlex::Common::SHA256()'],['../classbinlex_1_1File.html#a131432625c0ab075df51a4ab63764fa9',1,'binlex::File::sha256()']]], - ['sha256_5fctx_94',['SHA256_CTX',['../structSHA256__CTX.html',1,'']]], - ['simpletableindex_95',['SimpleTableIndex',['../classdotnet_1_1SimpleTableIndex.html',1,'dotnet']]], - ['sizeoftrait_96',['SizeOfTrait',['../classbinlex_1_1CILDecompiler.html#ad36cfbf20a7ead33e32cf31464cb906d',1,'binlex::CILDecompiler']]], - ['stringheapindex_97',['StringHeapIndex',['../classdotnet_1_1StringHeapIndex.html',1,'dotnet']]] -]; diff --git a/docs/html/search/classes_0.html b/docs/html/search/classes_0.html deleted file mode 100644 index f7e4c14e..00000000 --- a/docs/html/search/classes_0.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/classes_0.js b/docs/html/search/classes_0.js deleted file mode 100644 index c78cef84..00000000 --- a/docs/html/search/classes_0.js +++ /dev/null @@ -1,5 +0,0 @@ -var searchData= -[ - ['args_114',['Args',['../classbinlex_1_1Args.html',1,'binlex']]], - ['autolex_115',['AutoLex',['../classbinlex_1_1AutoLex.html',1,'binlex']]] -]; diff --git a/docs/html/search/classes_1.html b/docs/html/search/classes_1.html deleted file mode 100644 index c7ff4b31..00000000 --- a/docs/html/search/classes_1.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/classes_1.js b/docs/html/search/classes_1.js deleted file mode 100644 index 328e8902..00000000 --- a/docs/html/search/classes_1.js +++ /dev/null @@ -1,4 +0,0 @@ -var searchData= -[ - ['blobheapindex_116',['BlobHeapIndex',['../classdotnet_1_1BlobHeapIndex.html',1,'dotnet']]] -]; diff --git a/docs/html/search/classes_2.html b/docs/html/search/classes_2.html deleted file mode 100644 index 0d1e8a0c..00000000 --- a/docs/html/search/classes_2.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/classes_2.js b/docs/html/search/classes_2.js deleted file mode 100644 index 47868913..00000000 --- a/docs/html/search/classes_2.js +++ /dev/null @@ -1,10 +0,0 @@ -var searchData= -[ - ['cildecompiler_117',['CILDecompiler',['../classbinlex_1_1CILDecompiler.html',1,'binlex']]], - ['common_118',['Common',['../classbinlex_1_1Common.html',1,'binlex']]], - ['cor20_5fheader_119',['COR20_HEADER',['../structdotnet_1_1COR20__HEADER.html',1,'dotnet']]], - ['cor20_5fstorage_5fheader_120',['COR20_STORAGE_HEADER',['../structdotnet_1_1COR20__STORAGE__HEADER.html',1,'dotnet']]], - ['cor20_5fstorage_5fsignature_121',['COR20_STORAGE_SIGNATURE',['../structdotnet_1_1COR20__STORAGE__SIGNATURE.html',1,'dotnet']]], - ['cor20_5fstream_5fheader_122',['COR20_STREAM_HEADER',['../structdotnet_1_1COR20__STREAM__HEADER.html',1,'dotnet']]], - ['cor20metadatatable_123',['Cor20MetadataTable',['../classdotnet_1_1Cor20MetadataTable.html',1,'dotnet']]] -]; diff --git a/docs/html/search/classes_3.html b/docs/html/search/classes_3.html deleted file mode 100644 index 21025456..00000000 --- a/docs/html/search/classes_3.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/classes_3.js b/docs/html/search/classes_3.js deleted file mode 100644 index 236916ea..00000000 --- a/docs/html/search/classes_3.js +++ /dev/null @@ -1,6 +0,0 @@ -var searchData= -[ - ['decompiler_124',['Decompiler',['../classbinlex_1_1Decompiler.html',1,'binlex']]], - ['decompilerbase_125',['DecompilerBase',['../classbinlex_1_1DecompilerBase.html',1,'binlex']]], - ['dotnet_126',['DOTNET',['../classbinlex_1_1DOTNET.html',1,'binlex']]] -]; diff --git a/docs/html/search/classes_4.html b/docs/html/search/classes_4.html deleted file mode 100644 index 095ab595..00000000 --- a/docs/html/search/classes_4.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/classes_4.js b/docs/html/search/classes_4.js deleted file mode 100644 index 4e5d90bb..00000000 --- a/docs/html/search/classes_4.js +++ /dev/null @@ -1,4 +0,0 @@ -var searchData= -[ - ['elf_127',['ELF',['../classbinlex_1_1ELF.html',1,'binlex']]] -]; diff --git a/docs/html/search/classes_5.html b/docs/html/search/classes_5.html deleted file mode 100644 index fc9cdc99..00000000 --- a/docs/html/search/classes_5.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/classes_5.js b/docs/html/search/classes_5.js deleted file mode 100644 index 27dd0a9d..00000000 --- a/docs/html/search/classes_5.js +++ /dev/null @@ -1,7 +0,0 @@ -var searchData= -[ - ['fatheader_128',['FatHeader',['../classdotnet_1_1FatHeader.html',1,'dotnet']]], - ['fieldentry_129',['FieldEntry',['../classdotnet_1_1FieldEntry.html',1,'dotnet']]], - ['fieldptrentry_130',['FieldPtrEntry',['../classdotnet_1_1FieldPtrEntry.html',1,'dotnet']]], - ['file_131',['File',['../classbinlex_1_1File.html',1,'binlex']]] -]; diff --git a/docs/html/search/classes_6.html b/docs/html/search/classes_6.html deleted file mode 100644 index 1ecfdddf..00000000 --- a/docs/html/search/classes_6.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/classes_6.js b/docs/html/search/classes_6.js deleted file mode 100644 index be0926b8..00000000 --- a/docs/html/search/classes_6.js +++ /dev/null @@ -1,4 +0,0 @@ -var searchData= -[ - ['guidheapindex_132',['GuidHeapIndex',['../classdotnet_1_1GuidHeapIndex.html',1,'dotnet']]] -]; diff --git a/docs/html/search/classes_7.html b/docs/html/search/classes_7.html deleted file mode 100644 index 0fc6fc3e..00000000 --- a/docs/html/search/classes_7.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/classes_7.js b/docs/html/search/classes_7.js deleted file mode 100644 index 3b1ac6c7..00000000 --- a/docs/html/search/classes_7.js +++ /dev/null @@ -1,4 +0,0 @@ -var searchData= -[ - ['instruction_133',['Instruction',['../structbinlex_1_1CILDecompiler_1_1Instruction.html',1,'binlex::CILDecompiler']]] -]; diff --git a/docs/html/search/functions_0.html b/docs/html/search/functions_0.html deleted file mode 100644 index e17c7111..00000000 --- a/docs/html/search/functions_0.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_0.js b/docs/html/search/functions_0.js deleted file mode 100644 index 7a382f70..00000000 --- a/docs/html/search/functions_0.js +++ /dev/null @@ -1,5 +0,0 @@ -var searchData= -[ - ['adddiscoveredblock_156',['AddDiscoveredBlock',['../classbinlex_1_1Decompiler.html#aac547ccd10ac7acefea742c37e05d69b',1,'binlex::Decompiler']]], - ['appendtrait_157',['AppendTrait',['../classbinlex_1_1Decompiler.html#a1af533c4e46a327d004c37bb609bb528',1,'binlex::Decompiler']]] -]; diff --git a/docs/html/search/functions_1.html b/docs/html/search/functions_1.html deleted file mode 100644 index 0ddac0a4..00000000 --- a/docs/html/search/functions_1.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_1.js b/docs/html/search/functions_1.js deleted file mode 100644 index 879a8f9c..00000000 --- a/docs/html/search/functions_1.js +++ /dev/null @@ -1,11 +0,0 @@ -var searchData= -[ - ['calculatefilehashes_158',['CalculateFileHashes',['../classbinlex_1_1File.html#a744ec07afb83c38432ab836cedbfc15b',1,'binlex::File::CalculateFileHashes(char *file_path)'],['../classbinlex_1_1File.html#ada874bdcd50ff6426c1bd479cc2be290',1,'binlex::File::CalculateFileHashes(const vector< uint8_t > &data)']]], - ['check_5fmode_159',['check_mode',['../classbinlex_1_1Args.html#ad39861cfaf635961de187ecccb8c8768',1,'binlex::Args']]], - ['cleartrait_160',['ClearTrait',['../classbinlex_1_1Decompiler.html#a98a6fe2d51d40a3c4763855e51b339d1',1,'binlex::Decompiler']]], - ['collectinsn_161',['CollectInsn',['../classbinlex_1_1Decompiler.html#a6792a979d3ed4881c63996d67bef6969',1,'binlex::Decompiler']]], - ['collectoperands_162',['CollectOperands',['../classbinlex_1_1Decompiler.html#a9ab3b7775ccb2aa1e3c8898960d44702',1,'binlex::Decompiler']]], - ['convbytes_163',['ConvBytes',['../classbinlex_1_1CILDecompiler.html#a2ed417089f22d5f86a94666768c54122',1,'binlex::CILDecompiler']]], - ['convtraitbytes_164',['ConvTraitBytes',['../classbinlex_1_1CILDecompiler.html#a480b9d36c9e0ee600294cb9e5ecd10a3',1,'binlex::CILDecompiler']]], - ['createtraitsforsection_165',['CreateTraitsForSection',['../classbinlex_1_1Decompiler.html#ae1a9193c009a4adcfd20441dc9896efe',1,'binlex::Decompiler']]] -]; diff --git a/docs/html/search/functions_2.html b/docs/html/search/functions_2.html deleted file mode 100644 index 2737c5ac..00000000 --- a/docs/html/search/functions_2.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_2.js b/docs/html/search/functions_2.js deleted file mode 100644 index 7efee033..00000000 --- a/docs/html/search/functions_2.js +++ /dev/null @@ -1,5 +0,0 @@ -var searchData= -[ - ['decompile_166',['Decompile',['../classbinlex_1_1Decompiler.html#acf647c06620257b5dc942c02c674e54f',1,'binlex::Decompiler']]], - ['decompilerbase_167',['DecompilerBase',['../classbinlex_1_1DecompilerBase.html#a0192d82e24d70b26ab2d86329c4e7591',1,'binlex::DecompilerBase']]] -]; diff --git a/docs/html/search/functions_3.html b/docs/html/search/functions_3.html deleted file mode 100644 index 6da86e7d..00000000 --- a/docs/html/search/functions_3.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_3.js b/docs/html/search/functions_3.js deleted file mode 100644 index 322e88b7..00000000 --- a/docs/html/search/functions_3.js +++ /dev/null @@ -1,4 +0,0 @@ -var searchData= -[ - ['entropy_168',['Entropy',['../classbinlex_1_1Common.html#a581cc519cdd00312a55fe0649a4b168d',1,'binlex::Common']]] -]; diff --git a/docs/html/search/functions_4.html b/docs/html/search/functions_4.html deleted file mode 100644 index 911304e6..00000000 --- a/docs/html/search/functions_4.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_4.js b/docs/html/search/functions_4.js deleted file mode 100644 index c4e946d2..00000000 --- a/docs/html/search/functions_4.js +++ /dev/null @@ -1,12 +0,0 @@ -var searchData= -[ - ['get_5ftags_5fas_5fstr_169',['get_tags_as_str',['../classbinlex_1_1Args.html#a07ab8a9c6d5a9578516a0b2de7999233',1,'binlex::Args']]], - ['getbytesize_170',['GetByteSize',['../classbinlex_1_1Common.html#afcb68aa2cb7c96abedd55b3fc77fabdd',1,'binlex::Common']]], - ['getfilesha256_171',['GetFileSHA256',['../classbinlex_1_1Common.html#acaf355a97857c4563c3b8e6162187e90',1,'binlex::Common']]], - ['getfilesize_172',['GetFileSize',['../classbinlex_1_1Raw.html#a8aacf571ea478ede2feb0dbfbd3e9756',1,'binlex::Raw']]], - ['getfiletlsh_173',['GetFileTLSH',['../classbinlex_1_1Common.html#a0c426523358896f7594a836cb286fca2',1,'binlex::Common']]], - ['getsha256_174',['GetSHA256',['../classbinlex_1_1Common.html#a85b33e0d5148b97b1db40d2f59b2679c',1,'binlex::Common']]], - ['gettlsh_175',['GetTLSH',['../classbinlex_1_1Common.html#adf666341ef78e3cba311e576e525fbf4',1,'binlex::Common']]], - ['gettrait_176',['GetTrait',['../classbinlex_1_1CILDecompiler.html#a6907fc607dfc77e0aabf1df12557923b',1,'binlex::CILDecompiler::GetTrait()'],['../classbinlex_1_1Decompiler.html#a1f766992f51fb6a39f26e0a111a3fd06',1,'binlex::Decompiler::GetTrait()']]], - ['gettraits_177',['GetTraits',['../classbinlex_1_1CILDecompiler.html#aedf8801582485d3abacd7d4c3769b7dc',1,'binlex::CILDecompiler::GetTraits()'],['../classbinlex_1_1Decompiler.html#afc5ee0847582e4df55b12bdb36e5ac1b',1,'binlex::Decompiler::GetTraits()'],['../classbinlex_1_1DecompilerBase.html#a436f937767ebbd74eb47e9d76fd849e2',1,'binlex::DecompilerBase::GetTraits()']]] -]; diff --git a/docs/html/search/functions_5.html b/docs/html/search/functions_5.html deleted file mode 100644 index 61b920db..00000000 --- a/docs/html/search/functions_5.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_5.js b/docs/html/search/functions_5.js deleted file mode 100644 index 1ee57792..00000000 --- a/docs/html/search/functions_5.js +++ /dev/null @@ -1,6 +0,0 @@ -var searchData= -[ - ['haslimitations_178',['HasLimitations',['../classbinlex_1_1PE.html#afc5cd018a384c015a298fe97d3658a3e',1,'binlex::PE']]], - ['hexdump_179',['Hexdump',['../classbinlex_1_1Common.html#a654dea4accfca1f897e7a7bc1587f343',1,'binlex::Common']]], - ['hexdumpbe_180',['HexdumpBE',['../classbinlex_1_1Common.html#a92eb94689731b4cf6a376b34eca54480',1,'binlex::Common']]] -]; diff --git a/docs/html/search/functions_6.html b/docs/html/search/functions_6.html deleted file mode 100644 index dc70a4a0..00000000 --- a/docs/html/search/functions_6.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_6.js b/docs/html/search/functions_6.js deleted file mode 100644 index c1c71f44..00000000 --- a/docs/html/search/functions_6.js +++ /dev/null @@ -1,18 +0,0 @@ -var searchData= -[ - ['is_5fdir_181',['is_dir',['../classbinlex_1_1Args.html#a4123f3468f9460122c5c57c3ee6ca3e6',1,'binlex::Args']]], - ['is_5ffile_182',['is_file',['../classbinlex_1_1Args.html#ad2f4242136bc00eeaed9417652306334',1,'binlex::Args']]], - ['isaddress_183',['IsAddress',['../classbinlex_1_1Decompiler.html#a4b98cc138ec7b506f54abf66cedd058d',1,'binlex::Decompiler']]], - ['isblock_184',['IsBlock',['../classbinlex_1_1Decompiler.html#ae18d074f16e507dcd6ec5b683c116570',1,'binlex::Decompiler']]], - ['isconditionalinsn_185',['IsConditionalInsn',['../classbinlex_1_1CILDecompiler.html#a988e9c0d563b2a24468e321ec015a8ba',1,'binlex::CILDecompiler::IsConditionalInsn()'],['../classbinlex_1_1Decompiler.html#a21cbd642799beb361b065e431fb5b4df',1,'binlex::Decompiler::IsConditionalInsn()']]], - ['isdotnet_186',['IsDotNet',['../classbinlex_1_1PE.html#a7d59d7af11c403d379d4b748065517d6',1,'binlex::PE']]], - ['isendinsn_187',['IsEndInsn',['../classbinlex_1_1Decompiler.html#aa28e4ef73f38f9fa1367570821a37864',1,'binlex::Decompiler']]], - ['isfunction_188',['IsFunction',['../classbinlex_1_1Decompiler.html#a78c24e43540d1e46aacc6fc10d3f95b2',1,'binlex::Decompiler']]], - ['isnopinsn_189',['IsNopInsn',['../classbinlex_1_1Decompiler.html#a3b0ba77bf8438ac544b232a3f00c3375',1,'binlex::Decompiler']]], - ['isprefixinstr_190',['IsPrefixInstr',['../classbinlex_1_1CILDecompiler.html#a365ed814ac434f9f55bc76e3d0a107c6',1,'binlex::CILDecompiler']]], - ['isprivinsn_191',['IsPrivInsn',['../classbinlex_1_1Decompiler.html#ad8006d35aaf95f840c249e28ed1f08b5',1,'binlex::Decompiler']]], - ['issemanticnopinsn_192',['IsSemanticNopInsn',['../classbinlex_1_1Decompiler.html#abeaa67c7f228d53db239d86e82fd5444',1,'binlex::Decompiler']]], - ['istrapinsn_193',['IsTrapInsn',['../classbinlex_1_1Decompiler.html#af3525ec47db26c08d6bdeaf9728a7ac7',1,'binlex::Decompiler']]], - ['isvisited_194',['IsVisited',['../classbinlex_1_1Decompiler.html#a22cafe99445d2c34cf60e5f62f44cdf7',1,'binlex::Decompiler']]], - ['iswildcardinsn_195',['IsWildcardInsn',['../classbinlex_1_1Decompiler.html#a88eb75d131555324252d66dcaeb44fa9',1,'binlex::Decompiler']]] -]; diff --git a/docs/html/search/functions_7.html b/docs/html/search/functions_7.html deleted file mode 100644 index 7de31067..00000000 --- a/docs/html/search/functions_7.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_7.js b/docs/html/search/functions_7.js deleted file mode 100644 index 12533300..00000000 --- a/docs/html/search/functions_7.js +++ /dev/null @@ -1,4 +0,0 @@ -var searchData= -[ - ['lineardisassemble_196',['LinearDisassemble',['../classbinlex_1_1Decompiler.html#a0045a92875a089edb6e862387851bb4d',1,'binlex::Decompiler']]] -]; diff --git a/docs/html/search/functions_8.html b/docs/html/search/functions_8.html deleted file mode 100644 index 7422be24..00000000 --- a/docs/html/search/functions_8.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_8.js b/docs/html/search/functions_8.js deleted file mode 100644 index e3b9b04f..00000000 --- a/docs/html/search/functions_8.js +++ /dev/null @@ -1,4 +0,0 @@ -var searchData= -[ - ['maxaddress_197',['MaxAddress',['../classbinlex_1_1Decompiler.html#a0df46873b8d487ed00bc252d59fb9aaa',1,'binlex::Decompiler']]] -]; diff --git a/docs/html/search/functions_9.html b/docs/html/search/functions_9.html deleted file mode 100644 index befd4faa..00000000 --- a/docs/html/search/functions_9.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_9.js b/docs/html/search/functions_9.js deleted file mode 100644 index fc37239b..00000000 --- a/docs/html/search/functions_9.js +++ /dev/null @@ -1,11 +0,0 @@ -var searchData= -[ - ['parse_198',['parse',['../classbinlex_1_1Args.html#ad98e40d517d1f5f00ea0957c91a565bc',1,'binlex::Args']]], - ['print_5fhelp_199',['print_help',['../classbinlex_1_1Args.html#abf121fddbd8c005159add8135fc81786',1,'binlex::Args']]], - ['processfile_200',['ProcessFile',['../classbinlex_1_1AutoLex.html#a3b8efd8b134bddb06956041230d4c7b9',1,'binlex::AutoLex']]], - ['py_5fsetcorpus_201',['py_SetCorpus',['../classbinlex_1_1DecompilerBase.html#a83d585e6e625f33db9f0411f137eec91',1,'binlex::DecompilerBase']]], - ['py_5fsetinstructions_202',['py_SetInstructions',['../classbinlex_1_1DecompilerBase.html#a05c7b3139af23eae016cc7a39bf52e6b',1,'binlex::DecompilerBase']]], - ['py_5fsetmode_203',['py_SetMode',['../classbinlex_1_1DecompilerBase.html#ac56114b8f7f1abc3e633aa27eedaf60a',1,'binlex::DecompilerBase']]], - ['py_5fsettags_204',['py_SetTags',['../classbinlex_1_1DecompilerBase.html#a0a0d7ff04acbf54166a42efa2eb89369',1,'binlex::DecompilerBase']]], - ['py_5fsetthreads_205',['py_SetThreads',['../classbinlex_1_1DecompilerBase.html#a46cc962e01cd3ba61b63ed5b259efbc8',1,'binlex::DecompilerBase']]] -]; diff --git a/docs/html/search/functions_a.html b/docs/html/search/functions_a.html deleted file mode 100644 index a81e9633..00000000 --- a/docs/html/search/functions_a.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_a.js b/docs/html/search/functions_a.js deleted file mode 100644 index d3cf79d5..00000000 --- a/docs/html/search/functions_a.js +++ /dev/null @@ -1,9 +0,0 @@ -var searchData= -[ - ['readbuffer_206',['ReadBuffer',['../classbinlex_1_1File.html#a7d80d86e8c30d68e11a7f024518c9172',1,'binlex::File']]], - ['readfile_207',['ReadFile',['../classbinlex_1_1File.html#af69581745439422e597e9c8ab2dcb27c',1,'binlex::File']]], - ['readfileintovector_208',['ReadFileIntoVector',['../classbinlex_1_1File.html#a2194b1dc5be789889ebe10099a778935',1,'binlex::File']]], - ['readvector_209',['ReadVector',['../classbinlex_1_1ELF.html#a4674a8acf19db9a120ec0af10ed25538',1,'binlex::ELF::ReadVector()'],['../classbinlex_1_1File.html#a29099a68d0b4f22be01c8818ec83d8a8',1,'binlex::File::ReadVector()'],['../classbinlex_1_1DOTNET.html#a1c59921ce0752ad78c176229a664b27c',1,'binlex::DOTNET::ReadVector()'],['../classbinlex_1_1PE.html#a4722bee8f84fa658852eb429d7e2399c',1,'binlex::PE::ReadVector()'],['../classbinlex_1_1Raw.html#a8c13d914c33d29f1df3d01e4cdc0273b',1,'binlex::Raw::ReadVector()']]], - ['removespaces_210',['RemoveSpaces',['../classbinlex_1_1Common.html#ad5ca8c8e5a5177c3bab1dafe1e9d17c0',1,'binlex::Common']]], - ['removewildcards_211',['RemoveWildcards',['../classbinlex_1_1Common.html#a35dbc25fdb59cf2d65eb35fcb1126c69',1,'binlex::Common']]] -]; diff --git a/docs/html/search/functions_b.html b/docs/html/search/functions_b.html deleted file mode 100644 index 345265d6..00000000 --- a/docs/html/search/functions_b.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/functions_b.js b/docs/html/search/functions_b.js deleted file mode 100644 index 41bf67b9..00000000 --- a/docs/html/search/functions_b.js +++ /dev/null @@ -1,9 +0,0 @@ -var searchData= -[ - ['set_5fio_5ftype_212',['set_io_type',['../classbinlex_1_1Args.html#a0589f1ceb485b09037e248c5867f0644',1,'binlex::Args']]], - ['setdefault_213',['SetDefault',['../classbinlex_1_1Args.html#a315af4879832687b962c26c559d9fb4d',1,'binlex::Args']]], - ['setinstructions_214',['SetInstructions',['../classbinlex_1_1Decompiler.html#a48c0658768bd8fc5683b964141103e68',1,'binlex::Decompiler']]], - ['setup_215',['Setup',['../classbinlex_1_1ELF.html#a1fedaeda9047ad1b92d720d7564d88e1',1,'binlex::ELF::Setup()'],['../classbinlex_1_1Decompiler.html#a87c28dc8b055a327f8c0502ed84c02f8',1,'binlex::Decompiler::Setup()'],['../classbinlex_1_1PE.html#aaf2fa4d79bc0fc9449eadd366b870d6b',1,'binlex::PE::Setup()']]], - ['sha256_216',['SHA256',['../classbinlex_1_1Common.html#a0c3eb3179bd7d87b3180387be76573e7',1,'binlex::Common']]], - ['sizeoftrait_217',['SizeOfTrait',['../classbinlex_1_1CILDecompiler.html#ad36cfbf20a7ead33e32cf31464cb906d',1,'binlex::CILDecompiler']]] -]; diff --git a/docs/html/search/namespaces_0.html b/docs/html/search/namespaces_0.html deleted file mode 100644 index 76996d1c..00000000 --- a/docs/html/search/namespaces_0.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/namespaces_0.js b/docs/html/search/namespaces_0.js deleted file mode 100644 index 00508006..00000000 --- a/docs/html/search/namespaces_0.js +++ /dev/null @@ -1,4 +0,0 @@ -var searchData= -[ - ['binlex_155',['binlex',['../namespacebinlex.html',1,'']]] -]; diff --git a/docs/html/search/nomatches.html b/docs/html/search/nomatches.html deleted file mode 100644 index 43773208..00000000 --- a/docs/html/search/nomatches.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - -
    -
    No Matches
    -
    - - diff --git a/docs/html/search/pages_0.html b/docs/html/search/pages_0.html deleted file mode 100644 index 9a6a29ad..00000000 --- a/docs/html/search/pages_0.html +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - -
    -
    Loading...
    -
    - -
    Searching...
    -
    No Matches
    - -
    - - diff --git a/docs/html/search/pages_0.js b/docs/html/search/pages_0.js deleted file mode 100644 index 7c199697..00000000 --- a/docs/html/search/pages_0.js +++ /dev/null @@ -1,4 +0,0 @@ -var searchData= -[ - ['binlex_229',['binlex',['../index.html',1,'']]] -]; diff --git a/docs/html/search/search.css b/docs/html/search/search.css deleted file mode 100644 index 3cf9df94..00000000 --- a/docs/html/search/search.css +++ /dev/null @@ -1,271 +0,0 @@ -/*---------------- Search Box */ - -#FSearchBox { - float: left; -} - -#MSearchBox { - white-space : nowrap; - float: none; - margin-top: 8px; - right: 0px; - width: 170px; - height: 24px; - z-index: 102; -} - -#MSearchBox .left -{ - display:block; - position:absolute; - left:10px; - width:20px; - height:19px; - background:url('search_l.png') no-repeat; - background-position:right; -} - -#MSearchSelect { - display:block; - position:absolute; - width:20px; - height:19px; -} - -.left #MSearchSelect { - left:4px; -} - -.right #MSearchSelect { - right:5px; -} - -#MSearchField { - display:block; - position:absolute; - height:19px; - background:url('search_m.png') repeat-x; - border:none; - width:115px; - margin-left:20px; - padding-left:4px; - color: #909090; - outline: none; - font: 9pt Arial, Verdana, sans-serif; - -webkit-border-radius: 0px; -} - -#FSearchBox #MSearchField { - margin-left:15px; -} - -#MSearchBox .right { - display:block; - position:absolute; - right:10px; - top:8px; - width:20px; - height:19px; - background:url('search_r.png') no-repeat; - background-position:left; -} - -#MSearchClose { - display: none; - position: absolute; - top: 4px; - background : none; - border: none; - margin: 0px 4px 0px 0px; - padding: 0px 0px; - outline: none; -} - -.left #MSearchClose { - left: 6px; -} - -.right #MSearchClose { - right: 2px; -} - -.MSearchBoxActive #MSearchField { - color: #000000; -} - -/*---------------- Search filter selection */ - -#MSearchSelectWindow { - display: none; - position: absolute; - left: 0; top: 0; - border: 1px solid #90A5CE; - background-color: #F9FAFC; - z-index: 10001; - padding-top: 4px; - padding-bottom: 4px; - -moz-border-radius: 4px; - -webkit-border-top-left-radius: 4px; - -webkit-border-top-right-radius: 4px; - -webkit-border-bottom-left-radius: 4px; - -webkit-border-bottom-right-radius: 4px; - -webkit-box-shadow: 5px 5px 5px rgba(0, 0, 0, 0.15); -} - -.SelectItem { - font: 8pt Arial, Verdana, sans-serif; - padding-left: 2px; - padding-right: 12px; - border: 0px; -} - -span.SelectionMark { - margin-right: 4px; - font-family: monospace; - outline-style: none; - text-decoration: none; -} - -a.SelectItem { - display: block; - outline-style: none; - color: #000000; - text-decoration: none; - padding-left: 6px; - padding-right: 12px; -} - -a.SelectItem:focus, -a.SelectItem:active { - color: #000000; - outline-style: none; - text-decoration: none; -} - -a.SelectItem:hover { - color: #FFFFFF; - background-color: #3D578C; - outline-style: none; - text-decoration: none; - cursor: pointer; - display: block; -} - -/*---------------- Search results window */ - -iframe#MSearchResults { - width: 60ex; - height: 15em; -} - -#MSearchResultsWindow { - display: none; - position: absolute; - left: 0; top: 0; - border: 1px solid #000; - background-color: #EEF1F7; - z-index:10000; -} - -/* ----------------------------------- */ - - -#SRIndex { - clear:both; - padding-bottom: 15px; -} - -.SREntry { - font-size: 10pt; - padding-left: 1ex; -} - -.SRPage .SREntry { - font-size: 8pt; - padding: 1px 5px; -} - -body.SRPage { - margin: 5px 2px; -} - -.SRChildren { - padding-left: 3ex; padding-bottom: .5em -} - -.SRPage .SRChildren { - display: none; -} - -.SRSymbol { - font-weight: bold; - color: #425E97; - font-family: Arial, Verdana, sans-serif; - text-decoration: none; - outline: none; -} - -a.SRScope { - display: block; - color: #425E97; - font-family: Arial, Verdana, sans-serif; - text-decoration: none; - outline: none; -} - -a.SRSymbol:focus, a.SRSymbol:active, -a.SRScope:focus, a.SRScope:active { - text-decoration: underline; -} - -span.SRScope { - padding-left: 4px; -} - -.SRPage .SRStatus { - padding: 2px 5px; - font-size: 8pt; - font-style: italic; -} - -.SRResult { - display: none; -} - -DIV.searchresults { - margin-left: 10px; - margin-right: 10px; -} - -/*---------------- External search page results */ - -.searchresult { - background-color: #F0F3F8; -} - -.pages b { - color: white; - padding: 5px 5px 3px 5px; - background-image: url("../tab_a.png"); - background-repeat: repeat-x; - text-shadow: 0 1px 1px #000000; -} - -.pages { - line-height: 17px; - margin-left: 4px; - text-decoration: none; -} - -.hl { - font-weight: bold; -} - -#searchresults { - margin-bottom: 20px; -} - -.searchpages { - margin-top: 10px; -} - diff --git a/docs/html/search/search.js b/docs/html/search/search.js deleted file mode 100644 index a554ab9c..00000000 --- a/docs/html/search/search.js +++ /dev/null @@ -1,814 +0,0 @@ -/* - @licstart The following is the entire license notice for the - JavaScript code in this file. - - Copyright (C) 1997-2017 by Dimitri van Heesch - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 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 General Public License for more details. - - You should have received a copy of the GNU General Public License along - with this program; if not, write to the Free Software Foundation, Inc., - 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - - @licend The above is the entire license notice - for the JavaScript code in this file - */ -function convertToId(search) -{ - var result = ''; - for (i=0;i do a search - { - this.Search(); - } - } - - this.OnSearchSelectKey = function(evt) - { - var e = (evt) ? evt : window.event; // for IE - if (e.keyCode==40 && this.searchIndex0) // Up - { - this.searchIndex--; - this.OnSelectItem(this.searchIndex); - } - else if (e.keyCode==13 || e.keyCode==27) - { - this.OnSelectItem(this.searchIndex); - this.CloseSelectionWindow(); - this.DOMSearchField().focus(); - } - return false; - } - - // --------- Actions - - // Closes the results window. - this.CloseResultsWindow = function() - { - this.DOMPopupSearchResultsWindow().style.display = 'none'; - this.DOMSearchClose().style.display = 'none'; - this.Activate(false); - } - - this.CloseSelectionWindow = function() - { - this.DOMSearchSelectWindow().style.display = 'none'; - } - - // Performs a search. - this.Search = function() - { - this.keyTimeout = 0; - - // strip leading whitespace - var searchValue = this.DOMSearchField().value.replace(/^ +/, ""); - - var code = searchValue.toLowerCase().charCodeAt(0); - var idxChar = searchValue.substr(0, 1).toLowerCase(); - if ( 0xD800 <= code && code <= 0xDBFF && searchValue > 1) // surrogate pair - { - idxChar = searchValue.substr(0, 2); - } - - var resultsPage; - var resultsPageWithSearch; - var hasResultsPage; - - var idx = indexSectionsWithContent[this.searchIndex].indexOf(idxChar); - if (idx!=-1) - { - var hexCode=idx.toString(16); - resultsPage = this.resultsPath + '/' + indexSectionNames[this.searchIndex] + '_' + hexCode + '.html'; - resultsPageWithSearch = resultsPage+'?'+escape(searchValue); - hasResultsPage = true; - } - else // nothing available for this search term - { - resultsPage = this.resultsPath + '/nomatches.html'; - resultsPageWithSearch = resultsPage; - hasResultsPage = false; - } - - window.frames.MSearchResults.location = resultsPageWithSearch; - var domPopupSearchResultsWindow = this.DOMPopupSearchResultsWindow(); - - if (domPopupSearchResultsWindow.style.display!='block') - { - var domSearchBox = this.DOMSearchBox(); - this.DOMSearchClose().style.display = 'inline'; - if (this.insideFrame) - { - var domPopupSearchResults = this.DOMPopupSearchResults(); - domPopupSearchResultsWindow.style.position = 'relative'; - domPopupSearchResultsWindow.style.display = 'block'; - var width = document.body.clientWidth - 8; // the -8 is for IE :-( - domPopupSearchResultsWindow.style.width = width + 'px'; - domPopupSearchResults.style.width = width + 'px'; - } - else - { - var domPopupSearchResults = this.DOMPopupSearchResults(); - var left = getXPos(domSearchBox) + 150; // domSearchBox.offsetWidth; - var top = getYPos(domSearchBox) + 20; // domSearchBox.offsetHeight + 1; - domPopupSearchResultsWindow.style.display = 'block'; - left -= domPopupSearchResults.offsetWidth; - domPopupSearchResultsWindow.style.top = top + 'px'; - domPopupSearchResultsWindow.style.left = left + 'px'; - } - } - - this.lastSearchValue = searchValue; - this.lastResultsPage = resultsPage; - } - - // -------- Activation Functions - - // Activates or deactivates the search panel, resetting things to - // their default values if necessary. - this.Activate = function(isActive) - { - if (isActive || // open it - this.DOMPopupSearchResultsWindow().style.display == 'block' - ) - { - this.DOMSearchBox().className = 'MSearchBoxActive'; - - var searchField = this.DOMSearchField(); - - if (searchField.value == this.searchLabel) // clear "Search" term upon entry - { - searchField.value = ''; - this.searchActive = true; - } - } - else if (!isActive) // directly remove the panel - { - this.DOMSearchBox().className = 'MSearchBoxInactive'; - this.DOMSearchField().value = this.searchLabel; - this.searchActive = false; - this.lastSearchValue = '' - this.lastResultsPage = ''; - } - } -} - -// ----------------------------------------------------------------------- - -// The class that handles everything on the search results page. -function SearchResults(name) -{ - // The number of matches from the last run of . - this.lastMatchCount = 0; - this.lastKey = 0; - this.repeatOn = false; - - // Toggles the visibility of the passed element ID. - this.FindChildElement = function(id) - { - var parentElement = document.getElementById(id); - var element = parentElement.firstChild; - - while (element && element!=parentElement) - { - if (element.nodeName == 'DIV' && element.className == 'SRChildren') - { - return element; - } - - if (element.nodeName == 'DIV' && element.hasChildNodes()) - { - element = element.firstChild; - } - else if (element.nextSibling) - { - element = element.nextSibling; - } - else - { - do - { - element = element.parentNode; - } - while (element && element!=parentElement && !element.nextSibling); - - if (element && element!=parentElement) - { - element = element.nextSibling; - } - } - } - } - - this.Toggle = function(id) - { - var element = this.FindChildElement(id); - if (element) - { - if (element.style.display == 'block') - { - element.style.display = 'none'; - } - else - { - element.style.display = 'block'; - } - } - } - - // Searches for the passed string. If there is no parameter, - // it takes it from the URL query. - // - // Always returns true, since other documents may try to call it - // and that may or may not be possible. - this.Search = function(search) - { - if (!search) // get search word from URL - { - search = window.location.search; - search = search.substring(1); // Remove the leading '?' - search = unescape(search); - } - - search = search.replace(/^ +/, ""); // strip leading spaces - search = search.replace(/ +$/, ""); // strip trailing spaces - search = search.toLowerCase(); - search = convertToId(search); - - var resultRows = document.getElementsByTagName("div"); - var matches = 0; - - var i = 0; - while (i < resultRows.length) - { - var row = resultRows.item(i); - if (row.className == "SRResult") - { - var rowMatchName = row.id.toLowerCase(); - rowMatchName = rowMatchName.replace(/^sr\d*_/, ''); // strip 'sr123_' - - if (search.length<=rowMatchName.length && - rowMatchName.substr(0, search.length)==search) - { - row.style.display = 'block'; - matches++; - } - else - { - row.style.display = 'none'; - } - } - i++; - } - document.getElementById("Searching").style.display='none'; - if (matches == 0) // no results - { - document.getElementById("NoMatches").style.display='block'; - } - else // at least one result - { - document.getElementById("NoMatches").style.display='none'; - } - this.lastMatchCount = matches; - return true; - } - - // return the first item with index index or higher that is visible - this.NavNext = function(index) - { - var focusItem; - while (1) - { - var focusName = 'Item'+index; - focusItem = document.getElementById(focusName); - if (focusItem && focusItem.parentNode.parentNode.style.display=='block') - { - break; - } - else if (!focusItem) // last element - { - break; - } - focusItem=null; - index++; - } - return focusItem; - } - - this.NavPrev = function(index) - { - var focusItem; - while (1) - { - var focusName = 'Item'+index; - focusItem = document.getElementById(focusName); - if (focusItem && focusItem.parentNode.parentNode.style.display=='block') - { - break; - } - else if (!focusItem) // last element - { - break; - } - focusItem=null; - index--; - } - return focusItem; - } - - this.ProcessKeys = function(e) - { - if (e.type == "keydown") - { - this.repeatOn = false; - this.lastKey = e.keyCode; - } - else if (e.type == "keypress") - { - if (!this.repeatOn) - { - if (this.lastKey) this.repeatOn = true; - return false; // ignore first keypress after keydown - } - } - else if (e.type == "keyup") - { - this.lastKey = 0; - this.repeatOn = false; - } - return this.lastKey!=0; - } - - this.Nav = function(evt,itemIndex) - { - var e = (evt) ? evt : window.event; // for IE - if (e.keyCode==13) return true; - if (!this.ProcessKeys(e)) return false; - - if (this.lastKey==38) // Up - { - var newIndex = itemIndex-1; - var focusItem = this.NavPrev(newIndex); - if (focusItem) - { - var child = this.FindChildElement(focusItem.parentNode.parentNode.id); - if (child && child.style.display == 'block') // children visible - { - var n=0; - var tmpElem; - while (1) // search for last child - { - tmpElem = document.getElementById('Item'+newIndex+'_c'+n); - if (tmpElem) - { - focusItem = tmpElem; - } - else // found it! - { - break; - } - n++; - } - } - } - if (focusItem) - { - focusItem.focus(); - } - else // return focus to search field - { - parent.document.getElementById("MSearchField").focus(); - } - } - else if (this.lastKey==40) // Down - { - var newIndex = itemIndex+1; - var focusItem; - var item = document.getElementById('Item'+itemIndex); - var elem = this.FindChildElement(item.parentNode.parentNode.id); - if (elem && elem.style.display == 'block') // children visible - { - focusItem = document.getElementById('Item'+itemIndex+'_c0'); - } - if (!focusItem) focusItem = this.NavNext(newIndex); - if (focusItem) focusItem.focus(); - } - else if (this.lastKey==39) // Right - { - var item = document.getElementById('Item'+itemIndex); - var elem = this.FindChildElement(item.parentNode.parentNode.id); - if (elem) elem.style.display = 'block'; - } - else if (this.lastKey==37) // Left - { - var item = document.getElementById('Item'+itemIndex); - var elem = this.FindChildElement(item.parentNode.parentNode.id); - if (elem) elem.style.display = 'none'; - } - else if (this.lastKey==27) // Escape - { - parent.searchBox.CloseResultsWindow(); - parent.document.getElementById("MSearchField").focus(); - } - else if (this.lastKey==13) // Enter - { - return true; - } - return false; - } - - this.NavChild = function(evt,itemIndex,childIndex) - { - var e = (evt) ? evt : window.event; // for IE - if (e.keyCode==13) return true; - if (!this.ProcessKeys(e)) return false; - - if (this.lastKey==38) // Up - { - if (childIndex>0) - { - var newIndex = childIndex-1; - document.getElementById('Item'+itemIndex+'_c'+newIndex).focus(); - } - else // already at first child, jump to parent - { - document.getElementById('Item'+itemIndex).focus(); - } - } - else if (this.lastKey==40) // Down - { - var newIndex = childIndex+1; - var elem = document.getElementById('Item'+itemIndex+'_c'+newIndex); - if (!elem) // last child, jump to parent next parent - { - elem = this.NavNext(itemIndex+1); - } - if (elem) - { - elem.focus(); - } - } - else if (this.lastKey==27) // Escape - { - parent.searchBox.CloseResultsWindow(); - parent.document.getElementById("MSearchField").focus(); - } - else if (this.lastKey==13) // Enter - { - return true; - } - return false; - } -} - -function setKeyActions(elem,action) -{ - elem.setAttribute('onkeydown',action); - elem.setAttribute('onkeypress',action); - elem.setAttribute('onkeyup',action); -} - -function setClassAttr(elem,attr) -{ - elem.setAttribute('class',attr); - elem.setAttribute('className',attr); -} - -function createResults() -{ - var results = document.getElementById("SRResults"); - for (var e=0; e - - - - - - -binlex: include/sha256.h Source File - - - - - - - - - - - - - - -
    -
    - - - - - - -
    -
    binlex -
    -
    -
    - - - - - - - -
    -
    - -
    -
    -
    - -
    - -
    -
    - - -
    - -
    - -
    -
    -
    sha256.h
    -
    -
    -
    1 /*********************************************************************
    -
    2 * Filename: sha256.h
    -
    3 * Author: Brad Conte (brad AT bradconte.com)
    -
    4 * Copyright:
    -
    5 * Disclaimer: This code is presented "as is" without any guarantees.
    -
    6 * Details: Defines the API for the corresponding SHA1 implementation.
    -
    7 *********************************************************************/
    -
    8 
    -
    9 #ifndef SHA256_H
    -
    10 #define SHA256_H
    -
    11 
    -
    12 /*************************** HEADER FILES ***************************/
    -
    13 #include <stddef.h>
    -
    14 
    -
    15 /****************************** MACROS ******************************/
    -
    16 #define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
    -
    17 
    -
    18 /**************************** DATA TYPES ****************************/
    -
    19 typedef unsigned char BYTE; // 8-bit byte
    -
    20 typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines
    -
    21 
    -
    22 typedef struct {
    -
    23  BYTE data[64];
    -
    24  WORD datalen;
    -
    25  unsigned long long bitlen;
    -
    26  WORD state[8];
    -
    27 } SHA256_CTX;
    -
    28 
    -
    29 /*********************** FUNCTION DECLARATIONS **********************/
    -
    30 void sha256_init(SHA256_CTX *ctx);
    -
    31 void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);
    -
    32 void sha256_final(SHA256_CTX *ctx, BYTE hash[]);
    -
    33 
    -
    34 #endif // SHA256_H
    -
    -
    -
    Definition: sha256.h:22
    - - - - diff --git a/docs/html/splitbar.png b/docs/html/splitbar.png deleted file mode 100644 index fe895f2c..00000000 Binary files a/docs/html/splitbar.png and /dev/null differ diff --git a/docs/html/structSHA256__CTX-members.html b/docs/html/structSHA256__CTX-members.html deleted file mode 100644 index 5c015de6..00000000 --- a/docs/html/structSHA256__CTX-members.html +++ /dev/null @@ -1,107 +0,0 @@ - - - - - - - -binlex: Member List - - - - - - - - - - - - - - -
    -
    - - - - - - -
    -
    binlex -
    -
    -
    - - - - - - - -
    -
    - -
    -
    -
    - -
    - -
    -
    - - -
    - -
    - -
    -
    -
    SHA256_CTX Member List
    -
    -
    - -

    This is the complete list of members for SHA256_CTX, including all inherited members.

    - - - - - -
    bitlen (defined in SHA256_CTX)SHA256_CTX
    data (defined in SHA256_CTX)SHA256_CTX
    datalen (defined in SHA256_CTX)SHA256_CTX
    state (defined in SHA256_CTX)SHA256_CTX
    -
    - - - - diff --git a/docs/html/structSHA256__CTX.html b/docs/html/structSHA256__CTX.html deleted file mode 100644 index eb202e08..00000000 --- a/docs/html/structSHA256__CTX.html +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - -binlex: SHA256_CTX Struct Reference - - - - - - - - - - - - - - -
    -
    - - - - - - -
    -
    binlex -
    -
    -
    - - - - - - - -
    -
    - -
    -
    -
    - -
    - -
    -
    - - -
    - -
    - -
    - -
    -
    SHA256_CTX Struct Reference
    -
    -
    - - - - - - - - - - -

    -Public Attributes

    -BYTE data [64]
     
    -WORD datalen
     
    -unsigned long long bitlen
     
    -WORD state [8]
     
    -
    The documentation for this struct was generated from the following file: -
    -
    - - - - diff --git a/docs/html/structSHA256__CTX.js b/docs/html/structSHA256__CTX.js deleted file mode 100644 index f2557681..00000000 --- a/docs/html/structSHA256__CTX.js +++ /dev/null @@ -1,7 +0,0 @@ -var structSHA256__CTX = -[ - [ "bitlen", "structSHA256__CTX.html#a7971befc0fa37b07350552d3a949634a", null ], - [ "data", "structSHA256__CTX.html#ac4cf981ce4100c3af150be3c98b2d03e", null ], - [ "datalen", "structSHA256__CTX.html#acff27dfecde1d6e42f7c8474a3175529", null ], - [ "state", "structSHA256__CTX.html#a09c4eef8a0dc02bc9860e2a02c3f2638", null ] -]; \ No newline at end of file diff --git a/docs/html/structbinlex_1_1Decompiler_1_1Section-members.html b/docs/html/structbinlex_1_1Decompiler_1_1Section-members.html deleted file mode 100644 index fa9bb70c..00000000 --- a/docs/html/structbinlex_1_1Decompiler_1_1Section-members.html +++ /dev/null @@ -1,113 +0,0 @@ - - - - - - - -binlex: Member List - - - - - - - - - - - - - - -
    -
    - - - - - - -
    -
    binlex -
    -
    -
    - - - - - - - -
    -
    - -
    -
    -
    - -
    - -
    -
    - - -
    - -
    - -
    -
    -
    binlex::Decompiler::Section Member List
    -
    - -
    - - - - diff --git a/docs/html/structbinlex_1_1Decompiler_1_1Section.html b/docs/html/structbinlex_1_1Decompiler_1_1Section.html deleted file mode 100644 index a53bb6f0..00000000 --- a/docs/html/structbinlex_1_1Decompiler_1_1Section.html +++ /dev/null @@ -1,141 +0,0 @@ - - - - - - - -binlex: binlex::Decompiler::Section Struct Reference - - - - - - - - - - - - - - -
    -
    - - - - - - -
    -
    binlex -
    -
    -
    - - - - - - - -
    -
    - -
    -
    -
    - -
    - -
    -
    - - -
    - -
    - -
    - -
    -
    binlex::Decompiler::Section Struct Reference
    -
    -
    - - - - - - - - - - - - - - - - - - - - - - -

    -Public Attributes

    -char * cpu
     
    -bool instructions
     
    -uint offset
     
    -vector< struct Traittraits
     
    -void * data
     
    -size_t data_size
     
    -set< uint64_t > coverage
     
    -map< uint64_t, uint > addresses
     
    -map< uint64_t, int > visited
     
    -queue< uint64_t > discovered
     
    -
    The documentation for this struct was generated from the following file: -
    -
    - - - - diff --git a/docs/html/structbinlex_1_1Decompiler_1_1Section.js b/docs/html/structbinlex_1_1Decompiler_1_1Section.js deleted file mode 100644 index ab51722d..00000000 --- a/docs/html/structbinlex_1_1Decompiler_1_1Section.js +++ /dev/null @@ -1,13 +0,0 @@ -var structbinlex_1_1Decompiler_1_1Section = -[ - [ "addresses", "structbinlex_1_1Decompiler_1_1Section.html#a806c48881ca4ca0a601d65d43a7ac016", null ], - [ "coverage", "structbinlex_1_1Decompiler_1_1Section.html#add47fadfaeaa63a7ac733a1a4ca9e142", null ], - [ "cpu", "structbinlex_1_1Decompiler_1_1Section.html#a826b5b993ace1b352ade095843207abe", null ], - [ "data", "structbinlex_1_1Decompiler_1_1Section.html#a2bf16240eede8bd9fe0a3ac3a398c9d1", null ], - [ "data_size", "structbinlex_1_1Decompiler_1_1Section.html#a1bd97ab7f60be6348fdfc7c9226c287e", null ], - [ "discovered", "structbinlex_1_1Decompiler_1_1Section.html#a42c1f940ded2047679bfed897c85bbe2", null ], - [ "instructions", "structbinlex_1_1Decompiler_1_1Section.html#a8ac6293a8fe8dcde51b4415f356ec791", null ], - [ "offset", "structbinlex_1_1Decompiler_1_1Section.html#a177fe4deb5ae98d8fedd297ffab2c477", null ], - [ "traits", "structbinlex_1_1Decompiler_1_1Section.html#a8a6b1dfb624dc361c96f0960d3a57452", null ], - [ "visited", "structbinlex_1_1Decompiler_1_1Section.html#ace04417199c92121e8afcd4dfe703423", null ] -]; \ No newline at end of file diff --git a/docs/html/structbinlex_1_1Decompiler_1_1Trait-members.html b/docs/html/structbinlex_1_1Decompiler_1_1Trait-members.html deleted file mode 100644 index dc2f2441..00000000 --- a/docs/html/structbinlex_1_1Decompiler_1_1Trait-members.html +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - -binlex: Member List - - - - - - - - - - - - - - -
    -
    - - - - - - -
    -
    binlex -
    -
    -
    - - - - - - - -
    -
    - -
    -
    -
    - -
    - -
    -
    - - -
    - -
    - -
    -
    -
    binlex::Decompiler::Trait Member List
    -
    - -
    - - - - diff --git a/docs/html/structbinlex_1_1Decompiler_1_1Trait.html b/docs/html/structbinlex_1_1Decompiler_1_1Trait.html deleted file mode 100644 index 110c2226..00000000 --- a/docs/html/structbinlex_1_1Decompiler_1_1Trait.html +++ /dev/null @@ -1,162 +0,0 @@ - - - - - - - -binlex: binlex::Decompiler::Trait Struct Reference - - - - - - - - - - - - - - -
    -
    - - - - - - -
    -
    binlex -
    -
    -
    - - - - - - - -
    -
    - -
    -
    -
    - -
    - -
    -
    - - -
    - -
    - -
    - -
    -
    binlex::Decompiler::Trait Struct Reference
    -
    -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    -Public Attributes

    -string type
     
    -string tmp_bytes
     
    -string bytes
     
    -string tmp_trait
     
    -string trait
     
    -uint edges
     
    -uint blocks
     
    -uint instructions
     
    -uint size
     
    -uint offset
     
    -uint invalid_instructions
     
    -uint cyclomatic_complexity
     
    -uint average_instructions_per_block
     
    -float bytes_entropy
     
    -float trait_entropy
     
    -char bytes_sha256 [SHA256_PRINTABLE_SIZE]
     
    -char trait_sha256 [SHA256_PRINTABLE_SIZE]
     
    -
    The documentation for this struct was generated from the following file: -
    -
    - - - - diff --git a/docs/html/structbinlex_1_1Decompiler_1_1Trait.js b/docs/html/structbinlex_1_1Decompiler_1_1Trait.js deleted file mode 100644 index 5dd513a7..00000000 --- a/docs/html/structbinlex_1_1Decompiler_1_1Trait.js +++ /dev/null @@ -1,20 +0,0 @@ -var structbinlex_1_1Decompiler_1_1Trait = -[ - [ "average_instructions_per_block", "structbinlex_1_1Decompiler_1_1Trait.html#a14633d33e290465f61101e6ad46c7e03", null ], - [ "blocks", "structbinlex_1_1Decompiler_1_1Trait.html#a4f00fdeeaedb1fc3f487d0a8344ca25d", null ], - [ "bytes", "structbinlex_1_1Decompiler_1_1Trait.html#a182b37c8d727ecc7f0a421d59a3e3b75", null ], - [ "bytes_entropy", "structbinlex_1_1Decompiler_1_1Trait.html#a5f21bb73d7832500f31d2e017b34959b", null ], - [ "bytes_sha256", "structbinlex_1_1Decompiler_1_1Trait.html#ac7ad188aabefa4135b505f14178f49b5", null ], - [ "cyclomatic_complexity", "structbinlex_1_1Decompiler_1_1Trait.html#a6c305a83df4c0d31173e4af6863440ce", null ], - [ "edges", "structbinlex_1_1Decompiler_1_1Trait.html#a88185c92aa60a59c52485e4a50122216", null ], - [ "instructions", "structbinlex_1_1Decompiler_1_1Trait.html#a9084e24801a6cacc2047772876a49baa", null ], - [ "invalid_instructions", "structbinlex_1_1Decompiler_1_1Trait.html#a9fe6e699d26db5df47ac609dce8f5762", null ], - [ "offset", "structbinlex_1_1Decompiler_1_1Trait.html#a25d5f70a2f321aff60066bee98dcabf9", null ], - [ "size", "structbinlex_1_1Decompiler_1_1Trait.html#a850f31820aeabf18ae90d9ea47e31f95", null ], - [ "tmp_bytes", "structbinlex_1_1Decompiler_1_1Trait.html#a60bafbcf89561be8d7f67219f3c9ba03", null ], - [ "tmp_trait", "structbinlex_1_1Decompiler_1_1Trait.html#a42185f18cf8b3d8faeb5354c1ca2d7b5", null ], - [ "trait", "structbinlex_1_1Decompiler_1_1Trait.html#a26580360ad39d28898336b3b3b3244e1", null ], - [ "trait_entropy", "structbinlex_1_1Decompiler_1_1Trait.html#ad031b514b7abd6bce62b7750f1cdb98a", null ], - [ "trait_sha256", "structbinlex_1_1Decompiler_1_1Trait.html#a09312688dd0f8c00a658c741b65ae90d", null ], - [ "type", "structbinlex_1_1Decompiler_1_1Trait.html#aed00dca6f240dc8ba79d855156383063", null ] -]; \ No newline at end of file diff --git a/docs/html/sync_off.png b/docs/html/sync_off.png deleted file mode 100644 index 3b443fc6..00000000 Binary files a/docs/html/sync_off.png and /dev/null differ diff --git a/docs/html/sync_on.png b/docs/html/sync_on.png deleted file mode 100644 index e08320fb..00000000 Binary files a/docs/html/sync_on.png and /dev/null differ diff --git a/docs/html/tab_a.png b/docs/html/tab_a.png deleted file mode 100644 index 3b725c41..00000000 Binary files a/docs/html/tab_a.png and /dev/null differ diff --git a/docs/html/tab_b.png b/docs/html/tab_b.png deleted file mode 100644 index e2b4a863..00000000 Binary files a/docs/html/tab_b.png and /dev/null differ diff --git a/docs/html/tab_h.png b/docs/html/tab_h.png deleted file mode 100644 index fd5cb705..00000000 Binary files a/docs/html/tab_h.png and /dev/null differ diff --git a/docs/html/tab_s.png b/docs/html/tab_s.png deleted file mode 100644 index ab478c95..00000000 Binary files a/docs/html/tab_s.png and /dev/null differ diff --git a/docs/html/tabs.css b/docs/html/tabs.css deleted file mode 100644 index 7d45d36c..00000000 --- a/docs/html/tabs.css +++ /dev/null @@ -1 +0,0 @@ -.sm{position:relative;z-index:9999}.sm,.sm ul,.sm li{display:block;list-style:none;margin:0;padding:0;line-height:normal;direction:ltr;text-align:left;-webkit-tap-highlight-color:rgba(0,0,0,0)}.sm-rtl,.sm-rtl ul,.sm-rtl li{direction:rtl;text-align:right}.sm>li>h1,.sm>li>h2,.sm>li>h3,.sm>li>h4,.sm>li>h5,.sm>li>h6{margin:0;padding:0}.sm ul{display:none}.sm li,.sm a{position:relative}.sm a{display:block}.sm a.disabled{cursor:not-allowed}.sm:after{content:"\00a0";display:block;height:0;font:0px/0 serif;clear:both;visibility:hidden;overflow:hidden}.sm,.sm *,.sm *:before,.sm *:after{-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box}.sm-dox{background-image:url("tab_b.png")}.sm-dox a,.sm-dox a:focus,.sm-dox a:hover,.sm-dox a:active{padding:0px 12px;padding-right:43px;font-family:"Lucida Grande","Geneva","Helvetica",Arial,sans-serif;font-size:13px;font-weight:bold;line-height:36px;text-decoration:none;text-shadow:0px 1px 1px rgba(255,255,255,0.9);color:#283A5D;outline:none}.sm-dox a:hover{background-image:url("tab_a.png");background-repeat:repeat-x;color:#fff;text-shadow:0px 1px 1px #000}.sm-dox a.current{color:#D23600}.sm-dox a.disabled{color:#bbb}.sm-dox a span.sub-arrow{position:absolute;top:50%;margin-top:-14px;left:auto;right:3px;width:28px;height:28px;overflow:hidden;font:bold 12px/28px monospace !important;text-align:center;text-shadow:none;background:rgba(255,255,255,0.5);border-radius:5px}.sm-dox a.highlighted span.sub-arrow:before{display:block;content:'-'}.sm-dox>li:first-child>a,.sm-dox>li:first-child>:not(ul) a{border-radius:5px 5px 0 0}.sm-dox>li:last-child>a,.sm-dox>li:last-child>*:not(ul) a,.sm-dox>li:last-child>ul,.sm-dox>li:last-child>ul>li:last-child>a,.sm-dox>li:last-child>ul>li:last-child>*:not(ul) a,.sm-dox>li:last-child>ul>li:last-child>ul,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul{border-radius:0 0 5px 5px}.sm-dox>li:last-child>a.highlighted,.sm-dox>li:last-child>*:not(ul) a.highlighted,.sm-dox>li:last-child>ul>li:last-child>a.highlighted,.sm-dox>li:last-child>ul>li:last-child>*:not(ul) a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>a.highlighted,.sm-dox>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>ul>li:last-child>*:not(ul) a.highlighted{border-radius:0}.sm-dox ul{background:rgba(162,162,162,0.1)}.sm-dox ul a,.sm-dox ul a:focus,.sm-dox ul a:hover,.sm-dox ul a:active{font-size:12px;border-left:8px solid transparent;line-height:36px;text-shadow:none;background-color:white;background-image:none}.sm-dox ul a:hover{background-image:url("tab_a.png");background-repeat:repeat-x;color:#fff;text-shadow:0px 1px 1px #000}.sm-dox ul ul a,.sm-dox ul ul a:hover,.sm-dox ul ul a:focus,.sm-dox ul ul a:active{border-left:16px solid transparent}.sm-dox ul ul ul a,.sm-dox ul ul ul a:hover,.sm-dox ul ul ul a:focus,.sm-dox ul ul ul a:active{border-left:24px solid transparent}.sm-dox ul ul ul ul a,.sm-dox ul ul ul ul a:hover,.sm-dox ul ul ul ul a:focus,.sm-dox ul ul ul ul a:active{border-left:32px solid transparent}.sm-dox ul ul ul ul ul a,.sm-dox ul ul ul ul ul a:hover,.sm-dox ul ul ul ul ul a:focus,.sm-dox ul ul ul ul ul a:active{border-left:40px solid transparent}@media (min-width: 768px){.sm-dox ul{position:absolute;width:12em}.sm-dox li{float:left}.sm-dox.sm-rtl li{float:right}.sm-dox ul li,.sm-dox.sm-rtl ul li,.sm-dox.sm-vertical li{float:none}.sm-dox a{white-space:nowrap}.sm-dox ul a,.sm-dox.sm-vertical a{white-space:normal}.sm-dox .sm-nowrap>li>a,.sm-dox .sm-nowrap>li>:not(ul) a{white-space:nowrap}.sm-dox{padding:0 10px;background-image:url("tab_b.png");line-height:36px}.sm-dox a span.sub-arrow{top:50%;margin-top:-2px;right:12px;width:0;height:0;border-width:4px;border-style:solid dashed dashed dashed;border-color:#283A5D transparent transparent transparent;background:transparent;border-radius:0}.sm-dox a,.sm-dox a:focus,.sm-dox a:active,.sm-dox a:hover,.sm-dox a.highlighted{padding:0px 12px;background-image:url("tab_s.png");background-repeat:no-repeat;background-position:right;border-radius:0 !important}.sm-dox a:hover{background-image:url("tab_a.png");background-repeat:repeat-x;color:#fff;text-shadow:0px 1px 1px #000}.sm-dox a:hover span.sub-arrow{border-color:#fff transparent transparent transparent}.sm-dox a.has-submenu{padding-right:24px}.sm-dox li{border-top:0}.sm-dox>li>ul:before,.sm-dox>li>ul:after{content:'';position:absolute;top:-18px;left:30px;width:0;height:0;overflow:hidden;border-width:9px;border-style:dashed dashed solid dashed;border-color:transparent transparent #bbb transparent}.sm-dox>li>ul:after{top:-16px;left:31px;border-width:8px;border-color:transparent transparent #fff transparent}.sm-dox ul{border:1px solid #bbb;padding:5px 0;background:#fff;border-radius:5px !important;box-shadow:0 5px 9px rgba(0,0,0,0.2)}.sm-dox ul a span.sub-arrow{right:8px;top:50%;margin-top:-5px;border-width:5px;border-color:transparent transparent transparent #555;border-style:dashed dashed dashed solid}.sm-dox ul a,.sm-dox ul a:hover,.sm-dox ul a:focus,.sm-dox ul a:active,.sm-dox ul a.highlighted{color:#555;background-image:none;border:0 !important;color:#555;background-image:none}.sm-dox ul a:hover{background-image:url("tab_a.png");background-repeat:repeat-x;color:#fff;text-shadow:0px 1px 1px #000}.sm-dox ul a:hover span.sub-arrow{border-color:transparent transparent transparent #fff}.sm-dox span.scroll-up,.sm-dox span.scroll-down{position:absolute;display:none;visibility:hidden;overflow:hidden;background:#fff;height:36px}.sm-dox span.scroll-up:hover,.sm-dox span.scroll-down:hover{background:#eee}.sm-dox span.scroll-up:hover span.scroll-up-arrow,.sm-dox span.scroll-up:hover span.scroll-down-arrow{border-color:transparent transparent #D23600 transparent}.sm-dox span.scroll-down:hover span.scroll-down-arrow{border-color:#D23600 transparent transparent transparent}.sm-dox span.scroll-up-arrow,.sm-dox span.scroll-down-arrow{position:absolute;top:0;left:50%;margin-left:-6px;width:0;height:0;overflow:hidden;border-width:6px;border-style:dashed dashed solid dashed;border-color:transparent transparent #555 transparent}.sm-dox span.scroll-down-arrow{top:8px;border-style:solid dashed dashed dashed;border-color:#555 transparent transparent transparent}.sm-dox.sm-rtl a.has-submenu{padding-right:12px;padding-left:24px}.sm-dox.sm-rtl a span.sub-arrow{right:auto;left:12px}.sm-dox.sm-rtl.sm-vertical a.has-submenu{padding:10px 20px}.sm-dox.sm-rtl.sm-vertical a span.sub-arrow{right:auto;left:8px;border-style:dashed solid dashed dashed;border-color:transparent #555 transparent transparent}.sm-dox.sm-rtl>li>ul:before{left:auto;right:30px}.sm-dox.sm-rtl>li>ul:after{left:auto;right:31px}.sm-dox.sm-rtl ul a.has-submenu{padding:10px 20px !important}.sm-dox.sm-rtl ul a span.sub-arrow{right:auto;left:8px;border-style:dashed solid dashed dashed;border-color:transparent #555 transparent transparent}.sm-dox.sm-vertical{padding:10px 0;border-radius:5px}.sm-dox.sm-vertical a{padding:10px 20px}.sm-dox.sm-vertical a:hover,.sm-dox.sm-vertical a:focus,.sm-dox.sm-vertical a:active,.sm-dox.sm-vertical a.highlighted{background:#fff}.sm-dox.sm-vertical a.disabled{background-image:url("tab_b.png")}.sm-dox.sm-vertical a span.sub-arrow{right:8px;top:50%;margin-top:-5px;border-width:5px;border-style:dashed dashed dashed solid;border-color:transparent transparent transparent #555}.sm-dox.sm-vertical>li>ul:before,.sm-dox.sm-vertical>li>ul:after{display:none}.sm-dox.sm-vertical ul a{padding:10px 20px}.sm-dox.sm-vertical ul a:hover,.sm-dox.sm-vertical ul a:focus,.sm-dox.sm-vertical ul a:active,.sm-dox.sm-vertical ul a.highlighted{background:#eee}.sm-dox.sm-vertical ul a.disabled{background:#fff}} diff --git a/docs/img/demo_0.gif b/docs/img/demo_0.gif deleted file mode 100644 index a8553c4c..00000000 Binary files a/docs/img/demo_0.gif and /dev/null differ diff --git a/docs/index.html b/docs/index.html deleted file mode 100644 index cfd32901..00000000 --- a/docs/index.html +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/docs/oalabs.pdf b/docs/oalabs.pdf deleted file mode 100644 index efef457a..00000000 Binary files a/docs/oalabs.pdf and /dev/null differ diff --git a/docs/theme b/docs/theme deleted file mode 160000 index b6a33731..00000000 --- a/docs/theme +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b6a337311ec3bbc5603fa2ce541850a603844e58 diff --git a/include/args.h b/include/args.h deleted file mode 100644 index 3c10972f..00000000 --- a/include/args.h +++ /dev/null @@ -1,108 +0,0 @@ -#ifndef ARGS_H -#define ARGS_H - -#include -#include -#include -#include -#include -#include -#include -#include - -#ifndef _WIN32 -#include -#else -#include -#endif - -#ifdef _WIN32 -#define BINLEX_EXPORT __declspec(dllexport) -#else -#define BINLEX_EXPORT -#endif - -#define ARGS_MODE_COUNT 9 - -#define ARGS_IO_TYPE_UNKNOWN 0 -#define ARGS_IO_TYPE_FILE 1 -#define ARGS_IO_TYPE_DIR 2 - -#ifdef _WIN32 -typedef unsigned int uint; -typedef uint useconds_t; -#endif - -/** -* @namespace binlex -* @brief the binlex namespace -*/ -namespace binlex{ - class Args { - public: - char version[7] = "v1.1.1"; - const char *modes[ARGS_MODE_COUNT] = {"elf:x86", "elf:x86_64", "pe:x86", "pe:x86_64", "raw:x86", "raw:x86_64", "raw:cil", "pe:cil", "auto"}; - struct{ - char *input; - int io_type; - char *output; - uint timeout; - uint threads; - bool help; - bool list_modes; - std::string mode; - std::string corpus; - bool pretty; - bool debug; - std::set tags; //!< Set for storing the tags. - } options; - BINLEX_EXPORT Args(); - /** - * Set the default CLI parameters. - * @return void - */ - BINLEX_EXPORT void SetDefault(); - /** - * Check the CLI mode provided by the user. - * @param mode the mode provided - * @return bool - */ - BINLEX_EXPORT bool check_mode(const char *mode); - /** - * Check if path to file is valid. - * @param path file path - * @return int result - */ - BINLEX_EXPORT int is_file(const char *path); - /** - * Check if path is a directory. - * @param path path to a directory - * @return int result - */ - BINLEX_EXPORT int is_dir(const char *path); - /** - * Set input type. - * @param input file path or directory - * @return void - */ - BINLEX_EXPORT void set_io_type(char *input); - /** - * Print CLI help menu. - * @return void - */ - BINLEX_EXPORT void print_help(); - /** - * Parse CLI arguments. - * @return void - */ - BINLEX_EXPORT void parse(int argc, char **argv); - /** - * Get tags from CLI as string. - * @return std::string - */ - BINLEX_EXPORT std::string get_tags_as_str(); - BINLEX_EXPORT ~Args(); - }; -} - -#endif diff --git a/include/auto.h b/include/auto.h deleted file mode 100644 index 2236bdfd..00000000 --- a/include/auto.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef AUTO_H -#define AUTO_H - -#ifdef _WIN32 -#include -#endif - -#include "pe.h" -#include "pe-dotnet.h" -#include "blelf.h" -#include "disassembler.h" -#include "cil.h" -#include "common.h" -#include -#include - -#ifdef _WIN32 -#define BINLEX_EXPORT __declspec(dllexport) -#else -#define BINLEX_EXPORT -#endif - -using namespace std; - -namespace binlex{ - class AutoLex{ - /** - This class is used to process files with auto mode. - */ - private: - struct { - LIEF::EXE_FORMATS format; - cs_mode mode; - cs_arch arch; - int machineType; - } characteristics; - /** - * Get initial characteristics of a file. - * @param file_path path to the file to read - * @return bool - */ - bool GetFileCharacteristics(char *file_path); - public: - BINLEX_EXPORT AutoLex(); - /** - * This method processes a files for auto mode. - * @param file_path path to the file to read - * @return int result - */ - BINLEX_EXPORT int ProcessFile(char *file_path); - }; -}; - -#endif diff --git a/include/blelf.h b/include/blelf.h deleted file mode 100644 index 427b61a8..00000000 --- a/include/blelf.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef ELF_H -#define ELF_H - -#ifdef _WIN32 -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include "file.h" - -#ifdef _WIN32 -#define BINLEX_EXPORT __declspec(dllexport) -#else -#define BINLEX_EXPORT -#endif - -using namespace std; -using namespace LIEF::ELF; - -namespace binlex{ - class ELF : public File{ - /** - This class is used to read ELF files. - */ - private: - /** - This method parses the ELF file sections. - @return bool - */ - bool ParseSections(); - public: - ARCH mode = ARCH::EM_NONE; - unique_ptr binary; - //struct Section sections[BINARY_MAX_SECTIONS]; - //uint32_t total_exec_sections; - BINLEX_EXPORT ELF(); - /** - This method reads an ELF file from a buffer. - @param data vector - @return bool - */ - virtual bool ReadVector(const std::vector &data); - BINLEX_EXPORT ~ELF(); - }; -}; - -#endif diff --git a/include/cil.h b/include/cil.h deleted file mode 100644 index d57061f0..00000000 --- a/include/cil.h +++ /dev/null @@ -1,397 +0,0 @@ -#ifndef CIL_H -#define CIL_H - -#include -#include -#include -#include -#include -#include -#include -#include -#if !defined(_WIN32) && !defined(__APPLE__) -#include -#endif // _WIN32 -#include -#include -#include -#include -#include -#include -#include "disassemblerbase.h" -#include "json.h" - -// Bytecode Reference: https://en.wikipedia.org/wiki/List_of_CIL_instructions -#define INVALID_OP 0xFFFFFFFF - -// CIL Decompiler Types -#define CIL_DISASSEMBLER_TYPE_FUNCS 0 -#define CIL_DISASSEMBLER_TYPE_BLCKS 1 -#define CIL_DISASSEMBLER_TYPE_UNSET 2 -#define CIL_DISASSEMBLER_TYPE_ALL 3 - -#define CIL_DISASSEMBLER_MAX_SECTIONS 256 -#define CIL_DISASSEMBLER_MAX_INSN 16384 - -// CIL Instructions -#define CIL_INS_ADD 0x58 -#define CIL_INS_ADD_OVF 0xD6 -#define CIL_INS_ADD_OVF_UN 0xD7 -#define CIL_INS_AND 0x5F -#define CIL_INS_BEQ 0x3B -#define CIL_INS_BEQ_S 0x2E -#define CIL_INS_BGE 0x3C -#define CIL_INS_BGE_S 0x2F -#define CIL_INS_BGE_UN 0x41 -#define CIL_INS_BGE_UN_S 0x34 -#define CIL_INS_BGT 0x3D -#define CIL_INS_BGT_S 0x30 -#define CIL_INS_BGT_UN 0x42 -#define CIL_INS_BGT_UN_S 0x35 -#define CIL_INS_BLE 0x3E -#define CIL_INS_BLE_S 0x31 -#define CIL_INS_BLE_UN 0x43 -#define CIL_INS_BLE_UN_S 0x36 -#define CIL_INS_BLT 0x3F -#define CIL_INS_BLT_S 0x32 -#define CIL_INS_BLT_UN 0x44 -#define CIL_INS_BLT_UN_S 0x37 -#define CIL_INS_BNE_UN 0x40 -#define CIL_INS_BNE_UN_S 0x33 -#define CIL_INS_BOX 0x8C -#define CIL_INS_BR 0x38 -#define CIL_INS_BR_S 0x2B -#define CIL_INS_BREAK 0x01 -#define CIL_INS_BRFALSE 0x39 -#define CIL_INS_BRFALSE_S 0x2C -#define CIL_INS_BRINST 0x3A -#define CIL_INS_BRINST_S 0x2D -#define CIL_INS_BRNULL 0x39 -#define CIL_INS_BRNULL_S 0x2C -#define CIL_INS_BRTRUE 0x3A -#define CIL_INS_BRTRUE_S 0x2D -#define CIL_INS_BRZERO 0x39 -#define CIL_INS_BRZERO_S 0x2C -#define CIL_INS_CALL 0x28 -#define CIL_INS_CALLI 0x29 -#define CIL_INS_CALLVIRT 0x6F -#define CIL_INS_CASTCLASS 0x74 -#define CIL_INS_CKINITE 0xC3 -#define CIL_INS_CONV_I 0xD3 -#define CIL_INS_CONV_I1 0x67 -#define CIL_INS_CONV_I2 0x68 -#define CIL_INS_CONV_I4 0x69 -#define CIL_INS_CONV_I8 0x6A -#define CIL_INS_CONV_OVF_i 0xD4 -#define CIL_INS_CONV_OVF_I_UN 0x8A -#define CIL_INS_CONV_OVF_I1 0xB3 -#define CIL_INS_CONV_OVF_I1_UN 0x82 -#define CIL_INS_CONV_OVF_I2 0xB5 -#define CIL_INS_CONV_OVF_I2_UN 0x83 -#define CIL_INS_CONV_OVF_I4 0xB7 -#define CIL_INS_CONV_OVF_I4_UN 0x84 -#define CIL_INS_CONV_OVF_I8 0xB9 -#define CIL_INS_CONV_OVF_I8_UN 0x85 -#define CIL_INS_CONV_OVF_U 0xD5 -#define CIL_INS_CONV_OVF_U_UN 0x8B -#define CIL_INS_CONV_OVF_U1 0xB4 -#define CIL_INS_CONV_OVF_U1_UN 0x86 -#define CIL_INS_CONV_OVF_U2 0xB6 -#define CIL_INS_CONV_OVF_U2_UN 0x87 -#define CIL_INS_CONV_OVF_U4 0xB8 -#define CIL_INS_CONV_OVF_U4_UN 0x88 -#define CIL_INS_CONV_OVF_U8 0xBA -#define CIL_INS_CONV_OVF_U8_UN 0x89 -#define CIL_INS_CONV_R_UN 0x76 -#define CIL_INS_CONV_R4 0x6B -#define CIL_INS_CONV_R8 0x6C -#define CIL_INS_CONV_U 0xE0 -#define CIL_INS_CONV_U1 0xD2 -#define CIL_INS_CONV_U2 0xD1 -#define CIL_INS_CONV_U4 0x6D -#define CIL_INS_CONV_U8 0x6E -#define CIL_INS_CPOBJ 0x70 -#define CIL_INS_DIV 0x5B -#define CIL_INS_DIV_UN 0x5C -#define CIL_INS_DUP 0x25 -#define CIL_INS_ENDFAULT 0xDC -#define CIL_INS_ENDFINALLY 0xDC -#define CIL_INS_ISINST 0x75 -#define CIL_INS_JMP 0x27 -#define CIL_INS_LDARG_0 0x02 -#define CIL_INS_LDARG_1 0x03 -#define CIL_INS_LDARG_2 0x04 -#define CIL_INS_LDARG_3 0x05 -#define CIL_INS_LDARG_S 0x0E -#define CIL_INS_LDARGA_S 0x0F -#define CIL_INS_LDC_I4 0x20 -#define CIL_INS_LDC_I4_0 0x16 -#define CIL_INS_LDC_I4_1 0x17 -#define CIL_INS_LDC_I4_2 0x18 -#define CIL_INS_LDC_I4_3 0x19 -#define CIL_INS_LDC_I4_4 0x1A -#define CIL_INS_LDC_I4_5 0x1B -#define CIL_INS_LDC_I4_6 0x1C -#define CIL_INS_LDC_I4_7 0x1D -#define CIL_INS_LDC_I4_8 0x1E -#define CIL_INS_LDC_I4_M1 0x15 -#define CIL_INS_LDC_I4_S 0x1F -#define CIL_INS_LDC_I8 0x21 -#define CIL_INS_LDC_R4 0x22 -#define CIL_INS_LDC_R8 0x23 -#define CIL_INS_LDELM 0xA3 -#define CIL_INS_LDELM_I 0x97 -#define CIL_INS_LDELM_I1 0x90 -#define CIL_INS_LDELM_I2 0x92 -#define CIL_INS_LDELM_I4 0x94 -#define CIL_INS_LDELM_I8 0x96 -#define CIL_INS_LDELM_R4 0x98 -#define CIL_INS_LDELM_R8 0x99 -#define CIL_INS_LDELM_REF 0x9A -#define CIL_INS_LDELM_U1 0x91 -#define CIL_INS_LDELM_U2 0x93 -#define CIL_INS_LDELM_U4 0x95 -#define CIL_INS_LDELM_U8 0x96 -#define CIL_INS_LDELMA 0x8F -#define CIL_INS_LDFLD 0x7B -#define CIL_INS_LDFLDA 0x7C -#define CIL_INS_LDIND_I 0x4D -#define CIL_INS_LDIND_I1 0x46 -#define CIL_INS_LDIND_I2 0x48 -#define CIL_INS_LDIND_I4 0x4A -#define CIL_INS_LDIND_I8 0x4C -#define CIL_INS_LDIND_R4 0x4E -#define CIL_INS_LDIND_R8 0x4F -#define CIL_INS_LDIND_REF 0x50 -#define CIL_INS_LDIND_U1 0x47 -#define CIL_INS_LDIND_U2 0x49 -#define CIL_INS_LDIND_U4 0x4B -#define CIL_INS_LDIND_U8 0x4C -#define CIL_INS_LDLEN 0x8E -#define CIL_INS_LDLOC_0 0x06 -#define CIL_INS_LDLOC_1 0x07 -#define CIL_INS_LDLOC_2 0x08 -#define CIL_INS_LDLOC_3 0x09 -#define CIL_INS_LDLOC_S 0x11 -#define CIL_INS_LDLOCA_S 0x12 -#define CIL_INS_LDNULL 0x14 -#define CIL_INS_LDOBJ 0x71 -#define CIL_INS_LDSFLD 0x7E -#define CIL_INS_LDSFLDA 0x7F -#define CIL_INS_LDSTR 0x72 -#define CIL_INS_LDTOKEN 0xD0 -#define CIL_INS_LEAVE 0xDD -#define CIL_INS_LEAVE_S 0xDE -#define CIL_INS_MKREFANY 0xC6 -#define CIL_INS_MUL 0x5A -#define CIL_INS_MUL_OVF 0xD8 -#define CIL_INS_MUL_OVF_UN 0xD9 -#define CIL_INS_NEG 0x65 -#define CIL_INS_NEWARR 0x8D -#define CIL_INS_NEWOBJ 0x73 -#define CIL_INS_NOP 0x00 -#define CIL_INS_NOT 0x66 -#define CIL_INS_OR 0x60 -#define CIL_INS_POP 0x26 -#define CIL_INS_REFANYVAL 0xC2 -#define CIL_INS_REM 0x5D -#define CIL_INS_REM_UN 0x5E -#define CIL_INS_RET 0x2A -#define CIL_INS_SHL 0x62 -#define CIL_INS_SHR 0x63 -#define CIL_INS_SHR_UN 0x64 -#define CIL_INS_STARG_S 0x10 -#define CIL_INS_STELEM 0xA4 -#define CIL_INS_STELEM_I 0x9B -#define CIL_INS_STELEM_I1 0x9C -#define CIL_INS_STELEM_I2 0x9D -#define CIL_INS_STELEM_I4 0x9E -#define CIL_INS_STELEM_I8 0x9F -#define CIL_INS_STELEM_R4 0xA0 -#define CIL_INS_STELEM_R8 0xA1 -#define CIL_INS_STELEM_REF 0xA2 -#define CIL_INS_STFLD 0x7D -#define CIL_INS_STIND_I 0xDF -#define CIL_INS_STIND_I1 0x52 -#define CIL_INS_STIND_I2 0x53 -#define CIL_INS_STIND_I4 0x54 -#define CIL_INS_STIND_I8 0x55 -#define CIL_INS_STIND_R4 0x56 -#define CIL_INS_STIND_R8 0x57 -#define CIL_INS_STIND_REF 0x51 -#define CIL_INS_STLOC_0 0x0A -#define CIL_INS_STLOC_1 0x0B -#define CIL_INS_STLOC_2 0x0C -#define CIL_INS_STLOC_3 0x0D -#define CIL_INS_STOBJ 0x81 -#define CIL_INS_STSFLD 0x80 -#define CIL_INS_SUB 0x59 -#define CIL_INS_SUB_OVF 0xDA -#define CIL_INS_SUB_OVF_UN 0xDB -#define CIL_INS_SWITCH 0x45 -#define CIL_INS_THROW 0x7A -#define CIL_INS_UNBOX 0x79 -#define CIL_INS_UNBOX_ANY 0xA5 -#define CIL_INS_XOR 0x61 -#define CIL_INS_STLOC_S 0x13 - -// CIL Prefix Instructions -#define CIL_INS_PREFIX 0xFE -#define CIL_INS_ARGLIST 0x00 -#define CIL_INS_CEQ 0x01 -#define CIL_INS_CGT 0x02 -#define CIL_INS_CGT_UN 0x03 -#define CIL_INS_CLT 0x04 -#define CIL_INS_CLT_UN 0x05 -#define CIL_INS_CONSTRAINED 0x16 -#define CIL_INS_CPBLK 0x17 -#define CIL_INS_ENDFILTER 0x11 -#define CIL_INS_INITBLK 0x18 -#define CIL_INS_INITOBJ 0x15 -#define CIL_INS_LDARG 0x09 -#define CIL_INS_LDARGA 0x0A -#define CIL_INS_LDFTN 0x06 -#define CIL_INS_LDLOC 0x0C -#define CIL_INS_LDLOCA 0x0D -#define CIL_INS_LDVIRTFTN 0x07 -#define CIL_INS_LOCALLOC 0x0F -#define CIL_INS_NO 0x19 -#define CIL_INS_READONLY 0x1E -#define CIL_INS_REFANYTYPE 0x1D -#define CIL_INS_RETHROW 0x1A -#define CIL_INS_SIZEOF 0x1C -#define CIL_INS_STARG 0x0B -#define CIL_INS_STLOC 0x0E -#define CIL_INS_TAIL 0x14 -#define CIL_INS_UNALIGNED 0x12 -#define CIL_INS_VOLATILE 0x13 - -using namespace std; - -namespace binlex { - class CILDisassembler : public DisassemblerBase { - /* - This class is used to decompile CIL/.NET bytecode. - */ - private: - int type = CIL_DISASSEMBLER_TYPE_UNSET; - int update_offset(int operand_size, int i); - typedef struct worker { - csh handle; - cs_err error; - uint64_t pc; - const uint8_t *code; - size_t code_size; - } worker; - typedef struct{ - uint index; - void *sections; - } worker_args; - public: - CILDisassembler(const binlex::File &firef); - struct Instruction { - unsigned char instruction; - uint operand_size; - uint offset; - }; - struct Trait { - string corpus; - string type; - string architecture; - string tmp_bytes; - string bytes; - string trait; - uint edges; - uint blocks; - vector< Instruction* >* instructions; - uint num_instructions; - uint size; - uint offset; - uint invalid_instructions; - uint cyclomatic_complexity; - uint average_instructions_per_block; - float bytes_entropy; - float trait_entropy; - string trait_sha256; - string bytes_sha256; - }; - struct Section { - vector function_traits; - vector block_traits; - char *trait; - cs_arch arch; - cs_mode mode; - char *arch_str; - char *cpu; - string corpus; - uint threads; - bool instructions; - uint thread_cycles; - useconds_t thread_sleep; - uint offset; - uint ntraits; - struct Trait **traits; - void *data; - size_t data_size; - set coverage; - map addresses; - map visited; - queue discovered; - }; - struct Section sections[CIL_DISASSEMBLER_MAX_SECTIONS]; - //Map containing prefix instructions and their operand sizes - map prefixInstrMap; - //Map containing conditional instructions and their operand sizes - map condInstrMap; - //Map for all remaining instruction types that don't need special - //treatment - map miscInstrMap; - /** - * Disassemble traits - * @param data pointer to data to disassemble - * @param data_size size of the data to disassemble - * @param index the section index - * @return bool - */ - bool Disassemble(void *data, int data_size, int index); - /** - * Get Traits as json vector - * @return list of traits JSON objects - */ - vector GetTraits(); - /** - * Get Trait as json - * @return return trait as json object - */ - json GetTrait(struct Trait *trait); - /** - * Converts instruction objects to trait pattern for output - * @param insn Source instruction to check and resulting operand size - */ - string ConvTraitBytes(vector< Instruction* > instructions); - /** - * Converts instruction objects to raw bytes for output using offsets - * and section data. - * @param insn Source instruction to check and resulting operand size - */ - string ConvBytes(vector< Instruction* > allinst, void *data, int data_size); - /** - * Checks if CIL instruction is conditional for stats - * @param insn Source instruction to check and resulting operand size - */ - bool IsConditionalInsn(Instruction *insn); - /** - * Checks if CIL instruction a prefix instruction - * @param insn Source instruction to check and resulting operand size - */ - bool IsPrefixInstr(Instruction *insn); - /** - * Gets size of trait using the beginning and ending offsets - * @param allinst Source instructions - */ - uint SizeOfTrait(vector< Instruction* > allinst); - ~CILDisassembler(); - }; -}; - -#endif diff --git a/include/common.h b/include/common.h deleted file mode 100644 index 2bdfb295..00000000 --- a/include/common.h +++ /dev/null @@ -1,228 +0,0 @@ -#ifndef COMMON_H -#define COMMON_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "args.h" - -extern "C" { - #include "sha256.h" -} - -#ifdef _WIN32 -#define BINLEX_EXPORT __declspec(dllexport) -#else -#define BINLEX_EXPORT -#endif - -#ifdef _WIN32 -#pragma comment(lib, "capstone") -#pragma comment(lib, "LIEF") -#endif - -using std::set; -using std::map; -using std::queue; -using std::set; -using std::vector; -using std::ofstream; -using std::stringstream; -using std::string; -using std::cin; -using std::cout; -using std::cerr; -using std::endl; -using std::hex; -using std::setfill; -using std::setw; - -#define BINARY_MAX_SECTIONS 256 - -typedef enum BINARY_ARCH { - BINARY_ARCH_UNKNOWN = 0, - BINARY_ARCH_X86 = 1, - BINARY_ARCH_X86_64 = 2, -} BINARY_ARCH; - -typedef enum BINARY_MODE { - BINARY_MODE_UNKNOWN = 0, - BINARY_MODE_32 = 1, - BINARY_MODE_64 = 2, - BINARY_MODE_CIL = 3, -} BINARY_MODE; - -typedef enum BINARY_TYPE { - BINARY_TYPE_PE = 0, - BINARY_TYPE_ELF = 1, - BINARY_TYPE_MACHO = 2, - BINARY_TYPE_RAW = 3, - BINARY_TYPE_UNKNOWN = 4 -} BINARY_TYPE; - -#ifdef _WIN32 -typedef unsigned int uint; -typedef uint useconds_t; -#endif - -extern binlex::Args g_args; - -// Debug -#define PRINT_DEBUG(...) {if (g_args.options.debug) fprintf(stderr, __VA_ARGS__); } -#define PRINT_ERROR_AND_EXIT(...) { fprintf(stderr, __VA_ARGS__); exit(EXIT_FAILURE); } -void print_data(string title, void *data, uint32_t size); -#define PRINT_DATA(title, data, size) { print_data(title, data, size); } - -#define PERF_START(tag) { binlex::TimedCode *tc = new binlex::TimedCode(tag); -#define PERF_END tc->Print(); } - -namespace binlex { - class Common{ - /** - This class contains methods common to binlex. - */ - public: - /** - * This method takes a (binary) input string and returns its tlsh hash. - * @param data input string - * @param len length of data - * @return Returns the tlsh hash of the trait string - */ - BINLEX_EXPORT static string GetTLSH(const uint8_t *data, size_t len); - /** - This method reads a file returns its tlsh hash. - * @param file_path path to the file to read - * @return Returns the tlsh hash of the file - * @throw std::runtime_error if file operation fails - */ - BINLEX_EXPORT static string GetFileTLSH(const char *file_path); - /** - * This method takes an input string and returns its sha256 hash. - * @param trait input string. - * @return Returns the sha256 hash of the trait string - */ - BINLEX_EXPORT static string SHA256(char *trait); - /** - * This method reads a file returns its sha256 hash. - * @param file_path path to the file to read - * @return Returns the sha256 hash of the file - * @throw std::runtime_error if file operation fails - */ - BINLEX_EXPORT static string GetFileSHA256(char *file_path); - /** - * This method reads a file returns its sha256 hash. - * @param data input string - * @param len length of data - * @return Returns the sha256 hash of the file - * @throw std::runtime_error if file operation fails - */ - BINLEX_EXPORT static string GetSHA256(const uint8_t *data, size_t len); - /** - * This method takes an input trait string and returns a char vector of bytes (ignores wildcards). - * @param trait input string. - * @return Returns char vector of bytes - */ - BINLEX_EXPORT static vector TraitToChar(string trait); - /** - * This method removes wildcards from a trait string. - * @param trait input trait string. - * @return Returns trait without wildcards - */ - BINLEX_EXPORT static string RemoveWildcards(string trait); - /** - * This method gets the size in bytes of a trait string (includes wildcards). - * @param trait input trait string. - * @return Returns uint size of bytes - */ - BINLEX_EXPORT static uint GetByteSize(string s); - /** - * This method removes spaces from a string. - * @param s input string - * @return Returns string without spaces - */ - BINLEX_EXPORT static string RemoveSpaces(string s); - /** - * This method wildcards byte strings for traits. - * @param trait input trait string - * @param bytes byte string to wildcard - * @return Returns wildcarded trait string - */ - BINLEX_EXPORT static string WildcardTrait(string trait, string bytes); - /** - * This method removes whitespace on the right. - * @param s input string - * @return Returns string with whitespace on right trimmed - */ - BINLEX_EXPORT static string TrimRight(const std::string &s); - /** - * This method creates a byte string based on a pointer and its size. - * @param data A pointer to the data - * @param size The size of the data to collect - * @return Returns a byte string of the selected data - */ - BINLEX_EXPORT static string HexdumpBE(const void *data, size_t size); - /** - * This method converts bytes hex string to TLSH of the bytes. - * @param bytes hex string of bytes - * @return TLSH hash string - */ - BINLEX_EXPORT static string TraitToTLSH(string bytes); - /** - * This method converts a trait to a uint_8 vector - * @param trait string - * @return uint8_t vector of data - */ - BINLEX_EXPORT static vector TraitToData(string trait); - /** - * Generate Number of Wildcards - * @param count number of wildcard bytes to create - * @return string - */ - BINLEX_EXPORT static string Wildcards(uint count); - /** - * Calculate the entropy of a trait. - * @param trait trait hex string - * @return entropy float - */ - BINLEX_EXPORT static float Entropy(string trait); - /** - * This method prints hexdump to stdout. - * @param desc A description of the data. - * @param data A pointer to the data - * @param size The size of the data to collect - */ - BINLEX_EXPORT static void Hexdump(const char * desc, const void * addr, const int len); - }; - - - class TimedCode { - /** - This class contains functionality for binlex code instrumentation. - */ - private: - std::chrono::steady_clock::time_point start; - const char *print_tag; - public: - TimedCode(const char *tag); - void Print(); - ~TimedCode() {}; - }; -} - -#endif diff --git a/include/disassembler.h b/include/disassembler.h deleted file mode 100644 index 3d3f2bc6..00000000 --- a/include/disassembler.h +++ /dev/null @@ -1,289 +0,0 @@ -#ifndef DISASSEMBLER_H -#define DISASSEMBLER_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "common.h" -#include "json.h" -#include "file.h" -#include "disassemblerbase.h" - -#if defined(__linux__) || defined(__APPLE__) -#include -#include -#elif _WIN32 -#include -#include -#endif - -#ifdef _WIN32 -#define BINLEX_EXPORT __declspec(dllexport) -#else -#define BINLEX_EXPORT -#endif - -#define SHA256_PRINTABLE_SIZE 65 /* including NULL terminator */ - -typedef enum DISASSEMBLER_VISITED { - DISASSEMBLER_VISITED_QUEUED = 0, - DISASSEMBLER_VISITED_ANALYZED = 1 -} DISASSEMBLER_VISITED; - -typedef enum DISASSEMBLER_OPERAND_TYPE { - DISASSEMBLER_OPERAND_TYPE_BLOCK = 0, - DISASSEMBLER_OPERAND_TYPE_FUNCTION = 1, - DISASSEMBLER_OPERAND_TYPE_UNSET = 2 -} DISASSEMBLER_OPERAND_TYPE; - -using json = nlohmann::json; - -namespace binlex { - class Disassembler : public DisassemblerBase{ - private: - typedef struct disasm_t { - csh handle; - cs_err error; - uint64_t pc; - const uint8_t *code; - size_t code_size; - } disasm_t; - public: - struct Trait { - string type; - string bytes; - string trait; - uint edges; - uint blocks; - uint instructions; - uint size; - uint offset; - uint invalid_instructions; - uint cyclomatic_complexity; - uint average_instructions_per_block; - float bytes_entropy; - float trait_entropy; - string bytes_sha256; - string trait_sha256; - set xrefs; - }; - struct Section { - char *cpu; - bool instructions; - uint offset; - vector traits; - void *data; - size_t data_size; - set blocks; - set functions; - map visited; - queue discovered; - }; - static cs_arch arch; - static cs_mode mode; - struct Section sections[BINARY_MAX_SECTIONS]; - BINLEX_EXPORT Disassembler(const binlex::File &firef); - /** - * This method processes the queue and collects traits for a section. - * @param args pointer to worker arguments - * @returns NULL - */ - BINLEX_EXPORT void* CreateTraitsForSection(uint index); - /** - * This method adds a discovered block address to queue. - * @param address the block address - * @param operand_type the operand type - * @param index the section index - * @return bool - */ - BINLEX_EXPORT static void AddDiscoveredBlock(uint64_t address, struct Section *sections, uint index); - /** - * This method adds a discovered function address to queue. - * @param address the block address - * @param operand_type the operand type - * @param index the section index - * @return bool - */ - BINLEX_EXPORT static void AddDiscoveredFunction(uint64_t address, struct Section *sections, uint index); - /** - * This method collects addresses. - * @param insn the instruction - * @param operand_type the operand type - * @param index the section index - * @return bool - */ - BINLEX_EXPORT static void CollectOperands(cs_insn* insn, int operand_type, struct Section *sections, uint index); - /** - * This method collects addresses from instructions and adds them to the queue. - * @param insn the instruction - * @param index the section index - * @return operand type - */ - BINLEX_EXPORT DISASSEMBLER_OPERAND_TYPE CollectInsn(cs_insn* insn, struct Section *sections, uint index); - /** - * This method performs a linear disassembly of the binary to identify initial blocks and functions for the queue. - * @param data pointer to data - * @param data_size size of data - * @param offset include section offset - * @param index the section index - */ - BINLEX_EXPORT void LinearDisassemble(void* data, size_t data_size, size_t offset, uint index); - /** - * This method disassembles the binary and collects traits. - * @param data pointer to data - * @param data_size size of data - * @param offset include section offset - * @param index the section index - */ - BINLEX_EXPORT void Disassemble(); - /** - * This method appends traits to the section. - * @param trait trait to append - * @param sections sections pointer - * @param index the section index - * @return bool - */ - BINLEX_EXPORT static void AppendTrait(struct Trait *trait, struct Section *sections, uint index); - /** - * This method checks if the instruction is a nop instruction. - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT static bool IsNopInsn(cs_insn *ins); - /** - * Checks if the Instruction is a semantic nop (padding) - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT static bool IsSemanticNopInsn(cs_insn *ins); - /** - * This method checks if the instruction is a trap instruction. - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT static bool IsTrapInsn(cs_insn *ins); - /** - * This method checks if the instruction is privileged. - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT static bool IsPrivInsn(cs_insn *ins); - /** - * This method checks if the instruction is a return instruction. - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT static bool IsRetInsn(cs_insn *insn); - /** - * This method if instruction is an unconditional jump. - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT static bool IsUnconditionalJumpInsn(cs_insn *insn); - /** - * This method checks if an instruction is suspicious. - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT bool IsSuspiciousInsn(cs_insn *insn); - /** - * This method checks if the instruction is a conditional jump. - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT static bool IsConditionalJumpInsn(cs_insn *insn); - /** - * This method checks if the instruction is a jump instruction. - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT static bool IsJumpInsn(cs_insn *insn); - /** - * This method checks if the instruction is a call instruction. - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT static bool IsCallInsn(cs_insn *insn); - /** - * This method checks if the instruction is a padding instruction. - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT bool IsPaddingInsn(cs_insn *ins); - /** - * This method returns how many edges an instruction has. - * @param insn - * @return bool - */ - BINLEX_EXPORT static uint64_t GetInsnEdges(cs_insn *insn); - /** - * This method checks if the address is a function. - * @param address address to check - * @return bool - */ - BINLEX_EXPORT static bool IsFunction(set &addresses, uint64_t address); - /** - * This method check sif the address is a block. - * @param address address to check - * @return bool - */ - BINLEX_EXPORT static bool IsBlock(set &addresses, uint64_t address); - /** - * This method checks if the address was already visited by the disassembler. - * @param address address to check - * @return bool - */ - BINLEX_EXPORT static bool IsVisited(map &visited, uint64_t address); - /** - * Checks if Instruction is Wildcard Instruction - * @param insn the instruction - * @return bool - */ - BINLEX_EXPORT static bool IsWildcardInsn(cs_insn *insn); - /** - * This method performs wildcarding on an instruction. - * @param insn the instruction - * @return trait wildcard string - */ - BINLEX_EXPORT static string WildcardInsn(cs_insn *insn); - /** - * This method clears all properties about a trait except its type. - * @param trait the trait struct address - */ - BINLEX_EXPORT static void ClearTrait(struct Trait *trait); - /** - * Gets Trait as JSON - * @param trait pointer to trait structure - * @return json string - */ - BINLEX_EXPORT json GetTrait(struct Trait &trait); - /** - * This method returns a trait as json. - * @return json - */ - vector GetTraits(); - /** - * This method performs post-proceessing on a trait. - * @param trait pointer to the trait to perform post-processing on - */ - BINLEX_EXPORT static void * FinalizeTrait(struct Trait &trait); - /** - * This method appends the queue with a set of functions or blocks. - * @param addresses a set of addresses - * @param operand_type the type of operand - * @param index the section index - */ - BINLEX_EXPORT void AppendQueue(set &addresses, DISASSEMBLER_OPERAND_TYPE operand_type, uint index); - BINLEX_EXPORT ~Disassembler(); - }; -} -#endif diff --git a/include/disassemblerbase.h b/include/disassemblerbase.h deleted file mode 100644 index 3ce84adc..00000000 --- a/include/disassemblerbase.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef DISASSEMBLERBASE_H -#define DISASSEMBLERBASE_H - -#include -#include -#include -#include "json.h" -#include "file.h" -#include "args.h" - -using json = nlohmann::json; - -namespace binlex { - class DisassemblerBase : public Common { - /** - * This class shares common methods between all decompilers. - */ - public: - const binlex::File &file_reference; - /** - * Decompiler constructor requiring file object. - * @param fileref File object - */ - DisassemblerBase(const binlex::File &firef); - /** - * Get Traits as JSON - * @return list of traits json objects - */ - virtual vector GetTraits() = 0; - - /** - * Write Traits to output or file - * This function usees GetTraits() to get the traits data as a json. - */ - BINLEX_EXPORT void WriteTraits(); - - /* - * The following functions are for pybind-only use. They offer a way to pass arguments to - * the CPP code, which otherwise if obtained via command-line arguments. - */ - - /** - * Set Threads and Thread Cycles, via pybind11 - * @param threads number of threads - */ - BINLEX_EXPORT void py_SetThreads(uint threads); - /** - * Sets The Corpus Name, via pybind11 - * @param corpus pointer to corpus name - */ - BINLEX_EXPORT void py_SetCorpus(const char *corpus); - /** - * Sets the tags, via pybind11 - * @param tags set of tags - */ - BINLEX_EXPORT void py_SetTags(const vector &tags); - /** - * Set decompiler mode, via pybind11 - * @param mode decompiler mode - */ - BINLEX_EXPORT void py_SetMode(string mode); - }; -} -#endif diff --git a/include/file.h b/include/file.h deleted file mode 100644 index 9cb447e0..00000000 --- a/include/file.h +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef FILE_H -#define FILE_H - -#include -#include -#include -#ifndef _WIN32 -#include -#endif // _WIN32 -#include -#include "common.h" - -using namespace std; - -namespace binlex { - class File : public Common{ - /** - * This class holds data and methods common to files. - */ - public: - BINARY_ARCH binary_arch = BINARY_ARCH_UNKNOWN; - BINARY_MODE binary_mode = BINARY_MODE_UNKNOWN; - BINARY_TYPE binary_type = BINARY_TYPE_UNKNOWN; - string sha256; - string tlsh; - struct Section { - uint offset; - int size; - void *data; - set functions; - }; - struct Section sections[BINARY_MAX_SECTIONS]; - uint32_t total_exec_sections = 0; - /** - * This method manually sets the binary architecture and its mode. - * @param arch BINARY_ARCH - * @param mode BINARY_MODE - * @return bool - */ - bool SetArchitecture(BINARY_ARCH arch, BINARY_MODE mode); - /** - * Function will calculate all the hashes for the complete file. - * @param file_path: path to the file - */ - void CalculateFileHashes(char *file_path); - /** - ** Function will calculate all the hashes for the complete file. - * @param data: file data in a vector - */ - void CalculateFileHashes(const vector &data); - /** - * Read a file into a vector - * @param file_path path to the file to read - * @return vector containing the bytes of the file - * @throws runtime_error if a read error occurs - */ - vector ReadFileIntoVector(const char *file_path); - /** - * Read data from file. - * @param file_path path to the file to read from - * @return true if reading successful - */ - bool ReadFile(const char *file_path); - /** - * Read data from a buffer - * @param data pointer to data - * @param size size of the buffer - * @return true if reading successful - */ - bool ReadBuffer(void *data, size_t size); - /** - * Read data from std::vector - * @param data pointer to data - * @return true if reading successful - */ - virtual bool ReadVector(const std::vector &data) = 0; - }; -}; - -#endif diff --git a/include/json.h b/include/json.h deleted file mode 100644 index 87475ab3..00000000 --- a/include/json.h +++ /dev/null @@ -1,26753 +0,0 @@ -/* - __ _____ _____ _____ - __| | __| | | | JSON for Modern C++ -| | |__ | | | | | | version 3.10.4 -|_____|_____|_____|_|___| https://github.com/nlohmann/json - -Licensed under the MIT License . -SPDX-License-Identifier: MIT -Copyright (c) 2013-2019 Niels Lohmann . - -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. -*/ - -#ifndef INCLUDE_NLOHMANN_JSON_HPP_ -#define INCLUDE_NLOHMANN_JSON_HPP_ - -#define NLOHMANN_JSON_VERSION_MAJOR 3 -#define NLOHMANN_JSON_VERSION_MINOR 10 -#define NLOHMANN_JSON_VERSION_PATCH 4 - -#include // all_of, find, for_each -#include // nullptr_t, ptrdiff_t, size_t -#include // hash, less -#include // initializer_list -#ifndef JSON_NO_IO - #include // istream, ostream -#endif // JSON_NO_IO -#include // random_access_iterator_tag -#include // unique_ptr -#include // accumulate -#include // string, stoi, to_string -#include // declval, forward, move, pair, swap -#include // vector - -// #include - - -#include -#include - -// #include - - -#include // transform -#include // array -#include // forward_list -#include // inserter, front_inserter, end -#include // map -#include // string -#include // tuple, make_tuple -#include // is_arithmetic, is_same, is_enum, underlying_type, is_convertible -#include // unordered_map -#include // pair, declval -#include // valarray - -// #include - - -#include // exception -#include // runtime_error -#include // to_string -#include // vector - -// #include - - -#include // array -#include // size_t -#include // uint8_t -#include // string - -namespace nlohmann -{ -namespace detail -{ -/////////////////////////// -// JSON type enumeration // -/////////////////////////// - -/*! -@brief the JSON type enumeration - -This enumeration collects the different JSON types. It is internally used to -distinguish the stored values, and the functions @ref basic_json::is_null(), -@ref basic_json::is_object(), @ref basic_json::is_array(), -@ref basic_json::is_string(), @ref basic_json::is_boolean(), -@ref basic_json::is_number() (with @ref basic_json::is_number_integer(), -@ref basic_json::is_number_unsigned(), and @ref basic_json::is_number_float()), -@ref basic_json::is_discarded(), @ref basic_json::is_primitive(), and -@ref basic_json::is_structured() rely on it. - -@note There are three enumeration entries (number_integer, number_unsigned, and -number_float), because the library distinguishes these three types for numbers: -@ref basic_json::number_unsigned_t is used for unsigned integers, -@ref basic_json::number_integer_t is used for signed integers, and -@ref basic_json::number_float_t is used for floating-point numbers or to -approximate integers which do not fit in the limits of their respective type. - -@sa see @ref basic_json::basic_json(const value_t value_type) -- create a JSON -value with the default value for a given type - -@since version 1.0.0 -*/ -enum class value_t : std::uint8_t -{ - null, ///< null value - object, ///< object (unordered set of name/value pairs) - array, ///< array (ordered collection of values) - string, ///< string value - boolean, ///< boolean value - number_integer, ///< number value (signed integer) - number_unsigned, ///< number value (unsigned integer) - number_float, ///< number value (floating-point) - binary, ///< binary array (ordered collection of bytes) - discarded ///< discarded by the parser callback function -}; - -/*! -@brief comparison operator for JSON types - -Returns an ordering that is similar to Python: -- order: null < boolean < number < object < array < string < binary -- furthermore, each type is not smaller than itself -- discarded values are not comparable -- binary is represented as a b"" string in python and directly comparable to a - string; however, making a binary array directly comparable with a string would - be surprising behavior in a JSON file. - -@since version 1.0.0 -*/ -inline bool operator<(const value_t lhs, const value_t rhs) noexcept -{ - static constexpr std::array order = {{ - 0 /* null */, 3 /* object */, 4 /* array */, 5 /* string */, - 1 /* boolean */, 2 /* integer */, 2 /* unsigned */, 2 /* float */, - 6 /* binary */ - } - }; - - const auto l_index = static_cast(lhs); - const auto r_index = static_cast(rhs); - return l_index < order.size() && r_index < order.size() && order[l_index] < order[r_index]; -} -} // namespace detail -} // namespace nlohmann - -// #include - - -#include -// #include - - -#include // declval, pair -// #include - - -/* Hedley - https://nemequ.github.io/hedley - * Created by Evan Nemerson - * - * To the extent possible under law, the author(s) have dedicated all - * copyright and related and neighboring rights to this software to - * the public domain worldwide. This software is distributed without - * any warranty. - * - * For details, see . - * SPDX-License-Identifier: CC0-1.0 - */ - -#if !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < 15) -#if defined(JSON_HEDLEY_VERSION) - #undef JSON_HEDLEY_VERSION -#endif -#define JSON_HEDLEY_VERSION 15 - -#if defined(JSON_HEDLEY_STRINGIFY_EX) - #undef JSON_HEDLEY_STRINGIFY_EX -#endif -#define JSON_HEDLEY_STRINGIFY_EX(x) #x - -#if defined(JSON_HEDLEY_STRINGIFY) - #undef JSON_HEDLEY_STRINGIFY -#endif -#define JSON_HEDLEY_STRINGIFY(x) JSON_HEDLEY_STRINGIFY_EX(x) - -#if defined(JSON_HEDLEY_CONCAT_EX) - #undef JSON_HEDLEY_CONCAT_EX -#endif -#define JSON_HEDLEY_CONCAT_EX(a,b) a##b - -#if defined(JSON_HEDLEY_CONCAT) - #undef JSON_HEDLEY_CONCAT -#endif -#define JSON_HEDLEY_CONCAT(a,b) JSON_HEDLEY_CONCAT_EX(a,b) - -#if defined(JSON_HEDLEY_CONCAT3_EX) - #undef JSON_HEDLEY_CONCAT3_EX -#endif -#define JSON_HEDLEY_CONCAT3_EX(a,b,c) a##b##c - -#if defined(JSON_HEDLEY_CONCAT3) - #undef JSON_HEDLEY_CONCAT3 -#endif -#define JSON_HEDLEY_CONCAT3(a,b,c) JSON_HEDLEY_CONCAT3_EX(a,b,c) - -#if defined(JSON_HEDLEY_VERSION_ENCODE) - #undef JSON_HEDLEY_VERSION_ENCODE -#endif -#define JSON_HEDLEY_VERSION_ENCODE(major,minor,revision) (((major) * 1000000) + ((minor) * 1000) + (revision)) - -#if defined(JSON_HEDLEY_VERSION_DECODE_MAJOR) - #undef JSON_HEDLEY_VERSION_DECODE_MAJOR -#endif -#define JSON_HEDLEY_VERSION_DECODE_MAJOR(version) ((version) / 1000000) - -#if defined(JSON_HEDLEY_VERSION_DECODE_MINOR) - #undef JSON_HEDLEY_VERSION_DECODE_MINOR -#endif -#define JSON_HEDLEY_VERSION_DECODE_MINOR(version) (((version) % 1000000) / 1000) - -#if defined(JSON_HEDLEY_VERSION_DECODE_REVISION) - #undef JSON_HEDLEY_VERSION_DECODE_REVISION -#endif -#define JSON_HEDLEY_VERSION_DECODE_REVISION(version) ((version) % 1000) - -#if defined(JSON_HEDLEY_GNUC_VERSION) - #undef JSON_HEDLEY_GNUC_VERSION -#endif -#if defined(__GNUC__) && defined(__GNUC_PATCHLEVEL__) - #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) -#elif defined(__GNUC__) - #define JSON_HEDLEY_GNUC_VERSION JSON_HEDLEY_VERSION_ENCODE(__GNUC__, __GNUC_MINOR__, 0) -#endif - -#if defined(JSON_HEDLEY_GNUC_VERSION_CHECK) - #undef JSON_HEDLEY_GNUC_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_GNUC_VERSION) - #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GNUC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_MSVC_VERSION) - #undef JSON_HEDLEY_MSVC_VERSION -#endif -#if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 140000000) && !defined(__ICL) - #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 10000000, (_MSC_FULL_VER % 10000000) / 100000, (_MSC_FULL_VER % 100000) / 100) -#elif defined(_MSC_FULL_VER) && !defined(__ICL) - #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_FULL_VER / 1000000, (_MSC_FULL_VER % 1000000) / 10000, (_MSC_FULL_VER % 10000) / 10) -#elif defined(_MSC_VER) && !defined(__ICL) - #define JSON_HEDLEY_MSVC_VERSION JSON_HEDLEY_VERSION_ENCODE(_MSC_VER / 100, _MSC_VER % 100, 0) -#endif - -#if defined(JSON_HEDLEY_MSVC_VERSION_CHECK) - #undef JSON_HEDLEY_MSVC_VERSION_CHECK -#endif -#if !defined(JSON_HEDLEY_MSVC_VERSION) - #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (0) -#elif defined(_MSC_VER) && (_MSC_VER >= 1400) - #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 10000000) + (minor * 100000) + (patch))) -#elif defined(_MSC_VER) && (_MSC_VER >= 1200) - #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_FULL_VER >= ((major * 1000000) + (minor * 10000) + (patch))) -#else - #define JSON_HEDLEY_MSVC_VERSION_CHECK(major,minor,patch) (_MSC_VER >= ((major * 100) + (minor))) -#endif - -#if defined(JSON_HEDLEY_INTEL_VERSION) - #undef JSON_HEDLEY_INTEL_VERSION -#endif -#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && !defined(__ICL) - #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, __INTEL_COMPILER_UPDATE) -#elif defined(__INTEL_COMPILER) && !defined(__ICL) - #define JSON_HEDLEY_INTEL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER / 100, __INTEL_COMPILER % 100, 0) -#endif - -#if defined(JSON_HEDLEY_INTEL_VERSION_CHECK) - #undef JSON_HEDLEY_INTEL_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_INTEL_VERSION) - #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_INTEL_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_INTEL_CL_VERSION) - #undef JSON_HEDLEY_INTEL_CL_VERSION -#endif -#if defined(__INTEL_COMPILER) && defined(__INTEL_COMPILER_UPDATE) && defined(__ICL) - #define JSON_HEDLEY_INTEL_CL_VERSION JSON_HEDLEY_VERSION_ENCODE(__INTEL_COMPILER, __INTEL_COMPILER_UPDATE, 0) -#endif - -#if defined(JSON_HEDLEY_INTEL_CL_VERSION_CHECK) - #undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_INTEL_CL_VERSION) - #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_INTEL_CL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_INTEL_CL_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_PGI_VERSION) - #undef JSON_HEDLEY_PGI_VERSION -#endif -#if defined(__PGI) && defined(__PGIC__) && defined(__PGIC_MINOR__) && defined(__PGIC_PATCHLEVEL__) - #define JSON_HEDLEY_PGI_VERSION JSON_HEDLEY_VERSION_ENCODE(__PGIC__, __PGIC_MINOR__, __PGIC_PATCHLEVEL__) -#endif - -#if defined(JSON_HEDLEY_PGI_VERSION_CHECK) - #undef JSON_HEDLEY_PGI_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_PGI_VERSION) - #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PGI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_PGI_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_SUNPRO_VERSION) - #undef JSON_HEDLEY_SUNPRO_VERSION -#endif -#if defined(__SUNPRO_C) && (__SUNPRO_C > 0x1000) - #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_C >> 16) & 0xf) * 10) + ((__SUNPRO_C >> 12) & 0xf), (((__SUNPRO_C >> 8) & 0xf) * 10) + ((__SUNPRO_C >> 4) & 0xf), (__SUNPRO_C & 0xf) * 10) -#elif defined(__SUNPRO_C) - #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_C >> 8) & 0xf, (__SUNPRO_C >> 4) & 0xf, (__SUNPRO_C) & 0xf) -#elif defined(__SUNPRO_CC) && (__SUNPRO_CC > 0x1000) - #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((((__SUNPRO_CC >> 16) & 0xf) * 10) + ((__SUNPRO_CC >> 12) & 0xf), (((__SUNPRO_CC >> 8) & 0xf) * 10) + ((__SUNPRO_CC >> 4) & 0xf), (__SUNPRO_CC & 0xf) * 10) -#elif defined(__SUNPRO_CC) - #define JSON_HEDLEY_SUNPRO_VERSION JSON_HEDLEY_VERSION_ENCODE((__SUNPRO_CC >> 8) & 0xf, (__SUNPRO_CC >> 4) & 0xf, (__SUNPRO_CC) & 0xf) -#endif - -#if defined(JSON_HEDLEY_SUNPRO_VERSION_CHECK) - #undef JSON_HEDLEY_SUNPRO_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_SUNPRO_VERSION) - #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_SUNPRO_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_SUNPRO_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION) - #undef JSON_HEDLEY_EMSCRIPTEN_VERSION -#endif -#if defined(__EMSCRIPTEN__) - #define JSON_HEDLEY_EMSCRIPTEN_VERSION JSON_HEDLEY_VERSION_ENCODE(__EMSCRIPTEN_major__, __EMSCRIPTEN_minor__, __EMSCRIPTEN_tiny__) -#endif - -#if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK) - #undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_EMSCRIPTEN_VERSION) - #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_EMSCRIPTEN_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_ARM_VERSION) - #undef JSON_HEDLEY_ARM_VERSION -#endif -#if defined(__CC_ARM) && defined(__ARMCOMPILER_VERSION) - #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCOMPILER_VERSION / 1000000, (__ARMCOMPILER_VERSION % 1000000) / 10000, (__ARMCOMPILER_VERSION % 10000) / 100) -#elif defined(__CC_ARM) && defined(__ARMCC_VERSION) - #define JSON_HEDLEY_ARM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ARMCC_VERSION / 1000000, (__ARMCC_VERSION % 1000000) / 10000, (__ARMCC_VERSION % 10000) / 100) -#endif - -#if defined(JSON_HEDLEY_ARM_VERSION_CHECK) - #undef JSON_HEDLEY_ARM_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_ARM_VERSION) - #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_ARM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_ARM_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_IBM_VERSION) - #undef JSON_HEDLEY_IBM_VERSION -#endif -#if defined(__ibmxl__) - #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__ibmxl_version__, __ibmxl_release__, __ibmxl_modification__) -#elif defined(__xlC__) && defined(__xlC_ver__) - #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, (__xlC_ver__ >> 8) & 0xff) -#elif defined(__xlC__) - #define JSON_HEDLEY_IBM_VERSION JSON_HEDLEY_VERSION_ENCODE(__xlC__ >> 8, __xlC__ & 0xff, 0) -#endif - -#if defined(JSON_HEDLEY_IBM_VERSION_CHECK) - #undef JSON_HEDLEY_IBM_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_IBM_VERSION) - #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IBM_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_IBM_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_TI_VERSION) - #undef JSON_HEDLEY_TI_VERSION -#endif -#if \ - defined(__TI_COMPILER_VERSION__) && \ - ( \ - defined(__TMS470__) || defined(__TI_ARM__) || \ - defined(__MSP430__) || \ - defined(__TMS320C2000__) \ - ) -#if (__TI_COMPILER_VERSION__ >= 16000000) - #define JSON_HEDLEY_TI_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) -#endif -#endif - -#if defined(JSON_HEDLEY_TI_VERSION_CHECK) - #undef JSON_HEDLEY_TI_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_TI_VERSION) - #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_TI_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_TI_CL2000_VERSION) - #undef JSON_HEDLEY_TI_CL2000_VERSION -#endif -#if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C2000__) - #define JSON_HEDLEY_TI_CL2000_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) -#endif - -#if defined(JSON_HEDLEY_TI_CL2000_VERSION_CHECK) - #undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_TI_CL2000_VERSION) - #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL2000_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_TI_CL2000_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_TI_CL430_VERSION) - #undef JSON_HEDLEY_TI_CL430_VERSION -#endif -#if defined(__TI_COMPILER_VERSION__) && defined(__MSP430__) - #define JSON_HEDLEY_TI_CL430_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) -#endif - -#if defined(JSON_HEDLEY_TI_CL430_VERSION_CHECK) - #undef JSON_HEDLEY_TI_CL430_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_TI_CL430_VERSION) - #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL430_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_TI_CL430_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_TI_ARMCL_VERSION) - #undef JSON_HEDLEY_TI_ARMCL_VERSION -#endif -#if defined(__TI_COMPILER_VERSION__) && (defined(__TMS470__) || defined(__TI_ARM__)) - #define JSON_HEDLEY_TI_ARMCL_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) -#endif - -#if defined(JSON_HEDLEY_TI_ARMCL_VERSION_CHECK) - #undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_TI_ARMCL_VERSION) - #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_ARMCL_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_TI_CL6X_VERSION) - #undef JSON_HEDLEY_TI_CL6X_VERSION -#endif -#if defined(__TI_COMPILER_VERSION__) && defined(__TMS320C6X__) - #define JSON_HEDLEY_TI_CL6X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) -#endif - -#if defined(JSON_HEDLEY_TI_CL6X_VERSION_CHECK) - #undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_TI_CL6X_VERSION) - #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL6X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_TI_CL6X_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_TI_CL7X_VERSION) - #undef JSON_HEDLEY_TI_CL7X_VERSION -#endif -#if defined(__TI_COMPILER_VERSION__) && defined(__C7000__) - #define JSON_HEDLEY_TI_CL7X_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) -#endif - -#if defined(JSON_HEDLEY_TI_CL7X_VERSION_CHECK) - #undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_TI_CL7X_VERSION) - #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CL7X_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_TI_CL7X_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_TI_CLPRU_VERSION) - #undef JSON_HEDLEY_TI_CLPRU_VERSION -#endif -#if defined(__TI_COMPILER_VERSION__) && defined(__PRU__) - #define JSON_HEDLEY_TI_CLPRU_VERSION JSON_HEDLEY_VERSION_ENCODE(__TI_COMPILER_VERSION__ / 1000000, (__TI_COMPILER_VERSION__ % 1000000) / 1000, (__TI_COMPILER_VERSION__ % 1000)) -#endif - -#if defined(JSON_HEDLEY_TI_CLPRU_VERSION_CHECK) - #undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_TI_CLPRU_VERSION) - #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TI_CLPRU_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_CRAY_VERSION) - #undef JSON_HEDLEY_CRAY_VERSION -#endif -#if defined(_CRAYC) - #if defined(_RELEASE_PATCHLEVEL) - #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, _RELEASE_PATCHLEVEL) - #else - #define JSON_HEDLEY_CRAY_VERSION JSON_HEDLEY_VERSION_ENCODE(_RELEASE_MAJOR, _RELEASE_MINOR, 0) - #endif -#endif - -#if defined(JSON_HEDLEY_CRAY_VERSION_CHECK) - #undef JSON_HEDLEY_CRAY_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_CRAY_VERSION) - #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_CRAY_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_CRAY_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_IAR_VERSION) - #undef JSON_HEDLEY_IAR_VERSION -#endif -#if defined(__IAR_SYSTEMS_ICC__) - #if __VER__ > 1000 - #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE((__VER__ / 1000000), ((__VER__ / 1000) % 1000), (__VER__ % 1000)) - #else - #define JSON_HEDLEY_IAR_VERSION JSON_HEDLEY_VERSION_ENCODE(__VER__ / 100, __VER__ % 100, 0) - #endif -#endif - -#if defined(JSON_HEDLEY_IAR_VERSION_CHECK) - #undef JSON_HEDLEY_IAR_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_IAR_VERSION) - #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_IAR_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_IAR_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_TINYC_VERSION) - #undef JSON_HEDLEY_TINYC_VERSION -#endif -#if defined(__TINYC__) - #define JSON_HEDLEY_TINYC_VERSION JSON_HEDLEY_VERSION_ENCODE(__TINYC__ / 1000, (__TINYC__ / 100) % 10, __TINYC__ % 100) -#endif - -#if defined(JSON_HEDLEY_TINYC_VERSION_CHECK) - #undef JSON_HEDLEY_TINYC_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_TINYC_VERSION) - #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_TINYC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_TINYC_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_DMC_VERSION) - #undef JSON_HEDLEY_DMC_VERSION -#endif -#if defined(__DMC__) - #define JSON_HEDLEY_DMC_VERSION JSON_HEDLEY_VERSION_ENCODE(__DMC__ >> 8, (__DMC__ >> 4) & 0xf, __DMC__ & 0xf) -#endif - -#if defined(JSON_HEDLEY_DMC_VERSION_CHECK) - #undef JSON_HEDLEY_DMC_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_DMC_VERSION) - #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_DMC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_DMC_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_COMPCERT_VERSION) - #undef JSON_HEDLEY_COMPCERT_VERSION -#endif -#if defined(__COMPCERT_VERSION__) - #define JSON_HEDLEY_COMPCERT_VERSION JSON_HEDLEY_VERSION_ENCODE(__COMPCERT_VERSION__ / 10000, (__COMPCERT_VERSION__ / 100) % 100, __COMPCERT_VERSION__ % 100) -#endif - -#if defined(JSON_HEDLEY_COMPCERT_VERSION_CHECK) - #undef JSON_HEDLEY_COMPCERT_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_COMPCERT_VERSION) - #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_COMPCERT_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_COMPCERT_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_PELLES_VERSION) - #undef JSON_HEDLEY_PELLES_VERSION -#endif -#if defined(__POCC__) - #define JSON_HEDLEY_PELLES_VERSION JSON_HEDLEY_VERSION_ENCODE(__POCC__ / 100, __POCC__ % 100, 0) -#endif - -#if defined(JSON_HEDLEY_PELLES_VERSION_CHECK) - #undef JSON_HEDLEY_PELLES_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_PELLES_VERSION) - #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_PELLES_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_PELLES_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_MCST_LCC_VERSION) - #undef JSON_HEDLEY_MCST_LCC_VERSION -#endif -#if defined(__LCC__) && defined(__LCC_MINOR__) - #define JSON_HEDLEY_MCST_LCC_VERSION JSON_HEDLEY_VERSION_ENCODE(__LCC__ / 100, __LCC__ % 100, __LCC_MINOR__) -#endif - -#if defined(JSON_HEDLEY_MCST_LCC_VERSION_CHECK) - #undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_MCST_LCC_VERSION) - #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_MCST_LCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_MCST_LCC_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_GCC_VERSION) - #undef JSON_HEDLEY_GCC_VERSION -#endif -#if \ - defined(JSON_HEDLEY_GNUC_VERSION) && \ - !defined(__clang__) && \ - !defined(JSON_HEDLEY_INTEL_VERSION) && \ - !defined(JSON_HEDLEY_PGI_VERSION) && \ - !defined(JSON_HEDLEY_ARM_VERSION) && \ - !defined(JSON_HEDLEY_CRAY_VERSION) && \ - !defined(JSON_HEDLEY_TI_VERSION) && \ - !defined(JSON_HEDLEY_TI_ARMCL_VERSION) && \ - !defined(JSON_HEDLEY_TI_CL430_VERSION) && \ - !defined(JSON_HEDLEY_TI_CL2000_VERSION) && \ - !defined(JSON_HEDLEY_TI_CL6X_VERSION) && \ - !defined(JSON_HEDLEY_TI_CL7X_VERSION) && \ - !defined(JSON_HEDLEY_TI_CLPRU_VERSION) && \ - !defined(__COMPCERT__) && \ - !defined(JSON_HEDLEY_MCST_LCC_VERSION) - #define JSON_HEDLEY_GCC_VERSION JSON_HEDLEY_GNUC_VERSION -#endif - -#if defined(JSON_HEDLEY_GCC_VERSION_CHECK) - #undef JSON_HEDLEY_GCC_VERSION_CHECK -#endif -#if defined(JSON_HEDLEY_GCC_VERSION) - #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (JSON_HEDLEY_GCC_VERSION >= JSON_HEDLEY_VERSION_ENCODE(major, minor, patch)) -#else - #define JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) (0) -#endif - -#if defined(JSON_HEDLEY_HAS_ATTRIBUTE) - #undef JSON_HEDLEY_HAS_ATTRIBUTE -#endif -#if \ - defined(__has_attribute) && \ - ( \ - (!defined(JSON_HEDLEY_IAR_VERSION) || JSON_HEDLEY_IAR_VERSION_CHECK(8,5,9)) \ - ) -# define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) __has_attribute(attribute) -#else -# define JSON_HEDLEY_HAS_ATTRIBUTE(attribute) (0) -#endif - -#if defined(JSON_HEDLEY_GNUC_HAS_ATTRIBUTE) - #undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE -#endif -#if defined(__has_attribute) - #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) -#else - #define JSON_HEDLEY_GNUC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_GCC_HAS_ATTRIBUTE) - #undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE -#endif -#if defined(__has_attribute) - #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) -#else - #define JSON_HEDLEY_GCC_HAS_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE) - #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE -#endif -#if \ - defined(__has_cpp_attribute) && \ - defined(__cplusplus) && \ - (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) - #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) __has_cpp_attribute(attribute) -#else - #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) (0) -#endif - -#if defined(JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS) - #undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS -#endif -#if !defined(__cplusplus) || !defined(__has_cpp_attribute) - #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0) -#elif \ - !defined(JSON_HEDLEY_PGI_VERSION) && \ - !defined(JSON_HEDLEY_IAR_VERSION) && \ - (!defined(JSON_HEDLEY_SUNPRO_VERSION) || JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0)) && \ - (!defined(JSON_HEDLEY_MSVC_VERSION) || JSON_HEDLEY_MSVC_VERSION_CHECK(19,20,0)) - #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(ns::attribute) -#else - #define JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(ns,attribute) (0) -#endif - -#if defined(JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE) - #undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE -#endif -#if defined(__has_cpp_attribute) && defined(__cplusplus) - #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute) -#else - #define JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE) - #undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE -#endif -#if defined(__has_cpp_attribute) && defined(__cplusplus) - #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) __has_cpp_attribute(attribute) -#else - #define JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_HAS_BUILTIN) - #undef JSON_HEDLEY_HAS_BUILTIN -#endif -#if defined(__has_builtin) - #define JSON_HEDLEY_HAS_BUILTIN(builtin) __has_builtin(builtin) -#else - #define JSON_HEDLEY_HAS_BUILTIN(builtin) (0) -#endif - -#if defined(JSON_HEDLEY_GNUC_HAS_BUILTIN) - #undef JSON_HEDLEY_GNUC_HAS_BUILTIN -#endif -#if defined(__has_builtin) - #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin) -#else - #define JSON_HEDLEY_GNUC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_GCC_HAS_BUILTIN) - #undef JSON_HEDLEY_GCC_HAS_BUILTIN -#endif -#if defined(__has_builtin) - #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) __has_builtin(builtin) -#else - #define JSON_HEDLEY_GCC_HAS_BUILTIN(builtin,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_HAS_FEATURE) - #undef JSON_HEDLEY_HAS_FEATURE -#endif -#if defined(__has_feature) - #define JSON_HEDLEY_HAS_FEATURE(feature) __has_feature(feature) -#else - #define JSON_HEDLEY_HAS_FEATURE(feature) (0) -#endif - -#if defined(JSON_HEDLEY_GNUC_HAS_FEATURE) - #undef JSON_HEDLEY_GNUC_HAS_FEATURE -#endif -#if defined(__has_feature) - #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature) -#else - #define JSON_HEDLEY_GNUC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_GCC_HAS_FEATURE) - #undef JSON_HEDLEY_GCC_HAS_FEATURE -#endif -#if defined(__has_feature) - #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) __has_feature(feature) -#else - #define JSON_HEDLEY_GCC_HAS_FEATURE(feature,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_HAS_EXTENSION) - #undef JSON_HEDLEY_HAS_EXTENSION -#endif -#if defined(__has_extension) - #define JSON_HEDLEY_HAS_EXTENSION(extension) __has_extension(extension) -#else - #define JSON_HEDLEY_HAS_EXTENSION(extension) (0) -#endif - -#if defined(JSON_HEDLEY_GNUC_HAS_EXTENSION) - #undef JSON_HEDLEY_GNUC_HAS_EXTENSION -#endif -#if defined(__has_extension) - #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension) -#else - #define JSON_HEDLEY_GNUC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_GCC_HAS_EXTENSION) - #undef JSON_HEDLEY_GCC_HAS_EXTENSION -#endif -#if defined(__has_extension) - #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) __has_extension(extension) -#else - #define JSON_HEDLEY_GCC_HAS_EXTENSION(extension,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE) - #undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE -#endif -#if defined(__has_declspec_attribute) - #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) __has_declspec_attribute(attribute) -#else - #define JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) (0) -#endif - -#if defined(JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE) - #undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE -#endif -#if defined(__has_declspec_attribute) - #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute) -#else - #define JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE) - #undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE -#endif -#if defined(__has_declspec_attribute) - #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) __has_declspec_attribute(attribute) -#else - #define JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE(attribute,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_HAS_WARNING) - #undef JSON_HEDLEY_HAS_WARNING -#endif -#if defined(__has_warning) - #define JSON_HEDLEY_HAS_WARNING(warning) __has_warning(warning) -#else - #define JSON_HEDLEY_HAS_WARNING(warning) (0) -#endif - -#if defined(JSON_HEDLEY_GNUC_HAS_WARNING) - #undef JSON_HEDLEY_GNUC_HAS_WARNING -#endif -#if defined(__has_warning) - #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning) -#else - #define JSON_HEDLEY_GNUC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GNUC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_GCC_HAS_WARNING) - #undef JSON_HEDLEY_GCC_HAS_WARNING -#endif -#if defined(__has_warning) - #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) __has_warning(warning) -#else - #define JSON_HEDLEY_GCC_HAS_WARNING(warning,major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) -#endif - -#if \ - (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ - defined(__clang__) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,0,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) || \ - JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,17) || \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(8,0,0) || \ - (JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) && defined(__C99_PRAGMA_OPERATOR)) - #define JSON_HEDLEY_PRAGMA(value) _Pragma(#value) -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) - #define JSON_HEDLEY_PRAGMA(value) __pragma(value) -#else - #define JSON_HEDLEY_PRAGMA(value) -#endif - -#if defined(JSON_HEDLEY_DIAGNOSTIC_PUSH) - #undef JSON_HEDLEY_DIAGNOSTIC_PUSH -#endif -#if defined(JSON_HEDLEY_DIAGNOSTIC_POP) - #undef JSON_HEDLEY_DIAGNOSTIC_POP -#endif -#if defined(__clang__) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("clang diagnostic push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("clang diagnostic pop") -#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") -#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("GCC diagnostic push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("GCC diagnostic pop") -#elif \ - JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH __pragma(warning(push)) - #define JSON_HEDLEY_DIAGNOSTIC_POP __pragma(warning(pop)) -#elif JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("pop") -#elif \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,4,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("diag_push") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("diag_pop") -#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0) - #define JSON_HEDLEY_DIAGNOSTIC_PUSH _Pragma("warning(push)") - #define JSON_HEDLEY_DIAGNOSTIC_POP _Pragma("warning(pop)") -#else - #define JSON_HEDLEY_DIAGNOSTIC_PUSH - #define JSON_HEDLEY_DIAGNOSTIC_POP -#endif - -/* JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ is for - HEDLEY INTERNAL USE ONLY. API subject to change without notice. */ -#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_) - #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ -#endif -#if defined(__cplusplus) -# if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat") -# if JSON_HEDLEY_HAS_WARNING("-Wc++17-extensions") -# if JSON_HEDLEY_HAS_WARNING("-Wc++1z-extensions") -# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ - JSON_HEDLEY_DIAGNOSTIC_PUSH \ - _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ - _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \ - _Pragma("clang diagnostic ignored \"-Wc++1z-extensions\"") \ - xpr \ - JSON_HEDLEY_DIAGNOSTIC_POP -# else -# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ - JSON_HEDLEY_DIAGNOSTIC_PUSH \ - _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ - _Pragma("clang diagnostic ignored \"-Wc++17-extensions\"") \ - xpr \ - JSON_HEDLEY_DIAGNOSTIC_POP -# endif -# else -# define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(xpr) \ - JSON_HEDLEY_DIAGNOSTIC_PUSH \ - _Pragma("clang diagnostic ignored \"-Wc++98-compat\"") \ - xpr \ - JSON_HEDLEY_DIAGNOSTIC_POP -# endif -# endif -#endif -#if !defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(x) x -#endif - -#if defined(JSON_HEDLEY_CONST_CAST) - #undef JSON_HEDLEY_CONST_CAST -#endif -#if defined(__cplusplus) -# define JSON_HEDLEY_CONST_CAST(T, expr) (const_cast(expr)) -#elif \ - JSON_HEDLEY_HAS_WARNING("-Wcast-qual") || \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) -# define JSON_HEDLEY_CONST_CAST(T, expr) (__extension__ ({ \ - JSON_HEDLEY_DIAGNOSTIC_PUSH \ - JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL \ - ((T) (expr)); \ - JSON_HEDLEY_DIAGNOSTIC_POP \ - })) -#else -# define JSON_HEDLEY_CONST_CAST(T, expr) ((T) (expr)) -#endif - -#if defined(JSON_HEDLEY_REINTERPRET_CAST) - #undef JSON_HEDLEY_REINTERPRET_CAST -#endif -#if defined(__cplusplus) - #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) (reinterpret_cast(expr)) -#else - #define JSON_HEDLEY_REINTERPRET_CAST(T, expr) ((T) (expr)) -#endif - -#if defined(JSON_HEDLEY_STATIC_CAST) - #undef JSON_HEDLEY_STATIC_CAST -#endif -#if defined(__cplusplus) - #define JSON_HEDLEY_STATIC_CAST(T, expr) (static_cast(expr)) -#else - #define JSON_HEDLEY_STATIC_CAST(T, expr) ((T) (expr)) -#endif - -#if defined(JSON_HEDLEY_CPP_CAST) - #undef JSON_HEDLEY_CPP_CAST -#endif -#if defined(__cplusplus) -# if JSON_HEDLEY_HAS_WARNING("-Wold-style-cast") -# define JSON_HEDLEY_CPP_CAST(T, expr) \ - JSON_HEDLEY_DIAGNOSTIC_PUSH \ - _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \ - ((T) (expr)) \ - JSON_HEDLEY_DIAGNOSTIC_POP -# elif JSON_HEDLEY_IAR_VERSION_CHECK(8,3,0) -# define JSON_HEDLEY_CPP_CAST(T, expr) \ - JSON_HEDLEY_DIAGNOSTIC_PUSH \ - _Pragma("diag_suppress=Pe137") \ - JSON_HEDLEY_DIAGNOSTIC_POP -# else -# define JSON_HEDLEY_CPP_CAST(T, expr) ((T) (expr)) -# endif -#else -# define JSON_HEDLEY_CPP_CAST(T, expr) (expr) -#endif - -#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED) - #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED -#endif -#if JSON_HEDLEY_HAS_WARNING("-Wdeprecated-declarations") - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") -#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warning(disable:1478 1786)") -#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:1478 1786)) -#elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1216,1444,1445") -#elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444") -#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED __pragma(warning(disable:4996)) -#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1215,1444") -#elif \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ - (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress 1291,1718") -#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && !defined(__cplusplus) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,E_DEPRECATED_ATT,E_DEPRECATED_ATT_MESS)") -#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) && defined(__cplusplus) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("error_messages(off,symdeprecated,symdeprecated2)") -#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("diag_suppress=Pe1444,Pe1215") -#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,90,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED _Pragma("warn(disable:2241)") -#else - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED -#endif - -#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS) - #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS -#endif -#if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas") - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"") -#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("warning(disable:161)") -#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:161)) -#elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 1675") -#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("GCC diagnostic ignored \"-Wunknown-pragmas\"") -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS __pragma(warning(disable:4068)) -#elif \ - JSON_HEDLEY_TI_VERSION_CHECK(16,9,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163") -#elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 163") -#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress=Pe161") -#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS _Pragma("diag_suppress 161") -#else - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS -#endif - -#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES) - #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES -#endif -#if JSON_HEDLEY_HAS_WARNING("-Wunknown-attributes") - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("clang diagnostic ignored \"-Wunknown-attributes\"") -#elif JSON_HEDLEY_GCC_VERSION_CHECK(4,6,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"") -#elif JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("warning(disable:1292)") -#elif JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:1292)) -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES __pragma(warning(disable:5030)) -#elif JSON_HEDLEY_PGI_VERSION_CHECK(20,7,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097,1098") -#elif JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097") -#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("error_messages(off,attrskipunsup)") -#elif \ - JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1173") -#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress=Pe1097") -#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES _Pragma("diag_suppress 1097") -#else - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES -#endif - -#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL) - #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL -#endif -#if JSON_HEDLEY_HAS_WARNING("-Wcast-qual") - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("clang diagnostic ignored \"-Wcast-qual\"") -#elif JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("warning(disable:2203 2331)") -#elif JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL _Pragma("GCC diagnostic ignored \"-Wcast-qual\"") -#else - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL -#endif - -#if defined(JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION) - #undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION -#endif -#if JSON_HEDLEY_HAS_WARNING("-Wunused-function") - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("clang diagnostic ignored \"-Wunused-function\"") -#elif JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("GCC diagnostic ignored \"-Wunused-function\"") -#elif JSON_HEDLEY_MSVC_VERSION_CHECK(1,0,0) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION __pragma(warning(disable:4505)) -#elif JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION _Pragma("diag_suppress 3142") -#else - #define JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION -#endif - -#if defined(JSON_HEDLEY_DEPRECATED) - #undef JSON_HEDLEY_DEPRECATED -#endif -#if defined(JSON_HEDLEY_DEPRECATED_FOR) - #undef JSON_HEDLEY_DEPRECATED_FOR -#endif -#if \ - JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) - #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since)) - #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement)) -#elif \ - (JSON_HEDLEY_HAS_EXTENSION(attribute_deprecated_with_message) && !defined(JSON_HEDLEY_IAR_VERSION)) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,13,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(18,1,0) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(18,1,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,3,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,3,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since))) - #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement))) -#elif defined(__cplusplus) && (__cplusplus >= 201402L) - #define JSON_HEDLEY_DEPRECATED(since) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since)]]) - #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[deprecated("Since " #since "; use " #replacement)]]) -#elif \ - JSON_HEDLEY_HAS_ATTRIBUTE(deprecated) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ - (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ - JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) - #define JSON_HEDLEY_DEPRECATED(since) __attribute__((__deprecated__)) - #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__)) -#elif \ - JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ - JSON_HEDLEY_PELLES_VERSION_CHECK(6,50,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) - #define JSON_HEDLEY_DEPRECATED(since) __declspec(deprecated) - #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated) -#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) - #define JSON_HEDLEY_DEPRECATED(since) _Pragma("deprecated") - #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) _Pragma("deprecated") -#else - #define JSON_HEDLEY_DEPRECATED(since) - #define JSON_HEDLEY_DEPRECATED_FOR(since, replacement) -#endif - -#if defined(JSON_HEDLEY_UNAVAILABLE) - #undef JSON_HEDLEY_UNAVAILABLE -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(warning) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,3,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_UNAVAILABLE(available_since) __attribute__((__warning__("Not available until " #available_since))) -#else - #define JSON_HEDLEY_UNAVAILABLE(available_since) -#endif - -#if defined(JSON_HEDLEY_WARN_UNUSED_RESULT) - #undef JSON_HEDLEY_WARN_UNUSED_RESULT -#endif -#if defined(JSON_HEDLEY_WARN_UNUSED_RESULT_MSG) - #undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(warn_unused_result) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ - (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) - #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) __attribute__((__warn_unused_result__)) -#elif (JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) >= 201907L) - #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) - #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard(msg)]]) -#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(nodiscard) - #define JSON_HEDLEY_WARN_UNUSED_RESULT JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) - #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[nodiscard]]) -#elif defined(_Check_return_) /* SAL */ - #define JSON_HEDLEY_WARN_UNUSED_RESULT _Check_return_ - #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) _Check_return_ -#else - #define JSON_HEDLEY_WARN_UNUSED_RESULT - #define JSON_HEDLEY_WARN_UNUSED_RESULT_MSG(msg) -#endif - -#if defined(JSON_HEDLEY_SENTINEL) - #undef JSON_HEDLEY_SENTINEL -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(sentinel) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_SENTINEL(position) __attribute__((__sentinel__(position))) -#else - #define JSON_HEDLEY_SENTINEL(position) -#endif - -#if defined(JSON_HEDLEY_NO_RETURN) - #undef JSON_HEDLEY_NO_RETURN -#endif -#if JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) - #define JSON_HEDLEY_NO_RETURN __noreturn -#elif \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) -#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L - #define JSON_HEDLEY_NO_RETURN _Noreturn -#elif defined(__cplusplus) && (__cplusplus >= 201103L) - #define JSON_HEDLEY_NO_RETURN JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[noreturn]]) -#elif \ - JSON_HEDLEY_HAS_ATTRIBUTE(noreturn) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,2,0) || \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ - (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) - #define JSON_HEDLEY_NO_RETURN __attribute__((__noreturn__)) -#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) - #define JSON_HEDLEY_NO_RETURN _Pragma("does_not_return") -#elif \ - JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) - #define JSON_HEDLEY_NO_RETURN __declspec(noreturn) -#elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus) - #define JSON_HEDLEY_NO_RETURN _Pragma("FUNC_NEVER_RETURNS;") -#elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0) - #define JSON_HEDLEY_NO_RETURN __attribute((noreturn)) -#elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0) - #define JSON_HEDLEY_NO_RETURN __declspec(noreturn) -#else - #define JSON_HEDLEY_NO_RETURN -#endif - -#if defined(JSON_HEDLEY_NO_ESCAPE) - #undef JSON_HEDLEY_NO_ESCAPE -#endif -#if JSON_HEDLEY_HAS_ATTRIBUTE(noescape) - #define JSON_HEDLEY_NO_ESCAPE __attribute__((__noescape__)) -#else - #define JSON_HEDLEY_NO_ESCAPE -#endif - -#if defined(JSON_HEDLEY_UNREACHABLE) - #undef JSON_HEDLEY_UNREACHABLE -#endif -#if defined(JSON_HEDLEY_UNREACHABLE_RETURN) - #undef JSON_HEDLEY_UNREACHABLE_RETURN -#endif -#if defined(JSON_HEDLEY_ASSUME) - #undef JSON_HEDLEY_ASSUME -#endif -#if \ - JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) - #define JSON_HEDLEY_ASSUME(expr) __assume(expr) -#elif JSON_HEDLEY_HAS_BUILTIN(__builtin_assume) - #define JSON_HEDLEY_ASSUME(expr) __builtin_assume(expr) -#elif \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) - #if defined(__cplusplus) - #define JSON_HEDLEY_ASSUME(expr) std::_nassert(expr) - #else - #define JSON_HEDLEY_ASSUME(expr) _nassert(expr) - #endif -#endif -#if \ - (JSON_HEDLEY_HAS_BUILTIN(__builtin_unreachable) && (!defined(JSON_HEDLEY_ARM_VERSION))) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,5,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(18,10,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(13,1,5) || \ - JSON_HEDLEY_CRAY_VERSION_CHECK(10,0,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_UNREACHABLE() __builtin_unreachable() -#elif defined(JSON_HEDLEY_ASSUME) - #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0) -#endif -#if !defined(JSON_HEDLEY_ASSUME) - #if defined(JSON_HEDLEY_UNREACHABLE) - #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, ((expr) ? 1 : (JSON_HEDLEY_UNREACHABLE(), 1))) - #else - #define JSON_HEDLEY_ASSUME(expr) JSON_HEDLEY_STATIC_CAST(void, expr) - #endif -#endif -#if defined(JSON_HEDLEY_UNREACHABLE) - #if \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) - #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (JSON_HEDLEY_STATIC_CAST(void, JSON_HEDLEY_ASSUME(0)), (value)) - #else - #define JSON_HEDLEY_UNREACHABLE_RETURN(value) JSON_HEDLEY_UNREACHABLE() - #endif -#else - #define JSON_HEDLEY_UNREACHABLE_RETURN(value) return (value) -#endif -#if !defined(JSON_HEDLEY_UNREACHABLE) - #define JSON_HEDLEY_UNREACHABLE() JSON_HEDLEY_ASSUME(0) -#endif - -JSON_HEDLEY_DIAGNOSTIC_PUSH -#if JSON_HEDLEY_HAS_WARNING("-Wpedantic") - #pragma clang diagnostic ignored "-Wpedantic" -#endif -#if JSON_HEDLEY_HAS_WARNING("-Wc++98-compat-pedantic") && defined(__cplusplus) - #pragma clang diagnostic ignored "-Wc++98-compat-pedantic" -#endif -#if JSON_HEDLEY_GCC_HAS_WARNING("-Wvariadic-macros",4,0,0) - #if defined(__clang__) - #pragma clang diagnostic ignored "-Wvariadic-macros" - #elif defined(JSON_HEDLEY_GCC_VERSION) - #pragma GCC diagnostic ignored "-Wvariadic-macros" - #endif -#endif -#if defined(JSON_HEDLEY_NON_NULL) - #undef JSON_HEDLEY_NON_NULL -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(nonnull) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) - #define JSON_HEDLEY_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__))) -#else - #define JSON_HEDLEY_NON_NULL(...) -#endif -JSON_HEDLEY_DIAGNOSTIC_POP - -#if defined(JSON_HEDLEY_PRINTF_FORMAT) - #undef JSON_HEDLEY_PRINTF_FORMAT -#endif -#if defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && !defined(__USE_MINGW_ANSI_STDIO) - #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(ms_printf, string_idx, first_to_check))) -#elif defined(__MINGW32__) && JSON_HEDLEY_GCC_HAS_ATTRIBUTE(format,4,4,0) && defined(__USE_MINGW_ANSI_STDIO) - #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(gnu_printf, string_idx, first_to_check))) -#elif \ - JSON_HEDLEY_HAS_ATTRIBUTE(format) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(5,6,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ - (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __attribute__((__format__(__printf__, string_idx, first_to_check))) -#elif JSON_HEDLEY_PELLES_VERSION_CHECK(6,0,0) - #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) __declspec(vaformat(printf,string_idx,first_to_check)) -#else - #define JSON_HEDLEY_PRINTF_FORMAT(string_idx,first_to_check) -#endif - -#if defined(JSON_HEDLEY_CONSTEXPR) - #undef JSON_HEDLEY_CONSTEXPR -#endif -#if defined(__cplusplus) - #if __cplusplus >= 201103L - #define JSON_HEDLEY_CONSTEXPR JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(constexpr) - #endif -#endif -#if !defined(JSON_HEDLEY_CONSTEXPR) - #define JSON_HEDLEY_CONSTEXPR -#endif - -#if defined(JSON_HEDLEY_PREDICT) - #undef JSON_HEDLEY_PREDICT -#endif -#if defined(JSON_HEDLEY_LIKELY) - #undef JSON_HEDLEY_LIKELY -#endif -#if defined(JSON_HEDLEY_UNLIKELY) - #undef JSON_HEDLEY_UNLIKELY -#endif -#if defined(JSON_HEDLEY_UNPREDICTABLE) - #undef JSON_HEDLEY_UNPREDICTABLE -#endif -#if JSON_HEDLEY_HAS_BUILTIN(__builtin_unpredictable) - #define JSON_HEDLEY_UNPREDICTABLE(expr) __builtin_unpredictable((expr)) -#endif -#if \ - (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect_with_probability) && !defined(JSON_HEDLEY_PGI_VERSION)) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(9,0,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) -# define JSON_HEDLEY_PREDICT(expr, value, probability) __builtin_expect_with_probability( (expr), (value), (probability)) -# define JSON_HEDLEY_PREDICT_TRUE(expr, probability) __builtin_expect_with_probability(!!(expr), 1 , (probability)) -# define JSON_HEDLEY_PREDICT_FALSE(expr, probability) __builtin_expect_with_probability(!!(expr), 0 , (probability)) -# define JSON_HEDLEY_LIKELY(expr) __builtin_expect (!!(expr), 1 ) -# define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect (!!(expr), 0 ) -#elif \ - (JSON_HEDLEY_HAS_BUILTIN(__builtin_expect) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,0,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,15,0) && defined(__cplusplus)) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,7,0) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,1,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,27) || \ - JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) -# define JSON_HEDLEY_PREDICT(expr, expected, probability) \ - (((probability) >= 0.9) ? __builtin_expect((expr), (expected)) : (JSON_HEDLEY_STATIC_CAST(void, expected), (expr))) -# define JSON_HEDLEY_PREDICT_TRUE(expr, probability) \ - (__extension__ ({ \ - double hedley_probability_ = (probability); \ - ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 1) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 0) : !!(expr))); \ - })) -# define JSON_HEDLEY_PREDICT_FALSE(expr, probability) \ - (__extension__ ({ \ - double hedley_probability_ = (probability); \ - ((hedley_probability_ >= 0.9) ? __builtin_expect(!!(expr), 0) : ((hedley_probability_ <= 0.1) ? __builtin_expect(!!(expr), 1) : !!(expr))); \ - })) -# define JSON_HEDLEY_LIKELY(expr) __builtin_expect(!!(expr), 1) -# define JSON_HEDLEY_UNLIKELY(expr) __builtin_expect(!!(expr), 0) -#else -# define JSON_HEDLEY_PREDICT(expr, expected, probability) (JSON_HEDLEY_STATIC_CAST(void, expected), (expr)) -# define JSON_HEDLEY_PREDICT_TRUE(expr, probability) (!!(expr)) -# define JSON_HEDLEY_PREDICT_FALSE(expr, probability) (!!(expr)) -# define JSON_HEDLEY_LIKELY(expr) (!!(expr)) -# define JSON_HEDLEY_UNLIKELY(expr) (!!(expr)) -#endif -#if !defined(JSON_HEDLEY_UNPREDICTABLE) - #define JSON_HEDLEY_UNPREDICTABLE(expr) JSON_HEDLEY_PREDICT(expr, 1, 0.5) -#endif - -#if defined(JSON_HEDLEY_MALLOC) - #undef JSON_HEDLEY_MALLOC -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(malloc) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ - (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_MALLOC __attribute__((__malloc__)) -#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) - #define JSON_HEDLEY_MALLOC _Pragma("returns_new_memory") -#elif \ - JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) - #define JSON_HEDLEY_MALLOC __declspec(restrict) -#else - #define JSON_HEDLEY_MALLOC -#endif - -#if defined(JSON_HEDLEY_PURE) - #undef JSON_HEDLEY_PURE -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(pure) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(2,96,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ - (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) -# define JSON_HEDLEY_PURE __attribute__((__pure__)) -#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) -# define JSON_HEDLEY_PURE _Pragma("does_not_write_global_data") -#elif defined(__cplusplus) && \ - ( \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(2,0,1) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(4,0,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) \ - ) -# define JSON_HEDLEY_PURE _Pragma("FUNC_IS_PURE;") -#else -# define JSON_HEDLEY_PURE -#endif - -#if defined(JSON_HEDLEY_CONST) - #undef JSON_HEDLEY_CONST -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(const) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(2,5,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ - (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_CONST __attribute__((__const__)) -#elif \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) - #define JSON_HEDLEY_CONST _Pragma("no_side_effect") -#else - #define JSON_HEDLEY_CONST JSON_HEDLEY_PURE -#endif - -#if defined(JSON_HEDLEY_RESTRICT) - #undef JSON_HEDLEY_RESTRICT -#endif -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && !defined(__cplusplus) - #define JSON_HEDLEY_RESTRICT restrict -#elif \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,1,0) || \ - JSON_HEDLEY_MSVC_VERSION_CHECK(14,0,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(17,10,0) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,4) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,1,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,14,0) && defined(__cplusplus)) || \ - JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) || \ - defined(__clang__) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_RESTRICT __restrict -#elif JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,3,0) && !defined(__cplusplus) - #define JSON_HEDLEY_RESTRICT _Restrict -#else - #define JSON_HEDLEY_RESTRICT -#endif - -#if defined(JSON_HEDLEY_INLINE) - #undef JSON_HEDLEY_INLINE -#endif -#if \ - (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || \ - (defined(__cplusplus) && (__cplusplus >= 199711L)) - #define JSON_HEDLEY_INLINE inline -#elif \ - defined(JSON_HEDLEY_GCC_VERSION) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(6,2,0) - #define JSON_HEDLEY_INLINE __inline__ -#elif \ - JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,1,0) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(3,1,0) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,2,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(8,0,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_INLINE __inline -#else - #define JSON_HEDLEY_INLINE -#endif - -#if defined(JSON_HEDLEY_ALWAYS_INLINE) - #undef JSON_HEDLEY_ALWAYS_INLINE -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(always_inline) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ - (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ - JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) -# define JSON_HEDLEY_ALWAYS_INLINE __attribute__((__always_inline__)) JSON_HEDLEY_INLINE -#elif \ - JSON_HEDLEY_MSVC_VERSION_CHECK(12,0,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) -# define JSON_HEDLEY_ALWAYS_INLINE __forceinline -#elif defined(__cplusplus) && \ - ( \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) \ - ) -# define JSON_HEDLEY_ALWAYS_INLINE _Pragma("FUNC_ALWAYS_INLINE;") -#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) -# define JSON_HEDLEY_ALWAYS_INLINE _Pragma("inline=forced") -#else -# define JSON_HEDLEY_ALWAYS_INLINE JSON_HEDLEY_INLINE -#endif - -#if defined(JSON_HEDLEY_NEVER_INLINE) - #undef JSON_HEDLEY_NEVER_INLINE -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(noinline) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,0,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(10,1,0) || \ - JSON_HEDLEY_TI_VERSION_CHECK(15,12,0) || \ - (JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(4,8,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_ARMCL_VERSION_CHECK(5,2,0) || \ - (JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL2000_VERSION_CHECK(6,4,0) || \ - (JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,0,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(4,3,0) || \ - (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) || \ - JSON_HEDLEY_TI_CL7X_VERSION_CHECK(1,2,0) || \ - JSON_HEDLEY_TI_CLPRU_VERSION_CHECK(2,1,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) || \ - JSON_HEDLEY_IAR_VERSION_CHECK(8,10,0) - #define JSON_HEDLEY_NEVER_INLINE __attribute__((__noinline__)) -#elif \ - JSON_HEDLEY_MSVC_VERSION_CHECK(13,10,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) - #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline) -#elif JSON_HEDLEY_PGI_VERSION_CHECK(10,2,0) - #define JSON_HEDLEY_NEVER_INLINE _Pragma("noinline") -#elif JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,0,0) && defined(__cplusplus) - #define JSON_HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;") -#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) - #define JSON_HEDLEY_NEVER_INLINE _Pragma("inline=never") -#elif JSON_HEDLEY_COMPCERT_VERSION_CHECK(3,2,0) - #define JSON_HEDLEY_NEVER_INLINE __attribute((noinline)) -#elif JSON_HEDLEY_PELLES_VERSION_CHECK(9,0,0) - #define JSON_HEDLEY_NEVER_INLINE __declspec(noinline) -#else - #define JSON_HEDLEY_NEVER_INLINE -#endif - -#if defined(JSON_HEDLEY_PRIVATE) - #undef JSON_HEDLEY_PRIVATE -#endif -#if defined(JSON_HEDLEY_PUBLIC) - #undef JSON_HEDLEY_PUBLIC -#endif -#if defined(JSON_HEDLEY_IMPORT) - #undef JSON_HEDLEY_IMPORT -#endif -#if defined(_WIN32) || defined(__CYGWIN__) -# define JSON_HEDLEY_PRIVATE -# define JSON_HEDLEY_PUBLIC __declspec(dllexport) -# define JSON_HEDLEY_IMPORT __declspec(dllimport) -#else -# if \ - JSON_HEDLEY_HAS_ATTRIBUTE(visibility) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \ - JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \ - ( \ - defined(__TI_EABI__) && \ - ( \ - (JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,2,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__)) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(7,5,0) \ - ) \ - ) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) -# define JSON_HEDLEY_PRIVATE __attribute__((__visibility__("hidden"))) -# define JSON_HEDLEY_PUBLIC __attribute__((__visibility__("default"))) -# else -# define JSON_HEDLEY_PRIVATE -# define JSON_HEDLEY_PUBLIC -# endif -# define JSON_HEDLEY_IMPORT extern -#endif - -#if defined(JSON_HEDLEY_NO_THROW) - #undef JSON_HEDLEY_NO_THROW -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(nothrow) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,3,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_NO_THROW __attribute__((__nothrow__)) -#elif \ - JSON_HEDLEY_MSVC_VERSION_CHECK(13,1,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) - #define JSON_HEDLEY_NO_THROW __declspec(nothrow) -#else - #define JSON_HEDLEY_NO_THROW -#endif - -#if defined(JSON_HEDLEY_FALL_THROUGH) - #undef JSON_HEDLEY_FALL_THROUGH -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(fallthrough) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(7,0,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_FALL_THROUGH __attribute__((__fallthrough__)) -#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS(clang,fallthrough) - #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[clang::fallthrough]]) -#elif JSON_HEDLEY_HAS_CPP_ATTRIBUTE(fallthrough) - #define JSON_HEDLEY_FALL_THROUGH JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_([[fallthrough]]) -#elif defined(__fallthrough) /* SAL */ - #define JSON_HEDLEY_FALL_THROUGH __fallthrough -#else - #define JSON_HEDLEY_FALL_THROUGH -#endif - -#if defined(JSON_HEDLEY_RETURNS_NON_NULL) - #undef JSON_HEDLEY_RETURNS_NON_NULL -#endif -#if \ - JSON_HEDLEY_HAS_ATTRIBUTE(returns_nonnull) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_RETURNS_NON_NULL __attribute__((__returns_nonnull__)) -#elif defined(_Ret_notnull_) /* SAL */ - #define JSON_HEDLEY_RETURNS_NON_NULL _Ret_notnull_ -#else - #define JSON_HEDLEY_RETURNS_NON_NULL -#endif - -#if defined(JSON_HEDLEY_ARRAY_PARAM) - #undef JSON_HEDLEY_ARRAY_PARAM -#endif -#if \ - defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) && \ - !defined(__STDC_NO_VLA__) && \ - !defined(__cplusplus) && \ - !defined(JSON_HEDLEY_PGI_VERSION) && \ - !defined(JSON_HEDLEY_TINYC_VERSION) - #define JSON_HEDLEY_ARRAY_PARAM(name) (name) -#else - #define JSON_HEDLEY_ARRAY_PARAM(name) -#endif - -#if defined(JSON_HEDLEY_IS_CONSTANT) - #undef JSON_HEDLEY_IS_CONSTANT -#endif -#if defined(JSON_HEDLEY_REQUIRE_CONSTEXPR) - #undef JSON_HEDLEY_REQUIRE_CONSTEXPR -#endif -/* JSON_HEDLEY_IS_CONSTEXPR_ is for - HEDLEY INTERNAL USE ONLY. API subject to change without notice. */ -#if defined(JSON_HEDLEY_IS_CONSTEXPR_) - #undef JSON_HEDLEY_IS_CONSTEXPR_ -#endif -#if \ - JSON_HEDLEY_HAS_BUILTIN(__builtin_constant_p) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,19) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(4,1,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \ - JSON_HEDLEY_TI_CL6X_VERSION_CHECK(6,1,0) || \ - (JSON_HEDLEY_SUNPRO_VERSION_CHECK(5,10,0) && !defined(__cplusplus)) || \ - JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ - JSON_HEDLEY_MCST_LCC_VERSION_CHECK(1,25,10) - #define JSON_HEDLEY_IS_CONSTANT(expr) __builtin_constant_p(expr) -#endif -#if !defined(__cplusplus) -# if \ - JSON_HEDLEY_HAS_BUILTIN(__builtin_types_compatible_p) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(3,4,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(13,1,0) || \ - JSON_HEDLEY_CRAY_VERSION_CHECK(8,1,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(5,4,0) || \ - JSON_HEDLEY_TINYC_VERSION_CHECK(0,9,24) -#if defined(__INTPTR_TYPE__) - #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0)), int*) -#else - #include - #define JSON_HEDLEY_IS_CONSTEXPR_(expr) __builtin_types_compatible_p(__typeof__((1 ? (void*) ((intptr_t) ((expr) * 0)) : (int*) 0)), int*) -#endif -# elif \ - ( \ - defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) && \ - !defined(JSON_HEDLEY_SUNPRO_VERSION) && \ - !defined(JSON_HEDLEY_PGI_VERSION) && \ - !defined(JSON_HEDLEY_IAR_VERSION)) || \ - (JSON_HEDLEY_HAS_EXTENSION(c_generic_selections) && !defined(JSON_HEDLEY_IAR_VERSION)) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,9,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(17,0,0) || \ - JSON_HEDLEY_IBM_VERSION_CHECK(12,1,0) || \ - JSON_HEDLEY_ARM_VERSION_CHECK(5,3,0) -#if defined(__INTPTR_TYPE__) - #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((__INTPTR_TYPE__) ((expr) * 0)) : (int*) 0), int*: 1, void*: 0) -#else - #include - #define JSON_HEDLEY_IS_CONSTEXPR_(expr) _Generic((1 ? (void*) ((intptr_t) * 0) : (int*) 0), int*: 1, void*: 0) -#endif -# elif \ - defined(JSON_HEDLEY_GCC_VERSION) || \ - defined(JSON_HEDLEY_INTEL_VERSION) || \ - defined(JSON_HEDLEY_TINYC_VERSION) || \ - defined(JSON_HEDLEY_TI_ARMCL_VERSION) || \ - JSON_HEDLEY_TI_CL430_VERSION_CHECK(18,12,0) || \ - defined(JSON_HEDLEY_TI_CL2000_VERSION) || \ - defined(JSON_HEDLEY_TI_CL6X_VERSION) || \ - defined(JSON_HEDLEY_TI_CL7X_VERSION) || \ - defined(JSON_HEDLEY_TI_CLPRU_VERSION) || \ - defined(__clang__) -# define JSON_HEDLEY_IS_CONSTEXPR_(expr) ( \ - sizeof(void) != \ - sizeof(*( \ - 1 ? \ - ((void*) ((expr) * 0L) ) : \ -((struct { char v[sizeof(void) * 2]; } *) 1) \ - ) \ - ) \ - ) -# endif -#endif -#if defined(JSON_HEDLEY_IS_CONSTEXPR_) - #if !defined(JSON_HEDLEY_IS_CONSTANT) - #define JSON_HEDLEY_IS_CONSTANT(expr) JSON_HEDLEY_IS_CONSTEXPR_(expr) - #endif - #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (JSON_HEDLEY_IS_CONSTEXPR_(expr) ? (expr) : (-1)) -#else - #if !defined(JSON_HEDLEY_IS_CONSTANT) - #define JSON_HEDLEY_IS_CONSTANT(expr) (0) - #endif - #define JSON_HEDLEY_REQUIRE_CONSTEXPR(expr) (expr) -#endif - -#if defined(JSON_HEDLEY_BEGIN_C_DECLS) - #undef JSON_HEDLEY_BEGIN_C_DECLS -#endif -#if defined(JSON_HEDLEY_END_C_DECLS) - #undef JSON_HEDLEY_END_C_DECLS -#endif -#if defined(JSON_HEDLEY_C_DECL) - #undef JSON_HEDLEY_C_DECL -#endif -#if defined(__cplusplus) - #define JSON_HEDLEY_BEGIN_C_DECLS extern "C" { - #define JSON_HEDLEY_END_C_DECLS } - #define JSON_HEDLEY_C_DECL extern "C" -#else - #define JSON_HEDLEY_BEGIN_C_DECLS - #define JSON_HEDLEY_END_C_DECLS - #define JSON_HEDLEY_C_DECL -#endif - -#if defined(JSON_HEDLEY_STATIC_ASSERT) - #undef JSON_HEDLEY_STATIC_ASSERT -#endif -#if \ - !defined(__cplusplus) && ( \ - (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L)) || \ - (JSON_HEDLEY_HAS_FEATURE(c_static_assert) && !defined(JSON_HEDLEY_INTEL_CL_VERSION)) || \ - JSON_HEDLEY_GCC_VERSION_CHECK(6,0,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) || \ - defined(_Static_assert) \ - ) -# define JSON_HEDLEY_STATIC_ASSERT(expr, message) _Static_assert(expr, message) -#elif \ - (defined(__cplusplus) && (__cplusplus >= 201103L)) || \ - JSON_HEDLEY_MSVC_VERSION_CHECK(16,0,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) -# define JSON_HEDLEY_STATIC_ASSERT(expr, message) JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(static_assert(expr, message)) -#else -# define JSON_HEDLEY_STATIC_ASSERT(expr, message) -#endif - -#if defined(JSON_HEDLEY_NULL) - #undef JSON_HEDLEY_NULL -#endif -#if defined(__cplusplus) - #if __cplusplus >= 201103L - #define JSON_HEDLEY_NULL JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_(nullptr) - #elif defined(NULL) - #define JSON_HEDLEY_NULL NULL - #else - #define JSON_HEDLEY_NULL JSON_HEDLEY_STATIC_CAST(void*, 0) - #endif -#elif defined(NULL) - #define JSON_HEDLEY_NULL NULL -#else - #define JSON_HEDLEY_NULL ((void*) 0) -#endif - -#if defined(JSON_HEDLEY_MESSAGE) - #undef JSON_HEDLEY_MESSAGE -#endif -#if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas") -# define JSON_HEDLEY_MESSAGE(msg) \ - JSON_HEDLEY_DIAGNOSTIC_PUSH \ - JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \ - JSON_HEDLEY_PRAGMA(message msg) \ - JSON_HEDLEY_DIAGNOSTIC_POP -#elif \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,4,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) -# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message msg) -#elif JSON_HEDLEY_CRAY_VERSION_CHECK(5,0,0) -# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(_CRI message msg) -#elif JSON_HEDLEY_IAR_VERSION_CHECK(8,0,0) -# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg)) -#elif JSON_HEDLEY_PELLES_VERSION_CHECK(2,0,0) -# define JSON_HEDLEY_MESSAGE(msg) JSON_HEDLEY_PRAGMA(message(msg)) -#else -# define JSON_HEDLEY_MESSAGE(msg) -#endif - -#if defined(JSON_HEDLEY_WARNING) - #undef JSON_HEDLEY_WARNING -#endif -#if JSON_HEDLEY_HAS_WARNING("-Wunknown-pragmas") -# define JSON_HEDLEY_WARNING(msg) \ - JSON_HEDLEY_DIAGNOSTIC_PUSH \ - JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS \ - JSON_HEDLEY_PRAGMA(clang warning msg) \ - JSON_HEDLEY_DIAGNOSTIC_POP -#elif \ - JSON_HEDLEY_GCC_VERSION_CHECK(4,8,0) || \ - JSON_HEDLEY_PGI_VERSION_CHECK(18,4,0) || \ - JSON_HEDLEY_INTEL_VERSION_CHECK(13,0,0) -# define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(GCC warning msg) -#elif \ - JSON_HEDLEY_MSVC_VERSION_CHECK(15,0,0) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) -# define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_PRAGMA(message(msg)) -#else -# define JSON_HEDLEY_WARNING(msg) JSON_HEDLEY_MESSAGE(msg) -#endif - -#if defined(JSON_HEDLEY_REQUIRE) - #undef JSON_HEDLEY_REQUIRE -#endif -#if defined(JSON_HEDLEY_REQUIRE_MSG) - #undef JSON_HEDLEY_REQUIRE_MSG -#endif -#if JSON_HEDLEY_HAS_ATTRIBUTE(diagnose_if) -# if JSON_HEDLEY_HAS_WARNING("-Wgcc-compat") -# define JSON_HEDLEY_REQUIRE(expr) \ - JSON_HEDLEY_DIAGNOSTIC_PUSH \ - _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \ - __attribute__((diagnose_if(!(expr), #expr, "error"))) \ - JSON_HEDLEY_DIAGNOSTIC_POP -# define JSON_HEDLEY_REQUIRE_MSG(expr,msg) \ - JSON_HEDLEY_DIAGNOSTIC_PUSH \ - _Pragma("clang diagnostic ignored \"-Wgcc-compat\"") \ - __attribute__((diagnose_if(!(expr), msg, "error"))) \ - JSON_HEDLEY_DIAGNOSTIC_POP -# else -# define JSON_HEDLEY_REQUIRE(expr) __attribute__((diagnose_if(!(expr), #expr, "error"))) -# define JSON_HEDLEY_REQUIRE_MSG(expr,msg) __attribute__((diagnose_if(!(expr), msg, "error"))) -# endif -#else -# define JSON_HEDLEY_REQUIRE(expr) -# define JSON_HEDLEY_REQUIRE_MSG(expr,msg) -#endif - -#if defined(JSON_HEDLEY_FLAGS) - #undef JSON_HEDLEY_FLAGS -#endif -#if JSON_HEDLEY_HAS_ATTRIBUTE(flag_enum) && (!defined(__cplusplus) || JSON_HEDLEY_HAS_WARNING("-Wbitfield-enum-conversion")) - #define JSON_HEDLEY_FLAGS __attribute__((__flag_enum__)) -#else - #define JSON_HEDLEY_FLAGS -#endif - -#if defined(JSON_HEDLEY_FLAGS_CAST) - #undef JSON_HEDLEY_FLAGS_CAST -#endif -#if JSON_HEDLEY_INTEL_VERSION_CHECK(19,0,0) -# define JSON_HEDLEY_FLAGS_CAST(T, expr) (__extension__ ({ \ - JSON_HEDLEY_DIAGNOSTIC_PUSH \ - _Pragma("warning(disable:188)") \ - ((T) (expr)); \ - JSON_HEDLEY_DIAGNOSTIC_POP \ - })) -#else -# define JSON_HEDLEY_FLAGS_CAST(T, expr) JSON_HEDLEY_STATIC_CAST(T, expr) -#endif - -#if defined(JSON_HEDLEY_EMPTY_BASES) - #undef JSON_HEDLEY_EMPTY_BASES -#endif -#if \ - (JSON_HEDLEY_MSVC_VERSION_CHECK(19,0,23918) && !JSON_HEDLEY_MSVC_VERSION_CHECK(20,0,0)) || \ - JSON_HEDLEY_INTEL_CL_VERSION_CHECK(2021,1,0) - #define JSON_HEDLEY_EMPTY_BASES __declspec(empty_bases) -#else - #define JSON_HEDLEY_EMPTY_BASES -#endif - -/* Remaining macros are deprecated. */ - -#if defined(JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK) - #undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK -#endif -#if defined(__clang__) - #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) (0) -#else - #define JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK(major,minor,patch) JSON_HEDLEY_GCC_VERSION_CHECK(major,minor,patch) -#endif - -#if defined(JSON_HEDLEY_CLANG_HAS_ATTRIBUTE) - #undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE -#endif -#define JSON_HEDLEY_CLANG_HAS_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_ATTRIBUTE(attribute) - -#if defined(JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE) - #undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE -#endif -#define JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_CPP_ATTRIBUTE(attribute) - -#if defined(JSON_HEDLEY_CLANG_HAS_BUILTIN) - #undef JSON_HEDLEY_CLANG_HAS_BUILTIN -#endif -#define JSON_HEDLEY_CLANG_HAS_BUILTIN(builtin) JSON_HEDLEY_HAS_BUILTIN(builtin) - -#if defined(JSON_HEDLEY_CLANG_HAS_FEATURE) - #undef JSON_HEDLEY_CLANG_HAS_FEATURE -#endif -#define JSON_HEDLEY_CLANG_HAS_FEATURE(feature) JSON_HEDLEY_HAS_FEATURE(feature) - -#if defined(JSON_HEDLEY_CLANG_HAS_EXTENSION) - #undef JSON_HEDLEY_CLANG_HAS_EXTENSION -#endif -#define JSON_HEDLEY_CLANG_HAS_EXTENSION(extension) JSON_HEDLEY_HAS_EXTENSION(extension) - -#if defined(JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE) - #undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE -#endif -#define JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE(attribute) JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE(attribute) - -#if defined(JSON_HEDLEY_CLANG_HAS_WARNING) - #undef JSON_HEDLEY_CLANG_HAS_WARNING -#endif -#define JSON_HEDLEY_CLANG_HAS_WARNING(warning) JSON_HEDLEY_HAS_WARNING(warning) - -#endif /* !defined(JSON_HEDLEY_VERSION) || (JSON_HEDLEY_VERSION < X) */ - -// #include - - -#include - -// #include - - -namespace nlohmann -{ -namespace detail -{ -template struct make_void -{ - using type = void; -}; -template using void_t = typename make_void::type; -} // namespace detail -} // namespace nlohmann - - -// https://en.cppreference.com/w/cpp/experimental/is_detected -namespace nlohmann -{ -namespace detail -{ -struct nonesuch -{ - nonesuch() = delete; - ~nonesuch() = delete; - nonesuch(nonesuch const&) = delete; - nonesuch(nonesuch const&&) = delete; - void operator=(nonesuch const&) = delete; - void operator=(nonesuch&&) = delete; -}; - -template class Op, - class... Args> -struct detector -{ - using value_t = std::false_type; - using type = Default; -}; - -template class Op, class... Args> -struct detector>, Op, Args...> -{ - using value_t = std::true_type; - using type = Op; -}; - -template class Op, class... Args> -using is_detected = typename detector::value_t; - -template class Op, class... Args> -struct is_detected_lazy : is_detected { }; - -template class Op, class... Args> -using detected_t = typename detector::type; - -template class Op, class... Args> -using detected_or = detector; - -template class Op, class... Args> -using detected_or_t = typename detected_or::type; - -template class Op, class... Args> -using is_detected_exact = std::is_same>; - -template class Op, class... Args> -using is_detected_convertible = - std::is_convertible, To>; -} // namespace detail -} // namespace nlohmann - - -// This file contains all internal macro definitions -// You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them - -// exclude unsupported compilers -#if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) - #if defined(__clang__) - #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400 - #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" - #endif - #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER)) - #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800 - #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" - #endif - #endif -#endif - -// C++ language standard detection -// if the user manually specified the used c++ version this is skipped -#if !defined(JSON_HAS_CPP_20) && !defined(JSON_HAS_CPP_17) && !defined(JSON_HAS_CPP_14) && !defined(JSON_HAS_CPP_11) - #if (defined(__cplusplus) && __cplusplus >= 202002L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 202002L) - #define JSON_HAS_CPP_20 - #define JSON_HAS_CPP_17 - #define JSON_HAS_CPP_14 - #elif (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 - #define JSON_HAS_CPP_17 - #define JSON_HAS_CPP_14 - #elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) - #define JSON_HAS_CPP_14 - #endif - // the cpp 11 flag is always specified because it is the minimal required version - #define JSON_HAS_CPP_11 -#endif - -// disable documentation warnings on clang -#if defined(__clang__) - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Wdocumentation" - #pragma clang diagnostic ignored "-Wdocumentation-unknown-command" -#endif - -// allow to disable exceptions -#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) - #define JSON_THROW(exception) throw exception - #define JSON_TRY try - #define JSON_CATCH(exception) catch(exception) - #define JSON_INTERNAL_CATCH(exception) catch(exception) -#else - #include - #define JSON_THROW(exception) std::abort() - #define JSON_TRY if(true) - #define JSON_CATCH(exception) if(false) - #define JSON_INTERNAL_CATCH(exception) if(false) -#endif - -// override exception macros -#if defined(JSON_THROW_USER) - #undef JSON_THROW - #define JSON_THROW JSON_THROW_USER -#endif -#if defined(JSON_TRY_USER) - #undef JSON_TRY - #define JSON_TRY JSON_TRY_USER -#endif -#if defined(JSON_CATCH_USER) - #undef JSON_CATCH - #define JSON_CATCH JSON_CATCH_USER - #undef JSON_INTERNAL_CATCH - #define JSON_INTERNAL_CATCH JSON_CATCH_USER -#endif -#if defined(JSON_INTERNAL_CATCH_USER) - #undef JSON_INTERNAL_CATCH - #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER -#endif - -// allow to override assert -#if !defined(JSON_ASSERT) - #include // assert - #define JSON_ASSERT(x) assert(x) -#endif - -// allow to access some private functions (needed by the test suite) -#if defined(JSON_TESTS_PRIVATE) - #define JSON_PRIVATE_UNLESS_TESTED public -#else - #define JSON_PRIVATE_UNLESS_TESTED private -#endif - -/*! -@brief macro to briefly define a mapping between an enum and JSON -@def NLOHMANN_JSON_SERIALIZE_ENUM -@since version 3.4.0 -*/ -#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \ - template \ - inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \ - { \ - static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ - static const std::pair m[] = __VA_ARGS__; \ - auto it = std::find_if(std::begin(m), std::end(m), \ - [e](const std::pair& ej_pair) -> bool \ - { \ - return ej_pair.first == e; \ - }); \ - j = ((it != std::end(m)) ? it : std::begin(m))->second; \ - } \ - template \ - inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \ - { \ - static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ - static const std::pair m[] = __VA_ARGS__; \ - auto it = std::find_if(std::begin(m), std::end(m), \ - [&j](const std::pair& ej_pair) -> bool \ - { \ - return ej_pair.second == j; \ - }); \ - e = ((it != std::end(m)) ? it : std::begin(m))->first; \ - } - -// Ugly macros to avoid uglier copy-paste when specializing basic_json. They -// may be removed in the future once the class is split. - -#define NLOHMANN_BASIC_JSON_TPL_DECLARATION \ - template class ObjectType, \ - template class ArrayType, \ - class StringType, class BooleanType, class NumberIntegerType, \ - class NumberUnsignedType, class NumberFloatType, \ - template class AllocatorType, \ - template class JSONSerializer, \ - class BinaryType> - -#define NLOHMANN_BASIC_JSON_TPL \ - basic_json - -// Macros to simplify conversion from/to types - -#define NLOHMANN_JSON_EXPAND( x ) x -#define NLOHMANN_JSON_GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, _32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, _47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, _62, _63, _64, NAME,...) NAME -#define NLOHMANN_JSON_PASTE(...) NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_GET_MACRO(__VA_ARGS__, \ - NLOHMANN_JSON_PASTE64, \ - NLOHMANN_JSON_PASTE63, \ - NLOHMANN_JSON_PASTE62, \ - NLOHMANN_JSON_PASTE61, \ - NLOHMANN_JSON_PASTE60, \ - NLOHMANN_JSON_PASTE59, \ - NLOHMANN_JSON_PASTE58, \ - NLOHMANN_JSON_PASTE57, \ - NLOHMANN_JSON_PASTE56, \ - NLOHMANN_JSON_PASTE55, \ - NLOHMANN_JSON_PASTE54, \ - NLOHMANN_JSON_PASTE53, \ - NLOHMANN_JSON_PASTE52, \ - NLOHMANN_JSON_PASTE51, \ - NLOHMANN_JSON_PASTE50, \ - NLOHMANN_JSON_PASTE49, \ - NLOHMANN_JSON_PASTE48, \ - NLOHMANN_JSON_PASTE47, \ - NLOHMANN_JSON_PASTE46, \ - NLOHMANN_JSON_PASTE45, \ - NLOHMANN_JSON_PASTE44, \ - NLOHMANN_JSON_PASTE43, \ - NLOHMANN_JSON_PASTE42, \ - NLOHMANN_JSON_PASTE41, \ - NLOHMANN_JSON_PASTE40, \ - NLOHMANN_JSON_PASTE39, \ - NLOHMANN_JSON_PASTE38, \ - NLOHMANN_JSON_PASTE37, \ - NLOHMANN_JSON_PASTE36, \ - NLOHMANN_JSON_PASTE35, \ - NLOHMANN_JSON_PASTE34, \ - NLOHMANN_JSON_PASTE33, \ - NLOHMANN_JSON_PASTE32, \ - NLOHMANN_JSON_PASTE31, \ - NLOHMANN_JSON_PASTE30, \ - NLOHMANN_JSON_PASTE29, \ - NLOHMANN_JSON_PASTE28, \ - NLOHMANN_JSON_PASTE27, \ - NLOHMANN_JSON_PASTE26, \ - NLOHMANN_JSON_PASTE25, \ - NLOHMANN_JSON_PASTE24, \ - NLOHMANN_JSON_PASTE23, \ - NLOHMANN_JSON_PASTE22, \ - NLOHMANN_JSON_PASTE21, \ - NLOHMANN_JSON_PASTE20, \ - NLOHMANN_JSON_PASTE19, \ - NLOHMANN_JSON_PASTE18, \ - NLOHMANN_JSON_PASTE17, \ - NLOHMANN_JSON_PASTE16, \ - NLOHMANN_JSON_PASTE15, \ - NLOHMANN_JSON_PASTE14, \ - NLOHMANN_JSON_PASTE13, \ - NLOHMANN_JSON_PASTE12, \ - NLOHMANN_JSON_PASTE11, \ - NLOHMANN_JSON_PASTE10, \ - NLOHMANN_JSON_PASTE9, \ - NLOHMANN_JSON_PASTE8, \ - NLOHMANN_JSON_PASTE7, \ - NLOHMANN_JSON_PASTE6, \ - NLOHMANN_JSON_PASTE5, \ - NLOHMANN_JSON_PASTE4, \ - NLOHMANN_JSON_PASTE3, \ - NLOHMANN_JSON_PASTE2, \ - NLOHMANN_JSON_PASTE1)(__VA_ARGS__)) -#define NLOHMANN_JSON_PASTE2(func, v1) func(v1) -#define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2) -#define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3) -#define NLOHMANN_JSON_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE4(func, v2, v3, v4) -#define NLOHMANN_JSON_PASTE6(func, v1, v2, v3, v4, v5) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE5(func, v2, v3, v4, v5) -#define NLOHMANN_JSON_PASTE7(func, v1, v2, v3, v4, v5, v6) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE6(func, v2, v3, v4, v5, v6) -#define NLOHMANN_JSON_PASTE8(func, v1, v2, v3, v4, v5, v6, v7) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE7(func, v2, v3, v4, v5, v6, v7) -#define NLOHMANN_JSON_PASTE9(func, v1, v2, v3, v4, v5, v6, v7, v8) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE8(func, v2, v3, v4, v5, v6, v7, v8) -#define NLOHMANN_JSON_PASTE10(func, v1, v2, v3, v4, v5, v6, v7, v8, v9) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE9(func, v2, v3, v4, v5, v6, v7, v8, v9) -#define NLOHMANN_JSON_PASTE11(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE10(func, v2, v3, v4, v5, v6, v7, v8, v9, v10) -#define NLOHMANN_JSON_PASTE12(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE11(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11) -#define NLOHMANN_JSON_PASTE13(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE12(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12) -#define NLOHMANN_JSON_PASTE14(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE13(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) -#define NLOHMANN_JSON_PASTE15(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE14(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14) -#define NLOHMANN_JSON_PASTE16(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE15(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) -#define NLOHMANN_JSON_PASTE17(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE16(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16) -#define NLOHMANN_JSON_PASTE18(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE17(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17) -#define NLOHMANN_JSON_PASTE19(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE18(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18) -#define NLOHMANN_JSON_PASTE20(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE19(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19) -#define NLOHMANN_JSON_PASTE21(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE20(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20) -#define NLOHMANN_JSON_PASTE22(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE21(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21) -#define NLOHMANN_JSON_PASTE23(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE22(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22) -#define NLOHMANN_JSON_PASTE24(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE23(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23) -#define NLOHMANN_JSON_PASTE25(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE24(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24) -#define NLOHMANN_JSON_PASTE26(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE25(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25) -#define NLOHMANN_JSON_PASTE27(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE26(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26) -#define NLOHMANN_JSON_PASTE28(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE27(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27) -#define NLOHMANN_JSON_PASTE29(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE28(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28) -#define NLOHMANN_JSON_PASTE30(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE29(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29) -#define NLOHMANN_JSON_PASTE31(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE30(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30) -#define NLOHMANN_JSON_PASTE32(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE31(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31) -#define NLOHMANN_JSON_PASTE33(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE32(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32) -#define NLOHMANN_JSON_PASTE34(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE33(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33) -#define NLOHMANN_JSON_PASTE35(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE34(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34) -#define NLOHMANN_JSON_PASTE36(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE35(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35) -#define NLOHMANN_JSON_PASTE37(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE36(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36) -#define NLOHMANN_JSON_PASTE38(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE37(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37) -#define NLOHMANN_JSON_PASTE39(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE38(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38) -#define NLOHMANN_JSON_PASTE40(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE39(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39) -#define NLOHMANN_JSON_PASTE41(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE40(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40) -#define NLOHMANN_JSON_PASTE42(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE41(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41) -#define NLOHMANN_JSON_PASTE43(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE42(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42) -#define NLOHMANN_JSON_PASTE44(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE43(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43) -#define NLOHMANN_JSON_PASTE45(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE44(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44) -#define NLOHMANN_JSON_PASTE46(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE45(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45) -#define NLOHMANN_JSON_PASTE47(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE46(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46) -#define NLOHMANN_JSON_PASTE48(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE47(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47) -#define NLOHMANN_JSON_PASTE49(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE48(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48) -#define NLOHMANN_JSON_PASTE50(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE49(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49) -#define NLOHMANN_JSON_PASTE51(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE50(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50) -#define NLOHMANN_JSON_PASTE52(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE51(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51) -#define NLOHMANN_JSON_PASTE53(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE52(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52) -#define NLOHMANN_JSON_PASTE54(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE53(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53) -#define NLOHMANN_JSON_PASTE55(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE54(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54) -#define NLOHMANN_JSON_PASTE56(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE55(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55) -#define NLOHMANN_JSON_PASTE57(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE56(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56) -#define NLOHMANN_JSON_PASTE58(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE57(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57) -#define NLOHMANN_JSON_PASTE59(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE58(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58) -#define NLOHMANN_JSON_PASTE60(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE59(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59) -#define NLOHMANN_JSON_PASTE61(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE60(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60) -#define NLOHMANN_JSON_PASTE62(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE61(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61) -#define NLOHMANN_JSON_PASTE63(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE62(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62) -#define NLOHMANN_JSON_PASTE64(func, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE63(func, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16, v17, v18, v19, v20, v21, v22, v23, v24, v25, v26, v27, v28, v29, v30, v31, v32, v33, v34, v35, v36, v37, v38, v39, v40, v41, v42, v43, v44, v45, v46, v47, v48, v49, v50, v51, v52, v53, v54, v55, v56, v57, v58, v59, v60, v61, v62, v63) - -#define NLOHMANN_JSON_TO(v1) nlohmann_json_j[#v1] = nlohmann_json_t.v1; -#define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1); - -/*! -@brief macro -@def NLOHMANN_DEFINE_TYPE_INTRUSIVE -@since version 3.9.0 -*/ -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \ - friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } - -/*! -@brief macro -@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE -@since version 3.9.0 -*/ -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \ - inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } - - -// inspired from https://stackoverflow.com/a/26745591 -// allows to call any std function as if (e.g. with begin): -// using std::begin; begin(x); -// -// it allows using the detected idiom to retrieve the return type -// of such an expression -#define NLOHMANN_CAN_CALL_STD_FUNC_IMPL(std_name) \ - namespace detail { \ - using std::std_name; \ - \ - template \ - using result_of_##std_name = decltype(std_name(std::declval()...)); \ - } \ - \ - namespace detail2 { \ - struct std_name##_tag \ - { \ - }; \ - \ - template \ - std_name##_tag std_name(T&&...); \ - \ - template \ - using result_of_##std_name = decltype(std_name(std::declval()...)); \ - \ - template \ - struct would_call_std_##std_name \ - { \ - static constexpr auto const value = ::nlohmann::detail:: \ - is_detected_exact::value; \ - }; \ - } /* namespace detail2 */ \ - \ - template \ - struct would_call_std_##std_name : detail2::would_call_std_##std_name \ - { \ - } - -#ifndef JSON_USE_IMPLICIT_CONVERSIONS - #define JSON_USE_IMPLICIT_CONVERSIONS 1 -#endif - -#if JSON_USE_IMPLICIT_CONVERSIONS - #define JSON_EXPLICIT -#else - #define JSON_EXPLICIT explicit -#endif - -#ifndef JSON_DIAGNOSTICS - #define JSON_DIAGNOSTICS 0 -#endif - - -namespace nlohmann -{ -namespace detail -{ - -/*! -@brief replace all occurrences of a substring by another string - -@param[in,out] s the string to manipulate; changed so that all - occurrences of @a f are replaced with @a t -@param[in] f the substring to replace with @a t -@param[in] t the string to replace @a f - -@pre The search string @a f must not be empty. **This precondition is -enforced with an assertion.** - -@since version 2.0.0 -*/ -inline void replace_substring(std::string& s, const std::string& f, - const std::string& t) -{ - JSON_ASSERT(!f.empty()); - for (auto pos = s.find(f); // find first occurrence of f - pos != std::string::npos; // make sure f was found - s.replace(pos, f.size(), t), // replace with t, and - pos = s.find(f, pos + t.size())) // find next occurrence of f - {} -} - -/*! - * @brief string escaping as described in RFC 6901 (Sect. 4) - * @param[in] s string to escape - * @return escaped string - * - * Note the order of escaping "~" to "~0" and "/" to "~1" is important. - */ -inline std::string escape(std::string s) -{ - replace_substring(s, "~", "~0"); - replace_substring(s, "/", "~1"); - return s; -} - -/*! - * @brief string unescaping as described in RFC 6901 (Sect. 4) - * @param[in] s string to unescape - * @return unescaped string - * - * Note the order of escaping "~1" to "/" and "~0" to "~" is important. - */ -static void unescape(std::string& s) -{ - replace_substring(s, "~1", "/"); - replace_substring(s, "~0", "~"); -} - -} // namespace detail -} // namespace nlohmann - -// #include - - -#include // size_t - -namespace nlohmann -{ -namespace detail -{ -/// struct to capture the start position of the current token -struct position_t -{ - /// the total number of characters read - std::size_t chars_read_total = 0; - /// the number of characters read in the current line - std::size_t chars_read_current_line = 0; - /// the number of lines read - std::size_t lines_read = 0; - - /// conversion to size_t to preserve SAX interface - constexpr operator size_t() const - { - return chars_read_total; - } -}; - -} // namespace detail -} // namespace nlohmann - -// #include - - -namespace nlohmann -{ -namespace detail -{ -//////////////// -// exceptions // -//////////////// - -/*! -@brief general exception of the @ref basic_json class - -This class is an extension of `std::exception` objects with a member @a id for -exception ids. It is used as the base class for all exceptions thrown by the -@ref basic_json class. This class can hence be used as "wildcard" to catch -exceptions. - -Subclasses: -- @ref parse_error for exceptions indicating a parse error -- @ref invalid_iterator for exceptions indicating errors with iterators -- @ref type_error for exceptions indicating executing a member function with - a wrong type -- @ref out_of_range for exceptions indicating access out of the defined range -- @ref other_error for exceptions indicating other library errors - -@internal -@note To have nothrow-copy-constructible exceptions, we internally use - `std::runtime_error` which can cope with arbitrary-length error messages. - Intermediate strings are built with static functions and then passed to - the actual constructor. -@endinternal - -@liveexample{The following code shows how arbitrary library exceptions can be -caught.,exception} - -@since version 3.0.0 -*/ -class exception : public std::exception -{ - public: - /// returns the explanatory string - const char* what() const noexcept override - { - return m.what(); - } - - /// the id of the exception - const int id; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes) - - protected: - JSON_HEDLEY_NON_NULL(3) - exception(int id_, const char* what_arg) : id(id_), m(what_arg) {} - - static std::string name(const std::string& ename, int id_) - { - return "[json.exception." + ename + "." + std::to_string(id_) + "] "; - } - - template - static std::string diagnostics(const BasicJsonType& leaf_element) - { -#if JSON_DIAGNOSTICS - std::vector tokens; - for (const auto* current = &leaf_element; current->m_parent != nullptr; current = current->m_parent) - { - switch (current->m_parent->type()) - { - case value_t::array: - { - for (std::size_t i = 0; i < current->m_parent->m_value.array->size(); ++i) - { - if (¤t->m_parent->m_value.array->operator[](i) == current) - { - tokens.emplace_back(std::to_string(i)); - break; - } - } - break; - } - - case value_t::object: - { - for (const auto& element : *current->m_parent->m_value.object) - { - if (&element.second == current) - { - tokens.emplace_back(element.first.c_str()); - break; - } - } - break; - } - - case value_t::null: // LCOV_EXCL_LINE - case value_t::string: // LCOV_EXCL_LINE - case value_t::boolean: // LCOV_EXCL_LINE - case value_t::number_integer: // LCOV_EXCL_LINE - case value_t::number_unsigned: // LCOV_EXCL_LINE - case value_t::number_float: // LCOV_EXCL_LINE - case value_t::binary: // LCOV_EXCL_LINE - case value_t::discarded: // LCOV_EXCL_LINE - default: // LCOV_EXCL_LINE - break; // LCOV_EXCL_LINE - } - } - - if (tokens.empty()) - { - return ""; - } - - return "(" + std::accumulate(tokens.rbegin(), tokens.rend(), std::string{}, - [](const std::string & a, const std::string & b) - { - return a + "/" + detail::escape(b); - }) + ") "; -#else - static_cast(leaf_element); - return ""; -#endif - } - - private: - /// an exception object as storage for error messages - std::runtime_error m; -}; - -/*! -@brief exception indicating a parse error - -This exception is thrown by the library when a parse error occurs. Parse errors -can occur during the deserialization of JSON text, CBOR, MessagePack, as well -as when using JSON Patch. - -Member @a byte holds the byte index of the last read character in the input -file. - -Exceptions have ids 1xx. - -name / id | example message | description ------------------------------- | --------------- | ------------------------- -json.exception.parse_error.101 | parse error at 2: unexpected end of input; expected string literal | This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member @a byte indicates the error position. -json.exception.parse_error.102 | parse error at 14: missing or wrong low surrogate | JSON uses the `\uxxxx` format to describe Unicode characters. Code points above above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point. -json.exception.parse_error.103 | parse error: code points above 0x10FFFF are invalid | Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid. -json.exception.parse_error.104 | parse error: JSON patch must be an array of objects | [RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects. -json.exception.parse_error.105 | parse error: operation must have string member 'op' | An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors. -json.exception.parse_error.106 | parse error: array index '01' must not begin with '0' | An array index in a JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)) may be `0` or any number without a leading `0`. -json.exception.parse_error.107 | parse error: JSON pointer must be empty or begin with '/' - was: 'foo' | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character. -json.exception.parse_error.108 | parse error: escape character '~' must be followed with '0' or '1' | In a JSON Pointer, only `~0` and `~1` are valid escape sequences. -json.exception.parse_error.109 | parse error: array index 'one' is not a number | A JSON Pointer array index must be a number. -json.exception.parse_error.110 | parse error at 1: cannot read 2 bytes from vector | When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read. -json.exception.parse_error.112 | parse error at 1: error reading CBOR; last byte: 0xF8 | Not all types of CBOR or MessagePack are supported. This exception occurs if an unsupported byte was read. -json.exception.parse_error.113 | parse error at 2: expected a CBOR string; last byte: 0x98 | While parsing a map key, a value that is not a string has been read. -json.exception.parse_error.114 | parse error: Unsupported BSON record type 0x0F | The parsing of the corresponding BSON record type is not implemented (yet). -json.exception.parse_error.115 | parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1A | A UBJSON high-precision number could not be parsed. - -@note For an input with n bytes, 1 is the index of the first character and n+1 - is the index of the terminating null byte or the end of file. This also - holds true when reading a byte vector (CBOR or MessagePack). - -@liveexample{The following code shows how a `parse_error` exception can be -caught.,parse_error} - -@sa - @ref exception for the base class of the library exceptions -@sa - @ref invalid_iterator for exceptions indicating errors with iterators -@sa - @ref type_error for exceptions indicating executing a member function with - a wrong type -@sa - @ref out_of_range for exceptions indicating access out of the defined range -@sa - @ref other_error for exceptions indicating other library errors - -@since version 3.0.0 -*/ -class parse_error : public exception -{ - public: - /*! - @brief create a parse error exception - @param[in] id_ the id of the exception - @param[in] pos the position where the error occurred (or with - chars_read_total=0 if the position cannot be - determined) - @param[in] what_arg the explanatory string - @return parse_error object - */ - template - static parse_error create(int id_, const position_t& pos, const std::string& what_arg, const BasicJsonType& context) - { - std::string w = exception::name("parse_error", id_) + "parse error" + - position_string(pos) + ": " + exception::diagnostics(context) + what_arg; - return parse_error(id_, pos.chars_read_total, w.c_str()); - } - - template - static parse_error create(int id_, std::size_t byte_, const std::string& what_arg, const BasicJsonType& context) - { - std::string w = exception::name("parse_error", id_) + "parse error" + - (byte_ != 0 ? (" at byte " + std::to_string(byte_)) : "") + - ": " + exception::diagnostics(context) + what_arg; - return parse_error(id_, byte_, w.c_str()); - } - - /*! - @brief byte index of the parse error - - The byte index of the last read character in the input file. - - @note For an input with n bytes, 1 is the index of the first character and - n+1 is the index of the terminating null byte or the end of file. - This also holds true when reading a byte vector (CBOR or MessagePack). - */ - const std::size_t byte; - - private: - parse_error(int id_, std::size_t byte_, const char* what_arg) - : exception(id_, what_arg), byte(byte_) {} - - static std::string position_string(const position_t& pos) - { - return " at line " + std::to_string(pos.lines_read + 1) + - ", column " + std::to_string(pos.chars_read_current_line); - } -}; - -/*! -@brief exception indicating errors with iterators - -This exception is thrown if iterators passed to a library function do not match -the expected semantics. - -Exceptions have ids 2xx. - -name / id | example message | description ------------------------------------ | --------------- | ------------------------- -json.exception.invalid_iterator.201 | iterators are not compatible | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid. -json.exception.invalid_iterator.202 | iterator does not fit current value | In an erase or insert function, the passed iterator @a pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion. -json.exception.invalid_iterator.203 | iterators do not fit current value | Either iterator passed to function @ref erase(IteratorType first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from. -json.exception.invalid_iterator.204 | iterators out of range | When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (@ref begin(), @ref end()), because this is the only way the single stored value is expressed. All other ranges are invalid. -json.exception.invalid_iterator.205 | iterator out of range | When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the @ref begin() iterator, because it is the only way to address the stored value. All other iterators are invalid. -json.exception.invalid_iterator.206 | cannot construct with iterators from null | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) belong to a JSON null value and hence to not define a valid range. -json.exception.invalid_iterator.207 | cannot use key() for non-object iterators | The key() member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key. -json.exception.invalid_iterator.208 | cannot use operator[] for object iterators | The operator[] to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. -json.exception.invalid_iterator.209 | cannot use offsets with object iterators | The offset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered. -json.exception.invalid_iterator.210 | iterators do not fit | The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid. -json.exception.invalid_iterator.211 | passed iterators may not belong to container | The iterator range passed to the insert function must not be a subrange of the container to insert to. -json.exception.invalid_iterator.212 | cannot compare iterators of different containers | When two iterators are compared, they must belong to the same container. -json.exception.invalid_iterator.213 | cannot compare order of object iterators | The order of object iterators cannot be compared, because JSON objects are unordered. -json.exception.invalid_iterator.214 | cannot get value | Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to @ref begin(). - -@liveexample{The following code shows how an `invalid_iterator` exception can be -caught.,invalid_iterator} - -@sa - @ref exception for the base class of the library exceptions -@sa - @ref parse_error for exceptions indicating a parse error -@sa - @ref type_error for exceptions indicating executing a member function with - a wrong type -@sa - @ref out_of_range for exceptions indicating access out of the defined range -@sa - @ref other_error for exceptions indicating other library errors - -@since version 3.0.0 -*/ -class invalid_iterator : public exception -{ - public: - template - static invalid_iterator create(int id_, const std::string& what_arg, const BasicJsonType& context) - { - std::string w = exception::name("invalid_iterator", id_) + exception::diagnostics(context) + what_arg; - return invalid_iterator(id_, w.c_str()); - } - - private: - JSON_HEDLEY_NON_NULL(3) - invalid_iterator(int id_, const char* what_arg) - : exception(id_, what_arg) {} -}; - -/*! -@brief exception indicating executing a member function with a wrong type - -This exception is thrown in case of a type error; that is, a library function is -executed on a JSON value whose type does not match the expected semantics. - -Exceptions have ids 3xx. - -name / id | example message | description ------------------------------ | --------------- | ------------------------- -json.exception.type_error.301 | cannot create object from initializer list | To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead. -json.exception.type_error.302 | type must be object, but is array | During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types. -json.exception.type_error.303 | incompatible ReferenceType for get_ref, actual type is object | To retrieve a reference to a value stored in a @ref basic_json object with @ref get_ref, the type of the reference must match the value type. For instance, for a JSON array, the @a ReferenceType must be @ref array_t &. -json.exception.type_error.304 | cannot use at() with string | The @ref at() member functions can only be executed for certain JSON types. -json.exception.type_error.305 | cannot use operator[] with string | The @ref operator[] member functions can only be executed for certain JSON types. -json.exception.type_error.306 | cannot use value() with string | The @ref value() member functions can only be executed for certain JSON types. -json.exception.type_error.307 | cannot use erase() with string | The @ref erase() member functions can only be executed for certain JSON types. -json.exception.type_error.308 | cannot use push_back() with string | The @ref push_back() and @ref operator+= member functions can only be executed for certain JSON types. -json.exception.type_error.309 | cannot use insert() with | The @ref insert() member functions can only be executed for certain JSON types. -json.exception.type_error.310 | cannot use swap() with number | The @ref swap() member functions can only be executed for certain JSON types. -json.exception.type_error.311 | cannot use emplace_back() with string | The @ref emplace_back() member function can only be executed for certain JSON types. -json.exception.type_error.312 | cannot use update() with string | The @ref update() member functions can only be executed for certain JSON types. -json.exception.type_error.313 | invalid value to unflatten | The @ref unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined. -json.exception.type_error.314 | only objects can be unflattened | The @ref unflatten function only works for an object whose keys are JSON Pointers. -json.exception.type_error.315 | values in object must be primitive | The @ref unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive. -json.exception.type_error.316 | invalid UTF-8 byte at index 10: 0x7E | The @ref dump function only works with UTF-8 encoded strings; that is, if you assign a `std::string` to a JSON value, make sure it is UTF-8 encoded. | -json.exception.type_error.317 | JSON value cannot be serialized to requested format | The dynamic type of the object cannot be represented in the requested serialization format (e.g. a raw `true` or `null` JSON object cannot be serialized to BSON) | - -@liveexample{The following code shows how a `type_error` exception can be -caught.,type_error} - -@sa - @ref exception for the base class of the library exceptions -@sa - @ref parse_error for exceptions indicating a parse error -@sa - @ref invalid_iterator for exceptions indicating errors with iterators -@sa - @ref out_of_range for exceptions indicating access out of the defined range -@sa - @ref other_error for exceptions indicating other library errors - -@since version 3.0.0 -*/ -class type_error : public exception -{ - public: - template - static type_error create(int id_, const std::string& what_arg, const BasicJsonType& context) - { - std::string w = exception::name("type_error", id_) + exception::diagnostics(context) + what_arg; - return type_error(id_, w.c_str()); - } - - private: - JSON_HEDLEY_NON_NULL(3) - type_error(int id_, const char* what_arg) : exception(id_, what_arg) {} -}; - -/*! -@brief exception indicating access out of the defined range - -This exception is thrown in case a library function is called on an input -parameter that exceeds the expected range, for instance in case of array -indices or nonexisting object keys. - -Exceptions have ids 4xx. - -name / id | example message | description -------------------------------- | --------------- | ------------------------- -json.exception.out_of_range.401 | array index 3 is out of range | The provided array index @a i is larger than @a size-1. -json.exception.out_of_range.402 | array index '-' (3) is out of range | The special array index `-` in a JSON Pointer never describes a valid element of the array, but the index past the end. That is, it can only be used to add elements at this position, but not to read it. -json.exception.out_of_range.403 | key 'foo' not found | The provided key was not found in the JSON object. -json.exception.out_of_range.404 | unresolved reference token 'foo' | A reference token in a JSON Pointer could not be resolved. -json.exception.out_of_range.405 | JSON pointer has no parent | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value. -json.exception.out_of_range.406 | number overflow parsing '10E1000' | A parsed number could not be stored as without changing it to NaN or INF. -json.exception.out_of_range.407 | number overflow serializing '9223372036854775808' | UBJSON and BSON only support integer numbers up to 9223372036854775807. (until version 3.8.0) | -json.exception.out_of_range.408 | excessive array size: 8658170730974374167 | The size (following `#`) of an UBJSON array or object exceeds the maximal capacity. | -json.exception.out_of_range.409 | BSON key cannot contain code point U+0000 (at byte 2) | Key identifiers to be serialized to BSON cannot contain code point U+0000, since the key is stored as zero-terminated c-string | - -@liveexample{The following code shows how an `out_of_range` exception can be -caught.,out_of_range} - -@sa - @ref exception for the base class of the library exceptions -@sa - @ref parse_error for exceptions indicating a parse error -@sa - @ref invalid_iterator for exceptions indicating errors with iterators -@sa - @ref type_error for exceptions indicating executing a member function with - a wrong type -@sa - @ref other_error for exceptions indicating other library errors - -@since version 3.0.0 -*/ -class out_of_range : public exception -{ - public: - template - static out_of_range create(int id_, const std::string& what_arg, const BasicJsonType& context) - { - std::string w = exception::name("out_of_range", id_) + exception::diagnostics(context) + what_arg; - return out_of_range(id_, w.c_str()); - } - - private: - JSON_HEDLEY_NON_NULL(3) - out_of_range(int id_, const char* what_arg) : exception(id_, what_arg) {} -}; - -/*! -@brief exception indicating other library errors - -This exception is thrown in case of errors that cannot be classified with the -other exception types. - -Exceptions have ids 5xx. - -name / id | example message | description ------------------------------- | --------------- | ------------------------- -json.exception.other_error.501 | unsuccessful: {"op":"test","path":"/baz", "value":"bar"} | A JSON Patch operation 'test' failed. The unsuccessful operation is also printed. - -@sa - @ref exception for the base class of the library exceptions -@sa - @ref parse_error for exceptions indicating a parse error -@sa - @ref invalid_iterator for exceptions indicating errors with iterators -@sa - @ref type_error for exceptions indicating executing a member function with - a wrong type -@sa - @ref out_of_range for exceptions indicating access out of the defined range - -@liveexample{The following code shows how an `other_error` exception can be -caught.,other_error} - -@since version 3.0.0 -*/ -class other_error : public exception -{ - public: - template - static other_error create(int id_, const std::string& what_arg, const BasicJsonType& context) - { - std::string w = exception::name("other_error", id_) + exception::diagnostics(context) + what_arg; - return other_error(id_, w.c_str()); - } - - private: - JSON_HEDLEY_NON_NULL(3) - other_error(int id_, const char* what_arg) : exception(id_, what_arg) {} -}; -} // namespace detail -} // namespace nlohmann - -// #include - -// #include - - -#include // size_t -#include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type -#include // index_sequence, make_index_sequence, index_sequence_for - -// #include - - -namespace nlohmann -{ -namespace detail -{ - -template -using uncvref_t = typename std::remove_cv::type>::type; - -#ifdef JSON_HAS_CPP_14 - -// the following utilities are natively available in C++14 -using std::enable_if_t; -using std::index_sequence; -using std::make_index_sequence; -using std::index_sequence_for; - -#else - -// alias templates to reduce boilerplate -template -using enable_if_t = typename std::enable_if::type; - -// The following code is taken from https://github.com/abseil/abseil-cpp/blob/10cb35e459f5ecca5b2ff107635da0bfa41011b4/absl/utility/utility.h -// which is part of Google Abseil (https://github.com/abseil/abseil-cpp), licensed under the Apache License 2.0. - -//// START OF CODE FROM GOOGLE ABSEIL - -// integer_sequence -// -// Class template representing a compile-time integer sequence. An instantiation -// of `integer_sequence` has a sequence of integers encoded in its -// type through its template arguments (which is a common need when -// working with C++11 variadic templates). `absl::integer_sequence` is designed -// to be a drop-in replacement for C++14's `std::integer_sequence`. -// -// Example: -// -// template< class T, T... Ints > -// void user_function(integer_sequence); -// -// int main() -// { -// // user_function's `T` will be deduced to `int` and `Ints...` -// // will be deduced to `0, 1, 2, 3, 4`. -// user_function(make_integer_sequence()); -// } -template -struct integer_sequence -{ - using value_type = T; - static constexpr std::size_t size() noexcept - { - return sizeof...(Ints); - } -}; - -// index_sequence -// -// A helper template for an `integer_sequence` of `size_t`, -// `absl::index_sequence` is designed to be a drop-in replacement for C++14's -// `std::index_sequence`. -template -using index_sequence = integer_sequence; - -namespace utility_internal -{ - -template -struct Extend; - -// Note that SeqSize == sizeof...(Ints). It's passed explicitly for efficiency. -template -struct Extend, SeqSize, 0> -{ - using type = integer_sequence < T, Ints..., (Ints + SeqSize)... >; -}; - -template -struct Extend, SeqSize, 1> -{ - using type = integer_sequence < T, Ints..., (Ints + SeqSize)..., 2 * SeqSize >; -}; - -// Recursion helper for 'make_integer_sequence'. -// 'Gen::type' is an alias for 'integer_sequence'. -template -struct Gen -{ - using type = - typename Extend < typename Gen < T, N / 2 >::type, N / 2, N % 2 >::type; -}; - -template -struct Gen -{ - using type = integer_sequence; -}; - -} // namespace utility_internal - -// Compile-time sequences of integers - -// make_integer_sequence -// -// This template alias is equivalent to -// `integer_sequence`, and is designed to be a drop-in -// replacement for C++14's `std::make_integer_sequence`. -template -using make_integer_sequence = typename utility_internal::Gen::type; - -// make_index_sequence -// -// This template alias is equivalent to `index_sequence<0, 1, ..., N-1>`, -// and is designed to be a drop-in replacement for C++14's -// `std::make_index_sequence`. -template -using make_index_sequence = make_integer_sequence; - -// index_sequence_for -// -// Converts a typename pack into an index sequence of the same length, and -// is designed to be a drop-in replacement for C++14's -// `std::index_sequence_for()` -template -using index_sequence_for = make_index_sequence; - -//// END OF CODE FROM GOOGLE ABSEIL - -#endif - -// dispatch utility (taken from ranges-v3) -template struct priority_tag : priority_tag < N - 1 > {}; -template<> struct priority_tag<0> {}; - -// taken from ranges-v3 -template -struct static_const -{ - static constexpr T value{}; -}; - -template -constexpr T static_const::value; - -} // namespace detail -} // namespace nlohmann - -// #include - - -namespace nlohmann -{ -namespace detail -{ -// dispatching helper struct -template struct identity_tag {}; -} // namespace detail -} // namespace nlohmann - -// #include - - -#include // numeric_limits -#include // false_type, is_constructible, is_integral, is_same, true_type -#include // declval -#include // tuple - -// #include - - -// #include - - -#include // random_access_iterator_tag - -// #include - -// #include - - -namespace nlohmann -{ -namespace detail -{ -template -struct iterator_types {}; - -template -struct iterator_types < - It, - void_t> -{ - using difference_type = typename It::difference_type; - using value_type = typename It::value_type; - using pointer = typename It::pointer; - using reference = typename It::reference; - using iterator_category = typename It::iterator_category; -}; - -// This is required as some compilers implement std::iterator_traits in a way that -// doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341. -template -struct iterator_traits -{ -}; - -template -struct iterator_traits < T, enable_if_t < !std::is_pointer::value >> - : iterator_types -{ -}; - -template -struct iterator_traits::value>> -{ - using iterator_category = std::random_access_iterator_tag; - using value_type = T; - using difference_type = ptrdiff_t; - using pointer = T*; - using reference = T&; -}; -} // namespace detail -} // namespace nlohmann - -// #include - - -// #include - - -namespace nlohmann -{ -NLOHMANN_CAN_CALL_STD_FUNC_IMPL(begin); -} // namespace nlohmann - -// #include - - -// #include - - -namespace nlohmann -{ -NLOHMANN_CAN_CALL_STD_FUNC_IMPL(end); -} // namespace nlohmann - -// #include - -// #include - -// #include -#ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_ -#define INCLUDE_NLOHMANN_JSON_FWD_HPP_ - -#include // int64_t, uint64_t -#include // map -#include // allocator -#include // string -#include // vector - -/*! -@brief namespace for Niels Lohmann -@see https://github.com/nlohmann -@since version 1.0.0 -*/ -namespace nlohmann -{ -/*! -@brief default JSONSerializer template argument - -This serializer ignores the template arguments and uses ADL -([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl)) -for serialization. -*/ -template -struct adl_serializer; - -template class ObjectType = - std::map, - template class ArrayType = std::vector, - class StringType = std::string, class BooleanType = bool, - class NumberIntegerType = std::int64_t, - class NumberUnsignedType = std::uint64_t, - class NumberFloatType = double, - template class AllocatorType = std::allocator, - template class JSONSerializer = - adl_serializer, - class BinaryType = std::vector> -class basic_json; - -/*! -@brief JSON Pointer - -A JSON pointer defines a string syntax for identifying a specific value -within a JSON document. It can be used with functions `at` and -`operator[]`. Furthermore, JSON pointers are the base for JSON patches. - -@sa [RFC 6901](https://tools.ietf.org/html/rfc6901) - -@since version 2.0.0 -*/ -template -class json_pointer; - -/*! -@brief default JSON class - -This type is the default specialization of the @ref basic_json class which -uses the standard template types. - -@since version 1.0.0 -*/ -using json = basic_json<>; - -template -struct ordered_map; - -/*! -@brief ordered JSON class - -This type preserves the insertion order of object keys. - -@since version 3.9.0 -*/ -using ordered_json = basic_json; - -} // namespace nlohmann - -#endif // INCLUDE_NLOHMANN_JSON_FWD_HPP_ - - -namespace nlohmann -{ -/*! -@brief detail namespace with internal helper functions - -This namespace collects functions that should not be exposed, -implementations of some @ref basic_json methods, and meta-programming helpers. - -@since version 2.1.0 -*/ -namespace detail -{ -///////////// -// helpers // -///////////// - -// Note to maintainers: -// -// Every trait in this file expects a non CV-qualified type. -// The only exceptions are in the 'aliases for detected' section -// (i.e. those of the form: decltype(T::member_function(std::declval()))) -// -// In this case, T has to be properly CV-qualified to constraint the function arguments -// (e.g. to_json(BasicJsonType&, const T&)) - -template struct is_basic_json : std::false_type {}; - -NLOHMANN_BASIC_JSON_TPL_DECLARATION -struct is_basic_json : std::true_type {}; - -////////////////////// -// json_ref helpers // -////////////////////// - -template -class json_ref; - -template -struct is_json_ref : std::false_type {}; - -template -struct is_json_ref> : std::true_type {}; - -////////////////////////// -// aliases for detected // -////////////////////////// - -template -using mapped_type_t = typename T::mapped_type; - -template -using key_type_t = typename T::key_type; - -template -using value_type_t = typename T::value_type; - -template -using difference_type_t = typename T::difference_type; - -template -using pointer_t = typename T::pointer; - -template -using reference_t = typename T::reference; - -template -using iterator_category_t = typename T::iterator_category; - -template -using to_json_function = decltype(T::to_json(std::declval()...)); - -template -using from_json_function = decltype(T::from_json(std::declval()...)); - -template -using get_template_function = decltype(std::declval().template get()); - -// trait checking if JSONSerializer::from_json(json const&, udt&) exists -template -struct has_from_json : std::false_type {}; - -// trait checking if j.get is valid -// use this trait instead of std::is_constructible or std::is_convertible, -// both rely on, or make use of implicit conversions, and thus fail when T -// has several constructors/operator= (see https://github.com/nlohmann/json/issues/958) -template -struct is_getable -{ - static constexpr bool value = is_detected::value; -}; - -template -struct has_from_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> -{ - using serializer = typename BasicJsonType::template json_serializer; - - static constexpr bool value = - is_detected_exact::value; -}; - -// This trait checks if JSONSerializer::from_json(json const&) exists -// this overload is used for non-default-constructible user-defined-types -template -struct has_non_default_from_json : std::false_type {}; - -template -struct has_non_default_from_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> -{ - using serializer = typename BasicJsonType::template json_serializer; - - static constexpr bool value = - is_detected_exact::value; -}; - -// This trait checks if BasicJsonType::json_serializer::to_json exists -// Do not evaluate the trait when T is a basic_json type, to avoid template instantiation infinite recursion. -template -struct has_to_json : std::false_type {}; - -template -struct has_to_json < BasicJsonType, T, enable_if_t < !is_basic_json::value >> -{ - using serializer = typename BasicJsonType::template json_serializer; - - static constexpr bool value = - is_detected_exact::value; -}; - - -/////////////////// -// is_ functions // -/////////////////// - -// https://en.cppreference.com/w/cpp/types/conjunction -template struct conjunction : std::true_type { }; -template struct conjunction : B1 { }; -template -struct conjunction -: std::conditional, B1>::type {}; - -// https://en.cppreference.com/w/cpp/types/negation -template struct negation : std::integral_constant < bool, !B::value > { }; - -// Reimplementation of is_constructible and is_default_constructible, due to them being broken for -// std::pair and std::tuple until LWG 2367 fix (see https://cplusplus.github.io/LWG/lwg-defects.html#2367). -// This causes compile errors in e.g. clang 3.5 or gcc 4.9. -template -struct is_default_constructible : std::is_default_constructible {}; - -template -struct is_default_constructible> - : conjunction, is_default_constructible> {}; - -template -struct is_default_constructible> - : conjunction, is_default_constructible> {}; - -template -struct is_default_constructible> - : conjunction...> {}; - -template -struct is_default_constructible> - : conjunction...> {}; - - -template -struct is_constructible : std::is_constructible {}; - -template -struct is_constructible> : is_default_constructible> {}; - -template -struct is_constructible> : is_default_constructible> {}; - -template -struct is_constructible> : is_default_constructible> {}; - -template -struct is_constructible> : is_default_constructible> {}; - - -template -struct is_iterator_traits : std::false_type {}; - -template -struct is_iterator_traits> -{ - private: - using traits = iterator_traits; - - public: - static constexpr auto value = - is_detected::value && - is_detected::value && - is_detected::value && - is_detected::value && - is_detected::value; -}; - -template -struct is_range -{ - private: - using t_ref = typename std::add_lvalue_reference::type; - - using iterator = detected_t; - using sentinel = detected_t; - - // to be 100% correct, it should use https://en.cppreference.com/w/cpp/iterator/input_or_output_iterator - // and https://en.cppreference.com/w/cpp/iterator/sentinel_for - // but reimplementing these would be too much work, as a lot of other concepts are used underneath - static constexpr auto is_iterator_begin = - is_iterator_traits>::value; - - public: - static constexpr bool value = !std::is_same::value && !std::is_same::value && is_iterator_begin; -}; - -template -using iterator_t = enable_if_t::value, result_of_begin())>>; - -template -using range_value_t = value_type_t>>; - -// The following implementation of is_complete_type is taken from -// https://blogs.msdn.microsoft.com/vcblog/2015/12/02/partial-support-for-expression-sfinae-in-vs-2015-update-1/ -// and is written by Xiang Fan who agreed to using it in this library. - -template -struct is_complete_type : std::false_type {}; - -template -struct is_complete_type : std::true_type {}; - -template -struct is_compatible_object_type_impl : std::false_type {}; - -template -struct is_compatible_object_type_impl < - BasicJsonType, CompatibleObjectType, - enable_if_t < is_detected::value&& - is_detected::value >> -{ - using object_t = typename BasicJsonType::object_t; - - // macOS's is_constructible does not play well with nonesuch... - static constexpr bool value = - is_constructible::value && - is_constructible::value; -}; - -template -struct is_compatible_object_type - : is_compatible_object_type_impl {}; - -template -struct is_constructible_object_type_impl : std::false_type {}; - -template -struct is_constructible_object_type_impl < - BasicJsonType, ConstructibleObjectType, - enable_if_t < is_detected::value&& - is_detected::value >> -{ - using object_t = typename BasicJsonType::object_t; - - static constexpr bool value = - (is_default_constructible::value && - (std::is_move_assignable::value || - std::is_copy_assignable::value) && - (is_constructible::value && - std::is_same < - typename object_t::mapped_type, - typename ConstructibleObjectType::mapped_type >::value)) || - (has_from_json::value || - has_non_default_from_json < - BasicJsonType, - typename ConstructibleObjectType::mapped_type >::value); -}; - -template -struct is_constructible_object_type - : is_constructible_object_type_impl {}; - -template -struct is_compatible_string_type -{ - static constexpr auto value = - is_constructible::value; -}; - -template -struct is_constructible_string_type -{ - static constexpr auto value = - is_constructible::value; -}; - -template -struct is_compatible_array_type_impl : std::false_type {}; - -template -struct is_compatible_array_type_impl < - BasicJsonType, CompatibleArrayType, - enable_if_t < - is_detected::value&& - is_iterator_traits>>::value&& -// special case for types like std::filesystem::path whose iterator's value_type are themselves -// c.f. https://github.com/nlohmann/json/pull/3073 - !std::is_same>::value >> -{ - static constexpr bool value = - is_constructible>::value; -}; - -template -struct is_compatible_array_type - : is_compatible_array_type_impl {}; - -template -struct is_constructible_array_type_impl : std::false_type {}; - -template -struct is_constructible_array_type_impl < - BasicJsonType, ConstructibleArrayType, - enable_if_t::value >> - : std::true_type {}; - -template -struct is_constructible_array_type_impl < - BasicJsonType, ConstructibleArrayType, - enable_if_t < !std::is_same::value&& - !is_compatible_string_type::value&& - is_default_constructible::value&& -(std::is_move_assignable::value || - std::is_copy_assignable::value)&& -is_detected::value&& -is_iterator_traits>>::value&& -is_detected::value&& -// special case for types like std::filesystem::path whose iterator's value_type are themselves -// c.f. https://github.com/nlohmann/json/pull/3073 -!std::is_same>::value&& - is_complete_type < - detected_t>::value >> -{ - using value_type = range_value_t; - - static constexpr bool value = - std::is_same::value || - has_from_json::value || - has_non_default_from_json < - BasicJsonType, - value_type >::value; -}; - -template -struct is_constructible_array_type - : is_constructible_array_type_impl {}; - -template -struct is_compatible_integer_type_impl : std::false_type {}; - -template -struct is_compatible_integer_type_impl < - RealIntegerType, CompatibleNumberIntegerType, - enable_if_t < std::is_integral::value&& - std::is_integral::value&& - !std::is_same::value >> -{ - // is there an assert somewhere on overflows? - using RealLimits = std::numeric_limits; - using CompatibleLimits = std::numeric_limits; - - static constexpr auto value = - is_constructible::value && - CompatibleLimits::is_integer && - RealLimits::is_signed == CompatibleLimits::is_signed; -}; - -template -struct is_compatible_integer_type - : is_compatible_integer_type_impl {}; - -template -struct is_compatible_type_impl: std::false_type {}; - -template -struct is_compatible_type_impl < - BasicJsonType, CompatibleType, - enable_if_t::value >> -{ - static constexpr bool value = - has_to_json::value; -}; - -template -struct is_compatible_type - : is_compatible_type_impl {}; - -template -struct is_constructible_tuple : std::false_type {}; - -template -struct is_constructible_tuple> : conjunction...> {}; - -// a naive helper to check if a type is an ordered_map (exploits the fact that -// ordered_map inherits capacity() from std::vector) -template -struct is_ordered_map -{ - using one = char; - - struct two - { - char x[2]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - }; - - template static one test( decltype(&C::capacity) ) ; - template static two test(...); - - enum { value = sizeof(test(nullptr)) == sizeof(char) }; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) -}; - -// to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324) -template < typename T, typename U, enable_if_t < !std::is_same::value, int > = 0 > -T conditional_static_cast(U value) -{ - return static_cast(value); -} - -template::value, int> = 0> -T conditional_static_cast(U value) -{ - return value; -} - -} // namespace detail -} // namespace nlohmann - -// #include - - -#ifdef JSON_HAS_CPP_17 - #include -#endif - -namespace nlohmann -{ -namespace detail -{ -template -void from_json(const BasicJsonType& j, typename std::nullptr_t& n) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_null())) - { - JSON_THROW(type_error::create(302, "type must be null, but is " + std::string(j.type_name()), j)); - } - n = nullptr; -} - -// overloads for basic_json template parameters -template < typename BasicJsonType, typename ArithmeticType, - enable_if_t < std::is_arithmetic::value&& - !std::is_same::value, - int > = 0 > -void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) -{ - switch (static_cast(j)) - { - case value_t::number_unsigned: - { - val = static_cast(*j.template get_ptr()); - break; - } - case value_t::number_integer: - { - val = static_cast(*j.template get_ptr()); - break; - } - case value_t::number_float: - { - val = static_cast(*j.template get_ptr()); - break; - } - - case value_t::null: - case value_t::object: - case value_t::array: - case value_t::string: - case value_t::boolean: - case value_t::binary: - case value_t::discarded: - default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j)); - } -} - -template -void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) - { - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(j.type_name()), j)); - } - b = *j.template get_ptr(); -} - -template -void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_string())) - { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j)); - } - s = *j.template get_ptr(); -} - -template < - typename BasicJsonType, typename ConstructibleStringType, - enable_if_t < - is_constructible_string_type::value&& - !std::is_same::value, - int > = 0 > -void from_json(const BasicJsonType& j, ConstructibleStringType& s) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_string())) - { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j)); - } - - s = *j.template get_ptr(); -} - -template -void from_json(const BasicJsonType& j, typename BasicJsonType::number_float_t& val) -{ - get_arithmetic_value(j, val); -} - -template -void from_json(const BasicJsonType& j, typename BasicJsonType::number_unsigned_t& val) -{ - get_arithmetic_value(j, val); -} - -template -void from_json(const BasicJsonType& j, typename BasicJsonType::number_integer_t& val) -{ - get_arithmetic_value(j, val); -} - -template::value, int> = 0> -void from_json(const BasicJsonType& j, EnumType& e) -{ - typename std::underlying_type::type val; - get_arithmetic_value(j, val); - e = static_cast(val); -} - -// forward_list doesn't have an insert method -template::value, int> = 0> -void from_json(const BasicJsonType& j, std::forward_list& l) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); - } - l.clear(); - std::transform(j.rbegin(), j.rend(), - std::front_inserter(l), [](const BasicJsonType & i) - { - return i.template get(); - }); -} - -// valarray doesn't have an insert method -template::value, int> = 0> -void from_json(const BasicJsonType& j, std::valarray& l) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); - } - l.resize(j.size()); - std::transform(j.begin(), j.end(), std::begin(l), - [](const BasicJsonType & elem) - { - return elem.template get(); - }); -} - -template -auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) --> decltype(j.template get(), void()) -{ - for (std::size_t i = 0; i < N; ++i) - { - arr[i] = j.at(i).template get(); - } -} - -template -void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/) -{ - arr = *j.template get_ptr(); -} - -template -auto from_json_array_impl(const BasicJsonType& j, std::array& arr, - priority_tag<2> /*unused*/) --> decltype(j.template get(), void()) -{ - for (std::size_t i = 0; i < N; ++i) - { - arr[i] = j.at(i).template get(); - } -} - -template::value, - int> = 0> -auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, priority_tag<1> /*unused*/) --> decltype( - arr.reserve(std::declval()), - j.template get(), - void()) -{ - using std::end; - - ConstructibleArrayType ret; - ret.reserve(j.size()); - std::transform(j.begin(), j.end(), - std::inserter(ret, end(ret)), [](const BasicJsonType & i) - { - // get() returns *this, this won't call a from_json - // method when value_type is BasicJsonType - return i.template get(); - }); - arr = std::move(ret); -} - -template::value, - int> = 0> -void from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, - priority_tag<0> /*unused*/) -{ - using std::end; - - ConstructibleArrayType ret; - std::transform( - j.begin(), j.end(), std::inserter(ret, end(ret)), - [](const BasicJsonType & i) - { - // get() returns *this, this won't call a from_json - // method when value_type is BasicJsonType - return i.template get(); - }); - arr = std::move(ret); -} - -template < typename BasicJsonType, typename ConstructibleArrayType, - enable_if_t < - is_constructible_array_type::value&& - !is_constructible_object_type::value&& - !is_constructible_string_type::value&& - !std::is_same::value&& - !is_basic_json::value, - int > = 0 > -auto from_json(const BasicJsonType& j, ConstructibleArrayType& arr) --> decltype(from_json_array_impl(j, arr, priority_tag<3> {}), -j.template get(), -void()) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); - } - - from_json_array_impl(j, arr, priority_tag<3> {}); -} - -template < typename BasicJsonType, typename T, std::size_t... Idx > -std::array from_json_inplace_array_impl(BasicJsonType&& j, - identity_tag> /*unused*/, index_sequence /*unused*/) -{ - return { { std::forward(j).at(Idx).template get()... } }; -} - -template < typename BasicJsonType, typename T, std::size_t N > -auto from_json(BasicJsonType&& j, identity_tag> tag) --> decltype(from_json_inplace_array_impl(std::forward(j), tag, make_index_sequence {})) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); - } - - return from_json_inplace_array_impl(std::forward(j), tag, make_index_sequence {}); -} - -template -void from_json(const BasicJsonType& j, typename BasicJsonType::binary_t& bin) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_binary())) - { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(j.type_name()), j)); - } - - bin = *j.template get_ptr(); -} - -template::value, int> = 0> -void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_object())) - { - JSON_THROW(type_error::create(302, "type must be object, but is " + std::string(j.type_name()), j)); - } - - ConstructibleObjectType ret; - const auto* inner_object = j.template get_ptr(); - using value_type = typename ConstructibleObjectType::value_type; - std::transform( - inner_object->begin(), inner_object->end(), - std::inserter(ret, ret.begin()), - [](typename BasicJsonType::object_t::value_type const & p) - { - return value_type(p.first, p.second.template get()); - }); - obj = std::move(ret); -} - -// overload for arithmetic types, not chosen for basic_json template arguments -// (BooleanType, etc..); note: Is it really necessary to provide explicit -// overloads for boolean_t etc. in case of a custom BooleanType which is not -// an arithmetic type? -template < typename BasicJsonType, typename ArithmeticType, - enable_if_t < - std::is_arithmetic::value&& - !std::is_same::value&& - !std::is_same::value&& - !std::is_same::value&& - !std::is_same::value, - int > = 0 > -void from_json(const BasicJsonType& j, ArithmeticType& val) -{ - switch (static_cast(j)) - { - case value_t::number_unsigned: - { - val = static_cast(*j.template get_ptr()); - break; - } - case value_t::number_integer: - { - val = static_cast(*j.template get_ptr()); - break; - } - case value_t::number_float: - { - val = static_cast(*j.template get_ptr()); - break; - } - case value_t::boolean: - { - val = static_cast(*j.template get_ptr()); - break; - } - - case value_t::null: - case value_t::object: - case value_t::array: - case value_t::string: - case value_t::binary: - case value_t::discarded: - default: - JSON_THROW(type_error::create(302, "type must be number, but is " + std::string(j.type_name()), j)); - } -} - -template -std::tuple from_json_tuple_impl_base(BasicJsonType&& j, index_sequence /*unused*/) -{ - return std::make_tuple(std::forward(j).at(Idx).template get()...); -} - -template < typename BasicJsonType, class A1, class A2 > -std::pair from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<0> /*unused*/) -{ - return {std::forward(j).at(0).template get(), - std::forward(j).at(1).template get()}; -} - -template -void from_json_tuple_impl(BasicJsonType&& j, std::pair& p, priority_tag<1> /*unused*/) -{ - p = from_json_tuple_impl(std::forward(j), identity_tag> {}, priority_tag<0> {}); -} - -template -std::tuple from_json_tuple_impl(BasicJsonType&& j, identity_tag> /*unused*/, priority_tag<2> /*unused*/) -{ - return from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); -} - -template -void from_json_tuple_impl(BasicJsonType&& j, std::tuple& t, priority_tag<3> /*unused*/) -{ - t = from_json_tuple_impl_base(std::forward(j), index_sequence_for {}); -} - -template -auto from_json(BasicJsonType&& j, TupleRelated&& t) --> decltype(from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<3> {})) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); - } - - return from_json_tuple_impl(std::forward(j), std::forward(t), priority_tag<3> {}); -} - -template < typename BasicJsonType, typename Key, typename Value, typename Compare, typename Allocator, - typename = enable_if_t < !std::is_constructible < - typename BasicJsonType::string_t, Key >::value >> -void from_json(const BasicJsonType& j, std::map& m) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); - } - m.clear(); - for (const auto& p : j) - { - if (JSON_HEDLEY_UNLIKELY(!p.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j)); - } - m.emplace(p.at(0).template get(), p.at(1).template get()); - } -} - -template < typename BasicJsonType, typename Key, typename Value, typename Hash, typename KeyEqual, typename Allocator, - typename = enable_if_t < !std::is_constructible < - typename BasicJsonType::string_t, Key >::value >> -void from_json(const BasicJsonType& j, std::unordered_map& m) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(j.type_name()), j)); - } - m.clear(); - for (const auto& p : j) - { - if (JSON_HEDLEY_UNLIKELY(!p.is_array())) - { - JSON_THROW(type_error::create(302, "type must be array, but is " + std::string(p.type_name()), j)); - } - m.emplace(p.at(0).template get(), p.at(1).template get()); - } -} - -#ifdef JSON_HAS_CPP_17 -template -void from_json(const BasicJsonType& j, std::filesystem::path& p) -{ - if (JSON_HEDLEY_UNLIKELY(!j.is_string())) - { - JSON_THROW(type_error::create(302, "type must be string, but is " + std::string(j.type_name()), j)); - } - p = *j.template get_ptr(); -} -#endif - -struct from_json_fn -{ - template - auto operator()(const BasicJsonType& j, T&& val) const - noexcept(noexcept(from_json(j, std::forward(val)))) - -> decltype(from_json(j, std::forward(val))) - { - return from_json(j, std::forward(val)); - } -}; -} // namespace detail - -/// namespace to hold default `from_json` function -/// to see why this is required: -/// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html -namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces) -{ -constexpr const auto& from_json = detail::static_const::value; // NOLINT(misc-definitions-in-headers) -} // namespace -} // namespace nlohmann - -// #include - - -#include // copy -#include // begin, end -#include // string -#include // tuple, get -#include // is_same, is_constructible, is_floating_point, is_enum, underlying_type -#include // move, forward, declval, pair -#include // valarray -#include // vector - -// #include - -// #include - - -#include // size_t -#include // input_iterator_tag -#include // string, to_string -#include // tuple_size, get, tuple_element -#include // move - -// #include - -// #include - - -namespace nlohmann -{ -namespace detail -{ -template -void int_to_string( string_type& target, std::size_t value ) -{ - // For ADL - using std::to_string; - target = to_string(value); -} -template class iteration_proxy_value -{ - public: - using difference_type = std::ptrdiff_t; - using value_type = iteration_proxy_value; - using pointer = value_type * ; - using reference = value_type & ; - using iterator_category = std::input_iterator_tag; - using string_type = typename std::remove_cv< typename std::remove_reference().key() ) >::type >::type; - - private: - /// the iterator - IteratorType anchor; - /// an index for arrays (used to create key names) - std::size_t array_index = 0; - /// last stringified array index - mutable std::size_t array_index_last = 0; - /// a string representation of the array index - mutable string_type array_index_str = "0"; - /// an empty string (to return a reference for primitive values) - const string_type empty_str{}; - - public: - explicit iteration_proxy_value(IteratorType it) noexcept - : anchor(std::move(it)) - {} - - /// dereference operator (needed for range-based for) - iteration_proxy_value& operator*() - { - return *this; - } - - /// increment operator (needed for range-based for) - iteration_proxy_value& operator++() - { - ++anchor; - ++array_index; - - return *this; - } - - /// equality operator (needed for InputIterator) - bool operator==(const iteration_proxy_value& o) const - { - return anchor == o.anchor; - } - - /// inequality operator (needed for range-based for) - bool operator!=(const iteration_proxy_value& o) const - { - return anchor != o.anchor; - } - - /// return key of the iterator - const string_type& key() const - { - JSON_ASSERT(anchor.m_object != nullptr); - - switch (anchor.m_object->type()) - { - // use integer array index as key - case value_t::array: - { - if (array_index != array_index_last) - { - int_to_string( array_index_str, array_index ); - array_index_last = array_index; - } - return array_index_str; - } - - // use key from the object - case value_t::object: - return anchor.key(); - - // use an empty key for all primitive types - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return empty_str; - } - } - - /// return value of the iterator - typename IteratorType::reference value() const - { - return anchor.value(); - } -}; - -/// proxy class for the items() function -template class iteration_proxy -{ - private: - /// the container to iterate - typename IteratorType::reference container; - - public: - /// construct iteration proxy from a container - explicit iteration_proxy(typename IteratorType::reference cont) noexcept - : container(cont) {} - - /// return iterator begin (needed for range-based for) - iteration_proxy_value begin() noexcept - { - return iteration_proxy_value(container.begin()); - } - - /// return iterator end (needed for range-based for) - iteration_proxy_value end() noexcept - { - return iteration_proxy_value(container.end()); - } -}; -// Structured Bindings Support -// For further reference see https://blog.tartanllama.xyz/structured-bindings/ -// And see https://github.com/nlohmann/json/pull/1391 -template = 0> -auto get(const nlohmann::detail::iteration_proxy_value& i) -> decltype(i.key()) -{ - return i.key(); -} -// Structured Bindings Support -// For further reference see https://blog.tartanllama.xyz/structured-bindings/ -// And see https://github.com/nlohmann/json/pull/1391 -template = 0> -auto get(const nlohmann::detail::iteration_proxy_value& i) -> decltype(i.value()) -{ - return i.value(); -} -} // namespace detail -} // namespace nlohmann - -// The Addition to the STD Namespace is required to add -// Structured Bindings Support to the iteration_proxy_value class -// For further reference see https://blog.tartanllama.xyz/structured-bindings/ -// And see https://github.com/nlohmann/json/pull/1391 -namespace std -{ -#if defined(__clang__) - // Fix: https://github.com/nlohmann/json/issues/1401 - #pragma clang diagnostic push - #pragma clang diagnostic ignored "-Wmismatched-tags" -#endif -template -class tuple_size<::nlohmann::detail::iteration_proxy_value> - : public std::integral_constant {}; - -template -class tuple_element> -{ - public: - using type = decltype( - get(std::declval < - ::nlohmann::detail::iteration_proxy_value> ())); -}; -#if defined(__clang__) - #pragma clang diagnostic pop -#endif -} // namespace std - -// #include - -// #include - -// #include - - -#ifdef JSON_HAS_CPP_17 - #include -#endif - -namespace nlohmann -{ -namespace detail -{ -////////////////// -// constructors // -////////////////// - -/* - * Note all external_constructor<>::construct functions need to call - * j.m_value.destroy(j.m_type) to avoid a memory leak in case j contains an - * allocated value (e.g., a string). See bug issue - * https://github.com/nlohmann/json/issues/2865 for more information. - */ - -template struct external_constructor; - -template<> -struct external_constructor -{ - template - static void construct(BasicJsonType& j, typename BasicJsonType::boolean_t b) noexcept - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::boolean; - j.m_value = b; - j.assert_invariant(); - } -}; - -template<> -struct external_constructor -{ - template - static void construct(BasicJsonType& j, const typename BasicJsonType::string_t& s) - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::string; - j.m_value = s; - j.assert_invariant(); - } - - template - static void construct(BasicJsonType& j, typename BasicJsonType::string_t&& s) - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::string; - j.m_value = std::move(s); - j.assert_invariant(); - } - - template < typename BasicJsonType, typename CompatibleStringType, - enable_if_t < !std::is_same::value, - int > = 0 > - static void construct(BasicJsonType& j, const CompatibleStringType& str) - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::string; - j.m_value.string = j.template create(str); - j.assert_invariant(); - } -}; - -template<> -struct external_constructor -{ - template - static void construct(BasicJsonType& j, const typename BasicJsonType::binary_t& b) - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::binary; - j.m_value = typename BasicJsonType::binary_t(b); - j.assert_invariant(); - } - - template - static void construct(BasicJsonType& j, typename BasicJsonType::binary_t&& b) - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::binary; - j.m_value = typename BasicJsonType::binary_t(std::move(b)); - j.assert_invariant(); - } -}; - -template<> -struct external_constructor -{ - template - static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::number_float; - j.m_value = val; - j.assert_invariant(); - } -}; - -template<> -struct external_constructor -{ - template - static void construct(BasicJsonType& j, typename BasicJsonType::number_unsigned_t val) noexcept - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::number_unsigned; - j.m_value = val; - j.assert_invariant(); - } -}; - -template<> -struct external_constructor -{ - template - static void construct(BasicJsonType& j, typename BasicJsonType::number_integer_t val) noexcept - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::number_integer; - j.m_value = val; - j.assert_invariant(); - } -}; - -template<> -struct external_constructor -{ - template - static void construct(BasicJsonType& j, const typename BasicJsonType::array_t& arr) - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::array; - j.m_value = arr; - j.set_parents(); - j.assert_invariant(); - } - - template - static void construct(BasicJsonType& j, typename BasicJsonType::array_t&& arr) - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::array; - j.m_value = std::move(arr); - j.set_parents(); - j.assert_invariant(); - } - - template < typename BasicJsonType, typename CompatibleArrayType, - enable_if_t < !std::is_same::value, - int > = 0 > - static void construct(BasicJsonType& j, const CompatibleArrayType& arr) - { - using std::begin; - using std::end; - - j.m_value.destroy(j.m_type); - j.m_type = value_t::array; - j.m_value.array = j.template create(begin(arr), end(arr)); - j.set_parents(); - j.assert_invariant(); - } - - template - static void construct(BasicJsonType& j, const std::vector& arr) - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::array; - j.m_value = value_t::array; - j.m_value.array->reserve(arr.size()); - for (const bool x : arr) - { - j.m_value.array->push_back(x); - j.set_parent(j.m_value.array->back()); - } - j.assert_invariant(); - } - - template::value, int> = 0> - static void construct(BasicJsonType& j, const std::valarray& arr) - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::array; - j.m_value = value_t::array; - j.m_value.array->resize(arr.size()); - if (arr.size() > 0) - { - std::copy(std::begin(arr), std::end(arr), j.m_value.array->begin()); - } - j.set_parents(); - j.assert_invariant(); - } -}; - -template<> -struct external_constructor -{ - template - static void construct(BasicJsonType& j, const typename BasicJsonType::object_t& obj) - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::object; - j.m_value = obj; - j.set_parents(); - j.assert_invariant(); - } - - template - static void construct(BasicJsonType& j, typename BasicJsonType::object_t&& obj) - { - j.m_value.destroy(j.m_type); - j.m_type = value_t::object; - j.m_value = std::move(obj); - j.set_parents(); - j.assert_invariant(); - } - - template < typename BasicJsonType, typename CompatibleObjectType, - enable_if_t < !std::is_same::value, int > = 0 > - static void construct(BasicJsonType& j, const CompatibleObjectType& obj) - { - using std::begin; - using std::end; - - j.m_value.destroy(j.m_type); - j.m_type = value_t::object; - j.m_value.object = j.template create(begin(obj), end(obj)); - j.set_parents(); - j.assert_invariant(); - } -}; - -///////////// -// to_json // -///////////// - -template::value, int> = 0> -void to_json(BasicJsonType& j, T b) noexcept -{ - external_constructor::construct(j, b); -} - -template::value, int> = 0> -void to_json(BasicJsonType& j, const CompatibleString& s) -{ - external_constructor::construct(j, s); -} - -template -void to_json(BasicJsonType& j, typename BasicJsonType::string_t&& s) -{ - external_constructor::construct(j, std::move(s)); -} - -template::value, int> = 0> -void to_json(BasicJsonType& j, FloatType val) noexcept -{ - external_constructor::construct(j, static_cast(val)); -} - -template::value, int> = 0> -void to_json(BasicJsonType& j, CompatibleNumberUnsignedType val) noexcept -{ - external_constructor::construct(j, static_cast(val)); -} - -template::value, int> = 0> -void to_json(BasicJsonType& j, CompatibleNumberIntegerType val) noexcept -{ - external_constructor::construct(j, static_cast(val)); -} - -template::value, int> = 0> -void to_json(BasicJsonType& j, EnumType e) noexcept -{ - using underlying_type = typename std::underlying_type::type; - external_constructor::construct(j, static_cast(e)); -} - -template -void to_json(BasicJsonType& j, const std::vector& e) -{ - external_constructor::construct(j, e); -} - -template < typename BasicJsonType, typename CompatibleArrayType, - enable_if_t < is_compatible_array_type::value&& - !is_compatible_object_type::value&& - !is_compatible_string_type::value&& - !std::is_same::value&& - !is_basic_json::value, - int > = 0 > -void to_json(BasicJsonType& j, const CompatibleArrayType& arr) -{ - external_constructor::construct(j, arr); -} - -template -void to_json(BasicJsonType& j, const typename BasicJsonType::binary_t& bin) -{ - external_constructor::construct(j, bin); -} - -template::value, int> = 0> -void to_json(BasicJsonType& j, const std::valarray& arr) -{ - external_constructor::construct(j, std::move(arr)); -} - -template -void to_json(BasicJsonType& j, typename BasicJsonType::array_t&& arr) -{ - external_constructor::construct(j, std::move(arr)); -} - -template < typename BasicJsonType, typename CompatibleObjectType, - enable_if_t < is_compatible_object_type::value&& !is_basic_json::value, int > = 0 > -void to_json(BasicJsonType& j, const CompatibleObjectType& obj) -{ - external_constructor::construct(j, obj); -} - -template -void to_json(BasicJsonType& j, typename BasicJsonType::object_t&& obj) -{ - external_constructor::construct(j, std::move(obj)); -} - -template < - typename BasicJsonType, typename T, std::size_t N, - enable_if_t < !std::is_constructible::value, // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - int > = 0 > -void to_json(BasicJsonType& j, const T(&arr)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) -{ - external_constructor::construct(j, arr); -} - -template < typename BasicJsonType, typename T1, typename T2, enable_if_t < std::is_constructible::value&& std::is_constructible::value, int > = 0 > -void to_json(BasicJsonType& j, const std::pair& p) -{ - j = { p.first, p.second }; -} - -// for https://github.com/nlohmann/json/pull/1134 -template>::value, int> = 0> -void to_json(BasicJsonType& j, const T& b) -{ - j = { {b.key(), b.value()} }; -} - -template -void to_json_tuple_impl(BasicJsonType& j, const Tuple& t, index_sequence /*unused*/) -{ - j = { std::get(t)... }; -} - -template::value, int > = 0> -void to_json(BasicJsonType& j, const T& t) -{ - to_json_tuple_impl(j, t, make_index_sequence::value> {}); -} - -#ifdef JSON_HAS_CPP_17 -template -void to_json(BasicJsonType& j, const std::filesystem::path& p) -{ - j = p.string(); -} -#endif - -struct to_json_fn -{ - template - auto operator()(BasicJsonType& j, T&& val) const noexcept(noexcept(to_json(j, std::forward(val)))) - -> decltype(to_json(j, std::forward(val)), void()) - { - return to_json(j, std::forward(val)); - } -}; -} // namespace detail - -/// namespace to hold default `to_json` function -/// to see why this is required: -/// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4381.html -namespace // NOLINT(cert-dcl59-cpp,fuchsia-header-anon-namespaces,google-build-namespaces) -{ -constexpr const auto& to_json = detail::static_const::value; // NOLINT(misc-definitions-in-headers) -} // namespace -} // namespace nlohmann - -// #include - -// #include - - -namespace nlohmann -{ - -template -struct adl_serializer -{ - /*! - @brief convert a JSON value to any value type - - This function is usually called by the `get()` function of the - @ref basic_json class (either explicit or via conversion operators). - - @note This function is chosen for default-constructible value types. - - @param[in] j JSON value to read from - @param[in,out] val value to write to - */ - template - static auto from_json(BasicJsonType && j, TargetType& val) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), val))) - -> decltype(::nlohmann::from_json(std::forward(j), val), void()) - { - ::nlohmann::from_json(std::forward(j), val); - } - - /*! - @brief convert a JSON value to any value type - - This function is usually called by the `get()` function of the - @ref basic_json class (either explicit or via conversion operators). - - @note This function is chosen for value types which are not default-constructible. - - @param[in] j JSON value to read from - - @return copy of the JSON value, converted to @a ValueType - */ - template - static auto from_json(BasicJsonType && j) noexcept( - noexcept(::nlohmann::from_json(std::forward(j), detail::identity_tag {}))) - -> decltype(::nlohmann::from_json(std::forward(j), detail::identity_tag {})) - { - return ::nlohmann::from_json(std::forward(j), detail::identity_tag {}); - } - - /*! - @brief convert any value type to a JSON value - - This function is usually called by the constructors of the @ref basic_json - class. - - @param[in,out] j JSON value to write to - @param[in] val value to read from - */ - template - static auto to_json(BasicJsonType& j, TargetType && val) noexcept( - noexcept(::nlohmann::to_json(j, std::forward(val)))) - -> decltype(::nlohmann::to_json(j, std::forward(val)), void()) - { - ::nlohmann::to_json(j, std::forward(val)); - } -}; -} // namespace nlohmann - -// #include - - -#include // uint8_t, uint64_t -#include // tie -#include // move - -namespace nlohmann -{ - -/*! -@brief an internal type for a backed binary type - -This type extends the template parameter @a BinaryType provided to `basic_json` -with a subtype used by BSON and MessagePack. This type exists so that the user -does not have to specify a type themselves with a specific naming scheme in -order to override the binary type. - -@tparam BinaryType container to store bytes (`std::vector` by - default) - -@since version 3.8.0; changed type of subtypes to std::uint64_t in 3.10.0. -*/ -template -class byte_container_with_subtype : public BinaryType -{ - public: - /// the type of the underlying container - using container_type = BinaryType; - /// the type of the subtype - using subtype_type = std::uint64_t; - - byte_container_with_subtype() noexcept(noexcept(container_type())) - : container_type() - {} - - byte_container_with_subtype(const container_type& b) noexcept(noexcept(container_type(b))) - : container_type(b) - {} - - byte_container_with_subtype(container_type&& b) noexcept(noexcept(container_type(std::move(b)))) - : container_type(std::move(b)) - {} - - byte_container_with_subtype(const container_type& b, subtype_type subtype_) noexcept(noexcept(container_type(b))) - : container_type(b) - , m_subtype(subtype_) - , m_has_subtype(true) - {} - - byte_container_with_subtype(container_type&& b, subtype_type subtype_) noexcept(noexcept(container_type(std::move(b)))) - : container_type(std::move(b)) - , m_subtype(subtype_) - , m_has_subtype(true) - {} - - bool operator==(const byte_container_with_subtype& rhs) const - { - return std::tie(static_cast(*this), m_subtype, m_has_subtype) == - std::tie(static_cast(rhs), rhs.m_subtype, rhs.m_has_subtype); - } - - bool operator!=(const byte_container_with_subtype& rhs) const - { - return !(rhs == *this); - } - - /*! - @brief sets the binary subtype - - Sets the binary subtype of the value, also flags a binary JSON value as - having a subtype, which has implications for serialization. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @sa see @ref subtype() -- return the binary subtype - @sa see @ref clear_subtype() -- clears the binary subtype - @sa see @ref has_subtype() -- returns whether or not the binary value has a - subtype - - @since version 3.8.0 - */ - void set_subtype(subtype_type subtype_) noexcept - { - m_subtype = subtype_; - m_has_subtype = true; - } - - /*! - @brief return the binary subtype - - Returns the numerical subtype of the value if it has a subtype. If it does - not have a subtype, this function will return subtype_type(-1) as a sentinel - value. - - @return the numerical subtype of the binary value - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @sa see @ref set_subtype() -- sets the binary subtype - @sa see @ref clear_subtype() -- clears the binary subtype - @sa see @ref has_subtype() -- returns whether or not the binary value has a - subtype - - @since version 3.8.0; fixed return value to properly return - subtype_type(-1) as documented in version 3.10.0 - */ - constexpr subtype_type subtype() const noexcept - { - return m_has_subtype ? m_subtype : subtype_type(-1); - } - - /*! - @brief return whether the value has a subtype - - @return whether the value has a subtype - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @sa see @ref subtype() -- return the binary subtype - @sa see @ref set_subtype() -- sets the binary subtype - @sa see @ref clear_subtype() -- clears the binary subtype - - @since version 3.8.0 - */ - constexpr bool has_subtype() const noexcept - { - return m_has_subtype; - } - - /*! - @brief clears the binary subtype - - Clears the binary subtype and flags the value as not having a subtype, which - has implications for serialization; for instance MessagePack will prefer the - bin family over the ext family. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @sa see @ref subtype() -- return the binary subtype - @sa see @ref set_subtype() -- sets the binary subtype - @sa see @ref has_subtype() -- returns whether or not the binary value has a - subtype - - @since version 3.8.0 - */ - void clear_subtype() noexcept - { - m_subtype = 0; - m_has_subtype = false; - } - - private: - subtype_type m_subtype = 0; - bool m_has_subtype = false; -}; - -} // namespace nlohmann - -// #include - -// #include - -// #include - -// #include - - -#include // uint8_t -#include // size_t -#include // hash - -// #include - -// #include - - -namespace nlohmann -{ -namespace detail -{ - -// boost::hash_combine -inline std::size_t combine(std::size_t seed, std::size_t h) noexcept -{ - seed ^= h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U); - return seed; -} - -/*! -@brief hash a JSON value - -The hash function tries to rely on std::hash where possible. Furthermore, the -type of the JSON value is taken into account to have different hash values for -null, 0, 0U, and false, etc. - -@tparam BasicJsonType basic_json specialization -@param j JSON value to hash -@return hash value of j -*/ -template -std::size_t hash(const BasicJsonType& j) -{ - using string_t = typename BasicJsonType::string_t; - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - - const auto type = static_cast(j.type()); - switch (j.type()) - { - case BasicJsonType::value_t::null: - case BasicJsonType::value_t::discarded: - { - return combine(type, 0); - } - - case BasicJsonType::value_t::object: - { - auto seed = combine(type, j.size()); - for (const auto& element : j.items()) - { - const auto h = std::hash {}(element.key()); - seed = combine(seed, h); - seed = combine(seed, hash(element.value())); - } - return seed; - } - - case BasicJsonType::value_t::array: - { - auto seed = combine(type, j.size()); - for (const auto& element : j) - { - seed = combine(seed, hash(element)); - } - return seed; - } - - case BasicJsonType::value_t::string: - { - const auto h = std::hash {}(j.template get_ref()); - return combine(type, h); - } - - case BasicJsonType::value_t::boolean: - { - const auto h = std::hash {}(j.template get()); - return combine(type, h); - } - - case BasicJsonType::value_t::number_integer: - { - const auto h = std::hash {}(j.template get()); - return combine(type, h); - } - - case BasicJsonType::value_t::number_unsigned: - { - const auto h = std::hash {}(j.template get()); - return combine(type, h); - } - - case BasicJsonType::value_t::number_float: - { - const auto h = std::hash {}(j.template get()); - return combine(type, h); - } - - case BasicJsonType::value_t::binary: - { - auto seed = combine(type, j.get_binary().size()); - const auto h = std::hash {}(j.get_binary().has_subtype()); - seed = combine(seed, h); - seed = combine(seed, static_cast(j.get_binary().subtype())); - for (const auto byte : j.get_binary()) - { - seed = combine(seed, std::hash {}(byte)); - } - return seed; - } - - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - return 0; // LCOV_EXCL_LINE - } -} - -} // namespace detail -} // namespace nlohmann - -// #include - - -#include // generate_n -#include // array -#include // ldexp -#include // size_t -#include // uint8_t, uint16_t, uint32_t, uint64_t -#include // snprintf -#include // memcpy -#include // back_inserter -#include // numeric_limits -#include // char_traits, string -#include // make_pair, move -#include // vector - -// #include - -// #include - - -#include // array -#include // size_t -#include // strlen -#include // begin, end, iterator_traits, random_access_iterator_tag, distance, next -#include // shared_ptr, make_shared, addressof -#include // accumulate -#include // string, char_traits -#include // enable_if, is_base_of, is_pointer, is_integral, remove_pointer -#include // pair, declval - -#ifndef JSON_NO_IO - #include // FILE * - #include // istream -#endif // JSON_NO_IO - -// #include - -// #include - - -namespace nlohmann -{ -namespace detail -{ -/// the supported input formats -enum class input_format_t { json, cbor, msgpack, ubjson, bson }; - -//////////////////// -// input adapters // -//////////////////// - -#ifndef JSON_NO_IO -/*! -Input adapter for stdio file access. This adapter read only 1 byte and do not use any - buffer. This adapter is a very low level adapter. -*/ -class file_input_adapter -{ - public: - using char_type = char; - - JSON_HEDLEY_NON_NULL(2) - explicit file_input_adapter(std::FILE* f) noexcept - : m_file(f) - {} - - // make class move-only - file_input_adapter(const file_input_adapter&) = delete; - file_input_adapter(file_input_adapter&&) noexcept = default; - file_input_adapter& operator=(const file_input_adapter&) = delete; - file_input_adapter& operator=(file_input_adapter&&) = delete; - ~file_input_adapter() = default; - - std::char_traits::int_type get_character() noexcept - { - return std::fgetc(m_file); - } - - private: - /// the file pointer to read from - std::FILE* m_file; -}; - - -/*! -Input adapter for a (caching) istream. Ignores a UFT Byte Order Mark at -beginning of input. Does not support changing the underlying std::streambuf -in mid-input. Maintains underlying std::istream and std::streambuf to support -subsequent use of standard std::istream operations to process any input -characters following those used in parsing the JSON input. Clears the -std::istream flags; any input errors (e.g., EOF) will be detected by the first -subsequent call for input from the std::istream. -*/ -class input_stream_adapter -{ - public: - using char_type = char; - - ~input_stream_adapter() - { - // clear stream flags; we use underlying streambuf I/O, do not - // maintain ifstream flags, except eof - if (is != nullptr) - { - is->clear(is->rdstate() & std::ios::eofbit); - } - } - - explicit input_stream_adapter(std::istream& i) - : is(&i), sb(i.rdbuf()) - {} - - // delete because of pointer members - input_stream_adapter(const input_stream_adapter&) = delete; - input_stream_adapter& operator=(input_stream_adapter&) = delete; - input_stream_adapter& operator=(input_stream_adapter&&) = delete; - - input_stream_adapter(input_stream_adapter&& rhs) noexcept - : is(rhs.is), sb(rhs.sb) - { - rhs.is = nullptr; - rhs.sb = nullptr; - } - - // std::istream/std::streambuf use std::char_traits::to_int_type, to - // ensure that std::char_traits::eof() and the character 0xFF do not - // end up as the same value, eg. 0xFFFFFFFF. - std::char_traits::int_type get_character() - { - auto res = sb->sbumpc(); - // set eof manually, as we don't use the istream interface. - if (JSON_HEDLEY_UNLIKELY(res == std::char_traits::eof())) - { - is->clear(is->rdstate() | std::ios::eofbit); - } - return res; - } - - private: - /// the associated input stream - std::istream* is = nullptr; - std::streambuf* sb = nullptr; -}; -#endif // JSON_NO_IO - -// General-purpose iterator-based adapter. It might not be as fast as -// theoretically possible for some containers, but it is extremely versatile. -template -class iterator_input_adapter -{ - public: - using char_type = typename std::iterator_traits::value_type; - - iterator_input_adapter(IteratorType first, IteratorType last) - : current(std::move(first)), end(std::move(last)) - {} - - typename std::char_traits::int_type get_character() - { - if (JSON_HEDLEY_LIKELY(current != end)) - { - auto result = std::char_traits::to_int_type(*current); - std::advance(current, 1); - return result; - } - - return std::char_traits::eof(); - } - - private: - IteratorType current; - IteratorType end; - - template - friend struct wide_string_input_helper; - - bool empty() const - { - return current == end; - } -}; - - -template -struct wide_string_input_helper; - -template -struct wide_string_input_helper -{ - // UTF-32 - static void fill_buffer(BaseInputAdapter& input, - std::array::int_type, 4>& utf8_bytes, - size_t& utf8_bytes_index, - size_t& utf8_bytes_filled) - { - utf8_bytes_index = 0; - - if (JSON_HEDLEY_UNLIKELY(input.empty())) - { - utf8_bytes[0] = std::char_traits::eof(); - utf8_bytes_filled = 1; - } - else - { - // get the current character - const auto wc = input.get_character(); - - // UTF-32 to UTF-8 encoding - if (wc < 0x80) - { - utf8_bytes[0] = static_cast::int_type>(wc); - utf8_bytes_filled = 1; - } - else if (wc <= 0x7FF) - { - utf8_bytes[0] = static_cast::int_type>(0xC0u | ((static_cast(wc) >> 6u) & 0x1Fu)); - utf8_bytes[1] = static_cast::int_type>(0x80u | (static_cast(wc) & 0x3Fu)); - utf8_bytes_filled = 2; - } - else if (wc <= 0xFFFF) - { - utf8_bytes[0] = static_cast::int_type>(0xE0u | ((static_cast(wc) >> 12u) & 0x0Fu)); - utf8_bytes[1] = static_cast::int_type>(0x80u | ((static_cast(wc) >> 6u) & 0x3Fu)); - utf8_bytes[2] = static_cast::int_type>(0x80u | (static_cast(wc) & 0x3Fu)); - utf8_bytes_filled = 3; - } - else if (wc <= 0x10FFFF) - { - utf8_bytes[0] = static_cast::int_type>(0xF0u | ((static_cast(wc) >> 18u) & 0x07u)); - utf8_bytes[1] = static_cast::int_type>(0x80u | ((static_cast(wc) >> 12u) & 0x3Fu)); - utf8_bytes[2] = static_cast::int_type>(0x80u | ((static_cast(wc) >> 6u) & 0x3Fu)); - utf8_bytes[3] = static_cast::int_type>(0x80u | (static_cast(wc) & 0x3Fu)); - utf8_bytes_filled = 4; - } - else - { - // unknown character - utf8_bytes[0] = static_cast::int_type>(wc); - utf8_bytes_filled = 1; - } - } - } -}; - -template -struct wide_string_input_helper -{ - // UTF-16 - static void fill_buffer(BaseInputAdapter& input, - std::array::int_type, 4>& utf8_bytes, - size_t& utf8_bytes_index, - size_t& utf8_bytes_filled) - { - utf8_bytes_index = 0; - - if (JSON_HEDLEY_UNLIKELY(input.empty())) - { - utf8_bytes[0] = std::char_traits::eof(); - utf8_bytes_filled = 1; - } - else - { - // get the current character - const auto wc = input.get_character(); - - // UTF-16 to UTF-8 encoding - if (wc < 0x80) - { - utf8_bytes[0] = static_cast::int_type>(wc); - utf8_bytes_filled = 1; - } - else if (wc <= 0x7FF) - { - utf8_bytes[0] = static_cast::int_type>(0xC0u | ((static_cast(wc) >> 6u))); - utf8_bytes[1] = static_cast::int_type>(0x80u | (static_cast(wc) & 0x3Fu)); - utf8_bytes_filled = 2; - } - else if (0xD800 > wc || wc >= 0xE000) - { - utf8_bytes[0] = static_cast::int_type>(0xE0u | ((static_cast(wc) >> 12u))); - utf8_bytes[1] = static_cast::int_type>(0x80u | ((static_cast(wc) >> 6u) & 0x3Fu)); - utf8_bytes[2] = static_cast::int_type>(0x80u | (static_cast(wc) & 0x3Fu)); - utf8_bytes_filled = 3; - } - else - { - if (JSON_HEDLEY_UNLIKELY(!input.empty())) - { - const auto wc2 = static_cast(input.get_character()); - const auto charcode = 0x10000u + (((static_cast(wc) & 0x3FFu) << 10u) | (wc2 & 0x3FFu)); - utf8_bytes[0] = static_cast::int_type>(0xF0u | (charcode >> 18u)); - utf8_bytes[1] = static_cast::int_type>(0x80u | ((charcode >> 12u) & 0x3Fu)); - utf8_bytes[2] = static_cast::int_type>(0x80u | ((charcode >> 6u) & 0x3Fu)); - utf8_bytes[3] = static_cast::int_type>(0x80u | (charcode & 0x3Fu)); - utf8_bytes_filled = 4; - } - else - { - utf8_bytes[0] = static_cast::int_type>(wc); - utf8_bytes_filled = 1; - } - } - } - } -}; - -// Wraps another input apdater to convert wide character types into individual bytes. -template -class wide_string_input_adapter -{ - public: - using char_type = char; - - wide_string_input_adapter(BaseInputAdapter base) - : base_adapter(base) {} - - typename std::char_traits::int_type get_character() noexcept - { - // check if buffer needs to be filled - if (utf8_bytes_index == utf8_bytes_filled) - { - fill_buffer(); - - JSON_ASSERT(utf8_bytes_filled > 0); - JSON_ASSERT(utf8_bytes_index == 0); - } - - // use buffer - JSON_ASSERT(utf8_bytes_filled > 0); - JSON_ASSERT(utf8_bytes_index < utf8_bytes_filled); - return utf8_bytes[utf8_bytes_index++]; - } - - private: - BaseInputAdapter base_adapter; - - template - void fill_buffer() - { - wide_string_input_helper::fill_buffer(base_adapter, utf8_bytes, utf8_bytes_index, utf8_bytes_filled); - } - - /// a buffer for UTF-8 bytes - std::array::int_type, 4> utf8_bytes = {{0, 0, 0, 0}}; - - /// index to the utf8_codes array for the next valid byte - std::size_t utf8_bytes_index = 0; - /// number of valid bytes in the utf8_codes array - std::size_t utf8_bytes_filled = 0; -}; - - -template -struct iterator_input_adapter_factory -{ - using iterator_type = IteratorType; - using char_type = typename std::iterator_traits::value_type; - using adapter_type = iterator_input_adapter; - - static adapter_type create(IteratorType first, IteratorType last) - { - return adapter_type(std::move(first), std::move(last)); - } -}; - -template -struct is_iterator_of_multibyte -{ - using value_type = typename std::iterator_traits::value_type; - enum - { - value = sizeof(value_type) > 1 - }; -}; - -template -struct iterator_input_adapter_factory::value>> -{ - using iterator_type = IteratorType; - using char_type = typename std::iterator_traits::value_type; - using base_adapter_type = iterator_input_adapter; - using adapter_type = wide_string_input_adapter; - - static adapter_type create(IteratorType first, IteratorType last) - { - return adapter_type(base_adapter_type(std::move(first), std::move(last))); - } -}; - -// General purpose iterator-based input -template -typename iterator_input_adapter_factory::adapter_type input_adapter(IteratorType first, IteratorType last) -{ - using factory_type = iterator_input_adapter_factory; - return factory_type::create(first, last); -} - -// Convenience shorthand from container to iterator -// Enables ADL on begin(container) and end(container) -// Encloses the using declarations in namespace for not to leak them to outside scope - -namespace container_input_adapter_factory_impl -{ - -using std::begin; -using std::end; - -template -struct container_input_adapter_factory {}; - -template -struct container_input_adapter_factory< ContainerType, - void_t()), end(std::declval()))>> - { - using adapter_type = decltype(input_adapter(begin(std::declval()), end(std::declval()))); - - static adapter_type create(const ContainerType& container) -{ - return input_adapter(begin(container), end(container)); -} - }; - -} // namespace container_input_adapter_factory_impl - -template -typename container_input_adapter_factory_impl::container_input_adapter_factory::adapter_type input_adapter(const ContainerType& container) -{ - return container_input_adapter_factory_impl::container_input_adapter_factory::create(container); -} - -#ifndef JSON_NO_IO -// Special cases with fast paths -inline file_input_adapter input_adapter(std::FILE* file) -{ - return file_input_adapter(file); -} - -inline input_stream_adapter input_adapter(std::istream& stream) -{ - return input_stream_adapter(stream); -} - -inline input_stream_adapter input_adapter(std::istream&& stream) -{ - return input_stream_adapter(stream); -} -#endif // JSON_NO_IO - -using contiguous_bytes_input_adapter = decltype(input_adapter(std::declval(), std::declval())); - -// Null-delimited strings, and the like. -template < typename CharT, - typename std::enable_if < - std::is_pointer::value&& - !std::is_array::value&& - std::is_integral::type>::value&& - sizeof(typename std::remove_pointer::type) == 1, - int >::type = 0 > -contiguous_bytes_input_adapter input_adapter(CharT b) -{ - auto length = std::strlen(reinterpret_cast(b)); - const auto* ptr = reinterpret_cast(b); - return input_adapter(ptr, ptr + length); -} - -template -auto input_adapter(T (&array)[N]) -> decltype(input_adapter(array, array + N)) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) -{ - return input_adapter(array, array + N); -} - -// This class only handles inputs of input_buffer_adapter type. -// It's required so that expressions like {ptr, len} can be implicitely casted -// to the correct adapter. -class span_input_adapter -{ - public: - template < typename CharT, - typename std::enable_if < - std::is_pointer::value&& - std::is_integral::type>::value&& - sizeof(typename std::remove_pointer::type) == 1, - int >::type = 0 > - span_input_adapter(CharT b, std::size_t l) - : ia(reinterpret_cast(b), reinterpret_cast(b) + l) {} - - template::iterator_category, std::random_access_iterator_tag>::value, - int>::type = 0> - span_input_adapter(IteratorType first, IteratorType last) - : ia(input_adapter(first, last)) {} - - contiguous_bytes_input_adapter&& get() - { - return std::move(ia); // NOLINT(hicpp-move-const-arg,performance-move-const-arg) - } - - private: - contiguous_bytes_input_adapter ia; -}; -} // namespace detail -} // namespace nlohmann - -// #include - - -#include -#include // string -#include // move -#include // vector - -// #include - -// #include - - -namespace nlohmann -{ - -/*! -@brief SAX interface - -This class describes the SAX interface used by @ref nlohmann::json::sax_parse. -Each function is called in different situations while the input is parsed. The -boolean return value informs the parser whether to continue processing the -input. -*/ -template -struct json_sax -{ - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - using binary_t = typename BasicJsonType::binary_t; - - /*! - @brief a null value was read - @return whether parsing should proceed - */ - virtual bool null() = 0; - - /*! - @brief a boolean value was read - @param[in] val boolean value - @return whether parsing should proceed - */ - virtual bool boolean(bool val) = 0; - - /*! - @brief an integer number was read - @param[in] val integer value - @return whether parsing should proceed - */ - virtual bool number_integer(number_integer_t val) = 0; - - /*! - @brief an unsigned integer number was read - @param[in] val unsigned integer value - @return whether parsing should proceed - */ - virtual bool number_unsigned(number_unsigned_t val) = 0; - - /*! - @brief an floating-point number was read - @param[in] val floating-point value - @param[in] s raw token value - @return whether parsing should proceed - */ - virtual bool number_float(number_float_t val, const string_t& s) = 0; - - /*! - @brief a string was read - @param[in] val string value - @return whether parsing should proceed - @note It is safe to move the passed string. - */ - virtual bool string(string_t& val) = 0; - - /*! - @brief a binary string was read - @param[in] val binary value - @return whether parsing should proceed - @note It is safe to move the passed binary. - */ - virtual bool binary(binary_t& val) = 0; - - /*! - @brief the beginning of an object was read - @param[in] elements number of object elements or -1 if unknown - @return whether parsing should proceed - @note binary formats may report the number of elements - */ - virtual bool start_object(std::size_t elements) = 0; - - /*! - @brief an object key was read - @param[in] val object key - @return whether parsing should proceed - @note It is safe to move the passed string. - */ - virtual bool key(string_t& val) = 0; - - /*! - @brief the end of an object was read - @return whether parsing should proceed - */ - virtual bool end_object() = 0; - - /*! - @brief the beginning of an array was read - @param[in] elements number of array elements or -1 if unknown - @return whether parsing should proceed - @note binary formats may report the number of elements - */ - virtual bool start_array(std::size_t elements) = 0; - - /*! - @brief the end of an array was read - @return whether parsing should proceed - */ - virtual bool end_array() = 0; - - /*! - @brief a parse error occurred - @param[in] position the position in the input where the error occurs - @param[in] last_token the last read token - @param[in] ex an exception object describing the error - @return whether parsing should proceed (must return false) - */ - virtual bool parse_error(std::size_t position, - const std::string& last_token, - const detail::exception& ex) = 0; - - json_sax() = default; - json_sax(const json_sax&) = default; - json_sax(json_sax&&) noexcept = default; - json_sax& operator=(const json_sax&) = default; - json_sax& operator=(json_sax&&) noexcept = default; - virtual ~json_sax() = default; -}; - - -namespace detail -{ -/*! -@brief SAX implementation to create a JSON value from SAX events - -This class implements the @ref json_sax interface and processes the SAX events -to create a JSON value which makes it basically a DOM parser. The structure or -hierarchy of the JSON value is managed by the stack `ref_stack` which contains -a pointer to the respective array or object for each recursion depth. - -After successful parsing, the value that is passed by reference to the -constructor contains the parsed value. - -@tparam BasicJsonType the JSON type -*/ -template -class json_sax_dom_parser -{ - public: - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - using binary_t = typename BasicJsonType::binary_t; - - /*! - @param[in,out] r reference to a JSON value that is manipulated while - parsing - @param[in] allow_exceptions_ whether parse errors yield exceptions - */ - explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true) - : root(r), allow_exceptions(allow_exceptions_) - {} - - // make class move-only - json_sax_dom_parser(const json_sax_dom_parser&) = delete; - json_sax_dom_parser(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) - json_sax_dom_parser& operator=(const json_sax_dom_parser&) = delete; - json_sax_dom_parser& operator=(json_sax_dom_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) - ~json_sax_dom_parser() = default; - - bool null() - { - handle_value(nullptr); - return true; - } - - bool boolean(bool val) - { - handle_value(val); - return true; - } - - bool number_integer(number_integer_t val) - { - handle_value(val); - return true; - } - - bool number_unsigned(number_unsigned_t val) - { - handle_value(val); - return true; - } - - bool number_float(number_float_t val, const string_t& /*unused*/) - { - handle_value(val); - return true; - } - - bool string(string_t& val) - { - handle_value(val); - return true; - } - - bool binary(binary_t& val) - { - handle_value(std::move(val)); - return true; - } - - bool start_object(std::size_t len) - { - ref_stack.push_back(handle_value(BasicJsonType::value_t::object)); - - if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) - { - JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back())); - } - - return true; - } - - bool key(string_t& val) - { - // add null at given key and store the reference for later - object_element = &(ref_stack.back()->m_value.object->operator[](val)); - return true; - } - - bool end_object() - { - ref_stack.back()->set_parents(); - ref_stack.pop_back(); - return true; - } - - bool start_array(std::size_t len) - { - ref_stack.push_back(handle_value(BasicJsonType::value_t::array)); - - if (JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) - { - JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back())); - } - - return true; - } - - bool end_array() - { - ref_stack.back()->set_parents(); - ref_stack.pop_back(); - return true; - } - - template - bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, - const Exception& ex) - { - errored = true; - static_cast(ex); - if (allow_exceptions) - { - JSON_THROW(ex); - } - return false; - } - - constexpr bool is_errored() const - { - return errored; - } - - private: - /*! - @invariant If the ref stack is empty, then the passed value will be the new - root. - @invariant If the ref stack contains a value, then it is an array or an - object to which we can add elements - */ - template - JSON_HEDLEY_RETURNS_NON_NULL - BasicJsonType* handle_value(Value&& v) - { - if (ref_stack.empty()) - { - root = BasicJsonType(std::forward(v)); - return &root; - } - - JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object()); - - if (ref_stack.back()->is_array()) - { - ref_stack.back()->m_value.array->emplace_back(std::forward(v)); - return &(ref_stack.back()->m_value.array->back()); - } - - JSON_ASSERT(ref_stack.back()->is_object()); - JSON_ASSERT(object_element); - *object_element = BasicJsonType(std::forward(v)); - return object_element; - } - - /// the parsed JSON value - BasicJsonType& root; - /// stack to model hierarchy of values - std::vector ref_stack {}; - /// helper to hold the reference for the next object element - BasicJsonType* object_element = nullptr; - /// whether a syntax error occurred - bool errored = false; - /// whether to throw exceptions in case of errors - const bool allow_exceptions = true; -}; - -template -class json_sax_dom_callback_parser -{ - public: - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - using binary_t = typename BasicJsonType::binary_t; - using parser_callback_t = typename BasicJsonType::parser_callback_t; - using parse_event_t = typename BasicJsonType::parse_event_t; - - json_sax_dom_callback_parser(BasicJsonType& r, - const parser_callback_t cb, - const bool allow_exceptions_ = true) - : root(r), callback(cb), allow_exceptions(allow_exceptions_) - { - keep_stack.push_back(true); - } - - // make class move-only - json_sax_dom_callback_parser(const json_sax_dom_callback_parser&) = delete; - json_sax_dom_callback_parser(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) - json_sax_dom_callback_parser& operator=(const json_sax_dom_callback_parser&) = delete; - json_sax_dom_callback_parser& operator=(json_sax_dom_callback_parser&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) - ~json_sax_dom_callback_parser() = default; - - bool null() - { - handle_value(nullptr); - return true; - } - - bool boolean(bool val) - { - handle_value(val); - return true; - } - - bool number_integer(number_integer_t val) - { - handle_value(val); - return true; - } - - bool number_unsigned(number_unsigned_t val) - { - handle_value(val); - return true; - } - - bool number_float(number_float_t val, const string_t& /*unused*/) - { - handle_value(val); - return true; - } - - bool string(string_t& val) - { - handle_value(val); - return true; - } - - bool binary(binary_t& val) - { - handle_value(std::move(val)); - return true; - } - - bool start_object(std::size_t len) - { - // check callback for object start - const bool keep = callback(static_cast(ref_stack.size()), parse_event_t::object_start, discarded); - keep_stack.push_back(keep); - - auto val = handle_value(BasicJsonType::value_t::object, true); - ref_stack.push_back(val.second); - - // check object limit - if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) - { - JSON_THROW(out_of_range::create(408, "excessive object size: " + std::to_string(len), *ref_stack.back())); - } - - return true; - } - - bool key(string_t& val) - { - BasicJsonType k = BasicJsonType(val); - - // check callback for key - const bool keep = callback(static_cast(ref_stack.size()), parse_event_t::key, k); - key_keep_stack.push_back(keep); - - // add discarded value at given key and store the reference for later - if (keep && ref_stack.back()) - { - object_element = &(ref_stack.back()->m_value.object->operator[](val) = discarded); - } - - return true; - } - - bool end_object() - { - if (ref_stack.back()) - { - if (!callback(static_cast(ref_stack.size()) - 1, parse_event_t::object_end, *ref_stack.back())) - { - // discard object - *ref_stack.back() = discarded; - } - else - { - ref_stack.back()->set_parents(); - } - } - - JSON_ASSERT(!ref_stack.empty()); - JSON_ASSERT(!keep_stack.empty()); - ref_stack.pop_back(); - keep_stack.pop_back(); - - if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured()) - { - // remove discarded value - for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it) - { - if (it->is_discarded()) - { - ref_stack.back()->erase(it); - break; - } - } - } - - return true; - } - - bool start_array(std::size_t len) - { - const bool keep = callback(static_cast(ref_stack.size()), parse_event_t::array_start, discarded); - keep_stack.push_back(keep); - - auto val = handle_value(BasicJsonType::value_t::array, true); - ref_stack.push_back(val.second); - - // check array limit - if (ref_stack.back() && JSON_HEDLEY_UNLIKELY(len != std::size_t(-1) && len > ref_stack.back()->max_size())) - { - JSON_THROW(out_of_range::create(408, "excessive array size: " + std::to_string(len), *ref_stack.back())); - } - - return true; - } - - bool end_array() - { - bool keep = true; - - if (ref_stack.back()) - { - keep = callback(static_cast(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back()); - if (keep) - { - ref_stack.back()->set_parents(); - } - else - { - // discard array - *ref_stack.back() = discarded; - } - } - - JSON_ASSERT(!ref_stack.empty()); - JSON_ASSERT(!keep_stack.empty()); - ref_stack.pop_back(); - keep_stack.pop_back(); - - // remove discarded value - if (!keep && !ref_stack.empty() && ref_stack.back()->is_array()) - { - ref_stack.back()->m_value.array->pop_back(); - } - - return true; - } - - template - bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, - const Exception& ex) - { - errored = true; - static_cast(ex); - if (allow_exceptions) - { - JSON_THROW(ex); - } - return false; - } - - constexpr bool is_errored() const - { - return errored; - } - - private: - /*! - @param[in] v value to add to the JSON value we build during parsing - @param[in] skip_callback whether we should skip calling the callback - function; this is required after start_array() and - start_object() SAX events, because otherwise we would call the - callback function with an empty array or object, respectively. - - @invariant If the ref stack is empty, then the passed value will be the new - root. - @invariant If the ref stack contains a value, then it is an array or an - object to which we can add elements - - @return pair of boolean (whether value should be kept) and pointer (to the - passed value in the ref_stack hierarchy; nullptr if not kept) - */ - template - std::pair handle_value(Value&& v, const bool skip_callback = false) - { - JSON_ASSERT(!keep_stack.empty()); - - // do not handle this value if we know it would be added to a discarded - // container - if (!keep_stack.back()) - { - return {false, nullptr}; - } - - // create value - auto value = BasicJsonType(std::forward(v)); - - // check callback - const bool keep = skip_callback || callback(static_cast(ref_stack.size()), parse_event_t::value, value); - - // do not handle this value if we just learnt it shall be discarded - if (!keep) - { - return {false, nullptr}; - } - - if (ref_stack.empty()) - { - root = std::move(value); - return {true, &root}; - } - - // skip this value if we already decided to skip the parent - // (https://github.com/nlohmann/json/issues/971#issuecomment-413678360) - if (!ref_stack.back()) - { - return {false, nullptr}; - } - - // we now only expect arrays and objects - JSON_ASSERT(ref_stack.back()->is_array() || ref_stack.back()->is_object()); - - // array - if (ref_stack.back()->is_array()) - { - ref_stack.back()->m_value.array->emplace_back(std::move(value)); - return {true, &(ref_stack.back()->m_value.array->back())}; - } - - // object - JSON_ASSERT(ref_stack.back()->is_object()); - // check if we should store an element for the current key - JSON_ASSERT(!key_keep_stack.empty()); - const bool store_element = key_keep_stack.back(); - key_keep_stack.pop_back(); - - if (!store_element) - { - return {false, nullptr}; - } - - JSON_ASSERT(object_element); - *object_element = std::move(value); - return {true, object_element}; - } - - /// the parsed JSON value - BasicJsonType& root; - /// stack to model hierarchy of values - std::vector ref_stack {}; - /// stack to manage which values to keep - std::vector keep_stack {}; - /// stack to manage which object keys to keep - std::vector key_keep_stack {}; - /// helper to hold the reference for the next object element - BasicJsonType* object_element = nullptr; - /// whether a syntax error occurred - bool errored = false; - /// callback function - const parser_callback_t callback = nullptr; - /// whether to throw exceptions in case of errors - const bool allow_exceptions = true; - /// a discarded value for the callback - BasicJsonType discarded = BasicJsonType::value_t::discarded; -}; - -template -class json_sax_acceptor -{ - public: - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - using binary_t = typename BasicJsonType::binary_t; - - bool null() - { - return true; - } - - bool boolean(bool /*unused*/) - { - return true; - } - - bool number_integer(number_integer_t /*unused*/) - { - return true; - } - - bool number_unsigned(number_unsigned_t /*unused*/) - { - return true; - } - - bool number_float(number_float_t /*unused*/, const string_t& /*unused*/) - { - return true; - } - - bool string(string_t& /*unused*/) - { - return true; - } - - bool binary(binary_t& /*unused*/) - { - return true; - } - - bool start_object(std::size_t /*unused*/ = std::size_t(-1)) - { - return true; - } - - bool key(string_t& /*unused*/) - { - return true; - } - - bool end_object() - { - return true; - } - - bool start_array(std::size_t /*unused*/ = std::size_t(-1)) - { - return true; - } - - bool end_array() - { - return true; - } - - bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, const detail::exception& /*unused*/) - { - return false; - } -}; -} // namespace detail - -} // namespace nlohmann - -// #include - - -#include // array -#include // localeconv -#include // size_t -#include // snprintf -#include // strtof, strtod, strtold, strtoll, strtoull -#include // initializer_list -#include // char_traits, string -#include // move -#include // vector - -// #include - -// #include - -// #include - - -namespace nlohmann -{ -namespace detail -{ -/////////// -// lexer // -/////////// - -template -class lexer_base -{ - public: - /// token types for the parser - enum class token_type - { - uninitialized, ///< indicating the scanner is uninitialized - literal_true, ///< the `true` literal - literal_false, ///< the `false` literal - literal_null, ///< the `null` literal - value_string, ///< a string -- use get_string() for actual value - value_unsigned, ///< an unsigned integer -- use get_number_unsigned() for actual value - value_integer, ///< a signed integer -- use get_number_integer() for actual value - value_float, ///< an floating point number -- use get_number_float() for actual value - begin_array, ///< the character for array begin `[` - begin_object, ///< the character for object begin `{` - end_array, ///< the character for array end `]` - end_object, ///< the character for object end `}` - name_separator, ///< the name separator `:` - value_separator, ///< the value separator `,` - parse_error, ///< indicating a parse error - end_of_input, ///< indicating the end of the input buffer - literal_or_value ///< a literal or the begin of a value (only for diagnostics) - }; - - /// return name of values of type token_type (only used for errors) - JSON_HEDLEY_RETURNS_NON_NULL - JSON_HEDLEY_CONST - static const char* token_type_name(const token_type t) noexcept - { - switch (t) - { - case token_type::uninitialized: - return ""; - case token_type::literal_true: - return "true literal"; - case token_type::literal_false: - return "false literal"; - case token_type::literal_null: - return "null literal"; - case token_type::value_string: - return "string literal"; - case token_type::value_unsigned: - case token_type::value_integer: - case token_type::value_float: - return "number literal"; - case token_type::begin_array: - return "'['"; - case token_type::begin_object: - return "'{'"; - case token_type::end_array: - return "']'"; - case token_type::end_object: - return "'}'"; - case token_type::name_separator: - return "':'"; - case token_type::value_separator: - return "','"; - case token_type::parse_error: - return ""; - case token_type::end_of_input: - return "end of input"; - case token_type::literal_or_value: - return "'[', '{', or a literal"; - // LCOV_EXCL_START - default: // catch non-enum values - return "unknown token"; - // LCOV_EXCL_STOP - } - } -}; -/*! -@brief lexical analysis - -This class organizes the lexical analysis during JSON deserialization. -*/ -template -class lexer : public lexer_base -{ - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - using char_type = typename InputAdapterType::char_type; - using char_int_type = typename std::char_traits::int_type; - - public: - using token_type = typename lexer_base::token_type; - - explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept - : ia(std::move(adapter)) - , ignore_comments(ignore_comments_) - , decimal_point_char(static_cast(get_decimal_point())) - {} - - // delete because of pointer members - lexer(const lexer&) = delete; - lexer(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) - lexer& operator=(lexer&) = delete; - lexer& operator=(lexer&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) - ~lexer() = default; - - private: - ///////////////////// - // locales - ///////////////////// - - /// return the locale-dependent decimal point - JSON_HEDLEY_PURE - static char get_decimal_point() noexcept - { - const auto* loc = localeconv(); - JSON_ASSERT(loc != nullptr); - return (loc->decimal_point == nullptr) ? '.' : *(loc->decimal_point); - } - - ///////////////////// - // scan functions - ///////////////////// - - /*! - @brief get codepoint from 4 hex characters following `\u` - - For input "\u c1 c2 c3 c4" the codepoint is: - (c1 * 0x1000) + (c2 * 0x0100) + (c3 * 0x0010) + c4 - = (c1 << 12) + (c2 << 8) + (c3 << 4) + (c4 << 0) - - Furthermore, the possible characters '0'..'9', 'A'..'F', and 'a'..'f' - must be converted to the integers 0x0..0x9, 0xA..0xF, 0xA..0xF, resp. The - conversion is done by subtracting the offset (0x30, 0x37, and 0x57) - between the ASCII value of the character and the desired integer value. - - @return codepoint (0x0000..0xFFFF) or -1 in case of an error (e.g. EOF or - non-hex character) - */ - int get_codepoint() - { - // this function only makes sense after reading `\u` - JSON_ASSERT(current == 'u'); - int codepoint = 0; - - const auto factors = { 12u, 8u, 4u, 0u }; - for (const auto factor : factors) - { - get(); - - if (current >= '0' && current <= '9') - { - codepoint += static_cast((static_cast(current) - 0x30u) << factor); - } - else if (current >= 'A' && current <= 'F') - { - codepoint += static_cast((static_cast(current) - 0x37u) << factor); - } - else if (current >= 'a' && current <= 'f') - { - codepoint += static_cast((static_cast(current) - 0x57u) << factor); - } - else - { - return -1; - } - } - - JSON_ASSERT(0x0000 <= codepoint && codepoint <= 0xFFFF); - return codepoint; - } - - /*! - @brief check if the next byte(s) are inside a given range - - Adds the current byte and, for each passed range, reads a new byte and - checks if it is inside the range. If a violation was detected, set up an - error message and return false. Otherwise, return true. - - @param[in] ranges list of integers; interpreted as list of pairs of - inclusive lower and upper bound, respectively - - @pre The passed list @a ranges must have 2, 4, or 6 elements; that is, - 1, 2, or 3 pairs. This precondition is enforced by an assertion. - - @return true if and only if no range violation was detected - */ - bool next_byte_in_range(std::initializer_list ranges) - { - JSON_ASSERT(ranges.size() == 2 || ranges.size() == 4 || ranges.size() == 6); - add(current); - - for (auto range = ranges.begin(); range != ranges.end(); ++range) - { - get(); - if (JSON_HEDLEY_LIKELY(*range <= current && current <= *(++range))) - { - add(current); - } - else - { - error_message = "invalid string: ill-formed UTF-8 byte"; - return false; - } - } - - return true; - } - - /*! - @brief scan a string literal - - This function scans a string according to Sect. 7 of RFC 8259. While - scanning, bytes are escaped and copied into buffer token_buffer. Then the - function returns successfully, token_buffer is *not* null-terminated (as it - may contain \0 bytes), and token_buffer.size() is the number of bytes in the - string. - - @return token_type::value_string if string could be successfully scanned, - token_type::parse_error otherwise - - @note In case of errors, variable error_message contains a textual - description. - */ - token_type scan_string() - { - // reset token_buffer (ignore opening quote) - reset(); - - // we entered the function by reading an open quote - JSON_ASSERT(current == '\"'); - - while (true) - { - // get next character - switch (get()) - { - // end of file while parsing string - case std::char_traits::eof(): - { - error_message = "invalid string: missing closing quote"; - return token_type::parse_error; - } - - // closing quote - case '\"': - { - return token_type::value_string; - } - - // escapes - case '\\': - { - switch (get()) - { - // quotation mark - case '\"': - add('\"'); - break; - // reverse solidus - case '\\': - add('\\'); - break; - // solidus - case '/': - add('/'); - break; - // backspace - case 'b': - add('\b'); - break; - // form feed - case 'f': - add('\f'); - break; - // line feed - case 'n': - add('\n'); - break; - // carriage return - case 'r': - add('\r'); - break; - // tab - case 't': - add('\t'); - break; - - // unicode escapes - case 'u': - { - const int codepoint1 = get_codepoint(); - int codepoint = codepoint1; // start with codepoint1 - - if (JSON_HEDLEY_UNLIKELY(codepoint1 == -1)) - { - error_message = "invalid string: '\\u' must be followed by 4 hex digits"; - return token_type::parse_error; - } - - // check if code point is a high surrogate - if (0xD800 <= codepoint1 && codepoint1 <= 0xDBFF) - { - // expect next \uxxxx entry - if (JSON_HEDLEY_LIKELY(get() == '\\' && get() == 'u')) - { - const int codepoint2 = get_codepoint(); - - if (JSON_HEDLEY_UNLIKELY(codepoint2 == -1)) - { - error_message = "invalid string: '\\u' must be followed by 4 hex digits"; - return token_type::parse_error; - } - - // check if codepoint2 is a low surrogate - if (JSON_HEDLEY_LIKELY(0xDC00 <= codepoint2 && codepoint2 <= 0xDFFF)) - { - // overwrite codepoint - codepoint = static_cast( - // high surrogate occupies the most significant 22 bits - (static_cast(codepoint1) << 10u) - // low surrogate occupies the least significant 15 bits - + static_cast(codepoint2) - // there is still the 0xD800, 0xDC00 and 0x10000 noise - // in the result so we have to subtract with: - // (0xD800 << 10) + DC00 - 0x10000 = 0x35FDC00 - - 0x35FDC00u); - } - else - { - error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF"; - return token_type::parse_error; - } - } - else - { - error_message = "invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF"; - return token_type::parse_error; - } - } - else - { - if (JSON_HEDLEY_UNLIKELY(0xDC00 <= codepoint1 && codepoint1 <= 0xDFFF)) - { - error_message = "invalid string: surrogate U+DC00..U+DFFF must follow U+D800..U+DBFF"; - return token_type::parse_error; - } - } - - // result of the above calculation yields a proper codepoint - JSON_ASSERT(0x00 <= codepoint && codepoint <= 0x10FFFF); - - // translate codepoint into bytes - if (codepoint < 0x80) - { - // 1-byte characters: 0xxxxxxx (ASCII) - add(static_cast(codepoint)); - } - else if (codepoint <= 0x7FF) - { - // 2-byte characters: 110xxxxx 10xxxxxx - add(static_cast(0xC0u | (static_cast(codepoint) >> 6u))); - add(static_cast(0x80u | (static_cast(codepoint) & 0x3Fu))); - } - else if (codepoint <= 0xFFFF) - { - // 3-byte characters: 1110xxxx 10xxxxxx 10xxxxxx - add(static_cast(0xE0u | (static_cast(codepoint) >> 12u))); - add(static_cast(0x80u | ((static_cast(codepoint) >> 6u) & 0x3Fu))); - add(static_cast(0x80u | (static_cast(codepoint) & 0x3Fu))); - } - else - { - // 4-byte characters: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx - add(static_cast(0xF0u | (static_cast(codepoint) >> 18u))); - add(static_cast(0x80u | ((static_cast(codepoint) >> 12u) & 0x3Fu))); - add(static_cast(0x80u | ((static_cast(codepoint) >> 6u) & 0x3Fu))); - add(static_cast(0x80u | (static_cast(codepoint) & 0x3Fu))); - } - - break; - } - - // other characters after escape - default: - error_message = "invalid string: forbidden character after backslash"; - return token_type::parse_error; - } - - break; - } - - // invalid control characters - case 0x00: - { - error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000"; - return token_type::parse_error; - } - - case 0x01: - { - error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001"; - return token_type::parse_error; - } - - case 0x02: - { - error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002"; - return token_type::parse_error; - } - - case 0x03: - { - error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003"; - return token_type::parse_error; - } - - case 0x04: - { - error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004"; - return token_type::parse_error; - } - - case 0x05: - { - error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005"; - return token_type::parse_error; - } - - case 0x06: - { - error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006"; - return token_type::parse_error; - } - - case 0x07: - { - error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007"; - return token_type::parse_error; - } - - case 0x08: - { - error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b"; - return token_type::parse_error; - } - - case 0x09: - { - error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t"; - return token_type::parse_error; - } - - case 0x0A: - { - error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n"; - return token_type::parse_error; - } - - case 0x0B: - { - error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B"; - return token_type::parse_error; - } - - case 0x0C: - { - error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f"; - return token_type::parse_error; - } - - case 0x0D: - { - error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r"; - return token_type::parse_error; - } - - case 0x0E: - { - error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E"; - return token_type::parse_error; - } - - case 0x0F: - { - error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F"; - return token_type::parse_error; - } - - case 0x10: - { - error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010"; - return token_type::parse_error; - } - - case 0x11: - { - error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011"; - return token_type::parse_error; - } - - case 0x12: - { - error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012"; - return token_type::parse_error; - } - - case 0x13: - { - error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013"; - return token_type::parse_error; - } - - case 0x14: - { - error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014"; - return token_type::parse_error; - } - - case 0x15: - { - error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015"; - return token_type::parse_error; - } - - case 0x16: - { - error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016"; - return token_type::parse_error; - } - - case 0x17: - { - error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017"; - return token_type::parse_error; - } - - case 0x18: - { - error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018"; - return token_type::parse_error; - } - - case 0x19: - { - error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019"; - return token_type::parse_error; - } - - case 0x1A: - { - error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A"; - return token_type::parse_error; - } - - case 0x1B: - { - error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B"; - return token_type::parse_error; - } - - case 0x1C: - { - error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C"; - return token_type::parse_error; - } - - case 0x1D: - { - error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D"; - return token_type::parse_error; - } - - case 0x1E: - { - error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E"; - return token_type::parse_error; - } - - case 0x1F: - { - error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F"; - return token_type::parse_error; - } - - // U+0020..U+007F (except U+0022 (quote) and U+005C (backspace)) - case 0x20: - case 0x21: - case 0x23: - case 0x24: - case 0x25: - case 0x26: - case 0x27: - case 0x28: - case 0x29: - case 0x2A: - case 0x2B: - case 0x2C: - case 0x2D: - case 0x2E: - case 0x2F: - case 0x30: - case 0x31: - case 0x32: - case 0x33: - case 0x34: - case 0x35: - case 0x36: - case 0x37: - case 0x38: - case 0x39: - case 0x3A: - case 0x3B: - case 0x3C: - case 0x3D: - case 0x3E: - case 0x3F: - case 0x40: - case 0x41: - case 0x42: - case 0x43: - case 0x44: - case 0x45: - case 0x46: - case 0x47: - case 0x48: - case 0x49: - case 0x4A: - case 0x4B: - case 0x4C: - case 0x4D: - case 0x4E: - case 0x4F: - case 0x50: - case 0x51: - case 0x52: - case 0x53: - case 0x54: - case 0x55: - case 0x56: - case 0x57: - case 0x58: - case 0x59: - case 0x5A: - case 0x5B: - case 0x5D: - case 0x5E: - case 0x5F: - case 0x60: - case 0x61: - case 0x62: - case 0x63: - case 0x64: - case 0x65: - case 0x66: - case 0x67: - case 0x68: - case 0x69: - case 0x6A: - case 0x6B: - case 0x6C: - case 0x6D: - case 0x6E: - case 0x6F: - case 0x70: - case 0x71: - case 0x72: - case 0x73: - case 0x74: - case 0x75: - case 0x76: - case 0x77: - case 0x78: - case 0x79: - case 0x7A: - case 0x7B: - case 0x7C: - case 0x7D: - case 0x7E: - case 0x7F: - { - add(current); - break; - } - - // U+0080..U+07FF: bytes C2..DF 80..BF - case 0xC2: - case 0xC3: - case 0xC4: - case 0xC5: - case 0xC6: - case 0xC7: - case 0xC8: - case 0xC9: - case 0xCA: - case 0xCB: - case 0xCC: - case 0xCD: - case 0xCE: - case 0xCF: - case 0xD0: - case 0xD1: - case 0xD2: - case 0xD3: - case 0xD4: - case 0xD5: - case 0xD6: - case 0xD7: - case 0xD8: - case 0xD9: - case 0xDA: - case 0xDB: - case 0xDC: - case 0xDD: - case 0xDE: - case 0xDF: - { - if (JSON_HEDLEY_UNLIKELY(!next_byte_in_range({0x80, 0xBF}))) - { - return token_type::parse_error; - } - break; - } - - // U+0800..U+0FFF: bytes E0 A0..BF 80..BF - case 0xE0: - { - if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0xA0, 0xBF, 0x80, 0xBF})))) - { - return token_type::parse_error; - } - break; - } - - // U+1000..U+CFFF: bytes E1..EC 80..BF 80..BF - // U+E000..U+FFFF: bytes EE..EF 80..BF 80..BF - case 0xE1: - case 0xE2: - case 0xE3: - case 0xE4: - case 0xE5: - case 0xE6: - case 0xE7: - case 0xE8: - case 0xE9: - case 0xEA: - case 0xEB: - case 0xEC: - case 0xEE: - case 0xEF: - { - if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF})))) - { - return token_type::parse_error; - } - break; - } - - // U+D000..U+D7FF: bytes ED 80..9F 80..BF - case 0xED: - { - if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x9F, 0x80, 0xBF})))) - { - return token_type::parse_error; - } - break; - } - - // U+10000..U+3FFFF F0 90..BF 80..BF 80..BF - case 0xF0: - { - if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x90, 0xBF, 0x80, 0xBF, 0x80, 0xBF})))) - { - return token_type::parse_error; - } - break; - } - - // U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF - case 0xF1: - case 0xF2: - case 0xF3: - { - if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0xBF, 0x80, 0xBF, 0x80, 0xBF})))) - { - return token_type::parse_error; - } - break; - } - - // U+100000..U+10FFFF F4 80..8F 80..BF 80..BF - case 0xF4: - { - if (JSON_HEDLEY_UNLIKELY(!(next_byte_in_range({0x80, 0x8F, 0x80, 0xBF, 0x80, 0xBF})))) - { - return token_type::parse_error; - } - break; - } - - // remaining bytes (80..C1 and F5..FF) are ill-formed - default: - { - error_message = "invalid string: ill-formed UTF-8 byte"; - return token_type::parse_error; - } - } - } - } - - /*! - * @brief scan a comment - * @return whether comment could be scanned successfully - */ - bool scan_comment() - { - switch (get()) - { - // single-line comments skip input until a newline or EOF is read - case '/': - { - while (true) - { - switch (get()) - { - case '\n': - case '\r': - case std::char_traits::eof(): - case '\0': - return true; - - default: - break; - } - } - } - - // multi-line comments skip input until */ is read - case '*': - { - while (true) - { - switch (get()) - { - case std::char_traits::eof(): - case '\0': - { - error_message = "invalid comment; missing closing '*/'"; - return false; - } - - case '*': - { - switch (get()) - { - case '/': - return true; - - default: - { - unget(); - continue; - } - } - } - - default: - continue; - } - } - } - - // unexpected character after reading '/' - default: - { - error_message = "invalid comment; expecting '/' or '*' after '/'"; - return false; - } - } - } - - JSON_HEDLEY_NON_NULL(2) - static void strtof(float& f, const char* str, char** endptr) noexcept - { - f = std::strtof(str, endptr); - } - - JSON_HEDLEY_NON_NULL(2) - static void strtof(double& f, const char* str, char** endptr) noexcept - { - f = std::strtod(str, endptr); - } - - JSON_HEDLEY_NON_NULL(2) - static void strtof(long double& f, const char* str, char** endptr) noexcept - { - f = std::strtold(str, endptr); - } - - /*! - @brief scan a number literal - - This function scans a string according to Sect. 6 of RFC 8259. - - The function is realized with a deterministic finite state machine derived - from the grammar described in RFC 8259. Starting in state "init", the - input is read and used to determined the next state. Only state "done" - accepts the number. State "error" is a trap state to model errors. In the - table below, "anything" means any character but the ones listed before. - - state | 0 | 1-9 | e E | + | - | . | anything - ---------|----------|----------|----------|---------|---------|----------|----------- - init | zero | any1 | [error] | [error] | minus | [error] | [error] - minus | zero | any1 | [error] | [error] | [error] | [error] | [error] - zero | done | done | exponent | done | done | decimal1 | done - any1 | any1 | any1 | exponent | done | done | decimal1 | done - decimal1 | decimal2 | decimal2 | [error] | [error] | [error] | [error] | [error] - decimal2 | decimal2 | decimal2 | exponent | done | done | done | done - exponent | any2 | any2 | [error] | sign | sign | [error] | [error] - sign | any2 | any2 | [error] | [error] | [error] | [error] | [error] - any2 | any2 | any2 | done | done | done | done | done - - The state machine is realized with one label per state (prefixed with - "scan_number_") and `goto` statements between them. The state machine - contains cycles, but any cycle can be left when EOF is read. Therefore, - the function is guaranteed to terminate. - - During scanning, the read bytes are stored in token_buffer. This string is - then converted to a signed integer, an unsigned integer, or a - floating-point number. - - @return token_type::value_unsigned, token_type::value_integer, or - token_type::value_float if number could be successfully scanned, - token_type::parse_error otherwise - - @note The scanner is independent of the current locale. Internally, the - locale's decimal point is used instead of `.` to work with the - locale-dependent converters. - */ - token_type scan_number() // lgtm [cpp/use-of-goto] - { - // reset token_buffer to store the number's bytes - reset(); - - // the type of the parsed number; initially set to unsigned; will be - // changed if minus sign, decimal point or exponent is read - token_type number_type = token_type::value_unsigned; - - // state (init): we just found out we need to scan a number - switch (current) - { - case '-': - { - add(current); - goto scan_number_minus; - } - - case '0': - { - add(current); - goto scan_number_zero; - } - - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - { - add(current); - goto scan_number_any1; - } - - // all other characters are rejected outside scan_number() - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - } - -scan_number_minus: - // state: we just parsed a leading minus sign - number_type = token_type::value_integer; - switch (get()) - { - case '0': - { - add(current); - goto scan_number_zero; - } - - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - { - add(current); - goto scan_number_any1; - } - - default: - { - error_message = "invalid number; expected digit after '-'"; - return token_type::parse_error; - } - } - -scan_number_zero: - // state: we just parse a zero (maybe with a leading minus sign) - switch (get()) - { - case '.': - { - add(decimal_point_char); - goto scan_number_decimal1; - } - - case 'e': - case 'E': - { - add(current); - goto scan_number_exponent; - } - - default: - goto scan_number_done; - } - -scan_number_any1: - // state: we just parsed a number 0-9 (maybe with a leading minus sign) - switch (get()) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - { - add(current); - goto scan_number_any1; - } - - case '.': - { - add(decimal_point_char); - goto scan_number_decimal1; - } - - case 'e': - case 'E': - { - add(current); - goto scan_number_exponent; - } - - default: - goto scan_number_done; - } - -scan_number_decimal1: - // state: we just parsed a decimal point - number_type = token_type::value_float; - switch (get()) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - { - add(current); - goto scan_number_decimal2; - } - - default: - { - error_message = "invalid number; expected digit after '.'"; - return token_type::parse_error; - } - } - -scan_number_decimal2: - // we just parsed at least one number after a decimal point - switch (get()) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - { - add(current); - goto scan_number_decimal2; - } - - case 'e': - case 'E': - { - add(current); - goto scan_number_exponent; - } - - default: - goto scan_number_done; - } - -scan_number_exponent: - // we just parsed an exponent - number_type = token_type::value_float; - switch (get()) - { - case '+': - case '-': - { - add(current); - goto scan_number_sign; - } - - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - { - add(current); - goto scan_number_any2; - } - - default: - { - error_message = - "invalid number; expected '+', '-', or digit after exponent"; - return token_type::parse_error; - } - } - -scan_number_sign: - // we just parsed an exponent sign - switch (get()) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - { - add(current); - goto scan_number_any2; - } - - default: - { - error_message = "invalid number; expected digit after exponent sign"; - return token_type::parse_error; - } - } - -scan_number_any2: - // we just parsed a number after the exponent or exponent sign - switch (get()) - { - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - { - add(current); - goto scan_number_any2; - } - - default: - goto scan_number_done; - } - -scan_number_done: - // unget the character after the number (we only read it to know that - // we are done scanning a number) - unget(); - - char* endptr = nullptr; // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - errno = 0; - - // try to parse integers first and fall back to floats - if (number_type == token_type::value_unsigned) - { - const auto x = std::strtoull(token_buffer.data(), &endptr, 10); - - // we checked the number format before - JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size()); - - if (errno == 0) - { - value_unsigned = static_cast(x); - if (value_unsigned == x) - { - return token_type::value_unsigned; - } - } - } - else if (number_type == token_type::value_integer) - { - const auto x = std::strtoll(token_buffer.data(), &endptr, 10); - - // we checked the number format before - JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size()); - - if (errno == 0) - { - value_integer = static_cast(x); - if (value_integer == x) - { - return token_type::value_integer; - } - } - } - - // this code is reached if we parse a floating-point number or if an - // integer conversion above failed - strtof(value_float, token_buffer.data(), &endptr); - - // we checked the number format before - JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size()); - - return token_type::value_float; - } - - /*! - @param[in] literal_text the literal text to expect - @param[in] length the length of the passed literal text - @param[in] return_type the token type to return on success - */ - JSON_HEDLEY_NON_NULL(2) - token_type scan_literal(const char_type* literal_text, const std::size_t length, - token_type return_type) - { - JSON_ASSERT(std::char_traits::to_char_type(current) == literal_text[0]); - for (std::size_t i = 1; i < length; ++i) - { - if (JSON_HEDLEY_UNLIKELY(std::char_traits::to_char_type(get()) != literal_text[i])) - { - error_message = "invalid literal"; - return token_type::parse_error; - } - } - return return_type; - } - - ///////////////////// - // input management - ///////////////////// - - /// reset token_buffer; current character is beginning of token - void reset() noexcept - { - token_buffer.clear(); - token_string.clear(); - token_string.push_back(std::char_traits::to_char_type(current)); - } - - /* - @brief get next character from the input - - This function provides the interface to the used input adapter. It does - not throw in case the input reached EOF, but returns a - `std::char_traits::eof()` in that case. Stores the scanned characters - for use in error messages. - - @return character read from the input - */ - char_int_type get() - { - ++position.chars_read_total; - ++position.chars_read_current_line; - - if (next_unget) - { - // just reset the next_unget variable and work with current - next_unget = false; - } - else - { - current = ia.get_character(); - } - - if (JSON_HEDLEY_LIKELY(current != std::char_traits::eof())) - { - token_string.push_back(std::char_traits::to_char_type(current)); - } - - if (current == '\n') - { - ++position.lines_read; - position.chars_read_current_line = 0; - } - - return current; - } - - /*! - @brief unget current character (read it again on next get) - - We implement unget by setting variable next_unget to true. The input is not - changed - we just simulate ungetting by modifying chars_read_total, - chars_read_current_line, and token_string. The next call to get() will - behave as if the unget character is read again. - */ - void unget() - { - next_unget = true; - - --position.chars_read_total; - - // in case we "unget" a newline, we have to also decrement the lines_read - if (position.chars_read_current_line == 0) - { - if (position.lines_read > 0) - { - --position.lines_read; - } - } - else - { - --position.chars_read_current_line; - } - - if (JSON_HEDLEY_LIKELY(current != std::char_traits::eof())) - { - JSON_ASSERT(!token_string.empty()); - token_string.pop_back(); - } - } - - /// add a character to token_buffer - void add(char_int_type c) - { - token_buffer.push_back(static_cast(c)); - } - - public: - ///////////////////// - // value getters - ///////////////////// - - /// return integer value - constexpr number_integer_t get_number_integer() const noexcept - { - return value_integer; - } - - /// return unsigned integer value - constexpr number_unsigned_t get_number_unsigned() const noexcept - { - return value_unsigned; - } - - /// return floating-point value - constexpr number_float_t get_number_float() const noexcept - { - return value_float; - } - - /// return current string value (implicitly resets the token; useful only once) - string_t& get_string() - { - return token_buffer; - } - - ///////////////////// - // diagnostics - ///////////////////// - - /// return position of last read token - constexpr position_t get_position() const noexcept - { - return position; - } - - /// return the last read token (for errors only). Will never contain EOF - /// (an arbitrary value that is not a valid char value, often -1), because - /// 255 may legitimately occur. May contain NUL, which should be escaped. - std::string get_token_string() const - { - // escape control characters - std::string result; - for (const auto c : token_string) - { - if (static_cast(c) <= '\x1F') - { - // escape control characters - std::array cs{{}}; - (std::snprintf)(cs.data(), cs.size(), "", static_cast(c)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - result += cs.data(); - } - else - { - // add character as is - result.push_back(static_cast(c)); - } - } - - return result; - } - - /// return syntax error message - JSON_HEDLEY_RETURNS_NON_NULL - constexpr const char* get_error_message() const noexcept - { - return error_message; - } - - ///////////////////// - // actual scanner - ///////////////////// - - /*! - @brief skip the UTF-8 byte order mark - @return true iff there is no BOM or the correct BOM has been skipped - */ - bool skip_bom() - { - if (get() == 0xEF) - { - // check if we completely parse the BOM - return get() == 0xBB && get() == 0xBF; - } - - // the first character is not the beginning of the BOM; unget it to - // process is later - unget(); - return true; - } - - void skip_whitespace() - { - do - { - get(); - } - while (current == ' ' || current == '\t' || current == '\n' || current == '\r'); - } - - token_type scan() - { - // initially, skip the BOM - if (position.chars_read_total == 0 && !skip_bom()) - { - error_message = "invalid BOM; must be 0xEF 0xBB 0xBF if given"; - return token_type::parse_error; - } - - // read next character and ignore whitespace - skip_whitespace(); - - // ignore comments - while (ignore_comments && current == '/') - { - if (!scan_comment()) - { - return token_type::parse_error; - } - - // skip following whitespace - skip_whitespace(); - } - - switch (current) - { - // structural characters - case '[': - return token_type::begin_array; - case ']': - return token_type::end_array; - case '{': - return token_type::begin_object; - case '}': - return token_type::end_object; - case ':': - return token_type::name_separator; - case ',': - return token_type::value_separator; - - // literals - case 't': - { - std::array true_literal = {{char_type('t'), char_type('r'), char_type('u'), char_type('e')}}; - return scan_literal(true_literal.data(), true_literal.size(), token_type::literal_true); - } - case 'f': - { - std::array false_literal = {{char_type('f'), char_type('a'), char_type('l'), char_type('s'), char_type('e')}}; - return scan_literal(false_literal.data(), false_literal.size(), token_type::literal_false); - } - case 'n': - { - std::array null_literal = {{char_type('n'), char_type('u'), char_type('l'), char_type('l')}}; - return scan_literal(null_literal.data(), null_literal.size(), token_type::literal_null); - } - - // string - case '\"': - return scan_string(); - - // number - case '-': - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': - case '9': - return scan_number(); - - // end of input (the null byte is needed when parsing from - // string literals) - case '\0': - case std::char_traits::eof(): - return token_type::end_of_input; - - // error - default: - error_message = "invalid literal"; - return token_type::parse_error; - } - } - - private: - /// input adapter - InputAdapterType ia; - - /// whether comments should be ignored (true) or signaled as errors (false) - const bool ignore_comments = false; - - /// the current character - char_int_type current = std::char_traits::eof(); - - /// whether the next get() call should just return current - bool next_unget = false; - - /// the start position of the current token - position_t position {}; - - /// raw input token string (for error messages) - std::vector token_string {}; - - /// buffer for variable-length tokens (numbers, strings) - string_t token_buffer {}; - - /// a description of occurred lexer errors - const char* error_message = ""; - - // number values - number_integer_t value_integer = 0; - number_unsigned_t value_unsigned = 0; - number_float_t value_float = 0; - - /// the decimal point - const char_int_type decimal_point_char = '.'; -}; -} // namespace detail -} // namespace nlohmann - -// #include - -// #include - - -#include // size_t -#include // declval -#include // string - -// #include - -// #include - - -namespace nlohmann -{ -namespace detail -{ -template -using null_function_t = decltype(std::declval().null()); - -template -using boolean_function_t = - decltype(std::declval().boolean(std::declval())); - -template -using number_integer_function_t = - decltype(std::declval().number_integer(std::declval())); - -template -using number_unsigned_function_t = - decltype(std::declval().number_unsigned(std::declval())); - -template -using number_float_function_t = decltype(std::declval().number_float( - std::declval(), std::declval())); - -template -using string_function_t = - decltype(std::declval().string(std::declval())); - -template -using binary_function_t = - decltype(std::declval().binary(std::declval())); - -template -using start_object_function_t = - decltype(std::declval().start_object(std::declval())); - -template -using key_function_t = - decltype(std::declval().key(std::declval())); - -template -using end_object_function_t = decltype(std::declval().end_object()); - -template -using start_array_function_t = - decltype(std::declval().start_array(std::declval())); - -template -using end_array_function_t = decltype(std::declval().end_array()); - -template -using parse_error_function_t = decltype(std::declval().parse_error( - std::declval(), std::declval(), - std::declval())); - -template -struct is_sax -{ - private: - static_assert(is_basic_json::value, - "BasicJsonType must be of type basic_json<...>"); - - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - using binary_t = typename BasicJsonType::binary_t; - using exception_t = typename BasicJsonType::exception; - - public: - static constexpr bool value = - is_detected_exact::value && - is_detected_exact::value && - is_detected_exact::value && - is_detected_exact::value && - is_detected_exact::value && - is_detected_exact::value && - is_detected_exact::value && - is_detected_exact::value && - is_detected_exact::value && - is_detected_exact::value && - is_detected_exact::value && - is_detected_exact::value && - is_detected_exact::value; -}; - -template -struct is_sax_static_asserts -{ - private: - static_assert(is_basic_json::value, - "BasicJsonType must be of type basic_json<...>"); - - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - using binary_t = typename BasicJsonType::binary_t; - using exception_t = typename BasicJsonType::exception; - - public: - static_assert(is_detected_exact::value, - "Missing/invalid function: bool null()"); - static_assert(is_detected_exact::value, - "Missing/invalid function: bool boolean(bool)"); - static_assert(is_detected_exact::value, - "Missing/invalid function: bool boolean(bool)"); - static_assert( - is_detected_exact::value, - "Missing/invalid function: bool number_integer(number_integer_t)"); - static_assert( - is_detected_exact::value, - "Missing/invalid function: bool number_unsigned(number_unsigned_t)"); - static_assert(is_detected_exact::value, - "Missing/invalid function: bool number_float(number_float_t, const string_t&)"); - static_assert( - is_detected_exact::value, - "Missing/invalid function: bool string(string_t&)"); - static_assert( - is_detected_exact::value, - "Missing/invalid function: bool binary(binary_t&)"); - static_assert(is_detected_exact::value, - "Missing/invalid function: bool start_object(std::size_t)"); - static_assert(is_detected_exact::value, - "Missing/invalid function: bool key(string_t&)"); - static_assert(is_detected_exact::value, - "Missing/invalid function: bool end_object()"); - static_assert(is_detected_exact::value, - "Missing/invalid function: bool start_array(std::size_t)"); - static_assert(is_detected_exact::value, - "Missing/invalid function: bool end_array()"); - static_assert( - is_detected_exact::value, - "Missing/invalid function: bool parse_error(std::size_t, const " - "std::string&, const exception&)"); -}; -} // namespace detail -} // namespace nlohmann - -// #include - -// #include - - -namespace nlohmann -{ -namespace detail -{ - -/// how to treat CBOR tags -enum class cbor_tag_handler_t -{ - error, ///< throw a parse_error exception in case of a tag - ignore, ///< ignore tags - store ///< store tags as binary type -}; - -/*! -@brief determine system byte order - -@return true if and only if system's byte order is little endian - -@note from https://stackoverflow.com/a/1001328/266378 -*/ -static inline bool little_endianess(int num = 1) noexcept -{ - return *reinterpret_cast(&num) == 1; -} - - -/////////////////// -// binary reader // -/////////////////// - -/*! -@brief deserialization of CBOR, MessagePack, and UBJSON values -*/ -template> -class binary_reader -{ - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - using binary_t = typename BasicJsonType::binary_t; - using json_sax_t = SAX; - using char_type = typename InputAdapterType::char_type; - using char_int_type = typename std::char_traits::int_type; - - public: - /*! - @brief create a binary reader - - @param[in] adapter input adapter to read from - */ - explicit binary_reader(InputAdapterType&& adapter) noexcept : ia(std::move(adapter)) - { - (void)detail::is_sax_static_asserts {}; - } - - // make class move-only - binary_reader(const binary_reader&) = delete; - binary_reader(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) - binary_reader& operator=(const binary_reader&) = delete; - binary_reader& operator=(binary_reader&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) - ~binary_reader() = default; - - /*! - @param[in] format the binary format to parse - @param[in] sax_ a SAX event processor - @param[in] strict whether to expect the input to be consumed completed - @param[in] tag_handler how to treat CBOR tags - - @return whether parsing was successful - */ - JSON_HEDLEY_NON_NULL(3) - bool sax_parse(const input_format_t format, - json_sax_t* sax_, - const bool strict = true, - const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) - { - sax = sax_; - bool result = false; - - switch (format) - { - case input_format_t::bson: - result = parse_bson_internal(); - break; - - case input_format_t::cbor: - result = parse_cbor_internal(true, tag_handler); - break; - - case input_format_t::msgpack: - result = parse_msgpack_internal(); - break; - - case input_format_t::ubjson: - result = parse_ubjson_internal(); - break; - - case input_format_t::json: // LCOV_EXCL_LINE - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - } - - // strict mode: next byte must be EOF - if (result && strict) - { - if (format == input_format_t::ubjson) - { - get_ignore_noop(); - } - else - { - get(); - } - - if (JSON_HEDLEY_UNLIKELY(current != std::char_traits::eof())) - { - return sax->parse_error(chars_read, get_token_string(), - parse_error::create(110, chars_read, exception_message(format, "expected end of input; last byte: 0x" + get_token_string(), "value"), BasicJsonType())); - } - } - - return result; - } - - private: - ////////// - // BSON // - ////////// - - /*! - @brief Reads in a BSON-object and passes it to the SAX-parser. - @return whether a valid BSON-value was passed to the SAX parser - */ - bool parse_bson_internal() - { - std::int32_t document_size{}; - get_number(input_format_t::bson, document_size); - - if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::size_t(-1)))) - { - return false; - } - - if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/false))) - { - return false; - } - - return sax->end_object(); - } - - /*! - @brief Parses a C-style string from the BSON input. - @param[in,out] result A reference to the string variable where the read - string is to be stored. - @return `true` if the \x00-byte indicating the end of the string was - encountered before the EOF; false` indicates an unexpected EOF. - */ - bool get_bson_cstr(string_t& result) - { - auto out = std::back_inserter(result); - while (true) - { - get(); - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "cstring"))) - { - return false; - } - if (current == 0x00) - { - return true; - } - *out++ = static_cast(current); - } - } - - /*! - @brief Parses a zero-terminated string of length @a len from the BSON - input. - @param[in] len The length (including the zero-byte at the end) of the - string to be read. - @param[in,out] result A reference to the string variable where the read - string is to be stored. - @tparam NumberType The type of the length @a len - @pre len >= 1 - @return `true` if the string was successfully parsed - */ - template - bool get_bson_string(const NumberType len, string_t& result) - { - if (JSON_HEDLEY_UNLIKELY(len < 1)) - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "string length must be at least 1, is " + std::to_string(len), "string"), BasicJsonType())); - } - - return get_string(input_format_t::bson, len - static_cast(1), result) && get() != std::char_traits::eof(); - } - - /*! - @brief Parses a byte array input of length @a len from the BSON input. - @param[in] len The length of the byte array to be read. - @param[in,out] result A reference to the binary variable where the read - array is to be stored. - @tparam NumberType The type of the length @a len - @pre len >= 0 - @return `true` if the byte array was successfully parsed - */ - template - bool get_bson_binary(const NumberType len, binary_t& result) - { - if (JSON_HEDLEY_UNLIKELY(len < 0)) - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::bson, "byte array length cannot be negative, is " + std::to_string(len), "binary"), BasicJsonType())); - } - - // All BSON binary values have a subtype - std::uint8_t subtype{}; - get_number(input_format_t::bson, subtype); - result.set_subtype(subtype); - - return get_binary(input_format_t::bson, len, result); - } - - /*! - @brief Read a BSON document element of the given @a element_type. - @param[in] element_type The BSON element type, c.f. http://bsonspec.org/spec.html - @param[in] element_type_parse_position The position in the input stream, - where the `element_type` was read. - @warning Not all BSON element types are supported yet. An unsupported - @a element_type will give rise to a parse_error.114: - Unsupported BSON record type 0x... - @return whether a valid BSON-object/array was passed to the SAX parser - */ - bool parse_bson_element_internal(const char_int_type element_type, - const std::size_t element_type_parse_position) - { - switch (element_type) - { - case 0x01: // double - { - double number{}; - return get_number(input_format_t::bson, number) && sax->number_float(static_cast(number), ""); - } - - case 0x02: // string - { - std::int32_t len{}; - string_t value; - return get_number(input_format_t::bson, len) && get_bson_string(len, value) && sax->string(value); - } - - case 0x03: // object - { - return parse_bson_internal(); - } - - case 0x04: // array - { - return parse_bson_array(); - } - - case 0x05: // binary - { - std::int32_t len{}; - binary_t value; - return get_number(input_format_t::bson, len) && get_bson_binary(len, value) && sax->binary(value); - } - - case 0x08: // boolean - { - return sax->boolean(get() != 0); - } - - case 0x0A: // null - { - return sax->null(); - } - - case 0x10: // int32 - { - std::int32_t value{}; - return get_number(input_format_t::bson, value) && sax->number_integer(value); - } - - case 0x12: // int64 - { - std::int64_t value{}; - return get_number(input_format_t::bson, value) && sax->number_integer(value); - } - - default: // anything else not supported (yet) - { - std::array cr{{}}; - (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(element_type)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - return sax->parse_error(element_type_parse_position, std::string(cr.data()), parse_error::create(114, element_type_parse_position, "Unsupported BSON record type 0x" + std::string(cr.data()), BasicJsonType())); - } - } - } - - /*! - @brief Read a BSON element list (as specified in the BSON-spec) - - The same binary layout is used for objects and arrays, hence it must be - indicated with the argument @a is_array which one is expected - (true --> array, false --> object). - - @param[in] is_array Determines if the element list being read is to be - treated as an object (@a is_array == false), or as an - array (@a is_array == true). - @return whether a valid BSON-object/array was passed to the SAX parser - */ - bool parse_bson_element_list(const bool is_array) - { - string_t key; - - while (auto element_type = get()) - { - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::bson, "element list"))) - { - return false; - } - - const std::size_t element_type_parse_position = chars_read; - if (JSON_HEDLEY_UNLIKELY(!get_bson_cstr(key))) - { - return false; - } - - if (!is_array && !sax->key(key)) - { - return false; - } - - if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_internal(element_type, element_type_parse_position))) - { - return false; - } - - // get_bson_cstr only appends - key.clear(); - } - - return true; - } - - /*! - @brief Reads an array from the BSON input and passes it to the SAX-parser. - @return whether a valid BSON-array was passed to the SAX parser - */ - bool parse_bson_array() - { - std::int32_t document_size{}; - get_number(input_format_t::bson, document_size); - - if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::size_t(-1)))) - { - return false; - } - - if (JSON_HEDLEY_UNLIKELY(!parse_bson_element_list(/*is_array*/true))) - { - return false; - } - - return sax->end_array(); - } - - ////////// - // CBOR // - ////////// - - /*! - @param[in] get_char whether a new character should be retrieved from the - input (true) or whether the last read character should - be considered instead (false) - @param[in] tag_handler how CBOR tags should be treated - - @return whether a valid CBOR value was passed to the SAX parser - */ - bool parse_cbor_internal(const bool get_char, - const cbor_tag_handler_t tag_handler) - { - switch (get_char ? get() : current) - { - // EOF - case std::char_traits::eof(): - return unexpect_eof(input_format_t::cbor, "value"); - - // Integer 0x00..0x17 (0..23) - case 0x00: - case 0x01: - case 0x02: - case 0x03: - case 0x04: - case 0x05: - case 0x06: - case 0x07: - case 0x08: - case 0x09: - case 0x0A: - case 0x0B: - case 0x0C: - case 0x0D: - case 0x0E: - case 0x0F: - case 0x10: - case 0x11: - case 0x12: - case 0x13: - case 0x14: - case 0x15: - case 0x16: - case 0x17: - return sax->number_unsigned(static_cast(current)); - - case 0x18: // Unsigned integer (one-byte uint8_t follows) - { - std::uint8_t number{}; - return get_number(input_format_t::cbor, number) && sax->number_unsigned(number); - } - - case 0x19: // Unsigned integer (two-byte uint16_t follows) - { - std::uint16_t number{}; - return get_number(input_format_t::cbor, number) && sax->number_unsigned(number); - } - - case 0x1A: // Unsigned integer (four-byte uint32_t follows) - { - std::uint32_t number{}; - return get_number(input_format_t::cbor, number) && sax->number_unsigned(number); - } - - case 0x1B: // Unsigned integer (eight-byte uint64_t follows) - { - std::uint64_t number{}; - return get_number(input_format_t::cbor, number) && sax->number_unsigned(number); - } - - // Negative integer -1-0x00..-1-0x17 (-1..-24) - case 0x20: - case 0x21: - case 0x22: - case 0x23: - case 0x24: - case 0x25: - case 0x26: - case 0x27: - case 0x28: - case 0x29: - case 0x2A: - case 0x2B: - case 0x2C: - case 0x2D: - case 0x2E: - case 0x2F: - case 0x30: - case 0x31: - case 0x32: - case 0x33: - case 0x34: - case 0x35: - case 0x36: - case 0x37: - return sax->number_integer(static_cast(0x20 - 1 - current)); - - case 0x38: // Negative integer (one-byte uint8_t follows) - { - std::uint8_t number{}; - return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast(-1) - number); - } - - case 0x39: // Negative integer -1-n (two-byte uint16_t follows) - { - std::uint16_t number{}; - return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast(-1) - number); - } - - case 0x3A: // Negative integer -1-n (four-byte uint32_t follows) - { - std::uint32_t number{}; - return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast(-1) - number); - } - - case 0x3B: // Negative integer -1-n (eight-byte uint64_t follows) - { - std::uint64_t number{}; - return get_number(input_format_t::cbor, number) && sax->number_integer(static_cast(-1) - - static_cast(number)); - } - - // Binary data (0x00..0x17 bytes follow) - case 0x40: - case 0x41: - case 0x42: - case 0x43: - case 0x44: - case 0x45: - case 0x46: - case 0x47: - case 0x48: - case 0x49: - case 0x4A: - case 0x4B: - case 0x4C: - case 0x4D: - case 0x4E: - case 0x4F: - case 0x50: - case 0x51: - case 0x52: - case 0x53: - case 0x54: - case 0x55: - case 0x56: - case 0x57: - case 0x58: // Binary data (one-byte uint8_t for n follows) - case 0x59: // Binary data (two-byte uint16_t for n follow) - case 0x5A: // Binary data (four-byte uint32_t for n follow) - case 0x5B: // Binary data (eight-byte uint64_t for n follow) - case 0x5F: // Binary data (indefinite length) - { - binary_t b; - return get_cbor_binary(b) && sax->binary(b); - } - - // UTF-8 string (0x00..0x17 bytes follow) - case 0x60: - case 0x61: - case 0x62: - case 0x63: - case 0x64: - case 0x65: - case 0x66: - case 0x67: - case 0x68: - case 0x69: - case 0x6A: - case 0x6B: - case 0x6C: - case 0x6D: - case 0x6E: - case 0x6F: - case 0x70: - case 0x71: - case 0x72: - case 0x73: - case 0x74: - case 0x75: - case 0x76: - case 0x77: - case 0x78: // UTF-8 string (one-byte uint8_t for n follows) - case 0x79: // UTF-8 string (two-byte uint16_t for n follow) - case 0x7A: // UTF-8 string (four-byte uint32_t for n follow) - case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow) - case 0x7F: // UTF-8 string (indefinite length) - { - string_t s; - return get_cbor_string(s) && sax->string(s); - } - - // array (0x00..0x17 data items follow) - case 0x80: - case 0x81: - case 0x82: - case 0x83: - case 0x84: - case 0x85: - case 0x86: - case 0x87: - case 0x88: - case 0x89: - case 0x8A: - case 0x8B: - case 0x8C: - case 0x8D: - case 0x8E: - case 0x8F: - case 0x90: - case 0x91: - case 0x92: - case 0x93: - case 0x94: - case 0x95: - case 0x96: - case 0x97: - return get_cbor_array(static_cast(static_cast(current) & 0x1Fu), tag_handler); - - case 0x98: // array (one-byte uint8_t for n follows) - { - std::uint8_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast(len), tag_handler); - } - - case 0x99: // array (two-byte uint16_t for n follow) - { - std::uint16_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast(len), tag_handler); - } - - case 0x9A: // array (four-byte uint32_t for n follow) - { - std::uint32_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_array(static_cast(len), tag_handler); - } - - case 0x9B: // array (eight-byte uint64_t for n follow) - { - std::uint64_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_array(detail::conditional_static_cast(len), tag_handler); - } - - case 0x9F: // array (indefinite length) - return get_cbor_array(std::size_t(-1), tag_handler); - - // map (0x00..0x17 pairs of data items follow) - case 0xA0: - case 0xA1: - case 0xA2: - case 0xA3: - case 0xA4: - case 0xA5: - case 0xA6: - case 0xA7: - case 0xA8: - case 0xA9: - case 0xAA: - case 0xAB: - case 0xAC: - case 0xAD: - case 0xAE: - case 0xAF: - case 0xB0: - case 0xB1: - case 0xB2: - case 0xB3: - case 0xB4: - case 0xB5: - case 0xB6: - case 0xB7: - return get_cbor_object(static_cast(static_cast(current) & 0x1Fu), tag_handler); - - case 0xB8: // map (one-byte uint8_t for n follows) - { - std::uint8_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast(len), tag_handler); - } - - case 0xB9: // map (two-byte uint16_t for n follow) - { - std::uint16_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast(len), tag_handler); - } - - case 0xBA: // map (four-byte uint32_t for n follow) - { - std::uint32_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_object(static_cast(len), tag_handler); - } - - case 0xBB: // map (eight-byte uint64_t for n follow) - { - std::uint64_t len{}; - return get_number(input_format_t::cbor, len) && get_cbor_object(detail::conditional_static_cast(len), tag_handler); - } - - case 0xBF: // map (indefinite length) - return get_cbor_object(std::size_t(-1), tag_handler); - - case 0xC6: // tagged item - case 0xC7: - case 0xC8: - case 0xC9: - case 0xCA: - case 0xCB: - case 0xCC: - case 0xCD: - case 0xCE: - case 0xCF: - case 0xD0: - case 0xD1: - case 0xD2: - case 0xD3: - case 0xD4: - case 0xD8: // tagged item (1 bytes follow) - case 0xD9: // tagged item (2 bytes follow) - case 0xDA: // tagged item (4 bytes follow) - case 0xDB: // tagged item (8 bytes follow) - { - switch (tag_handler) - { - case cbor_tag_handler_t::error: - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); - } - - case cbor_tag_handler_t::ignore: - { - // ignore binary subtype - switch (current) - { - case 0xD8: - { - std::uint8_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); - break; - } - case 0xD9: - { - std::uint16_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); - break; - } - case 0xDA: - { - std::uint32_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); - break; - } - case 0xDB: - { - std::uint64_t subtype_to_ignore{}; - get_number(input_format_t::cbor, subtype_to_ignore); - break; - } - default: - break; - } - return parse_cbor_internal(true, tag_handler); - } - - case cbor_tag_handler_t::store: - { - binary_t b; - // use binary subtype and store in binary container - switch (current) - { - case 0xD8: - { - std::uint8_t subtype{}; - get_number(input_format_t::cbor, subtype); - b.set_subtype(detail::conditional_static_cast(subtype)); - break; - } - case 0xD9: - { - std::uint16_t subtype{}; - get_number(input_format_t::cbor, subtype); - b.set_subtype(detail::conditional_static_cast(subtype)); - break; - } - case 0xDA: - { - std::uint32_t subtype{}; - get_number(input_format_t::cbor, subtype); - b.set_subtype(detail::conditional_static_cast(subtype)); - break; - } - case 0xDB: - { - std::uint64_t subtype{}; - get_number(input_format_t::cbor, subtype); - b.set_subtype(detail::conditional_static_cast(subtype)); - break; - } - default: - return parse_cbor_internal(true, tag_handler); - } - get(); - return get_cbor_binary(b) && sax->binary(b); - } - - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - return false; // LCOV_EXCL_LINE - } - } - - case 0xF4: // false - return sax->boolean(false); - - case 0xF5: // true - return sax->boolean(true); - - case 0xF6: // null - return sax->null(); - - case 0xF9: // Half-Precision Float (two-byte IEEE 754) - { - const auto byte1_raw = get(); - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number"))) - { - return false; - } - const auto byte2_raw = get(); - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "number"))) - { - return false; - } - - const auto byte1 = static_cast(byte1_raw); - const auto byte2 = static_cast(byte2_raw); - - // code from RFC 7049, Appendix D, Figure 3: - // As half-precision floating-point numbers were only added - // to IEEE 754 in 2008, today's programming platforms often - // still only have limited support for them. It is very - // easy to include at least decoding support for them even - // without such support. An example of a small decoder for - // half-precision floating-point numbers in the C language - // is shown in Fig. 3. - const auto half = static_cast((byte1 << 8u) + byte2); - const double val = [&half] - { - const int exp = (half >> 10u) & 0x1Fu; - const unsigned int mant = half & 0x3FFu; - JSON_ASSERT(0 <= exp&& exp <= 32); - JSON_ASSERT(mant <= 1024); - switch (exp) - { - case 0: - return std::ldexp(mant, -24); - case 31: - return (mant == 0) - ? std::numeric_limits::infinity() - : std::numeric_limits::quiet_NaN(); - default: - return std::ldexp(mant + 1024, exp - 25); - } - }(); - return sax->number_float((half & 0x8000u) != 0 - ? static_cast(-val) - : static_cast(val), ""); - } - - case 0xFA: // Single-Precision Float (four-byte IEEE 754) - { - float number{}; - return get_number(input_format_t::cbor, number) && sax->number_float(static_cast(number), ""); - } - - case 0xFB: // Double-Precision Float (eight-byte IEEE 754) - { - double number{}; - return get_number(input_format_t::cbor, number) && sax->number_float(static_cast(number), ""); - } - - default: // anything else (0xFF is handled inside the other types) - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::cbor, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); - } - } - } - - /*! - @brief reads a CBOR string - - This function first reads starting bytes to determine the expected - string length and then copies this number of bytes into a string. - Additionally, CBOR's strings with indefinite lengths are supported. - - @param[out] result created string - - @return whether string creation completed - */ - bool get_cbor_string(string_t& result) - { - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string"))) - { - return false; - } - - switch (current) - { - // UTF-8 string (0x00..0x17 bytes follow) - case 0x60: - case 0x61: - case 0x62: - case 0x63: - case 0x64: - case 0x65: - case 0x66: - case 0x67: - case 0x68: - case 0x69: - case 0x6A: - case 0x6B: - case 0x6C: - case 0x6D: - case 0x6E: - case 0x6F: - case 0x70: - case 0x71: - case 0x72: - case 0x73: - case 0x74: - case 0x75: - case 0x76: - case 0x77: - { - return get_string(input_format_t::cbor, static_cast(current) & 0x1Fu, result); - } - - case 0x78: // UTF-8 string (one-byte uint8_t for n follows) - { - std::uint8_t len{}; - return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result); - } - - case 0x79: // UTF-8 string (two-byte uint16_t for n follow) - { - std::uint16_t len{}; - return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result); - } - - case 0x7A: // UTF-8 string (four-byte uint32_t for n follow) - { - std::uint32_t len{}; - return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result); - } - - case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow) - { - std::uint64_t len{}; - return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result); - } - - case 0x7F: // UTF-8 string (indefinite length) - { - while (get() != 0xFF) - { - string_t chunk; - if (!get_cbor_string(chunk)) - { - return false; - } - result.append(chunk); - } - return true; - } - - default: - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x" + last_token, "string"), BasicJsonType())); - } - } - } - - /*! - @brief reads a CBOR byte array - - This function first reads starting bytes to determine the expected - byte array length and then copies this number of bytes into the byte array. - Additionally, CBOR's byte arrays with indefinite lengths are supported. - - @param[out] result created byte array - - @return whether byte array creation completed - */ - bool get_cbor_binary(binary_t& result) - { - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary"))) - { - return false; - } - - switch (current) - { - // Binary data (0x00..0x17 bytes follow) - case 0x40: - case 0x41: - case 0x42: - case 0x43: - case 0x44: - case 0x45: - case 0x46: - case 0x47: - case 0x48: - case 0x49: - case 0x4A: - case 0x4B: - case 0x4C: - case 0x4D: - case 0x4E: - case 0x4F: - case 0x50: - case 0x51: - case 0x52: - case 0x53: - case 0x54: - case 0x55: - case 0x56: - case 0x57: - { - return get_binary(input_format_t::cbor, static_cast(current) & 0x1Fu, result); - } - - case 0x58: // Binary data (one-byte uint8_t for n follows) - { - std::uint8_t len{}; - return get_number(input_format_t::cbor, len) && - get_binary(input_format_t::cbor, len, result); - } - - case 0x59: // Binary data (two-byte uint16_t for n follow) - { - std::uint16_t len{}; - return get_number(input_format_t::cbor, len) && - get_binary(input_format_t::cbor, len, result); - } - - case 0x5A: // Binary data (four-byte uint32_t for n follow) - { - std::uint32_t len{}; - return get_number(input_format_t::cbor, len) && - get_binary(input_format_t::cbor, len, result); - } - - case 0x5B: // Binary data (eight-byte uint64_t for n follow) - { - std::uint64_t len{}; - return get_number(input_format_t::cbor, len) && - get_binary(input_format_t::cbor, len, result); - } - - case 0x5F: // Binary data (indefinite length) - { - while (get() != 0xFF) - { - binary_t chunk; - if (!get_cbor_binary(chunk)) - { - return false; - } - result.insert(result.end(), chunk.begin(), chunk.end()); - } - return true; - } - - default: - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::cbor, "expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x" + last_token, "binary"), BasicJsonType())); - } - } - } - - /*! - @param[in] len the length of the array or std::size_t(-1) for an - array of indefinite size - @param[in] tag_handler how CBOR tags should be treated - @return whether array creation completed - */ - bool get_cbor_array(const std::size_t len, - const cbor_tag_handler_t tag_handler) - { - if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len))) - { - return false; - } - - if (len != std::size_t(-1)) - { - for (std::size_t i = 0; i < len; ++i) - { - if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) - { - return false; - } - } - } - else - { - while (get() != 0xFF) - { - if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(false, tag_handler))) - { - return false; - } - } - } - - return sax->end_array(); - } - - /*! - @param[in] len the length of the object or std::size_t(-1) for an - object of indefinite size - @param[in] tag_handler how CBOR tags should be treated - @return whether object creation completed - */ - bool get_cbor_object(const std::size_t len, - const cbor_tag_handler_t tag_handler) - { - if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len))) - { - return false; - } - - if (len != 0) - { - string_t key; - if (len != std::size_t(-1)) - { - for (std::size_t i = 0; i < len; ++i) - { - get(); - if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) - { - return false; - } - - if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) - { - return false; - } - key.clear(); - } - } - else - { - while (get() != 0xFF) - { - if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key))) - { - return false; - } - - if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler))) - { - return false; - } - key.clear(); - } - } - } - - return sax->end_object(); - } - - ///////////// - // MsgPack // - ///////////// - - /*! - @return whether a valid MessagePack value was passed to the SAX parser - */ - bool parse_msgpack_internal() - { - switch (get()) - { - // EOF - case std::char_traits::eof(): - return unexpect_eof(input_format_t::msgpack, "value"); - - // positive fixint - case 0x00: - case 0x01: - case 0x02: - case 0x03: - case 0x04: - case 0x05: - case 0x06: - case 0x07: - case 0x08: - case 0x09: - case 0x0A: - case 0x0B: - case 0x0C: - case 0x0D: - case 0x0E: - case 0x0F: - case 0x10: - case 0x11: - case 0x12: - case 0x13: - case 0x14: - case 0x15: - case 0x16: - case 0x17: - case 0x18: - case 0x19: - case 0x1A: - case 0x1B: - case 0x1C: - case 0x1D: - case 0x1E: - case 0x1F: - case 0x20: - case 0x21: - case 0x22: - case 0x23: - case 0x24: - case 0x25: - case 0x26: - case 0x27: - case 0x28: - case 0x29: - case 0x2A: - case 0x2B: - case 0x2C: - case 0x2D: - case 0x2E: - case 0x2F: - case 0x30: - case 0x31: - case 0x32: - case 0x33: - case 0x34: - case 0x35: - case 0x36: - case 0x37: - case 0x38: - case 0x39: - case 0x3A: - case 0x3B: - case 0x3C: - case 0x3D: - case 0x3E: - case 0x3F: - case 0x40: - case 0x41: - case 0x42: - case 0x43: - case 0x44: - case 0x45: - case 0x46: - case 0x47: - case 0x48: - case 0x49: - case 0x4A: - case 0x4B: - case 0x4C: - case 0x4D: - case 0x4E: - case 0x4F: - case 0x50: - case 0x51: - case 0x52: - case 0x53: - case 0x54: - case 0x55: - case 0x56: - case 0x57: - case 0x58: - case 0x59: - case 0x5A: - case 0x5B: - case 0x5C: - case 0x5D: - case 0x5E: - case 0x5F: - case 0x60: - case 0x61: - case 0x62: - case 0x63: - case 0x64: - case 0x65: - case 0x66: - case 0x67: - case 0x68: - case 0x69: - case 0x6A: - case 0x6B: - case 0x6C: - case 0x6D: - case 0x6E: - case 0x6F: - case 0x70: - case 0x71: - case 0x72: - case 0x73: - case 0x74: - case 0x75: - case 0x76: - case 0x77: - case 0x78: - case 0x79: - case 0x7A: - case 0x7B: - case 0x7C: - case 0x7D: - case 0x7E: - case 0x7F: - return sax->number_unsigned(static_cast(current)); - - // fixmap - case 0x80: - case 0x81: - case 0x82: - case 0x83: - case 0x84: - case 0x85: - case 0x86: - case 0x87: - case 0x88: - case 0x89: - case 0x8A: - case 0x8B: - case 0x8C: - case 0x8D: - case 0x8E: - case 0x8F: - return get_msgpack_object(static_cast(static_cast(current) & 0x0Fu)); - - // fixarray - case 0x90: - case 0x91: - case 0x92: - case 0x93: - case 0x94: - case 0x95: - case 0x96: - case 0x97: - case 0x98: - case 0x99: - case 0x9A: - case 0x9B: - case 0x9C: - case 0x9D: - case 0x9E: - case 0x9F: - return get_msgpack_array(static_cast(static_cast(current) & 0x0Fu)); - - // fixstr - case 0xA0: - case 0xA1: - case 0xA2: - case 0xA3: - case 0xA4: - case 0xA5: - case 0xA6: - case 0xA7: - case 0xA8: - case 0xA9: - case 0xAA: - case 0xAB: - case 0xAC: - case 0xAD: - case 0xAE: - case 0xAF: - case 0xB0: - case 0xB1: - case 0xB2: - case 0xB3: - case 0xB4: - case 0xB5: - case 0xB6: - case 0xB7: - case 0xB8: - case 0xB9: - case 0xBA: - case 0xBB: - case 0xBC: - case 0xBD: - case 0xBE: - case 0xBF: - case 0xD9: // str 8 - case 0xDA: // str 16 - case 0xDB: // str 32 - { - string_t s; - return get_msgpack_string(s) && sax->string(s); - } - - case 0xC0: // nil - return sax->null(); - - case 0xC2: // false - return sax->boolean(false); - - case 0xC3: // true - return sax->boolean(true); - - case 0xC4: // bin 8 - case 0xC5: // bin 16 - case 0xC6: // bin 32 - case 0xC7: // ext 8 - case 0xC8: // ext 16 - case 0xC9: // ext 32 - case 0xD4: // fixext 1 - case 0xD5: // fixext 2 - case 0xD6: // fixext 4 - case 0xD7: // fixext 8 - case 0xD8: // fixext 16 - { - binary_t b; - return get_msgpack_binary(b) && sax->binary(b); - } - - case 0xCA: // float 32 - { - float number{}; - return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast(number), ""); - } - - case 0xCB: // float 64 - { - double number{}; - return get_number(input_format_t::msgpack, number) && sax->number_float(static_cast(number), ""); - } - - case 0xCC: // uint 8 - { - std::uint8_t number{}; - return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number); - } - - case 0xCD: // uint 16 - { - std::uint16_t number{}; - return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number); - } - - case 0xCE: // uint 32 - { - std::uint32_t number{}; - return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number); - } - - case 0xCF: // uint 64 - { - std::uint64_t number{}; - return get_number(input_format_t::msgpack, number) && sax->number_unsigned(number); - } - - case 0xD0: // int 8 - { - std::int8_t number{}; - return get_number(input_format_t::msgpack, number) && sax->number_integer(number); - } - - case 0xD1: // int 16 - { - std::int16_t number{}; - return get_number(input_format_t::msgpack, number) && sax->number_integer(number); - } - - case 0xD2: // int 32 - { - std::int32_t number{}; - return get_number(input_format_t::msgpack, number) && sax->number_integer(number); - } - - case 0xD3: // int 64 - { - std::int64_t number{}; - return get_number(input_format_t::msgpack, number) && sax->number_integer(number); - } - - case 0xDC: // array 16 - { - std::uint16_t len{}; - return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast(len)); - } - - case 0xDD: // array 32 - { - std::uint32_t len{}; - return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast(len)); - } - - case 0xDE: // map 16 - { - std::uint16_t len{}; - return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast(len)); - } - - case 0xDF: // map 32 - { - std::uint32_t len{}; - return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast(len)); - } - - // negative fixint - case 0xE0: - case 0xE1: - case 0xE2: - case 0xE3: - case 0xE4: - case 0xE5: - case 0xE6: - case 0xE7: - case 0xE8: - case 0xE9: - case 0xEA: - case 0xEB: - case 0xEC: - case 0xED: - case 0xEE: - case 0xEF: - case 0xF0: - case 0xF1: - case 0xF2: - case 0xF3: - case 0xF4: - case 0xF5: - case 0xF6: - case 0xF7: - case 0xF8: - case 0xF9: - case 0xFA: - case 0xFB: - case 0xFC: - case 0xFD: - case 0xFE: - case 0xFF: - return sax->number_integer(static_cast(current)); - - default: // anything else - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::msgpack, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); - } - } - } - - /*! - @brief reads a MessagePack string - - This function first reads starting bytes to determine the expected - string length and then copies this number of bytes into a string. - - @param[out] result created string - - @return whether string creation completed - */ - bool get_msgpack_string(string_t& result) - { - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::msgpack, "string"))) - { - return false; - } - - switch (current) - { - // fixstr - case 0xA0: - case 0xA1: - case 0xA2: - case 0xA3: - case 0xA4: - case 0xA5: - case 0xA6: - case 0xA7: - case 0xA8: - case 0xA9: - case 0xAA: - case 0xAB: - case 0xAC: - case 0xAD: - case 0xAE: - case 0xAF: - case 0xB0: - case 0xB1: - case 0xB2: - case 0xB3: - case 0xB4: - case 0xB5: - case 0xB6: - case 0xB7: - case 0xB8: - case 0xB9: - case 0xBA: - case 0xBB: - case 0xBC: - case 0xBD: - case 0xBE: - case 0xBF: - { - return get_string(input_format_t::msgpack, static_cast(current) & 0x1Fu, result); - } - - case 0xD9: // str 8 - { - std::uint8_t len{}; - return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result); - } - - case 0xDA: // str 16 - { - std::uint16_t len{}; - return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result); - } - - case 0xDB: // str 32 - { - std::uint32_t len{}; - return get_number(input_format_t::msgpack, len) && get_string(input_format_t::msgpack, len, result); - } - - default: - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::msgpack, "expected length specification (0xA0-0xBF, 0xD9-0xDB); last byte: 0x" + last_token, "string"), BasicJsonType())); - } - } - } - - /*! - @brief reads a MessagePack byte array - - This function first reads starting bytes to determine the expected - byte array length and then copies this number of bytes into a byte array. - - @param[out] result created byte array - - @return whether byte array creation completed - */ - bool get_msgpack_binary(binary_t& result) - { - // helper function to set the subtype - auto assign_and_return_true = [&result](std::int8_t subtype) - { - result.set_subtype(static_cast(subtype)); - return true; - }; - - switch (current) - { - case 0xC4: // bin 8 - { - std::uint8_t len{}; - return get_number(input_format_t::msgpack, len) && - get_binary(input_format_t::msgpack, len, result); - } - - case 0xC5: // bin 16 - { - std::uint16_t len{}; - return get_number(input_format_t::msgpack, len) && - get_binary(input_format_t::msgpack, len, result); - } - - case 0xC6: // bin 32 - { - std::uint32_t len{}; - return get_number(input_format_t::msgpack, len) && - get_binary(input_format_t::msgpack, len, result); - } - - case 0xC7: // ext 8 - { - std::uint8_t len{}; - std::int8_t subtype{}; - return get_number(input_format_t::msgpack, len) && - get_number(input_format_t::msgpack, subtype) && - get_binary(input_format_t::msgpack, len, result) && - assign_and_return_true(subtype); - } - - case 0xC8: // ext 16 - { - std::uint16_t len{}; - std::int8_t subtype{}; - return get_number(input_format_t::msgpack, len) && - get_number(input_format_t::msgpack, subtype) && - get_binary(input_format_t::msgpack, len, result) && - assign_and_return_true(subtype); - } - - case 0xC9: // ext 32 - { - std::uint32_t len{}; - std::int8_t subtype{}; - return get_number(input_format_t::msgpack, len) && - get_number(input_format_t::msgpack, subtype) && - get_binary(input_format_t::msgpack, len, result) && - assign_and_return_true(subtype); - } - - case 0xD4: // fixext 1 - { - std::int8_t subtype{}; - return get_number(input_format_t::msgpack, subtype) && - get_binary(input_format_t::msgpack, 1, result) && - assign_and_return_true(subtype); - } - - case 0xD5: // fixext 2 - { - std::int8_t subtype{}; - return get_number(input_format_t::msgpack, subtype) && - get_binary(input_format_t::msgpack, 2, result) && - assign_and_return_true(subtype); - } - - case 0xD6: // fixext 4 - { - std::int8_t subtype{}; - return get_number(input_format_t::msgpack, subtype) && - get_binary(input_format_t::msgpack, 4, result) && - assign_and_return_true(subtype); - } - - case 0xD7: // fixext 8 - { - std::int8_t subtype{}; - return get_number(input_format_t::msgpack, subtype) && - get_binary(input_format_t::msgpack, 8, result) && - assign_and_return_true(subtype); - } - - case 0xD8: // fixext 16 - { - std::int8_t subtype{}; - return get_number(input_format_t::msgpack, subtype) && - get_binary(input_format_t::msgpack, 16, result) && - assign_and_return_true(subtype); - } - - default: // LCOV_EXCL_LINE - return false; // LCOV_EXCL_LINE - } - } - - /*! - @param[in] len the length of the array - @return whether array creation completed - */ - bool get_msgpack_array(const std::size_t len) - { - if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len))) - { - return false; - } - - for (std::size_t i = 0; i < len; ++i) - { - if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal())) - { - return false; - } - } - - return sax->end_array(); - } - - /*! - @param[in] len the length of the object - @return whether object creation completed - */ - bool get_msgpack_object(const std::size_t len) - { - if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len))) - { - return false; - } - - string_t key; - for (std::size_t i = 0; i < len; ++i) - { - get(); - if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key))) - { - return false; - } - - if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal())) - { - return false; - } - key.clear(); - } - - return sax->end_object(); - } - - //////////// - // UBJSON // - //////////// - - /*! - @param[in] get_char whether a new character should be retrieved from the - input (true, default) or whether the last read - character should be considered instead - - @return whether a valid UBJSON value was passed to the SAX parser - */ - bool parse_ubjson_internal(const bool get_char = true) - { - return get_ubjson_value(get_char ? get_ignore_noop() : current); - } - - /*! - @brief reads a UBJSON string - - This function is either called after reading the 'S' byte explicitly - indicating a string, or in case of an object key where the 'S' byte can be - left out. - - @param[out] result created string - @param[in] get_char whether a new character should be retrieved from the - input (true, default) or whether the last read - character should be considered instead - - @return whether string creation completed - */ - bool get_ubjson_string(string_t& result, const bool get_char = true) - { - if (get_char) - { - get(); // TODO(niels): may we ignore N here? - } - - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "value"))) - { - return false; - } - - switch (current) - { - case 'U': - { - std::uint8_t len{}; - return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result); - } - - case 'i': - { - std::int8_t len{}; - return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result); - } - - case 'I': - { - std::int16_t len{}; - return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result); - } - - case 'l': - { - std::int32_t len{}; - return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result); - } - - case 'L': - { - std::int64_t len{}; - return get_number(input_format_t::ubjson, len) && get_string(input_format_t::ubjson, len, result); - } - - default: - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L); last byte: 0x" + last_token, "string"), BasicJsonType())); - } - } - - /*! - @param[out] result determined size - @return whether size determination completed - */ - bool get_ubjson_size_value(std::size_t& result) - { - switch (get_ignore_noop()) - { - case 'U': - { - std::uint8_t number{}; - if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number))) - { - return false; - } - result = static_cast(number); - return true; - } - - case 'i': - { - std::int8_t number{}; - if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number))) - { - return false; - } - result = static_cast(number); // NOLINT(bugprone-signed-char-misuse,cert-str34-c): number is not a char - return true; - } - - case 'I': - { - std::int16_t number{}; - if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number))) - { - return false; - } - result = static_cast(number); - return true; - } - - case 'l': - { - std::int32_t number{}; - if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number))) - { - return false; - } - result = static_cast(number); - return true; - } - - case 'L': - { - std::int64_t number{}; - if (JSON_HEDLEY_UNLIKELY(!get_number(input_format_t::ubjson, number))) - { - return false; - } - result = static_cast(number); - return true; - } - - default: - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "expected length type specification (U, i, I, l, L) after '#'; last byte: 0x" + last_token, "size"), BasicJsonType())); - } - } - } - - /*! - @brief determine the type and size for a container - - In the optimized UBJSON format, a type and a size can be provided to allow - for a more compact representation. - - @param[out] result pair of the size and the type - - @return whether pair creation completed - */ - bool get_ubjson_size_type(std::pair& result) - { - result.first = string_t::npos; // size - result.second = 0; // type - - get_ignore_noop(); - - if (current == '$') - { - result.second = get(); // must not ignore 'N', because 'N' maybe the type - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "type"))) - { - return false; - } - - get_ignore_noop(); - if (JSON_HEDLEY_UNLIKELY(current != '#')) - { - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "value"))) - { - return false; - } - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "expected '#' after type information; last byte: 0x" + last_token, "size"), BasicJsonType())); - } - - return get_ubjson_size_value(result.first); - } - - if (current == '#') - { - return get_ubjson_size_value(result.first); - } - - return true; - } - - /*! - @param prefix the previously read or set type prefix - @return whether value creation completed - */ - bool get_ubjson_value(const char_int_type prefix) - { - switch (prefix) - { - case std::char_traits::eof(): // EOF - return unexpect_eof(input_format_t::ubjson, "value"); - - case 'T': // true - return sax->boolean(true); - case 'F': // false - return sax->boolean(false); - - case 'Z': // null - return sax->null(); - - case 'U': - { - std::uint8_t number{}; - return get_number(input_format_t::ubjson, number) && sax->number_unsigned(number); - } - - case 'i': - { - std::int8_t number{}; - return get_number(input_format_t::ubjson, number) && sax->number_integer(number); - } - - case 'I': - { - std::int16_t number{}; - return get_number(input_format_t::ubjson, number) && sax->number_integer(number); - } - - case 'l': - { - std::int32_t number{}; - return get_number(input_format_t::ubjson, number) && sax->number_integer(number); - } - - case 'L': - { - std::int64_t number{}; - return get_number(input_format_t::ubjson, number) && sax->number_integer(number); - } - - case 'd': - { - float number{}; - return get_number(input_format_t::ubjson, number) && sax->number_float(static_cast(number), ""); - } - - case 'D': - { - double number{}; - return get_number(input_format_t::ubjson, number) && sax->number_float(static_cast(number), ""); - } - - case 'H': - { - return get_ubjson_high_precision_number(); - } - - case 'C': // char - { - get(); - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "char"))) - { - return false; - } - if (JSON_HEDLEY_UNLIKELY(current > 127)) - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(113, chars_read, exception_message(input_format_t::ubjson, "byte after 'C' must be in range 0x00..0x7F; last byte: 0x" + last_token, "char"), BasicJsonType())); - } - string_t s(1, static_cast(current)); - return sax->string(s); - } - - case 'S': // string - { - string_t s; - return get_ubjson_string(s) && sax->string(s); - } - - case '[': // array - return get_ubjson_array(); - - case '{': // object - return get_ubjson_object(); - - default: // anything else - { - auto last_token = get_token_string(); - return sax->parse_error(chars_read, last_token, parse_error::create(112, chars_read, exception_message(input_format_t::ubjson, "invalid byte: 0x" + last_token, "value"), BasicJsonType())); - } - } - } - - /*! - @return whether array creation completed - */ - bool get_ubjson_array() - { - std::pair size_and_type; - if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type))) - { - return false; - } - - if (size_and_type.first != string_t::npos) - { - if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first))) - { - return false; - } - - if (size_and_type.second != 0) - { - if (size_and_type.second != 'N') - { - for (std::size_t i = 0; i < size_and_type.first; ++i) - { - if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second))) - { - return false; - } - } - } - } - else - { - for (std::size_t i = 0; i < size_and_type.first; ++i) - { - if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal())) - { - return false; - } - } - } - } - else - { - if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::size_t(-1)))) - { - return false; - } - - while (current != ']') - { - if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal(false))) - { - return false; - } - get_ignore_noop(); - } - } - - return sax->end_array(); - } - - /*! - @return whether object creation completed - */ - bool get_ubjson_object() - { - std::pair size_and_type; - if (JSON_HEDLEY_UNLIKELY(!get_ubjson_size_type(size_and_type))) - { - return false; - } - - string_t key; - if (size_and_type.first != string_t::npos) - { - if (JSON_HEDLEY_UNLIKELY(!sax->start_object(size_and_type.first))) - { - return false; - } - - if (size_and_type.second != 0) - { - for (std::size_t i = 0; i < size_and_type.first; ++i) - { - if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key))) - { - return false; - } - if (JSON_HEDLEY_UNLIKELY(!get_ubjson_value(size_and_type.second))) - { - return false; - } - key.clear(); - } - } - else - { - for (std::size_t i = 0; i < size_and_type.first; ++i) - { - if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key) || !sax->key(key))) - { - return false; - } - if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal())) - { - return false; - } - key.clear(); - } - } - } - else - { - if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::size_t(-1)))) - { - return false; - } - - while (current != '}') - { - if (JSON_HEDLEY_UNLIKELY(!get_ubjson_string(key, false) || !sax->key(key))) - { - return false; - } - if (JSON_HEDLEY_UNLIKELY(!parse_ubjson_internal())) - { - return false; - } - get_ignore_noop(); - key.clear(); - } - } - - return sax->end_object(); - } - - // Note, no reader for UBJSON binary types is implemented because they do - // not exist - - bool get_ubjson_high_precision_number() - { - // get size of following number string - std::size_t size{}; - auto res = get_ubjson_size_value(size); - if (JSON_HEDLEY_UNLIKELY(!res)) - { - return res; - } - - // get number string - std::vector number_vector; - for (std::size_t i = 0; i < size; ++i) - { - get(); - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::ubjson, "number"))) - { - return false; - } - number_vector.push_back(static_cast(current)); - } - - // parse number string - using ia_type = decltype(detail::input_adapter(number_vector)); - auto number_lexer = detail::lexer(detail::input_adapter(number_vector), false); - const auto result_number = number_lexer.scan(); - const auto number_string = number_lexer.get_token_string(); - const auto result_remainder = number_lexer.scan(); - - using token_type = typename detail::lexer_base::token_type; - - if (JSON_HEDLEY_UNLIKELY(result_remainder != token_type::end_of_input)) - { - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType())); - } - - switch (result_number) - { - case token_type::value_integer: - return sax->number_integer(number_lexer.get_number_integer()); - case token_type::value_unsigned: - return sax->number_unsigned(number_lexer.get_number_unsigned()); - case token_type::value_float: - return sax->number_float(number_lexer.get_number_float(), std::move(number_string)); - case token_type::uninitialized: - case token_type::literal_true: - case token_type::literal_false: - case token_type::literal_null: - case token_type::value_string: - case token_type::begin_array: - case token_type::begin_object: - case token_type::end_array: - case token_type::end_object: - case token_type::name_separator: - case token_type::value_separator: - case token_type::parse_error: - case token_type::end_of_input: - case token_type::literal_or_value: - default: - return sax->parse_error(chars_read, number_string, parse_error::create(115, chars_read, exception_message(input_format_t::ubjson, "invalid number text: " + number_lexer.get_token_string(), "high-precision number"), BasicJsonType())); - } - } - - /////////////////////// - // Utility functions // - /////////////////////// - - /*! - @brief get next character from the input - - This function provides the interface to the used input adapter. It does - not throw in case the input reached EOF, but returns a -'ve valued - `std::char_traits::eof()` in that case. - - @return character read from the input - */ - char_int_type get() - { - ++chars_read; - return current = ia.get_character(); - } - - /*! - @return character read from the input after ignoring all 'N' entries - */ - char_int_type get_ignore_noop() - { - do - { - get(); - } - while (current == 'N'); - - return current; - } - - /* - @brief read a number from the input - - @tparam NumberType the type of the number - @param[in] format the current format (for diagnostics) - @param[out] result number of type @a NumberType - - @return whether conversion completed - - @note This function needs to respect the system's endianess, because - bytes in CBOR, MessagePack, and UBJSON are stored in network order - (big endian) and therefore need reordering on little endian systems. - */ - template - bool get_number(const input_format_t format, NumberType& result) - { - // step 1: read input into array with system's byte order - std::array vec{}; - for (std::size_t i = 0; i < sizeof(NumberType); ++i) - { - get(); - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "number"))) - { - return false; - } - - // reverse byte order prior to conversion if necessary - if (is_little_endian != InputIsLittleEndian) - { - vec[sizeof(NumberType) - i - 1] = static_cast(current); - } - else - { - vec[i] = static_cast(current); // LCOV_EXCL_LINE - } - } - - // step 2: convert array into number of type T and return - std::memcpy(&result, vec.data(), sizeof(NumberType)); - return true; - } - - /*! - @brief create a string by reading characters from the input - - @tparam NumberType the type of the number - @param[in] format the current format (for diagnostics) - @param[in] len number of characters to read - @param[out] result string created by reading @a len bytes - - @return whether string creation completed - - @note We can not reserve @a len bytes for the result, because @a len - may be too large. Usually, @ref unexpect_eof() detects the end of - the input before we run out of string memory. - */ - template - bool get_string(const input_format_t format, - const NumberType len, - string_t& result) - { - bool success = true; - for (NumberType i = 0; i < len; i++) - { - get(); - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string"))) - { - success = false; - break; - } - result.push_back(static_cast(current)); - } - return success; - } - - /*! - @brief create a byte array by reading bytes from the input - - @tparam NumberType the type of the number - @param[in] format the current format (for diagnostics) - @param[in] len number of bytes to read - @param[out] result byte array created by reading @a len bytes - - @return whether byte array creation completed - - @note We can not reserve @a len bytes for the result, because @a len - may be too large. Usually, @ref unexpect_eof() detects the end of - the input before we run out of memory. - */ - template - bool get_binary(const input_format_t format, - const NumberType len, - binary_t& result) - { - bool success = true; - for (NumberType i = 0; i < len; i++) - { - get(); - if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary"))) - { - success = false; - break; - } - result.push_back(static_cast(current)); - } - return success; - } - - /*! - @param[in] format the current format (for diagnostics) - @param[in] context further context information (for diagnostics) - @return whether the last read character is not EOF - */ - JSON_HEDLEY_NON_NULL(3) - bool unexpect_eof(const input_format_t format, const char* context) const - { - if (JSON_HEDLEY_UNLIKELY(current == std::char_traits::eof())) - { - return sax->parse_error(chars_read, "", - parse_error::create(110, chars_read, exception_message(format, "unexpected end of input", context), BasicJsonType())); - } - return true; - } - - /*! - @return a string representation of the last read byte - */ - std::string get_token_string() const - { - std::array cr{{}}; - (std::snprintf)(cr.data(), cr.size(), "%.2hhX", static_cast(current)); // NOLINT(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - return std::string{cr.data()}; - } - - /*! - @param[in] format the current format - @param[in] detail a detailed error message - @param[in] context further context information - @return a message string to use in the parse_error exceptions - */ - std::string exception_message(const input_format_t format, - const std::string& detail, - const std::string& context) const - { - std::string error_msg = "syntax error while parsing "; - - switch (format) - { - case input_format_t::cbor: - error_msg += "CBOR"; - break; - - case input_format_t::msgpack: - error_msg += "MessagePack"; - break; - - case input_format_t::ubjson: - error_msg += "UBJSON"; - break; - - case input_format_t::bson: - error_msg += "BSON"; - break; - - case input_format_t::json: // LCOV_EXCL_LINE - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - } - - return error_msg + " " + context + ": " + detail; - } - - private: - /// input adapter - InputAdapterType ia; - - /// the current character - char_int_type current = std::char_traits::eof(); - - /// the number of characters read - std::size_t chars_read = 0; - - /// whether we can assume little endianess - const bool is_little_endian = little_endianess(); - - /// the SAX parser - json_sax_t* sax = nullptr; -}; -} // namespace detail -} // namespace nlohmann - -// #include - -// #include - -// #include - - -#include // isfinite -#include // uint8_t -#include // function -#include // string -#include // move -#include // vector - -// #include - -// #include - -// #include - -// #include - -// #include - -// #include - -// #include - - -namespace nlohmann -{ -namespace detail -{ -//////////// -// parser // -//////////// - -enum class parse_event_t : std::uint8_t -{ - /// the parser read `{` and started to process a JSON object - object_start, - /// the parser read `}` and finished processing a JSON object - object_end, - /// the parser read `[` and started to process a JSON array - array_start, - /// the parser read `]` and finished processing a JSON array - array_end, - /// the parser read a key of a value in an object - key, - /// the parser finished reading a JSON value - value -}; - -template -using parser_callback_t = - std::function; - -/*! -@brief syntax analysis - -This class implements a recursive descent parser. -*/ -template -class parser -{ - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using number_float_t = typename BasicJsonType::number_float_t; - using string_t = typename BasicJsonType::string_t; - using lexer_t = lexer; - using token_type = typename lexer_t::token_type; - - public: - /// a parser reading from an input adapter - explicit parser(InputAdapterType&& adapter, - const parser_callback_t cb = nullptr, - const bool allow_exceptions_ = true, - const bool skip_comments = false) - : callback(cb) - , m_lexer(std::move(adapter), skip_comments) - , allow_exceptions(allow_exceptions_) - { - // read first token - get_token(); - } - - /*! - @brief public parser interface - - @param[in] strict whether to expect the last token to be EOF - @param[in,out] result parsed JSON value - - @throw parse_error.101 in case of an unexpected token - @throw parse_error.102 if to_unicode fails or surrogate error - @throw parse_error.103 if to_unicode fails - */ - void parse(const bool strict, BasicJsonType& result) - { - if (callback) - { - json_sax_dom_callback_parser sdp(result, callback, allow_exceptions); - sax_parse_internal(&sdp); - - // in strict mode, input must be completely read - if (strict && (get_token() != token_type::end_of_input)) - { - sdp.parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), - exception_message(token_type::end_of_input, "value"), BasicJsonType())); - } - - // in case of an error, return discarded value - if (sdp.is_errored()) - { - result = value_t::discarded; - return; - } - - // set top-level value to null if it was discarded by the callback - // function - if (result.is_discarded()) - { - result = nullptr; - } - } - else - { - json_sax_dom_parser sdp(result, allow_exceptions); - sax_parse_internal(&sdp); - - // in strict mode, input must be completely read - if (strict && (get_token() != token_type::end_of_input)) - { - sdp.parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType())); - } - - // in case of an error, return discarded value - if (sdp.is_errored()) - { - result = value_t::discarded; - return; - } - } - - result.assert_invariant(); - } - - /*! - @brief public accept interface - - @param[in] strict whether to expect the last token to be EOF - @return whether the input is a proper JSON text - */ - bool accept(const bool strict = true) - { - json_sax_acceptor sax_acceptor; - return sax_parse(&sax_acceptor, strict); - } - - template - JSON_HEDLEY_NON_NULL(2) - bool sax_parse(SAX* sax, const bool strict = true) - { - (void)detail::is_sax_static_asserts {}; - const bool result = sax_parse_internal(sax); - - // strict mode: next byte must be EOF - if (result && strict && (get_token() != token_type::end_of_input)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_of_input, "value"), BasicJsonType())); - } - - return result; - } - - private: - template - JSON_HEDLEY_NON_NULL(2) - bool sax_parse_internal(SAX* sax) - { - // stack to remember the hierarchy of structured values we are parsing - // true = array; false = object - std::vector states; - // value to avoid a goto (see comment where set to true) - bool skip_to_state_evaluation = false; - - while (true) - { - if (!skip_to_state_evaluation) - { - // invariant: get_token() was called before each iteration - switch (last_token) - { - case token_type::begin_object: - { - if (JSON_HEDLEY_UNLIKELY(!sax->start_object(std::size_t(-1)))) - { - return false; - } - - // closing } -> we are done - if (get_token() == token_type::end_object) - { - if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) - { - return false; - } - break; - } - - // parse key - if (JSON_HEDLEY_UNLIKELY(last_token != token_type::value_string)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType())); - } - if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) - { - return false; - } - - // parse separator (:) - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType())); - } - - // remember we are now inside an object - states.push_back(false); - - // parse values - get_token(); - continue; - } - - case token_type::begin_array: - { - if (JSON_HEDLEY_UNLIKELY(!sax->start_array(std::size_t(-1)))) - { - return false; - } - - // closing ] -> we are done - if (get_token() == token_type::end_array) - { - if (JSON_HEDLEY_UNLIKELY(!sax->end_array())) - { - return false; - } - break; - } - - // remember we are now inside an array - states.push_back(true); - - // parse values (no need to call get_token) - continue; - } - - case token_type::value_float: - { - const auto res = m_lexer.get_number_float(); - - if (JSON_HEDLEY_UNLIKELY(!std::isfinite(res))) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - out_of_range::create(406, "number overflow parsing '" + m_lexer.get_token_string() + "'", BasicJsonType())); - } - - if (JSON_HEDLEY_UNLIKELY(!sax->number_float(res, m_lexer.get_string()))) - { - return false; - } - - break; - } - - case token_type::literal_false: - { - if (JSON_HEDLEY_UNLIKELY(!sax->boolean(false))) - { - return false; - } - break; - } - - case token_type::literal_null: - { - if (JSON_HEDLEY_UNLIKELY(!sax->null())) - { - return false; - } - break; - } - - case token_type::literal_true: - { - if (JSON_HEDLEY_UNLIKELY(!sax->boolean(true))) - { - return false; - } - break; - } - - case token_type::value_integer: - { - if (JSON_HEDLEY_UNLIKELY(!sax->number_integer(m_lexer.get_number_integer()))) - { - return false; - } - break; - } - - case token_type::value_string: - { - if (JSON_HEDLEY_UNLIKELY(!sax->string(m_lexer.get_string()))) - { - return false; - } - break; - } - - case token_type::value_unsigned: - { - if (JSON_HEDLEY_UNLIKELY(!sax->number_unsigned(m_lexer.get_number_unsigned()))) - { - return false; - } - break; - } - - case token_type::parse_error: - { - // using "uninitialized" to avoid "expected" message - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::uninitialized, "value"), BasicJsonType())); - } - - case token_type::uninitialized: - case token_type::end_array: - case token_type::end_object: - case token_type::name_separator: - case token_type::value_separator: - case token_type::end_of_input: - case token_type::literal_or_value: - default: // the last token was unexpected - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::literal_or_value, "value"), BasicJsonType())); - } - } - } - else - { - skip_to_state_evaluation = false; - } - - // we reached this line after we successfully parsed a value - if (states.empty()) - { - // empty stack: we reached the end of the hierarchy: done - return true; - } - - if (states.back()) // array - { - // comma -> next value - if (get_token() == token_type::value_separator) - { - // parse a new value - get_token(); - continue; - } - - // closing ] - if (JSON_HEDLEY_LIKELY(last_token == token_type::end_array)) - { - if (JSON_HEDLEY_UNLIKELY(!sax->end_array())) - { - return false; - } - - // We are done with this array. Before we can parse a - // new value, we need to evaluate the new state first. - // By setting skip_to_state_evaluation to false, we - // are effectively jumping to the beginning of this if. - JSON_ASSERT(!states.empty()); - states.pop_back(); - skip_to_state_evaluation = true; - continue; - } - - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_array, "array"), BasicJsonType())); - } - - // states.back() is false -> object - - // comma -> next value - if (get_token() == token_type::value_separator) - { - // parse key - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::value_string)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::value_string, "object key"), BasicJsonType())); - } - - if (JSON_HEDLEY_UNLIKELY(!sax->key(m_lexer.get_string()))) - { - return false; - } - - // parse separator (:) - if (JSON_HEDLEY_UNLIKELY(get_token() != token_type::name_separator)) - { - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::name_separator, "object separator"), BasicJsonType())); - } - - // parse values - get_token(); - continue; - } - - // closing } - if (JSON_HEDLEY_LIKELY(last_token == token_type::end_object)) - { - if (JSON_HEDLEY_UNLIKELY(!sax->end_object())) - { - return false; - } - - // We are done with this object. Before we can parse a - // new value, we need to evaluate the new state first. - // By setting skip_to_state_evaluation to false, we - // are effectively jumping to the beginning of this if. - JSON_ASSERT(!states.empty()); - states.pop_back(); - skip_to_state_evaluation = true; - continue; - } - - return sax->parse_error(m_lexer.get_position(), - m_lexer.get_token_string(), - parse_error::create(101, m_lexer.get_position(), exception_message(token_type::end_object, "object"), BasicJsonType())); - } - } - - /// get next token from lexer - token_type get_token() - { - return last_token = m_lexer.scan(); - } - - std::string exception_message(const token_type expected, const std::string& context) - { - std::string error_msg = "syntax error "; - - if (!context.empty()) - { - error_msg += "while parsing " + context + " "; - } - - error_msg += "- "; - - if (last_token == token_type::parse_error) - { - error_msg += std::string(m_lexer.get_error_message()) + "; last read: '" + - m_lexer.get_token_string() + "'"; - } - else - { - error_msg += "unexpected " + std::string(lexer_t::token_type_name(last_token)); - } - - if (expected != token_type::uninitialized) - { - error_msg += "; expected " + std::string(lexer_t::token_type_name(expected)); - } - - return error_msg; - } - - private: - /// callback function - const parser_callback_t callback = nullptr; - /// the type of the last read token - token_type last_token = token_type::uninitialized; - /// the lexer - lexer_t m_lexer; - /// whether to throw exceptions in case of errors - const bool allow_exceptions = true; -}; - -} // namespace detail -} // namespace nlohmann - -// #include - - -// #include - - -#include // ptrdiff_t -#include // numeric_limits - -// #include - - -namespace nlohmann -{ -namespace detail -{ -/* -@brief an iterator for primitive JSON types - -This class models an iterator for primitive JSON types (boolean, number, -string). It's only purpose is to allow the iterator/const_iterator classes -to "iterate" over primitive values. Internally, the iterator is modeled by -a `difference_type` variable. Value begin_value (`0`) models the begin, -end_value (`1`) models past the end. -*/ -class primitive_iterator_t -{ - private: - using difference_type = std::ptrdiff_t; - static constexpr difference_type begin_value = 0; - static constexpr difference_type end_value = begin_value + 1; - - JSON_PRIVATE_UNLESS_TESTED: - /// iterator as signed integer type - difference_type m_it = (std::numeric_limits::min)(); - - public: - constexpr difference_type get_value() const noexcept - { - return m_it; - } - - /// set iterator to a defined beginning - void set_begin() noexcept - { - m_it = begin_value; - } - - /// set iterator to a defined past the end - void set_end() noexcept - { - m_it = end_value; - } - - /// return whether the iterator can be dereferenced - constexpr bool is_begin() const noexcept - { - return m_it == begin_value; - } - - /// return whether the iterator is at end - constexpr bool is_end() const noexcept - { - return m_it == end_value; - } - - friend constexpr bool operator==(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept - { - return lhs.m_it == rhs.m_it; - } - - friend constexpr bool operator<(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept - { - return lhs.m_it < rhs.m_it; - } - - primitive_iterator_t operator+(difference_type n) noexcept - { - auto result = *this; - result += n; - return result; - } - - friend constexpr difference_type operator-(primitive_iterator_t lhs, primitive_iterator_t rhs) noexcept - { - return lhs.m_it - rhs.m_it; - } - - primitive_iterator_t& operator++() noexcept - { - ++m_it; - return *this; - } - - primitive_iterator_t const operator++(int) noexcept // NOLINT(readability-const-return-type) - { - auto result = *this; - ++m_it; - return result; - } - - primitive_iterator_t& operator--() noexcept - { - --m_it; - return *this; - } - - primitive_iterator_t const operator--(int) noexcept // NOLINT(readability-const-return-type) - { - auto result = *this; - --m_it; - return result; - } - - primitive_iterator_t& operator+=(difference_type n) noexcept - { - m_it += n; - return *this; - } - - primitive_iterator_t& operator-=(difference_type n) noexcept - { - m_it -= n; - return *this; - } -}; -} // namespace detail -} // namespace nlohmann - - -namespace nlohmann -{ -namespace detail -{ -/*! -@brief an iterator value - -@note This structure could easily be a union, but MSVC currently does not allow -unions members with complex constructors, see https://github.com/nlohmann/json/pull/105. -*/ -template struct internal_iterator -{ - /// iterator for JSON objects - typename BasicJsonType::object_t::iterator object_iterator {}; - /// iterator for JSON arrays - typename BasicJsonType::array_t::iterator array_iterator {}; - /// generic iterator for all other types - primitive_iterator_t primitive_iterator {}; -}; -} // namespace detail -} // namespace nlohmann - -// #include - - -#include // iterator, random_access_iterator_tag, bidirectional_iterator_tag, advance, next -#include // conditional, is_const, remove_const - -// #include - -// #include - -// #include - -// #include - -// #include - -// #include - -// #include - - -namespace nlohmann -{ -namespace detail -{ -// forward declare, to be able to friend it later on -template class iteration_proxy; -template class iteration_proxy_value; - -/*! -@brief a template for a bidirectional iterator for the @ref basic_json class -This class implements a both iterators (iterator and const_iterator) for the -@ref basic_json class. -@note An iterator is called *initialized* when a pointer to a JSON value has - been set (e.g., by a constructor or a copy assignment). If the iterator is - default-constructed, it is *uninitialized* and most methods are undefined. - **The library uses assertions to detect calls on uninitialized iterators.** -@requirement The class satisfies the following concept requirements: -- -[BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator): - The iterator that can be moved can be moved in both directions (i.e. - incremented and decremented). -@since version 1.0.0, simplified in version 2.0.9, change to bidirectional - iterators in version 3.0.0 (see https://github.com/nlohmann/json/issues/593) -*/ -template -class iter_impl -{ - /// the iterator with BasicJsonType of different const-ness - using other_iter_impl = iter_impl::value, typename std::remove_const::type, const BasicJsonType>::type>; - /// allow basic_json to access private members - friend other_iter_impl; - friend BasicJsonType; - friend iteration_proxy; - friend iteration_proxy_value; - - using object_t = typename BasicJsonType::object_t; - using array_t = typename BasicJsonType::array_t; - // make sure BasicJsonType is basic_json or const basic_json - static_assert(is_basic_json::type>::value, - "iter_impl only accepts (const) basic_json"); - - public: - - /// The std::iterator class template (used as a base class to provide typedefs) is deprecated in C++17. - /// The C++ Standard has never required user-defined iterators to derive from std::iterator. - /// A user-defined iterator should provide publicly accessible typedefs named - /// iterator_category, value_type, difference_type, pointer, and reference. - /// Note that value_type is required to be non-const, even for constant iterators. - using iterator_category = std::bidirectional_iterator_tag; - - /// the type of the values when the iterator is dereferenced - using value_type = typename BasicJsonType::value_type; - /// a type to represent differences between iterators - using difference_type = typename BasicJsonType::difference_type; - /// defines a pointer to the type iterated over (value_type) - using pointer = typename std::conditional::value, - typename BasicJsonType::const_pointer, - typename BasicJsonType::pointer>::type; - /// defines a reference to the type iterated over (value_type) - using reference = - typename std::conditional::value, - typename BasicJsonType::const_reference, - typename BasicJsonType::reference>::type; - - iter_impl() = default; - ~iter_impl() = default; - iter_impl(iter_impl&&) noexcept = default; - iter_impl& operator=(iter_impl&&) noexcept = default; - - /*! - @brief constructor for a given JSON instance - @param[in] object pointer to a JSON object for this iterator - @pre object != nullptr - @post The iterator is initialized; i.e. `m_object != nullptr`. - */ - explicit iter_impl(pointer object) noexcept : m_object(object) - { - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - { - m_it.object_iterator = typename object_t::iterator(); - break; - } - - case value_t::array: - { - m_it.array_iterator = typename array_t::iterator(); - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - m_it.primitive_iterator = primitive_iterator_t(); - break; - } - } - } - - /*! - @note The conventional copy constructor and copy assignment are implicitly - defined. Combined with the following converting constructor and - assignment, they support: (1) copy from iterator to iterator, (2) - copy from const iterator to const iterator, and (3) conversion from - iterator to const iterator. However conversion from const iterator - to iterator is not defined. - */ - - /*! - @brief const copy constructor - @param[in] other const iterator to copy from - @note This copy constructor had to be defined explicitly to circumvent a bug - occurring on msvc v19.0 compiler (VS 2015) debug build. For more - information refer to: https://github.com/nlohmann/json/issues/1608 - */ - iter_impl(const iter_impl& other) noexcept - : m_object(other.m_object), m_it(other.m_it) - {} - - /*! - @brief converting assignment - @param[in] other const iterator to copy from - @return const/non-const iterator - @note It is not checked whether @a other is initialized. - */ - iter_impl& operator=(const iter_impl& other) noexcept - { - if (&other != this) - { - m_object = other.m_object; - m_it = other.m_it; - } - return *this; - } - - /*! - @brief converting constructor - @param[in] other non-const iterator to copy from - @note It is not checked whether @a other is initialized. - */ - iter_impl(const iter_impl::type>& other) noexcept - : m_object(other.m_object), m_it(other.m_it) - {} - - /*! - @brief converting assignment - @param[in] other non-const iterator to copy from - @return const/non-const iterator - @note It is not checked whether @a other is initialized. - */ - iter_impl& operator=(const iter_impl::type>& other) noexcept // NOLINT(cert-oop54-cpp) - { - m_object = other.m_object; - m_it = other.m_it; - return *this; - } - - JSON_PRIVATE_UNLESS_TESTED: - /*! - @brief set the iterator to the first value - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - void set_begin() noexcept - { - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - { - m_it.object_iterator = m_object->m_value.object->begin(); - break; - } - - case value_t::array: - { - m_it.array_iterator = m_object->m_value.array->begin(); - break; - } - - case value_t::null: - { - // set to end so begin()==end() is true: null is empty - m_it.primitive_iterator.set_end(); - break; - } - - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - m_it.primitive_iterator.set_begin(); - break; - } - } - } - - /*! - @brief set the iterator past the last value - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - void set_end() noexcept - { - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - { - m_it.object_iterator = m_object->m_value.object->end(); - break; - } - - case value_t::array: - { - m_it.array_iterator = m_object->m_value.array->end(); - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - m_it.primitive_iterator.set_end(); - break; - } - } - } - - public: - /*! - @brief return a reference to the value pointed to by the iterator - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - reference operator*() const - { - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - { - JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end()); - return m_it.object_iterator->second; - } - - case value_t::array: - { - JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end()); - return *m_it.array_iterator; - } - - case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); - - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin())) - { - return *m_object; - } - - JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); - } - } - } - - /*! - @brief dereference the iterator - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - pointer operator->() const - { - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - { - JSON_ASSERT(m_it.object_iterator != m_object->m_value.object->end()); - return &(m_it.object_iterator->second); - } - - case value_t::array: - { - JSON_ASSERT(m_it.array_iterator != m_object->m_value.array->end()); - return &*m_it.array_iterator; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.is_begin())) - { - return m_object; - } - - JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); - } - } - } - - /*! - @brief post-increment (it++) - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - iter_impl const operator++(int) // NOLINT(readability-const-return-type) - { - auto result = *this; - ++(*this); - return result; - } - - /*! - @brief pre-increment (++it) - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - iter_impl& operator++() - { - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - { - std::advance(m_it.object_iterator, 1); - break; - } - - case value_t::array: - { - std::advance(m_it.array_iterator, 1); - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - ++m_it.primitive_iterator; - break; - } - } - - return *this; - } - - /*! - @brief post-decrement (it--) - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - iter_impl const operator--(int) // NOLINT(readability-const-return-type) - { - auto result = *this; - --(*this); - return result; - } - - /*! - @brief pre-decrement (--it) - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - iter_impl& operator--() - { - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - { - std::advance(m_it.object_iterator, -1); - break; - } - - case value_t::array: - { - std::advance(m_it.array_iterator, -1); - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - --m_it.primitive_iterator; - break; - } - } - - return *this; - } - - /*! - @brief comparison: equal - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - template < typename IterImpl, detail::enable_if_t < (std::is_same::value || std::is_same::value), std::nullptr_t > = nullptr > - bool operator==(const IterImpl& other) const - { - // if objects are not the same, the comparison is undefined - if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) - { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object)); - } - - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - return (m_it.object_iterator == other.m_it.object_iterator); - - case value_t::array: - return (m_it.array_iterator == other.m_it.array_iterator); - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return (m_it.primitive_iterator == other.m_it.primitive_iterator); - } - } - - /*! - @brief comparison: not equal - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - template < typename IterImpl, detail::enable_if_t < (std::is_same::value || std::is_same::value), std::nullptr_t > = nullptr > - bool operator!=(const IterImpl& other) const - { - return !operator==(other); - } - - /*! - @brief comparison: smaller - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - bool operator<(const iter_impl& other) const - { - // if objects are not the same, the comparison is undefined - if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) - { - JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", *m_object)); - } - - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - JSON_THROW(invalid_iterator::create(213, "cannot compare order of object iterators", *m_object)); - - case value_t::array: - return (m_it.array_iterator < other.m_it.array_iterator); - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return (m_it.primitive_iterator < other.m_it.primitive_iterator); - } - } - - /*! - @brief comparison: less than or equal - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - bool operator<=(const iter_impl& other) const - { - return !other.operator < (*this); - } - - /*! - @brief comparison: greater than - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - bool operator>(const iter_impl& other) const - { - return !operator<=(other); - } - - /*! - @brief comparison: greater than or equal - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - bool operator>=(const iter_impl& other) const - { - return !operator<(other); - } - - /*! - @brief add to iterator - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - iter_impl& operator+=(difference_type i) - { - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object)); - - case value_t::array: - { - std::advance(m_it.array_iterator, i); - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - m_it.primitive_iterator += i; - break; - } - } - - return *this; - } - - /*! - @brief subtract from iterator - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - iter_impl& operator-=(difference_type i) - { - return operator+=(-i); - } - - /*! - @brief add to iterator - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - iter_impl operator+(difference_type i) const - { - auto result = *this; - result += i; - return result; - } - - /*! - @brief addition of distance and iterator - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - friend iter_impl operator+(difference_type i, const iter_impl& it) - { - auto result = it; - result += i; - return result; - } - - /*! - @brief subtract from iterator - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - iter_impl operator-(difference_type i) const - { - auto result = *this; - result -= i; - return result; - } - - /*! - @brief return difference - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - difference_type operator-(const iter_impl& other) const - { - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - JSON_THROW(invalid_iterator::create(209, "cannot use offsets with object iterators", *m_object)); - - case value_t::array: - return m_it.array_iterator - other.m_it.array_iterator; - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return m_it.primitive_iterator - other.m_it.primitive_iterator; - } - } - - /*! - @brief access to successor - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - reference operator[](difference_type n) const - { - JSON_ASSERT(m_object != nullptr); - - switch (m_object->m_type) - { - case value_t::object: - JSON_THROW(invalid_iterator::create(208, "cannot use operator[] for object iterators", *m_object)); - - case value_t::array: - return *std::next(m_it.array_iterator, n); - - case value_t::null: - JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); - - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - if (JSON_HEDLEY_LIKELY(m_it.primitive_iterator.get_value() == -n)) - { - return *m_object; - } - - JSON_THROW(invalid_iterator::create(214, "cannot get value", *m_object)); - } - } - } - - /*! - @brief return the key of an object iterator - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - const typename object_t::key_type& key() const - { - JSON_ASSERT(m_object != nullptr); - - if (JSON_HEDLEY_LIKELY(m_object->is_object())) - { - return m_it.object_iterator->first; - } - - JSON_THROW(invalid_iterator::create(207, "cannot use key() for non-object iterators", *m_object)); - } - - /*! - @brief return the value of an iterator - @pre The iterator is initialized; i.e. `m_object != nullptr`. - */ - reference value() const - { - return operator*(); - } - - JSON_PRIVATE_UNLESS_TESTED: - /// associated JSON instance - pointer m_object = nullptr; - /// the actual iterator of the associated instance - internal_iterator::type> m_it {}; -}; -} // namespace detail -} // namespace nlohmann - -// #include - -// #include - - -#include // ptrdiff_t -#include // reverse_iterator -#include // declval - -namespace nlohmann -{ -namespace detail -{ -////////////////////// -// reverse_iterator // -////////////////////// - -/*! -@brief a template for a reverse iterator class - -@tparam Base the base iterator type to reverse. Valid types are @ref -iterator (to create @ref reverse_iterator) and @ref const_iterator (to -create @ref const_reverse_iterator). - -@requirement The class satisfies the following concept requirements: -- -[BidirectionalIterator](https://en.cppreference.com/w/cpp/named_req/BidirectionalIterator): - The iterator that can be moved can be moved in both directions (i.e. - incremented and decremented). -- [OutputIterator](https://en.cppreference.com/w/cpp/named_req/OutputIterator): - It is possible to write to the pointed-to element (only if @a Base is - @ref iterator). - -@since version 1.0.0 -*/ -template -class json_reverse_iterator : public std::reverse_iterator -{ - public: - using difference_type = std::ptrdiff_t; - /// shortcut to the reverse iterator adapter - using base_iterator = std::reverse_iterator; - /// the reference type for the pointed-to element - using reference = typename Base::reference; - - /// create reverse iterator from iterator - explicit json_reverse_iterator(const typename base_iterator::iterator_type& it) noexcept - : base_iterator(it) {} - - /// create reverse iterator from base class - explicit json_reverse_iterator(const base_iterator& it) noexcept : base_iterator(it) {} - - /// post-increment (it++) - json_reverse_iterator const operator++(int) // NOLINT(readability-const-return-type) - { - return static_cast(base_iterator::operator++(1)); - } - - /// pre-increment (++it) - json_reverse_iterator& operator++() - { - return static_cast(base_iterator::operator++()); - } - - /// post-decrement (it--) - json_reverse_iterator const operator--(int) // NOLINT(readability-const-return-type) - { - return static_cast(base_iterator::operator--(1)); - } - - /// pre-decrement (--it) - json_reverse_iterator& operator--() - { - return static_cast(base_iterator::operator--()); - } - - /// add to iterator - json_reverse_iterator& operator+=(difference_type i) - { - return static_cast(base_iterator::operator+=(i)); - } - - /// add to iterator - json_reverse_iterator operator+(difference_type i) const - { - return static_cast(base_iterator::operator+(i)); - } - - /// subtract from iterator - json_reverse_iterator operator-(difference_type i) const - { - return static_cast(base_iterator::operator-(i)); - } - - /// return difference - difference_type operator-(const json_reverse_iterator& other) const - { - return base_iterator(*this) - base_iterator(other); - } - - /// access to successor - reference operator[](difference_type n) const - { - return *(this->operator+(n)); - } - - /// return the key of an object iterator - auto key() const -> decltype(std::declval().key()) - { - auto it = --this->base(); - return it.key(); - } - - /// return the value of an iterator - reference value() const - { - auto it = --this->base(); - return it.operator * (); - } -}; -} // namespace detail -} // namespace nlohmann - -// #include - -// #include - - -#include // all_of -#include // isdigit -#include // max -#include // accumulate -#include // string -#include // move -#include // vector - -// #include - -// #include - -// #include - -// #include - - -namespace nlohmann -{ -template -class json_pointer -{ - // allow basic_json to access private members - NLOHMANN_BASIC_JSON_TPL_DECLARATION - friend class basic_json; - - public: - /*! - @brief create JSON pointer - - Create a JSON pointer according to the syntax described in - [Section 3 of RFC6901](https://tools.ietf.org/html/rfc6901#section-3). - - @param[in] s string representing the JSON pointer; if omitted, the empty - string is assumed which references the whole JSON value - - @throw parse_error.107 if the given JSON pointer @a s is nonempty and does - not begin with a slash (`/`); see example below - - @throw parse_error.108 if a tilde (`~`) in the given JSON pointer @a s is - not followed by `0` (representing `~`) or `1` (representing `/`); see - example below - - @liveexample{The example shows the construction several valid JSON pointers - as well as the exceptional behavior.,json_pointer} - - @since version 2.0.0 - */ - explicit json_pointer(const std::string& s = "") - : reference_tokens(split(s)) - {} - - /*! - @brief return a string representation of the JSON pointer - - @invariant For each JSON pointer `ptr`, it holds: - @code {.cpp} - ptr == json_pointer(ptr.to_string()); - @endcode - - @return a string representation of the JSON pointer - - @liveexample{The example shows the result of `to_string`.,json_pointer__to_string} - - @since version 2.0.0 - */ - std::string to_string() const - { - return std::accumulate(reference_tokens.begin(), reference_tokens.end(), - std::string{}, - [](const std::string & a, const std::string & b) - { - return a + "/" + detail::escape(b); - }); - } - - /// @copydoc to_string() - operator std::string() const - { - return to_string(); - } - - /*! - @brief append another JSON pointer at the end of this JSON pointer - - @param[in] ptr JSON pointer to append - @return JSON pointer with @a ptr appended - - @liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add} - - @complexity Linear in the length of @a ptr. - - @sa see @ref operator/=(std::string) to append a reference token - @sa see @ref operator/=(std::size_t) to append an array index - @sa see @ref operator/(const json_pointer&, const json_pointer&) for a binary operator - - @since version 3.6.0 - */ - json_pointer& operator/=(const json_pointer& ptr) - { - reference_tokens.insert(reference_tokens.end(), - ptr.reference_tokens.begin(), - ptr.reference_tokens.end()); - return *this; - } - - /*! - @brief append an unescaped reference token at the end of this JSON pointer - - @param[in] token reference token to append - @return JSON pointer with @a token appended without escaping @a token - - @liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add} - - @complexity Amortized constant. - - @sa see @ref operator/=(const json_pointer&) to append a JSON pointer - @sa see @ref operator/=(std::size_t) to append an array index - @sa see @ref operator/(const json_pointer&, std::size_t) for a binary operator - - @since version 3.6.0 - */ - json_pointer& operator/=(std::string token) - { - push_back(std::move(token)); - return *this; - } - - /*! - @brief append an array index at the end of this JSON pointer - - @param[in] array_idx array index to append - @return JSON pointer with @a array_idx appended - - @liveexample{The example shows the usage of `operator/=`.,json_pointer__operator_add} - - @complexity Amortized constant. - - @sa see @ref operator/=(const json_pointer&) to append a JSON pointer - @sa see @ref operator/=(std::string) to append a reference token - @sa see @ref operator/(const json_pointer&, std::string) for a binary operator - - @since version 3.6.0 - */ - json_pointer& operator/=(std::size_t array_idx) - { - return *this /= std::to_string(array_idx); - } - - /*! - @brief create a new JSON pointer by appending the right JSON pointer at the end of the left JSON pointer - - @param[in] lhs JSON pointer - @param[in] rhs JSON pointer - @return a new JSON pointer with @a rhs appended to @a lhs - - @liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary} - - @complexity Linear in the length of @a lhs and @a rhs. - - @sa see @ref operator/=(const json_pointer&) to append a JSON pointer - - @since version 3.6.0 - */ - friend json_pointer operator/(const json_pointer& lhs, - const json_pointer& rhs) - { - return json_pointer(lhs) /= rhs; - } - - /*! - @brief create a new JSON pointer by appending the unescaped token at the end of the JSON pointer - - @param[in] ptr JSON pointer - @param[in] token reference token - @return a new JSON pointer with unescaped @a token appended to @a ptr - - @liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary} - - @complexity Linear in the length of @a ptr. - - @sa see @ref operator/=(std::string) to append a reference token - - @since version 3.6.0 - */ - friend json_pointer operator/(const json_pointer& ptr, std::string token) // NOLINT(performance-unnecessary-value-param) - { - return json_pointer(ptr) /= std::move(token); - } - - /*! - @brief create a new JSON pointer by appending the array-index-token at the end of the JSON pointer - - @param[in] ptr JSON pointer - @param[in] array_idx array index - @return a new JSON pointer with @a array_idx appended to @a ptr - - @liveexample{The example shows the usage of `operator/`.,json_pointer__operator_add_binary} - - @complexity Linear in the length of @a ptr. - - @sa see @ref operator/=(std::size_t) to append an array index - - @since version 3.6.0 - */ - friend json_pointer operator/(const json_pointer& ptr, std::size_t array_idx) - { - return json_pointer(ptr) /= array_idx; - } - - /*! - @brief returns the parent of this JSON pointer - - @return parent of this JSON pointer; in case this JSON pointer is the root, - the root itself is returned - - @complexity Linear in the length of the JSON pointer. - - @liveexample{The example shows the result of `parent_pointer` for different - JSON Pointers.,json_pointer__parent_pointer} - - @since version 3.6.0 - */ - json_pointer parent_pointer() const - { - if (empty()) - { - return *this; - } - - json_pointer res = *this; - res.pop_back(); - return res; - } - - /*! - @brief remove last reference token - - @pre not `empty()` - - @liveexample{The example shows the usage of `pop_back`.,json_pointer__pop_back} - - @complexity Constant. - - @throw out_of_range.405 if JSON pointer has no parent - - @since version 3.6.0 - */ - void pop_back() - { - if (JSON_HEDLEY_UNLIKELY(empty())) - { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); - } - - reference_tokens.pop_back(); - } - - /*! - @brief return last reference token - - @pre not `empty()` - @return last reference token - - @liveexample{The example shows the usage of `back`.,json_pointer__back} - - @complexity Constant. - - @throw out_of_range.405 if JSON pointer has no parent - - @since version 3.6.0 - */ - const std::string& back() const - { - if (JSON_HEDLEY_UNLIKELY(empty())) - { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); - } - - return reference_tokens.back(); - } - - /*! - @brief append an unescaped token at the end of the reference pointer - - @param[in] token token to add - - @complexity Amortized constant. - - @liveexample{The example shows the result of `push_back` for different - JSON Pointers.,json_pointer__push_back} - - @since version 3.6.0 - */ - void push_back(const std::string& token) - { - reference_tokens.push_back(token); - } - - /// @copydoc push_back(const std::string&) - void push_back(std::string&& token) - { - reference_tokens.push_back(std::move(token)); - } - - /*! - @brief return whether pointer points to the root document - - @return true iff the JSON pointer points to the root document - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @liveexample{The example shows the result of `empty` for different JSON - Pointers.,json_pointer__empty} - - @since version 3.6.0 - */ - bool empty() const noexcept - { - return reference_tokens.empty(); - } - - private: - /*! - @param[in] s reference token to be converted into an array index - - @return integer representation of @a s - - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index begins not with a digit - @throw out_of_range.404 if string @a s could not be converted to an integer - @throw out_of_range.410 if an array index exceeds size_type - */ - static typename BasicJsonType::size_type array_index(const std::string& s) - { - using size_type = typename BasicJsonType::size_type; - - // error condition (cf. RFC 6901, Sect. 4) - if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) - { - JSON_THROW(detail::parse_error::create(106, 0, "array index '" + s + "' must not begin with '0'", BasicJsonType())); - } - - // error condition (cf. RFC 6901, Sect. 4) - if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && !(s[0] >= '1' && s[0] <= '9'))) - { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number", BasicJsonType())); - } - - std::size_t processed_chars = 0; - unsigned long long res = 0; // NOLINT(runtime/int) - JSON_TRY - { - res = std::stoull(s, &processed_chars); - } - JSON_CATCH(std::out_of_range&) - { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType())); - } - - // check if the string was completely read - if (JSON_HEDLEY_UNLIKELY(processed_chars != s.size())) - { - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'", BasicJsonType())); - } - - // only triggered on special platforms (like 32bit), see also - // https://github.com/nlohmann/json/pull/2203 - if (res >= static_cast((std::numeric_limits::max)())) // NOLINT(runtime/int) - { - JSON_THROW(detail::out_of_range::create(410, "array index " + s + " exceeds size_type", BasicJsonType())); // LCOV_EXCL_LINE - } - - return static_cast(res); - } - - JSON_PRIVATE_UNLESS_TESTED: - json_pointer top() const - { - if (JSON_HEDLEY_UNLIKELY(empty())) - { - JSON_THROW(detail::out_of_range::create(405, "JSON pointer has no parent", BasicJsonType())); - } - - json_pointer result = *this; - result.reference_tokens = {reference_tokens[0]}; - return result; - } - - private: - /*! - @brief create and return a reference to the pointed to value - - @complexity Linear in the number of reference tokens. - - @throw parse_error.109 if array index is not a number - @throw type_error.313 if value cannot be unflattened - */ - BasicJsonType& get_and_create(BasicJsonType& j) const - { - auto* result = &j; - - // in case no reference tokens exist, return a reference to the JSON value - // j which will be overwritten by a primitive value - for (const auto& reference_token : reference_tokens) - { - switch (result->type()) - { - case detail::value_t::null: - { - if (reference_token == "0") - { - // start a new array if reference token is 0 - result = &result->operator[](0); - } - else - { - // start a new object otherwise - result = &result->operator[](reference_token); - } - break; - } - - case detail::value_t::object: - { - // create an entry in the object - result = &result->operator[](reference_token); - break; - } - - case detail::value_t::array: - { - // create an entry in the array - result = &result->operator[](array_index(reference_token)); - break; - } - - /* - The following code is only reached if there exists a reference - token _and_ the current value is primitive. In this case, we have - an error situation, because primitive values may only occur as - single value; that is, with an empty list of reference tokens. - */ - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - JSON_THROW(detail::type_error::create(313, "invalid value to unflatten", j)); - } - } - - return *result; - } - - /*! - @brief return a reference to the pointed to value - - @note This version does not throw if a value is not present, but tries to - create nested values instead. For instance, calling this function - with pointer `"/this/that"` on a null value is equivalent to calling - `operator[]("this").operator[]("that")` on that value, effectively - changing the null value to an object. - - @param[in] ptr a JSON value - - @return reference to the JSON value pointed to by the JSON pointer - - @complexity Linear in the length of the JSON pointer. - - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - @throw out_of_range.404 if the JSON pointer can not be resolved - */ - BasicJsonType& get_unchecked(BasicJsonType* ptr) const - { - for (const auto& reference_token : reference_tokens) - { - // convert null values to arrays or objects before continuing - if (ptr->is_null()) - { - // check if reference token is a number - const bool nums = - std::all_of(reference_token.begin(), reference_token.end(), - [](const unsigned char x) - { - return std::isdigit(x); - }); - - // change value to array for numbers or "-" or to object otherwise - *ptr = (nums || reference_token == "-") - ? detail::value_t::array - : detail::value_t::object; - } - - switch (ptr->type()) - { - case detail::value_t::object: - { - // use unchecked object access - ptr = &ptr->operator[](reference_token); - break; - } - - case detail::value_t::array: - { - if (reference_token == "-") - { - // explicitly treat "-" as index beyond the end - ptr = &ptr->operator[](ptr->m_value.array->size()); - } - else - { - // convert array index to number; unchecked access - ptr = &ptr->operator[](array_index(reference_token)); - } - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); - } - } - - return *ptr; - } - - /*! - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - @throw out_of_range.402 if the array index '-' is used - @throw out_of_range.404 if the JSON pointer can not be resolved - */ - BasicJsonType& get_checked(BasicJsonType* ptr) const - { - for (const auto& reference_token : reference_tokens) - { - switch (ptr->type()) - { - case detail::value_t::object: - { - // note: at performs range check - ptr = &ptr->at(reference_token); - break; - } - - case detail::value_t::array: - { - if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) - { - // "-" always fails the range check - JSON_THROW(detail::out_of_range::create(402, - "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range", *ptr)); - } - - // note: at performs range check - ptr = &ptr->at(array_index(reference_token)); - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); - } - } - - return *ptr; - } - - /*! - @brief return a const reference to the pointed to value - - @param[in] ptr a JSON value - - @return const reference to the JSON value pointed to by the JSON - pointer - - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - @throw out_of_range.402 if the array index '-' is used - @throw out_of_range.404 if the JSON pointer can not be resolved - */ - const BasicJsonType& get_unchecked(const BasicJsonType* ptr) const - { - for (const auto& reference_token : reference_tokens) - { - switch (ptr->type()) - { - case detail::value_t::object: - { - // use unchecked object access - ptr = &ptr->operator[](reference_token); - break; - } - - case detail::value_t::array: - { - if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) - { - // "-" cannot be used for const access - JSON_THROW(detail::out_of_range::create(402, "array index '-' (" + std::to_string(ptr->m_value.array->size()) + ") is out of range", *ptr)); - } - - // use unchecked array access - ptr = &ptr->operator[](array_index(reference_token)); - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); - } - } - - return *ptr; - } - - /*! - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - @throw out_of_range.402 if the array index '-' is used - @throw out_of_range.404 if the JSON pointer can not be resolved - */ - const BasicJsonType& get_checked(const BasicJsonType* ptr) const - { - for (const auto& reference_token : reference_tokens) - { - switch (ptr->type()) - { - case detail::value_t::object: - { - // note: at performs range check - ptr = &ptr->at(reference_token); - break; - } - - case detail::value_t::array: - { - if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) - { - // "-" always fails the range check - JSON_THROW(detail::out_of_range::create(402, - "array index '-' (" + std::to_string(ptr->m_value.array->size()) + - ") is out of range", *ptr)); - } - - // note: at performs range check - ptr = &ptr->at(array_index(reference_token)); - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + reference_token + "'", *ptr)); - } - } - - return *ptr; - } - - /*! - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - */ - bool contains(const BasicJsonType* ptr) const - { - for (const auto& reference_token : reference_tokens) - { - switch (ptr->type()) - { - case detail::value_t::object: - { - if (!ptr->contains(reference_token)) - { - // we did not find the key in the object - return false; - } - - ptr = &ptr->operator[](reference_token); - break; - } - - case detail::value_t::array: - { - if (JSON_HEDLEY_UNLIKELY(reference_token == "-")) - { - // "-" always fails the range check - return false; - } - if (JSON_HEDLEY_UNLIKELY(reference_token.size() == 1 && !("0" <= reference_token && reference_token <= "9"))) - { - // invalid char - return false; - } - if (JSON_HEDLEY_UNLIKELY(reference_token.size() > 1)) - { - if (JSON_HEDLEY_UNLIKELY(!('1' <= reference_token[0] && reference_token[0] <= '9'))) - { - // first char should be between '1' and '9' - return false; - } - for (std::size_t i = 1; i < reference_token.size(); i++) - { - if (JSON_HEDLEY_UNLIKELY(!('0' <= reference_token[i] && reference_token[i] <= '9'))) - { - // other char should be between '0' and '9' - return false; - } - } - } - - const auto idx = array_index(reference_token); - if (idx >= ptr->size()) - { - // index out of range - return false; - } - - ptr = &ptr->operator[](idx); - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - { - // we do not expect primitive values if there is still a - // reference token to process - return false; - } - } - } - - // no reference token left means we found a primitive value - return true; - } - - /*! - @brief split the string input to reference tokens - - @note This function is only called by the json_pointer constructor. - All exceptions below are documented there. - - @throw parse_error.107 if the pointer is not empty or begins with '/' - @throw parse_error.108 if character '~' is not followed by '0' or '1' - */ - static std::vector split(const std::string& reference_string) - { - std::vector result; - - // special case: empty reference string -> no reference tokens - if (reference_string.empty()) - { - return result; - } - - // check if nonempty reference string begins with slash - if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) - { - JSON_THROW(detail::parse_error::create(107, 1, "JSON pointer must be empty or begin with '/' - was: '" + reference_string + "'", BasicJsonType())); - } - - // extract the reference tokens: - // - slash: position of the last read slash (or end of string) - // - start: position after the previous slash - for ( - // search for the first slash after the first character - std::size_t slash = reference_string.find_first_of('/', 1), - // set the beginning of the first reference token - start = 1; - // we can stop if start == 0 (if slash == std::string::npos) - start != 0; - // set the beginning of the next reference token - // (will eventually be 0 if slash == std::string::npos) - start = (slash == std::string::npos) ? 0 : slash + 1, - // find next slash - slash = reference_string.find_first_of('/', start)) - { - // use the text between the beginning of the reference token - // (start) and the last slash (slash). - auto reference_token = reference_string.substr(start, slash - start); - - // check reference tokens are properly escaped - for (std::size_t pos = reference_token.find_first_of('~'); - pos != std::string::npos; - pos = reference_token.find_first_of('~', pos + 1)) - { - JSON_ASSERT(reference_token[pos] == '~'); - - // ~ must be followed by 0 or 1 - if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 || - (reference_token[pos + 1] != '0' && - reference_token[pos + 1] != '1'))) - { - JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", BasicJsonType())); - } - } - - // finally, store the reference token - detail::unescape(reference_token); - result.push_back(reference_token); - } - - return result; - } - - private: - /*! - @param[in] reference_string the reference string to the current value - @param[in] value the value to consider - @param[in,out] result the result object to insert values to - - @note Empty objects or arrays are flattened to `null`. - */ - static void flatten(const std::string& reference_string, - const BasicJsonType& value, - BasicJsonType& result) - { - switch (value.type()) - { - case detail::value_t::array: - { - if (value.m_value.array->empty()) - { - // flatten empty array as null - result[reference_string] = nullptr; - } - else - { - // iterate array and use index as reference string - for (std::size_t i = 0; i < value.m_value.array->size(); ++i) - { - flatten(reference_string + "/" + std::to_string(i), - value.m_value.array->operator[](i), result); - } - } - break; - } - - case detail::value_t::object: - { - if (value.m_value.object->empty()) - { - // flatten empty object as null - result[reference_string] = nullptr; - } - else - { - // iterate object and use keys as reference string - for (const auto& element : *value.m_value.object) - { - flatten(reference_string + "/" + detail::escape(element.first), element.second, result); - } - } - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - { - // add primitive value with its reference string - result[reference_string] = value; - break; - } - } - } - - /*! - @param[in] value flattened JSON - - @return unflattened JSON - - @throw parse_error.109 if array index is not a number - @throw type_error.314 if value is not an object - @throw type_error.315 if object values are not primitive - @throw type_error.313 if value cannot be unflattened - */ - static BasicJsonType - unflatten(const BasicJsonType& value) - { - if (JSON_HEDLEY_UNLIKELY(!value.is_object())) - { - JSON_THROW(detail::type_error::create(314, "only objects can be unflattened", value)); - } - - BasicJsonType result; - - // iterate the JSON object values - for (const auto& element : *value.m_value.object) - { - if (JSON_HEDLEY_UNLIKELY(!element.second.is_primitive())) - { - JSON_THROW(detail::type_error::create(315, "values in object must be primitive", element.second)); - } - - // assign value to reference pointed to by JSON pointer; Note that if - // the JSON pointer is "" (i.e., points to the whole value), function - // get_and_create returns a reference to result itself. An assignment - // will then create a primitive value. - json_pointer(element.first).get_and_create(result) = element.second; - } - - return result; - } - - /*! - @brief compares two JSON pointers for equality - - @param[in] lhs JSON pointer to compare - @param[in] rhs JSON pointer to compare - @return whether @a lhs is equal to @a rhs - - @complexity Linear in the length of the JSON pointer - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - */ - friend bool operator==(json_pointer const& lhs, - json_pointer const& rhs) noexcept - { - return lhs.reference_tokens == rhs.reference_tokens; - } - - /*! - @brief compares two JSON pointers for inequality - - @param[in] lhs JSON pointer to compare - @param[in] rhs JSON pointer to compare - @return whether @a lhs is not equal @a rhs - - @complexity Linear in the length of the JSON pointer - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - */ - friend bool operator!=(json_pointer const& lhs, - json_pointer const& rhs) noexcept - { - return !(lhs == rhs); - } - - /// the reference tokens - std::vector reference_tokens; -}; -} // namespace nlohmann - -// #include - - -#include -#include - -// #include - - -namespace nlohmann -{ -namespace detail -{ -template -class json_ref -{ - public: - using value_type = BasicJsonType; - - json_ref(value_type&& value) - : owned_value(std::move(value)) - {} - - json_ref(const value_type& value) - : value_ref(&value) - {} - - json_ref(std::initializer_list init) - : owned_value(init) - {} - - template < - class... Args, - enable_if_t::value, int> = 0 > - json_ref(Args && ... args) - : owned_value(std::forward(args)...) - {} - - // class should be movable only - json_ref(json_ref&&) noexcept = default; - json_ref(const json_ref&) = delete; - json_ref& operator=(const json_ref&) = delete; - json_ref& operator=(json_ref&&) = delete; - ~json_ref() = default; - - value_type moved_or_copied() const - { - if (value_ref == nullptr) - { - return std::move(owned_value); - } - return *value_ref; - } - - value_type const& operator*() const - { - return value_ref ? *value_ref : owned_value; - } - - value_type const* operator->() const - { - return &** this; - } - - private: - mutable value_type owned_value = nullptr; - value_type const* value_ref = nullptr; -}; -} // namespace detail -} // namespace nlohmann - -// #include - -// #include - -// #include - -// #include - -// #include - - -#include // reverse -#include // array -#include // isnan, isinf -#include // uint8_t, uint16_t, uint32_t, uint64_t -#include // memcpy -#include // numeric_limits -#include // string -#include // move - -// #include - -// #include - -// #include - - -#include // copy -#include // size_t -#include // back_inserter -#include // shared_ptr, make_shared -#include // basic_string -#include // vector - -#ifndef JSON_NO_IO - #include // streamsize - #include // basic_ostream -#endif // JSON_NO_IO - -// #include - - -namespace nlohmann -{ -namespace detail -{ -/// abstract output adapter interface -template struct output_adapter_protocol -{ - virtual void write_character(CharType c) = 0; - virtual void write_characters(const CharType* s, std::size_t length) = 0; - virtual ~output_adapter_protocol() = default; - - output_adapter_protocol() = default; - output_adapter_protocol(const output_adapter_protocol&) = default; - output_adapter_protocol(output_adapter_protocol&&) noexcept = default; - output_adapter_protocol& operator=(const output_adapter_protocol&) = default; - output_adapter_protocol& operator=(output_adapter_protocol&&) noexcept = default; -}; - -/// a type to simplify interfaces -template -using output_adapter_t = std::shared_ptr>; - -/// output adapter for byte vectors -template> -class output_vector_adapter : public output_adapter_protocol -{ - public: - explicit output_vector_adapter(std::vector& vec) noexcept - : v(vec) - {} - - void write_character(CharType c) override - { - v.push_back(c); - } - - JSON_HEDLEY_NON_NULL(2) - void write_characters(const CharType* s, std::size_t length) override - { - std::copy(s, s + length, std::back_inserter(v)); - } - - private: - std::vector& v; -}; - -#ifndef JSON_NO_IO -/// output adapter for output streams -template -class output_stream_adapter : public output_adapter_protocol -{ - public: - explicit output_stream_adapter(std::basic_ostream& s) noexcept - : stream(s) - {} - - void write_character(CharType c) override - { - stream.put(c); - } - - JSON_HEDLEY_NON_NULL(2) - void write_characters(const CharType* s, std::size_t length) override - { - stream.write(s, static_cast(length)); - } - - private: - std::basic_ostream& stream; -}; -#endif // JSON_NO_IO - -/// output adapter for basic_string -template> -class output_string_adapter : public output_adapter_protocol -{ - public: - explicit output_string_adapter(StringType& s) noexcept - : str(s) - {} - - void write_character(CharType c) override - { - str.push_back(c); - } - - JSON_HEDLEY_NON_NULL(2) - void write_characters(const CharType* s, std::size_t length) override - { - str.append(s, length); - } - - private: - StringType& str; -}; - -template> -class output_adapter -{ - public: - template> - output_adapter(std::vector& vec) - : oa(std::make_shared>(vec)) {} - -#ifndef JSON_NO_IO - output_adapter(std::basic_ostream& s) - : oa(std::make_shared>(s)) {} -#endif // JSON_NO_IO - - output_adapter(StringType& s) - : oa(std::make_shared>(s)) {} - - operator output_adapter_t() - { - return oa; - } - - private: - output_adapter_t oa = nullptr; -}; -} // namespace detail -} // namespace nlohmann - - -namespace nlohmann -{ -namespace detail -{ -/////////////////// -// binary writer // -/////////////////// - -/*! -@brief serialization to CBOR and MessagePack values -*/ -template -class binary_writer -{ - using string_t = typename BasicJsonType::string_t; - using binary_t = typename BasicJsonType::binary_t; - using number_float_t = typename BasicJsonType::number_float_t; - - public: - /*! - @brief create a binary writer - - @param[in] adapter output adapter to write to - */ - explicit binary_writer(output_adapter_t adapter) : oa(std::move(adapter)) - { - JSON_ASSERT(oa); - } - - /*! - @param[in] j JSON value to serialize - @pre j.type() == value_t::object - */ - void write_bson(const BasicJsonType& j) - { - switch (j.type()) - { - case value_t::object: - { - write_bson_object(*j.m_value.object); - break; - } - - case value_t::null: - case value_t::array: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - JSON_THROW(type_error::create(317, "to serialize to BSON, top-level type must be object, but is " + std::string(j.type_name()), j)); - } - } - } - - /*! - @param[in] j JSON value to serialize - */ - void write_cbor(const BasicJsonType& j) - { - switch (j.type()) - { - case value_t::null: - { - oa->write_character(to_char_type(0xF6)); - break; - } - - case value_t::boolean: - { - oa->write_character(j.m_value.boolean - ? to_char_type(0xF5) - : to_char_type(0xF4)); - break; - } - - case value_t::number_integer: - { - if (j.m_value.number_integer >= 0) - { - // CBOR does not differentiate between positive signed - // integers and unsigned integers. Therefore, we used the - // code from the value_t::number_unsigned case here. - if (j.m_value.number_integer <= 0x17) - { - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_integer <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x18)); - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_integer <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x19)); - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_integer <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x1A)); - write_number(static_cast(j.m_value.number_integer)); - } - else - { - oa->write_character(to_char_type(0x1B)); - write_number(static_cast(j.m_value.number_integer)); - } - } - else - { - // The conversions below encode the sign in the first - // byte, and the value is converted to a positive number. - const auto positive_number = -1 - j.m_value.number_integer; - if (j.m_value.number_integer >= -24) - { - write_number(static_cast(0x20 + positive_number)); - } - else if (positive_number <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x38)); - write_number(static_cast(positive_number)); - } - else if (positive_number <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x39)); - write_number(static_cast(positive_number)); - } - else if (positive_number <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x3A)); - write_number(static_cast(positive_number)); - } - else - { - oa->write_character(to_char_type(0x3B)); - write_number(static_cast(positive_number)); - } - } - break; - } - - case value_t::number_unsigned: - { - if (j.m_value.number_unsigned <= 0x17) - { - write_number(static_cast(j.m_value.number_unsigned)); - } - else if (j.m_value.number_unsigned <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x18)); - write_number(static_cast(j.m_value.number_unsigned)); - } - else if (j.m_value.number_unsigned <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x19)); - write_number(static_cast(j.m_value.number_unsigned)); - } - else if (j.m_value.number_unsigned <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x1A)); - write_number(static_cast(j.m_value.number_unsigned)); - } - else - { - oa->write_character(to_char_type(0x1B)); - write_number(static_cast(j.m_value.number_unsigned)); - } - break; - } - - case value_t::number_float: - { - if (std::isnan(j.m_value.number_float)) - { - // NaN is 0xf97e00 in CBOR - oa->write_character(to_char_type(0xF9)); - oa->write_character(to_char_type(0x7E)); - oa->write_character(to_char_type(0x00)); - } - else if (std::isinf(j.m_value.number_float)) - { - // Infinity is 0xf97c00, -Infinity is 0xf9fc00 - oa->write_character(to_char_type(0xf9)); - oa->write_character(j.m_value.number_float > 0 ? to_char_type(0x7C) : to_char_type(0xFC)); - oa->write_character(to_char_type(0x00)); - } - else - { - write_compact_float(j.m_value.number_float, detail::input_format_t::cbor); - } - break; - } - - case value_t::string: - { - // step 1: write control byte and the string length - const auto N = j.m_value.string->size(); - if (N <= 0x17) - { - write_number(static_cast(0x60 + N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x78)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x79)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x7A)); - write_number(static_cast(N)); - } - // LCOV_EXCL_START - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x7B)); - write_number(static_cast(N)); - } - // LCOV_EXCL_STOP - - // step 2: write the string - oa->write_characters( - reinterpret_cast(j.m_value.string->c_str()), - j.m_value.string->size()); - break; - } - - case value_t::array: - { - // step 1: write control byte and the array size - const auto N = j.m_value.array->size(); - if (N <= 0x17) - { - write_number(static_cast(0x80 + N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x98)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x99)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x9A)); - write_number(static_cast(N)); - } - // LCOV_EXCL_START - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x9B)); - write_number(static_cast(N)); - } - // LCOV_EXCL_STOP - - // step 2: write each element - for (const auto& el : *j.m_value.array) - { - write_cbor(el); - } - break; - } - - case value_t::binary: - { - if (j.m_value.binary->has_subtype()) - { - if (j.m_value.binary->subtype() <= (std::numeric_limits::max)()) - { - write_number(static_cast(0xd8)); - write_number(static_cast(j.m_value.binary->subtype())); - } - else if (j.m_value.binary->subtype() <= (std::numeric_limits::max)()) - { - write_number(static_cast(0xd9)); - write_number(static_cast(j.m_value.binary->subtype())); - } - else if (j.m_value.binary->subtype() <= (std::numeric_limits::max)()) - { - write_number(static_cast(0xda)); - write_number(static_cast(j.m_value.binary->subtype())); - } - else if (j.m_value.binary->subtype() <= (std::numeric_limits::max)()) - { - write_number(static_cast(0xdb)); - write_number(static_cast(j.m_value.binary->subtype())); - } - } - - // step 1: write control byte and the binary array size - const auto N = j.m_value.binary->size(); - if (N <= 0x17) - { - write_number(static_cast(0x40 + N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x58)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x59)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x5A)); - write_number(static_cast(N)); - } - // LCOV_EXCL_START - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0x5B)); - write_number(static_cast(N)); - } - // LCOV_EXCL_STOP - - // step 2: write each element - oa->write_characters( - reinterpret_cast(j.m_value.binary->data()), - N); - - break; - } - - case value_t::object: - { - // step 1: write control byte and the object size - const auto N = j.m_value.object->size(); - if (N <= 0x17) - { - write_number(static_cast(0xA0 + N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0xB8)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0xB9)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0xBA)); - write_number(static_cast(N)); - } - // LCOV_EXCL_START - else if (N <= (std::numeric_limits::max)()) - { - oa->write_character(to_char_type(0xBB)); - write_number(static_cast(N)); - } - // LCOV_EXCL_STOP - - // step 2: write each element - for (const auto& el : *j.m_value.object) - { - write_cbor(el.first); - write_cbor(el.second); - } - break; - } - - case value_t::discarded: - default: - break; - } - } - - /*! - @param[in] j JSON value to serialize - */ - void write_msgpack(const BasicJsonType& j) - { - switch (j.type()) - { - case value_t::null: // nil - { - oa->write_character(to_char_type(0xC0)); - break; - } - - case value_t::boolean: // true and false - { - oa->write_character(j.m_value.boolean - ? to_char_type(0xC3) - : to_char_type(0xC2)); - break; - } - - case value_t::number_integer: - { - if (j.m_value.number_integer >= 0) - { - // MessagePack does not differentiate between positive - // signed integers and unsigned integers. Therefore, we used - // the code from the value_t::number_unsigned case here. - if (j.m_value.number_unsigned < 128) - { - // positive fixnum - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_unsigned <= (std::numeric_limits::max)()) - { - // uint 8 - oa->write_character(to_char_type(0xCC)); - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_unsigned <= (std::numeric_limits::max)()) - { - // uint 16 - oa->write_character(to_char_type(0xCD)); - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_unsigned <= (std::numeric_limits::max)()) - { - // uint 32 - oa->write_character(to_char_type(0xCE)); - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_unsigned <= (std::numeric_limits::max)()) - { - // uint 64 - oa->write_character(to_char_type(0xCF)); - write_number(static_cast(j.m_value.number_integer)); - } - } - else - { - if (j.m_value.number_integer >= -32) - { - // negative fixnum - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_integer >= (std::numeric_limits::min)() && - j.m_value.number_integer <= (std::numeric_limits::max)()) - { - // int 8 - oa->write_character(to_char_type(0xD0)); - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_integer >= (std::numeric_limits::min)() && - j.m_value.number_integer <= (std::numeric_limits::max)()) - { - // int 16 - oa->write_character(to_char_type(0xD1)); - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_integer >= (std::numeric_limits::min)() && - j.m_value.number_integer <= (std::numeric_limits::max)()) - { - // int 32 - oa->write_character(to_char_type(0xD2)); - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_integer >= (std::numeric_limits::min)() && - j.m_value.number_integer <= (std::numeric_limits::max)()) - { - // int 64 - oa->write_character(to_char_type(0xD3)); - write_number(static_cast(j.m_value.number_integer)); - } - } - break; - } - - case value_t::number_unsigned: - { - if (j.m_value.number_unsigned < 128) - { - // positive fixnum - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_unsigned <= (std::numeric_limits::max)()) - { - // uint 8 - oa->write_character(to_char_type(0xCC)); - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_unsigned <= (std::numeric_limits::max)()) - { - // uint 16 - oa->write_character(to_char_type(0xCD)); - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_unsigned <= (std::numeric_limits::max)()) - { - // uint 32 - oa->write_character(to_char_type(0xCE)); - write_number(static_cast(j.m_value.number_integer)); - } - else if (j.m_value.number_unsigned <= (std::numeric_limits::max)()) - { - // uint 64 - oa->write_character(to_char_type(0xCF)); - write_number(static_cast(j.m_value.number_integer)); - } - break; - } - - case value_t::number_float: - { - write_compact_float(j.m_value.number_float, detail::input_format_t::msgpack); - break; - } - - case value_t::string: - { - // step 1: write control byte and the string length - const auto N = j.m_value.string->size(); - if (N <= 31) - { - // fixstr - write_number(static_cast(0xA0 | N)); - } - else if (N <= (std::numeric_limits::max)()) - { - // str 8 - oa->write_character(to_char_type(0xD9)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - // str 16 - oa->write_character(to_char_type(0xDA)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - // str 32 - oa->write_character(to_char_type(0xDB)); - write_number(static_cast(N)); - } - - // step 2: write the string - oa->write_characters( - reinterpret_cast(j.m_value.string->c_str()), - j.m_value.string->size()); - break; - } - - case value_t::array: - { - // step 1: write control byte and the array size - const auto N = j.m_value.array->size(); - if (N <= 15) - { - // fixarray - write_number(static_cast(0x90 | N)); - } - else if (N <= (std::numeric_limits::max)()) - { - // array 16 - oa->write_character(to_char_type(0xDC)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - // array 32 - oa->write_character(to_char_type(0xDD)); - write_number(static_cast(N)); - } - - // step 2: write each element - for (const auto& el : *j.m_value.array) - { - write_msgpack(el); - } - break; - } - - case value_t::binary: - { - // step 0: determine if the binary type has a set subtype to - // determine whether or not to use the ext or fixext types - const bool use_ext = j.m_value.binary->has_subtype(); - - // step 1: write control byte and the byte string length - const auto N = j.m_value.binary->size(); - if (N <= (std::numeric_limits::max)()) - { - std::uint8_t output_type{}; - bool fixed = true; - if (use_ext) - { - switch (N) - { - case 1: - output_type = 0xD4; // fixext 1 - break; - case 2: - output_type = 0xD5; // fixext 2 - break; - case 4: - output_type = 0xD6; // fixext 4 - break; - case 8: - output_type = 0xD7; // fixext 8 - break; - case 16: - output_type = 0xD8; // fixext 16 - break; - default: - output_type = 0xC7; // ext 8 - fixed = false; - break; - } - - } - else - { - output_type = 0xC4; // bin 8 - fixed = false; - } - - oa->write_character(to_char_type(output_type)); - if (!fixed) - { - write_number(static_cast(N)); - } - } - else if (N <= (std::numeric_limits::max)()) - { - std::uint8_t output_type = use_ext - ? 0xC8 // ext 16 - : 0xC5; // bin 16 - - oa->write_character(to_char_type(output_type)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - std::uint8_t output_type = use_ext - ? 0xC9 // ext 32 - : 0xC6; // bin 32 - - oa->write_character(to_char_type(output_type)); - write_number(static_cast(N)); - } - - // step 1.5: if this is an ext type, write the subtype - if (use_ext) - { - write_number(static_cast(j.m_value.binary->subtype())); - } - - // step 2: write the byte string - oa->write_characters( - reinterpret_cast(j.m_value.binary->data()), - N); - - break; - } - - case value_t::object: - { - // step 1: write control byte and the object size - const auto N = j.m_value.object->size(); - if (N <= 15) - { - // fixmap - write_number(static_cast(0x80 | (N & 0xF))); - } - else if (N <= (std::numeric_limits::max)()) - { - // map 16 - oa->write_character(to_char_type(0xDE)); - write_number(static_cast(N)); - } - else if (N <= (std::numeric_limits::max)()) - { - // map 32 - oa->write_character(to_char_type(0xDF)); - write_number(static_cast(N)); - } - - // step 2: write each element - for (const auto& el : *j.m_value.object) - { - write_msgpack(el.first); - write_msgpack(el.second); - } - break; - } - - case value_t::discarded: - default: - break; - } - } - - /*! - @param[in] j JSON value to serialize - @param[in] use_count whether to use '#' prefixes (optimized format) - @param[in] use_type whether to use '$' prefixes (optimized format) - @param[in] add_prefix whether prefixes need to be used for this value - */ - void write_ubjson(const BasicJsonType& j, const bool use_count, - const bool use_type, const bool add_prefix = true) - { - switch (j.type()) - { - case value_t::null: - { - if (add_prefix) - { - oa->write_character(to_char_type('Z')); - } - break; - } - - case value_t::boolean: - { - if (add_prefix) - { - oa->write_character(j.m_value.boolean - ? to_char_type('T') - : to_char_type('F')); - } - break; - } - - case value_t::number_integer: - { - write_number_with_ubjson_prefix(j.m_value.number_integer, add_prefix); - break; - } - - case value_t::number_unsigned: - { - write_number_with_ubjson_prefix(j.m_value.number_unsigned, add_prefix); - break; - } - - case value_t::number_float: - { - write_number_with_ubjson_prefix(j.m_value.number_float, add_prefix); - break; - } - - case value_t::string: - { - if (add_prefix) - { - oa->write_character(to_char_type('S')); - } - write_number_with_ubjson_prefix(j.m_value.string->size(), true); - oa->write_characters( - reinterpret_cast(j.m_value.string->c_str()), - j.m_value.string->size()); - break; - } - - case value_t::array: - { - if (add_prefix) - { - oa->write_character(to_char_type('[')); - } - - bool prefix_required = true; - if (use_type && !j.m_value.array->empty()) - { - JSON_ASSERT(use_count); - const CharType first_prefix = ubjson_prefix(j.front()); - const bool same_prefix = std::all_of(j.begin() + 1, j.end(), - [this, first_prefix](const BasicJsonType & v) - { - return ubjson_prefix(v) == first_prefix; - }); - - if (same_prefix) - { - prefix_required = false; - oa->write_character(to_char_type('$')); - oa->write_character(first_prefix); - } - } - - if (use_count) - { - oa->write_character(to_char_type('#')); - write_number_with_ubjson_prefix(j.m_value.array->size(), true); - } - - for (const auto& el : *j.m_value.array) - { - write_ubjson(el, use_count, use_type, prefix_required); - } - - if (!use_count) - { - oa->write_character(to_char_type(']')); - } - - break; - } - - case value_t::binary: - { - if (add_prefix) - { - oa->write_character(to_char_type('[')); - } - - if (use_type && !j.m_value.binary->empty()) - { - JSON_ASSERT(use_count); - oa->write_character(to_char_type('$')); - oa->write_character('U'); - } - - if (use_count) - { - oa->write_character(to_char_type('#')); - write_number_with_ubjson_prefix(j.m_value.binary->size(), true); - } - - if (use_type) - { - oa->write_characters( - reinterpret_cast(j.m_value.binary->data()), - j.m_value.binary->size()); - } - else - { - for (size_t i = 0; i < j.m_value.binary->size(); ++i) - { - oa->write_character(to_char_type('U')); - oa->write_character(j.m_value.binary->data()[i]); - } - } - - if (!use_count) - { - oa->write_character(to_char_type(']')); - } - - break; - } - - case value_t::object: - { - if (add_prefix) - { - oa->write_character(to_char_type('{')); - } - - bool prefix_required = true; - if (use_type && !j.m_value.object->empty()) - { - JSON_ASSERT(use_count); - const CharType first_prefix = ubjson_prefix(j.front()); - const bool same_prefix = std::all_of(j.begin(), j.end(), - [this, first_prefix](const BasicJsonType & v) - { - return ubjson_prefix(v) == first_prefix; - }); - - if (same_prefix) - { - prefix_required = false; - oa->write_character(to_char_type('$')); - oa->write_character(first_prefix); - } - } - - if (use_count) - { - oa->write_character(to_char_type('#')); - write_number_with_ubjson_prefix(j.m_value.object->size(), true); - } - - for (const auto& el : *j.m_value.object) - { - write_number_with_ubjson_prefix(el.first.size(), true); - oa->write_characters( - reinterpret_cast(el.first.c_str()), - el.first.size()); - write_ubjson(el.second, use_count, use_type, prefix_required); - } - - if (!use_count) - { - oa->write_character(to_char_type('}')); - } - - break; - } - - case value_t::discarded: - default: - break; - } - } - - private: - ////////// - // BSON // - ////////// - - /*! - @return The size of a BSON document entry header, including the id marker - and the entry name size (and its null-terminator). - */ - static std::size_t calc_bson_entry_header_size(const string_t& name, const BasicJsonType& j) - { - const auto it = name.find(static_cast(0)); - if (JSON_HEDLEY_UNLIKELY(it != BasicJsonType::string_t::npos)) - { - JSON_THROW(out_of_range::create(409, "BSON key cannot contain code point U+0000 (at byte " + std::to_string(it) + ")", j)); - static_cast(j); - } - - return /*id*/ 1ul + name.size() + /*zero-terminator*/1u; - } - - /*! - @brief Writes the given @a element_type and @a name to the output adapter - */ - void write_bson_entry_header(const string_t& name, - const std::uint8_t element_type) - { - oa->write_character(to_char_type(element_type)); // boolean - oa->write_characters( - reinterpret_cast(name.c_str()), - name.size() + 1u); - } - - /*! - @brief Writes a BSON element with key @a name and boolean value @a value - */ - void write_bson_boolean(const string_t& name, - const bool value) - { - write_bson_entry_header(name, 0x08); - oa->write_character(value ? to_char_type(0x01) : to_char_type(0x00)); - } - - /*! - @brief Writes a BSON element with key @a name and double value @a value - */ - void write_bson_double(const string_t& name, - const double value) - { - write_bson_entry_header(name, 0x01); - write_number(value); - } - - /*! - @return The size of the BSON-encoded string in @a value - */ - static std::size_t calc_bson_string_size(const string_t& value) - { - return sizeof(std::int32_t) + value.size() + 1ul; - } - - /*! - @brief Writes a BSON element with key @a name and string value @a value - */ - void write_bson_string(const string_t& name, - const string_t& value) - { - write_bson_entry_header(name, 0x02); - - write_number(static_cast(value.size() + 1ul)); - oa->write_characters( - reinterpret_cast(value.c_str()), - value.size() + 1); - } - - /*! - @brief Writes a BSON element with key @a name and null value - */ - void write_bson_null(const string_t& name) - { - write_bson_entry_header(name, 0x0A); - } - - /*! - @return The size of the BSON-encoded integer @a value - */ - static std::size_t calc_bson_integer_size(const std::int64_t value) - { - return (std::numeric_limits::min)() <= value && value <= (std::numeric_limits::max)() - ? sizeof(std::int32_t) - : sizeof(std::int64_t); - } - - /*! - @brief Writes a BSON element with key @a name and integer @a value - */ - void write_bson_integer(const string_t& name, - const std::int64_t value) - { - if ((std::numeric_limits::min)() <= value && value <= (std::numeric_limits::max)()) - { - write_bson_entry_header(name, 0x10); // int32 - write_number(static_cast(value)); - } - else - { - write_bson_entry_header(name, 0x12); // int64 - write_number(static_cast(value)); - } - } - - /*! - @return The size of the BSON-encoded unsigned integer in @a j - */ - static constexpr std::size_t calc_bson_unsigned_size(const std::uint64_t value) noexcept - { - return (value <= static_cast((std::numeric_limits::max)())) - ? sizeof(std::int32_t) - : sizeof(std::int64_t); - } - - /*! - @brief Writes a BSON element with key @a name and unsigned @a value - */ - void write_bson_unsigned(const string_t& name, - const BasicJsonType& j) - { - if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) - { - write_bson_entry_header(name, 0x10 /* int32 */); - write_number(static_cast(j.m_value.number_unsigned)); - } - else if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) - { - write_bson_entry_header(name, 0x12 /* int64 */); - write_number(static_cast(j.m_value.number_unsigned)); - } - else - { - JSON_THROW(out_of_range::create(407, "integer number " + std::to_string(j.m_value.number_unsigned) + " cannot be represented by BSON as it does not fit int64", j)); - } - } - - /*! - @brief Writes a BSON element with key @a name and object @a value - */ - void write_bson_object_entry(const string_t& name, - const typename BasicJsonType::object_t& value) - { - write_bson_entry_header(name, 0x03); // object - write_bson_object(value); - } - - /*! - @return The size of the BSON-encoded array @a value - */ - static std::size_t calc_bson_array_size(const typename BasicJsonType::array_t& value) - { - std::size_t array_index = 0ul; - - const std::size_t embedded_document_size = std::accumulate(std::begin(value), std::end(value), std::size_t(0), [&array_index](std::size_t result, const typename BasicJsonType::array_t::value_type & el) - { - return result + calc_bson_element_size(std::to_string(array_index++), el); - }); - - return sizeof(std::int32_t) + embedded_document_size + 1ul; - } - - /*! - @return The size of the BSON-encoded binary array @a value - */ - static std::size_t calc_bson_binary_size(const typename BasicJsonType::binary_t& value) - { - return sizeof(std::int32_t) + value.size() + 1ul; - } - - /*! - @brief Writes a BSON element with key @a name and array @a value - */ - void write_bson_array(const string_t& name, - const typename BasicJsonType::array_t& value) - { - write_bson_entry_header(name, 0x04); // array - write_number(static_cast(calc_bson_array_size(value))); - - std::size_t array_index = 0ul; - - for (const auto& el : value) - { - write_bson_element(std::to_string(array_index++), el); - } - - oa->write_character(to_char_type(0x00)); - } - - /*! - @brief Writes a BSON element with key @a name and binary value @a value - */ - void write_bson_binary(const string_t& name, - const binary_t& value) - { - write_bson_entry_header(name, 0x05); - - write_number(static_cast(value.size())); - write_number(value.has_subtype() ? static_cast(value.subtype()) : std::uint8_t(0x00)); - - oa->write_characters(reinterpret_cast(value.data()), value.size()); - } - - /*! - @brief Calculates the size necessary to serialize the JSON value @a j with its @a name - @return The calculated size for the BSON document entry for @a j with the given @a name. - */ - static std::size_t calc_bson_element_size(const string_t& name, - const BasicJsonType& j) - { - const auto header_size = calc_bson_entry_header_size(name, j); - switch (j.type()) - { - case value_t::object: - return header_size + calc_bson_object_size(*j.m_value.object); - - case value_t::array: - return header_size + calc_bson_array_size(*j.m_value.array); - - case value_t::binary: - return header_size + calc_bson_binary_size(*j.m_value.binary); - - case value_t::boolean: - return header_size + 1ul; - - case value_t::number_float: - return header_size + 8ul; - - case value_t::number_integer: - return header_size + calc_bson_integer_size(j.m_value.number_integer); - - case value_t::number_unsigned: - return header_size + calc_bson_unsigned_size(j.m_value.number_unsigned); - - case value_t::string: - return header_size + calc_bson_string_size(*j.m_value.string); - - case value_t::null: - return header_size + 0ul; - - // LCOV_EXCL_START - case value_t::discarded: - default: - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) - return 0ul; - // LCOV_EXCL_STOP - } - } - - /*! - @brief Serializes the JSON value @a j to BSON and associates it with the - key @a name. - @param name The name to associate with the JSON entity @a j within the - current BSON document - */ - void write_bson_element(const string_t& name, - const BasicJsonType& j) - { - switch (j.type()) - { - case value_t::object: - return write_bson_object_entry(name, *j.m_value.object); - - case value_t::array: - return write_bson_array(name, *j.m_value.array); - - case value_t::binary: - return write_bson_binary(name, *j.m_value.binary); - - case value_t::boolean: - return write_bson_boolean(name, j.m_value.boolean); - - case value_t::number_float: - return write_bson_double(name, j.m_value.number_float); - - case value_t::number_integer: - return write_bson_integer(name, j.m_value.number_integer); - - case value_t::number_unsigned: - return write_bson_unsigned(name, j); - - case value_t::string: - return write_bson_string(name, *j.m_value.string); - - case value_t::null: - return write_bson_null(name); - - // LCOV_EXCL_START - case value_t::discarded: - default: - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) - return; - // LCOV_EXCL_STOP - } - } - - /*! - @brief Calculates the size of the BSON serialization of the given - JSON-object @a j. - @param[in] value JSON value to serialize - @pre value.type() == value_t::object - */ - static std::size_t calc_bson_object_size(const typename BasicJsonType::object_t& value) - { - std::size_t document_size = std::accumulate(value.begin(), value.end(), std::size_t(0), - [](size_t result, const typename BasicJsonType::object_t::value_type & el) - { - return result += calc_bson_element_size(el.first, el.second); - }); - - return sizeof(std::int32_t) + document_size + 1ul; - } - - /*! - @param[in] value JSON value to serialize - @pre value.type() == value_t::object - */ - void write_bson_object(const typename BasicJsonType::object_t& value) - { - write_number(static_cast(calc_bson_object_size(value))); - - for (const auto& el : value) - { - write_bson_element(el.first, el.second); - } - - oa->write_character(to_char_type(0x00)); - } - - ////////// - // CBOR // - ////////// - - static constexpr CharType get_cbor_float_prefix(float /*unused*/) - { - return to_char_type(0xFA); // Single-Precision Float - } - - static constexpr CharType get_cbor_float_prefix(double /*unused*/) - { - return to_char_type(0xFB); // Double-Precision Float - } - - ///////////// - // MsgPack // - ///////////// - - static constexpr CharType get_msgpack_float_prefix(float /*unused*/) - { - return to_char_type(0xCA); // float 32 - } - - static constexpr CharType get_msgpack_float_prefix(double /*unused*/) - { - return to_char_type(0xCB); // float 64 - } - - //////////// - // UBJSON // - //////////// - - // UBJSON: write number (floating point) - template::value, int>::type = 0> - void write_number_with_ubjson_prefix(const NumberType n, - const bool add_prefix) - { - if (add_prefix) - { - oa->write_character(get_ubjson_float_prefix(n)); - } - write_number(n); - } - - // UBJSON: write number (unsigned integer) - template::value, int>::type = 0> - void write_number_with_ubjson_prefix(const NumberType n, - const bool add_prefix) - { - if (n <= static_cast((std::numeric_limits::max)())) - { - if (add_prefix) - { - oa->write_character(to_char_type('i')); // int8 - } - write_number(static_cast(n)); - } - else if (n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(to_char_type('U')); // uint8 - } - write_number(static_cast(n)); - } - else if (n <= static_cast((std::numeric_limits::max)())) - { - if (add_prefix) - { - oa->write_character(to_char_type('I')); // int16 - } - write_number(static_cast(n)); - } - else if (n <= static_cast((std::numeric_limits::max)())) - { - if (add_prefix) - { - oa->write_character(to_char_type('l')); // int32 - } - write_number(static_cast(n)); - } - else if (n <= static_cast((std::numeric_limits::max)())) - { - if (add_prefix) - { - oa->write_character(to_char_type('L')); // int64 - } - write_number(static_cast(n)); - } - else - { - if (add_prefix) - { - oa->write_character(to_char_type('H')); // high-precision number - } - - const auto number = BasicJsonType(n).dump(); - write_number_with_ubjson_prefix(number.size(), true); - for (std::size_t i = 0; i < number.size(); ++i) - { - oa->write_character(to_char_type(static_cast(number[i]))); - } - } - } - - // UBJSON: write number (signed integer) - template < typename NumberType, typename std::enable_if < - std::is_signed::value&& - !std::is_floating_point::value, int >::type = 0 > - void write_number_with_ubjson_prefix(const NumberType n, - const bool add_prefix) - { - if ((std::numeric_limits::min)() <= n && n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(to_char_type('i')); // int8 - } - write_number(static_cast(n)); - } - else if (static_cast((std::numeric_limits::min)()) <= n && n <= static_cast((std::numeric_limits::max)())) - { - if (add_prefix) - { - oa->write_character(to_char_type('U')); // uint8 - } - write_number(static_cast(n)); - } - else if ((std::numeric_limits::min)() <= n && n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(to_char_type('I')); // int16 - } - write_number(static_cast(n)); - } - else if ((std::numeric_limits::min)() <= n && n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(to_char_type('l')); // int32 - } - write_number(static_cast(n)); - } - else if ((std::numeric_limits::min)() <= n && n <= (std::numeric_limits::max)()) - { - if (add_prefix) - { - oa->write_character(to_char_type('L')); // int64 - } - write_number(static_cast(n)); - } - // LCOV_EXCL_START - else - { - if (add_prefix) - { - oa->write_character(to_char_type('H')); // high-precision number - } - - const auto number = BasicJsonType(n).dump(); - write_number_with_ubjson_prefix(number.size(), true); - for (std::size_t i = 0; i < number.size(); ++i) - { - oa->write_character(to_char_type(static_cast(number[i]))); - } - } - // LCOV_EXCL_STOP - } - - /*! - @brief determine the type prefix of container values - */ - CharType ubjson_prefix(const BasicJsonType& j) const noexcept - { - switch (j.type()) - { - case value_t::null: - return 'Z'; - - case value_t::boolean: - return j.m_value.boolean ? 'T' : 'F'; - - case value_t::number_integer: - { - if ((std::numeric_limits::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits::max)()) - { - return 'i'; - } - if ((std::numeric_limits::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits::max)()) - { - return 'U'; - } - if ((std::numeric_limits::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits::max)()) - { - return 'I'; - } - if ((std::numeric_limits::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits::max)()) - { - return 'l'; - } - if ((std::numeric_limits::min)() <= j.m_value.number_integer && j.m_value.number_integer <= (std::numeric_limits::max)()) - { - return 'L'; - } - // anything else is treated as high-precision number - return 'H'; // LCOV_EXCL_LINE - } - - case value_t::number_unsigned: - { - if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) - { - return 'i'; - } - if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) - { - return 'U'; - } - if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) - { - return 'I'; - } - if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) - { - return 'l'; - } - if (j.m_value.number_unsigned <= static_cast((std::numeric_limits::max)())) - { - return 'L'; - } - // anything else is treated as high-precision number - return 'H'; // LCOV_EXCL_LINE - } - - case value_t::number_float: - return get_ubjson_float_prefix(j.m_value.number_float); - - case value_t::string: - return 'S'; - - case value_t::array: // fallthrough - case value_t::binary: - return '['; - - case value_t::object: - return '{'; - - case value_t::discarded: - default: // discarded values - return 'N'; - } - } - - static constexpr CharType get_ubjson_float_prefix(float /*unused*/) - { - return 'd'; // float 32 - } - - static constexpr CharType get_ubjson_float_prefix(double /*unused*/) - { - return 'D'; // float 64 - } - - /////////////////////// - // Utility functions // - /////////////////////// - - /* - @brief write a number to output input - @param[in] n number of type @a NumberType - @tparam NumberType the type of the number - @tparam OutputIsLittleEndian Set to true if output data is - required to be little endian - - @note This function needs to respect the system's endianess, because bytes - in CBOR, MessagePack, and UBJSON are stored in network order (big - endian) and therefore need reordering on little endian systems. - */ - template - void write_number(const NumberType n) - { - // step 1: write number to array of length NumberType - std::array vec{}; - std::memcpy(vec.data(), &n, sizeof(NumberType)); - - // step 2: write array to output (with possible reordering) - if (is_little_endian != OutputIsLittleEndian) - { - // reverse byte order prior to conversion if necessary - std::reverse(vec.begin(), vec.end()); - } - - oa->write_characters(vec.data(), sizeof(NumberType)); - } - - void write_compact_float(const number_float_t n, detail::input_format_t format) - { -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wfloat-equal" -#endif - if (static_cast(n) >= static_cast(std::numeric_limits::lowest()) && - static_cast(n) <= static_cast((std::numeric_limits::max)()) && - static_cast(static_cast(n)) == static_cast(n)) - { - oa->write_character(format == detail::input_format_t::cbor - ? get_cbor_float_prefix(static_cast(n)) - : get_msgpack_float_prefix(static_cast(n))); - write_number(static_cast(n)); - } - else - { - oa->write_character(format == detail::input_format_t::cbor - ? get_cbor_float_prefix(n) - : get_msgpack_float_prefix(n)); - write_number(n); - } -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif - } - - public: - // The following to_char_type functions are implement the conversion - // between uint8_t and CharType. In case CharType is not unsigned, - // such a conversion is required to allow values greater than 128. - // See for a discussion. - template < typename C = CharType, - enable_if_t < std::is_signed::value && std::is_signed::value > * = nullptr > - static constexpr CharType to_char_type(std::uint8_t x) noexcept - { - return *reinterpret_cast(&x); - } - - template < typename C = CharType, - enable_if_t < std::is_signed::value && std::is_unsigned::value > * = nullptr > - static CharType to_char_type(std::uint8_t x) noexcept - { - static_assert(sizeof(std::uint8_t) == sizeof(CharType), "size of CharType must be equal to std::uint8_t"); - static_assert(std::is_trivial::value, "CharType must be trivial"); - CharType result; - std::memcpy(&result, &x, sizeof(x)); - return result; - } - - template::value>* = nullptr> - static constexpr CharType to_char_type(std::uint8_t x) noexcept - { - return x; - } - - template < typename InputCharType, typename C = CharType, - enable_if_t < - std::is_signed::value && - std::is_signed::value && - std::is_same::type>::value - > * = nullptr > - static constexpr CharType to_char_type(InputCharType x) noexcept - { - return x; - } - - private: - /// whether we can assume little endianess - const bool is_little_endian = little_endianess(); - - /// the output - output_adapter_t oa = nullptr; -}; -} // namespace detail -} // namespace nlohmann - -// #include - -// #include - - -#include // reverse, remove, fill, find, none_of -#include // array -#include // localeconv, lconv -#include // labs, isfinite, isnan, signbit -#include // size_t, ptrdiff_t -#include // uint8_t -#include // snprintf -#include // numeric_limits -#include // string, char_traits -#include // is_same -#include // move - -// #include - - -#include // array -#include // signbit, isfinite -#include // intN_t, uintN_t -#include // memcpy, memmove -#include // numeric_limits -#include // conditional - -// #include - - -namespace nlohmann -{ -namespace detail -{ - -/*! -@brief implements the Grisu2 algorithm for binary to decimal floating-point -conversion. - -This implementation is a slightly modified version of the reference -implementation which may be obtained from -http://florian.loitsch.com/publications (bench.tar.gz). - -The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch. - -For a detailed description of the algorithm see: - -[1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with - Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming - Language Design and Implementation, PLDI 2010 -[2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately", - Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language - Design and Implementation, PLDI 1996 -*/ -namespace dtoa_impl -{ - -template -Target reinterpret_bits(const Source source) -{ - static_assert(sizeof(Target) == sizeof(Source), "size mismatch"); - - Target target; - std::memcpy(&target, &source, sizeof(Source)); - return target; -} - -struct diyfp // f * 2^e -{ - static constexpr int kPrecision = 64; // = q - - std::uint64_t f = 0; - int e = 0; - - constexpr diyfp(std::uint64_t f_, int e_) noexcept : f(f_), e(e_) {} - - /*! - @brief returns x - y - @pre x.e == y.e and x.f >= y.f - */ - static diyfp sub(const diyfp& x, const diyfp& y) noexcept - { - JSON_ASSERT(x.e == y.e); - JSON_ASSERT(x.f >= y.f); - - return {x.f - y.f, x.e}; - } - - /*! - @brief returns x * y - @note The result is rounded. (Only the upper q bits are returned.) - */ - static diyfp mul(const diyfp& x, const diyfp& y) noexcept - { - static_assert(kPrecision == 64, "internal error"); - - // Computes: - // f = round((x.f * y.f) / 2^q) - // e = x.e + y.e + q - - // Emulate the 64-bit * 64-bit multiplication: - // - // p = u * v - // = (u_lo + 2^32 u_hi) (v_lo + 2^32 v_hi) - // = (u_lo v_lo ) + 2^32 ((u_lo v_hi ) + (u_hi v_lo )) + 2^64 (u_hi v_hi ) - // = (p0 ) + 2^32 ((p1 ) + (p2 )) + 2^64 (p3 ) - // = (p0_lo + 2^32 p0_hi) + 2^32 ((p1_lo + 2^32 p1_hi) + (p2_lo + 2^32 p2_hi)) + 2^64 (p3 ) - // = (p0_lo ) + 2^32 (p0_hi + p1_lo + p2_lo ) + 2^64 (p1_hi + p2_hi + p3) - // = (p0_lo ) + 2^32 (Q ) + 2^64 (H ) - // = (p0_lo ) + 2^32 (Q_lo + 2^32 Q_hi ) + 2^64 (H ) - // - // (Since Q might be larger than 2^32 - 1) - // - // = (p0_lo + 2^32 Q_lo) + 2^64 (Q_hi + H) - // - // (Q_hi + H does not overflow a 64-bit int) - // - // = p_lo + 2^64 p_hi - - const std::uint64_t u_lo = x.f & 0xFFFFFFFFu; - const std::uint64_t u_hi = x.f >> 32u; - const std::uint64_t v_lo = y.f & 0xFFFFFFFFu; - const std::uint64_t v_hi = y.f >> 32u; - - const std::uint64_t p0 = u_lo * v_lo; - const std::uint64_t p1 = u_lo * v_hi; - const std::uint64_t p2 = u_hi * v_lo; - const std::uint64_t p3 = u_hi * v_hi; - - const std::uint64_t p0_hi = p0 >> 32u; - const std::uint64_t p1_lo = p1 & 0xFFFFFFFFu; - const std::uint64_t p1_hi = p1 >> 32u; - const std::uint64_t p2_lo = p2 & 0xFFFFFFFFu; - const std::uint64_t p2_hi = p2 >> 32u; - - std::uint64_t Q = p0_hi + p1_lo + p2_lo; - - // The full product might now be computed as - // - // p_hi = p3 + p2_hi + p1_hi + (Q >> 32) - // p_lo = p0_lo + (Q << 32) - // - // But in this particular case here, the full p_lo is not required. - // Effectively we only need to add the highest bit in p_lo to p_hi (and - // Q_hi + 1 does not overflow). - - Q += std::uint64_t{1} << (64u - 32u - 1u); // round, ties up - - const std::uint64_t h = p3 + p2_hi + p1_hi + (Q >> 32u); - - return {h, x.e + y.e + 64}; - } - - /*! - @brief normalize x such that the significand is >= 2^(q-1) - @pre x.f != 0 - */ - static diyfp normalize(diyfp x) noexcept - { - JSON_ASSERT(x.f != 0); - - while ((x.f >> 63u) == 0) - { - x.f <<= 1u; - x.e--; - } - - return x; - } - - /*! - @brief normalize x such that the result has the exponent E - @pre e >= x.e and the upper e - x.e bits of x.f must be zero. - */ - static diyfp normalize_to(const diyfp& x, const int target_exponent) noexcept - { - const int delta = x.e - target_exponent; - - JSON_ASSERT(delta >= 0); - JSON_ASSERT(((x.f << delta) >> delta) == x.f); - - return {x.f << delta, target_exponent}; - } -}; - -struct boundaries -{ - diyfp w; - diyfp minus; - diyfp plus; -}; - -/*! -Compute the (normalized) diyfp representing the input number 'value' and its -boundaries. - -@pre value must be finite and positive -*/ -template -boundaries compute_boundaries(FloatType value) -{ - JSON_ASSERT(std::isfinite(value)); - JSON_ASSERT(value > 0); - - // Convert the IEEE representation into a diyfp. - // - // If v is denormal: - // value = 0.F * 2^(1 - bias) = ( F) * 2^(1 - bias - (p-1)) - // If v is normalized: - // value = 1.F * 2^(E - bias) = (2^(p-1) + F) * 2^(E - bias - (p-1)) - - static_assert(std::numeric_limits::is_iec559, - "internal error: dtoa_short requires an IEEE-754 floating-point implementation"); - - constexpr int kPrecision = std::numeric_limits::digits; // = p (includes the hidden bit) - constexpr int kBias = std::numeric_limits::max_exponent - 1 + (kPrecision - 1); - constexpr int kMinExp = 1 - kBias; - constexpr std::uint64_t kHiddenBit = std::uint64_t{1} << (kPrecision - 1); // = 2^(p-1) - - using bits_type = typename std::conditional::type; - - const auto bits = static_cast(reinterpret_bits(value)); - const std::uint64_t E = bits >> (kPrecision - 1); - const std::uint64_t F = bits & (kHiddenBit - 1); - - const bool is_denormal = E == 0; - const diyfp v = is_denormal - ? diyfp(F, kMinExp) - : diyfp(F + kHiddenBit, static_cast(E) - kBias); - - // Compute the boundaries m- and m+ of the floating-point value - // v = f * 2^e. - // - // Determine v- and v+, the floating-point predecessor and successor if v, - // respectively. - // - // v- = v - 2^e if f != 2^(p-1) or e == e_min (A) - // = v - 2^(e-1) if f == 2^(p-1) and e > e_min (B) - // - // v+ = v + 2^e - // - // Let m- = (v- + v) / 2 and m+ = (v + v+) / 2. All real numbers _strictly_ - // between m- and m+ round to v, regardless of how the input rounding - // algorithm breaks ties. - // - // ---+-------------+-------------+-------------+-------------+--- (A) - // v- m- v m+ v+ - // - // -----------------+------+------+-------------+-------------+--- (B) - // v- m- v m+ v+ - - const bool lower_boundary_is_closer = F == 0 && E > 1; - const diyfp m_plus = diyfp(2 * v.f + 1, v.e - 1); - const diyfp m_minus = lower_boundary_is_closer - ? diyfp(4 * v.f - 1, v.e - 2) // (B) - : diyfp(2 * v.f - 1, v.e - 1); // (A) - - // Determine the normalized w+ = m+. - const diyfp w_plus = diyfp::normalize(m_plus); - - // Determine w- = m- such that e_(w-) = e_(w+). - const diyfp w_minus = diyfp::normalize_to(m_minus, w_plus.e); - - return {diyfp::normalize(v), w_minus, w_plus}; -} - -// Given normalized diyfp w, Grisu needs to find a (normalized) cached -// power-of-ten c, such that the exponent of the product c * w = f * 2^e lies -// within a certain range [alpha, gamma] (Definition 3.2 from [1]) -// -// alpha <= e = e_c + e_w + q <= gamma -// -// or -// -// f_c * f_w * 2^alpha <= f_c 2^(e_c) * f_w 2^(e_w) * 2^q -// <= f_c * f_w * 2^gamma -// -// Since c and w are normalized, i.e. 2^(q-1) <= f < 2^q, this implies -// -// 2^(q-1) * 2^(q-1) * 2^alpha <= c * w * 2^q < 2^q * 2^q * 2^gamma -// -// or -// -// 2^(q - 2 + alpha) <= c * w < 2^(q + gamma) -// -// The choice of (alpha,gamma) determines the size of the table and the form of -// the digit generation procedure. Using (alpha,gamma)=(-60,-32) works out well -// in practice: -// -// The idea is to cut the number c * w = f * 2^e into two parts, which can be -// processed independently: An integral part p1, and a fractional part p2: -// -// f * 2^e = ( (f div 2^-e) * 2^-e + (f mod 2^-e) ) * 2^e -// = (f div 2^-e) + (f mod 2^-e) * 2^e -// = p1 + p2 * 2^e -// -// The conversion of p1 into decimal form requires a series of divisions and -// modulos by (a power of) 10. These operations are faster for 32-bit than for -// 64-bit integers, so p1 should ideally fit into a 32-bit integer. This can be -// achieved by choosing -// -// -e >= 32 or e <= -32 := gamma -// -// In order to convert the fractional part -// -// p2 * 2^e = p2 / 2^-e = d[-1] / 10^1 + d[-2] / 10^2 + ... -// -// into decimal form, the fraction is repeatedly multiplied by 10 and the digits -// d[-i] are extracted in order: -// -// (10 * p2) div 2^-e = d[-1] -// (10 * p2) mod 2^-e = d[-2] / 10^1 + ... -// -// The multiplication by 10 must not overflow. It is sufficient to choose -// -// 10 * p2 < 16 * p2 = 2^4 * p2 <= 2^64. -// -// Since p2 = f mod 2^-e < 2^-e, -// -// -e <= 60 or e >= -60 := alpha - -constexpr int kAlpha = -60; -constexpr int kGamma = -32; - -struct cached_power // c = f * 2^e ~= 10^k -{ - std::uint64_t f; - int e; - int k; -}; - -/*! -For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached -power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c -satisfies (Definition 3.2 from [1]) - - alpha <= e_c + e + q <= gamma. -*/ -inline cached_power get_cached_power_for_binary_exponent(int e) -{ - // Now - // - // alpha <= e_c + e + q <= gamma (1) - // ==> f_c * 2^alpha <= c * 2^e * 2^q - // - // and since the c's are normalized, 2^(q-1) <= f_c, - // - // ==> 2^(q - 1 + alpha) <= c * 2^(e + q) - // ==> 2^(alpha - e - 1) <= c - // - // If c were an exact power of ten, i.e. c = 10^k, one may determine k as - // - // k = ceil( log_10( 2^(alpha - e - 1) ) ) - // = ceil( (alpha - e - 1) * log_10(2) ) - // - // From the paper: - // "In theory the result of the procedure could be wrong since c is rounded, - // and the computation itself is approximated [...]. In practice, however, - // this simple function is sufficient." - // - // For IEEE double precision floating-point numbers converted into - // normalized diyfp's w = f * 2^e, with q = 64, - // - // e >= -1022 (min IEEE exponent) - // -52 (p - 1) - // -52 (p - 1, possibly normalize denormal IEEE numbers) - // -11 (normalize the diyfp) - // = -1137 - // - // and - // - // e <= +1023 (max IEEE exponent) - // -52 (p - 1) - // -11 (normalize the diyfp) - // = 960 - // - // This binary exponent range [-1137,960] results in a decimal exponent - // range [-307,324]. One does not need to store a cached power for each - // k in this range. For each such k it suffices to find a cached power - // such that the exponent of the product lies in [alpha,gamma]. - // This implies that the difference of the decimal exponents of adjacent - // table entries must be less than or equal to - // - // floor( (gamma - alpha) * log_10(2) ) = 8. - // - // (A smaller distance gamma-alpha would require a larger table.) - - // NB: - // Actually this function returns c, such that -60 <= e_c + e + 64 <= -34. - - constexpr int kCachedPowersMinDecExp = -300; - constexpr int kCachedPowersDecStep = 8; - - static constexpr std::array kCachedPowers = - { - { - { 0xAB70FE17C79AC6CA, -1060, -300 }, - { 0xFF77B1FCBEBCDC4F, -1034, -292 }, - { 0xBE5691EF416BD60C, -1007, -284 }, - { 0x8DD01FAD907FFC3C, -980, -276 }, - { 0xD3515C2831559A83, -954, -268 }, - { 0x9D71AC8FADA6C9B5, -927, -260 }, - { 0xEA9C227723EE8BCB, -901, -252 }, - { 0xAECC49914078536D, -874, -244 }, - { 0x823C12795DB6CE57, -847, -236 }, - { 0xC21094364DFB5637, -821, -228 }, - { 0x9096EA6F3848984F, -794, -220 }, - { 0xD77485CB25823AC7, -768, -212 }, - { 0xA086CFCD97BF97F4, -741, -204 }, - { 0xEF340A98172AACE5, -715, -196 }, - { 0xB23867FB2A35B28E, -688, -188 }, - { 0x84C8D4DFD2C63F3B, -661, -180 }, - { 0xC5DD44271AD3CDBA, -635, -172 }, - { 0x936B9FCEBB25C996, -608, -164 }, - { 0xDBAC6C247D62A584, -582, -156 }, - { 0xA3AB66580D5FDAF6, -555, -148 }, - { 0xF3E2F893DEC3F126, -529, -140 }, - { 0xB5B5ADA8AAFF80B8, -502, -132 }, - { 0x87625F056C7C4A8B, -475, -124 }, - { 0xC9BCFF6034C13053, -449, -116 }, - { 0x964E858C91BA2655, -422, -108 }, - { 0xDFF9772470297EBD, -396, -100 }, - { 0xA6DFBD9FB8E5B88F, -369, -92 }, - { 0xF8A95FCF88747D94, -343, -84 }, - { 0xB94470938FA89BCF, -316, -76 }, - { 0x8A08F0F8BF0F156B, -289, -68 }, - { 0xCDB02555653131B6, -263, -60 }, - { 0x993FE2C6D07B7FAC, -236, -52 }, - { 0xE45C10C42A2B3B06, -210, -44 }, - { 0xAA242499697392D3, -183, -36 }, - { 0xFD87B5F28300CA0E, -157, -28 }, - { 0xBCE5086492111AEB, -130, -20 }, - { 0x8CBCCC096F5088CC, -103, -12 }, - { 0xD1B71758E219652C, -77, -4 }, - { 0x9C40000000000000, -50, 4 }, - { 0xE8D4A51000000000, -24, 12 }, - { 0xAD78EBC5AC620000, 3, 20 }, - { 0x813F3978F8940984, 30, 28 }, - { 0xC097CE7BC90715B3, 56, 36 }, - { 0x8F7E32CE7BEA5C70, 83, 44 }, - { 0xD5D238A4ABE98068, 109, 52 }, - { 0x9F4F2726179A2245, 136, 60 }, - { 0xED63A231D4C4FB27, 162, 68 }, - { 0xB0DE65388CC8ADA8, 189, 76 }, - { 0x83C7088E1AAB65DB, 216, 84 }, - { 0xC45D1DF942711D9A, 242, 92 }, - { 0x924D692CA61BE758, 269, 100 }, - { 0xDA01EE641A708DEA, 295, 108 }, - { 0xA26DA3999AEF774A, 322, 116 }, - { 0xF209787BB47D6B85, 348, 124 }, - { 0xB454E4A179DD1877, 375, 132 }, - { 0x865B86925B9BC5C2, 402, 140 }, - { 0xC83553C5C8965D3D, 428, 148 }, - { 0x952AB45CFA97A0B3, 455, 156 }, - { 0xDE469FBD99A05FE3, 481, 164 }, - { 0xA59BC234DB398C25, 508, 172 }, - { 0xF6C69A72A3989F5C, 534, 180 }, - { 0xB7DCBF5354E9BECE, 561, 188 }, - { 0x88FCF317F22241E2, 588, 196 }, - { 0xCC20CE9BD35C78A5, 614, 204 }, - { 0x98165AF37B2153DF, 641, 212 }, - { 0xE2A0B5DC971F303A, 667, 220 }, - { 0xA8D9D1535CE3B396, 694, 228 }, - { 0xFB9B7CD9A4A7443C, 720, 236 }, - { 0xBB764C4CA7A44410, 747, 244 }, - { 0x8BAB8EEFB6409C1A, 774, 252 }, - { 0xD01FEF10A657842C, 800, 260 }, - { 0x9B10A4E5E9913129, 827, 268 }, - { 0xE7109BFBA19C0C9D, 853, 276 }, - { 0xAC2820D9623BF429, 880, 284 }, - { 0x80444B5E7AA7CF85, 907, 292 }, - { 0xBF21E44003ACDD2D, 933, 300 }, - { 0x8E679C2F5E44FF8F, 960, 308 }, - { 0xD433179D9C8CB841, 986, 316 }, - { 0x9E19DB92B4E31BA9, 1013, 324 }, - } - }; - - // This computation gives exactly the same results for k as - // k = ceil((kAlpha - e - 1) * 0.30102999566398114) - // for |e| <= 1500, but doesn't require floating-point operations. - // NB: log_10(2) ~= 78913 / 2^18 - JSON_ASSERT(e >= -1500); - JSON_ASSERT(e <= 1500); - const int f = kAlpha - e - 1; - const int k = (f * 78913) / (1 << 18) + static_cast(f > 0); - - const int index = (-kCachedPowersMinDecExp + k + (kCachedPowersDecStep - 1)) / kCachedPowersDecStep; - JSON_ASSERT(index >= 0); - JSON_ASSERT(static_cast(index) < kCachedPowers.size()); - - const cached_power cached = kCachedPowers[static_cast(index)]; - JSON_ASSERT(kAlpha <= cached.e + e + 64); - JSON_ASSERT(kGamma >= cached.e + e + 64); - - return cached; -} - -/*! -For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k. -For n == 0, returns 1 and sets pow10 := 1. -*/ -inline int find_largest_pow10(const std::uint32_t n, std::uint32_t& pow10) -{ - // LCOV_EXCL_START - if (n >= 1000000000) - { - pow10 = 1000000000; - return 10; - } - // LCOV_EXCL_STOP - if (n >= 100000000) - { - pow10 = 100000000; - return 9; - } - if (n >= 10000000) - { - pow10 = 10000000; - return 8; - } - if (n >= 1000000) - { - pow10 = 1000000; - return 7; - } - if (n >= 100000) - { - pow10 = 100000; - return 6; - } - if (n >= 10000) - { - pow10 = 10000; - return 5; - } - if (n >= 1000) - { - pow10 = 1000; - return 4; - } - if (n >= 100) - { - pow10 = 100; - return 3; - } - if (n >= 10) - { - pow10 = 10; - return 2; - } - - pow10 = 1; - return 1; -} - -inline void grisu2_round(char* buf, int len, std::uint64_t dist, std::uint64_t delta, - std::uint64_t rest, std::uint64_t ten_k) -{ - JSON_ASSERT(len >= 1); - JSON_ASSERT(dist <= delta); - JSON_ASSERT(rest <= delta); - JSON_ASSERT(ten_k > 0); - - // <--------------------------- delta ----> - // <---- dist ---------> - // --------------[------------------+-------------------]-------------- - // M- w M+ - // - // ten_k - // <------> - // <---- rest ----> - // --------------[------------------+----+--------------]-------------- - // w V - // = buf * 10^k - // - // ten_k represents a unit-in-the-last-place in the decimal representation - // stored in buf. - // Decrement buf by ten_k while this takes buf closer to w. - - // The tests are written in this order to avoid overflow in unsigned - // integer arithmetic. - - while (rest < dist - && delta - rest >= ten_k - && (rest + ten_k < dist || dist - rest > rest + ten_k - dist)) - { - JSON_ASSERT(buf[len - 1] != '0'); - buf[len - 1]--; - rest += ten_k; - } -} - -/*! -Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+. -M- and M+ must be normalized and share the same exponent -60 <= e <= -32. -*/ -inline void grisu2_digit_gen(char* buffer, int& length, int& decimal_exponent, - diyfp M_minus, diyfp w, diyfp M_plus) -{ - static_assert(kAlpha >= -60, "internal error"); - static_assert(kGamma <= -32, "internal error"); - - // Generates the digits (and the exponent) of a decimal floating-point - // number V = buffer * 10^decimal_exponent in the range [M-, M+]. The diyfp's - // w, M- and M+ share the same exponent e, which satisfies alpha <= e <= gamma. - // - // <--------------------------- delta ----> - // <---- dist ---------> - // --------------[------------------+-------------------]-------------- - // M- w M+ - // - // Grisu2 generates the digits of M+ from left to right and stops as soon as - // V is in [M-,M+]. - - JSON_ASSERT(M_plus.e >= kAlpha); - JSON_ASSERT(M_plus.e <= kGamma); - - std::uint64_t delta = diyfp::sub(M_plus, M_minus).f; // (significand of (M+ - M-), implicit exponent is e) - std::uint64_t dist = diyfp::sub(M_plus, w ).f; // (significand of (M+ - w ), implicit exponent is e) - - // Split M+ = f * 2^e into two parts p1 and p2 (note: e < 0): - // - // M+ = f * 2^e - // = ((f div 2^-e) * 2^-e + (f mod 2^-e)) * 2^e - // = ((p1 ) * 2^-e + (p2 )) * 2^e - // = p1 + p2 * 2^e - - const diyfp one(std::uint64_t{1} << -M_plus.e, M_plus.e); - - auto p1 = static_cast(M_plus.f >> -one.e); // p1 = f div 2^-e (Since -e >= 32, p1 fits into a 32-bit int.) - std::uint64_t p2 = M_plus.f & (one.f - 1); // p2 = f mod 2^-e - - // 1) - // - // Generate the digits of the integral part p1 = d[n-1]...d[1]d[0] - - JSON_ASSERT(p1 > 0); - - std::uint32_t pow10{}; - const int k = find_largest_pow10(p1, pow10); - - // 10^(k-1) <= p1 < 10^k, pow10 = 10^(k-1) - // - // p1 = (p1 div 10^(k-1)) * 10^(k-1) + (p1 mod 10^(k-1)) - // = (d[k-1] ) * 10^(k-1) + (p1 mod 10^(k-1)) - // - // M+ = p1 + p2 * 2^e - // = d[k-1] * 10^(k-1) + (p1 mod 10^(k-1)) + p2 * 2^e - // = d[k-1] * 10^(k-1) + ((p1 mod 10^(k-1)) * 2^-e + p2) * 2^e - // = d[k-1] * 10^(k-1) + ( rest) * 2^e - // - // Now generate the digits d[n] of p1 from left to right (n = k-1,...,0) - // - // p1 = d[k-1]...d[n] * 10^n + d[n-1]...d[0] - // - // but stop as soon as - // - // rest * 2^e = (d[n-1]...d[0] * 2^-e + p2) * 2^e <= delta * 2^e - - int n = k; - while (n > 0) - { - // Invariants: - // M+ = buffer * 10^n + (p1 + p2 * 2^e) (buffer = 0 for n = k) - // pow10 = 10^(n-1) <= p1 < 10^n - // - const std::uint32_t d = p1 / pow10; // d = p1 div 10^(n-1) - const std::uint32_t r = p1 % pow10; // r = p1 mod 10^(n-1) - // - // M+ = buffer * 10^n + (d * 10^(n-1) + r) + p2 * 2^e - // = (buffer * 10 + d) * 10^(n-1) + (r + p2 * 2^e) - // - JSON_ASSERT(d <= 9); - buffer[length++] = static_cast('0' + d); // buffer := buffer * 10 + d - // - // M+ = buffer * 10^(n-1) + (r + p2 * 2^e) - // - p1 = r; - n--; - // - // M+ = buffer * 10^n + (p1 + p2 * 2^e) - // pow10 = 10^n - // - - // Now check if enough digits have been generated. - // Compute - // - // p1 + p2 * 2^e = (p1 * 2^-e + p2) * 2^e = rest * 2^e - // - // Note: - // Since rest and delta share the same exponent e, it suffices to - // compare the significands. - const std::uint64_t rest = (std::uint64_t{p1} << -one.e) + p2; - if (rest <= delta) - { - // V = buffer * 10^n, with M- <= V <= M+. - - decimal_exponent += n; - - // We may now just stop. But instead look if the buffer could be - // decremented to bring V closer to w. - // - // pow10 = 10^n is now 1 ulp in the decimal representation V. - // The rounding procedure works with diyfp's with an implicit - // exponent of e. - // - // 10^n = (10^n * 2^-e) * 2^e = ulp * 2^e - // - const std::uint64_t ten_n = std::uint64_t{pow10} << -one.e; - grisu2_round(buffer, length, dist, delta, rest, ten_n); - - return; - } - - pow10 /= 10; - // - // pow10 = 10^(n-1) <= p1 < 10^n - // Invariants restored. - } - - // 2) - // - // The digits of the integral part have been generated: - // - // M+ = d[k-1]...d[1]d[0] + p2 * 2^e - // = buffer + p2 * 2^e - // - // Now generate the digits of the fractional part p2 * 2^e. - // - // Note: - // No decimal point is generated: the exponent is adjusted instead. - // - // p2 actually represents the fraction - // - // p2 * 2^e - // = p2 / 2^-e - // = d[-1] / 10^1 + d[-2] / 10^2 + ... - // - // Now generate the digits d[-m] of p1 from left to right (m = 1,2,...) - // - // p2 * 2^e = d[-1]d[-2]...d[-m] * 10^-m - // + 10^-m * (d[-m-1] / 10^1 + d[-m-2] / 10^2 + ...) - // - // using - // - // 10^m * p2 = ((10^m * p2) div 2^-e) * 2^-e + ((10^m * p2) mod 2^-e) - // = ( d) * 2^-e + ( r) - // - // or - // 10^m * p2 * 2^e = d + r * 2^e - // - // i.e. - // - // M+ = buffer + p2 * 2^e - // = buffer + 10^-m * (d + r * 2^e) - // = (buffer * 10^m + d) * 10^-m + 10^-m * r * 2^e - // - // and stop as soon as 10^-m * r * 2^e <= delta * 2^e - - JSON_ASSERT(p2 > delta); - - int m = 0; - for (;;) - { - // Invariant: - // M+ = buffer * 10^-m + 10^-m * (d[-m-1] / 10 + d[-m-2] / 10^2 + ...) * 2^e - // = buffer * 10^-m + 10^-m * (p2 ) * 2^e - // = buffer * 10^-m + 10^-m * (1/10 * (10 * p2) ) * 2^e - // = buffer * 10^-m + 10^-m * (1/10 * ((10*p2 div 2^-e) * 2^-e + (10*p2 mod 2^-e)) * 2^e - // - JSON_ASSERT(p2 <= (std::numeric_limits::max)() / 10); - p2 *= 10; - const std::uint64_t d = p2 >> -one.e; // d = (10 * p2) div 2^-e - const std::uint64_t r = p2 & (one.f - 1); // r = (10 * p2) mod 2^-e - // - // M+ = buffer * 10^-m + 10^-m * (1/10 * (d * 2^-e + r) * 2^e - // = buffer * 10^-m + 10^-m * (1/10 * (d + r * 2^e)) - // = (buffer * 10 + d) * 10^(-m-1) + 10^(-m-1) * r * 2^e - // - JSON_ASSERT(d <= 9); - buffer[length++] = static_cast('0' + d); // buffer := buffer * 10 + d - // - // M+ = buffer * 10^(-m-1) + 10^(-m-1) * r * 2^e - // - p2 = r; - m++; - // - // M+ = buffer * 10^-m + 10^-m * p2 * 2^e - // Invariant restored. - - // Check if enough digits have been generated. - // - // 10^-m * p2 * 2^e <= delta * 2^e - // p2 * 2^e <= 10^m * delta * 2^e - // p2 <= 10^m * delta - delta *= 10; - dist *= 10; - if (p2 <= delta) - { - break; - } - } - - // V = buffer * 10^-m, with M- <= V <= M+. - - decimal_exponent -= m; - - // 1 ulp in the decimal representation is now 10^-m. - // Since delta and dist are now scaled by 10^m, we need to do the - // same with ulp in order to keep the units in sync. - // - // 10^m * 10^-m = 1 = 2^-e * 2^e = ten_m * 2^e - // - const std::uint64_t ten_m = one.f; - grisu2_round(buffer, length, dist, delta, p2, ten_m); - - // By construction this algorithm generates the shortest possible decimal - // number (Loitsch, Theorem 6.2) which rounds back to w. - // For an input number of precision p, at least - // - // N = 1 + ceil(p * log_10(2)) - // - // decimal digits are sufficient to identify all binary floating-point - // numbers (Matula, "In-and-Out conversions"). - // This implies that the algorithm does not produce more than N decimal - // digits. - // - // N = 17 for p = 53 (IEEE double precision) - // N = 9 for p = 24 (IEEE single precision) -} - -/*! -v = buf * 10^decimal_exponent -len is the length of the buffer (number of decimal digits) -The buffer must be large enough, i.e. >= max_digits10. -*/ -JSON_HEDLEY_NON_NULL(1) -inline void grisu2(char* buf, int& len, int& decimal_exponent, - diyfp m_minus, diyfp v, diyfp m_plus) -{ - JSON_ASSERT(m_plus.e == m_minus.e); - JSON_ASSERT(m_plus.e == v.e); - - // --------(-----------------------+-----------------------)-------- (A) - // m- v m+ - // - // --------------------(-----------+-----------------------)-------- (B) - // m- v m+ - // - // First scale v (and m- and m+) such that the exponent is in the range - // [alpha, gamma]. - - const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e); - - const diyfp c_minus_k(cached.f, cached.e); // = c ~= 10^-k - - // The exponent of the products is = v.e + c_minus_k.e + q and is in the range [alpha,gamma] - const diyfp w = diyfp::mul(v, c_minus_k); - const diyfp w_minus = diyfp::mul(m_minus, c_minus_k); - const diyfp w_plus = diyfp::mul(m_plus, c_minus_k); - - // ----(---+---)---------------(---+---)---------------(---+---)---- - // w- w w+ - // = c*m- = c*v = c*m+ - // - // diyfp::mul rounds its result and c_minus_k is approximated too. w, w- and - // w+ are now off by a small amount. - // In fact: - // - // w - v * 10^k < 1 ulp - // - // To account for this inaccuracy, add resp. subtract 1 ulp. - // - // --------+---[---------------(---+---)---------------]---+-------- - // w- M- w M+ w+ - // - // Now any number in [M-, M+] (bounds included) will round to w when input, - // regardless of how the input rounding algorithm breaks ties. - // - // And digit_gen generates the shortest possible such number in [M-, M+]. - // Note that this does not mean that Grisu2 always generates the shortest - // possible number in the interval (m-, m+). - const diyfp M_minus(w_minus.f + 1, w_minus.e); - const diyfp M_plus (w_plus.f - 1, w_plus.e ); - - decimal_exponent = -cached.k; // = -(-k) = k - - grisu2_digit_gen(buf, len, decimal_exponent, M_minus, w, M_plus); -} - -/*! -v = buf * 10^decimal_exponent -len is the length of the buffer (number of decimal digits) -The buffer must be large enough, i.e. >= max_digits10. -*/ -template -JSON_HEDLEY_NON_NULL(1) -void grisu2(char* buf, int& len, int& decimal_exponent, FloatType value) -{ - static_assert(diyfp::kPrecision >= std::numeric_limits::digits + 3, - "internal error: not enough precision"); - - JSON_ASSERT(std::isfinite(value)); - JSON_ASSERT(value > 0); - - // If the neighbors (and boundaries) of 'value' are always computed for double-precision - // numbers, all float's can be recovered using strtod (and strtof). However, the resulting - // decimal representations are not exactly "short". - // - // The documentation for 'std::to_chars' (https://en.cppreference.com/w/cpp/utility/to_chars) - // says "value is converted to a string as if by std::sprintf in the default ("C") locale" - // and since sprintf promotes float's to double's, I think this is exactly what 'std::to_chars' - // does. - // On the other hand, the documentation for 'std::to_chars' requires that "parsing the - // representation using the corresponding std::from_chars function recovers value exactly". That - // indicates that single precision floating-point numbers should be recovered using - // 'std::strtof'. - // - // NB: If the neighbors are computed for single-precision numbers, there is a single float - // (7.0385307e-26f) which can't be recovered using strtod. The resulting double precision - // value is off by 1 ulp. -#if 0 - const boundaries w = compute_boundaries(static_cast(value)); -#else - const boundaries w = compute_boundaries(value); -#endif - - grisu2(buf, len, decimal_exponent, w.minus, w.w, w.plus); -} - -/*! -@brief appends a decimal representation of e to buf -@return a pointer to the element following the exponent. -@pre -1000 < e < 1000 -*/ -JSON_HEDLEY_NON_NULL(1) -JSON_HEDLEY_RETURNS_NON_NULL -inline char* append_exponent(char* buf, int e) -{ - JSON_ASSERT(e > -1000); - JSON_ASSERT(e < 1000); - - if (e < 0) - { - e = -e; - *buf++ = '-'; - } - else - { - *buf++ = '+'; - } - - auto k = static_cast(e); - if (k < 10) - { - // Always print at least two digits in the exponent. - // This is for compatibility with printf("%g"). - *buf++ = '0'; - *buf++ = static_cast('0' + k); - } - else if (k < 100) - { - *buf++ = static_cast('0' + k / 10); - k %= 10; - *buf++ = static_cast('0' + k); - } - else - { - *buf++ = static_cast('0' + k / 100); - k %= 100; - *buf++ = static_cast('0' + k / 10); - k %= 10; - *buf++ = static_cast('0' + k); - } - - return buf; -} - -/*! -@brief prettify v = buf * 10^decimal_exponent - -If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point -notation. Otherwise it will be printed in exponential notation. - -@pre min_exp < 0 -@pre max_exp > 0 -*/ -JSON_HEDLEY_NON_NULL(1) -JSON_HEDLEY_RETURNS_NON_NULL -inline char* format_buffer(char* buf, int len, int decimal_exponent, - int min_exp, int max_exp) -{ - JSON_ASSERT(min_exp < 0); - JSON_ASSERT(max_exp > 0); - - const int k = len; - const int n = len + decimal_exponent; - - // v = buf * 10^(n-k) - // k is the length of the buffer (number of decimal digits) - // n is the position of the decimal point relative to the start of the buffer. - - if (k <= n && n <= max_exp) - { - // digits[000] - // len <= max_exp + 2 - - std::memset(buf + k, '0', static_cast(n) - static_cast(k)); - // Make it look like a floating-point number (#362, #378) - buf[n + 0] = '.'; - buf[n + 1] = '0'; - return buf + (static_cast(n) + 2); - } - - if (0 < n && n <= max_exp) - { - // dig.its - // len <= max_digits10 + 1 - - JSON_ASSERT(k > n); - - std::memmove(buf + (static_cast(n) + 1), buf + n, static_cast(k) - static_cast(n)); - buf[n] = '.'; - return buf + (static_cast(k) + 1U); - } - - if (min_exp < n && n <= 0) - { - // 0.[000]digits - // len <= 2 + (-min_exp - 1) + max_digits10 - - std::memmove(buf + (2 + static_cast(-n)), buf, static_cast(k)); - buf[0] = '0'; - buf[1] = '.'; - std::memset(buf + 2, '0', static_cast(-n)); - return buf + (2U + static_cast(-n) + static_cast(k)); - } - - if (k == 1) - { - // dE+123 - // len <= 1 + 5 - - buf += 1; - } - else - { - // d.igitsE+123 - // len <= max_digits10 + 1 + 5 - - std::memmove(buf + 2, buf + 1, static_cast(k) - 1); - buf[1] = '.'; - buf += 1 + static_cast(k); - } - - *buf++ = 'e'; - return append_exponent(buf, n - 1); -} - -} // namespace dtoa_impl - -/*! -@brief generates a decimal representation of the floating-point number value in [first, last). - -The format of the resulting decimal representation is similar to printf's %g -format. Returns an iterator pointing past-the-end of the decimal representation. - -@note The input number must be finite, i.e. NaN's and Inf's are not supported. -@note The buffer must be large enough. -@note The result is NOT null-terminated. -*/ -template -JSON_HEDLEY_NON_NULL(1, 2) -JSON_HEDLEY_RETURNS_NON_NULL -char* to_chars(char* first, const char* last, FloatType value) -{ - static_cast(last); // maybe unused - fix warning - JSON_ASSERT(std::isfinite(value)); - - // Use signbit(value) instead of (value < 0) since signbit works for -0. - if (std::signbit(value)) - { - value = -value; - *first++ = '-'; - } - -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wfloat-equal" -#endif - if (value == 0) // +-0 - { - *first++ = '0'; - // Make it look like a floating-point number (#362, #378) - *first++ = '.'; - *first++ = '0'; - return first; - } -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif - - JSON_ASSERT(last - first >= std::numeric_limits::max_digits10); - - // Compute v = buffer * 10^decimal_exponent. - // The decimal digits are stored in the buffer, which needs to be interpreted - // as an unsigned decimal integer. - // len is the length of the buffer, i.e. the number of decimal digits. - int len = 0; - int decimal_exponent = 0; - dtoa_impl::grisu2(first, len, decimal_exponent, value); - - JSON_ASSERT(len <= std::numeric_limits::max_digits10); - - // Format the buffer like printf("%.*g", prec, value) - constexpr int kMinExp = -4; - // Use digits10 here to increase compatibility with version 2. - constexpr int kMaxExp = std::numeric_limits::digits10; - - JSON_ASSERT(last - first >= kMaxExp + 2); - JSON_ASSERT(last - first >= 2 + (-kMinExp - 1) + std::numeric_limits::max_digits10); - JSON_ASSERT(last - first >= std::numeric_limits::max_digits10 + 6); - - return dtoa_impl::format_buffer(first, len, decimal_exponent, kMinExp, kMaxExp); -} - -} // namespace detail -} // namespace nlohmann - -// #include - -// #include - -// #include - -// #include - -// #include - -// #include - - -namespace nlohmann -{ -namespace detail -{ -/////////////////// -// serialization // -/////////////////// - -/// how to treat decoding errors -enum class error_handler_t -{ - strict, ///< throw a type_error exception in case of invalid UTF-8 - replace, ///< replace invalid UTF-8 sequences with U+FFFD - ignore ///< ignore invalid UTF-8 sequences -}; - -template -class serializer -{ - using string_t = typename BasicJsonType::string_t; - using number_float_t = typename BasicJsonType::number_float_t; - using number_integer_t = typename BasicJsonType::number_integer_t; - using number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using binary_char_t = typename BasicJsonType::binary_t::value_type; - static constexpr std::uint8_t UTF8_ACCEPT = 0; - static constexpr std::uint8_t UTF8_REJECT = 1; - - public: - /*! - @param[in] s output stream to serialize to - @param[in] ichar indentation character to use - @param[in] error_handler_ how to react on decoding errors - */ - serializer(output_adapter_t s, const char ichar, - error_handler_t error_handler_ = error_handler_t::strict) - : o(std::move(s)) - , loc(std::localeconv()) - , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits::to_char_type(* (loc->thousands_sep))) - , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits::to_char_type(* (loc->decimal_point))) - , indent_char(ichar) - , indent_string(512, indent_char) - , error_handler(error_handler_) - {} - - // delete because of pointer members - serializer(const serializer&) = delete; - serializer& operator=(const serializer&) = delete; - serializer(serializer&&) = delete; - serializer& operator=(serializer&&) = delete; - ~serializer() = default; - - /*! - @brief internal implementation of the serialization function - - This function is called by the public member function dump and organizes - the serialization internally. The indentation level is propagated as - additional parameter. In case of arrays and objects, the function is - called recursively. - - - strings and object keys are escaped using `escape_string()` - - integer numbers are converted implicitly via `operator<<` - - floating-point numbers are converted to a string using `"%g"` format - - binary values are serialized as objects containing the subtype and the - byte array - - @param[in] val value to serialize - @param[in] pretty_print whether the output shall be pretty-printed - @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters - in the output are escaped with `\uXXXX` sequences, and the result consists - of ASCII characters only. - @param[in] indent_step the indent level - @param[in] current_indent the current indent level (only used internally) - */ - void dump(const BasicJsonType& val, - const bool pretty_print, - const bool ensure_ascii, - const unsigned int indent_step, - const unsigned int current_indent = 0) - { - switch (val.m_type) - { - case value_t::object: - { - if (val.m_value.object->empty()) - { - o->write_characters("{}", 2); - return; - } - - if (pretty_print) - { - o->write_characters("{\n", 2); - - // variable to hold indentation for recursive calls - const auto new_indent = current_indent + indent_step; - if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent)) - { - indent_string.resize(indent_string.size() * 2, ' '); - } - - // first n-1 elements - auto i = val.m_value.object->cbegin(); - for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i) - { - o->write_characters(indent_string.c_str(), new_indent); - o->write_character('\"'); - dump_escaped(i->first, ensure_ascii); - o->write_characters("\": ", 3); - dump(i->second, true, ensure_ascii, indent_step, new_indent); - o->write_characters(",\n", 2); - } - - // last element - JSON_ASSERT(i != val.m_value.object->cend()); - JSON_ASSERT(std::next(i) == val.m_value.object->cend()); - o->write_characters(indent_string.c_str(), new_indent); - o->write_character('\"'); - dump_escaped(i->first, ensure_ascii); - o->write_characters("\": ", 3); - dump(i->second, true, ensure_ascii, indent_step, new_indent); - - o->write_character('\n'); - o->write_characters(indent_string.c_str(), current_indent); - o->write_character('}'); - } - else - { - o->write_character('{'); - - // first n-1 elements - auto i = val.m_value.object->cbegin(); - for (std::size_t cnt = 0; cnt < val.m_value.object->size() - 1; ++cnt, ++i) - { - o->write_character('\"'); - dump_escaped(i->first, ensure_ascii); - o->write_characters("\":", 2); - dump(i->second, false, ensure_ascii, indent_step, current_indent); - o->write_character(','); - } - - // last element - JSON_ASSERT(i != val.m_value.object->cend()); - JSON_ASSERT(std::next(i) == val.m_value.object->cend()); - o->write_character('\"'); - dump_escaped(i->first, ensure_ascii); - o->write_characters("\":", 2); - dump(i->second, false, ensure_ascii, indent_step, current_indent); - - o->write_character('}'); - } - - return; - } - - case value_t::array: - { - if (val.m_value.array->empty()) - { - o->write_characters("[]", 2); - return; - } - - if (pretty_print) - { - o->write_characters("[\n", 2); - - // variable to hold indentation for recursive calls - const auto new_indent = current_indent + indent_step; - if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent)) - { - indent_string.resize(indent_string.size() * 2, ' '); - } - - // first n-1 elements - for (auto i = val.m_value.array->cbegin(); - i != val.m_value.array->cend() - 1; ++i) - { - o->write_characters(indent_string.c_str(), new_indent); - dump(*i, true, ensure_ascii, indent_step, new_indent); - o->write_characters(",\n", 2); - } - - // last element - JSON_ASSERT(!val.m_value.array->empty()); - o->write_characters(indent_string.c_str(), new_indent); - dump(val.m_value.array->back(), true, ensure_ascii, indent_step, new_indent); - - o->write_character('\n'); - o->write_characters(indent_string.c_str(), current_indent); - o->write_character(']'); - } - else - { - o->write_character('['); - - // first n-1 elements - for (auto i = val.m_value.array->cbegin(); - i != val.m_value.array->cend() - 1; ++i) - { - dump(*i, false, ensure_ascii, indent_step, current_indent); - o->write_character(','); - } - - // last element - JSON_ASSERT(!val.m_value.array->empty()); - dump(val.m_value.array->back(), false, ensure_ascii, indent_step, current_indent); - - o->write_character(']'); - } - - return; - } - - case value_t::string: - { - o->write_character('\"'); - dump_escaped(*val.m_value.string, ensure_ascii); - o->write_character('\"'); - return; - } - - case value_t::binary: - { - if (pretty_print) - { - o->write_characters("{\n", 2); - - // variable to hold indentation for recursive calls - const auto new_indent = current_indent + indent_step; - if (JSON_HEDLEY_UNLIKELY(indent_string.size() < new_indent)) - { - indent_string.resize(indent_string.size() * 2, ' '); - } - - o->write_characters(indent_string.c_str(), new_indent); - - o->write_characters("\"bytes\": [", 10); - - if (!val.m_value.binary->empty()) - { - for (auto i = val.m_value.binary->cbegin(); - i != val.m_value.binary->cend() - 1; ++i) - { - dump_integer(*i); - o->write_characters(", ", 2); - } - dump_integer(val.m_value.binary->back()); - } - - o->write_characters("],\n", 3); - o->write_characters(indent_string.c_str(), new_indent); - - o->write_characters("\"subtype\": ", 11); - if (val.m_value.binary->has_subtype()) - { - dump_integer(val.m_value.binary->subtype()); - } - else - { - o->write_characters("null", 4); - } - o->write_character('\n'); - o->write_characters(indent_string.c_str(), current_indent); - o->write_character('}'); - } - else - { - o->write_characters("{\"bytes\":[", 10); - - if (!val.m_value.binary->empty()) - { - for (auto i = val.m_value.binary->cbegin(); - i != val.m_value.binary->cend() - 1; ++i) - { - dump_integer(*i); - o->write_character(','); - } - dump_integer(val.m_value.binary->back()); - } - - o->write_characters("],\"subtype\":", 12); - if (val.m_value.binary->has_subtype()) - { - dump_integer(val.m_value.binary->subtype()); - o->write_character('}'); - } - else - { - o->write_characters("null}", 5); - } - } - return; - } - - case value_t::boolean: - { - if (val.m_value.boolean) - { - o->write_characters("true", 4); - } - else - { - o->write_characters("false", 5); - } - return; - } - - case value_t::number_integer: - { - dump_integer(val.m_value.number_integer); - return; - } - - case value_t::number_unsigned: - { - dump_integer(val.m_value.number_unsigned); - return; - } - - case value_t::number_float: - { - dump_float(val.m_value.number_float); - return; - } - - case value_t::discarded: - { - o->write_characters("", 11); - return; - } - - case value_t::null: - { - o->write_characters("null", 4); - return; - } - - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - } - } - - JSON_PRIVATE_UNLESS_TESTED: - /*! - @brief dump escaped string - - Escape a string by replacing certain special characters by a sequence of an - escape character (backslash) and another character and other control - characters by a sequence of "\u" followed by a four-digit hex - representation. The escaped string is written to output stream @a o. - - @param[in] s the string to escape - @param[in] ensure_ascii whether to escape non-ASCII characters with - \uXXXX sequences - - @complexity Linear in the length of string @a s. - */ - void dump_escaped(const string_t& s, const bool ensure_ascii) - { - std::uint32_t codepoint{}; - std::uint8_t state = UTF8_ACCEPT; - std::size_t bytes = 0; // number of bytes written to string_buffer - - // number of bytes written at the point of the last valid byte - std::size_t bytes_after_last_accept = 0; - std::size_t undumped_chars = 0; - - for (std::size_t i = 0; i < s.size(); ++i) - { - const auto byte = static_cast(s[i]); - - switch (decode(state, codepoint, byte)) - { - case UTF8_ACCEPT: // decode found a new code point - { - switch (codepoint) - { - case 0x08: // backspace - { - string_buffer[bytes++] = '\\'; - string_buffer[bytes++] = 'b'; - break; - } - - case 0x09: // horizontal tab - { - string_buffer[bytes++] = '\\'; - string_buffer[bytes++] = 't'; - break; - } - - case 0x0A: // newline - { - string_buffer[bytes++] = '\\'; - string_buffer[bytes++] = 'n'; - break; - } - - case 0x0C: // formfeed - { - string_buffer[bytes++] = '\\'; - string_buffer[bytes++] = 'f'; - break; - } - - case 0x0D: // carriage return - { - string_buffer[bytes++] = '\\'; - string_buffer[bytes++] = 'r'; - break; - } - - case 0x22: // quotation mark - { - string_buffer[bytes++] = '\\'; - string_buffer[bytes++] = '\"'; - break; - } - - case 0x5C: // reverse solidus - { - string_buffer[bytes++] = '\\'; - string_buffer[bytes++] = '\\'; - break; - } - - default: - { - // escape control characters (0x00..0x1F) or, if - // ensure_ascii parameter is used, non-ASCII characters - if ((codepoint <= 0x1F) || (ensure_ascii && (codepoint >= 0x7F))) - { - if (codepoint <= 0xFFFF) - { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - (std::snprintf)(string_buffer.data() + bytes, 7, "\\u%04x", - static_cast(codepoint)); - bytes += 6; - } - else - { - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - (std::snprintf)(string_buffer.data() + bytes, 13, "\\u%04x\\u%04x", - static_cast(0xD7C0u + (codepoint >> 10u)), - static_cast(0xDC00u + (codepoint & 0x3FFu))); - bytes += 12; - } - } - else - { - // copy byte to buffer (all previous bytes - // been copied have in default case above) - string_buffer[bytes++] = s[i]; - } - break; - } - } - - // write buffer and reset index; there must be 13 bytes - // left, as this is the maximal number of bytes to be - // written ("\uxxxx\uxxxx\0") for one code point - if (string_buffer.size() - bytes < 13) - { - o->write_characters(string_buffer.data(), bytes); - bytes = 0; - } - - // remember the byte position of this accept - bytes_after_last_accept = bytes; - undumped_chars = 0; - break; - } - - case UTF8_REJECT: // decode found invalid UTF-8 byte - { - switch (error_handler) - { - case error_handler_t::strict: - { - std::string sn(9, '\0'); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - (std::snprintf)(&sn[0], sn.size(), "%.2X", byte); - JSON_THROW(type_error::create(316, "invalid UTF-8 byte at index " + std::to_string(i) + ": 0x" + sn, BasicJsonType())); - } - - case error_handler_t::ignore: - case error_handler_t::replace: - { - // in case we saw this character the first time, we - // would like to read it again, because the byte - // may be OK for itself, but just not OK for the - // previous sequence - if (undumped_chars > 0) - { - --i; - } - - // reset length buffer to the last accepted index; - // thus removing/ignoring the invalid characters - bytes = bytes_after_last_accept; - - if (error_handler == error_handler_t::replace) - { - // add a replacement character - if (ensure_ascii) - { - string_buffer[bytes++] = '\\'; - string_buffer[bytes++] = 'u'; - string_buffer[bytes++] = 'f'; - string_buffer[bytes++] = 'f'; - string_buffer[bytes++] = 'f'; - string_buffer[bytes++] = 'd'; - } - else - { - string_buffer[bytes++] = detail::binary_writer::to_char_type('\xEF'); - string_buffer[bytes++] = detail::binary_writer::to_char_type('\xBF'); - string_buffer[bytes++] = detail::binary_writer::to_char_type('\xBD'); - } - - // write buffer and reset index; there must be 13 bytes - // left, as this is the maximal number of bytes to be - // written ("\uxxxx\uxxxx\0") for one code point - if (string_buffer.size() - bytes < 13) - { - o->write_characters(string_buffer.data(), bytes); - bytes = 0; - } - - bytes_after_last_accept = bytes; - } - - undumped_chars = 0; - - // continue processing the string - state = UTF8_ACCEPT; - break; - } - - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - } - break; - } - - default: // decode found yet incomplete multi-byte code point - { - if (!ensure_ascii) - { - // code point will not be escaped - copy byte to buffer - string_buffer[bytes++] = s[i]; - } - ++undumped_chars; - break; - } - } - } - - // we finished processing the string - if (JSON_HEDLEY_LIKELY(state == UTF8_ACCEPT)) - { - // write buffer - if (bytes > 0) - { - o->write_characters(string_buffer.data(), bytes); - } - } - else - { - // we finish reading, but do not accept: string was incomplete - switch (error_handler) - { - case error_handler_t::strict: - { - std::string sn(9, '\0'); - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - (std::snprintf)(&sn[0], sn.size(), "%.2X", static_cast(s.back())); - JSON_THROW(type_error::create(316, "incomplete UTF-8 string; last byte: 0x" + sn, BasicJsonType())); - } - - case error_handler_t::ignore: - { - // write all accepted bytes - o->write_characters(string_buffer.data(), bytes_after_last_accept); - break; - } - - case error_handler_t::replace: - { - // write all accepted bytes - o->write_characters(string_buffer.data(), bytes_after_last_accept); - // add a replacement character - if (ensure_ascii) - { - o->write_characters("\\ufffd", 6); - } - else - { - o->write_characters("\xEF\xBF\xBD", 3); - } - break; - } - - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - } - } - } - - private: - /*! - @brief count digits - - Count the number of decimal (base 10) digits for an input unsigned integer. - - @param[in] x unsigned integer number to count its digits - @return number of decimal digits - */ - inline unsigned int count_digits(number_unsigned_t x) noexcept - { - unsigned int n_digits = 1; - for (;;) - { - if (x < 10) - { - return n_digits; - } - if (x < 100) - { - return n_digits + 1; - } - if (x < 1000) - { - return n_digits + 2; - } - if (x < 10000) - { - return n_digits + 3; - } - x = x / 10000u; - n_digits += 4; - } - } - - /*! - @brief dump an integer - - Dump a given integer to output stream @a o. Works internally with - @a number_buffer. - - @param[in] x integer number (signed or unsigned) to dump - @tparam NumberType either @a number_integer_t or @a number_unsigned_t - */ - template < typename NumberType, detail::enable_if_t < - std::is_integral::value || - std::is_same::value || - std::is_same::value || - std::is_same::value, - int > = 0 > - void dump_integer(NumberType x) - { - static constexpr std::array, 100> digits_to_99 - { - { - {{'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'}}, - } - }; - - // special case for "0" - if (x == 0) - { - o->write_character('0'); - return; - } - - // use a pointer to fill the buffer - auto buffer_ptr = number_buffer.begin(); // NOLINT(llvm-qualified-auto,readability-qualified-auto,cppcoreguidelines-pro-type-vararg,hicpp-vararg) - - const bool is_negative = std::is_signed::value && !(x >= 0); // see issue #755 - number_unsigned_t abs_value; - - unsigned int n_chars{}; - - if (is_negative) - { - *buffer_ptr = '-'; - abs_value = remove_sign(static_cast(x)); - - // account one more byte for the minus sign - n_chars = 1 + count_digits(abs_value); - } - else - { - abs_value = static_cast(x); - n_chars = count_digits(abs_value); - } - - // spare 1 byte for '\0' - JSON_ASSERT(n_chars < number_buffer.size() - 1); - - // jump to the end to generate the string from backward - // so we later avoid reversing the result - buffer_ptr += n_chars; - - // Fast int2ascii implementation inspired by "Fastware" talk by Andrei Alexandrescu - // See: https://www.youtube.com/watch?v=o4-CwDo2zpg - while (abs_value >= 100) - { - const auto digits_index = static_cast((abs_value % 100)); - abs_value /= 100; - *(--buffer_ptr) = digits_to_99[digits_index][1]; - *(--buffer_ptr) = digits_to_99[digits_index][0]; - } - - if (abs_value >= 10) - { - const auto digits_index = static_cast(abs_value); - *(--buffer_ptr) = digits_to_99[digits_index][1]; - *(--buffer_ptr) = digits_to_99[digits_index][0]; - } - else - { - *(--buffer_ptr) = static_cast('0' + abs_value); - } - - o->write_characters(number_buffer.data(), n_chars); - } - - /*! - @brief dump a floating-point number - - Dump a given floating-point number to output stream @a o. Works internally - with @a number_buffer. - - @param[in] x floating-point number to dump - */ - void dump_float(number_float_t x) - { - // NaN / inf - if (!std::isfinite(x)) - { - o->write_characters("null", 4); - return; - } - - // If number_float_t is an IEEE-754 single or double precision number, - // use the Grisu2 algorithm to produce short numbers which are - // guaranteed to round-trip, using strtof and strtod, resp. - // - // NB: The test below works if == . - static constexpr bool is_ieee_single_or_double - = (std::numeric_limits::is_iec559 && std::numeric_limits::digits == 24 && std::numeric_limits::max_exponent == 128) || - (std::numeric_limits::is_iec559 && std::numeric_limits::digits == 53 && std::numeric_limits::max_exponent == 1024); - - dump_float(x, std::integral_constant()); - } - - void dump_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/) - { - auto* begin = number_buffer.data(); - auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x); - - o->write_characters(begin, static_cast(end - begin)); - } - - void dump_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/) - { - // get number of digits for a float -> text -> float round-trip - static constexpr auto d = std::numeric_limits::max_digits10; - - // the actual conversion - // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x); - - // negative value indicates an error - JSON_ASSERT(len > 0); - // check if buffer was large enough - JSON_ASSERT(static_cast(len) < number_buffer.size()); - - // erase thousands separator - if (thousands_sep != '\0') - { - // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::remove returns an iterator, see https://github.com/nlohmann/json/issues/3081 - const auto end = std::remove(number_buffer.begin(), number_buffer.begin() + len, thousands_sep); - std::fill(end, number_buffer.end(), '\0'); - JSON_ASSERT((end - number_buffer.begin()) <= len); - len = (end - number_buffer.begin()); - } - - // convert decimal point to '.' - if (decimal_point != '\0' && decimal_point != '.') - { - // NOLINTNEXTLINE(readability-qualified-auto,llvm-qualified-auto): std::find returns an iterator, see https://github.com/nlohmann/json/issues/3081 - const auto dec_pos = std::find(number_buffer.begin(), number_buffer.end(), decimal_point); - if (dec_pos != number_buffer.end()) - { - *dec_pos = '.'; - } - } - - o->write_characters(number_buffer.data(), static_cast(len)); - - // determine if need to append ".0" - const bool value_is_int_like = - std::none_of(number_buffer.begin(), number_buffer.begin() + len + 1, - [](char c) - { - return c == '.' || c == 'e'; - }); - - if (value_is_int_like) - { - o->write_characters(".0", 2); - } - } - - /*! - @brief check whether a string is UTF-8 encoded - - The function checks each byte of a string whether it is UTF-8 encoded. The - result of the check is stored in the @a state parameter. The function must - be called initially with state 0 (accept). State 1 means the string must - be rejected, because the current byte is not allowed. If the string is - completely processed, but the state is non-zero, the string ended - prematurely; that is, the last byte indicated more bytes should have - followed. - - @param[in,out] state the state of the decoding - @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT) - @param[in] byte next byte to decode - @return new state - - @note The function has been edited: a std::array is used. - - @copyright Copyright (c) 2008-2009 Bjoern Hoehrmann - @sa http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ - */ - static std::uint8_t decode(std::uint8_t& state, std::uint32_t& codep, const std::uint8_t byte) noexcept - { - static const std::array utf8d = - { - { - 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, // 00..1F - 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, // 20..3F - 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, // 40..5F - 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, // 60..7F - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9F - 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // A0..BF - 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, // C0..DF - 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF - 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF - 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2 - 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4 - 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6 - 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // s7..s8 - } - }; - - JSON_ASSERT(byte < utf8d.size()); - const std::uint8_t type = utf8d[byte]; - - codep = (state != UTF8_ACCEPT) - ? (byte & 0x3fu) | (codep << 6u) - : (0xFFu >> type) & (byte); - - std::size_t index = 256u + static_cast(state) * 16u + static_cast(type); - JSON_ASSERT(index < 400); - state = utf8d[index]; - return state; - } - - /* - * Overload to make the compiler happy while it is instantiating - * dump_integer for number_unsigned_t. - * Must never be called. - */ - number_unsigned_t remove_sign(number_unsigned_t x) - { - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - return x; // LCOV_EXCL_LINE - } - - /* - * Helper function for dump_integer - * - * This function takes a negative signed integer and returns its absolute - * value as unsigned integer. The plus/minus shuffling is necessary as we can - * not directly remove the sign of an arbitrary signed integer as the - * absolute values of INT_MIN and INT_MAX are usually not the same. See - * #1708 for details. - */ - inline number_unsigned_t remove_sign(number_integer_t x) noexcept - { - JSON_ASSERT(x < 0 && x < (std::numeric_limits::max)()); // NOLINT(misc-redundant-expression) - return static_cast(-(x + 1)) + 1; - } - - private: - /// the output of the serializer - output_adapter_t o = nullptr; - - /// a (hopefully) large enough character buffer - std::array number_buffer{{}}; - - /// the locale - const std::lconv* loc = nullptr; - /// the locale's thousand separator character - const char thousands_sep = '\0'; - /// the locale's decimal point character - const char decimal_point = '\0'; - - /// string buffer - std::array string_buffer{{}}; - - /// the indentation character - const char indent_char; - /// the indentation string - string_t indent_string; - - /// error_handler how to react on decoding errors - const error_handler_t error_handler; -}; -} // namespace detail -} // namespace nlohmann - -// #include - -// #include - -// #include - - -#include // less -#include // initializer_list -#include // input_iterator_tag, iterator_traits -#include // allocator -#include // for out_of_range -#include // enable_if, is_convertible -#include // pair -#include // vector - -// #include - - -namespace nlohmann -{ - -/// ordered_map: a minimal map-like container that preserves insertion order -/// for use within nlohmann::basic_json -template , - class Allocator = std::allocator>> - struct ordered_map : std::vector, Allocator> -{ - using key_type = Key; - using mapped_type = T; - using Container = std::vector, Allocator>; - using typename Container::iterator; - using typename Container::const_iterator; - using typename Container::size_type; - using typename Container::value_type; - - // Explicit constructors instead of `using Container::Container` - // otherwise older compilers choke on it (GCC <= 5.5, xcode <= 9.4) - ordered_map(const Allocator& alloc = Allocator()) : Container{alloc} {} - template - ordered_map(It first, It last, const Allocator& alloc = Allocator()) - : Container{first, last, alloc} {} - ordered_map(std::initializer_list init, const Allocator& alloc = Allocator() ) - : Container{init, alloc} {} - - std::pair emplace(const key_type& key, T&& t) - { - for (auto it = this->begin(); it != this->end(); ++it) - { - if (it->first == key) - { - return {it, false}; - } - } - Container::emplace_back(key, t); - return {--this->end(), true}; - } - - T& operator[](const Key& key) - { - return emplace(key, T{}).first->second; - } - - const T& operator[](const Key& key) const - { - return at(key); - } - - T& at(const Key& key) - { - for (auto it = this->begin(); it != this->end(); ++it) - { - if (it->first == key) - { - return it->second; - } - } - - JSON_THROW(std::out_of_range("key not found")); - } - - const T& at(const Key& key) const - { - for (auto it = this->begin(); it != this->end(); ++it) - { - if (it->first == key) - { - return it->second; - } - } - - JSON_THROW(std::out_of_range("key not found")); - } - - size_type erase(const Key& key) - { - for (auto it = this->begin(); it != this->end(); ++it) - { - if (it->first == key) - { - // Since we cannot move const Keys, re-construct them in place - for (auto next = it; ++next != this->end(); ++it) - { - it->~value_type(); // Destroy but keep allocation - new (&*it) value_type{std::move(*next)}; - } - Container::pop_back(); - return 1; - } - } - return 0; - } - - iterator erase(iterator pos) - { - auto it = pos; - - // Since we cannot move const Keys, re-construct them in place - for (auto next = it; ++next != this->end(); ++it) - { - it->~value_type(); // Destroy but keep allocation - new (&*it) value_type{std::move(*next)}; - } - Container::pop_back(); - return pos; - } - - size_type count(const Key& key) const - { - for (auto it = this->begin(); it != this->end(); ++it) - { - if (it->first == key) - { - return 1; - } - } - return 0; - } - - iterator find(const Key& key) - { - for (auto it = this->begin(); it != this->end(); ++it) - { - if (it->first == key) - { - return it; - } - } - return Container::end(); - } - - const_iterator find(const Key& key) const - { - for (auto it = this->begin(); it != this->end(); ++it) - { - if (it->first == key) - { - return it; - } - } - return Container::end(); - } - - std::pair insert( value_type&& value ) - { - return emplace(value.first, std::move(value.second)); - } - - std::pair insert( const value_type& value ) - { - for (auto it = this->begin(); it != this->end(); ++it) - { - if (it->first == value.first) - { - return {it, false}; - } - } - Container::push_back(value); - return {--this->end(), true}; - } - - template - using require_input_iter = typename std::enable_if::iterator_category, - std::input_iterator_tag>::value>::type; - - template> - void insert(InputIt first, InputIt last) - { - for (auto it = first; it != last; ++it) - { - insert(*it); - } - } -}; - -} // namespace nlohmann - - -#if defined(JSON_HAS_CPP_17) - #include -#endif - -/*! -@brief namespace for Niels Lohmann -@see https://github.com/nlohmann -@since version 1.0.0 -*/ -namespace nlohmann -{ - -/*! -@brief a class to store JSON values - -@tparam ObjectType type for JSON objects (`std::map` by default; will be used -in @ref object_t) -@tparam ArrayType type for JSON arrays (`std::vector` by default; will be used -in @ref array_t) -@tparam StringType type for JSON strings and object keys (`std::string` by -default; will be used in @ref string_t) -@tparam BooleanType type for JSON booleans (`bool` by default; will be used -in @ref boolean_t) -@tparam NumberIntegerType type for JSON integer numbers (`int64_t` by -default; will be used in @ref number_integer_t) -@tparam NumberUnsignedType type for JSON unsigned integer numbers (@c -`uint64_t` by default; will be used in @ref number_unsigned_t) -@tparam NumberFloatType type for JSON floating-point numbers (`double` by -default; will be used in @ref number_float_t) -@tparam BinaryType type for packed binary data for compatibility with binary -serialization formats (`std::vector` by default; will be used in -@ref binary_t) -@tparam AllocatorType type of the allocator to use (`std::allocator` by -default) -@tparam JSONSerializer the serializer to resolve internal calls to `to_json()` -and `from_json()` (@ref adl_serializer by default) - -@requirement The class satisfies the following concept requirements: -- Basic - - [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible): - JSON values can be default constructed. The result will be a JSON null - value. - - [MoveConstructible](https://en.cppreference.com/w/cpp/named_req/MoveConstructible): - A JSON value can be constructed from an rvalue argument. - - [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible): - A JSON value can be copy-constructed from an lvalue expression. - - [MoveAssignable](https://en.cppreference.com/w/cpp/named_req/MoveAssignable): - A JSON value van be assigned from an rvalue argument. - - [CopyAssignable](https://en.cppreference.com/w/cpp/named_req/CopyAssignable): - A JSON value can be copy-assigned from an lvalue expression. - - [Destructible](https://en.cppreference.com/w/cpp/named_req/Destructible): - JSON values can be destructed. -- Layout - - [StandardLayoutType](https://en.cppreference.com/w/cpp/named_req/StandardLayoutType): - JSON values have - [standard layout](https://en.cppreference.com/w/cpp/language/data_members#Standard_layout): - All non-static data members are private and standard layout types, the - class has no virtual functions or (virtual) base classes. -- Library-wide - - [EqualityComparable](https://en.cppreference.com/w/cpp/named_req/EqualityComparable): - JSON values can be compared with `==`, see @ref - operator==(const_reference,const_reference). - - [LessThanComparable](https://en.cppreference.com/w/cpp/named_req/LessThanComparable): - JSON values can be compared with `<`, see @ref - operator<(const_reference,const_reference). - - [Swappable](https://en.cppreference.com/w/cpp/named_req/Swappable): - Any JSON lvalue or rvalue of can be swapped with any lvalue or rvalue of - other compatible types, using unqualified function call @ref swap(). - - [NullablePointer](https://en.cppreference.com/w/cpp/named_req/NullablePointer): - JSON values can be compared against `std::nullptr_t` objects which are used - to model the `null` value. -- Container - - [Container](https://en.cppreference.com/w/cpp/named_req/Container): - JSON values can be used like STL containers and provide iterator access. - - [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer); - JSON values can be used like STL containers and provide reverse iterator - access. - -@invariant The member variables @a m_value and @a m_type have the following -relationship: -- If `m_type == value_t::object`, then `m_value.object != nullptr`. -- If `m_type == value_t::array`, then `m_value.array != nullptr`. -- If `m_type == value_t::string`, then `m_value.string != nullptr`. -The invariants are checked by member function assert_invariant(). - -@internal -@note ObjectType trick from https://stackoverflow.com/a/9860911 -@endinternal - -@see [RFC 8259: The JavaScript Object Notation (JSON) Data Interchange -Format](https://tools.ietf.org/html/rfc8259) - -@since version 1.0.0 - -@nosubgrouping -*/ -NLOHMANN_BASIC_JSON_TPL_DECLARATION -class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-special-member-functions) -{ - private: - template friend struct detail::external_constructor; - friend ::nlohmann::json_pointer; - - template - friend class ::nlohmann::detail::parser; - friend ::nlohmann::detail::serializer; - template - friend class ::nlohmann::detail::iter_impl; - template - friend class ::nlohmann::detail::binary_writer; - template - friend class ::nlohmann::detail::binary_reader; - template - friend class ::nlohmann::detail::json_sax_dom_parser; - template - friend class ::nlohmann::detail::json_sax_dom_callback_parser; - friend class ::nlohmann::detail::exception; - - /// workaround type for MSVC - using basic_json_t = NLOHMANN_BASIC_JSON_TPL; - - JSON_PRIVATE_UNLESS_TESTED: - // convenience aliases for types residing in namespace detail; - using lexer = ::nlohmann::detail::lexer_base; - - template - static ::nlohmann::detail::parser parser( - InputAdapterType adapter, - detail::parser_callback_tcb = nullptr, - const bool allow_exceptions = true, - const bool ignore_comments = false - ) - { - return ::nlohmann::detail::parser(std::move(adapter), - std::move(cb), allow_exceptions, ignore_comments); - } - - private: - using primitive_iterator_t = ::nlohmann::detail::primitive_iterator_t; - template - using internal_iterator = ::nlohmann::detail::internal_iterator; - template - using iter_impl = ::nlohmann::detail::iter_impl; - template - using iteration_proxy = ::nlohmann::detail::iteration_proxy; - template using json_reverse_iterator = ::nlohmann::detail::json_reverse_iterator; - - template - using output_adapter_t = ::nlohmann::detail::output_adapter_t; - - template - using binary_reader = ::nlohmann::detail::binary_reader; - template using binary_writer = ::nlohmann::detail::binary_writer; - - JSON_PRIVATE_UNLESS_TESTED: - using serializer = ::nlohmann::detail::serializer; - - public: - using value_t = detail::value_t; - /// JSON Pointer, see @ref nlohmann::json_pointer - using json_pointer = ::nlohmann::json_pointer; - template - using json_serializer = JSONSerializer; - /// how to treat decoding errors - using error_handler_t = detail::error_handler_t; - /// how to treat CBOR tags - using cbor_tag_handler_t = detail::cbor_tag_handler_t; - /// helper type for initializer lists of basic_json values - using initializer_list_t = std::initializer_list>; - - using input_format_t = detail::input_format_t; - /// SAX interface type, see @ref nlohmann::json_sax - using json_sax_t = json_sax; - - //////////////// - // exceptions // - //////////////// - - /// @name exceptions - /// Classes to implement user-defined exceptions. - /// @{ - - /// @copydoc detail::exception - using exception = detail::exception; - /// @copydoc detail::parse_error - using parse_error = detail::parse_error; - /// @copydoc detail::invalid_iterator - using invalid_iterator = detail::invalid_iterator; - /// @copydoc detail::type_error - using type_error = detail::type_error; - /// @copydoc detail::out_of_range - using out_of_range = detail::out_of_range; - /// @copydoc detail::other_error - using other_error = detail::other_error; - - /// @} - - - ///////////////////// - // container types // - ///////////////////// - - /// @name container types - /// The canonic container types to use @ref basic_json like any other STL - /// container. - /// @{ - - /// the type of elements in a basic_json container - using value_type = basic_json; - - /// the type of an element reference - using reference = value_type&; - /// the type of an element const reference - using const_reference = const value_type&; - - /// a type to represent differences between iterators - using difference_type = std::ptrdiff_t; - /// a type to represent container sizes - using size_type = std::size_t; - - /// the allocator type - using allocator_type = AllocatorType; - - /// the type of an element pointer - using pointer = typename std::allocator_traits::pointer; - /// the type of an element const pointer - using const_pointer = typename std::allocator_traits::const_pointer; - - /// an iterator for a basic_json container - using iterator = iter_impl; - /// a const iterator for a basic_json container - using const_iterator = iter_impl; - /// a reverse iterator for a basic_json container - using reverse_iterator = json_reverse_iterator; - /// a const reverse iterator for a basic_json container - using const_reverse_iterator = json_reverse_iterator; - - /// @} - - - /*! - @brief returns the allocator associated with the container - */ - static allocator_type get_allocator() - { - return allocator_type(); - } - - /*! - @brief returns version information on the library - - This function returns a JSON object with information about the library, - including the version number and information on the platform and compiler. - - @return JSON object holding version information - key | description - ----------- | --------------- - `compiler` | Information on the used compiler. It is an object with the following keys: `c++` (the used C++ standard), `family` (the compiler family; possible values are `clang`, `icc`, `gcc`, `ilecpp`, `msvc`, `pgcpp`, `sunpro`, and `unknown`), and `version` (the compiler version). - `copyright` | The copyright line for the library as string. - `name` | The name of the library as string. - `platform` | The used platform as string. Possible values are `win32`, `linux`, `apple`, `unix`, and `unknown`. - `url` | The URL of the project as string. - `version` | The version of the library. It is an object with the following keys: `major`, `minor`, and `patch` as defined by [Semantic Versioning](http://semver.org), and `string` (the version string). - - @liveexample{The following code shows an example output of the `meta()` - function.,meta} - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes to any JSON value. - - @complexity Constant. - - @since 2.1.0 - */ - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json meta() - { - basic_json result; - - result["copyright"] = "(C) 2013-2021 Niels Lohmann"; - result["name"] = "JSON for Modern C++"; - result["url"] = "https://github.com/nlohmann/json"; - result["version"]["string"] = - std::to_string(NLOHMANN_JSON_VERSION_MAJOR) + "." + - std::to_string(NLOHMANN_JSON_VERSION_MINOR) + "." + - std::to_string(NLOHMANN_JSON_VERSION_PATCH); - result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR; - result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR; - result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH; - -#ifdef _WIN32 - result["platform"] = "win32"; -#elif defined __linux__ - result["platform"] = "linux"; -#elif defined __APPLE__ - result["platform"] = "apple"; -#elif defined __unix__ - result["platform"] = "unix"; -#else - result["platform"] = "unknown"; -#endif - -#if defined(__ICC) || defined(__INTEL_COMPILER) - result["compiler"] = {{"family", "icc"}, {"version", __INTEL_COMPILER}}; -#elif defined(__clang__) - result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}}; -#elif defined(__GNUC__) || defined(__GNUG__) - result["compiler"] = {{"family", "gcc"}, {"version", std::to_string(__GNUC__) + "." + std::to_string(__GNUC_MINOR__) + "." + std::to_string(__GNUC_PATCHLEVEL__)}}; -#elif defined(__HP_cc) || defined(__HP_aCC) - result["compiler"] = "hp" -#elif defined(__IBMCPP__) - result["compiler"] = {{"family", "ilecpp"}, {"version", __IBMCPP__}}; -#elif defined(_MSC_VER) - result["compiler"] = {{"family", "msvc"}, {"version", _MSC_VER}}; -#elif defined(__PGI) - result["compiler"] = {{"family", "pgcpp"}, {"version", __PGI}}; -#elif defined(__SUNPRO_CC) - result["compiler"] = {{"family", "sunpro"}, {"version", __SUNPRO_CC}}; -#else - result["compiler"] = {{"family", "unknown"}, {"version", "unknown"}}; -#endif - -#ifdef __cplusplus - result["compiler"]["c++"] = std::to_string(__cplusplus); -#else - result["compiler"]["c++"] = "unknown"; -#endif - return result; - } - - - /////////////////////////// - // JSON value data types // - /////////////////////////// - - /// @name JSON value data types - /// The data types to store a JSON value. These types are derived from - /// the template arguments passed to class @ref basic_json. - /// @{ - -#if defined(JSON_HAS_CPP_14) - // Use transparent comparator if possible, combined with perfect forwarding - // on find() and count() calls prevents unnecessary string construction. - using object_comparator_t = std::less<>; -#else - using object_comparator_t = std::less; -#endif - - /*! - @brief a type for an object - - [RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON objects as follows: - > An object is an unordered collection of zero or more name/value pairs, - > where a name is a string and a value is a string, number, boolean, null, - > object, or array. - - To store objects in C++, a type is defined by the template parameters - described below. - - @tparam ObjectType the container to store objects (e.g., `std::map` or - `std::unordered_map`) - @tparam StringType the type of the keys or names (e.g., `std::string`). - The comparison function `std::less` is used to order elements - inside the container. - @tparam AllocatorType the allocator to use for objects (e.g., - `std::allocator`) - - #### Default type - - With the default values for @a ObjectType (`std::map`), @a StringType - (`std::string`), and @a AllocatorType (`std::allocator`), the default - value for @a object_t is: - - @code {.cpp} - std::map< - std::string, // key_type - basic_json, // value_type - std::less, // key_compare - std::allocator> // allocator_type - > - @endcode - - #### Behavior - - The choice of @a object_t influences the behavior of the JSON class. With - the default type, objects have the following behavior: - - - When all names are unique, objects will be interoperable in the sense - that all software implementations receiving that object will agree on - the name-value mappings. - - When the names within an object are not unique, it is unspecified which - one of the values for a given key will be chosen. For instance, - `{"key": 2, "key": 1}` could be equal to either `{"key": 1}` or - `{"key": 2}`. - - Internally, name/value pairs are stored in lexicographical order of the - names. Objects will also be serialized (see @ref dump) in this order. - For instance, `{"b": 1, "a": 2}` and `{"a": 2, "b": 1}` will be stored - and serialized as `{"a": 2, "b": 1}`. - - When comparing objects, the order of the name/value pairs is irrelevant. - This makes objects interoperable in the sense that they will not be - affected by these differences. For instance, `{"b": 1, "a": 2}` and - `{"a": 2, "b": 1}` will be treated as equal. - - #### Limits - - [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: - > An implementation may set limits on the maximum depth of nesting. - - In this class, the object's limit of nesting is not explicitly constrained. - However, a maximum depth of nesting may be introduced by the compiler or - runtime environment. A theoretical limit can be queried by calling the - @ref max_size function of a JSON object. - - #### Storage - - Objects are stored as pointers in a @ref basic_json type. That is, for any - access to object values, a pointer of type `object_t*` must be - dereferenced. - - @sa see @ref array_t -- type for an array value - - @since version 1.0.0 - - @note The order name/value pairs are added to the object is *not* - preserved by the library. Therefore, iterating an object may return - name/value pairs in a different order than they were originally stored. In - fact, keys will be traversed in alphabetical order as `std::map` with - `std::less` is used by default. Please note this behavior conforms to [RFC - 8259](https://tools.ietf.org/html/rfc8259), because any order implements the - specified "unordered" nature of JSON objects. - */ - using object_t = ObjectType>>; - - /*! - @brief a type for an array - - [RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON arrays as follows: - > An array is an ordered sequence of zero or more values. - - To store objects in C++, a type is defined by the template parameters - explained below. - - @tparam ArrayType container type to store arrays (e.g., `std::vector` or - `std::list`) - @tparam AllocatorType allocator to use for arrays (e.g., `std::allocator`) - - #### Default type - - With the default values for @a ArrayType (`std::vector`) and @a - AllocatorType (`std::allocator`), the default value for @a array_t is: - - @code {.cpp} - std::vector< - basic_json, // value_type - std::allocator // allocator_type - > - @endcode - - #### Limits - - [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: - > An implementation may set limits on the maximum depth of nesting. - - In this class, the array's limit of nesting is not explicitly constrained. - However, a maximum depth of nesting may be introduced by the compiler or - runtime environment. A theoretical limit can be queried by calling the - @ref max_size function of a JSON array. - - #### Storage - - Arrays are stored as pointers in a @ref basic_json type. That is, for any - access to array values, a pointer of type `array_t*` must be dereferenced. - - @sa see @ref object_t -- type for an object value - - @since version 1.0.0 - */ - using array_t = ArrayType>; - - /*! - @brief a type for a string - - [RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON strings as follows: - > A string is a sequence of zero or more Unicode characters. - - To store objects in C++, a type is defined by the template parameter - described below. Unicode values are split by the JSON class into - byte-sized characters during deserialization. - - @tparam StringType the container to store strings (e.g., `std::string`). - Note this container is used for keys/names in objects, see @ref object_t. - - #### Default type - - With the default values for @a StringType (`std::string`), the default - value for @a string_t is: - - @code {.cpp} - std::string - @endcode - - #### Encoding - - Strings are stored in UTF-8 encoding. Therefore, functions like - `std::string::size()` or `std::string::length()` return the number of - bytes in the string rather than the number of characters or glyphs. - - #### String comparison - - [RFC 8259](https://tools.ietf.org/html/rfc8259) states: - > Software implementations are typically required to test names of object - > members for equality. Implementations that transform the textual - > representation into sequences of Unicode code units and then perform the - > comparison numerically, code unit by code unit, are interoperable in the - > sense that implementations will agree in all cases on equality or - > inequality of two strings. For example, implementations that compare - > strings with escaped characters unconverted may incorrectly find that - > `"a\\b"` and `"a\u005Cb"` are not equal. - - This implementation is interoperable as it does compare strings code unit - by code unit. - - #### Storage - - String values are stored as pointers in a @ref basic_json type. That is, - for any access to string values, a pointer of type `string_t*` must be - dereferenced. - - @since version 1.0.0 - */ - using string_t = StringType; - - /*! - @brief a type for a boolean - - [RFC 8259](https://tools.ietf.org/html/rfc8259) implicitly describes a boolean as a - type which differentiates the two literals `true` and `false`. - - To store objects in C++, a type is defined by the template parameter @a - BooleanType which chooses the type to use. - - #### Default type - - With the default values for @a BooleanType (`bool`), the default value for - @a boolean_t is: - - @code {.cpp} - bool - @endcode - - #### Storage - - Boolean values are stored directly inside a @ref basic_json type. - - @since version 1.0.0 - */ - using boolean_t = BooleanType; - - /*! - @brief a type for a number (integer) - - [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: - > The representation of numbers is similar to that used in most - > programming languages. A number is represented in base 10 using decimal - > digits. It contains an integer component that may be prefixed with an - > optional minus sign, which may be followed by a fraction part and/or an - > exponent part. Leading zeros are not allowed. (...) Numeric values that - > cannot be represented in the grammar below (such as Infinity and NaN) - > are not permitted. - - This description includes both integer and floating-point numbers. - However, C++ allows more precise storage if it is known whether the number - is a signed integer, an unsigned integer or a floating-point number. - Therefore, three different types, @ref number_integer_t, @ref - number_unsigned_t and @ref number_float_t are used. - - To store integer numbers in C++, a type is defined by the template - parameter @a NumberIntegerType which chooses the type to use. - - #### Default type - - With the default values for @a NumberIntegerType (`int64_t`), the default - value for @a number_integer_t is: - - @code {.cpp} - int64_t - @endcode - - #### Default behavior - - - The restrictions about leading zeros is not enforced in C++. Instead, - leading zeros in integer literals lead to an interpretation as octal - number. Internally, the value will be stored as decimal number. For - instance, the C++ integer literal `010` will be serialized to `8`. - During deserialization, leading zeros yield an error. - - Not-a-number (NaN) values will be serialized to `null`. - - #### Limits - - [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: - > An implementation may set limits on the range and precision of numbers. - - When the default type is used, the maximal integer number that can be - stored is `9223372036854775807` (INT64_MAX) and the minimal integer number - that can be stored is `-9223372036854775808` (INT64_MIN). Integer numbers - that are out of range will yield over/underflow when used in a - constructor. During deserialization, too large or small integer numbers - will be automatically be stored as @ref number_unsigned_t or @ref - number_float_t. - - [RFC 8259](https://tools.ietf.org/html/rfc8259) further states: - > Note that when such software is used, numbers that are integers and are - > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense - > that implementations will agree exactly on their numeric values. - - As this range is a subrange of the exactly supported range [INT64_MIN, - INT64_MAX], this class's integer type is interoperable. - - #### Storage - - Integer number values are stored directly inside a @ref basic_json type. - - @sa see @ref number_float_t -- type for number values (floating-point) - - @sa see @ref number_unsigned_t -- type for number values (unsigned integer) - - @since version 1.0.0 - */ - using number_integer_t = NumberIntegerType; - - /*! - @brief a type for a number (unsigned) - - [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: - > The representation of numbers is similar to that used in most - > programming languages. A number is represented in base 10 using decimal - > digits. It contains an integer component that may be prefixed with an - > optional minus sign, which may be followed by a fraction part and/or an - > exponent part. Leading zeros are not allowed. (...) Numeric values that - > cannot be represented in the grammar below (such as Infinity and NaN) - > are not permitted. - - This description includes both integer and floating-point numbers. - However, C++ allows more precise storage if it is known whether the number - is a signed integer, an unsigned integer or a floating-point number. - Therefore, three different types, @ref number_integer_t, @ref - number_unsigned_t and @ref number_float_t are used. - - To store unsigned integer numbers in C++, a type is defined by the - template parameter @a NumberUnsignedType which chooses the type to use. - - #### Default type - - With the default values for @a NumberUnsignedType (`uint64_t`), the - default value for @a number_unsigned_t is: - - @code {.cpp} - uint64_t - @endcode - - #### Default behavior - - - The restrictions about leading zeros is not enforced in C++. Instead, - leading zeros in integer literals lead to an interpretation as octal - number. Internally, the value will be stored as decimal number. For - instance, the C++ integer literal `010` will be serialized to `8`. - During deserialization, leading zeros yield an error. - - Not-a-number (NaN) values will be serialized to `null`. - - #### Limits - - [RFC 8259](https://tools.ietf.org/html/rfc8259) specifies: - > An implementation may set limits on the range and precision of numbers. - - When the default type is used, the maximal integer number that can be - stored is `18446744073709551615` (UINT64_MAX) and the minimal integer - number that can be stored is `0`. Integer numbers that are out of range - will yield over/underflow when used in a constructor. During - deserialization, too large or small integer numbers will be automatically - be stored as @ref number_integer_t or @ref number_float_t. - - [RFC 8259](https://tools.ietf.org/html/rfc8259) further states: - > Note that when such software is used, numbers that are integers and are - > in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are interoperable in the sense - > that implementations will agree exactly on their numeric values. - - As this range is a subrange (when considered in conjunction with the - number_integer_t type) of the exactly supported range [0, UINT64_MAX], - this class's integer type is interoperable. - - #### Storage - - Integer number values are stored directly inside a @ref basic_json type. - - @sa see @ref number_float_t -- type for number values (floating-point) - @sa see @ref number_integer_t -- type for number values (integer) - - @since version 2.0.0 - */ - using number_unsigned_t = NumberUnsignedType; - - /*! - @brief a type for a number (floating-point) - - [RFC 8259](https://tools.ietf.org/html/rfc8259) describes numbers as follows: - > The representation of numbers is similar to that used in most - > programming languages. A number is represented in base 10 using decimal - > digits. It contains an integer component that may be prefixed with an - > optional minus sign, which may be followed by a fraction part and/or an - > exponent part. Leading zeros are not allowed. (...) Numeric values that - > cannot be represented in the grammar below (such as Infinity and NaN) - > are not permitted. - - This description includes both integer and floating-point numbers. - However, C++ allows more precise storage if it is known whether the number - is a signed integer, an unsigned integer or a floating-point number. - Therefore, three different types, @ref number_integer_t, @ref - number_unsigned_t and @ref number_float_t are used. - - To store floating-point numbers in C++, a type is defined by the template - parameter @a NumberFloatType which chooses the type to use. - - #### Default type - - With the default values for @a NumberFloatType (`double`), the default - value for @a number_float_t is: - - @code {.cpp} - double - @endcode - - #### Default behavior - - - The restrictions about leading zeros is not enforced in C++. Instead, - leading zeros in floating-point literals will be ignored. Internally, - the value will be stored as decimal number. For instance, the C++ - floating-point literal `01.2` will be serialized to `1.2`. During - deserialization, leading zeros yield an error. - - Not-a-number (NaN) values will be serialized to `null`. - - #### Limits - - [RFC 8259](https://tools.ietf.org/html/rfc8259) states: - > This specification allows implementations to set limits on the range and - > precision of numbers accepted. Since software that implements IEEE - > 754-2008 binary64 (double precision) numbers is generally available and - > widely used, good interoperability can be achieved by implementations - > that expect no more precision or range than these provide, in the sense - > that implementations will approximate JSON numbers within the expected - > precision. - - This implementation does exactly follow this approach, as it uses double - precision floating-point numbers. Note values smaller than - `-1.79769313486232e+308` and values greater than `1.79769313486232e+308` - will be stored as NaN internally and be serialized to `null`. - - #### Storage - - Floating-point number values are stored directly inside a @ref basic_json - type. - - @sa see @ref number_integer_t -- type for number values (integer) - - @sa see @ref number_unsigned_t -- type for number values (unsigned integer) - - @since version 1.0.0 - */ - using number_float_t = NumberFloatType; - - /*! - @brief a type for a packed binary type - - This type is a type designed to carry binary data that appears in various - serialized formats, such as CBOR's Major Type 2, MessagePack's bin, and - BSON's generic binary subtype. This type is NOT a part of standard JSON and - exists solely for compatibility with these binary types. As such, it is - simply defined as an ordered sequence of zero or more byte values. - - Additionally, as an implementation detail, the subtype of the binary data is - carried around as a `std::uint8_t`, which is compatible with both of the - binary data formats that use binary subtyping, (though the specific - numbering is incompatible with each other, and it is up to the user to - translate between them). - - [CBOR's RFC 7049](https://tools.ietf.org/html/rfc7049) describes this type - as: - > Major type 2: a byte string. The string's length in bytes is represented - > following the rules for positive integers (major type 0). - - [MessagePack's documentation on the bin type - family](https://github.com/msgpack/msgpack/blob/master/spec.md#bin-format-family) - describes this type as: - > Bin format family stores an byte array in 2, 3, or 5 bytes of extra bytes - > in addition to the size of the byte array. - - [BSON's specifications](http://bsonspec.org/spec.html) describe several - binary types; however, this type is intended to represent the generic binary - type which has the description: - > Generic binary subtype - This is the most commonly used binary subtype and - > should be the 'default' for drivers and tools. - - None of these impose any limitations on the internal representation other - than the basic unit of storage be some type of array whose parts are - decomposable into bytes. - - The default representation of this binary format is a - `std::vector`, which is a very common way to represent a byte - array in modern C++. - - #### Default type - - The default values for @a BinaryType is `std::vector` - - #### Storage - - Binary Arrays are stored as pointers in a @ref basic_json type. That is, - for any access to array values, a pointer of the type `binary_t*` must be - dereferenced. - - #### Notes on subtypes - - - CBOR - - Binary values are represented as byte strings. Subtypes are serialized - as tagged values. - - MessagePack - - If a subtype is given and the binary array contains exactly 1, 2, 4, 8, - or 16 elements, the fixext family (fixext1, fixext2, fixext4, fixext8) - is used. For other sizes, the ext family (ext8, ext16, ext32) is used. - The subtype is then added as singed 8-bit integer. - - If no subtype is given, the bin family (bin8, bin16, bin32) is used. - - BSON - - If a subtype is given, it is used and added as unsigned 8-bit integer. - - If no subtype is given, the generic binary subtype 0x00 is used. - - @sa see @ref binary -- create a binary array - - @since version 3.8.0 - */ - using binary_t = nlohmann::byte_container_with_subtype; - /// @} - - private: - - /// helper for exception-safe object creation - template - JSON_HEDLEY_RETURNS_NON_NULL - static T* create(Args&& ... args) - { - AllocatorType alloc; - using AllocatorTraits = std::allocator_traits>; - - auto deleter = [&](T * obj) - { - AllocatorTraits::deallocate(alloc, obj, 1); - }; - std::unique_ptr obj(AllocatorTraits::allocate(alloc, 1), deleter); - AllocatorTraits::construct(alloc, obj.get(), std::forward(args)...); - JSON_ASSERT(obj != nullptr); - return obj.release(); - } - - //////////////////////// - // JSON value storage // - //////////////////////// - - JSON_PRIVATE_UNLESS_TESTED: - /*! - @brief a JSON value - - The actual storage for a JSON value of the @ref basic_json class. This - union combines the different storage types for the JSON value types - defined in @ref value_t. - - JSON type | value_t type | used type - --------- | --------------- | ------------------------ - object | object | pointer to @ref object_t - array | array | pointer to @ref array_t - string | string | pointer to @ref string_t - boolean | boolean | @ref boolean_t - number | number_integer | @ref number_integer_t - number | number_unsigned | @ref number_unsigned_t - number | number_float | @ref number_float_t - binary | binary | pointer to @ref binary_t - null | null | *no value is stored* - - @note Variable-length types (objects, arrays, and strings) are stored as - pointers. The size of the union should not exceed 64 bits if the default - value types are used. - - @since version 1.0.0 - */ - union json_value - { - /// object (stored with pointer to save storage) - object_t* object; - /// array (stored with pointer to save storage) - array_t* array; - /// string (stored with pointer to save storage) - string_t* string; - /// binary (stored with pointer to save storage) - binary_t* binary; - /// boolean - boolean_t boolean; - /// number (integer) - number_integer_t number_integer; - /// number (unsigned integer) - number_unsigned_t number_unsigned; - /// number (floating-point) - number_float_t number_float; - - /// default constructor (for null values) - json_value() = default; - /// constructor for booleans - json_value(boolean_t v) noexcept : boolean(v) {} - /// constructor for numbers (integer) - json_value(number_integer_t v) noexcept : number_integer(v) {} - /// constructor for numbers (unsigned) - json_value(number_unsigned_t v) noexcept : number_unsigned(v) {} - /// constructor for numbers (floating-point) - json_value(number_float_t v) noexcept : number_float(v) {} - /// constructor for empty values of a given type - json_value(value_t t) - { - switch (t) - { - case value_t::object: - { - object = create(); - break; - } - - case value_t::array: - { - array = create(); - break; - } - - case value_t::string: - { - string = create(""); - break; - } - - case value_t::binary: - { - binary = create(); - break; - } - - case value_t::boolean: - { - boolean = boolean_t(false); - break; - } - - case value_t::number_integer: - { - number_integer = number_integer_t(0); - break; - } - - case value_t::number_unsigned: - { - number_unsigned = number_unsigned_t(0); - break; - } - - case value_t::number_float: - { - number_float = number_float_t(0.0); - break; - } - - case value_t::null: - { - object = nullptr; // silence warning, see #821 - break; - } - - case value_t::discarded: - default: - { - object = nullptr; // silence warning, see #821 - if (JSON_HEDLEY_UNLIKELY(t == value_t::null)) - { - JSON_THROW(other_error::create(500, "961c151d2e87f2686a955a9be24d316f1362bf21 3.10.4", basic_json())); // LCOV_EXCL_LINE - } - break; - } - } - } - - /// constructor for strings - json_value(const string_t& value) - { - string = create(value); - } - - /// constructor for rvalue strings - json_value(string_t&& value) - { - string = create(std::move(value)); - } - - /// constructor for objects - json_value(const object_t& value) - { - object = create(value); - } - - /// constructor for rvalue objects - json_value(object_t&& value) - { - object = create(std::move(value)); - } - - /// constructor for arrays - json_value(const array_t& value) - { - array = create(value); - } - - /// constructor for rvalue arrays - json_value(array_t&& value) - { - array = create(std::move(value)); - } - - /// constructor for binary arrays - json_value(const typename binary_t::container_type& value) - { - binary = create(value); - } - - /// constructor for rvalue binary arrays - json_value(typename binary_t::container_type&& value) - { - binary = create(std::move(value)); - } - - /// constructor for binary arrays (internal type) - json_value(const binary_t& value) - { - binary = create(value); - } - - /// constructor for rvalue binary arrays (internal type) - json_value(binary_t&& value) - { - binary = create(std::move(value)); - } - - void destroy(value_t t) - { - if (t == value_t::array || t == value_t::object) - { - // flatten the current json_value to a heap-allocated stack - std::vector stack; - - // move the top-level items to stack - if (t == value_t::array) - { - stack.reserve(array->size()); - std::move(array->begin(), array->end(), std::back_inserter(stack)); - } - else - { - stack.reserve(object->size()); - for (auto&& it : *object) - { - stack.push_back(std::move(it.second)); - } - } - - while (!stack.empty()) - { - // move the last item to local variable to be processed - basic_json current_item(std::move(stack.back())); - stack.pop_back(); - - // if current_item is array/object, move - // its children to the stack to be processed later - if (current_item.is_array()) - { - std::move(current_item.m_value.array->begin(), current_item.m_value.array->end(), std::back_inserter(stack)); - - current_item.m_value.array->clear(); - } - else if (current_item.is_object()) - { - for (auto&& it : *current_item.m_value.object) - { - stack.push_back(std::move(it.second)); - } - - current_item.m_value.object->clear(); - } - - // it's now safe that current_item get destructed - // since it doesn't have any children - } - } - - switch (t) - { - case value_t::object: - { - AllocatorType alloc; - std::allocator_traits::destroy(alloc, object); - std::allocator_traits::deallocate(alloc, object, 1); - break; - } - - case value_t::array: - { - AllocatorType alloc; - std::allocator_traits::destroy(alloc, array); - std::allocator_traits::deallocate(alloc, array, 1); - break; - } - - case value_t::string: - { - AllocatorType alloc; - std::allocator_traits::destroy(alloc, string); - std::allocator_traits::deallocate(alloc, string, 1); - break; - } - - case value_t::binary: - { - AllocatorType alloc; - std::allocator_traits::destroy(alloc, binary); - std::allocator_traits::deallocate(alloc, binary, 1); - break; - } - - case value_t::null: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::discarded: - default: - { - break; - } - } - } - }; - - private: - /*! - @brief checks the class invariants - - This function asserts the class invariants. It needs to be called at the - end of every constructor to make sure that created objects respect the - invariant. Furthermore, it has to be called each time the type of a JSON - value is changed, because the invariant expresses a relationship between - @a m_type and @a m_value. - - Furthermore, the parent relation is checked for arrays and objects: If - @a check_parents true and the value is an array or object, then the - container's elements must have the current value as parent. - - @param[in] check_parents whether the parent relation should be checked. - The value is true by default and should only be set to false - during destruction of objects when the invariant does not - need to hold. - */ - void assert_invariant(bool check_parents = true) const noexcept - { - JSON_ASSERT(m_type != value_t::object || m_value.object != nullptr); - JSON_ASSERT(m_type != value_t::array || m_value.array != nullptr); - JSON_ASSERT(m_type != value_t::string || m_value.string != nullptr); - JSON_ASSERT(m_type != value_t::binary || m_value.binary != nullptr); - -#if JSON_DIAGNOSTICS - JSON_TRY - { - // cppcheck-suppress assertWithSideEffect - JSON_ASSERT(!check_parents || !is_structured() || std::all_of(begin(), end(), [this](const basic_json & j) - { - return j.m_parent == this; - })); - } - JSON_CATCH(...) {} // LCOV_EXCL_LINE -#endif - static_cast(check_parents); - } - - void set_parents() - { -#if JSON_DIAGNOSTICS - switch (m_type) - { - case value_t::array: - { - for (auto& element : *m_value.array) - { - element.m_parent = this; - } - break; - } - - case value_t::object: - { - for (auto& element : *m_value.object) - { - element.second.m_parent = this; - } - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - break; - } -#endif - } - - iterator set_parents(iterator it, typename iterator::difference_type count) - { -#if JSON_DIAGNOSTICS - for (typename iterator::difference_type i = 0; i < count; ++i) - { - (it + i)->m_parent = this; - } -#else - static_cast(count); -#endif - return it; - } - - reference set_parent(reference j, std::size_t old_capacity = std::size_t(-1)) - { -#if JSON_DIAGNOSTICS - if (old_capacity != std::size_t(-1)) - { - // see https://github.com/nlohmann/json/issues/2838 - JSON_ASSERT(type() == value_t::array); - if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity)) - { - // capacity has changed: update all parents - set_parents(); - return j; - } - } - - // ordered_json uses a vector internally, so pointers could have - // been invalidated; see https://github.com/nlohmann/json/issues/2962 -#ifdef JSON_HEDLEY_MSVC_VERSION -#pragma warning(push ) -#pragma warning(disable : 4127) // ignore warning to replace if with if constexpr -#endif - if (detail::is_ordered_map::value) - { - set_parents(); - return j; - } -#ifdef JSON_HEDLEY_MSVC_VERSION -#pragma warning( pop ) -#endif - - j.m_parent = this; -#else - static_cast(j); - static_cast(old_capacity); -#endif - return j; - } - - public: - ////////////////////////// - // JSON parser callback // - ////////////////////////// - - /*! - @brief parser event types - - The parser callback distinguishes the following events: - - `object_start`: the parser read `{` and started to process a JSON object - - `key`: the parser read a key of a value in an object - - `object_end`: the parser read `}` and finished processing a JSON object - - `array_start`: the parser read `[` and started to process a JSON array - - `array_end`: the parser read `]` and finished processing a JSON array - - `value`: the parser finished reading a JSON value - - @image html callback_events.png "Example when certain parse events are triggered" - - @sa see @ref parser_callback_t for more information and examples - */ - using parse_event_t = detail::parse_event_t; - - /*! - @brief per-element parser callback type - - With a parser callback function, the result of parsing a JSON text can be - influenced. When passed to @ref parse, it is called on certain events - (passed as @ref parse_event_t via parameter @a event) with a set recursion - depth @a depth and context JSON value @a parsed. The return value of the - callback function is a boolean indicating whether the element that emitted - the callback shall be kept or not. - - We distinguish six scenarios (determined by the event type) in which the - callback function can be called. The following table describes the values - of the parameters @a depth, @a event, and @a parsed. - - parameter @a event | description | parameter @a depth | parameter @a parsed - ------------------ | ----------- | ------------------ | ------------------- - parse_event_t::object_start | the parser read `{` and started to process a JSON object | depth of the parent of the JSON object | a JSON value with type discarded - parse_event_t::key | the parser read a key of a value in an object | depth of the currently parsed JSON object | a JSON string containing the key - parse_event_t::object_end | the parser read `}` and finished processing a JSON object | depth of the parent of the JSON object | the parsed JSON object - parse_event_t::array_start | the parser read `[` and started to process a JSON array | depth of the parent of the JSON array | a JSON value with type discarded - parse_event_t::array_end | the parser read `]` and finished processing a JSON array | depth of the parent of the JSON array | the parsed JSON array - parse_event_t::value | the parser finished reading a JSON value | depth of the value | the parsed JSON value - - @image html callback_events.png "Example when certain parse events are triggered" - - Discarding a value (i.e., returning `false`) has different effects - depending on the context in which function was called: - - - Discarded values in structured types are skipped. That is, the parser - will behave as if the discarded value was never read. - - In case a value outside a structured type is skipped, it is replaced - with `null`. This case happens if the top-level element is skipped. - - @param[in] depth the depth of the recursion during parsing - - @param[in] event an event of type parse_event_t indicating the context in - the callback function has been called - - @param[in,out] parsed the current intermediate parse result; note that - writing to this value has no effect for parse_event_t::key events - - @return Whether the JSON value which called the function during parsing - should be kept (`true`) or not (`false`). In the latter case, it is either - skipped completely or replaced by an empty discarded object. - - @sa see @ref parse for examples - - @since version 1.0.0 - */ - using parser_callback_t = detail::parser_callback_t; - - ////////////////// - // constructors // - ////////////////// - - /// @name constructors and destructors - /// Constructors of class @ref basic_json, copy/move constructor, copy - /// assignment, static functions creating objects, and the destructor. - /// @{ - - /*! - @brief create an empty value with a given type - - Create an empty JSON value with a given type. The value will be default - initialized with an empty value which depends on the type: - - Value type | initial value - ----------- | ------------- - null | `null` - boolean | `false` - string | `""` - number | `0` - object | `{}` - array | `[]` - binary | empty array - - @param[in] v the type of the value to create - - @complexity Constant. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes to any JSON value. - - @liveexample{The following code shows the constructor for different @ref - value_t values,basic_json__value_t} - - @sa see @ref clear() -- restores the postcondition of this constructor - - @since version 1.0.0 - */ - basic_json(const value_t v) - : m_type(v), m_value(v) - { - assert_invariant(); - } - - /*! - @brief create a null object - - Create a `null` JSON value. It either takes a null pointer as parameter - (explicitly creating `null`) or no parameter (implicitly creating `null`). - The passed null pointer itself is not read -- it is only used to choose - the right constructor. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this constructor never throws - exceptions. - - @liveexample{The following code shows the constructor with and without a - null pointer parameter.,basic_json__nullptr_t} - - @since version 1.0.0 - */ - basic_json(std::nullptr_t = nullptr) noexcept - : basic_json(value_t::null) - { - assert_invariant(); - } - - /*! - @brief create a JSON value - - This is a "catch all" constructor for all compatible JSON types; that is, - types for which a `to_json()` method exists. The constructor forwards the - parameter @a val to that method (to `json_serializer::to_json` method - with `U = uncvref_t`, to be exact). - - Template type @a CompatibleType includes, but is not limited to, the - following types: - - **arrays**: @ref array_t and all kinds of compatible containers such as - `std::vector`, `std::deque`, `std::list`, `std::forward_list`, - `std::array`, `std::valarray`, `std::set`, `std::unordered_set`, - `std::multiset`, and `std::unordered_multiset` with a `value_type` from - which a @ref basic_json value can be constructed. - - **objects**: @ref object_t and all kinds of compatible associative - containers such as `std::map`, `std::unordered_map`, `std::multimap`, - and `std::unordered_multimap` with a `key_type` compatible to - @ref string_t and a `value_type` from which a @ref basic_json value can - be constructed. - - **strings**: @ref string_t, string literals, and all compatible string - containers can be used. - - **numbers**: @ref number_integer_t, @ref number_unsigned_t, - @ref number_float_t, and all convertible number types such as `int`, - `size_t`, `int64_t`, `float` or `double` can be used. - - **boolean**: @ref boolean_t / `bool` can be used. - - **binary**: @ref binary_t / `std::vector` may be used, - unfortunately because string literals cannot be distinguished from binary - character arrays by the C++ type system, all types compatible with `const - char*` will be directed to the string constructor instead. This is both - for backwards compatibility, and due to the fact that a binary type is not - a standard JSON type. - - See the examples below. - - @tparam CompatibleType a type such that: - - @a CompatibleType is not derived from `std::istream`, - - @a CompatibleType is not @ref basic_json (to avoid hijacking copy/move - constructors), - - @a CompatibleType is not a different @ref basic_json type (i.e. with different template arguments) - - @a CompatibleType is not a @ref basic_json nested type (e.g., - @ref json_pointer, @ref iterator, etc ...) - - `json_serializer` has a `to_json(basic_json_t&, CompatibleType&&)` method - - @tparam U = `uncvref_t` - - @param[in] val the value to be forwarded to the respective constructor - - @complexity Usually linear in the size of the passed @a val, also - depending on the implementation of the called `to_json()` - method. - - @exceptionsafety Depends on the called constructor. For types directly - supported by the library (i.e., all types for which no `to_json()` function - was provided), strong guarantee holds: if an exception is thrown, there are - no changes to any JSON value. - - @liveexample{The following code shows the constructor with several - compatible types.,basic_json__CompatibleType} - - @since version 2.1.0 - */ - template < typename CompatibleType, - typename U = detail::uncvref_t, - detail::enable_if_t < - !detail::is_basic_json::value && detail::is_compatible_type::value, int > = 0 > - basic_json(CompatibleType && val) noexcept(noexcept( // NOLINT(bugprone-forwarding-reference-overload,bugprone-exception-escape) - JSONSerializer::to_json(std::declval(), - std::forward(val)))) - { - JSONSerializer::to_json(*this, std::forward(val)); - set_parents(); - assert_invariant(); - } - - /*! - @brief create a JSON value from an existing one - - This is a constructor for existing @ref basic_json types. - It does not hijack copy/move constructors, since the parameter has different - template arguments than the current ones. - - The constructor tries to convert the internal @ref m_value of the parameter. - - @tparam BasicJsonType a type such that: - - @a BasicJsonType is a @ref basic_json type. - - @a BasicJsonType has different template arguments than @ref basic_json_t. - - @param[in] val the @ref basic_json value to be converted. - - @complexity Usually linear in the size of the passed @a val, also - depending on the implementation of the called `to_json()` - method. - - @exceptionsafety Depends on the called constructor. For types directly - supported by the library (i.e., all types for which no `to_json()` function - was provided), strong guarantee holds: if an exception is thrown, there are - no changes to any JSON value. - - @since version 3.2.0 - */ - template < typename BasicJsonType, - detail::enable_if_t < - detail::is_basic_json::value&& !std::is_same::value, int > = 0 > - basic_json(const BasicJsonType& val) - { - using other_boolean_t = typename BasicJsonType::boolean_t; - using other_number_float_t = typename BasicJsonType::number_float_t; - using other_number_integer_t = typename BasicJsonType::number_integer_t; - using other_number_unsigned_t = typename BasicJsonType::number_unsigned_t; - using other_string_t = typename BasicJsonType::string_t; - using other_object_t = typename BasicJsonType::object_t; - using other_array_t = typename BasicJsonType::array_t; - using other_binary_t = typename BasicJsonType::binary_t; - - switch (val.type()) - { - case value_t::boolean: - JSONSerializer::to_json(*this, val.template get()); - break; - case value_t::number_float: - JSONSerializer::to_json(*this, val.template get()); - break; - case value_t::number_integer: - JSONSerializer::to_json(*this, val.template get()); - break; - case value_t::number_unsigned: - JSONSerializer::to_json(*this, val.template get()); - break; - case value_t::string: - JSONSerializer::to_json(*this, val.template get_ref()); - break; - case value_t::object: - JSONSerializer::to_json(*this, val.template get_ref()); - break; - case value_t::array: - JSONSerializer::to_json(*this, val.template get_ref()); - break; - case value_t::binary: - JSONSerializer::to_json(*this, val.template get_ref()); - break; - case value_t::null: - *this = nullptr; - break; - case value_t::discarded: - m_type = value_t::discarded; - break; - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - } - set_parents(); - assert_invariant(); - } - - /*! - @brief create a container (array or object) from an initializer list - - Creates a JSON value of type array or object from the passed initializer - list @a init. In case @a type_deduction is `true` (default), the type of - the JSON value to be created is deducted from the initializer list @a init - according to the following rules: - - 1. If the list is empty, an empty JSON object value `{}` is created. - 2. If the list consists of pairs whose first element is a string, a JSON - object value is created where the first elements of the pairs are - treated as keys and the second elements are as values. - 3. In all other cases, an array is created. - - The rules aim to create the best fit between a C++ initializer list and - JSON values. The rationale is as follows: - - 1. The empty initializer list is written as `{}` which is exactly an empty - JSON object. - 2. C++ has no way of describing mapped types other than to list a list of - pairs. As JSON requires that keys must be of type string, rule 2 is the - weakest constraint one can pose on initializer lists to interpret them - as an object. - 3. In all other cases, the initializer list could not be interpreted as - JSON object type, so interpreting it as JSON array type is safe. - - With the rules described above, the following JSON values cannot be - expressed by an initializer list: - - - the empty array (`[]`): use @ref array(initializer_list_t) - with an empty initializer list in this case - - arrays whose elements satisfy rule 2: use @ref - array(initializer_list_t) with the same initializer list - in this case - - @note When used without parentheses around an empty initializer list, @ref - basic_json() is called instead of this function, yielding the JSON null - value. - - @param[in] init initializer list with JSON values - - @param[in] type_deduction internal parameter; when set to `true`, the type - of the JSON value is deducted from the initializer list @a init; when set - to `false`, the type provided via @a manual_type is forced. This mode is - used by the functions @ref array(initializer_list_t) and - @ref object(initializer_list_t). - - @param[in] manual_type internal parameter; when @a type_deduction is set - to `false`, the created JSON value will use the provided type (only @ref - value_t::array and @ref value_t::object are valid); when @a type_deduction - is set to `true`, this parameter has no effect - - @throw type_error.301 if @a type_deduction is `false`, @a manual_type is - `value_t::object`, but @a init contains an element which is not a pair - whose first element is a string. In this case, the constructor could not - create an object. If @a type_deduction would have be `true`, an array - would have been created. See @ref object(initializer_list_t) - for an example. - - @complexity Linear in the size of the initializer list @a init. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes to any JSON value. - - @liveexample{The example below shows how JSON values are created from - initializer lists.,basic_json__list_init_t} - - @sa see @ref array(initializer_list_t) -- create a JSON array - value from an initializer list - @sa see @ref object(initializer_list_t) -- create a JSON object - value from an initializer list - - @since version 1.0.0 - */ - basic_json(initializer_list_t init, - bool type_deduction = true, - value_t manual_type = value_t::array) - { - // check if each element is an array with two elements whose first - // element is a string - bool is_an_object = std::all_of(init.begin(), init.end(), - [](const detail::json_ref& element_ref) - { - return element_ref->is_array() && element_ref->size() == 2 && (*element_ref)[0].is_string(); - }); - - // adjust type if type deduction is not wanted - if (!type_deduction) - { - // if array is wanted, do not create an object though possible - if (manual_type == value_t::array) - { - is_an_object = false; - } - - // if object is wanted but impossible, throw an exception - if (JSON_HEDLEY_UNLIKELY(manual_type == value_t::object && !is_an_object)) - { - JSON_THROW(type_error::create(301, "cannot create object from initializer list", basic_json())); - } - } - - if (is_an_object) - { - // the initializer list is a list of pairs -> create object - m_type = value_t::object; - m_value = value_t::object; - - for (auto& element_ref : init) - { - auto element = element_ref.moved_or_copied(); - m_value.object->emplace( - std::move(*((*element.m_value.array)[0].m_value.string)), - std::move((*element.m_value.array)[1])); - } - } - else - { - // the initializer list describes an array -> create array - m_type = value_t::array; - m_value.array = create(init.begin(), init.end()); - } - - set_parents(); - assert_invariant(); - } - - /*! - @brief explicitly create a binary array (without subtype) - - Creates a JSON binary array value from a given binary container. Binary - values are part of various binary formats, such as CBOR, MessagePack, and - BSON. This constructor is used to create a value for serialization to those - formats. - - @note Note, this function exists because of the difficulty in correctly - specifying the correct template overload in the standard value ctor, as both - JSON arrays and JSON binary arrays are backed with some form of a - `std::vector`. Because JSON binary arrays are a non-standard extension it - was decided that it would be best to prevent automatic initialization of a - binary array type, for backwards compatibility and so it does not happen on - accident. - - @param[in] init container containing bytes to use as binary type - - @return JSON binary array value - - @complexity Linear in the size of @a init. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes to any JSON value. - - @since version 3.8.0 - */ - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json binary(const typename binary_t::container_type& init) - { - auto res = basic_json(); - res.m_type = value_t::binary; - res.m_value = init; - return res; - } - - /*! - @brief explicitly create a binary array (with subtype) - - Creates a JSON binary array value from a given binary container. Binary - values are part of various binary formats, such as CBOR, MessagePack, and - BSON. This constructor is used to create a value for serialization to those - formats. - - @note Note, this function exists because of the difficulty in correctly - specifying the correct template overload in the standard value ctor, as both - JSON arrays and JSON binary arrays are backed with some form of a - `std::vector`. Because JSON binary arrays are a non-standard extension it - was decided that it would be best to prevent automatic initialization of a - binary array type, for backwards compatibility and so it does not happen on - accident. - - @param[in] init container containing bytes to use as binary type - @param[in] subtype subtype to use in MessagePack and BSON - - @return JSON binary array value - - @complexity Linear in the size of @a init. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes to any JSON value. - - @since version 3.8.0 - */ - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json binary(const typename binary_t::container_type& init, typename binary_t::subtype_type subtype) - { - auto res = basic_json(); - res.m_type = value_t::binary; - res.m_value = binary_t(init, subtype); - return res; - } - - /// @copydoc binary(const typename binary_t::container_type&) - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json binary(typename binary_t::container_type&& init) - { - auto res = basic_json(); - res.m_type = value_t::binary; - res.m_value = std::move(init); - return res; - } - - /// @copydoc binary(const typename binary_t::container_type&, typename binary_t::subtype_type) - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json binary(typename binary_t::container_type&& init, typename binary_t::subtype_type subtype) - { - auto res = basic_json(); - res.m_type = value_t::binary; - res.m_value = binary_t(std::move(init), subtype); - return res; - } - - /*! - @brief explicitly create an array from an initializer list - - Creates a JSON array value from a given initializer list. That is, given a - list of values `a, b, c`, creates the JSON value `[a, b, c]`. If the - initializer list is empty, the empty array `[]` is created. - - @note This function is only needed to express two edge cases that cannot - be realized with the initializer list constructor (@ref - basic_json(initializer_list_t, bool, value_t)). These cases - are: - 1. creating an array whose elements are all pairs whose first element is a - string -- in this case, the initializer list constructor would create an - object, taking the first elements as keys - 2. creating an empty array -- passing the empty initializer list to the - initializer list constructor yields an empty object - - @param[in] init initializer list with JSON values to create an array from - (optional) - - @return JSON array value - - @complexity Linear in the size of @a init. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes to any JSON value. - - @liveexample{The following code shows an example for the `array` - function.,array} - - @sa see @ref basic_json(initializer_list_t, bool, value_t) -- - create a JSON value from an initializer list - @sa see @ref object(initializer_list_t) -- create a JSON object - value from an initializer list - - @since version 1.0.0 - */ - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json array(initializer_list_t init = {}) - { - return basic_json(init, false, value_t::array); - } - - /*! - @brief explicitly create an object from an initializer list - - Creates a JSON object value from a given initializer list. The initializer - lists elements must be pairs, and their first elements must be strings. If - the initializer list is empty, the empty object `{}` is created. - - @note This function is only added for symmetry reasons. In contrast to the - related function @ref array(initializer_list_t), there are - no cases which can only be expressed by this function. That is, any - initializer list @a init can also be passed to the initializer list - constructor @ref basic_json(initializer_list_t, bool, value_t). - - @param[in] init initializer list to create an object from (optional) - - @return JSON object value - - @throw type_error.301 if @a init is not a list of pairs whose first - elements are strings. In this case, no object can be created. When such a - value is passed to @ref basic_json(initializer_list_t, bool, value_t), - an array would have been created from the passed initializer list @a init. - See example below. - - @complexity Linear in the size of @a init. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes to any JSON value. - - @liveexample{The following code shows an example for the `object` - function.,object} - - @sa see @ref basic_json(initializer_list_t, bool, value_t) -- - create a JSON value from an initializer list - @sa see @ref array(initializer_list_t) -- create a JSON array - value from an initializer list - - @since version 1.0.0 - */ - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json object(initializer_list_t init = {}) - { - return basic_json(init, false, value_t::object); - } - - /*! - @brief construct an array with count copies of given value - - Constructs a JSON array value by creating @a cnt copies of a passed value. - In case @a cnt is `0`, an empty array is created. - - @param[in] cnt the number of JSON copies of @a val to create - @param[in] val the JSON value to copy - - @post `std::distance(begin(),end()) == cnt` holds. - - @complexity Linear in @a cnt. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes to any JSON value. - - @liveexample{The following code shows examples for the @ref - basic_json(size_type\, const basic_json&) - constructor.,basic_json__size_type_basic_json} - - @since version 1.0.0 - */ - basic_json(size_type cnt, const basic_json& val) - : m_type(value_t::array) - { - m_value.array = create(cnt, val); - set_parents(); - assert_invariant(); - } - - /*! - @brief construct a JSON container given an iterator range - - Constructs the JSON value with the contents of the range `[first, last)`. - The semantics depends on the different types a JSON value can have: - - In case of a null type, invalid_iterator.206 is thrown. - - In case of other primitive types (number, boolean, or string), @a first - must be `begin()` and @a last must be `end()`. In this case, the value is - copied. Otherwise, invalid_iterator.204 is thrown. - - In case of structured types (array, object), the constructor behaves as - similar versions for `std::vector` or `std::map`; that is, a JSON array - or object is constructed from the values in the range. - - @tparam InputIT an input iterator type (@ref iterator or @ref - const_iterator) - - @param[in] first begin of the range to copy from (included) - @param[in] last end of the range to copy from (excluded) - - @pre Iterators @a first and @a last must be initialized. **This - precondition is enforced with an assertion (see warning).** If - assertions are switched off, a violation of this precondition yields - undefined behavior. - - @pre Range `[first, last)` is valid. Usually, this precondition cannot be - checked efficiently. Only certain edge cases are detected; see the - description of the exceptions below. A violation of this precondition - yields undefined behavior. - - @warning A precondition is enforced with a runtime assertion that will - result in calling `std::abort` if this precondition is not met. - Assertions can be disabled by defining `NDEBUG` at compile time. - See https://en.cppreference.com/w/cpp/error/assert for more - information. - - @throw invalid_iterator.201 if iterators @a first and @a last are not - compatible (i.e., do not belong to the same JSON value). In this case, - the range `[first, last)` is undefined. - @throw invalid_iterator.204 if iterators @a first and @a last belong to a - primitive type (number, boolean, or string), but @a first does not point - to the first element any more. In this case, the range `[first, last)` is - undefined. See example code below. - @throw invalid_iterator.206 if iterators @a first and @a last belong to a - null value. In this case, the range `[first, last)` is undefined. - - @complexity Linear in distance between @a first and @a last. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes to any JSON value. - - @liveexample{The example below shows several ways to create JSON values by - specifying a subrange with iterators.,basic_json__InputIt_InputIt} - - @since version 1.0.0 - */ - template < class InputIT, typename std::enable_if < - std::is_same::value || - std::is_same::value, int >::type = 0 > - basic_json(InputIT first, InputIT last) - { - JSON_ASSERT(first.m_object != nullptr); - JSON_ASSERT(last.m_object != nullptr); - - // make sure iterator fits the current value - if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) - { - JSON_THROW(invalid_iterator::create(201, "iterators are not compatible", basic_json())); - } - - // copy type from first iterator - m_type = first.m_object->m_type; - - // check if iterator range is complete for primitive values - switch (m_type) - { - case value_t::boolean: - case value_t::number_float: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::string: - { - if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() - || !last.m_it.primitive_iterator.is_end())) - { - JSON_THROW(invalid_iterator::create(204, "iterators out of range", *first.m_object)); - } - break; - } - - case value_t::null: - case value_t::object: - case value_t::array: - case value_t::binary: - case value_t::discarded: - default: - break; - } - - switch (m_type) - { - case value_t::number_integer: - { - m_value.number_integer = first.m_object->m_value.number_integer; - break; - } - - case value_t::number_unsigned: - { - m_value.number_unsigned = first.m_object->m_value.number_unsigned; - break; - } - - case value_t::number_float: - { - m_value.number_float = first.m_object->m_value.number_float; - break; - } - - case value_t::boolean: - { - m_value.boolean = first.m_object->m_value.boolean; - break; - } - - case value_t::string: - { - m_value = *first.m_object->m_value.string; - break; - } - - case value_t::object: - { - m_value.object = create(first.m_it.object_iterator, - last.m_it.object_iterator); - break; - } - - case value_t::array: - { - m_value.array = create(first.m_it.array_iterator, - last.m_it.array_iterator); - break; - } - - case value_t::binary: - { - m_value = *first.m_object->m_value.binary; - break; - } - - case value_t::null: - case value_t::discarded: - default: - JSON_THROW(invalid_iterator::create(206, "cannot construct with iterators from " + std::string(first.m_object->type_name()), *first.m_object)); - } - - set_parents(); - assert_invariant(); - } - - - /////////////////////////////////////// - // other constructors and destructor // - /////////////////////////////////////// - - template, - std::is_same>::value, int> = 0 > - basic_json(const JsonRef& ref) : basic_json(ref.moved_or_copied()) {} - - /*! - @brief copy constructor - - Creates a copy of a given JSON value. - - @param[in] other the JSON value to copy - - @post `*this == other` - - @complexity Linear in the size of @a other. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes to any JSON value. - - @requirement This function helps `basic_json` satisfying the - [Container](https://en.cppreference.com/w/cpp/named_req/Container) - requirements: - - The complexity is linear. - - As postcondition, it holds: `other == basic_json(other)`. - - @liveexample{The following code shows an example for the copy - constructor.,basic_json__basic_json} - - @since version 1.0.0 - */ - basic_json(const basic_json& other) - : m_type(other.m_type) - { - // check of passed value is valid - other.assert_invariant(); - - switch (m_type) - { - case value_t::object: - { - m_value = *other.m_value.object; - break; - } - - case value_t::array: - { - m_value = *other.m_value.array; - break; - } - - case value_t::string: - { - m_value = *other.m_value.string; - break; - } - - case value_t::boolean: - { - m_value = other.m_value.boolean; - break; - } - - case value_t::number_integer: - { - m_value = other.m_value.number_integer; - break; - } - - case value_t::number_unsigned: - { - m_value = other.m_value.number_unsigned; - break; - } - - case value_t::number_float: - { - m_value = other.m_value.number_float; - break; - } - - case value_t::binary: - { - m_value = *other.m_value.binary; - break; - } - - case value_t::null: - case value_t::discarded: - default: - break; - } - - set_parents(); - assert_invariant(); - } - - /*! - @brief move constructor - - Move constructor. Constructs a JSON value with the contents of the given - value @a other using move semantics. It "steals" the resources from @a - other and leaves it as JSON null value. - - @param[in,out] other value to move to this object - - @post `*this` has the same value as @a other before the call. - @post @a other is a JSON null value. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this constructor never throws - exceptions. - - @requirement This function helps `basic_json` satisfying the - [MoveConstructible](https://en.cppreference.com/w/cpp/named_req/MoveConstructible) - requirements. - - @liveexample{The code below shows the move constructor explicitly called - via std::move.,basic_json__moveconstructor} - - @since version 1.0.0 - */ - basic_json(basic_json&& other) noexcept - : m_type(std::move(other.m_type)), - m_value(std::move(other.m_value)) - { - // check that passed value is valid - other.assert_invariant(false); - - // invalidate payload - other.m_type = value_t::null; - other.m_value = {}; - - set_parents(); - assert_invariant(); - } - - /*! - @brief copy assignment - - Copy assignment operator. Copies a JSON value via the "copy and swap" - strategy: It is expressed in terms of the copy constructor, destructor, - and the `swap()` member function. - - @param[in] other value to copy from - - @complexity Linear. - - @requirement This function helps `basic_json` satisfying the - [Container](https://en.cppreference.com/w/cpp/named_req/Container) - requirements: - - The complexity is linear. - - @liveexample{The code below shows and example for the copy assignment. It - creates a copy of value `a` which is then swapped with `b`. Finally\, the - copy of `a` (which is the null value after the swap) is - destroyed.,basic_json__copyassignment} - - @since version 1.0.0 - */ - basic_json& operator=(basic_json other) noexcept ( - std::is_nothrow_move_constructible::value&& - std::is_nothrow_move_assignable::value&& - std::is_nothrow_move_constructible::value&& - std::is_nothrow_move_assignable::value - ) - { - // check that passed value is valid - other.assert_invariant(); - - using std::swap; - swap(m_type, other.m_type); - swap(m_value, other.m_value); - - set_parents(); - assert_invariant(); - return *this; - } - - /*! - @brief destructor - - Destroys the JSON value and frees all allocated memory. - - @complexity Linear. - - @requirement This function helps `basic_json` satisfying the - [Container](https://en.cppreference.com/w/cpp/named_req/Container) - requirements: - - The complexity is linear. - - All stored elements are destroyed and all memory is freed. - - @since version 1.0.0 - */ - ~basic_json() noexcept - { - assert_invariant(false); - m_value.destroy(m_type); - } - - /// @} - - public: - /////////////////////// - // object inspection // - /////////////////////// - - /// @name object inspection - /// Functions to inspect the type of a JSON value. - /// @{ - - /*! - @brief serialization - - Serialization function for JSON values. The function tries to mimic - Python's `json.dumps()` function, and currently supports its @a indent - and @a ensure_ascii parameters. - - @param[in] indent If indent is nonnegative, then array elements and object - members will be pretty-printed with that indent level. An indent level of - `0` will only insert newlines. `-1` (the default) selects the most compact - representation. - @param[in] indent_char The character to use for indentation if @a indent is - greater than `0`. The default is ` ` (space). - @param[in] ensure_ascii If @a ensure_ascii is true, all non-ASCII characters - in the output are escaped with `\uXXXX` sequences, and the result consists - of ASCII characters only. - @param[in] error_handler how to react on decoding errors; there are three - possible values: `strict` (throws and exception in case a decoding error - occurs; default), `replace` (replace invalid UTF-8 sequences with U+FFFD), - and `ignore` (ignore invalid UTF-8 sequences during serialization; all - bytes are copied to the output unchanged). - - @return string containing the serialization of the JSON value - - @throw type_error.316 if a string stored inside the JSON value is not - UTF-8 encoded and @a error_handler is set to strict - - @note Binary values are serialized as object containing two keys: - - "bytes": an array of bytes as integers - - "subtype": the subtype as integer or "null" if the binary has no subtype - - @complexity Linear. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes in the JSON value. - - @liveexample{The following example shows the effect of different @a indent\, - @a indent_char\, and @a ensure_ascii parameters to the result of the - serialization.,dump} - - @see https://docs.python.org/2/library/json.html#json.dump - - @since version 1.0.0; indentation character @a indent_char, option - @a ensure_ascii and exceptions added in version 3.0.0; error - handlers added in version 3.4.0; serialization of binary values added - in version 3.8.0. - */ - string_t dump(const int indent = -1, - const char indent_char = ' ', - const bool ensure_ascii = false, - const error_handler_t error_handler = error_handler_t::strict) const - { - string_t result; - serializer s(detail::output_adapter(result), indent_char, error_handler); - - if (indent >= 0) - { - s.dump(*this, true, ensure_ascii, static_cast(indent)); - } - else - { - s.dump(*this, false, ensure_ascii, 0); - } - - return result; - } - - /*! - @brief return the type of the JSON value (explicit) - - Return the type of the JSON value as a value from the @ref value_t - enumeration. - - @return the type of the JSON value - Value type | return value - ------------------------- | ------------------------- - null | value_t::null - boolean | value_t::boolean - string | value_t::string - number (integer) | value_t::number_integer - number (unsigned integer) | value_t::number_unsigned - number (floating-point) | value_t::number_float - object | value_t::object - array | value_t::array - binary | value_t::binary - discarded | value_t::discarded - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `type()` for all JSON - types.,type} - - @sa see @ref operator value_t() -- return the type of the JSON value (implicit) - @sa see @ref type_name() -- return the type as string - - @since version 1.0.0 - */ - constexpr value_t type() const noexcept - { - return m_type; - } - - /*! - @brief return whether type is primitive - - This function returns true if and only if the JSON type is primitive - (string, number, boolean, or null). - - @return `true` if type is primitive (string, number, boolean, or null), - `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_primitive()` for all JSON - types.,is_primitive} - - @sa see @ref is_structured() -- returns whether JSON value is structured - @sa see @ref is_null() -- returns whether JSON value is `null` - @sa see @ref is_string() -- returns whether JSON value is a string - @sa see @ref is_boolean() -- returns whether JSON value is a boolean - @sa see @ref is_number() -- returns whether JSON value is a number - @sa see @ref is_binary() -- returns whether JSON value is a binary array - - @since version 1.0.0 - */ - constexpr bool is_primitive() const noexcept - { - return is_null() || is_string() || is_boolean() || is_number() || is_binary(); - } - - /*! - @brief return whether type is structured - - This function returns true if and only if the JSON type is structured - (array or object). - - @return `true` if type is structured (array or object), `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_structured()` for all JSON - types.,is_structured} - - @sa see @ref is_primitive() -- returns whether value is primitive - @sa see @ref is_array() -- returns whether value is an array - @sa see @ref is_object() -- returns whether value is an object - - @since version 1.0.0 - */ - constexpr bool is_structured() const noexcept - { - return is_array() || is_object(); - } - - /*! - @brief return whether value is null - - This function returns true if and only if the JSON value is null. - - @return `true` if type is null, `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_null()` for all JSON - types.,is_null} - - @since version 1.0.0 - */ - constexpr bool is_null() const noexcept - { - return m_type == value_t::null; - } - - /*! - @brief return whether value is a boolean - - This function returns true if and only if the JSON value is a boolean. - - @return `true` if type is boolean, `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_boolean()` for all JSON - types.,is_boolean} - - @since version 1.0.0 - */ - constexpr bool is_boolean() const noexcept - { - return m_type == value_t::boolean; - } - - /*! - @brief return whether value is a number - - This function returns true if and only if the JSON value is a number. This - includes both integer (signed and unsigned) and floating-point values. - - @return `true` if type is number (regardless whether integer, unsigned - integer or floating-type), `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_number()` for all JSON - types.,is_number} - - @sa see @ref is_number_integer() -- check if value is an integer or unsigned - integer number - @sa see @ref is_number_unsigned() -- check if value is an unsigned integer - number - @sa see @ref is_number_float() -- check if value is a floating-point number - - @since version 1.0.0 - */ - constexpr bool is_number() const noexcept - { - return is_number_integer() || is_number_float(); - } - - /*! - @brief return whether value is an integer number - - This function returns true if and only if the JSON value is a signed or - unsigned integer number. This excludes floating-point values. - - @return `true` if type is an integer or unsigned integer number, `false` - otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_number_integer()` for all - JSON types.,is_number_integer} - - @sa see @ref is_number() -- check if value is a number - @sa see @ref is_number_unsigned() -- check if value is an unsigned integer - number - @sa see @ref is_number_float() -- check if value is a floating-point number - - @since version 1.0.0 - */ - constexpr bool is_number_integer() const noexcept - { - return m_type == value_t::number_integer || m_type == value_t::number_unsigned; - } - - /*! - @brief return whether value is an unsigned integer number - - This function returns true if and only if the JSON value is an unsigned - integer number. This excludes floating-point and signed integer values. - - @return `true` if type is an unsigned integer number, `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_number_unsigned()` for all - JSON types.,is_number_unsigned} - - @sa see @ref is_number() -- check if value is a number - @sa see @ref is_number_integer() -- check if value is an integer or unsigned - integer number - @sa see @ref is_number_float() -- check if value is a floating-point number - - @since version 2.0.0 - */ - constexpr bool is_number_unsigned() const noexcept - { - return m_type == value_t::number_unsigned; - } - - /*! - @brief return whether value is a floating-point number - - This function returns true if and only if the JSON value is a - floating-point number. This excludes signed and unsigned integer values. - - @return `true` if type is a floating-point number, `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_number_float()` for all - JSON types.,is_number_float} - - @sa see @ref is_number() -- check if value is number - @sa see @ref is_number_integer() -- check if value is an integer number - @sa see @ref is_number_unsigned() -- check if value is an unsigned integer - number - - @since version 1.0.0 - */ - constexpr bool is_number_float() const noexcept - { - return m_type == value_t::number_float; - } - - /*! - @brief return whether value is an object - - This function returns true if and only if the JSON value is an object. - - @return `true` if type is object, `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_object()` for all JSON - types.,is_object} - - @since version 1.0.0 - */ - constexpr bool is_object() const noexcept - { - return m_type == value_t::object; - } - - /*! - @brief return whether value is an array - - This function returns true if and only if the JSON value is an array. - - @return `true` if type is array, `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_array()` for all JSON - types.,is_array} - - @since version 1.0.0 - */ - constexpr bool is_array() const noexcept - { - return m_type == value_t::array; - } - - /*! - @brief return whether value is a string - - This function returns true if and only if the JSON value is a string. - - @return `true` if type is string, `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_string()` for all JSON - types.,is_string} - - @since version 1.0.0 - */ - constexpr bool is_string() const noexcept - { - return m_type == value_t::string; - } - - /*! - @brief return whether value is a binary array - - This function returns true if and only if the JSON value is a binary array. - - @return `true` if type is binary array, `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_binary()` for all JSON - types.,is_binary} - - @since version 3.8.0 - */ - constexpr bool is_binary() const noexcept - { - return m_type == value_t::binary; - } - - /*! - @brief return whether value is discarded - - This function returns true if and only if the JSON value was discarded - during parsing with a callback function (see @ref parser_callback_t). - - @note This function will always be `false` for JSON values after parsing. - That is, discarded values can only occur during parsing, but will be - removed when inside a structured value or replaced by null in other cases. - - @return `true` if type is discarded, `false` otherwise. - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies `is_discarded()` for all JSON - types.,is_discarded} - - @since version 1.0.0 - */ - constexpr bool is_discarded() const noexcept - { - return m_type == value_t::discarded; - } - - /*! - @brief return the type of the JSON value (implicit) - - Implicitly return the type of the JSON value as a value from the @ref - value_t enumeration. - - @return the type of the JSON value - - @complexity Constant. - - @exceptionsafety No-throw guarantee: this member function never throws - exceptions. - - @liveexample{The following code exemplifies the @ref value_t operator for - all JSON types.,operator__value_t} - - @sa see @ref type() -- return the type of the JSON value (explicit) - @sa see @ref type_name() -- return the type as string - - @since version 1.0.0 - */ - constexpr operator value_t() const noexcept - { - return m_type; - } - - /// @} - - private: - ////////////////// - // value access // - ////////////////// - - /// get a boolean (explicit) - boolean_t get_impl(boolean_t* /*unused*/) const - { - if (JSON_HEDLEY_LIKELY(is_boolean())) - { - return m_value.boolean; - } - - JSON_THROW(type_error::create(302, "type must be boolean, but is " + std::string(type_name()), *this)); - } - - /// get a pointer to the value (object) - object_t* get_impl_ptr(object_t* /*unused*/) noexcept - { - return is_object() ? m_value.object : nullptr; - } - - /// get a pointer to the value (object) - constexpr const object_t* get_impl_ptr(const object_t* /*unused*/) const noexcept - { - return is_object() ? m_value.object : nullptr; - } - - /// get a pointer to the value (array) - array_t* get_impl_ptr(array_t* /*unused*/) noexcept - { - return is_array() ? m_value.array : nullptr; - } - - /// get a pointer to the value (array) - constexpr const array_t* get_impl_ptr(const array_t* /*unused*/) const noexcept - { - return is_array() ? m_value.array : nullptr; - } - - /// get a pointer to the value (string) - string_t* get_impl_ptr(string_t* /*unused*/) noexcept - { - return is_string() ? m_value.string : nullptr; - } - - /// get a pointer to the value (string) - constexpr const string_t* get_impl_ptr(const string_t* /*unused*/) const noexcept - { - return is_string() ? m_value.string : nullptr; - } - - /// get a pointer to the value (boolean) - boolean_t* get_impl_ptr(boolean_t* /*unused*/) noexcept - { - return is_boolean() ? &m_value.boolean : nullptr; - } - - /// get a pointer to the value (boolean) - constexpr const boolean_t* get_impl_ptr(const boolean_t* /*unused*/) const noexcept - { - return is_boolean() ? &m_value.boolean : nullptr; - } - - /// get a pointer to the value (integer number) - number_integer_t* get_impl_ptr(number_integer_t* /*unused*/) noexcept - { - return is_number_integer() ? &m_value.number_integer : nullptr; - } - - /// get a pointer to the value (integer number) - constexpr const number_integer_t* get_impl_ptr(const number_integer_t* /*unused*/) const noexcept - { - return is_number_integer() ? &m_value.number_integer : nullptr; - } - - /// get a pointer to the value (unsigned number) - number_unsigned_t* get_impl_ptr(number_unsigned_t* /*unused*/) noexcept - { - return is_number_unsigned() ? &m_value.number_unsigned : nullptr; - } - - /// get a pointer to the value (unsigned number) - constexpr const number_unsigned_t* get_impl_ptr(const number_unsigned_t* /*unused*/) const noexcept - { - return is_number_unsigned() ? &m_value.number_unsigned : nullptr; - } - - /// get a pointer to the value (floating-point number) - number_float_t* get_impl_ptr(number_float_t* /*unused*/) noexcept - { - return is_number_float() ? &m_value.number_float : nullptr; - } - - /// get a pointer to the value (floating-point number) - constexpr const number_float_t* get_impl_ptr(const number_float_t* /*unused*/) const noexcept - { - return is_number_float() ? &m_value.number_float : nullptr; - } - - /// get a pointer to the value (binary) - binary_t* get_impl_ptr(binary_t* /*unused*/) noexcept - { - return is_binary() ? m_value.binary : nullptr; - } - - /// get a pointer to the value (binary) - constexpr const binary_t* get_impl_ptr(const binary_t* /*unused*/) const noexcept - { - return is_binary() ? m_value.binary : nullptr; - } - - /*! - @brief helper function to implement get_ref() - - This function helps to implement get_ref() without code duplication for - const and non-const overloads - - @tparam ThisType will be deduced as `basic_json` or `const basic_json` - - @throw type_error.303 if ReferenceType does not match underlying value - type of the current JSON - */ - template - static ReferenceType get_ref_impl(ThisType& obj) - { - // delegate the call to get_ptr<>() - auto* ptr = obj.template get_ptr::type>(); - - if (JSON_HEDLEY_LIKELY(ptr != nullptr)) - { - return *ptr; - } - - JSON_THROW(type_error::create(303, "incompatible ReferenceType for get_ref, actual type is " + std::string(obj.type_name()), obj)); - } - - public: - /// @name value access - /// Direct access to the stored value of a JSON value. - /// @{ - - /*! - @brief get a pointer value (implicit) - - Implicit pointer access to the internally stored JSON value. No copies are - made. - - @warning Writing data to the pointee of the result yields an undefined - state. - - @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref - object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, - @ref number_unsigned_t, or @ref number_float_t. Enforced by a static - assertion. - - @return pointer to the internally stored JSON value if the requested - pointer type @a PointerType fits to the JSON value; `nullptr` otherwise - - @complexity Constant. - - @liveexample{The example below shows how pointers to internal values of a - JSON value can be requested. Note that no type conversions are made and a - `nullptr` is returned if the value and the requested pointer type does not - match.,get_ptr} - - @since version 1.0.0 - */ - template::value, int>::type = 0> - auto get_ptr() noexcept -> decltype(std::declval().get_impl_ptr(std::declval())) - { - // delegate the call to get_impl_ptr<>() - return get_impl_ptr(static_cast(nullptr)); - } - - /*! - @brief get a pointer value (implicit) - @copydoc get_ptr() - */ - template < typename PointerType, typename std::enable_if < - std::is_pointer::value&& - std::is_const::type>::value, int >::type = 0 > - constexpr auto get_ptr() const noexcept -> decltype(std::declval().get_impl_ptr(std::declval())) - { - // delegate the call to get_impl_ptr<>() const - return get_impl_ptr(static_cast(nullptr)); - } - - private: - /*! - @brief get a value (explicit) - - Explicit type conversion between the JSON value and a compatible value - which is [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible) - and [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible). - The value is converted by calling the @ref json_serializer - `from_json()` method. - - The function is equivalent to executing - @code {.cpp} - ValueType ret; - JSONSerializer::from_json(*this, ret); - return ret; - @endcode - - This overloads is chosen if: - - @a ValueType is not @ref basic_json, - - @ref json_serializer has a `from_json()` method of the form - `void from_json(const basic_json&, ValueType&)`, and - - @ref json_serializer does not have a `from_json()` method of - the form `ValueType from_json(const basic_json&)` - - @tparam ValueType the returned value type - - @return copy of the JSON value, converted to @a ValueType - - @throw what @ref json_serializer `from_json()` method throws - - @liveexample{The example below shows several conversions from JSON values - to other types. There a few things to note: (1) Floating-point numbers can - be converted to integers\, (2) A JSON array can be converted to a standard - `std::vector`\, (3) A JSON object can be converted to C++ - associative containers such as `std::unordered_map`.,get__ValueType_const} - - @since version 2.1.0 - */ - template < typename ValueType, - detail::enable_if_t < - detail::is_default_constructible::value&& - detail::has_from_json::value, - int > = 0 > - ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept( - JSONSerializer::from_json(std::declval(), std::declval()))) - { - auto ret = ValueType(); - JSONSerializer::from_json(*this, ret); - return ret; - } - - /*! - @brief get a value (explicit); special case - - Explicit type conversion between the JSON value and a compatible value - which is **not** [CopyConstructible](https://en.cppreference.com/w/cpp/named_req/CopyConstructible) - and **not** [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible). - The value is converted by calling the @ref json_serializer - `from_json()` method. - - The function is equivalent to executing - @code {.cpp} - return JSONSerializer::from_json(*this); - @endcode - - This overloads is chosen if: - - @a ValueType is not @ref basic_json and - - @ref json_serializer has a `from_json()` method of the form - `ValueType from_json(const basic_json&)` - - @note If @ref json_serializer has both overloads of - `from_json()`, this one is chosen. - - @tparam ValueType the returned value type - - @return copy of the JSON value, converted to @a ValueType - - @throw what @ref json_serializer `from_json()` method throws - - @since version 2.1.0 - */ - template < typename ValueType, - detail::enable_if_t < - detail::has_non_default_from_json::value, - int > = 0 > - ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept( - JSONSerializer::from_json(std::declval()))) - { - return JSONSerializer::from_json(*this); - } - - /*! - @brief get special-case overload - - This overloads converts the current @ref basic_json in a different - @ref basic_json type - - @tparam BasicJsonType == @ref basic_json - - @return a copy of *this, converted into @a BasicJsonType - - @complexity Depending on the implementation of the called `from_json()` - method. - - @since version 3.2.0 - */ - template < typename BasicJsonType, - detail::enable_if_t < - detail::is_basic_json::value, - int > = 0 > - BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const - { - return *this; - } - - /*! - @brief get special-case overload - - This overloads avoids a lot of template boilerplate, it can be seen as the - identity method - - @tparam BasicJsonType == @ref basic_json - - @return a copy of *this - - @complexity Constant. - - @since version 2.1.0 - */ - template::value, - int> = 0> - basic_json get_impl(detail::priority_tag<3> /*unused*/) const - { - return *this; - } - - /*! - @brief get a pointer value (explicit) - @copydoc get() - */ - template::value, - int> = 0> - constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept - -> decltype(std::declval().template get_ptr()) - { - // delegate the call to get_ptr - return get_ptr(); - } - - public: - /*! - @brief get a (pointer) value (explicit) - - Performs explicit type conversion between the JSON value and a compatible value if required. - - - If the requested type is a pointer to the internally stored JSON value that pointer is returned. - No copies are made. - - - If the requested type is the current @ref basic_json, or a different @ref basic_json convertible - from the current @ref basic_json. - - - Otherwise the value is converted by calling the @ref json_serializer `from_json()` - method. - - @tparam ValueTypeCV the provided value type - @tparam ValueType the returned value type - - @return copy of the JSON value, converted to @tparam ValueType if necessary - - @throw what @ref json_serializer `from_json()` method throws if conversion is required - - @since version 2.1.0 - */ - template < typename ValueTypeCV, typename ValueType = detail::uncvref_t> -#if defined(JSON_HAS_CPP_14) - constexpr -#endif - auto get() const noexcept( - noexcept(std::declval().template get_impl(detail::priority_tag<4> {}))) - -> decltype(std::declval().template get_impl(detail::priority_tag<4> {})) - { - // we cannot static_assert on ValueTypeCV being non-const, because - // there is support for get(), which is why we - // still need the uncvref - static_assert(!std::is_reference::value, - "get() cannot be used with reference types, you might want to use get_ref()"); - return get_impl(detail::priority_tag<4> {}); - } - - /*! - @brief get a pointer value (explicit) - - Explicit pointer access to the internally stored JSON value. No copies are - made. - - @warning The pointer becomes invalid if the underlying JSON object - changes. - - @tparam PointerType pointer type; must be a pointer to @ref array_t, @ref - object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, - @ref number_unsigned_t, or @ref number_float_t. - - @return pointer to the internally stored JSON value if the requested - pointer type @a PointerType fits to the JSON value; `nullptr` otherwise - - @complexity Constant. - - @liveexample{The example below shows how pointers to internal values of a - JSON value can be requested. Note that no type conversions are made and a - `nullptr` is returned if the value and the requested pointer type does not - match.,get__PointerType} - - @sa see @ref get_ptr() for explicit pointer-member access - - @since version 1.0.0 - */ - template::value, int>::type = 0> - auto get() noexcept -> decltype(std::declval().template get_ptr()) - { - // delegate the call to get_ptr - return get_ptr(); - } - - /*! - @brief get a value (explicit) - - Explicit type conversion between the JSON value and a compatible value. - The value is filled into the input parameter by calling the @ref json_serializer - `from_json()` method. - - The function is equivalent to executing - @code {.cpp} - ValueType v; - JSONSerializer::from_json(*this, v); - @endcode - - This overloads is chosen if: - - @a ValueType is not @ref basic_json, - - @ref json_serializer has a `from_json()` method of the form - `void from_json(const basic_json&, ValueType&)`, and - - @tparam ValueType the input parameter type. - - @return the input parameter, allowing chaining calls. - - @throw what @ref json_serializer `from_json()` method throws - - @liveexample{The example below shows several conversions from JSON values - to other types. There a few things to note: (1) Floating-point numbers can - be converted to integers\, (2) A JSON array can be converted to a standard - `std::vector`\, (3) A JSON object can be converted to C++ - associative containers such as `std::unordered_map`.,get_to} - - @since version 3.3.0 - */ - template < typename ValueType, - detail::enable_if_t < - !detail::is_basic_json::value&& - detail::has_from_json::value, - int > = 0 > - ValueType & get_to(ValueType& v) const noexcept(noexcept( - JSONSerializer::from_json(std::declval(), v))) - { - JSONSerializer::from_json(*this, v); - return v; - } - - // specialization to allow to call get_to with a basic_json value - // see https://github.com/nlohmann/json/issues/2175 - template::value, - int> = 0> - ValueType & get_to(ValueType& v) const - { - v = *this; - return v; - } - - template < - typename T, std::size_t N, - typename Array = T (&)[N], // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - detail::enable_if_t < - detail::has_from_json::value, int > = 0 > - Array get_to(T (&v)[N]) const // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays) - noexcept(noexcept(JSONSerializer::from_json( - std::declval(), v))) - { - JSONSerializer::from_json(*this, v); - return v; - } - - /*! - @brief get a reference value (implicit) - - Implicit reference access to the internally stored JSON value. No copies - are made. - - @warning Writing data to the referee of the result yields an undefined - state. - - @tparam ReferenceType reference type; must be a reference to @ref array_t, - @ref object_t, @ref string_t, @ref boolean_t, @ref number_integer_t, or - @ref number_float_t. Enforced by static assertion. - - @return reference to the internally stored JSON value if the requested - reference type @a ReferenceType fits to the JSON value; throws - type_error.303 otherwise - - @throw type_error.303 in case passed type @a ReferenceType is incompatible - with the stored JSON value; see example below - - @complexity Constant. - - @liveexample{The example shows several calls to `get_ref()`.,get_ref} - - @since version 1.1.0 - */ - template::value, int>::type = 0> - ReferenceType get_ref() - { - // delegate call to get_ref_impl - return get_ref_impl(*this); - } - - /*! - @brief get a reference value (implicit) - @copydoc get_ref() - */ - template < typename ReferenceType, typename std::enable_if < - std::is_reference::value&& - std::is_const::type>::value, int >::type = 0 > - ReferenceType get_ref() const - { - // delegate call to get_ref_impl - return get_ref_impl(*this); - } - - /*! - @brief get a value (implicit) - - Implicit type conversion between the JSON value and a compatible value. - The call is realized by calling @ref get() const. - - @tparam ValueType non-pointer type compatible to the JSON value, for - instance `int` for JSON integer numbers, `bool` for JSON booleans, or - `std::vector` types for JSON arrays. The character type of @ref string_t - as well as an initializer list of this type is excluded to avoid - ambiguities as these types implicitly convert to `std::string`. - - @return copy of the JSON value, converted to type @a ValueType - - @throw type_error.302 in case passed type @a ValueType is incompatible - to the JSON value type (e.g., the JSON value is of type boolean, but a - string is requested); see example below - - @complexity Linear in the size of the JSON value. - - @liveexample{The example below shows several conversions from JSON values - to other types. There a few things to note: (1) Floating-point numbers can - be converted to integers\, (2) A JSON array can be converted to a standard - `std::vector`\, (3) A JSON object can be converted to C++ - associative containers such as `std::unordered_map`.,operator__ValueType} - - @since version 1.0.0 - */ - template < typename ValueType, typename std::enable_if < - detail::conjunction < - detail::negation>, - detail::negation>>, - detail::negation>, - detail::negation>, - detail::negation>>, - -#if defined(JSON_HAS_CPP_17) && (defined(__GNUC__) || (defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER <= 1914)) - detail::negation>, -#endif - detail::is_detected_lazy - >::value, int >::type = 0 > - JSON_EXPLICIT operator ValueType() const - { - // delegate the call to get<>() const - return get(); - } - - /*! - @return reference to the binary value - - @throw type_error.302 if the value is not binary - - @sa see @ref is_binary() to check if the value is binary - - @since version 3.8.0 - */ - binary_t& get_binary() - { - if (!is_binary()) - { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this)); - } - - return *get_ptr(); - } - - /// @copydoc get_binary() - const binary_t& get_binary() const - { - if (!is_binary()) - { - JSON_THROW(type_error::create(302, "type must be binary, but is " + std::string(type_name()), *this)); - } - - return *get_ptr(); - } - - /// @} - - - //////////////////// - // element access // - //////////////////// - - /// @name element access - /// Access to the JSON value. - /// @{ - - /*! - @brief access specified array element with bounds checking - - Returns a reference to the element at specified location @a idx, with - bounds checking. - - @param[in] idx index of the element to access - - @return reference to the element at index @a idx - - @throw type_error.304 if the JSON value is not an array; in this case, - calling `at` with an index makes no sense. See example below. - @throw out_of_range.401 if the index @a idx is out of range of the array; - that is, `idx >= size()`. See example below. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes in the JSON value. - - @complexity Constant. - - @since version 1.0.0 - - @liveexample{The example below shows how array elements can be read and - written using `at()`. It also demonstrates the different exceptions that - can be thrown.,at__size_type} - */ - reference at(size_type idx) - { - // at only works for arrays - if (JSON_HEDLEY_LIKELY(is_array())) - { - JSON_TRY - { - return set_parent(m_value.array->at(idx)); - } - JSON_CATCH (std::out_of_range&) - { - // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); - } - } - else - { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); - } - } - - /*! - @brief access specified array element with bounds checking - - Returns a const reference to the element at specified location @a idx, - with bounds checking. - - @param[in] idx index of the element to access - - @return const reference to the element at index @a idx - - @throw type_error.304 if the JSON value is not an array; in this case, - calling `at` with an index makes no sense. See example below. - @throw out_of_range.401 if the index @a idx is out of range of the array; - that is, `idx >= size()`. See example below. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes in the JSON value. - - @complexity Constant. - - @since version 1.0.0 - - @liveexample{The example below shows how array elements can be read using - `at()`. It also demonstrates the different exceptions that can be thrown., - at__size_type_const} - */ - const_reference at(size_type idx) const - { - // at only works for arrays - if (JSON_HEDLEY_LIKELY(is_array())) - { - JSON_TRY - { - return m_value.array->at(idx); - } - JSON_CATCH (std::out_of_range&) - { - // create better exception explanation - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); - } - } - else - { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); - } - } - - /*! - @brief access specified object element with bounds checking - - Returns a reference to the element at with specified key @a key, with - bounds checking. - - @param[in] key key of the element to access - - @return reference to the element at key @a key - - @throw type_error.304 if the JSON value is not an object; in this case, - calling `at` with a key makes no sense. See example below. - @throw out_of_range.403 if the key @a key is is not stored in the object; - that is, `find(key) == end()`. See example below. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes in the JSON value. - - @complexity Logarithmic in the size of the container. - - @sa see @ref operator[](const typename object_t::key_type&) for unchecked - access by reference - @sa see @ref value() for access by value with a default value - - @since version 1.0.0 - - @liveexample{The example below shows how object elements can be read and - written using `at()`. It also demonstrates the different exceptions that - can be thrown.,at__object_t_key_type} - */ - reference at(const typename object_t::key_type& key) - { - // at only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - JSON_TRY - { - return set_parent(m_value.object->at(key)); - } - JSON_CATCH (std::out_of_range&) - { - // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this)); - } - } - else - { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); - } - } - - /*! - @brief access specified object element with bounds checking - - Returns a const reference to the element at with specified key @a key, - with bounds checking. - - @param[in] key key of the element to access - - @return const reference to the element at key @a key - - @throw type_error.304 if the JSON value is not an object; in this case, - calling `at` with a key makes no sense. See example below. - @throw out_of_range.403 if the key @a key is is not stored in the object; - that is, `find(key) == end()`. See example below. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes in the JSON value. - - @complexity Logarithmic in the size of the container. - - @sa see @ref operator[](const typename object_t::key_type&) for unchecked - access by reference - @sa see @ref value() for access by value with a default value - - @since version 1.0.0 - - @liveexample{The example below shows how object elements can be read using - `at()`. It also demonstrates the different exceptions that can be thrown., - at__object_t_key_type_const} - */ - const_reference at(const typename object_t::key_type& key) const - { - // at only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - JSON_TRY - { - return m_value.object->at(key); - } - JSON_CATCH (std::out_of_range&) - { - // create better exception explanation - JSON_THROW(out_of_range::create(403, "key '" + key + "' not found", *this)); - } - } - else - { - JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this)); - } - } - - /*! - @brief access specified array element - - Returns a reference to the element at specified location @a idx. - - @note If @a idx is beyond the range of the array (i.e., `idx >= size()`), - then the array is silently filled up with `null` values to make `idx` a - valid reference to the last stored element. - - @param[in] idx index of the element to access - - @return reference to the element at index @a idx - - @throw type_error.305 if the JSON value is not an array or null; in that - cases, using the [] operator with an index makes no sense. - - @complexity Constant if @a idx is in the range of the array. Otherwise - linear in `idx - size()`. - - @liveexample{The example below shows how array elements can be read and - written using `[]` operator. Note the addition of `null` - values.,operatorarray__size_type} - - @since version 1.0.0 - */ - reference operator[](size_type idx) - { - // implicitly convert null value to an empty array - if (is_null()) - { - m_type = value_t::array; - m_value.array = create(); - assert_invariant(); - } - - // operator[] only works for arrays - if (JSON_HEDLEY_LIKELY(is_array())) - { - // fill up array with null values if given idx is outside range - if (idx >= m_value.array->size()) - { -#if JSON_DIAGNOSTICS - // remember array size & capacity before resizing - const auto old_size = m_value.array->size(); - const auto old_capacity = m_value.array->capacity(); -#endif - m_value.array->resize(idx + 1); - -#if JSON_DIAGNOSTICS - if (JSON_HEDLEY_UNLIKELY(m_value.array->capacity() != old_capacity)) - { - // capacity has changed: update all parents - set_parents(); - } - else - { - // set parent for values added above - set_parents(begin() + static_cast(old_size), static_cast(idx + 1 - old_size)); - } -#endif - assert_invariant(); - } - - return m_value.array->operator[](idx); - } - - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this)); - } - - /*! - @brief access specified array element - - Returns a const reference to the element at specified location @a idx. - - @param[in] idx index of the element to access - - @return const reference to the element at index @a idx - - @throw type_error.305 if the JSON value is not an array; in that case, - using the [] operator with an index makes no sense. - - @complexity Constant. - - @liveexample{The example below shows how array elements can be read using - the `[]` operator.,operatorarray__size_type_const} - - @since version 1.0.0 - */ - const_reference operator[](size_type idx) const - { - // const operator[] only works for arrays - if (JSON_HEDLEY_LIKELY(is_array())) - { - return m_value.array->operator[](idx); - } - - JSON_THROW(type_error::create(305, "cannot use operator[] with a numeric argument with " + std::string(type_name()), *this)); - } - - /*! - @brief access specified object element - - Returns a reference to the element at with specified key @a key. - - @note If @a key is not found in the object, then it is silently added to - the object and filled with a `null` value to make `key` a valid reference. - In case the value was `null` before, it is converted to an object. - - @param[in] key key of the element to access - - @return reference to the element at key @a key - - @throw type_error.305 if the JSON value is not an object or null; in that - cases, using the [] operator with a key makes no sense. - - @complexity Logarithmic in the size of the container. - - @liveexample{The example below shows how object elements can be read and - written using the `[]` operator.,operatorarray__key_type} - - @sa see @ref at(const typename object_t::key_type&) for access by reference - with range checking - @sa see @ref value() for access by value with a default value - - @since version 1.0.0 - */ - reference operator[](const typename object_t::key_type& key) - { - // implicitly convert null value to an empty object - if (is_null()) - { - m_type = value_t::object; - m_value.object = create(); - assert_invariant(); - } - - // operator[] only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - return set_parent(m_value.object->operator[](key)); - } - - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); - } - - /*! - @brief read-only access specified object element - - Returns a const reference to the element at with specified key @a key. No - bounds checking is performed. - - @warning If the element with key @a key does not exist, the behavior is - undefined. - - @param[in] key key of the element to access - - @return const reference to the element at key @a key - - @pre The element with key @a key must exist. **This precondition is - enforced with an assertion.** - - @throw type_error.305 if the JSON value is not an object; in that case, - using the [] operator with a key makes no sense. - - @complexity Logarithmic in the size of the container. - - @liveexample{The example below shows how object elements can be read using - the `[]` operator.,operatorarray__key_type_const} - - @sa see @ref at(const typename object_t::key_type&) for access by reference - with range checking - @sa see @ref value() for access by value with a default value - - @since version 1.0.0 - */ - const_reference operator[](const typename object_t::key_type& key) const - { - // const operator[] only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - JSON_ASSERT(m_value.object->find(key) != m_value.object->end()); - return m_value.object->find(key)->second; - } - - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); - } - - /*! - @brief access specified object element - - Returns a reference to the element at with specified key @a key. - - @note If @a key is not found in the object, then it is silently added to - the object and filled with a `null` value to make `key` a valid reference. - In case the value was `null` before, it is converted to an object. - - @param[in] key key of the element to access - - @return reference to the element at key @a key - - @throw type_error.305 if the JSON value is not an object or null; in that - cases, using the [] operator with a key makes no sense. - - @complexity Logarithmic in the size of the container. - - @liveexample{The example below shows how object elements can be read and - written using the `[]` operator.,operatorarray__key_type} - - @sa see @ref at(const typename object_t::key_type&) for access by reference - with range checking - @sa see @ref value() for access by value with a default value - - @since version 1.1.0 - */ - template - JSON_HEDLEY_NON_NULL(2) - reference operator[](T* key) - { - // implicitly convert null to object - if (is_null()) - { - m_type = value_t::object; - m_value = value_t::object; - assert_invariant(); - } - - // at only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - return set_parent(m_value.object->operator[](key)); - } - - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); - } - - /*! - @brief read-only access specified object element - - Returns a const reference to the element at with specified key @a key. No - bounds checking is performed. - - @warning If the element with key @a key does not exist, the behavior is - undefined. - - @param[in] key key of the element to access - - @return const reference to the element at key @a key - - @pre The element with key @a key must exist. **This precondition is - enforced with an assertion.** - - @throw type_error.305 if the JSON value is not an object; in that case, - using the [] operator with a key makes no sense. - - @complexity Logarithmic in the size of the container. - - @liveexample{The example below shows how object elements can be read using - the `[]` operator.,operatorarray__key_type_const} - - @sa see @ref at(const typename object_t::key_type&) for access by reference - with range checking - @sa see @ref value() for access by value with a default value - - @since version 1.1.0 - */ - template - JSON_HEDLEY_NON_NULL(2) - const_reference operator[](T* key) const - { - // at only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - JSON_ASSERT(m_value.object->find(key) != m_value.object->end()); - return m_value.object->find(key)->second; - } - - JSON_THROW(type_error::create(305, "cannot use operator[] with a string argument with " + std::string(type_name()), *this)); - } - - /*! - @brief access specified object element with default value - - Returns either a copy of an object's element at the specified key @a key - or a given default value if no element with key @a key exists. - - The function is basically equivalent to executing - @code {.cpp} - try { - return at(key); - } catch(out_of_range) { - return default_value; - } - @endcode - - @note Unlike @ref at(const typename object_t::key_type&), this function - does not throw if the given key @a key was not found. - - @note Unlike @ref operator[](const typename object_t::key_type& key), this - function does not implicitly add an element to the position defined by @a - key. This function is furthermore also applicable to const objects. - - @param[in] key key of the element to access - @param[in] default_value the value to return if @a key is not found - - @tparam ValueType type compatible to JSON values, for instance `int` for - JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for - JSON arrays. Note the type of the expected value at @a key and the default - value @a default_value must be compatible. - - @return copy of the element at key @a key or @a default_value if @a key - is not found - - @throw type_error.302 if @a default_value does not match the type of the - value at @a key - @throw type_error.306 if the JSON value is not an object; in that case, - using `value()` with a key makes no sense. - - @complexity Logarithmic in the size of the container. - - @liveexample{The example below shows how object elements can be queried - with a default value.,basic_json__value} - - @sa see @ref at(const typename object_t::key_type&) for access by reference - with range checking - @sa see @ref operator[](const typename object_t::key_type&) for unchecked - access by reference - - @since version 1.0.0 - */ - // using std::is_convertible in a std::enable_if will fail when using explicit conversions - template < class ValueType, typename std::enable_if < - detail::is_getable::value - && !std::is_same::value, int >::type = 0 > - ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const - { - // at only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - // if key is found, return value and given default value otherwise - const auto it = find(key); - if (it != end()) - { - return it->template get(); - } - - return default_value; - } - - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this)); - } - - /*! - @brief overload for a default value of type const char* - @copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const - */ - string_t value(const typename object_t::key_type& key, const char* default_value) const - { - return value(key, string_t(default_value)); - } - - /*! - @brief access specified object element via JSON Pointer with default value - - Returns either a copy of an object's element at the specified key @a key - or a given default value if no element with key @a key exists. - - The function is basically equivalent to executing - @code {.cpp} - try { - return at(ptr); - } catch(out_of_range) { - return default_value; - } - @endcode - - @note Unlike @ref at(const json_pointer&), this function does not throw - if the given key @a key was not found. - - @param[in] ptr a JSON pointer to the element to access - @param[in] default_value the value to return if @a ptr found no value - - @tparam ValueType type compatible to JSON values, for instance `int` for - JSON integer numbers, `bool` for JSON booleans, or `std::vector` types for - JSON arrays. Note the type of the expected value at @a key and the default - value @a default_value must be compatible. - - @return copy of the element at key @a key or @a default_value if @a key - is not found - - @throw type_error.302 if @a default_value does not match the type of the - value at @a ptr - @throw type_error.306 if the JSON value is not an object; in that case, - using `value()` with a key makes no sense. - - @complexity Logarithmic in the size of the container. - - @liveexample{The example below shows how object elements can be queried - with a default value.,basic_json__value_ptr} - - @sa see @ref operator[](const json_pointer&) for unchecked access by reference - - @since version 2.0.2 - */ - template::value, int>::type = 0> - ValueType value(const json_pointer& ptr, const ValueType& default_value) const - { - // at only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - // if pointer resolves a value, return it or use default value - JSON_TRY - { - return ptr.get_checked(this).template get(); - } - JSON_INTERNAL_CATCH (out_of_range&) - { - return default_value; - } - } - - JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name()), *this)); - } - - /*! - @brief overload for a default value of type const char* - @copydoc basic_json::value(const json_pointer&, ValueType) const - */ - JSON_HEDLEY_NON_NULL(3) - string_t value(const json_pointer& ptr, const char* default_value) const - { - return value(ptr, string_t(default_value)); - } - - /*! - @brief access the first element - - Returns a reference to the first element in the container. For a JSON - container `c`, the expression `c.front()` is equivalent to `*c.begin()`. - - @return In case of a structured type (array or object), a reference to the - first element is returned. In case of number, string, boolean, or binary - values, a reference to the value is returned. - - @complexity Constant. - - @pre The JSON value must not be `null` (would throw `std::out_of_range`) - or an empty array or object (undefined behavior, **guarded by - assertions**). - @post The JSON value remains unchanged. - - @throw invalid_iterator.214 when called on `null` value - - @liveexample{The following code shows an example for `front()`.,front} - - @sa see @ref back() -- access the last element - - @since version 1.0.0 - */ - reference front() - { - return *begin(); - } - - /*! - @copydoc basic_json::front() - */ - const_reference front() const - { - return *cbegin(); - } - - /*! - @brief access the last element - - Returns a reference to the last element in the container. For a JSON - container `c`, the expression `c.back()` is equivalent to - @code {.cpp} - auto tmp = c.end(); - --tmp; - return *tmp; - @endcode - - @return In case of a structured type (array or object), a reference to the - last element is returned. In case of number, string, boolean, or binary - values, a reference to the value is returned. - - @complexity Constant. - - @pre The JSON value must not be `null` (would throw `std::out_of_range`) - or an empty array or object (undefined behavior, **guarded by - assertions**). - @post The JSON value remains unchanged. - - @throw invalid_iterator.214 when called on a `null` value. See example - below. - - @liveexample{The following code shows an example for `back()`.,back} - - @sa see @ref front() -- access the first element - - @since version 1.0.0 - */ - reference back() - { - auto tmp = end(); - --tmp; - return *tmp; - } - - /*! - @copydoc basic_json::back() - */ - const_reference back() const - { - auto tmp = cend(); - --tmp; - return *tmp; - } - - /*! - @brief remove element given an iterator - - Removes the element specified by iterator @a pos. The iterator @a pos must - be valid and dereferenceable. Thus the `end()` iterator (which is valid, - but is not dereferenceable) cannot be used as a value for @a pos. - - If called on a primitive type other than `null`, the resulting JSON value - will be `null`. - - @param[in] pos iterator to the element to remove - @return Iterator following the last removed element. If the iterator @a - pos refers to the last element, the `end()` iterator is returned. - - @tparam IteratorType an @ref iterator or @ref const_iterator - - @post Invalidates iterators and references at or after the point of the - erase, including the `end()` iterator. - - @throw type_error.307 if called on a `null` value; example: `"cannot use - erase() with null"` - @throw invalid_iterator.202 if called on an iterator which does not belong - to the current JSON value; example: `"iterator does not fit current - value"` - @throw invalid_iterator.205 if called on a primitive type with invalid - iterator (i.e., any iterator which is not `begin()`); example: `"iterator - out of range"` - - @complexity The complexity depends on the type: - - objects: amortized constant - - arrays: linear in distance between @a pos and the end of the container - - strings and binary: linear in the length of the member - - other types: constant - - @liveexample{The example shows the result of `erase()` for different JSON - types.,erase__IteratorType} - - @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in - the given range - @sa see @ref erase(const typename object_t::key_type&) -- removes the element - from an object at the given key - @sa see @ref erase(const size_type) -- removes the element from an array at - the given index - - @since version 1.0.0 - */ - template < class IteratorType, typename std::enable_if < - std::is_same::value || - std::is_same::value, int >::type - = 0 > - IteratorType erase(IteratorType pos) - { - // make sure iterator fits the current value - if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) - { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); - } - - IteratorType result = end(); - - switch (m_type) - { - case value_t::boolean: - case value_t::number_float: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::string: - case value_t::binary: - { - if (JSON_HEDLEY_UNLIKELY(!pos.m_it.primitive_iterator.is_begin())) - { - JSON_THROW(invalid_iterator::create(205, "iterator out of range", *this)); - } - - if (is_string()) - { - AllocatorType alloc; - std::allocator_traits::destroy(alloc, m_value.string); - std::allocator_traits::deallocate(alloc, m_value.string, 1); - m_value.string = nullptr; - } - else if (is_binary()) - { - AllocatorType alloc; - std::allocator_traits::destroy(alloc, m_value.binary); - std::allocator_traits::deallocate(alloc, m_value.binary, 1); - m_value.binary = nullptr; - } - - m_type = value_t::null; - assert_invariant(); - break; - } - - case value_t::object: - { - result.m_it.object_iterator = m_value.object->erase(pos.m_it.object_iterator); - break; - } - - case value_t::array: - { - result.m_it.array_iterator = m_value.array->erase(pos.m_it.array_iterator); - break; - } - - case value_t::null: - case value_t::discarded: - default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); - } - - return result; - } - - /*! - @brief remove elements given an iterator range - - Removes the element specified by the range `[first; last)`. The iterator - @a first does not need to be dereferenceable if `first == last`: erasing - an empty range is a no-op. - - If called on a primitive type other than `null`, the resulting JSON value - will be `null`. - - @param[in] first iterator to the beginning of the range to remove - @param[in] last iterator past the end of the range to remove - @return Iterator following the last removed element. If the iterator @a - second refers to the last element, the `end()` iterator is returned. - - @tparam IteratorType an @ref iterator or @ref const_iterator - - @post Invalidates iterators and references at or after the point of the - erase, including the `end()` iterator. - - @throw type_error.307 if called on a `null` value; example: `"cannot use - erase() with null"` - @throw invalid_iterator.203 if called on iterators which does not belong - to the current JSON value; example: `"iterators do not fit current value"` - @throw invalid_iterator.204 if called on a primitive type with invalid - iterators (i.e., if `first != begin()` and `last != end()`); example: - `"iterators out of range"` - - @complexity The complexity depends on the type: - - objects: `log(size()) + std::distance(first, last)` - - arrays: linear in the distance between @a first and @a last, plus linear - in the distance between @a last and end of the container - - strings and binary: linear in the length of the member - - other types: constant - - @liveexample{The example shows the result of `erase()` for different JSON - types.,erase__IteratorType_IteratorType} - - @sa see @ref erase(IteratorType) -- removes the element at a given position - @sa see @ref erase(const typename object_t::key_type&) -- removes the element - from an object at the given key - @sa see @ref erase(const size_type) -- removes the element from an array at - the given index - - @since version 1.0.0 - */ - template < class IteratorType, typename std::enable_if < - std::is_same::value || - std::is_same::value, int >::type - = 0 > - IteratorType erase(IteratorType first, IteratorType last) - { - // make sure iterator fits the current value - if (JSON_HEDLEY_UNLIKELY(this != first.m_object || this != last.m_object)) - { - JSON_THROW(invalid_iterator::create(203, "iterators do not fit current value", *this)); - } - - IteratorType result = end(); - - switch (m_type) - { - case value_t::boolean: - case value_t::number_float: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::string: - case value_t::binary: - { - if (JSON_HEDLEY_LIKELY(!first.m_it.primitive_iterator.is_begin() - || !last.m_it.primitive_iterator.is_end())) - { - JSON_THROW(invalid_iterator::create(204, "iterators out of range", *this)); - } - - if (is_string()) - { - AllocatorType alloc; - std::allocator_traits::destroy(alloc, m_value.string); - std::allocator_traits::deallocate(alloc, m_value.string, 1); - m_value.string = nullptr; - } - else if (is_binary()) - { - AllocatorType alloc; - std::allocator_traits::destroy(alloc, m_value.binary); - std::allocator_traits::deallocate(alloc, m_value.binary, 1); - m_value.binary = nullptr; - } - - m_type = value_t::null; - assert_invariant(); - break; - } - - case value_t::object: - { - result.m_it.object_iterator = m_value.object->erase(first.m_it.object_iterator, - last.m_it.object_iterator); - break; - } - - case value_t::array: - { - result.m_it.array_iterator = m_value.array->erase(first.m_it.array_iterator, - last.m_it.array_iterator); - break; - } - - case value_t::null: - case value_t::discarded: - default: - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); - } - - return result; - } - - /*! - @brief remove element from a JSON object given a key - - Removes elements from a JSON object with the key value @a key. - - @param[in] key value of the elements to remove - - @return Number of elements removed. If @a ObjectType is the default - `std::map` type, the return value will always be `0` (@a key was not - found) or `1` (@a key was found). - - @post References and iterators to the erased elements are invalidated. - Other references and iterators are not affected. - - @throw type_error.307 when called on a type other than JSON object; - example: `"cannot use erase() with null"` - - @complexity `log(size()) + count(key)` - - @liveexample{The example shows the effect of `erase()`.,erase__key_type} - - @sa see @ref erase(IteratorType) -- removes the element at a given position - @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in - the given range - @sa see @ref erase(const size_type) -- removes the element from an array at - the given index - - @since version 1.0.0 - */ - size_type erase(const typename object_t::key_type& key) - { - // this erase only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - return m_value.object->erase(key); - } - - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); - } - - /*! - @brief remove element from a JSON array given an index - - Removes element from a JSON array at the index @a idx. - - @param[in] idx index of the element to remove - - @throw type_error.307 when called on a type other than JSON object; - example: `"cannot use erase() with null"` - @throw out_of_range.401 when `idx >= size()`; example: `"array index 17 - is out of range"` - - @complexity Linear in distance between @a idx and the end of the container. - - @liveexample{The example shows the effect of `erase()`.,erase__size_type} - - @sa see @ref erase(IteratorType) -- removes the element at a given position - @sa see @ref erase(IteratorType, IteratorType) -- removes the elements in - the given range - @sa see @ref erase(const typename object_t::key_type&) -- removes the element - from an object at the given key - - @since version 1.0.0 - */ - void erase(const size_type idx) - { - // this erase only works for arrays - if (JSON_HEDLEY_LIKELY(is_array())) - { - if (JSON_HEDLEY_UNLIKELY(idx >= size())) - { - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this)); - } - - m_value.array->erase(m_value.array->begin() + static_cast(idx)); - } - else - { - JSON_THROW(type_error::create(307, "cannot use erase() with " + std::string(type_name()), *this)); - } - } - - /// @} - - - //////////// - // lookup // - //////////// - - /// @name lookup - /// @{ - - /*! - @brief find an element in a JSON object - - Finds an element in a JSON object with key equivalent to @a key. If the - element is not found or the JSON value is not an object, end() is - returned. - - @note This method always returns @ref end() when executed on a JSON type - that is not an object. - - @param[in] key key value of the element to search for. - - @return Iterator to an element with key equivalent to @a key. If no such - element is found or the JSON value is not an object, past-the-end (see - @ref end()) iterator is returned. - - @complexity Logarithmic in the size of the JSON object. - - @liveexample{The example shows how `find()` is used.,find__key_type} - - @sa see @ref contains(KeyT&&) const -- checks whether a key exists - - @since version 1.0.0 - */ - template - iterator find(KeyT&& key) - { - auto result = end(); - - if (is_object()) - { - result.m_it.object_iterator = m_value.object->find(std::forward(key)); - } - - return result; - } - - /*! - @brief find an element in a JSON object - @copydoc find(KeyT&&) - */ - template - const_iterator find(KeyT&& key) const - { - auto result = cend(); - - if (is_object()) - { - result.m_it.object_iterator = m_value.object->find(std::forward(key)); - } - - return result; - } - - /*! - @brief returns the number of occurrences of a key in a JSON object - - Returns the number of elements with key @a key. If ObjectType is the - default `std::map` type, the return value will always be `0` (@a key was - not found) or `1` (@a key was found). - - @note This method always returns `0` when executed on a JSON type that is - not an object. - - @param[in] key key value of the element to count - - @return Number of elements with key @a key. If the JSON value is not an - object, the return value will be `0`. - - @complexity Logarithmic in the size of the JSON object. - - @liveexample{The example shows how `count()` is used.,count} - - @since version 1.0.0 - */ - template - size_type count(KeyT&& key) const - { - // return 0 for all nonobject types - return is_object() ? m_value.object->count(std::forward(key)) : 0; - } - - /*! - @brief check the existence of an element in a JSON object - - Check whether an element exists in a JSON object with key equivalent to - @a key. If the element is not found or the JSON value is not an object, - false is returned. - - @note This method always returns false when executed on a JSON type - that is not an object. - - @param[in] key key value to check its existence. - - @return true if an element with specified @a key exists. If no such - element with such key is found or the JSON value is not an object, - false is returned. - - @complexity Logarithmic in the size of the JSON object. - - @liveexample{The following code shows an example for `contains()`.,contains} - - @sa see @ref find(KeyT&&) -- returns an iterator to an object element - @sa see @ref contains(const json_pointer&) const -- checks the existence for a JSON pointer - - @since version 3.6.0 - */ - template < typename KeyT, typename std::enable_if < - !std::is_same::type, json_pointer>::value, int >::type = 0 > - bool contains(KeyT && key) const - { - return is_object() && m_value.object->find(std::forward(key)) != m_value.object->end(); - } - - /*! - @brief check the existence of an element in a JSON object given a JSON pointer - - Check whether the given JSON pointer @a ptr can be resolved in the current - JSON value. - - @note This method can be executed on any JSON value type. - - @param[in] ptr JSON pointer to check its existence. - - @return true if the JSON pointer can be resolved to a stored value, false - otherwise. - - @post If `j.contains(ptr)` returns true, it is safe to call `j[ptr]`. - - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - - @complexity Logarithmic in the size of the JSON object. - - @liveexample{The following code shows an example for `contains()`.,contains_json_pointer} - - @sa see @ref contains(KeyT &&) const -- checks the existence of a key - - @since version 3.7.0 - */ - bool contains(const json_pointer& ptr) const - { - return ptr.contains(this); - } - - /// @} - - - /////////////// - // iterators // - /////////////// - - /// @name iterators - /// @{ - - /*! - @brief returns an iterator to the first element - - Returns an iterator to the first element. - - @image html range-begin-end.svg "Illustration from cppreference.com" - - @return iterator to the first element - - @complexity Constant. - - @requirement This function helps `basic_json` satisfying the - [Container](https://en.cppreference.com/w/cpp/named_req/Container) - requirements: - - The complexity is constant. - - @liveexample{The following code shows an example for `begin()`.,begin} - - @sa see @ref cbegin() -- returns a const iterator to the beginning - @sa see @ref end() -- returns an iterator to the end - @sa see @ref cend() -- returns a const iterator to the end - - @since version 1.0.0 - */ - iterator begin() noexcept - { - iterator result(this); - result.set_begin(); - return result; - } - - /*! - @copydoc basic_json::cbegin() - */ - const_iterator begin() const noexcept - { - return cbegin(); - } - - /*! - @brief returns a const iterator to the first element - - Returns a const iterator to the first element. - - @image html range-begin-end.svg "Illustration from cppreference.com" - - @return const iterator to the first element - - @complexity Constant. - - @requirement This function helps `basic_json` satisfying the - [Container](https://en.cppreference.com/w/cpp/named_req/Container) - requirements: - - The complexity is constant. - - Has the semantics of `const_cast(*this).begin()`. - - @liveexample{The following code shows an example for `cbegin()`.,cbegin} - - @sa see @ref begin() -- returns an iterator to the beginning - @sa see @ref end() -- returns an iterator to the end - @sa see @ref cend() -- returns a const iterator to the end - - @since version 1.0.0 - */ - const_iterator cbegin() const noexcept - { - const_iterator result(this); - result.set_begin(); - return result; - } - - /*! - @brief returns an iterator to one past the last element - - Returns an iterator to one past the last element. - - @image html range-begin-end.svg "Illustration from cppreference.com" - - @return iterator one past the last element - - @complexity Constant. - - @requirement This function helps `basic_json` satisfying the - [Container](https://en.cppreference.com/w/cpp/named_req/Container) - requirements: - - The complexity is constant. - - @liveexample{The following code shows an example for `end()`.,end} - - @sa see @ref cend() -- returns a const iterator to the end - @sa see @ref begin() -- returns an iterator to the beginning - @sa see @ref cbegin() -- returns a const iterator to the beginning - - @since version 1.0.0 - */ - iterator end() noexcept - { - iterator result(this); - result.set_end(); - return result; - } - - /*! - @copydoc basic_json::cend() - */ - const_iterator end() const noexcept - { - return cend(); - } - - /*! - @brief returns a const iterator to one past the last element - - Returns a const iterator to one past the last element. - - @image html range-begin-end.svg "Illustration from cppreference.com" - - @return const iterator one past the last element - - @complexity Constant. - - @requirement This function helps `basic_json` satisfying the - [Container](https://en.cppreference.com/w/cpp/named_req/Container) - requirements: - - The complexity is constant. - - Has the semantics of `const_cast(*this).end()`. - - @liveexample{The following code shows an example for `cend()`.,cend} - - @sa see @ref end() -- returns an iterator to the end - @sa see @ref begin() -- returns an iterator to the beginning - @sa see @ref cbegin() -- returns a const iterator to the beginning - - @since version 1.0.0 - */ - const_iterator cend() const noexcept - { - const_iterator result(this); - result.set_end(); - return result; - } - - /*! - @brief returns an iterator to the reverse-beginning - - Returns an iterator to the reverse-beginning; that is, the last element. - - @image html range-rbegin-rend.svg "Illustration from cppreference.com" - - @complexity Constant. - - @requirement This function helps `basic_json` satisfying the - [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer) - requirements: - - The complexity is constant. - - Has the semantics of `reverse_iterator(end())`. - - @liveexample{The following code shows an example for `rbegin()`.,rbegin} - - @sa see @ref crbegin() -- returns a const reverse iterator to the beginning - @sa see @ref rend() -- returns a reverse iterator to the end - @sa see @ref crend() -- returns a const reverse iterator to the end - - @since version 1.0.0 - */ - reverse_iterator rbegin() noexcept - { - return reverse_iterator(end()); - } - - /*! - @copydoc basic_json::crbegin() - */ - const_reverse_iterator rbegin() const noexcept - { - return crbegin(); - } - - /*! - @brief returns an iterator to the reverse-end - - Returns an iterator to the reverse-end; that is, one before the first - element. - - @image html range-rbegin-rend.svg "Illustration from cppreference.com" - - @complexity Constant. - - @requirement This function helps `basic_json` satisfying the - [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer) - requirements: - - The complexity is constant. - - Has the semantics of `reverse_iterator(begin())`. - - @liveexample{The following code shows an example for `rend()`.,rend} - - @sa see @ref crend() -- returns a const reverse iterator to the end - @sa see @ref rbegin() -- returns a reverse iterator to the beginning - @sa see @ref crbegin() -- returns a const reverse iterator to the beginning - - @since version 1.0.0 - */ - reverse_iterator rend() noexcept - { - return reverse_iterator(begin()); - } - - /*! - @copydoc basic_json::crend() - */ - const_reverse_iterator rend() const noexcept - { - return crend(); - } - - /*! - @brief returns a const reverse iterator to the last element - - Returns a const iterator to the reverse-beginning; that is, the last - element. - - @image html range-rbegin-rend.svg "Illustration from cppreference.com" - - @complexity Constant. - - @requirement This function helps `basic_json` satisfying the - [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer) - requirements: - - The complexity is constant. - - Has the semantics of `const_cast(*this).rbegin()`. - - @liveexample{The following code shows an example for `crbegin()`.,crbegin} - - @sa see @ref rbegin() -- returns a reverse iterator to the beginning - @sa see @ref rend() -- returns a reverse iterator to the end - @sa see @ref crend() -- returns a const reverse iterator to the end - - @since version 1.0.0 - */ - const_reverse_iterator crbegin() const noexcept - { - return const_reverse_iterator(cend()); - } - - /*! - @brief returns a const reverse iterator to one before the first - - Returns a const reverse iterator to the reverse-end; that is, one before - the first element. - - @image html range-rbegin-rend.svg "Illustration from cppreference.com" - - @complexity Constant. - - @requirement This function helps `basic_json` satisfying the - [ReversibleContainer](https://en.cppreference.com/w/cpp/named_req/ReversibleContainer) - requirements: - - The complexity is constant. - - Has the semantics of `const_cast(*this).rend()`. - - @liveexample{The following code shows an example for `crend()`.,crend} - - @sa see @ref rend() -- returns a reverse iterator to the end - @sa see @ref rbegin() -- returns a reverse iterator to the beginning - @sa see @ref crbegin() -- returns a const reverse iterator to the beginning - - @since version 1.0.0 - */ - const_reverse_iterator crend() const noexcept - { - return const_reverse_iterator(cbegin()); - } - - public: - /*! - @brief wrapper to access iterator member functions in range-based for - - This function allows to access @ref iterator::key() and @ref - iterator::value() during range-based for loops. In these loops, a - reference to the JSON values is returned, so there is no access to the - underlying iterator. - - For loop without iterator_wrapper: - - @code{cpp} - for (auto it = j_object.begin(); it != j_object.end(); ++it) - { - std::cout << "key: " << it.key() << ", value:" << it.value() << '\n'; - } - @endcode - - Range-based for loop without iterator proxy: - - @code{cpp} - for (auto it : j_object) - { - // "it" is of type json::reference and has no key() member - std::cout << "value: " << it << '\n'; - } - @endcode - - Range-based for loop with iterator proxy: - - @code{cpp} - for (auto it : json::iterator_wrapper(j_object)) - { - std::cout << "key: " << it.key() << ", value:" << it.value() << '\n'; - } - @endcode - - @note When iterating over an array, `key()` will return the index of the - element as string (see example). - - @param[in] ref reference to a JSON value - @return iteration proxy object wrapping @a ref with an interface to use in - range-based for loops - - @liveexample{The following code shows how the wrapper is used,iterator_wrapper} - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes in the JSON value. - - @complexity Constant. - - @note The name of this function is not yet final and may change in the - future. - - @deprecated This stream operator is deprecated and will be removed in - future 4.0.0 of the library. Please use @ref items() instead; - that is, replace `json::iterator_wrapper(j)` with `j.items()`. - */ - JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items()) - static iteration_proxy iterator_wrapper(reference ref) noexcept - { - return ref.items(); - } - - /*! - @copydoc iterator_wrapper(reference) - */ - JSON_HEDLEY_DEPRECATED_FOR(3.1.0, items()) - static iteration_proxy iterator_wrapper(const_reference ref) noexcept - { - return ref.items(); - } - - /*! - @brief helper to access iterator member functions in range-based for - - This function allows to access @ref iterator::key() and @ref - iterator::value() during range-based for loops. In these loops, a - reference to the JSON values is returned, so there is no access to the - underlying iterator. - - For loop without `items()` function: - - @code{cpp} - for (auto it = j_object.begin(); it != j_object.end(); ++it) - { - std::cout << "key: " << it.key() << ", value:" << it.value() << '\n'; - } - @endcode - - Range-based for loop without `items()` function: - - @code{cpp} - for (auto it : j_object) - { - // "it" is of type json::reference and has no key() member - std::cout << "value: " << it << '\n'; - } - @endcode - - Range-based for loop with `items()` function: - - @code{cpp} - for (auto& el : j_object.items()) - { - std::cout << "key: " << el.key() << ", value:" << el.value() << '\n'; - } - @endcode - - The `items()` function also allows to use - [structured bindings](https://en.cppreference.com/w/cpp/language/structured_binding) - (C++17): - - @code{cpp} - for (auto& [key, val] : j_object.items()) - { - std::cout << "key: " << key << ", value:" << val << '\n'; - } - @endcode - - @note When iterating over an array, `key()` will return the index of the - element as string (see example). For primitive types (e.g., numbers), - `key()` returns an empty string. - - @warning Using `items()` on temporary objects is dangerous. Make sure the - object's lifetime exeeds the iteration. See - for more - information. - - @return iteration proxy object wrapping @a ref with an interface to use in - range-based for loops - - @liveexample{The following code shows how the function is used.,items} - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes in the JSON value. - - @complexity Constant. - - @since version 3.1.0, structured bindings support since 3.5.0. - */ - iteration_proxy items() noexcept - { - return iteration_proxy(*this); - } - - /*! - @copydoc items() - */ - iteration_proxy items() const noexcept - { - return iteration_proxy(*this); - } - - /// @} - - - ////////////// - // capacity // - ////////////// - - /// @name capacity - /// @{ - - /*! - @brief checks whether the container is empty. - - Checks if a JSON value has no elements (i.e. whether its @ref size is `0`). - - @return The return value depends on the different types and is - defined as follows: - Value type | return value - ----------- | ------------- - null | `true` - boolean | `false` - string | `false` - number | `false` - binary | `false` - object | result of function `object_t::empty()` - array | result of function `array_t::empty()` - - @liveexample{The following code uses `empty()` to check if a JSON - object contains any elements.,empty} - - @complexity Constant, as long as @ref array_t and @ref object_t satisfy - the Container concept; that is, their `empty()` functions have constant - complexity. - - @iterators No changes. - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @note This function does not return whether a string stored as JSON value - is empty - it returns whether the JSON container itself is empty which is - false in the case of a string. - - @requirement This function helps `basic_json` satisfying the - [Container](https://en.cppreference.com/w/cpp/named_req/Container) - requirements: - - The complexity is constant. - - Has the semantics of `begin() == end()`. - - @sa see @ref size() -- returns the number of elements - - @since version 1.0.0 - */ - bool empty() const noexcept - { - switch (m_type) - { - case value_t::null: - { - // null values are empty - return true; - } - - case value_t::array: - { - // delegate call to array_t::empty() - return m_value.array->empty(); - } - - case value_t::object: - { - // delegate call to object_t::empty() - return m_value.object->empty(); - } - - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - // all other types are nonempty - return false; - } - } - } - - /*! - @brief returns the number of elements - - Returns the number of elements in a JSON value. - - @return The return value depends on the different types and is - defined as follows: - Value type | return value - ----------- | ------------- - null | `0` - boolean | `1` - string | `1` - number | `1` - binary | `1` - object | result of function object_t::size() - array | result of function array_t::size() - - @liveexample{The following code calls `size()` on the different value - types.,size} - - @complexity Constant, as long as @ref array_t and @ref object_t satisfy - the Container concept; that is, their size() functions have constant - complexity. - - @iterators No changes. - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @note This function does not return the length of a string stored as JSON - value - it returns the number of elements in the JSON value which is 1 in - the case of a string. - - @requirement This function helps `basic_json` satisfying the - [Container](https://en.cppreference.com/w/cpp/named_req/Container) - requirements: - - The complexity is constant. - - Has the semantics of `std::distance(begin(), end())`. - - @sa see @ref empty() -- checks whether the container is empty - @sa see @ref max_size() -- returns the maximal number of elements - - @since version 1.0.0 - */ - size_type size() const noexcept - { - switch (m_type) - { - case value_t::null: - { - // null values are empty - return 0; - } - - case value_t::array: - { - // delegate call to array_t::size() - return m_value.array->size(); - } - - case value_t::object: - { - // delegate call to object_t::size() - return m_value.object->size(); - } - - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - // all other types have size 1 - return 1; - } - } - } - - /*! - @brief returns the maximum possible number of elements - - Returns the maximum number of elements a JSON value is able to hold due to - system or library implementation limitations, i.e. `std::distance(begin(), - end())` for the JSON value. - - @return The return value depends on the different types and is - defined as follows: - Value type | return value - ----------- | ------------- - null | `0` (same as `size()`) - boolean | `1` (same as `size()`) - string | `1` (same as `size()`) - number | `1` (same as `size()`) - binary | `1` (same as `size()`) - object | result of function `object_t::max_size()` - array | result of function `array_t::max_size()` - - @liveexample{The following code calls `max_size()` on the different value - types. Note the output is implementation specific.,max_size} - - @complexity Constant, as long as @ref array_t and @ref object_t satisfy - the Container concept; that is, their `max_size()` functions have constant - complexity. - - @iterators No changes. - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @requirement This function helps `basic_json` satisfying the - [Container](https://en.cppreference.com/w/cpp/named_req/Container) - requirements: - - The complexity is constant. - - Has the semantics of returning `b.size()` where `b` is the largest - possible JSON value. - - @sa see @ref size() -- returns the number of elements - - @since version 1.0.0 - */ - size_type max_size() const noexcept - { - switch (m_type) - { - case value_t::array: - { - // delegate call to array_t::max_size() - return m_value.array->max_size(); - } - - case value_t::object: - { - // delegate call to object_t::max_size() - return m_value.object->max_size(); - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - // all other types have max_size() == size() - return size(); - } - } - } - - /// @} - - - /////////////// - // modifiers // - /////////////// - - /// @name modifiers - /// @{ - - /*! - @brief clears the contents - - Clears the content of a JSON value and resets it to the default value as - if @ref basic_json(value_t) would have been called with the current value - type from @ref type(): - - Value type | initial value - ----------- | ------------- - null | `null` - boolean | `false` - string | `""` - number | `0` - binary | An empty byte vector - object | `{}` - array | `[]` - - @post Has the same effect as calling - @code {.cpp} - *this = basic_json(type()); - @endcode - - @liveexample{The example below shows the effect of `clear()` to different - JSON types.,clear} - - @complexity Linear in the size of the JSON value. - - @iterators All iterators, pointers and references related to this container - are invalidated. - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @sa see @ref basic_json(value_t) -- constructor that creates an object with the - same value than calling `clear()` - - @since version 1.0.0 - */ - void clear() noexcept - { - switch (m_type) - { - case value_t::number_integer: - { - m_value.number_integer = 0; - break; - } - - case value_t::number_unsigned: - { - m_value.number_unsigned = 0; - break; - } - - case value_t::number_float: - { - m_value.number_float = 0.0; - break; - } - - case value_t::boolean: - { - m_value.boolean = false; - break; - } - - case value_t::string: - { - m_value.string->clear(); - break; - } - - case value_t::binary: - { - m_value.binary->clear(); - break; - } - - case value_t::array: - { - m_value.array->clear(); - break; - } - - case value_t::object: - { - m_value.object->clear(); - break; - } - - case value_t::null: - case value_t::discarded: - default: - break; - } - } - - /*! - @brief add an object to an array - - Appends the given element @a val to the end of the JSON value. If the - function is called on a JSON null value, an empty array is created before - appending @a val. - - @param[in] val the value to add to the JSON array - - @throw type_error.308 when called on a type other than JSON array or - null; example: `"cannot use push_back() with number"` - - @complexity Amortized constant. - - @liveexample{The example shows how `push_back()` and `+=` can be used to - add elements to a JSON array. Note how the `null` value was silently - converted to a JSON array.,push_back} - - @since version 1.0.0 - */ - void push_back(basic_json&& val) - { - // push_back only works for null objects or arrays - if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) - { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); - } - - // transform null object into an array - if (is_null()) - { - m_type = value_t::array; - m_value = value_t::array; - assert_invariant(); - } - - // add element to array (move semantics) - const auto old_capacity = m_value.array->capacity(); - m_value.array->push_back(std::move(val)); - set_parent(m_value.array->back(), old_capacity); - // if val is moved from, basic_json move constructor marks it null so we do not call the destructor - } - - /*! - @brief add an object to an array - @copydoc push_back(basic_json&&) - */ - reference operator+=(basic_json&& val) - { - push_back(std::move(val)); - return *this; - } - - /*! - @brief add an object to an array - @copydoc push_back(basic_json&&) - */ - void push_back(const basic_json& val) - { - // push_back only works for null objects or arrays - if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) - { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); - } - - // transform null object into an array - if (is_null()) - { - m_type = value_t::array; - m_value = value_t::array; - assert_invariant(); - } - - // add element to array - const auto old_capacity = m_value.array->capacity(); - m_value.array->push_back(val); - set_parent(m_value.array->back(), old_capacity); - } - - /*! - @brief add an object to an array - @copydoc push_back(basic_json&&) - */ - reference operator+=(const basic_json& val) - { - push_back(val); - return *this; - } - - /*! - @brief add an object to an object - - Inserts the given element @a val to the JSON object. If the function is - called on a JSON null value, an empty object is created before inserting - @a val. - - @param[in] val the value to add to the JSON object - - @throw type_error.308 when called on a type other than JSON object or - null; example: `"cannot use push_back() with number"` - - @complexity Logarithmic in the size of the container, O(log(`size()`)). - - @liveexample{The example shows how `push_back()` and `+=` can be used to - add elements to a JSON object. Note how the `null` value was silently - converted to a JSON object.,push_back__object_t__value} - - @since version 1.0.0 - */ - void push_back(const typename object_t::value_type& val) - { - // push_back only works for null objects or objects - if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) - { - JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this)); - } - - // transform null object into an object - if (is_null()) - { - m_type = value_t::object; - m_value = value_t::object; - assert_invariant(); - } - - // add element to object - auto res = m_value.object->insert(val); - set_parent(res.first->second); - } - - /*! - @brief add an object to an object - @copydoc push_back(const typename object_t::value_type&) - */ - reference operator+=(const typename object_t::value_type& val) - { - push_back(val); - return *this; - } - - /*! - @brief add an object to an object - - This function allows to use `push_back` with an initializer list. In case - - 1. the current value is an object, - 2. the initializer list @a init contains only two elements, and - 3. the first element of @a init is a string, - - @a init is converted into an object element and added using - @ref push_back(const typename object_t::value_type&). Otherwise, @a init - is converted to a JSON value and added using @ref push_back(basic_json&&). - - @param[in] init an initializer list - - @complexity Linear in the size of the initializer list @a init. - - @note This function is required to resolve an ambiguous overload error, - because pairs like `{"key", "value"}` can be both interpreted as - `object_t::value_type` or `std::initializer_list`, see - https://github.com/nlohmann/json/issues/235 for more information. - - @liveexample{The example shows how initializer lists are treated as - objects when possible.,push_back__initializer_list} - */ - void push_back(initializer_list_t init) - { - if (is_object() && init.size() == 2 && (*init.begin())->is_string()) - { - basic_json&& key = init.begin()->moved_or_copied(); - push_back(typename object_t::value_type( - std::move(key.get_ref()), (init.begin() + 1)->moved_or_copied())); - } - else - { - push_back(basic_json(init)); - } - } - - /*! - @brief add an object to an object - @copydoc push_back(initializer_list_t) - */ - reference operator+=(initializer_list_t init) - { - push_back(init); - return *this; - } - - /*! - @brief add an object to an array - - Creates a JSON value from the passed parameters @a args to the end of the - JSON value. If the function is called on a JSON null value, an empty array - is created before appending the value created from @a args. - - @param[in] args arguments to forward to a constructor of @ref basic_json - @tparam Args compatible types to create a @ref basic_json object - - @return reference to the inserted element - - @throw type_error.311 when called on a type other than JSON array or - null; example: `"cannot use emplace_back() with number"` - - @complexity Amortized constant. - - @liveexample{The example shows how `push_back()` can be used to add - elements to a JSON array. Note how the `null` value was silently converted - to a JSON array.,emplace_back} - - @since version 2.0.8, returns reference since 3.7.0 - */ - template - reference emplace_back(Args&& ... args) - { - // emplace_back only works for null objects or arrays - if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) - { - JSON_THROW(type_error::create(311, "cannot use emplace_back() with " + std::string(type_name()), *this)); - } - - // transform null object into an array - if (is_null()) - { - m_type = value_t::array; - m_value = value_t::array; - assert_invariant(); - } - - // add element to array (perfect forwarding) - const auto old_capacity = m_value.array->capacity(); - m_value.array->emplace_back(std::forward(args)...); - return set_parent(m_value.array->back(), old_capacity); - } - - /*! - @brief add an object to an object if key does not exist - - Inserts a new element into a JSON object constructed in-place with the - given @a args if there is no element with the key in the container. If the - function is called on a JSON null value, an empty object is created before - appending the value created from @a args. - - @param[in] args arguments to forward to a constructor of @ref basic_json - @tparam Args compatible types to create a @ref basic_json object - - @return a pair consisting of an iterator to the inserted element, or the - already-existing element if no insertion happened, and a bool - denoting whether the insertion took place. - - @throw type_error.311 when called on a type other than JSON object or - null; example: `"cannot use emplace() with number"` - - @complexity Logarithmic in the size of the container, O(log(`size()`)). - - @liveexample{The example shows how `emplace()` can be used to add elements - to a JSON object. Note how the `null` value was silently converted to a - JSON object. Further note how no value is added if there was already one - value stored with the same key.,emplace} - - @since version 2.0.8 - */ - template - std::pair emplace(Args&& ... args) - { - // emplace only works for null objects or arrays - if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_object()))) - { - JSON_THROW(type_error::create(311, "cannot use emplace() with " + std::string(type_name()), *this)); - } - - // transform null object into an object - if (is_null()) - { - m_type = value_t::object; - m_value = value_t::object; - assert_invariant(); - } - - // add element to array (perfect forwarding) - auto res = m_value.object->emplace(std::forward(args)...); - set_parent(res.first->second); - - // create result iterator and set iterator to the result of emplace - auto it = begin(); - it.m_it.object_iterator = res.first; - - // return pair of iterator and boolean - return {it, res.second}; - } - - /// Helper for insertion of an iterator - /// @note: This uses std::distance to support GCC 4.8, - /// see https://github.com/nlohmann/json/pull/1257 - template - iterator insert_iterator(const_iterator pos, Args&& ... args) - { - iterator result(this); - JSON_ASSERT(m_value.array != nullptr); - - auto insert_pos = std::distance(m_value.array->begin(), pos.m_it.array_iterator); - m_value.array->insert(pos.m_it.array_iterator, std::forward(args)...); - result.m_it.array_iterator = m_value.array->begin() + insert_pos; - - // This could have been written as: - // result.m_it.array_iterator = m_value.array->insert(pos.m_it.array_iterator, cnt, val); - // but the return value of insert is missing in GCC 4.8, so it is written this way instead. - - set_parents(); - return result; - } - - /*! - @brief inserts element - - Inserts element @a val before iterator @a pos. - - @param[in] pos iterator before which the content will be inserted; may be - the end() iterator - @param[in] val element to insert - @return iterator pointing to the inserted @a val. - - @throw type_error.309 if called on JSON values other than arrays; - example: `"cannot use insert() with string"` - @throw invalid_iterator.202 if @a pos is not an iterator of *this; - example: `"iterator does not fit current value"` - - @complexity Constant plus linear in the distance between @a pos and end of - the container. - - @liveexample{The example shows how `insert()` is used.,insert} - - @since version 1.0.0 - */ - iterator insert(const_iterator pos, const basic_json& val) - { - // insert only works for arrays - if (JSON_HEDLEY_LIKELY(is_array())) - { - // check if iterator pos fits to this JSON value - if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) - { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); - } - - // insert to array and return iterator - return insert_iterator(pos, val); - } - - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); - } - - /*! - @brief inserts element - @copydoc insert(const_iterator, const basic_json&) - */ - iterator insert(const_iterator pos, basic_json&& val) - { - return insert(pos, val); - } - - /*! - @brief inserts elements - - Inserts @a cnt copies of @a val before iterator @a pos. - - @param[in] pos iterator before which the content will be inserted; may be - the end() iterator - @param[in] cnt number of copies of @a val to insert - @param[in] val element to insert - @return iterator pointing to the first element inserted, or @a pos if - `cnt==0` - - @throw type_error.309 if called on JSON values other than arrays; example: - `"cannot use insert() with string"` - @throw invalid_iterator.202 if @a pos is not an iterator of *this; - example: `"iterator does not fit current value"` - - @complexity Linear in @a cnt plus linear in the distance between @a pos - and end of the container. - - @liveexample{The example shows how `insert()` is used.,insert__count} - - @since version 1.0.0 - */ - iterator insert(const_iterator pos, size_type cnt, const basic_json& val) - { - // insert only works for arrays - if (JSON_HEDLEY_LIKELY(is_array())) - { - // check if iterator pos fits to this JSON value - if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) - { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); - } - - // insert to array and return iterator - return insert_iterator(pos, cnt, val); - } - - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); - } - - /*! - @brief inserts elements - - Inserts elements from range `[first, last)` before iterator @a pos. - - @param[in] pos iterator before which the content will be inserted; may be - the end() iterator - @param[in] first begin of the range of elements to insert - @param[in] last end of the range of elements to insert - - @throw type_error.309 if called on JSON values other than arrays; example: - `"cannot use insert() with string"` - @throw invalid_iterator.202 if @a pos is not an iterator of *this; - example: `"iterator does not fit current value"` - @throw invalid_iterator.210 if @a first and @a last do not belong to the - same JSON value; example: `"iterators do not fit"` - @throw invalid_iterator.211 if @a first or @a last are iterators into - container for which insert is called; example: `"passed iterators may not - belong to container"` - - @return iterator pointing to the first element inserted, or @a pos if - `first==last` - - @complexity Linear in `std::distance(first, last)` plus linear in the - distance between @a pos and end of the container. - - @liveexample{The example shows how `insert()` is used.,insert__range} - - @since version 1.0.0 - */ - iterator insert(const_iterator pos, const_iterator first, const_iterator last) - { - // insert only works for arrays - if (JSON_HEDLEY_UNLIKELY(!is_array())) - { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); - } - - // check if iterator pos fits to this JSON value - if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) - { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); - } - - // check if range iterators belong to the same JSON object - if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) - { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); - } - - if (JSON_HEDLEY_UNLIKELY(first.m_object == this)) - { - JSON_THROW(invalid_iterator::create(211, "passed iterators may not belong to container", *this)); - } - - // insert to array and return iterator - return insert_iterator(pos, first.m_it.array_iterator, last.m_it.array_iterator); - } - - /*! - @brief inserts elements - - Inserts elements from initializer list @a ilist before iterator @a pos. - - @param[in] pos iterator before which the content will be inserted; may be - the end() iterator - @param[in] ilist initializer list to insert the values from - - @throw type_error.309 if called on JSON values other than arrays; example: - `"cannot use insert() with string"` - @throw invalid_iterator.202 if @a pos is not an iterator of *this; - example: `"iterator does not fit current value"` - - @return iterator pointing to the first element inserted, or @a pos if - `ilist` is empty - - @complexity Linear in `ilist.size()` plus linear in the distance between - @a pos and end of the container. - - @liveexample{The example shows how `insert()` is used.,insert__ilist} - - @since version 1.0.0 - */ - iterator insert(const_iterator pos, initializer_list_t ilist) - { - // insert only works for arrays - if (JSON_HEDLEY_UNLIKELY(!is_array())) - { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); - } - - // check if iterator pos fits to this JSON value - if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) - { - JSON_THROW(invalid_iterator::create(202, "iterator does not fit current value", *this)); - } - - // insert to array and return iterator - return insert_iterator(pos, ilist.begin(), ilist.end()); - } - - /*! - @brief inserts elements - - Inserts elements from range `[first, last)`. - - @param[in] first begin of the range of elements to insert - @param[in] last end of the range of elements to insert - - @throw type_error.309 if called on JSON values other than objects; example: - `"cannot use insert() with string"` - @throw invalid_iterator.202 if iterator @a first or @a last does does not - point to an object; example: `"iterators first and last must point to - objects"` - @throw invalid_iterator.210 if @a first and @a last do not belong to the - same JSON value; example: `"iterators do not fit"` - - @complexity Logarithmic: `O(N*log(size() + N))`, where `N` is the number - of elements to insert. - - @liveexample{The example shows how `insert()` is used.,insert__range_object} - - @since version 3.0.0 - */ - void insert(const_iterator first, const_iterator last) - { - // insert only works for objects - if (JSON_HEDLEY_UNLIKELY(!is_object())) - { - JSON_THROW(type_error::create(309, "cannot use insert() with " + std::string(type_name()), *this)); - } - - // check if range iterators belong to the same JSON object - if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) - { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); - } - - // passed iterators must belong to objects - if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object())) - { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this)); - } - - m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator); - } - - /*! - @brief updates a JSON object from another object, overwriting existing keys - - Inserts all values from JSON object @a j and overwrites existing keys. - - @param[in] j JSON object to read values from - - @throw type_error.312 if called on JSON values other than objects; example: - `"cannot use update() with string"` - - @complexity O(N*log(size() + N)), where N is the number of elements to - insert. - - @liveexample{The example shows how `update()` is used.,update} - - @sa https://docs.python.org/3.6/library/stdtypes.html#dict.update - - @since version 3.0.0 - */ - void update(const_reference j) - { - // implicitly convert null value to an empty object - if (is_null()) - { - m_type = value_t::object; - m_value.object = create(); - assert_invariant(); - } - - if (JSON_HEDLEY_UNLIKELY(!is_object())) - { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this)); - } - if (JSON_HEDLEY_UNLIKELY(!j.is_object())) - { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name()), *this)); - } - - for (auto it = j.cbegin(); it != j.cend(); ++it) - { - m_value.object->operator[](it.key()) = it.value(); -#if JSON_DIAGNOSTICS - m_value.object->operator[](it.key()).m_parent = this; -#endif - } - } - - /*! - @brief updates a JSON object from another object, overwriting existing keys - - Inserts all values from from range `[first, last)` and overwrites existing - keys. - - @param[in] first begin of the range of elements to insert - @param[in] last end of the range of elements to insert - - @throw type_error.312 if called on JSON values other than objects; example: - `"cannot use update() with string"` - @throw invalid_iterator.202 if iterator @a first or @a last does does not - point to an object; example: `"iterators first and last must point to - objects"` - @throw invalid_iterator.210 if @a first and @a last do not belong to the - same JSON value; example: `"iterators do not fit"` - - @complexity O(N*log(size() + N)), where N is the number of elements to - insert. - - @liveexample{The example shows how `update()` is used__range.,update} - - @sa https://docs.python.org/3.6/library/stdtypes.html#dict.update - - @since version 3.0.0 - */ - void update(const_iterator first, const_iterator last) - { - // implicitly convert null value to an empty object - if (is_null()) - { - m_type = value_t::object; - m_value.object = create(); - assert_invariant(); - } - - if (JSON_HEDLEY_UNLIKELY(!is_object())) - { - JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name()), *this)); - } - - // check if range iterators belong to the same JSON object - if (JSON_HEDLEY_UNLIKELY(first.m_object != last.m_object)) - { - JSON_THROW(invalid_iterator::create(210, "iterators do not fit", *this)); - } - - // passed iterators must belong to objects - if (JSON_HEDLEY_UNLIKELY(!first.m_object->is_object() - || !last.m_object->is_object())) - { - JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects", *this)); - } - - for (auto it = first; it != last; ++it) - { - m_value.object->operator[](it.key()) = it.value(); -#if JSON_DIAGNOSTICS - m_value.object->operator[](it.key()).m_parent = this; -#endif - } - } - - /*! - @brief exchanges the values - - Exchanges the contents of the JSON value with those of @a other. Does not - invoke any move, copy, or swap operations on individual elements. All - iterators and references remain valid. The past-the-end iterator is - invalidated. - - @param[in,out] other JSON value to exchange the contents with - - @complexity Constant. - - @liveexample{The example below shows how JSON values can be swapped with - `swap()`.,swap__reference} - - @since version 1.0.0 - */ - void swap(reference other) noexcept ( - std::is_nothrow_move_constructible::value&& - std::is_nothrow_move_assignable::value&& - std::is_nothrow_move_constructible::value&& - std::is_nothrow_move_assignable::value - ) - { - std::swap(m_type, other.m_type); - std::swap(m_value, other.m_value); - - set_parents(); - other.set_parents(); - assert_invariant(); - } - - /*! - @brief exchanges the values - - Exchanges the contents of the JSON value from @a left with those of @a right. Does not - invoke any move, copy, or swap operations on individual elements. All - iterators and references remain valid. The past-the-end iterator is - invalidated. implemented as a friend function callable via ADL. - - @param[in,out] left JSON value to exchange the contents with - @param[in,out] right JSON value to exchange the contents with - - @complexity Constant. - - @liveexample{The example below shows how JSON values can be swapped with - `swap()`.,swap__reference} - - @since version 1.0.0 - */ - friend void swap(reference left, reference right) noexcept ( - std::is_nothrow_move_constructible::value&& - std::is_nothrow_move_assignable::value&& - std::is_nothrow_move_constructible::value&& - std::is_nothrow_move_assignable::value - ) - { - left.swap(right); - } - - /*! - @brief exchanges the values - - Exchanges the contents of a JSON array with those of @a other. Does not - invoke any move, copy, or swap operations on individual elements. All - iterators and references remain valid. The past-the-end iterator is - invalidated. - - @param[in,out] other array to exchange the contents with - - @throw type_error.310 when JSON value is not an array; example: `"cannot - use swap() with string"` - - @complexity Constant. - - @liveexample{The example below shows how arrays can be swapped with - `swap()`.,swap__array_t} - - @since version 1.0.0 - */ - void swap(array_t& other) // NOLINT(bugprone-exception-escape) - { - // swap only works for arrays - if (JSON_HEDLEY_LIKELY(is_array())) - { - std::swap(*(m_value.array), other); - } - else - { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); - } - } - - /*! - @brief exchanges the values - - Exchanges the contents of a JSON object with those of @a other. Does not - invoke any move, copy, or swap operations on individual elements. All - iterators and references remain valid. The past-the-end iterator is - invalidated. - - @param[in,out] other object to exchange the contents with - - @throw type_error.310 when JSON value is not an object; example: - `"cannot use swap() with string"` - - @complexity Constant. - - @liveexample{The example below shows how objects can be swapped with - `swap()`.,swap__object_t} - - @since version 1.0.0 - */ - void swap(object_t& other) // NOLINT(bugprone-exception-escape) - { - // swap only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - std::swap(*(m_value.object), other); - } - else - { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); - } - } - - /*! - @brief exchanges the values - - Exchanges the contents of a JSON string with those of @a other. Does not - invoke any move, copy, or swap operations on individual elements. All - iterators and references remain valid. The past-the-end iterator is - invalidated. - - @param[in,out] other string to exchange the contents with - - @throw type_error.310 when JSON value is not a string; example: `"cannot - use swap() with boolean"` - - @complexity Constant. - - @liveexample{The example below shows how strings can be swapped with - `swap()`.,swap__string_t} - - @since version 1.0.0 - */ - void swap(string_t& other) // NOLINT(bugprone-exception-escape) - { - // swap only works for strings - if (JSON_HEDLEY_LIKELY(is_string())) - { - std::swap(*(m_value.string), other); - } - else - { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); - } - } - - /*! - @brief exchanges the values - - Exchanges the contents of a JSON string with those of @a other. Does not - invoke any move, copy, or swap operations on individual elements. All - iterators and references remain valid. The past-the-end iterator is - invalidated. - - @param[in,out] other binary to exchange the contents with - - @throw type_error.310 when JSON value is not a string; example: `"cannot - use swap() with boolean"` - - @complexity Constant. - - @liveexample{The example below shows how strings can be swapped with - `swap()`.,swap__binary_t} - - @since version 3.8.0 - */ - void swap(binary_t& other) // NOLINT(bugprone-exception-escape) - { - // swap only works for strings - if (JSON_HEDLEY_LIKELY(is_binary())) - { - std::swap(*(m_value.binary), other); - } - else - { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); - } - } - - /// @copydoc swap(binary_t&) - void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape) - { - // swap only works for strings - if (JSON_HEDLEY_LIKELY(is_binary())) - { - std::swap(*(m_value.binary), other); - } - else - { - JSON_THROW(type_error::create(310, "cannot use swap() with " + std::string(type_name()), *this)); - } - } - - /// @} - - public: - ////////////////////////////////////////// - // lexicographical comparison operators // - ////////////////////////////////////////// - - /// @name lexicographical comparison operators - /// @{ - - /*! - @brief comparison: equal - - Compares two JSON values for equality according to the following rules: - - Two JSON values are equal if (1) they are from the same type and (2) - their stored values are the same according to their respective - `operator==`. - - Integer and floating-point numbers are automatically converted before - comparison. Note that two NaN values are always treated as unequal. - - Two JSON null values are equal. - - @note Floating-point inside JSON values numbers are compared with - `json::number_float_t::operator==` which is `double::operator==` by - default. To compare floating-point while respecting an epsilon, an alternative - [comparison function](https://github.com/mariokonrad/marnav/blob/master/include/marnav/math/floatingpoint.hpp#L34-#L39) - could be used, for instance - @code {.cpp} - template::value, T>::type> - inline bool is_same(T a, T b, T epsilon = std::numeric_limits::epsilon()) noexcept - { - return std::abs(a - b) <= epsilon; - } - @endcode - Or you can self-defined operator equal function like this: - @code {.cpp} - bool my_equal(const_reference lhs, const_reference rhs) { - const auto lhs_type lhs.type(); - const auto rhs_type rhs.type(); - if (lhs_type == rhs_type) { - switch(lhs_type) - // self_defined case - case value_t::number_float: - return std::abs(lhs - rhs) <= std::numeric_limits::epsilon(); - // other cases remain the same with the original - ... - } - ... - } - @endcode - - @note NaN values never compare equal to themselves or to other NaN values. - - @param[in] lhs first JSON value to consider - @param[in] rhs second JSON value to consider - @return whether the values @a lhs and @a rhs are equal - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @complexity Linear. - - @liveexample{The example demonstrates comparing several JSON - types.,operator__equal} - - @since version 1.0.0 - */ - friend bool operator==(const_reference lhs, const_reference rhs) noexcept - { -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wfloat-equal" -#endif - const auto lhs_type = lhs.type(); - const auto rhs_type = rhs.type(); - - if (lhs_type == rhs_type) - { - switch (lhs_type) - { - case value_t::array: - return *lhs.m_value.array == *rhs.m_value.array; - - case value_t::object: - return *lhs.m_value.object == *rhs.m_value.object; - - case value_t::null: - return true; - - case value_t::string: - return *lhs.m_value.string == *rhs.m_value.string; - - case value_t::boolean: - return lhs.m_value.boolean == rhs.m_value.boolean; - - case value_t::number_integer: - return lhs.m_value.number_integer == rhs.m_value.number_integer; - - case value_t::number_unsigned: - return lhs.m_value.number_unsigned == rhs.m_value.number_unsigned; - - case value_t::number_float: - return lhs.m_value.number_float == rhs.m_value.number_float; - - case value_t::binary: - return *lhs.m_value.binary == *rhs.m_value.binary; - - case value_t::discarded: - default: - return false; - } - } - else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) - { - return static_cast(lhs.m_value.number_integer) == rhs.m_value.number_float; - } - else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) - { - return lhs.m_value.number_float == static_cast(rhs.m_value.number_integer); - } - else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) - { - return static_cast(lhs.m_value.number_unsigned) == rhs.m_value.number_float; - } - else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) - { - return lhs.m_value.number_float == static_cast(rhs.m_value.number_unsigned); - } - else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) - { - return static_cast(lhs.m_value.number_unsigned) == rhs.m_value.number_integer; - } - else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) - { - return lhs.m_value.number_integer == static_cast(rhs.m_value.number_unsigned); - } - - return false; -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif - } - - /*! - @brief comparison: equal - @copydoc operator==(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator==(const_reference lhs, ScalarType rhs) noexcept - { - return lhs == basic_json(rhs); - } - - /*! - @brief comparison: equal - @copydoc operator==(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator==(ScalarType lhs, const_reference rhs) noexcept - { - return basic_json(lhs) == rhs; - } - - /*! - @brief comparison: not equal - - Compares two JSON values for inequality by calculating `not (lhs == rhs)`. - - @param[in] lhs first JSON value to consider - @param[in] rhs second JSON value to consider - @return whether the values @a lhs and @a rhs are not equal - - @complexity Linear. - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @liveexample{The example demonstrates comparing several JSON - types.,operator__notequal} - - @since version 1.0.0 - */ - friend bool operator!=(const_reference lhs, const_reference rhs) noexcept - { - return !(lhs == rhs); - } - - /*! - @brief comparison: not equal - @copydoc operator!=(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator!=(const_reference lhs, ScalarType rhs) noexcept - { - return lhs != basic_json(rhs); - } - - /*! - @brief comparison: not equal - @copydoc operator!=(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator!=(ScalarType lhs, const_reference rhs) noexcept - { - return basic_json(lhs) != rhs; - } - - /*! - @brief comparison: less than - - Compares whether one JSON value @a lhs is less than another JSON value @a - rhs according to the following rules: - - If @a lhs and @a rhs have the same type, the values are compared using - the default `<` operator. - - Integer and floating-point numbers are automatically converted before - comparison - - In case @a lhs and @a rhs have different types, the values are ignored - and the order of the types is considered, see - @ref operator<(const value_t, const value_t). - - @param[in] lhs first JSON value to consider - @param[in] rhs second JSON value to consider - @return whether @a lhs is less than @a rhs - - @complexity Linear. - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @liveexample{The example demonstrates comparing several JSON - types.,operator__less} - - @since version 1.0.0 - */ - friend bool operator<(const_reference lhs, const_reference rhs) noexcept - { - const auto lhs_type = lhs.type(); - const auto rhs_type = rhs.type(); - - if (lhs_type == rhs_type) - { - switch (lhs_type) - { - case value_t::array: - // note parentheses are necessary, see - // https://github.com/nlohmann/json/issues/1530 - return (*lhs.m_value.array) < (*rhs.m_value.array); - - case value_t::object: - return (*lhs.m_value.object) < (*rhs.m_value.object); - - case value_t::null: - return false; - - case value_t::string: - return (*lhs.m_value.string) < (*rhs.m_value.string); - - case value_t::boolean: - return (lhs.m_value.boolean) < (rhs.m_value.boolean); - - case value_t::number_integer: - return (lhs.m_value.number_integer) < (rhs.m_value.number_integer); - - case value_t::number_unsigned: - return (lhs.m_value.number_unsigned) < (rhs.m_value.number_unsigned); - - case value_t::number_float: - return (lhs.m_value.number_float) < (rhs.m_value.number_float); - - case value_t::binary: - return (*lhs.m_value.binary) < (*rhs.m_value.binary); - - case value_t::discarded: - default: - return false; - } - } - else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_float) - { - return static_cast(lhs.m_value.number_integer) < rhs.m_value.number_float; - } - else if (lhs_type == value_t::number_float && rhs_type == value_t::number_integer) - { - return lhs.m_value.number_float < static_cast(rhs.m_value.number_integer); - } - else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_float) - { - return static_cast(lhs.m_value.number_unsigned) < rhs.m_value.number_float; - } - else if (lhs_type == value_t::number_float && rhs_type == value_t::number_unsigned) - { - return lhs.m_value.number_float < static_cast(rhs.m_value.number_unsigned); - } - else if (lhs_type == value_t::number_integer && rhs_type == value_t::number_unsigned) - { - return lhs.m_value.number_integer < static_cast(rhs.m_value.number_unsigned); - } - else if (lhs_type == value_t::number_unsigned && rhs_type == value_t::number_integer) - { - return static_cast(lhs.m_value.number_unsigned) < rhs.m_value.number_integer; - } - - // We only reach this line if we cannot compare values. In that case, - // we compare types. Note we have to call the operator explicitly, - // because MSVC has problems otherwise. - return operator<(lhs_type, rhs_type); - } - - /*! - @brief comparison: less than - @copydoc operator<(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator<(const_reference lhs, ScalarType rhs) noexcept - { - return lhs < basic_json(rhs); - } - - /*! - @brief comparison: less than - @copydoc operator<(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator<(ScalarType lhs, const_reference rhs) noexcept - { - return basic_json(lhs) < rhs; - } - - /*! - @brief comparison: less than or equal - - Compares whether one JSON value @a lhs is less than or equal to another - JSON value by calculating `not (rhs < lhs)`. - - @param[in] lhs first JSON value to consider - @param[in] rhs second JSON value to consider - @return whether @a lhs is less than or equal to @a rhs - - @complexity Linear. - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @liveexample{The example demonstrates comparing several JSON - types.,operator__greater} - - @since version 1.0.0 - */ - friend bool operator<=(const_reference lhs, const_reference rhs) noexcept - { - return !(rhs < lhs); - } - - /*! - @brief comparison: less than or equal - @copydoc operator<=(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator<=(const_reference lhs, ScalarType rhs) noexcept - { - return lhs <= basic_json(rhs); - } - - /*! - @brief comparison: less than or equal - @copydoc operator<=(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator<=(ScalarType lhs, const_reference rhs) noexcept - { - return basic_json(lhs) <= rhs; - } - - /*! - @brief comparison: greater than - - Compares whether one JSON value @a lhs is greater than another - JSON value by calculating `not (lhs <= rhs)`. - - @param[in] lhs first JSON value to consider - @param[in] rhs second JSON value to consider - @return whether @a lhs is greater than to @a rhs - - @complexity Linear. - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @liveexample{The example demonstrates comparing several JSON - types.,operator__lessequal} - - @since version 1.0.0 - */ - friend bool operator>(const_reference lhs, const_reference rhs) noexcept - { - return !(lhs <= rhs); - } - - /*! - @brief comparison: greater than - @copydoc operator>(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator>(const_reference lhs, ScalarType rhs) noexcept - { - return lhs > basic_json(rhs); - } - - /*! - @brief comparison: greater than - @copydoc operator>(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator>(ScalarType lhs, const_reference rhs) noexcept - { - return basic_json(lhs) > rhs; - } - - /*! - @brief comparison: greater than or equal - - Compares whether one JSON value @a lhs is greater than or equal to another - JSON value by calculating `not (lhs < rhs)`. - - @param[in] lhs first JSON value to consider - @param[in] rhs second JSON value to consider - @return whether @a lhs is greater than or equal to @a rhs - - @complexity Linear. - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @liveexample{The example demonstrates comparing several JSON - types.,operator__greaterequal} - - @since version 1.0.0 - */ - friend bool operator>=(const_reference lhs, const_reference rhs) noexcept - { - return !(lhs < rhs); - } - - /*! - @brief comparison: greater than or equal - @copydoc operator>=(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator>=(const_reference lhs, ScalarType rhs) noexcept - { - return lhs >= basic_json(rhs); - } - - /*! - @brief comparison: greater than or equal - @copydoc operator>=(const_reference, const_reference) - */ - template::value, int>::type = 0> - friend bool operator>=(ScalarType lhs, const_reference rhs) noexcept - { - return basic_json(lhs) >= rhs; - } - - /// @} - - /////////////////// - // serialization // - /////////////////// - - /// @name serialization - /// @{ -#ifndef JSON_NO_IO - /*! - @brief serialize to stream - - Serialize the given JSON value @a j to the output stream @a o. The JSON - value will be serialized using the @ref dump member function. - - - The indentation of the output can be controlled with the member variable - `width` of the output stream @a o. For instance, using the manipulator - `std::setw(4)` on @a o sets the indentation level to `4` and the - serialization result is the same as calling `dump(4)`. - - - The indentation character can be controlled with the member variable - `fill` of the output stream @a o. For instance, the manipulator - `std::setfill('\\t')` sets indentation to use a tab character rather than - the default space character. - - @param[in,out] o stream to serialize to - @param[in] j JSON value to serialize - - @return the stream @a o - - @throw type_error.316 if a string stored inside the JSON value is not - UTF-8 encoded - - @complexity Linear. - - @liveexample{The example below shows the serialization with different - parameters to `width` to adjust the indentation level.,operator_serialize} - - @since version 1.0.0; indentation character added in version 3.0.0 - */ - friend std::ostream& operator<<(std::ostream& o, const basic_json& j) - { - // read width member and use it as indentation parameter if nonzero - const bool pretty_print = o.width() > 0; - const auto indentation = pretty_print ? o.width() : 0; - - // reset width to 0 for subsequent calls to this stream - o.width(0); - - // do the actual serialization - serializer s(detail::output_adapter(o), o.fill()); - s.dump(j, pretty_print, false, static_cast(indentation)); - return o; - } - - /*! - @brief serialize to stream - @deprecated This stream operator is deprecated and will be removed in - future 4.0.0 of the library. Please use - @ref operator<<(std::ostream&, const basic_json&) - instead; that is, replace calls like `j >> o;` with `o << j;`. - @since version 1.0.0; deprecated since version 3.0.0 - */ - JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator<<(std::ostream&, const basic_json&)) - friend std::ostream& operator>>(const basic_json& j, std::ostream& o) - { - return o << j; - } -#endif // JSON_NO_IO - /// @} - - - ///////////////////// - // deserialization // - ///////////////////// - - /// @name deserialization - /// @{ - - /*! - @brief deserialize from a compatible input - - @tparam InputType A compatible input, for instance - - an std::istream object - - a FILE pointer - - a C-style array of characters - - a pointer to a null-terminated string of single byte characters - - an object obj for which begin(obj) and end(obj) produces a valid pair of - iterators. - - @param[in] i input to read from - @param[in] cb a parser callback function of type @ref parser_callback_t - which is used to control the deserialization by filtering unwanted values - (optional) - @param[in] allow_exceptions whether to throw exceptions in case of a - parse error (optional, true by default) - @param[in] ignore_comments whether comments should be ignored and treated - like whitespace (true) or yield a parse error (true); (optional, false by - default) - - @return deserialized JSON value; in case of a parse error and - @a allow_exceptions set to `false`, the return value will be - value_t::discarded. - - @throw parse_error.101 if a parse error occurs; example: `""unexpected end - of input; expected string literal""` - @throw parse_error.102 if to_unicode fails or surrogate error - @throw parse_error.103 if to_unicode fails - - @complexity Linear in the length of the input. The parser is a predictive - LL(1) parser. The complexity can be higher if the parser callback function - @a cb or reading from the input @a i has a super-linear complexity. - - @note A UTF-8 byte order mark is silently ignored. - - @liveexample{The example below demonstrates the `parse()` function reading - from an array.,parse__array__parser_callback_t} - - @liveexample{The example below demonstrates the `parse()` function with - and without callback function.,parse__string__parser_callback_t} - - @liveexample{The example below demonstrates the `parse()` function with - and without callback function.,parse__istream__parser_callback_t} - - @liveexample{The example below demonstrates the `parse()` function reading - from a contiguous container.,parse__contiguouscontainer__parser_callback_t} - - @since version 2.0.3 (contiguous containers); version 3.9.0 allowed to - ignore comments. - */ - template - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json parse(InputType&& i, - const parser_callback_t cb = nullptr, - const bool allow_exceptions = true, - const bool ignore_comments = false) - { - basic_json result; - parser(detail::input_adapter(std::forward(i)), cb, allow_exceptions, ignore_comments).parse(true, result); - return result; - } - - /*! - @brief deserialize from a pair of character iterators - - The value_type of the iterator must be a integral type with size of 1, 2 or - 4 bytes, which will be interpreted respectively as UTF-8, UTF-16 and UTF-32. - - @param[in] first iterator to start of character range - @param[in] last iterator to end of character range - @param[in] cb a parser callback function of type @ref parser_callback_t - which is used to control the deserialization by filtering unwanted values - (optional) - @param[in] allow_exceptions whether to throw exceptions in case of a - parse error (optional, true by default) - @param[in] ignore_comments whether comments should be ignored and treated - like whitespace (true) or yield a parse error (true); (optional, false by - default) - - @return deserialized JSON value; in case of a parse error and - @a allow_exceptions set to `false`, the return value will be - value_t::discarded. - - @throw parse_error.101 if a parse error occurs; example: `""unexpected end - of input; expected string literal""` - @throw parse_error.102 if to_unicode fails or surrogate error - @throw parse_error.103 if to_unicode fails - */ - template - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json parse(IteratorType first, - IteratorType last, - const parser_callback_t cb = nullptr, - const bool allow_exceptions = true, - const bool ignore_comments = false) - { - basic_json result; - parser(detail::input_adapter(std::move(first), std::move(last)), cb, allow_exceptions, ignore_comments).parse(true, result); - return result; - } - - JSON_HEDLEY_WARN_UNUSED_RESULT - JSON_HEDLEY_DEPRECATED_FOR(3.8.0, parse(ptr, ptr + len)) - static basic_json parse(detail::span_input_adapter&& i, - const parser_callback_t cb = nullptr, - const bool allow_exceptions = true, - const bool ignore_comments = false) - { - basic_json result; - parser(i.get(), cb, allow_exceptions, ignore_comments).parse(true, result); - return result; - } - - /*! - @brief check if the input is valid JSON - - Unlike the @ref parse(InputType&&, const parser_callback_t,const bool) - function, this function neither throws an exception in case of invalid JSON - input (i.e., a parse error) nor creates diagnostic information. - - @tparam InputType A compatible input, for instance - - an std::istream object - - a FILE pointer - - a C-style array of characters - - a pointer to a null-terminated string of single byte characters - - an object obj for which begin(obj) and end(obj) produces a valid pair of - iterators. - - @param[in] i input to read from - @param[in] ignore_comments whether comments should be ignored and treated - like whitespace (true) or yield a parse error (true); (optional, false by - default) - - @return Whether the input read from @a i is valid JSON. - - @complexity Linear in the length of the input. The parser is a predictive - LL(1) parser. - - @note A UTF-8 byte order mark is silently ignored. - - @liveexample{The example below demonstrates the `accept()` function reading - from a string.,accept__string} - */ - template - static bool accept(InputType&& i, - const bool ignore_comments = false) - { - return parser(detail::input_adapter(std::forward(i)), nullptr, false, ignore_comments).accept(true); - } - - template - static bool accept(IteratorType first, IteratorType last, - const bool ignore_comments = false) - { - return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments).accept(true); - } - - JSON_HEDLEY_WARN_UNUSED_RESULT - JSON_HEDLEY_DEPRECATED_FOR(3.8.0, accept(ptr, ptr + len)) - static bool accept(detail::span_input_adapter&& i, - const bool ignore_comments = false) - { - return parser(i.get(), nullptr, false, ignore_comments).accept(true); - } - - /*! - @brief generate SAX events - - The SAX event lister must follow the interface of @ref json_sax. - - This function reads from a compatible input. Examples are: - - an std::istream object - - a FILE pointer - - a C-style array of characters - - a pointer to a null-terminated string of single byte characters - - an object obj for which begin(obj) and end(obj) produces a valid pair of - iterators. - - @param[in] i input to read from - @param[in,out] sax SAX event listener - @param[in] format the format to parse (JSON, CBOR, MessagePack, or UBJSON) - @param[in] strict whether the input has to be consumed completely - @param[in] ignore_comments whether comments should be ignored and treated - like whitespace (true) or yield a parse error (true); (optional, false by - default); only applies to the JSON file format. - - @return return value of the last processed SAX event - - @throw parse_error.101 if a parse error occurs; example: `""unexpected end - of input; expected string literal""` - @throw parse_error.102 if to_unicode fails or surrogate error - @throw parse_error.103 if to_unicode fails - - @complexity Linear in the length of the input. The parser is a predictive - LL(1) parser. The complexity can be higher if the SAX consumer @a sax has - a super-linear complexity. - - @note A UTF-8 byte order mark is silently ignored. - - @liveexample{The example below demonstrates the `sax_parse()` function - reading from string and processing the events with a user-defined SAX - event consumer.,sax_parse} - - @since version 3.2.0 - */ - template - JSON_HEDLEY_NON_NULL(2) - static bool sax_parse(InputType&& i, SAX* sax, - input_format_t format = input_format_t::json, - const bool strict = true, - const bool ignore_comments = false) - { - auto ia = detail::input_adapter(std::forward(i)); - return format == input_format_t::json - ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); - } - - template - JSON_HEDLEY_NON_NULL(3) - static bool sax_parse(IteratorType first, IteratorType last, SAX* sax, - input_format_t format = input_format_t::json, - const bool strict = true, - const bool ignore_comments = false) - { - auto ia = detail::input_adapter(std::move(first), std::move(last)); - return format == input_format_t::json - ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); - } - - template - JSON_HEDLEY_DEPRECATED_FOR(3.8.0, sax_parse(ptr, ptr + len, ...)) - JSON_HEDLEY_NON_NULL(2) - static bool sax_parse(detail::span_input_adapter&& i, SAX* sax, - input_format_t format = input_format_t::json, - const bool strict = true, - const bool ignore_comments = false) - { - auto ia = i.get(); - return format == input_format_t::json - // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - ? parser(std::move(ia), nullptr, true, ignore_comments).sax_parse(sax, strict) - // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - : detail::binary_reader(std::move(ia)).sax_parse(format, sax, strict); - } -#ifndef JSON_NO_IO - /*! - @brief deserialize from stream - @deprecated This stream operator is deprecated and will be removed in - version 4.0.0 of the library. Please use - @ref operator>>(std::istream&, basic_json&) - instead; that is, replace calls like `j << i;` with `i >> j;`. - @since version 1.0.0; deprecated since version 3.0.0 - */ - JSON_HEDLEY_DEPRECATED_FOR(3.0.0, operator>>(std::istream&, basic_json&)) - friend std::istream& operator<<(basic_json& j, std::istream& i) - { - return operator>>(i, j); - } - - /*! - @brief deserialize from stream - - Deserializes an input stream to a JSON value. - - @param[in,out] i input stream to read a serialized JSON value from - @param[in,out] j JSON value to write the deserialized input to - - @throw parse_error.101 in case of an unexpected token - @throw parse_error.102 if to_unicode fails or surrogate error - @throw parse_error.103 if to_unicode fails - - @complexity Linear in the length of the input. The parser is a predictive - LL(1) parser. - - @note A UTF-8 byte order mark is silently ignored. - - @liveexample{The example below shows how a JSON value is constructed by - reading a serialization from a stream.,operator_deserialize} - - @sa parse(std::istream&, const parser_callback_t) for a variant with a - parser callback function to filter values while parsing - - @since version 1.0.0 - */ - friend std::istream& operator>>(std::istream& i, basic_json& j) - { - parser(detail::input_adapter(i)).parse(false, j); - return i; - } -#endif // JSON_NO_IO - /// @} - - /////////////////////////// - // convenience functions // - /////////////////////////// - - /*! - @brief return the type as string - - Returns the type name as string to be used in error messages - usually to - indicate that a function was called on a wrong JSON type. - - @return a string representation of a the @a m_type member: - Value type | return value - ----------- | ------------- - null | `"null"` - boolean | `"boolean"` - string | `"string"` - number | `"number"` (for all number types) - object | `"object"` - array | `"array"` - binary | `"binary"` - discarded | `"discarded"` - - @exceptionsafety No-throw guarantee: this function never throws exceptions. - - @complexity Constant. - - @liveexample{The following code exemplifies `type_name()` for all JSON - types.,type_name} - - @sa see @ref type() -- return the type of the JSON value - @sa see @ref operator value_t() -- return the type of the JSON value (implicit) - - @since version 1.0.0, public since 2.1.0, `const char*` and `noexcept` - since 3.0.0 - */ - JSON_HEDLEY_RETURNS_NON_NULL - const char* type_name() const noexcept - { - { - switch (m_type) - { - case value_t::null: - return "null"; - case value_t::object: - return "object"; - case value_t::array: - return "array"; - case value_t::string: - return "string"; - case value_t::boolean: - return "boolean"; - case value_t::binary: - return "binary"; - case value_t::discarded: - return "discarded"; - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - default: - return "number"; - } - } - } - - - JSON_PRIVATE_UNLESS_TESTED: - ////////////////////// - // member variables // - ////////////////////// - - /// the type of the current element - value_t m_type = value_t::null; - - /// the value of the current element - json_value m_value = {}; - -#if JSON_DIAGNOSTICS - /// a pointer to a parent value (for debugging purposes) - basic_json* m_parent = nullptr; -#endif - - ////////////////////////////////////////// - // binary serialization/deserialization // - ////////////////////////////////////////// - - /// @name binary serialization/deserialization support - /// @{ - - public: - /*! - @brief create a CBOR serialization of a given JSON value - - Serializes a given JSON value @a j to a byte vector using the CBOR (Concise - Binary Object Representation) serialization format. CBOR is a binary - serialization format which aims to be more compact than JSON itself, yet - more efficient to parse. - - The library uses the following mapping from JSON values types to - CBOR types according to the CBOR specification (RFC 7049): - - JSON value type | value/range | CBOR type | first byte - --------------- | ------------------------------------------ | ---------------------------------- | --------------- - null | `null` | Null | 0xF6 - boolean | `true` | True | 0xF5 - boolean | `false` | False | 0xF4 - number_integer | -9223372036854775808..-2147483649 | Negative integer (8 bytes follow) | 0x3B - number_integer | -2147483648..-32769 | Negative integer (4 bytes follow) | 0x3A - number_integer | -32768..-129 | Negative integer (2 bytes follow) | 0x39 - number_integer | -128..-25 | Negative integer (1 byte follow) | 0x38 - number_integer | -24..-1 | Negative integer | 0x20..0x37 - number_integer | 0..23 | Integer | 0x00..0x17 - number_integer | 24..255 | Unsigned integer (1 byte follow) | 0x18 - number_integer | 256..65535 | Unsigned integer (2 bytes follow) | 0x19 - number_integer | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A - number_integer | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B - number_unsigned | 0..23 | Integer | 0x00..0x17 - number_unsigned | 24..255 | Unsigned integer (1 byte follow) | 0x18 - number_unsigned | 256..65535 | Unsigned integer (2 bytes follow) | 0x19 - number_unsigned | 65536..4294967295 | Unsigned integer (4 bytes follow) | 0x1A - number_unsigned | 4294967296..18446744073709551615 | Unsigned integer (8 bytes follow) | 0x1B - number_float | *any value representable by a float* | Single-Precision Float | 0xFA - number_float | *any value NOT representable by a float* | Double-Precision Float | 0xFB - string | *length*: 0..23 | UTF-8 string | 0x60..0x77 - string | *length*: 23..255 | UTF-8 string (1 byte follow) | 0x78 - string | *length*: 256..65535 | UTF-8 string (2 bytes follow) | 0x79 - string | *length*: 65536..4294967295 | UTF-8 string (4 bytes follow) | 0x7A - string | *length*: 4294967296..18446744073709551615 | UTF-8 string (8 bytes follow) | 0x7B - array | *size*: 0..23 | array | 0x80..0x97 - array | *size*: 23..255 | array (1 byte follow) | 0x98 - array | *size*: 256..65535 | array (2 bytes follow) | 0x99 - array | *size*: 65536..4294967295 | array (4 bytes follow) | 0x9A - array | *size*: 4294967296..18446744073709551615 | array (8 bytes follow) | 0x9B - object | *size*: 0..23 | map | 0xA0..0xB7 - object | *size*: 23..255 | map (1 byte follow) | 0xB8 - object | *size*: 256..65535 | map (2 bytes follow) | 0xB9 - object | *size*: 65536..4294967295 | map (4 bytes follow) | 0xBA - object | *size*: 4294967296..18446744073709551615 | map (8 bytes follow) | 0xBB - binary | *size*: 0..23 | byte string | 0x40..0x57 - binary | *size*: 23..255 | byte string (1 byte follow) | 0x58 - binary | *size*: 256..65535 | byte string (2 bytes follow) | 0x59 - binary | *size*: 65536..4294967295 | byte string (4 bytes follow) | 0x5A - binary | *size*: 4294967296..18446744073709551615 | byte string (8 bytes follow) | 0x5B - - Binary values with subtype are mapped to tagged values (0xD8..0xDB) - depending on the subtype, followed by a byte string, see "binary" cells - in the table above. - - @note The mapping is **complete** in the sense that any JSON value type - can be converted to a CBOR value. - - @note If NaN or Infinity are stored inside a JSON number, they are - serialized properly. This behavior differs from the @ref dump() - function which serializes NaN or Infinity to `null`. - - @note The following CBOR types are not used in the conversion: - - UTF-8 strings terminated by "break" (0x7F) - - arrays terminated by "break" (0x9F) - - maps terminated by "break" (0xBF) - - byte strings terminated by "break" (0x5F) - - date/time (0xC0..0xC1) - - bignum (0xC2..0xC3) - - decimal fraction (0xC4) - - bigfloat (0xC5) - - expected conversions (0xD5..0xD7) - - simple values (0xE0..0xF3, 0xF8) - - undefined (0xF7) - - half-precision floats (0xF9) - - break (0xFF) - - @param[in] j JSON value to serialize - @return CBOR serialization as byte vector - - @complexity Linear in the size of the JSON value @a j. - - @liveexample{The example shows the serialization of a JSON value to a byte - vector in CBOR format.,to_cbor} - - @sa http://cbor.io - @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the - analogous deserialization - @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format - @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the - related UBJSON format - - @since version 2.0.9; compact representation of floating-point numbers - since version 3.8.0 - */ - static std::vector to_cbor(const basic_json& j) - { - std::vector result; - to_cbor(j, result); - return result; - } - - static void to_cbor(const basic_json& j, detail::output_adapter o) - { - binary_writer(o).write_cbor(j); - } - - static void to_cbor(const basic_json& j, detail::output_adapter o) - { - binary_writer(o).write_cbor(j); - } - - /*! - @brief create a MessagePack serialization of a given JSON value - - Serializes a given JSON value @a j to a byte vector using the MessagePack - serialization format. MessagePack is a binary serialization format which - aims to be more compact than JSON itself, yet more efficient to parse. - - The library uses the following mapping from JSON values types to - MessagePack types according to the MessagePack specification: - - JSON value type | value/range | MessagePack type | first byte - --------------- | --------------------------------- | ---------------- | ---------- - null | `null` | nil | 0xC0 - boolean | `true` | true | 0xC3 - boolean | `false` | false | 0xC2 - number_integer | -9223372036854775808..-2147483649 | int64 | 0xD3 - number_integer | -2147483648..-32769 | int32 | 0xD2 - number_integer | -32768..-129 | int16 | 0xD1 - number_integer | -128..-33 | int8 | 0xD0 - number_integer | -32..-1 | negative fixint | 0xE0..0xFF - number_integer | 0..127 | positive fixint | 0x00..0x7F - number_integer | 128..255 | uint 8 | 0xCC - number_integer | 256..65535 | uint 16 | 0xCD - number_integer | 65536..4294967295 | uint 32 | 0xCE - number_integer | 4294967296..18446744073709551615 | uint 64 | 0xCF - number_unsigned | 0..127 | positive fixint | 0x00..0x7F - number_unsigned | 128..255 | uint 8 | 0xCC - number_unsigned | 256..65535 | uint 16 | 0xCD - number_unsigned | 65536..4294967295 | uint 32 | 0xCE - number_unsigned | 4294967296..18446744073709551615 | uint 64 | 0xCF - number_float | *any value representable by a float* | float 32 | 0xCA - number_float | *any value NOT representable by a float* | float 64 | 0xCB - string | *length*: 0..31 | fixstr | 0xA0..0xBF - string | *length*: 32..255 | str 8 | 0xD9 - string | *length*: 256..65535 | str 16 | 0xDA - string | *length*: 65536..4294967295 | str 32 | 0xDB - array | *size*: 0..15 | fixarray | 0x90..0x9F - array | *size*: 16..65535 | array 16 | 0xDC - array | *size*: 65536..4294967295 | array 32 | 0xDD - object | *size*: 0..15 | fix map | 0x80..0x8F - object | *size*: 16..65535 | map 16 | 0xDE - object | *size*: 65536..4294967295 | map 32 | 0xDF - binary | *size*: 0..255 | bin 8 | 0xC4 - binary | *size*: 256..65535 | bin 16 | 0xC5 - binary | *size*: 65536..4294967295 | bin 32 | 0xC6 - - @note The mapping is **complete** in the sense that any JSON value type - can be converted to a MessagePack value. - - @note The following values can **not** be converted to a MessagePack value: - - strings with more than 4294967295 bytes - - byte strings with more than 4294967295 bytes - - arrays with more than 4294967295 elements - - objects with more than 4294967295 elements - - @note Any MessagePack output created @ref to_msgpack can be successfully - parsed by @ref from_msgpack. - - @note If NaN or Infinity are stored inside a JSON number, they are - serialized properly. This behavior differs from the @ref dump() - function which serializes NaN or Infinity to `null`. - - @param[in] j JSON value to serialize - @return MessagePack serialization as byte vector - - @complexity Linear in the size of the JSON value @a j. - - @liveexample{The example shows the serialization of a JSON value to a byte - vector in MessagePack format.,to_msgpack} - - @sa http://msgpack.org - @sa see @ref from_msgpack for the analogous deserialization - @sa see @ref to_cbor(const basic_json& for the related CBOR format - @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the - related UBJSON format - - @since version 2.0.9 - */ - static std::vector to_msgpack(const basic_json& j) - { - std::vector result; - to_msgpack(j, result); - return result; - } - - static void to_msgpack(const basic_json& j, detail::output_adapter o) - { - binary_writer(o).write_msgpack(j); - } - - static void to_msgpack(const basic_json& j, detail::output_adapter o) - { - binary_writer(o).write_msgpack(j); - } - - /*! - @brief create a UBJSON serialization of a given JSON value - - Serializes a given JSON value @a j to a byte vector using the UBJSON - (Universal Binary JSON) serialization format. UBJSON aims to be more compact - than JSON itself, yet more efficient to parse. - - The library uses the following mapping from JSON values types to - UBJSON types according to the UBJSON specification: - - JSON value type | value/range | UBJSON type | marker - --------------- | --------------------------------- | ----------- | ------ - null | `null` | null | `Z` - boolean | `true` | true | `T` - boolean | `false` | false | `F` - number_integer | -9223372036854775808..-2147483649 | int64 | `L` - number_integer | -2147483648..-32769 | int32 | `l` - number_integer | -32768..-129 | int16 | `I` - number_integer | -128..127 | int8 | `i` - number_integer | 128..255 | uint8 | `U` - number_integer | 256..32767 | int16 | `I` - number_integer | 32768..2147483647 | int32 | `l` - number_integer | 2147483648..9223372036854775807 | int64 | `L` - number_unsigned | 0..127 | int8 | `i` - number_unsigned | 128..255 | uint8 | `U` - number_unsigned | 256..32767 | int16 | `I` - number_unsigned | 32768..2147483647 | int32 | `l` - number_unsigned | 2147483648..9223372036854775807 | int64 | `L` - number_unsigned | 2147483649..18446744073709551615 | high-precision | `H` - number_float | *any value* | float64 | `D` - string | *with shortest length indicator* | string | `S` - array | *see notes on optimized format* | array | `[` - object | *see notes on optimized format* | map | `{` - - @note The mapping is **complete** in the sense that any JSON value type - can be converted to a UBJSON value. - - @note The following values can **not** be converted to a UBJSON value: - - strings with more than 9223372036854775807 bytes (theoretical) - - @note The following markers are not used in the conversion: - - `Z`: no-op values are not created. - - `C`: single-byte strings are serialized with `S` markers. - - @note Any UBJSON output created @ref to_ubjson can be successfully parsed - by @ref from_ubjson. - - @note If NaN or Infinity are stored inside a JSON number, they are - serialized properly. This behavior differs from the @ref dump() - function which serializes NaN or Infinity to `null`. - - @note The optimized formats for containers are supported: Parameter - @a use_size adds size information to the beginning of a container and - removes the closing marker. Parameter @a use_type further checks - whether all elements of a container have the same type and adds the - type marker to the beginning of the container. The @a use_type - parameter must only be used together with @a use_size = true. Note - that @a use_size = true alone may result in larger representations - - the benefit of this parameter is that the receiving side is - immediately informed on the number of elements of the container. - - @note If the JSON data contains the binary type, the value stored is a list - of integers, as suggested by the UBJSON documentation. In particular, - this means that serialization and the deserialization of a JSON - containing binary values into UBJSON and back will result in a - different JSON object. - - @param[in] j JSON value to serialize - @param[in] use_size whether to add size annotations to container types - @param[in] use_type whether to add type annotations to container types - (must be combined with @a use_size = true) - @return UBJSON serialization as byte vector - - @complexity Linear in the size of the JSON value @a j. - - @liveexample{The example shows the serialization of a JSON value to a byte - vector in UBJSON format.,to_ubjson} - - @sa http://ubjson.org - @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the - analogous deserialization - @sa see @ref to_cbor(const basic_json& for the related CBOR format - @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format - - @since version 3.1.0 - */ - static std::vector to_ubjson(const basic_json& j, - const bool use_size = false, - const bool use_type = false) - { - std::vector result; - to_ubjson(j, result, use_size, use_type); - return result; - } - - static void to_ubjson(const basic_json& j, detail::output_adapter o, - const bool use_size = false, const bool use_type = false) - { - binary_writer(o).write_ubjson(j, use_size, use_type); - } - - static void to_ubjson(const basic_json& j, detail::output_adapter o, - const bool use_size = false, const bool use_type = false) - { - binary_writer(o).write_ubjson(j, use_size, use_type); - } - - - /*! - @brief Serializes the given JSON object `j` to BSON and returns a vector - containing the corresponding BSON-representation. - - BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are - stored as a single entity (a so-called document). - - The library uses the following mapping from JSON values types to BSON types: - - JSON value type | value/range | BSON type | marker - --------------- | --------------------------------- | ----------- | ------ - null | `null` | null | 0x0A - boolean | `true`, `false` | boolean | 0x08 - number_integer | -9223372036854775808..-2147483649 | int64 | 0x12 - number_integer | -2147483648..2147483647 | int32 | 0x10 - number_integer | 2147483648..9223372036854775807 | int64 | 0x12 - number_unsigned | 0..2147483647 | int32 | 0x10 - number_unsigned | 2147483648..9223372036854775807 | int64 | 0x12 - number_unsigned | 9223372036854775808..18446744073709551615| -- | -- - number_float | *any value* | double | 0x01 - string | *any value* | string | 0x02 - array | *any value* | document | 0x04 - object | *any value* | document | 0x03 - binary | *any value* | binary | 0x05 - - @warning The mapping is **incomplete**, since only JSON-objects (and things - contained therein) can be serialized to BSON. - Also, integers larger than 9223372036854775807 cannot be serialized to BSON, - and the keys may not contain U+0000, since they are serialized a - zero-terminated c-strings. - - @throw out_of_range.407 if `j.is_number_unsigned() && j.get() > 9223372036854775807` - @throw out_of_range.409 if a key in `j` contains a NULL (U+0000) - @throw type_error.317 if `!j.is_object()` - - @pre The input `j` is required to be an object: `j.is_object() == true`. - - @note Any BSON output created via @ref to_bson can be successfully parsed - by @ref from_bson. - - @param[in] j JSON value to serialize - @return BSON serialization as byte vector - - @complexity Linear in the size of the JSON value @a j. - - @liveexample{The example shows the serialization of a JSON value to a byte - vector in BSON format.,to_bson} - - @sa http://bsonspec.org/spec.html - @sa see @ref from_bson(detail::input_adapter&&, const bool strict) for the - analogous deserialization - @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the - related UBJSON format - @sa see @ref to_cbor(const basic_json&) for the related CBOR format - @sa see @ref to_msgpack(const basic_json&) for the related MessagePack format - */ - static std::vector to_bson(const basic_json& j) - { - std::vector result; - to_bson(j, result); - return result; - } - - /*! - @brief Serializes the given JSON object `j` to BSON and forwards the - corresponding BSON-representation to the given output_adapter `o`. - @param j The JSON object to convert to BSON. - @param o The output adapter that receives the binary BSON representation. - @pre The input `j` shall be an object: `j.is_object() == true` - @sa see @ref to_bson(const basic_json&) - */ - static void to_bson(const basic_json& j, detail::output_adapter o) - { - binary_writer(o).write_bson(j); - } - - /*! - @copydoc to_bson(const basic_json&, detail::output_adapter) - */ - static void to_bson(const basic_json& j, detail::output_adapter o) - { - binary_writer(o).write_bson(j); - } - - - /*! - @brief create a JSON value from an input in CBOR format - - Deserializes a given input @a i to a JSON value using the CBOR (Concise - Binary Object Representation) serialization format. - - The library maps CBOR types to JSON value types as follows: - - CBOR type | JSON value type | first byte - ---------------------- | --------------- | ---------- - Integer | number_unsigned | 0x00..0x17 - Unsigned integer | number_unsigned | 0x18 - Unsigned integer | number_unsigned | 0x19 - Unsigned integer | number_unsigned | 0x1A - Unsigned integer | number_unsigned | 0x1B - Negative integer | number_integer | 0x20..0x37 - Negative integer | number_integer | 0x38 - Negative integer | number_integer | 0x39 - Negative integer | number_integer | 0x3A - Negative integer | number_integer | 0x3B - Byte string | binary | 0x40..0x57 - Byte string | binary | 0x58 - Byte string | binary | 0x59 - Byte string | binary | 0x5A - Byte string | binary | 0x5B - UTF-8 string | string | 0x60..0x77 - UTF-8 string | string | 0x78 - UTF-8 string | string | 0x79 - UTF-8 string | string | 0x7A - UTF-8 string | string | 0x7B - UTF-8 string | string | 0x7F - array | array | 0x80..0x97 - array | array | 0x98 - array | array | 0x99 - array | array | 0x9A - array | array | 0x9B - array | array | 0x9F - map | object | 0xA0..0xB7 - map | object | 0xB8 - map | object | 0xB9 - map | object | 0xBA - map | object | 0xBB - map | object | 0xBF - False | `false` | 0xF4 - True | `true` | 0xF5 - Null | `null` | 0xF6 - Half-Precision Float | number_float | 0xF9 - Single-Precision Float | number_float | 0xFA - Double-Precision Float | number_float | 0xFB - - @warning The mapping is **incomplete** in the sense that not all CBOR - types can be converted to a JSON value. The following CBOR types - are not supported and will yield parse errors (parse_error.112): - - date/time (0xC0..0xC1) - - bignum (0xC2..0xC3) - - decimal fraction (0xC4) - - bigfloat (0xC5) - - expected conversions (0xD5..0xD7) - - simple values (0xE0..0xF3, 0xF8) - - undefined (0xF7) - - @warning CBOR allows map keys of any type, whereas JSON only allows - strings as keys in object values. Therefore, CBOR maps with keys - other than UTF-8 strings are rejected (parse_error.113). - - @note Any CBOR output created @ref to_cbor can be successfully parsed by - @ref from_cbor. - - @param[in] i an input in CBOR format convertible to an input adapter - @param[in] strict whether to expect the input to be consumed until EOF - (true by default) - @param[in] allow_exceptions whether to throw exceptions in case of a - parse error (optional, true by default) - @param[in] tag_handler how to treat CBOR tags (optional, error by default) - - @return deserialized JSON value; in case of a parse error and - @a allow_exceptions set to `false`, the return value will be - value_t::discarded. - - @throw parse_error.110 if the given input ends prematurely or the end of - file was not reached when @a strict was set to true - @throw parse_error.112 if unsupported features from CBOR were - used in the given input @a v or if the input is not valid CBOR - @throw parse_error.113 if a string was expected as map key, but not found - - @complexity Linear in the size of the input @a i. - - @liveexample{The example shows the deserialization of a byte vector in CBOR - format to a JSON value.,from_cbor} - - @sa http://cbor.io - @sa see @ref to_cbor(const basic_json&) for the analogous serialization - @sa see @ref from_msgpack(InputType&&, const bool, const bool) for the - related MessagePack format - @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the - related UBJSON format - - @since version 2.0.9; parameter @a start_index since 2.1.1; changed to - consume input adapters, removed start_index parameter, and added - @a strict parameter since 3.0.0; added @a allow_exceptions parameter - since 3.2.0; added @a tag_handler parameter since 3.9.0. - */ - template - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_cbor(InputType&& i, - const bool strict = true, - const bool allow_exceptions = true, - const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = detail::input_adapter(std::forward(i)); - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); - return res ? result : basic_json(value_t::discarded); - } - - /*! - @copydoc from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) - */ - template - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_cbor(IteratorType first, IteratorType last, - const bool strict = true, - const bool allow_exceptions = true, - const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = detail::input_adapter(std::move(first), std::move(last)); - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); - return res ? result : basic_json(value_t::discarded); - } - - template - JSON_HEDLEY_WARN_UNUSED_RESULT - JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len)) - static basic_json from_cbor(const T* ptr, std::size_t len, - const bool strict = true, - const bool allow_exceptions = true, - const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) - { - return from_cbor(ptr, ptr + len, strict, allow_exceptions, tag_handler); - } - - - JSON_HEDLEY_WARN_UNUSED_RESULT - JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_cbor(ptr, ptr + len)) - static basic_json from_cbor(detail::span_input_adapter&& i, - const bool strict = true, - const bool allow_exceptions = true, - const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = i.get(); - // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); - return res ? result : basic_json(value_t::discarded); - } - - /*! - @brief create a JSON value from an input in MessagePack format - - Deserializes a given input @a i to a JSON value using the MessagePack - serialization format. - - The library maps MessagePack types to JSON value types as follows: - - MessagePack type | JSON value type | first byte - ---------------- | --------------- | ---------- - positive fixint | number_unsigned | 0x00..0x7F - fixmap | object | 0x80..0x8F - fixarray | array | 0x90..0x9F - fixstr | string | 0xA0..0xBF - nil | `null` | 0xC0 - false | `false` | 0xC2 - true | `true` | 0xC3 - float 32 | number_float | 0xCA - float 64 | number_float | 0xCB - uint 8 | number_unsigned | 0xCC - uint 16 | number_unsigned | 0xCD - uint 32 | number_unsigned | 0xCE - uint 64 | number_unsigned | 0xCF - int 8 | number_integer | 0xD0 - int 16 | number_integer | 0xD1 - int 32 | number_integer | 0xD2 - int 64 | number_integer | 0xD3 - str 8 | string | 0xD9 - str 16 | string | 0xDA - str 32 | string | 0xDB - array 16 | array | 0xDC - array 32 | array | 0xDD - map 16 | object | 0xDE - map 32 | object | 0xDF - bin 8 | binary | 0xC4 - bin 16 | binary | 0xC5 - bin 32 | binary | 0xC6 - ext 8 | binary | 0xC7 - ext 16 | binary | 0xC8 - ext 32 | binary | 0xC9 - fixext 1 | binary | 0xD4 - fixext 2 | binary | 0xD5 - fixext 4 | binary | 0xD6 - fixext 8 | binary | 0xD7 - fixext 16 | binary | 0xD8 - negative fixint | number_integer | 0xE0-0xFF - - @note Any MessagePack output created @ref to_msgpack can be successfully - parsed by @ref from_msgpack. - - @param[in] i an input in MessagePack format convertible to an input - adapter - @param[in] strict whether to expect the input to be consumed until EOF - (true by default) - @param[in] allow_exceptions whether to throw exceptions in case of a - parse error (optional, true by default) - - @return deserialized JSON value; in case of a parse error and - @a allow_exceptions set to `false`, the return value will be - value_t::discarded. - - @throw parse_error.110 if the given input ends prematurely or the end of - file was not reached when @a strict was set to true - @throw parse_error.112 if unsupported features from MessagePack were - used in the given input @a i or if the input is not valid MessagePack - @throw parse_error.113 if a string was expected as map key, but not found - - @complexity Linear in the size of the input @a i. - - @liveexample{The example shows the deserialization of a byte vector in - MessagePack format to a JSON value.,from_msgpack} - - @sa http://msgpack.org - @sa see @ref to_msgpack(const basic_json&) for the analogous serialization - @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the - related CBOR format - @sa see @ref from_ubjson(InputType&&, const bool, const bool) for - the related UBJSON format - @sa see @ref from_bson(InputType&&, const bool, const bool) for - the related BSON format - - @since version 2.0.9; parameter @a start_index since 2.1.1; changed to - consume input adapters, removed start_index parameter, and added - @a strict parameter since 3.0.0; added @a allow_exceptions parameter - since 3.2.0 - */ - template - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_msgpack(InputType&& i, - const bool strict = true, - const bool allow_exceptions = true) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = detail::input_adapter(std::forward(i)); - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict); - return res ? result : basic_json(value_t::discarded); - } - - /*! - @copydoc from_msgpack(InputType&&, const bool, const bool) - */ - template - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_msgpack(IteratorType first, IteratorType last, - const bool strict = true, - const bool allow_exceptions = true) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = detail::input_adapter(std::move(first), std::move(last)); - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict); - return res ? result : basic_json(value_t::discarded); - } - - - template - JSON_HEDLEY_WARN_UNUSED_RESULT - JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len)) - static basic_json from_msgpack(const T* ptr, std::size_t len, - const bool strict = true, - const bool allow_exceptions = true) - { - return from_msgpack(ptr, ptr + len, strict, allow_exceptions); - } - - JSON_HEDLEY_WARN_UNUSED_RESULT - JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_msgpack(ptr, ptr + len)) - static basic_json from_msgpack(detail::span_input_adapter&& i, - const bool strict = true, - const bool allow_exceptions = true) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = i.get(); - // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::msgpack, &sdp, strict); - return res ? result : basic_json(value_t::discarded); - } - - - /*! - @brief create a JSON value from an input in UBJSON format - - Deserializes a given input @a i to a JSON value using the UBJSON (Universal - Binary JSON) serialization format. - - The library maps UBJSON types to JSON value types as follows: - - UBJSON type | JSON value type | marker - ----------- | --------------------------------------- | ------ - no-op | *no value, next value is read* | `N` - null | `null` | `Z` - false | `false` | `F` - true | `true` | `T` - float32 | number_float | `d` - float64 | number_float | `D` - uint8 | number_unsigned | `U` - int8 | number_integer | `i` - int16 | number_integer | `I` - int32 | number_integer | `l` - int64 | number_integer | `L` - high-precision number | number_integer, number_unsigned, or number_float - depends on number string | 'H' - string | string | `S` - char | string | `C` - array | array (optimized values are supported) | `[` - object | object (optimized values are supported) | `{` - - @note The mapping is **complete** in the sense that any UBJSON value can - be converted to a JSON value. - - @param[in] i an input in UBJSON format convertible to an input adapter - @param[in] strict whether to expect the input to be consumed until EOF - (true by default) - @param[in] allow_exceptions whether to throw exceptions in case of a - parse error (optional, true by default) - - @return deserialized JSON value; in case of a parse error and - @a allow_exceptions set to `false`, the return value will be - value_t::discarded. - - @throw parse_error.110 if the given input ends prematurely or the end of - file was not reached when @a strict was set to true - @throw parse_error.112 if a parse error occurs - @throw parse_error.113 if a string could not be parsed successfully - - @complexity Linear in the size of the input @a i. - - @liveexample{The example shows the deserialization of a byte vector in - UBJSON format to a JSON value.,from_ubjson} - - @sa http://ubjson.org - @sa see @ref to_ubjson(const basic_json&, const bool, const bool) for the - analogous serialization - @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the - related CBOR format - @sa see @ref from_msgpack(InputType&&, const bool, const bool) for - the related MessagePack format - @sa see @ref from_bson(InputType&&, const bool, const bool) for - the related BSON format - - @since version 3.1.0; added @a allow_exceptions parameter since 3.2.0 - */ - template - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_ubjson(InputType&& i, - const bool strict = true, - const bool allow_exceptions = true) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = detail::input_adapter(std::forward(i)); - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict); - return res ? result : basic_json(value_t::discarded); - } - - /*! - @copydoc from_ubjson(InputType&&, const bool, const bool) - */ - template - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_ubjson(IteratorType first, IteratorType last, - const bool strict = true, - const bool allow_exceptions = true) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = detail::input_adapter(std::move(first), std::move(last)); - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict); - return res ? result : basic_json(value_t::discarded); - } - - template - JSON_HEDLEY_WARN_UNUSED_RESULT - JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len)) - static basic_json from_ubjson(const T* ptr, std::size_t len, - const bool strict = true, - const bool allow_exceptions = true) - { - return from_ubjson(ptr, ptr + len, strict, allow_exceptions); - } - - JSON_HEDLEY_WARN_UNUSED_RESULT - JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_ubjson(ptr, ptr + len)) - static basic_json from_ubjson(detail::span_input_adapter&& i, - const bool strict = true, - const bool allow_exceptions = true) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = i.get(); - // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::ubjson, &sdp, strict); - return res ? result : basic_json(value_t::discarded); - } - - - /*! - @brief Create a JSON value from an input in BSON format - - Deserializes a given input @a i to a JSON value using the BSON (Binary JSON) - serialization format. - - The library maps BSON record types to JSON value types as follows: - - BSON type | BSON marker byte | JSON value type - --------------- | ---------------- | --------------------------- - double | 0x01 | number_float - string | 0x02 | string - document | 0x03 | object - array | 0x04 | array - binary | 0x05 | binary - undefined | 0x06 | still unsupported - ObjectId | 0x07 | still unsupported - boolean | 0x08 | boolean - UTC Date-Time | 0x09 | still unsupported - null | 0x0A | null - Regular Expr. | 0x0B | still unsupported - DB Pointer | 0x0C | still unsupported - JavaScript Code | 0x0D | still unsupported - Symbol | 0x0E | still unsupported - JavaScript Code | 0x0F | still unsupported - int32 | 0x10 | number_integer - Timestamp | 0x11 | still unsupported - 128-bit decimal float | 0x13 | still unsupported - Max Key | 0x7F | still unsupported - Min Key | 0xFF | still unsupported - - @warning The mapping is **incomplete**. The unsupported mappings - are indicated in the table above. - - @param[in] i an input in BSON format convertible to an input adapter - @param[in] strict whether to expect the input to be consumed until EOF - (true by default) - @param[in] allow_exceptions whether to throw exceptions in case of a - parse error (optional, true by default) - - @return deserialized JSON value; in case of a parse error and - @a allow_exceptions set to `false`, the return value will be - value_t::discarded. - - @throw parse_error.114 if an unsupported BSON record type is encountered - - @complexity Linear in the size of the input @a i. - - @liveexample{The example shows the deserialization of a byte vector in - BSON format to a JSON value.,from_bson} - - @sa http://bsonspec.org/spec.html - @sa see @ref to_bson(const basic_json&) for the analogous serialization - @sa see @ref from_cbor(InputType&&, const bool, const bool, const cbor_tag_handler_t) for the - related CBOR format - @sa see @ref from_msgpack(InputType&&, const bool, const bool) for - the related MessagePack format - @sa see @ref from_ubjson(InputType&&, const bool, const bool) for the - related UBJSON format - */ - template - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_bson(InputType&& i, - const bool strict = true, - const bool allow_exceptions = true) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = detail::input_adapter(std::forward(i)); - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict); - return res ? result : basic_json(value_t::discarded); - } - - /*! - @copydoc from_bson(InputType&&, const bool, const bool) - */ - template - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json from_bson(IteratorType first, IteratorType last, - const bool strict = true, - const bool allow_exceptions = true) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = detail::input_adapter(std::move(first), std::move(last)); - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict); - return res ? result : basic_json(value_t::discarded); - } - - template - JSON_HEDLEY_WARN_UNUSED_RESULT - JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len)) - static basic_json from_bson(const T* ptr, std::size_t len, - const bool strict = true, - const bool allow_exceptions = true) - { - return from_bson(ptr, ptr + len, strict, allow_exceptions); - } - - JSON_HEDLEY_WARN_UNUSED_RESULT - JSON_HEDLEY_DEPRECATED_FOR(3.8.0, from_bson(ptr, ptr + len)) - static basic_json from_bson(detail::span_input_adapter&& i, - const bool strict = true, - const bool allow_exceptions = true) - { - basic_json result; - detail::json_sax_dom_parser sdp(result, allow_exceptions); - auto ia = i.get(); - // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - const bool res = binary_reader(std::move(ia)).sax_parse(input_format_t::bson, &sdp, strict); - return res ? result : basic_json(value_t::discarded); - } - /// @} - - ////////////////////////// - // JSON Pointer support // - ////////////////////////// - - /// @name JSON Pointer functions - /// @{ - - /*! - @brief access specified element via JSON Pointer - - Uses a JSON pointer to retrieve a reference to the respective JSON value. - No bound checking is performed. Similar to @ref operator[](const typename - object_t::key_type&), `null` values are created in arrays and objects if - necessary. - - In particular: - - If the JSON pointer points to an object key that does not exist, it - is created an filled with a `null` value before a reference to it - is returned. - - If the JSON pointer points to an array index that does not exist, it - is created an filled with a `null` value before a reference to it - is returned. All indices between the current maximum and the given - index are also filled with `null`. - - The special value `-` is treated as a synonym for the index past the - end. - - @param[in] ptr a JSON pointer - - @return reference to the element pointed to by @a ptr - - @complexity Constant. - - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - @throw out_of_range.404 if the JSON pointer can not be resolved - - @liveexample{The behavior is shown in the example.,operatorjson_pointer} - - @since version 2.0.0 - */ - reference operator[](const json_pointer& ptr) - { - return ptr.get_unchecked(this); - } - - /*! - @brief access specified element via JSON Pointer - - Uses a JSON pointer to retrieve a reference to the respective JSON value. - No bound checking is performed. The function does not change the JSON - value; no `null` values are created. In particular, the special value - `-` yields an exception. - - @param[in] ptr JSON pointer to the desired element - - @return const reference to the element pointed to by @a ptr - - @complexity Constant. - - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - @throw out_of_range.402 if the array index '-' is used - @throw out_of_range.404 if the JSON pointer can not be resolved - - @liveexample{The behavior is shown in the example.,operatorjson_pointer_const} - - @since version 2.0.0 - */ - const_reference operator[](const json_pointer& ptr) const - { - return ptr.get_unchecked(this); - } - - /*! - @brief access specified element via JSON Pointer - - Returns a reference to the element at with specified JSON pointer @a ptr, - with bounds checking. - - @param[in] ptr JSON pointer to the desired element - - @return reference to the element pointed to by @a ptr - - @throw parse_error.106 if an array index in the passed JSON pointer @a ptr - begins with '0'. See example below. - - @throw parse_error.109 if an array index in the passed JSON pointer @a ptr - is not a number. See example below. - - @throw out_of_range.401 if an array index in the passed JSON pointer @a ptr - is out of range. See example below. - - @throw out_of_range.402 if the array index '-' is used in the passed JSON - pointer @a ptr. As `at` provides checked access (and no elements are - implicitly inserted), the index '-' is always invalid. See example below. - - @throw out_of_range.403 if the JSON pointer describes a key of an object - which cannot be found. See example below. - - @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved. - See example below. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes in the JSON value. - - @complexity Constant. - - @since version 2.0.0 - - @liveexample{The behavior is shown in the example.,at_json_pointer} - */ - reference at(const json_pointer& ptr) - { - return ptr.get_checked(this); - } - - /*! - @brief access specified element via JSON Pointer - - Returns a const reference to the element at with specified JSON pointer @a - ptr, with bounds checking. - - @param[in] ptr JSON pointer to the desired element - - @return reference to the element pointed to by @a ptr - - @throw parse_error.106 if an array index in the passed JSON pointer @a ptr - begins with '0'. See example below. - - @throw parse_error.109 if an array index in the passed JSON pointer @a ptr - is not a number. See example below. - - @throw out_of_range.401 if an array index in the passed JSON pointer @a ptr - is out of range. See example below. - - @throw out_of_range.402 if the array index '-' is used in the passed JSON - pointer @a ptr. As `at` provides checked access (and no elements are - implicitly inserted), the index '-' is always invalid. See example below. - - @throw out_of_range.403 if the JSON pointer describes a key of an object - which cannot be found. See example below. - - @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved. - See example below. - - @exceptionsafety Strong guarantee: if an exception is thrown, there are no - changes in the JSON value. - - @complexity Constant. - - @since version 2.0.0 - - @liveexample{The behavior is shown in the example.,at_json_pointer_const} - */ - const_reference at(const json_pointer& ptr) const - { - return ptr.get_checked(this); - } - - /*! - @brief return flattened JSON value - - The function creates a JSON object whose keys are JSON pointers (see [RFC - 6901](https://tools.ietf.org/html/rfc6901)) and whose values are all - primitive. The original JSON value can be restored using the @ref - unflatten() function. - - @return an object that maps JSON pointers to primitive values - - @note Empty objects and arrays are flattened to `null` and will not be - reconstructed correctly by the @ref unflatten() function. - - @complexity Linear in the size the JSON value. - - @liveexample{The following code shows how a JSON object is flattened to an - object whose keys consist of JSON pointers.,flatten} - - @sa see @ref unflatten() for the reverse function - - @since version 2.0.0 - */ - basic_json flatten() const - { - basic_json result(value_t::object); - json_pointer::flatten("", *this, result); - return result; - } - - /*! - @brief unflatten a previously flattened JSON value - - The function restores the arbitrary nesting of a JSON value that has been - flattened before using the @ref flatten() function. The JSON value must - meet certain constraints: - 1. The value must be an object. - 2. The keys must be JSON pointers (see - [RFC 6901](https://tools.ietf.org/html/rfc6901)) - 3. The mapped values must be primitive JSON types. - - @return the original JSON from a flattened version - - @note Empty objects and arrays are flattened by @ref flatten() to `null` - values and can not unflattened to their original type. Apart from - this example, for a JSON value `j`, the following is always true: - `j == j.flatten().unflatten()`. - - @complexity Linear in the size the JSON value. - - @throw type_error.314 if value is not an object - @throw type_error.315 if object values are not primitive - - @liveexample{The following code shows how a flattened JSON object is - unflattened into the original nested JSON object.,unflatten} - - @sa see @ref flatten() for the reverse function - - @since version 2.0.0 - */ - basic_json unflatten() const - { - return json_pointer::unflatten(*this); - } - - /// @} - - ////////////////////////// - // JSON Patch functions // - ////////////////////////// - - /// @name JSON Patch functions - /// @{ - - /*! - @brief applies a JSON patch - - [JSON Patch](http://jsonpatch.com) defines a JSON document structure for - expressing a sequence of operations to apply to a JSON) document. With - this function, a JSON Patch is applied to the current JSON value by - executing all operations from the patch. - - @param[in] json_patch JSON patch document - @return patched document - - @note The application of a patch is atomic: Either all operations succeed - and the patched document is returned or an exception is thrown. In - any case, the original value is not changed: the patch is applied - to a copy of the value. - - @throw parse_error.104 if the JSON patch does not consist of an array of - objects - - @throw parse_error.105 if the JSON patch is malformed (e.g., mandatory - attributes are missing); example: `"operation add must have member path"` - - @throw out_of_range.401 if an array index is out of range. - - @throw out_of_range.403 if a JSON pointer inside the patch could not be - resolved successfully in the current JSON value; example: `"key baz not - found"` - - @throw out_of_range.405 if JSON pointer has no parent ("add", "remove", - "move") - - @throw other_error.501 if "test" operation was unsuccessful - - @complexity Linear in the size of the JSON value and the length of the - JSON patch. As usually only a fraction of the JSON value is affected by - the patch, the complexity can usually be neglected. - - @liveexample{The following code shows how a JSON patch is applied to a - value.,patch} - - @sa see @ref diff -- create a JSON patch by comparing two JSON values - - @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902) - @sa [RFC 6901 (JSON Pointer)](https://tools.ietf.org/html/rfc6901) - - @since version 2.0.0 - */ - basic_json patch(const basic_json& json_patch) const - { - // make a working copy to apply the patch to - basic_json result = *this; - - // the valid JSON Patch operations - enum class patch_operations {add, remove, replace, move, copy, test, invalid}; - - const auto get_op = [](const std::string & op) - { - if (op == "add") - { - return patch_operations::add; - } - if (op == "remove") - { - return patch_operations::remove; - } - if (op == "replace") - { - return patch_operations::replace; - } - if (op == "move") - { - return patch_operations::move; - } - if (op == "copy") - { - return patch_operations::copy; - } - if (op == "test") - { - return patch_operations::test; - } - - return patch_operations::invalid; - }; - - // wrapper for "add" operation; add value at ptr - const auto operation_add = [&result](json_pointer & ptr, basic_json val) - { - // adding to the root of the target document means replacing it - if (ptr.empty()) - { - result = val; - return; - } - - // make sure the top element of the pointer exists - json_pointer top_pointer = ptr.top(); - if (top_pointer != ptr) - { - result.at(top_pointer); - } - - // get reference to parent of JSON pointer ptr - const auto last_path = ptr.back(); - ptr.pop_back(); - basic_json& parent = result[ptr]; - - switch (parent.m_type) - { - case value_t::null: - case value_t::object: - { - // use operator[] to add value - parent[last_path] = val; - break; - } - - case value_t::array: - { - if (last_path == "-") - { - // special case: append to back - parent.push_back(val); - } - else - { - const auto idx = json_pointer::array_index(last_path); - if (JSON_HEDLEY_UNLIKELY(idx > parent.size())) - { - // avoid undefined behavior - JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", parent)); - } - - // default case: insert add offset - parent.insert(parent.begin() + static_cast(idx), val); - } - break; - } - - // if there exists a parent it cannot be primitive - case value_t::string: // LCOV_EXCL_LINE - case value_t::boolean: // LCOV_EXCL_LINE - case value_t::number_integer: // LCOV_EXCL_LINE - case value_t::number_unsigned: // LCOV_EXCL_LINE - case value_t::number_float: // LCOV_EXCL_LINE - case value_t::binary: // LCOV_EXCL_LINE - case value_t::discarded: // LCOV_EXCL_LINE - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - } - }; - - // wrapper for "remove" operation; remove value at ptr - const auto operation_remove = [this, &result](json_pointer & ptr) - { - // get reference to parent of JSON pointer ptr - const auto last_path = ptr.back(); - ptr.pop_back(); - basic_json& parent = result.at(ptr); - - // remove child - if (parent.is_object()) - { - // perform range check - auto it = parent.find(last_path); - if (JSON_HEDLEY_LIKELY(it != parent.end())) - { - parent.erase(it); - } - else - { - JSON_THROW(out_of_range::create(403, "key '" + last_path + "' not found", *this)); - } - } - else if (parent.is_array()) - { - // note erase performs range check - parent.erase(json_pointer::array_index(last_path)); - } - }; - - // type check: top level value must be an array - if (JSON_HEDLEY_UNLIKELY(!json_patch.is_array())) - { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", json_patch)); - } - - // iterate and apply the operations - for (const auto& val : json_patch) - { - // wrapper to get a value for an operation - const auto get_value = [&val](const std::string & op, - const std::string & member, - bool string_type) -> basic_json & - { - // find value - auto it = val.m_value.object->find(member); - - // context-sensitive error message - const auto error_msg = (op == "op") ? "operation" : "operation '" + op + "'"; - - // check if desired value is present - if (JSON_HEDLEY_UNLIKELY(it == val.m_value.object->end())) - { - // NOLINTNEXTLINE(performance-inefficient-string-concatenation) - JSON_THROW(parse_error::create(105, 0, error_msg + " must have member '" + member + "'", val)); - } - - // check if result is of type string - if (JSON_HEDLEY_UNLIKELY(string_type && !it->second.is_string())) - { - // NOLINTNEXTLINE(performance-inefficient-string-concatenation) - JSON_THROW(parse_error::create(105, 0, error_msg + " must have string member '" + member + "'", val)); - } - - // no error: return value - return it->second; - }; - - // type check: every element of the array must be an object - if (JSON_HEDLEY_UNLIKELY(!val.is_object())) - { - JSON_THROW(parse_error::create(104, 0, "JSON patch must be an array of objects", val)); - } - - // collect mandatory members - const auto op = get_value("op", "op", true).template get(); - const auto path = get_value(op, "path", true).template get(); - json_pointer ptr(path); - - switch (get_op(op)) - { - case patch_operations::add: - { - operation_add(ptr, get_value("add", "value", false)); - break; - } - - case patch_operations::remove: - { - operation_remove(ptr); - break; - } - - case patch_operations::replace: - { - // the "path" location must exist - use at() - result.at(ptr) = get_value("replace", "value", false); - break; - } - - case patch_operations::move: - { - const auto from_path = get_value("move", "from", true).template get(); - json_pointer from_ptr(from_path); - - // the "from" location must exist - use at() - basic_json v = result.at(from_ptr); - - // The move operation is functionally identical to a - // "remove" operation on the "from" location, followed - // immediately by an "add" operation at the target - // location with the value that was just removed. - operation_remove(from_ptr); - operation_add(ptr, v); - break; - } - - case patch_operations::copy: - { - const auto from_path = get_value("copy", "from", true).template get(); - const json_pointer from_ptr(from_path); - - // the "from" location must exist - use at() - basic_json v = result.at(from_ptr); - - // The copy is functionally identical to an "add" - // operation at the target location using the value - // specified in the "from" member. - operation_add(ptr, v); - break; - } - - case patch_operations::test: - { - bool success = false; - JSON_TRY - { - // check if "value" matches the one at "path" - // the "path" location must exist - use at() - success = (result.at(ptr) == get_value("test", "value", false)); - } - JSON_INTERNAL_CATCH (out_of_range&) - { - // ignore out of range errors: success remains false - } - - // throw an exception if test fails - if (JSON_HEDLEY_UNLIKELY(!success)) - { - JSON_THROW(other_error::create(501, "unsuccessful: " + val.dump(), val)); - } - - break; - } - - case patch_operations::invalid: - default: - { - // op must be "add", "remove", "replace", "move", "copy", or - // "test" - JSON_THROW(parse_error::create(105, 0, "operation value '" + op + "' is invalid", val)); - } - } - } - - return result; - } - - /*! - @brief creates a diff as a JSON patch - - Creates a [JSON Patch](http://jsonpatch.com) so that value @a source can - be changed into the value @a target by calling @ref patch function. - - @invariant For two JSON values @a source and @a target, the following code - yields always `true`: - @code {.cpp} - source.patch(diff(source, target)) == target; - @endcode - - @note Currently, only `remove`, `add`, and `replace` operations are - generated. - - @param[in] source JSON value to compare from - @param[in] target JSON value to compare against - @param[in] path helper value to create JSON pointers - - @return a JSON patch to convert the @a source to @a target - - @complexity Linear in the lengths of @a source and @a target. - - @liveexample{The following code shows how a JSON patch is created as a - diff for two JSON values.,diff} - - @sa see @ref patch -- apply a JSON patch - @sa see @ref merge_patch -- apply a JSON Merge Patch - - @sa [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902) - - @since version 2.0.0 - */ - JSON_HEDLEY_WARN_UNUSED_RESULT - static basic_json diff(const basic_json& source, const basic_json& target, - const std::string& path = "") - { - // the patch - basic_json result(value_t::array); - - // if the values are the same, return empty patch - if (source == target) - { - return result; - } - - if (source.type() != target.type()) - { - // different types: replace value - result.push_back( - { - {"op", "replace"}, {"path", path}, {"value", target} - }); - return result; - } - - switch (source.type()) - { - case value_t::array: - { - // first pass: traverse common elements - std::size_t i = 0; - while (i < source.size() && i < target.size()) - { - // recursive call to compare array values at index i - auto temp_diff = diff(source[i], target[i], path + "/" + std::to_string(i)); - result.insert(result.end(), temp_diff.begin(), temp_diff.end()); - ++i; - } - - // i now reached the end of at least one array - // in a second pass, traverse the remaining elements - - // remove my remaining elements - const auto end_index = static_cast(result.size()); - while (i < source.size()) - { - // add operations in reverse order to avoid invalid - // indices - result.insert(result.begin() + end_index, object( - { - {"op", "remove"}, - {"path", path + "/" + std::to_string(i)} - })); - ++i; - } - - // add other remaining elements - while (i < target.size()) - { - result.push_back( - { - {"op", "add"}, - {"path", path + "/-"}, - {"value", target[i]} - }); - ++i; - } - - break; - } - - case value_t::object: - { - // first pass: traverse this object's elements - for (auto it = source.cbegin(); it != source.cend(); ++it) - { - // escape the key name to be used in a JSON patch - const auto path_key = path + "/" + detail::escape(it.key()); - - if (target.find(it.key()) != target.end()) - { - // recursive call to compare object values at key it - auto temp_diff = diff(it.value(), target[it.key()], path_key); - result.insert(result.end(), temp_diff.begin(), temp_diff.end()); - } - else - { - // found a key that is not in o -> remove it - result.push_back(object( - { - {"op", "remove"}, {"path", path_key} - })); - } - } - - // second pass: traverse other object's elements - for (auto it = target.cbegin(); it != target.cend(); ++it) - { - if (source.find(it.key()) == source.end()) - { - // found a key that is not in this -> add it - const auto path_key = path + "/" + detail::escape(it.key()); - result.push_back( - { - {"op", "add"}, {"path", path_key}, - {"value", it.value()} - }); - } - } - - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - // both primitive type: replace value - result.push_back( - { - {"op", "replace"}, {"path", path}, {"value", target} - }); - break; - } - } - - return result; - } - - /// @} - - //////////////////////////////// - // JSON Merge Patch functions // - //////////////////////////////// - - /// @name JSON Merge Patch functions - /// @{ - - /*! - @brief applies a JSON Merge Patch - - The merge patch format is primarily intended for use with the HTTP PATCH - method as a means of describing a set of modifications to a target - resource's content. This function applies a merge patch to the current - JSON value. - - The function implements the following algorithm from Section 2 of - [RFC 7396 (JSON Merge Patch)](https://tools.ietf.org/html/rfc7396): - - ``` - define MergePatch(Target, Patch): - if Patch is an Object: - if Target is not an Object: - Target = {} // Ignore the contents and set it to an empty Object - for each Name/Value pair in Patch: - if Value is null: - if Name exists in Target: - remove the Name/Value pair from Target - else: - Target[Name] = MergePatch(Target[Name], Value) - return Target - else: - return Patch - ``` - - Thereby, `Target` is the current object; that is, the patch is applied to - the current value. - - @param[in] apply_patch the patch to apply - - @complexity Linear in the lengths of @a patch. - - @liveexample{The following code shows how a JSON Merge Patch is applied to - a JSON document.,merge_patch} - - @sa see @ref patch -- apply a JSON patch - @sa [RFC 7396 (JSON Merge Patch)](https://tools.ietf.org/html/rfc7396) - - @since version 3.0.0 - */ - void merge_patch(const basic_json& apply_patch) - { - if (apply_patch.is_object()) - { - if (!is_object()) - { - *this = object(); - } - for (auto it = apply_patch.begin(); it != apply_patch.end(); ++it) - { - if (it.value().is_null()) - { - erase(it.key()); - } - else - { - operator[](it.key()).merge_patch(it.value()); - } - } - } - else - { - *this = apply_patch; - } - } - - /// @} -}; - -/*! -@brief user-defined to_string function for JSON values - -This function implements a user-defined to_string for JSON objects. - -@param[in] j a JSON object -@return a std::string object -*/ - -NLOHMANN_BASIC_JSON_TPL_DECLARATION -std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j) -{ - return j.dump(); -} -} // namespace nlohmann - -/////////////////////// -// nonmember support // -/////////////////////// - -// specialization of std::swap, and std::hash -namespace std -{ - -/// hash value for JSON objects -template<> -struct hash -{ - /*! - @brief return a hash value for a JSON object - - @since version 1.0.0 - */ - std::size_t operator()(const nlohmann::json& j) const - { - return nlohmann::detail::hash(j); - } -}; - -/// specialization for std::less -/// @note: do not remove the space after '<', -/// see https://github.com/nlohmann/json/pull/679 -template<> -struct less<::nlohmann::detail::value_t> -{ - /*! - @brief compare two value_t enum values - @since version 3.0.0 - */ - bool operator()(nlohmann::detail::value_t lhs, - nlohmann::detail::value_t rhs) const noexcept - { - return nlohmann::detail::operator<(lhs, rhs); - } -}; - -// C++20 prohibit function specialization in the std namespace. -#ifndef JSON_HAS_CPP_20 - -/*! -@brief exchanges the values of two JSON objects - -@since version 1.0.0 -*/ -template<> -inline void swap(nlohmann::json& j1, nlohmann::json& j2) noexcept( // NOLINT(readability-inconsistent-declaration-parameter-name) - is_nothrow_move_constructible::value&& // NOLINT(misc-redundant-expression) - is_nothrow_move_assignable::value - ) -{ - j1.swap(j2); -} - -#endif - -} // namespace std - -/*! -@brief user-defined string literal for JSON values - -This operator implements a user-defined string literal for JSON objects. It -can be used by adding `"_json"` to a string literal and returns a JSON object -if no parse error occurred. - -@param[in] s a string representation of a JSON object -@param[in] n the length of string @a s -@return a JSON object - -@since version 1.0.0 -*/ -JSON_HEDLEY_NON_NULL(1) -inline nlohmann::json operator "" _json(const char* s, std::size_t n) -{ - return nlohmann::json::parse(s, s + n); -} - -/*! -@brief user-defined string literal for JSON pointer - -This operator implements a user-defined string literal for JSON Pointers. It -can be used by adding `"_json_pointer"` to a string literal and returns a JSON pointer -object if no parse error occurred. - -@param[in] s a string representation of a JSON Pointer -@param[in] n the length of string @a s -@return a JSON pointer object - -@since version 2.0.0 -*/ -JSON_HEDLEY_NON_NULL(1) -inline nlohmann::json::json_pointer operator "" _json_pointer(const char* s, std::size_t n) -{ - return nlohmann::json::json_pointer(std::string(s, n)); -} - -// #include - - -// restore clang diagnostic settings -#if defined(__clang__) - #pragma clang diagnostic pop -#endif - -// clean up -#undef JSON_ASSERT -#undef JSON_INTERNAL_CATCH -#undef JSON_CATCH -#undef JSON_THROW -#undef JSON_TRY -#undef JSON_PRIVATE_UNLESS_TESTED -#undef JSON_HAS_CPP_11 -#undef JSON_HAS_CPP_14 -#undef JSON_HAS_CPP_17 -#undef JSON_HAS_CPP_20 -#undef NLOHMANN_BASIC_JSON_TPL_DECLARATION -#undef NLOHMANN_BASIC_JSON_TPL -#undef JSON_EXPLICIT -#undef NLOHMANN_CAN_CALL_STD_FUNC_IMPL - -// #include - - -#undef JSON_HEDLEY_ALWAYS_INLINE -#undef JSON_HEDLEY_ARM_VERSION -#undef JSON_HEDLEY_ARM_VERSION_CHECK -#undef JSON_HEDLEY_ARRAY_PARAM -#undef JSON_HEDLEY_ASSUME -#undef JSON_HEDLEY_BEGIN_C_DECLS -#undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE -#undef JSON_HEDLEY_CLANG_HAS_BUILTIN -#undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE -#undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE -#undef JSON_HEDLEY_CLANG_HAS_EXTENSION -#undef JSON_HEDLEY_CLANG_HAS_FEATURE -#undef JSON_HEDLEY_CLANG_HAS_WARNING -#undef JSON_HEDLEY_COMPCERT_VERSION -#undef JSON_HEDLEY_COMPCERT_VERSION_CHECK -#undef JSON_HEDLEY_CONCAT -#undef JSON_HEDLEY_CONCAT3 -#undef JSON_HEDLEY_CONCAT3_EX -#undef JSON_HEDLEY_CONCAT_EX -#undef JSON_HEDLEY_CONST -#undef JSON_HEDLEY_CONSTEXPR -#undef JSON_HEDLEY_CONST_CAST -#undef JSON_HEDLEY_CPP_CAST -#undef JSON_HEDLEY_CRAY_VERSION -#undef JSON_HEDLEY_CRAY_VERSION_CHECK -#undef JSON_HEDLEY_C_DECL -#undef JSON_HEDLEY_DEPRECATED -#undef JSON_HEDLEY_DEPRECATED_FOR -#undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CAST_QUAL -#undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_CPP98_COMPAT_WRAP_ -#undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_DEPRECATED -#undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_CPP_ATTRIBUTES -#undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNKNOWN_PRAGMAS -#undef JSON_HEDLEY_DIAGNOSTIC_DISABLE_UNUSED_FUNCTION -#undef JSON_HEDLEY_DIAGNOSTIC_POP -#undef JSON_HEDLEY_DIAGNOSTIC_PUSH -#undef JSON_HEDLEY_DMC_VERSION -#undef JSON_HEDLEY_DMC_VERSION_CHECK -#undef JSON_HEDLEY_EMPTY_BASES -#undef JSON_HEDLEY_EMSCRIPTEN_VERSION -#undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK -#undef JSON_HEDLEY_END_C_DECLS -#undef JSON_HEDLEY_FLAGS -#undef JSON_HEDLEY_FLAGS_CAST -#undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE -#undef JSON_HEDLEY_GCC_HAS_BUILTIN -#undef JSON_HEDLEY_GCC_HAS_CPP_ATTRIBUTE -#undef JSON_HEDLEY_GCC_HAS_DECLSPEC_ATTRIBUTE -#undef JSON_HEDLEY_GCC_HAS_EXTENSION -#undef JSON_HEDLEY_GCC_HAS_FEATURE -#undef JSON_HEDLEY_GCC_HAS_WARNING -#undef JSON_HEDLEY_GCC_NOT_CLANG_VERSION_CHECK -#undef JSON_HEDLEY_GCC_VERSION -#undef JSON_HEDLEY_GCC_VERSION_CHECK -#undef JSON_HEDLEY_GNUC_HAS_ATTRIBUTE -#undef JSON_HEDLEY_GNUC_HAS_BUILTIN -#undef JSON_HEDLEY_GNUC_HAS_CPP_ATTRIBUTE -#undef JSON_HEDLEY_GNUC_HAS_DECLSPEC_ATTRIBUTE -#undef JSON_HEDLEY_GNUC_HAS_EXTENSION -#undef JSON_HEDLEY_GNUC_HAS_FEATURE -#undef JSON_HEDLEY_GNUC_HAS_WARNING -#undef JSON_HEDLEY_GNUC_VERSION -#undef JSON_HEDLEY_GNUC_VERSION_CHECK -#undef JSON_HEDLEY_HAS_ATTRIBUTE -#undef JSON_HEDLEY_HAS_BUILTIN -#undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE -#undef JSON_HEDLEY_HAS_CPP_ATTRIBUTE_NS -#undef JSON_HEDLEY_HAS_DECLSPEC_ATTRIBUTE -#undef JSON_HEDLEY_HAS_EXTENSION -#undef JSON_HEDLEY_HAS_FEATURE -#undef JSON_HEDLEY_HAS_WARNING -#undef JSON_HEDLEY_IAR_VERSION -#undef JSON_HEDLEY_IAR_VERSION_CHECK -#undef JSON_HEDLEY_IBM_VERSION -#undef JSON_HEDLEY_IBM_VERSION_CHECK -#undef JSON_HEDLEY_IMPORT -#undef JSON_HEDLEY_INLINE -#undef JSON_HEDLEY_INTEL_CL_VERSION -#undef JSON_HEDLEY_INTEL_CL_VERSION_CHECK -#undef JSON_HEDLEY_INTEL_VERSION -#undef JSON_HEDLEY_INTEL_VERSION_CHECK -#undef JSON_HEDLEY_IS_CONSTANT -#undef JSON_HEDLEY_IS_CONSTEXPR_ -#undef JSON_HEDLEY_LIKELY -#undef JSON_HEDLEY_MALLOC -#undef JSON_HEDLEY_MCST_LCC_VERSION -#undef JSON_HEDLEY_MCST_LCC_VERSION_CHECK -#undef JSON_HEDLEY_MESSAGE -#undef JSON_HEDLEY_MSVC_VERSION -#undef JSON_HEDLEY_MSVC_VERSION_CHECK -#undef JSON_HEDLEY_NEVER_INLINE -#undef JSON_HEDLEY_NON_NULL -#undef JSON_HEDLEY_NO_ESCAPE -#undef JSON_HEDLEY_NO_RETURN -#undef JSON_HEDLEY_NO_THROW -#undef JSON_HEDLEY_NULL -#undef JSON_HEDLEY_PELLES_VERSION -#undef JSON_HEDLEY_PELLES_VERSION_CHECK -#undef JSON_HEDLEY_PGI_VERSION -#undef JSON_HEDLEY_PGI_VERSION_CHECK -#undef JSON_HEDLEY_PREDICT -#undef JSON_HEDLEY_PRINTF_FORMAT -#undef JSON_HEDLEY_PRIVATE -#undef JSON_HEDLEY_PUBLIC -#undef JSON_HEDLEY_PURE -#undef JSON_HEDLEY_REINTERPRET_CAST -#undef JSON_HEDLEY_REQUIRE -#undef JSON_HEDLEY_REQUIRE_CONSTEXPR -#undef JSON_HEDLEY_REQUIRE_MSG -#undef JSON_HEDLEY_RESTRICT -#undef JSON_HEDLEY_RETURNS_NON_NULL -#undef JSON_HEDLEY_SENTINEL -#undef JSON_HEDLEY_STATIC_ASSERT -#undef JSON_HEDLEY_STATIC_CAST -#undef JSON_HEDLEY_STRINGIFY -#undef JSON_HEDLEY_STRINGIFY_EX -#undef JSON_HEDLEY_SUNPRO_VERSION -#undef JSON_HEDLEY_SUNPRO_VERSION_CHECK -#undef JSON_HEDLEY_TINYC_VERSION -#undef JSON_HEDLEY_TINYC_VERSION_CHECK -#undef JSON_HEDLEY_TI_ARMCL_VERSION -#undef JSON_HEDLEY_TI_ARMCL_VERSION_CHECK -#undef JSON_HEDLEY_TI_CL2000_VERSION -#undef JSON_HEDLEY_TI_CL2000_VERSION_CHECK -#undef JSON_HEDLEY_TI_CL430_VERSION -#undef JSON_HEDLEY_TI_CL430_VERSION_CHECK -#undef JSON_HEDLEY_TI_CL6X_VERSION -#undef JSON_HEDLEY_TI_CL6X_VERSION_CHECK -#undef JSON_HEDLEY_TI_CL7X_VERSION -#undef JSON_HEDLEY_TI_CL7X_VERSION_CHECK -#undef JSON_HEDLEY_TI_CLPRU_VERSION -#undef JSON_HEDLEY_TI_CLPRU_VERSION_CHECK -#undef JSON_HEDLEY_TI_VERSION -#undef JSON_HEDLEY_TI_VERSION_CHECK -#undef JSON_HEDLEY_UNAVAILABLE -#undef JSON_HEDLEY_UNLIKELY -#undef JSON_HEDLEY_UNPREDICTABLE -#undef JSON_HEDLEY_UNREACHABLE -#undef JSON_HEDLEY_UNREACHABLE_RETURN -#undef JSON_HEDLEY_VERSION -#undef JSON_HEDLEY_VERSION_DECODE_MAJOR -#undef JSON_HEDLEY_VERSION_DECODE_MINOR -#undef JSON_HEDLEY_VERSION_DECODE_REVISION -#undef JSON_HEDLEY_VERSION_ENCODE -#undef JSON_HEDLEY_WARNING -#undef JSON_HEDLEY_WARN_UNUSED_RESULT -#undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG -#undef JSON_HEDLEY_FALL_THROUGH - - - -#endif // INCLUDE_NLOHMANN_JSON_HPP_ diff --git a/include/jvm.h b/include/jvm.h deleted file mode 100644 index e69de29b..00000000 diff --git a/include/pe-dotnet.h b/include/pe-dotnet.h deleted file mode 100644 index 1f36901c..00000000 --- a/include/pe-dotnet.h +++ /dev/null @@ -1,420 +0,0 @@ -#ifndef DOTNET_H -#define DOTNET_H - -#include "pe.h" -#include "common.h" -#include - -#ifdef _WIN32 -#define BINLEX_EXPORT __declspec(dllexport) -#else -#define BINLEX_EXPORT -#endif - -using namespace binlex; - -#define MODULE 0 -#define TYPE_REF 1 -#define TYPE_DEF 2 -#define FIELD_PTR 3 -#define FIELD 4 -#define METHOD_PTR 5 -#define METHOD_DEF 6 -#define PARAMPTR 7 -#define PARAM 8 -#define INTERFACEIMPL 9 -#define MEMBERREF 10 -#define CONSTANT 11 -#define CUSTOMATTRIBUTE 12 -#define FIELDMARSHAL 13 -#define DECLSECURITY 14 -#define CLASSLAYOUT 15 -#define FIELDLAYOUT 16 -#define STANDALONESIG 17 -#define EVENTMAP 18 -#define EVENTPTR 19 -#define EVENT 20 -#define PROPERTYMAP 21 -#define PROPERTYPTR 22 -#define PROPERTY 23 -#define METHODSEMANTICS 24 -#define METHODIMPL 25 -#define MODULE_REF 26 -#define TYPE_SPEC 27 -#define IMPLMAP 28 -#define FIELDRVA 29 -#define ENCLOG 30 -#define ENCMAP 31 -#define ASSEMBLY 32 -#define ASSEMBLYPROCESSOR 33 -#define ASSEMBLYOS 34 -#define ASSEMBLY_REF 35 -#define ASSEMBLYREFPROCESSOR 36 -#define ASSEMBLYREFOS 37 -#define _FILE 38 -#define EXPORTEDTYPE 39 -#define MANIFESTRESOURCE 40 -#define NESTEDCLASS 41 -#define GENERICPARAM 42 -#define METHODSPEC 43 -#define GENERICPARAMCONSTRAINT 44 -#define DOCUMENT 48 -#define METHODDEBUGINFORMATION 49 -#define LOCALSCOPE 50 -#define LOCALVARIABLE 51 -#define LOCALCONSTANT 52 -#define IMPORTSCOPE 53 -#define STATEMACHINEMETHOD 54 -#define CUSTOMDEBUGINFORMATION 55 - - -namespace dotnet { - class MethodHeader {}; - - class TinyHeader : public MethodHeader { - uint32_t size; - }; - - class FatHeader : public MethodHeader { - uint32_t size; - uint16_t max_stack; - uint16_t local_var_sig_tok; - }; - - class Method { - bool tiny_header = false; - MethodHeader *header; - vector code; - }; - - class MultiTableIndex { - private: - vector refs = {}; - virtual vector GetRefs() { return refs; }; - uint32_t MaxTableEntries(uint32_t *table_entries) { - uint32_t max_entries = 0; - for (uint8_t i = 0; i < GetRefs().size(); i++) { - if (table_entries[GetRefs()[i]] > max_entries) max_entries = table_entries[GetRefs()[i]]; - } - return max_entries; - }; - public: - uint32_t size = 2; - uint32_t offset = 0; - uint32_t value = 0; - uint32_t Parse(char *&buffer, uint32_t *table_entries) { - uint32_t max_entry_number, needed_bits_for_tag, remaining_bits_for_indexing; - needed_bits_for_tag = uint32_t(log2(refs.size()) + 1); - max_entry_number = MaxTableEntries(table_entries); - remaining_bits_for_indexing = 16 - needed_bits_for_tag; - if ( pow(2, remaining_bits_for_indexing) < max_entry_number ) { - size = 4; - } - if (size == 2) offset = *(uint16_t *)buffer ; - if (size == 4) offset = *(uint32_t *)buffer ; - return size; - }; - }; - - class ResolutionScopeIndex : public MultiTableIndex { - private: - vector refs = { - MODULE, - MODULE_REF, - ASSEMBLY_REF, - TYPE_REF - }; - vector GetRefs() { return refs; }; - }; - - class TypeDefOrRefIndex: public MultiTableIndex { - private: - vector refs = { - TYPE_DEF, - TYPE_REF, - TYPE_SPEC, - }; - vector GetRefs() { return refs; }; - }; - - class SimpleTableIndex { - public: - uint32_t offset = 0; - uint32_t value = 0; - uint32_t size = 2; - uint32_t Parse(char *&buffer) { - offset = *(uint16_t *)buffer ; - return size; - }; - }; - - class StringHeapIndex { - public: - StringHeapIndex(uint8_t heap_size) { if ( heap_size & 1 ) size = 4; }; - uint32_t offset = 0; - uint32_t value = 0; - uint32_t size = 2; - uint32_t Parse(char *&buffer) { - if (size == 2) offset = *(uint16_t *)buffer; - if (size == 4) offset = *(uint32_t *)buffer ; - return size; - }; - }; - - class GuidHeapIndex { - public: - GuidHeapIndex(uint8_t heap_size) { if ( heap_size & 2 ) size = 4; }; - uint32_t offset = 0; - uint32_t value = 0; - uint32_t size = 2; - uint32_t Parse(char *&buffer) { - if (size == 2) offset = *(uint16_t *)buffer; - if (size == 4) offset = *(uint32_t *)buffer ; - return size; - }; - }; - - class BlobHeapIndex { - public: - BlobHeapIndex(uint8_t heap_size) { if ( heap_size & 3 ) size = 4; }; - uint32_t offset; - uint32_t value; - uint32_t size = 2; - uint32_t Parse(char *&buffer) { - if (size == 2) offset = *(uint16_t *)buffer; - if (size == 4) offset = *(uint32_t *)buffer ; - return size; - }; - }; - - class TableEntry { - public: - struct ParseArgs { - char *buff; - uint8_t heap_sizes; - uint32_t *table_entries; - }; - virtual uint32_t Parse(ParseArgs *args){ - return (uint32_t)args->heap_sizes*0; - } - static TableEntry* TableEntryFactory(uint8_t entry_type); - virtual ~TableEntry() { }; - }; - - class ModuleEntry: public TableEntry { - public: - uint32_t generation; - StringHeapIndex name = 0; - GuidHeapIndex mv_id = 0; - GuidHeapIndex enc_id = 0; - GuidHeapIndex enc_base_id = 0; - uint32_t Parse(ParseArgs *args){ - char *buff_aux; - buff_aux = args->buff; - memcpy(&generation, buff_aux, 2); - buff_aux += 2; - name = StringHeapIndex(args->heap_sizes); - buff_aux += name.Parse(buff_aux); - mv_id = GuidHeapIndex(args->heap_sizes); - buff_aux += mv_id.Parse(buff_aux); - enc_id = GuidHeapIndex(args->heap_sizes); - buff_aux += enc_id.Parse(buff_aux); - enc_base_id = GuidHeapIndex(args->heap_sizes); - buff_aux += enc_base_id.Parse(buff_aux); - return buff_aux - args->buff; - }; - }; - - class TypeRefEntry: public TableEntry { - public: - ResolutionScopeIndex resolution_scope; - StringHeapIndex name = 0; - StringHeapIndex name_space = 0; - uint32_t Parse(ParseArgs *args){ - char *buff_aux; - buff_aux = args->buff; - resolution_scope = ResolutionScopeIndex(); - buff_aux += resolution_scope.Parse(buff_aux, args->table_entries); - name = StringHeapIndex(args->heap_sizes); - buff_aux += name.Parse(buff_aux); - name_space = StringHeapIndex(args->heap_sizes); - buff_aux += name_space.Parse(buff_aux); - return buff_aux - args->buff; - }; - }; - - class TypeDefEntry: public TableEntry { - public: - uint32_t flags = 0; - StringHeapIndex name = 0; - StringHeapIndex name_space = 0; - TypeDefOrRefIndex extends; - SimpleTableIndex field_list; - SimpleTableIndex method_list; - uint32_t Parse(ParseArgs *args){ - char *buff_aux; - buff_aux = args->buff; - flags = *(uint32_t *)buff_aux; - buff_aux += 4; - name = StringHeapIndex(args->heap_sizes); - buff_aux += name.Parse(buff_aux); - name_space = StringHeapIndex(args->heap_sizes); - buff_aux += name_space.Parse(buff_aux); - extends = TypeDefOrRefIndex(); - buff_aux += extends.Parse(buff_aux, args->table_entries); - field_list = SimpleTableIndex(); - buff_aux += field_list.Parse(buff_aux); - method_list = SimpleTableIndex(); - buff_aux += method_list.Parse(buff_aux); - return buff_aux - args->buff; - }; - }; - - class FieldPtrEntry: public TableEntry { - public: - uint16_t ref; - uint32_t Parse(ParseArgs *args){ - ref = *(uint16_t *)args->buff; - return 2; - }; - }; - - class FieldEntry: public TableEntry { - public: - uint16_t flags; - StringHeapIndex name = 0; - BlobHeapIndex signature = 0; - uint32_t Parse(ParseArgs *args){ - char *buff_aux; - buff_aux = args->buff; - flags = *(uint16_t *)buff_aux; - buff_aux += 2; - name = StringHeapIndex(args->heap_sizes); - buff_aux += name.Parse(buff_aux); - signature = BlobHeapIndex(args->heap_sizes); - buff_aux += signature.Parse(buff_aux); - return buff_aux - args->buff; - }; - }; - - class MethodPtrEntry: public TableEntry { - public: - uint16_t ref; - uint32_t Parse(ParseArgs *args){ - ref = *(uint16_t *)args->buff; - return 2; - }; - }; - - class MethodDefEntry: public TableEntry { - public: - uint32_t rva; - uint16_t impl_flags; - uint16_t flags; - StringHeapIndex name = 0; - BlobHeapIndex signature = 0; - SimpleTableIndex param_list; - uint32_t Parse(ParseArgs *args){ - char *buff_aux; - buff_aux = args->buff; - rva = *(uint32_t *)buff_aux; - buff_aux += 4; - impl_flags = *(uint16_t *)buff_aux; - buff_aux += 2; - flags = *(uint16_t *)buff_aux; - buff_aux += 2; - name = StringHeapIndex(args->heap_sizes); - buff_aux += name.Parse(buff_aux); - signature = BlobHeapIndex(args->heap_sizes); - buff_aux += signature.Parse(buff_aux); - param_list = SimpleTableIndex(); - buff_aux += param_list.Parse(buff_aux); - return buff_aux - args->buff; - }; - }; - - - class Cor20MetadataTable { - public: - uint32_t reserved; - uint8_t major_version; - uint8_t minor_version; - uint8_t heap_sizes; - uint8_t rid; - uint64_t mask_valid; - uint64_t mask_sorted; - uint32_t table_entries[8 * 8] = { 0 }; - vector tables[8 * 8]; - uint32_t ParseTablePointers(char *&buffer); - BINLEX_EXPORT uint32_t ParseTables(char *&buffer); - BINLEX_EXPORT ~Cor20MetadataTable(); - }; - - struct COR20_STREAM_HEADER { - uint32_t offset; - uint32_t size; - char *name; - }; - - struct COR20_STORAGE_HEADER { - uint8_t flags; - uint8_t pad; - uint16_t number_of_streams; - }; - - struct COR20_STORAGE_SIGNATURE { - uint32_t signature; - uint16_t major_version; - uint16_t minor_version; - uint32_t extra_data; - uint32_t version_string_size; - unsigned char *version_string; - }; - - struct COR20_HEADER { - uint32_t cb; - uint16_t major_runtime_version; - uint16_t minor_runtime_version; - uint32_t metadata_rva; - uint32_t metadata_size; - uint32_t flags; - uint32_t entry_point_token_rva; - uint32_t resources_rva; - uint32_t resources_size; - uint32_t strong_name_signature_rva; - uint32_t strong_name_signature_size; - uint32_t code_manager_table_rva; - uint32_t code_manager_table_size; - uint32_t vtable_fixups_rva; - uint32_t vtable_fixups_size; - uint32_t export_address_table_jumps_rva; - uint32_t export_address_table_jumps_size; - uint32_t managed_native_header_rva; - uint32_t managed_native_header_size; - }; -}; - -namespace binlex { - class DOTNET : public PE { - private: - void ParseSections(); - bool ParseCor20Header(); - bool ParseCor20StorageSignature(); - bool ParseCor20StorageHeader(); - bool ParseCor20StreamsHeader(); - bool ParseCor20MetadataStream(); - public: - dotnet::COR20_HEADER cor20_header = {}; - dotnet::COR20_STORAGE_SIGNATURE cor20_storage_signature = {}; - dotnet::COR20_STORAGE_HEADER cor20_storage_header = {}; - dotnet::COR20_STREAM_HEADER **StreamsHeader = {}; - dotnet::Cor20MetadataTable cor20_metadata_table; - - vector
    _sections; - BINLEX_EXPORT virtual bool ReadVector(const std::vector &data); - BINLEX_EXPORT bool Parse(); - BINLEX_EXPORT ~DOTNET(); - - }; -}; -#endif diff --git a/include/pe.h b/include/pe.h deleted file mode 100644 index 588c2114..00000000 --- a/include/pe.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef PE_H -#define PE_H - -#ifdef _WIN32 -#include -#include -#endif - -#include -#include -#include -#include -#include "common.h" -#include "file.h" -#include -#include - -#ifdef _WIN32 -#define BINLEX_EXPORT __declspec(dllexport) -#else -#define BINLEX_EXPORT -#endif - -using namespace std; -using namespace LIEF::PE; - -namespace binlex { - class PE : public File{ - /** - * This class is for reading PE files or buffers. - */ - private: - bool ParseSections(); - public: - #ifndef _WIN32 - MACHINE_TYPES mode = MACHINE_TYPES::IMAGE_FILE_MACHINE_UNKNOWN; - #else - MACHINE_TYPES mode = MACHINE_TYPES::IMAGE_FILE_MACHINE_UNKNOWN; - #endif - unique_ptr binary; - BINLEX_EXPORT PE(); - //struct Section sections[BINARY_MAX_SECTIONS]; - //uint32_t total_exec_sections; - /** - * Check if the PE file is a .NET file - * @return bool - */ - BINLEX_EXPORT bool IsDotNet(); - /** - * Check if the file has limitations that may result in invalid traits. - * @return bool - */ - BINLEX_EXPORT bool HasLimitations(); - /** - * Read data vector pointer. - * @param data vector of uint8_t data. - * @return bool - */ - virtual bool ReadVector(const std::vector &data); - BINLEX_EXPORT ~PE(); - }; -}; - -#endif diff --git a/include/raw.h b/include/raw.h deleted file mode 100644 index 4dfc934f..00000000 --- a/include/raw.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef RAW_H -#define RAW_H - -#include -#include -#include -#ifndef _WIN32 -#include -#endif // _WIN32 -#include -#include "file.h" - -#ifdef _WIN32 -#define BINLEX_EXPORT __declspec(dllexport) -#else -#define BINLEX_EXPORT -#endif - -#ifdef _WIN32 -typedef unsigned int uint; -#endif - -#ifdef _WIN32 -typedef unsigned int uint; -#endif - -namespace binlex{ - class Raw : public File{ - /** - * This class is used to read raw data. - */ - public: - /** - * Get the size of a file. - * @param fd file descriptor - * @return int result - */ - int GetFileSize(FILE *fd); - BINLEX_EXPORT Raw(); - /** - * Read data. - * @param data pointer to uint8_t vector - * @return bool - */ - BINLEX_EXPORT virtual bool ReadVector(const std::vector &data); - BINLEX_EXPORT ~Raw(); - }; -} - -#endif diff --git a/include/sha256.h b/include/sha256.h deleted file mode 100644 index 4d218d4c..00000000 --- a/include/sha256.h +++ /dev/null @@ -1,33 +0,0 @@ -/********************************************************************* -* Filename: sha256.h -* Author: Brad Conte (brad AT bradconte.com) -* Copyright: -* Disclaimer: This code is presented "as is" without any guarantees. -* Details: Defines the API for the corresponding SHA1 implementation. -*********************************************************************/ - -#ifndef SHA256_H -#define SHA256_H - -/*************************** HEADER FILES ***************************/ -#include -#include - -/****************************** MACROS ******************************/ -#define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest - -/**************************** DATA TYPES ****************************/ - -typedef struct { - uint8_t data[64]; - uint32_t datalen; - uint64_t bitlen; - uint32_t state[8]; -} SHA256_CTX; - -/*********************** FUNCTION DECLARATIONS **********************/ -void sha256_init(SHA256_CTX *ctx); -void sha256_update(SHA256_CTX *ctx, const uint8_t data[], size_t len); -void sha256_final(SHA256_CTX *ctx, uint8_t hash[]); - -#endif // SHA256_H diff --git a/libpybinlex/__init__.py b/libpybinlex/__init__.py deleted file mode 100644 index 314d7148..00000000 --- a/libpybinlex/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from libpybinlex import server -from libpybinlex import webapi diff --git a/libpybinlex/server.py b/libpybinlex/server.py deleted file mode 100755 index a7321e4e..00000000 --- a/libpybinlex/server.py +++ /dev/null @@ -1,153 +0,0 @@ -#!/usr/bin/env python - -import argparse -from flask import Flask, request -from flask_restx import Api, Resource, fields -from hashlib import sha256 -from pybinlex import Raw, PE, ELF, Disassembler -from pybinlex import BINARY_ARCH -from pybinlex import BINARY_MODE - -BINARY_MODE_32 = BINARY_MODE.BINARY_MODE_32 -BINARY_MODE_64 = BINARY_MODE.BINARY_MODE_64 -BINARY_ARCH_X86 = BINARY_ARCH.BINARY_ARCH_X86 - -__author__ = '@c3rb3ru5d3d53c' -__version__ = '1.1.1' - -app = Flask(__name__) - -api = Api(app) - -blapi = api.namespace('', description='Binlex Web API') - -def parse_mode(mode): - try: - return { - 'type': mode.split(':')[0], - 'arch': mode.split(':')[1] - } - except: return None - -def parse_tags(tags): - if tags in ['', None]: return None - return tags.split(',') - -def disasm_pe(corpus, mode, tags, data): - if mode['arch'] not in ['x86', 'x86_64', 'auto']: return [], 404 - if len(data) <= 0: return 422, [] - f = PE() - if mode['arch'] == 'x86': f.set_architecture(BINARY_ARCH_X86, BINARY_MODE_32) - if mode['arch'] == 'x86_64': f.set_architecture(BINARY_ARCH_X86, BINARY_MODE_64) - f.read_buffer(data) - d = Disassembler(f) - d.set_tags([]) - if tags is not None: d.set_tags(tags) - d.set_corpus(corpus) - if mode['arch'] != 'auto': d.set_mode(mode['type'] + ':' + mode['arch']) - d.disassemble() - return d.get_traits() - -def disasm_elf(corpus, mode, tags, data): - if mode['arch'] not in ['x86', 'x86_64', 'auto']: return [], 404 - f = ELF() - if mode['arch'] == 'x86': f.set_architecture(BINARY_ARCH_X86, BINARY_MODE_32) - if mode['arch'] == 'x86_64': f.set_architecture(BINARY_ARCH_X86, BINARY_MODE_64) - f.read_buffer(data) - d = Disassembler(f) - d.set_tags([]) - if tags is not None: d.set_tags(tags) - d.set_corpus(corpus) - if mode['arch'] != 'auto': d.set_mode(mode['type'] + ':' + mode['arch']) - d.disassemble() - return d.get_traits() - -def disasm_raw(corpus, mode, tags, data): - if mode['arch'] not in ['x86', 'x86_64']: return [], 404 - f = Raw() - if mode['arch'] == 'x86': f.set_architecture(BINARY_ARCH_X86, BINARY_MODE_32) - if mode['arch'] == 'x86_64': f.set_architecture(BINARY_ARCH_X86, BINARY_MODE_64) - f.read_buffer(data) - d = Disassembler(f) - d.set_tags([]) - if tags is not None: d.set_tags(tags) - d.set_corpus(corpus) - d.set_mode(mode['type'] + ':' + mode['arch']) - d.disassemble() - return d.get_traits() - -@blapi.route('/api/v1//') -@blapi.route('/api/v1///') -@blapi.param('mode', 'Trait disassembler mode') -@blapi.param('corpus', 'Corpus name') -@blapi.param('tags', 'List of tags delimited by commas (optional)') -class Disasm(Resource): - def post(self, corpus, mode, tags=None): - """ - Disassemble Traits - """ - mode = parse_mode(mode) - tags = parse_tags(tags) - if mode is None: return [], 404 - if corpus in ['', None]: return [], 404 - if len(request.data) <= 0 or request.data is None: return [], 422 - if mode['type'] == 'pe': return disasm_pe(corpus, mode, tags, request.data) - if mode['type'] == 'elf': return disasm_elf(corpus, mode, tags, request.data) - if mode['type'] == 'raw': return disasm_raw(corpus, mode, tags, request.data) - if result is None: return [], 404 - return [], 404 - -@blapi.route('/api/v1/modes') -class Modes(Resource): - def get(self): - """ - List Available Trait Disassemble Modes - """ - return [ - 'pe:x86', - 'pe:x86_64', - 'pe:auto', - 'raw:x86', - 'raw:x86_64', - 'elf:x86', - 'elf:x86_64', - 'elf:auto' - ], 200 - -def main(): - parser = argparse.ArgumentParser( - prog=f'blserver v{__version__}', - description='Binlex Web API', - epilog=f'Author: {__author__}' - ) - - parser.add_argument( - '--version', - action='version', - version=f'v{__version__}' - ) - - parser.add_argument( - '--host', - default='127.0.0.1', - required=False, - help='Host' - ) - - parser.add_argument( - '-p', - '--port', - default=8080, - type=int, - required=False - ) - parser.add_argument( - '-d', - '--debug', - action='store_true', - required=False - ) - - args = parser.parse_args() - - app.run(debug=args.debug, host=args.host, port=args.port) diff --git a/libpybinlex/webapi.py b/libpybinlex/webapi.py deleted file mode 100644 index 267535f9..00000000 --- a/libpybinlex/webapi.py +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env python - -import json -import requests - -class WebAPIv1(): - def __init__(self, url: str, verify=True): - self.url = url + '/api/v1' - self.verify = verify - def get_modes(self): - return requests.get( - url=self.url + '/modes', - verify=self.verify - ) - def is_mode(self, mode): - r = self.get_modes() - if r.status_code != 200: return False - if mode in json.loads(r.content): return True - return False - def get_traits(self, data: bytes, mode: str, corpus='default', tags=[]): - tags = ','.join(tags) - return requests.post( - url=self.url + f'/{corpus}/{mode}/{tags}', - data=data, - verify=self.verify - ) diff --git a/plugins/binlex_cutter.py b/plugins/binlex_cutter.py deleted file mode 100644 index 304b6c92..00000000 --- a/plugins/binlex_cutter.py +++ /dev/null @@ -1,220 +0,0 @@ -import cutter -import re -import os -import tempfile -import subprocess -from pathlib import Path -from functools import partial -from multiprocessing import Pool -from PySide2.QtCore import QObject, SIGNAL, Qt -from PySide2.QtWidgets import * -from glob import glob - -def load_traits_worker(file_path): - - """ - Binlex Load Traits Thread - """ - - f = open(file_path, 'r') - traits = list(set([line.strip() for line in f])) - f.close() - data = [] - for trait in traits: - data.append( - { - 'name': Path(file_path).stem, - 'trait': trait - } - ) - return data - -def scan_traits_workder(trait): - - """ - Binlex Scan Trait - """ - - pass - -class Binlex(cutter.CutterDockWidget): - - """ - Binlex Cutter Plugin - """ - - def __init__(self, parent, action): - super(Binlex, self).__init__(parent, action) - self.setObjectName("Binlex") - self.setWindowTitle("Binlex") - - # Set Threads - self.threads = 4 - - content = QWidget() - self.setWidget(content) - - # Create layout - layout = QVBoxLayout(content) - content.setLayout(layout) - - # Title Label - label_title = QLabel(content) - label_title_font = label_title.font() - label_title_font.setPointSize(16) - label_title_font.setBold(True) - label_title.setText("Binlex - Genetic Binary Traits") - label_title.setFont(label_title_font) - layout.addWidget(label_title) - layout.setAlignment(label_title, Qt.AlignHCenter | Qt.AlignTop) - - menu = QWidget(content) - menu_layout = QHBoxLayout(menu) - - traits_library_btn = QPushButton(menu) - traits_library_btn.setText('Traits Library') - QObject.connect(traits_library_btn, SIGNAL("clicked()"), self.traits_library) - - matches_btn = QPushButton(menu) - matches_btn.setText('Matches') - QObject.connect(matches_btn, SIGNAL("clicked()"), self.matches) - - similarities_btn = QPushButton(menu) - similarities_btn.setText('Similarities') - QObject.connect(similarities_btn, SIGNAL("clicked()"), self.similarities) - - menu_layout.addWidget(traits_library_btn) - menu_layout.addWidget(matches_btn) - menu_layout.addWidget(similarities_btn) - menu.setLayout(menu_layout) - - layout.addWidget(menu) - - # Traits Table - self.table_traits = QTableWidget() - self.table_traits.setShowGrid(False) - self.table_traits.verticalHeader().hide() - self.table_traits.setSelectionBehavior(QAbstractItemView.SelectRows) - self.table_traits.setColumnCount(2) - self.table_traits.setRowCount(1) - self.table_traits.setHorizontalHeaderLabels(['Name', 'Trait']) - self.table_traits.setSortingEnabled(True) - self.table_traits.setVerticalScrollMode(QAbstractItemView.ScrollPerPixel) - self.table_traits.setHorizontalScrollMode(QAbstractItemView.ScrollPerPixel) - self.table_traits.setContentsMargins(0,0,0,0) - self.table_traits.setEditTriggers(QAbstractItemView.NoEditTriggers) - - header = self.table_traits.horizontalHeader() - header.setSectionResizeMode(1, QHeaderView.ResizeToContents) - - self.table_traits.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents) - self.table_traits.setAlternatingRowColors(True) - self.table_traits.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) - - - layout.addWidget(self.table_traits) - layout.setAlignment(self.table_traits, Qt.AlignLeft) - - # Search Type - search_type = QComboBox() - search_type.addItems(["Name", "Trait"]) - layout.addWidget(search_type) - layout.setAlignment(search_type, Qt.AlignRight | Qt.AlignBottom) - - # Search Traits - search_traits = QLineEdit() - search_traits.setAlignment(Qt.AlignTop | Qt.AlignLeft) - search_traits.setContentsMargins(0,0,0,0) - search_traits.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed) - search_traits.setPlaceholderText("Quick Filter") - layout.addWidget(search_traits) - - # Load Traits Button - btn_load = QPushButton(content) - btn_load.setText("Load Traits") - layout.addWidget(btn_load) - layout.setAlignment(btn_load, Qt.AlignRight | Qt.AlignBottom) - QObject.connect(btn_load, SIGNAL("clicked()"), self.load_traits) - - self.show() - - def generate_yara_rule(self, traits): - cutter.message('[-] binlex generating yara signature...') - yara_rule = 'rule binlex {\n\tstrings:\n' - for i in range(0, len(traits)): - yara_rule = yara_rule + '\t\t${i} = {open}{trait}{closed}\n'.format(i=i,trait=traits[i]['trait'],open='{',closed='}') - yara_rule = yara_rule + '\n\tcondition:\n\t\tany of them\n}' - cutter.message('[*] binlex yara generation complete') - return yara_rule - - - def yara_scan(self, rule, file_path): - cutter.message('[-] binlex starting yara scan...') - yara_rule_file = tempfile.NamedTemporaryFile() - yara_rule_file.write(str.encode(yara_rule)) - command = 'yara -w -s {rule} {file_path}'.format(rule=yara_rule_file.name,file_path=file_path) - out = subprocess.getoutput(command) - yara_rule_file.close() - cutter.message('[*] binlex yara scan complete') - return out - - def load_traits(self): - directory = QFileDialog.getExistingDirectory(self, 'Select Folder') - cutter.message("[-] binlex loading traits...") - files = glob('{directory}**/*.traits'.format(directory=directory), recursive=True) - files = [f for f in files if os.path.isfile(f)] - if len(files) <= 0: - cutter.message("[x] no binlex traits files found!") - return None - pool = Pool(processes=self.threads) - traits = pool.map(partial(load_traits_worker,), files) - traits = [item for sublist in traits for item in sublist] - yara_rule = self.generate_yara_rule(traits) - self.yara_scan(yara_rule, '/usr/bin/ls') - self.table_traits.setRowCount(len(traits)) - for i in range(0, len(traits)): - self.table_traits.setItem(i, 0, QTableWidgetItem(traits[i]['name'])) - self.table_traits.setItem(i, 1, QTableWidgetItem(traits[i]['trait'])) - self.table_traits.resizeColumnsToContents() - self.table_traits.resizeRowsToContents() - self.show() - cutter.message("[*] binlex finished loading traits") - - def traits_library(): - pass - - def matches(): - pass - - def similarities(): - pass - - -class BinlexPlugin(cutter.CutterPlugin): - - """ - Binlex Plugin Class - """ - - name = "Binlex" - description = "Binary Genetic Traits Plugin" - version = "1.0.0" - author = "@c3rb3ru5d3d53c" - - def __init__(self): - super(BinlexPlugin, self).__init__() - - def setupPlugin(self): - pass - - def setupInterface(self, main): - action = QAction("Binlex", main) - action.setCheckable(True) - widget = Binlex(main, action) - main.addPluginDockWidget(widget, action) - - def terminate(self): - cutter.message("[*] binlex plugin shutting down...") - -def create_cutter_plugin(): - return BinlexPlugin() diff --git a/pyproject.toml b/pyproject.toml deleted file mode 100644 index 36432fa2..00000000 --- a/pyproject.toml +++ /dev/null @@ -1,6 +0,0 @@ -[build-system] -requires = [ - "setuptools>=42", - "wheel", - "pybind11>=2.10.0" -] diff --git a/scripts/blghidra/blghidra.py b/scripts/blghidra/blghidra.py new file mode 100755 index 00000000..c4a05108 --- /dev/null +++ b/scripts/blghidra/blghidra.py @@ -0,0 +1,18 @@ +#A Ghidra to Binlex Tool +#@author @c3rb3ru5d3d53c +#@category +#@keybinding +#@menupath +#@toolbar + +import json + +for function in currentProgram().getFunctionManager().getFunctions(True): + print(json.dumps({ + 'type': 'symbol', + 'symbol_type': 'function', + 'name': function.getName(), + 'file_offset': None, + 'relative_virtual_address': None, + 'virtual_address': function.getEntryPoint().getOffset() + })) diff --git a/scripts/blserver b/scripts/blserver deleted file mode 100644 index 6266f956..00000000 --- a/scripts/blserver +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env python - -from libpybinlex import server - -server.main() diff --git a/scripts/bltensor/.gitignore b/scripts/bltensor/.gitignore new file mode 100644 index 00000000..5c9b6d1d --- /dev/null +++ b/scripts/bltensor/.gitignore @@ -0,0 +1,5 @@ +venv/ +bltensor.egg-info/ +build/ +*.pyc +__pycache__/ diff --git a/scripts/bltensor/README.md b/scripts/bltensor/README.md new file mode 100644 index 00000000..bab00318 --- /dev/null +++ b/scripts/bltensor/README.md @@ -0,0 +1 @@ +# BLTensor - A Binlex Tensorflow Tool \ No newline at end of file diff --git a/scripts/bltensor/requirements.txt b/scripts/bltensor/requirements.txt new file mode 100644 index 00000000..166b4219 --- /dev/null +++ b/scripts/bltensor/requirements.txt @@ -0,0 +1,5 @@ +onnx==1.17.0 +tf2onnx==1.16.1 +numpy==2.0.2 +tensorflow==2.18.0 +onnxruntime==1.20.0 \ No newline at end of file diff --git a/scripts/bltensor/scripts/bltensor b/scripts/bltensor/scripts/bltensor new file mode 100755 index 00000000..a405ccc3 --- /dev/null +++ b/scripts/bltensor/scripts/bltensor @@ -0,0 +1,253 @@ +#!/usr/bin/env python + +import sys +import json +import onnx +import glob +import os + +os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' +os.environ["GRPC_VERBOSITY"] = 'error' +os.environ["GLOG_minloglevel"] = '2' + +import tf2onnx +import argparse +import numpy as np +import tensorflow as tf +import onnxruntime as ort +import concurrent.futures +from tensorflow.keras.models import Model +from tensorflow.keras.preprocessing.sequence import pad_sequences +from tensorflow.keras.layers import ( + Input, LSTM, RepeatVector, TimeDistributed, Masking, Dense, + Dropout, BatchNormalization +) + +__author__ = '@c3rb3ru5d3d53c' +__version__ = '1.0.0' + +class OnnxClassifier: + def __init__(self, model_paths: list): + self.sessions = [ort.InferenceSession(path) for path in model_paths] + input_info = self.sessions[0].get_inputs()[0] + self.input_name = input_info.name + _, self.max_sequence_length, input_dim = input_info.shape + self.input_dimensions = input_dim or 1 + + def classify(self, feature: list, threshold: float = 0.05, scale: float = 0.01): + padded_feature = pad_sequences( + [feature], + maxlen=self.max_sequence_length, + padding='post', + dtype='float32' + ) + + input_tensor = padded_feature.reshape( + 1, self.max_sequence_length, self.input_dimensions + ).astype(np.float32) + + similarity_scores = [] + + for session in self.sessions: + output = session.run(None, {self.input_name: input_tensor})[0] + + mse = np.mean((input_tensor - output) ** 2) + max_mse = np.mean(input_tensor ** 2) + 1e-6 + normalized_mse = mse / max_mse + + exponent = (normalized_mse - threshold) / scale + anomaly_score = 1 / (1 + np.exp(-exponent)) + + similarity_score = 1 - anomaly_score + similarity_scores.append(similarity_score) + + return np.mean(similarity_scores) + +class OnnxTrainer: + def __init__( + self, + features: list, + batch_size: int = 64, + epochs: int = 10, + neurons: int = 256, + opset: int = 13 + ): + self.features = features + self.batch_size = batch_size + self.epochs = epochs + self.neurons = neurons + self.opset = opset + + self.input_dimensions = 1 + self.max_sequence_length = min(100, max(len(f) for f in features)) + self.padded_features = pad_sequences( + features, + maxlen=self.max_sequence_length, + padding='post', + dtype='float32' + ).reshape(-1, self.max_sequence_length, self.input_dimensions) + + def get_dataset(self): + dataset = tf.data.Dataset.from_tensor_slices((self.padded_features, self.padded_features)) + dataset = dataset.batch(self.batch_size).prefetch(tf.data.AUTOTUNE) + return dataset + + def train(self, output: str): + timesteps = self.max_sequence_length + inputs = Input(shape=(timesteps, self.input_dimensions), dtype=tf.float32) + masked_inputs = Masking(mask_value=0.0)(inputs) + + encoded = LSTM(self.neurons, return_sequences=True)(masked_inputs) + encoded = Dropout(0.2)(encoded) + encoded = LSTM(self.neurons // 2)(encoded) + encoded = Dropout(0.2)(encoded) + encoded = BatchNormalization()(encoded) + + decoded = RepeatVector(timesteps)(encoded) + decoded = LSTM(self.neurons // 2, return_sequences=True)(decoded) + decoded = Dropout(0.2)(decoded) + decoded = LSTM(self.neurons, return_sequences=True)(decoded) + decoded = Dropout(0.2)(decoded) + decoded = BatchNormalization()(decoded) + outputs = TimeDistributed(Dense(1))(decoded) + + autoencoder = Model(inputs, outputs) + + optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4, clipnorm=1.0) + autoencoder.compile(optimizer=optimizer, loss='mean_squared_error') + + if np.isnan(self.padded_features).any() or np.isinf(self.padded_features).any(): + print("model training failed with NaN or Inf values.", file=sys.stderr) + sys.exit(1) + + autoencoder.fit( + self.get_dataset(), + epochs=self.epochs + ) + + spec = (tf.TensorSpec((None, timesteps, self.input_dimensions), tf.float32, name='input'),) + tf2onnx.convert.from_keras( + autoencoder, + input_signature=spec, + opset=self.opset, + output_path=output + ) + +def classify_line(line, onnx, threshold, scale): + try: + data = json.loads(line) + if data['chromosome'] is None: return None + if data['chromosome']['feature'] is None: return None + feature = data['chromosome']['feature'] + classification_value = onnx.classify( + feature, + threshold=threshold, + scale=scale + ) + return classification_value + except json.JSONDecodeError as error: + print(f"JSON decode error: {error}", file=sys.stderr) + except Exception as error: + print(f"Error: {error}", file=sys.stderr) + return None + +def main(): + parser = argparse.ArgumentParser( + prog=f'bltensor v{__version__}', + description='A Tensorflow Binlex Training and Filtering Tool', + epilog=f'Author: {__author__}' + ) + parser.add_argument('--version', action='version', version=f'v{__version__}') + subparsers = parser.add_subparsers(dest="mode", required=True) + + parser_filter = subparsers.add_parser('filter', help='Filter Mode') + parser_filter.add_argument('-t', '--threshold', type=float, required=True, help='Threshold') + parser_filter.add_argument('-s', '--scale', type=float, default=0.01, help='Scale for anomaly score calculation') + parser_filter.add_argument('-fm', '--filter-mode', choices=['gte', 'lte', 'gt', 'lt'], default='gte', help="Filter Mode") + parser_filter.add_argument('-f', '--filter', type=float, required=True, help="Filter by Score") + parser_filter.add_argument('-i', '--input', type=str, required=True, help='Input ONNX Model Directory') + + parser_train = subparsers.add_parser('train', help='Train ONNX Model') + parser_train.add_argument('-t', '--threads', type=int, default=1, help='Threads') + parser_train.add_argument('-e', '--epochs', type=int, default=10, help='Epochs') + parser_train.add_argument('-b', '--batch-size', type=int, default=64, help='Batch Size') + parser_train.add_argument('-n', '--neurons', type=int, default=256, help='Number of neurons') + parser_train.add_argument('-o', '--output', type=str, required=True, help='Output model path') + + parser_classify = subparsers.add_parser('classify', help='Classify Sample Mode') + parser_classify.add_argument('--threshold', type=float, required=True, help='Threshold') + parser_classify.add_argument('--threads', type=int, default=1, help='Threads') + parser_classify.add_argument('--scale', type=float, default=0.01, help='Scale for anomaly score calculation') + parser_classify.add_argument('--input', type=str, required=True, help='Input ONNX Model Directory') + + args = parser.parse_args() + + if args.mode == 'classify': + model_paths = glob.glob(os.path.join(args.input, '**', '*.onnx'), recursive=True) + + onnx = OnnxClassifier(model_paths=model_paths) + + classification_values = [] + + with concurrent.futures.ThreadPoolExecutor(max_workers=args.threads) as executor: + futures = [executor.submit(classify_line, line, onnx, args.threshold, args.scale) for line in sys.stdin] + for future in concurrent.futures.as_completed(futures): + classification_value = future.result() + if classification_value is not None: + classification_values.append(classification_value) + + similarity = sum(classification_values) / len(classification_values) + print(f'Similarity: {similarity}') + + elif args.mode == 'filter': + + model_paths = glob.glob(os.path.join(args.input, '**', '*.onnx'), recursive=True) + + onnx = OnnxClassifier(model_paths=model_paths) + + threshold_check = { + 'gte': lambda x: x >= args.filter, + 'lte': lambda x: x <= args.filter, + 'gt': lambda x: x > args.filter, + 'lt': lambda x: x < args.filter + }[args.filter_mode] + + for line in sys.stdin: + classification_value = classify_line(line, onnx, args.threshold, args.scale) + if classification_value is None: continue + + if threshold_check(classification_value): + print(line.strip()) + + elif args.mode == 'train': + tf.config.threading.set_intra_op_parallelism_threads(args.threads) + tf.config.threading.set_inter_op_parallelism_threads(args.threads) + tf.config.optimizer.set_jit(False) + + features = [] + for line in sys.stdin: + try: + data = json.loads(line) + if data['chromosome'] is None: continue + if data['chromosome']['feature'] is None: continue + features.append(data['chromosome']['feature']) + except json.JSONDecodeError as error: + print(f"JSON decode error: {error}", file=sys.stderr) + except Exception as error: + print(f"Error: {error}", file=sys.stderr) + + if not features: + print('No features found for training.', file=sys.stderr) + sys.exit(1) + + trainer = OnnxTrainer( + features=features, + batch_size=args.batch_size, + epochs=args.epochs, + neurons=args.neurons + ) + + trainer.train(args.output) + +if __name__ == '__main__': + main() diff --git a/scripts/bltensor/setup.py b/scripts/bltensor/setup.py new file mode 100755 index 00000000..55c81cd3 --- /dev/null +++ b/scripts/bltensor/setup.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python + +from setuptools import setup, find_packages + +with open('README.md', 'r', encoding='utf-8') as fh: + long_description = fh.read() + +with open('requirements.txt') as f: + requirements = f.read().splitlines() + +setup( + name='bltensor', + version='2.0.0', + author='@c3rb3ru5d3d53c', + description='A Binlex Tensorflow Tool', + long_description=long_description, + long_description_content_type='text/markdown', + scripts=['scripts/bltensor'], + install_requires=requirements, + classifiers=[ + 'Programming Language :: Python :: 3', + 'Operating System :: OS Independent', + ], + python_requires='>=3.6', +) diff --git a/setup.py b/setup.py deleted file mode 100755 index 824523be..00000000 --- a/setup.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python - -import os -import re -import sys -from shutil import move -from glob import glob -import subprocess -import platform -from multiprocessing import cpu_count -from setuptools import Extension, setup, find_packages -from setuptools.command.build_ext import build_ext - -__version__ = "1.1.1" -__author__ = "@c3rb3ru5d3d53c" - -def get_base_prefix_compat(): - return getattr(sys, "base_prefix", None) or getattr(sys, "real_prefix", None) or sys.prefix - -def in_virtualenv(): - return get_base_prefix_compat() != sys.prefix - -class CMakeExtension(Extension): - def __init__(self, name, sourcedir=""): - Extension.__init__(self, name, sources=[]) - self.sourcedir = os.path.abspath(sourcedir) - -class CMakeBuild(build_ext): - def build_extension(self, ext): - extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name))) - if not extdir.endswith(os.path.sep): - extdir += os.path.sep - debug = int(os.environ.get("DEBUG", 0)) if self.debug is None else self.debug - cfg = "Debug" if debug else "Release" - cmake_args = [ - f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}", - f"-DPYTHON_EXECUTABLE={sys.executable}", - f"-DCMAKE_BUILD_TYPE={cfg}", - '-DBUILD_PYTHON_BINDINGS=ON', - f'-DPYBIND11_PYTHON_VERSION={platform.python_version()}' - ] - build_temp = os.path.join(self.build_temp, ext.name) - if not os.path.exists(build_temp): - os.makedirs(build_temp) - subprocess.check_call(["cmake", "-B", "deps/build", "-S", "deps"], cwd=ext.sourcedir) - subprocess.check_call(["cmake", "--build", "deps/build", "--config", cfg, '--parallel', f'{cpu_count()}'], cwd=ext.sourcedir) - subprocess.check_call(["cmake", "-B", "build"] + cmake_args, cwd=ext.sourcedir) - subprocess.check_call(["cmake", "--build", "build", "--config", cfg, '--parallel', f'{cpu_count()}'], cwd=ext.sourcedir) - subprocess.check_call(["cmake", "--install", "build", "--prefix", "build/install", "--config", cfg], cwd=ext.sourcedir) - -setup( - name="pybinlex", - version=__version__, - author=__author__, - author_email="c3rb3ru5d3d53c@protonmail.com", - url="https://github.com/c3rb3ru5d3d53c/binlex", - long_description=open('README.md').read(), - long_description_content_type='text/markdown', - packages=['libpybinlex'], - scripts=['scripts/blserver'], - install_requires=[ - 'Flask==2.2.3', - 'flask-restx==1.0.6', - 'gunicorn==20.1.0', - 'requests==2.28.2' - ], - ext_modules=[CMakeExtension("pybinlex")], - cmdclass={ - "build_ext": CMakeBuild - }, - zip_safe=False, - python_requires=">=3.6", -) diff --git a/src/args.cpp b/src/args.cpp deleted file mode 100644 index bf9352f3..00000000 --- a/src/args.cpp +++ /dev/null @@ -1,205 +0,0 @@ -#include "args.h" - -using namespace binlex; - -Args::Args(){ - SetDefault(); -} - -void Args::SetDefault(){ - options.timeout = 0; - options.input = NULL; - options.threads = 1; - options.help = false; - options.output = NULL; - options.corpus = "default"; - options.list_modes = false; - options.mode = "auto"; - options.io_type = ARGS_IO_TYPE_UNKNOWN; - options.pretty = false; - options.debug = false; - options.tags.clear(); // Clear if defaults are needed. -} - -bool Args::check_mode(const char *mode){ - for (int i = 0; i < ARGS_MODE_COUNT; i++){ - if (strcmp(modes[i], mode) == 0){ - return true; - } - } - return false; -} - -int Args::is_file(const char* path) { -#ifndef _WIN32 - struct stat path_stat; - if (stat(path, &path_stat) != 0) { - return 0; - } - return S_ISREG(path_stat.st_mode); -#else - DWORD dwFileAttributes = GetFileAttributesA(path); - return !(dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); -#endif -} - -int Args::is_dir(const char* path) { -#ifndef _WIN32 - struct stat statbuf; - if (stat(path, &statbuf) != 0) { - return 0; - } - return S_ISDIR(statbuf.st_mode); -#else - DWORD dwFileAttributes = GetFileAttributesA(path); - return dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; -#endif -} - -void Args::set_io_type(char *input){ - if (is_file(input) != 0){ - options.io_type = ARGS_IO_TYPE_FILE; - } else if (is_dir(input) != 0){ - options.io_type = ARGS_IO_TYPE_DIR; - } else{ - options.io_type = ARGS_IO_TYPE_UNKNOWN; - fprintf(stderr, "unknown input type\n"); - exit(1); - } -} - -std::string Args::get_tags_as_str(){ - std::ostringstream out; - - if(!options.tags.empty()) { - auto tbegin = options.tags.begin(); - auto tend = options.tags.end(); - out << *tbegin; - while((++tbegin) != tend){ - out << ',' << *tbegin; - } - } - return out.str(); -} - - -void Args::print_help(){ - printf( - "binlex %s - A Binary Genetic Traits Lexer\n" - " -i --input\t\tinput file\t\t(required)\n" - " -m --mode\t\tset mode\t\t(optional)\n" - " -lm --list-modes\tlist modes\t\t(optional)\n" - " -c --corpus\t\tcorpus name\t\t(optional)\n" - " -g --tag\t\tadd a tag\t\t(optional)\n" - " \t\t(can be specified multiple times)\n" - " -t --threads\t\tnumber of threads\t(optional)\n" - " -to --timeout\t\texecution timeout in s\t(optional)\n" - " -h --help\t\tdisplay help\t\t(optional)\n" - " -o --output\t\toutput file\t\t(optional)\n" - " -p --pretty\t\tpretty output\t\t(optional)\n" - " -d --debug\t\tprint debug info\t(optional)\n" - " -v --version\t\tdisplay version\t\t(optional)\n" - "Author: @c3rb3ru5d3d53c\n", - version - ); -} - -void Args::parse(int argc, char **argv){ - if (argc < 1){ - print_help(); - exit(EXIT_FAILURE); - } - for (int i = 0; i < argc; i++){ - if (strcmp(argv[i], (char *)"-h") == 0 || - strcmp(argv[i], (char *)"--help") == 0){ - options.help = true; - print_help(); - exit(EXIT_SUCCESS); - } - if (strcmp(argv[i], (char *)"-v") == 0 || - strcmp(argv[i], (char *)"--version") == 0){ - options.help = true; - printf("%s\n", version); - exit(EXIT_SUCCESS); - } - if (strcmp(argv[i], (char *)"-lm") == 0 || - strcmp(argv[i], (char *)"--list-modes") == 0){ - options.list_modes = true; - for (int j = 0; j < ARGS_MODE_COUNT; j++){ - printf("%s\n", modes[j]); - } - exit(EXIT_SUCCESS); - } - if (strcmp(argv[i], (char *)"-i") == 0 || - strcmp(argv[i], (char *)"--input") == 0){ - options.input = argv[i+1]; - set_io_type(options.input); - } - if (strcmp(argv[i], (char *)"-p") == 0 || - strcmp(argv[i], (char *)"--pretty") == 0){ - options.pretty = true; - } - if (strcmp(argv[i], (char *)"-t") == 0 || - strcmp(argv[i], (char *)"--threads") == 0){ - if (argc < i+2){ - fprintf(stderr, "[x] invalid thread count\n"); - exit(EXIT_FAILURE); - } - options.threads = atoi(argv[i+1]); - if (options.threads <= 0){ - fprintf(stderr, "[x] invalid number of threads\n"); - exit(EXIT_FAILURE); - } - } - if (strcmp(argv[i], (char *)"-to") == 0 || - strcmp(argv[i], (char *)"--timeout") == 0){ - if (argc < i+2){ - fprintf(stderr, "[x] timeout requires a parameter\n"); - exit(EXIT_FAILURE); - } - options.timeout = atoi(argv[i+1]); - if (options.timeout <= 0){ - fprintf(stderr, "[x] invalid timeout value\n"); - exit(EXIT_FAILURE); - } - } - if (strcmp(argv[i], (char *)"-c") == 0 || - strcmp(argv[i], (char *)"--corpus") == 0){ - if (argc < i+2){ - fprintf(stderr, "[x] corpus requres 1 parameter\n"); - exit(EXIT_FAILURE); - } - options.corpus = argv[i+1]; - } - if (strcmp(argv[i], (char *)"-o") == 0 || - strcmp(argv[i], (char *)"--output") == 0){ - options.output = argv[i+1]; - } - if (strcmp(argv[i], (char *)"-m") == 0 || - strcmp(argv[i], (char *)"--mode") == 0){ - options.mode = argv[i+1]; - - if (check_mode(options.mode.c_str()) == false){ - fprintf(stderr, "%s is an invalid mode\n", options.mode.c_str()); - exit(EXIT_FAILURE); - } - } - if (strcmp(argv[i], (char *)"-d") == 0 || - strcmp(argv[i], (char *)"--debug") == 0){ - options.debug = true; - fprintf(stderr, "DEBUG ENABLED...\n"); - } - if (strcmp(argv[i], (char *)"-g") == 0 || - strcmp(argv[i], (char *)"--tag") == 0){ - if (argc < i + 2){ - fprintf(stderr, "[x] tag requires a parameter\n"); - exit(EXIT_FAILURE); - } - options.tags.insert(argv[i + 1]); - } - } -} - -Args::~Args(){ - SetDefault(); -} diff --git a/src/auto.cpp b/src/auto.cpp deleted file mode 100644 index 0daedba1..00000000 --- a/src/auto.cpp +++ /dev/null @@ -1,111 +0,0 @@ -#include "auto.h" - -using namespace std; -using namespace binlex; - -AutoLex::AutoLex(){ - characteristics.mode = CS_MODE_32; - characteristics.format = LIEF::FORMAT_PE; - characteristics.arch = CS_ARCH_X86; - characteristics.machineType = (int) MACHINE_TYPES::IMAGE_FILE_MACHINE_I386; -} - -bool AutoLex::GetFileCharacteristics(char * file_path){ - - auto bin = LIEF::Parser::parse(file_path); - - if (bin == NULL){ - return false; - } - - characteristics.format = bin->format(); - - if(bin->header().is_32()){ - characteristics.mode = CS_MODE_32; - if(bin->format() == LIEF::FORMAT_PE) { - characteristics.machineType = (int) MACHINE_TYPES::IMAGE_FILE_MACHINE_I386; - } - else if(bin->format() == LIEF::FORMAT_ELF) { - characteristics.machineType = (int) ARCH::EM_386; - } - } - else if(bin->header().is_64()){ - characteristics.mode = CS_MODE_64; - if(bin->format() == LIEF::FORMAT_PE) { - characteristics.machineType = (int) MACHINE_TYPES::IMAGE_FILE_MACHINE_AMD64; - } - else if(bin->format() == LIEF::FORMAT_ELF) { - characteristics.machineType = (int) ARCH::EM_X86_64; - } - } - return true; -} - -int AutoLex::ProcessFile(char *file_path){ - - // Todo: - // - raise exceptions instead of returning a null decompiler to better handle being called as a lib - - if (!GetFileCharacteristics(file_path)){ - fprintf(stderr, "[x] unable to get file characteristics\n"); - return -1; - } - - if(characteristics.format == LIEF::FORMAT_PE){ - PE pe; - - if (!pe.ReadFile(file_path)){ - return EXIT_FAILURE; - } - - if(pe.HasLimitations()){ - PRINT_ERROR_AND_EXIT("[x] file has limitations\n"); - } - - if(pe.IsDotNet()){ - DOTNET pe; - g_args.options.mode = "pe:cil"; - if (pe.ReadFile(file_path) == false) return 1; - CILDisassembler disassembler(pe); - int si = 0; - for (auto section : pe._sections) { - if (section.offset == 0) continue; - if (disassembler.Disassemble(section.data, section.size, si) == false) continue; - si++; - } - disassembler.WriteTraits(); - return EXIT_SUCCESS; - } else { - if (characteristics.arch == CS_ARCH_X86 && - characteristics.mode == CS_MODE_32){ - g_args.options.mode = "pe:x86"; - } - if (characteristics.arch == CS_ARCH_X86 && - characteristics.mode == CS_MODE_64){ - g_args.options.mode = "pe:x86_64"; - } - Disassembler disassembler(pe); - disassembler.Disassemble(); - disassembler.WriteTraits(); - } - } else if (characteristics.format == LIEF::FORMAT_ELF){ - ELF elf; - - if (!elf.ReadFile(file_path)){ - return EXIT_FAILURE; - } - Disassembler disassembler(elf); - - if (characteristics.arch == CS_ARCH_X86 && - characteristics.mode == CS_MODE_32){ - g_args.options.mode = "elf:x86"; - } - if (characteristics.arch == CS_ARCH_X86 && - characteristics.mode == CS_MODE_64){ - g_args.options.mode = "elf:x86_64"; - } - disassembler.Disassemble(); - disassembler.WriteTraits(); - } - return EXIT_SUCCESS; -} diff --git a/src/bin/binlex.rs b/src/bin/binlex.rs new file mode 100644 index 00000000..31e39567 --- /dev/null +++ b/src/bin/binlex.rs @@ -0,0 +1,790 @@ +use binlex::io::Stderr; +use binlex::Architecture; +use rayon::ThreadPoolBuilder; +use binlex::formats::pe::PE; +use binlex::disassemblers::capstone::Disassembler; +use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; +use serde_json::json; +use std::collections::BTreeMap; +use std::process; +use std::fs::File; +use std::io::Write; +use std::collections::BTreeSet; +use std::collections::HashSet; +use binlex::controlflow::Graph; +use binlex::controlflow::Instruction; +use binlex::controlflow::Block; +use binlex::controlflow::Function; +use binlex::types::LZ4String; +use binlex::io::Stdout; +use binlex::io::JSON; +use binlex::controlflow::Symbol; +use clap::Parser; +use binlex::Config; +use binlex::VERSION; +use binlex::AUTHOR; +use binlex::controlflow::Attributes; +use binlex::controlflow::Tag; +use binlex::Format; +use binlex::formats::File as BLFile; +use binlex::formats::ELF; +use binlex::formats::MACHO; +use binlex::io::Stdin; +use binlex::disassemblers::custom::cil::Disassembler as CILDisassembler; + +#[derive(Parser, Debug)] +#[command( + name = "binlex", + version = VERSION, + about = format!("A Binary Pattern Lexer\n\nVersion: {}", VERSION), + after_help = format!("Author: {}", AUTHOR), +)] +pub struct Args { + #[arg(short, long)] + pub input: String, + #[arg(short, long)] + pub output: Option, + #[arg(short, long, help = format!("[{}]", Architecture::to_list()))] + pub architecture: Option, + #[arg(short, long)] + pub config: Option, + #[arg(short, long)] + pub threads: Option, + #[arg(long, value_delimiter = ',', default_value = None)] + pub tags: Option>, + #[arg(long, default_value_t = false)] + pub minimal: bool, + #[arg(short, long, default_value_t = false)] + pub debug: bool, + #[arg(long, default_value_t = false)] + pub enable_instructions: bool, + #[arg(long, default_value_t = false)] + pub disable_hashing: bool, + #[arg(long, default_value_t = false)] + pub disable_disassembler_sweep: bool, + #[arg(long, default_value_t = false)] + pub disable_heuristics: bool, + #[arg(long, default_value_t = false)] + pub enable_mmap_cache: bool, + #[arg(long)] + pub mmap_directory: Option, +} + +fn validate_args(args: &Args) { + + if let Some(tags) = &args.tags { + let mut unique_tags = HashSet::new(); + for tag in tags { + if !unique_tags.insert(tag) { + eprintln!("tags must be unique"); + process::exit(1); + } + } + } + +} + +fn get_elf_function_symbols(elf: &ELF) -> BTreeMap { + let mut symbols = BTreeMap::::new(); + + if !Stdin::is_terminal() { return symbols; } + + let json = JSON::from_stdin_with_filter(|value| { + let obj = match value.as_object_mut() { + Some(obj) => obj, + None => return false, + }; + + let obj_type = obj.get("type").and_then(|v| v.as_str()).map(String::from); + let symbol_type = obj.get("symbol_type").and_then(|v| v.as_str()).map(String::from); + let file_offset = obj.get("file_offset").and_then(|v| v.as_u64()); + let relative_virtual_address = obj.get("relative_virtual_address").and_then(|v| v.as_u64()); + let mut virtual_address = obj.get("virtual_address").and_then(|v| v.as_u64()); + + if obj_type.as_deref() != Some("symbol") { + return false; + } + + if symbol_type.is_none() { + return false; + } + + if file_offset.is_none() && relative_virtual_address.is_none() && virtual_address.is_none() { + return false; + } + + if virtual_address.is_some() { + return true; + } + + if virtual_address.is_none() { + if let Some(rva) = relative_virtual_address { + virtual_address = Some(elf.relative_virtual_address_to_virtual_address(rva)); + } + if let Some(offset) = file_offset { + if let Some(va) = elf.file_offset_to_virtual_address(offset) { + virtual_address = Some(va); + } + } + + if let Some(va) = virtual_address { + obj.insert("virtual_address".to_string(), json!(va)); + return true; + } + } + + false + + }); + + if json.is_ok() { + for value in json.unwrap().values() { + let address = value.get("virtual_address").and_then(|v| v.as_u64()); + let name = value.get("name").and_then(|v| v.as_str()); + let symbol_type = value.get("symbol_type").and_then(|v| v.as_str()); + if address.is_none() { continue; } + if name.is_none() { continue; } + if symbol_type.is_none() { continue; } + let symbol = Symbol::new( + address.unwrap(), + symbol_type.unwrap().to_string(), + name.unwrap().to_string()); + symbols.insert(address.unwrap(),symbol); + } + } + + return symbols; +} + +fn get_macho_function_symbols(macho: &MACHO) -> BTreeMap { + let mut symbols = BTreeMap::::new(); + + if !Stdin::is_terminal() { return symbols; } + + let json = JSON::from_stdin_with_filter(|value| { + let obj = match value.as_object_mut() { + Some(obj) => obj, + None => return false, + }; + + let obj_type = obj.get("type").and_then(|v| v.as_str()).map(String::from); + let symbol_type = obj.get("symbol_type").and_then(|v| v.as_str()).map(String::from); + let file_offset = obj.get("file_offset").and_then(|v| v.as_u64()); + let relative_virtual_address = obj.get("relative_virtual_address").and_then(|v| v.as_u64()); + let mut virtual_address = obj.get("virtual_address").and_then(|v| v.as_u64()); + let slice = obj.get("slice").and_then(|v| v.as_u64()); + + if slice.is_none() { + return false; + } + + let slice = slice.unwrap() as usize; + + if obj_type.as_deref() != Some("symbol") { + return false; + } + + if symbol_type.is_none() { + return false; + } + + if file_offset.is_none() && relative_virtual_address.is_none() && virtual_address.is_none() { + return false; + } + + if virtual_address.is_some() { + return true; + } + + if virtual_address.is_none() { + if let Some(rva) = relative_virtual_address { + let va = macho.relative_virtual_address_to_virtual_address(rva, slice); + if va.is_none() { return false; } + virtual_address = Some(va.unwrap()); + } + if let Some(offset) = file_offset { + if let Some(va) = macho.file_offset_to_virtual_address(offset, slice) { + virtual_address = Some(va); + } + } + + if let Some(va) = virtual_address { + obj.insert("virtual_address".to_string(), json!(va)); + return true; + } + } + + false + + }); + + if json.is_ok() { + for value in json.unwrap().values() { + let address = value.get("virtual_address").and_then(|v| v.as_u64()); + let name = value.get("name").and_then(|v| v.as_str()); + let symbol_type = value.get("symbol_type").and_then(|v| v.as_str()); + if address.is_none() { continue; } + if name.is_none() { continue; } + if symbol_type.is_none() { continue; } + let symbol = Symbol::new( + address.unwrap(), + symbol_type.unwrap().to_string(), + name.unwrap().to_string()); + symbols.insert(address.unwrap(),symbol); + } + } + + return symbols; +} + +fn get_pe_function_symbols(pe: &PE) -> BTreeMap { + let mut symbols = BTreeMap::::new(); + + if !Stdin::is_terminal() { return symbols; } + + let json = JSON::from_stdin_with_filter(|value| { + let obj = match value.as_object_mut() { + Some(obj) => obj, + None => return false, + }; + + let obj_type = obj.get("type").and_then(|v| v.as_str()).map(String::from); + let symbol_type = obj.get("symbol_type").and_then(|v| v.as_str()).map(String::from); + let file_offset = obj.get("file_offset").and_then(|v| v.as_u64()); + let relative_virtual_address = obj.get("relative_virtual_address").and_then(|v| v.as_u64()); + let mut virtual_address = obj.get("virtual_address").and_then(|v| v.as_u64()); + + if obj_type.as_deref() != Some("symbol") { + return false; + } + + if symbol_type.is_none() { + return false; + } + + if file_offset.is_none() && relative_virtual_address.is_none() && virtual_address.is_none() { + return false; + } + + if virtual_address.is_some() { + return true; + } + + if virtual_address.is_none() { + if let Some(rva) = relative_virtual_address { + virtual_address = Some(pe.relative_virtual_address_to_virtual_address(rva)); + } + if let Some(offset) = file_offset { + if let Some(va) = pe.file_offset_to_virtual_address(offset) { + virtual_address = Some(va); + } + } + + if let Some(va) = virtual_address { + obj.insert("virtual_address".to_string(), json!(va)); + return true; + } + } + + false + + }); + + if json.is_ok() { + for value in json.unwrap().values() { + let address = value.get("virtual_address").and_then(|v| v.as_u64()); + let name = value.get("name").and_then(|v| v.as_str()); + let symbol_type = value.get("symbol_type").and_then(|v| v.as_str()); + if address.is_none() { continue; } + if name.is_none() { continue; } + if symbol_type.is_none() { continue; } + let symbol = Symbol::new( + address.unwrap(), + symbol_type.unwrap().to_string(), + name.unwrap().to_string()); + symbols.insert(address.unwrap(),symbol); + } + } + + return symbols; +} + +fn process_output(output: Option, enable_instructions: bool, cfg: &Graph, attributes: &Attributes, function_symbols: &BTreeMap) { + + let mut instructions = Vec::::new(); + + if enable_instructions { + instructions = cfg.instructions.valid() + .iter() + .map(|entry| *entry) + .collect::>() + .par_iter() + .filter_map(|address| Instruction::new(*address, &cfg).ok()) + .filter_map(|instruction| instruction.json_with_attributes(attributes.clone()).ok()) + .map(|js| LZ4String::new(&js)) + .collect(); + } + + let blocks: Vec = cfg.blocks.valid() + .iter() + .map(|entry| *entry) + .collect::>() + .par_iter() + .filter_map(|address| Block::new(*address, &cfg).ok()) + .filter_map(|block| block.json_with_attributes(attributes.clone()).ok()) + .map(|js| LZ4String::new(&js)) + .collect(); + + let functions: Vec = cfg.functions.valid() + .iter() + .map(|entry| *entry) + .collect::>() + .par_iter() + .filter_map(|address| Function::new(*address, &cfg).ok()) + .filter_map(|function| { + let mut function_attributes = attributes.clone(); + let symbol= function_symbols.get(&function.address); + if symbol.is_some() { + function_attributes.push(symbol.unwrap().attribute()); + } + function.json_with_attributes(function_attributes).ok() + }) + .map(|js| LZ4String::new(&js)) + .collect(); + + if output.is_none() { + + if enable_instructions { + instructions.iter().for_each(|result| { + Stdout::print(result); + }); + } + + blocks.iter().for_each(|result| { + Stdout::print(result); + }); + + functions.iter().for_each(|result| { + Stdout::print(result); + }); + } + + if let Some(output_file) = output { + let mut file = match File::create(output_file) { + Ok(file) => file, + Err(error) => { + eprintln!("{}", error); + std::process::exit(1); + } + }; + + if enable_instructions { + for instruction in instructions { + if let Err(error) = writeln!(file, "{}", instruction) { + eprintln!("{}", error); + std::process::exit(1); + } + } + } + + for block in blocks { + if let Err(error) = writeln!(file, "{}", block) { + eprintln!("{}", error); + std::process::exit(1); + } + } + + for function in functions { + if let Err(error) = writeln!(file, "{}", function) { + eprintln!("{}", error); + std::process::exit(1); + } + } + + } +} + +fn process_pe(input: String, config: Config, tags: Option>, output: Option, enable_instructions: bool) { + let mut attributes = Attributes::new(); + + let pe = match PE::new(input, config.clone()) { + Ok(pe) => pe, + Err(error) => { + eprintln!("failed to read pe file: {}", error); + process::exit(1); + } + }; + + match pe.architecture() { + Architecture::UNKNOWN => { + eprintln!("unsupported pe architecture"); + process::exit(1); + }, + _ => {} + } + + if !config.general.minimal { + let file_attribute = pe.file.attribute(); + if tags.is_some() { + for tag in tags.unwrap() { + attributes.push(Tag::new(tag).attribute()); + } + } + attributes.push(file_attribute); + } + + let function_symbols = get_pe_function_symbols(&pe); + + let mapped_file = pe.image() + .unwrap_or_else(|error| { eprintln!("failed to map pe image: {}", error); process::exit(1)}); + + let image = mapped_file + .mmap() + .unwrap_or_else(|error| { eprintln!("failed to get pe virtual image: {}", error); process::exit(1); }); + + let executable_address_ranges = match pe.is_dotnet() { + true => pe.dotnet_executable_virtual_address_ranges(), + _ => pe.executable_virtual_address_ranges(), + }; + + let mut entrypoints = BTreeSet::::new(); + + match pe.is_dotnet(){ + true => entrypoints.extend(pe.dotnet_entrypoints()), + _ => entrypoints.extend(pe.entrypoints()), + } + + entrypoints.extend(function_symbols.keys()); + + let mut cfg = Graph::new(pe.architecture(), config.clone()); + + if !pe.is_dotnet() { + let disassembler = match Disassembler::new(pe.architecture(), &image, executable_address_ranges.clone()) { + Ok(disassembler) => disassembler, + Err(error) => { + eprintln!("{}", error); + process::exit(1); + } + }; + + disassembler.disassemble_controlflow(entrypoints.clone(), &mut cfg) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + } else if pe.is_dotnet() { + let disassembler = match CILDisassembler::new(pe.architecture(), &image, executable_address_ranges.clone()) { + Ok(disassembler) => disassembler, + Err(error) => { + eprintln!("{}", error); + process::exit(1); + } + }; + + disassembler.disassemble_controlflow(entrypoints.clone(), &mut cfg) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + } else { + eprintln!("invalid or unsupported pe file"); + process::exit(1); + } + + process_output(output, enable_instructions, &cfg, &attributes, &function_symbols); +} + +fn process_elf(input: String, config: Config, tags: Option>, output: Option, enable_instructions: bool) { + let mut attributes = Attributes::new(); + + let elf = ELF::new(input, config.clone()).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + match elf.architecture() { + Architecture::UNKNOWN => { + eprintln!("unsupported pe architecture"); + process::exit(1); + }, + _ => {} + } + + if !config.general.minimal { + let file_attribute = elf.file.attribute(); + if tags.is_some() { + for tag in tags.unwrap() { + attributes.push(Tag::new(tag).attribute()); + } + } + attributes.push(file_attribute); + } + + let function_symbols = get_elf_function_symbols(&elf); + + let mapped_file = elf.image() + .unwrap_or_else(|error| { eprintln!("{}", error); process::exit(1)}); + + let image = mapped_file + .mmap() + .unwrap_or_else(|error| { eprintln!("{}", error); process::exit(1); }); + + let executable_address_ranges = elf.executable_virtual_address_ranges(); + + let mut entrypoints = BTreeSet::::new(); + + entrypoints.extend(elf.entrypoints()); + + let mut cfg = Graph::new(elf.architecture(), config.clone()); + + let disassembler = match Disassembler::new(elf.architecture(), &image, executable_address_ranges.clone()) { + Ok(disassembler) => disassembler, + Err(error) => { + eprintln!("{}", error); + process::exit(1); + } + }; + + disassembler.disassemble_controlflow(entrypoints, &mut cfg) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + process_output(output, enable_instructions, &cfg, &attributes, &function_symbols); +} + +fn process_code(input: String, config: Config, architecture: Architecture, output: Option, enable_instructions: bool) { + let mut attributes = Attributes::new(); + + let mut file = BLFile::new(input, config.clone()).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + file.read() + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + let mut cfg = Graph::new(architecture, config.clone()); + + let mut executable_address_ranges = BTreeMap::::new(); + executable_address_ranges.insert(0, file.size()); + + let mut entrypoints = BTreeSet::::new(); + + entrypoints.insert(0x00); + + match architecture { + Architecture::AMD64 | Architecture::I386 => { + let disassembler = match Disassembler::new(architecture, &file.data, executable_address_ranges.clone()) { + Ok(disassembler) => disassembler, + Err(error) => { + eprintln!("{}", error); + process::exit(1); + } + }; + + disassembler.disassemble_controlflow(entrypoints, &mut cfg) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + }, + Architecture::CIL => { + let disassembler = match CILDisassembler::new(architecture, &file.data, executable_address_ranges.clone()) { + Ok(disassembler) => disassembler, + Err(error) => { + eprintln!("{}", error); + process::exit(1); + } + }; + + disassembler.disassemble_controlflow(entrypoints, &mut cfg) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + }, + _ => {}, + } + + attributes.push(file.attribute()); + + let function_symbols = BTreeMap::::new(); + + process_output(output, enable_instructions, &cfg, &attributes, &function_symbols); +} + +fn process_macho(input: String, config: Config, tags: Option>, output: Option, enable_instructions: bool) { + let mut attributes = Attributes::new(); + + let macho = MACHO::new(input, config.clone()).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + for slice in 0..macho.number_of_slices() { + let architecture = macho.architecture(slice); + if architecture.is_none() { continue; } + let architecture = architecture.unwrap(); + if architecture == Architecture::UNKNOWN { continue; } + + let tags = tags.clone(); + + if !config.general.minimal { + let file_attribute = macho.file.attribute(); + if tags.is_some() { + for tag in tags.unwrap() { + attributes.push(Tag::new(tag).attribute()); + } + } + attributes.push(file_attribute); + } + + let function_symbols = get_macho_function_symbols(&macho); + + let mapped_file = macho.image(slice) + .unwrap_or_else(|error| { eprintln!("{}", error); process::exit(1)}); + + let image = mapped_file + .mmap() + .unwrap_or_else(|error| { eprintln!("{}", error); process::exit(1); }); + + let executable_address_ranges = macho.executable_virtual_address_ranges(slice); + + let mut entrypoints = BTreeSet::::new(); + + entrypoints.extend(macho.entrypoints(slice)); + + let mut cfg = Graph::new(architecture, config.clone()); + + let disassembler = match Disassembler::new(architecture, &image, executable_address_ranges.clone()) { + Ok(disassembler) => disassembler, + Err(error) => { + eprintln!("{}", error); + process::exit(1); + } + }; + + disassembler.disassemble_controlflow(entrypoints, &mut cfg) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + process_output(output.clone(), enable_instructions, &cfg, &attributes, &function_symbols); + } +} + +fn main() { + + let args = Args::parse(); + + validate_args(&args); + + let mut config = Config::new(); + + let _ = config.write_default(); + + if args.config.is_some() { + match Config::from_file(&args.config.unwrap().to_string()) { + Ok(result) => { + config = result; + }, + Err(error) => { + eprintln!("{}", error); + process::exit(1); + } + } + } else { + if let Err(error) = config.from_default() { + eprintln!("failed to read default config: {}", error); + process::exit(1); + } + } + + if args.debug != false { + config.general.debug = args.debug; + } + + if args.threads.is_some() { + config.general.threads = args.threads.unwrap(); + } + + if args.disable_heuristics == true { + config.disable_heuristics(); + } + + if args.disable_hashing == true { + config.disable_hashing(); + } + + if args.mmap_directory.is_some() { + config.mmap.directory = args.mmap_directory.unwrap(); + } + + if args.enable_mmap_cache != false { + config.mmap.cache.enabled = args.enable_mmap_cache; + } + + if args.disable_disassembler_sweep == true { + config.disassembler.sweep.enabled = false; + } + + if args.minimal == true || config.general.minimal == true { + config.enable_minimal(); + } + + Stderr::print_debug(config.clone(), "finished reading arguments and configuration"); + + ThreadPoolBuilder::new() + .num_threads(config.general.threads) + .build_global() + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + if args.architecture.is_none() { + let format = Format::from_file(args.input.clone()) + .unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + match format { + Format::PE => { + Stderr::print_debug(config.clone(), "processing pe"); + process_pe(args.input, config, args.tags, args.output, args.enable_instructions); + }, + Format::ELF => { + Stderr::print_debug(config.clone(), "processing elf"); + process_elf(args.input, config, args.tags, args.output, args.enable_instructions); + }, + Format::MACHO => { + Stderr::print_debug(config.clone(), "processing macho"); + process_macho(args.input, config, args.tags, args.output, args.enable_instructions); + } + _ => { + eprintln!("unable to identify file format"); + process::exit(1); + } + } + } else { + let architecture = args.architecture.unwrap(); + match architecture { + Architecture::AMD64 | Architecture::I386 | Architecture::CIL => { + Stderr::print_debug(config.clone(), "processing code"); + process_code(args.input, config, architecture, args.output, args.enable_instructions); + }, + _ => { + eprintln!("unsupported architecture"); + process::exit(1); + } + } + } + + process::exit(0); + +} diff --git a/src/bin/blcompare.rs b/src/bin/blcompare.rs new file mode 100644 index 00000000..69621028 --- /dev/null +++ b/src/bin/blcompare.rs @@ -0,0 +1,233 @@ +use std::process; +use std::path::Path; +use std::sync::Arc; + +use clap::Parser; +use glob::glob; +use rayon::prelude::*; +use rayon::ThreadPoolBuilder; +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +use binlex::{AUTHOR, VERSION}; +use binlex::hashing::TLSH; +use binlex::io::{JSON, Stdout}; + +/// Structure to represent the comparison result between two JSON entries. +#[derive(Serialize, Deserialize)] +pub struct ComparisonJson { + /// The type of this entity, always `"comparison"`. + #[serde(rename = "type")] + pub type_: String, + /// The JSON entry from the LHS. + pub lhs: Value, + /// The JSON entry from the RHS. + pub rhs: Value, + /// TLSH Similarity Score + pub tlsh: Option, +} + +#[derive(Parser, Debug)] +#[command( + name = "blcompare", + version = VERSION, + about = format!("A Binlex Trait Comparison Tool\n\nVersion: {}", VERSION), + after_help = format!("Author: {}", AUTHOR), +)] +struct Args { + /// Input file or wildcard pattern for LHS (Left-Hand Side). + #[arg(long)] + input_lhs: Option, + + /// Input file or wildcard pattern for RHS (Right-Hand Side). + #[arg(long)] + input_rhs: String, + + /// Number of threads to use. + #[arg(short, long, default_value_t = 1)] + pub threads: usize, + + /// Enable recursive wildcard expansion. + #[arg(short = 'r', long = "recursive")] + pub recursive: bool, +} + +fn main() { + let args = Args::parse(); + + initialize_thread_pool(args.threads); + + let rhs_files = expand_paths(&args.input_rhs, args.recursive); + if rhs_files.is_empty() { + eprintln!("No RHS files matched the pattern."); + process::exit(1); + } + + match args.input_lhs { + Some(lhs_pattern) => handle_lhs_files(&lhs_pattern, &rhs_files, args.recursive), + None => handle_stdin_lhs(&rhs_files), + } + + process::exit(0); +} + +fn initialize_thread_pool(num_threads: usize) { + ThreadPoolBuilder::new() + .num_threads(num_threads) + .build_global() + .unwrap_or_else(|error| { + eprintln!("Error building thread pool: {}", error); + process::exit(1); + }); +} + +fn expand_paths(pattern: &str, recursive: bool) -> Vec { + let modified_pattern = if recursive { + let path = Path::new(pattern); + let parent = path.parent().unwrap_or_else(|| Path::new(".")); + let file_pattern = path.file_name().unwrap_or_default().to_str().unwrap_or(""); + parent.join("**").join(file_pattern).to_string_lossy().to_string() + } else { + pattern.to_string() + }; + + glob(&modified_pattern) + .expect("Failed to read glob pattern") + .filter_map(|entry| { + match entry { + Ok(path) if path.is_file() => Some(path.to_string_lossy().to_string()), + Ok(_) => None, + Err(e) => { + eprintln!("Glob pattern error: {:?}", e); + None + } + } + }) + .collect() +} + +fn handle_lhs_files(lhs_pattern: &str, rhs_files: &[String], recursive: bool) { + let lhs_files = expand_paths(lhs_pattern, recursive); + if lhs_files.is_empty() { + eprintln!("No LHS files matched the pattern."); + process::exit(1); + } + + let pairs: Vec<(String, String)> = lhs_files + .iter() + .flat_map(|lhs| rhs_files.iter().map(move |rhs| (lhs.clone(), rhs.clone()))) + .collect(); + + pairs.par_iter().for_each(|(lhs_path, rhs_path)| { + let json_lhs = load_json_with_filter(lhs_path); + let json_rhs = load_json_with_filter(rhs_path); + + if let (Some(json_lhs), Some(json_rhs)) = (json_lhs, json_rhs) { + compare_json_entries(&json_lhs, &json_rhs); + } + }); +} + +fn handle_stdin_lhs(rhs_files: &[String]) { + let json_lhs = match JSON::from_stdin_with_filter(filter_json) { + Ok(json) => Arc::new(json), + Err(e) => { + eprintln!("Error reading LHS from stdin: {}", e); + process::exit(1); + } + }; + + eprintln!( + "Starting comparisons: 1 LHS (from stdin) x {} RHS files = {} pairs.", + rhs_files.len(), + rhs_files.len() + ); + + rhs_files.par_iter().for_each(|rhs_path| { + let json_rhs = load_json_with_filter(rhs_path); + + if let Some(json_rhs) = json_rhs { + compare_json_entries(&json_lhs, &json_rhs); + } + }); +} + +fn load_json_with_filter(path: &str) -> Option { + match JSON::from_file_with_filter(path, filter_json) { + Ok(json) => Some(json), + Err(e) => { + eprintln!("{}", e); + None + } + } +} + +fn filter_json(value: &mut Value) -> bool { + value.get("architecture").and_then(|v| v.as_str()).is_some() + && value + .get("signature") + .and_then(|v| v.get("tlsh")) + .and_then(|v| v.as_str()) + .is_some() +} + +fn compare_json_entries(json_lhs: &JSON, json_rhs: &JSON) { + let lhs_entries = json_lhs.values(); + let rhs_entries: Vec = json_rhs.values().into_iter().cloned().collect(); + + for lhs in lhs_entries { + let lhs_type = match extract_field(lhs, "type") { + Some(t) => t, + None => continue, + }; + let lhs_arch = match extract_field(lhs, "architecture") { + Some(a) => a, + None => continue, + }; + let lhs_tlsh = match extract_nested_field(lhs, "signature", "tlsh") { + Some(t) => t, + None => continue, + }; + + for rhs in &rhs_entries { + let rhs_type = match extract_field(rhs, "type") { + Some(t) => t, + None => continue, + }; + let rhs_arch = match extract_field(rhs, "architecture") { + Some(a) => a, + None => continue, + }; + let rhs_tlsh = match extract_nested_field(rhs, "signature", "tlsh") { + Some(t) => t, + None => continue, + }; + + if lhs_type != rhs_type || lhs_arch != rhs_arch { + continue; + } + + let tlsh_similarity = TLSH::compare(lhs_tlsh.clone(), rhs_tlsh.clone()).ok(); + + let comparison = ComparisonJson { + type_: "comparison".to_string(), + lhs: lhs.clone(), + rhs: rhs.clone(), + tlsh: tlsh_similarity, + }; + + match serde_json::to_string(&comparison) { + Ok(serialized) => Stdout::print(serialized), + Err(e) => eprintln!("Serialization error: {}", e), + } + } + } +} + +fn extract_field<'a>(value: &'a Value, field: &str) -> Option { + value.get(field)?.as_str().map(String::from) +} + +fn extract_nested_field<'a>(value: &'a Value, field: &str, subfield: &str) -> Option { + value.get(field)?.get(subfield)?.as_str().map(String::from) +} diff --git a/src/bin/blelfsym.rs b/src/bin/blelfsym.rs new file mode 100644 index 00000000..02aa3a51 --- /dev/null +++ b/src/bin/blelfsym.rs @@ -0,0 +1,77 @@ +use std::process; +use std::fs::File; +use std::io::Write; +use binlex::io::Stdout; +use clap::Parser; +use binlex::AUTHOR; +use binlex::VERSION; +use binlex::formats::ELF; +use binlex::Config; +use binlex::controlflow::SymbolIoJson; +use binlex::io::Stdin; +use binlex::types::LZ4String; + +#[derive(Parser, Debug)] +#[command( + name = "blelfsym", + version = VERSION, + about = format!("A Binlex ELF Symbol Parsing Tool\n\nVersion: {}", VERSION), + after_help = format!("Author: {}", AUTHOR), +)] +struct Args { + #[arg(short, long, required = true)] + input: String, + #[arg(short, long)] + output: Option, +} + +fn main() -> pdb::Result<()> { + let args = Args::parse(); + + let config = Config::new(); + + let elf = ELF::new(args.input, config).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + let mut symbols = Vec::::new(); + for (_, symbol) in elf.symbols() { + let symbol = SymbolIoJson{ + type_: "symbol".to_string(), + symbol_type: "function".to_string(), + name: symbol.name, + file_offset: None, + relative_virtual_address: None, + virtual_address: Some(symbol.address), + slice: None, + }; + if let Ok(string) = serde_json::to_string(&symbol) { + symbols.push(LZ4String::new(&string)); + } + } + + Stdin::passthrough(); + + if args.output.is_none() { + for symbol in symbols { + Stdout::print(symbol); + } + } else { + let mut file = match File::create(args.output.unwrap()) { + Ok(file) => file, + Err(error) => { + eprintln!("{}", error); + std::process::exit(1); + } + }; + for symbol in symbols { + if let Err(error) = writeln!(file, "{}", symbol) { + eprintln!("{}", error); + std::process::exit(1); + } + } + } + + process::exit(0); +} diff --git a/src/bin/blhash.rs b/src/bin/blhash.rs new file mode 100644 index 00000000..b4568eb2 --- /dev/null +++ b/src/bin/blhash.rs @@ -0,0 +1,82 @@ +use std::process; +use clap::Parser; +use binlex::AUTHOR; +use binlex::VERSION; +use clap::ValueEnum; +use std::fmt; +use binlex::Config; +use binlex::formats::File; + +#[derive(Parser, Debug)] +#[command( + name = "blhash", + version = VERSION, + about = format!("A Binlex File Hashing Tool\n\nVersion: {}", VERSION), + after_help = format!("Author: {}", AUTHOR), +)] +struct Args { + #[arg(short, long)] + input: String, + #[arg(long, value_enum, default_value = "tlsh")] + hashtype: HashType, +} + + +#[derive(Debug, Clone, ValueEnum)] +pub enum HashType { + Sha256, + Tlsh, +} + +impl fmt::Display for HashType { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + match self { + HashType::Sha256 => "sha256", + HashType::Tlsh => "tlsh", + } + ) + } +} + +fn main () { + + let mut config = Config::new(); + + config.formats.file.hashing.tlsh.enabled = true; + config.formats.file.hashing.sha256.enabled = true; + config.formats.file.hashing.minhash.enabled = true; + + let args = Args::parse(); + + let mut file = File::new(args.input, config).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + file.read().unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + let hash = match args.hashtype.to_string().as_str() { + "sha256" => { + file.sha256() + }, + "tlsh" => { + file.tlsh() + }, + _ => { None } + }; + + if hash.is_some() { + println!("{}", hash.unwrap()); + } else { + eprintln!("unable to calculate hash"); + process::exit(1); + } + + process::exit(0); +} diff --git a/src/bin/blimage.rs b/src/bin/blimage.rs new file mode 100644 index 00000000..c9f36eb4 --- /dev/null +++ b/src/bin/blimage.rs @@ -0,0 +1,190 @@ +use std::fs::File; +use std::io::Read; +use std::process; +use clap::Parser; +use binlex::AUTHOR; +use binlex::VERSION; +use clap::ValueEnum; +use std::fmt; +use binlex::io::Stdout; +use std::collections::BTreeMap; +use binlex::hashing::SHA256; + +#[derive(Parser, Debug)] +#[command( + name = "blimage", + version = VERSION, + about = format!("A Binlex Binary Visualization Tool\n\nVersion: {}", VERSION), + after_help = format!("Author: {}", AUTHOR), +)] +struct Args { + #[arg(short, long)] + input: String, + #[arg(short, long)] + output: Option, + #[arg(short, long, value_enum, default_value = "grayscale")] + color: ColorMap, + #[arg(short, long, default_value_t = 10)] + shape_size: usize, +} + +#[derive(Debug, Clone, ValueEnum)] +pub enum ColorMap { + Grayscale, + Heatmap, + Bluegreen, + Redblack, +} + +impl fmt::Display for ColorMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + match self { + ColorMap::Grayscale => "grayscale", + ColorMap::Heatmap => "heatmap", + ColorMap::Bluegreen => "bluegreen", + ColorMap::Redblack => "redblack", + } + ) + } +} + +impl ColorMap { + + pub fn map_byte(&self, byte: u8) -> String { + match self { + ColorMap::Grayscale => format!("rgb({},{},{})", byte, byte, byte), + ColorMap::Heatmap => { + let r = (byte as f32 * 1.2).min(255.0) as u8; + let g = (255 - byte).max(0) as u8; + let b = (byte as f32 * 0.5).min(255.0) as u8; + format!("rgb({},{},{})", r, g, b) + } + ColorMap::Bluegreen => { + let r = (byte as f32 * 0.2).min(255.0) as u8; + let g = (byte as f32 * 0.8).min(255.0) as u8; + let b = (255 - byte).max(0) as u8; + format!("rgb({},{},{})", r, g, b) + } + ColorMap::Redblack => { + let r = byte; + let g = 0; + let b = 0; + format!("rgb({},{},{})", r, g, b) + } + } + } +} + +fn main() { + + let args = Args::parse(); + + let colormap = ColorMap::from_str(&args.color.to_string(), false).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + if args.output.is_some() { + let mut file = File::open(args.input).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + let mut byte_data = Vec::new(); + + file.read_to_end(&mut byte_data).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + let mut metadata = BTreeMap::::new(); + metadata.insert("Hash".to_string(), "sha256:".to_string() + &SHA256::new(&byte_data).hexdigest().unwrap()); + + let svg_content = bytes_to_svg(&byte_data, args.shape_size, &colormap, metadata); + + std::fs::write(args.output.unwrap(), svg_content).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + } else { + let mut file = File::open(args.input).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + let mut byte_data = Vec::new(); + + file.read_to_end(&mut byte_data).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + let mut metadata = BTreeMap::::new(); + metadata.insert("Hash".to_string(), "sha256:".to_string() + &SHA256::new(&byte_data).hexdigest().unwrap()); + + let svg_content = bytes_to_svg(&byte_data, args.shape_size, &colormap, metadata); + Stdout::print(svg_content); + } + + process::exit(0); +} + + +fn map_to_svg_metadata(metadata: BTreeMap::) -> String { + let mut svg = String::new(); + for (key, value) in metadata { + svg.push_str(r#"\n"#); + svg.push_str(&format!(r#"<{}>{}\n"#, key, value, key)); + svg.push_str(r#"\n"#); + } + svg +} + +/// Converts byte data into an SVG representation with a given colormap +/// +/// # Arguments +/// +/// * `byte_data` - A slice of bytes to visualize. +/// * `shape_size` - The size of each rectangle in the grid (in pixels). +/// * `colormap` - The colormap to use for color mapping. +fn bytes_to_svg(byte_data: &[u8], shape_size: usize, colormap: &ColorMap, metadata: BTreeMap::) -> String { + let num_bytes = byte_data.len(); + let grid_size = (num_bytes as f64).sqrt().ceil() as usize; + + let mut svg = String::new(); + + // SVG Header + svg.push_str(&format!( + r#"\n"#, + grid_size * shape_size, + grid_size * shape_size, + grid_size * shape_size, + grid_size * shape_size + )); + + svg.push_str(&map_to_svg_metadata(metadata)); + + // Add rectangles for each byte + for (i, &byte) in byte_data.iter().enumerate() { + let row = i / grid_size; + let col = i % grid_size; + + let x = col * shape_size; + let y = row * shape_size; + + let color = colormap.map_byte(byte); + + svg.push_str(&format!( + r#"\n"#, + x, y, shape_size, shape_size, color + )); + } + + // SVG Footer + svg.push_str("\n"); + + svg +} diff --git a/src/bin/blmachosym.rs b/src/bin/blmachosym.rs new file mode 100644 index 00000000..9b3bc32b --- /dev/null +++ b/src/bin/blmachosym.rs @@ -0,0 +1,79 @@ +use std::process; +use binlex::formats::MACHO; +use binlex::io::Stdout; +use clap::Parser; +use std::fs::File; +use std::io::Write; +use binlex::AUTHOR; +use binlex::VERSION; +use binlex::Config; +use binlex::controlflow::SymbolIoJson; +use binlex::io::Stdin; +use binlex::types::LZ4String; + +#[derive(Parser, Debug)] +#[command( + name = "blmachosym", + version = VERSION, + about = format!("A Binlex MachO Symbol Parsing Tool\n\nVersion: {}", VERSION), + after_help = format!("Author: {}", AUTHOR), +)] +struct Args { + #[arg(short, long, required = true)] + input: String, + #[arg(short, long)] + output: Option, +} + +fn main() -> pdb::Result<()> { + let args = Args::parse(); + + let config = Config::new(); + + let macho = MACHO::new(args.input, config).unwrap_or_else(|error| { + eprintln!("{}", error); + process::exit(1); + }); + + let mut symbols = Vec::::new(); + for slice in 0..macho.number_of_slices() { + for (_, symbol) in macho.symbols(slice) { + let symbol = SymbolIoJson{ + type_: "symbol".to_string(), + symbol_type: "function".to_string(), + name: symbol.name, + file_offset: None, + relative_virtual_address: None, + virtual_address: Some(symbol.address), + slice: Some(slice), + }; + if let Ok(string) = serde_json::to_string(&symbol) { + symbols.push(LZ4String::new(&string)); + } + } + } + + Stdin::passthrough(); + + if args.output.is_none() { + for symbol in symbols { + Stdout::print(symbol); + } + } else { + let mut file = match File::create(args.output.unwrap()) { + Ok(file) => file, + Err(error) => { + eprintln!("{}", error); + std::process::exit(1); + } + }; + for symbol in symbols { + if let Err(error) = writeln!(file, "{}", symbol) { + eprintln!("{}", error); + std::process::exit(1); + } + } + } + + process::exit(0); +} diff --git a/src/bin/blpdb.rs b/src/bin/blpdb.rs new file mode 100644 index 00000000..c2d6afe0 --- /dev/null +++ b/src/bin/blpdb.rs @@ -0,0 +1,70 @@ +use std::process; +use binlex::controlflow::SymbolIoJson; +use clap::Parser; +use pdb::FallibleIterator; +use std::fs::File; +use binlex::io::Stdin; +use binlex::io::Stdout; +use binlex::controlflow::Symbol; +use binlex::AUTHOR; +use binlex::VERSION; + +#[derive(Parser, Debug)] +#[command( + name = "blpdb", + version = VERSION, + about = format!("A Binlex PDB Parsing Tool\n\nVersion: {}", VERSION), + after_help = format!("Author: {}", AUTHOR), +)] +struct Cli { + #[arg(short, long, required = true)] + input: String, + #[arg(short, long)] + output: Option, + #[arg(long, default_value_t = false)] + demangle_msvc_names: bool +} + +fn main() -> pdb::Result<()> { + let cli = Cli::parse(); + + let file = File::open(cli.input)?; + let mut pdb = pdb::PDB::open(file)?; + + let symbol_table = pdb.global_symbols()?; + let address_map = pdb.address_map()?; + + let mut results = Vec::::new(); + let mut symbols = symbol_table.iter(); + while let Some(symbol) = symbols.next()? { + match symbol.parse() { + Ok(pdb::SymbolData::Public(data)) if data.function => { + let rva = data.offset.to_rva(&address_map).unwrap_or_default(); + let mut name = data.name.to_string().into_owned(); + if cli.demangle_msvc_names { + name = Symbol::demangle_msvc_name(&name); + } + results.push(SymbolIoJson{ + type_: "symbol".to_string(), + symbol_type: "function".to_string(), + name: name, + file_offset: None, + relative_virtual_address: Some(rva.0 as u64), + virtual_address: None, + slice: None, + }); + } + _ => {} + } + } + + Stdin::passthrough(); + + for result in results { + if let Ok(json_string) = serde_json::to_string(&result){ + Stdout::print(json_string); + } + } + + process::exit(0); +} diff --git a/src/bin/blrizin.rs b/src/bin/blrizin.rs new file mode 100644 index 00000000..4f8494f4 --- /dev/null +++ b/src/bin/blrizin.rs @@ -0,0 +1,86 @@ +use clap::Parser; +use serde_json::Value; +use std::fs::File; +use std::io::Error; +use std::io::Write; +use std::process; +use binlex::types::LZ4String; +use binlex::AUTHOR; +use binlex::VERSION; +use binlex::io::Stdout; +use binlex::io::JSON; +use binlex::controlflow::SymbolIoJson; + +#[derive(Parser, Debug)] +#[command( + name = "blrizin", + version = VERSION, + about = format!("A Binlex Rizin Tool\n\nVersion: {}", VERSION), + after_help = format!("Author: {}", AUTHOR), +)] +struct Args { + #[arg(short, long)] + input: Option, + #[arg(short, long)] + output: Option, +} + +fn process_value(parsed: &Value) -> Result { + let virtual_address = parsed.get("offset").unwrap().as_u64().unwrap(); + let function_name = parsed.get("name").unwrap().as_str().unwrap().to_string(); + let symbol = SymbolIoJson { + type_: "symbol".to_string(), + symbol_type: "function".to_string(), + name: function_name, + file_offset: None, + relative_virtual_address: None, + virtual_address: Some(virtual_address), + slice: None, + }; + let result = serde_json::to_string(&symbol)?; + Ok(LZ4String::new(&result)) +} + +fn main() { + let args = Args::parse(); + let json = JSON::from_file_or_stdin_as_array(args.input, |value| { + let object = match value.as_object() { + Some(object) => object, + None => return false, + }; + let virtual_address = object.get("offset").and_then(|v| v.as_u64()); + let function_name = object.get("name").and_then(|v| v.as_str()).map(String::from); + + if virtual_address.is_none() || function_name.is_none() { + return false; + } + true + }); + + if args.output.is_none() && json.is_ok(){ + for value in json.unwrap().values() { + if let Ok(string) = process_value(value) { + Stdout::print(string); + } + } + } else if args.output.is_some() && json.is_ok() { + let mut file = match File::create(args.output.unwrap()) { + Ok(file) => file, + Err(error) => { + eprintln!("{}", error); + std::process::exit(1); + } + }; + for value in json.unwrap().values() { + if let Ok(string) = process_value(value) { + if let Err(error) = writeln!(file, "{}", string) { + eprintln!("{}", error); + std::process::exit(1); + } + } + } + } + + process::exit(0); + +} diff --git a/src/bin/blscaler.rs b/src/bin/blscaler.rs new file mode 100644 index 00000000..b352f9b4 --- /dev/null +++ b/src/bin/blscaler.rs @@ -0,0 +1,108 @@ +use clap::Parser; +use rayon::prelude::*; +use serde_json::Value; +use serde_json::Number; +use std::fs::File; +use std::io::Write; +use std::process; +use rayon::ThreadPoolBuilder; +use binlex::types::LZ4String; +use binlex::AUTHOR; +use binlex::VERSION; +use binlex::io::Stdout; +use binlex::io::JSON; + +#[derive(Parser, Debug)] +#[command( + name = "blscaler", + version = VERSION, + about = format!("A Binlex ML Scaler Tool\n\nVersion: {}", VERSION), + after_help = format!("Author: {}", AUTHOR), +)] +struct Args { + #[arg(short, long)] + input: Option, + #[arg(short, long)] + output: Option, + #[arg(short, long, default_value_t = 1)] + threads: usize +} + +fn normalize(data: &[f64]) -> Vec { + let min = data.iter().cloned().fold(f64::INFINITY, f64::min); + let max = data.iter().cloned().fold(f64::NEG_INFINITY, f64::max); + data.iter().map(|&x| (x - min) / (max - min)).collect() +} + +fn process_value(mut parsed: Value) -> String { + if let Some(feature) = parsed + .get_mut("signature") + .and_then(|signature| signature.get_mut("feature")) + { + if let Some(array) = feature.as_array() { + let values: Vec = array.iter().filter_map(|v| v.as_f64()).collect(); + let normalized_values = normalize(&values); + *feature = Value::Array( + normalized_values + .into_iter() + .filter_map(|num| Number::from_f64(num).map(Value::Number)) // Filter out non-finite numbers + .collect(), + ); + } + } + let result = match serde_json::to_string(&parsed) { + Ok(result) => result, + Err(error) => { + eprintln!("{}", error); + process::exit(1); + } + }; + return result; +} + +fn main() { + + let args = Args::parse(); + + ThreadPoolBuilder::new() + .num_threads(args.threads) + .build_global() + .expect("failed to build thread pool"); + + let j = match JSON::from_file_or_stdin(args.input) { + Ok(j) => j, + Err(error) => { + eprintln!("{}", error); + process::exit(1); + } + }; + + let results: Vec = j.values().into_par_iter() + .map(|value| { + LZ4String::from(process_value(value.clone())) + }) + .collect(); + + if let Some(output_file) = args.output { + let mut file = match File::create(output_file) { + Ok(file) => file, + Err(error) => { + eprintln!("{}", error); + std::process::exit(1); + } + }; + for result in results { + if let Err(error) = writeln!(file, "{}", result) { + eprintln!("{}", error); + std::process::exit(1); + } + } + } else { + results.iter().for_each(|result| { + Stdout::print(result); + }); + } + + process::exit(0); + +} diff --git a/src/bin/blyara.rs b/src/bin/blyara.rs new file mode 100644 index 00000000..0e7613b9 --- /dev/null +++ b/src/bin/blyara.rs @@ -0,0 +1,133 @@ +use clap::Parser; +use serde_json::{Map, Value}; +use std::fs::File; +use std::io::{self, BufRead, Write}; +use std::process; +use binlex::AUTHOR; +use binlex::VERSION; +use binlex::io::Stdout; + +#[derive(Parser, Debug)] +#[command( + name = "blyara", + version = VERSION, + about = format!("A Binlex YARA Generation Tool\n\nVersion: {}", VERSION), + after_help = format!("Author: {}", AUTHOR), +)] +struct Cli { + #[arg(short, long)] + input: Option, + #[arg( + short, + long, + num_args(2), + value_names = ["KEY", "VALUE"], + action = clap::ArgAction::Append + )] + metadata: Vec, + #[arg(short, long, required = true)] + name: String, + #[arg(short, long, default_value_t = 1)] + count: usize, + #[arg(short, long)] + output: Option, +} + +fn main() { + let cli = Cli::parse(); + + let metadata_map = collect_metadata(&cli.metadata); + let pattern_map = collect_patterns(&cli.input); + + if pattern_map.is_empty() { + eprintln!("no signature patterns collected."); + process::exit(1); + } + + let signature = generate_signature(&cli.name, &metadata_map, &pattern_map, cli.count); + + if let Some(output_file) = &cli.output { + if let Err(e) = write_to_file(output_file, &signature) { + eprintln!("failed to write yara rule to output file: {}", e); + process::exit(1); + } + } else { + Stdout::print(signature); + } +} + +fn collect_metadata(metadata_vec: &[String]) -> Map { + let mut metadata_map = Map::new(); + for chunk in metadata_vec.chunks(2) { + if let [key, value] = chunk { + metadata_map.insert(key.clone(), Value::String(value.clone())); + } + } + metadata_map +} + +fn collect_patterns(input_file: &Option) -> Map { + let reader: Box = match input_file { + Some(file_name) => { + let file = File::open(file_name).unwrap_or_else(|_| { + eprintln!("failed to open input file: {}", file_name); + process::exit(1); + }); + Box::new(io::BufReader::new(file)) + } + None => Box::new(io::BufReader::new(io::stdin())), + }; + + let mut pattern_map = Map::new(); + for (count, line) in reader.lines().enumerate() { + match line { + Ok(l) => { + pattern_map.insert( + format!("trait_{}", count), + Value::String(format!("{{{}}}", l)), + ); + } + Err(e) => { + eprintln!("failed to read line: {}", e); + process::exit(1); + } + } + } + pattern_map +} + +fn generate_signature( + name: &str, + metadata_map: &Map, + pattern_map: &Map, + count: usize, +) -> String { + let mut signature = format!("rule {} {{\n", name); + + if !metadata_map.is_empty() { + signature.push_str(" meta:\n"); + for (key, value) in metadata_map { + signature.push_str(&format!(" {} = {}\n", key, value)); + } + } + + signature.push_str(" strings:\n"); + for (key, value) in pattern_map { + signature.push_str(&format!( + " ${} = {}\n", + key, + value.as_str().unwrap_or("") + )); + } + + signature.push_str(" condition:\n"); + signature.push_str(&format!(" {} of them\n", count)); + signature.push_str("}\n"); + + signature +} + +fn write_to_file(output_file: &str, content: &str) -> io::Result<()> { + let mut file = File::create(output_file)?; + file.write_all(content.as_bytes()) +} diff --git a/src/binary.rs b/src/binary.rs new file mode 100644 index 00000000..1b678a0c --- /dev/null +++ b/src/binary.rs @@ -0,0 +1,95 @@ +use std::collections::HashMap; + +/// A struct representing a binary, used for various binary-related utilities. + +pub struct Binary; + +impl Binary { + + /// Calculates the entropy of the given byte slice. + /// + /// This method computes the Shannon entropy, which is a measure of the randomness + /// or unpredictability of the data. The entropy value is returned as an `Option`. + /// + /// # Arguments + /// + /// * `bytes` - A reference to a `Vec` containing the binary data. + /// + /// # Returns + /// + /// An `Option`, where `Some(f64)` is the calculated entropy, or `None` if the data + /// is empty. + pub fn entropy(bytes: &Vec) -> Option { + let mut frequency: HashMap = HashMap::new(); + for &byte in bytes { + *frequency.entry(byte).or_insert(0) += 1; + } + + let data_len = bytes.len() as f64; + if data_len == 0.0 { + return None; + } + + let entropy = frequency.values().fold(0.0, |entropy, &count| { + let probability = count as f64 / data_len; + entropy - probability * probability.log2() + }); + + Some(entropy) + } + + /// Converts a byte slice to a hexadecimal string representation. + /// + /// This method takes a slice of bytes and returns a `String` where each byte is + /// represented as a 2-character hexadecimal string. + /// + /// # Arguments + /// + /// * `data` - A reference to a byte slice (`&[u8]`). + /// + /// # Returns + /// + /// A `String` containing the hexadecimal representation of the byte data. + pub fn to_hex(data: &[u8]) -> String { + data.iter() + .map(|byte| format!("{:02x}", byte)) + .collect::() + } + + /// Creates a human-readable hex dump of the provided byte data. + /// + /// This method formats the binary data into a string representation with both + /// hexadecimal values and ASCII characters, often used for debugging or inspecting + /// binary content. + /// + /// # Arguments + /// + /// * `data` - A reference to a byte slice (`&[u8]`). + /// * `address` - The starting memory address (in hexadecimal) to be used in the dump. + /// + /// # Returns + /// + /// A `String` formatted as a hex dump with both hexadecimal and ASCII views of the data. + #[allow(dead_code)] + pub fn hexdump(data: &[u8], address: u64) -> String { + const BYTES_PER_LINE: usize = 16; + let mut result = String::new(); + for (i, chunk) in data.chunks(BYTES_PER_LINE).enumerate() { + let current_address = address as usize + i * BYTES_PER_LINE; + let hex_repr = format!("{:08x}: ", current_address); + result.push_str(&hex_repr); + let hex_values: String = chunk.iter().map(|byte| format!("{:02x} ", byte)).collect(); + result.push_str(&hex_values); + let padding = " ".repeat(BYTES_PER_LINE - chunk.len()); + result.push_str(&padding); + let ascii_values: String = chunk + .iter() + .map(|&byte| if byte.is_ascii_graphic() || byte == b' ' { byte as char } else { '.' }) + .collect(); + result.push('|'); + result.push_str(&ascii_values); + result.push_str("|\n"); + } + result + } +} diff --git a/src/bindings/python/.gitignore b/src/bindings/python/.gitignore new file mode 100644 index 00000000..3f452f01 --- /dev/null +++ b/src/bindings/python/.gitignore @@ -0,0 +1,6 @@ +*.dll +*.exe +*.so +*.macho +venv/ +*.py diff --git a/src/bindings/python/Cargo.toml b/src/bindings/python/Cargo.toml new file mode 100644 index 00000000..840eceab --- /dev/null +++ b/src/bindings/python/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "pybinlex" +version = "2.0.0" +edition = "2021" + +[dependencies] +pyo3 = { version = "0.22.6", features = ["extension-module"] } +memmap2 = "0.9.5" + +[lib] +name = "binlex" +crate-type = ["cdylib"] + +[dependencies.binlex] +path = "../../../" diff --git a/src/bindings/python/src/binary.rs b/src/bindings/python/src/binary.rs new file mode 100644 index 00000000..2c771930 --- /dev/null +++ b/src/bindings/python/src/binary.rs @@ -0,0 +1,33 @@ +use pyo3::prelude::*; + +use binlex::binary::Binary as InnerBinary; + +#[pyclass] +pub struct Binary; + +#[pymethods] +impl Binary { + #[staticmethod] + pub fn entropy(bytes: Vec) -> Option { + InnerBinary::entropy(&bytes) + } + #[staticmethod] + pub fn to_hex(bytes: Vec) -> String { + InnerBinary::to_hex(&bytes) + } + #[staticmethod] + pub fn hexdump(bytes: Vec, address: u64) -> String { + InnerBinary::hexdump(&bytes, address) + } +} + +#[pymodule] +#[pyo3(name = "binary")] +pub fn binary_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.binary", m)?; + m.setattr("__name__", "binlex.binary")?; + Ok(()) +} diff --git a/src/bindings/python/src/config.rs b/src/bindings/python/src/config.rs new file mode 100644 index 00000000..b8dbcf20 --- /dev/null +++ b/src/bindings/python/src/config.rs @@ -0,0 +1,1323 @@ +use pyo3::prelude::*; +use std::sync::{Arc, Mutex}; +use binlex::Config as InnerConfig; + +use binlex::Architecture as InnerBinaryArchitecture; + +#[pyclass(eq)] +#[derive(PartialEq)] +pub struct Architecture { + pub inner: InnerBinaryArchitecture, +} + +#[pymethods] +impl Architecture { + #[new] + pub fn new(value: u16) -> Self { + let inner = match value { + 0x00 => InnerBinaryArchitecture::AMD64, + 0x01 => InnerBinaryArchitecture::I386, + _ => InnerBinaryArchitecture::UNKNOWN, + }; + Architecture { inner } + } + + #[getter] + pub fn get_value(&self) -> u16 { + self.inner as u16 + } +} + +#[pyclass] +pub struct ConfigChromosomes { + inner: Arc>, +} + +#[pymethods] +impl ConfigChromosomes { + #[getter] + pub fn get_hashing(&self) -> ConfigChromosomesHashing { + ConfigChromosomesHashing { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_heuristics(&self) -> ConfigChromosomesHeuristics { + ConfigChromosomesHeuristics { + inner: Arc::clone(&self.inner) + } + } +} + +#[pyclass] +pub struct ConfigChromosomesHeuristics { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigChromosomesHeuristics { + #[getter] + pub fn get_features(&self) -> ConfigChromosomesHeuristicsFeatures { + ConfigChromosomesHeuristicsFeatures { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_normalized(&self) -> ConfigChromosomesHeuristicsNormalization { + ConfigChromosomesHeuristicsNormalization { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_entropy(&self) -> ConfigChromosomesHeuristicsEntropy { + ConfigChromosomesHeuristicsEntropy { + inner: Arc::clone(&self.inner) + } + } +} + + +#[pyclass] +pub struct ConfigChromosomesHashing { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigChromosomesHashing { + #[getter] + pub fn get_sha256(&self) -> ConfigChromosomesHashingSHA256 { + ConfigChromosomesHashingSHA256 { + inner: Arc::clone(&self.inner) + } + } + + #[getter] + pub fn get_tlsh(&self) -> ConfigChromosomesHashingTLSH { + ConfigChromosomesHashingTLSH { + inner: Arc::clone(&self.inner) + } + } + + #[getter] + pub fn get_minhash(&self) -> ConfigChromosomesHashingMinhash { + ConfigChromosomesHashingMinhash { + inner: Arc::clone(&self.inner) + } + } +} + + +#[pyclass] +pub struct ConfigChromosomesHeuristicsEntropy { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigChromosomesHeuristicsEntropy { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.chromosomes.heuristics.entropy.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.chromosomes.heuristics.entropy.enabled = value; + } +} + +#[pyclass] +pub struct ConfigChromosomesHeuristicsNormalization { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigChromosomesHeuristicsNormalization { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.chromosomes.heuristics.normalized.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.chromosomes.heuristics.normalized.enabled = value; + } +} + +#[pyclass] +pub struct ConfigChromosomesHeuristicsFeatures { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigChromosomesHeuristicsFeatures { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.chromosomes.heuristics.features.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.chromosomes.heuristics.features.enabled = value; + } +} + +#[pyclass] +pub struct ConfigChromosomesHashingSHA256 { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigChromosomesHashingSHA256 { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.sha256.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.sha256.enabled = value; + } +} + +#[pyclass] +pub struct ConfigChromosomesHashingTLSH { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigChromosomesHashingTLSH { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.tlsh.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.tlsh.enabled = value; + } + + #[getter] + pub fn get_minimum_byte_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.tlsh.minimum_byte_size + } + + #[setter] + pub fn set_minimum_byte_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.tlsh.minimum_byte_size = value; + } +} + + +#[pyclass] +pub struct ConfigChromosomesHashingMinhash { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigChromosomesHashingMinhash { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.minhash.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.minhash.enabled = value; + } + + #[getter] + pub fn get_number_of_hashes(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.minhash.number_of_hashes + } + + #[setter] + pub fn set_number_of_hashes(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.minhash.number_of_hashes = value; + } + + #[getter] + pub fn get_shingle_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.minhash.shingle_size + } + + #[setter] + pub fn set_shingle_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.minhash.shingle_size = value; + } + + #[getter] + pub fn get_maximum_byte_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.minhash.maximum_byte_size + } + + #[setter] + pub fn set_maximum_byte_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.minhash.maximum_byte_size = value; + } + #[getter] + pub fn get_seed(&self) -> u64 { + let inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.minhash.seed + } + + #[setter] + pub fn set_seed(&mut self, value: u64) { + let mut inner = self.inner.lock().unwrap(); + inner.chromosomes.hashing.minhash.seed = value; + } +} + +// stop + +#[pyclass] +pub struct ConfigFunctions { + inner: Arc>, +} + +#[pymethods] +impl ConfigFunctions { + #[getter] + pub fn get_hashing(&self) -> ConfigFunctionsHashing { + ConfigFunctionsHashing { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_heuristics(&self) -> ConfigFunctionsHeuristics { + ConfigFunctionsHeuristics { + inner: Arc::clone(&self.inner) + } + } +} + +#[pyclass] +pub struct ConfigFunctionsHeuristics { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFunctionsHeuristics { + #[getter] + pub fn get_features(&self) -> ConfigFunctionsHeuristicsFeatures { + ConfigFunctionsHeuristicsFeatures { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_normalized(&self) -> ConfigFunctionsHeuristicsNormalization { + ConfigFunctionsHeuristicsNormalization { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_entropy(&self) -> ConfigFunctionsHeuristicsEntropy { + ConfigFunctionsHeuristicsEntropy { + inner: Arc::clone(&self.inner) + } + } +} + + +#[pyclass] +pub struct ConfigFunctionsHashing { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFunctionsHashing { + #[getter] + pub fn get_sha256(&self) -> ConfigFunctionsHashingSHA256 { + ConfigFunctionsHashingSHA256 { + inner: Arc::clone(&self.inner) + } + } + + #[getter] + pub fn get_tlsh(&self) -> ConfigFunctionsHashingTLSH { + ConfigFunctionsHashingTLSH { + inner: Arc::clone(&self.inner) + } + } + + #[getter] + pub fn get_minhash(&self) -> ConfigFunctionsHashingMinhash { + ConfigFunctionsHashingMinhash { + inner: Arc::clone(&self.inner) + } + } +} + + +#[pyclass] +pub struct ConfigFunctionsHeuristicsEntropy { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFunctionsHeuristicsEntropy { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.functions.heuristics.entropy.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.functions.heuristics.entropy.enabled = value; + } +} + +#[pyclass] +pub struct ConfigFunctionsHeuristicsNormalization { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFunctionsHeuristicsNormalization { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.functions.heuristics.normalized.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.functions.heuristics.normalized.enabled = value; + } +} + +#[pyclass] +pub struct ConfigFunctionsHeuristicsFeatures { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFunctionsHeuristicsFeatures { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.functions.heuristics.features.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.functions.heuristics.features.enabled = value; + } +} + +#[pyclass] +pub struct ConfigFunctionsHashingSHA256 { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFunctionsHashingSHA256 { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.functions.hashing.sha256.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.functions.hashing.sha256.enabled = value; + } +} + +#[pyclass] +pub struct ConfigFunctionsHashingTLSH { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFunctionsHashingTLSH { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.functions.hashing.tlsh.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.functions.hashing.tlsh.enabled = value; + } + + #[getter] + pub fn get_minimum_byte_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.functions.hashing.tlsh.minimum_byte_size + } + + #[setter] + pub fn set_minimum_byte_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.functions.hashing.tlsh.minimum_byte_size = value; + } +} + + +#[pyclass] +pub struct ConfigFunctionsHashingMinhash { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFunctionsHashingMinhash { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.functions.hashing.minhash.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.functions.hashing.minhash.enabled = value; + } + + #[getter] + pub fn get_number_of_hashes(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.functions.hashing.minhash.number_of_hashes + } + + #[setter] + pub fn set_number_of_hashes(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.functions.hashing.minhash.number_of_hashes = value; + } + + #[getter] + pub fn get_shingle_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.functions.hashing.minhash.shingle_size + } + + #[setter] + pub fn set_shingle_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.functions.hashing.minhash.shingle_size = value; + } + + #[getter] + pub fn get_maximum_byte_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.functions.hashing.minhash.maximum_byte_size + } + + #[setter] + pub fn set_maximum_byte_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.functions.hashing.minhash.maximum_byte_size = value; + } + #[getter] + pub fn get_seed(&self) -> u64 { + let inner = self.inner.lock().unwrap(); + inner.functions.hashing.minhash.seed + } + + #[setter] + pub fn set_seed(&mut self, value: u64) { + let mut inner = self.inner.lock().unwrap(); + inner.functions.hashing.minhash.seed = value; + } +} + +// stop + +#[pyclass] +pub struct ConfigBlocks { + inner: Arc>, +} + +#[pymethods] +impl ConfigBlocks { + #[getter] + pub fn get_hashing(&self) -> ConfigBlocksHashing { + ConfigBlocksHashing { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_heuristics(&self) -> ConfigBlocksHeuristics { + ConfigBlocksHeuristics { + inner: Arc::clone(&self.inner) + } + } +} + +#[pyclass] +pub struct ConfigBlocksHeuristics { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigBlocksHeuristics { + #[getter] + pub fn get_features(&self) -> ConfigBlocksHeuristicsFeatures { + ConfigBlocksHeuristicsFeatures { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_normalized(&self) -> ConfigBlocksHeuristicsNormalization { + ConfigBlocksHeuristicsNormalization { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_entropy(&self) -> ConfigBlocksHeuristicsEntropy { + ConfigBlocksHeuristicsEntropy { + inner: Arc::clone(&self.inner) + } + } +} + + +#[pyclass] +pub struct ConfigBlocksHashing { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigBlocksHashing { + #[getter] + pub fn get_sha256(&self) -> ConfigBlocksHashingSHA256 { + ConfigBlocksHashingSHA256 { + inner: Arc::clone(&self.inner) + } + } + + #[getter] + pub fn get_tlsh(&self) -> ConfigBlocksHashingTLSH { + ConfigBlocksHashingTLSH { + inner: Arc::clone(&self.inner) + } + } + + #[getter] + pub fn get_minhash(&self) -> ConfigBlocksHashingMinhash { + ConfigBlocksHashingMinhash { + inner: Arc::clone(&self.inner) + } + } +} + + +#[pyclass] +pub struct ConfigBlocksHeuristicsEntropy { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigBlocksHeuristicsEntropy { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.blocks.heuristics.entropy.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.blocks.heuristics.entropy.enabled = value; + } +} + +#[pyclass] +pub struct ConfigBlocksHeuristicsNormalization { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigBlocksHeuristicsNormalization { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.blocks.heuristics.normalized.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.blocks.heuristics.normalized.enabled = value; + } +} + +#[pyclass] +pub struct ConfigBlocksHeuristicsFeatures { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigBlocksHeuristicsFeatures { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.blocks.heuristics.features.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.blocks.heuristics.features.enabled = value; + } +} + +#[pyclass] +pub struct ConfigBlocksHashingSHA256 { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigBlocksHashingSHA256 { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.blocks.hashing.sha256.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.blocks.hashing.sha256.enabled = value; + } +} + +#[pyclass] +pub struct ConfigBlocksHashingTLSH { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigBlocksHashingTLSH { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.blocks.hashing.tlsh.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.blocks.hashing.tlsh.enabled = value; + } + + #[getter] + pub fn get_minimum_byte_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.blocks.hashing.tlsh.minimum_byte_size + } + + #[setter] + pub fn set_minimum_byte_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.blocks.hashing.tlsh.minimum_byte_size = value; + } +} + + +#[pyclass] +pub struct ConfigBlocksHashingMinhash { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigBlocksHashingMinhash { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.blocks.hashing.minhash.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.blocks.hashing.minhash.enabled = value; + } + + #[getter] + pub fn get_number_of_hashes(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.blocks.hashing.minhash.number_of_hashes + } + + #[setter] + pub fn set_number_of_hashes(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.blocks.hashing.minhash.number_of_hashes = value; + } + + #[getter] + pub fn get_shingle_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.blocks.hashing.minhash.shingle_size + } + + #[setter] + pub fn set_shingle_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.blocks.hashing.minhash.shingle_size = value; + } + + #[getter] + pub fn get_maximum_byte_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.blocks.hashing.minhash.maximum_byte_size + } + + #[setter] + pub fn set_maximum_byte_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.blocks.hashing.minhash.maximum_byte_size = value; + } + #[getter] + pub fn get_seed(&self) -> u64 { + let inner = self.inner.lock().unwrap(); + inner.blocks.hashing.minhash.seed + } + + #[setter] + pub fn set_seed(&mut self, value: u64) { + let mut inner = self.inner.lock().unwrap(); + inner.blocks.hashing.minhash.seed = value; + } +} + + +/// stop + +#[pyclass] +pub struct ConfigFormats { + inner: Arc>, +} + +#[pymethods] +impl ConfigFormats { + #[getter] + pub fn get_file(&self) -> ConfigFormatsFile { + ConfigFormatsFile { + inner: Arc::clone(&self.inner) + } + } +} + +#[pyclass] +pub struct ConfigFormatsFile { + inner: Arc>, +} + +#[pymethods] +impl ConfigFormatsFile { + #[getter] + pub fn get_hashing(&self) -> ConfigFormatsFileHashing { + ConfigFormatsFileHashing { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_heuristics(&self) -> ConfigFormatsFileHeuristics { + ConfigFormatsFileHeuristics { + inner: Arc::clone(&self.inner) + } + } +} + +#[pyclass] +pub struct ConfigFormatsFileHeuristics { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFormatsFileHeuristics { + #[getter] + pub fn get_features(&self) -> ConfigFormatsFileHeuristicsFeatures { + ConfigFormatsFileHeuristicsFeatures { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_normalized(&self) -> ConfigFormatsFileHeuristicsNormalization { + ConfigFormatsFileHeuristicsNormalization { + inner: Arc::clone(&self.inner) + } + } + #[getter] + pub fn get_entropy(&self) -> ConfigFormatsFileHeuristicsEntropy { + ConfigFormatsFileHeuristicsEntropy { + inner: Arc::clone(&self.inner) + } + } +} + + +#[pyclass] +pub struct ConfigFormatsFileHashing { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFormatsFileHashing { + #[getter] + pub fn get_sha256(&self) -> ConfigFormatsFileHashingSHA256 { + ConfigFormatsFileHashingSHA256 { + inner: Arc::clone(&self.inner) + } + } + + #[getter] + pub fn get_tlsh(&self) -> ConfigFormatsFileHashingTLSH { + ConfigFormatsFileHashingTLSH { + inner: Arc::clone(&self.inner) + } + } + + #[getter] + pub fn get_minhash(&self) -> ConfigFormatsFileHashingMinhash { + ConfigFormatsFileHashingMinhash { + inner: Arc::clone(&self.inner) + } + } +} + + +#[pyclass] +pub struct ConfigFormatsFileHeuristicsEntropy { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFormatsFileHeuristicsEntropy { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.formats.file.heuristics.entropy.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.formats.file.heuristics.entropy.enabled = value; + } +} + +#[pyclass] +pub struct ConfigFormatsFileHeuristicsNormalization { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFormatsFileHeuristicsNormalization { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.formats.file.heuristics.normalized.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.formats.file.heuristics.normalized.enabled = value; + } +} + +#[pyclass] +pub struct ConfigFormatsFileHeuristicsFeatures { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFormatsFileHeuristicsFeatures { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.formats.file.heuristics.features.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.formats.file.heuristics.features.enabled = value; + } +} + +#[pyclass] +pub struct ConfigFormatsFileHashingSHA256 { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFormatsFileHashingSHA256 { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.sha256.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.sha256.enabled = value; + } +} + +#[pyclass] +pub struct ConfigFormatsFileHashingTLSH { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFormatsFileHashingTLSH { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.tlsh.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.tlsh.enabled = value; + } + + #[getter] + pub fn get_minimum_byte_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.tlsh.minimum_byte_size + } + + #[setter] + pub fn set_minimum_byte_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.tlsh.minimum_byte_size = value; + } +} + + +#[pyclass] +pub struct ConfigFormatsFileHashingMinhash { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigFormatsFileHashingMinhash { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.minhash.enabled + } + + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.minhash.enabled = value; + } + + #[getter] + pub fn get_number_of_hashes(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.minhash.number_of_hashes + } + + #[setter] + pub fn set_number_of_hashes(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.minhash.number_of_hashes = value; + } + + #[getter] + pub fn get_shingle_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.minhash.shingle_size + } + + #[setter] + pub fn set_shingle_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.minhash.shingle_size = value; + } + + #[getter] + pub fn get_maximum_byte_size(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.minhash.maximum_byte_size + } + + #[setter] + pub fn set_maximum_byte_size(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.minhash.maximum_byte_size = value; + } + #[getter] + pub fn get_seed(&self) -> u64 { + let inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.minhash.seed + } + + #[setter] + pub fn set_seed(&mut self, value: u64) { + let mut inner = self.inner.lock().unwrap(); + inner.formats.file.hashing.minhash.seed = value; + } +} + +#[pyclass] +pub struct Config { + pub inner: Arc>, +} + +#[pymethods] +impl Config { + #[new] + pub fn new() -> Self { + Self { + inner: Arc::new(Mutex::new(InnerConfig::new())), + } + } + + #[getter] + pub fn get_general(&self) -> PyResult { + Ok(ConfigGeneral { + inner: Arc::clone(&self.inner), + }) + } + #[getter] + pub fn get_formats(&self) -> PyResult { + Ok(ConfigFormats { + inner: Arc::clone(&self.inner), + }) + } + + #[getter] + pub fn get_blocks(&self) -> PyResult { + Ok(ConfigBlocks { + inner: Arc::clone(&self.inner), + }) + } + + #[getter] + pub fn get_functions(&self) -> PyResult { + Ok(ConfigFunctions { + inner: Arc::clone(&self.inner), + }) + } + + #[getter] + pub fn get_chromosomes(&self) -> PyResult { + Ok(ConfigChromosomes { + inner: Arc::clone(&self.inner), + }) + } + + #[getter] + pub fn get_mmap(&self) -> PyResult { + Ok(ConfigMmap { + inner: Arc::clone(&self.inner), + }) + } + + #[getter] + pub fn get_disassembler(&self) -> PyResult { + Ok(ConfigDisassembler { + inner: Arc::clone(&self.inner), + }) + } + + pub fn enable_minimal(&mut self) { + self.inner.lock().unwrap().enable_minimal(); + } + + pub fn disable_hashing(&mut self) { + self.inner.lock().unwrap().disable_hashing(); + } + + pub fn disable_heuristics(&mut self) { + self.inner.lock().unwrap().disable_heuristics(); + } + + pub fn disable_chromosome_heuristics(&mut self) { + self.inner.lock().unwrap().disable_chromosome_heuristics(); + } + + pub fn disable_chromosome_hashing(&mut self) { + self.inner.lock().unwrap().disable_chromosome_hashing(); + } + + pub fn disable_block_hashing(&mut self) { + self.inner.lock().unwrap().disable_block_hashing(); + } + + pub fn disable_function_hashing(&mut self) { + self.inner.lock().unwrap().disable_function_hashing(); + } + + pub fn disable_function_heuristics(&mut self) { + self.inner.lock().unwrap().disable_function_heuristics(); + } + + pub fn disable_block_heuristics(&mut self) { + self.inner.lock().unwrap().disable_block_heuristics(); + } +} + +#[pyclass] +pub struct ConfigDisassembler { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigDisassembler { + #[getter] + pub fn get_sweep(&self) -> ConfigDisassemblerSweep { + ConfigDisassemblerSweep { + inner: Arc::clone(&self.inner) + } + } +} + +#[pyclass] +pub struct ConfigDisassemblerSweep { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigDisassemblerSweep { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.disassembler.sweep.enabled + } + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.disassembler.sweep.enabled = value; + } +} + + +#[pyclass] +pub struct ConfigMmap { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigMmap { + #[getter] + pub fn get_directory(&self) -> String { + let inner = self.inner.lock().unwrap(); + inner.mmap.directory.clone() + } + + #[setter] + pub fn set_directory(&mut self, value: String) { + let mut inner = self.inner.lock().unwrap(); + inner.mmap.directory = value; + } + + #[getter] + pub fn get_cache(&self) -> ConfigMmapCache { + ConfigMmapCache { + inner: Arc::clone(&self.inner) + } + } +} + +#[pyclass] +pub struct ConfigMmapCache { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigMmapCache { + #[getter] + pub fn get_enabled(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.mmap.cache.enabled + } + #[setter] + pub fn set_enabled(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.mmap.cache.enabled = value; + } +} + +#[pyclass] +pub struct ConfigGeneral { + pub inner: Arc>, +} + +#[pymethods] +impl ConfigGeneral { + #[getter] + pub fn get_threads(&self) -> usize { + let inner = self.inner.lock().unwrap(); + inner.general.threads + } + + #[setter] + pub fn set_threads(&mut self, value: usize) { + let mut inner = self.inner.lock().unwrap(); + inner.general.threads = value; + } + + #[getter] + pub fn get_minimal(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.general.minimal + } + + #[setter] + pub fn set_minimal(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.general.minimal = value; + } + + #[getter] + pub fn get_debug(&self) -> bool { + let inner = self.inner.lock().unwrap(); + inner.general.debug + } + + #[setter] + pub fn set_debug(&mut self, value: bool) { + let mut inner = self.inner.lock().unwrap(); + inner.general.debug = value; + } + +} + +#[pymodule] +#[pyo3(name = "config")] +pub fn config_init(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.config", m)?; + m.setattr("__name__", "binlex.config")?; + Ok(()) +} diff --git a/src/bindings/python/src/controlflow/block.rs b/src/bindings/python/src/controlflow/block.rs new file mode 100644 index 00000000..0062b6c9 --- /dev/null +++ b/src/bindings/python/src/controlflow/block.rs @@ -0,0 +1,185 @@ +use pyo3::prelude::*; +use pyo3::Py; +use std::collections::{BTreeMap, BTreeSet}; +use binlex::controlflow::Block as InnerBlock; +use crate::controlflow::graph::Graph; +use std::sync::Arc; +use std::sync::Mutex; +use pyo3::types::PyBytes; + +/// A class representing a control flow block in the binary analysis. +#[pyclass] +pub struct Block { + /// The starting address of the block. + pub address: u64, + /// A reference to the control flow graph associated with the block. + pub cfg: Py, + inner_block_cache: Arc>>>, +} + +impl Block { + fn with_inner_block(&self, py: Python, f: F) -> PyResult + where + F: FnOnce(&InnerBlock<'static>) -> PyResult, + { + let mut cache = self.inner_block_cache.lock().unwrap(); + + if cache.is_none() { + let binding = self.cfg.borrow(py); + let inner = binding.inner.lock().unwrap(); + + let inner_ref: &'static _ = unsafe { std::mem::transmute(&*inner) }; + let inner_block = InnerBlock::new(self.address, inner_ref)?; + *cache = Some(inner_block); + } + + f(cache.as_ref().unwrap()) + } +} + +#[pymethods] +impl Block { + #[new] + #[pyo3(text_signature = "(address, cfg)")] + /// Creates a new `Block` instance. + /// + /// # Arguments + /// - `address`: The starting address of the block. + /// - `cfg`: The control flow graph associated with the block. + /// + /// # Returns + /// A new `Block` object. + fn new(address: u64, cfg: Py) -> PyResult { + Ok(Self { + address, + cfg, + inner_block_cache: Arc::new(Mutex::new(None)), + }) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the raw bytes of the block. + fn bytes(&self, py: Python) -> PyResult> { + self.with_inner_block(py, |block| { + let bytes = PyBytes::new_bound(py, &block.bytes()); + Ok(bytes.into()) + }) + } + + #[pyo3(text_signature = "($self)")] + /// Checks if the block is a prologue block. + pub fn is_prologue(&self, py: Python) -> PyResult { + self.with_inner_block(py, |block| Ok(block.is_prologue())) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the number of edges from the block. + pub fn edges(&self, py: Python) -> PyResult { + self.with_inner_block(py, |block| Ok(block.edges())) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the next address in the block. + pub fn next(&self, py: Python) -> PyResult> { + self.with_inner_block(py, |block| Ok(block.next())) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the set of addresses the block points to. + pub fn to(&self, py: Python) -> PyResult> { + self.with_inner_block(py, |block| Ok(block.to())) + } + + #[pyo3(text_signature = "($self)")] + /// Calculates the entropy of the block. + pub fn entropy(&self, py: Python) -> PyResult> { + self.with_inner_block(py, |block| Ok(block.entropy())) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the set of addresses of blocks referenced by this block. + pub fn blocks(&self, py: Python) -> PyResult> { + self.with_inner_block(py, |block| Ok(block.blocks())) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the number of instructions in the block. + pub fn number_of_instructions(&self, py: Python) -> PyResult { + self.with_inner_block(py, |block| Ok(block.number_of_instructions())) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the functions referenced in the block as a map. + pub fn functions(&self, py: Python) -> PyResult> { + self.with_inner_block(py, |block| Ok(block.functions())) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the TLSH (Trend Micro Locality Sensitive Hash) of the block. + pub fn tlsh(&self, py: Python) -> PyResult> { + self.with_inner_block(py, |block| Ok(block.tlsh())) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the SHA-256 hash of the block. + pub fn sha256(&self, py: Python) -> PyResult> { + self.with_inner_block(py, |block| Ok(block.sha256())) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the MinHash of the block. + pub fn minhash(&self, py: Python) -> PyResult> { + self.with_inner_block(py, |block| Ok(block.minhash())) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the ending address of the block. + pub fn end(&self, py: Python) -> PyResult { + self.with_inner_block(py, |block| Ok(block.end())) + } + + #[pyo3(text_signature = "($self)")] + /// Retrieves the size of the block in bytes. + pub fn size(&self, py: Python) -> PyResult { + self.with_inner_block(py, |block| Ok(block.size())) + } + + #[pyo3(text_signature = "($self)")] + /// Prints a human-readable representation of the block. + pub fn print(&self, py: Python) -> PyResult<()> { + self.with_inner_block(py, |block| Ok(block.print())) + } + + #[pyo3(text_signature = "($self)")] + /// Converts the block to a Python dictionary. + pub fn to_dict(&self, py: Python) -> PyResult> { + let json_str = self.json(py)?; + let json_module = py.import_bound("json")?; + let py_dict = json_module.call_method1("loads", (json_str,))?; + Ok(py_dict.into()) + } + + #[pyo3(text_signature = "($self)")] + /// Converts the block to a JSON string. + pub fn json(&self, py: Python) -> PyResult { + self.with_inner_block(py, |block| { + block.json().map_err(|e| PyErr::new::(e.to_string())) + }) + } + + /// Converts the block to a JSON string when printed. + pub fn __str__(&self, py: Python) -> PyResult { + self.json(py) + } +} + +#[pymodule] +#[pyo3(name = "block")] +pub fn block_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.controlflow.block", m)?; + m.setattr("__name__", "binlex.controlflow.block")?; + Ok(()) +} diff --git a/src/bindings/python/src/controlflow/function.rs b/src/bindings/python/src/controlflow/function.rs new file mode 100644 index 00000000..95e796aa --- /dev/null +++ b/src/bindings/python/src/controlflow/function.rs @@ -0,0 +1,233 @@ +use pyo3::prelude::*; + +use pyo3::Py; +use std::collections::BTreeSet; +use std::collections::BTreeMap; +use binlex::controlflow::Function as InnerFunction; +use crate::controlflow::Graph; +use std::sync::Arc; +use std::sync::Mutex; +use pyo3::types::PyBytes; + +#[pyclass] +/// Represents a function within a control flow graph (CFG). +pub struct Function { + /// The address of the function. + pub address: u64, + /// The control flow graph associated with the function. + pub cfg: Py, + inner_function_cache: Arc>>>, +} + +impl Function { + fn with_inner_function(&self, py: Python, f: F) -> PyResult + where + F: FnOnce(&InnerFunction<'static>) -> PyResult, + { + let mut cache = self.inner_function_cache.lock().unwrap(); + + if cache.is_none() { + let binding = self.cfg.borrow(py); + let inner = binding.inner.lock().unwrap(); + + let inner_ref: &'static _ = unsafe { std::mem::transmute(&*inner) }; + let inner_block = InnerFunction::new(self.address, inner_ref)?; + *cache = Some(inner_block); + } + + f(cache.as_ref().unwrap()) + } +} + +#[pymethods] +impl Function { + #[new] + #[pyo3(text_signature = "(address, cfg)")] + /// Creates a new `Function` instance. + /// + /// # Arguments + /// - `address` (`u64`): The address of the function. + /// - `cfg` (`Graph`): The control flow graph associated with the function. + /// + /// # Returns + /// - A new instance of `Function`. + fn new(address: u64, cfg: Py) -> PyResult { + Ok(Self { + address, + cfg, + inner_function_cache: Arc::new(Mutex::new(None)), + }) + } + + #[pyo3(text_signature = "($self)")] + /// Returns the raw bytes of the function. + /// + /// # Returns + /// - `bytes | None`: The raw bytes of the function, if available + fn bytes(&self, py: Python) -> PyResult>> { + self.with_inner_function(py, |function| { + if let Some(raw_bytes) = function.bytes() { + let bytes = PyBytes::new_bound(py, &raw_bytes); + Ok(Some(bytes.into())) + } else { + Ok(None) + } + }) + } + + #[pyo3(text_signature = "($self)")] + /// Determines if the function starts with a prologue. + /// + /// # Returns + /// - `bool`: `true` if the function starts with a prologue; otherwise, `false`. + pub fn is_prologue(&self, py: Python) -> PyResult { + self.with_inner_function(py, |function| Ok(function.is_prologue())) + } + + #[pyo3(text_signature = "($self)")] + /// Returns the number of edges in the control flow graph. + /// + /// # Returns + /// - `usize`: The number of edges. + pub fn edges(&self, py: Python) -> PyResult { + self.with_inner_function(py, |function| Ok(function.edges())) + } + + #[pyo3(text_signature = "($self)")] + /// Returns the entropy of the function. + /// + /// # Returns + /// - `Option`: The entropy value, if available. + pub fn entropy(&self, py: Python) -> PyResult> { + self.with_inner_function(py, |function| Ok(function.entropy())) + } + + #[pyo3(text_signature = "($self)")] + /// Returns a set of all block addresses in the function. + /// + /// # Returns + /// - `BTreeSet`: A set of block addresses. + pub fn blocks(&self, py: Python) -> PyResult> { + self.with_inner_function(py, |function| Ok(function.blocks())) + } + + #[pyo3(text_signature = "($self)")] + /// Returns the number of instructions in the function. + /// + /// # Returns + /// - `usize`: The number of instructions. + pub fn number_of_instructions(&self, py: Python) -> PyResult { + self.with_inner_function(py, |function| Ok(function.number_of_instructions())) + } + + #[pyo3(text_signature = "($self)")] + /// Returns a mapping of function calls within the current function. + /// + /// # Returns + /// - `BTreeMap`: A map of called functions' addresses and counts. + pub fn functions(&self, py: Python) -> PyResult> { + self.with_inner_function(py, |function| Ok(function.functions())) + } + + #[pyo3(text_signature = "($self)")] + /// Returns the TLSH (Trend Micro Locality Sensitive Hash) of the function. + /// + /// # Returns + /// - `Option`: The TLSH hash, if available. + pub fn tlsh(&self, py: Python) -> PyResult> { + self.with_inner_function(py, |function| Ok(function.tlsh())) + } + + #[pyo3(text_signature = "($self)")] + /// Returns the SHA-256 hash of the function. + /// + /// # Returns + /// - `Option`: The SHA-256 hash, if available. + pub fn sha256(&self, py: Python) -> PyResult> { + self.with_inner_function(py, |function| Ok(function.sha256())) + } + + #[pyo3(text_signature = "($self)")] + /// Returns the MinHash of the function. + /// + /// # Returns + /// - `Option`: The MinHash, if available. + pub fn minhash(&self, py: Python) -> PyResult> { + self.with_inner_function(py, |function| Ok(function.minhash())) + } + + #[pyo3(text_signature = "($self)")] + /// Returns the size of the function in bytes. + /// + /// # Returns + /// - `usize`: The size of the function in bytes. + pub fn size(&self, py: Python) -> PyResult { + self.with_inner_function(py, |function| Ok(function.size())) + } + + #[pyo3(text_signature = "($self)")] + /// Determines if the function's memory layout is contiguous. + /// + /// # Returns + /// - `bool`: `True` if contiguous; otherwise, `False`. + pub fn is_contiguous(&self, py: Python) -> PyResult { + self.with_inner_function(py, |function| Ok(function.is_contiguous())) + } + + #[pyo3(text_signature = "($self)")] + /// Returns the ending address of the function. + /// + /// # Returns + /// - `int | None`: The ending address, if available. + pub fn end(&self, py: Python) -> PyResult> { + self.with_inner_function(py, |function| Ok(function.end())) + } + + #[pyo3(text_signature = "($self)")] + /// Prints a textual representation of the function in JSON. + /// + /// # Returns + /// - `()` (unit): Output is sent to stdout. + pub fn print(&self, py: Python) -> PyResult<()> { + self.with_inner_function(py, |function| Ok(function.print())) + } + + #[pyo3(text_signature = "($self)")] + /// Converts the function to a JSON dictionary representation. + /// + /// # Returns + /// - `dict`: A Python dictionary representation of the function. + pub fn to_dict(&self, py: Python) -> PyResult> { + let json_str = self.json(py)?; + let json_module = py.import_bound("json")?; + let py_dict = json_module.call_method1("loads", (json_str,))?; + Ok(py_dict.into()) + } + + #[pyo3(text_signature = "($self)")] + /// Converts the function to JSON representation. + /// + /// # Returns + /// - `str`: JSON string representing the function. + pub fn json(&self, py: Python) -> PyResult { + self.with_inner_function(py, |block| { + block.json().map_err(|e| PyErr::new::(e.to_string())) + }) + } + + /// When printed directly print the JSON representation of the function. + pub fn __str__(&self, py: Python) -> PyResult { + self.json(py) + } +} + +#[pymodule] +#[pyo3(name = "function")] +pub fn function_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.controlflow.function", m)?; + m.setattr("__name__", "binlex.controlflow.function")?; + Ok(()) +} diff --git a/src/bindings/python/src/controlflow/graph.rs b/src/bindings/python/src/controlflow/graph.rs new file mode 100644 index 00000000..84b8bc75 --- /dev/null +++ b/src/bindings/python/src/controlflow/graph.rs @@ -0,0 +1,200 @@ +use pyo3::prelude::*; +use std::collections::BTreeSet; +use binlex::controlflow::GraphQueue as InnerGraphQueue; +use binlex::controlflow::Graph as InnerGraph; +use crate::Architecture; +use crate::config::Config; +use std::sync::Arc; +use std::sync::Mutex; + +#[pyclass] +pub struct GraphQueue { + inner_graph: Arc>, + kind: QueueKind, +} + +#[derive(Clone, Copy)] +enum QueueKind { + Instructions, + Blocks, + Functions, +} + +impl GraphQueue { + fn get_queue<'a>(&self, inner: &'a InnerGraph) -> &'a InnerGraphQueue { + match self.kind { + QueueKind::Instructions => &inner.instructions, + QueueKind::Blocks => &inner.blocks, + QueueKind::Functions => &inner.functions, + } + } + + fn get_queue_mut<'a>(&self, inner: &'a mut InnerGraph) -> &'a mut InnerGraphQueue { + match self.kind { + QueueKind::Instructions => &mut inner.instructions, + QueueKind::Blocks => &mut inner.blocks, + QueueKind::Functions => &mut inner.functions, + } + } +} + +#[pymethods] +impl GraphQueue { + #[pyo3(text_signature = "($self, address)")] + pub fn insert_invalid(&self, address: u64) { + let mut inner = self.inner_graph.lock().unwrap(); + self.get_queue_mut(&mut inner).insert_invalid(address); + } + + #[pyo3(text_signature = "($self, address)")] + pub fn is_invalid(&self, address: u64) -> bool { + let inner = self.inner_graph.lock().unwrap(); + self.get_queue(&inner).is_invalid(address) + } + + #[pyo3(text_signature = "($self)")] + pub fn valid_addresses(&self) -> BTreeSet { + let inner = self.inner_graph.lock().unwrap(); + self.get_queue(&inner).valid_addresses() + } + + #[pyo3(text_signature = "($self)")] + pub fn invalid_addresses(&self) -> BTreeSet { + let inner = self.inner_graph.lock().unwrap(); + self.get_queue(&inner).invalid_addresses() + } + + #[pyo3(text_signature = "($self)")] + pub fn processed_addresses(&self) -> BTreeSet { + let inner = self.inner_graph.lock().unwrap(); + self.get_queue(&inner).processed_addresses() + } + + #[pyo3(text_signature = "($self, address)")] + pub fn is_valid(&self, address: u64) -> bool { + let inner = self.inner_graph.lock().unwrap(); + self.get_queue(&inner).is_valid(address) + } + + #[pyo3(text_signature = "($self, address)")] + pub fn insert_valid(&self, address: u64) { + let mut inner = self.inner_graph.lock().unwrap(); + self.get_queue_mut(&mut inner).insert_valid(address); + } + + #[pyo3(text_signature = "($self, addresses)")] + pub fn insert_processed_extend(&self, addresses: BTreeSet) { + let mut inner = self.inner_graph.lock().unwrap(); + self.get_queue_mut(&mut inner).insert_processed_extend(addresses); + } + + #[pyo3(text_signature = "($self, address)")] + pub fn insert_processed(&self, address: u64) { + let mut inner = self.inner_graph.lock().unwrap(); + self.get_queue_mut(&mut inner).insert_processed(address); + } + + #[pyo3(text_signature = "($self, address)")] + pub fn is_processed(&self, address: u64) -> bool { + let inner = self.inner_graph.lock().unwrap(); + self.get_queue(&inner).is_processed(address) + } + + #[pyo3(text_signature = "($self, addresses)")] + pub fn enqueue_extend(&self, addresses: BTreeSet) { + let mut inner = self.inner_graph.lock().unwrap(); + self.get_queue_mut(&mut inner).enqueue_extend(addresses); + } + + #[pyo3(text_signature = "($self, address)")] + pub fn enqueue(&self, address: u64) -> bool { + let mut inner = self.inner_graph.lock().unwrap(); + self.get_queue_mut(&mut inner).enqueue(address) + } + + #[pyo3(text_signature = "($self)")] + pub fn dequeue(&self) -> Option { + let mut inner = self.inner_graph.lock().unwrap(); + self.get_queue_mut(&mut inner).dequeue() + } + + #[pyo3(text_signature = "($self)")] + pub fn dequeue_all(&self) -> BTreeSet { + let mut inner = self.inner_graph.lock().unwrap(); + self.get_queue_mut(&mut inner).dequeue_all() + } +} + +#[pyclass] +pub struct Graph { + pub inner: Arc>, +} + +#[pymethods] +impl Graph { + #[new] + #[pyo3(text_signature = "(architecture, config)")] + pub fn new(py: Python, architecture: Py, config: Py) -> Self { + let inner_config = config.borrow(py).inner.lock().unwrap().clone(); + let inner = InnerGraph::new(architecture.borrow(py).inner, inner_config); + Self { + inner: Arc::new(Mutex::new(inner)), + } + } + + #[getter] + pub fn get_instructions(&self, py: Python) -> Py { + Py::new( + py, + GraphQueue { + inner_graph: Arc::clone(&self.inner), + kind: QueueKind::Instructions, + }, + ) + .expect("failed to get instructions graph queue") + } + + #[getter] + pub fn get_blocks(&self, py: Python) -> Py { + Py::new( + py, + GraphQueue { + inner_graph: Arc::clone(&self.inner), + kind: QueueKind::Blocks, + }, + ) + .expect("failed to get blocks graph queue") + } + + #[getter] + pub fn get_functions(&self, py: Python) -> Py { + Py::new( + py, + GraphQueue { + inner_graph: Arc::clone(&self.inner), + kind: QueueKind::Functions, + }, + ) + .expect("failed to get functions graph queue") + } + + #[pyo3(text_signature = "($self, cfg)")] + pub fn absorb(&mut self, py: Python, cfg: Py) { + self.inner + .lock() + .unwrap() + .absorb(&mut cfg.borrow_mut(py).inner.lock().unwrap()); + } +} + +#[pymodule] +#[pyo3(name = "graph")] +pub fn graph_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.controlflow.graph", m)?; + m.setattr("__name__", "binlex.controlflow.graph")?; + Ok(()) +} diff --git a/src/bindings/python/src/controlflow/instruction.rs b/src/bindings/python/src/controlflow/instruction.rs new file mode 100644 index 00000000..861bf441 --- /dev/null +++ b/src/bindings/python/src/controlflow/instruction.rs @@ -0,0 +1,122 @@ +use pyo3::prelude::*; +use std::collections::BTreeSet; +use binlex::controlflow::Instruction as InnerInstruction; +use crate::controlflow::Graph; +use std::sync::Mutex; +use std::sync::Arc; + +#[pyclass] +pub struct Instruction { + pub address: u64, + pub cfg: Py, + inner: Arc>>, +} + +impl Instruction { + fn with_inner_instruction(&self, py: Python, f: F) -> PyResult + where + F: FnOnce(&InnerInstruction) -> PyResult, + { + let mut cache = self.inner.lock().unwrap(); + + if cache.is_none() { + let binding = self.cfg.borrow(py); + let inner = binding.inner.lock().unwrap(); + + #[allow(mutable_transmutes)] + let inner_ref: _ = unsafe { std::mem::transmute(&*inner) }; + let inner_instruction = InnerInstruction::new(self.address, inner_ref); + if inner_instruction.is_err() { + return Err(pyo3::exceptions::PyRuntimeError::new_err( + "instruction does not exist", + )); + } + *cache = Some(inner_instruction.unwrap()); + } + + f(cache.as_ref().unwrap()) + } +} + +#[pymethods] +impl Instruction { + #[new] + #[pyo3(text_signature = "(address, cfg)")] + fn new(address: u64, cfg: Py) -> PyResult { + Ok(Self { + address, + cfg, + inner: Arc::new(Mutex::new(None)), + }) + } + + #[pyo3(text_signature = "($self)")] + pub fn blocks(&self, py: Python) -> PyResult> { + self.with_inner_instruction(py, |instruction| { + Ok(instruction.blocks()) + }) + } + + #[pyo3(text_signature = "($self)")] + pub fn next(&self, py: Python) -> PyResult> { + self.with_inner_instruction(py, |instruction| { + Ok(instruction.next()) + }) + } + + #[pyo3(text_signature = "($self)")] + pub fn to(&self, py: Python) -> PyResult> { + self.with_inner_instruction(py, |instruction| { + Ok(instruction.to()) + }) + } + + #[pyo3(text_signature = "($self)")] + pub fn functions(&self, py: Python) -> PyResult> { + self.with_inner_instruction(py, |instruction| { + Ok(instruction.functions()) + }) + } + + #[pyo3(text_signature = "($self)")] + pub fn size(&self, py: Python) -> PyResult { + self.with_inner_instruction(py, |instruction| { + Ok(instruction.size()) + }) + } + + #[pyo3(text_signature = "($self)")] + pub fn to_dict(&self, py: Python) -> PyResult> { + let json_str = self.json(py)?; + let json_module = py.import_bound("json")?; + let py_dict = json_module.call_method1("loads", (json_str,))?; + Ok(py_dict.into()) + } + + #[pyo3(text_signature = "($self)")] + pub fn json(&self, py: Python) -> PyResult { + self.with_inner_instruction(py, |instruction| { + instruction.json().map_err(|e| PyErr::new::(e.to_string())) + }) + } + + pub fn __str__(&self, py: Python) -> PyResult { + self.json(py) + } + + #[pyo3(text_signature = "($self)")] + pub fn print(&self, py: Python) -> PyResult<()> { + self.with_inner_instruction(py, |instruction| Ok(instruction.print())) + } +} + +#[pymodule] +#[pyo3(name = "instruction")] +pub fn instruction_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.controlflow.instruction", m)?; + m.setattr("__name__", "binlex.controlflow.instruction")?; + Ok(()) +} diff --git a/src/bindings/python/src/controlflow/mod.rs b/src/bindings/python/src/controlflow/mod.rs new file mode 100644 index 00000000..163ccf97 --- /dev/null +++ b/src/bindings/python/src/controlflow/mod.rs @@ -0,0 +1,36 @@ +pub mod graph; +pub mod instruction; +pub mod block; +pub mod function; + +pub use crate::controlflow::graph::Graph; +pub use crate::controlflow::graph::GraphQueue; +pub use crate::controlflow::block::Block; +pub use crate::controlflow::function::Function; +pub use crate::controlflow::instruction::Instruction; + +use crate::controlflow::graph::graph_init; +use crate::controlflow::instruction::instruction_init; +use crate::controlflow::block::block_init; +use crate::controlflow::function::function_init; + +use pyo3::{prelude::*, wrap_pymodule}; + +#[pymodule] +#[pyo3(name = "controlflow")] +pub fn controlflow_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_wrapped(wrap_pymodule!(graph_init))?; + m.add_wrapped(wrap_pymodule!(instruction_init))?; + m.add_wrapped(wrap_pymodule!(block_init))?; + m.add_wrapped(wrap_pymodule!(function_init))?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.controlflow", m)?; + m.setattr("__name__", "binlex.controlflow")?; + Ok(()) +} diff --git a/src/bindings/python/src/disassemblers/capstone/disassembler.rs b/src/bindings/python/src/disassemblers/capstone/disassembler.rs new file mode 100644 index 00000000..e5947919 --- /dev/null +++ b/src/bindings/python/src/disassemblers/capstone/disassembler.rs @@ -0,0 +1,130 @@ +use pyo3::prelude::*; +use pyo3::Py; +use std::borrow::Borrow; +use std::io::Error; +use std::collections::BTreeSet; +use std::collections::BTreeMap; +use binlex::disassemblers::capstone::Disassembler as InnerDisassembler; +use crate::Architecture; +use crate::controlflow::Graph; +use pyo3::types::PyBytes; +use pyo3::types::PyAny; +use pyo3::types::PyMemoryView; +use pyo3::exceptions::PyTypeError; +use pyo3::buffer::PyBuffer; + +#[pyclass(unsendable)] +pub struct Disassembler{ + image: Py, + machine: Py, + executable_address_ranges: BTreeMap, +} + +#[pymethods] +impl Disassembler { + #[new] + #[pyo3(text_signature = "(machine, image, executable_address_ranges)")] + pub fn new(machine: Py, image: Py, executable_address_ranges: BTreeMap) -> Self { + Self { + machine: machine, + image: image, + executable_address_ranges: executable_address_ranges, + } + } + + fn get_image_data<'py>(&'py self, py: Python<'py>) -> PyResult<&'py [u8]> { + let image_ref = self.image.borrow(); + + if let Ok(bytes) = image_ref.downcast_bound::(py) { + return Ok(bytes.as_bytes()); + } + + if let Ok(memory_view) = image_ref.downcast_bound::(py) { + + let buffer = PyBuffer::::get_bound(memory_view)?; + + if !buffer.is_c_contiguous() { + return Err(PyTypeError::new_err( + "the memoryview is not c-contiguous", + )); + } + + let slice = buffer.as_slice(py).unwrap(); + + let result: &[u8] = unsafe { + std::slice::from_raw_parts(slice.as_ptr() as *const u8, slice.len()) + }; + + return Ok(result); + + } + + Err(PyTypeError::new_err("expected a bytes or memoryview object for the 'image' argument")) + } + + #[pyo3(text_signature = "($self, address, cfg)")] + pub fn disassemble_instruction(&self, py: Python, address: u64, cfg: Py) -> Result { + let image = self.get_image_data(py)?; + let machine_binding = &self.machine.borrow(py); + let disassembler = InnerDisassembler::new(machine_binding.inner, image, self.executable_address_ranges.clone())?; + let cfg_ref= &mut cfg.borrow_mut(py); + let result = disassembler.disassemble_instruction(address, &mut cfg_ref.inner.lock().unwrap())?; + return Ok(result); + } + + #[pyo3(text_signature = "($self, address, cfg)")] + pub fn disassemble_function(&self, py: Python, address: u64, cfg: Py) -> Result { + let image = self.get_image_data(py)?; + let machine_binding = &self.machine.borrow(py); + let disassembler = InnerDisassembler::new(machine_binding.inner, image, self.executable_address_ranges.clone())?; + let cfg_ref= &mut cfg.borrow_mut(py); + let result = disassembler.disassemble_function(address, &mut cfg_ref.inner.lock().unwrap())?; + return Ok(result); + } + + #[pyo3(text_signature = "($self, address, cfg)")] + pub fn disassemble_block(&self, py: Python, address: u64, cfg: Py) -> Result { + let image = self.get_image_data(py)?; + let machine_binding = &self.machine.borrow(py); + let disassembler = InnerDisassembler::new(machine_binding.inner, image, self.executable_address_ranges.clone())?; + let cfg_ref= &mut cfg.borrow_mut(py); + let result = disassembler.disassemble_block(address, &mut cfg_ref.inner.lock().unwrap())?; + return Ok(result); + } + + #[pyo3(text_signature = "($self, addresses, cfg)")] + pub fn disassemble_controlflow(&self, py: Python, addresses: BTreeSet, cfg: Py) -> Result<(), Error> { + let image = self.get_image_data(py)?; + let machine_binding = &self.machine.borrow(py); + let disassembler = InnerDisassembler::new(machine_binding.inner, image, self.executable_address_ranges.clone())?; + let cfg_ref= &mut cfg.borrow_mut(py); + disassembler.disassemble_controlflow(addresses, &mut cfg_ref.inner.lock().unwrap())?; + Ok(()) + } + + #[pyo3(text_signature = "($self)")] + pub fn disassemble_sweep(&self, py: Python) -> Result, Error> { + let image = self.get_image_data(py)?; + let machine_binding = &self.machine.borrow(py); + let disassembler = InnerDisassembler::new(machine_binding.inner, image, self.executable_address_ranges.clone())?; + let results = disassembler.disassemble_sweep(); + let mut asdf = BTreeSet::::new(); + for result in results { + asdf.insert(result); + } + Ok(asdf) + } + +} + + +#[pymodule] +#[pyo3(name = "disassembler")] +pub fn disassembler_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.disassemblers.capstone", m)?; + m.setattr("__name__", "binlex.disassemblers.capstone")?; + Ok(()) +} diff --git a/src/bindings/python/src/disassemblers/capstone/mod.rs b/src/bindings/python/src/disassemblers/capstone/mod.rs new file mode 100644 index 00000000..68646a3c --- /dev/null +++ b/src/bindings/python/src/disassemblers/capstone/mod.rs @@ -0,0 +1,18 @@ +pub mod disassembler; + +use pyo3::{prelude::*, wrap_pymodule}; + +use crate::disassemblers::capstone::disassembler::disassembler_init; +use crate::disassemblers::capstone::disassembler::Disassembler; + +#[pymodule] +#[pyo3(name = "capstone")] +pub fn capstone_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_wrapped(wrap_pymodule!(disassembler_init))?; + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.disassemblers.capstone", m)?; + m.setattr("__name__", "binlex.disassemblers.capstone")?; + Ok(()) +} diff --git a/src/bindings/python/src/disassemblers/custom/cil/disassembler.rs b/src/bindings/python/src/disassemblers/custom/cil/disassembler.rs new file mode 100644 index 00000000..6bf4fc68 --- /dev/null +++ b/src/bindings/python/src/disassemblers/custom/cil/disassembler.rs @@ -0,0 +1,117 @@ +use pyo3::prelude::*; +use pyo3::Py; +use std::borrow::Borrow; +use std::io::Error; +use std::collections::BTreeSet; +use std::collections::BTreeMap; +use binlex::disassemblers::custom::cil::Disassembler as InnerDisassembler; +use crate::Architecture; +use crate::controlflow::Graph; +use pyo3::types::PyBytes; +use pyo3::types::PyAny; +use pyo3::types::PyMemoryView; +use pyo3::exceptions::PyTypeError; +use pyo3::buffer::PyBuffer; + +#[pyclass(unsendable)] +pub struct Disassembler{ + image: Py, + machine: Py, + executable_address_ranges: BTreeMap, +} + +#[pymethods] +impl Disassembler { + #[new] + #[pyo3(text_signature = "(machine, image, executable_address_ranges)")] + pub fn new(machine: Py, image: Py, executable_address_ranges: BTreeMap) -> Self { + Self { + machine: machine, + image: image, + executable_address_ranges: executable_address_ranges, + } + } + + fn get_image_data<'py>(&'py self, py: Python<'py>) -> PyResult<&'py [u8]> { + let image_ref = self.image.borrow(); + + if let Ok(bytes) = image_ref.downcast_bound::(py) { + return Ok(bytes.as_bytes()); + } + + if let Ok(memory_view) = image_ref.downcast_bound::(py) { + + let buffer = PyBuffer::::get_bound(memory_view)?; + + if !buffer.is_c_contiguous() { + return Err(PyTypeError::new_err( + "the memoryview is not c-contiguous", + )); + } + + let slice = buffer.as_slice(py).unwrap(); + + let result: &[u8] = unsafe { + std::slice::from_raw_parts(slice.as_ptr() as *const u8, slice.len()) + }; + + return Ok(result); + + } + + Err(PyTypeError::new_err("expected a bytes or memoryview object for the 'image' argument")) + } + + #[pyo3(text_signature = "($self, address, cfg)")] + pub fn disassemble_instruction(&self, py: Python, address: u64, cfg: Py) -> Result { + let image = self.get_image_data(py)?; + let machine_binding = &self.machine.borrow(py); + let disassembler = InnerDisassembler::new(machine_binding.inner, image, self.executable_address_ranges.clone())?; + let cfg_ref= &mut cfg.borrow_mut(py); + let result = disassembler.disassemble_instruction(address, &mut cfg_ref.inner.lock().unwrap())?; + return Ok(result); + } + + #[pyo3(text_signature = "($self, address, cfg)")] + pub fn disassemble_function(&self, py: Python, address: u64, cfg: Py) -> Result { + let image = self.get_image_data(py)?; + let machine_binding = &self.machine.borrow(py); + let disassembler = InnerDisassembler::new(machine_binding.inner, image, self.executable_address_ranges.clone())?; + let cfg_ref= &mut cfg.borrow_mut(py); + let result = disassembler.disassemble_function(address, &mut cfg_ref.inner.lock().unwrap())?; + return Ok(result); + } + + #[pyo3(text_signature = "($self, address, cfg)")] + pub fn disassemble_block(&self, py: Python, address: u64, cfg: Py) -> Result { + let image = self.get_image_data(py)?; + let machine_binding = &self.machine.borrow(py); + let disassembler = InnerDisassembler::new(machine_binding.inner, image, self.executable_address_ranges.clone())?; + let cfg_ref= &mut cfg.borrow_mut(py); + let result = disassembler.disassemble_block(address, &mut cfg_ref.inner.lock().unwrap())?; + return Ok(result); + } + + #[pyo3(text_signature = "($self, addresses, cfg)")] + pub fn disassemble_controlflow(&self, py: Python, addresses: BTreeSet, cfg: Py) -> Result<(), Error> { + let image = self.get_image_data(py)?; + let machine_binding = &self.machine.borrow(py); + let disassembler = InnerDisassembler::new(machine_binding.inner, image, self.executable_address_ranges.clone())?; + let cfg_ref= &mut cfg.borrow_mut(py); + disassembler.disassemble_controlflow(addresses, &mut cfg_ref.inner.lock().unwrap())?; + Ok(()) + } + +} + + +#[pymodule] +#[pyo3(name = "binlex_cil_disassembler")] +pub fn binlex_cil_disassembler_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.disassemblers.custom.cil", m)?; + m.setattr("__name__", "binlex.disassemblers.custom.cil")?; + Ok(()) +} diff --git a/src/bindings/python/src/disassemblers/custom/cil/mod.rs b/src/bindings/python/src/disassemblers/custom/cil/mod.rs new file mode 100644 index 00000000..2a9333c8 --- /dev/null +++ b/src/bindings/python/src/disassemblers/custom/cil/mod.rs @@ -0,0 +1,18 @@ +pub mod disassembler; + +use pyo3::{prelude::*, wrap_pymodule}; + +use disassembler::binlex_cil_disassembler_init; +use disassembler::Disassembler; + +#[pymodule] +#[pyo3(name = "cil")] +pub fn cil_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_wrapped(wrap_pymodule!(binlex_cil_disassembler_init))?; + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.disassemblers.custom.cil", m)?; + m.setattr("__name__", "binlex.disassemblers.custom.cil")?; + Ok(()) +} diff --git a/src/bindings/python/src/disassemblers/custom/mod.rs b/src/bindings/python/src/disassemblers/custom/mod.rs new file mode 100644 index 00000000..d3949ba0 --- /dev/null +++ b/src/bindings/python/src/disassemblers/custom/mod.rs @@ -0,0 +1,16 @@ +pub mod cil; + +use cil::cil_init; + +use pyo3::{prelude::*, wrap_pymodule}; + +#[pymodule] +#[pyo3(name = "custom_disassemblers")] +pub fn custom_disassemblers_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_wrapped(wrap_pymodule!(cil_init))?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.disassemblers.custom", m)?; + m.setattr("__name__", "binlex.disassemblers.custom")?; + Ok(()) +} diff --git a/src/bindings/python/src/disassemblers/mod.rs b/src/bindings/python/src/disassemblers/mod.rs new file mode 100644 index 00000000..349b0e5e --- /dev/null +++ b/src/bindings/python/src/disassemblers/mod.rs @@ -0,0 +1,19 @@ +pub mod capstone; +pub mod custom; + +use crate::disassemblers::capstone::capstone_init; +use crate::disassemblers::custom::custom_disassemblers_init; + +use pyo3::{prelude::*, wrap_pymodule}; + +#[pymodule] +#[pyo3(name = "disassemblers")] +pub fn disassemblers_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_wrapped(wrap_pymodule!(custom_disassemblers_init))?; + m.add_wrapped(wrap_pymodule!(capstone_init))?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.disassemblers", m)?; + m.setattr("__name__", "binlex.disassemblers")?; + Ok(()) +} diff --git a/src/bindings/python/src/formats/elf.rs b/src/bindings/python/src/formats/elf.rs new file mode 100644 index 00000000..63aded14 --- /dev/null +++ b/src/bindings/python/src/formats/elf.rs @@ -0,0 +1,108 @@ +use pyo3::prelude::*; +use std::io::Error; +use std::collections::BTreeMap; +use std::collections::BTreeSet; +use binlex::formats::ELF as InnerELF; +use crate::Architecture; +use crate::types::memorymappedfile::MemoryMappedFile; +use pyo3::types::PyType; +use crate::Config; +use std::sync::Arc; +use std::sync::Mutex; + +#[pyclass(unsendable)] +pub struct ELF { + pub inner: Arc>, +} + +#[pymethods] +impl ELF { + #[new] + #[pyo3(text_signature = "(path, config)")] + pub fn new(py: Python, path: String, config: Py) -> Result { + let inner_config = config.borrow(py).inner.lock().unwrap().clone(); + let inner = InnerELF::new(path, inner_config)?; + Ok(Self{ + inner: Arc::new(Mutex::new(inner)), + }) + } + + #[classmethod] + #[pyo3(text_signature = "(bytes, config)")] + pub fn from_bytes(_: &Bound<'_, PyType>, py: Python, bytes: Vec, config: Py) -> PyResult { + let inner_config = config.borrow(py).inner.lock().unwrap().clone(); + let inner = InnerELF::from_bytes(bytes, inner_config)?; + Ok(Self { inner: Arc::new(Mutex::new(inner)) }) + } + + #[pyo3(text_signature = "($self)")] + pub fn architecture(&self) -> Architecture { + return Architecture::new(self.inner.lock().unwrap().architecture() as u16); + } + + #[pyo3(text_signature = "($self)")] + pub fn executable_virtual_address_ranges(&self) -> BTreeMap { + self.inner.lock().unwrap().executable_virtual_address_ranges() + } + + #[pyo3(text_signature = "($self, relative_virtual_address)")] + pub fn relative_virtual_address_to_virtual_address(&self, relative_virtual_address: u64) -> u64 { + self.inner.lock().unwrap().relative_virtual_address_to_virtual_address(relative_virtual_address) + } + + #[pyo3(text_signature = "($self, file_offset)")] + pub fn file_offset_to_virtual_address(&self, file_offset: u64) -> Option { + self.inner.lock().unwrap().file_offset_to_virtual_address(file_offset) + } + + #[pyo3(text_signature = "($self)")] + pub fn entrypoints(&self) -> BTreeSet { + self.inner.lock().unwrap().entrypoints() + } + + #[pyo3(text_signature = "($self)")] + pub fn entrypoint(&self) -> u64 { + self.inner.lock().unwrap().entrypoint() + } + + #[pyo3(text_signature = "($self)")] + pub fn image(&self, py: Python<'_>) -> PyResult> { + let result = self.inner.lock().unwrap().image().map_err(|e| { + pyo3::exceptions::PyIOError::new_err(e.to_string()) + })?; + let py_memory_mapped_file = Py::new(py, MemoryMappedFile { inner: result, mmap: None})?; + Ok(py_memory_mapped_file) + } + + #[pyo3(text_signature = "($self)")] + pub fn tlsh(&self) -> Option { + self.inner.lock().unwrap().tlsh() + } + + #[pyo3(text_signature = "($self)")] + pub fn sha256(&self) -> Option { + self.inner.lock().unwrap().sha256() + } + + #[pyo3(text_signature = "($self)")] + pub fn size(&self) -> u64 { + self.inner.lock().unwrap().size() + } + + #[pyo3(text_signature = "($self)")] + pub fn exports(&self) -> BTreeSet { + self.inner.lock().unwrap().exports() + } + +} + +#[pymodule] +#[pyo3(name = "elf")] +pub fn elf_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.formats.elf", m)?; + m.setattr("__name__", "binlex.formats.elf")?; + Ok(()) +} diff --git a/src/bindings/python/src/formats/file.rs b/src/bindings/python/src/formats/file.rs new file mode 100644 index 00000000..72f64d3e --- /dev/null +++ b/src/bindings/python/src/formats/file.rs @@ -0,0 +1,57 @@ +use pyo3::prelude::*; + +use std::io::Error; +use binlex::formats::file::File as InnerFile; +use crate::Config; + +#[pyclass] +pub struct File { + pub inner: InnerFile, + pub config: Py, +} + +#[pymethods] +impl File { + #[new] + #[pyo3(text_signature = "(path, config)")] + pub fn new(py: Python, path: String, config: Py) -> PyResult { + let inner_config = config.borrow(py).inner.lock().unwrap().clone(); + let inner = InnerFile::new(path, inner_config)?; + Ok(Self { + inner: inner, + config: config, + }) + } + + #[pyo3(text_signature = "($self)")] + pub fn tlsh(&self) -> Option { + self.inner.tlsh() + } + + #[pyo3(text_signature = "($self)")] + pub fn sha256(&self) -> Option { + self.inner.sha256() + } + + #[pyo3(text_signature = "($self)")] + pub fn size(&self) -> u64 { + self.inner.size() + } + + #[pyo3(text_signature = "($self)")] + pub fn read(&mut self) -> Result<(), Error> { + self.inner.read() + } + +} + +#[pymodule] +#[pyo3(name = "file")] +pub fn file_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.formats.file", m)?; + m.setattr("__name__", "binlex.formats.file")?; + Ok(()) +} diff --git a/src/bindings/python/src/formats/macho.rs b/src/bindings/python/src/formats/macho.rs new file mode 100644 index 00000000..2ac7e7d0 --- /dev/null +++ b/src/bindings/python/src/formats/macho.rs @@ -0,0 +1,109 @@ +use pyo3::prelude::*; +use std::io::Error; +use std::collections::BTreeMap; +use std::collections::BTreeSet; +use pyo3::types::PyType; +use binlex::formats::MACHO as InnerMACHO; +use crate::Architecture; +use crate::types::memorymappedfile::MemoryMappedFile; +use crate::Config; +use std::sync::Arc; +use std::sync::Mutex; + +#[pyclass(unsendable)] +pub struct MACHO { + pub inner: Arc>, +} + +#[pymethods] +impl MACHO { + #[new] + #[pyo3(text_signature = "(path, config)")] + pub fn new(py: Python, path: String, config: Py) -> Result { + let inner_config = config.borrow(py).inner.lock().unwrap().clone(); + let inner = InnerMACHO::new(path, inner_config)?; + Ok(Self{ + inner: Arc::new(Mutex::new(inner)), + }) + } + + #[classmethod] + #[pyo3(text_signature = "(bytes, config)")] + pub fn from_bytes(_: &Bound<'_, PyType>, py: Python, bytes: Vec, config: Py) -> PyResult { + let inner_config = config.borrow(py).inner.lock().unwrap().clone(); + let inner = InnerMACHO::from_bytes(bytes, inner_config)?; + Ok(Self { inner: Arc::new(Mutex::new(inner)) }) + } + + #[pyo3(text_signature = "($self, relative_virtual_address, slice)")] + pub fn relative_virtual_address_to_virtual_address(&self, relative_virtual_address: u64, slice: usize) -> Option { + self.inner.lock().unwrap().relative_virtual_address_to_virtual_address(relative_virtual_address, slice) + } + + #[pyo3(text_signature = "($self, file_offset, slice)")] + pub fn file_offset_to_virtual_address(&self, file_offset: u64, slice: usize) -> Option { + self.inner.lock().unwrap().file_offset_to_virtual_address(file_offset, slice) + } + + #[pyo3(text_signature = "($self)")] + pub fn number_of_slices(&self) -> usize { + self.inner.lock().unwrap().number_of_slices() + } + + #[pyo3(text_signature = "($self, slice)")] + pub fn entrypoint(&self, slice: usize) -> Option { + self.inner.lock().unwrap().entrypoint(slice) + } + + #[pyo3(text_signature = "($self, slice)")] + pub fn imagebase(&self, slice: usize) -> Option { + self.inner.lock().unwrap().imagebase(slice) + } + + #[pyo3(text_signature = "($self, slice)")] + pub fn sizeofheaders(&self, slice: usize) -> Option { + self.inner.lock().unwrap().sizeofheaders(slice) + } + + #[pyo3(text_signature = "($self, slice)")] + pub fn architecture(&self, slice: usize) -> Option { + let architecture = self.inner.lock().unwrap().architecture(slice); + if architecture.is_none() { return None; } + Some(Architecture{inner: architecture.unwrap()}) + } + + #[pyo3(text_signature = "($self, slice)")] + pub fn entrypoints(&self, slice: usize) -> BTreeSet { + self.inner.lock().unwrap().entrypoints(slice) + } + + #[pyo3(text_signature = "($self, slice)")] + pub fn exports(&self, slice: usize) -> BTreeSet { + self.inner.lock().unwrap().exports(slice) + } + + #[pyo3(text_signature = "($self, slice)")] + pub fn executable_virtual_address_ranges(&self, slice: usize) -> BTreeMap { + self.inner.lock().unwrap().executable_virtual_address_ranges(slice) + } + + #[pyo3(text_signature = "($self)")] + pub fn image(&self, py: Python<'_>, slice: usize) -> PyResult> { + let result = self.inner.lock().unwrap().image(slice).map_err(|e| { + pyo3::exceptions::PyIOError::new_err(e.to_string()) + })?; + let py_memory_mapped_file = Py::new(py, MemoryMappedFile { inner: result, mmap: None})?; + Ok(py_memory_mapped_file) + } +} + +#[pymodule] +#[pyo3(name = "macho")] +pub fn macho_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.formats.macho", m)?; + m.setattr("__name__", "binlex.formats.macho")?; + Ok(()) +} diff --git a/src/bindings/python/src/formats/mod.rs b/src/bindings/python/src/formats/mod.rs new file mode 100644 index 00000000..23a8e797 --- /dev/null +++ b/src/bindings/python/src/formats/mod.rs @@ -0,0 +1,32 @@ +pub mod file; +pub mod pe; +pub mod elf; +pub mod macho; + +use crate::formats::file::file_init; +use crate::formats::pe::pe_init; +use crate::formats::macho::macho_init; + +pub use crate::formats::pe::PE; +pub use crate::formats::file::File; +pub use crate::formats::elf::ELF; +pub use crate::formats::macho::MACHO; + +use pyo3::{prelude::*, wrap_pymodule}; + +#[pymodule] +#[pyo3(name = "formats")] +pub fn formats_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_wrapped(wrap_pymodule!(file_init))?; + m.add_wrapped(wrap_pymodule!(pe_init))?; + m.add_wrapped(wrap_pymodule!(macho_init))?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.formats", m)?; + m.setattr("__name__", "binlex.formats")?; + Ok(()) +} diff --git a/src/bindings/python/src/formats/pe.rs b/src/bindings/python/src/formats/pe.rs new file mode 100644 index 00000000..e1804b61 --- /dev/null +++ b/src/bindings/python/src/formats/pe.rs @@ -0,0 +1,169 @@ +use pyo3::prelude::*; +use std::io::Error; +use std::collections::BTreeMap; +use std::collections::BTreeSet; +use std::collections::HashMap; +use binlex::formats::pe::PE as InnerPe; +use crate::Architecture; +use crate::types::memorymappedfile::MemoryMappedFile; +use pyo3::types::PyType; +use crate::Config; +use std::sync::Arc; +use std::sync::Mutex; + +#[pyclass(unsendable)] +pub struct PE { + pub inner: Arc>, +} + +#[pymethods] +impl PE { + #[new] + #[pyo3(text_signature = "(path, config)")] + pub fn new(py: Python, path: String, config: Py) -> Result { + let inner_config = config.borrow(py).inner.lock().unwrap().clone(); + let inner = InnerPe::new(path, inner_config)?; + Ok(Self{ + inner: Arc::new(Mutex::new(inner)), + }) + } + + #[classmethod] + #[pyo3(text_signature = "(bytes, config)")] + pub fn from_bytes(_: &Bound<'_, PyType>, py: Python, bytes: Vec, config: Py) -> PyResult { + let inner_config = config.borrow(py).inner.lock().unwrap().clone(); + let inner = InnerPe::from_bytes(bytes, inner_config)?; + Ok(Self { inner: Arc::new(Mutex::new(inner)) }) + } + + #[pyo3(text_signature = "($self)")] + pub fn is_dotnet(&self) -> bool { + self.inner.lock().unwrap().is_dotnet() + } + + #[pyo3(text_signature = "($self, virtual_address)")] + pub fn virtual_address_to_relative_virtual_address(&self, virtual_address: u64) -> u64 { + self.inner.lock().unwrap().virtual_address_to_relative_virtual_address(virtual_address) + } + + #[pyo3(text_signature = "($self, virtual_address)")] + pub fn virtual_address_to_file_offset(&self, virtual_address: u64) -> Option { + self.inner.lock().unwrap().virtual_address_to_file_offset(virtual_address) + } + + #[pyo3(text_signature = "($self, relative_virtual_address)")] + pub fn relative_virtual_address_to_virtual_address(&self, relative_virtual_address: u64) -> u64 { + self.inner.lock().unwrap().relative_virtual_address_to_virtual_address(relative_virtual_address) + } + + #[pyo3(text_signature = "($self, offset)")] + pub fn file_offset_to_virtual_address(&self, file_offset: u64) -> Option { + self.inner.lock().unwrap().file_offset_to_virtual_address(file_offset) + } + + #[pyo3(text_signature = "($self)")] + pub fn architecture(&self) -> Architecture { + return Architecture::new(self.inner.lock().unwrap().architecture() as u16); + } + + #[pyo3(text_signature = "($self)")] + pub fn dotnet_executable_virtual_address_ranges(&self) -> BTreeMap { + self.inner.lock().unwrap().dotnet_executable_virtual_address_ranges() + } + + #[pyo3(text_signature = "($self)")] + pub fn executable_virtual_address_ranges(&self) -> BTreeMap { + self.inner.lock().unwrap().executable_virtual_address_ranges() + } + + #[pyo3(text_signature = "($self)")] + pub fn pogos(&self) -> HashMap { + self.inner.lock().unwrap().pogos() + } + + #[pyo3(text_signature = "($self)")] + pub fn tlscallbacks(&self) -> BTreeSet { + self.inner.lock().unwrap().tlscallbacks() + } + + #[pyo3(text_signature = "($self)")] + pub fn dotnet_entrypoints(&self) -> BTreeSet { + self.inner.lock().unwrap().dotnet_entrypoints() + } + + #[pyo3(text_signature = "($self)")] + pub fn entrypoints(&self) -> BTreeSet { + self.inner.lock().unwrap().entrypoints() + } + + #[pyo3(text_signature = "($self)")] + pub fn entrypoint(&self) -> u64 { + self.inner.lock().unwrap().entrypoint() + } + + #[pyo3(text_signature = "($self)")] + pub fn sizeofheaders(&self) -> u64 { + self.inner.lock().unwrap().sizeofheaders() + } + + #[pyo3(text_signature = "($self)")] + pub fn image(&self, py: Python<'_>) -> PyResult> { + let result = self.inner.lock().unwrap().image().map_err(|e| { + pyo3::exceptions::PyIOError::new_err(e.to_string()) + })?; + let py_memory_mapped_file = Py::new(py, MemoryMappedFile { inner: result, mmap: None})?; + Ok(py_memory_mapped_file) + } + + #[pyo3(text_signature = "($self)")] + pub fn size(&self) -> u64 { + self.inner.lock().unwrap().size() + } + + #[staticmethod] + pub fn align_section_virtual_address(value: u64, section_alignment: u64, file_alignment: u64) -> u64 { + InnerPe::align_section_virtual_address(value, section_alignment, file_alignment) + } + + #[pyo3(text_signature = "($self)")] + pub fn exports(&self) -> BTreeSet { + self.inner.lock().unwrap().exports() + } + + #[pyo3(text_signature = "($self)")] + pub fn tlsh(&self) -> Option { + self.inner.lock().unwrap().tlsh() + } + + #[pyo3(text_signature = "($self)")] + pub fn sha256(&self) -> Option { + self.inner.lock().unwrap().sha256() + } + + #[pyo3(text_signature = "($self)")] + pub fn imagebase(&self) -> u64 { + self.inner.lock().unwrap().imagebase() + } + + #[pyo3(text_signature = "($self)")] + pub fn section_alignment(&self) -> u64 { + self.inner.lock().unwrap().section_alignment() + } + + #[pyo3(text_signature = "($self)")] + pub fn file_alignment(&self) -> u64 { + self.inner.lock().unwrap().file_alignment() + } + +} + +#[pymodule] +#[pyo3(name = "pe")] +pub fn pe_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.formats.pe", m)?; + m.setattr("__name__", "binlex.formats.pe")?; + Ok(()) +} diff --git a/src/bindings/python/src/hashing/minhash.rs b/src/bindings/python/src/hashing/minhash.rs new file mode 100644 index 00000000..94c9046e --- /dev/null +++ b/src/bindings/python/src/hashing/minhash.rs @@ -0,0 +1,41 @@ +use pyo3::prelude::*; + +use binlex::hashing::minhash::MinHash32 as InnerMinHash32; + +#[pyclass] +pub struct MinHash32 { + num_hashes: usize, + shingle_size: usize, + seed: u64, + bytes: Vec, +} + +#[pymethods] +impl MinHash32 { + #[new] + #[pyo3(text_signature = "(bytes, num_hashes, shingle_size, seed)")] + pub fn new(bytes: Vec, num_hashes: usize, shingle_size: usize, seed: u64) -> Self { + Self { + bytes: bytes, + num_hashes: num_hashes, + shingle_size: shingle_size, + seed: seed, + } + } + #[pyo3(text_signature = "($self)")] + pub fn hexdigest(&self) -> Option { + InnerMinHash32::new(&self.bytes, self.num_hashes, self.shingle_size, self.seed).hexdigest() + } +} + + +#[pymodule] +#[pyo3(name = "minhash")] +pub fn minhash_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.hashing.minhash", m)?; + m.setattr("__name__", "binlex.hashing.minhash")?; + Ok(()) +} diff --git a/src/bindings/python/src/hashing/mod.rs b/src/bindings/python/src/hashing/mod.rs new file mode 100644 index 00000000..372e488b --- /dev/null +++ b/src/bindings/python/src/hashing/mod.rs @@ -0,0 +1,29 @@ +pub mod sha256; +pub mod tlsh; +pub mod minhash; + +use crate::hashing::sha256::sha256_init; +use crate::hashing::tlsh::tlsh_init; +use crate::hashing::minhash::minhash_init; + +pub use crate::hashing::minhash::MinHash32; +pub use crate::hashing::tlsh::TLSH; +pub use crate::hashing::sha256::SHA256; + +use pyo3::{prelude::*, wrap_pymodule}; + +#[pymodule] +#[pyo3(name = "hashing")] +pub fn hashing_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_wrapped(wrap_pymodule!(sha256_init))?; + m.add_wrapped(wrap_pymodule!(tlsh_init))?; + m.add_wrapped(wrap_pymodule!(minhash_init))?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.hashing", m)?; + m.setattr("__name__", "binlex.hashing")?; + Ok(()) +} diff --git a/src/bindings/python/src/hashing/sha256.rs b/src/bindings/python/src/hashing/sha256.rs new file mode 100644 index 00000000..6de6fec1 --- /dev/null +++ b/src/bindings/python/src/hashing/sha256.rs @@ -0,0 +1,36 @@ +use pyo3::prelude::*; + +use binlex::hashing::sha256::SHA256 as InnerSHA256; + +#[pyclass] +pub struct SHA256 { + pub bytes: Vec +} + +#[pymethods] +impl SHA256 { + #[new] + #[pyo3(text_signature = "(bytes)")] + pub fn new(bytes: Vec) -> Self { + Self { + bytes: bytes, + } + } + + #[pyo3(text_signature = "($self)")] + pub fn hexdigest(&self) -> Option { + InnerSHA256::new(&self.bytes).hexdigest() + } + +} + +#[pymodule] +#[pyo3(name = "sha256")] +pub fn sha256_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.hashing.sha256", m)?; + m.setattr("__name__", "binlex.hashing.sha256")?; + Ok(()) +} diff --git a/src/bindings/python/src/hashing/tlsh.rs b/src/bindings/python/src/hashing/tlsh.rs new file mode 100644 index 00000000..6d40dcc5 --- /dev/null +++ b/src/bindings/python/src/hashing/tlsh.rs @@ -0,0 +1,37 @@ +use pyo3::prelude::*; + +use binlex::hashing::tlsh::TLSH as InnerTLSH; + +#[pyclass] +pub struct TLSH { + bytes: Vec, +} + +#[pymethods] +impl TLSH { + #[new] + #[pyo3(text_signature = "(bytes)")] + pub fn new(bytes: Vec) -> Self { + Self { + bytes: bytes, + } + } + + #[pyo3(text_signature = "($self)")] + pub fn hexdigest(&self, mininum_byte_size: usize) -> Option { + InnerTLSH::new(&self.bytes, mininum_byte_size).hexdigest() + } + +} + + +#[pymodule] +#[pyo3(name = "tlsh")] +pub fn tlsh_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.hashing.tlsh", m)?; + m.setattr("__name__", "binlex.hashing.tlsh")?; + Ok(()) +} diff --git a/src/bindings/python/src/lib.rs b/src/bindings/python/src/lib.rs new file mode 100644 index 00000000..0a7a8b5e --- /dev/null +++ b/src/bindings/python/src/lib.rs @@ -0,0 +1,33 @@ +pub mod formats; +pub mod types; +pub mod config; +pub mod hashing; +pub mod binary; +pub mod disassemblers; +pub mod controlflow; + +pub use config::Architecture; +pub use binary::Binary; +pub use config::Config; + +use crate::formats::formats_init; +use crate::types::types_init; +use crate::config::config_init; +use crate::binary::binary_init; +use crate::disassemblers::disassemblers_init; +use crate::controlflow::controlflow_init; + +use pyo3::{prelude::*, wrap_pymodule}; + +#[pymodule] +fn binlex(m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_wrapped(wrap_pymodule!(formats_init))?; + m.add_wrapped(wrap_pymodule!(controlflow_init))?; + m.add_wrapped(wrap_pymodule!(types_init))?; + m.add_wrapped(wrap_pymodule!(config_init))?; + m.add_wrapped(wrap_pymodule!(binary_init))?; + m.add_wrapped(wrap_pymodule!(disassemblers_init))?; + m.add_class::()?; + m.add_class::()?; + Ok(()) +} diff --git a/src/bindings/python/src/types/lz4string.rs b/src/bindings/python/src/types/lz4string.rs new file mode 100644 index 00000000..93826d6b --- /dev/null +++ b/src/bindings/python/src/types/lz4string.rs @@ -0,0 +1,37 @@ +use pyo3::prelude::*; +use binlex::types::LZ4String as InnerLZ4String; + +#[pyclass] +pub struct LZ4String { + pub inner: InnerLZ4String, +} + +#[pymethods] +impl LZ4String { + #[new] + pub fn new(string: String) -> Self { + Self { + inner: InnerLZ4String::new(&string) + } + } + + pub fn to_string(&self) -> String { + self.inner.to_string() + } + + fn __str__(&self) -> PyResult { + Ok(format!("{}", self.inner)) + } + +} + +#[pymodule] +#[pyo3(name = "lz4string")] +pub fn memorymappedfile_init(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.types.lz4string", m)?; + m.setattr("__name__", "binlex.types.lz4string")?; + Ok(()) +} diff --git a/src/bindings/python/src/types/memorymappedfile.rs b/src/bindings/python/src/types/memorymappedfile.rs new file mode 100644 index 00000000..2d97a779 --- /dev/null +++ b/src/bindings/python/src/types/memorymappedfile.rs @@ -0,0 +1,135 @@ +use pyo3::prelude::*; +use pyo3::exceptions; +use pyo3::types::PyMemoryView; +use memmap2::Mmap; +use binlex::types::MemoryMappedFile as InnerMemoryMappedFile; +use pyo3::ffi; +use std::os::raw::c_char; + +#[pyclass] +pub struct MemoryMappedFile { + pub inner: InnerMemoryMappedFile, + pub mmap: Option, +} + +#[pymethods] +impl MemoryMappedFile { + #[new] + pub fn new(path: &str, cache: bool) -> PyResult { + let path = std::path::PathBuf::from(path); + let inner = InnerMemoryMappedFile::new(path, cache) + .map_err(|e| exceptions::PyIOError::new_err(e.to_string()))?; + Ok(MemoryMappedFile { inner: inner, mmap: None }) + } + + #[getter] + pub fn is_cached(&self) -> bool { + self.inner.is_cached() + } + + #[getter] + pub fn path(&self) -> String { + self.inner.path() + } + + pub fn write(&mut self, data: &[u8]) -> PyResult { + let mut reader = std::io::Cursor::new(data); + self.inner + .write(&mut reader) + .map_err(|e| exceptions::PyIOError::new_err(e.to_string())) + } + + pub fn write_padding(&mut self, length: usize) -> PyResult<()> { + self.inner + .write_padding(length) + .map_err(|e| exceptions::PyIOError::new_err(e.to_string())) + } + + #[getter] + pub fn size(&self) -> PyResult { + self.inner + .size() + .map_err(|e| exceptions::PyIOError::new_err(e.to_string())) + } + + /// Maps the file into memory and returns a MappedFile object. + pub fn mmap(&self) -> PyResult { + let mmap = self + .inner + .mmap() + .map_err(|e| exceptions::PyIOError::new_err(e.to_string()))?; + Ok(MappedFile { mmap }) + } + + /// Maps the file into memory and returns a memoryview without copying data. + pub fn as_memoryview<'py>(&mut self, py: Python<'py>) -> PyResult> { + if self.mmap.is_none() { + let mmap = self + .inner + .mmap() + .map_err(|e| exceptions::PyIOError::new_err(e.to_string()))?; + self.mmap = Some(mmap); + } + if let Some(mmap) = &self.mmap { + let data = &mmap[..]; + let ptr = data.as_ptr() as *mut c_char; + let len = data.len() as ffi::Py_ssize_t; + unsafe { + // Create a raw memoryview pointer without copying data + let memview_ptr = ffi::PyMemoryView_FromMemory(ptr, len, ffi::PyBUF_READ); + if memview_ptr.is_null() { + Err(PyErr::fetch(py)) + } else { + // Convert the raw pointer into a PyObject + let obj = PyObject::from_owned_ptr(py, memview_ptr); + // Use downcast_bound to convert PyObject to Bound<'py, PyMemoryView> + let memview = obj.downcast_bound::(py)?; + Ok(memview.clone()) + } + } + } else { + Err(exceptions::PyRuntimeError::new_err("Failed to map file")) + } + } + +} + +#[pyclass] +pub struct MappedFile { + mmap: Mmap, +} + +#[pymethods] +impl MappedFile { + /// Returns a memoryview of the mapped file without copying data into RAM. + pub fn as_memoryview<'py>(&self, py: Python<'py>) -> PyResult> { + let data = &self.mmap[..]; + let ptr = data.as_ptr() as *mut c_char; + let len = data.len() as ffi::Py_ssize_t; + unsafe { + // Create a raw memoryview pointer without copying data + let memview_ptr = ffi::PyMemoryView_FromMemory(ptr, len, ffi::PyBUF_READ); + if memview_ptr.is_null() { + Err(PyErr::fetch(py)) + } else { + // Convert the raw pointer into a PyObject + let obj = PyObject::from_owned_ptr(py, memview_ptr); + // Use downcast_bound to convert PyObject to Bound<'py, PyMemoryView> + let memview = obj.downcast_bound::(py)?; + Ok(memview.clone()) + } + } + } +} + +#[pymodule] +#[pyo3(name = "memorymappedfile")] +pub fn memorymappedfile_init(py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_class::()?; + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.types.memorymappedfile", m)?; + m.setattr("__name__", "binlex.types.memorymappedfile")?; + Ok(()) +} diff --git a/src/bindings/python/src/types/mod.rs b/src/bindings/python/src/types/mod.rs new file mode 100644 index 00000000..49d3a8e7 --- /dev/null +++ b/src/bindings/python/src/types/mod.rs @@ -0,0 +1,22 @@ +pub mod memorymappedfile; +pub mod lz4string; + +use crate::types::memorymappedfile::memorymappedfile_init; + +pub use crate::types::memorymappedfile::MemoryMappedFile; +pub use crate::types::lz4string::LZ4String; + +use pyo3::{prelude::*, wrap_pymodule}; + +#[pymodule] +#[pyo3(name = "types")] +pub fn types_init(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> { + m.add_wrapped(wrap_pymodule!(memorymappedfile_init))?; + m.add_class::()?; + m.add_class::()?; + py.import_bound("sys")? + .getattr("modules")? + .set_item("binlex.types", m)?; + m.setattr("__name__", "binlex.types")?; + Ok(()) +} diff --git a/src/binlex.cpp b/src/binlex.cpp deleted file mode 100644 index 26d1021c..00000000 --- a/src/binlex.cpp +++ /dev/null @@ -1,180 +0,0 @@ -#include -#include -#include -#include -#if defined(__linux__) || defined(__APPLE__) -#include -#include -#elif _WIN32 -#include -#endif -#include "args.h" -#include "pe.h" -#include "raw.h" -#include "cil.h" -#include "pe-dotnet.h" -#include "blelf.h" -#include "auto.h" -#include "disassembler.h" - -#ifdef _WIN32 -#pragma comment(lib, "capstone") -#pragma comment(lib, "binlex") -#endif -using namespace binlex; - -void timeout_handler(int signum) { - (void)signum; - fprintf(stderr, "[x] execution timeout\n"); - exit(0); -} - -#if defined(__linux__) || defined(__APPLE__) -void start_timeout(time_t seconds){ - struct itimerval timer; - timer.it_value.tv_sec = seconds; - timer.it_value.tv_usec = 0; - timer.it_interval.tv_sec = 0; - timer.it_interval.tv_usec = 0; - setitimer (ITIMER_VIRTUAL, &timer, 0); - struct sigaction sa; - memset(&sa, 0, sizeof (sa)); - sa.sa_handler = &timeout_handler; - sigaction(SIGVTALRM, &sa, 0); -} -#endif - -int main(int argc, char **argv){ - g_args.parse(argc, argv); - if (g_args.options.debug == true){ - LIEF::logging::set_level(LIEF::logging::LOGGING_LEVEL::LOG_DEBUG); - } else { - LIEF::logging::disable(); - } - if (g_args.options.timeout > 0){ - #if defined(__linux__) || defined(__APPLE__) - start_timeout(g_args.options.timeout); - #endif - } - if (g_args.options.mode.empty() == true){ - g_args.print_help(); - return EXIT_FAILURE; - } - if (g_args.options.mode == "auto" && - g_args.options.io_type == ARGS_IO_TYPE_FILE){ - AutoLex autolex; - return autolex.ProcessFile(g_args.options.input); - return 0; - } - if (g_args.options.mode == "elf:x86_64" && - g_args.options.io_type == ARGS_IO_TYPE_FILE){ - ELF elf64; - elf64.SetArchitecture(BINARY_ARCH_X86_64, BINARY_MODE_64); - if (elf64.ReadFile(g_args.options.input) == false){ - return EXIT_FAILURE; - } - PRINT_DEBUG("[binlex.cpp] number of total executable sections = %u\n", elf64.total_exec_sections); - Disassembler disassembler(elf64); - disassembler.Disassemble(); - disassembler.WriteTraits(); - return EXIT_SUCCESS; - } - if (g_args.options.mode == "elf:x86" && - g_args.options.io_type == ARGS_IO_TYPE_FILE){ - ELF elf32; - elf32.SetArchitecture(BINARY_ARCH_X86, BINARY_MODE_32); - if (elf32.ReadFile(g_args.options.input) == false){ - return EXIT_FAILURE; - } - PRINT_DEBUG("[binlex.cpp] number of total executable sections = %u\n", elf32.total_exec_sections); - Disassembler disassembler(elf32); - disassembler.Disassemble(); - disassembler.WriteTraits(); - return EXIT_SUCCESS; - } - if (g_args.options.mode == "pe:cil" && - g_args.options.io_type == ARGS_IO_TYPE_FILE){ - // TODO: This should be valid for both x86-86 and x86-64 - // we need to do this more generic - DOTNET pe; - pe.SetArchitecture(BINARY_ARCH_X86, BINARY_MODE_CIL); - if (pe.ReadFile(g_args.options.input) == false) return 1; - CILDisassembler disassembler(pe); - PRINT_DEBUG("[binlex.cpp] analyzing %lu sections for CIL byte code.\n", pe._sections.size()); - int si = 0; - for (auto section : pe._sections) { - if (section.offset == 0) continue; - if (disassembler.Disassemble(section.data, section.size, si) == false){ - continue; - } - si++; - } - disassembler.WriteTraits(); - return EXIT_SUCCESS; - } - if (g_args.options.mode == "pe:x86" && - g_args.options.io_type == ARGS_IO_TYPE_FILE){ - PE pe32; - pe32.SetArchitecture(BINARY_ARCH_X86, BINARY_MODE_32); - if (pe32.ReadFile(g_args.options.input) == false){ - return EXIT_FAILURE; - } - PRINT_DEBUG("[binlex.cpp] number of total sections = %u\n", pe32.total_exec_sections); - Disassembler disassembler(pe32); - disassembler.Disassemble(); - disassembler.WriteTraits(); - return EXIT_SUCCESS; - } - if (g_args.options.mode == "pe:x86_64" && - g_args.options.io_type == ARGS_IO_TYPE_FILE){ - PE pe64; - pe64.SetArchitecture(BINARY_ARCH_X86_64, BINARY_MODE_64); - if (pe64.ReadFile(g_args.options.input) == false){ - return EXIT_FAILURE; - } - PRINT_DEBUG("[binlex.cpp] number of total executable sections = %u\n", pe64.total_exec_sections); - Disassembler disassembler(pe64); - disassembler.Disassemble(); - disassembler.WriteTraits(); - return EXIT_SUCCESS; - } - if (g_args.options.mode == "raw:x86" && - g_args.options.io_type == ARGS_IO_TYPE_FILE){ - Raw rawx86; - rawx86.SetArchitecture(BINARY_ARCH_X86, BINARY_MODE_32); - if (rawx86.ReadFile(g_args.options.input) == false){ - return EXIT_FAILURE; - } - Disassembler disassembler(rawx86); - disassembler.Disassemble(); - disassembler.WriteTraits(); - return EXIT_SUCCESS; - } - if (g_args.options.mode == "raw:x86_64" && - g_args.options.io_type == ARGS_IO_TYPE_FILE){ - Raw rawx86_64; - rawx86_64.SetArchitecture(BINARY_ARCH_X86_64, BINARY_MODE_64); - if (rawx86_64.ReadFile(g_args.options.input) == false){ - return EXIT_FAILURE; - } - Disassembler disassembler(rawx86_64); - disassembler.Disassemble(); - disassembler.WriteTraits(); - return EXIT_SUCCESS; - } - if (g_args.options.mode == "raw:cil" && - g_args.options.io_type == ARGS_IO_TYPE_FILE){ - Raw rawcil; - rawcil.SetArchitecture(BINARY_ARCH_X86, BINARY_MODE_CIL); - if (rawcil.ReadFile(g_args.options.input) == false){ - return EXIT_FAILURE; - } - CILDisassembler disassembler(rawcil); - disassembler.Disassemble(rawcil.sections[0].data, rawcil.sections[0].size, 0); - disassembler.WriteTraits(); - return EXIT_SUCCESS; - } - - g_args.print_help(); - return EXIT_FAILURE; -} diff --git a/src/blelf.cpp b/src/blelf.cpp deleted file mode 100644 index ec2bbcb1..00000000 --- a/src/blelf.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "blelf.h" - -using namespace binlex; -using namespace LIEF::ELF; - -ELF::ELF(){ - total_exec_sections = 0; - for (int i = 0; i < BINARY_MAX_SECTIONS; i++){ - sections[i].offset = 0; - sections[i].size = 0; - sections[i].data = NULL; - } -} - -bool ELF::ReadVector(const std::vector &data){ - binary = Parser::parse(data); - if (binary == NULL){ - return false; - } - if (binary_arch == BINARY_ARCH_UNKNOWN || - binary_mode == BINARY_MODE_UNKNOWN){ - switch(binary->header().machine_type()){ - case ARCH::EM_386: - SetArchitecture(BINARY_ARCH_X86, BINARY_MODE_32); - g_args.options.mode = "elf:x86"; - break; - case ARCH::EM_X86_64: - SetArchitecture(BINARY_ARCH_X86, BINARY_MODE_64); - g_args.options.mode = "elf:x86_64"; - break; - default: - binary_arch = BINARY_ARCH_UNKNOWN; - binary_mode = BINARY_MODE_UNKNOWN; - return false; - } - } - CalculateFileHashes(data); - binary_type = BINARY_TYPE_ELF; - return ParseSections(); -} - -bool ELF::ParseSections(){ - uint index = 0; - Binary::it_sections local_sections = binary->sections(); - for (auto it = local_sections.begin(); it != local_sections.end(); it++){ - if (it->flags() & (uint64_t)ELF_SECTION_FLAGS::SHF_EXECINSTR){ - vector data = binary->get_content_from_virtual_address(it->virtual_address(), it->original_size()); - if (data.size() == 0) { - continue; - } - sections[index].offset = it->offset(); - sections[index].size = it->original_size(); - sections[index].data = malloc(sections[index].size); - memset(sections[index].data, 0, sections[index].size); - memcpy(sections[index].data, &data[0], sections[index].size); - Binary::it_exported_symbols symbols = binary->exported_symbols(); - // Add export to function list - for (auto j = symbols.begin(); j != symbols.end(); j++){ - uint64_t tmp_offset = binary->virtual_address_to_offset(j->value()); - PRINT_DEBUG("Elf Export offset: 0x%x\n", (int)tmp_offset); - if (tmp_offset > sections[index].offset && - tmp_offset < sections[index].offset + sections[index].size){ - sections[index].functions.insert(tmp_offset-sections[index].offset); - } - } - // Add entrypoint to the function list - uint64_t entrypoint_offset = binary->virtual_address_to_offset(binary->entrypoint()); - PRINT_DEBUG("Elf Entrypoint offset: 0x%x\n", (int)entrypoint_offset); - if (entrypoint_offset > sections[index].offset && entrypoint_offset < sections[index].offset + sections[index].size){ - sections[index].functions.insert(entrypoint_offset-sections[index].offset); - } - index++; - if (BINARY_MAX_SECTIONS == index) - { - fprintf(stderr, "[x] malformed binary, too many executable sections\n"); - return false; - } - } - } - total_exec_sections = index + 1; - return true; -} - -ELF::~ELF(){ - for (uint32_t i = 0; i < total_exec_sections; i++){ - sections[i].offset = 0; - sections[i].size = 0; - free(sections[i].data); - sections[i].functions.clear(); - } -} diff --git a/src/blyara.cpp b/src/blyara.cpp deleted file mode 100644 index 4a5987bc..00000000 --- a/src/blyara.cpp +++ /dev/null @@ -1,172 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "json.h" - -using namespace std; -using json = nlohmann::json; - -class Args{ - public: - char version[7] = "v1.1.1"; - struct { - json metadata; - json traits; - char *input; - char *name; - char *output; - bool version; - bool help; - int count; - } options; - Args(){ - SetDefault(); - } - void PrintHelp(){ - printf( - "blyara %s - A Binlex Yara Generation Utility\n" - " -i --input\t\tinput file\t\t(optional)\n" - " -m --metadata\tset metadata\t\t(optional)\n" - " -n --name\t\tsignature name\n" - " -c --count\t\tcount\t\t\t(optional)\n" - " -h --help\t\tdisplay help\n" - " -o --output\t\toutput file\t\t(optional)\n" - " -v --version\t\tdisplay version\n" - "Author: @c3rb3ru5d3d53c\n", - version - ); - } - void Parse(int argc, char **argv){ - for (int i = 0; i < argc; i++){ - if (strcmp(argv[i], (char *)"-h") == 0 || - strcmp(argv[i], (char *)"--help") == 0){ - options.help = true; - PrintHelp(); - exit(0); - } - if (strcmp(argv[i], (char *)"-v") == 0 || - strcmp(argv[i], (char *)"--version") == 0){ - options.version = true; - printf("%s\n", version); - exit(0); - } - if (strcmp(argv[i], (char *)"-i") == 0 || - strcmp(argv[i], (char *)"--input") == 0){ - if (argc < i+2){ - fprintf(stderr, "[x] input requires 1 parameter\n"); - exit(1); - } - options.input = argv[i+1]; - } - if (strcmp(argv[i], (char *)"-o") == 0 || - strcmp(argv[i], (char *)"--output") == 0){ - if (argc < i+2){ - fprintf(stderr, "[x] input requires 1 parameter\n"); - exit(1); - } - options.output = argv[i+1]; - } - if (strcmp(argv[i], (char *)"-n") == 0 || - strcmp(argv[i], (char *)"--name") == 0){ - if (argc < i+2){ - fprintf(stderr, "[x] name requires 1 parameter\n"); - exit(1); - } - options.name = argv[i+1]; - } - if (strcmp(argv[i], (char *)"-c") == 0 || - strcmp(argv[i], (char *)"--count") == 0){ - if (argc < i+2){ - fprintf(stderr, "[x] count requires 1 parameter\n"); - exit(1); - } - options.count = atoi(argv[i+1]); - if (options.count < 1){ - fprintf(stderr, "[x] count must be greater or equal to 1\n"); - exit(1); - } - } - if (strcmp(argv[i], (char *)"-m") == 0 || - strcmp(argv[i], (char *)"--metadata") == 0){ - if (argc < i+3){ - fprintf(stderr, "[x] metadata requires 2 parameters\n"); - exit(1); - } - options.metadata[argv[i+1]] = argv[i+2]; - i = i + 2; - } - } - if (options.name == NULL){ - fprintf(stderr, "[x] name parameter is required\n"); - exit(1); - } - if (options.input == NULL){ - CollectStdinTraits(); - } else { - CollectInputTraits(); - } - } - void CollectStdinTraits(){ - int count = 0; - for (string line; getline(cin, line);) { - options.traits["trait_" + to_string(count)] = "{" + line + "}"; - count++; - } - } - void CollectInputTraits(){ - fstream input_file; - input_file.open(options.input, ios::in); - if (input_file.is_open()) { - string line; - int count = 0; - while (getline(input_file, line)) { - options.traits["trait_" + to_string(count)] = "{" + line + "}"; - count++; - } - input_file.close(); - } - } - void SetDefault(){ - options.count = 1; - options.name = NULL; - options.input = NULL; - options.output = NULL; - options.help = false; - } - ~Args(){ - SetDefault(); - } -}; - -int main(int argc, char **argv){ - Args args; - args.Parse(argc, argv); - if (args.options.traits.is_null() == false){ - stringstream signature; - signature << "rule " << args.options.name << " {" << endl; - if (args.options.metadata.is_null() == false){ - signature << " " << "meta:" << endl; - for (json::iterator it = args.options.metadata.begin(); it != args.options.metadata.end(); ++it){ - signature << " " << it.key() << " = " << it.value() << endl; - } - } - signature << " " << "strings:" << endl; - for (json::iterator it = args.options.traits.begin(); it != args.options.traits.end(); ++it){ - signature << " $" << it.key() << " = " << it.value().get() << endl; - } - signature << " " << "condition:" << endl; - signature << " " << to_string(args.options.count) << " of them" << endl; - signature << "}" << endl; - if (args.options.output == NULL){ - cout << signature.str(); - } else { - FILE *fd = fopen(args.options.output, "w"); - fwrite(signature.str().c_str(), sizeof(char), signature.str().length(), fd); - fclose(fd); - } - } - return 0; -} diff --git a/src/cil.cpp b/src/cil.cpp deleted file mode 100644 index ebb5d636..00000000 --- a/src/cil.cpp +++ /dev/null @@ -1,589 +0,0 @@ -#include "cil.h" - -using namespace binlex; -using json = nlohmann::json; - -CILDisassembler::CILDisassembler(const binlex::File &firef) : DisassemblerBase(firef) { - type = CIL_DISASSEMBLER_TYPE_ALL; - for (int i = 0; i < CIL_DISASSEMBLER_MAX_SECTIONS; i++){ - sections[i].offset = 0; - sections[i].ntraits = 0; - sections[i].data = NULL; - sections[i].data_size = 0; - sections[i].threads = 1; - sections[i].thread_cycles = 1; - sections[i].thread_sleep = 500; - sections[i].corpus = g_args.options.corpus; - sections[i].instructions = false; - sections[i].arch_str = NULL; - } - //Maps give us O(nlogn) lookup efficiency - //much better than case statements - prefixInstrMap = { - {CIL_INS_CEQ, 0}, - {CIL_INS_ARGLIST, 0}, - {CIL_INS_CGT, 0}, - {CIL_INS_CGT_UN, 0}, - {CIL_INS_CLT, 0}, - {CIL_INS_CLT_UN, 0}, - {CIL_INS_CONSTRAINED, 32}, - {CIL_INS_CPBLK, 0}, - {CIL_INS_ENDFILTER, 0}, - {CIL_INS_INITBLK, 0}, - {CIL_INS_INITOBJ, 32}, - {CIL_INS_LDARG, 16}, - {CIL_INS_LDARGA, 32}, - {CIL_INS_LDFTN, 32}, - {CIL_INS_LDLOC, 16}, - {CIL_INS_LDLOCA, 16}, - {CIL_INS_LDVIRTFTN, 32}, - {CIL_INS_LOCALLOC, 0}, - {CIL_INS_NO, 0}, - {CIL_INS_READONLY, 32}, - {CIL_INS_REFANYTYPE, 0}, - {CIL_INS_RETHROW, 0}, - {CIL_INS_SIZEOF, 32}, - {CIL_INS_STARG, 16}, - {CIL_INS_STLOC, 16}, - {CIL_INS_TAIL, 0}, - {CIL_INS_UNALIGNED, 0}, - {CIL_INS_VOLATILE, 32} - }; - condInstrMap = { - {CIL_INS_BEQ, 32}, - {CIL_INS_BEQ_S, 8}, - {CIL_INS_BGE, 32}, - {CIL_INS_BGE_S, 8}, - {CIL_INS_BGE_UN, 32}, - {CIL_INS_BGE_UN_S, 8}, - {CIL_INS_BGT, 32}, - {CIL_INS_BGT_S, 8}, - {CIL_INS_BGT_UN, 32}, - {CIL_INS_BGT_UN_S, 8}, - {CIL_INS_BLE, 32}, - {CIL_INS_BLE_S, 8}, - {CIL_INS_BLE_UN, 32}, - {CIL_INS_BLE_UN_S, 8}, - {CIL_INS_BLT, 32}, - {CIL_INS_BLT_S, 8}, - {CIL_INS_BLT_UN, 32}, - {CIL_INS_BLT_UN_S, 8}, - {CIL_INS_BNE_UN, 32}, - {CIL_INS_BNE_UN_S, 8}, - {CIL_INS_BOX, 32}, - {CIL_INS_BR, 32}, - {CIL_INS_BR_S, 8}, - {CIL_INS_BREAK, 0}, - {CIL_INS_BRFALSE, 32}, - {CIL_INS_BRFALSE_S, 8}, - // case CIL_INS_BRINST: - // printf("brinst\n"); - // break; - // case CIL_INS_BRINST_S: - // printf("brinst.s\n"); - // break; - // case CIL_INS_BRNULL: - // printf("brnull\n"); - // break; - // case CIL_INS_BRNULL_S: - // printf("brnull.s\n"); - // break; - {CIL_INS_BRTRUE, 32}, - {CIL_INS_BRTRUE_S, 8} - // case CIL_INS_BRZERO: - // printf("brzero\n"); - // break; - // case CIL_INS_BRZERO_S: - // printf("brzero.s\n"); - // break; - }; - - miscInstrMap = { - {CIL_INS_ADD, 0}, - {CIL_INS_ADD_OVF, 0}, - {CIL_INS_ADD_OVF_UN, 0}, - {CIL_INS_AND, 0}, - {CIL_INS_CASTCLASS, 32}, - {CIL_INS_CKINITE, 0}, - {CIL_INS_CONV_I, 0}, - {CIL_INS_CONV_I1, 0}, - {CIL_INS_CONV_I2, 0}, - {CIL_INS_CONV_I4, 0}, - {CIL_INS_CONV_I8, 0}, - {CIL_INS_CONV_OVF_i, 0}, - {CIL_INS_CONV_OVF_I_UN, 0}, - {CIL_INS_CONV_OVF_I1, 0}, - {CIL_INS_CONV_OVF_I1_UN, 0}, - {CIL_INS_CONV_OVF_I2, 0}, - {CIL_INS_CONV_OVF_I2_UN, 0}, - {CIL_INS_CONV_OVF_I4, 0}, - {CIL_INS_CONV_OVF_I4_UN, 0}, - {CIL_INS_CONV_OVF_I8, 0}, - {CIL_INS_CONV_OVF_I8_UN, 0}, - {CIL_INS_CONV_OVF_U, 0}, - {CIL_INS_CONV_OVF_U_UN, 0}, - {CIL_INS_CONV_OVF_U1, 0}, - {CIL_INS_CONV_OVF_U1_UN, 0}, - {CIL_INS_CONV_OVF_U2, 0}, - {CIL_INS_CONV_OVF_U2_UN, 0}, - {CIL_INS_CONV_OVF_U4, 0}, - {CIL_INS_CONV_OVF_U4_UN, 0}, - {CIL_INS_CONV_OVF_U8, 0}, - {CIL_INS_CONV_OVF_U8_UN, 0}, - {CIL_INS_CONV_R_UN, 0}, - {CIL_INS_CONV_R4, 0}, - {CIL_INS_CONV_R8, 0}, - {CIL_INS_CONV_U, 0}, - {CIL_INS_CONV_U1, 0}, - {CIL_INS_CONV_U2, 0}, - {CIL_INS_CONV_U4, 0}, - {CIL_INS_CONV_U8, 0}, - {CIL_INS_CPOBJ, 32}, - {CIL_INS_DIV, 0}, - {CIL_INS_DIV_UN, 0}, - {CIL_INS_DUP, 0}, - //CIL_INS_ENDFAULT: - //printf("endfault - //break; - {CIL_INS_ENDFINALLY, 0}, - {CIL_INS_ISINST, 32}, - {CIL_INS_JMP, 32}, - {CIL_INS_LDARG_0, 0}, - {CIL_INS_LDARG_1, 0}, - {CIL_INS_LDARG_2, 0}, - {CIL_INS_LDARG_3, 0}, - {CIL_INS_LDARG_S, 8}, - {CIL_INS_LDARGA_S, 8}, - {CIL_INS_LDC_I4, 32}, - {CIL_INS_LDC_I4_0, 0}, - {CIL_INS_LDC_I4_1, 0}, - {CIL_INS_LDC_I4_2, 0}, - {CIL_INS_LDC_I4_3, 0}, - {CIL_INS_LDC_I4_4, 0}, - {CIL_INS_LDC_I4_5, 0}, - {CIL_INS_LDC_I4_6, 0}, - {CIL_INS_LDC_I4_7, 0}, - {CIL_INS_LDC_I4_8, 0}, - {CIL_INS_LDC_I4_M1, 0}, - {CIL_INS_LDC_I4_S, 8}, - {CIL_INS_LDC_I8, 64}, - {CIL_INS_LDC_R4, 32}, - {CIL_INS_LDC_R8, 64}, - {CIL_INS_LDELM, 32}, - {CIL_INS_LDELM_I, 0}, - {CIL_INS_LDELM_I1, 0}, - {CIL_INS_LDELM_I2, 0}, - {CIL_INS_LDELM_I4, 0}, - {CIL_INS_LDELM_I8, 0}, - {CIL_INS_LDELM_R4, 0}, - {CIL_INS_LDELM_R8, 0}, - {CIL_INS_LDELM_REF, 0}, - {CIL_INS_LDELM_U1, 0}, - {CIL_INS_LDELM_U2, 0}, - {CIL_INS_LDELM_U4, 0}, - //CIL_INS_LDELM_U8: - //printf("ldelm.u8 - //break; - {CIL_INS_LDELMA, 32}, - {CIL_INS_LDFLD, 32}, - {CIL_INS_LDFLDA, 32}, - {CIL_INS_LDIND_I, 0}, - {CIL_INS_LDIND_I1, 0}, - {CIL_INS_LDIND_I2, 0}, - {CIL_INS_LDIND_I4, 0}, - {CIL_INS_LDIND_I8, 0}, - {CIL_INS_LDIND_R4, 0}, - {CIL_INS_LDIND_R8, 0}, - {CIL_INS_LDIND_REF, 0}, - {CIL_INS_LDIND_U1, 0}, - {CIL_INS_LDIND_U2, 0}, - {CIL_INS_LDIND_U4, 0}, - //CIL_INS_LDIND_U8: - //printf("ldind.u8 - //break; - {CIL_INS_LDLEN, 0}, - {CIL_INS_LDLOC_0, 0}, - {CIL_INS_LDLOC_1, 0}, - {CIL_INS_LDLOC_2, 0}, - {CIL_INS_LDLOC_3, 0}, - {CIL_INS_LDLOC_S, 8}, - {CIL_INS_LDLOCA_S, 8}, - {CIL_INS_LDNULL, 0}, - {CIL_INS_LDOBJ, 32}, - {CIL_INS_LDSFLD, 32}, - {CIL_INS_LDSFLDA, 32}, - {CIL_INS_LDSTR, 32}, - {CIL_INS_LDTOKEN, 32}, - {CIL_INS_LEAVE, 32}, - {CIL_INS_LEAVE_S, 8}, - {CIL_INS_MKREFANY, 32}, - {CIL_INS_MUL, 0}, - {CIL_INS_MUL_OVF, 0}, - {CIL_INS_MUL_OVF_UN, 0}, - {CIL_INS_NEG, 0}, - {CIL_INS_NEWARR, 32}, - {CIL_INS_NEWOBJ, 32}, - {CIL_INS_NOP, 0}, - {CIL_INS_NOT, 0}, - {CIL_INS_OR, 0}, - {CIL_INS_POP, 0}, - {CIL_INS_REFANYVAL, 32}, - {CIL_INS_REM, 0}, - {CIL_INS_REM_UN, 0}, - {CIL_INS_RET, 0}, - {CIL_INS_SHL, 0}, - {CIL_INS_SHR, 0}, - {CIL_INS_SHR_UN, 0}, - {CIL_INS_STARG_S, 8}, - {CIL_INS_STELEM, 32}, - {CIL_INS_STELEM_I, 0}, - {CIL_INS_STELEM_I1, 0}, - {CIL_INS_STELEM_I2, 0}, - {CIL_INS_STELEM_I4, 0}, - {CIL_INS_STELEM_I8, 0}, - {CIL_INS_STELEM_R4, 0}, - {CIL_INS_STELEM_R8, 0}, - {CIL_INS_STELEM_REF, 0}, - {CIL_INS_STFLD, 32}, - {CIL_INS_STIND_I, 0}, - {CIL_INS_STIND_I1, 0}, - {CIL_INS_STIND_I2, 0}, - {CIL_INS_STIND_I4, 0}, - {CIL_INS_STIND_I8, 0}, - {CIL_INS_STIND_R4, 0}, - {CIL_INS_STIND_R8, 0}, - {CIL_INS_STIND_REF, 0}, - {CIL_INS_STLOC_S, 8}, - {CIL_INS_STLOC_0, 0}, - {CIL_INS_STLOC_1, 0}, - {CIL_INS_STLOC_2, 0}, - {CIL_INS_STLOC_3, 0}, - {CIL_INS_STOBJ, 32}, - {CIL_INS_STSFLD, 32}, - {CIL_INS_SUB, 0}, - {CIL_INS_SUB_OVF, 0}, - {CIL_INS_SUB_OVF_UN, 0}, - {CIL_INS_SWITCH, 32}, - {CIL_INS_THROW, 0}, - {CIL_INS_UNBOX, 32}, - {CIL_INS_UNBOX_ANY, 32}, - {CIL_INS_XOR, 0}, - {CIL_INS_CALL, 32}, - {CIL_INS_CALLI, 32}, - {CIL_INS_CALLVIRT, 32} - }; -} - -int CILDisassembler::update_offset(int operand_size, int i) { - //fprintf(stderr, "[+] updating offset using operand size %d\n", operand_size); - switch(operand_size){ - case 0: - break; - case 8: - i++; - break; - case 16: - i = i + 2; - break; - case 32: - i = i + 4; - break; - case 64: - i = i + 8; - break; - default: - fprintf(stderr, "[x] unknown operand size %d\n", operand_size); - i = -1; - } - return i; -} - -bool CILDisassembler::Disassemble(void *data, int data_size, int index){ - const unsigned char *pc = (const unsigned char *)data; - vector traits; - vector ftraits; - vector< Instruction* >* instructions = new vector; - vector< Instruction* >* finstructions = new vector; - //We need an iterator for our hashmap searches - map::iterator it; - uint num_edges = 0; - uint num_f_edges = 0; - uint num_instructions = 0; - uint num_f_instructions = 0; - uint func_block_count = 0; - for (int i = 0; i < data_size; i++){ - bool end_block = false; - bool end_func = false; - Instruction *insn = new Instruction; - PRINT_DEBUG("Instruction being decompiled: 0x%x\n", pc[i]); - if (pc[i] == CIL_INS_PREFIX){ - //Let's add prefix instruction to our instructions - insn->instruction = pc[i]; - insn->operand_size = 0; - insn->offset = i; - instructions->push_back(insn); - finstructions->push_back(insn); - //Then let's move on to the next instruction - i++; - PRINT_DEBUG("Instruction being decompiled: 0x%x\n", pc[i]); - //Then let's create a new instruction for the ... new instruction - insn = new Instruction; - insn->instruction = pc[i]; - it = prefixInstrMap.find(pc[i]); - if(it != prefixInstrMap.end()) { - PRINT_DEBUG("[+] found prefix opcode 0x%02x at offset %d with operand size: %d\n", pc[i], i, it->second); - insn->instruction = pc[i]; - insn->operand_size = it->second; - insn->offset = i; - instructions->push_back(insn); - finstructions->push_back(insn); - num_instructions++; - num_f_instructions++; - } else { - PRINT_ERROR_AND_EXIT( "[x] unknown prefix opcode 0x%02x at offset %d\n", pc[i], i); - return false; - } - } else { - it = condInstrMap.find(pc[i]); - if(it != condInstrMap.end()) { - num_edges++; - insn->instruction = pc[i]; - insn->operand_size = it->second; - insn->offset = i; - instructions->push_back(insn); - finstructions->push_back(insn); - end_block = true; - num_instructions++; - num_f_instructions++; - PRINT_DEBUG("[+] end block found -> opcode 0x%02x at offset %d\n", pc[i], i); - } else { - it = miscInstrMap.find(pc[i]); - if(it != miscInstrMap.end()) { - PRINT_DEBUG("[+] found misc opcode 0x%02x at offset %d with operand size: %d\n", pc[i], i, it->second); - insn->instruction = pc[i]; - insn->operand_size = it->second; - insn->offset = i; - instructions->push_back(insn); - finstructions->push_back(insn); - num_instructions++; - num_f_instructions++; - } else { - PRINT_ERROR_AND_EXIT("[x] unknown opcode 0x%02x at offset %d\n", pc[i], i); - return false; - } - } - } - if(insn->instruction == CIL_INS_RET) { - end_func = true; - } - - int updated = update_offset(insn->operand_size, i); - if (updated != -1) { - i = updated; - } - //If we're at the end of a block, at the end of a function, or - //at the end of our data then we need to store the block trait data. - //Even the end of a function should be considered a "block". - if ((end_func || (end_block && i < data_size - 1)) || - ((end_block == false && end_func == false) && i == data_size -1)) { - Trait *ctrait = new Trait; - ctrait->instructions = instructions; - ctrait->corpus = sections[index].corpus; - //Limiting to x86 for now but this should be set by the PE parsing code - //higher up in the call-stack. - ctrait->architecture = "x86"; - //The first offset of the first instruction will give us the offset - //of our trait. - uint trait_offset = instructions->front()->offset; - PRINT_DEBUG("Adding offset to trait: %d\n", trait_offset); - ctrait->offset = instructions->front()->offset; - ctrait->num_instructions = num_instructions; - ctrait->trait = ConvTraitBytes(*instructions); - ctrait->bytes = ConvBytes(*instructions, data, data_size); - //Since traits are differentiated by blocks then this will always be 1 - //maybe this should be different in the future? - ctrait->blocks = 1; - ctrait->edges = num_edges; - ctrait->size = SizeOfTrait(*instructions); - ctrait->invalid_instructions = 0; //TODO - ctrait->type = "block"; - ctrait->corpus = string(sections[index].corpus); - //The cyclomatic complexity differs by type of trait. - //Which for now only supports block. - ctrait->cyclomatic_complexity = num_edges - 1 + 2; - ctrait->average_instructions_per_block = instructions->size(); - ctrait->bytes_entropy = Entropy(ctrait->bytes); - ctrait->trait_entropy = Entropy(ctrait->trait); - ctrait->trait_sha256 = SHA256(&ctrait->trait[0]); - ctrait->bytes_sha256 = SHA256(&ctrait->bytes[0]); - //The number of edges needs to be reset once the trait is stored. - num_edges = 0; - sections[index].block_traits.push_back(ctrait); - //Once we're done adding a trait we need to create a new set of instructions - //for the next trait. - instructions = new vector; - num_instructions = 0; - func_block_count++; - } - if ((end_func && i < data_size - 1) || - ((end_func == false) && i == data_size -1)) { - Trait *ftrait = new Trait; - ftrait->instructions = finstructions; - ftrait->corpus = sections[index].corpus; - //Limiting to x86 for now but this should be set by the PE parsing code - //higher up in the call-stack. - ftrait->architecture = "x86"; - //The first offset of the first instruction will give us the offset - //of our trait. - uint trait_offset = finstructions->front()->offset; - PRINT_DEBUG("Adding offset to function trait: %d\n", trait_offset); - ftrait->offset = finstructions->front()->offset; - ftrait->num_instructions = num_f_instructions; - ftrait->trait = ConvTraitBytes(*finstructions); - ftrait->bytes = ConvBytes(*finstructions, data, data_size); - ftrait->blocks = func_block_count; - ftrait->edges = num_edges; - ftrait->size = SizeOfTrait(*finstructions); - ftrait->invalid_instructions = 0; //TODO - ftrait->type = "function"; - ftrait->corpus = string(sections[index].corpus); - ftrait->cyclomatic_complexity = num_f_edges - func_block_count + 2; - ftrait->average_instructions_per_block = finstructions->size()/func_block_count; - ftrait->bytes_entropy = Entropy(ftrait->bytes); - ftrait->trait_entropy = Entropy(ftrait->trait); - ftrait->trait_sha256 = SHA256(&ftrait->trait[0]); - ftrait->bytes_sha256 = SHA256(&ftrait->bytes[0]); - //The number of edges needs to be reset once the trait is stored. - num_edges = 0; - sections[index].function_traits.push_back(ftrait); - //Once we're done adding a trait we need to create a new set of instructions - //for the next trait. - finstructions = new vector; - num_f_instructions = 0; - func_block_count = 0; - } - } - return true; -} - -string CILDisassembler::ConvTraitBytes(vector< Instruction* > allinst) { - string rstr = ""; - string fstr = ""; - for(auto inst : allinst) { - if(inst->instruction == CIL_INS_NOP) { - rstr.append("??"); - rstr.append(" "); - } else { - char hexbytes[3]; - sprintf(hexbytes, "%02x", inst->instruction); - hexbytes[2] = '\0'; - rstr.append(string(hexbytes)); - rstr.append(" "); - } - for(uint i = 0; i < inst->operand_size/8; i++) { - rstr.append("??"); - rstr.append(" "); - } - } - fstr = TrimRight(rstr); - return fstr; -} - -uint CILDisassembler::SizeOfTrait(vector< Instruction* > inst) { - int begin_offset = inst.front()->offset; - int end_offset = inst.back()->offset; - uint size = (end_offset-begin_offset)+(inst.back()->operand_size/8)+1; - return size; -} - -string CILDisassembler::ConvBytes(vector< Instruction* > allinst, void *data, int data_size) { - string byte_rep = ""; - string byte_rep_t; - int begin_offset = allinst.front()->offset; - if(begin_offset > data_size) { - PRINT_ERROR_AND_EXIT("Beginning offset trait offset:\ - %d cannot be greater than total data length: %d", begin_offset, data_size); - } - uint trait_size = SizeOfTrait(allinst); - unsigned char *cdata = (unsigned char *)data; - char hexbytes[3]; - for(uint i = begin_offset; i < begin_offset+trait_size; i++) { - sprintf(hexbytes, "%02x", cdata[i]); - hexbytes[2] = '\0'; - byte_rep.append(string(hexbytes)); - byte_rep.append(" "); - } - byte_rep_t = TrimRight(byte_rep); - return byte_rep_t; -} - -json CILDisassembler::GetTrait(struct Trait *trait){ - json data; - data["type"] = trait->type; - data["corpus"] = trait->corpus; - data["tags"] = g_args.options.tags; - data["mode"] = g_args.options.mode; - data["bytes"] = trait->bytes; - data["trait"] = trait->trait; - data["edges"] = trait->edges; - data["blocks"] = trait->blocks; - data["instructions"] = trait->num_instructions; - data["size"] = trait->size; - data["offset"] = trait->offset; - data["bytes_entropy"] = trait->bytes_entropy; - data["bytes_sha256"] = trait->bytes_sha256; - string bytes_tlsh = TraitToTLSH(trait->bytes); - if (bytes_tlsh.length() > 0){ - data["bytes_tlsh"] = bytes_tlsh; - } else { - data["bytes_tlsh"] = nullptr; - } - string trait_tlsh = TraitToTLSH(trait->trait); - if (trait_tlsh.length() > 0){ - data["trait_tlsh"] = trait_tlsh; - } else { - data["trait_tlsh"] = nullptr; - } - data["trait_sha256"] = trait->trait_sha256; - data["trait_entropy"] = trait->trait_entropy; - data["invalid_instructions"] = trait->invalid_instructions; - data["cyclomatic_complexity"] = trait->cyclomatic_complexity; - data["average_instructions_per_block"] = trait->average_instructions_per_block; - return data; -} - -vector CILDisassembler::GetTraits(){ - vector traitsjson; - for (int i = 0; i < CIL_DISASSEMBLER_MAX_SECTIONS; i++){ - if ((sections[i].function_traits.size() > 0) && (type == CIL_DISASSEMBLER_TYPE_ALL - || type == CIL_DISASSEMBLER_TYPE_FUNCS)){ - for(auto trait : sections[i].function_traits) { - json jdata(GetTrait(trait)); - traitsjson.push_back(jdata); - } - } - if ((sections[i].block_traits.size() > 0) && (type == CIL_DISASSEMBLER_TYPE_ALL - || type == CIL_DISASSEMBLER_TYPE_BLCKS)){ - for(auto trait : sections[i].block_traits) { - json jdata(GetTrait(trait)); - traitsjson.push_back(jdata); - } - } - } - return traitsjson; -} - -CILDisassembler::~CILDisassembler() { - for (int i = 0; i < CIL_DISASSEMBLER_MAX_SECTIONS; i++){ - if (sections[i].function_traits.size() > 0) { - for(auto trait : sections[i].function_traits) { - delete trait->instructions; - } - } - if (sections[i].block_traits.size() > 0) { - for(auto trait : sections[i].block_traits) { - delete trait->instructions; - } - } - } -} diff --git a/src/common.cpp b/src/common.cpp deleted file mode 100644 index afd41c49..00000000 --- a/src/common.cpp +++ /dev/null @@ -1,254 +0,0 @@ -#include -#include -#include -#include -#include -#include "common.h" - -using namespace binlex; - -// Global Arguments -Args g_args; - -void print_data(string title, void *data, uint32_t size){ - if (g_args.options.debug){ - uint32_t i; - uint32_t counter = 0; - cerr << "Hexdump: " << title; - for (i = 0; i < size; i++) { - if (counter % 16 == 0) { cerr << endl; } - cerr << hex << setfill('0') << setw(2) << (uint32_t)((uint8_t *)data)[i] << " "; - ++counter; - } - cerr << endl; - } -} - -string Common::GetTLSH(const uint8_t *data, size_t len){ - Tlsh tlsh; - tlsh.update(data, len); - tlsh.final(); - string result = "T1" + string(tlsh.getHash()); - if (result == "T1"){ - return ""; - } - return result; -} - -string Common::GetFileTLSH(const char *file_path){ - FILE *inp; - uint8_t buf[8192]; - Tlsh tlsh; - size_t bread; - string ret; - inp = fopen(file_path, "rb"); - if(!inp){ - throw std::runtime_error(strerror(errno)); - } - while((bread = fread(buf, 1, sizeof(buf), inp)) > 0){ - tlsh.update(buf, bread); - } - if(errno != 0) { - throw std::runtime_error(strerror(errno)); - } - tlsh.final(); - fclose(inp); - ret = tlsh.getHash(); - return ret; -} - -string Common::GetFileSHA256(char *file_path){ - FILE *inp; - SHA256_CTX ctx; - uint8_t buf[8192]; - size_t bread; - uint8_t hash[SHA256_BLOCK_SIZE]; - inp = fopen(file_path, "rb"); - if(!inp){ - throw std::runtime_error(strerror(errno)); - } - sha256_init(&ctx); - while((bread = fread(buf, 1, sizeof(buf), inp)) > 0){ - sha256_update(&ctx, buf, bread); - } - if(errno != 0) { - throw std::runtime_error(strerror(errno)); - } - sha256_final(&ctx, hash); - fclose(inp); - return RemoveSpaces(HexdumpBE(&hash, SHA256_BLOCK_SIZE)); -} - -string Common::Wildcards(uint count){ - stringstream s; - s << ""; - for (uint i = 0; i < count; i++){ - s << "?? "; - } - return TrimRight(s.str()); -} - -string Common::GetSHA256(const uint8_t *data, size_t len){ - uint8_t hash[SHA256_BLOCK_SIZE]; - SHA256_CTX ctx; - sha256_init(&ctx); - sha256_update(&ctx, data, len); - sha256_final(&ctx, hash); - return RemoveSpaces(HexdumpBE(&hash, SHA256_BLOCK_SIZE)); -} - -string Common::SHA256(char *trait){ - uint8_t hash[SHA256_BLOCK_SIZE]; - SHA256_CTX ctx; - sha256_init(&ctx); - sha256_update(&ctx, (uint8_t *)trait, strlen(trait)); - sha256_final(&ctx, hash); - return RemoveSpaces(HexdumpBE(&hash, SHA256_BLOCK_SIZE)); -} - -vector Common::TraitToChar(string trait){ - trait = RemoveSpaces(RemoveWildcards(trait)); - vector bytes; - for (size_t i = 0; i < trait.length(); i = i + 2){ - const char *s_byte = trait.substr(i, 2).c_str(); - unsigned char byte = (char)strtol(s_byte, NULL, 16); - bytes.push_back(byte); - } - return bytes; -} - -float Common::Entropy(string trait){ - vector bytes = TraitToChar(trait); - float result = 0; - vector frequencies(256); - for (char c : bytes){ - frequencies[static_cast(c)]++; - } - for (auto count : frequencies){ - if(count > 0){ - float freq = static_cast( count ) / bytes.size(); - result -= freq * log2(freq); - } - } - return result; -} - -string Common::RemoveWildcards(string trait){ - string::iterator end_pos = remove(trait.begin(), trait.end(), '?'); - trait.erase(end_pos, trait.end()); - return trait; -} - -uint Common::GetByteSize(string s){ - return RemoveSpaces(s).length() / 2; -} - -string Common::RemoveSpaces(string s){ - string::iterator end_pos = remove(s.begin(), s.end(), ' '); - s.erase(end_pos, s.end()); - return s; -} - -string Common::WildcardTrait(string trait, string bytes){ - int count = bytes.length(); - for(int i = 0; i < count - 2; i = i + 3){ - bytes.erase(bytes.length() - 3); - size_t index = trait.find(bytes, 0); - if (index != string::npos){ - for (size_t j = index; j < trait.length(); j = j + 3){ - trait.replace(j, 2, "??"); - } - break; - } - } - return TrimRight(trait); -} - -string Common::HexdumpBE(const void *data, size_t size){ - stringstream bytes; - bytes << ""; - const unsigned char *local_pc = (const unsigned char *)data; - for (size_t i = 0; i < size; i++){ - bytes << hex << setfill('0') << setw(2) << (uint32_t)local_pc[i] << " "; - } - return TrimRight(bytes.str()); -} - -string Common::TraitToTLSH(string trait){ - const vector data = TraitToData(trait); - if (data.size() < 50){ - return ""; - } - return GetTLSH((uint8_t *)&data[0], data.size()); -} - -vector Common::TraitToData(string trait){ - trait = RemoveSpaces(RemoveWildcards(trait)); - vector bytes; - for (size_t i = 0; i < trait.length(); i = i + 2){ - const char *s_byte = trait.substr(i, 2).c_str(); - unsigned char byte = (char)strtol(s_byte, NULL, 16); - bytes.push_back(byte); - } - return bytes; -} - -string Common::TrimRight(const string &s){ - const string whitespace = " \n\r\t\f\v"; - size_t end = s.find_last_not_of(whitespace); - return (end == std::string::npos) ? "" : s.substr(0, end + 1); -} - -void Common::Hexdump(const char * desc, const void * addr, const int len){ - int i; - unsigned char buff[17]; - const unsigned char * pc = (const unsigned char *)addr; - if (desc != NULL) - printf ("%s:\n", desc); - if (len == 0) { - printf(" ZERO LENGTH\n"); - return; - } - else if (len < 0) { - printf(" NEGATIVE LENGTH: %d\n", len); - return; - } - for (i = 0; i < len; i++) { - if ((i % 16) == 0) { - if (i != 0) - printf (" %s\n", buff); - printf (" %04x ", i); - } - printf (" %02x", pc[i]); - if ((pc[i] < 0x20) || (pc[i] > 0x7e)) - buff[i % 16] = '.'; - else - buff[i % 16] = pc[i]; - buff[(i % 16) + 1] = '\0'; - } - while ((i % 16) != 0) { - printf (" "); - i++; - } - printf (" %s\n", buff); -} - -TimedCode::TimedCode(const char *tag) { - print_tag = tag; - start = std::chrono::steady_clock::now(); -} - -void TimedCode::Print() { - std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); - - int64_t start_time = std::chrono::time_point_cast(start).time_since_epoch().count(); - int64_t end_time = std::chrono::time_point_cast(end).time_since_epoch().count(); - - int64_t diff = end_time - start_time; - int64_t diff_s = diff / 1000000; - int64_t diff_ms = diff / 1000 - diff_s * 1000; - int64_t diff_us = diff - diff_s * 1000000 - diff_ms * 1000; - - cerr << "TimedCode: '" << print_tag << "': " << diff_s << " s " - << diff_ms << " ms " << diff_us << " us" << endl; -} diff --git a/src/controlflow/allepair.rs b/src/controlflow/allepair.rs new file mode 100644 index 00000000..49475289 --- /dev/null +++ b/src/controlflow/allepair.rs @@ -0,0 +1,15 @@ +use crate::controlflow::Gene; + +#[allow(dead_code)] +#[derive(Debug, Clone, Copy)] +pub struct AllelePair { + pub high: Gene, + pub low: Gene, +} + +#[allow(dead_code)] +impl AllelePair { + pub fn to_string(&self) -> String { + format!("{}{}", self.high.to_char(), self.low.to_char()) + } +} diff --git a/src/controlflow/attribute.rs b/src/controlflow/attribute.rs new file mode 100644 index 00000000..2f2b6b32 --- /dev/null +++ b/src/controlflow/attribute.rs @@ -0,0 +1,74 @@ +use serde_json::json; +use crate::formats::file::FileJson; +use crate::controlflow::SymbolJson; +use crate::controlflow::TagJson; +use serde_json::Value; +use std::io::Error; + +#[derive(Clone)] +pub enum Attribute { + File(FileJson), + Symbol(SymbolJson), + Tag(TagJson), +} + +impl Attribute { + pub fn to_json_value(&self) -> serde_json::Value { + match self { + Attribute::File(file_json) => serde_json::to_value(file_json) + .unwrap_or(json!({})), + Attribute::Symbol(symbol_json) => serde_json::to_value(symbol_json) + .unwrap_or(json!({})), + Attribute::Tag(tag_json) => serde_json::to_value(tag_json) + .unwrap_or(json!({})), + } + } +} + +#[derive(Clone)] +pub struct Attributes { + values: Vec, +} + +impl Attributes { + pub fn new() -> Self { + Self { + values: Vec::::new(), + } + } + + pub fn push(&mut self, attribute: Attribute) { + self.values.push(attribute); + } + + pub fn pop(&mut self) -> Option { + self.values.pop() + } + + pub fn len(&self) -> usize { + self.values.len() + } + + pub fn process(&self) -> Value { + let json_list: Vec = self + .values + .iter() + .map(|attribute| attribute.to_json_value()) + .collect(); + json!(json_list) + } + + pub fn json(&self) -> Result { + let raw = self.process(); + let result = serde_json::to_string(&raw)?; + Ok(result) + } + + #[allow(dead_code)] + pub fn print(&self) { + if let Ok(json) = self.json() { + println!("{}", json); + } + } + +} diff --git a/src/controlflow/block.rs b/src/controlflow/block.rs new file mode 100644 index 00000000..b676d5fa --- /dev/null +++ b/src/controlflow/block.rs @@ -0,0 +1,400 @@ +use crate::Architecture; +use crate::controlflow::instruction::Instruction; +use serde::{Deserialize, Serialize}; +use serde_json; +use serde_json::Value; +use std::collections::BTreeMap; +use std::collections::BTreeSet; +use std::io::Error; +use std::io::ErrorKind; +use crate::binary::Binary; +use crate::controlflow::graph::Graph; +use crate::controlflow::Chromosome; +use crate::controlflow::ChromosomeJson; +use crate::hashing::SHA256; +use crate::hashing::TLSH; +use crate::hashing::MinHash32; +use crate::controlflow::Attributes; + +/// Represents the JSON-serializable structure of a control flow block. +#[derive(Serialize, Deserialize)] +pub struct BlockJson { + /// The type of this entity, always `"block"`. + #[serde(rename = "type")] + pub type_: String, + /// The architecture of the block. + pub architecture: String, + /// The starting address of the block. + pub address: u64, + /// The address of the next sequential block, if any. + pub next: Option, + /// A set of addresses this block may branch or jump to. + pub to: BTreeSet, + /// The number of edges (connections) this block has. + pub edges: usize, + /// Indicates whether this block starts with a function prologue. + pub prologue: bool, + /// Indicates whether this block contains a conditional instruction. + pub conditional: bool, + /// The chromosome of the block in JSON format. + pub chromosome: ChromosomeJson, + /// The size of the block in bytes. + pub size: usize, + /// The raw bytes of the block in hexadecimal format. + pub bytes: String, + /// A map of function addresses related to this block. + pub functions: BTreeMap, + /// The number of instructions in this block. + pub number_of_instructions: usize, + /// The entropy of the block, if enabled. + pub entropy: Option, + /// The SHA-256 hash of the block, if enabled. + pub sha256: Option, + /// The MinHash of the block, if enabled. + pub minhash: Option, + /// The TLSH of the block, if enabled. + pub tlsh: Option, + /// Indicates whether the block is contiguous. + pub contiguous: bool, + /// Attributes + pub attributes: Option, +} + +/// Represents a control flow block within a graph. +#[derive(Clone)] +pub struct Block <'block>{ + /// The starting address of the block. + pub address: u64, + /// The control flow graph this block belongs to. + pub cfg: &'block Graph, + /// The terminating instruction of the block. + pub terminator: Instruction, +} + +impl<'block> Block<'block> { + /// Creates a new `Block` instance for the given address in the control flow graph. + /// + /// # Arguments + /// + /// * `address` - The starting address of the block. + /// * `cfg` - A reference to the control flow graph the block belongs to. + /// + /// # Returns + /// + /// Returns `Ok(Block)` if the block is valid and contiguous; otherwise, + /// returns an `Err` with an appropriate error message. + pub fn new(address: u64, cfg: &'block Graph) -> Result { + + if !cfg.blocks.is_valid(address) { + return Err(Error::new(ErrorKind::Other, format!("Block -> 0x{:x}: is not valid", address))); + } + + if !cfg.is_instruction_address(address) { + return Err(Error::new(ErrorKind::Other, format!("Instruction -> 0x{:x}: is not valid", address))); + } + + let mut terminator: Option = None; + + let previous_address: Option = None; + for entry in cfg.listing.range(address..){ + let instruction = entry.value(); + if let Some(prev_addr) = previous_address{ + if instruction.address != prev_addr { + return Err(Error::new(ErrorKind::Other, format!("Block -> 0x{:x}: is not contiguous", address))); + } + } + if instruction.is_jump + || instruction.is_trap + || instruction.is_return + || (address != instruction.address && instruction.is_block_start) { + terminator = Some(instruction.clone()); + break; + } + } + + if terminator.is_none() { + return Err(Error::new(ErrorKind::Other, format!("Block -> 0x{:x}: has no end instruction", address))); + } + + return Ok(Self { + address: address, + cfg: cfg, + terminator: terminator.unwrap(), + }); + } + + #[allow(dead_code)] + /// Get the architecture of the block. + pub fn architecture(&self) -> Architecture { + self.cfg.architecture + } + + /// Prints the JSON representation of the block to standard output. + #[allow(dead_code)] + pub fn print(&self) { + if let Ok(json) = self.json() { + println!("{}", json); + } + } + + /// Converts the block into a JSON string representation. + /// + /// # Returns + /// + /// Returns `Ok(String)` containing the JSON representation, or an `Err` if serialization fails. + pub fn json(&self) -> Result { + let raw = self.process(); + let result = serde_json::to_string(&raw)?; + Ok(result) + } + + /// Converts the block into a JSON string representation including `Attributes`. + /// + /// # Returns + /// + /// Returns `Ok(String)` containing the JSON representation, or an `Err` if serialization fails. + pub fn json_with_attributes(&self, attributes: Attributes) -> Result { + let raw = self.process_with_attributes(attributes); + let result = serde_json::to_string(&raw)?; + Ok(result) + } + + /// Processes the block into its JSON-serializable representation. + /// + /// # Returns + /// + /// Returns a `BlockJson` instance containing the block's metadata and related information. + pub fn process(&self) -> BlockJson { + BlockJson { + type_: "block".to_string(), + address: self.address, + architecture: self.architecture().to_string(), + next: self.next(), + to: self.terminator.to(), + edges: self.edges(), + chromosome: self.chromosome(), + prologue: self.is_prologue(), + conditional: self.terminator.is_conditional, + size: self.size(), + bytes: Binary::to_hex(&self.bytes()), + number_of_instructions: self.number_of_instructions(), + functions: self.functions(), + entropy: self.entropy(), + sha256: self.sha256(), + minhash: self.minhash(), + tlsh: self.tlsh(), + contiguous: true, + attributes: None, + } + } + + /// Processes the block into its JSON-serializable representation including `Attributes`. + /// + /// # Returns + /// + /// Returns a `BlockJson` instance containing the block's metadata and `Attributes`. + pub fn process_with_attributes(&self, attributes: Attributes) -> BlockJson { + let mut result = self.process(); + result.attributes = Some(attributes.process()); + return result; + } + + /// Determines whether the block starts with a function prologue. + /// + /// # Returns + /// + /// Returns `true` if the block starts with a prologue; otherwise, `false`. + pub fn is_prologue(&self) -> bool { + if let Some(entry) = self.cfg.listing.get(&self.address) { + return entry.value().is_prologue; + } + return false; + } + + /// Retrieves the number of edges (connections) this block has. + /// + /// # Returns + /// + /// Returns the number of edges as a `usize`. + pub fn edges(&self) -> usize { + return self.terminator.edges; + } + + /// Retrieves the address of the next sequential block, if any. + /// + /// # Returns + /// + /// Returns `Some(u64)` containing the address of the next block if it is + /// conditional or has specific ending conditions. Returns `None` otherwise. + pub fn next(&self) -> Option { + if !self.terminator.is_conditional { return None; } + if self.terminator.address == self.address { return None; } + if self.terminator.is_block_start { return Some(self.terminator.address); } + if self.terminator.is_return { return None; } + if self.terminator.is_trap { return None; } + if self.terminator.is_block_start { + return Some(self.terminator.address); + } + self.terminator.next() + } + + /// Retrieves the set of addresses this block may jump or branch to. + /// + /// # Returns + /// + /// Returns a `BTreeSet` containing the target addresses. + pub fn to(&self) -> BTreeSet { + self.terminator.to() + } + + pub fn blocks(&self) -> BTreeSet { + let mut result = BTreeSet::new(); + for item in self.to().iter().map(|ref_multi| *ref_multi).chain(self.next()) { + result.insert(item); + } + result + } + + /// Generates a signature for the block using its address range and control flow graph. + /// + /// # Returns + /// + /// Returns a `SignatureJson` representing the block's signature. + pub fn chromosome(&self) -> ChromosomeJson { + Chromosome::new(self.pattern(), self.cfg.config.clone()).unwrap().process() + } + + /// Retrieves the pattern string representation of the chromosome. + /// + /// # Returns + /// + /// Returns a `Option` containing the pattern representation of the chromosome. + fn pattern(&self) -> String { + let mut result = String::new(); + for entry in self.cfg.listing.range(self.address..self.address + self.size() as u64) { + let instruction = entry.value(); + result += instruction.pattern.as_str(); + } + return result; + } + + /// Retrieves the function addresses associated with this block. + /// + /// # Returns + /// + /// Returns a `BTreeMap` where each key is an instruction address + /// and each value is the address of the function containing that instruction. + pub fn functions(&self) -> BTreeMap { + let mut result = BTreeMap::::new(); + for entry in self.cfg.listing.range(self.address..self.end()){ + let instruction = entry.value(); + for function_address in instruction.functions.clone() { + result.insert(instruction.address, function_address); + } + } + return result; + } + + /// Computes the entropy of the block's bytes, if enabled. + /// + /// # Returns + /// + /// Returns `Some(f64)` containing the entropy, or `None` if entropy calculation is disabled. + pub fn entropy(&self) -> Option { + if !self.cfg.config.blocks.heuristics.entropy.enabled { return None; } + return Binary::entropy(&self.bytes()); + } + + /// Computes the TLSH of the block's bytes, if enabled. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the TLSH, or `None` if TLSH is disabled or the block size is too small. + pub fn tlsh(&self) -> Option { + if !self.cfg.config.blocks.hashing.tlsh.enabled { return None; } + return TLSH::new(&self.bytes(), self.cfg.config.blocks.hashing.tlsh.minimum_byte_size).hexdigest(); + } + + /// Computes the MinHash of the block's bytes, if enabled. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the MinHash, or `None` if MinHash is disabled or the block's size exceeds the configured maximum. + pub fn minhash(&self) -> Option { + if !self.cfg.config.blocks.hashing.minhash.enabled { return None; } + if self.bytes().len() > self.cfg.config.blocks.hashing.minhash.maximum_byte_size { return None; } + return MinHash32::new( + &self.bytes(), + self.cfg.config.blocks.hashing.minhash.number_of_hashes, + self.cfg.config.blocks.hashing.minhash.shingle_size, + self.cfg.config.blocks.hashing.minhash.seed + ).hexdigest(); + } + + /// Computes the SHA-256 hash of the block's bytes, if enabled. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the hash, or `None` if SHA-256 is disabled. + pub fn sha256(&self) -> Option { + if !self.cfg.config.blocks.hashing.sha256.enabled { return None; } + return SHA256::new(&self.bytes()).hexdigest(); + } + + /// Retrieves the size of the block in bytes. + /// + /// # Returns + /// + /// Returns the size as a `usize`. + pub fn size(&self) -> usize { + (self.end() - self.address) as usize + } + + /// Retrieves the raw bytes of the block. + /// + /// # Returns + /// + /// Returns a `Vec` containing the bytes of the block. + pub fn bytes(&self) -> Vec { + let mut result = Vec::::new(); + for entry in self.cfg.listing.range(self.address..self.end()){ + let instruction = entry.value(); + result.extend(instruction.bytes.clone()); + } + return result; + } + + /// Counts the number of instructions in the block. + /// + /// # Returns + /// + /// Returns the number of instructions as a `usize`. + pub fn number_of_instructions(&self) -> usize { + let mut result: usize = 0; + for _ in self.cfg.listing.range(self.address..=self.terminator.address){ + result += 1; + } + return result; + } + + /// Retrieves the address of the block's last instruction. + /// + /// # Returns + /// + /// Returns the address as a `u64`. + #[allow(dead_code)] + pub fn end(&self) -> u64 { + if self.address == self.terminator.address { return self.terminator.address + self.terminator.size() as u64; } + if self.terminator.is_block_start { + return self.terminator.address; + } + if self.terminator.is_return { + return self.terminator.address + self.terminator.size() as u64; + } + if let Some(next)= self.next() { + return next; + } + return self.terminator.address; + } + +} diff --git a/src/controlflow/chromosome.rs b/src/controlflow/chromosome.rs new file mode 100644 index 00000000..16df3936 --- /dev/null +++ b/src/controlflow/chromosome.rs @@ -0,0 +1,207 @@ +use serde::{Deserialize, Serialize}; +use serde_json; +use std::io::Error; +use std::io::ErrorKind; +use crate::binary::Binary; +use crate::hashing::SHA256; +use crate::hashing::TLSH; +use crate::hashing::MinHash32; +use crate::controlflow::AllelePair; +use crate::controlflow::Gene; +use crate::Config; + +/// Represents a JSON-serializable structure containing metadata about a chromosome. +#[derive(Serialize, Deserialize)] +pub struct ChromosomeJson { + /// The raw pattern string of the chromosome. + pub pattern: String, + /// The feature vector extracted from the chromosome. + pub feature: Vec, + /// The entropy of the normalized chromosome, if enabled. + pub entropy: Option, + /// The SHA-256 hash of the normalized chromosome, if enabled. + pub sha256: Option, + /// The MinHash of the normalized chromosome, if enabled. + pub minhash: Option, + /// The TLSH (Locality Sensitive Hash) of the normalized chromosome, if enabled. + pub tlsh: Option, +} + +/// Represents a chromosome within a control flow graph. +pub struct Chromosome { + pub pairs: Vec, + config: Config, +} + +impl Chromosome { + /// Creates a new `Chromosome` instance for a specified address range within a control flow graph. + /// + /// # Arguments + /// + /// * `pattern` - The chromosome pattern string. + /// * `config` - The configuraiton. + /// + /// # Returns + /// + /// Returns `Result`. + pub fn new(pattern: String, config: Config) -> Result { + let pairs = Self::parse_pairs(pattern)?; + Ok(Self { + pairs: pairs, + config: config, + }) + } + + #[allow(dead_code)] + fn parse_pairs(pattern: String) -> Result, Error> { + if pattern.len() % 2 != 0 { + return Err(Error::new(ErrorKind::InvalidData, format!("pattern length must be even"))); + } + let mut parsed = Vec::new(); + let chars: Vec = pattern.chars().collect(); + for chunk in chars.chunks(2) { + let high = Self::parse_gene(chunk[0])?; + let low = Self::parse_gene(chunk[1])?; + parsed.push(AllelePair { high, low }); + } + Ok(parsed) + } + + fn parse_gene(c: char) -> Result { + match c { + '?' => Ok(Gene::Wildcard), + _ if c.is_ascii_hexdigit() => { + let value = u8::from_str_radix(&c.to_string(), 16) + .map_err(|_| Error::new(ErrorKind::InvalidData, format!("invalid genene hexidecimal value")))?; + Ok(Gene::Value(value)) + } + _ => Err(Error::new(ErrorKind::InvalidData, "invalid character in gene")), + } + } + + pub fn pattern(&self) -> String { + let mut result = String::new(); + for pair in &self.pairs { + result += &pair.to_string(); + } + result + } + + /// Retrieves the raw bytes within the address range of the chromosome. + /// + /// # Returns + /// + /// Returns a `Vec` containing the raw bytes of the chromosome. + pub fn normalized(&self) -> Vec { + let mut result = Vec::new(); + let mut temp_byte: Option = None; + for pair in &self.pairs { + if let Gene::Value(high) = pair.high { + if let Some(low) = temp_byte { + result.push((low << 4) | high); + temp_byte = None; + } else { + temp_byte = Some(high); + } + } + if let Gene::Value(low) = pair.low { + if let Some(high) = temp_byte { + result.push((high << 4) | low); + temp_byte = None; + } else { + temp_byte = Some(low); + } + } + } + result + } + + /// Extracts the feature vector from the normalized chromosome, if enabled. + /// + /// # Returns + /// + /// Returns a `Vec` containing the feature vector, or an empty vector if feature extraction is disabled. + pub fn feature(&self) -> Vec { + if !self.config.chromosomes.heuristics.features.enabled { return Vec::::new(); } + self.normalized() + .iter() + .flat_map(|byte| vec![((byte & 0xf0) >> 4) as u8, (byte & 0x0f) as u8]) + .collect() + } + + /// Computes the TLSH (Locality Sensitive Hash) of the normalized chromosome, if enabled. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the TLSH, or `None` if TLSH is disabled. + pub fn tlsh(&self) -> Option { + if !self.config.chromosomes.hashing.tlsh.enabled { return None; } + return TLSH::new(&self.normalized(), self.config.chromosomes.hashing.tlsh.minimum_byte_size).hexdigest(); + } + + /// Computes the MinHash of the normalized signature, if enabled. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the MinHash, or `None` if MinHash is disabled. + #[allow(dead_code)] + pub fn minhash(&self) -> Option { + if !self.config.chromosomes.hashing.minhash.enabled { return None; } + if self.normalized().len() > self.config.chromosomes.hashing.minhash.maximum_byte_size { return None; } + return MinHash32::new( + &self.normalized(), + self.config.chromosomes.hashing.minhash.number_of_hashes, + self.config.chromosomes.hashing.minhash.shingle_size, + self.config.chromosomes.hashing.minhash.seed).hexdigest(); + } + + /// Computes the SHA-256 hash of the normalized chromosome, if enabled. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the SHA-256 hash, or `None` if SHA-256 is disabled. + pub fn sha256(&self) -> Option { + if !self.config.chromosomes.hashing.sha256.enabled { return None; } + SHA256::new(&self.normalized()).hexdigest() + } + + /// Computes the entropy of the normalized chromosome, if enabled. + /// + /// # Returns + /// + /// Returns `Some(f64)` containing the entropy, or `None` if entropy calculation is disabled. + pub fn entropy(&self) -> Option { + if !self.config.chromosomes.heuristics.entropy.enabled { return None; } + Binary::entropy(&self.normalized()) + } + + /// Processes the chromosome into its JSON-serializable representation. + /// + /// # Returns + /// + /// Returns a `ChromosomeJson` struct containing metadata about the chromosome. + pub fn process(&self) -> ChromosomeJson { + ChromosomeJson { + pattern: self.pattern(), + feature: self.feature(), + sha256: self.sha256(), + entropy: self.entropy(), + minhash: self.minhash(), + tlsh: self.tlsh(), + } + } + + /// Converts the signature metadata into a JSON string representation. + /// + /// # Returns + /// + /// Returns `Ok(String)` containing the JSON representation of the signature, + /// or an `Err` if serialization fails. + #[allow(dead_code)] + pub fn json(&self) -> Result { + let raw = self.process(); + let result = serde_json::to_string(&raw)?; + Ok(result) + } + +} diff --git a/src/controlflow/function.rs b/src/controlflow/function.rs new file mode 100644 index 00000000..599d5f65 --- /dev/null +++ b/src/controlflow/function.rs @@ -0,0 +1,458 @@ + +use crate::Architecture; +use serde::{Deserialize, Serialize}; +use serde_json; +use std::collections::BTreeMap; +use std::collections::BTreeSet; +use std::io::Error; +use std::io::ErrorKind; +use crate::binary::Binary; +use crate::controlflow::Graph; +use crate::controlflow::GraphQueue; +use crate::controlflow::Block; +use crate::controlflow::Chromosome; +use crate::controlflow::ChromosomeJson; +use crate::controlflow::Attributes; +use crate::hashing::SHA256; +use crate::hashing::TLSH; +use crate::hashing::MinHash32; +use serde_json::Value; + +/// Represents a JSON-serializable structure containing metadata about a function. +#[derive(Serialize, Deserialize)] +pub struct FunctionJson { + /// The type of this entity, typically `"function"`. + #[serde(rename = "type")] + pub type_: String, + /// The architecture of the function. + pub architecture: String, + /// The starting address of the function. + pub address: u64, + /// The number of edges (connections) in the function. + pub edges: usize, + /// Indicates whether this function starts with a prologue. + pub prologue: bool, + /// The chromosome of the function in JSON format. + pub chromosome: Option, + /// The size of the function in bytes, if available. + pub size: usize, + /// The raw bytes of the function in hexadecimal format, if available. + pub bytes: Option, + /// A map of functions associated with the function. + pub functions: BTreeMap, + /// The set of blocks contained within the function. + pub blocks: BTreeSet, + /// The number of instructions in the function. + pub number_of_instructions: usize, + /// The cyclomatic complexity of the function. + pub cyclomatic_complexity: usize, + /// Average Instructions per Block + pub average_instructions_per_block: f64, + /// The entropy of the function, if enabled. + pub entropy: Option, + /// The SHA-256 hash of the function, if enabled. + pub sha256: Option, + /// The MinHash of the function, if enabled. + pub minhash: Option, + /// The TLSH of the function, if enabled. + pub tlsh: Option, + /// Indicates whether the function is contiguous. + pub contiguous: bool, + /// Attributes + pub attributes: Option, +} + +/// Represents a control flow function within a graph. +#[derive(Clone)] +pub struct Function <'function>{ + /// The starting address of the function. + pub address: u64, + /// The control flow graph this function belongs to. + pub cfg: &'function Graph, + /// The blocks that make up the function, mapped by their start addresses. + pub blocks: BTreeMap>, +} + +impl<'function> Function<'function> { + /// Creates a new `Function` instance for the given address in the control flow graph. + /// + /// # Arguments + /// + /// * `address` - The starting address of the function. + /// * `cfg` - A reference to the control flow graph the function belongs to. + /// + /// # Returns + /// + /// Returns `Ok(Function)` if the function is valid; otherwise, + /// returns an `Err` with an appropriate error message. + pub fn new(address: u64, cfg: &'function Graph) -> Result { + + if !cfg.functions.is_valid(address) { + return Err(Error::new(ErrorKind::Other, format!("Function -> 0x{:x}: is not valid", address))); + } + + if !cfg.is_instruction_address(address) { + return Err(Error::new(ErrorKind::Other, format!("Instruction -> 0x{:x}: is not valid", address))); + } + + let mut blocks = BTreeMap::::new(); + + let mut queue = GraphQueue::new(); + + queue.enqueue(address); + + while let Some(block_address) = queue.dequeue() { + queue.insert_processed(block_address); + if cfg.blocks.is_invalid(block_address) { + return Err(Error::new(ErrorKind::Other, format!("Function -> 0x{:x} -> Block -> 0x{:x}: is invalid", address, block_address))); + } + if let Ok(block) = Block::new(block_address, &cfg) { + queue.enqueue_extend(block.blocks()); + blocks.insert(block_address, block); + } + } + + return Ok(Self { + address: address, + cfg: cfg, + blocks: blocks, + }); + } + + #[allow(dead_code)] + pub fn architecture(&self) -> Architecture { + self.cfg.architecture + } + + /// Calculates the average instructions per block in the function. + /// + /// # Returns + /// + /// Returns a `usize` representing the average instrucitons per block. + pub fn average_instructions_per_block(&self) -> f64 { + self.number_of_instructions() as f64 / self.blocks.len() as f64 + } + + /// Calculates the cyclomatic complexity of the function. + /// + /// # Returns + /// + /// Returns a `usize` representing the cyclomatic complexity. + pub fn cyclomatic_complexity(&self) -> usize { + let nodes = self.blocks().len(); + let edges = self.edges(); + let components = 1; + edges - nodes + 2 * components + } + + /// Processes the function into its JSON-serializable representation. + /// + /// # Returns + /// + /// Returns a `FunctionJson` struct containing metadata about the function. + pub fn process(&self) -> FunctionJson { + FunctionJson { + address: self.address, + type_: "function".to_string(), + edges: self.edges(), + prologue: self.is_prologue(), + chromosome: self.chromosome(), + bytes: self.bytes_to_hex(), + size: self.size(), + functions: self.functions(), + blocks: self.blocks(), + number_of_instructions: self.number_of_instructions(), + cyclomatic_complexity: self.cyclomatic_complexity(), + average_instructions_per_block: self.average_instructions_per_block(), + entropy: self.entropy(), + sha256: self.sha256(), + minhash: self.minhash(), + tlsh: self.tlsh(), + contiguous: self.is_contiguous(), + architecture: self.architecture().to_string(), + attributes: None, + } + } + + /// Processes the function into its JSON-serializable representation including `Attributes` + /// + /// # Returns + /// + /// Returns a `FunctionJson` instance containing the function's metadata and `Attributes`. + pub fn process_with_attributes(&self, attributes: Attributes) -> FunctionJson { + let mut result = self.process(); + result.attributes = Some(attributes.process()); + return result; + } + + /// Prints the JSON representation of the function to standard output. + #[allow(dead_code)] + pub fn print(&self) { + if let Ok(json) = self.json() { + println!("{}", json); + } + } + + /// Converts the function metadata into a JSON string representation. + /// + /// # Returns + /// + /// Returns `Ok(String)` containing the JSON representation, or an `Err` if serialization fails. + pub fn json(&self) -> Result { + let raw = self.process(); + let result = serde_json::to_string(&raw)?; + Ok(result) + } + + /// Converts the function metadata into a JSON string representation including `Attributes`. + /// + /// # Returns + /// + /// Returns `Ok(String)` containing the JSON representation, or an `Err` if serialization fails. + pub fn json_with_attributes(&self, attributes: Attributes) -> Result { + let raw = self.process_with_attributes(attributes); + let result = serde_json::to_string(&raw)?; + Ok(result) + } + + /// Generates the function's chromosome if the function is contiguous. + /// + /// # Returns + /// + /// Returns `Some(ChromosomeJson)` if the function is contiguous; otherwise, `None`. + pub fn chromosome(&self) -> Option { + if !self.is_contiguous() { return None; } + let bytes = self.bytes(); + if bytes.is_none() { return None; } + let pattern = self.pattern()?; + let chromosome = Chromosome::new(pattern, self.cfg.config.clone()).ok()?; + return Some(chromosome.process()); + } + + /// Retrieves the pattern string representation of the chromosome. + /// + /// # Returns + /// + /// Returns a `Option` containing the pattern representation of the chromosome. + fn pattern(&self) -> Option { + if !self.is_contiguous() { return None; } + let mut result = String::new(); + for entry in self.cfg.listing.range(self.address..self.address + self.size() as u64) { + let instruction = entry.value(); + result += instruction.pattern.as_str(); + } + return Some(result); + } + + /// Retrieves the total number of instructions in the function. + /// + /// # Returns + /// + /// Returns the number of instructions as a `usize`. + pub fn number_of_instructions(&self) -> usize { + let mut result: usize = 0; + for (_, block) in &self.blocks { + result += block.number_of_instructions(); + } + result + } + + /// Indicates whether this function starts with a prologue. + /// + /// # Returns + /// + /// Returns `true` if the function starts with a prologue; otherwise, `false`. + pub fn is_prologue(&self) -> bool { + if let Some((_, block)) = self.blocks.iter().next() { + return block.is_prologue(); + } + return false; + } + + /// Retrieves the set of block addresses in the function. + /// + /// # Returns + /// + /// Returns a `BTreeSet` containing the addresses of all blocks in the function. + pub fn blocks(&self) -> BTreeSet { + let mut result = BTreeSet::::new(); + for (block_address, _) in &self.blocks { + result.insert(*block_address); + } + result + } + + /// Retrieves the number of edges (connections) in the function. + /// + /// # Returns + /// + /// Returns the number of edges as a `usize`. + pub fn edges(&self) -> usize { + let mut result: usize = 0; + for (_, block) in &self.blocks { + result += block.edges(); + } + result + } + + /// Converts the function's bytes to a hexadecimal string, if available. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the hexadecimal representation of the bytes, or `None` if unavailable. + fn bytes_to_hex(&self) -> Option { + if let Some(bytes) = self.bytes() { + return Some(Binary::to_hex(&bytes)); + } + return None; + } + + /// Retrieves the size of the function in bytes, if contiguous. + /// + /// # Returns + /// + /// Returns `Some(usize)` if the function is contiguous; otherwise, `None`. + pub fn size(&self) -> usize { + let mut result: usize = 0; + for (_, block) in &self.blocks { + result += block.size(); + } + result + } + + /// Retrieves the address of the function's last instruction, if contiguous. + /// + /// # Returns + /// + /// Returns `Some(u64)` containing the address, or `None` if the function is not contiguous. + pub fn end(&self) -> Option { + if !self.is_contiguous() { return None; } + if let Some((_, block)) = self.blocks.iter().last() { + return Some(block.end()); + } + None + } + + /// Retrieves the raw bytes of the function, if contiguous. + /// + /// # Returns + /// + /// Returns `Some(Vec)` containing the bytes, or `None` if the function is not contiguous. + pub fn bytes(&self) -> Option> { + if !self.is_contiguous() { return None; } + let mut bytes = Vec::::new(); + let mut block_previous_end: Option = None; + for (block_start_address, block) in &self.blocks { + bytes.extend(block.bytes()); + if block.terminator.is_return { break; } + if let Some(previous_end) = block_previous_end { + if previous_end != *block_start_address { + return None; + } + } + block_previous_end = Some(block.address + block.size() as u64); + } + Some(bytes) + } + + /// Computes the SHA-256 hash of the function's bytes, if enabled and contiguous. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the hash, or `None` if SHA-256 is disabled or the function is not contiguous. + pub fn sha256(&self) -> Option { + if !self.cfg.config.functions.hashing.sha256.enabled { return None; } + if !self.is_contiguous() { return None; } + if let Some(bytes) = self.bytes() { + return SHA256::new(&bytes).hexdigest(); + } + return None; + } + + /// Computes the entropy of the function's bytes, if enabled and contiguous. + /// + /// # Returns + /// + /// Returns `Some(f64)` containing the entropy, or `None` if entropy calculation is disabled or the function is not contiguous. + pub fn entropy(&self) -> Option { + if !self.cfg.config.functions.heuristics.entropy.enabled { return None; } + if self.is_contiguous() { + if let Some(bytes) = self.bytes() { + return Binary::entropy(&bytes); + } + return None; + } + let mut entropi = Vec::::new(); + for (_, block) in &self.blocks { + if let Some(entropy) = block.entropy() { + entropi.push(entropy); + } + } + if entropi.is_empty() { return Some(0.0); } + Some(entropi.iter().sum::() / entropi.len() as f64) + } + + /// Computes the TLSH of the function's bytes, if enabled and contiguous. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the TLSH, or `None` if TLSH is disabled or the function is not contiguous. + pub fn tlsh(&self) -> Option { + if !self.cfg.config.functions.hashing.tlsh.enabled { return None; } + if !self.is_contiguous() { return None; } + if let Some(bytes) = self.bytes() { + return TLSH::new(&bytes, self.cfg.config.functions.hashing.tlsh.minimum_byte_size).hexdigest(); + } + return None; + } + + /// Computes the MinHash of the function's bytes, if enabled and contiguous. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the MinHash, or `None` if MinHash is disabled or the function is not contiguous. + pub fn minhash(&self) -> Option { + if !self.cfg.config.functions.hashing.minhash.enabled { return None; } + if !self.is_contiguous() { return None; } + if let Some(bytes) = self.bytes() { + if bytes.len() > self.cfg.config.functions.hashing.minhash.maximum_byte_size { return None; } + return MinHash32::new( + &bytes, + self.cfg.config.functions.hashing.minhash.number_of_hashes, + self.cfg.config.functions.hashing.minhash.shingle_size, + self.cfg.config.functions.hashing.minhash.seed).hexdigest(); + } + return None; + } + + /// Retrieves the functions associated with this function. + /// + /// # Returns + /// + /// Returns a `BTreeMap` containing function addresses. + pub fn functions(&self) -> BTreeMap { + let mut result = BTreeMap::::new(); + for (_, block) in &self.blocks { + result.extend(block.functions()); + } + result + } + + /// Checks whether the function is contiguous in memory. + /// + /// # Returns + /// + /// Returns `true` if the function is contiguous; otherwise, `false`. + pub fn is_contiguous(&self) -> bool { + let mut block_previous_end: Option = None; + for (block_start_address, block) in &self.blocks { + if let Some(previous_end) = block_previous_end { + if previous_end != *block_start_address { + return false; + } + } + block_previous_end = Some(block.address + block.size() as u64); + } + return true; + } +} diff --git a/src/controlflow/gene.rs b/src/controlflow/gene.rs new file mode 100644 index 00000000..3518978e --- /dev/null +++ b/src/controlflow/gene.rs @@ -0,0 +1,16 @@ +#[allow(dead_code)] +#[derive(Debug, Clone, Copy)] +pub enum Gene { + Wildcard, + Value(u8), +} + +#[allow(dead_code)] +impl Gene { + pub fn to_char(self) -> String { + match self { + Gene::Wildcard => "?".to_string(), + Gene::Value(v) => format!("{:x}", v), + } + } +} diff --git a/src/controlflow/graph.rs b/src/controlflow/graph.rs new file mode 100644 index 00000000..e884f041 --- /dev/null +++ b/src/controlflow/graph.rs @@ -0,0 +1,366 @@ +use std::collections::BTreeSet; +use crate::Architecture; +use crate::controlflow::Instruction; +use crossbeam::queue::SegQueue; +use crossbeam_skiplist::SkipMap; +use crossbeam_skiplist::SkipSet; +use crate::Config; + +/// Queue structure used within `Graph` for managing addresses in processing stages. +pub struct GraphQueue { + /// Queue of addresses to be processed. + pub queue: SegQueue, + /// Set of addresses that have been processed. + pub processed: SkipSet, + /// Set of valid addresses in the graph. + pub valid: SkipSet, + /// Set of invalid addresses in the graph. + pub invalid: SkipSet, +} + +impl Clone for GraphQueue { + /// Creates a clone of the `GraphQueue`, including all processed, valid, and invalid addresses. + fn clone(&self) -> Self { + let cloned_queue = SegQueue::new(); + let mut temp_queue = Vec::new(); + while let Some(item) = self.queue.pop() { + cloned_queue.push(item); + temp_queue.push(item); + } + for item in temp_queue { + self.queue.push(item); + } + let cloned_processed = SkipSet::new(); + for item in self.processed.iter() { + cloned_processed.insert(*item); + } + let cloned_valid = SkipSet::new(); + for item in self.valid.iter() { + cloned_valid.insert(*item); + } + let cloned_invalid = SkipSet::new(); + for item in self.invalid.iter() { + cloned_invalid.insert(*item); + } + GraphQueue { + queue: cloned_queue, + processed: cloned_processed, + valid: cloned_valid, + invalid: cloned_invalid, + } + } +} + +impl GraphQueue { + /// Creates a new, empty `GraphQueue` instance. + /// + /// # Returns + /// + /// Returns a new `GraphQueue` instance with empty sets and queues. + pub fn new() -> Self { + return Self { + queue: SegQueue::::new(), + processed: SkipSet::::new(), + valid: SkipSet::::new(), + invalid: SkipSet::::new(), + } + } + + /// Marks an address as invalid if it has not been marked as valid. + /// + /// # Arguments + /// + /// * `address` - The address to mark as invalid. + pub fn insert_invalid(&mut self, address: u64) { + if !self.is_invalid(address) { + if !self.is_valid(address) { + self.invalid.insert(address); + } + } + } + + /// Checks if an address is marked as invalid. + /// + /// # Returns + /// + /// Returns `true` if the address is invalid, otherwise `false`. + pub fn is_invalid(&self, address: u64) -> bool { + self.invalid.contains(&address) + } + + /// Retrieves a reference to the invalid address set. + /// + /// # Returns + /// + /// Returns a reference to the `SkipSet` containing invalid addresses. + #[allow(dead_code)] + pub fn invalid(&self) -> &SkipSet { + return &self.invalid; + } + + /// Retrieves a reference to the valid address set. + /// + /// # Returns + /// + /// Returns a reference to the `SkipSet` containing valid addresses. + pub fn valid(&self) -> &SkipSet { + return &self.valid; + } + + /// Collects valid addresses in a set + /// + /// # Returns + /// + /// Returns a `BTreeSet` containing valid addresses. + pub fn valid_addresses(&self) -> BTreeSet { + let mut result = BTreeSet::::new(); + for entry in self.valid() { + result.insert(*entry.value()); + } + result + } + + /// Collects invalid addresses in a set + /// + /// # Returns + /// + /// Returns a `BTreeSet` containing valid addresses. + pub fn invalid_addresses(&self) -> BTreeSet { + let mut result = BTreeSet::::new(); + for entry in self.invalid() { + result.insert(*entry.value()); + } + result + } + + /// Collects processed addresses in a set + /// + /// # Returns + /// + /// Returns a `BTreeSet` containing processed addresses. + pub fn processed_addresses(&self) -> BTreeSet { + let mut result = BTreeSet::::new(); + for entry in self.processed() { + result.insert(*entry.value()); + } + result + } + + /// Retrieves a reference to the processed address set. + /// + /// # Returns + /// + /// Returns a reference to the `SkipSet` containing processed addresses. + pub fn processed(&self) -> &SkipSet { + return &self.processed; + } + + /// Checks if an address is marked as valid. + /// + /// # Returns + /// + /// Returns `true` if the address is valid, otherwise `false`. + pub fn is_valid(&self, address: u64) -> bool { + self.valid.contains(&address) + } + + /// Marks an address as valid if it has been processed. + /// + /// # Arguments + /// + /// * `address` - The address to mark as valid. + pub fn insert_valid(&mut self, address: u64) { + if self.is_processed(address) { + self.valid.insert(address); + } + } + + /// Marks multiple addresses as processed. + /// + /// # Arguments + /// + /// * `addresses` - A set of addresses to mark as processed. + pub fn insert_processed_extend(&mut self, addresses: BTreeSet) { + for address in addresses { + self.insert_processed(address); + } + } + + /// Marks a single address as processed. + /// + /// # Arguments + /// + /// * `address` - The address to mark as processed. + pub fn insert_processed(&mut self, address: u64) { + self.processed.insert(address); + } + + /// Checks if an address has been processed. + /// + /// # Returns + /// + /// Returns `true` if the address is processed, otherwise `false`. + pub fn is_processed(&self, address: u64) -> bool { + self.processed.contains(&address) + } + + /// Adds multiple addresses to the processing queue. + /// + /// # Arguments + /// + /// * `addresses` - A set of addresses to enqueue. + pub fn enqueue_extend(&mut self, addresses: BTreeSet) { + for address in addresses { + self.enqueue(address); + } + } + + /// Adds an address to the processing queue if it hasn't been processed. + /// + /// # Returns + /// + /// Returns `true` if the address was enqueued, otherwise `false`. + pub fn enqueue(&mut self, address: u64) -> bool { + if self.is_processed(address) { return false; } + self.queue.push(address); + return true; + } + + /// Removes an address from the processing queue. + /// + /// # Returns + /// + /// Returns `Some(u64)` containing the dequeued address if available, otherwise `None`. + pub fn dequeue(&mut self) -> Option { + self.queue.pop() + } + + /// Removes all addresses from the processing queue. + /// + /// # Returns + /// + /// Returns a `BTreeSet` containing all dequeued addresses. + pub fn dequeue_all(&mut self) -> BTreeSet { + let mut set = BTreeSet::new(); + while let Some(address) = self.queue.pop() { + set.insert(address); + } + set + } +} + +/// Represents a control flow graph with instructions, blocks, and functions. +pub struct Graph { + /// The Instruction Architecture + pub architecture: Architecture, + /// A map of instruction addresses to `Instruction` instances. + pub listing: SkipMap, + /// Queue for managing basic blocks within the graph. + pub blocks: GraphQueue, + /// Queue for managing functions within the graph. + pub functions: GraphQueue, + /// Queue for managing instructions within the graph. + pub instructions: GraphQueue, + /// Configuration + pub config: Config, +} + +impl Graph { + /// Creates a new, empty `Graph` instance with default options. + /// + /// # Returns + /// + /// Returns a `Graph` instance with empty instructions, blocks, and functions. + #[allow(dead_code)] + pub fn new(architecture: Architecture, config: Config) -> Self { + return Self{ + architecture: architecture, + listing: SkipMap::::new(), + blocks: GraphQueue::new(), + functions: GraphQueue::new(), + instructions: GraphQueue::new(), + config: config, + }; + } + + pub fn instruction_addresses(&self) -> BTreeSet { + let mut result = BTreeSet::::new(); + for entry in &self.listing { + result.insert(*entry.key()); + } + result + } + + pub fn listing(&self) -> &SkipMap { + return &self.listing; + } + + pub fn insert_instruction(&mut self, instruction: Instruction) { + if !self.is_instruction_address(instruction.address) { + self.listing.insert(instruction.address, instruction); + } + } + + pub fn update_instruction(&mut self, instruction: Instruction) { + if !self.is_instruction_address(instruction.address) { return } + self.listing.insert(instruction.address, instruction); + } + + pub fn is_instruction_address(&self, address: u64) -> bool { + self.listing.contains_key(&address) + } + + pub fn get_instruction(&self, address: u64) -> Option { + self.listing.get(&address).map(|entry|entry.value().clone()) + } + pub fn absorb(&mut self, graph: &mut Graph) { + + for entry in graph.listing() { + self.insert_instruction(entry.value().clone()); + } + + for entry in graph.instructions.processed() { + self.instructions.insert_processed(entry.value().clone()); + } + + self.instructions.enqueue_extend(graph.instructions.dequeue_all()); + + for entry in graph.blocks.processed() { + self.blocks.insert_processed(entry.value().clone()); + } + + self.blocks.enqueue_extend(graph.blocks.dequeue_all()); + + for entry in graph.functions.processed() { + self.functions.insert_processed(entry.value().clone()); + } + + self.functions.enqueue_extend(graph.functions.dequeue_all()); + + for entry in graph.instructions.valid() { + self.instructions.insert_valid(entry.value().clone()); + } + + for entry in graph.instructions.invalid() { + self.instructions.insert_invalid(entry.value().clone()); + } + + for entry in graph.blocks.valid() { + self.blocks.insert_valid(entry.value().clone()); + } + + for entry in graph.blocks.invalid() { + self.blocks.insert_invalid(entry.value().clone()); + } + + for entry in graph.functions.valid() { + self.functions.insert_valid(entry.value().clone()); + } + + for entry in graph.functions.invalid() { + self.functions.insert_invalid(entry.value().clone()); + } + + } + +} diff --git a/src/controlflow/instruction.rs b/src/controlflow/instruction.rs new file mode 100644 index 00000000..c8e41288 --- /dev/null +++ b/src/controlflow/instruction.rs @@ -0,0 +1,279 @@ +use std::{collections::BTreeSet, io::Error}; +use crate::binary::Binary; +use serde::{Deserialize, Serialize}; +use serde_json; +use serde_json::Value; +use std::io::ErrorKind; +use crate::controlflow::Graph; +use crate::controlflow::Attributes; +use crate::controlflow::Chromosome; +use crate::controlflow::ChromosomeJson; +use crate::Config; +use crate::Architecture; + +/// Represents a single instruction in disassembled binary code. +/// +/// This struct encapsulates metadata and properties of an instruction, +/// such as its address, type, and relationships with other instructions. +#[derive(Clone)] +pub struct Instruction { + // The instruction architecture + pub architecture: Architecture, + /// The configuration + pub config: Config, + /// The address of the instruction in memory. + pub address: u64, + /// Indicates whether this instruction is part of a function prologue. + pub is_prologue: bool, + /// Indicates whether this instruction is the start of a basic block. + pub is_block_start: bool, + /// Indicates whether this instruction is the start of a function. + pub is_function_start: bool, + /// The raw bytes of the instruction. + pub bytes: Vec, + /// The signature pattern of the instruction. + pub pattern: String, + /// Indicates whether this instruction is a return instruction. + pub is_return: bool, + /// Indicates whether this instruction is a call instruction. + pub is_call: bool, + /// A set of functions that this instruction may belong to. + pub functions: BTreeSet, + /// Indicates whether this instruction is a jump instruction. + pub is_jump: bool, + /// Indicates whether this instruction is a conditional instruction. + pub is_conditional: bool, + /// Indicates whether this instruction is a trap instruction. + pub is_trap: bool, + /// A set of addresses this instruction may jump or branch to. + pub to: BTreeSet, + /// The number of edges (connections) for this instruction. + pub edges: usize, +} + +/// Represents a JSON-serializable view of an `Instruction`. +#[derive(Serialize, Deserialize)] +pub struct InstructionJson { + /// The type of this entity, always `"instruction"`. + #[serde(rename = "type")] + pub type_: String, + // The architecture of the instruction. + pub architecture: String, + /// The address of the instruction in memory. + pub address: u64, + /// Indicates whether this instruction is part of a function prologue. + pub is_prologue: bool, + /// Indicates whether this instruction is the start of a basic block. + pub is_block_start: bool, + /// Indicates whether this instruction is the start of a function. + pub is_function_start: bool, + /// Indicates whether this instruction is a call instruction. + pub is_call: bool, + /// Indicates whether this instruction is a return instruction. + pub is_return: bool, + /// Indicates whether this instruction is a jump instruction. + pub is_jump: bool, + /// Indicates whether this instruction is a trap instruction. + pub is_trap: bool, + /// Indicates whether this instruction is conditional. + pub is_conditional: bool, + /// The number of edges (connections) for this instruction. + pub edges: usize, + /// The raw bytes of the instruction in hexadecimal format. + pub bytes: String, + /// The size of the instruction in bytes. + pub size: usize, + /// The chromosome + pub chromosome: ChromosomeJson, + /// A set of functions that this instruction may belong to. + pub functions: BTreeSet, + /// A set of addresses for the blocks this instruction may branch to. + pub blocks: BTreeSet, + /// A set of addresses this instruction may jump or branch to. + pub to: BTreeSet, + /// The address of the next sequential instruction, if any. + pub next: Option, + /// Attributes + pub attributes: Option, +} + +impl Instruction { + /// Creates a new `Instruction` with the specified address. + /// + /// # Arguments + /// + /// * `address` - The memory address of the instruction. + /// + /// # Returns + /// + /// Returns a new `Instruction` with default values for its properties. + #[allow(dead_code)] + pub fn create(address: u64, architecture: Architecture, config: Config) -> Self { + Self { + address: address, + is_prologue: false, + is_block_start: false, + is_function_start: false, + bytes: Vec::::new(), + pattern: String::new(), + is_call: false, + is_return: false, + functions: BTreeSet::::new(), + is_conditional: false, + is_jump: false, + to: BTreeSet::::new(), + edges: 0, + is_trap: false, + architecture: architecture, + config: config, + } + } + + /// Retrieves an `Instruction` from the control flow graph if available. + /// + /// # Returns + /// + /// Returns a `Result` containing the `Instruction`. + pub fn new(address: u64, cfg: &Graph) -> Result { + let instruction = cfg.get_instruction(address); + if instruction.is_none() { return Err(Error::new(ErrorKind::Other, format!("instruction does not exist"))); } + Ok(instruction.unwrap()) + } + + /// Retrieves the set of addresses for the blocks this instruction may branch to. + /// + /// # Returns + /// + /// Returns a `BTreeSet` containing the block addresses. + pub fn blocks(&self) -> BTreeSet { + let mut result = BTreeSet::new(); + if !self.is_jump { return result; } + for item in self.to.iter().map(|ref_multi| *ref_multi).chain(self.next()) { + result.insert(item); + } + result + } + + /// Retrieves the address of the next sequential instruction. + /// + /// # Returns + /// + /// Returns `Some(u64)` containing the address of the next instruction, or `None` + /// if the current instruction is a return or trap instruction. + pub fn next(&self) -> Option { + if self.is_jump && !self.is_conditional { return None; } + if self.is_return { return None; } + if self.is_trap { return None; } + Some(self.address + self.size() as u64) + } + + /// Computes the size of the instruction in bytes. + /// + /// # Returns + /// + /// Returns the size of the instruction as a `usize`. + #[allow(dead_code)] + pub fn size(&self) -> usize { + return self.bytes.len(); + } + + /// Converts the `Instruction` into its JSON-serializable representation. + /// + /// # Returns + /// + /// Returns an `InstructionJson` struct containing the properties of the instruction. + #[allow(dead_code)] + pub fn process(&self) -> InstructionJson { + InstructionJson { + type_: "instruction".to_string(), + architecture: self.architecture.to_string(), + address: self.address, + is_block_start: self.is_block_start, + bytes: Binary::to_hex(&self.bytes), + size: self.size(), + chromosome: self.chromosome(), + is_return: self.is_return, + is_trap: self.is_trap, + is_call: self.is_call, + is_jump: self.is_jump, + is_conditional: self.is_conditional, + is_function_start: self.is_function_start, + is_prologue: self.is_prologue, + edges: self.edges, + functions: self.functions(), + blocks: self.blocks(), + to: self.to(), + next: self.next(), + attributes: None, + } + } + + /// Generates a signature for the block using its address range and control flow graph. + /// + /// # Returns + /// + /// Returns a `SignatureJson` representing the block's signature. + pub fn chromosome(&self) -> ChromosomeJson { + Chromosome::new(self.pattern.clone(), self.config.clone()).unwrap().process() + } + + /// Retrieves the set of addresses this instruction may jump or branch to. + /// + /// # Returns + /// + /// Returns a `BTreeSet` containing the target addresses. + pub fn to(&self) -> BTreeSet { + return self.to.clone(); + } + + /// Retrieves the set of functions this instruction may belong to. + /// + /// # Returns + /// + /// Returns a `BTreeSet` containing the function addresses. + pub fn functions(&self) -> BTreeSet { + return self.functions.clone(); + } + + /// Converts the instruction into a JSON string representation including `Attributes`. + /// + /// # Returns + /// + /// Returns `Ok(String)` containing the JSON representation, or an `Err` if serialization fails. + pub fn json_with_attributes(&self, attributes: Attributes) -> Result { + let raw = self.process_with_attributes(attributes); + let result = serde_json::to_string(&raw)?; + Ok(result) + } + + /// Processes the instruction into its JSON-serializable representation including `Attributes`. + /// + /// # Returns + /// + /// Returns a `BlockJson` instance containing the block's metadata and `Attributes`. + pub fn process_with_attributes(&self, attributes: Attributes) -> InstructionJson { + let mut result = self.process(); + result.attributes = Some(attributes.process()); + return result; + } + + /// Converts the `Instruction` into a JSON string representation. + /// + /// # Returns + /// + /// Returns `Ok(String)` containing the JSON representation, or an `Err(Error)` if serialization fails. + #[allow(dead_code)] + pub fn json(&self) -> Result { + let raw = self.process(); + let result = serde_json::to_string(&raw)?; + Ok(result) + } + + /// Prints the JSON representation of the `Instruction` to standard output. + #[allow(dead_code)] + pub fn print(&self) { + if let Ok(json) = self.json() { + println!("{}", json); + } + } +} diff --git a/src/controlflow/mod.rs b/src/controlflow/mod.rs new file mode 100644 index 00000000..3286ddac --- /dev/null +++ b/src/controlflow/mod.rs @@ -0,0 +1,30 @@ +pub mod graph; +pub mod function; +pub mod block; +pub mod instruction; +pub mod chromosome; +pub mod attribute; +pub mod tag; +pub mod symbol; +pub mod gene; +pub mod allepair; + +pub use graph::Graph; +pub use graph::GraphQueue; +pub use function::Function; +pub use function::FunctionJson; +pub use block::Block; +pub use block::BlockJson; +pub use instruction::Instruction; +pub use instruction::InstructionJson; +pub use chromosome::Chromosome; +pub use chromosome::ChromosomeJson; +pub use attribute::Attribute; +pub use attribute::Attributes; +pub use tag::Tag; +pub use tag::TagJson; +pub use symbol::Symbol; +pub use symbol::SymbolJson; +pub use symbol::SymbolIoJson; +pub use gene::Gene; +pub use allepair::AllelePair; diff --git a/src/controlflow/symbol.rs b/src/controlflow/symbol.rs new file mode 100644 index 00000000..c6a6c3b9 --- /dev/null +++ b/src/controlflow/symbol.rs @@ -0,0 +1,130 @@ +use std::io::Error; +use serde::{Deserialize, Serialize}; +use serde_json; +use crate::controlflow::Attribute; + +/// Represents a JSON-serializable structure containing metadata about a function symbol. +#[derive(Serialize, Deserialize)] +pub struct SymbolIoJson { + /// The type of this entity. + #[serde(rename = "type")] + pub type_: String, + /// The type of symbol + pub symbol_type: String, + /// Names associated with the function symbol. + pub name: String, + /// The offset of the function symbol, if available. + pub file_offset: Option, + /// The relative virtual address of the function symbol, if available. + pub relative_virtual_address: Option, + /// The virtual address of the function symbol, if available. + pub virtual_address: Option, + /// The slice associated with the function symbol, MachO format only + pub slice: Option, +} + +/// Represents a JSON-serializable structure containing metadata about a function symbol. +#[derive(Serialize, Deserialize, Clone)] +pub struct SymbolJson { + #[serde(rename = "type")] + /// The type always `symbol`. + pub type_: String, + /// The type of symbol. + pub symbol_type: String, + /// Names associated with the function symbol. + pub name: String, + /// The virtual address of the function symbol. + #[serde(skip)] + pub address: u64, +} + +/// Represents a structure containing metadata about a function symbol. +#[derive(Clone, Debug)] +pub struct Symbol { + /// Names associated with the function symbol. + pub name: String, + /// The virtual address of the function symbol. + pub address: u64, + /// The type of symbol + pub symbol_type: String, +} + +impl Symbol { + #[allow(dead_code)] + pub fn new(address: u64, symbol_type: String, name: String) -> Self{ + Self { + name: name, + address: address, + symbol_type: symbol_type, + } + } + + /// Processes the function signature into its JSON-serializable representation. + /// + /// # Returns + /// + /// Returns a `FunctionSymbolJson` struct containing metadata about the function symbol. + pub fn process(&self) -> SymbolJson { + SymbolJson { + type_: "symbol".to_string(), + symbol_type: self.symbol_type.clone(), + name: self.name.clone(), + address: self.address, + } + } + + /// Processes the tag into an `Attribute`. + /// + /// # Returns + /// + /// Returns a `Attribute` struct containing the tag. + pub fn attribute(&self) -> Attribute { + Attribute::Symbol(self.process()) + } + + /// Prints the JSON representation of the function symbol to standard output. + #[allow(dead_code)] + pub fn print(&self) { + if let Ok(json) = self.json() { + println!("{}", json); + } + } + + /// Converts the function symbol metadata into a JSON string representation. + /// + /// # Returns + /// + /// Returns `Ok(String)` containing the JSON representation, or an `Err` if serialization fails. + pub fn json(&self) -> Result { + let raw = self.process(); + let result = serde_json::to_string(&raw)?; + Ok(result) + } + + /// Demangles a Microsoft Visual C++ (MSVC) mangled symbol name. + /// + /// # Arguments + /// + /// * `mangled_name` - A string slice representing the mangled symbol name to demangle. + /// + /// # Returns + /// + /// A `String` containing the demangled symbol name in the form `namespace::...::function_name`. + /// If the input string does not start with the MSVC mangling prefix `?`, the original string + /// is returned unchanged. + #[allow(dead_code)] + pub fn demangle_msvc_name(mangled_name: &str) -> String { + if !mangled_name.starts_with('?') { + return mangled_name.to_string(); + } + let parts: Vec<&str> = mangled_name.trim_start_matches('?').split('@').collect(); + let function_name = parts.get(0).unwrap_or(&mangled_name); + let mut namespaces: Vec<&str> = parts.iter().skip(1).take_while(|&&s| s != "").map(|&s| s).collect(); + namespaces.reverse(); + format!( + "{}::{}", + namespaces.join("::"), + function_name + ) + } +} diff --git a/src/controlflow/tag.rs b/src/controlflow/tag.rs new file mode 100644 index 00000000..8e78380d --- /dev/null +++ b/src/controlflow/tag.rs @@ -0,0 +1,68 @@ +use std::io::Error; +use serde::{Deserialize, Serialize}; +use crate::controlflow::Attribute; + +/// Represents a JSON-serializable structure containing metadata about a tag. +#[derive(Serialize, Deserialize, Clone)] +pub struct TagJson { + /// The type of this entity, always `"tag"`. + #[serde(rename = "type")] + pub type_: String, + /// The tag value + pub value: String, +} + +#[derive(Clone)] +pub struct Tag { + tag: String, +} + +impl Tag { + #[allow(dead_code)] + pub fn new(tag: String) -> Self{ + Self { + tag: tag, + } + } + + /// Processes the function signature into its JSON-serializable representation. + /// + /// # Returns + /// + /// Returns a `FunctionSymbolJson` struct containing metadata about the function symbol. + pub fn process(&self) -> TagJson { + TagJson { + type_: "tag".to_string(), + value: self.tag.clone(), + } + } + + /// Processes the tag into an `Attribute`. + /// + /// # Returns + /// + /// Returns a `Attribute` struct containing the tag. + pub fn attribute(&self) -> Attribute { + Attribute::Tag(self.process()) + } + + /// Prints the JSON representation of the function symbol to standard output. + #[allow(dead_code)] + pub fn print(&self) { + if let Ok(json) = self.json() { + println!("{}", json); + } + } + + /// Converts the function symbol metadata into a JSON string representation. + /// + /// # Returns + /// + /// Returns `Ok(String)` containing the JSON representation, or an `Err` if serialization fails. + pub fn json(&self) -> Result { + let raw = self.process(); + let result = serde_json::to_string(&raw)?; + Ok(result) + } + +} diff --git a/src/disassembler.cpp b/src/disassembler.cpp deleted file mode 100644 index ebfc9184..00000000 --- a/src/disassembler.cpp +++ /dev/null @@ -1,701 +0,0 @@ -#include "disassembler.h" - -// Very WIP Multi-Threaded Recursive Decompiler - -using namespace binlex; -using json = nlohmann::json; - -cs_arch Disassembler::arch; -cs_mode Disassembler::mode; - -//from https://github.com/capstone-engine/capstone/blob/master/include/capstone/x86.h - -#define X86_REL_ADDR(insn) (((insn).detail->x86.operands[0].type == X86_OP_IMM) \ - ? (uint64_t)((insn).detail->x86.operands[0].imm) \ - : (((insn).address + (insn).size) + (uint64_t)(insn).detail->x86.disp)) - -Disassembler::Disassembler(const binlex::File &firef) : DisassemblerBase(firef) { - // Set Decompiler Architecture - switch(file_reference.binary_arch){ - case BINARY_ARCH_X86: - case BINARY_ARCH_X86_64: - arch = CS_ARCH_X86; - break; - default: - PRINT_ERROR_AND_EXIT("[x] failed to set decompiler architecture\n"); - } - // Set Decompiler Mode - switch(file_reference.binary_mode){ - case BINARY_MODE_32: - mode = CS_MODE_32; - break; - case BINARY_MODE_64: - mode = CS_MODE_64; - break; - default: - PRINT_ERROR_AND_EXIT("[x] failed to set decompiler mode\n"); - } - // Append the Function Queue - for (uint32_t i = 0; i < file_reference.total_exec_sections; i++){ - std::set tmp = file_reference.sections[i].functions; - AppendQueue(tmp, DISASSEMBLER_OPERAND_TYPE_FUNCTION, i); - } - for (int i = 0; i < BINARY_MAX_SECTIONS; i++) { - sections[i].offset = 0; - sections[i].data = NULL; - sections[i].data_size = 0; - } -} - -void Disassembler::AppendTrait(struct Trait *trait, struct Section *sections, uint index){ - struct Trait new_elem_trait = *trait; - sections[index].traits.push_back(new_elem_trait); - } - -json Disassembler::GetTrait(struct Trait &trait){ - json data; - data["type"] = trait.type; - data["corpus"] = g_args.options.corpus; - data["tags"] = g_args.options.tags; - data["mode"] = g_args.options.mode; - data["bytes"] = trait.bytes; - data["trait"] = trait.trait; - data["edges"] = trait.edges; - data["blocks"] = trait.blocks; - data["instructions"] = trait.instructions; - data["size"] = trait.size; - data["offset"] = trait.offset; - data["bytes_entropy"] = trait.bytes_entropy; - data["bytes_sha256"] = trait.bytes_sha256; - string bytes_tlsh = TraitToTLSH(trait.bytes); - if (bytes_tlsh.length() > 0){ - data["bytes_tlsh"] = bytes_tlsh; - } else { - data["bytes_tlsh"] = nullptr; - } - data["trait_sha256"] = trait.trait_sha256; - string trait_tlsh = TraitToTLSH(trait.trait); - if (trait_tlsh.length() > 0){ - data["trait_tlsh"] = trait_tlsh; - } else { - data["trait_tlsh"] = nullptr; - } - data["trait_entropy"] = trait.trait_entropy; - data["invalid_instructions"] = trait.invalid_instructions; - data["cyclomatic_complexity"] = trait.cyclomatic_complexity; - data["average_instructions_per_block"] = trait.average_instructions_per_block; - return data; -} - -vector Disassembler::GetTraits(){ - vector traitsjson; - for (int i = 0; i < BINARY_MAX_SECTIONS; i++){ - if (sections[i].data != NULL){ - for (size_t j = 0; j < sections[i].traits.size(); j++){ - json jdata(GetTrait(sections[i].traits[j])); - traitsjson.push_back(jdata); - } - } - } - return traitsjson; -} - -void * Disassembler::CreateTraitsForSection(uint index) { - disasm_t myself; - - struct Trait b_trait; - struct Trait f_trait; - - PRINT_DEBUG("----------\nHandling section %u\n----------\n", index); - - b_trait.type = "block"; - ClearTrait(&b_trait); - f_trait.type = "function"; - ClearTrait(&f_trait); - - myself.error = cs_open(arch, mode, &myself.handle); - if (myself.error != CS_ERR_OK) { - return NULL; - } - myself.error = cs_option(myself.handle, CS_OPT_DETAIL, CS_OPT_ON); - if (myself.error != CS_ERR_OK) { - return NULL; - } - - cs_insn *insn = cs_malloc(myself.handle); - while (!sections[index].discovered.empty()){ - uint64_t address = 0; - - PRINT_DEBUG("discovered size = %u\n", (uint32_t)sections[index].discovered.size()); - PRINT_DEBUG("visited size = %u\n", (uint32_t)sections[index].visited.size()); - PRINT_DEBUG("functions size = %u\n", (uint32_t)sections[index].functions.size()); - PRINT_DEBUG("blocks size = %u\n", (uint32_t)sections[index].blocks.size()); - - address = sections[index].discovered.front(); - sections[index].discovered.pop(); - sections[index].visited[address] = DISASSEMBLER_VISITED_ANALYZED; - - myself.pc = address; - myself.code = (uint8_t *)((uint8_t *)sections[index].data + address); - myself.code_size = sections[index].data_size + address; - - //bool block = IsBlock(sections[index].blocks, address); - bool function = IsFunction(sections[index].functions, address); - uint suspicious_instructions = 0; - - while(true) { - uint edges = 0; - - if (myself.pc >= sections[index].data_size) { - break; - } - - bool result = cs_disasm_iter(myself.handle, &myself.code, &myself.code_size, &myself.pc, insn); - - if (result != true){ - // Error with disassembly, not a valid basic block, - PRINT_DEBUG("*** Decompile error rejected block: 0x%" PRIx64 "\n", myself.pc); - ClearTrait(&b_trait); - ClearTrait(&f_trait); - myself.code = (uint8_t *)((uint8_t *)myself.code + 1); - myself.code_size +=1; - myself.pc +=1; - break; - } - - // Check for suspicious instructions and count them - if (IsSuspiciousInsn(insn) == true){ - suspicious_instructions += 1; - } - - // If there are too many suspicious instructions in the bb discard it - // TODO: Make this configurable as an argument - if (suspicious_instructions > 2){ - PRINT_DEBUG("*** Suspicious instructions rejected block: 0x%" PRIx64 "\n", insn->address); - ClearTrait(&b_trait); - ClearTrait(&f_trait); - break; - } - - b_trait.instructions++; - f_trait.instructions++; - - // Need to Wildcard Traits Here - if (IsWildcardInsn(insn) == true){ - b_trait.trait = b_trait.trait + Wildcards(insn->size) + " "; - f_trait.trait = f_trait.trait + Wildcards(insn->size) + " "; - } else { - b_trait.trait = b_trait.trait + WildcardInsn(insn) + " "; - f_trait.trait = f_trait.trait + WildcardInsn(insn) + " "; - } - b_trait.bytes = b_trait.bytes + HexdumpBE(insn->bytes, insn->size) + " "; - f_trait.bytes = f_trait.bytes + HexdumpBE(insn->bytes, insn->size) + " "; - edges = GetInsnEdges(insn); - b_trait.edges = b_trait.edges + edges; - f_trait.edges = f_trait.edges + edges; - if (edges > 0){ - b_trait.blocks++; - f_trait.blocks++; - } - - CollectInsn(insn, sections, index); - - PRINT_DEBUG("address=0%" PRIx64 ",block=%d,function=%d,queue=%ld,instruction=%s\t%s\n", insn->address,IsBlock(sections[index].blocks, insn->address), IsFunction(sections[index].functions, insn->address), sections[index].discovered.size(), insn->mnemonic, insn->op_str); - - if (IsJumpInsn(insn) == true){ - b_trait.trait = TrimRight(b_trait.trait); - b_trait.bytes = TrimRight(b_trait.bytes); - b_trait.size = GetByteSize(b_trait.bytes); - b_trait.offset = sections[index].offset + myself.pc - b_trait.size; - AppendTrait(&b_trait, sections, index); - ClearTrait(&b_trait); - if (function == false){ - ClearTrait(&f_trait); - break; - } - } - if (IsRetInsn(insn) == true){ - b_trait.xrefs.insert(address); - b_trait.trait = TrimRight(b_trait.trait); - b_trait.bytes = TrimRight(b_trait.bytes); - b_trait.size = GetByteSize(b_trait.bytes); - b_trait.offset = sections[index].offset + myself.pc - b_trait.size; - AppendTrait(&b_trait, sections, index); - ClearTrait(&b_trait); - f_trait.xrefs.insert(address); - f_trait.trait = TrimRight(f_trait.trait); - f_trait.bytes = TrimRight(f_trait.bytes); - f_trait.size = GetByteSize(f_trait.bytes); - f_trait.offset = sections[index].offset + myself.pc - f_trait.size; - AppendTrait(&f_trait, sections, index); - ClearTrait(&f_trait); - break; - } - } - } - cs_free(insn, 1); - cs_close(&myself.handle); - return NULL; -} - -void * Disassembler::FinalizeTrait(struct Trait &trait){ - if (trait.blocks == 0 && - (strcmp(trait.type.c_str(), "function") == 0 || - strcmp(trait.type.c_str(), "block") == 0)){ - trait.blocks++; - } - trait.bytes_entropy = Entropy(string(trait.bytes)); - trait.trait_entropy = Entropy(string(trait.trait)); - trait.trait_sha256 = SHA256((char *)trait.trait.c_str()); - trait.bytes_sha256 = SHA256((char *)trait.bytes.c_str()); - if (strcmp(trait.type.c_str(), (char *)"block") == 0){ - trait.cyclomatic_complexity = trait.edges - 1 + 2; - trait.average_instructions_per_block = trait.instructions / 1; - } - if (strcmp(trait.type.c_str(), (char *)"function") == 0){ - trait.cyclomatic_complexity = trait.edges - trait.blocks + 2; - trait.average_instructions_per_block = trait.instructions / trait.blocks; - } - return NULL; -} - -void Disassembler::ClearTrait(struct Trait *trait){ - trait->bytes.clear(); - trait->edges = 0; - trait->instructions = 0; - trait->blocks = 0; - trait->offset = 0; - trait->size = 0; - trait->invalid_instructions = 0; - trait->trait.clear(); - trait->trait.clear(); - trait->bytes_sha256.clear(); - trait->trait_sha256.clear(); - trait->xrefs.clear(); -} - -void Disassembler::AppendQueue(set &addresses, DISASSEMBLER_OPERAND_TYPE operand_type, uint index){ - PRINT_DEBUG("List of queued addresses for section %u correponding to found functions: ", index); - for (auto it = addresses.begin(); it != addresses.end(); ++it){ - uint64_t tmp_addr = *it; - sections[index].discovered.push(tmp_addr); - sections[index].visited[tmp_addr] = DISASSEMBLER_VISITED_QUEUED; - switch(operand_type){ - case DISASSEMBLER_OPERAND_TYPE_BLOCK: - AddDiscoveredBlock(tmp_addr, sections, index); - break; - case DISASSEMBLER_OPERAND_TYPE_FUNCTION: - AddDiscoveredFunction(tmp_addr, sections, index); - break; - default: - break; - } - PRINT_DEBUG("0x%" PRIu64 " ", tmp_addr); - } - PRINT_DEBUG("\n"); -} - -void Disassembler::LinearDisassemble(void* data, size_t data_size, size_t offset, uint index) { - // This function is intended to perform a preliminary quick linear disassembly pass of the section - // and initially populate the discovered queue with addressed that may not be found via recursive disassembly. - // - // TODO: This algorithm is garbage and creates a lot of false positives, it should be replaced with a proper - // linear pass that can differentiate data and code. - // See research linked here: https://github.com/c3rb3ru5d3d53c/binlex/issues/42#issuecomment-1110479885 - // - // * The Algorithm * - // - Disassemble each instruction sequentially - // - Track the state of the disassembly (valid / invalid) - // - The state is set to invalid for nops, traps, privileged instructions, and errors - // - When a jmp (conditional or unconditional) is encountered if the state is valid begin counting valid β€œblocks” - // - When three consecutive blocks are found push the jmp addresses onto the queue - // - When a jmp (conditional or unconditional) is encountered if the state is invalid reset to valid and begin tracking blocks - // - // * Weaknesses * - // - We don’t collect calls (these are assumed to be collected in the recursive disassembler) (DONE) - // - We don’t reset the state on ret or call instructions possibly missing some valid blocks (DONE) - // - We don’t collect the next address after a jmp (next block) missing some valid blocks (effective?) - // - Even with the filtering we will still add some number of addresses that are from invalid jmp institutions - - disasm_t disasm; - - PRINT_DEBUG("LinearDisassemble: Started at offset = 0x%" PRIu64 " data_size = %" PRIu64 " bytes\n", offset, data_size); - - if(cs_open(arch, mode, &disasm.handle) != CS_ERR_OK) { - PRINT_ERROR_AND_EXIT("[x] LinearDisassembly failed to init capstone\n"); - } - - if (cs_option(disasm.handle, CS_OPT_DETAIL, CS_OPT_ON) != CS_ERR_OK) { - PRINT_ERROR_AND_EXIT("[x] LinearDisassembly failed to set capstone options\n"); - } - - cs_insn *cs_ins = cs_malloc(disasm.handle); - disasm.pc = offset; - disasm.code = (uint8_t *)((uint8_t *)data); - disasm.code_size = data_size + disasm.pc; - bool valid_block = true; - uint64_t valid_block_count = 1; - uint64_t jmp_address_1 = 0; - uint64_t jmp_address_2 = 0; - - while(disasm.pc < disasm.code_size){ - if (!cs_disasm_iter(disasm.handle, &disasm.code, &disasm.code_size, &disasm.pc, cs_ins)){ - PRINT_DEBUG("LinearDisassemble: 0x%" PRIu64 ": Disassemble ERROR\n", disasm.pc); - disasm.pc += 1; - disasm.code_size -= 1; - disasm.code = (uint8_t *)((uint8_t *)disasm.code + 1); - valid_block = false; - valid_block_count = 0; - continue; - } - PRINT_DEBUG("LinearDisassemble: 0x%" PRIu64 ": %s\t%s\n", cs_ins->address, cs_ins->mnemonic, cs_ins->op_str); - - if (IsSuspiciousInsn(cs_ins) == true){ - PRINT_DEBUG("LinearDisassemble: Suspicious instruction at 0x%" PRIu64 "\n", cs_ins->address); - valid_block = false; - valid_block_count = 0; - continue; - } - - if (IsCallInsn(cs_ins) == true && valid_block_count >= 2){ - CollectInsn(cs_ins, sections, index); - } - - if (IsJumpInsn(cs_ins) == true){ - if (valid_block){ - if (valid_block_count == 1) { - jmp_address_2 = X86_REL_ADDR(*cs_ins); - } else if (valid_block_count == 2) { - PRINT_DEBUG("LinearDisassemble: Found three consecutive valid blocks adding jmp addresses\n"); - AddDiscoveredBlock(jmp_address_1, sections, index); - AddDiscoveredBlock(jmp_address_2, sections, index); - CollectInsn(cs_ins, sections, index); - } - valid_block_count++; - } else { - valid_block = true; - valid_block_count = 1; - jmp_address_1 = X86_REL_ADDR(*cs_ins); - } - } - } - cs_free(cs_ins, 1); - cs_close(&disasm.handle); -}; - -void Disassembler::Disassemble() { - for (uint32_t i = 0; i < file_reference.total_exec_sections; i++){ - sections[i].offset = file_reference.sections[i].offset; - sections[i].data = file_reference.sections[i].data; - sections[i].data_size = file_reference.sections[i].size; - LinearDisassemble(sections[i].data, sections[i].data_size, sections[i].offset, i); - CreateTraitsForSection(i); - for (size_t j = 0; j < sections[i].traits.size(); ++j) { - FinalizeTrait(sections[i].traits[j]); - } - } -} - -string Disassembler::WildcardInsn(cs_insn *insn){ - string bytes = HexdumpBE(insn->bytes, insn->size); - string trait = bytes; - for (int j = 0; j < insn->detail->x86.op_count; j++){ - cs_x86_op operand = insn->detail->x86.operands[j]; - switch(operand.type){ - case X86_OP_MEM: - { - if (operand.mem.disp != 0){ - trait = WildcardTrait(bytes, HexdumpBE(&operand.mem.disp, sizeof(uint64_t))); - } - break; - } - default: - break; - } - } - return TrimRight(trait); -} - -bool Disassembler::IsVisited(map &visited, uint64_t address) { - return visited.find(address) != visited.end(); -} - -bool Disassembler::IsNopInsn(cs_insn *ins){ - switch(ins->id) { - case X86_INS_NOP: - case X86_INS_FNOP: - return true; - default: - return false; - } -} - -bool Disassembler::IsPaddingInsn(cs_insn *insn){ - return IsNopInsn(insn) || - (IsSemanticNopInsn(insn) && (file_reference.binary_type != BINARY_TYPE_PE)) || - (IsTrapInsn(insn) && (file_reference.binary_type == BINARY_TYPE_PE)); -} - -bool Disassembler::IsSemanticNopInsn(cs_insn *ins){ - cs_x86 *x86; - - /* XXX: to make this truly platform-independent, we need some real - * semantic analysis, but for now checking known cases is sufficient */ - - x86 = &ins->detail->x86; - switch(ins->id) { - case X86_INS_MOV: - /* mov reg,reg */ - if((x86->op_count == 2) - && (x86->operands[0].type == X86_OP_REG) - && (x86->operands[1].type == X86_OP_REG) - && (x86->operands[0].reg == x86->operands[1].reg)) { - return true; - } - return false; - case X86_INS_XCHG: - /* xchg reg,reg */ - if((x86->op_count == 2) - && (x86->operands[0].type == X86_OP_REG) - && (x86->operands[1].type == X86_OP_REG) - && (x86->operands[0].reg == x86->operands[1].reg)) { - return true; - } - return false; - case X86_INS_LEA: - /* lea reg,[reg + 0x0] */ - if((x86->op_count == 2) - && (x86->operands[0].type == X86_OP_REG) - && (x86->operands[1].type == X86_OP_MEM) - && (x86->operands[1].mem.segment == X86_REG_INVALID) - && (x86->operands[1].mem.base == x86->operands[0].reg) - && (x86->operands[1].mem.index == X86_REG_INVALID) - /* mem.scale is irrelevant since index is not used */ - && (x86->operands[1].mem.disp == 0)) { - return true; - } - /* lea reg,[reg + eiz*x + 0x0] */ - if((x86->op_count == 2) - && (x86->operands[0].type == X86_OP_REG) - && (x86->operands[1].type == X86_OP_MEM) - && (x86->operands[1].mem.segment == X86_REG_INVALID) - && (x86->operands[1].mem.base == x86->operands[0].reg) - && (x86->operands[1].mem.index == X86_REG_EIZ) - /* mem.scale is irrelevant since index is the zero-register */ - && (x86->operands[1].mem.disp == 0)) { - return true; - } - return false; - default: - return false; - } -} - -bool Disassembler::IsTrapInsn(cs_insn *ins){ - switch(ins->id) { - case X86_INS_INT3: - case X86_INS_UD2: - case X86_INS_INT1: - case X86_INS_INTO: - return true; - default: - return false; - } -} - -bool Disassembler::IsSuspiciousInsn(cs_insn *insn){ - return (IsPaddingInsn(insn) || IsSemanticNopInsn(insn) || IsTrapInsn(insn) || IsPrivInsn(insn)); -} - -bool Disassembler::IsPrivInsn(cs_insn *ins){ - switch(ins->id) { - case X86_INS_HLT: - case X86_INS_IN: - case X86_INS_INSB: - case X86_INS_INSW: - case X86_INS_INSD: - case X86_INS_OUT: - case X86_INS_OUTSB: - case X86_INS_OUTSW: - case X86_INS_OUTSD: - case X86_INS_RDMSR: - case X86_INS_WRMSR: - case X86_INS_RDPMC: - case X86_INS_RDTSC: - case X86_INS_LGDT: - case X86_INS_LLDT: - case X86_INS_LTR: - case X86_INS_LMSW: - case X86_INS_CLTS: - case X86_INS_INVD: - case X86_INS_INVLPG: - case X86_INS_WBINVD: - return true; - default: - return false; - } -} - -bool Disassembler::IsWildcardInsn(cs_insn *insn){ - switch (insn->id) { - case X86_INS_NOP: - case X86_INS_FNOP: - return true; - default: - break; - } - return false; -} - -bool Disassembler::IsRetInsn(cs_insn *insn) { - switch (insn->id) { - case X86_INS_RET: - case X86_INS_RETF: - case X86_INS_RETFQ: - case X86_INS_IRET: - case X86_INS_IRETD: - case X86_INS_IRETQ: - return true; - default: - return false; - } -} - -bool Disassembler::IsJumpInsn(cs_insn *insn){ - if (IsUnconditionalJumpInsn(insn) == true){ - return true; - } - if (IsConditionalJumpInsn(insn) == true){ - return true; - } - return false; -} - -uint64_t Disassembler::GetInsnEdges(cs_insn *insn){ - if (IsUnconditionalJumpInsn(insn) == true){ - return 1; - } - if (IsConditionalJumpInsn(insn) == true){ - return 2; - } - return 0; -} - -bool Disassembler::IsUnconditionalJumpInsn(cs_insn *insn){ - switch (insn->id) { - case X86_INS_JMP: - return true; - default: - return false; - } -} - -bool Disassembler::IsConditionalJumpInsn(cs_insn* insn) { - switch (insn->id) { - case X86_INS_JNE: - case X86_INS_JNO: - case X86_INS_JNP: - case X86_INS_JL: - case X86_INS_JLE: - case X86_INS_JG: - case X86_INS_JGE: - case X86_INS_JE: - case X86_INS_JECXZ: - case X86_INS_JCXZ: - case X86_INS_JB: - case X86_INS_JBE: - case X86_INS_JA: - case X86_INS_JAE: - case X86_INS_JNS: - case X86_INS_JO: - case X86_INS_JP: - case X86_INS_JRCXZ: - case X86_INS_JS: - case X86_INS_LOOPE: - case X86_INS_LOOPNE: - return true; - default: - return false; - } -} - -bool Disassembler::IsCallInsn(cs_insn *insn){ - switch(insn->id){ - case X86_INS_CALL: - case X86_INS_LCALL: - return true; - default: - break; - } - return false; -} - -DISASSEMBLER_OPERAND_TYPE Disassembler::CollectInsn(cs_insn* insn, struct Section *sections, uint index) { - DISASSEMBLER_OPERAND_TYPE result = DISASSEMBLER_OPERAND_TYPE_UNSET; - if (IsJumpInsn(insn) == true){ - CollectOperands(insn, DISASSEMBLER_OPERAND_TYPE_BLOCK, sections, index); - result = DISASSEMBLER_OPERAND_TYPE_BLOCK; - return result; - } - if (IsCallInsn(insn) == true){ - CollectOperands(insn, DISASSEMBLER_OPERAND_TYPE_FUNCTION, sections, index); - result = DISASSEMBLER_OPERAND_TYPE_FUNCTION; - return result; - } - return result; -} - -void Disassembler::AddDiscoveredBlock(uint64_t address, struct Section *sections, uint index) { - if (IsVisited(sections[index].visited, address) == false && - address < sections[index].data_size && - IsFunction(sections[index].functions, address) == false) { - if (sections[index].blocks.insert(address).second == true){ - sections[index].visited[address] = DISASSEMBLER_VISITED_QUEUED; - sections[index].discovered.push(address); - } - } -} - -void Disassembler::AddDiscoveredFunction(uint64_t address, struct Section *sections, uint index) { - if (IsVisited(sections[index].visited, address) == false && address < sections[index].data_size) { - if (sections[index].functions.insert(address).second == true){ - sections[index].visited[address] = DISASSEMBLER_VISITED_QUEUED; - sections[index].discovered.push(address); - } - } -} - -void Disassembler::CollectOperands(cs_insn* insn, int operand_type, struct Section *sections, uint index) { - uint64_t address = X86_REL_ADDR(*insn); - switch(operand_type){ - case DISASSEMBLER_OPERAND_TYPE_BLOCK: - { - AddDiscoveredBlock(address, sections, index); - AddDiscoveredBlock(insn->address + insn->size, sections, index); - break; - } - case DISASSEMBLER_OPERAND_TYPE_FUNCTION: - { - AddDiscoveredFunction(address, sections, index); - break; - } - default: - break; - } -} - -bool Disassembler::IsFunction(set &addresses, uint64_t address){ - if (addresses.find(address) != addresses.end()){ - return true; - } - return false; -} - -bool Disassembler::IsBlock(set &addresses, uint64_t address){ - if (addresses.find(address) != addresses.end()){ - return true; - } - return false; -} - -Disassembler::~Disassembler() {} diff --git a/src/disassemblerbase.cpp b/src/disassemblerbase.cpp deleted file mode 100644 index 1085039f..00000000 --- a/src/disassemblerbase.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include "disassemblerbase.h" - -using namespace binlex; - -DisassemblerBase::DisassemblerBase(const binlex::File &firef) : file_reference(firef) { -} - -void DisassemblerBase::py_SetThreads(uint threads) { - g_args.options.threads = threads; -} - -void DisassemblerBase::py_SetCorpus(const char *corpus) { - g_args.options.corpus = corpus; -} - -void DisassemblerBase::py_SetTags(const vector &tags){ - g_args.options.tags = set(tags.begin(), tags.end()); -} - -void DisassemblerBase::py_SetMode(string mode){ - g_args.options.mode = mode; -} - -void DisassemblerBase::WriteTraits(){ - std::ofstream output_stream; - if (g_args.options.output != NULL) { - output_stream.open(g_args.options.output); - if(!output_stream.is_open()) { - PRINT_ERROR_AND_EXIT("Unable to open file %s for writing\n", g_args.options.output); - } - } - auto traits(GetTraits()); - if(g_args.options.output != NULL) { - for(auto trait : traits) { - trait["file_sha256"] = file_reference.sha256; - trait["file_tlsh"] = file_reference.tlsh; - output_stream << (g_args.options.pretty ? trait.dump(4) : trait.dump()) << endl; - } - } else { - for(auto trait : traits) { - trait["file_sha256"] = file_reference.sha256; - trait["file_tlsh"] = file_reference.tlsh; - cout << (g_args.options.pretty ? trait.dump(4) : trait.dump()) << endl; - } - } - if (g_args.options.output != NULL) { - output_stream.close(); - } -} diff --git a/src/disassemblers/capstone/disassembler.rs b/src/disassemblers/capstone/disassembler.rs new file mode 100644 index 00000000..caf3398a --- /dev/null +++ b/src/disassemblers/capstone/disassembler.rs @@ -0,0 +1,906 @@ +extern crate capstone; +use arch::x86::X86OpMem; +use arch::x86::X86Reg::{ + X86_REG_RSP, + X86_REG_RBP, + X86_REG_ESP, + X86_REG_EBP, + X86_REG_RIP, +}; +use capstone::prelude::*; +use capstone::arch::x86::X86Insn; +use capstone::arch::x86::X86OperandType; +use capstone::arch::ArchOperand; +use capstone::Insn; +use capstone::InsnId; +use capstone::Instructions; +use std::io::Error; +use std::io::ErrorKind; +use std::collections::{BTreeMap, BTreeSet}; +use crate::binary::Binary; +use crate::Architecture; +use crate::controlflow::instruction::Instruction; +use crate::controlflow::graph::Graph; +use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; +use rayon::ThreadPoolBuilder; +use crate::io::Stderr; + +pub struct Disassembler<'disassembler> { + cs: Capstone, + image: &'disassembler[u8], + machine: Architecture, + executable_address_ranges: BTreeMap, +} + +impl<'disassembler> Disassembler<'disassembler> { + + pub fn new(machine: Architecture, image: &'disassembler[u8], executable_address_ranges: BTreeMap) -> Result { + let cs = Disassembler::cs_new(machine, true)?; + Ok(Self{ + cs: cs, + image: image, + machine: machine, + executable_address_ranges: executable_address_ranges, + }) + } + + pub fn is_executable_address(&self, address: u64) -> bool { + self.executable_address_ranges + .iter() + .any(|(start, end)| address >= *start && address <= *end) + } + + #[allow(dead_code)] + pub fn disassemble_sweep(&self) -> BTreeSet { + + let valid_jump_threshold: usize = 2; + let valid_instruction_threshold: usize = 4; + + let mut functions = BTreeSet::::new(); + for (start, end) in self.executable_address_ranges.clone() { + let mut pc = start; + let mut valid_instructions = 0; + let mut valid_jumps = 0; + while pc < end { + let instructions = match self.disassemble_instructions(pc, 1) { + Ok(instructions) => instructions, + Err(_) => { + pc += 1; + valid_instructions = 0; + valid_jumps = 0; + continue; + }, + }; + let instruction = instructions.iter().next().unwrap(); + if Disassembler::is_privilege_instruction(instruction) + || Disassembler::is_trap_instruction(instruction) { + pc += instruction.bytes().len() as u64; + continue; + } + if let Some(imm) = self.get_jump_immutable(instruction) { + if self.is_executable_address(imm) { + valid_jumps += 1; + } else { + valid_instructions = 0; + valid_jumps = 0; + pc += 1; + continue; + } + } + if let Some(imm) = self.get_call_immutable(instruction) { + if valid_jumps >= valid_jump_threshold + && valid_instructions >= valid_instruction_threshold { + if self.is_executable_address(imm) { + functions.insert(imm); + } else { + valid_instructions = 0; + valid_jumps = 0; + pc += 1; + continue; + } + } + } + valid_instructions += 1; + pc += instruction.bytes().len() as u64; + } + } + return functions; + } + + #[allow(dead_code)] + pub fn disassemble_controlflow<'a>(&'a self, addresses: BTreeSet, cfg: &'a mut Graph) -> Result<(), Error> { + + let pool = ThreadPoolBuilder::new() + .num_threads(cfg.config.general.threads) + .build() + .map_err(|error| Error::new(ErrorKind::Other, format!("{}", error)))?; + + if cfg.config.disassembler.sweep.enabled { + cfg.functions.enqueue_extend(self.disassemble_sweep()); + } + + cfg.functions.enqueue_extend(addresses); + + let external_image = self.image; + + let external_machine = self.machine.clone(); + + let external_executable_address_ranges = self.executable_address_ranges.clone(); + + pool.install(|| { + while !cfg.functions.queue.is_empty() { + let function_addresses = cfg.functions.dequeue_all(); + cfg.functions.insert_processed_extend(function_addresses.clone()); + let graphs: Vec = function_addresses + .par_iter() + .map(|address| { + let machine = external_machine.clone(); + let executable_address_ranges = external_executable_address_ranges.clone(); + let image = external_image; + let mut graph = Graph::new(machine, cfg.config.clone()); + if let Ok(disasm) = Disassembler::new(machine, image, executable_address_ranges) { + let _ = disasm.disassemble_function(*address, &mut graph); + } + graph + }) + .collect(); + for mut graph in graphs { + cfg.absorb(&mut graph); + } + } + }); + + return Ok(()); + } + + pub fn disassemble_function<'a>(&'a self, address: u64, cfg: &'a mut Graph) -> Result { + + cfg.functions.insert_processed(address); + + if !self.is_executable_address(address) { + cfg.functions.insert_invalid(address); + let error_message = format!("Function -> 0x{:x}: it is not in executable memory", address); + Stderr::print_debug(cfg.config.clone(), &error_message); + return Err(Error::new(ErrorKind::Other, error_message)); + } + + cfg.blocks.enqueue(address); + + while let Some(block_start_address) = cfg.blocks.dequeue() { + + if cfg.blocks.is_processed(block_start_address) { + continue; + } + + let block_end_address = self + .disassemble_block(block_start_address, cfg) + .map_err(|error| { + cfg.functions.insert_invalid(address); + error + })?; + + if block_start_address == address { + if let Some(mut instruction) = cfg.get_instruction(block_start_address) { + instruction.is_function_start = true; + cfg.update_instruction(instruction); + } + } + + if let Some(instruction) = cfg.get_instruction(block_end_address) { + cfg.blocks.enqueue_extend(instruction.blocks()); + } + } + + cfg.functions.insert_valid(address); + + Ok(address) + } + + pub fn disassemble_instruction<'a>(&'a self, address: u64, cfg: &'a mut Graph) -> Result { + + cfg.instructions.insert_processed(address); + + if let Some(instruction) = cfg.get_instruction(address) { + return Ok(instruction.address); + } + + if !self.is_executable_address(address) { + cfg.instructions.insert_invalid(address); + let error = format!("Instruction -> 0x{:x}: it is not in executable memory", address); + Stderr::print_debug(cfg.config.clone(), error.clone()); + return Err(Error::new(ErrorKind::Other, error)); + } + + let instruction_container = self.disassemble_instructions(address, 1)?; + let instruction = instruction_container.iter().next().ok_or_else(|| { + cfg.instructions.insert_invalid(address); + let error = format!("0x{:x}: failed to disassemble instruction", address); + Error::new(ErrorKind::Other, error) + })?; + + let instruction_signature = self.get_instruction_pattern(&instruction)?; + + let mut blinstruction = Instruction::create(instruction.address(), cfg.architecture, cfg.config.clone()); + + blinstruction.is_jump = Disassembler::is_jump_instruction(instruction); + blinstruction.is_call = Disassembler::is_call_instruction(instruction); + blinstruction.is_return = Disassembler::is_return_instruction(instruction); + blinstruction.is_trap = Disassembler::is_trap_instruction(instruction); + + if blinstruction.is_jump { + blinstruction.is_conditional = Disassembler::is_conditional_jump_instruction(instruction); + } + + blinstruction.edges = self.get_instruction_edges(instruction); + blinstruction.bytes = instruction.bytes().to_vec(); + blinstruction.pattern = instruction_signature; + + if let Some(addr) = self.get_conditional_jump_immutable(instruction) { + blinstruction.to.insert(addr); + } + if let Some(addr) = self.get_unconditional_jump_immutable(instruction) { + blinstruction.to.insert(addr); + } + if let Some(addr) = self.get_call_immutable(instruction) { + cfg.functions.enqueue(addr); + blinstruction.functions.insert(addr); + } + if let Some(addr) = self.get_instruction_executable_addresses(instruction) { + cfg.functions.enqueue(addr); + blinstruction.functions.insert(addr); + } + + Stderr::print_debug( + cfg.config.clone(), + format!( + "0x{:x}: mnemonic: {:?}, next: {:?}, to: {:?}", + blinstruction.address, + instruction.mnemonic().unwrap(), + blinstruction.next(), + blinstruction.to() + ) + ); + + cfg.insert_instruction(blinstruction); + + cfg.instructions.insert_valid(address); + + Ok(address) + } + + #[allow(dead_code)] + pub fn disassemble_block<'a>(&'a self, address: u64, cfg: &'a mut Graph) -> Result { + + cfg.blocks.insert_processed(address); + + if !self.is_executable_address(address) { + cfg.functions.insert_invalid(address); + let error_message = format!("Block -> 0x{:x}: it is not in executable memory", address); + Stderr::print_debug(cfg.config.clone(), error_message.clone()); + return Err(Error::new(ErrorKind::Other, error_message)); + } + + let mut pc = address; + let mut has_prologue = false; + + while let Ok(_) = self.disassemble_instruction(pc, cfg) { + let mut instruction = match cfg.get_instruction(pc) { + Some(instr) => instr, + None => { + cfg.blocks.insert_invalid(address); + return Err(Error::new(ErrorKind::Other, "failed to disassemble instruction")); + } + }; + + if instruction.address == address { + instruction.is_block_start = true; + cfg.update_instruction(instruction.clone()); + } + + if instruction.address == address && instruction.is_block_start { + instruction.is_prologue = self.is_function_prologue(instruction.address); + has_prologue = instruction.is_prologue; + cfg.update_instruction(instruction.clone()); + } + + let is_block_start = instruction.address != address && instruction.is_block_start; + + if instruction.is_trap || instruction.is_return || instruction.is_jump || is_block_start { + break; + } + + pc += instruction.size() as u64; + } + + if has_prologue { + cfg.functions.enqueue(address); + } + cfg.blocks.insert_valid(address); + + Ok(pc) + } + + pub fn is_function_prologue(&self, address: u64) -> bool { + + // Starting Instructions + if let Ok(instructions) = self.disassemble_instructions(address, 2) { + match self.machine { + Architecture::AMD64 => { + if instructions[0].id() == InsnId(X86Insn::X86_INS_PUSH as u32) + && self.instruction_has_register_operand(&instructions[0], 0, RegId(X86_REG_RBP as u16)) + && instructions[1].id() != InsnId(X86Insn::X86_INS_MOV as u32) + && self.instruction_has_register_operand(&instructions[1], 0, RegId(X86_REG_RBP as u16)) + && self.instruction_has_register_operand(&instructions[1], 1, RegId(X86_REG_RSP as u16)) + { + return true; + } + }, + Architecture::I386 => { + if instructions[0].id() == InsnId(X86Insn::X86_INS_PUSH as u32) + && self.instruction_has_register_operand(&instructions[0], 0, RegId(X86_REG_EBP as u16)) + && instructions[1].id() != InsnId(X86Insn::X86_INS_MOV as u32) + && self.instruction_has_register_operand(&instructions[1], 0, RegId(X86_REG_EBP as u16)) + && self.instruction_has_register_operand(&instructions[1], 1, RegId(X86_REG_ESP as u16)) + { + return true; + } + }, + _ => {} + } + } + + return false; + } + + fn instruction_has_register_operand(&self, instruction: &Insn, index: usize, register_id: RegId) -> bool { + let operands = match self.get_instruction_operands(instruction) { + Ok(operands) => operands, + Err(_) => return false, + }; + + if let Some(operand) = operands.get(index) { + if let ArchOperand::X86Operand(op) = operand { + if let X86OperandType::Reg(reg_id) = op.op_type { + return reg_id == register_id; + } + } + } + return false; + } + + #[allow(dead_code)] + pub fn get_operand_mem(operand: &ArchOperand) -> Option { + if let ArchOperand::X86Operand(operand) = operand { + if let X86OperandType::Mem(_operand) = operand.op_type { + return Some(_operand); + } + } + None + } + + #[allow(dead_code)] + pub fn get_instruction_total_operand_size(&self, instruction: &Insn) -> Result { + let operands = self.get_instruction_operands(instruction)?; + let mut result: usize = 0; + for operand in operands { + match operand { + ArchOperand::X86Operand(op) => { + result += op.size as usize; + }, + _ => return Err(Error::new(ErrorKind::Other, "unsupported operand architecture")), + } + } + return Ok(result); + } + + pub fn instruction_contains_memory_operand(&self, instruction: &Insn) -> bool { + let operands = match self.get_instruction_operands(instruction) { + Ok(operands) => operands, + Err(_) => return false, + }; + for operand in operands { + if let ArchOperand::X86Operand(op) = operand { + match op.op_type { + X86OperandType::Mem(_) => { + return true + }, + _ => continue, + }; + } + } + return false; + } + + pub fn instruction_contains_immutable_operand(&self, instruction: &Insn) -> bool { + let operands = match self.get_instruction_operands(instruction) { + Ok(operands) => operands, + Err(_) => return false, + }; + for operand in operands { + if let ArchOperand::X86Operand(op) = operand { + match op.op_type { + X86OperandType::Imm(_) => return true, + _ => continue, + }; + } + } + return false; + } + + #[allow(dead_code)] + pub fn get_instruction_pattern(&self, instruction: &Insn) -> Result { + + if Disassembler::is_unsupported_pattern_instruction(instruction) { + return Ok(Binary::to_hex(instruction.bytes())); + } + + if Disassembler::is_wildcard_instruction(instruction) { + return Ok("??".repeat(instruction.bytes().len())); + } + + if !self.instruction_contains_immutable_operand(instruction) + && !self.instruction_contains_memory_operand(instruction) { + return Ok(Binary::to_hex(instruction.bytes())); + } + + let instruction_size = instruction.bytes().len() * 8; + + let mut wildcarded = vec![false; instruction_size]; + + let instruction_trailing_null_size = instruction.bytes().iter().rev().take_while(|&&b| b == 0).count() * 8; + + let operands = self.get_instruction_operands(instruction)?; + + let total_operand_size = self.get_instruction_total_operand_size(instruction)?; + + if total_operand_size > instruction_size { + return Err(Error::new(ErrorKind::Other, format!("Instruction -> 0x{:x}: operand offset exceeds instruction size", instruction.address()))); + } + + let instruction_trailing_null_offset = instruction_size - instruction_trailing_null_size; + + let is_immutable_signature = self.is_immutable_instruction_to_pattern(instruction); + + if total_operand_size <= 0 && operands.len() > 0 { + return Err(Error::new(ErrorKind::Other, format!("Instruction -> 0x{:x}: instruction has operands but missing operand sizes", instruction.address()))); + } + + for operand in operands { + if let ArchOperand::X86Operand(op) = operand { + let should_wildcard = match op.op_type { + X86OperandType::Imm(_) => is_immutable_signature, + X86OperandType::Mem(mem) => { + mem.index() == RegId(0) + }, + _ => false, + }; + + let displacement_size = match op.op_type { + X86OperandType::Mem(op_mem) => { + Disassembler::get_displacement_size(op_mem.disp() as u64) * 8 + }, + _ => 0, + }; + + let mut op_size = if (op.size as usize) > displacement_size { + op.size as usize + } else { + displacement_size + }; + + if op_size > instruction_size { + op_size = op.size as usize; + } + + if op_size > instruction_size { + Disassembler::print_instruction(instruction); + return Err(Error::new(ErrorKind::Other, format!("Instruction -> 0x{:x}: instruction operand size exceeds instruction size", instruction.address()))); + } + + let operand_offset = instruction_size - op_size; + + if should_wildcard { + for i in 0..op_size as usize { + if operand_offset + i > wildcarded.len() { + Disassembler::print_instruction(instruction); + return Err(Error::new(ErrorKind::Other, format!("Instruction -> 0x{:x}: instruction wildcard index is out of bounds", instruction.address()))); + } + wildcarded[operand_offset + i] = true; + } + } + } + } + + let instruction_hex = Binary::to_hex(instruction.bytes()); + + if instruction_hex.len() % 2 != 0 { + return Err(Error::new(ErrorKind::Other, format!("Instruction -> 0x{:x}: instruction hex string length is not even", instruction.address()))); + } + + let signature: String = instruction_hex + .chars() + .enumerate() + .map(|(index, ch)| { + let start = index * 4; + let end = start + 4; + if start >= instruction_trailing_null_offset && is_immutable_signature { + '?' + } else if wildcarded[start..end].iter().all(|&x| x) { + '?' + } else { + ch + } + }) + .collect(); + + if signature.len() % 2 != 0 { + return Err(Error::new(ErrorKind::Other, format!("Instruction -> 0x{:x}: wildcarded hex string length is not even", instruction.address()))); + } + + if instruction_hex.len() != signature.len() { + return Err(Error::new(ErrorKind::Other, format!("Instruction -> 0x{:x}: instruction hex length not same as wildcard hex length", instruction.address()))); + } + + return Ok(signature); + + } + + fn get_displacement_size(displacement: u64) -> usize { + match displacement { + 0x00..=0xFF => 1, + 0x100..=0xFFFF => 2, + 0x10000..=0xFFFFFFFF => 4, + _ => 8, + } + } + + #[allow(dead_code)] + pub fn get_jump_immutable(&self, instruction: &Insn) -> Option { + if Disassembler::is_jump_instruction(instruction) { + let operand = match self.get_instruction_operand(instruction, 0) { + Ok(operand) => operand, + Err(_error) => return None, + }; + return Disassembler::get_operand_immutable(&operand); + } + None + } + + #[allow(dead_code)] + pub fn get_conditional_jump_immutable(&self, instruction: &Insn) -> Option { + if Disassembler::is_conditional_jump_instruction(instruction) { + let operand = match self.get_instruction_operand(instruction, 0) { + Ok(operand) => operand, + Err(_error) => return None, + }; + return Disassembler::get_operand_immutable(&operand); + } + None + } + + #[allow(dead_code)] + pub fn get_unconditional_jump_immutable(&self, instruction: &Insn) -> Option { + if Disassembler::is_unconditional_jump_instruction(instruction) { + let operand = match self.get_instruction_operand(instruction, 0) { + Ok(operand) => operand, + Err(_error) => return None, + }; + return Disassembler::get_operand_immutable(&operand); + } + None + } + + #[allow(dead_code)] + pub fn get_instruction_executable_addresses(&self, instruction: &Insn) -> Option { + if !Disassembler::is_load_address_instruction(instruction) { + return None; + } + let operands = match self.get_instruction_operands(instruction) { + Ok(operands) => operands, + Err(_) => return None, + }; + for operand in operands { + if let ArchOperand::X86Operand(operand) = operand { + if let X86OperandType::Mem(mem) = operand.op_type { + if mem.base() != RegId(X86_REG_RIP as u16) { continue; } + if mem.index() != RegId(0) { continue; } + let address: u64 = (instruction.address() as i64 + mem.disp() + instruction.bytes().len() as i64) as u64; + if !self.is_executable_address(address) { continue; } + if self.disassemble_instructions(address, 1).is_err() { continue; } + return Some(address); + } + } + } + None + } + + #[allow(dead_code)] + pub fn get_call_immutable(&self, instruction: &Insn) -> Option { + if Disassembler::is_call_instruction(instruction) { + let operand = match self.get_instruction_operand(instruction, 0) { + Ok(operand) => operand, + Err(_error) => return None, + }; + return Disassembler::get_operand_immutable(&operand); + } + None + } + + #[allow(dead_code)] + pub fn get_operand_immutable(op: &ArchOperand) -> Option { + if let ArchOperand::X86Operand(op) = op { + if let X86OperandType::Imm(imm) = op.op_type { + return Some(imm as u64); + } + } + None + } + + #[allow(dead_code)] + pub fn get_instruction_operands(&self, instruction: &Insn) -> Result, Error> { + let detail = match self.cs.insn_detail(&instruction) { + Ok(detail) => detail, + Err(_error) => return Err(Error::new(ErrorKind::Other, "failed to get instruction detail")), + }; + let arch = detail.arch_detail(); + return Ok(arch.operands()); + } + + #[allow(dead_code)] + pub fn get_instruction_operand(&self, instruction: &Insn, index: usize) -> Result { + let operands = match self.get_instruction_operands(instruction) { + Ok(operands) => operands, + Err(error) => return Err(error), + }; + let operand = match operands.get(index) { + Some(operand) => operand.clone(), + None => return Err(Error::new(ErrorKind::Other, "failed to get instruction operand")) + }; + return Ok(operand); + } + + #[allow(dead_code)] + pub fn print_instructions(instructions: &Instructions) { + for instruction in instructions.iter() { + Disassembler::print_instruction(&instruction); + } + } + + #[allow(dead_code)] + pub fn get_instruction_edges(&self, instruction: &Insn) -> usize { + if Disassembler::is_unconditional_jump_instruction(instruction) { + return 1; + } + if Disassembler::is_return_instruction(instruction) { + return 1; + } + if Disassembler::is_conditional_jump_instruction(instruction){ + return 2; + } + return 0; + } + + #[allow(dead_code)] + pub fn is_immutable_instruction_to_pattern(&self, instruction: &Insn) -> bool { + + if !self.instruction_contains_immutable_operand(instruction) { + return false; + } + + if Disassembler::is_call_instruction(instruction) || Disassembler::is_jump_instruction(instruction) { + return true; + } + + const STACK_INSTRUCTIONS: [InsnId; 5] = [ + InsnId(X86Insn::X86_INS_MOV as u32), + InsnId(X86Insn::X86_INS_SUB as u32), + InsnId(X86Insn::X86_INS_ADD as u32), + InsnId(X86Insn::X86_INS_INC as u32), + InsnId(X86Insn::X86_INS_DEC as u32), + ]; + + if STACK_INSTRUCTIONS.contains(&instruction.id()) { + let operands = match self.get_instruction_operands(instruction) { + Ok(operands) => operands, + Err(_) => return false, + }; + + for operand in operands { + if let ArchOperand::X86Operand(op) = operand { + if let X86OperandType::Reg(register_id) = op.op_type { + if [X86_REG_RSP, X86_REG_RBP, X86_REG_ESP, X86_REG_EBP].contains(&(register_id.0 as u32)) { + return true; + } + } + } + } + } + + return false; + } + + #[allow(dead_code)] + pub fn is_unsupported_pattern_instruction(instruction: &Insn) -> bool { + vec![ + InsnId(X86Insn::X86_INS_MOVUPS as u32), + InsnId(X86Insn::X86_INS_MOVAPS as u32), + InsnId(X86Insn::X86_INS_XORPS as u32), + ].contains(&instruction.id()) + } + + #[allow(dead_code)] + pub fn is_return_instruction(instruction: &Insn) -> bool { + vec![ + InsnId(X86Insn::X86_INS_RET as u32), + InsnId(X86Insn::X86_INS_RETF as u32), + InsnId(X86Insn::X86_INS_RETFQ as u32), + InsnId(X86Insn::X86_INS_IRET as u32), + InsnId(X86Insn::X86_INS_IRETD as u32), + InsnId(X86Insn::X86_INS_IRETQ as u32), + ].contains(&instruction.id()) + } + + #[allow(dead_code)] + pub fn is_privilege_instruction(instruction: &Insn) -> bool { + vec![ + InsnId(X86Insn::X86_INS_HLT as u32), + InsnId(X86Insn::X86_INS_IN as u32), + InsnId(X86Insn::X86_INS_INSB as u32), + InsnId(X86Insn::X86_INS_INSW as u32), + InsnId(X86Insn::X86_INS_INSD as u32), + InsnId(X86Insn::X86_INS_OUT as u32), + InsnId(X86Insn::X86_INS_OUTSB as u32), + InsnId(X86Insn::X86_INS_OUTSW as u32), + InsnId(X86Insn::X86_INS_OUTSD as u32), + InsnId(X86Insn::X86_INS_RDMSR as u32), + InsnId(X86Insn::X86_INS_WRMSR as u32), + InsnId(X86Insn::X86_INS_RDPMC as u32), + InsnId(X86Insn::X86_INS_RDTSC as u32), + InsnId(X86Insn::X86_INS_LGDT as u32), + InsnId(X86Insn::X86_INS_LLDT as u32), + InsnId(X86Insn::X86_INS_LTR as u32), + InsnId(X86Insn::X86_INS_LMSW as u32), + InsnId(X86Insn::X86_INS_CLTS as u32), + InsnId(X86Insn::X86_INS_INVD as u32), + InsnId(X86Insn::X86_INS_INVLPG as u32), + InsnId(X86Insn::X86_INS_WBINVD as u32), + ].contains(&instruction.id()) + } + + #[allow(dead_code)] + pub fn is_wildcard_instruction(instruction: &Insn) -> bool { + Disassembler::is_nop_instruction(instruction) + || Disassembler::is_trap_instruction(instruction) + } + + #[allow(dead_code)] + pub fn is_nop_instruction(instruction: &Insn) -> bool { + vec![ + InsnId(X86Insn::X86_INS_NOP as u32), + InsnId(X86Insn::X86_INS_FNOP as u32), + ].contains(&instruction.id()) + } + + #[allow(dead_code)] + pub fn is_trap_instruction(instruction: &Insn) -> bool { + vec![ + InsnId(X86Insn::X86_INS_INT3 as u32), + InsnId(X86Insn::X86_INS_UD2 as u32), + InsnId(X86Insn::X86_INS_INT1 as u32), + InsnId(X86Insn::X86_INS_INTO as u32), + ].contains(&instruction.id()) + } + + #[allow(dead_code)] + pub fn is_jump_instruction(instruction: &Insn) -> bool { + if Disassembler::is_conditional_jump_instruction(instruction){ + return true; + } + if Disassembler::is_unconditional_jump_instruction(instruction){ + return true; + } + return false; + } + + #[allow(dead_code)] + pub fn is_load_address_instruction(instruction: &Insn) -> bool { + vec![ + InsnId(X86Insn::X86_INS_LEA as u32), + ].contains(&instruction.id()) + } + + #[allow(dead_code)] + pub fn is_call_instruction(instruction: &Insn) -> bool { + vec![ + InsnId(X86Insn::X86_INS_CALL as u32), + InsnId(X86Insn::X86_INS_LCALL as u32), + ].contains(&instruction.id()) + } + + #[allow(dead_code)] + pub fn is_unconditional_jump_instruction(instruction: &Insn) -> bool { + vec![ + InsnId(X86Insn::X86_INS_JMP as u32), + ].contains(&instruction.id()) + } + + #[allow(dead_code)] + pub fn is_conditional_jump_instruction(instruction: &Insn) -> bool { + vec![ + InsnId(X86Insn::X86_INS_JNE as u32), + InsnId(X86Insn::X86_INS_JNO as u32), + InsnId(X86Insn::X86_INS_JNP as u32), + InsnId(X86Insn::X86_INS_JL as u32), + InsnId(X86Insn::X86_INS_JLE as u32), + InsnId(X86Insn::X86_INS_JG as u32), + InsnId(X86Insn::X86_INS_JGE as u32), + InsnId(X86Insn::X86_INS_JE as u32), + InsnId(X86Insn::X86_INS_JECXZ as u32), + InsnId(X86Insn::X86_INS_JCXZ as u32), + InsnId(X86Insn::X86_INS_JB as u32), + InsnId(X86Insn::X86_INS_JBE as u32), + InsnId(X86Insn::X86_INS_JA as u32), + InsnId(X86Insn::X86_INS_JAE as u32), + InsnId(X86Insn::X86_INS_JNS as u32), + InsnId(X86Insn::X86_INS_JO as u32), + InsnId(X86Insn::X86_INS_JP as u32), + InsnId(X86Insn::X86_INS_JRCXZ as u32), + InsnId(X86Insn::X86_INS_JS as u32), + InsnId(X86Insn::X86_INS_LOOPE as u32), + InsnId(X86Insn::X86_INS_LOOPNE as u32), + ].contains(&instruction.id()) + } + + pub fn print_instruction(instruction: &Insn) { + println!( + "0x{:x}: {} {} {}", + instruction.address(), + instruction + .bytes() + .iter() + .map(|byte| format!("{:02x}", byte)) + .collect::>() + .join(" "), + instruction.mnemonic().unwrap_or(""), + instruction.op_str().unwrap_or(""), + ); + } + + pub fn disassemble_instructions(&self, address: u64, count: u64) -> Result, Error> { + if (address as usize) >= self.image.len() { + return Err(Error::new(ErrorKind::Other, "address out of bounds")); + } + let instructions = self + .cs + .disasm_count(&self.image[address as usize..], address, count as usize) + .map_err(|_| Error::new(ErrorKind::Other, "failed to disassemble instructions"))?; + if instructions.len() <= 0 { + return Err(Error::new(ErrorKind::Other, "no instructions found")); + } + Ok(instructions) + } + + fn cs_new(machine: Architecture, detail: bool) -> Result { + match machine { + Architecture::AMD64 => { + Capstone::new() + .x86() + .mode(arch::x86::ArchMode::Mode64) + .syntax(arch::x86::ArchSyntax::Intel) + .detail(detail) + .build() + .map_err(|e| Error::new(ErrorKind::Other, format!("capstone error: {:?}", e))) + }, + Architecture::I386 => { + Capstone::new() + .x86() + .mode(arch::x86::ArchMode::Mode32) + .syntax(arch::x86::ArchSyntax::Intel) + .detail(detail) + .build() + .map_err(|e| Error::new(ErrorKind::Other, format!("capstone error: {:?}", e))) + }, + _ => Err(Error::new(ErrorKind::Other, "unsupported architecture")) + } + } +} diff --git a/src/disassemblers/capstone/mod.rs b/src/disassemblers/capstone/mod.rs new file mode 100644 index 00000000..6a0177a7 --- /dev/null +++ b/src/disassemblers/capstone/mod.rs @@ -0,0 +1,3 @@ +pub mod disassembler; + +pub use disassembler::Disassembler; diff --git a/src/disassemblers/custom/cil/disassembler.rs b/src/disassemblers/custom/cil/disassembler.rs new file mode 100644 index 00000000..8d87dac3 --- /dev/null +++ b/src/disassemblers/custom/cil/disassembler.rs @@ -0,0 +1,210 @@ +use std::io::Error; +use std::io::ErrorKind; +use crate::Architecture; +use std::collections::BTreeMap; +use crate::controlflow::Graph; +use crate::controlflow::Instruction as CFGInstruction; +use crate::disassemblers::custom::cil::Instruction; +use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; +use rayon::ThreadPoolBuilder; +use std::collections::BTreeSet; +use crate::io::Stderr; + +pub struct Disassembler <'disassembler> { + pub architecture: Architecture, + pub executable_address_ranges: BTreeMap, + pub image: &'disassembler [u8] +} + +impl <'disassembler> Disassembler <'disassembler> { + pub fn new(architecture: Architecture, image: &'disassembler[u8], executable_address_rannges: BTreeMap) -> Result { + match architecture { + Architecture::CIL => {}, + _ => { + return Err(Error::new(ErrorKind::Unsupported, "unsupported architecture")); + } + } + Ok(Self { + architecture: architecture, + executable_address_ranges: executable_address_rannges, + image: image + }) + } + + pub fn is_executable_address(&self, address: u64) -> bool { + self.executable_address_ranges + .iter() + .any(|(start, end)| address >= *start && address <= *end) + } + + pub fn disassemble_instruction(&self, address: u64, cfg: &mut Graph) -> Result { + + cfg.instructions.insert_processed(address); + + if self.is_executable_address(address) == false { + cfg.instructions.insert_invalid(address); + return Err(Error::new(ErrorKind::InvalidData, format!("0x{:x}: instruction address is not executable", address))); + } + + let instruction = match Instruction::new(&self.image[address as usize..], address) { + Ok(instruction) => instruction, + Err(_) => { + cfg.instructions.insert_invalid(address); + return Err(Error::new(ErrorKind::Unsupported, format!("0x{:x}: failed to disassemble instruction", address))); + } + }; + + let mut cfginstruction = CFGInstruction::create(address, self.architecture, cfg.config.clone()); + + cfginstruction.bytes = instruction.bytes(); + cfginstruction.is_call = instruction.is_call(); + cfginstruction.is_jump = instruction.is_jump(); + cfginstruction.is_conditional = instruction.is_conditional_jump(); + cfginstruction.is_return = instruction.is_return(); + cfginstruction.is_trap = false; + cfginstruction.pattern = instruction.pattern(); + cfginstruction.edges = instruction.edges(); + cfginstruction.to = instruction.to(); + + Stderr::print_debug( + cfg.config.clone(), + format!( + "0x{:x}: mnemonic: {:?}, mnemonic_size: {}, operand_size: {}, operand_bytes: {:?}, bytes: {:?}, next: {:?}, to: {:?}, blocks: {:?}", + instruction.address, + instruction.mnemonic, + instruction.mnemonic_size(), + instruction.operand_size(), + instruction.operand_bytes(), + instruction.bytes(), + instruction.next(), + instruction.to(), + cfginstruction.blocks(), + ) + ); + + cfg.insert_instruction(cfginstruction); + + cfg.instructions.insert_valid(address); + + Ok(address) + } + + pub fn disassemble_block(&self, address: u64, cfg: &mut Graph) -> Result { + cfg.blocks.insert_processed(address); + + if self.is_executable_address(address) == false { + return Err(Error::new(ErrorKind::InvalidData, format!("0x{:x}: block address is not executable", address))); + } + + let mut pc = address; + + loop { + if let Err(error) = self.disassemble_instruction(pc, cfg) { + cfg.blocks.insert_invalid(address); + return Err(error); + } + + let mut instruction = match cfg.get_instruction(pc) { + Some(instruction) => instruction, + None => { + cfg.blocks.insert_invalid(address); + return Err(Error::new(ErrorKind::InvalidData, format!("0x{:x}: failed to disassemble instruction", pc))); + } + }; + + if instruction.address == address { + instruction.is_block_start = true; + cfg.update_instruction(instruction.clone()); + } + + let is_block_start = instruction.address != address && instruction.is_block_start; + + if instruction.is_trap || instruction.is_return || instruction.is_jump || is_block_start { + break; + } + + pc += instruction.size() as u64; + } + + cfg.blocks.insert_valid(address); + + Ok(pc) + } + + pub fn disassemble_function(&self, address: u64, cfg: &mut Graph) -> Result { + cfg.functions.insert_processed(address); + + if self.is_executable_address(address) == false { + return Err(Error::new(ErrorKind::InvalidData, format!("0x{:x}: function address is not executable", address))); + } + + cfg.blocks.enqueue(address); + + while let Some(block_start_address) = cfg.blocks.dequeue() { + if cfg.blocks.is_processed(block_start_address) { continue; } + + let block_end_address = self + .disassemble_block(block_start_address, cfg) + .map_err(|error| { + cfg.functions.insert_invalid(address); + error + })?; + + if block_start_address == address { + if let Some(mut instruction) = cfg.get_instruction(block_start_address) { + instruction.is_function_start = true; + cfg.update_instruction(instruction); + } + } + + if let Some(instruction) = cfg.get_instruction(block_end_address) { + cfg.blocks.enqueue_extend(instruction.blocks()); + } + } + + cfg.functions.insert_valid(address); + + Ok(address) + } + + pub fn disassemble_controlflow<'a>(&'a self, addresses: BTreeSet, cfg: &'a mut Graph) -> Result<(), Error> { + + let pool = ThreadPoolBuilder::new() + .num_threads(cfg.config.general.threads) + .build() + .map_err(|error| Error::new(ErrorKind::Other, format!("{}", error)))?; + + cfg.functions.enqueue_extend(addresses); + + let external_image = self.image; + + let external_machine = self.architecture.clone(); + + let external_executable_address_ranges = self.executable_address_ranges.clone(); + + pool.install(|| { + while !cfg.functions.queue.is_empty() { + let function_addresses = cfg.functions.dequeue_all(); + cfg.functions.insert_processed_extend(function_addresses.clone()); + let graphs: Vec = function_addresses + .par_iter() + .map(|address| { + let machine = external_machine.clone(); + let executable_address_ranges = external_executable_address_ranges.clone(); + let image = external_image; + let mut graph = Graph::new(machine, cfg.config.clone()); + if let Ok(disasm) = Disassembler::new(machine, image, executable_address_ranges) { + let _ = disasm.disassemble_function(*address, &mut graph); + } + graph + }) + .collect(); + for mut graph in graphs { + cfg.absorb(&mut graph); + } + } + }); + + return Ok(()); + } +} diff --git a/src/disassemblers/custom/cil/instruction.rs b/src/disassemblers/custom/cil/instruction.rs new file mode 100644 index 00000000..16f4fb00 --- /dev/null +++ b/src/disassemblers/custom/cil/instruction.rs @@ -0,0 +1,224 @@ +use std::io::Error; +use crate::disassemblers::custom::cil::Mnemonic; +use std::collections::BTreeSet; +use crate::Binary; + +pub struct Instruction <'instruction> { + pub mnemonic: Mnemonic, + bytes: &'instruction [u8], + pub address: u64, +} + +impl <'instruction> Instruction <'instruction> { + pub fn new(bytes: &'instruction [u8], address: u64) -> Result { + let mnemonic = Mnemonic::from_bytes(bytes)?; + Ok(Self {mnemonic, bytes, address}) + } + + pub fn pattern(&self) -> String { + if self.is_wildcard() { return "??".repeat(self.size()); } + let mut pattern = Binary::to_hex(&self.mnemonic_bytes()); + pattern.push_str(&"??".repeat(self.operand_size())); + pattern + } + + pub fn mnemonic_bytes(&self) -> Vec { + let mut result = Vec::::new(); + for byte in &self.bytes[..self.mnemonic_size()] { + result.push(*byte); + } + result + } + + pub fn bytes(&self) -> Vec { + let mut result = Vec::::new(); + for byte in &self.bytes[..self.mnemonic_size() + self.operand_size()] { + result.push(*byte); + } + result + } + + pub fn operand_bytes(&self) -> Vec { + let mut result = Vec::::new(); + for byte in &self.bytes[self.mnemonic_size()..self.mnemonic_size() + self.operand_size()] { + result.push(*byte); + } + result + } + + pub fn edges(&self) -> usize { + if self.is_unconditional_jump() { + return 1; + } + if self.is_return() { + return 1; + } + if self.is_conditional_jump() { + return 2; + } + return 0; + } + + pub fn size(&self) -> usize { + self.mnemonic_size() + self.operand_size() + } + + pub fn operand_size(&self) -> usize { + if self.is_switch() { + let count = self.bytes + .get(self.mnemonic_size()..self.mnemonic_size() + 4) + .and_then(|bytes| bytes.try_into().ok()) + .map(u32::from_le_bytes) + .map(|v| v as u32).unwrap(); + return 4 + (count as usize * 4); + } + self.mnemonic.operand_size() / 8 + } + + pub fn mnemonic_size(&self) -> usize { + if self.mnemonic as u16 >> 8 == 0xfe { + return 2; + } + return 1; + } + + pub fn is_wildcard(&self) -> bool { + self.is_nop() + } + + pub fn is_nop(&self) -> bool { + match self.mnemonic { + Mnemonic::Nop => true, + _ => false, + } + } + + pub fn is_jump(&self) -> bool { + self.is_conditional_jump() || self.is_unconditional_jump() + } + + pub fn next(&self) -> Option { + if self.is_unconditional_jump() || self.is_return() || self.is_switch() { return None; } + Some(self.address + self.size() as u64) + } + + pub fn to(&self) -> BTreeSet { + let mut result = BTreeSet::::new(); + + if self.is_switch() { + let address = self.address as i64; + let count = self.bytes + .get(self.mnemonic_size()..self.mnemonic_size() + 4) + .and_then(|bytes| bytes.try_into().ok()) + .map(u32::from_le_bytes) + .map(|v| v as u32).unwrap(); + for index in 1..=count { + let start = self.mnemonic_size() + (index as usize * 4); + let end = start + 4; + + let relative_offset = self.bytes + .get(start..end) + .and_then(|bytes| bytes.try_into().ok()) + .map(i32::from_le_bytes) + .unwrap(); + + result.insert( + address.wrapping_add(relative_offset as i64) as u64 + + self.size() as u64, + ); + } + } else if self.is_jump() { + let operand_bytes = self.operand_bytes(); + let address = self.address as i64; + let relative_offset = match self.operand_size() { + 1 => { + operand_bytes.get(0).map(|&b| i8::from_le_bytes([b]) as i64) + } + 2 => { + operand_bytes + .get(..2) + .and_then(|bytes| bytes.try_into().ok()) + .map(i16::from_le_bytes) + .map(|v| v as i64) + } + 4 => { + operand_bytes + .get(..4) + .and_then(|bytes| bytes.try_into().ok()) + .map(i32::from_le_bytes) + .map(|v| v as i64) + } + _ => None, + }; + if let Some(relative) = relative_offset { + result.insert(address.wrapping_add(relative) as u64 + self.size() as u64); + } + } + result + } + + pub fn is_switch(&self) -> bool { + match self.mnemonic { + Mnemonic::Switch => true, + _ => false, + } + } + + pub fn is_conditional_jump(&self) -> bool { + match self.mnemonic { + Mnemonic::BrFalse => true, + Mnemonic::BrFalseS => true, + Mnemonic::BrTrue => true, + Mnemonic::BrTrueS => true, + Mnemonic::BneUn => true, + Mnemonic::BneUnS => true, + Mnemonic::Blt => true, + Mnemonic::BltS => true, + Mnemonic::BltUn => true, + Mnemonic::BltUnS => true, + Mnemonic::Beq => true, + Mnemonic::BeqS => true, + Mnemonic::Bge => true, + Mnemonic::BgeS => true, + Mnemonic::BgeUn => true, + Mnemonic::BgeUnS => true, + Mnemonic::Bgt => true, + Mnemonic::BgtS => true, + Mnemonic::BgtUn => true, + Mnemonic::BgtUnS => true, + Mnemonic::Ble => true, + Mnemonic::BleS => true, + Mnemonic::BleUn => true, + Mnemonic::BleUnS => true, + _ => false, + } + } + + pub fn is_return(&self) -> bool { + match self.mnemonic { + Mnemonic::Ret => true, + Mnemonic::Throw => true, + _ => false, + } + } + + pub fn is_call(&self) -> bool { + match self.mnemonic { + Mnemonic::Call => true, + Mnemonic::CallI => true, + Mnemonic::CallVirt => true, + _ => false, + } + } + + pub fn is_unconditional_jump(&self) -> bool { + match self.mnemonic { + Mnemonic::Br => true, + Mnemonic::Jmp => true, + Mnemonic::BrS => true, + Mnemonic::Leave => true, + Mnemonic::LeaveS => true, + _ => false, + } + } +} diff --git a/src/disassemblers/custom/cil/mnemonic.rs b/src/disassemblers/custom/cil/mnemonic.rs new file mode 100644 index 00000000..fe4156d7 --- /dev/null +++ b/src/disassemblers/custom/cil/mnemonic.rs @@ -0,0 +1,698 @@ +use std::io::Error; +use std::io::ErrorKind; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Mnemonic { + Add = 0x58, + AddOvf = 0xD6, + AddOvfUn = 0xD7, + And = 0x5F, + Beq = 0x3B, + BeqS = 0x2E, + Bge = 0x3C, + BgeS = 0x2F, + BgeUn = 0x41, + BgeUnS = 0x34, + Bgt = 0x3D, + BgtS = 0x30, + BgtUn = 0x42, + BgtUnS = 0x35, + Ble = 0x3E, + BleS = 0x31, + BleUn = 0x43, + BleUnS = 0x36, + Blt = 0x3F, + BltS = 0x32, + BltUn = 0x44, + BltUnS = 0x37, + BneUn = 0x40, + BneUnS = 0x33, + Box = 0x8C, + Br = 0x38, + BrS = 0x2B, + Break = 0x01, + BrFalse = 0x39, + BrFalseS = 0x2C, + BrTrue = 0x3A, + BrTrueS = 0x2D, + Call = 0x28, + CallI = 0x29, + CallVirt = 0x6F, + CastClass = 0x74, + CkInite = 0xC3, + ConvI = 0xD3, + ConvI1 = 0x67, + ConvI2 = 0x68, + ConvI4 = 0x69, + ConvI8 = 0x6A, + ConvOvfI = 0xD4, + ConvOvfIUn = 0x8A, + ConvOvfI1 = 0xB3, + ConvOvfI1Un = 0x82, + ConvOvfI2 = 0xB5, + ConvOvfI2Un = 0x83, + ConvOvfI4 = 0xB7, + ConvOvfI4Un = 0x84, + ConvOvfI8 = 0xB9, + ConvOvfI8Un = 0x85, + ConvOvfU = 0xD5, + ConvOvfUUn = 0x8B, + ConvOvfU1 = 0xB4, + ConvOvfU1Un = 0x86, + ConvOvfU2 = 0xB6, + ConvOvfU2Un = 0x87, + ConvOvfU4 = 0xB8, + ConvOvfU4Un = 0x88, + ConvOvfU8 = 0xBA, + ConvOvfU8Un = 0x89, + ConvRUn = 0x76, + ConvR4 = 0x6B, + ConvR8 = 0x6C, + ConvU = 0xE0, + ConvU1 = 0xD2, + ConvU2 = 0xD1, + ConvU4 = 0x6D, + ConvU8 = 0x6E, + Cpobj = 0x70, + Div = 0x5B, + DivUn = 0x5C, + DUP = 0x25, + End = 0xDC, + IsInst = 0x75, + Jmp = 0x27, + LdArg0 = 0x02, + LdArg1 = 0x03, + LdArg2 = 0x04, + LdArg3 = 0x05, + LdArgS = 0x0E, + LdArgAS = 0x0F, + LdcI4 = 0x20, + LdcI40 = 0x16, + LdcI41 = 0x17, + LdcI42 = 0x18, + LdcI43 = 0x19, + LdcI44 = 0x1A, + LdcI45 = 0x1B, + LdcI46 = 0x1C, + LdcI47 = 0x1D, + LdcI48 = 0x1E, + LdcI4M1 = 0x15, + LdcI4S = 0x1F, + LdcI8 = 0x21, + LdcR4 = 0x22, + LdcR8 = 0x23, + LdElm = 0xA3, + LdElmI = 0x97, + LdElmI1 = 0x90, + LdElmI2 = 0x92, + LdElmI4 = 0x94, + LdElmU8 = 0x96, + LdElmR4 = 0x98, + LdElmR8 = 0x99, + LdElmRef = 0x9A, + LdElmU1 = 0x91, + LdElmU2 = 0x93, + LdElmU4 = 0x95, + LdElmA = 0x8F, + LdFld = 0x7B, + LdFldA = 0x7C, + LdIndI = 0x4D, + LdIndI1 = 0x46, + LdIndI2 = 0x48, + LdIndI4 = 0x4A, + LdIndU8 = 0x4C, + LdIndR4 = 0x4E, + LdIndR8 = 0x4F, + LdIndRef = 0x50, + LdIndU1 = 0x47, + LdIndU2 = 0x49, + LdIndU4 = 0x4B, + LdLen = 0x8E, + LdLoc0 = 0x06, + LdLoc1 = 0x07, + LdLoc2 = 0x08, + LdLoc3 = 0x09, + LdLocS = 0x11, + LdLocAS = 0x12, + LdNull = 0x14, + LdObj = 0x71, + LdSFld = 0x7E, + LdSFldA = 0x7F, + LdStr = 0x72, + LdToken = 0xD0, + Leave = 0xDD, + LeaveS = 0xDE, + MkRefAny = 0xC6, + Mul = 0x5A, + MulOvf = 0xD8, + MulOvfUn = 0xD9, + Neg = 0x65, + NewArr = 0x8D, + NewObj = 0x73, + Nop = 0x00, + Not = 0x66, + Or = 0x60, + Pop = 0x26, + RefAnyVal = 0xC2, + Rem = 0x5D, + RemUn = 0x5E, + Ret = 0x2A, + Shl = 0x62, + Shr = 0x63, + ShrUn = 0x64, + StArgS = 0x10, + StElem = 0xA4, + StElemI = 0x9B, + StElemI1 = 0x9C, + StElemI2 = 0x9D, + StElemI4 = 0x9E, + StElemI8 = 0x9F, + StElemR4 = 0xA0, + StElemR8 = 0xA1, + StElemREF = 0xA2, + StFld = 0x7D, + StIndI = 0xDF, + StIndI1 = 0x52, + StIndI2 = 0x53, + StIndI4 = 0x54, + StIndI8 = 0x55, + StIndR4 = 0x56, + StIndR8 = 0x57, + StIndRef = 0x51, + StLoc0 = 0x0A, + StLoc1 = 0x0B, + StLoc2 = 0x0C, + StLoc3 = 0x0D, + StObj = 0x81, + StSFld = 0x80, + Sub = 0x59, + SubOvf = 0xDA, + SubOvfUn = 0xDB, + Switch = 0x45, + Throw = 0x7A, + Unbox = 0x79, + UnboxAny = 0xA5, + Xor = 0x61, + StLocS = 0x13, + ArgList = 0xfe00, + Ceq = 0xfe01, + Cgt = 0xfe02, + CgtUn = 0xfe03, + Clt = 0xfe04, + CltUn = 0xfe05, + Constrained = 0xfe16, + CpBlk = 0xfe17, + EndFilter = 0xfe11, + InitBlk = 0xfe18, + InitObj = 0xfe15, + LdArg = 0xfe09, + LdArgA = 0xfe0a, + LdFtn = 0xfe06, + LdLoc = 0xfe0c, + LdLocA = 0xfe0d, + LdVirtFtn = 0xfe07, + LocAlloc = 0xfe0f, + No = 0xfe19, + ReadOnly = 0xfe1e, + RefAnyType = 0xfe1d, + ReThrow = 0xfe1a, + SizeOf = 0xfe1c, + StArg = 0xfe0b, + SLoc = 0xfe0e, + Tail = 0xfe14, + Unaligned = 0xfe12, + Volatile = 0xfe13, +} + +impl Mnemonic { + pub const fn all_variants() -> &'static [Self] { + &[ + Self::Add, + Self::AddOvf, + Self::AddOvfUn, + Self::And, + Self::Beq, + Self::BeqS, + Self::Bge, + Self::BgeS, + Self::BgeUn, + Self::BgeUnS, + Self::Bgt, + Self::BgtS, + Self::BgtUn, + Self::BgtUnS, + Self::Ble, + Self::BleS, + Self::BleUn, + Self::BleUnS, + Self::Blt, + Self::BltS, + Self::BltUn, + Self::BltUnS, + Self::BneUn, + Self::BneUnS, + Self::Box, + Self::Br, + Self::BrS, + Self::Break, + Self::BrFalse, + Self::BrFalseS, + Self::BrTrue, + Self::BrTrueS, + Self::Call, + Self::CallI, + Self::CallVirt, + Self::CastClass, + Self::CkInite, + Self::ConvI, + Self::ConvI1, + Self::ConvI2, + Self::ConvI4, + Self::ConvI8, + Self::ConvOvfI, + Self::ConvOvfIUn, + Self::ConvOvfI1, + Self::ConvOvfI1Un, + Self::ConvOvfI2, + Self::ConvOvfI2Un, + Self::ConvOvfI4, + Self::ConvOvfI4Un, + Self::ConvOvfI8, + Self::ConvOvfI8Un, + Self::ConvOvfU, + Self::ConvOvfUUn, + Self::ConvOvfU1, + Self::ConvOvfU1Un, + Self::ConvOvfU2, + Self::ConvOvfU2Un, + Self::ConvOvfU4, + Self::ConvOvfU4Un, + Self::ConvOvfU8, + Self::ConvOvfU8Un, + Self::ConvRUn, + Self::ConvR4, + Self::ConvR8, + Self::ConvU, + Self::ConvU1, + Self::ConvU2, + Self::ConvU4, + Self::ConvU8, + Self::Cpobj, + Self::Div, + Self::DivUn, + Self::DUP, + Self::End, + Self::IsInst, + Self::Jmp, + Self::LdArg0, + Self::LdArg1, + Self::LdArg2, + Self::LdArg3, + Self::LdArgS, + Self::LdArgAS, + Self::LdcI4, + Self::LdcI40, + Self::LdcI41, + Self::LdcI42, + Self::LdcI43, + Self::LdcI44, + Self::LdcI45, + Self::LdcI46, + Self::LdcI47, + Self::LdcI48, + Self::LdcI4M1, + Self::LdcI4S, + Self::LdcI8, + Self::LdcR4, + Self::LdcR8, + Self::LdElm, + Self::LdElmI, + Self::LdElmI1, + Self::LdElmI2, + Self::LdElmI4, + Self::LdElmU8, + Self::LdElmR4, + Self::LdElmR8, + Self::LdElmRef, + Self::LdElmU1, + Self::LdElmU2, + Self::LdElmU4, + Self::LdElmA, + Self::LdFld, + Self::LdFldA, + Self::LdIndI, + Self::LdIndI1, + Self::LdIndI2, + Self::LdIndI4, + Self::LdIndU8, + Self::LdIndR4, + Self::LdIndR8, + Self::LdIndRef, + Self::LdIndU1, + Self::LdIndU2, + Self::LdIndU4, + Self::LdLen, + Self::LdLoc0, + Self::LdLoc1, + Self::LdLoc2, + Self::LdLoc3, + Self::LdLocS, + Self::LdLocAS, + Self::LdNull, + Self::LdObj, + Self::LdSFld, + Self::LdSFldA, + Self::LdStr, + Self::LdToken, + Self::Leave, + Self::LeaveS, + Self::MkRefAny, + Self::Mul, + Self::MulOvf, + Self::MulOvfUn, + Self::Neg, + Self::NewArr, + Self::NewObj, + Self::Nop, + Self::Not, + Self::Or, + Self::Pop, + Self::RefAnyVal, + Self::Rem, + Self::RemUn, + Self::Ret, + Self::Shl, + Self::Shr, + Self::ShrUn, + Self::StArgS, + Self::StElem, + Self::StElemI, + Self::StElemI1, + Self::StElemI2, + Self::StElemI4, + Self::StElemI8, + Self::StElemR4, + Self::StElemR8, + Self::StElemREF, + Self::StFld, + Self::StIndI, + Self::StIndI1, + Self::StIndI2, + Self::StIndI4, + Self::StIndI8, + Self::StIndR4, + Self::StIndR8, + Self::StIndRef, + Self::StLoc0, + Self::StLoc1, + Self::StLoc2, + Self::StLoc3, + Self::StObj, + Self::StSFld, + Self::Sub, + Self::SubOvf, + Self::SubOvfUn, + Self::Switch, + Self::Throw, + Self::Unbox, + Self::UnboxAny, + Self::Xor, + Self::StLocS, + Self::ArgList, + Self::Ceq, + Self::Cgt, + Self::CgtUn, + Self::Clt, + Self::CltUn, + Self::Constrained, + Self::CpBlk, + Self::EndFilter, + Self::InitBlk, + Self::InitObj, + Self::LdArg, + Self::LdArgA, + Self::LdFtn, + Self::LdLoc, + Self::LdLocA, + Self::LdVirtFtn, + Self::LocAlloc, + Self::No, + Self::ReadOnly, + Self::RefAnyType, + Self::ReThrow, + Self::SizeOf, + Self::StArg, + Self::SLoc, + Self::Tail, + Self::Unaligned, + Self::Volatile, + ] + } + + pub fn operand_size(&self) -> usize { + match self { + Self::Ceq => 0, + Self::ArgList => 0, + Self::Cgt => 0, + Self::CgtUn => 0, + Self::Clt => 0, + Self::CltUn => 0, + Self::Constrained => 32, + Self::CpBlk => 0, + Self::EndFilter => 0, + Self::InitBlk => 0, + Self::InitObj => 32, + Self::LdArg => 16, + Self::LdArgA => 32, + Self::LdFtn => 32, + Self::LdLoc => 16, + Self::LdLocA => 16, + Self::LdVirtFtn => 32, + Self::LocAlloc => 0, + Self::No => 0, + Self::ReadOnly => 32, + Self::RefAnyType => 0, + Self::ReThrow => 0, + Self::SizeOf => 32, + Self::StArg => 16, + Self::Tail => 0, + Self::Unaligned => 0, + Self::Volatile => 32, + Self::Beq => 32, + Self::BeqS => 8, + Self::Bge => 32, + Self::BgeS => 8, + Self::BgeUn => 32, + Self::BgeUnS => 8, + Self::Bgt => 32, + Self::BgtS => 8, + Self::Ble => 32, + Self::BleS => 8, + Self::BleUn => 32, + Self::BleUnS => 8, + Self::Blt => 32, + Self::BltS => 8, + Self::BltUn => 32, + Self::BltUnS => 8, + Self::Box => 32, + Self::Br => 32, + Self::BrS => 8, + Self::Break => 0, + Self::BrFalse => 32, + Self::BrFalseS => 8, + Self::BrTrue => 32, + Self::BrTrueS => 8, + Self::Add => 0, + Self::AddOvf => 0, + Self::AddOvfUn => 0, + Self::And => 0, + Self::CastClass => 32, + Self::CkInite => 0, + Self::ConvI => 0, + Self::ConvI1 => 0, + Self::ConvI2 => 0, + Self::ConvI4 => 0, + Self::ConvI8 => 0, + Self::ConvOvfI => 0, + Self::ConvOvfIUn => 0, + Self::ConvOvfI1 => 0, + Self::ConvOvfI1Un => 0, + Self::ConvOvfI2 => 0, + Self::ConvOvfI2Un => 0, + Self::ConvOvfI4 => 0, + Self::ConvOvfI4Un => 0, + Self::ConvOvfI8 => 0, + Self::ConvOvfI8Un => 0, + Self::ConvOvfU => 0, + Self::ConvOvfUUn => 0, + Self::ConvOvfU1 => 0, + Self::ConvOvfU1Un => 0, + Self::ConvOvfU2 => 0, + Self::ConvOvfU2Un => 0, + Self::ConvOvfU4 => 0, + Self::ConvOvfU4Un => 0, + Self::ConvOvfU8 => 0, + Self::ConvOvfU8Un => 0, + Self::ConvRUn => 0, + Self::ConvR4 => 0, + Self::ConvR8 => 0, + Self::ConvU => 0, + Self::ConvU1 => 0, + Self::ConvU2 => 0, + Self::ConvU4 => 0, + Self::ConvU8 => 0, + Self::Cpobj => 32, + Self::Div => 0, + Self::DivUn => 0, + Self::DUP => 0, + Self::IsInst => 32, + Self::Jmp => 32, + Self::LdArg0 => 0, + Self::LdArg1 => 0, + Self::LdArg2 => 0, + Self::LdArg3 => 0, + Self::LdArgS => 8, + Self::LdArgAS => 8, + Self::LdcI4 => 32, + Self::LdcI40 => 0, + Self::LdcI41 => 0, + Self::LdcI42 => 0, + Self::LdcI43 => 0, + Self::LdcI44 => 0, + Self::LdcI45 => 0, + Self::LdcI46 => 0, + Self::LdcI47 => 0, + Self::LdcI48 => 0, + Self::LdcI4M1 => 0, + Self::LdcI4S => 8, + Self::LdcI8 => 64, + Self::LdcR4 => 32, + Self::LdcR8 => 64, + Self::LdElm => 32, + Self::LdElmI => 0, + Self::LdElmI1 => 0, + Self::LdElmI2 => 0, + Self::LdElmI4 => 0, + Self::LdElmU8 => 0, + Self::LdElmR4 => 0, + Self::LdElmR8 => 0, + Self::LdElmRef => 0, + Self::LdElmU1 => 0, + Self::LdElmU2 => 0, + Self::LdElmU4 => 0, + Self::LdElmA => 32, + Self::LdFld => 32, + Self::LdFldA => 32, + Self::LdIndI => 0, + Self::LdIndI1 => 0, + Self::LdIndI2 => 0, + Self::LdIndI4 => 0, + Self::LdIndU8 => 0, + Self::LdIndR4 => 0, + Self::LdIndR8 => 0, + Self::LdIndU1 => 0, + Self::LdIndU2 => 0, + Self::LdIndU4 => 0, + Self::LdLen => 0, + Self::LdLoc0 => 0, + Self::LdLoc1 => 0, + Self::LdLoc2 => 0, + Self::LdLoc3 => 0, + Self::LdLocS => 8, + Self::LdLocAS => 8, + Self::LdNull => 0, + Self::LdObj => 32, + Self::LdSFld => 32, + Self::LdSFldA => 32, + Self::LdStr => 32, + Self::LdToken => 32, + Self::Leave => 32, + Self::LeaveS => 8, + Self::MkRefAny => 32, + Self::Mul => 0, + Self::MulOvf => 0, + Self::MulOvfUn => 0, + Self::Neg => 0, + Self::NewArr => 32, + Self::NewObj => 32, + Self::Nop => 0, + Self::Not => 0, + Self::Or => 0, + Self::Pop => 0, + Self::RefAnyVal => 32, + Self::Rem => 0, + Self::RemUn => 0, + Self::Ret => 0, + Self::Shl => 0, + Self::Shr => 0, + Self::ShrUn => 0, + Self::StArgS => 8, + Self::StElem => 32, + Self::StElemI => 0, + Self::StElemI2 => 0, + Self::StElemI4 => 0, + Self::StElemI8 => 0, + Self::StElemR4 => 0, + Self::StElemR8 => 0, + Self::StElemREF => 0, + Self::StFld => 32, + Self::StIndI => 0, + Self::StIndI2 => 0, + Self::StIndI8 => 0, + Self::StIndR4 => 0, + Self::StIndR8 => 0, + Self::StIndRef => 0, + Self::StLocS => 8, + Self::StLoc0 => 0, + Self::StLoc1 => 0, + Self::StLoc2 => 0, + Self::StLoc3 => 0, + Self::StObj => 32, + Self::StSFld => 32, + Self::Sub => 0, + Self::SubOvf => 0, + Self::SubOvfUn => 0, + Self::Switch => 32, + Self::Throw => 0, + Self::Unbox => 32, + Self::UnboxAny => 32, + Self::Xor => 0, + Self::Call => 32, + Self::CallI => 32, + Self::CallVirt => 32, + Self::BneUnS => 8, + Self::BneUn => 32, + Self::BgtUn => 32, + Self::BgtUnS => 8, + _ => 0, + } + } + + pub fn from_bytes(bytes: &[u8]) -> Result { + if bytes.is_empty() { + return Err(Error::new(ErrorKind::Other, "not enough bytes to parse mnemonic")); + } + + let value = bytes[0] as u16; + for &mnemonic in Self::all_variants() { + if (mnemonic as u16) == value { + return Ok(mnemonic); + } + } + + if bytes[0] == 0xfe { + if bytes.len() < 2 { + return Err(Error::new(ErrorKind::Other, "not enough bytes for prefix instruction")); + } + let value = u16::from_be_bytes([bytes[0], bytes[1]]); + for &mnemonic in Self::all_variants() { + if (mnemonic as u16) == value { + return Ok(mnemonic); + } + } + } + + Err(Error::new(ErrorKind::NotFound, "0x{:x}: no matching mnemonic found")) + } + +} diff --git a/src/disassemblers/custom/cil/mod.rs b/src/disassemblers/custom/cil/mod.rs new file mode 100644 index 00000000..e0c2df2e --- /dev/null +++ b/src/disassemblers/custom/cil/mod.rs @@ -0,0 +1,7 @@ +pub mod disassembler; +pub mod mnemonic; +pub mod instruction; + +pub use disassembler::Disassembler; +pub use mnemonic::Mnemonic; +pub use instruction::Instruction; diff --git a/src/disassemblers/custom/mod.rs b/src/disassemblers/custom/mod.rs new file mode 100644 index 00000000..506b91c0 --- /dev/null +++ b/src/disassemblers/custom/mod.rs @@ -0,0 +1 @@ +pub mod cil; diff --git a/src/disassemblers/mod.rs b/src/disassemblers/mod.rs new file mode 100644 index 00000000..c6c1dea1 --- /dev/null +++ b/src/disassemblers/mod.rs @@ -0,0 +1,2 @@ +pub mod capstone; +pub mod custom; diff --git a/src/file.cpp b/src/file.cpp deleted file mode 100644 index 806e6821..00000000 --- a/src/file.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include "file.h" - -using namespace binlex; - -void File::CalculateFileHashes(char *file_path){ - tlsh = GetFileTLSH(file_path); - sha256 = GetFileSHA256(file_path); -} - -void File::CalculateFileHashes(const vector &data){ - tlsh = GetTLSH(&data[0], data.size()); - sha256 = GetSHA256(&data[0], data.size()); -} - -bool File::SetArchitecture(BINARY_ARCH arch, BINARY_MODE mode){ - switch(arch){ - case BINARY_ARCH_X86: - case BINARY_ARCH_X86_64: - binary_arch = arch; - break; - default: - binary_arch = BINARY_ARCH_UNKNOWN; - return false; - } - switch(mode){ - case BINARY_MODE_32: - case BINARY_MODE_64: - case BINARY_MODE_CIL: - binary_mode = mode; - break; - default: - binary_mode = BINARY_MODE_UNKNOWN; - return false; - } - return true; -} - -std::vector File::ReadFileIntoVector(const char *file_path){ - FILE *inp; - uint8_t buf[8192]; - size_t bread; - std::vector data; - - inp = fopen(file_path, "rb"); - if(!inp){ - throw std::runtime_error(strerror(errno)); - } - while((bread = fread(buf, 1, sizeof(buf), inp)) > 0){ - data.insert(data.end(), buf, buf + bread); - } - if(errno != 0) { - throw std::runtime_error(strerror(errno)); - } - fclose(inp); - return data; -} - -bool File::ReadFile(const char *file_path){ - try { - std::vector data_v(ReadFileIntoVector(file_path)); - ReadVector(data_v); - } - catch(const std::exception &err) { - cerr << "error while reading: " << err.what() << endl; - return false; - } - return true; -} - -bool File::ReadBuffer(void *data, size_t size){ - vector data_v((uint8_t *)data, (uint8_t *)data + size); - return ReadVector(data_v); -} diff --git a/src/formats/cli.rs b/src/formats/cli.rs new file mode 100644 index 00000000..23cb825b --- /dev/null +++ b/src/formats/cli.rs @@ -0,0 +1,1188 @@ +use std::mem; + +/// Represents a metadata token type in a .NET metadata structure. +/// +/// The `MetadataToken` enum defines various types of metadata tokens used to identify +/// rows in different metadata tables within a .NET assembly. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum MetadataToken { + Module = 0, + TypeRef = 1, + TypeDef = 2, + FieldPtr = 3, + Field = 4, + MethodPtr = 5, + MethodDef = 6, + ParamPtr = 7, + Param = 8, + InterfaceImpl = 9, + MemberRef = 10, + Constant = 11, + CustomAttribute = 12, + FieldMarshal = 13, + DeclSecurity = 14, + ClassLayout = 15, + FieldLayout = 16, + StandAloneSig = 17, + EventMap = 18, + EventPtr = 19, + Event = 20, + PropertyMap = 21, + PropertyPtr = 22, + Property = 23, + MethodSemantics = 24, + MethodImpl = 25, + ModuleRef = 26, + TypeSpec = 27, + ImplMap = 28, + FieldRva = 29, + EncLog = 30, + EncMap = 31, + Assembly = 32, + AssemblyProcessor = 33, + AssemblyOs = 34, + AssemblyRef = 35, + AssemblyRefProcessor = 36, + AssemblyRefOs = 37, + File = 38, + ExportedType = 39, + ManifestResource = 40, + NestedClass = 41, + GenericParam = 42, + MethodSpec = 43, + GenericParamConstraint = 44, + Document = 48, + MethodDebugInformation = 49, + LocalScope = 50, + LocalVariable = 51, + LocalConstant = 52, + ImportScope = 53, + StateMachineMethod = 54, + CustomDebugInformation = 55, +} + +/// Represents an image data directory in a .NET metadata structure. +/// +/// The `ImageDataDirectory` provides information about a specific data directory, +/// including its virtual address and size. +#[repr(C)] +pub struct ImageDataDirectory { + /// A `u32` value representing the virtual address of the data directory. + pub virtual_address: u32, + /// A `u32` value representing the size of the data directory. + pub size: u32, +} + +/// Represents the anonymous field in the `Cor20Header`, used to define the entry point. +/// +/// The `Cor20Header0` union allows for two possible representations: +/// an entry point token for managed code or an RVA for native code. +#[repr(C)] +pub union Cor20Header0 { + /// A `u32` value representing the entry point token for managed code. + pub entry_point_token: u32, + /// A `u32` value representing the RVA of the entry point for native code. + pub entry_point_rva: u32, +} + +/// Represents the .NET COR20 header in a PE file. +/// +/// The `Cor20Header` provides information about the Common Language Runtime (CLR) +/// metadata, versioning, and related data structures required for .NET assemblies. +#[repr(C)] +pub struct Cor20Header { + /// A `u32` value representing the size of the header in bytes. + pub cb: u32, + /// A `u16` value indicating the major version of the CLR runtime. + pub major_runtime_version: u16, + /// A `u16` value indicating the minor version of the CLR runtime. + pub minor_runtime_version: u16, + /// An `ImageDataDirectory` pointing to the metadata. + pub meta_data: ImageDataDirectory, + /// A `u32` value representing various flags related to the assembly. + pub flags: u32, + /// A `Cor20Header0` union containing either an entry point token or an RVA. + pub anonymous: Cor20Header0, + /// An `ImageDataDirectory` pointing to resources in the assembly. + pub resources: ImageDataDirectory, + /// An `ImageDataDirectory` pointing to the strong name signature. + pub strong_name_signature: ImageDataDirectory, + /// An `ImageDataDirectory` pointing to the code manager table. + pub code_manager_table: ImageDataDirectory, + /// An `ImageDataDirectory` pointing to VTable fixups. + pub vtable_fixups: ImageDataDirectory, + /// An `ImageDataDirectory` pointing to export address table jumps. + pub export_address_table_jumps: ImageDataDirectory, + /// An `ImageDataDirectory` pointing to the managed native header. + pub managed_native_header: ImageDataDirectory, +} + +impl Cor20Header { + /// Parses a `Cor20Header` from a byte slice. + /// + /// This function validates the size and alignment of the byte slice before returning + /// a reference to the `Cor20Header`. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the `Cor20Header` data. + /// + /// # Returns + /// + /// * `Some(&Cor20Header)` - A reference to the parsed `Cor20Header` if the byte slice is valid. + /// * `None` - If the byte slice is invalid, does not contain enough data, or is misaligned. + pub fn from_bytes(bytes: &[u8]) -> Option<&Self> { + if bytes.len() != mem::size_of::() { + return None; + } + if bytes.as_ptr().align_offset(mem::align_of::()) != 0 { + return None; + } + Some(unsafe { &*(bytes.as_ptr() as *const Self) }) + } + + pub fn size() -> usize { + mem::size_of::() + } +} + +/// Represents the storage signature in a .NET metadata structure. +/// +/// The `StorageSignature` contains metadata about the storage, including its signature, +/// version, and additional data fields. +#[repr(C)] +pub struct StorageSignature { + /// A `u32` value representing the storage signature. + pub signature: u32, + /// A `u16` value indicating the major version of the storage. + pub major_version: u16, + /// A `u16` value indicating the minor version of the storage. + pub minor_version: u16, + /// A `u32` value containing additional data. + pub extra_data: u32, + /// A `u32` value specifying the size of the version string. + pub version_string_size: u32, + /// A `u32` value referencing the version string. + pub version_string: u32, +} + +impl StorageSignature { + /// Parses a `StorageSignature` from a byte slice. + /// + /// This function validates the size and alignment of the byte slice before returning + /// a reference to the `StorageSignature`. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the `StorageSignature` data. + /// + /// # Returns + /// + /// * `Some(&StorageSignature)` - A reference to the parsed `StorageSignature` if the byte slice is valid. + /// * `None` - If the byte slice is invalid, does not contain enough data, or is misaligned. + pub fn from_bytes(bytes: &[u8]) -> Option<&Self> { + if bytes.len() != mem::size_of::() { + return None; + } + if bytes.as_ptr().align_offset(mem::align_of::()) != 0 { + return None; + } + Some(unsafe { &*(bytes.as_ptr() as *const Self) }) + } + + pub fn size() -> usize { + mem::size_of::() + } +} + +/// Represents the storage header in a .NET metadata structure. +/// +/// The `StorageHeader` provides metadata about the storage streams, including the number +/// of streams and associated flags. +#[repr(C)] +pub struct StorageHeader { + /// A `u8` value representing the storage flags. + pub flags: u8, + /// A `u8` value for padding (reserved). + pub pad: u8, + /// A `u16` value indicating the number of streams in the storage. + pub number_of_streams: u16, +} + +impl StorageHeader { + /// Parses a `StorageHeader` from a byte slice. + /// + /// This function validates the size and alignment of the byte slice before returning + /// a reference to the `StorageHeader`. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the `StorageHeader` data. + /// + /// # Returns + /// + /// * `Some(&StorageHeader)` - A reference to the parsed `StorageHeader` if the byte slice is valid. + /// * `None` - If the byte slice is invalid, does not contain enough data, or is misaligned. + pub fn from_bytes(bytes: &[u8]) -> Option<&Self> { + if bytes.len() != mem::size_of::() { + return None; + } + if bytes.as_ptr().align_offset(mem::align_of::()) != 0 { + return None; + } + Some(unsafe { &*(bytes.as_ptr() as *const Self) }) + } + + pub fn size() -> usize { + mem::size_of::() + } +} + +/// Represents a stream header in a .NET metadata structure. +/// +/// The `StreamHeader` contains metadata about a stream, including its offset and size, +/// and provides methods to retrieve its name and the total header size. +#[repr(C)] +pub struct StreamHeader { + /// The offset of the stream in the metadata section. + pub offset: u32, + /// The size of the stream in bytes. + pub size: u32, +} + +impl StreamHeader { + /// Parses a `StreamHeader` from a byte slice. + /// + /// This function validates that the byte slice contains enough data for a `StreamHeader` + /// and returns a reference to it. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the `StreamHeader` data. + /// + /// # Returns + /// + /// * `Some(&StreamHeader)` - A reference to the parsed `StreamHeader` if the byte slice is valid. + /// * `None` - If the byte slice is too short to contain a valid `StreamHeader`. + /// + pub fn from_bytes(bytes: &[u8]) -> Option<&Self> { + if bytes.len() < mem::size_of::() { + return None; + } + Some(unsafe { &*(bytes.as_ptr() as *const StreamHeader) }) + } + + /// Retrieves the name of the stream as a byte slice, including any padding. + /// + /// This method locates the null-terminated name string following the `StreamHeader` fields + /// and includes padding up to a 4-byte boundary. + /// + /// # Returns + /// + /// * `&[u8]` - A slice containing the name of the stream with padding. + pub fn name(&self) -> &[u8] { + let header_size = mem::size_of::(); + let base_ptr = self as *const Self as *const u8; + + unsafe { + let name_ptr = base_ptr.add(header_size); + + let mut len = 0; + while *name_ptr.add(len) != 0 { + len += 1; + } + + let padded_len = (len + 4) & !3; + + std::slice::from_raw_parts(name_ptr, padded_len) + } + } + + /// Calculates the total size of the `StreamHeader` including the name and padding. + /// + /// The size includes the fixed fields of the `StreamHeader` and the length of the name + /// (rounded to a 4-byte boundary). + /// + /// # Returns + /// + /// * `usize` - The total size of the `StreamHeader` in bytes. + pub fn size() -> usize { + mem::size_of::() + // let header_size = mem::size_of::(); + // header_size + self.name().len() + } +} + +/// Represents a Metadata Table header in a .NET metadata structure. +/// +/// The `MetadataTable` provides information about the structure and versioning of +/// the metadata, as well as the sizes and characteristics of various heaps. +#[repr(C)] +pub struct MetadataTable { + /// Reserved space, typically set to zero. + pub reserved: u32, + /// The major version of the metadata. + pub major_version: u8, + /// The minor version of the metadata. + pub minor_version: u8, + /// A bitfield indicating the sizes of the various heaps (e.g., String, GUID, Blob). + pub heap_sizes: u8, + /// The RID (Row ID) base, typically used for addressing rows in tables. + pub rid: u8, + /// A bitmask indicating which tables are present in the metadata. + pub mask_valid: u64, + /// A bitmask indicating which tables are sorted. + pub mask_sorted: u64, +} + +impl MetadataTable { + /// Parses a `MetadataTable` from a byte slice. + /// + /// This function validates the size and alignment of the byte slice before + /// returning a reference to the `MetadataTable`. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the `MetadataTable` data. + /// + /// # Returns + /// + /// * `Some(&MetadataTable)` - A reference to the parsed `MetadataTable` if the byte slice is valid. + /// * `None` - If the byte slice is invalid, does not contain enough data, or is misaligned. + pub fn from_bytes(bytes: &[u8]) -> Option<&Self> { + if bytes.len() != mem::size_of::() { + return None; + } + if bytes.as_ptr().align_offset(mem::align_of::()) != 0 { + return None; + } + Some(unsafe { &*(bytes.as_ptr() as *const Self) }) + } + + pub fn size() -> usize { + mem::size_of::() + } +} + +/// Represents an entry in the Module table in a .NET metadata structure. +/// +/// The `ModuleEntry` provides information about a module, including its generation, +/// name, and GUIDs for module versioning and edit-and-continue (ENC) information. +#[repr(C)] +pub struct ModuleEntry { + /// A `u16` value representing the generation of the module. + pub generation: u16, + /// A `StringHeapIndex` referencing the module's name in the String heap. + pub name: StringHeapIndex, + /// A `GuidHeapIndex` referencing the module version ID in the GUID heap. + pub mv_id: GuidHeapIndex, + /// A `GuidHeapIndex` referencing the edit-and-continue (ENC) ID in the GUID heap. + pub enc_id: GuidHeapIndex, + /// A `GuidHeapIndex` referencing the edit-and-continue base ID in the GUID heap. + pub enc_base_id: GuidHeapIndex, +} + +impl ModuleEntry { + /// Parses a `ModuleEntry` from a byte slice based on the heap size. + /// + /// This function extracts the fields of the `ModuleEntry` from the given byte slice, + /// validating and parsing each component, such as `StringHeapIndex` and `GuidHeapIndex`. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the `ModuleEntry` data. + /// * `heap_size` - A `u8` value indicating the size of the heap, which affects how indices are parsed. + /// + /// # Returns + /// + /// * `Some(ModuleEntry)` - The parsed `ModuleEntry` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or does not contain enough data. + pub fn from_bytes(bytes: &[u8], heap_size: u8) -> Option { + if bytes.len() < 2 { return None; } + let generation = u16::from_le_bytes(bytes[0..2].try_into().unwrap()); + let mut offset: usize = mem::size_of::(); + let name = StringHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += name.size(); + let mv_id = GuidHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += mv_id.size(); + let enc_id = GuidHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += enc_id.size(); + let enc_base_id = GuidHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + Some(Self { + generation, + name, + mv_id, + enc_id, + enc_base_id, + }) + } + + /// Returns the size of the `ModuleEntry` in bytes. + /// + /// This method calculates the size of the entry, accounting for variable-sized + /// fields like `StringHeapIndex` and `GuidHeapIndex`. + /// + /// # Returns + /// + /// * `usize` - The total size of the `ModuleEntry` in bytes. + pub fn size(&self) -> usize { + let mut size: usize = mem::size_of::(); + size += self.name.size(); + size += self.mv_id.size(); + size += self.enc_id.size(); + size += self.enc_base_id.size(); + size + } +} + +/// Represents an entry in the TypeRef table in a .NET metadata structure. +/// +/// The `TypeRefEntry` provides information about a type reference, including its +/// resolution scope, name, and namespace. +#[repr(C)] +pub struct TypeRefEntry { + /// A `ResolutionScopeIndex` referencing the scope where the type is defined. + pub resolution_scope: ResolutionScopeIndex, + /// A `StringHeapIndex` referencing the type's name in the String heap. + pub name: StringHeapIndex, + /// A `StringHeapIndex` referencing the type's namespace in the String heap. + pub namespace: StringHeapIndex, +} + +impl TypeRefEntry { + /// Parses a `TypeRefEntry` from a byte slice based on the heap size. + /// + /// This function extracts the fields of the `TypeRefEntry` from the given byte slice, + /// validating and parsing each component, such as `ResolutionScopeIndex` and `StringHeapIndex`. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the `TypeRefEntry` data. + /// * `heap_size` - A `u8` value indicating the size of the heap, which affects how indices are parsed. + /// + /// # Returns + /// + /// * `Some(TypeRefEntry)` - The parsed `TypeRefEntry` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or does not contain enough data. + pub fn from_bytes(bytes: &[u8], heap_size: u8) -> Option { + let mut offset: usize = 0; + let resolution_scope = ResolutionScopeIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += resolution_scope.size(); + let name = StringHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += name.size(); + let namespace = StringHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + Some(Self { + resolution_scope, + name, + namespace, + }) + } + + /// Returns the size of the `TypeRefEntry` in bytes. + /// + /// This method calculates the size of the entry, accounting for variable-sized + /// fields like `ResolutionScopeIndex` and `StringHeapIndex`. + /// + /// # Returns + /// + /// * `usize` - The total size of the `TypeRefEntry` in bytes. + pub fn size(&self) -> usize { + let mut size = self.resolution_scope.size(); + size += self.name.size(); + size += self.namespace.size(); + size + } +} + +/// Represents an entry in the TypeDef table in a .NET metadata structure. +/// +/// The `TypeDefEntry` provides detailed information about a type definition, +/// including its attributes, name, namespace, parent type, and lists of fields and methods. +#[repr(C)] +pub struct TypeDefEntry { + /// Type attributes specifying visibility, layout, and other characteristics. + pub flags: u32, + /// A `StringHeapIndex` referencing the type's name in the String heap. + pub name: StringHeapIndex, + /// A `StringHeapIndex` referencing the type's namespace in the String heap. + pub namespace: StringHeapIndex, + /// A `TypeDefOrRefIndex` referencing the base type or interface. + pub extends: TypeDefOrRefIndex, + /// A `SimpleTableIndex` pointing to the start of the field list for this type. + pub field_list: SimpleTableIndex, + /// A `SimpleTableIndex` pointing to the start of the method list for this type. + pub method_list: SimpleTableIndex, +} + +impl TypeDefEntry { + /// Parses a `TypeDefEntry` from a byte slice based on the heap size. + /// + /// This function extracts the fields of the `TypeDefEntry` from the given byte slice, + /// validating and parsing each component, such as `StringHeapIndex`, `TypeDefOrRefIndex`, + /// and `SimpleTableIndex`. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the `TypeDefEntry` data. + /// * `heap_size` - A `u8` value indicating the size of the heap, which affects how indices are parsed. + /// + /// # Returns + /// + /// * `Some(TypeDefEntry)` - The parsed `TypeDefEntry` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or does not contain enough data. + pub fn from_bytes(bytes: &[u8], heap_size: u8) -> Option { + if bytes.len() < 4 { return None; } + let flags = u32::from_le_bytes(bytes[0..4].try_into().unwrap()); + let mut offset: usize = mem::size_of::(); + let name = StringHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += name.size(); + let namespace = StringHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += namespace.size(); + let extends = TypeDefOrRefIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += extends.size(); + let field_list = SimpleTableIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += field_list.size(); + let method_list = SimpleTableIndex::from_bytes(&bytes[offset..], heap_size)?; + Some(Self { + flags, + name, + namespace, + extends, + field_list, + method_list, + }) + } + + /// Returns the size of the `TypeDefEntry` in bytes. + /// + /// This method calculates the size of the entry, accounting for variable-sized + /// fields like `StringHeapIndex`, `TypeDefOrRefIndex`, and `SimpleTableIndex`. + /// + /// # Returns + /// + /// * `usize` - The total size of the `TypeDefEntry` in bytes. + pub fn size(&self) -> usize { + let mut size: usize = mem::size_of::(); + size += self.name.size(); + size += self.namespace.size(); + size += self.extends.size(); + size += self.field_list.size(); + size += self.method_list.size(); + size + } +} + +/// Represents an entry in the Field table in a .NET metadata structure. +/// +/// The `FieldEntry` provides information about a field definition, including its +/// flags, name, and signature. +#[repr(C)] +pub struct FieldEntry { + /// Field attributes specifying visibility, special behavior, and other characteristics. + pub flags: u16, + /// A `StringHeapIndex` referencing the field's name in the String heap. + pub name: StringHeapIndex, + /// A `BlobHeapIndex` referencing the field's signature in the Blob heap. + pub signature: BlobHeapIndex, +} + +impl FieldEntry { + /// Parses a `FieldEntry` from a byte slice based on the heap size. + /// + /// This function extracts the fields of the `FieldEntry` from the given byte slice, + /// validating and parsing each component, such as `StringHeapIndex` and `BlobHeapIndex`. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the `FieldEntry` data. + /// * `heap_size` - A `u8` value indicating the size of the heap, which affects how indices are parsed. + /// + /// # Returns + /// + /// * `Some(FieldEntry)` - The parsed `FieldEntry` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or does not contain enough data. + pub fn from_bytes(bytes: &[u8], heap_size: u8) -> Option { + if bytes.len() < 2 { return None; } + let flags = u16::from_le_bytes(bytes[0..2].try_into().unwrap()); + let mut offset: usize = mem::size_of::(); + let name: StringHeapIndex = StringHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += name.size(); + let signature = BlobHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + Some(Self { + flags, + name, + signature, + }) + } + + /// Returns the size of the `FieldEntry` in bytes. + /// + /// This method calculates the size of the entry, accounting for variable-sized + /// fields like `StringHeapIndex` and `BlobHeapIndex`. + /// + /// # Returns + /// + /// * `usize` - The total size of the `FieldEntry` in bytes. + pub fn size(&self) -> usize { + let mut size: usize = mem::size_of::(); + size += self.name.size(); + size += self.signature.size(); + size + } +} + +/// Represents an entry in the MethodDef table in a .NET metadata structure. +/// +/// The `MethodDefEntry` provides detailed information about a method definition, +/// including its address, flags, name, signature, and parameters. +#[repr(C)] +pub struct MethodDefEntry { + /// The relative virtual address (RVA) of the method's executable code. + pub rva: u32, + /// Implementation flags specifying method attributes. + pub impl_flags: u16, + /// Method flags specifying additional attributes. + pub flags: u16, + /// A `StringHeapIndex` referencing the method's name in the String heap. + pub name: StringHeapIndex, + /// A `BlobHeapIndex` referencing the method's signature in the Blob heap. + pub signature: BlobHeapIndex, + /// A `SimpleTableIndex` referencing the method's parameter list in the Parameter table. + pub param_list: SimpleTableIndex, +} + +impl MethodDefEntry { + /// Parses a `MethodDefEntry` from a byte slice based on the heap size. + /// + /// This function extracts the fields of the `MethodDefEntry` from the given byte slice, + /// validating and parsing each component, such as `StringHeapIndex`, `BlobHeapIndex`, + /// and `SimpleTableIndex`. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the `MethodDefEntry` data. + /// * `heap_size` - A `u8` value indicating the size of the heap, which affects how indices are parsed. + /// + /// # Returns + /// + /// * `Some(MethodDefEntry)` - The parsed `MethodDefEntry` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or does not contain enough data. + pub fn from_bytes(bytes: &[u8], heap_size: u8) -> Option { + let rva = u32::from_le_bytes(bytes[0..4].try_into().unwrap()); + let impl_flags = u16::from_le_bytes(bytes[4..6].try_into().unwrap()); + let flags = u16::from_le_bytes(bytes[6..8].try_into().unwrap()); + let mut offset: usize = 8; + let name = StringHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += name.size(); + let signature = BlobHeapIndex::from_bytes(&bytes[offset..], heap_size)?; + offset += signature.size(); + let param_list = SimpleTableIndex::from_bytes(&bytes[offset..], heap_size)?; + Some(Self{ + rva, + impl_flags, + flags, + name, + signature, + param_list, + }) + } + + /// Returns the size of the `MethodDefEntry` in bytes. + /// + /// This method calculates the size of the entry, accounting for variable-sized + /// fields like `StringHeapIndex`, `BlobHeapIndex`, and `SimpleTableIndex`. + /// + /// # Returns + /// + /// * `usize` - The total size of the `MethodDefEntry` in bytes. + pub fn size(&self) -> usize { + let mut size: usize = 8; + size += self.name.size(); + size += self.signature.size(); + size += self.param_list.size(); + size + } +} + +/// Represents an index into a simple table in a .NET metadata structure. +/// +/// The `SimpleTableIndex` is used to reference entries in a metadata table, +/// such as the Method, Field, or TypeDef tables, depending on the context. +#[repr(C)] +pub struct SimpleTableIndex { + /// The offset in the table where the entry starts. + pub offset: u32, + /// The size of the referenced entry in bytes. + pub size: u32, +} + +impl SimpleTableIndex { + /// Parses a `SimpleTableIndex` from a byte slice based on the heap size. + /// + /// The size of the index (2 or 4 bytes) is determined by the `heap_size` parameter. + /// The function validates the byte slice length before parsing. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the index data. + /// * `heap_size` - A `u8` value indicating the size of the heap (used to determine + /// whether the index is 2 or 4 bytes). + /// + /// # Returns + /// + /// * `Some(SimpleTableIndex)` - The parsed `SimpleTableIndex` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or does not contain enough data. + pub fn from_bytes(bytes: &[u8], heap_size: u8) -> Option { + let size = if heap_size & 1 != 0 { 4 } else { 2 }; + + let offset = match size { + 2 if bytes.len() >= 2 => u16::from_le_bytes(bytes[0..2].try_into().unwrap()) as u32, + 4 if bytes.len() >= 4 => u32::from_le_bytes(bytes[0..4].try_into().unwrap()), + _ => return None, + }; + + Some(Self { + offset, + size, + }) + } + + /// Returns the size of the referenced entry in the table. + /// + /// # Returns + /// + /// * `usize` - The size of the referenced entry in bytes. + pub fn size(&self) -> usize { + self.size as usize + } +} + +/// Represents an index into the String heap in a .NET metadata structure. +/// +/// The `StringHeapIndex` is used to reference entries in the String heap, which stores +/// strings used in metadata tables. +#[derive(Debug)] +pub struct StringHeapIndex { + /// The offset in the String heap where the data starts. + pub offset: u32, + /// The size of the referenced data in bytes. + pub size: u32, +} + +impl StringHeapIndex { + /// Parses a `StringHeapIndex` from a byte slice based on the heap size. + /// + /// The size of the index (2 or 4 bytes) is determined by the `heap_size` parameter. + /// The function validates the byte slice length before parsing. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the index data. + /// * `heap_size` - A `u8` value indicating the size of the heap (used to determine + /// whether the index is 2 or 4 bytes). + /// + /// # Returns + /// + /// * `Some(StringHeapIndex)` - The parsed `StringHeapIndex` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or does not contain enough data. + pub fn from_bytes(bytes: &[u8], heap_size: u8) -> Option { + let size = if heap_size & 1 != 0 { 4 } else { 2 }; + + let offset = match size { + 2 if bytes.len() >= 2 => u16::from_le_bytes(bytes[0..2].try_into().unwrap()) as u32, + 4 if bytes.len() >= 4 => u32::from_le_bytes(bytes[0..4].try_into().unwrap()), + _ => return None, + }; + + Some(Self { + offset, + size, + }) + } + + /// Returns the size of the referenced data in the String heap. + /// + /// # Returns + /// + /// * `usize` - The size of the referenced data in bytes. + pub fn size(&self) -> usize { + self.size as usize + } +} + +/// Represents an index into the GUID heap in a .NET metadata structure. +/// +/// The `GuidHeapIndex` is used to reference entries in the GUID heap, which stores +/// globally unique identifiers (GUIDs) used in metadata tables. +#[derive(Debug)] +pub struct GuidHeapIndex { + /// The offset in the GUID heap where the data starts. + pub offset: u32, + /// The size of the referenced data in bytes. + pub size: u32, +} + +impl GuidHeapIndex { + /// Parses a `GuidHeapIndex` from a byte slice based on the heap size. + /// + /// The size of the index (2 or 4 bytes) is determined by the `heap_size` parameter. + /// The function validates the byte slice length before parsing. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the index data. + /// * `heap_size` - A `u8` value indicating the size of the heap (used to determine + /// whether the index is 2 or 4 bytes). + /// + /// # Returns + /// + /// * `Some(GuidHeapIndex)` - The parsed `GuidHeapIndex` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or does not contain enough data. + pub fn from_bytes(bytes: &[u8], heap_size: u8) -> Option { + let size = if heap_size & 2 != 0 { 4 } else { 2 }; + + let offset = match size { + 2 if bytes.len() >= 2 => u16::from_le_bytes(bytes[0..2].try_into().unwrap()) as u32, + 4 if bytes.len() >= 4 => u32::from_le_bytes(bytes[0..4].try_into().unwrap()), + _ => return None, + }; + + Some(Self { + offset, + size, + }) + } + + /// Returns the size of the referenced data in the GUID heap. + /// + /// # Returns + /// + /// * `usize` - The size of the referenced data in bytes. + pub fn size(&self) -> usize { + self.size as usize + } +} + +/// Represents an index into the ResolutionScope table in a .NET metadata structure. +/// +/// The `ResolutionScopeIndex` is used to reference entries in the ResolutionScope table, +/// which includes assemblies, modules, and other scopes that define or reference types. +#[repr(C)] +pub struct ResolutionScopeIndex { + /// The offset in the ResolutionScope table where the data starts. + pub offset: u32, + /// The offset in the ResolutionScope table where the data starts. + pub size: u32, +} + +impl ResolutionScopeIndex { + /// Parses a `ResolutionScopeIndex` from a byte slice based on the heap size. + /// + /// The size of the index (2 or 4 bytes) is determined by the `heap_size` parameter. + /// The function validates the byte slice length before parsing. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the index data. + /// * `heap_size` - A `u8` value indicating the size of the heap (used to determine + /// whether the index is 2 or 4 bytes). + /// + /// # Returns + /// + /// * `Some(ResolutionScopeIndex)` - The parsed `ResolutionScopeIndex` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or does not contain enough data. + pub fn from_bytes(bytes: &[u8], heap_size: u8) -> Option { + let size = if heap_size & 2 != 0 { 4 } else { 2 }; + + let offset = match size { + 2 if bytes.len() >= 2 => u16::from_le_bytes(bytes[0..2].try_into().unwrap()) as u32, + 4 if bytes.len() >= 4 => u32::from_le_bytes(bytes[0..4].try_into().unwrap()), + _ => return None, + }; + + Some(Self { + offset, + size, + }) + } + + /// Returns the size of the referenced data in the ResolutionScope table. + /// + /// # Returns + /// + /// * `usize` - The size of the referenced data in bytes. + pub fn size(&self) -> usize { + self.size as usize + } +} + +/// Represents an index into the TypeDef or TypeRef table in a .NET metadata structure. +/// +/// The `TypeDefOrRefIndex` is used to reference types defined or referenced in the +/// metadata tables, facilitating access to type definitions or references. +#[repr(C)] +#[derive(Debug)] +pub struct TypeDefOrRefIndex { + /// The offset in the TypeDef or TypeRef table where the data starts. + pub offset: u32, + /// The size of the referenced data in bytes. + pub size: u32, +} + +impl TypeDefOrRefIndex { + /// Parses a `TypeDefOrRefIndex` from a byte slice based on the heap size. + /// + /// The size of the index (2 or 4 bytes) is determined by the `heap_size` parameter. + /// The function validates the byte slice length before parsing. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the index data. + /// * `heap_size` - A `u8` value indicating the size of the heap (used to determine + /// whether the index is 2 or 4 bytes). + /// + /// # Returns + /// + /// * `Some(TypeDefOrRefIndex)` - The parsed `TypeDefOrRefIndex` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or does not contain enough data. + pub fn from_bytes(bytes: &[u8], heap_size: u8) -> Option { + let size = if heap_size & 2 != 0 { 4 } else { 2 }; + + let offset = match size { + 2 if bytes.len() >= 2 => u16::from_le_bytes(bytes[0..2].try_into().unwrap()) as u32, + 4 if bytes.len() >= 4 => u32::from_le_bytes(bytes[0..4].try_into().unwrap()), + _ => return None, + }; + + Some(Self { + offset, + size, + }) + } + + /// Returns the size of the referenced data in the TypeDef or TypeRef table. + /// + /// # Returns + /// + /// * `usize` - The size of the referenced data in bytes. + pub fn size(&self) -> usize { + self.size as usize + } +} + +/// Represents an index into the Blob heap in a .NET metadata structure. +/// +/// The `BlobHeapIndex` is used to reference data in the Blob heap, which contains +/// metadata such as constants, custom attributes, and signatures. +/// +/// # Fields +/// +/// * `offset` - The offset in the Blob heap where the data starts. +/// * `size` - The size of the referenced data in bytes. +#[repr(C)] +pub struct BlobHeapIndex { + /// The offset in the Blob heap where the data starts. + pub offset: u32, + /// The size of the referenced data in bytes. + pub size: u32, +} + +impl BlobHeapIndex { + /// Parses a `BlobHeapIndex` from a byte slice based on the heap size. + /// + /// The size of the index (2 or 4 bytes) is determined by the `heap_size` parameter. + /// The function validates the byte slice length before parsing. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the index data. + /// * `heap_size` - A `u8` value indicating the size of the heap (used to determine + /// whether the index is 2 or 4 bytes). + /// + /// # Returns + /// + /// * `Some(BlobHeapIndex)` - The parsed `BlobHeapIndex` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or does not contain enough data. + pub fn from_bytes(bytes: &[u8], heap_size: u8) -> Option { + let size = if heap_size & 2 != 0 { 4 } else { 2 }; + + let offset = match size { + 2 if bytes.len() >= 2 => u16::from_le_bytes(bytes[0..2].try_into().unwrap()) as u32, + 4 if bytes.len() >= 4 => u32::from_le_bytes(bytes[0..4].try_into().unwrap()), + _ => return None, + }; + + Some(Self { + offset, + size, + }) + } + + /// Returns the size of the referenced data in the Blob heap. + /// + /// # Returns + /// + /// * `usize` - The size of the referenced data in bytes. + pub fn size(&self) -> usize { + self.size as usize + } +} + +/// Represents an entry in the .NET metadata table. +/// +/// Each entry corresponds to a specific metadata table type, such as `Module`, +/// `TypeRef`, `TypeDef`, `Field`, or `MethodDef`. +/// +/// # Variants +/// +/// * `Module(ModuleEntry)` - Represents a module definition entry. +/// * `TypeRef(TypeRefEntry)` - Represents a type reference entry. +/// * `TypeDef(TypeDefEntry)` - Represents a type definition entry. +/// * `Field(FieldEntry)` - Represents a field entry. +/// * `MethodDef(MethodDefEntry)` - Represents a method definition entry. +pub enum Entry { + Module(ModuleEntry), + TypeRef(TypeRefEntry), + TypeDef(TypeDefEntry), + Field(FieldEntry), + MethodDef(MethodDefEntry), +} + +/// Represents a Tiny method header in a .NET executable. +/// +/// The `TinyHeader` is a compact representation of method headers for small methods +/// with limited fields and constraints. +/// +/// # Fields +/// +/// * `code_size` - The size of the method's executable code in bytes. +#[repr(C)] +pub struct TinyHeader { + /// The size of the method's executable code in bytes. + pub code_size: u8, +} + +impl TinyHeader { + /// Parses a `TinyHeader` from a byte slice. + /// + /// This function validates the size and alignment of the byte slice before + /// returning a reference to the `TinyHeader`. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the header data. + /// + /// # Returns + /// + /// * `Some(&TinyHeader)` - A reference to the parsed `TinyHeader` if the byte slice is valid. + /// * `None` - If the byte slice is invalid or misaligned. + pub fn from_bytes(bytes: &[u8]) -> Option<&Self> { + if bytes.len() != mem::size_of::() { + return None; + } + if bytes.as_ptr().align_offset(mem::align_of::()) != 0 { + return None; + } + Some(unsafe { &*(bytes.as_ptr() as *const Self) }) + } + + pub fn size(&self) -> usize { + 1 + } +} + +/// Represents the method header in a .NET executable. +/// +/// The method header can either be a `Tiny` or `Fat` header, depending on the +/// method's structure and size. +/// +/// # Variants +/// +/// * `Tiny(TinyHeader)` - A compact method header with limited fields. +/// * `Fat(FatHeader)` - A full-featured method header with additional details. +pub enum MethodHeader { + Tiny(TinyHeader), + Fat(FatHeader), +} + +impl MethodHeader { + /// Returns the size of the method header in bytes. + /// + /// # Returns + /// + /// * `Some(usize)` - The size of the method header in bytes. + /// * `None` - If the method header type is not recognized. + pub fn size(&self) -> Option { + match self { + Self::Tiny(header) => Some(header.size()), + Self::Fat(header) => Some(header.size()), + } + } + + /// Returns the size of the method's executable code in bytes. + /// + /// # Returns + /// + /// * `Some(usize)` - The size of the method's code. + /// * `None` - If the method header type is not recognized. + pub fn code_size(&self) -> Option { + match self { + Self::Tiny(header) => Some(header.code_size as usize), + Self::Fat(header) => Some(header.code_size as usize), + } + } +} + +/// Represents a fat method header in a .NET executable. +/// +/// The fat header provides detailed information about a method, including its +/// flags, stack size, code size, and local variable signature token. +#[repr(C)] +pub struct FatHeader { + /// Flags indicating the method's attributes. + pub flags: u16, + /// The maximum stack depth required by the method. + pub max_stack: u16, + /// The size of the method's executable code in bytes. + pub code_size: u32, + /// A metadata token for the method's local variable signature. + pub local_var_sig_token: u32, +} + +impl FatHeader { + /// Parses a `FatHeader` from a byte slice. + /// + /// # Parameters + /// + /// * `bytes` - A byte slice containing the header data. + /// + /// # Returns + /// + /// * `Ok(FatHeader)` - The parsed `FatHeader`. + /// * `Err(std::io::Error)` - If the byte slice is too short or invalid. + /// + /// # Errors + /// + /// Returns an error if the byte slice does not contain enough data to parse a valid `FatHeader`. + pub fn from_bytes(bytes: &[u8]) -> Result { + if bytes.len() < 12 { + return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "Not enough bytes for FatHeader")); + } + + Ok(Self { + flags: u16::from_le_bytes(bytes[0..2].try_into().unwrap()), + max_stack: u16::from_le_bytes(bytes[2..4].try_into().unwrap()), + code_size: u32::from_le_bytes(bytes[4..8].try_into().unwrap()), + local_var_sig_token: u32::from_le_bytes(bytes[8..12].try_into().unwrap()), + }) + } + + /// Returns the size of the `FatHeader` in bytes. + /// + /// # Returns + /// + /// * `usize` - The size of the `FatHeader`, which is always 12 bytes. + pub fn size(&self) -> usize { + 12 + } +} diff --git a/src/formats/elf.rs b/src/formats/elf.rs new file mode 100644 index 00000000..d39477f1 --- /dev/null +++ b/src/formats/elf.rs @@ -0,0 +1,218 @@ +use lief::generic::{Section, Symbol}; +use lief::Binary; +use lief::elf::segment::Type as SegmentType; +use std::io::{Cursor, Error, ErrorKind}; +use std::collections::BTreeSet; +use std::path::PathBuf; +use crate::Architecture; +use crate::formats::File; +use std::collections::BTreeMap; +use crate::types::MemoryMappedFile; +use lief::elf::section::Flags; +use crate::Config; +use lief::elf::symbol::Type as ElfSymbolType; +use crate::controlflow::Symbol as BlSymbol; + +pub const DEFAULT_IMAGEBASE: u64 = 0x100000; + +pub struct ELF { + elf: lief::elf::Binary, + pub file: File, + pub config: Config, +} + +impl ELF { + /// Creates a new `ELF` instance by reading a ELF file from the provided path. + /// + /// # Parameters + /// - `path`: The file path to the ELF file to be loaded. + /// + /// # Returns + /// A `Result` containing the `ELF` object on success or an `Error` on failure. + pub fn new(path: String, config: Config) -> Result { + let mut file = File::new(path.clone(), config.clone())?; + match file.read() { + Ok(_) => (), + Err(_) => { + return Err(Error::new(ErrorKind::InvalidInput, "failed to read file")); + } + }; + let binary = Binary::parse(&path); + if let Some(Binary::ELF(elf)) = binary { + return Ok(Self { + elf: elf, + file: file, + config: config, + }); + } + return Err(Error::new(ErrorKind::InvalidInput, "invalid elf file")); + } + + /// Creates a new `ELF` instance from a byte vector containing ELF file data. + /// + /// # Parameters + /// - `bytes`: A vector of bytes representing the PE file data. + /// + /// # Returns + /// A `Result` containing the `ELF` object on success or an `Error` on failure. + #[allow(dead_code)] + pub fn from_bytes(bytes: Vec, config: Config) -> Result { + let file = File::from_bytes(bytes, config.clone()); + let mut cursor = Cursor::new(&file.data); + if let Some(Binary::ELF(elf)) = Binary::from(&mut cursor) { + return Ok(Self{ + elf: elf, + file: file, + config: config, + }) + } + return Err(Error::new(ErrorKind::InvalidInput, "invalid elf file")); + } + + pub fn architecture(&self) -> Architecture { + let architecture = match self.elf.header().machine_type() { + 62 => Architecture::AMD64, + 3 => Architecture::I386, + _ => Architecture::UNKNOWN, + }; + architecture + } + + pub fn entrypoint(&self) -> u64 { + self.imagebase() + self.elf.header().entrypoint() + } + + pub fn imagebase(&self) -> u64 { + for segment in self.elf.segments() { + if segment.p_type() == SegmentType::LOAD { + if segment.virtual_address() != 0 { + return segment.virtual_address(); + } + return DEFAULT_IMAGEBASE; + } + } + DEFAULT_IMAGEBASE + } + + pub fn size(&self) -> u64 { + self.file.size() + } + + pub fn exports(&self) -> BTreeSet { + let mut result = BTreeSet::::new(); + for symbol in self.elf.exported_symbols() { + result.insert(self.imagebase() + symbol.value()); + } + result + } + + pub fn symbols(&self) -> BTreeMap { + self.elf + .dynamic_symbols() + .chain(self.elf.exported_symbols()) + .chain(self.elf.imported_symbols()) + .chain(self.elf.symtab_symbols()) + .filter(|symbol| symbol.get_type() == ElfSymbolType::FUNC) + .map(|symbol| { + ( + (self.imagebase() + symbol.value()) as u64, + BlSymbol { + symbol_type: "function".to_string(), + name: symbol.name(), + address: self.imagebase() + symbol.value(), + }, + ) + }) + .collect() + } + + pub fn relative_virtual_address_to_virtual_address(&self, relative_virtual_address: u64) -> u64 { + self.imagebase() + relative_virtual_address + } + + pub fn file_offset_to_virtual_address(&self, file_offset: u64) -> Option { + for segment in self.elf.segments() { + let start = segment.file_offset(); + let end = start + segment.physical_size(); + if file_offset >= start && file_offset < end { + let segment_virtual_address = self.imagebase() + segment.virtual_address(); + return Some(segment_virtual_address + (file_offset - start)) + } + } + None + } + + pub fn image(&self) -> Result { + let pathbuf = PathBuf::from(self.config.mmap.directory.clone()) + .join(self.file.sha256_no_config().unwrap()); + let mut tempmap = match MemoryMappedFile::new( + pathbuf, + self.config.mmap.cache.enabled) { + Ok(tempmmap) => tempmmap, + Err(error) => return Err(error), + }; + + if tempmap.is_cached() { + return Ok(tempmap); + } + + tempmap.seek_to_end()?; + tempmap.write(&self.file.data[0..self.elf.header().header_size() as usize])?; + + for segment in self.elf.segments() { + let segment_virtual_address = self.imagebase() + segment.virtual_address(); + + if segment_virtual_address > tempmap.size()? as u64 { + let padding_length = segment_virtual_address - tempmap.size()? as u64; + tempmap.seek_to_end()?; + tempmap.write_padding(padding_length as usize)?; + } + + if segment.p_type() == SegmentType::LOAD { + let segment_file_offset = segment.file_offset() as usize; + let segment_size = segment.physical_size() as usize; + + if segment_file_offset + segment_size <= self.file.data.len() { + tempmap.seek_to_end()?; + tempmap.write(&self.file.data[segment_file_offset..segment_file_offset + segment_size])?; + } else { + return Err(Error::new( + ErrorKind::InvalidData, + "elf segment size exceeds file data length", + )); + } + } + } + + Ok(tempmap) + } + + pub fn tlsh(&self) -> Option { + self.file.tlsh() + } + + pub fn sha256(&self) -> Option { + self.file.sha256() + } + + pub fn entrypoints(&self) -> BTreeSet { + let mut entrypoints = BTreeSet::::new(); + entrypoints.insert(self.entrypoint()); + entrypoints.extend(self.exports()); + entrypoints.extend(self.symbols().keys()); + entrypoints + } + + pub fn executable_virtual_address_ranges(&self) -> BTreeMap { + let mut result = BTreeMap::::new(); + for section in self.elf.sections() { + if section.flags().contains(Flags::EXECINSTR) { + let start = self.imagebase() + section.virtual_address(); + let end = start + section.size(); + result.insert(start, end); + } + } + result + } + +} diff --git a/src/formats/file.rs b/src/formats/file.rs new file mode 100644 index 00000000..0f7637c2 --- /dev/null +++ b/src/formats/file.rs @@ -0,0 +1,242 @@ +use std::fs::File as StdFile; +use std::io::{Read, SeekFrom, Seek, Error, Cursor}; +use crate::hashing::sha256::SHA256; +use crate::hashing::tlsh::TLSH; +use crate::Binary; +use std::io::ErrorKind; +use serde::{Deserialize, Serialize}; +use serde_json; +use crate::controlflow::Attribute; +use crate::Config; + +#[cfg(windows)] +use std::os::windows::fs::OpenOptionsExt; + +#[cfg(windows)] +use std::fs::OpenOptions; + +#[cfg(windows)] +use winapi::um::winnt::{FILE_SHARE_READ}; + +pub trait FileHandle: Read + Seek + Send {} + +impl FileHandle for T {} + +#[derive(Serialize, Deserialize, Clone)] +pub struct FileJson { + #[serde(rename = "type")] + /// The type always `file` + pub type_: String, + /// The SHA-256 hash of the file, if available. + pub sha256: Option, + /// The TLSH (Trend Micro Locality Sensitive Hash) of the file, if available. + pub tlsh: Option, + /// The File Size, + pub size: Option, + // The File Entropy + pub entropy: Option, +} + +/// Represents a file with its contents and an optional file path. +pub struct File { + /// The contents of the file as a byte vector. + pub data: Vec, + /// The path of the file, if available. + pub path: Option, + /// The configuration `Config` + pub config: Config, + /// Handle to the file + handle: Box, +} + +impl File { + /// Creates a new `File` instance with a given path. + /// + /// # Arguments + /// + /// * `path` - A `String` representing the path to the file. + /// + /// # Returns + /// + /// A `File` instance with the given path and empty data. + pub fn new(path: String, config: Config) -> Result { + #[cfg(windows)] + let handle = Box::new( + OpenOptions::new() + .read(true) + .write(false) + .share_mode(FILE_SHARE_READ) + .open(&path)? + ) as Box; + #[cfg(not(windows))] + let handle = Box::new(StdFile::open(&path)?) as Box; + Ok(Self { + data: Vec::new(), + path: Some(path), + config, + handle, + }) + } + + /// Creates a new `File` instance from the provided byte data. + /// + /// # Arguments + /// + /// * `bytes` - A `Vec` representing the byte data of the file. + /// + /// # Returns + /// + /// A `File` instance with the given byte data and no path. + #[allow(dead_code)] + pub fn from_bytes(bytes: Vec, config: Config) -> Self { + let handle = Box::new(Cursor::new(bytes.clone())) as Box; + Self { + data: bytes, + path: None, + config, + handle, + } + } + + /// Computes the TLSH (Trend Locality Sensitive Hashing) of the file's data. + /// + /// # Returns + /// + /// An `Option` containing the hexadecimal representation of the TLSH, + /// or `None` if the file's size is zero or less. + #[allow(dead_code)] + pub fn tlsh(&self) -> Option { + if !self.config.formats.file.hashing.tlsh.enabled { return None; } + if self.size() <= 0 { return None; } + TLSH::new(&self.data, 50).hexdigest() + } + + /// Computes the SHA-256 hash of the file's data. + /// + /// # Returns + /// + /// An `Option` containing the hexadecimal representation of the SHA-256 hash, + /// or `None` if the file's size is zero or less. + #[allow(dead_code)] + pub fn sha256(&self) -> Option { + if !self.config.formats.file.hashing.sha256.enabled { return None; } + if self.size() <= 0 { return None; } + SHA256::new(&self.data).hexdigest() + } + + /// Computes the SHA-256 hash of the file's data. + /// + /// # Returns + /// + /// An `Option` containing the hexadecimal representation of the SHA-256 hash, + /// or `None` if the file's size is zero or less. + #[allow(dead_code)] + pub fn sha256_no_config(&self) -> Option { + if self.size() <= 0 { return None; } + SHA256::new(&self.data).hexdigest() + } + + /// Returns the size of the file in bytes. + /// + /// # Returns + /// + /// The size of the file in bytes as a `u64`. + #[allow(dead_code)] + pub fn size(&self) -> u64 { + self.data.len() as u64 + } + + /// Seeks to a specific offset in the file. + /// + /// # Arguments + /// + /// * `offset` - The position to seek to, specified as a `u64` (absolute offset from the start of the file). + /// + /// # Returns + /// + /// A `Result` indicating the success or failure of the seek operation, returning the new position. + /// + /// # Errors + /// + /// Returns an error if the file path is missing or the seek operation fails. + pub fn seek(&mut self, offset: u64) -> Result { + let new_position = self.handle.seek(SeekFrom::Start(offset))?; + Ok(new_position) + } + + /// Gets the current position of the file cursor. + /// # Returns + /// + /// A `Result` with the current cursor position. + pub fn current_position(&mut self) -> Result { + let position = self.handle.seek(SeekFrom::Current(0))?; + Ok(position) + } + + /// Reads the content of the file from the given path and stores it in `data`. + /// + /// # Returns + /// + /// A `Result` indicating the success or failure of the operation. + /// Returns `Ok(())` on success, or an `Err` with an `Error` if the file cannot be read. + /// + /// # Errors + /// + /// Returns an error if the file path is missing or the file cannot be opened or read. + pub fn read(&mut self) -> Result<(), Error> { + if self.path.is_none() { return Err(Error::new(ErrorKind::InvalidInput, "missing file path to read")); } + let mut file = StdFile::open(&self.path.clone().unwrap())?; + file.read_to_end(&mut self.data)?; + Ok(()) + } + + /// Prints the JSON representation of the file metadata to standard output. + #[allow(dead_code)] + pub fn print(&self) { + if let Ok(json) = self.json() { + println!("{}", json); + } + } + + /// Processes the file metadata into a JSON-serializable `FileJson` structure. + /// + /// # Returns + /// + /// Returns a `FileJson` struct containing the file's SHA-256 hash, TLSH hash, and size. + pub fn process(&self) -> FileJson { + FileJson { + type_: "file".to_string(), + sha256: self.sha256(), + tlsh: self.tlsh(), + size: Some(self.size()), + entropy: self.entropy(), + } + } + + pub fn entropy(&self) -> Option { + if !self.config.formats.file.heuristics.entropy.enabled { return None; } + Binary::entropy(&self.data) + } + + /// Gets attribute information about a file + /// + /// # Returns + /// + /// Returns a `Attribute` struct containing the file's SHA-256 hash, TLSH hash, and size. + pub fn attribute(&self) -> Attribute { + Attribute::File(self.process()) + } + + /// Converts the file metadata into a JSON string representation. + /// + /// # Returns + /// + /// Returns `Ok(String)` containing the JSON representation of the file metadata, + /// or an `Err` if serialization fails. + pub fn json(&self) -> Result { + let raw = self.process(); + let result = serde_json::to_string(&raw)?; + Ok(result) + } + +} diff --git a/src/formats/macho.rs b/src/formats/macho.rs new file mode 100644 index 00000000..27c6a0a6 --- /dev/null +++ b/src/formats/macho.rs @@ -0,0 +1,306 @@ +use lief::generic::{Section, Symbol}; +use lief::Binary; +use std::collections::{BTreeMap, BTreeSet}; +use std::io::{Cursor, Error, ErrorKind}; +use crate::Architecture; +use crate::formats::File; +use crate::Config; +use lief::macho::header::CpuType as MachoCpuType; +use crate::controlflow::Symbol as BlSymbol; +use crate::types::MemoryMappedFile; +use std::path::PathBuf; +use lief::macho::commands::{Command, LoadCommandTypes}; +use lief::macho::section::Flags as SectionFlags; + +pub const N_STAB: u8 = 0xE0; +pub const N_TYPE: u8 = 0x0E; +pub const N_SECT: u8 = 0x0E; +pub const VM_PROT_EXECUTE: u8 = 0x04; + +pub struct MACHO { + macho: lief::macho::FatBinary, + pub file: File, + pub config: Config, +} + +impl MACHO { + /// Creates a new `MACHO` instance by reading a ELF file from the provided path. + /// + /// # Parameters + /// - `path`: The file path to the MACHO file to be loaded. + /// + /// # Returns + /// A `Result` containing the `MACHO` object on success or an `Error` on failure. + pub fn new(path: String, config: Config) -> Result { + let mut file = File::new(path.clone(), config.clone())?; + match file.read() { + Ok(_) => (), + Err(_) => { + return Err(Error::new(ErrorKind::InvalidInput, "failed to read macho file")); + } + }; + let binary = Binary::parse(&path); + if let Some(Binary::MachO(macho)) = binary { + return Ok(Self { + macho: macho, + file: file, + config: config, + }); + } + return Err(Error::new(ErrorKind::InvalidInput, "invalid macho file")); + } + + /// Creates a new `MACHO` instance from a byte vector containing MACHO file data. + /// + /// # Parameters + /// - `bytes`: A vector of bytes representing the PE file data. + /// + /// # Returns + /// A `Result` containing the `MACHO` object on success or an `Error` on failure. + #[allow(dead_code)] + pub fn from_bytes(bytes: Vec, config: Config) -> Result { + let file = File::from_bytes(bytes, config.clone()); + let mut cursor = Cursor::new(&file.data); + if let Some(Binary::MachO(macho)) = Binary::from(&mut cursor) { + return Ok(Self{ + macho: macho, + file: file, + config: config, + }) + } + return Err(Error::new(ErrorKind::InvalidInput, "invalid macho file")); + } + + pub fn relative_virtual_address_to_virtual_address(&self, relative_virtual_address: u64, slice: usize) -> Option { + Some(self.imagebase(slice)? + relative_virtual_address) + } + + pub fn file_offset_to_virtual_address(&self, file_offset: u64, slice: usize) -> Option { + let binding = self.macho.iter().nth(slice); + if binding.is_none() { return None; } + for segment in binding.unwrap().segments() { + let start = segment.file_offset(); + let end = start + segment.file_size(); + if file_offset >= start && file_offset < end { + return Some(segment.virtual_address() + (file_offset - start)); + } + } + None + } + + /// Returns the number of binaries contained in the MachO binary. + /// + /// # Returns + /// A `usize` containing the number of binaries in the MachO binary. + pub fn number_of_slices(&self) -> usize { + self.macho.iter().count() + } + + /// Returns the entrypoint of the MachO binary by index. + /// + /// # Returns + /// A `Option` representing the entrypoint of the binary. + pub fn entrypoint(&self, slice: usize) -> Option { + Some(self.imagebase(slice)? + self.macho.iter().nth(slice)?.main_command()?.entrypoint()) + } + + /// Returns the imagebase of the MachO binary by index. + /// + /// # Returns + /// A `Option` representing the imagebase of the binary. + pub fn imagebase(&self, slice: usize) -> Option { + let binding = self.macho.iter().nth(slice)?; + for segment in binding.segments() { + if segment.name() == "__TEXT" { + return Some(segment.virtual_address()) + } + } + Some(0) + } + + /// Returns the size of the headers for the MachO slice. + /// + /// # Returns + /// A `Option` representing the size of the headers for the MachO slice. + pub fn sizeofheaders(&self, slice: usize) -> Option { + let binding = self.macho.iter().nth(slice)?; + let architecture = self.architecture(slice)?; + let macho_header_size: u32 = match architecture { + Architecture::AMD64 => 32, + Architecture::I386 => 28, + _ => { return None; } + }; + Some(macho_header_size as u64 + binding.header().sizeof_cmds() as u64) + } + + /// Returns the architecture of the MachO binary by index. + /// + /// # Returns + /// A `Option` representing the architecture of the binary. + pub fn architecture(&self, slice: usize) -> Option { + let cpu_type = self.macho.iter().nth(slice).map(|b|b.header().cpu_type()); + if cpu_type.is_none() { return None; } + let architecture = match cpu_type.unwrap() { + MachoCpuType::X86 => Architecture::I386, + MachoCpuType::X86_64 => Architecture::AMD64, + _ => { return None; }, + }; + Some(architecture) + } + + /// Checks if the symbol n_type is a function. + /// + /// # Returns + /// A `bool` representing if the n_type is a function or not. + pub fn is_function_symbol_type(n_type: u8) -> bool { + (n_type & N_STAB) == 0 && (n_type & N_TYPE) == N_SECT + } + + pub fn symbols(&self, slice: usize) -> BTreeMap { + let mut symbols = BTreeMap::::new(); + let binding = self.macho.iter().nth(slice); + if binding.is_none() { return symbols; } + for symbol in binding.unwrap().symbols() { + if !MACHO::is_function_symbol_type(symbol.get_type()) { continue; } + symbols.insert(symbol.value(), BlSymbol{ + symbol_type: "function".to_string(), + name: symbol.name(), + address: symbol.value(), + }); + } + symbols + } + + /// Returns a set of function addresses identified in the MachO slice. + /// + /// # Returns + /// A `BTreeSet` of function addresses for the MachO slice. + pub fn entrypoints(&self, slice: usize) -> BTreeSet { + let mut entrypoints = BTreeSet::::new(); + if self.entrypoint(slice).is_some() { + entrypoints.insert(self.entrypoint(slice).unwrap()); + } + entrypoints.extend(self.symbols(slice).keys()); + entrypoints.extend(self.exports(slice)); + entrypoints + } + + /// Checks if the provided segment flags contain the executable flag. + /// + /// # Arguments + /// + /// `segment_flags` The segment flags as a `u32` + /// + /// # Returns + /// A `bool` representing if the segment flags contain the executable flag or not. + pub fn is_segment_flags_executable(segment_flags: u32) -> bool { + (segment_flags & VM_PROT_EXECUTE as u32) != 0 + } + + /// Checks if the symbol n_type is a function. + /// + /// # Arguments + /// + /// `slice` The index representing a binary contained in the MacO fat binary format. + /// + /// # Returns + /// A `BTreeSet` containing the virtual addresses of function export addresses. + pub fn exports(&self, slice: usize) -> BTreeSet { + let mut result = BTreeSet::::new(); + let binding = self.macho.iter().nth(slice); + if binding.is_none() { return result; } + let binding = binding.unwrap(); + let dyld_exports_trie = binding.dyld_exports_trie(); + if dyld_exports_trie.is_none() { return result; } + for export in dyld_exports_trie.unwrap().exports() { + result.insert(self.imagebase(slice).unwrap() + export.address()); + } + result + } + + pub fn executable_virtual_address_ranges(&self, slice: usize) -> BTreeMap { + let mut result = BTreeMap::::new(); + let binding = self.macho.iter().nth(slice); + if binding.is_none() { return result; } + for segment in binding.unwrap().segments() { + if !MACHO::is_segment_flags_executable(segment.init_protection()) { continue; } + for section in segment.sections() { + if [ + "__cstring", + "__const", + "__info_plist", + "__unwind_info", + "__eh_frame", + "__stubs", + "__stub_helper"].contains(§ion.name().as_str()) { continue; } + if !section.flags().contains(SectionFlags::PURE_INSTRUCTIONS) { continue; } + if !section.flags().contains(SectionFlags::SOME_INSTRUCTIONS) { continue; } + let start = section.virtual_address(); + let end = start + section.size(); + result.insert(start, end); + } + } + result + } + + /// Caches the MachO file contents and returns a `MemoryMappedFile` object. + /// + /// # Parameters + /// - `slice`: The MachoO binary slice. + /// + /// # Returns + /// A `Result` containing the `MemoryMappedFile` object on success or an `Error` on failure. + pub fn image(&self, slice: usize) -> Result { + let pathbuf = PathBuf::from(self.config.mmap.directory.clone()) + .join(self.file.sha256_no_config().unwrap()); + + let mut tempmap = match MemoryMappedFile::new( + pathbuf, + self.config.mmap.cache.enabled) { + Ok(tempmmap) => tempmmap, + Err(error) => return Err(error), + }; + + if tempmap.is_cached() { + return Ok(tempmap); + } + + let sizeofheaders = self.sizeofheaders(slice); + tempmap.seek_to_end()?; + tempmap.write(&self.file.data[0..sizeofheaders.unwrap() as usize])?; + + let binary = match self.macho.iter().nth(slice) { + Some(binary) => binary, + None => { + return Err(Error::new( + ErrorKind::InvalidData, + "Invalid Mach-O slice", + )); + } + }; + + for segment in binary.segments() { + let segment_virtual_address = segment.virtual_address(); + if segment_virtual_address > tempmap.size()? as u64 { + let padding_length = segment_virtual_address - tempmap.size()? as u64; + tempmap.seek_to_end()?; + tempmap.write_padding(padding_length as usize)?; + } + if !matches!(segment.command_type(), LoadCommandTypes::Segment | LoadCommandTypes::Segment64) { continue; } + let segment_file_offset = segment.file_offset() as usize; + let segment_size = segment.file_size() as usize; + if segment_file_offset + segment_size <= self.file.data.len() { + tempmap.seek_to_end()?; + tempmap.write(&self.file.data[segment_file_offset..segment_file_offset + segment_size])?; + } else { + return Err(Error::new( + ErrorKind::InvalidData, + "macho slice segment size exceeds file data length", + )); + } + } + + Ok(tempmap) + } + +} diff --git a/src/formats/mod.rs b/src/formats/mod.rs new file mode 100644 index 00000000..94ec237f --- /dev/null +++ b/src/formats/mod.rs @@ -0,0 +1,10 @@ +pub mod file; +pub mod pe; +pub mod elf; +pub mod macho; +pub mod cli; + +pub use pe::PE; +pub use file::File; +pub use elf::ELF; +pub use macho::MACHO; diff --git a/src/formats/pe.rs b/src/formats/pe.rs new file mode 100644 index 00000000..99b4e1d0 --- /dev/null +++ b/src/formats/pe.rs @@ -0,0 +1,879 @@ +use lief::generic::Section; +use lief::Binary; +use lief::pe::section::Characteristics; +use std::io::{Cursor, Error, ErrorKind}; +use std::collections::BTreeSet; +use std::collections::HashMap; +use std::path::PathBuf; +use lief::pe::headers::MachineType; +use crate::Architecture; +use crate::formats::File; +use std::collections::BTreeMap; +use lief::pe::debug::Entries; +use crate::types::MemoryMappedFile; +use crate::Config; +use lief::pe::data_directory::Type as DATA_DIRECTORY; +use crate::formats::cli::Cor20Header; +use crate::formats::cli::StorageHeader; +use crate::formats::cli::StorageSignature; +use crate::formats::cli::StreamHeader; +use crate::formats::cli::MetadataTable; +use crate::formats::cli::Entry; +use crate::formats::cli::MetadataToken; +use crate::formats::cli::MethodDefEntry; +use crate::formats::cli::MethodHeader; +use crate::formats::cli::ModuleEntry; +use crate::formats::cli::TypeRefEntry; +use crate::formats::cli::FieldEntry; +use crate::formats::cli::TinyHeader; +use crate::formats::cli::FatHeader; +use crate::formats::cli::TypeDefEntry; + +/// Represents a PE (Portable Executable) file, encapsulating the `lief::pe::Binary` and associated metadata. +pub struct PE { + pe: lief::pe::Binary, + pub file: File, + pub config: Config, +} + +impl PE { + /// Creates a new `PE` instance by reading a PE file from the provided path. + /// + /// # Parameters + /// - `path`: The file path to the PE file to be loaded. + /// + /// # Returns + /// A `Result` containing the `PE` object on success or an `Error` on failure. + pub fn new(path: String, config: Config) -> Result { + let mut file = File::new(path.clone(), config.clone())?; + match file.read() { + Ok(_) => (), + Err(_) => { + return Err(Error::new(ErrorKind::InvalidInput, "failed to read file")); + } + }; + if let Some(Binary::PE(pe)) = Binary::parse(&path) { + return Ok(Self { + pe: pe, + file: file, + config: config, + }); + } + return Err(Error::new(ErrorKind::InvalidInput, "invalid pe file")); + } + + /// Converts a relative virtual address to a file offset + /// + /// # Returns + /// The file offset as a `Option`. + pub fn relative_virtual_address_to_file_offset(&self, rva: u64) -> Option { + for section in self.pe.sections() { + let section_start_rva = section.virtual_address() as u64; + let section_end_rva = section_start_rva + section.virtual_size() as u64; + if rva >= section_start_rva && rva < section_end_rva { + let section_offset = rva - section_start_rva; + let file_offset = section.pointerto_raw_data() as u64 + section_offset; + return Some(file_offset); + } + } + None + } + + /// Parses the .NET Core 2.0 header from the PE file if it is a .NET executable. + /// + /// This function attempts to locate and parse the CLR runtime header by resolving its + /// virtual address and reading its data from the file. If successful, it returns the + /// file offset of the header and a reference to the parsed `Cor20Header` structure. + /// + /// # Returns + /// + /// * `Option<(u64, &Cor20Header)>` - A tuple containing: + /// * The file offset of the header as `u64`. + /// * A reference to the parsed `Cor20Header` structure. + /// * `None` - If the file is not a .NET executable or the header cannot be parsed. + fn dotnet_parse_cor20_header(&self) -> Option<(u64, &Cor20Header)> { + if !self.is_dotnet() { return None; } + if let Some(clr_runtime_header) = self.pe.data_directory_by_type(DATA_DIRECTORY::CLR_RUNTIME_HEADER) { + if let Some(start) = self.relative_virtual_address_to_file_offset(clr_runtime_header.rva() as u64) { + let end = start + clr_runtime_header.size() as u64; + let data = &self.file.data[start as usize..end as usize]; + let header = &Cor20Header::from_bytes(&data)?; + return Some((start, header)); + } + } + None + } + + /// Retrieves the .NET Core 2.0 header from the PE file if it is a .NET executable. + /// + /// This function provides a simpler interface to access the `Cor20Header` directly + /// by internally calling `dotnet_parse_cor20_header` and returning only the header. + /// + /// # Returns + /// + /// * `Option<&Cor20Header>` - A reference to the parsed `Cor20Header` structure. + /// * `None` - If the file is not a .NET executable or the header cannot be parsed. + pub fn dotnet_cor20_header(&self) -> Option<&Cor20Header> { + Some(self.dotnet_parse_cor20_header()?.1) + } + + /// Parses the .NET storage signature from the metadata of a PE file. + /// + /// This function attempts to locate and parse the storage signature in the + /// metadata section of the PE file, based on the metadata virtual address + /// specified in the `Cor20Header`. + /// + /// # Returns + /// + /// * `Option<(u64, &StorageSignature)>` - A tuple containing: + /// * The file offset of the storage signature as `u64`. + /// * A reference to the parsed `StorageSignature` structure. + /// * `None` - If the file is not a .NET executable or the storage signature + /// cannot be parsed. + fn dotnet_parse_storage_signature(&self) -> Option<(u64, &StorageSignature)> { + if !self.is_dotnet() { return None; } + let (_, image_cor20_header) = self.dotnet_parse_cor20_header()?; + let rva = image_cor20_header.meta_data.virtual_address as u64; + let start = self.relative_virtual_address_to_file_offset(rva)? as usize; + let end = start + StorageSignature::size(); + let data = &self.file.data[start..end]; + let header = StorageSignature::from_bytes(&data)?; + Some((start as u64, header)) + } + + /// Retrieves the .NET storage signature from the metadata of a PE file. + /// + /// This function provides a simpler interface to access the `StorageSignature` directly + /// by internally calling `dotnet_parse_storage_signature` and returning only the signature. + /// + /// # Returns + /// + /// * `Option<&StorageSignature>` - A reference to the parsed `StorageSignature` structure. + /// * `None` - If the file is not a .NET executable or the storage signature cannot be parsed. + pub fn dotnet_storage_signature(&self) -> Option<&StorageSignature> { + Some(self.dotnet_parse_storage_signature()?.1) + } + + /// Parses the .NET storage header from the metadata of a PE file. + /// + /// This function attempts to locate and parse the `StorageHeader` in the metadata + /// section of the PE file. It calculates the starting position based on the size of + /// the `StorageSignature` and the version string, then reads and parses the + /// header data. + /// + /// # Returns + /// + /// * `Option<(u64, &StorageHeader)>` - A tuple containing: + /// * The file offset of the storage header as `u64`. + /// * A reference to the fparsed `StorageHeader` structure. + /// * `None` - If the file is not a .NET executable or the storage header cannot be parsed. + fn dotnet_parse_storage_header(&self) -> Option<(u64, &StorageHeader)> { + if !self.is_dotnet() { return None; }; + let (mut start, cor20_storage_signaure_header) = self.dotnet_parse_storage_signature()?; + start += StorageSignature::size() as u64; + start += cor20_storage_signaure_header.version_string_size as u64; + start -= 4 as u64; + let end = start as usize + StorageHeader::size() as usize; + let data = &self.file.data[start as usize..end]; + let header = StorageHeader::from_bytes(data)?; + Some((start, header)) + } + + /// Retrieves the .NET storage header from the metadata of a PE file. + /// + /// This function provides a simpler interface to access the `StorageHeader` directly + /// by internally calling `dotnet_parse_storage_header` and returning only the header. + /// + /// # Returns + /// + /// * `Option<&StorageHeader>` - A reference to the parsed `StorageHeader` structure. + /// * `None` - If the file is not a .NET executable or the storage header cannot be parsed. + pub fn dotnet_storage_header(&self) -> Option<&StorageHeader> { + Some(self.dotnet_parse_storage_header()?.1) + } + + /// Parses the .NET stream headers from the metadata of a PE file. + /// + /// This function reads and parses the stream headers defined in the metadata section + /// of the PE file. It calculates the starting position based on the `StorageHeader` + /// and iterates through the number of streams specified, creating a `BTreeMap` of the + /// file offsets and their corresponding `StreamHeader` structures. + /// + /// # Returns + /// + /// * `Option>` - A map where: + /// * The keys are the file offsets of the stream headers as `u64`. + /// * The values are references to the parsed `StreamHeader` structures. + /// * `None` - If the file is not a .NET executable, the storage header cannot be parsed, + /// or no stream headers are found. + fn dotnet_parse_stream_headers(&self) -> Option> { + if !self.is_dotnet() { return None; } + let (cor20_storage_header_offset, cor20_storage_header) = self.dotnet_parse_storage_header()?; + let mut offset = cor20_storage_header_offset as usize + StorageHeader::size(); + let mut result = BTreeMap::::new(); + for _ in 0.. cor20_storage_header.number_of_streams { + let data = &self.file.data[offset..offset + StreamHeader::size()]; + let header = StreamHeader::from_bytes(data)?; + result.insert(offset as u64, header); + offset += StreamHeader::size() + header.name().len(); + } + if result.len() <= 0 { + return None; + } + Some(result) + } + + /// Retrieves the .NET stream headers from the metadata of a PE file as a vector. + /// + /// This function provides a simpler interface to access the `StreamHeader` structures + /// directly by internally calling `dotnet_parse_stream_headers` and returning only the + /// parsed headers in a vector. + /// + /// # Returns + /// + /// * `Vec<&StreamHeader>` - A vector of references to the parsed `StreamHeader` structures. + /// * An empty vector - If the file is not a .NET executable or the stream headers cannot + /// be parsed. + pub fn dotnet_stream_headers(&self) -> Vec<&StreamHeader> { + let mut result = Vec::<&StreamHeader>::new(); + let headers = self.dotnet_parse_stream_headers(); + if headers.is_none() { return result; } + for (_, header) in headers.unwrap() { + result.push(header); + } + result + } + + /// Parses the .NET metadata table from the metadata of a PE file. + /// + /// This function locates and parses the `MetadataTable` in the metadata section of the + /// PE file. It identifies the stream header with the `#~` name, calculates the correct + /// offset based on its location and the storage signature, and reads the metadata table data. + /// + /// # Returns + /// + /// * `Option<(u64, &MetadataTable)>` - A tuple containing: + /// * The file offset of the metadata table as `u64`. + /// * A reference to the parsed `MetadataTable` structure. + /// * `None` - If the file is not a .NET executable, the relevant stream header cannot + /// be found, or the metadata table cannot be parsed. + fn dotnet_parse_metadata_table(&self) -> Option<(u64, &MetadataTable)> { + if !self.is_dotnet() { return None; } + let (mut start, _) = self.dotnet_parse_storage_signature()?; + for (_, header) in self.dotnet_parse_stream_headers()? { + if header.name() == vec![0x23, 0x7e, 0x00, 0x00] { + start += header.offset as u64; + } + } + let data = &self.file.data[start as usize..start as usize + MetadataTable::size()]; + Some((start, MetadataTable::from_bytes(data)?)) + } + + /// Retrieves the .NET metadata table from the metadata of a PE file. + /// + /// This function provides a simpler interface to access the `MetadataTable` directly + /// by internally calling `dotnet_parse_metadata_table` and returning only the parsed table. + /// + /// # Returns + /// + /// * `Option<&MetadataTable>` - A reference to the parsed `MetadataTable` structure. + /// * `None` - If the file is not a .NET executable or the metadata table cannot be parsed. + pub fn dotnet_metadata_table(&self) -> Option<&MetadataTable> { + Some(self.dotnet_parse_metadata_table()?.1) + } + + /// Parses and retrieves the entries from the .NET metadata table of a PE file. + /// + /// This function iterates through the metadata table entries specified in the + /// `MetadataTable` structure, reading and parsing each entry based on its type + /// (e.g., `Module`, `TypeRef`, `TypeDef`, `Field`, `MethodDef`). The function calculates + /// the correct offsets, validates entry counts, and constructs a vector of parsed entries. + /// + /// # Returns + /// + /// * `Option>` - A vector containing parsed entries from the metadata table. + /// Each entry is wrapped in the `Entry` enum to represent its specific type. + /// * `None` - If the file is not a .NET executable, the metadata table cannot be parsed, + /// or an error occurs during entry parsing. + /// + /// # Notes + /// + /// * This function uses `MetadataToken` to determine the type of each metadata table entry. + /// * The parsing depends on the `heap_sizes` field in the `MetadataTable` to correctly interpret + /// data sizes within entries. + /// * If an invalid offset or entry count is encountered, the function will return `None`. + pub fn dotnet_metadata_table_entries(&self) -> Option> { + if !self.is_dotnet() { return None; } + + let (cor20_metadata_table_offset, cor20_metadata_table) = self.dotnet_parse_metadata_table()?; + + let mut offset: usize = cor20_metadata_table_offset as usize + + MetadataTable::size() + + cor20_metadata_table.mask_valid.count_ones() as usize * 4; + + let mut valid_index: usize = 0; + + let mut entries = Vec::::new(); + + for i in 0..64 as usize { + + let entry_offset = cor20_metadata_table_offset as usize + + MetadataTable::size() + + (valid_index * 4); + + if entry_offset + 4 > self.file.data.len() { + return None; + } + + let entry_count = u32::from_le_bytes( + self.file.data[entry_offset..entry_offset + 4].try_into().unwrap(), + ) as usize; + + match i { + x if x == MetadataToken::Module as usize => { + for _ in 0..entry_count { + let entry = ModuleEntry::from_bytes( + &self.file.data[offset..], + cor20_metadata_table.heap_sizes)?; + offset += entry.size(); + entries.push(Entry::Module(entry)); + } + valid_index += 1; + } + x if x == MetadataToken::TypeRef as usize => { + for _ in 0..entry_count { + let entry = TypeRefEntry::from_bytes( + &self.file.data[offset..], + cor20_metadata_table.heap_sizes)?; + offset += entry.size(); + entries.push(Entry::TypeRef(entry)); + } + valid_index += 1; + } + x if x == MetadataToken::TypeDef as usize => { + for _ in 0..entry_count { + let entry = TypeDefEntry::from_bytes( + &self.file.data[offset..], + cor20_metadata_table.heap_sizes, + )?; + offset += entry.size(); + entries.push(Entry::TypeDef(entry)); + } + valid_index += 1; + } + x if x == MetadataToken::Field as usize => { + for _ in 0..entry_count { + let entry = FieldEntry::from_bytes( + &self.file.data[offset..], + cor20_metadata_table.heap_sizes, + )?; + offset += entry.size(); + entries.push(Entry::Field(entry)); + } + valid_index += 1; + } + x if x == MetadataToken::MethodDef as usize => { + for _ in 0..entry_count { + let entry = MethodDefEntry::from_bytes( + &self.file.data[offset..], + cor20_metadata_table.heap_sizes)?; + offset += entry.size(); + entries.push(Entry::MethodDef(entry)); + } + } + _ => {} + } + } + + Some(entries) + } + + /// Converts a virtual address to a relative virtual address (RVA). + /// + /// This function computes the relative virtual address by subtracting the image base + /// address of the file from the given virtual address. + /// + /// # Parameters + /// + /// * `address` - The virtual address (`u64`) to be converted. + /// + /// # Returns + /// + /// * `u64` - The relative virtual address (RVA). + pub fn virtual_address_to_relative_virtual_address(&self, address: u64) -> u64{ + address - self.imagebase() + } + + /// Converts a virtual address to a file offset in the PE file. + /// + /// This function first converts the virtual address to a relative virtual address (RVA) + /// using `virtual_address_to_relative_virtual_address` and then resolves the RVA to a + /// file offset using `relative_virtual_address_to_file_offset`. + /// + /// # Parameters + /// + /// * `address` - The virtual address (`u64`) to be converted. + /// + /// # Returns + /// + /// * `Option` - The file offset corresponding to the given virtual address, or + /// `None` if the conversion fails. + pub fn virtual_address_to_file_offset(&self, address: u64) -> Option { + let rva = self.virtual_address_to_relative_virtual_address(address); + self.relative_virtual_address_to_file_offset(rva) + } + + /// Parses and retrieves a method header from a given virtual address in the PE file. + /// + /// This function identifies and parses the method header (either Tiny or Fat) + /// associated with the given virtual address. The header type is determined based + /// on specific bits in the header's first byte. If the address is invalid or the + /// data does not correspond to a valid method header, an error is returned. + /// + /// # Parameters + /// + /// * `address` - The virtual address (`u64`) of the method header. + /// + /// # Returns + /// + /// * `Result` - + /// * `Ok(MethodHeader)` - The parsed method header as either `Tiny` or `Fat`. + /// * `Err(Error)` - If the virtual address is invalid or the data is not a valid method header. + pub fn dotnet_method_header(&self, address: u64) -> Result { + + let offset = self.virtual_address_to_file_offset(address); + + if offset.is_none() { return Err(Error::new(ErrorKind::InvalidInput, "failed to convert virtual address to file offset")); } + + let bytes = &self.file.data[offset.unwrap() as usize..offset.unwrap() as usize + 12]; + + if bytes[0] & 0b11 == 0b10 { + let code_size = bytes[0] >> 2; + let tiny_header = TinyHeader { code_size }; + return Ok(MethodHeader::Tiny(tiny_header)); + } + if bytes[0] & 0b11 == 0b11 { + let fat_header = FatHeader::from_bytes(bytes)?; + return Ok(MethodHeader::Fat(fat_header)); + } + return Err(Error::new(ErrorKind::InvalidData, "invalid method header")); + } + + /// Checks if the PE file is a .NET assembly. + /// + /// This function inspects the imports of the PE file to identify whether it is a .NET application. + /// It does so by looking for the presence of specific .NET-related DLLs (`mscorelib.dll` and `mscoree.dll`) + /// in the import table and confirming the existence of a CLR runtime header. + /// + /// # Returns + /// + /// - `true` if the PE file is a .NET assembly. + /// - `false` otherwise. + #[allow(dead_code)] + pub fn is_dotnet(&self) -> bool { + self.pe.imports().any(|import| { + matches!(import.name().to_lowercase().as_str(), "mscorelib.dll" | "mscoree.dll") + && self.pe.data_directory_by_type(DATA_DIRECTORY::CLR_RUNTIME_HEADER).is_some() + }) + } + + /// Creates a new `PE` instance from a byte vector containing PE file data. + /// + /// # Parameters + /// - `bytes`: A vector of bytes representing the PE file data. + /// + /// # Returns + /// A `Result` containing the `PE` object on success or an `Error` on failure. + #[allow(dead_code)] + pub fn from_bytes(bytes: Vec, config: Config) -> Result { + let file = File::from_bytes(bytes, config.clone()); + let mut cursor = Cursor::new(&file.data); + if let Some(Binary::PE(pe)) = Binary::from(&mut cursor) { + return Ok(Self{ + pe: pe, + file: file, + config: config, + }) + } + return Err(Error::new(ErrorKind::InvalidInput, "invalid pe file")); + } + + /// Returns the architecture of the PE file based on its machine type. + /// + /// # Returns + /// The `BinaryArchitecture` enum value corresponding to the PE machine type (e.g., AMD64, I386, CIL or UNKNOWN). + #[allow(dead_code)] + pub fn architecture(&self) -> Architecture { + match self.pe.header().machine() { + MachineType::I386 if self.is_dotnet() => Architecture::CIL, + MachineType::I386 => Architecture::I386, + MachineType::AMD64 => Architecture::AMD64, + _ => Architecture::UNKNOWN, + } + } + + /// Retrieves the virtual address ranges of executable methods in a .NET executable. + /// + /// This function scans the .NET metadata table for `MethodDef` entries and computes + /// the virtual address ranges for executable methods. It uses the relative virtual + /// address (RVA) of each method to determine its virtual address and extracts the + /// method's header to calculate the start and end addresses of the method's executable code. + /// + /// # Returns + /// + /// * `BTreeMap` - A map where: + /// * Keys represent the start of the method's executable code (virtual address). + /// * Values represent the end of the method's executable code (virtual address). + pub fn dotnet_executable_virtual_address_ranges(&self) -> BTreeMap { + let mut result = BTreeMap::::new(); + if !self.is_dotnet() { return result; } + let entries = match self.dotnet_metadata_table_entries() { + Some(entries) => entries, + None => return result, + }; + + for entry in entries { + let Entry::MethodDef(entry) = entry else { continue }; + + if entry.rva == 0 { + continue; + } + + let va = self.relative_virtual_address_to_virtual_address(entry.rva as u64); + + let header = match self.dotnet_method_header(va).ok() { + Some(header) => header, + None => continue, + }; + + let header_size = match header.size() { + Some(size) => size, + None => continue, + }; + + let code_size = match header.code_size() { + Some(size) => size, + None => continue, + }; + + result.insert( va + header_size as u64, va + header_size as u64 + code_size as u64); + } + result + } + + /// Returns the ranges of executable memory addresses within the PE file. + /// + /// This includes sections marked as executable (`MEM_EXECUTE`) and with valid data. + /// + /// # Returns + /// A `BTreeMap` where the key is the start address of the executable range and the value is the end address. + #[allow(dead_code)] + pub fn executable_virtual_address_ranges(&self) -> BTreeMap { + let mut result = BTreeMap::::new(); + if self.is_dotnet() { return result; } + for section in self.pe.sections() { + if (section.characteristics().bits() & u64::from(Characteristics::MEM_EXECUTE)) == 0 { continue; } + if section.virtual_size() == 0 { continue; } + if section.sizeof_raw_data() == 0 { continue; } + let section_virtual_adddress = PE::align_section_virtual_address( + self.imagebase() + section.pointerto_raw_data() as u64, + self.section_alignment(), + self.file_alignment()); + result.insert( + section_virtual_adddress, + section_virtual_adddress + section.virtual_size() as u64); + } + return result; + } + + /// Returns a map of Pogo (debug) entries found in the PE file, keyed by their start RVA (Relative Virtual Address). + /// + /// # Returns + /// A `HashMap` where the key is the RVA of the start of the Pogo entry and the value is the name of the entry. + #[allow(dead_code)] + pub fn pogos(&self) -> HashMap { + let mut result = HashMap::::new(); + for entry in self.pe.debug() { + match entry { + Entries::Pogo(pogos) => { + for pogo in pogos.entries() { + result.insert(self.imagebase() + pogo.start_rva() as u64, pogo.name()); + } + }, + _ => {} + } + + } + result + } + + /// Returns a set of TLS (Thread Local Storage) callback addresses in the PE file. + /// + /// The method retrieves the TLS callbacks from the PE file's TLS data directory, if present. + /// TLS callbacks are functions that are called when a thread is created or terminated, and they + /// are often used in applications to initialize or clean up thread-local data. + /// + /// # Returns + /// A `BTreeSet` containing the addresses of the TLS callback functions. + pub fn tlscallbacks(&self) -> BTreeSet { + self.pe.tls() + .into_iter() + .flat_map(|tls| tls.callbacks()) + .collect() + } + + /// Returns a set of dotnet function virtual addresses in the PE file. + /// + /// # Returns + /// A `BTreeSet` of function addresses in the PE file. + pub fn dotnet_entrypoints(&self) -> BTreeSet { + let mut addresses = BTreeSet::::new(); + if !self.is_dotnet() { return addresses; } + let entries = match self.dotnet_metadata_table_entries() { + Some(entries) => entries, + None => { + return addresses; + }, + }; + for entry in entries { + match entry { + Entry::MethodDef(header) => { + if header.rva <= 0 { continue; } + let mut va = self.relative_virtual_address_to_virtual_address(header.rva as u64); + let method_header = match self.dotnet_method_header(va) { + Ok(method_header) => method_header, + Err(_) => { + continue; + } + }; + if method_header.size().is_none() { + continue; + } + va += method_header.size().unwrap() as u64; + addresses.insert(va); + + }, + _ => {}, + }; + } + return addresses; + } + + /// Returns a set of function addresses (entry point, exports, TLS callbacks, and Pogo entries) in the PE file. + /// + /// # Returns + /// A `BTreeSet` of function addresses in the PE file. + #[allow(dead_code)] + pub fn entrypoints(&self) -> BTreeSet { + let mut addresses = BTreeSet::::new(); + addresses.insert(self.entrypoint()); + addresses.extend(self.exports()); + addresses.extend(self.tlscallbacks()); + addresses.extend(self.pogos().keys().cloned()); + return addresses; + } + + /// Returns the entry point address of the PE file. + /// + /// # Returns + /// The entry point address as a `u64` value. + #[allow(dead_code)] + pub fn entrypoint(&self) -> u64 { + self.imagebase() + self.pe.optional_header().addressof_entrypoint() as u64 + } + + /// Returns the size of the headers of the PE file. + /// + /// # Returns + /// The size of the headers as a `u64` value. + #[allow(dead_code)] + pub fn sizeofheaders(&self) -> u64 { + self.pe.optional_header().sizeof_headers() as u64 + } + + /// Aligns a section's virtual address to the specified section and file alignment boundaries. + /// + /// # Parameters + /// - `value`: The virtual address to align. + /// - `section_alignment`: The section alignment boundary. + /// - `file_alignment`: The file alignment boundary. + /// + /// # Returns + /// The aligned virtual address. + #[allow(dead_code)] + pub fn align_section_virtual_address(value: u64, mut section_alignment: u64, file_alignment: u64) -> u64 { + if section_alignment < 0x1000 { + section_alignment = file_alignment; + } + if section_alignment != 0 && (value % section_alignment) != 0 { + return section_alignment * ((value + section_alignment - 1) / section_alignment); + } + return value; + } + + /// Returns the section alignment used in the PE file. + /// + /// # Returns + /// The section alignment value as a `u64`. + #[allow(dead_code)] + pub fn section_alignment(&self) -> u64 { + self.pe.optional_header().section_alignment() as u64 + } + + /// Returns the file alignment used in the PE file. + /// + /// # Returns + /// The file alignment value as a `u64`. + #[allow(dead_code)] + pub fn file_alignment(&self) -> u64 { + self.pe.optional_header().file_alignment() as u64 + } + + /// Converts a relative virtual address to a virtual address + /// + /// # Returns + /// The virtual address as a `u64`. + #[allow(dead_code)] + pub fn relative_virtual_address_to_virtual_address(&self, relative_virtual_address: u64) -> u64 { + self.imagebase() + relative_virtual_address + } + + /// Converts a file offset to a virtual address. + /// + /// This method looks through the PE file's sections to determine which section contains the file offset. + /// It then computes the corresponding virtual address within that section. + /// + /// # Parameters + /// - `file_offset`: The file offset (raw data offset) to convert to a virtual address. + /// + /// # Returns + /// The corresponding virtual address as a `u64`. + #[allow(dead_code)] + pub fn file_offset_to_virtual_address(&self, file_offset: u64) -> Option { + for section in self.pe.sections() { + let section_raw_data_offset = section.pointerto_raw_data() as u64; + let section_raw_data_size = section.sizeof_raw_data() as u64; + if file_offset >= section_raw_data_offset && file_offset < section_raw_data_offset + section_raw_data_size { + let section_virtual_address = self.imagebase() + section.pointerto_raw_data() as u64; + let section_offset = file_offset - section_raw_data_offset; + let virtual_address = section_virtual_address + section_offset; + return Some(virtual_address); + } + } + None + } + + /// Caches the PE file contents and returns a `MemoryMappedFile` object. + /// + /// # Parameters + /// - `path`: The base path to store the memory mapped file. + /// - `cache`: Whether to cache the file or not. + /// + /// # Returns + /// A `Result` containing the `MemoryMappedFile` object on success or an `Error` on failure. + pub fn image(&self) -> Result { + let pathbuf = PathBuf::from(self.config.mmap.directory.clone()) + .join(self.file.sha256_no_config().unwrap()); + let mut tempmap = match MemoryMappedFile::new(pathbuf, self.config.mmap.cache.enabled) { + Ok(tempmmap) => tempmmap, + Err(error) => return Err(error), + }; + if tempmap.is_cached() { + return Ok(tempmap); + } + tempmap.seek_to_end()?; + tempmap.write(&self.file.data[0..self.sizeofheaders() as usize]).map_err(|error| Error::new( + ErrorKind::Other, + format!( + "failed to write headers to memory-mapped pe file: {}", + error + ) + ))?; + for section in self.pe.sections() { + if section.virtual_size() == 0 { continue; } + if section.sizeof_raw_data() == 0 { continue; } + let section_virtual_adddress = PE::align_section_virtual_address( + self.imagebase() + section.pointerto_raw_data() as u64, + self.section_alignment(), + self.file_alignment()); + if section_virtual_adddress > tempmap.size().unwrap() as u64 { + let padding_length = section_virtual_adddress - tempmap.size().unwrap() as u64; + tempmap.seek_to_end()?; + tempmap.write_padding(padding_length as usize).map_err(|error| Error::new( + ErrorKind::Other, + format!( + "write padding to pe memory-mapped pe file: {}", + error + ) + ))?; + } + let pointerto_raw_data = section.pointerto_raw_data() as usize; + let sizeof_raw_data = section.sizeof_raw_data() as usize; + tempmap.seek_to_end()?; + tempmap.write(&self.file.data[pointerto_raw_data..pointerto_raw_data + sizeof_raw_data]).map_err(|error| Error::new( + ErrorKind::Other, + format!( + "failed to write section to memory-mapped pe file: {}", + error + ) + ))?; + } + Ok(tempmap) + } + + /// Returns the size of the PE file. + /// + /// # Returns + /// The size of the file as a `u64`. + #[allow(dead_code)] + pub fn size(&self) -> u64 { + self.file.size() + } + + /// Returns the TLS (Thread Local Storage) hash value if present in the PE file. + /// + /// # Returns + /// An `Option` containing the TLS hash if present, otherwise `None`. + #[allow(dead_code)] + pub fn tlsh(&self) -> Option { + self.file.tlsh() + } + + /// Returns the SHA-256 hash value of the PE file. + /// + /// # Returns + /// An `Option` containing the SHA-256 hash if available, otherwise `None`. + #[allow(dead_code)] + pub fn sha256(&self) -> Option { + self.file.sha256() + } + + /// Returns the base address (image base) of the PE file. + /// + /// # Returns + /// The image base address as a `u64`. + #[allow(dead_code)] + pub fn imagebase(&self) -> u64 { + self.pe.optional_header().imagebase() + } + + /// Returns a set of exported function addresses in the PE file. + /// + /// # Returns + /// A `BTreeSet` of exported function addresses. + #[allow(dead_code)] + pub fn exports(&self) -> BTreeSet { + let mut addresses = BTreeSet::::new(); + let export = match self.pe.export(){ + Some(export) => export, + None => { + return addresses; + } + }; + for entry in export.entries(){ + let address = entry.address() as u64 + self.imagebase(); + addresses.insert(address); + } + return addresses; + } +} diff --git a/src/global/architecture.rs b/src/global/architecture.rs new file mode 100644 index 00000000..28c101b9 --- /dev/null +++ b/src/global/architecture.rs @@ -0,0 +1,57 @@ +use std::str::FromStr; +use std::fmt; + +/// Represents the different architectures of a binary. +#[repr(u16)] +#[derive(Copy, Clone, PartialEq, Debug)] +pub enum Architecture { + /// 64-bit AMD architecture. + AMD64 = 0x00, + /// 32-bit Intel architecture. + I386 = 0x01, + /// CIL + CIL = 0x02, + /// Unknown architecture. + UNKNOWN= 0x03, +} + +impl Architecture { + pub fn to_vec() -> Vec { + vec![ + Architecture::AMD64.to_string(), + Architecture::I386.to_string(), + Architecture::CIL.to_string(), + ] + } +} + +impl Architecture { + pub fn to_list() -> String { + Architecture::to_vec().join(", ") + } +} + +/// Implements Display for `BinaryArchitecture` enum +impl fmt::Display for Architecture { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let architecture = match self { + Architecture::AMD64 => "amd64", + Architecture::I386 => "i386", + Architecture::CIL => "cil", + Architecture::UNKNOWN => "unknown", + }; + write!(f, "{}", architecture) + } +} + +impl FromStr for Architecture { + type Err = String; + fn from_str(s: &str) -> Result { + match s { + "amd64" => Ok(Architecture::AMD64), + "i386" => Ok(Architecture::I386), + "cil" => Ok(Architecture::CIL), + _ => Err(format!("invalid architecutre")), + } + } +} diff --git a/src/global/config.rs b/src/global/config.rs new file mode 100644 index 00000000..b9b20b72 --- /dev/null +++ b/src/global/config.rs @@ -0,0 +1,420 @@ +use dirs; +use std::{fs, path::PathBuf}; +use std::io::Error; +use std::io::ErrorKind; +use std::env; +use serde::{Deserialize, Serialize}; +use serde; + +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); +pub const AUTHOR: &str = "@c3rb3ru5d3d53c"; +pub const DIRECTORY: &str = "binlex"; +pub const FILE_NAME: &str = "binlex.toml"; + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigBlocks { + pub hashing: ConfigHashing, + pub heuristics: ConfigHeuristics, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigChromosomes { + pub hashing: ConfigHashing, + pub heuristics: ConfigHeuristics, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigFunctions { + pub hashing: ConfigHashing, + pub heuristics: ConfigHeuristics, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigFile { + pub hashing: ConfigHashing, + pub heuristics: ConfigHeuristics, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigFormats { + pub file: ConfigFile, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct Config { + pub general: ConfigGeneral, + pub formats: ConfigFormats, + pub blocks: ConfigBlocks, + pub functions: ConfigFunctions, + pub chromosomes: ConfigChromosomes, + pub mmap: ConfigMmap, + pub disassembler: ConfigDisassembler, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigDisassembler { + pub sweep: ConfigDisassemblerSweep, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigDisassemblerSweep { + pub enabled: bool, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigHeuristics { + pub features: ConfigHeuristicFeatures, + pub normalized: ConfigHeuristicNormalization, + pub entropy: ConfigHeuristicEntropy, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigHeuristicFeatures { + pub enabled: bool, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigHeuristicNormalization { + pub enabled: bool, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigHeuristicEntropy { + pub enabled: bool, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigHashing { + pub sha256: ConfigSHA256, + pub tlsh: ConfigTLSH, + pub minhash: ConfigMinhash, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigFileHashes { + pub sha256: ConfigSHA256, + pub tlsh: ConfigTLSH, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigGeneral { + pub threads: usize, + pub minimal: bool, + pub debug: bool, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigMmap { + pub directory: String, + pub cache: ConfigMmapCache, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigMmapCache { + pub enabled: bool, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigMinhash { + pub enabled: bool, + pub number_of_hashes: usize, + pub shingle_size: usize, + pub maximum_byte_size: usize, + pub seed: u64, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigTLSH { + pub enabled: bool, + pub minimum_byte_size: usize, +} + +#[derive(Serialize, Deserialize, Clone)] +pub struct ConfigSHA256 { + pub enabled: bool, +} + +impl Config { + #[allow(dead_code)] + pub fn new() -> Self { + Config { + general: ConfigGeneral { + threads: 1, + minimal: false, + debug: false, + }, + formats: ConfigFormats { + file: ConfigFile { + hashing: ConfigHashing { + sha256: ConfigSHA256 { + enabled: true, + }, + tlsh: ConfigTLSH { + enabled: true, + minimum_byte_size: 50, + }, + minhash: ConfigMinhash { + enabled: true, + number_of_hashes: 64, + shingle_size: 4, + maximum_byte_size: 50, + seed: 0, + } + }, + heuristics: ConfigHeuristics { + features: ConfigHeuristicFeatures { + enabled: true, + }, + normalized: ConfigHeuristicNormalization { + enabled: false, + }, + entropy: ConfigHeuristicEntropy { + enabled: true, + } + } + } + }, + blocks: ConfigBlocks { + hashing: ConfigHashing { + sha256: ConfigSHA256 { + enabled: true, + }, + tlsh: ConfigTLSH { + enabled: true, + minimum_byte_size: 50, + }, + minhash: ConfigMinhash { + enabled: true, + number_of_hashes: 64, + shingle_size: 4, + maximum_byte_size: 50, + seed: 0, + } + }, + heuristics: ConfigHeuristics { + features: ConfigHeuristicFeatures { + enabled: true, + }, + normalized: ConfigHeuristicNormalization { + enabled: false, + }, + entropy: ConfigHeuristicEntropy { + enabled: true, + } + } + }, + functions: ConfigFunctions { + hashing: ConfigHashing { + sha256: ConfigSHA256 { + enabled: true, + }, + tlsh: ConfigTLSH { + enabled: true, + minimum_byte_size: 50, + }, + minhash: ConfigMinhash { + enabled: true, + number_of_hashes: 64, + shingle_size: 4, + maximum_byte_size: 50, + seed: 0, + } + }, + heuristics: ConfigHeuristics { + features: ConfigHeuristicFeatures { + enabled: true, + }, + normalized: ConfigHeuristicNormalization { + enabled: false, + }, + entropy: ConfigHeuristicEntropy { + enabled: true, + } + } + }, + chromosomes: ConfigChromosomes { + hashing: ConfigHashing { + sha256: ConfigSHA256 { + enabled: true, + }, + tlsh: ConfigTLSH { + enabled: true, + minimum_byte_size: 50, + }, + minhash: ConfigMinhash { + enabled: true, + number_of_hashes: 64, + shingle_size: 4, + maximum_byte_size: 50, + seed: 0, + } + }, + heuristics: ConfigHeuristics { + features: ConfigHeuristicFeatures { + enabled: true, + }, + normalized: ConfigHeuristicNormalization { + enabled: false, + }, + entropy: ConfigHeuristicEntropy { + enabled: true, + } + } + }, + mmap: ConfigMmap { + directory: Config::default_file_mapping_directory(), + cache: ConfigMmapCache { + enabled: false, + } + }, + disassembler: ConfigDisassembler { + sweep: ConfigDisassemblerSweep { + enabled: true, + } + } + } + } + + pub fn enable_minimal(&mut self) { + self.general.minimal = true; + self.disable_heuristics(); + self.disable_hashing(); + } + + pub fn disable_hashing(&mut self) { + self.disable_block_hashing(); + self.disable_function_hashing(); + self.disable_chromosome_hashing(); + self.disable_file_hashing(); + } + + pub fn disable_chromosome_heuristics(&mut self) { + self.chromosomes.heuristics.entropy.enabled = false; + self.chromosomes.heuristics.features.enabled = false; + self.chromosomes.heuristics.normalized.enabled = false; + } + + pub fn disable_block_hashing(&mut self){ + self.blocks.hashing.sha256.enabled = false; + self.blocks.hashing.tlsh.enabled = false; + self.blocks.hashing.minhash.enabled = false; + } + + pub fn disable_file_hashing(&mut self) { + self.formats.file.hashing.sha256.enabled = false; + self.formats.file.hashing.tlsh.enabled = false; + self.formats.file.hashing.minhash.enabled = false; + } + + pub fn disable_file_heuristics(&mut self) { + self.formats.file.heuristics.entropy.enabled = false; + self.formats.file.heuristics.features.enabled = false; + self.formats.file.heuristics.normalized.enabled = false; + } + + pub fn disable_heuristics(&mut self) { + self.disable_block_heuristics(); + self.disable_function_heuristics(); + self.disable_chromosome_heuristics(); + self.disable_file_heuristics(); + } + + pub fn disable_chromosome_hashing(&mut self) { + self.chromosomes.hashing.sha256.enabled = false; + self.chromosomes.hashing.tlsh.enabled = false; + self.chromosomes.hashing.minhash.enabled = false; + } + + pub fn disable_function_hashing(&mut self) { + self.functions.hashing.sha256.enabled = false; + self.functions.hashing.tlsh.enabled = false; + self.functions.hashing.minhash.enabled = false; + } + + pub fn disable_block_heuristics(&mut self) { + self.blocks.heuristics.entropy.enabled = false; + self.blocks.heuristics.features.enabled = false; + self.blocks.heuristics.normalized.enabled = false; + } + + pub fn disable_function_heuristics(&mut self) { + self.functions.heuristics.entropy.enabled = false; + self.functions.heuristics.features.enabled = false; + self.functions.heuristics.normalized.enabled = false; + } + + // Get Default File Mapping Directory + #[allow(dead_code)] + pub fn default_file_mapping_directory() -> String { + env::temp_dir() + .join(DIRECTORY) + .to_str() + .expect("failed to convert file mapping directory to string") + .to_owned() + } + + /// Prints the Current Configuration + #[allow(dead_code)] + pub fn print(&self) { + println!("{}", self.to_string().unwrap()); + } + + /// Convert Config to a TOML String + #[allow(dead_code)] + pub fn to_string(&self) -> Result { + toml::to_string_pretty(self).map_err(|e| Error::new(ErrorKind::Other, e)) + } + + /// Reads the Configuration TOML from a File Path + pub fn from_file(file_path: &str) -> Result { + let toml_string = fs::read_to_string(file_path)?; + let config: Config = toml::from_str(&toml_string) + .map_err(|error| Error::new(ErrorKind::InvalidData, format!("failed to read configuration file {}\n\n{}", file_path, error)))?; + Ok(config) + } + + /// Write the configuration TOML to a file + #[allow(dead_code)] + pub fn write_to_file(&self, file_path: &str) -> Result<(), Error> { + let toml_string = self.to_string() + .expect("failed to serialize binlex configration to toml format"); + fs::write(file_path, toml_string)?; + Ok(()) + } + + /// Writes Default TOML Configuration File To Configuration Directory + #[allow(dead_code)] + pub fn write_default(&self) -> Result<(), Error> { + if let Some(config_directory) = dirs::config_dir() { + let config_file_path: PathBuf = config_directory.join(format!("{}/{}", DIRECTORY, FILE_NAME)); + if let Some(parent_directory) = config_file_path.parent() { + if !parent_directory.exists() { + fs::create_dir_all(parent_directory).expect("failed to create binlex configuration directory"); + } + } + if !config_file_path.exists() { + return self.write_to_file(config_file_path.to_str().unwrap()); + } + } + return Err(Error::new(ErrorKind::Other, format!("default configuration already exists"))); + } + + /// Reads the default TOML Configuration File + #[allow(dead_code)] + pub fn from_default(&mut self) -> Result<(), Error> { + if let Some(config_directory) = dirs::config_dir() { + let config_file_path: PathBuf = config_directory.join(format!("{}/{}", DIRECTORY, FILE_NAME)); + if config_file_path.exists() { + match Config::from_file(config_file_path.to_str().unwrap()) { + Ok(config) => return { + *self = config; + Ok(()) + }, + Err(error) => return Err(error), + } + } + } + return Err(Error::new(ErrorKind::Other, format!("unable to read binlex default configuration file"))); + } + +} diff --git a/src/global/format.rs b/src/global/format.rs new file mode 100644 index 00000000..276cb4c5 --- /dev/null +++ b/src/global/format.rs @@ -0,0 +1,85 @@ + +use std::io::Error; +use std::str::FromStr; +use std::fmt; +use std::fs::File; +use std::io::{Read, Seek, SeekFrom}; + +#[repr(u16)] +#[derive(Copy, Clone, PartialEq, Debug)] +pub enum Format { + /// Raw File + CODE = 0x00, + /// Portable Executable + PE = 0x01, + /// ELF Executable + ELF = 0x02, + /// MachO Executable + MACHO = 0x03, + /// Unknown formats + UNKNOWN = 0x04, +} + +impl Format { + pub fn from_file(path: String) -> Result { + let mut file = File::open(path)?; + + let mut buffer = [0u8; 2]; + file.seek(SeekFrom::Start(0x00))?; + file.read_exact(&mut buffer)?; + if buffer == [0x4d, 0x5a] { + file.seek(SeekFrom::Start(0x3c))?; + let mut pe_offset = [0u8; 4]; + file.read_exact(&mut pe_offset)?; + let pe_offset = u32::from_le_bytes(pe_offset); + file.seek(SeekFrom::Start(pe_offset as u64))?; + let mut pe_signature = [0u8; 4]; + file.read_exact(&mut pe_signature)?; + if pe_signature == [0x50, 0x45, 0x00, 0x00] { + return Ok(Format::PE); + } + } + let mut buffer = [0u8; 3]; + file.seek(SeekFrom::Start(0x01))?; + file.read_exact(&mut buffer)?; + if buffer == [0x45, 0x4c, 0x46] { + return Ok(Format::ELF); + } + + let mut buffer = [0u8; 4]; + file.seek(SeekFrom::Start(0x00))?; + file.read_exact(&mut buffer)?; + if buffer == [0xCE, 0xFA, 0xED, 0xFE] || buffer == [0xCF, 0xFA, 0xED, 0xFE] || buffer == [0xBE, 0xBA, 0xFE, 0xCA] { + return Ok(Format::MACHO); + } + + return Ok(Format::UNKNOWN); + } +} + +impl fmt::Display for Format { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let format: &str = match self { + Format::CODE => "code", + Format::PE => "pe", + Format::ELF => "elf", + Format::MACHO => "macho", + Format::UNKNOWN => "unknown", + }; + write!(f, "{}", format) + } +} + +impl FromStr for Format { + type Err = String; + fn from_str(s: &str) -> Result { + match s { + "code" => Ok(Format::CODE), + "pe" => Ok(Format::PE), + "elf" => Ok(Format::ELF), + "macho" => Ok(Format::MACHO), + "unknown" => Ok(Format::UNKNOWN), + _ => Err(format!("invalid format")), + } + } +} diff --git a/src/global/mod.rs b/src/global/mod.rs new file mode 100644 index 00000000..be86b13e --- /dev/null +++ b/src/global/mod.rs @@ -0,0 +1,9 @@ +pub mod format; +pub mod architecture; +pub mod config; + +pub use architecture::Architecture; +pub use format::Format; +pub use config::Config; +pub use config::AUTHOR; +pub use config::VERSION; diff --git a/src/hashing/minhash.rs b/src/hashing/minhash.rs new file mode 100644 index 00000000..431e25c7 --- /dev/null +++ b/src/hashing/minhash.rs @@ -0,0 +1,126 @@ +use rand::{Rng, SeedableRng}; +use rand::rngs::SmallRng; +use twox_hash::XxHash32; +use std::hash::{Hash, Hasher}; + +const PRIME_MODULUS: u32 = 4294967291; + +/// A MinHash implementation using 32-bit hashes for approximate set similarity calculations. +/// +/// This struct provides methods to compute MinHash signatures for a given set of shingles (substrings of fixed size) +/// from a byte slice and to calculate the Jaccard similarity between two MinHash signatures. +pub struct MinHash32 <'minhash32> { + /// Coefficients for the linear hash functions used in MinHash. + a_coefficients: Vec, + /// Intercept coefficients for the linear hash functions used in MinHash. + b_coefficients: Vec, + /// The number of hash functions to use for MinHash. + num_hashes: usize, + /// The size of shingles (substrings) used to compute MinHash. + shingle_size: usize, + /// The byte slice to be hashed. + bytes: &'minhash32 [u8], +} + +impl <'minhash32> MinHash32 <'minhash32> { + /// Creates a new `MinHash32` instance with the provided parameters. + /// + /// # Arguments + /// + /// * `bytes` - A reference to the byte slice to be hashed. + /// * `num_hashes` - The number of hash functions to use for MinHash. + /// * `shingle_size` - The size of shingles (substrings) used to compute MinHash. + /// * `seed` - A seed for the random number generator to ensure deterministic coefficients. + /// + /// # Returns + /// + /// Returns a `MinHash32` instance initialized with the provided parameters and + /// randomly generated coefficients for the hash functions. + pub fn new(bytes: &'minhash32 [u8], num_hashes: usize, shingle_size: usize, seed: u64) -> Self { + let mut rng = SmallRng::seed_from_u64(seed); + let max_hash: u32 = u32::MAX; + let mut a_coefficients = Vec::with_capacity(num_hashes); + let mut b_coefficients = Vec::with_capacity(num_hashes); + + for _ in 0..num_hashes { + a_coefficients.push(rng.gen_range(1..max_hash)); + b_coefficients.push(rng.gen_range(0..max_hash)); + } + + Self { + a_coefficients: a_coefficients, + b_coefficients: b_coefficients, + num_hashes: num_hashes, + shingle_size: shingle_size, + bytes: bytes, + } + } + + /// Computes the MinHash signature for the byte slice. + /// + /// The signature is computed by applying multiple hash functions to each shingle + /// and taking the minimum hash value for each function across all shingles. + /// + /// # Returns + /// + /// Returns `Some(Vec)` containing the MinHash signature if the byte slice is large enough + /// to generate shingles of the specified size. Returns `None` otherwise. + pub fn hash(&self) -> Option> { + if self.bytes.len() < self.shingle_size { return None; } + let mut min_hashes = vec![u32::MAX; self.num_hashes]; + for shingle in self.bytes.windows(self.shingle_size) { + let mut hasher = XxHash32::default(); + shingle.hash(&mut hasher); + let shingle_hash = hasher.finish() as u32; + for i in 0..self.num_hashes { + let a = self.a_coefficients[i]; + let b = self.b_coefficients[i]; + let hash_value = (a.wrapping_mul(shingle_hash).wrapping_add(b)) % PRIME_MODULUS; + if hash_value < min_hashes[i] { + min_hashes[i] = hash_value; + } + } + } + Some(min_hashes) + } + + /// Computes the Jaccard similarity between two MinHash signatures. + /// + /// The similarity is calculated as the ratio of the number of matching hash values + /// to the total number of hash values. + /// + /// # Arguments + /// + /// * `hash1` - The first MinHash signature. + /// * `hash2` - The second MinHash signature. + /// + /// # Returns + /// + /// Returns a `f64` value representing the Jaccard similarity between the two signatures. + /// If the signatures have different lengths, it returns `0.0`. + #[allow(dead_code)] + pub fn jaccard_similarity(hash1: &[u32], hash2: &[u32]) -> f64 { + if hash1.len() != hash2.len() { return 0.0; } + let mut intersection = 0; + for i in 0..hash1.len() { + if hash1[i] == hash2[i] { + intersection += 1; + } + } + intersection as f64 / hash1.len() as f64 + } + + /// Computes the MinHash signature and returns it as a hexadecimal string. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the hexadecimal representation of the MinHash signature + /// if the byte slice is large enough to generate shingles. Returns `None` otherwise. + pub fn hexdigest(&self) -> Option { + self.hash().map(|minhash| { + minhash.iter() + .map(|hash| format!("{:08x}", hash)) + .collect() + }) + } +} diff --git a/src/hashing/mod.rs b/src/hashing/mod.rs new file mode 100644 index 00000000..e45795e6 --- /dev/null +++ b/src/hashing/mod.rs @@ -0,0 +1,7 @@ +pub mod minhash; +pub mod sha256; +pub mod tlsh; + +pub use minhash::MinHash32; +pub use sha256::SHA256; +pub use tlsh::TLSH; diff --git a/src/hashing/sha256.rs b/src/hashing/sha256.rs new file mode 100644 index 00000000..4eaf4662 --- /dev/null +++ b/src/hashing/sha256.rs @@ -0,0 +1,41 @@ +use ring::digest; +use crate::binary::Binary; + +/// Represents a wrapper for computing SHA-256 hashes. +/// +/// This struct provides functionality for hashing a byte slice using the SHA-256 +/// cryptographic hash algorithm and returning the hash as a hexadecimal string. +pub struct SHA256 <'sha256> { + pub bytes: &'sha256 [u8], +} + +impl <'sha256> SHA256 <'sha256> { + /// Creates a new `SHA256` instance with the provided byte slice. + /// + /// # Arguments + /// + /// * `bytes` - A reference to the byte slice to be hashed. + /// + /// # Returns + /// + /// Returns a `SHA256` instance initialized with the provided byte slice. + #[allow(dead_code)] + pub fn new(bytes: &'sha256 [u8]) -> Self { + Self { + bytes: bytes + } + } + + /// Computes the SHA-256 hash of the byte slice and returns it as a hexadecimal string. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the hexadecimal representation of the SHA-256 hash. + /// If the operation fails, it returns `None`. This implementation is currently + /// designed to always succeed, as `ring::digest` does not fail under normal conditions. + #[allow(dead_code)] + pub fn hexdigest(&self) -> Option { + let digest = digest::digest(&digest::SHA256, &self.bytes); + return Some(Binary::to_hex(digest.as_ref())); + } +} diff --git a/src/hashing/tlsh.rs b/src/hashing/tlsh.rs new file mode 100644 index 00000000..b14e731f --- /dev/null +++ b/src/hashing/tlsh.rs @@ -0,0 +1,58 @@ +use tlsh; +use std::io::Error; +use std::io::ErrorKind; + +/// Represents a wrapper around the TLSH (Trend Micro Locality Sensitive Hash) functionality. +/// +/// This struct provides functionality for creating TLSH hashes from a slice of bytes with a minimum +/// byte size requirement, which ensures only sufficiently large data is hashed. +pub struct TLSH <'tlsh> { + /// The slice of bytes to be hashed. + pub bytes: &'tlsh [u8], + /// The minimum required byte size for hashing. + pub mininum_byte_size: usize, +} + +impl <'tlsh> TLSH <'tlsh> { + /// Creates a new `TLSH` instance with the provided bytes and minimum byte size. + /// + /// # Arguments + /// + /// * `bytes` - A reference to the byte slice that will be hashed. + /// * `mininum_byte_size` - The minimum size of `bytes` required for hashing. + /// + /// # Returns + /// + /// Returns a `TLSH` instance initialized with the provided byte slice and minimum byte size. + #[allow(dead_code)] + pub fn new(bytes: &'tlsh [u8], mininum_byte_size: usize) -> Self { + Self { + bytes: bytes, + mininum_byte_size: mininum_byte_size, + } + } + + + /// Compares two hexdigests and get the simialrity score between 0 and 1 where 0 is not similar and 1 is the same. + /// + /// # Returns + /// + /// Returns a `Result` where the `u32` is the similarity score and `Error` if compare fails. + pub fn compare(lhs: String, rhs: String) -> Result { + tlsh::compare(&lhs, &rhs) + .map_err(|e| Error::new(ErrorKind::Other, e)) + } + + /// Computes the TLSH hash of the byte slice if it meets the minimum size requirement. + /// + /// # Returns + /// + /// Returns `Some(String)` containing the hexadecimal digest of the TLSH hash if the byte slice + /// length is greater than or equal to `mininum_byte_size`. Returns `None` otherwise. + #[allow(dead_code)] + pub fn hexdigest(&self) -> Option { + if self.bytes.len() < self.mininum_byte_size { return None; } + tlsh::hash_buf(&self.bytes).ok().map(|h| h.to_string()) + } + +} diff --git a/src/io/json.rs b/src/io/json.rs new file mode 100644 index 00000000..a4d49e1f --- /dev/null +++ b/src/io/json.rs @@ -0,0 +1,208 @@ +use std::io::{self, Read, BufRead, BufReader, IsTerminal, Write}; +use std::fs::File; +use serde_json::{Value, Deserializer}; +use std::fmt; + +#[derive(Debug)] +pub enum JSONError { + FileOpenError(String), + StdinReadError, + JSONParseError(String), + JSONToStringError(String), + FileWriteError(String), +} + +impl fmt::Display for JSONError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + JSONError::FileOpenError(path) => write!(f, "failed to open file: {}", path), + JSONError::StdinReadError => write!(f, "failed to read from standard input"), + JSONError::JSONParseError(err) => write!(f, "failed parsing json: {}", err), + JSONError::JSONToStringError(err) => write!(f, "error converting json value to string: {}", err), + JSONError::FileWriteError(path) => write!(f, "failed to write to file: {}", path), + } + } +} + +pub struct JSON { + values: Vec, +} + +impl JSON { + /// Constructs a `JSON` instance from a file path. + #[allow(dead_code)] + pub fn from_file(path: &str) -> Result { + let file = File::open(path).map_err(|_| JSONError::FileOpenError(path.to_string()))?; + let reader = BufReader::new(file); + Self::deserialize(reader) + } + + /// Constructs a `JSON` instance from standard input. + #[allow(dead_code)] + pub fn from_stdin() -> Result { + if io::stdin().is_terminal() { + return Err(JSONError::StdinReadError); + } + + let reader = BufReader::new(io::stdin()); + Self::deserialize(reader) + } + + /// Constructs a `JSON` instance from a file path or standard input. + /// If the file path is `None`, reads from standard input. + #[allow(dead_code)] + pub fn from_file_or_stdin(path: Option) -> Result { + match path { + Some(file_path) => Self::from_file(&file_path), + None => Self::from_stdin(), + } + } + + /// Private method to deserialize JSON from a given reader. + #[allow(dead_code)] + fn deserialize(reader: R) -> Result { + let values: Vec = Deserializer::from_reader(reader) + .into_iter::() + .map(|value| value.map_err(|e| JSONError::JSONParseError(e.to_string()))) + .collect::>()?; + + Ok(JSON { values }) + } + + /// Private method to deserialize JSON with filtering and in-place modification. + #[allow(dead_code)] + fn deserialize_with_filter(reader: R, filter: F) -> Result + where + R: BufRead, + F: Fn(&mut Value) -> bool, + { + let mut values = Vec::new(); + + for item in Deserializer::from_reader(reader).into_iter::() { + match item { + Ok(mut value) => { + if filter(&mut value) { + values.push(value); + } + } + Err(e) => return Err(JSONError::JSONParseError(e.to_string())), + } + } + + Ok(JSON { values }) + } + + /// Constructs a `JSON` instance from a file path with filtering and in-place modification. + #[allow(dead_code)] + pub fn from_file_with_filter(path: &str, filter: F) -> Result + where + F: Fn(&mut Value) -> bool, + { + let file = File::open(path).map_err(|_| JSONError::FileOpenError(path.to_string()))?; + let reader = BufReader::new(file); + Self::deserialize_with_filter(reader, filter) + } + + /// Constructs a `JSON` instance from standard input with filtering and in-place modification. + pub fn from_stdin_with_filter(filter: F) -> Result + where + F: Fn(&mut Value) -> bool, + { + if io::stdin().is_terminal() { + return Err(JSONError::StdinReadError); + } + + let reader = BufReader::new(io::stdin()); + Self::deserialize_with_filter(reader, filter) + } + + /// Constructs a `JSON` instance from a file path or standard input with filtering and in-place modification. + #[allow(dead_code)] + pub fn from_file_or_stdin_with_filter(path: Option, filter: F) -> Result + where + F: Fn(&mut Value) -> bool, + { + match path { + Some(file_path) => Self::from_file_with_filter(&file_path, filter), + None => Self::from_stdin_with_filter(filter), + } + } + + #[allow(dead_code)] + pub fn from_file_or_stdin_as_array(path: Option, filter: F) -> Result + where + F: Fn(&Value) -> bool, + { + // Read the JSON input from file or stdin + let input = match path { + Some(ref file_path) => { // Use `ref` to avoid moving `file_path` + let mut file = File::open(file_path).map_err(|_| JSONError::FileOpenError(file_path.clone()))?; + let mut buffer = String::new(); + file.read_to_string(&mut buffer).map_err(|_| JSONError::FileOpenError(file_path.clone()))?; + buffer + } + None => { + if io::stdin().is_terminal() { + return Err(JSONError::StdinReadError); + } + let mut buffer = String::new(); + io::stdin() + .read_to_string(&mut buffer) + .map_err(|_| JSONError::StdinReadError)?; + buffer + } + }; + + // Parse the input as JSON + let parsed_json: Value = serde_json::from_str(&input).map_err(|e| JSONError::JSONParseError(e.to_string()))?; + + // Ensure the input is an array + let array = parsed_json + .as_array() + .ok_or_else(|| JSONError::JSONParseError("Input JSON is not an array".to_string()))?; + + // Filter and collect the array elements + let values = array + .iter() + .filter(|value| filter(value)) + .cloned() + .collect(); + + Ok(JSON { values }) + } + + /// Returns a reference to the parsed JSON values. + #[allow(dead_code)] + pub fn values(&self) -> &Vec { + &self.values + } + + /// Converts a `serde_json::Value` to a `String`. + #[allow(dead_code)] + pub fn value_to_string(value: &Value) -> Result { + serde_json::to_string(value).map_err(|e| JSONError::JSONToStringError(e.to_string())) + } + + /// Converts all `serde_json::Value`s into a `Vec`. + #[allow(dead_code)] + pub fn values_as_strings(&self) -> Vec { + self.values + .iter() + .filter_map(|value| Self::value_to_string(value).ok()) + .collect() + } + + /// Writes all JSON values as single-line strings to a file. + #[allow(dead_code)] + pub fn write_to_file(&self, file_path: &str) -> Result<(), JSONError> { + let strings = self.values_as_strings(); + + let mut file = File::create(file_path).map_err(|_| JSONError::FileWriteError(file_path.to_string()))?; + + for line in strings { + writeln!(file, "{}", line).map_err(|_| JSONError::FileWriteError(file_path.to_string()))?; + } + + Ok(()) + } +} diff --git a/src/io/mod.rs b/src/io/mod.rs new file mode 100644 index 00000000..385cae0d --- /dev/null +++ b/src/io/mod.rs @@ -0,0 +1,9 @@ +pub mod stdin; +pub mod stdout; +pub mod stderr; +pub mod json; + +pub use stdin::Stdin; +pub use stdout::Stdout; +pub use stderr::Stderr; +pub use json::JSON; diff --git a/src/io/stderr.rs b/src/io/stderr.rs new file mode 100644 index 00000000..8e5b86aa --- /dev/null +++ b/src/io/stderr.rs @@ -0,0 +1,37 @@ +use std::io::ErrorKind; +use std::io::{self, Write}; +use std::fmt::Display; +use crate::Config; + +/// Represents a wrapper for standard error operations. +pub struct Stderr; + +impl Stderr { + /// Prints a line to standard error. + /// + /// # Arguments + /// + /// * `line` - The line to be printed, which implements the `Display` trait. + /// + /// If an error occurs, this method checks if it was due to a broken pipe. + /// If it was, the program exits with code `0`. For other errors, it logs + /// an error message to standard error and exits with code `1`. + #[allow(dead_code)] + pub fn print(line: T) { + writeln!(io::stderr(), "{}", line).unwrap_or_else(|e| { + if e.kind() == ErrorKind::BrokenPipe { + std::process::exit(0); + } else { + eprintln!("error writing to stdout: {}", e); + std::process::exit(1); + } + }); + } + + /// Prints a line to standard error if debug configuration is set. + pub fn print_debug(config: Config, line: T) { + if config.general.debug { + Stderr::print(line); + } + } +} diff --git a/src/io/stdin.rs b/src/io/stdin.rs new file mode 100644 index 00000000..76fd438a --- /dev/null +++ b/src/io/stdin.rs @@ -0,0 +1,62 @@ +use std::io::{stdin, ErrorKind}; +use std::io::{self, BufRead, IsTerminal, Write}; +use std::fmt::Display; +use std::process; +use crate::io::Stdout; + +/// Represents a wrapper for standard input operations. +pub struct Stdin; + +impl Stdin { + + #[allow(dead_code)] + pub fn is_terminal() -> bool { + stdin().is_terminal() + } + + /// Reads lines from standard input and writes each line to standard output. + /// + /// This function reads lines from standard input if it's not a terminal, + /// locking the input for safe handling in buffered mode. If a line is read + /// successfully, it's printed using `Stdout`. If an error occurs, it prints + /// the error message and exits with a non-zero status code. + #[allow(dead_code)] + pub fn passthrough() { + let stdin = io::stdin(); + if !stdin.is_terminal() { + let handle = stdin.lock(); + for line in handle.lines() { + match line { + Ok(line) => { + Stdout::print(line); + }, + Err(error) => { + eprintln!("{}", error); + process::exit(1); + }, + } + } + } + } + /// Prints a line to standard error. + /// + /// # Arguments + /// + /// * `line` - The line to be printed, which implements the `Display` trait. + /// + /// If an error occurs, this method checks if it was due to a broken pipe. + /// If it was, the program exits with code `0`. For other errors, it logs + /// an error message to standard error and exits with code `1`. + #[allow(dead_code)] + pub fn print(line: T) { + writeln!(io::stderr(), "{}", line).unwrap_or_else(|e| { + if e.kind() == ErrorKind::BrokenPipe { + std::process::exit(0); + } else { + eprintln!("error writing to stdout: {}", e); + std::process::exit(1); + } + }); + } + +} diff --git a/src/io/stdout.rs b/src/io/stdout.rs new file mode 100644 index 00000000..8b1aaddd --- /dev/null +++ b/src/io/stdout.rs @@ -0,0 +1,28 @@ +use std::io::ErrorKind; +use std::io::{self, Write}; +use std::fmt::Display; +/// Represents a wrapper for standard output operations. +pub struct Stdout; + +impl Stdout { + /// Prints a line to standard output. + /// + /// # Arguments + /// + /// * `line` - The line to be printed, which implements the `Display` trait. + /// + /// If an error occurs, this method checks if it was due to a broken pipe. + /// If it was, the program exits with code `0`. For other errors, it logs + /// an error message to standard error and exits with code `1`. + #[allow(dead_code)] + pub fn print(line: T) { + writeln!(io::stdout(), "{}", line).unwrap_or_else(|e| { + if e.kind() == ErrorKind::BrokenPipe { + std::process::exit(0); + } else { + eprintln!("error writing to stdout: {}", e); + std::process::exit(1); + } + }); + } +} diff --git a/src/jvm.cpp b/src/jvm.cpp deleted file mode 100644 index 870db640..00000000 --- a/src/jvm.cpp +++ /dev/null @@ -1,305 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "common.h" - -#ifndef JVM_H -#define JVM_H - -#define JVM_DECOMPILER_TYPE_BLCKS 0 -#define JVM_DECOMPILER_TYPE_FUNCS 1 -#define JVM_DECOMPILER_TYPE_UNSET 2 - -#define JVM_DECOMPILER_MAX_SECTIONS 256 - -#define JVM_INS_AALOAD 0x32 -#define JVM_INS_AASTORE 0x53 -#define JVM_INS_ACONST_NULL 0x01 -#define JVM_INS_ALOAD 0x19 -#define JVM_INS_ALOAD_0 0x2a -#define JVM_INS_ALOAD_1 0x2b -#define JVM_INS_ALOAD_2 0x2c -#define JVM_INS_ALOAD_3 0x2d -#define JVM_INS_ANEWARRAY 0xbd -#define JVM_INS_ARETURN 0xb0 -#define JVM_INS_ARRAYLENGTH 0xbe -#define JVM_INS_ASTORE 0x3a -#define JVM_INS_ASTORE_0 0x4b -#define JVM_INS_ASTORE_1 0x4c -#define JVM_INS_ASTORE_2 0x4d -#define JVM_INS_ASTORE_3 0x4e -#define JVM_INS_ATHROW 0xbf -#define JVM_INS_BALOAD 0x33 -#define JVM_INS_BASTORE 0x54 -#define JVM_INS_BIPUSH 0x10 -#define JVM_INS_BREAKPOINT 0xca -#define JVM_INS_CALOAD 0x34 -#define JVM_INS_CASTORE 0x55 -#define JVM_INS_CHECKCAST 0xc0 -#define JVM_INS_D2F 0x90 -#define JVM_INS_D2I 0x8e -#define JVM_INS_D2L 0x8f -#define JVM_INS_DADD 0x63 -#define JVM_INS_DALOAD 0x31 -#define JVM_INS_DASTORE 0x52 -#define JVM_INS_DCMPG 0x98 -#define JVM_INS_DCMPL 0x97 -#define JVM_INS_DCONST_0 0x0e -#define JVM_INS_DCONST_1 0x0f -#define JVM_INS_DDIV 0x6f -#define JVM_INS_DLOAD 0x18 -#define JVM_INS_DLOAD_0 0x26 -#define JVM_INS_DLOAD_1 0x27 -#define JVM_INS_DLOAD_2 0x28 -#define JVM_INS_DLOAD_3 0x29 -#define JVM_INS_DMUL 0x6b -#define JVM_INS_DNEG 0x77 -#define JVM_INS_DREM 0x73 -#define JVM_INS_DRETURN 0xaf -#define JVM_INS_DSTORE 0x39 -#define JVM_INS_DSTORE_0 0x47 -#define JVM_INS_DSTORE_1 0x48 -#define JVM_INS_DSTORE_2 0x49 -#define JVM_INS_DSTORE_3 0x4a -#define JVM_INS_DSUB 0x67 -#define JVM_INS_DUP 0x59 -#define JVM_INS_DUP_X1 0x5a -#define JVM_INS_DUP_X2 0x5e -#define JVM_INS_F2D 0x8d -#define JVM_INS_F2I 0x8b -#define JVM_INS_F2L 0x8c -#define JVM_INS_FADD 0x62 -#define JVM_INS_FALOAD 0x30 -#define JVM_INS_FASTORE 0x51 -#define JVM_INS_FCMPG 0x96 -#define JVM_INS_FCMPL 0x95 -#define JVM_INS_FCONST_0 0x0b -#define JVM_INS_FCONST_1 0x0c -#define JVM_INS_FCONST_2 0x0d -#define JVM_INS_FDIV 0x6e -#define JVM_INS_FLOAD 0x17 -#define JVM_INS_FLOAD_0 0x22 -#define JVM_INS_FLOAD_1 0x23 -#define JVM_INS_FLOAD_2 0x24 -#define JVM_INS_FLOAD_3 0x25 -#define JVM_INS_FMUL 0x6a -#define JVM_INS_FNEG 0x76 -#define JVM_INS_FREM 0x72 -#define JVM_INS_FRETURN 0xae -#define JVM_INS_FSTORE 0x38 -#define JVM_INS_FSTORE_0 0x43 -#define JVM_INS_FSTORE_1 0x44 -#define JVM_INS_FSTORE_2 0x45 -#define JVM_INS_FSTORE_3 0x46 -#define JVM_INS_FSUB 0x66 -#define JVM_INS_GETFIELD 0xb4 -#define JVM_INS_GETSTATIC 0xb2 -#define JVM_INS_GOTO 0xa7 -#define JVM_INS_GOTO_W 0xc8 -#define JVM_INS_I2B 0x91 -#define JVM_INS_I2C 0x92 -#define JVM_INS_I2D 0x87 -#define JVM_INS_I2F 0x86 -#define JVM_INS_I2L 0x85 -#define JVM_INS_I2S 0x93 -#define JVM_INS_IADD 0x60 -#define JVM_INS_IALOAD 0x2e -#define JVM_INS_IAND 0x7e -#define JVM_INS_IASTORE 0x4f -#define JVM_INS_ICONST_M1 0x02 -#define JVM_INS_ICONST_0 0x03 -#define JVM_INS_ICONST_1 0x04 -#define JVM_INS_ICONST_2 0x05 -#define JVM_INS_ICONST_3 0x06 -#define JVM_INS_ICONST_4 0x07 -#define JVM_INS_ICONST_5 0x08 -#define JVM_INS_IDIV 0x6c -#define JVM_INS_IF_ACMPEQ 0xa5 -#define JVM_INS_IF_ACMPNE 0xa6 -#define JVM_INS_IF_ICMPEQ 0x9f -#define JVM_INS_IF_ICMPGE 0xa2 -#define JVM_INS_IF_ICMPGT 0xa3 -#define JVM_INS_IF_ICMPLE 0xa4 -#define JVM_INS_IF_ICMPLT 0xa1 -#define JVM_INS_IF_ICMPNE 0xa0 -#define JVM_INS_IFEQ 0x99 -#define JVM_INS_IFGE 0x9c -#define JVM_INS_IFGT 0x9d -#define JVM_INS_IFLE 0x9e -#define JVM_INS_IFLT 0x9b -#define JVM_INS_IFNE 0x9a -#define JVM_INS_IFNONNULL 0xc7 -#define JVM_INS_IFNULL 0xc6 -#define JVM_INS_IINC 0x84 -#define JVM_INS_ILOAD 0x15 -#define JVM_INS_ILOAD_0 0x1a -#define JVM_INS_ILOAD_1 0x1b -#define JVM_INS_ILOAD_2 0x1c -#define JVM_INS_ILOAD_3 0x1d -#define JVM_INS_IMPDEP1 0xfe -#define JVM_INS_IMPDEP2 0xff -#define JVM_INS_IMUL 0x68 -#define JVM_INS_INEG 0x74 -#define JVM_INS_INSTANCEOF 0xc1 -#define JVM_INS_INVOKEDYNAMIC 0xba -#define JVM_INS_INVOKEINTERFACE 0xb9 -#define JVM_INS_INVOKESPECIAL 0xb7 -#define JVM_INS_INVOKESTATIC 0xb8 -#define JVM_INS_INVOKEVIRTUAL 0xb6 -#define JVM_INS_IOR 0x80 -#define JVM_INS_IREM 0x70 -#define JVM_INS_IRETURN 0xac -#define JVM_INS_ISHL 0x78 -#define JVM_INS_ISHR 0x7a -#define JVM_INS_ISTORE 0x36 -#define JVM_INS_ISTORE_0 0x3b -#define JVM_INS_ISTORE_1 0x3c -#define JVM_INS_ISTORE_2 0x3d -#define JVM_INS_ISTORE_3 0x3e -#define JVM_INS_ISUB 0x64 -#define JVM_INS_IUSHR 0x7c -#define JVM_INS_IXOR 0x82 -#define JVM_INS_JSR 0xa8 -#define JVM_INS_JSR_W 0xc9 -#define JVM_INS_L2D 0x8a -#define JVM_INS_L2F 0x89 -#define JVM_INS_L2I 0x88 -#define JVM_INS_LADD 0x61 -#define JVM_INS_LALOAD 0x2f -#define JVM_INS_LAND 0x7f -#define JVM_INS_LASTORE 0x50 -#define JVM_INS_LCMP 0x94 -#define JVM_INS_LCONST_0 0x09 -#define JVM_INS_LCONST_1 0x0a -#define JVM_INS_LDC 0x12 -#define JVM_INS_LDC_W 0x13 -#define JVM_INS_LDC2_W 0x14 -#define JVM_INS_LDIV 0x6d -#define JVM_INS_LLOAD 0x16 -#define JVM_INS_LLOAD_0 0x1e -#define JVM_INS_LLOAD_1 0x1f -#define JVM_INS_LLOAD_2 0x20 -#define JVM_INS_LLOAD_3 0x21 -#define JVM_INS_LMUL 0x69 -#define JVM_INS_LNEG 0x75 -#define JVM_INS_LOOKUPSWITCH 0xab -#define JVM_INS_LOR 0x81 -#define JVM_INS_LREM 0x71 -#define JVM_INS_LRETURN 0xad -#define JVM_INS_LSHL 0x79 -#define JVM_INS_LSHR 0x7b -#define JVM_INS_LSTORE 0x37 -#define JVM_INS_LSTORE_0 0x3f -#define JVM_INS_LSTORE_1 0x40 -#define JVM_INS_LSTORE_2 0x41 -#define JVM_INS_LSTORE_3 0x42 -#define JVM_INS_LSUB 0x65 -#define JVM_INS_LUSHR 0x7d -#define JVM_INS_LXOR 0x83 -#define JVM_INS_MONITORENTER 0xc2 -#define JVM_INS_MONITOREXIT 0xc3 -#define JVM_INS_MULTIANEWARRAY 0xc5 -#define JVM_INS_NEW 0xbb -#define JVM_INS_NEWARRAY 0xbc -#define JVM_INS_NOP 0x00 -#define JVM_INS_POP 0x57 -#define JVM_INS_POP2 0x58 -#define JVM_INS_PUTFIELD 0xb5 -#define JVM_INS_PUTSTATIC 0xb3 -#define JVM_INS_RET 0xa9 -#define JVM_INS_RETURN 0xb1 -#define JVM_INS_SALOAD 0x35 -#define JVM_INS_SASTORE 0x56 -#define JVM_INS_SIPUSH 0x11 -#define JVM_INS_SWAP 0x5f -#define JVM_INS_TABLESWITCH 0xaa -#define JVM_INS_WIDE 0xc4 -#define JVM_INS_NONAMESTART 0xcb -#define JVM_INS_NONAMEEND 0xfd - -class JVMDecompiler { - private: - struct Section { - char *function_traits; - char *block_traits; - }; - int type = JVM_DECOMPILER_TYPE_UNSET; - char * hexdump_traits(char *buffer0, const void *data, int size, int operand_size){ - const unsigned char *pc = (const unsigned char *)data; - for (int i = 0; i < size; i++){ - if (i >= size - (operand_size/8)){ - sprintf(buffer0, "%s?? ", buffer0); - } else { - sprintf(buffer0, "%s%02x ", buffer0, pc[i]); - } - } - return buffer0; - } - char * traits_nl(char *traits){ - sprintf(traits, "%s\n", traits); - return traits; - } - public: - struct Section sections[JVM_DECOMPILER_MAX_SECTIONS]; - JVMDecompiler(){ - for (int i = 0; i < JVM_DECOMPILER_MAX_SECTIONS; i++){ - sections[i].function_traits = NULL; - sections[i].block_traits = NULL; - } - } - bool Setup(int input_type){ - switch(input_type){ - case JVM_DECOMPILER_TYPE_BLCKS: - type = JVM_DECOMPILER_TYPE_BLCKS; - break; - case JVM_DECOMPILER_TYPE_FUNCS: - type = JVM_DECOMPILER_TYPE_FUNCS; - break; - default: - fprintf(stderr, "[x] unsupported JVM decompiler type\n"); - type = JVM_DECOMPILER_TYPE_UNSET; - return false; - } - return true; - } - void WriteTraits(char *file_path){ - FILE *fd = fopen(file_path, "w"); - for (int i = 0; i < JVM_DECOMPILER_MAX_SECTIONS; i++){ - if (sections[i].function_traits != NULL){ - fwrite(sections[i].function_traits, sizeof(char), strlen(sections[i].function_traits), fd); - } - if (sections[i].block_traits != NULL){ - fwrite(sections[i].block_traits, sizeof(char), strlen(sections[i].block_traits), fd); - } - } - fclose(fd); - } - void PrintTraits(){ - for (int i = 0; i < JVM_DECOMPILER_MAX_SECTIONS; i++){ - if (sections[i].function_traits != NULL){ - printf("%s", sections[i].function_traits); - } - if (sections[i].block_traits != NULL){ - printf("%s", sections[i].block_traits); - } - } - } - ~JVMDecompiler(){ - for (int i = 0; i < JVM_DECOMPILER_MAX_SECTIONS; i++){ - if (sections[i].function_traits != NULL){ - free(sections[i].function_traits); - } - if (sections[i].block_traits != NULL){ - free(sections[i].block_traits); - } - } - } -}; - -#endif diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..8b8109af --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,15 @@ +pub mod formats; +pub mod types; +pub mod hashing; +pub mod controlflow; +pub mod disassemblers; +pub mod binary; +pub mod global; +pub mod io; + +pub use global::Config; +pub use binary::Binary; +pub use global::Architecture; +pub use global::Format; +pub use global::AUTHOR; +pub use global::VERSION; diff --git a/src/pe-dotnet.cpp b/src/pe-dotnet.cpp deleted file mode 100644 index 16618a1e..00000000 --- a/src/pe-dotnet.cpp +++ /dev/null @@ -1,300 +0,0 @@ -#include "pe-dotnet.h" - -using namespace binlex; -using namespace std; -using namespace dotnet; - -TableEntry* TableEntry::TableEntryFactory(uint8_t entry_type) { - switch(entry_type){ - case MODULE: - return new ModuleEntry(); - case TYPE_REF: - return new TypeRefEntry(); - case TYPE_DEF: - return new TypeDefEntry(); - case FIELD_PTR: - return new FieldPtrEntry(); - case FIELD: - return new FieldEntry(); - case METHOD_PTR: - return new MethodPtrEntry(); - case METHOD_DEF: - return new MethodDefEntry(); - } - return NULL; -}; - -uint32_t Cor20MetadataTable::ParseTablePointers(char *&buffer) { - uint32_t *buffer_aux; - uint32_t read_bytes; - buffer_aux = (uint32_t *)buffer; - uint8_t j = 0; - read_bytes = 0; - for (uint16_t i = 0; i < 8 * 8; i++) { - if ( (mask_valid >> i) & 1 ) { - table_entries[i] = buffer_aux[j++]; - read_bytes += 4; - } - } - return read_bytes; -} - -uint32_t Cor20MetadataTable::ParseTables(char *&buffer) { - char *buff_aux; - TableEntry* entry; - buff_aux = buffer; - for (size_t i = 0; i < 8 * 8; i++) { - if (table_entries[i] == 0) continue; - for (size_t j = 0; j < table_entries[i]; j++) { - entry = TableEntry().TableEntryFactory(i); - if (entry == NULL) continue; - TableEntry::ParseArgs args; - args.buff = buff_aux; - args.heap_sizes = heap_sizes; - args.table_entries = table_entries; - buff_aux += entry->Parse(&args); - tables[i].push_back(entry); - } - // We don't need to parse the rest of the .NET at this moment to get the IL code - // so simply stop here parsing process - if ( i > METHOD_DEF ) break; - } - return (uint32_t)(buff_aux - buffer); -} - -Cor20MetadataTable::~Cor20MetadataTable() { - for (size_t i = 0; i < 8 * 8; i++){ - if (table_entries[i] == 0) continue; - for (size_t j = 0; j < tables[i].size(); j++){ - if (tables[i][j] != NULL){ - delete tables[i][j]; - } - } - } -} - -bool DOTNET::Parse(){ - if (ParseCor20Header() == false){ - return false; - } - if (ParseCor20StorageSignature() == false){ - return false; - } - if (ParseCor20StorageHeader() == false){ - return false; - } - if (ParseCor20StreamsHeader() == false){ - return false; - } - if (ParseCor20MetadataStream() == false){ - return false; - } - return true; -} - -bool DOTNET::ParseCor20Header(){ - DataDirectory clr_data_directory; - vector raw_data; - - if (binary->has(DATA_DIRECTORY::CLR_RUNTIME_HEADER) == false) return false; - - clr_data_directory = binary->data_directory(DATA_DIRECTORY::CLR_RUNTIME_HEADER); - raw_data = binary->get_content_from_virtual_address(clr_data_directory.RVA(), - clr_data_directory.size()); - memcpy((void *) &cor20_header, raw_data.data(), sizeof(cor20_header)); - return true; -} - -bool DOTNET::ParseCor20StorageSignature(){ - vector raw_data; - unsigned char* storage_signature; - - raw_data = binary->get_content_from_virtual_address(cor20_header.metadata_rva, - cor20_header.metadata_size); - storage_signature = raw_data.data(); - cor20_storage_signature.signature = *(uint32_t *)(storage_signature + 0); - cor20_storage_signature.major_version = *(uint16_t *)(storage_signature + 4); - cor20_storage_signature.minor_version = *(uint16_t *)(storage_signature + 6); - cor20_storage_signature.extra_data = *(uint32_t *)(storage_signature + 8); - cor20_storage_signature.version_string_size = *(uint32_t *)(storage_signature + 12); - cor20_storage_signature.version_string = (unsigned char *)malloc( - cor20_storage_signature.version_string_size); - - if (cor20_storage_signature.version_string == NULL) return false; - - memcpy(cor20_storage_signature.version_string, - (char*)(storage_signature + 16), - cor20_storage_signature.version_string_size); - return true; -} - -bool DOTNET::ParseCor20StorageHeader(){ - vector raw_data; - unsigned char *storage_signature, *storage_header; - uint32_t size_of_storage_signature; - - raw_data = binary->get_content_from_virtual_address(cor20_header.metadata_rva, cor20_header.metadata_size); - storage_signature = raw_data.data(); - - size_of_storage_signature = sizeof(cor20_storage_signature) - \ - sizeof(cor20_storage_signature.version_string) + \ - cor20_storage_signature.version_string_size; - - storage_header = storage_signature + size_of_storage_signature; - - cor20_storage_header.flags = *(uint8_t *)(storage_header + 0); - cor20_storage_header.pad = *(uint8_t *)(storage_header + 1); - cor20_storage_header.number_of_streams = *(uint16_t *)(storage_header + 2); - return true; -} - -bool DOTNET::ParseCor20StreamsHeader(){ - vector raw_data; - unsigned char *storage_signature, *storage_header, *streams_header, *streams_header_aux; - uint32_t size_of_storage_signature; - - raw_data = binary->get_content_from_virtual_address(cor20_header.metadata_rva, cor20_header.metadata_size); - storage_signature = raw_data.data(); - - size_of_storage_signature = sizeof(cor20_storage_signature) - \ - sizeof(cor20_storage_signature.version_string) + \ - cor20_storage_signature.version_string_size; - - storage_header = storage_signature + size_of_storage_signature; - - streams_header = storage_header + sizeof(cor20_storage_header); - streams_header_aux = streams_header; - StreamsHeader = (dotnet::COR20_STREAM_HEADER **)calloc(sizeof(dotnet::COR20_STREAM_HEADER *), cor20_storage_header.number_of_streams); - for (uint16_t i = 0; - i < cor20_storage_header.number_of_streams; - i++) - { - uint32_t offset = *(uint32_t *)(streams_header_aux); streams_header_aux += 4; - uint32_t size = *(uint32_t *)(streams_header_aux); streams_header_aux += 4; - char *name = (char *)streams_header_aux; - - StreamsHeader[i] = (dotnet::COR20_STREAM_HEADER *)calloc(sizeof(dotnet::COR20_STREAM_HEADER), 1); - StreamsHeader[i]->offset = offset; - StreamsHeader[i]->size = size; - StreamsHeader[i]->name = (char *)calloc(sizeof(char), strlen(name) + 1); - memcpy(StreamsHeader[i]->name, name, strlen(name)); - size_t name_size = strlen(name); - size_t boundary = 4 - (name_size % 4); - streams_header_aux += name_size + boundary; - } - return true; -} - -bool DOTNET::ParseCor20MetadataStream() -{ - vector raw_data; - unsigned char *storage_signature; - dotnet::COR20_STREAM_HEADER *stream_metadata_header = NULL; - - raw_data = binary->get_content_from_virtual_address(cor20_header.metadata_rva, cor20_header.metadata_size); - storage_signature = raw_data.data(); - - for (size_t i = 0; i < cor20_storage_header.number_of_streams; i++) - { - if (strncmp(StreamsHeader[i]->name, "#~", 2) == 0) - { - stream_metadata_header = StreamsHeader[i]; - } - } - if (stream_metadata_header == NULL) return false; - - memcpy(&cor20_metadata_table.reserved, storage_signature + stream_metadata_header->offset, 24); - char *buff = (char *)(storage_signature + stream_metadata_header->offset + 24); - buff += cor20_metadata_table.ParseTablePointers(buff); - cor20_metadata_table.ParseTables(buff); - - return true; -} - -void DOTNET::ParseSections(){ - size_t num_of_sections; - uint8_t header, code_offset; - vector data; - num_of_sections = ( cor20_metadata_table.tables[METHOD_DEF].size() < BINARY_MAX_SECTIONS) ? cor20_metadata_table.tables[METHOD_DEF].size() : BINARY_MAX_SECTIONS; - for (size_t i = 0; i < num_of_sections; i++) { - MethodDefEntry* mentry = (MethodDefEntry *)cor20_metadata_table.tables[METHOD_DEF][i]; - if (mentry->rva == 0) continue; - Section section = {}; - section.offset = mentry->rva; - - header = binary->get_content_from_virtual_address(mentry->rva, 1).data()[0]; - - // Check header type Tiny or FAT type. - code_offset = 0; - if ( (header & 3) == 2){ - // Tiny header - section.size = header >> 2; - code_offset = 1; - } else { - // FAT header - uint32_t code_size = *(uint32_t *)&binary->get_content_from_virtual_address( - mentry->rva + 4, - 4)[0]; - section.size = code_size; - code_offset = 12; - } - section.data = calloc(section.size, 1); - if (section.data == NULL){ - fprintf(stderr, "[x] cannot allocate memory\n"); - } - data = binary->get_content_from_virtual_address(section.offset + code_offset, section.size); - memcpy(section.data, &data[0], section.size); - _sections.push_back(section); - } -} - -bool DOTNET::ReadVector(const std::vector &data){ - binary = Parser::parse(data); - if (binary == NULL){ - return false; - } - if (binary_arch == BINARY_ARCH_UNKNOWN || - binary_mode == BINARY_MODE_UNKNOWN){ - switch(binary->header().machine()){ - case MACHINE_TYPES::IMAGE_FILE_MACHINE_I386: - binary_arch = BINARY_ARCH_X86; - binary_mode = BINARY_MODE_CIL; - break; - default: - binary_arch = BINARY_ARCH_UNKNOWN; - binary_mode = BINARY_MODE_UNKNOWN; - return false; - } - } - CalculateFileHashes(data); - if (IsDotNet() == false) return false; - if (Parse() == false) return false; - ParseSections(); - return true; -} - -DOTNET::~DOTNET(){ - for (size_t i = 0; i < _sections.size(); i++){ - if (_sections[i].data != NULL){ - free(_sections[i].data); - } - } - _sections.clear(); - - if (cor20_storage_signature.version_string != NULL){ - free(cor20_storage_signature.version_string); - } - - for (size_t i = 0; i < cor20_storage_header.number_of_streams; i++){ - if (StreamsHeader == NULL) break; - if (StreamsHeader[i] != NULL){ - if (StreamsHeader[i]->name != NULL){ - free(StreamsHeader[i]->name); - } - free(StreamsHeader[i]); - } - } - if (StreamsHeader != NULL){ - free(StreamsHeader); - } -} diff --git a/src/pe.cpp b/src/pe.cpp deleted file mode 100644 index 80ec28b4..00000000 --- a/src/pe.cpp +++ /dev/null @@ -1,130 +0,0 @@ -#include "pe.h" - -using namespace binlex; -using namespace LIEF::PE; - -PE::PE(){ - total_exec_sections = 0; - for (int i = 0; i < BINARY_MAX_SECTIONS; i++){ - sections[i].offset = 0; - sections[i].size = 0; - sections[i].data = NULL; - } -} - -bool PE::ReadVector(const std::vector &data){ - if (binary = Parser::parse(data)){ - binary_type = BINARY_TYPE_PE; - if (binary_arch == BINARY_ARCH_UNKNOWN || - binary_mode == BINARY_MODE_UNKNOWN){ - if (IsDotNet() == true){ - binary_arch = BINARY_ARCH_X86; - binary_mode = BINARY_MODE_CIL; - } else { - switch(binary->header().machine()){ - case MACHINE_TYPES::IMAGE_FILE_MACHINE_I386: - SetArchitecture(BINARY_ARCH_X86, BINARY_MODE_32); - g_args.options.mode = "pe:x86"; - break; - case MACHINE_TYPES::IMAGE_FILE_MACHINE_AMD64: - SetArchitecture(BINARY_ARCH_X86, BINARY_MODE_64); - g_args.options.mode = "pe:x86_64"; - break; - default: - binary_arch = BINARY_ARCH_UNKNOWN; - binary_mode = BINARY_MODE_UNKNOWN; - return false; - } - } - } - CalculateFileHashes(data); - return ParseSections(); - } - return false; -} - -bool PE::IsDotNet(){ - try { - auto imports = binary->imports(); - for(Import i : imports) { - if (i.name() == "mscorelib.dll") { - if(binary->data_directory(DATA_DIRECTORY::CLR_RUNTIME_HEADER).RVA() > 0) { - return true; - } - } - if (i.name() == "mscoree.dll") { - if(binary->data_directory(DATA_DIRECTORY::CLR_RUNTIME_HEADER).RVA() > 0) { - return true; - } - } - } - return false; - } catch(LIEF::bad_format const&) { - return false; - } -} - -bool PE::HasLimitations(){ - if(binary->has_imports()){ - auto imports = binary->imports(); - for(Import i : imports){ - if(i.name() == "MSVBVM60.DLL"){ - return true; - } - } - } - return false; -} - -bool PE::ParseSections(){ - uint32_t index = 0; - Binary::it_sections local_sections = binary->sections(); - for (auto it = local_sections.begin(); it != local_sections.end(); it++){ - if (it->characteristics() & (uint32_t)SECTION_CHARACTERISTICS::IMAGE_SCN_MEM_EXECUTE){ - vector data = binary->get_content_from_virtual_address(it->virtual_address(), it->sizeof_raw_data()); - if (data.size() == 0) { - continue; - } - sections[index].offset = it->offset(); - sections[index].size = it->sizeof_raw_data(); - sections[index].data = malloc(sections[index].size); - memset(sections[index].data, 0, sections[index].size); - memcpy(sections[index].data, &data[0], sections[index].size); - // Add exports to the function list - if (binary->has_exports()){ - Export exports = binary->get_export(); - Export::it_entries export_entries = exports.entries(); - for (auto j = export_entries.begin(); j != export_entries.end(); j++){ - PRINT_DEBUG("PE Export offset: 0x%x\n", (int)binary->rva_to_offset(j->address())); - uint64_t tmp_offset = binary->rva_to_offset(j->address()); - if (tmp_offset > sections[index].offset && - tmp_offset < sections[index].offset + sections[index].size){ - sections[index].functions.insert(tmp_offset-sections[index].offset); - } - } - } - // Add entrypoint to the function list - uint64_t entrypoint_offset = binary->va_to_offset(binary->entrypoint()); - PRINT_DEBUG("PE Entrypoint offset: 0x%x\n", (int)entrypoint_offset); - if (entrypoint_offset > sections[index].offset && entrypoint_offset < sections[index].offset + sections[index].size){ - sections[index].functions.insert(entrypoint_offset-sections[index].offset); - } - index++; - if (BINARY_MAX_SECTIONS == index){ - fprintf(stderr, "[x] malformed binary, too many executable sections\n"); - return false; - } - } - } - total_exec_sections = index + 1; - return true; -} - -PE::~PE(){ - for (uint32_t i = 0; i < total_exec_sections; i++){ - sections[i].offset = 0; - sections[i].size = 0; - free(sections[i].data); - sections[i].functions.clear(); - } -} diff --git a/src/raw.cpp b/src/raw.cpp deleted file mode 100644 index 155b2b95..00000000 --- a/src/raw.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include "raw.h" - -using namespace binlex; - -Raw::Raw(){ - total_exec_sections = 0; - for (int i = 0; i < BINARY_MAX_SECTIONS; i++){ - sections[i].offset = 0; - sections[i].size = 0; - sections[i].data = NULL; - } -} - -int Raw::GetFileSize(FILE *fd){ - int start = ftell(fd); - fseek(fd, 0, SEEK_END); - int size = ftell(fd); - fseek(fd, start, SEEK_SET); - return size; -} - -bool Raw::ReadVector(const std::vector &data){ - if (binary_arch == BINARY_ARCH_UNKNOWN || - binary_mode == BINARY_MODE_UNKNOWN){ - return false; - } else { - if (binary_arch == BINARY_ARCH_X86 && - binary_mode == BINARY_MODE_32){ - g_args.options.mode = "raw:x86"; - } else if ((binary_arch == BINARY_ARCH_X86 || - binary_arch == BINARY_ARCH_X86_64) && - binary_mode == BINARY_MODE_64){ - g_args.options.mode = "raw:x86_64"; - } - } - binary_type = BINARY_TYPE_RAW; - const int section_index = 0; - sections[section_index].offset = 0; - sections[section_index].functions.insert(0); - sections[section_index].size = data.size(); - sections[section_index].data = malloc(data.size()); - memset(sections[section_index].data, 0, sections[section_index].size); - total_exec_sections++; - if(sections[section_index].data == NULL) { - return false; - } - memcpy(sections[section_index].data, &data[0], sections[section_index].size); - CalculateFileHashes(data); - return true; -} - -Raw::~Raw(){ - for (uint32_t i = 0; i < total_exec_sections; i++){ - sections[i].size = 0; - sections[i].offset = 0; - free(sections[i].data); - sections[i].functions.clear(); - } -} diff --git a/src/sha256.c b/src/sha256.c deleted file mode 100644 index b86769cd..00000000 --- a/src/sha256.c +++ /dev/null @@ -1,161 +0,0 @@ -/********************************************************************* -* Filename: sha256.c -* Author: Brad Conte (brad AT bradconte.com) -* Copyright: -* Disclaimer: This code is presented "as is" without any guarantees. -* Details: Implementation of the SHA-256 hashing algorithm. - SHA-256 is one of the three algorithms in the SHA2 - specification. The others, SHA-384 and SHA-512, are not - offered in this implementation. - Algorithm specification can be found here: - * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf - This implementation uses little endian byte order. -*********************************************************************/ - -/*************************** HEADER FILES ***************************/ -#include -#include -#include "sha256.h" - -typedef uint32_t WORD; -typedef uint8_t BYTE; - -/****************************** MACROS ******************************/ -#define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b)))) -#define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b)))) - -#define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z))) -#define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) -#define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22)) -#define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25)) -#define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3)) -#define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10)) - -/**************************** VARIABLES *****************************/ -static const WORD k[64] = { - 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, - 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, - 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, - 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, - 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, - 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, - 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, - 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 -}; - -/*********************** FUNCTION DEFINITIONS ***********************/ -void sha256_transform(SHA256_CTX *ctx, const BYTE data[]) -{ - WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64]; - - for (i = 0, j = 0; i < 16; ++i, j += 4) - m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]); - for ( ; i < 64; ++i) - m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; - - a = ctx->state[0]; - b = ctx->state[1]; - c = ctx->state[2]; - d = ctx->state[3]; - e = ctx->state[4]; - f = ctx->state[5]; - g = ctx->state[6]; - h = ctx->state[7]; - - for (i = 0; i < 64; ++i) { - t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i]; - t2 = EP0(a) + MAJ(a,b,c); - h = g; - g = f; - f = e; - e = d + t1; - d = c; - c = b; - b = a; - a = t1 + t2; - } - - ctx->state[0] += a; - ctx->state[1] += b; - ctx->state[2] += c; - ctx->state[3] += d; - ctx->state[4] += e; - ctx->state[5] += f; - ctx->state[6] += g; - ctx->state[7] += h; -} - -void sha256_init(SHA256_CTX *ctx) -{ - ctx->datalen = 0; - ctx->bitlen = 0; - ctx->state[0] = 0x6a09e667; - ctx->state[1] = 0xbb67ae85; - ctx->state[2] = 0x3c6ef372; - ctx->state[3] = 0xa54ff53a; - ctx->state[4] = 0x510e527f; - ctx->state[5] = 0x9b05688c; - ctx->state[6] = 0x1f83d9ab; - ctx->state[7] = 0x5be0cd19; -} - -void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len) -{ - WORD i; - - for (i = 0; i < len; ++i) { - ctx->data[ctx->datalen] = data[i]; - ctx->datalen++; - if (ctx->datalen == 64) { - sha256_transform(ctx, ctx->data); - ctx->bitlen += 512; - ctx->datalen = 0; - } - } -} - -void sha256_final(SHA256_CTX *ctx, BYTE hash[]) -{ - WORD i; - - i = ctx->datalen; - - // Pad whatever data is left in the buffer. - if (ctx->datalen < 56) { - ctx->data[i++] = 0x80; - while (i < 56) - ctx->data[i++] = 0x00; - } - else { - ctx->data[i++] = 0x80; - while (i < 64) - ctx->data[i++] = 0x00; - sha256_transform(ctx, ctx->data); - memset(ctx->data, 0, 56); - } - - // Append to the padding the total message's length in bits and transform. - ctx->bitlen += ctx->datalen * 8; - ctx->data[63] = ctx->bitlen; - ctx->data[62] = ctx->bitlen >> 8; - ctx->data[61] = ctx->bitlen >> 16; - ctx->data[60] = ctx->bitlen >> 24; - ctx->data[59] = ctx->bitlen >> 32; - ctx->data[58] = ctx->bitlen >> 40; - ctx->data[57] = ctx->bitlen >> 48; - ctx->data[56] = ctx->bitlen >> 56; - sha256_transform(ctx, ctx->data); - - // Since this implementation uses little endian byte ordering and SHA uses big endian, - // reverse all the bytes when copying the final state to the output hash. - for (i = 0; i < 4; ++i) { - hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff; - hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff; - hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff; - hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff; - hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff; - hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff; - hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff; - hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff; - } -} diff --git a/src/types/lz4string.rs b/src/types/lz4string.rs new file mode 100644 index 00000000..89fdcfcc --- /dev/null +++ b/src/types/lz4string.rs @@ -0,0 +1,94 @@ +use std::convert::From; +use lz4::block::{compress, decompress}; + +/// A structure representing a compressed string using the LZ4 compression algorithm. +pub struct LZ4String { + /// The compressed representation of the string. + compressed_data: Vec, + /// The size of the original uncompressed string. + uncompressed_size: usize, +} + +impl LZ4String { + + /// Creates a new `LZ4String` from a given string slice. + /// + /// # Arguments + /// + /// * `data` - The string slice to compress. + /// + /// # Returns + /// + /// A new `LZ4String` containing the compressed data and the original size. + /// + /// # Panics + /// + /// This function will panic if the compression operation fails. + #[allow(dead_code)] + pub fn new(data: &str) -> Self { + let compressed = compress(data.as_bytes(), None, false).expect("lz4string compression failed"); + LZ4String { + compressed_data: compressed, + uncompressed_size: data.len(), + } + } + + /// Decompresses the `LZ4String` back into its original string representation. + /// + /// # Returns + /// + /// The original uncompressed string. + /// + /// # Panics + /// + /// This function will panic if the decompression operation fails or if the decompressed data is not valid UTF-8. + #[allow(dead_code)] + pub fn to_string(&self) -> String { + let decompressed = decompress(&self.compressed_data, Some(self.uncompressed_size as i32)) + .expect("lz4string decompression failed"); + String::from_utf8(decompressed).expect("lz4string invalid utf8") + } +} + +impl From for LZ4String { + /// Converts a `String` into an `LZ4String`. + /// + /// # Arguments + /// + /// * `data` - The string to compress. + /// + /// # Returns + /// + /// An `LZ4String` containing the compressed data and the original size. + /// + /// # Panics + /// + /// This function will panic if the compression operation fails. + fn from(data: String) -> Self { + let compressed = compress(data.as_bytes(), None, false).expect("lz4string compression failed"); + LZ4String { + compressed_data: compressed, + uncompressed_size: data.len(), + } + } +} + +impl std::fmt::Display for LZ4String { + /// Formats the `LZ4String` by decompressing it and writing the original string to the formatter. + /// + /// # Arguments + /// + /// * `f` - The formatter to write the decompressed string to. + /// + /// # Returns + /// + /// A result indicating whether the operation was successful. + /// + /// # Panics + /// + /// This method will panic if the decompression operation fails or if the decompressed data is not valid UTF-8. + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let s = self.to_string(); + write!(f, "{}", s) + } +} diff --git a/src/types/memorymappedfile.rs b/src/types/memorymappedfile.rs new file mode 100644 index 00000000..5f028760 --- /dev/null +++ b/src/types/memorymappedfile.rs @@ -0,0 +1,208 @@ +use memmap2::{Mmap, MmapMut}; +use std::fs::OpenOptions; +use std::io::{self, Error, Read, Seek, SeekFrom, Write}; +use std::path::PathBuf; + +#[cfg(windows)] +use std::os::windows::fs::OpenOptionsExt; + +#[cfg(windows)] +use winapi::um::winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE}; + +/// A `MemoryMappedFile` struct that provides a memory-mapped file interface, +/// enabling file read/write operations with optional disk caching, +/// and automatic file cleanup on object drop. +pub struct MemoryMappedFile { + /// Path to the file as a `String`. + pub path: String, + /// Handle to the file as an optional open file descriptor. + pub handle: Option, + /// Flag indicating whether the file is already cached (exists on disk). + pub is_cached: bool, + /// Flag to determine if the file should be cached. If `false`, the file will + /// be deleted upon the object being dropped. + pub cache: bool, +} + +impl MemoryMappedFile { + /// Creates a new `MemoryMappedFile` instance. + /// + /// This function opens a file at the specified path, with options to append and/or cache the file. + /// If the file's parent directories do not exist, they are created. + /// + /// # Arguments + /// + /// * `path` - The `PathBuf` specifying the file's location. + /// * `append` - If `true`, opens the file in append mode. + /// * `cache` - If `true`, retains the file on disk after the `MemoryMappedFile` instance is dropped. + /// + /// # Returns + /// + /// A `Result` containing the `MemoryMappedFile` on success, or an `io::Error` if file creation fails. + pub fn new(path: PathBuf, cache: bool) -> Result { + // if let Some(parent) = path.parent() { + // std::fs::create_dir_all(parent)?; + // } + + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).map_err(|e| { + io::Error::new( + e.kind(), + format!("failed to create parent directories for '{}': {}", + path.display(), e) + ) + })?; + } + + let is_cached = path.is_file(); + + let mut options = OpenOptions::new(); + + options.read(true).write(true).create(true); + + #[cfg(windows)] + options.share_mode(FILE_SHARE_READ | FILE_SHARE_WRITE); + + //let handle = options.open(&path)?; + + let handle = options.open(&path).map_err(|e| { + io::Error::new( + e.kind(), + format!("failed to open file '{}': {}", path.display(), e) + ) + })?; + + Ok(Self { + path: path.to_string_lossy().into_owned(), + handle: Some(handle), + is_cached, + cache, + }) + } + + pub fn seek_to_end(&mut self) -> Result { + if let Some(ref mut handle) = self.handle { + let pos = handle.seek(SeekFrom::End(0))?; + Ok(pos) + } else { + Err(io::Error::new(io::ErrorKind::Other, "File handle is closed")) + } + } + + /// Creates a new `MemoryMappedFile` instance in read-only mode. + /// + /// # Arguments + /// + /// * `path` - The `PathBuf` specifying the file's location. + /// + /// # Returns + /// + /// A `Result` containing the `MemoryMappedFile` on success, or an `Error` if file creation fails. + pub fn new_readonly(path: PathBuf) -> Result { + let mut options = OpenOptions::new(); + options.read(true).write(false).create(false); + + #[cfg(windows)] + options.share_mode(FILE_SHARE_READ); + + let handle = options.open(&path)?; + + Ok(Self { + path: path.to_string_lossy().into_owned(), + handle: Some(handle), + is_cached: false, + cache: false, + }) + } + + /// Explicitly closes the file handle. + pub fn close(&mut self) { + if let Some(file) = self.handle.take() { + drop(file); // Explicitly drop the file to close the handle + } + } + + /// Checks if the file is cached (exists on disk). + pub fn is_cached(&self) -> bool { + self.is_cached + } + + /// Retrieves the file path as a `String`. + pub fn path(&self) -> String { + self.path.clone() + } + + /// Writes data from a reader to the file. + pub fn write(&mut self, mut reader: R) -> Result { + if let Some(ref mut handle) = self.handle { + if handle.metadata()?.permissions().readonly() { + return Err(Error::new(io::ErrorKind::Other, "File is read-only")); + } + + let bytes_written = io::copy(&mut reader, handle)?; + handle.flush()?; + Ok(bytes_written) + } else { + Err(Error::new(io::ErrorKind::Other, "File handle is closed")) + } + } + + /// Adds symbolic padding (increases the file size without writing data) to the end of the file. + pub fn write_padding(&mut self, length: usize) -> Result<(), Error> { + if let Some(ref mut handle) = self.handle { + let current_size = handle.metadata()?.len(); + let new_size = current_size + length as u64; + + handle.set_len(new_size)?; + handle.seek(SeekFrom::Start(new_size))?; + Ok(()) + } else { + Err(Error::new(io::ErrorKind::Other, "File handle is closed")) + } + } + + /// Maps the file into memory as mutable using `mmap2`. + pub fn mmap_mut(&self) -> Result { + if let Some(ref handle) = self.handle { + unsafe { MmapMut::map_mut(handle) } + } else { + Err(Error::new(io::ErrorKind::Other, "File handle is closed")) + } + } + + /// Retrieves the size of the file in bytes. + pub fn size(&self) -> Result { + if let Some(ref handle) = self.handle { + Ok(handle.metadata()?.len()) + } else { + Err(Error::new(io::ErrorKind::Other, "File handle is closed")) + } + } + + /// Maps the file into memory using `mmap`. + pub fn mmap(&self) -> Result { + if let Some(ref handle) = self.handle { + unsafe { Mmap::map(handle) } + } else { + Err(Error::new(io::ErrorKind::Other, "File handle is closed")) + } + } +} + +/// Automatically handles cleanup for the `MemoryMappedFile` when it goes out of scope. +/// +/// If caching is disabled, this `Drop` implementation deletes the file from disk +/// when the `MemoryMappedFile` instance is dropped, provided there were no errors in file removal. +impl Drop for MemoryMappedFile { + fn drop(&mut self) { + // Ensure the file handle is dropped + self.close(); + + // Remove the file if caching is disabled + if !self.cache { + if let Err(error) = std::fs::remove_file(&self.path) { + eprintln!("Failed to remove file {}: {}", self.path, error); + } + } + } +} diff --git a/src/types/mod.rs b/src/types/mod.rs new file mode 100644 index 00000000..712b6ef6 --- /dev/null +++ b/src/types/mod.rs @@ -0,0 +1,5 @@ +pub mod lz4string; +pub mod memorymappedfile; + +pub use lz4string::LZ4String; +pub use memorymappedfile::MemoryMappedFile; diff --git a/test.py b/test.py deleted file mode 100755 index c8a5b372..00000000 --- a/test.py +++ /dev/null @@ -1,164 +0,0 @@ -#!/usr/bin/env python - -import os -import sys -import json -import zipfile -import subprocess - -zip_files = [ - 'tests/pe.zip', - 'tests/raw.zip', - 'tests/elf.zip', -] - -pe_files = [ - 'tests/pe/pe.cil.0', - 'tests/pe/pe.cil.1', - 'tests/pe/emotet.x86', - 'tests/pe/pe.trickbot.x86', - 'tests/pe/pe.trickbot.x86_64', - 'tests/pe/pe.x86', - 'tests/pe/pe.x86_64' -] - -elf_files = [ - 'tests/elf/elf.x86', - 'tests/elf/elf.x86_64' -] - -coverage_files = [ - { - 'ida': 'tests/ida_baseline/elf.x86.ida.json', - 'binlex': 'tests/elf/elf.x86.json' - }, - { - 'ida': 'tests/ida_baseline/elf.x86_64.ida.json', - 'binlex': 'tests/elf/elf.x86_64.json' - }, - { - 'ida': 'tests/ida_baseline/pe.trickbot.x86.ida.json', - 'binlex': 'tests/pe/pe.trickbot.x86.json' - }, - { - 'ida': 'tests/ida_baseline/pe.trickbot.x86_64.ida.json', - 'binlex': 'tests/pe/pe.trickbot.x86_64.json' - }, - { - 'ida': 'tests/ida_baseline/pe.x86.ida.json', - 'binlex': 'tests/pe/pe.x86.json' - }, - { - 'ida': 'tests/ida_baseline/pe.x86_64.ida.json', - 'binlex': 'tests/pe/pe.x86_64.json' - } -] - -def noramilize_binlex_data(binlex_flat_file): - # Parse binlex data into three sets - # function_set - set of function addresses - # bb_set - set of bb addresses - # bb_code_map - dict of basic block bytes - function_set = set() - bb_set = set() - bb_code_map = {} - with open(binlex_flat_file,'r') as fp: - for line in fp.read().split('\n'): - if line != '': - trait_data = json.loads(line) - trait_type = trait_data.get('type') - trait_offset = trait_data.get('offset') - if trait_type == "function": - function_set.add(trait_offset) - elif trait_type == "block": - bb_set.add(trait_offset) - bb_code_map[trait_offset] = trait_data.get('bytes') - return function_set, bb_set, bb_code_map - - -def normalize_ida_data(ida_json_file): - # Parse ida data into three sets - # function_set - set of function addresses - # bb_set - set of bb addresses - # bb_code_map - dict of basic block bytes - function_set = set() - bb_set = set() - bb_code_map = {} - ida_data = json.loads(open(ida_json_file,'r').read()) - for trait_data in ida_data: - trait_type = trait_data.get('type') - trait_offset = trait_data.get('offset') - if trait_type == "function": - function_set.add(trait_offset) - elif trait_type == "block": - bb_set.add(trait_offset) - bb_code_map[trait_offset] = trait_data.get('bytes') - return function_set, bb_set, bb_code_map - - -def calculate_coverage(ida_json_file, binlex_flat_file): - binlex_function_set, binlex_bb_set, binlex_bb_code_map = noramilize_binlex_data(binlex_flat_file) - ida_function_set, ida_bb_set, ida_bb_code_map = normalize_ida_data(ida_json_file) - - total_missing_functions = len(ida_function_set.difference(binlex_function_set)) - total_functions = len(ida_function_set) - function_coverage = round((total_functions - total_missing_functions)/total_functions* 100,2) - - intersection_bb = ida_bb_set.intersection(binlex_bb_set) - total_bb = len(ida_bb_set) - total_missing_bb = total_bb - len(intersection_bb) - bb_coverage = round((total_bb - total_missing_bb)/total_bb* 100,2) - total_extra_bb = len(binlex_bb_set) - len(intersection_bb) - - total_missmatch_bb = 0 - - for bb in intersection_bb: - if binlex_bb_code_map[bb] != ida_bb_code_map[bb]: - total_missmatch_bb += 1 - - return {'total_missing_functions':total_missing_functions, - 'total_functions':total_functions, - 'function_coverage':function_coverage, - 'total_missing_bb':total_missing_bb, - 'total_bb':total_bb, - 'bb_coverage':bb_coverage, - 'total_missmatch_bb':total_missmatch_bb, - 'extra_bb':total_extra_bb - } - -def print_coverage(coverage): - print(f"Function coverage: {coverage.get('function_coverage')}% ( Missing {coverage.get('total_missing_functions')} from total {coverage.get('total_functions')} )") - print(f"Basic block coverage: {coverage.get('bb_coverage')}% ( Missing {coverage.get('total_missing_bb')} from total {coverage.get('total_bb')} )") - print(f"Basic block errors: {coverage.get('total_missmatch_bb')}") - print(f"Extra blocks from binlex: {coverage.get('extra_bb')}") - -for zip_file in zip_files: - print("[-] {}".format(zip_file)) - z = zipfile.ZipFile(zip_file,"r") - z.setpassword(b'infected') - z.extractall("tests/") - print("[*] {}".format(zip_file)) - -z = zipfile.ZipFile('tests/ida_baseline.zip', 'r') -z.extractall('tests/') - -for pe_file in pe_files: - print("[-] {}".format(pe_file)) - command = [ - 'build/binlex', - '-i', pe_file, - '-o', os.path.join(os.path.dirname(pe_file),os.path.basename(pe_file) + '.json')] - subprocess.run(command) - print("[*] {}".format(pe_file)) - -for elf_file in elf_files: - print("[-] {}".format(elf_file)) - command = [ - 'build/binlex', - '-i', elf_file, - '-o', os.path.join(os.path.dirname(elf_file),os.path.basename(elf_file) + '.json')] - subprocess.run(command) - print("[*] {}".format(elf_file)) - -for coverage_file in coverage_files: - print_coverage(calculate_coverage(coverage_file['ida'], coverage_file['binlex'])) diff --git a/tests/.gitignore b/tests/.gitignore deleted file mode 100644 index e53f6b51..00000000 --- a/tests/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -elf/** -pe/** -raw/** -macho/** -cil/** -venv/** -ida_baseline/** diff --git a/tests/cil.zip b/tests/cil.zip deleted file mode 100644 index 6777b4da..00000000 --- a/tests/cil.zip +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:764fde3a4d85a289b41815581382a4bbf4e937d4c0fb7dd16453eb6f17d3cedb -size 402 diff --git a/tests/elf.zip b/tests/elf.zip deleted file mode 100644 index 6552f56d..00000000 --- a/tests/elf.zip +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:8c347c1ce61fec25fc892ac1010a6f2b7d4a1cf4081e4d9c76391824cee8c946 -size 340161 diff --git a/tests/ida_baseline.zip b/tests/ida_baseline.zip deleted file mode 100644 index 395b3dff..00000000 --- a/tests/ida_baseline.zip +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:10ada2adf2f73a33c890201985f597b594df4fb3b9181669217d540cbc642aa3 -size 2002387 diff --git a/tests/macho.zip b/tests/macho.zip deleted file mode 100644 index 5328d6be..00000000 --- a/tests/macho.zip +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:ec3d6afff0a6ba90edce90ca081dc7f74585a8875ca6367669a959c611ffe978 -size 13707 diff --git a/tests/pe.zip b/tests/pe.zip deleted file mode 100644 index 9455161e..00000000 --- a/tests/pe.zip +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:76e9e7a880fc368189688bec997d1af4bdce3b0830ae3a5f52a45ac0778059ae -size 2901862 diff --git a/tests/raw.zip b/tests/raw.zip deleted file mode 100644 index 90d4dc7c..00000000 --- a/tests/raw.zip +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:dd85f6be2f703c076ca8d2fd2d1c19f35030e2f2472a1b2c08aacb06b8713529 -size 1328 diff --git a/tests/test_models_binary.rs b/tests/test_models_binary.rs new file mode 100644 index 00000000..2deb7a58 --- /dev/null +++ b/tests/test_models_binary.rs @@ -0,0 +1,18 @@ +#[cfg(test)] +mod tests { + use binlex::binary::Binary; + + #[test] + fn test_models_binary_to_hex(){ + let data = vec![0xDE, 0xAD, 0xBE, 0xEF]; + let result = Binary::to_hex(&data); + assert_eq!(result, "deadbeef", "hex string does not match"); + } + + #[test] + fn test_models_binary_hexdump() { + let data = vec![0xDE, 0xAD, 0xBE, 0xEF]; + let result = Binary::hexdump(&data, 0); + assert_eq!(result, "00000000: de ad be ef |....|\n", "hexdump string does not match"); + } +} diff --git a/tests/test_models_hashing.rs b/tests/test_models_hashing.rs new file mode 100644 index 00000000..452e0136 --- /dev/null +++ b/tests/test_models_hashing.rs @@ -0,0 +1,42 @@ +#[cfg(test)] +mod tests { + + use binlex::hashing::SHA256; + use binlex::hashing::TLSH; + use binlex::hashing::MinHash32; + + #[test] + fn test_models_hashing_sha256() { + let data = vec![0xDE, 0xAD, 0xBE, 0xEF]; + let hexdigest = SHA256::new(&data).hexdigest(); + assert!(hexdigest.is_some(), "hexdigest should not be none"); + assert_eq!(hexdigest.unwrap(), "5f78c33274e43fa9de5659265c1d917e25c03722dcb0b8d27db8d5feaa813953", "hexdigest does not match the expected value"); + } + + #[test] + fn test_models_hashing_tlsh() { + let data: Vec = vec![ + 0x3A, 0x7F, 0x92, 0x5C, 0xE4, 0xA1, 0xD8, 0x47, 0x29, 0xB3, + 0x1E, 0x8D, 0x4F, 0x6A, 0xCD, 0x72, 0x90, 0x33, 0xB6, 0xF1, + 0xD4, 0x5E, 0xAA, 0x64, 0x13, 0xFA, 0x38, 0x9C, 0x41, 0xB8, + 0xD0, 0xE7, 0x6F, 0x25, 0xA9, 0x54, 0x1B, 0xC2, 0x8E, 0xF5, + 0x77, 0x3D, 0xAC, 0x12, 0x8A, 0x9E, 0x6B, 0xC7, 0x5A, 0xEF]; + let hexdigest = TLSH::new(&data, 50).hexdigest(); + assert!(hexdigest.is_some(), "hexdigest should not be none"); + assert_eq!(hexdigest.unwrap(), "T13390022E54110904084C76152B45D85A53A52164A647348D894A421D554C0266352468", "hexdigest does not match the expected value"); + } + + #[test] + fn test_models_hashing_minhash() { + let data: Vec = vec![ + 0x3A, 0x7F, 0x92, 0x5C, 0xE4, 0xA1, 0xD8, 0x47, 0x29, 0xB3, + 0x1E, 0x8D, 0x4F, 0x6A, 0xCD, 0x72, 0x90, 0x33, 0xB6, 0xF1, + 0xD4, 0x5E, 0xAA, 0x64, 0x13, 0xFA, 0x38, 0x9C, 0x41, 0xB8, + 0xD0, 0xE7, 0x6F, 0x25, 0xA9, 0x54, 0x1B, 0xC2, 0x8E, 0xF5, + 0x77, 0x3D, 0xAC, 0x12, 0x8A, 0x9E, 0x6B, 0xC7, 0x5A, 0xEF]; + let hexdigest = MinHash32::new(&data, 64, 4, 0).hexdigest(); + assert!(hexdigest.is_some(), "hexdigest should not be none"); + assert_eq!(hexdigest.unwrap(), "00510c10037f0c85108b1886039fba0907d95f6f012c5a570358233b016873a000ba1ef80b1cf59f0675d519066afadd021ae2420147ed0b084c726703cb11900eb906aa040ec25d01001a10011889ab040e3b94000fec940b2506870538268300e5e9b50a7740d70858815105789e8a03f7296d00c77bc600e3a1b800717a8e02da37480096176f00b442c30463506c032f0efe08c1512c02c057d10c612b8e046f8c5a05f06c0317ac542c06254c91023009c60bccf3510c1a81ef01b1cfd6021ddf2f04e63b4a03884e2b079acef81622d85901db282d05d417c103ba54c40b19a64c0b6720f102125783033628850147997d06ae204c0835ee0a06b3b80b", "hexdigest does not match the expected value"); + } + +} diff --git a/tests/test_pe_disassembler.rs b/tests/test_pe_disassembler.rs new file mode 100644 index 00000000..c500cf7c --- /dev/null +++ b/tests/test_pe_disassembler.rs @@ -0,0 +1,212 @@ +// PE Disassembler Tests + +// PE File Source Code +// #include +// #include +// #include + +// #ifdef _WIN32 +// // Windows-specific includes +// #include +// #define EXPORT __declspec(dllexport) +// #else +// // Non-Windows platform specific exports +// #define EXPORT __attribute__((visibility("default"))) +// #endif + +// extern "C" { +// EXPORT void vtable_example(); +// EXPORT void jump_table_example(int option); +// EXPORT void function_one(); +// EXPORT void function_two(); +// EXPORT void function_three(); +// EXPORT void call_sequence_example(); +// EXPORT int recursive_function(int n); + +// #ifdef _WIN32 +// // Exported TLS callback function for Windows +// EXPORT void tls_callback(PVOID DllHandle, DWORD Reason, PVOID Reserved); +// #endif +// } + +// // Define the TLS callback function, which is only compiled on Windows +// #ifdef _WIN32 +// void tls_callback(PVOID DllHandle, DWORD Reason, PVOID Reserved) { +// if (Reason == DLL_THREAD_ATTACH) { +// std::cout << "Thread attached!" << std::endl; +// } +// else if (Reason == DLL_THREAD_DETACH) { +// std::cout << "Thread detached!" << std::endl; +// } +// else if (Reason == DLL_PROCESS_ATTACH) { +// std::cout << "Process attached!" << std::endl; +// } +// else if (Reason == DLL_PROCESS_DETACH) { +// std::cout << "Process detached!" << std::endl; +// } +// } + +// // Function to simulate DLL entry point (DllMain) - only for Windows +// BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { +// // Register TLS callback when the process is attached or detached +// if (ul_reason_for_call == DLL_PROCESS_ATTACH) { +// // Simulate TLS callback invocation when DLL is loaded +// tls_callback(hModule, DLL_PROCESS_ATTACH, lpReserved); +// } +// else if (ul_reason_for_call == DLL_PROCESS_DETACH) { +// // Simulate TLS callback invocation when DLL is unloaded +// tls_callback(hModule, DLL_PROCESS_DETACH, lpReserved); +// } +// return TRUE; +// } +// #endif + +// class Base { +// public: +// virtual ~Base() {} +// virtual void execute() = 0; +// }; + +// class DerivedA : public Base { +// public: +// void execute() override { +// std::cout << "Executing DerivedA" << std::endl; +// } +// }; + +// class DerivedB : public Base { +// public: +// void execute() override { +// std::cout << "Executing DerivedB" << std::endl; +// } +// }; + +// class DerivedC : public Base { +// public: +// void execute() override { +// std::cout << "Executing DerivedC" << std::endl; +// } +// }; + +// void vtable_example() { +// std::vector objects = { new DerivedA(), new DerivedB(), new DerivedC() }; +// for (auto obj : objects) { +// obj->execute(); +// delete obj; +// } +// } + +// // Jump table example function +// void jump_table_example(int option) { +// switch (option) { +// case 1: std::cout << "Option 1 selected" << std::endl; break; +// case 2: std::cout << "Option 2 selected" << std::endl; break; +// case 3: std::cout << "Option 3 selected" << std::endl; break; +// case 4: std::cout << "Option 4 selected" << std::endl; break; +// default: std::cout << "Invalid option" << std::endl; break; +// } +// } + +// // Regular function calls to demonstrate call sequences +// void function_one() { +// std::cout << "Function One" << std::endl; +// } + +// void function_two() { +// std::cout << "Function Two" << std::endl; +// } + +// void function_three() { +// std::cout << "Function Three" << std::endl; +// } + +// void call_sequence_example() { +// function_one(); +// function_two(); +// function_three(); +// } + +// // A simple recursive function for disassembler testing +// int recursive_function(int n) { +// if (n <= 0) return 0; +// return n + recursive_function(n - 1); +// } + +// int main() { +// // Test the vtable functionality +// std::cout << "Testing vtable example:" << std::endl; +// vtable_example(); + +// // Test the jump table example +// std::cout << "\nTesting jump table example:" << std::endl; +// for (int i = 1; i <= 5; ++i) { +// jump_table_example(i); +// } + +// // Test the call sequence +// std::cout << "\nTesting call sequence example:" << std::endl; +// call_sequence_example(); + +// // Test recursive function +// std::cout << "\nTesting recursive function:" << std::endl; +// int result = recursive_function(5); +// std::cout << "Result of recursive_function(5): " << result << std::endl; + +// return 0; +// } + +// Compiled PE Binary +static DATA: &[u8] = &[0x04, 0x22, 0x4d, 0x18, 0x64, 0x40, 0xa7, 0x0a, 0x28, 0x00, 0x00, 0xf2, 0x03, 0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xb8, 0x00, 0x01, 0x00, 0x12, 0x40, 0x07, 0x00, 0x0f, 0x02, 0x00, 0x0a, 0xf3, 0x2e, 0xf8, 0x00, 0x00, 0x00, 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd, 0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21, 0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6e, 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20, 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x0d, 0x0d, 0x0a, 0x24, 0x5a, 0x00, 0x84, 0xe0, 0x9e, 0x7a, 0xee, 0xa4, 0xff, 0x14, 0xbd, 0x04, 0x00, 0xd1, 0xad, 0x87, 0x87, 0xbd, 0xae, 0xff, 0x14, 0xbd, 0x24, 0x84, 0x11, 0xbc, 0xb3, 0x08, 0x00, 0x22, 0x10, 0xbc, 0x10, 0x00, 0x31, 0x17, 0xbc, 0xa7, 0x10, 0x00, 0xb1, 0x15, 0xbc, 0xa3, 0xff, 0x14, 0xbd, 0xb0, 0x94, 0x15, 0xbc, 0xa6, 0x38, 0x00, 0xb1, 0x15, 0xbd, 0xeb, 0xff, 0x14, 0xbd, 0x2a, 0x84, 0x1d, 0xbc, 0xa5, 0x08, 0x00, 0x13, 0x14, 0x08, 0x00, 0x22, 0xeb, 0xbd, 0x10, 0x00, 0x11, 0x16, 0x10, 0x00, 0x40, 0x52, 0x69, 0x63, 0x68, 0x64, 0x00, 0x03, 0x77, 0x00, 0xd4, 0x00, 0x50, 0x45, 0x00, 0x00, 0x64, 0x86, 0x06, 0x00, 0x81, 0xf0, 0x36, 0x67, 0x14, 0x00, 0xe2, 0xf0, 0x00, 0x22, 0x00, 0x0b, 0x02, 0x0e, 0x21, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x30, 0x16, 0x00, 0x61, 0xd0, 0x1c, 0x00, 0x00, 0x00, 0x10, 0x0c, 0x00, 0x20, 0x40, 0x01, 0x07, 0x00, 0x00, 0x0c, 0x00, 0x40, 0x02, 0x00, 0x00, 0x06, 0x0c, 0x00, 0x16, 0x00, 0x08, 0x00, 0x23, 0x00, 0x90, 0x45, 0x01, 0x72, 0x00, 0x00, 0x00, 0x03, 0x00, 0x60, 0x81, 0x29, 0x00, 0x05, 0x3c, 0x00, 0x06, 0x09, 0x00, 0x00, 0x40, 0x00, 0x03, 0x02, 0x00, 0x00, 0x0b, 0x00, 0xd0, 0x80, 0x42, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x84, 0x43, 0x00, 0x00, 0xc8, 0x18, 0x00, 0x40, 0x70, 0x00, 0x00, 0xe0, 0x69, 0x00, 0x50, 0x60, 0x00, 0x00, 0xc4, 0x02, 0x11, 0x00, 0x03, 0x02, 0x00, 0x40, 0x80, 0x00, 0x00, 0x78, 0x97, 0x00, 0x11, 0x37, 0x23, 0x00, 0x0f, 0x02, 0x00, 0x06, 0x24, 0xf0, 0x35, 0xa9, 0x00, 0x03, 0x02, 0x00, 0x48, 0x30, 0x00, 0x00, 0x50, 0x48, 0x00, 0x0b, 0x02, 0x00, 0xa3, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x39, 0x18, 0xa3, 0x00, 0x00, 0xfc, 0x00, 0x00, 0x0d, 0x02, 0x07, 0x02, 0x00, 0xe0, 0x20, 0x00, 0x00, 0x60, 0x2e, 0x72, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x80, 0x1e, 0x85, 0x00, 0x02, 0x15, 0x00, 0x10, 0x00, 0x0c, 0x00, 0x07, 0x02, 0x00, 0x52, 0x40, 0x00, 0x00, 0x40, 0x2e, 0x27, 0x00, 0x30, 0x00, 0x58, 0x07, 0x65, 0x01, 0x02, 0x2c, 0x01, 0x27, 0x00, 0x3e, 0x25, 0x00, 0x02, 0x28, 0x00, 0x32, 0xc0, 0x2e, 0x70, 0x29, 0x00, 0x01, 0xe4, 0x00, 0x13, 0x60, 0x3c, 0x01, 0x0b, 0x75, 0x02, 0x01, 0x50, 0x00, 0x53, 0x72, 0x73, 0x72, 0x63, 0x00, 0x14, 0x01, 0x13, 0x70, 0x50, 0x00, 0x11, 0x44, 0x47, 0x00, 0x05, 0x02, 0x00, 0x02, 0x28, 0x00, 0x42, 0x65, 0x6c, 0x6f, 0x63, 0x24, 0x01, 0x23, 0x00, 0x80, 0x28, 0x00, 0x15, 0x46, 0x23, 0x00, 0x01, 0x02, 0x00, 0x41, 0x40, 0x00, 0x00, 0x42, 0x09, 0x00, 0x0f, 0x02, 0x00, 0xf8, 0xf8, 0x25, 0x40, 0x53, 0x48, 0x83, 0xec, 0x20, 0x48, 0x8b, 0xd9, 0x48, 0x8b, 0xc2, 0x48, 0x8d, 0x0d, 0xf5, 0x22, 0x00, 0x00, 0x0f, 0x57, 0xc0, 0x48, 0x8d, 0x53, 0x08, 0x48, 0x89, 0x0b, 0x48, 0x8d, 0x48, 0x08, 0x0f, 0x11, 0x02, 0xff, 0x15, 0xe6, 0x20, 0x00, 0x00, 0x48, 0x8b, 0xc3, 0x48, 0x83, 0xc4, 0x20, 0x5b, 0xc3, 0xcc, 0x01, 0x00, 0xfb, 0x03, 0x48, 0x8b, 0x51, 0x08, 0x48, 0x8d, 0x05, 0x25, 0x23, 0x00, 0x00, 0x48, 0x85, 0xd2, 0x48, 0x0f, 0x45, 0xc2, 0x20, 0x00, 0x51, 0x89, 0x5c, 0x24, 0x08, 0x57, 0x64, 0x00, 0x40, 0x8d, 0x05, 0x97, 0x22, 0x47, 0x00, 0xf0, 0x07, 0xf9, 0x48, 0x89, 0x01, 0x8b, 0xda, 0x48, 0x83, 0xc1, 0x08, 0xff, 0x15, 0x95, 0x20, 0x00, 0x00, 0xf6, 0xc3, 0x01, 0x74, 0x0d, 0xba, 0x80, 0x02, 0x60, 0x48, 0x8b, 0xcf, 0xe8, 0xa7, 0x09, 0x08, 0x00, 0x60, 0x5c, 0x24, 0x30, 0x48, 0x8b, 0xc7, 0x70, 0x00, 0x1b, 0x5f, 0x50, 0x00, 0x30, 0x8d, 0x05, 0x51, 0x46, 0x00, 0x20, 0x89, 0x01, 0x41, 0x00, 0x77, 0x48, 0xff, 0x25, 0x53, 0x20, 0x00, 0x00, 0x91, 0x00, 0x40, 0x48, 0x8d, 0x05, 0xb1, 0x20, 0x00, 0x20, 0xc7, 0x41, 0x5e, 0x03, 0x40, 0x00, 0x48, 0x89, 0x41, 0x9f, 0x00, 0x10, 0x5e, 0x13, 0x00, 0x5a, 0x89, 0x01, 0x48, 0x8b, 0xc1, 0x4e, 0x00, 0x2c, 0xcc, 0xcc, 0x00, 0x01, 0x1f, 0x21, 0x00, 0x01, 0x03, 0x71, 0x1f, 0x00, 0x00, 0x48, 0x8d, 0x05, 0x17, 0x47, 0x00, 0x18, 0x03, 0x0a, 0x01, 0x0b, 0x40, 0x00, 0x1f, 0xb5, 0x40, 0x00, 0x03, 0x12, 0xa6, 0x40, 0x00, 0x2d, 0xaf, 0x21, 0x40, 0x00, 0xf1, 0x06, 0x48, 0x83, 0xec, 0x28, 0x83, 0xfa, 0x02, 0x75, 0x09, 0x48, 0x8d, 0x15, 0x10, 0x22, 0x00, 0x00, 0xeb, 0x27, 0x83, 0xfa, 0x03, 0x0e, 0x00, 0x10, 0x1a, 0x0e, 0x00, 0x41, 0x19, 0x83, 0xfa, 0x01, 0x0e, 0x00, 0x10, 0x24, 0x0e, 0x00, 0x90, 0x0b, 0x85, 0xd2, 0x75, 0x28, 0x48, 0x8d, 0x15, 0x2f, 0x88, 0x00, 0x90, 0x8b, 0x0d, 0xd8, 0x1e, 0x00, 0x00, 0xe8, 0xfb, 0x05, 0x9b, 0x00, 0xf0, 0x04, 0x15, 0xc4, 0x07, 0x00, 0x00, 0x48, 0x8b, 0xc8, 0x48, 0x83, 0xc4, 0x28, 0x48, 0xff, 0x25, 0xae, 0x1e, 0x00, 0x00, 0x0b, 0x00, 0x14, 0xc3, 0xe0, 0x00, 0x30, 0x8d, 0x05, 0xf3, 0xa2, 0x01, 0xf0, 0x03, 0x8b, 0xd9, 0x48, 0x89, 0x01, 0xf6, 0xc2, 0x01, 0x74, 0x0a, 0xba, 0x08, 0x00, 0x00, 0x00, 0xe8, 0x3a, 0x08, 0x36, 0x00, 0x08, 0xd8, 0x01, 0x00, 0x90, 0x00, 0x40, 0x48, 0x8b, 0x0d, 0x7d, 0x41, 0x00, 0x80, 0x8d, 0x15, 0xde, 0x21, 0x00, 0x00, 0xe8, 0x99, 0x62, 0x00, 0x60, 0x8b, 0xc8, 0x48, 0x8d, 0x15, 0x5f, 0x65, 0x00, 0x02, 0x62, 0x00, 0x22, 0x4c, 0x1e, 0x77, 0x01, 0x03, 0x30, 0x00, 0x12, 0x4d, 0x30, 0x00, 0x10, 0xc6, 0x30, 0x00, 0x15, 0x69, 0x30, 0x00, 0x16, 0x2f, 0x30, 0x00, 0x1a, 0x1c, 0x30, 0x00, 0x12, 0x1d, 0x30, 0x00, 0x10, 0xae, 0x30, 0x00, 0x15, 0x39, 0x30, 0x00, 0x22, 0xff, 0x06, 0xb7, 0x00, 0x53, 0x48, 0xff, 0x25, 0xec, 0x1d, 0x60, 0x00, 0xa0, 0x89, 0x5c, 0x24, 0x18, 0x55, 0x48, 0x83, 0xec, 0x40, 0xb9, 0xb2, 0x00, 0x70, 0x48, 0x89, 0x74, 0x24, 0x50, 0xe8, 0x47, 0x58, 0x00, 0x63, 0x8d, 0x15, 0xe8, 0x22, 0x00, 0x00, 0x16, 0x00, 0x82, 0x10, 0x48, 0x89, 0x44, 0x24, 0x20, 0xe8, 0x2e, 0x19, 0x00, 0x1b, 0xe7, 0x19, 0x00, 0x31, 0x28, 0xe8, 0x15, 0x19, 0x00, 0x20, 0x0d, 0x9e, 0x39, 0x01, 0x31, 0x89, 0x08, 0xb9, 0x6d, 0x02, 0xb1, 0x89, 0x44, 0x24, 0x30, 0xe8, 0xfc, 0x06, 0x00, 0x00, 0x41, 0xb8, 0x10, 0x00, 0x40, 0x8d, 0x54, 0x24, 0x20, 0x43, 0x01, 0x90, 0x8b, 0xe8, 0x48, 0x8d, 0x58, 0x18, 0xe8, 0x3e, 0x14, 0x1c, 0x01, 0xe0, 0xf5, 0x48, 0x3b, 0xeb, 0x74, 0x31, 0x48, 0x89, 0x7c, 0x24, 0x58, 0x0f, 0x1f, 0x44, 0x12, 0x00, 0xe0, 0x3e, 0x48, 0x8b, 0xcf, 0x48, 0x8b, 0x07, 0xff, 0x50, 0x08, 0x48, 0x8b, 0x07, 0xba, 0xab, 0x05, 0xf0, 0x18, 0x48, 0x8b, 0xcf, 0xff, 0x10, 0x48, 0x83, 0xc6, 0x08, 0x48, 0x3b, 0xf3, 0x75, 0xde, 0x48, 0x8b, 0x7c, 0x24, 0x58, 0x48, 0x8b, 0x74, 0x24, 0x50, 0x48, 0x85, 0xed, 0x74, 0x33, 0x48, 0x2b, 0xdd, 0x48, 0x83, 0xe3, 0xf8, 0x48, 0x81, 0xfb, 0x47, 0x06, 0xf0, 0x13, 0x72, 0x18, 0x48, 0x8b, 0x4d, 0xf8, 0x48, 0x83, 0xc3, 0x27, 0x48, 0x2b, 0xe9, 0x48, 0x8d, 0x45, 0xf8, 0x48, 0x83, 0xf8, 0x1f, 0x77, 0x19, 0x48, 0x8b, 0xe9, 0x48, 0x8b, 0xd3, 0x48, 0x8b, 0xcd, 0xe8, 0xa8, 0x03, 0x01, 0xd3, 0x8b, 0x5c, 0x24, 0x60, 0x48, 0x83, 0xc4, 0x40, 0x5d, 0xc3, 0xff, 0x15, 0x23, 0x39, 0x01, 0x03, 0x02, 0x00, 0x00, 0x70, 0x01, 0x50, 0x83, 0xe9, 0x01, 0x74, 0x3a, 0x05, 0x00, 0x10, 0x2c, 0x05, 0x00, 0xc0, 0x1e, 0x83, 0xf9, 0x01, 0x48, 0x8b, 0x0d, 0xcb, 0x1c, 0x00, 0x00, 0x74, 0x46, 0x02, 0xa0, 0xd2, 0x20, 0x00, 0x00, 0xeb, 0x29, 0x48, 0x8d, 0x15, 0xb1, 0x09, 0x00, 0x50, 0x20, 0x48, 0x8d, 0x15, 0x90, 0x09, 0x00, 0x50, 0x10, 0x48, 0x8d, 0x15, 0x6f, 0x09, 0x00, 0x51, 0x07, 0x48, 0x8d, 0x15, 0x4e, 0xd0, 0x03, 0x81, 0x0d, 0x97, 0x1c, 0x00, 0x00, 0xe8, 0xba, 0x03, 0x41, 0x02, 0x13, 0x83, 0x86, 0x01, 0x02, 0x7f, 0x01, 0x22, 0x6d, 0x1c, 0x7f, 0x01, 0x01, 0x70, 0x00, 0x30, 0x48, 0x8b, 0x0d, 0x10, 0x00, 0x90, 0x48, 0x8d, 0x15, 0x86, 0x20, 0x00, 0x00, 0xe8, 0x89, 0x31, 0x00, 0x01, 0x10, 0x02, 0x10, 0x4f, 0x34, 0x00, 0x02, 0x31, 0x00, 0x13, 0x3c, 0x31, 0x00, 0x03, 0x30, 0x00, 0x12, 0x3d, 0x30, 0x00, 0x10, 0x66, 0x30, 0x00, 0x15, 0x59, 0x30, 0x00, 0x16, 0x1f, 0x30, 0x00, 0x1a, 0x0c, 0x30, 0x00, 0x12, 0x0d, 0x30, 0x00, 0x10, 0x46, 0x30, 0x00, 0x15, 0x29, 0x30, 0x00, 0x25, 0xef, 0x04, 0x60, 0x00, 0x22, 0xdc, 0x1b, 0x91, 0x00, 0x03, 0x60, 0x00, 0x21, 0xdd, 0x1b, 0x60, 0x00, 0x70, 0xf6, 0x1f, 0x00, 0x00, 0xe8, 0xf9, 0x02, 0x83, 0x01, 0xb0, 0xc8, 0x48, 0x8d, 0x15, 0xbf, 0x04, 0x00, 0x00, 0xff, 0x15, 0xb1, 0x1c, 0x00, 0x30, 0x8b, 0x0d, 0xba, 0x07, 0x00, 0x30, 0x8d, 0x15, 0xe3, 0x23, 0x00, 0x15, 0xd6, 0x23, 0x00, 0x11, 0x9c, 0x23, 0x00, 0x10, 0x8e, 0x1c, 0x00, 0x30, 0x8b, 0x0d, 0x97, 0x07, 0x00, 0x30, 0x8d, 0x15, 0xd0, 0x23, 0x00, 0x15, 0xb3, 0x23, 0x00, 0x16, 0x79, 0x76, 0x00, 0x13, 0x66, 0x76, 0x00, 0x06, 0x02, 0x00, 0x02, 0xf0, 0x03, 0x80, 0x8b, 0xd9, 0x85, 0xc9, 0x7f, 0x08, 0x33, 0xc0, 0xa1, 0x04, 0xa8, 0x5b, 0xc3, 0xff, 0xc9, 0xe8, 0xe5, 0xff, 0xff, 0xff, 0x03, 0x48, 0x03, 0x04, 0x02, 0x00, 0x02, 0x30, 0x00, 0x42, 0x48, 0x8b, 0x0d, 0x2b, 0x6c, 0x00, 0x10, 0x74, 0x6c, 0x00, 0x15, 0x47, 0x6c, 0x00, 0x11, 0x0d, 0x8f, 0x00, 0xd2, 0xff, 0x1a, 0x00, 0x00, 0xe8, 0x12, 0xfd, 0xff, 0xff, 0x48, 0x8b, 0x0d, 0x03, 0x28, 0x00, 0x10, 0x64, 0x28, 0x00, 0x15, 0x1f, 0x28, 0x00, 0x20, 0xe5, 0x03, 0xda, 0x00, 0x50, 0xd7, 0x1a, 0x00, 0x00, 0xbb, 0x72, 0x02, 0x51, 0x66, 0x66, 0x0f, 0x1f, 0x84, 0xc6, 0x06, 0xb1, 0x8b, 0xcb, 0x83, 0xe9, 0x01, 0x0f, 0x84, 0x73, 0x01, 0x00, 0x00, 0x09, 0x00, 0x14, 0x40, 0x09, 0x00, 0x10, 0x2e, 0x09, 0x00, 0x60, 0xf9, 0x01, 0x0f, 0x84, 0x1c, 0x01, 0x1f, 0x01, 0x31, 0x0d, 0xab, 0x1a, 0x32, 0x01, 0x10, 0xb4, 0x34, 0x04, 0x11, 0xc7, 0x13, 0x00, 0x00, 0x32, 0x01, 0x11, 0x8d, 0x58, 0x00, 0xf2, 0x00, 0x7f, 0x1a, 0x00, 0x00, 0xff, 0xc3, 0x83, 0xfb, 0x05, 0x7e, 0xb0, 0x48, 0x8b, 0x0d, 0x81, 0x2a, 0x00, 0x10, 0x02, 0x82, 0x00, 0x15, 0x9d, 0x2a, 0x00, 0x11, 0x63, 0x2a, 0x00, 0x10, 0x55, 0x1c, 0x00, 0x30, 0x8b, 0x0d, 0x5e, 0x07, 0x00, 0x30, 0x8d, 0x15, 0x77, 0x4d, 0x00, 0x15, 0x7a, 0x23, 0x00, 0x11, 0x40, 0x23, 0x00, 0x10, 0x32, 0x1c, 0x00, 0x30, 0x8b, 0x0d, 0x3b, 0x07, 0x00, 0x30, 0x8d, 0x15, 0x64, 0x23, 0x00, 0x15, 0x57, 0x23, 0x00, 0x11, 0x1d, 0x23, 0x00, 0x10, 0x0f, 0x1c, 0x00, 0x30, 0x8b, 0x0d, 0x18, 0x07, 0x00, 0x30, 0x8d, 0x15, 0x51, 0x23, 0x00, 0x15, 0x34, 0x23, 0x00, 0x20, 0xfa, 0x02, 0xeb, 0x00, 0x21, 0xec, 0x19, 0xb6, 0x00, 0x10, 0xf5, 0x07, 0x00, 0x30, 0x8d, 0x15, 0x96, 0x23, 0x00, 0x15, 0x11, 0x23, 0x00, 0x11, 0xd7, 0x23, 0x00, 0x50, 0xc9, 0x19, 0x00, 0x00, 0xb9, 0xab, 0x08, 0x31, 0xe8, 0x67, 0xfe, 0x3b, 0x01, 0x12, 0xc8, 0x2d, 0x00, 0x90, 0x89, 0x1e, 0x00, 0x00, 0x8b, 0xd8, 0xe8, 0xe2, 0x00, 0x42, 0x00, 0x70, 0xc8, 0x8d, 0x53, 0x05, 0xff, 0x15, 0x96, 0x1a, 0x00, 0x01, 0xb3, 0x02, 0x11, 0x9c, 0x3b, 0x00, 0x44, 0x8e, 0x19, 0x00, 0x00, 0xbe, 0x01, 0x53, 0x48, 0x8d, 0x15, 0x87, 0x1d, 0x21, 0x03, 0x10, 0x66, 0x09, 0x00, 0x00, 0x21, 0x03, 0x20, 0x45, 0x1d, 0x3d, 0x00, 0x30, 0x0d, 0x76, 0x19, 0x00, 0x05, 0x02, 0x49, 0x00, 0x00, 0x00, 0x05, 0x01, 0x3d, 0x00, 0x20, 0x51, 0x19, 0x2e, 0x01, 0x20, 0xe9, 0x82, 0x75, 0x00, 0x30, 0x8d, 0x15, 0x03, 0x33, 0x00, 0x14, 0xd4, 0xef, 0x01, 0x04, 0x70, 0x05, 0xf2, 0x22, 0x8b, 0xd9, 0xff, 0x15, 0x71, 0x19, 0x00, 0x00, 0x84, 0xc0, 0x75, 0x0a, 0x48, 0x8b, 0x0b, 0xff, 0x15, 0x3c, 0x19, 0x00, 0x00, 0x90, 0x48, 0x8b, 0x13, 0x48, 0x8b, 0x02, 0x48, 0x63, 0x48, 0x04, 0x48, 0x8b, 0x4c, 0x11, 0x48, 0x48, 0x85, 0xc9, 0x74, 0x07, 0x48, 0x8b, 0x01, 0xff, 0x50, 0x10, 0x90, 0x4a, 0x02, 0x04, 0xe0, 0x02, 0x1f, 0x11, 0x27, 0x00, 0x08, 0x19, 0x28, 0xc3, 0x06, 0xf0, 0x02, 0x48, 0x89, 0x5c, 0x24, 0x10, 0x48, 0x89, 0x4c, 0x24, 0x08, 0x56, 0x57, 0x41, 0x54, 0x41, 0x56, 0x41, 0x6c, 0x07, 0xf0, 0x07, 0x30, 0x4c, 0x8b, 0xe2, 0x48, 0x8b, 0xf1, 0x33, 0xdb, 0x89, 0x5c, 0x24, 0x70, 0x49, 0xc7, 0xc6, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x1f, 0x16, 0x09, 0xd2, 0x00, 0x49, 0xff, 0xc6, 0x42, 0x38, 0x1c, 0x32, 0x75, 0xf7, 0x48, 0x8b, 0x01, 0x89, 0x00, 0xf0, 0x08, 0x7c, 0x31, 0x28, 0x48, 0x85, 0xff, 0x7e, 0x0a, 0x49, 0x3b, 0xfe, 0x7e, 0x05, 0x49, 0x2b, 0xfe, 0xeb, 0x02, 0x33, 0xff, 0x4c, 0x8b, 0xfe, 0x6a, 0x05, 0x57, 0x20, 0x48, 0x8b, 0x4c, 0x31, 0xa7, 0x00, 0xf0, 0x1b, 0x08, 0x90, 0x48, 0x8b, 0x0e, 0x48, 0x63, 0x41, 0x04, 0x83, 0x7c, 0x30, 0x10, 0x00, 0x74, 0x04, 0x32, 0xc0, 0xeb, 0x2b, 0x48, 0x8b, 0x44, 0x30, 0x50, 0x48, 0x85, 0xc0, 0x74, 0x1f, 0x48, 0x3b, 0xc6, 0x74, 0x1a, 0x48, 0x8b, 0xc8, 0xff, 0x15, 0x47, 0x18, 0x3e, 0x01, 0x06, 0x2a, 0x00, 0xb0, 0x0f, 0x94, 0xc0, 0xeb, 0x02, 0xb0, 0x01, 0x88, 0x44, 0x24, 0x28, 0x11, 0x01, 0x10, 0xbb, 0xb5, 0x01, 0x20, 0xe9, 0xaf, 0x57, 0x01, 0xa1, 0x63, 0x41, 0x04, 0x8b, 0x4c, 0x30, 0x18, 0x81, 0xe1, 0xc0, 0xaf, 0x02, 0x30, 0x40, 0x74, 0x2b, 0x8c, 0x00, 0x40, 0x26, 0x48, 0x8b, 0x06, 0x9d, 0x00, 0x51, 0x0f, 0xb6, 0x54, 0x31, 0x58, 0x84, 0x00, 0xf3, 0x06, 0xff, 0x15, 0x03, 0x18, 0x00, 0x00, 0x83, 0xf8, 0xff, 0x75, 0x05, 0x8d, 0x58, 0x05, 0xeb, 0x52, 0x48, 0xff, 0xcf, 0xeb, 0xd5, 0x26, 0x00, 0x63, 0x4d, 0x8b, 0xc6, 0x49, 0x8b, 0xd4, 0x27, 0x00, 0x93, 0xf4, 0x17, 0x00, 0x00, 0x49, 0x3b, 0xc6, 0x75, 0x2d, 0xf0, 0x00, 0x00, 0x4f, 0x00, 0x13, 0x28, 0x29, 0x00, 0x08, 0x4f, 0x00, 0x21, 0xb4, 0x17, 0x4f, 0x00, 0x20, 0x74, 0x05, 0x4a, 0x00, 0x40, 0xda, 0x83, 0xcb, 0x04, 0x2b, 0x01, 0x03, 0x28, 0x00, 0x50, 0x48, 0xc7, 0x44, 0x31, 0x28, 0x5e, 0x03, 0xe0, 0xeb, 0x0e, 0x48, 0x8b, 0x74, 0x24, 0x60, 0x8b, 0x5c, 0x24, 0x70, 0x4c, 0x8b, 0x7c, 0x1e, 0x06, 0x01, 0x97, 0x00, 0xf2, 0x04, 0x48, 0x03, 0xce, 0x45, 0x33, 0xc0, 0x8b, 0xd3, 0xff, 0x15, 0x4e, 0x17, 0x00, 0x00, 0x90, 0xff, 0x15, 0x87, 0x17, 0xea, 0x01, 0x60, 0x49, 0x8b, 0xcf, 0xff, 0x15, 0x52, 0x14, 0x00, 0x30, 0x49, 0x8b, 0x07, 0xc1, 0x00, 0x47, 0x4a, 0x8b, 0x4c, 0x39, 0x40, 0x01, 0x50, 0x10, 0x90, 0x48, 0x8b, 0xc6, 0xdd, 0x08, 0xd9, 0x68, 0x48, 0x83, 0xc4, 0x30, 0x41, 0x5f, 0x41, 0x5e, 0x41, 0x5c, 0x5f, 0x5e, 0xd0, 0x01, 0x04, 0x30, 0x04, 0x60, 0x01, 0x48, 0x8b, 0xd9, 0xb2, 0x0a, 0x44, 0x00, 0xf0, 0x04, 0x48, 0x03, 0xcb, 0xff, 0x15, 0x15, 0x17, 0x00, 0x00, 0x0f, 0xb6, 0xd0, 0x48, 0x8b, 0xcb, 0xff, 0x15, 0x01, 0x17, 0x5e, 0x01, 0x50, 0xcb, 0xff, 0x15, 0xe0, 0x16, 0x09, 0x00, 0x0f, 0x76, 0x04, 0x01, 0x06, 0x20, 0x04, 0xf0, 0x0d, 0x48, 0x3b, 0x0d, 0x21, 0x36, 0x00, 0x00, 0x75, 0x10, 0x48, 0xc1, 0xc1, 0x10, 0x66, 0xf7, 0xc1, 0xff, 0xff, 0x75, 0x01, 0xc3, 0x48, 0xc1, 0xc9, 0x10, 0xe9, 0x1a, 0x03, 0x52, 0x05, 0x04, 0x70, 0x00, 0xf0, 0x00, 0xd9, 0xeb, 0x0f, 0x48, 0x8b, 0xcb, 0xe8, 0xad, 0x0c, 0x00, 0x00, 0x85, 0xc0, 0x74, 0x13, 0x0c, 0x00, 0x20, 0xa7, 0x0c, 0x3f, 0x01, 0x32, 0xc0, 0x74, 0xe7, 0x9c, 0x02, 0xe0, 0x48, 0x83, 0xfb, 0xff, 0x74, 0x06, 0xe8, 0x4b, 0x04, 0x00, 0x00, 0xcc, 0xe8, 0x65, 0x06, 0x00, 0x20, 0xe9, 0x7f, 0x06, 0x00, 0x05, 0x44, 0x00, 0x31, 0x8d, 0x05, 0x97, 0xf8, 0x01, 0x06, 0x64, 0x08, 0x00, 0x57, 0x07, 0x30, 0xe8, 0xd6, 0xff, 0x9d, 0x03, 0x04, 0xa6, 0x00, 0x02, 0x70, 0x00, 0x10, 0xb9, 0xc5, 0x04, 0xe0, 0xe8, 0x52, 0x0c, 0x00, 0x00, 0xe8, 0x4b, 0x07, 0x00, 0x00, 0x8b, 0xc8, 0xe8, 0x7c, 0x0c, 0x00, 0x10, 0x33, 0x0c, 0x00, 0x61, 0xd8, 0xe8, 0xa0, 0x0c, 0x00, 0x00, 0x22, 0x00, 0x50, 0x89, 0x18, 0xe8, 0xac, 0x04, 0x5b, 0x01, 0x22, 0x74, 0x73, 0x18, 0x0a, 0xd0, 0x8d, 0x0d, 0xdc, 0x09, 0x00, 0x00, 0xe8, 0x47, 0x06, 0x00, 0x00, 0xe8, 0x0a, 0x2d, 0x00, 0x31, 0xc8, 0xe8, 0x19, 0xb2, 0x00, 0x21, 0x75, 0x52, 0x10, 0x00, 0x31, 0xe8, 0x49, 0x07, 0xc0, 0x00, 0x50, 0x0c, 0x48, 0x8d, 0x0d, 0xe6, 0x25, 0x00, 0xa0, 0xf5, 0x0b, 0x00, 0x00, 0xe8, 0x04, 0x07, 0x00, 0x00, 0xe8, 0x5c, 0x08, 0x31, 0xe8, 0xd2, 0x06, 0x6d, 0x00, 0x10, 0x33, 0x6d, 0x00, 0x21, 0xea, 0x06, 0x5a, 0x00, 0x30, 0x05, 0xe8, 0xdd, 0x24, 0x00, 0x10, 0xb8, 0x2e, 0x00, 0x20, 0x7b, 0x08, 0x3e, 0x00, 0x22, 0x75, 0x06, 0xf1, 0x00, 0x10, 0xb9, 0xc3, 0x0c, 0x30, 0xe8, 0x1b, 0x07, 0x27, 0x01, 0x01, 0x08, 0x07, 0x33, 0xe8, 0xcf, 0x06, 0x37, 0x04, 0x20, 0x28, 0xc3, 0x88, 0x06, 0x71, 0xe8, 0xa7, 0x08, 0x00, 0x00, 0xe8, 0x7e, 0x54, 0x00, 0x00, 0x6e, 0x09, 0x32, 0xe9, 0xe1, 0x0b, 0x2c, 0x00, 0x00, 0xf4, 0x0a, 0x00, 0x40, 0x03, 0x11, 0x10, 0x8d, 0x03, 0x01, 0xcb, 0x00, 0x30, 0xe8, 0x97, 0x03, 0x6f, 0x00, 0xf3, 0x0a, 0x0f, 0x84, 0x36, 0x01, 0x00, 0x00, 0x40, 0x32, 0xf6, 0x40, 0x88, 0x74, 0x24, 0x20, 0xe8, 0x46, 0x03, 0x00, 0x00, 0x8a, 0xd8, 0x8b, 0x0d, 0x46, 0x3b, 0xad, 0x05, 0xe0, 0x23, 0x01, 0x00, 0x00, 0x85, 0xc9, 0x75, 0x4a, 0xc7, 0x05, 0x2f, 0x3b, 0x00, 0x00, 0x26, 0x01, 0x40, 0x48, 0x8d, 0x15, 0x10, 0xf1, 0x01, 0x90, 0x8d, 0x0d, 0xf1, 0x16, 0x00, 0x00, 0xe8, 0x42, 0x0b, 0xa3, 0x00, 0x90, 0x74, 0x0a, 0xb8, 0xff, 0x00, 0x00, 0x00, 0xe9, 0xd9, 0x44, 0x03, 0x30, 0x8d, 0x15, 0xcf, 0x09, 0x02, 0x30, 0x8d, 0x0d, 0xb8, 0x21, 0x00, 0x82, 0x1b, 0x0b, 0x00, 0x00, 0xc7, 0x05, 0xf1, 0x3a, 0x7a, 0x0d, 0x51, 0xeb, 0x08, 0x40, 0xb6, 0x01, 0x6c, 0x00, 0x91, 0x8a, 0xcb, 0xe8, 0x84, 0x04, 0x00, 0x00, 0xe8, 0x33, 0x61, 0x08, 0xc1, 0xd8, 0x48, 0x83, 0x38, 0x00, 0x74, 0x1e, 0x48, 0x8b, 0xc8, 0xe8, 0xd6, 0x99, 0x00, 0xf0, 0x02, 0x74, 0x12, 0x45, 0x33, 0xc0, 0x41, 0x8d, 0x50, 0x02, 0x33, 0xc9, 0x48, 0x8b, 0x03, 0xff, 0x15, 0x44, 0x4c, 0x00, 0x17, 0x0f, 0x2c, 0x00, 0x10, 0x14, 0x2c, 0x00, 0x12, 0xaa, 0x2c, 0x00, 0xc0, 0x08, 0x48, 0x8b, 0x0b, 0xe8, 0xe8, 0x0a, 0x00, 0x00, 0xe8, 0xa7, 0x0a, 0x83, 0x02, 0x31, 0xf8, 0xe8, 0xc9, 0x08, 0x00, 0xf0, 0x09, 0x18, 0xe8, 0xbb, 0x0a, 0x00, 0x00, 0x4c, 0x8b, 0xc7, 0x48, 0x8b, 0xd3, 0x8b, 0x08, 0xe8, 0x00, 0xf9, 0xff, 0xff, 0x8b, 0xd8, 0xe8, 0x2d, 0x07, 0xfa, 0x00, 0xf2, 0x0e, 0x74, 0x55, 0x40, 0x84, 0xf6, 0x75, 0x05, 0xe8, 0xa5, 0x0a, 0x00, 0x00, 0x33, 0xd2, 0xb1, 0x01, 0xe8, 0x1a, 0x04, 0x00, 0x00, 0x8b, 0xc3, 0xeb, 0x19, 0x8b, 0xd8, 0xe8, 0x0b, 0x22, 0x00, 0xb0, 0x3b, 0x80, 0x7c, 0x24, 0x20, 0x00, 0x75, 0x05, 0xe8, 0x87, 0x0a, 0x1b, 0x00, 0x00, 0x29, 0x03, 0x10, 0x40, 0x81, 0x03, 0x10, 0x48, 0x2e, 0x03, 0x13, 0x5f, 0x90, 0x01, 0x52, 0x8b, 0x05, 0x00, 0x00, 0x90, 0x0b, 0x00, 0xf0, 0x01, 0x80, 0x05, 0x00, 0x00, 0x8b, 0xcb, 0xe8, 0x35, 0x0a, 0x00, 0x00, 0x90, 0x8b, 0xcb, 0xe8, 0x33, 0x08, 0x00, 0x01, 0x98, 0x01, 0x13, 0x3f, 0xc2, 0x07, 0x55, 0xe9, 0x72, 0xfe, 0xff, 0xff, 0xa0, 0x02, 0x80, 0x8b, 0xd9, 0x33, 0xc9, 0xff, 0x15, 0x5b, 0x13, 0xb2, 0x00, 0x50, 0xcb, 0xff, 0x15, 0x5a, 0x13, 0x68, 0x06, 0x11, 0x44, 0x0f, 0x00, 0x52, 0xc8, 0xba, 0x09, 0x04, 0x00, 0xce, 0x07, 0x40, 0x48, 0xff, 0x25, 0x28, 0x14, 0x00, 0x00, 0x53, 0x05, 0x60, 0x48, 0x83, 0xec, 0x38, 0xb9, 0x17, 0x1a, 0x11, 0x31, 0x15, 0x0c, 0x13, 0x78, 0x01, 0x20, 0x07, 0xb9, 0x4c, 0x0f, 0xb0, 0xcd, 0x29, 0x48, 0x8d, 0x0d, 0xc2, 0x34, 0x00, 0x00, 0xe8, 0xa9, 0x81, 0x01, 0x90, 0x8b, 0x44, 0x24, 0x38, 0x48, 0x89, 0x05, 0xa9, 0x35, 0xae, 0x01, 0x00, 0x0c, 0x00, 0x70, 0x83, 0xc0, 0x08, 0x48, 0x89, 0x05, 0x39, 0x10, 0x00, 0x30, 0x8b, 0x05, 0x92, 0x07, 0x00, 0x40, 0x89, 0x05, 0x03, 0x34, 0x78, 0x00, 0x80, 0x44, 0x24, 0x40, 0x48, 0x89, 0x05, 0x07, 0x35, 0xa4, 0x01, 0x40, 0xdd, 0x33, 0x00, 0x00, 0x77, 0x00, 0x42, 0xc7, 0x05, 0xd7, 0x33, 0xec, 0x01, 0x33, 0xc7, 0x05, 0xe1, 0x0a, 0x00, 0x11, 0xb8, 0xed, 0x0a, 0x20, 0x6b, 0xc0, 0xd7, 0x01, 0x80, 0xd9, 0x33, 0x00, 0x00, 0x48, 0xc7, 0x04, 0x01, 0x7a, 0x00, 0x06, 0x18, 0x00, 0x40, 0x8b, 0x0d, 0x49, 0x32, 0x10, 0x0b, 0x34, 0x4c, 0x04, 0x20, 0x15, 0x00, 0x00, 0x07, 0x0a, 0x14, 0x2c, 0x15, 0x00, 0x50, 0x48, 0x8d, 0x0d, 0x10, 0x15, 0xf8, 0x02, 0x80, 0xfe, 0xff, 0xff, 0x48, 0x83, 0xc4, 0x38, 0xc3, 0x08, 0x01, 0x10, 0x56, 0x91, 0x02, 0x11, 0x40, 0x9e, 0x06, 0x20, 0x03, 0x12, 0x90, 0x00, 0x10, 0xb3, 0xc4, 0x11, 0xf0, 0x01, 0x33, 0xff, 0x45, 0x33, 0xc0, 0x48, 0x8d, 0x54, 0x24, 0x60, 0x48, 0x8b, 0xce, 0xff, 0x15, 0x51, 0x1a, 0x00, 0xf0, 0x08, 0x85, 0xc0, 0x74, 0x39, 0x48, 0x83, 0x64, 0x24, 0x38, 0x00, 0x48, 0x8d, 0x4c, 0x24, 0x68, 0x48, 0x8b, 0x54, 0x24, 0x60, 0x4c, 0x8b, 0xc8, 0x6a, 0x06, 0x40, 0x30, 0x4c, 0x8b, 0xc6, 0x15, 0x00, 0x10, 0x70, 0x0d, 0x00, 0x30, 0x28, 0x33, 0xc9, 0x83, 0x06, 0xe0, 0x20, 0xff, 0x15, 0x12, 0x12, 0x00, 0x00, 0xff, 0xc7, 0x83, 0xff, 0x02, 0x7c, 0xb1, 0xbc, 0x0a, 0x30, 0x5f, 0x5e, 0x5b, 0xd9, 0x04, 0x40, 0x48, 0x83, 0x61, 0x10, 0x3b, 0x0d, 0x10, 0xc4, 0x4e, 0x0b, 0x02, 0x8d, 0x0d, 0x11, 0xa9, 0x0b, 0x00, 0x03, 0x8d, 0x0d, 0x40, 0x48, 0x83, 0xec, 0x48, 0x4d, 0x00, 0x30, 0x20, 0xe8, 0xd2, 0x28, 0x04, 0x30, 0x8d, 0x15, 0xf3, 0xa3, 0x0b, 0x01, 0x11, 0x00, 0x40, 0x09, 0x08, 0x00, 0x00, 0x78, 0x03, 0x03, 0x20, 0x00, 0x21, 0x22, 0xf2, 0x20, 0x00, 0x10, 0xa3, 0xc8, 0x0c, 0x01, 0x20, 0x00, 0x10, 0xe9, 0x9a, 0x03, 0x20, 0xe9, 0x79, 0x26, 0x00, 0x01, 0x48, 0x00, 0x32, 0x28, 0xe8, 0xa7, 0xfe, 0x03, 0x60, 0x21, 0x65, 0x48, 0x8b, 0x04, 0x25, 0xc1, 0x11, 0xf0, 0x0a, 0x48, 0x8b, 0x48, 0x08, 0xeb, 0x05, 0x48, 0x3b, 0xc8, 0x74, 0x14, 0x33, 0xc0, 0xf0, 0x48, 0x0f, 0xb1, 0x0d, 0xe4, 0x37, 0x00, 0x00, 0x75, 0xee, 0x32, 0xec, 0x01, 0x65, 0x28, 0xc3, 0xb0, 0x01, 0xeb, 0xf7, 0xb4, 0x07, 0x50, 0x0f, 0xb6, 0x05, 0xcf, 0x37, 0x7e, 0x03, 0x10, 0xbb, 0x77, 0x03, 0xf0, 0x02, 0x0f, 0x44, 0xc3, 0x88, 0x05, 0xbf, 0x37, 0x00, 0x00, 0xe8, 0xa6, 0x05, 0x00, 0x00, 0xe8, 0xbd, 0x02, 0xc4, 0x02, 0x10, 0x75, 0xf2, 0x06, 0x32, 0x14, 0xe8, 0xb0, 0x0d, 0x00, 0xb4, 0x09, 0x33, 0xc9, 0xe8, 0xa5, 0x02, 0x00, 0x00, 0xeb, 0xea, 0x8a, 0xde, 0x04, 0x00, 0x64, 0x01, 0xf2, 0x07, 0x48, 0x83, 0xec, 0x20, 0x80, 0x3d, 0x84, 0x37, 0x00, 0x00, 0x00, 0x8b, 0xd9, 0x75, 0x67, 0x83, 0xf9, 0x01, 0x77, 0x6a, 0xe8, 0x0d, 0x9a, 0x00, 0x90, 0x28, 0x85, 0xdb, 0x75, 0x24, 0x48, 0x8d, 0x0d, 0x6e, 0x59, 0x00, 0x11, 0xc5, 0x14, 0x00, 0x60, 0x75, 0x10, 0x48, 0x8d, 0x0d, 0x76, 0x10, 0x00, 0x11, 0xb5, 0x10, 0x00, 0xb0, 0x74, 0x2e, 0x32, 0xc0, 0xeb, 0x33, 0x66, 0x0f, 0x6f, 0x05, 0xc1, 0x87, 0x02, 0x90, 0x83, 0xc8, 0xff, 0xf3, 0x0f, 0x7f, 0x05, 0x3d, 0x37, 0xec, 0x01, 0x50, 0x05, 0x46, 0x37, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x08, 0x00, 0xf3, 0x01, 0x48, 0x89, 0x05, 0x4f, 0x37, 0x00, 0x00, 0xc6, 0x05, 0x19, 0x37, 0x00, 0x00, 0x01, 0xb0, 0x01, 0xb5, 0x04, 0x10, 0x05, 0xd8, 0x0d, 0x20, 0x66, 0x02, 0x89, 0x04, 0xf1, 0x0b, 0x48, 0x83, 0xec, 0x18, 0x4c, 0x8b, 0xc1, 0xb8, 0x4d, 0x5a, 0x00, 0x00, 0x66, 0x39, 0x05, 0x11, 0xe0, 0xff, 0xff, 0x75, 0x78, 0x48, 0x63, 0x0d, 0x44, 0xe0, 0x4a, 0x01, 0x10, 0x01, 0x07, 0x00, 0xd0, 0x03, 0xca, 0x81, 0x39, 0x50, 0x45, 0x00, 0x00, 0x75, 0x5f, 0xb8, 0x0b, 0x02, 0x27, 0x00, 0xf2, 0x35, 0x41, 0x18, 0x75, 0x54, 0x4c, 0x2b, 0xc2, 0x0f, 0xb7, 0x51, 0x14, 0x48, 0x83, 0xc2, 0x18, 0x48, 0x03, 0xd1, 0x0f, 0xb7, 0x41, 0x06, 0x48, 0x8d, 0x0c, 0x80, 0x4c, 0x8d, 0x0c, 0xca, 0x48, 0x89, 0x14, 0x24, 0x49, 0x3b, 0xd1, 0x74, 0x18, 0x8b, 0x4a, 0x0c, 0x4c, 0x3b, 0xc1, 0x72, 0x0a, 0x8b, 0x42, 0x08, 0x03, 0xc1, 0x4c, 0x3b, 0xc0, 0x72, 0x08, 0x48, 0x83, 0xc2, 0x28, 0xeb, 0xdf, 0x33, 0xd2, 0x48, 0x85, 0xd2, 0x28, 0x01, 0x50, 0x83, 0x7a, 0x24, 0x00, 0x7d, 0x32, 0x01, 0x91, 0x0a, 0xb0, 0x01, 0xeb, 0x06, 0x32, 0xc0, 0xeb, 0x02, 0x77, 0x01, 0x22, 0x18, 0xc3, 0x24, 0x01, 0x50, 0x8a, 0xd9, 0xe8, 0xf7, 0x05, 0x0c, 0x04, 0xd1, 0x85, 0xc0, 0x74, 0x0b, 0x84, 0xdb, 0x75, 0x07, 0x48, 0x87, 0x15, 0x46, 0x36, 0xf1, 0x0b, 0x23, 0x20, 0x5b, 0x24, 0x00, 0xf0, 0x01, 0x80, 0x3d, 0x3b, 0x36, 0x00, 0x00, 0x00, 0x8a, 0xd9, 0x74, 0x04, 0x84, 0xd2, 0x75, 0x0c, 0xe8, 0x3d, 0x05, 0x74, 0x8a, 0xcb, 0xe8, 0x2f, 0x01, 0x00, 0x00, 0xf1, 0x00, 0x05, 0xc0, 0x01, 0xf0, 0x03, 0x48, 0x83, 0x3d, 0x16, 0x36, 0x00, 0x00, 0xff, 0x48, 0x8b, 0xd9, 0x75, 0x07, 0xe8, 0x74, 0x06, 0x00, 0x00, 0xd3, 0x06, 0xa0, 0xd3, 0x48, 0x8d, 0x0d, 0x00, 0x36, 0x00, 0x00, 0xe8, 0x5d, 0xbc, 0x05, 0x70, 0xd2, 0x85, 0xc0, 0x48, 0x0f, 0x44, 0xd3, 0xec, 0x10, 0x01, 0x66, 0x00, 0x01, 0x24, 0x01, 0x30, 0x28, 0xe8, 0xbb, 0x7b, 0x02, 0x71, 0xf7, 0xd8, 0x1b, 0xc0, 0xf7, 0xd8, 0xff, 0x02, 0x0d, 0x11, 0xc3, 0xc4, 0x05, 0x50, 0x20, 0x55, 0x48, 0x8b, 0xec, 0xcf, 0x01, 0xf0, 0x00, 0x48, 0x8b, 0x05, 0xdc, 0x2e, 0x00, 0x00, 0x48, 0xbb, 0x32, 0xa2, 0xdf, 0x2d, 0x99, 0x2b, 0x56, 0x07, 0xf0, 0x01, 0xc3, 0x75, 0x74, 0x48, 0x83, 0x65, 0x18, 0x00, 0x48, 0x8d, 0x4d, 0x18, 0xff, 0x15, 0xce, 0x0e, 0x6c, 0x02, 0xf0, 0x01, 0x45, 0x18, 0x48, 0x89, 0x45, 0x10, 0xff, 0x15, 0xc8, 0x0e, 0x00, 0x00, 0x8b, 0xc0, 0x48, 0x31, 0x0c, 0x00, 0x12, 0xc4, 0x0c, 0x00, 0x32, 0x8d, 0x4d, 0x20, 0x10, 0x00, 0x10, 0xbc, 0x10, 0x00, 0xd0, 0x45, 0x20, 0x48, 0x8d, 0x4d, 0x10, 0x48, 0xc1, 0xe0, 0x20, 0x48, 0x33, 0x45, 0x04, 0x00, 0x60, 0x10, 0x48, 0x33, 0xc1, 0x48, 0xb9, 0xa7, 0x09, 0x00, 0x84, 0x15, 0x66, 0x48, 0x23, 0xc1, 0x48, 0xb9, 0x33, 0x6b, 0x00, 0x80, 0x48, 0x0f, 0x44, 0xc1, 0x48, 0x89, 0x05, 0x59, 0x83, 0x00, 0xb0, 0x8b, 0x5c, 0x24, 0x48, 0x48, 0xf7, 0xd0, 0x48, 0x89, 0x05, 0x42, 0x0f, 0x00, 0xa0, 0x83, 0xc4, 0x20, 0x5d, 0xc3, 0x33, 0xc0, 0xc3, 0xcc, 0xb8, 0xb5, 0x02, 0x61, 0xc3, 0xcc, 0xcc, 0xb8, 0x00, 0x40, 0x08, 0x00, 0x40, 0x48, 0x8d, 0x0d, 0x41, 0x79, 0x04, 0x40, 0xff, 0x25, 0x8a, 0x0e, 0x0c, 0x02, 0x50, 0xb0, 0x01, 0xc3, 0xcc, 0xc2, 0x50, 0x03, 0x11, 0x8d, 0x98, 0x04, 0x15, 0xc3, 0x08, 0x00, 0x00, 0x00, 0x01, 0xa2, 0xe7, 0xff, 0xff, 0xff, 0x48, 0x83, 0x08, 0x24, 0xe8, 0xe6, 0x09, 0x00, 0x10, 0x02, 0xce, 0x06, 0xd0, 0xc3, 0xcc, 0x33, 0xc0, 0x39, 0x05, 0xf0, 0x2d, 0x00, 0x00, 0x0f, 0x94, 0xc0, 0x30, 0x00, 0x13, 0x21, 0x38, 0x00, 0x10, 0x11, 0x08, 0x00, 0x50, 0x83, 0x25, 0x01, 0x35, 0x00, 0x10, 0x00, 0x00, 0xec, 0x06, 0xd0, 0x55, 0x48, 0x8d, 0xac, 0x24, 0x40, 0xfb, 0xff, 0xff, 0x48, 0x81, 0xec, 0xc0, 0x95, 0x05, 0x13, 0xd9, 0x36, 0x05, 0x21, 0xd6, 0x0d, 0x36, 0x05, 0x60, 0x04, 0x8b, 0xcb, 0xcd, 0x29, 0xb9, 0x67, 0x16, 0xe0, 0xe8, 0xc4, 0xff, 0xff, 0xff, 0x33, 0xd2, 0x48, 0x8d, 0x4d, 0xf0, 0x41, 0xb8, 0xd0, 0x90, 0x06, 0x10, 0x35, 0xac, 0x05, 0x70, 0x8d, 0x4d, 0xf0, 0xff, 0x15, 0x71, 0x0d, 0x45, 0x01, 0x20, 0x9d, 0xe8, 0x53, 0x05, 0x30, 0x8d, 0x95, 0xd8, 0x18, 0x00, 0x80, 0x8b, 0xcb, 0x45, 0x33, 0xc0, 0xff, 0x15, 0xbf, 0x1a, 0x00, 0x44, 0x85, 0xc0, 0x74, 0x3c, 0x92, 0x04, 0x21, 0x8d, 0xe0, 0x1e, 0x00, 0x01, 0x25, 0x00, 0x06, 0x96, 0x04, 0x50, 0xc3, 0x48, 0x8d, 0x8d, 0xe8, 0x19, 0x00, 0x40, 0x89, 0x4c, 0x24, 0x28, 0x63, 0x00, 0x00, 0xa1, 0x04, 0x60, 0x20, 0x33, 0xc9, 0xff, 0x15, 0x76, 0x41, 0x00, 0x30, 0x8b, 0x85, 0xc8, 0x1d, 0x00, 0x70, 0x8d, 0x4c, 0x24, 0x50, 0x48, 0x89, 0x85, 0x67, 0x00, 0x00, 0x89, 0x00, 0x01, 0x15, 0x00, 0x30, 0x41, 0xb8, 0x98, 0x76, 0x00, 0x01, 0xb8, 0x05, 0x20, 0x85, 0x88, 0x42, 0x03, 0x11, 0x9e, 0x85, 0x0e, 0x02, 0x32, 0x00, 0xf0, 0x01, 0x89, 0x44, 0x24, 0x60, 0xc7, 0x44, 0x24, 0x50, 0x15, 0x00, 0x00, 0x40, 0xc7, 0x44, 0x24, 0x54, 0x6b, 0x01, 0x40, 0xff, 0x15, 0xca, 0x0c, 0x42, 0x0a, 0x10, 0x01, 0xf2, 0x05, 0x10, 0x50, 0x7e, 0x10, 0x80, 0x40, 0x48, 0x8d, 0x45, 0xf0, 0x0f, 0x94, 0xc3, 0x0c, 0x00, 0x10, 0x48, 0x75, 0x00, 0x10, 0xf1, 0x40, 0x09, 0x80, 0x8d, 0x4c, 0x24, 0x40, 0xff, 0x15, 0xee, 0x0c, 0x08, 0x01, 0xb0, 0x75, 0x0c, 0x84, 0xdb, 0x75, 0x08, 0x8d, 0x48, 0x03, 0xe8, 0xbe, 0x95, 0x05, 0x40, 0x8b, 0x9c, 0x24, 0xd0, 0x11, 0x0f, 0xb1, 0x81, 0xc4, 0xc0, 0x05, 0x00, 0x00, 0x5d, 0xc3, 0xcc, 0xe9, 0x33, 0xaf, 0x06, 0x10, 0xcc, 0xc4, 0x06, 0x00, 0x41, 0x00, 0x10, 0x70, 0x41, 0x00, 0x00, 0x89, 0x05, 0x12, 0xb9, 0xc2, 0x03, 0xa3, 0x08, 0x75, 0x2f, 0x48, 0x63, 0x48, 0x3c, 0x48, 0x03, 0xc8, 0xb4, 0x03, 0x16, 0x20, 0xb4, 0x03, 0x30, 0x15, 0x83, 0xb9, 0x11, 0x0e, 0x50, 0x0e, 0x76, 0x0c, 0x83, 0xb9, 0xd4, 0x05, 0x34, 0x00, 0x0f, 0x95, 0x71, 0x03, 0x01, 0x31, 0x0c, 0x40, 0x48, 0x8d, 0x0d, 0x09, 0xe3, 0x00, 0x40, 0xff, 0x25, 0x5a, 0x0c, 0x10, 0x02, 0x00, 0xb5, 0x05, 0x03, 0x98, 0x13, 0xf0, 0x1a, 0x8b, 0x19, 0x48, 0x8b, 0xf9, 0x81, 0x3b, 0x63, 0x73, 0x6d, 0xe0, 0x75, 0x1c, 0x83, 0x7b, 0x18, 0x04, 0x75, 0x16, 0x8b, 0x53, 0x20, 0x8d, 0x82, 0xe0, 0xfa, 0x6c, 0xe6, 0x83, 0xf8, 0x02, 0x76, 0x15, 0x81, 0xfa, 0x00, 0x40, 0x99, 0x01, 0x74, 0x0d, 0x91, 0x07, 0x12, 0x30, 0x37, 0x0d, 0x40, 0x5f, 0xc3, 0xe8, 0x70, 0x9d, 0x0e, 0x82, 0x89, 0x18, 0x48, 0x8b, 0x5f, 0x08, 0xe8, 0x6a, 0x0c, 0x00, 0x21, 0xe8, 0x04, 0x54, 0x0a, 0x07, 0x5c, 0x00, 0x22, 0x8d, 0x1d, 0xf8, 0x0e, 0xa0, 0x3d, 0x24, 0x1b, 0x00, 0x00, 0xeb, 0x12, 0x48, 0x8b, 0x03, 0x52, 0x0a, 0x40, 0x06, 0xff, 0x15, 0xe4, 0x92, 0x01, 0x81, 0x83, 0xc3, 0x08, 0x48, 0x3b, 0xdf, 0x72, 0xe9, 0x59, 0x00, 0x00, 0xc0, 0x04, 0x12, 0x5f, 0x50, 0x02, 0x00, 0xa6, 0x06, 0x00, 0x3c, 0x00, 0x20, 0xff, 0x1a, 0x52, 0x07, 0x3a, 0x3d, 0xf8, 0x1a, 0x3c, 0x00, 0x1f, 0xa8, 0x3c, 0x00, 0x08, 0x10, 0x10, 0x78, 0x09, 0x10, 0x18, 0x41, 0x00, 0xf0, 0x32, 0x10, 0x33, 0xc0, 0x33, 0xc9, 0x0f, 0xa2, 0x44, 0x8b, 0xc1, 0x45, 0x33, 0xdb, 0x44, 0x8b, 0xd2, 0x41, 0x81, 0xf0, 0x6e, 0x74, 0x65, 0x6c, 0x41, 0x81, 0xf2, 0x69, 0x6e, 0x65, 0x49, 0x44, 0x8b, 0xcb, 0x8b, 0xf0, 0x33, 0xc9, 0x41, 0x8d, 0x43, 0x01, 0x45, 0x0b, 0xd0, 0x0f, 0xa2, 0x41, 0x81, 0xf1, 0x47, 0x65, 0x6e, 0x75, 0x89, 0x04, 0x24, 0x45, 0x0b, 0xd1, 0x89, 0x5c, 0x24, 0x04, 0x8b, 0xf9, 0x02, 0x08, 0xf2, 0x09, 0x89, 0x54, 0x24, 0x0c, 0x75, 0x5b, 0x48, 0x83, 0x0d, 0xfb, 0x2a, 0x00, 0x00, 0xff, 0x25, 0xf0, 0x3f, 0xff, 0x0f, 0x48, 0xc7, 0x05, 0xe3, 0x2a, 0x65, 0x16, 0xf0, 0x01, 0x3d, 0xc0, 0x06, 0x01, 0x00, 0x74, 0x28, 0x3d, 0x60, 0x06, 0x02, 0x00, 0x74, 0x21, 0x3d, 0x70, 0x07, 0x00, 0xf0, 0x00, 0x1a, 0x05, 0xb0, 0xf9, 0xfc, 0xff, 0x83, 0xf8, 0x20, 0x77, 0x24, 0x48, 0xb9, 0x01, 0x00, 0x02, 0x00, 0xf0, 0x15, 0x00, 0x00, 0x48, 0x0f, 0xa3, 0xc1, 0x73, 0x14, 0x44, 0x8b, 0x05, 0xd1, 0x31, 0x00, 0x00, 0x41, 0x83, 0xc8, 0x01, 0x44, 0x89, 0x05, 0xc6, 0x31, 0x00, 0x00, 0xeb, 0x07, 0x44, 0x8b, 0x05, 0xbd, 0x31, 0x00, 0x00, 0xb8, 0x6c, 0x0a, 0x80, 0x44, 0x8d, 0x48, 0xfb, 0x3b, 0xf0, 0x7c, 0x26, 0xb7, 0x00, 0x50, 0x89, 0x04, 0x24, 0x44, 0x8b, 0xc0, 0x0d, 0x14, 0x04, 0x87, 0x00, 0xe0, 0x0f, 0xba, 0xe3, 0x09, 0x73, 0x0a, 0x45, 0x0b, 0xc1, 0x44, 0x89, 0x05, 0x8a, 0x31, 0x2d, 0x08, 0x22, 0x54, 0x2a, 0x37, 0x08, 0xe2, 0x44, 0x89, 0x0d, 0x51, 0x2a, 0x00, 0x00, 0x0f, 0xba, 0xe7, 0x14, 0x0f, 0x83, 0x91, 0x11, 0x00, 0x50, 0x3c, 0x2a, 0x00, 0x00, 0xbb, 0xa5, 0x18, 0x32, 0x89, 0x1d, 0x35, 0x1c, 0x00, 0xf1, 0x08, 0x1b, 0x73, 0x79, 0x0f, 0xba, 0xe7, 0x1c, 0x73, 0x73, 0x33, 0xc9, 0x0f, 0x01, 0xd0, 0x48, 0xc1, 0xe2, 0x20, 0x48, 0x0b, 0xd0, 0x48, 0x89, 0xf5, 0x12, 0xf0, 0x07, 0x44, 0x24, 0x20, 0x22, 0xc3, 0x3a, 0xc3, 0x75, 0x57, 0x8b, 0x05, 0x07, 0x2a, 0x00, 0x00, 0x83, 0xc8, 0x08, 0xc7, 0x05, 0xf6, 0x29, 0xca, 0x18, 0xf0, 0x08, 0x00, 0x00, 0x89, 0x05, 0xf4, 0x29, 0x00, 0x00, 0x41, 0xf6, 0xc3, 0x20, 0x74, 0x38, 0x83, 0xc8, 0x20, 0xc7, 0x05, 0xdd, 0x29, 0x00, 0x00, 0x66, 0x06, 0x41, 0x89, 0x05, 0xdb, 0x29, 0x31, 0x1a, 0xa1, 0x03, 0xd0, 0x44, 0x23, 0xd8, 0x44, 0x3b, 0xd8, 0x75, 0x18, 0x4a, 0x00, 0xf0, 0x02, 0x24, 0xe0, 0x3c, 0xe0, 0x75, 0x0d, 0x83, 0x0d, 0xbc, 0x29, 0x00, 0x00, 0x40, 0x89, 0x1d, 0xb2, 0x29, 0xd7, 0x03, 0x50, 0x5c, 0x24, 0x28, 0x33, 0xc0, 0xcd, 0x09, 0x70, 0x30, 0x48, 0x83, 0xc4, 0x10, 0x5f, 0xc3, 0x5c, 0x04, 0x21, 0xb0, 0x29, 0xa7, 0x02, 0x27, 0xc3, 0xcc, 0x01, 0x00, 0x40, 0xff, 0x25, 0xaa, 0x0a, 0x6a, 0x01, 0x11, 0x64, 0x06, 0x00, 0x11, 0x8e, 0x06, 0x00, 0x11, 0x50, 0x06, 0x00, 0x11, 0x42, 0x06, 0x00, 0x11, 0x34, 0x06, 0x00, 0x11, 0x26, 0x06, 0x00, 0x11, 0x68, 0x06, 0x00, 0x11, 0xa2, 0x06, 0x00, 0x11, 0x94, 0x06, 0x00, 0x20, 0x3e, 0x0b, 0x3c, 0x00, 0x11, 0x30, 0x06, 0x00, 0x02, 0x48, 0x00, 0x11, 0x14, 0x0c, 0x00, 0x11, 0x06, 0x06, 0x00, 0x11, 0xf8, 0x24, 0x00, 0x11, 0xea, 0x06, 0x00, 0x11, 0x2c, 0x12, 0x00, 0x11, 0xd6, 0x0c, 0x00, 0x11, 0xc8, 0x06, 0x00, 0x11, 0x32, 0x12, 0x00, 0x11, 0xac, 0x0c, 0x00, 0x11, 0x9e, 0x06, 0x00, 0x11, 0x00, 0x12, 0x00, 0x11, 0xf2, 0x0c, 0x00, 0x11, 0xd4, 0x06, 0x00, 0x11, 0x46, 0x06, 0x00, 0x11, 0x18, 0x06, 0x00, 0x11, 0xfa, 0x06, 0x00, 0x11, 0x14, 0x06, 0x00, 0x11, 0x66, 0x06, 0x00, 0x11, 0x58, 0x06, 0x00, 0x11, 0x4a, 0x06, 0x00, 0x11, 0x3c, 0x06, 0x00, 0x20, 0xbe, 0x09, 0x90, 0x00, 0x20, 0xa0, 0x09, 0x72, 0x03, 0x08, 0x02, 0x00, 0x06, 0xa0, 0x0d, 0x28, 0xff, 0xe0, 0x18, 0x00, 0x04, 0x02, 0x00, 0x07, 0x20, 0x00, 0x38, 0x25, 0xba, 0x0a, 0x3e, 0x00, 0x40, 0x48, 0x8d, 0x8a, 0x20, 0xfa, 0x0b, 0x20, 0xd4, 0xef, 0xc4, 0x07, 0x02, 0x0c, 0x00, 0x10, 0x88, 0x0c, 0x00, 0x40, 0x89, 0x54, 0x24, 0x10, 0x28, 0x15, 0x84, 0x20, 0x48, 0x8b, 0xea, 0x48, 0x8b, 0x55, 0x60, 0x42, 0x10, 0x60, 0x03, 0xca, 0x41, 0xb0, 0x01, 0xba, 0x72, 0x0f, 0x90, 0xff, 0x15, 0x9f, 0x08, 0x00, 0x00, 0x90, 0x48, 0xb8, 0xdb, 0x0e, 0x00, 0x02, 0x00, 0x00, 0x72, 0x03, 0x46, 0x5d, 0xc3, 0xcc, 0x40, 0x37, 0x00, 0xb1, 0x01, 0x48, 0x8b, 0xd1, 0x8b, 0x08, 0xe8, 0xb3, 0xfe, 0xff, 0xff, 0x92, 0x10, 0x02, 0x1e, 0x00, 0x00, 0x51, 0x00, 0xf0, 0x02, 0x01, 0x33, 0xc9, 0x81, 0x38, 0x05, 0x00, 0x00, 0xc0, 0x0f, 0x94, 0xc1, 0x8b, 0xc1, 0x5d, 0xc3, 0xcc, 0x41, 0x00, 0x0f, 0x02, 0x00, 0xff, 0xb2, 0x12, 0x4d, 0xc5, 0x01, 0x22, 0x36, 0x4e, 0x08, 0x00, 0x13, 0x4a, 0x08, 0x00, 0x13, 0x06, 0x08, 0x00, 0x13, 0xf0, 0x20, 0x00, 0x13, 0xda, 0x08, 0x00, 0x13, 0xc0, 0x08, 0x00, 0x13, 0xa4, 0x08, 0x00, 0x13, 0x90, 0x08, 0x00, 0x13, 0x7c, 0x08, 0x00, 0x13, 0x5e, 0x08, 0x00, 0x13, 0x42, 0x08, 0x00, 0x13, 0x2e, 0x08, 0x00, 0x13, 0x14, 0x08, 0x00, 0x13, 0x20, 0x58, 0x00, 0x04, 0x02, 0x00, 0x22, 0x1e, 0x49, 0x0a, 0x00, 0x22, 0xca, 0x48, 0x08, 0x00, 0x13, 0x8a, 0x08, 0x00, 0x22, 0xa0, 0x46, 0x10, 0x00, 0x13, 0x46, 0x10, 0x00, 0x13, 0x08, 0x08, 0x00, 0x22, 0xc6, 0x47, 0x18, 0x00, 0x13, 0x82, 0x08, 0x00, 0x13, 0x46, 0x08, 0x00, 0x13, 0xfe, 0x30, 0x00, 0x13, 0xdc, 0x08, 0x00, 0x04, 0x02, 0x00, 0x22, 0x18, 0x4a, 0x0a, 0x00, 0x13, 0x02, 0x08, 0x00, 0x13, 0xec, 0x70, 0x00, 0x13, 0xd4, 0x08, 0x00, 0x13, 0xb6, 0x08, 0x00, 0x13, 0x76, 0x98, 0x00, 0x13, 0x9e, 0x10, 0x00, 0x13, 0x84, 0x08, 0x00, 0x13, 0x6c, 0x18, 0x00, 0x13, 0x36, 0x40, 0x00, 0x13, 0xc2, 0x18, 0x00, 0x04, 0x02, 0x00, 0x1b, 0x6e, 0x10, 0x00, 0x22, 0xe2, 0x4b, 0x1a, 0x00, 0x22, 0x02, 0x4c, 0x08, 0x00, 0x13, 0x98, 0x38, 0x00, 0x13, 0x8c, 0x08, 0x00, 0x04, 0x02, 0x00, 0x13, 0xcc, 0x28, 0x00, 0x04, 0x02, 0x00, 0x1b, 0xc4, 0x20, 0x00, 0x13, 0x50, 0x40, 0x00, 0x13, 0x42, 0x08, 0x00, 0x13, 0x26, 0x08, 0x00, 0x13, 0x0a, 0x08, 0x00, 0x13, 0x7c, 0x40, 0x00, 0x13, 0x6e, 0x08, 0x00, 0x13, 0x66, 0x40, 0x00, 0x13, 0x58, 0x10, 0x00, 0x13, 0x50, 0x08, 0x00, 0x13, 0x36, 0x08, 0x00, 0x13, 0x14, 0x08, 0x00, 0x13, 0xf2, 0x28, 0x00, 0x13, 0xd8, 0x08, 0x00, 0x13, 0x9e, 0x18, 0x00, 0x13, 0xb4, 0x10, 0x00, 0x13, 0xa2, 0x08, 0x00, 0x13, 0x94, 0x18, 0x00, 0x13, 0x8a, 0x08, 0x00, 0x13, 0x42, 0x08, 0x00, 0x04, 0x02, 0x00, 0x13, 0xf2, 0x10, 0x00, 0x13, 0x60, 0x08, 0x00, 0x04, 0x02, 0x00, 0x30, 0xec, 0x21, 0x00, 0x83, 0x16, 0x05, 0x08, 0x00, 0x22, 0x80, 0x27, 0x10, 0x00, 0x1b, 0xa0, 0x08, 0x00, 0x04, 0x02, 0x00, 0x13, 0x62, 0x18, 0x00, 0x04, 0x02, 0x00, 0x22, 0x38, 0x1b, 0x30, 0x00, 0x0c, 0x02, 0x00, 0x22, 0x70, 0x1a, 0x18, 0x00, 0x1f, 0x28, 0x20, 0x00, 0x04, 0x0f, 0x02, 0x00, 0x06, 0x12, 0x38, 0x38, 0x00, 0x13, 0x44, 0x40, 0x00, 0x22, 0x60, 0x51, 0x10, 0x00, 0x22, 0x00, 0x52, 0x08, 0x00, 0x13, 0x78, 0x20, 0x00, 0x22, 0x60, 0x10, 0x10, 0x00, 0x13, 0x40, 0x08, 0x00, 0x22, 0x20, 0x3a, 0x10, 0x00, 0x0c, 0x18, 0x00, 0xff, 0x02, 0x62, 0x61, 0x64, 0x20, 0x61, 0x6c, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x90, 0x28, 0x00, 0x04, 0x04, 0x02, 0x00, 0x00, 0xd2, 0x0b, 0x08, 0x02, 0x00, 0xd2, 0x55, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x20, 0x65, 0x78, 0x63, 0x65, 0x70, 0x43, 0x00, 0x01, 0x02, 0x00, 0x01, 0x58, 0x00, 0xf0, 0x00, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6e, 0x65, 0x77, 0x20, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x19, 0x00, 0x40, 0x54, 0x68, 0x72, 0x65, 0x73, 0x00, 0x80, 0x74, 0x74, 0x61, 0x63, 0x68, 0x65, 0x64, 0x21, 0x14, 0x00, 0x00, 0x02, 0x00, 0x03, 0x18, 0x00, 0x2b, 0x64, 0x65, 0x18, 0x00, 0x7d, 0x50, 0x72, 0x6f, 0x63, 0x65, 0x73, 0x73, 0x31, 0x00, 0x04, 0x18, 0x00, 0x0c, 0x31, 0x00, 0xf0, 0x03, 0x45, 0x78, 0x65, 0x63, 0x75, 0x74, 0x69, 0x6e, 0x67, 0x20, 0x44, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x41, 0x5e, 0x00, 0x0f, 0x18, 0x00, 0x00, 0x03, 0x3a, 0x1f, 0x0d, 0x30, 0x00, 0x10, 0x43, 0x2e, 0x00, 0x31, 0x00, 0x00, 0x4f, 0xcd, 0x00, 0xb0, 0x20, 0x31, 0x20, 0x73, 0x65, 0x6c, 0x65, 0x63, 0x74, 0x65, 0x64, 0x15, 0x00, 0x06, 0x18, 0x00, 0x1f, 0x32, 0x18, 0x00, 0x04, 0x1f, 0x33, 0x18, 0x00, 0x04, 0x1c, 0x34, 0x18, 0x00, 0x91, 0x49, 0x6e, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x20, 0x6f, 0x68, 0x00, 0x60, 0x00, 0x00, 0x46, 0x75, 0x6e, 0x63, 0x3f, 0x01, 0x40, 0x20, 0x4f, 0x6e, 0x65, 0x69, 0x00, 0x05, 0x10, 0x00, 0x3a, 0x54, 0x77, 0x6f, 0x10, 0x00, 0x91, 0x68, 0x72, 0x65, 0x65, 0x00, 0x00, 0x54, 0x65, 0x73, 0xe6, 0x00, 0xf4, 0x02, 0x76, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x3a, 0x00, 0x0a, 0x19, 0x00, 0x5b, 0x6a, 0x75, 0x6d, 0x70, 0x20, 0x1d, 0x00, 0x26, 0x00, 0x00, 0x20, 0x00, 0xcf, 0x63, 0x61, 0x6c, 0x6c, 0x20, 0x73, 0x65, 0x71, 0x75, 0x65, 0x6e, 0x63, 0x40, 0x00, 0x01, 0xb3, 0x72, 0x65, 0x63, 0x75, 0x72, 0x73, 0x69, 0x76, 0x65, 0x20, 0x66, 0x9b, 0x00, 0x01, 0x40, 0x00, 0x96, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x20, 0x6f, 0x66, 0x21, 0x00, 0x14, 0x5f, 0x21, 0x00, 0x40, 0x28, 0x35, 0x29, 0x3a, 0xcd, 0x07, 0x00, 0x02, 0x00, 0x13, 0xf8, 0x48, 0x02, 0x22, 0xe0, 0x11, 0x78, 0x02, 0x22, 0x70, 0x12, 0x08, 0x00, 0x22, 0xa0, 0x39, 0x08, 0x00, 0x04, 0x18, 0x00, 0x13, 0x10, 0x18, 0x00, 0x13, 0xd8, 0xb8, 0x02, 0x04, 0x18, 0x00, 0x13, 0x40, 0x18, 0x00, 0x04, 0x02, 0x00, 0x2a, 0x38, 0x3b, 0x38, 0x00, 0x22, 0x96, 0x26, 0x10, 0x00, 0x01, 0x9d, 0x03, 0x0f, 0x02, 0x00, 0x40, 0x22, 0x08, 0x50, 0x60, 0x00, 0x0c, 0x02, 0x00, 0x22, 0x50, 0x32, 0x18, 0x00, 0x13, 0x60, 0x08, 0x00, 0x0d, 0x02, 0x00, 0x00, 0x1f, 0x0b, 0x0f, 0x02, 0x00, 0x58, 0x13, 0xec, 0x18, 0x01, 0x0c, 0x02, 0x00, 0x13, 0x58, 0xa0, 0x00, 0x13, 0x68, 0x08, 0x00, 0x13, 0x70, 0x08, 0x00, 0x13, 0x78, 0x08, 0x00, 0x13, 0x80, 0x08, 0x00, 0x00, 0x02, 0x00, 0x04, 0x34, 0x24, 0x00, 0x91, 0x13, 0x00, 0x1f, 0x06, 0x50, 0x40, 0x3c, 0x00, 0x00, 0x40, 0x13, 0x0c, 0x07, 0x1c, 0x00, 0x50, 0x0c, 0x00, 0x00, 0x00, 0x14, 0x17, 0x24, 0x4b, 0x3c, 0x00, 0x00, 0x90, 0x1c, 0x00, 0x10, 0x0d, 0xd0, 0x22, 0x8b, 0x02, 0x00, 0x00, 0xa4, 0x3c, 0x00, 0x00, 0xa4, 0x1c, 0x00, 0x11, 0x0e, 0x46, 0x00, 0x0f, 0x02, 0x00, 0x57, 0x08, 0x7f, 0x01, 0x61, 0xb8, 0x50, 0x00, 0x00, 0x28, 0x38, 0x34, 0x05, 0x0f, 0x81, 0x00, 0x06, 0x00, 0x30, 0x00, 0x17, 0x40, 0x20, 0x00, 0x17, 0x50, 0x0c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x44, 0x00, 0x04, 0x02, 0x00, 0x00, 0xf8, 0x04, 0x00, 0x0c, 0x00, 0x00, 0xd7, 0x23, 0x01, 0x58, 0x00, 0x07, 0x02, 0x00, 0x00, 0x48, 0x00, 0x04, 0x02, 0x00, 0x61, 0x60, 0x50, 0x00, 0x00, 0xe0, 0x39, 0x8c, 0x05, 0x04, 0x13, 0x00, 0x05, 0x02, 0x00, 0x60, 0x60, 0x3b, 0x00, 0x00, 0x28, 0x39, 0xa1, 0x24, 0x05, 0x13, 0x00, 0x09, 0x02, 0x00, 0x0f, 0x1c, 0x00, 0x05, 0x08, 0x60, 0x00, 0x61, 0xf8, 0x50, 0x00, 0x00, 0xb8, 0x3a, 0x34, 0x03, 0x09, 0x3c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x7c, 0x00, 0x04, 0x02, 0x00, 0x08, 0xb0, 0x00, 0x00, 0x90, 0x00, 0x08, 0x02, 0x00, 0x26, 0x38, 0x50, 0x54, 0x00, 0x08, 0x28, 0x00, 0x11, 0x78, 0xe6, 0x03, 0x06, 0x02, 0x00, 0x1f, 0xd8, 0x28, 0x00, 0x04, 0x26, 0x20, 0x3b, 0x24, 0x00, 0x08, 0x02, 0x00, 0x00, 0xa8, 0x00, 0x18, 0xc8, 0x1c, 0x00, 0x00, 0xec, 0x00, 0x07, 0x02, 0x00, 0x00, 0x20, 0x00, 0x04, 0x02, 0x00, 0x61, 0x38, 0x51, 0x00, 0x00, 0x60, 0x3a, 0x14, 0x04, 0x04, 0x13, 0x00, 0x05, 0x02, 0x00, 0x5f, 0x50, 0x39, 0x00, 0x00, 0xf8, 0x18, 0x00, 0x00, 0x04, 0x02, 0x00, 0x00, 0x48, 0x00, 0x17, 0x90, 0x20, 0x00, 0x24, 0x18, 0x51, 0x1a, 0x00, 0x0b, 0xd0, 0x00, 0x07, 0x24, 0x00, 0x00, 0x02, 0x00, 0x00, 0x38, 0x00, 0x04, 0x02, 0x00, 0x00, 0x04, 0x01, 0x00, 0xf0, 0x00, 0x1b, 0x20, 0xf4, 0x00, 0x08, 0x02, 0x00, 0x00, 0x32, 0x0e, 0x1f, 0xa0, 0x68, 0x01, 0x00, 0x00, 0x2c, 0x03, 0x18, 0xd8, 0xe8, 0x00, 0x03, 0x02, 0x00, 0x00, 0x18, 0x00, 0x17, 0xc0, 0x30, 0x00, 0x08, 0x70, 0x00, 0x61, 0x88, 0x50, 0x00, 0x00, 0x48, 0x3a, 0x64, 0x07, 0x03, 0x2e, 0x00, 0x0e, 0x02, 0x00, 0x00, 0x40, 0x00, 0x17, 0xb0, 0x58, 0x00, 0x00, 0xec, 0x01, 0x04, 0x44, 0x00, 0x08, 0xa8, 0x01, 0x00, 0x00, 0x02, 0x08, 0x02, 0x00, 0x04, 0x24, 0x00, 0x00, 0x02, 0x00, 0x00, 0xb4, 0x01, 0x00, 0xa0, 0x01, 0x1f, 0xf8, 0x68, 0x00, 0x0c, 0x17, 0xc8, 0x1c, 0x01, 0x08, 0x40, 0x00, 0x00, 0x4c, 0x01, 0x21, 0x78, 0x39, 0x74, 0x05, 0x00, 0x4f, 0x00, 0x09, 0x02, 0x00, 0x00, 0xc4, 0x00, 0x00, 0xa4, 0x00, 0x00, 0x02, 0x00, 0x08, 0x90, 0x00, 0x00, 0xd8, 0x00, 0x08, 0x02, 0x00, 0x00, 0xdc, 0x01, 0x04, 0x54, 0x00, 0x08, 0x28, 0x00, 0x00, 0xf0, 0x01, 0x08, 0x02, 0x00, 0x2f, 0xd0, 0x3a, 0xe8, 0x01, 0x03, 0x0c, 0xfc, 0x01, 0x2e, 0x88, 0x3b, 0x14, 0x00, 0x00, 0x8f, 0x1b, 0x90, 0x02, 0x80, 0x02, 0x80, 0x04, 0x3c, 0x00, 0x00, 0x2c, 0xc7, 0x27, 0x12, 0x3c, 0x84, 0x28, 0xf0, 0x02, 0x43, 0x1d, 0x00, 0x00, 0xbf, 0x1d, 0x00, 0x00, 0xd4, 0x1d, 0x00, 0x00, 0xd5, 0x1e, 0x00, 0x00, 0xeb, 0x41, 0x1f, 0x90, 0x20, 0x00, 0x00, 0xea, 0x22, 0x00, 0x00, 0x1c, 0x23, 0xc2, 0x0e, 0x82, 0x00, 0x00, 0x04, 0x26, 0x00, 0x00, 0x4e, 0x26, 0x20, 0x28, 0x80, 0x90, 0x0c, 0x00, 0x00, 0x03, 0x28, 0x00, 0x00, 0x9b, 0x15, 0xf0, 0x05, 0x52, 0x53, 0x44, 0x53, 0xb5, 0x7f, 0x84, 0x75, 0xa2, 0x17, 0xc0, 0x4d, 0x9f, 0xea, 0x8c, 0xfe, 0x80, 0x9a, 0x5c, 0xa3, 0xc8, 0x00, 0xf0, 0x1e, 0x43, 0x3a, 0x5c, 0x55, 0x73, 0x65, 0x72, 0x73, 0x5c, 0x6d, 0x61, 0x6c, 0x77, 0x61, 0x72, 0x65, 0x5c, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x5c, 0x72, 0x65, 0x70, 0x6f, 0x73, 0x5c, 0x63, 0x70, 0x70, 0x5c, 0x78, 0x36, 0x34, 0x5c, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x10, 0x00, 0x43, 0x2e, 0x70, 0x64, 0x62, 0xe9, 0x00, 0x13, 0x22, 0x04, 0x00, 0x00, 0x48, 0x00, 0x00, 0xf1, 0x08, 0x40, 0x47, 0x43, 0x54, 0x4c, 0x3d, 0x23, 0xc0, 0x70, 0x17, 0x00, 0x00, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x24, 0x6d, 0x6e, 0x2b, 0x00, 0x22, 0x70, 0x27, 0x5c, 0x04, 0x04, 0x14, 0x00, 0x95, 0x24, 0x30, 0x30, 0x00, 0xb0, 0x27, 0x00, 0x00, 0x89, 0x14, 0x00, 0x15, 0x78, 0x00, 0x29, 0x80, 0x2e, 0x69, 0x64, 0x61, 0x74, 0x61, 0x24, 0x35, 0x38, 0x00, 0x22, 0x50, 0x32, 0xe3, 0x04, 0xc0, 0x2e, 0x30, 0x30, 0x63, 0x66, 0x67, 0x00, 0x00, 0x88, 0x32, 0x00, 0x00, 0x70, 0x19, 0x71, 0x2e, 0x43, 0x52, 0x54, 0x24, 0x58, 0x43, 0x02, 0x09, 0x1b, 0x90, 0x14, 0x00, 0x00, 0x15, 0x00, 0x1a, 0x98, 0x14, 0x00, 0x10, 0x5a, 0x4c, 0x00, 0x19, 0xa0, 0x14, 0x00, 0x10, 0x49, 0x27, 0x00, 0x2b, 0x00, 0xa8, 0x14, 0x00, 0x00, 0x15, 0x00, 0x1b, 0xb0, 0x14, 0x00, 0x00, 0x37, 0x09, 0x1a, 0xb8, 0x14, 0x00, 0x01, 0x50, 0x00, 0x19, 0xc0, 0x14, 0x00, 0x10, 0x50, 0x3b, 0x00, 0x2a, 0x00, 0xc8, 0x14, 0x00, 0x01, 0x28, 0x00, 0x19, 0xd0, 0x14, 0x00, 0x11, 0x54, 0x28, 0x00, 0x1a, 0xd8, 0x14, 0x00, 0x01, 0x28, 0x00, 0x84, 0xe0, 0x32, 0x00, 0x00, 0x20, 0x05, 0x00, 0x00, 0xc0, 0x29, 0x64, 0x00, 0x38, 0x00, 0x00, 0xec, 0x03, 0x10, 0x00, 0x20, 0x24, 0x72, 0xc4, 0x00, 0x50, 0xec, 0x3b, 0x00, 0x00, 0x54, 0x34, 0x01, 0x01, 0x24, 0x00, 0x60, 0x24, 0x76, 0x6f, 0x6c, 0x74, 0x6d, 0xc0, 0x09, 0x00, 0xd8, 0x06, 0x16, 0x48, 0x2c, 0x00, 0x50, 0x7a, 0x7a, 0x7a, 0x64, 0x62, 0xf9, 0x06, 0x23, 0x88, 0x3f, 0x30, 0x01, 0x42, 0x72, 0x74, 0x63, 0x24, 0xdf, 0x00, 0x29, 0x00, 0x90, 0x14, 0x00, 0x11, 0x5a, 0x7c, 0x00, 0x18, 0x98, 0x14, 0x00, 0x21, 0x54, 0x41, 0xa4, 0x00, 0x19, 0xa0, 0x14, 0x00, 0x02, 0x28, 0x00, 0x22, 0xa8, 0x3f, 0xf4, 0x2a, 0x20, 0x2e, 0x78, 0xa4, 0x01, 0x42, 0x00, 0x00, 0x88, 0x41, 0x5c, 0x2c, 0x02, 0x10, 0x00, 0x10, 0x24, 0xf7, 0x2a, 0x05, 0x28, 0x2b, 0x22, 0x2e, 0x65, 0x24, 0x00, 0x50, 0x84, 0x43, 0x00, 0x00, 0xb4, 0xb4, 0x00, 0x02, 0xd8, 0x01, 0x10, 0x32, 0xc8, 0x00, 0x22, 0x38, 0x44, 0xe4, 0x02, 0x03, 0xec, 0x01, 0x10, 0x33, 0x14, 0x00, 0x22, 0x50, 0x44, 0x00, 0x2b, 0x03, 0x14, 0x00, 0x10, 0x34, 0x14, 0x00, 0x00, 0x5c, 0x0e, 0x25, 0xe0, 0x07, 0x14, 0x00, 0x00, 0xc7, 0x02, 0x31, 0x00, 0x00, 0x50, 0x1b, 0x01, 0x22, 0x00, 0x2e, 0x5f, 0x00, 0x01, 0xec, 0x04, 0x00, 0x31, 0x21, 0x01, 0x10, 0x00, 0x21, 0x24, 0x72, 0xd8, 0x06, 0x14, 0xa8, 0x20, 0x00, 0x30, 0x24, 0x72, 0x73, 0x33, 0x00, 0x50, 0x60, 0x51, 0x00, 0x00, 0xf8, 0x5c, 0x01, 0x21, 0x62, 0x73, 0x10, 0x00, 0x04, 0xb4, 0x2b, 0x04, 0xdc, 0x2a, 0x21, 0x00, 0x70, 0x13, 0x00, 0x11, 0x00, 0xc4, 0x2a, 0x31, 0x24, 0x30, 0x31, 0x34, 0x00, 0x50, 0x70, 0x00, 0x00, 0x80, 0x01, 0x80, 0x01, 0x01, 0x14, 0x00, 0x01, 0xb8, 0x00, 0x0f, 0x02, 0x00, 0x11, 0xf2, 0x0d, 0x01, 0x06, 0x02, 0x00, 0x06, 0x32, 0x02, 0x30, 0x01, 0x0a, 0x04, 0x00, 0x0a, 0x34, 0x06, 0x00, 0x0a, 0x32, 0x06, 0x70, 0x01, 0x04, 0x01, 0x00, 0x04, 0x42, 0x00, 0x00, 0x14, 0x00, 0xf1, 0x0b, 0x0c, 0x00, 0x0a, 0x72, 0x06, 0x50, 0x21, 0x05, 0x02, 0x00, 0x05, 0x64, 0x0a, 0x00, 0xa0, 0x12, 0x00, 0x00, 0xaf, 0x12, 0x00, 0x00, 0xc4, 0x3f, 0x00, 0x00, 0x14, 0x00, 0x21, 0x74, 0x0b, 0x10, 0x00, 0x50, 0x26, 0x13, 0x00, 0x00, 0xd0, 0x14, 0x00, 0x12, 0x00, 0x20, 0x00, 0x08, 0x10, 0x00, 0x08, 0x34, 0x00, 0x13, 0x19, 0x70, 0x00, 0xf3, 0x0e, 0x90, 0x26, 0x00, 0x00, 0x28, 0x40, 0x00, 0x00, 0x68, 0x31, 0x40, 0x00, 0x00, 0x37, 0x40, 0x00, 0x00, 0x02, 0x0e, 0x9c, 0x26, 0x00, 0x00, 0x04, 0x2c, 0x00, 0x3c, 0x02, 0x19, 0x80, 0x00, 0x00, 0x24, 0x00, 0x20, 0x4c, 0x40, 0x10, 0x01, 0x00, 0x20, 0x00, 0xf0, 0x07, 0x36, 0x00, 0x19, 0x16, 0x08, 0x00, 0x16, 0x34, 0x0d, 0x00, 0x16, 0x52, 0x12, 0xf0, 0x10, 0xe0, 0x0e, 0xc0, 0x0c, 0x70, 0x0b, 0x60, 0x24, 0x00, 0xf0, 0x0b, 0x70, 0x40, 0x00, 0x00, 0x38, 0x7d, 0x40, 0x00, 0x00, 0x96, 0x40, 0x00, 0x00, 0xa7, 0x40, 0x00, 0x00, 0x0c, 0x0a, 0x90, 0x17, 0x00, 0x00, 0x40, 0x3a, 0x50, 0x06, 0x00, 0x30, 0x30, 0x38, 0x7e, 0x5a, 0x00, 0x10, 0x2e, 0x05, 0x00, 0x50, 0x02, 0x04, 0x04, 0x06, 0x9e, 0x4d, 0x00, 0xf0, 0x06, 0x11, 0x80, 0xc8, 0x27, 0x00, 0x00, 0x7d, 0x05, 0x0e, 0xd6, 0x00, 0x50, 0x02, 0xa8, 0x06, 0xec, 0x04, 0x4c, 0x0a, 0x28, 0x0c, 0xda, 0x1b, 0x30, 0x01, 0x0a, 0x02, 0x04, 0x01, 0x10, 0x50, 0x24, 0x04, 0xf2, 0x03, 0x09, 0x0f, 0x06, 0x00, 0x0f, 0x64, 0x09, 0x00, 0x0f, 0x34, 0x08, 0x00, 0x0f, 0x52, 0x0b, 0x70, 0xa2, 0x26, 0x74, 0x05, 0x62, 0x7d, 0x1b, 0x00, 0x00, 0x82, 0x1c, 0xac, 0x04, 0x00, 0x08, 0x00, 0x57, 0xb6, 0x1c, 0x00, 0x00, 0xc8, 0x10, 0x00, 0x03, 0x54, 0x01, 0xf1, 0x06, 0x50, 0x01, 0x09, 0x01, 0x00, 0x09, 0x62, 0x00, 0x00, 0x01, 0x08, 0x04, 0x00, 0x08, 0x72, 0x04, 0x70, 0x03, 0x60, 0x02, 0x30, 0x5c, 0x01, 0x40, 0x82, 0x00, 0x00, 0x09, 0xe4, 0x00, 0x30, 0x22, 0x00, 0x00, 0x54, 0x00, 0x00, 0x6c, 0x00, 0xa2, 0xe3, 0x1f, 0x00, 0x00, 0x6d, 0x20, 0x00, 0x00, 0x21, 0x28, 0x08, 0x00, 0x50, 0x01, 0x02, 0x01, 0x00, 0x02, 0xf4, 0x07, 0x80, 0x0d, 0x04, 0x00, 0x0d, 0x34, 0x09, 0x00, 0x0d, 0x94, 0x00, 0xc0, 0x15, 0x05, 0x00, 0x15, 0x34, 0xba, 0x00, 0x15, 0x01, 0xb8, 0x00, 0x06, 0x1c, 0x00, 0x01, 0xa0, 0x00, 0xa0, 0x06, 0x00, 0x0f, 0x34, 0x05, 0x00, 0x0f, 0x12, 0x0b, 0x70, 0xf0, 0x01, 0x00, 0x4c, 0x00, 0x00, 0x02, 0x00, 0x04, 0x08, 0x00, 0x00, 0x02, 0x00, 0x11, 0xb0, 0xb3, 0x2a, 0x31, 0x00, 0x00, 0xa8, 0x2a, 0x03, 0x0a, 0x02, 0x00, 0x00, 0x44, 0x06, 0x53, 0x08, 0x42, 0x00, 0x00, 0x30, 0x88, 0x0d, 0x04, 0x02, 0x00, 0x00, 0x70, 0x07, 0x2f, 0xe0, 0x41, 0x1c, 0x00, 0x03, 0x04, 0x02, 0x00, 0x00, 0x84, 0x06, 0x00, 0x02, 0x00, 0x04, 0x58, 0x06, 0x00, 0x08, 0x06, 0x20, 0x00, 0x11, 0x12, 0x00, 0x06, 0x02, 0x00, 0x00, 0x7b, 0x00, 0x00, 0xf4, 0x02, 0x00, 0x02, 0x00, 0x08, 0x28, 0x00, 0x1b, 0x40, 0x28, 0x00, 0x00, 0x02, 0x00, 0x04, 0x34, 0x09, 0x08, 0x28, 0x00, 0x00, 0xa0, 0x05, 0x0c, 0x02, 0x00, 0x04, 0xd0, 0x00, 0x1f, 0xc0, 0xd0, 0x00, 0x00, 0x08, 0x02, 0x00, 0x04, 0x48, 0x00, 0x10, 0xf8, 0xcc, 0x02, 0x12, 0x00, 0x5c, 0x04, 0x00, 0x90, 0x05, 0xf2, 0x17, 0xa8, 0x42, 0x00, 0x00, 0xc8, 0x42, 0x00, 0x00, 0xe8, 0x42, 0x00, 0x00, 0xb0, 0x14, 0x00, 0x00, 0x20, 0x14, 0x00, 0x00, 0x80, 0x14, 0x00, 0x00, 0x50, 0x14, 0x00, 0x00, 0xb0, 0x13, 0x00, 0x00, 0x30, 0x15, 0x00, 0x00, 0x80, 0x11, 0xb8, 0x02, 0xf0, 0x0e, 0x00, 0x43, 0x00, 0x00, 0x16, 0x43, 0x00, 0x00, 0x23, 0x43, 0x00, 0x00, 0x32, 0x43, 0x00, 0x00, 0x3f, 0x43, 0x00, 0x00, 0x52, 0x43, 0x00, 0x00, 0x65, 0x43, 0x00, 0x00, 0x72, 0x6d, 0x05, 0xf0, 0x08, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, 0x63, 0x70, 0x70, 0x2e, 0x65, 0x78, 0x65, 0x00, 0xd7, 0x0d, 0x14, 0x5f, 0xd7, 0x0d, 0x13, 0x5f, 0x17, 0x0e, 0x14, 0x00, 0xc3, 0x0d, 0x37, 0x5f, 0x6f, 0x6e, 0x0d, 0x00, 0x11, 0x74, 0x4b, 0x0e, 0x05, 0x1c, 0x00, 0x40, 0x74, 0x77, 0x6f, 0x00, 0x36, 0x0e, 0x11, 0x5f, 0x36, 0x0e, 0x05, 0x3c, 0x00, 0x0e, 0xe8, 0x0d, 0x50, 0x00, 0x74, 0x6c, 0x73, 0x5f, 0x69, 0x00, 0x52, 0x62, 0x61, 0x63, 0x6b, 0x00, 0x82, 0x0e, 0x05, 0x2f, 0x00, 0x47, 0x00, 0x00, 0x00, 0xd0, 0xd0, 0x2e, 0xa6, 0x60, 0x49, 0x00, 0x00, 0x80, 0x30, 0x00, 0x00, 0x90, 0x45, 0x22, 0x01, 0x97, 0x40, 0x4a, 0x00, 0x00, 0x40, 0x31, 0x00, 0x00, 0x30, 0x14, 0x00, 0x97, 0x54, 0x4a, 0x00, 0x00, 0xe0, 0x30, 0x00, 0x00, 0xe8, 0x14, 0x00, 0x97, 0x5c, 0x4c, 0x00, 0x00, 0x98, 0x31, 0x00, 0x00, 0xa0, 0x14, 0x00, 0x97, 0x7e, 0x4c, 0x00, 0x00, 0x50, 0x31, 0x00, 0x00, 0xd8, 0x14, 0x00, 0x97, 0x9e, 0x4c, 0x00, 0x00, 0x88, 0x31, 0x00, 0x00, 0x88, 0x2c, 0x13, 0x97, 0xbe, 0x4c, 0x00, 0x00, 0x38, 0x32, 0x00, 0x00, 0xc8, 0x28, 0x00, 0x62, 0xde, 0x4c, 0x00, 0x00, 0x78, 0x31, 0x44, 0x05, 0x04, 0x02, 0x00, 0x10, 0x5e, 0xf1, 0x0c, 0x00, 0x81, 0x03, 0x0f, 0x02, 0x00, 0x05, 0x03, 0xe8, 0x13, 0x10, 0x36, 0x28, 0x00, 0x0f, 0x50, 0x14, 0xff, 0xff, 0x32, 0xf2, 0x21, 0xb4, 0x02, 0x3f, 0x63, 0x6f, 0x75, 0x74, 0x40, 0x73, 0x74, 0x64, 0x40, 0x40, 0x33, 0x56, 0x3f, 0x24, 0x62, 0x61, 0x73, 0x69, 0x63, 0x5f, 0x6f, 0x73, 0x74, 0x72, 0x65, 0x61, 0x6d, 0x40, 0x44, 0x55, 0x3f, 0x24, 0x63, 0x68, 0x61, 0x72, 0x5f, 0x74, 0x72, 0x61, 0x69, 0x74, 0x73, 0x40, 0x44, 0x29, 0x00, 0xf5, 0x03, 0x40, 0x31, 0x40, 0x41, 0x00, 0x00, 0x1e, 0x05, 0x3f, 0x75, 0x6e, 0x63, 0x61, 0x75, 0x67, 0x68, 0x74, 0x5f, 0x70, 0x13, 0x02, 0x21, 0x00, 0xf4, 0x01, 0x59, 0x41, 0x5f, 0x4e, 0x58, 0x5a, 0x00, 0xe1, 0x04, 0x3f, 0x73, 0x70, 0x75, 0x74, 0x6e, 0x40, 0x58, 0x00, 0x02, 0x57, 0x00, 0x3f, 0x62, 0x75, 0x66, 0x5a, 0x00, 0x06, 0x01, 0x89, 0x00, 0xf6, 0x08, 0x51, 0x45, 0x41, 0x41, 0x5f, 0x4a, 0x50, 0x45, 0x42, 0x44, 0x5f, 0x4a, 0x40, 0x5a, 0x00, 0x00, 0x34, 0x05, 0x3f, 0x77, 0x69, 0x64, 0x65, 0x48, 0x00, 0x3f, 0x69, 0x6f, 0x73, 0x42, 0x00, 0x0d, 0x40, 0x42, 0x41, 0x44, 0x44, 0x3c, 0x00, 0x65, 0x61, 0x04, 0x3f, 0x70, 0x75, 0x74, 0x82, 0x00, 0x0f, 0xda, 0x00, 0x0d, 0x05, 0x80, 0x00, 0x71, 0x41, 0x45, 0x41, 0x56, 0x31, 0x32, 0x40, 0x44, 0x00, 0x12, 0xde, 0xc8, 0x00, 0x15, 0x63, 0x46, 0x00, 0x0f, 0xc8, 0x00, 0x18, 0x11, 0x48, 0x42, 0x00, 0x85, 0x44, 0x02, 0x3f, 0x5f, 0x4f, 0x73, 0x66, 0x78, 0x42, 0x00, 0x0f, 0x88, 0x00, 0x16, 0xcf, 0x58, 0x58, 0x5a, 0x00, 0x68, 0x03, 0x3f, 0x66, 0x6c, 0x75, 0x73, 0x68, 0x3e, 0x00, 0x1f, 0x03, 0xc6, 0x00, 0xe5, 0x58, 0x5a, 0x00, 0xc5, 0x04, 0x3f, 0x73, 0x65, 0x74, 0x73, 0x74, 0x61, 0x74, 0x65, 0x47, 0x00, 0x0f, 0x47, 0x01, 0x10, 0xe4, 0x41, 0x41, 0x58, 0x48, 0x5f, 0x4e, 0x40, 0x5a, 0x00, 0x06, 0x01, 0x3f, 0x3f, 0x36, 0xc8, 0x01, 0x0f, 0xbe, 0x00, 0x16, 0x00, 0x80, 0x00, 0x54, 0x30, 0x31, 0x40, 0x50, 0x36, 0x0a, 0x00, 0x03, 0x07, 0x00, 0x20, 0x40, 0x5a, 0x9c, 0x01, 0x2f, 0xff, 0x00, 0x54, 0x00, 0x28, 0x10, 0x48, 0x42, 0x00, 0xc0, 0x4d, 0x53, 0x56, 0x43, 0x50, 0x31, 0x34, 0x30, 0x2e, 0x64, 0x6c, 0x6c, 0x11, 0x05, 0xf0, 0x03, 0x5f, 0x5f, 0x43, 0x78, 0x78, 0x46, 0x72, 0x61, 0x6d, 0x65, 0x48, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x72, 0x34, 0xec, 0x0c, 0x56, 0x5f, 0x5f, 0x73, 0x74, 0x64, 0xa4, 0x02, 0xad, 0x5f, 0x64, 0x65, 0x73, 0x74, 0x72, 0x6f, 0x79, 0x00, 0x21, 0x1a, 0x00, 0x40, 0x63, 0x6f, 0x70, 0x79, 0x7a, 0x0d, 0x50, 0x5f, 0x70, 0x75, 0x72, 0x65, 0x54, 0x06, 0x23, 0x00, 0x23, 0x24, 0x00, 0xf2, 0x0b, 0x74, 0x65, 0x72, 0x6d, 0x69, 0x6e, 0x61, 0x74, 0x65, 0x00, 0x08, 0x00, 0x5f, 0x5f, 0x43, 0x5f, 0x73, 0x70, 0x65, 0x63, 0x69, 0x66, 0x69, 0x63, 0x5f, 0x68, 0x69, 0x00, 0x00, 0x6c, 0x08, 0x00, 0x7d, 0x00, 0x66, 0x54, 0x68, 0x72, 0x6f, 0x77, 0x45, 0x7f, 0x16, 0xa7, 0x1b, 0x00, 0x5f, 0x5f, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x26, 0x03, 0x2f, 0x00, 0x1c, 0x16, 0x00, 0x01, 0x41, 0x5f, 0x63, 0x6f, 0x6e, 0x30, 0x36, 0xf2, 0x09, 0x3e, 0x00, 0x6d, 0x65, 0x6d, 0x73, 0x65, 0x74, 0x00, 0x00, 0x56, 0x43, 0x52, 0x55, 0x4e, 0x54, 0x49, 0x4d, 0x45, 0x31, 0x34, 0x30, 0x5f, 0x31, 0xe6, 0x00, 0x08, 0x14, 0x00, 0x02, 0x12, 0x00, 0x42, 0x39, 0x00, 0x5f, 0x69, 0xc1, 0x15, 0x30, 0x5f, 0x70, 0x61, 0xfd, 0x00, 0xf0, 0x04, 0x74, 0x65, 0x72, 0x5f, 0x6e, 0x6f, 0x69, 0x6e, 0x66, 0x6f, 0x5f, 0x6e, 0x6f, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0xf4, 0x07, 0x01, 0x26, 0x07, 0x81, 0x6e, 0x65, 0x77, 0x68, 0x00, 0x19, 0x00, 0x6d, 0x67, 0x17, 0x00, 0xe2, 0x0d, 0x80, 0x5f, 0x73, 0x65, 0x68, 0x5f, 0x66, 0x69, 0x6c, 0x35, 0x00, 0x00, 0xb4, 0x07, 0xf0, 0x02, 0x42, 0x00, 0x5f, 0x73, 0x65, 0x74, 0x5f, 0x61, 0x70, 0x70, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x00, 0x09, 0x02, 0x01, 0xd0, 0x65, 0x74, 0x75, 0x73, 0x65, 0x72, 0x6d, 0x61, 0x74, 0x68, 0x65, 0x72, 0x72, 0xe4, 0x08, 0x00, 0xad, 0x00, 0xf0, 0x06, 0x66, 0x69, 0x67, 0x75, 0x72, 0x65, 0x5f, 0x6e, 0x61, 0x72, 0x72, 0x6f, 0x77, 0x5f, 0x61, 0x72, 0x67, 0x76, 0x00, 0x00, 0x33, 0x8c, 0x00, 0x75, 0x69, 0x74, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x1b, 0x00, 0xf4, 0x04, 0x65, 0x6e, 0x76, 0x69, 0x72, 0x6f, 0x6e, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x28, 0x00, 0x5f, 0x67, 0x65, 0x74, 0x26, 0x00, 0x04, 0x3e, 0x00, 0x08, 0x23, 0x00, 0x12, 0x36, 0x44, 0x00, 0x00, 0x73, 0x01, 0x26, 0x00, 0x37, 0x0c, 0x00, 0xa0, 0x5f, 0x65, 0x00, 0x55, 0x00, 0x65, 0x78, 0x69, 0x74, 0x00, 0x96, 0x01, 0x01, 0x09, 0x00, 0x12, 0x54, 0xac, 0x00, 0xe0, 0x66, 0x6d, 0x6f, 0x64, 0x65, 0x00, 0x00, 0x04, 0x00, 0x5f, 0x5f, 0x70, 0x5f, 0x5f, 0x8a, 0x00, 0x46, 0x63, 0x00, 0x00, 0x05, 0x0e, 0x00, 0x71, 0x76, 0x00, 0x00, 0x16, 0x00, 0x5f, 0x63, 0x33, 0x00, 0x61, 0x00, 0x15, 0x00, 0x5f, 0x63, 0x5f, 0x0b, 0x00, 0x80, 0x3d, 0x00, 0x5f, 0x72, 0x65, 0x67, 0x69, 0x73, 0xfa, 0x00, 0x11, 0x74, 0x0a, 0x18, 0x10, 0x5f, 0x7b, 0x18, 0x80, 0x6c, 0x5f, 0x65, 0x78, 0x65, 0x5f, 0x61, 0x74, 0x24, 0x00, 0x01, 0x33, 0x01, 0x01, 0x59, 0x08, 0x00, 0xf8, 0x01, 0x02, 0xf4, 0x00, 0x02, 0x2b, 0x00, 0x01, 0x2a, 0x00, 0x10, 0x65, 0x58, 0x00, 0x00, 0x2e, 0x01, 0x41, 0x6e, 0x65, 0x77, 0x5f, 0x85, 0x00, 0x12, 0x01, 0x76, 0x00, 0x31, 0x63, 0x6f, 0x6d, 0x0f, 0x00, 0x41, 0x00, 0x18, 0x00, 0x66, 0x22, 0x17, 0x12, 0x34, 0xc8, 0x00, 0x03, 0x18, 0x01, 0x21, 0x6f, 0x6e, 0x5d, 0x00, 0x01, 0xdb, 0x08, 0x37, 0x00, 0x00, 0x3c, 0x88, 0x00, 0x03, 0x1a, 0x00, 0x04, 0x07, 0x09, 0x73, 0x00, 0x1e, 0x00, 0x5f, 0x63, 0x72, 0x74, 0x8e, 0x00, 0x30, 0x00, 0x67, 0x00, 0x15, 0x01, 0x02, 0x88, 0x02, 0xf2, 0x0d, 0x61, 0x70, 0x69, 0x2d, 0x6d, 0x73, 0x2d, 0x77, 0x69, 0x6e, 0x2d, 0x63, 0x72, 0x74, 0x2d, 0x72, 0x75, 0x6e, 0x74, 0x69, 0x6d, 0x65, 0x2d, 0x6c, 0x31, 0x2d, 0x31, 0x2d, 0x11, 0x03, 0x0b, 0x22, 0x00, 0x48, 0x68, 0x65, 0x61, 0x70, 0x1f, 0x00, 0x0c, 0x20, 0x00, 0x00, 0xde, 0x01, 0x0f, 0x20, 0x00, 0x09, 0x58, 0x73, 0x74, 0x64, 0x69, 0x6f, 0x21, 0x00, 0x0b, 0x40, 0x00, 0x02, 0x12, 0x01, 0x08, 0x21, 0x00, 0xe3, 0x00, 0xd5, 0x04, 0x52, 0x74, 0x6c, 0x43, 0x61, 0x70, 0x74, 0x75, 0x72, 0x65, 0x43, 0xde, 0x02, 0x10, 0xdc, 0x14, 0x00, 0x64, 0x4c, 0x6f, 0x6f, 0x6b, 0x75, 0x70, 0x57, 0x18, 0x80, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x00, 0x00, 0xe3, 0x1a, 0x00, 0xf2, 0x04, 0x56, 0x69, 0x72, 0x74, 0x75, 0x61, 0x6c, 0x55, 0x6e, 0x77, 0x69, 0x6e, 0x64, 0x00, 0x00, 0xc0, 0x05, 0x55, 0x6e, 0x63, 0x03, 0x15, 0x64, 0x56, 0x03, 0x11, 0x46, 0xad, 0x02, 0x7f, 0x00, 0x00, 0x7f, 0x05, 0x53, 0x65, 0x74, 0x1f, 0x00, 0x06, 0x62, 0x20, 0x02, 0x47, 0x65, 0x74, 0x43, 0x7b, 0x03, 0x03, 0xa0, 0x19, 0x44, 0x00, 0x9e, 0x05, 0x54, 0x40, 0x01, 0x04, 0x13, 0x00, 0x53, 0x00, 0x8c, 0x03, 0x49, 0x73, 0x0d, 0x00, 0x50, 0x6f, 0x72, 0x46, 0x65, 0x61, 0xac, 0x00, 0x40, 0x50, 0x72, 0x65, 0x73, 0xad, 0x02, 0xfe, 0x0c, 0x52, 0x04, 0x51, 0x75, 0x65, 0x72, 0x79, 0x50, 0x65, 0x72, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x6e, 0x63, 0x65, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x65, 0x72, 0x00, 0x21, 0x5e, 0x00, 0x47, 0x49, 0x64, 0x00, 0x25, 0x16, 0x00, 0x02, 0x44, 0x1a, 0x50, 0x49, 0x64, 0x00, 0x00, 0xf3, 0x16, 0x00, 0xf0, 0x01, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x54, 0x69, 0x6d, 0x65, 0x41, 0x73, 0x46, 0x69, 0x6c, 0x65, 0x0a, 0x00, 0x45, 0x00, 0x6f, 0x03, 0x49, 0x15, 0x02, 0xf4, 0x07, 0x53, 0x4c, 0x69, 0x73, 0x74, 0x48, 0x65, 0x61, 0x64, 0x00, 0x85, 0x03, 0x49, 0x73, 0x44, 0x65, 0x62, 0x75, 0x67, 0x67, 0x65, 0x72, 0x8a, 0x00, 0x10, 0x81, 0x44, 0x00, 0x53, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0xdb, 0x04, 0xb2, 0x57, 0x00, 0x00, 0x4b, 0x45, 0x52, 0x4e, 0x45, 0x4c, 0x33, 0x32, 0x06, 0x04, 0x10, 0x3d, 0x36, 0x04, 0x60, 0x6d, 0x6f, 0x76, 0x65, 0x00, 0x3c, 0x0a, 0x00, 0x10, 0x63, 0xca, 0x04, 0x0f, 0x02, 0x00, 0xff, 0x6e, 0x93, 0xcd, 0x5d, 0x20, 0xd2, 0x66, 0xd4, 0xff, 0xff, 0x32, 0x6f, 0x28, 0x00, 0x8c, 0x0d, 0x11, 0x01, 0x2e, 0x0d, 0x02, 0x74, 0x0e, 0x00, 0x96, 0x05, 0x00, 0x06, 0x00, 0x03, 0x52, 0x3b, 0x01, 0xa0, 0x0d, 0x00, 0x02, 0x00, 0x17, 0xe8, 0x10, 0x19, 0x00, 0x02, 0x00, 0x81, 0x2e, 0x3f, 0x41, 0x56, 0x62, 0x61, 0x64, 0x5f, 0xb5, 0x05, 0x02, 0x64, 0x09, 0x00, 0x17, 0x00, 0x0f, 0x28, 0x00, 0x02, 0x0b, 0x8c, 0x09, 0x0f, 0x28, 0x00, 0x06, 0x01, 0x50, 0x00, 0x00, 0x14, 0x1d, 0x01, 0xbd, 0x04, 0x02, 0x14, 0x1d, 0x04, 0x5b, 0x00, 0x0f, 0x58, 0x00, 0x01, 0x00, 0x0d, 0x06, 0x10, 0x5f, 0x54, 0x06, 0x2f, 0x40, 0x40, 0x78, 0x00, 0x02, 0x03, 0xe2, 0x1c, 0x10, 0x43, 0x73, 0x00, 0x0f, 0x40, 0x00, 0x01, 0x03, 0x20, 0x00, 0x1f, 0x42, 0x20, 0x00, 0x05, 0x40, 0x42, 0x61, 0x73, 0x65, 0x1c, 0x00, 0x00, 0x02, 0x00, 0x0f, 0x40, 0x00, 0x08, 0x14, 0x41, 0x24, 0x00, 0x0f, 0x02, 0x00, 0x92, 0x52, 0x10, 0x00, 0x00, 0x33, 0x10, 0x84, 0x13, 0xf2, 0x03, 0x60, 0x10, 0x00, 0x00, 0xa3, 0x10, 0x00, 0x00, 0xb0, 0x3f, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x3d, 0x11, 0x18, 0x00, 0x00, 0x04, 0x10, 0x13, 0x7d, 0x0c, 0x00, 0x00, 0x70, 0x0f, 0x50, 0xdf, 0x11, 0x00, 0x00, 0xbc, 0xb4, 0x13, 0x52, 0x11, 0x00, 0x00, 0x0b, 0x12, 0x24, 0x00, 0x62, 0x10, 0x12, 0x00, 0x00, 0x3c, 0x12, 0x18, 0x00, 0x53, 0x40, 0x12, 0x00, 0x00, 0x6c, 0x0c, 0x00, 0x53, 0x70, 0x12, 0x00, 0x00, 0x9c, 0x0c, 0x00, 0x08, 0x60, 0x12, 0x00, 0x9c, 0x12, 0x04, 0x7c, 0x12, 0x00, 0x08, 0x00, 0x62, 0x57, 0x13, 0x00, 0x00, 0xe4, 0x3f, 0x08, 0x00, 0x62, 0x61, 0x13, 0x00, 0x00, 0xf8, 0x3f, 0x08, 0x00, 0x62, 0xa6, 0x13, 0x00, 0x00, 0x08, 0x40, 0xf0, 0x0f, 0x22, 0x1b, 0x14, 0x60, 0x00, 0x00, 0x08, 0x10, 0x13, 0x4c, 0x0c, 0x00, 0x53, 0x50, 0x14, 0x00, 0x00, 0x7c, 0x0c, 0x00, 0x00, 0x1c, 0x10, 0x13, 0xac, 0x0c, 0x00, 0x00, 0x30, 0x10, 0x22, 0x22, 0x15, 0x30, 0x00, 0x00, 0x28, 0x10, 0x22, 0x53, 0x15, 0xa8, 0x00, 0x62, 0x60, 0x15, 0x00, 0x00, 0x47, 0x17, 0x0c, 0x00, 0x00, 0x77, 0x12, 0x80, 0x8e, 0x17, 0x00, 0x00, 0x18, 0x40, 0x00, 0x00, 0x89, 0x12, 0x00, 0x14, 0x34, 0x50, 0x3c, 0x40, 0x00, 0x00, 0xc0, 0xcf, 0x33, 0x40, 0x19, 0x00, 0x00, 0x54, 0x18, 0x00, 0x52, 0x19, 0x00, 0x00, 0xc9, 0x19, 0x30, 0x00, 0x90, 0xe0, 0x19, 0x00, 0x00, 0xfe, 0x19, 0x00, 0x00, 0xc0, 0xd1, 0x1a, 0x52, 0x1a, 0x00, 0x00, 0x3c, 0x1a, 0x18, 0x00, 0x53, 0x44, 0x1a, 0x00, 0x00, 0x6f, 0x0c, 0x00, 0x62, 0x70, 0x1a, 0x00, 0x00, 0x26, 0x1b, 0x18, 0x00, 0x62, 0x28, 0x1b, 0x00, 0x00, 0x38, 0x1b, 0x84, 0x00, 0x00, 0x08, 0x00, 0x13, 0x51, 0x0c, 0x00, 0x22, 0x54, 0x1b, 0x58, 0x40, 0x22, 0xc4, 0x40, 0x08, 0x00, 0x22, 0xe2, 0x1c, 0x24, 0x00, 0x62, 0xe4, 0x1c, 0x00, 0x00, 0x18, 0x1d, 0x3c, 0x00, 0x00, 0x08, 0x00, 0x80, 0xea, 0x1d, 0x00, 0x00, 0x04, 0x41, 0x00, 0x00, 0x0c, 0x3b, 0xf2, 0x03, 0x5d, 0x1e, 0x00, 0x00, 0x0c, 0x41, 0x00, 0x00, 0x80, 0x1e, 0x00, 0x00, 0xa0, 0x1e, 0x00, 0x00, 0x18, 0x41, 0x08, 0x00, 0x13, 0xc0, 0x0c, 0x00, 0x62, 0xc8, 0x1e, 0x00, 0x00, 0x01, 0x1f, 0x48, 0x00, 0x62, 0x04, 0x1f, 0x00, 0x00, 0x4d, 0x1f, 0x48, 0x00, 0x53, 0x50, 0x1f, 0x00, 0x00, 0xdb, 0x0c, 0x00, 0xa2, 0xdc, 0x1f, 0x00, 0x00, 0x74, 0x20, 0x00, 0x00, 0x20, 0x41, 0x08, 0x00, 0x22, 0x98, 0x20, 0x24, 0x00, 0x00, 0x08, 0x00, 0x13, 0xc1, 0x0c, 0x00, 0x53, 0xc4, 0x20, 0x00, 0x00, 0xfe, 0x0c, 0x00, 0x00, 0x25, 0x14, 0x22, 0x17, 0x21, 0x54, 0x00, 0x50, 0x18, 0x21, 0x00, 0x00, 0xc4, 0xbf, 0x3c, 0x00, 0xcc, 0x11, 0x52, 0x22, 0x00, 0x00, 0x1b, 0x22, 0x18, 0x00, 0x80, 0x40, 0x22, 0x00, 0x00, 0x8b, 0x23, 0x00, 0x00, 0x82, 0x16, 0x62, 0x94, 0x23, 0x00, 0x00, 0xe5, 0x23, 0x18, 0x00, 0x62, 0xf8, 0x23, 0x00, 0x00, 0x53, 0x24, 0x4c, 0x02, 0x53, 0x54, 0x24, 0x00, 0x00, 0x90, 0x0c, 0x00, 0x00, 0x08, 0x00, 0x13, 0xcc, 0x0c, 0x00, 0x00, 0x08, 0x00, 0x50, 0x78, 0x26, 0x00, 0x00, 0x64, 0xd8, 0x00, 0x80, 0x27, 0x00, 0x00, 0x82, 0x27, 0x00, 0x00, 0x78, 0xd8, 0x00, 0xb0, 0x27, 0x00, 0x00, 0xa6, 0x27, 0x00, 0x00, 0x80, 0x41, 0x00, 0x00, 0xff, 0x13, 0x00, 0x6c, 0x18, 0x22, 0xb8, 0x40, 0x08, 0x00, 0x00, 0x78, 0x13, 0x22, 0xfc, 0x40, 0x08, 0x00, 0x40, 0x39, 0x28, 0x00, 0x00, 0xe8, 0x0d, 0x0f, 0x02, 0x00, 0xff, 0x38, 0x11, 0x01, 0x1c, 0x14, 0x12, 0x18, 0xfb, 0x16, 0x07, 0x02, 0x00, 0x02, 0xc6, 0x2a, 0x1f, 0x30, 0x18, 0x00, 0x00, 0x40, 0x09, 0x04, 0x00, 0x00, 0x9b, 0x25, 0x50, 0x60, 0x70, 0x00, 0x00, 0x7d, 0x35, 0x06, 0x0b, 0x02, 0x00, 0xf8, 0x66, 0x3c, 0x3f, 0x78, 0x6d, 0x6c, 0x20, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x3d, 0x27, 0x31, 0x2e, 0x30, 0x27, 0x20, 0x65, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3d, 0x27, 0x55, 0x54, 0x46, 0x2d, 0x38, 0x27, 0x20, 0x73, 0x74, 0x61, 0x6e, 0x64, 0x61, 0x6c, 0x6f, 0x6e, 0x65, 0x3d, 0x27, 0x79, 0x65, 0x73, 0x27, 0x3f, 0x3e, 0x0d, 0x0a, 0x3c, 0x61, 0x73, 0x73, 0x65, 0x6d, 0x62, 0x6c, 0x79, 0x20, 0x78, 0x6d, 0x6c, 0x6e, 0x73, 0x3d, 0x27, 0x75, 0x72, 0x6e, 0x3a, 0x73, 0x63, 0x68, 0x65, 0x6d, 0x61, 0x73, 0x2d, 0x6d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, 0x74, 0x2d, 0x63, 0x6f, 0x6d, 0x3a, 0x61, 0x73, 0x6d, 0x2e, 0x76, 0x31, 0x27, 0x20, 0x6d, 0x61, 0x6e, 0x69, 0x66, 0x65, 0x73, 0x74, 0x56, 0x6e, 0x00, 0xf3, 0x00, 0x3e, 0x0d, 0x0a, 0x20, 0x20, 0x3c, 0x74, 0x72, 0x75, 0x73, 0x74, 0x49, 0x6e, 0x66, 0x6f, 0x4e, 0x00, 0x1f, 0x22, 0x4e, 0x00, 0x0c, 0x21, 0x33, 0x22, 0x38, 0x00, 0x40, 0x20, 0x20, 0x3c, 0x73, 0xd8, 0x21, 0x33, 0x69, 0x74, 0x79, 0x10, 0x00, 0xf5, 0x07, 0x20, 0x20, 0x3c, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x65, 0x64, 0x50, 0x72, 0x69, 0x76, 0x69, 0x6c, 0x65, 0x67, 0x65, 0x73, 0x1d, 0x00, 0x08, 0x1f, 0x00, 0x03, 0x2b, 0x23, 0x90, 0x6f, 0x6e, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x20, 0x6c, 0x06, 0x00, 0xf5, 0x10, 0x3d, 0x27, 0x61, 0x73, 0x49, 0x6e, 0x76, 0x6f, 0x6b, 0x65, 0x72, 0x27, 0x20, 0x75, 0x69, 0x41, 0x63, 0x63, 0x65, 0x73, 0x73, 0x3d, 0x27, 0x66, 0x61, 0x6c, 0x73, 0x65, 0x27, 0x20, 0x2f, 0x48, 0x00, 0x2f, 0x3c, 0x2f, 0x66, 0x00, 0x07, 0x29, 0x3c, 0x2f, 0x94, 0x00, 0x25, 0x3c, 0x2f, 0xdd, 0x00, 0x54, 0x3e, 0x0d, 0x0a, 0x3c, 0x2f, 0x38, 0x01, 0x3b, 0x3e, 0x0d, 0x0a, 0x8c, 0x01, 0x0f, 0x02, 0x00, 0x02, 0x12, 0x30, 0xa4, 0x18, 0xf2, 0x47, 0x50, 0xa2, 0x58, 0xa2, 0x60, 0xa2, 0x68, 0xa2, 0x70, 0xa2, 0x80, 0xa2, 0x90, 0xa2, 0xa8, 0xa2, 0xb0, 0xa2, 0xe0, 0xa2, 0xe8, 0xa2, 0xf0, 0xa2, 0xf8, 0xa2, 0x00, 0xa3, 0x08, 0xa3, 0x10, 0xa3, 0x18, 0xa3, 0x20, 0xa3, 0x28, 0xa3, 0x40, 0xa3, 0x48, 0xa3, 0x50, 0xa3, 0x88, 0xa5, 0x90, 0xa5, 0x98, 0xa5, 0xa0, 0xa5, 0xa8, 0xa5, 0xb0, 0xa5, 0xb8, 0xa5, 0xc0, 0xa5, 0xc8, 0xa5, 0xd8, 0xa5, 0xe0, 0xa5, 0xe8, 0xa5, 0x48, 0xa6, 0x60, 0xa6, 0x68, 0xa6, 0xf0, 0xa6, 0x08, 0xa7, 0x10, 0xa7, 0x18, 0xa7, 0x20, 0xa7, 0x28, 0xa7, 0x58, 0x19, 0x00, 0x54, 0x02, 0xff, 0x01, 0x38, 0xa0, 0x60, 0xa0, 0x88, 0xa0, 0xb8, 0xa0, 0xd8, 0xa0, 0xf8, 0xa0, 0x18, 0xa1, 0x38, 0xa1, 0x8c, 0x00, 0x02, 0x0f, 0x02, 0x00, 0xff, 0x5c, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xd9, 0x57, 0x9e]; + +#[cfg(test)] +mod tests { + + use std::io::Read; + use std::collections::BTreeSet; + use std::collections::BTreeMap; + use lz4; + use super::DATA; + use binlex::formats::PE; + use binlex::Architecture; + use binlex::Config; + + #[test] + fn test_formats_pe() { + let config = Config::new(); + let mut decoder = lz4::Decoder::new(DATA).expect("failed to create lz4 decoder"); + let mut data = Vec::new(); + let result = decoder.read_to_end(&mut data); + assert!(result.is_ok(), "failed to lz4 decompress pe file"); + let pe_result = PE::from_bytes(data, config); + assert!(pe_result.is_ok(), "failed to parse pe file"); + let pe = pe_result.unwrap(); + assert_eq!(pe.entrypoint(), 0x140001cd0, "incorrect pe entrypoint"); + let mut executable_address_ranges = BTreeMap::::new(); + executable_address_ranges.insert(0x140001000, 0x140002839); + assert_eq!(pe.executable_virtual_address_ranges(), executable_address_ranges, "pe execuable address ranges are incorrect"); + assert_eq!(pe.architecture(), Architecture::AMD64, "incorrect pe architecture"); + let sha256_result = pe.sha256(); + assert!(sha256_result.is_some(), "sha256 of pe should not be none"); + assert_eq!(sha256_result.unwrap(), "227f75802f50956a31c7623932fdc640706ae1b9f65b1f628ea3e6d8e759c7ec", "the pe sha256 does not match"); + let tlsh_result = pe.tlsh(); + assert!(tlsh_result.is_some(), "tlsh of pe should not be none"); + assert_eq!(tlsh_result.unwrap(), "T1F682290A774B88E6D226923EC5638F18E272F51257626BCFA362439D0FB13D06D37D45", "the pe tlsh does not match"); + assert_eq!(pe.imagebase(), 0x140000000, "the pe imagebase is incorrect"); + assert_eq!(pe.sizeofheaders(), 1024, "the pe header size is incorrect"); + assert_eq!(pe.size(), 18432, "the size of the pe file in bytes is incorrect"); + let set: BTreeSet = [ + 0x140001180, + 0x1400012a0, + 0x1400013b0, + 0x140001420, + 0x140001450, + 0x140001480, + 0x1400014b0, + 0x140001530, + ] + .into_iter() + .collect(); + assert_eq!(pe.exports(), set, "missing pe exports"); + + } +} diff --git a/tests/test_types.rs b/tests/test_types.rs new file mode 100644 index 00000000..0d6b6724 --- /dev/null +++ b/tests/test_types.rs @@ -0,0 +1,9 @@ +#[cfg(test)] +mod tests { + use binlex::types::lz4string::LZ4String; + #[test] + fn test_types_lz4string() { + let result = LZ4String::new("test"); + assert_eq!(result.to_string(), "test", "string failed to decompress correctly"); + } +} diff --git a/tests/tests.py b/tests/tests.py deleted file mode 100755 index 4d5d8315..00000000 --- a/tests/tests.py +++ /dev/null @@ -1,33 +0,0 @@ -#!/usr/bin/env python - -import sys -import json -import pybinlex - -raw = pybinlex.Raw() -raw.set_architecture( - pybinlex.BINARY_ARCH.BINARY_ARCH_X86, - pybinlex.BINARY_MODE.BINARY_MODE_32) -result = raw.read_file('../tests/raw/raw.x86') -if result is False: sys.exit(1) -disassembler = pybinlex.Disassembler(raw) -disassembler.disassemble() -traits = disassembler.get_traits() -print(json.dumps(traits, indent=4)) - -pe = pybinlex.PE() -result = pe.read_file('../tests/pe/pe.x86') -if result is False: sys.exit(1) -pe_sections = pe.get_sections() -disassembler = pybinlex.Disassembler(pe) -disassembler.disassemble() -traits = disassembler.get_traits() -print(json.dumps(traits, indent=4)) - -elf = pybinlex.ELF() -result = elf.read_file('../tests/elf/elf.x86') -if result is False: sys.exit(1) -disassembler = pybinlex.Disassembler(elf) -disassembler.disassemble() -traits = disassembler.get_traits() -print(json.dumps(traits, indent=4)) diff --git a/tests/tsmda.py b/tests/tsmda.py deleted file mode 100755 index 6952c419..00000000 --- a/tests/tsmda.py +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env python - -import json -import sys -import os -from smda.Disassembler import Disassembler - -disassembler = Disassembler() -report = disassembler.disassembleFile(sys.argv[1]) -json_report = report.toDict()