aboutsummaryrefslogtreecommitdiff
path: root/contrib/libcbor/src/cbor/streaming.c
blob: 922a71d8e6b062f461f88c4327193038788aa4d3 (plain) (blame)
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
 * 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 "streaming.h"
#include "internal/loaders.h"

static bool claim_bytes(size_t required, size_t provided,
                        struct cbor_decoder_result *result) {
  if (required > (provided - result->read)) {
    result->required = required + result->read;
    result->read = 0;
    result->status = CBOR_DECODER_NEDATA;
    return false;
  } else {
    result->read += required;
    result->required = 0;
    return true;
  }
}

// Use implicit capture as an exception to avoid the super long parameter list
#define CLAIM_BYTES_AND_INVOKE(callback_name, length, source_extra_offset) \
  do {                                                                     \
    if (claim_bytes(length, source_size, &result)) {                       \
      callbacks->callback_name(context, source + 1 + source_extra_offset,  \
                               length);                                    \
    }                                                                      \
  } while (0)

#define READ_CLAIM_INVOKE(callback_name, length_reader, length_bytes) \
  do {                                                                \
    if (claim_bytes(length_bytes, source_size, &result)) {            \
      uint64_t length = length_reader(source + 1);                    \
      CLAIM_BYTES_AND_INVOKE(callback_name, length, length_bytes);    \
    }                                                                 \
    return result;                                                    \
  } while (0)

