Contiki-NG
Loading...
Searching...
No Matches

Functions for reading and writing CBOR. More...

Files

file  os/lib/cbor.c
 CBOR implementation.
file  os/lib/cbor.h
 CBOR API.

Data Structures

struct  cbor_nesting_record_t
 Structure of a nesting record. More...
struct  cbor_writer_state_t
 Structure of the internal state of a CBOR writer. More...
struct  cbor_reader_state_t
 Structure of the internal state of a CBOR reader. More...

Macros

#define CBOR_MAX_NESTING   (8)
 Defines how many arrays and maps can be open simultaneously while writing.

Typedefs

typedef enum cbor_major_type_t cbor_major_type_t
 Enumeration of major types.
typedef enum cbor_simple_value_t cbor_simple_value_t
 Enumeration of simple values.
typedef enum cbor_size_t cbor_size_t
 Enumeration of size information in various major types.
typedef struct cbor_nesting_record_t cbor_nesting_record_t
 Structure of a nesting record.
typedef struct cbor_writer_state_t cbor_writer_state_t
 Structure of the internal state of a CBOR writer.
typedef struct cbor_reader_state_t cbor_reader_state_t
 Structure of the internal state of a CBOR reader.

Enumerations

enum  cbor_major_type_t
 Enumeration of major types.
enum  cbor_simple_value_t
 Enumeration of simple values.
enum  cbor_size_t {
  CBOR_SIZE_NONE = -1 , CBOR_SIZE_1 = 0x18 , CBOR_SIZE_2 = 0x19 , CBOR_SIZE_4 = 0x1A ,
  CBOR_SIZE_8 = 0x1B
}
 Enumeration of size information in various major types. More...

Functions

void cbor_init_writer (cbor_writer_state_t *state, uint8_t *buffer, size_t buffer_size)
 Prepares for writing CBOR output.
size_t cbor_end_writer (cbor_writer_state_t *state)
 Finishes writing CBOR output.
void cbor_break_writer (cbor_writer_state_t *state)
 Marks the CBOR output as erroneous.
void cbor_write_object (cbor_writer_state_t *state, const void *object, size_t object_size)
 Appends an arbitrary CBOR object.
void cbor_write_unsigned (cbor_writer_state_t *state, uint64_t value)
 Appends an unsigned integer.
void cbor_write_signed (cbor_writer_state_t *state, int64_t value)
 Appends a signed integer.
void cbor_write_data (cbor_writer_state_t *state, const uint8_t *data, size_t data_size)
 Appends a byte string.
void cbor_write_text (cbor_writer_state_t *state, const char *text, size_t text_size)
 Appends a text string.
void cbor_write_null (cbor_writer_state_t *state)
 Appends the simple value null.
void cbor_write_undefined (cbor_writer_state_t *state)
 Appends the simple value undefined.
void cbor_write_bool (cbor_writer_state_t *state, bool boolean)
 Appends a boolean simple value.
void cbor_open_data (cbor_writer_state_t *state)
 Encloses subsequent CBOR objects in a byte string.
void cbor_close_data (cbor_writer_state_t *state)
 Stops enclosing subsequent CBOR objects in the innermost byte string.
void cbor_open_array (cbor_writer_state_t *state)
 Adds subsequent CBOR objects to an array.
void cbor_close_array (cbor_writer_state_t *state)
 Stops adding subsequent CBOR objects to the innermost array.
void cbor_open_map (cbor_writer_state_t *state)
 Adds subsequent entries to a map.
void cbor_close_map (cbor_writer_state_t *state)
 Stops adding subsequent entries to the innermost map.
void cbor_init_reader (cbor_reader_state_t *state, const uint8_t *cbor, size_t cbor_size)
 Prepares for reading CBOR input.
cbor_major_type_t cbor_peek_next (cbor_reader_state_t *state)
 Inspects the next major type.
bool cbor_end_reader (cbor_reader_state_t *state)
 Ensures that no bytes remain unread.
cbor_size_t cbor_read_unsigned (cbor_reader_state_t *state, uint64_t *value)
 Reads an unsigned integer.
cbor_size_t cbor_read_signed (cbor_reader_state_t *state, int64_t *value)
 Reads a signed integer.
const uint8_t * cbor_read_data (cbor_reader_state_t *state, size_t *data_size)
 Reads a byte string.
