Streaming Encoding

cbor/encoding.h exposes a low-level encoding API to encode CBOR objects on the fly. Unlike cbor_serialize(), these functions take logical values (integers, floats, strings, etc.) instead of cbor_item_t. The client is responsible for constructing the compound types correctly (e.g. terminating arrays).

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

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)

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

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)