TinyCBOR 0.5.2 API
Data Structures | Typedefs | Enumerations | Functions | Variables
cbor.h File Reference

The <cbor.h> is the main header in TinyCBOR and defines the constants used by most functions as well as the structures for encoding (CborEncoder) and decoding (CborValue). More...

#include <assert.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "tinycbor-version.h"
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  CborEncoder
 Structure used to encode to CBOR. More...
 
struct  CborValue
 This type contains one value parsed from the CBOR stream. More...
 

Typedefs

typedef uint64_t CborTag
 This typedef is an unsigned 64-bit integer. More...
 

Enumerations

enum  CborType
 The CborType enum contains the types known to TinyCBOR. More...
 
enum  CborKnownTags
 The CborKnownTags enum contains known tags specified in RFC 7049, for use by the application. More...
 
enum  CborError
 The CborError enum contains the possible error values used by the CBOR encoder and decoder. More...
 
enum  CborValidationFlags
 
The CborValidationFlags enum contains flags that control the validation of a CBOR stream. More...
 
enum  CborPrettyFlags
 The CborPrettyFlags enum contains flags that control the conversion of CBOR to text format. More...
 

Functions

CBOR_API const char * cbor_error_string (CborError error)
 Returns the error string corresponding to the CBOR error condition error.
 
CBOR_API void cbor_encoder_init (CborEncoder *encoder, uint8_t *buffer, size_t size, int flags)
 Initializes a CborEncoder structure encoder by pointing it to buffer buffer of size size. More...
 
CBOR_API CborError cbor_encode_uint (CborEncoder *encoder, uint64_t value)
 Appends the unsigned 64-bit integer value to the CBOR stream provided by encoder. More...
 
CBOR_API CborError cbor_encode_int (CborEncoder *encoder, int64_t value)
 Appends the signed 64-bit integer value to the CBOR stream provided by encoder. More...
 
CBOR_API CborError cbor_encode_negative_int (CborEncoder *encoder, uint64_t absolute_value)
 Appends the negative 64-bit integer whose absolute value is absolute_value to the CBOR stream provided by encoder. More...
 
CBOR_API CborError cbor_encode_simple_value (CborEncoder *encoder, uint8_t value)
 Appends the CBOR Simple Type of value value to the CBOR stream provided by encoder. More...
 
CBOR_API CborError cbor_encode_tag (CborEncoder *encoder, CborTag tag)
 Appends the CBOR tag tag to the CBOR stream provided by encoder. More...
 
CBOR_API CborError cbor_encode_text_string (CborEncoder *encoder, const char *string, size_t length)
 Appends the byte string string of length length to the CBOR stream provided by encoder. More...
 
CborError cbor_encode_text_stringz (CborEncoder *encoder, const char *string)
 Appends the null-terminated text string string to the CBOR stream provided by encoder. More...
 
CBOR_API CborError cbor_encode_byte_string (CborEncoder *encoder, const uint8_t *string, size_t length)
 Appends the text string string of length length to the CBOR stream provided by encoder. More...
 
CBOR_API CborError cbor_encode_floating_point (CborEncoder *encoder, CborType fpType, const void *value)
 Appends the floating-point value of type fpType and pointed to by value to the CBOR stream provided by encoder. More...
 
CborError cbor_encode_boolean (CborEncoder *encoder, bool value)
 Appends the boolean value value to the CBOR stream provided by encoder.
 
CborError cbor_encode_null (CborEncoder *encoder)
 Appends the CBOR type representing a null value to the CBOR stream provided by encoder. More...
 
CborError cbor_encode_undefined (CborEncoder *encoder)
 Appends the CBOR type representing an undefined value to the CBOR stream provided by encoder. More...
 
CborError cbor_encode_half_float (CborEncoder *encoder, const void *value)
 Appends the IEEE 754 half-precision (16-bit) floating point value pointed to by value to the CBOR stream provided by encoder. More...
 
CborError cbor_encode_float (CborEncoder *encoder, float value)
 Appends the IEEE 754 single-precision (32-bit) floating point value value to the CBOR stream provided by encoder. More...
 
CborError cbor_encode_double (CborEncoder *encoder, double value)
 Appends the IEEE 754 double-precision (64-bit) floating point value value to the CBOR stream provided by encoder. More...
 
