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