45 uint8_t *buffer,
size_t buffer_size)
47 state->buffer_head = buffer;
48 state->buffer = buffer;
49 state->buffer_size = buffer_size;
57 ? (size_t)(state->buffer - state->buffer_head)
65 state->buffer_size = 0;
74 state->records[state->nesting_depth].objects++;
80 if(!state->buffer_size) {
84 *state->buffer++ = value;
91 const void *
object,
size_t object_size)
96 if(state->buffer_size < object_size) {
100 memcpy(state->buffer,
object, object_size);
101 state->buffer += object_size;
102 state->buffer_size -= object_size;
107 const void *
object,
size_t object_size)
112 write_object(state,
object, object_size);
118 uint8_t *
const destination,
121 size_t length_to_copy;
129 destination[-1] |= value;
132 if(value <= UINT8_MAX) {
135 }
else if(value <= UINT16_MAX) {
138 }
else if(value <= UINT32_MAX) {
147 if(state->buffer_size < length_to_copy) {
153 memmove(destination + length_to_copy,
155 state->buffer - destination);
158 state->buffer += length_to_copy;
159 state->buffer_size -= length_to_copy;
162 while(length_to_copy--) {
163 destination[length_to_copy] = value;
171 write_first_byte(state, CBOR_MAJOR_TYPE_UNSIGNED);
172 insert_unsigned(state, state->buffer, value);
181 write_first_byte(state, CBOR_MAJOR_TYPE_SIGNED);
182 insert_unsigned(state, state->buffer, (uint64_t)(-1 - value));
188 const uint8_t *data,
size_t data_size)
190 write_first_byte(state, CBOR_MAJOR_TYPE_BYTE_STRING);
191 insert_unsigned(state, state->buffer, data_size);
192 write_object(state, data, data_size);
197 const char *text,
size_t text_size)
199 write_first_byte(state, CBOR_MAJOR_TYPE_TEXT_STRING);
200 insert_unsigned(state, state->buffer, text_size);
201 write_object(state, text, text_size);
207 write_first_byte(state, CBOR_SIMPLE_VALUE_NULL);
213 write_first_byte(state, CBOR_SIMPLE_VALUE_UNDEFINED);
219 write_first_byte(state,
220 boolean ? CBOR_SIMPLE_VALUE_TRUE : CBOR_SIMPLE_VALUE_FALSE);
226 if(!state->nesting_depth) {
230 write_first_byte(state, major_type);
231 state->records[--state->nesting_depth].start = state->buffer;
232 state->records[state->nesting_depth].objects = 0;
238 insert_unsigned(state, state->records[state->nesting_depth].start, value);
239 state->nesting_depth++;
245 generic_open(state, CBOR_MAJOR_TYPE_BYTE_STRING);
256 state->buffer - state->records[state->nesting_depth].start);
262 generic_open(state, CBOR_MAJOR_TYPE_ARRAY);
272 generic_close(state, state->records[state->nesting_depth].objects);
278 generic_open(state, CBOR_MAJOR_TYPE_MAP);
285 || (state->records[state->nesting_depth].objects & 1)) {
289 generic_close(state, state->records[state->nesting_depth].objects >> 1);
294 const uint8_t *cbor,
size_t cbor_size)
297 state->cbor_size = cbor_size;
303 if(!state->cbor_size) {
304 return CBOR_MAJOR_TYPE_NONE;
306 return *state->cbor & 0xE0;
312 return state->cbor_size == 0;
318 size_t bytes_to_read;
320 if(!state->cbor_size) {
350 if(bytes_to_read > state->cbor_size) {
353 state->cbor_size -= bytes_to_read;
356 while(bytes_to_read--) {
358 *value += *state->cbor++;
366 uint64_t unsigned_value;
374 case CBOR_MAJOR_TYPE_UNSIGNED:
375 *value = (int64_t)unsigned_value;
377 case CBOR_MAJOR_TYPE_SIGNED:
378 *value = -(int64_t)(unsigned_value + 1);
385static const uint8_t *
391 || (state->cbor_size < value)) {
395 const uint8_t *beginning = state->cbor;
396 state->cbor += *size;
397 state->cbor_size -= *size;
407 return read_byte_or_text_string(state, data_size);
416 return (
const char *)read_byte_or_text_string(state, text_size);
422 if(!state->cbor_size) {
423 return CBOR_SIMPLE_VALUE_NONE;
426 return *state->cbor++;
435 || (value >= SIZE_MAX)) {
447 return read_array_or_map(state);
456 return read_array_or_map(state);
size_t cbor_end_writer(cbor_writer_state_t *state)
Finishes writing CBOR output.
void cbor_close_data(cbor_writer_state_t *state)
Stops enclosing subsequent CBOR objects in the innermost byte string.
void cbor_open_map(cbor_writer_state_t *state)
Adds subsequent entries to a map.
cbor_size_t cbor_read_signed(cbor_reader_state_t *state, int64_t *value)
Reads a signed integer.
void cbor_break_writer(cbor_writer_state_t *state)
Marks the CBOR output as erroneous.
size_t cbor_read_array(cbor_reader_state_t *state)
Reads the number of elements of an array.
cbor_major_type_t
Enumeration of major types.
bool cbor_end_reader(cbor_reader_state_t *state)
Ensures that no bytes remain unread.
cbor_simple_value_t
Enumeration of simple values.
cbor_size_t cbor_read_unsigned(cbor_reader_state_t *state, uint64_t *value)
Reads an unsigned integer.
cbor_simple_value_t cbor_read_simple(cbor_reader_state_t *state)
Reads a simple value.
void cbor_write_text(cbor_writer_state_t *state, const char *text, size_t text_size)
Appends a text string.
void cbor_write_unsigned(cbor_writer_state_t *state, uint64_t value)
Appends an unsigned integer.
cbor_major_type_t cbor_peek_next(cbor_reader_state_t *state)
Inspects the next major type.
void cbor_write_null(cbor_writer_state_t *state)
Appends the simple value null.
void cbor_open_data(cbor_writer_state_t *state)
Encloses subsequent CBOR objects in a byte string.
void cbor_write_bool(cbor_writer_state_t *state, bool boolean)
Appends a boolean simple value.
void cbor_open_array(cbor_writer_state_t *state)
Adds subsequent CBOR objects to an array.
const uint8_t * cbor_read_data(cbor_reader_state_t *state, size_t *data_size)
Reads a byte string.
void cbor_close_map(cbor_writer_state_t *state)
Stops adding subsequent entries to the innermost map.
void cbor_write_object(cbor_writer_state_t *state, const void *object, size_t object_size)
Appends an arbitrary CBOR object.
#define CBOR_MAX_NESTING
Defines how many arrays and maps can be open simultaneously while writing.
void cbor_init_writer(cbor_writer_state_t *state, uint8_t *buffer, size_t buffer_size)
Prepares for writing CBOR output.
cbor_size_t
Enumeration of size information in various major types.
void cbor_close_array(cbor_writer_state_t *state)
Stops adding subsequent CBOR objects to the innermost array.
void cbor_init_reader(cbor_reader_state_t *state, const uint8_t *cbor, size_t cbor_size)
Prepares for reading CBOR input.
void cbor_write_data(cbor_writer_state_t *state, const uint8_t *data, size_t data_size)
Appends a byte string.
void cbor_write_signed(cbor_writer_state_t *state, int64_t value)
Appends a signed integer.
size_t cbor_read_map(cbor_reader_state_t *state)
Reads the number of entries of a map.
void cbor_write_undefined(cbor_writer_state_t *state)
Appends the simple value undefined.
const char * cbor_read_text(cbor_reader_state_t *state, size_t *text_size)
Reads a text string.
@ CBOR_SIZE_NONE
error condition
Structure of the internal state of a CBOR reader.
Structure of the internal state of a CBOR writer.