CBOR_API CborError cbor_encoder_create_array (CborEncoder *encoder, CborEncoder *arrayEncoder, size_t length)
 Creates a CBOR array in the CBOR stream provided by encoder and initializes arrayEncoder so that items can be added to the array using the CborEncoder functions. More...
 
CBOR_API CborError cbor_encoder_create_map (CborEncoder *encoder, CborEncoder *mapEncoder, size_t length)
 Creates a CBOR map in the CBOR stream provided by encoder and initializes mapEncoder so that items can be added to the map using the CborEncoder functions. More...
 
CBOR_API CborError cbor_encoder_close_container (CborEncoder *encoder, const CborEncoder *containerEncoder)
 Closes the CBOR container (array or map) provided by containerEncoder and updates the CBOR stream provided by encoder. More...
 
CBOR_API CborError cbor_encoder_close_container_checked (CborEncoder *encoder, const CborEncoder *containerEncoder)
 
size_t cbor_encoder_get_buffer_size (const CborEncoder *encoder, const uint8_t *buffer)
 Returns the total size of the buffer starting at buffer after the encoding finished without errors. More...
 
size_t cbor_encoder_get_extra_bytes_needed (const CborEncoder *encoder)
 Returns how many more bytes the original buffer supplied to cbor_encoder_init() needs to be extended by so that no CborErrorOutOfMemory condition will happen for the encoding. More...
 
CBOR_API CborError cbor_parser_init (const uint8_t *buffer, size_t size, int flags, CborParser *parser, CborValue *it)
 Initializes the CBOR parser for parsing size bytes beginning at buffer. More...
 
CBOR_API CborError cbor_value_validate_basic (const CborValue *it)
 Performs a basic validation of the CBOR stream pointed by it and returns the error it found. More...
 
bool cbor_value_at_end (const CborValue *it)
 Returns true if it has reached the end of the iteration, usually when advancing after the last item in an array or map. More...
 
const uint8_t * cbor_value_get_next_byte (const CborValue *it)
 Returns a pointer to the next byte that would be decoded if this CborValue object were advanced. More...
 
CBOR_API CborError cbor_value_advance_fixed (CborValue *it)
 Advances the CBOR value it by one fixed-size position. More...
 
CBOR_API CborError cbor_value_advance (CborValue *it)
 Advances the CBOR value it by one element, skipping over containers. More...
 
bool cbor_value_is_container (const CborValue *it)
 Returns true if the it value is a container and requires recursion in order to decode (maps and arrays), false otherwise.
 
CBOR_API CborError cbor_value_enter_container (const CborValue *it, CborValue *recursed)
 Creates a CborValue iterator pointing to the first element of the container represented by it and saves it in recursed. More...
 
CBOR_API CborError cbor_value_leave_container (CborValue *it, const CborValue *recursed)
 Updates it to point to the next element after the container. More...
 
bool cbor_value_is_valid (const CborValue *value)
 Returns true if the iterator it contains a valid value. More...
 
CborType cbor_value_get_type (const CborValue *value)
 Returns the type of the CBOR value that the iterator value points to. More...
 
