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