zcbor_decode.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /*
  2. * Copyright (c) 2020 Nordic Semiconductor ASA
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <stdbool.h>
  8. #include <stddef.h>
  9. #include <string.h>
  10. #include "zcbor_decode.h"
  11. #include "zcbor_common.h"
  12. /** Return value length from additional value.
  13. */
  14. static uint_fast32_t additional_len(uint8_t additional)
  15. {
  16. if (ZCBOR_VALUE_IS_1_BYTE <= additional && additional <= ZCBOR_VALUE_IS_8_BYTES) {
  17. /* 24 => 1
  18. * 25 => 2
  19. * 26 => 4
  20. * 27 => 8
  21. */
  22. return 1U << (additional - ZCBOR_VALUE_IS_1_BYTE);
  23. }
  24. return 0;
  25. }
  26. /** Extract the major type, i.e. the first 3 bits of the header byte. */
  27. #define MAJOR_TYPE(header_byte) ((zcbor_major_type_t)(((header_byte) >> 5) & 0x7))
  28. /** Extract the additional info, i.e. the last 5 bits of the header byte. */
  29. #define ADDITIONAL(header_byte) ((header_byte) & 0x1F)
  30. #define FAIL_AND_DECR_IF(expr, err) \
  31. do {\
  32. if (expr) { \
  33. (state->payload)--; \
  34. ZCBOR_ERR(err); \
  35. } \
  36. } while(0)
  37. static bool initial_checks(zcbor_state_t *state)
  38. {
  39. ZCBOR_CHECK_ERROR();
  40. ZCBOR_CHECK_PAYLOAD();
  41. return true;
  42. }
  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 = MAJOR_TYPE(*state->payload);
  49. if (major_type != exp_major_type) {
  50. ZCBOR_ERR(ZCBOR_ERR_WRONG_TYPE);
  51. }
  52. return true;
  53. }
  54. #define INITIAL_CHECKS() \
  55. do {\
  56. if (!initial_checks(state)) { \
  57. ZCBOR_FAIL(); \
  58. } \
  59. } while(0)
  60. #define INITIAL_CHECKS_WITH_TYPE(exp_major_type) \
  61. do {\
  62. if (!type_check(state, exp_major_type)) { \
  63. ZCBOR_FAIL(); \
  64. } \
  65. } while(0)
  66. #define ERR_RESTORE(err) \
  67. do { \
  68. state->payload = state->payload_bak; \
  69. state->elem_count++; \
  70. ZCBOR_ERR(err); \
  71. } while(0)
  72. #define FAIL_RESTORE() \
  73. do { \
  74. state->payload = state->payload_bak; \
  75. state->elem_count++; \
  76. ZCBOR_FAIL(); \
  77. } while(0)
  78. /** Get a single value.
  79. *
  80. * @details @p ppayload must point to the header byte. This function will
  81. * retrieve the value (either from within the additional info, or from
  82. * the subsequent bytes) and return it in the result. The result can
  83. * have arbitrary length.
  84. *
  85. * The function will also validate
  86. * - Min/max constraints on the value.
  87. * - That @p payload doesn't overrun past @p payload_end.
  88. * - That @p elem_count has not been exhausted.
  89. *
  90. * @p ppayload and @p elem_count are updated if the function
  91. * succeeds. If not, they are left unchanged.
  92. *
  93. * CBOR values are always big-endian, so this function converts from
  94. * big to little-endian if necessary (@ref CONFIG_BIG_ENDIAN).
  95. */
  96. static bool value_extract(zcbor_state_t *state,
  97. void *const result, uint_fast32_t result_len)
  98. {
  99. zcbor_trace();
  100. zcbor_assert_state(result_len != 0, "0-length result not supported.\r\n");
  101. zcbor_assert_state(result != NULL, NULL);
  102. INITIAL_CHECKS();
  103. ZCBOR_ERR_IF((state->elem_count == 0), ZCBOR_ERR_LOW_ELEM_COUNT);
  104. uint8_t *u8_result = (uint8_t *)result;
  105. uint8_t additional = ADDITIONAL(*state->payload);
  106. state->payload_bak = state->payload;
  107. (state->payload)++;
  108. memset(result, 0, result_len);
  109. if (additional <= ZCBOR_VALUE_IN_HEADER) {
  110. #ifdef CONFIG_BIG_ENDIAN
  111. u8_result[result_len - 1] = additional;
  112. #else
  113. u8_result[0] = additional;
  114. #endif /* CONFIG_BIG_ENDIAN */
  115. } else {
  116. uint_fast32_t len = additional_len(additional);
  117. FAIL_AND_DECR_IF(len > result_len, ZCBOR_ERR_INT_SIZE);
  118. FAIL_AND_DECR_IF(len == 0, ZCBOR_ERR_ADDITIONAL_INVAL); // additional_len() did not recognize the additional value.
  119. FAIL_AND_DECR_IF((state->payload + len) > state->payload_end,
  120. ZCBOR_ERR_NO_PAYLOAD);
  121. #ifdef CONFIG_BIG_ENDIAN
  122. memcpy(&u8_result[result_len - len], state->payload, len);
  123. #else
  124. for (uint_fast32_t i = 0; i < len; i++) {
  125. u8_result[i] = (state->payload)[len - i - 1];
  126. }
  127. #endif /* CONFIG_BIG_ENDIAN */
  128. (state->payload) += len;
  129. }
  130. (state->elem_count)--;
  131. return true;
  132. }
  133. bool zcbor_int_decode(zcbor_state_t *state, void *result_int, size_t int_size)
  134. {
  135. INITIAL_CHECKS();
  136. zcbor_major_type_t major_type = MAJOR_TYPE(*state->payload);
  137. uint8_t *result_uint8 = (uint8_t *)result_int;
  138. int8_t *result_int8 = (int8_t *)result_int;
  139. if (major_type != ZCBOR_MAJOR_TYPE_PINT
  140. && major_type != ZCBOR_MAJOR_TYPE_NINT) {
  141. /* Value to be read doesn't have the right type. */
  142. ZCBOR_ERR(ZCBOR_ERR_WRONG_TYPE);
  143. }
  144. if (!value_extract(state, result_int, int_size)) {
  145. ZCBOR_FAIL();
  146. }
  147. #ifdef CONFIG_BIG_ENDIAN
  148. if (result_int8[0] < 0) {
  149. #else
  150. if (result_int8[int_size - 1] < 0) {
  151. #endif
  152. /* Value is too large to fit in a signed integer. */
  153. ERR_RESTORE(ZCBOR_ERR_INT_SIZE);
  154. }
  155. if (major_type == ZCBOR_MAJOR_TYPE_NINT) {
  156. /* Convert from CBOR's representation by flipping all bits. */
  157. for (int i = 0; i < int_size; i++) {
  158. result_uint8[i] = (uint8_t)~result_uint8[i];
  159. }
  160. }
  161. return true;
  162. }
  163. bool zcbor_int32_decode(zcbor_state_t *state, int32_t *result)
  164. {
  165. return zcbor_int_decode(state, result, sizeof(*result));
  166. }
  167. bool zcbor_int64_decode(zcbor_state_t *state, int64_t *result)
  168. {
  169. return zcbor_int_decode(state, result, sizeof(*result));
  170. }
  171. bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result)
  172. {
  173. INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PINT);
  174. if (!value_extract(state, result, sizeof(*result))) {
  175. ZCBOR_FAIL();
  176. }
  177. return true;
  178. }
  179. bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t result)
  180. {
  181. if (!zcbor_union_elem_code(state)) {
  182. ZCBOR_FAIL();
  183. }
  184. return zcbor_int32_expect(state, result);
  185. }
  186. bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t result)
  187. {
  188. if (!zcbor_union_elem_code(state)) {
  189. ZCBOR_FAIL();
  190. }
  191. return zcbor_int64_expect(state, result);
  192. }
  193. bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t result)
  194. {
  195. if (!zcbor_union_elem_code(state)) {
  196. ZCBOR_FAIL();
  197. }
  198. return zcbor_uint32_expect(state, result);
  199. }
  200. bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t result)
  201. {
  202. if (!zcbor_union_elem_code(state)) {
  203. ZCBOR_FAIL();
  204. }
  205. return zcbor_uint64_expect(state, result);
  206. }
  207. bool zcbor_int32_expect(zcbor_state_t *state, int32_t result)
  208. {
  209. return zcbor_int64_expect(state, result);
  210. }
  211. bool zcbor_int64_expect(zcbor_state_t *state, int64_t result)
  212. {
  213. int64_t value;
  214. if (!zcbor_int64_decode(state, &value)) {
  215. ZCBOR_FAIL();
  216. }
  217. if (value != result) {
  218. zcbor_print("%" PRIi64 " != %" PRIi64 "\r\n", value, result);
  219. ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
  220. }
  221. return true;
  222. }
  223. bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result)
  224. {
  225. INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PINT);
  226. if (!value_extract(state, result, sizeof(*result))) {
  227. ZCBOR_FAIL();
  228. }
  229. return true;
  230. }
  231. #ifdef ZCBOR_SUPPORTS_SIZE_T
  232. bool zcbor_size_decode(zcbor_state_t *state, size_t *result)
  233. {
  234. return value_extract(state, result, sizeof(size_t));
  235. }
  236. #endif
  237. bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t result)
  238. {
  239. return zcbor_uint64_expect(state, result);
  240. }
  241. bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t result)
  242. {
  243. uint64_t value;
  244. if (!zcbor_uint64_decode(state, &value)) {
  245. ZCBOR_FAIL();
  246. }
  247. if (value != result) {
  248. zcbor_print("%" PRIu64 " != %" PRIu64 "\r\n", value, result);
  249. ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
  250. }
  251. return true;
  252. }
  253. #ifdef ZCBOR_SUPPORTS_SIZE_T
  254. bool zcbor_size_expect(zcbor_state_t *state, size_t result)
  255. {
  256. return zcbor_uint64_expect(state, result);
  257. }
  258. #endif
  259. static bool str_start_decode(zcbor_state_t *state,
  260. struct zcbor_string *result, zcbor_major_type_t exp_major_type)
  261. {
  262. INITIAL_CHECKS_WITH_TYPE(exp_major_type);
  263. if (!value_extract(state, &result->len, sizeof(result->len))) {
  264. ZCBOR_FAIL();
  265. }
  266. result->value = state->payload;
  267. return true;
  268. }
  269. static bool str_overflow_check(zcbor_state_t *state, struct zcbor_string *result)
  270. {
  271. if (result->len > (state->payload_end - state->payload)) {
  272. zcbor_print("error: 0x%zu > 0x%zu\r\n",
  273. result->len,
  274. (state->payload_end - state->payload));
  275. ERR_RESTORE(ZCBOR_ERR_NO_PAYLOAD);
  276. }
  277. return true;
  278. }
  279. bool zcbor_bstr_start_decode(zcbor_state_t *state, struct zcbor_string *result)
  280. {
  281. struct zcbor_string dummy;
  282. if (result == NULL) {
  283. result = &dummy;
  284. }
  285. if(!str_start_decode(state, result, ZCBOR_MAJOR_TYPE_BSTR)) {
  286. ZCBOR_FAIL();
  287. }
  288. if (!str_overflow_check(state, result)) {
  289. ZCBOR_FAIL();
  290. }
  291. if (!zcbor_new_backup(state, ZCBOR_MAX_ELEM_COUNT)) {
  292. FAIL_RESTORE();
  293. }
  294. state->payload_end = result->value + result->len;
  295. return true;
  296. }
  297. bool zcbor_bstr_end_decode(zcbor_state_t *state)
  298. {
  299. ZCBOR_ERR_IF(state->payload != state->payload_end, ZCBOR_ERR_PAYLOAD_NOT_CONSUMED);
  300. if (!zcbor_process_backup(state,
  301. ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_CONSUME | ZCBOR_FLAG_TRANSFER_PAYLOAD,
  302. ZCBOR_MAX_ELEM_COUNT)) {
  303. ZCBOR_FAIL();
  304. }
  305. return true;
  306. }
  307. static void partition_fragment(const zcbor_state_t *state,
  308. struct zcbor_string_fragment *result)
  309. {
  310. result->fragment.len = MIN(result->fragment.len,
  311. (size_t)state->payload_end - (size_t)state->payload);
  312. }
  313. static bool start_decode_fragment(zcbor_state_t *state,
  314. struct zcbor_string_fragment *result,
  315. zcbor_major_type_t exp_major_type)
  316. {
  317. if(!str_start_decode(state, &result->fragment, exp_major_type)) {
  318. ZCBOR_FAIL();
  319. }
  320. result->offset = 0;
  321. result->total_len = result->fragment.len;
  322. partition_fragment(state, result);
  323. state->payload_end = state->payload + result->fragment.len;
  324. return true;
  325. }
  326. bool zcbor_bstr_start_decode_fragment(zcbor_state_t *state,
  327. struct zcbor_string_fragment *result)
  328. {
  329. if (!start_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR)) {
  330. ZCBOR_FAIL();
  331. }
  332. if (!zcbor_new_backup(state, ZCBOR_MAX_ELEM_COUNT)) {
  333. FAIL_RESTORE();
  334. }
  335. return true;
  336. }
  337. void zcbor_next_fragment(zcbor_state_t *state,
  338. struct zcbor_string_fragment *prev_fragment,
  339. struct zcbor_string_fragment *result)
  340. {
  341. memcpy(result, prev_fragment, sizeof(*result));
  342. result->fragment.value = state->payload_mut;
  343. result->offset += prev_fragment->fragment.len;
  344. result->fragment.len = result->total_len - result->offset;
  345. partition_fragment(state, result);
  346. zcbor_print("New fragment length %zu\r\n", result->fragment.len);
  347. state->payload += result->fragment.len;
  348. }
  349. void zcbor_bstr_next_fragment(zcbor_state_t *state,
  350. struct zcbor_string_fragment *prev_fragment,
  351. struct zcbor_string_fragment *result)
  352. {
  353. memcpy(result, prev_fragment, sizeof(*result));
  354. result->fragment.value = state->payload_mut;
  355. result->offset += prev_fragment->fragment.len;
  356. result->fragment.len = result->total_len - result->offset;
  357. partition_fragment(state, result);
  358. zcbor_print("fragment length %zu\r\n", result->fragment.len);
  359. state->payload_end = state->payload + result->fragment.len;
  360. }
  361. bool zcbor_is_last_fragment(const struct zcbor_string_fragment *fragment)
  362. {
  363. return (fragment->total_len == (fragment->offset + fragment->fragment.len));
  364. }
  365. static bool str_decode(zcbor_state_t *state, struct zcbor_string *result,
  366. zcbor_major_type_t exp_major_type)
  367. {
  368. if (!str_start_decode(state, result, exp_major_type)) {
  369. ZCBOR_FAIL();
  370. }
  371. if (!str_overflow_check(state, result)) {
  372. ZCBOR_FAIL();
  373. }
  374. state->payload += result->len;
  375. return true;
  376. }
  377. static bool str_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result,
  378. zcbor_major_type_t exp_major_type)
  379. {
  380. if (!start_decode_fragment(state, result, exp_major_type)) {
  381. ZCBOR_FAIL();
  382. }
  383. (state->payload) += result->fragment.len;
  384. return true;
  385. }
  386. static bool str_expect(zcbor_state_t *state, struct zcbor_string *result,
  387. zcbor_major_type_t exp_major_type)
  388. {
  389. struct zcbor_string tmp_result;
  390. if (!str_decode(state, &tmp_result, exp_major_type)) {
  391. ZCBOR_FAIL();
  392. }
  393. if ((tmp_result.len != result->len)
  394. || memcmp(result->value, tmp_result.value, tmp_result.len)) {
  395. ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
  396. }
  397. return true;
  398. }
  399. bool zcbor_bstr_decode(zcbor_state_t *state, struct zcbor_string *result)
  400. {
  401. return str_decode(state, result, ZCBOR_MAJOR_TYPE_BSTR);
  402. }
  403. bool zcbor_bstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result)
  404. {
  405. return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_BSTR);
  406. }
  407. bool zcbor_bstr_expect(zcbor_state_t *state, struct zcbor_string *result)
  408. {
  409. return str_expect(state, result, ZCBOR_MAJOR_TYPE_BSTR);
  410. }
  411. bool zcbor_tstr_decode(zcbor_state_t *state, struct zcbor_string *result)
  412. {
  413. return str_decode(state, result, ZCBOR_MAJOR_TYPE_TSTR);
  414. }
  415. bool zcbor_tstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result)
  416. {
  417. return str_decode_fragment(state, result, ZCBOR_MAJOR_TYPE_TSTR);
  418. }
  419. bool zcbor_tstr_expect(zcbor_state_t *state, struct zcbor_string *result)
  420. {
  421. return str_expect(state, result, ZCBOR_MAJOR_TYPE_TSTR);
  422. }
  423. static bool list_map_start_decode(zcbor_state_t *state,
  424. zcbor_major_type_t exp_major_type)
  425. {
  426. uint_fast32_t new_elem_count;
  427. bool indefinite_length_array = false;
  428. INITIAL_CHECKS_WITH_TYPE(exp_major_type);
  429. if (ADDITIONAL(*state->payload) == ZCBOR_VALUE_IS_INDEFINITE_LENGTH) {
  430. /* Indefinite length array. */
  431. new_elem_count = ZCBOR_LARGE_ELEM_COUNT;
  432. ZCBOR_ERR_IF(state->elem_count == 0, ZCBOR_ERR_LOW_ELEM_COUNT);
  433. indefinite_length_array = true;
  434. state->payload++;
  435. state->elem_count--;
  436. } else {
  437. if (!value_extract(state, &new_elem_count, sizeof(new_elem_count))) {
  438. ZCBOR_FAIL();
  439. }
  440. }
  441. if (!zcbor_new_backup(state, new_elem_count)) {
  442. FAIL_RESTORE();
  443. }
  444. state->indefinite_length_array = indefinite_length_array;
  445. return true;
  446. }
  447. bool zcbor_list_start_decode(zcbor_state_t *state)
  448. {
  449. return list_map_start_decode(state, ZCBOR_MAJOR_TYPE_LIST);
  450. }
  451. bool zcbor_map_start_decode(zcbor_state_t *state)
  452. {
  453. bool ret = list_map_start_decode(state, ZCBOR_MAJOR_TYPE_MAP);
  454. if (ret && !state->indefinite_length_array) {
  455. if (state->elem_count >= (ZCBOR_MAX_ELEM_COUNT / 2)) {
  456. /* The new elem_count is too large. */
  457. ERR_RESTORE(ZCBOR_ERR_INT_SIZE);
  458. }
  459. state->elem_count *= 2;
  460. }
  461. return ret;
  462. }
  463. static bool array_end_expect(zcbor_state_t *state)
  464. {
  465. INITIAL_CHECKS();
  466. ZCBOR_ERR_IF(*state->payload != 0xFF, ZCBOR_ERR_WRONG_TYPE);
  467. state->payload++;
  468. return true;
  469. }
  470. static bool list_map_end_decode(zcbor_state_t *state)
  471. {
  472. uint_fast32_t max_elem_count = 0;
  473. if (state->indefinite_length_array) {
  474. if (!array_end_expect(state)) {
  475. ZCBOR_FAIL();
  476. }
  477. max_elem_count = ZCBOR_MAX_ELEM_COUNT;
  478. state->indefinite_length_array = false;
  479. }
  480. if (!zcbor_process_backup(state,
  481. ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_CONSUME | ZCBOR_FLAG_TRANSFER_PAYLOAD,
  482. max_elem_count)) {
  483. ZCBOR_FAIL();
  484. }
  485. return true;
  486. }
  487. bool zcbor_list_end_decode(zcbor_state_t *state)
  488. {
  489. return list_map_end_decode(state);
  490. }
  491. bool zcbor_map_end_decode(zcbor_state_t *state)
  492. {
  493. return list_map_end_decode(state);
  494. }
  495. bool zcbor_list_map_end_force_decode(zcbor_state_t *state)
  496. {
  497. if (!zcbor_process_backup(state,
  498. ZCBOR_FLAG_RESTORE | ZCBOR_FLAG_CONSUME | ZCBOR_FLAG_TRANSFER_PAYLOAD,
  499. ZCBOR_MAX_ELEM_COUNT)) {
  500. ZCBOR_FAIL();
  501. }
  502. return true;
  503. }
  504. static bool primx_expect(zcbor_state_t *state, uint8_t result)
  505. {
  506. uint32_t value;
  507. INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PRIM);
  508. if (!value_extract(state, &value, sizeof(value))) {
  509. ZCBOR_FAIL();
  510. }
  511. if (value != result) {
  512. return false;
  513. //ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
  514. }
  515. return true;
  516. }
  517. bool zcbor_nil_expect(zcbor_state_t *state, void *unused)
  518. {
  519. if (!primx_expect(state, 22)) {
  520. ZCBOR_FAIL();
  521. }
  522. return true;
  523. }
  524. bool zcbor_undefined_expect(zcbor_state_t *state, void *unused)
  525. {
  526. if (!primx_expect(state, 23)) {
  527. ZCBOR_FAIL();
  528. }
  529. return true;
  530. }
  531. bool zcbor_bool_decode(zcbor_state_t *state, bool *result)
  532. {
  533. if (zcbor_bool_expect(state, false)) {
  534. *result = false;
  535. } else if (zcbor_bool_expect(state, true)) {
  536. *result = true;
  537. } else {
  538. ZCBOR_FAIL();
  539. }
  540. zcbor_print("boolval: %u\r\n", *result);
  541. return true;
  542. }
  543. bool zcbor_bool_expect(zcbor_state_t *state, bool result)
  544. {
  545. if (!primx_expect(state, (uint8_t)(!!result) + ZCBOR_BOOL_TO_PRIM)) {
  546. //ZCBOR_FAIL();
  547. return false;
  548. }
  549. return true;
  550. }
  551. bool zcbor_float32_decode(zcbor_state_t *state, float *result)
  552. {
  553. INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PRIM);
  554. ZCBOR_ERR_IF(ADDITIONAL(*state->payload) != ZCBOR_VALUE_IS_4_BYTES, ZCBOR_ERR_FLOAT_SIZE);
  555. if (!value_extract(state, result, sizeof(*result))) {
  556. ZCBOR_FAIL();
  557. }
  558. return true;
  559. }
  560. bool zcbor_float32_expect(zcbor_state_t *state, float result)
  561. {
  562. float value;
  563. if (!zcbor_float32_decode(state, &value)) {
  564. ZCBOR_FAIL();
  565. }
  566. if (value != result) {
  567. ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
  568. }
  569. return true;
  570. }
  571. bool zcbor_float64_decode(zcbor_state_t *state, double *result)
  572. {
  573. INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PRIM);
  574. ZCBOR_ERR_IF(ADDITIONAL(*state->payload) != ZCBOR_VALUE_IS_8_BYTES, ZCBOR_ERR_FLOAT_SIZE);
  575. if (!value_extract(state, result, sizeof(*result))) {
  576. ZCBOR_FAIL();
  577. }
  578. return true;
  579. }
  580. bool zcbor_float64_expect(zcbor_state_t *state, double result)
  581. {
  582. double value;
  583. if (!zcbor_float64_decode(state, &value)) {
  584. ZCBOR_FAIL();
  585. }
  586. if (value != result) {
  587. ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
  588. }
  589. return true;
  590. }
  591. bool zcbor_float_decode(zcbor_state_t *state, double *result)
  592. {
  593. float float_result;
  594. if (zcbor_float32_decode(state, &float_result)) {
  595. *result = (double)float_result;
  596. } else if (!zcbor_float64_decode(state, result)) {
  597. ZCBOR_FAIL();
  598. }
  599. return true;
  600. }
  601. bool zcbor_float_expect(zcbor_state_t *state, double result)
  602. {
  603. if (zcbor_float32_expect(state, (float)result)) {
  604. /* Do nothing */
  605. } else if (!zcbor_float64_expect(state, result)) {
  606. ZCBOR_FAIL();
  607. }
  608. return true;
  609. }
  610. bool zcbor_any_skip(zcbor_state_t *state, void *result)
  611. {
  612. zcbor_assert_state(result == NULL,
  613. "'any' type cannot be returned, only skipped.\r\n");
  614. INITIAL_CHECKS();
  615. zcbor_major_type_t major_type = MAJOR_TYPE(*state->payload);
  616. uint8_t additional = ADDITIONAL(*state->payload);
  617. uint_fast32_t value;
  618. uint_fast32_t num_decode;
  619. uint_fast32_t temp_elem_count;
  620. uint_fast32_t elem_count_bak = state->elem_count;
  621. uint8_t const *payload_bak = state->payload;
  622. uint64_t tag_dummy;
  623. payload_bak = state->payload;
  624. if (!zcbor_multi_decode(0, ZCBOR_LARGE_ELEM_COUNT, &num_decode,
  625. (zcbor_decoder_t *)zcbor_tag_decode, state,
  626. (void *)&tag_dummy, 0)) {
  627. state->elem_count = elem_count_bak;
  628. state->payload = payload_bak;
  629. ZCBOR_FAIL();
  630. }
  631. if ((major_type == ZCBOR_MAJOR_TYPE_MAP) || (major_type == ZCBOR_MAJOR_TYPE_LIST)) {
  632. if (additional == ZCBOR_VALUE_IS_INDEFINITE_LENGTH) {
  633. ZCBOR_ERR_IF(state->elem_count == 0, ZCBOR_ERR_LOW_ELEM_COUNT);
  634. state->payload++;
  635. state->elem_count--;
  636. temp_elem_count = state->elem_count;
  637. payload_bak = state->payload;
  638. state->elem_count = ZCBOR_LARGE_ELEM_COUNT;
  639. if (!zcbor_multi_decode(0, ZCBOR_LARGE_ELEM_COUNT, &num_decode,
  640. (zcbor_decoder_t *)zcbor_any_skip, state,
  641. NULL, 0)
  642. || (state->payload >= state->payload_end)
  643. || !(*(state->payload++) == 0xFF)) {
  644. state->elem_count = elem_count_bak;
  645. state->payload = payload_bak;
  646. ZCBOR_FAIL();
  647. }
  648. state->elem_count = temp_elem_count;
  649. return true;
  650. }
  651. }
  652. if (!value_extract(state, &value, sizeof(value))) {
  653. /* Can happen because of elem_count (or payload_end) */
  654. ZCBOR_FAIL();
  655. }
  656. switch (major_type) {
  657. case ZCBOR_MAJOR_TYPE_BSTR:
  658. case ZCBOR_MAJOR_TYPE_TSTR:
  659. /* 'value' is the length of the BSTR or TSTR */
  660. if (value > (state->payload_end - state->payload)) {
  661. ZCBOR_ERR(ZCBOR_ERR_NO_PAYLOAD);
  662. }
  663. (state->payload) += value;
  664. break;
  665. case ZCBOR_MAJOR_TYPE_MAP:
  666. value *= 2; /* Because all members have a key. */
  667. /* Fallthrough */
  668. case ZCBOR_MAJOR_TYPE_LIST:
  669. temp_elem_count = state->elem_count;
  670. state->elem_count = value;
  671. if (!zcbor_multi_decode(value, value, &num_decode,
  672. (zcbor_decoder_t *)zcbor_any_skip, state,
  673. NULL, 0)) {
  674. state->elem_count = elem_count_bak;
  675. state->payload = payload_bak;
  676. ZCBOR_FAIL();
  677. }
  678. state->elem_count = temp_elem_count;
  679. break;
  680. default:
  681. /* Do nothing */
  682. break;
  683. }
  684. return true;
  685. }
  686. bool zcbor_tag_decode(zcbor_state_t *state, uint32_t *result)
  687. {
  688. INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_TAG);
  689. if (!value_extract(state, result, sizeof(*result))) {
  690. ZCBOR_FAIL();
  691. }
  692. state->elem_count++;
  693. return true;
  694. }
  695. bool zcbor_tag_expect(zcbor_state_t *state, uint32_t result)
  696. {
  697. uint32_t tag_val;
  698. if (!zcbor_tag_decode(state, &tag_val)) {
  699. ZCBOR_FAIL();
  700. }
  701. if (tag_val != result) {
  702. ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE);
  703. }
  704. return true;
  705. }
  706. bool zcbor_multi_decode(uint_fast32_t min_decode,
  707. uint_fast32_t max_decode,
  708. uint_fast32_t *num_decode,
  709. zcbor_decoder_t decoder,
  710. zcbor_state_t *state,
  711. void *result,
  712. uint_fast32_t result_len)
  713. {
  714. ZCBOR_CHECK_ERROR();
  715. for (uint_fast32_t i = 0; i < max_decode; i++) {
  716. uint8_t const *payload_bak = state->payload;
  717. uint_fast32_t elem_count_bak = state->elem_count;
  718. if (!decoder(state,
  719. (uint8_t *)result + i*result_len)) {
  720. *num_decode = i;
  721. state->payload = payload_bak;
  722. state->elem_count = elem_count_bak;
  723. ZCBOR_ERR_IF(i < min_decode, ZCBOR_ERR_ITERATIONS);
  724. zcbor_print("Found %" PRIuFAST32 " elements.\r\n", i);
  725. return true;
  726. }
  727. }
  728. zcbor_print("Found %" PRIuFAST32 " elements.\r\n", max_decode);
  729. *num_decode = max_decode;
  730. return true;
  731. }
  732. bool zcbor_present_decode(uint_fast32_t *present,
  733. zcbor_decoder_t decoder,
  734. zcbor_state_t *state,
  735. void *result)
  736. {
  737. uint_fast32_t num_decode;
  738. bool retval = zcbor_multi_decode(0, 1, &num_decode, decoder, state, result, 0);
  739. zcbor_assert_state(retval, "zcbor_multi_decode should not fail with these parameters.\r\n");
  740. *present = num_decode;
  741. return retval;
  742. }
  743. void zcbor_new_decode_state(zcbor_state_t *state_array, uint_fast32_t n_states,
  744. const uint8_t *payload, size_t payload_len, uint_fast32_t elem_count)
  745. {
  746. zcbor_new_state(state_array, n_states, payload, payload_len, elem_count);
  747. }