-
Notifications
You must be signed in to change notification settings - Fork 23
/
cbor.cpp
295 lines (259 loc) · 7.02 KB
/
cbor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <cassert>
#include "cn-cbor/cn-cbor.h"
#include <cose/cose.h>
#include <stdlib.h>
#ifdef __MBED__
#include <string.h>
#else
#include <memory.h>
#endif
#define INIT_CB(v) \
if (errp) { \
errp->err = CN_CBOR_NO_ERROR; \
} \
(v) = static_cast<cn_cbor *>(CN_CALLOC_CONTEXT()); \
if (!(v)) { \
if (errp) { \
errp->err = CN_CBOR_ERR_OUT_OF_MEMORY; \
} \
return false; \
}
#ifdef USE_CBOR_CONTEXT
#define CBOR_CONTEXT_PARAM , context
#define CBOR_CONTEXT_PARAM_COMMA context,
#if 1
#define CN_CALLOC(ctx) \
((ctx) && (ctx)->calloc_func) \
? (ctx)->calloc_func(1, sizeof(cn_cbor), (ctx)->context) \
: calloc(1, sizeof(cn_cbor))
#endif
#define CN_CALLOC_CONTEXT() CN_CALLOC(context)
#define CN_CBOR_CALLOC(c, i, ctx) \
((ctx) && (ctx)->calloc_func) ? (ctx)->calloc_func(c, i, (ctx)->context) \
: calloc(c, i)
#define COSE_FREE(ptr, ctx) \
((((ctx) && (ctx)->free_func)) ? ((ctx)->free_func((ptr), (ctx)->context)) \
: free((ptr)))
#else
#define CBOR_CONTEXT_PARAM
#define CBOR_CONTEXT_PARAM_COMMA
#define CN_CALLOC(ctx) calloc(1, sizeof(cn_cbor))
#define CN_CALLOC_CONTEXT() CN_CALLOC(context)
#define CN_CBOR_CALLOC(c, i, ctx) calloc(c, i)
#define COSE_FREE(ptr, ctx) free(ptr)
#endif
/***
* Replace the i-th element in the array.
* Extend the array if necessary so it has enough elements.
*
* @param[in] cb_array Array to use
* @param[in] cb_value New item to be placed in the array
* @param[in] index Zero based index to be used
* @param[in] CBOR_CONTEXT Context based allocation structure
* @param[in,out] errp CBOR error return on failure
* returns Did we succeed?
*/
bool cn_cbor_array_replace(cn_cbor *cb_array,
cn_cbor *cb_value,
int index,
CBOR_CONTEXT_COMMA cn_cbor_errback *errp)
{
int i;
cn_cbor *cb_temp;
cn_cbor *cb_temp2;
if (!cb_array || !cb_value || cb_array->type != CN_CBOR_ARRAY) {
if (errp != nullptr) {
errp->err = CN_CBOR_ERR_INVALID_PARAMETER;
}
return false;
}
if (index == 0) {
if (cb_array->length > 0) {
cb_temp = cb_array->first_child;
cb_value->parent = cb_array;
cb_value->next = cb_temp->next;
if (cb_array->last_child == cb_temp) {
cb_array->last_child = cb_value;
}
cb_array->first_child = cb_value;
cb_temp->parent = nullptr;
cb_temp->next = nullptr;
cn_cbor_free(cb_temp CBOR_CONTEXT_PARAM);
return true;
}
return cn_cbor_array_append(cb_array, cb_value, errp);
}
if (cb_array->first_child == nullptr) {
INIT_CB(cb_temp2);
cb_array->first_child = cb_array->last_child = cb_temp2;
cb_temp2->parent = cb_array;
cb_array->length = 1;
cb_temp2->type = CN_CBOR_INVALID;
}
cb_temp = cb_array->first_child;
for (i = 1; i < index; i++) {
if (cb_temp->next == nullptr) {
INIT_CB(cb_temp2);
cb_temp2->type = CN_CBOR_INVALID;
cb_temp->next = cb_temp2;
cb_temp2->parent = cb_array;
cb_array->last_child = cb_temp2;
cb_array->length += 1;
}
cb_temp = cb_temp->next;
}
if (cb_temp->next == nullptr) {
cb_temp->next = cb_value;
cb_value->parent = cb_array;
cb_array->last_child = cb_value;
cb_array->length += 1;
return true;
}
cb_temp2 = cb_temp->next;
cb_value->next = cb_temp2->next;
cb_temp->next = cb_value;
cb_value->parent = cb_array;
if (cb_array->last_child == cb_temp2) {
cb_array->last_child = cb_value;
}
cb_temp2->next = nullptr;
cb_temp2->parent = nullptr;
cn_cbor_free(cb_temp2 CBOR_CONTEXT_PARAM);
return true;
}
cn_cbor *cn_cbor_clone(const cn_cbor *pIn,
CBOR_CONTEXT_COMMA cn_cbor_errback *pcn_cbor_error)
{
cn_cbor *pOut = nullptr;
char *sz;
unsigned char *pb;
cn_cbor *pTemp;
cn_cbor *pLast;
int count;
switch (pIn->type) {
case CN_CBOR_TEXT:
sz = (char *)(CN_CBOR_CALLOC(pIn->length + 1, 1, context));
if (sz == nullptr) {
return nullptr;
}
memcpy(sz, pIn->v.str, pIn->length);
sz[pIn->length] = 0;
pOut = cn_cbor_string_create2(
sz, 0 CBOR_CONTEXT_PARAM, pcn_cbor_error);
if (pOut == nullptr) {
COSE_FREE(sz, context);
}
break;
case CN_CBOR_UINT:
pOut = cn_cbor_int_create(
pIn->v.sint CBOR_CONTEXT_PARAM, pcn_cbor_error);
break;
case CN_CBOR_INT:
pOut = cn_cbor_int_create(
pIn->v.uint CBOR_CONTEXT_PARAM, pcn_cbor_error);
break;
case CN_CBOR_TRUE:
pOut = cn_cbor_bool_create(true CBOR_CONTEXT_PARAM, pcn_cbor_error);
break;
case CN_CBOR_FALSE:
pOut =
cn_cbor_bool_create(false CBOR_CONTEXT_PARAM, pcn_cbor_error);
break;
case CN_CBOR_BYTES:
pb = static_cast<unsigned char *>(
CN_CBOR_CALLOC((int)pIn->length, 1, context));
if (pb == nullptr) {
return nullptr;
}
memcpy(pb, pIn->v.bytes, pIn->length);
pOut = cn_cbor_data_create2(
pb, (int)pIn->length, 0 CBOR_CONTEXT_PARAM, pcn_cbor_error);
if (pOut == nullptr) {
COSE_FREE((cn_cbor *)pb, context);
}
break;
case CN_CBOR_MAP:
pOut = cn_cbor_map_create(CBOR_CONTEXT_PARAM_COMMA pcn_cbor_error);
if (pOut == nullptr) {
return nullptr;
}
pTemp = pIn->first_child;
pLast = nullptr;
count = 0;
while (pTemp != nullptr) {
cn_cbor *p = cn_cbor_clone(
pTemp, CBOR_CONTEXT_PARAM_COMMA pcn_cbor_error);
if (p == nullptr) {
cn_cbor_free(pOut CBOR_CONTEXT_PARAM);
return nullptr;
}
if (pLast == nullptr) {
pOut->first_child = p;
pLast = p;
}
else {
pLast->next = p;
pLast = p;
}
p->parent = pOut;
count += 1;
pTemp = pTemp->next;
}
pOut->last_child = pLast;
pOut->length = count;
break;
default:
assert(false);
break;
}
return pOut;
}
#ifndef CN_CBOR_VERSION
cn_cbor *cn_cbor_tag_create(int tag,
cn_cbor *child,
CBOR_CONTEXT_COMMA cn_cbor_errback *perr)
{
cn_cbor *pcnTag = CN_CALLOC(context);
if (pcnTag == nullptr) {
if (perr != nullptr) {
perr->err = CN_CBOR_ERR_OUT_OF_MEMORY;
}
return nullptr;
}
pcnTag->type = CN_CBOR_TAG;
pcnTag->v.sint = tag;
pcnTag->first_child = child;
child->parent = pcnTag;
return pcnTag;
}
cn_cbor *cn_cbor_bool_create(int boolValue,
CBOR_CONTEXT_COMMA cn_cbor_errback *errp)
{
cn_cbor *pcn = CN_CALLOC(context);
if (pcn == nullptr) {
if (errp != nullptr) {
errp->err = CN_CBOR_ERR_OUT_OF_MEMORY;
}
return nullptr;
}
pcn->type = CN_CBOR_FALSE + (boolValue != 0);
return pcn;
}
cn_cbor *cn_cbor_null_create(CBOR_CONTEXT_COMMA cn_cbor_errback *errp)
{
cn_cbor *pcn = CN_CALLOC(context);
if (pcn == nullptr) {
if (errp != nullptr) {
errp->err = CN_CBOR_ERR_OUT_OF_MEMORY;
}
return nullptr;
}
pcn->type = CN_CBOR_NULL;
return pcn;
}
#endif
size_t cn_cbor_encode_size(cn_cbor *object)
{
ssize_t size = cn_cbor_encoder_write(nullptr, 0, 0, object);
return size >= 0 ? size : 0;
}