const char * cbor_read_text (cbor_reader_state_t *state, size_t *text_size)
 Reads a text string.
cbor_simple_value_t cbor_read_simple (cbor_reader_state_t *state)
 Reads a simple value.
size_t cbor_read_array (cbor_reader_state_t *state)
 Reads the number of elements of an array.
size_t cbor_read_map (cbor_reader_state_t *state)
 Reads the number of entries of a map.
const uint8_t * cbor_get_position (cbor_reader_state_t *state)
 Gets the current position in the CBOR buffer.
size_t cbor_get_remaining (cbor_reader_state_t *state)
 Gets the remaining bytes in the CBOR buffer.
bool cbor_skip_next (cbor_reader_state_t *state)
 Skips the next CBOR object in the buffer.

Detailed Description

Functions for reading and writing CBOR.

Enumeration Type Documentation

◆ cbor_size_t

Enumeration of size information in various major types.

Enumerator
CBOR_SIZE_NONE 

error condition

CBOR_SIZE_1 

1 byte

CBOR_SIZE_2 

2 bytes

CBOR_SIZE_4 

4 bytes

CBOR_SIZE_8 

8 bytes

Definition at line 100 of file cbor.h.

Function Documentation

◆ cbor_break_writer()

void cbor_break_writer ( cbor_writer_state_t * state)

Marks the CBOR output as erroneous.

Parameters
stateState of the CBOR writer.

Definition at line 62 of file cbor.c.

Referenced by cbor_close_array(), cbor_close_data(), and cbor_close_map().

◆ cbor_close_array()

void cbor_close_array ( cbor_writer_state_t * state)

Stops adding subsequent CBOR objects to the innermost array.

Parameters
stateState of the CBOR writer.

Definition at line 266 of file cbor.c.

References cbor_break_writer(), and CBOR_MAX_NESTING.

◆ cbor_close_data()

void cbor_close_data ( cbor_writer_state_t * state)

Stops enclosing subsequent CBOR objects in the innermost byte string.

Parameters
stateState of the CBOR writer.

Definition at line 249 of file cbor.c.

References cbor_break_writer(), and CBOR_MAX_NESTING.

Referenced by edhoc_generate_message_2().

◆ cbor_close_map()

void cbor_close_map ( cbor_writer_state_t * state)

Stops adding subsequent entries to the innermost map.

Parameters
stateState of the CBOR writer.

Definition at line 282 of file cbor.c.

References cbor_break_writer(), and CBOR_MAX_NESTING.

Referenced by edhoc_generate_cred_x(), and edhoc_generate_id_cred_x().

◆ cbor_end_reader()

bool cbor_end_reader ( cbor_reader_state_t * state)

Ensures that no bytes remain unread.

Parameters
stateState of the CBOR reader.
Returns
true if no unread bytes remain, or false otherwise.

Definition at line 310 of file cbor.c.

◆ cbor_end_writer()

size_t cbor_end_writer ( cbor_writer_state_t * state)

◆ cbor_get_position()

const uint8_t * cbor_get_position ( cbor_reader_state_t * state)

Gets the current position in the CBOR buffer.

Parameters
stateState of the CBOR reader.
Returns
Current position pointer or NULL on error.

Definition at line 469 of file cbor.c.

Referenced by edhoc_authenticate_msg(), edhoc_get_key_id_cred_x(), and edhoc_read_byte_identifier().

◆ cbor_get_remaining()

size_t cbor_get_remaining ( cbor_reader_state_t * state)

Gets the remaining bytes in the CBOR buffer.

Parameters
stateState of the CBOR reader.
Returns
Number of remaining bytes.

Definition at line 475 of file cbor.c.

Referenced by edhoc_authenticate_msg(), edhoc_deserialize_err(), and edhoc_deserialize_msg_1().

◆ cbor_init_reader()

void cbor_init_reader ( cbor_reader_state_t * state,
const uint8_t * cbor,
size_t cbor_size )

Prepares for reading CBOR input.

Parameters
stateState of the CBOR reader.
cborBuffer that holds the CBOR input.
cbor_sizeSize of cbor in bytes.

Definition at line 293 of file cbor.c.

Referenced by edhoc_authenticate_msg(), edhoc_deserialize_err(), edhoc_deserialize_msg_1(), edhoc_deserialize_msg_2(), edhoc_deserialize_msg_3(), and edhoc_handler_msg_2().

