• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef CBOR_PROTOCOL_H__
2 #define CBOR_PROTOCOL_H__
3 
4 /* The 8 major types */
5 #define MT_UNSIGNED 0
6 #define MT_NEGATIVE 1
7 #define MT_BYTES    2
8 #define MT_TEXT     3
9 #define MT_ARRAY    4
10 #define MT_MAP      5
11 #define MT_TAG      6
12 #define MT_PRIM     7
13 
14 /* The initial bytes resulting from those */
15 #define IB_UNSIGNED (MT_UNSIGNED << 5)
16 #define IB_NEGATIVE (MT_NEGATIVE << 5)
17 #define IB_BYTES    (MT_BYTES << 5)
18 #define IB_TEXT     (MT_TEXT << 5)
19 #define IB_ARRAY    (MT_ARRAY << 5)
20 #define IB_MAP      (MT_MAP << 5)
21 #define IB_TAG      (MT_TAG << 5)
22 #define IB_PRIM     (MT_PRIM << 5)
23 
24 #define IB_NEGFLAG (IB_NEGATIVE - IB_UNSIGNED)
25 #define IB_NEGFLAG_AS_BIT(ib) ((ib) >> 5)
26 #define IB_TEXTFLAG (IB_TEXT - IB_BYTES)
27 
28 #define IB_AI(ib) ((ib) & 0x1F)
29 #define IB_MT(ib) ((ib) >> 5)
30 
31 /* Tag numbers handled by this implementation */
32 #define TAG_TIME_EPOCH 1
33 #define TAG_BIGNUM     2
34 #define TAG_BIGNUM_NEG 3
35 #define TAG_URI        32
36 #define TAG_RE         35
37 
38 /* Initial bytes of those tag numbers */
39 #define IB_TIME_EPOCH (IB_TAG | TAG_TIME_EPOCH)
40 #define IB_BIGNUM     (IB_TAG | TAG_BIGNUM)
41 #define IB_BIGNUM_NEG (IB_TAG | TAG_BIGNUM_NEG)
42 /* TAG_URI and TAG_RE are non-immediate tags */
43 
44 /* Simple values handled by this implementation */
45 #define VAL_FALSE 20
46 #define VAL_TRUE  21
47 #define VAL_NIL   22
48 #define VAL_UNDEF 23
49 
50 /* Initial bytes of those simple values */
51 #define IB_FALSE (IB_PRIM | VAL_FALSE)
52 #define IB_TRUE  (IB_PRIM | VAL_TRUE)
53 #define IB_NIL   (IB_PRIM | VAL_NIL)
54 #define IB_UNDEF (IB_PRIM | VAL_UNDEF)
55 
56 /* AI values with more data in head */
57 #define AI_1 24
58 #define AI_2 25
59 #define AI_4 26
60 #define AI_8 27
61 #define AI_INDEF 31
62 #define IB_BREAK (IB_PRIM | AI_INDEF)
63 /* For  */
64 #define IB_UNUSED (IB_TAG | AI_INDEF)
65 
66 /* Floating point initial bytes */
67 #define IB_FLOAT2 (IB_PRIM | AI_2)
68 #define IB_FLOAT4 (IB_PRIM | AI_4)
69 #define IB_FLOAT8 (IB_PRIM | AI_8)
70 
71 // These definitions are here because they aren't required for the public
72 // interface, and they were quite confusing in cn-cbor.h
73 
74 #ifdef USE_CBOR_CONTEXT
75 /**
76  * Allocate enough space for 1 `cn_cbor` structure.
77  *
78  * @param[in]  ctx  The allocation context, or NULL for calloc.
79  * @return          A pointer to a `cn_cbor` or NULL on failure
80  */
81 #define CN_CALLOC(ctx) ((ctx) && (ctx)->calloc_func) ? \
82     (ctx)->calloc_func(1, sizeof(cn_cbor), (ctx)->context) : \
83     calloc(1, sizeof(cn_cbor));
84 
85 /**
86  * Free a
87  * @param  free_func [description]
88  * @return           [description]
89  */
90 #define CN_FREE(ptr, ctx) ((ctx) && (ctx)->free_func) ? \
91     (ctx)->free_func((ptr), (ctx)->context) : \
92     free((ptr));
93 
94 #define CBOR_CONTEXT_PARAM , context
95 #define CN_CALLOC_CONTEXT() CN_CALLOC(context)
96 #define CN_CBOR_FREE_CONTEXT(p) CN_FREE(p, context)
97 
98 #else
99 
100 #define CBOR_CONTEXT_PARAM
101 #define CN_CALLOC_CONTEXT() CN_CALLOC
102 #define CN_CBOR_FREE_CONTEXT(p) CN_FREE(p)
103 
104 #ifndef CN_CALLOC
105 #define CN_CALLOC calloc(1, sizeof(cn_cbor))
106 #endif
107 
108 #ifndef CN_FREE
109 #define CN_FREE free
110 #endif
111 
112 #endif // USE_CBOR_CONTEXT
113 
114 #ifndef UNUSED_PARAM
115 #define UNUSED_PARAM(p) ((void)&(p))
116 #endif
117 
118 #endif // CBOR_PROTOCOL_H__
119