1 /*
2 * Copyright (c) 2020 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdint.h>
8 #include <stdbool.h>
9 #include <stddef.h>
10 #include <string.h>
11 #include "zcbor_decode.h"
12 #include "zcbor_common.h"
13
14
15 /** Return value length from additional value.
16 */
additional_len(uint8_t additional)17 static size_t additional_len(uint8_t additional)
18 {
19 if (ZCBOR_VALUE_IS_1_BYTE <= additional && additional <= ZCBOR_VALUE_IS_8_BYTES) {
20 /* 24 => 1
21 * 25 => 2
22 * 26 => 4
23 * 27 => 8
24 */
25 return 1U << (additional - ZCBOR_VALUE_IS_1_BYTE);
26 }
27 return 0;
28 }
29
30 /** Extract the major type, i.e. the first 3 bits of the header byte. */
31 #define MAJOR_TYPE(header_byte) ((zcbor_major_type_t)(((header_byte) >> 5) & 0x7))
32
33 /** Extract the additional info, i.e. the last 5 bits of the header byte. */
34 #define ADDITIONAL(header_byte) ((header_byte) & 0x1F)
35
36
37 #define FAIL_AND_DECR_IF(expr, err) \
38 do {\
39 if (expr) { \
40 (state->payload)--; \
41 ZCBOR_ERR(err); \
42 } \
43 } while(0)
44
45
initial_checks(zcbor_state_t * state)46 static bool initial_checks(zcbor_state_t *state)
47 {
48 ZCBOR_CHECK_ERROR();
49 ZCBOR_CHECK_PAYLOAD();
50 return true;
51 }
52
53
type_check(zcbor_state_t * state,zcbor_major_type_t exp_major_type)54 static bool type_check(zcbor_state_t *state, zcbor_major_type_t exp_major_type)
55 {
56 if (!initial_checks(state)) {
57 ZCBOR_FAIL();
58 }
59 zcbor_major_type_t major_type = MAJOR_TYPE(*state->payload);
60
61 if (major_type != exp_major_type) {
62 ZCBOR_ERR(ZCBOR_ERR_WRONG_TYPE);
63 }
64 return true;
65 }
66
67
68 #define INITIAL_CHECKS() \
69 do {\
70 if (!initial_checks(state)) { \
71 ZCBOR_FAIL(); \
72 } \
73 } while(0)
74
75 #define INITIAL_CHECKS_WITH_TYPE(exp_major_type) \
76 do {\
77 if (!type_check(state, exp_major_type)) { \
78 ZCBOR_FAIL(); \
79 } \
80 } while(0)
81
err_restore(zcbor_state_t * state,int err)82 static void err_restore(zcbor_state_t *state, int err)
83 {
84 state->payload = state->payload_bak;
85 state->elem_count++;
86 zcbor_error(state, err);
87 }
88
89 #define ERR_RESTORE(err) \
90 do { \
91 err_restore(state, err); \
92 ZCBOR_FAIL(); \
93 } while(0)
94
95 #define FAIL_RESTORE() \
96 do { \
97 state->payload = state->payload_bak; \
98 state->elem_count++; \
99 ZCBOR_FAIL(); \
100 } while(0)
101
102
103 /** Get a single value.
104 *
105 * @details @p ppayload must point to the header byte. This function will
106 * retrieve the value (either from within the additional info, or from
107 * the subsequent bytes) and return it in the result. The result can
108 * have arbitrary length.
109 *
110 * The function will also validate
111 * - Min/max constraints on the value.
112 * - That @p payload doesn't overrun past @p payload_end.
113 * - That @p elem_count has not been exhausted.
114 *
115 * @p ppayload and @p elem_count are updated if the function
116 * succeeds. If not, they are left unchanged.
117 *
118 * CBOR values are always big-endian, so this function converts from
119 * big to little-endian if necessary (@ref CONFIG_BIG_ENDIAN).
120 */
value_extract(zcbor_state_t * state,void * const result,size_t result_len)121 static bool value_extract(zcbor_state_t *state,
122 void *const result, size_t result_len)
123 {
124 zcbor_trace();
125 zcbor_assert_state(result_len != 0, "0-length result not supported.\r\n");
126 zcbor_assert_state(result != NULL, NULL);
127
128 INITIAL_CHECKS();
129 ZCBOR_ERR_IF((state->elem_count == 0), ZCBOR_ERR_LOW_ELEM_COUNT);
130
131 uint8_t *u8_result = (uint8_t *)result;
132 uint8_t additional = ADDITIONAL(*state->payload);
133
134 state->payload_bak = state->payload;
135 (state->payload)++;
136
137 memset(result, 0, result_len);
138 if (additional <= ZCBOR_VALUE_IN_HEADER) {
139 #ifdef CONFIG_BIG_ENDIAN
140 u8_result[result_len - 1] = additional;
141 #else
142 u8_result[0] = additional;
143 #endif /* CONFIG_BIG_ENDIAN */
144 } else {
145 size_t len = additional_len(additional);
146
147 FAIL_AND_DECR_IF(len > result_len, ZCBOR_ERR_INT_SIZE);
148 FAIL_AND_DECR_IF(len == 0, ZCBOR_ERR_ADDITIONAL_INVAL); // additional_len() did not recognize the additional value.
149 FAIL_AND_DECR_IF((state->payload + len) > state->payload_end,
150 ZCBOR_ERR_NO_PAYLOAD);
151
152 #ifdef CONFIG_BIG_ENDIAN
153 memcpy(&u8_result[result_len - len], state->payload, len);
154 #else
155 for (size_t i = 0; i < len; i++) {
156 u8_result[i] = (state->payload)[len - i - 1];
157 }
158 #endif /* CONFIG_BIG_ENDIAN */
159
160 (state->payload) += len;
161 }
162
163 (state->elem_count)--;
164 return true;
165 }
166
167
zcbor_int_decode(zcbor_state_t * state,void * result_int,size_t int_size)168 bool zcbor_int_decode(zcbor_state_t *state, void *result_int, size_t int_size)
169 {
170 INITIAL_CHECKS();
171 zcbor_major_type_t major_type = MAJOR_TYPE(*state->payload);
172 uint8_t *result_uint8 = (uint8_t *)result_int;
173 int8_t *result_int8 = (int8_t *)result_int;
174
175 if (major_type != ZCBOR_MAJOR_TYPE_PINT
176 && major_type != ZCBOR_MAJOR_TYPE_NINT) {
177 /* Value to be read doesn't have the right type. */
178 ZCBOR_ERR(ZCBOR_ERR_WRONG_TYPE);
179 }
180
181 if (!value_extract(state, result_int, int_size)) {
182 ZCBOR_FAIL();
183 }
184
185 #ifdef CONFIG_BIG_ENDIAN
186 if (result_int8[0] < 0) {
187 #else
188 if (result_int8[int_size - 1] < 0) {
189 #endif
190 /* Value is too large to fit in a signed integer. */
191 ERR_RESTORE(ZCBOR_ERR_INT_SIZE);
192 }
193
194 if (major_type == ZCBOR_MAJOR_TYPE_NINT) {
195 /* Convert from CBOR's representation by flipping all bits. */
196 for (unsigned int i = 0; i < int_size; i++) {
197 result_uint8[i] = (uint8_t)~result_uint8[i];
198 }
199 }
200
201 return true;
202 }
203
204
205 bool zcbor_int32_decode(zcbor_state_t *state, int32_t *result)
206 {
207 return zcbor_int_decode(state, result, sizeof(*result));
208 }
209
210
211 bool zcbor_int64_decode(zcbor_state_t *state, int64_t *result)
212 {
213 return zcbor_int_decode(state, result, sizeof(*result));
214 }
215
216
217 bool zcbor_uint_decode(zcbor_state_t *state, void *result_uint, size_t uint_size)
218 {
219 INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PINT);
220
221 if (!value_extract(state, result_uint, uint_size)) {
222 zcbor_print("uint with size %d failed.\r\n", uint_size);
223 ZCBOR_FAIL();
224 }
225 return true;
226 }
227
228
229 bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result)
230 {
231 return zcbor_uint_decode(state, result, sizeof(*result));
232 }
233
234
235 bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t result)
236 {
237 if (!zcbor_union_elem_code(state)) {
238 ZCBOR_FAIL();
239 }
240 return zcbor_int32_expect(state, result);
241 }
242
243
244 bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t result)
245 {
246 if (!zcbor_union_elem_code(state)) {
247 ZCBOR_FAIL();
248 }
249 return zcbor_int64_expect(state, result);
250 }
251
252
253 bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t result)
254 {
255 if (!zcbor_union_elem_code(state)) {
256 ZCBOR_FAIL();
257 }
258 return zcbor_uint32_expect(state, result);
259 }
260
261
262 bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t result)
263 {
264 if (!zcbor_union_elem_code(state)) {
265 ZCBOR_FAIL();
266 }
267 return zcbor_uint64_expect(state, result);
268 }
269
270
271 bool zcbor_int32_expect(zcbor_state_t *state, int32_t result)
272 {
273 return zcbor_int64_expect(state, result);
274 }
275
276
277 bool zcbor_int64_expect(zcbor_state_t *state, int64_t result)
278 {
279 int64_t value;
280
281 if (!zcbor_int64_decode(state, &value)) {
282 ZCBOR_FAIL();
283 }
284
285 if (value != result) {
286 zcbor_print("%" PRIi64 " != %" PRIi64 "\r\n", value, result);
287 ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
288 }
289 return true;
290 }
291
292
293 bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result)
294 {
295 return zcbor_uint_decode(state, result, sizeof(*result));
296 }
297
298
299 #ifdef ZCBOR_SUPPORTS_SIZE_T
300 bool zcbor_size_decode(zcbor_state_t *state, size_t *result)
301 {
302 return zcbor_uint_decode(state, result, sizeof(*result));
303 }
304 #endif
305
306
307 bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t result)
308 {
309 return zcbor_uint64_expect(state, result);
310 }
311
312
313 bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t result)
314 {
315 uint64_t value;
316
317 if (!zcbor_uint64_decode(state, &value)) {
318 ZCBOR_FAIL();
319 }
320 if (value != result) {
321 zcbor_print("%" PRIu64 " != %" PRIu64 "\r\n", value, result);
322 ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
323 }
324 return true;
325 }
326
327
328 #ifdef ZCBOR_SUPPORTS_SIZE_T
329 bool zcbor_size_expect(zcbor_state_t *state, size_t result)
330 {
331 return zcbor_uint64_expect(state, result);
332 }
333 #endif
334
335
336 static bool str_start_decode(zcbor_state_t *state,
337 struct zcbor_string *result, zcbor_major_type_t exp_major_type)
338 {
339 INITIAL_CHECKS_WITH_TYPE(exp_major_type);
340
341 if (!value_extract(state, &result->len, sizeof(result->len))) {
342 ZCBOR_FAIL();
343 }
344
345 result->value = state->payload;
346 return true;
347 }
348
349 static bool str_start_decode_with_overflow_check(zcbor_state_t *state,
350 struct zcbor_string *result, zcbor_major_type_t exp_major_type)
351 {
352 bool res = str_start_decode(state, result, exp_major_type);
353
354 if (!res) {
355 ZCBOR_FAIL();
356 }
357
358 /* Casting to size_t is safe since str_start_decode() checks that
359 * payload_end is bigger that payload. */
360 if (result->len > (size_t)(state->payload_end - state->payload)) {
361 zcbor_print("error: 0x%zu > 0x%zu\r\n",
362 result->len,
363 (state->payload_end - state->payload));
364 ERR_RESTORE(ZCBOR_ERR_NO_PAYLOAD);
365 }
366
367 return true;
368 }
369
370
371 bool zcbor_bstr_start_decode(zcbor_state_t *state, struct zcbor_string *result)
372 {
373 struct zcbor_string dummy;
374 if (result == NULL) {
375 result = &dummy;
376 }
377
378 if(!str_start_decode_with_overflow_check(state, result, ZCBOR_MAJOR_TYPE_BSTR)) {
379 ZCBOR_FAIL();
380 }
381
382 if (!zcbor_new_backup(state, ZCBOR_MAX_ELEM_COUNT)) {
383 FAIL_RESTORE();
384 }
385
386 state->payload_end = result->value + result->len;
387 return true;
388 }
389
390
391 bool zcbor_bstr_end_decode(zcbor_state_t *state)
392 {
393 ZCBOR_ERR_IF(state->payload != state->payload_end, ZCBOR_ERR_PAYLOAD_NOT_CONSUMED);
394
395 if (!zcbor_process_backup(state,
396 ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_CONSUME | ZCBOR_FLAG_TRANSFER_PAYLOAD,
397 ZCBOR_MAX_ELEM_COUNT)) {
398 ZCBOR_FAIL();
399 }
400
401 return true;
402 }
403
404
405 static void partition_fragment(const zcbor_state_t *state,
406 struct zcbor_string_fragment *result)
407 {
408 result->fragment.len = MIN(result->fragment.len,
409 (size_t)state->payload_end - (size_t)state->payload);
410 }
411
412
413 static bool start_decode_fragment(zcbor_state_t *state,
414 struct zcbor_string_fragment *result,
415 zcbor_major_type_t exp_major_type)
416 {
417 if(!str_start_decode(state, &result->fragment, exp_major_type)) {
418 ZCBOR_FAIL();
419 }
420
421 result->offset = 0;
422 result->total_len = result->fragment.len;
423 partition_fragment(state, result);
424 state->payload_end = state->payload + result->fragment.len;
425
426 return true;
427 }
428
429 bool zcbor_bstr_start_decode_fragment(zcbor_state_t *state,
430 struct zcbor_string_fragment *result)
431 {
432 if (!start_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR)) {
433 ZCBOR_FAIL();
434 }
435 if (!zcbor_new_backup(state, ZCBOR_MAX_ELEM_COUNT)) {
436 FAIL_RESTORE();
437 }
438 return true;
439 }
440
441
442 void zcbor_next_fragment(zcbor_state_t *state,
443 struct zcbor_string_fragment *prev_fragment,
444 struct zcbor_string_fragment *result)
445 {
446 memcpy(result, prev_fragment, sizeof(*result));
447 result->fragment.value = state->payload_mut;
448 result->offset += prev_fragment->fragment.len;
449 result->fragment.len = result->total_len - result->offset;
450
451 partition_fragment(state, result);
452 zcbor_print("New fragment length %zu\r\n", result->fragment.len);
453
454 state->payload += result->fragment.len;
455 }
456
457
458 void zcbor_bstr_next_fragment(zcbor_state_t *state,
459 struct zcbor_string_fragment *prev_fragment,
460 struct zcbor_string_fragment *result)
461 {
462 memcpy(result, prev_fragment, sizeof(*result));
463 result->fragment.value = state->payload_mut;
464 result->offset += prev_fragment->fragment.len;
465 result->fragment.len = result->total_len - result->offset;
466
467 partition_fragment(state, result);
468 zcbor_print("fragment length %zu\r\n", result->fragment.len);
469 state->payload_end = state->payload + result->fragment.len;
470 }
471
472
473 bool zcbor_is_last_fragment(const struct zcbor_string_fragment *fragment)
474 {
475 return (fragment->total_len == (fragment->offset + fragment->fragment.len));
476 }
477
478
479 static bool str_decode(zcbor_state_t *state, struct zcbor_string *result,
480 zcbor_major_type_t exp_major_type)
481 {
482 if (!str_start_decode_with_overflow_check(state, result, exp_major_type)) {
483 ZCBOR_FAIL();
484 }
485
486 state->payload += result->len;
487 return true;
488 }
489
490
491 static bool str_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result,
492 zcbor_major_type_t exp_major_type)
493 {
494 if (!start_decode_fragment(state, result, exp_major_type)) {
495 ZCBOR_FAIL();
496 }
497
498 (state->payload) += result->fragment.len;
499 return true;
500 }
501
502
503 static bool str_expect(zcbor_state_t *state, struct zcbor_string *result,
504 zcbor_major_type_t exp_major_type)
505 {
506 struct zcbor_string tmp_result;
507
508 if (!str_decode(state, &tmp_result, exp_major_type)) {
509 ZCBOR_FAIL();
510 }
511 if (!zcbor_compare_strings(&tmp_result, result)) {
512 ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
513 }
514 return true;
515 }
516
517
518 bool zcbor_bstr_decode(zcbor_state_t *state, struct zcbor_string *result)
519 {
520 return str_decode(state, result, ZCBOR_MAJOR_TYPE_BSTR);
521 }
522
523
524 bool zcbor_bstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result)
525 {
526 return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR);
527 }
528
529
530 bool zcbor_bstr_expect(zcbor_state_t *state, struct zcbor_string *result)
531 {
532 return str_expect(state, result, ZCBOR_MAJOR_TYPE_BSTR);
533 }
534
535
536 bool zcbor_tstr_decode(zcbor_state_t *state, struct zcbor_string *result)
537 {
538 return str_decode(state, result, ZCBOR_MAJOR_TYPE_TSTR);
539 }
540
541
542 bool zcbor_tstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result)
543 {
544 return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_TSTR);
545 }
546
547
548 bool zcbor_tstr_expect(zcbor_state_t *state, struct zcbor_string *result)
549 {
550 return str_expect(state, result, ZCBOR_MAJOR_TYPE_TSTR);
551 }
552
553
554 static bool list_map_start_decode(zcbor_state_t *state,
555 zcbor_major_type_t exp_major_type)
556 {
557 size_t new_elem_count;
558 bool indefinite_length_array = false;
559
560 INITIAL_CHECKS_WITH_TYPE(exp_major_type);
561
562 if (ADDITIONAL(*state->payload) == ZCBOR_VALUE_IS_INDEFINITE_LENGTH) {
563 /* Indefinite length array. */
564 new_elem_count = ZCBOR_LARGE_ELEM_COUNT;
565 ZCBOR_ERR_IF(state->elem_count == 0, ZCBOR_ERR_LOW_ELEM_COUNT);
566 indefinite_length_array = true;
567 state->payload++;
568 state->elem_count--;
569 } else {
570 if (!value_extract(state, &new_elem_count, sizeof(new_elem_count))) {
571 ZCBOR_FAIL();
572 }
573 }
574
575 if (!zcbor_new_backup(state, new_elem_count)) {
576 FAIL_RESTORE();
577 }
578
579 state->indefinite_length_array = indefinite_length_array;
580
581 return true;
582 }
583
584
585 bool zcbor_list_start_decode(zcbor_state_t *state)
586 {
587 return list_map_start_decode(state, ZCBOR_MAJOR_TYPE_LIST);
588 }
589
590
591 bool zcbor_map_start_decode(zcbor_state_t *state)
592 {
593 bool ret = list_map_start_decode(state, ZCBOR_MAJOR_TYPE_MAP);
594
595 if (ret && !state->indefinite_length_array) {
596 if (state->elem_count >= (ZCBOR_MAX_ELEM_COUNT / 2)) {
597 /* The new elem_count is too large. */
598 ERR_RESTORE(ZCBOR_ERR_INT_SIZE);
599 }
600 state->elem_count *= 2;
601 }
602 return ret;
603 }
604
605
606 static bool array_end_expect(zcbor_state_t *state)
607 {
608 INITIAL_CHECKS();
609 ZCBOR_ERR_IF(*state->payload != 0xFF, ZCBOR_ERR_WRONG_TYPE);
610
611 state->payload++;
612 return true;
613 }
614
615
616 static bool list_map_end_decode(zcbor_state_t *state)
617 {
618 size_t max_elem_count = 0;
619
620 if (state->indefinite_length_array) {
621 if (!array_end_expect(state)) {
622 ZCBOR_FAIL();
623 }
624 max_elem_count = ZCBOR_MAX_ELEM_COUNT;
625 state->indefinite_length_array = false;
626 }
627 if (!zcbor_process_backup(state,
628 ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_CONSUME | ZCBOR_FLAG_TRANSFER_PAYLOAD,
629 max_elem_count)) {
630 ZCBOR_FAIL();
631 }
632
633 return true;
634 }
635
636
637 bool zcbor_list_end_decode(zcbor_state_t *state)
638 {
639 return list_map_end_decode(state);
640 }
641
642
643 bool zcbor_map_end_decode(zcbor_state_t *state)
644 {
645 return list_map_end_decode(state);
646 }
647
648
649 bool zcbor_list_map_end_force_decode(zcbor_state_t *state)
650 {
651 if (!zcbor_process_backup(state,
652 ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_CONSUME | ZCBOR_FLAG_TRANSFER_PAYLOAD,
653 ZCBOR_MAX_ELEM_COUNT)) {
654 ZCBOR_FAIL();
655 }
656
657 return true;
658 }
659
660
661 bool zcbor_simple_decode(zcbor_state_t *state, uint8_t *result)
662 {
663 INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_SIMPLE);
664
665 /* Simple values must be 0-23 (additional is 0-23) or 24-255 (additional is 24).
666 * Other additional values are not considered simple values. */
667 ZCBOR_ERR_IF(ADDITIONAL(*state->payload) > 24, ZCBOR_ERR_WRONG_TYPE);
668
669 if (!value_extract(state, result, sizeof(*result))) {
670 ZCBOR_FAIL();
671 }
672 return true;
673 }
674
675
676 bool zcbor_simple_expect(zcbor_state_t *state, uint8_t result)
677 {
678 uint8_t value;
679
680 if (!zcbor_simple_decode(state, &value)) {
681 ZCBOR_FAIL();
682 }
683
684 if (value != result) {
685 zcbor_print("simple value %u != %u\r\n", value, result);
686 ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
687 }
688
689 return true;
690 }
691
692
693 bool zcbor_nil_expect(zcbor_state_t *state, void *unused)
694 {
695 (void)unused;
696 return zcbor_simple_expect(state, 22);
697 }
698
699
700 bool zcbor_undefined_expect(zcbor_state_t *state, void *unused)
701 {
702 (void)unused;
703 return zcbor_simple_expect(state, 23);
704 }
705
706
707 bool zcbor_bool_decode(zcbor_state_t *state, bool *result)
708 {
709 uint8_t value;
710
711 if (!zcbor_simple_decode(state, &value)) {
712 ZCBOR_FAIL();
713 }
714 value -= ZCBOR_BOOL_TO_SIMPLE;
715 if (value > 1) {
716 ERR_RESTORE(ZCBOR_ERR_WRONG_TYPE);
717 }
718 *result = value;
719
720 zcbor_print("boolval: %u\r\n", *result);
721 return true;
722 }
723
724
725 bool zcbor_bool_expect(zcbor_state_t *state, bool result)
726 {
727 return zcbor_simple_expect(state, (uint8_t)(!!result) + ZCBOR_BOOL_TO_SIMPLE);
728 }
729
730
731 static bool float_check(zcbor_state_t *state, uint8_t additional_val)
732 {
733 INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_SIMPLE);
734 ZCBOR_ERR_IF(ADDITIONAL(*state->payload) != additional_val, ZCBOR_ERR_FLOAT_SIZE);
735 return true;
736 }
737
738
739 bool zcbor_float16_bytes_decode(zcbor_state_t *state, uint16_t *result)
740 {
741
742 ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_2_BYTES));
743
744 if (!value_extract(state, result, sizeof(*result))) {
745 ZCBOR_FAIL();
746 }
747
748 return true;
749 }
750
751
752 bool zcbor_float16_bytes_expect(zcbor_state_t *state, uint16_t result)
753 {
754 uint16_t value;
755
756 if (!zcbor_float16_bytes_decode(state, &value)) {
757 ZCBOR_FAIL();
758 }
759 if (value != result) {
760 ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
761 }
762 return true;
763 }
764
765
766 /* Float16: */
767 #define F16_SIGN_OFFS 15 /* Bit offset of the sign bit. */
768 #define F16_EXPO_OFFS 10 /* Bit offset of the exponent. */
769 #define F16_EXPO_MSK 0x1F /* Bitmask for the exponent (right shifted by F16_EXPO_OFFS). */
770 #define F16_MANTISSA_MSK 0x3FF /* Bitmask for the mantissa. */
771 #define F16_MIN_EXPO 24 /* Negative exponent of the non-zero float16 value closest to 0 (2^-24) */
772 #define F16_MIN (1.0 / (1 << F16_MIN_EXPO)) /* The non-zero float16 value closest to 0 (2^-24) */
773 #define F16_BIAS 15 /* The exponent bias of normalized float16 values. */
774
775 /* Float32: */
776 #define F32_SIGN_OFFS 31 /* Bit offset of the sign bit. */
777 #define F32_EXPO_OFFS 23 /* Bit offset of the exponent. */
778 #define F32_EXPO_MSK 0xFF /* Bitmask for the exponent (right shifted by F32_EXPO_OFFS). */
779 #define F32_BIAS 127 /* The exponent bias of normalized float32 values. */
780
781
782 bool zcbor_float16_decode(zcbor_state_t *state, float *result)
783 {
784 uint16_t value16;
785
786 if (!zcbor_float16_bytes_decode(state, &value16)) {
787 ZCBOR_FAIL();
788 }
789
790 uint32_t sign = value16 >> F16_SIGN_OFFS;
791 uint32_t expo = (value16 >> F16_EXPO_OFFS) & F16_EXPO_MSK;
792 uint32_t mantissa = value16 & F16_MANTISSA_MSK;
793
794 if ((expo == 0) && (mantissa != 0)) {
795 /* Subnormal float16 - convert to normalized float32 */
796 *result = ((float)mantissa * (float)F16_MIN) * (sign ? -1 : 1);
797 } else {
798 /* Normalized / zero / Infinity / NaN */
799 uint32_t new_expo = (expo == 0 /* zero */) ? 0
800 : (expo == F16_EXPO_MSK /* inf/NaN */) ? F32_EXPO_MSK
801 : (expo + (F32_BIAS - F16_BIAS));
802 uint32_t value32 = (sign << F32_SIGN_OFFS) | (new_expo << F32_EXPO_OFFS)
803 | (mantissa << (F32_EXPO_OFFS - F16_EXPO_OFFS));
804 memcpy(result, &value32, sizeof(*result));
805 }
806
807 return true;
808 }
809
810
811 bool zcbor_float16_expect(zcbor_state_t *state, float result)
812 {
813 float value;
814
815 if (!zcbor_float16_decode(state, &value)) {
816 ZCBOR_FAIL();
817 }
818 if (value != result) {
819 ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
820 }
821 return true;
822 }
823
824
825 bool zcbor_float32_decode(zcbor_state_t *state, float *result)
826 {
827 ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_4_BYTES));
828
829 if (!value_extract(state, result, sizeof(*result))) {
830 ZCBOR_FAIL();
831 }
832
833 return true;
834 }
835
836
837 bool zcbor_float32_expect(zcbor_state_t *state, float result)
838 {
839 float value;
840
841 if (!zcbor_float32_decode(state, &value)) {
842 ZCBOR_FAIL();
843 }
844 if (value != result) {
845 ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
846 }
847 return true;
848 }
849
850
851 bool zcbor_float16_32_decode(zcbor_state_t *state, float *result)
852 {
853 if (zcbor_float16_decode(state, result)) {
854 /* Do nothing */
855 } else if (!zcbor_float32_decode(state, result)) {
856 ZCBOR_FAIL();
857 }
858
859 return true;
860 }
861
862
863 bool zcbor_float16_32_expect(zcbor_state_t *state, float result)
864 {
865 if (zcbor_float16_expect(state, (float)result)) {
866 /* Do nothing */
867 } else if (!zcbor_float32_expect(state, result)) {
868 ZCBOR_FAIL();
869 }
870
871 return true;
872 }
873
874
875 bool zcbor_float64_decode(zcbor_state_t *state, double *result)
876 {
877 ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_8_BYTES));
878
879 if (!value_extract(state, result, sizeof(*result))) {
880 ZCBOR_FAIL();
881 }
882
883 return true;
884 }
885
886
887 bool zcbor_float64_expect(zcbor_state_t *state, double result)
888 {
889 double value;
890
891 if (!zcbor_float64_decode(state, &value)) {
892 ZCBOR_FAIL();
893 }
894 if (value != result) {
895 ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
896 }
897 return true;
898 }
899
900
901 bool zcbor_float32_64_decode(zcbor_state_t *state, double *result)
902 {
903 float float_result;
904
905 if (zcbor_float32_decode(state, &float_result)) {
906 *result = (double)float_result;
907 } else if (!zcbor_float64_decode(state, result)) {
908 ZCBOR_FAIL();
909 }
910
911 return true;
912 }
913
914
915 bool zcbor_float32_64_expect(zcbor_state_t *state, double result)
916 {
917 if (zcbor_float64_expect(state, result)) {
918 /* Do nothing */
919 } else if (!zcbor_float32_expect(state, (float)result)) {
920 ZCBOR_FAIL();
921 }
922
923 return true;
924 }
925
926
927 bool zcbor_float_decode(zcbor_state_t *state, double *result)
928 {
929 float float_result;
930
931 if (zcbor_float16_decode(state, &float_result)) {
932 *result = (double)float_result;
933 } else if (zcbor_float32_decode(state, &float_result)) {
934 *result = (double)float_result;
935 } else if (!zcbor_float64_decode(state, result)) {
936 ZCBOR_FAIL();
937 }
938
939 return true;
940 }
941
942
943 bool zcbor_float_expect(zcbor_state_t *state, double result)
944 {
945 if (zcbor_float16_expect(state, (float)result)) {
946 /* Do nothing */
947 } else if (zcbor_float32_expect(state, (float)result)) {
948 /* Do nothing */
949 } else if (!zcbor_float64_expect(state, result)) {
950 ZCBOR_FAIL();
951 }
952
953 return true;
954 }
955
956
957 bool zcbor_any_skip(zcbor_state_t *state, void *result)
958 {
959 zcbor_assert_state(result == NULL,
960 "'any' type cannot be returned, only skipped.\r\n");
961 (void)result;
962
963 INITIAL_CHECKS();
964 zcbor_major_type_t major_type = MAJOR_TYPE(*state->payload);
965 uint8_t additional = ADDITIONAL(*state->payload);
966 uint64_t value = 0; /* In case of indefinite_length_array. */
967 zcbor_state_t state_copy;
968
969 memcpy(&state_copy, state, sizeof(zcbor_state_t));
970
971 while (major_type == ZCBOR_MAJOR_TYPE_TAG) {
972 uint32_t tag_dummy;
973
974 if (!zcbor_tag_decode(&state_copy, &tag_dummy)) {
975 ZCBOR_FAIL();
976 }
977 ZCBOR_ERR_IF(state_copy.payload >= state_copy.payload_end, ZCBOR_ERR_NO_PAYLOAD);
978 major_type = MAJOR_TYPE(*state_copy.payload);
979 additional = ADDITIONAL(*state_copy.payload);
980 }
981
982 const bool indefinite_length_array = ((additional == ZCBOR_VALUE_IS_INDEFINITE_LENGTH)
983 && ((major_type == ZCBOR_MAJOR_TYPE_LIST) || (major_type == ZCBOR_MAJOR_TYPE_MAP)));
984
985 if (!indefinite_length_array && !value_extract(&state_copy, &value, sizeof(value))) {
986 /* Can happen because of elem_count (or payload_end) */
987 ZCBOR_FAIL();
988 }
989
990 switch (major_type) {
991 case ZCBOR_MAJOR_TYPE_BSTR:
992 case ZCBOR_MAJOR_TYPE_TSTR:
993 /* 'value' is the length of the BSTR or TSTR.
994 * The cast to size_t is safe because value_extract() above
995 * checks that payload_end is greater than payload. */
996 ZCBOR_ERR_IF(
997 value > (uint64_t)(state_copy.payload_end - state_copy.payload),
998 ZCBOR_ERR_NO_PAYLOAD);
999 (state_copy.payload) += value;
1000 break;
1001 case ZCBOR_MAJOR_TYPE_MAP:
1002 ZCBOR_ERR_IF(value > (SIZE_MAX / 2), ZCBOR_ERR_INT_SIZE);
1003 value *= 2;
1004 /* fallthrough */
1005 case ZCBOR_MAJOR_TYPE_LIST:
1006 if (indefinite_length_array) {
1007 state_copy.payload++;
1008 value = ZCBOR_LARGE_ELEM_COUNT;
1009 }
1010 state_copy.elem_count = (size_t)value;
1011 state_copy.indefinite_length_array = indefinite_length_array;
1012 while (!zcbor_array_at_end(&state_copy)) {
1013 if (!zcbor_any_skip(&state_copy, NULL)) {
1014 ZCBOR_FAIL();
1015 }
1016 }
1017 if (indefinite_length_array && !array_end_expect(&state_copy)) {
1018 ZCBOR_FAIL();
1019 }
1020 break;
1021 default:
1022 /* Do nothing */
1023 break;
1024 }
1025
1026 state->payload = state_copy.payload;
1027 state->elem_count--;
1028
1029 return true;
1030 }
1031
1032
1033 bool zcbor_tag_decode(zcbor_state_t *state, uint32_t *result)
1034 {
1035 INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_TAG);
1036
1037 if (!value_extract(state, result, sizeof(*result))) {
1038 ZCBOR_FAIL();
1039 }
1040 state->elem_count++;
1041 return true;
1042 }
1043
1044
1045 bool zcbor_tag_expect(zcbor_state_t *state, uint32_t result)
1046 {
1047 uint32_t tag_val;
1048
1049 if (!zcbor_tag_decode(state, &tag_val)) {
1050 ZCBOR_FAIL();
1051 }
1052 if (tag_val != result) {
1053 ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1054 }
1055 return true;
1056 }
1057
1058
1059 bool zcbor_multi_decode(size_t min_decode,
1060 size_t max_decode,
1061 size_t *num_decode,
1062 zcbor_decoder_t decoder,
1063 zcbor_state_t *state,
1064 void *result,
1065 size_t result_len)
1066 {
1067 ZCBOR_CHECK_ERROR();
1068 for (size_t i = 0; i < max_decode; i++) {
1069 uint8_t const *payload_bak = state->payload;
1070 size_t elem_count_bak = state->elem_count;
1071
1072 if (!decoder(state,
1073 (uint8_t *)result + i*result_len)) {
1074 *num_decode = i;
1075 state->payload = payload_bak;
1076 state->elem_count = elem_count_bak;
1077 ZCBOR_ERR_IF(i < min_decode, ZCBOR_ERR_ITERATIONS);
1078 zcbor_print("Found %" PRIuFAST32 " elements.\r\n", i);
1079 return true;
1080 }
1081 }
1082 zcbor_print("Found %" PRIuFAST32 " elements.\r\n", max_decode);
1083 *num_decode = max_decode;
1084 return true;
1085 }
1086
1087
1088 bool zcbor_present_decode(bool *present,
1089 zcbor_decoder_t decoder,
1090 zcbor_state_t *state,
1091 void *result)
1092 {
1093 size_t num_decode;
1094 bool retval = zcbor_multi_decode(0, 1, &num_decode, decoder, state, result, 0);
1095
1096 zcbor_assert_state(retval, "zcbor_multi_decode should not fail with these parameters.\r\n");
1097
1098 *present = !!num_decode;
1099 return retval;
1100 }
1101
1102
1103 void zcbor_new_decode_state(zcbor_state_t *state_array, size_t n_states,
1104 const uint8_t *payload, size_t payload_len, size_t elem_count)
1105 {
1106 zcbor_new_state(state_array, n_states, payload, payload_len, elem_count);
1107 }
1108