◆ cbor_init_writer()

void cbor_init_writer ( cbor_writer_state_t * state,
uint8_t * buffer,
size_t buffer_size )

Prepares for writing CBOR output.

Parameters
stateState of the CBOR writer.
bufferBuffer for holding CBOR output.
buffer_sizeSize of buffer in bytes.

Definition at line 44 of file cbor.c.

References CBOR_MAX_NESTING.

Referenced by edhoc_authenticate_msg(), edhoc_calc_mac(), edhoc_generate_cred_x(), edhoc_generate_id_cred_x(), edhoc_generate_message_2(), edhoc_generate_message_3(), edhoc_generate_transcript_hash_2(), edhoc_generate_transcript_hash_3(), edhoc_generate_transcript_hash_4(), edhoc_serialize_err(), and edhoc_serialize_msg_1().

◆ cbor_open_array()

void cbor_open_array ( cbor_writer_state_t * state)

Adds subsequent CBOR objects to an array.

Parameters
stateState of the CBOR writer.

Definition at line 260 of file cbor.c.

◆ cbor_open_data()

void cbor_open_data ( cbor_writer_state_t * state)

Encloses subsequent CBOR objects in a byte string.

Parameters
stateState of the CBOR writer.

Definition at line 243 of file cbor.c.

Referenced by edhoc_generate_message_2().

◆ cbor_open_map()

void cbor_open_map ( cbor_writer_state_t * state)

Adds subsequent entries to a map.

Parameters
stateState of the CBOR writer.

Definition at line 276 of file cbor.c.

Referenced by edhoc_generate_cred_x(), and edhoc_generate_id_cred_x().

◆ cbor_peek_next()

cbor_major_type_t cbor_peek_next ( cbor_reader_state_t * state)

Inspects the next major type.

Parameters
stateState of the CBOR reader.
Returns
The next major type (could be CBOR_MAJOR_TYPE_NONE).

Definition at line 301 of file cbor.c.

Referenced by cbor_read_array(), cbor_read_data(), cbor_read_map(), cbor_read_signed(), cbor_read_text(), cbor_read_unsigned(), cbor_skip_next(), and edhoc_read_byte_identifier().

◆ cbor_read_array()

size_t cbor_read_array ( cbor_reader_state_t * state)

Reads the number of elements of an array.

Parameters
stateState of the CBOR reader.
Returns
Number of elements or SIZE_MAX on error.

Definition at line 451 of file cbor.c.

References cbor_peek_next().

◆ cbor_read_data()

const uint8_t * cbor_read_data ( cbor_reader_state_t * state,
size_t * data_size )

Reads a byte string.

Parameters
stateState of the CBOR reader.
data_sizeSize of the byte string in bytes.
Returns
First byte of the byte string or NULL on error.

Definition at line 411 of file cbor.c.

References cbor_peek_next().

Referenced by edhoc_deserialize_msg_1(), edhoc_deserialize_msg_2(), edhoc_deserialize_msg_3(), edhoc_get_ad(), edhoc_get_sign(), and edhoc_read_byte_identifier().

◆ cbor_read_map()

size_t cbor_read_map ( cbor_reader_state_t * state)

Reads the number of entries of a map.

Parameters
stateState of the CBOR reader.
Returns
Number of entries or SIZE_MAX on error.

Definition at line 460 of file cbor.c.

References cbor_peek_next().

◆ cbor_read_signed()

cbor_size_t cbor_read_signed ( cbor_reader_state_t * state,
int64_t * value )

Reads a signed integer.

Parameters
stateState of the CBOR reader.
valueBuffer to store the signed integer.
Returns
Size of the signed integer or CBOR_SIZE_NONE on error.

Definition at line 373 of file cbor.c.

References cbor_peek_next(), CBOR_SIZE_NONE, and value().

Referenced by edhoc_deserialize_err(), edhoc_deserialize_msg_1(), and edhoc_read_byte_identifier().

◆ cbor_read_simple()

cbor_simple_value_t cbor_read_simple ( cbor_reader_state_t * state)

Reads a simple value.

Parameters
stateState of the CBOR reader.
Returns
The simple value or CBOR_SIMPLE_VALUE_NONE on error.

Definition at line 429 of file cbor.c.

