Laurence Lundblade | 6ed3422 | 2018-12-18 09:46:23 -0800 | [diff] [blame] | 1 | /*============================================================================== |
| 2 | Copyright (c) 2016-2018, The Linux Foundation. |
| 3 | Copyright (c) 2018-2019, Laurence Lundblade. |
| 4 | All rights reserved. |
| 5 | |
| 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are |
| 8 | met: |
| 9 | * Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | * Redistributions in binary form must reproduce the above |
| 12 | copyright notice, this list of conditions and the following |
| 13 | disclaimer in the documentation and/or other materials provided |
| 14 | with the distribution. |
| 15 | * Neither the name of The Linux Foundation nor the names of its |
| 16 | contributors, nor the name "Laurence Lundblade" may be used to |
| 17 | endorse or promote products derived from this software without |
| 18 | specific prior written permission. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | ==============================================================================*/ |
| 32 | |
| 33 | |
| 34 | /*=================================================================================== |
| 35 | FILE: qcbor.h |
| 36 | |
| 37 | DESCRIPTION: This is the full public API and data structures for QCBOR |
| 38 | |
| 39 | EDIT HISTORY FOR FILE: |
| 40 | |
| 41 | This section contains comments describing changes made to the module. |
| 42 | Notice that changes are listed in reverse chronological order. |
| 43 | |
| 44 | when who what, where, why |
| 45 | -------- ---- --------------------------------------------------- |
| 46 | 12/18/18 llundblade Move decode malloc optional code to separate repository |
| 47 | 12/13/18 llundblade Documentatation improvements |
| 48 | 11/29/18 llundblade Rework to simpler handling of tags and labels. |
| 49 | 11/9/18 llundblade Error codes are now enums. |
| 50 | 11/1/18 llundblade Floating support. |
| 51 | 10/31/18 llundblade Switch to one license that is almost BSD-3. |
| 52 | 10/15/18 llundblade Indefinite length maps and arrays supported |
| 53 | 10/8/18 llundblade Indefinite length strings supported |
| 54 | 09/28/18 llundblade Added bstr wrapping feature for COSE implementation. |
| 55 | 07/05/17 llundbla Add bstr wrapping of maps/arrays for COSE. |
| 56 | 03/01/17 llundbla More data types; decoding improvements and fixes. |
| 57 | 11/13/16 llundbla Integrate most TZ changes back into github version. |
| 58 | 09/30/16 gkanike Porting to TZ. |
| 59 | 03/15/16 llundbla Initial Version. |
| 60 | |
| 61 | =====================================================================================*/ |
| 62 | |
| 63 | #ifndef __QCBOR__qcbor__ |
| 64 | #define __QCBOR__qcbor__ |
| 65 | |
| 66 | /*...... This is a ruler that is 80 characters long...........................*/ |
| 67 | |
| 68 | /* =========================================================================== |
| 69 | BEGINNING OF PRIVATE PART OF THIS FILE |
| 70 | |
| 71 | Caller of QCBOR should not reference any of the details below up until |
| 72 | the start of the public part. |
| 73 | =========================================================================== */ |
| 74 | |
| 75 | /* |
| 76 | Standard integer types are used in the interface to be precise about |
| 77 | sizes to be better at preventing underflow/overflow errors. |
| 78 | */ |
| 79 | #include <stdint.h> |
| 80 | #include <stdbool.h> |
| 81 | #include "UsefulBuf.h" |
| 82 | |
| 83 | |
| 84 | /* |
| 85 | The maxium nesting of arrays and maps when encoding or decoding. |
| 86 | (Further down in the file there is a definition that refers to this |
| 87 | that is public. This is done this way so there can be a nice |
| 88 | separation of public and private parts in this file. |
| 89 | */ |
| 90 | #define QCBOR_MAX_ARRAY_NESTING1 15 // Do not increase this over 255 |
| 91 | |
| 92 | |
| 93 | /* The largest offset to the start of an array or map. It is slightly |
| 94 | less than UINT32_MAX so the error condition can be tests on 32-bit machines. |
| 95 | UINT32_MAX comes from uStart in QCBORTrackNesting being a uin32_t. |
| 96 | |
| 97 | This will cause trouble on a machine where size_t is less than 32-bits. |
| 98 | */ |
| 99 | #define QCBOR_MAX_ARRAY_OFFSET (UINT32_MAX - 100) |
| 100 | |
| 101 | /* |
| 102 | PRIVATE DATA STRUCTURE |
| 103 | |
| 104 | Holds the data for tracking array and map nesting during encoding. Pairs up with |
| 105 | the Nesting_xxx functions to make an "object" to handle nesting encoding. |
| 106 | |
| 107 | uStart is a uint32_t instead of a size_t to keep the size of this |
| 108 | struct down so it can be on the stack without any concern. It would be about |
| 109 | double if size_t was used instead. |
| 110 | |
| 111 | Size approximation (varies with CPU/compiler): |
| 112 | 64-bit machine: (15 + 1) * (4 + 2 + 1 + 1 pad) + 8 = 136 bytes |
| 113 | 32-bit machine: (15 + 1) * (4 + 2 + 1 + 1 pad) + 4 = 132 bytes |
| 114 | */ |
| 115 | typedef struct __QCBORTrackNesting { |
| 116 | // PRIVATE DATA STRUCTURE |
| 117 | struct { |
| 118 | // See function OpenArrayInternal() for detailed comments on how this works |
| 119 | uint32_t uStart; // uStart is the byte position where the array starts |
| 120 | uint16_t uCount; // Number of items in the arrary or map; counts items in a map, not pairs of items |
| 121 | uint8_t uMajorType; // Indicates if item is a map or an array |
| 122 | } pArrays[QCBOR_MAX_ARRAY_NESTING1+1], // stored state for the nesting levels |
| 123 | *pCurrentNesting; // the current nesting level |
| 124 | } QCBORTrackNesting; |
| 125 | |
| 126 | |
| 127 | /* |
| 128 | PRIVATE DATA STRUCTURE |
| 129 | |
| 130 | Context / data object for encoding some CBOR. Used by all encode functions to |
| 131 | form a public "object" that does the job of encdoing. |
| 132 | |
| 133 | Size approximation (varies with CPU/compiler): |
| 134 | 64-bit machine: 27 + 1 (+ 4 padding) + 136 = 32 + 136 = 168 bytes |
| 135 | 32-bit machine: 15 + 1 + 132 = 148 bytes |
| 136 | */ |
| 137 | struct _QCBOREncodeContext { |
| 138 | // PRIVATE DATA STRUCTURE |
| 139 | UsefulOutBuf OutBuf; // Pointer to output buffer, its length and position in it |
| 140 | uint8_t uError; // Error state |
| 141 | QCBORTrackNesting nesting; // Keep track of array and map nesting |
| 142 | }; |
| 143 | |
| 144 | |
| 145 | /* |
| 146 | PRIVATE DATA STRUCTURE |
| 147 | |
| 148 | Holds the data for array and map nesting for decoding work. This structure |
| 149 | and the DecodeNesting_xxx functions form an "object" that does the work |
| 150 | for arrays and maps. |
| 151 | |
| 152 | Size approximation (varies with CPU/compiler): |
| 153 | 64-bit machine: 4 * 16 + 8 = 72 |
| 154 | 32-bit machine: 4 * 16 + 4 = 68 |
| 155 | */ |
| 156 | typedef struct __QCBORDecodeNesting { |
| 157 | // PRIVATE DATA STRUCTURE |
| 158 | struct { |
| 159 | uint16_t uCount; |
| 160 | uint8_t uMajorType; |
| 161 | } pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING1+1], |
| 162 | *pCurrent; |
| 163 | } QCBORDecodeNesting; |
| 164 | |
| 165 | |
| 166 | /* |
| 167 | PRIVATE DATA STRUCTURE |
| 168 | |
| 169 | The decode context. This data structure plus the public QCBORDecode_xxx |
| 170 | functions form an "object" that does CBOR decoding. |
| 171 | |
| 172 | Size approximation (varies with CPU/compiler): |
| 173 | 64-bit machine: 32 + 1 + 1 + 6 bytes padding + 72 + 16 = 128 bytes |
| 174 | 32-bit machine: 16 + 1 + 1 + 2 bytes padding + 68 + 8 = 68 bytes |
| 175 | */ |
| 176 | struct _QCBORDecodeContext { |
| 177 | // PRIVATE DATA STRUCTURE |
| 178 | UsefulInputBuf InBuf; |
| 179 | |
| 180 | uint8_t uDecodeMode; |
| 181 | uint8_t bStringAllocateAll; |
| 182 | |
| 183 | QCBORDecodeNesting nesting; |
| 184 | |
| 185 | // This is NULL or points to a QCBORStringAllocator. It is void |
| 186 | // here because _QCBORDecodeContext is defined early in the |
| 187 | // private part of this file and QCBORStringAllocat is defined |
| 188 | // later in the public part of this file. |
| 189 | void *pStringAllocator; |
| 190 | |
| 191 | // This is NULL or points to QCBORTagList. |
| 192 | // It is type void for the same reason as above. |
| 193 | const void *pCallerConfiguredTagList; |
| 194 | }; |
| 195 | |
| 196 | // Used internally in the impementation here |
| 197 | // Must not conflict with any of the official CBOR types |
| 198 | #define CBOR_MAJOR_NONE_TYPE_RAW 9 |
| 199 | #define CBOR_MAJOR_NONE_TAG_LABEL_REORDER 10 |
| 200 | |
| 201 | |
| 202 | /* =========================================================================== |
| 203 | END OF PRIVATE PART OF THIS FILE |
| 204 | |
| 205 | BEGINNING OF PUBLIC PART OF THIS FILE |
| 206 | =========================================================================== */ |
| 207 | |
| 208 | |
| 209 | |
| 210 | /* =========================================================================== |
| 211 | BEGINNING OF CONSTANTS THAT COME FROM THE CBOR STANDARD, RFC 7049 |
| 212 | |
| 213 | It is not necessary to use these directly when encoding or decoding |
| 214 | CBOR with this implementation. |
| 215 | =========================================================================== */ |
| 216 | |
| 217 | /* Standard CBOR Major type for positive integers of various lengths */ |
| 218 | #define CBOR_MAJOR_TYPE_POSITIVE_INT 0 |
| 219 | |
| 220 | /* Standard CBOR Major type for negative integer of various lengths */ |
| 221 | #define CBOR_MAJOR_TYPE_NEGATIVE_INT 1 |
| 222 | |
| 223 | /* Standard CBOR Major type for an array of arbitrary 8-bit bytes. */ |
| 224 | #define CBOR_MAJOR_TYPE_BYTE_STRING 2 |
| 225 | |
| 226 | /* Standard CBOR Major type for a UTF-8 string. Note this is true 8-bit UTF8 |
| 227 | with no encoding and no NULL termination */ |
| 228 | #define CBOR_MAJOR_TYPE_TEXT_STRING 3 |
| 229 | |
| 230 | /* Standard CBOR Major type for an ordered array of other CBOR data items */ |
| 231 | #define CBOR_MAJOR_TYPE_ARRAY 4 |
| 232 | |
| 233 | /* Standard CBOR Major type for CBOR MAP. Maps an array of pairs. The |
| 234 | first item in the pair is the "label" (key, name or identfier) and the second |
| 235 | item is the value. */ |
| 236 | #define CBOR_MAJOR_TYPE_MAP 5 |
| 237 | |
| 238 | /* Standard CBOR optional tagging. This tags things like dates and URLs */ |
| 239 | #define CBOR_MAJOR_TYPE_OPTIONAL 6 |
| 240 | |
| 241 | /* Standard CBOR extra simple types like floats and the values true and false */ |
| 242 | #define CBOR_MAJOR_TYPE_SIMPLE 7 |
| 243 | |
| 244 | |
| 245 | /* |
| 246 | These are special values for the AdditionalInfo bits that are part of the first byte. |
| 247 | Mostly they encode the length of the data item. |
| 248 | */ |
| 249 | #define LEN_IS_ONE_BYTE 24 |
| 250 | #define LEN_IS_TWO_BYTES 25 |
| 251 | #define LEN_IS_FOUR_BYTES 26 |
| 252 | #define LEN_IS_EIGHT_BYTES 27 |
| 253 | #define ADDINFO_RESERVED1 28 |
| 254 | #define ADDINFO_RESERVED2 29 |
| 255 | #define ADDINFO_RESERVED3 30 |
| 256 | #define LEN_IS_INDEFINITE 31 |
| 257 | |
| 258 | |
| 259 | /* |
| 260 | 24 is a special number for CBOR. Integers and lengths |
| 261 | less than it are encoded in the same byte as the major type |
| 262 | */ |
| 263 | #define CBOR_TWENTY_FOUR 24 |
| 264 | |
| 265 | |
| 266 | /* |
| 267 | Tags that are used with CBOR_MAJOR_TYPE_OPTIONAL. These are |
| 268 | the ones defined in the CBOR spec. |
| 269 | */ |
| 270 | /** See QCBOREncode_AddDateString() below */ |
| 271 | #define CBOR_TAG_DATE_STRING 0 |
| 272 | /** See QCBOREncode_AddDateEpoch_2() */ |
| 273 | #define CBOR_TAG_DATE_EPOCH 1 |
| 274 | #define CBOR_TAG_POS_BIGNUM 2 |
| 275 | #define CBOR_TAG_NEG_BIGNUM 3 |
| 276 | #define CBOR_TAG_FRACTION 4 |
| 277 | #define CBOR_TAG_BIGFLOAT 5 |
| 278 | |
| 279 | #define CBOR_TAG_COSE_ENCRYPTO 16 |
| 280 | #define CBOR_TAG_COSE_MAC0 17 |
| 281 | #define CBOR_TAG_COSE_SIGN1 18 |
| 282 | |
| 283 | /* The data in byte string should be converted in base 64 URL when encoding in JSON or similar text-based representations */ |
| 284 | #define CBOR_TAG_ENC_AS_B64URL 21 |
| 285 | /* The data in byte string should be encoded in base 64 when encoding in JSON */ |
| 286 | #define CBOR_TAG_ENC_AS_B64 22 |
| 287 | /* The data in byte string should be encoded in base 16 when encoding in JSON */ |
| 288 | #define CBOR_TAG_ENC_AS_B16 23 |
| 289 | #define CBOR_TAG_CBOR 24 |
| 290 | /** The data in the string is a URIs, as defined in RFC3986 */ |
| 291 | #define CBOR_TAG_URI 32 |
| 292 | /** The data in the string is a base 64'd URL */ |
| 293 | #define CBOR_TAG_B64URL 33 |
| 294 | /** The data in the string is base 64'd */ |
| 295 | #define CBOR_TAG_B64 34 |
| 296 | /** regular expressions in Perl Compatible Regular Expressions (PCRE) / JavaScript syntax ECMA262. */ |
| 297 | #define CBOR_TAG_REGEX 35 |
| 298 | /** MIME messages (including all headers), as defined in RFC2045 */ |
| 299 | #define CBOR_TAG_MIME 36 |
| 300 | /** Binary UUID */ |
| 301 | #define CBOR_TAG_BIN_UUID 37 |
| 302 | |
| 303 | #define CBOR_TAG_CWT 61 |
| 304 | |
| 305 | #define CBOR_TAG_ENCRYPT 96 |
| 306 | #define CBOR_TAG_MAC 97 |
| 307 | #define CBOR_TAG_SIGN 98 |
| 308 | |
| 309 | #define CBOR_TAG_GEO_COORD 103 |
| 310 | |
| 311 | |
| 312 | /** The data is CBOR data */ |
| 313 | #define CBOR_TAG_CBOR_MAGIC 55799 |
| 314 | #define CBOR_TAG_NONE UINT64_MAX |
| 315 | |
| 316 | |
| 317 | /* |
| 318 | Values for the 5 bits for items of major type 7 |
| 319 | */ |
| 320 | #define CBOR_SIMPLEV_FALSE 20 |
| 321 | #define CBOR_SIMPLEV_TRUE 21 |
| 322 | #define CBOR_SIMPLEV_NULL 22 |
| 323 | #define CBOR_SIMPLEV_UNDEF 23 |
| 324 | #define CBOR_SIMPLEV_ONEBYTE 24 |
| 325 | #define HALF_PREC_FLOAT 25 |
| 326 | #define SINGLE_PREC_FLOAT 26 |
| 327 | #define DOUBLE_PREC_FLOAT 27 |
| 328 | #define CBOR_SIMPLE_BREAK 31 |
| 329 | |
| 330 | |
| 331 | |
| 332 | /* =========================================================================== |
| 333 | |
| 334 | END OF CONSTANTS THAT COME FROM THE CBOR STANDARD, RFC 7049 |
| 335 | |
| 336 | BEGINNING OF PUBLIC INTERFACE FOR QCBOR ENCODER / DECODER |
| 337 | |
| 338 | =========================================================================== */ |
| 339 | |
| 340 | /** |
| 341 | |
| 342 | @file qcbor.h |
| 343 | |
| 344 | Q C B O R E n c o d e / D e c o d e |
| 345 | |
| 346 | This implements CBOR -- Concise Binary Object Representation as defined |
| 347 | in RFC 7049. More info is at http://cbor.io. This is a near-complete |
| 348 | implementation of the specification. Limitations are listed further down. |
| 349 | |
| 350 | CBOR is intentionally designed to be translatable to JSON, but not |
| 351 | all CBOR can convert to JSON. See RFC 7049 for more info on how to |
| 352 | construct CBOR that is the most JSON friendly. |
| 353 | |
| 354 | The memory model for encoding and decoding is that encoded CBOR |
| 355 | must be in a contiguous buffer in memory. During encoding the |
| 356 | caller must supply an output buffer and if the encoding would go |
| 357 | off the end of the buffer an error is returned. During decoding |
| 358 | the caller supplies the encoded CBOR in a contiguous buffer |
| 359 | and the decoder returns pointers and lengths into that buffer |
| 360 | for strings. |
| 361 | |
| 362 | This implementation does not require malloc. All data structures |
| 363 | passed in/out of the APIs can fit on the stack. |
| 364 | |
| 365 | Decoding of indefinite length strings is a special case that requires |
| 366 | a "string allocator" to allocate memory into which the segments of |
| 367 | the string are coalesced. Without this, decoding will error out if |
| 368 | an indefinite length string is encountered (indefinite length maps |
| 369 | and arrays do not require the string allocator). A simple string |
| 370 | allocator called MemPool is built-in and will work if supplied with |
| 371 | a block of memory to allocate. The string allocator can optionally |
| 372 | use malloc() or some other custom scheme. |
| 373 | |
| 374 | Here are some terms and definitions: |
| 375 | |
| 376 | - "Item", "Data Item": An integer or string or such. The basic "thing" that |
| 377 | CBOR is about. An array is an item itself that contains some items. |
| 378 | |
| 379 | - "Array": An ordered sequence of items, the same as JSON. |
| 380 | |
| 381 | - "Map": A collection of label/value pairs. Each pair is a data |
| 382 | item. A JSON "object" is the same as a CBOR "map". |
| 383 | |
| 384 | - "Label": The data item in a pair in a map that names or identifies the |
| 385 | pair, not the value. This implementation refers to it as a "label". |
| 386 | JSON refers to it as the "name". The CBOR RFC refers to it this as a "key". |
| 387 | This implementation chooses label instead because key is too easily confused |
| 388 | with a cryptographic key. The COSE standard, which uses CBOR, has also |
| 389 | chosen to use the term "label" rather than "key" for this same reason. |
| 390 | |
| 391 | - "Key": See "Label" above. |
| 392 | |
| 393 | - "Tag": Optional info that can be added before each data item. This is always |
| 394 | CBOR major type 6. |
| 395 | |
| 396 | - "Initial Byte": The first byte of an encoded item. Encoding and decoding of |
| 397 | this byte is taken care of by the implementation. |
| 398 | |
| 399 | - "Additional Info": In addition to the major type, all data items have some |
| 400 | other info. This is usually the length of the data, but can be several |
| 401 | other things. Encoding and decoding of this is taken care of by the |
| 402 | implementation. |
| 403 | |
| 404 | CBOR has two mechanisms for tagging and labeling the data |
| 405 | values like integers and strings. For example, an integer that |
| 406 | represents someone's birthday in epoch seconds since Jan 1, 1970 |
| 407 | could be encoded like this: |
| 408 | |
| 409 | - First it is CBOR_MAJOR_TYPE_POSITIVE_INT, the primitive positive |
| 410 | integer. |
| 411 | - Next it has a "tag" CBOR_TAG_DATE_EPOCH indicating the integer |
| 412 | represents a date in the form of the number of seconds since |
| 413 | Jan 1, 1970. |
| 414 | - Last it has a string "label" like "BirthDate" indicating |
| 415 | the meaning of the data. |
| 416 | |
| 417 | The encoded binary looks like this: |
| 418 | a1 # Map of 1 item |
| 419 | 69 # Indicates text string of 9 bytes |
| 420 | 426972746844617465 # The text "BirthDate" |
| 421 | c1 # Tags next int as epoch date |
| 422 | 1a # Indicates 4 byte integer |
| 423 | 580d4172 # unsigned integer date 1477263730 |
| 424 | |
| 425 | Implementors using this API will primarily work with labels. Generally |
| 426 | tags are only needed for making up new data types. This implementation |
| 427 | covers most of the data types defined in the RFC using tags. It also, |
| 428 | allows for the creation of news tags if necessary. |
| 429 | |
| 430 | This implementation explicitly supports labels that are text strings |
| 431 | and integers. Text strings translate nicely into JSON objects and |
| 432 | are very readable. Integer labels are much less readable, but |
| 433 | can be very compact. If they are in the range of -23 to |
| 434 | 23 they take up only one byte. |
| 435 | |
| 436 | CBOR allows a label to be any type of data including an array or |
| 437 | a map. It is possible to use this API to construct and |
| 438 | parse such labels, but it is not explicitly supported. |
| 439 | |
| 440 | A common encoding usage mode is to invoke the encoding twice. First |
| 441 | with no output buffer to compute the length of the needed output |
| 442 | buffer. Then the correct sized output buffer is allocated. Last the |
| 443 | encoder is invoked again, this time with the output buffer. |
| 444 | |
| 445 | The double invocation is not required if the max output buffer size |
| 446 | can be predicted. This is usually possible for simple CBOR structures. |
| 447 | If the double invocation is implemented, it can be |
| 448 | in a loop or function as in the example code so that the code doesn't |
| 449 | have to actually be written twice, saving code size. |
| 450 | |
| 451 | If a buffer too small to hold the encoded output is given, the error |
| 452 | QCBOR_ERR_BUFFER_TOO_SMALL will be returned. Data will never be |
| 453 | written off the end of the output buffer no matter which functions |
| 454 | here are called or what parameters are passed to them. |
| 455 | |
| 456 | The error handling is simple. The only possible errors are trying to |
| 457 | encode structures that are too large or too complex. There are no |
| 458 | internal malloc calls so there will be no failures for out of memory. |
| 459 | Only the final call, QCBOREncode_Finish(), returns an error code. |
| 460 | Once an error happens, the encoder goes into an error state and calls |
| 461 | to it will do nothing so the encoding can just go on. An error |
| 462 | check is not needed after every data item is added. |
| 463 | |
| 464 | Encoding generally proceeds by calling QCBOREncode_Init(), calling |
| 465 | lots of "Add" functions and calling QCBOREncode_Finish(). There |
| 466 | are many "Add" functions for various data types. The input |
| 467 | buffers need only to be valid during the "Add" calls. The |
| 468 | data is copied into the output buf during the "Add" call. |
| 469 | |
| 470 | There are three `Add` functions for each data type. The first |
| 471 | / main one for the type is for adding the data item to an array. |
| 472 | The second one's name ends in `ToMap`, is used for adding |
| 473 | data items to maps and takes a string |
| 474 | argument that is its label in the map. The third one ends in |
| 475 | `ToMapN`, is also used for adding data items to maps, and |
| 476 | takes an integer argument that is its label in the map. |
| 477 | |
| 478 | The simplest aggregate type is an array, which is a simple ordered |
| 479 | set of items without labels the same as JSON arrays. Call |
| 480 | QCBOREncode_OpenArray() to open a new array, then "Add" to |
| 481 | put items in the array and then QCBOREncode_CloseArray(). Nesting |
| 482 | to a limit is allowed. All opens must be matched by closes or an |
| 483 | encoding error will be returned. |
| 484 | |
| 485 | The other aggregate type is a map which does use labels. The |
| 486 | `Add` functions that end in `ToMap` and `ToMapN` are convenient |
| 487 | ways to add labeled data items to a map. You can also call |
| 488 | any type of `Add` function once to add a label of any time and |
| 489 | then call any type of `Add` again to add its value. |
| 490 | |
| 491 | Note that when you nest arrays or maps in a map, the nested |
| 492 | array or map has a label. |
| 493 | |
| 494 | Usually it is not necessary to add tags explicitly as most |
| 495 | tagged types have functions here, but they can be added by |
| 496 | calling QCBOREncode_AddTag(). There is an IANA registry for new tags that are |
| 497 | for broad use and standardization as per RFC 7049. It is also |
| 498 | allowed for protocols to make up new tags in the range above 256. |
| 499 | Note that even arrays and maps can be tagged. |
| 500 | |
| 501 | Summary Limits of this implementation: |
| 502 | - The entire encoded CBOR must fit into contiguous memory. |
| 503 | - Max size of encoded / decoded CBOR data is UINT32_MAX (4GB). |
| 504 | - Max array / map nesting level when encoding / decoding is |
| 505 | QCBOR_MAX_ARRAY_NESTING (this is typically 15). |
| 506 | - Max items in an array or map when encoding / decoding is |
| 507 | QCBOR_MAX_ITEMS_IN_ARRAY (typically 65,536). |
| 508 | - Does not support encoding indefinite lengths (decoding is supported). |
| 509 | - Does not directly support some tagged types: decimal fractions, big floats |
| 510 | - Does not directly support labels in maps other than text strings and ints. |
| 511 | - Does not directly support int labels greater than INT64_MAX |
| 512 | - Epoch dates limited to INT64_MAX (+/- 292 billion years) |
| 513 | - Tags on labels are ignored during decoding |
| 514 | |
| 515 | This implementation is intended to run on 32 and 64-bit CPUs. Minor |
| 516 | modifications are needed for it to work on 16-bit CPUs. |
| 517 | |
| 518 | The public interface uses size_t for all lengths. Internally the |
| 519 | implementation uses 32-bit lengths by design to use less memory and |
| 520 | fit structures on the stack. This limits the encoded |
| 521 | CBOR it can work with to size UINT32_MAX (4GB) which should be |
| 522 | enough. |
| 523 | |
| 524 | This implementation assumes two's compliment integer |
| 525 | machines. Stdint.h also requires this. It of course would be easy to |
| 526 | fix this implementation for another integer representation, but all |
| 527 | modern machines seem to be two's compliment. |
| 528 | |
| 529 | */ |
| 530 | |
| 531 | |
| 532 | /** |
| 533 | The maximum number of items in a single array or map when encoding of decoding. |
| 534 | */ |
| 535 | // -1 is because the value UINT16_MAX is used to track indefinite length arraysUINT16_MAX |
| 536 | #define QCBOR_MAX_ITEMS_IN_ARRAY (UINT16_MAX-1) |
| 537 | |
| 538 | /** |
| 539 | The maximum nesting of arrays and maps when encoding or decoding. The |
| 540 | error QCBOR_ERR_ARRAY_NESTING_TOO_DEEP will be returned on encoding |
| 541 | of decoding if it is exceeded |
| 542 | */ |
| 543 | #define QCBOR_MAX_ARRAY_NESTING QCBOR_MAX_ARRAY_NESTING1 |
| 544 | |
| 545 | /** |
| 546 | The maximum number of tags that can be in QCBORTagListIn and passed to |
| 547 | QCBORDecode_SetCallerConfiguredTagList() |
| 548 | */ |
| 549 | #define QCBOR_MAX_CUSTOM_TAGS 16 |
| 550 | |
| 551 | |
| 552 | typedef enum { |
| 553 | /** The encode or decode completely correctly. */ |
| 554 | QCBOR_SUCCESS = 0, |
| 555 | |
| 556 | /** The buffer provided for the encoded output when doing encoding was |
| 557 | too small and the encoded output will not fit. Also, when the buffer |
| 558 | given to QCBORDecode_SetMemPool() is too small. */ |
| 559 | QCBOR_ERR_BUFFER_TOO_SMALL, |
| 560 | |
| 561 | /** During encoding or decoding, the array or map nesting was deeper than |
| 562 | this implementation can handle. Note that in the interest of code size |
| 563 | and memory use, this implementation has a hard limit on array nesting. The |
| 564 | limit is defined as the constant QCBOR_MAX_ARRAY_NESTING. */ |
| 565 | QCBOR_ERR_ARRAY_NESTING_TOO_DEEP, |
| 566 | |
| 567 | /** During decoding or encoding, the array or map had too many items in it. |
| 568 | This limit QCBOR_MAX_ITEMS_IN_ARRAY, typically 65,535. */ |
| 569 | QCBOR_ERR_ARRAY_TOO_LONG, |
| 570 | |
| 571 | /** During encoding, more arrays or maps were closed than opened. This is a |
| 572 | coding error on the part of the caller of the encoder. */ |
| 573 | QCBOR_ERR_TOO_MANY_CLOSES, |
| 574 | |
| 575 | /** During decoding, some CBOR construct was encountered that this decoder |
| 576 | doesn't support, primarily this is the reserved additional info values, |
| 577 | 28 through 30. */ |
| 578 | QCBOR_ERR_UNSUPPORTED, |
| 579 | |
| 580 | /** During decoding, hit the end of the given data to decode. For example, |
| 581 | a byte string of 100 bytes was expected, but the end of the input was |
| 582 | hit before finding those 100 bytes. Corrupted CBOR input will often |
| 583 | result in this error. */ |
| 584 | QCBOR_ERR_HIT_END, |
| 585 | |
| 586 | /** During encoding, the length of the encoded CBOR exceeded UINT32_MAX. |
| 587 | */ |
| 588 | QCBOR_ERR_BUFFER_TOO_LARGE, |
| 589 | |
| 590 | /** During decoding, an integer smaller than INT64_MIN was received (CBOR |
| 591 | can represent integers smaller than INT64_MIN, but C cannot). */ |
| 592 | QCBOR_ERR_INT_OVERFLOW, |
| 593 | |
| 594 | /** During decoding, the label for a map entry is bad. What causes this |
| 595 | error depends on the decoding mode. */ |
| 596 | QCBOR_ERR_MAP_LABEL_TYPE, |
| 597 | |
| 598 | /** During encoding or decoding, the number of array or map opens was not |
| 599 | matched by the number of closes. */ |
| 600 | QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN, |
| 601 | |
| 602 | /** During encoding, the simple value is not between CBOR_SIMPLEV_FALSE |
| 603 | and CBOR_SIMPLEV_UNDEF. */ |
| 604 | QCBOR_ERR_BAD_SIMPLE, |
| 605 | |
| 606 | /** During decoding, a date greater than +- 292 billion years from Jan 1 |
| 607 | 1970 encountered during parsing. */ |
| 608 | QCBOR_ERR_DATE_OVERFLOW, |
| 609 | |
| 610 | /** During decoding, the CBOR is not valid, primarily a simple type is encoded in |
| 611 | a prohibited way. */ |
| 612 | QCBOR_ERR_INVALID_CBOR, |
| 613 | |
| 614 | /** Optional tagging that doesn't make sense (an int is tagged as a |
| 615 | date string) or can't be handled. */ |
| 616 | QCBOR_ERR_BAD_OPT_TAG, |
| 617 | |
| 618 | /** Returned by QCBORDecode_Finish() if all the inputs bytes have not |
| 619 | been consumed. */ |
| 620 | QCBOR_ERR_EXTRA_BYTES, |
| 621 | |
| 622 | /** During encoding, QCBOREncode_Close() call with a different type than |
| 623 | is currently open. */ |
| 624 | QCBOR_ERR_CLOSE_MISMATCH, |
| 625 | |
| 626 | /** Unable to decode an indefinite length string because no string |
| 627 | allocator was configured. */ |
| 628 | QCBOR_ERR_NO_STRING_ALLOCATOR, |
| 629 | |
| 630 | /** One of the chunks in an indefinite length string is not of the type of |
| 631 | the string. */ |
| 632 | QCBOR_ERR_INDEFINITE_STRING_CHUNK, |
| 633 | |
| 634 | /** Error allocating space for a string, usually for an indefinite length |
| 635 | string. */ |
| 636 | QCBOR_ERR_STRING_ALLOCATE, |
| 637 | |
| 638 | /** During decoding, a break occurred outside an indefinite length item. */ |
| 639 | QCBOR_ERR_BAD_BREAK, |
| 640 | |
| 641 | /** During decoding, too many tags in the caller-configured tag list, or not |
| 642 | enough space in QCBORTagListOut. */ |
| 643 | QCBOR_ERR_TOO_MANY_TAGS, |
| 644 | |
| 645 | /** Returned by QCBORDecode_SetMemPool() when xx is too small. This should |
| 646 | never happen on a machine with 64-bit or smaller pointers. Fixing |
| 647 | it is probably by increasing QCBOR_DECODE_MIN_MEM_POOL_SIZE. */ |
| 648 | QCBOR_ERR_MEM_POOL_INTERNAL |
| 649 | |
| 650 | } QCBORError; |
| 651 | |
| 652 | |
| 653 | typedef enum { |
| 654 | /** See QCBORDecode_Init() */ |
| 655 | QCBOR_DECODE_MODE_NORMAL = 0, |
| 656 | /** See QCBORDecode_Init() */ |
| 657 | QCBOR_DECODE_MODE_MAP_STRINGS_ONLY = 1, |
| 658 | /** See QCBORDecode_Init() */ |
| 659 | QCBOR_DECODE_MODE_MAP_AS_ARRAY = 2 |
| 660 | } QCBORDecodeMode; |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | /* Do not renumber these. Code depends on some of these values. */ |
| 667 | /** The type is unknown, unset or invalid */ |
| 668 | #define QCBOR_TYPE_NONE 0 |
| 669 | /** Type for an integer that decoded either between INT64_MIN and INT32_MIN or INT32_MAX and INT64_MAX; val.int64 */ |
| 670 | #define QCBOR_TYPE_INT64 2 |
| 671 | /** Type for an integer that decoded to a more than INT64_MAX and UINT64_MAX; val.uint64 */ |
| 672 | #define QCBOR_TYPE_UINT64 3 |
| 673 | /** Type for an array. The number of items in the array is in val.uCount. */ |
| 674 | #define QCBOR_TYPE_ARRAY 4 |
| 675 | /** Type for a map; number of items in map is in val.uCount */ |
| 676 | #define QCBOR_TYPE_MAP 5 |
| 677 | /** Type for a buffer full of bytes. Data is in val.string. */ |
| 678 | #define QCBOR_TYPE_BYTE_STRING 6 |
| 679 | /** Type for a UTF-8 string. It is not NULL terminated. Data is in val.string. */ |
| 680 | #define QCBOR_TYPE_TEXT_STRING 7 |
| 681 | /** Type for a positive big number. Data is in val.bignum, a pointer and a length. */ |
| 682 | #define QCBOR_TYPE_POSBIGNUM 9 |
| 683 | /** Type for a negative big number. Data is in val.bignum, a pointer and a length. */ |
| 684 | #define QCBOR_TYPE_NEGBIGNUM 10 |
| 685 | /** Type for RFC 3339 date string, possibly with time zone. Data is in val.dateString */ |
| 686 | #define QCBOR_TYPE_DATE_STRING 11 |
| 687 | /** Type for integer seconds since Jan 1970 + floating point fraction. Data is in val.epochDate */ |
| 688 | #define QCBOR_TYPE_DATE_EPOCH 12 |
| 689 | /** A simple type that this CBOR implementation doesn't know about; Type is in val.uSimple. */ |
| 690 | #define QCBOR_TYPE_UKNOWN_SIMPLE 13 |
| 691 | /** Type for the simple value false; nothing more; nothing in val union. */ |
| 692 | #define QCBOR_TYPE_FALSE 20 |
| 693 | /** Type for the simple value true; nothing more; nothing in val union. */ |
| 694 | #define QCBOR_TYPE_TRUE 21 |
| 695 | /** Type for the simple value null; nothing more; nothing in val union. */ |
| 696 | #define QCBOR_TYPE_NULL 22 |
| 697 | /** Type for the simple value undef; nothing more; nothing in val union. */ |
| 698 | #define QCBOR_TYPE_UNDEF 23 |
| 699 | /** Type for a floating point number. Data is in val.float. */ |
| 700 | #define QCBOR_TYPE_FLOAT 26 |
| 701 | /** Type for a double floating point number. Data is in val.double. */ |
| 702 | #define QCBOR_TYPE_DOUBLE 27 |
| 703 | /** For QCBOR_DECODE_MODE_MAP_AS_ARRAY decode mode, a map that is being traversed as an array. See QCBORDecode_Init() */ |
| 704 | #define QCBOR_TYPE_MAP_AS_ARRAY 32 |
| 705 | |
| 706 | #define QCBOR_TYPE_BREAK 31 // Used internally; never returned |
| 707 | |
| 708 | #define QCBOR_TYPE_OPTTAG 254 // Used internally; never returned |
| 709 | |
| 710 | |
| 711 | |
| 712 | /* |
| 713 | Approx Size of this: |
| 714 | 8 + 8 + 1 + 1 + 1 + (1 padding) + (4 padding on 64-bit machine) = 24 for first part (20 on a 32-bit machine) |
| 715 | 16 bytes for the val union |
| 716 | 16 bytes for label union |
| 717 | total = 56 bytes (52 bytes on 32-bit machine) |
| 718 | */ |
| 719 | |
| 720 | /** |
| 721 | QCBORItem holds the type, value and other info for a decoded item returned by GetNextItem(). |
| 722 | */ |
| 723 | typedef struct _QCBORItem { |
| 724 | uint8_t uDataType; /** Tells what element of the val union to use. One of QCBOR_TYPE_XXXX */ |
| 725 | uint8_t uNestingLevel; /** How deep the nesting from arrays and maps are. 0 is the top level with no arrays or maps entered */ |
| 726 | uint8_t uLabelType; /** Tells what element of the label union to use */ |
| 727 | uint8_t uDataAlloc; /** 1 if allocated with string allocator, 0 if not. See QCBORDecode_MakeMallocStringAllocator() */ |
| 728 | uint8_t uLabelAlloc; /** Like uDataAlloc, but for label */ |
| 729 | uint8_t uNextNestLevel; /** If not equal to uNestingLevel, this item closed out at least one map/array */ |
| 730 | |
| 731 | union { |
| 732 | int64_t int64; /** The value for uDataType QCBOR_TYPE_INT64 */ |
| 733 | uint64_t uint64; /** The value for uDataType QCBOR_TYPE_UINT64 */ |
| 734 | |
| 735 | UsefulBufC string; /** The value for uDataType QCBOR_TYPE_BYTE_STRING and QCBOR_TYPE_TEXT_STRING */ |
| 736 | uint16_t uCount; /** The "value" for uDataType QCBOR_TYPE_ARRAY or QCBOR_TYPE_MAP -- the number of items in the array or map |
| 737 | UINT16_MAX when decoding indefinite lengths maps and arrays. */ |
| 738 | double dfnum; /** The value for uDataType QCBOR_TYPE_DOUBLE */ |
| 739 | struct { |
| 740 | int64_t nSeconds; |
| 741 | double fSecondsFraction; |
| 742 | } epochDate; /** The value for uDataType QCBOR_TYPE_DATE_EPOCH */ |
| 743 | UsefulBufC dateString; /** The value for uDataType QCBOR_TYPE_DATE_STRING */ |
| 744 | UsefulBufC bigNum; /** The value for uDataType QCBOR_TYPE_BIGNUM */ |
| 745 | uint8_t uSimple; /** The integer value for unknown simple types */ |
| 746 | uint64_t uTagV; |
| 747 | |
| 748 | } val; /** The union holding the item's value. Select union member based on uDataType */ |
| 749 | |
| 750 | union { |
| 751 | UsefulBufC string; /** The label for uLabelType QCBOR_TYPE_BYTE_STRING and QCBOR_TYPE_TEXT_STRING */ |
| 752 | int64_t int64; /** The label for uLabelType for QCBOR_TYPE_INT64 */ |
| 753 | uint64_t uint64; /** The label for uLabelType for QCBOR_TYPE_UINT64 */ |
| 754 | } label; /** Union holding the different label types selected based on uLabelType */ |
| 755 | |
| 756 | uint64_t uTagBits; /** Bit indicating which tags (major type 6) on this item. */ |
| 757 | |
| 758 | } QCBORItem; |
| 759 | |
| 760 | |
| 761 | /** |
| 762 | This is a set of functions and pointer context (in object-oriented parlance, |
| 763 | an "object") used to allocate memory for coalescing the segments of an indefinite |
| 764 | length string into one. |
| 765 | |
| 766 | The fAllocate function works as an initial allocator and a reallocator to |
| 767 | expand the string for each new segment. When it is an initial allocator |
| 768 | pOldMem is NULL. |
| 769 | |
| 770 | The fFree function is called to clean up an individual allocation when an error occurs. |
| 771 | |
| 772 | The fDesctructor function is called when QCBORDecode_Finish is called. |
| 773 | |
| 774 | Any memory allocated with this will be marked by setting uDataAlloc |
| 775 | or uLabelAlloc in the QCBORItem structure so the caller knows they |
| 776 | have to free it. |
| 777 | |
| 778 | fAllocate is only ever called to increase the single most recent |
| 779 | allocation made, making implementation of a memory pool very simple. |
| 780 | |
| 781 | fFree is also only called on the single most recent allocation. |
| 782 | */ |
| 783 | typedef struct { |
| 784 | void *pAllocaterContext; |
| 785 | UsefulBuf (*fAllocate)(void *pAllocaterContext, void *pOldMem, size_t uNewSize); |
| 786 | void (*fFree)(void *pAllocaterContext, void *pMem); |
| 787 | void (*fDestructor)(void *pAllocaterContext); |
| 788 | } QCBORStringAllocator; |
| 789 | |
| 790 | |
| 791 | /** |
| 792 | This only matters if you use a string allocator |
| 793 | and and set it up with QCBORDecode_SetMemPool(). It is |
| 794 | the size of the overhead needed needed by |
| 795 | QCBORDecode_SetMemPool(). If you write your own |
| 796 | string allocator or use the separately available malloc |
| 797 | based string allocator, this size will not apply |
| 798 | */ |
| 799 | #define QCBOR_DECODE_MIN_MEM_POOL_SIZE 72 |
| 800 | |
| 801 | |
| 802 | /** |
| 803 | This is used to tell the decoder about tags that it should |
| 804 | record in uTagBits in QCBORItem beyond the built-in |
| 805 | tags. puTags points to an |
| 806 | array of uint64_t integers that are the tags. uNumTags |
| 807 | is the number of integers in the array. The maximum |
| 808 | size is QCBOR_MAX_CUSTOM_TAGS. See QCBORDecode_IsTagged() |
| 809 | and QCBORDecode_SetCallerAddedTagMap(). |
| 810 | */ |
| 811 | typedef struct { |
| 812 | uint8_t uNumTags; |
| 813 | const uint64_t *puTags; |
| 814 | } QCBORTagListIn; |
| 815 | |
| 816 | |
| 817 | /** |
| 818 | This is for QCBORDecode_GetNextWithTags() to be able to return the |
| 819 | full list of tags on an item. It not needed for most CBOR protocol |
| 820 | implementations. Its primary use is for pretty-printing CBOR or |
| 821 | protocol conversion to another format. |
| 822 | |
| 823 | On input, puTags points to a buffer to be filled in |
| 824 | and uNumAllocated is the number of uint64_t values |
| 825 | in the buffer. |
| 826 | |
| 827 | On output the buffer contains the tags for the item. |
| 828 | uNumUsed tells how many there are. |
| 829 | */ |
| 830 | typedef struct { |
| 831 | uint8_t uNumUsed; |
| 832 | uint8_t uNumAllocated; |
| 833 | uint64_t *puTags; |
| 834 | } QCBORTagListOut; |
| 835 | |
| 836 | |
| 837 | /** |
| 838 | QCBOREncodeContext is the data type that holds context for all the |
| 839 | encoding functions. It is less than 200 bytes, so it can go on |
| 840 | the stack. The contents are opaque, and the caller should not access |
| 841 | any internal items. A context may be re used serially as long as |
| 842 | it is re initialized. |
| 843 | */ |
| 844 | typedef struct _QCBOREncodeContext QCBOREncodeContext; |
| 845 | |
| 846 | |
| 847 | /** |
| 848 | Initialize the the encoder to prepare to encode some CBOR. |
| 849 | |
| 850 | @param[in,out] pCtx The encoder context to initialize. |
| 851 | @param[in] Storage The buffer into which this encoded result will be placed. |
| 852 | |
| 853 | Call this once at the start of an encoding of a CBOR structure. Then |
| 854 | call the various QCBOREncode_AddXXX() functions to add the data |
| 855 | items. Then call QCBOREncode_Finish(). |
| 856 | |
| 857 | The maximum output buffer is UINT32_MAX (4GB). This is not a practical |
| 858 | limit in any way and reduces the memory needed by the implementation. |
| 859 | The error QCBOR_ERR_BUFFER_TOO_LARGE will be returned by QCBOR_Finish() |
| 860 | if a larger buffer length is passed in. |
| 861 | |
| 862 | If this is called with pBuf as NULL and uBufLen a large value like |
| 863 | UINT32_MAX, all the QCBOREncode_AddXXXX() functions and |
| 864 | QCBORE_Encode_Finish() can still be called. No data will be encoded, |
| 865 | but the length of what would be encoded will be calculated. The |
| 866 | length of the encoded structure will be handed back in the call to |
| 867 | QCBOREncode_Finish(). You can then allocate a buffer of that size |
| 868 | and call all the encoding again, this time to fill in the buffer. |
| 869 | |
| 870 | A QCBORContext can be reused over and over as long as |
| 871 | QCBOREncode_Init() is called. |
| 872 | */ |
| 873 | void QCBOREncode_Init(QCBOREncodeContext *pCtx, UsefulBuf Storage); |
| 874 | |
| 875 | |
| 876 | /** |
| 877 | @brief Add a signed 64-bit integer to the encoded output. |
| 878 | |
| 879 | @param[in] pCtx The encoding context to add the integer to. |
| 880 | @param[in] nNum The integer to add. |
| 881 | |
| 882 | The integer will be encoded and added to the CBOR output. |
| 883 | |
| 884 | This function figures out the size and the sign and encodes in the |
| 885 | correct minimal CBOR. Specifically, it will select CBOR major type 0 or 1 |
| 886 | based on sign and will encode to 1, 2, 4 or 8 bytes depending on the |
| 887 | value of the integer. Values less than 24 effectively encode to one |
| 888 | byte because they are encoded in with the CBOR major type. This is |
| 889 | a neat and efficient characteristic of CBOR that can be taken |
| 890 | advantage of when designing CBOR-based protocols. If integers like |
| 891 | tags can be kept between -23 and 23 they will be encoded in one byte |
| 892 | including the major type. |
| 893 | |
| 894 | If you pass a smaller int, say an int16_t or a small value, say 100, |
| 895 | the encoding will still be CBOR's most compact that can represent the |
| 896 | value. For example, CBOR always encodes the value 0 as one byte, |
| 897 | 0x00. The representation as 0x00 includes identification of the type |
| 898 | as an integer too as the major type for an integer is 0. See RFC 7049 |
| 899 | Appendix A for more examples of CBOR encoding. This compact encoding |
| 900 | is also canonical CBOR as per section 3.9 in RFC 7049. |
| 901 | |
| 902 | There are no functions to add int16_t or int32_t because they are |
| 903 | not necessary because this always encodes to the smallest number |
| 904 | of bytes based on the value (If this code is running on a 32-bit |
| 905 | machine having a way to add 32-bit integers would reduce code size some). |
| 906 | |
| 907 | If the encoding context is in an error state, this will do |
| 908 | nothing. If an error occurs when adding this integer, the internal |
| 909 | error flag will be set, and the error will be returned when |
| 910 | QCBOREncode_Finish() is called. |
| 911 | |
| 912 | See also QCBOREncode_AddUInt64(). |
| 913 | */ |
| 914 | void QCBOREncode_AddInt64(QCBOREncodeContext *pCtx, int64_t nNum); |
| 915 | |
| 916 | static void QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t uNum); |
| 917 | |
| 918 | static void QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum); |
| 919 | |
| 920 | |
| 921 | /** |
| 922 | @brief Add an unsigned 64-bit integer to the encoded output. |
| 923 | |
| 924 | @param[in] pCtx The encoding context to add the integer to. |
| 925 | @param[in] uNum The integer to add. |
| 926 | |
| 927 | The integer will be encoded and added to the CBOR output. |
| 928 | |
| 929 | The only reason so use this function is for integers larger than |
| 930 | INT64_MAX and smaller than UINT64_MAX. Otherwise QCBOREncode_AddInt64() |
| 931 | will work fine. |
| 932 | |
| 933 | Error handling is the same as for QCBOREncode_AddInt64(). |
| 934 | */ |
| 935 | void QCBOREncode_AddUInt64(QCBOREncodeContext *pCtx, uint64_t uNum); |
| 936 | |
| 937 | static void QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum); |
| 938 | |
| 939 | static void QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum); |
| 940 | |
| 941 | |
| 942 | /** |
| 943 | |
| 944 | @brief Add a UTF-8 text string to the encoded output |
| 945 | |
| 946 | @param[in] pCtx The context to initialize. |
| 947 | @param[in] Text Pointer and length of text to add. |
| 948 | |
| 949 | The text passed in must be unencoded UTF-8 according to RFC |
| 950 | 3629. There is no NULL termination. The text is added as CBOR |
| 951 | major type 3. |
| 952 | |
| 953 | If called with nBytesLen equal to 0, an empty string will be |
| 954 | added. When nBytesLen is 0, pBytes may be NULL. |
| 955 | |
| 956 | Note that the restriction of the buffer length to an uint32_t is |
| 957 | entirely intentional as this encoder is not capable of encoding |
| 958 | lengths greater. This limit to 4GB for a text string should not be a |
| 959 | problem. |
| 960 | |
| 961 | Error handling is the same as QCBOREncode_AddInt64(). |
| 962 | */ |
| 963 | static void QCBOREncode_AddText(QCBOREncodeContext *pCtx, UsefulBufC Text); |
| 964 | |
| 965 | static void QCBOREncode_AddTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Text); |
| 966 | |
| 967 | static void QCBOREncode_AddTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Text); |
| 968 | |
| 969 | |
| 970 | /** |
| 971 | @brief Add a UTF-8 text string to the encoded output |
| 972 | |
| 973 | @param[in] pCtx The context to initialize. |
| 974 | @param[in] szString Null-terminated text to add. |
| 975 | |
| 976 | This works the same as QCBOREncode_AddText(). |
| 977 | */ |
| 978 | static void QCBOREncode_AddSZString(QCBOREncodeContext *pCtx, const char *szString); |
| 979 | |
| 980 | static void QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szString); |
| 981 | |
| 982 | static void QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szString); |
| 983 | |
| 984 | |
| 985 | /** |
| 986 | @brief Add a floating-point number to the encoded output |
| 987 | |
| 988 | @param[in] pCtx The encoding context to add the float to. |
| 989 | @param[in] dNum The double precision number to add. |
| 990 | |
| 991 | This outputs a floating-point number with CBOR major type 7. |
| 992 | |
| 993 | This will selectively encode the double-precision floating point |
| 994 | number as either double-precision, single-precision or |
| 995 | half-precision. It will always encode infinity, NaN and 0 has half |
| 996 | precision. If no precision will be lost in the conversion to |
| 997 | half-precision then it will be converted and encoded. If not and no |
| 998 | precision will be lost in conversion to single-precision, then it |
| 999 | will be converted and encoded. If not, then no conversion is |
| 1000 | performed, and it encoded as a double. |
| 1001 | |
| 1002 | Half-precision floating point numbers take up 2 bytes, half that of |
| 1003 | single-precision, one quarter of double-precision |
| 1004 | |
| 1005 | This automatically reduces the size of encoded messages a lot, maybe |
| 1006 | even by four if most of values are 0, infinity or NaN. |
| 1007 | |
| 1008 | On decode, these will always be returned as a double. |
| 1009 | |
| 1010 | Error handling is the same as QCBOREncode_AddInt64(). |
| 1011 | */ |
| 1012 | void QCBOREncode_AddDouble(QCBOREncodeContext *pCtx, double dNum); |
| 1013 | |
| 1014 | static void QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum); |
| 1015 | |
| 1016 | static void QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum); |
| 1017 | |
| 1018 | |
| 1019 | /** |
| 1020 | @brief[in] Add an optional tag |
| 1021 | |
| 1022 | @param[in] pCtx The encoding context to add the integer to. |
| 1023 | @param[in] uTag The tag to add |
| 1024 | |
| 1025 | This outputs a CBOR major type 6 optional tag. |
| 1026 | |
| 1027 | The tag is applied to the next data item added to the encoded |
| 1028 | output. That data item that is to be tagged can be of any major |
| 1029 | CBOR type. Any number of tags can be added to a data item by calling |
| 1030 | this multiple times before the data item is added. |
| 1031 | |
| 1032 | For many of the common standard tags a function to encode |
| 1033 | data using it already exists and this is not needed. For example, |
| 1034 | QCBOREncode_AddDateEpoch() already exists to output |
| 1035 | integers representing dates with the right tag. |
| 1036 | */ |
| 1037 | void QCBOREncode_AddTag(QCBOREncodeContext *pCtx,uint64_t uTag); |
| 1038 | |
| 1039 | |
| 1040 | /** |
| 1041 | @brief Add an epoch-based date |
| 1042 | |
| 1043 | @param[in] pCtx The encoding context to add the simple value to. |
| 1044 | @param[in] date Number of seconds since 1970-01-01T00:00Z in UTC time. |
| 1045 | |
| 1046 | As per RFC 7049 this is similar to UNIX/Linux/POSIX dates. This is |
| 1047 | the most compact way to specify a date and time in CBOR. Note that this |
| 1048 | is always UTC and does not include the time zone. Use |
| 1049 | QCBOREncode_AddDateString() if you want to include the time zone. |
| 1050 | |
| 1051 | The integer encoding rules apply here so the date will be encoded in a |
| 1052 | minimal number of 1, 2 4 or 8 bytes. Until about the year 2106 these |
| 1053 | dates should encode in 6 bytes -- one byte for the tag, one byte for the type |
| 1054 | and 4 bytes for the integer. |
| 1055 | |
| 1056 | If you care about leap-seconds and that level of accuracy, make sure the |
| 1057 | system you are running this code on does it correctly. This code just takes |
| 1058 | the value passed in. |
| 1059 | |
| 1060 | This implementation cannot encode fractional seconds using float or double |
| 1061 | even though that is allowed by CBOR, but you can encode them if you |
| 1062 | want to by calling QCBOREncode_AddDouble() |
| 1063 | with the right parameters. |
| 1064 | |
| 1065 | Error handling is the same as QCBOREncode_AddInt64(). |
| 1066 | */ |
| 1067 | static void QCBOREncode_AddDateEpoch(QCBOREncodeContext *pCtx, int64_t date); |
| 1068 | |
| 1069 | static void QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t date); |
| 1070 | |
| 1071 | static void QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t date); |
| 1072 | |
| 1073 | |
| 1074 | /** |
| 1075 | @brief Add a byte string to the encoded output. |
| 1076 | |
| 1077 | @param[in] pCtx The context to initialize. |
| 1078 | @param[in] Bytes Pointer and length of the input data. |
| 1079 | |
| 1080 | Simply adds the bytes to the encoded output as CBOR major type 2. |
| 1081 | |
| 1082 | If called with Bytes.len equal to 0, an empty string will be |
| 1083 | added. When Bytes.len is 0, Bytes.ptr may be NULL. |
| 1084 | |
| 1085 | Error handling is the same as QCBOREncode_AddInt64(). |
| 1086 | */ |
| 1087 | static void QCBOREncode_AddBytes(QCBOREncodeContext *pCtx, UsefulBufC Bytes); |
| 1088 | |
| 1089 | static void QCBOREncode_AddBytesToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes); |
| 1090 | |
| 1091 | static void QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes); |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | /** |
| 1096 | @brief Add a binary UUID to the encoded output. |
| 1097 | |
| 1098 | @param[in] pCtx The context to initialize. |
| 1099 | @param[in] Bytes Pointer and length of the binary UUID. |
| 1100 | |
| 1101 | A binary UUID as defined in RFC 4122 is added to the ouput. |
| 1102 | |
| 1103 | It is output as CBOR major type 2, a binary string, with |
| 1104 | optional tag 36 indicating the binary string is a UUID. |
| 1105 | */ |
| 1106 | static void QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pCtx, UsefulBufC Bytes); |
| 1107 | |
| 1108 | static void QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes); |
| 1109 | |
| 1110 | static void QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes); |
| 1111 | |
| 1112 | |
| 1113 | /** |
| 1114 | @brief Add a positive big number to the encoded output. |
| 1115 | |
| 1116 | @param[in] pCtx The context to initialize. |
| 1117 | @param[in] Bytes Pointer and length of the big number. |
| 1118 | |
| 1119 | Big numbers are integers larger than 64-bits. Their format |
| 1120 | is described in RFC 7049. |
| 1121 | |
| 1122 | It is output as CBOR major type 2, a binary string, with |
| 1123 | optional tag 2 indicating the binary string is a positive big |
| 1124 | number. |
| 1125 | |
| 1126 | Often big numbers are used to represent cryptographic keys, |
| 1127 | however, COSE which defines representations for keys chose not |
| 1128 | to use this particular type. |
| 1129 | */ |
| 1130 | static void QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx, UsefulBufC Bytes); |
| 1131 | |
| 1132 | static void QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes); |
| 1133 | |
| 1134 | static void QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes); |
| 1135 | |
| 1136 | |
| 1137 | /** |
| 1138 | @brief Add a negative big number to the encoded output. |
| 1139 | |
| 1140 | @param[in] pCtx The context to initialize. |
| 1141 | @param[in] Bytes Pointer and length of the big number. |
| 1142 | |
| 1143 | Big numbers are integers larger than 64-bits. Their format |
| 1144 | is described in RFC 7049. |
| 1145 | |
| 1146 | It is output as CBOR major type 2, a binary string, with |
| 1147 | optional tag 2 indicating the binary string is a negative big |
| 1148 | number. |
| 1149 | |
| 1150 | Often big numbers are used to represent cryptographic keys, |
| 1151 | however, COSE which defines representations for keys chose not |
| 1152 | to use this particular type. |
| 1153 | */ |
| 1154 | static void QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx, UsefulBufC Bytes); |
| 1155 | |
| 1156 | static void QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes); |
| 1157 | |
| 1158 | static void QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes); |
| 1159 | |
| 1160 | |
| 1161 | /** |
| 1162 | @brief Add a text URI to the encoded output. |
| 1163 | |
| 1164 | @param[in] pCtx The context to initialize. |
| 1165 | @param[in] URI Pointer and length of the URI. |
| 1166 | |
| 1167 | The format of URI is RFC 3986. |
| 1168 | |
| 1169 | It is output as CBOR major type 3, a text string, with |
| 1170 | optional tag 32 indicating the text string is a URI. |
| 1171 | */ |
| 1172 | static void QCBOREncode_AddURI(QCBOREncodeContext *pCtx, UsefulBufC URI); |
| 1173 | |
| 1174 | static void QCBOREncode_AddURIToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC URI); |
| 1175 | |
| 1176 | static void QCBOREncode_AddURIToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC URI); |
| 1177 | |
| 1178 | |
| 1179 | /** |
| 1180 | @brief Add base 64-encoded text to encoded output. |
| 1181 | |
| 1182 | @param[in] pCtx The context to initialize. |
| 1183 | @param[in] B64Text Pointer and length of the base-64 encoded text. |
| 1184 | |
| 1185 | The text content is base 64 encoded data per RFC 4648. |
| 1186 | |
| 1187 | It is output as CBOR major type 3, a text string, with |
| 1188 | optional tag 34 indicating the text string is a URI. |
| 1189 | */ |
| 1190 | static void QCBOREncode_AddB64Text(QCBOREncodeContext *pCtx, UsefulBufC B64Text); |
| 1191 | |
| 1192 | static void QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC B64Text); |
| 1193 | |
| 1194 | static void QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC B64Text); |
| 1195 | |
| 1196 | |
| 1197 | /** |
| 1198 | @brief Add base 64URL -encoded URL to encoded output. |
| 1199 | |
| 1200 | @param[in] pCtx The context to initialize. |
| 1201 | @param[in] B64Text Pointer and length of the base-64 encoded text. |
| 1202 | |
| 1203 | The text content is base 64 URL format encoded text as per RFC 4648. |
| 1204 | |
| 1205 | It is output as CBOR major type 3, a text string, with |
| 1206 | optional tag 33 indicating the text string is a URI. |
| 1207 | */ |
| 1208 | static void QCBOREncode_AddB64URLText(QCBOREncodeContext *pCtx, UsefulBufC B64Text); |
| 1209 | |
| 1210 | static void QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC B64Text); |
| 1211 | |
| 1212 | static void QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC B64Text); |
| 1213 | |
| 1214 | |
| 1215 | /** |
| 1216 | @brief Add Perl Compatible Regular Expression |
| 1217 | |
| 1218 | @param[in] pCtx The context to initialize. |
| 1219 | @param[in] Regex Pointer and length of the regular expression. |
| 1220 | |
| 1221 | The text content is Perl Compatible Regular |
| 1222 | Expressions (PCRE) / JavaScript syntax [ECMA262]. |
| 1223 | |
| 1224 | It is output as CBOR major type 3, a text string, with |
| 1225 | optional tag 35 indicating the text string is a regular expression. |
| 1226 | */ |
| 1227 | static void QCBOREncode_AddRegex(QCBOREncodeContext *pCtx, UsefulBufC Regex); |
| 1228 | |
| 1229 | static void QCBOREncode_AddRegexToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Regex); |
| 1230 | |
| 1231 | static void QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Regex); |
| 1232 | |
| 1233 | |
| 1234 | /** |
| 1235 | @brief MIME encoded text to the encoded output. |
| 1236 | |
| 1237 | @param[in] pCtx The context to initialize. |
| 1238 | @param[in] MIMEData Pointer and length of the regular expression. |
| 1239 | |
| 1240 | The text content is in MIME format per RFC 2045 including the headers. |
| 1241 | |
| 1242 | It is output as CBOR major type 3, a text string, with |
| 1243 | optional tag 36 indicating the text string is MIME data. |
| 1244 | */ |
| 1245 | static void QCBOREncode_AddMIMEData(QCBOREncodeContext *pCtx, UsefulBufC MIMEData); |
| 1246 | |
| 1247 | static void QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC MIMEData); |
| 1248 | |
| 1249 | static void QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC MIMEData); |
| 1250 | |
| 1251 | |
| 1252 | /** |
| 1253 | @brief Add an RFC 3339 date string |
| 1254 | |
| 1255 | @param[in] pCtx The encoding context to add the simple value to. |
| 1256 | @param[in] szDate Null-terminated string with date to add |
| 1257 | |
| 1258 | The string szDate should be in the form of RFC 3339 as defined by section |
| 1259 | 3.3 in RFC 4287. This is as described in section 2.4.1 in RFC 7049. |
| 1260 | |
| 1261 | Note that this function doesn't validate the format of the date string |
| 1262 | at all. If you add an incorrect format date string, the generated |
| 1263 | CBOR will be incorrect and the receiver may not be able to handle it. |
| 1264 | |
| 1265 | Error handling is the same as QCBOREncode_AddInt64(). |
| 1266 | */ |
| 1267 | static void QCBOREncode_AddDateString(QCBOREncodeContext *pCtx, const char *szDate); |
| 1268 | |
| 1269 | static void QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szDate); |
| 1270 | |
| 1271 | static void QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szDate); |
| 1272 | |
| 1273 | |
| 1274 | /** |
| 1275 | @brief Add a standard boolean. |
| 1276 | |
| 1277 | @param[in] pCtx The encoding context to add the simple value to. |
| 1278 | @param[in] b true or false from stdbool. Anything will result in an error. |
| 1279 | |
| 1280 | Adds a boolean value as CBOR major type 7. |
| 1281 | |
| 1282 | Error handling is the same as QCBOREncode_AddInt64(). |
| 1283 | */ |
| 1284 | static void QCBOREncode_AddBool(QCBOREncodeContext *pCtx, bool b); |
| 1285 | |
| 1286 | static void QCBOREncode_AddBoolToMap(QCBOREncodeContext *pCtx, const char *szLabel, bool b); |
| 1287 | |
| 1288 | static void QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, bool b); |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | /** |
| 1293 | @brief Add a NULL to the encoded output. |
| 1294 | |
| 1295 | @param[in] pCtx The encoding context to add the simple value to. |
| 1296 | |
| 1297 | Adds the NULL value as CBOR major type 7. |
| 1298 | |
| 1299 | This NULL doesn't have any special meaning in CBOR such as a terminating |
| 1300 | value for a string or an empty value. |
| 1301 | |
| 1302 | Error handling is the same as QCBOREncode_AddInt64(). |
| 1303 | */ |
| 1304 | static void QCBOREncode_AddNULL(QCBOREncodeContext *pCtx); |
| 1305 | |
| 1306 | static void QCBOREncode_AddNULLToMap(QCBOREncodeContext *pCtx, const char *szLabel); |
| 1307 | |
| 1308 | static void QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
| 1309 | |
| 1310 | |
| 1311 | /** |
| 1312 | @brief Add an "undef" to the encoded output. |
| 1313 | |
| 1314 | @param[in] pCtx The encoding context to add the simple value to. |
| 1315 | |
| 1316 | Adds the undef value as CBOR major type 7. |
| 1317 | |
| 1318 | Note that this value will not translate to JSON. |
| 1319 | |
| 1320 | This Undef doesn't have any special meaning in CBOR such as a terminating |
| 1321 | value for a string or an empty value. |
| 1322 | |
| 1323 | Error handling is the same as QCBOREncode_AddInt64(). |
| 1324 | */ |
| 1325 | static void QCBOREncode_AddUndef(QCBOREncodeContext *pCtx); |
| 1326 | |
| 1327 | static void QCBOREncode_AddUndefToMap(QCBOREncodeContext *pCtx, const char *szLabel); |
| 1328 | |
| 1329 | static void QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
| 1330 | |
| 1331 | |
| 1332 | /** |
| 1333 | @brief Indicates that the next items added are in an array. |
| 1334 | |
| 1335 | @param[in] pCtx The encoding context to open the array in. |
| 1336 | |
| 1337 | Arrays are the basic CBOR aggregate or structure type. Call this |
| 1338 | function to start or open an array. Then call the various AddXXX |
| 1339 | functions to add the items that go into the array. Then call |
| 1340 | QCBOREncode_CloseArray() when all items have been added. The data |
| 1341 | items in the array can be of any type and can be of mixed types. |
| 1342 | |
| 1343 | Nesting of arrays and maps is allowed and supported just by calling |
| 1344 | QCBOREncode_OpenArray() again before calling CloseArray. While CBOR |
| 1345 | has no limit on nesting, this implementation does in order to keep it |
| 1346 | smaller and simpler. The limit is QCBOR_MAX_ARRAY_NESTING. This is |
| 1347 | the max number of times this can be called without calling |
| 1348 | QCBOREncode_CloseArray(). QCBOREncode_Finish() will return |
| 1349 | QCBOR_ERR_ARRAY_NESTING_TOO_DEEP when it is called as this function |
| 1350 | just sets an error state and returns no value when this occurs. |
| 1351 | |
| 1352 | If you try to add more than QCBOR_MAX_ITEMS_IN_ARRAY items to a |
| 1353 | single array or map, QCBOR_ERR_ARRAY_TOO_LONG will be returned when |
| 1354 | QCBOREncode_Finish() is called. |
| 1355 | |
| 1356 | An array itself must have a label if it is being added to a map. |
| 1357 | Note that array elements do not have labels (but map elements do). |
| 1358 | |
| 1359 | An array itself may be tagged. |
| 1360 | */ |
| 1361 | static void QCBOREncode_OpenArray(QCBOREncodeContext *pCtx); |
| 1362 | |
| 1363 | static void QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pCtx, const char *szLabel); |
| 1364 | |
| 1365 | static void QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
| 1366 | |
| 1367 | |
| 1368 | /** |
| 1369 | @brief Close an open array. |
| 1370 | |
| 1371 | @param[in] pCtx The context to add to. |
| 1372 | |
| 1373 | The closes an array opened by QCBOREncode_OpenArray(). It reduces |
| 1374 | nesting level by one. All arrays (and maps) must be closed before |
| 1375 | calling QCBOREncode_Finish(). |
| 1376 | |
| 1377 | When an error occurs as a result of this call, the encoder records |
| 1378 | the error and enters the error state. The error will be returned when |
| 1379 | QCBOREncode_Finish() is called. |
| 1380 | |
| 1381 | If this has been called more times than QCBOREncode_OpenArray(), then |
| 1382 | QCBOR_ERR_TOO_MANY_CLOSES will be returned when QCBOREncode_Finish() |
| 1383 | is called. |
| 1384 | |
| 1385 | If this is called and it is not an array that is currently open, |
| 1386 | QCBOR_ERR_CLOSE_MISMATCH will be returned when QCBOREncode_Finish() |
| 1387 | is called. |
| 1388 | */ |
| 1389 | static void QCBOREncode_CloseArray(QCBOREncodeContext *pCtx); |
| 1390 | |
| 1391 | |
| 1392 | /** |
| 1393 | @brief Indicates that the next items added are in a map. |
| 1394 | |
| 1395 | @param[in] pCtx The context to add to. |
| 1396 | |
| 1397 | See QCBOREncode_OpenArray() for more information, particularly error |
| 1398 | handling. |
| 1399 | |
| 1400 | CBOR maps are an aggregate type where each item in the map consists |
| 1401 | of a label and a value. They are similar to JSON objects. |
| 1402 | |
| 1403 | The value can be any CBOR type including another map. |
| 1404 | |
| 1405 | The label can also be any CBOR type, but in practice they are |
| 1406 | typically, integers as this gives the most compact output. They might |
| 1407 | also be text strings which gives readability and translation to JSON. |
| 1408 | |
| 1409 | Every QCBOREncode_AddXXX() call has once version that is "InMap" for |
| 1410 | adding items to maps with string labels and on that is "InMapN" that |
| 1411 | is for adding with integer labels. |
| 1412 | |
| 1413 | RFC 7049 uses the term "key" instead of "label". |
| 1414 | |
| 1415 | If you wish to use map labels that are neither integer labels or |
| 1416 | text strings, then just call the QCBOREncode_AddXXX() function |
| 1417 | explicitly to add the label. Then call it again to add the value. |
| 1418 | |
| 1419 | See the RFC7049 for a lot more information on creating maps. |
| 1420 | */ |
| 1421 | static void QCBOREncode_OpenMap(QCBOREncodeContext *pCtx); |
| 1422 | |
| 1423 | static void QCBOREncode_OpenMapInMap(QCBOREncodeContext *pCtx, const char *szLabel); |
| 1424 | |
| 1425 | static void QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
| 1426 | |
| 1427 | |
| 1428 | |
| 1429 | /** |
| 1430 | @brief Close an open map. |
| 1431 | |
| 1432 | @param[in] pCtx The context to add to. |
| 1433 | |
| 1434 | The closes a map opened by QCBOREncode_OpenMap(). It reduces nesting |
| 1435 | level by one. |
| 1436 | |
| 1437 | When an error occurs as a result of this call, the encoder records |
| 1438 | the error and enters the error state. The error will be returned when |
| 1439 | QCBOREncode_Finish() is called. |
| 1440 | |
| 1441 | If this has been called more times than QCBOREncode_OpenMap(), |
| 1442 | then QCBOR_ERR_TOO_MANY_CLOSES will be returned when |
| 1443 | QCBOREncode_Finish() is called. |
| 1444 | |
| 1445 | If this is called and it is not a map that is currently |
| 1446 | open, QCBOR_ERR_CLOSE_MISMATCH will be returned when QCBOREncode_Finish() |
| 1447 | is called. |
| 1448 | */ |
| 1449 | static void QCBOREncode_CloseMap(QCBOREncodeContext *pCtx); |
| 1450 | |
| 1451 | |
| 1452 | /** |
| 1453 | @brief Indicate start of encoded CBOR to be wrapped in a bstr. |
| 1454 | |
| 1455 | @param[in] pCtx The context to add to. |
| 1456 | |
| 1457 | All added encoded items between this call and a call to |
| 1458 | QCBOREncode_CloseBstrWrap() will be wrapped in a bstr. They will |
| 1459 | appear in the final output as a byte string. That byte string will |
| 1460 | contain encoded CBOR. |
| 1461 | |
| 1462 | The typical use case is for encoded CBOR that is to be |
| 1463 | cryptographically hashed, as part of a COSE (RFC 8152) |
| 1464 | implementation. This avoids having to encode the items first in one |
| 1465 | buffer (e.g., the COSE payload) and then add that buffer as a bstr to |
| 1466 | another encoding (e.g. the COSE to-be-signed bytes, the |
| 1467 | Sig_structure) potentially saving a lot of memory. |
| 1468 | |
| 1469 | When constructing cryptographically signed CBOR objects, maps or |
| 1470 | arrays, they typically are encoded normally and then wrapped as a |
| 1471 | byte string. The COSE standard for example does this. The wrapping is |
| 1472 | simply treating the encoded CBOR map as a byte string. |
| 1473 | |
| 1474 | The stated purpose of this wrapping is to prevent code relaying the |
| 1475 | signed data but not verifying it from tampering with the signed data |
| 1476 | thus making the signature unverifiable. It is also quite beneficial |
| 1477 | for the signature verification code. Standard CBOR parsers usually do |
| 1478 | not give access to partially parsed CBOR as would be need to check |
| 1479 | the signature of some CBOR. With this wrapping, standard CBOR parsers |
| 1480 | can be used to get to all the data needed for a signature |
| 1481 | verification. |
| 1482 | */ |
| 1483 | static void QCBOREncode_BstrWrap(QCBOREncodeContext *pCtx); |
| 1484 | |
| 1485 | static void QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pCtx, const char *szLabel); |
| 1486 | |
| 1487 | static void QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
| 1488 | |
| 1489 | |
| 1490 | /** |
| 1491 | @brief Close a wrapping bstr. |
| 1492 | |
| 1493 | @param[in] pCtx The context to add to. |
| 1494 | @param[out] pWrappedCBOR UsefulBufC containing wrapped bytes |
| 1495 | |
| 1496 | The closes a wrapping bstr opened by QCBOREncode_BstrWrap(). It reduces |
| 1497 | nesting level by one. |
| 1498 | |
| 1499 | A pointer and length of the enclosed encoded CBOR is returned in |
| 1500 | *pWrappedCBOR if it is not NULL. The main purpose of this is so this |
| 1501 | data can be hashed (e.g., with SHA-256) as part of a COSE (RFC 8152) |
| 1502 | implementation. **WARNING**, this pointer and length should be used |
| 1503 | right away before any other calls to QCBOREncode_xxxx() as they will |
| 1504 | move data around and the pointer and length will no longer be to the |
| 1505 | correct encoded CBOR. |
| 1506 | |
| 1507 | When an error occurs as a result of this call, the encoder records |
| 1508 | the error and enters the error state. The error will be returned when |
| 1509 | QCBOREncode_Finish() is called. |
| 1510 | |
| 1511 | If this has been called more times then QCBOREncode_BstrWrap(), |
| 1512 | then QCBOR_ERR_TOO_MANY_CLOSES will be returned when |
| 1513 | QCBOREncode_Finish() is called. |
| 1514 | |
| 1515 | If this is called and it is not a wrapping bstr that is currently |
| 1516 | open, QCBOR_ERR_CLOSE_MISMATCH will be returned when QCBOREncode_Finish() |
| 1517 | is called. |
| 1518 | */ |
| 1519 | static void QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pCtx, UsefulBufC *pWrappedCBOR); |
| 1520 | |
| 1521 | |
| 1522 | /** |
| 1523 | @brief Add some already-encoded CBOR bytes. |
| 1524 | |
| 1525 | @param[in] pCtx The context to add to. |
| 1526 | @param[in] Encoded The already-encoded CBOR to add to the context. |
| 1527 | |
| 1528 | The encoded CBOR being added must be fully conforming CBOR. It must |
| 1529 | be complete with no arrays or maps that are incomplete. While this |
| 1530 | encoder doesn't ever produce indefinite lengths, it is OK for the |
| 1531 | raw CBOR added here to have indefinite lengths. |
| 1532 | |
| 1533 | The raw CBOR added here is not checked in anyway. If it is not |
| 1534 | conforming or has open arrays or such, the final encoded CBOR |
| 1535 | will probably be wrong or not what was intended. |
| 1536 | |
| 1537 | If the encoded CBOR being added here contains multiple items, they |
| 1538 | must be enclosed in a map or array. At the top level the raw |
| 1539 | CBOR must be a single data item. |
| 1540 | */ |
| 1541 | static void QCBOREncode_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded); |
| 1542 | |
| 1543 | static void QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Encoded); |
| 1544 | |
| 1545 | static void QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Encoded); |
| 1546 | |
| 1547 | |
| 1548 | /** |
| 1549 | @brief Get the encoded result. |
| 1550 | |
| 1551 | @param[in] pCtx The context to finish encoding with. |
| 1552 | @param[out] pEncodedCBOR Pointer and length of encoded CBOR. |
| 1553 | |
| 1554 | @return |
| 1555 | One of the CBOR error codes. |
| 1556 | |
| 1557 | If this returns success QCBOR_SUCCESS the encoding was a success and |
| 1558 | the return length is correct and complete. |
| 1559 | |
| 1560 | If no buffer was passed to QCBOR_Init(), then only the length and |
| 1561 | number of items was computed. The length is in |
| 1562 | pEncodedCBOR->Bytes.len. pEncodedCBOR->Bytes.ptr is NULL. |
| 1563 | |
| 1564 | If a buffer was passed, then pEncodedCBOR->Bytes.ptr is the same as |
| 1565 | the buffer passed to QCBOR_Init() and contains the encoded CBOR |
| 1566 | and the length is filled in. |
| 1567 | |
| 1568 | If an error is returned, the buffer may have partially encoded |
| 1569 | incorrect CBOR in it and it should not be used. Likewise, the length |
| 1570 | may be incorrect and should not be used. |
| 1571 | |
| 1572 | Note that the error could have occurred in one of the many |
| 1573 | QCBOR_AddXXX calls long before QCBOREncode_Finish() was called. This |
| 1574 | error handling approach reduces the CBOR implementation size, but makes |
| 1575 | debugging a problem a little more difficult. |
| 1576 | */ |
| 1577 | QCBORError QCBOREncode_Finish(QCBOREncodeContext *pCtx, UsefulBufC *pEncodedCBOR); |
| 1578 | |
| 1579 | |
| 1580 | /** |
| 1581 | @brief Get the encoded CBOR and error status. |
| 1582 | |
| 1583 | @param[in] pCtx The context to finish encoding with. |
| 1584 | @param[out] uEncodedLen The length of the encoded or potentially encoded CBOR in bytes. |
| 1585 | |
| 1586 | @return |
| 1587 | One of the CBOR error codes. |
| 1588 | |
| 1589 | If this returns success QCBOR_SUCCESS the encoding was a success and |
| 1590 | the return length is correct and complete. |
| 1591 | |
| 1592 | If no buffer was passed to QCBOR_Init(), then only the length was |
| 1593 | computed. If a buffer was passed, then the encoded CBOR is in the |
| 1594 | buffer. |
| 1595 | |
| 1596 | If an error is returned, the buffer may have partially encoded |
| 1597 | incorrect CBOR in it and it should not be used. Likewise, the length |
| 1598 | may be incorrect and should not be used. |
| 1599 | |
| 1600 | Note that the error could have occurred in one of the many |
| 1601 | QCBOR_AddXXX calls long before QCBOREncode_Finish() was called. This |
| 1602 | error handling reduces the CBOR implementation size, but makes |
| 1603 | debugging harder. |
| 1604 | */ |
| 1605 | QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *pCtx, size_t *uEncodedLen); |
| 1606 | |
| 1607 | |
| 1608 | |
| 1609 | |
| 1610 | |
| 1611 | |
| 1612 | /** |
| 1613 | QCBORDecodeContext is the data type that holds context decoding the |
| 1614 | data items for some received CBOR. It is about 100 bytes, so it can go |
| 1615 | on the stack. The contents are opaque, and the caller should not |
| 1616 | access any internal items. A context may be re used serially as long |
| 1617 | as it is re initialized. |
| 1618 | */ |
| 1619 | typedef struct _QCBORDecodeContext QCBORDecodeContext; |
| 1620 | |
| 1621 | |
| 1622 | /** |
| 1623 | Initialize the CBOR decoder context. |
| 1624 | |
| 1625 | @param[in] pCtx The context to initialize. |
| 1626 | @param[in] EncodedCBOR The buffer with CBOR encoded bytes to be decoded. |
| 1627 | @param[in] nMode One of QCBOR_DECODE_MODE_xxx |
| 1628 | |
| 1629 | Initialize context for a pre-order travesal of the encoded CBOR tree. |
| 1630 | |
| 1631 | Most CBOR decoding can be completed by calling this function to start |
| 1632 | and QCBORDecode_GetNext() in a loop. |
| 1633 | |
| 1634 | If indefinite length strings are to be decoded, then |
| 1635 | QCBORDecode_SetMemPool() or QCBORDecode_SetUpAllocator() must be |
| 1636 | called to set up a string allocator. |
| 1637 | |
| 1638 | If tags other than built-in tags are to be recognized, then |
| 1639 | QCBORDecode_SetCallerAddedTagMap() must be called. The built-in tags |
| 1640 | are those for which a macro of the form CBOR_TAG_XXX is defined. |
| 1641 | |
| 1642 | Three decoding modes are supported. In normal mode, |
| 1643 | QCBOR_DECODE_MODE_NORMAL, maps are decoded and strings and ints are |
| 1644 | accepted as map labels. If a label is other than these, the error |
| 1645 | QCBOR_ERR_MAP_LABEL_TYPE is returned by QCBORDecode_GetNext(). |
| 1646 | |
| 1647 | In strings-only mode, QCBOR_DECODE_MODE_MAP_STRINGS_ONLY, only text |
| 1648 | strings are accepted for map labels. This lines up with CBOR that |
| 1649 | converts to JSON. The error QCBOR_ERR_MAP_LABEL_TYPE is returned by |
| 1650 | QCBORDecode_GetNext() if anything but a text string label is |
| 1651 | encountered. |
| 1652 | |
| 1653 | In QCBOR_DECODE_MODE_MAP_AS_ARRAY maps are treated as special arrays. |
| 1654 | They will be return with special uDataType QCBOR_TYPE_MAP_AS_ARRAY |
| 1655 | and uCount, the number of items, will be double what it would be |
| 1656 | for a normal map because the labels are also counted. This mode |
| 1657 | is useful for decoding CBOR that has labels that are not |
| 1658 | integers or text strings, but the caller must manage much of |
| 1659 | the map decoding. |
| 1660 | */ |
| 1661 | void QCBORDecode_Init(QCBORDecodeContext *pCtx, UsefulBufC EncodedCBOR, QCBORDecodeMode nMode); |
| 1662 | |
| 1663 | |
| 1664 | /** |
| 1665 | @brief Set up the MemPool string allocator for indefinite length strings. |
| 1666 | |
| 1667 | @param[in] pCtx The decode context. |
| 1668 | @param[in] MemPool The pointer and length of the memory pool. |
| 1669 | @param[in] bAllStrings true means to put even definite length strings in the pool. |
| 1670 | |
| 1671 | @return error if the MemPool was less than QCBOR_DECODE_MIN_MEM_POOL_SIZE. |
| 1672 | |
| 1673 | Indefinite length strings (text and byte) cannot be decoded unless |
| 1674 | there is a string allocator configured. MemPool is a simple built-in |
| 1675 | string allocator that allocates bytes from a memory pool handed to it |
| 1676 | by calling this function. The memory pool is just a pointer and |
| 1677 | length for some block of memory that is to be used for string |
| 1678 | allocation. It can come from the stack, heap or other. |
| 1679 | |
| 1680 | The memory pool must be QCBOR_DECODE_MIN_MEM_POOL_SIZE plus space for |
| 1681 | all the strings allocated. There is no overhead per string allocated |
| 1682 | |
| 1683 | This memory pool is used for all indefinite length strings that are |
| 1684 | text strings or byte strings, including strings used as labels. |
| 1685 | |
| 1686 | The pointers to strings in QCBORItem will point into the memory pool set |
| 1687 | here. They do not need to be individually freed. Just discard the buffer |
| 1688 | when they are no longer needed. |
| 1689 | |
| 1690 | If bAllStrings is set, then the size will be the overhead plus the |
| 1691 | space to hold **all** strings, definite and indefinite length, value |
| 1692 | or label. The advantage of this is that after the decode is complete, |
| 1693 | the original memory holding the encoded CBOR does not need to remain |
| 1694 | valid. |
| 1695 | |
| 1696 | If this function is never called because there is no need to support |
| 1697 | indefinite length strings, the MemPool implementation should be |
| 1698 | dead-stripped by the loader and not add to code size. |
| 1699 | */ |
| 1700 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pCtx, UsefulBuf MemPool, bool bAllStrings); |
| 1701 | |
| 1702 | |
| 1703 | /** |
| 1704 | @brief Sets up a custom string allocator for indefinite length strings |
| 1705 | |
| 1706 | @param[in] pCtx The decoder context to set up an allocator for |
| 1707 | @param[in] pAllocator The string allocator "object" |
| 1708 | |
| 1709 | See QCBORStringAllocator for the requirements of the string allocator. |
| 1710 | |
| 1711 | Typically, this is used if the simple MemPool allocator isn't desired. |
| 1712 | |
| 1713 | A malloc based string allocator can be obtained by calling |
| 1714 | QCBOR_DMalloc(). This function is supply separately from qcbor |
| 1715 | to keep qcbor smaller and neater. It is in a separate |
| 1716 | GitHub repository. |
| 1717 | |
| 1718 | You can also write your own allocator. Create the allocate, free, |
| 1719 | and destroy functions and put pointers to them in a QCBORStringAllocator. |
| 1720 | */ |
| 1721 | void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pCtx, const QCBORStringAllocator *pAllocator, bool bAllStrings); |
| 1722 | |
| 1723 | |
| 1724 | /** |
| 1725 | @brief Configure list of caller selected tags to be recognized |
| 1726 | |
| 1727 | @param[in] pCtx The decode context. |
| 1728 | @param[out] pTagList Structure holding the list of tags to configure |
| 1729 | |
| 1730 | This is used to tell the decoder about tags beyond those that are |
| 1731 | built-in that should be recognized. The built-in tags are those |
| 1732 | with macros of the form CBOR_TAG_XXX. |
| 1733 | |
| 1734 | See description of QCBORTagListIn. |
| 1735 | */ |
| 1736 | void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *pCtx, const QCBORTagListIn *pTagList); |
| 1737 | |
| 1738 | |
| 1739 | /** |
| 1740 | @brief Gets the next item (integer, byte string, array...) in pre order traversal of CBOR tree |
| 1741 | |
| 1742 | @param[in] pCtx The decoder context. |
| 1743 | @param[out] pDecodedItem Holds the CBOR item just decoded. |
| 1744 | |
| 1745 | @return 0 or error. All errors except QCBOR_ERR_TOO_MANY_TAGS |
| 1746 | and QCBOR_ERR_STRING_ALLOCATE indicate that the CBOR input |
| 1747 | could not be decoded. In most cases |
| 1748 | this is because the CBOR is invalid. In a few cases |
| 1749 | (QCBOR_ERR_ARRAY_NESTING_TOO_DEEP, QCBOR_ERR_INT_OVERFLOW, |
| 1750 | QCBOR_ERR_DATE_OVERFLOW) it is because the CBOR is beyond |
| 1751 | the limits of what this implementation can handle. |
| 1752 | QCBOR_ERR_NO_STRING_ALLOCATOR indicates CBOR that cannot |
| 1753 | be handled unless a string allocator is configured. |
| 1754 | QCBOR_ERR_MAP_LABEL_TYPE is in a way a limitation of |
| 1755 | this implementation, but can be avoided by decoding |
| 1756 | in QCBOR_DECODE_MODE_MAP_AS_ARRAY mode. |
| 1757 | |
| 1758 | pDecodedItem is filled in with the value parsed. Generally, the |
| 1759 | following data is returned in the structure. |
| 1760 | |
| 1761 | - The data type in uDataType which indicates which member of the val |
| 1762 | union the data is in. This decoder figures out the type based on the |
| 1763 | CBOR major type, the CBOR "additionalInfo", the CBOR optional tags |
| 1764 | and the value of the integer. |
| 1765 | |
| 1766 | - The value of the item, which might be an integer, a pointer and a |
| 1767 | length, the count of items in an array, a floating-point number or |
| 1768 | other. |
| 1769 | |
| 1770 | - The nesting level for maps and arrays. |
| 1771 | |
| 1772 | - The label for an item in a map, which may be a text or byte string or an integer. |
| 1773 | |
| 1774 | - The CBOR optional tag or tags. |
| 1775 | |
| 1776 | See documentation on in the data type QCBORItem for all the details |
| 1777 | on what is returned. |
| 1778 | |
| 1779 | This function also handles arrays and maps. When first encountered a |
| 1780 | QCBORItem will be returned with major type CBOR_MAJOR_TYPE_ARRAY or |
| 1781 | CBOR_MAJOR_TYPE_ARRAY_MAP. QCBORItem.val.uCount will indicate the number |
| 1782 | of Items in the array or map. Typically, an implementation will call |
| 1783 | QCBORDecode_GetNext() in a for loop to fetch them all. When decoding |
| 1784 | indefinite length maps and arrays, QCBORItem.val.uCount is UINT16_MAX |
| 1785 | and uNextNestLevel must be used to know when the end of a map |
| 1786 | or array is reached. |
| 1787 | |
| 1788 | Nesting level 0 is the outside top-most nesting level. For example, in |
| 1789 | a CBOR structure with two items, an integer and a byte string only, |
| 1790 | both would be at nesting level 0. A CBOR structure with an array |
| 1791 | open, an integer and a byte string, would have the integer and byte |
| 1792 | string as nesting level 1. |
| 1793 | |
| 1794 | Here is an example of how the nesting level is reported with no arrays |
| 1795 | or maps at all |
| 1796 | |
| 1797 | @verbatim |
| 1798 | CBOR Structure Nesting Level |
| 1799 | Integer 0 |
| 1800 | Byte String 0 |
| 1801 | @endverbatim |
| 1802 | |
| 1803 | Here is an example of how the nesting level is reported with an a simple |
| 1804 | array and some top-level items. |
| 1805 | |
| 1806 | @verbatim |
| 1807 | Integer 0 |
| 1808 | Array (with 2 items) 0 |
| 1809 | Byte String 1 |
| 1810 | Byte string 1 |
| 1811 | Integer 0 |
| 1812 | @endverbatim |
| 1813 | |
| 1814 | |
| 1815 | Here's a more complex example |
| 1816 | @verbatim |
| 1817 | |
| 1818 | Map with 2 items 0 |
| 1819 | Text string 1 |
| 1820 | Array with 3 integers 1 |
| 1821 | integer 2 |
| 1822 | integer 2 |
| 1823 | integer 2 |
| 1824 | text string 1 |
| 1825 | byte string 1 |
| 1826 | @endverbatim |
| 1827 | |
| 1828 | In QCBORItem, uNextNestLevel is the nesting level for the next call |
| 1829 | to QCBORDecode_GetNext(). It indicates if any maps or arrays were closed |
| 1830 | out during the processing of the just-fecthed QCBORItem. This processing |
| 1831 | includes a look-ahead for any breaks that close out indefinite length |
| 1832 | arrays or maps. This value is needed to be able to understand the |
| 1833 | hierarchical structure. If uNextNestLevel is not equal to uNestLevel |
| 1834 | the end of the current map or array has been encountered. This |
| 1835 | works the same for both definite and indefinite length arrays. |
| 1836 | |
| 1837 | Most uses of this decoder will not need to do anything extra for |
| 1838 | tag handling. The built-in tags, those with a macro of the form |
| 1839 | CBOR_TAG_XXXX, will be enough. |
| 1840 | |
| 1841 | If tags beyond built-in tags are to be recognized, they must be |
| 1842 | configured by calling QCBORDecode_SetCallerConfiguredTags(). If |
| 1843 | a tag is not recognized it is silently ignored. |
| 1844 | |
| 1845 | Several tagged types are automatically recognized and decoded and |
| 1846 | returned in their decoded form. |
| 1847 | |
| 1848 | To find out if a QCBORItem was tagged with a particular tag |
| 1849 | call QCBORDecode_IsTagged(). This works only for built-in |
| 1850 | tags and caller-configured tags. |
| 1851 | |
| 1852 | To get the full list of tags on an Item without having to |
| 1853 | pre-configure any predetermined list of tags use |
| 1854 | QCBORDecode_GetNextWithTags(). |
| 1855 | */ |
| 1856 | QCBORError QCBORDecode_GetNext(QCBORDecodeContext *pCtx, QCBORItem *pDecodedItem); |
| 1857 | |
| 1858 | |
| 1859 | /** |
| 1860 | @brief Gets the next item including full list of tags for item |
| 1861 | |
| 1862 | @param[in] pCtx The decoder context. |
| 1863 | @param[out] pDecodedItem Holds the CBOR item just decoded. |
| 1864 | @param[in,out] pTagList On input array to put tags in; on output the tags on this item. |
| 1865 | |
| 1866 | @return 0 or error. |
| 1867 | |
| 1868 | This works the same as QCBORDecode_GetNext() except that it also returns |
| 1869 | the full list of tags for the data item. This function should only |
| 1870 | be needed when parsing CBOR to print it out or convert it to some other |
| 1871 | format. It should not be needed in an actual CBOR protocol implementation. |
| 1872 | |
| 1873 | Tags will be returned here whether or not they are in the built-in or |
| 1874 | caller-configured tag lists. |
| 1875 | |
| 1876 | CBOR has no upper bound of limit on the number of tags that can be |
| 1877 | associated with a data item. In practice the number of tags on an item |
| 1878 | will usually be small, perhaps less than five. This will return an error |
| 1879 | if the array in pTagList is too small to hold all the tags for an item. |
| 1880 | |
| 1881 | (This function is separate from QCBORDecode_GetNext() so as to not have to |
| 1882 | make QCBORItem large enough to be able to hold a full list of tags. Even a list of |
| 1883 | five tags would nearly double its size because tags can be a uint64_t). |
| 1884 | */ |
| 1885 | QCBORError QCBORDecode_GetNextWithTags(QCBORDecodeContext *pCtx, QCBORItem *pDecodedItem, QCBORTagListOut *pTagList); |
| 1886 | |
| 1887 | |
| 1888 | /** |
| 1889 | @brief Determine if a CBOR item was tagged with a particular tag |
| 1890 | |
| 1891 | @param[in] pCtx The decoder context. |
| 1892 | @param[in] pItem The CBOR item to check |
| 1893 | @param[in] uTag The tag to check |
| 1894 | |
| 1895 | @return 1 if it was tagged, 0 if not |
| 1896 | |
| 1897 | QCBORDecode_GetNext() processes tags by looking them up |
| 1898 | in two lists and setting a bit corresponding to the tag |
| 1899 | in uTagBits in the QCBORItem. To find out if a |
| 1900 | QCBORItem was tagged with a particular tag, call |
| 1901 | this function. It handles the mapping between |
| 1902 | the two lists of tags and the bits set for it. |
| 1903 | |
| 1904 | The first tag list is the built-in tags, those |
| 1905 | with a macro of the form CBOR_TAG_XXX in this |
| 1906 | header file. There are up to 48 of these, |
| 1907 | corresponding to the lower 48 tag bits. |
| 1908 | |
| 1909 | The other optional tag list is the ones |
| 1910 | the caller configured using QCBORDecode_SetCallerConfiguredTagList() |
| 1911 | There are QCBOR_MAX_CUSTOM_TAGS (16) of these corresponding to the |
| 1912 | upper 16 tag bits. |
| 1913 | |
| 1914 | See also QCBORDecode_GetTags() and QCBORDecode_GetNextWithTags(). |
| 1915 | */ |
| 1916 | int QCBORDecode_IsTagged(QCBORDecodeContext *pCtx, const QCBORItem *pItem, uint64_t uTag); |
| 1917 | |
| 1918 | |
| 1919 | /** |
| 1920 | Check whether all the bytes have been decoded and maps and arrays closed. |
| 1921 | |
| 1922 | @param[in] pCtx The context to check |
| 1923 | |
| 1924 | @return QCBOR_SUCCESS or error |
| 1925 | |
| 1926 | This tells you if all the bytes given to QCBORDecode_Init() have |
| 1927 | been consumed and whether all maps and arrays were closed. |
| 1928 | The decode is considered to be incorrect or incomplete if not |
| 1929 | and an error will be returned. |
| 1930 | */ |
| 1931 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *pCtx); |
| 1932 | |
| 1933 | |
| 1934 | |
| 1935 | |
| 1936 | /** |
| 1937 | Convert int64_t to smaller int's safely |
| 1938 | |
| 1939 | @param [in] src An int64_t |
| 1940 | @param [out] dest A smaller sized int to convert to |
| 1941 | |
| 1942 | @return 0 on success -1 if not |
| 1943 | |
| 1944 | When decoding an integer, the CBOR decoder will return the value as an |
| 1945 | int64_t unless the integer is in the range of INT64_MAX and |
| 1946 | UINT64_MAX. That is, unless the value is so large that it can only be |
| 1947 | represented as a uint64_t, it will be an int64_t. |
| 1948 | |
| 1949 | CBOR itself doesn't size the individual integers it carries at |
| 1950 | all. The only limits it puts on the major integer types is that they |
| 1951 | are 8 bytes or less in length. Then encoders like this one use the |
| 1952 | smallest number of 1, 2, 4 or 8 bytes to represent the integer based |
| 1953 | on its value. There is thus no notion that one data item in CBOR is |
| 1954 | an 1 byte integer and another is a 4 byte integer. |
| 1955 | |
| 1956 | The interface to this CBOR encoder only uses 64-bit integers. Some |
| 1957 | CBOR protocols or implementations of CBOR protocols may not want to |
| 1958 | work with something smaller than a 64-bit integer. Perhaps an array |
| 1959 | of 1000 integers needs to be sent and none has a value larger than |
| 1960 | 50,000 and are represented as uint16_t. |
| 1961 | |
| 1962 | The sending / encoding side is easy. Integers are temporarily widened |
| 1963 | to 64-bits as a parameter passing through QCBOREncode_AddInt64() and |
| 1964 | encoded in the smallest way possible for their value, possibly in |
| 1965 | less than an uint16_t. |
| 1966 | |
| 1967 | On the decoding side the integers will be returned at int64_t even if |
| 1968 | they are small and were represented by only 1 or 2 bytes in the |
| 1969 | encoded CBOR. The functions here will convert integers to a small |
| 1970 | representation with an overflow check. |
| 1971 | |
| 1972 | (The decoder could have support 8 different integer types and |
| 1973 | represented the integer with the smallest type automatically, but |
| 1974 | this would have made the decoder more complex and code calling the |
| 1975 | decoder more complex in most use cases. In most use cases on 64-bit |
| 1976 | machines it is no burden to carry around even small integers as |
| 1977 | 64-bit values). |
| 1978 | */ |
| 1979 | static inline int QCBOR_Int64ToInt32(int64_t src, int32_t *dest) |
| 1980 | { |
| 1981 | if(src > INT32_MAX || src < INT32_MIN) { |
| 1982 | return -1; |
| 1983 | } else { |
| 1984 | *dest = (int32_t) src; |
| 1985 | } |
| 1986 | return 0; |
| 1987 | } |
| 1988 | |
| 1989 | static inline int QCBOR_Int64ToInt16(int64_t src, int16_t *dest) |
| 1990 | { |
| 1991 | if(src > INT16_MAX || src < INT16_MIN) { |
| 1992 | return -1; |
| 1993 | } else { |
| 1994 | *dest = (int16_t) src; |
| 1995 | } |
| 1996 | return 0; |
| 1997 | } |
| 1998 | |
| 1999 | static inline int QCBOR_Int64ToInt8(int64_t src, int8_t *dest) |
| 2000 | { |
| 2001 | if(src > INT8_MAX || src < INT8_MIN) { |
| 2002 | return -1; |
| 2003 | } else { |
| 2004 | *dest = (int8_t) src; |
| 2005 | } |
| 2006 | return 0; |
| 2007 | } |
| 2008 | |
| 2009 | static inline int QCBOR_Int64ToUInt32(int64_t src, uint32_t *dest) |
| 2010 | { |
| 2011 | if(src > UINT32_MAX || src < 0) { |
| 2012 | return -1; |
| 2013 | } else { |
| 2014 | *dest = (uint32_t) src; |
| 2015 | } |
| 2016 | return 0; |
| 2017 | } |
| 2018 | |
| 2019 | static inline int QCBOR_Int64UToInt16(int64_t src, uint16_t *dest) |
| 2020 | { |
| 2021 | if(src > UINT16_MAX || src < 0) { |
| 2022 | return -1; |
| 2023 | } else { |
| 2024 | *dest = (uint16_t) src; |
| 2025 | } |
| 2026 | return 0; |
| 2027 | } |
| 2028 | |
| 2029 | static inline int QCBOR_Int64ToUInt8(int64_t src, uint8_t *dest) |
| 2030 | { |
| 2031 | if(src > UINT8_MAX || src < 0) { |
| 2032 | return -1; |
| 2033 | } else { |
| 2034 | *dest = (uint8_t) src; |
| 2035 | } |
| 2036 | return 0; |
| 2037 | } |
| 2038 | |
| 2039 | static inline int QCBOR_Int64ToUInt64(int64_t src, uint64_t *dest) |
| 2040 | { |
| 2041 | if(src > 0) { |
| 2042 | return -1; |
| 2043 | } else { |
| 2044 | *dest = (uint64_t) src; |
| 2045 | } |
| 2046 | return 0; |
| 2047 | } |
| 2048 | |
| 2049 | |
| 2050 | |
| 2051 | |
| 2052 | |
| 2053 | /* =========================================================================== |
| 2054 | BEGINNING OF PRIVATE INLINE IMPLEMENTATION |
| 2055 | |
| 2056 | =========================================================================== */ |
| 2057 | |
| 2058 | /** |
| 2059 | @brief Semi-private method to add a buffer full of bytes to encoded output |
| 2060 | |
| 2061 | @param[in] pCtx The encoding context to add the integer to. |
| 2062 | @param[in] uMajorType The CBOR major type of the bytes. |
| 2063 | @param[in] Bytes The bytes to add. |
| 2064 | |
| 2065 | Use QCBOREncode_AddText() or QCBOREncode_AddBytes() or |
| 2066 | QCBOREncode_AddEncoded() instead. They are inline functions |
| 2067 | that call this and supply the correct major type. This function |
| 2068 | is public to make the inline functions work to keep the overall |
| 2069 | code size down and because the C language has no way to make |
| 2070 | it private. |
| 2071 | |
| 2072 | If this is called the major type should be CBOR_MAJOR_TYPE_TEXT_STRING, |
| 2073 | CBOR_MAJOR_TYPE_BYTE_STRING or CBOR_MAJOR_NONE_TYPE_RAW. The last |
| 2074 | one is special for adding already-encoded CBOR. |
| 2075 | */ |
| 2076 | void QCBOREncode_AddBuffer(QCBOREncodeContext *pCtx, uint8_t uMajorType, UsefulBufC Bytes); |
| 2077 | |
| 2078 | |
| 2079 | /** |
| 2080 | @brief Semi-private method to open a map, array or bstr wrapped CBOR |
| 2081 | |
| 2082 | @param[in] pCtx The context to add to. |
| 2083 | @param[in] uMajorType The major CBOR type to close |
| 2084 | |
| 2085 | Call QCBOREncode_OpenArray(), QCBOREncode_OpenMap() or |
| 2086 | QCBOREncode_BstrWrap() instead of this. |
| 2087 | */ |
| 2088 | void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *pCtx, uint8_t uMajorType); |
| 2089 | |
| 2090 | |
| 2091 | /** |
| 2092 | @brief Semi-private method to close a map, array or bstr wrapped CBOR |
| 2093 | |
| 2094 | @param[in] pCtx The context to add to. |
| 2095 | @param[in] uMajorType The major CBOR type to close |
| 2096 | @param[out] pWrappedCBOR UsefulBufC containing wrapped bytes |
| 2097 | |
| 2098 | Call QCBOREncode_CloseArray(), QCBOREncode_CloseMap() or |
| 2099 | QCBOREncode_CloseBstrWrap() instead of this. |
| 2100 | */ |
| 2101 | void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *pCtx, uint8_t uMajorType, UsefulBufC *pWrappedCBOR); |
| 2102 | |
| 2103 | |
| 2104 | /** |
| 2105 | @brief Semi-private method to add simple types. |
| 2106 | |
| 2107 | @param[in] pCtx The encoding context to add the simple value to. |
| 2108 | @param[in] uSize Minimum encoding size for uNum. Usually 0. |
| 2109 | @param[in] uNum One of CBOR_SIMPLEV_FALSE through _UNDEF or other. |
| 2110 | |
| 2111 | This is used to add simple types like true and false. |
| 2112 | |
| 2113 | Call QCBOREncode_AddBool(), QCBOREncode_AddNULL(), QCBOREncode_AddUndef() |
| 2114 | instead of this. |
| 2115 | |
| 2116 | This function can add simple values that are not defined by CBOR yet. This expansion |
| 2117 | point in CBOR should not be used unless they are standardized. |
| 2118 | |
| 2119 | Error handling is the same as QCBOREncode_AddInt64(). |
| 2120 | */ |
| 2121 | void QCBOREncode_AddType7(QCBOREncodeContext *pCtx, size_t uSize, uint64_t uNum); |
| 2122 | |
| 2123 | |
| 2124 | static inline void QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t uNum) |
| 2125 | { |
| 2126 | QCBOREncode_AddBuffer(pCtx, CBOR_MAJOR_TYPE_TEXT_STRING, UsefulBuf_FromSZ(szLabel)); // AddSZString not defined yet |
| 2127 | QCBOREncode_AddInt64(pCtx, uNum); |
| 2128 | } |
| 2129 | |
| 2130 | static inline void QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum) |
| 2131 | { |
| 2132 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2133 | QCBOREncode_AddInt64(pCtx, uNum); |
| 2134 | } |
| 2135 | |
| 2136 | |
| 2137 | static inline void QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum) |
| 2138 | { |
| 2139 | QCBOREncode_AddBuffer(pCtx, CBOR_MAJOR_TYPE_TEXT_STRING, UsefulBuf_FromSZ(szLabel)); // AddSZString not defined yet |
| 2140 | QCBOREncode_AddUInt64(pCtx, uNum); |
| 2141 | } |
| 2142 | |
| 2143 | static inline void QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum) |
| 2144 | { |
| 2145 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2146 | QCBOREncode_AddUInt64(pCtx, uNum); |
| 2147 | } |
| 2148 | |
| 2149 | |
| 2150 | static inline void QCBOREncode_AddText(QCBOREncodeContext *pCtx, UsefulBufC Text) |
| 2151 | { |
| 2152 | QCBOREncode_AddBuffer(pCtx, CBOR_MAJOR_TYPE_TEXT_STRING, Text); |
| 2153 | } |
| 2154 | |
| 2155 | static inline void QCBOREncode_AddTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Text) |
| 2156 | { |
| 2157 | QCBOREncode_AddText(pCtx, UsefulBuf_FromSZ(szLabel)); // AddSZString not defined yet |
| 2158 | QCBOREncode_AddText(pCtx, Text); |
| 2159 | } |
| 2160 | |
| 2161 | static inline void QCBOREncode_AddTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Text) |
| 2162 | { |
| 2163 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2164 | QCBOREncode_AddText(pCtx, Text); |
| 2165 | } |
| 2166 | |
| 2167 | |
| 2168 | inline static void QCBOREncode_AddSZString(QCBOREncodeContext *pCtx, const char *szString) |
| 2169 | { |
| 2170 | QCBOREncode_AddText(pCtx, UsefulBuf_FromSZ(szString)); |
| 2171 | } |
| 2172 | |
| 2173 | static inline void QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szString) |
| 2174 | { |
| 2175 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2176 | QCBOREncode_AddSZString(pCtx, szString); |
| 2177 | } |
| 2178 | |
| 2179 | static inline void QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szString) |
| 2180 | { |
| 2181 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2182 | QCBOREncode_AddSZString(pCtx, szString); |
| 2183 | } |
| 2184 | |
| 2185 | |
| 2186 | static inline void QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum) |
| 2187 | { |
| 2188 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2189 | QCBOREncode_AddDouble(pCtx, dNum); |
| 2190 | } |
| 2191 | |
| 2192 | static inline void QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum) |
| 2193 | { |
| 2194 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2195 | QCBOREncode_AddDouble(pCtx, dNum); |
| 2196 | } |
| 2197 | |
| 2198 | |
| 2199 | static inline void QCBOREncode_AddDateEpoch(QCBOREncodeContext *pCtx, int64_t date) |
| 2200 | { |
| 2201 | QCBOREncode_AddTag(pCtx, CBOR_TAG_DATE_EPOCH); |
| 2202 | QCBOREncode_AddInt64(pCtx, date); |
| 2203 | } |
| 2204 | |
| 2205 | static inline void QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t date) |
| 2206 | { |
| 2207 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2208 | QCBOREncode_AddTag(pCtx, CBOR_TAG_DATE_EPOCH); |
| 2209 | QCBOREncode_AddInt64(pCtx, date); |
| 2210 | } |
| 2211 | |
| 2212 | static inline void QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t date) |
| 2213 | { |
| 2214 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2215 | QCBOREncode_AddTag(pCtx, CBOR_TAG_DATE_EPOCH); |
| 2216 | QCBOREncode_AddInt64(pCtx, date); |
| 2217 | } |
| 2218 | |
| 2219 | |
| 2220 | static inline void QCBOREncode_AddBytes(QCBOREncodeContext *pCtx, UsefulBufC Bytes) |
| 2221 | { |
| 2222 | QCBOREncode_AddBuffer(pCtx, CBOR_MAJOR_TYPE_BYTE_STRING, Bytes); |
| 2223 | } |
| 2224 | |
| 2225 | static inline void QCBOREncode_AddBytesToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes) |
| 2226 | { |
| 2227 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2228 | QCBOREncode_AddBytes(pCtx, Bytes); |
| 2229 | } |
| 2230 | |
| 2231 | static inline void QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes) |
| 2232 | { |
| 2233 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2234 | QCBOREncode_AddBytes(pCtx, Bytes); |
| 2235 | } |
| 2236 | |
| 2237 | |
| 2238 | static inline void QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pCtx, UsefulBufC Bytes) |
| 2239 | { |
| 2240 | QCBOREncode_AddTag(pCtx, CBOR_TAG_BIN_UUID); |
| 2241 | QCBOREncode_AddBytes(pCtx, Bytes); |
| 2242 | } |
| 2243 | |
| 2244 | static inline void QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes) |
| 2245 | { |
| 2246 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2247 | QCBOREncode_AddTag(pCtx, CBOR_TAG_BIN_UUID); |
| 2248 | QCBOREncode_AddBytes(pCtx, Bytes); |
| 2249 | } |
| 2250 | |
| 2251 | static inline void QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes) |
| 2252 | { |
| 2253 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2254 | QCBOREncode_AddTag(pCtx, CBOR_TAG_BIN_UUID); |
| 2255 | QCBOREncode_AddBytes(pCtx, Bytes); |
| 2256 | } |
| 2257 | |
| 2258 | |
| 2259 | static inline void QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx, UsefulBufC Bytes) |
| 2260 | { |
| 2261 | QCBOREncode_AddTag(pCtx, CBOR_TAG_POS_BIGNUM); |
| 2262 | QCBOREncode_AddBytes(pCtx, Bytes); |
| 2263 | } |
| 2264 | |
| 2265 | static inline void QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes) |
| 2266 | { |
| 2267 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2268 | QCBOREncode_AddTag(pCtx, CBOR_TAG_POS_BIGNUM); |
| 2269 | QCBOREncode_AddBytes(pCtx, Bytes); |
| 2270 | } |
| 2271 | |
| 2272 | static inline void QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes) |
| 2273 | { |
| 2274 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2275 | QCBOREncode_AddTag(pCtx, CBOR_TAG_POS_BIGNUM); |
| 2276 | QCBOREncode_AddBytes(pCtx, Bytes); |
| 2277 | } |
| 2278 | |
| 2279 | |
| 2280 | static inline void QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx, UsefulBufC Bytes) |
| 2281 | { |
| 2282 | QCBOREncode_AddTag(pCtx, CBOR_TAG_NEG_BIGNUM); |
| 2283 | QCBOREncode_AddBytes(pCtx, Bytes); |
| 2284 | } |
| 2285 | |
| 2286 | static inline void QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes) |
| 2287 | { |
| 2288 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2289 | QCBOREncode_AddTag(pCtx, CBOR_TAG_NEG_BIGNUM); |
| 2290 | QCBOREncode_AddBytes(pCtx, Bytes); |
| 2291 | } |
| 2292 | |
| 2293 | static inline void QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes) |
| 2294 | { |
| 2295 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2296 | QCBOREncode_AddTag(pCtx, CBOR_TAG_NEG_BIGNUM); |
| 2297 | QCBOREncode_AddBytes(pCtx, Bytes); |
| 2298 | } |
| 2299 | |
| 2300 | |
| 2301 | static inline void QCBOREncode_AddURI(QCBOREncodeContext *pCtx, UsefulBufC URI) |
| 2302 | { |
| 2303 | QCBOREncode_AddTag(pCtx, CBOR_TAG_URI); |
| 2304 | QCBOREncode_AddText(pCtx, URI); |
| 2305 | } |
| 2306 | |
| 2307 | static inline void QCBOREncode_AddURIToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC URI) |
| 2308 | { |
| 2309 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2310 | QCBOREncode_AddTag(pCtx, CBOR_TAG_URI); |
| 2311 | QCBOREncode_AddText(pCtx, URI); |
| 2312 | } |
| 2313 | |
| 2314 | static inline void QCBOREncode_AddURIToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC URI) |
| 2315 | { |
| 2316 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2317 | QCBOREncode_AddTag(pCtx, CBOR_TAG_URI); |
| 2318 | QCBOREncode_AddText(pCtx, URI); |
| 2319 | } |
| 2320 | |
| 2321 | |
| 2322 | |
| 2323 | static inline void QCBOREncode_AddB64Text(QCBOREncodeContext *pCtx, UsefulBufC B64Text) |
| 2324 | { |
| 2325 | QCBOREncode_AddTag(pCtx, CBOR_TAG_B64); |
| 2326 | QCBOREncode_AddText(pCtx, B64Text); |
| 2327 | } |
| 2328 | |
| 2329 | static inline void QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC B64Text) |
| 2330 | { |
| 2331 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2332 | QCBOREncode_AddTag(pCtx, CBOR_TAG_B64); |
| 2333 | QCBOREncode_AddText(pCtx, B64Text); |
| 2334 | } |
| 2335 | |
| 2336 | static inline void QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC B64Text) |
| 2337 | { |
| 2338 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2339 | QCBOREncode_AddTag(pCtx, CBOR_TAG_B64); |
| 2340 | QCBOREncode_AddText(pCtx, B64Text); |
| 2341 | } |
| 2342 | |
| 2343 | |
| 2344 | static inline void QCBOREncode_AddB64URLText(QCBOREncodeContext *pCtx, UsefulBufC B64Text) |
| 2345 | { |
| 2346 | QCBOREncode_AddTag(pCtx, CBOR_TAG_B64URL); |
| 2347 | QCBOREncode_AddText(pCtx, B64Text); |
| 2348 | } |
| 2349 | |
| 2350 | static inline void QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC B64Text) |
| 2351 | { |
| 2352 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2353 | QCBOREncode_AddTag(pCtx, CBOR_TAG_B64URL); |
| 2354 | QCBOREncode_AddText(pCtx, B64Text); |
| 2355 | } |
| 2356 | |
| 2357 | static inline void QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC B64Text) |
| 2358 | { |
| 2359 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2360 | QCBOREncode_AddTag(pCtx, CBOR_TAG_B64URL); |
| 2361 | QCBOREncode_AddText(pCtx, B64Text); |
| 2362 | } |
| 2363 | |
| 2364 | |
| 2365 | static inline void QCBOREncode_AddRegex(QCBOREncodeContext *pCtx, UsefulBufC Bytes) |
| 2366 | { |
| 2367 | QCBOREncode_AddTag(pCtx, CBOR_TAG_REGEX); |
| 2368 | QCBOREncode_AddText(pCtx, Bytes); |
| 2369 | } |
| 2370 | |
| 2371 | static inline void QCBOREncode_AddRegexToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes) |
| 2372 | { |
| 2373 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2374 | QCBOREncode_AddTag(pCtx, CBOR_TAG_REGEX); |
| 2375 | QCBOREncode_AddText(pCtx, Bytes); |
| 2376 | } |
| 2377 | |
| 2378 | static inline void QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes) |
| 2379 | { |
| 2380 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2381 | QCBOREncode_AddTag(pCtx, CBOR_TAG_REGEX); |
| 2382 | QCBOREncode_AddText(pCtx, Bytes); |
| 2383 | } |
| 2384 | |
| 2385 | |
| 2386 | static inline void QCBOREncode_AddMIMEData(QCBOREncodeContext *pCtx, UsefulBufC MIMEData) |
| 2387 | { |
| 2388 | QCBOREncode_AddTag(pCtx, CBOR_TAG_MIME); |
| 2389 | QCBOREncode_AddText(pCtx, MIMEData); |
| 2390 | } |
| 2391 | |
| 2392 | static inline void QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC MIMEData) |
| 2393 | { |
| 2394 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2395 | QCBOREncode_AddTag(pCtx, CBOR_TAG_MIME); |
| 2396 | QCBOREncode_AddText(pCtx, MIMEData); |
| 2397 | } |
| 2398 | |
| 2399 | static inline void QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC MIMEData) |
| 2400 | { |
| 2401 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2402 | QCBOREncode_AddTag(pCtx, CBOR_TAG_MIME); |
| 2403 | QCBOREncode_AddText(pCtx, MIMEData); |
| 2404 | } |
| 2405 | |
| 2406 | |
| 2407 | static inline void QCBOREncode_AddDateString(QCBOREncodeContext *pCtx, const char *szDate) |
| 2408 | { |
| 2409 | QCBOREncode_AddTag(pCtx, CBOR_TAG_DATE_STRING); |
| 2410 | QCBOREncode_AddSZString(pCtx, szDate); |
| 2411 | } |
| 2412 | |
| 2413 | static inline void QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szDate) |
| 2414 | { |
| 2415 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2416 | QCBOREncode_AddTag(pCtx, CBOR_TAG_DATE_STRING); |
| 2417 | QCBOREncode_AddSZString(pCtx, szDate); |
| 2418 | } |
| 2419 | |
| 2420 | static inline void QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szDate) |
| 2421 | { |
| 2422 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2423 | QCBOREncode_AddTag(pCtx, CBOR_TAG_DATE_STRING); |
| 2424 | QCBOREncode_AddSZString(pCtx, szDate); |
| 2425 | } |
| 2426 | |
| 2427 | |
| 2428 | static inline void QCBOREncode_AddSimple(QCBOREncodeContext *pCtx, uint64_t uNum) |
| 2429 | { |
| 2430 | QCBOREncode_AddType7(pCtx, 0, uNum); |
| 2431 | } |
| 2432 | |
| 2433 | static inline void QCBOREncode_AddSimpleToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint8_t uSimple) |
| 2434 | { |
| 2435 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2436 | QCBOREncode_AddSimple(pCtx, uSimple); |
| 2437 | } |
| 2438 | |
| 2439 | static inline void QCBOREncode_AddSimpleToMapN(QCBOREncodeContext *pCtx, int nLabel, uint8_t uSimple) |
| 2440 | { |
| 2441 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2442 | QCBOREncode_AddSimple(pCtx, uSimple); |
| 2443 | } |
| 2444 | |
| 2445 | |
| 2446 | static inline void QCBOREncode_AddBool(QCBOREncodeContext *pCtx, bool b) |
| 2447 | { |
| 2448 | uint8_t uSimple = CBOR_SIMPLEV_FALSE; |
| 2449 | if(b) { |
| 2450 | uSimple = CBOR_SIMPLEV_TRUE; |
| 2451 | } |
| 2452 | QCBOREncode_AddSimple(pCtx, uSimple); |
| 2453 | } |
| 2454 | |
| 2455 | static inline void QCBOREncode_AddBoolToMap(QCBOREncodeContext *pCtx, const char *szLabel, bool b) |
| 2456 | { |
| 2457 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2458 | QCBOREncode_AddBool(pCtx, b); |
| 2459 | } |
| 2460 | |
| 2461 | static inline void QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, bool b) |
| 2462 | { |
| 2463 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2464 | QCBOREncode_AddBool(pCtx, b); |
| 2465 | } |
| 2466 | |
| 2467 | |
| 2468 | static inline void QCBOREncode_AddNULL(QCBOREncodeContext *pCtx) |
| 2469 | { |
| 2470 | QCBOREncode_AddSimple(pCtx, CBOR_SIMPLEV_NULL); |
| 2471 | } |
| 2472 | |
| 2473 | static inline void QCBOREncode_AddNULLToMap(QCBOREncodeContext *pCtx, const char *szLabel) |
| 2474 | { |
| 2475 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2476 | QCBOREncode_AddNULL(pCtx); |
| 2477 | } |
| 2478 | |
| 2479 | static inline void QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pCtx, int64_t nLabel) |
| 2480 | { |
| 2481 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2482 | QCBOREncode_AddNULL(pCtx); |
| 2483 | } |
| 2484 | |
| 2485 | |
| 2486 | static inline void QCBOREncode_AddUndef(QCBOREncodeContext *pCtx) |
| 2487 | { |
| 2488 | QCBOREncode_AddSimple(pCtx, CBOR_SIMPLEV_UNDEF); |
| 2489 | } |
| 2490 | |
| 2491 | static inline void QCBOREncode_AddUndefToMap(QCBOREncodeContext *pCtx, const char *szLabel) |
| 2492 | { |
| 2493 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2494 | QCBOREncode_AddUndef(pCtx); |
| 2495 | } |
| 2496 | |
| 2497 | static inline void QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pCtx, int64_t nLabel) |
| 2498 | { |
| 2499 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2500 | QCBOREncode_AddUndef(pCtx); |
| 2501 | } |
| 2502 | |
| 2503 | |
| 2504 | static inline void QCBOREncode_OpenArray(QCBOREncodeContext *pCtx) |
| 2505 | { |
| 2506 | QCBOREncode_OpenMapOrArray(pCtx, CBOR_MAJOR_TYPE_ARRAY); |
| 2507 | } |
| 2508 | |
| 2509 | static inline void QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pCtx, const char *szLabel) |
| 2510 | { |
| 2511 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2512 | QCBOREncode_OpenArray(pCtx); |
| 2513 | } |
| 2514 | |
| 2515 | static inline void QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pCtx, int64_t nLabel) |
| 2516 | { |
| 2517 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2518 | QCBOREncode_OpenArray(pCtx); |
| 2519 | } |
| 2520 | |
| 2521 | static inline void QCBOREncode_CloseArray(QCBOREncodeContext *pCtx) |
| 2522 | { |
| 2523 | QCBOREncode_CloseMapOrArray(pCtx, CBOR_MAJOR_TYPE_ARRAY, NULL); |
| 2524 | } |
| 2525 | |
| 2526 | |
| 2527 | static inline void QCBOREncode_OpenMap(QCBOREncodeContext *pCtx) |
| 2528 | { |
| 2529 | QCBOREncode_OpenMapOrArray(pCtx, CBOR_MAJOR_TYPE_MAP); |
| 2530 | } |
| 2531 | |
| 2532 | static inline void QCBOREncode_OpenMapInMap(QCBOREncodeContext *pCtx, const char *szLabel) |
| 2533 | { |
| 2534 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2535 | QCBOREncode_OpenMap(pCtx); |
| 2536 | } |
| 2537 | |
| 2538 | static inline void QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel) |
| 2539 | { |
| 2540 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2541 | QCBOREncode_OpenMap(pCtx); |
| 2542 | } |
| 2543 | |
| 2544 | static inline void QCBOREncode_CloseMap(QCBOREncodeContext *pCtx) |
| 2545 | { |
| 2546 | QCBOREncode_CloseMapOrArray(pCtx, CBOR_MAJOR_TYPE_MAP, NULL); |
| 2547 | } |
| 2548 | |
| 2549 | |
| 2550 | static inline void QCBOREncode_BstrWrap(QCBOREncodeContext *pCtx) |
| 2551 | { |
| 2552 | QCBOREncode_OpenMapOrArray(pCtx, CBOR_MAJOR_TYPE_BYTE_STRING); |
| 2553 | } |
| 2554 | |
| 2555 | static inline void QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pCtx, const char *szLabel) |
| 2556 | { |
| 2557 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2558 | QCBOREncode_BstrWrap(pCtx); |
| 2559 | } |
| 2560 | |
| 2561 | static inline void QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel) |
| 2562 | { |
| 2563 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2564 | QCBOREncode_BstrWrap(pCtx); |
| 2565 | } |
| 2566 | |
| 2567 | static inline void QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pCtx, UsefulBufC *pWrappedCBOR) |
| 2568 | { |
| 2569 | QCBOREncode_CloseMapOrArray(pCtx, CBOR_MAJOR_TYPE_BYTE_STRING, pWrappedCBOR); |
| 2570 | } |
| 2571 | |
| 2572 | |
| 2573 | static inline void QCBOREncode_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded) |
| 2574 | { |
| 2575 | QCBOREncode_AddBuffer(pCtx, CBOR_MAJOR_NONE_TYPE_RAW, Encoded); |
| 2576 | } |
| 2577 | |
| 2578 | static inline void QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Encoded) |
| 2579 | { |
| 2580 | QCBOREncode_AddSZString(pCtx, szLabel); |
| 2581 | QCBOREncode_AddEncoded(pCtx, Encoded); |
| 2582 | } |
| 2583 | |
| 2584 | static inline void QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Encoded) |
| 2585 | { |
| 2586 | QCBOREncode_AddInt64(pCtx, nLabel); |
| 2587 | QCBOREncode_AddEncoded(pCtx, Encoded); |
| 2588 | } |
| 2589 | |
| 2590 | |
| 2591 | /* =========================================================================== |
| 2592 | END OF PRIVATE INLINE IMPLEMENTATION |
| 2593 | |
| 2594 | =========================================================================== */ |
| 2595 | |
| 2596 | #endif /* defined(__QCBOR__qcbor__) */ |
| 2597 | |