The friendly Operating System for the Internet of Things
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 
101 #ifndef CBOR_H
102 #define CBOR_H
103 
104 #include <stdbool.h>
105 #include <stdint.h>
106 #include <stdlib.h>
107 
108 #ifndef CBOR_NO_CTIME
109 #include <time.h>
110 #endif /* CBOR_NO_CTIME */
111 
112 #ifdef __cplusplus
113 extern "C" {
114 #endif
115 
136 typedef struct {
138  unsigned char *data;
140  size_t size;
142  size_t pos;
143 } cbor_stream_t;
144 
153 void cbor_init(cbor_stream_t *stream, unsigned char *buffer, size_t size);
154 
162 void cbor_clear(cbor_stream_t *stream);
163 
171 void cbor_destroy(cbor_stream_t *stream);
172 
173 #ifndef CBOR_NO_PRINT
174 
179 void cbor_stream_print(const cbor_stream_t *stream);
180 
199 void cbor_stream_decode(cbor_stream_t *stream);
200 #endif /* CBOR_NO_PRINT */
201 
210 size_t cbor_serialize_int(cbor_stream_t *stream, int val);
211 
221 size_t cbor_deserialize_int(const cbor_stream_t *stream, size_t offset,
222  int *val);
223 
232 size_t cbor_serialize_uint64_t(cbor_stream_t *stream, uint64_t val);
233 
243 size_t cbor_deserialize_uint64_t(const cbor_stream_t *stream, size_t offset,
244  uint64_t *val);
245 
254 size_t cbor_serialize_int64_t(cbor_stream_t *stream, int64_t val);
255 
265 size_t cbor_deserialize_int64_t(const cbor_stream_t *stream, size_t offset,
266  int64_t *val);
267 
276 size_t cbor_serialize_bool(cbor_stream_t *stream, bool val);
277 
287 size_t cbor_deserialize_bool(const cbor_stream_t *stream, size_t offset,
288  bool *val);
289 
290 #ifndef CBOR_NO_FLOAT
291 
299 size_t cbor_serialize_float_half(cbor_stream_t *stream, float val);
300 
311 size_t cbor_deserialize_float_half(const cbor_stream_t *stream, size_t offset,
312  float *val);
321 size_t cbor_serialize_float(cbor_stream_t *stream, float val);
322 
332 size_t cbor_deserialize_float(const cbor_stream_t *stream, size_t offset,
333  float *val);
342 size_t cbor_serialize_double(cbor_stream_t *stream, double val);
343 
354 size_t cbor_deserialize_double(const cbor_stream_t *stream, size_t offset,
355  double *val);
356 #endif /* CBOR_NO_FLOAT */
357 
366 size_t cbor_serialize_byte_string(cbor_stream_t *stream, const char *val);
367 
377 size_t cbor_serialize_byte_stringl(cbor_stream_t *stream, const char *val, size_t length);
378 
389 size_t cbor_deserialize_byte_string(const cbor_stream_t *stream, size_t offset,
390  char *val, size_t length);
391 
392 size_t cbor_serialize_unicode_string(cbor_stream_t *stream, const char *val);
393 
404 size_t cbor_deserialize_byte_string_no_copy(const cbor_stream_t *stream, size_t offset,
405  unsigned char **val, size_t *length);
406 
407 size_t cbor_deserialize_unicode_string_no_copy(const cbor_stream_t *stream, size_t offset,
408  unsigned char **val, size_t *length);
409 
421  size_t offset, char *val, size_t length);
422 
441 size_t cbor_serialize_array(cbor_stream_t *stream, size_t array_length);
442 
462 size_t cbor_deserialize_array(const cbor_stream_t *stream, size_t offset,
463  size_t *array_length);
464 
473 
482 size_t cbor_deserialize_array_indefinite(const cbor_stream_t *stream, size_t offset);
483 
501 size_t cbor_serialize_map(cbor_stream_t *stream, size_t map_length);
502 
529 size_t cbor_deserialize_map(const cbor_stream_t *stream, size_t offset,
530  size_t *map_length);
531 
540 
549 size_t cbor_deserialize_map_indefinite(const cbor_stream_t *stream, size_t offset);
550 
551 #ifndef CBOR_NO_SEMANTIC_TAGGING
552 #ifndef CBOR_NO_CTIME
553 
574 size_t cbor_serialize_date_time(cbor_stream_t *stream, struct tm *val);
575 
591 size_t cbor_deserialize_date_time(const cbor_stream_t *stream, size_t offset, struct tm *val);
592 
593 size_t cbor_serialize_date_time_epoch(cbor_stream_t *stream, time_t val);
594 
595 size_t cbor_deserialize_date_time_epoch(const cbor_stream_t *stream, size_t offset, time_t *val);
596 
597 #endif /* CBOR_NO_CTIME */
598 
609 size_t cbor_write_tag(cbor_stream_t *stream, unsigned char tag);
610 
619 bool cbor_at_tag(const cbor_stream_t *stream, size_t offset);
620 
621 #endif /* CBOR_NO_SEMANTIC_TAGGING */
622 
632 size_t cbor_write_break(cbor_stream_t *stream);
633 
642 bool cbor_at_break(const cbor_stream_t *stream, size_t offset);
643 
654 bool cbor_at_end(const cbor_stream_t *stream, size_t offset);
655 
656 #ifdef __cplusplus
657 }
658 #endif
659 
660 #endif
661 
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.
void cbor_init(cbor_stream_t *stream, unsigned char *buffer, size_t size)
Initialize cbor struct.
size_t pos
Index to the next free byte.
Definition: cbor.h:142
size_t cbor_serialize_bool(cbor_stream_t *stream, bool val)
Serializes a boolean value.
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.
void cbor_destroy(cbor_stream_t *stream)
Destroy the cbor struct.
size_t cbor_serialize_date_time(cbor_stream_t *stream, struct tm *val)
Serialize date and time.
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_byte_string(const cbor_stream_t *stream, size_t offset, char *val, size_t length)
Deserialize bytes from stream to val.
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.
void cbor_clear(cbor_stream_t *stream)
Clear cbor struct.
void cbor_stream_decode(cbor_stream_t *stream)
Decode CBOR from stream.
size_t size
Size of the array.
Definition: cbor.h:140
size_t cbor_serialize_array(cbor_stream_t *stream, size_t array_length)
Serialize array of length array_length.
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_stream_print(const cbor_stream_t *stream)
Print stream in hex representation.
size_t cbor_serialize_int64_t(cbor_stream_t *stream, int64_t val)
Serializes a signed 64 bit value.
size_t cbor_serialize_uint64_t(cbor_stream_t *stream, uint64_t val)
Serializes an unsigned 64 bit 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_serialize_double(cbor_stream_t *stream, double val)
Serializes a double precision floating point value.
unsigned char * data
Array containing CBOR encoded data.
Definition: cbor.h:138
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.
size_t cbor_deserialize_byte_string_no_copy(const cbor_stream_t *stream, size_t offset, unsigned char **val, size_t *length)
Deserialize bytes/unicode from stream to val (without copy)
size_t cbor_deserialize_array(const cbor_stream_t *stream, size_t offset, size_t *array_length)
Deserialize array of items.
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_int(const cbor_stream_t *stream, size_t offset, int *val)
Deserialize integers from stream to val.
size_t cbor_write_break(cbor_stream_t *stream)
Write a break symbol at the current offset in stream stream.
size_t cbor_serialize_byte_string(cbor_stream_t *stream, const char *val)
Serializes a signed 64 bit 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_serialize_float(cbor_stream_t *stream, float val)
Serializes a floating point value.
size_t cbor_serialize_float_half(cbor_stream_t *stream, float val)
Serializes a half-width floating point value.
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 cbor_deserialize_array_indefinite(const cbor_stream_t *stream, size_t offset)
Deserialize array of items.
size_t cbor_serialize_array_indefinite(cbor_stream_t *stream)
Serialize array of infite length.
Datatype to represent time.
size_t cbor_serialize_map(cbor_stream_t *stream, size_t map_length)
Serialize map of length map_length.
size_t cbor_serialize_map_indefinite(cbor_stream_t *stream)
Serialize map of infite length.
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_byte_stringl(cbor_stream_t *stream, const char *val, size_t length)
Serializes an arbitrary byte string.
Struct containing CBOR-encoded data.
Definition: cbor.h:136
stdlib.h wrapper for MSP430
size_t cbor_deserialize_map_indefinite(const cbor_stream_t *stream, size_t offset)
Deserialize map of items.
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_map(const cbor_stream_t *stream, size_t offset, size_t *map_length)
Deserialize map of items.