LIBCBOR(3)

πŸ“¦
version 0.13.0 Β· 3 versions
, in fedora 44

πŸ¦‹ libcbor β€” the LIBCBOR(3) manual page, but make it make sense πŸ¦‹

okay so bestie, pull up a chair, grab a strawberry πŸ“, because we're about to get into libcbor and honestly? it's giving iconic.


πŸ“› NAME πŸ“›

libcbor β€” this is the libcbor documentation. yeah. that's the name. slay.

so like, this is the docs for version 0.13.0, updated on Jan 16, 2026 ✨

Git repo, if you wanna go lurk: <https://github.com/PJK/libcbor>


🌈 OVERVIEW 🌈

libcbor is a C library for parsing and generating CBOR <https://www.rfc-editor.org/info/std94> β€” and CBOR is the general-purpose, schema-less, binary data format. no schema, no problem, we just vibe πŸ’…

✨ Main features ✨

  • Complete IETF RFC 8949 (STD 94) <https://www.rfc-editor.org/info/std94> 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 IETF standard conformance. [2] With the exception of custom memory allocators (see Memory management and reference counting).

πŸ“š CONTENTS πŸ“š

πŸŽ€ Getting started πŸŽ€

pre-built Linux packages are literally right there in most mainstream distributions, no cap:

Ubuntu, Debian, etc.:

apt-get install libcbor-dev

Fedora, openSUSE, etc.:

yum install libcbor-devel

OS X girlies can use Homebrew <http://brew.sh/>:

brew install libcbor

For other platforms, you will need to compile it from source. yeah. from scratch. it's fine. you got this. πŸ’ͺ


πŸ› οΈ Building & installing libcbor πŸ› οΈ

Prerequisites, aka the vibe check before you start:

  • C99 compiler
  • CMake <http://cmake.org/> 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. here's the full rundown β€” libcbor compile-time directives plus several important generic flags:

  • CMAKE_C_COMPILER β€” C compiler to use. Default: cc. Possible values: gcc, clang, clang-3.5, ...
  • CMAKE_INSTALL_PREFIX β€” Installation prefix. Default: System-dependent. Possible values: /usr/local/lib, ...
  • CMAKE_INTERPROCEDURAL_OPTIMIZATION β€” Enable LTO (if supported). Default: System-dependent. Possible values: ON, OFF
  • BUILD_SHARED_LIBS β€” Build as a shared library. Default: OFF. Possible values: ON, OFF
  • HUGE_FUZZ β€” Fuzz test with 8GB of data. Default: OFF. Possible values: ON, OFF
  • SANE_MALLOC β€” Assume malloc will refuse unreasonable allocations. Default: OFF. Possible values: ON, OFF
  • COVERAGE β€” Generate test coverage instrumentation. Default: OFF. Possible values: ON, OFF
  • WITH_TESTS β€” Build unit tests (see Development). Default: OFF. Possible values: ON, OFF

and the following configuration options will also be defined as macros [1] in <cbor/common.h> and can therefore be used in client code β€” so like, you can actually touch them from your own code, bestie:

  • CBOR_PRETTY_PRINTER β€” Include a pretty-printing routine. Default: ON. Possible values: ON, OFF
  • CBOR_BUFFER_GROWTH β€” Factor for buffer growth & shrinking. Default: 2. Possible values: Decimals > 1
[1] ON & OFF will be translated to 1 and 0 using cmakedefine <https://cmake.org/cmake/help/v3.2/command/configure_file.html?highlight=cmakedefine>. If you want to pass other custom configuration options, please refer to <http://www.cmake.org/Wiki/CMake_Useful_Variables>.
Note: When CMAKE_INTERPROCEDURAL_OPTIMIZATION is enabled, the generated static library (libcbor.a) should be used with an LTO-enabled linker downstream. On LLVM toolchains without bitcode embedding (-fembed-bitcode), the archive will contain LLVM IR only and linking without LTO will not work <https://github.com/PJK/libcbor/issues/372>. 😭 do not skip this one.
Warning: CBOR_CUSTOM_ALLOC has been removed <https://github.com/PJK/libcbor/pull/237>. Custom allocators (historically a controlled by a build flag) are always enabled. gone but never forgotten πŸ•ŠοΈ

🧱 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 <https://github.com/PJK/libcbor/issues>. they wanna hear about it fr fr.

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. zero. none. it just works ✨

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

and that's just the beginning bestie, there is so much more coming πŸ‘€ keep scrolling πŸ’œ

