cbor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Freie Universit├Ąt Berlin
3  * Copyright (C) 2014 Kevin Funk <kfunk@kde.org>
4  * Copyright (C) 2014 Jana Cavojska <jana.cavojska9@gmail.com>
5  *
6  * This file is subject to the terms and conditions of the GNU Lesser
7  * General Public License v2.1. See the file LICENSE in the top level
8  * directory for more details.
9  */
10 
120 #ifndef CBOR_H
121 #define CBOR_H
122 
123 #include <stdbool.h>
124 #include <stdint.h>
125 #include <stdlib.h>
126 
127 #ifdef MODULE_CBOR_CTIME
128 #include <time.h>
129 #endif /* MODULE_CBOR_CTIME */
130 
131 #ifdef __cplusplus
132 extern "C" {
133 #endif
134 
156 typedef struct {
157  unsigned char *data;
158  size_t size;
159  size_t pos;
160 } cbor_stream_t;
161 
170 void cbor_init(cbor_stream_t *stream, unsigned char *buffer, size_t size);
171 
179 void cbor_clear(cbor_stream_t *stream);
180 
188 void cbor_destroy(cbor_stream_t *stream);
189 
195 void cbor_stream_print(const cbor_stream_t *stream);
196 
215 void cbor_stream_decode(cbor_stream_t *stream);
216 
225 size_t cbor_serialize_int(cbor_stream_t *stream, int val);
226 
236 size_t cbor_deserialize_int(const cbor_stream_t *stream, size_t offset,
237  int *val);
238 
247 size_t cbor_serialize_uint64_t(cbor_stream_t *stream, uint64_t val);
248 
258 size_t cbor_deserialize_uint64_t(const cbor_stream_t *stream, size_t offset,
259  uint64_t *val);
260 
269 size_t cbor_serialize_int64_t(cbor_stream_t *stream, int64_t val);
270 
280 size_t cbor_deserialize_int64_t(const cbor_stream_t *stream, size_t offset,
281  int64_t *val);
282 
291 size_t cbor_serialize_bool(cbor_stream_t *stream, bool val);
292 
302 size_t cbor_deserialize_bool(const cbor_stream_t *stream, size_t offset,
303  bool *val);
304 
305 #ifdef MODULE_CBOR_FLOAT
306 
314 size_t cbor_serialize_float_half(cbor_stream_t *stream, float val);
315 
326 size_t cbor_deserialize_float_half(const cbor_stream_t *stream, size_t offset,
327  float *val);
336 size_t cbor_serialize_float(cbor_stream_t *stream, float val);
337 
347 size_t cbor_deserialize_float(const cbor_stream_t *stream, size_t offset,
348  float *val);
357 size_t cbor_serialize_double(cbor_stream_t *stream, double val);
358 
369 size_t cbor_deserialize_double(const cbor_stream_t *stream, size_t offset,
370  double *val);
371 #endif /* MODULE_CBOR_FLOAT */
372 
381 size_t cbor_serialize_byte_string(cbor_stream_t *stream, const char *val);
382 
392 size_t cbor_serialize_byte_stringl(cbor_stream_t *stream, const char *val, size_t length);
393 
404 size_t cbor_deserialize_byte_string(const cbor_stream_t *stream, size_t offset,
405  char *val, size_t length);
406 
416 size_t cbor_serialize_unicode_string(cbor_stream_t *stream, const char *val);
417 
428 size_t cbor_deserialize_byte_string_no_copy(const cbor_stream_t *stream, size_t offset,
429  unsigned char **val, size_t *length);
430 
441 size_t cbor_deserialize_unicode_string_no_copy(const cbor_stream_t *stream, size_t offset,
442  unsigned char **val, size_t *length);
443 
455  size_t offset, char *val, size_t length);
456 
475 size_t cbor_serialize_array(cbor_stream_t *stream, size_t array_length);
476 
496 size_t cbor_deserialize_array(const cbor_stream_t *stream, size_t offset,
497  size_t *array_length);
498 
507 
516 size_t cbor_deserialize_array_indefinite(const cbor_stream_t *stream, size_t offset);
517 
535 size_t cbor_serialize_map(cbor_stream_t *stream, size_t map_length);
536 
563 size_t cbor_deserialize_map(const cbor_stream_t *stream, size_t offset,
564  size_t *map_length);
565 
574 
583 size_t cbor_deserialize_map_indefinite(const cbor_stream_t *stream, size_t offset);
584 
585 #ifdef MODULE_CBOR_SEMANTIC_TAGGING
586 #ifdef MODULE_CBOR_CTIME
587 
608 size_t cbor_serialize_date_time(cbor_stream_t *stream, struct tm *val);
609 
625 size_t cbor_deserialize_date_time(const cbor_stream_t *stream, size_t offset, struct tm *val);
626 
635 size_t cbor_serialize_date_time_epoch(cbor_stream_t *stream, time_t val);
636 
646 size_t cbor_deserialize_date_time_epoch(const cbor_stream_t *stream, size_t offset, time_t *val);
647 
648 #endif /* MODULE_CBOR_CTIME */
649 
660 size_t cbor_write_tag(cbor_stream_t *stream, unsigned char tag);
661 
670 bool cbor_at_tag(const cbor_stream_t *stream, size_t offset);
671 
672 #endif /* MODULE_CBOR_SEMANTIC_TAGGING */
673 
683 size_t cbor_write_break(cbor_stream_t *stream);
684 
693 bool cbor_at_break(const cbor_stream_t *stream, size_t offset);
694 
705 bool cbor_at_end(const cbor_stream_t *stream, size_t offset);
706 
707 #ifdef __cplusplus
708 }
709 #endif
710 
711 #endif /* CBOR_H */
712 
size_t cbor_deserialize_int(const cbor_stream_t *stream, size_t offset, int *val)
Deserialize integers from stream to val.
size_t pos
Index to the next free byte.
Definition: cbor.h:159
size_t cbor_deserialize_bool(const cbor_stream_t *stream, size_t offset, bool *val)
Deserialize boolean values from stream to val.
size_t cbor_serialize_int(cbor_stream_t *stream, int val)
Serializes an integer.
size_t cbor_deserialize_array_indefinite(const cbor_stream_t *stream, size_t offset)
Deserialize array of items.
size_t cbor_deserialize_byte_string(const cbor_stream_t *stream, size_t offset, char *val, size_t length)
Deserialize bytes from stream to val.
size_t cbor_deserialize_uint64_t(const cbor_stream_t *stream, size_t offset, uint64_t *val)
Deserialize unsigned 64 bit values from stream to val.
size_t size
Size of the array.
Definition: cbor.h:158
void cbor_destroy(cbor_stream_t *stream)
Destroy the cbor struct.
size_t cbor_deserialize_unicode_string_no_copy(const cbor_stream_t *stream, size_t offset, unsigned char **val, size_t *length)
Deserialize unicode string from stream to val (without copy)
size_t cbor_serialize_bool(cbor_stream_t *stream, bool val)
Serializes a boolean value.
size_t cbor_serialize_byte_string(cbor_stream_t *stream, const char *val)
Serializes a signed 64 bit value.
size_t cbor_serialize_int64_t(cbor_stream_t *stream, int64_t val)
Serializes a signed 64 bit value.
unsigned char * data
Array containing CBOR encoded data.
Definition: cbor.h:157
bool cbor_at_end(const cbor_stream_t *stream, size_t offset)
Whether we are at the end of the stream stream at offset offset.
void cbor_clear(cbor_stream_t *stream)
Clear cbor struct.
size_t cbor_serialize_map(cbor_stream_t *stream, size_t map_length)
Serialize map of length map_length.
size_t cbor_serialize_array(cbor_stream_t *stream, size_t array_length)
Serialize array of length array_length.
size_t cbor_deserialize_array(const cbor_stream_t *stream, size_t offset, size_t *array_length)
Deserialize array of items.
size_t cbor_serialize_byte_stringl(cbor_stream_t *stream, const char *val, size_t length)
Serializes an arbitrary byte string.
void cbor_init(cbor_stream_t *stream, unsigned char *buffer, size_t size)
Initialize cbor struct.
void cbor_stream_print(const cbor_stream_t *stream)
Print stream in hex representation.
size_t cbor_write_break(cbor_stream_t *stream)
Write a break symbol at the current offset in stream stream.
size_t cbor_deserialize_unicode_string(const cbor_stream_t *stream, size_t offset, char *val, size_t length)
Deserialize unicode string from stream to val.
size_t cbor_serialize_uint64_t(cbor_stream_t *stream, uint64_t val)
Serializes an unsigned 64 bit value.
size_t cbor_serialize_array_indefinite(cbor_stream_t *stream)
Serialize array of infite length.
Datatype to represent time.
size_t cbor_deserialize_map_indefinite(const cbor_stream_t *stream, size_t offset)
Deserialize map of items.
size_t cbor_serialize_map_indefinite(cbor_stream_t *stream)
Serialize map of infite length.
bool cbor_at_break(const cbor_stream_t *stream, size_t offset)
Whether we are at a break symbol in stream stream at offset offset.
size_t cbor_deserialize_map(const cbor_stream_t *stream, size_t offset, size_t *map_length)
Deserialize map of items.
unsigned long time_t
Definition of time_t for the MSP430.
Definition: msp430_types.h:51
size_t cbor_serialize_unicode_string(cbor_stream_t *stream, const char *val)
Serializes a unicode string.
size_t cbor_deserialize_byte_string_no_copy(const cbor_stream_t *stream, size_t offset, unsigned char **val, size_t *length)
Deserialize bytes from stream to val (without copy)
size_t cbor_deserialize_int64_t(const cbor_stream_t *stream, size_t offset, int64_t *val)
Deserialize signed 64 bit values from stream to val.
Struct containing CBOR-encoded data.
Definition: cbor.h:156
stdlib.h wrapper for MSP430
void cbor_stream_decode(cbor_stream_t *stream)
Decode CBOR from stream.