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 
116 #ifndef CBOR_H
117 #define CBOR_H
118 
119 #include <stdbool.h>
120 #include <stdint.h>
121 #include <stdlib.h>
122 
123 #ifndef CBOR_NO_CTIME
124 #include <time.h>
125 #endif /* CBOR_NO_CTIME */
126 
127 #ifdef __cplusplus
128 extern "C" {
129 #endif
130 
152 typedef struct {
153  unsigned char *data;
154  size_t size;
155  size_t pos;
156 } cbor_stream_t;
157 
166 void cbor_init(cbor_stream_t *stream, unsigned char *buffer, size_t size);
167 
175 void cbor_clear(cbor_stream_t *stream);
176 
184 void cbor_destroy(cbor_stream_t *stream);
185 
186 #ifndef CBOR_NO_PRINT
187 
192 void cbor_stream_print(const cbor_stream_t *stream);
193 
212 void cbor_stream_decode(cbor_stream_t *stream);
213 #endif /* CBOR_NO_PRINT */
214 
223 size_t cbor_serialize_int(cbor_stream_t *stream, int val);
224 
234 size_t cbor_deserialize_int(const cbor_stream_t *stream, size_t offset,
235  int *val);
236 
245 size_t cbor_serialize_uint64_t(cbor_stream_t *stream, uint64_t val);
246 
256 size_t cbor_deserialize_uint64_t(const cbor_stream_t *stream, size_t offset,
257  uint64_t *val);
258 
267 size_t cbor_serialize_int64_t(cbor_stream_t *stream, int64_t val);
268 
278 size_t cbor_deserialize_int64_t(const cbor_stream_t *stream, size_t offset,
279  int64_t *val);
280 
289 size_t cbor_serialize_bool(cbor_stream_t *stream, bool val);
290 
300 size_t cbor_deserialize_bool(const cbor_stream_t *stream, size_t offset,
301  bool *val);
302 
303 #ifndef CBOR_NO_FLOAT
304 
312 size_t cbor_serialize_float_half(cbor_stream_t *stream, float val);
313 
324 size_t cbor_deserialize_float_half(const cbor_stream_t *stream, size_t offset,
325  float *val);
334 size_t cbor_serialize_float(cbor_stream_t *stream, float val);
335 
345 size_t cbor_deserialize_float(const cbor_stream_t *stream, size_t offset,
346  float *val);
355 size_t cbor_serialize_double(cbor_stream_t *stream, double val);
356 
367 size_t cbor_deserialize_double(const cbor_stream_t *stream, size_t offset,
368  double *val);
369 #endif /* CBOR_NO_FLOAT */
370 
379 size_t cbor_serialize_byte_string(cbor_stream_t *stream, const char *val);
380 
390 size_t cbor_serialize_byte_stringl(cbor_stream_t *stream, const char *val, size_t length);
391 
402 size_t cbor_deserialize_byte_string(const cbor_stream_t *stream, size_t offset,
403  char *val, size_t length);
404 
414 size_t cbor_serialize_unicode_string(cbor_stream_t *stream, const char *val);
415 
426 size_t cbor_deserialize_byte_string_no_copy(const cbor_stream_t *stream, size_t offset,
427  unsigned char **val, size_t *length);
428 
439 size_t cbor_deserialize_unicode_string_no_copy(const cbor_stream_t *stream, size_t offset,
440  unsigned char **val, size_t *length);
441 
453  size_t offset, char *val, size_t length);
454 
473 size_t cbor_serialize_array(cbor_stream_t *stream, size_t array_length);
474 
494 size_t cbor_deserialize_array(const cbor_stream_t *stream, size_t offset,
495  size_t *array_length);
496 
505 
514 size_t cbor_deserialize_array_indefinite(const cbor_stream_t *stream, size_t offset);
515 
533 size_t cbor_serialize_map(cbor_stream_t *stream, size_t map_length);
534 
561 size_t cbor_deserialize_map(const cbor_stream_t *stream, size_t offset,
562  size_t *map_length);
563 
572 
581 size_t cbor_deserialize_map_indefinite(const cbor_stream_t *stream, size_t offset);
582 
583 #ifndef CBOR_NO_SEMANTIC_TAGGING
584 #ifndef CBOR_NO_CTIME
585 
606 size_t cbor_serialize_date_time(cbor_stream_t *stream, struct tm *val);
607 
623 size_t cbor_deserialize_date_time(const cbor_stream_t *stream, size_t offset, struct tm *val);
624 
633 size_t cbor_serialize_date_time_epoch(cbor_stream_t *stream, time_t val);
634 
644 size_t cbor_deserialize_date_time_epoch(const cbor_stream_t *stream, size_t offset, time_t *val);
645 
646 #endif /* CBOR_NO_CTIME */
647 
658 size_t cbor_write_tag(cbor_stream_t *stream, unsigned char tag);
659 
668 bool cbor_at_tag(const cbor_stream_t *stream, size_t offset);
669 
670 #endif /* CBOR_NO_SEMANTIC_TAGGING */
671 
681 size_t cbor_write_break(cbor_stream_t *stream);
682 
691 bool cbor_at_break(const cbor_stream_t *stream, size_t offset);
692 
703 bool cbor_at_end(const cbor_stream_t *stream, size_t offset);
704 
705 #ifdef __cplusplus
706 }
707 #endif
708 
709 #endif /* CBOR_H */
710 
size_t cbor_deserialize_int(const cbor_stream_t *stream, size_t offset, int *val)
Deserialize integers from stream to val.
size_t cbor_deserialize_float_half(const cbor_stream_t *stream, size_t offset, float *val)
Deserialize half-width floating point values values from stream to val.
size_t pos
Index to the next free byte.
Definition: cbor.h:155
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:154
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:153
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_date_time(const cbor_stream_t *stream, size_t offset, struct tm *val)
Deserialize date and time.
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.
size_t cbor_deserialize_float(const cbor_stream_t *stream, size_t offset, float *val)
Deserialize floating point values values from stream to val.
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_serialize_date_time(cbor_stream_t *stream, struct tm *val)
Serialize date and time.
size_t cbor_serialize_float(cbor_stream_t *stream, float val)
Serializes a floating point value.
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_deserialize_double(const cbor_stream_t *stream, size_t offset, double *val)
Deserialize double precision floating point valeus 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_date_time_epoch(cbor_stream_t *stream, time_t val)
Serialize seconds since UNIX epoch.
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.
bool cbor_at_tag(const cbor_stream_t *stream, size_t offset)
Whether we are at a tag symbol in stream stream at offset offset.
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.
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:152
stdlib.h wrapper for MSP430
void cbor_stream_decode(cbor_stream_t *stream)
Decode CBOR from stream.
size_t cbor_serialize_double(cbor_stream_t *stream, double val)
Serializes a double precision floating point value.
size_t cbor_write_tag(cbor_stream_t *stream, unsigned char tag)
Write a tag to give the next CBOR item additional semantics.
size_t cbor_deserialize_date_time_epoch(const cbor_stream_t *stream, size_t offset, time_t *val)
Deserialize seconds since UNIX epoch.
size_t cbor_serialize_float_half(cbor_stream_t *stream, float val)
Serializes a half-width floating point value.