Ok so β€” libcbor also comes with pkg-config support πŸŽ€ (yes, the fancy freedesktop one: https://wiki.freedesktop.org/www/Software/pkg-config/). And if you installed libcbor with a custom prefix and your compiler is being dramatic about where the headers live, bestie, pkg-config will just resolve the headers and objects for you πŸ’…

cc $(pkg-config --cflags libcbor) hello_cbor.c $(pkg-config --libs libcbor) -o hello_cbor

✨ A note on linkage ✨

Real talk: libcbor is primarily intended to be linked statically. That's the plan, that's the design, that's the whole moment πŸ‘‘

The shared library versioning scheme generally follows SemVer (https://semver.org/), but β€” and this is the messy part β€” it's 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) (see https://developer.apple.com/library/archive/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html):

  • 0.Y for the 0.Y.Z branch. Patches are backwards compatible, but 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 releases of the major version are backwards compatible 🫢
⚠️ Warning: releases up to and including v0.6.0 may export a misleading .so/.dylib version number (https://github.com/PJK/libcbor/issues/52). Not us being shady, just history being history.

πŸ› οΈ 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'll 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 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, bestie 🫠 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)

🌸 TUTORIAL 🌸

libcbor is a C library to encode, decode, and manipulate CBOR data. It is to CBOR to what cJSON (https://github.com/DaveGamble/cJSON) is to JSON β€” like, that's the energy ✨

We assume you are already familiar with the CBOR standard. If not… we recommend cbor.io (http://cbor.io/) πŸ’–

🌈 Where to start 🌈

  • Skim through the Crash course section below πŸ‘‡
  • Examples of how to read, write, manipulate, and translate data to and from JSON using libcbor are in the examples directory (https://github.com/PJK/libcbor/tree/master/examples) πŸ“
  • The API documentation is a complete reference of libcbor πŸ“š

πŸ¦‹ Crash course πŸ¦‹

πŸ’œ CBOR data objects are cbor_item_t πŸ’œ

void item_examples() {
  // A cbor_item_t can contain any CBOR data type
  cbor_item_t* float_item = cbor_build_float4(3.14f);
  cbor_item_t* string_item = cbor_build_string("Hello World!");
  cbor_item_t* array_item = cbor_new_indefinite_array();

  // They can be inspected
  assert(cbor_is_float(float_item));
  assert(cbor_typeof(string_item) == CBOR_TYPE_STRING);
  assert(cbor_array_is_indefinite(array_item));
  assert(cbor_array_size(array_item) == 0);

  // The data can be accessed
  assert(cbor_float_get_float4(float_item) == 3.14f);
  assert(memcmp(cbor_string_handle(string_item), "Hello World!",
                cbor_string_length(string_item)) == 0);

  // And they can be modified
  assert(cbor_array_push(array_item, float_item));
  assert(cbor_array_push(array_item, string_item));
  assert(cbor_array_size(array_item) == 2);

  // At the end of their lifetime, items must be freed
  cbor_decref(&float_item);
  cbor_decref(&string_item);
  cbor_decref(&array_item);
}

🧚 Objects can be serialized and deserialized 🧚

void encode_decode() {
  cbor_item_t* item = cbor_build_uint8(42);

  // Serialize the item to a buffer (it will be allocated by libcbor)
  unsigned char* buffer;
  size_t buffer_size;
  cbor_serialize_alloc(item, &buffer, &buffer_size);
  assert(buffer_size == 2);
  assert(buffer[0] == 0x18);  // Encoding byte for uint8
  assert(buffer[1] == 42);    // The value itself

  // And deserialize bytes back to an item
  struct cbor_load_result result;
  cbor_item_t* decoded_item = cbor_load(buffer, buffer_size, &result);
  assert(result.error.code == CBOR_ERR_NONE);
  assert(cbor_isa_uint(decoded_item));
  assert(cbor_get_uint8(decoded_item) == 42);

  // Free the allocated buffer and items
  free(buffer);
  cbor_decref(&decoded_item);
  cbor_decref(&item);
}

πŸ’… Reference counting πŸ’…

void reference_counting() {
  // cbor_item_t is a reference counted pointer under the hood
  cbor_item_t* item = cbor_build_uint8(42);

  // Reference count starts at 1
  assert(cbor_refcount(item) == 1);

  // Most operations have reference semantics
  cbor_item_t* array_item = cbor_new_definite_array(1);
  assert(cbor_array_push(array_item, item));
  assert(cbor_refcount(item) == 2);  // item and array_item reference it
  cbor_item_t* first_array_element = cbor_array_get(array_item, 0);
  assert(first_array_element == item);  // same item under the hood
  assert(cbor_refcount(item) ==
         3);  // and now first_array_element also points to it

  // To release the reference, use cbor_decref
  cbor_decref(&first_array_element);

  // When reference count reaches 0, the item is freed
  assert(cbor_refcount(array_item) == 1);
  cbor_decref(&array_item);
  assert(array_item == NULL);
  assert(cbor_refcount(item) == 1);

  // Be careful, loops leak memory!

  // Deep copy copies the whole item tree
  cbor_item_t* item_copy = cbor_copy(item);
  assert(cbor_refcount(item) == 1);
  assert(cbor_refcount(item_copy) == 1);
  assert(item_copy != item);
  cbor_decref(&item);
  cbor_decref(&item_copy);
}

πŸ”₯ Moving intermediate values πŸ”₯

void moving_values() {
  {
    // Move the "42" into an array.
    cbor_item_t* array_item = cbor_new_definite_array(1);
    // The line below leaks memory!
    assert(cbor_array_push(array_item, cbor_build_uint8(42)));
    cbor_item_t* first_array_element = cbor_array_get(array_item, 0);
    assert(cbor_refcount(first_array_element) == 3);  // Should be 2!
    cbor_decref(&first_array_element);
    cbor_decref(&array_item);
    assert(cbor_refcount(first_array_element) == 1);  // Shouldn't exist!
    // Clean up
    cbor_decref(&first_array_element);
  }

  {
    // A correct way to move values is to decref them in the caller scope.
    cbor_item_t* array_item = cbor_new_definite_array(1);
    cbor_item_t* item = cbor_build_uint8(42);
    assert(cbor_array_push(array_item, item));
    assert(cbor_refcount(item) == 2);
    // "Give up" the item
    cbor_decref(&item);
    cbor_decref(&array_item);
    // item is a dangling pointer at this point
  }

  {
    // cbor_move avoids the need to decref and the dangling pointer
    cbor_item_t* array_item = cbor_new_definite_array(1);
    assert(cbor_array_push(array_item, cbor_move(cbor_build_uint8(42))));
    cbor_item_t* first_array_element = cbor_array_get(array_item, 0);
    assert(cbor_refcount(first_array_element) == 2);
    cbor_decref(&first_array_element);
    cbor_decref(&array_item);
  }
}

πŸ‘‘ Ownership πŸ‘‘

// Refcount can be managed in conjunction with ownership
static cbor_item_t* global_item = NULL;

// This function takes shared ownership of the item
void borrow_item(cbor_item_t* item) {
  global_item = item;
  // Mark the extra reference
  cbor_incref(item);
}

void return_item() {
  cbor_decref(&global_item);
  global_item = NULL;
}

void reference_ownership() {
  cbor_item_t* item = cbor_build_uint8(42);

  // Lend the item
  borrow_item(item);
  assert(cbor_refcount(item) == 2);
  cbor_decref(&item);

  // Release the shared ownership. return_item will deallocate the item.
  return_item();
}

🌊 Streaming IO 🌊

See https://github.com/PJK/libcbor/blob/master/examples/streaming_array.c and https://github.com/PJK/libcbor/blob/master/examples/streaming_parser.c ✨


πŸŽ€ 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 link items 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 the CBOR standard (https://tools.ietf.org/html/rfc7049). This part of the documentation provides a short overview of the CBOR constructs, as well as a general introduction to the libcbor API. The 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, a 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 the aforementioned details and only be presented complete data structures ✨

libcbor provides

  • stateless encoders and decoders
  • encoding and decoding drivers, routines that coordinate encoding and decoding of complex structures
  • data structures to represent and transform CBOR structures
  • routines for building and manipulating these structures
  • utilities for inspection and debugging

✨ Types of items ✨

okay so every single cbor_item_t has a cbor_type attached to it, and those constants map one-to-one onto the types the CBOR standard defines πŸ’–

enum cbor_type

Specifies the Major type of cbor_item_t.

Values:

  • CBOR_TYPE_UINT β€” 0 β€” positive integers πŸ”’
  • CBOR_TYPE_NEGINT β€” 1 β€” negative integers
  • CBOR_TYPE_BYTESTRING β€” 2 β€” byte strings 🧊
  • CBOR_TYPE_STRING β€” 3 β€” strings
  • CBOR_TYPE_ARRAY β€” 4 β€” arrays
  • CBOR_TYPE_MAP β€” 5 β€” maps πŸ—ΊοΈ
  • CBOR_TYPE_TAG β€” 6 β€” tags 🏷️
  • CBOR_TYPE_FLOAT_CTRL β€” 7 β€” decimals and special values (true, false, nil, ...) πŸŽ€

So to find out what type an item is, bestie, you literally just use this:

cbor_type cbor_typeof(const cbor_item_t *item)
  • item β€” the item
  • returns the type ✨
heads up! 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 β€” and this is lowkey the easier route β€” there are functions to query each particular type πŸ’…

Warning: Passing an invalid cbor_item_t reference to any of these functions results in undefined behavior. not me finding that out the hard way 😭
bool cbor_isa_uint(const cbor_item_t *item)

Does the item have the appropriate major type?

  • item β€” the item
  • returns: Is the item an CBOR_TYPE_UINT? πŸ’–
bool cbor_isa_negint(const cbor_item_t *item)

Does the item have the appropriate major type?

  • item β€” the item
  • returns: Is the item a CBOR_TYPE_NEGINT?
bool cbor_isa_bytestring(const cbor_item_t *item)

Does the item have the appropriate major type?

  • item β€” the item
  • returns: Is the item a CBOR_TYPE_BYTESTRING? 🧊
bool cbor_isa_string(const cbor_item_t *item)

Does the item have the appropriate major type?

  • item β€” the item
  • returns: Is the item a CBOR_TYPE_STRING? 🌸
bool cbor_isa_array(const cbor_item_t *item)

Does the item have the appropriate major type?

  • item β€” the item
  • returns: Is the item an CBOR_TYPE_ARRAY?
bool cbor_isa_map(const cbor_item_t *item)

Does the item have the appropriate major type?

  • item β€” the item
  • returns: Is the item a CBOR_TYPE_MAP? πŸ—ΊοΈ
bool cbor_isa_tag(const cbor_item_t *item)

Does the item have the appropriate major type?

  • item β€” the item
  • returns: Is the item a CBOR_TYPE_TAG? 🏷️
bool cbor_isa_float_ctrl(const cbor_item_t *item)

Does the item have the appropriate major type?

  • item β€” the item
  • returns: Is the item a CBOR_TYPE_FLOAT_CTRL? πŸŽ€

🧠 Logical queries

These ones give you info about the item type from a higher-level perspective β€” it's giving ✨big picture✨ energy πŸ¦‹

bool cbor_is_int(const cbor_item_t *item)

Is the item an integer, either positive or negative?

  • item β€” the item
  • returns: Is the item an integer, either positive or negative? πŸ”’
bool cbor_is_float(const cbor_item_t *item)

Is the item an a floating point number?

  • item β€” the item
  • returns: Is the item a floating point number? πŸ“
bool cbor_is_bool(const cbor_item_t *item)

Is the item an a boolean?

  • item β€” the item
  • returns: Is the item a boolean? βœ…
bool cbor_is_null(const cbor_item_t *item)

Does this item represent null.

Warning: This is in no way related to the value of the pointer. Passing a null pointer will most likely result in a crash πŸ’€ fr fr, don't do it.
  • item β€” the item
  • returns: Is the item (CBOR logical) null?
bool cbor_is_undef(const cbor_item_t *item)

Does this item represent undefined.

Warning: Care must be taken to distinguish nulls and undefined values in C. πŸ™ˆ they are not the same thing, bestie.
  • item β€” the item
  • returns: Is the item (CBOR logical) undefined?

πŸ’Ύ Memory management and reference counting

So because of the nature of its domain, libcbor will need to work with heap memory. The stateless decoder and encoder doesn't allocate any memory at all πŸ‘‘

If you have specific requirements, you should consider rolling your own driver for the stateless API.


πŸŽ€ Using custom allocator

libcbor gives you 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). obsessed, honestly πŸ’…

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.

By default, libcbor will use the standard library malloc, realloc, and free.

Note: realloc implementation must correctly support NULL reallocation (see e.g. <http://en.cppreference.com/w/c/memory/realloc>)
Warning: This function modifies the global state and should therefore be used accordingly. Changing the memory handlers while allocated items exist will result in a free/malloc mismatch. This function is not thread safe with respect to both itself and all the other libcbor functions that work with the heap. 😭 yeah. global state. iconic but dangerous.
  • custom_malloc β€” malloc implementation
  • custom_realloc β€” realloc implementation
  • 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 item's reference count by one.

Constant complexity; items referring to this one or items being referred to are not updated.

This function can be used to extend reference counting to client code.

  • item β€” Reference to an item
  • returns: The input item 🫢
void cbor_decref(cbor_item_t **item)

Decreases the item's reference count by one, deallocating the item if needed.

In case the item is deallocated, the reference count of all items this item references will also be cbor_decref'ed recursively.

  • item β€” Reference to an item. Will be set to NULL if deallocated
void cbor_intermediate_decref(cbor_item_t *item)

Decreases the item's reference count by one, deallocating the item if needed.

Convenience wrapper for cbor_decref when its set-to-null behavior is not needed.

  • item β€” Reference to an item
size_t cbor_refcount(const cbor_item_t *item)

Get the item's reference count.

Todo: Add some inline examples for reference counting

Warning: This does not account for transitive references.
  • item β€” the item
  • returns: the reference count
cbor_item_t *cbor_move(cbor_item_t *item)

Provides CPP-like move construct.

Decreases the reference count by one, but does not deallocate the item even if its refcount reaches zero. This is useful for passing intermediate values to functions that increase reference count. Should only be used with functions that incref their arguments.

Warning: If the item is moved without correctly increasing the reference count afterwards, the memory will be leaked. 😭 leak era. we don't want that.
  • item β€” Reference to an item
  • returns: the item with reference count decreased by one
cbor_item_t *cbor_copy(cbor_item_t *item)

Take a deep copy of an item. πŸ¦‹

...and bestie, the way this copy works? chef's kiss πŸ’… All items this item points to β€” array and map members, string chunks, tagged items β€” get copied recursively using cbor_copy. The new item doesn't alias or point to anything from the original item, and all the reference counts in the new structure are set to one. Fresh start, no shared trauma, we love to see it. 🫢

  • item β€” the item to copy
  • returns a reference to the new item, whose reference count is initialized to one
  • returns NULL if memory allocation fails 😭

cbor_item_t *cbor_copy_definite(cbor_item_t *item)

Copy the item with all items converted to definite length equivalents. Same deep copy semantics as cbor_copy above, no cap.

  • item β€” the item to copy
  • returns a reference to the new item, whose reference count is initialized to one
  • returns NULL if memory allocation fails πŸ’€

πŸ”“ Decoding πŸ”“

Okay so this diagram? It's giving ✨architecture✨. It shows how all the different parts of libcbor relate to each other from the decoding standpoint. Study it, bestie, it's the map:

          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚                                                                                              β”‚
          β”‚                                      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)

So like, this section is dealing with the API labeled as the "Default driver" in that diagram. Translation: these are the routines that decode complete libcbor data items for you. πŸŽ€

cbor_item_t *cbor_load(cbor_data source, size_t source_size, struct cbor_load_result *result)

Loads a data item from a buffer. Yes queen, just hand it a buffer. πŸ“¦

  • source β€” the buffer
  • source_size β€” (the size of it, bestie)
  • result β€” [out] result indicator. CBOR_ERR_NONE on success ✨
  • returns the decoded CBOR item, whose reference count is initialized to one
  • returns NULL on failure β€” and in that case, result contains the location and description of the error. It's telling on itself. πŸ’€

🧰 Associated data structures 🧰

enum cbor_error_code

Possible decoding errors. The full roster:

  • CBOR_ERR_NONE
  • CBOR_ERR_NOTENOUGHDATA
  • CBOR_ERR_NODATA
  • CBOR_ERR_MALFORMATED
  • CBOR_ERR_MEMERROR β€” memory error, item allocation failed. Is it too big for your allocator? πŸ€” asking for a friend
  • CBOR_ERR_SYNTAXERROR β€” the stack parsing algorithm failed. Not me, not my stack, it was already like that. πŸ™ˆ

struct cbor_load_result

High-level decoding result. Its public members:

  • struct cbor_error error β€” the error indicator
  • size_t read β€” number of bytes read

struct cbor_error

High-level decoding error. Its public members:

  • size_t position β€” approximate position
  • cbor_error_code code β€” the description

πŸ”₯ Encoding πŸ”₯

The easiest way to encode data items is using the cbor_serialize() or cbor_serialize_alloc() functions. Slay, no stress:

size_t cbor_serialize(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)

Serialize the given item.

  • item β€” a data item
  • buffer β€” buffer to serialize to
  • buffer_size β€” size of the buffer
  • returns the length of the result, or 0 on failure

size_t cbor_serialize_alloc(const cbor_item_t *item, unsigned char **buffer, size_t *buffer_size)

Serialize the given item, allocating buffers as needed. So convenient of it. πŸ’–

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 just ignore the return value, bestie, it's not the moment.

⚠️ Warning: it is the caller's responsibility to free the buffer using an appropriate free implementation. That's you. Yes, you. No takebacks. 😭
  • item β€” a data item
  • buffer β€” [out] buffer containing the result
  • buffer_size β€” [out] size of the buffer, or 0 on memory allocation failure
  • returns the length of the result in bytes
  • returns 0 on memory allocation failure, in which case buffer is NULL

To figure out how many bytes you need to serialize an item, use cbor_serialized_size():

size_t cbor_serialized_size(const cbor_item_t *item)

Compute the length (in bytes) of the item when serialized using cbor_serialize. Time complexity is proportional to the number of nested items, so keep that in mind before you go nesting like it's 2014 tumblr. πŸ¦‹

  • item β€” a data item
  • returns the length (>= 1) of the item when serialized. Returns 0 if the length overflows size_t. πŸ’€

πŸŽ€ Type-specific serializers πŸŽ€

In case you already know the type of the item you want to serialize beforehand, you can use one of the type-specific serializers. iykyk. ✨

🚨 Note: unless compiled in debug mode, these do not verify the type. Passing an incorrect item will result in undefined behavior. Not a cute look, bestie, be careful.

size_t cbor_serialize_uint(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)

Serialize a uint.

  • item β€” a uint
  • buffer β€” [out] buffer to serialize to
  • buffer_size β€” size of the buffer
  • returns the length of the result
  • returns 0 if the buffer_size doesn't fit the result

size_t cbor_serialize_negint(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)

Serialize a negint.

  • item β€” a negint
  • buffer β€” [out] buffer to serialize to
  • buffer_size β€” size of the buffer
  • returns the length of the result
  • returns 0 if the buffer_size doesn't fit the result

size_t cbor_serialize_bytestring(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)

Serialize a bytestring.

  • item β€” a bytestring
  • buffer β€” [out] buffer to serialize to
  • buffer_size β€” size of the buffer
  • returns the length of the result
  • returns 0 if the buffer_size doesn't fit the result. Heads up: the buffer may still be modified. πŸ™ˆ

size_t cbor_serialize_string(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)

Serialize a string.

  • item β€” a string
  • buffer β€” [out] the buffer you're serializing into, bestie
  • buffer_size β€” how big that buffer is
  • Returns β€” the length of the result πŸ’«
  • Returns β€” 0 when the buffer_size doesn't fit the result. And heads up: the buffer may still be modified πŸ™ˆ

✨ cbor_serialize_array()

size_t cbor_serialize_array(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)

Serializes an array.

  • item β€” an array
  • buffer β€” [out] buffer to serialize to
  • buffer_size β€” size of the buffer
  • Returns β€” length of the result
  • Returns β€” 0 if the buffer_size doesn't fit the result. The buffer may still be modified

✨ cbor_serialize_map()

size_t cbor_serialize_map(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)

Serializes a map.

  • item β€” a map
  • buffer β€” [out] buffer to serialize to
  • buffer_size β€” size of the buffer
  • Returns β€” length of the result
  • Returns β€” 0 if the buffer_size doesn't fit the result. The buffer may still be modified

✨ cbor_serialize_tag()

size_t cbor_serialize_tag(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)

Serializes a tag.

  • item β€” a tag
  • buffer β€” [out] buffer to serialize to
  • buffer_size β€” size of the buffer
  • Returns β€” length of the result
  • Returns β€” 0 if the buffer_size doesn't fit the result. The buffer may still be modified

✨ cbor_serialize_float_ctrl()

size_t cbor_serialize_float_ctrl(const cbor_item_t *item, cbor_mutable_data buffer, size_t buffer_size)

Serialize a.

(yes. that's the whole description. "Serialize a." that's it. the docs said what they said and we respect it πŸ’…)

  • item β€” a float or ctrl
  • buffer β€” [out] buffer to serialize to
  • buffer_size β€” size of the buffer
  • Returns β€” length of the result
  • Returns β€” 0 if the buffer_size doesn't fit the result
ngl notice this one does not say the buffer may still be modified. that's not me forgetting, that's the actual doc. read it exactly as written πŸ™

🌊 Streaming Decoding

okay so this is the fun part. libcbor exposes a stateless decoder that reads a stream of input bytes out of a buffer and invokes user-provided callbacks as it decodes the input πŸ¦‹

struct cbor_decoder_result cbor_stream_decode(cbor_data source, size_t source_size, const struct cbor_callbacks *callbacks, void *context)

Stateless decoder.

It'll try parsing the source and will invoke the appropriate callback on success. Decodes one item at a time. No memory allocations occur. (iconic behavior honestly ✨)

  • source β€” input buffer
  • source_size β€” length of the buffer
  • callbacks β€” the callback bundle
  • context β€” an arbitrary pointer to allow for maintaining 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 (https://github.com/PJK/libcbor/blob/master/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 undefined β€” undef
  • cbor_simple_callback null β€” null
  • cbor_bool_callback boolean β€” bool
  • 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. (the "respond to nothing, serve looks" bundle 🫢)


πŸŽ€ 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 (https://github.com/PJK/libcbor/blob/master/src/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). no cap, that part is on you πŸ’…

Streaming encoding is typically used to create an streaming (indefinite length) CBOR strings, byte strings, arrays, and maps. Complete example: examples/streaming_array.c (https://github.com/PJK/libcbor/blob/master/examples/streaming_array.c)

size_t cbor_encode_uint8(uint8_t value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_uint16(uint16_t value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_uint32(uint32_t value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_uint64(uint64_t value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_uint(uint64_t value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_negint8(uint8_t value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_negint16(uint16_t value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_negint32(uint32_t value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_negint64(uint64_t value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_negint(uint64_t value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_bytestring_start(size_t length, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_indef_bytestring_start(unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_string_start(size_t length, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_indef_string_start(unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_array_start(size_t length, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_indef_array_start(unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_map_start(size_t length, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_indef_map_start(unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_tag(uint64_t value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_bool(bool value, unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_null(unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_undef(unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_half(float value, unsigned char *buffer, size_t buffer_size)

πŸ’– cbor_encode_half()

Encodes a half-precision float.

Since there is no native representation or semantics for half floats in the language, we use single-precision floats, as every value that can be expressed as a half-float can also be expressed as a float.

This however means that not all floats passed to this function can be unambiguously encoded. The behavior is as follows:

  • Infinity, NaN are preserved
  • Zero is preserved
  • Denormalized numbers keep their sign bit and 10 most significant bit of the significand
  • All other numbers.
    • If the logical value of the exponent is < -24, the output is zero
    • If the logical value of the exponent is between -23 and -14, the output is cut off to represent the 'magnitude' of the input, by which we mean (-1)^{signbit} x 1.0e{exponent}. The value in the significand is lost.
    • In all other cases, the sign bit, the exponent, and 10 most significant bits of the significand are kept
bestie that middle bullet is literally lossy β€” when the exponent lands between -23 and -14 your significand is gone gone. not my rules, that's just what it says 😭
Note: Signaling NaNs are encoded as a standard, "quiet" NaN.
size_t cbor_encode_single(float value, unsigned char *buffer, size_t buffer_size)

Encodes a single precision float. 🫧

Note: Signaling NaNs are encoded as a standard, "quiet" NaN.
size_t cbor_encode_double(double value, unsigned char *buffer, size_t buffer_size)

Encodes a double precision float. ✨

Note: Signaling NaNs are encoded as a standard, "quiet" NaN.
size_t cbor_encode_break(unsigned char *buffer, size_t buffer_size)
size_t cbor_encode_ctrl(uint8_t value, unsigned char *buffer, size_t buffer_size)

Types 0 & 1 – Positive and negative integers</p> πŸ”’

CBOR has two types of integers β€” positive (which you can lowkey just treat as unsigned, bestie) and negative. And there are four possible widths for an integer: 1, 2, 4, or 8 bytes. These are represented by:

enum cbor_int_width

Possible widths of CBOR_TYPE_UINT <#data_8h_1a3a931b40fc31b51cccfd1bc3dc5fc5d6a2137d55d8028e37ff4319a633ef0634f> items.

Values:

  • enumerator CBOR_INT_8
  • enumerator CBOR_INT_16
  • enumerator CBOR_INT_32
  • enumerator CBOR_INT_64

Type 0 - positive integers ⭐

  • Corresponding cbor_type <#_CPPv49cbor_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. Periodt. πŸ’…

Type 1 - negative integers πŸŒ€

  • Corresponding cbor_type <#_CPPv49cbor_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. (yes, same energy as above, we're just built like that) πŸŽ€

Type 0 & 1 🫢

Because their semantics are basically twins, the functions below work for both Type 0 and Type 1 items. You can convert between them freely using the conversion functions <#dealing-with-signedness>.

The actual type of the integer can be checked using the item types API <>.

An integer item gets created with one of the four widths. And because an integer's storage is bundled together with the handle </internal#c.cbor_item_t.data>, the width cannot be changed over its lifetime. Sorry bestie, no glow-up for widths. 😭

Warning: So here's the thing β€” CBOR negative integers represent integers in the range [-1, -2^N], which makes the cbor_set_uint API kinda counter-intuitive, because the resulting logical value is 1 less. This behavior is necessary so you can uniformly manipulate the full range of permitted values. For example, this snippet: c cbor_item_t * item = cbor_new_int8(); cbor_mark_negint(item); cbor_set_uint8(0); will produce an item with the logical value of -1. But ngl there's an upside too: there is only one representation of zero. iconic. πŸ‘‘

Building new items πŸ”¨

cbor_item_t <#_CPPv411cbor_item_t> *cbor_build_uint8(uint8_t value)

Constructs a new positive integer.

  • param value β€” the value to use
  • return new positive integer or NULL on memory allocation failure
cbor_item_t <#_CPPv411cbor_item_t> *cbor_build_uint16(uint16_t value)

Constructs a new positive integer.

  • param value β€” the value to use
  • return new positive integer or NULL on memory allocation failure
cbor_item_t <#_CPPv411cbor_item_t> *cbor_build_uint32(uint32_t value)

Constructs a new positive integer.

  • param value β€” the value to use
  • return new positive integer or NULL on memory allocation failure
cbor_item_t <#_CPPv411cbor_item_t> *cbor_build_uint64(uint64_t value)

Constructs a new positive integer.

  • param value β€” the value to use
  • return new positive integer or NULL on memory allocation failure

Retrieving values πŸ“

uint8_t cbor_get_uint8(const cbor_item_t <#_CPPv411cbor_item_t> *item)

Extracts the integer value.

  • param item β€” positive or negative integer
  • return the value
uint16_t cbor_get_uint16(const cbor_item_t <#_CPPv411cbor_item_t> *item)

Extracts the integer value.

  • param item β€” positive or negative integer
  • return the value
uint32_t cbor_get_uint32(const cbor_item_t <#_CPPv411cbor_item_t> *item)

Extracts the integer value.

  • param item β€” positive or negative integer
  • return the value
uint64_t cbor_get_uint64(const cbor_item_t <#_CPPv411cbor_item_t> *item)

Extracts the integer value.

  • param item β€” positive or negative integer
  • return the value

Setting values ✍️

void cbor_set_uint8(cbor_item_t <#_CPPv411cbor_item_t> *item, uint8_t value)

Assigns the integer value.

  • param item β€” 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 <#_CPPv411cbor_item_t> *item, uint16_t value)

Assigns the integer value.

  • param item β€” 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 <#_CPPv411cbor_item_t> *item, uint32_t value)

Assigns the integer value.

  • param item β€” 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 <#_CPPv411cbor_item_t> *item, uint64_t value)

Assigns the integer value.

  • param item β€” positive or negative integer item
  • param value β€” the value to assign. For negative integer, the logical value is -value - 1

Dealing with width πŸ“

cbor_int_width <#_CPPv414cbor_int_width> cbor_int_get_width(const cbor_item_t <#_CPPv411cbor_item_t> *item)

Queries the integer width.

  • param item β€” positive or negative integer item
  • return the width

Dealing with signedness πŸ¦‹

void cbor_mark_uint(cbor_item_t <#_CPPv411cbor_item_t> *item)

Marks the integer item as a positive integer.

The data value is not changed.

  • param item β€” positive or negative integer item
void cbor_mark_negint(cbor_item_t <#_CPPv411cbor_item_t> *item)

Marks the integer item as a negative integer.

The data value is not changed.

  • param item β€” positive or negative integer item

Creating new items 🌱

cbor_item_t <#_CPPv411cbor_item_t> *cbor_new_int8(void)

Allocates new integer with 1B width.

The width cannot be changed once allocated.

  • return new positive integer or NULL on memory allocation failure. The value is not initialized
cbor_item_t <#_CPPv411cbor_item_t> *cbor_new_int16(void)

Allocates new integer with 2B width.

The width cannot be changed once allocated.

  • return new positive integer or NULL on memory allocation failure. The value is not initialized
cbor_item_t <#_CPPv411cbor_item_t> *cbor_new_int32(void)

Allocates new integer with 4B width.

The width cannot be changed once allocated.

  • return new positive integer or NULL on memory allocation failure. The value is not initialized
cbor_item_t <#_CPPv411cbor_item_t> *cbor_new_int64(void)

Allocates new integer with 8B width.

The width cannot be changed once allocated.

  • return new positive integer or NULL on memory allocation failure. The value is not initialized

Type 2 – Byte strings 🧊

CBOR byte strings are literally just an (ordered) series of bytes with no further interpretation (unless there's a tag <>). A byte string's length may or may not be known during encoding, and you can tell these two kinds apart with cbor_bytestring_is_definite() and cbor_bytestring_is_indefinite() respectively. πŸŽ€

If a byte string is indefinite, it gets encoded as a series of definite byte strings. Those 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. Okay so here's the tradeoff bestie: on one hand it enables streaming messages even before they are fully generated ✨, on the other hand it adds more complexity to the client code 😭. Iconic behaviour, honestly.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Corresponding cbor_type <# β”‚ CBOR_TYPE_BYTESTRING       β”‚
β”‚ _CPPv49cbor_type>          β”‚                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Number of allocations      β”‚ One plus any manipulations β”‚
β”‚ (definite)                 β”‚ with the data              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Number of allocations      β”‚ One plus logarithmically   β”‚
β”‚ (indefinite)               β”‚ many reallocations         β”‚
β”‚                            β”‚ relative  to chunk count   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Storage requirements       β”‚ sizeof(cbor_item_t) +      β”‚
β”‚ (definite)                 β”‚ length(handle)             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Storage requirements       β”‚ sizeof(cbor_item_t) * (1 + β”‚
β”‚ (indefinite)               β”‚ chunk_count) + chunks      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🌸 Getting metadata

  • size_t cbor_bytestring_length(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” hands you the length of the binary data πŸ’…. Definite byte strings only, no exceptions bestie.
    • item β€” a definite bytestring.
    • Returns β€” length of the binary data. Zero if no chunk has been attached yet.
  • bool cbor_bytestring_is_definite(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” Is the byte string definite? 🫢
    • item β€” a byte string.
    • Returns β€” Is the byte string definite?
  • bool cbor_bytestring_is_indefinite(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” Is the byte string indefinite? 🌈
    • item β€” a byte string.
    • Returns β€” Is the byte string indefinite?
  • size_t cbor_bytestring_chunk_count(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” Get the number of chunks this string consist of πŸŽ€.
    • item β€” A indefinite bytestring.
    • Returns β€” The chunk count. 0 for freshly created items.

πŸ¦‹ Reading data

  • cbor_mutable_data cbor_bytestring_handle(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” Get the handle to the binary data ✨. Definite items only. Modifying the data is allowed β€” but in that case, the caller takes responsibility for the effect on items this item might be a part of πŸ‘€.
    • item β€” A definite byte string.
    • Returns β€” The address of the underlying binary data.
    • Returns β€” NULL if no data have been assigned yet.
  • cbor_item_t <#_CPPv411cbor_item_t> **cbor_bytestring_chunks_handle(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” Get the handle to the array of chunks πŸ’–.
    • Manipulations with the memory block (e.g. sorting it) are allowed, but the validity and the number of chunks must be retained.
    • item β€” A indefinite byte string.
    • Returns β€” array of cbor_bytestring_chunk_count <#bytestrings_8h_1ad02102832d452fd5a5cfa96272b1095d> definite bytestrings.

🧚 Creating new items

  • cbor_item_t <#_CPPv411cbor_item_t> *cbor_new_definite_bytestring(void) β€” Creates a new definite byte string πŸ’«. The handle is initialized to NULL and length to 0.
    • Returns β€” Reference to the new bytestring item. The item's reference count is initialized to one.
    • Returns β€” NULL if memory allocation fails 😭.
  • cbor_item_t <#_CPPv411cbor_item_t> *cbor_new_indefinite_bytestring(void) β€” Creates a new indefinite byte string πŸŒ€. The chunks array is initialized to NULL and chunk count to 0.
    • Returns β€” Reference to the new bytestring item. The item's reference count is initialized to one.
    • Returns β€” NULL if memory allocation fails 😭.

πŸ“ Building items

  • cbor_item_t <#_CPPv411cbor_item_t> *cbor_build_bytestring(cbor_data handle, size_t length) β€” Creates a new byte string and initializes it πŸ”₯. The handle will be copied to a newly allocated block, so no sneaky aliasing.
    • handle β€” Block of binary data.
    • length β€” Length of data.
    • Returns β€” Reference to the new bytestring item. The item's reference count is initialized to one.
    • Returns β€” NULL if memory allocation fails.

πŸ’… Manipulating existing items

  • void cbor_bytestring_set_handle(cbor_item_t <#_CPPv411cbor_item_t> *item, cbor_mutable_data data, size_t length) β€” Set the handle to the binary data.
    • item β€” A definite byte string.
    • data β€” The memory block. The caller gives up the ownership of the block. libcbor will deallocate it when appropriate using the free implementation configured using cbor_set_allocs.
    • length β€” Length of the data block.
  • bool cbor_bytestring_add_chunk(cbor_item_t <#_CPPv411cbor_item_t> *item, cbor_item_t <#_CPPv411cbor_item_t> *chunk) β€” Appends a chunk to the bytestring 🌸. Indefinite byte strings only. May realloc the chunk storage (so, y'know, expect some movement).
    • item β€” An indefinite byte string.
    • chunk β€” A definite byte string. Its reference count will be be increased by one. (yes, "be be", we said what we said πŸ’…)
    • Returns β€” true on success, false on realloc failure. In that case, the refcount of chunk is not increased and the item is left intact.

πŸ‘‘ Type 3 – UTF-8 strings

CBOR strings have the same structure as Type 2 – Byte strings 🌈. Literally the same blueprint, different vibe.

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Corresponding cbor_type <# β”‚ CBOR_TYPE_STRING           β”‚
β”‚ _CPPv49cbor_type>          β”‚                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Number of allocations      β”‚ One plus any manipulations β”‚
β”‚ (definite)                 β”‚ with the data              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Number of allocations      β”‚ One plus logarithmically   β”‚
β”‚ (indefinite)               β”‚ many reallocations         β”‚
β”‚                            β”‚ relative  to chunk count   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Storage requirements       β”‚ sizeof(cbor_item_t) +      β”‚
β”‚ (definite)                 β”‚ length(handle)             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Storage requirements       β”‚ sizeof(cbor_item_t) * (1 + β”‚
β”‚ (indefinite)               β”‚ 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 β€” so bestie, that one's on you.


🌸 Getting metadata

  • size_t cbor_string_length(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” Returns the length of the underlying string in bytes πŸ’–. There can be fewer unicode character than bytes (see cbor_string_codepoint_count). For definite strings only.
    • item β€” a definite string.
    • Returns β€” length of the string. Zero if no chunk has been attached yet.
  • bool cbor_string_is_definite(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” Is the string definite? 🫢
    • item β€” a string.
    • Returns β€” Is the string definite?
  • bool cbor_string_is_indefinite(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” Is the string indefinite? 🌈
    • item β€” a string.
    • Returns β€” Is the string indefinite?
  • size_t cbor_string_chunk_count(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” Get the number of chunks this string consist of πŸŽ€.
    • item β€” A indefinite string.
    • Returns β€” The chunk count. 0 for freshly created items.

πŸ¦‹ Reading data

  • cbor_mutable_data cbor_string_handle(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” Get the handle to the underlying string ✨. Definite items only. Modifying the data is allowed β€” but in that case, the caller takes responsibility for the effect on items this item might be a part of πŸ‘€.
    • item β€” A definite string.
    • Returns β€” The address of the underlying string.
    • Returns β€” NULL if no data have been assigned yet.
  • cbor_item_t <#_CPPv411cbor_item_t> **cbor_string_chunks_handle(const cbor_item_t <#_CPPv411cbor_item_t> *item) β€” Get the handle to the array of chunks πŸ’œ.
    • Manipulations with the memory block (e.g. sorting it) are allowed, but the validity and the number of chunks must be retained.
    • item β€” A indefinite string.
    • Returns β€” array of cbor_string_chunk_count <#strings_8h_1ad8062db21711a51ae520cc1e4f5fe5ab> definite strings.

✨ Creating new items ✨

🌱 cbor_new_definite_string

cbor_item_t *cbor_new_definite_string(void)

Makes you a shiny new definite string. The handle gets initialized to NULL and the length to 0 β€” clean slate energy, we love to see it πŸ’–

  • return β€” reference to the new string item. The item's reference count is initialized to one.
  • return β€” NULL if memory allocation fails 😭

🌱 cbor_new_indefinite_string

cbor_item_t *cbor_new_indefinite_string(void)

Makes you a shiny new indefinite string. The chunks array gets initialized to NULL and chunkcount to 0.

  • return β€” reference to the new string item. The item's reference count is initialized to one.
  • return β€” NULL if memory allocation fails

πŸ› οΈ Building items

πŸŽ€ cbor_build_string

cbor_item_t *cbor_build_string(const char *val)

Creates a new string and initializes it β€” doing two things at once, main character behaviour honestly ✨

The data from val will be copied to a newly allocated memory block.

Heads up bestie: valid UTF-8 strings do not contain null bytes, so this routine is correct for all valid inputs. But if your input is not guaranteed to be valid, use cbor_build_stringn instead. Don't be messy, k? πŸ™ˆ
  • param val β€” a null-terminated UTF-8 string
  • return β€” reference to the new string item. The item's reference count is initialized to one.
  • return β€” NULL if memory allocation fails

πŸ’… Manipulating existing items

cbor_string_set_handle

void cbor_string_set_handle(cbor_item_t *item, cbor_mutable_data data, size_t length)

Sets the handle to the underlying string.

The data is assumed to be a valid UTF-8 string. If the string is non-empty and invalid, cbor_string_codepoint_count will return 0.

⚠️ Warning: using a pointer to a stack allocated constant is a super common mistake. The lifetime of the string will expire when it goes out of scope and the CBOR item will be left inconsistent. It's giving dangling pointer, it's giving chaos. Don't. πŸ™ˆ
  • param item β€” a definite string
  • param data β€” the memory block. The caller gives up the ownership of the block. libcbor will deallocate it when appropriate using its free function
  • param length β€” length of the data block

cbor_string_add_chunk

bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk)

Appends a chunk to the string. Indefinite strings only, okurrr πŸ’…

May realloc the chunk storage.

  • param item β€” an indefinite string
  • param chunk β€” a definite string item. Its reference count will be increased by one.
  • return β€” true on success. false on memory allocation failure. In that case, the refcount of chunk is not increased and the item is left intact 🫢

πŸ”’ 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. Periodt.

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: indefinite arrays can be conveniently used with streaming decoding <> and encoding <>.

Okay so here's the stats breakdown, bestie β€” no table, just vibes and facts:

  • 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

cbor_array_size

size_t cbor_array_size(const cbor_item_t *item)

Get the number of members.

  • param item β€” an array
  • return β€” the number of members

cbor_array_allocated

size_t cbor_array_allocated(const cbor_item_t *item)

Get the size of the allocated storage.

  • param item β€” an array
  • return β€” the size of the allocated storage (number of items)

cbor_array_is_definite

bool cbor_array_is_definite(const cbor_item_t *item)

Is the array definite?

  • param item β€” an array
  • return β€” is the array definite?

cbor_array_is_indefinite

bool cbor_array_is_indefinite(const cbor_item_t *item)

Is the array indefinite?

  • param item β€” an array
  • return β€” is the array indefinite?

πŸ“– Reading data

cbor_array_handle

cbor_item_t **cbor_array_handle(const cbor_item_t *item)

Get the array contents.

The items may be reordered and modified as long as references remain consistent.

  • param item β€” an array item
  • return β€” an array of cbor_item_t pointers of size cbor_array_size

cbor_array_get

cbor_item_t *cbor_array_get(const cbor_item_t *item, size_t index)

Get item by index. Slay, indexing.

Increases the reference count of the underlying item. The returned reference must be released using cbor_decref.

  • param item β€” an array
  • param index β€” the index (zero-based)
  • return β€” reference to the item, or NULL in case of boundary violation 😭

✨ Creating new items ✨

🌱 cbor_new_definite_array

cbor_item_t *cbor_new_definite_array(size_t size)

Create new definite array.

  • param size β€” number of slots to preallocate
  • return β€” reference to the new array item. The item's reference count is initialized to one.
  • return β€” NULL if memory allocation fails

🌱 cbor_new_indefinite_array

cbor_item_t *cbor_new_indefinite_array(void)

Create new indefinite array.

  • return β€” reference to the new array item. The item's reference count is initialized to one.
  • return β€” NULL if memory allocation fails

πŸ› οΈ Modifying items

cbor_array_push

bool cbor_array_push(cbor_item_t *array, cbor_item_t *pushee)

Append to the end. Push it, push it πŸ’…

For indefinite items, storage may be reallocated. For definite items, only the preallocated capacity is available.

  • param array β€” an array
  • param pushee β€” the item to push. Its reference count will be increased by one.
  • return β€” true on success, false on failure

cbor_array_replace

bool cbor_array_replace(cbor_item_t *item, size_t index, cbor_item_t *value)

Replace item at an index.

The reference to the item being replaced will be released using cbor_decref.

  • param item β€” an array
  • param value β€” the item to assign. Its reference count will be increased by one.
  • param index β€” the index (zero-based)
  • return β€” true on success, false on allocation failure.

cbor_array_set

bool cbor_array_set(cbor_item_t *item, size_t index, cbor_item_t *value)

Set item by index.

If the index is out of bounds, the array is not modified and false is returned. Creating arrays with holes is not possible. No gap year for arrays, bestie 🚫

  • param item β€” an array
  • param value β€” the item to assign. Its reference count will be increased by one.
  • param index β€” the index (zero-based)
  • return β€” true on success, false on allocation failure.

πŸ—ΊοΈ Type 5 – Maps

CBOR maps are the plain old associative maps similar JSON objects or Python dictionaries. You know the vibe β€” keys and values, doing their little pair thing πŸ’–

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: indefinite maps can be conveniently used with streaming decoding <> and encoding <>. Keys and values can simply be output one by one, alternating keys and values. It's giving conveyor belt, it's giving iconic 🌈
⚠️ Warning: literally any CBOR data item can be a legal map key, not just strings. bestie the keys are unhinged and free πŸ’€
  • 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 ✨

okay so here's the vibe in bytes πŸ¦‹

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 πŸ”

size_t cbor_map_size(const cbor_item_t *item)

Get the number of pairs. periodt.

  • item β€” A map
  • returns β€” The number of pairs

size_t cbor_map_allocated(const cbor_item_t *item)

Get the size of the allocated storage. it's giving capacity, not count πŸ’…

  • item β€” A map
  • returns β€” Allocated storage size (as the number of cbor_pair items)

bool cbor_map_is_definite(const cbor_item_t *item)

Is this map definite? the vibe check πŸŽ€

  • item β€” A map
  • returns β€” Is this map definite?

bool cbor_map_is_indefinite(const cbor_item_t *item)

Is this map indefinite? the other vibe check ✨

  • item β€” A map
  • returns β€” Is this map indefinite?

πŸ“– Reading data πŸ“–

struct cbor_pair *cbor_map_handle(const cbor_item_t *item)

Get the pairs storage. the actual goods πŸ”₯

  • item β€” A map
  • returns β€” Array of cbor_map_size pairs. Manipulation is possible as long as references remain valid.

🌱 Creating new items 🌱

cbor_item_t *cbor_new_definite_map(size_t size)

Create a new definite map. main character energy, no realloc arcs πŸ‘‘

  • size β€” The number of slots to preallocate
  • returns β€” Reference to the new map item. The item's reference count is initialized to one.
  • returns β€” NULL if memory allocation fails 😭

cbor_item_t *cbor_new_indefinite_map(void)

Create a new indefinite map. it grows as it goes, lowkey iconic 🧚

  • returns β€” Reference to the new map item. The item's reference count is initialized to one.
  • returns β€” NULL if memory allocation fails 😭

πŸ› οΈ Modifying items πŸ› οΈ

bool cbor_map_add(cbor_item_t *item, struct cbor_pair pair)

Add a pair to the map. slay.

For definite maps, items can only be added to the preallocated space. For indefinite maps, the storage will be expanded as needed.

  • item β€” A map
  • pair β€” The key-value pair to add. Reference count of the cbor_pair.key and cbor_pair.value will be increased by one.
  • returns β€” true on success, false if memory allocation failed (indefinite maps) or the preallocated storage is full (definite maps)

🏷️ Type 6 – Semantic tags 🏷️

Tags are additional metadata that can be used to extend or specialize the meaning or interpretation of the other data items. it's giving sticky notes for your data fr fr

For example, one might tag an array of numbers to communicate that it should be interpreted as a vector. cute right?

Please consult the official IANA repository of CBOR tags at https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml for known registered values.

πŸ’€ Please note that libcbor does not understand the semantics of tags and will process all well-formed tags (as defined in https://datatracker.ietf.org/doc/html/rfc8949#name-terminology) regardless of whether they are valid with respect to the data they are applied to. no cap, it does not read the room.
  • 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

cbor_item_t *cbor_new_tag(uint64_t value)

Create a new tag. fresh tag, who dis 🌸

  • value β€” The tag value (number).
  • returns β€” Reference to the new tag. Its reference count is initialized to one and it points to a NULL item.
  • returns β€” NULL if memory allocation fails.

cbor_item_t *cbor_tag_item(const cbor_item_t *tag)

Get the tagged item (what the tag points to). the item the tag is ✨obsessed✨ with

Increases the reference count of the underlying item. The returned reference must be released using cbor_decref.

  • tag β€” A CBOR_TYPE_TAG tag.
  • returns β€” Reference to the tagged item.

uint64_t cbor_tag_value(const cbor_item_t *tag)

Get the tag value. the number, not the thing πŸŽ€

  • tag β€” A CBOR_TYPE_TAG tag.
  • returns β€” The tag value (number).

void cbor_tag_set_item(cbor_item_t *tag, cbor_item_t *tagged_item)

Assign a tag to an item. matchmaker matchmaker πŸ’–

If the tag already points to an item, the pointer will be replaced, without a reference count change on the previous item. TODO: Should we release the reference automatically?

  • tag β€” A CBOR_TYPE_TAG tag.
  • tagged_item β€” The item to tag. Its reference count will be increased by one.

🎈 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. two things in one trench coat, highkey chaotic 🌈

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:

  • CBOR_FLOAT_0 β€” Internal use - ctrl and special values.
  • CBOR_FLOAT_16 β€” Half float.
  • CBOR_FLOAT_32 β€” Single float.
  • 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 πŸ”

bool cbor_float_ctrl_is_ctrl(const cbor_item_t *item)

Is this a ctrl value? is it a control token or just a float being a float πŸ™ˆ

  • item β€” A float or ctrl item
  • returns β€” Is this a ctrl value?

cbor_float_width cbor_float_get_width(const cbor_item_t *item)

Get the float width. how chunky is this float πŸ’…

  • item β€” A float or ctrl item
  • returns β€” The width.

πŸ’– Reading data πŸ’–

okay bestie, first up: getting values OUT of the items you already have. And listen β€” these getters are picky about width, they will not do the work for you πŸ’€

  • float cbor_float_get_float2(const cbor_item_t *item) β€” grab a half precision float 🌸
    • The item must have the corresponding width
    • item: A half precision float
    • returns: half precision value
  • float cbor_float_get_float4(const cbor_item_t *item) β€” grab a single precision float ✨
    • The item must have the corresponding width
    • item: A single precision float
    • returns: single precision value
  • double cbor_float_get_float8(const cbor_item_t *item) β€” grab a double precision float πŸ’…
    • The item must have the corresponding width
    • item: A double precision float
    • returns: double precision value
  • double cbor_float_get_float(const cbor_item_t *item) β€” the universal girlie, gets the float value represented as double πŸ‘‘
    • Can be used regardless of the width β€” she truly does not care
    • item: Any float
    • returns: double precision value
  • uint8_t cbor_ctrl_value(const cbor_item_t *item) β€” reads the control value πŸŽ€
    • item: A ctrl item
    • returns: the simple value
  • bool cbor_get_bool(const cbor_item_t *item) β€” get value from a boolean ctrl item πŸ¦‹
    • item: A ctrl item
    • returns: boolean value

✨ Creating new items ✨

brand new item, who dis? 🫢 these all hand you back a reference with the reference count already at one, and they all scream NULL if memory allocation fails.

heads up: the width cannot be changed once the item is created. So pick your width like you pick your fit β€” it's permanent, no cap.
  • cbor_item_t *cbor_new_ctrl(void) β€” constructs a new ctrl item πŸŽ€
    • returns: Reference to the new ctrl item. The item's reference count is initialized to one.
    • returns: NULL if memory allocation fails
  • cbor_item_t *cbor_new_float2(void) β€” constructs a new float item πŸ’–
    • returns: Reference to the new float item. The item's reference count is initialized to one.
    • returns: NULL if memory allocation fails
  • cbor_item_t *cbor_new_float4(void) β€” constructs a new float item ✨
    • returns: Reference to the new float item. The item's reference count is initialized to one.
    • returns: NULL if memory allocation fails
  • cbor_item_t *cbor_new_float8(void) β€” constructs a new float item 🌸
    • returns: Reference to the new float item. The item's reference count is initialized to one.
    • returns: NULL if memory allocation fails
  • cbor_item_t *cbor_new_null(void) β€” constructs new null ctrl item 🌈
    • returns: Reference to the new null item. The item's reference count is initialized to one.
    • returns: NULL if memory allocation fails
  • cbor_item_t *cbor_new_undef(void) β€” constructs new undef ctrl item 🧚
    • returns: Reference to the new undef item. The item's reference count is initialized to one.
    • returns: NULL if memory allocation fails

πŸ› οΈ Building items πŸ› οΈ

same energy as the new-item girlies, but these ones let you hand over the value right at birth. so efficient, we love her πŸ’…

  • cbor_item_t *cbor_build_bool(bool value) β€” constructs new boolean ctrl item πŸ’œ
    • value: The value to use
    • returns: Reference to the new boolean item. The item's reference count is initialized to one.
    • returns: NULL if memory allocation fails
  • cbor_item_t *cbor_build_ctrl(uint8_t value) β€” constructs a ctrl item πŸŽ€
    • value: the value to use
    • returns: Reference to the new ctrl item. The item's reference count is initialized to one.
    • returns: NULL if memory allocation fails
  • cbor_item_t *cbor_build_float2(float value) β€” constructs a new float πŸ¦‹
    • value: the value to use
    • returns: Reference to the new float item. The item's reference count is initialized to one.
    • returns: NULL if memory allocation fails
  • cbor_item_t *cbor_build_float4(float value) β€” constructs a new float ✨
    • value: the value to use
    • returns: Reference to the new float item. The item's reference count is initialized to one.
    • returns: NULL if memory allocation fails
  • cbor_item_t *cbor_build_float8(double value) β€” constructs a new float πŸ”₯
    • value: the value to use
    • returns: Reference to the new float item. The item's reference count is initialized to one.
    • returns: NULL if memory allocation fails

🌸 Manipulating existing items 🌸

for when the item already exists and you just wanna change her mind.

  • void cbor_set_ctrl(cbor_item_t *item, uint8_t value) β€” assign a control value
    • item: A ctrl item
    • value: The simple value to assign. Please consult the standard for allowed values
⚠️ Warning: It is possible to produce an invalid CBOR value by assigning an invalid value using this mechanism. Please consult the standard before use. like, fr fr, read the standard first, don't just yolo it 😭
  • void cbor_set_bool(cbor_item_t *item, bool value) β€” assign a boolean value to a boolean ctrl item πŸŽ€
    • item: A ctrl item
    • value: The simple value to assign.
  • void cbor_set_float2(cbor_item_t *item, float value) β€” assigns a float value 🌸
    • item: A half precision float
    • value: The value to assign
  • void cbor_set_float4(cbor_item_t *item, float value) β€” assigns a float value ✨
    • item: A single precision float
    • value: The value to assign
  • void cbor_set_float8(cbor_item_t *item, double value) β€” assigns a float value πŸ’œ
    • item: A double precision float
    • value: The value to assign

🧚 Half floats 🧚

okay so CBOR supports two bytes wide ("half-precision") floats, and bestie… C literally does not have those. the C language said "no <3". so libcbor represents them using float values throughout the API instead, and that's just how it is πŸ’…

Encoding will be performed by cbor_encode_half(), which will handle any values that cannot be represented as a half-float. she's got it covered, don't stress 🌸


πŸ’€ Signaling NaNs πŸ’€

Signaling NaNs are always encoded as a standard, "quiet" NaN.

The reason for this simplification is that standard C does not offer a way to handle the signaling payload without assumptions about the host architecture. See https://github.com/PJK/libcbor/issues/336 for more context.

[1] http://softwareengineering.vazexqi.com/files/pattern.html

πŸ§ͺ Tests πŸ§ͺ

Unit tests

There is a comprehensive test suite employing CMocka (http://cmocka.org/). You can run all of them using ctest in the build directory. Individual tests are themselves runnable. Please refer to CTest (http://www.cmake.org/Wiki/CMake/Testing_With_CTest) documentation for detailed information on how to specify particular subset of tests. so many options, so much power πŸ‘‘

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] Project should be configured with -DCMAKE_BUILD_TYPE=Debug to obtain meaningful description of location of the leak. You might also need --dsymutil=yes on OS X.

Code coverage

Every release is inspected using GCOV/LCOV (http://ltp.sourceforge.net/coverage/lcov.php). Platform-independent code should be fully covered by the test suite. Simply run

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. chaos but make it responsible ✨


πŸ“œ IETF standard conformance πŸ“œ

libcbor is, generally speaking, a very faithful implementation of IETF RFC 8949 (STD 94) (https://www.rfc-editor.org/info/std94). she ate, honestly. There are, however, some limitations related to the numerical range and precision available in portable C99. not her fault, it's the language's fault 😭

Bytestring length

There is no explicit limitation of indefinite length byte strings. libcbor will not handle byte strings with more chunks than the maximum value of 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 🌊

[1] https://www.rfc-editor.org/rfc/rfc8949.html#section-3.2.3

"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. 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. (scroll up bestie, it's right there πŸ’–)


πŸ”§ 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. iykyk 🌈

✨ Terminology ✨

Okay bestie, vocab check before we go any further, this part matters:


πŸŽ€ Conventions πŸŽ€

API symbols start with cbor_ or CBOR_ prefix, internal symbols have _cbor_ or _CBOR_ prefix. That's the whole rule. Icons only. πŸ’…


Most of the API is largely modelled after existing JSON libraries, including

and also borrowing from


🌸 General notes on the API design 🌸

The API design has two main driving principles:

  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 <https://www.kernel.org/doc/Documentation/CodingStyle>. Tabs are tabs, and they are 4 characters wide. No cap. 🫢


πŸ‘‘ 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 <#_CPPv49cbor_type> type β€” Type discriminator
  • size_t refcount β€” Reference counter. Used by cbor_decref() <#_CPPv411cbor_decrefPP11cbor_item_t>, cbor_incref() <#_CPPv411cbor_increfP11cbor_item_t>
  • union cbor_item_metadata <#_CPPv418cbor_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. Iconic behaviour honestly ✨

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 <>
  • struct _cbor_bytestring_metadata bytestring_metadata
  • struct _cbor_string_metadata string_metadata
  • struct _cbor_array_metadata array_metadata
  • struct _cbor_map_metadata map_metadata
  • struct _cbor_tag_metadata tag_metadata
  • struct _cbor_float_ctrl_metadata float_ctrl_metadata

πŸ”₯ 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. That's it, that's the vibe. πŸ’œ


🌈 Changelog 🌈

Template:


⭐ Next ⭐

0.13.0 (2025-08-30)

0.12.0 (2025-03-16)

0.11.0 (2024-02-04)

0.10.2 (2023-01-31)

🌸 0.10.1 (2022-12-30)

✨ 0.10.0 (2022-12-29)

Warning: This may cause new build warnings and (in rare cases, depending on your configuration) errors 🚨
  • BREAKING: Fixed 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 πŸ‘€
  • Fixed cbor_build_tag illegal memory behavior when the allocator fails (https://github.com/PJK/libcbor/pull/249)
  • Added 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: Deprecated 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)

πŸŽ€ 0.8.0 (2020-09-20)

πŸ’œ 0.7.0 (2020-04-25)

Warning: Previous versions encoded NaNs as 0xf9e700 instead of 0xf97e00; if you rely on the broken behavior, this will be a breaking change ⚠️

🌈 0.6.1 (2020-03-26)

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. 😬

πŸ“ 0.5.0 (2017-02-06)

🧚 0.4.0 (2015-12-25)

Breaks build & header compatibility due to:

  • Improved build configuration and feature check macros
  • Endianness 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 (she got a glow-up, bestie πŸ’…)
  • Added cbor_array_allocated & the map equivalent πŸŽ€
  • Overhauled endianess conversion β€” ARM now works as expected πŸ¦‹ (we love a platform that finally behaves)
  • sort.c example added πŸ“
  • Significantly improved and doxyfied documentation πŸ“šβœ¨

0.1.0 (2015-05-06) πŸ’–

The initial release, yay! πŸŽ‰πŸ‘‘


🧚 Development 🧚

🌸 Vision and principles 🌸

Okay so, consistency and coherence are literally one of the key characteristics of good software. While the reality is never black and white, it is important that 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. periodt. πŸ’…

πŸ‘‘ Goals πŸ‘‘

🎯 Standard conformance and full feature support

Anything the standard allows, libcbor can do. no cap.

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 [CBOR standard](<https://www.rfc-editor.org/info/std94>) 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. none. zero. nada. πŸ™ˆ

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. fr fr. πŸ¦‹

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. bestie said no surprise jumpscares. πŸ’…

Why? Industry-standard <https://semver.org/> versioning is a basic requirement for production-quality software. This is especially relevant in IoT environments where updates may be costly. πŸŽ€

⚑ Performance

libcbor is fast and resource-efficient by design πŸ”₯

Why? Because the main maintainer is an avid hater of slow bloated software. Who wouldn't want more bang per their electricity buck? πŸ‘‘

🚫 Non-goals 🚫

  • Convenience β€” libcbor only provides the minimum surface to make it usable
  • FFI/SWIG/interop support β€” libcbor is primarily a C library for C clients
  • One-off usecases support β€” although there are primitives to reuse, the basic assumption is that most clients want most of CBOR features

πŸ› οΈ Development dependencies πŸ› οΈ

🐍 Installing sphinx

pip install -r doc/source/requirements.txt

To update the Python dependencies:

pip-compile --upgrade doc/source/requirements.in

Sphinx reference: <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 <https://pages.github.com/> 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 πŸ’œ

2014 - 2020, Pavel Kalvoda 🌈


and that's a wrap on the whole page, bestie!! πŸŽ€ thank you for reading all the way to the end, you're literally the main character. πŸ’…βœ¨

version 0.13 πŸ¦‹, source date Jan 16, 2026 🌸 β€” this is the LIBCBOR(3) page, and yes she's iconic. bye pookie πŸ‘‘πŸ’–

πŸ“¦ where this page comes from