struct cbor_decoder_result cbor_stream_decode(
    cbor_data source, size_t source_size,
    const struct cbor_callbacks *callbacks, void *context) {
  // Attempt to claim the initial MTB byte
  struct cbor_decoder_result result = {.status = CBOR_DECODER_FINISHED};
  if (!claim_bytes(1, source_size, &result)) {
    return result;
  }

  switch (*source) {
    case 0x00: /* Fallthrough */
    case 0x01: /* Fallthrough */
    case 0x02: /* Fallthrough */
    case 0x03: /* Fallthrough */
    case 0x04: /* Fallthrough */
    case 0x05: /* Fallthrough */
    case 0x06: /* Fallthrough */
    case 0x07: /* Fallthrough */
    case 0x08: /* Fallthrough */
    case 0x09: /* Fallthrough */
    case 0x0A: /* Fallthrough */
    case 0x0B: /* Fallthrough */
    case 0x0C: /* Fallthrough */
    case 0x0D: /* Fallthrough */
    case 0x0E: /* Fallthrough */
    case 0x0F: /* Fallthrough */
    case 0x10: /* Fallthrough */
    case 0x11: /* Fallthrough */
    case 0x12: /* Fallthrough */
    case 0x13: /* Fallthrough */
    case 0x14: /* Fallthrough */
    case 0x15: /* Fallthrough */
    case 0x16: /* Fallthrough */
    case 0x17:
      /* Embedded one byte unsigned integer */
      {
        callbacks->uint8(context, _cbor_load_uint8(source));
        return result;
      }
    case 0x18:
      /* One byte unsigned integer */
      {
        if (claim_bytes(1, source_size, &result)) {
          callbacks->uint8(context, _cbor_load_uint8(source + 1));
        }
        return result;
      }
    case 0x19:
      /* Two bytes unsigned integer */
      {
        if (claim_bytes(2, source_size, &result)) {
          callbacks->uint16(context, _cbor_load_uint16(source + 1));
        }
        return result;
      }
    case 0x1A:
      /* Four bytes unsigned integer */
      {
        if (claim_bytes(4, source_size, &result)) {
          callbacks->uint32(context, _cbor_load_uint32(source + 1));
        }
        return result;
      }
    case 0x1B:
      /* Eight bytes unsigned integer */
      {
        if (claim_bytes(8, source_size, &result)) {
          callbacks->uint64(context, _cbor_load_uint64(source + 1));
        }
        return result;
      }
    case 0x1C: /* Fallthrough */
    case 0x1D: /* Fallthrough */
    case 0x1E: /* Fallthrough */
    case 0x1F:
      /* Reserved */
      { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; }
    case 0x20: /* Fallthrough */
    case 0x21: /* Fallthrough */
    case 0x22: /* Fallthrough */
    case 0x23: /* Fallthrough */
    case 0x24: /* Fallthrough */
    case 0x25: /* Fallthrough */
    case 0x26: /* Fallthrough */
    case 0x27: /* Fallthrough */
    case 0x28: /* Fallthrough */
    case 0x29: /* Fallthrough */
    case 0x2A: /* Fallthrough */
    case 0x2B: /* Fallthrough */
    case 0x2C: /* Fallthrough */
    case 0x2D: /* Fallthrough */
    case 0x2E: /* Fallthrough */
    case 0x2F: /* Fallthrough */
    case 0x30: /* Fallthrough */
    case 0x31: /* Fallthrough */
    case 0x32: /* Fallthrough */
    case 0x33: /* Fallthrough */
    case 0x34: /* Fallthrough */
    case 0x35: /* Fallthrough */
    case 0x36: /* Fallthrough */
    case 0x37:
      /* Embedded one byte negative integer */
      {
        callbacks->negint8(context,
                           _cbor_load_uint8(source) - 0x20); /* 0x20 offset */
        return result;
      }
    case 0x38:
      /* One byte negative integer */
      {
        if (claim_bytes(1, source_size, &result)) {
          callbacks->negint8(context, _cbor_load_uint8(source + 1));
        }
        return result;
      }
    case 0x39:
      /* Two bytes negative integer */
      {
        if (claim_bytes(2, source_size, &result)) {
          callbacks->negint16(context, _cbor_load_uint16(source + 1));
        }
        return result;
      }
    case 0x3A:
      /* Four bytes negative integer */
      {
        if (claim_bytes(4, source_size, &result)) {
          callbacks->negint32(context, _cbor_load_uint32(source + 1));
        }
        return result;
      }
    case 0x3B:
      /* Eight bytes negative integer */
      {
        if (claim_bytes(8, source_size, &result)) {
          callbacks->negint64(context, _cbor_load_uint64(source + 1));
        }
        return result;
      }
    case 0x3C: /* Fallthrough */
    case 0x3D: /* Fallthrough */
    case 0x3E: /* Fallthrough */
    case 0x3F:
      /* Reserved */
      { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; }
    case 0x40: /* Fallthrough */
    case 0x41: /* Fallthrough */
    case 0x42: /* Fallthrough */
    case 0x43: /* Fallthrough */
    case 0x44: /* Fallthrough */
    case 0x45: /* Fallthrough */
    case 0x46: /* Fallthrough */
    case 0x47: /* Fallthrough */
    case 0x48: /* Fallthrough */
    case 0x49: /* Fallthrough */
    case 0x4A: /* Fallthrough */
    case 0x4B: /* Fallthrough */
    case 0x4C: /* Fallthrough */
    case 0x4D: /* Fallthrough */
    case 0x4E: /* Fallthrough */
    case 0x4F: /* Fallthrough */
    case 0x50: /* Fallthrough */
    case 0x51: /* Fallthrough */
    case 0x52: /* Fallthrough */
    case 0x53: /* Fallthrough */
    case 0x54: /* Fallthrough */
    case 0x55: /* Fallthrough */
    case 0x56: /* Fallthrough */
    case 0x57:
      /* Embedded length byte string */
      {
        uint64_t length = _cbor_load_uint8(source) - 0x40; /* 0x40 offset */
        CLAIM_BYTES_AND_INVOKE(byte_string, length, 0);
        return result;
      }
    case 0x58:
      /* One byte length byte string */
      READ_CLAIM_INVOKE(byte_string, _cbor_load_uint8, 1);
    case 0x59:
      /* Two bytes length byte string */
      READ_CLAIM_INVOKE(byte_string, _cbor_load_uint16, 2);
    case 0x5A:
      /* Four bytes length byte string */
      READ_CLAIM_INVOKE(byte_string, _cbor_load_uint32, 4);
    case 0x5B:
      /* Eight bytes length byte string */
      READ_CLAIM_INVOKE(byte_string, _cbor_load_uint64, 8);
    case 0x5C: /* Fallthrough */
    case 0x5D: /* Fallthrough */
    case 0x5E:
      /* Reserved */
      { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; }
    case 0x5F:
      /* Indefinite byte string */
      {
        callbacks->byte_string_start(context);
        return result;
      }
    case 0x60: /* Fallthrough */
    case 0x61: /* Fallthrough */
    case 0x62: /* Fallthrough */
    case 0x63: /* Fallthrough */
    case 0x64: /* Fallthrough */
    case 0x65: /* Fallthrough */
    case 0x66: /* Fallthrough */
    case 0x67: /* Fallthrough */
    case 0x68: /* Fallthrough */
    case 0x69: /* Fallthrough */
    case 0x6A: /* Fallthrough */
    case 0x6B: /* Fallthrough */
    case 0x6C: /* Fallthrough */
    case 0x6D: /* Fallthrough */
    case 0x6E: /* Fallthrough */
    case 0x6F: /* Fallthrough */
    case 0x70: /* Fallthrough */
    case 0x71: /* Fallthrough */
    case 0x72: /* Fallthrough */
    case 0x73: /* Fallthrough */
    case 0x74: /* Fallthrough */
    case 0x75: /* Fallthrough */
    case 0x76: /* Fallthrough */
    case 0x77:
      /* Embedded one byte length string */
      {
        uint64_t length = _cbor_load_uint8(source) - 0x60; /* 0x60 offset */
        CLAIM_BYTES_AND_INVOKE(string, length, 0);
        return result;
      }
    case 0x78:
      /* One byte length string */
      READ_CLAIM_INVOKE(string, _cbor_load_uint8, 1);
    case 0x79:
      /* Two bytes length string */
      READ_CLAIM_INVOKE(string, _cbor_load_uint16, 2);
    case 0x7A:
      /* Four bytes length string */
      READ_CLAIM_INVOKE(string, _cbor_load_uint32, 4);
    case 0x7B:
      /* Eight bytes length string */
      READ_CLAIM_INVOKE(string, _cbor_load_uint64, 8);
    case 0x7C: /* Fallthrough */
    case 0x7D: /* Fallthrough */
    case 0x7E:
      /* Reserved */
      { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; }
    case 0x7F:
      /* Indefinite length string */
      {
        callbacks->string_start(context);
        return result;
      }
    case 0x80: /* Fallthrough */
    case 0x81: /* Fallthrough */
    case 0x82: /* Fallthrough */
    case 0x83: /* Fallthrough */
    case 0x84: /* Fallthrough */
    case 0x85: /* Fallthrough */
    case 0x86: /* Fallthrough */
    case 0x87: /* Fallthrough */
    case 0x88: /* Fallthrough */
    case 0x89: /* Fallthrough */
    case 0x8A: /* Fallthrough */
    case 0x8B: /* Fallthrough */
    case 0x8C: /* Fallthrough */
    case 0x8D: /* Fallthrough */
    case 0x8E: /* Fallthrough */
    case 0x8F: /* Fallthrough */
    case 0x90: /* Fallthrough */
    case 0x91: /* Fallthrough */
    case 0x92: /* Fallthrough */
    case 0x93: /* Fallthrough */
    case 0x94: /* Fallthrough */
    case 0x95: /* Fallthrough */
    case 0x96: /* Fallthrough */
    case 0x97:
      /* Embedded one byte length array */
      {
        callbacks->array_start(
            context, _cbor_load_uint8(source) - 0x80); /* 0x40 offset */
        return result;
      }
    case 0x98:
      /* One byte length array */
      {
        if (claim_bytes(1, source_size, &result)) {
          callbacks->array_start(context, _cbor_load_uint8(source + 1));
        }
        return result;
      }
    case 0x99:
      /* Two bytes length array */
      {
        if (claim_bytes(2, source_size, &result)) {
          callbacks->array_start(context, _cbor_load_uint16(source + 1));
        }
        return result;
      }
    case 0x9A:
      /* Four bytes length array */
      {
        if (claim_bytes(4, source_size, &result)) {
          callbacks->array_start(context, _cbor_load_uint32(source + 1));
        }
        return result;
      }
    case 0x9B:
      /* Eight bytes length array */
      {
        if (claim_bytes(8, source_size, &result)) {
          callbacks->array_start(context, _cbor_load_uint64(source + 1));
        }
        return result;
      }
    case 0x9C: /* Fallthrough */
    case 0x9D: /* Fallthrough */
    case 0x9E:
      /* Reserved */
      { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; }
    case 0x9F:
      /* Indefinite length array */
      {
        callbacks->indef_array_start(context);
        return result;
      }
    case 0xA0: /* Fallthrough */
    case 0xA1: /* Fallthrough */
    case 0xA2: /* Fallthrough */
    case 0xA3: /* Fallthrough */
    case 0xA4: /* Fallthrough */
    case 0xA5: /* Fallthrough */
    case 0xA6: /* Fallthrough */
    case 0xA7: /* Fallthrough */
    case 0xA8: /* Fallthrough */
    case 0xA9: /* Fallthrough */
    case 0xAA: /* Fallthrough */
    case 0xAB: /* Fallthrough */
    case 0xAC: /* Fallthrough */
    case 0xAD: /* Fallthrough */
    case 0xAE: /* Fallthrough */
    case 0xAF: /* Fallthrough */
    case 0xB0: /* Fallthrough */
    case 0xB1: /* Fallthrough */
    case 0xB2: /* Fallthrough */
    case 0xB3: /* Fallthrough */
    case 0xB4: /* Fallthrough */
    case 0xB5: /* Fallthrough */
    case 0xB6: /* Fallthrough */
    case 0xB7:
      /* Embedded one byte length map */
      {
        callbacks->map_start(context,
                             _cbor_load_uint8(source) - 0xA0); /* 0xA0 offset */
        return result;
      }
    case 0xB8:
      /* One byte length map */
      {
        if (claim_bytes(1, source_size, &result)) {
          callbacks->map_start(context, _cbor_load_uint8(source + 1));
        }
        return result;
      }
    case 0xB9:
      /* Two bytes length map */
      {
        if (claim_bytes(2, source_size, &result)) {
          callbacks->map_start(context, _cbor_load_uint16(source + 1));
        }
        return result;
      }
    case 0xBA:
      /* Four bytes length map */
      {
        if (claim_bytes(4, source_size, &result)) {
          callbacks->map_start(context, _cbor_load_uint32(source + 1));
        }
        return result;
      }
    case 0xBB:
      /* Eight bytes length map */
      {
        if (claim_bytes(8, source_size, &result)) {
          callbacks->map_start(context, _cbor_load_uint64(source + 1));
        }
        return result;
      }
    case 0xBC: /* Fallthrough */
    case 0xBD: /* Fallthrough */
    case 0xBE:
      /* Reserved */
      { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; }
    case 0xBF:
      /* Indefinite length map */
      {
        callbacks->indef_map_start(context);
        return result;
      }
    case 0xC0:
      /* Text date/time - RFC 3339 tag, fallthrough */
    case 0xC1:
      /* Epoch date tag, fallthrough */
    case 0xC2:
      /* Positive bignum tag, fallthrough */
    case 0xC3:
      /* Negative bignum tag, fallthrough */
    case 0xC4:
      /* Fraction, fallthrough */
    case 0xC5:
      /* Big float */
      {
        callbacks->tag(context, (uint64_t)(_cbor_load_uint8(source) -
                                           0xC0)); /* 0xC0 offset */
        return result;
      }
    case 0xC6: /* Fallthrough */
    case 0xC7: /* Fallthrough */
    case 0xC8: /* Fallthrough */
    case 0xC9: /* Fallthrough */
    case 0xCA: /* Fallthrough */
    case 0xCB: /* Fallthrough */
    case 0xCC: /* Fallthrough */
    case 0xCD: /* Fallthrough */
    case 0xCE: /* Fallthrough */
    case 0xCF: /* Fallthrough */
    case 0xD0: /* Fallthrough */
    case 0xD1: /* Fallthrough */
    case 0xD2: /* Fallthrough */
    case 0xD3: /* Fallthrough */
    case 0xD4: /* Unassigned tag value */
    {
      return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR};
    }
    case 0xD5: /* Expected b64url conversion tag - fallthrough */
    case 0xD6: /* Expected b64 conversion tag - fallthrough */
    case 0xD7: /* Expected b16 conversion tag */
    {
      callbacks->tag(context, (uint64_t)(_cbor_load_uint8(source) -
                                         0xC0)); /* 0xC0 offset */
      return result;
    }
    case 0xD8: /* 1B tag */
    {
      if (claim_bytes(1, source_size, &result)) {
        callbacks->tag(context, _cbor_load_uint8(source + 1));
      }
      return result;
    }
    case 0xD9: /* 2B tag */
    {
      if (claim_bytes(2, source_size, &result)) {
        callbacks->tag(context, _cbor_load_uint16(source + 1));
      }
      return result;
    }
    case 0xDA: /* 4B tag */
    {
      if (claim_bytes(4, source_size, &result)) {
        callbacks->tag(context, _cbor_load_uint32(source + 1));
      }
      return result;
    }
    case 0xDB: /* 8B tag */
    {
      if (claim_bytes(8, source_size, &result)) {
        callbacks->tag(context, _cbor_load_uint64(source + 1));
      }
      return result;
    }
    case 0xDC: /* Fallthrough */
    case 0xDD: /* Fallthrough */
    case 0xDE: /* Fallthrough */
    case 0xDF: /* Reserved */
    {
      return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR};
    }
    case 0xE0: /* Fallthrough */
    case 0xE1: /* Fallthrough */
    case 0xE2: /* Fallthrough */
    case 0xE3: /* Fallthrough */
    case 0xE4: /* Fallthrough */
    case 0xE5: /* Fallthrough */
    case 0xE6: /* Fallthrough */
    case 0xE7: /* Fallthrough */
    case 0xE8: /* Fallthrough */
    case 0xE9: /* Fallthrough */
    case 0xEA: /* Fallthrough */
    case 0xEB: /* Fallthrough */
    case 0xEC: /* Fallthrough */
    case 0xED: /* Fallthrough */
    case 0xEE: /* Fallthrough */
    case 0xEF: /* Fallthrough */
    case 0xF0: /* Fallthrough */
    case 0xF1: /* Fallthrough */
    case 0xF2: /* Fallthrough */
    case 0xF3: /* Simple value - unassigned */
    {
      return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR};
    }
    case 0xF4:
      /* False */
      {
        callbacks->boolean(context, false);
        return result;
      }
    case 0xF5:
      /* True */
      {
        callbacks->boolean(context, true);
        return result;
      }
    case 0xF6:
      /* Null */
      {
        callbacks->null(context);
        return result;
      }
    case 0xF7:
      /* Undefined */
      {
        callbacks->undefined(context);
        return result;
      }
    case 0xF8:
      /* 1B simple value, unassigned */
      { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; }
    case 0xF9:
      /* 2B float */
      {
        if (claim_bytes(2, source_size, &result)) {
          callbacks->float2(context, _cbor_load_half(source + 1));
        }
        return result;
      }
    case 0xFA:
      /* 4B float */
      {
        if (claim_bytes(4, source_size, &result)) {
          callbacks->float4(context, _cbor_load_float(source + 1));
        }
        return result;
      }
    case 0xFB:
      /* 8B float */
      {
        if (claim_bytes(8, source_size, &result)) {
          callbacks->float8(context, _cbor_load_double(source + 1));
        }
        return result;
      }
    case 0xFC: /* Fallthrough */
    case 0xFD: /* Fallthrough */
    case 0xFE:
      /* Reserved */
      { return (struct cbor_decoder_result){.status = CBOR_DECODER_ERROR}; }
    case 0xFF:
      /* Break */
      callbacks->indef_break(context);
      // Never happens, the switch statement is exhaustive on the 1B range; make
      // compiler happy
    default:
      return result;
  }
}