Encoding

The easiest way to encode data items is using the cbor_serialize() or cbor_serialize_alloc() functions:

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

Serialize the given item.

param item:

A data item

param buffer:

Buffer to serialize to

param buffer_size:

Size of the buffer

return:

Length of the result. 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.

Since libcbor v0.10, the return value is always the same as buffer_size (if provided, see https://github.com/PJK/libcbor/pull/251/). New clients should ignore the return value.

Warning

It is the caller’s responsibility to free the buffer using an appropriate free implementation.

param item:

A data item

param buffer:

[out] Buffer containing the result

param buffer_size:

[out] Size of the buffer, or 0 on memory allocation failure.

return:

Length of the result in bytes

return:

0 on memory allocation failure, in which case buffer is NULL.

To determine the number of bytes needed 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.

param item:

A data item

return:

Length (>= 1) of the item when serialized. 0 if the length overflows size_t.

Type-specific serializers

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

Note

Unless compiled in debug mode, these do not verify the type. Passing an incorrect item will result in an undefined behavior.

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

Serialize an uint.

param item:

A uint

param buffer:

[out] Buffer to serialize to

param buffer_size:

Size of the buffer

return:

Length of the result

return:

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.

param item:

A negint

param buffer:

[out] Buffer to serialize to

param buffer_size:

Size of the buffer

return:

Length of the result

return:

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.

param item:

A bytestring

param buffer:

[out] Buffer to serialize to

param buffer_size:

Size of the buffer

return:

Length of the result

return:

0 if the buffer_size doesn’t fit the result. 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.

param item:

A string

param buffer:

[out] Buffer to serialize to

param buffer_size:

Size of the buffer

return:

Length of the result

return:

0 if the buffer_size doesn’t fit the result. The buffer may still be modified

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

Serialize an array.

param item:

An array

param buffer:

[out] Buffer to serialize to

param buffer_size:

Size of the buffer

return:

Length of the result

return:

0 if the buffer_size doesn’t fit the result. The buffer may still be modified

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

Serialize a map.

param item:

A map

param buffer:

[out] Buffer to serialize to

param buffer_size:

Size of the buffer

return:

Length of the result

return:

0 if the buffer_size doesn’t fit the result. The buffer may still be modified

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

Serialize a tag.

param item:

A tag

param buffer:

[out] Buffer to serialize to

param buffer_size:

Size of the buffer

return:

Length of the result

return:

0 if the buffer_size doesn’t fit the result. The buffer may still be modified

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

Serialize a.

param item:

A float or ctrl

param buffer:

[out] Buffer to serialize to

param buffer_size:

Size of the buffer

return:

Length of the result

return:

0 if the buffer_size doesn’t fit the result