1 /* 2 * Copyright (c) 2020 Nordic Semiconductor ASA 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef ZCBOR_DECODE_H__ 8 #define ZCBOR_DECODE_H__ 9 10 #include <stdint.h> 11 #include <stdbool.h> 12 #include <stddef.h> 13 #include "zcbor_common.h" 14 15 #ifdef __cplusplus 16 extern "C" { 17 #endif 18 19 /** The zcbor_decode library provides functions for decoding CBOR data elements. 20 * 21 * See The README for an introduction to CBOR, including the meaning of pint, 22 * nint, bstr etc. 23 */ 24 25 26 /** See @ref zcbor_new_state() */ 27 void zcbor_new_decode_state(zcbor_state_t *state_array, size_t n_states, 28 const uint8_t *payload, size_t payload_len, size_t elem_count, 29 uint8_t *elem_state, size_t elem_state_bytes); 30 31 /** Convenience macro for declaring and initializing a decoding state with backups. 32 * 33 * This gives you a state variable named @p name. The variable functions like 34 * a pointer. 35 * 36 * @param[in] name The name of the new state variable. 37 * @param[in] num_backups The number of backup slots to keep in the state. 38 * @param[in] payload The payload to work on. 39 * @param[in] payload_size The size (in bytes) of @p payload. 40 * @param[in] elem_count The starting elem_count (typically 1). 41 * @param[in] n_flags For use if ZCBOR_MAP_SMART_SEARCH is enabled, ignored otherwise. 42 * The total number of unordered map search flags needed. 43 * I.e. the largest number of elements expected in an unordered map, 44 * including elements in nested unordered maps. 45 */ 46 #define ZCBOR_STATE_D(name, num_backups, payload, payload_size, elem_count, n_flags) \ 47 zcbor_state_t name[((num_backups) + 2 + ZCBOR_FLAG_STATES(n_flags))]; \ 48 do { \ 49 zcbor_new_decode_state(name, ZCBOR_ARRAY_SIZE(name), payload, payload_size, elem_count, \ 50 (uint8_t *)&name[(num_backups) + 1], ZCBOR_FLAG_STATES(n_flags) * sizeof(zcbor_state_t)); \ 51 } while(0) 52 53 54 /** The following applies to all _decode() functions listed directly below. 55 * 56 * @param[inout] state The current state of the decoding. 57 * @param[out] result Where to place the decoded value. 58 * @param[in] result_size (if present) Size in bytes of the memory at @p result 59 * 60 * @retval true If the value was decoded correctly. 61 * @retval false If the value has the wrong type, the payload overflowed, the 62 * element count was exhausted, or the value was larger than can 63 * fit in the result variable. 64 * Use zcbor_peek_error() to see the error code. 65 */ 66 bool zcbor_int32_decode(zcbor_state_t *state, int32_t *result); /* pint/nint */ 67 bool zcbor_int64_decode(zcbor_state_t *state, int64_t *result); /* pint/nint */ 68 bool zcbor_uint32_decode(zcbor_state_t *state, uint32_t *result); /* pint */ 69 bool zcbor_uint64_decode(zcbor_state_t *state, uint64_t *result); /* pint */ 70 bool zcbor_size_decode(zcbor_state_t *state, size_t *result); /* pint */ 71 bool zcbor_int_decode(zcbor_state_t *state, void *result, size_t result_size); /* pint/nint */ 72 bool zcbor_uint_decode(zcbor_state_t *state, void *result, size_t result_size); /* pint */ 73 bool zcbor_bstr_decode(zcbor_state_t *state, struct zcbor_string *result); /* bstr */ 74 bool zcbor_tstr_decode(zcbor_state_t *state, struct zcbor_string *result); /* tstr */ 75 bool zcbor_tag_decode(zcbor_state_t *state, uint32_t *result); /* CBOR tag */ 76 bool zcbor_simple_decode(zcbor_state_t *state, uint8_t *result); /* CBOR simple value */ 77 bool zcbor_bool_decode(zcbor_state_t *state, bool *result); /* boolean CBOR simple value */ 78 bool zcbor_float16_decode(zcbor_state_t *state, float *result); /* IEEE754 float16 */ 79 bool zcbor_float16_bytes_decode(zcbor_state_t *state, uint16_t *result); /* IEEE754 float16 raw bytes */ 80 bool zcbor_float16_32_decode(zcbor_state_t *state, float *result); /* IEEE754 float16 or float32 */ 81 bool zcbor_float32_decode(zcbor_state_t *state, float *result); /* IEEE754 float32 */ 82 bool zcbor_float32_64_decode(zcbor_state_t *state, double *result); /* IEEE754 float32 or float64 */ 83 bool zcbor_float64_decode(zcbor_state_t *state, double *result); /* IEEE754 float64 */ 84 bool zcbor_float_decode(zcbor_state_t *state, double *result); /* IEEE754 float16, float32, or float64 */ 85 86 /** The following applies to all _expect() and _pexpect() functions listed directly below. 87 * 88 * @param[inout] state The current state of the decoding. 89 * @param[in] expected The expected value. 90 * 91 * @retval true If the result was decoded correctly and has the expected value. 92 * @retval false If the decoding failed or the result doesn't have the 93 * expected value. 94 * Use zcbor_peek_error() to see the error code. 95 */ 96 bool zcbor_int32_expect(zcbor_state_t *state, int32_t expected); /* pint/nint */ 97 bool zcbor_int64_expect(zcbor_state_t *state, int64_t expected); /* pint/nint */ 98 bool zcbor_uint32_expect(zcbor_state_t *state, uint32_t expected); /* pint */ 99 bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t expected); /* pint */ 100 bool zcbor_size_expect(zcbor_state_t *state, size_t expected); /* pint */ 101 bool zcbor_bstr_expect(zcbor_state_t *state, struct zcbor_string *expected); /* bstr */ 102 bool zcbor_tstr_expect(zcbor_state_t *state, struct zcbor_string *expected); /* tstr */ 103 bool zcbor_tag_expect(zcbor_state_t *state, uint32_t expected); /* CBOR tag */ 104 bool zcbor_simple_expect(zcbor_state_t *state, uint8_t expected); /* CBOR simple value */ 105 bool zcbor_bool_expect(zcbor_state_t *state, bool expected); /* boolean CBOR simple value */ 106 bool zcbor_nil_expect(zcbor_state_t *state, void *unused); /* 'nil' CBOR simple value */ 107 bool zcbor_undefined_expect(zcbor_state_t *state, void *unused); /* 'undefined' CBOR simple value */ 108 bool zcbor_float16_expect(zcbor_state_t *state, float expected); /* IEEE754 float16 */ 109 bool zcbor_float16_bytes_expect(zcbor_state_t *state, uint16_t expected); /* IEEE754 float16 raw bytes */ 110 bool zcbor_float16_32_expect(zcbor_state_t *state, float expected); /* IEEE754 float16 or float32 */ 111 bool zcbor_float32_expect(zcbor_state_t *state, float expected); /* IEEE754 float32 */ 112 bool zcbor_float32_64_expect(zcbor_state_t *state, double expected); /* IEEE754 float32 or float64 */ 113 bool zcbor_float64_expect(zcbor_state_t *state, double expected); /* IEEE754 float64 */ 114 bool zcbor_float_expect(zcbor_state_t *state, double expected); /* IEEE754 float16, float32, or float64 */ 115 116 /** Like the _expect() functions but the value is passed through a pointer. 117 * (for use as a zcbor_decoder_t function) */ 118 bool zcbor_int32_pexpect(zcbor_state_t *state, int32_t *expected); /* pint/nint */ 119 bool zcbor_int64_pexpect(zcbor_state_t *state, int64_t *expected); /* pint/nint */ 120 bool zcbor_uint32_pexpect(zcbor_state_t *state, uint32_t *expected); /* pint */ 121 bool zcbor_uint64_pexpect(zcbor_state_t *state, uint64_t *expected); /* pint */ 122 bool zcbor_size_pexpect(zcbor_state_t *state, size_t *expected); /* pint */ 123 bool zcbor_tag_pexpect(zcbor_state_t *state, uint32_t *expected); /* CBOR tag */ 124 bool zcbor_simple_pexpect(zcbor_state_t *state, uint8_t *expected); /* CBOR simple value */ 125 bool zcbor_bool_pexpect(zcbor_state_t *state, bool *expected); /* boolean CBOR simple value */ 126 bool zcbor_float16_pexpect(zcbor_state_t *state, float *expected); /* IEEE754 float16 */ 127 bool zcbor_float16_bytes_pexpect(zcbor_state_t *state, uint16_t *expected); /* IEEE754 float16 raw bytes */ 128 bool zcbor_float16_32_pexpect(zcbor_state_t *state, float *expected); /* IEEE754 float16 or float32 */ 129 bool zcbor_float32_pexpect(zcbor_state_t *state, float *expected); /* IEEE754 float32 */ 130 bool zcbor_float32_64_pexpect(zcbor_state_t *state, double *expected); /* IEEE754 float32 or float64 */ 131 bool zcbor_float64_pexpect(zcbor_state_t *state, double *expected); /* IEEE754 float64 */ 132 bool zcbor_float_pexpect(zcbor_state_t *state, double *expected); /* IEEE754 float16, float32, or float64 */ 133 134 /** Consume and expect a pint/nint with a certain value, within a union. 135 * 136 * Calls @ref zcbor_union_elem_code then @ref zcbor_[u]int[32|64]_expect. 137 */ 138 bool zcbor_int32_expect_union(zcbor_state_t *state, int32_t expected); 139 bool zcbor_int64_expect_union(zcbor_state_t *state, int64_t expected); 140 bool zcbor_uint32_expect_union(zcbor_state_t *state, uint32_t expected); 141 bool zcbor_uint64_expect_union(zcbor_state_t *state, uint64_t expected); 142 143 /** Decode and consume a list/map header. 144 * 145 * The contents of the list can be decoded via subsequent function calls. 146 * A state backup is created to keep track of the element count. 147 * Call @ref zcbor_list_end_decode / @ref zcbor_map_end_decode when done 148 * decoding the contents of the list/map 149 * 150 * @retval true Header decoded correctly 151 * @retval false Header decoded incorrectly, or backup failed. 152 */ 153 bool zcbor_list_start_decode(zcbor_state_t *state); 154 bool zcbor_map_start_decode(zcbor_state_t *state); 155 bool zcbor_unordered_map_start_decode(zcbor_state_t *state); 156 157 /** Search for a key in a map. 158 * 159 * The CBOR spec allows elements (key-value pairs) in maps to appear in any order. 160 * This function should be used when the order of elements is unknown. 161 * 162 * This must only be used while inside a map that has been entered via 163 * @ref zcbor_unordered_map_start_decode. Use @ref zcbor_unordered_map_end_decode 164 * when leaving the map. 165 * 166 * This function searches for keys. When this function returns successfully, 167 * the @p state is pointing to the value corresponding to the found key. 168 * Therefore, to be able to call this function again, the value must first be 169 * decoded or skipped. 170 * 171 * When searching unordered maps, the found elements must be kept track of. 172 * By default, this function automatically keeps track, which means it keeps a 173 * running count of the number of found elements, which is checked when exiting 174 * the map. You can do this manually instead, see @ref zcbor_elem_processed and 175 * @ref manually_process_elem. If ZCBOR_MAP_SMART_SEARCH is defined, a flag is 176 * kept for each element, instead of a rolling count. 177 * 178 * @note Unless ZCBOR_MAP_SMART_SEARCH is defined, 179 * elements are not individually marked as processed, so they may 180 * be returned again in a subsequent call to this function, if it is 181 * matched by the @p key_decoder of that call. Because of this, you should 182 * only use this function when you know the @p key_decoder matches no more 183 * than one of the keys. Typically this means all keys are known strings 184 * or integers, i.e. the @p key_decoder is typically a _pexpect() function. 185 * 186 * When searching for strings, there are convenience functions available, 187 * see the zcbor_search_key_* functions. 188 * 189 * @param[in] key_decoder A decoding function that will be tried against all 190 * keys in the map until it returns true, at which point 191 * @ref zcbor_unordered_map_search will return true. 192 * For example, a zcbor_*_pexpect() function. 193 * @param[inout] state The current state of decoding. Must be currently decoding 194 * the contents of a map, and pointing to one (any) of the 195 * keys, not one of the values. If successful, the @p state 196 * will be pointing to the value corresponding to the 197 * matched key. If unsuccessful, the @p state will be 198 * unchanged. 199 * @param[inout] key_result This will be passed as the second argument to the 200 * @p key_decoder. 201 * 202 * @retval true If the key was found, i.e. @p key_decoder returned true. 203 * @retval false If the key was not found after searching all map elements. 204 * Or the map was pointing to a value (not a key). 205 * Or an unexpected error happened while skipping elements or 206 * jumping from the end of the map to the start. 207 */ 208 bool zcbor_unordered_map_search(zcbor_decoder_t key_decoder, zcbor_state_t *state, void *key_result); 209 210 /** Find a specific bstr/tstr key as part of a map with unknown element order. 211 * 212 * Uses @ref zcbor_unordered_map_search under the hood. Please refer to those docs 213 * for the conditions under which this can be called. 214 * Refer to the docs for zcbor_(t|b)str_expect_* (e.g. @ref zcbor_bstr_expect_ptr) 215 * for parameter docs. 216 */ 217 bool zcbor_search_key_bstr_ptr(zcbor_state_t *state, char const *ptr, size_t len); 218 bool zcbor_search_key_tstr_ptr(zcbor_state_t *state, char const *ptr, size_t len); 219 bool zcbor_search_key_bstr_term(zcbor_state_t *state, char const *str, size_t maxlen); 220 bool zcbor_search_key_tstr_term(zcbor_state_t *state, char const *str, size_t maxlen); 221 #define zcbor_search_key_bstr_lit(state, str) zcbor_search_key_bstr_ptr(state, str, sizeof(str) - 1) 222 #define zcbor_search_key_tstr_lit(state, str) zcbor_search_key_tstr_ptr(state, str, sizeof(str) - 1) 223 #define zcbor_search_key_bstr_arr(state, str) zcbor_search_key_bstr_ptr(state, str, (sizeof(str))) 224 #define zcbor_search_key_tstr_arr(state, str) zcbor_search_key_tstr_ptr(state, str, (sizeof(str))) 225 226 /** (Optional) Call this function to mark an (unordered map) element as processed. 227 * 228 * @note This should not be called unless the @ref manually_process_elem flag is set. 229 * By default, i.e. when @ref manually_process_elem is not set, this function is 230 * called internally by @ref zcbor_unordered_map_search whenever a key is found. 231 * 232 * By default, this function increments the internal count @ref map_elems_processed. 233 * 234 * If ZCBOR_MAP_SMART_SEARCH is defined, this function instead clears a flag for the 235 * element (key-value pair) that is currently being processed, or that has just been 236 * processed, meaning the element won't be found again via @ref zcbor_unordered_map_search. 237 * 238 * @ref zcbor_unordered_map_end_decode will fail if @ref map_elems_processed does not 239 * match the number of elements in the map, or if any of the map element's flag is set. 240 */ 241 bool zcbor_elem_processed(zcbor_state_t *state); 242 243 /** Finalize decoding a list/map 244 * 245 * Check that the list/map had the correct number of elements, and restore the 246 * previous element count from the backup. 247 * 248 * Use @ref zcbor_list_map_end_force_decode to forcibly consume the backup if 249 * something has gone wrong. 250 * 251 * In all successful cases, the state is returned pointing to the byte/element 252 * after the list/map in the payload. 253 * 254 * @retval true Everything ok. 255 * @retval false Element count not correct. 256 */ 257 bool zcbor_list_end_decode(zcbor_state_t *state); 258 bool zcbor_map_end_decode(zcbor_state_t *state); 259 bool zcbor_unordered_map_end_decode(zcbor_state_t *state); 260 bool zcbor_list_map_end_force_decode(zcbor_state_t *state); 261 262 /** Find whether the state is at the end of a list or map. 263 */ 264 bool zcbor_array_at_end(zcbor_state_t *state); 265 266 /** Skip a single element, regardless of type and value. 267 * 268 * This means if the element is a map or list, this function will recursively 269 * skip all its contents. 270 * This function will also skip any tags preceeding the element. 271 * 272 * @param[inout] state The current state of the decoding. 273 * @param[in] unused Unused parameter to maintain signature parity with 274 * @ref zcbor_decoder_t. 275 */ 276 bool zcbor_any_skip(zcbor_state_t *state, void *unused); 277 278 /** Decode 0 or more elements with the same type and constraints. 279 * 280 * The decoded values will appear consecutively in the @p result array. 281 * 282 * The following is an example of decoding a list containing 3 INTS followed by 283 * 0 to 2 bstrs: 284 * 285 * @code{c} 286 * uint32_t ints[3]; 287 * struct zcbor_string bstrs[2]; 288 * uint32_t num_decode; 289 * bool res; 290 * 291 * res = zcbor_list_start_decode(state); 292 * res = res && zcbor_multi_decode(3, 3, &num_decode, zcbor_uint32_decode, 293 * state, ints, sizeof(ints[0])); 294 * res = res && zcbor_multi_decode(0, 2, &num_decode, zcbor_bstr_decode, 295 * state, bstrs, sizeof(bstrs[0])); 296 * res = res && zcbor_list_end_decode(state); 297 * // check res 298 * @endcode 299 * 300 * The @ref zcbor_decoder_t type is designed to be compatible with all single- 301 * value decoder functions in this library, e.g. @ref zcbor_uint32_decode, 302 * @ref zcbor_tstr_expect, @ref zcbor_nil_expect, etc. For _expect() functions, 303 * @p result will be used as a value instead of an array/pointer, so 304 * @p result_len will determine how much the value changes for each call. 305 * To decode the same value multiple times, use a @p result_len of 0. 306 * This function can also be used with custom decoder functions, such as those 307 * generated by the zcbor.py script, which for example decodes larger chunks of 308 * the data at once. 309 * 310 * @param[in] min_decode The minimum acceptable number of elements. 311 * @param[in] max_decode The maximum acceptable number of elements. 312 * @param[out] num_decode The actual number of elements decoded. 313 * @param[in] decoder The decoder function to call under the hood. This 314 * function will be called with the provided arguments 315 * repeatedly until the function fails (returns false) 316 * or until it has been called @p max_decode times. 317 * The result pointer is moved @p result_len bytes for 318 * each call to @p decoder, i.e. @p result refers to 319 * an array of result variables. 320 * Should not be an _expect() function, use 321 * _pexpect() instead. 322 * @param[out] result Where to place the decoded values. Must be an array 323 * of at least @p max_decode elements. 324 * @param[in] result_len The length of each result variable. Must be the 325 * length of the individual elements of @p result. 326 * 327 * @retval true If at least @p min_decode variables were correctly decoded. 328 * @retval false If @p decoder failed before having decoded @p min_decode 329 * values. 330 */ 331 bool zcbor_multi_decode(size_t min_decode, size_t max_decode, size_t *num_decode, 332 zcbor_decoder_t decoder, zcbor_state_t *state, void *result, 333 size_t result_len); 334 335 /** Attempt to decode a value that might not be present in the data. 336 * 337 * Works like @ref zcbor_multi_decode, with @p present as num_decode. 338 * Will return true, even if the data is not present. 339 * 340 * @param[out] present Whether or not the data was present and successfully decoded. 341 * @param[in] decoder The decoder to attempt. 342 * @param[out] result The result, if present. 343 * 344 * @return Should always return true. 345 */ 346 bool zcbor_present_decode(bool *present, 347 zcbor_decoder_t decoder, 348 zcbor_state_t *state, 349 void *result); 350 351 352 /** Supplementary string (bstr/tstr) decoding functions: */ 353 354 /** Consume and expect a bstr/tstr with the value of the provided char/uint8_t array. 355 * 356 * @param[inout] state The current state of the decoding. 357 * @param[in] str The value to expect. A pointer to the string/array. 358 * _term() uses strnlen(), so @p str must be null-terminated. 359 * _lit() uses sizeof()-1, so @p str must be a (null-terminated) string literal. 360 * _arr() uses sizeof(), so @p str must be a uint8_t array (not null-terminated). 361 * @param[in] len (if present) The length of the string pointed to by @p str 362 * @param[in] maxlen (if present) The maximum length of the string pointed to by @p str. 363 * This value is passed to strnlen. 364 */ 365 bool zcbor_bstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len); 366 bool zcbor_tstr_expect_ptr(zcbor_state_t *state, char const *ptr, size_t len); 367 bool zcbor_bstr_expect_term(zcbor_state_t *state, char const *str, size_t maxlen); 368 bool zcbor_tstr_expect_term(zcbor_state_t *state, char const *str, size_t maxlen); 369 #define zcbor_bstr_expect_lit(state, str) zcbor_bstr_expect_ptr(state, str, sizeof(str) - 1) 370 #define zcbor_tstr_expect_lit(state, str) zcbor_tstr_expect_ptr(state, str, sizeof(str) - 1) 371 #define zcbor_bstr_expect_arr(state, str) zcbor_bstr_expect_ptr(state, str, sizeof(str)) 372 #define zcbor_tstr_expect_arr(state, str) zcbor_tstr_expect_ptr(state, str, sizeof(str)) 373 374 /** Decode and consume a bstr header. 375 * 376 * The rest of the string can be decoded as CBOR. 377 * A state backup is created to keep track of the element count. 378 * Call @ref zcbor_bstr_end_decode when done decoding the contents of the bstr. 379 * 380 * @param[inout] state The current state of the decoding. 381 * @param[out] result The resulting string, for reference. The string should be decoded via 382 * functions from this API since state is pointing to the start of the string, 383 * not the end. 384 * 385 * @retval true Header decoded correctly 386 * @retval false Header decoded incorrectly, or backup failed, or payload is not large enough 387 * to contain the contents of the string. Use @ref zcbor_bstr_start_decode_fragment 388 * for decoding fragmented payloads. 389 */ 390 bool zcbor_bstr_start_decode(zcbor_state_t *state, struct zcbor_string *result); 391 392 /** Finalize decoding a CBOR-encoded bstr. 393 * 394 * Restore element count from backup. 395 */ 396 bool zcbor_bstr_end_decode(zcbor_state_t *state); 397 398 399 /** Supplementary string (bstr/tstr) decoding functions for fragmented payloads: */ 400 401 /** Start decoding a bstr/tstr, even if the payload contains only part of it. 402 * 403 * This must be followed by a call to @ref zcbor_update_state, which can be 404 * followed by a call to @ref zcbor_next_fragment. Do not call this function 405 * again on subsequent fragments of the same string. 406 * 407 * This consumes the remaining payload as long as it belongs to the string. 408 */ 409 bool zcbor_bstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result); 410 bool zcbor_tstr_decode_fragment(zcbor_state_t *state, struct zcbor_string_fragment *result); 411 412 /** Extract the next fragment of a string. 413 * 414 * Use this function to extract all but the first fragment. 415 */ 416 void zcbor_next_fragment(zcbor_state_t *state, 417 struct zcbor_string_fragment *prev_fragment, 418 struct zcbor_string_fragment *result); 419 420 /** Decode and consume a bstr header, assuming the payload does not contain the whole bstr. 421 * 422 * The rest of the string can be decoded as CBOR. 423 * A state backup is created to keep track of the element count. 424 * Call @ref zcbor_update_state followed by @ref zcbor_bstr_next_fragment when 425 * the current payload has been exhausted. 426 * Call @ref zcbor_bstr_end_decode when done decoding the contents of the bstr. 427 */ 428 bool zcbor_bstr_start_decode_fragment(zcbor_state_t *state, 429 struct zcbor_string_fragment *result); 430 431 /** Start decoding the next fragment of a string. 432 * 433 * Use this function to extract all but the first fragment of a CBOR-encoded 434 * bstr. 435 */ 436 void zcbor_bstr_next_fragment(zcbor_state_t *state, 437 struct zcbor_string_fragment *prev_fragment, 438 struct zcbor_string_fragment *result); 439 440 /** Can be used on any fragment to tell if it is the final fragment of the string. */ 441 bool zcbor_is_last_fragment(const struct zcbor_string_fragment *fragment); 442 443 #ifdef __cplusplus 444 } 445 #endif 446 447 #endif /* ZCBOR_DECODE_H__ */ 448