1 /*
2  * This file has been copied from the zcbor library.
3  * Commit zcbor 0.8.1
4  */
5 
6 /*
7  * Copyright (c) 2020 Nordic Semiconductor ASA
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 
12 #include <stdint.h>
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include <string.h>
16 #include <inttypes.h>
17 #include "zcbor_decode.h"
18 #include "zcbor_common.h"
19 #include "zcbor_print.h"
20 
21 
22 /** Return value length from additional value.
23  */
additional_len(uint8_t additional)24 static size_t additional_len(uint8_t additional)
25 {
26 	if (additional <= ZCBOR_VALUE_IN_HEADER) {
27 		return 0;
28 	} else if (ZCBOR_VALUE_IS_1_BYTE <= additional && additional <= ZCBOR_VALUE_IS_8_BYTES) {
29 		/* 24 => 1
30 		 * 25 => 2
31 		 * 26 => 4
32 		 * 27 => 8
33 		 */
34 		return 1U << (additional - ZCBOR_VALUE_IS_1_BYTE);
35 	}
36 	return 0xF;
37 }
38 
39 
initial_checks(zcbor_state_t * state)40 static bool initial_checks(zcbor_state_t *state)
41 {
42 	ZCBOR_CHECK_ERROR();
43 	ZCBOR_CHECK_PAYLOAD();
44 	return true;
45 }
46 
47 
type_check(zcbor_state_t * state,zcbor_major_type_t exp_major_type)48 static bool type_check(zcbor_state_t *state, zcbor_major_type_t exp_major_type)
49 {
50 	if (!initial_checks(state)) {
51 		ZCBOR_FAIL();
52 	}
53 	zcbor_major_type_t major_type = ZCBOR_MAJOR_TYPE(*state->payload);
54 
55 	if (major_type != exp_major_type) {
56 		ZCBOR_ERR(ZCBOR_ERR_WRONG_TYPE);
57 	}
58 	return true;
59 }
60 
61 
62 #define INITIAL_CHECKS() \
63 do {\
64 	if (!initial_checks(state)) { \
65 		ZCBOR_FAIL(); \
66 	} \
67 } while(0)
68 
69 #define INITIAL_CHECKS_WITH_TYPE(exp_major_type) \
70 do {\
71 	if (!type_check(state, exp_major_type)) { \
72 		ZCBOR_FAIL(); \
73 	} \
74 } while(0)
75 
err_restore(zcbor_state_t * state,int err)76 static void err_restore(zcbor_state_t *state, int err)
77 {
78 	state->payload = state->payload_bak;
79 	state->elem_count++;
80 	zcbor_error(state, err);
81 }
82 
83 #define ERR_RESTORE(err) \
84 do { \
85 	err_restore(state, err); \
86 	ZCBOR_FAIL(); \
87 } while(0)
88 
89 #define FAIL_RESTORE() \
90 do { \
91 	state->payload = state->payload_bak; \
92 	state->elem_count++; \
93 	ZCBOR_FAIL(); \
94 } while(0)
95 
96 #define PRINT_FUNC() zcbor_log("%s ", __func__);
97 
98 
endian_copy(uint8_t * dst,const uint8_t * src,size_t src_len)99 static void endian_copy(uint8_t *dst, const uint8_t *src, size_t src_len)
100 {
101 #ifdef ZCBOR_BIG_ENDIAN
102 	memcpy(dst, src, src_len);
103 #else
104 	for (size_t i = 0; i < src_len; i++) {
105 		dst[i] = src[src_len - 1 - i];
106 	}
107 #endif /* ZCBOR_BIG_ENDIAN */
108 }
109 
110 
111 /** Get a single value.
112  *
113  * @details @p ppayload must point to the header byte. This function will
114  *          retrieve the value (either from within the additional info, or from
115  *          the subsequent bytes) and return it in the result. The result can
116  *          have arbitrary length.
117  *
118  *          The function will also validate
119  *           - Min/max constraints on the value.
120  *           - That @p payload doesn't overrun past @p payload_end.
121  *           - That @p elem_count has not been exhausted.
122  *
123  *          @p ppayload and @p elem_count are updated if the function
124  *          succeeds. If not, they are left unchanged.
125  *
126  *          CBOR values are always big-endian, so this function converts from
127  *          big to little-endian if necessary (@ref ZCBOR_BIG_ENDIAN).
128  */
value_extract(zcbor_state_t * state,void * const result,size_t result_len)129 static bool value_extract(zcbor_state_t *state,
130 		void *const result, size_t result_len)
131 {
132 	zcbor_trace(state, "value_extract");
133 	zcbor_assert_state(result_len != 0, "0-length result not supported.\r\n");
134 	zcbor_assert_state(result_len <= 8, "result sizes above 8 bytes not supported.\r\n");
135 	zcbor_assert_state(result != NULL, "result cannot be NULL.\r\n");
136 
137 	INITIAL_CHECKS();
138 	ZCBOR_ERR_IF((state->elem_count == 0), ZCBOR_ERR_LOW_ELEM_COUNT);
139 
140 	uint8_t additional = ZCBOR_ADDITIONAL(*state->payload);
141 	size_t len = additional_len(additional);
142 	uint8_t *result_offs = (uint8_t *)result + ZCBOR_ECPY_OFFS(result_len, MAX(1, len));
143 
144 	ZCBOR_ERR_IF(additional > ZCBOR_VALUE_IS_8_BYTES, ZCBOR_ERR_ADDITIONAL_INVAL);
145 	ZCBOR_ERR_IF(len > result_len, ZCBOR_ERR_INT_SIZE);
146 	ZCBOR_ERR_IF((state->payload + len + 1) > state->payload_end,
147 		ZCBOR_ERR_NO_PAYLOAD);
148 
149 	memset(result, 0, result_len);
150 
151 	if (len == 0) {
152 		*result_offs = additional;
153 	} else {
154 		endian_copy(result_offs, state->payload + 1, len);
155 
156 #ifdef ZCBOR_CANONICAL
157 		ZCBOR_ERR_IF((zcbor_header_len_ptr(result, result_len) != (len + 1)),
158 			ZCBOR_ERR_INVALID_VALUE_ENCODING);
159 #endif
160 	}
161 
162 	state->payload_bak = state->payload;
163 	(state->payload) += len + 1;
164 	(state->elem_count)--;
165 	return true;
166 }
167 
168 
zcbor_int_decode(zcbor_state_t * state,void * result,size_t result_size)169 bool zcbor_int_decode(zcbor_state_t *state, void *result, size_t result_size)
170 {
171 	PRINT_FUNC();
172 	INITIAL_CHECKS();
173 	zcbor_major_type_t major_type = ZCBOR_MAJOR_TYPE(*state->payload);
174 	uint8_t *result_uint8 = (uint8_t *)result;
175 	int8_t *result_int8 = (int8_t *)result;
176 
177 	if (major_type != ZCBOR_MAJOR_TYPE_PINT
178 		&& major_type != ZCBOR_MAJOR_TYPE_NINT) {
179 		/* Value to be read doesn't have the right type. */
180 		ZCBOR_ERR(ZCBOR_ERR_WRONG_TYPE);
181 	}
182 
183 	if (!value_extract(state, result, result_size)) {
184 		ZCBOR_FAIL();
185 	}
186 
187 #ifdef ZCBOR_BIG_ENDIAN
188 	if (result_int8[0] < 0) {
189 #else
190 	if (result_int8[result_size - 1] < 0) {
191 #endif
192 		/* Value is too large to fit in a signed integer. */
193 		ERR_RESTORE(ZCBOR_ERR_INT_SIZE);
194 	}
195 
196 	if (major_type == ZCBOR_MAJOR_TYPE_NINT) {
197 		/* Convert from CBOR's representation by flipping all bits. */
198 		for (unsigned int i = 0; i < result_size; i++) {
199 			result_uint8[i] = (uint8_t)~result_uint8[i];
200 		}
201 	}
202 
203 	return true;
204 }
205 
206 
207 bool zcbor_int32_decode(zcbor_state_t *state, int32_t *result)
208 {
209 	PRINT_FUNC();
210 	return zcbor_int_decode(state, result, sizeof(*result));
211 }
212 
213 
214 bool zcbor_int64_decode(zcbor_state_t *state, int64_t *result)
215 {
216 	PRINT_FUNC();
217 	return zcbor_int_decode(state, result, sizeof(*result));
218 }
219 
220 
221 bool zcbor_uint_decode(zcbor_state_t *state, void *result, size_t result_size)
222 {
223 	PRINT_FUNC();
224 	INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PINT);
225 
226 	if (!value_extract(state, result, result_size)) {
227 		zcbor_log("uint with size %zu failed.\r\n", result_size);
228 		ZCBOR_FAIL();
229 	}
230 	return true;
231 }
232 
233 
234 bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result)
235 {
236 	PRINT_FUNC();
237 	return zcbor_uint_decode(state, result, sizeof(*result));
238 }
239 
240 
241 bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t result)
242 {
243 	PRINT_FUNC();
244 	if (!zcbor_union_elem_code(state)) {
245 		ZCBOR_FAIL();
246 	}
247 	return zcbor_int32_expect(state, result);
248 }
249 
250 
251 bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t result)
252 {
253 	PRINT_FUNC();
254 	if (!zcbor_union_elem_code(state)) {
255 		ZCBOR_FAIL();
256 	}
257 	return zcbor_int64_expect(state, result);
258 }
259 
260 
261 bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t result)
262 {
263 	PRINT_FUNC();
264 	if (!zcbor_union_elem_code(state)) {
265 		ZCBOR_FAIL();
266 	}
267 	return zcbor_uint32_expect(state, result);
268 }
269 
270 
271 bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t result)
272 {
273 	PRINT_FUNC();
274 	if (!zcbor_union_elem_code(state)) {
275 		ZCBOR_FAIL();
276 	}
277 	return zcbor_uint64_expect(state, result);
278 }
279 
280 
281 bool zcbor_int32_expect(zcbor_state_t *state, int32_t expected)
282 {
283 	PRINT_FUNC();
284 	return zcbor_int64_expect(state, expected);
285 }
286 
287 
288 bool zcbor_int32_pexpect(zcbor_state_t *state, int32_t *expected)
289 {
290 	PRINT_FUNC();
291 	return zcbor_int32_expect(state, *expected);
292 }
293 
294 
295 bool zcbor_int64_expect(zcbor_state_t *state, int64_t expected)
296 {
297 	PRINT_FUNC();
298 	int64_t actual;
299 
300 	if (!zcbor_int64_decode(state, &actual)) {
301 		ZCBOR_FAIL();
302 	}
303 
304 	if (actual != expected) {
305 		zcbor_log("%" PRIi64 " != %" PRIi64 "\r\n", actual, expected);
306 		ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
307 	}
308 	return true;
309 }
310 
311 
312 bool zcbor_int64_pexpect(zcbor_state_t *state, int64_t *expected)
313 {
314 	PRINT_FUNC();
315 	return zcbor_int64_expect(state, *expected);
316 }
317 
318 
319 bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result)
320 {
321 	PRINT_FUNC();
322 	return zcbor_uint_decode(state, result, sizeof(*result));
323 }
324 
325 
326 #ifdef ZCBOR_SUPPORTS_SIZE_T
327 bool zcbor_size_decode(zcbor_state_t *state, size_t *result)
328 {
329 	PRINT_FUNC();
330 	return zcbor_uint_decode(state, result, sizeof(*result));
331 }
332 #endif
333 
334 
335 bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t expected)
336 {
337 	PRINT_FUNC();
338 	return zcbor_uint64_expect(state, expected);
339 }
340 
341 
342 bool zcbor_uint32_pexpect(zcbor_state_t *state, uint32_t *expected)
343 {
344 	PRINT_FUNC();
345 	return zcbor_uint32_expect(state, *expected);
346 }
347 
348 
349 bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t expected)
350 {
351 	PRINT_FUNC();
352 	uint64_t actual;
353 
354 	if (!zcbor_uint64_decode(state, &actual)) {
355 		ZCBOR_FAIL();
356 	}
357 	if (actual != expected) {
358 		zcbor_log("%" PRIu64 " != %" PRIu64 "\r\n", actual, expected);
359 		ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
360 	}
361 	return true;
362 }
363 
364 
365 bool zcbor_uint64_pexpect(zcbor_state_t *state, uint64_t *expected)
366 {
367 	PRINT_FUNC();
368 	return zcbor_uint64_expect(state, *expected);
369 }
370 
371 
372 #ifdef ZCBOR_SUPPORTS_SIZE_T
373 bool zcbor_size_expect(zcbor_state_t *state, size_t expected)
374 {
375 	PRINT_FUNC();
376 	return zcbor_uint64_expect(state, expected);
377 }
378 
379 
380 bool zcbor_size_pexpect(zcbor_state_t *state, size_t *expected)
381 {
382 	PRINT_FUNC();
383 	return zcbor_size_expect(state, *expected);
384 }
385 #endif
386 
387 
388 static bool str_start_decode(zcbor_state_t *state,
389 		struct zcbor_string *result, zcbor_major_type_t exp_major_type)
390 {
391 	INITIAL_CHECKS_WITH_TYPE(exp_major_type);
392 
393 	if (!value_extract(state, &result->len, sizeof(result->len))) {
394 		ZCBOR_FAIL();
395 	}
396 
397 	result->value = state->payload;
398 	return true;
399 }
400 
401 static bool str_start_decode_with_overflow_check(zcbor_state_t *state,
402 		struct zcbor_string *result, zcbor_major_type_t exp_major_type)
403 {
404 	bool res = str_start_decode(state, result, exp_major_type);
405 
406 	if (!res) {
407 		ZCBOR_FAIL();
408 	}
409 
410 	/* Casting to size_t is safe since str_start_decode() checks that
411 	 * payload_end is bigger that payload. */
412 	if (result->len > (size_t)(state->payload_end - state->payload)) {
413 		zcbor_log("error: 0x%zu > 0x%zu\r\n",
414 			result->len,
415 			(state->payload_end - state->payload));
416 		ERR_RESTORE(ZCBOR_ERR_NO_PAYLOAD);
417 	}
418 
419 	return true;
420 }
421 
422 
423 bool zcbor_bstr_start_decode(zcbor_state_t *state, struct zcbor_string *result)
424 {
425 	PRINT_FUNC();
426 	struct zcbor_string dummy;
427 	if (result == NULL) {
428 		result = &dummy;
429 	}
430 
431 	if(!str_start_decode_with_overflow_check(state, result, ZCBOR_MAJOR_TYPE_BSTR)) {
432 		ZCBOR_FAIL();
433 	}
434 
435 	if (!zcbor_new_backup(state, ZCBOR_MAX_ELEM_COUNT)) {
436 		FAIL_RESTORE();
437 	}
438 
439 	state->payload_end = result->value + result->len;
440 	return true;
441 }
442 
443 
444 bool zcbor_bstr_end_decode(zcbor_state_t *state)
445 {
446 	ZCBOR_ERR_IF(state->payload != state->payload_end, ZCBOR_ERR_PAYLOAD_NOT_CONSUMED);
447 
448 	if (!zcbor_process_backup(state,
449 			ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_CONSUME | ZCBOR_FLAG_KEEP_PAYLOAD,
450 			ZCBOR_MAX_ELEM_COUNT)) {
451 		ZCBOR_FAIL();
452 	}
453 
454 	return true;
455 }
456 
457 
458 static void partition_fragment(const zcbor_state_t *state,
459 	struct zcbor_string_fragment *result)
460 {
461 	result->fragment.len = MIN(result->fragment.len,
462 		(size_t)state->payload_end - (size_t)state->payload);
463 }
464 
465 
466 static bool start_decode_fragment(zcbor_state_t *state,
467 	struct zcbor_string_fragment *result,
468 	zcbor_major_type_t exp_major_type)
469 {
470 	PRINT_FUNC();
471 	if(!str_start_decode(state, &result->fragment, exp_major_type)) {
472 		ZCBOR_FAIL();
473 	}
474 
475 	result->offset = 0;
476 	result->total_len = result->fragment.len;
477 	partition_fragment(state, result);
478 	state->payload_end = state->payload + result->fragment.len;
479 
480 	return true;
481 }
482 
483 bool zcbor_bstr_start_decode_fragment(zcbor_state_t *state,
484 	struct zcbor_string_fragment *result)
485 {
486 	PRINT_FUNC();
487 	if (!start_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR)) {
488 		ZCBOR_FAIL();
489 	}
490 	if (!zcbor_new_backup(state, ZCBOR_MAX_ELEM_COUNT)) {
491 		FAIL_RESTORE();
492 	}
493 	return true;
494 }
495 
496 
497 void zcbor_next_fragment(zcbor_state_t *state,
498 	struct zcbor_string_fragment *prev_fragment,
499 	struct zcbor_string_fragment *result)
500 {
501 	memcpy(result, prev_fragment, sizeof(*result));
502 	result->fragment.value = state->payload_mut;
503 	result->offset += prev_fragment->fragment.len;
504 	result->fragment.len = result->total_len - result->offset;
505 
506 	partition_fragment(state, result);
507 	zcbor_log("New fragment length %zu\r\n", result->fragment.len);
508 
509 	state->payload += result->fragment.len;
510 }
511 
512 
513 void zcbor_bstr_next_fragment(zcbor_state_t *state,
514 	struct zcbor_string_fragment *prev_fragment,
515 	struct zcbor_string_fragment *result)
516 {
517 	memcpy(result, prev_fragment, sizeof(*result));
518 	result->fragment.value = state->payload_mut;
519 	result->offset += prev_fragment->fragment.len;
520 	result->fragment.len = result->total_len - result->offset;
521 
522 	partition_fragment(state, result);
523 	zcbor_log("fragment length %zu\r\n", result->fragment.len);
524 	state->payload_end = state->payload + result->fragment.len;
525 }
526 
527 
528 bool zcbor_is_last_fragment(const struct zcbor_string_fragment *fragment)
529 {
530 	return (fragment->total_len == (fragment->offset + fragment->fragment.len));
531 }
532 
533 
534 static bool str_decode(zcbor_state_t *state, struct zcbor_string *result,
535 		zcbor_major_type_t exp_major_type)
536 {
537 	if (!str_start_decode_with_overflow_check(state, result, exp_major_type)) {
538 		ZCBOR_FAIL();
539 	}
540 
541 	state->payload += result->len;
542 	return true;
543 }
544 
545 
546 static bool str_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result,
547 		zcbor_major_type_t exp_major_type)
548 {
549 	if (!start_decode_fragment(state, result, exp_major_type)) {
550 		ZCBOR_FAIL();
551 	}
552 
553 	(state->payload) += result->fragment.len;
554 	return true;
555 }
556 
557 
558 static bool str_expect(zcbor_state_t *state, struct zcbor_string *result,
559 		zcbor_major_type_t exp_major_type)
560 {
561 	struct zcbor_string tmp_result;
562 
563 	if (!str_decode(state, &tmp_result, exp_major_type)) {
564 		ZCBOR_FAIL();
565 	}
566 	if (!zcbor_compare_strings(&tmp_result, result)) {
567 		ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
568 	}
569 	return true;
570 }
571 
572 
573 bool zcbor_bstr_decode(zcbor_state_t *state, struct zcbor_string *result)
574 {
575 	PRINT_FUNC();
576 	return str_decode(state, result, ZCBOR_MAJOR_TYPE_BSTR);
577 }
578 
579 
580 bool zcbor_bstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result)
581 {
582 	PRINT_FUNC();
583 	return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR);
584 }
585 
586 
587 bool zcbor_bstr_expect(zcbor_state_t *state, struct zcbor_string *expected)
588 {
589 	PRINT_FUNC();
590 	return str_expect(state, expected, ZCBOR_MAJOR_TYPE_BSTR);
591 }
592 
593 
594 bool zcbor_tstr_decode(zcbor_state_t *state, struct zcbor_string *result)
595 {
596 	PRINT_FUNC();
597 	return str_decode(state, result, ZCBOR_MAJOR_TYPE_TSTR);
598 }
599 
600 
601 bool zcbor_tstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result)
602 {
603 	PRINT_FUNC();
604 	return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_TSTR);
605 }
606 
607 
608 bool zcbor_tstr_expect(zcbor_state_t *state, struct zcbor_string *expected)
609 {
610 	PRINT_FUNC();
611 	return str_expect(state, expected, ZCBOR_MAJOR_TYPE_TSTR);
612 }
613 
614 
615 bool zcbor_bstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len)
616 {
617 	PRINT_FUNC();
618 	struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
619 
620 	return zcbor_bstr_expect(state, &zs);
621 }
622 
623 
624 bool zcbor_tstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len)
625 {
626 	PRINT_FUNC();
627 	struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
628 
629 	return zcbor_tstr_expect(state, &zs);
630 }
631 
632 
633 bool zcbor_bstr_expect_term(zcbor_state_t *state, char const *string, size_t maxlen)
634 {
635 	PRINT_FUNC();
636 	return zcbor_bstr_expect_ptr(state, string, strnlen(string, maxlen));
637 }
638 
639 
640 bool zcbor_tstr_expect_term(zcbor_state_t *state, char const *string, size_t maxlen)
641 {
642 	PRINT_FUNC();
643 	return zcbor_tstr_expect_ptr(state, string, strnlen(string, maxlen));
644 }
645 
646 
647 static bool list_map_start_decode(zcbor_state_t *state,
648 		zcbor_major_type_t exp_major_type)
649 {
650 	size_t new_elem_count;
651 	bool indefinite_length_array = false;
652 
653 	INITIAL_CHECKS_WITH_TYPE(exp_major_type);
654 
655 #ifndef ZCBOR_CANONICAL
656 	if (ZCBOR_ADDITIONAL(*state->payload) == ZCBOR_VALUE_IS_INDEFINITE_LENGTH) {
657 		/* Indefinite length array. */
658 		new_elem_count = ZCBOR_LARGE_ELEM_COUNT;
659 		ZCBOR_ERR_IF(state->elem_count == 0, ZCBOR_ERR_LOW_ELEM_COUNT);
660 		indefinite_length_array = true;
661 		state->payload_bak = state->payload++;
662 		state->elem_count--;
663 	} else
664 #endif
665 	{
666 		if (!value_extract(state, &new_elem_count, sizeof(new_elem_count))) {
667 			ZCBOR_FAIL();
668 		}
669 	}
670 
671 	if (!zcbor_new_backup(state, new_elem_count)) {
672 		FAIL_RESTORE();
673 	}
674 
675 	state->decode_state.indefinite_length_array = indefinite_length_array;
676 
677 	return true;
678 }
679 
680 
681 bool zcbor_list_start_decode(zcbor_state_t *state)
682 {
683 	PRINT_FUNC();
684 	return list_map_start_decode(state, ZCBOR_MAJOR_TYPE_LIST);
685 }
686 
687 
688 bool zcbor_map_start_decode(zcbor_state_t *state)
689 {
690 	PRINT_FUNC();
691 	bool ret = list_map_start_decode(state, ZCBOR_MAJOR_TYPE_MAP);
692 
693 	if (ret && !state->decode_state.indefinite_length_array) {
694 		if (state->elem_count >= (ZCBOR_MAX_ELEM_COUNT / 2)) {
695 			/* The new elem_count is too large. */
696 			ERR_RESTORE(ZCBOR_ERR_INT_SIZE);
697 		}
698 		state->elem_count *= 2;
699 	}
700 	return ret;
701 }
702 
703 
704 bool zcbor_array_at_end(zcbor_state_t *state)
705 {
706 #ifdef ZCBOR_CANONICAL
707 	const bool indefinite_length_array = false;
708 #else
709 	const bool indefinite_length_array = state->decode_state.indefinite_length_array;
710 #endif
711 	return ((!indefinite_length_array && (state->elem_count == 0))
712 		|| (indefinite_length_array
713 			&& (state->payload < state->payload_end)
714 			&& (*state->payload == 0xFF)));
715 }
716 
717 
718 static size_t update_map_elem_count(zcbor_state_t *state, size_t elem_count);
719 #ifdef ZCBOR_MAP_SMART_SEARCH
720 static bool allocate_map_flags(zcbor_state_t *state, size_t elem_count);
721 #endif
722 
723 
724 bool zcbor_unordered_map_start_decode(zcbor_state_t *state)
725 {
726 	PRINT_FUNC();
727 	ZCBOR_FAIL_IF(!zcbor_map_start_decode(state));
728 
729 #ifdef ZCBOR_MAP_SMART_SEARCH
730 	state->decode_state.map_search_elem_state
731 		+= zcbor_flags_to_bytes(state->decode_state.map_elem_count);
732 #else
733 	state->decode_state.map_elems_processed = 0;
734 #endif
735 	state->decode_state.map_elem_count = 0;
736 	state->decode_state.counting_map_elems = state->decode_state.indefinite_length_array;
737 
738 	if (!state->decode_state.counting_map_elems) {
739 		size_t old_flags = update_map_elem_count(state, state->elem_count);
740 #ifdef ZCBOR_MAP_SMART_SEARCH
741 		ZCBOR_FAIL_IF(!allocate_map_flags(state, old_flags));
742 #endif
743 		(void)old_flags;
744 	}
745 
746 	return true;
747 }
748 
749 
750 /** Return the max (starting) elem_count of the current container.
751  *
752  *  Should only be used for unordered maps (started with @ref zcbor_unordered_map_start_decode)
753  */
754 static size_t zcbor_current_max_elem_count(zcbor_state_t *state)
755 {
756 	return (state->decode_state.indefinite_length_array ? \
757 		ZCBOR_LARGE_ELEM_COUNT : state->decode_state.map_elem_count * 2);
758 }
759 
760 
761 static bool map_restart(zcbor_state_t *state)
762 {
763 	if (!zcbor_process_backup(state, ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_KEEP_DECODE_STATE,
764 				ZCBOR_MAX_ELEM_COUNT)) {
765 		ZCBOR_FAIL();
766 	}
767 
768 	state->elem_count = zcbor_current_max_elem_count(state);
769 	return true;
770 }
771 
772 
773 __attribute__((used))
774 static size_t get_current_index(zcbor_state_t *state, uint32_t index_offset)
775 {
776 	/* Subtract mode because for GET, you want the index you are pointing to, while for SET,
777 	 * you want the one you just processed. This only comes into play when elem_count is even. */
778 	return ((zcbor_current_max_elem_count(state) - state->elem_count - index_offset) / 2);
779 }
780 
781 
782 #ifdef ZCBOR_MAP_SMART_SEARCH
783 #define FLAG_MODE_GET_CURRENT 0
784 #define FLAG_MODE_CLEAR_CURRENT 1
785 #define FLAG_MODE_CLEAR_UNUSED 2
786 
787 static bool manipulate_flags(zcbor_state_t *state, uint32_t mode)
788 {
789 	const size_t last_index = (state->decode_state.map_elem_count - 1);
790 	size_t index = (mode == FLAG_MODE_CLEAR_UNUSED) ? last_index : get_current_index(state, mode);
791 
792 	ZCBOR_ERR_IF((index >= state->decode_state.map_elem_count),
793 		ZCBOR_ERR_MAP_FLAGS_NOT_AVAILABLE);
794 	uint8_t *flag_byte = &state->decode_state.map_search_elem_state[index >> 3];
795 	uint8_t flag_mask = (uint8_t)(1 << (index & 7));
796 
797 	switch(mode) {
798 	case FLAG_MODE_GET_CURRENT:
799 		return (!!(*flag_byte & flag_mask));
800 	case FLAG_MODE_CLEAR_CURRENT:
801 		*flag_byte &= ~flag_mask;
802 		return true;
803 	case FLAG_MODE_CLEAR_UNUSED:
804 		*flag_byte &= (uint8_t)((flag_mask << 1) - 1);
805 		return true;
806 	}
807 	return false;
808 }
809 
810 
811 static bool should_try_key(zcbor_state_t *state)
812 {
813 	return manipulate_flags(state, FLAG_MODE_GET_CURRENT);
814 }
815 
816 
817 bool zcbor_elem_processed(zcbor_state_t *state)
818 {
819 	return manipulate_flags(state, FLAG_MODE_CLEAR_CURRENT);
820 }
821 
822 
823 static bool allocate_map_flags(zcbor_state_t *state, size_t old_flags)
824 {
825 	size_t new_bytes = zcbor_flags_to_bytes(state->decode_state.map_elem_count);
826 	size_t old_bytes = zcbor_flags_to_bytes(old_flags);
827 	size_t extra_bytes = new_bytes - old_bytes;
828 	const uint8_t *flags_end = state->constant_state->map_search_elem_state_end;
829 
830 	if (extra_bytes) {
831 		if ((state->decode_state.map_search_elem_state + new_bytes) > flags_end) {
832 			state->decode_state.map_elem_count
833 				= 8 * (size_t)(flags_end - state->decode_state.map_search_elem_state);
834 			ZCBOR_ERR(ZCBOR_ERR_MAP_FLAGS_NOT_AVAILABLE);
835 		}
836 
837 		memset(&state->decode_state.map_search_elem_state[new_bytes - extra_bytes], 0xFF, extra_bytes);
838 	}
839 	return true;
840 }
841 #else
842 
843 static bool should_try_key(zcbor_state_t *state)
844 {
845 	return (state->decode_state.map_elems_processed < state->decode_state.map_elem_count);
846 }
847 
848 
849 bool zcbor_elem_processed(zcbor_state_t *state)
850 {
851 	if (should_try_key(state)) {
852 		state->decode_state.map_elems_processed++;
853 	}
854 	return true;
855 }
856 #endif
857 
858 
859 static size_t update_map_elem_count(zcbor_state_t *state, size_t elem_count)
860 {
861 	size_t old_map_elem_count = state->decode_state.map_elem_count;
862 
863 	state->decode_state.map_elem_count = MAX(old_map_elem_count, elem_count / 2);
864 	return old_map_elem_count;
865 }
866 
867 
868 static bool handle_map_end(zcbor_state_t *state)
869 {
870 	state->decode_state.counting_map_elems = false;
871 	return map_restart(state);
872 }
873 
874 
875 static bool try_key(zcbor_state_t *state, void *key_result, zcbor_decoder_t key_decoder)
876 {
877 	uint8_t const *payload_bak2 = state->payload;
878 	size_t elem_count_bak = state->elem_count;
879 
880 	if (!key_decoder(state, (uint8_t *)key_result)) {
881 		state->payload = payload_bak2;
882 		state->elem_count = elem_count_bak;
883 		return false;
884 	}
885 
886 	zcbor_log("Found element at index %zu.\n", get_current_index(state, 1));
887 	return true;
888 }
889 
890 
891 bool zcbor_unordered_map_search(zcbor_decoder_t key_decoder, zcbor_state_t *state, void *key_result)
892 {
893 	PRINT_FUNC();
894 	/* elem_count cannot be odd since the map consists of key-value-pairs.
895 	 * This might mean that this function was called while pointing at a value (instead
896 	 * of a key). */
897 	ZCBOR_ERR_IF(state->elem_count & 1, ZCBOR_ERR_MAP_MISALIGNED);
898 
899 	uint8_t const *payload_bak = state->payload;
900 	size_t elem_count = state->elem_count;
901 
902 	/* Loop once through all the elements of the map. */
903 	do {
904 		if (zcbor_array_at_end(state)) {
905 			if (!handle_map_end(state)) {
906 				goto error;
907 			}
908 			continue; /* This continue is needed so the loop stops both if elem_count is
909 			           * at the very start or the very end of the map. */
910 		}
911 
912 		if (state->decode_state.counting_map_elems) {
913 			size_t m_elem_count = ZCBOR_LARGE_ELEM_COUNT - state->elem_count + 2;
914 			size_t old_flags = update_map_elem_count(state, m_elem_count);
915 #ifdef ZCBOR_MAP_SMART_SEARCH
916 			ZCBOR_FAIL_IF(!allocate_map_flags(state, old_flags));
917 #endif
918 			(void)old_flags;
919 		}
920 
921 		if (should_try_key(state) && try_key(state, key_result, key_decoder)) {
922 			if (!state->constant_state->manually_process_elem) {
923 				ZCBOR_FAIL_IF(!zcbor_elem_processed(state));
924 			}
925 			return true;
926 		}
927 
928 		/* Skip over both the key and the value. */
929 		if (!zcbor_any_skip(state, NULL) || !zcbor_any_skip(state, NULL)) {
930 			goto error;
931 		}
932 	} while (state->elem_count != elem_count);
933 
934 	zcbor_error(state, ZCBOR_ERR_ELEM_NOT_FOUND);
935 error:
936 	state->payload = payload_bak;
937 	state->elem_count = elem_count;
938 	ZCBOR_FAIL();
939 }
940 
941 
942 bool zcbor_search_key_bstr_ptr(zcbor_state_t *state, char const *ptr, size_t len)
943 {
944 	struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
945 
946 	return zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_bstr_expect, state, &zs);
947 }
948 
949 
950 bool zcbor_search_key_tstr_ptr(zcbor_state_t *state, char const *ptr, size_t len)
951 {
952 	struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
953 
954 	return zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_tstr_expect, state, &zs);
955 }
956 
957 
958 bool zcbor_search_key_bstr_term(zcbor_state_t *state, char const *str, size_t maxlen)
959 {
960 	return zcbor_search_key_bstr_ptr(state, str, strnlen(str, maxlen));
961 }
962 
963 
964 bool zcbor_search_key_tstr_term(zcbor_state_t *state, char const *str, size_t maxlen)
965 {
966 	return zcbor_search_key_tstr_ptr(state, str, strnlen(str, maxlen));
967 }
968 
969 
970 static bool array_end_expect(zcbor_state_t *state)
971 {
972 	INITIAL_CHECKS();
973 	ZCBOR_ERR_IF(*state->payload != 0xFF, ZCBOR_ERR_WRONG_TYPE);
974 
975 	state->payload++;
976 	return true;
977 }
978 
979 
980 static bool list_map_end_decode(zcbor_state_t *state)
981 {
982 	size_t max_elem_count = 0;
983 
984 #ifndef ZCBOR_CANONICAL
985 	if (state->decode_state.indefinite_length_array) {
986 		if (!array_end_expect(state)) {
987 			ZCBOR_FAIL();
988 		}
989 		max_elem_count = ZCBOR_MAX_ELEM_COUNT;
990 		state->decode_state.indefinite_length_array = false;
991 	}
992 #endif
993 	if (!zcbor_process_backup(state,
994 			ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_CONSUME | ZCBOR_FLAG_KEEP_PAYLOAD,
995 			max_elem_count)) {
996 		ZCBOR_FAIL();
997 	}
998 
999 	return true;
1000 }
1001 
1002 
1003 bool zcbor_list_end_decode(zcbor_state_t *state)
1004 {
1005 	PRINT_FUNC();
1006 	return list_map_end_decode(state);
1007 }
1008 
1009 
1010 bool zcbor_map_end_decode(zcbor_state_t *state)
1011 {
1012 	PRINT_FUNC();
1013 	return list_map_end_decode(state);
1014 }
1015 
1016 
1017 bool zcbor_unordered_map_end_decode(zcbor_state_t *state)
1018 {
1019 	/* Checking zcbor_array_at_end() ensures that check is valid.
1020 	 * In case the map is at the end, but state->decode_state.counting_map_elems isn't updated.*/
1021 	ZCBOR_ERR_IF(!zcbor_array_at_end(state) && state->decode_state.counting_map_elems,
1022 			ZCBOR_ERR_ELEMS_NOT_PROCESSED);
1023 
1024 	if (state->decode_state.map_elem_count > 0) {
1025 #ifdef ZCBOR_MAP_SMART_SEARCH
1026 		manipulate_flags(state, FLAG_MODE_CLEAR_UNUSED);
1027 
1028 		for (size_t i = 0; i < zcbor_flags_to_bytes(state->decode_state.map_elem_count); i++) {
1029 			if (state->decode_state.map_search_elem_state[i] != 0) {
1030 				zcbor_log("unprocessed element(s) in map: [%zu] = 0x%02x\n",
1031 						i, state->decode_state.map_search_elem_state[i]);
1032 				ZCBOR_ERR(ZCBOR_ERR_ELEMS_NOT_PROCESSED);
1033 			}
1034 		}
1035 #else
1036 		ZCBOR_ERR_IF(should_try_key(state), ZCBOR_ERR_ELEMS_NOT_PROCESSED);
1037 #endif
1038 	}
1039 	while (!zcbor_array_at_end(state)) {
1040 		zcbor_any_skip(state, NULL);
1041 	}
1042 	return zcbor_map_end_decode(state);
1043 }
1044 
1045 
1046 bool zcbor_list_map_end_force_decode(zcbor_state_t *state)
1047 {
1048 	if (!zcbor_process_backup(state,
1049 			ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_CONSUME | ZCBOR_FLAG_KEEP_PAYLOAD,
1050 			ZCBOR_MAX_ELEM_COUNT)) {
1051 		ZCBOR_FAIL();
1052 	}
1053 
1054 	return true;
1055 }
1056 
1057 
1058 bool zcbor_simple_decode(zcbor_state_t *state, uint8_t *result)
1059 {
1060 	PRINT_FUNC();
1061 	PRINT_FUNC();
1062 	INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_SIMPLE);
1063 
1064 	/* Simple values must be 0-23 (additional is 0-23) or 24-255 (additional is 24).
1065 	 * Other additional values are not considered simple values. */
1066 	ZCBOR_ERR_IF(ZCBOR_ADDITIONAL(*state->payload) > 24, ZCBOR_ERR_WRONG_TYPE);
1067 
1068 	if (!value_extract(state, result, sizeof(*result))) {
1069 		ZCBOR_FAIL();
1070 	}
1071 	return true;
1072 }
1073 
1074 
1075 bool zcbor_simple_expect(zcbor_state_t *state, uint8_t expected)
1076 {
1077 	PRINT_FUNC();
1078 	uint8_t actual;
1079 
1080 	if (!zcbor_simple_decode(state, &actual)) {
1081 		ZCBOR_FAIL();
1082 	}
1083 
1084 	if (actual != expected) {
1085 		zcbor_log("simple value %u != %u\r\n", actual, expected);
1086 		ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1087 	}
1088 
1089 	return true;
1090 }
1091 
1092 
1093 bool zcbor_simple_pexpect(zcbor_state_t *state, uint8_t *expected)
1094 {
1095 	PRINT_FUNC();
1096 	return zcbor_simple_expect(state, *expected);
1097 }
1098 
1099 
1100 bool zcbor_nil_expect(zcbor_state_t *state, void *unused)
1101 {
1102 	PRINT_FUNC();
1103 	(void)unused;
1104 	return zcbor_simple_expect(state, 22);
1105 }
1106 
1107 
1108 bool zcbor_undefined_expect(zcbor_state_t *state, void *unused)
1109 {
1110 	PRINT_FUNC();
1111 	(void)unused;
1112 	return zcbor_simple_expect(state, 23);
1113 }
1114 
1115 
1116 bool zcbor_bool_decode(zcbor_state_t *state, bool *result)
1117 {
1118 	PRINT_FUNC();
1119 	uint8_t value;
1120 
1121 	if (!zcbor_simple_decode(state, &value)) {
1122 		ZCBOR_FAIL();
1123 	}
1124 	value -= ZCBOR_BOOL_TO_SIMPLE;
1125 	if (value > 1) {
1126 		ERR_RESTORE(ZCBOR_ERR_WRONG_TYPE);
1127 	}
1128 	*result = value;
1129 
1130 	zcbor_log("boolval: %u\r\n", *result);
1131 	return true;
1132 }
1133 
1134 
1135 bool zcbor_bool_expect(zcbor_state_t *state, bool expected)
1136 {
1137 	PRINT_FUNC();
1138 	return zcbor_simple_expect(state, (uint8_t)(!!expected) + ZCBOR_BOOL_TO_SIMPLE);
1139 }
1140 
1141 
1142 bool zcbor_bool_pexpect(zcbor_state_t *state, bool *expected)
1143 {
1144 	PRINT_FUNC();
1145 	return zcbor_bool_expect(state, *expected);
1146 }
1147 
1148 
1149 static bool float_check(zcbor_state_t *state, uint8_t additional_val)
1150 {
1151 	INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_SIMPLE);
1152 	ZCBOR_ERR_IF(ZCBOR_ADDITIONAL(*state->payload) != additional_val, ZCBOR_ERR_FLOAT_SIZE);
1153 	return true;
1154 }
1155 
1156 
1157 bool zcbor_float16_bytes_decode(zcbor_state_t *state, uint16_t *result)
1158 {
1159 	PRINT_FUNC();
1160 	ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_2_BYTES));
1161 
1162 	if (!value_extract(state, result, sizeof(*result))) {
1163 		ZCBOR_FAIL();
1164 	}
1165 
1166 	return true;
1167 }
1168 
1169 
1170 bool zcbor_float16_bytes_expect(zcbor_state_t *state, uint16_t expected)
1171 {
1172 	PRINT_FUNC();
1173 	uint16_t actual;
1174 
1175 	if (!zcbor_float16_bytes_decode(state, &actual)) {
1176 		ZCBOR_FAIL();
1177 	}
1178 	if (actual != expected) {
1179 		ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1180 	}
1181 	return true;
1182 }
1183 
1184 
1185 bool zcbor_float16_bytes_pexpect(zcbor_state_t *state, uint16_t *expected)
1186 {
1187 	PRINT_FUNC();
1188 	return zcbor_float16_bytes_expect(state, *expected);
1189 }
1190 
1191 
1192 bool zcbor_float16_decode(zcbor_state_t *state, float *result)
1193 {
1194 	PRINT_FUNC();
1195 	uint16_t value16;
1196 
1197 	if (!zcbor_float16_bytes_decode(state, &value16)) {
1198 		ZCBOR_FAIL();
1199 	}
1200 
1201 	*result = zcbor_float16_to_32(value16);
1202 	return true;
1203 }
1204 
1205 
1206 bool zcbor_float16_expect(zcbor_state_t *state, float expected)
1207 {
1208 	PRINT_FUNC();
1209 	float actual;
1210 
1211 	if (!zcbor_float16_decode(state, &actual)) {
1212 		ZCBOR_FAIL();
1213 	}
1214 	if (actual != expected) {
1215 		ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1216 	}
1217 	return true;
1218 }
1219 
1220 
1221 bool zcbor_float16_pexpect(zcbor_state_t *state, float *expected)
1222 {
1223 	PRINT_FUNC();
1224 	return zcbor_float16_expect(state, *expected);
1225 }
1226 
1227 
1228 bool zcbor_float32_decode(zcbor_state_t *state, float *result)
1229 {
1230 	PRINT_FUNC();
1231 	ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_4_BYTES));
1232 
1233 	if (!value_extract(state, result, sizeof(*result))) {
1234 		ZCBOR_FAIL();
1235 	}
1236 
1237 	return true;
1238 }
1239 
1240 
1241 bool zcbor_float32_expect(zcbor_state_t *state, float expected)
1242 {
1243 	PRINT_FUNC();
1244 	float actual;
1245 
1246 	if (!zcbor_float32_decode(state, &actual)) {
1247 		ZCBOR_FAIL();
1248 	}
1249 	if (actual != expected) {
1250 		ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1251 	}
1252 	return true;
1253 }
1254 
1255 
1256 bool zcbor_float32_pexpect(zcbor_state_t *state, float *expected)
1257 {
1258 	PRINT_FUNC();
1259 	return zcbor_float32_expect(state, *expected);
1260 }
1261 
1262 
1263 bool zcbor_float16_32_decode(zcbor_state_t *state, float *result)
1264 {
1265 	PRINT_FUNC();
1266 	if (zcbor_float16_decode(state, result)) {
1267 		/* Do nothing */
1268 	} else if (!zcbor_float32_decode(state, result)) {
1269 		ZCBOR_FAIL();
1270 	}
1271 
1272 	return true;
1273 }
1274 
1275 
1276 bool zcbor_float16_32_expect(zcbor_state_t *state, float expected)
1277 {
1278 	PRINT_FUNC();
1279 	if (zcbor_float16_expect(state, expected)) {
1280 		/* Do nothing */
1281 	} else if (!zcbor_float32_expect(state, expected)) {
1282 		ZCBOR_FAIL();
1283 	}
1284 
1285 	return true;
1286 }
1287 
1288 
1289 bool zcbor_float16_32_pexpect(zcbor_state_t *state, float *expected)
1290 {
1291 	PRINT_FUNC();
1292 	return zcbor_float16_32_expect(state, *expected);
1293 }
1294 
1295 
1296 bool zcbor_float64_decode(zcbor_state_t *state, double *result)
1297 {
1298 	PRINT_FUNC();
1299 	ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_8_BYTES));
1300 
1301 	if (!value_extract(state, result, sizeof(*result))) {
1302 		ZCBOR_FAIL();
1303 	}
1304 
1305 	return true;
1306 }
1307 
1308 
1309 bool zcbor_float64_expect(zcbor_state_t *state, double expected)
1310 {
1311 	PRINT_FUNC();
1312 	double actual;
1313 
1314 	if (!zcbor_float64_decode(state, &actual)) {
1315 		ZCBOR_FAIL();
1316 	}
1317 	if (actual != expected) {
1318 		ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1319 	}
1320 	return true;
1321 }
1322 
1323 
1324 bool zcbor_float64_pexpect(zcbor_state_t *state, double *expected)
1325 {
1326 	PRINT_FUNC();
1327 	return zcbor_float64_expect(state, *expected);
1328 }
1329 
1330 
1331 bool zcbor_float32_64_decode(zcbor_state_t *state, double *result)
1332 {
1333 	PRINT_FUNC();
1334 	float float_result;
1335 
1336 	if (zcbor_float32_decode(state, &float_result)) {
1337 		*result = (double)float_result;
1338 	} else if (!zcbor_float64_decode(state, result)) {
1339 		ZCBOR_FAIL();
1340 	}
1341 
1342 	return true;
1343 }
1344 
1345 
1346 bool zcbor_float32_64_expect(zcbor_state_t *state, double expected)
1347 {
1348 	PRINT_FUNC();
1349 	if (zcbor_float64_expect(state, expected)) {
1350 		/* Do nothing */
1351 	} else if (!zcbor_float32_expect(state, (float)expected)) {
1352 		ZCBOR_FAIL();
1353 	}
1354 
1355 	return true;
1356 }
1357 
1358 
1359 bool zcbor_float32_64_pexpect(zcbor_state_t *state, double *expected)
1360 {
1361 	PRINT_FUNC();
1362 	return zcbor_float32_64_expect(state, *expected);
1363 }
1364 
1365 
1366 bool zcbor_float_decode(zcbor_state_t *state, double *result)
1367 {
1368 	PRINT_FUNC();
1369 	float float_result;
1370 
1371 	if (zcbor_float16_decode(state, &float_result)) {
1372 		*result = (double)float_result;
1373 	} else if (zcbor_float32_decode(state, &float_result)) {
1374 		*result = (double)float_result;
1375 	} else if (!zcbor_float64_decode(state, result)) {
1376 		ZCBOR_FAIL();
1377 	}
1378 
1379 	return true;
1380 }
1381 
1382 
1383 bool zcbor_float_expect(zcbor_state_t *state, double expected)
1384 {
1385 	PRINT_FUNC();
1386 	if (zcbor_float16_expect(state, (float)expected)) {
1387 		/* Do nothing */
1388 	} else if (zcbor_float32_expect(state, (float)expected)) {
1389 		/* Do nothing */
1390 	} else if (!zcbor_float64_expect(state, expected)) {
1391 		ZCBOR_FAIL();
1392 	}
1393 
1394 	return true;
1395 }
1396 
1397 
1398 bool zcbor_float_pexpect(zcbor_state_t *state, double *expected)
1399 {
1400 	PRINT_FUNC();
1401 	return zcbor_float_expect(state, *expected);
1402 }
1403 
1404 
1405 bool zcbor_any_skip(zcbor_state_t *state, void *result)
1406 {
1407 	PRINT_FUNC();
1408 	zcbor_assert_state(result == NULL,
1409 			"'any' type cannot be returned, only skipped.\r\n");
1410 	(void)result;
1411 
1412 	INITIAL_CHECKS();
1413 	zcbor_major_type_t major_type = ZCBOR_MAJOR_TYPE(*state->payload);
1414 	uint8_t additional = ZCBOR_ADDITIONAL(*state->payload);
1415 	uint64_t value = 0; /* In case of indefinite_length_array. */
1416 	zcbor_state_t state_copy;
1417 
1418 	memcpy(&state_copy, state, sizeof(zcbor_state_t));
1419 
1420 	while (major_type == ZCBOR_MAJOR_TYPE_TAG) {
1421 		uint32_t tag_dummy;
1422 
1423 		if (!zcbor_tag_decode(&state_copy, &tag_dummy)) {
1424 			ZCBOR_FAIL();
1425 		}
1426 		ZCBOR_ERR_IF(state_copy.payload >= state_copy.payload_end, ZCBOR_ERR_NO_PAYLOAD);
1427 		major_type = ZCBOR_MAJOR_TYPE(*state_copy.payload);
1428 		additional = ZCBOR_ADDITIONAL(*state_copy.payload);
1429 	}
1430 
1431 #ifdef ZCBOR_CANONICAL
1432 	const bool indefinite_length_array = false;
1433 #else
1434 	const bool indefinite_length_array = ((additional == ZCBOR_VALUE_IS_INDEFINITE_LENGTH)
1435 		&& ((major_type == ZCBOR_MAJOR_TYPE_LIST) || (major_type == ZCBOR_MAJOR_TYPE_MAP)));
1436 #endif
1437 
1438 	if (!indefinite_length_array && !value_extract(&state_copy, &value, sizeof(value))) {
1439 		/* Can happen because of elem_count (or payload_end) */
1440 		ZCBOR_FAIL();
1441 	}
1442 
1443 	switch (major_type) {
1444 		case ZCBOR_MAJOR_TYPE_BSTR:
1445 		case ZCBOR_MAJOR_TYPE_TSTR:
1446 			/* 'value' is the length of the BSTR or TSTR.
1447 			 * The subtraction is safe because value_extract() above
1448 			 * checks that payload_end is greater than payload. */
1449 			ZCBOR_ERR_IF(
1450 				value > (uint64_t)(state_copy.payload_end - state_copy.payload),
1451 				ZCBOR_ERR_NO_PAYLOAD);
1452 			(state_copy.payload) += value;
1453 			break;
1454 		case ZCBOR_MAJOR_TYPE_MAP:
1455 			ZCBOR_ERR_IF(value > (SIZE_MAX / 2), ZCBOR_ERR_INT_SIZE);
1456 			value *= 2;
1457 			/* fallthrough */
1458 		case ZCBOR_MAJOR_TYPE_LIST:
1459 			if (indefinite_length_array) {
1460 				state_copy.payload++;
1461 				value = ZCBOR_LARGE_ELEM_COUNT;
1462 			}
1463 			state_copy.elem_count = (size_t)value;
1464 			state_copy.decode_state.indefinite_length_array = indefinite_length_array;
1465 			while (!zcbor_array_at_end(&state_copy)) {
1466 				if (!zcbor_any_skip(&state_copy, NULL)) {
1467 					ZCBOR_FAIL();
1468 				}
1469 			}
1470 			if (indefinite_length_array && !array_end_expect(&state_copy)) {
1471 				ZCBOR_FAIL();
1472 			}
1473 			break;
1474 		default:
1475 			/* Do nothing */
1476 			break;
1477 	}
1478 
1479 	state->payload = state_copy.payload;
1480 	state->elem_count--;
1481 
1482 	return true;
1483 }
1484 
1485 
1486 bool zcbor_tag_decode(zcbor_state_t *state, uint32_t *result)
1487 {
1488 	PRINT_FUNC();
1489 	INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_TAG);
1490 
1491 	if (!value_extract(state, result, sizeof(*result))) {
1492 		ZCBOR_FAIL();
1493 	}
1494 	state->elem_count++;
1495 	return true;
1496 }
1497 
1498 
1499 bool zcbor_tag_expect(zcbor_state_t *state, uint32_t expected)
1500 {
1501 	PRINT_FUNC();
1502 	uint32_t actual;
1503 
1504 	if (!zcbor_tag_decode(state, &actual)) {
1505 		ZCBOR_FAIL();
1506 	}
1507 	if (actual != expected) {
1508 		ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1509 	}
1510 	return true;
1511 }
1512 
1513 
1514 bool zcbor_tag_pexpect(zcbor_state_t *state, uint32_t *expected)
1515 {
1516 	PRINT_FUNC();
1517 	return zcbor_tag_expect(state, *expected);
1518 }
1519 
1520 
1521 bool zcbor_multi_decode(size_t min_decode,
1522 		size_t max_decode,
1523 		size_t *num_decode,
1524 		zcbor_decoder_t decoder,
1525 		zcbor_state_t *state,
1526 		void *result,
1527 		size_t result_len)
1528 {
1529 	PRINT_FUNC();
1530 	ZCBOR_CHECK_ERROR();
1531 	for (size_t i = 0; i < max_decode; i++) {
1532 		uint8_t const *payload_bak = state->payload;
1533 		size_t elem_count_bak = state->elem_count;
1534 
1535 		if (!decoder(state,
1536 				(uint8_t *)result + i*result_len)) {
1537 			*num_decode = i;
1538 			state->payload = payload_bak;
1539 			state->elem_count = elem_count_bak;
1540 			ZCBOR_ERR_IF(i < min_decode, ZCBOR_ERR_ITERATIONS);
1541 			zcbor_log("Found %zu elements.\r\n", i);
1542 			return true;
1543 		}
1544 	}
1545 	zcbor_log("Found %zu elements.\r\n", max_decode);
1546 	*num_decode = max_decode;
1547 	return true;
1548 }
1549 
1550 
1551 bool zcbor_present_decode(bool *present,
1552 		zcbor_decoder_t decoder,
1553 		zcbor_state_t *state,
1554 		void *result)
1555 {
1556 	PRINT_FUNC();
1557 	size_t num_decode = 0;
1558 	bool retval = zcbor_multi_decode(0, 1, &num_decode, decoder, state, result, 0);
1559 
1560 	zcbor_assert_state(retval, "zcbor_multi_decode should not fail with these parameters.\r\n");
1561 
1562 	*present = !!num_decode;
1563 	return retval;
1564 }
1565 
1566 
1567 void zcbor_new_decode_state(zcbor_state_t *state_array, size_t n_states,
1568 		const uint8_t *payload, size_t payload_len, size_t elem_count,
1569 		uint8_t *flags, size_t flags_bytes)
1570 {
1571 	zcbor_new_state(state_array, n_states, payload, payload_len, elem_count, flags, flags_bytes);
1572 }
1573