bool cbor_value_is_null (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR null type. More...
 
bool cbor_value_is_undefined (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR undefined type. More...
 
bool cbor_value_is_boolean (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR boolean type (true or false). More...
 
CborError cbor_value_get_boolean (const CborValue *value, bool *result)
 Retrieves the boolean value that value points to and stores it in result. More...
 
bool cbor_value_is_simple_type (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR Simple Type type (other than true, false, null and undefined). More...
 
CborError cbor_value_get_simple_type (const CborValue *value, uint8_t *result)
 Retrieves the CBOR Simple Type value that value points to and stores it in result. More...
 
bool cbor_value_is_integer (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR integer type. More...
 
bool cbor_value_is_unsigned_integer (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR unsigned integer type (positive values or zero). More...
 
bool cbor_value_is_negative_integer (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR negative integer type. More...
 
CborError cbor_value_get_raw_integer (const CborValue *value, uint64_t *result)
 Retrieves the CBOR integer value that value points to and stores it in result. More...
 
CborError cbor_value_get_uint64 (const CborValue *value, uint64_t *result)
 Retrieves the CBOR integer value that value points to and stores it in result. More...
 
CborError cbor_value_get_int64 (const CborValue *value, int64_t *result)
 Retrieves the CBOR integer value that value points to and stores it in result. More...
 
CborError cbor_value_get_int (const CborValue *value, int *result)
 Retrieves the CBOR integer value that value points to and stores it in result. More...
 
CBOR_API CborError cbor_value_get_int64_checked (const CborValue *value, int64_t *result)
 Retrieves the CBOR integer value that value points to and stores it in result. More...
 
CBOR_API CborError cbor_value_get_int_checked (const CborValue *value, int *result)
 Retrieves the CBOR integer value that value points to and stores it in result. More...
 
bool cbor_value_is_length_known (const CborValue *value)
 Returns true if the length of this type is known without calculation. More...
 
bool cbor_value_is_tag (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR tag. More...
 
CborError cbor_value_get_tag (const CborValue *value, CborTag *result)
 Retrieves the CBOR tag value that value points to and stores it in result. More...
 
CBOR_API CborError cbor_value_skip_tag (CborValue *it)
 Advances the CBOR value it until it no longer points to a tag. More...
 
bool cbor_value_is_byte_string (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR text string. More...
 
bool cbor_value_is_text_string (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR text string. More...
 
CborError cbor_value_get_string_length (const CborValue *value, size_t *length)
 Extracts the length of the byte or text string that value points to and stores it in result. More...
 
CBOR_API CborError cbor_value_calculate_string_length (const CborValue *value, size_t *length)
 Calculates the length of the byte or text string that value points to and stores it in len. More...
 
CborError cbor_value_copy_text_string (const CborValue *value, char *buffer, size_t *buflen, CborValue *next)
 Copies the string pointed to by value into the buffer provided at buffer of buflen bytes. More...
 
CborError cbor_value_copy_byte_string (const CborValue *value, uint8_t *buffer, size_t *buflen, CborValue *next)
 Copies the string pointed by value into the buffer provided at buffer of buflen bytes. More...
 
CborError cbor_value_dup_text_string (const CborValue *value, char **buffer, size_t *buflen, CborValue *next)
 Allocates memory for the string pointed by value and copies it into this buffer. More...
 
CborError cbor_value_dup_byte_string (const CborValue *value, uint8_t **buffer, size_t *buflen, CborValue *next)
 Allocates memory for the string pointed by value and copies it into this buffer. More...
 
CBOR_API CborError cbor_value_text_string_equals (const CborValue *value, const char *string, bool *result)
 Compares the entry value with the string string and stores the result in result. More...
 
bool cbor_value_is_array (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR array. More...
 
bool cbor_value_is_map (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR map. More...
 
CborError cbor_value_get_array_length (const CborValue *value, size_t *length)
 Extracts the length of the CBOR array that value points to and stores it in result. More...
 
CborError cbor_value_get_map_length (const CborValue *value, size_t *length)
 Extracts the length of the CBOR map that value points to and stores it in result. More...
 
CBOR_API CborError cbor_value_map_find_value (const CborValue *map, const char *string, CborValue *element)
 Attempts to find the value in map map that corresponds to the text string entry string. More...
 
bool cbor_value_is_half_float (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR single-precision floating point (16-bit). More...
 
CBOR_API CborError cbor_value_get_half_float (const CborValue *value, void *result)
 Retrieves the CBOR half-precision floating point (16-bit) value that value points to and stores it in result. More...
 
bool cbor_value_is_float (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR single-precision floating point (32-bit). More...
 
CborError cbor_value_get_float (const CborValue *value, float *result)
 Retrieves the CBOR single-precision floating point (32-bit) value that value points to and stores it in result. More...
 
bool cbor_value_is_double (const CborValue *value)
 Returns true if the iterator value is valid and points to a CBOR double-precision floating point (64-bit). More...
 
CBOR_API CborError cbor_value_validate (const CborValue *it, int flags)
 Performs a full validation, controlled by the flags options, of the CBOR stream pointed by it and returns the error it found. More...
 
CBOR_API CborError cbor_value_to_pretty_stream (CborStreamFunction streamFunction, void *token, CborValue *value, int flags)
 Converts the current CBOR type pointed by value to its textual representation and writes it to the stream by calling the streamFunction. More...
 
CBOR_API CborError cbor_value_to_pretty_advance_flags (FILE *out, CborValue *value, int flags)
 Converts the current CBOR type pointed to by value to its textual representation and writes it to the out stream. More...
 
CBOR_API CborError cbor_value_to_pretty_advance (FILE *out, CborValue *value)
 Converts the current CBOR type pointed to by value to its textual representation and writes it to the out stream. More...
 
CborError cbor_value_to_pretty (FILE *out, const CborValue *value)
 Converts the current CBOR type pointed to by value to its textual representation and writes it to the out stream. More...
 

Variables

static const size_t CborIndefiniteLength = ((size_t)-1)
 This variable is a constant used to indicate that the length of the map or array is not yet determined. More...
 

Detailed Description

The <cbor.h> is the main header in TinyCBOR and defines the constants used by most functions as well as the structures for encoding (CborEncoder) and decoding (CborValue).

See also
<cborjson.h>

Function Documentation

◆ cbor_value_dup_byte_string()

CborError cbor_value_dup_byte_string ( const CborValue value,
uint8_t **  buffer,
size_t *  buflen,
CborValue next 
)

Allocates memory for the string pointed by value and copies it into this buffer.

The pointer to the buffer is stored in buffer and the number of bytes copied is stored in buflen (those variables must not be NULL).

If the iterator value does not point to a byte string, the behaviour is undefined, so checking with cbor_value_get_type or cbor_value_is_byte_string is recommended.

If malloc returns a NULL pointer, this function will return error condition CborErrorOutOfMemory.

On success, {*buffer} will contain a valid pointer that must be freed by calling {free()}. This is the case even for zero-length strings.

The next pointer, if not null, will be updated to point to the next item after this string. If value points to the last item, then next will be invalid.

This function may not run in constant time (it will run in O(n) time on the number of chunks). It requires constant memory (O(1)) in addition to the malloc'ed block.

See also
cbor_value_get_text_string_chunk(), cbor_value_copy_byte_string(), cbor_value_dup_text_string()

◆ cbor_value_dup_text_string()

CborError cbor_value_dup_text_string ( const CborValue value,
char **  buffer,
size_t *  buflen,
CborValue next 
)

Allocates memory for the string pointed by value and copies it into this buffer.

The pointer to the buffer is stored in buffer and the number of bytes copied is stored in buflen (those variables must not be NULL).

If the iterator value does not point to a text string, the behaviour is undefined, so checking with cbor_value_get_type or cbor_value_is_text_string is recommended.

If malloc returns a NULL pointer, this function will return error condition CborErrorOutOfMemory.

On success, {*buffer} will contain a valid pointer that must be freed by calling {free()}. This is the case even for zero-length strings.

The next pointer, if not null, will be updated to point to the next item after this string. If value points to the last item, then next will be invalid.

This function may not run in constant time (it will run in O(n) time on the number of chunks). It requires constant memory (O(1)) in addition to the malloc'ed block.

Note
This function does not perform UTF-8 validation on the incoming text string.
See also
cbor_value_get_text_string_chunk(), cbor_value_copy_text_string(), cbor_value_dup_byte_string()

◆ cbor_value_to_pretty()

CborError cbor_value_to_pretty ( FILE *  out,
const CborValue value 
)

Converts the current CBOR type pointed to by value to its textual representation and writes it to the out stream.

If an error occurs, this function returns an error code similar to CborParsing.

See also
cbor_value_to_pretty_advance(), cbor_value_to_json_advance()

◆ cbor_value_to_pretty_advance()

CBOR_API CborError cbor_value_to_pretty_advance ( FILE *  out,
CborValue value 
)

Converts the current CBOR type pointed to by value to its textual representation and writes it to the out stream.

If an error occurs, this function returns an error code similar to CborParsing.

If no error ocurred, this function advances value to the next element. Often, concatenating the text representation of multiple elements can be done by appending a comma to the output stream in between calls to this function.

See also
cbor_value_to_pretty(), cbor_value_to_pretty_stream(), cbor_value_to_json_advance()

◆ cbor_value_to_pretty_advance_flags()

CBOR_API CborError cbor_value_to_pretty_advance_flags ( FILE *  out,
CborValue value,
int  flags 
)

Converts the current CBOR type pointed to by value to its textual representation and writes it to the out stream.

If an error occurs, this function returns an error code similar to CborParsing.

The textual representation can be controlled by the flags parameter (see CborPrettyFlags for more information).

If no error ocurred, this function advances value to the next element. Often, concatenating the text representation of multiple elements can be done by appending a comma to the output stream in between calls to this function.

See also
cbor_value_to_pretty_stream(), cbor_value_to_pretty(), cbor_value_to_json_advance()

Referenced by cbor_value_to_pretty().