libcbor 0.12.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
Loading...
Searching...
No Matches
serialization.c File Reference
#include "serialization.h"
#include <string.h>
#include "cbor/arrays.h"
#include "cbor/bytestrings.h"
#include "cbor/floats_ctrls.h"
#include "cbor/ints.h"
#include "cbor/maps.h"
#include "cbor/strings.h"
#include "cbor/tags.h"
#include "encoding.h"
#include "internal/memory_utils.h"

Go to the source code of this file.

Functions

size_t cbor_serialize (const cbor_item_t *item, unsigned char *buffer, size_t buffer_size)
 Serialize the given item.
 
size_t _cbor_encoded_header_size (uint64_t size)
 How many bytes will a tag for a nested item of a given size take when encoded.
 
size_t cbor_serialized_size (const cbor_item_t *item)
 Compute the length (in bytes) of the item when serialized using cbor_serialize.
 
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.
 
size_t cbor_serialize_uint (const cbor_item_t *item, unsigned char *buffer, size_t buffer_size)
 Serialize an uint.
 
size_t cbor_serialize_negint (const cbor_item_t *item, unsigned char *buffer, size_t buffer_size)
 Serialize a negint.
 
size_t cbor_serialize_bytestring (const cbor_item_t *item, unsigned char *buffer, size_t buffer_size)
 Serialize a bytestring.
 
size_t cbor_serialize_string (const cbor_item_t *item, unsigned char *buffer, size_t buffer_size)
 Serialize a string.
 
size_t cbor_serialize_array (const cbor_item_t *item, unsigned char *buffer, size_t buffer_size)
 Serialize an array.
 
size_t cbor_serialize_map (const cbor_item_t *item, unsigned char *buffer, size_t buffer_size)
 Serialize a map.
 
size_t cbor_serialize_tag (const cbor_item_t *item, unsigned char *buffer, size_t buffer_size)
 Serialize a tag.
 
size_t cbor_serialize_float_ctrl (const cbor_item_t *item, unsigned char *buffer, size_t buffer_size)
 Serialize a.
 

Variables

const uint64_t kMaxEmbeddedInt = 23
 Largest integer that can be encoded as embedded in the item leading byte.
 

Function Documentation

◆ _cbor_encoded_header_size()

size_t _cbor_encoded_header_size ( uint64_t size)

How many bytes will a tag for a nested item of a given size take when encoded.

Definition at line 50 of file serialization.c.

◆ cbor_serialize()

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

Serialize the given item.

Parameters
itemA data item
bufferBuffer to serialize to
buffer_sizeSize of the buffer
Returns
Length of the result. 0 on failure.

Definition at line 20 of file serialization.c.

◆ cbor_serialize_alloc()

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.

embed:rst:leading-asterisk 
* .. warning:: It is the caller's responsibility to free the buffer using an
*  appropriate ``free`` implementation.
* 
Parameters
itemA data item
[out]bufferBuffer containing the result
[out]buffer_sizeSize of the buffer, or 0 on memory allocation failure.
Returns
Length of the result in bytes
0 on memory allocation failure, in which case buffer is NULL.

Definition at line 164 of file serialization.c.

◆ cbor_serialize_array()

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

Serialize an array.

Parameters
itemAn array
[out]bufferBuffer to serialize to
buffer_sizeSize of the buffer
Returns
Length of the result
0 if the buffer_size doesn't fit the result. The buffer may still be modified

Definition at line 286 of file serialization.c.

◆ cbor_serialize_bytestring()

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

Serialize a bytestring.

Parameters
itemA bytestring
[out]bufferBuffer to serialize to
buffer_sizeSize of the buffer
Returns
Length of the result
0 if the buffer_size doesn't fit the result. The buffer may still be modified

Definition at line 222 of file serialization.c.

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

Parameters
itemA float or ctrl
[out]bufferBuffer to serialize to
buffer_sizeSize of the buffer
Returns
Length of the result
0 if the buffer_size doesn't fit the result

Definition at line 367 of file serialization.c.

◆ cbor_serialize_map()

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

Serialize a map.

Parameters
itemA map
[out]bufferBuffer to serialize to
buffer_sizeSize of the buffer
Returns
Length of the result
0 if the buffer_size doesn't fit the result. The buffer may still be modified

Definition at line 317 of file serialization.c.

◆ cbor_serialize_negint()

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

Serialize a negint.

Parameters
itemA negint
[out]bufferBuffer to serialize to
buffer_sizeSize of the buffer
Returns
Length of the result
0 if the buffer_size doesn't fit the result

Definition at line 203 of file serialization.c.

◆ cbor_serialize_string()

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

Serialize a string.

Parameters
itemA string
[out]bufferBuffer to serialize to
buffer_sizeSize of the buffer
Returns
Length of the result
0 if the buffer_size doesn't fit the result. The buffer may still be modified

Definition at line 254 of file serialization.c.

◆ cbor_serialize_tag()

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

Serialize a tag.

Parameters
itemA tag
[out]bufferBuffer to serialize to
buffer_sizeSize of the buffer
Returns
Length of the result
0 if the buffer_size doesn't fit the result. The buffer may still be modified

Definition at line 355 of file serialization.c.

◆ cbor_serialize_uint()

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

Serialize an uint.

Parameters
itemA uint
[out]bufferBuffer to serialize to
buffer_sizeSize of the buffer
Returns
Length of the result
0 if the buffer_size doesn't fit the result

Definition at line 184 of file serialization.c.

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

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

Definition at line 63 of file serialization.c.

Variable Documentation

◆ kMaxEmbeddedInt

const uint64_t kMaxEmbeddedInt = 23

Largest integer that can be encoded as embedded in the item leading byte.

Definition at line 46 of file serialization.c.