LIBCBOR(3) | libcbor | LIBCBOR(3) |
NAME¶
libcbor - libcbor Documentation
Documentation for version 0.10.1, updated on Jan 16, 2023.
OVERVIEW¶
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
- Complete RFC conformance [1]
- Robust C99 implementation
- Layered architecture offers both control and convenience
- Flexible memory management
- No shared global state - threading friendly [2]
- Proper handling of UTF-8
- Full support for streams & incremental processing
- Extensive documentation and test suite
- No runtime dependencies, small footprint
- [1]
- See RFC conformance
- [2]
- With the exception of custom memory allocators (see Memory management and reference counting)
CONTENTS¶
Getting started¶
Pre-built Linux packages are available in most mainstream distributions
Ubuntu, Debian, etc.:
apt-get install libcbor-dev
Fedora, openSUSE, etc.:
yum install libcbor-devel
OS X users can use Homebrew:
brew install libcbor
For other platforms, you will need to compile it from source.
Building & installing libcbor¶
- 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, ...)
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 | Fuzz test 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 Development) | OFF | ON, OFF |
The following configuration options will also be defined as macros [1] in <cbor/common.h> and can therefore be used in client code:
Option | Meaning | Default | Possible values |
CBOR_PRETTY_PRINTER | Include a pretty-printing routine | ON | ON, OFF |
CBOR_BUFFER_GROWTH | Factor for buffer growth & shrinking | 2 | Decimals > 1 |
- [1]
- ON & OFF will be translated to 1 and 0
using cmakedefine.
If you want to pass other custom configuration options, please refer to http://www.cmake.org/Wiki/CMake_Useful_Variables.
WARNING:
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:
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:
cmake -DCMAKE_BUILD_TYPE=Release path_to_libcbor_dir make cbor
... or as a dynamic library:
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON path_to_libcbor_dir make cbor
To install locally:
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 test suite. If you encounter any problems, please report them to the issue tracker.
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
#include <cbor.h> #include <stdio.h> int main(int argc, char * argv[]) {
printf("Hello from libcbor %s\n", CBOR_VERSION); }
and compiling it
cc hello_cbor.c -lcbor -o hello_cbor
libcbor also comes with pkg-config support. If you install libcbor with a custom prefix, you can use pkg-config to resolve the headers and objects:
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, 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" (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:
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
... -- 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.:
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.:
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:
⇒ 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.
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
#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 - Type 4 – Arrays
- cbor/bytestrings.h - Type 2 – Byte strings
- cbor/callbacks.h - Callbacks used for 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 Streaming Encoding
- cbor/floats_ctrls.h - Type 7 – Floats & control tokens
- cbor/ints.h - Types 0 & 1 – Positive and negative integers
- cbor/maps.h - Type 5 – Maps
- cbor/serialization.h - High level serialization such as cbor_serialize()
- cbor/streaming.h - Home of cbor_stream_decode()
- cbor/strings.h - Type 3 – UTF-8 strings
- cbor/tags.h - Type 6 – Semantic tags
Using libcbor¶
If you want to get more familiar with CBOR, we recommend the 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 API documentation should then provide with all the information you may need.
Creating and serializing items
#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: `buffer_size` bytes of data in the `buffer` */
unsigned char * buffer;
size_t buffer_size;
cbor_serialize_alloc(root, &buffer, &buffer_size);
fwrite(buffer, 1, buffer_size, stdout);
free(buffer);
fflush(stdout);
cbor_decref(&root); }
Reading serialized data
#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
#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); }
API¶
The data API is centered around 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: 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. 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. [1] 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.
- 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
Types of items¶
Every cbor_item_t has a cbor_type associated with it - these constants correspond to the types specified by the CBOR standard:
- enum cbor_type
- Specifies the Major type of cbor_item_t.
Values:
- enumerator CBOR_TYPE_UINT
- 0 - positive integers
- enumerator CBOR_TYPE_NEGINT
- 1 - negative integers
- enumerator CBOR_TYPE_BYTESTRING
- 2 - byte strings
- enumerator CBOR_TYPE_STRING
- 3 - strings
- enumerator CBOR_TYPE_ARRAY
- 4 - arrays
- enumerator CBOR_TYPE_MAP
- 5 - maps
- enumerator CBOR_TYPE_TAG
- 6 - tags
- enumerator CBOR_TYPE_FLOAT_CTRL
- 7 - decimals and special values (true, false, nil, ...)
To find out the type of an item, one can use
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_type cbor_typeof (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 26]
_CBOR_NODISCARD cbor_type cbor_typeof (const cbor_item_t *item)
--------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 26]
_CBOR_NODISCARD cbor_type cbor_typeof (const cbor_item_t *item)
--------------------------^
Please note the distinction between functions like cbor_isa_uint() and 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:
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_isa_uint (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_uint (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_uint (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_isa_negint (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_negint (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_negint (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_isa_bytestring (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_bytestring (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_bytestring (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_isa_string (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_string (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_string (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_isa_array (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_array (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_array (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_isa_map (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_map (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_map (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_isa_tag (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_tag (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_tag (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_isa_float_ctrl (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_float_ctrl (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_isa_float_ctrl (const cbor_item_t *item)
--------------------^
Logical queries¶
These functions provide information about the item type from a more high-level perspective
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_is_int (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_is_int (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_is_int (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_is_float (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_is_float (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_is_float (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_is_bool (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_is_bool (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_is_bool (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_is_null (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_is_null (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_is_null (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_is_undef (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_is_undef (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_is_undef (const cbor_item_t *item)
--------------------^
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 doesn'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).
cbor_set_allocs(malloc, realloc, free);
- void cbor_set_allocs(_cbor_malloc_t custom_malloc, _cbor_realloc_t custom_realloc, _cbor_free_t custom_free)
- Sets the memory management routines to use.
NOTE:
WARNING:
- param custom_malloc
- malloc implementation
- param custom_realloc
- realloc implementation
- param custom_free
- free implementation
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 a very simple GC when integrating libcbor into a 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 - the 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 cbor_decref().
- cbor_item_t *cbor_incref(cbor_item_t *item)
- Increases the reference count by one.
No dependent items are affected.
- param item[incref]
- item the item
- return
- the input reference
- void cbor_decref(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
- void cbor_intermediate_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
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_refcount (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_refcount (const cbor_item_t *item)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_refcount (const cbor_item_t *item)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_move (cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_move (cbor_item_t *item)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_move (cbor_item_t *item)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_copy (cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_copy (cbor_item_t *item)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_copy (cbor_item_t *item)
----------------------------^
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
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_load (cbor_data source, size_t source_size, struct cbor_load_result *result)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_load (cbor_data source, size_t source_size, struct cbor_load_result *result)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_load (cbor_data source, size_t source_size, struct cbor_load_result *result)
----------------------------^
Associated data structures¶
- enum cbor_error_code
- Possible decoding errors.
Values:
- enumerator CBOR_ERR_MEMERROR
- Memory error - item allocation failed.
Is it too big for your allocator?
- enumerator CBOR_ERR_SYNTAXERROR
- Stack parsing algorithm failed.
- struct cbor_load_result
- High-level decoding result.
Public Members
- struct cbor_error error
- Error indicator.
- size_t read
- Number of bytes read.
- struct cbor_error
- High-level decoding error.
Public Members
- size_t position
- Approximate position.
- cbor_error_code code
- Description.
Encoding¶
The easiest way to encode data items is using the cbor_serialize() or cbor_serialize_alloc() functions:
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_serialize (const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_serialize (const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_serialize (const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)
-----------------------^
- size_t cbor_serialize_alloc(const cbor_item_t *item, cbor_mutable_data *buffer, size_t *buffer_size)
- Serialize the given item, allocating buffers as needed.
Since libcbor v0.10, the return value is always the same as buffer_size (if provided, see https://github.com/PJK/libcbor/pull/251/). New clients should ignore the return value.
WARNING:
- param item[borrow]
- A data item
- param buffer[out]
- Buffer containing the result
- param buffer_size[out]
- Size of the buffer, or NULL
- return
- Length of the result. 0 on failure, in which case buffer is NULL.
To determine the number of bytes needed to serialize an item, use cbor_serialized_size():
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_serialized_size (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_serialized_size (const cbor_item_t *item)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_serialized_size (const cbor_item_t *item)
-----------------------^
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:
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_serialize_uint (const cbor_item_t *, cbor_mutable_data, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_uint (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_uint (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_serialize_negint (const cbor_item_t *, cbor_mutable_data, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_negint (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_negint (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_serialize_bytestring (const cbor_item_t *, cbor_mutable_data, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_bytestring (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_bytestring (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_serialize_string (const cbor_item_t *, cbor_mutable_data, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_string (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_string (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_serialize_array (const cbor_item_t *, cbor_mutable_data, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_array (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_array (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_serialize_map (const cbor_item_t *, cbor_mutable_data, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_map (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_map (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_serialize_tag (const cbor_item_t *, cbor_mutable_data, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_tag (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_tag (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_serialize_float_ctrl (const cbor_item_t *, cbor_mutable_data, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_float_ctrl (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_serialize_float_ctrl (const cbor_item_t *, cbor_mutable_data, size_t)
-----------------------^
Streaming Decoding¶
libcbor exposes a stateless decoder that reads a stream of input bytes from a buffer and invokes user-provided callbacks as it decodes the input:
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD struct cbor_decoder_result cbor_stream_decode (cbor_data source, size_t source_size, const struct cbor_callbacks *callbacks, void *context)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: struct [error at 22]
_CBOR_NODISCARD struct cbor_decoder_result cbor_stream_decode (cbor_data source, size_t source_size, const struct cbor_callbacks *callbacks, void *context)
----------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: struct [error at 22]
_CBOR_NODISCARD struct cbor_decoder_result cbor_stream_decode (cbor_data source, size_t source_size, const struct cbor_callbacks *callbacks, void *context)
----------------------^
For example, when cbor_stream_decode() encounters a 1B unsigned integer, it will invoke the function pointer stored in cbor_callbacks.uint8. Complete usage example: examples/streaming_parser.c
The callbacks are defined by
- struct cbor_callbacks
- Callback bundle — passed to the decoder.
Public Members
- cbor_int8_callback uint8
- Unsigned int.
- cbor_int16_callback uint16
- Unsigned int.
- cbor_int32_callback uint32
- Unsigned int.
- cbor_int64_callback uint64
- Unsigned int.
- cbor_int64_callback negint64
- Negative int.
- cbor_int32_callback negint32
- Negative int.
- cbor_int16_callback negint16
- Negative int.
- cbor_int8_callback negint8
- Negative int.
- cbor_simple_callback byte_string_start
- Definite byte string.
- cbor_string_callback byte_string
- Indefinite byte string start.
- cbor_string_callback string
- Definite string.
- cbor_simple_callback string_start
- Indefinite string start.
- cbor_simple_callback indef_array_start
- Definite array.
- cbor_collection_callback array_start
- Indefinite array.
- cbor_simple_callback indef_map_start
- Definite map.
- cbor_collection_callback map_start
- Indefinite map.
- cbor_int64_callback tag
- Tags.
- cbor_float_callback float2
- Half float.
- cbor_float_callback float4
- Single float.
- cbor_double_callback float8
- Double float.
- cbor_simple_callback indef_break
- Indefinite item break.
When building custom sets of callbacks, feel free to start from
- const struct cbor_callbacks cbor_empty_callbacks
- Dummy callback bundle - does nothing.
Callback types definition¶
- 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, uint64_t)
- Callback prototype.
- typedef void (*cbor_collection_callback)(void*, uint64_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 prototype.
Streaming Encoding¶
cbor/encoding.h exposes a low-level encoding API to encode CBOR objects on the fly. Unlike cbor_serialize(), these functions take logical values (integers, floats, strings, etc.) instead of cbor_item_t. The client is responsible for constructing the compound types correctly (e.g. terminating arrays).
Streaming encoding is typically used to create an streaming (indefinite length) CBOR strings, byte strings, arrays, and maps. Complete example: examples/streaming_array.c
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_uint8 (uint8_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_uint8 (uint8_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_uint8 (uint8_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_uint16 (uint16_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_uint16 (uint16_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_uint16 (uint16_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_uint32 (uint32_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_uint32 (uint32_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_uint32 (uint32_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_uint64 (uint64_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_uint64 (uint64_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_uint64 (uint64_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_uint (uint64_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_uint (uint64_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_uint (uint64_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_negint8 (uint8_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_negint8 (uint8_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_negint8 (uint8_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_negint16 (uint16_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_negint16 (uint16_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_negint16 (uint16_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_negint32 (uint32_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_negint32 (uint32_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_negint32 (uint32_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_negint64 (uint64_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_negint64 (uint64_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_negint64 (uint64_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_negint (uint64_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_negint (uint64_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_negint (uint64_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_bytestring_start (size_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_bytestring_start (size_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_bytestring_start (size_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_indef_bytestring_start (unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_indef_bytestring_start (unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_indef_bytestring_start (unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_string_start (size_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_string_start (size_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_string_start (size_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_indef_string_start (unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_indef_string_start (unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_indef_string_start (unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_array_start (size_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_array_start (size_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_array_start (size_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_indef_array_start (unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_indef_array_start (unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_indef_array_start (unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_map_start (size_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_map_start (size_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_map_start (size_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_indef_map_start (unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_indef_map_start (unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_indef_map_start (unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_tag (uint64_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_tag (uint64_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_tag (uint64_t, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_bool (bool, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_bool (bool, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_bool (bool, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_null (unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_null (unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_null (unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_undef (unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_undef (unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_undef (unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_half (float, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_half (float, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_half (float, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_single (float, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_single (float, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_single (float, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_double (double, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_double (double, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_double (double, unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_break (unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_break (unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_break (unsigned char *, size_t)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_encode_ctrl (uint8_t, unsigned char *, size_t)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_encode_ctrl (uint8_t, unsigned char *, size_t)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_encode_ctrl (uint8_t, unsigned char *, size_t)
-----------------------^
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
Type 0 - positive integers¶
Corresponding 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 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.
Actual Type of the integer can be checked using item types API.
An integer item is created with one of the four widths. Because integers' storage is bundled together with the handle, the width cannot be changed over its lifetime.
WARNING:
cbor_item_t * item = cbor_new_int8(); cbor_mark_negint(item); cbor_set_uint8(0);
will produce an item with the logical value of -1. There is, however, an upside to this as well: There is only one representation of zero.
Building new items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint8 (uint8_t value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint8 (uint8_t value)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint8 (uint8_t value)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint16 (uint16_t value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint16 (uint16_t value)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint16 (uint16_t value)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint32 (uint32_t value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint32 (uint32_t value)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint32 (uint32_t value)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint64 (uint64_t value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint64 (uint64_t value)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_uint64 (uint64_t value)
----------------------------^
Retrieving values¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD uint8_t cbor_get_uint8 (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 24]
_CBOR_NODISCARD uint8_t cbor_get_uint8 (const cbor_item_t *item)
------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 24]
_CBOR_NODISCARD uint8_t cbor_get_uint8 (const cbor_item_t *item)
------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD uint16_t cbor_get_uint16 (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 25]
_CBOR_NODISCARD uint16_t cbor_get_uint16 (const cbor_item_t *item)
-------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 25]
_CBOR_NODISCARD uint16_t cbor_get_uint16 (const cbor_item_t *item)
-------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD uint32_t cbor_get_uint32 (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 25]
_CBOR_NODISCARD uint32_t cbor_get_uint32 (const cbor_item_t *item)
-------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 25]
_CBOR_NODISCARD uint32_t cbor_get_uint32 (const cbor_item_t *item)
-------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD uint64_t cbor_get_uint64 (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 25]
_CBOR_NODISCARD uint64_t cbor_get_uint64 (const cbor_item_t *item)
-------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 25]
_CBOR_NODISCARD uint64_t cbor_get_uint64 (const cbor_item_t *item)
-------------------------^
Setting values¶
- 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
- 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
- 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
- void cbor_set_uint64(cbor_item_t *item, uint64_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
Dealing with width¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_int_width cbor_int_get_width (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 31]
_CBOR_NODISCARD cbor_int_width cbor_int_get_width (const cbor_item_t *item)
-------------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 31]
_CBOR_NODISCARD cbor_int_width cbor_int_get_width (const cbor_item_t *item)
-------------------------------^
Dealing with signedness¶
- void cbor_mark_uint(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
- void cbor_mark_negint(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
Creating new items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_int8 (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_int8 (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_int8 (void)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_int16 (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_int16 (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_int16 (void)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_int32 (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_int32 (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_int32 (void)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_int64 (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_int64 (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_int64 (void)
----------------------------^
Type 2 – Byte strings¶
CBOR byte strings are just (ordered) series of bytes without further interpretation (unless there is a tag). Byte string's length may or may not be known during encoding. These two kinds of byte strings can be distinguished using cbor_bytestring_is_definite() and 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 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 |
Getting metadata¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_bytestring_length (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_bytestring_length (const cbor_item_t *item)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_bytestring_length (const cbor_item_t *item)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_bytestring_is_definite (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_bytestring_is_definite (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_bytestring_is_definite (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_bytestring_is_indefinite (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_bytestring_is_indefinite (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_bytestring_is_indefinite (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_bytestring_chunk_count (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_bytestring_chunk_count (const cbor_item_t *item)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_bytestring_chunk_count (const cbor_item_t *item)
-----------------------^
Reading data¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_mutable_data cbor_bytestring_handle (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 34]
_CBOR_NODISCARD cbor_mutable_data cbor_bytestring_handle (const cbor_item_t *item)
----------------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 34]
_CBOR_NODISCARD cbor_mutable_data cbor_bytestring_handle (const cbor_item_t *item)
----------------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t ** cbor_bytestring_chunks_handle (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t ** cbor_bytestring_chunks_handle (const cbor_item_t *item)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t ** cbor_bytestring_chunks_handle (const cbor_item_t *item)
----------------------------^
Creating new items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_bytestring (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_bytestring (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_bytestring (void)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_bytestring (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_bytestring (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_bytestring (void)
----------------------------^
Building items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_build_bytestring (cbor_data handle, size_t length)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_bytestring (cbor_data handle, size_t length)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_bytestring (cbor_data handle, size_t length)
----------------------------^
Manipulating existing items¶
- void cbor_bytestring_set_handle(cbor_item_t *item, cbor_mutable_data data, size_t length)
- 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
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_bytestring_add_chunk (cbor_item_t *item, cbor_item_t *chunk)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_bytestring_add_chunk (cbor_item_t *item, cbor_item_t *chunk)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_bytestring_add_chunk (cbor_item_t *item, cbor_item_t *chunk)
--------------------^
Type 3 – UTF-8 strings¶
CBOR strings have the same structure as Type 2 – Byte strings.
Corresponding 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 |
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¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_string_length (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_string_length (const cbor_item_t *item)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_string_length (const cbor_item_t *item)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_string_is_definite (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_string_is_definite (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_string_is_definite (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_string_is_indefinite (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_string_is_indefinite (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_string_is_indefinite (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_string_chunk_count (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_string_chunk_count (const cbor_item_t *item)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_string_chunk_count (const cbor_item_t *item)
-----------------------^
Reading data¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_mutable_data cbor_string_handle (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 34]
_CBOR_NODISCARD cbor_mutable_data cbor_string_handle (const cbor_item_t *item)
----------------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 34]
_CBOR_NODISCARD cbor_mutable_data cbor_string_handle (const cbor_item_t *item)
----------------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t ** cbor_string_chunks_handle (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t ** cbor_string_chunks_handle (const cbor_item_t *item)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t ** cbor_string_chunks_handle (const cbor_item_t *item)
----------------------------^
Creating new items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_string (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_string (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_string (void)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_string (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_string (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_string (void)
----------------------------^
Building items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_build_string (const char *val)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_string (const char *val)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_string (const char *val)
----------------------------^
Manipulating existing items¶
- void cbor_string_set_handle(cbor_item_t *item, cbor_mutable_data data, size_t length)
- Set the handle to the underlying string.
WARNING:
- 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
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_string_add_chunk (cbor_item_t *item, cbor_item_t *chunk)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_string_add_chunk (cbor_item_t *item, cbor_item_t *chunk)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_string_add_chunk (cbor_item_t *item, cbor_item_t *chunk)
--------------------^
Type 4 – Arrays¶
CBOR arrays, just like byte strings and strings, can be encoded either as definite, or as indefinite. Definite arrays have a fixed size which is stored in the header, whereas indefinite arrays do not and are terminated by a special "break" byte instead.
Arrays are explicitly created or decoded as definite or indefinite and will be encoded using the corresponding wire representation, regardless of whether the actual size is known at the time of encoding.
NOTE:
Corresponding 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
Getting metadata¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_array_size (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_array_size (const cbor_item_t *item)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_array_size (const cbor_item_t *item)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_array_allocated (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_array_allocated (const cbor_item_t *item)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_array_allocated (const cbor_item_t *item)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_array_is_definite (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_array_is_definite (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_array_is_definite (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_array_is_indefinite (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_array_is_indefinite (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_array_is_indefinite (const cbor_item_t *item)
--------------------^
Reading data¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t ** cbor_array_handle (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t ** cbor_array_handle (const cbor_item_t *item)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t ** cbor_array_handle (const cbor_item_t *item)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_array_get (const cbor_item_t *item, size_t index)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_array_get (const cbor_item_t *item, size_t index)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_array_get (const cbor_item_t *item, size_t index)
----------------------------^
Creating new items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_array (size_t size)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_array (size_t size)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_array (size_t size)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_array (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_array (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_array (void)
----------------------------^
Modifying items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_array_push (cbor_item_t *array, cbor_item_t *pushee)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_array_push (cbor_item_t *array, cbor_item_t *pushee)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_array_push (cbor_item_t *array, cbor_item_t *pushee)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_array_replace (cbor_item_t *item, size_t index, cbor_item_t *value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_array_replace (cbor_item_t *item, size_t index, cbor_item_t *value)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_array_replace (cbor_item_t *item, size_t index, cbor_item_t *value)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_array_set (cbor_item_t *item, size_t index, cbor_item_t *value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_array_set (cbor_item_t *item, size_t index, cbor_item_t *value)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_array_set (cbor_item_t *item, size_t index, cbor_item_t *value)
--------------------^
Type 5 – Maps¶
CBOR maps are the plain old associative maps similar JSON objects or Python dictionaries.
Definite maps have a fixed size which is stored in the header, whereas indefinite maps do not and are terminated by a special "break" byte instead.
Map are explicitly created or decoded as definite or indefinite and will be encoded using the corresponding wire representation, regardless of whether the actual size is known at the time of encoding.
NOTE:
WARNING:
Corresponding 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 |
Examples¶
0xbf Start indefinite map (represents {1: 2})
0x01 Unsigned integer 1 (key)
0x02 Unsigned integer 2 (value)
0xff "Break" control token
0xa0 Map of size 0
Getting metadata¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_map_size (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_map_size (const cbor_item_t *item)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_map_size (const cbor_item_t *item)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD size_t cbor_map_allocated (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 23]
_CBOR_NODISCARD size_t cbor_map_allocated (const cbor_item_t *item)
-----------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 23]
_CBOR_NODISCARD size_t cbor_map_allocated (const cbor_item_t *item)
-----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_map_is_definite (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_map_is_definite (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_map_is_definite (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_map_is_indefinite (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_map_is_indefinite (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_map_is_indefinite (const cbor_item_t *item)
--------------------^
Reading data¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD struct cbor_pair * cbor_map_handle (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: struct [error at 22]
_CBOR_NODISCARD struct cbor_pair * cbor_map_handle (const cbor_item_t *item)
----------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: struct [error at 22]
_CBOR_NODISCARD struct cbor_pair * cbor_map_handle (const cbor_item_t *item)
----------------------^
Creating new items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_map (size_t size)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_map (size_t size)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_definite_map (size_t size)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_map (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_map (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_indefinite_map (void)
----------------------------^
Modifying items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_map_add (cbor_item_t *item, struct cbor_pair pair)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_map_add (cbor_item_t *item, struct cbor_pair pair)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_map_add (cbor_item_t *item, struct cbor_pair pair)
--------------------^
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 before inventing new ones.
Corresponding 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 |
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_tag (uint64_t value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_tag (uint64_t value)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_tag (uint64_t value)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_tag_item (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_tag_item (const cbor_item_t *item)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_tag_item (const cbor_item_t *item)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD uint64_t cbor_tag_value (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 25]
_CBOR_NODISCARD uint64_t cbor_tag_value (const cbor_item_t *item)
-------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 25]
_CBOR_NODISCARD uint64_t cbor_tag_value (const cbor_item_t *item)
-------------------------^
- void cbor_tag_set_item(cbor_item_t *item, cbor_item_t *tagged_item)
- Set the tagged item.
- param item[borrow]
- A tag
- param tagged_item[incref]
- The item to tag
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).
- enum cbor_float_width
- Possible widths of CBOR_TYPE_FLOAT_CTRL items.
Values:
- enumerator CBOR_FLOAT_0
- Internal use - ctrl and special values.
- enumerator CBOR_FLOAT_16
- Half float.
- enumerator CBOR_FLOAT_32
- Single float.
- enumerator CBOR_FLOAT_64
- Double.
Corresponding cbor_type | CBOR_TYPE_FLOAT_CTRL |
Number of allocations | One per lifetime |
Storage requirements | sizeof(cbor_item_t) + 1/4/8 |
Getting metadata¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_float_ctrl_is_ctrl (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_float_ctrl_is_ctrl (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_float_ctrl_is_ctrl (const cbor_item_t *item)
--------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_float_width cbor_float_get_width (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 33]
_CBOR_NODISCARD cbor_float_width cbor_float_get_width (const cbor_item_t *item)
---------------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 33]
_CBOR_NODISCARD cbor_float_width cbor_float_get_width (const cbor_item_t *item)
---------------------------------^
Reading data¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD float cbor_float_get_float2 (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: float [error at 21]
_CBOR_NODISCARD float cbor_float_get_float2 (const cbor_item_t *item)
---------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: float [error at 21]
_CBOR_NODISCARD float cbor_float_get_float2 (const cbor_item_t *item)
---------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD float cbor_float_get_float4 (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: float [error at 21]
_CBOR_NODISCARD float cbor_float_get_float4 (const cbor_item_t *item)
---------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: float [error at 21]
_CBOR_NODISCARD float cbor_float_get_float4 (const cbor_item_t *item)
---------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD double cbor_float_get_float8 (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: double [error at 22]
_CBOR_NODISCARD double cbor_float_get_float8 (const cbor_item_t *item)
----------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: double [error at 22]
_CBOR_NODISCARD double cbor_float_get_float8 (const cbor_item_t *item)
----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD double cbor_float_get_float (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: double [error at 22]
_CBOR_NODISCARD double cbor_float_get_float (const cbor_item_t *item)
----------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: double [error at 22]
_CBOR_NODISCARD double cbor_float_get_float (const cbor_item_t *item)
----------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD uint8_t cbor_ctrl_value (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 24]
_CBOR_NODISCARD uint8_t cbor_ctrl_value (const cbor_item_t *item)
------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 24]
_CBOR_NODISCARD uint8_t cbor_ctrl_value (const cbor_item_t *item)
------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD bool cbor_get_bool (const cbor_item_t *item)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_get_bool (const cbor_item_t *item)
--------------------^
If declarator-id:
Invalid C++ declaration: Expected identifier in nested name, got keyword: bool [error at 20]
_CBOR_NODISCARD bool cbor_get_bool (const cbor_item_t *item)
--------------------^
Creating new items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_ctrl (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_ctrl (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_ctrl (void)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_float2 (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_float2 (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_float2 (void)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_float4 (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_float4 (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_float4 (void)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_float8 (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_float8 (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_float8 (void)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_null (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_null (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_null (void)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_new_undef (void)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_undef (void)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_new_undef (void)
----------------------------^
Building items¶
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_build_bool (bool value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_bool (bool value)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_bool (bool value)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_build_ctrl (uint8_t value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_ctrl (uint8_t value)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_ctrl (uint8_t value)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_build_float2 (float value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_float2 (float value)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_float2 (float value)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_build_float4 (float value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_float4 (float value)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_float4 (float value)
----------------------------^
WARNING:
Error in declarator or parameters-and-qualifiers
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 16]
_CBOR_NODISCARD cbor_item_t * cbor_build_float8 (double value)
----------------^ If the function has a return type:
Error in declarator or parameters-and-qualifiers
If pointer to member declarator:
Invalid C++ declaration: Expected '::' in pointer to member (function). [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_float8 (double value)
----------------------------^
If declarator-id:
Invalid C++ declaration: Expecting "(" in parameters-and-qualifiers. [error at 28]
_CBOR_NODISCARD cbor_item_t * cbor_build_float8 (double value)
----------------------------^
Manipulating existing items¶
- void cbor_set_ctrl(cbor_item_t *item, uint8_t value)
- Assign a control value.
WARNING:
- param item[borrow]
- A ctrl item
- param value
- The simple value to assign. Please consult the standard for allowed values
- void cbor_set_bool(cbor_item_t *item, bool value)
- Assign a boolean value to a boolean ctrl item.
- param item[borrow]
- A ctrl item
- param value
- The simple value to assign.
- void cbor_set_float2(cbor_item_t *item, float value)
- Assigns a float value.
- param item[borrow]
- A half precision float
- param value
- The value to assign
- void cbor_set_float4(cbor_item_t *item, float value)
- Assigns a float value.
- param item[borrow]
- A single precision float
- param value
- The value to assign
- void cbor_set_float8(cbor_item_t *item, double value)
- Assigns a float value.
- param item[borrow]
- A double precision float
- param value
- The value to assign
Half floats¶
CBOR supports two bytes wide ("half-precision") 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. Encoding will be performed by cbor_encode_half(), which will handle any values that cannot be represented as a half-float.
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 documentation for detailed information on how to specify particular subset of tests.
Testing for memory leaks¶
Every release is tested for memory correctness. You can run these tests by passing the -T memcheck flag to ctest. [1]
- [1]
- 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. Platform-independent code should be fully covered by the test suite. Simply run
make coverage
or alternatively run lcov by hand using
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.
RFC conformance¶
libcbor is, generally speaking, very faithful implementation of RFC 7049. There are, however, some limitations imposed by technical constraints.
Bytestring length¶
There is no explicit limitation of indefinite length byte strings. [1] libcbor will not handle byte strings with more chunks than the maximum value of 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.
"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 Half floats.
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 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
- json-c
- Gnome's JsonGlib
and also borrowing from
- msgpack-c
- Google Protocol Buffers.
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 realloc() sparsely and predictably [1]
- provide strong guarantees about its usage (to prevent latency spikes)
- provide APIs to avoid realloc() altogether
- •
- allow proper handling of (streamed) data bigger than available memory
- [1]
- Reasonable handling of DSTs requires reallocation if the API is to remain sane.
Coding style¶
This code loosely follows the Linux kernel coding style. 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 handle,
- the associated metadata,
- and the actual data
- type cbor_item_t
- Represents the item. Used as an opaque type
- cbor_type type
- Type discriminator
- size_t refcount
- Reference counter. Used by cbor_decref(), cbor_incref()
- union cbor_item_metadata metadata
- Union discriminated by type. Contains type-specific metadata
- unsigned char *data
- Contains pointer to the actual data. Small, fixed size items (Types 0
& 1 – Positive and negative integers, Type 6 –
Semantic tags, Type 7 – Floats & control tokens) are
allocated as a single memory block.
Consider the following snippet
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 (Type 2 – Byte strings, Type 3 – UTF-8 strings, Type 4 – Arrays, Type 5 – Maps) may store handle and data in separate locations. This enables creating large items (e.g byte strings) without 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:
- struct _cbor_int_metadata int_metadata
- Used both by both Types 0 & 1 – Positive and negative integers
Decoding¶
As outlined in API, there decoding is based on the streaming decoder Essentially, the decoder is a custom set of callbacks for the streaming decoder.
Changelog¶
Template: - [Fix issue X in feature Y](https://github.com/PJK/libcbor/pull/XXX) (by [YYY](https://github.com/YYY))
Next¶
0.10.1 (2022-12-30)¶
- •
- [Fix a regression in cbor_serialize_alloc that caused serialization of zero-length strings and bytestrings or byte/strings with zero-length chunks to fail](https://github.com/PJK/libcbor/pull/260) (discovered by [martelletto](https://github.com/martelletto))
0.10.0 (2022-12-29)¶
- Make the buffer_size optional in cbor_serialize_alloc [[#205]](https://github.com/PJK/libcbor/pull/205) (by [hughsie](https://github.com/hughsie))
- BREAKING: Improved half-float encoding for denormalized numbers. [[#208]](https://github.com/PJK/libcbor/pull/208) (by [ranvis](https://github.com/ranvis)) - Denormalized half-floats will now preserve data in the mantissa - Note: Half-float NaNs still lose data (https://github.com/PJK/libcbor/issues/215)
- BUILD BREAKING: Minimum CMake version is 3.0 [[#201]](https://github.com/PJK/libcbor/pull/201) (by [thewtex@](https://github.com/thewtex)) - See https://repology.org/project/cmake/versions for support; the vast majority of users should not be affected.
- Fix a potential memory leak when the allocator fails during array or map decoding [[#224]](https://github.com/PJK/libcbor/pull/224) (by [James-ZHANG](https://github.com/James-ZHANG))
- [Fix a memory leak when the allocator fails when adding chunks to indefinite bytestrings.](https://github.com/PJK/libcbor/pull/242) ([discovered](https://github.com/PJK/libcbor/pull/228) by [James-ZHANG](https://github.com/James-ZHANG))
- [Fix a memory leak when the allocator fails when adding chunks to indefinite strings](https://github.com/PJK/libcbor/pull/246)
- Potentially BUILD BREAKING: [Add nodiscard attributes to most functions](https://github.com/PJK/libcbor/pull/248) - Warning: This may cause new build warnings and (in rare cases, depending on your configuration) errors
- BREAKING: [Fix cbor_copy leaking memory and creating invalid items when the allocator fails](https://github.com/PJK/libcbor/pull/249). - Previously, the failures were not handled in the interface. Now, cbor_copy may return NULL upon failure; clients should check the return value
- [Fix cbor_build_tag illegal memory behavior when the allocator fails](https://github.com/PJK/libcbor/pull/249)
- [Add a new cbor_serialized_size API](https://github.com/PJK/libcbor/pull/250)
- [Reworked cbor_serialize_alloc to allocate the exact amount of memory necessary upfront](https://github.com/PJK/libcbor/pull/251) - This should significantly speed up cbor_serialize_alloc for large items by avoiding multiple reallocation iterations - Clients should not use the return value of cbor_serialize_alloc. It may be removed in the future.
- BUILD BREAKING: [Deprecate CBOR_CUSTOM_ALLOC](https://github.com/PJK/libcbor/pull/237) - cbor_set_allocs will always be enabled from now on - Note: The flag will be kept as a no-op triggering a warning when used for one version and then removed completely
0.9.0 (2021-11-14)¶
- Improved pkg-config paths handling [[#164]](https://github.com/PJK/libcbor/pull/164) (by [jtojnar@](https://github.com/jtojnar))
- Use explicit math.h linkage [[#170]](https://github.com/PJK/libcbor/pull/170)
- BREAKING: Fixed handling of items that exceed the host size_t range [[#186]](https://github.com/PJK/libcbor/pull/186hg)
- Callbacks for bytestrings, strings, arrays, and maps use uint64_t instead of size_t to allow handling of large items that exceed size_t even if size_t < uint64_t
- cbor_decode explicitly checks size to avoid overflows (previously broken, potentially resulting in erroneous decoding on affected systems)
- The change should be a noop for 64b systems
- •
- Added a [Bazel](https://bazel.build/) build example [[#196]](https://github.com/PJK/libcbor/pull/196) (by [andyjgf@](https://github.com/andyjgf))
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))
- 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)¶
- •
- •
- 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!
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 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 (testing)
- Python and pip (Sphinx platform)
- Doxygen
- Sphinx (documentation)
- There are some Ruby scripts in misc
- Valgrind (memory correctness & profiling)
- GCOV/LCOV (test coverage)
- clang-format
Installing sphinx¶
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¶
cd doc make livehtml
Set up git hooks¶
A catch-all git hook that runs clang-format and automatically refreshes the GH pages contents located in docs can be symlinked:
ln -sf $(pwd)/misc/hooks/pre-commit .git/hooks
Testing and code coverage¶
Please refer to Tests
AUTHOR¶
Pavel Kalvoda
COPYRIGHT¶
2023 - 2020, Pavel Kalvoda
January 16, 2023 | 0.10 |