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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/*
* Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
*
* libcbor is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include "builder_callbacks.h"
#include <string.h>
#include "../arrays.h"
#include "../bytestrings.h"
#include "../floats_ctrls.h"
#include "../ints.h"
#include "../maps.h"
#include "../strings.h"
#include "../tags.h"
#include "unicode.h"
void _cbor_builder_append(cbor_item_t *item,
struct _cbor_decoder_context *ctx) {
if (ctx->stack->size == 0) {
/* Top level item */
ctx->root = item;
} else {
/* Part of a bigger structure */
switch (ctx->stack->top->item->type) {
case CBOR_TYPE_ARRAY: {
if (cbor_array_is_definite(ctx->stack->top->item)) {
/*
* We don't need an explicit check for whether the item still belongs
* into this array because if there are extra items, they will cause a
* syntax error when decoded.
*/
assert(ctx->stack->top->subitems > 0);
cbor_array_push(ctx->stack->top->item, item);
ctx->stack->top->subitems--;
if (ctx->stack->top->subitems == 0) {
cbor_item_t *item = ctx->stack->top->item;
_cbor_stack_pop(ctx->stack);
_cbor_builder_append(item, ctx);
}
cbor_decref(&item);
} else {
/* Indefinite array, don't bother with subitems */
cbor_array_push(ctx->stack->top->item, item);
cbor_decref(&item);
}
break;
}
case CBOR_TYPE_MAP: {
/* We use 0 and 1 subitems to distinguish between keys and values in
* indefinite items */
if (ctx->stack->top->subitems % 2) {
/* Odd record, this is a value */
_cbor_map_add_value(ctx->stack->top->item, cbor_move(item));
} else {
/* Even record, this is a key */
_cbor_map_add_key(ctx->stack->top->item, cbor_move(item));
}
if (cbor_map_is_definite(ctx->stack->top->item)) {
ctx->stack->top->subitems--;
if (ctx->stack->top->subitems == 0) {
cbor_item_t *item = ctx->stack->top->item;
_cbor_stack_pop(ctx->stack);
_cbor_builder_append(item, ctx);
}
} else {
ctx->stack->top->subitems ^=
1; /* Flip the indicator for indefinite items */
}
break;
}
case CBOR_TYPE_TAG: {
assert(ctx->stack->top->subitems == 1);
cbor_tag_set_item(ctx->stack->top->item, item);
cbor_decref(&item); /* Give up on our reference */
cbor_item_t *item = ctx->stack->top->item;
_cbor_stack_pop(ctx->stack);
_cbor_builder_append(item, ctx);
break;
}
default: {
cbor_decref(&item);
ctx->syntax_error = true;
}
}
}
}
#define CHECK_RES(ctx, res) \
do { \
if (res == NULL) { \
ctx->creation_failed = true; \
return; \
} \
} while (0)
#define PUSH_CTX_STACK(ctx, res, subitems) \
do { \
if (_cbor_stack_push(ctx->stack, res, subitems) == NULL) { \
cbor_decref(&res); \
ctx->creation_failed = true; \
} \
} while (0)
void cbor_builder_uint8_callback(void *context, uint8_t value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_int8();
CHECK_RES(ctx, res);
cbor_mark_uint(res);
cbor_set_uint8(res, value);
_cbor_builder_append(res, ctx);
}
void cbor_builder_uint16_callback(void *context, uint16_t value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_int16();
CHECK_RES(ctx, res);
cbor_mark_uint(res);
cbor_set_uint16(res, value);
_cbor_builder_append(res, ctx);
}
void cbor_builder_uint32_callback(void *context, uint32_t value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_int32();
CHECK_RES(ctx, res);
cbor_mark_uint(res);
cbor_set_uint32(res, value);
_cbor_builder_append(res, ctx);
}
void cbor_builder_uint64_callback(void *context, uint64_t value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_int64();
CHECK_RES(ctx, res);
cbor_mark_uint(res);
cbor_set_uint64(res, value);
_cbor_builder_append(res, ctx);
}
void cbor_builder_negint8_callback(void *context, uint8_t value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_int8();
CHECK_RES(ctx, res);
cbor_mark_negint(res);
cbor_set_uint8(res, value);
_cbor_builder_append(res, ctx);
}
void cbor_builder_negint16_callback(void *context, uint16_t value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_int16();
cbor_mark_negint(res);
cbor_set_uint16(res, value);
_cbor_builder_append(res, ctx);
}
void cbor_builder_negint32_callback(void *context, uint32_t value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_int32();
CHECK_RES(ctx, res);
cbor_mark_negint(res);
cbor_set_uint32(res, value);
_cbor_builder_append(res, ctx);
}
void cbor_builder_negint64_callback(void *context, uint64_t value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_int64();
CHECK_RES(ctx, res);
cbor_mark_negint(res);
cbor_set_uint64(res, value);
_cbor_builder_append(res, ctx);
}
void cbor_builder_byte_string_callback(void *context, cbor_data data,
size_t length) {
struct _cbor_decoder_context *ctx = context;
unsigned char *new_handle = _CBOR_MALLOC(length);
if (new_handle == NULL) {
ctx->creation_failed = true;
return;
}
memcpy(new_handle, data, length);
cbor_item_t *res = cbor_new_definite_bytestring();
if (res == NULL) {
_CBOR_FREE(new_handle);
ctx->creation_failed = true;
return;
}
cbor_bytestring_set_handle(res, new_handle, length);
if (ctx->stack->size > 0 && cbor_isa_bytestring(ctx->stack->top->item)) {
if (cbor_bytestring_is_indefinite(ctx->stack->top->item)) {
cbor_bytestring_add_chunk(ctx->stack->top->item, cbor_move(res));
} else {
cbor_decref(&res);
ctx->syntax_error = true;
}
} else {
_cbor_builder_append(res, ctx);
}
}
void cbor_builder_byte_string_start_callback(void *context) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_indefinite_bytestring();
CHECK_RES(ctx, res);
PUSH_CTX_STACK(ctx, res, 0);
}
void cbor_builder_string_callback(void *context, cbor_data data,
size_t length) {
struct _cbor_decoder_context *ctx = context;
struct _cbor_unicode_status unicode_status;
size_t codepoint_count =
_cbor_unicode_codepoint_count(data, length, &unicode_status);
if (unicode_status.status == _CBOR_UNICODE_BADCP) {
ctx->syntax_error = true;
return;
}
unsigned char *new_handle = _CBOR_MALLOC(length);
if (new_handle == NULL) {
ctx->creation_failed = true;
return;
}
memcpy(new_handle, data, length);
cbor_item_t *res = cbor_new_definite_string();
if (res == NULL) {
_CBOR_FREE(new_handle);
ctx->creation_failed = true;
return;
}
cbor_string_set_handle(res, new_handle, length);
res->metadata.string_metadata.codepoint_count = codepoint_count;
/* Careful here: order matters */
if (ctx->stack->size > 0 && cbor_isa_string(ctx->stack->top->item)) {
if (cbor_string_is_indefinite(ctx->stack->top->item)) {
cbor_string_add_chunk(ctx->stack->top->item, cbor_move(res));
} else {
cbor_decref(&res);
ctx->syntax_error = true;
}
} else {
_cbor_builder_append(res, ctx);
}
}
void cbor_builder_string_start_callback(void *context) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_indefinite_string();
CHECK_RES(ctx, res);
PUSH_CTX_STACK(ctx, res, 0);
}
void cbor_builder_array_start_callback(void *context, size_t size) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_definite_array(size);
CHECK_RES(ctx, res);
if (size > 0) {
PUSH_CTX_STACK(ctx, res, size);
} else {
_cbor_builder_append(res, ctx);
}
}
void cbor_builder_indef_array_start_callback(void *context) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_indefinite_array();
CHECK_RES(ctx, res);
PUSH_CTX_STACK(ctx, res, 0);
}
void cbor_builder_indef_map_start_callback(void *context) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_indefinite_map();
CHECK_RES(ctx, res);
PUSH_CTX_STACK(ctx, res, 0);
}
void cbor_builder_map_start_callback(void *context, size_t size) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_definite_map(size);
CHECK_RES(ctx, res);
if (size > 0) {
PUSH_CTX_STACK(ctx, res, size * 2);
} else {
_cbor_builder_append(res, ctx);
}
}
/**
* Is the (partially constructed) item indefinite?
*/
bool _cbor_is_indefinite(cbor_item_t *item) {
switch (item->type) {
case CBOR_TYPE_BYTESTRING:
return item->metadata.bytestring_metadata.type ==
_CBOR_METADATA_INDEFINITE;
case CBOR_TYPE_STRING:
return item->metadata.string_metadata.type == _CBOR_METADATA_INDEFINITE;
case CBOR_TYPE_ARRAY:
return item->metadata.array_metadata.type == _CBOR_METADATA_INDEFINITE;
case CBOR_TYPE_MAP:
return item->metadata.map_metadata.type == _CBOR_METADATA_INDEFINITE;
default:
return false;
}
}
void cbor_builder_indef_break_callback(void *context) {
struct _cbor_decoder_context *ctx = context;
/* There must be an item to break out of*/
if (ctx->stack->size > 0) {
cbor_item_t *item = ctx->stack->top->item;
if (_cbor_is_indefinite(
item) && /* Only indefinite items can be terminated by 0xFF */
/* Special case: we cannot append up if an indefinite map is incomplete
(we are expecting a value). */
(item->type != CBOR_TYPE_MAP || ctx->stack->top->subitems % 2 == 0)) {
_cbor_stack_pop(ctx->stack);
_cbor_builder_append(item, ctx);
return;
}
}
ctx->syntax_error = true;
}
void cbor_builder_float2_callback(void *context, float value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_float2();
cbor_set_float2(res, value);
_cbor_builder_append(res, ctx);
}
void cbor_builder_float4_callback(void *context, float value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_float4();
CHECK_RES(ctx, res);
cbor_set_float4(res, value);
_cbor_builder_append(res, ctx);
}
void cbor_builder_float8_callback(void *context, double value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_float8();
CHECK_RES(ctx, res);
cbor_set_float8(res, value);
_cbor_builder_append(res, ctx);
}
void cbor_builder_null_callback(void *context) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_null();
CHECK_RES(ctx, res);
_cbor_builder_append(res, ctx);
}
void cbor_builder_undefined_callback(void *context) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_undef();
CHECK_RES(ctx, res);
_cbor_builder_append(res, ctx);
}
void cbor_builder_boolean_callback(void *context, bool value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_build_bool(value);
CHECK_RES(ctx, res);
_cbor_builder_append(res, ctx);
}
void cbor_builder_tag_callback(void *context, uint64_t value) {
struct _cbor_decoder_context *ctx = context;
cbor_item_t *res = cbor_new_tag(value);
CHECK_RES(ctx, res);
PUSH_CTX_STACK(ctx, res, 1);
}
|