Referenced by edhoc_deserialize_err().

◆ cbor_read_text()

const char * cbor_read_text ( cbor_reader_state_t * state,
size_t * text_size )

Reads a text string.

Parameters
stateState of the CBOR reader.
text_sizeNumber of characters in the text string.
Returns
First character of the text string or NULL on error.

Definition at line 420 of file cbor.c.

References cbor_peek_next().

Referenced by edhoc_deserialize_err().

◆ cbor_read_unsigned()

cbor_size_t cbor_read_unsigned ( cbor_reader_state_t * state,
uint64_t * value )

Reads an unsigned integer.

Parameters
stateState of the CBOR reader.
valueBuffer to store the unsigned integer.
Returns
Size of the unsigned integer or CBOR_SIZE_NONE on error.

Definition at line 364 of file cbor.c.

References cbor_peek_next(), CBOR_SIZE_NONE, and value().

Referenced by edhoc_deserialize_msg_1().

◆ cbor_skip_next()

bool cbor_skip_next ( cbor_reader_state_t * state)

Skips the next CBOR object in the buffer.

This function advances the reader state past the next complete CBOR object, regardless of its type. It can handle all CBOR major types including:

  • Unsigned and signed integers
  • Byte strings and text strings
  • Arrays (recursively skips all elements)
  • Maps (recursively skips all key-value pairs)
  • Simple values (null, undefined, booleans, etc.)
Parameters
stateState of the CBOR reader.
Returns
true if an object was successfully skipped, or false on error.

Definition at line 481 of file cbor.c.

References cbor_peek_next(), CBOR_SIZE_NONE, and value().

Referenced by edhoc_deserialize_err().

◆ cbor_write_bool()

void cbor_write_bool ( cbor_writer_state_t * state,
bool boolean )

Appends a boolean simple value.

Parameters
stateState of the CBOR writer.
booleanThe boolean simple value to append.

Definition at line 217 of file cbor.c.

Referenced by edhoc_serialize_err().

◆ cbor_write_data()

void cbor_write_data ( cbor_writer_state_t * state,
const uint8_t * data,
size_t data_size )

◆ cbor_write_null()

void cbor_write_null ( cbor_writer_state_t * state)

Appends the simple value null.

Parameters
stateState of the CBOR writer.

Definition at line 205 of file cbor.c.

◆ cbor_write_object()

void cbor_write_object ( cbor_writer_state_t * state,
const void * object,
size_t object_size )

Appends an arbitrary CBOR object.

Parameters
stateState of the CBOR writer.
objectLocation of the CBOR object.
object_sizeSize of object in bytes.

Definition at line 106 of file cbor.c.

Referenced by edhoc_authenticate_msg(), edhoc_calc_mac(), edhoc_generate_message_2(), edhoc_generate_message_3(), edhoc_generate_transcript_hash_3(), edhoc_generate_transcript_hash_4(), and edhoc_write_byte_identifier().

◆ cbor_write_signed()

void cbor_write_signed ( cbor_writer_state_t * state,
int64_t value )

Appends a signed integer.

Parameters
stateState of the CBOR writer.
valueSigned integer.

Definition at line 176 of file cbor.c.

References cbor_write_unsigned(), and value().

Referenced by edhoc_generate_cred_x().

◆ cbor_write_text()

void cbor_write_text ( cbor_writer_state_t * state,
const char * text,
size_t text_size )

Appends a text string.

Parameters
stateState of the CBOR writer.
textThe text string.
text_sizeNumber of characters in text.

Definition at line 196 of file cbor.c.

Referenced by edhoc_generate_cred_x(), and edhoc_serialize_err().

◆ cbor_write_undefined()

void cbor_write_undefined ( cbor_writer_state_t * state)

Appends the simple value undefined.

Parameters
stateState of the CBOR writer.

Definition at line 211 of file cbor.c.

◆ cbor_write_unsigned()

void cbor_write_unsigned ( cbor_writer_state_t * state,
uint64_t value )

Appends an unsigned integer.

Parameters
stateState of the CBOR writer.
valueUnsigned integer.

Definition at line 169 of file cbor.c.

References value().

Referenced by cbor_write_signed(), edhoc_generate_cred_x(), edhoc_generate_id_cred_x(), edhoc_serialize_err(), and edhoc_serialize_msg_1().