Branch data Line data Source code
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 : : */
19 : 156 : static size_t additional_len(uint8_t additional)
20 : : {
21 [ + + ]: 156 : if (additional <= ZCBOR_VALUE_IN_HEADER) {
22 : : return 0;
23 [ + - ]: 87 : } 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 : 87 : return 1U << (additional - ZCBOR_VALUE_IS_1_BYTE);
30 : : }
31 : : return 0xF;
32 : : }
33 : :
34 : :
35 : 421 : static bool initial_checks(zcbor_state_t *state)
36 : : {
37 : 421 : ZCBOR_CHECK_ERROR();
38 [ + + ]: 421 : ZCBOR_CHECK_PAYLOAD();
39 : : return true;
40 : : }
41 : :
42 : :
43 : 238 : static bool type_check(zcbor_state_t *state, zcbor_major_type_t exp_major_type)
44 : : {
45 [ + + ]: 238 : if (!initial_checks(state)) {
46 : 165 : ZCBOR_FAIL();
47 : : }
48 : 165 : zcbor_major_type_t major_type = ZCBOR_MAJOR_TYPE(*state->payload);
49 : :
50 [ + + ]: 165 : if (major_type != exp_major_type) {
51 : 35 : 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 : :
71 : 48 : static void err_restore(zcbor_state_t *state, int err)
72 : : {
73 : 48 : state->payload = state->payload_bak;
74 : 48 : state->elem_count++;
75 : 48 : zcbor_error(state, err);
76 : 48 : }
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 : :
94 : 87 : 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 [ + + ]: 182 : for (size_t i = 0; i < src_len; i++) {
100 : 95 : dst[i] = src[src_len - 1 - i];
101 : : }
102 : : #endif /* ZCBOR_BIG_ENDIAN */
103 : 87 : }
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 : : */
139 : 156 : static bool value_extract(zcbor_state_t *state,
140 : : void *const result, size_t result_len, bool *indefinite_length_array)
141 : : {
142 : 156 : zcbor_trace(state, "value_extract");
143 : 156 : zcbor_assert_state(result_len != 0, "0-length result not supported.\r\n");
144 : 156 : zcbor_assert_state(result_len <= 8, "result sizes above 8 bytes not supported.\r\n");
145 : 156 : zcbor_assert_state(state != NULL, "state cannot be NULL.\r\n");
146 : 156 : zcbor_assert_state(result != NULL, "result cannot be NULL.\r\n");
147 : :
148 [ - + ]: 156 : INITIAL_CHECKS();
149 [ - + ]: 156 : ZCBOR_ERR_IF((state->elem_count == 0), ZCBOR_ERR_LOW_ELEM_COUNT);
150 : :
151 : 156 : uint8_t header_byte = *state->payload;
152 : 156 : uint8_t additional = ZCBOR_ADDITIONAL(header_byte);
153 : 156 : size_t len = 0;
154 : :
155 [ - + ]: 156 : if ((additional == ZCBOR_VALUE_IS_INDEFINITE_LENGTH) && (indefinite_length_array != NULL)) {
156 : : /* Indefinite length is not allowed in canonical CBOR */
157 [ # # # # ]: 0 : ZCBOR_ERR_IF(ZCBOR_ENFORCE_CANONICAL(state),
158 : : ZCBOR_ERR_INVALID_VALUE_ENCODING);
159 : :
160 : 0 : *indefinite_length_array = true;
161 : : } else {
162 : 156 : len = additional_len(additional);
163 : 156 : uint8_t *result_offs = (uint8_t *)result + ZCBOR_ECPY_OFFS(result_len, MAX(1, len));
164 : :
165 [ - + ]: 156 : ZCBOR_ERR_IF(additional > ZCBOR_VALUE_IS_8_BYTES, ZCBOR_ERR_ADDITIONAL_INVAL);
166 [ - + ]: 156 : ZCBOR_ERR_IF(len > result_len, ZCBOR_ERR_INT_SIZE);
167 [ - + ]: 156 : ZCBOR_ERR_IF((state->payload + len + 1) > state->payload_end,
168 : : ZCBOR_ERR_NO_PAYLOAD);
169 : :
170 [ + + ]: 156 : memset(result, 0, result_len);
171 : :
172 [ + + ]: 156 : if (len == 0) {
173 : 69 : *result_offs = additional;
174 : : } else {
175 : 87 : 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 [ + - - + : 87 : if (ZCBOR_ENFORCE_CANONICAL(state) && !ZCBOR_IS_FLOAT(header_byte)) {
- + ]
180 [ + - ]: 87 : ZCBOR_ERR_IF((zcbor_header_len_ptr(result, result_len) != (len + 1)),
181 : : ZCBOR_ERR_INVALID_VALUE_ENCODING);
182 : : }
183 : : }
184 : : }
185 : :
186 : 156 : state->payload_bak = state->payload;
187 : 156 : (state->payload) += len + 1;
188 : 156 : (state->elem_count)--;
189 : 156 : return true;
190 : : }
191 : :
192 : 26 : 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 : 26 : int8_t msbyte = ((int8_t *)result)[result_size - 1];
198 : : #endif
199 : 26 : uint8_t *result_uint8 = (uint8_t *)result;
200 : :
201 [ + - ]: 26 : 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 [ + + ]: 26 : if (major_type == ZCBOR_MAJOR_TYPE_NINT) {
207 : : /* Convert from CBOR's representation by flipping all bits. */
208 [ + + ]: 100 : for (unsigned int i = 0; i < result_size; i++) {
209 : 80 : result_uint8[i] = (uint8_t)~result_uint8[i];
210 : : }
211 : : }
212 : :
213 : : return ZCBOR_SUCCESS;
214 : : }
215 : :
216 : :
217 : 27 : bool zcbor_int_decode(zcbor_state_t *state, void *result, size_t result_size)
218 : : {
219 : 27 : PRINT_FUNC();
220 [ - + ]: 27 : INITIAL_CHECKS();
221 : 27 : zcbor_major_type_t major_type = ZCBOR_MAJOR_TYPE(*state->payload);
222 : :
223 [ + + ]: 27 : 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 : 1 : ZCBOR_ERR(ZCBOR_ERR_WRONG_TYPE);
227 : : }
228 : :
229 [ - + ]: 26 : if (!value_extract(state, result, result_size, NULL)) {
230 : 26 : ZCBOR_FAIL();
231 : : }
232 : :
233 : 26 : int err = val_to_int(major_type, result, result_size);
234 [ - + ]: 26 : if (err != ZCBOR_SUCCESS) {
235 : 0 : ERR_RESTORE(err);
236 : : }
237 : :
238 : : return true;
239 : : }
240 : :
241 : :
242 : 27 : bool zcbor_int32_decode(zcbor_state_t *state, int32_t *result)
243 : : {
244 : 27 : PRINT_FUNC();
245 : 27 : return zcbor_int_decode(state, result, sizeof(*result));
246 : : }
247 : :
248 : :
249 : 0 : bool zcbor_int64_decode(zcbor_state_t *state, int64_t *result)
250 : : {
251 : 0 : PRINT_FUNC();
252 : 0 : return zcbor_int_decode(state, result, sizeof(*result));
253 : : }
254 : :
255 : :
256 : 162 : bool zcbor_uint_decode(zcbor_state_t *state, void *result, size_t result_size)
257 : : {
258 : 162 : PRINT_FUNC();
259 [ + + ]: 162 : INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PINT);
260 : :
261 [ - + ]: 66 : 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 : :
269 : 0 : bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result)
270 : : {
271 : 0 : PRINT_FUNC();
272 : 0 : return zcbor_uint_decode(state, result, sizeof(*result));
273 : : }
274 : :
275 : :
276 : 0 : bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t result)
277 : : {
278 : 0 : PRINT_FUNC();
279 [ # # ]: 0 : if (!zcbor_union_elem_code(state)) {
280 : 0 : ZCBOR_FAIL();
281 : : }
282 : 0 : return zcbor_int32_expect(state, result);
283 : : }
284 : :
285 : :
286 : 0 : bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t result)
287 : : {
288 : 0 : PRINT_FUNC();
289 [ # # ]: 0 : if (!zcbor_union_elem_code(state)) {
290 : 0 : ZCBOR_FAIL();
291 : : }
292 : 0 : return zcbor_int64_expect(state, result);
293 : : }
294 : :
295 : :
296 : 0 : bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t result)
297 : : {
298 : 0 : PRINT_FUNC();
299 [ # # ]: 0 : if (!zcbor_union_elem_code(state)) {
300 : 0 : ZCBOR_FAIL();
301 : : }
302 : 0 : return zcbor_uint32_expect(state, result);
303 : : }
304 : :
305 : :
306 : 0 : bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t result)
307 : : {
308 : 0 : PRINT_FUNC();
309 [ # # ]: 0 : if (!zcbor_union_elem_code(state)) {
310 : 0 : ZCBOR_FAIL();
311 : : }
312 : 0 : return zcbor_uint64_expect(state, result);
313 : : }
314 : :
315 : :
316 : 0 : bool zcbor_int32_expect(zcbor_state_t *state, int32_t expected)
317 : : {
318 : 0 : PRINT_FUNC();
319 : 0 : return zcbor_int64_expect(state, expected);
320 : : }
321 : :
322 : :
323 : 0 : bool zcbor_int32_pexpect(zcbor_state_t *state, int32_t *expected)
324 : : {
325 : 0 : PRINT_FUNC();
326 : 0 : return zcbor_int32_expect(state, *expected);
327 : : }
328 : :
329 : :
330 : 0 : bool zcbor_int64_expect(zcbor_state_t *state, int64_t expected)
331 : : {
332 : 0 : PRINT_FUNC();
333 : 0 : int64_t actual;
334 : :
335 [ # # ]: 0 : if (!zcbor_int64_decode(state, &actual)) {
336 : 0 : ZCBOR_FAIL();
337 : : }
338 : :
339 [ # # ]: 0 : if (actual != expected) {
340 : 0 : zcbor_log("%" PRIi64 " != %" PRIi64 "\r\n", actual, expected);
341 : 0 : ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
342 : : }
343 : : return true;
344 : : }
345 : :
346 : :
347 : 0 : bool zcbor_int64_pexpect(zcbor_state_t *state, int64_t *expected)
348 : : {
349 : 0 : PRINT_FUNC();
350 : 0 : return zcbor_int64_expect(state, *expected);
351 : : }
352 : :
353 : :
354 : 162 : bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result)
355 : : {
356 : 162 : PRINT_FUNC();
357 : 162 : return zcbor_uint_decode(state, result, sizeof(*result));
358 : : }
359 : :
360 : :
361 : : #ifdef ZCBOR_SUPPORTS_SIZE_T
362 : 0 : bool zcbor_size_decode(zcbor_state_t *state, size_t *result)
363 : : {
364 : 0 : PRINT_FUNC();
365 : 0 : return zcbor_uint_decode(state, result, sizeof(*result));
366 : : }
367 : : #endif
368 : :
369 : :
370 : 162 : bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t expected)
371 : : {
372 : 162 : PRINT_FUNC();
373 : 162 : return zcbor_uint64_expect(state, expected);
374 : : }
375 : :
376 : :
377 : 0 : bool zcbor_uint32_pexpect(zcbor_state_t *state, uint32_t *expected)
378 : : {
379 : 0 : PRINT_FUNC();
380 : 0 : return zcbor_uint32_expect(state, *expected);
381 : : }
382 : :
383 : :
384 : 162 : bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t expected)
385 : : {
386 : 162 : PRINT_FUNC();
387 : 162 : uint64_t actual;
388 : :
389 [ + + ]: 162 : if (!zcbor_uint64_decode(state, &actual)) {
390 : 66 : ZCBOR_FAIL();
391 : : }
392 [ + + ]: 66 : if (actual != expected) {
393 : 48 : zcbor_log("%" PRIu64 " != %" PRIu64 "\r\n", actual, expected);
394 : 48 : ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
395 : : }
396 : : return true;
397 : : }
398 : :
399 : :
400 : 0 : bool zcbor_uint64_pexpect(zcbor_state_t *state, uint64_t *expected)
401 : : {
402 : 0 : PRINT_FUNC();
403 : 0 : return zcbor_uint64_expect(state, *expected);
404 : : }
405 : :
406 : :
407 : : #ifdef ZCBOR_SUPPORTS_SIZE_T
408 : 0 : bool zcbor_size_expect(zcbor_state_t *state, size_t expected)
409 : : {
410 : 0 : PRINT_FUNC();
411 : 0 : return zcbor_uint64_expect(state, expected);
412 : : }
413 : :
414 : :
415 : 0 : bool zcbor_size_pexpect(zcbor_state_t *state, size_t *expected)
416 : : {
417 : 0 : PRINT_FUNC();
418 : 0 : return zcbor_size_expect(state, *expected);
419 : : }
420 : : #endif
421 : :
422 : :
423 : 43 : static bool str_start_decode(zcbor_state_t *state,
424 : : struct zcbor_string *result, zcbor_major_type_t exp_major_type)
425 : : {
426 [ + + ]: 43 : INITIAL_CHECKS_WITH_TYPE(exp_major_type);
427 : :
428 [ - + ]: 34 : if (!value_extract(state, &result->len, sizeof(result->len), NULL)) {
429 : 34 : ZCBOR_FAIL();
430 : : }
431 : :
432 : 34 : result->value = state->payload;
433 : 34 : 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 : : */
449 : 34 : 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 [ - + ]: 34 : if (len > (size_t)(state->payload_end - state->payload)) {
454 : : zcbor_log("error: 0x%zu > 0x%zu\r\n",
455 : : len,
456 : 0 : (state->payload_end - state->payload));
457 : 0 : ERR_RESTORE(ZCBOR_ERR_NO_PAYLOAD);
458 : : }
459 : : return true;
460 : : }
461 : :
462 : :
463 : 43 : 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 : 43 : bool res = str_start_decode(state, result, exp_major_type);
467 : :
468 [ + + - + ]: 43 : if (!res || !str_overflow_check(state, result->len)) {
469 : 9 : ZCBOR_FAIL();
470 : : }
471 : :
472 : : return true;
473 : : }
474 : :
475 : :
476 : 0 : bool zcbor_bstr_start_decode(zcbor_state_t *state, struct zcbor_string *result)
477 : : {
478 : 0 : PRINT_FUNC();
479 : 0 : struct zcbor_string dummy;
480 [ # # ]: 0 : if (result == NULL) {
481 : 0 : result = &dummy;
482 : : }
483 : :
484 [ # # ]: 0 : if(!str_start_decode_with_overflow_check(state, result, ZCBOR_MAJOR_TYPE_BSTR)) {
485 : 0 : ZCBOR_FAIL();
486 : : }
487 : :
488 [ # # ]: 0 : if (!zcbor_new_backup(state, ZCBOR_MAX_ELEM_COUNT)) {
489 : 0 : FAIL_RESTORE();
490 : : }
491 : :
492 : 0 : state->payload_end = result->value + result->len;
493 : 0 : return true;
494 : : }
495 : :
496 : :
497 : 0 : bool zcbor_bstr_end_decode(zcbor_state_t *state)
498 : : {
499 [ # # ]: 0 : ZCBOR_ERR_IF(state->payload != state->payload_end, ZCBOR_ERR_PAYLOAD_NOT_CONSUMED);
500 : :
501 [ # # ]: 0 : 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 : :
511 : 0 : static void partition_fragment(const zcbor_state_t *state,
512 : : struct zcbor_string_fragment *result)
513 : : {
514 : 0 : result->fragment.len = MIN(result->fragment.len,
515 : : (size_t)state->payload_end - (size_t)state->payload);
516 : 0 : }
517 : :
518 : :
519 : 0 : 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 : 0 : PRINT_FUNC();
524 [ # # ]: 0 : if(!str_start_decode(state, &result->fragment, exp_major_type)) {
525 : 0 : ZCBOR_FAIL();
526 : : }
527 : :
528 : 0 : result->offset = 0;
529 : 0 : result->total_len = result->fragment.len;
530 : 0 : partition_fragment(state, result);
531 : 0 : state->payload_end = state->payload + result->fragment.len;
532 : :
533 : 0 : return true;
534 : : }
535 : :
536 : 0 : bool zcbor_bstr_start_decode_fragment(zcbor_state_t *state,
537 : : struct zcbor_string_fragment *result)
538 : : {
539 : 0 : PRINT_FUNC();
540 [ # # ]: 0 : if (!start_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR)) {
541 : 0 : ZCBOR_FAIL();
542 : : }
543 [ # # ]: 0 : if (!zcbor_new_backup(state, ZCBOR_MAX_ELEM_COUNT)) {
544 : 0 : FAIL_RESTORE();
545 : : }
546 : : return true;
547 : : }
548 : :
549 : :
550 : 0 : void zcbor_next_fragment(zcbor_state_t *state,
551 : : struct zcbor_string_fragment *prev_fragment,
552 : : struct zcbor_string_fragment *result)
553 : : {
554 : 0 : memcpy(result, prev_fragment, sizeof(*result));
555 : 0 : result->fragment.value = state->payload_mut;
556 : 0 : result->offset += prev_fragment->fragment.len;
557 : 0 : result->fragment.len = result->total_len - result->offset;
558 : :
559 : 0 : partition_fragment(state, result);
560 : 0 : zcbor_log("New fragment length %zu\r\n", result->fragment.len);
561 : :
562 : 0 : state->payload += result->fragment.len;
563 : 0 : }
564 : :
565 : :
566 : 0 : void zcbor_bstr_next_fragment(zcbor_state_t *state,
567 : : struct zcbor_string_fragment *prev_fragment,
568 : : struct zcbor_string_fragment *result)
569 : : {
570 : 0 : memcpy(result, prev_fragment, sizeof(*result));
571 : 0 : result->fragment.value = state->payload_mut;
572 : 0 : result->offset += prev_fragment->fragment.len;
573 : 0 : result->fragment.len = result->total_len - result->offset;
574 : :
575 : 0 : partition_fragment(state, result);
576 : 0 : zcbor_log("fragment length %zu\r\n", result->fragment.len);
577 : 0 : state->payload_end = state->payload + result->fragment.len;
578 : 0 : }
579 : :
580 : :
581 : 0 : bool zcbor_is_last_fragment(const struct zcbor_string_fragment *fragment)
582 : : {
583 : 0 : return (fragment->total_len == (fragment->offset + fragment->fragment.len));
584 : : }
585 : :
586 : :
587 : 43 : static bool str_decode(zcbor_state_t *state, struct zcbor_string *result,
588 : : zcbor_major_type_t exp_major_type)
589 : : {
590 [ + + ]: 43 : if (!str_start_decode_with_overflow_check(state, result, exp_major_type)) {
591 : 34 : ZCBOR_FAIL();
592 : : }
593 : :
594 : 34 : state->payload += result->len;
595 : 34 : return true;
596 : : }
597 : :
598 : :
599 : 0 : static bool str_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result,
600 : : zcbor_major_type_t exp_major_type)
601 : : {
602 [ # # ]: 0 : if (!start_decode_fragment(state, result, exp_major_type)) {
603 : 0 : ZCBOR_FAIL();
604 : : }
605 : :
606 : 0 : (state->payload) += result->fragment.len;
607 : 0 : return true;
608 : : }
609 : :
610 : :
611 : 0 : static bool str_expect(zcbor_state_t *state, struct zcbor_string *result,
612 : : zcbor_major_type_t exp_major_type)
613 : : {
614 : 0 : struct zcbor_string tmp_result;
615 : :
616 [ # # ]: 0 : if (!str_decode(state, &tmp_result, exp_major_type)) {
617 : 0 : ZCBOR_FAIL();
618 : : }
619 [ # # ]: 0 : if (!zcbor_compare_strings(&tmp_result, result)) {
620 : 0 : ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
621 : : }
622 : : return true;
623 : : }
624 : :
625 : :
626 : 43 : bool zcbor_bstr_decode(zcbor_state_t *state, struct zcbor_string *result)
627 : : {
628 : 43 : PRINT_FUNC();
629 : 43 : return str_decode(state, result, ZCBOR_MAJOR_TYPE_BSTR);
630 : : }
631 : :
632 : :
633 : 0 : bool zcbor_bstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result)
634 : : {
635 : 0 : PRINT_FUNC();
636 : 0 : return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR);
637 : : }
638 : :
639 : :
640 : 0 : bool zcbor_bstr_expect(zcbor_state_t *state, struct zcbor_string *expected)
641 : : {
642 : 0 : PRINT_FUNC();
643 : 0 : return str_expect(state, expected, ZCBOR_MAJOR_TYPE_BSTR);
644 : : }
645 : :
646 : :
647 : 0 : bool zcbor_tstr_decode(zcbor_state_t *state, struct zcbor_string *result)
648 : : {
649 : 0 : PRINT_FUNC();
650 : 0 : return str_decode(state, result, ZCBOR_MAJOR_TYPE_TSTR);
651 : : }
652 : :
653 : :
654 : 0 : bool zcbor_tstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result)
655 : : {
656 : 0 : PRINT_FUNC();
657 : 0 : return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_TSTR);
658 : : }
659 : :
660 : :
661 : 0 : bool zcbor_tstr_expect(zcbor_state_t *state, struct zcbor_string *expected)
662 : : {
663 : 0 : PRINT_FUNC();
664 : 0 : return str_expect(state, expected, ZCBOR_MAJOR_TYPE_TSTR);
665 : : }
666 : :
667 : :
668 : 0 : bool zcbor_bstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len)
669 : : {
670 : 0 : PRINT_FUNC();
671 : 0 : struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
672 : :
673 : 0 : return zcbor_bstr_expect(state, &zs);
674 : : }
675 : :
676 : :
677 : 0 : bool zcbor_tstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len)
678 : : {
679 : 0 : PRINT_FUNC();
680 : 0 : struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
681 : :
682 : 0 : return zcbor_tstr_expect(state, &zs);
683 : : }
684 : :
685 : :
686 : 0 : bool zcbor_bstr_expect_term(zcbor_state_t *state, char const *string, size_t maxlen)
687 : : {
688 : 0 : PRINT_FUNC();
689 : 0 : return zcbor_bstr_expect_ptr(state, string, strnlen(string, maxlen));
690 : : }
691 : :
692 : :
693 : 0 : bool zcbor_tstr_expect_term(zcbor_state_t *state, char const *string, size_t maxlen)
694 : : {
695 : 0 : PRINT_FUNC();
696 : 0 : return zcbor_tstr_expect_ptr(state, string, strnlen(string, maxlen));
697 : : }
698 : :
699 : :
700 : 33 : static bool list_map_start_decode(zcbor_state_t *state,
701 : : zcbor_major_type_t exp_major_type)
702 : : {
703 : 33 : size_t new_elem_count;
704 : 33 : bool indefinite_length_array = false;
705 : :
706 [ + + ]: 33 : INITIAL_CHECKS_WITH_TYPE(exp_major_type);
707 : :
708 [ - + ]: 30 : if (!value_extract(state, &new_elem_count, sizeof(new_elem_count), &indefinite_length_array)) {
709 : 30 : ZCBOR_FAIL();
710 : : }
711 : :
712 [ + - - + ]: 30 : if (!zcbor_new_backup(state, indefinite_length_array
713 : : ? ZCBOR_LARGE_ELEM_COUNT : new_elem_count)) {
714 : 0 : FAIL_RESTORE();
715 : : }
716 : :
717 : 30 : state->decode_state.indefinite_length_array = indefinite_length_array;
718 : :
719 : 30 : return true;
720 : : }
721 : :
722 : :
723 : 15 : bool zcbor_list_start_decode(zcbor_state_t *state)
724 : : {
725 : 15 : PRINT_FUNC();
726 : 15 : return list_map_start_decode(state, ZCBOR_MAJOR_TYPE_LIST);
727 : : }
728 : :
729 : :
730 : 18 : bool zcbor_map_start_decode(zcbor_state_t *state)
731 : : {
732 : 18 : PRINT_FUNC();
733 : 18 : bool ret = list_map_start_decode(state, ZCBOR_MAJOR_TYPE_MAP);
734 : :
735 [ + - - + ]: 18 : if (ret && !state->decode_state.indefinite_length_array) {
736 [ - + ]: 18 : if (state->elem_count >= (ZCBOR_MAX_ELEM_COUNT / 2)) {
737 : : /* The new elem_count is too large. */
738 : 0 : ERR_RESTORE(ZCBOR_ERR_INT_SIZE);
739 : : }
740 : 18 : state->elem_count *= 2;
741 : : }
742 : : return ret;
743 : : }
744 : :
745 : :
746 : 0 : bool zcbor_array_at_end(zcbor_state_t *state)
747 : : {
748 : 0 : const bool indefinite_length_array = state->decode_state.indefinite_length_array;
749 : :
750 [ # # ]: 0 : return ((!indefinite_length_array && (state->elem_count == 0))
751 [ # # ]: 0 : || (indefinite_length_array
752 [ # # ]: 0 : && (state->payload < state->payload_end)
753 [ # # ]: 0 : && (*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 : :
763 : 0 : bool zcbor_unordered_map_start_decode(zcbor_state_t *state)
764 : : {
765 : 0 : PRINT_FUNC();
766 [ # # ]: 0 : 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 : 0 : state->decode_state.map_elems_processed = 0;
773 : : #endif
774 : 0 : state->decode_state.map_elem_count = 0;
775 : 0 : state->decode_state.counting_map_elems = state->decode_state.indefinite_length_array;
776 : :
777 [ # # ]: 0 : if (!state->decode_state.counting_map_elems) {
778 : 0 : 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 : : */
793 : 0 : static size_t zcbor_current_max_elem_count(zcbor_state_t *state)
794 : : {
795 : 0 : return (state->decode_state.indefinite_length_array ? \
796 [ # # ]: 0 : ZCBOR_LARGE_ELEM_COUNT : state->decode_state.map_elem_count * 2);
797 : : }
798 : :
799 : :
800 : 0 : static bool map_restart(zcbor_state_t *state)
801 : : {
802 [ # # ]: 0 : if (!zcbor_process_backup(state, ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_KEEP_DECODE_STATE,
803 : : ZCBOR_MAX_ELEM_COUNT)) {
804 : 0 : ZCBOR_FAIL();
805 : : }
806 : :
807 : 0 : state->elem_count = zcbor_current_max_elem_count(state);
808 : 0 : return true;
809 : : }
810 : :
811 : :
812 : : __attribute__((used))
813 : 0 : 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 : 0 : 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 : :
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 : :
850 : : static bool should_try_key(zcbor_state_t *state)
851 : : {
852 : : return manipulate_flags(state, FLAG_MODE_GET_CURRENT);
853 : : }
854 : :
855 : :
856 : : bool zcbor_elem_processed(zcbor_state_t *state)
857 : : {
858 : : return manipulate_flags(state, FLAG_MODE_CLEAR_CURRENT);
859 : : }
860 : :
861 : :
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 : :
884 : 0 : static bool should_try_key(zcbor_state_t *state)
885 : : {
886 : 0 : return (state->decode_state.map_elems_processed < state->decode_state.map_elem_count);
887 : : }
888 : :
889 : :
890 : 0 : bool zcbor_elem_processed(zcbor_state_t *state)
891 : : {
892 [ # # ]: 0 : if (should_try_key(state)) {
893 : 0 : state->decode_state.map_elems_processed++;
894 : : }
895 : 0 : return true;
896 : : }
897 : : #endif
898 : :
899 : :
900 : 0 : static size_t update_map_elem_count(zcbor_state_t *state, size_t elem_count)
901 : : {
902 : 0 : size_t old_map_elem_count = state->decode_state.map_elem_count;
903 : :
904 : 0 : state->decode_state.map_elem_count = MAX(old_map_elem_count, elem_count / 2);
905 : 0 : return old_map_elem_count;
906 : : }
907 : :
908 : :
909 : 0 : static bool handle_map_end(zcbor_state_t *state)
910 : : {
911 : 0 : state->decode_state.counting_map_elems = false;
912 : 0 : return map_restart(state);
913 : : }
914 : :
915 : :
916 : 0 : static bool try_key(zcbor_state_t *state, void *key_result, zcbor_decoder_t key_decoder)
917 : : {
918 : 0 : uint8_t const *payload_bak2 = state->payload;
919 : 0 : size_t elem_count_bak = state->elem_count;
920 : :
921 [ # # ]: 0 : if (!key_decoder(state, (uint8_t *)key_result)) {
922 : 0 : state->payload = payload_bak2;
923 : 0 : state->elem_count = elem_count_bak;
924 : 0 : return false;
925 : : }
926 : :
927 : : zcbor_log("Found element at index %zu.\n", get_current_index(state, 1));
928 : : return true;
929 : : }
930 : :
931 : :
932 : 0 : bool zcbor_unordered_map_search(zcbor_decoder_t key_decoder, zcbor_state_t *state, void *key_result)
933 : : {
934 : 0 : 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 [ # # ]: 0 : ZCBOR_ERR_IF(state->elem_count & 1, ZCBOR_ERR_MAP_MISALIGNED);
939 : :
940 : 0 : uint8_t const *payload_bak = state->payload;
941 : 0 : size_t elem_count = state->elem_count;
942 : :
943 : : /* Loop once through all the elements of the map. */
944 : 0 : do {
945 [ # # ]: 0 : if (zcbor_array_at_end(state)) {
946 [ # # ]: 0 : if (!handle_map_end(state)) {
947 : 0 : goto error;
948 : : }
949 : 0 : 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 [ # # ]: 0 : if (state->decode_state.counting_map_elems) {
954 : 0 : size_t m_elem_count = ZCBOR_LARGE_ELEM_COUNT - state->elem_count + 2;
955 : 0 : 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 : 0 : (void)old_flags;
960 : : }
961 : :
962 [ # # # # ]: 0 : if (should_try_key(state) && try_key(state, key_result, key_decoder)) {
963 [ # # # # ]: 0 : if (!ZCBOR_MANUALLY_PROCESS_ELEM(state)) {
964 [ # # ]: 0 : ZCBOR_FAIL_IF(!zcbor_elem_processed(state));
965 : : }
966 : 0 : return true;
967 : : }
968 : :
969 : : /* Skip over both the key and the value. */
970 [ # # # # ]: 0 : if (!zcbor_any_skip(state, NULL) || !zcbor_any_skip(state, NULL)) {
971 : 0 : goto error;
972 : : }
973 [ # # ]: 0 : } while (state->elem_count != elem_count);
974 : :
975 : 0 : zcbor_error(state, ZCBOR_ERR_ELEM_NOT_FOUND);
976 : 0 : error:
977 : 0 : state->payload = payload_bak;
978 : 0 : state->elem_count = elem_count;
979 : 0 : ZCBOR_FAIL();
980 : : }
981 : :
982 : :
983 : 0 : bool zcbor_search_key_bstr_ptr(zcbor_state_t *state, char const *ptr, size_t len)
984 : : {
985 : 0 : struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
986 : :
987 : 0 : return zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_bstr_expect, state, &zs);
988 : : }
989 : :
990 : :
991 : 0 : bool zcbor_search_key_tstr_ptr(zcbor_state_t *state, char const *ptr, size_t len)
992 : : {
993 : 0 : struct zcbor_string zs = { .value = (const uint8_t *)ptr, .len = len };
994 : :
995 : 0 : return zcbor_unordered_map_search((zcbor_decoder_t *)zcbor_tstr_expect, state, &zs);
996 : : }
997 : :
998 : :
999 : 0 : bool zcbor_search_key_bstr_term(zcbor_state_t *state, char const *str, size_t maxlen)
1000 : : {
1001 : 0 : return zcbor_search_key_bstr_ptr(state, str, strnlen(str, maxlen));
1002 : : }
1003 : :
1004 : :
1005 : 0 : bool zcbor_search_key_tstr_term(zcbor_state_t *state, char const *str, size_t maxlen)
1006 : : {
1007 : 0 : return zcbor_search_key_tstr_ptr(state, str, strnlen(str, maxlen));
1008 : : }
1009 : :
1010 : :
1011 : 0 : static bool array_end_expect(zcbor_state_t *state)
1012 : : {
1013 [ # # ]: 0 : INITIAL_CHECKS();
1014 [ # # ]: 0 : ZCBOR_ERR_IF(*state->payload != 0xFF, ZCBOR_ERR_WRONG_TYPE);
1015 : :
1016 : 0 : state->payload++;
1017 : 0 : return true;
1018 : : }
1019 : :
1020 : :
1021 : 30 : static bool list_map_end_decode(zcbor_state_t *state)
1022 : : {
1023 : 30 : size_t max_elem_count = 0;
1024 : :
1025 [ - + ]: 30 : if (state->decode_state.indefinite_length_array) {
1026 [ # # ]: 0 : if (!array_end_expect(state)) {
1027 : 0 : ZCBOR_FAIL();
1028 : : }
1029 : 0 : max_elem_count = ZCBOR_MAX_ELEM_COUNT;
1030 : 0 : state->decode_state.indefinite_length_array = false;
1031 : : }
1032 [ - + ]: 30 : 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 : :
1042 : 12 : bool zcbor_list_end_decode(zcbor_state_t *state)
1043 : : {
1044 : 12 : PRINT_FUNC();
1045 : 12 : return list_map_end_decode(state);
1046 : : }
1047 : :
1048 : :
1049 : 18 : bool zcbor_map_end_decode(zcbor_state_t *state)
1050 : : {
1051 : 18 : PRINT_FUNC();
1052 : 18 : return list_map_end_decode(state);
1053 : : }
1054 : :
1055 : :
1056 : 0 : 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 [ # # # # ]: 0 : ZCBOR_ERR_IF(!zcbor_array_at_end(state) && state->decode_state.counting_map_elems,
1061 : : ZCBOR_ERR_ELEMS_NOT_PROCESSED);
1062 : :
1063 [ # # ]: 0 : 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 [ # # ]: 0 : ZCBOR_ERR_IF(should_try_key(state), ZCBOR_ERR_ELEMS_NOT_PROCESSED);
1076 : : #endif
1077 : : }
1078 [ # # ]: 0 : while (!zcbor_array_at_end(state)) {
1079 : 0 : zcbor_any_skip(state, NULL);
1080 : : }
1081 : 0 : return zcbor_map_end_decode(state);
1082 : : }
1083 : :
1084 : :
1085 : 0 : bool zcbor_list_map_end_force_decode(zcbor_state_t *state)
1086 : : {
1087 : 0 : 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 : :
1097 : 0 : bool zcbor_simple_decode(zcbor_state_t *state, uint8_t *result)
1098 : : {
1099 : 0 : PRINT_FUNC();
1100 : 0 : PRINT_FUNC();
1101 [ # # ]: 0 : INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_SIMPLE);
1102 : :
1103 : 0 : 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 [ # # ]: 0 : ZCBOR_ERR_IF(additional > ZCBOR_VALUE_IS_1_BYTE, ZCBOR_ERR_WRONG_TYPE);
1108 : :
1109 [ # # ]: 0 : if (!value_extract(state, result, sizeof(*result), NULL)) {
1110 : 0 : 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 [ # # # # ]: 0 : if ((additional == ZCBOR_VALUE_IS_1_BYTE) && (*result < 32)) {
1116 : 0 : ERR_RESTORE(ZCBOR_ERR_INVALID_VALUE_ENCODING);
1117 : : }
1118 : : return true;
1119 : : }
1120 : :
1121 : :
1122 : 0 : bool zcbor_simple_expect(zcbor_state_t *state, uint8_t expected)
1123 : : {
1124 : 0 : PRINT_FUNC();
1125 : 0 : uint8_t actual;
1126 : :
1127 [ # # ]: 0 : if (!zcbor_simple_decode(state, &actual)) {
1128 : 0 : ZCBOR_FAIL();
1129 : : }
1130 : :
1131 [ # # ]: 0 : if (actual != expected) {
1132 : 0 : zcbor_log("simple value %u != %u\r\n", actual, expected);
1133 : 0 : ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1134 : : }
1135 : :
1136 : : return true;
1137 : : }
1138 : :
1139 : :
1140 : 0 : bool zcbor_simple_pexpect(zcbor_state_t *state, uint8_t *expected)
1141 : : {
1142 : 0 : PRINT_FUNC();
1143 : 0 : return zcbor_simple_expect(state, *expected);
1144 : : }
1145 : :
1146 : :
1147 : 0 : bool zcbor_nil_expect(zcbor_state_t *state, void *unused)
1148 : : {
1149 : 0 : PRINT_FUNC();
1150 : 0 : (void)unused;
1151 : 0 : return zcbor_simple_expect(state, ZCBOR_NIL_VAL);
1152 : : }
1153 : :
1154 : :
1155 : 0 : bool zcbor_undefined_expect(zcbor_state_t *state, void *unused)
1156 : : {
1157 : 0 : PRINT_FUNC();
1158 : 0 : (void)unused;
1159 : 0 : return zcbor_simple_expect(state, ZCBOR_UNDEF_VAL);
1160 : : }
1161 : :
1162 : :
1163 : 0 : bool zcbor_bool_decode(zcbor_state_t *state, bool *result)
1164 : : {
1165 : 0 : PRINT_FUNC();
1166 : 0 : uint8_t value;
1167 : :
1168 [ # # ]: 0 : if (!zcbor_simple_decode(state, &value)) {
1169 : 0 : ZCBOR_FAIL();
1170 : : }
1171 : 0 : value -= ZCBOR_BOOL_TO_SIMPLE;
1172 [ # # ]: 0 : if (value > 1) {
1173 : 0 : ERR_RESTORE(ZCBOR_ERR_WRONG_TYPE);
1174 : : }
1175 : 0 : *result = value;
1176 : :
1177 : 0 : zcbor_log("boolval: %u\r\n", *result);
1178 : 0 : return true;
1179 : : }
1180 : :
1181 : :
1182 : 0 : bool zcbor_bool_expect(zcbor_state_t *state, bool expected)
1183 : : {
1184 : 0 : PRINT_FUNC();
1185 [ # # ]: 0 : return zcbor_simple_expect(state, (uint8_t)(!!expected) + ZCBOR_BOOL_TO_SIMPLE);
1186 : : }
1187 : :
1188 : :
1189 : 0 : bool zcbor_bool_pexpect(zcbor_state_t *state, bool *expected)
1190 : : {
1191 : 0 : PRINT_FUNC();
1192 : 0 : return zcbor_bool_expect(state, *expected);
1193 : : }
1194 : :
1195 : :
1196 : 0 : static bool float_check(zcbor_state_t *state, uint8_t additional_val)
1197 : : {
1198 [ # # ]: 0 : INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_SIMPLE);
1199 [ # # ]: 0 : ZCBOR_ERR_IF(ZCBOR_ADDITIONAL(*state->payload) != additional_val, ZCBOR_ERR_FLOAT_SIZE);
1200 : : return true;
1201 : : }
1202 : :
1203 : :
1204 : 0 : bool zcbor_float16_bytes_decode(zcbor_state_t *state, uint16_t *result)
1205 : : {
1206 : 0 : PRINT_FUNC();
1207 [ # # ]: 0 : ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_2_BYTES));
1208 : :
1209 [ # # ]: 0 : if (!value_extract(state, result, sizeof(*result), NULL)) {
1210 : : ZCBOR_FAIL();
1211 : : }
1212 : :
1213 : : return true;
1214 : : }
1215 : :
1216 : :
1217 : 0 : bool zcbor_float16_bytes_expect(zcbor_state_t *state, uint16_t expected)
1218 : : {
1219 : 0 : PRINT_FUNC();
1220 : 0 : uint16_t actual;
1221 : :
1222 [ # # ]: 0 : if (!zcbor_float16_bytes_decode(state, &actual)) {
1223 : 0 : ZCBOR_FAIL();
1224 : : }
1225 [ # # ]: 0 : if (actual != expected) {
1226 : 0 : ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1227 : : }
1228 : : return true;
1229 : : }
1230 : :
1231 : :
1232 : 0 : bool zcbor_float16_bytes_pexpect(zcbor_state_t *state, uint16_t *expected)
1233 : : {
1234 : 0 : PRINT_FUNC();
1235 : 0 : return zcbor_float16_bytes_expect(state, *expected);
1236 : : }
1237 : :
1238 : :
1239 : 0 : bool zcbor_float16_decode(zcbor_state_t *state, float *result)
1240 : : {
1241 : 0 : PRINT_FUNC();
1242 : 0 : uint16_t value16;
1243 : :
1244 [ # # ]: 0 : if (!zcbor_float16_bytes_decode(state, &value16)) {
1245 : 0 : ZCBOR_FAIL();
1246 : : }
1247 : :
1248 : 0 : *result = zcbor_float16_to_32(value16);
1249 : 0 : return true;
1250 : : }
1251 : :
1252 : :
1253 : 0 : bool zcbor_float16_expect(zcbor_state_t *state, float expected)
1254 : : {
1255 : 0 : PRINT_FUNC();
1256 : 0 : float actual;
1257 : :
1258 [ # # ]: 0 : if (!zcbor_float16_decode(state, &actual)) {
1259 : 0 : ZCBOR_FAIL();
1260 : : }
1261 [ # # ]: 0 : if (actual != expected) {
1262 : 0 : ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1263 : : }
1264 : : return true;
1265 : : }
1266 : :
1267 : :
1268 : 0 : bool zcbor_float16_pexpect(zcbor_state_t *state, float *expected)
1269 : : {
1270 : 0 : PRINT_FUNC();
1271 : 0 : return zcbor_float16_expect(state, *expected);
1272 : : }
1273 : :
1274 : :
1275 : 0 : bool zcbor_float32_decode(zcbor_state_t *state, float *result)
1276 : : {
1277 : 0 : PRINT_FUNC();
1278 [ # # ]: 0 : ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_4_BYTES));
1279 : :
1280 [ # # ]: 0 : if (!value_extract(state, result, sizeof(*result), NULL)) {
1281 : : ZCBOR_FAIL();
1282 : : }
1283 : :
1284 : : return true;
1285 : : }
1286 : :
1287 : :
1288 : 0 : bool zcbor_float32_expect(zcbor_state_t *state, float expected)
1289 : : {
1290 : 0 : PRINT_FUNC();
1291 : 0 : float actual;
1292 : :
1293 [ # # ]: 0 : if (!zcbor_float32_decode(state, &actual)) {
1294 : 0 : ZCBOR_FAIL();
1295 : : }
1296 [ # # ]: 0 : if (actual != expected) {
1297 : 0 : ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1298 : : }
1299 : : return true;
1300 : : }
1301 : :
1302 : :
1303 : 0 : bool zcbor_float32_pexpect(zcbor_state_t *state, float *expected)
1304 : : {
1305 : 0 : PRINT_FUNC();
1306 : 0 : return zcbor_float32_expect(state, *expected);
1307 : : }
1308 : :
1309 : :
1310 : 0 : bool zcbor_float16_32_decode(zcbor_state_t *state, float *result)
1311 : : {
1312 : 0 : PRINT_FUNC();
1313 [ # # ]: 0 : if (zcbor_float16_decode(state, result)) {
1314 : : /* Do nothing */
1315 [ # # ]: 0 : } else if (!zcbor_float32_decode(state, result)) {
1316 : : ZCBOR_FAIL();
1317 : : }
1318 : :
1319 : : return true;
1320 : : }
1321 : :
1322 : :
1323 : 0 : bool zcbor_float16_32_expect(zcbor_state_t *state, float expected)
1324 : : {
1325 : 0 : PRINT_FUNC();
1326 [ # # ]: 0 : if (zcbor_float16_expect(state, expected)) {
1327 : : /* Do nothing */
1328 [ # # ]: 0 : } else if (!zcbor_float32_expect(state, expected)) {
1329 : : ZCBOR_FAIL();
1330 : : }
1331 : :
1332 : : return true;
1333 : : }
1334 : :
1335 : :
1336 : 0 : bool zcbor_float16_32_pexpect(zcbor_state_t *state, float *expected)
1337 : : {
1338 : 0 : PRINT_FUNC();
1339 : 0 : return zcbor_float16_32_expect(state, *expected);
1340 : : }
1341 : :
1342 : :
1343 : 0 : bool zcbor_float64_decode(zcbor_state_t *state, double *result)
1344 : : {
1345 : 0 : PRINT_FUNC();
1346 [ # # ]: 0 : ZCBOR_FAIL_IF(!float_check(state, ZCBOR_VALUE_IS_8_BYTES));
1347 : :
1348 [ # # ]: 0 : if (!value_extract(state, result, sizeof(*result), NULL)) {
1349 : : ZCBOR_FAIL();
1350 : : }
1351 : :
1352 : : return true;
1353 : : }
1354 : :
1355 : :
1356 : 0 : bool zcbor_float64_expect(zcbor_state_t *state, double expected)
1357 : : {
1358 : 0 : PRINT_FUNC();
1359 : 0 : double actual;
1360 : :
1361 [ # # ]: 0 : if (!zcbor_float64_decode(state, &actual)) {
1362 : 0 : ZCBOR_FAIL();
1363 : : }
1364 [ # # ]: 0 : if (actual != expected) {
1365 : 0 : ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1366 : : }
1367 : : return true;
1368 : : }
1369 : :
1370 : :
1371 : 0 : bool zcbor_float64_pexpect(zcbor_state_t *state, double *expected)
1372 : : {
1373 : 0 : PRINT_FUNC();
1374 : 0 : return zcbor_float64_expect(state, *expected);
1375 : : }
1376 : :
1377 : :
1378 : 0 : bool zcbor_float32_64_decode(zcbor_state_t *state, double *result)
1379 : : {
1380 : 0 : PRINT_FUNC();
1381 : 0 : float float_result;
1382 : :
1383 [ # # ]: 0 : if (zcbor_float32_decode(state, &float_result)) {
1384 : 0 : *result = (double)float_result;
1385 [ # # ]: 0 : } else if (!zcbor_float64_decode(state, result)) {
1386 : : ZCBOR_FAIL();
1387 : : }
1388 : :
1389 : : return true;
1390 : : }
1391 : :
1392 : :
1393 : 0 : bool zcbor_float32_64_expect(zcbor_state_t *state, double expected)
1394 : : {
1395 : 0 : PRINT_FUNC();
1396 [ # # ]: 0 : if (zcbor_float64_expect(state, expected)) {
1397 : : /* Do nothing */
1398 [ # # ]: 0 : } else if (!zcbor_float32_expect(state, (float)expected)) {
1399 : : ZCBOR_FAIL();
1400 : : }
1401 : :
1402 : : return true;
1403 : : }
1404 : :
1405 : :
1406 : 0 : bool zcbor_float32_64_pexpect(zcbor_state_t *state, double *expected)
1407 : : {
1408 : 0 : PRINT_FUNC();
1409 : 0 : return zcbor_float32_64_expect(state, *expected);
1410 : : }
1411 : :
1412 : :
1413 : 0 : bool zcbor_float_decode(zcbor_state_t *state, double *result)
1414 : : {
1415 : 0 : PRINT_FUNC();
1416 : 0 : float float_result;
1417 : :
1418 [ # # ]: 0 : if (zcbor_float16_decode(state, &float_result)) {
1419 : 0 : *result = (double)float_result;
1420 [ # # ]: 0 : } else if (zcbor_float32_decode(state, &float_result)) {
1421 : 0 : *result = (double)float_result;
1422 [ # # ]: 0 : } else if (!zcbor_float64_decode(state, result)) {
1423 : : ZCBOR_FAIL();
1424 : : }
1425 : :
1426 : : return true;
1427 : : }
1428 : :
1429 : :
1430 : 0 : bool zcbor_float_expect(zcbor_state_t *state, double expected)
1431 : : {
1432 : 0 : PRINT_FUNC();
1433 [ # # ]: 0 : if (zcbor_float16_expect(state, (float)expected)) {
1434 : : /* Do nothing */
1435 [ # # ]: 0 : } else if (zcbor_float32_expect(state, (float)expected)) {
1436 : : /* Do nothing */
1437 [ # # ]: 0 : } else if (!zcbor_float64_expect(state, expected)) {
1438 : : ZCBOR_FAIL();
1439 : : }
1440 : :
1441 : : return true;
1442 : : }
1443 : :
1444 : :
1445 : 0 : bool zcbor_float_pexpect(zcbor_state_t *state, double *expected)
1446 : : {
1447 : 0 : PRINT_FUNC();
1448 : 0 : return zcbor_float_expect(state, *expected);
1449 : : }
1450 : :
1451 : :
1452 : 0 : bool zcbor_any_skip(zcbor_state_t *state, void *result)
1453 : : {
1454 : 0 : PRINT_FUNC();
1455 : : zcbor_assert_state(result == NULL,
1456 : 0 : "'any' type cannot be returned, only skipped.\r\n");
1457 : 0 : (void)result;
1458 : :
1459 [ # # ]: 0 : INITIAL_CHECKS();
1460 : 0 : zcbor_major_type_t major_type = ZCBOR_MAJOR_TYPE(*state->payload);
1461 : 0 : uint64_t value = 0; /* In case of indefinite_length_array. */
1462 : 0 : zcbor_state_t state_copy;
1463 : :
1464 : 0 : memcpy(&state_copy, state, sizeof(zcbor_state_t));
1465 : :
1466 [ # # ]: 0 : while (major_type == ZCBOR_MAJOR_TYPE_TAG) {
1467 : 0 : uint32_t tag_dummy;
1468 : :
1469 [ # # ]: 0 : if (!zcbor_tag_decode(&state_copy, &tag_dummy)) {
1470 : 0 : ZCBOR_FAIL();
1471 : : }
1472 [ # # ]: 0 : ZCBOR_ERR_IF(state_copy.payload >= state_copy.payload_end, ZCBOR_ERR_NO_PAYLOAD);
1473 : 0 : major_type = ZCBOR_MAJOR_TYPE(*state_copy.payload);
1474 : : }
1475 : :
1476 : 0 : bool indefinite_length_array = false;
1477 : 0 : bool *ila_ptr = ((major_type == ZCBOR_MAJOR_TYPE_MAP)
1478 [ # # ]: 0 : || (major_type == ZCBOR_MAJOR_TYPE_LIST)) ? &indefinite_length_array : NULL;
1479 : :
1480 [ # # ]: 0 : if (!value_extract(&state_copy, &value, sizeof(value), ila_ptr)) {
1481 : : /* Can happen because of elem_count (or payload_end) */
1482 : 0 : ZCBOR_FAIL();
1483 : : }
1484 : :
1485 [ # # # # ]: 0 : switch (major_type) {
1486 : 0 : case ZCBOR_MAJOR_TYPE_BSTR:
1487 : : case ZCBOR_MAJOR_TYPE_TSTR:
1488 : : /* 'value' is the length of the BSTR or TSTR. */
1489 [ # # ]: 0 : ZCBOR_FAIL_IF(!str_overflow_check(state, (size_t)value));
1490 : 0 : (state_copy.payload) += value;
1491 : 0 : break;
1492 : 0 : case ZCBOR_MAJOR_TYPE_MAP:
1493 [ # # ]: 0 : ZCBOR_ERR_IF(value > (SIZE_MAX / 2), ZCBOR_ERR_INT_SIZE);
1494 : 0 : value *= 2;
1495 : : /* fallthrough */
1496 : 0 : case ZCBOR_MAJOR_TYPE_LIST:
1497 [ # # ]: 0 : if (indefinite_length_array) {
1498 : 0 : value = ZCBOR_LARGE_ELEM_COUNT;
1499 : : }
1500 : 0 : state_copy.elem_count = (size_t)value;
1501 : 0 : state_copy.decode_state.indefinite_length_array = indefinite_length_array;
1502 : 0 : while (!zcbor_array_at_end(&state_copy)) {
1503 [ # # ]: 0 : if (!zcbor_any_skip(&state_copy, NULL)) {
1504 [ # # ]: 0 : ZCBOR_FAIL();
1505 : : }
1506 : : }
1507 [ # # # # ]: 0 : if (indefinite_length_array && !array_end_expect(&state_copy)) {
1508 : : ZCBOR_FAIL();
1509 : : }
1510 : : break;
1511 : : default:
1512 : : /* Do nothing */
1513 : : break;
1514 : : }
1515 : :
1516 : 0 : state->payload = state_copy.payload;
1517 : 0 : state->elem_count--;
1518 : :
1519 : 0 : return true;
1520 : : }
1521 : :
1522 : :
1523 : 0 : bool zcbor_tag_decode(zcbor_state_t *state, uint32_t *result)
1524 : : {
1525 : 0 : PRINT_FUNC();
1526 [ # # ]: 0 : INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_TAG);
1527 : :
1528 [ # # ]: 0 : if (!value_extract(state, result, sizeof(*result), NULL)) {
1529 : 0 : ZCBOR_FAIL();
1530 : : }
1531 : 0 : state->elem_count++;
1532 : 0 : return true;
1533 : : }
1534 : :
1535 : :
1536 : 0 : bool zcbor_tag_expect(zcbor_state_t *state, uint32_t expected)
1537 : : {
1538 : 0 : PRINT_FUNC();
1539 : 0 : uint32_t actual;
1540 : :
1541 [ # # ]: 0 : if (!zcbor_tag_decode(state, &actual)) {
1542 : 0 : ZCBOR_FAIL();
1543 : : }
1544 [ # # ]: 0 : if (actual != expected) {
1545 : 0 : ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
1546 : : }
1547 : : return true;
1548 : : }
1549 : :
1550 : :
1551 : 0 : bool zcbor_tag_pexpect(zcbor_state_t *state, uint32_t *expected)
1552 : : {
1553 : 0 : PRINT_FUNC();
1554 : 0 : return zcbor_tag_expect(state, *expected);
1555 : : }
1556 : :
1557 : :
1558 : 162 : bool zcbor_multi_decode(size_t min_decode,
1559 : : size_t max_decode,
1560 : : size_t *num_decode,
1561 : : zcbor_decoder_t decoder,
1562 : : zcbor_state_t *state,
1563 : : void *result,
1564 : : size_t result_len)
1565 : : {
1566 : 162 : PRINT_FUNC();
1567 : 162 : ZCBOR_CHECK_ERROR();
1568 [ + + ]: 180 : for (size_t i = 0; i < max_decode; i++) {
1569 : 162 : uint8_t const *payload_bak = state->payload;
1570 : 162 : size_t elem_count_bak = state->elem_count;
1571 : :
1572 [ + + ]: 162 : if (!decoder(state,
1573 : 162 : (uint8_t *)result + i*result_len)) {
1574 : 144 : *num_decode = i;
1575 : 144 : state->payload = payload_bak;
1576 : 144 : state->elem_count = elem_count_bak;
1577 [ - + ]: 144 : ZCBOR_ERR_IF(i < min_decode, ZCBOR_ERR_ITERATIONS);
1578 : : zcbor_log("Found %zu elements.\r\n", i);
1579 : : return true;
1580 : : }
1581 : : }
1582 : 18 : zcbor_log("Found %zu elements.\r\n", max_decode);
1583 : 18 : *num_decode = max_decode;
1584 : 18 : return true;
1585 : : }
1586 : :
1587 : :
1588 : 162 : bool zcbor_present_decode(bool *present,
1589 : : zcbor_decoder_t decoder,
1590 : : zcbor_state_t *state,
1591 : : void *result)
1592 : : {
1593 : 162 : PRINT_FUNC();
1594 : 162 : size_t num_decode = 0;
1595 : 162 : bool retval = zcbor_multi_decode(0, 1, &num_decode, decoder, state, result, 0);
1596 : :
1597 : 162 : zcbor_assert_state(retval, "zcbor_multi_decode should not fail with these parameters.\r\n");
1598 : :
1599 : 162 : *present = !!num_decode;
1600 : 162 : return retval;
1601 : : }
1602 : :
1603 : :
1604 : 0 : void zcbor_new_decode_state(zcbor_state_t *state_array, size_t n_states,
1605 : : const uint8_t *payload, size_t payload_len, size_t elem_count,
1606 : : uint8_t *flags, size_t flags_bytes)
1607 : : {
1608 : 0 : zcbor_new_state(state_array, n_states, payload, payload_len, elem_count, flags, flags_bytes);
1609 : 0 : }
|