aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEd Maste <emaste@FreeBSD.org>2021-10-01 23:46:00 +0000
committerEd Maste <emaste@FreeBSD.org>2021-10-01 23:46:00 +0000
commit5b2defbd2a1aa991bd0a2855eef8e15107572747 (patch)
tree0e8b33a28f1fb7396bf8b6fcef974fa82de59e9d
downloadsrc-vendor/libcbor.tar.gz
src-vendor/libcbor.zip
Vendor import of libcbor 0.8.0vendor/libcbor
-rw-r--r--.clang-format151
-rw-r--r--.github/workflows/fuzz.yml23
-rw-r--r--.gitignore8
-rw-r--r--.gitmodules3
-rw-r--r--.travis.yml66
-rw-r--r--CHANGELOG.md109
-rw-r--r--CMakeLists.txt159
-rw-r--r--CMakeModules/FindCJSON.cmake9
-rw-r--r--CMakeModules/FindCMocka.cmake49
-rw-r--r--CMakeModules/LibFindMacros.cmake120
-rw-r--r--Doxyfile2417
-rw-r--r--LICENSE.md21
-rw-r--r--README.md122
-rw-r--r--appveyor.yml27
-rwxr-xr-xclang-format.sh17
-rw-r--r--codecov.yml2
-rw-r--r--doc/Makefile180
-rw-r--r--doc/doxy_frontpage.md37
-rw-r--r--doc/make.bat242
-rw-r--r--doc/source/api.rst39
-rw-r--r--doc/source/api/decoding.rst53
-rw-r--r--doc/source/api/encoding.rst23
-rw-r--r--doc/source/api/item_reference_counting.rst41
-rw-r--r--doc/source/api/item_types.rst41
-rw-r--r--doc/source/api/type_0_1.rst91
-rw-r--r--doc/source/api/type_2.rst66
-rw-r--r--doc/source/api/type_3.rst55
-rw-r--r--doc/source/api/type_4.rst62
-rw-r--r--doc/source/api/type_5.rst44
-rw-r--r--doc/source/api/type_6.rst21
-rw-r--r--doc/source/api/type_7.rst75
-rw-r--r--doc/source/changelog.rst4
-rw-r--r--doc/source/conf.py296
-rw-r--r--doc/source/development.rst143
-rw-r--r--doc/source/getting_started.rst190
-rw-r--r--doc/source/img/win_1.pngbin0 -> 29291 bytes
-rw-r--r--doc/source/img/win_2.pngbin0 -> 20179 bytes
-rw-r--r--doc/source/img/win_3.pngbin0 -> 47224 bytes
-rw-r--r--doc/source/img/win_4.pngbin0 -> 11272 bytes
-rw-r--r--doc/source/img/win_5.pngbin0 -> 29490 bytes
-rw-r--r--doc/source/img/win_6.pngbin0 -> 25195 bytes
-rw-r--r--doc/source/index.rst40
-rw-r--r--doc/source/internal.rst128
-rw-r--r--doc/source/requirements.txt39
-rw-r--r--doc/source/rfc_conformance.rst17
-rw-r--r--doc/source/streaming.rst13
-rw-r--r--doc/source/streaming/decoding.rst40
-rw-r--r--doc/source/streaming/encoding.rst4
-rw-r--r--doc/source/tests.rst39
-rw-r--r--doc/source/using.rst173
-rw-r--r--examples/CMakeLists.txt25
-rw-r--r--examples/cjson2cbor.c145
-rw-r--r--examples/create_items.c33
-rw-r--r--examples/data/floats.cborbin0 -> 39 bytes
-rw-r--r--examples/data/indef_string.cbor1
-rw-r--r--examples/data/integer.cbor1
-rw-r--r--examples/data/json_example.json6
-rw-r--r--examples/data/map.cborbin0 -> 26 bytes
-rw-r--r--examples/data/nested_array.cbor1
-rw-r--r--examples/data/tagged_date.cbor1
-rw-r--r--examples/hello.c16
-rw-r--r--examples/readfile.c78
-rw-r--r--examples/sort.c41
-rw-r--r--examples/streaming_parser.c58
-rwxr-xr-xmisc/bytes.rb3
-rwxr-xr-xmisc/file_to_bytes.rb5
-rwxr-xr-xmisc/hooks/pre-commit18
-rwxr-xr-xmisc/repeat.rb3
-rwxr-xr-xmisc/seq.rb3
-rwxr-xr-xmisc/update_version.py38
-rwxr-xr-xoss-fuzz/build.sh28
-rw-r--r--oss-fuzz/cbor_load_fuzzer.cc37
-rwxr-xr-xrelease.sh75
-rw-r--r--src/CMakeLists.txt45
-rw-r--r--src/allocators.c19
-rw-r--r--src/cbor.c345
-rw-r--r--src/cbor.h68
-rw-r--r--src/cbor/arrays.c135
-rw-r--r--src/cbor/arrays.h116
-rw-r--r--src/cbor/bytestrings.c117
-rw-r--r--src/cbor/bytestrings.h132
-rw-r--r--src/cbor/callbacks.c116
-rw-r--r--src/cbor/callbacks.h187
-rw-r--r--src/cbor/common.c160
-rw-r--r--src/cbor/common.h303
-rw-r--r--src/cbor/configuration.h.in16
-rw-r--r--src/cbor/data.h255
-rw-r--r--src/cbor/encoding.c192
-rw-r--r--src/cbor/encoding.h109
-rw-r--r--src/cbor/floats_ctrls.c189
-rw-r--r--src/cbor/floats_ctrls.h212
-rw-r--r--src/cbor/internal/builder_callbacks.c389
-rw-r--r--src/cbor/internal/builder_callbacks.h81
-rw-r--r--src/cbor/internal/encoders.c98
-rw-r--r--src/cbor/internal/encoders.h36
-rw-r--r--src/cbor/internal/loaders.c80
-rw-r--r--src/cbor/internal/loaders.h36
-rw-r--r--src/cbor/internal/memory_utils.c44
-rw-r--r--src/cbor/internal/memory_utils.h39
-rw-r--r--src/cbor/internal/stack.c33
-rw-r--r--src/cbor/internal/stack.h41
-rw-r--r--src/cbor/internal/unicode.c94
-rw-r--r--src/cbor/internal/unicode.h32
-rw-r--r--src/cbor/ints.c191
-rw-r--r--src/cbor/ints.h210
-rw-r--r--src/cbor/maps.c125
-rw-r--r--src/cbor/maps.h112
-rw-r--r--src/cbor/serialization.c286
-rw-r--r--src/cbor/serialization.h135
-rw-r--r--src/cbor/streaming.c664
-rw-r--r--src/cbor/streaming.h37
-rw-r--r--src/cbor/strings.c133
-rw-r--r--src/cbor/strings.h153
-rw-r--r--src/cbor/tags.c43
-rw-r--r--src/cbor/tags.h65
-rw-r--r--src/libcbor.pc.in11
-rw-r--r--test/CMakeLists.txt29
-rw-r--r--test/assertions.c53
-rw-r--r--test/assertions.h34
-rw-r--r--test/bad_inputs_test.c121
-rw-r--r--test/callbacks_test.c40
-rw-r--r--test/cbor_serialize_test.c323
-rw-r--r--test/cbor_stream_decode_test.c599
-rw-r--r--test/copy_test.c207
-rw-r--r--test/cpp_linkage_test.cpp11
-rw-r--r--test/data/callbacks.cborbin0 -> 79 bytes
-rw-r--r--test/data/callbacks.diag1
-rw-r--r--test/data/pretty.cbor3
-rw-r--r--test/data/pretty.diag1
-rw-r--r--test/fuzz_test.c87
-rw-r--r--test/memory_allocation_test.c277
-rw-r--r--test/pretty_printer_test.c43
-rw-r--r--test/stack_over_limit_test.c35
-rw-r--r--test/stream_expectations.c317
-rw-r--r--test/stream_expectations.h151
-rw-r--r--test/type_0_encoders_test.c74
-rw-r--r--test/type_0_test.c147
-rw-r--r--test/type_1_encoders_test.c75
-rw-r--r--test/type_1_test.c102
-rw-r--r--test/type_2_encoders_test.c41
-rw-r--r--test/type_2_test.c327
-rw-r--r--test/type_3_encoders_test.c41
-rw-r--r--test/type_3_test.c242
-rw-r--r--test/type_4_encoders_test.c55
-rw-r--r--test/type_4_test.c127
-rw-r--r--test/type_5_encoders_test.c40
-rw-r--r--test/type_5_test.c190
-rw-r--r--test/type_6_encoders_test.c35
-rw-r--r--test/type_6_test.c117
-rw-r--r--test/type_7_encoders_test.c109
-rw-r--r--test/type_7_test.c111
-rw-r--r--test/unicode_test.c41
152 files changed, 16205 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 000000000000..de0dc5e77907
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,151 @@
+---
+Language: Cpp
+# BasedOnStyle: Google
+AccessModifierOffset: -1
+AlignAfterOpenBracket: Align
+AlignConsecutiveAssignments: false
+AlignConsecutiveDeclarations: false
+AlignEscapedNewlines: Left
+AlignOperands: true
+AlignTrailingComments: true
+AllowAllParametersOfDeclarationOnNextLine: true
+AllowShortBlocksOnASingleLine: false
+AllowShortCaseLabelsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: All
+AllowShortIfStatementsOnASingleLine: true
+AllowShortLoopsOnASingleLine: true
+AlwaysBreakAfterDefinitionReturnType: None
+AlwaysBreakAfterReturnType: None
+AlwaysBreakBeforeMultilineStrings: true
+AlwaysBreakTemplateDeclarations: true
+BinPackArguments: true
+BinPackParameters: true
+BraceWrapping:
+ AfterClass: false
+ AfterControlStatement: false
+ AfterEnum: false
+ AfterFunction: false
+ AfterNamespace: false
+ AfterObjCDeclaration: false
+ AfterStruct: false
+ AfterUnion: false
+ AfterExternBlock: false
+ BeforeCatch: false
+ BeforeElse: false
+ IndentBraces: false
+ SplitEmptyFunction: true
+ SplitEmptyRecord: true
+ SplitEmptyNamespace: true
+BreakBeforeBinaryOperators: None
+BreakBeforeBraces: Attach
+BreakBeforeInheritanceComma: false
+BreakInheritanceList: BeforeColon
+BreakBeforeTernaryOperators: true
+BreakConstructorInitializersBeforeComma: false
+BreakConstructorInitializers: BeforeColon
+BreakAfterJavaFieldAnnotations: false
+BreakStringLiterals: true
+ColumnLimit: 80
+CommentPragmas: '^ IWYU pragma:'
+CompactNamespaces: false
+ConstructorInitializerAllOnOneLineOrOnePerLine: true
+ConstructorInitializerIndentWidth: 4
+ContinuationIndentWidth: 4
+Cpp11BracedListStyle: true
+DerivePointerAlignment: true
+DisableFormat: false
+ExperimentalAutoDetectBinPacking: false
+FixNamespaceComments: true
+ForEachMacros:
+ - foreach
+ - Q_FOREACH
+ - BOOST_FOREACH
+IncludeBlocks: Preserve
+IncludeCategories:
+ - Regex: '^<ext/.*\.h>'
+ Priority: 2
+ - Regex: '^<.*\.h>'
+ Priority: 1
+ - Regex: '^<.*'
+ Priority: 2
+ - Regex: '.*'
+ Priority: 3
+IncludeIsMainRegex: '([-_](test|unittest))?$'
+IndentCaseLabels: true
+IndentPPDirectives: None
+IndentWidth: 2
+IndentWrappedFunctionNames: false
+JavaScriptQuotes: Leave
+JavaScriptWrapImports: true
+KeepEmptyLinesAtTheStartOfBlocks: false
+MacroBlockBegin: ''
+MacroBlockEnd: ''
+MaxEmptyLinesToKeep: 1
+NamespaceIndentation: None
+ObjCBinPackProtocolList: Never
+ObjCBlockIndentWidth: 2
+ObjCSpaceAfterProperty: false
+ObjCSpaceBeforeProtocolList: true
+PenaltyBreakAssignment: 2
+PenaltyBreakBeforeFirstCallParameter: 1
+PenaltyBreakComment: 300
+PenaltyBreakFirstLessLess: 120
+PenaltyBreakString: 1000
+PenaltyBreakTemplateDeclaration: 10
+PenaltyExcessCharacter: 1000000
+PenaltyReturnTypeOnItsOwnLine: 200
+PointerAlignment: Left
+RawStringFormats:
+ - Language: Cpp
+ Delimiters:
+ - cc
+ - CC
+ - cpp
+ - Cpp
+ - CPP
+ - 'c++'
+ - 'C++'
+ CanonicalDelimiter: ''
+ BasedOnStyle: google
+ - Language: TextProto
+ Delimiters:
+ - pb
+ - PB
+ - proto
+ - PROTO
+ EnclosingFunctions:
+ - EqualsProto
+ - EquivToProto
+ - PARSE_PARTIAL_TEXT_PROTO
+ - PARSE_TEST_PROTO
+ - PARSE_TEXT_PROTO
+ - ParseTextOrDie
+ - ParseTextProtoOrDie
+ CanonicalDelimiter: ''
+ BasedOnStyle: google
+ReflowComments: true
+SortIncludes: true
+SortUsingDeclarations: true
+SpaceAfterCStyleCast: false
+SpaceAfterTemplateKeyword: true
+SpaceBeforeAssignmentOperators: true
+SpaceBeforeCpp11BracedList: false
+SpaceBeforeCtorInitializerColon: true
+SpaceBeforeInheritanceColon: true
+SpaceBeforeParens: ControlStatements
+SpaceBeforeRangeBasedForLoopColon: true
+SpaceInEmptyParentheses: false
+SpacesBeforeTrailingComments: 2
+SpacesInAngles: false
+SpacesInContainerLiterals: true
+SpacesInCStyleCastParentheses: false
+SpacesInParentheses: false
+SpacesInSquareBrackets: false
+Standard: Auto
+StatementMacros:
+ - Q_UNUSED
+ - QT_REQUIRE_VERSION
+TabWidth: 8
+UseTab: Never
+...
+
diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml
new file mode 100644
index 000000000000..c5275121868f
--- /dev/null
+++ b/.github/workflows/fuzz.yml
@@ -0,0 +1,23 @@
+name: CIFuzz
+on: [pull_request]
+jobs:
+ Fuzzing:
+ runs-on: ubuntu-latest
+ steps:
+ - name: Build Fuzzers
+ uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master
+ with:
+ oss-fuzz-project-name: 'libcbor'
+ dry-run: false
+ - name: Run Fuzzers
+ uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master
+ with:
+ oss-fuzz-project-name: 'libcbor'
+ fuzz-seconds: 600
+ dry-run: false
+ - name: Upload Crash
+ uses: actions/upload-artifact@v1
+ if: failure()
+ with:
+ name: artifacts
+ path: ./out/artifacts
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 000000000000..b6fe00f938ad
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,8 @@
+build
+*~
+nbproject
+.idea
+doxygen_docs
+*/*.out
+cmake-build-debug
+venv
diff --git a/.gitmodules b/.gitmodules
new file mode 100644
index 000000000000..b1f6f9c259a8
--- /dev/null
+++ b/.gitmodules
@@ -0,0 +1,3 @@
+[submodule "doxygen-theme"]
+ path = doxygen-theme
+ url = https://github.com/nnen/doxygen-theme.git
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 000000000000..288de2903399
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,66 @@
+language: c
+
+matrix:
+ include:
+ - arch: amd64
+ os: linux
+ dist: bionic
+ compiler: clang
+ env: TRAVIS_ARCH="amd64"
+ - arch: amd64
+ os: linux
+ dist: bionic
+ compiler: gcc
+ env: TRAVIS_ARCH="amd64"
+ - arch: arm64
+ os: linux
+ dist: bionic
+ compiler: gcc
+ env: TRAVIS_ARCH="arm64"
+ - arch: ppc64le
+ os: linux
+ dist: bionic
+ compiler: gcc
+ env: TRAVIS_ARCH="ppc64le"
+ - arch: amd64
+ os: osx
+ compiler: gcc
+ env: TRAVIS_ARCH="amd64"
+
+before_install:
+ - pushd ${HOME}
+ - git clone https://gitlab.com/cmocka/cmocka.git
+ - cd cmocka && mkdir build && cd build
+ - cmake .. && make -j2 && sudo make install
+ - cd .. && popd
+ - if [ "$TRAVIS_OS_NAME" = "linux" ]; then sudo apt-get update -qq; sudo apt-get install -y clang-format-8 cppcheck; fi
+ - if [ "$TRAVIS_OS_NAME" = "linux" -a "$CC" = "gcc" -a "$TRAVIS_ARCH" = "amd64" ]; then pip install --user codecov; export CFLAGS="-coverage"; fi
+
+script:
+ - >
+ if [ "$TRAVIS_OS_NAME" = "linux" ]; then
+ cppcheck . --error-exitcode=1
+ # Fail if re-formatting creates diffs (implying bad formatting)
+ /clang-format.sh --verbose
+ git diff-index --quiet HEAD
+ fi;
+ - mkdir build && cd build
+ - cmake -DWITH_TESTS=ON
+ -DCBOR_CUSTOM_ALLOC=ON
+ -DCMAKE_BUILD_TYPE=Debug
+ -DSANITIZE=OFF
+ ..
+ - make VERBOSE=1
+ - ctest -VV
+ - ctest -T memcheck | tee memcheck.out
+ - >
+ if grep -q 'Memory Leak\|IPW\|Uninitialized Memory Conditional\|Uninitialized Memory Read' memcheck.out; then
+ exit 1
+ fi;
+
+
+after_success:
+ - if [ "$TRAVIS_OS_NAME" = "linux" -a "$CC" = "gcc" -a "$TRAVIS_ARCH" = "amd64" ]; then codecov; fi
+
+notifications:
+ email: false
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 000000000000..0f13a45d6b24
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,109 @@
+Next
+---------------------
+
+0.8.0 (2020-09-20)
+---------------------
+- BUILD BREAKING: Use BUILD_SHARED_LIBS to determine how to build libraries (fixed Windows linkage) [[#148]](https://github.com/PJK/libcbor/pull/148) (by [intelligide@](https://github.com/intelligide))
+- BREAKING: Fix `cbor_tag_item` not increasing the reference count on the tagged item reference it returns [[Fixes #109](https://github.com/PJK/libcbor/issues/109)] (discovered bt [JohnGilmour](https://github.com/JohnGilmour))
+ - If you have previously relied on the broken behavior, you can use `cbor_move` to emulate as long as the returned handle is an "rvalue"
+- BREAKING: [`CBOR_DECODER_EBUFFER` removed from `cbor_decoder_status`](https://github.com/PJK/libcbor/pull/156)
+ - `cbor_stream_decode` will set `CBOR_DECODER_NEDATA` instead if the input buffer is empty
+- [Fix `cbor_stream_decode`](https://github.com/PJK/libcbor/pull/156) to set `cbor_decoder_result.required` to the minimum number of input bytes necessary to receive the next callback (as long as at least one byte was passed) (discovered by [woefulwabbit](https://github.com/woefulwabbit))
+- Fixed several minor manpage issues [[#159]](https://github.com/PJK/libcbor/pull/159) (discovered by [kloczek@](https://github.com/kloczek))
+
+0.7.0 (2020-04-25)
+---------------------
+- Fix bad encoding of NaN half-floats [[Fixes #53]](https://github.com/PJK/libcbor/issues/53) (discovered by [BSipos-RKF](https://github.com/BSipos-RKF))
+ - **Warning**: Previous versions encoded NaNs as `0xf9e700` instead of `0xf97e00`; if you rely on the broken behavior, this will be a breaking change
+- Fix potentially bad encoding of negative half-float with exponent < -14 [[Fixes #112]](https://github.com/PJK/libcbor/issues/112) (discovered by [yami36](https://github.com/yami36))
+- BREAKING: Improved bool support [[Fixes #63]](https://github.com/PJK/libcbor/issues/63)
+ - Rename `cbor_ctrl_is_bool` to `cbor_get_bool` and fix the behavior
+ - Add `cbor_set_bool`
+- Fix memory_allocation_test breaking the build without CBOR_CUSTOM_ALLOC [[Fixes #128]](https://github.com/PJK/libcbor/issues/128) (by [panlinux](https://github.com/panlinux))
+- [Fix a potential build issue where cJSON includes may be misconfigured](https://github.com/PJK/libcbor/pull/132)
+- Breaking: [Add a limit on the size of the decoding context stack](https://github.com/PJK/libcbor/pull/138) (by [James-ZHANG](https://github.com/James-ZHANG))
+ - If your usecase requires parsing very deeply nested structures, you might need to increase the default 2k limit via `CBOR_MAX_STACK_SIZE`
+- Enable LTO/IPO based on [CheckIPOSupported](https://cmake.org/cmake/help/latest/module/CheckIPOSupported.html#module:CheckIPOSupported) [[#143]](https://github.com/PJK/libcbor/pull/143) (by [xanderlent](https://github.com/xanderlent))
+ - If you rely on LTO being enabled and use CMake version older than 3.9, you will need to re-enable it manually or upgrade your CMake
+
+0.6.1 (2020-03-26)
+---------------------
+- [Fix bad shared library version number](https://github.com/PJK/libcbor/pull/131)
+ - **Warning**: Shared library built from the 0.6.0 release is erroneously marked as version "0.6.0", which makes it incompatible with future releases *including the v0.6.X line* even though they may be compatible API/ABI-wise. Refer to the documentation for the new SO versioning scheme.
+
+0.6.0 (2020-03-15)
+---------------------
+- Correctly set .so version [[Fixes #52]](https://github.com/PJK/libcbor/issues/52).
+ - **Warning**: All previous releases will be identified as 0.0 by the linker.
+- Fix & prevent heap overflow error in example code [[#74]](https://github.com/PJK/libcbor/pull/74) [[#76]](https://github.com/PJK/libcbor/pull/76) (by @nevun)
+- Correctly set OSX dynamic library version [[Fixes #75]](https://github.com/PJK/libcbor/issues/75)
+- [Fix misplaced 0xFF bytes in maps possibly causing memory corruption](https://github.com/PJK/libcbor/pull/82)
+- BREAKING: Fix handling & cleanup of failed memory allocation in constructor
+ and builder helper functions [[Fixes #84]](https://github.com/PJK/libcbor/issues/84)
+ - All cbor_new_* and cbor_build_* functions will now explicitly return NULL when memory allocation fails
+ - It is up to the client to handle such cases
+- Globally enforced code style [[Fixes #83]](https://github.com/PJK/libcbor/issues/83)
+- Fix issue possible memory corruption bug on repeated
+ cbor_(byte)string_add_chunk calls with intermittently failing realloc calls
+- Fix possibly misaligned reads and writes when endian.h is uses or when
+ running on a big-endian machine [[Fixes #99](https://github.com/PJK/libcbor/issues/99), [#100](https://github.com/PJK/libcbor/issues/100)]
+- [Improved CI setup with Travis-native arm64 support](https://github.com/PJK/libcbor/pull/116)
+- [Docs migrated to Sphinx 2.4 and Python3](https://github.com/PJK/libcbor/pull/117)
+
+0.5.0 (2017-02-06)
+---------------------
+- Remove cmocka from the subtree (always rely on system or user-provided version)
+- Windows CI
+- Only build tests if explicitly enabled (`-DWITH_TESTS=ON`)
+- Fixed static header declarations (by cedric-d)
+- Improved documentation (by Michael Richardson)
+- Improved `examples/readfile.c`
+- Reworked (re)allocation to handle huge inputs and overflows in size_t [[Fixes #16]](https://github.com/PJK/libcbor/issues/16)
+- Improvements to C++ linkage (corrected `cbor_empty_callbacks`, fixed `restrict` pointers) (by Dennis Bijwaard)
+- Fixed Linux installation directory depending on architecture [[Fixes #34]](https://github.com/PJK/libcbor/issues/34) (by jvymazal)
+- Improved 32-bit support [[Fixes #35]](https://github.com/PJK/libcbor/issues/35)
+- Fixed MSVC compatibility [[Fixes #31]](https://github.com/PJK/libcbor/issues/31)
+- Fixed and improved half-float encoding [[Fixes #5](https://github.com/PJK/libcbor/issues/5), [#11](https://github.com/PJK/libcbor/issues/11)]
+
+0.4.0 (2015-12-25)
+---------------------
+Breaks build & header compatibility due to:
+
+- Improved build configuration and feature check macros
+- Endianess configuration fixes (by Erwin Kroon and David Grigsby)
+- pkg-config compatibility (by Vincent Bernat)
+- enable use of versioned SONAME (by Vincent Bernat)
+- better fuzzer (wasn't random until now, ooops)
+
+0.3.1 (2015-05-21)
+---------------------
+- documentation and comments improvements, mostly for the API reference
+
+0.3.0 (2015-05-21)
+---------------------
+
+- Fixes, polishing, niceties across the code base
+- Updated examples
+- `cbor_copy`
+- `cbor_build_negint8`, 16, 32, 64, matching asserts
+- `cbor_build_stringn`
+- `cbor_build_tag`
+- `cbor_build_float2`, ...
+
+0.2.1 (2015-05-17)
+---------------------
+- C99 support
+
+0.2.0 (2015-05-17)
+---------------------
+
+- `cbor_ctrl_bool` -> `cbor_ctrl_is_bool`
+- Added `cbor_array_allocated` & map equivalent
+- Overhauled endianess conversion - ARM now works as expected
+- 'sort.c' example added
+- Significantly improved and doxyfied documentation
+
+0.1.0 (2015-05-06)
+---------------------
+
+The initial release, yay!
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 000000000000..edba03720d2f
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,159 @@
+cmake_minimum_required(VERSION 2.8)
+project(libcbor)
+set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
+include(CTest)
+
+SET(CBOR_VERSION_MAJOR "0")
+SET(CBOR_VERSION_MINOR "8")
+SET(CBOR_VERSION_PATCH "0")
+SET(CBOR_VERSION ${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR}.${CBOR_VERSION_PATCH})
+
+set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY true)
+include(CheckIncludeFiles)
+
+include(TestBigEndian)
+test_big_endian(BIG_ENDIAN)
+if(BIG_ENDIAN)
+ add_definitions(-DIS_BIG_ENDIAN)
+endif()
+
+option(CBOR_CUSTOM_ALLOC "Custom, dynamically defined allocator support" OFF)
+option(CBOR_PRETTY_PRINTER "Include a pretty-printing routine" ON)
+set(CBOR_BUFFER_GROWTH "2" CACHE STRING "Factor for buffer growth & shrinking")
+set(CBOR_MAX_STACK_SIZE "2048" CACHE STRING "maximum size for decoding context stack")
+
+option(WITH_TESTS "[TEST] Build unit tests (requires CMocka)" OFF)
+if(WITH_TESTS)
+ add_definitions(-DWITH_TESTS)
+endif(WITH_TESTS)
+
+option(WITH_EXAMPLES "Build examples" ON)
+
+option(HUGE_FUZZ "[TEST] Fuzz through 8GB of data in the test. Do not use with memory instrumentation!" OFF)
+if(HUGE_FUZZ)
+ add_definitions(-DHUGE_FUZZ)
+endif(HUGE_FUZZ)
+
+option(SANE_MALLOC "[TEST] Assume that malloc will not allocate multi-GB blocks. Tests only, platform specific" OFF)
+if(SANE_MALLOC)
+ add_definitions(-DSANE_MALLOC)
+endif(SANE_MALLOC)
+
+option(PRINT_FUZZ "[TEST] Print the fuzzer input" OFF)
+if(PRINT_FUZZ)
+ add_definitions(-DPRINT_FUZZ)
+endif(PRINT_FUZZ)
+
+option(SANITIZE "Enable ASan & a few compatible sanitizers in Debug mode" ON)
+
+set(CPACK_GENERATOR "DEB" "TGZ" "RPM")
+set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")
+set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Pavel Kalvoda")
+set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6")
+set(CPACK_PACKAGE_VERSION_MAJOR ${CBOR_VERSION_MAJOR})
+set(CPACK_PACKAGE_VERSION_MINOR ${CBOR_VERSION_MINOR})
+set(CPACK_PACKAGE_VERSION_PATCH ${CBOR_VERSION_PATCH})
+
+include(CPack)
+
+if(MINGW)
+ # https://github.com/PJK/libcbor/issues/13
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
+elseif(NOT MSVC)
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pedantic")
+endif()
+
+if(MSVC)
+ # This just doesn't work right -- https://msdn.microsoft.com/en-us/library/5ft82fed.aspx
+ set(CBOR_RESTRICT_SPECIFIER "")
+else()
+ set(CBOR_RESTRICT_SPECIFIER "restrict")
+
+ set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -g -ggdb -DDEBUG=true")
+ set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Wall -DNDEBUG")
+
+ if(SANITIZE)
+ set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} \
+ -fsanitize=undefined -fsanitize=address \
+ -fsanitize=bounds -fsanitize=alignment")
+ endif()
+endif()
+
+set(CMAKE_EXE_LINKER_FLAGS_DEBUG "-g")
+
+
+include(CheckTypeSize)
+check_type_size("size_t" SIZEOF_SIZE_T)
+if(SIZEOF_SIZE_T LESS 8)
+ message(WARNING "Your size_t is less than 8 bytes. Long items with 64b length specifiers might not work as expected. Make sure to run the tests!")
+else()
+ add_definitions(-DEIGHT_BYTE_SIZE_T)
+endif()
+
+enable_testing()
+
+set(CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind")
+set(MEMORYCHECK_COMMAND_OPTIONS "--tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=1")
+
+add_custom_target(coverage
+ COMMAND ctest
+ COMMAND lcov --capture --directory . --output-file coverage.info
+ COMMAND genhtml coverage.info --highlight --legend --output-directory coverage_html
+ COMMAND echo "Coverage report ready: file://${CMAKE_CURRENT_BINARY_DIR}/coverage_html/index.html")
+include_directories(src)
+
+
+option(COVERAGE "Enable code coverage instrumentation" OFF)
+if (COVERAGE)
+ message("Configuring code coverage instrumentation")
+ if(NOT CMAKE_C_COMPILER MATCHES "gcc")
+ message(WARNING "Gcov instrumentation only works with GCC")
+ endif()
+ # https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
+ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fprofile-arcs -ftest-coverage --coverage")
+ set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage --coverage")
+endif (COVERAGE)
+
+
+# We want to generate configuration.h from the template and make it so that it is accessible using the same
+# path during both library build and installed header use, without littering the source dir.
+# Using cbor/configuration.h in the build dir works b/c headers will be installed to <prefix>/cbor
+configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/cbor/configuration.h.in ${PROJECT_BINARY_DIR}/cbor/configuration.h)
+install(FILES ${PROJECT_BINARY_DIR}/cbor/configuration.h DESTINATION include/cbor)
+# Make the header visible at compile time
+include_directories(${PROJECT_BINARY_DIR})
+
+# CMake >= 3.9.0 enables LTO for GCC and Clang with INTERPROCEDURAL_OPTIMIZATION
+# Policy CMP0069 enables this behavior when we set the minimum CMake version < 3.9.0
+# Checking for LTO support before setting INTERPROCEDURAL_OPTIMIZATION is mandatory with CMP0069 set to NEW.
+set(use_lto FALSE)
+if(${CMAKE_VERSION} VERSION_GREATER "3.9.0" OR ${CMAKE_VERSION} VERSION_EQUAL "3.9.0")
+ cmake_policy(SET CMP0069 NEW)
+ # Require LTO support to build libcbor with newer CMake versions
+ include(CheckIPOSupported)
+ check_ipo_supported(RESULT use_lto)
+endif(${CMAKE_VERSION} VERSION_GREATER "3.9.0" OR ${CMAKE_VERSION} VERSION_EQUAL "3.9.0")
+if(use_lto)
+ message(STATUS "LTO is enabled")
+else()
+ message(STATUS "LTO is not enabled")
+endif(use_lto)
+
+subdirs(src)
+if(use_lto)
+ set_property(DIRECTORY src PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
+endif(use_lto)
+
+if (WITH_TESTS)
+ subdirs(test)
+ if(use_lto)
+ set_property(DIRECTORY test PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
+ endif(use_lto)
+endif (WITH_TESTS)
+
+if (WITH_EXAMPLES)
+ subdirs(examples)
+ if(use_lto)
+ set_property(DIRECTORY examples PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
+ endif(use_lto)
+endif (WITH_EXAMPLES)
diff --git a/CMakeModules/FindCJSON.cmake b/CMakeModules/FindCJSON.cmake
new file mode 100644
index 000000000000..e0629057def7
--- /dev/null
+++ b/CMakeModules/FindCJSON.cmake
@@ -0,0 +1,9 @@
+include (LibFindMacros)
+
+find_path(CJSON_INCLUDE_DIR NAMES cjson/cJSON.h)
+find_library(CJSON_LIBRARY NAMES cjson)
+
+set(CJSON_PROCESS_INCLUDES CJSON_INCLUDE_DIR)
+set(CJSON_PROCESS_LIBS CJSON_LIBRARY)
+
+libfind_process(CJSON)
diff --git a/CMakeModules/FindCMocka.cmake b/CMakeModules/FindCMocka.cmake
new file mode 100644
index 000000000000..770b44afc426
--- /dev/null
+++ b/CMakeModules/FindCMocka.cmake
@@ -0,0 +1,49 @@
+# - Try to find CMocka
+# Once done this will define
+#
+# CMOCKA_ROOT_DIR - Set this variable to the root installation of CMocka
+#
+# Read-Only variables:
+# CMOCKA_FOUND - system has CMocka
+# CMOCKA_INCLUDE_DIR - the CMocka include directory
+# CMOCKA_LIBRARIES - Link these to use CMocka
+# CMOCKA_DEFINITIONS - Compiler switches required for using CMocka
+#
+#=============================================================================
+# Copyright (c) 2011-2012 Andreas Schneider <asn@cryptomilk.org>
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
+#=============================================================================
+#
+
+find_path(CMOCKA_INCLUDE_DIR
+ NAMES
+ cmocka.h
+ PATHS
+ ${CMOCKA_ROOT_DIR}/include
+)
+
+find_library(CMOCKA_LIBRARY
+ NAMES
+ cmocka cmocka_shared
+ PATHS
+ ${CMOCKA_ROOT_DIR}/include
+)
+
+if (CMOCKA_LIBRARY)
+ set(CMOCKA_LIBRARIES
+ ${CMOCKA_LIBRARIES}
+ ${CMOCKA_LIBRARY}
+ )
+endif (CMOCKA_LIBRARY)
+
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(CMocka DEFAULT_MSG CMOCKA_LIBRARIES CMOCKA_INCLUDE_DIR)
+
+# show the CMOCKA_INCLUDE_DIR and CMOCKA_LIBRARIES variables only in the advanced view
+mark_as_advanced(CMOCKA_INCLUDE_DIR CMOCKA_LIBRARIES)
diff --git a/CMakeModules/LibFindMacros.cmake b/CMakeModules/LibFindMacros.cmake
new file mode 100644
index 000000000000..baea8c2be928
--- /dev/null
+++ b/CMakeModules/LibFindMacros.cmake
@@ -0,0 +1,120 @@
+# Fetched from https://github.com/JonathanSalwan/Triton/blob/master/CMakeModules/LibFindMacros.cmake
+#
+# Copyright:
+#
+# * Jonathan Salwan (Quarkslab)
+# * Pierrick Brunet (Quarkslab)
+# * Romain Thomas (Quarkslab)
+# * Florent Saudel (Bordeaux University)
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Works the same as find_package, but forwards the "REQUIRED" and "QUIET" arguments
+# used for the current package. For this to work, the first parameter must be the
+# prefix of the current package, then the prefix of the new package etc, which are
+# passed to find_package.
+macro (libfind_package PREFIX)
+ set (LIBFIND_PACKAGE_ARGS ${ARGN})
+ if (${PREFIX}_FIND_QUIETLY)
+ set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} QUIET)
+ endif (${PREFIX}_FIND_QUIETLY)
+ if (${PREFIX}_FIND_REQUIRED)
+ set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} REQUIRED)
+ endif (${PREFIX}_FIND_REQUIRED)
+ find_package(${LIBFIND_PACKAGE_ARGS})
+endmacro (libfind_package)
+
+# CMake developers made the UsePkgConfig system deprecated in the same release (2.6)
+# where they added pkg_check_modules. Consequently I need to support both in my scripts
+# to avoid those deprecated warnings. Here's a helper that does just that.
+# Works identically to pkg_check_modules, except that no checks are needed prior to use.
+macro (libfind_pkg_check_modules PREFIX PKGNAME)
+ if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
+ include(UsePkgConfig)
+ pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS)
+ else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
+ find_package(PkgConfig)
+ if (PKG_CONFIG_FOUND)
+ pkg_check_modules(${PREFIX} ${PKGNAME})
+ endif (PKG_CONFIG_FOUND)
+ endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
+endmacro (libfind_pkg_check_modules)
+
+# Do the final processing once the paths have been detected.
+# If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
+# all the variables, each of which contain one include directory.
+# Ditto for ${PREFIX}_PROCESS_LIBS and library files.
+# Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
+# Also handles errors in case library detection was required, etc.
+macro (libfind_process PREFIX)
+ # Skip processing if already processed during this run
+ if (NOT ${PREFIX}_FOUND)
+ # Start with the assumption that the library was found
+ set (${PREFIX}_FOUND TRUE)
+
+ # Process all includes and set _FOUND to false if any are missing
+ foreach (i ${${PREFIX}_PROCESS_INCLUDES})
+ if (${i})
+ set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}})
+ mark_as_advanced(${i})
+ else (${i})
+ set (${PREFIX}_FOUND FALSE)
+ endif (${i})
+ endforeach (i)
+
+ # Process all libraries and set _FOUND to false if any are missing
+ foreach (i ${${PREFIX}_PROCESS_LIBS})
+ if (${i})
+ set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
+ mark_as_advanced(${i})
+ else (${i})
+ set (${PREFIX}_FOUND FALSE)
+ endif (${i})
+ endforeach (i)
+
+ # Print message and/or exit on fatal error
+ if (${PREFIX}_FOUND)
+ if (NOT ${PREFIX}_FIND_QUIETLY)
+ message (STATUS "Found ${PREFIX} include directory: ${${PREFIX}_INCLUDE_DIR}")
+ message (STATUS "Found ${PREFIX} library: ${${PREFIX}_LIBRARY}")
+ endif (NOT ${PREFIX}_FIND_QUIETLY)
+ else (${PREFIX}_FOUND)
+ if (${PREFIX}_FIND_REQUIRED)
+ foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
+ message("${i}=${${i}}")
+ endforeach (i)
+ message (FATAL_ERROR "Required library ${PREFIX} NOT FOUND.\nInstall the library (dev version) and try again. If the library is already installed, use ccmake to set the missing variables manually.")
+ endif (${PREFIX}_FIND_REQUIRED)
+ endif (${PREFIX}_FOUND)
+ endif (NOT ${PREFIX}_FOUND)
+endmacro (libfind_process)
+
+macro(libfind_library PREFIX basename)
+ set(TMP "")
+ if(MSVC80)
+ set(TMP -vc80)
+ endif(MSVC80)
+ if(MSVC90)
+ set(TMP -vc90)
+ endif(MSVC90)
+ set(${PREFIX}_LIBNAMES ${basename}${TMP})
+ if(${ARGC} GREATER 2)
+ set(${PREFIX}_LIBNAMES ${basename}${TMP}-${ARGV2})
+ string(REGEX REPLACE "\\." "_" TMP ${${PREFIX}_LIBNAMES})
+ set(${PREFIX}_LIBNAMES ${${PREFIX}_LIBNAMES} ${TMP})
+ endif(${ARGC} GREATER 2)
+ find_library(${PREFIX}_LIBRARY
+ NAMES ${${PREFIX}_LIBNAMES}
+ PATHS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}
+ )
+endmacro(libfind_library)
diff --git a/Doxyfile b/Doxyfile
new file mode 100644
index 000000000000..b197d2bcdedc
--- /dev/null
+++ b/Doxyfile
@@ -0,0 +1,2417 @@
+# Doxyfile 1.8.12
+
+# This file describes the settings to be used by the documentation system
+# doxygen (www.doxygen.org) for a project.
+#
+# All text after a double hash (##) is considered a comment and is placed in
+# front of the TAG it is preceding.
+#
+# All text after a single hash (#) is considered a comment and will be ignored.
+# The format is:
+# TAG = value [value, ...]
+# For lists, items can also be appended using:
+# TAG += value [value, ...]
+# Values that contain spaces should be placed between quotes (\" \").
+
+#---------------------------------------------------------------------------
+# Project related configuration options
+#---------------------------------------------------------------------------
+
+# This tag specifies the encoding used for all characters in the config file
+# that follow. The default is UTF-8 which is also the encoding used for all text
+# before the first occurrence of this tag. Doxygen uses libiconv (or the iconv
+# built into libc) for the transcoding. See http://www.gnu.org/software/libiconv
+# for the list of possible encodings.
+# The default value is: UTF-8.
+
+DOXYFILE_ENCODING = UTF-8
+
+# The PROJECT_NAME tag is a single word (or a sequence of words surrounded by
+# double-quotes, unless you are using Doxywizard) that should identify the
+# project for which the documentation is generated. This name is used in the
+# title of most generated pages and in a few other places.
+# The default value is: My Project.
+
+PROJECT_NAME = "libcbor"
+
+# The PROJECT_NUMBER tag can be used to enter a project or revision number. This
+# could be handy for archiving the generated documentation or if some version
+# control system is used.
+
+PROJECT_NUMBER = 0.8.0
+
+# Using the PROJECT_BRIEF tag one can provide an optional one line description
+# for a project that appears at the top of each page and should give viewer a
+# quick idea about the purpose of the project. Keep the description short.
+
+PROJECT_BRIEF = "libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format."
+
+# With the PROJECT_LOGO tag one can specify a logo or an icon that is included
+# in the documentation. The maximum height of the logo should not exceed 55
+# pixels and the maximum width should not exceed 200 pixels. Doxygen will copy
+# the logo to the output directory.
+
+PROJECT_LOGO =
+
+# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) path
+# into which the generated documentation will be written. If a relative path is
+# entered, it will be relative to the location where doxygen was started. If
+# left blank the current directory will be used.
+
+OUTPUT_DIRECTORY = doc/build/doxygen
+
+# If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub-
+# directories (in 2 levels) under the output directory of each output format and
+# will distribute the generated files over these directories. Enabling this
+# option can be useful when feeding doxygen a huge amount of source files, where
+# putting all generated files in the same directory would otherwise causes
+# performance problems for the file system.
+# The default value is: NO.
+
+CREATE_SUBDIRS = NO
+
+# If the ALLOW_UNICODE_NAMES tag is set to YES, doxygen will allow non-ASCII
+# characters to appear in the names of generated files. If set to NO, non-ASCII
+# characters will be escaped, for example _xE3_x81_x84 will be used for Unicode
+# U+3044.
+# The default value is: NO.
+
+ALLOW_UNICODE_NAMES = NO
+
+# The OUTPUT_LANGUAGE tag is used to specify the language in which all
+# documentation generated by doxygen is written. Doxygen will use this
+# information to generate all constant output in the proper language.
+# Possible values are: Afrikaans, Arabic, Armenian, Brazilian, Catalan, Chinese,
+# Chinese-Traditional, Croatian, Czech, Danish, Dutch, English (United States),
+# Esperanto, Farsi (Persian), Finnish, French, German, Greek, Hungarian,
+# Indonesian, Italian, Japanese, Japanese-en (Japanese with English messages),
+# Korean, Korean-en (Korean with English messages), Latvian, Lithuanian,
+# Macedonian, Norwegian, Persian (Farsi), Polish, Portuguese, Romanian, Russian,
+# Serbian, Serbian-Cyrillic, Slovak, Slovene, Spanish, Swedish, Turkish,
+# Ukrainian and Vietnamese.
+# The default value is: English.
+
+OUTPUT_LANGUAGE = English
+
+# If the BRIEF_MEMBER_DESC tag is set to YES, doxygen will include brief member
+# descriptions after the members that are listed in the file and class
+# documentation (similar to Javadoc). Set to NO to disable this.
+# The default value is: YES.
+
+BRIEF_MEMBER_DESC = YES
+
+# If the REPEAT_BRIEF tag is set to YES, doxygen will prepend the brief
+# description of a member or function before the detailed description
+#
+# Note: If both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the
+# brief descriptions will be completely suppressed.
+# The default value is: YES.
+
+REPEAT_BRIEF = YES
+
+# This tag implements a quasi-intelligent brief description abbreviator that is
+# used to form the text in various listings. Each string in this list, if found
+# as the leading text of the brief description, will be stripped from the text
+# and the result, after processing the whole list, is used as the annotated
+# text. Otherwise, the brief description is used as-is. If left blank, the
+# following values are used ($name is automatically replaced with the name of
+# the entity):The $name class, The $name widget, The $name file, is, provides,
+# specifies, contains, represents, a, an and the.
+
+ABBREVIATE_BRIEF =
+
+# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then
+# doxygen will generate a detailed section even if there is only a brief
+# description.
+# The default value is: NO.
+
+ALWAYS_DETAILED_SEC = NO
+
+# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all
+# inherited members of a class in the documentation of that class as if those
+# members were ordinary class members. Constructors, destructors and assignment
+# operators of the base classes will not be shown.
+# The default value is: NO.
+
+INLINE_INHERITED_MEMB = NO
+
+# If the FULL_PATH_NAMES tag is set to YES, doxygen will prepend the full path
+# before files name in the file list and in the header files. If set to NO the
+# shortest path that makes the file name unique will be used
+# The default value is: YES.
+
+FULL_PATH_NAMES = YES
+
+# The STRIP_FROM_PATH tag can be used to strip a user-defined part of the path.
+# Stripping is only done if one of the specified strings matches the left-hand
+# part of the path. The tag can be used to show relative paths in the file list.
+# If left blank the directory from which doxygen is run is used as the path to
+# strip.
+#
+# Note that you can specify absolute paths here, but also relative paths, which
+# will be relative from the directory where doxygen is started.
+# This tag requires that the tag FULL_PATH_NAMES is set to YES.
+
+STRIP_FROM_PATH =
+
+# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of the
+# path mentioned in the documentation of a class, which tells the reader which
+# header file to include in order to use a class. If left blank only the name of
+# the header file containing the class definition is used. Otherwise one should
+# specify the list of include paths that are normally passed to the compiler
+# using the -I flag.
+
+STRIP_FROM_INC_PATH =
+
+# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter (but
+# less readable) file names. This can be useful is your file systems doesn't
+# support long names like on DOS, Mac, or CD-ROM.
+# The default value is: NO.
+
+SHORT_NAMES = NO
+
+# If the JAVADOC_AUTOBRIEF tag is set to YES then doxygen will interpret the
+# first line (until the first dot) of a Javadoc-style comment as the brief
+# description. If set to NO, the Javadoc-style will behave just like regular Qt-
+# style comments (thus requiring an explicit @brief command for a brief
+# description.)
+# The default value is: NO.
+
+JAVADOC_AUTOBRIEF = YES
+
+# If the QT_AUTOBRIEF tag is set to YES then doxygen will interpret the first
+# line (until the first dot) of a Qt-style comment as the brief description. If
+# set to NO, the Qt-style will behave just like regular Qt-style comments (thus
+# requiring an explicit \brief command for a brief description.)
+# The default value is: NO.
+
+QT_AUTOBRIEF = NO
+
+# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make doxygen treat a
+# multi-line C++ special comment block (i.e. a block of //! or /// comments) as
+# a brief description. This used to be the default behavior. The new default is
+# to treat a multi-line C++ comment block as a detailed description. Set this
+# tag to YES if you prefer the old behavior instead.
+#
+# Note that setting this tag to YES also means that rational rose comments are
+# not recognized any more.
+# The default value is: NO.
+
+MULTILINE_CPP_IS_BRIEF = NO
+
+# If the INHERIT_DOCS tag is set to YES then an undocumented member inherits the
+# documentation from any documented member that it re-implements.
+# The default value is: YES.
+
+INHERIT_DOCS = YES
+
+# If the SEPARATE_MEMBER_PAGES tag is set to YES then doxygen will produce a new
+# page for each member. If set to NO, the documentation of a member will be part
+# of the file/class/namespace that contains it.
+# The default value is: NO.
+
+SEPARATE_MEMBER_PAGES = NO
+
+# The TAB_SIZE tag can be used to set the number of spaces in a tab. Doxygen
+# uses this value to replace tabs by spaces in code fragments.
+# Minimum value: 1, maximum value: 16, default value: 4.
+
+TAB_SIZE = 4
+
+# This tag can be used to specify a number of aliases that act as commands in
+# the documentation. An alias has the form:
+# name=value
+# For example adding
+# "sideeffect=@par Side Effects:\n"
+# will allow you to put the command \sideeffect (or @sideeffect) in the
+# documentation, which will result in a user-defined paragraph with heading
+# "Side Effects:". You can put \n's in the value part of an alias to insert
+# newlines.
+
+ALIASES = "rst=\verbatim embed:rst:leading-asterisk" \
+ "endrst=\endverbatim"
+
+# This tag can be used to specify a number of word-keyword mappings (TCL only).
+# A mapping has the form "name=value". For example adding "class=itcl::class"
+# will allow you to use the command class in the itcl::class meaning.
+
+TCL_SUBST =
+
+# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C sources
+# only. Doxygen will then generate output that is more tailored for C. For
+# instance, some of the names that are used will be different. The list of all
+# members will be omitted, etc.
+# The default value is: NO.
+
+OPTIMIZE_OUTPUT_FOR_C = YES
+
+# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java or
+# Python sources only. Doxygen will then generate output that is more tailored
+# for that language. For instance, namespaces will be presented as packages,
+# qualified scopes will look different, etc.
+# The default value is: NO.
+
+OPTIMIZE_OUTPUT_JAVA = NO
+
+# Set the OPTIMIZE_FOR_FORTRAN tag to YES if your project consists of Fortran
+# sources. Doxygen will then generate output that is tailored for Fortran.
+# The default value is: NO.
+
+OPTIMIZE_FOR_FORTRAN = NO
+
+# Set the OPTIMIZE_OUTPUT_VHDL tag to YES if your project consists of VHDL
+# sources. Doxygen will then generate output that is tailored for VHDL.
+# The default value is: NO.
+
+OPTIMIZE_OUTPUT_VHDL = NO
+
+# Doxygen selects the parser to use depending on the extension of the files it
+# parses. With this tag you can assign which parser to use for a given
+# extension. Doxygen has a built-in mapping, but you can override or extend it
+# using this tag. The format is ext=language, where ext is a file extension, and
+# language is one of the parsers supported by doxygen: IDL, Java, Javascript,
+# C#, C, C++, D, PHP, Objective-C, Python, Fortran (fixed format Fortran:
+# FortranFixed, free formatted Fortran: FortranFree, unknown formatted Fortran:
+# Fortran. In the later case the parser tries to guess whether the code is fixed
+# or free formatted code, this is the default for Fortran type files), VHDL. For
+# instance to make doxygen treat .inc files as Fortran files (default is PHP),
+# and .f files as C (default is Fortran), use: inc=Fortran f=C.
+#
+# Note: For files without extension you can use no_extension as a placeholder.
+#
+# Note that for custom extensions you also need to set FILE_PATTERNS otherwise
+# the files are not read by doxygen.
+
+EXTENSION_MAPPING =
+
+# If the MARKDOWN_SUPPORT tag is enabled then doxygen pre-processes all comments
+# according to the Markdown format, which allows for more readable
+# documentation. See http://daringfireball.net/projects/markdown/ for details.
+# The output of markdown processing is further processed by doxygen, so you can
+# mix doxygen, HTML, and XML commands with Markdown formatting. Disable only in
+# case of backward compatibilities issues.
+# The default value is: YES.
+
+MARKDOWN_SUPPORT = YES
+
+# When the TOC_INCLUDE_HEADINGS tag is set to a non-zero value, all headings up
+# to that level are automatically included in the table of contents, even if
+# they do not have an id attribute.
+# Note: This feature currently applies only to Markdown headings.
+# Minimum value: 0, maximum value: 99, default value: 0.
+# This tag requires that the tag MARKDOWN_SUPPORT is set to YES.
+
+TOC_INCLUDE_HEADINGS = 0
+
+# When enabled doxygen tries to link words that correspond to documented
+# classes, or namespaces to their corresponding documentation. Such a link can
+# be prevented in individual cases by putting a % sign in front of the word or
+# globally by setting AUTOLINK_SUPPORT to NO.
+# The default value is: YES.
+
+AUTOLINK_SUPPORT = YES
+
+# If you use STL classes (i.e. std::string, std::vector, etc.) but do not want
+# to include (a tag file for) the STL sources as input, then you should set this
+# tag to YES in order to let doxygen match functions declarations and
+# definitions whose arguments contain STL classes (e.g. func(std::string);
+# versus func(std::string) {}). This also make the inheritance and collaboration
+# diagrams that involve STL classes more complete and accurate.
+# The default value is: NO.
+
+BUILTIN_STL_SUPPORT = NO
+
+# If you use Microsoft's C++/CLI language, you should set this option to YES to
+# enable parsing support.
+# The default value is: NO.
+
+CPP_CLI_SUPPORT = NO
+
+# Set the SIP_SUPPORT tag to YES if your project consists of sip (see:
+# http://www.riverbankcomputing.co.uk/software/sip/intro) sources only. Doxygen
+# will parse them like normal C++ but will assume all classes use public instead
+# of private inheritance when no explicit protection keyword is present.
+# The default value is: NO.
+
+SIP_SUPPORT = NO
+
+# For Microsoft's IDL there are propget and propput attributes to indicate
+# getter and setter methods for a property. Setting this option to YES will make
+# doxygen to replace the get and set methods by a property in the documentation.
+# This will only work if the methods are indeed getting or setting a simple
+# type. If this is not the case, or you want to show the methods anyway, you
+# should set this option to NO.
+# The default value is: YES.
+
+IDL_PROPERTY_SUPPORT = YES
+
+# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC
+# tag is set to YES then doxygen will reuse the documentation of the first
+# member in the group (if any) for the other members of the group. By default
+# all members of a group must be documented explicitly.
+# The default value is: NO.
+
+DISTRIBUTE_GROUP_DOC = NO
+
+# If one adds a struct or class to a group and this option is enabled, then also
+# any nested class or struct is added to the same group. By default this option
+# is disabled and one has to add nested compounds explicitly via \ingroup.
+# The default value is: NO.
+
+GROUP_NESTED_COMPOUNDS = NO
+
+# Set the SUBGROUPING tag to YES to allow class member groups of the same type
+# (for instance a group of public functions) to be put as a subgroup of that
+# type (e.g. under the Public Functions section). Set it to NO to prevent
+# subgrouping. Alternatively, this can be done per class using the
+# \nosubgrouping command.
+# The default value is: YES.
+
+SUBGROUPING = YES
+
+# When the INLINE_GROUPED_CLASSES tag is set to YES, classes, structs and unions
+# are shown inside the group in which they are included (e.g. using \ingroup)
+# instead of on a separate page (for HTML and Man pages) or section (for LaTeX
+# and RTF).
+#
+# Note that this feature does not work in combination with
+# SEPARATE_MEMBER_PAGES.
+# The default value is: NO.
+
+INLINE_GROUPED_CLASSES = NO
+
+# When the INLINE_SIMPLE_STRUCTS tag is set to YES, structs, classes, and unions
+# with only public data fields or simple typedef fields will be shown inline in
+# the documentation of the scope in which they are defined (i.e. file,
+# namespace, or group documentation), provided this scope is documented. If set
+# to NO, structs, classes, and unions are shown on a separate page (for HTML and
+# Man pages) or section (for LaTeX and RTF).
+# The default value is: NO.
+
+INLINE_SIMPLE_STRUCTS = NO
+
+# When TYPEDEF_HIDES_STRUCT tag is enabled, a typedef of a struct, union, or
+# enum is documented as struct, union, or enum with the name of the typedef. So
+# typedef struct TypeS {} TypeT, will appear in the documentation as a struct
+# with name TypeT. When disabled the typedef will appear as a member of a file,
+# namespace, or class. And the struct will be named TypeS. This can typically be
+# useful for C code in case the coding convention dictates that all compound
+# types are typedef'ed and only the typedef is referenced, never the tag name.
+# The default value is: NO.
+
+TYPEDEF_HIDES_STRUCT = NO
+
+# The size of the symbol lookup cache can be set using LOOKUP_CACHE_SIZE. This
+# cache is used to resolve symbols given their name and scope. Since this can be
+# an expensive process and often the same symbol appears multiple times in the
+# code, doxygen keeps a cache of pre-resolved symbols. If the cache is too small
+# doxygen will become slower. If the cache is too large, memory is wasted. The
+# cache size is given by this formula: 2^(16+LOOKUP_CACHE_SIZE). The valid range
+# is 0..9, the default is 0, corresponding to a cache size of 2^16=65536
+# symbols. At the end of a run doxygen will report the cache usage and suggest
+# the optimal cache size from a speed point of view.
+# Minimum value: 0, maximum value: 9, default value: 0.
+
+LOOKUP_CACHE_SIZE = 0
+
+#---------------------------------------------------------------------------
+# Build related configuration options
+#---------------------------------------------------------------------------
+
+# If the EXTRACT_ALL tag is set to YES, doxygen will assume all entities in
+# documentation are documented, even if no documentation was available. Private
+# class members and static file members will be hidden unless the
+# EXTRACT_PRIVATE respectively EXTRACT_STATIC tags are set to YES.
+# Note: This will also disable the warnings about undocumented members that are
+# normally produced when WARNINGS is set to YES.
+# The default value is: NO.
+
+EXTRACT_ALL = YES
+
+# If the EXTRACT_PRIVATE tag is set to YES, all private members of a class will
+# be included in the documentation.
+# The default value is: NO.
+
+EXTRACT_PRIVATE = NO
+
+# If the EXTRACT_PACKAGE tag is set to YES, all members with package or internal
+# scope will be included in the documentation.
+# The default value is: NO.
+
+EXTRACT_PACKAGE = NO
+
+# If the EXTRACT_STATIC tag is set to YES, all static members of a file will be
+# included in the documentation.
+# The default value is: NO.
+
+EXTRACT_STATIC = NO
+
+# If the EXTRACT_LOCAL_CLASSES tag is set to YES, classes (and structs) defined
+# locally in source files will be included in the documentation. If set to NO,
+# only classes defined in header files are included. Does not have any effect
+# for Java sources.
+# The default value is: YES.
+
+EXTRACT_LOCAL_CLASSES = YES
+
+# This flag is only useful for Objective-C code. If set to YES, local methods,
+# which are defined in the implementation section but not in the interface are
+# included in the documentation. If set to NO, only methods in the interface are
+# included.
+# The default value is: NO.
+
+EXTRACT_LOCAL_METHODS = NO
+
+# If this flag is set to YES, the members of anonymous namespaces will be
+# extracted and appear in the documentation as a namespace called
+# 'anonymous_namespace{file}', where file will be replaced with the base name of
+# the file that contains the anonymous namespace. By default anonymous namespace
+# are hidden.
+# The default value is: NO.
+
+EXTRACT_ANON_NSPACES = NO
+
+# If the HIDE_UNDOC_MEMBERS tag is set to YES, doxygen will hide all
+# undocumented members inside documented classes or files. If set to NO these
+# members will be included in the various overviews, but no documentation
+# section is generated. This option has no effect if EXTRACT_ALL is enabled.
+# The default value is: NO.
+
+HIDE_UNDOC_MEMBERS = NO
+
+# If the HIDE_UNDOC_CLASSES tag is set to YES, doxygen will hide all
+# undocumented classes that are normally visible in the class hierarchy. If set
+# to NO, these classes will be included in the various overviews. This option
+# has no effect if EXTRACT_ALL is enabled.
+# The default value is: NO.
+
+HIDE_UNDOC_CLASSES = NO
+
+# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, doxygen will hide all friend
+# (class|struct|union) declarations. If set to NO, these declarations will be
+# included in the documentation.
+# The default value is: NO.
+
+HIDE_FRIEND_COMPOUNDS = NO
+
+# If the HIDE_IN_BODY_DOCS tag is set to YES, doxygen will hide any
+# documentation blocks found inside the body of a function. If set to NO, these
+# blocks will be appended to the function's detailed documentation block.
+# The default value is: NO.
+
+HIDE_IN_BODY_DOCS = NO
+
+# The INTERNAL_DOCS tag determines if documentation that is typed after a
+# \internal command is included. If the tag is set to NO then the documentation
+# will be excluded. Set it to YES to include the internal documentation.
+# The default value is: NO.
+
+INTERNAL_DOCS = NO
+
+# If the CASE_SENSE_NAMES tag is set to NO then doxygen will only generate file
+# names in lower-case letters. If set to YES, upper-case letters are also
+# allowed. This is useful if you have classes or files whose names only differ
+# in case and if your file system supports case sensitive file names. Windows
+# and Mac users are advised to set this option to NO.
+# The default value is: system dependent.
+
+CASE_SENSE_NAMES = YES
+
+# If the HIDE_SCOPE_NAMES tag is set to NO then doxygen will show members with
+# their full class and namespace scopes in the documentation. If set to YES, the
+# scope will be hidden.
+# The default value is: NO.
+
+HIDE_SCOPE_NAMES = NO
+
+# If the HIDE_COMPOUND_REFERENCE tag is set to NO (default) then doxygen will
+# append additional text to a page's title, such as Class Reference. If set to
+# YES the compound reference will be hidden.
+# The default value is: NO.
+
+HIDE_COMPOUND_REFERENCE= NO
+
+# If the SHOW_INCLUDE_FILES tag is set to YES then doxygen will put a list of
+# the files that are included by a file in the documentation of that file.
+# The default value is: YES.
+
+SHOW_INCLUDE_FILES = YES
+
+# If the SHOW_GROUPED_MEMB_INC tag is set to YES then Doxygen will add for each
+# grouped member an include statement to the documentation, telling the reader
+# which file to include in order to use the member.
+# The default value is: NO.
+
+SHOW_GROUPED_MEMB_INC = NO
+
+# If the FORCE_LOCAL_INCLUDES tag is set to YES then doxygen will list include
+# files with double quotes in the documentation rather than with sharp brackets.
+# The default value is: NO.
+
+FORCE_LOCAL_INCLUDES = NO
+
+# If the INLINE_INFO tag is set to YES then a tag [inline] is inserted in the
+# documentation for inline members.
+# The default value is: YES.
+
+INLINE_INFO = YES
+
+# If the SORT_MEMBER_DOCS tag is set to YES then doxygen will sort the
+# (detailed) documentation of file and class members alphabetically by member
+# name. If set to NO, the members will appear in declaration order.
+# The default value is: YES.
+
+SORT_MEMBER_DOCS = YES
+
+# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the brief
+# descriptions of file, namespace and class members alphabetically by member
+# name. If set to NO, the members will appear in declaration order. Note that
+# this will also influence the order of the classes in the class list.
+# The default value is: NO.
+
+SORT_BRIEF_DOCS = NO
+
+# If the SORT_MEMBERS_CTORS_1ST tag is set to YES then doxygen will sort the
+# (brief and detailed) documentation of class members so that constructors and
+# destructors are listed first. If set to NO the constructors will appear in the
+# respective orders defined by SORT_BRIEF_DOCS and SORT_MEMBER_DOCS.
+# Note: If SORT_BRIEF_DOCS is set to NO this option is ignored for sorting brief
+# member documentation.
+# Note: If SORT_MEMBER_DOCS is set to NO this option is ignored for sorting
+# detailed member documentation.
+# The default value is: NO.
+
+SORT_MEMBERS_CTORS_1ST = NO
+
+# If the SORT_GROUP_NAMES tag is set to YES then doxygen will sort the hierarchy
+# of group names into alphabetical order. If set to NO the group names will
+# appear in their defined order.
+# The default value is: NO.
+
+SORT_GROUP_NAMES = NO
+
+# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be sorted by
+# fully-qualified names, including namespaces. If set to NO, the class list will
+# be sorted only by class name, not including the namespace part.
+# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES.
+# Note: This option applies only to the class list, not to the alphabetical
+# list.
+# The default value is: NO.
+
+SORT_BY_SCOPE_NAME = NO
+
+# If the STRICT_PROTO_MATCHING option is enabled and doxygen fails to do proper
+# type resolution of all parameters of a function it will reject a match between
+# the prototype and the implementation of a member function even if there is
+# only one candidate or it is obvious which candidate to choose by doing a
+# simple string match. By disabling STRICT_PROTO_MATCHING doxygen will still
+# accept a match between prototype and implementation in such cases.
+# The default value is: NO.
+
+STRICT_PROTO_MATCHING = NO
+
+# The GENERATE_TODOLIST tag can be used to enable (YES) or disable (NO) the todo
+# list. This list is created by putting \todo commands in the documentation.
+# The default value is: YES.
+
+GENERATE_TODOLIST = YES
+
+# The GENERATE_TESTLIST tag can be used to enable (YES) or disable (NO) the test
+# list. This list is created by putting \test commands in the documentation.
+# The default value is: YES.
+
+GENERATE_TESTLIST = YES
+
+# The GENERATE_BUGLIST tag can be used to enable (YES) or disable (NO) the bug
+# list. This list is created by putting \bug commands in the documentation.
+# The default value is: YES.
+
+GENERATE_BUGLIST = YES
+
+# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or disable (NO)
+# the deprecated list. This list is created by putting \deprecated commands in
+# the documentation.
+# The default value is: YES.
+
+GENERATE_DEPRECATEDLIST= YES
+
+# The ENABLED_SECTIONS tag can be used to enable conditional documentation
+# sections, marked by \if <section_label> ... \endif and \cond <section_label>
+# ... \endcond blocks.
+
+ENABLED_SECTIONS =
+
+# The MAX_INITIALIZER_LINES tag determines the maximum number of lines that the
+# initial value of a variable or macro / define can have for it to appear in the
+# documentation. If the initializer consists of more lines than specified here
+# it will be hidden. Use a value of 0 to hide initializers completely. The
+# appearance of the value of individual variables and macros / defines can be
+# controlled using \showinitializer or \hideinitializer command in the
+# documentation regardless of this setting.
+# Minimum value: 0, maximum value: 10000, default value: 30.
+
+MAX_INITIALIZER_LINES = 30
+
+# Set the SHOW_USED_FILES tag to NO to disable the list of files generated at
+# the bottom of the documentation of classes and structs. If set to YES, the
+# list will mention the files that were used to generate the documentation.
+# The default value is: YES.
+
+SHOW_USED_FILES = YES
+
+# Set the SHOW_FILES tag to NO to disable the generation of the Files page. This
+# will remove the Files entry from the Quick Index and from the Folder Tree View
+# (if specified).
+# The default value is: YES.
+
+SHOW_FILES = YES
+
+# Set the SHOW_NAMESPACES tag to NO to disable the generation of the Namespaces
+# page. This will remove the Namespaces entry from the Quick Index and from the
+# Folder Tree View (if specified).
+# The default value is: YES.
+
+SHOW_NAMESPACES = YES
+
+# The FILE_VERSION_FILTER tag can be used to specify a program or script that
+# doxygen should invoke to get the current version for each file (typically from
+# the version control system). Doxygen will invoke the program by executing (via
+# popen()) the command command input-file, where command is the value of the
+# FILE_VERSION_FILTER tag, and input-file is the name of an input file provided
+# by doxygen. Whatever the program writes to standard output is used as the file
+# version. For an example see the documentation.
+
+FILE_VERSION_FILTER =
+
+# The LAYOUT_FILE tag can be used to specify a layout file which will be parsed
+# by doxygen. The layout file controls the global structure of the generated
+# output files in an output format independent way. To create the layout file
+# that represents doxygen's defaults, run doxygen with the -l option. You can
+# optionally specify a file name after the option, if omitted DoxygenLayout.xml
+# will be used as the name of the layout file.
+#
+# Note that if you run doxygen from a directory containing a file called
+# DoxygenLayout.xml, doxygen will parse it automatically even if the LAYOUT_FILE
+# tag is left empty.
+
+LAYOUT_FILE = doxygen-theme/DoxygenLayout.xml
+
+# The CITE_BIB_FILES tag can be used to specify one or more bib files containing
+# the reference definitions. This must be a list of .bib files. The .bib
+# extension is automatically appended if omitted. This requires the bibtex tool
+# to be installed. See also http://en.wikipedia.org/wiki/BibTeX for more info.
+# For LaTeX the style of the bibliography can be controlled using
+# LATEX_BIB_STYLE. To use this feature you need bibtex and perl available in the
+# search path. See also \cite for info how to create references.
+
+CITE_BIB_FILES =
+
+#---------------------------------------------------------------------------
+# Configuration options related to warning and progress messages
+#---------------------------------------------------------------------------
+
+# The QUIET tag can be used to turn on/off the messages that are generated to
+# standard output by doxygen. If QUIET is set to YES this implies that the
+# messages are off.
+# The default value is: NO.
+
+QUIET = NO
+
+# The WARNINGS tag can be used to turn on/off the warning messages that are
+# generated to standard error (stderr) by doxygen. If WARNINGS is set to YES
+# this implies that the warnings are on.
+#
+# Tip: Turn warnings on while writing the documentation.
+# The default value is: YES.
+
+WARNINGS = YES
+
+# If the WARN_IF_UNDOCUMENTED tag is set to YES then doxygen will generate
+# warnings for undocumented members. If EXTRACT_ALL is set to YES then this flag
+# will automatically be disabled.
+# The default value is: YES.
+
+WARN_IF_UNDOCUMENTED = YES
+
+# If the WARN_IF_DOC_ERROR tag is set to YES, doxygen will generate warnings for
+# potential errors in the documentation, such as not documenting some parameters
+# in a documented function, or documenting parameters that don't exist or using
+# markup commands wrongly.
+# The default value is: YES.
+
+WARN_IF_DOC_ERROR = YES
+
+# This WARN_NO_PARAMDOC option can be enabled to get warnings for functions that
+# are documented, but have no documentation for their parameters or return
+# value. If set to NO, doxygen will only warn about wrong or incomplete
+# parameter documentation, but not about the absence of documentation.
+# The default value is: NO.
+
+WARN_NO_PARAMDOC = NO
+
+# If the WARN_AS_ERROR tag is set to YES then doxygen will immediately stop when
+# a warning is encountered.
+# The default value is: NO.
+
+WARN_AS_ERROR = NO
+
+# The WARN_FORMAT tag determines the format of the warning messages that doxygen
+# can produce. The string should contain the $file, $line, and $text tags, which
+# will be replaced by the file and line number from which the warning originated
+# and the warning text. Optionally the format may contain $version, which will
+# be replaced by the version of the file (if it could be obtained via
+# FILE_VERSION_FILTER)
+# The default value is: $file:$line: $text.
+
+WARN_FORMAT = "$file:$line: $text"
+
+# The WARN_LOGFILE tag can be used to specify a file to which warning and error
+# messages should be written. If left blank the output is written to standard
+# error (stderr).
+
+WARN_LOGFILE =
+
+#---------------------------------------------------------------------------
+# Configuration options related to the input files
+#---------------------------------------------------------------------------
+
+# The INPUT tag is used to specify the files and/or directories that contain
+# documented source files. You may enter file names like myfile.cpp or
+# directories like /usr/src/myproject. Separate the files or directories with
+# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
+# Note: If this tag is empty the current directory is searched.
+
+INPUT = src \
+ doc/doxy_frontpage.md
+
+# This tag can be used to specify the character encoding of the source files
+# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
+# libiconv (or the iconv built into libc) for the transcoding. See the libiconv
+# documentation (see: http://www.gnu.org/software/libiconv) for the list of
+# possible encodings.
+# The default value is: UTF-8.
+
+INPUT_ENCODING = UTF-8
+
+# If the value of the INPUT tag contains directories, you can use the
+# FILE_PATTERNS tag to specify one or more wildcard patterns (like *.cpp and
+# *.h) to filter out the source-files in the directories.
+#
+# Note that for custom extensions or not directly supported extensions you also
+# need to set EXTENSION_MAPPING for the extension otherwise the files are not
+# read by doxygen.
+#
+# If left blank the following patterns are tested:*.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, *.py, *.pyw, *.f90, *.f95, *.f03, *.f08,
+# *.f, *.for, *.tcl, *.vhd, *.vhdl, *.ucf and *.qsf.
+
+FILE_PATTERNS =
+
+# The RECURSIVE tag can be used to specify whether or not subdirectories should
+# be searched for input files as well.
+# The default value is: NO.
+
+RECURSIVE = YES
+
+# The EXCLUDE tag can be used to specify files and/or directories that should be
+# excluded from the INPUT source files. This way you can easily exclude a
+# subdirectory from a directory tree whose root is specified with the INPUT tag.
+#
+# Note that relative paths are relative to the directory from which doxygen is
+# run.
+
+EXCLUDE =
+
+# The EXCLUDE_SYMLINKS tag can be used to select whether or not files or
+# directories that are symbolic links (a Unix file system feature) are excluded
+# from the input.
+# The default value is: NO.
+
+EXCLUDE_SYMLINKS = NO
+
+# If the value of the INPUT tag contains directories, you can use the
+# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude
+# certain files from those directories.
+#
+# Note that the wildcards are matched against the file with absolute path, so to
+# exclude all test directories for example use the pattern */test/*
+
+EXCLUDE_PATTERNS =
+
+# The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names
+# (namespaces, classes, functions, etc.) that should be excluded from the
+# output. The symbol name can be a fully qualified name, a word, or if the
+# wildcard * is used, a substring. Examples: ANamespace, AClass,
+# AClass::ANamespace, ANamespace::*Test
+#
+# Note that the wildcards are matched against the file with absolute path, so to
+# exclude all test directories use the pattern */test/*
+
+EXCLUDE_SYMBOLS =
+
+# The EXAMPLE_PATH tag can be used to specify one or more files or directories
+# that contain example code fragments that are included (see the \include
+# command).
+
+EXAMPLE_PATH =
+
+# If the value of the EXAMPLE_PATH tag contains directories, you can use the
+# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp and
+# *.h) to filter out the source-files in the directories. If left blank all
+# files are included.
+
+EXAMPLE_PATTERNS =
+
+# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be
+# searched for input files to be used with the \include or \dontinclude commands
+# irrespective of the value of the RECURSIVE tag.
+# The default value is: NO.
+
+EXAMPLE_RECURSIVE = NO
+
+# The IMAGE_PATH tag can be used to specify one or more files or directories
+# that contain images that are to be included in the documentation (see the
+# \image command).
+
+IMAGE_PATH =
+
+# The INPUT_FILTER tag can be used to specify a program that doxygen should
+# invoke to filter for each input file. Doxygen will invoke the filter program
+# by executing (via popen()) the command:
+#
+# <filter> <input-file>
+#
+# where <filter> is the value of the INPUT_FILTER tag, and <input-file> is the
+# name of an input file. Doxygen will then use the output that the filter
+# program writes to standard output. If FILTER_PATTERNS is specified, this tag
+# will be ignored.
+#
+# Note that the filter must not add or remove lines; it is applied before the
+# code is scanned, but not when the output code is generated. If lines are added
+# or removed, the anchors will not be placed correctly.
+#
+# Note that for custom extensions or not directly supported extensions you also
+# need to set EXTENSION_MAPPING for the extension otherwise the files are not
+# properly processed by doxygen.
+
+INPUT_FILTER =
+
+# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern
+# basis. Doxygen will compare the file name with each pattern and apply the
+# filter if there is a match. The filters are a list of the form: pattern=filter
+# (like *.cpp=my_cpp_filter). See INPUT_FILTER for further information on how
+# filters are used. If the FILTER_PATTERNS tag is empty or if none of the
+# patterns match the file name, INPUT_FILTER is applied.
+#
+# Note that for custom extensions or not directly supported extensions you also
+# need to set EXTENSION_MAPPING for the extension otherwise the files are not
+# properly processed by doxygen.
+
+FILTER_PATTERNS =
+
+# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using
+# INPUT_FILTER) will also be used to filter the input files that are used for
+# producing the source files to browse (i.e. when SOURCE_BROWSER is set to YES).
+# The default value is: NO.
+
+FILTER_SOURCE_FILES = NO
+
+# The FILTER_SOURCE_PATTERNS tag can be used to specify source filters per file
+# pattern. A pattern will override the setting for FILTER_PATTERN (if any) and
+# it is also possible to disable source filtering for a specific pattern using
+# *.ext= (so without naming a filter).
+# This tag requires that the tag FILTER_SOURCE_FILES is set to YES.
+
+FILTER_SOURCE_PATTERNS =
+
+# If the USE_MDFILE_AS_MAINPAGE tag refers to the name of a markdown file that
+# is part of the input, its contents will be placed on the main page
+# (index.html). This can be useful if you have a project on for instance GitHub
+# and want to reuse the introduction page also for the doxygen output.
+
+USE_MDFILE_AS_MAINPAGE = doc/doxy_frontpage.md
+
+#---------------------------------------------------------------------------
+# Configuration options related to source browsing
+#---------------------------------------------------------------------------
+
+# If the SOURCE_BROWSER tag is set to YES then a list of source files will be
+# generated. Documented entities will be cross-referenced with these sources.
+#
+# Note: To get rid of all source code in the generated output, make sure that
+# also VERBATIM_HEADERS is set to NO.
+# The default value is: NO.
+
+SOURCE_BROWSER = YES
+
+# Setting the INLINE_SOURCES tag to YES will include the body of functions,
+# classes and enums directly into the documentation.
+# The default value is: NO.
+
+INLINE_SOURCES = NO
+
+# Setting the STRIP_CODE_COMMENTS tag to YES will instruct doxygen to hide any
+# special comment blocks from generated source code fragments. Normal C, C++ and
+# Fortran comments will always remain visible.
+# The default value is: YES.
+
+STRIP_CODE_COMMENTS = YES
+
+# If the REFERENCED_BY_RELATION tag is set to YES then for each documented
+# function all documented functions referencing it will be listed.
+# The default value is: NO.
+
+REFERENCED_BY_RELATION = NO
+
+# If the REFERENCES_RELATION tag is set to YES then for each documented function
+# all documented entities called/used by that function will be listed.
+# The default value is: NO.
+
+REFERENCES_RELATION = NO
+
+# If the REFERENCES_LINK_SOURCE tag is set to YES and SOURCE_BROWSER tag is set
+# to YES then the hyperlinks from functions in REFERENCES_RELATION and
+# REFERENCED_BY_RELATION lists will link to the source code. Otherwise they will
+# link to the documentation.
+# The default value is: YES.
+
+REFERENCES_LINK_SOURCE = YES
+
+# If SOURCE_TOOLTIPS is enabled (the default) then hovering a hyperlink in the
+# source code will show a tooltip with additional information such as prototype,
+# brief description and links to the definition and documentation. Since this
+# will make the HTML file larger and loading of large files a bit slower, you
+# can opt to disable this feature.
+# The default value is: YES.
+# This tag requires that the tag SOURCE_BROWSER is set to YES.
+
+SOURCE_TOOLTIPS = YES
+
+# If the USE_HTAGS tag is set to YES then the references to source code will
+# point to the HTML generated by the htags(1) tool instead of doxygen built-in
+# source browser. The htags tool is part of GNU's global source tagging system
+# (see http://www.gnu.org/software/global/global.html). You will need version
+# 4.8.6 or higher.
+#
+# To use it do the following:
+# - Install the latest version of global
+# - Enable SOURCE_BROWSER and USE_HTAGS in the config file
+# - Make sure the INPUT points to the root of the source tree
+# - Run doxygen as normal
+#
+# Doxygen will invoke htags (and that will in turn invoke gtags), so these
+# tools must be available from the command line (i.e. in the search path).
+#
+# The result: instead of the source browser generated by doxygen, the links to
+# source code will now point to the output of htags.
+# The default value is: NO.
+# This tag requires that the tag SOURCE_BROWSER is set to YES.
+
+USE_HTAGS = NO
+
+# If the VERBATIM_HEADERS tag is set the YES then doxygen will generate a
+# verbatim copy of the header file for each class for which an include is
+# specified. Set to NO to disable this.
+# See also: Section \class.
+# The default value is: YES.
+
+VERBATIM_HEADERS = YES
+
+#---------------------------------------------------------------------------
+# Configuration options related to the alphabetical class index
+#---------------------------------------------------------------------------
+
+# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index of all
+# compounds will be generated. Enable this if the project contains a lot of
+# classes, structs, unions or interfaces.
+# The default value is: YES.
+
+ALPHABETICAL_INDEX = YES
+
+# The COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns in
+# which the alphabetical index list will be split.
+# Minimum value: 1, maximum value: 20, default value: 5.
+# This tag requires that the tag ALPHABETICAL_INDEX is set to YES.
+
+COLS_IN_ALPHA_INDEX = 5
+
+# In case all classes in a project start with a common prefix, all classes will
+# be put under the same header in the alphabetical index. The IGNORE_PREFIX tag
+# can be used to specify a prefix (or a list of prefixes) that should be ignored
+# while generating the index headers.
+# This tag requires that the tag ALPHABETICAL_INDEX is set to YES.
+
+IGNORE_PREFIX =
+
+#---------------------------------------------------------------------------
+# Configuration options related to the HTML output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_HTML tag is set to YES, doxygen will generate HTML output
+# The default value is: YES.
+
+GENERATE_HTML = YES
+
+# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. If a
+# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
+# it.
+# The default directory is: html.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_OUTPUT = html
+
+# The HTML_FILE_EXTENSION tag can be used to specify the file extension for each
+# generated HTML page (for example: .htm, .php, .asp).
+# The default value is: .html.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_FILE_EXTENSION = .html
+
+# The HTML_HEADER tag can be used to specify a user-defined HTML header file for
+# each generated HTML page. If the tag is left blank doxygen will generate a
+# standard header.
+#
+# To get valid HTML the header file that includes any scripts and style sheets
+# that doxygen needs, which is dependent on the configuration options used (e.g.
+# the setting GENERATE_TREEVIEW). It is highly recommended to start with a
+# default header using
+# doxygen -w html new_header.html new_footer.html new_stylesheet.css
+# YourConfigFile
+# and then modify the file new_header.html. See also section "Doxygen usage"
+# for information on how to generate the default header that doxygen normally
+# uses.
+# Note: The header is subject to change so you typically have to regenerate the
+# default header when upgrading to a newer version of doxygen. For a description
+# of the possible markers and block names see the documentation.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_HEADER =
+
+# The HTML_FOOTER tag can be used to specify a user-defined HTML footer for each
+# generated HTML page. If the tag is left blank doxygen will generate a standard
+# footer. See HTML_HEADER for more information on how to generate a default
+# footer and what special commands can be used inside the footer. See also
+# section "Doxygen usage" for information on how to generate the default footer
+# that doxygen normally uses.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_FOOTER =
+
+# The HTML_STYLESHEET tag can be used to specify a user-defined cascading style
+# sheet that is used by each HTML page. It can be used to fine-tune the look of
+# the HTML output. If left blank doxygen will generate a default style sheet.
+# See also section "Doxygen usage" for information on how to generate the style
+# sheet that doxygen normally uses.
+# Note: It is recommended to use HTML_EXTRA_STYLESHEET instead of this tag, as
+# it is more robust and this tag (HTML_STYLESHEET) will in the future become
+# obsolete.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_STYLESHEET =
+
+# The HTML_EXTRA_STYLESHEET tag can be used to specify additional user-defined
+# cascading style sheets that are included after the standard style sheets
+# created by doxygen. Using this option one can overrule certain style aspects.
+# This is preferred over using HTML_STYLESHEET since it does not replace the
+# standard style sheet and is therefore more robust against future updates.
+# Doxygen will copy the style sheet files to the output directory.
+# Note: The order of the extra style sheet files is of importance (e.g. the last
+# style sheet in the list overrules the setting of the previous ones in the
+# list). For an example see the documentation.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_EXTRA_STYLESHEET = doxygen-theme/customdoxygen.css
+
+# The HTML_EXTRA_FILES tag can be used to specify one or more extra images or
+# other source files which should be copied to the HTML output directory. Note
+# that these files will be copied to the base HTML output directory. Use the
+# $relpath^ marker in the HTML_HEADER and/or HTML_FOOTER files to load these
+# files. In the HTML_STYLESHEET file, use the file name only. Also note that the
+# files will be copied as-is; there are no commands or markers available.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_EXTRA_FILES =
+
+# The HTML_COLORSTYLE_HUE tag controls the color of the HTML output. Doxygen
+# will adjust the colors in the style sheet and background images according to
+# this color. Hue is specified as an angle on a colorwheel, see
+# http://en.wikipedia.org/wiki/Hue for more information. For instance the value
+# 0 represents red, 60 is yellow, 120 is green, 180 is cyan, 240 is blue, 300
+# purple, and 360 is red again.
+# Minimum value: 0, maximum value: 359, default value: 220.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_COLORSTYLE_HUE = 220
+
+# The HTML_COLORSTYLE_SAT tag controls the purity (or saturation) of the colors
+# in the HTML output. For a value of 0 the output will use grayscales only. A
+# value of 255 will produce the most vivid colors.
+# Minimum value: 0, maximum value: 255, default value: 100.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_COLORSTYLE_SAT = 100
+
+# The HTML_COLORSTYLE_GAMMA tag controls the gamma correction applied to the
+# luminance component of the colors in the HTML output. Values below 100
+# gradually make the output lighter, whereas values above 100 make the output
+# darker. The value divided by 100 is the actual gamma applied, so 80 represents
+# a gamma of 0.8, The value 220 represents a gamma of 2.2, and 100 does not
+# change the gamma.
+# Minimum value: 40, maximum value: 240, default value: 80.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_COLORSTYLE_GAMMA = 80
+
+# If the HTML_TIMESTAMP tag is set to YES then the footer of each generated HTML
+# page will contain the date and time when the page was generated. Setting this
+# to YES can help to show when doxygen was last run and thus if the
+# documentation is up to date.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_TIMESTAMP = YES
+
+# If the HTML_DYNAMIC_SECTIONS tag is set to YES then the generated HTML
+# documentation will contain sections that can be hidden and shown after the
+# page has loaded.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_DYNAMIC_SECTIONS = NO
+
+# With HTML_INDEX_NUM_ENTRIES one can control the preferred number of entries
+# shown in the various tree structured indices initially; the user can expand
+# and collapse entries dynamically later on. Doxygen will expand the tree to
+# such a level that at most the specified number of entries are visible (unless
+# a fully collapsed tree already exceeds this amount). So setting the number of
+# entries 1 will produce a full collapsed tree by default. 0 is a special value
+# representing an infinite number of entries and will result in a full expanded
+# tree by default.
+# Minimum value: 0, maximum value: 9999, default value: 100.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+HTML_INDEX_NUM_ENTRIES = 100
+
+# If the GENERATE_DOCSET tag is set to YES, additional index files will be
+# generated that can be used as input for Apple's Xcode 3 integrated development
+# environment (see: http://developer.apple.com/tools/xcode/), introduced with
+# OSX 10.5 (Leopard). To create a documentation set, doxygen will generate a
+# Makefile in the HTML output directory. Running make will produce the docset in
+# that directory and running make install will install the docset in
+# ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find it at
+# startup. See http://developer.apple.com/tools/creatingdocsetswithdoxygen.html
+# for more information.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+GENERATE_DOCSET = NO
+
+# This tag determines the name of the docset feed. A documentation feed provides
+# an umbrella under which multiple documentation sets from a single provider
+# (such as a company or product suite) can be grouped.
+# The default value is: Doxygen generated docs.
+# This tag requires that the tag GENERATE_DOCSET is set to YES.
+
+DOCSET_FEEDNAME = "Doxygen generated docs"
+
+# This tag specifies a string that should uniquely identify the documentation
+# set bundle. This should be a reverse domain-name style string, e.g.
+# com.mycompany.MyDocSet. Doxygen will append .docset to the name.
+# The default value is: org.doxygen.Project.
+# This tag requires that the tag GENERATE_DOCSET is set to YES.
+
+DOCSET_BUNDLE_ID = org.doxygen.Project
+
+# The DOCSET_PUBLISHER_ID tag specifies a string that should uniquely identify
+# the documentation publisher. This should be a reverse domain-name style
+# string, e.g. com.mycompany.MyDocSet.documentation.
+# The default value is: org.doxygen.Publisher.
+# This tag requires that the tag GENERATE_DOCSET is set to YES.
+
+DOCSET_PUBLISHER_ID = org.doxygen.Publisher
+
+# The DOCSET_PUBLISHER_NAME tag identifies the documentation publisher.
+# The default value is: Publisher.
+# This tag requires that the tag GENERATE_DOCSET is set to YES.
+
+DOCSET_PUBLISHER_NAME = Publisher
+
+# If the GENERATE_HTMLHELP tag is set to YES then doxygen generates three
+# additional HTML index files: index.hhp, index.hhc, and index.hhk. The
+# index.hhp is a project file that can be read by Microsoft's HTML Help Workshop
+# (see: http://www.microsoft.com/en-us/download/details.aspx?id=21138) on
+# Windows.
+#
+# The HTML Help Workshop contains a compiler that can convert all HTML output
+# generated by doxygen into a single compiled HTML file (.chm). Compiled HTML
+# files are now used as the Windows 98 help format, and will replace the old
+# Windows help format (.hlp) on all Windows platforms in the future. Compressed
+# HTML files also contain an index, a table of contents, and you can search for
+# words in the documentation. The HTML workshop also contains a viewer for
+# compressed HTML files.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+GENERATE_HTMLHELP = NO
+
+# The CHM_FILE tag can be used to specify the file name of the resulting .chm
+# file. You can add a path in front of the file if the result should not be
+# written to the html output directory.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+CHM_FILE =
+
+# The HHC_LOCATION tag can be used to specify the location (absolute path
+# including file name) of the HTML help compiler (hhc.exe). If non-empty,
+# doxygen will try to run the HTML help compiler on the generated index.hhp.
+# The file has to be specified with full path.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+HHC_LOCATION =
+
+# The GENERATE_CHI flag controls if a separate .chi index file is generated
+# (YES) or that it should be included in the master .chm file (NO).
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+GENERATE_CHI = NO
+
+# The CHM_INDEX_ENCODING is used to encode HtmlHelp index (hhk), content (hhc)
+# and project file content.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+CHM_INDEX_ENCODING =
+
+# The BINARY_TOC flag controls whether a binary table of contents is generated
+# (YES) or a normal table of contents (NO) in the .chm file. Furthermore it
+# enables the Previous and Next buttons.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+BINARY_TOC = NO
+
+# The TOC_EXPAND flag can be set to YES to add extra items for group members to
+# the table of contents of the HTML help documentation and to the tree view.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTMLHELP is set to YES.
+
+TOC_EXPAND = NO
+
+# If the GENERATE_QHP tag is set to YES and both QHP_NAMESPACE and
+# QHP_VIRTUAL_FOLDER are set, an additional index file will be generated that
+# can be used as input for Qt's qhelpgenerator to generate a Qt Compressed Help
+# (.qch) of the generated HTML documentation.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+GENERATE_QHP = NO
+
+# If the QHG_LOCATION tag is specified, the QCH_FILE tag can be used to specify
+# the file name of the resulting .qch file. The path specified is relative to
+# the HTML output folder.
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QCH_FILE =
+
+# The QHP_NAMESPACE tag specifies the namespace to use when generating Qt Help
+# Project output. For more information please see Qt Help Project / Namespace
+# (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#namespace).
+# The default value is: org.doxygen.Project.
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHP_NAMESPACE = org.doxygen.Project
+
+# The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating Qt
+# Help Project output. For more information please see Qt Help Project / Virtual
+# Folders (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#virtual-
+# folders).
+# The default value is: doc.
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHP_VIRTUAL_FOLDER = doc
+
+# If the QHP_CUST_FILTER_NAME tag is set, it specifies the name of a custom
+# filter to add. For more information please see Qt Help Project / Custom
+# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom-
+# filters).
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHP_CUST_FILTER_NAME =
+
+# The QHP_CUST_FILTER_ATTRS tag specifies the list of the attributes of the
+# custom filter to add. For more information please see Qt Help Project / Custom
+# Filters (see: http://qt-project.org/doc/qt-4.8/qthelpproject.html#custom-
+# filters).
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHP_CUST_FILTER_ATTRS =
+
+# The QHP_SECT_FILTER_ATTRS tag specifies the list of the attributes this
+# project's filter section matches. Qt Help Project / Filter Attributes (see:
+# http://qt-project.org/doc/qt-4.8/qthelpproject.html#filter-attributes).
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHP_SECT_FILTER_ATTRS =
+
+# The QHG_LOCATION tag can be used to specify the location of Qt's
+# qhelpgenerator. If non-empty doxygen will try to run qhelpgenerator on the
+# generated .qhp file.
+# This tag requires that the tag GENERATE_QHP is set to YES.
+
+QHG_LOCATION =
+
+# If the GENERATE_ECLIPSEHELP tag is set to YES, additional index files will be
+# generated, together with the HTML files, they form an Eclipse help plugin. To
+# install this plugin and make it available under the help contents menu in
+# Eclipse, the contents of the directory containing the HTML and XML files needs
+# to be copied into the plugins directory of eclipse. The name of the directory
+# within the plugins directory should be the same as the ECLIPSE_DOC_ID value.
+# After copying Eclipse needs to be restarted before the help appears.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+GENERATE_ECLIPSEHELP = NO
+
+# A unique identifier for the Eclipse help plugin. When installing the plugin
+# the directory name containing the HTML and XML files should also have this
+# name. Each documentation set should have its own identifier.
+# The default value is: org.doxygen.Project.
+# This tag requires that the tag GENERATE_ECLIPSEHELP is set to YES.
+
+ECLIPSE_DOC_ID = org.doxygen.Project
+
+# If you want full control over the layout of the generated HTML pages it might
+# be necessary to disable the index and replace it with your own. The
+# DISABLE_INDEX tag can be used to turn on/off the condensed index (tabs) at top
+# of each HTML page. A value of NO enables the index and the value YES disables
+# it. Since the tabs in the index contain the same information as the navigation
+# tree, you can set this option to YES if you also set GENERATE_TREEVIEW to YES.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+DISABLE_INDEX = NO
+
+# The GENERATE_TREEVIEW tag is used to specify whether a tree-like index
+# structure should be generated to display hierarchical information. If the tag
+# value is set to YES, a side panel will be generated containing a tree-like
+# index structure (just like the one that is generated for HTML Help). For this
+# to work a browser that supports JavaScript, DHTML, CSS and frames is required
+# (i.e. any modern browser). Windows users are probably better off using the
+# HTML help feature. Via custom style sheets (see HTML_EXTRA_STYLESHEET) one can
+# further fine-tune the look of the index. As an example, the default style
+# sheet generated by doxygen has an example that shows how to put an image at
+# the root of the tree instead of the PROJECT_NAME. Since the tree basically has
+# the same information as the tab index, you could consider setting
+# DISABLE_INDEX to YES when enabling this option.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+GENERATE_TREEVIEW = NO
+
+# The ENUM_VALUES_PER_LINE tag can be used to set the number of enum values that
+# doxygen will group on one line in the generated HTML documentation.
+#
+# Note that a value of 0 will completely suppress the enum values from appearing
+# in the overview section.
+# Minimum value: 0, maximum value: 20, default value: 4.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+ENUM_VALUES_PER_LINE = 4
+
+# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be used
+# to set the initial width (in pixels) of the frame in which the tree is shown.
+# Minimum value: 0, maximum value: 1500, default value: 250.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+TREEVIEW_WIDTH = 250
+
+# If the EXT_LINKS_IN_WINDOW option is set to YES, doxygen will open links to
+# external symbols imported via tag files in a separate window.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+EXT_LINKS_IN_WINDOW = NO
+
+# Use this tag to change the font size of LaTeX formulas included as images in
+# the HTML documentation. When you change the font size after a successful
+# doxygen run you need to manually remove any form_*.png images from the HTML
+# output directory to force them to be regenerated.
+# Minimum value: 8, maximum value: 50, default value: 10.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+FORMULA_FONTSIZE = 10
+
+# Use the FORMULA_TRANPARENT tag to determine whether or not the images
+# generated for formulas are transparent PNGs. Transparent PNGs are not
+# supported properly for IE 6.0, but are supported on all modern browsers.
+#
+# Note that when changing this option you need to delete any form_*.png files in
+# the HTML output directory before the changes have effect.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+FORMULA_TRANSPARENT = YES
+
+# Enable the USE_MATHJAX option to render LaTeX formulas using MathJax (see
+# http://www.mathjax.org) which uses client side Javascript for the rendering
+# instead of using pre-rendered bitmaps. Use this if you do not have LaTeX
+# installed or if you want to formulas look prettier in the HTML output. When
+# enabled you may also need to install MathJax separately and configure the path
+# to it using the MATHJAX_RELPATH option.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+USE_MATHJAX = NO
+
+# When MathJax is enabled you can set the default output format to be used for
+# the MathJax output. See the MathJax site (see:
+# http://docs.mathjax.org/en/latest/output.html) for more details.
+# Possible values are: HTML-CSS (which is slower, but has the best
+# compatibility), NativeMML (i.e. MathML) and SVG.
+# The default value is: HTML-CSS.
+# This tag requires that the tag USE_MATHJAX is set to YES.
+
+MATHJAX_FORMAT = HTML-CSS
+
+# When MathJax is enabled you need to specify the location relative to the HTML
+# output directory using the MATHJAX_RELPATH option. The destination directory
+# should contain the MathJax.js script. For instance, if the mathjax directory
+# is located at the same level as the HTML output directory, then
+# MATHJAX_RELPATH should be ../mathjax. The default value points to the MathJax
+# Content Delivery Network so you can quickly see the result without installing
+# MathJax. However, it is strongly recommended to install a local copy of
+# MathJax from http://www.mathjax.org before deployment.
+# The default value is: http://cdn.mathjax.org/mathjax/latest.
+# This tag requires that the tag USE_MATHJAX is set to YES.
+
+MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest
+
+# The MATHJAX_EXTENSIONS tag can be used to specify one or more MathJax
+# extension names that should be enabled during MathJax rendering. For example
+# MATHJAX_EXTENSIONS = TeX/AMSmath TeX/AMSsymbols
+# This tag requires that the tag USE_MATHJAX is set to YES.
+
+MATHJAX_EXTENSIONS =
+
+# The MATHJAX_CODEFILE tag can be used to specify a file with javascript pieces
+# of code that will be used on startup of the MathJax code. See the MathJax site
+# (see: http://docs.mathjax.org/en/latest/output.html) for more details. For an
+# example see the documentation.
+# This tag requires that the tag USE_MATHJAX is set to YES.
+
+MATHJAX_CODEFILE =
+
+# When the SEARCHENGINE tag is enabled doxygen will generate a search box for
+# the HTML output. The underlying search engine uses javascript and DHTML and
+# should work on any modern browser. Note that when using HTML help
+# (GENERATE_HTMLHELP), Qt help (GENERATE_QHP), or docsets (GENERATE_DOCSET)
+# there is already a search function so this one should typically be disabled.
+# For large projects the javascript based search engine can be slow, then
+# enabling SERVER_BASED_SEARCH may provide a better solution. It is possible to
+# search using the keyboard; to jump to the search box use <access key> + S
+# (what the <access key> is depends on the OS and browser, but it is typically
+# <CTRL>, <ALT>/<option>, or both). Inside the search box use the <cursor down
+# key> to jump into the search results window, the results can be navigated
+# using the <cursor keys>. Press <Enter> to select an item or <escape> to cancel
+# the search. The filter options can be selected when the cursor is inside the
+# search box by pressing <Shift>+<cursor down>. Also here use the <cursor keys>
+# to select a filter and <Enter> or <escape> to activate or cancel the filter
+# option.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_HTML is set to YES.
+
+SEARCHENGINE = YES
+
+# When the SERVER_BASED_SEARCH tag is enabled the search engine will be
+# implemented using a web server instead of a web client using Javascript. There
+# are two flavors of web server based searching depending on the EXTERNAL_SEARCH
+# setting. When disabled, doxygen will generate a PHP script for searching and
+# an index file used by the script. When EXTERNAL_SEARCH is enabled the indexing
+# and searching needs to be provided by external tools. See the section
+# "External Indexing and Searching" for details.
+# The default value is: NO.
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+SERVER_BASED_SEARCH = NO
+
+# When EXTERNAL_SEARCH tag is enabled doxygen will no longer generate the PHP
+# script for searching. Instead the search results are written to an XML file
+# which needs to be processed by an external indexer. Doxygen will invoke an
+# external search engine pointed to by the SEARCHENGINE_URL option to obtain the
+# search results.
+#
+# Doxygen ships with an example indexer (doxyindexer) and search engine
+# (doxysearch.cgi) which are based on the open source search engine library
+# Xapian (see: http://xapian.org/).
+#
+# See the section "External Indexing and Searching" for details.
+# The default value is: NO.
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+EXTERNAL_SEARCH = NO
+
+# The SEARCHENGINE_URL should point to a search engine hosted by a web server
+# which will return the search results when EXTERNAL_SEARCH is enabled.
+#
+# Doxygen ships with an example indexer (doxyindexer) and search engine
+# (doxysearch.cgi) which are based on the open source search engine library
+# Xapian (see: http://xapian.org/). See the section "External Indexing and
+# Searching" for details.
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+SEARCHENGINE_URL =
+
+# When SERVER_BASED_SEARCH and EXTERNAL_SEARCH are both enabled the unindexed
+# search data is written to a file for indexing by an external tool. With the
+# SEARCHDATA_FILE tag the name of this file can be specified.
+# The default file is: searchdata.xml.
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+SEARCHDATA_FILE = searchdata.xml
+
+# When SERVER_BASED_SEARCH and EXTERNAL_SEARCH are both enabled the
+# EXTERNAL_SEARCH_ID tag can be used as an identifier for the project. This is
+# useful in combination with EXTRA_SEARCH_MAPPINGS to search through multiple
+# projects and redirect the results back to the right project.
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+EXTERNAL_SEARCH_ID =
+
+# The EXTRA_SEARCH_MAPPINGS tag can be used to enable searching through doxygen
+# projects other than the one defined by this configuration file, but that are
+# all added to the same external search index. Each project needs to have a
+# unique id set via EXTERNAL_SEARCH_ID. The search mapping then maps the id of
+# to a relative location where the documentation can be found. The format is:
+# EXTRA_SEARCH_MAPPINGS = tagname1=loc1 tagname2=loc2 ...
+# This tag requires that the tag SEARCHENGINE is set to YES.
+
+EXTRA_SEARCH_MAPPINGS =
+
+#---------------------------------------------------------------------------
+# Configuration options related to the LaTeX output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output.
+# The default value is: YES.
+
+GENERATE_LATEX = YES
+
+# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. If a
+# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
+# it.
+# The default directory is: latex.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_OUTPUT = latex
+
+# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be
+# invoked.
+#
+# Note that when enabling USE_PDFLATEX this option is only used for generating
+# bitmaps for formulas in the HTML output, but not in the Makefile that is
+# written to the output directory.
+# The default file is: latex.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_CMD_NAME = latex
+
+# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to generate
+# index for LaTeX.
+# The default file is: makeindex.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+MAKEINDEX_CMD_NAME = makeindex
+
+# If the COMPACT_LATEX tag is set to YES, doxygen generates more compact LaTeX
+# documents. This may be useful for small projects and may help to save some
+# trees in general.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+COMPACT_LATEX = NO
+
+# The PAPER_TYPE tag can be used to set the paper type that is used by the
+# printer.
+# Possible values are: a4 (210 x 297 mm), letter (8.5 x 11 inches), legal (8.5 x
+# 14 inches) and executive (7.25 x 10.5 inches).
+# The default value is: a4.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+PAPER_TYPE = a4
+
+# The EXTRA_PACKAGES tag can be used to specify one or more LaTeX package names
+# that should be included in the LaTeX output. The package can be specified just
+# by its name or with the correct syntax as to be used with the LaTeX
+# \usepackage command. To get the times font for instance you can specify :
+# EXTRA_PACKAGES=times or EXTRA_PACKAGES={times}
+# To use the option intlimits with the amsmath package you can specify:
+# EXTRA_PACKAGES=[intlimits]{amsmath}
+# If left blank no extra packages will be included.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+EXTRA_PACKAGES =
+
+# The LATEX_HEADER tag can be used to specify a personal LaTeX header for the
+# generated LaTeX document. The header should contain everything until the first
+# chapter. If it is left blank doxygen will generate a standard header. See
+# section "Doxygen usage" for information on how to let doxygen write the
+# default header to a separate file.
+#
+# Note: Only use a user-defined header if you know what you are doing! The
+# following commands have a special meaning inside the header: $title,
+# $datetime, $date, $doxygenversion, $projectname, $projectnumber,
+# $projectbrief, $projectlogo. Doxygen will replace $title with the empty
+# string, for the replacement values of the other commands the user is referred
+# to HTML_HEADER.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_HEADER =
+
+# The LATEX_FOOTER tag can be used to specify a personal LaTeX footer for the
+# generated LaTeX document. The footer should contain everything after the last
+# chapter. If it is left blank doxygen will generate a standard footer. See
+# LATEX_HEADER for more information on how to generate a default footer and what
+# special commands can be used inside the footer.
+#
+# Note: Only use a user-defined footer if you know what you are doing!
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_FOOTER =
+
+# The LATEX_EXTRA_STYLESHEET tag can be used to specify additional user-defined
+# LaTeX style sheets that are included after the standard style sheets created
+# by doxygen. Using this option one can overrule certain style aspects. Doxygen
+# will copy the style sheet files to the output directory.
+# Note: The order of the extra style sheet files is of importance (e.g. the last
+# style sheet in the list overrules the setting of the previous ones in the
+# list).
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_EXTRA_STYLESHEET =
+
+# The LATEX_EXTRA_FILES tag can be used to specify one or more extra images or
+# other source files which should be copied to the LATEX_OUTPUT output
+# directory. Note that the files will be copied as-is; there are no commands or
+# markers available.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_EXTRA_FILES =
+
+# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated is
+# prepared for conversion to PDF (using ps2pdf or pdflatex). The PDF file will
+# contain links (just like the HTML output) instead of page references. This
+# makes the output suitable for online browsing using a PDF viewer.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+PDF_HYPERLINKS = YES
+
+# If the USE_PDFLATEX tag is set to YES, doxygen will use pdflatex to generate
+# the PDF file directly from the LaTeX files. Set this option to YES, to get a
+# higher quality PDF documentation.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+USE_PDFLATEX = YES
+
+# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \batchmode
+# command to the generated LaTeX files. This will instruct LaTeX to keep running
+# if errors occur, instead of asking the user for help. This option is also used
+# when generating formulas in HTML.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_BATCHMODE = NO
+
+# If the LATEX_HIDE_INDICES tag is set to YES then doxygen will not include the
+# index chapters (such as File Index, Compound Index, etc.) in the output.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_HIDE_INDICES = NO
+
+# If the LATEX_SOURCE_CODE tag is set to YES then doxygen will include source
+# code with syntax highlighting in the LaTeX output.
+#
+# Note that which sources are shown also depends on other settings such as
+# SOURCE_BROWSER.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_SOURCE_CODE = NO
+
+# The LATEX_BIB_STYLE tag can be used to specify the style to use for the
+# bibliography, e.g. plainnat, or ieeetr. See
+# http://en.wikipedia.org/wiki/BibTeX and \cite for more info.
+# The default value is: plain.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_BIB_STYLE = plain
+
+# If the LATEX_TIMESTAMP tag is set to YES then the footer of each generated
+# page will contain the date and time when the page was generated. Setting this
+# to NO can help when comparing the output of multiple runs.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_LATEX is set to YES.
+
+LATEX_TIMESTAMP = NO
+
+#---------------------------------------------------------------------------
+# Configuration options related to the RTF output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_RTF tag is set to YES, doxygen will generate RTF output. The
+# RTF output is optimized for Word 97 and may not look too pretty with other RTF
+# readers/editors.
+# The default value is: NO.
+
+GENERATE_RTF = NO
+
+# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. If a
+# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
+# it.
+# The default directory is: rtf.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+RTF_OUTPUT = rtf
+
+# If the COMPACT_RTF tag is set to YES, doxygen generates more compact RTF
+# documents. This may be useful for small projects and may help to save some
+# trees in general.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+COMPACT_RTF = NO
+
+# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated will
+# contain hyperlink fields. The RTF file will contain links (just like the HTML
+# output) instead of page references. This makes the output suitable for online
+# browsing using Word or some other Word compatible readers that support those
+# fields.
+#
+# Note: WordPad (write) and others do not support links.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+RTF_HYPERLINKS = NO
+
+# Load stylesheet definitions from file. Syntax is similar to doxygen's config
+# file, i.e. a series of assignments. You only have to provide replacements,
+# missing definitions are set to their default value.
+#
+# See also section "Doxygen usage" for information on how to generate the
+# default style sheet that doxygen normally uses.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+RTF_STYLESHEET_FILE =
+
+# Set optional variables used in the generation of an RTF document. Syntax is
+# similar to doxygen's config file. A template extensions file can be generated
+# using doxygen -e rtf extensionFile.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+RTF_EXTENSIONS_FILE =
+
+# If the RTF_SOURCE_CODE tag is set to YES then doxygen will include source code
+# with syntax highlighting in the RTF output.
+#
+# Note that which sources are shown also depends on other settings such as
+# SOURCE_BROWSER.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_RTF is set to YES.
+
+RTF_SOURCE_CODE = NO
+
+#---------------------------------------------------------------------------
+# Configuration options related to the man page output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_MAN tag is set to YES, doxygen will generate man pages for
+# classes and files.
+# The default value is: NO.
+
+GENERATE_MAN = NO
+
+# The MAN_OUTPUT tag is used to specify where the man pages will be put. If a
+# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
+# it. A directory man3 will be created inside the directory specified by
+# MAN_OUTPUT.
+# The default directory is: man.
+# This tag requires that the tag GENERATE_MAN is set to YES.
+
+MAN_OUTPUT = man
+
+# The MAN_EXTENSION tag determines the extension that is added to the generated
+# man pages. In case the manual section does not start with a number, the number
+# 3 is prepended. The dot (.) at the beginning of the MAN_EXTENSION tag is
+# optional.
+# The default value is: .3.
+# This tag requires that the tag GENERATE_MAN is set to YES.
+
+MAN_EXTENSION = .3
+
+# The MAN_SUBDIR tag determines the name of the directory created within
+# MAN_OUTPUT in which the man pages are placed. If defaults to man followed by
+# MAN_EXTENSION with the initial . removed.
+# This tag requires that the tag GENERATE_MAN is set to YES.
+
+MAN_SUBDIR =
+
+# If the MAN_LINKS tag is set to YES and doxygen generates man output, then it
+# will generate one additional man file for each entity documented in the real
+# man page(s). These additional files only source the real man page, but without
+# them the man command would be unable to find the correct page.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_MAN is set to YES.
+
+MAN_LINKS = NO
+
+#---------------------------------------------------------------------------
+# Configuration options related to the XML output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_XML tag is set to YES, doxygen will generate an XML file that
+# captures the structure of the code including all documentation.
+# The default value is: NO.
+
+GENERATE_XML = YES
+
+# The XML_OUTPUT tag is used to specify where the XML pages will be put. If a
+# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
+# it.
+# The default directory is: xml.
+# This tag requires that the tag GENERATE_XML is set to YES.
+
+XML_OUTPUT = xml
+
+# If the XML_PROGRAMLISTING tag is set to YES, doxygen will dump the program
+# listings (including syntax highlighting and cross-referencing information) to
+# the XML output. Note that enabling this will significantly increase the size
+# of the XML output.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_XML is set to YES.
+
+XML_PROGRAMLISTING = YES
+
+#---------------------------------------------------------------------------
+# Configuration options related to the DOCBOOK output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_DOCBOOK tag is set to YES, doxygen will generate Docbook files
+# that can be used to generate PDF.
+# The default value is: NO.
+
+GENERATE_DOCBOOK = NO
+
+# The DOCBOOK_OUTPUT tag is used to specify where the Docbook pages will be put.
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be put in
+# front of it.
+# The default directory is: docbook.
+# This tag requires that the tag GENERATE_DOCBOOK is set to YES.
+
+DOCBOOK_OUTPUT = docbook
+
+# If the DOCBOOK_PROGRAMLISTING tag is set to YES, doxygen will include the
+# program listings (including syntax highlighting and cross-referencing
+# information) to the DOCBOOK output. Note that enabling this will significantly
+# increase the size of the DOCBOOK output.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_DOCBOOK is set to YES.
+
+DOCBOOK_PROGRAMLISTING = NO
+
+#---------------------------------------------------------------------------
+# Configuration options for the AutoGen Definitions output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_AUTOGEN_DEF tag is set to YES, doxygen will generate an
+# AutoGen Definitions (see http://autogen.sf.net) file that captures the
+# structure of the code including all documentation. Note that this feature is
+# still experimental and incomplete at the moment.
+# The default value is: NO.
+
+GENERATE_AUTOGEN_DEF = NO
+
+#---------------------------------------------------------------------------
+# Configuration options related to the Perl module output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_PERLMOD tag is set to YES, doxygen will generate a Perl module
+# file that captures the structure of the code including all documentation.
+#
+# Note that this feature is still experimental and incomplete at the moment.
+# The default value is: NO.
+
+GENERATE_PERLMOD = NO
+
+# If the PERLMOD_LATEX tag is set to YES, doxygen will generate the necessary
+# Makefile rules, Perl scripts and LaTeX code to be able to generate PDF and DVI
+# output from the Perl module output.
+# The default value is: NO.
+# This tag requires that the tag GENERATE_PERLMOD is set to YES.
+
+PERLMOD_LATEX = NO
+
+# If the PERLMOD_PRETTY tag is set to YES, the Perl module output will be nicely
+# formatted so it can be parsed by a human reader. This is useful if you want to
+# understand what is going on. On the other hand, if this tag is set to NO, the
+# size of the Perl module output will be much smaller and Perl will parse it
+# just the same.
+# The default value is: YES.
+# This tag requires that the tag GENERATE_PERLMOD is set to YES.
+
+PERLMOD_PRETTY = YES
+
+# The names of the make variables in the generated doxyrules.make file are
+# prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. This is useful
+# so different doxyrules.make files included by the same Makefile don't
+# overwrite each other's variables.
+# This tag requires that the tag GENERATE_PERLMOD is set to YES.
+
+PERLMOD_MAKEVAR_PREFIX =
+
+#---------------------------------------------------------------------------
+# Configuration options related to the preprocessor
+#---------------------------------------------------------------------------
+
+# If the ENABLE_PREPROCESSING tag is set to YES, doxygen will evaluate all
+# C-preprocessor directives found in the sources and include files.
+# The default value is: YES.
+
+ENABLE_PREPROCESSING = YES
+
+# If the MACRO_EXPANSION tag is set to YES, doxygen will expand all macro names
+# in the source code. If set to NO, only conditional compilation will be
+# performed. Macro expansion can be done in a controlled way by setting
+# EXPAND_ONLY_PREDEF to YES.
+# The default value is: NO.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+MACRO_EXPANSION = YES
+
+# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES then
+# the macro expansion is limited to the macros specified with the PREDEFINED and
+# EXPAND_AS_DEFINED tags.
+# The default value is: NO.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+EXPAND_ONLY_PREDEF = YES
+
+# If the SEARCH_INCLUDES tag is set to YES, the include files in the
+# INCLUDE_PATH will be searched if a #include is found.
+# The default value is: YES.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+SEARCH_INCLUDES = YES
+
+# The INCLUDE_PATH tag can be used to specify one or more directories that
+# contain include files that are not input files but should be processed by the
+# preprocessor.
+# This tag requires that the tag SEARCH_INCLUDES is set to YES.
+
+INCLUDE_PATH =
+
+# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard
+# patterns (like *.h and *.hpp) to filter out the header-files in the
+# directories. If left blank, the patterns specified with FILE_PATTERNS will be
+# used.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+INCLUDE_FILE_PATTERNS =
+
+# The PREDEFINED tag can be used to specify one or more macro names that are
+# defined before the preprocessor is started (similar to the -D option of e.g.
+# gcc). The argument of the tag is a list of macros of the form: name or
+# name=definition (no spaces). If the definition and the "=" are omitted, "=1"
+# is assumed. To prevent a macro definition from being undefined via #undef or
+# recursively expanded use the := operator instead of the = operator.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+PREDEFINED = CBOR_CUSTOM_ALLOC CBOR_RESTRICT_POINTER= CBOR_EXPORT
+
+# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
+# tag can be used to specify a list of macro names that should be expanded. The
+# macro definition that is found in the sources will be used. Use the PREDEFINED
+# tag if you want to use a different macro definition that overrules the
+# definition found in the source code.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+EXPAND_AS_DEFINED = CBOR_RESTRICT_POINTER
+
+# If the SKIP_FUNCTION_MACROS tag is set to YES then doxygen's preprocessor will
+# remove all references to function-like macros that are alone on a line, have
+# an all uppercase name, and do not end with a semicolon. Such function macros
+# are typically used for boiler-plate code, and will confuse the parser if not
+# removed.
+# The default value is: YES.
+# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
+
+SKIP_FUNCTION_MACROS = YES
+
+#---------------------------------------------------------------------------
+# Configuration options related to external references
+#---------------------------------------------------------------------------
+
+# The TAGFILES tag can be used to specify one or more tag files. For each tag
+# file the location of the external documentation should be added. The format of
+# a tag file without this location is as follows:
+# TAGFILES = file1 file2 ...
+# Adding location for the tag files is done as follows:
+# TAGFILES = file1=loc1 "file2 = loc2" ...
+# where loc1 and loc2 can be relative or absolute paths or URLs. See the
+# section "Linking to external documentation" for more information about the use
+# of tag files.
+# Note: Each tag file must have a unique name (where the name does NOT include
+# the path). If a tag file is not located in the directory in which doxygen is
+# run, you must also specify the path to the tagfile here.
+
+TAGFILES =
+
+# When a file name is specified after GENERATE_TAGFILE, doxygen will create a
+# tag file that is based on the input files it reads. See section "Linking to
+# external documentation" for more information about the usage of tag files.
+
+GENERATE_TAGFILE =
+
+# If the ALLEXTERNALS tag is set to YES, all external class will be listed in
+# the class index. If set to NO, only the inherited external classes will be
+# listed.
+# The default value is: NO.
+
+ALLEXTERNALS = NO
+
+# If the EXTERNAL_GROUPS tag is set to YES, all external groups will be listed
+# in the modules index. If set to NO, only the current project's groups will be
+# listed.
+# The default value is: YES.
+
+EXTERNAL_GROUPS = YES
+
+# If the EXTERNAL_PAGES tag is set to YES, all external pages will be listed in
+# the related pages index. If set to NO, only the current project's pages will
+# be listed.
+# The default value is: YES.
+
+EXTERNAL_PAGES = YES
+
+# The PERL_PATH should be the absolute path and name of the perl script
+# interpreter (i.e. the result of 'which perl').
+# The default file (with absolute path) is: /usr/bin/perl.
+
+PERL_PATH = /usr/bin/perl
+
+#---------------------------------------------------------------------------
+# Configuration options related to the dot tool
+#---------------------------------------------------------------------------
+
+# If the CLASS_DIAGRAMS tag is set to YES, doxygen will generate a class diagram
+# (in HTML and LaTeX) for classes with base or super classes. Setting the tag to
+# NO turns the diagrams off. Note that this option also works with HAVE_DOT
+# disabled, but it is recommended to install and use dot, since it yields more
+# powerful graphs.
+# The default value is: YES.
+
+CLASS_DIAGRAMS = YES
+
+# You can define message sequence charts within doxygen comments using the \msc
+# command. Doxygen will then run the mscgen tool (see:
+# http://www.mcternan.me.uk/mscgen/)) to produce the chart and insert it in the
+# documentation. The MSCGEN_PATH tag allows you to specify the directory where
+# the mscgen tool resides. If left empty the tool is assumed to be found in the
+# default search path.
+
+MSCGEN_PATH =
+
+# You can include diagrams made with dia in doxygen documentation. Doxygen will
+# then run dia to produce the diagram and insert it in the documentation. The
+# DIA_PATH tag allows you to specify the directory where the dia binary resides.
+# If left empty dia is assumed to be found in the default search path.
+
+DIA_PATH =
+
+# If set to YES the inheritance and collaboration graphs will hide inheritance
+# and usage relations if the target is undocumented or is not a class.
+# The default value is: YES.
+
+HIDE_UNDOC_RELATIONS = YES
+
+# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is
+# available from the path. This tool is part of Graphviz (see:
+# http://www.graphviz.org/), a graph visualization toolkit from AT&T and Lucent
+# Bell Labs. The other options in this section have no effect if this option is
+# set to NO
+# The default value is: NO.
+
+HAVE_DOT = NO
+
+# The DOT_NUM_THREADS specifies the number of dot invocations doxygen is allowed
+# to run in parallel. When set to 0 doxygen will base this on the number of
+# processors available in the system. You can set it explicitly to a value
+# larger than 0 to get control over the balance between CPU load and processing
+# speed.
+# Minimum value: 0, maximum value: 32, default value: 0.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_NUM_THREADS = 0
+
+# When you want a differently looking font in the dot files that doxygen
+# generates you can specify the font name using DOT_FONTNAME. You need to make
+# sure dot is able to find the font, which can be done by putting it in a
+# standard location or by setting the DOTFONTPATH environment variable or by
+# setting DOT_FONTPATH to the directory containing the font.
+# The default value is: Helvetica.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_FONTNAME = Helvetica
+
+# The DOT_FONTSIZE tag can be used to set the size (in points) of the font of
+# dot graphs.
+# Minimum value: 4, maximum value: 24, default value: 10.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_FONTSIZE = 10
+
+# By default doxygen will tell dot to use the default font as specified with
+# DOT_FONTNAME. If you specify a different font using DOT_FONTNAME you can set
+# the path where dot can find it using this tag.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_FONTPATH =
+
+# If the CLASS_GRAPH tag is set to YES then doxygen will generate a graph for
+# each documented class showing the direct and indirect inheritance relations.
+# Setting this tag to YES will force the CLASS_DIAGRAMS tag to NO.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+CLASS_GRAPH = YES
+
+# If the COLLABORATION_GRAPH tag is set to YES then doxygen will generate a
+# graph for each documented class showing the direct and indirect implementation
+# dependencies (inheritance, containment, and class references variables) of the
+# class with other documented classes.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+COLLABORATION_GRAPH = YES
+
+# If the GROUP_GRAPHS tag is set to YES then doxygen will generate a graph for
+# groups, showing the direct groups dependencies.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+GROUP_GRAPHS = YES
+
+# If the UML_LOOK tag is set to YES, doxygen will generate inheritance and
+# collaboration diagrams in a style similar to the OMG's Unified Modeling
+# Language.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+UML_LOOK = NO
+
+# If the UML_LOOK tag is enabled, the fields and methods are shown inside the
+# class node. If there are many fields or methods and many nodes the graph may
+# become too big to be useful. The UML_LIMIT_NUM_FIELDS threshold limits the
+# number of items for each type to make the size more manageable. Set this to 0
+# for no limit. Note that the threshold may be exceeded by 50% before the limit
+# is enforced. So when you set the threshold to 10, up to 15 fields may appear,
+# but if the number exceeds 15, the total amount of fields shown is limited to
+# 10.
+# Minimum value: 0, maximum value: 100, default value: 10.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+UML_LIMIT_NUM_FIELDS = 10
+
+# If the TEMPLATE_RELATIONS tag is set to YES then the inheritance and
+# collaboration graphs will show the relations between templates and their
+# instances.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+TEMPLATE_RELATIONS = NO
+
+# If the INCLUDE_GRAPH, ENABLE_PREPROCESSING and SEARCH_INCLUDES tags are set to
+# YES then doxygen will generate a graph for each documented file showing the
+# direct and indirect include dependencies of the file with other documented
+# files.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+INCLUDE_GRAPH = YES
+
+# If the INCLUDED_BY_GRAPH, ENABLE_PREPROCESSING and SEARCH_INCLUDES tags are
+# set to YES then doxygen will generate a graph for each documented file showing
+# the direct and indirect include dependencies of the file with other documented
+# files.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+INCLUDED_BY_GRAPH = YES
+
+# If the CALL_GRAPH tag is set to YES then doxygen will generate a call
+# dependency graph for every global function or class method.
+#
+# Note that enabling this option will significantly increase the time of a run.
+# So in most cases it will be better to enable call graphs for selected
+# functions only using the \callgraph command. Disabling a call graph can be
+# accomplished by means of the command \hidecallgraph.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+CALL_GRAPH = NO
+
+# If the CALLER_GRAPH tag is set to YES then doxygen will generate a caller
+# dependency graph for every global function or class method.
+#
+# Note that enabling this option will significantly increase the time of a run.
+# So in most cases it will be better to enable caller graphs for selected
+# functions only using the \callergraph command. Disabling a caller graph can be
+# accomplished by means of the command \hidecallergraph.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+CALLER_GRAPH = NO
+
+# If the GRAPHICAL_HIERARCHY tag is set to YES then doxygen will graphical
+# hierarchy of all classes instead of a textual one.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+GRAPHICAL_HIERARCHY = YES
+
+# If the DIRECTORY_GRAPH tag is set to YES then doxygen will show the
+# dependencies a directory has on other directories in a graphical way. The
+# dependency relations are determined by the #include relations between the
+# files in the directories.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DIRECTORY_GRAPH = YES
+
+# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images
+# generated by dot. For an explanation of the image formats see the section
+# output formats in the documentation of the dot tool (Graphviz (see:
+# http://www.graphviz.org/)).
+# Note: If you choose svg you need to set HTML_FILE_EXTENSION to xhtml in order
+# to make the SVG files visible in IE 9+ (other browsers do not have this
+# requirement).
+# Possible values are: png, jpg, gif, svg, png:gd, png:gd:gd, png:cairo,
+# png:cairo:gd, png:cairo:cairo, png:cairo:gdiplus, png:gdiplus and
+# png:gdiplus:gdiplus.
+# The default value is: png.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_IMAGE_FORMAT = png
+
+# If DOT_IMAGE_FORMAT is set to svg, then this option can be set to YES to
+# enable generation of interactive SVG images that allow zooming and panning.
+#
+# Note that this requires a modern browser other than Internet Explorer. Tested
+# and working are Firefox, Chrome, Safari, and Opera.
+# Note: For IE 9+ you need to set HTML_FILE_EXTENSION to xhtml in order to make
+# the SVG files visible. Older versions of IE do not have SVG support.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+INTERACTIVE_SVG = NO
+
+# The DOT_PATH tag can be used to specify the path where the dot tool can be
+# found. If left blank, it is assumed the dot tool can be found in the path.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_PATH =
+
+# The DOTFILE_DIRS tag can be used to specify one or more directories that
+# contain dot files that are included in the documentation (see the \dotfile
+# command).
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOTFILE_DIRS =
+
+# The MSCFILE_DIRS tag can be used to specify one or more directories that
+# contain msc files that are included in the documentation (see the \mscfile
+# command).
+
+MSCFILE_DIRS =
+
+# The DIAFILE_DIRS tag can be used to specify one or more directories that
+# contain dia files that are included in the documentation (see the \diafile
+# command).
+
+DIAFILE_DIRS =
+
+# When using plantuml, the PLANTUML_JAR_PATH tag should be used to specify the
+# path where java can find the plantuml.jar file. If left blank, it is assumed
+# PlantUML is not used or called during a preprocessing step. Doxygen will
+# generate a warning when it encounters a \startuml command in this case and
+# will not generate output for the diagram.
+
+PLANTUML_JAR_PATH =
+
+# When using plantuml, the specified paths are searched for files specified by
+# the !include statement in a plantuml block.
+
+PLANTUML_INCLUDE_PATH =
+
+# The DOT_GRAPH_MAX_NODES tag can be used to set the maximum number of nodes
+# that will be shown in the graph. If the number of nodes in a graph becomes
+# larger than this value, doxygen will truncate the graph, which is visualized
+# by representing a node as a red box. Note that doxygen if the number of direct
+# children of the root node in a graph is already larger than
+# DOT_GRAPH_MAX_NODES then the graph will not be shown at all. Also note that
+# the size of a graph can be further restricted by MAX_DOT_GRAPH_DEPTH.
+# Minimum value: 0, maximum value: 10000, default value: 50.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_GRAPH_MAX_NODES = 50
+
+# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the graphs
+# generated by dot. A depth value of 3 means that only nodes reachable from the
+# root by following a path via at most 3 edges will be shown. Nodes that lay
+# further from the root node will be omitted. Note that setting this option to 1
+# or 2 may greatly reduce the computation time needed for large code bases. Also
+# note that the size of a graph can be further restricted by
+# DOT_GRAPH_MAX_NODES. Using a depth of 0 means no depth restriction.
+# Minimum value: 0, maximum value: 1000, default value: 0.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+MAX_DOT_GRAPH_DEPTH = 0
+
+# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent
+# background. This is disabled by default, because dot on Windows does not seem
+# to support this out of the box.
+#
+# Warning: Depending on the platform used, enabling this option may lead to
+# badly anti-aliased labels on the edges of a graph (i.e. they become hard to
+# read).
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_TRANSPARENT = NO
+
+# Set the DOT_MULTI_TARGETS tag to YES to allow dot to generate multiple output
+# files in one run (i.e. multiple -o and -T options on the command line). This
+# makes dot run faster, but since only newer versions of dot (>1.8.10) support
+# this, this feature is disabled by default.
+# The default value is: NO.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_MULTI_TARGETS = YES
+
+# If the GENERATE_LEGEND tag is set to YES doxygen will generate a legend page
+# explaining the meaning of the various boxes and arrows in the dot generated
+# graphs.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+GENERATE_LEGEND = YES
+
+# If the DOT_CLEANUP tag is set to YES, doxygen will remove the intermediate dot
+# files that are used to generate the various graphs.
+# The default value is: YES.
+# This tag requires that the tag HAVE_DOT is set to YES.
+
+DOT_CLEANUP = YES
diff --git a/LICENSE.md b/LICENSE.md
new file mode 100644
index 000000000000..49e9b5390364
--- /dev/null
+++ b/LICENSE.md
@@ -0,0 +1,21 @@
+MIT License
+
+Copyright (c) 2014-2017 Pavel Kalvoda
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 000000000000..cffae9191d01
--- /dev/null
+++ b/README.md
@@ -0,0 +1,122 @@
+# [libcbor](https://github.com/PJK/libcbor)
+
+[![Build Status](https://travis-ci.org/PJK/libcbor.svg?branch=master)](https://travis-ci.org/PJK/libcbor)
+[![Build status](https://ci.appveyor.com/api/projects/status/8kkmvmefelsxp5u2?svg=true)](https://ci.appveyor.com/project/PJK/libcbor)
+[![Documentation Status](https://readthedocs.org/projects/libcbor/badge/?version=latest)](https://readthedocs.org/projects/libcbor/?badge=latest)
+[![latest packaged version(s)](https://repology.org/badge/latest-versions/libcbor.svg)](https://repology.org/project/libcbor/versions)
+[![codecov](https://codecov.io/gh/PJK/libcbor/branch/master/graph/badge.svg)](https://codecov.io/gh/PJK/libcbor)
+
+**libcbor** is a C library for parsing and generating [CBOR](http://tools.ietf.org/html/rfc7049), the general-purpose schema-less binary data format.
+
+## Main features
+ - Complete RFC conformance
+ - Robust C99 implementation
+ - Layered architecture offers both control and convenience
+ - Flexible memory management
+ - No shared global state - threading friendly
+ - Proper handling of UTF-8
+ - Full support for streams & incremental processing
+ - Extensive documentation and test suite
+ - No runtime dependencies, small footprint
+
+## Getting started
+
+### Compile from source
+
+```bash
+git clone https://github.com/PJK/libcbor
+cmake -DCMAKE_BUILD_TYPE=Release -DCBOR_CUSTOM_ALLOC=ON libcbor
+make
+make install
+```
+
+### Homebrew
+
+```bash
+brew install libcbor
+```
+
+### Ubuntu 18.04 and above
+
+```bash
+sudo add-apt-repository universe
+sudo apt-get install libcbor-dev
+```
+
+### Fedora & RPM friends
+
+```bash
+yum install libcbor-devel
+```
+
+### Others
+
+<details>
+ <summary>Packaged libcbor is available from 15+ major repositories. Click here for more detail</summary>
+
+ [![Packaging status](https://repology.org/badge/vertical-allrepos/libcbor.svg)](https://repology.org/project/libcbor/versions)
+</details>
+
+## Usage example
+
+```c
+#include <cbor.h>
+#include <stdio.h>
+
+int main(int argc, char * argv[])
+{
+ /* Preallocate the map structure */
+ cbor_item_t * root = cbor_new_definite_map(2);
+ /* Add the content */
+ cbor_map_add(root, (struct cbor_pair) {
+ .key = cbor_move(cbor_build_string("Is CBOR awesome?")),
+ .value = cbor_move(cbor_build_bool(true))
+ });
+ cbor_map_add(root, (struct cbor_pair) {
+ .key = cbor_move(cbor_build_uint8(42)),
+ .value = cbor_move(cbor_build_string("Is the answer"))
+ });
+ /* Output: `length` bytes of data in the `buffer` */
+ unsigned char * buffer;
+ size_t buffer_size,
+ length = cbor_serialize_alloc(root, &buffer, &buffer_size);
+
+ fwrite(buffer, 1, length, stdout);
+ free(buffer);
+
+ fflush(stdout);
+ cbor_decref(&root);
+}
+```
+
+## Documentation
+Get the latest documentation at [libcbor.readthedocs.org](http://libcbor.readthedocs.org/)
+
+## Contributions
+
+All bug reports and contributions are welcome. Please see https://github.com/PJK/libcbor for more info.
+
+Kudos to all the [contributors](https://github.com/PJK/libcbor/graphs/contributors)!
+
+## License
+The MIT License (MIT)
+
+Copyright (c) Pavel Kalvoda, 2014-2020
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/appveyor.yml b/appveyor.yml
new file mode 100644
index 000000000000..452ced5666b7
--- /dev/null
+++ b/appveyor.yml
@@ -0,0 +1,27 @@
+image: Visual Studio 2015
+version: '{build}'
+
+branches:
+ except:
+ - gh-pages
+
+platform: x64
+
+
+environment:
+ matrix:
+ - CMAKE_GENERATOR: "Visual Studio 14 2015 Win64"
+
+# Via https://github.com/apitrace/apitrace/blob/master/appveyor.yml
+
+before_build:
+- cmake -H. -Bbuild -G "%CMAKE_GENERATOR%"
+- C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\x86_amd64\CL.exe /?
+
+build_script:
+- if "%APPVEYOR_REPO_TAG%"=="true" (set CONFIGURATION=RelWithDebInfo) else (set CONFIGURATION=Debug)
+- cmake --build build --config "%CONFIGURATION%"
+
+# TODO enable CMocka tests, maybe package the binaries
+# TODO add MinGW
+# TODO add older MSVC \ No newline at end of file
diff --git a/clang-format.sh b/clang-format.sh
new file mode 100755
index 000000000000..e4076500c4f0
--- /dev/null
+++ b/clang-format.sh
@@ -0,0 +1,17 @@
+#!/usr/bin/env bash
+
+# Usage: ./clang-format.sh <extra arguments>
+
+DIRS="src test examples"
+SOURCES=$(find ${DIRS} -name "*.c")
+SOURCES+=" $(find ${DIRS} -name "*.h")"
+SOURCES+=" $(find ${DIRS} -name "*.cpp")"
+
+# TravisCI workaround to use new clang-format while avoiding painful aliasing
+# into the subshell
+if which clang-format-8; then
+ clang-format-8 $@ -style=file -i ${SOURCES}
+else
+ clang-format $@ -style=file -i ${SOURCES}
+fi
+
diff --git a/codecov.yml b/codecov.yml
new file mode 100644
index 000000000000..e374997dcd15
--- /dev/null
+++ b/codecov.yml
@@ -0,0 +1,2 @@
+ignore:
+ - "test/stream_expectations.c" # Function pointers are not resolved correctly
diff --git a/doc/Makefile b/doc/Makefile
new file mode 100644
index 000000000000..97a6bdc99543
--- /dev/null
+++ b/doc/Makefile
@@ -0,0 +1,180 @@
+# Makefile for Sphinx documentation
+#
+
+# You can set these variables from the command line.
+SPHINXOPTS =
+SPHINXBUILD = sphinx-build
+PAPER =
+BUILDDIR = build
+
+# User-friendly check for sphinx-build
+ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1)
+$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/)
+endif
+
+# Internal variables.
+PAPEROPT_a4 = -D latex_paper_size=a4
+PAPEROPT_letter = -D latex_paper_size=letter
+ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source
+# the i18n builder cannot share the environment and doctrees with the others
+I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source
+
+.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext
+
+help:
+ @echo "Please use \`make <target>' where <target> is one of"
+ @echo " html to make standalone HTML files"
+ @echo " dirhtml to make HTML files named index.html in directories"
+ @echo " singlehtml to make a single large HTML file"
+ @echo " pickle to make pickle files"
+ @echo " json to make JSON files"
+ @echo " htmlhelp to make HTML files and a HTML help project"
+ @echo " qthelp to make HTML files and a qthelp project"
+ @echo " devhelp to make HTML files and a Devhelp project"
+ @echo " epub to make an epub"
+ @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
+ @echo " latexpdf to make LaTeX files and run them through pdflatex"
+ @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx"
+ @echo " text to make text files"
+ @echo " man to make manual pages"
+ @echo " texinfo to make Texinfo files"
+ @echo " info to make Texinfo files and run them through makeinfo"
+ @echo " gettext to make PO message catalogs"
+ @echo " changes to make an overview of all changed/added/deprecated items"
+ @echo " xml to make Docutils-native XML files"
+ @echo " pseudoxml to make pseudoxml-XML files for display purposes"
+ @echo " linkcheck to check all external links for integrity"
+ @echo " doctest to run all doctests embedded in the documentation (if enabled)"
+
+clean:
+ rm -rf $(BUILDDIR)/*
+
+html:
+ $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
+ @echo
+ @echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
+
+dirhtml:
+ $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
+ @echo
+ @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
+
+singlehtml:
+ $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
+ @echo
+ @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
+
+pickle:
+ $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
+ @echo
+ @echo "Build finished; now you can process the pickle files."
+
+json:
+ $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
+ @echo
+ @echo "Build finished; now you can process the JSON files."
+
+htmlhelp:
+ $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
+ @echo
+ @echo "Build finished; now you can run HTML Help Workshop with the" \
+ ".hhp project file in $(BUILDDIR)/htmlhelp."
+
+qthelp:
+ $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
+ @echo
+ @echo "Build finished; now you can run "qcollectiongenerator" with the" \
+ ".qhcp project file in $(BUILDDIR)/qthelp, like this:"
+ @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/libcbor.qhcp"
+ @echo "To view the help file:"
+ @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/libcbor.qhc"
+
+devhelp:
+ $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
+ @echo
+ @echo "Build finished."
+ @echo "To view the help file:"
+ @echo "# mkdir -p $$HOME/.local/share/devhelp/libcbor"
+ @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/libcbor"
+ @echo "# devhelp"
+
+epub:
+ $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
+ @echo
+ @echo "Build finished. The epub file is in $(BUILDDIR)/epub."
+
+latex:
+ $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
+ @echo
+ @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
+ @echo "Run \`make' in that directory to run these through (pdf)latex" \
+ "(use \`make latexpdf' here to do that automatically)."
+
+latexpdf:
+ $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
+ @echo "Running LaTeX files through pdflatex..."
+ $(MAKE) -C $(BUILDDIR)/latex all-pdf
+ @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
+
+latexpdfja:
+ $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
+ @echo "Running LaTeX files through platex and dvipdfmx..."
+ $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja
+ @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
+
+text:
+ $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
+ @echo
+ @echo "Build finished. The text files are in $(BUILDDIR)/text."
+
+man:
+ $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
+ @echo
+ @echo "Build finished. The manual pages are in $(BUILDDIR)/man."
+
+texinfo:
+ $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
+ @echo
+ @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
+ @echo "Run \`make' in that directory to run these through makeinfo" \
+ "(use \`make info' here to do that automatically)."
+
+info:
+ $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
+ @echo "Running Texinfo files through makeinfo..."
+ make -C $(BUILDDIR)/texinfo info
+ @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
+
+gettext:
+ $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
+ @echo
+ @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
+
+changes:
+ $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
+ @echo
+ @echo "The overview file is in $(BUILDDIR)/changes."
+
+linkcheck:
+ $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
+ @echo
+ @echo "Link check complete; look for any errors in the above output " \
+ "or in $(BUILDDIR)/linkcheck/output.txt."
+
+doctest:
+ $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
+ @echo "Testing of doctests in the sources finished, look at the " \
+ "results in $(BUILDDIR)/doctest/output.txt."
+
+xml:
+ $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml
+ @echo
+ @echo "Build finished. The XML files are in $(BUILDDIR)/xml."
+
+pseudoxml:
+ $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
+ @echo
+ @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
+
+livehtml: html
+ sphinx-autobuild -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
diff --git a/doc/doxy_frontpage.md b/doc/doxy_frontpage.md
new file mode 100644
index 000000000000..30980c4872d4
--- /dev/null
+++ b/doc/doxy_frontpage.md
@@ -0,0 +1,37 @@
+
+
+This is the development reference of [libcbor](https://github.com/PJK/libcbor). Looking for the [user documentation](https://libcbor.readthedocs.io/)?
+
+
+# Where to start
+
+A couple of pointers for you to start with: `0x00000000`, `0xDEADBEEF`.
+
+If you just want to peek under the hood, have a look at:
+ - \ref src/cbor/common.h
+ - \ref src/cbor/encoding.h
+ - \ref src/cbor.h
+
+If you want to implement your own decoder or see how the default one is made:
+ - \ref src/cbor/internal/builder_callbacks.h
+ - \ref src/cbor/internal/stack.h
+
+For details on encoding and packing (could be useful when porting to exotic platforms):
+ - \ref src/cbor/internal/encoders.h
+ - \ref src/cbor/internal/loaders.h
+
+Streaming driver:
+ - \ref src/cbor/streaming.h
+
+Manipulation routines for particular types:
+ - \ref src/cbor/ints.h
+ - \ref src/cbor/bytestrings.h
+ - \ref src/cbor/strings.h
+ - \ref src/cbor/arrays.h
+ - \ref src/cbor/maps.h
+ - \ref src/cbor/tags.h
+ - \ref src/cbor/floats_ctrls.h
+
+# How to contribute
+
+Please refer to [the repository](https://github.com/PJK/libcbor)
diff --git a/doc/make.bat b/doc/make.bat
new file mode 100644
index 000000000000..b3647968e8a5
--- /dev/null
+++ b/doc/make.bat
@@ -0,0 +1,242 @@
+@ECHO OFF
+
+REM Command file for Sphinx documentation
+
+if "%SPHINXBUILD%" == "" (
+ set SPHINXBUILD=sphinx-build
+)
+set BUILDDIR=build
+set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% source
+set I18NSPHINXOPTS=%SPHINXOPTS% source
+if NOT "%PAPER%" == "" (
+ set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS%
+ set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS%
+)
+
+if "%1" == "" goto help
+
+if "%1" == "help" (
+ :help
+ echo.Please use `make ^<target^>` where ^<target^> is one of
+ echo. html to make standalone HTML files
+ echo. dirhtml to make HTML files named index.html in directories
+ echo. singlehtml to make a single large HTML file
+ echo. pickle to make pickle files
+ echo. json to make JSON files
+ echo. htmlhelp to make HTML files and a HTML help project
+ echo. qthelp to make HTML files and a qthelp project
+ echo. devhelp to make HTML files and a Devhelp project
+ echo. epub to make an epub
+ echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter
+ echo. text to make text files
+ echo. man to make manual pages
+ echo. texinfo to make Texinfo files
+ echo. gettext to make PO message catalogs
+ echo. changes to make an overview over all changed/added/deprecated items
+ echo. xml to make Docutils-native XML files
+ echo. pseudoxml to make pseudoxml-XML files for display purposes
+ echo. linkcheck to check all external links for integrity
+ echo. doctest to run all doctests embedded in the documentation if enabled
+ goto end
+)
+
+if "%1" == "clean" (
+ for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i
+ del /q /s %BUILDDIR%\*
+ goto end
+)
+
+
+%SPHINXBUILD% 2> nul
+if errorlevel 9009 (
+ echo.
+ echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
+ echo.installed, then set the SPHINXBUILD environment variable to point
+ echo.to the full path of the 'sphinx-build' executable. Alternatively you
+ echo.may add the Sphinx directory to PATH.
+ echo.
+ echo.If you don't have Sphinx installed, grab it from
+ echo.http://sphinx-doc.org/
+ exit /b 1
+)
+
+if "%1" == "html" (
+ %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished. The HTML pages are in %BUILDDIR%/html.
+ goto end
+)
+
+if "%1" == "dirhtml" (
+ %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml.
+ goto end
+)
+
+if "%1" == "singlehtml" (
+ %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml.
+ goto end
+)
+
+if "%1" == "pickle" (
+ %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished; now you can process the pickle files.
+ goto end
+)
+
+if "%1" == "json" (
+ %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished; now you can process the JSON files.
+ goto end
+)
+
+if "%1" == "htmlhelp" (
+ %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished; now you can run HTML Help Workshop with the ^
+.hhp project file in %BUILDDIR%/htmlhelp.
+ goto end
+)
+
+if "%1" == "qthelp" (
+ %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished; now you can run "qcollectiongenerator" with the ^
+.qhcp project file in %BUILDDIR%/qthelp, like this:
+ echo.^> qcollectiongenerator %BUILDDIR%\qthelp\libcbor.qhcp
+ echo.To view the help file:
+ echo.^> assistant -collectionFile %BUILDDIR%\qthelp\libcbor.ghc
+ goto end
+)
+
+if "%1" == "devhelp" (
+ %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished.
+ goto end
+)
+
+if "%1" == "epub" (
+ %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished. The epub file is in %BUILDDIR%/epub.
+ goto end
+)
+
+if "%1" == "latex" (
+ %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished; the LaTeX files are in %BUILDDIR%/latex.
+ goto end
+)
+
+if "%1" == "latexpdf" (
+ %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
+ cd %BUILDDIR%/latex
+ make all-pdf
+ cd %BUILDDIR%/..
+ echo.
+ echo.Build finished; the PDF files are in %BUILDDIR%/latex.
+ goto end
+)
+
+if "%1" == "latexpdfja" (
+ %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
+ cd %BUILDDIR%/latex
+ make all-pdf-ja
+ cd %BUILDDIR%/..
+ echo.
+ echo.Build finished; the PDF files are in %BUILDDIR%/latex.
+ goto end
+)
+
+if "%1" == "text" (
+ %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished. The text files are in %BUILDDIR%/text.
+ goto end
+)
+
+if "%1" == "man" (
+ %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished. The manual pages are in %BUILDDIR%/man.
+ goto end
+)
+
+if "%1" == "texinfo" (
+ %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo.
+ goto end
+)
+
+if "%1" == "gettext" (
+ %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished. The message catalogs are in %BUILDDIR%/locale.
+ goto end
+)
+
+if "%1" == "changes" (
+ %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.The overview file is in %BUILDDIR%/changes.
+ goto end
+)
+
+if "%1" == "linkcheck" (
+ %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Link check complete; look for any errors in the above output ^
+or in %BUILDDIR%/linkcheck/output.txt.
+ goto end
+)
+
+if "%1" == "doctest" (
+ %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Testing of doctests in the sources finished, look at the ^
+results in %BUILDDIR%/doctest/output.txt.
+ goto end
+)
+
+if "%1" == "xml" (
+ %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished. The XML files are in %BUILDDIR%/xml.
+ goto end
+)
+
+if "%1" == "pseudoxml" (
+ %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml
+ if errorlevel 1 exit /b 1
+ echo.
+ echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml.
+ goto end
+)
+
+:end
diff --git a/doc/source/api.rst b/doc/source/api.rst
new file mode 100644
index 000000000000..c25629115060
--- /dev/null
+++ b/doc/source/api.rst
@@ -0,0 +1,39 @@
+API
+=======
+
+The data API is centered around :type:`cbor_item_t`, a generic handle for any CBOR item. There are functions to
+
+ - create items,
+ - set items' data,
+ - parse serialized data into items,
+ - manage, move, and links item together.
+
+The single most important thing to keep in mind is: :type:`cbor_item_t` **is an opaque type and should only be manipulated using the appropriate functions!** Think of it as an object.
+
+The *libcbor* API closely follows the semantics outlined by `CBOR standard <http://tools.ietf.org/html/rfc7049>`_. This part of the documentation provides a short overview of the CBOR constructs, as well as a general introduction to the *libcbor* API. Remaining reference can be found in the following files structured by data types.
+
+The API is designed to allow both very tight control & flexibility and general convenience with sane defaults. [#]_ For example, client with very specific requirements (constrained environment, custom application protocol built on top of CBOR, etc.) may choose to take full control (and responsibility) of memory and data structures management by interacting directly with the decoder. Other clients might want to take control of specific aspects (streamed collections, hash maps storage), but leave other responsibilities to *libcbor*. More general clients might prefer to be abstracted away from all aforementioned details and only be presented complete data structures.
+
+
+*libcbor* provides
+ - stateless encoders and decoders
+ - encoding and decoding *drivers*, routines that coordinate encoding and decoding of complex structures
+ - data structures to represent and transform CBOR structures
+ - routines for building and manipulating these structures
+ - utilities for inspection and debugging
+
+.. toctree::
+
+ api/item_types
+ api/item_reference_counting
+ api/decoding
+ api/encoding
+ api/type_0_1
+ api/type_2
+ api/type_3
+ api/type_4
+ api/type_5
+ api/type_6
+ api/type_7
+
+.. [#] http://softwareengineering.vazexqi.com/files/pattern.html
diff --git a/doc/source/api/decoding.rst b/doc/source/api/decoding.rst
new file mode 100644
index 000000000000..e3a41f467a15
--- /dev/null
+++ b/doc/source/api/decoding.rst
@@ -0,0 +1,53 @@
+Decoding
+=============================
+
+The following diagram illustrates the relationship among different parts of libcbor from the decoding standpoint.
+
+::
+
+ ┌──────────────────────────────────────────────────────────────────────────────────────────────┐
+ │ │
+ │ Client application │
+ │ │
+ │ ┌────────────────────────────────────────────┘
+ │ │ ↕
+ │ │ ┌──────────────────────────────────────────┐
+ │ │ │ │
+ │ │ │ Manipulation routines │
+ │ │ │ │
+ │ ┌─────────────────────────────────────┘ └──────────────────────────────────────────┘
+ │ │ ↑ ↑ ↑ ↑
+ │ │ │ │ ┌─────────────╫──────────┬───────────────────┴─┐
+ │ │ │ CDS │ ║ │ │
+ │ │ │ │ PDS ║ PDS PDS
+ │ │ ↓ ↓ ↓ ↓ ↓ ↓
+ │ │ ┌─────────────────┐ ┌────────────────────┐ ┌────────────────────────────┐
+ │ │ │ │ │ │ │ │
+ │ │ │ Custom driver │ ↔ │ Streaming driver │ ↔ │ Default driver │ ↔ CD
+ │ │ │ │ │ │ │ │
+ └───────────┘ └─────────────────┘ └────────────────────┘ └────────────────────────────┘
+ ↕ ↕ ↕ ↕
+ ┌──────────────────────────────────────────────────────────────────────────────────────────────┐
+ │ │
+ │ Stateless event─driven decoder │
+ │ │
+ └──────────────────────────────────────────────────────────────────────────────────────────────┘
+
+ (PSD = Provided Data Structures, CDS = Custom Data Structures)
+
+This section will deal with the API that is labeled as the "Default driver" in the diagram. That is, routines that
+decode complete libcbor data items
+
+.. doxygenfunction:: cbor_load
+
+Associated data structures
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenenum:: cbor_error_code
+
+.. doxygenstruct:: cbor_load_result
+ :members:
+
+.. doxygenstruct:: cbor_error
+ :members:
+
diff --git a/doc/source/api/encoding.rst b/doc/source/api/encoding.rst
new file mode 100644
index 000000000000..af6bbbe7b9d3
--- /dev/null
+++ b/doc/source/api/encoding.rst
@@ -0,0 +1,23 @@
+Encoding
+=============================
+
+The easiest way to encode data items is using the :func:`cbor_serialize` or :func:`cbor_serialize_alloc` functions:
+
+.. doxygenfunction:: cbor_serialize
+.. doxygenfunction:: cbor_serialize_alloc
+
+Type-specific serializers
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+In case you know the type of the item you want to serialize beforehand, you can use one
+of the type-specific serializers.
+
+.. note:: Unless compiled in debug mode, these do not verify the type. Passing an incorrect item will result in an undefined behavior.
+
+.. doxygenfunction:: cbor_serialize_uint
+.. doxygenfunction:: cbor_serialize_negint
+.. doxygenfunction:: cbor_serialize_bytestring
+.. doxygenfunction:: cbor_serialize_string
+.. doxygenfunction:: cbor_serialize_array
+.. doxygenfunction:: cbor_serialize_map
+.. doxygenfunction:: cbor_serialize_tag
+.. doxygenfunction:: cbor_serialize_float_ctrl
diff --git a/doc/source/api/item_reference_counting.rst b/doc/source/api/item_reference_counting.rst
new file mode 100644
index 000000000000..0c0fa7257826
--- /dev/null
+++ b/doc/source/api/item_reference_counting.rst
@@ -0,0 +1,41 @@
+Memory management and reference counting
+===============================================
+
+Due to the nature of its domain, *libcbor* will need to work with heap memory. The stateless decoder and encoder don't allocate any memory.
+
+If you have specific requirements, you should consider rolling your own driver for the stateless API.
+
+Using custom allocator
+^^^^^^^^^^^^^^^^^^^^^^^^
+
+*libcbor* gives you with the ability to provide your own implementations of ``malloc``, ``realloc``, and ``free``. This can be useful if you are using a custom allocator throughout your application, or if you want to implement custom policies (e.g. tighter restrictions on the amount of allocated memory).
+
+In order to use this feature, *libcbor* has to be compiled with the :doc:`appropriate flags </getting_started>`. You can verify the configuration using the ``CBOR_CUSTOM_ALLOC`` macro. A simple usage might be as follows:
+
+.. code-block:: c
+
+ #if CBOR_CUSTOM_ALLOC
+ cbor_set_allocs(malloc, realloc, free);
+ #else
+ #error "libcbor built with support for custom allocation is required"
+ #endif
+
+.. doxygenfunction:: cbor_set_allocs
+
+
+Reference counting
+^^^^^^^^^^^^^^^^^^^^^
+
+As CBOR items may require complex cleanups at the end of their lifetime, there is a reference counting mechanism in place. This also enables very simple GC when integrating *libcbor* into managed environment. Every item starts its life (by either explicit creation, or as a result of parsing) with reference count set to 1. When the refcount reaches zero, it will be destroyed.
+
+Items containing nested items will be destroyed recursively - refcount of every nested item will be decreased by one.
+
+The destruction is synchronous and renders any pointers to items with refcount zero invalid immediately after calling the :func:`cbor_decref`.
+
+
+.. doxygenfunction:: cbor_incref
+.. doxygenfunction:: cbor_decref
+.. doxygenfunction:: cbor_intermediate_decref
+.. doxygenfunction:: cbor_refcount
+.. doxygenfunction:: cbor_move
+.. doxygenfunction:: cbor_copy
diff --git a/doc/source/api/item_types.rst b/doc/source/api/item_types.rst
new file mode 100644
index 000000000000..1452b3e5f90e
--- /dev/null
+++ b/doc/source/api/item_types.rst
@@ -0,0 +1,41 @@
+Types of items
+===============================================
+
+Every :type:`cbor_item_t` has a :type:`cbor_type` associated with it - these constants correspond to the types specified by the `CBOR standard <http://tools.ietf.org/html/rfc7049>`_:
+
+.. doxygenenum:: cbor_type
+
+To find out the type of an item, one can use
+
+.. doxygenfunction:: cbor_typeof
+
+Please note the distinction between functions like :func:`cbor_isa_uint()` and :func:`cbor_is_int()`. The following functions work solely with the major type value.
+
+
+Binary queries
+------------------------
+
+Alternatively, there are functions to query each particular type.
+
+.. warning:: Passing an invalid :type:`cbor_item_t` reference to any of these functions results in undefined behavior.
+
+.. doxygenfunction:: cbor_isa_uint
+.. doxygenfunction:: cbor_isa_negint
+.. doxygenfunction:: cbor_isa_bytestring
+.. doxygenfunction:: cbor_isa_string
+.. doxygenfunction:: cbor_isa_array
+.. doxygenfunction:: cbor_isa_map
+.. doxygenfunction:: cbor_isa_tag
+.. doxygenfunction:: cbor_isa_float_ctrl
+
+
+Logical queries
+------------------------
+
+These functions provide information about the item type from a more high-level perspective
+
+.. doxygenfunction:: cbor_is_int
+.. doxygenfunction:: cbor_is_float
+.. doxygenfunction:: cbor_is_bool
+.. doxygenfunction:: cbor_is_null
+.. doxygenfunction:: cbor_is_undef
diff --git a/doc/source/api/type_0_1.rst b/doc/source/api/type_0_1.rst
new file mode 100644
index 000000000000..4fc851dd1ed1
--- /dev/null
+++ b/doc/source/api/type_0_1.rst
@@ -0,0 +1,91 @@
+Types 0 & 1 – Positive and negative integers
+===============================================
+
+*CBOR* has two types of integers – positive (which may be effectively regarded as unsigned), and negative. There are four possible widths for an integer – 1, 2, 4, or 8 bytes. These are represented by
+
+.. doxygenenum:: cbor_int_width
+
+
+Type 0 - positive integers
+--------------------------
+================================== =========================================
+Corresponding :type:`cbor_type` ``CBOR_TYPE_UINT``
+Number of allocations One per lifetime
+Storage requirements ``sizeof(cbor_item_t) + sizeof(uint*_t)``
+================================== =========================================
+
+**Note:** once a positive integer has been created, its width *cannot* be changed.
+
+Type 1 - negative integers
+--------------------------
+================================== =========================================
+Corresponding :type:`cbor_type` ``CBOR_TYPE_NEGINT``
+Number of allocations One per lifetime
+Storage requirements ``sizeof(cbor_item_t) + sizeof(uint*_t)``
+================================== =========================================
+
+**Note:** once a positive integer has been created, its width *cannot* be changed.
+
+Type 0 & 1
+-------------
+Due to their largely similar semantics, the following functions can be used for both Type 0 and Type 1 items. One can convert between them freely using `the conversion functions <#dealing-with-signedness>`_.
+
+Actual Type of the integer can be checked using :doc:`item types API <item_types>`.
+
+
+
+An integer item is created with one of the four widths. Because integers' `storage is bundled together with the handle </internal#c.cbor_item_t.data>`_, the width cannot be changed over its lifetime.
+
+.. warning::
+
+ Due to the fact that CBOR negative integers represent integers in the range :math:`[-1, -2^N]`, ``cbor_set_uint`` API is somewhat counter-intuitive as the resulting logical value is 1 less. This behavior is necessary in order to permit uniform manipulation with the full range of permitted values. For example, the following snippet
+
+ .. code-block:: c
+
+ cbor_item_t * item = cbor_new_int8();
+ cbor_mark_negint(item);
+ cbor_set_uint8(0);
+
+ will produce an item with the logical value of :math:`-1`. There is, however, an upside to this as well: There is only one representation of zero.
+
+
+Building new items
+------------------------
+.. doxygenfunction:: cbor_build_uint8
+.. doxygenfunction:: cbor_build_uint16
+.. doxygenfunction:: cbor_build_uint32
+.. doxygenfunction:: cbor_build_uint64
+
+
+Retrieving values
+------------------------
+.. doxygenfunction:: cbor_get_uint8
+.. doxygenfunction:: cbor_get_uint16
+.. doxygenfunction:: cbor_get_uint32
+.. doxygenfunction:: cbor_get_uint64
+
+Setting values
+------------------------
+
+.. doxygenfunction:: cbor_set_uint8
+.. doxygenfunction:: cbor_set_uint16
+.. doxygenfunction:: cbor_set_uint32
+.. doxygenfunction:: cbor_set_uint64
+
+Dealing with width
+---------------------
+.. doxygenfunction:: cbor_int_get_width
+
+Dealing with signedness
+--------------------------
+
+.. doxygenfunction:: cbor_mark_uint
+.. doxygenfunction:: cbor_mark_negint
+
+Creating new items
+------------------------
+
+.. doxygenfunction:: cbor_new_int8
+.. doxygenfunction:: cbor_new_int16
+.. doxygenfunction:: cbor_new_int32
+.. doxygenfunction:: cbor_new_int64
diff --git a/doc/source/api/type_2.rst b/doc/source/api/type_2.rst
new file mode 100644
index 000000000000..17df5716ccc2
--- /dev/null
+++ b/doc/source/api/type_2.rst
@@ -0,0 +1,66 @@
+Type 2 – Byte strings
+=============================
+
+CBOR byte strings are just (ordered) series of bytes without further interpretation (unless there is a :doc:`tag <type_6>`). Byte string's length may or may not be known during encoding. These two kinds of byte strings can be distinguished using :func:`cbor_bytestring_is_definite` and :func:`cbor_bytestring_is_indefinite` respectively.
+
+In case a byte string is indefinite, it is encoded as a series of definite byte strings. These are called "chunks". For example, the encoded item
+
+::
+
+ 0xf5 Start indefinite byte string
+ 0x41 Byte string (1B long)
+ 0x00
+ 0x41 Byte string (1B long)
+ 0xff
+ 0xff "Break" control token
+
+represents two bytes, ``0x00`` and ``0xff``. This on one hand enables streaming messages even before they are fully generated, but on the other hand it adds more complexity to the client code.
+
+
+================================== ======================================================
+Corresponding :type:`cbor_type` ``CBOR_TYPE_BYTESTRING``
+Number of allocations (definite) One plus any manipulations with the data
+Number of allocations (indefinite) One plus logarithmically many
+ reallocations relative to chunk count
+Storage requirements (definite) ``sizeof(cbor_item_t) + length(handle)``
+Storage requirements (indefinite) ``sizeof(cbor_item_t) * (1 + chunk_count) + chunks``
+================================== ======================================================
+
+
+Streaming indefinite byte strings
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Please refer to :doc:`/streaming`.
+
+Getting metadata
+~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_bytestring_length
+.. doxygenfunction:: cbor_bytestring_is_definite
+.. doxygenfunction:: cbor_bytestring_is_indefinite
+.. doxygenfunction:: cbor_bytestring_chunk_count
+
+Reading data
+~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_bytestring_handle
+.. doxygenfunction:: cbor_bytestring_chunks_handle
+
+Creating new items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_new_definite_bytestring
+.. doxygenfunction:: cbor_new_indefinite_bytestring
+
+
+Building items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.. doxygenfunction:: cbor_build_bytestring
+
+
+Manipulating existing items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_bytestring_set_handle
+.. doxygenfunction:: cbor_bytestring_add_chunk
+
diff --git a/doc/source/api/type_3.rst b/doc/source/api/type_3.rst
new file mode 100644
index 000000000000..abec24de40a3
--- /dev/null
+++ b/doc/source/api/type_3.rst
@@ -0,0 +1,55 @@
+Type 3 – UTF-8 strings
+=============================
+
+CBOR strings work in much the same ways as :doc:`type_2`.
+
+================================== ======================================================
+Corresponding :type:`cbor_type` ``CBOR_TYPE_STRING``
+Number of allocations (definite) One plus any manipulations with the data
+Number of allocations (indefinite) One plus logarithmically many
+ reallocations relative to chunk count
+Storage requirements (definite) ``sizeof(cbor_item_t) + length(handle)``
+Storage requirements (indefinite) ``sizeof(cbor_item_t) * (1 + chunk_count) + chunks``
+================================== ======================================================
+
+Streaming indefinite strings
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Please refer to :doc:`/streaming`.
+
+UTF-8 encoding validation
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+*libcbor* considers UTF-8 encoding validity to be a part of the well-formedness notion of CBOR and therefore invalid UTF-8 strings will be rejected by the parser. Strings created by the user are not checked.
+
+
+Getting metadata
+~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_string_length
+.. doxygenfunction:: cbor_string_is_definite
+.. doxygenfunction:: cbor_string_is_indefinite
+.. doxygenfunction:: cbor_string_chunk_count
+
+Reading data
+~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_string_handle
+.. doxygenfunction:: cbor_string_chunks_handle
+
+Creating new items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_new_definite_string
+.. doxygenfunction:: cbor_new_indefinite_string
+
+
+Building items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+.. doxygenfunction:: cbor_build_string
+
+
+Manipulating existing items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_string_set_handle
+.. doxygenfunction:: cbor_string_add_chunk
diff --git a/doc/source/api/type_4.rst b/doc/source/api/type_4.rst
new file mode 100644
index 000000000000..c3bb88a9cd8a
--- /dev/null
+++ b/doc/source/api/type_4.rst
@@ -0,0 +1,62 @@
+Type 4 – Arrays
+=============================
+
+CBOR arrays, just like :doc:`byte strings <type_2>` and :doc:`strings <type_3>`, can be encoded either as definite, or as indefinite.
+
+================================== =====================================================================================
+Corresponding :type:`cbor_type` ``CBOR_TYPE_ARRAY``
+Number of allocations (definite) Two plus any manipulations with the data
+Number of allocations (indefinite) Two plus logarithmically many
+ reallocations relative to additions
+Storage requirements (definite) ``(sizeof(cbor_item_t) + 1) * size``
+Storage requirements (indefinite) ``<= sizeof(cbor_item_t) + sizeof(cbor_item_t) * size * BUFFER_GROWTH``
+================================== =====================================================================================
+
+
+Examples
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+::
+
+ 0x9f Start indefinite array
+ 0x01 Unsigned integer 1
+ 0xff "Break" control token
+
+::
+
+ 0x9f Start array, 1B length follows
+ 0x20 Unsigned integer 32
+ ... 32 items follow
+
+Streaming indefinite arrays
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Please refer to :doc:`/streaming`.
+
+Getting metadata
+~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_array_size
+.. doxygenfunction:: cbor_array_allocated
+.. doxygenfunction:: cbor_array_is_definite
+.. doxygenfunction:: cbor_array_is_indefinite
+
+Reading data
+~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_array_handle
+.. doxygenfunction:: cbor_array_get
+
+Creating new items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_new_definite_array
+.. doxygenfunction:: cbor_new_indefinite_array
+
+
+Modifying items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_array_push
+.. doxygenfunction:: cbor_array_replace
+.. doxygenfunction:: cbor_array_set
diff --git a/doc/source/api/type_5.rst b/doc/source/api/type_5.rst
new file mode 100644
index 000000000000..c9b2e904af6e
--- /dev/null
+++ b/doc/source/api/type_5.rst
@@ -0,0 +1,44 @@
+Type 5 – Maps
+=============================
+
+CBOR maps are the plain old associate hash maps known from JSON and many other formats and languages, with one exception: any CBOR data item can be a key, not just strings. This is somewhat unusual and you, as an application developer, should keep that in mind.
+
+Maps can be either definite or indefinite, in much the same way as :doc:`type_4`.
+
+================================== =====================================================================================
+Corresponding :type:`cbor_type` ``CBOR_TYPE_MAP``
+Number of allocations (definite) Two plus any manipulations with the data
+Number of allocations (indefinite) Two plus logarithmically many
+ reallocations relative to additions
+Storage requirements (definite) ``sizeof(cbor_pair) * size + sizeof(cbor_item_t)``
+Storage requirements (indefinite) ``<= sizeof(cbor_item_t) + sizeof(cbor_pair) * size * BUFFER_GROWTH``
+================================== =====================================================================================
+
+Streaming maps
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Please refer to :doc:`/streaming`.
+
+Getting metadata
+~~~~~~~~~~~~~~~~~
+.. doxygenfunction:: cbor_map_size
+.. doxygenfunction:: cbor_map_allocated
+.. doxygenfunction:: cbor_map_is_definite
+.. doxygenfunction:: cbor_map_is_indefinite
+
+Reading data
+~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_map_handle
+
+Creating new items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_new_definite_map
+.. doxygenfunction:: cbor_new_indefinite_map
+
+
+Modifying items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_map_add
diff --git a/doc/source/api/type_6.rst b/doc/source/api/type_6.rst
new file mode 100644
index 000000000000..e98457ceae2a
--- /dev/null
+++ b/doc/source/api/type_6.rst
@@ -0,0 +1,21 @@
+Type 6 – Semantic tags
+=============================
+
+Tag are additional metadata that can be used to extend or specialize the meaning or interpretation of the other data items.
+
+For example, one might tag an array of numbers to communicate that it should be interpreted as a vector.
+
+Please consult the official `IANA repository of CBOR tags <https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>`_ before inventing new ones.
+
+================================== ======================================================
+Corresponding :type:`cbor_type` ``CBOR_TYPE_TAG``
+Number of allocations One plus any manipulations with the data
+ reallocations relative to chunk count
+Storage requirements ``sizeof(cbor_item_t) + the tagged item``
+================================== ======================================================
+
+.. doxygenfunction:: cbor_new_tag
+.. doxygenfunction:: cbor_tag_item
+.. doxygenfunction:: cbor_tag_value
+.. doxygenfunction:: cbor_tag_set_item
+
diff --git a/doc/source/api/type_7.rst b/doc/source/api/type_7.rst
new file mode 100644
index 000000000000..d2e75c8bf0b3
--- /dev/null
+++ b/doc/source/api/type_7.rst
@@ -0,0 +1,75 @@
+Type 7 – Floats & control tokens
+=================================
+
+This type combines two completely unrelated types of items -- floating point numbers and special values such as true, false, null, etc. We refer to these special values as 'control values' or 'ctrls' for short throughout the code.
+
+Just like integers, they have different possible width (resulting in different value ranges and precisions).
+
+.. doxygenenum:: cbor_float_width
+
+================================== =========================================
+Corresponding :type:`cbor_type` ``CBOR_TYPE_FLOAT_CTRL``
+Number of allocations One per lifetime
+Storage requirements ``sizeof(cbor_item_t) + 1/4/8``
+================================== =========================================
+
+Getting metadata
+~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_float_ctrl_is_ctrl
+.. doxygenfunction:: cbor_float_get_width
+
+Reading data
+~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_float_get_float2
+.. doxygenfunction:: cbor_float_get_float4
+.. doxygenfunction:: cbor_float_get_float8
+.. doxygenfunction:: cbor_float_get_float
+.. doxygenfunction:: cbor_ctrl_value
+.. doxygenfunction:: cbor_get_bool
+
+Creating new items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_new_ctrl
+.. doxygenfunction:: cbor_new_float2
+.. doxygenfunction:: cbor_new_float4
+.. doxygenfunction:: cbor_new_float8
+.. doxygenfunction:: cbor_new_null
+.. doxygenfunction:: cbor_new_undef
+
+
+Building items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_build_bool
+.. doxygenfunction:: cbor_build_ctrl
+.. doxygenfunction:: cbor_build_float2
+.. doxygenfunction:: cbor_build_float4
+.. doxygenfunction:: cbor_build_float8
+
+
+Manipulating existing items
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenfunction:: cbor_set_ctrl
+.. doxygenfunction:: cbor_set_bool
+.. doxygenfunction:: cbor_set_float2
+.. doxygenfunction:: cbor_set_float4
+.. doxygenfunction:: cbor_set_float8
+
+
+.. _api_type_7_hard_floats:
+
+Half floats
+~~~~~~~~~~~~
+CBOR supports two `bytes wide ("half-precision") <https://en.wikipedia.org/wiki/Half-precision_floating-point_format>`_
+floats which are not supported by the C language. *libcbor* represents them using `float <https://en.cppreference.com/w/c/language/type>` values throughout the API, which has important implications when manipulating these values.
+
+In particular, if a user uses some of the manipulation APIs
+(e.g. :func:`cbor_set_float2`, :func:`cbor_new_float2`)
+to introduce a value that doesn't have an exect half-float representation,
+the encoding semantics are given by :func:`cbor_encode_half` as follows:
+
+.. doxygenfunction:: cbor_encode_half
diff --git a/doc/source/changelog.rst b/doc/source/changelog.rst
new file mode 100644
index 000000000000..89e7008afab2
--- /dev/null
+++ b/doc/source/changelog.rst
@@ -0,0 +1,4 @@
+Changelog
+==============
+
+.. include:: ../../CHANGELOG.md
diff --git a/doc/source/conf.py b/doc/source/conf.py
new file mode 100644
index 000000000000..3ca95bd6dd1f
--- /dev/null
+++ b/doc/source/conf.py
@@ -0,0 +1,296 @@
+# -*- coding: utf-8 -*-
+#
+# libcbor documentation build configuration file, created by
+# sphinx-quickstart on Sun Jun 8 13:27:19 2014.
+#
+# This file is execfile()d with the current directory set to its
+# containing dir.
+#
+# Note that not all possible configuration values are present in this
+# autogenerated file.
+#
+# All configuration values have a default; values that are commented out
+# serve to show the default.
+
+import sys
+import os
+
+# If extensions (or modules to document with autodoc) are in another directory,
+# add these directories to sys.path here. If the directory is relative to the
+# documentation root, use os.path.abspath to make it absolute, like shown here.
+#sys.path.insert(0, os.path.abspath('.'))
+sys.path.append(os.path.abspath(os.path.dirname(__file__)))
+
+# -- General configuration ------------------------------------------------
+
+# If your documentation needs a minimal Sphinx version, state it here.
+#needs_sphinx = '1.0'
+
+# Add any Sphinx extension module names here, as strings. They can be
+# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
+# ones.
+extensions = [
+ 'breathe',
+ 'sphinx.ext.mathjax',
+ 'sphinx.ext.autodoc',
+ 'sphinx.ext.ifconfig'
+]
+
+import subprocess, os
+
+# on_rtd is whether we are on readthedocs.org, this line of code grabbed from docs.readthedocs.org
+on_rtd = os.environ.get('READTHEDOCS', None) == 'True'
+print(subprocess.check_output('cd ../..; mkdir doc/build; doxygen', shell=True))
+
+if on_rtd:
+ print(subprocess.check_output('cd ../..; mkdir doc/build; doxygen', shell=True))
+
+
+print(os.getcwd())
+print(os.getcwd() + '/../build/doxygen/xml')
+
+breathe_projects = {
+ 'libcbor': '../build/doxygen/xml'
+}
+
+breathe_default_project = "libcbor"
+
+# Add any paths that contain templates here, relative to this directory.
+templates_path = ['_templates']
+
+# The suffix of source filenames.
+source_suffix = '.rst'
+
+# The encoding of source files.
+#source_encoding = 'utf-8-sig'
+
+# The master toctree document.
+master_doc = 'index'
+
+# General information about the project.
+project = 'libcbor'
+copyright = '2014 - 2020, Pavel Kalvoda'
+
+# The version info for the project you're documenting, acts as replacement for
+# |version| and |release|, also used in various other places throughout the
+# built documents.
+#
+# The short X.Y version.
+version = '0.8'
+release = '0.8.0'
+
+# The language for content autogenerated by Sphinx. Refer to documentation
+# for a list of supported languages.
+#language = None
+
+# There are two options for replacing |today|: either, you set today to some
+# non-false value, then it is used:
+#today = ''
+# Else, today_fmt is used as the format for a strftime call.
+#today_fmt = '%B %d, %Y'
+
+# List of patterns, relative to source directory, that match files and
+# directories to ignore when looking for source files.
+# https://github.com/michaeljones/breathe/issues/197
+exclude_patterns = ['breathe/*']
+
+# The reST default role (used for this markup: `text`) to use for all
+# documents.
+breathe_domain_by_extension = {
+ "h" : "C",
+ }
+#default_role = 'c:func'
+primary_domain = "cpp"
+
+# If true, '()' will be appended to :func: etc. cross-reference text.
+add_function_parentheses = True
+
+# If true, the current module name will be prepended to all description
+# unit titles (such as .. function::).
+#add_module_names = True
+
+# If true, sectionauthor and moduleauthor directives will be shown in the
+# output. They are ignored by default.
+#show_authors = False
+
+# The name of the Pygments (syntax highlighting) style to use.
+pygments_style = 'sphinx'
+
+# A list of ignored prefixes for module index sorting.
+#modindex_common_prefix = []
+
+# If true, keep warnings as "system message" paragraphs in the built documents.
+#keep_warnings = False
+
+
+# -- Options for HTML output ----------------------------------------------
+
+# The theme to use for HTML and HTML Help pages. See the documentation for
+# a list of builtin themes.
+html_theme = 'default'
+
+# Theme options are theme-specific and customize the look and feel of a theme
+# further. For a list of options available for each theme, see the
+# documentation.
+#html_theme_options = {}
+
+# Add any paths that contain custom themes here, relative to this directory.
+#html_theme_path = []
+
+# The name for this set of Sphinx documents. If None, it defaults to
+# "<project> v<release> documentation".
+#html_title = None
+
+# A shorter title for the navigation bar. Default is the same as html_title.
+#html_short_title = None
+
+# The name of an image file (relative to this directory) to place at the top
+# of the sidebar.
+#html_logo = None
+
+# The name of an image file (within the static path) to use as favicon of the
+# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
+# pixels large.
+#html_favicon = None
+
+# Add any paths that contain custom static files (such as style sheets) here,
+# relative to this directory. They are copied after the builtin static files,
+# so a file named "default.css" will overwrite the builtin "default.css".
+html_static_path = []
+
+# Add any extra paths that contain custom files (such as robots.txt or
+# .htaccess) here, relative to this directory. These files are copied
+# directly to the root of the documentation.
+#html_extra_path = []
+
+# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
+# using the given strftime format.
+#html_last_updated_fmt = '%b %d, %Y'
+
+# If true, SmartyPants will be used to convert quotes and dashes to
+# typographically correct entities.
+#html_use_smartypants = True
+
+# Custom sidebar templates, maps document names to template names.
+#html_sidebars = {}
+
+# Additional templates that should be rendered to pages, maps page names to
+# template names.
+#html_additional_pages = {}
+
+# If false, no module index is generated.
+#html_domain_indices = True
+
+# If false, no index is generated.
+#html_use_index = True
+
+# If true, the index is split into individual pages for each letter.
+#html_split_index = False
+
+# If true, links to the reST sources are added to the pages.
+#html_show_sourcelink = True
+
+# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
+#html_show_sphinx = True
+
+# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
+#html_show_copyright = True
+
+# If true, an OpenSearch description file will be output, and all pages will
+# contain a <link> tag referring to it. The value of this option must be the
+# base URL from which the finished HTML is served.
+#html_use_opensearch = ''
+
+# This is the file name suffix for HTML files (e.g. ".xhtml").
+#html_file_suffix = None
+
+# Output file base name for HTML help builder.
+htmlhelp_basename = 'libcbordoc'
+
+
+# -- Options for LaTeX output ---------------------------------------------
+
+latex_elements = {
+# The paper size ('letterpaper' or 'a4paper').
+#'papersize': 'letterpaper',
+
+# The font size ('10pt', '11pt' or '12pt').
+#'pointsize': '10pt',
+
+# Additional stuff for the LaTeX preamble.
+#'preamble': '',
+}
+
+# Grouping the document tree into LaTeX files. List of tuples
+# (source start file, target name, title,
+# author, documentclass [howto, manual, or own class]).
+latex_documents = [
+ ('index', 'libcbor.tex', 'libcbor Documentation',
+ 'Pavel Kalvoda', 'manual'),
+]
+
+# The name of an image file (relative to this directory) to place at the top of
+# the title page.
+#latex_logo = None
+
+# For "manual" documents, if this is true, then toplevel headings are parts,
+# not chapters.
+#latex_use_parts = False
+
+# If true, show page references after internal links.
+#latex_show_pagerefs = False
+
+# If true, show URL addresses after external links.
+#latex_show_urls = False
+
+# Documents to append as an appendix to all manuals.
+#latex_appendices = []
+
+# If false, no module index is generated.
+#latex_domain_indices = True
+
+
+# -- Options for manual page output ---------------------------------------
+
+# One entry per manual page. List of tuples
+# (source start file, name, description, authors, manual section).
+man_pages = [
+ ('index', 'libcbor', 'libcbor Documentation',
+ ['Pavel Kalvoda'], 3)
+]
+
+# If true, show URL addresses after external links.
+#man_show_urls = False
+
+
+# -- Options for Texinfo output -------------------------------------------
+
+# Grouping the document tree into Texinfo files. List of tuples
+# (source start file, target name, title, author,
+# dir menu entry, description, category)
+texinfo_documents = [
+ ('index', 'libcbor', 'libcbor Documentation',
+ 'Pavel Kalvoda', 'libcbor', 'C library for parsing and generating CBOR.',
+ 'Miscellaneous'),
+]
+
+# Documents to append as an appendix to all manuals.
+#texinfo_appendices = []
+
+# If false, no module index is generated.
+#texinfo_domain_indices = True
+
+# How to display URL addresses: 'footnote', 'no', or 'inline'.
+#texinfo_show_urls = 'footnote'
+
+# If true, do not generate a @detailmenu in the "Top" node's menu.
+#texinfo_no_detailmenu = False
+
+
+if not on_rtd: # only import and set the theme if we're building docs locally
+ import sphinx_rtd_theme
+ html_theme = 'sphinx_rtd_theme'
+ html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
+
+# otherwise, readthedocs.org uses their theme by default, so no need to specify it
+
diff --git a/doc/source/development.rst b/doc/source/development.rst
new file mode 100644
index 000000000000..13e91ef25762
--- /dev/null
+++ b/doc/source/development.rst
@@ -0,0 +1,143 @@
+Development
+==========================
+
+Vision and principles
+---------------------------
+
+Consistency and coherence are one of the key characteristics of good software.
+While the reality is never black and white, it is important libcbor
+contributors are working towards the same high-level goal. This document
+attempts to set out the basic principles of libcbor and the rationale behind
+them. If you are contributing to libcbor or looking to evaluate whether libcbor
+is the right choice for your project, it might be worthwhile to skim through the
+section below.
+
+Mission statement
+~~~~~~~~~~~~~~~~~~~~~~
+
+*libcbor* is the compact, full-featured, and safe CBOR library that works
+everywhere.
+
+
+Goals
+~~~~~~~~~~~~~~~~~~~~~~
+
+RFC-conformance and full feature support
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Anything the standard allows, libcbor can do.
+
+**Why?** Because conformance and interoperability is the point of defining
+standards. Clients expect the support to be feature-complete and
+there is no significant complexity reduction that can be achieved by slightly
+cutting corners, which means that the incremental cost of full RFC support is
+comparatively small over "almost-conformance" seen in many alternatives.
+
+
+Safety
+^^^^^^^^^^^^^^^^^^^^^^
+
+Untrusted bytes from the network are the typical input.
+
+**Why?** Because it is the client expectation. Vast majority of security
+vulnerabilities are violations of contracts -- in other words, bugs -- anyway.
+
+
+Self-containment
+^^^^^^^^^^^^^^^^^^^^^^
+
+libcbor has no runtime dependencies.
+
+**Why?** Because any constraint imposed on libcbor has to be enforced
+transitively, which is difficult and leads to incompatibilities and
+distribution issues, especially in IoT applications.
+
+Portability
+^^^^^^^^^^^^^^^^^^^^^^
+
+If you can compile C for it, libcbor will work there.
+
+**Why?** Lowest-common-denominator solution for system-level and IoT software
+was the original niche of libcbor. Users who rely on libcbor expect future
+updates to work on their target platform.
+
+Stable and predictable API
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+libcbor will not break without a warning.
+
+**Why?** `Industry-standard <https://semver.org/>`_ versioning is a basic
+requirement for production-quality software. This is especially relevant in IoT
+environments where updates may be costly.
+
+Performance
+^^^^^^^^^^^^^^^^^^^^^^
+
+libcbor is fast and resource-efficient by design
+
+
+**Why?** Because the main maintainer is an avid hater of slow bloated software.
+Who wouldn't want more bang per their electricity buck?
+
+
+Non-goals
+~~~~~~~~~~~~~~~~~~~~~~
+
+ - Convenience -- libcbor only provides the minimum surface to make it usable
+ - FFI/SWIG/interop support -- libcbor is primarily a C library for C clients
+ - One-off usecases support -- although there are primitives to reuse, the
+ basic
+ assumption is that most clients want most of CBOR features
+
+
+Development dependencies
+---------------------------
+- `CMocka <http://cmocka.org/>`_ (testing)
+- `Python <https://www.python.org/>`_ and `pip <https://pypi.python.org/pypi/pip>`_ (Sphinx platform)
+- `Doxygen <http://www.stack.nl/~dimitri/doxygen/>`_
+- `Sphinx <http://sphinx-doc.org/>`_ (documentation)
+- There are some `Ruby <https://www.ruby-lang.org/en/>`_ scripts in ``misc``
+- `Valgrind <http://valgrind.org/>`_ (memory correctness & profiling)
+- `GCOV/LCOV <http://ltp.sourceforge.net/coverage/lcov.php>`_ (test coverage)
+- `clang-format`
+
+
+Installing *sphinx*
+~~~~~~~~~~~~~~~~~~~~~~
+
+.. code-block:: bash
+
+ pip install sphinx
+ pip install sphinx_rtd_theme
+ pip install breathe
+ pip install https://github.com/lepture/python-livereload/archive/master.zip
+ pip install sphinx-autobuild
+
+Further instructions on configuring advanced features can be found at `<http://read-the-docs.readthedocs.org/en/latest/install.html>`_.
+
+
+Live preview of docs
+~~~~~~~~~~~~~~~~~~~~~~
+
+.. code-block:: bash
+
+ cd doc
+ make livehtml
+
+
+Set up git hooks
+~~~~~~~~~~~~~~~~~
+
+A catch-all git hook that runs clang-format and automatically refreshes the `GH
+pages <https://pages.github.com/>`_ contents located in ``docs`` can be
+symlinked:
+
+.. code-block:: bash
+
+ ln -sf $(pwd)/misc/hooks/pre-commit .git/hooks
+
+
+Testing and code coverage
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Please refer to :doc:`tests`
diff --git a/doc/source/getting_started.rst b/doc/source/getting_started.rst
new file mode 100644
index 000000000000..3e56f2c84c08
--- /dev/null
+++ b/doc/source/getting_started.rst
@@ -0,0 +1,190 @@
+Getting started
+==========================
+
+Pre-built Linux packages are available in most mainstream distributions
+
+**Ubuntu, Debian, etc.**:
+
+.. code-block:: bash
+
+ apt-get install libcbor-dev
+
+**Fedora, openSUSE, etc.**:
+
+.. code-block:: bash
+
+ yum install libcbor-devel
+
+
+**OS X** users can use `Homebrew <http://brew.sh/>`_:
+
+.. code-block:: bash
+
+ brew install libcbor
+
+For other platforms, you will need to compile it from source.
+
+Building & installing libcbor
+------------------------------
+
+Prerequisites:
+ - C99 compiler
+ - CMake_ 2.8 or newer (might also be called ``cmakesetup``, ``cmake-gui`` or ``ccmake`` depending on the installed version and system)
+ - C build system CMake can target (make, Apple Xcode, MinGW, ...)
+
+.. _CMake: http://cmake.org/
+
+**Configuration options**
+
+A handful of configuration flags can be passed to `cmake`. The following table lists libcbor compile-time directives and several important generic flags.
+
+======================== ======================================================= ====================== =====================================================================================================================
+Option Meaning Default Possible values
+------------------------ ------------------------------------------------------- ---------------------- ---------------------------------------------------------------------------------------------------------------------
+``CMAKE_C_COMPILER`` C compiler to use ``cc`` ``gcc``, ``clang``, ``clang-3.5``, ...
+``CMAKE_INSTALL_PREFIX`` Installation prefix System-dependent ``/usr/local/lib``, ...
+``BUILD_SHARED_LIBS`` Build as a shared library ``OFF`` ``ON``, ``OFF``
+``HUGE_FUZZ`` :doc:`Fuzz test </tests>` with 8GB of data ``OFF`` ``ON``, ``OFF``
+``SANE_MALLOC`` Assume ``malloc`` will refuse unreasonable allocations ``OFF`` ``ON``, ``OFF``
+``COVERAGE`` Generate test coverage instrumentation ``OFF`` ``ON``, ``OFF``
+``WITH_TESTS`` Build unit tests (see :doc:`development`) ``OFF`` ``ON``, ``OFF``
+======================== ======================================================= ====================== =====================================================================================================================
+
+The following configuration options will also be defined as macros[#]_ in ``<cbor/common.h>`` and can therefore be used in client code:
+
+======================== ======================================================= ====================== =====================================================================================================================
+Option Meaning Default Possible values
+------------------------ ------------------------------------------------------- ---------------------- ---------------------------------------------------------------------------------------------------------------------
+``CBOR_CUSTOM_ALLOC`` Enable custom allocator support ``OFF`` ``ON``, ``OFF``
+``CBOR_PRETTY_PRINTER`` Include a pretty-printing routine ``ON`` ``ON``, ``OFF``
+``CBOR_BUFFER_GROWTH`` Factor for buffer growth & shrinking ``2`` Decimals > 1
+======================== ======================================================= ====================== =====================================================================================================================
+
+.. [#] ``ON`` & ``OFF`` will be translated to ``1`` and ``0`` using `cmakedefine <https://cmake.org/cmake/help/v3.2/command/configure_file.html?highlight=cmakedefine>`_.
+
+If you want to pass other custom configuration options, please refer to `<http://www.cmake.org/Wiki/CMake_Useful_Variables>`_.
+
+**Building using make**
+
+CMake will generate a Makefile and other configuration files for the build. As a rule of thumb, you should configure the
+build *outside of the source tree* in order to keep different configurations isolated. If you are unsure where to
+execute the build, just use a temporary directory:
+
+.. code-block:: bash
+
+ cd $(mktemp -d /tmp/cbor_build.XXXX)
+
+Now, assuming you are in the directory where you want to build, build libcbor as a **static library**:
+
+.. code-block:: bash
+
+ cmake -DCMAKE_BUILD_TYPE=Release path_to_libcbor_dir
+ make cbor
+
+... or as a **dynamic library**:
+
+.. code-block:: bash
+
+ cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON path_to_libcbor_dir
+ make cbor
+
+To install locally:
+
+.. code-block:: bash
+
+ make install
+
+Root permissions are required on most systems when using the default installation prefix.
+
+
+**Portability**
+
+libcbor is highly portable and works on both little- and big-endian systems regardless of the operating system. After building
+on an exotic platform, you might wish to verify the result by running the :doc:`test suite </tests>`. If you encounter any problems, please
+report them to the `issue tracker <https://github.com/PJK/libcbor/issues>`_.
+
+libcbor is known to successfully work on ARM Android devices. Cross-compilation is possible with ``arm-linux-gnueabi-gcc``.
+
+
+Linking with libcbor
+---------------------
+
+If you include and linker paths include the directories to which libcbor has been installed, compiling programs that uses libcbor requires
+no extra considerations.
+
+You can verify that everything has been set up properly by creating a file with the following contents
+
+.. code-block:: c
+
+ #include <cbor.h>
+ #include <stdio.h>
+
+ int main(int argc, char * argv[])
+ {
+ printf("Hello from libcbor %s\n", CBOR_VERSION);
+ }
+
+
+and compiling it
+
+.. code-block:: bash
+
+ cc hello_cbor.c -lcbor -o hello_cbor
+
+
+libcbor also comes with `pkg-config <https://wiki.freedesktop.org/www/Software/pkg-config/>`_ support. If you install libcbor with a custom prefix, you can use pkg-config to resolve the headers and objects:
+
+.. code-block:: bash
+
+ cc $(pkg-config --cflags libcbor) hello_cbor.c $(pkg-config --libs libcbor) -o hello_cbor
+
+
+**A note on linkage**
+
+libcbor is primarily intended to be linked statically. The shared library versioning scheme generally follows `SemVer <https://semver.org/>`_, but is irregular for the 0.X.Y development branch for historical reasons. The following version identifiers are used as a part of the SONAME (Linux) or the dylib `"Compatibility version" <https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html>`_ (OS X):
+
+ - 0.Y for the 0.Y.Z branch. Patches are backwards compatible, minor releases are generally not and require re-compilation of any dependent code.
+ - X for the X.Y.Z stable versions starting 1.X.Y. All minor release of the major version are backwards compatible.
+
+.. warning:: Please note that releases up to and including v0.6.0 `may export misleading .so/.dylib version number <https://github.com/PJK/libcbor/issues/52>`_.
+
+
+Troubleshooting
+---------------------
+
+**cbor.h not found**: The headers directory is probably not in your include path. First, verify the installation
+location by checking the installation log. If you used make, it will look something like
+
+.. code-block:: text
+
+ ...
+ -- Installing: /usr/local/include/cbor
+ -- Installing: /usr/local/include/cbor/callbacks.h
+ -- Installing: /usr/local/include/cbor/encoding.h
+ ...
+
+Make sure that ``CMAKE_INSTALL_PREFIX`` (if you provided it) was correct. Including the path path during compilation should suffice, e.g.:
+
+.. code-block:: bash
+
+ cc -I/usr/local/include hello_cbor.c -lcbor -o hello_cbor
+
+
+**cannot find -lcbor during linking**: Most likely the same problem as before. Include the installation directory in the
+linker shared path using ``-R``, e.g.:
+
+.. code-block:: bash
+
+ cc -Wl,-rpath,/usr/local/lib -lcbor -o hello_cbor
+
+**shared library missing during execution**: Verify the linkage using ``ldd``, ``otool``, or similar and adjust the compilation directives accordingly:
+
+.. code-block:: text
+
+ ⇒ ldd hello_cbor
+ linux-vdso.so.1 => (0x00007ffe85585000)
+ libcbor.so => /usr/local/lib/libcbor.so (0x00007f9af69da000)
+ libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9af65eb000)
+ /lib64/ld-linux-x86-64.so.2 (0x00007f9af6be9000)
+
+**compilation failed**: If your compiler supports C99 yet the compilation has failed, please report the issue to the `issue tracker <https://github.com/PJK/libcbor/issues>`_.
diff --git a/doc/source/img/win_1.png b/doc/source/img/win_1.png
new file mode 100644
index 000000000000..1bc34e5f8de5
--- /dev/null
+++ b/doc/source/img/win_1.png
Binary files differ
diff --git a/doc/source/img/win_2.png b/doc/source/img/win_2.png
new file mode 100644
index 000000000000..f6af59949df6
--- /dev/null
+++ b/doc/source/img/win_2.png
Binary files differ
diff --git a/doc/source/img/win_3.png b/doc/source/img/win_3.png
new file mode 100644
index 000000000000..68ba112cbae0
--- /dev/null
+++ b/doc/source/img/win_3.png
Binary files differ
diff --git a/doc/source/img/win_4.png b/doc/source/img/win_4.png
new file mode 100644
index 000000000000..b274be7d530b
--- /dev/null
+++ b/doc/source/img/win_4.png
Binary files differ
diff --git a/doc/source/img/win_5.png b/doc/source/img/win_5.png
new file mode 100644
index 000000000000..dffeb882f744
--- /dev/null
+++ b/doc/source/img/win_5.png
Binary files differ
diff --git a/doc/source/img/win_6.png b/doc/source/img/win_6.png
new file mode 100644
index 000000000000..850e3e2f6cc8
--- /dev/null
+++ b/doc/source/img/win_6.png
Binary files differ
diff --git a/doc/source/index.rst b/doc/source/index.rst
new file mode 100644
index 000000000000..1922586097e3
--- /dev/null
+++ b/doc/source/index.rst
@@ -0,0 +1,40 @@
+libcbor
+===================================
+
+Documentation for version |release|, updated on |today|.
+
+Overview
+--------
+*libcbor* is a C library for parsing and generating CBOR_, the general-purpose schema-less binary data format.
+
+
+Main features
+ - Complete RFC conformance [#]_
+ - Robust C99 implementation
+ - Layered architecture offers both control and convenience
+ - Flexible memory management
+ - No shared global state - threading friendly [#]_
+ - Proper handling of UTF-8
+ - Full support for streams & incremental processing
+ - Extensive documentation and test suite
+ - No runtime dependencies, small footprint
+
+.. [#] See :doc:`rfc_conformance`
+
+.. [#] With the exception of custom memory allocators (see :doc:`api/item_reference_counting`)
+
+Contents
+----------
+.. toctree::
+
+ getting_started
+ using
+ api
+ streaming
+ tests
+ rfc_conformance
+ internal
+ changelog
+ development
+
+.. _CBOR: http://tools.ietf.org/html/rfc7049
diff --git a/doc/source/internal.rst b/doc/source/internal.rst
new file mode 100644
index 000000000000..2c62a82b2329
--- /dev/null
+++ b/doc/source/internal.rst
@@ -0,0 +1,128 @@
+Internal mechanics
+==========================
+
+Internal workings of *libcbor* are mostly derived from the specification. The purpose of this document is to describe technical choices made during design & implementation and to explicate the reasoning behind those choices.
+
+Terminology
+---------------
+=== ====================== ========================================================================================================================================
+MTB Major Type Byte http://tools.ietf.org/html/rfc7049#section-2.1
+--- ---------------------- ----------------------------------------------------------------------------------------------------------------------------------------
+DST Dynamically Sized Type Type whose storage requirements cannot be determined
+
+ during compilation (originated in the `Rust <http://www.rust-lang.org/>`_ community)
+=== ====================== ========================================================================================================================================
+
+Conventions
+--------------
+API symbols start with ``cbor_`` or ``CBOR_`` prefix, internal symbols have ``_cbor_`` or ``_CBOR_`` prefix.
+
+Inspiration & related projects
+-------------------------------
+Most of the API is largely modelled after existing JSON libraries, including
+
+ - `Jansson <http://www.digip.org/jansson/>`_
+ - `json-c <https://github.com/json-c/json-c>`_
+ - Gnome's `JsonGlib <https://wiki.gnome.org/action/show/Projects/JsonGlib?action=show&redirect=JsonGlib>`_
+
+and also borrowing from
+
+ - `msgpack-c <https://github.com/msgpack/msgpack-c>`_
+ - `Google Protocol Buffers <http://code.google.com/p/protobuf/>`_.
+
+General notes on the API design
+--------------------------------
+The API design has two main driving priciples:
+
+ 1. Let the client manage the memory as much as possible
+ 2. Behave exactly as specified by the standard
+
+Combining these two principles in practice turns out to be quite difficult. Indefinite-length strings, arrays, and maps require client to handle every fixed-size chunk explicitly in order to
+
+ - ensure the client never runs out of memory due to *libcbor*
+ - use :func:`realloc` sparsely and predictably [#]_
+
+ - provide strong guarantees about its usage (to prevent latency spikes)
+ - provide APIs to avoid :func:`realloc` altogether
+ - allow proper handling of (streamed) data bigger than available memory
+
+ .. [#] Reasonable handling of DSTs requires reallocation if the API is to remain sane.
+
+
+Coding style
+-------------
+This code loosely follows the `Linux kernel coding style <https://www.kernel.org/doc/Documentation/CodingStyle>`_. Tabs are tabs, and they are 4 characters wide.
+
+
+Memory layout
+---------------
+CBOR is very dynamic in the sense that it contains many data elements of variable length, sometimes even indefinite length. This section describes internal representation of all CBOR data types.
+
+Generally speaking, data items consist of three parts:
+
+ - a generic :type:`handle <cbor_item_t>`,
+ - the associated :type:`metadata <cbor_item_metadata>`,
+ - and the actual data
+
+.. type:: cbor_item_t
+
+ Represents the item. Used as an opaque type
+
+ .. member:: cbor_type type
+
+ Type discriminator
+
+ .. member:: size_t refcount
+
+ Reference counter. Used by :func:`cbor_decref`, :func:`cbor_incref`
+
+ .. member:: union cbor_item_metadata metadata
+
+ Union discriminated by :member:`type`. Contains type-specific metadata
+
+ .. member:: unsigned char * data
+
+ Contains pointer to the actual data. Small, fixed size items (:doc:`api/type_0_1`, :doc:`api/type_6`, :doc:`api/type_7`) are allocated as a single memory block.
+
+ Consider the following snippet
+
+ .. code-block:: c
+
+ cbor_item_t * item = cbor_new_int8();
+
+ then the memory is laid out as follows
+
+ ::
+
+ +-----------+---------------+---------------+-----------------------------------++-----------+
+ | | | | || |
+ | type | refcount | metadata | data || uint8_t |
+ | | | | (= item + sizeof(cbor_item_t)) || |
+ +-----------+---------------+---------------+-----------------------------------++-----------+
+ ^ ^
+ | |
+ +--- item +--- item->data
+
+ Dynamically sized types (:doc:`api/type_2`, :doc:`api/type_3`, :doc:`api/type_4`, :doc:`api/type_5`) may store handle and data in separate locations. This enables creating large items (e.g :doc:`byte strings <api/type_2>`) without :func:`realloc` or copying large blocks of memory. One simply attaches the correct pointer to the handle.
+
+
+.. type:: cbor_item_metadata
+
+ Union type of the following members, based on the item type:
+
+ .. member:: struct _cbor_int_metadata int_metadata
+
+ Used both by both :doc:`api/type_0_1`
+
+ .. member:: struct _cbor_bytestring_metadata bytestring_metadata
+ .. member:: struct _cbor_string_metadata string_metadata
+ .. member:: struct _cbor_array_metadata array_metadata
+ .. member:: struct _cbor_map_metadata map_metadata
+ .. member:: struct _cbor_tag_metadata tag_metadata
+ .. member:: struct _cbor_float_ctrl_metadata float_ctrl_metadata
+
+Decoding
+---------
+
+As outlined in :doc:`api`, there decoding is based on the streaming decoder Essentially, the decoder is a custom set of callbacks for the streaming decoder.
+
diff --git a/doc/source/requirements.txt b/doc/source/requirements.txt
new file mode 100644
index 000000000000..461bb7e804d2
--- /dev/null
+++ b/doc/source/requirements.txt
@@ -0,0 +1,39 @@
+alabaster==0.7.12
+argh==0.26.2
+Babel==2.8.0
+breathe==4.14.1
+certifi==2019.11.28
+chardet==3.0.4
+Click==7.0
+docutils==0.16
+Flask==1.1.1
+idna==2.9
+imagesize==1.2.0
+itsdangerous==1.1.0
+Jinja2==2.10.1
+livereload==2.6.1
+MarkupSafe==1.1.1
+packaging==20.3
+pathtools==0.1.2
+port-for==0.3.1
+Pygments==2.6.1
+pyparsing==2.4.6
+pytz==2019.3
+PyYAML==5.3
+requests==2.23.0
+six==1.14.0
+snowballstemmer==2.0.0
+sortedcontainers==2.1.0
+Sphinx==2.4.4
+sphinx-autobuild==0.7.1
+sphinx-rtd-theme==0.4.3
+sphinxcontrib-applehelp==1.0.2
+sphinxcontrib-devhelp==1.0.2
+sphinxcontrib-htmlhelp==1.0.3
+sphinxcontrib-jsmath==1.0.1
+sphinxcontrib-qthelp==1.0.3
+sphinxcontrib-serializinghtml==1.1.4
+tornado==6.0.4
+urllib3==1.25.8
+watchdog==0.10.2
+Werkzeug==0.15.5
diff --git a/doc/source/rfc_conformance.rst b/doc/source/rfc_conformance.rst
new file mode 100644
index 000000000000..817614835a1f
--- /dev/null
+++ b/doc/source/rfc_conformance.rst
@@ -0,0 +1,17 @@
+RFC conformance
+=========================
+
+*libcbor* is, generally speaking, very faithful implementation of `RFC 7049 <http://tools.ietf.org/html/rfc7049>`_. There are, however, some limitations imposed by technical constraints.
+
+Bytestring length
+-------------------
+There is no explicit limitation of indefinite length byte strings. [#]_ *libcbor* will not handle byte strings with more chunks than the maximum value of :type:`size_t`. On any sane platform, such string would not fit in the memory anyway. It is, however, possible to process arbitrarily long strings and byte strings using the streaming decoder.
+
+.. [#] http://tools.ietf.org/html/rfc7049#section-2.2.2
+
+"Half-precision" IEEE 754 floats
+---------------------------------
+As of C99 and even C11, there is no standard implementation for 2 bytes floats. *libcbor* packs them as a `float <https://en.cppreference.com/w/c/language/type>`. When encoding, *libcbor* selects the appropriate wire representation based on metadata and the actual value. This applies both to canonical and normal mode.
+
+For more information on half-float serialization, please refer to the section on :ref:`api_type_7_hard_floats`.
+
diff --git a/doc/source/streaming.rst b/doc/source/streaming.rst
new file mode 100644
index 000000000000..8e3bb0d9bade
--- /dev/null
+++ b/doc/source/streaming.rst
@@ -0,0 +1,13 @@
+Streaming & indefinite items
+=============================
+
+CBOR :doc:`strings <api/type_2>`, :doc:`byte strings <api/type_3>`, :doc:`arrays <api/type_4>`, and :doc:`maps <api/type_5>` can be encoded as *indefinite*, meaning their length or size is not specified. Instead, they are divided into *chunks* (:doc:`strings <api/type_2>`, :doc:`byte strings <api/type_3>`), or explicitly terminated (:doc:`arrays <api/type_4>`, :doc:`maps <api/type_5>`).
+
+This is one of the most important (and due to poor implementations, underutilized) features of CBOR. It enables low-overhead streaming just about anywhere without dealing with channels or pub/sub mechanism. It is, however, important to recognize that CBOR streaming is not a substitute for Websockets [#]_ and similar technologies.
+
+.. [#] :RFC:`6455`
+
+.. toctree::
+
+ streaming/decoding
+ streaming/encoding
diff --git a/doc/source/streaming/decoding.rst b/doc/source/streaming/decoding.rst
new file mode 100644
index 000000000000..70174aa2715d
--- /dev/null
+++ b/doc/source/streaming/decoding.rst
@@ -0,0 +1,40 @@
+Decoding
+=============================
+
+Another way to decode data using libcbor is to specify a callbacks that will be invoked when upon finding certain items in the input. This API is provided by
+
+.. doxygenfunction:: cbor_stream_decode
+
+Usage example: https://github.com/PJK/libcbor/blob/master/examples/streaming_parser.c
+
+The callbacks are defined by
+
+.. doxygenstruct:: cbor_callbacks
+ :members:
+
+When building custom sets of callbacks, feel free to start from
+
+.. doxygenvariable:: cbor_empty_callbacks
+
+Related structures
+~~~~~~~~~~~~~~~~~~~~~
+
+.. doxygenenum:: cbor_decoder_status
+.. doxygenstruct:: cbor_decoder_result
+ :members:
+
+
+Callback types definition
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+
+.. doxygentypedef:: cbor_int8_callback
+.. doxygentypedef:: cbor_int16_callback
+.. doxygentypedef:: cbor_int32_callback
+.. doxygentypedef:: cbor_int64_callback
+.. doxygentypedef:: cbor_simple_callback
+.. doxygentypedef:: cbor_string_callback
+.. doxygentypedef:: cbor_collection_callback
+.. doxygentypedef:: cbor_float_callback
+.. doxygentypedef:: cbor_double_callback
+.. doxygentypedef:: cbor_bool_callback
diff --git a/doc/source/streaming/encoding.rst b/doc/source/streaming/encoding.rst
new file mode 100644
index 000000000000..8a56461d3584
--- /dev/null
+++ b/doc/source/streaming/encoding.rst
@@ -0,0 +1,4 @@
+Encoding
+=============================
+
+TODO
diff --git a/doc/source/tests.rst b/doc/source/tests.rst
new file mode 100644
index 000000000000..47357ba36cd6
--- /dev/null
+++ b/doc/source/tests.rst
@@ -0,0 +1,39 @@
+Tests
+=============
+
+Unit tests
+--------------
+
+There is a comprehensive test suite employing CMocka_. You can run all of them using ``ctest`` in the build directory. Individual tests are themselves runnable. Please refer to `CTest <http://www.cmake.org/Wiki/CMake/Testing_With_CTest>`_ documentation for detailed information on how to specify particular subset of tests.
+
+.. _CMocka: http://cmocka.org/
+
+Testing for memory leaks
+------------------------
+
+Every release is tested for memory correctness. You can run these tests by passing the ``-T memcheck`` flag to ``ctest``. [#]_
+
+.. [#] Project should be configured with ``-DCMAKE_BUILD_TYPE=Debug`` to obtain meaningful description of location of the leak. You might also need ``--dsymutil=yes`` on OS X.
+
+
+Code coverage
+-------------------
+
+Every release is inspected using `GCOV/LCOV <http://ltp.sourceforge.net/coverage/lcov.php>`_. Platform-independent code should be fully covered by the test suite. Simply run
+
+.. code-block:: bash
+
+ make coverage
+
+
+or alternatively run ``lcov`` by hand using
+
+.. code-block:: bash
+
+ lcov --capture --directory . --output-file coverage.info
+ genhtml coverage.info --output-directory out
+
+Fuzz testing
+-----------------
+
+Every release is tested using a fuzz test. In this test, a huge buffer filled with random data is passed to the decoder. We require that it either succeeds or fail with a sensible error, without leaking any memory. This is intended to simulate real-world situations where data received from the network are CBOR-decoded before any further processing.
diff --git a/doc/source/using.rst b/doc/source/using.rst
new file mode 100644
index 000000000000..dbc09819d46b
--- /dev/null
+++ b/doc/source/using.rst
@@ -0,0 +1,173 @@
+Usage & preliminaries
+=======================
+
+Version information
+--------------------
+
+libcbor exports its version using three self-explanatory macros:
+
+ - ``CBOR_MAJOR_VERSION``
+ - ``CBOR_MINOR_VERSION``
+ - ``CBOR_PATCH_VERSION``
+
+The ``CBOR_VERSION`` is a string concatenating these three identifiers into one (e.g. ``0.2.0``).
+
+In order to simplify version comparisons, the version is also exported as
+
+.. code-block:: c
+
+ #define CBOR_HEX_VERSION ((CBOR_MAJOR_VERSION << 16) | (CBOR_MINOR_VERSION << 8) | CBOR_PATCH_VERSION)
+
+Since macros are difficult to work with through FFIs, the same information is also available through three ``uint8_t`` constants,
+namely
+
+ - ``cbor_major_version``
+ - ``cbor_minor_version``
+ - ``cbor_patch_version``
+
+
+Headers to include
+---------------------
+
+The ``cbor.h`` header includes all the symbols. If, for any reason, you don't want to include all the exported symbols,
+feel free to use just some of the ``cbor/*.h`` headers:
+
+ - ``cbor/arrays.h`` - :doc:`api/type_4`
+ - ``cbor/bytestrings.h`` - :doc:`api/type_2`
+ - ``cbor/callbacks.h`` - Callbacks used for :doc:`streaming/decoding`
+ - ``cbor/common.h`` - Common utilities - always transitively included
+ - ``cbor/data.h`` - Data types definitions - always transitively included
+ - ``cbor/encoding.h`` - Streaming encoders for :doc:`streaming/encoding`
+ - ``cbor/floats_ctrls.h`` - :doc:`api/type_7`
+ - ``cbor/ints.h`` - :doc:`api/type_0_1`
+ - ``cbor/maps.h`` - :doc:`api/type_5`
+ - ``cbor/serialization.h`` - High level serialization such as :func:`cbor_serialize`
+ - ``cbor/streaming.h`` - Home of :func:`cbor_stream_decode`
+ - ``cbor/strings.h`` - :doc:`api/type_3`
+ - ``cbor/tags.h`` - :doc:`api/type_6`
+
+
+Using libcbor
+--------------
+
+If you want to get more familiar with CBOR, we recommend the `cbor.io <http://cbor.io/>`_ website. Once you get the grasp
+of what is it CBOR does, the examples (located in the ``examples`` directory) should give you a good feel of the API. The
+:doc:`API documentation <api>` should then provide with all the information you may need.
+
+
+**Creating and serializing items**
+
+.. code-block:: c
+
+ #include "cbor.h"
+ #include <stdio.h>
+
+ int main(int argc, char * argv[])
+ {
+ /* Preallocate the map structure */
+ cbor_item_t * root = cbor_new_definite_map(2);
+ /* Add the content */
+ cbor_map_add(root, (struct cbor_pair) {
+ .key = cbor_move(cbor_build_string("Is CBOR awesome?")),
+ .value = cbor_move(cbor_build_bool(true))
+ });
+ cbor_map_add(root, (struct cbor_pair) {
+ .key = cbor_move(cbor_build_uint8(42)),
+ .value = cbor_move(cbor_build_string("Is the answer"))
+ });
+ /* Output: `length` bytes of data in the `buffer` */
+ unsigned char * buffer;
+ size_t buffer_size, length = cbor_serialize_alloc(root, &buffer, &buffer_size);
+
+ fwrite(buffer, 1, length, stdout);
+ free(buffer);
+
+ fflush(stdout);
+ cbor_decref(&root);
+ }
+
+
+**Reading serialized data**
+
+.. code-block:: c
+
+ #include "cbor.h"
+ #include <stdio.h>
+
+ /*
+ * Reads data from a file. Example usage:
+ * $ ./examples/readfile examples/data/nested_array.cbor
+ */
+
+ int main(int argc, char * argv[])
+ {
+ FILE * f = fopen(argv[1], "rb");
+ fseek(f, 0, SEEK_END);
+ size_t length = (size_t)ftell(f);
+ fseek(f, 0, SEEK_SET);
+ unsigned char * buffer = malloc(length);
+ fread(buffer, length, 1, f);
+
+ /* Assuming `buffer` contains `info.st_size` bytes of input data */
+ struct cbor_load_result result;
+ cbor_item_t * item = cbor_load(buffer, length, &result);
+ /* Pretty-print the result */
+ cbor_describe(item, stdout);
+ fflush(stdout);
+ /* Deallocate the result */
+ cbor_decref(&item);
+
+ fclose(f);
+ }
+
+
+**Using the streaming parser**
+
+.. code-block:: c
+
+ #include "cbor.h"
+ #include <stdio.h>
+ #include <string.h>
+
+ /*
+ * Illustrates how one might skim through a map (which is assumed to have
+ * string keys and values only), looking for the value of a specific key
+ *
+ * Use the examples/data/map.cbor input to test this.
+ */
+
+ const char * key = "a secret key";
+ bool key_found = false;
+
+ void find_string(void * _ctx, cbor_data buffer, size_t len)
+ {
+ if (key_found) {
+ printf("Found the value: %*s\n", (int) len, buffer);
+ key_found = false;
+ } else if (len == strlen(key)) {
+ key_found = (memcmp(key, buffer, len) == 0);
+ }
+ }
+
+ int main(int argc, char * argv[])
+ {
+ FILE * f = fopen(argv[1], "rb");
+ fseek(f, 0, SEEK_END);
+ size_t length = (size_t)ftell(f);
+ fseek(f, 0, SEEK_SET);
+ unsigned char * buffer = malloc(length);
+ fread(buffer, length, 1, f);
+
+ struct cbor_callbacks callbacks = cbor_empty_callbacks;
+ struct cbor_decoder_result decode_result;
+ size_t bytes_read = 0;
+ callbacks.string = find_string;
+ while (bytes_read < length) {
+ decode_result = cbor_stream_decode(buffer + bytes_read,
+ length - bytes_read,
+ &callbacks, NULL);
+ bytes_read += decode_result.read;
+ }
+
+ fclose(f);
+ }
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
new file mode 100644
index 000000000000..d1ddfb17b28a
--- /dev/null
+++ b/examples/CMakeLists.txt
@@ -0,0 +1,25 @@
+add_executable(readfile readfile.c)
+target_link_libraries(readfile cbor)
+
+add_executable(create_items create_items.c)
+target_link_libraries(create_items cbor)
+
+add_executable(streaming_parser streaming_parser.c)
+target_link_libraries(streaming_parser cbor)
+
+add_executable(sort sort.c)
+target_link_libraries(sort cbor)
+
+add_executable(hello hello.c)
+target_link_libraries(hello cbor)
+
+find_package(CJSON)
+
+if(CJSON_FOUND)
+ add_executable(cjson2cbor cjson2cbor.c)
+ target_include_directories(cjson2cbor PUBLIC ${CJSON_INCLUDE_DIRS})
+ target_link_libraries(cjson2cbor cbor ${CJSON_LIBRARY})
+endif()
+
+file(COPY data DESTINATION .)
+
diff --git a/examples/cjson2cbor.c b/examples/cjson2cbor.c
new file mode 100644
index 000000000000..2e3071fb47c5
--- /dev/null
+++ b/examples/cjson2cbor.c
@@ -0,0 +1,145 @@
+/*
+ * Contributed by Jacob Teplitsky <jacob.teplitsky@ericsson.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+/**
+ * This code demonstrates how cJSON (https://github.com/DaveGamble/cJSON)
+ * callbacks can be used in conjuction with the streaming parser to translate
+ * JSON to CBOR. Please note that cbor_builder_* APIs are internal and thus
+ * subject to change.
+ *
+ * The example will only be compiled when cJSON is available
+ */
+
+#include <cjson/cJSON.h>
+#include <float.h>
+#include <math.h>
+#include <string.h>
+#include "cbor.h"
+#include "cbor/internal/builder_callbacks.h"
+#include "cbor/internal/loaders.h"
+
+typedef void (*cbor_load_callback_t)(cJSON *, const struct cbor_callbacks *,
+ void *);
+
+cbor_item_t *cjson_cbor_load(void *source,
+ cbor_load_callback_t cbor_load_callback) {
+ static struct cbor_callbacks callbacks = {
+ .uint64 = &cbor_builder_uint64_callback,
+ .negint64 = &cbor_builder_negint64_callback,
+ .string = &cbor_builder_string_callback,
+ .array_start = &cbor_builder_array_start_callback,
+ .map_start = &cbor_builder_map_start_callback,
+ .null = &cbor_builder_null_callback,
+ .boolean = &cbor_builder_boolean_callback,
+ .float4 = &cbor_builder_float4_callback,
+ };
+
+ /* Context stack */
+ struct _cbor_stack stack = _cbor_stack_init();
+
+ /* Target for callbacks */
+ struct _cbor_decoder_context context = (struct _cbor_decoder_context){
+ .stack = &stack,
+ };
+
+ cbor_load_callback(source, &callbacks, &context);
+
+ return context.root;
+}
+
+void cjson_cbor_stream_decode(cJSON *source,
+ const struct cbor_callbacks *callbacks,
+ void *context) {
+ switch (source->type) {
+ case cJSON_False: {
+ callbacks->boolean(context, false);
+ return;
+ }
+ case cJSON_True: {
+ callbacks->boolean(context, true);
+ return;
+ }
+ case cJSON_NULL: {
+ callbacks->null(context);
+ return;
+ }
+ case cJSON_Number: {
+ // This is stupid -- ints and doubles cannot are not distinguished
+ if (fabs(source->valuedouble - source->valueint) > DBL_EPSILON) {
+ callbacks->float4(context, source->valuedouble);
+ } else {
+ // XXX: This is not portable
+ if (source->valueint >= 0) {
+ callbacks->uint64(context, source->valueint);
+ } else {
+ callbacks->negint64(context, source->valueint + 1);
+ }
+ }
+ return;
+ }
+ case cJSON_String: {
+ // XXX: Assume cJSON handled unicode correctly
+ callbacks->string(context, (unsigned char *)source->valuestring,
+ strlen(source->valuestring));
+ return;
+ }
+ case cJSON_Array: {
+ callbacks->array_start(context, cJSON_GetArraySize(source));
+ cJSON *item = source->child;
+ while (item != NULL) {
+ cjson_cbor_stream_decode(item, callbacks, context);
+ item = item->next;
+ }
+ return;
+ }
+ case cJSON_Object: {
+ callbacks->map_start(context, cJSON_GetArraySize(source));
+ cJSON *item = source->child;
+ while (item != NULL) {
+ callbacks->string(context, (unsigned char *)item->string,
+ strlen(item->string));
+ cjson_cbor_stream_decode(item, callbacks, context);
+ item = item->next;
+ }
+ return;
+ }
+ }
+}
+
+void usage() {
+ printf("Usage: cjson [input JSON file]\n");
+ exit(1);
+}
+
+int main(int argc, char *argv[]) {
+ if (argc != 2) usage();
+ FILE *f = fopen(argv[1], "rb");
+ if (f == NULL) usage();
+ /* Read input file into a buffer (cJSON doesn't work with streams) */
+ fseek(f, 0, SEEK_END);
+ size_t length = (size_t)ftell(f);
+ fseek(f, 0, SEEK_SET);
+ char *json_buffer = malloc(length + 1);
+ fread(json_buffer, length, 1, f);
+ json_buffer[length] = '\0';
+
+ /* Convert between JSON and CBOR */
+ cJSON *json = cJSON_Parse(json_buffer);
+ cbor_item_t *cbor = cjson_cbor_load(json, cjson_cbor_stream_decode);
+
+ /* Print out CBOR bytes */
+ unsigned char *buffer;
+ size_t buffer_size,
+ cbor_length = cbor_serialize_alloc(cbor, &buffer, &buffer_size);
+
+ fwrite(buffer, 1, cbor_length, stdout);
+
+ free(buffer);
+ fflush(stdout);
+ cJSON_Delete(json);
+ cbor_decref(&cbor);
+}
diff --git a/examples/create_items.c b/examples/create_items.c
new file mode 100644
index 000000000000..b875720b5d91
--- /dev/null
+++ b/examples/create_items.c
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <stdio.h>
+#include "cbor.h"
+
+int main(int argc, char* argv[]) {
+ /* Preallocate the map structure */
+ cbor_item_t* root = cbor_new_definite_map(2);
+ /* Add the content */
+ cbor_map_add(root,
+ (struct cbor_pair){
+ .key = cbor_move(cbor_build_string("Is CBOR awesome?")),
+ .value = cbor_move(cbor_build_bool(true))});
+ cbor_map_add(root,
+ (struct cbor_pair){
+ .key = cbor_move(cbor_build_uint8(42)),
+ .value = cbor_move(cbor_build_string("Is the answer"))});
+ /* Output: `length` bytes of data in the `buffer` */
+ unsigned char* buffer;
+ size_t buffer_size,
+ length = cbor_serialize_alloc(root, &buffer, &buffer_size);
+
+ fwrite(buffer, 1, length, stdout);
+ free(buffer);
+
+ fflush(stdout);
+ cbor_decref(&root);
+}
diff --git a/examples/data/floats.cbor b/examples/data/floats.cbor
new file mode 100644
index 000000000000..afb5e4a92f4a
--- /dev/null
+++ b/examples/data/floats.cbor
Binary files differ
diff --git a/examples/data/indef_string.cbor b/examples/data/indef_string.cbor
new file mode 100644
index 000000000000..ef51440cfe21
--- /dev/null
+++ b/examples/data/indef_string.cbor
@@ -0,0 +1 @@
+eHellofworld! \ No newline at end of file
diff --git a/examples/data/integer.cbor b/examples/data/integer.cbor
new file mode 100644
index 000000000000..d9ba7315ac8a
--- /dev/null
+++ b/examples/data/integer.cbor
@@ -0,0 +1 @@
+* \ No newline at end of file
diff --git a/examples/data/json_example.json b/examples/data/json_example.json
new file mode 100644
index 000000000000..9646aad098d1
--- /dev/null
+++ b/examples/data/json_example.json
@@ -0,0 +1,6 @@
+{
+ "a": [1, 2.2, -1, "žluťoučký kůň!!!"],
+ "b": {
+ "c": [true, false, null]
+ }
+}
diff --git a/examples/data/map.cbor b/examples/data/map.cbor
new file mode 100644
index 000000000000..699abb42b598
--- /dev/null
+++ b/examples/data/map.cbor
Binary files differ
diff --git a/examples/data/nested_array.cbor b/examples/data/nested_array.cbor
new file mode 100644
index 000000000000..6c9f0d7acf59
--- /dev/null
+++ b/examples/data/nested_array.cbor
@@ -0,0 +1 @@
+ \ No newline at end of file
diff --git a/examples/data/tagged_date.cbor b/examples/data/tagged_date.cbor
new file mode 100644
index 000000000000..d2fbff8927a3
--- /dev/null
+++ b/examples/data/tagged_date.cbor
@@ -0,0 +1 @@
+t2013-03-21T20:04:00Z \ No newline at end of file
diff --git a/examples/hello.c b/examples/hello.c
new file mode 100644
index 000000000000..275267445dac
--- /dev/null
+++ b/examples/hello.c
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <stdio.h>
+#include "cbor.h"
+
+int main(int argc, char* argv[]) {
+ printf("Hello from libcbor %s\n", CBOR_VERSION);
+ printf("Custom allocation support: %s\n", CBOR_CUSTOM_ALLOC ? "yes" : "no");
+ printf("Pretty-printer support: %s\n", CBOR_PRETTY_PRINTER ? "yes" : "no");
+ printf("Buffer growth factor: %f\n", (float)CBOR_BUFFER_GROWTH);
+}
diff --git a/examples/readfile.c b/examples/readfile.c
new file mode 100644
index 000000000000..213ee77f0fdd
--- /dev/null
+++ b/examples/readfile.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <stdio.h>
+#include "cbor.h"
+
+void usage() {
+ printf("Usage: readfile [input file]\n");
+ exit(1);
+}
+
+/*
+ * Reads data from a file. Example usage:
+ * $ ./examples/readfile examples/data/nested_array.cbor
+ */
+
+int main(int argc, char* argv[]) {
+ if (argc != 2) usage();
+ FILE* f = fopen(argv[1], "rb");
+ if (f == NULL) usage();
+ fseek(f, 0, SEEK_END);
+ size_t length = (size_t)ftell(f);
+ fseek(f, 0, SEEK_SET);
+ unsigned char* buffer = malloc(length);
+ fread(buffer, length, 1, f);
+
+ /* Assuming `buffer` contains `length` bytes of input data */
+ struct cbor_load_result result;
+ cbor_item_t* item = cbor_load(buffer, length, &result);
+
+ if (result.error.code != CBOR_ERR_NONE) {
+ printf(
+ "There was an error while reading the input near byte %zu (read %zu "
+ "bytes in total): ",
+ result.error.position, result.read);
+ switch (result.error.code) {
+ case CBOR_ERR_MALFORMATED: {
+ printf("Malformed data\n");
+ break;
+ }
+ case CBOR_ERR_MEMERROR: {
+ printf("Memory error -- perhaps the input is too large?\n");
+ break;
+ }
+ case CBOR_ERR_NODATA: {
+ printf("The input is empty\n");
+ break;
+ }
+ case CBOR_ERR_NOTENOUGHDATA: {
+ printf("Data seem to be missing -- is the input complete?\n");
+ break;
+ }
+ case CBOR_ERR_SYNTAXERROR: {
+ printf(
+ "Syntactically malformed data -- see "
+ "http://tools.ietf.org/html/rfc7049\n");
+ break;
+ }
+ case CBOR_ERR_NONE: {
+ // GCC's cheap dataflow analysis gag
+ break;
+ }
+ }
+ exit(1);
+ }
+
+ /* Pretty-print the result */
+ cbor_describe(item, stdout);
+ fflush(stdout);
+ /* Deallocate the result */
+ cbor_decref(&item);
+
+ fclose(f);
+}
diff --git a/examples/sort.c b/examples/sort.c
new file mode 100644
index 000000000000..d5e3aaff9dbe
--- /dev/null
+++ b/examples/sort.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "cbor.h"
+
+/*
+ * Illustrates how to use the contiguous storage of nested items with
+ * standard library functions.
+ */
+
+int comparUint(const void *a, const void *b) {
+ uint8_t av = cbor_get_uint8(*(cbor_item_t **)a),
+ bv = cbor_get_uint8(*(cbor_item_t **)b);
+
+ if (av < bv)
+ return -1;
+ else if (av == bv)
+ return 0;
+ else
+ return 1;
+}
+
+int main(int argc, char *argv[]) {
+ cbor_item_t *array = cbor_new_definite_array(4);
+ cbor_array_push(array, cbor_move(cbor_build_uint8(4)));
+ cbor_array_push(array, cbor_move(cbor_build_uint8(3)));
+ cbor_array_push(array, cbor_move(cbor_build_uint8(1)));
+ cbor_array_push(array, cbor_move(cbor_build_uint8(2)));
+
+ qsort(cbor_array_handle(array), cbor_array_size(array), sizeof(cbor_item_t *),
+ comparUint);
+
+ cbor_describe(array, stdout);
+ fflush(stdout);
+}
diff --git a/examples/streaming_parser.c b/examples/streaming_parser.c
new file mode 100644
index 000000000000..fe3ab853d978
--- /dev/null
+++ b/examples/streaming_parser.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include "cbor.h"
+
+void usage() {
+ printf("Usage: streaming_parser [input file]\n");
+ exit(1);
+}
+
+/*
+ * Illustrates how one might skim through a map (which is assumed to have
+ * string keys and values only), looking for the value of a specific key
+ *
+ * Use the examples/data/map.cbor input to test this.
+ */
+
+const char* key = "a secret key";
+bool key_found = false;
+
+void find_string(void* _ctx, cbor_data buffer, size_t len) {
+ if (key_found) {
+ printf("Found the value: %.*s\n", (int)len, buffer);
+ key_found = false;
+ } else if (len == strlen(key)) {
+ key_found = (memcmp(key, buffer, len) == 0);
+ }
+}
+
+int main(int argc, char* argv[]) {
+ if (argc != 2) usage();
+ FILE* f = fopen(argv[1], "rb");
+ if (f == NULL) usage();
+ fseek(f, 0, SEEK_END);
+ size_t length = (size_t)ftell(f);
+ fseek(f, 0, SEEK_SET);
+ unsigned char* buffer = malloc(length);
+ fread(buffer, length, 1, f);
+
+ struct cbor_callbacks callbacks = cbor_empty_callbacks;
+ struct cbor_decoder_result decode_result;
+ size_t bytes_read = 0;
+ callbacks.string = find_string;
+ while (bytes_read < length) {
+ decode_result = cbor_stream_decode(buffer + bytes_read, length - bytes_read,
+ &callbacks, NULL);
+ bytes_read += decode_result.read;
+ }
+
+ free(buffer);
+ fclose(f);
+}
diff --git a/misc/bytes.rb b/misc/bytes.rb
new file mode 100755
index 000000000000..ed453a4fcd81
--- /dev/null
+++ b/misc/bytes.rb
@@ -0,0 +1,3 @@
+#!/usr/bin/env ruby
+
+puts $*[0][2..-1].split('').each_slice(2).map {|_| '0x%02X' % _.join.to_i(16) }.join(', ')
diff --git a/misc/file_to_bytes.rb b/misc/file_to_bytes.rb
new file mode 100755
index 000000000000..047c91054f5e
--- /dev/null
+++ b/misc/file_to_bytes.rb
@@ -0,0 +1,5 @@
+#!/usr/bin/env ruby
+
+lst = (ARGV.empty? ? STDIN.read : IO.binread(ARGV[0])).bytes.map {|_| '0x%02X' % _ }
+puts lst.size
+puts lst.join(', ')
diff --git a/misc/hooks/pre-commit b/misc/hooks/pre-commit
new file mode 100755
index 000000000000..f3ac9f483799
--- /dev/null
+++ b/misc/hooks/pre-commit
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+set -e
+
+# Run clang-format and add modified files
+MODIFIED_UNSTAGED=$(git -C . diff --name-only)
+MODIFIED_STAGED=$(git -C . diff --name-only --cached --diff-filter=d)
+
+./clang-format.sh
+
+git add ${MODIFIED_STAGED}
+
+if [[ ${MODIFIED_UNSTAGED} != $(git -C . diff --name-only) ]]; then
+ echo "WARNING: Non-staged files were reformatted. Please review and/or add" \
+ "them"
+fi
+
+
diff --git a/misc/repeat.rb b/misc/repeat.rb
new file mode 100755
index 000000000000..fa3da5dbdffb
--- /dev/null
+++ b/misc/repeat.rb
@@ -0,0 +1,3 @@
+#!/usr/bin/env ruby
+
+(Integer($*[0])..Integer($*[1])).each {|i| puts "case 0x%02X:" % i}
diff --git a/misc/seq.rb b/misc/seq.rb
new file mode 100755
index 000000000000..0902eb0d96f5
--- /dev/null
+++ b/misc/seq.rb
@@ -0,0 +1,3 @@
+#!/usr/bin/env ruby
+
+Integer($*[0]).times {|i| print "0x%02X, " % ((Integer($*[1]) + i) % 0xFF) }
diff --git a/misc/update_version.py b/misc/update_version.py
new file mode 100755
index 000000000000..eb330215d375
--- /dev/null
+++ b/misc/update_version.py
@@ -0,0 +1,38 @@
+import sys, re
+from datetime import date
+
+version = sys.argv[1]
+release_date = date.today().strftime('%Y-%m-%d')
+major, minor, patch = version.split('.')
+
+
+def replace(file_path, pattern, replacement):
+ updated = re.sub(pattern, replacement, open(file_path).read())
+ with open(file_path, 'w') as f:
+ f.write(updated)
+
+# Update changelog
+SEP = '---------------------'
+NEXT = f'Next\n{SEP}'
+changelog_header = f'{NEXT}\n\n{version} ({release_date})\n{SEP}'
+replace('CHANGELOG.md', NEXT, changelog_header)
+
+# Update Doxyfile
+DOXY_VERSION = 'PROJECT_NUMBER = '
+replace('Doxyfile', DOXY_VERSION + '.*', DOXY_VERSION + version)
+
+# Update CMakeLists.txt
+replace('CMakeLists.txt',
+ '''SET\\(CBOR_VERSION_MAJOR "0"\\)
+SET\\(CBOR_VERSION_MINOR "7"\\)
+SET\\(CBOR_VERSION_PATCH "0"\\)''',
+ f'''SET(CBOR_VERSION_MAJOR "{major}")
+SET(CBOR_VERSION_MINOR "{minor}")
+SET(CBOR_VERSION_PATCH "{patch}")''')
+
+# Update Sphinx
+replace('doc/source/conf.py',
+ """version = '.*'
+release = '.*'""",
+ f"""version = '{major}.{minor}'
+release = '{major}.{minor}.{patch}'""")
diff --git a/oss-fuzz/build.sh b/oss-fuzz/build.sh
new file mode 100755
index 000000000000..4b19b7203f8b
--- /dev/null
+++ b/oss-fuzz/build.sh
@@ -0,0 +1,28 @@
+#!/bin/bash -eu
+# Copyright 2019 Google Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+################################################################################
+
+mkdir build
+cd build
+# We disable libcbor's default sanitizers since we'll be configuring them ourselves via CFLAGS.
+cmake -D CMAKE_BUILD_TYPE=Debug -D CMAKE_INSTALL_PREFIX="$WORK" -D CBOR_CUSTOM_ALLOC=ON -D SANITIZE=OFF ..
+make "-j$(nproc)"
+make install
+
+$CXX $CXXFLAGS -std=c++11 "-I$WORK/include" \
+ ../oss-fuzz/cbor_load_fuzzer.cc -o "$OUT/cbor_load_fuzzer" \
+ $LIB_FUZZING_ENGINE src/libcbor.a
+
diff --git a/oss-fuzz/cbor_load_fuzzer.cc b/oss-fuzz/cbor_load_fuzzer.cc
new file mode 100644
index 000000000000..b32a082bb8a9
--- /dev/null
+++ b/oss-fuzz/cbor_load_fuzzer.cc
@@ -0,0 +1,37 @@
+#include <cstdint>
+#include <cstdio>
+
+#include "cbor.h"
+
+void *limited_malloc(size_t size) {
+ if (size > 1 << 24) {
+ return nullptr;
+ }
+ return malloc(size);
+}
+
+struct State {
+ FILE* fout;
+
+ State() : fout(fopen("/dev/null", "r")) {
+ cbor_set_allocs(limited_malloc, realloc, free);
+ }
+};
+
+static State kState;
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
+ cbor_load_result result;
+ cbor_item_t *item = cbor_load(Data, Size, &result);
+ if (result.error.code == CBOR_ERR_NONE) {
+ cbor_describe(item, kState.fout);
+ unsigned char *buffer;
+ size_t buffer_size;
+ cbor_serialize_alloc(item, &buffer, &buffer_size);
+ free(buffer);
+ cbor_item_t *copied = cbor_copy(item);
+ cbor_decref(&copied);
+ cbor_decref(&item);
+ }
+ return 0;
+}
diff --git a/release.sh b/release.sh
new file mode 100755
index 000000000000..2fd73d967c22
--- /dev/null
+++ b/release.sh
@@ -0,0 +1,75 @@
+#!/usr/bin/env bash
+
+# Guides my forgetful self through the release process.
+# Usage release.sh VERSION
+
+set -e
+
+function prompt() {
+ echo "$1 Confirm with 'Yes'"
+ read check
+ if [ "$check" != "Yes" ]; then
+ echo "Aborting..."
+ exit 1
+ fi
+}
+# http://stackoverflow.com/questions/59895/getting-the-source-directory-of-a-bash-script-from-within
+DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+OUTDIR=$(mktemp -d)
+TAG_NAME="v$1"
+
+cd $DIR
+python3 misc/update_version.py "$1"
+
+echo ">>>>> Checking changelog"
+grep -A 5 -F "$1" CHANGELOG.md || true
+prompt "Is the changelog correct and complete?"
+
+echo ">>>>> Checking Doxyfile"
+grep PROJECT_NUMBER Doxyfile
+prompt "Is the Doxyfile version correct?"
+
+echo ">>>>> Checking CMakeLists"
+grep -A 2 'SET(CBOR_VERSION_MAJOR' CMakeLists.txt
+prompt "Is the CMake version correct?"
+
+echo ">>>>> Checking docs"
+grep 'version =\|release =' doc/source/conf.py
+prompt "Are the versions correct?"
+
+set -x
+pushd doc
+make clean
+popd
+doxygen
+cd doc
+make html
+cd build
+
+cp -r html libcbor_docs_html
+tar -zcf libcbor_docs.tar.gz libcbor_docs_html
+
+cp -r doxygen/html libcbor_api_docs_html
+tar -zcf libcbor_api_docs.tar.gz libcbor_api_docs_html
+
+mv libcbor_docs.tar.gz libcbor_api_docs.tar.gz "$OUTDIR"
+
+pushd "$OUTDIR"
+cmake "$DIR" -DCMAKE_BUILD_TYPE=Release -DWITH_TESTS=ON
+make
+ctest
+popd
+
+prompt "Will proceed to tag the release with $TAG_NAME."
+git tag "$TAG_NAME"
+git push --tags
+
+set +x
+
+echo "Release ready in $OUTDIR"
+echo "Add the release to GitHub at https://github.com/PJK/libcbor/releases/new *now*"
+prompt "Have you added the release to https://github.com/PJK/libcbor/releases/tag/$TAG_NAME?"
+
+echo "Update the Hombrew formula (https://github.com/Homebrew/homebrew-core/blob/master/Formula/libcbor.rb) *now*"
+echo "HOWTO: https://github.com/Linuxbrew/brew/blob/master/docs/How-To-Open-a-Homebrew-Pull-Request.md"
+prompt "Have you updated the formula?"
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 000000000000..2a1492bf6ee3
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,45 @@
+set(SOURCES cbor.c cbor/streaming.c cbor/internal/encoders.c cbor/internal/builder_callbacks.c cbor/internal/loaders.c cbor/internal/memory_utils.c cbor/internal/stack.c cbor/internal/unicode.c cbor/encoding.c cbor/serialization.c cbor/arrays.c cbor/common.c cbor/floats_ctrls.c cbor/bytestrings.c cbor/callbacks.c cbor/strings.c cbor/maps.c cbor/tags.c cbor/ints.c)
+
+include(GNUInstallDirs)
+set(CMAKE_SKIP_BUILD_RPATH FALSE)
+
+if (CBOR_CUSTOM_ALLOC)
+ LIST(APPEND SOURCES allocators.c)
+endif(CBOR_CUSTOM_ALLOC)
+
+if (NOT DEFINED CMAKE_MACOSX_RPATH)
+ set(CMAKE_MACOSX_RPATH 0)
+endif()
+
+add_library(cbor ${SOURCES})
+target_include_directories(cbor PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
+
+include(GenerateExportHeader)
+generate_export_header(cbor EXPORT_FILE_NAME ${CMAKE_CURRENT_BINARY_DIR}/cbor/cbor_export.h)
+target_include_directories(cbor PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/cbor/cbor_export.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cbor)
+
+if (NOT ${CBOR_VERSION_MAJOR} EQUAL 0)
+ MESSAGE(FATAL_ERROR "Change the shared library version scheme to reflect https://github.com/PJK/libcbor/issues/52.")
+endif()
+
+set_target_properties(cbor PROPERTIES
+ VERSION ${CBOR_VERSION}
+ MACHO_COMPATIBILITY_VERSION ${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR}.0
+ SOVERSION ${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR})
+
+configure_file(libcbor.pc.in libcbor.pc @ONLY)
+
+# http://www.cmake.org/Wiki/CMake:Install_Commands
+install(TARGETS cbor
+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
+
+install(DIRECTORY cbor DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
+ FILES_MATCHING PATTERN "*.h")
+
+install(FILES cbor.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
+
+install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libcbor.pc"
+ DESTINATION "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/pkgconfig")
diff --git a/src/allocators.c b/src/allocators.c
new file mode 100644
index 000000000000..43c5440ddb2a
--- /dev/null
+++ b/src/allocators.c
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "cbor/common.h"
+
+CBOR_EXPORT _cbor_malloc_t _cbor_malloc = malloc;
+CBOR_EXPORT _cbor_realloc_t _cbor_realloc = realloc;
+CBOR_EXPORT _cbor_free_t _cbor_free = free;
+
+void cbor_set_allocs(_cbor_malloc_t custom_malloc,
+ _cbor_realloc_t custom_realloc, _cbor_free_t custom_free) {
+ _cbor_malloc = custom_malloc;
+ _cbor_realloc = custom_realloc;
+ _cbor_free = custom_free;
+}
diff --git a/src/cbor.c b/src/cbor.c
new file mode 100644
index 000000000000..76b892c6b709
--- /dev/null
+++ b/src/cbor.c
@@ -0,0 +1,345 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "cbor.h"
+#include "cbor/internal/builder_callbacks.h"
+#include "cbor/internal/loaders.h"
+
+cbor_item_t *cbor_load(cbor_data source, size_t source_size,
+ struct cbor_load_result *result) {
+ /* Context stack */
+ static struct cbor_callbacks callbacks = {
+ .uint8 = &cbor_builder_uint8_callback,
+ .uint16 = &cbor_builder_uint16_callback,
+ .uint32 = &cbor_builder_uint32_callback,
+ .uint64 = &cbor_builder_uint64_callback,
+
+ .negint8 = &cbor_builder_negint8_callback,
+ .negint16 = &cbor_builder_negint16_callback,
+ .negint32 = &cbor_builder_negint32_callback,
+ .negint64 = &cbor_builder_negint64_callback,
+
+ .byte_string = &cbor_builder_byte_string_callback,
+ .byte_string_start = &cbor_builder_byte_string_start_callback,
+
+ .string = &cbor_builder_string_callback,
+ .string_start = &cbor_builder_string_start_callback,
+
+ .array_start = &cbor_builder_array_start_callback,
+ .indef_array_start = &cbor_builder_indef_array_start_callback,
+
+ .map_start = &cbor_builder_map_start_callback,
+ .indef_map_start = &cbor_builder_indef_map_start_callback,
+
+ .tag = &cbor_builder_tag_callback,
+
+ .null = &cbor_builder_null_callback,
+ .undefined = &cbor_builder_undefined_callback,
+ .boolean = &cbor_builder_boolean_callback,
+ .float2 = &cbor_builder_float2_callback,
+ .float4 = &cbor_builder_float4_callback,
+ .float8 = &cbor_builder_float8_callback,
+ .indef_break = &cbor_builder_indef_break_callback};
+
+ if (source_size == 0) {
+ result->error.code = CBOR_ERR_NODATA;
+ return NULL;
+ }
+ struct _cbor_stack stack = _cbor_stack_init();
+
+ /* Target for callbacks */
+ struct _cbor_decoder_context context = (struct _cbor_decoder_context){
+ .stack = &stack, .creation_failed = false, .syntax_error = false};
+ struct cbor_decoder_result decode_result;
+ *result =
+ (struct cbor_load_result){.read = 0, .error = {.code = CBOR_ERR_NONE}};
+
+ do {
+ if (source_size > result->read) { /* Check for overflows */
+ decode_result =
+ cbor_stream_decode(source + result->read, source_size - result->read,
+ &callbacks, &context);
+ } else {
+ result->error = (struct cbor_error){.code = CBOR_ERR_NOTENOUGHDATA,
+ .position = result->read};
+ goto error;
+ }
+
+ switch (decode_result.status) {
+ case CBOR_DECODER_FINISHED:
+ /* Everything OK */
+ {
+ result->read += decode_result.read;
+ break;
+ }
+ case CBOR_DECODER_NEDATA:
+ /* Data length doesn't match MTB expectation */
+ {
+ result->error.code = CBOR_ERR_NOTENOUGHDATA;
+ goto error;
+ }
+ case CBOR_DECODER_ERROR:
+ /* Reserved/malformated item */
+ {
+ result->error.code = CBOR_ERR_MALFORMATED;
+ goto error;
+ }
+ }
+
+ if (context.creation_failed) {
+ /* Most likely unsuccessful allocation - our callback has failed */
+ result->error.code = CBOR_ERR_MEMERROR;
+ goto error;
+ } else if (context.syntax_error) {
+ result->error.code = CBOR_ERR_SYNTAXERROR;
+ goto error;
+ }
+ } while (stack.size > 0);
+
+ /* Move the result before free */
+ cbor_item_t *result_item = context.root;
+ return result_item;
+
+error:
+ result->error.position = result->read;
+ // debug_print("Failed with decoder error %d at %d\n", result->error.code,
+ // result->error.position); cbor_describe(stack.top->item, stdout);
+ /* Free the stack */
+ while (stack.size > 0) {
+ cbor_decref(&stack.top->item);
+ _cbor_stack_pop(&stack);
+ }
+ return NULL;
+}
+
+static cbor_item_t *_cbor_copy_int(cbor_item_t *item, bool negative) {
+ cbor_item_t *res;
+ switch (cbor_int_get_width(item)) {
+ case CBOR_INT_8:
+ res = cbor_build_uint8(cbor_get_uint8(item));
+ break;
+ case CBOR_INT_16:
+ res = cbor_build_uint16(cbor_get_uint16(item));
+ break;
+ case CBOR_INT_32:
+ res = cbor_build_uint32(cbor_get_uint32(item));
+ break;
+ case CBOR_INT_64:
+ res = cbor_build_uint64(cbor_get_uint64(item));
+ break;
+ default:
+ return NULL;
+ }
+
+ if (negative) cbor_mark_negint(res);
+
+ return res;
+}
+
+static cbor_item_t *_cbor_copy_float_ctrl(cbor_item_t *item) {
+ switch (cbor_float_get_width(item)) {
+ case CBOR_FLOAT_0:
+ return cbor_build_ctrl(cbor_ctrl_value(item));
+ case CBOR_FLOAT_16:
+ return cbor_build_float2(cbor_float_get_float2(item));
+ case CBOR_FLOAT_32:
+ return cbor_build_float4(cbor_float_get_float4(item));
+ case CBOR_FLOAT_64:
+ return cbor_build_float8(cbor_float_get_float8(item));
+ }
+
+ return NULL;
+}
+
+cbor_item_t *cbor_copy(cbor_item_t *item) {
+ switch (cbor_typeof(item)) {
+ case CBOR_TYPE_UINT:
+ return _cbor_copy_int(item, false);
+ case CBOR_TYPE_NEGINT:
+ return _cbor_copy_int(item, true);
+ case CBOR_TYPE_BYTESTRING:
+ if (cbor_bytestring_is_definite(item)) {
+ return cbor_build_bytestring(cbor_bytestring_handle(item),
+ cbor_bytestring_length(item));
+ } else {
+ cbor_item_t *res = cbor_new_indefinite_bytestring();
+ for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++)
+ cbor_bytestring_add_chunk(
+ res,
+ cbor_move(cbor_copy(cbor_bytestring_chunks_handle(item)[i])));
+ return res;
+ }
+ case CBOR_TYPE_STRING:
+ if (cbor_string_is_definite(item)) {
+ return cbor_build_stringn((const char *)cbor_string_handle(item),
+ cbor_string_length(item));
+ } else {
+ cbor_item_t *res = cbor_new_indefinite_string();
+ for (size_t i = 0; i < cbor_string_chunk_count(item); i++)
+ cbor_string_add_chunk(
+ res, cbor_move(cbor_copy(cbor_string_chunks_handle(item)[i])));
+ return res;
+ }
+ case CBOR_TYPE_ARRAY: {
+ cbor_item_t *res;
+ if (cbor_array_is_definite(item))
+ res = cbor_new_definite_array(cbor_array_size(item));
+ else
+ res = cbor_new_indefinite_array();
+
+ for (size_t i = 0; i < cbor_array_size(item); i++)
+ cbor_array_push(
+ res, cbor_move(cbor_copy(cbor_move(cbor_array_get(item, i)))));
+ return res;
+ }
+ case CBOR_TYPE_MAP: {
+ cbor_item_t *res;
+ if (cbor_map_is_definite(item))
+ res = cbor_new_definite_map(cbor_map_size(item));
+ else
+ res = cbor_new_indefinite_map();
+
+ struct cbor_pair *it = cbor_map_handle(item);
+ for (size_t i = 0; i < cbor_map_size(item); i++)
+ cbor_map_add(res, (struct cbor_pair){
+ .key = cbor_move(cbor_copy(it[i].key)),
+ .value = cbor_move(cbor_copy(it[i].value))});
+ return res;
+ }
+ case CBOR_TYPE_TAG:
+ return cbor_build_tag(
+ cbor_tag_value(item),
+ cbor_move(cbor_copy(cbor_move(cbor_tag_item(item)))));
+ case CBOR_TYPE_FLOAT_CTRL:
+ return _cbor_copy_float_ctrl(item);
+ }
+
+ return NULL;
+}
+
+#if CBOR_PRETTY_PRINTER
+
+#include <inttypes.h>
+#include <locale.h>
+#include <stdlib.h>
+#include <wchar.h>
+
+#define __STDC_FORMAT_MACROS
+
+static int _pow(int b, int ex) {
+ if (ex == 0) return 1;
+ int res = b;
+ while (--ex > 0) res *= b;
+ return res;
+}
+
+static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) {
+ setlocale(LC_ALL, "");
+ switch (cbor_typeof(item)) {
+ case CBOR_TYPE_UINT: {
+ fprintf(out, "%*s[CBOR_TYPE_UINT] ", indent, " ");
+ fprintf(out, "Width: %dB, ", _pow(2, cbor_int_get_width(item)));
+ fprintf(out, "Value: %" PRIu64 "\n", cbor_get_int(item));
+ break;
+ };
+ case CBOR_TYPE_NEGINT: {
+ fprintf(out, "%*s[CBOR_TYPE_NEGINT] ", indent, " ");
+ fprintf(out, "Width: %dB, ", _pow(2, cbor_int_get_width(item)));
+ fprintf(out, "Value: -%" PRIu64 " -1\n", cbor_get_int(item));
+ break;
+ };
+ case CBOR_TYPE_BYTESTRING: {
+ fprintf(out, "%*s[CBOR_TYPE_BYTESTRING] ", indent, " ");
+ if (cbor_bytestring_is_indefinite(item)) {
+ fprintf(out, "Indefinite, with %zu chunks:\n",
+ cbor_bytestring_chunk_count(item));
+ for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++)
+ _cbor_nested_describe(cbor_bytestring_chunks_handle(item)[i], out,
+ indent + 4);
+ } else {
+ fprintf(out, "Definite, length %zuB\n", cbor_bytestring_length(item));
+ }
+ break;
+ };
+ case CBOR_TYPE_STRING: {
+ fprintf(out, "%*s[CBOR_TYPE_STRING] ", indent, " ");
+ if (cbor_string_is_indefinite(item)) {
+ fprintf(out, "Indefinite, with %zu chunks:\n",
+ cbor_string_chunk_count(item));
+ for (size_t i = 0; i < cbor_string_chunk_count(item); i++)
+ _cbor_nested_describe(cbor_string_chunks_handle(item)[i], out,
+ indent + 4);
+ } else {
+ fprintf(out, "Definite, length %zuB, %zu codepoints\n",
+ cbor_string_length(item), cbor_string_codepoint_count(item));
+ /* Careful - this doesn't support multibyte characters! */
+ /* Printing those is out of the scope of this demo :) */
+ /* libICU is your friend */
+ fprintf(out, "%*s", indent + 4, " ");
+ /* XXX: no null at the end -> confused vprintf */
+ fwrite(cbor_string_handle(item), (int)cbor_string_length(item), 1, out);
+ fprintf(out, "\n");
+ }
+ break;
+ };
+ case CBOR_TYPE_ARRAY: {
+ fprintf(out, "%*s[CBOR_TYPE_ARRAY] ", indent, " ");
+ if (cbor_array_is_definite(item)) {
+ fprintf(out, "Definite, size: %zu\n", cbor_array_size(item));
+ } else {
+ fprintf(out, "Indefinite, size: %zu\n", cbor_array_size(item));
+ }
+
+ for (size_t i = 0; i < cbor_array_size(item); i++)
+ _cbor_nested_describe(cbor_array_handle(item)[i], out, indent + 4);
+ break;
+ };
+ case CBOR_TYPE_MAP: {
+ fprintf(out, "%*s[CBOR_TYPE_MAP] ", indent, " ");
+ if (cbor_map_is_definite(item)) {
+ fprintf(out, "Definite, size: %zu\n", cbor_map_size(item));
+ } else {
+ fprintf(out, "Indefinite, size: %zu\n", cbor_map_size(item));
+ }
+
+ for (size_t i = 0; i < cbor_map_size(item); i++) {
+ _cbor_nested_describe(cbor_map_handle(item)[i].key, out, indent + 4);
+ _cbor_nested_describe(cbor_map_handle(item)[i].value, out, indent + 4);
+ }
+ break;
+ };
+ case CBOR_TYPE_TAG: {
+ fprintf(out, "%*s[CBOR_TYPE_TAG] ", indent, " ");
+ fprintf(out, "Value: %" PRIu64 "\n", cbor_tag_value(item));
+ _cbor_nested_describe(cbor_move(cbor_tag_item(item)), out, indent + 4);
+ break;
+ };
+ case CBOR_TYPE_FLOAT_CTRL: {
+ fprintf(out, "%*s[CBOR_TYPE_FLOAT_CTRL] ", indent, " ");
+ if (cbor_float_ctrl_is_ctrl(item)) {
+ if (cbor_is_bool(item))
+ fprintf(out, "Bool: %s\n", cbor_get_bool(item) ? "true" : "false");
+ else if (cbor_is_undef(item))
+ fprintf(out, "Undefined\n");
+ else if (cbor_is_null(item))
+ fprintf(out, "Null\n");
+ else
+ fprintf(out, "Simple value %d\n", cbor_ctrl_value(item));
+ } else {
+ fprintf(out, "Width: %dB, ", _pow(2, cbor_float_get_width(item)));
+ fprintf(out, "value: %lf\n", cbor_float_get_float(item));
+ }
+ break;
+ };
+ }
+}
+
+void cbor_describe(cbor_item_t *item, FILE *out) {
+ _cbor_nested_describe(item, out, 0);
+}
+
+#endif
diff --git a/src/cbor.h b/src/cbor.h
new file mode 100644
index 000000000000..d490e6c5862e
--- /dev/null
+++ b/src/cbor.h
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_H_
+#define LIBCBOR_H_
+
+#include "cbor/common.h"
+#include "cbor/data.h"
+
+#include "cbor/arrays.h"
+#include "cbor/bytestrings.h"
+#include "cbor/floats_ctrls.h"
+#include "cbor/ints.h"
+#include "cbor/maps.h"
+#include "cbor/strings.h"
+#include "cbor/tags.h"
+
+#include "cbor/callbacks.h"
+#include "cbor/cbor_export.h"
+#include "cbor/encoding.h"
+#include "cbor/serialization.h"
+#include "cbor/streaming.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * ============================================================================
+ * High level decoding
+ * ============================================================================
+ */
+
+/** Loads data item from a buffer
+ *
+ * @param source The buffer
+ * @param source_size
+ * @param result[out] Result indicator. #CBOR_ERR_NONE on success
+ * @return **new** CBOR item or `NULL` on failure. In that case, \p result
+ * contains location and description of the error.
+ */
+CBOR_EXPORT cbor_item_t* cbor_load(cbor_data source, size_t source_size,
+ struct cbor_load_result* result);
+
+/** Deep copy of an item
+ *
+ * All the reference counts in the new structure are set to one.
+ *
+ * @param item[borrow] item to copy
+ * @return **new** CBOR deep copy
+ */
+CBOR_EXPORT cbor_item_t* cbor_copy(cbor_item_t* item);
+
+#if CBOR_PRETTY_PRINTER
+#include <stdio.h>
+
+CBOR_EXPORT void cbor_describe(cbor_item_t* item, FILE* out);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_H_
diff --git a/src/cbor/arrays.c b/src/cbor/arrays.c
new file mode 100644
index 000000000000..c1d01afafee1
--- /dev/null
+++ b/src/cbor/arrays.c
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "arrays.h"
+#include <string.h>
+#include "internal/memory_utils.h"
+
+size_t cbor_array_size(const cbor_item_t *item) {
+ assert(cbor_isa_array(item));
+ return item->metadata.array_metadata.end_ptr;
+}
+
+size_t cbor_array_allocated(const cbor_item_t *item) {
+ assert(cbor_isa_array(item));
+ return item->metadata.array_metadata.allocated;
+}
+
+cbor_item_t *cbor_array_get(const cbor_item_t *item, size_t index) {
+ return cbor_incref(((cbor_item_t **)item->data)[index]);
+}
+
+bool cbor_array_set(cbor_item_t *item, size_t index, cbor_item_t *value) {
+ if (index == item->metadata.array_metadata.end_ptr) {
+ return cbor_array_push(item, value);
+ } else if (index < item->metadata.array_metadata.end_ptr) {
+ return cbor_array_replace(item, index, value);
+ } else {
+ return false;
+ }
+ // TODO: This is unreachable and the index checking logic above seems
+ // suspicious -- out of bounds index is a caller error. Figure out & fix.
+ return true;
+}
+
+bool cbor_array_replace(cbor_item_t *item, size_t index, cbor_item_t *value) {
+ if (index >= item->metadata.array_metadata.end_ptr) return false;
+ /* We cannot use cbor_array_get as that would increase the refcount */
+ cbor_intermediate_decref(((cbor_item_t **)item->data)[index]);
+ ((cbor_item_t **)item->data)[index] = cbor_incref(value);
+ return true;
+}
+
+bool cbor_array_push(cbor_item_t *array, cbor_item_t *pushee) {
+ assert(cbor_isa_array(array));
+ struct _cbor_array_metadata *metadata =
+ (struct _cbor_array_metadata *)&array->metadata;
+ cbor_item_t **data = (cbor_item_t **)array->data;
+ if (cbor_array_is_definite(array)) {
+ /* Do not reallocate definite arrays */
+ if (metadata->end_ptr >= metadata->allocated) {
+ return false;
+ }
+ data[metadata->end_ptr++] = pushee;
+ } else {
+ /* Exponential realloc */
+ if (metadata->end_ptr >= metadata->allocated) {
+ // Check for overflows first
+ // TODO: Explicitly test this
+ if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, metadata->allocated)) {
+ return false;
+ }
+
+ size_t new_allocation = metadata->allocated == 0
+ ? 1
+ : CBOR_BUFFER_GROWTH * metadata->allocated;
+
+ unsigned char *new_data = _cbor_realloc_multiple(
+ array->data, sizeof(cbor_item_t *), new_allocation);
+ if (new_data == NULL) {
+ return false;
+ }
+
+ array->data = new_data;
+ metadata->allocated = new_allocation;
+ }
+ ((cbor_item_t **)array->data)[metadata->end_ptr++] = pushee;
+ }
+ cbor_incref(pushee);
+ return true;
+}
+
+bool cbor_array_is_definite(const cbor_item_t *item) {
+ assert(cbor_isa_array(item));
+ return item->metadata.array_metadata.type == _CBOR_METADATA_DEFINITE;
+}
+
+bool cbor_array_is_indefinite(const cbor_item_t *item) {
+ assert(cbor_isa_array(item));
+ return item->metadata.array_metadata.type == _CBOR_METADATA_INDEFINITE;
+}
+
+cbor_item_t **cbor_array_handle(const cbor_item_t *item) {
+ assert(cbor_isa_array(item));
+ return (cbor_item_t **)item->data;
+}
+
+cbor_item_t *cbor_new_definite_array(size_t size) {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
+ _CBOR_NOTNULL(item);
+ cbor_item_t **data = _cbor_alloc_multiple(sizeof(cbor_item_t *), size);
+ _CBOR_DEPENDENT_NOTNULL(item, data);
+
+ for (size_t i = 0; i < size; i++) {
+ data[i] = NULL;
+ }
+
+ *item = (cbor_item_t){
+ .refcount = 1,
+ .type = CBOR_TYPE_ARRAY,
+ .metadata = {.array_metadata = {.type = _CBOR_METADATA_DEFINITE,
+ .allocated = size,
+ .end_ptr = 0}},
+ .data = (unsigned char *)data};
+
+ return item;
+}
+
+cbor_item_t *cbor_new_indefinite_array() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
+ _CBOR_NOTNULL(item);
+
+ *item = (cbor_item_t){
+ .refcount = 1,
+ .type = CBOR_TYPE_ARRAY,
+ .metadata = {.array_metadata = {.type = _CBOR_METADATA_INDEFINITE,
+ .allocated = 0,
+ .end_ptr = 0}},
+ .data = NULL /* Can be safely realloc-ed */
+ };
+ return item;
+}
diff --git a/src/cbor/arrays.h b/src/cbor/arrays.h
new file mode 100644
index 000000000000..85fe51dcd8dc
--- /dev/null
+++ b/src/cbor/arrays.h
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_ARRAYS_H
+#define LIBCBOR_ARRAYS_H
+
+#include "cbor/cbor_export.h"
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Get the number of members
+ *
+ * @param item[borrow] An array
+ * @return The number of members
+ */
+CBOR_EXPORT size_t cbor_array_size(const cbor_item_t* item);
+
+/** Get the size of the allocated storage
+ *
+ * @param item[borrow] An array
+ * @return The size of the allocated storage (number of items)
+ */
+CBOR_EXPORT size_t cbor_array_allocated(const cbor_item_t* item);
+
+/** Get item by index
+ *
+ * @param item[borrow] An array
+ * @param index The index
+ * @return **incref** The item, or `NULL` in case of boundary violation
+ */
+CBOR_EXPORT cbor_item_t* cbor_array_get(const cbor_item_t* item, size_t index);
+
+/** Set item by index
+ *
+ * Creating arrays with holes is not possible
+ *
+ * @param item[borrow] An array
+ * @param value[incref] The item to assign
+ * @param index The index, first item is 0.
+ * @return true on success, false on allocation failure.
+ */
+CBOR_EXPORT bool cbor_array_set(cbor_item_t* item, size_t index,
+ cbor_item_t* value);
+
+/** Replace item at an index
+ *
+ * The item being replace will be #cbor_decref 'ed.
+ *
+ * @param item[borrow] An array
+ * @param value[incref] The item to assign
+ * @param index The index, first item is 0.
+ * @return true on success, false on allocation failure.
+ */
+CBOR_EXPORT bool cbor_array_replace(cbor_item_t* item, size_t index,
+ cbor_item_t* value);
+
+/** Is the array definite?
+ *
+ * @param item[borrow] An array
+ * @return Is the array definite?
+ */
+CBOR_EXPORT bool cbor_array_is_definite(const cbor_item_t* item);
+
+/** Is the array indefinite?
+ *
+ * @param item[borrow] An array
+ * @return Is the array indefinite?
+ */
+CBOR_EXPORT bool cbor_array_is_indefinite(const cbor_item_t* item);
+
+/** Get the array contents
+ *
+ * The items may be reordered and modified as long as references remain
+ * consistent.
+ *
+ * @param item[borrow] An array
+ * @return #cbor_array_size items
+ */
+CBOR_EXPORT cbor_item_t** cbor_array_handle(const cbor_item_t* item);
+
+/** Create new definite array
+ *
+ * @param size Number of slots to preallocate
+ * @return **new** array or `NULL` upon malloc failure
+ */
+CBOR_EXPORT cbor_item_t* cbor_new_definite_array(size_t size);
+
+/** Create new indefinite array
+ *
+ * @return **new** array or `NULL` upon malloc failure
+ */
+CBOR_EXPORT cbor_item_t* cbor_new_indefinite_array();
+
+/** Append to the end
+ *
+ * For indefinite items, storage may be realloacted. For definite items, only
+ * the preallocated capacity is available.
+ *
+ * @param array[borrow] An array
+ * @param pushee[incref] The item to push
+ * @return true on success, false on failure
+ */
+CBOR_EXPORT bool cbor_array_push(cbor_item_t* array, cbor_item_t* pushee);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_ARRAYS_H
diff --git a/src/cbor/bytestrings.c b/src/cbor/bytestrings.c
new file mode 100644
index 000000000000..75a737bd92ae
--- /dev/null
+++ b/src/cbor/bytestrings.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "bytestrings.h"
+#include <string.h>
+#include "internal/memory_utils.h"
+
+size_t cbor_bytestring_length(const cbor_item_t *item) {
+ assert(cbor_isa_bytestring(item));
+ return item->metadata.bytestring_metadata.length;
+}
+
+unsigned char *cbor_bytestring_handle(const cbor_item_t *item) {
+ assert(cbor_isa_bytestring(item));
+ return item->data;
+}
+
+bool cbor_bytestring_is_definite(const cbor_item_t *item) {
+ assert(cbor_isa_bytestring(item));
+ return item->metadata.bytestring_metadata.type == _CBOR_METADATA_DEFINITE;
+}
+
+bool cbor_bytestring_is_indefinite(const cbor_item_t *item) {
+ return !cbor_bytestring_is_definite(item);
+}
+
+cbor_item_t *cbor_new_definite_bytestring() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
+ _CBOR_NOTNULL(item);
+ *item = (cbor_item_t){
+ .refcount = 1,
+ .type = CBOR_TYPE_BYTESTRING,
+ .metadata = {.bytestring_metadata = {_CBOR_METADATA_DEFINITE, 0}}};
+ return item;
+}
+
+cbor_item_t *cbor_new_indefinite_bytestring() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
+ _CBOR_NOTNULL(item);
+ *item = (cbor_item_t){
+ .refcount = 1,
+ .type = CBOR_TYPE_BYTESTRING,
+ .metadata = {.bytestring_metadata = {.type = _CBOR_METADATA_INDEFINITE,
+ .length = 0}},
+ .data = _CBOR_MALLOC(sizeof(struct cbor_indefinite_string_data))};
+ _CBOR_DEPENDENT_NOTNULL(item, item->data);
+ *((struct cbor_indefinite_string_data *)item->data) =
+ (struct cbor_indefinite_string_data){
+ .chunk_count = 0,
+ .chunk_capacity = 0,
+ .chunks = NULL,
+ };
+ return item;
+}
+
+cbor_item_t *cbor_build_bytestring(cbor_data handle, size_t length) {
+ cbor_item_t *item = cbor_new_definite_bytestring();
+ _CBOR_NOTNULL(item);
+ void *content = _CBOR_MALLOC(length);
+ _CBOR_DEPENDENT_NOTNULL(item, content);
+ memcpy(content, handle, length);
+ cbor_bytestring_set_handle(item, content, length);
+ return item;
+}
+
+void cbor_bytestring_set_handle(cbor_item_t *item,
+ cbor_mutable_data CBOR_RESTRICT_POINTER data,
+ size_t length) {
+ assert(cbor_isa_bytestring(item));
+ assert(cbor_bytestring_is_definite(item));
+ item->data = data;
+ item->metadata.bytestring_metadata.length = length;
+}
+
+cbor_item_t **cbor_bytestring_chunks_handle(const cbor_item_t *item) {
+ assert(cbor_isa_bytestring(item));
+ assert(cbor_bytestring_is_indefinite(item));
+ return ((struct cbor_indefinite_string_data *)item->data)->chunks;
+}
+
+size_t cbor_bytestring_chunk_count(const cbor_item_t *item) {
+ assert(cbor_isa_bytestring(item));
+ assert(cbor_bytestring_is_indefinite(item));
+ return ((struct cbor_indefinite_string_data *)item->data)->chunk_count;
+}
+
+bool cbor_bytestring_add_chunk(cbor_item_t *item, cbor_item_t *chunk) {
+ assert(cbor_isa_bytestring(item));
+ assert(cbor_bytestring_is_indefinite(item));
+ struct cbor_indefinite_string_data *data =
+ (struct cbor_indefinite_string_data *)item->data;
+ if (data->chunk_count == data->chunk_capacity) {
+ // TODO: Add a test for this
+ if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) {
+ return false;
+ }
+
+ size_t new_chunk_capacity =
+ data->chunk_capacity == 0 ? 1
+ : CBOR_BUFFER_GROWTH * (data->chunk_capacity);
+
+ cbor_item_t **new_chunks_data = _cbor_realloc_multiple(
+ data->chunks, sizeof(cbor_item_t *), new_chunk_capacity);
+
+ if (new_chunks_data == NULL) {
+ return false;
+ }
+ data->chunk_capacity = new_chunk_capacity;
+ data->chunks = new_chunks_data;
+ }
+ data->chunks[data->chunk_count++] = cbor_incref(chunk);
+ return true;
+}
diff --git a/src/cbor/bytestrings.h b/src/cbor/bytestrings.h
new file mode 100644
index 000000000000..71483f708e26
--- /dev/null
+++ b/src/cbor/bytestrings.h
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_BYTESTRINGS_H
+#define LIBCBOR_BYTESTRINGS_H
+
+#include "cbor/cbor_export.h"
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * ============================================================================
+ * Byte string manipulation
+ * ============================================================================
+ */
+
+/** Returns the length of the binary data
+ *
+ * For definite byte strings only
+ *
+ * @param item[borrow] a definite bytestring
+ * @return length of the binary data. Zero if no chunk has been attached yet
+ */
+CBOR_EXPORT size_t cbor_bytestring_length(const cbor_item_t *item);
+
+/** Is the byte string definite?
+ *
+ * @param item[borrow] a byte string
+ * @return Is the byte string definite?
+ */
+CBOR_EXPORT bool cbor_bytestring_is_definite(const cbor_item_t *item);
+
+/** Is the byte string indefinite?
+ *
+ * @param item[borrow] a byte string
+ * @return Is the byte string indefinite?
+ */
+CBOR_EXPORT bool cbor_bytestring_is_indefinite(const cbor_item_t *item);
+
+/** Get the handle to the binary data
+ *
+ * Definite items only. Modifying the data is allowed. In that case, the caller
+ * takes responsibility for the effect on items this item might be a part of
+ *
+ * @param item[borrow] A definite byte string
+ * @return The address of the binary data. `NULL` if no data have been assigned
+ * yet.
+ */
+CBOR_EXPORT cbor_mutable_data cbor_bytestring_handle(const cbor_item_t *item);
+
+/** Set the handle to the binary data
+ *
+ * @param item[borrow] A definite byte string
+ * @param data The memory block. The caller gives up the ownership of the block.
+ * libcbor will deallocate it when appropriate using its free function
+ * @param length Length of the data block
+ */
+CBOR_EXPORT void cbor_bytestring_set_handle(
+ cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data,
+ size_t length);
+
+/** Get the handle to the array of chunks
+ *
+ * Manipulations with the memory block (e.g. sorting it) are allowed, but the
+ * validity and the number of chunks must be retained.
+ *
+ * @param item[borrow] A indefinite byte string
+ * @return array of #cbor_bytestring_chunk_count definite bytestrings
+ */
+CBOR_EXPORT cbor_item_t **cbor_bytestring_chunks_handle(
+ const cbor_item_t *item);
+
+/** Get the number of chunks this string consist of
+ *
+ * @param item[borrow] A indefinite bytestring
+ * @return The chunk count. 0 for freshly created items.
+ */
+CBOR_EXPORT size_t cbor_bytestring_chunk_count(const cbor_item_t *item);
+
+/** Appends a chunk to the bytestring
+ *
+ * Indefinite byte strings only.
+ *
+ * May realloc the chunk storage.
+ *
+ * @param item[borrow] An indefinite byte string
+ * @param item[incref] A definite byte string
+ * @return true on success, false on realloc failure. In that case, the refcount
+ * of `chunk` is not increased and the `item` is left intact.
+ */
+CBOR_EXPORT bool cbor_bytestring_add_chunk(cbor_item_t *item,
+ cbor_item_t *chunk);
+
+/** Creates a new definite byte string
+ *
+ * The handle is initialized to `NULL` and length to 0
+ *
+ * @return **new** definite bytestring. `NULL` on malloc failure.
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_definite_bytestring();
+
+/** Creates a new indefinite byte string
+ *
+ * The chunks array is initialized to `NULL` and chunkcount to 0
+ *
+ * @return **new** indefinite bytestring. `NULL` on malloc failure.
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_indefinite_bytestring();
+
+/** Creates a new byte string and initializes it
+ *
+ * The `handle` will be copied to a newly allocated block
+ *
+ * @param handle Block of binary data
+ * @param length Length of `data`
+ * @return A **new** byte string with content `handle`. `NULL` on malloc
+ * failure.
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_bytestring(cbor_data handle, size_t length);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_BYTESTRINGS_H
diff --git a/src/cbor/callbacks.c b/src/cbor/callbacks.c
new file mode 100644
index 000000000000..3f1f547a09ef
--- /dev/null
+++ b/src/cbor/callbacks.c
@@ -0,0 +1,116 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "callbacks.h"
+
+#define CBOR_DUMMY_CALLBACK \
+ {}
+
+void cbor_null_uint8_callback(void *_ctx, uint8_t _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_uint16_callback(void *_ctx,
+ uint16_t _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_uint32_callback(void *_ctx,
+ uint32_t _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_uint64_callback(void *_ctx,
+ uint64_t _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_negint8_callback(void *_ctx,
+ uint8_t _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_negint16_callback(void *_ctx,
+ uint16_t _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_negint32_callback(void *_ctx,
+ uint32_t _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_negint64_callback(void *_ctx,
+ uint64_t _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_string_callback(void *_ctx, cbor_data _val,
+ size_t _val2) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_string_start_callback(void *_ctx) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_byte_string_callback(void *_ctx, cbor_data _val,
+ size_t _val2) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_byte_string_start_callback(void *_ctx) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_array_start_callback(void *_ctx,
+ size_t _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_indef_array_start_callback(void *_ctx) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_map_start_callback(void *_ctx,
+ size_t _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_indef_map_start_callback(void *_ctx) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_tag_callback(void *_ctx, uint64_t _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_float2_callback(void *_ctx, float _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_float4_callback(void *_ctx, float _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_float8_callback(void *_ctx, double _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_null_callback(void *_ctx) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_undefined_callback(void *_ctx) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_boolean_callback(void *_ctx, bool _val) CBOR_DUMMY_CALLBACK
+
+ void cbor_null_indef_break_callback(void *_ctx) CBOR_DUMMY_CALLBACK
+
+ CBOR_EXPORT const struct cbor_callbacks cbor_empty_callbacks = {
+ /* Type 0 - Unsigned integers */
+ .uint8 = cbor_null_uint8_callback,
+ .uint16 = cbor_null_uint16_callback,
+ .uint32 = cbor_null_uint32_callback,
+ .uint64 = cbor_null_uint64_callback,
+
+ /* Type 1 - Negative integers */
+ .negint8 = cbor_null_negint8_callback,
+ .negint16 = cbor_null_negint16_callback,
+ .negint32 = cbor_null_negint32_callback,
+ .negint64 = cbor_null_negint64_callback,
+
+ /* Type 2 - Byte strings */
+ .byte_string_start = cbor_null_byte_string_start_callback,
+ .byte_string = cbor_null_byte_string_callback,
+
+ /* Type 3 - Strings */
+ .string_start = cbor_null_string_start_callback,
+ .string = cbor_null_string_callback,
+
+ /* Type 4 - Arrays */
+ .indef_array_start = cbor_null_indef_array_start_callback,
+ .array_start = cbor_null_array_start_callback,
+
+ /* Type 5 - Maps */
+ .indef_map_start = cbor_null_indef_map_start_callback,
+ .map_start = cbor_null_map_start_callback,
+
+ /* Type 6 - Tags */
+ .tag = cbor_null_tag_callback,
+
+ /* Type 7 - Floats & misc */
+ /* Type names cannot be member names */
+ .float2 = cbor_null_float2_callback,
+ /* 2B float is not supported in standard C */
+ .float4 = cbor_null_float4_callback,
+ .float8 = cbor_null_float8_callback,
+ .undefined = cbor_null_undefined_callback,
+ .null = cbor_null_null_callback,
+ .boolean = cbor_null_boolean_callback,
+
+ /* Shared indefinites */
+ .indef_break = cbor_null_indef_break_callback,
+};
diff --git a/src/cbor/callbacks.h b/src/cbor/callbacks.h
new file mode 100644
index 000000000000..9e5965f2d921
--- /dev/null
+++ b/src/cbor/callbacks.h
@@ -0,0 +1,187 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_CALLBACKS_H
+#define LIBCBOR_CALLBACKS_H
+
+#include "cbor/cbor_export.h"
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Callback prototype */
+typedef void (*cbor_int8_callback)(void *, uint8_t);
+
+/** Callback prototype */
+typedef void (*cbor_int16_callback)(void *, uint16_t);
+
+/** Callback prototype */
+typedef void (*cbor_int32_callback)(void *, uint32_t);
+
+/** Callback prototype */
+typedef void (*cbor_int64_callback)(void *, uint64_t);
+
+/** Callback prototype */
+typedef void (*cbor_simple_callback)(void *);
+
+/** Callback prototype */
+typedef void (*cbor_string_callback)(void *, cbor_data, size_t);
+
+/** Callback prototype */
+typedef void (*cbor_collection_callback)(void *, size_t);
+
+/** Callback prototype */
+typedef void (*cbor_float_callback)(void *, float);
+
+/** Callback prototype */
+typedef void (*cbor_double_callback)(void *, double);
+
+/** Callback prototype */
+typedef void (*cbor_bool_callback)(void *, bool);
+
+/** Callback bundle -- passed to the decoder */
+struct cbor_callbacks {
+ /** Unsigned int */
+ cbor_int8_callback uint8;
+ /** Unsigned int */
+ cbor_int16_callback uint16;
+ /** Unsigned int */
+ cbor_int32_callback uint32;
+ /** Unsigned int */
+ cbor_int64_callback uint64;
+
+ /** Negative int */
+ cbor_int64_callback negint64;
+ /** Negative int */
+ cbor_int32_callback negint32;
+ /** Negative int */
+ cbor_int16_callback negint16;
+ /** Negative int */
+ cbor_int8_callback negint8;
+
+ /** Definite byte string */
+ cbor_simple_callback byte_string_start;
+ /** Indefinite byte string start */
+ cbor_string_callback byte_string;
+
+ /** Definite string */
+ cbor_string_callback string;
+ /** Indefinite string start */
+ cbor_simple_callback string_start;
+
+ /** Definite array */
+ cbor_simple_callback indef_array_start;
+ /** Indefinite array */
+ cbor_collection_callback array_start;
+
+ /** Definite map */
+ cbor_simple_callback indef_map_start;
+ /** Indefinite map */
+ cbor_collection_callback map_start;
+
+ /** Tags */
+ cbor_int64_callback tag;
+
+ /** Half float */
+ cbor_float_callback float2;
+ /** Single float */
+ cbor_float_callback float4;
+ /** Double float */
+ cbor_double_callback float8;
+ /** Undef */
+ cbor_simple_callback undefined;
+ /** Null */
+ cbor_simple_callback null;
+ /** Bool */
+ cbor_bool_callback boolean;
+
+ /** Indefinite item break */
+ cbor_simple_callback indef_break;
+};
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_uint8_callback(void *, uint8_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_uint16_callback(void *, uint16_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_uint32_callback(void *, uint32_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_uint64_callback(void *, uint64_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_negint8_callback(void *, uint8_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_negint16_callback(void *, uint16_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_negint32_callback(void *, uint32_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_negint64_callback(void *, uint64_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_string_callback(void *, cbor_data, size_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_string_start_callback(void *);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_byte_string_callback(void *, cbor_data, size_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_byte_string_start_callback(void *);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_array_start_callback(void *, size_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_indef_array_start_callback(void *);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_map_start_callback(void *, size_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_indef_map_start_callback(void *);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_tag_callback(void *, uint64_t);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_float2_callback(void *, float);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_float4_callback(void *, float);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_float8_callback(void *, double);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_null_callback(void *);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_undefined_callback(void *);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_boolean_callback(void *, bool);
+
+/** Dummy callback implementation - does nothing */
+CBOR_EXPORT void cbor_null_indef_break_callback(void *);
+
+/** Dummy callback bundle - does nothing */
+CBOR_EXPORT extern const struct cbor_callbacks cbor_empty_callbacks;
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_CALLBACKS_H
diff --git a/src/cbor/common.c b/src/cbor/common.c
new file mode 100644
index 000000000000..7ccce38ac25b
--- /dev/null
+++ b/src/cbor/common.c
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "cbor/common.h"
+#include "arrays.h"
+#include "bytestrings.h"
+#include "data.h"
+#include "floats_ctrls.h"
+#include "ints.h"
+#include "maps.h"
+#include "strings.h"
+#include "tags.h"
+
+bool cbor_isa_uint(const cbor_item_t *item) {
+ return item->type == CBOR_TYPE_UINT;
+}
+
+bool cbor_isa_negint(const cbor_item_t *item) {
+ return item->type == CBOR_TYPE_NEGINT;
+}
+
+bool cbor_isa_bytestring(const cbor_item_t *item) {
+ return item->type == CBOR_TYPE_BYTESTRING;
+}
+
+bool cbor_isa_string(const cbor_item_t *item) {
+ return item->type == CBOR_TYPE_STRING;
+}
+
+bool cbor_isa_array(const cbor_item_t *item) {
+ return item->type == CBOR_TYPE_ARRAY;
+}
+
+bool cbor_isa_map(const cbor_item_t *item) {
+ return item->type == CBOR_TYPE_MAP;
+}
+
+bool cbor_isa_tag(const cbor_item_t *item) {
+ return item->type == CBOR_TYPE_TAG;
+}
+
+bool cbor_isa_float_ctrl(const cbor_item_t *item) {
+ return item->type == CBOR_TYPE_FLOAT_CTRL;
+}
+
+cbor_type cbor_typeof(const cbor_item_t *item) { return item->type; }
+
+bool cbor_is_int(const cbor_item_t *item) {
+ return cbor_isa_uint(item) || cbor_isa_negint(item);
+}
+
+bool cbor_is_bool(const cbor_item_t *item) {
+ return cbor_isa_float_ctrl(item) &&
+ (cbor_ctrl_value(item) == CBOR_CTRL_FALSE ||
+ cbor_ctrl_value(item) == CBOR_CTRL_TRUE);
+}
+
+bool cbor_is_null(const cbor_item_t *item) {
+ return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_NULL;
+}
+
+bool cbor_is_undef(const cbor_item_t *item) {
+ return cbor_isa_float_ctrl(item) && cbor_ctrl_value(item) == CBOR_CTRL_UNDEF;
+}
+
+bool cbor_is_float(const cbor_item_t *item) {
+ return cbor_isa_float_ctrl(item) && !cbor_float_ctrl_is_ctrl(item);
+}
+
+cbor_item_t *cbor_incref(cbor_item_t *item) {
+ item->refcount++;
+ return item;
+}
+
+void cbor_decref(cbor_item_t **item_ref) {
+ cbor_item_t *item = *item_ref;
+ assert(item->refcount > 0);
+ if (--item->refcount == 0) {
+ switch (item->type) {
+ case CBOR_TYPE_UINT:
+ /* Fallthrough */
+ case CBOR_TYPE_NEGINT:
+ /* Combined allocation, freeing the item suffices */
+ { break; }
+ case CBOR_TYPE_BYTESTRING: {
+ if (cbor_bytestring_is_definite(item)) {
+ _CBOR_FREE(item->data);
+ } else {
+ /* We need to decref all chunks */
+ cbor_item_t **handle = cbor_bytestring_chunks_handle(item);
+ for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++)
+ cbor_decref(&handle[i]);
+ _CBOR_FREE(
+ ((struct cbor_indefinite_string_data *)item->data)->chunks);
+ _CBOR_FREE(item->data);
+ }
+ break;
+ }
+ case CBOR_TYPE_STRING: {
+ if (cbor_string_is_definite(item)) {
+ _CBOR_FREE(item->data);
+ } else {
+ /* We need to decref all chunks */
+ cbor_item_t **handle = cbor_string_chunks_handle(item);
+ for (size_t i = 0; i < cbor_string_chunk_count(item); i++)
+ cbor_decref(&handle[i]);
+ _CBOR_FREE(
+ ((struct cbor_indefinite_string_data *)item->data)->chunks);
+ _CBOR_FREE(item->data);
+ }
+ break;
+ }
+ case CBOR_TYPE_ARRAY: {
+ /* Get all items and decref them */
+ cbor_item_t **handle = cbor_array_handle(item);
+ size_t size = cbor_array_size(item);
+ for (size_t i = 0; i < size; i++)
+ if (handle[i] != NULL) cbor_decref(&handle[i]);
+ _CBOR_FREE(item->data);
+ break;
+ }
+ case CBOR_TYPE_MAP: {
+ struct cbor_pair *handle = cbor_map_handle(item);
+ for (size_t i = 0; i < item->metadata.map_metadata.end_ptr;
+ i++, handle++) {
+ cbor_decref(&handle->key);
+ if (handle->value != NULL) cbor_decref(&handle->value);
+ }
+ _CBOR_FREE(item->data);
+ break;
+ };
+ case CBOR_TYPE_TAG: {
+ if (item->metadata.tag_metadata.tagged_item != NULL)
+ cbor_decref(&item->metadata.tag_metadata.tagged_item);
+ _CBOR_FREE(item->data);
+ break;
+ }
+ case CBOR_TYPE_FLOAT_CTRL: {
+ /* Floats have combined allocation */
+ break;
+ }
+ }
+ _CBOR_FREE(item);
+ // TODO
+ *item_ref = NULL;
+ }
+}
+
+void cbor_intermediate_decref(cbor_item_t *item) { cbor_decref(&item); }
+
+size_t cbor_refcount(const cbor_item_t *item) { return item->refcount; }
+
+cbor_item_t *cbor_move(cbor_item_t *item) {
+ item->refcount--;
+ return item;
+}
diff --git a/src/cbor/common.h b/src/cbor/common.h
new file mode 100644
index 000000000000..1f9b79e16d9d
--- /dev/null
+++ b/src/cbor/common.h
@@ -0,0 +1,303 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_COMMON_H
+#define LIBCBOR_COMMON_H
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include "cbor/cbor_export.h"
+#include "cbor/configuration.h"
+#include "data.h"
+
+#ifdef __cplusplus
+extern "C" {
+
+/**
+ * C++ is not a subset of C99 -- 'restrict' qualifier is not a part of the
+ * language. This is a workaround to keep it in C headers -- compilers allow
+ * linking non-restrict signatures with restrict implementations.
+ *
+ * If you know a nicer way, please do let me know.
+ */
+#define CBOR_RESTRICT_POINTER
+
+#else
+
+// MSVC + C++ workaround
+#define CBOR_RESTRICT_POINTER CBOR_RESTRICT_SPECIFIER
+
+#endif
+
+static const uint8_t cbor_major_version = CBOR_MAJOR_VERSION;
+static const uint8_t cbor_minor_version = CBOR_MINOR_VERSION;
+static const uint8_t cbor_patch_version = CBOR_PATCH_VERSION;
+
+#define CBOR_VERSION \
+ TO_STR(CBOR_MAJOR_VERSION) \
+ "." TO_STR(CBOR_MINOR_VERSION) "." TO_STR(CBOR_PATCH_VERSION)
+#define CBOR_HEX_VERSION \
+ ((CBOR_MAJOR_VERSION << 16) | (CBOR_MINOR_VERSION << 8) | CBOR_PATCH_VERSION)
+
+/* http://stackoverflow.com/questions/1644868/c-define-macro-for-debug-printing
+ */
+#ifdef DEBUG
+#include <stdio.h>
+#define debug_print(fmt, ...) \
+ do { \
+ if (DEBUG) \
+ fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, __LINE__, __func__, \
+ __VA_ARGS__); \
+ } while (0)
+#else
+#define debug_print(fmt, ...) \
+ do { \
+ } while (0)
+#endif
+
+#define TO_STR_(x) #x
+#define TO_STR(x) TO_STR_(x) /* enables proper double expansion */
+
+// Macro to short-circuit builder functions when memory allocation fails
+#define _CBOR_NOTNULL(cbor_item) \
+ do { \
+ if (cbor_item == NULL) { \
+ return NULL; \
+ } \
+ } while (0)
+
+// Macro to short-circuit builders when memory allocation of nested data fails
+#define _CBOR_DEPENDENT_NOTNULL(cbor_item, pointer) \
+ do { \
+ if (pointer == NULL) { \
+ _CBOR_FREE(cbor_item); \
+ return NULL; \
+ } \
+ } while (0)
+
+#if CBOR_CUSTOM_ALLOC
+
+typedef void *(*_cbor_malloc_t)(size_t);
+typedef void *(*_cbor_realloc_t)(void *, size_t);
+typedef void (*_cbor_free_t)(void *);
+
+CBOR_EXPORT extern _cbor_malloc_t _cbor_malloc;
+CBOR_EXPORT extern _cbor_realloc_t _cbor_realloc;
+CBOR_EXPORT extern _cbor_free_t _cbor_free;
+
+/** Sets the memory management routines to use.
+ *
+ * Only available when `CBOR_CUSTOM_ALLOC` is truthy
+ *
+ * \rst
+ * .. warning:: This function modifies the global state and should therefore be
+ * used accordingly. Changing the memory handlers while allocated items exist
+ * will result in a ``free``/``malloc`` mismatch. This function is not thread
+ * safe with respect to both itself and all the other *libcbor* functions that
+ * work with the heap.
+ *
+ * .. note:: `realloc` implementation must correctly support `NULL` reallocation
+ * (see e.g. http://en.cppreference.com/w/c/memory/realloc)
+ * \endrst
+ *
+ * @param custom_malloc malloc implementation
+ * @param custom_realloc realloc implementation
+ * @param custom_free free implementation
+ */
+CBOR_EXPORT void cbor_set_allocs(_cbor_malloc_t custom_malloc,
+ _cbor_realloc_t custom_realloc,
+ _cbor_free_t custom_free);
+
+#define _CBOR_MALLOC _cbor_malloc
+#define _CBOR_REALLOC _cbor_realloc
+#define _CBOR_FREE _cbor_free
+
+#else
+
+#define _CBOR_MALLOC malloc
+#define _CBOR_REALLOC realloc
+#define _CBOR_FREE free
+
+#endif
+
+/*
+ * ============================================================================
+ * Type manipulation
+ * ============================================================================
+ */
+
+/** Get the type of the item
+ *
+ * @param item[borrow]
+ * @return The type
+ */
+CBOR_EXPORT cbor_type cbor_typeof(
+ const cbor_item_t *item); /* Will be inlined iff link-time opt is enabled */
+
+/* Standard item types as described by the RFC */
+
+/** Does the item have the appropriate major type?
+ * @param item[borrow] the item
+ * @return Is the item an #CBOR_TYPE_UINT?
+ */
+CBOR_EXPORT bool cbor_isa_uint(const cbor_item_t *item);
+
+/** Does the item have the appropriate major type?
+ * @param item[borrow] the item
+ * @return Is the item a #CBOR_TYPE_NEGINT?
+ */
+CBOR_EXPORT bool cbor_isa_negint(const cbor_item_t *item);
+
+/** Does the item have the appropriate major type?
+ * @param item[borrow] the item
+ * @return Is the item a #CBOR_TYPE_BYTESTRING?
+ */
+CBOR_EXPORT bool cbor_isa_bytestring(const cbor_item_t *item);
+
+/** Does the item have the appropriate major type?
+ * @param item[borrow] the item
+ * @return Is the item a #CBOR_TYPE_STRING?
+ */
+CBOR_EXPORT bool cbor_isa_string(const cbor_item_t *item);
+
+/** Does the item have the appropriate major type?
+ * @param item[borrow] the item
+ * @return Is the item an #CBOR_TYPE_ARRAY?
+ */
+CBOR_EXPORT bool cbor_isa_array(const cbor_item_t *item);
+
+/** Does the item have the appropriate major type?
+ * @param item[borrow] the item
+ * @return Is the item a #CBOR_TYPE_MAP?
+ */
+CBOR_EXPORT bool cbor_isa_map(const cbor_item_t *item);
+
+/** Does the item have the appropriate major type?
+ * @param item[borrow] the item
+ * @return Is the item a #CBOR_TYPE_TAG?
+ */
+CBOR_EXPORT bool cbor_isa_tag(const cbor_item_t *item);
+
+/** Does the item have the appropriate major type?
+ * @param item[borrow] the item
+ * @return Is the item a #CBOR_TYPE_FLOAT_CTRL?
+ */
+CBOR_EXPORT bool cbor_isa_float_ctrl(const cbor_item_t *item);
+
+/* Practical types with respect to their semantics (but not tag values) */
+
+/** Is the item an integer, either positive or negative?
+ * @param item[borrow] the item
+ * @return Is the item an integer, either positive or negative?
+ */
+CBOR_EXPORT bool cbor_is_int(const cbor_item_t *item);
+
+/** Is the item an a floating point number?
+ * @param item[borrow] the item
+ * @return Is the item a floating point number?
+ */
+CBOR_EXPORT bool cbor_is_float(const cbor_item_t *item);
+
+/** Is the item an a boolean?
+ * @param item[borrow] the item
+ * @return Is the item a boolean?
+ */
+CBOR_EXPORT bool cbor_is_bool(const cbor_item_t *item);
+
+/** Does this item represent `null`
+ *
+ * \rst
+ * .. warning:: This is in no way related to the value of the pointer. Passing a
+ * null pointer will most likely result in a crash.
+ * \endrst
+ *
+ * @param item[borrow] the item
+ * @return Is the item (CBOR logical) null?
+ */
+CBOR_EXPORT bool cbor_is_null(const cbor_item_t *item);
+
+/** Does this item represent `undefined`
+ *
+ * \rst
+ * .. warning:: Care must be taken to distinguish nulls and undefined values in
+ * C.
+ * \endrst
+ *
+ * @param item[borrow] the item
+ * @return Is the item (CBOR logical) undefined?
+ */
+CBOR_EXPORT bool cbor_is_undef(const cbor_item_t *item);
+
+/*
+ * ============================================================================
+ * Memory management
+ * ============================================================================
+ */
+
+/** Increases the reference count by one
+ *
+ * No dependent items are affected.
+ *
+ * @param item[incref] item the item
+ * @return the input reference
+ */
+CBOR_EXPORT cbor_item_t *cbor_incref(cbor_item_t *item);
+
+/** Decreases the reference count by one, deallocating the item if needed
+ *
+ * In case the item is deallocated, the reference count of any dependent items
+ * is adjusted accordingly in a recursive manner.
+ *
+ * @param item[take] the item. Set to `NULL` if deallocated
+ */
+CBOR_EXPORT void cbor_decref(cbor_item_t **item);
+
+/** Decreases the reference count by one, deallocating the item if needed
+ *
+ * Convenience wrapper for #cbor_decref when its set-to-null behavior is not
+ * needed
+ *
+ * @param item[take] the item
+ */
+CBOR_EXPORT void cbor_intermediate_decref(cbor_item_t *item);
+
+/** Get the reference count
+ *
+ * \rst
+ * .. warning:: This does *not* account for transitive references.
+ * \endrst
+ *
+ * @param item[borrow] the item
+ * @return the reference count
+ */
+CBOR_EXPORT size_t cbor_refcount(const cbor_item_t *item);
+
+/** Provides CPP-like move construct
+ *
+ * Decreases the reference count by one, but does not deallocate the item even
+ * if its refcount reaches zero. This is useful for passing intermediate values
+ * to functions that increase reference count. Should only be used with
+ * functions that `incref` their arguments.
+ *
+ * \rst
+ * .. warning:: If the item is moved without correctly increasing the reference
+ * count afterwards, the memory will be leaked.
+ * \endrst
+ *
+ * @param item[take] the item
+ * @return the item with reference count decreased by one
+ */
+CBOR_EXPORT cbor_item_t *cbor_move(cbor_item_t *item);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_COMMON_H
diff --git a/src/cbor/configuration.h.in b/src/cbor/configuration.h.in
new file mode 100644
index 000000000000..6f65980aa8a2
--- /dev/null
+++ b/src/cbor/configuration.h.in
@@ -0,0 +1,16 @@
+#ifndef LIBCBOR_CONFIGURATION_H
+#define LIBCBOR_CONFIGURATION_H
+
+#define CBOR_MAJOR_VERSION ${CBOR_VERSION_MAJOR}
+#define CBOR_MINOR_VERSION ${CBOR_VERSION_MINOR}
+#define CBOR_PATCH_VERSION ${CBOR_VERSION_PATCH}
+
+#cmakedefine01 CBOR_CUSTOM_ALLOC
+#define CBOR_BUFFER_GROWTH ${CBOR_BUFFER_GROWTH}
+#define CBOR_MAX_STACK_SIZE ${CBOR_MAX_STACK_SIZE}
+#cmakedefine01 CBOR_PRETTY_PRINTER
+
+#define CBOR_RESTRICT_SPECIFIER ${CBOR_RESTRICT_SPECIFIER}
+#define CBOR_INLINE_SPECIFIER ${CBOR_INLINE_SPECIFIER}
+
+#endif //LIBCBOR_CONFIGURATION_H
diff --git a/src/cbor/data.h b/src/cbor/data.h
new file mode 100644
index 000000000000..982e6a2c9987
--- /dev/null
+++ b/src/cbor/data.h
@@ -0,0 +1,255 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_DATA_H
+#define LIBCBOR_DATA_H
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef const unsigned char* cbor_data;
+typedef unsigned char* cbor_mutable_data;
+
+/** Specifies the Major type of ::cbor_item_t */
+typedef enum cbor_type {
+ CBOR_TYPE_UINT /** 0 - positive integers */
+ ,
+ CBOR_TYPE_NEGINT /** 1 - negative integers*/
+ ,
+ CBOR_TYPE_BYTESTRING /** 2 - byte strings */
+ ,
+ CBOR_TYPE_STRING /** 3 - strings */
+ ,
+ CBOR_TYPE_ARRAY /** 4 - arrays */
+ ,
+ CBOR_TYPE_MAP /** 5 - maps */
+ ,
+ CBOR_TYPE_TAG /** 6 - tags */
+ ,
+ CBOR_TYPE_FLOAT_CTRL /** 7 - decimals and special values (true, false, nil,
+ ...) */
+} cbor_type;
+
+/** Possible decoding errors */
+typedef enum {
+ CBOR_ERR_NONE,
+ CBOR_ERR_NOTENOUGHDATA,
+ CBOR_ERR_NODATA,
+ CBOR_ERR_MALFORMATED,
+ CBOR_ERR_MEMERROR /** Memory error - item allocation failed. Is it too big for
+ your allocator? */
+ ,
+ CBOR_ERR_SYNTAXERROR /** Stack parsing algorithm failed */
+} cbor_error_code;
+
+/** Possible widths of #CBOR_TYPE_UINT items */
+typedef enum {
+ CBOR_INT_8,
+ CBOR_INT_16,
+ CBOR_INT_32,
+ CBOR_INT_64
+} cbor_int_width;
+
+/** Possible widths of #CBOR_TYPE_FLOAT_CTRL items */
+typedef enum {
+ CBOR_FLOAT_0 /** Internal use - ctrl and special values */
+ ,
+ CBOR_FLOAT_16 /** Half float */
+ ,
+ CBOR_FLOAT_32 /** Single float */
+ ,
+ CBOR_FLOAT_64 /** Double */
+} cbor_float_width;
+
+/** Metadata for dynamically sized types */
+typedef enum {
+ _CBOR_METADATA_DEFINITE,
+ _CBOR_METADATA_INDEFINITE
+} _cbor_dst_metadata;
+
+/** Semantic mapping for CTRL simple values */
+typedef enum {
+ CBOR_CTRL_NONE = 0,
+ CBOR_CTRL_FALSE = 20,
+ CBOR_CTRL_TRUE = 21,
+ CBOR_CTRL_NULL = 22,
+ CBOR_CTRL_UNDEF = 23
+} _cbor_ctrl;
+
+/** Integers specific metadata */
+struct _cbor_int_metadata {
+ cbor_int_width width;
+};
+
+/** Bytestrings specific metadata */
+struct _cbor_bytestring_metadata {
+ size_t length;
+ _cbor_dst_metadata type;
+};
+
+/** Strings specific metadata */
+struct _cbor_string_metadata {
+ size_t length;
+ size_t codepoint_count; /* Sum of chunks' codepoint_counts for indefinite
+ strings */
+ _cbor_dst_metadata type;
+};
+
+/** Arrays specific metadata */
+struct _cbor_array_metadata {
+ size_t allocated;
+ size_t end_ptr;
+ _cbor_dst_metadata type;
+};
+
+/** Maps specific metadata */
+struct _cbor_map_metadata {
+ size_t allocated;
+ size_t end_ptr;
+ _cbor_dst_metadata type;
+};
+
+/** Arrays specific metadata
+ *
+ * The pointer is included - cbor_item_metadata is
+ * 2 * sizeof(size_t) + sizeof(_cbor_string_type_metadata),
+ * lets use the space
+ */
+struct _cbor_tag_metadata {
+ struct cbor_item_t* tagged_item;
+ uint64_t value;
+};
+
+/** Floats specific metadata - includes CTRL values */
+struct _cbor_float_ctrl_metadata {
+ cbor_float_width width;
+ uint8_t ctrl;
+};
+
+/** Raw memory casts helper */
+union _cbor_float_helper {
+ float as_float;
+ uint32_t as_uint;
+};
+
+/** Raw memory casts helper */
+union _cbor_double_helper {
+ double as_double;
+ uint64_t as_uint;
+};
+
+/** Union of metadata across all possible types - discriminated in #cbor_item_t
+ */
+union cbor_item_metadata {
+ struct _cbor_int_metadata int_metadata;
+ struct _cbor_bytestring_metadata bytestring_metadata;
+ struct _cbor_string_metadata string_metadata;
+ struct _cbor_array_metadata array_metadata;
+ struct _cbor_map_metadata map_metadata;
+ struct _cbor_tag_metadata tag_metadata;
+ struct _cbor_float_ctrl_metadata float_ctrl_metadata;
+};
+
+/** The item handle */
+typedef struct cbor_item_t {
+ /** Discriminated by type */
+ union cbor_item_metadata metadata;
+ /** Reference count - initialize to 0 */
+ size_t refcount;
+ /** Major type discriminator */
+ cbor_type type;
+ /** Raw data block - interpretation depends on metadata */
+ unsigned char* data;
+} cbor_item_t;
+
+/** Defines cbor_item_t#data structure for indefinite strings and bytestrings
+ *
+ * Used to cast the raw representation for a sane manipulation
+ */
+struct cbor_indefinite_string_data {
+ size_t chunk_count;
+ size_t chunk_capacity;
+ cbor_item_t** chunks;
+};
+
+/** High-level decoding error */
+struct cbor_error {
+ /** Aproximate position */
+ size_t position;
+ /** Description */
+ cbor_error_code code;
+};
+
+/** Simple pair of items for use in maps */
+struct cbor_pair {
+ cbor_item_t *key, *value;
+};
+
+/** High-level decoding result */
+struct cbor_load_result {
+ /** Error indicator */
+ struct cbor_error error;
+ /** Number of bytes read */
+ size_t read;
+};
+
+/** Streaming decoder result - status */
+enum cbor_decoder_status {
+ /** Decoding finished successfully (a callback has been invoked)
+ *
+ * Note that this does *not* mean that the buffer has been fully decoded;
+ * there may still be unread bytes for which no callback has been involved.
+ */
+ CBOR_DECODER_FINISHED,
+ /** Not enough data to invoke a callback */
+ CBOR_DECODER_NEDATA,
+ /** Bad data (reserved MTB, malformed value, etc.) */
+ CBOR_DECODER_ERROR
+};
+
+/** Streaming decoder result */
+struct cbor_decoder_result {
+ /** Input bytes read/consumed
+ *
+ * If this is less than the size of input buffer, the client will likely
+ * resume parsing starting at the next byte (e.g. `buffer + result.read`).
+ *
+ * Set to 0 if the #status is not #CBOR_DECODER_FINISHED.
+ */
+ size_t read;
+
+ /** The decoding status */
+ enum cbor_decoder_status status;
+
+ /** Number of bytes in the input buffer needed to resume parsing
+ *
+ * Set to 0 unless the result status is #CBOR_DECODER_NEDATA. If it is, then:
+ * - If at least one byte was passed, #required will be set to the minimum
+ * number of bytes needed to invoke a decoded callback on the current
+ * prefix.
+ *
+ * For example: Attempting to decode a 1B buffer containing `0x19` will
+ * set #required to 3 as `0x19` signals a 2B integer item, so we need at
+ * least 3B to continue (the `0x19` MTB byte and two bytes of data needed
+ * to invoke #cbor_callbacks.uint16).
+ *
+ * - If there was no data at all, #read will always be set to 1
+ */
+ size_t required;
+};
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_DATA_H
diff --git a/src/cbor/encoding.c b/src/cbor/encoding.c
new file mode 100644
index 000000000000..19281520edd9
--- /dev/null
+++ b/src/cbor/encoding.c
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "encoding.h"
+#include "internal/encoders.h"
+
+size_t cbor_encode_uint8(uint8_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint8(value, buffer, buffer_size, 0x00);
+}
+
+size_t cbor_encode_uint16(uint16_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint16(value, buffer, buffer_size, 0x00);
+}
+
+size_t cbor_encode_uint32(uint32_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint32(value, buffer, buffer_size, 0x00);
+}
+
+size_t cbor_encode_uint64(uint64_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint64(value, buffer, buffer_size, 0x00);
+}
+
+size_t cbor_encode_uint(uint64_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint(value, buffer, buffer_size, 0x00);
+}
+
+size_t cbor_encode_negint8(uint8_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint8(value, buffer, buffer_size, 0x20);
+}
+
+size_t cbor_encode_negint16(uint16_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint16(value, buffer, buffer_size, 0x20);
+}
+
+size_t cbor_encode_negint32(uint32_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint32(value, buffer, buffer_size, 0x20);
+}
+
+size_t cbor_encode_negint64(uint64_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint64(value, buffer, buffer_size, 0x20);
+}
+
+size_t cbor_encode_negint(uint64_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint(value, buffer, buffer_size, 0x20);
+}
+
+size_t cbor_encode_bytestring_start(size_t length, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint((size_t)length, buffer, buffer_size, 0x40);
+}
+
+size_t _cbor_encode_byte(uint8_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ if (buffer_size >= 1) {
+ buffer[0] = value;
+ return 1;
+ } else
+ return 0;
+}
+
+size_t cbor_encode_indef_bytestring_start(unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_byte(0x5F, buffer, buffer_size);
+}
+
+size_t cbor_encode_string_start(size_t length, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint((size_t)length, buffer, buffer_size, 0x60);
+}
+
+size_t cbor_encode_indef_string_start(unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_byte(0x7F, buffer, buffer_size);
+}
+
+size_t cbor_encode_array_start(size_t length, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint((size_t)length, buffer, buffer_size, 0x80);
+}
+
+size_t cbor_encode_indef_array_start(unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_byte(0x9F, buffer, buffer_size);
+}
+
+size_t cbor_encode_map_start(size_t length, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint((size_t)length, buffer, buffer_size, 0xA0);
+}
+
+size_t cbor_encode_indef_map_start(unsigned char *buffer, size_t buffer_size) {
+ return _cbor_encode_byte(0xBF, buffer, buffer_size);
+}
+
+size_t cbor_encode_tag(uint64_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint(value, buffer, buffer_size, 0xC0);
+}
+
+size_t cbor_encode_bool(bool value, unsigned char *buffer, size_t buffer_size) {
+ return value ? _cbor_encode_byte(0xF5, buffer, buffer_size)
+ : _cbor_encode_byte(0xF4, buffer, buffer_size);
+}
+
+size_t cbor_encode_null(unsigned char *buffer, size_t buffer_size) {
+ return _cbor_encode_byte(0xF6, buffer, buffer_size);
+}
+
+size_t cbor_encode_undef(unsigned char *buffer, size_t buffer_size) {
+ return _cbor_encode_byte(0xF7, buffer, buffer_size);
+}
+
+size_t cbor_encode_half(float value, unsigned char *buffer,
+ size_t buffer_size) {
+ /* Assuming value is normalized */
+ uint32_t val = ((union _cbor_float_helper){.as_float = value}).as_uint;
+ uint16_t res;
+ uint8_t exp = (uint8_t)((val & 0x7F800000u) >>
+ 23u); /* 0b0111_1111_1000_0000_0000_0000_0000_0000 */
+ uint32_t mant =
+ val & 0x7FFFFFu; /* 0b0000_0000_0111_1111_1111_1111_1111_1111 */
+ if (exp == 0xFF) { /* Infinity or NaNs */
+ if (value != value) {
+ res = (uint16_t)0x007e00; /* Not IEEE semantics - required by CBOR
+ [s. 3.9] */
+ } else {
+ res = (uint16_t)((val & 0x80000000u) >> 16u | 0x7C00u |
+ (mant ? 1u : 0u) << 15u);
+ }
+ } else if (exp == 0x00) { /* Zeroes or subnorms */
+ res = (uint16_t)((val & 0x80000000u) >> 16u | mant >> 13u);
+ } else { /* Normal numbers */
+ int8_t logical_exp = (int8_t)(exp - 127);
+ assert(logical_exp == exp - 127);
+
+ // Now we know that 2^exp <= 0 logically
+ if (logical_exp < -24) {
+ /* No unambiguous representation exists, this float is not a half float
+ and is too small to be represented using a half, round off to zero.
+ Consistent with the reference implementation. */
+ res = 0;
+ } else if (logical_exp < -14) {
+ /* Offset the remaining decimal places by shifting the significand, the
+ value is lost. This is an implementation decision that works around the
+ absence of standard half-float in the language. */
+ res = (uint16_t)((val & 0x80000000u) >> 16u) | // Extract sign bit
+ (uint16_t)(1u << (24u + logical_exp));
+ } else {
+ res = (uint16_t)((val & 0x80000000u) >> 16u |
+ ((((uint8_t)logical_exp) + 15u) << 10u) |
+ (uint16_t)(mant >> 13u));
+ }
+ }
+ return _cbor_encode_uint16(res, buffer, buffer_size, 0xE0);
+}
+
+size_t cbor_encode_single(float value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint32(
+ ((union _cbor_float_helper){.as_float = value}).as_uint, buffer,
+ buffer_size, 0xE0);
+}
+
+size_t cbor_encode_double(double value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint64(
+ ((union _cbor_double_helper){.as_double = value}).as_uint, buffer,
+ buffer_size, 0xE0);
+}
+
+size_t cbor_encode_break(unsigned char *buffer, size_t buffer_size) {
+ return _cbor_encode_byte(0xFF, buffer, buffer_size);
+}
+
+size_t cbor_encode_ctrl(uint8_t value, unsigned char *buffer,
+ size_t buffer_size) {
+ return _cbor_encode_uint8(value, buffer, buffer_size, 0xE0);
+}
diff --git a/src/cbor/encoding.h b/src/cbor/encoding.h
new file mode 100644
index 000000000000..e4f2102b636a
--- /dev/null
+++ b/src/cbor/encoding.h
@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_ENCODING_H
+#define LIBCBOR_ENCODING_H
+
+#include "cbor/cbor_export.h"
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * ============================================================================
+ * Primitives encoding
+ * ============================================================================
+ */
+
+CBOR_EXPORT size_t cbor_encode_uint8(uint8_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_uint16(uint16_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_uint32(uint32_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_uint64(uint64_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_uint(uint64_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_negint8(uint8_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_negint16(uint16_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_negint32(uint32_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_negint64(uint64_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_negint(uint64_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_bytestring_start(size_t, unsigned char *,
+ size_t);
+
+CBOR_EXPORT size_t cbor_encode_indef_bytestring_start(unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_string_start(size_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_indef_string_start(unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_array_start(size_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_indef_array_start(unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_map_start(size_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_indef_map_start(unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_tag(uint64_t, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_bool(bool, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_null(unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_undef(unsigned char *, size_t);
+
+/** Encodes a half-precision float
+ *
+ * Since there is no native representation or semantics for half floats
+ * in the language, we use single-precision floats, as every value that
+ * can be expressed as a half-float can also be expressed as a float.
+ *
+ * This however means that not all floats passed to this function can be
+ * unambiguously encoded. The behavior is as follows:
+ * - Infinity, NaN are preserved
+ * - Zero is preserved
+ * - Denormalized numbers keep their sign bit and 10 most significant bit of
+ * the significand
+ * - All other numbers
+ * - If the logical value of the exponent is < -24, the output is zero
+ * - If the logical value of the exponent is between -23 and -14, the output
+ * is cut off to represent the 'magnitude' of the input, by which we
+ * mean (-1)^{signbit} x 1.0e{exponent}. The value in the significand is
+ * lost.
+ * - In all other cases, the sign bit, the exponent, and 10 most significant
+ * bits of the significand are kept
+ *
+ * @param value
+ * @param buffer Target buffer
+ * @param buffer_size Available space in the buffer
+ * @return number of bytes written
+ */
+CBOR_EXPORT size_t cbor_encode_half(float, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_single(float, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_double(double, unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_break(unsigned char *, size_t);
+
+CBOR_EXPORT size_t cbor_encode_ctrl(uint8_t, unsigned char *, size_t);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_ENCODING_H
diff --git a/src/cbor/floats_ctrls.c b/src/cbor/floats_ctrls.c
new file mode 100644
index 000000000000..b7e5fcef8530
--- /dev/null
+++ b/src/cbor/floats_ctrls.c
@@ -0,0 +1,189 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "floats_ctrls.h"
+#include <math.h>
+#include "assert.h"
+
+cbor_float_width cbor_float_get_width(const cbor_item_t *item) {
+ assert(cbor_isa_float_ctrl(item));
+ return item->metadata.float_ctrl_metadata.width;
+}
+
+uint8_t cbor_ctrl_value(const cbor_item_t *item) {
+ assert(cbor_isa_float_ctrl(item));
+ assert(cbor_float_get_width(item) == CBOR_FLOAT_0);
+ return item->metadata.float_ctrl_metadata.ctrl;
+}
+
+bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item) {
+ assert(cbor_isa_float_ctrl(item));
+ return cbor_float_get_width(item) == CBOR_FLOAT_0;
+}
+
+float cbor_float_get_float2(const cbor_item_t *item) {
+ assert(cbor_is_float(item));
+ assert(cbor_float_get_width(item) == CBOR_FLOAT_16);
+ return *(float *)item->data;
+}
+
+float cbor_float_get_float4(const cbor_item_t *item) {
+ assert(cbor_is_float(item));
+ assert(cbor_float_get_width(item) == CBOR_FLOAT_32);
+ return *(float *)item->data;
+}
+
+double cbor_float_get_float8(const cbor_item_t *item) {
+ assert(cbor_is_float(item));
+ assert(cbor_float_get_width(item) == CBOR_FLOAT_64);
+ return *(double *)item->data;
+}
+
+double cbor_float_get_float(const cbor_item_t *item) {
+ assert(cbor_is_float(item));
+ switch (cbor_float_get_width(item)) {
+ case CBOR_FLOAT_0:
+ return NAN;
+ case CBOR_FLOAT_16:
+ return cbor_float_get_float2(item);
+ case CBOR_FLOAT_32:
+ return cbor_float_get_float4(item);
+ case CBOR_FLOAT_64:
+ return cbor_float_get_float8(item);
+ }
+ return NAN; /* Compiler complaints */
+}
+
+bool cbor_get_bool(const cbor_item_t *item) {
+ assert(cbor_is_bool(item));
+ return item->metadata.float_ctrl_metadata.ctrl == CBOR_CTRL_TRUE;
+}
+
+void cbor_set_float2(cbor_item_t *item, float value) {
+ assert(cbor_is_float(item));
+ assert(cbor_float_get_width(item) == CBOR_FLOAT_16);
+ *((float *)item->data) = value;
+}
+
+void cbor_set_float4(cbor_item_t *item, float value) {
+ assert(cbor_is_float(item));
+ assert(cbor_float_get_width(item) == CBOR_FLOAT_32);
+ *((float *)item->data) = value;
+}
+
+void cbor_set_float8(cbor_item_t *item, double value) {
+ assert(cbor_is_float(item));
+ assert(cbor_float_get_width(item) == CBOR_FLOAT_64);
+ *((double *)item->data) = value;
+}
+
+void cbor_set_ctrl(cbor_item_t *item, uint8_t value) {
+ assert(cbor_isa_float_ctrl(item));
+ assert(cbor_float_get_width(item) == CBOR_FLOAT_0);
+ item->metadata.float_ctrl_metadata.ctrl = value;
+}
+
+void cbor_set_bool(cbor_item_t *item, bool value) {
+ assert(cbor_is_bool(item));
+ item->metadata.float_ctrl_metadata.ctrl =
+ value ? CBOR_CTRL_TRUE : CBOR_CTRL_FALSE;
+}
+
+cbor_item_t *cbor_new_ctrl() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
+ _CBOR_NOTNULL(item);
+
+ *item = (cbor_item_t){
+ .type = CBOR_TYPE_FLOAT_CTRL,
+ .data = NULL,
+ .refcount = 1,
+ .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_0,
+ .ctrl = CBOR_CTRL_NONE}}};
+ return item;
+}
+
+cbor_item_t *cbor_new_float2() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 4);
+ _CBOR_NOTNULL(item);
+
+ *item = (cbor_item_t){
+ .type = CBOR_TYPE_FLOAT_CTRL,
+ .data = (unsigned char *)item + sizeof(cbor_item_t),
+ .refcount = 1,
+ .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_16}}};
+ return item;
+}
+
+cbor_item_t *cbor_new_float4() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 4);
+ _CBOR_NOTNULL(item);
+
+ *item = (cbor_item_t){
+ .type = CBOR_TYPE_FLOAT_CTRL,
+ .data = (unsigned char *)item + sizeof(cbor_item_t),
+ .refcount = 1,
+ .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_32}}};
+ return item;
+}
+
+cbor_item_t *cbor_new_float8() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 8);
+ _CBOR_NOTNULL(item);
+
+ *item = (cbor_item_t){
+ .type = CBOR_TYPE_FLOAT_CTRL,
+ .data = (unsigned char *)item + sizeof(cbor_item_t),
+ .refcount = 1,
+ .metadata = {.float_ctrl_metadata = {.width = CBOR_FLOAT_64}}};
+ return item;
+}
+
+cbor_item_t *cbor_new_null() {
+ cbor_item_t *item = cbor_new_ctrl();
+ _CBOR_NOTNULL(item);
+ cbor_set_ctrl(item, CBOR_CTRL_NULL);
+ return item;
+}
+
+cbor_item_t *cbor_new_undef() {
+ cbor_item_t *item = cbor_new_ctrl();
+ _CBOR_NOTNULL(item);
+ cbor_set_ctrl(item, CBOR_CTRL_UNDEF);
+ return item;
+}
+
+cbor_item_t *cbor_build_bool(bool value) {
+ return cbor_build_ctrl(value ? CBOR_CTRL_TRUE : CBOR_CTRL_FALSE);
+}
+
+cbor_item_t *cbor_build_float2(float value) {
+ cbor_item_t *item = cbor_new_float2();
+ _CBOR_NOTNULL(item);
+ cbor_set_float2(item, value);
+ return item;
+}
+
+cbor_item_t *cbor_build_float4(float value) {
+ cbor_item_t *item = cbor_new_float4();
+ _CBOR_NOTNULL(item);
+ cbor_set_float4(item, value);
+ return item;
+}
+
+cbor_item_t *cbor_build_float8(double value) {
+ cbor_item_t *item = cbor_new_float8();
+ _CBOR_NOTNULL(item);
+ cbor_set_float8(item, value);
+ return item;
+}
+
+cbor_item_t *cbor_build_ctrl(uint8_t value) {
+ cbor_item_t *item = cbor_new_ctrl();
+ _CBOR_NOTNULL(item);
+ cbor_set_ctrl(item, value);
+ return item;
+}
diff --git a/src/cbor/floats_ctrls.h b/src/cbor/floats_ctrls.h
new file mode 100644
index 000000000000..92eb8bc3eaa3
--- /dev/null
+++ b/src/cbor/floats_ctrls.h
@@ -0,0 +1,212 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_FLOATS_CTRLS_H
+#define LIBCBOR_FLOATS_CTRLS_H
+
+#include "cbor/cbor_export.h"
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * ============================================================================
+ * Float manipulation
+ * ============================================================================
+ */
+
+/** Is this a ctrl value?
+ *
+ * @param item[borrow] A float or ctrl item
+ * @return Is this a ctrl value?
+ */
+CBOR_EXPORT bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item);
+
+/** Get the float width
+ *
+ * @param item[borrow] A float or ctrl item
+ * @return The width.
+ */
+CBOR_EXPORT cbor_float_width cbor_float_get_width(const cbor_item_t *item);
+
+/** Get a half precision float
+ *
+ * The item must have the corresponding width
+ *
+ * @param[borrow] A half precision float
+ * @return half precision value
+ */
+CBOR_EXPORT float cbor_float_get_float2(const cbor_item_t *item);
+
+/** Get a single precision float
+ *
+ * The item must have the corresponding width
+ *
+ * @param[borrow] A signle precision float
+ * @return single precision value
+ */
+CBOR_EXPORT float cbor_float_get_float4(const cbor_item_t *item);
+
+/** Get a double precision float
+ *
+ * The item must have the corresponding width
+ *
+ * @param[borrow] A double precision float
+ * @return double precision value
+ */
+CBOR_EXPORT double cbor_float_get_float8(const cbor_item_t *item);
+
+/** Get the float value represented as double
+ *
+ * Can be used regardless of the width.
+ *
+ * @param[borrow] Any float
+ * @return double precision value
+ */
+CBOR_EXPORT double cbor_float_get_float(const cbor_item_t *item);
+
+/** Get value from a boolean ctrl item
+ *
+ * @param item[borrow] A ctrl item
+ * @return boolean value
+ */
+CBOR_EXPORT bool cbor_get_bool(const cbor_item_t *item);
+
+/** Constructs a new ctrl item
+ *
+ * The width cannot be changed once the item is created
+ *
+ * @return **new** 1B ctrl or `NULL` upon memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_ctrl();
+
+/** Constructs a new float item
+ *
+ * The width cannot be changed once the item is created
+ *
+ * @return **new** 2B float or `NULL` upon memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_float2();
+
+/** Constructs a new float item
+ *
+ * The width cannot be changed once the item is created
+ *
+ * @return **new** 4B float or `NULL` upon memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_float4();
+
+/** Constructs a new float item
+ *
+ * The width cannot be changed once the item is created
+ *
+ * @return **new** 8B float or `NULL` upon memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_float8();
+
+/** Constructs new null ctrl item
+ *
+ * @return **new** null ctrl item or `NULL` upon memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_null();
+
+/** Constructs new undef ctrl item
+ *
+ * @return **new** undef ctrl item or `NULL` upon memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_undef();
+
+/** Constructs new boolean ctrl item
+ *
+ * @param value The value to use
+ * @return **new** boolen ctrl item or `NULL` upon memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_bool(bool value);
+
+/** Assign a control value
+ *
+ * \rst
+ * .. warning:: It is possible to produce an invalid CBOR value by assigning a
+ * invalid value using this mechanism. Please consult the standard before use.
+ * \endrst
+ *
+ * @param item[borrow] A ctrl item
+ * @param value The simple value to assign. Please consult the standard for
+ * allowed values
+ */
+CBOR_EXPORT void cbor_set_ctrl(cbor_item_t *item, uint8_t value);
+
+/** Assign a boolean value to a boolean ctrl item
+ *
+ * @param item[borrow] A ctrl item
+ * @param value The simple value to assign.
+ */
+CBOR_EXPORT void cbor_set_bool(cbor_item_t *item, bool value);
+
+/** Assigns a float value
+ *
+ * @param item[borrow] A half precision float
+ * @param value The value to assign
+ */
+CBOR_EXPORT void cbor_set_float2(cbor_item_t *item, float value);
+
+/** Assigns a float value
+ *
+ * @param item[borrow] A single precision float
+ * @param value The value to assign
+ */
+CBOR_EXPORT void cbor_set_float4(cbor_item_t *item, float value);
+
+/** Assigns a float value
+ *
+ * @param item[borrow] A double precision float
+ * @param value The value to assign
+ */
+CBOR_EXPORT void cbor_set_float8(cbor_item_t *item, double value);
+
+/** Reads the control value
+ *
+ * @param item[borrow] A ctrl item
+ * @return the simple value
+ */
+CBOR_EXPORT uint8_t cbor_ctrl_value(const cbor_item_t *item);
+
+/** Constructs a new float
+ *
+ * @param value the value to use
+ * @return **new** float
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_float2(float value);
+
+/** Constructs a new float
+ *
+ * @param value the value to use
+ * @return **new** float or `NULL` upon memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_float4(float value);
+
+/** Constructs a new float
+ *
+ * @param value the value to use
+ * @return **new** float or `NULL` upon memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_float8(double value);
+
+/** Constructs a ctrl item
+ *
+ * @param value the value to use
+ * @return **new** ctrl item or `NULL` upon memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_ctrl(uint8_t value);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_FLOATS_CTRLS_H
diff --git a/src/cbor/internal/builder_callbacks.c b/src/cbor/internal/builder_callbacks.c
new file mode 100644
index 000000000000..f6c571136162
--- /dev/null
+++ b/src/cbor/internal/builder_callbacks.c
@@ -0,0 +1,389 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "builder_callbacks.h"
+#include <string.h>
+#include "../arrays.h"
+#include "../bytestrings.h"
+#include "../floats_ctrls.h"
+#include "../ints.h"
+#include "../maps.h"
+#include "../strings.h"
+#include "../tags.h"
+#include "unicode.h"
+
+void _cbor_builder_append(cbor_item_t *item,
+ struct _cbor_decoder_context *ctx) {
+ if (ctx->stack->size == 0) {
+ /* Top level item */
+ ctx->root = item;
+ } else {
+ /* Part of a bigger structure */
+ switch (ctx->stack->top->item->type) {
+ case CBOR_TYPE_ARRAY: {
+ if (cbor_array_is_definite(ctx->stack->top->item)) {
+ /*
+ * We don't need an explicit check for whether the item still belongs
+ * into this array because if there are extra items, they will cause a
+ * syntax error when decoded.
+ */
+ assert(ctx->stack->top->subitems > 0);
+ cbor_array_push(ctx->stack->top->item, item);
+ ctx->stack->top->subitems--;
+ if (ctx->stack->top->subitems == 0) {
+ cbor_item_t *item = ctx->stack->top->item;
+ _cbor_stack_pop(ctx->stack);
+ _cbor_builder_append(item, ctx);
+ }
+ cbor_decref(&item);
+ } else {
+ /* Indefinite array, don't bother with subitems */
+ cbor_array_push(ctx->stack->top->item, item);
+ cbor_decref(&item);
+ }
+ break;
+ }
+ case CBOR_TYPE_MAP: {
+ /* We use 0 and 1 subitems to distinguish between keys and values in
+ * indefinite items */
+ if (ctx->stack->top->subitems % 2) {
+ /* Odd record, this is a value */
+ _cbor_map_add_value(ctx->stack->top->item, cbor_move(item));
+ } else {
+ /* Even record, this is a key */
+ _cbor_map_add_key(ctx->stack->top->item, cbor_move(item));
+ }
+ if (cbor_map_is_definite(ctx->stack->top->item)) {
+ ctx->stack->top->subitems--;
+ if (ctx->stack->top->subitems == 0) {
+ cbor_item_t *item = ctx->stack->top->item;
+ _cbor_stack_pop(ctx->stack);
+ _cbor_builder_append(item, ctx);
+ }
+ } else {
+ ctx->stack->top->subitems ^=
+ 1; /* Flip the indicator for indefinite items */
+ }
+ break;
+ }
+ case CBOR_TYPE_TAG: {
+ assert(ctx->stack->top->subitems == 1);
+ cbor_tag_set_item(ctx->stack->top->item, item);
+ cbor_decref(&item); /* Give up on our reference */
+ cbor_item_t *item = ctx->stack->top->item;
+ _cbor_stack_pop(ctx->stack);
+ _cbor_builder_append(item, ctx);
+ break;
+ }
+ default: {
+ cbor_decref(&item);
+ ctx->syntax_error = true;
+ }
+ }
+ }
+}
+
+#define CHECK_RES(ctx, res) \
+ do { \
+ if (res == NULL) { \
+ ctx->creation_failed = true; \
+ return; \
+ } \
+ } while (0)
+
+#define PUSH_CTX_STACK(ctx, res, subitems) \
+ do { \
+ if (_cbor_stack_push(ctx->stack, res, subitems) == NULL) { \
+ cbor_decref(&res); \
+ ctx->creation_failed = true; \
+ } \
+ } while (0)
+
+void cbor_builder_uint8_callback(void *context, uint8_t value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_int8();
+ CHECK_RES(ctx, res);
+ cbor_mark_uint(res);
+ cbor_set_uint8(res, value);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_uint16_callback(void *context, uint16_t value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_int16();
+ CHECK_RES(ctx, res);
+ cbor_mark_uint(res);
+ cbor_set_uint16(res, value);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_uint32_callback(void *context, uint32_t value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_int32();
+ CHECK_RES(ctx, res);
+ cbor_mark_uint(res);
+ cbor_set_uint32(res, value);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_uint64_callback(void *context, uint64_t value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_int64();
+ CHECK_RES(ctx, res);
+ cbor_mark_uint(res);
+ cbor_set_uint64(res, value);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_negint8_callback(void *context, uint8_t value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_int8();
+ CHECK_RES(ctx, res);
+ cbor_mark_negint(res);
+ cbor_set_uint8(res, value);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_negint16_callback(void *context, uint16_t value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_int16();
+ cbor_mark_negint(res);
+ cbor_set_uint16(res, value);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_negint32_callback(void *context, uint32_t value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_int32();
+ CHECK_RES(ctx, res);
+ cbor_mark_negint(res);
+ cbor_set_uint32(res, value);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_negint64_callback(void *context, uint64_t value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_int64();
+ CHECK_RES(ctx, res);
+ cbor_mark_negint(res);
+ cbor_set_uint64(res, value);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_byte_string_callback(void *context, cbor_data data,
+ size_t length) {
+ struct _cbor_decoder_context *ctx = context;
+ unsigned char *new_handle = _CBOR_MALLOC(length);
+ if (new_handle == NULL) {
+ ctx->creation_failed = true;
+ return;
+ }
+
+ memcpy(new_handle, data, length);
+ cbor_item_t *res = cbor_new_definite_bytestring();
+
+ if (res == NULL) {
+ _CBOR_FREE(new_handle);
+ ctx->creation_failed = true;
+ return;
+ }
+
+ cbor_bytestring_set_handle(res, new_handle, length);
+
+ if (ctx->stack->size > 0 && cbor_isa_bytestring(ctx->stack->top->item)) {
+ if (cbor_bytestring_is_indefinite(ctx->stack->top->item)) {
+ cbor_bytestring_add_chunk(ctx->stack->top->item, cbor_move(res));
+ } else {
+ cbor_decref(&res);
+ ctx->syntax_error = true;
+ }
+ } else {
+ _cbor_builder_append(res, ctx);
+ }
+}
+
+void cbor_builder_byte_string_start_callback(void *context) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_indefinite_bytestring();
+ CHECK_RES(ctx, res);
+ PUSH_CTX_STACK(ctx, res, 0);
+}
+
+void cbor_builder_string_callback(void *context, cbor_data data,
+ size_t length) {
+ struct _cbor_decoder_context *ctx = context;
+ struct _cbor_unicode_status unicode_status;
+
+ size_t codepoint_count =
+ _cbor_unicode_codepoint_count(data, length, &unicode_status);
+
+ if (unicode_status.status == _CBOR_UNICODE_BADCP) {
+ ctx->syntax_error = true;
+ return;
+ }
+
+ unsigned char *new_handle = _CBOR_MALLOC(length);
+
+ if (new_handle == NULL) {
+ ctx->creation_failed = true;
+ return;
+ }
+
+ memcpy(new_handle, data, length);
+ cbor_item_t *res = cbor_new_definite_string();
+ if (res == NULL) {
+ _CBOR_FREE(new_handle);
+ ctx->creation_failed = true;
+ return;
+ }
+ cbor_string_set_handle(res, new_handle, length);
+ res->metadata.string_metadata.codepoint_count = codepoint_count;
+
+ /* Careful here: order matters */
+ if (ctx->stack->size > 0 && cbor_isa_string(ctx->stack->top->item)) {
+ if (cbor_string_is_indefinite(ctx->stack->top->item)) {
+ cbor_string_add_chunk(ctx->stack->top->item, cbor_move(res));
+ } else {
+ cbor_decref(&res);
+ ctx->syntax_error = true;
+ }
+ } else {
+ _cbor_builder_append(res, ctx);
+ }
+}
+
+void cbor_builder_string_start_callback(void *context) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_indefinite_string();
+ CHECK_RES(ctx, res);
+ PUSH_CTX_STACK(ctx, res, 0);
+}
+
+void cbor_builder_array_start_callback(void *context, size_t size) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_definite_array(size);
+ CHECK_RES(ctx, res);
+ if (size > 0) {
+ PUSH_CTX_STACK(ctx, res, size);
+ } else {
+ _cbor_builder_append(res, ctx);
+ }
+}
+
+void cbor_builder_indef_array_start_callback(void *context) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_indefinite_array();
+ CHECK_RES(ctx, res);
+ PUSH_CTX_STACK(ctx, res, 0);
+}
+
+void cbor_builder_indef_map_start_callback(void *context) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_indefinite_map();
+ CHECK_RES(ctx, res);
+ PUSH_CTX_STACK(ctx, res, 0);
+}
+
+void cbor_builder_map_start_callback(void *context, size_t size) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_definite_map(size);
+ CHECK_RES(ctx, res);
+ if (size > 0) {
+ PUSH_CTX_STACK(ctx, res, size * 2);
+ } else {
+ _cbor_builder_append(res, ctx);
+ }
+}
+
+/**
+ * Is the (partially constructed) item indefinite?
+ */
+bool _cbor_is_indefinite(cbor_item_t *item) {
+ switch (item->type) {
+ case CBOR_TYPE_BYTESTRING:
+ return item->metadata.bytestring_metadata.type ==
+ _CBOR_METADATA_INDEFINITE;
+ case CBOR_TYPE_STRING:
+ return item->metadata.string_metadata.type == _CBOR_METADATA_INDEFINITE;
+ case CBOR_TYPE_ARRAY:
+ return item->metadata.array_metadata.type == _CBOR_METADATA_INDEFINITE;
+ case CBOR_TYPE_MAP:
+ return item->metadata.map_metadata.type == _CBOR_METADATA_INDEFINITE;
+ default:
+ return false;
+ }
+}
+
+void cbor_builder_indef_break_callback(void *context) {
+ struct _cbor_decoder_context *ctx = context;
+ /* There must be an item to break out of*/
+ if (ctx->stack->size > 0) {
+ cbor_item_t *item = ctx->stack->top->item;
+ if (_cbor_is_indefinite(
+ item) && /* Only indefinite items can be terminated by 0xFF */
+ /* Special case: we cannot append up if an indefinite map is incomplete
+ (we are expecting a value). */
+ (item->type != CBOR_TYPE_MAP || ctx->stack->top->subitems % 2 == 0)) {
+ _cbor_stack_pop(ctx->stack);
+ _cbor_builder_append(item, ctx);
+ return;
+ }
+ }
+
+ ctx->syntax_error = true;
+}
+
+void cbor_builder_float2_callback(void *context, float value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_float2();
+ cbor_set_float2(res, value);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_float4_callback(void *context, float value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_float4();
+ CHECK_RES(ctx, res);
+ cbor_set_float4(res, value);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_float8_callback(void *context, double value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_float8();
+ CHECK_RES(ctx, res);
+ cbor_set_float8(res, value);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_null_callback(void *context) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_null();
+ CHECK_RES(ctx, res);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_undefined_callback(void *context) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_undef();
+ CHECK_RES(ctx, res);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_boolean_callback(void *context, bool value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_build_bool(value);
+ CHECK_RES(ctx, res);
+ _cbor_builder_append(res, ctx);
+}
+
+void cbor_builder_tag_callback(void *context, uint64_t value) {
+ struct _cbor_decoder_context *ctx = context;
+ cbor_item_t *res = cbor_new_tag(value);
+ CHECK_RES(ctx, res);
+ PUSH_CTX_STACK(ctx, res, 1);
+}
diff --git a/src/cbor/internal/builder_callbacks.h b/src/cbor/internal/builder_callbacks.h
new file mode 100644
index 000000000000..a93afb1ca8f8
--- /dev/null
+++ b/src/cbor/internal/builder_callbacks.h
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_BUILDER_CALLBACKS_H
+#define LIBCBOR_BUILDER_CALLBACKS_H
+
+#include "../callbacks.h"
+#include "cbor/common.h"
+#include "stack.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** High-level decoding context */
+struct _cbor_decoder_context {
+ /** Callback creating the last item has failed */
+ bool creation_failed;
+ /** Stack expectation mismatch */
+ bool syntax_error;
+ cbor_item_t *root;
+ struct _cbor_stack *stack;
+};
+
+void cbor_builder_uint8_callback(void *, uint8_t);
+
+void cbor_builder_uint16_callback(void *, uint16_t);
+
+void cbor_builder_uint32_callback(void *, uint32_t);
+
+void cbor_builder_uint64_callback(void *, uint64_t);
+
+void cbor_builder_negint8_callback(void *, uint8_t);
+
+void cbor_builder_negint16_callback(void *, uint16_t);
+
+void cbor_builder_negint32_callback(void *, uint32_t);
+
+void cbor_builder_negint64_callback(void *, uint64_t);
+
+void cbor_builder_string_callback(void *, cbor_data, size_t);
+
+void cbor_builder_string_start_callback(void *);
+
+void cbor_builder_byte_string_callback(void *, cbor_data, size_t);
+
+void cbor_builder_byte_string_start_callback(void *);
+
+void cbor_builder_array_start_callback(void *, size_t);
+
+void cbor_builder_indef_array_start_callback(void *);
+
+void cbor_builder_map_start_callback(void *, size_t);
+
+void cbor_builder_indef_map_start_callback(void *);
+
+void cbor_builder_tag_callback(void *, uint64_t);
+
+void cbor_builder_float2_callback(void *, float);
+
+void cbor_builder_float4_callback(void *, float);
+
+void cbor_builder_float8_callback(void *, double);
+
+void cbor_builder_null_callback(void *);
+
+void cbor_builder_undefined_callback(void *);
+
+void cbor_builder_boolean_callback(void *, bool);
+
+void cbor_builder_indef_break_callback(void *);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_BUILDER_CALLBACKS_H
diff --git a/src/cbor/internal/encoders.c b/src/cbor/internal/encoders.c
new file mode 100644
index 000000000000..657e25c3987a
--- /dev/null
+++ b/src/cbor/internal/encoders.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "encoders.h"
+#include <string.h>
+
+size_t _cbor_encode_uint8(uint8_t value, unsigned char *buffer,
+ size_t buffer_size, uint8_t offset) {
+ if (value <= 23) {
+ if (buffer_size >= 1) {
+ buffer[0] = value + offset;
+ return 1;
+ }
+ } else {
+ if (buffer_size >= 2) {
+ buffer[0] = 0x18 + offset;
+ buffer[1] = value;
+ return 2;
+ }
+ }
+ return 0;
+}
+
+size_t _cbor_encode_uint16(uint16_t value, unsigned char *buffer,
+ size_t buffer_size, uint8_t offset) {
+ if (buffer_size >= 3) {
+ buffer[0] = 0x19 + offset;
+
+#ifdef IS_BIG_ENDIAN
+ memcpy(buffer + 1, &value, 2);
+#else
+ buffer[1] = value >> 8;
+ buffer[2] = value;
+#endif
+
+ return 3;
+ } else
+ return 0;
+}
+
+size_t _cbor_encode_uint32(uint32_t value, unsigned char *buffer,
+ size_t buffer_size, uint8_t offset) {
+ if (buffer_size >= 5) {
+ buffer[0] = 0x1A + offset;
+
+#ifdef IS_BIG_ENDIAN
+ memcpy(buffer + 1, &value, 4);
+#else
+ buffer[1] = value >> 24;
+ buffer[2] = value >> 16;
+ buffer[3] = value >> 8;
+ buffer[4] = value;
+#endif
+
+ return 5;
+ } else
+ return 0;
+}
+
+size_t _cbor_encode_uint64(uint64_t value, unsigned char *buffer,
+ size_t buffer_size, uint8_t offset) {
+ if (buffer_size >= 9) {
+ buffer[0] = 0x1B + offset;
+
+#ifdef IS_BIG_ENDIAN
+ memcpy(buffer + 1, &value, 8);
+#else
+ buffer[1] = value >> 56;
+ buffer[2] = value >> 48;
+ buffer[3] = value >> 40;
+ buffer[4] = value >> 32;
+ buffer[5] = value >> 24;
+ buffer[6] = value >> 16;
+ buffer[7] = value >> 8;
+ buffer[8] = value;
+#endif
+
+ return 9;
+ } else
+ return 0;
+}
+
+size_t _cbor_encode_uint(uint64_t value, unsigned char *buffer,
+ size_t buffer_size, uint8_t offset) {
+ if (value <= UINT16_MAX)
+ if (value <= UINT8_MAX)
+ return _cbor_encode_uint8((uint8_t)value, buffer, buffer_size, offset);
+ else
+ return _cbor_encode_uint16((uint16_t)value, buffer, buffer_size, offset);
+ else if (value <= UINT32_MAX)
+ return _cbor_encode_uint32((uint32_t)value, buffer, buffer_size, offset);
+ else
+ return _cbor_encode_uint64((uint64_t)value, buffer, buffer_size, offset);
+}
diff --git a/src/cbor/internal/encoders.h b/src/cbor/internal/encoders.h
new file mode 100644
index 000000000000..14ad5015cb5a
--- /dev/null
+++ b/src/cbor/internal/encoders.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_ENCODERS_H
+#define LIBCBOR_ENCODERS_H
+
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+size_t _cbor_encode_uint8(uint8_t value, unsigned char *buffer,
+ size_t buffer_size, uint8_t offset);
+
+size_t _cbor_encode_uint16(uint16_t value, unsigned char *buffer,
+ size_t buffer_size, uint8_t offset);
+
+size_t _cbor_encode_uint32(uint32_t value, unsigned char *buffer,
+ size_t buffer_size, uint8_t offset);
+
+size_t _cbor_encode_uint64(uint64_t value, unsigned char *buffer,
+ size_t buffer_size, uint8_t offset);
+
+size_t _cbor_encode_uint(uint64_t value, unsigned char *buffer,
+ size_t buffer_size, uint8_t offset);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_ENCODERS_H
diff --git a/src/cbor/internal/loaders.c b/src/cbor/internal/loaders.c
new file mode 100644
index 000000000000..af00f135527f
--- /dev/null
+++ b/src/cbor/internal/loaders.c
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "loaders.h"
+#include <math.h>
+#include <string.h>
+
+uint8_t _cbor_load_uint8(cbor_data source) { return (uint8_t)*source; }
+
+uint16_t _cbor_load_uint16(const unsigned char *source) {
+#ifdef IS_BIG_ENDIAN
+ uint16_t result;
+ memcpy(&result, source, 2);
+ return result;
+#else
+ return ((uint16_t) * (source + 0) << 8) + (uint8_t) * (source + 1);
+#endif
+}
+
+uint32_t _cbor_load_uint32(const unsigned char *source) {
+#ifdef IS_BIG_ENDIAN
+ uint32_t result;
+ memcpy(&result, source, 4);
+ return result;
+#else
+ return ((uint32_t) * (source + 0) << 0x18) +
+ ((uint32_t) * (source + 1) << 0x10) +
+ ((uint16_t) * (source + 2) << 0x08) + (uint8_t) * (source + 3);
+#endif
+}
+
+uint64_t _cbor_load_uint64(const unsigned char *source) {
+#ifdef IS_BIG_ENDIAN
+ uint64_t result;
+ memcpy(&result, source, 8);
+ return result;
+#else
+ return ((uint64_t) * (source + 0) << 0x38) +
+ ((uint64_t) * (source + 1) << 0x30) +
+ ((uint64_t) * (source + 2) << 0x28) +
+ ((uint64_t) * (source + 3) << 0x20) +
+ ((uint32_t) * (source + 4) << 0x18) +
+ ((uint32_t) * (source + 5) << 0x10) +
+ ((uint16_t) * (source + 6) << 0x08) + (uint8_t) * (source + 7);
+#endif
+}
+
+/* As per http://tools.ietf.org/html/rfc7049#appendix-D */
+float _cbor_decode_half(unsigned char *halfp) {
+ int half = (halfp[0] << 8) + halfp[1];
+ int exp = (half >> 10) & 0x1f;
+ int mant = half & 0x3ff;
+ double val;
+ if (exp == 0)
+ val = ldexp(mant, -24);
+ else if (exp != 31)
+ val = ldexp(mant + 1024, exp - 25);
+ else
+ val = mant == 0 ? INFINITY : NAN;
+ return (float)(half & 0x8000 ? -val : val);
+}
+
+double _cbor_load_half(cbor_data source) {
+ /* Discard const */
+ return _cbor_decode_half((unsigned char *)source);
+}
+
+float _cbor_load_float(cbor_data source) {
+ union _cbor_float_helper helper = {.as_uint = _cbor_load_uint32(source)};
+ return helper.as_float;
+}
+
+double _cbor_load_double(cbor_data source) {
+ union _cbor_double_helper helper = {.as_uint = _cbor_load_uint64(source)};
+ return helper.as_double;
+}
diff --git a/src/cbor/internal/loaders.h b/src/cbor/internal/loaders.h
new file mode 100644
index 000000000000..a4c82b209f55
--- /dev/null
+++ b/src/cbor/internal/loaders.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_LOADERS_H
+#define LIBCBOR_LOADERS_H
+
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Read the given uint from the given location, no questions asked */
+uint8_t _cbor_load_uint8(const unsigned char *source);
+
+uint16_t _cbor_load_uint16(const unsigned char *source);
+
+uint32_t _cbor_load_uint32(const unsigned char *source);
+
+uint64_t _cbor_load_uint64(const unsigned char *source);
+
+double _cbor_load_half(cbor_data source);
+
+float _cbor_load_float(cbor_data source);
+
+double _cbor_load_double(cbor_data source);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_LOADERS_H
diff --git a/src/cbor/internal/memory_utils.c b/src/cbor/internal/memory_utils.c
new file mode 100644
index 000000000000..918b708e3313
--- /dev/null
+++ b/src/cbor/internal/memory_utils.c
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "memory_utils.h"
+#include "cbor/common.h"
+
+// TODO: Consider builtins
+// (https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html)
+
+/** Highest on bit position */
+size_t _cbor_highest_bit(size_t number) {
+ size_t bit = 0;
+ while (number != 0) {
+ bit++;
+ number >>= 1;
+ }
+
+ return bit;
+}
+
+bool _cbor_safe_to_multiply(size_t a, size_t b) {
+ return _cbor_highest_bit(a) + _cbor_highest_bit(b) <= sizeof(size_t) * 8;
+}
+
+void* _cbor_alloc_multiple(size_t item_size, size_t item_count) {
+ if (_cbor_safe_to_multiply(item_size, item_count)) {
+ return _CBOR_MALLOC(item_size * item_count);
+ } else {
+ return NULL;
+ }
+}
+
+void* _cbor_realloc_multiple(void* pointer, size_t item_size,
+ size_t item_count) {
+ if (_cbor_safe_to_multiply(item_size, item_count)) {
+ return _CBOR_REALLOC(pointer, item_size * item_count);
+ } else {
+ return NULL;
+ }
+}
diff --git a/src/cbor/internal/memory_utils.h b/src/cbor/internal/memory_utils.h
new file mode 100644
index 000000000000..c41ace67948f
--- /dev/null
+++ b/src/cbor/internal/memory_utils.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_MEMORY_UTILS_H
+#define LIBCBOR_MEMORY_UTILS_H
+
+#include <stdbool.h>
+#include <string.h>
+
+/** Can a and b be multiplied without overflowing size_t? */
+bool _cbor_safe_to_multiply(size_t a, size_t b);
+
+/** Overflow-proof contiguous array allocation
+ *
+ * @param item_size
+ * @param item_count
+ * @return Region of item_size * item_count bytes, or NULL if the total size
+ * overflows size_t or the underlying allocator failed
+ */
+void* _cbor_alloc_multiple(size_t item_size, size_t item_count);
+
+/** Overflow-proof contiguous array reallocation
+ *
+ * This implements the OpenBSD `reallocarray` functionality.
+ *
+ * @param pointer
+ * @param item_size
+ * @param item_count
+ * @return Realloc'd of item_size * item_count bytes, or NULL if the total size
+ * overflows size_t or the underlying allocator failed
+ */
+void* _cbor_realloc_multiple(void* pointer, size_t item_size,
+ size_t item_count);
+
+#endif // LIBCBOR_MEMORY_UTILS_H
diff --git a/src/cbor/internal/stack.c b/src/cbor/internal/stack.c
new file mode 100644
index 000000000000..79c9e5e5297e
--- /dev/null
+++ b/src/cbor/internal/stack.c
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "stack.h"
+
+struct _cbor_stack _cbor_stack_init() {
+ return (struct _cbor_stack){.top = NULL, .size = 0};
+}
+
+void _cbor_stack_pop(struct _cbor_stack *stack) {
+ struct _cbor_stack_record *top = stack->top;
+ stack->top = stack->top->lower;
+ _CBOR_FREE(top);
+ stack->size--;
+}
+
+struct _cbor_stack_record *_cbor_stack_push(struct _cbor_stack *stack,
+ cbor_item_t *item,
+ size_t subitems) {
+ if (stack->size == CBOR_MAX_STACK_SIZE) return NULL;
+ struct _cbor_stack_record *new_top =
+ _CBOR_MALLOC(sizeof(struct _cbor_stack_record));
+ if (new_top == NULL) return NULL;
+
+ *new_top = (struct _cbor_stack_record){stack->top, item, subitems};
+ stack->top = new_top;
+ stack->size++;
+ return new_top;
+}
diff --git a/src/cbor/internal/stack.h b/src/cbor/internal/stack.h
new file mode 100644
index 000000000000..42ed04429c7e
--- /dev/null
+++ b/src/cbor/internal/stack.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_STACK_H
+#define LIBCBOR_STACK_H
+
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Simple stack record for the parser */
+struct _cbor_stack_record {
+ struct _cbor_stack_record *lower;
+ cbor_item_t *item;
+ size_t subitems;
+};
+
+/** Stack handle - contents and size */
+struct _cbor_stack {
+ struct _cbor_stack_record *top;
+ size_t size;
+};
+
+struct _cbor_stack _cbor_stack_init();
+
+void _cbor_stack_pop(struct _cbor_stack *);
+
+struct _cbor_stack_record *_cbor_stack_push(struct _cbor_stack *, cbor_item_t *,
+ size_t);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_STACK_H
diff --git a/src/cbor/internal/unicode.c b/src/cbor/internal/unicode.c
new file mode 100644
index 000000000000..98b49728989e
--- /dev/null
+++ b/src/cbor/internal/unicode.c
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "unicode.h"
+
+#define UTF8_ACCEPT 0
+#define UTF8_REJECT 1
+
+static const uint8_t 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 */
+};
+
+/* Copyright of this function: (c) 2008-2009 Bjoern Hoehrmann
+ * <bjoern@hoehrmann.de> */
+/* See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. */
+uint32_t _cbor_unicode_decode(uint32_t* state, uint32_t* codep, uint32_t byte) {
+ uint32_t type = utf8d[byte];
+
+ *codep = (*state != UTF8_ACCEPT) ? (byte & 0x3fu) | (*codep << 6)
+ : (0xff >> type) & (byte);
+
+ *state = utf8d[256 + *state * 16 + type];
+ return *state;
+}
+
+size_t _cbor_unicode_codepoint_count(cbor_data source, size_t source_length,
+ struct _cbor_unicode_status* status) {
+ *status =
+ (struct _cbor_unicode_status){.location = 0, .status = _CBOR_UNICODE_OK};
+ uint32_t codepoint, state = UTF8_ACCEPT, res;
+ size_t pos = 0, count = 0;
+
+ for (; pos < source_length; pos++) {
+ res = _cbor_unicode_decode(&state, &codepoint, source[pos]);
+
+ if (res == UTF8_ACCEPT) {
+ count++;
+ } else if (res == UTF8_REJECT) {
+ goto error;
+ }
+ }
+
+ /* Unfinished multibyte codepoint */
+ if (state != UTF8_ACCEPT) goto error;
+
+ return count;
+
+error:
+ *status = (struct _cbor_unicode_status){.location = pos,
+ .status = _CBOR_UNICODE_BADCP};
+ return -1;
+}
diff --git a/src/cbor/internal/unicode.h b/src/cbor/internal/unicode.h
new file mode 100644
index 000000000000..5f6456029306
--- /dev/null
+++ b/src/cbor/internal/unicode.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_UNICODE_H
+#define LIBCBOR_UNICODE_H
+
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum _cbor_unicode_status_error { _CBOR_UNICODE_OK, _CBOR_UNICODE_BADCP };
+
+/** Signals unicode validation error and possibly its location */
+struct _cbor_unicode_status {
+ enum _cbor_unicode_status_error status;
+ size_t location;
+};
+
+size_t _cbor_unicode_codepoint_count(cbor_data source, size_t source_length,
+ struct _cbor_unicode_status* status);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_UNICODE_H
diff --git a/src/cbor/ints.c b/src/cbor/ints.c
new file mode 100644
index 000000000000..880982e5a3e5
--- /dev/null
+++ b/src/cbor/ints.c
@@ -0,0 +1,191 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "ints.h"
+
+cbor_int_width cbor_int_get_width(const cbor_item_t *item) {
+ assert(cbor_is_int(item));
+ return item->metadata.int_metadata.width;
+}
+
+uint8_t cbor_get_uint8(const cbor_item_t *item) {
+ assert(cbor_is_int(item));
+ assert(cbor_int_get_width(item) == CBOR_INT_8);
+ return *item->data;
+}
+
+uint16_t cbor_get_uint16(const cbor_item_t *item) {
+ assert(cbor_is_int(item));
+ assert(cbor_int_get_width(item) == CBOR_INT_16);
+ return *(uint16_t *)item->data;
+}
+
+uint32_t cbor_get_uint32(const cbor_item_t *item) {
+ assert(cbor_is_int(item));
+ assert(cbor_int_get_width(item) == CBOR_INT_32);
+ return *(uint32_t *)item->data;
+}
+
+uint64_t cbor_get_uint64(const cbor_item_t *item) {
+ assert(cbor_is_int(item));
+ assert(cbor_int_get_width(item) == CBOR_INT_64);
+ return *(uint64_t *)item->data;
+}
+
+uint64_t cbor_get_int(const cbor_item_t *item) {
+ assert(cbor_is_int(item));
+ switch (cbor_int_get_width(item)) {
+ case CBOR_INT_8:
+ return cbor_get_uint8(item);
+ case CBOR_INT_16:
+ return cbor_get_uint16(item);
+ case CBOR_INT_32:
+ return cbor_get_uint32(item);
+ case CBOR_INT_64:
+ return cbor_get_uint64(item);
+ }
+ // TODO: This should be handled in a default branch
+ return 0xDEADBEEF; /* Compiler complaints */
+}
+
+void cbor_set_uint8(cbor_item_t *item, uint8_t value) {
+ assert(cbor_is_int(item));
+ assert(cbor_int_get_width(item) == CBOR_INT_8);
+ *item->data = value;
+}
+
+void cbor_set_uint16(cbor_item_t *item, uint16_t value) {
+ assert(cbor_is_int(item));
+ assert(cbor_int_get_width(item) == CBOR_INT_16);
+ *(uint16_t *)item->data = value;
+}
+
+void cbor_set_uint32(cbor_item_t *item, uint32_t value) {
+ assert(cbor_is_int(item));
+ assert(cbor_int_get_width(item) == CBOR_INT_32);
+ *(uint32_t *)item->data = value;
+}
+
+void cbor_set_uint64(cbor_item_t *item, uint64_t value) {
+ assert(cbor_is_int(item));
+ assert(cbor_int_get_width(item) == CBOR_INT_64);
+ *(uint64_t *)item->data = value;
+}
+
+void cbor_mark_uint(cbor_item_t *item) {
+ assert(cbor_is_int(item));
+ item->type = CBOR_TYPE_UINT;
+}
+
+void cbor_mark_negint(cbor_item_t *item) {
+ assert(cbor_is_int(item));
+ item->type = CBOR_TYPE_NEGINT;
+}
+
+cbor_item_t *cbor_new_int8() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 1);
+ _CBOR_NOTNULL(item);
+ *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
+ .refcount = 1,
+ .metadata = {.int_metadata = {.width = CBOR_INT_8}},
+ .type = CBOR_TYPE_UINT};
+ return item;
+}
+
+cbor_item_t *cbor_new_int16() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 2);
+ _CBOR_NOTNULL(item);
+ *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
+ .refcount = 1,
+ .metadata = {.int_metadata = {.width = CBOR_INT_16}},
+ .type = CBOR_TYPE_UINT};
+ return item;
+}
+
+cbor_item_t *cbor_new_int32() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 4);
+ _CBOR_NOTNULL(item);
+ *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
+ .refcount = 1,
+ .metadata = {.int_metadata = {.width = CBOR_INT_32}},
+ .type = CBOR_TYPE_UINT};
+ return item;
+}
+
+cbor_item_t *cbor_new_int64() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t) + 8);
+ _CBOR_NOTNULL(item);
+ *item = (cbor_item_t){.data = (unsigned char *)item + sizeof(cbor_item_t),
+ .refcount = 1,
+ .metadata = {.int_metadata = {.width = CBOR_INT_64}},
+ .type = CBOR_TYPE_UINT};
+ return item;
+}
+
+cbor_item_t *cbor_build_uint8(uint8_t value) {
+ cbor_item_t *item = cbor_new_int8();
+ _CBOR_NOTNULL(item);
+ cbor_set_uint8(item, value);
+ cbor_mark_uint(item);
+ return item;
+}
+
+cbor_item_t *cbor_build_uint16(uint16_t value) {
+ cbor_item_t *item = cbor_new_int16();
+ _CBOR_NOTNULL(item);
+ cbor_set_uint16(item, value);
+ cbor_mark_uint(item);
+ return item;
+}
+
+cbor_item_t *cbor_build_uint32(uint32_t value) {
+ cbor_item_t *item = cbor_new_int32();
+ _CBOR_NOTNULL(item);
+ cbor_set_uint32(item, value);
+ cbor_mark_uint(item);
+ return item;
+}
+
+cbor_item_t *cbor_build_uint64(uint64_t value) {
+ cbor_item_t *item = cbor_new_int64();
+ _CBOR_NOTNULL(item);
+ cbor_set_uint64(item, value);
+ cbor_mark_uint(item);
+ return item;
+}
+
+cbor_item_t *cbor_build_negint8(uint8_t value) {
+ cbor_item_t *item = cbor_new_int8();
+ _CBOR_NOTNULL(item);
+ cbor_set_uint8(item, value);
+ cbor_mark_negint(item);
+ return item;
+}
+
+cbor_item_t *cbor_build_negint16(uint16_t value) {
+ cbor_item_t *item = cbor_new_int16();
+ _CBOR_NOTNULL(item);
+ cbor_set_uint16(item, value);
+ cbor_mark_negint(item);
+ return item;
+}
+
+cbor_item_t *cbor_build_negint32(uint32_t value) {
+ cbor_item_t *item = cbor_new_int32();
+ _CBOR_NOTNULL(item);
+ cbor_set_uint32(item, value);
+ cbor_mark_negint(item);
+ return item;
+}
+
+cbor_item_t *cbor_build_negint64(uint64_t value) {
+ cbor_item_t *item = cbor_new_int64();
+ _CBOR_NOTNULL(item);
+ cbor_set_uint64(item, value);
+ cbor_mark_negint(item);
+ return item;
+}
diff --git a/src/cbor/ints.h b/src/cbor/ints.h
new file mode 100644
index 000000000000..fc7de600102e
--- /dev/null
+++ b/src/cbor/ints.h
@@ -0,0 +1,210 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_INTS_H
+#define LIBCBOR_INTS_H
+
+#include "cbor/cbor_export.h"
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * ============================================================================
+ * Integer (uints and negints) manipulation
+ * ============================================================================
+ */
+
+/** Extracts the integer value
+ *
+ * @param item[borrow] positive or negative integer
+ * @return the value
+ */
+CBOR_EXPORT uint8_t cbor_get_uint8(const cbor_item_t *item);
+
+/** Extracts the integer value
+ *
+ * @param item[borrow] positive or negative integer
+ * @return the value
+ */
+CBOR_EXPORT uint16_t cbor_get_uint16(const cbor_item_t *item);
+
+/** Extracts the integer value
+ *
+ * @param item[borrow] positive or negative integer
+ * @return the value
+ */
+CBOR_EXPORT uint32_t cbor_get_uint32(const cbor_item_t *item);
+
+/** Extracts the integer value
+ *
+ * @param item[borrow] positive or negative integer
+ * @return the value
+ */
+CBOR_EXPORT uint64_t cbor_get_uint64(const cbor_item_t *item);
+
+/** Extracts the integer value
+ *
+ * @param item[borrow] positive or negative integer
+ * @return the value, extended to `uint64_t`
+ */
+CBOR_EXPORT uint64_t cbor_get_int(const cbor_item_t *item);
+
+/** Assigns the integer value
+ *
+ * @param item[borrow] positive or negative integer item
+ * @param value the value to assign. For negative integer, the logical value is
+ * `-value - 1`
+ */
+CBOR_EXPORT void cbor_set_uint8(cbor_item_t *item, uint8_t value);
+
+/** Assigns the integer value
+ *
+ * @param item[borrow] positive or negative integer item
+ * @param value the value to assign. For negative integer, the logical value is
+ * `-value - 1`
+ */
+CBOR_EXPORT void cbor_set_uint16(cbor_item_t *item, uint16_t value);
+
+/** Assigns the integer value
+ *
+ * @param item[borrow] positive or negative integer item
+ * @param value the value to assign. For negative integer, the logical value is
+ * `-value - 1`
+ */
+CBOR_EXPORT void cbor_set_uint32(cbor_item_t *item, uint32_t value);
+
+/** Assigns the integer value
+ *
+ * @param item[borrow] positive or negative integer item
+ * @param value the value to assign. For negative integer, the logical value is
+ * `-value - 1`
+ */
+CBOR_EXPORT void cbor_set_uint64(cbor_item_t *item, uint64_t value);
+
+/** Queries the integer width
+ *
+ * @param item[borrow] positive or negative integer item
+ * @return the width
+ */
+CBOR_EXPORT cbor_int_width cbor_int_get_width(const cbor_item_t *item);
+
+/** Marks the integer item as a positive integer
+ *
+ * The data value is not changed
+ *
+ * @param item[borrow] positive or negative integer item
+ */
+CBOR_EXPORT void cbor_mark_uint(cbor_item_t *item);
+
+/** Marks the integer item as a negative integer
+ *
+ * The data value is not changed
+ *
+ * @param item[borrow] positive or negative integer item
+ */
+CBOR_EXPORT void cbor_mark_negint(cbor_item_t *item);
+
+/** Allocates new integer with 1B width
+ *
+ * The width cannot be changed once allocated
+ *
+ * @return **new** positive integer or `NULL` on memory allocation failure. The
+ * value is not initialized
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_int8();
+
+/** Allocates new integer with 2B width
+ *
+ * The width cannot be changed once allocated
+ *
+ * @return **new** positive integer or `NULL` on memory allocation failure. The
+ * value is not initialized
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_int16();
+
+/** Allocates new integer with 4B width
+ *
+ * The width cannot be changed once allocated
+ *
+ * @return **new** positive integer or `NULL` on memory allocation failure. The
+ * value is not initialized
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_int32();
+
+/** Allocates new integer with 8B width
+ *
+ * The width cannot be changed once allocated
+ *
+ * @return **new** positive integer or `NULL` on memory allocation failure. The
+ * value is not initialized
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_int64();
+
+/** Constructs a new positive integer
+ *
+ * @param value the value to use
+ * @return **new** positive integer or `NULL` on memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_uint8(uint8_t value);
+
+/** Constructs a new positive integer
+ *
+ * @param value the value to use
+ * @return **new** positive integer or `NULL` on memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_uint16(uint16_t value);
+
+/** Constructs a new positive integer
+ *
+ * @param value the value to use
+ * @return **new** positive integer or `NULL` on memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_uint32(uint32_t value);
+
+/** Constructs a new positive integer
+ *
+ * @param value the value to use
+ * @return **new** positive integer or `NULL` on memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_uint64(uint64_t value);
+
+/** Constructs a new negative integer
+ *
+ * @param value the value to use
+ * @return **new** negative integer or `NULL` on memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_negint8(uint8_t value);
+
+/** Constructs a new negative integer
+ *
+ * @param value the value to use
+ * @return **new** negative integer or `NULL` on memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_negint16(uint16_t value);
+
+/** Constructs a new negative integer
+ *
+ * @param value the value to use
+ * @return **new** negative integer or `NULL` on memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_negint32(uint32_t value);
+
+/** Constructs a new negative integer
+ *
+ * @param value the value to use
+ * @return **new** negative integer or `NULL` on memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_negint64(uint64_t value);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_INTS_H
diff --git a/src/cbor/maps.c b/src/cbor/maps.c
new file mode 100644
index 000000000000..45140e2cfa2b
--- /dev/null
+++ b/src/cbor/maps.c
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "maps.h"
+#include "internal/memory_utils.h"
+
+size_t cbor_map_size(const cbor_item_t *item) {
+ assert(cbor_isa_map(item));
+ return item->metadata.map_metadata.end_ptr;
+}
+
+size_t cbor_map_allocated(const cbor_item_t *item) {
+ assert(cbor_isa_map(item));
+ return item->metadata.map_metadata.allocated;
+}
+
+cbor_item_t *cbor_new_definite_map(size_t size) {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
+ _CBOR_NOTNULL(item);
+
+ *item = (cbor_item_t){
+ .refcount = 1,
+ .type = CBOR_TYPE_MAP,
+ .metadata = {.map_metadata = {.allocated = size,
+ .type = _CBOR_METADATA_DEFINITE,
+ .end_ptr = 0}},
+ .data = _cbor_alloc_multiple(sizeof(struct cbor_pair), size)};
+ _CBOR_DEPENDENT_NOTNULL(item, item->data);
+
+ return item;
+}
+
+cbor_item_t *cbor_new_indefinite_map() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
+ _CBOR_NOTNULL(item);
+
+ *item = (cbor_item_t){
+ .refcount = 1,
+ .type = CBOR_TYPE_MAP,
+ .metadata = {.map_metadata = {.allocated = 0,
+ .type = _CBOR_METADATA_INDEFINITE,
+ .end_ptr = 0}},
+ .data = NULL};
+
+ return item;
+}
+
+bool _cbor_map_add_key(cbor_item_t *item, cbor_item_t *key) {
+ assert(cbor_isa_map(item));
+ struct _cbor_map_metadata *metadata =
+ (struct _cbor_map_metadata *)&item->metadata;
+ if (cbor_map_is_definite(item)) {
+ struct cbor_pair *data = cbor_map_handle(item);
+ if (metadata->end_ptr >= metadata->allocated) {
+ /* Don't realloc definite preallocated map */
+ return false;
+ }
+
+ data[metadata->end_ptr].key = key;
+ data[metadata->end_ptr++].value = NULL;
+ } else {
+ if (metadata->end_ptr >= metadata->allocated) {
+ /* Exponential realloc */
+ // Check for overflows first
+ // TODO: Explicitly test this
+ if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, metadata->allocated)) {
+ return false;
+ }
+
+ size_t new_allocation = metadata->allocated == 0
+ ? 1
+ : CBOR_BUFFER_GROWTH * metadata->allocated;
+
+ unsigned char *new_data = _cbor_realloc_multiple(
+ item->data, sizeof(struct cbor_pair), new_allocation);
+
+ if (new_data == NULL) {
+ return false;
+ }
+
+ item->data = new_data;
+ metadata->allocated = new_allocation;
+ }
+ struct cbor_pair *data = cbor_map_handle(item);
+ data[metadata->end_ptr].key = key;
+ data[metadata->end_ptr++].value = NULL;
+ }
+ cbor_incref(key);
+ return true;
+}
+
+bool _cbor_map_add_value(cbor_item_t *item, cbor_item_t *value) {
+ assert(cbor_isa_map(item));
+ cbor_incref(value);
+ cbor_map_handle(item)[
+ /* Move one back since we are assuming _add_key (which increased the ptr)
+ * was the previous operation on this object */
+ item->metadata.map_metadata.end_ptr - 1]
+ .value = value;
+ return true;
+}
+
+bool cbor_map_add(cbor_item_t *item, struct cbor_pair pair) {
+ assert(cbor_isa_map(item));
+ if (!_cbor_map_add_key(item, pair.key)) return false;
+ return _cbor_map_add_value(item, pair.value);
+}
+
+bool cbor_map_is_definite(const cbor_item_t *item) {
+ assert(cbor_isa_map(item));
+ return item->metadata.map_metadata.type == _CBOR_METADATA_DEFINITE;
+}
+
+bool cbor_map_is_indefinite(const cbor_item_t *item) {
+ return !cbor_map_is_definite(item);
+}
+
+struct cbor_pair *cbor_map_handle(const cbor_item_t *item) {
+ assert(cbor_isa_map(item));
+ return (struct cbor_pair *)item->data;
+}
diff --git a/src/cbor/maps.h b/src/cbor/maps.h
new file mode 100644
index 000000000000..370c6fcd4336
--- /dev/null
+++ b/src/cbor/maps.h
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_MAPS_H
+#define LIBCBOR_MAPS_H
+
+#include "cbor/cbor_export.h"
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * ============================================================================
+ * Map manipulation
+ * ============================================================================
+ */
+
+/** Get the number of pairs
+ *
+ * @param item[borrow] A map
+ * @return The number of pairs
+ */
+CBOR_EXPORT size_t cbor_map_size(const cbor_item_t *item);
+
+/** Get the size of the allocated storage
+ *
+ * @param item[borrow] A map
+ * @return Allocated storage size (as the number of #cbor_pair items)
+ */
+CBOR_EXPORT size_t cbor_map_allocated(const cbor_item_t *item);
+
+/** Create a new definite map
+ *
+ * @param size The number of slots to preallocate
+ * @return **new** definite map. `NULL` on malloc failure.
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_definite_map(size_t size);
+
+/** Create a new indefinite map
+ *
+ * @param size The number of slots to preallocate
+ * @return **new** definite map. `NULL` on malloc failure.
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_indefinite_map();
+
+/** Add a pair to the map
+ *
+ * For definite maps, items can only be added to the preallocated space. For
+ * indefinite maps, the storage will be expanded as needed
+ *
+ * @param item[borrow] A map
+ * @param pair[incref] The key-value pair to add (incref is member-wise)
+ * @return `true` on success, `false` if either reallocation failed or the
+ * preallcoated storage is full
+ */
+CBOR_EXPORT bool cbor_map_add(cbor_item_t *item, struct cbor_pair pair);
+
+/** Add a key to the map
+ *
+ * Sets the value to `NULL`. Internal API.
+ *
+ * @param item[borrow] A map
+ * @param key[incref] The key
+ * @return `true` on success, `false` if either reallocation failed or the
+ * preallcoated storage is full
+ */
+CBOR_EXPORT bool _cbor_map_add_key(cbor_item_t *item, cbor_item_t *key);
+
+/** Add a value to the map
+ *
+ * Assumes that #_cbor_map_add_key has been called. Internal API.
+ *
+ * @param item[borrow] A map
+ * @param key[incref] The value
+ * @return `true` on success, `false` if either reallocation failed or the
+ * preallcoated storage is full
+ */
+CBOR_EXPORT bool _cbor_map_add_value(cbor_item_t *item, cbor_item_t *value);
+
+/** Is this map definite?
+ *
+ * @param item[borrow] A map
+ * @return Is this map definite?
+ */
+CBOR_EXPORT bool cbor_map_is_definite(const cbor_item_t *item);
+
+/** Is this map indefinite?
+ *
+ * @param item[borrow] A map
+ * @return Is this map indefinite?
+ */
+CBOR_EXPORT bool cbor_map_is_indefinite(const cbor_item_t *item);
+
+/** Get the pairs storage
+ *
+ * @param item[borrow] A map
+ * @return Array of #cbor_map_size pairs. Manipulation is possible as long as
+ * references remain valid.
+ */
+CBOR_EXPORT struct cbor_pair *cbor_map_handle(const cbor_item_t *item);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_MAPS_H
diff --git a/src/cbor/serialization.c b/src/cbor/serialization.c
new file mode 100644
index 000000000000..56a48176aab4
--- /dev/null
+++ b/src/cbor/serialization.c
@@ -0,0 +1,286 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "serialization.h"
+#include <string.h>
+#include "cbor/arrays.h"
+#include "cbor/bytestrings.h"
+#include "cbor/floats_ctrls.h"
+#include "cbor/ints.h"
+#include "cbor/maps.h"
+#include "cbor/strings.h"
+#include "cbor/tags.h"
+#include "encoding.h"
+#include "internal/memory_utils.h"
+
+size_t cbor_serialize(const cbor_item_t *item, unsigned char *buffer,
+ size_t buffer_size) {
+ switch (cbor_typeof(item)) {
+ case CBOR_TYPE_UINT:
+ return cbor_serialize_uint(item, buffer, buffer_size);
+ case CBOR_TYPE_NEGINT:
+ return cbor_serialize_negint(item, buffer, buffer_size);
+ case CBOR_TYPE_BYTESTRING:
+ return cbor_serialize_bytestring(item, buffer, buffer_size);
+ case CBOR_TYPE_STRING:
+ return cbor_serialize_string(item, buffer, buffer_size);
+ case CBOR_TYPE_ARRAY:
+ return cbor_serialize_array(item, buffer, buffer_size);
+ case CBOR_TYPE_MAP:
+ return cbor_serialize_map(item, buffer, buffer_size);
+ case CBOR_TYPE_TAG:
+ return cbor_serialize_tag(item, buffer, buffer_size);
+ case CBOR_TYPE_FLOAT_CTRL:
+ return cbor_serialize_float_ctrl(item, buffer, buffer_size);
+ default:
+ return 0;
+ }
+}
+
+size_t cbor_serialize_alloc(const cbor_item_t *item, unsigned char **buffer,
+ size_t *buffer_size) {
+ size_t bfr_size = 32;
+ unsigned char *bfr = _CBOR_MALLOC(bfr_size), *tmp_bfr;
+ if (bfr == NULL) {
+ return 0;
+ }
+
+ size_t written;
+
+ /* This is waaay too optimistic - figure out something smarter (eventually) */
+ while ((written = cbor_serialize(item, bfr, bfr_size)) == 0) {
+ if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, bfr_size)) {
+ _CBOR_FREE(bfr);
+ return 0;
+ }
+
+ tmp_bfr = _CBOR_REALLOC(bfr, bfr_size *= 2);
+
+ if (tmp_bfr == NULL) {
+ _CBOR_FREE(bfr);
+ return 0;
+ }
+ bfr = tmp_bfr;
+ }
+ *buffer = bfr;
+ *buffer_size = bfr_size;
+ return written;
+}
+
+size_t cbor_serialize_uint(const cbor_item_t *item, unsigned char *buffer,
+ size_t buffer_size) {
+ assert(cbor_isa_uint(item));
+ switch (cbor_int_get_width(item)) {
+ case CBOR_INT_8:
+ return cbor_encode_uint8(cbor_get_uint8(item), buffer, buffer_size);
+ case CBOR_INT_16:
+ return cbor_encode_uint16(cbor_get_uint16(item), buffer, buffer_size);
+ case CBOR_INT_32:
+ return cbor_encode_uint32(cbor_get_uint32(item), buffer, buffer_size);
+ case CBOR_INT_64:
+ return cbor_encode_uint64(cbor_get_uint64(item), buffer, buffer_size);
+ default:
+ return 0;
+ }
+}
+
+size_t cbor_serialize_negint(const cbor_item_t *item, unsigned char *buffer,
+ size_t buffer_size) {
+ assert(cbor_isa_negint(item));
+ switch (cbor_int_get_width(item)) {
+ case CBOR_INT_8:
+ return cbor_encode_negint8(cbor_get_uint8(item), buffer, buffer_size);
+ case CBOR_INT_16:
+ return cbor_encode_negint16(cbor_get_uint16(item), buffer, buffer_size);
+ case CBOR_INT_32:
+ return cbor_encode_negint32(cbor_get_uint32(item), buffer, buffer_size);
+ case CBOR_INT_64:
+ return cbor_encode_negint64(cbor_get_uint64(item), buffer, buffer_size);
+ default:
+ return 0;
+ }
+}
+
+size_t cbor_serialize_bytestring(const cbor_item_t *item, unsigned char *buffer,
+ size_t buffer_size) {
+ assert(cbor_isa_bytestring(item));
+ if (cbor_bytestring_is_definite(item)) {
+ size_t length = cbor_bytestring_length(item);
+ size_t written = cbor_encode_bytestring_start(length, buffer, buffer_size);
+ if (written && (buffer_size - written >= length)) {
+ memcpy(buffer + written, cbor_bytestring_handle(item), length);
+ return written + length;
+ } else
+ return 0;
+ } else {
+ assert(cbor_bytestring_is_indefinite(item));
+ size_t chunk_count = cbor_bytestring_chunk_count(item);
+ size_t written = cbor_encode_indef_bytestring_start(buffer, buffer_size);
+
+ if (written == 0) return 0;
+
+ cbor_item_t **chunks = cbor_bytestring_chunks_handle(item);
+ for (size_t i = 0; i < chunk_count; i++) {
+ size_t chunk_written = cbor_serialize_bytestring(
+ chunks[i], buffer + written, buffer_size - written);
+ if (chunk_written == 0)
+ return 0;
+ else
+ written += chunk_written;
+ }
+ if (cbor_encode_break(buffer + written, buffer_size - written) > 0)
+ return written + 1;
+ else
+ return 0;
+ }
+}
+
+size_t cbor_serialize_string(const cbor_item_t *item, unsigned char *buffer,
+ size_t buffer_size) {
+ assert(cbor_isa_string(item));
+ if (cbor_string_is_definite(item)) {
+ size_t length = cbor_string_length(item);
+ size_t written = cbor_encode_string_start(length, buffer, buffer_size);
+ if (written && (buffer_size - written >= length)) {
+ memcpy(buffer + written, cbor_string_handle(item), length);
+ return written + length;
+ } else
+ return 0;
+ } else {
+ assert(cbor_string_is_indefinite(item));
+ size_t chunk_count = cbor_string_chunk_count(item);
+ size_t written = cbor_encode_indef_string_start(buffer, buffer_size);
+
+ if (written == 0) return 0;
+
+ cbor_item_t **chunks = cbor_string_chunks_handle(item);
+ for (size_t i = 0; i < chunk_count; i++) {
+ size_t chunk_written = cbor_serialize_string(chunks[i], buffer + written,
+ buffer_size - written);
+ if (chunk_written == 0)
+ return 0;
+ else
+ written += chunk_written;
+ }
+ if (cbor_encode_break(buffer + written, buffer_size - written) > 0)
+ return written + 1;
+ else
+ return 0;
+ }
+}
+
+size_t cbor_serialize_array(const cbor_item_t *item, unsigned char *buffer,
+ size_t buffer_size) {
+ assert(cbor_isa_array(item));
+ size_t size = cbor_array_size(item), written = 0;
+ cbor_item_t **handle = cbor_array_handle(item);
+ if (cbor_array_is_definite(item)) {
+ written = cbor_encode_array_start(size, buffer, buffer_size);
+ } else {
+ assert(cbor_array_is_indefinite(item));
+ written = cbor_encode_indef_array_start(buffer, buffer_size);
+ }
+ if (written == 0) return 0;
+
+ size_t item_written;
+ for (size_t i = 0; i < size; i++) {
+ item_written =
+ cbor_serialize(*(handle++), buffer + written, buffer_size - written);
+ if (item_written == 0)
+ return 0;
+ else
+ written += item_written;
+ }
+
+ if (cbor_array_is_definite(item)) {
+ return written;
+ } else {
+ assert(cbor_array_is_indefinite(item));
+ item_written = cbor_encode_break(buffer + written, buffer_size - written);
+ if (item_written == 0)
+ return 0;
+ else
+ return written + 1;
+ }
+}
+
+size_t cbor_serialize_map(const cbor_item_t *item, unsigned char *buffer,
+ size_t buffer_size) {
+ assert(cbor_isa_map(item));
+ size_t size = cbor_map_size(item), written = 0;
+ struct cbor_pair *handle = cbor_map_handle(item);
+
+ if (cbor_map_is_definite(item)) {
+ written = cbor_encode_map_start(size, buffer, buffer_size);
+ } else {
+ assert(cbor_map_is_indefinite(item));
+ written = cbor_encode_indef_map_start(buffer, buffer_size);
+ }
+ if (written == 0) return 0;
+
+ size_t item_written;
+ for (size_t i = 0; i < size; i++) {
+ item_written =
+ cbor_serialize(handle->key, buffer + written, buffer_size - written);
+ if (item_written == 0)
+ return 0;
+ else
+ written += item_written;
+ item_written = cbor_serialize((handle++)->value, buffer + written,
+ buffer_size - written);
+ if (item_written == 0)
+ return 0;
+ else
+ written += item_written;
+ }
+
+ if (cbor_map_is_definite(item)) {
+ return written;
+ } else {
+ assert(cbor_map_is_indefinite(item));
+ item_written = cbor_encode_break(buffer + written, buffer_size - written);
+ if (item_written == 0)
+ return 0;
+ else
+ return written + 1;
+ }
+}
+
+size_t cbor_serialize_tag(const cbor_item_t *item, unsigned char *buffer,
+ size_t buffer_size) {
+ assert(cbor_isa_tag(item));
+ size_t written = cbor_encode_tag(cbor_tag_value(item), buffer, buffer_size);
+ if (written == 0) return 0;
+
+ size_t item_written = cbor_serialize(cbor_move(cbor_tag_item(item)),
+ buffer + written, buffer_size - written);
+ if (item_written == 0)
+ return 0;
+ else
+ return written + item_written;
+}
+
+size_t cbor_serialize_float_ctrl(const cbor_item_t *item, unsigned char *buffer,
+ size_t buffer_size) {
+ assert(cbor_isa_float_ctrl(item));
+ switch (cbor_float_get_width(item)) {
+ case CBOR_FLOAT_0:
+ /* CTRL - special treatment */
+ return cbor_encode_ctrl(cbor_ctrl_value(item), buffer, buffer_size);
+ case CBOR_FLOAT_16:
+ return cbor_encode_half(cbor_float_get_float2(item), buffer, buffer_size);
+ case CBOR_FLOAT_32:
+ return cbor_encode_single(cbor_float_get_float4(item), buffer,
+ buffer_size);
+ case CBOR_FLOAT_64:
+ return cbor_encode_double(cbor_float_get_float8(item), buffer,
+ buffer_size);
+ }
+
+ /* Should never happen - make the compiler happy */
+ return 0;
+}
diff --git a/src/cbor/serialization.h b/src/cbor/serialization.h
new file mode 100644
index 000000000000..3f7707afca61
--- /dev/null
+++ b/src/cbor/serialization.h
@@ -0,0 +1,135 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_SERIALIZATION_H
+#define LIBCBOR_SERIALIZATION_H
+
+#include "cbor/cbor_export.h"
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * ============================================================================
+ * High level encoding
+ * ============================================================================
+ */
+
+/** Serialize the given item
+ *
+ * @param item[borrow] A data item
+ * @param buffer Buffer to serialize to
+ * @param buffer_size Size of the \p buffer
+ * @return Length of the result. 0 on failure.
+ */
+CBOR_EXPORT size_t cbor_serialize(const cbor_item_t *item,
+ cbor_mutable_data buffer, size_t buffer_size);
+
+/** Serialize the given item, allocating buffers as needed
+ *
+ * \rst
+ * .. warning:: It is your responsibility to free the buffer using an
+ * appropriate ``free`` implementation.
+ * \endrst
+ *
+ * @param item[borrow] A data item
+ * @param buffer[out] Buffer containing the result
+ * @param buffer_size[out] Size of the \p buffer
+ * @return Length of the result. 0 on failure, in which case \p buffer is
+ * ``NULL``.
+ */
+CBOR_EXPORT size_t cbor_serialize_alloc(const cbor_item_t *item,
+ cbor_mutable_data *buffer,
+ size_t *buffer_size);
+
+/** Serialize an uint
+ *
+ * @param item[borrow] A uint
+ * @param buffer Buffer to serialize to
+ * @param buffer_size Size of the \p buffer
+ * @return Length of the result. 0 on failure.
+ */
+CBOR_EXPORT size_t cbor_serialize_uint(const cbor_item_t *, cbor_mutable_data,
+ size_t);
+
+/** Serialize a negint
+ *
+ * @param item[borrow] A neging
+ * @param buffer Buffer to serialize to
+ * @param buffer_size Size of the \p buffer
+ * @return Length of the result. 0 on failure.
+ */
+CBOR_EXPORT size_t cbor_serialize_negint(const cbor_item_t *, cbor_mutable_data,
+ size_t);
+
+/** Serialize a bytestring
+ *
+ * @param item[borrow] A bytestring
+ * @param buffer Buffer to serialize to
+ * @param buffer_size Size of the \p buffer
+ * @return Length of the result. 0 on failure.
+ */
+CBOR_EXPORT size_t cbor_serialize_bytestring(const cbor_item_t *,
+ cbor_mutable_data, size_t);
+
+/** Serialize a string
+ *
+ * @param item[borrow] A string
+ * @param buffer Buffer to serialize to
+ * @param buffer_size Size of the \p buffer
+ * @return Length of the result. 0 on failure.
+ */
+CBOR_EXPORT size_t cbor_serialize_string(const cbor_item_t *, cbor_mutable_data,
+ size_t);
+
+/** Serialize an array
+ *
+ * @param item[borrow] An array
+ * @param buffer Buffer to serialize to
+ * @param buffer_size Size of the \p buffer
+ * @return Length of the result. 0 on failure.
+ */
+CBOR_EXPORT size_t cbor_serialize_array(const cbor_item_t *, cbor_mutable_data,
+ size_t);
+
+/** Serialize a map
+ *
+ * @param item[borrow] A map
+ * @param buffer Buffer to serialize to
+ * @param buffer_size Size of the \p buffer
+ * @return Length of the result. 0 on failure.
+ */
+CBOR_EXPORT size_t cbor_serialize_map(const cbor_item_t *, cbor_mutable_data,
+ size_t);
+
+/** Serialize a tag
+ *
+ * @param item[borrow] A tag
+ * @param buffer Buffer to serialize to
+ * @param buffer_size Size of the \p buffer
+ * @return Length of the result. 0 on failure.
+ */
+CBOR_EXPORT size_t cbor_serialize_tag(const cbor_item_t *, cbor_mutable_data,
+ size_t);
+
+/** Serialize a
+ *
+ * @param item[borrow] A float or ctrl
+ * @param buffer Buffer to serialize to
+ * @param buffer_size Size of the \p buffer
+ * @return Length of the result. 0 on failure.
+ */
+CBOR_EXPORT size_t cbor_serialize_float_ctrl(const cbor_item_t *,
+ cbor_mutable_data, size_t);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_SERIALIZATION_H
diff --git a/src/cbor/streaming.c b/src/cbor/streaming.c
new file mode 100644
index 000000000000..aa58abb3eaba
--- /dev/null
+++ b/src/cbor/streaming.c
@@ -0,0 +1,664 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "streaming.h"
+#include "internal/loaders.h"
+
+bool static claim_bytes(size_t required, size_t provided,
+ struct cbor_decoder_result *result) {
+ if (required > (provided - result->read)) {
+ result->required = required + result->read;
+ result->read = 0;
+ result->status = CBOR_DECODER_NEDATA;
+ return false;
+ } else {
+ result->read += required;
+ result->required = 0;
+ return true;
+ }
+}
+
+struct cbor_decoder_result cbor_stream_decode(
+ cbor_data source, size_t source_size,
+ const struct cbor_callbacks *callbacks, void *context) {
+ // Attempt to claim the initial MTB byte
+ struct cbor_decoder_result result = {.status = CBOR_DECODER_FINISHED};
+ if (!claim_bytes(1, source_size, &result)) {
+ return result;
+ }
+
+ switch (*source) {
+ case 0x00: /* Fallthrough */
+ case 0x01: /* Fallthrough */
+ case 0x02: /* Fallthrough */
+ case 0x03: /* Fallthrough */
+ case 0x04: /* Fallthrough */
+ case 0x05: /* Fallthrough */
+ case 0x06: /* Fallthrough */
+ case 0x07: /* Fallthrough */
+ case 0x08: /* Fallthrough */
+ case 0x09: /* Fallthrough */
+ case 0x0A: /* Fallthrough */
+ case 0x0B: /* Fallthrough */
+ case 0x0C: /* Fallthrough */
+ case 0x0D: /* Fallthrough */
+ case 0x0E: /* Fallthrough */
+ case 0x0F: /* Fallthrough */
+ case 0x10: /* Fallthrough */
+ case 0x11: /* Fallthrough */
+ case 0x12: /* Fallthrough */
+ case 0x13: /* Fallthrough */
+ case 0x14: /* Fallthrough */
+ case 0x15: /* Fallthrough */
+ case 0x16: /* Fallthrough */
+ case 0x17:
+ /* Embedded one byte unsigned integer */
+ {
+ callbacks->uint8(context, _cbor_load_uint8(source));
+ return result;
+ }
+ case 0x18:
+ /* One byte unsigned integer */
+ {
+ if (claim_bytes(1, source_size, &result)) {
+ callbacks->uint8(context, _cbor_load_uint8(source + 1));
+ }
+ return result;
+ }
+ case 0x19:
+ /* Two bytes unsigned integer */
+ {
+ if (claim_bytes(2, source_size, &result)) {
+ callbacks->uint16(context, _cbor_load_uint16(source + 1));
+ }
+ return result;
+ }
+ case 0x1A:
+ /* Four bytes unsigned integer */
+ {
+ if (claim_bytes(4, source_size, &result)) {
+ callbacks->uint32(context, _cbor_load_uint32(source + 1));
+ }
+ return result;
+ }
+ case 0x1B:
+ /* Eight bytes unsigned integer */
+ {
+ if (claim_bytes(8, source_size, &result)) {
+ callbacks->uint64(context, _cbor_load_uint64(source + 1));
+ }
+ return result;
+ }
+ case 0x1C: /* Fallthrough */
+ case 0x1D: /* Fallthrough */
+ case 0x1E: /* Fallthrough */
+ case 0x1F:
+ /* Reserved */
+ { return (struct cbor_decoder_result){0, CBOR_DECODER_ERROR}; }
+ case 0x20: /* Fallthrough */
+ case 0x21: /* Fallthrough */
+ case 0x22: /* Fallthrough */
+ case 0x23: /* Fallthrough */
+ case 0x24: /* Fallthrough */
+ case 0x25: /* Fallthrough */
+ case 0x26: /* Fallthrough */
+ case 0x27: /* Fallthrough */
+ case 0x28: /* Fallthrough */
+ case 0x29: /* Fallthrough */
+ case 0x2A: /* Fallthrough */
+ case 0x2B: /* Fallthrough */
+ case 0x2C: /* Fallthrough */
+ case 0x2D: /* Fallthrough */
+ case 0x2E: /* Fallthrough */
+ case 0x2F: /* Fallthrough */
+ case 0x30: /* Fallthrough */
+ case 0x31: /* Fallthrough */
+ case 0x32: /* Fallthrough */
+ case 0x33: /* Fallthrough */
+ case 0x34: /* Fallthrough */
+ case 0x35: /* Fallthrough */
+ case 0x36: /* Fallthrough */
+ case 0x37:
+ /* Embedded one byte negative integer */
+ {
+ callbacks->negint8(context,
+ _cbor_load_uint8(source) - 0x20); /* 0x20 offset */
+ return result;
+ }
+ case 0x38:
+ /* One byte negative integer */
+ {
+ if (claim_bytes(1, source_size, &result)) {
+ callbacks->negint8(context, _cbor_load_uint8(source + 1));
+ }
+ return result;
+ }
+ case 0x39:
+ /* Two bytes negative integer */
+ {
+ if (claim_bytes(2, source_size, &result)) {
+ callbacks->negint16(context, _cbor_load_uint16(source + 1));
+ }
+ return result;
+ }
+ case 0x3A:
+ /* Four bytes negative integer */
+ {
+ if (claim_bytes(4, source_size, &result)) {
+ callbacks->negint32(context, _cbor_load_uint32(source + 1));
+ }
+ return result;
+ }
+ case 0x3B:
+ /* Eight bytes negative integer */
+ {
+ if (claim_bytes(8, source_size, &result)) {
+ callbacks->negint64(context, _cbor_load_uint64(source + 1));
+ }
+ return result;
+ }
+ case 0x3C: /* Fallthrough */
+ case 0x3D: /* Fallthrough */
+ case 0x3E: /* Fallthrough */
+ case 0x3F:
+ /* Reserved */
+ { return (struct cbor_decoder_result){0, CBOR_DECODER_ERROR}; }
+ case 0x40: /* Fallthrough */
+ case 0x41: /* Fallthrough */
+ case 0x42: /* Fallthrough */
+ case 0x43: /* Fallthrough */
+ case 0x44: /* Fallthrough */
+ case 0x45: /* Fallthrough */
+ case 0x46: /* Fallthrough */
+ case 0x47: /* Fallthrough */
+ case 0x48: /* Fallthrough */
+ case 0x49: /* Fallthrough */
+ case 0x4A: /* Fallthrough */
+ case 0x4B: /* Fallthrough */
+ case 0x4C: /* Fallthrough */
+ case 0x4D: /* Fallthrough */
+ case 0x4E: /* Fallthrough */
+ case 0x4F: /* Fallthrough */
+ case 0x50: /* Fallthrough */
+ case 0x51: /* Fallthrough */
+ case 0x52: /* Fallthrough */
+ case 0x53: /* Fallthrough */
+ case 0x54: /* Fallthrough */
+ case 0x55: /* Fallthrough */
+ case 0x56: /* Fallthrough */
+ case 0x57:
+ /* Embedded length byte string */
+ {
+ size_t length =
+ (size_t)_cbor_load_uint8(source) - 0x40; /* 0x40 offset */
+ if (claim_bytes(length, source_size, &result)) {
+ callbacks->byte_string(context, source + 1, length);
+ }
+ return result;
+ }
+ case 0x58:
+ /* One byte length byte string */
+ // TODO template this?
+ {
+ if (claim_bytes(1, source_size, &result)) {
+ size_t length = (size_t)_cbor_load_uint8(source + 1);
+ if (claim_bytes(length, source_size, &result)) {
+ callbacks->byte_string(context, source + 1 + 1, length);
+ }
+ }
+ return result;
+ }
+ case 0x59:
+ /* Two bytes length byte string */
+ {
+ if (claim_bytes(2, source_size, &result)) {
+ size_t length = (size_t)_cbor_load_uint16(source + 1);
+ if (claim_bytes(length, source_size, &result)) {
+ callbacks->byte_string(context, source + 1 + 2, length);
+ }
+ }
+ return result;
+ }
+ case 0x5A:
+ /* Four bytes length byte string */
+ {
+ if (claim_bytes(4, source_size, &result)) {
+ size_t length = (size_t)_cbor_load_uint32(source + 1);
+ if (claim_bytes(length, source_size, &result)) {
+ callbacks->byte_string(context, source + 1 + 4, length);
+ }
+ }
+ return result;
+ }
+ case 0x5B:
+ /* Eight bytes length byte string */
+ {
+ if (claim_bytes(8, source_size, &result)) {
+ size_t length = (size_t)_cbor_load_uint64(source + 1);
+ if (claim_bytes(length, source_size, &result)) {
+ callbacks->byte_string(context, source + 1 + 8, length);
+ }
+ }
+ return result;
+ }
+ case 0x5C: /* Fallthrough */
+ case 0x5D: /* Fallthrough */
+ case 0x5E:
+ /* Reserved */
+ { return (struct cbor_decoder_result){0, CBOR_DECODER_ERROR}; }
+ case 0x5F:
+ /* Indefinite byte string */
+ {
+ callbacks->byte_string_start(context);
+ return result;
+ }
+ case 0x60: /* Fallthrough */
+ case 0x61: /* Fallthrough */
+ case 0x62: /* Fallthrough */
+ case 0x63: /* Fallthrough */
+ case 0x64: /* Fallthrough */
+ case 0x65: /* Fallthrough */
+ case 0x66: /* Fallthrough */
+ case 0x67: /* Fallthrough */
+ case 0x68: /* Fallthrough */
+ case 0x69: /* Fallthrough */
+ case 0x6A: /* Fallthrough */
+ case 0x6B: /* Fallthrough */
+ case 0x6C: /* Fallthrough */
+ case 0x6D: /* Fallthrough */
+ case 0x6E: /* Fallthrough */
+ case 0x6F: /* Fallthrough */
+ case 0x70: /* Fallthrough */
+ case 0x71: /* Fallthrough */
+ case 0x72: /* Fallthrough */
+ case 0x73: /* Fallthrough */
+ case 0x74: /* Fallthrough */
+ case 0x75: /* Fallthrough */
+ case 0x76: /* Fallthrough */
+ case 0x77:
+ /* Embedded one byte length string */
+ {
+ size_t length =
+ (size_t)_cbor_load_uint8(source) - 0x60; /* 0x60 offset */
+ if (claim_bytes(length, source_size, &result)) {
+ callbacks->string(context, source + 1, length);
+ }
+ return result;
+ }
+ case 0x78:
+ /* One byte length string */
+ {
+ if (claim_bytes(1, source_size, &result)) {
+ size_t length = (size_t)_cbor_load_uint8(source + 1);
+ if (claim_bytes(length, source_size, &result)) {
+ callbacks->string(context, source + 1 + 1, length);
+ }
+ }
+ return result;
+ }
+ case 0x79:
+ /* Two bytes length string */
+ {
+ if (claim_bytes(2, source_size, &result)) {
+ size_t length = (size_t)_cbor_load_uint16(source + 1);
+ if (claim_bytes(length, source_size, &result)) {
+ callbacks->string(context, source + 1 + 2, length);
+ }
+ }
+ return result;
+ }
+ case 0x7A:
+ /* Four bytes length string */
+ {
+ if (claim_bytes(4, source_size, &result)) {
+ size_t length = (size_t)_cbor_load_uint32(source + 1);
+ if (claim_bytes(length, source_size, &result)) {
+ callbacks->string(context, source + 1 + 4, length);
+ }
+ }
+ return result;
+ }
+ case 0x7B:
+ /* Eight bytes length string */
+ {
+ if (claim_bytes(8, source_size, &result)) {
+ size_t length = (size_t)_cbor_load_uint64(source + 1);
+ if (claim_bytes(length, source_size, &result)) {
+ callbacks->string(context, source + 1 + 8, length);
+ }
+ }
+ return result;
+ }
+ case 0x7C: /* Fallthrough */
+ case 0x7D: /* Fallthrough */
+ case 0x7E:
+ /* Reserved */
+ { return (struct cbor_decoder_result){0, CBOR_DECODER_ERROR}; }
+ case 0x7F:
+ /* Indefinite length string */
+ {
+ callbacks->string_start(context);
+ return result;
+ }
+ case 0x80: /* Fallthrough */
+ case 0x81: /* Fallthrough */
+ case 0x82: /* Fallthrough */
+ case 0x83: /* Fallthrough */
+ case 0x84: /* Fallthrough */
+ case 0x85: /* Fallthrough */
+ case 0x86: /* Fallthrough */
+ case 0x87: /* Fallthrough */
+ case 0x88: /* Fallthrough */
+ case 0x89: /* Fallthrough */
+ case 0x8A: /* Fallthrough */
+ case 0x8B: /* Fallthrough */
+ case 0x8C: /* Fallthrough */
+ case 0x8D: /* Fallthrough */
+ case 0x8E: /* Fallthrough */
+ case 0x8F: /* Fallthrough */
+ case 0x90: /* Fallthrough */
+ case 0x91: /* Fallthrough */
+ case 0x92: /* Fallthrough */
+ case 0x93: /* Fallthrough */
+ case 0x94: /* Fallthrough */
+ case 0x95: /* Fallthrough */
+ case 0x96: /* Fallthrough */
+ case 0x97:
+ /* Embedded one byte length array */
+ {
+ callbacks->array_start(
+ context, (size_t)_cbor_load_uint8(source) - 0x80); /* 0x40 offset */
+ return result;
+ }
+ case 0x98:
+ /* One byte length array */
+ {
+ if (claim_bytes(1, source_size, &result)) {
+ callbacks->array_start(context, (size_t)_cbor_load_uint8(source + 1));
+ }
+ return result;
+ }
+ case 0x99:
+ /* Two bytes length array */
+ {
+ if (claim_bytes(2, source_size, &result)) {
+ callbacks->array_start(context,
+ (size_t)_cbor_load_uint16(source + 1));
+ }
+ return result;
+ }
+ case 0x9A:
+ /* Four bytes length array */
+ {
+ if (claim_bytes(4, source_size, &result)) {
+ callbacks->array_start(context,
+ (size_t)_cbor_load_uint32(source + 1));
+ }
+ return result;
+ }
+ case 0x9B:
+ /* Eight bytes length array */
+ {
+ if (claim_bytes(8, source_size, &result)) {
+ callbacks->array_start(context,
+ (size_t)_cbor_load_uint64(source + 1));
+ }
+ return result;
+ }
+ case 0x9C: /* Fallthrough */
+ case 0x9D: /* Fallthrough */
+ case 0x9E:
+ /* Reserved */
+ { return (struct cbor_decoder_result){0, CBOR_DECODER_ERROR}; }
+ case 0x9F:
+ /* Indefinite length array */
+ {
+ callbacks->indef_array_start(context);
+ return result;
+ }
+ case 0xA0: /* Fallthrough */
+ case 0xA1: /* Fallthrough */
+ case 0xA2: /* Fallthrough */
+ case 0xA3: /* Fallthrough */
+ case 0xA4: /* Fallthrough */
+ case 0xA5: /* Fallthrough */
+ case 0xA6: /* Fallthrough */
+ case 0xA7: /* Fallthrough */
+ case 0xA8: /* Fallthrough */
+ case 0xA9: /* Fallthrough */
+ case 0xAA: /* Fallthrough */
+ case 0xAB: /* Fallthrough */
+ case 0xAC: /* Fallthrough */
+ case 0xAD: /* Fallthrough */
+ case 0xAE: /* Fallthrough */
+ case 0xAF: /* Fallthrough */
+ case 0xB0: /* Fallthrough */
+ case 0xB1: /* Fallthrough */
+ case 0xB2: /* Fallthrough */
+ case 0xB3: /* Fallthrough */
+ case 0xB4: /* Fallthrough */
+ case 0xB5: /* Fallthrough */
+ case 0xB6: /* Fallthrough */
+ case 0xB7:
+ /* Embedded one byte length map */
+ {
+ callbacks->map_start(
+ context, (size_t)_cbor_load_uint8(source) - 0xA0); /* 0xA0 offset */
+ return result;
+ }
+ case 0xB8:
+ /* One byte length map */
+ {
+ if (claim_bytes(1, source_size, &result)) {
+ callbacks->map_start(context, (size_t)_cbor_load_uint8(source + 1));
+ }
+ return result;
+ }
+ case 0xB9:
+ /* Two bytes length map */
+ {
+ if (claim_bytes(2, source_size, &result)) {
+ callbacks->map_start(context, (size_t)_cbor_load_uint16(source + 1));
+ }
+ return result;
+ }
+ case 0xBA:
+ /* Four bytes length map */
+ {
+ if (claim_bytes(4, source_size, &result)) {
+ callbacks->map_start(context, (size_t)_cbor_load_uint32(source + 1));
+ }
+ return result;
+ }
+ case 0xBB:
+ /* Eight bytes length map */
+ {
+ if (claim_bytes(8, source_size, &result)) {
+ callbacks->map_start(context, (size_t)_cbor_load_uint64(source + 1));
+ }
+ return result;
+ }
+ case 0xBC: /* Fallthrough */
+ case 0xBD: /* Fallthrough */
+ case 0xBE:
+ /* Reserved */
+ { return (struct cbor_decoder_result){0, CBOR_DECODER_ERROR}; }
+ case 0xBF:
+ /* Indefinite length map */
+ {
+ callbacks->indef_map_start(context);
+ return result;
+ }
+ case 0xC0:
+ /* Text date/time - RFC 3339 tag, fallthrough */
+ case 0xC1:
+ /* Epoch date tag, fallthrough */
+ case 0xC2:
+ /* Positive bignum tag, fallthrough */
+ case 0xC3:
+ /* Negative bignum tag, fallthrough */
+ case 0xC4:
+ /* Fraction, fallthrough */
+ case 0xC5:
+ /* Big float */
+ {
+ callbacks->tag(context,
+ _cbor_load_uint8(source) - 0xC0); /* 0xC0 offset */
+ return result;
+ }
+ case 0xC6: /* Fallthrough */
+ case 0xC7: /* Fallthrough */
+ case 0xC8: /* Fallthrough */
+ case 0xC9: /* Fallthrough */
+ case 0xCA: /* Fallthrough */
+ case 0xCB: /* Fallthrough */
+ case 0xCC: /* Fallthrough */
+ case 0xCD: /* Fallthrough */
+ case 0xCE: /* Fallthrough */
+ case 0xCF: /* Fallthrough */
+ case 0xD0: /* Fallthrough */
+ case 0xD1: /* Fallthrough */
+ case 0xD2: /* Fallthrough */
+ case 0xD3: /* Fallthrough */
+ case 0xD4: /* Unassigned tag value */
+ {
+ return (struct cbor_decoder_result){0, CBOR_DECODER_ERROR};
+ }
+ case 0xD5: /* Expected b64url conversion tag - fallthrough */
+ case 0xD6: /* Expected b64 conversion tag - fallthrough */
+ case 0xD7: /* Expected b16 conversion tag */
+ {
+ callbacks->tag(context,
+ _cbor_load_uint8(source) - 0xC0); /* 0xC0 offset */
+ return result;
+ }
+ case 0xD8: /* 1B tag */
+ {
+ if (claim_bytes(1, source_size, &result)) {
+ callbacks->tag(context, _cbor_load_uint8(source + 1));
+ }
+ return result;
+ }
+ case 0xD9: /* 2B tag */
+ {
+ if (claim_bytes(2, source_size, &result)) {
+ callbacks->tag(context, _cbor_load_uint16(source + 1));
+ }
+ return result;
+ }
+ case 0xDA: /* 4B tag */
+ {
+ if (claim_bytes(4, source_size, &result)) {
+ callbacks->tag(context, _cbor_load_uint32(source + 1));
+ }
+ return result;
+ }
+ case 0xDB: /* 8B tag */
+ {
+ if (claim_bytes(8, source_size, &result)) {
+ callbacks->tag(context, _cbor_load_uint64(source + 1));
+ }
+ return result;
+ }
+ case 0xDC: /* Fallthrough */
+ case 0xDD: /* Fallthrough */
+ case 0xDE: /* Fallthrough */
+ case 0xDF: /* Reserved */
+ {
+ return (struct cbor_decoder_result){0, CBOR_DECODER_ERROR};
+ }
+ case 0xE0: /* Fallthrough */
+ case 0xE1: /* Fallthrough */
+ case 0xE2: /* Fallthrough */
+ case 0xE3: /* Fallthrough */
+ case 0xE4: /* Fallthrough */
+ case 0xE5: /* Fallthrough */
+ case 0xE6: /* Fallthrough */
+ case 0xE7: /* Fallthrough */
+ case 0xE8: /* Fallthrough */
+ case 0xE9: /* Fallthrough */
+ case 0xEA: /* Fallthrough */
+ case 0xEB: /* Fallthrough */
+ case 0xEC: /* Fallthrough */
+ case 0xED: /* Fallthrough */
+ case 0xEE: /* Fallthrough */
+ case 0xEF: /* Fallthrough */
+ case 0xF0: /* Fallthrough */
+ case 0xF1: /* Fallthrough */
+ case 0xF2: /* Fallthrough */
+ case 0xF3: /* Simple value - unassigned */
+ {
+ return (struct cbor_decoder_result){0, CBOR_DECODER_ERROR};
+ }
+ case 0xF4:
+ /* False */
+ {
+ callbacks->boolean(context, false);
+ return result;
+ }
+ case 0xF5:
+ /* True */
+ {
+ callbacks->boolean(context, true);
+ return result;
+ }
+ case 0xF6:
+ /* Null */
+ {
+ callbacks->null(context);
+ return result;
+ }
+ case 0xF7:
+ /* Undefined */
+ {
+ callbacks->undefined(context);
+ return result;
+ }
+ case 0xF8:
+ /* 1B simple value, unassigned */
+ { return (struct cbor_decoder_result){0, CBOR_DECODER_ERROR}; }
+ case 0xF9:
+ /* 2B float */
+ {
+ if (claim_bytes(2, source_size, &result)) {
+ callbacks->float2(context, _cbor_load_half(source + 1));
+ }
+ return result;
+ }
+ case 0xFA:
+ /* 4B float */
+ {
+ if (claim_bytes(4, source_size, &result)) {
+ callbacks->float4(context, _cbor_load_float(source + 1));
+ }
+ return result;
+ }
+ case 0xFB:
+ /* 8B float */
+ {
+ if (claim_bytes(8, source_size, &result)) {
+ callbacks->float8(context, _cbor_load_double(source + 1));
+ }
+ return result;
+ }
+ case 0xFC: /* Fallthrough */
+ case 0xFD: /* Fallthrough */
+ case 0xFE:
+ /* Reserved */
+ { return (struct cbor_decoder_result){0, CBOR_DECODER_ERROR}; }
+ case 0xFF:
+ /* Break */
+ {
+ callbacks->indef_break(context);
+ return result;
+ }
+ default: /* Never happens - this shuts up the compiler */
+ {
+ return result;
+ }
+ }
+}
diff --git a/src/cbor/streaming.h b/src/cbor/streaming.h
new file mode 100644
index 000000000000..532d959b8b2e
--- /dev/null
+++ b/src/cbor/streaming.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_STREAMING_H
+#define LIBCBOR_STREAMING_H
+
+#include "callbacks.h"
+#include "cbor/cbor_export.h"
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Stateless decoder
+ *
+ * Will try parsing the \p buffer and will invoke the appropriate callback on
+ * success. Decodes one item at a time. No memory allocations occur.
+ *
+ * @param buffer Input buffer
+ * @param buffer_size Length of the buffer
+ * @param callbacks The callback bundle
+ * @param context An arbitrary pointer to allow for maintaining context.
+ */
+CBOR_EXPORT struct cbor_decoder_result cbor_stream_decode(
+ cbor_data buffer, size_t buffer_size,
+ const struct cbor_callbacks* callbacks, void* context);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_STREAMING_H
diff --git a/src/cbor/strings.c b/src/cbor/strings.c
new file mode 100644
index 000000000000..209886b5f8c3
--- /dev/null
+++ b/src/cbor/strings.c
@@ -0,0 +1,133 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "strings.h"
+#include <string.h>
+#include "internal/memory_utils.h"
+
+cbor_item_t *cbor_new_definite_string() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
+ _CBOR_NOTNULL(item);
+ *item = (cbor_item_t){
+ .refcount = 1,
+ .type = CBOR_TYPE_STRING,
+ .metadata = {.string_metadata = {_CBOR_METADATA_DEFINITE, 0}}};
+ return item;
+}
+
+cbor_item_t *cbor_new_indefinite_string() {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
+ _CBOR_NOTNULL(item);
+ *item = (cbor_item_t){
+ .refcount = 1,
+ .type = CBOR_TYPE_STRING,
+ .metadata = {.string_metadata = {.type = _CBOR_METADATA_INDEFINITE,
+ .length = 0}},
+ .data = _CBOR_MALLOC(sizeof(struct cbor_indefinite_string_data))};
+ _CBOR_DEPENDENT_NOTNULL(item, item->data);
+ *((struct cbor_indefinite_string_data *)item->data) =
+ (struct cbor_indefinite_string_data){
+ .chunk_count = 0,
+ .chunk_capacity = 0,
+ .chunks = NULL,
+ };
+ return item;
+}
+
+cbor_item_t *cbor_build_string(const char *val) {
+ cbor_item_t *item = cbor_new_definite_string();
+ _CBOR_NOTNULL(item);
+ size_t len = strlen(val);
+ unsigned char *handle = _CBOR_MALLOC(len);
+ _CBOR_DEPENDENT_NOTNULL(item, handle);
+ memcpy(handle, val, len);
+ cbor_string_set_handle(item, handle, len);
+ return item;
+}
+
+cbor_item_t *cbor_build_stringn(const char *val, size_t length) {
+ cbor_item_t *item = cbor_new_definite_string();
+ _CBOR_NOTNULL(item);
+ unsigned char *handle = _CBOR_MALLOC(length);
+ _CBOR_DEPENDENT_NOTNULL(item, handle);
+ memcpy(handle, val, length);
+ cbor_string_set_handle(item, handle, length);
+ return item;
+}
+
+void cbor_string_set_handle(cbor_item_t *item,
+ cbor_mutable_data CBOR_RESTRICT_POINTER data,
+ size_t length) {
+ assert(cbor_isa_string(item));
+ assert(cbor_string_is_definite(item));
+ item->data = data;
+ item->metadata.string_metadata.length = length;
+}
+
+cbor_item_t **cbor_string_chunks_handle(const cbor_item_t *item) {
+ assert(cbor_isa_string(item));
+ assert(cbor_string_is_indefinite(item));
+ return ((struct cbor_indefinite_string_data *)item->data)->chunks;
+}
+
+size_t cbor_string_chunk_count(const cbor_item_t *item) {
+ assert(cbor_isa_string(item));
+ assert(cbor_string_is_indefinite(item));
+ return ((struct cbor_indefinite_string_data *)item->data)->chunk_count;
+}
+
+bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk) {
+ assert(cbor_isa_string(item));
+ assert(cbor_string_is_indefinite(item));
+ struct cbor_indefinite_string_data *data =
+ (struct cbor_indefinite_string_data *)item->data;
+ if (data->chunk_count == data->chunk_capacity) {
+ // TODO: Add a test for this
+ if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) {
+ return false;
+ }
+
+ size_t new_chunk_capacity =
+ data->chunk_capacity == 0 ? 1
+ : CBOR_BUFFER_GROWTH * (data->chunk_capacity);
+ cbor_item_t **new_chunks_data = _cbor_realloc_multiple(
+ data->chunks, sizeof(cbor_item_t *), new_chunk_capacity);
+
+ if (new_chunks_data == NULL) {
+ return false;
+ }
+
+ data->chunk_capacity = new_chunk_capacity;
+ data->chunks = new_chunks_data;
+ }
+ data->chunks[data->chunk_count++] = cbor_incref(chunk);
+ return true;
+}
+
+size_t cbor_string_length(const cbor_item_t *item) {
+ assert(cbor_isa_string(item));
+ return item->metadata.string_metadata.length;
+}
+
+unsigned char *cbor_string_handle(const cbor_item_t *item) {
+ assert(cbor_isa_string(item));
+ return item->data;
+}
+
+size_t cbor_string_codepoint_count(const cbor_item_t *item) {
+ assert(cbor_isa_string(item));
+ return item->metadata.string_metadata.codepoint_count;
+}
+
+bool cbor_string_is_definite(const cbor_item_t *item) {
+ assert(cbor_isa_string(item));
+ return item->metadata.string_metadata.type == _CBOR_METADATA_DEFINITE;
+}
+
+bool cbor_string_is_indefinite(const cbor_item_t *item) {
+ return !cbor_string_is_definite(item);
+}
diff --git a/src/cbor/strings.h b/src/cbor/strings.h
new file mode 100644
index 000000000000..49398af842a0
--- /dev/null
+++ b/src/cbor/strings.h
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_STRINGS_H
+#define LIBCBOR_STRINGS_H
+
+#include "cbor/cbor_export.h"
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * ============================================================================
+ * String manipulation
+ * ============================================================================
+ */
+
+/** Returns the length of the underlying string
+ *
+ * For definite strings only
+ *
+ * @param item[borrow] a definite string
+ * @return length of the string. Zero if no chunk has been attached yet
+ */
+CBOR_EXPORT size_t cbor_string_length(const cbor_item_t *item);
+
+/** The number of codepoints in this string
+ *
+ * Might differ from length if there are multibyte ones
+ *
+ * @param item[borrow] A string
+ * @return The number of codepoints in this string
+ */
+CBOR_EXPORT size_t cbor_string_codepoint_count(const cbor_item_t *item);
+
+/** Is the string definite?
+ *
+ * @param item[borrow] a string
+ * @return Is the string definite?
+ */
+CBOR_EXPORT bool cbor_string_is_definite(const cbor_item_t *item);
+
+/** Is the string indefinite?
+ *
+ * @param item[borrow] a string
+ * @return Is the string indefinite?
+ */
+CBOR_EXPORT bool cbor_string_is_indefinite(const cbor_item_t *item);
+
+/** Get the handle to the underlying string
+ *
+ * Definite items only. Modifying the data is allowed. In that case, the caller
+ * takes responsibility for the effect on items this item might be a part of
+ *
+ * @param item[borrow] A definite string
+ * @return The address of the underlying string. `NULL` if no data have been
+ * assigned yet.
+ */
+CBOR_EXPORT cbor_mutable_data cbor_string_handle(const cbor_item_t *item);
+
+/** Set the handle to the underlying string
+ *
+ *
+ * \rst
+ * .. warning:: Using a pointer to a stack allocated constant is a common
+ * mistake. Lifetime of the string will expire when it goes out of scope and
+ * the CBOR item will be left inconsistent.
+ * \endrst
+ *
+ * @param item[borrow] A definite string
+ * @param data The memory block. The caller gives up the ownership of the block.
+ * libcbor will deallocate it when appropriate using its free function
+ * @param length Length of the data block
+ */
+CBOR_EXPORT void cbor_string_set_handle(
+ cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data,
+ size_t length);
+
+/** Get the handle to the array of chunks
+ *
+ * Manipulations with the memory block (e.g. sorting it) are allowed, but the
+ * validity and the number of chunks must be retained.
+ *
+ * @param item[borrow] A indefinite string
+ * @return array of #cbor_string_chunk_count definite strings
+ */
+CBOR_EXPORT cbor_item_t **cbor_string_chunks_handle(const cbor_item_t *item);
+
+/** Get the number of chunks this string consist of
+ *
+ * @param item[borrow] A indefinite string
+ * @return The chunk count. 0 for freshly created items.
+ */
+CBOR_EXPORT size_t cbor_string_chunk_count(const cbor_item_t *item);
+
+/** Appends a chunk to the string
+ *
+ * Indefinite strings only.
+ *
+ * May realloc the chunk storage.
+ *
+ * @param item[borrow] An indefinite string
+ * @param item[incref] A definite string
+ * @return true on success. false on realloc failure. In that case, the refcount
+ * of `chunk` is not increased and the `item` is left intact.
+ */
+CBOR_EXPORT bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk);
+
+/** Creates a new definite string
+ *
+ * The handle is initialized to `NULL` and length to 0
+ *
+ * @return **new** definite string. `NULL` on malloc failure.
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_definite_string();
+
+/** Creates a new indefinite string
+ *
+ * The chunks array is initialized to `NULL` and chunkcount to 0
+ *
+ * @return **new** indefinite string. `NULL` on malloc failure.
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_indefinite_string();
+
+/** Creates a new string and initializes it
+ *
+ * The `val` will be copied to a newly allocated block
+ *
+ * @param val A null-terminated UTF-8 string
+ * @return A **new** string with content `handle`. `NULL` on malloc failure.
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_string(const char *val);
+
+/** Creates a new string and initializes it
+ *
+ * The `handle` will be copied to a newly allocated block
+ *
+ * @param val A UTF-8 string, at least \p length long (excluding the null byte)
+ * @return A **new** string with content `handle`. `NULL` on malloc failure.
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_stringn(const char *val, size_t length);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_STRINGS_H
diff --git a/src/cbor/tags.c b/src/cbor/tags.c
new file mode 100644
index 000000000000..60b3e69eacd4
--- /dev/null
+++ b/src/cbor/tags.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "tags.h"
+
+cbor_item_t *cbor_new_tag(uint64_t value) {
+ cbor_item_t *item = _CBOR_MALLOC(sizeof(cbor_item_t));
+ _CBOR_NOTNULL(item);
+
+ *item = (cbor_item_t){
+ .refcount = 1,
+ .type = CBOR_TYPE_TAG,
+ .metadata = {.tag_metadata = {.value = value, .tagged_item = NULL}},
+ .data = NULL /* Never used */
+ };
+ return item;
+}
+
+cbor_item_t *cbor_tag_item(const cbor_item_t *item) {
+ assert(cbor_isa_tag(item));
+ return cbor_incref(item->metadata.tag_metadata.tagged_item);
+}
+
+uint64_t cbor_tag_value(const cbor_item_t *item) {
+ assert(cbor_isa_tag(item));
+ return item->metadata.tag_metadata.value;
+}
+
+void cbor_tag_set_item(cbor_item_t *item, cbor_item_t *tagged_item) {
+ assert(cbor_isa_tag(item));
+ cbor_incref(tagged_item);
+ item->metadata.tag_metadata.tagged_item = tagged_item;
+}
+
+cbor_item_t *cbor_build_tag(uint64_t value, cbor_item_t *item) {
+ cbor_item_t *res = cbor_new_tag(value);
+ cbor_tag_set_item(res, item);
+ return res;
+}
diff --git a/src/cbor/tags.h b/src/cbor/tags.h
new file mode 100644
index 000000000000..f4b8028f7951
--- /dev/null
+++ b/src/cbor/tags.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#ifndef LIBCBOR_TAGS_H
+#define LIBCBOR_TAGS_H
+
+#include "cbor/cbor_export.h"
+#include "cbor/common.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * ============================================================================
+ * Tag manipulation
+ * ============================================================================
+ */
+
+/** Create a new tag
+ *
+ * @param value The tag value. Please consult the tag repository
+ * @return **new** tag. Item reference is `NULL`. Returns `NULL` upon
+ * memory allocation failure
+ */
+CBOR_EXPORT cbor_item_t *cbor_new_tag(uint64_t value);
+
+/** Get the tagged item
+ *
+ * @param item[borrow] A tag
+ * @return **incref** the tagged item
+ */
+CBOR_EXPORT cbor_item_t *cbor_tag_item(const cbor_item_t *item);
+
+/** Get tag value
+ *
+ * @param item[borrow] A tag
+ * @return The tag value. Please consult the tag repository
+ */
+CBOR_EXPORT uint64_t cbor_tag_value(const cbor_item_t *item);
+
+/** Set the tagged item
+ *
+ * @param item[borrow] A tag
+ * @param tagged_item[incref] The item to tag
+ */
+CBOR_EXPORT void cbor_tag_set_item(cbor_item_t *item, cbor_item_t *tagged_item);
+
+/** Build a new tag
+ *
+ * @param item[incref] The tagee
+ * @param value Tag value
+ * @return **new** tag item
+ */
+CBOR_EXPORT cbor_item_t *cbor_build_tag(uint64_t value, cbor_item_t *item);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // LIBCBOR_TAGS_H
diff --git a/src/libcbor.pc.in b/src/libcbor.pc.in
new file mode 100644
index 000000000000..f144bda33bc6
--- /dev/null
+++ b/src/libcbor.pc.in
@@ -0,0 +1,11 @@
+prefix=@CMAKE_INSTALL_PREFIX@
+exec_prefix=${prefix}
+libdir=${prefix}/@CMAKE_INSTALL_LIBDIR@
+includedir=${prefix}/include
+
+Name: @PROJECT_NAME@
+Description: @PROJECT_NAME@ - CBOR protocol implementation
+URL: https://github.com/PJK/libcbor
+Version: @CBOR_VERSION@
+Libs: -L${libdir} -lcbor
+Cflags: -I${includedir}
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100644
index 000000000000..67ea13dfc44c
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,29 @@
+file(GLOB TESTS "*_test.c")
+
+if(NOT CBOR_CUSTOM_ALLOC)
+ # Memory allocation test requires custom allocator support to instrument it.
+ list(REMOVE_ITEM TESTS ${CMAKE_CURRENT_SOURCE_DIR}/memory_allocation_test.c)
+endif(NOT CBOR_CUSTOM_ALLOC)
+
+find_package(CMocka REQUIRED)
+
+message(STATUS "CMocka vars: ${CMOCKA_LIBRARIES} ${CMOCKA_INCLUDE_DIR}")
+
+foreach (TEST ${TESTS})
+ string(REGEX REPLACE ".*/([^/]+).c" "\\1" NAME ${TEST})
+ message("Adding test ${NAME}")
+ add_executable(${NAME} "${NAME}.c" assertions.c stream_expectations.c)
+ target_link_libraries(${NAME} ${CMOCKA_LIBRARIES})
+ target_link_libraries(${NAME} cbor)
+ target_include_directories(${NAME} PUBLIC ${CMOCKA_INCLUDE_DIR})
+ # See https://stackoverflow.com/a/10824578/499521
+ ADD_TEST(ctest_build_test_${NAME}
+ "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target ${NAME})
+ ADD_TEST(ctest_run_${NAME} ${NAME})
+ SET_TESTS_PROPERTIES(ctest_run_${NAME}
+ PROPERTIES DEPENDS ctest_build_test_${NAME})
+ add_dependencies(coverage ${NAME})
+endforeach (TEST)
+
+add_executable(cpp_linkage_test cpp_linkage_test.cpp)
+target_link_libraries(cpp_linkage_test cbor) \ No newline at end of file
diff --git a/test/assertions.c b/test/assertions.c
new file mode 100644
index 000000000000..16fb4cdaaaeb
--- /dev/null
+++ b/test/assertions.c
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include "assertions.h"
+#include "stream_expectations.h"
+
+void assert_uint8(cbor_item_t* item, uint8_t num) {
+ assert_true(cbor_isa_uint(item));
+ assert_true(cbor_int_get_width(item) == CBOR_INT_8);
+ assert_true(cbor_get_uint8(item) == num);
+}
+
+void assert_uint16(cbor_item_t* item, uint16_t num) {
+ assert_true(cbor_isa_uint(item));
+ assert_true(cbor_int_get_width(item) == CBOR_INT_16);
+ assert_true(cbor_get_uint16(item) == num);
+}
+
+void assert_uint32(cbor_item_t* item, uint32_t num) {
+ assert_true(cbor_isa_uint(item));
+ assert_true(cbor_int_get_width(item) == CBOR_INT_32);
+ assert_true(cbor_get_uint32(item) == num);
+}
+
+void assert_uint64(cbor_item_t* item, uint64_t num) {
+ assert_true(cbor_isa_uint(item));
+ assert_true(cbor_int_get_width(item) == CBOR_INT_64);
+ assert_true(cbor_get_uint64(item) == num);
+}
+
+void assert_decoder_result(size_t read, enum cbor_decoder_status status,
+ struct cbor_decoder_result result) {
+ assert_true(read == result.read);
+ assert_true(status == result.status);
+ assert_true(0 == result.required);
+}
+
+void assert_decoder_result_nedata(size_t required,
+ struct cbor_decoder_result result) {
+ assert_true(0 == result.read);
+ assert_true(CBOR_DECODER_NEDATA == result.status);
+ assert_int_equal((int)required, (int)result.required);
+}
+
+void assert_minimum_input_size(size_t expected, cbor_data data) {
+ for (size_t available = 1; available < expected; available++) {
+ assert_decoder_result_nedata(expected, decode(data, 1));
+ }
+}
diff --git a/test/assertions.h b/test/assertions.h
new file mode 100644
index 000000000000..7a36a4c5933a
--- /dev/null
+++ b/test/assertions.h
@@ -0,0 +1,34 @@
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+#ifndef ASSERTIONS_H_
+#define ASSERTIONS_H_
+
+void assert_uint8(cbor_item_t* item, uint8_t num);
+void assert_uint16(cbor_item_t* item, uint16_t num);
+void assert_uint32(cbor_item_t* item, uint32_t num);
+void assert_uint64(cbor_item_t* item, uint64_t num);
+
+/** Assert that result `status` and `read` are equal. */
+void assert_decoder_result(size_t read, enum cbor_decoder_status status,
+ struct cbor_decoder_result result);
+
+/**
+ * Assert that the result is set to CBOR_DECODER_NEDATA with the given
+ * `cbor_decoder_result.required` value.
+ */
+void assert_decoder_result_nedata(size_t required,
+ struct cbor_decoder_result result);
+
+/**
+ * Check that the streaming decoder returns a correct CBOR_DECODER_NEDATA
+ * result for all inputs from data[0..1] through data[0..(expected-1)].
+ */
+void assert_minimum_input_size(size_t expected, cbor_data data);
+
+#endif
diff --git a/test/bad_inputs_test.c b/test/bad_inputs_test.c
new file mode 100644
index 000000000000..6ca5270aa248
--- /dev/null
+++ b/test/bad_inputs_test.c
@@ -0,0 +1,121 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+/* These tests verify behavior on interesting randomly generated inputs from the
+ * fuzzer */
+
+cbor_item_t *item;
+struct cbor_load_result res;
+
+/* Map start + array with embedded length */
+unsigned char data1[] = {0xA9, 0x85};
+static void test_1(void **state) {
+ item = cbor_load(data1, 2, &res);
+ assert_null(item);
+ assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
+ assert_int_equal(res.error.position, 2);
+}
+
+unsigned char data2[] = {0x9D};
+static void test_2(void **state) {
+ item = cbor_load(data2, 1, &res);
+ assert_null(item);
+ assert_true(res.error.code == CBOR_ERR_MALFORMATED);
+ assert_int_equal(res.error.position, 0);
+}
+
+unsigned char data3[] = {0xD6};
+static void test_3(void **state) {
+ item = cbor_load(data3, 1, &res);
+ assert_null(item);
+ assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
+ assert_int_equal(res.error.position, 1);
+}
+
+#ifdef SANE_MALLOC
+unsigned char data4[] = {0xBA, 0xC1, 0xE8, 0x3E, 0xE7, 0x20, 0xA8};
+static void test_4(void **state) {
+ item = cbor_load(data4, 7, &res);
+ assert_null(item);
+ assert_true(res.error.code == CBOR_ERR_MEMERROR);
+ assert_int_equal(res.error.position, 5);
+}
+
+unsigned char data5[] = {0x9A, 0xDA, 0x3A, 0xB2, 0x7F, 0x29};
+static void test_5(void **state) {
+ assert_true(res.error.code == CBOR_ERR_MEMERROR);
+ item = cbor_load(data5, 6, &res);
+ assert_null(item);
+ assert_int_equal(res.error.position, 5);
+ /* Indef string expectation mismatch */
+}
+#endif
+
+unsigned char data6[] = {0x7F, 0x21, 0x4C, 0x02, 0x40};
+static void test_6(void **state) {
+ item = cbor_load(data6, 5, &res);
+ assert_null(item);
+ assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
+ assert_int_equal(res.error.position, 2);
+}
+
+#ifdef EIGHT_BYTE_SIZE_T
+/* Extremely high size value (overflows size_t in representation size). Only
+ * works with 64b sizes */
+unsigned char data7[] = {0xA2, 0x9B, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+static void test_7(void **state) {
+ item = cbor_load(data7, 16, &res);
+ assert_null(item);
+ assert_true(res.error.code == CBOR_ERR_MEMERROR);
+ assert_int_equal(res.error.position, 10);
+}
+#endif
+
+unsigned char data8[] = {0xA3, 0x64, 0x68, 0x61, 0x6C, 0x66, 0xFF, 0x00,
+ 0x00, 0x66, 0x73, 0x69, 0x6E, 0x67, 0x6C, 0x65,
+ 0xFA, 0x7F, 0x7F, 0xFF, 0xFF, 0x6D, 0x73, 0x69,
+ 0x6D, 0x70, 0x6C, 0x65, 0x20, 0x76, 0x61, 0x6C,
+ 0x75, 0x65, 0x73, 0x83, 0xF5, 0xF4, 0xF6};
+static void test_8(void **state) {
+ item = cbor_load(data8, 39, &res);
+ assert_null(item);
+ assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
+ assert_int_equal(res.error.position, 7);
+}
+
+unsigned char data9[] = {0xBF, 0x05, 0xFF, 0x00, 0x00, 0x00, 0x10, 0x04};
+static void test_9(void **state) {
+ item = cbor_load(data9, 8, &res);
+ assert_null(item);
+ assert_true(res.error.code == CBOR_ERR_SYNTAXERROR);
+ assert_int_equal(res.error.position, 3);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_1), cmocka_unit_test(test_2),
+ cmocka_unit_test(test_3),
+#ifdef SANE_MALLOC
+ cmocka_unit_test(test_4), cmocka_unit_test(test_5),
+#endif
+ cmocka_unit_test(test_6),
+#ifdef EIGHT_BYTE_SIZE_T
+ cmocka_unit_test(test_7),
+#endif
+ cmocka_unit_test(test_8), cmocka_unit_test(test_9),
+ };
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/callbacks_test.c b/test/callbacks_test.c
new file mode 100644
index 000000000000..6bb5d692bb12
--- /dev/null
+++ b/test/callbacks_test.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+unsigned char data[] = {
+ 0x93, 0x01, 0x19, 0x01, 0x01, 0x1A, 0x00, 0x01, 0x05, 0xB8, 0x1B, 0x00,
+ 0x00, 0x00, 0x01, 0x8F, 0x5A, 0xE8, 0xB8, 0x20, 0x39, 0x01, 0x00, 0x3A,
+ 0x00, 0x01, 0x05, 0xB7, 0x3B, 0x00, 0x00, 0x00, 0x01, 0x8F, 0x5A, 0xE8,
+ 0xB7, 0x5F, 0x41, 0x01, 0x41, 0x02, 0xFF, 0x7F, 0x61, 0x61, 0x61, 0x62,
+ 0xFF, 0x9F, 0xFF, 0xA1, 0x61, 0x61, 0x61, 0x62, 0xC0, 0xBF, 0xFF, 0xF9,
+ 0x3C, 0x00, 0xFA, 0x47, 0xC3, 0x50, 0x00, 0xFB, 0x7E, 0x37, 0xE4, 0x3C,
+ 0x88, 0x00, 0x75, 0x9C, 0xF6, 0xF7, 0xF5};
+
+/* Exercise the default callbacks */
+static void test_default_callbacks(void **state) {
+ size_t read = 0;
+ while (read < 79) {
+ struct cbor_decoder_result result =
+ cbor_stream_decode(data + read, 79 - read, &cbor_empty_callbacks, NULL);
+ read += result.read;
+ }
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+
+ cmocka_unit_test(test_default_callbacks)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/cbor_serialize_test.c b/test/cbor_serialize_test.c
new file mode 100644
index 000000000000..cd1f6d74ed6f
--- /dev/null
+++ b/test/cbor_serialize_test.c
@@ -0,0 +1,323 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include <math.h>
+#include <stdarg.h>
+#include <string.h>
+#include <strings.h>
+#include "cbor.h"
+
+unsigned char buffer[512];
+
+static void test_serialize_uint8(void **state) {
+ cbor_item_t *item = cbor_new_int8();
+ cbor_set_uint8(item, 0);
+ assert_int_equal(1, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, (unsigned char[]){0x00}, 1);
+ cbor_decref(&item);
+}
+
+static void test_serialize_uint16(void **state) {
+ cbor_item_t *item = cbor_new_int16();
+ cbor_set_uint16(item, 1000);
+ assert_int_equal(3, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x19, 0x03, 0xE8}), 3);
+ cbor_decref(&item);
+}
+
+static void test_serialize_uint32(void **state) {
+ cbor_item_t *item = cbor_new_int32();
+ cbor_set_uint32(item, 1000000);
+ assert_int_equal(5, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x1A, 0x00, 0x0F, 0x42, 0x40}),
+ 5);
+ cbor_decref(&item);
+}
+
+static void test_serialize_uint64(void **state) {
+ cbor_item_t *item = cbor_new_int64();
+ cbor_set_uint64(item, 1000000000000);
+ assert_int_equal(9, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(
+ buffer,
+ ((unsigned char[]){0x1B, 0x00, 0x00, 0x00, 0xE8, 0xD4, 0xA5, 0x10, 0x00}),
+ 9);
+ cbor_decref(&item);
+}
+
+static void test_serialize_negint8(void **state) {
+ cbor_item_t *item = cbor_new_int8();
+ cbor_set_uint8(item, 0);
+ cbor_mark_negint(item);
+ assert_int_equal(1, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, (unsigned char[]){0x20}, 1);
+ cbor_decref(&item);
+}
+
+static void test_serialize_negint16(void **state) {
+ cbor_item_t *item = cbor_new_int16();
+ cbor_set_uint16(item, 1000);
+ cbor_mark_negint(item);
+ assert_int_equal(3, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x39, 0x03, 0xE8}), 3);
+ cbor_decref(&item);
+}
+
+static void test_serialize_negint32(void **state) {
+ cbor_item_t *item = cbor_new_int32();
+ cbor_set_uint32(item, 1000000);
+ cbor_mark_negint(item);
+ assert_int_equal(5, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x3A, 0x00, 0x0F, 0x42, 0x40}),
+ 5);
+ cbor_decref(&item);
+}
+
+static void test_serialize_negint64(void **state) {
+ cbor_item_t *item = cbor_new_int64();
+ cbor_set_uint64(item, 1000000000000);
+ cbor_mark_negint(item);
+ assert_int_equal(9, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(
+ buffer,
+ ((unsigned char[]){0x3B, 0x00, 0x00, 0x00, 0xE8, 0xD4, 0xA5, 0x10, 0x00}),
+ 9);
+ cbor_decref(&item);
+}
+
+static void test_serialize_definite_bytestring(void **state) {
+ cbor_item_t *item = cbor_new_definite_bytestring();
+ unsigned char *data = malloc(256);
+ bzero(data, 256); /* Prevent undefined behavior in comparison */
+ cbor_bytestring_set_handle(item, data, 256);
+ assert_int_equal(256 + 3, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x59, 0x01, 0x00}), 3);
+ assert_memory_equal(buffer + 3, data, 256);
+ cbor_decref(&item);
+}
+
+static void test_serialize_indefinite_bytestring(void **state) {
+ cbor_item_t *item = cbor_new_indefinite_bytestring();
+
+ cbor_item_t *chunk = cbor_new_definite_bytestring();
+ unsigned char *data = malloc(256);
+ bzero(data, 256); /* Prevent undefined behavior in comparison */
+ cbor_bytestring_set_handle(chunk, data, 256);
+
+ cbor_bytestring_add_chunk(item, cbor_move(chunk));
+ assert_int_equal(cbor_bytestring_chunk_count(item), 1);
+
+ assert_int_equal(1 + 3 + 256 + 1, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x5F, 0x59, 0x01, 0x00}), 4);
+ assert_memory_equal(buffer + 4, data, 256);
+ assert_memory_equal(buffer + 4 + 256, ((unsigned char[]){0xFF}), 1);
+ cbor_decref(&item);
+}
+
+static void test_serialize_definite_string(void **state) {
+ cbor_item_t *item = cbor_new_definite_string();
+ unsigned char *data = malloc(12);
+ strncpy((char *)data, "Hello world!", 12);
+ cbor_string_set_handle(item, data, 12);
+ assert_int_equal(1 + 12, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(
+ buffer,
+ ((unsigned char[]){0x6C, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77, 0x6F,
+ 0x72, 0x6C, 0x64, 0x21}),
+ 13);
+ cbor_decref(&item);
+}
+
+static void test_serialize_indefinite_string(void **state) {
+ cbor_item_t *item = cbor_new_indefinite_string();
+ cbor_item_t *chunk = cbor_new_definite_string();
+
+ unsigned char *data = malloc(12);
+ strncpy((char *)data, "Hello world!", 12);
+ cbor_string_set_handle(chunk, data, 12);
+
+ assert_true(cbor_string_add_chunk(item, cbor_move(chunk)));
+ assert_int_equal(cbor_string_chunk_count(item), 1);
+
+ assert_int_equal(15, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(
+ buffer,
+ ((unsigned char[]){0x7F, 0x6C, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x77,
+ 0x6F, 0x72, 0x6C, 0x64, 0x21, 0xFF}),
+ 15);
+ cbor_decref(&item);
+}
+
+static void test_serialize_definite_array(void **state) {
+ cbor_item_t *item = cbor_new_definite_array(2);
+ cbor_item_t *one = cbor_build_uint8(1);
+ cbor_item_t *two = cbor_build_uint8(2);
+
+ cbor_array_push(item, one);
+ cbor_array_set(item, 1, two);
+ cbor_array_replace(item, 0, one);
+
+ assert_int_equal(3, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x82, 0x01, 0x02}), 3);
+ cbor_decref(&item);
+ cbor_decref(&one);
+ cbor_decref(&two);
+}
+
+static void test_serialize_indefinite_array(void **state) {
+ cbor_item_t *item = cbor_new_indefinite_array();
+ cbor_item_t *one = cbor_build_uint8(1);
+ cbor_item_t *two = cbor_build_uint8(2);
+
+ cbor_array_push(item, one);
+ cbor_array_push(item, two);
+
+ assert_int_equal(4, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x9F, 0x01, 0x02, 0xFF}), 4);
+ cbor_decref(&item);
+ cbor_decref(&one);
+ cbor_decref(&two);
+}
+
+static void test_serialize_definite_map(void **state) {
+ cbor_item_t *item = cbor_new_definite_map(2);
+ cbor_item_t *one = cbor_build_uint8(1);
+ cbor_item_t *two = cbor_build_uint8(2);
+
+ cbor_map_add(item, (struct cbor_pair){.key = one, .value = two});
+ cbor_map_add(item, (struct cbor_pair){.key = two, .value = one});
+
+ assert_int_equal(5, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0xA2, 0x01, 0x02, 0x02, 0x01}),
+ 5);
+ cbor_decref(&item);
+ cbor_decref(&one);
+ cbor_decref(&two);
+}
+
+static void test_serialize_indefinite_map(void **state) {
+ cbor_item_t *item = cbor_new_indefinite_map(2);
+ cbor_item_t *one = cbor_build_uint8(1);
+ cbor_item_t *two = cbor_build_uint8(2);
+
+ assert_true(cbor_map_add(item, (struct cbor_pair){.key = one, .value = two}));
+ assert_true(cbor_map_add(item, (struct cbor_pair){.key = two, .value = one}));
+
+ assert_int_equal(6, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(
+ buffer, ((unsigned char[]){0xBF, 0x01, 0x02, 0x02, 0x01, 0xFF}), 6);
+ cbor_decref(&item);
+ cbor_decref(&one);
+ cbor_decref(&two);
+}
+
+static void test_serialize_tags(void **state) {
+ cbor_item_t *item = cbor_new_tag(21);
+ cbor_item_t *one = cbor_build_uint8(1);
+ cbor_tag_set_item(item, one);
+
+ assert_int_equal(2, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0xD5, 0x01}), 2);
+ cbor_decref(&item);
+ cbor_decref(&one);
+}
+
+static void test_serialize_half(void **state) {
+ cbor_item_t *item = cbor_new_float2();
+ cbor_set_float2(item, NAN);
+
+ assert_int_equal(3, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0xF9, 0x7E, 0x00}), 3);
+ cbor_decref(&item);
+}
+
+static void test_serialize_single(void **state) {
+ cbor_item_t *item = cbor_new_float4();
+ cbor_set_float4(item, 100000.0f);
+
+ assert_int_equal(5, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0xFA, 0x47, 0xC3, 0x50, 0x00}),
+ 5);
+ cbor_decref(&item);
+}
+
+static void test_serialize_double(void **state) {
+ cbor_item_t *item = cbor_new_float8();
+ cbor_set_float8(item, -4.1);
+
+ assert_int_equal(9, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(
+ buffer,
+ ((unsigned char[]){0xFB, 0xC0, 0x10, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66}),
+ 9);
+ cbor_decref(&item);
+}
+
+static void test_serialize_ctrl(void **state) {
+ cbor_item_t *item = cbor_new_undef();
+
+ assert_int_equal(1, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0xF7}), 1);
+ cbor_decref(&item);
+}
+
+static void test_serialize_long_ctrl(void **state) {
+ cbor_item_t *item = cbor_new_ctrl();
+ cbor_set_ctrl(item, 254);
+
+ assert_int_equal(2, cbor_serialize(item, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0xF8, 0xFE}), 2);
+ cbor_decref(&item);
+}
+
+static void test_auto_serialize(void **state) {
+ cbor_item_t *item = cbor_new_definite_array(4);
+ for (size_t i = 0; i < 4; i++)
+ cbor_array_push(item, cbor_move(cbor_build_uint64(0)));
+
+ unsigned char *buffer;
+ size_t buffer_size;
+ assert_int_equal(37, cbor_serialize_alloc(item, &buffer, &buffer_size));
+ assert_int_equal(64, buffer_size);
+ assert_memory_equal(buffer, ((unsigned char[]){0x84, 0x1B}), 2);
+ cbor_decref(&item);
+ _CBOR_FREE(buffer);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_serialize_uint8),
+ cmocka_unit_test(test_serialize_uint16),
+ cmocka_unit_test(test_serialize_uint32),
+ cmocka_unit_test(test_serialize_uint64),
+ cmocka_unit_test(test_serialize_negint8),
+ cmocka_unit_test(test_serialize_negint16),
+ cmocka_unit_test(test_serialize_negint32),
+ cmocka_unit_test(test_serialize_negint64),
+ cmocka_unit_test(test_serialize_definite_bytestring),
+ cmocka_unit_test(test_serialize_indefinite_bytestring),
+ cmocka_unit_test(test_serialize_definite_string),
+ cmocka_unit_test(test_serialize_indefinite_string),
+ cmocka_unit_test(test_serialize_definite_array),
+ cmocka_unit_test(test_serialize_indefinite_array),
+ cmocka_unit_test(test_serialize_definite_map),
+ cmocka_unit_test(test_serialize_indefinite_map),
+ cmocka_unit_test(test_serialize_tags),
+ cmocka_unit_test(test_serialize_half),
+ cmocka_unit_test(test_serialize_single),
+ cmocka_unit_test(test_serialize_double),
+ cmocka_unit_test(test_serialize_ctrl),
+ cmocka_unit_test(test_serialize_long_ctrl),
+ cmocka_unit_test(test_auto_serialize)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/cbor_stream_decode_test.c b/test/cbor_stream_decode_test.c
new file mode 100644
index 000000000000..1d90bc7fdd76
--- /dev/null
+++ b/test/cbor_stream_decode_test.c
@@ -0,0 +1,599 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "assertions.h"
+#include "cbor.h"
+#include "stream_expectations.h"
+
+static void test_no_data(void **state) {
+ assert_decoder_result_nedata(1, decode(NULL, 0));
+}
+
+unsigned char embedded_uint8_data[] = {0x00, 0x01, 0x05, 0x17};
+static void test_uint8_embedded_decoding(void **state) {
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(embedded_uint8_data, 1));
+
+ assert_uint8_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(embedded_uint8_data + 1, 1));
+
+ assert_uint8_eq(5);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(embedded_uint8_data + 2, 1));
+
+ assert_uint8_eq(23);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(embedded_uint8_data + 3, 1));
+}
+
+unsigned char uint8_data[] = {0x18, 0x83, 0x18, 0xFF};
+static void test_uint8_decoding(void **state) {
+ assert_uint8_eq(0x83);
+ assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(uint8_data, 2));
+
+ assert_uint8_eq(0xFF);
+ assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(uint8_data + 2, 2));
+
+ assert_minimum_input_size(2, uint8_data);
+}
+
+unsigned char uint16_data[] = {0x19, 0x01, 0xf4};
+static void test_uint16_decoding(void **state) {
+ assert_uint16_eq(500);
+ assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(uint16_data, 3));
+
+ assert_minimum_input_size(3, uint16_data);
+}
+
+unsigned char uint32_data[] = {0x1a, 0xa5, 0xf7, 0x02, 0xb3};
+static void test_uint32_decoding(void **state) {
+ assert_uint32_eq((uint32_t)2784428723UL);
+ assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(uint32_data, 5));
+
+ assert_minimum_input_size(5, uint32_data);
+}
+
+unsigned char uint64_data[] = {0x1b, 0xa5, 0xf7, 0x02, 0xb3,
+ 0xa5, 0xf7, 0x02, 0xb3};
+static void test_uint64_decoding(void **state) {
+ assert_uint64_eq(11959030306112471731ULL);
+ assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(uint64_data, 9));
+
+ assert_minimum_input_size(9, uint64_data);
+}
+
+unsigned char embedded_negint8_data[] = {0x20, 0x21, 0x25, 0x37};
+static void test_negint8_embedded_decoding(void **state) {
+ assert_negint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(embedded_negint8_data, 1));
+
+ assert_negint8_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(embedded_negint8_data + 1, 1));
+
+ assert_negint8_eq(5);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(embedded_negint8_data + 2, 1));
+
+ assert_negint8_eq(23);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(embedded_negint8_data + 3, 1));
+}
+
+unsigned char negint8_data[] = {0x38, 0x83, 0x38, 0xFF};
+static void test_negint8_decoding(void **state) {
+ assert_negint8_eq(0x83);
+ assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(negint8_data, 2));
+
+ assert_negint8_eq(0xFF);
+ assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(negint8_data + 2, 2));
+
+ assert_minimum_input_size(2, negint8_data);
+}
+
+unsigned char negint16_data[] = {0x39, 0x01, 0xf4};
+static void test_negint16_decoding(void **state) {
+ assert_negint16_eq(500);
+ assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(negint16_data, 3));
+
+ assert_minimum_input_size(3, negint16_data);
+}
+
+unsigned char negint32_data[] = {0x3a, 0xa5, 0xf7, 0x02, 0xb3};
+static void test_negint32_decoding(void **state) {
+ assert_negint32_eq((uint32_t)2784428723UL);
+ assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(negint32_data, 5));
+
+ assert_minimum_input_size(5, negint32_data);
+}
+
+unsigned char negint64_data[] = {0x3b, 0xa5, 0xf7, 0x02, 0xb3,
+ 0xa5, 0xf7, 0x02, 0xb3};
+static void test_negint64_decoding(void **state) {
+ assert_negint64_eq(11959030306112471731ULL);
+ assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(negint64_data, 9));
+
+ assert_minimum_input_size(9, negint64_data);
+}
+
+unsigned char bstring_embedded_int8_data[] = {0x41, 0xFF};
+static void test_bstring_embedded_int8_decoding(void **state) {
+ assert_bstring_mem_eq(bstring_embedded_int8_data + 1, 1);
+ assert_decoder_result(2, CBOR_DECODER_FINISHED,
+ decode(bstring_embedded_int8_data, 2));
+
+ assert_minimum_input_size(2, bstring_embedded_int8_data);
+}
+
+// TODO: Add tests with actual bstring/string chunks
+
+unsigned char bstring_int8_data[] = {0x58, 0x00};
+static void test_bstring_int8_decoding(void **state) {
+ assert_bstring_mem_eq(bstring_int8_data + 2, 0);
+ assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(bstring_int8_data, 2));
+
+ assert_minimum_input_size(2, bstring_int8_data);
+}
+
+unsigned char bstring_int16_data[] = {0x59, 0x01, 0x5C /*, [348 bytes] */};
+static void test_bstring_int16_decoding(void **state) {
+ assert_bstring_mem_eq(bstring_int16_data + 3, 348);
+ assert_decoder_result(3 + 348, CBOR_DECODER_FINISHED,
+ decode(bstring_int16_data, 3 + 348));
+
+ assert_minimum_input_size(3, bstring_int16_data);
+ assert_decoder_result_nedata(3 + 348, decode(bstring_int16_data, 3));
+}
+
+unsigned char bstring_int32_data[] = {0x5A, 0x00, 0x10, 0x10,
+ 0x10 /*, [1052688 bytes] */};
+static void test_bstring_int32_decoding(void **state) {
+ assert_bstring_mem_eq(bstring_int32_data + 5, 1052688);
+ assert_decoder_result(5 + 1052688, CBOR_DECODER_FINISHED,
+ decode(bstring_int32_data, 5 + 1052688));
+
+ assert_minimum_input_size(5, bstring_int32_data);
+ assert_decoder_result_nedata(5 + 1052688, decode(bstring_int32_data, 5));
+}
+
+#ifdef EIGHT_BYTE_SIZE_T
+unsigned char bstring_int64_data[] = {
+ 0x5B, 0x00, 0x00, 0x00, 0x01,
+ 0x00, 0x00, 0x00, 0x00 /*, [4294967296 bytes] */};
+static void test_bstring_int64_decoding(void **state) {
+ assert_bstring_mem_eq(bstring_int64_data + 9, 4294967296);
+ assert_decoder_result(9 + 4294967296, CBOR_DECODER_FINISHED,
+ decode(bstring_int64_data, 9 + 4294967296));
+
+ assert_minimum_input_size(9, bstring_int64_data);
+ assert_decoder_result_nedata(9 + 4294967296, decode(bstring_int64_data, 9));
+}
+#endif
+
+unsigned char bstring_indef_1_data[] = {0x5F, 0x40 /* Empty byte string */,
+ 0xFF};
+static void test_bstring_indef_decoding_1(void **state) {
+ assert_bstring_indef_start();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(bstring_indef_1_data, 3));
+
+ assert_bstring_mem_eq(bstring_indef_1_data + 2, 0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(bstring_indef_1_data + 1, 2));
+
+ assert_indef_break();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(bstring_indef_1_data + 2, 1));
+}
+
+unsigned char bstring_indef_2_data[] = {0x5F, 0xFF};
+static void test_bstring_indef_decoding_2(void **state) {
+ assert_bstring_indef_start();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(bstring_indef_2_data, 2));
+
+ assert_indef_break();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(bstring_indef_2_data + 1, 1));
+}
+
+// TODO: Comment formatting seems weird
+unsigned char bstring_indef_3_data[] = {
+ 0x5F, 0x40 /* Empty byte string */, 0x58,
+ 0x01, 0xFF /* 1B 1 char bytes string */, 0xFF};
+static void test_bstring_indef_decoding_3(void **state) {
+ assert_bstring_indef_start();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(bstring_indef_3_data, 6));
+
+ assert_bstring_mem_eq(bstring_indef_3_data + 2, 0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(bstring_indef_3_data + 1, 5));
+
+ assert_bstring_mem_eq(bstring_indef_3_data + 4, 1);
+ assert_decoder_result(3, CBOR_DECODER_FINISHED,
+ decode(bstring_indef_3_data + 2, 4));
+
+ assert_indef_break();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(bstring_indef_3_data + 5, 1));
+}
+
+unsigned char array_embedded_int8_data[] = {0x80};
+static void test_array_embedded_int8_decoding(void **state) {
+ assert_array_start(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_embedded_int8_data, 1));
+}
+
+unsigned char array_int8_data[] = {0x98, 0x02, 0x00, 0x01};
+static void test_array_int8_decoding(void **state) {
+ assert_array_start(2);
+ assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(array_int8_data, 4));
+
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_int8_data + 2, 2));
+
+ assert_uint8_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_int8_data + 3, 1));
+
+ assert_minimum_input_size(2, array_int8_data);
+}
+
+unsigned char array_int16_data[] = {0x99, 0x00, 0x02, 0x00, 0x01};
+static void test_array_int16_decoding(void **state) {
+ assert_array_start(2);
+ assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(array_int16_data, 5));
+
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_int16_data + 3, 2));
+
+ assert_uint8_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_int16_data + 4, 1));
+
+ assert_minimum_input_size(3, array_int16_data);
+}
+
+unsigned char array_int32_data[] = {0x9A, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01};
+static void test_array_int32_decoding(void **state) {
+ assert_array_start(2);
+ assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(array_int32_data, 7));
+
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_int32_data + 5, 2));
+
+ assert_uint8_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_int32_data + 6, 1));
+
+ assert_minimum_input_size(5, array_int32_data);
+}
+
+unsigned char array_int64_data[] = {0x9B, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x02, 0x00, 0x01};
+static void test_array_int64_decoding(void **state) {
+ assert_array_start(2);
+ assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(array_int64_data, 11));
+
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_int64_data + 9, 2));
+
+ assert_uint8_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_int64_data + 10, 1));
+
+ assert_minimum_input_size(9, array_int64_data);
+}
+
+unsigned char array_of_arrays_data[] = {0x82, 0x80, 0x80};
+static void test_array_of_arrays_decoding(void **state) {
+ assert_array_start(2);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_of_arrays_data, 3));
+
+ assert_array_start(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_of_arrays_data + 1, 2));
+
+ assert_array_start(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(array_of_arrays_data + 2, 1));
+}
+
+unsigned char indef_array_data_1[] = {0x9F, 0x00, 0x18, 0xFF, 0x9F, 0xFF, 0xFF};
+static void test_indef_array_decoding_1(void **state) {
+ assert_indef_array_start();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(indef_array_data_1, 7));
+
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(indef_array_data_1 + 1, 6));
+
+ assert_uint8_eq(255);
+ assert_decoder_result(2, CBOR_DECODER_FINISHED,
+ decode(indef_array_data_1 + 2, 4));
+
+ assert_indef_array_start();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(indef_array_data_1 + 4, 3));
+
+ assert_indef_break();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(indef_array_data_1 + 5, 2));
+
+ assert_indef_break();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(indef_array_data_1 + 6, 1));
+}
+
+unsigned char map_embedded_int8_data[] = {0xa1, 0x01, 0x00};
+static void test_map_embedded_int8_decoding(void **state) {
+ assert_map_start(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(map_embedded_int8_data, 3));
+
+ assert_uint8_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(map_embedded_int8_data + 1, 2));
+
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(map_embedded_int8_data + 2, 1));
+}
+
+unsigned char map_int8_data[] = {0xB8, 0x01, 0x00, 0x01};
+static void test_map_int8_decoding(void **state) {
+ assert_map_start(1);
+ assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(map_int8_data, 4));
+
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(map_int8_data + 2, 2));
+
+ assert_uint8_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(map_int8_data + 3, 1));
+
+ assert_minimum_input_size(2, map_int8_data);
+}
+
+unsigned char map_int16_data[] = {0xB9, 0x00, 0x01, 0x00, 0x01};
+static void test_map_int16_decoding(void **state) {
+ assert_map_start(1);
+ assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(map_int16_data, 5));
+
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(map_int16_data + 3, 2));
+
+ assert_uint8_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(map_int16_data + 4, 1));
+
+ assert_minimum_input_size(3, map_int16_data);
+}
+
+unsigned char map_int32_data[] = {0xBA, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01};
+static void test_map_int32_decoding(void **state) {
+ assert_map_start(1);
+ assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(map_int32_data, 7));
+
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(map_int32_data + 5, 2));
+
+ assert_uint8_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(map_int32_data + 6, 1));
+
+ assert_minimum_input_size(5, map_int32_data);
+}
+
+unsigned char map_int64_data[] = {0xBB, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x01, 0x00, 0x01};
+static void test_map_int64_decoding(void **state) {
+ assert_map_start(1);
+ assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(map_int64_data, 11));
+
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(map_int64_data + 9, 2));
+
+ assert_uint8_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(map_int64_data + 10, 1));
+
+ assert_minimum_input_size(9, map_int64_data);
+}
+
+unsigned char indef_map_data_1[] = {0xBF, 0x00, 0x18, 0xFF, 0xFF};
+static void test_indef_map_decoding_1(void **state) {
+ assert_indef_map_start();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(indef_map_data_1, 5));
+
+ assert_uint8_eq(0);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(indef_map_data_1 + 1, 4));
+
+ assert_uint8_eq(255);
+ assert_decoder_result(2, CBOR_DECODER_FINISHED,
+ decode(indef_map_data_1 + 2, 3));
+
+ assert_indef_break();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED,
+ decode(indef_map_data_1 + 4, 1));
+}
+
+unsigned char embedded_tag_data[] = {0xC1};
+static void test_embedded_tag_decoding(void **state) {
+ assert_tag_eq(1);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(embedded_tag_data, 1));
+}
+
+unsigned char int8_tag_data[] = {0xD8, 0xFE};
+static void test_int8_tag_decoding(void **state) {
+ assert_tag_eq(254);
+ assert_decoder_result(2, CBOR_DECODER_FINISHED, decode(int8_tag_data, 2));
+
+ assert_minimum_input_size(2, int8_tag_data);
+}
+
+unsigned char int16_tag_data[] = {0xD9, 0xFE, 0xFD};
+static void test_int16_tag_decoding(void **state) {
+ assert_tag_eq(65277);
+ assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(int16_tag_data, 3));
+
+ assert_minimum_input_size(3, int16_tag_data);
+}
+
+unsigned char int32_tag_data[] = {0xDA, 0xFE, 0xFD, 0xFC, 0xFB};
+static void test_int32_tag_decoding(void **state) {
+ assert_tag_eq(4278058235ULL);
+ assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(int32_tag_data, 5));
+
+ assert_minimum_input_size(5, int32_tag_data);
+}
+
+unsigned char int64_tag_data[] = {0xDB, 0xFE, 0xFD, 0xFC, 0xFB,
+ 0xFA, 0xF9, 0xF8, 0xF7};
+static void test_int64_tag_decoding(void **state) {
+ assert_tag_eq(18374120213919168759ULL);
+ assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(int64_tag_data, 9));
+
+ assert_minimum_input_size(9, int64_tag_data);
+}
+
+unsigned char bad_tag_data[] = {0xC6};
+static void test_bad_tag_decoding(void **state) {
+ assert_decoder_result(0, CBOR_DECODER_ERROR, decode(bad_tag_data, 1));
+}
+
+unsigned char float2_data[] = {0xF9, 0x7B, 0xFF};
+static void test_float2_decoding(void **state) {
+ assert_half(65504.0f);
+ assert_decoder_result(3, CBOR_DECODER_FINISHED, decode(float2_data, 3));
+
+ assert_minimum_input_size(3, float2_data);
+}
+
+unsigned char float4_data[] = {0xFA, 0x47, 0xC3, 0x50, 0x00};
+static void test_float4_decoding(void **state) {
+ assert_float(100000.0f);
+ assert_decoder_result(5, CBOR_DECODER_FINISHED, decode(float4_data, 5));
+
+ assert_minimum_input_size(5, float4_data);
+}
+
+unsigned char float8_data[] = {0xFB, 0xC0, 0x10, 0x66, 0x66,
+ 0x66, 0x66, 0x66, 0x66};
+static void test_float8_decoding(void **state) {
+ assert_double(-4.1);
+ assert_decoder_result(9, CBOR_DECODER_FINISHED, decode(float8_data, 9));
+
+ assert_minimum_input_size(0, float8_data);
+}
+
+unsigned char false_data[] = {0xF4};
+static void test_false_decoding(void **state) {
+ assert_bool(false);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(false_data, 1));
+}
+
+unsigned char true_data[] = {0xF5};
+static void test_true_decoding(void **state) {
+ assert_bool(true);
+ assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(true_data, 1));
+}
+
+unsigned char null_data[] = {0xF6};
+static void test_null_decoding(void **state) {
+ assert_nil();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(null_data, 1));
+}
+
+unsigned char undef_data[] = {0xF7};
+static void test_undef_decoding(void **state) {
+ assert_undef();
+ assert_decoder_result(1, CBOR_DECODER_FINISHED, decode(undef_data, 1));
+}
+
+#define stream_test(f) cmocka_unit_test_teardown(f, clear_stream_assertions)
+
+int main(void) {
+ set_decoder(&cbor_stream_decode);
+ const struct CMUnitTest tests[] = {
+ stream_test(test_no_data),
+
+ stream_test(test_uint8_embedded_decoding),
+ stream_test(test_uint8_decoding),
+ stream_test(test_uint16_decoding),
+ stream_test(test_uint32_decoding),
+ stream_test(test_uint64_decoding),
+
+ stream_test(test_negint8_embedded_decoding),
+ stream_test(test_negint8_decoding),
+ stream_test(test_negint16_decoding),
+ stream_test(test_negint32_decoding),
+ stream_test(test_negint64_decoding),
+
+ stream_test(test_bstring_embedded_int8_decoding),
+ stream_test(test_bstring_int8_decoding),
+ stream_test(test_bstring_int16_decoding),
+ stream_test(test_bstring_int32_decoding),
+#ifdef EIGHT_BYTE_SIZE_T
+ stream_test(test_bstring_int64_decoding),
+#endif
+ stream_test(test_bstring_indef_decoding_1),
+ stream_test(test_bstring_indef_decoding_2),
+ stream_test(test_bstring_indef_decoding_3),
+
+ stream_test(test_array_embedded_int8_decoding),
+ stream_test(test_array_int8_decoding),
+ stream_test(test_array_int16_decoding),
+ stream_test(test_array_int32_decoding),
+ stream_test(test_array_int64_decoding),
+ stream_test(test_array_of_arrays_decoding),
+ stream_test(test_indef_array_decoding_1),
+
+ stream_test(test_map_embedded_int8_decoding),
+ stream_test(test_map_int8_decoding),
+ stream_test(test_map_int16_decoding),
+ stream_test(test_map_int32_decoding),
+ stream_test(test_map_int64_decoding),
+ stream_test(test_indef_map_decoding_1),
+
+ stream_test(test_embedded_tag_decoding),
+ stream_test(test_int8_tag_decoding),
+ stream_test(test_int16_tag_decoding),
+ stream_test(test_int32_tag_decoding),
+ stream_test(test_int64_tag_decoding),
+ stream_test(test_bad_tag_decoding),
+
+ stream_test(test_float2_decoding),
+ stream_test(test_float4_decoding),
+ stream_test(test_float8_decoding),
+
+ stream_test(test_false_decoding),
+ stream_test(test_true_decoding),
+ stream_test(test_null_decoding),
+ stream_test(test_undef_decoding)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/copy_test.c b/test/copy_test.c
new file mode 100644
index 000000000000..91651390aebb
--- /dev/null
+++ b/test/copy_test.c
@@ -0,0 +1,207 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "assertions.h"
+#include "cbor.h"
+
+cbor_item_t *item, *copy, *tmp;
+
+static void test_uints(void **state) {
+ item = cbor_build_uint8(10);
+ assert_uint8(copy = cbor_copy(item), 10);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+
+ item = cbor_build_uint16(10);
+ assert_uint16(copy = cbor_copy(item), 10);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+
+ item = cbor_build_uint32(10);
+ assert_uint32(copy = cbor_copy(item), 10);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+
+ item = cbor_build_uint64(10);
+ assert_uint64(copy = cbor_copy(item), 10);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+}
+
+static void test_negints(void **state) {
+ item = cbor_build_negint8(10);
+ assert_true(cbor_get_uint8(copy = cbor_copy(item)) == 10);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+
+ item = cbor_build_negint16(10);
+ assert_true(cbor_get_uint16(copy = cbor_copy(item)) == 10);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+
+ item = cbor_build_negint32(10);
+ assert_true(cbor_get_uint32(copy = cbor_copy(item)) == 10);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+
+ item = cbor_build_negint64(10);
+ assert_true(cbor_get_uint64(copy = cbor_copy(item)) == 10);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+}
+
+static void test_def_bytestring(void **state) {
+ item = cbor_build_bytestring((cbor_data) "abc", 3);
+ assert_memory_equal(cbor_bytestring_handle(copy = cbor_copy(item)),
+ cbor_bytestring_handle(item), 3);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+}
+
+static void test_indef_bytestring(void **state) {
+ item = cbor_new_indefinite_bytestring();
+ cbor_bytestring_add_chunk(
+ item, cbor_move(cbor_build_bytestring((cbor_data) "abc", 3)));
+ copy = cbor_copy(item);
+
+ assert_int_equal(cbor_bytestring_chunk_count(item),
+ cbor_bytestring_chunk_count(copy));
+
+ assert_memory_equal(
+ cbor_bytestring_handle(cbor_bytestring_chunks_handle(copy)[0]), "abc", 3);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+}
+
+static void test_def_string(void **state) {
+ item = cbor_build_string("abc");
+ assert_memory_equal(cbor_string_handle(copy = cbor_copy(item)),
+ cbor_string_handle(item), 3);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+}
+
+static void test_indef_string(void **state) {
+ item = cbor_new_indefinite_string();
+ cbor_string_add_chunk(item, cbor_move(cbor_build_string("abc")));
+ copy = cbor_copy(item);
+
+ assert_int_equal(cbor_string_chunk_count(item),
+ cbor_string_chunk_count(copy));
+
+ assert_memory_equal(cbor_string_handle(cbor_string_chunks_handle(copy)[0]),
+ "abc", 3);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+}
+
+static void test_def_array(void **state) {
+ item = cbor_new_definite_array(1);
+ cbor_array_push(item, cbor_move(cbor_build_uint8(42)));
+
+ assert_uint8(tmp = cbor_array_get(copy = cbor_copy(item), 0), 42);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+ cbor_decref(&tmp);
+}
+
+static void test_indef_array(void **state) {
+ item = cbor_new_indefinite_array();
+ cbor_array_push(item, cbor_move(cbor_build_uint8(42)));
+
+ assert_uint8(tmp = cbor_array_get(copy = cbor_copy(item), 0), 42);
+ cbor_decref(&item);
+ cbor_decref(&copy);
+ cbor_decref(&tmp);
+}
+
+static void test_def_map(void **state) {
+ item = cbor_new_definite_map(1);
+ cbor_map_add(item, (struct cbor_pair){
+ .key = cbor_move(cbor_build_uint8(42)),
+ .value = cbor_move(cbor_build_uint8(43)),
+ });
+
+ assert_uint8(cbor_map_handle(copy = cbor_copy(item))[0].key, 42);
+
+ cbor_decref(&item);
+ cbor_decref(&copy);
+}
+
+static void test_indef_map(void **state) {
+ item = cbor_new_indefinite_map(1);
+ cbor_map_add(item, (struct cbor_pair){
+ .key = cbor_move(cbor_build_uint8(42)),
+ .value = cbor_move(cbor_build_uint8(43)),
+ });
+
+ assert_uint8(cbor_map_handle(copy = cbor_copy(item))[0].key, 42);
+
+ cbor_decref(&item);
+ cbor_decref(&copy);
+}
+
+static void test_tag(void **state) {
+ item = cbor_build_tag(10, cbor_move(cbor_build_uint8(42)));
+
+ assert_uint8(cbor_move(cbor_tag_item(copy = cbor_copy(item))), 42);
+
+ cbor_decref(&item);
+ cbor_decref(&copy);
+}
+
+static void test_ctrls(void **state) {
+ item = cbor_new_null();
+ assert_true(cbor_is_null(copy = cbor_copy(item)));
+ cbor_decref(&item);
+ cbor_decref(&copy);
+}
+
+static void test_floats(void **state) {
+ item = cbor_build_float2(3.14f);
+ assert_true(cbor_float_get_float2(copy = cbor_copy(item)) ==
+ cbor_float_get_float2(item));
+ cbor_decref(&item);
+ cbor_decref(&copy);
+
+ item = cbor_build_float4(3.14f);
+ assert_true(cbor_float_get_float4(copy = cbor_copy(item)) ==
+ cbor_float_get_float4(item));
+ cbor_decref(&item);
+ cbor_decref(&copy);
+
+ item = cbor_build_float8(3.14);
+ assert_true(cbor_float_get_float8(copy = cbor_copy(item)) ==
+ cbor_float_get_float8(item));
+ cbor_decref(&item);
+ cbor_decref(&copy);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+
+ cmocka_unit_test(test_uints),
+ cmocka_unit_test(test_negints),
+ cmocka_unit_test(test_def_bytestring),
+ cmocka_unit_test(test_indef_bytestring),
+ cmocka_unit_test(test_def_string),
+ cmocka_unit_test(test_indef_string),
+ cmocka_unit_test(test_def_array),
+ cmocka_unit_test(test_indef_array),
+ cmocka_unit_test(test_def_map),
+ cmocka_unit_test(test_indef_map),
+ cmocka_unit_test(test_tag),
+ cmocka_unit_test(test_ctrls),
+ cmocka_unit_test(test_floats)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/cpp_linkage_test.cpp b/test/cpp_linkage_test.cpp
new file mode 100644
index 000000000000..a37351f0bc83
--- /dev/null
+++ b/test/cpp_linkage_test.cpp
@@ -0,0 +1,11 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <iostream>
+#include "cbor.h"
+
+int main() { std::cout << "Hello from libcbor " << CBOR_VERSION << std::endl; }
diff --git a/test/data/callbacks.cbor b/test/data/callbacks.cbor
new file mode 100644
index 000000000000..6903268bc8d7
--- /dev/null
+++ b/test/data/callbacks.cbor
Binary files differ
diff --git a/test/data/callbacks.diag b/test/data/callbacks.diag
new file mode 100644
index 000000000000..0dd611fa968b
--- /dev/null
+++ b/test/data/callbacks.diag
@@ -0,0 +1 @@
+[1, 257, 67000, 6700067000, -1, -257, -67000, -6700067000, (_ h'01', h'02'), (_ "a", "b"), [_], {"a":"b"}, 0({_}), 1.0, 100000.0, 1.0e+300 , null, undefined, true]
diff --git a/test/data/pretty.cbor b/test/data/pretty.cbor
new file mode 100644
index 000000000000..e580dcd648ff
--- /dev/null
+++ b/test/data/pretty.cbor
@@ -0,0 +1,3 @@
+*** can't parse 101
+0x5B, 0x31, 0x2C, 0x20, 0x2D, 0x31, 0x2C, 0x20, 0x28, 0x5F, 0x20, 0x68, 0x27, 0x30, 0x31, 0x27, 0x2C, 0x20, 0x68, 0x27, 0x30, 0x32, 0x27, 0x29, 0x2C, 0x20, 0x28, 0x5F, 0x20, 0x22, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x22, 0x2C, 0x20, 0x22, 0x62, 0x22, 0x29, 0x2C, 0x20, 0x5B, 0x5F, 0x5D, 0x2C, 0x20, 0x7B, 0x22, 0x61, 0x62, 0x63, 0x22, 0x3A, 0x22, 0x62, 0x61, 0x22, 0x7D, 0x2C, 0x20, 0x30, 0x28, 0x7B, 0x5F, 0x7D, 0x29, 0x2C, 0x20, 0x33, 0x2E, 0x31, 0x34, 0x2C, 0x20, 0x6E, 0x75, 0x6C, 0x6C, 0x2C, 0x20, 0x75, 0x6E, 0x64, 0x65, 0x66, 0x69, 0x6E, 0x65, 0x64, 0x2C, 0x20, 0x74, 0x72, 0x75, 0x65, 0x5D, 0x0A
+*** Expected [ \t\n\r] at line 2, column 1 (byte 5) after
diff --git a/test/data/pretty.diag b/test/data/pretty.diag
new file mode 100644
index 000000000000..8be1b0cc2a42
--- /dev/null
+++ b/test/data/pretty.diag
@@ -0,0 +1 @@
+[1, -1, (_ h'01', h'02'), (_ "abcdef", "b"), [_], {"abc":"ba"}, 0({_}), 3.14, null, undefined, true]
diff --git a/test/fuzz_test.c b/test/fuzz_test.c
new file mode 100644
index 000000000000..885424107bf1
--- /dev/null
+++ b/test/fuzz_test.c
@@ -0,0 +1,87 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include <time.h>
+#include "cbor.h"
+
+#ifdef HUGE_FUZZ
+#define ROUNDS 65536ULL
+#define MAXLEN 131072ULL
+#else
+#define ROUNDS 256ULL
+#define MAXLEN 2048ULL
+#endif
+
+#ifdef PRINT_FUZZ
+static void printmem(const unsigned char *ptr, size_t length) {
+ for (size_t i = 0; i < length; i++) printf("%02X", ptr[i]);
+ printf("\n");
+}
+#endif
+
+unsigned seed;
+
+#if CBOR_CUSTOM_ALLOC
+void *mock_malloc(size_t size) {
+ if (size > (1 << 19))
+ return NULL;
+ else
+ return malloc(size);
+}
+#endif
+
+static void run_round() {
+ cbor_item_t *item;
+ struct cbor_load_result res;
+
+ size_t length = rand() % MAXLEN + 1;
+ unsigned char *data = malloc(length);
+ for (size_t i = 0; i < length; i++) {
+ data[i] = rand() % 0xFF;
+ }
+
+#ifdef PRINT_FUZZ
+ printmem(data, length);
+#endif
+
+ item = cbor_load(data, length, &res);
+
+ if (res.error.code == CBOR_ERR_NONE) cbor_decref(&item);
+ /* Otherwise there should be nothing left behind by the decoder */
+
+ free(data);
+}
+
+static void fuzz(void **state) {
+#if CBOR_CUSTOM_ALLOC
+ cbor_set_allocs(mock_malloc, realloc, free);
+#endif
+ printf("Fuzzing %llu rounds of up to %llu bytes with seed %u\n", ROUNDS,
+ MAXLEN, seed);
+ srand(seed);
+
+ for (size_t i = 0; i < ROUNDS; i++) run_round();
+
+ printf("Successfully fuzzed through %llu kB of data\n",
+ (ROUNDS * MAXLEN) / 1024);
+}
+
+int main(int argc, char *argv[]) {
+ if (argc > 1)
+ seed = (unsigned)strtoul(argv[1], NULL, 10);
+ else
+ seed = (unsigned)time(NULL);
+
+ const struct CMUnitTest tests[] = {cmocka_unit_test(fuzz)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/memory_allocation_test.c b/test/memory_allocation_test.c
new file mode 100644
index 000000000000..991c16f87b33
--- /dev/null
+++ b/test/memory_allocation_test.c
@@ -0,0 +1,277 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+// This test simulates cases when malloc unexpectedly fails and leaves a
+// possibly partially constructed object behind. It this is especially useful
+// in conjunction with the memory correctness check.
+//
+// WARNING: The test only works with CBOR_CUSTOM_ALLOC
+
+typedef enum call_expectation {
+ MALLOC,
+ MALLOC_FAIL,
+ REALLOC,
+ REALLOC_FAIL
+} call_expectation;
+
+// How many alloc calls we expect
+int alloc_calls_expected;
+// How many alloc calls we got
+int alloc_calls;
+// Array of booleans indicating whether to return a block or fail with NULL
+call_expectation *expectations;
+
+void set_mock_malloc(int calls, ...) {
+ va_list args;
+ va_start(args, calls);
+ alloc_calls_expected = calls;
+ alloc_calls = 0;
+ expectations = calloc(calls, sizeof(expectations));
+ for (int i = 0; i < calls; i++) {
+ // Promotable types, baby
+ expectations[i] = va_arg(args, call_expectation);
+ }
+ va_end(args);
+}
+
+void finalize_mock_malloc() {
+ assert_int_equal(alloc_calls, alloc_calls_expected);
+ free(expectations);
+}
+
+void *instrumented_malloc(size_t size) {
+ if (alloc_calls >= alloc_calls_expected) {
+ goto error;
+ }
+
+ if (expectations[alloc_calls] == MALLOC) {
+ alloc_calls++;
+ return malloc(size);
+ } else if (expectations[alloc_calls] == MALLOC_FAIL) {
+ alloc_calls++;
+ return NULL;
+ }
+
+error:
+ print_error("Unexpected call to malloc");
+ fail();
+ return NULL;
+}
+
+void *instrumented_realloc(void *ptr, size_t size) {
+ if (alloc_calls >= alloc_calls_expected) {
+ goto error;
+ }
+
+ if (expectations[alloc_calls] == REALLOC) {
+ alloc_calls++;
+ return realloc(ptr, size);
+ } else if (expectations[alloc_calls] == REALLOC_FAIL) {
+ alloc_calls++;
+ return NULL;
+ }
+
+error:
+ print_error("Unexpected call to realloc");
+ fail();
+ return NULL;
+}
+
+#define WITH_MOCK_MALLOC(block, malloc_calls, ...) \
+ do { \
+ set_mock_malloc(malloc_calls, __VA_ARGS__); \
+ block; \
+ finalize_mock_malloc(); \
+ } while (0)
+
+#define WITH_FAILING_MALLOC(block) WITH_MOCK_MALLOC(block, 1, MALLOC_FAIL)
+
+static void test_int_creation(void **state) {
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_int8()); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_int16()); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_int32()); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_int64()); });
+
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_uint8(0xFF)); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_uint16(0xFF)); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_uint32(0xFF)); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_uint64(0xFF)); });
+
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_negint8(0xFF)); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_negint16(0xFF)); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_negint32(0xFF)); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_negint64(0xFF)); });
+}
+
+static void test_bytestring_creation(void **state) {
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_definite_bytestring()); });
+
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_indefinite_bytestring()); });
+ WITH_MOCK_MALLOC({ assert_null(cbor_new_indefinite_bytestring()); }, 2,
+ MALLOC, MALLOC_FAIL);
+
+ unsigned char bytes[] = {0, 0, 0xFF, 0xAB};
+
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_bytestring(bytes, 4)); });
+ WITH_MOCK_MALLOC({ assert_null(cbor_build_bytestring(bytes, 4)); }, 2, MALLOC,
+ MALLOC_FAIL);
+}
+
+static void test_string_creation(void **state) {
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_definite_string()); });
+
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_indefinite_string()); });
+ WITH_MOCK_MALLOC({ assert_null(cbor_new_indefinite_string()); }, 2, MALLOC,
+ MALLOC_FAIL);
+
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_string("Test")); });
+ WITH_MOCK_MALLOC({ assert_null(cbor_build_string("Test")); }, 2, MALLOC,
+ MALLOC_FAIL);
+
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_stringn("Test", 4)); });
+ WITH_MOCK_MALLOC({ assert_null(cbor_build_stringn("Test", 4)); }, 2, MALLOC,
+ MALLOC_FAIL);
+}
+
+static void test_array_creation(void **state) {
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_definite_array(42)); });
+ WITH_MOCK_MALLOC({ assert_null(cbor_new_definite_array(42)); }, 2, MALLOC,
+ MALLOC_FAIL);
+
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_indefinite_array()); });
+}
+
+static void test_map_creation(void **state) {
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_definite_map(42)); });
+ WITH_MOCK_MALLOC({ assert_null(cbor_new_definite_map(42)); }, 2, MALLOC,
+ MALLOC_FAIL);
+
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_indefinite_map()); });
+}
+
+static void test_tag_creation(void **state) {
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_tag(42)); });
+}
+
+static void test_float_ctrl_creation(void **state) {
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_ctrl()); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_float2()); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_float4()); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_float8()); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_null()); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_new_undef()); });
+
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_bool(false)); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_float2(3.14)); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_float4(3.14)); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_float8(3.14)); });
+ WITH_FAILING_MALLOC({ assert_null(cbor_build_ctrl(0xAF)); });
+}
+
+static void test_bytestring_add_chunk(void **state) {
+ unsigned char bytes[] = {0, 0, 0xFF, 0xAB};
+ WITH_MOCK_MALLOC(
+ {
+ cbor_item_t *bytestring = cbor_new_indefinite_bytestring();
+ cbor_item_t *chunk = cbor_build_bytestring(bytes, 4);
+
+ assert_false(cbor_bytestring_add_chunk(bytestring, chunk));
+ assert_int_equal(cbor_bytestring_chunk_count(bytestring), 0);
+ assert_int_equal(
+ ((struct cbor_indefinite_string_data *)bytestring->data)
+ ->chunk_capacity,
+ 0);
+
+ cbor_decref(&chunk);
+ cbor_decref(&bytestring);
+ },
+ 5, MALLOC, MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
+}
+
+static void test_string_add_chunk(void **state) {
+ WITH_MOCK_MALLOC(
+ {
+ cbor_item_t *string = cbor_new_indefinite_string();
+ cbor_item_t *chunk = cbor_build_string("Hello!");
+
+ assert_false(cbor_string_add_chunk(string, chunk));
+ assert_int_equal(cbor_string_chunk_count(string), 0);
+ assert_int_equal(((struct cbor_indefinite_string_data *)string->data)
+ ->chunk_capacity,
+ 0);
+
+ cbor_decref(&chunk);
+ cbor_decref(&string);
+ },
+ 5, MALLOC, MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
+}
+
+static void test_array_push(void **state) {
+ WITH_MOCK_MALLOC(
+ {
+ cbor_item_t *array = cbor_new_indefinite_array();
+ cbor_item_t *string = cbor_build_string("Hello!");
+
+ assert_false(cbor_array_push(array, string));
+ assert_int_equal(cbor_array_allocated(array), 0);
+ assert_null(array->data);
+ assert_int_equal(array->metadata.array_metadata.end_ptr, 0);
+
+ cbor_decref(&string);
+ cbor_decref(&array);
+ },
+ 4, MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
+}
+
+static void test_map_add(void **state) {
+ WITH_MOCK_MALLOC(
+ {
+ cbor_item_t *map = cbor_new_indefinite_map();
+ cbor_item_t *key = cbor_build_uint8(0);
+ cbor_item_t *value = cbor_build_bool(true);
+
+ assert_false(
+ cbor_map_add(map, (struct cbor_pair){.key = key, .value = value}));
+ assert_int_equal(cbor_map_allocated(map), 0);
+ assert_null(map->data);
+
+ cbor_decref(&map);
+ cbor_decref(&key);
+ cbor_decref(&value);
+ },
+ 4, MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
+}
+
+int main(void) {
+ cbor_set_allocs(instrumented_malloc, instrumented_realloc, free);
+
+ // TODO: string chunks realloc test
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_int_creation),
+ cmocka_unit_test(test_bytestring_creation),
+ cmocka_unit_test(test_string_creation),
+ cmocka_unit_test(test_array_creation),
+ cmocka_unit_test(test_map_creation),
+ cmocka_unit_test(test_tag_creation),
+ cmocka_unit_test(test_float_ctrl_creation),
+
+ cmocka_unit_test(test_bytestring_add_chunk),
+ cmocka_unit_test(test_string_add_chunk),
+ cmocka_unit_test(test_array_push),
+ cmocka_unit_test(test_map_add),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/pretty_printer_test.c b/test/pretty_printer_test.c
new file mode 100644
index 000000000000..978c72688859
--- /dev/null
+++ b/test/pretty_printer_test.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include <stdio.h>
+#include "cbor.h"
+
+unsigned char data[] = {0x8B, 0x01, 0x20, 0x5F, 0x41, 0x01, 0x41, 0x02,
+ 0xFF, 0x7F, 0x61, 0x61, 0x61, 0x62, 0xFF, 0x9F,
+ 0xFF, 0xA1, 0x61, 0x61, 0x61, 0x62, 0xC0, 0xBF,
+ 0xFF, 0xFB, 0x40, 0x09, 0x1E, 0xB8, 0x51, 0xEB,
+ 0x85, 0x1F, 0xF6, 0xF7, 0xF5};
+
+static void test_pretty_printer(void **state) {
+#if CBOR_PRETTY_PRINTER
+ FILE *outfile = tmpfile();
+ struct cbor_load_result res;
+ cbor_item_t *item = cbor_load(data, 37, &res);
+ cbor_describe(item, outfile);
+ cbor_decref(&item);
+
+ item = cbor_new_ctrl();
+ cbor_set_ctrl(item, 1);
+ cbor_describe(item, outfile);
+ cbor_decref(&item);
+
+ fclose(outfile);
+#endif
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {cmocka_unit_test(test_pretty_printer)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/stack_over_limit_test.c b/test/stack_over_limit_test.c
new file mode 100644
index 000000000000..09ea0d6e1d51
--- /dev/null
+++ b/test/stack_over_limit_test.c
@@ -0,0 +1,35 @@
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "assertions.h"
+#include "cbor.h"
+#include "stream_expectations.h"
+
+static size_t generate_overflow_data(unsigned char **overflow_data) {
+ int i;
+ *overflow_data = (unsigned char *)malloc(CBOR_MAX_STACK_SIZE + 3);
+ for (i = 0; i < CBOR_MAX_STACK_SIZE + 1; i++) {
+ (*overflow_data)[i] = 0xC2; // tag of positive bignum
+ }
+ (*overflow_data)[CBOR_MAX_STACK_SIZE + 1] = 0x41; // bytestring of length 1
+ (*overflow_data)[CBOR_MAX_STACK_SIZE + 2] = 0x01; // a bignum of value 1
+ return CBOR_MAX_STACK_SIZE + 3;
+}
+
+static void test_stack_over_limit(void **state) {
+ unsigned char *overflow_data;
+ size_t overflow_data_len;
+ struct cbor_load_result res;
+ overflow_data_len = generate_overflow_data(&overflow_data);
+ cbor_load(overflow_data, overflow_data_len, &res);
+ free(overflow_data);
+ assert_int_equal(res.error.code, CBOR_ERR_MEMERROR);
+}
+
+int main() {
+ const struct CMUnitTest tests[] = {cmocka_unit_test(test_stack_over_limit)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/stream_expectations.c b/test/stream_expectations.c
new file mode 100644
index 000000000000..890fafa763ea
--- /dev/null
+++ b/test/stream_expectations.c
@@ -0,0 +1,317 @@
+#include "stream_expectations.h"
+
+// TODO: The saved keystrokes are not worth the complexity. Get rid of this
+// file to prevent confusion, the fundamental structure is unlikely to change
+// in the future.
+
+/* Ordered from 0 to queue_size - 1 */
+struct test_assertion assertions_queue[MAX_QUEUE_ITEMS];
+int queue_size = 0;
+int current_expectation = 0;
+decoder_t *decoder;
+
+void set_decoder(decoder_t *dec) { decoder = dec; }
+
+int clear_stream_assertions(void **state) {
+ if (queue_size != current_expectation) {
+ return 1; // We have not matched all expectations correctly
+ }
+ queue_size = current_expectation = 0;
+ free(*state);
+ return 0;
+}
+
+/* Callbacks */
+struct test_assertion current() {
+ return assertions_queue[current_expectation];
+}
+
+/* Assertions builders and matcher callbacks */
+
+void assert_uint8_eq(uint8_t actual) {
+ assertions_queue[queue_size++] = (struct test_assertion){
+ UINT8_EQ, (union test_expectation_data){.int8 = actual}};
+}
+
+void uint8_callback(void *context, uint8_t actual) {
+ assert_true(current().expectation == UINT8_EQ);
+ assert_true(current().data.int8 == actual);
+ current_expectation++;
+}
+
+void assert_uint16_eq(uint16_t actual) {
+ assertions_queue[queue_size++] = (struct test_assertion){
+ UINT16_EQ, (union test_expectation_data){.int16 = actual}};
+}
+
+void uint16_callback(void *context, uint16_t actual) {
+ assert_true(current().expectation == UINT16_EQ);
+ assert_true(current().data.int16 == actual);
+ current_expectation++;
+}
+
+void assert_uint32_eq(uint32_t actual) {
+ assertions_queue[queue_size++] = (struct test_assertion){
+ UINT32_EQ, (union test_expectation_data){.int32 = actual}};
+}
+
+void uint32_callback(void *context, uint32_t actual) {
+ assert_true(current().expectation == UINT32_EQ);
+ assert_true(current().data.int32 == actual);
+ current_expectation++;
+}
+
+void assert_uint64_eq(uint64_t actual) {
+ assertions_queue[queue_size++] = (struct test_assertion){
+ UINT64_EQ, (union test_expectation_data){.int64 = actual}};
+}
+
+void uint64_callback(void *context, uint64_t actual) {
+ assert_true(current().expectation == UINT64_EQ);
+ assert_true(current().data.int64 == actual);
+ current_expectation++;
+}
+
+void assert_negint8_eq(uint8_t actual) {
+ assertions_queue[queue_size++] = (struct test_assertion){
+ NEGINT8_EQ, (union test_expectation_data){.int8 = actual}};
+}
+
+void negint8_callback(void *context, uint8_t actual) {
+ assert_true(current().expectation == NEGINT8_EQ);
+ assert_true(current().data.int8 == actual);
+ current_expectation++;
+}
+
+void assert_negint16_eq(uint16_t actual) {
+ assertions_queue[queue_size++] = (struct test_assertion){
+ NEGINT16_EQ, (union test_expectation_data){.int16 = actual}};
+}
+
+void negint16_callback(void *context, uint16_t actual) {
+ assert_true(current().expectation == NEGINT16_EQ);
+ assert_true(current().data.int16 == actual);
+ current_expectation++;
+}
+
+void assert_negint32_eq(uint32_t actual) {
+ assertions_queue[queue_size++] = (struct test_assertion){
+ NEGINT32_EQ, (union test_expectation_data){.int32 = actual}};
+}
+
+void negint32_callback(void *context, uint32_t actual) {
+ assert_true(current().expectation == NEGINT32_EQ);
+ assert_true(current().data.int32 == actual);
+ current_expectation++;
+}
+
+void assert_negint64_eq(uint64_t actual) {
+ assertions_queue[queue_size++] = (struct test_assertion){
+ NEGINT64_EQ, (union test_expectation_data){.int64 = actual}};
+}
+
+void negint64_callback(void *context, uint64_t actual) {
+ assert_true(current().expectation == NEGINT64_EQ);
+ assert_true(current().data.int64 == actual);
+ current_expectation++;
+}
+
+void assert_bstring_mem_eq(cbor_data address, size_t length) {
+ assertions_queue[queue_size++] = (struct test_assertion){
+ BSTRING_MEM_EQ,
+ (union test_expectation_data){.string = {address, length}}};
+}
+
+void byte_string_callback(void *context, cbor_data address, size_t length) {
+ assert_true(current().expectation == BSTRING_MEM_EQ);
+ assert_true(current().data.string.address == address);
+ assert_true(current().data.string.length == length);
+ current_expectation++;
+}
+
+void assert_bstring_indef_start() {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){.expectation = BSTRING_INDEF_START};
+}
+
+void byte_string_start_callback(void *context) {
+ assert_true(current().expectation == BSTRING_INDEF_START);
+ current_expectation++;
+}
+
+void assert_indef_break() {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){.expectation = INDEF_BREAK};
+}
+
+void indef_break_callback(void *context) {
+ assert_true(current().expectation == INDEF_BREAK);
+ current_expectation++;
+}
+
+void assert_array_start(size_t length) {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){ARRAY_START, {.length = length}};
+}
+
+void array_start_callback(void *context, size_t length) {
+ assert_true(current().expectation == ARRAY_START);
+ assert_true(current().data.length == length);
+ current_expectation++;
+}
+
+void assert_indef_array_start() {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){.expectation = ARRAY_INDEF_START};
+}
+
+void indef_array_start_callback(void *context) {
+ assert_true(current().expectation == ARRAY_INDEF_START);
+ current_expectation++;
+}
+
+void assert_map_start(size_t length) {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){MAP_START, {.length = length}};
+}
+
+void map_start_callback(void *context, size_t length) {
+ assert_true(current().expectation == MAP_START);
+ assert_true(current().data.length == length);
+ current_expectation++;
+}
+
+void assert_indef_map_start() {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){.expectation = MAP_INDEF_START};
+}
+
+void indef_map_start_callback(void *context) {
+ assert_true(current().expectation == MAP_INDEF_START);
+ current_expectation++;
+}
+
+void assert_tag_eq(uint64_t value) {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){TAG_EQ, {.int64 = value}};
+}
+
+void tag_callback(void *context, uint64_t value) {
+ assert_true(current().expectation == TAG_EQ);
+ assert_true(current().data.int64 == value);
+ current_expectation++;
+}
+
+void assert_half(float value) {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){HALF_EQ, {.float2 = value}};
+}
+
+void half_callback(void *context, float actual) {
+ assert_true(current().expectation == HALF_EQ);
+ assert_true(current().data.float2 == actual);
+ current_expectation++;
+}
+
+void assert_float(float value) {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){FLOAT_EQ, {.float4 = value}};
+}
+
+void float_callback(void *context, float actual) {
+ assert_true(current().expectation == FLOAT_EQ);
+ assert_true(current().data.float4 == actual);
+ current_expectation++;
+}
+
+void assert_double(double value) {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){DOUBLE_EQ, {.float8 = value}};
+}
+
+void double_callback(void *context, double actual) {
+ assert_true(current().expectation == DOUBLE_EQ);
+ assert_true(current().data.float8 == actual);
+ current_expectation++;
+}
+
+void assert_bool(bool value) {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){BOOL_EQ, {.boolean = value}};
+}
+
+void assert_nil() {
+ assertions_queue[queue_size++] = (struct test_assertion){.expectation = NIL};
+}
+
+void assert_undef() {
+ assertions_queue[queue_size++] =
+ (struct test_assertion){.expectation = UNDEF};
+}
+
+void bool_callback(void *context, bool actual) {
+ assert_true(current().expectation == BOOL_EQ);
+ assert_true(current().data.boolean == actual);
+ current_expectation++;
+}
+
+void null_callback(void *context) {
+ assert_true(current().expectation == NIL);
+ current_expectation++;
+}
+
+void undef_callback(void *context) {
+ assert_true(current().expectation == UNDEF);
+ current_expectation++;
+}
+
+const struct cbor_callbacks asserting_callbacks = {
+
+ .uint8 = &uint8_callback,
+
+ .uint16 = &uint16_callback,
+
+ .uint32 = &uint32_callback,
+
+ .uint64 = &uint64_callback,
+
+ .negint8 = &negint8_callback,
+
+ .negint16 = &negint16_callback,
+
+ .negint32 = &negint32_callback,
+
+ .negint64 = &negint64_callback,
+
+ .byte_string = &byte_string_callback,
+ .byte_string_start = &byte_string_start_callback,
+
+ .array_start = &array_start_callback,
+ .indef_array_start = &indef_array_start_callback,
+
+ .map_start = &map_start_callback,
+ .indef_map_start = &indef_map_start_callback,
+
+ .tag = &tag_callback,
+
+ .float2 = &half_callback,
+
+ .float4 = &float_callback,
+
+ .float8 = &double_callback,
+
+ .undefined = &undef_callback,
+ .boolean = &bool_callback,
+ .null = &null_callback,
+ .indef_break = &indef_break_callback};
+
+struct cbor_decoder_result decode(cbor_data source, size_t source_size) {
+ int last_expectation = current_expectation;
+ struct cbor_decoder_result result =
+ decoder(source, source_size, &asserting_callbacks, NULL);
+ if (result.status == CBOR_DECODER_FINISHED) {
+ // Check that we have matched an expectation from the queue
+ assert_true(last_expectation + 1 == current_expectation);
+ }
+ return result;
+}
diff --git a/test/stream_expectations.h b/test/stream_expectations.h
new file mode 100644
index 000000000000..84970ae943bc
--- /dev/null
+++ b/test/stream_expectations.h
@@ -0,0 +1,151 @@
+/*
+ * This file provides testing tools for the streaming decoder. The intended
+ * usage is as follows: 1) SE API wrapper is initialized 2) Client builds
+ * (ordered) series of expectations 3) The decoder is executed 4) SE checks all
+ * assertions 5) Go to 2) if desired
+ */
+
+#ifndef STREAM_EXPECTATIONS_H_
+#define STREAM_EXPECTATIONS_H_
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include <stdint.h>
+#include "cbor.h"
+
+// TODO: This setup is overengineered, we currently use one assertion at a time
+// TOOD: We never ensure that the queue is empty
+#define MAX_QUEUE_ITEMS 30
+
+enum test_expectation {
+ UINT8_EQ,
+ UINT16_EQ,
+ UINT32_EQ,
+ UINT64_EQ,
+
+ NEGINT8_EQ,
+ NEGINT16_EQ,
+ NEGINT32_EQ,
+ NEGINT64_EQ,
+
+ BSTRING_MEM_EQ, /* Matches length and memory address for definite byte strings
+ */
+ BSTRING_INDEF_START,
+
+ ARRAY_START, /* Definite arrays only */
+ ARRAY_INDEF_START,
+
+ MAP_START, /* Definite maps only */
+ MAP_INDEF_START,
+
+ TAG_EQ,
+
+ HALF_EQ,
+ FLOAT_EQ,
+ DOUBLE_EQ,
+ BOOL_EQ,
+ NIL,
+ UNDEF,
+ INDEF_BREAK /* Expect "Break" */
+};
+
+union test_expectation_data {
+ uint8_t int8;
+ uint16_t int16;
+ uint32_t int32;
+ uint64_t int64;
+ struct string {
+ cbor_data address;
+ size_t length;
+ } string;
+ size_t length;
+ float float2;
+ float float4;
+ double float8;
+ bool boolean;
+};
+
+struct test_assertion {
+ enum test_expectation expectation;
+ union test_expectation_data data;
+};
+
+/* Tested function */
+// TODO: This looks overengineered, we only ever use one (?) in the testsuite
+typedef struct cbor_decoder_result decoder_t(cbor_data, size_t,
+ const struct cbor_callbacks *,
+ void *);
+void set_decoder(decoder_t *);
+struct cbor_decoder_result decode(cbor_data, size_t);
+
+/* Test setup */
+int clear_stream_assertions(void **);
+
+/* Assertions builders */
+void assert_uint8_eq(uint8_t);
+void assert_uint16_eq(uint16_t);
+void assert_uint32_eq(uint32_t);
+void assert_uint64_eq(uint64_t);
+
+void assert_negint8_eq(uint8_t);
+void assert_negint16_eq(uint16_t);
+void assert_negint32_eq(uint32_t);
+void assert_negint64_eq(uint64_t);
+
+void assert_bstring_mem_eq(cbor_data, size_t);
+void assert_bstring_indef_start();
+
+void assert_array_start(size_t);
+void assert_indef_array_start();
+
+void assert_map_start(size_t);
+void assert_indef_map_start();
+
+void assert_tag_eq(uint64_t);
+
+void assert_half(float);
+void assert_float(float);
+void assert_double(double);
+
+void assert_bool(bool);
+void assert_nil(); /* assert_null already exists */
+void assert_undef();
+
+void assert_indef_break();
+
+/* Assertions verifying callbacks */
+void uint8_callback(void *, uint8_t);
+void uint16_callback(void *, uint16_t);
+void uint32_callback(void *, uint32_t);
+void uint64_callback(void *, uint64_t);
+
+void negint8_callback(void *, uint8_t);
+void negint16_callback(void *, uint16_t);
+void negint32_callback(void *, uint32_t);
+void negint64_callback(void *, uint64_t);
+
+void byte_string_callback(void *, cbor_data, size_t);
+void byte_string_start_callback(void *);
+
+void array_start_callback(void *, size_t);
+void indef_array_start_callback(void *);
+
+void map_start_callback(void *, size_t);
+void indef_map_start_callback(void *);
+
+void tag_callback(void *, uint64_t);
+
+void half_callback(void *, float);
+void float_callback(void *, float);
+void double_callback(void *, double);
+void indef_break_callback(void *);
+
+void bool_callback(void *, bool);
+void null_callback(void *);
+void undef_callback(void *);
+
+#endif
diff --git a/test/type_0_encoders_test.c b/test/type_0_encoders_test.c
new file mode 100644
index 000000000000..d6f2c15ef8b5
--- /dev/null
+++ b/test/type_0_encoders_test.c
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+unsigned char buffer[512];
+
+static void test_embedded_uint8(void **state) {
+ assert_int_equal(1, cbor_encode_uint8(14, buffer, 512));
+ assert_memory_equal(buffer, (unsigned char[]){0x0E}, 1);
+}
+
+static void test_uint8(void **state) {
+ assert_int_equal(0, cbor_encode_uint8(180, buffer, 1));
+ assert_int_equal(2, cbor_encode_uint8(255, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x18, 0xFF}), 2);
+}
+
+static void test_uint16(void **state) {
+ assert_int_equal(0, cbor_encode_uint16(1000, buffer, 2));
+ assert_int_equal(3, cbor_encode_uint16(1000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x19, 0x03, 0xE8}), 3);
+}
+
+static void test_uint32(void **state) {
+ assert_int_equal(0, cbor_encode_uint32(1000000, buffer, 4));
+ assert_int_equal(5, cbor_encode_uint32(1000000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x1A, 0x00, 0x0F, 0x42, 0x40}),
+ 5);
+}
+
+static void test_uint64(void **state) {
+ assert_int_equal(0, cbor_encode_uint64(18446744073709551615ULL, buffer, 8));
+ assert_int_equal(9, cbor_encode_uint64(18446744073709551615ULL, buffer, 512));
+ assert_memory_equal(
+ buffer,
+ ((unsigned char[]){0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
+ 9);
+}
+
+static void test_unspecified(void **state) {
+ assert_int_equal(9, cbor_encode_uint(18446744073709551615ULL, buffer, 512));
+ assert_memory_equal(
+ buffer,
+ ((unsigned char[]){0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
+ 9);
+ assert_int_equal(5, cbor_encode_uint(1000000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x1A, 0x00, 0x0F, 0x42, 0x40}),
+ 5);
+ assert_int_equal(3, cbor_encode_uint(1000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x19, 0x03, 0xE8}), 3);
+ assert_int_equal(2, cbor_encode_uint(255, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x18, 0xFF}), 2);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {cmocka_unit_test(test_embedded_uint8),
+ cmocka_unit_test(test_uint8),
+ cmocka_unit_test(test_uint16),
+ cmocka_unit_test(test_uint32),
+ cmocka_unit_test(test_uint64),
+ cmocka_unit_test(test_unspecified)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_0_test.c b/test/type_0_test.c
new file mode 100644
index 000000000000..8f7e480b7a62
--- /dev/null
+++ b/test/type_0_test.c
@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+cbor_item_t *number;
+struct cbor_load_result res;
+
+unsigned char data1[] = {0x02, 0xFF};
+unsigned char data2[] = {0x18, 0xFF, 0xFF};
+unsigned char data3[] = {0x19, 0x01, 0xf4, 0xFF};
+unsigned char data4[] = {0x1a, 0xa5, 0xf7, 0x02, 0xb3, 0xFF};
+unsigned char data5[] = {0x1b, 0xa5, 0xf7, 0x02, 0xb3,
+ 0xa5, 0xf7, 0x02, 0xb3, 0xFF};
+
+static void test_very_short_int(void **state) {
+ number = cbor_load(data1, 2, &res);
+ assert_true(cbor_typeof(number) == CBOR_TYPE_UINT);
+ assert_true(cbor_int_get_width(number) == CBOR_INT_8);
+ assert_true(cbor_isa_uint(number));
+ assert_false(cbor_isa_negint(number));
+ assert_true(cbor_get_uint8(number) == 2);
+ assert_true(res.error.code == 0);
+ assert_true(res.read == 1);
+ assert_true(cbor_is_int(number));
+ cbor_decref(&number);
+ assert_null(number);
+}
+
+static void test_incomplete_data(void **state) {
+ number = cbor_load(data2, 1, &res);
+ assert_null(number);
+ assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
+}
+
+static void test_short_int(void **state) {
+ number = cbor_load(data2, 3, &res);
+ assert_true(cbor_typeof(number) == CBOR_TYPE_UINT);
+ assert_true(cbor_int_get_width(number) == CBOR_INT_8);
+ assert_true(cbor_isa_uint(number));
+ assert_false(cbor_isa_negint(number));
+ assert_true(cbor_get_uint8(number) == 255);
+ assert_true(res.error.code == 0);
+ assert_true(res.read == 2);
+ assert_true(cbor_is_int(number));
+ cbor_decref(&number);
+ assert_null(number);
+}
+
+static void test_half_int(void **state) {
+ number = cbor_load(data3, 5, &res);
+ assert_true(cbor_typeof(number) == CBOR_TYPE_UINT);
+ assert_true(cbor_int_get_width(number) == CBOR_INT_16);
+ assert_true(cbor_isa_uint(number));
+ assert_false(cbor_isa_negint(number));
+ assert_true(cbor_get_uint16(number) == 500);
+ assert_true(res.error.code == 0);
+ assert_true(res.read == 3);
+ assert_true(cbor_is_int(number));
+ cbor_decref(&number);
+ assert_null(number);
+}
+
+static void test_int(void **state) {
+ number = cbor_load(data4, 6, &res);
+ assert_true(cbor_typeof(number) == CBOR_TYPE_UINT);
+ assert_true(cbor_int_get_width(number) == CBOR_INT_32);
+ assert_true(cbor_isa_uint(number));
+ assert_false(cbor_isa_negint(number));
+ assert_true(cbor_get_uint32(number) == 2784428723);
+ assert_true(res.error.code == 0);
+ assert_true(res.read == 5);
+ assert_true(cbor_is_int(number));
+ cbor_decref(&number);
+ assert_null(number);
+}
+
+static void test_long_int(void **state) {
+ number = cbor_load(data5, 10, &res);
+ assert_true(cbor_typeof(number) == CBOR_TYPE_UINT);
+ assert_true(cbor_int_get_width(number) == CBOR_INT_64);
+ assert_true(cbor_isa_uint(number));
+ assert_false(cbor_isa_negint(number));
+ assert_true(cbor_get_uint64(number) == 11959030306112471731ULL);
+ assert_true(res.error.code == 0);
+ assert_true(res.read == 9);
+ assert_true(cbor_is_int(number));
+ cbor_decref(&number);
+ assert_null(number);
+}
+
+static void test_refcounting(void **state) {
+ number = cbor_load(data5, 10, &res);
+ cbor_incref(number);
+ assert_true(number->refcount == 2);
+ cbor_decref(&number);
+ assert_non_null(number);
+ cbor_decref(&number);
+ assert_null(number);
+}
+
+static void test_empty_input(void **state) {
+ number = cbor_load(data5, 0, &res);
+ assert_null(number);
+ assert_true(res.error.code == CBOR_ERR_NODATA);
+}
+
+static void test_inline_creation(void **state) {
+ number = cbor_build_uint8(10);
+ assert_true(cbor_get_int(number) == 10);
+ cbor_decref(&number);
+
+ number = cbor_build_uint16(10);
+ assert_true(cbor_get_int(number) == 10);
+ cbor_decref(&number);
+
+ number = cbor_build_uint32(10);
+ assert_true(cbor_get_int(number) == 10);
+ cbor_decref(&number);
+
+ number = cbor_build_uint64(10);
+ assert_true(cbor_get_int(number) == 10);
+ cbor_decref(&number);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {cmocka_unit_test(test_very_short_int),
+ cmocka_unit_test(test_short_int),
+ cmocka_unit_test(test_half_int),
+ cmocka_unit_test(test_int),
+ cmocka_unit_test(test_long_int),
+ cmocka_unit_test(test_incomplete_data),
+ cmocka_unit_test(test_refcounting),
+ cmocka_unit_test(test_empty_input),
+ cmocka_unit_test(test_inline_creation)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_1_encoders_test.c b/test/type_1_encoders_test.c
new file mode 100644
index 000000000000..50edaa08cd7d
--- /dev/null
+++ b/test/type_1_encoders_test.c
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+unsigned char buffer[512];
+
+static void test_embedded_negint8(void **state) {
+ assert_int_equal(1, cbor_encode_negint8(14, buffer, 512));
+ assert_memory_equal(buffer, (unsigned char[]){0x2E}, 1);
+}
+
+static void test_negint8(void **state) {
+ assert_int_equal(0, cbor_encode_negint8(180, buffer, 1));
+ assert_int_equal(2, cbor_encode_negint8(255, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x38, 0xFF}), 2);
+}
+
+static void test_negint16(void **state) {
+ assert_int_equal(0, cbor_encode_negint16(1000, buffer, 2));
+ assert_int_equal(3, cbor_encode_negint16(1000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x39, 0x03, 0xE8}), 3);
+}
+
+static void test_negint32(void **state) {
+ assert_int_equal(0, cbor_encode_negint32(1000000, buffer, 4));
+ assert_int_equal(5, cbor_encode_negint32(1000000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x3A, 0x00, 0x0F, 0x42, 0x40}),
+ 5);
+}
+
+static void test_negint64(void **state) {
+ assert_int_equal(0, cbor_encode_negint64(18446744073709551615ULL, buffer, 8));
+ assert_int_equal(9,
+ cbor_encode_negint64(18446744073709551615ULL, buffer, 512));
+ assert_memory_equal(
+ buffer,
+ ((unsigned char[]){0x3B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
+ 9);
+}
+
+static void test_unspecified(void **state) {
+ assert_int_equal(9, cbor_encode_negint(18446744073709551615ULL, buffer, 512));
+ assert_memory_equal(
+ buffer,
+ ((unsigned char[]){0x3B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}),
+ 9);
+ assert_int_equal(5, cbor_encode_negint(1000000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x3A, 0x00, 0x0F, 0x42, 0x40}),
+ 5);
+ assert_int_equal(3, cbor_encode_negint(1000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x39, 0x03, 0xE8}), 3);
+ assert_int_equal(2, cbor_encode_negint(255, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x38, 0xFF}), 2);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {cmocka_unit_test(test_embedded_negint8),
+ cmocka_unit_test(test_negint8),
+ cmocka_unit_test(test_negint16),
+ cmocka_unit_test(test_negint32),
+ cmocka_unit_test(test_negint64),
+ cmocka_unit_test(test_unspecified)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_1_test.c b/test/type_1_test.c
new file mode 100644
index 000000000000..8a27d7e8b639
--- /dev/null
+++ b/test/type_1_test.c
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+cbor_item_t *number;
+struct cbor_load_result res;
+
+unsigned char data1[] = {0x22, 0xFF};
+unsigned char data2[] = {0x38, 0xFF, 0xFF};
+unsigned char data3[] = {0x39, 0x01, 0xf4, 0xFF};
+unsigned char data4[] = {0x3a, 0xa5, 0xf7, 0x02, 0xb3, 0xFF};
+unsigned char data5[] = {0x3b, 0xa5, 0xf7, 0x02, 0xb3,
+ 0xa5, 0xf7, 0x02, 0xb3, 0xFF};
+
+static void test_very_short_int(void **state) {
+ number = cbor_load(data1, 2, &res);
+ assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
+ assert_true(cbor_int_get_width(number) == CBOR_INT_8);
+ assert_false(cbor_isa_uint(number));
+ assert_true(cbor_isa_negint(number));
+ assert_true(cbor_get_uint8(number) == 2);
+ assert_true(res.error.code == 0);
+ assert_true(res.read == 1);
+ assert_true(cbor_is_int(number));
+ cbor_decref(&number);
+ assert_null(number);
+}
+
+static void test_short_int(void **state) {
+ number = cbor_load(data2, 3, &res);
+ assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
+ assert_true(cbor_int_get_width(number) == CBOR_INT_8);
+ assert_false(cbor_isa_uint(number));
+ assert_true(cbor_isa_negint(number));
+ assert_true(cbor_get_uint8(number) == 255);
+ assert_true(res.error.code == 0);
+ assert_true(res.read == 2);
+ assert_true(cbor_is_int(number));
+ cbor_decref(&number);
+ assert_null(number);
+}
+
+static void test_half_int(void **state) {
+ number = cbor_load(data3, 5, &res);
+ assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
+ assert_true(cbor_int_get_width(number) == CBOR_INT_16);
+ assert_false(cbor_isa_uint(number));
+ assert_true(cbor_isa_negint(number));
+ assert_true(cbor_get_uint16(number) == 500);
+ assert_true(res.error.code == 0);
+ assert_true(res.read == 3);
+ assert_true(cbor_is_int(number));
+ cbor_decref(&number);
+ assert_null(number);
+}
+
+static void test_int(void **state) {
+ number = cbor_load(data4, 6, &res);
+ assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
+ assert_true(cbor_int_get_width(number) == CBOR_INT_32);
+ assert_false(cbor_isa_uint(number));
+ assert_true(cbor_isa_negint(number));
+ assert_true(cbor_get_uint32(number) == 2784428723);
+ assert_true(res.error.code == 0);
+ assert_true(res.read == 5);
+ assert_true(cbor_is_int(number));
+ cbor_decref(&number);
+ assert_null(number);
+}
+
+static void test_long_int(void **state) {
+ number = cbor_load(data5, 10, &res);
+ assert_true(cbor_typeof(number) == CBOR_TYPE_NEGINT);
+ assert_true(cbor_int_get_width(number) == CBOR_INT_64);
+ assert_false(cbor_isa_uint(number));
+ assert_true(cbor_isa_negint(number));
+ assert_true(cbor_get_uint64(number) == 11959030306112471731ULL);
+ assert_true(res.error.code == 0);
+ assert_true(res.read == 9);
+ assert_true(cbor_is_int(number));
+ cbor_decref(&number);
+ assert_null(number);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_very_short_int), cmocka_unit_test(test_short_int),
+ cmocka_unit_test(test_half_int), cmocka_unit_test(test_int),
+ cmocka_unit_test(test_long_int)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_2_encoders_test.c b/test/type_2_encoders_test.c
new file mode 100644
index 000000000000..563fbe1b1261
--- /dev/null
+++ b/test/type_2_encoders_test.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+unsigned char buffer[512];
+
+static void test_embedded_bytestring_start(void **state) {
+ assert_int_equal(1, cbor_encode_bytestring_start(1, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x41}), 1);
+}
+
+static void test_bytestring_start(void **state) {
+ assert_int_equal(5, cbor_encode_bytestring_start(1000000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x5A, 0x00, 0x0F, 0x42, 0x40}),
+ 5);
+}
+
+static void test_indef_bytestring_start(void **state) {
+ assert_int_equal(0, cbor_encode_indef_bytestring_start(buffer, 0));
+ assert_int_equal(1, cbor_encode_indef_bytestring_start(buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x5F}), 1);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_embedded_bytestring_start),
+ cmocka_unit_test(test_bytestring_start),
+ cmocka_unit_test(test_indef_bytestring_start)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_2_test.c b/test/type_2_test.c
new file mode 100644
index 000000000000..d29b9f4c0fa9
--- /dev/null
+++ b/test/type_2_test.c
@@ -0,0 +1,327 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+cbor_item_t *bs;
+struct cbor_load_result res;
+
+unsigned char data1[] = {0x40, 0xFF};
+unsigned char data2[] = {0x41, 0xA1};
+unsigned char data3[] = {0x58, 0x01, 0xA1, 0xFF};
+unsigned char data4[] = {
+ 0x58, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
+ 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
+ 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21,
+ 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,
+ 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
+ 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
+ 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51,
+ 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D,
+ 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
+ 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
+ 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81,
+ 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D,
+ 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
+ 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5,
+ 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1,
+ 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD,
+ 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9,
+ 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5,
+ 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1,
+ 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED,
+ 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9,
+ 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF};
+unsigned char data5[] = {
+ 0x59, 0x00, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+ 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14,
+ 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
+ 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C,
+ 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
+ 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44,
+ 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
+ 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C,
+ 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
+ 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74,
+ 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80,
+ 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C,
+ 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
+ 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4,
+ 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0,
+ 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC,
+ 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8,
+ 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4,
+ 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0,
+ 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC,
+ 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
+ 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF};
+unsigned char data6[] = {
+ 0x5A, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
+ 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
+ 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
+ 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
+ 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42,
+ 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E,
+ 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A,
+ 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
+ 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72,
+ 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E,
+ 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A,
+ 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
+ 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2,
+ 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE,
+ 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
+ 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6,
+ 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2,
+ 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE,
+ 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA,
+ 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6,
+ 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF};
+unsigned char data7[] = {
+ 0x5B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x01, 0x02,
+ 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
+ 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
+ 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
+ 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
+ 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E,
+ 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A,
+ 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
+ 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62,
+ 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
+ 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
+ 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
+ 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92,
+ 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E,
+ 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA,
+ 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
+ 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2,
+ 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE,
+ 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
+ 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
+ 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2,
+ 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE,
+ 0xFF};
+unsigned char data8[] = {
+ 0x5C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xFF, 0x00, 0x01,
+ 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
+ 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
+ 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
+ 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31,
+ 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D,
+ 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
+ 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
+ 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61,
+ 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D,
+ 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
+ 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
+ 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91,
+ 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D,
+ 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9,
+ 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5,
+ 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1,
+ 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD,
+ 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9,
+ 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5,
+ 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1,
+ 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD,
+ 0xFE, 0xFF};
+
+static void test_empty_bs(void **state) {
+ bs = cbor_load(data1, 2, &res);
+ assert_non_null(bs);
+ assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
+ assert_true(cbor_isa_bytestring(bs));
+ assert_int_equal(cbor_bytestring_length(bs), 0);
+ assert_true(res.read == 1);
+ cbor_decref(&bs);
+ assert_null(bs);
+}
+
+static void test_embedded_bs(void **state) {
+ bs = cbor_load(data2, 2, &res);
+ assert_non_null(bs);
+ assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
+ assert_true(cbor_isa_bytestring(bs));
+ assert_true(cbor_bytestring_length(bs) == 1);
+ assert_true(res.read == 2);
+ assert_true(*cbor_bytestring_handle(bs) == 0xA1);
+ assert_memory_equal(cbor_bytestring_handle(bs), data2 + 1, 1);
+ cbor_decref(&bs);
+ assert_null(bs);
+}
+
+static void test_notenough_data(void **state) {
+ bs = cbor_load(data3, 2, &res);
+ assert_null(bs);
+ assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
+}
+
+static void test_short_bs1(void **state) {
+ bs = cbor_load(data3, 4, &res);
+ assert_non_null(bs);
+ assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
+ assert_true(cbor_isa_bytestring(bs));
+ assert_true(cbor_bytestring_length(bs) == 1);
+ assert_true(res.read == 3);
+ assert_true(*cbor_bytestring_handle(bs) == 0xA1);
+ assert_memory_equal(cbor_bytestring_handle(bs), data3 + 2, 1);
+ cbor_decref(&bs);
+ assert_null(bs);
+}
+
+static void test_short_bs2(void **state) {
+ bs = cbor_load(data4, 259, &res);
+ assert_non_null(bs);
+ assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
+ assert_true(cbor_isa_bytestring(bs));
+ assert_true(cbor_bytestring_length(bs) == 255);
+ assert_true(res.read == 257);
+ assert_memory_equal(cbor_bytestring_handle(bs), data4 + 2, 0xFF);
+ cbor_decref(&bs);
+ assert_null(bs);
+}
+
+static void test_half_bs(void **state) {
+ bs = cbor_load(data5, 259, &res);
+ assert_non_null(bs);
+ assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
+ assert_true(cbor_isa_bytestring(bs));
+ assert_true(cbor_bytestring_length(bs) == 255);
+ assert_true(res.read == 258);
+ assert_memory_equal(cbor_bytestring_handle(bs), data5 + 3, 0xFF);
+ cbor_decref(&bs);
+ assert_null(bs);
+}
+
+static void test_int_bs(void **state) {
+ bs = cbor_load(data6, 261, &res);
+ assert_non_null(bs);
+ assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
+ assert_true(cbor_isa_bytestring(bs));
+ assert_true(cbor_bytestring_length(bs) == 255);
+ assert_true(res.read == 260);
+ assert_memory_equal(cbor_bytestring_handle(bs), data6 + 5, 0xFF);
+ cbor_decref(&bs);
+ assert_null(bs);
+}
+
+static void test_long_bs(void **state) {
+ bs = cbor_load(data7, 265, &res);
+ assert_non_null(bs);
+ assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
+ assert_true(cbor_isa_bytestring(bs));
+ assert_true(cbor_bytestring_length(bs) == 255);
+ assert_true(res.read == 264);
+ assert_memory_equal(cbor_bytestring_handle(bs), data7 + 9, 0xFF);
+ cbor_decref(&bs);
+ assert_null(bs);
+}
+
+unsigned char data9[] = {0x5F, 0xFF};
+
+static void test_zero_indef(void **state) {
+ bs = cbor_load(data9, 2, &res);
+ assert_non_null(bs);
+ assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
+ assert_true(cbor_isa_bytestring(bs));
+ assert_true(cbor_bytestring_is_indefinite(bs));
+ assert_true(cbor_bytestring_chunk_count(bs) == 0);
+ assert_true(res.read == 2);
+ cbor_decref(&bs);
+ assert_null(bs);
+}
+
+unsigned char data10[] = {0x5F, 0x58, 0x01, 0xA1, 0xFF, 0xFF};
+
+/* start | bstring | break| extra */
+
+static void test_short_indef(void **state) {
+ bs = cbor_load(data10, 6, &res);
+ assert_non_null(bs);
+ assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
+ assert_true(cbor_isa_bytestring(bs));
+ assert_true(cbor_bytestring_length(bs) == 0);
+ assert_true(cbor_bytestring_is_indefinite(bs));
+ assert_true(cbor_bytestring_chunk_count(bs) == 1);
+ assert_true(res.read == 5);
+ assert_true(cbor_isa_bytestring(cbor_bytestring_chunks_handle(bs)[0]));
+ assert_true(cbor_bytestring_length(cbor_bytestring_chunks_handle(bs)[0]) ==
+ 1);
+ assert_true(*cbor_bytestring_handle(cbor_bytestring_chunks_handle(bs)[0]) ==
+ 0xA1);
+ cbor_decref(&bs);
+ assert_null(bs);
+}
+
+unsigned char data11[] = {0x5F, 0x58, 0x01, 0xA1, 0x58, 0x01, 0xA2, 0xFF, 0xFF};
+
+/* start | bstring | bstring | break|
+ * extra */
+
+static void test_two_indef(void **state) {
+ bs = cbor_load(data11, 9, &res);
+ assert_non_null(bs);
+ assert_int_equal(1, cbor_refcount(bs));
+ assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
+ assert_true(cbor_isa_bytestring(bs));
+ assert_true(cbor_bytestring_length(bs) == 0);
+ assert_true(cbor_bytestring_is_indefinite(bs));
+ assert_true(cbor_bytestring_chunk_count(bs) == 2);
+ assert_true(res.read == 8);
+ assert_true(cbor_isa_bytestring(cbor_bytestring_chunks_handle(bs)[0]));
+ assert_true(cbor_bytestring_length(cbor_bytestring_chunks_handle(bs)[0]) ==
+ 1);
+ assert_true(*cbor_bytestring_handle(cbor_bytestring_chunks_handle(bs)[0]) ==
+ 0xA1);
+ assert_true(cbor_isa_bytestring(cbor_bytestring_chunks_handle(bs)[1]));
+ assert_true(cbor_bytestring_length(cbor_bytestring_chunks_handle(bs)[1]) ==
+ 1);
+ assert_true(*cbor_bytestring_handle(cbor_bytestring_chunks_handle(bs)[1]) ==
+ 0xA2);
+ cbor_decref(&bs);
+ assert_null(bs);
+}
+
+unsigned char data12[] = {0x5F, 0x58, 0x01};
+
+/* start | bstring - too short */
+
+static void test_missing_indef(void **state) {
+ bs = cbor_load(data12, 3, &res);
+ assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
+ assert_null(bs);
+}
+
+static void test_inline_creation(void **state) {
+ bs = cbor_build_bytestring((cbor_data) "Hello!", 6);
+ assert_memory_equal(cbor_bytestring_handle(bs), "Hello!", 6);
+ cbor_decref(&bs);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {cmocka_unit_test(test_empty_bs),
+ cmocka_unit_test(test_embedded_bs),
+ cmocka_unit_test(test_notenough_data),
+ cmocka_unit_test(test_short_bs1),
+ cmocka_unit_test(test_short_bs2),
+ cmocka_unit_test(test_half_bs),
+ cmocka_unit_test(test_int_bs),
+ cmocka_unit_test(test_long_bs),
+ cmocka_unit_test(test_zero_indef),
+ cmocka_unit_test(test_short_indef),
+ cmocka_unit_test(test_two_indef),
+ cmocka_unit_test(test_missing_indef),
+ cmocka_unit_test(test_inline_creation)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_3_encoders_test.c b/test/type_3_encoders_test.c
new file mode 100644
index 000000000000..823e90355087
--- /dev/null
+++ b/test/type_3_encoders_test.c
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+unsigned char buffer[512];
+
+static void test_embedded_string_start(void **state) {
+ assert_int_equal(1, cbor_encode_string_start(1, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x61}), 1);
+}
+
+static void test_string_start(void **state) {
+ assert_int_equal(5, cbor_encode_string_start(1000000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x7A, 0x00, 0x0F, 0x42, 0x40}),
+ 5);
+}
+
+static void test_indef_string_start(void **state) {
+ assert_int_equal(1, cbor_encode_indef_string_start(buffer, 512));
+ assert_int_equal(0, cbor_encode_indef_string_start(buffer, 0));
+ assert_memory_equal(buffer, ((unsigned char[]){0x7F}), 1);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_embedded_string_start),
+ cmocka_unit_test(test_string_start),
+ cmocka_unit_test(test_indef_string_start)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_3_test.c b/test/type_3_test.c
new file mode 100644
index 000000000000..fd882e3befa1
--- /dev/null
+++ b/test/type_3_test.c
@@ -0,0 +1,242 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include <string.h>
+#include "cbor.h"
+
+cbor_item_t *string;
+struct cbor_load_result res;
+
+unsigned char empty_string_data[] = {0x60};
+
+static void test_empty_string(void **state) {
+ string = cbor_load(empty_string_data, 1, &res);
+ assert_non_null(string);
+ assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
+ assert_true(cbor_isa_string(string));
+ assert_int_equal(cbor_string_length(string), 0);
+ assert_int_equal(cbor_string_codepoint_count(string), 0);
+ assert_true(res.read == 1);
+ cbor_decref(&string);
+ assert_null(string);
+}
+
+unsigned char short_string_data[] = {0x6C, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20,
+ 0x77, 0x6F, 0x72, 0x6C, 0x64, 0x21};
+
+/* 0x60 + 12 | Hello world! */
+static void test_short_string(void **state) {
+ string = cbor_load(short_string_data, 13, &res);
+ assert_non_null(string);
+ assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
+ assert_true(cbor_isa_string(string));
+ assert_int_equal(cbor_string_length(string), 12);
+ assert_int_equal(cbor_string_codepoint_count(string), 12);
+ assert_memory_equal(&"Hello world!", cbor_string_handle(string), 12);
+ assert_true(res.read == 13);
+ cbor_decref(&string);
+ assert_null(string);
+}
+
+unsigned char short_multibyte_string_data[] = {
+ 0x6F, 0xC4, 0x8C, 0x61, 0x75, 0x65, 0x73, 0x20,
+ 0xC3, 0x9F, 0x76, 0xC4, 0x9B, 0x74, 0x65, 0x21};
+
+/* 0x60 + 15 | Čaues ßvěte! */
+static void test_short_multibyte_string(void **state) {
+ string = cbor_load(short_multibyte_string_data, 16, &res);
+ assert_non_null(string);
+ assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
+ assert_true(cbor_isa_string(string));
+ assert_int_equal(cbor_string_length(string), 15);
+ assert_int_equal(cbor_string_codepoint_count(string), 12);
+ assert_memory_equal(&"Čaues ßvěte!", cbor_string_handle(string), 15);
+ assert_true(res.read == 16);
+ cbor_decref(&string);
+ assert_null(string);
+}
+
+unsigned char int8_string_data[] = {
+ 0x78, 0x96, 0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73, 0x75,
+ 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20,
+ 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x65, 0x63,
+ 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73,
+ 0x63, 0x69, 0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2E, 0x20, 0x44,
+ 0x6F, 0x6E, 0x65, 0x63, 0x20, 0x6D, 0x69, 0x20, 0x74, 0x65, 0x6C, 0x6C,
+ 0x75, 0x73, 0x2C, 0x20, 0x69, 0x61, 0x63, 0x75, 0x6C, 0x69, 0x73, 0x20,
+ 0x6E, 0x65, 0x63, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6C,
+ 0x75, 0x6D, 0x20, 0x71, 0x75, 0x69, 0x73, 0x2C, 0x20, 0x66, 0x65, 0x72,
+ 0x6D, 0x65, 0x6E, 0x74, 0x75, 0x6D, 0x20, 0x6E, 0x6F, 0x6E, 0x20, 0x66,
+ 0x65, 0x6C, 0x69, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x65, 0x63, 0x65, 0x6E,
+ 0x61, 0x73, 0x20, 0x75, 0x74, 0x20, 0x6A, 0x75, 0x73, 0x74, 0x6F, 0x20,
+ 0x70, 0x6F, 0x73, 0x75, 0x65, 0x72, 0x65, 0x2E};
+
+/* 150 | Lorem ....*/
+static void test_int8_string(void **state) {
+ string = cbor_load(int8_string_data, 152, &res);
+ assert_non_null(string);
+ assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
+ assert_true(cbor_isa_string(string));
+ assert_int_equal(cbor_string_length(string), 150);
+ assert_int_equal(cbor_string_codepoint_count(string), 150);
+ assert_memory_equal(
+ &"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.",
+ cbor_string_handle(string),
+ 150
+ );
+ assert_true(res.read == 152);
+ cbor_decref(&string);
+ assert_null(string);
+}
+
+unsigned char int16_string_data[] = {
+ 0x79, 0x00, 0x96, 0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73,
+ 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x20, 0x73, 0x69, 0x74,
+ 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x65,
+ 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69,
+ 0x73, 0x63, 0x69, 0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2E, 0x20,
+ 0x44, 0x6F, 0x6E, 0x65, 0x63, 0x20, 0x6D, 0x69, 0x20, 0x74, 0x65, 0x6C,
+ 0x6C, 0x75, 0x73, 0x2C, 0x20, 0x69, 0x61, 0x63, 0x75, 0x6C, 0x69, 0x73,
+ 0x20, 0x6E, 0x65, 0x63, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69, 0x62, 0x75,
+ 0x6C, 0x75, 0x6D, 0x20, 0x71, 0x75, 0x69, 0x73, 0x2C, 0x20, 0x66, 0x65,
+ 0x72, 0x6D, 0x65, 0x6E, 0x74, 0x75, 0x6D, 0x20, 0x6E, 0x6F, 0x6E, 0x20,
+ 0x66, 0x65, 0x6C, 0x69, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x65, 0x63, 0x65,
+ 0x6E, 0x61, 0x73, 0x20, 0x75, 0x74, 0x20, 0x6A, 0x75, 0x73, 0x74, 0x6F,
+ 0x20, 0x70, 0x6F, 0x73, 0x75, 0x65, 0x72, 0x65, 0x2E};
+/* 150 | Lorem ....*/
+/* This valid but not realistic - length 150 could be encoded in a single
+ * uint8_t (but we need to keep the test files reasonably compact) */
+static void test_int16_string(void **state) {
+ string = cbor_load(int16_string_data, 153, &res);
+ assert_non_null(string);
+ assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
+ assert_true(cbor_isa_string(string));
+ assert_int_equal(cbor_string_length(string), 150);
+ assert_int_equal(cbor_string_codepoint_count(string), 150);
+ assert_memory_equal(
+ &"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.",
+ cbor_string_handle(string),
+ 150
+ );
+ assert_true(res.read == 153);
+ cbor_decref(&string);
+ assert_null(string);
+}
+
+unsigned char int32_string_data[] = {
+ 0x7A, 0x00, 0x00, 0x00, 0x96, 0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20, 0x69,
+ 0x70, 0x73, 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x20, 0x73,
+ 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20, 0x63, 0x6F, 0x6E,
+ 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69,
+ 0x70, 0x69, 0x73, 0x63, 0x69, 0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74,
+ 0x2E, 0x20, 0x44, 0x6F, 0x6E, 0x65, 0x63, 0x20, 0x6D, 0x69, 0x20, 0x74,
+ 0x65, 0x6C, 0x6C, 0x75, 0x73, 0x2C, 0x20, 0x69, 0x61, 0x63, 0x75, 0x6C,
+ 0x69, 0x73, 0x20, 0x6E, 0x65, 0x63, 0x20, 0x76, 0x65, 0x73, 0x74, 0x69,
+ 0x62, 0x75, 0x6C, 0x75, 0x6D, 0x20, 0x71, 0x75, 0x69, 0x73, 0x2C, 0x20,
+ 0x66, 0x65, 0x72, 0x6D, 0x65, 0x6E, 0x74, 0x75, 0x6D, 0x20, 0x6E, 0x6F,
+ 0x6E, 0x20, 0x66, 0x65, 0x6C, 0x69, 0x73, 0x2E, 0x20, 0x4D, 0x61, 0x65,
+ 0x63, 0x65, 0x6E, 0x61, 0x73, 0x20, 0x75, 0x74, 0x20, 0x6A, 0x75, 0x73,
+ 0x74, 0x6F, 0x20, 0x70, 0x6F, 0x73, 0x75, 0x65, 0x72, 0x65, 0x2E};
+
+/* 150 | Lorem ....*/
+static void test_int32_string(void **state) {
+ string = cbor_load(int32_string_data, 155, &res);
+ assert_non_null(string);
+ assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
+ assert_true(cbor_isa_string(string));
+ assert_int_equal(cbor_string_length(string), 150);
+ assert_int_equal(cbor_string_codepoint_count(string), 150);
+ assert_memory_equal(
+ &"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.",
+ cbor_string_handle(string),
+ 150
+ );
+ assert_true(res.read == 155);
+ cbor_decref(&string);
+ assert_null(string);
+}
+
+unsigned char int64_string_data[] = {
+ 0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x96, 0x4C, 0x6F, 0x72,
+ 0x65, 0x6D, 0x20, 0x69, 0x70, 0x73, 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C,
+ 0x6F, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C,
+ 0x20, 0x63, 0x6F, 0x6E, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72,
+ 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69, 0x6E, 0x67, 0x20,
+ 0x65, 0x6C, 0x69, 0x74, 0x2E, 0x20, 0x44, 0x6F, 0x6E, 0x65, 0x63, 0x20,
+ 0x6D, 0x69, 0x20, 0x74, 0x65, 0x6C, 0x6C, 0x75, 0x73, 0x2C, 0x20, 0x69,
+ 0x61, 0x63, 0x75, 0x6C, 0x69, 0x73, 0x20, 0x6E, 0x65, 0x63, 0x20, 0x76,
+ 0x65, 0x73, 0x74, 0x69, 0x62, 0x75, 0x6C, 0x75, 0x6D, 0x20, 0x71, 0x75,
+ 0x69, 0x73, 0x2C, 0x20, 0x66, 0x65, 0x72, 0x6D, 0x65, 0x6E, 0x74, 0x75,
+ 0x6D, 0x20, 0x6E, 0x6F, 0x6E, 0x20, 0x66, 0x65, 0x6C, 0x69, 0x73, 0x2E,
+ 0x20, 0x4D, 0x61, 0x65, 0x63, 0x65, 0x6E, 0x61, 0x73, 0x20, 0x75, 0x74,
+ 0x20, 0x6A, 0x75, 0x73, 0x74, 0x6F, 0x20, 0x70, 0x6F, 0x73, 0x75, 0x65,
+ 0x72, 0x65, 0x2E};
+
+/* 150 | Lorem ....*/
+static void test_int64_string(void **state) {
+ string = cbor_load(int64_string_data, 159, &res);
+ assert_non_null(string);
+ assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
+ assert_true(cbor_isa_string(string));
+ assert_int_equal(cbor_string_length(string), 150);
+ assert_int_equal(cbor_string_codepoint_count(string), 150);
+ assert_memory_equal(
+ &"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec mi tellus, iaculis nec vestibulum quis, fermentum non felis. Maecenas ut justo posuere.",
+ cbor_string_handle(string),
+ 150
+ );
+ assert_true(res.read == 159);
+ cbor_decref(&string);
+ assert_null(string);
+}
+
+unsigned char short_indef_string_data[] = {0x7F, 0x78, 0x01, 0x65, 0xFF, 0xFF};
+
+/* start | string | break| extra
+ */
+
+static void test_short_indef_string(void **state) {
+ string = cbor_load(short_indef_string_data, 6, &res);
+ assert_non_null(string);
+ assert_true(cbor_typeof(string) == CBOR_TYPE_STRING);
+ assert_true(cbor_isa_string(string));
+ assert_true(cbor_string_length(string) == 0);
+ assert_true(cbor_string_is_indefinite(string));
+ assert_true(cbor_string_chunk_count(string) == 1);
+ assert_true(res.read == 5);
+ assert_true(cbor_isa_string(cbor_string_chunks_handle(string)[0]));
+ assert_true(cbor_string_length(cbor_string_chunks_handle(string)[0]) == 1);
+ assert_true(*cbor_string_handle(cbor_string_chunks_handle(string)[0]) == 'e');
+ cbor_decref(&string);
+ assert_null(string);
+}
+
+static void test_inline_creation(void **state) {
+ string = cbor_build_string("Hello!");
+ assert_memory_equal(cbor_string_handle(string), "Hello!", strlen("Hello!"));
+ cbor_decref(&string);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_empty_string),
+ cmocka_unit_test(test_short_string),
+ cmocka_unit_test(test_short_multibyte_string),
+ cmocka_unit_test(test_int8_string),
+ cmocka_unit_test(test_int16_string),
+ cmocka_unit_test(test_int32_string),
+ cmocka_unit_test(test_int64_string),
+ cmocka_unit_test(test_short_indef_string),
+ cmocka_unit_test(test_inline_creation)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_4_encoders_test.c b/test/type_4_encoders_test.c
new file mode 100644
index 000000000000..fbd036bbf6d6
--- /dev/null
+++ b/test/type_4_encoders_test.c
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+unsigned char buffer[512];
+
+static void test_embedded_array_start(void **state) {
+ assert_int_equal(1, cbor_encode_array_start(1, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x81}), 1);
+}
+
+static void test_array_start(void **state) {
+ assert_int_equal(5, cbor_encode_array_start(1000000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x9A, 0x00, 0x0F, 0x42, 0x40}),
+ 5);
+}
+
+static void test_indef_array_start(void **state) {
+ assert_int_equal(1, cbor_encode_indef_array_start(buffer, 512));
+ assert_int_equal(0, cbor_encode_indef_array_start(buffer, 0));
+ assert_memory_equal(buffer, ((unsigned char[]){0x9F}), 1);
+}
+
+static void test_indef_array_encoding(void **state) {
+ cbor_item_t *array = cbor_new_indefinite_array();
+ cbor_item_t *one = cbor_build_uint8(1);
+ cbor_item_t *two = cbor_build_uint8(2);
+ cbor_array_push(array, one);
+ cbor_array_push(array, two);
+ assert_int_equal(4, cbor_serialize_array(array, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0x9F, 0x01, 0x02, 0xFF}), 4);
+ cbor_decref(&array);
+ cbor_decref(&one);
+ cbor_decref(&two);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_embedded_array_start),
+ cmocka_unit_test(test_array_start),
+ cmocka_unit_test(test_indef_array_start),
+ cmocka_unit_test(test_indef_array_encoding)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_4_test.c b/test/type_4_test.c
new file mode 100644
index 000000000000..b208c4e8f466
--- /dev/null
+++ b/test/type_4_test.c
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "assertions.h"
+#include "cbor.h"
+
+cbor_item_t *arr;
+struct cbor_load_result res;
+
+unsigned char data1[] = {0x80, 0xFF};
+
+static void test_empty_array(void **state) {
+ arr = cbor_load(data1, 2, &res);
+ assert_non_null(arr);
+ assert_true(cbor_typeof(arr) == CBOR_TYPE_ARRAY);
+ assert_true(cbor_isa_array(arr));
+ assert_true(cbor_array_size(arr) == 0);
+ assert_true(res.read == 1);
+ cbor_decref(&arr);
+ assert_null(arr);
+}
+
+unsigned char data2[] = {0x81, 0x01, 0xFF};
+
+static void test_simple_array(void **state) {
+ arr = cbor_load(data2, 3, &res);
+ assert_non_null(arr);
+ assert_true(cbor_typeof(arr) == CBOR_TYPE_ARRAY);
+ assert_true(cbor_isa_array(arr));
+ assert_int_equal(cbor_array_size(arr), 1);
+ assert_true(res.read == 2);
+ assert_int_equal(cbor_array_allocated(arr), 1);
+ /* Check the values */
+ assert_uint8(cbor_array_handle(arr)[0], 1);
+ cbor_item_t *intermediate = cbor_array_get(arr, 0);
+ assert_uint8(intermediate, 1);
+
+ cbor_item_t *new_val = cbor_build_uint8(10);
+ assert_false(cbor_array_set(arr, 1, new_val));
+ assert_false(cbor_array_set(arr, 3, new_val));
+ cbor_decref(&new_val);
+
+ cbor_decref(&arr);
+ cbor_decref(&intermediate);
+ assert_null(arr);
+ assert_null(intermediate);
+}
+
+unsigned char data3[] = {0x82, 0x01, 0x81, 0x01, 0xFF};
+
+static void test_nested_arrays(void **state) {
+ arr = cbor_load(data3, 5, &res);
+ assert_non_null(arr);
+ assert_true(cbor_typeof(arr) == CBOR_TYPE_ARRAY);
+ assert_true(cbor_isa_array(arr));
+ assert_true(cbor_array_size(arr) == 2);
+ assert_true(res.read == 4);
+ /* Check the values */
+ assert_uint8(cbor_array_handle(arr)[0], 1);
+
+ cbor_item_t *nested = cbor_array_handle(arr)[1];
+ assert_true(cbor_isa_array(nested));
+ assert_true(cbor_array_size(nested) == 1);
+ assert_uint8(cbor_array_handle(nested)[0], 1);
+
+ cbor_decref(&arr);
+ assert_null(arr);
+}
+
+unsigned char test_indef_arrays_data[] = {0x9f, 0x01, 0x02, 0xFF};
+
+static void test_indef_arrays(void **state) {
+ arr = cbor_load(test_indef_arrays_data, 4, &res);
+ assert_non_null(arr);
+ assert_true(cbor_typeof(arr) == CBOR_TYPE_ARRAY);
+ assert_true(cbor_isa_array(arr));
+ assert_true(cbor_array_size(arr) == 2);
+ assert_true(res.read == 4);
+ /* Check the values */
+ assert_uint8(cbor_array_handle(arr)[0], 1);
+ assert_uint8(cbor_array_handle(arr)[1], 2);
+
+ assert_true(cbor_array_set(arr, 1, cbor_move(cbor_build_uint8(10))));
+
+ cbor_decref(&arr);
+ assert_null(arr);
+}
+
+unsigned char test_nested_indef_arrays_data[] = {0x9f, 0x01, 0x9f, 0x02,
+ 0xFF, 0x03, 0xFF};
+
+static void test_nested_indef_arrays(void **state) {
+ arr = cbor_load(test_nested_indef_arrays_data, 7, &res);
+ assert_non_null(arr);
+ assert_true(cbor_typeof(arr) == CBOR_TYPE_ARRAY);
+ assert_true(cbor_isa_array(arr));
+ assert_int_equal(cbor_array_size(arr), 3);
+ assert_true(res.read == 7);
+ /* Check the values */
+ assert_uint8(cbor_array_handle(arr)[0], 1);
+
+ cbor_item_t *nested = cbor_array_handle(arr)[1];
+ assert_true(cbor_isa_array(nested));
+ assert_true(cbor_array_size(nested) == 1);
+ assert_uint8(cbor_array_handle(nested)[0], 2);
+
+ cbor_decref(&arr);
+ assert_null(arr);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_empty_array), cmocka_unit_test(test_simple_array),
+ cmocka_unit_test(test_nested_arrays), cmocka_unit_test(test_indef_arrays),
+ cmocka_unit_test(test_nested_indef_arrays)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_5_encoders_test.c b/test/type_5_encoders_test.c
new file mode 100644
index 000000000000..dc6e430e74ed
--- /dev/null
+++ b/test/type_5_encoders_test.c
@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+unsigned char buffer[512];
+
+static void test_embedded_map_start(void **state) {
+ assert_int_equal(1, cbor_encode_map_start(1, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0xA1}), 1);
+}
+
+static void test_map_start(void **state) {
+ assert_int_equal(5, cbor_encode_map_start(1000000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0xBA, 0x00, 0x0F, 0x42, 0x40}),
+ 5);
+}
+
+static void test_indef_map_start(void **state) {
+ assert_int_equal(1, cbor_encode_indef_map_start(buffer, 512));
+ assert_int_equal(0, cbor_encode_indef_map_start(buffer, 0));
+ assert_memory_equal(buffer, ((unsigned char[]){0xBF}), 1);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {cmocka_unit_test(test_embedded_map_start),
+ cmocka_unit_test(test_map_start),
+ cmocka_unit_test(test_indef_map_start)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_5_test.c b/test/type_5_test.c
new file mode 100644
index 000000000000..dbad446d8ff7
--- /dev/null
+++ b/test/type_5_test.c
@@ -0,0 +1,190 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include <string.h>
+#include "assertions.h"
+#include "cbor.h"
+
+cbor_item_t *map;
+struct cbor_load_result res;
+
+unsigned char empty_map[] = {0xA0};
+
+static void test_empty_map(void **state) {
+ map = cbor_load(empty_map, 1, &res);
+ assert_non_null(map);
+ assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
+ assert_true(cbor_isa_map(map));
+ assert_true(cbor_map_size(map) == 0);
+ assert_true(res.read == 1);
+ assert_int_equal(cbor_map_allocated(map), 0);
+ cbor_decref(&map);
+ assert_null(map);
+}
+
+unsigned char simple_map[] = {0xA2, 0x01, 0x02, 0x03, 0x04};
+
+/* {1: 2, 3: 4} */
+static void test_simple_map(void **state) {
+ map = cbor_load(simple_map, 5, &res);
+ assert_non_null(map);
+ assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
+ assert_true(cbor_isa_map(map));
+ assert_true(cbor_map_is_definite(map));
+ assert_true(cbor_map_size(map) == 2);
+ assert_true(res.read == 5);
+ struct cbor_pair *handle = cbor_map_handle(map);
+ assert_uint8(handle[0].key, 1);
+ assert_uint8(handle[0].value, 2);
+ assert_uint8(handle[1].key, 3);
+ assert_uint8(handle[1].value, 4);
+ cbor_decref(&map);
+ assert_null(map);
+}
+
+unsigned char simple_indef_map[] = {0xBF, 0x01, 0x02, 0x03, 0x04, 0xFF};
+
+/* {_ 1: 2, 3: 4} */
+static void test_indef_simple_map(void **state) {
+ map = cbor_load(simple_indef_map, 6, &res);
+ assert_non_null(map);
+ assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
+ assert_true(cbor_isa_map(map));
+ assert_true(cbor_map_is_indefinite(map));
+ assert_true(cbor_map_size(map) == 2);
+ assert_true(res.read == 6);
+ struct cbor_pair *handle = cbor_map_handle(map);
+ assert_uint8(handle[0].key, 1);
+ assert_uint8(handle[0].value, 2);
+ assert_uint8(handle[1].key, 3);
+ assert_uint8(handle[1].value, 4);
+ cbor_decref(&map);
+ assert_null(map);
+}
+
+//{
+// "glossary": {
+// "title": "example glossary"
+// }
+//}
+unsigned char def_nested_map[] = {
+ 0xA1, 0x68, 0x67, 0x6C, 0x6F, 0x73, 0x73, 0x61, 0x72, 0x79, 0xA1, 0x65,
+ 0x74, 0x69, 0x74, 0x6C, 0x65, 0x70, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C,
+ 0x65, 0x20, 0x67, 0x6C, 0x6F, 0x73, 0x73, 0x61, 0x72, 0x79};
+
+static void test_def_nested_map(void **state) {
+ map = cbor_load(def_nested_map, 34, &res);
+ assert_non_null(map);
+ assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
+ assert_true(cbor_isa_map(map));
+ assert_true(cbor_map_is_definite(map));
+ assert_true(cbor_map_size(map) == 1);
+ assert_true(res.read == 34);
+ struct cbor_pair *handle = cbor_map_handle(map);
+ assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
+ assert_true(cbor_typeof(handle[0].value) == CBOR_TYPE_MAP);
+ struct cbor_pair *inner_handle = cbor_map_handle(handle[0].value);
+ assert_true(cbor_typeof(inner_handle[0].key) == CBOR_TYPE_STRING);
+ assert_true(cbor_typeof(inner_handle[0].value) == CBOR_TYPE_STRING);
+ assert_memory_equal(cbor_string_handle(inner_handle[0].value),
+ "example glossary", strlen("example glossary"));
+ cbor_decref(&map);
+ assert_null(map);
+}
+
+unsigned char streamed_key_map[] = {0xA1, 0x7F, 0x61, 0x61,
+ 0x61, 0x62, 0xFF, 0xA0};
+
+/* '{ (_"a" "b"): {}}' */
+static void test_streamed_key_map(void **state) {
+ map = cbor_load(streamed_key_map, 8, &res);
+ assert_non_null(map);
+ assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
+ assert_true(cbor_isa_map(map));
+ assert_true(cbor_map_is_definite(map));
+ assert_true(cbor_map_size(map) == 1);
+ assert_true(res.read == 8);
+ struct cbor_pair *handle = cbor_map_handle(map);
+ assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
+ assert_true(cbor_string_is_indefinite(handle[0].key));
+ assert_int_equal(cbor_string_chunk_count(handle[0].key), 2);
+ assert_true(cbor_isa_map(handle[0].value));
+ assert_int_equal(cbor_map_size(handle[0].value), 0);
+ cbor_decref(&map);
+ assert_null(map);
+}
+
+unsigned char streamed_kv_map[] = {0xA1, 0x7F, 0x61, 0x61, 0x61, 0x62, 0xFF,
+ 0x7F, 0x61, 0x63, 0x61, 0x64, 0xFF};
+
+/* '{ (_"a" "b"): (_"c", "d")}' */
+static void test_streamed_kv_map(void **state) {
+ map = cbor_load(streamed_kv_map, 13, &res);
+ assert_non_null(map);
+ assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
+ assert_true(cbor_isa_map(map));
+ assert_true(cbor_map_is_definite(map));
+ assert_int_equal(cbor_map_size(map), 1);
+ assert_int_equal(res.read, 13);
+ struct cbor_pair *handle = cbor_map_handle(map);
+ assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
+ assert_true(cbor_string_is_indefinite(handle[0].key));
+ assert_int_equal(cbor_string_chunk_count(handle[0].key), 2);
+ assert_true(cbor_typeof(handle[0].value) == CBOR_TYPE_STRING);
+ assert_true(cbor_string_is_indefinite(handle[0].value));
+ assert_int_equal(cbor_string_chunk_count(handle[0].value), 2);
+ assert_memory_equal(
+ cbor_string_handle(cbor_string_chunks_handle(handle[0].value)[1]), "d",
+ 1);
+ cbor_decref(&map);
+ assert_null(map);
+}
+
+unsigned char streamed_streamed_kv_map[] = {0xBF, 0x7F, 0x61, 0x61, 0x61,
+ 0x62, 0xFF, 0x7F, 0x61, 0x63,
+ 0x61, 0x64, 0xFF, 0xFF};
+
+/* '{_ (_"a" "b"): (_"c", "d")}' */
+static void test_streamed_streamed_kv_map(void **state) {
+ map = cbor_load(streamed_streamed_kv_map, 14, &res);
+ assert_non_null(map);
+ assert_true(cbor_typeof(map) == CBOR_TYPE_MAP);
+ assert_true(cbor_isa_map(map));
+ assert_true(cbor_map_is_indefinite(map));
+ assert_int_equal(cbor_map_size(map), 1);
+ assert_int_equal(res.read, 14);
+ struct cbor_pair *handle = cbor_map_handle(map);
+ assert_true(cbor_typeof(handle[0].key) == CBOR_TYPE_STRING);
+ assert_true(cbor_string_is_indefinite(handle[0].key));
+ assert_int_equal(cbor_string_chunk_count(handle[0].key), 2);
+ assert_true(cbor_typeof(handle[0].value) == CBOR_TYPE_STRING);
+ assert_true(cbor_string_is_indefinite(handle[0].value));
+ assert_int_equal(cbor_string_chunk_count(handle[0].value), 2);
+ assert_memory_equal(
+ cbor_string_handle(cbor_string_chunks_handle(handle[0].value)[1]), "d",
+ 1);
+ cbor_decref(&map);
+ assert_null(map);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_empty_map),
+ cmocka_unit_test(test_simple_map),
+ cmocka_unit_test(test_indef_simple_map),
+ cmocka_unit_test(test_def_nested_map),
+ cmocka_unit_test(test_streamed_key_map),
+ cmocka_unit_test(test_streamed_kv_map),
+ cmocka_unit_test(test_streamed_streamed_kv_map)};
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_6_encoders_test.c b/test/type_6_encoders_test.c
new file mode 100644
index 000000000000..84b66da00e33
--- /dev/null
+++ b/test/type_6_encoders_test.c
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "cbor.h"
+
+unsigned char buffer[512];
+
+static void test_embedded_tag(void **state) {
+ assert_int_equal(1, cbor_encode_tag(1, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0xC1}), 1);
+}
+
+static void test_tag(void **state) {
+ assert_int_equal(5, cbor_encode_tag(1000000, buffer, 512));
+ assert_memory_equal(buffer, ((unsigned char[]){0xDA, 0x00, 0x0F, 0x42, 0x40}),
+ 5);
+}
+
+int main(void) {
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_embedded_tag),
+ cmocka_unit_test(test_tag),
+ };
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/type_6_test.c b/test/type_6_test.c
new file mode 100644
index 000000000000..1d54afcdaaf5
--- /dev/null
+++ b/test/type_6_test.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
+ *
+ * libcbor is free software; you can redistribute it and/or modify
+ * it under the terms of the MIT license. See LICENSE for details.
+ */
+
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stddef.h>
+
+#include <cmocka.h>
+
+#include "assertions.h"
+#include "cbor.h"
+
+cbor_item_t *tag;
+struct cbor_load_result res;
+
+unsigned char embedded_tag_data[] = {0xC0, 0x00};
+
+static void test_refcounting(void **state) {
+ tag = cbor_load(embedded_tag_data, 2, &res);
+ assert_true(cbor_refcount(tag) == 1);
+ cbor_item_t *item = cbor_tag_item(tag);
+ assert_true(cbor_refcount(item) == 2);
+ cbor_decref(&tag);
+ assert_null(tag);
+ assert_true(cbor_refcount(item) == 1);
+ cbor_decref(&item);
+ assert_null(item);
+}
+
+/* Tag 0 + uint 0 */
+static void test_embedded_tag(void **state) {
+ tag = cbor_load(embedded_tag_data, 2, &res);
+ assert_true(cbor_typeof(tag) == CBOR_TYPE_TAG);
+ assert_true(cbor_tag_value(tag) == 0);
+ assert_uint8(cbor_move(cbor_tag_item(tag)), 0);
+ cbor_decref(&tag);
+ assert_null(tag);
+}
+
+unsigned char int8_tag_data[] = {0xD8, 0xFF, 0x01};
+
+/* Tag 255 + uint 1 */
+static void test_int8_tag(void **state) {
+ tag = cbor_load(int8_tag_data, 3, &res);
+ assert_true(cbor_typeof(tag) == CBOR_TYPE_TAG);
+ assert_true(cbor_tag_value(tag) == 255);
+ assert_uint8(cbor_move(cbor_tag_item(tag)), 1);
+ cbor_decref(&tag);
+ assert_null(tag);
+}
+
+unsigned char int16_tag_data[] = {0xD9, 0xFF, 0x00, 0x02};
+
+/* Tag 255 << 8 + uint 2 */
+static void test_int16_tag(void **state) {
+ tag = cbor_load(int16_tag_data, 4, &res);
+ assert_true(cbor_typeof(tag) == CBOR_TYPE_TAG);
+ assert_true(cbor_tag_value(tag) == 255 << 8);
+ assert_uint8(cbor_move(cbor_tag_item(tag)), 2);
+ cbor_decref(&tag);
+ assert_null(tag);
+}
+
+unsigned char int32_tag_data[] = {0xDA, 0xFF, 0x00, 0x00, 0x00, 0x03};
+
+/* uint 3 */
+static void test_int32_tag(void **state) {
+ tag = cbor_load(int32_tag_data, 6, &res);
+ assert_true(cbor_typeof(tag) == CBOR_TYPE_TAG);
+ assert_true(cbor_tag_value(tag) == 4278190080ULL);
+ assert_uint8(cbor_move(cbor_tag_item(tag)), 3);
+ cbor_decref(&tag);
+ assert_null(tag);
+}
+
+unsigned char int64_tag_data[] = {0xDB, 0xFF, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x04};
+
+/* uint 4 */
+static void test_int64_tag(void **state) {
+ tag = cbor_load(int64_tag_data, 10, &res);
+ assert_true(cbor_typeof(tag) == CBOR_TYPE_TAG);
+ assert_true(cbor_tag_value(tag) == 18374686479671623680ULL);
+ assert_uint8(cbor_move(cbor_tag_item(tag)), 4);
+ cbor_decref(&tag);
+ assert_null(tag);