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 #ifdef MODULE_CBOR_CTIME
124 #include <time.h>
125 #endif /* MODULE_CBOR_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 
191 void cbor_stream_print(const cbor_stream_t *stream);
192 
211 void cbor_stream_decode(cbor_stream_t *stream);
212 
221 size_t cbor_serialize_int(cbor_stream_t *stream, int val);
222 
232 size_t cbor_deserialize_int(const cbor_stream_t *stream, size_t offset,
233  int *val);
234 
243 size_t cbor_serialize_uint64_t(cbor_stream_t *stream, uint64_t val);
244 
254 size_t cbor_deserialize_uint64_t(const cbor_stream_t *stream, size_t offset,
255  uint64_t *val);
256 
265 size_t cbor_serialize_int64_t(cbor_stream_t *stream, int64_t val);
266 
276 size_t cbor_deserialize_int64_t(const cbor_stream_t *stream, size_t offset,
277  int64_t *val);
278 
287 size_t cbor_serialize_bool(cbor_stream_t *stream, bool val);
288 
298 size_t cbor_deserialize_bool(const cbor_stream_t *stream, size_t offset,
299  bool *val);
300 
301 #ifdef MODULE_CBOR_FLOAT
302 
310 size_t cbor_serialize_float_half(cbor_stream_t *stream, float val);
311 
322 size_t cbor_deserialize_float_half(const cbor_stream_t *stream, size_t offset,
323  float *val);
332 size_t cbor_serialize_float(cbor_stream_t *stream, float val);
333 
343 size_t cbor_deserialize_float(const cbor_stream_t *stream, size_t offset,
344  float *val);
353 size_t cbor_serialize_double(cbor_stream_t *stream, double val);
354 
365 size_t cbor_deserialize_double(const cbor_stream_t *stream, size_t offset,
366  double *val);
367 #endif /* MODULE_CBOR_FLOAT */
368 
377 size_t cbor_serialize_byte_string(cbor_stream_t *stream, const char *val);
378 
388 size_t cbor_serialize_byte_stringl(cbor_stream_t *stream, const char *val, size_t length);
389 
400 size_t cbor_deserialize_byte_string(const cbor_stream_t *stream, size_t offset,
401  char *val, size_t length);
402 
412 size_t cbor_serialize_unicode_string(cbor_stream_t *stream, const char *val);
413 
424 size_t cbor_deserialize_byte_string_no_copy(const cbor_stream_t *stream, size_t offset,
425  unsigned char **val, size_t *length);
426 
437 size_t cbor_deserialize_unicode_string_no_copy(const cbor_stream_t *stream, size_t offset,
438  unsigned char **val, size_t *length);
439 
451  size_t offset, char *val, size_t length);
452 
471 size_t cbor_serialize_array(cbor_stream_t *stream, size_t array_length);
472 
492 size_t cbor_deserialize_array(const cbor_stream_t *stream, size_t offset,
493  size_t *array_length);
494 
503 
512 size_t cbor_deserialize_array_indefinite(const cbor_stream_t *stream, size_t offset);
513 
531 size_t cbor_serialize_map(cbor_stream_t *stream, size_t map_length);
532 
559 size_t cbor_deserialize_map(const cbor_stream_t *stream, size_t offset,
560  size_t *map_length);
561 
570 
579 size_t cbor_deserialize_map_indefinite(const cbor_stream_t *stream, size_t offset);
580 
581 #ifdef MODULE_CBOR_SEMANTIC_TAGGING
582 #ifdef MODULE_CBOR_CTIME
583 
604 size_t cbor_serialize_date_time(cbor_stream_t *stream, struct tm *val);
605 
621 size_t cbor_deserialize_date_time(const cbor_stream_t *stream, size_t offset, struct tm *val);
622 
631 size_t cbor_serialize_date_time_epoch(cbor_stream_t *stream, time_t val);
632 
642 size_t cbor_deserialize_date_time_epoch(const cbor_stream_t *stream, size_t offset, time_t *val);
643 
644 #endif /* MODULE_CBOR_CTIME */
645 
656 size_t cbor_write_tag(cbor_stream_t *stream, unsigned char tag);
657 
666 bool cbor_at_tag(const cbor_stream_t *stream, size_t offset);
667 
668 #endif /* MODULE_CBOR_SEMANTIC_TAGGING */
669 
679 size_t cbor_write_break(cbor_stream_t *stream);
680 
689 bool cbor_at_break(const cbor_stream_t *stream, size_t offset);
690 
701 bool cbor_at_end(const cbor_stream_t *stream, size_t offset);
702 
703 #ifdef __cplusplus
704 }
705 #endif
706 
707 #endif /* CBOR_H */
708 
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: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_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.
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.