forked from zephyrproject-rtos/zcbor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzcbor_decode.c
More file actions
1611 lines (1255 loc) · 38.1 KB
/
zcbor_decode.c
File metadata and controls
1611 lines (1255 loc) · 38.1 KB
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <inttypes.h>
#include "zcbor_decode.h"
#include "zcbor_common.h"
#include "zcbor_print.h"
/** Return value length from additional value.
*/
static size_t additional_len(uint8_t additional)
{
if (additional <= ZCBOR_VALUE_IN_HEADER) {
return 0;
} else if (ZCBOR_VALUE_IS_1_BYTE <= additional && additional <= ZCBOR_VALUE_IS_8_BYTES) {
/* 24 => 1
* 25 => 2
* 26 => 4
* 27 => 8
*/
return 1U << (additional - ZCBOR_VALUE_IS_1_BYTE);
}
return 0xF;
}
static bool initial_checks(zcbor_state_t *state)
{
ZCBOR_CHECK_ERROR();
ZCBOR_CHECK_PAYLOAD();
return true;
}
static bool type_check(zcbor_state_t *state, zcbor_major_type_t exp_major_type)
{
if (!initial_checks(state)) {
ZCBOR_FAIL();
}
zcbor_major_type_t major_type = ZCBOR_MAJOR_TYPE(*state->payload);
if (major_type != exp_major_type) {
ZCBOR_ERR(ZCBOR_ERR_WRONG_TYPE);
}
return true;
}
#define INITIAL_CHECKS() \
do {\
if (!initial_checks(state)) { \
ZCBOR_FAIL(); \
} \
} while(0)
#define INITIAL_CHECKS_WITH_TYPE(exp_major_type) \
do {\
if (!type_check(state, exp_major_type)) { \
ZCBOR_FAIL(); \
} \
} while(0)
static void err_restore(zcbor_state_t *state, int err)
{
state->payload = state->payload_bak;
state->elem_count++;
zcbor_error(state, err);
}
#define ERR_RESTORE(err) \
do { \
err_restore(state, err); \
ZCBOR_FAIL(); \
} while(0)
#define FAIL_RESTORE() \
do { \
state->payload = state->payload_bak; \
state->elem_count++; \
ZCBOR_FAIL(); \
} while(0)
#define PRINT_FUNC() zcbor_log("%s ", __func__);
static void endian_copy(uint8_t *dst, const uint8_t *src, size_t src_len)
{
#ifdef ZCBOR_BIG_ENDIAN
memcpy(dst, src, src_len);
#else
for (size_t i = 0; i < src_len; i++) {
dst[i] = src[src_len - 1 - i];
}
#endif /* ZCBOR_BIG_ENDIAN */
}
/** Get a single value. I.e. a number or simple value, or a list/map/string header.
*
* @details @p state->payload must point to the header byte. This function will
* retrieve the value (either from within the additional info, or from
* the subsequent bytes) and return it in the result. The result can
* have arbitrary length within 1-8 bytes.
*
* The function will also validate
* - That @p state->payload doesn't overrun past @p state->payload_end.
* - That @p state->elem_count has not been exhausted.
* - That the value is encoded in a canonical way (if enforce_canonical
* is enabled).
*
* @p state->payload and @p state->elem_count are updated if the function
* succeeds. If not, they are left unchanged. @p state->payload is updated
* to point to the next byte after the value (for a list/map/tstr/bstr
* this means the first byte of the list/string payload).
* @p state->elem_count is decremented by one.
*
* @p state->payload_bak is updated to point to the start of the value.
*
* CBOR values are always big-endian, so this function converts from
* big to little-endian if necessary (@ref ZCBOR_BIG_ENDIAN).
*
* @param state[inout] The current state of the decoding.
* @param result[out] Pointer to an integer where the result will be stored.
* @param result_len The size of the result integer.
* @param indefinite_length_array[inout]
* As input: NULL if an indefinite length value is not expected.
* As output: Whether the value signifies an indefinite length array.
*
* @return true if the value was successfully extracted, false otherwise.
*/
static bool value_extract(zcbor_state_t *state,
void *const result, size_t result_len, bool *indefinite_length_array)
{
zcbor_trace(state, "value_extract");
zcbor_assert_state(result_len != 0, "0-length result not supported.\r\n");
zcbor_assert_state(result_len <= 8, "result sizes above 8 bytes not supported.\r\n");
zcbor_assert_state(state != NULL, "state cannot be NULL.\r\n");
zcbor_assert_state(result != NULL, "result cannot be NULL.\r\n");
INITIAL_CHECKS();
ZCBOR_ERR_IF((state->elem_count == 0), ZCBOR_ERR_LOW_ELEM_COUNT);
uint8_t header_byte = *state->payload;
uint8_t additional = ZCBOR_ADDITIONAL(header_byte);
size_t len = 0;
if ((additional == ZCBOR_VALUE_IS_INDEFINITE_LENGTH) && (indefinite_length_array != NULL)) {
/* Indefinite length is not allowed in canonical CBOR */
ZCBOR_ERR_IF(ZCBOR_ENFORCE_CANONICAL(state),
ZCBOR_ERR_INVALID_VALUE_ENCODING);
*indefinite_length_array = true;
} else {
len = additional_len(additional);
uint8_t *result_offs = (uint8_t *)result + ZCBOR_ECPY_OFFS(result_len, MAX(1, len));
ZCBOR_ERR_IF(additional > ZCBOR_VALUE_IS_8_BYTES, ZCBOR_ERR_ADDITIONAL_INVAL);
ZCBOR_ERR_IF(len > result_len, ZCBOR_ERR_INT_SIZE);
ZCBOR_ERR_IF((state->payload + len + 1) > state->payload_end,
ZCBOR_ERR_NO_PAYLOAD);
memset(result, 0, result_len);
if (len == 0) {
*result_offs = additional;
} else {
endian_copy(result_offs, state->payload + 1, len);
/* Check whether value could have been encoded shorter.
Only check when enforcing canonical CBOR, and never check floats. */
if (ZCBOR_ENFORCE_CANONICAL(state) && !ZCBOR_IS_FLOAT(header_byte)) {
ZCBOR_ERR_IF((zcbor_header_len_ptr(result, result_len) != (len + 1)),
ZCBOR_ERR_INVALID_VALUE_ENCODING);
}
}
}
state->payload_bak = state->payload;
(state->payload) += len + 1;
(state->elem_count)--;
return true;
}
static int val_to_int(zcbor_major_type_t major_type, void *result, size_t result_size)
{
#ifdef ZCBOR_BIG_ENDIAN
int8_t msbyte = *(int8_t *)result;
#else
int8_t msbyte = ((int8_t *)result)[result_size - 1];
#endif
uint8_t *result_uint8 = (uint8_t *)result;
if ((result_size == 0) || (msbyte < 0)) {
/* Value is too large to fit in a signed integer. */
return ZCBOR_ERR_INT_SIZE;
}
if (major_type == ZCBOR_MAJOR_TYPE_NINT) {
/* Convert from CBOR's representation by flipping all bits. */
for (unsigned int i = 0; i < result_size; i++) {
result_uint8[i] = (uint8_t)~result_uint8[i];
}
}
return ZCBOR_SUCCESS;
}
bool zcbor_int_decode(zcbor_state_t *state, void *result, size_t result_size)
{
PRINT_FUNC();
INITIAL_CHECKS();
zcbor_major_type_t major_type = ZCBOR_MAJOR_TYPE(*state->payload);
if (major_type != ZCBOR_MAJOR_TYPE_PINT
&& major_type != ZCBOR_MAJOR_TYPE_NINT) {
/* Value to be read doesn't have the right type. */
ZCBOR_ERR(ZCBOR_ERR_WRONG_TYPE);
}
if (!value_extract(state, result, result_size, NULL)) {
ZCBOR_FAIL();
}
int err = val_to_int(major_type, result, result_size);
if (err != ZCBOR_SUCCESS) {
ERR_RESTORE(err);
}
return true;
}
bool zcbor_int32_decode(zcbor_state_t *state, int32_t *result)
{
PRINT_FUNC();
return zcbor_int_decode(state, result, sizeof(*result));
}
bool zcbor_int64_decode(zcbor_state_t *state, int64_t *result)
{
PRINT_FUNC();
return zcbor_int_decode(state, result, sizeof(*result));
}
bool zcbor_uint_decode(zcbor_state_t *state, void *result, size_t result_size)
{
PRINT_FUNC();
INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PINT);
if (!value_extract(state, result, result_size, NULL)) {
zcbor_log("uint with size %zu failed.\r\n", result_size);
ZCBOR_FAIL();
}
return true;
}
bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result)
{
PRINT_FUNC();
return zcbor_uint_decode(state, result, sizeof(*result));
}
bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t result)
{
PRINT_FUNC();
if (!zcbor_union_elem_code(state)) {
ZCBOR_FAIL();
}
return zcbor_int32_expect(state, result);
}
bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t result)
{
PRINT_FUNC();
if (!zcbor_union_elem_code(state)) {
ZCBOR_FAIL();
}
return zcbor_int64_expect(state, result);
}
bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t result)
{
PRINT_FUNC();
if (!zcbor_union_elem_code(state)) {
ZCBOR_FAIL();
}
return zcbor_uint32_expect(state, result);
}
bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t result)
{
PRINT_FUNC();
if (!zcbor_union_elem_code(state)) {
ZCBOR_FAIL();
}
return zcbor_uint64_expect(state, result);
}
bool zcbor_int32_expect(zcbor_state_t *state, int32_t expected)
{
PRINT_FUNC();
return zcbor_int64_expect(state, expected);
}
bool zcbor_int32_pexpect(zcbor_state_t *state, int32_t *expected)
{
PRINT_FUNC();
return zcbor_int32_expect(state, *expected);
}
bool zcbor_int64_expect(zcbor_state_t *state, int64_t expected)
{
PRINT_FUNC();
int64_t actual;
if (!zcbor_int64_decode(state, &actual)) {
ZCBOR_FAIL();
}
if (actual != expected) {
zcbor_log("%" PRIi64 " != %" PRIi64 "\r\n", actual, expected);
ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
}
return true;
}
bool zcbor_int64_pexpect(zcbor_state_t *state, int64_t *expected)
{
PRINT_FUNC();
return zcbor_int64_expect(state, *expected);
}
bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result)
{
PRINT_FUNC();
return zcbor_uint_decode(state, result, sizeof(*result));
}
#ifdef ZCBOR_SUPPORTS_SIZE_T
bool zcbor_size_decode(zcbor_state_t *state, size_t *result)
{
PRINT_FUNC();
return zcbor_uint_decode(state, result, sizeof(*result));
}
#endif
bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t expected)
{
PRINT_FUNC();
return zcbor_uint64_expect(state, expected);
}
bool zcbor_uint32_pexpect(zcbor_state_t *state, uint32_t *expected)
{
PRINT_FUNC();
return zcbor_uint32_expect(state, *expected);
}
bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t expected)
{
PRINT_FUNC();
uint64_t actual;
if (!zcbor_uint64_decode(state, &actual)) {
ZCBOR_FAIL();
}
if (actual != expected) {
zcbor_log("%" PRIu64 " != %" PRIu64 "\r\n", actual, expected);
ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
}
return true;
}
bool zcbor_uint64_pexpect(zcbor_state_t *state, uint64_t *expected)
{
PRINT_FUNC();
return zcbor_uint64_expect(state, *expected);
}
#ifdef ZCBOR_SUPPORTS_SIZE_T
bool zcbor_size_expect(zcbor_state_t *state, size_t expected)
{
PRINT_FUNC();
return zcbor_uint64_expect(state, expected);
}
bool zcbor_size_pexpect(zcbor_state_t *state, size_t *expected)
{
PRINT_FUNC();
return zcbor_size_expect(state, *expected);
}
#endif
static bool str_start_decode(zcbor_state_t *state,
struct zcbor_string *result, zcbor_major_type_t exp_major_type)
{
INITIAL_CHECKS_WITH_TYPE(exp_major_type);
if (!value_extract(state, &result->len, sizeof(result->len), NULL)) {
ZCBOR_FAIL();
}
result->value = state->payload;
return true;
}
/**
* @brief Checks if there is a potential overflow when decoding a string.
*
* This function is used to check if the payload contains the whole string.
* The payload in the state must point to the start of the string payload, the
* length having already been extracted from the header via @ref value_extract or
* @ref str_start_decode.
*
* @param state The current state of the decoding.
* @param len The decoded length of the string.
* @return true if ok (no overflow), false otherwise.
*/
static bool str_overflow_check(zcbor_state_t *state, size_t len)
{
/* Casting to size_t is safe since value_extract() checks that
* payload_end is bigger that payload. */
if (len > (size_t)(state->payload_end - state->payload)) {
zcbor_log("error: 0x%zu > 0x%zu\r\n",
len,
(state->payload_end - state->payload));
ERR_RESTORE(ZCBOR_ERR_NO_PAYLOAD);
}
return true;
}
static bool str_start_decode_with_overflow_check(zcbor_state_t *state,
struct zcbor_string *result, zcbor_major_type_t exp_major_type)
{
bool res = str_start_decode(state, result, exp_major_type);
if (!res || !str_overflow_check(state, result->len)) {
ZCBOR_FAIL();
}
return true;
}
bool zcbor_bstr_start_decode(zcbor_state_t *state, struct zcbor_string *result)
{
PRINT_FUNC();
struct zcbor_string dummy;
if (result == NULL) {
result = &dummy;
}
if(!str_start_decode_with_overflow_check(state, result, ZCBOR_MAJOR_TYPE_BSTR)) {
ZCBOR_FAIL();
}
if (!zcbor_new_backup(state, ZCBOR_MAX_ELEM_COUNT)) {
FAIL_RESTORE();
}
state->payload_end = result->value + result->len;
return true;
}
bool zcbor_bstr_end_decode(zcbor_state_t *state)
{
ZCBOR_ERR_IF(state->payload != state->payload_end, ZCBOR_ERR_PAYLOAD_NOT_CONSUMED);
if (!zcbor_process_backup(state,
ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_CONSUME | ZCBOR_FLAG_KEEP_PAYLOAD,
ZCBOR_MAX_ELEM_COUNT)) {
ZCBOR_FAIL();
}
return true;
}
static void partition_fragment(const zcbor_state_t *state,
struct zcbor_string_fragment *result)
{
result->fragment.len = MIN(result->fragment.len,
(size_t)state->payload_end - (size_t)state->payload);
}
static bool start_decode_fragment(zcbor_state_t *state,
struct zcbor_string_fragment *result,
zcbor_major_type_t exp_major_type)
{
PRINT_FUNC();
if(!str_start_decode(state, &result->fragment, exp_major_type)) {
ZCBOR_FAIL();
}
result->offset = 0;
result->total_len = result->fragment.len;
partition_fragment(state, result);
state->payload_end = state->payload + result->fragment.len;
return true;
}
bool zcbor_bstr_start_decode_fragment(zcbor_state_t *state,
struct zcbor_string_fragment *result)
{
PRINT_FUNC();
if (!start_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR)) {
ZCBOR_FAIL();
}
if (!zcbor_new_backup(state, ZCBOR_MAX_ELEM_COUNT)) {
FAIL_RESTORE();
}
return true;
}
void zcbor_next_fragment(zcbor_state_t *state,
struct zcbor_string_fragment *prev_fragment,
struct zcbor_string_fragment *result)
{
memcpy(result, prev_fragment, sizeof(*result));
result->fragment.value = state->payload_mut;
result->offset += prev_fragment->fragment.len;
result->fragment.len = result->total_len - result->offset;
partition_fragment(state, result);
zcbor_log("New fragment length %zu\r\n", result->fragment.len);
state->payload += result->fragment.len;
}
void zcbor_bstr_next_fragment(zcbor_state_t *state,
struct zcbor_string_fragment *prev_fragment,
struct zcbor_string_fragment *result)
{
memcpy(result, prev_fragment, sizeof(*result));
result->fragment.value = state->payload_mut;
result->offset += prev_fragment->fragment.len;
result->fragment.len = result->total_len - result->offset;
partition_fragment(state, result);
zcbor_log("fragment length %zu\r\n", result->fragment.len);
state->payload_end = state->payload + result->fragment.len;
}
bool zcbor_is_last_fragment(const struct zcbor_string_fragment *fragment)
{
return (fragment->total_len == (fragment->offset + fragment->fragment.len));
}
static bool str_decode(zcbor_state_t *state, struct zcbor_string *result,
zcbor_major_type_t exp_major_type)
{
if (!str_start_decode_with_overflow_check(state, result, exp_major_type)) {
ZCBOR_FAIL();
}
state->payload += result->len;
return true;
}
static bool str_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result,
zcbor_major_type_t exp_major_type)
{
if (!start_decode_fragment(state, result, exp_major_type)) {
ZCBOR_FAIL();
}
(state->payload) += result->fragment.len;
return true;
}
static bool str_expect(zcbor_state_t *state, struct zcbor_string *result,
zcbor_major_type_t exp_major_type)
{
struct zcbor_string tmp_result;
if (!str_decode(state, &tmp_result, exp_major_type)) {
ZCBOR_FAIL();
}
if (!zcbor_compare_strings(&tmp_result, result)) {
ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
}
return true;
}
bool zcbor_bstr_decode(zcbor_state_t *state, struct zcbor_string *result)
{
PRINT_FUNC();
return str_decode(state, result, ZCBOR_MAJOR_TYPE_BSTR);
}
bool zcbor_bstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result)
{
PRINT_FUNC();
return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR);
}
bool zcbor_bstr_expect(zcbor_state_t *state, struct zcbor_string *expected)
{
PRINT_FUNC();
return str_expect(state, expected, ZCBOR_MAJOR_TYPE_BSTR);
}
bool zcbor_tstr_decode(zcbor_state_t *state, struct zcbor_string *result)
{
PRINT_FUNC();
return str_decode(state, result, ZCBOR_MAJOR_TYPE_TSTR);
}
bool zcbor_tstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result)
{
PRINT_FUNC();
return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_TSTR);
}
bool zcbor_tstr_expect(zcbor_state_t *state, struct zcbor_string *expected)
{
PRINT_FUNC();
return str_expect(state, expected, ZCBOR_MAJOR_TYPE_TSTR);
}
bool zcbor_bstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len)
{
PRINT_FUNC();
struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
return zcbor_bstr_expect(state, &zs);
}
bool zcbor_tstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len)
{
PRINT_FUNC();
struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
return zcbor_tstr_expect(state, &zs);
}
bool zcbor_bstr_expect_term(zcbor_state_t *state, char const *string, size_t maxlen)
{
PRINT_FUNC();
return zcbor_bstr_expect_ptr(state, string, strnlen(string, maxlen));
}
bool zcbor_tstr_expect_term(zcbor_state_t *state, char const *string, size_t maxlen)
{
PRINT_FUNC();
return zcbor_tstr_expect_ptr(state, string, strnlen(string, maxlen));
}
static bool list_map_start_decode(zcbor_state_t *state,
zcbor_major_type_t exp_major_type)
{
size_t new_elem_count;
bool indefinite_length_array = false;
INITIAL_CHECKS_WITH_TYPE(exp_major_type);
if (!value_extract(state, &new_elem_count, sizeof(new_elem_count), &indefinite_length_array)) {
ZCBOR_FAIL();
}
if (!zcbor_new_backup(state, indefinite_length_array
? ZCBOR_LARGE_ELEM_COUNT : new_elem_count)) {
FAIL_RESTORE();
}
state->decode_state.indefinite_length_array = indefinite_length_array;
return true;
}
bool zcbor_list_start_decode(zcbor_state_t *state)
{
PRINT_FUNC();
return list_map_start_decode(state, ZCBOR_MAJOR_TYPE_LIST);
}
bool zcbor_map_start_decode(zcbor_state_t *state)
{
PRINT_FUNC();
bool ret = list_map_start_decode(state, ZCBOR_MAJOR_TYPE_MAP);
if (ret && !state->decode_state.indefinite_length_array) {
if (state->elem_count >= (ZCBOR_MAX_ELEM_COUNT / 2)) {
/* The new elem_count is too large. */
ERR_RESTORE(ZCBOR_ERR_INT_SIZE);
}
state->elem_count *= 2;
}
return ret;
}
bool zcbor_array_at_end(zcbor_state_t *state)
{
const bool indefinite_length_array = state->decode_state.indefinite_length_array;
return ((!indefinite_length_array && (state->elem_count == 0))
|| (indefinite_length_array
&& (state->payload < state->payload_end)
&& (*state->payload == 0xFF)));
}
static size_t update_map_elem_count(zcbor_state_t *state, size_t elem_count);
#ifdef ZCBOR_MAP_SMART_SEARCH
static bool allocate_map_flags(zcbor_state_t *state, size_t elem_count);
#endif
bool zcbor_unordered_map_start_decode(zcbor_state_t *state)
{
PRINT_FUNC();
ZCBOR_FAIL_IF(!zcbor_map_start_decode(state));
#ifdef ZCBOR_MAP_SMART_SEARCH
state->decode_state.map_search_elem_state
+= zcbor_flags_to_bytes(state->decode_state.map_elem_count);
#else
state->decode_state.map_elems_processed = 0;
#endif
state->decode_state.map_elem_count = 0;
state->decode_state.counting_map_elems = state->decode_state.indefinite_length_array;
if (!state->decode_state.counting_map_elems) {
size_t old_flags = update_map_elem_count(state, state->elem_count);
#ifdef ZCBOR_MAP_SMART_SEARCH
ZCBOR_FAIL_IF(!allocate_map_flags(state, old_flags));
#endif
(void)old_flags;
}
return true;
}
/** Return the max (starting) elem_count of the current container.
*
* Should only be used for unordered maps (started with @ref zcbor_unordered_map_start_decode)
*/
static size_t zcbor_current_max_elem_count(zcbor_state_t *state)
{
return (state->decode_state.indefinite_length_array ? \
ZCBOR_LARGE_ELEM_COUNT : state->decode_state.map_elem_count * 2);
}
static bool map_restart(zcbor_state_t *state)
{
if (!zcbor_process_backup(state, ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_KEEP_DECODE_STATE,
ZCBOR_MAX_ELEM_COUNT)) {
ZCBOR_FAIL();
}
state->elem_count = zcbor_current_max_elem_count(state);
return true;
}
__attribute__((used))
static size_t get_current_index(zcbor_state_t *state, uint32_t index_offset)
{
/* Subtract mode because for GET, you want the index you are pointing to, while for SET,
* you want the one you just processed. This only comes into play when elem_count is even. */
return ((zcbor_current_max_elem_count(state) - state->elem_count - index_offset) / 2);
}
#ifdef ZCBOR_MAP_SMART_SEARCH
#define FLAG_MODE_GET_CURRENT 0
#define FLAG_MODE_CLEAR_CURRENT 1
#define FLAG_MODE_CLEAR_UNUSED 2
static bool manipulate_flags(zcbor_state_t *state, uint32_t mode)
{
const size_t last_index = (state->decode_state.map_elem_count - 1);
size_t index = (mode == FLAG_MODE_CLEAR_UNUSED) ? last_index : get_current_index(state, mode);
ZCBOR_ERR_IF((index >= state->decode_state.map_elem_count),
ZCBOR_ERR_MAP_FLAGS_NOT_AVAILABLE);
uint8_t *flag_byte = &state->decode_state.map_search_elem_state[index >> 3];
uint8_t flag_mask = (uint8_t)(1 << (index & 7));
switch(mode) {
case FLAG_MODE_GET_CURRENT:
return (!!(*flag_byte & flag_mask));
case FLAG_MODE_CLEAR_CURRENT:
*flag_byte &= ~flag_mask;
return true;
case FLAG_MODE_CLEAR_UNUSED:
*flag_byte &= (uint8_t)((flag_mask << 1) - 1);
return true;
}
return false;
}
static bool should_try_key(zcbor_state_t *state)
{
return manipulate_flags(state, FLAG_MODE_GET_CURRENT);
}
bool zcbor_elem_processed(zcbor_state_t *state)
{
return manipulate_flags(state, FLAG_MODE_CLEAR_CURRENT);
}
static bool allocate_map_flags(zcbor_state_t *state, size_t old_flags)
{
size_t new_bytes = zcbor_flags_to_bytes(state->decode_state.map_elem_count);
size_t old_bytes = zcbor_flags_to_bytes(old_flags);
size_t extra_bytes = new_bytes - old_bytes;
ZCBOR_ERR_IF(!state->constant_state, ZCBOR_ERR_CONSTANT_STATE_MISSING);
const uint8_t *flags_end = state->constant_state->map_search_elem_state_end;
if (extra_bytes) {
if ((state->decode_state.map_search_elem_state + new_bytes) > flags_end) {
state->decode_state.map_elem_count
= 8 * (size_t)(flags_end - state->decode_state.map_search_elem_state);
ZCBOR_ERR(ZCBOR_ERR_MAP_FLAGS_NOT_AVAILABLE);
}
memset(&state->decode_state.map_search_elem_state[new_bytes - extra_bytes], 0xFF, extra_bytes);
}
return true;
}
#else
static bool should_try_key(zcbor_state_t *state)
{
return (state->decode_state.map_elems_processed < state->decode_state.map_elem_count);
}
bool zcbor_elem_processed(zcbor_state_t *state)
{
if (should_try_key(state)) {
state->decode_state.map_elems_processed++;
}
return true;
}
#endif
static size_t update_map_elem_count(zcbor_state_t *state, size_t elem_count)
{
size_t old_map_elem_count = state->decode_state.map_elem_count;
state->decode_state.map_elem_count = MAX(old_map_elem_count, elem_count / 2);
return old_map_elem_count;
}
static bool handle_map_end(zcbor_state_t *state)
{
state->decode_state.counting_map_elems = false;
return map_restart(state);
}
static bool try_key(zcbor_state_t *state, void *key_result, zcbor_decoder_t key_decoder)
{
uint8_t const *payload_bak2 = state->payload;
size_t elem_count_bak = state->elem_count;
if (!key_decoder(state, (uint8_t *)key_result)) {
state->payload = payload_bak2;
state->elem_count = elem_count_bak;
return false;
}
zcbor_log("Found element at index %zu.\n", get_current_index(state, 1));
return true;
}
bool zcbor_unordered_map_search(zcbor_decoder_t key_decoder, zcbor_state_t *state, void *key_result)
{
PRINT_FUNC();
/* elem_count cannot be odd since the map consists of key-value-pairs.
* This might mean that this function was called while pointing at a value (instead
* of a key). */
ZCBOR_ERR_IF(state->elem_count & 1, ZCBOR_ERR_MAP_MISALIGNED);
uint8_t const *payload_bak = state->payload;
size_t elem_count = state->elem_count;
/* Loop once through all the elements of the map. */
do {
if (zcbor_array_at_end(state)) {
if (!handle_map_end(state)) {
goto error;
}
continue; /* This continue is needed so the loop stops both if elem_count is
* at the very start or the very end of the map. */
}
if (state->decode_state.counting_map_elems) {
size_t m_elem_count = ZCBOR_LARGE_ELEM_COUNT - state->elem_count + 2;
size_t old_flags = update_map_elem_count(state, m_elem_count);
#ifdef ZCBOR_MAP_SMART_SEARCH
ZCBOR_FAIL_IF(!allocate_map_flags(state, old_flags));
#endif
(void)old_flags;
}
if (should_try_key(state) && try_key(state, key_result, key_decoder)) {
if (!ZCBOR_MANUALLY_PROCESS_ELEM(state)) {
ZCBOR_FAIL_IF(!zcbor_elem_processed(state));
}
return true;
}
/* Skip over both the key and the value. */
if (!zcbor_any_skip(state, NULL) || !zcbor_any_skip(state, NULL)) {
goto error;
}
} while (state->elem_count != elem_count);
zcbor_error(state, ZCBOR_ERR_ELEM_NOT_FOUND);
error:
state->payload = payload_bak;
state->elem_count = elem_count;
ZCBOR_FAIL();
}
bool zcbor_search_key_bstr_ptr(zcbor_state_t *state, char const *ptr, size_t len)
{
struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
return zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_bstr_expect, state, &zs);
}
bool zcbor_search_key_tstr_ptr(zcbor_state_t *state, char const *ptr, size_t len)
{
struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
return zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_tstr_expect, state, &zs);
}
bool zcbor_search_key_bstr_term(zcbor_state_t *state, char const *str, size_t maxlen)
{