Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1 | /* =========================================================================== |
| 2 | * Copyright (c) 2016-2018, The Linux Foundation. |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2018-2024, Laurence Lundblade. |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 4 | * Copyright (c) 2021, Arm Limited. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are |
| 9 | * met: |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following |
| 14 | * disclaimer in the documentation and/or other materials provided |
| 15 | * with the distribution. |
| 16 | * * Neither the name of The Linux Foundation nor the names of its |
| 17 | * contributors, nor the name "Laurence Lundblade" may be used to |
| 18 | * endorse or promote products derived from this software without |
| 19 | * specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 30 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 31 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * ========================================================================= */ |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 33 | |
| 34 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 35 | #ifndef qcbor_encode_h |
| 36 | #define qcbor_encode_h |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 37 | |
| 38 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 39 | #include "qcbor/qcbor_common.h" |
| 40 | #include "qcbor/qcbor_private.h" |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 41 | #include <stdbool.h> |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 42 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 43 | |
| 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
Dave Thaler | 12b2375 | 2020-03-27 01:23:08 -0700 | [diff] [blame] | 46 | #if 0 |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 47 | } // Keep editor indention formatting happy |
| 48 | #endif |
| 49 | #endif |
| 50 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 51 | |
| 52 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 53 | * @file qcbor_encode.h |
| 54 | * |
| 55 | * @anchor Overview |
| 56 | * |
| 57 | * # QCBOR Overview |
| 58 | * |
| 59 | * This implements CBOR -- Concise Binary Object Representation as |
| 60 | * defined in [RFC 8949] (https://tools.ietf.org/html/rfc8949). More |
| 61 | * information is at http://cbor.io. This is a near-complete implementation of |
| 62 | * the specification. [RFC 8742] (https://tools.ietf.org/html/rfc8742) CBOR |
| 63 | * Sequences is also supported. Limitations are listed further down. |
| 64 | * |
| 65 | * See @ref Encoding for general discussion on encoding, |
| 66 | * @ref BasicDecode for general discussion on the basic decode features |
| 67 | * and @ref SpiffyDecode for general discussion on the easier-to-use |
| 68 | * decoder functions. |
| 69 | * |
| 70 | * CBOR is intentionally designed to be translatable to JSON, but not |
| 71 | * all CBOR can convert to JSON. See RFC 8949 for more info on how to |
| 72 | * construct CBOR that is the most JSON friendly. |
| 73 | * |
| 74 | * The memory model for encoding and decoding is that encoded CBOR must |
| 75 | * be in a contiguous buffer in memory. During encoding the caller must |
| 76 | * supply an output buffer and if the encoding would go off the end of |
| 77 | * the buffer an error is returned. During decoding the caller supplies |
| 78 | * the encoded CBOR in a contiguous buffer and the decoder returns |
| 79 | * pointers and lengths into that buffer for strings. |
| 80 | * |
| 81 | * This implementation does not require malloc. All data structures |
| 82 | * passed in/out of the APIs can fit on the stack. |
| 83 | * |
| 84 | * Decoding of indefinite-length strings is a special case that requires |
| 85 | * a "string allocator" to allocate memory into which the segments of |
| 86 | * the string are coalesced. Without this, decoding will error out if an |
| 87 | * indefinite-length string is encountered (indefinite-length maps and |
| 88 | * arrays do not require the string allocator). A simple string |
| 89 | * allocator called MemPool is built-in and will work if supplied with a |
| 90 | * block of memory to allocate. The string allocator can optionally use |
| 91 | * malloc() or some other custom scheme. |
| 92 | * |
| 93 | * Here are some terms and definitions: |
| 94 | * |
| 95 | * - "Item", "Data Item": An integer or string or such. The basic "thing" that |
| 96 | * CBOR is about. An array is an item itself that contains some items. |
| 97 | * |
| 98 | * - "Array": An ordered sequence of items, the same as JSON. |
| 99 | * |
| 100 | * - "Map": A collection of label/value pairs. Each pair is a data |
| 101 | * item. A JSON "object" is the same as a CBOR "map". |
| 102 | * |
| 103 | * - "Label": The data item in a pair in a map that names or identifies |
| 104 | * the pair, not the value. This implementation refers to it as a |
| 105 | * "label". JSON refers to it as the "name". The CBOR RFC refers to it |
| 106 | * this as a "key". This implementation chooses label instead because |
| 107 | * key is too easily confused with a cryptographic key. The COSE |
| 108 | * standard, which uses CBOR, has also chosen to use the term "label" |
| 109 | * rather than "key" for this same reason. |
| 110 | * |
| 111 | * - "Key": See "Label" above. |
| 112 | * |
| 113 | * - "Tag": A data item that is an explicitly labeled new data |
| 114 | * type made up of the tagging integer and the tag content. |
| 115 | * See @ref Tags-Overview and @ref Tag-Usage. |
| 116 | * |
| 117 | * - "Initial Byte": The first byte of an encoded item. Encoding and |
| 118 | * decoding of this byte is taken care of by the implementation. |
| 119 | * |
| 120 | * - "Additional Info": In addition to the major type, all data items |
| 121 | * have some other info. This is usually the length of the data but can |
| 122 | * be several other things. Encoding and decoding of this is taken care |
| 123 | * of by the implementation. |
| 124 | * |
| 125 | * CBOR has two mechanisms for tagging and labeling the data values like |
| 126 | * integers and strings. For example, an integer that represents |
| 127 | * someone's birthday in epoch seconds since Jan 1, 1970 could be |
| 128 | * encoded like this: |
| 129 | * |
| 130 | * - First it is CBOR_MAJOR_TYPE_POSITIVE_INT (@ref QCBOR_TYPE_INT64), |
| 131 | * the primitive positive integer. |
| 132 | * |
| 133 | * - Next it has a "tag" @ref CBOR_TAG_DATE_EPOCH indicating the integer |
| 134 | * represents a date in the form of the number of seconds since Jan 1, |
| 135 | * 1970. |
| 136 | * |
| 137 | * - Last it has a string "label" like "BirthDate" indicating the |
| 138 | * meaning of the data. |
| 139 | * |
| 140 | * The encoded binary looks like this: |
| 141 | * |
| 142 | * a1 # Map of 1 item |
| 143 | * 69 # Indicates text string of 9 bytes |
| 144 | * 426972746844617465 # The text "BirthDate" |
| 145 | * c1 # Tags next integer as epoch date |
| 146 | * 1a # Indicates a 4-byte integer |
| 147 | * 580d4172 # unsigned integer date 1477263730 |
| 148 | * |
| 149 | * Implementors using this API will primarily work with |
| 150 | * labels. Generally, tags are only needed for making up new data |
| 151 | * types. This implementation covers most of the data types defined in |
| 152 | * the RFC using tags. It also, allows for the use of custom tags if |
| 153 | * necessary. |
| 154 | * |
| 155 | * This implementation explicitly supports labels that are text strings |
| 156 | * and integers. Text strings translate nicely into JSON objects and are |
| 157 | * very readable. Integer labels are much less readable but can be very |
| 158 | * compact. If they are in the range of 0 to 23, they take up only one |
| 159 | * byte. |
| 160 | * |
| 161 | * CBOR allows a label to be any type of data including an array or a |
| 162 | * map. It is possible to use this API to construct and parse such |
| 163 | * labels, but it is not explicitly supported. |
| 164 | * |
| 165 | * @anchor Encoding |
| 166 | * |
| 167 | * ## Encoding |
| 168 | * |
| 169 | * A common encoding usage mode is to invoke the encoding twice. First |
| 170 | * with the output buffer as @ref SizeCalculateUsefulBuf to compute the |
| 171 | * length of the needed output buffer. The correct sized output buffer |
| 172 | * is allocated. The encoder is invoked a second time with the allocated |
| 173 | * output buffer. |
| 174 | * |
| 175 | * The double invocation is not required if the maximum output buffer |
| 176 | * size can be predicted. This is usually possible for simple CBOR |
| 177 | * structures. |
| 178 | * |
| 179 | * If a buffer too small to hold the encoded output is given, the error |
| 180 | * @ref QCBOR_ERR_BUFFER_TOO_SMALL will be returned. Data will never be |
| 181 | * written off the end of the output buffer no matter which functions |
| 182 | * here are called or what parameters are passed to them. |
| 183 | * |
| 184 | * The encoding error handling is simple. The only possible errors are |
| 185 | * trying to encode structures that are too large or too complex. There |
| 186 | * are no internal malloc calls so there will be no failures for out of |
| 187 | * memory. The error state is tracked internally, so there is no need |
| 188 | * to check for errors when encoding. Only the return code from |
| 189 | * QCBOREncode_Finish() need be checked as once an error happens, the |
| 190 | * encoder goes into an error state and calls to it to add more data |
| 191 | * will do nothing. An error check is not needed after every data item |
| 192 | * is added. |
| 193 | * |
| 194 | * Encoding generally proceeds by calling QCBOREncode_Init(), calling |
| 195 | * lots of @c QCBOREncode_AddXxx() functions and calling |
| 196 | * QCBOREncode_Finish(). There are many @c QCBOREncode_AddXxx() |
| 197 | * functions for various data types. The input buffers need only to be |
| 198 | * valid during the @c QCBOREncode_AddXxx() calls as the data is copied |
| 199 | * into the output buffer. |
| 200 | * |
| 201 | * There are three `Add` functions for each data type. The first / main |
| 202 | * one for the type is for adding the data item to an array. The second |
| 203 | * one's name ends in `ToMap`, is used for adding data items to maps and |
| 204 | * takes a string argument that is its label in the map. The third one |
| 205 | * ends in `ToMapN`, is also used for adding data items to maps, and |
| 206 | * takes an integer argument that is its label in the map. |
| 207 | * |
| 208 | * The simplest aggregate type is an array, which is a simple ordered |
| 209 | * set of items without labels the same as JSON arrays. Call |
| 210 | * QCBOREncode_OpenArray() to open a new array, then various @c |
| 211 | * QCBOREncode_AddXxx() functions to put items in the array and then |
| 212 | * QCBOREncode_CloseArray(). Nesting to the limit @ref |
| 213 | * QCBOR_MAX_ARRAY_NESTING is allowed. All opens must be matched by |
| 214 | * closes or an encoding error will be returned. |
| 215 | * |
| 216 | * The other aggregate type is a map which does use labels. The `Add` |
| 217 | * functions that end in `ToMap` and `ToMapN` are convenient ways to add |
| 218 | * labeled data items to a map. You can also call any type of `Add` |
| 219 | * function once to add a label of any type and then call any type of |
| 220 | * `Add` again to add its value. |
| 221 | * |
| 222 | * Note that when you nest arrays or maps in a map, the nested array or |
| 223 | * map has a label. |
| 224 | * |
| 225 | * Many CBOR-based protocols start with an array or map. This makes them |
| 226 | * self-delimiting. No external length or end marker is needed to know |
| 227 | * the end. It is also possible not start this way, in which case this |
| 228 | * it is usually called a CBOR sequence which is described in |
| 229 | * [RFC 8742] (https://tools.ietf.org/html/rfc8742). This encoder supports |
| 230 | * either just by whether the first item added is an array, map or other. |
| 231 | * |
| 232 | * If QCBOR is compiled with QCBOR_DISABLE_ENCODE_USAGE_GUARDS defined, |
| 233 | * the errors QCBOR_ERR_CLOSE_MISMATCH, QCBOR_ERR_ARRAY_TOO_LONG, |
| 234 | * QCBOR_ERR_TOO_MANY_CLOSES, QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN, and |
| 235 | * QCBOR_ERR_ENCODE_UNSUPPORTED will never be returned. It is up to the |
| 236 | * caller to make sure that opened maps, arrays and byte-string wrapping |
| 237 | * is closed correctly and that QCBOREncode_AddType7() is called |
| 238 | * correctly. With this defined, it is easier to make a mistake when |
| 239 | * authoring the encoding of a protocol that will output not well formed |
| 240 | * CBOR, but as long as the calling code is correct, it is safe to |
| 241 | * disable these checks. Bounds checking that prevents security issues |
| 242 | * in the code is still enforced. This define reduces the size of |
| 243 | * encoding object code by about 150 bytes. |
| 244 | * |
| 245 | * @anchor Tags-Overview |
| 246 | * |
| 247 | * ## Tags Overview |
| 248 | * |
| 249 | * Any CBOR data item can be made into a tag to add semantics, define a |
| 250 | * new data type or such. Some tags are fully standardized and some are |
| 251 | * just registered. Others are not registered and used in a proprietary |
| 252 | * way. |
| 253 | * |
| 254 | * Encoding and decoding of many of the registered tags is fully |
| 255 | * implemented by QCBOR. It is also possible to encode and decode tags |
| 256 | * that are not directly supported. For many use cases the built-in tag |
| 257 | * support should be adequate. |
| 258 | * |
| 259 | * For example, the registered epoch date tag is supported in encoding |
| 260 | * by QCBOREncode_AddDateEpoch() and in decoding by @ref |
| 261 | * QCBOR_TYPE_DATE_EPOCH and the @c epochDate member of @ref |
| 262 | * QCBORItem. This is typical of the built-in tag support. There is an |
| 263 | * API to encode data for it and a @c QCBOR_TYPE_XXX when it is decoded. |
| 264 | * |
| 265 | * Tags are registered in the [IANA CBOR Tags Registry] |
| 266 | * (https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml). There |
| 267 | * are roughly three options to create a new tag. First, a public |
| 268 | * specification can be created and the new tag registered with IANA. |
| 269 | * This is the most formal. Second, the new tag can be registered with |
| 270 | * IANA with just a short description rather than a full specification. |
| 271 | * These tags must be greater than 256. Third, a tag can be used without |
| 272 | * any IANA registration, though the registry should be checked to see |
| 273 | * that the new value doesn't collide with one that is registered. The |
| 274 | * value of these tags must be 256 or larger. |
| 275 | * |
| 276 | * See also @ref CBORTags and @ref Tag-Usage |
| 277 | * |
| 278 | * The encoding side of tags not built-in is handled by |
| 279 | * QCBOREncode_AddTag() and is relatively simple. Tag decoding is more |
| 280 | * complex and mainly handled by QCBORDecode_GetNext(). Decoding of the |
| 281 | * structure of tagged data not built-in (if there is any) has to be |
| 282 | * implemented by the caller. |
| 283 | * |
| 284 | * @anchor Floating-Point |
| 285 | * |
| 286 | * ## Floating-Point |
| 287 | * |
| 288 | * By default QCBOR fully supports IEEE 754 floating-point: |
| 289 | * - Encode/decode of double, single and half-precision |
| 290 | * - CBOR preferred serialization of floating-point |
| 291 | * - Floating-point epoch dates |
| 292 | * |
| 293 | * For the most part, the type double is used in the interface for |
| 294 | * floating-point values. In the default configuration, all decoded |
| 295 | * floating-point values are returned as a double. |
| 296 | * |
| 297 | * With CBOR preferred serialization, the encoder outputs the smallest |
| 298 | * representation of the double or float that preserves precision. Zero, |
| 299 | * NaN and infinity are always output as a half-precision, each taking |
| 300 | * just 2 bytes. This reduces the number of bytes needed to encode |
| 301 | * double and single-precision, especially if zero, NaN and infinity are |
| 302 | * frequently used. |
| 303 | * |
| 304 | * To avoid use of preferred serialization in the standard configuration |
| 305 | * when encoding, use QCBOREncode_AddDoubleNoPreferred() or |
| 306 | * QCBOREncode_AddFloatNoPreferred(). |
| 307 | * |
| 308 | * This implementation of preferred floating-point serialization and |
| 309 | * half-precision does not depend on the CPU having floating-point HW or |
| 310 | * the compiler bringing in a (sometimes large) library to compensate |
| 311 | * for lack of CPU support. This implementation uses shifts and masks |
| 312 | * rather than floating-point functions. |
| 313 | * |
| 314 | * To reduce overall object code by about 900 bytes, define |
| 315 | * QCBOR_DISABLE_PREFERRED_FLOAT. This will eliminate all support for |
| 316 | * preferred serialization and half-precision. An error will be returned |
| 317 | * when attempting to decode half-precision. A float will always be |
| 318 | * encoded and decoded as 32-bits and a double will always be encoded |
| 319 | * and decoded as 64 bits. |
| 320 | * |
| 321 | * Note that even if QCBOR_DISABLE_PREFERRED_FLOAT is not defined all |
| 322 | * the float-point encoding object code can be avoided by never calling |
| 323 | * any functions that encode double or float. Just not calling |
| 324 | * floating-point functions will reduce object code by about 500 bytes. |
| 325 | * |
| 326 | * On CPUs that have no floating-point hardware, |
| 327 | * QCBOR_DISABLE_FLOAT_HW_USE should be defined in most cases. If it is |
| 328 | * not, then the compiler will bring in possibly large software |
| 329 | * libraries to compensate. Defining QCBOR_DISABLE_FLOAT_HW_USE reduces |
| 330 | * object code size on CPUs with floating-point hardware by a tiny |
| 331 | * amount and eliminates the need for <math.h> |
| 332 | * |
| 333 | * When QCBOR_DISABLE_FLOAT_HW_USE is defined, trying to decoding |
| 334 | * floating-point dates will give error |
| 335 | * @ref QCBOR_ERR_FLOAT_DATE_DISABLED and decoded single-precision |
| 336 | * numbers will be returned as @ref QCBOR_TYPE_FLOAT instead of |
| 337 | * converting them to double as usual. |
| 338 | * |
| 339 | * If both QCBOR_DISABLE_FLOAT_HW_USE and QCBOR_DISABLE_PREFERRED_FLOAT |
| 340 | * are defined, then the only thing QCBOR can do is encode/decode a C |
| 341 | * float type as 32-bits and a C double type as 64-bits. Floating-point |
| 342 | * epoch dates will be unsupported. |
| 343 | * |
| 344 | * If USEFULBUF_DISABLE_ALL_FLOATis defined, then floating point |
| 345 | * support is completely disabled. Decoding functions return |
| 346 | * @ref QCBOR_ERR_ALL_FLOAT_DISABLED if a floating point value is |
| 347 | * encountered during decoding. Functions that are encoding floating |
| 348 | * point values are not available. |
| 349 | * |
| 350 | * ## Limitations |
| 351 | * |
| 352 | * Summary Limits of this implementation: |
| 353 | * - The entire encoded CBOR must fit into contiguous memory. |
| 354 | * - Max size of encoded / decoded CBOR data is a few bytes less than @c UINT32_MAX (4GB). |
| 355 | * - Max array / map nesting level when encoding / decoding is |
| 356 | * @ref QCBOR_MAX_ARRAY_NESTING (this is typically 15). |
| 357 | * - Max items in an array or map when encoding / decoding is |
| 358 | * @ref QCBOR_MAX_ITEMS_IN_ARRAY (typically 65,536). |
| 359 | * - Does not directly support labels in maps other than text strings & integers. |
| 360 | * - Does not directly support integer labels greater than @c INT64_MAX. |
| 361 | * - Epoch dates limited to @c INT64_MAX (+/- 292 billion years). |
| 362 | * - Exponents for bigfloats and decimal integers are limited to @c INT64_MAX. |
| 363 | * - Tags on labels are ignored during decoding. |
| 364 | * - The maximum tag nesting is @c QCBOR_MAX_TAGS_PER_ITEM (typically 4). |
| 365 | * - Works only on 32- and 64-bit CPUs (modifications could make it work |
| 366 | * on 16-bit CPUs). |
| 367 | * |
| 368 | * The public interface uses @c size_t for all lengths. Internally the |
| 369 | * implementation uses 32-bit lengths by design to use less memory and |
| 370 | * fit structures on the stack. This limits the encoded CBOR it can |
| 371 | * work with to size @c UINT32_MAX (4GB) which should be enough. |
| 372 | * |
| 373 | * This implementation assumes two's compliment integer machines. |
| 374 | * @c <stdint.h> also requires this. It is possible to modify this |
| 375 | * implementation for another integer representation, but all modern |
| 376 | * machines seem to be two's compliment. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 377 | */ |
| 378 | |
| 379 | |
Laurence Lundblade | 825164e | 2020-10-22 20:18:06 -0700 | [diff] [blame] | 380 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 381 | * The size of the buffer to be passed to QCBOREncode_EncodeHead(). It |
| 382 | * is one byte larger than sizeof(uint64_t) + 1, the actual maximum |
| 383 | * size of the head of a CBOR data item because |
| 384 | * QCBOREncode_EncodeHead() needs one extra byte to work. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 385 | */ |
| 386 | #define QCBOR_HEAD_BUFFER_SIZE (sizeof(uint64_t) + 2) |
| 387 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 388 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 389 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 390 | * Output the full CBOR tag. See @ref CBORTags, @ref Tag-Usage and |
| 391 | * @ref Tags-Overview. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 392 | */ |
| 393 | #define QCBOR_ENCODE_AS_TAG 0 |
| 394 | |
| 395 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 396 | * Output only the 'borrowed' content format for the relevant tag. |
| 397 | * See @ref CBORTags, @ref Tag-Usage and @ref Tags-Overview. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 398 | */ |
| 399 | #define QCBOR_ENCODE_AS_BORROWED 1 |
| 400 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 401 | |
| 402 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 403 | * QCBOREncodeContext is the data type that holds context for all the |
| 404 | * encoding functions. It is less than 200 bytes, so it can go on the |
| 405 | * stack. The contents are opaque, and the caller should not access |
| 406 | * internal members. A context may be re used serially as long as it is |
| 407 | * re initialized. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 408 | */ |
| 409 | typedef struct _QCBOREncodeContext QCBOREncodeContext; |
| 410 | |
| 411 | |
| 412 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 413 | * Initialize the encoder to prepare to encode some CBOR. |
| 414 | * |
| 415 | * @param[in,out] pCtx The encoder context to initialize. |
| 416 | * @param[in] Storage The buffer into which the encoded result |
| 417 | * will be written. |
| 418 | * |
| 419 | * Call this once at the start of an encoding of some CBOR. Then call |
| 420 | * the many functions like QCBOREncode_AddInt64() and |
| 421 | * QCBOREncode_AddText() to add the different data items. Finally, |
| 422 | * call QCBOREncode_Finish() to get the pointer and length of the |
| 423 | * encoded result. |
| 424 | * |
| 425 | * The primary purpose of this function is to give the pointer and |
| 426 | * length of the output buffer into which the encoded CBOR will be |
| 427 | * written. This is done with a @ref UsefulBuf structure, which is |
| 428 | * just a pointer and length (it is equivalent to two parameters, one |
| 429 | * a pointer and one a length, but a little prettier). |
| 430 | * |
| 431 | * The output buffer can be allocated any way (malloc, stack, |
| 432 | * static). It is just some memory that QCBOR writes to. The length |
| 433 | * must be the length of the allocated buffer. QCBOR will never write |
| 434 | * past that length, but might write up to that length. If the buffer |
| 435 | * is too small, encoding will go into an error state and not write |
| 436 | * anything further. |
| 437 | * |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 438 | * If allocating on the stack, the convenience macro |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 439 | * UsefulBuf_MAKE_STACK_UB() can be used, but its use is not required. |
| 440 | * |
| 441 | * Since there is no reallocation or such, the output buffer must be |
| 442 | * correctly sized when passed in here. It is OK, but wasteful if it |
| 443 | * is too large. One way to pick the size is to figure out the maximum |
| 444 | * size that will ever be needed and hard code a buffer of that size. |
| 445 | * |
| 446 | * Another way to do it is to have QCBOR calculate it for you. To do |
| 447 | * this, pass @ref SizeCalculateUsefulBuf for @c Storage. Then call |
| 448 | * all the functions to add the CBOR exactly as if encoding for |
| 449 | * real. Finally, call QCBOREncode_FinishGetSize(). Once the length |
| 450 | * is obtained, allocate a buffer of that size, call |
| 451 | * QCBOREncode_Init() again with the real buffer. Call all the add |
| 452 | * functions again and finally, QCBOREncode_Finish() to obtain the |
| 453 | * final result. This uses twice the CPU time, but that is usually not |
| 454 | * an issue. |
| 455 | * |
| 456 | * See QCBOREncode_Finish() for how the pointer and length for the |
| 457 | * encoded CBOR is returned. |
| 458 | * |
| 459 | * For practical purposes QCBOR can't output encoded CBOR larger than |
| 460 | * @c UINT32_MAX (4GB) even on 64-bit CPUs because the internal |
| 461 | * offsets used to track the start of an array/map are 32 bits to |
| 462 | * reduce the size of the encoding context. |
| 463 | * |
| 464 | * A @ref QCBOREncodeContext can be reused over and over as long as |
| 465 | * QCBOREncode_Init() is called before each use. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 466 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 467 | void |
| 468 | QCBOREncode_Init(QCBOREncodeContext *pCtx, UsefulBuf Storage); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 469 | |
| 470 | |
| 471 | /** |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 472 | * @brief Select preferred serialization mode. |
| 473 | * |
| 474 | * @param[in] pCtx The encoding context for mode set. |
| 475 | * |
| 476 | * Setting this mode will cause QCBOR to return an error if an attempt |
| 477 | * is made to use one of the methods that produce non-preferred |
| 478 | * serialization. It doesn't change anything else as QCBOR produces |
| 479 | * preferred serialization by default. |
| 480 | * |
| 481 | * The non-preferred methods are: QCBOREncode_AddFloatNoPreferred(), |
| 482 | * QCBOREncode_AddDoubleNoPreferred(), |
| 483 | * QCBOREncode_OpenArrayIndefiniteLength(), |
| 484 | * QCBOREncode_CloseArrayIndefiniteLength(), |
| 485 | * QCBOREncode_OpenMapIndefiniteLength(), |
| 486 | * QCBOREncode_CloseMapIndefiniteLength(), plus those derived from the |
| 487 | * above listed. |
| 488 | * |
| 489 | * This mode is just a user guard to prevent accidentally calling |
| 490 | * something that produces non-preferred serialization. It doesn't do |
| 491 | * anything but causes errors to occur on attempts to call the above |
| 492 | * listed functions. This does nothing if the library is compiled |
| 493 | * QCBOR_DISABLE_ENCODE_USAGE_GUARDS. |
| 494 | * |
| 495 | * See @ref Serialization. It is usually not necessary to set this |
| 496 | * mode, but there is usually no disadvantage to setting it. Preferred |
| 497 | * Serialization is defined in RFC 8949, section 4.1. |
| 498 | */ |
| 499 | static void |
| 500 | QCBOREncode_SerializationPreferred(QCBOREncodeContext *pCtx); |
| 501 | |
| 502 | |
| 503 | /** |
| 504 | * @brief Select CBOR deterministic encoding mode. |
| 505 | * |
| 506 | * @param[in] pCtx The encoding context for mode set. |
| 507 | |
| 508 | * This causes QCBOR to produce CBOR Deterministic Encoding (CDE). |
| 509 | * With CDE, two distant unrelated CBOR encoders will produce exactly |
| 510 | * the same encoded CBOR for a given input. |
| 511 | * |
| 512 | * In addition to doing everything |
| 513 | * QCBOREncode_SerializationPreferred() does (including exclusion of |
| 514 | * indefinite lengths), this causes maps to be sorted. The map is |
| 515 | * sorted automatically when QCBOREncode_CloseMap() is called. |
| 516 | * QCBOREncode_CloseMap() becomes equivalent to |
| 517 | * QCBOREncode_CloseAndSortMap(). |
| 518 | * |
| 519 | * Note that linking this function causese about 30% more code from |
| 520 | * the QCBOR library to be linked. Also, QCBOREncode_CloseMap() runs |
| 521 | * slower, but this is probably only of consequence in very |
| 522 | * constrained environments. |
| 523 | * |
| 524 | * See @ref Serialization. It is usually not necessary to set this |
| 525 | * mode as determinism is very rarely needed. However it will |
| 526 | * usually work with most protocols. CDE is defined in |
| 527 | * draft-ietf-cbor-cde. |
| 528 | */ |
| 529 | static void |
| 530 | QCBOREncode_SerializationCDE(QCBOREncodeContext *pCtx); |
| 531 | |
| 532 | |
| 533 | /** |
| 534 | * @brief Select "dCBOR" encoding mode. |
| 535 | * |
| 536 | * @param[in] pCtx The encoding context for mode set. |
| 537 | * |
| 538 | * This is a superset of CDE. This function does everything |
| 539 | * QCBOREncode_SerializationCDE() does. Also it is a super set of |
| 540 | * preferred serialization and does everything |
| 541 | * QCBOREncode_SerializationPreferred() does. |
| 542 | * |
| 543 | * The main feature of dCBOR is that there is only one way to serialize a |
| 544 | * particular numeric value. This changes the behavior of functions |
| 545 | * that add floating-point numbers. If the floating-point number is |
| 546 | * whole, it will be encoded as an integer, not a floating-point number. |
| 547 | * 0.000 will be encoded as 0x00. Precision is never lost in this |
| 548 | * conversion. |
| 549 | * |
| 550 | * dCBOR also disallows NaN payloads. QCBOR will allow NaN payloads if |
| 551 | * you pass a NaN to one of the floating-point encoding functions. |
| 552 | * This mode forces all NaNs to the half-precision queit NaN. Also see |
| 553 | * QCBOREncode_Allow(). |
| 554 | * |
| 555 | * dCBOR also disallows 65-bit negative integers. |
| 556 | * |
| 557 | * dCBOR disallows use of any simple type other than true, false and |
| 558 | * NULL. In particular it disallows use of "undef" produced by |
| 559 | * QCBOREncode_AddUndef(). |
| 560 | * |
| 561 | * See @ref Serialization. Set this mode only if the protocol you are |
| 562 | * implementing requires dCBOR. This mode is usually not compatible |
| 563 | * with protocols that don't use dCBOR. dCBOR is defined in |
| 564 | * draft-mcnally-deterministic-cbor. |
| 565 | */ |
| 566 | static void |
| 567 | QCBOREncode_SerializationdCBOR(QCBOREncodeContext *pCtx); |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | /** Bit flag to be passed to QCBOREncode_Allow() to allow NaN payloads |
| 573 | * to be output by QCBOREncode_AddDouble(), |
| 574 | * QCBOREncode_AddDoubleNoPreferred(), QCBORENcode_AddFloat() and |
| 575 | * QCBOREncode_AddSingleleNoPreferred. */ |
| 576 | #define QCBOR_ENCODE_ALLOW_NAN_PAYLOAD 0x01 |
| 577 | |
| 578 | /** Bit flag to be passed to QCBOREncode_Allow() to allow use of |
| 579 | * QCBOREncode_AddNegativeUInt64(). */ |
| 580 | #define QCBOR_ENCODE_ALLOW_65_BIG_NEG 0x02 |
| 581 | |
| 582 | /** Bit flag to be passed to QCBOREncode_Allow() output of less |
| 583 | * interoperable values. See @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD, and |
| 584 | * @ref QCBOR_ENCODE_ALLOW_65_BIG_NEG. */ |
| 585 | #define QCBOR_ENCODE_ALLOW_ALL 0xFF |
| 586 | |
| 587 | |
| 588 | /** |
| 589 | * @brief Allow encoding of less-interoperable values. |
| 590 | * |
| 591 | * @param[in] pCtx The encoding context. |
| 592 | * @param[in] uAllow Bit flags indicating what to allow. |
| 593 | * |
| 594 | * There are a few things in the CBOR standard that are often not |
| 595 | * supported and are thus not very interoperable. By default QCBOR |
| 596 | * will error if you attempt to output them. This disables that |
| 597 | * error. |
| 598 | * |
| 599 | * See @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD and |
| 600 | * @ref QCBOR_ENCODE_ALLOW_65_BIG_NEG. |
| 601 | * |
| 602 | * This does nothing if the library is compiled |
| 603 | * QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 604 | static void |
| 605 | QCBOREncode_Allow(QCBOREncodeContext *pCtx, uint8_t uAllow); |
| 606 | |
| 607 | |
| 608 | |
| 609 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 610 | * @brief Add a signed 64-bit integer to the encoded output. |
| 611 | * |
| 612 | * @param[in] pCtx The encoding context to add the integer to. |
| 613 | * @param[in] nNum The integer to add. |
| 614 | * |
| 615 | * The integer will be encoded and added to the CBOR output. |
| 616 | * |
| 617 | * This function figures out the size and the sign and encodes in the |
| 618 | * correct minimal CBOR. Specifically, it will select CBOR major type |
| 619 | * 0 or 1 based on sign and will encode to 1, 2, 4 or 8 bytes |
| 620 | * depending on the value of the integer. Values less than 24 |
| 621 | * effectively encode to one byte because they are encoded in with the |
| 622 | * CBOR major type. This is a neat and efficient characteristic of |
| 623 | * CBOR that can be taken advantage of when designing CBOR-based |
| 624 | * protocols. If integers like tags can be kept between -23 and 23 |
| 625 | * they will be encoded in one byte including the major type. |
| 626 | * |
| 627 | * If you pass a smaller int, say an @c int16_t or a small value, say |
| 628 | * 100, the encoding will still be CBOR's most compact that can |
| 629 | * represent the value. For example, CBOR always encodes the value 0 |
| 630 | * as one byte, 0x00. The representation as 0x00 includes |
| 631 | * identification of the type as an integer too as the major type for |
| 632 | * an integer is 0. See [RFC 8949] |
| 633 | * (https://tools.ietf.org/html/rfc8949) Appendix A for more examples |
| 634 | * of CBOR encoding. This compact encoding is also preferred |
| 635 | * serialization CBOR as per section 34.1 in RFC 8949. |
| 636 | * |
| 637 | * There are no functions to add @c int16_t or @c int32_t because they |
| 638 | * are not necessary because this always encodes to the smallest |
| 639 | * number of bytes based on the value (If this code is running on a |
| 640 | * 32-bit machine having a way to add 32-bit integers would reduce |
| 641 | * code size some). |
| 642 | * |
| 643 | * If the encoding context is in an error state, this will do |
| 644 | * nothing. If an error occurs when adding this integer, the internal |
| 645 | * error flag will be set, and the error will be returned when |
| 646 | * QCBOREncode_Finish() is called. |
| 647 | * |
| 648 | * See also QCBOREncode_AddUInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 649 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 650 | void |
| 651 | QCBOREncode_AddInt64(QCBOREncodeContext *pCtx, int64_t nNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 652 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 653 | static void |
| 654 | QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 655 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 656 | static void |
| 657 | QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 658 | |
| 659 | |
| 660 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 661 | * @brief Add an unsigned 64-bit integer to the encoded output. |
| 662 | * |
| 663 | * @param[in] pCtx The encoding context to add the integer to. |
| 664 | * @param[in] uNum The integer to add. |
| 665 | * |
| 666 | * The integer will be encoded and added to the CBOR output. |
| 667 | * |
| 668 | * The only reason so use this function is for integers larger than |
| 669 | * @c INT64_MAX and smaller than @c UINT64_MAX. Otherwise |
| 670 | * QCBOREncode_AddInt64() will work fine. |
| 671 | * |
| 672 | * Error handling is the same as for QCBOREncode_AddInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 673 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 674 | void |
| 675 | QCBOREncode_AddUInt64(QCBOREncodeContext *pCtx, uint64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 676 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 677 | static void |
| 678 | QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 679 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 680 | static void |
| 681 | QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 682 | |
| 683 | |
| 684 | /** |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 685 | * @brief Add a negative 64-bit integer to encoded output |
| 686 | * |
| 687 | * @param[in] pCtx The encoding context to add the integer to. |
| 688 | * @param[in] uNum The integer to add. |
| 689 | * |
| 690 | * QCBOREncode_AddInt64() is much better to encode negative integers |
| 691 | * than this. What this can do is add integers with one more |
| 692 | * significant bit than an int64_t (a "65-bit" integer if you count |
| 693 | * the sign as a bit) which is possible because CBOR happens to |
| 694 | * support such integers. |
| 695 | * |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 696 | * Because use of this is discouraged. It must be explicitly allowed |
| 697 | * by passing @ref QCBOR_ENCODE_ALLOW_65_BIG_NEG to a call to |
| 698 | * QCBOREncode_Allow(). |
| 699 | * |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 700 | * The actual value encoded is -uNum - 1. That is, give 0 for uNum to |
| 701 | * transmit -1, give 1 to transmit -2 and give UINT64_MAX to transmit |
| 702 | * -UINT64_MAX-1 (18446744073709551616). The interface is odd like |
| 703 | * this so all negative values CBOR can represent can be encoded by |
| 704 | * QCBOR (making this a complete CBOR implementation). |
| 705 | * |
| 706 | * The most negative value QCBOREncode_AddInt64() can encode is |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 707 | * -9223372036854775808 which is -2^63 or negative 0x800000000000. |
| 708 | * This can encode from -9223372036854775809 to -18446744073709551616 |
| 709 | * or -2^63 - 1 to -2^64. Note that it is not possible to represent |
| 710 | * positive or negative 18446744073709551616 in any standard C data |
| 711 | * type. |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 712 | * |
| 713 | * Negative integers are normally decoded in QCBOR with type |
| 714 | * @ref QCBOR_TYPE_INT64. Integers in the range of -9223372036854775809 |
| 715 | * to -18446744073709551616 are returned as @ref QCBOR_TYPE_65BIT_NEG_INT. |
| 716 | * |
| 717 | * WARNING: some CBOR decoders will be unable to decode -2^63 - 1 to |
| 718 | * -2^64. Also, most CPUs do not have registers that can represent |
| 719 | * this range. If you need 65-bit negative integers, you likely need |
| 720 | * negative 66, 67 and 68-bit negative integers so it is likely better |
| 721 | * to use CBOR big numbers where you can have any number of bits. See |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 722 | * QCBOREncode_AddTNegativeBignum() and @ref Serialization. |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 723 | */ |
| 724 | void |
| 725 | QCBOREncode_AddNegativeUInt64(QCBOREncodeContext *pCtx, uint64_t uNum); |
| 726 | |
| 727 | static void |
| 728 | QCBOREncode_AddNegativeUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum); |
| 729 | |
| 730 | static void |
| 731 | QCBOREncode_AddNegativeUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum); |
| 732 | |
| 733 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 734 | * @brief Add a UTF-8 text string to the encoded output. |
| 735 | * |
| 736 | * @param[in] pCtx The encoding context to add the text to. |
| 737 | * @param[in] Text Pointer and length of text to add. |
| 738 | * |
| 739 | * The text passed in must be unencoded UTF-8 according to [RFC 3629] |
| 740 | * (https://tools.ietf.org/html/rfc3629). There is no NULL |
| 741 | * termination. The text is added as CBOR major type 3. |
| 742 | * |
| 743 | * If called with @c nBytesLen equal to 0, an empty string will be |
| 744 | * added. When @c nBytesLen is 0, @c pBytes may be @c NULL. |
| 745 | * |
| 746 | * Note that the restriction of the buffer length to a @c uint32_t is |
| 747 | * entirely intentional as this encoder is not capable of encoding |
| 748 | * lengths greater. This limit to 4GB for a text string should not be |
| 749 | * a problem. |
| 750 | * |
| 751 | * Text lines in Internet protocols (on the wire) are delimited by |
| 752 | * either a CRLF or just an LF. Officially many protocols specify |
| 753 | * CRLF, but implementations often work with either. CBOR type 3 text |
| 754 | * can be either line ending, even a mixture of both. |
| 755 | * |
| 756 | * Operating systems usually have a line end convention. Windows uses |
| 757 | * CRLF. Linux and MacOS use LF. Some applications on a given OS may |
| 758 | * work with either and some may not. |
| 759 | * |
| 760 | * The majority of use cases and CBOR protocols using type 3 text will |
| 761 | * work with either line ending. However, some use cases or protocols |
| 762 | * may not work with either in which case translation to and/or from |
| 763 | * the local line end convention, typically that of the OS, is |
| 764 | * necessary. |
| 765 | * |
| 766 | * QCBOR does no line ending translation for type 3 text when encoding |
| 767 | * and decoding. |
| 768 | * |
| 769 | * Error handling is the same as QCBOREncode_AddInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 770 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 771 | static void |
| 772 | QCBOREncode_AddText(QCBOREncodeContext *pCtx, UsefulBufC Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 773 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 774 | static void |
| 775 | QCBOREncode_AddTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 776 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 777 | static void |
| 778 | QCBOREncode_AddTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 779 | |
| 780 | |
| 781 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 782 | * @brief Add a UTF-8 text string to the encoded output. |
| 783 | * |
| 784 | * @param[in] pCtx The encoding context to add the text to. |
| 785 | * @param[in] szString Null-terminated text to add. |
| 786 | * |
| 787 | * This works the same as QCBOREncode_AddText(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 788 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 789 | static void |
| 790 | QCBOREncode_AddSZString(QCBOREncodeContext *pCtx, const char *szString); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 791 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 792 | static void |
| 793 | QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szString); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 794 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 795 | static void |
| 796 | QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szString); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 797 | |
| 798 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 799 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 800 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 801 | * @brief Add a double-precision floating-point number to the encoded output. |
| 802 | * |
| 803 | * @param[in] pCtx The encoding context to add the double to. |
| 804 | * @param[in] dNum The double-precision number to add. |
| 805 | * |
| 806 | * This encodes and outputs a floating-point number. CBOR major type 7 |
| 807 | * is used. |
| 808 | * |
| 809 | * This implements preferred serialization, selectively encoding the |
| 810 | * double-precision floating-point number as either double-precision, |
| 811 | * single-precision or half-precision. Infinity, NaN and 0 are always |
| 812 | * encoded as half-precision. If no precision will be lost in the |
| 813 | * conversion to half-precision, then it will be converted and |
| 814 | * encoded. If not and no precision will be lost in conversion to |
| 815 | * single-precision, then it will be converted and encoded. If not, |
| 816 | * then no conversion is performed, and it encoded as a |
| 817 | * double-precision. |
| 818 | * |
| 819 | * Half-precision floating-point numbers take up 2 bytes, half that of |
| 820 | * single-precision, one quarter of double-precision |
| 821 | * |
| 822 | * This automatically reduces the size of encoded CBOR, maybe even by |
| 823 | * four if most of values are 0, infinity or NaN. |
| 824 | * |
| 825 | * When decoded, QCBOR will usually return these values as |
| 826 | * double-precision. |
| 827 | * |
| 828 | * It is possible to disable this preferred serialization when compiling |
| 829 | * QCBOR. In that case, this functions the same as |
| 830 | * QCBOREncode_AddDoubleNoPreferred(). |
| 831 | * |
| 832 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 833 | * |
| 834 | * See also QCBOREncode_AddDoubleNoPreferred(), QCBOREncode_AddFloat() |
| 835 | * and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point. |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 836 | * |
| 837 | * By default, this will error out on an attempt to encode a NaN with |
| 838 | * a payload. See QCBOREncode_Allow() and @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 839 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 840 | void |
| 841 | QCBOREncode_AddDouble(QCBOREncodeContext *pCtx, double dNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 842 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 843 | static void |
| 844 | QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 845 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 846 | static void |
| 847 | QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 848 | |
| 849 | |
| 850 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 851 | * @brief Add a single-precision floating-point number to the encoded output. |
| 852 | * |
| 853 | * @param[in] pCtx The encoding context to add the double to. |
| 854 | * @param[in] fNum The single-precision number to add. |
| 855 | * |
| 856 | * This is identical to QCBOREncode_AddDouble() except the input is |
| 857 | * single-precision. |
| 858 | * |
| 859 | * See also QCBOREncode_AddDouble(), QCBOREncode_AddDoubleNoPreferred(), |
| 860 | * and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point. |
| 861 | */ |
| 862 | void |
| 863 | QCBOREncode_AddFloat(QCBOREncodeContext *pCtx, float fNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 864 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 865 | static void |
| 866 | QCBOREncode_AddFloatToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 867 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 868 | static void |
| 869 | QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float dNum); |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 870 | |
| 871 | |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 872 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 873 | * @brief Add a double-precision floating-point number without preferred encoding. |
| 874 | * |
| 875 | * @param[in] pCtx The encoding context to add the double to. |
| 876 | * @param[in] dNum The double-precision number to add. |
| 877 | * |
| 878 | * This always outputs the number as a 64-bit double-precision. |
| 879 | * Preferred serialization is not used. |
| 880 | * |
| 881 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 882 | * |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 883 | * By default, this will error out on an attempt to encode a NaN with |
| 884 | * a payload. See QCBOREncode_Allow() and @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD. |
| 885 | * |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 886 | * See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and |
| 887 | * QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point. |
| 888 | */ |
| 889 | void |
| 890 | QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pCtx, double dNum); |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 891 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 892 | static void |
| 893 | QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 894 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 895 | static void |
| 896 | QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 897 | |
| 898 | |
| 899 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 900 | * @brief Add a single-precision floating-point number without preferred encoding. |
| 901 | * |
| 902 | * @param[in] pCtx The encoding context to add the double to. |
| 903 | * @param[in] fNum The single-precision number to add. |
| 904 | * |
| 905 | * This always outputs the number as a 32-bit single-precision. |
| 906 | * Preferred serialization is not used. |
| 907 | * |
| 908 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 909 | * |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 910 | * By default, this will error out on an attempt to encode a NaN with |
| 911 | * a payload. See QCBOREncode_Allow() and @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD. |
| 912 | * |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 913 | * See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and |
| 914 | * QCBOREncode_AddDoubleNoPreferred() and @ref Floating-Point. |
| 915 | */ |
| 916 | void |
| 917 | QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pCtx, float fNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 918 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 919 | static void |
| 920 | QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 921 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 922 | static void |
| 923 | QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float fNum); |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 924 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 925 | |
| 926 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 927 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 928 | * @brief Add an optional tag. |
| 929 | * |
| 930 | * @param[in] pCtx The encoding context to add the tag to. |
| 931 | * @param[in] uTag The tag to add |
| 932 | * |
| 933 | * This outputs a CBOR major type 6 item that tags the next data item |
| 934 | * that is output usually to indicate it is some new data type. |
| 935 | * |
| 936 | * For many of the common standard tags, a function to encode data |
| 937 | * using it is provided and this is not needed. For example, |
| 938 | * QCBOREncode_AddDateEpoch() already exists to output integers |
| 939 | * representing dates with the right tag. |
| 940 | * |
| 941 | * The tag is applied to the next data item added to the encoded |
| 942 | * output. That data item that is to be tagged can be of any major |
| 943 | * CBOR type. Any number of tags can be added to a data item by |
| 944 | * calling this multiple times before the data item is added. |
| 945 | * |
| 946 | * See @ref Tags-Overview for discussion of creating new non-standard |
| 947 | * tags. See QCBORDecode_GetNext() for discussion of decoding custom |
| 948 | * tags. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 949 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 950 | void |
| 951 | QCBOREncode_AddTag(QCBOREncodeContext *pCtx, uint64_t uTag); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 952 | |
| 953 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 954 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 955 | * @brief Add an epoch-based date. |
| 956 | * |
| 957 | * @param[in] pCtx The encoding context to add the date to. |
| 958 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 959 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 960 | * @param[in] nDate Number of seconds since 1970-01-01T00:00Z |
| 961 | * in UTC time. |
| 962 | * |
| 963 | * As per RFC 8949 this is similar to UNIX/Linux/POSIX dates. This is |
| 964 | * the most compact way to specify a date and time in CBOR. Note that |
| 965 | * this is always UTC and does not include the time zone. Use |
| 966 | * QCBOREncode_AddDateString() if you want to include the time zone. |
| 967 | * |
| 968 | * The preferred integer serialization rules apply here so the date will be |
| 969 | * encoded in a minimal number of bytes. Until about the year 2106 |
| 970 | * these dates will encode in 6 bytes -- one byte for the tag, one |
| 971 | * byte for the type and 4 bytes for the integer. After that it will |
| 972 | * encode to 10 bytes. |
| 973 | * |
| 974 | * Negative values are supported for dates before 1970. |
| 975 | * |
| 976 | * If you care about leap-seconds and that level of accuracy, make sure |
| 977 | * the system you are running this code on does it correctly. This code |
| 978 | * just takes the value passed in. |
| 979 | * |
| 980 | * This implementation cannot encode fractional seconds using float or |
| 981 | * double even though that is allowed by CBOR, but you can encode them |
| 982 | * if you want to by calling QCBOREncode_AddTag() and QCBOREncode_AddDouble(). |
| 983 | * |
| 984 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 985 | * |
| 986 | * See also QCBOREncode_AddTDaysEpoch(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 987 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 988 | static void |
| 989 | QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pCtx, |
| 990 | uint8_t uTagRequirement, |
| 991 | int64_t nDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 992 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 993 | static void |
| 994 | QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pCtx, |
| 995 | const char *szLabel, |
| 996 | uint8_t uTagRequirement, |
| 997 | int64_t nDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 998 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 999 | static void |
| 1000 | QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pCtx, |
| 1001 | int64_t nLabel, |
| 1002 | uint8_t uTagRequirement, |
| 1003 | int64_t nDate); |
| 1004 | |
| 1005 | |
| 1006 | static void |
| 1007 | QCBOREncode_AddDateEpoch(QCBOREncodeContext *pCtx, |
| 1008 | int64_t nDate); |
| 1009 | |
| 1010 | static void |
| 1011 | QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pCtx, |
| 1012 | const char *szLabel, |
| 1013 | int64_t nDate); |
| 1014 | |
| 1015 | static void |
| 1016 | QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pCtx, |
| 1017 | int64_t nLabel, |
| 1018 | int64_t nDate); |
| 1019 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1020 | |
| 1021 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1022 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1023 | * @brief Add an epoch-based day-count date. |
| 1024 | * |
| 1025 | * @param[in] pCtx The encoding context to add the date to. |
| 1026 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1027 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1028 | * @param[in] nDays Number of days before or after 1970-01-0. |
| 1029 | * |
| 1030 | * This date format is described in |
| 1031 | * [RFC 8943] (https://tools.ietf.org/html/rfc8943). |
| 1032 | * |
| 1033 | * The preferred integer serialization rules apply here so the date |
| 1034 | * will be encoded in a minimal number of bytes. Until about the year |
| 1035 | * 2149 these dates will encode in 4 bytes -- one byte for the tag, |
| 1036 | * one byte for the type and 2 bytes for the integer. |
| 1037 | * |
| 1038 | * See also QCBOREncode_AddTDateEpoch(). |
| 1039 | */ |
| 1040 | static void |
| 1041 | QCBOREncode_AddTDaysEpoch(QCBOREncodeContext *pCtx, |
| 1042 | uint8_t uTagRequirement, |
| 1043 | int64_t nDays); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1044 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1045 | static void |
| 1046 | QCBOREncode_AddTDaysEpochToMapSZ(QCBOREncodeContext *pCtx, |
| 1047 | const char *szLabel, |
| 1048 | uint8_t uTagRequirement, |
| 1049 | int64_t nDays); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1050 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1051 | static void |
| 1052 | QCBOREncode_AddTDaysEpochToMapN(QCBOREncodeContext *pCtx, |
| 1053 | int64_t nLabel, |
| 1054 | uint8_t uTagRequirement, |
| 1055 | int64_t nDays); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1056 | |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1057 | |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1058 | |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1059 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1060 | /** |
| 1061 | * @brief Add a byte string to the encoded output. |
| 1062 | * |
| 1063 | * @param[in] pCtx The encoding context to add the bytes to. |
| 1064 | * @param[in] Bytes Pointer and length of the input data. |
| 1065 | * |
| 1066 | * Simply adds the bytes to the encoded output as CBOR major type 2. |
| 1067 | * |
| 1068 | * If called with @c Bytes.len equal to 0, an empty string will be |
| 1069 | * added. When @c Bytes.len is 0, @c Bytes.ptr may be @c NULL. |
| 1070 | * |
| 1071 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 1072 | */ |
| 1073 | static void |
| 1074 | QCBOREncode_AddBytes(QCBOREncodeContext *pCtx, UsefulBufC Bytes); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1075 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1076 | static void |
| 1077 | QCBOREncode_AddBytesToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes); |
| 1078 | |
| 1079 | static void |
| 1080 | QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes); |
| 1081 | |
| 1082 | |
| 1083 | /** |
| 1084 | * @brief Set up to write a byte string value directly to encoded output. |
| 1085 | * |
| 1086 | * @param[in] pCtx The encoding context to add the bytes to. |
| 1087 | * @param[out] pPlace Pointer and length of place to write byte string value. |
| 1088 | * |
| 1089 | * QCBOREncode_AddBytes() is the normal way to encode a byte string. |
| 1090 | * This is for special cases and by passes some of the pointer safety. |
| 1091 | * |
| 1092 | * The purpose of this is to output the bytes that make up a byte |
| 1093 | * string value directly to the QCBOR output buffer so you don't need |
| 1094 | * to have a copy of it in memory. This is particularly useful if the |
| 1095 | * byte string is large, for example, the encrypted payload of a |
| 1096 | * COSE_Encrypt message. The payload encryption algorithm can output |
| 1097 | * directly to the encoded CBOR buffer, perhaps by making it the |
| 1098 | * output buffer for some function (e.g. symmetric encryption) or by |
| 1099 | * multiple writes. |
| 1100 | * |
| 1101 | * The pointer in @c pPlace is where to start writing. Writing is just |
| 1102 | * copying bytes to the location by the pointer in \c pPlace. Writing |
| 1103 | * past the length in @c pPlace will be writing off the end of the |
| 1104 | * output buffer. |
| 1105 | * |
| 1106 | * If there is no room in the output buffer @ref NULLUsefulBuf will be |
| 1107 | * returned and there is no need to call QCBOREncode_CloseBytes(). |
| 1108 | * |
| 1109 | * The byte string must be closed by calling QCBOREncode_CloseBytes(). |
| 1110 | * |
| 1111 | * Warning: this bypasses some of the usual checks provided by QCBOR |
| 1112 | * against writing off the end of the encoded output buffer. |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1113 | */ |
| 1114 | void |
| 1115 | QCBOREncode_OpenBytes(QCBOREncodeContext *pCtx, UsefulBuf *pPlace); |
| 1116 | |
| 1117 | static void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1118 | QCBOREncode_OpenBytesInMapSZ(QCBOREncodeContext *pCtx, |
| 1119 | const char *szLabel, |
| 1120 | UsefulBuf *pPlace); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1121 | |
| 1122 | static void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1123 | QCBOREncode_OpenBytesInMapN(QCBOREncodeContext *pCtx, |
| 1124 | int64_t nLabel, |
| 1125 | UsefulBuf *pPlace); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1126 | |
| 1127 | |
| 1128 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1129 | * @brief Close out a byte string written directly to encoded output. |
| 1130 | * |
| 1131 | * @param[in] pCtx The encoding context to add the bytes to. |
| 1132 | * @param[out] uAmount The number of bytes written, the length of the |
| 1133 | * byte string. |
| 1134 | * |
| 1135 | * This closes out a call to QCBOREncode_OpenBytes(). This inserts a |
| 1136 | * CBOR header at the front of the byte string value to make it a |
| 1137 | * well-formed byte string. |
| 1138 | * |
| 1139 | * If there was no call to QCBOREncode_OpenBytes() then @ref |
| 1140 | * QCBOR_ERR_TOO_MANY_CLOSES is set. |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1141 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1142 | void |
| 1143 | QCBOREncode_CloseBytes(QCBOREncodeContext *pCtx, size_t uAmount); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1144 | |
| 1145 | |
| 1146 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1147 | * @brief Add a binary UUID to the encoded output. |
| 1148 | * |
| 1149 | * @param[in] pCtx The encoding context to add the UUID to. |
| 1150 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1151 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1152 | * @param[in] Bytes Pointer and length of the binary UUID. |
| 1153 | * |
| 1154 | * A binary UUID as defined in [RFC 4122] |
| 1155 | * (https://tools.ietf.org/html/rfc4122) is added to the output. |
| 1156 | * |
| 1157 | * It is output as CBOR major type 2, a binary string, with tag @ref |
| 1158 | * CBOR_TAG_BIN_UUID indicating the binary string is a UUID. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1159 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1160 | static void |
| 1161 | QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pCtx, |
| 1162 | uint8_t uTagRequirement, |
| 1163 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1164 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1165 | static void |
| 1166 | QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pCtx, |
| 1167 | const char *szLabel, |
| 1168 | uint8_t uTagRequirement, |
| 1169 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1170 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1171 | static void |
| 1172 | QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pCtx, |
| 1173 | int64_t nLabel, |
| 1174 | uint8_t uTagRequirement, |
| 1175 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1176 | |
| 1177 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1178 | static void |
| 1179 | QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pCtx, UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1180 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1181 | static void |
| 1182 | QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1183 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1184 | static void |
| 1185 | QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1186 | |
| 1187 | |
| 1188 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1189 | * @brief Add a positive big number to the encoded output. |
| 1190 | * |
| 1191 | * @param[in] pCtx The encoding context to add the big number to. |
| 1192 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1193 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1194 | * @param[in] Bytes Pointer and length of the big number. |
| 1195 | * |
| 1196 | * Big numbers are integers larger than 64-bits. Their format is |
| 1197 | * described in [RFC 8949] (https://tools.ietf.org/html/rfc8949). |
| 1198 | * |
| 1199 | * It is output as CBOR major type 2, a binary string, with tag |
| 1200 | * @ref CBOR_TAG_POS_BIGNUM indicating the binary string is a positive |
| 1201 | * big number. |
| 1202 | * |
| 1203 | * Often big numbers are used to represent cryptographic keys, |
| 1204 | * however, COSE which defines representations for keys chose not to |
| 1205 | * use this particular type. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1206 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1207 | static void |
| 1208 | QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pCtx, |
| 1209 | uint8_t uTagRequirement, |
| 1210 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1211 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1212 | static void |
| 1213 | QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pCtx, |
| 1214 | const char *szLabel, |
| 1215 | uint8_t uTagRequirement, |
| 1216 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1217 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1218 | static void |
| 1219 | QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pCtx, |
| 1220 | int64_t nLabel, |
| 1221 | uint8_t uTagRequirement, |
| 1222 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1223 | |
| 1224 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1225 | static void |
| 1226 | QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx, |
| 1227 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1228 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1229 | static void |
| 1230 | QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx, |
| 1231 | const char *szLabel, |
| 1232 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1233 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1234 | static void |
| 1235 | QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx, |
| 1236 | int64_t nLabel, |
| 1237 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1238 | |
| 1239 | |
| 1240 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1241 | * @brief Add a negative big number to the encoded output. |
| 1242 | * |
| 1243 | * @param[in] pCtx The encoding context to add the big number to. |
| 1244 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1245 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1246 | * @param[in] Bytes Pointer and length of the big number. |
| 1247 | * |
| 1248 | * Big numbers are integers larger than 64-bits. Their format is |
| 1249 | * described in [RFC 8949] (https://tools.ietf.org/html/rfc8949). |
| 1250 | * |
| 1251 | * It is output as CBOR major type 2, a binary string, with tag |
| 1252 | * @ref CBOR_TAG_NEG_BIGNUM indicating the binary string is a negative |
| 1253 | * big number. |
| 1254 | * |
| 1255 | * Often big numbers are used to represent cryptographic keys, |
| 1256 | * however, COSE which defines representations for keys chose not to |
| 1257 | * use this particular type. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1258 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1259 | static void |
| 1260 | QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pCtx, |
| 1261 | uint8_t uTagRequirement, |
| 1262 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1263 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1264 | static void |
| 1265 | QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pCtx, |
| 1266 | const char *szLabel, |
| 1267 | uint8_t uTagRequirement, |
| 1268 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1269 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1270 | static void |
| 1271 | QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pCtx, |
| 1272 | int64_t nLabel, |
| 1273 | uint8_t uTagRequirement, |
| 1274 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1275 | |
| 1276 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1277 | static void |
| 1278 | QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx, |
| 1279 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1280 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1281 | static void |
| 1282 | QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx, |
| 1283 | const char *szLabel, |
| 1284 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1285 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1286 | static void |
| 1287 | QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx, |
| 1288 | int64_t nLabel, |
| 1289 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1290 | |
| 1291 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 1292 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1293 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1294 | * @brief Add a decimal fraction to the encoded output. |
| 1295 | * |
| 1296 | * @param[in] pCtx Encoding context to add the decimal fraction to. |
| 1297 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1298 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1299 | * @param[in] nMantissa The mantissa. |
| 1300 | * @param[in] nBase10Exponent The exponent. |
| 1301 | * |
| 1302 | * The value is nMantissa * 10 ^ nBase10Exponent. |
| 1303 | * |
| 1304 | * A decimal fraction is good for exact representation of some values |
| 1305 | * that can't be represented exactly with standard C (IEEE 754) |
| 1306 | * floating-point numbers. Much larger and much smaller numbers can |
| 1307 | * also be represented than floating-point because of the larger |
| 1308 | * number of bits in the exponent. |
| 1309 | * |
| 1310 | * The decimal fraction is conveyed as two integers, a mantissa and a |
| 1311 | * base-10 scaling factor. |
| 1312 | * |
| 1313 | * For example, 273.15 is represented by the two integers 27315 and -2. |
| 1314 | * |
| 1315 | * The exponent and mantissa have the range from @c INT64_MIN to |
| 1316 | * @c INT64_MAX for both encoding and decoding (CBOR allows |
| 1317 | * @c -UINT64_MAX to @c UINT64_MAX, but this implementation doesn't |
| 1318 | * support this range to reduce code size and interface complexity a |
| 1319 | * little). |
| 1320 | * |
| 1321 | * CBOR Preferred serialization of the integers is used, thus they |
| 1322 | * will be encoded in the smallest number of bytes possible. |
| 1323 | * |
| 1324 | * See also QCBOREncode_AddDecimalFractionBigNum() for a decimal |
| 1325 | * fraction with arbitrarily large precision and |
| 1326 | * QCBOREncode_AddBigFloat(). |
| 1327 | * |
| 1328 | * There is no representation of positive or negative infinity or NaN |
| 1329 | * (Not a Number). Use QCBOREncode_AddDouble() to encode them. |
| 1330 | * |
| 1331 | * See @ref expAndMantissa for decoded representation. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1332 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1333 | static void |
| 1334 | QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pCtx, |
| 1335 | uint8_t uTagRequirement, |
| 1336 | int64_t nMantissa, |
| 1337 | int64_t nBase10Exponent); |
| 1338 | |
| 1339 | static void |
| 1340 | QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pCtx, |
| 1341 | const char *szLabel, |
| 1342 | uint8_t uTagRequirement, |
| 1343 | int64_t nMantissa, |
| 1344 | int64_t nBase10Exponent); |
| 1345 | |
| 1346 | static void |
| 1347 | QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pCtx, |
| 1348 | int64_t nLabel, |
| 1349 | uint8_t uTagRequirement, |
| 1350 | int64_t nMantissa, |
| 1351 | int64_t nBase10Exponent); |
| 1352 | |
| 1353 | |
| 1354 | static void |
| 1355 | QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pCtx, |
| 1356 | int64_t nMantissa, |
| 1357 | int64_t nBase10Exponent); |
| 1358 | |
| 1359 | static void |
| 1360 | QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx, |
| 1361 | const char *szLabel, |
| 1362 | int64_t nMantissa, |
| 1363 | int64_t nBase10Exponent); |
| 1364 | |
| 1365 | static void |
| 1366 | QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pCtx, |
| 1367 | int64_t nLabel, |
| 1368 | int64_t nMantissa, |
| 1369 | int64_t nBase10Exponent); |
| 1370 | |
| 1371 | |
| 1372 | /** |
| 1373 | * @brief Add a decimal fraction with a big number mantissa to the encoded output. |
| 1374 | * |
| 1375 | * @param[in] pCtx Encoding context to add the decimal fraction to. |
| 1376 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1377 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1378 | * @param[in] Mantissa The mantissa. |
| 1379 | * @param[in] bIsNegative false if mantissa is positive, true if negative. |
| 1380 | * @param[in] nBase10Exponent The exponent. |
| 1381 | * |
| 1382 | * This is the same as QCBOREncode_AddDecimalFraction() except the |
| 1383 | * mantissa is a big number (See QCBOREncode_AddPositiveBignum()) |
| 1384 | * allowing for arbitrarily large precision. |
| 1385 | * |
| 1386 | * See @ref expAndMantissa for decoded representation. |
| 1387 | */ |
| 1388 | static void |
| 1389 | QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pCtx, |
| 1390 | uint8_t uTagRequirement, |
| 1391 | UsefulBufC Mantissa, |
| 1392 | bool bIsNegative, |
| 1393 | int64_t nBase10Exponent); |
| 1394 | |
| 1395 | static void |
| 1396 | QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx, |
| 1397 | const char *szLabel, |
| 1398 | uint8_t uTagRequirement, |
| 1399 | UsefulBufC Mantissa, |
| 1400 | bool bIsNegative, |
| 1401 | int64_t nBase10Exponent); |
| 1402 | |
| 1403 | static void |
| 1404 | QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx, |
| 1405 | int64_t nLabel, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1406 | uint8_t uTagRequirement, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1407 | UsefulBufC Mantissa, |
| 1408 | bool bIsNegative, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1409 | int64_t nBase10Exponent); |
| 1410 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1411 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1412 | static void |
| 1413 | QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pCtx, |
| 1414 | UsefulBufC Mantissa, |
| 1415 | bool bIsNegative, |
| 1416 | int64_t nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1417 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1418 | static void |
| 1419 | QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx, |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 1420 | const char *szLabel, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1421 | UsefulBufC Mantissa, |
| 1422 | bool bIsNegative, |
| 1423 | int64_t nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1424 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1425 | static void |
| 1426 | QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1427 | int64_t nLabel, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1428 | UsefulBufC Mantissa, |
| 1429 | bool bIsNegative, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1430 | int64_t nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1431 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1432 | /** |
| 1433 | * @brief Add a big floating-point number to the encoded output. |
| 1434 | * |
| 1435 | * @param[in] pCtx The encoding context to add the bigfloat to. |
| 1436 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1437 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1438 | * @param[in] nMantissa The mantissa. |
| 1439 | * @param[in] nBase2Exponent The exponent. |
| 1440 | * |
| 1441 | * The value is nMantissa * 2 ^ nBase2Exponent. |
| 1442 | * |
| 1443 | * "Bigfloats", as CBOR terms them, are similar to IEEE floating-point |
| 1444 | * numbers in having a mantissa and base-2 exponent, but they are not |
| 1445 | * supported by hardware or encoded the same. They explicitly use two |
| 1446 | * CBOR-encoded integers to convey the mantissa and exponent, each of |
| 1447 | * which can be 8, 16, 32 or 64 bits. With both the mantissa and |
| 1448 | * exponent 64 bits they can express more precision and a larger range |
| 1449 | * than an IEEE double floating-point number. See |
| 1450 | * QCBOREncode_AddBigFloatBigNum() for even more precision. |
| 1451 | * |
| 1452 | * For example, 1.5 would be represented by a mantissa of 3 and an |
| 1453 | * exponent of -1. |
| 1454 | * |
| 1455 | * The exponent and mantissa have the range from @c INT64_MIN to |
| 1456 | * @c INT64_MAX for both encoding and decoding (CBOR allows @c |
| 1457 | * -UINT64_MAX to @c UINT64_MAX, but this implementation doesn't |
| 1458 | * support this range to reduce code size and interface complexity a |
| 1459 | * little). |
| 1460 | * |
| 1461 | * CBOR preferred serialization of the integers is used, thus they will |
| 1462 | * be encoded in the smallest number of bytes possible. |
| 1463 | * |
| 1464 | * This can also be used to represent floating-point numbers in |
| 1465 | * environments that don't support IEEE 754. |
| 1466 | * |
| 1467 | * See @ref expAndMantissa for decoded representation. |
| 1468 | */ |
| 1469 | static void |
| 1470 | QCBOREncode_AddTBigFloat(QCBOREncodeContext *pCtx, |
| 1471 | uint8_t uTagRequirement, |
| 1472 | int64_t nMantissa, |
| 1473 | int64_t nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1474 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1475 | static void |
| 1476 | QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pCtx, |
| 1477 | const char *szLabel, |
| 1478 | uint8_t uTagRequirement, |
| 1479 | int64_t nMantissa, |
| 1480 | int64_t nBase2Exponent); |
| 1481 | |
| 1482 | static void |
| 1483 | QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pCtx, |
| 1484 | int64_t nLabel, |
| 1485 | uint8_t uTagRequirement, |
| 1486 | int64_t nMantissa, |
| 1487 | int64_t nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1488 | |
| 1489 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1490 | static void |
| 1491 | QCBOREncode_AddBigFloat(QCBOREncodeContext *pCtx, |
| 1492 | int64_t nMantissa, |
| 1493 | int64_t nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1494 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1495 | static void |
| 1496 | QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pCtx, |
| 1497 | const char *szLabel, |
| 1498 | int64_t nMantissa, |
| 1499 | int64_t nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1500 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1501 | static void |
| 1502 | QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pCtx, |
| 1503 | int64_t nLabel, |
| 1504 | int64_t nMantissa, |
| 1505 | int64_t nBase2Exponent); |
| 1506 | |
| 1507 | /** |
| 1508 | * @brief Add a big floating-point number with a big number mantissa to |
| 1509 | * the encoded output. |
| 1510 | * |
| 1511 | * @param[in] pCtx The encoding context to add the bigfloat to. |
| 1512 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1513 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1514 | * @param[in] Mantissa The mantissa. |
| 1515 | * @param[in] bIsNegative false if mantissa is positive, true if negative. |
| 1516 | * @param[in] nBase2Exponent The exponent. |
| 1517 | * |
| 1518 | * This is the same as QCBOREncode_AddBigFloat() except the mantissa |
| 1519 | * is a big number (See QCBOREncode_AddPositiveBignum()) allowing for |
| 1520 | * arbitrary precision. |
| 1521 | * |
| 1522 | * See @ref expAndMantissa for decoded representation. |
| 1523 | */ |
| 1524 | static void |
| 1525 | QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pCtx, |
| 1526 | uint8_t uTagRequirement, |
| 1527 | UsefulBufC Mantissa, |
| 1528 | bool bIsNegative, |
| 1529 | int64_t nBase2Exponent); |
| 1530 | |
| 1531 | static void |
| 1532 | QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pCtx, |
| 1533 | const char *szLabel, |
| 1534 | uint8_t uTagRequirement, |
| 1535 | UsefulBufC Mantissa, |
| 1536 | bool bIsNegative, |
| 1537 | int64_t nBase2Exponent); |
| 1538 | |
| 1539 | static void |
| 1540 | QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pCtx, |
| 1541 | int64_t nLabel, |
| 1542 | uint8_t uTagRequirement, |
| 1543 | UsefulBufC Mantissa, |
| 1544 | bool bIsNegative, |
| 1545 | int64_t nBase2Exponent); |
| 1546 | |
| 1547 | |
| 1548 | static void |
| 1549 | QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pCtx, |
| 1550 | UsefulBufC Mantissa, |
| 1551 | bool bIsNegative, |
| 1552 | int64_t nBase2Exponent); |
| 1553 | |
| 1554 | static void |
| 1555 | QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pCtx, |
| 1556 | const char *szLabel, |
| 1557 | UsefulBufC Mantissa, |
| 1558 | bool bIsNegative, |
| 1559 | int64_t nBase2Exponent); |
| 1560 | |
| 1561 | static void |
| 1562 | QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pCtx, |
| 1563 | int64_t nLabel, |
| 1564 | UsefulBufC Mantissa, |
| 1565 | bool bIsNegative, |
| 1566 | int64_t nBase2Exponent); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 1567 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1568 | |
| 1569 | |
| 1570 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1571 | * @brief Add a text URI to the encoded output. |
| 1572 | * |
| 1573 | * @param[in] pCtx The encoding context to add the URI to. |
| 1574 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1575 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1576 | * @param[in] URI Pointer and length of the URI. |
| 1577 | * |
| 1578 | * The format of URI must be per [RFC 3986] |
| 1579 | * (https://tools.ietf.org/html/rfc3986). |
| 1580 | * |
| 1581 | * It is output as CBOR major type 3, a text string, with tag @ref |
| 1582 | * CBOR_TAG_URI indicating the text string is a URI. |
| 1583 | * |
| 1584 | * A URI in a NULL-terminated string, @c szURI, can be easily added with |
| 1585 | * this code: |
| 1586 | * |
| 1587 | * QCBOREncode_AddURI(pCtx, UsefulBuf_FromSZ(szURI)); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1588 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1589 | static void |
| 1590 | QCBOREncode_AddTURI(QCBOREncodeContext *pCtx, |
| 1591 | uint8_t uTagRequirement, |
| 1592 | UsefulBufC URI); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1593 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1594 | static void |
| 1595 | QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pCtx, |
| 1596 | const char *szLabel, |
| 1597 | uint8_t uTagRequirement, |
| 1598 | UsefulBufC URI); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1599 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1600 | static void |
| 1601 | QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pCtx, |
| 1602 | int64_t nLabel, |
| 1603 | uint8_t uTagRequirement, |
| 1604 | UsefulBufC URI); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1605 | |
| 1606 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1607 | static void |
| 1608 | QCBOREncode_AddURI(QCBOREncodeContext *pCtx, |
| 1609 | UsefulBufC URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1610 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1611 | static void |
| 1612 | QCBOREncode_AddURIToMap(QCBOREncodeContext *pCtx, |
| 1613 | const char *szLabel, |
| 1614 | UsefulBufC URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1615 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1616 | static void |
| 1617 | QCBOREncode_AddURIToMapN(QCBOREncodeContext *pCtx, |
| 1618 | int64_t nLabel, |
| 1619 | UsefulBufC URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1620 | |
| 1621 | |
| 1622 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1623 | * @brief Add Base64-encoded text to encoded output. |
| 1624 | * |
| 1625 | * @param[in] pCtx The encoding context to add the base-64 text to. |
| 1626 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1627 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1628 | * @param[in] B64Text Pointer and length of the base-64 encoded text. |
| 1629 | * |
| 1630 | * The text content is Base64 encoded data per [RFC 4648] |
| 1631 | * (https://tools.ietf.org/html/rfc4648). |
| 1632 | * |
| 1633 | * It is output as CBOR major type 3, a text string, with tag @ref |
| 1634 | * CBOR_TAG_B64 indicating the text string is Base64 encoded. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1635 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1636 | static void |
| 1637 | QCBOREncode_AddTB64Text(QCBOREncodeContext *pCtx, |
| 1638 | uint8_t uTagRequirement, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1639 | UsefulBufC B64Text); |
| 1640 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1641 | static void |
| 1642 | QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pCtx, |
| 1643 | const char *szLabel, |
| 1644 | uint8_t uTagRequirement, |
| 1645 | UsefulBufC B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1646 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1647 | static void |
| 1648 | QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pCtx, |
| 1649 | int64_t nLabel, |
| 1650 | uint8_t uTagRequirement, |
| 1651 | UsefulBufC B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1652 | |
| 1653 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1654 | static void |
| 1655 | QCBOREncode_AddB64Text(QCBOREncodeContext *pCtx, |
| 1656 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1657 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1658 | static void |
| 1659 | QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pCtx, |
| 1660 | const char *szLabel, |
| 1661 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1662 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1663 | static void |
| 1664 | QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pCtx, |
| 1665 | int64_t nLabel, |
| 1666 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1667 | |
| 1668 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1669 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1670 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1671 | * @brief Add base64url encoded data to encoded output. |
| 1672 | * |
| 1673 | * @param[in] pCtx The encoding context to add the base64url to. |
| 1674 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1675 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1676 | * @param[in] B64Text Pointer and length of the base64url encoded text. |
| 1677 | * |
| 1678 | * The text content is base64URL encoded text as per [RFC 4648] |
| 1679 | * (https://tools.ietf.org/html/rfc4648). |
| 1680 | * |
| 1681 | * It is output as CBOR major type 3, a text string, with tag |
| 1682 | * @ref CBOR_TAG_B64URL indicating the text string is a Base64url |
| 1683 | * encoded. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1684 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1685 | static void |
| 1686 | QCBOREncode_AddTB64URLText(QCBOREncodeContext *pCtx, |
| 1687 | uint8_t uTagRequirement, |
| 1688 | UsefulBufC B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1689 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1690 | static void |
| 1691 | QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pCtx, |
| 1692 | const char *szLabel, |
| 1693 | uint8_t uTagRequirement, |
| 1694 | UsefulBufC B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1695 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1696 | static void |
| 1697 | QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pCtx, |
| 1698 | int64_t nLabel, |
| 1699 | uint8_t uTagRequirement, |
| 1700 | UsefulBufC B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1701 | |
| 1702 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1703 | static void |
| 1704 | QCBOREncode_AddB64URLText(QCBOREncodeContext *pCtx, |
| 1705 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1706 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1707 | static void |
| 1708 | QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pCtx, |
| 1709 | const char *szLabel, |
| 1710 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1711 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1712 | static void |
| 1713 | QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pCtx, |
| 1714 | int64_t nLabel, |
| 1715 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1716 | |
| 1717 | |
| 1718 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1719 | * @brief Add Perl Compatible Regular Expression. |
| 1720 | * |
| 1721 | * @param[in] pCtx Encoding context to add the regular expression to. |
| 1722 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1723 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1724 | * @param[in] Regex Pointer and length of the regular expression. |
| 1725 | * |
| 1726 | * The text content is Perl Compatible Regular |
| 1727 | * Expressions (PCRE) / JavaScript syntax [ECMA262]. |
| 1728 | * |
| 1729 | * It is output as CBOR major type 3, a text string, with tag @ref |
| 1730 | * CBOR_TAG_REGEX indicating the text string is a regular expression. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1731 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1732 | static void |
| 1733 | QCBOREncode_AddTRegex(QCBOREncodeContext *pCtx, |
| 1734 | uint8_t uTagRequirement, |
| 1735 | UsefulBufC Regex); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1736 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1737 | static void |
| 1738 | QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pCtx, |
| 1739 | const char *szLabel, |
| 1740 | uint8_t uTagRequirement, |
| 1741 | UsefulBufC Regex); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1742 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1743 | static void |
| 1744 | QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pCtx, |
| 1745 | int64_t nLabel, |
| 1746 | uint8_t uTagRequirement, |
| 1747 | UsefulBufC Regex); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1748 | |
| 1749 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1750 | static void |
| 1751 | QCBOREncode_AddRegex(QCBOREncodeContext *pCtx, |
| 1752 | UsefulBufC Regex); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1753 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1754 | static void |
| 1755 | QCBOREncode_AddRegexToMap(QCBOREncodeContext *pCtx, |
| 1756 | const char *szLabel, |
| 1757 | UsefulBufC Regex); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1758 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1759 | static void |
| 1760 | QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pCtx, |
| 1761 | int64_t nLabel, |
| 1762 | UsefulBufC Regex); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1763 | |
| 1764 | |
| 1765 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1766 | * @brief MIME encoded data to the encoded output. |
| 1767 | * |
| 1768 | * @param[in] pCtx The encoding context to add the MIME data to. |
| 1769 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1770 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1771 | * @param[in] MIMEData Pointer and length of the MIME data. |
| 1772 | * |
| 1773 | * The text content is in MIME format per [RFC 2045] |
| 1774 | * (https://tools.ietf.org/html/rfc2045) including the headers. |
| 1775 | * |
| 1776 | * It is output as CBOR major type 2, a binary string, with tag |
| 1777 | * @ref CBOR_TAG_BINARY_MIME indicating the string is MIME data. This |
| 1778 | * outputs tag 257, not tag 36, as it can carry any type of MIME |
| 1779 | * binary, 7-bit, 8-bit, quoted-printable and base64 where tag 36 |
| 1780 | * cannot. |
| 1781 | * |
| 1782 | * Previous versions of QCBOR, those before spiffy decode, output tag |
| 1783 | * 36. Decoding supports both tag 36 and 257. (if the old behavior |
| 1784 | * with tag 36 is needed, copy the inline functions below and change |
| 1785 | * the tag number). |
| 1786 | * |
| 1787 | * See also QCBORDecode_GetMIMEMessage() and |
| 1788 | * @ref QCBOR_TYPE_BINARY_MIME. |
| 1789 | * |
| 1790 | * This does no translation of line endings. See QCBOREncode_AddText() |
| 1791 | * for a discussion of line endings in CBOR. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1792 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1793 | static void |
| 1794 | QCBOREncode_AddTMIMEData(QCBOREncodeContext *pCtx, |
| 1795 | uint8_t uTagRequirement, |
| 1796 | UsefulBufC MIMEData); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1797 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1798 | static void |
| 1799 | QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pCtx, |
| 1800 | const char *szLabel, |
| 1801 | uint8_t uTagRequirement, |
| 1802 | UsefulBufC MIMEData); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1803 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1804 | static void |
| 1805 | QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pCtx, |
| 1806 | int64_t nLabel, |
| 1807 | uint8_t uTagRequirement, |
| 1808 | UsefulBufC MIMEData); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1809 | |
| 1810 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1811 | static void |
| 1812 | QCBOREncode_AddMIMEData(QCBOREncodeContext *pCtx, |
| 1813 | UsefulBufC MIMEData); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1814 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1815 | static void |
| 1816 | QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pCtx, |
| 1817 | const char *szLabel, |
| 1818 | UsefulBufC MIMEData); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1819 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1820 | static void |
| 1821 | QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pCtx, |
| 1822 | int64_t nLabel, |
| 1823 | UsefulBufC MIMEData); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1824 | |
| 1825 | |
| 1826 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1827 | * @brief Add an RFC 3339 date string |
| 1828 | * |
| 1829 | * @param[in] pCtx The encoding context to add the date to. |
| 1830 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1831 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1832 | * @param[in] szDate Null-terminated string with date to add. |
| 1833 | * |
| 1834 | * The string szDate should be in the form of [RFC 3339] |
| 1835 | * (https://tools.ietf.org/html/rfc3339) as defined by section 3.3 in |
| 1836 | * [RFC 4287] (https://tools.ietf.org/html/rfc4287). This is as |
| 1837 | * described in section 3.4.1 in [RFC 8949] |
| 1838 | * (https://tools.ietf.org/html/rfc8949). |
| 1839 | * |
| 1840 | * Note that this function doesn't validate the format of the date |
| 1841 | * string at all. If you add an incorrect format date string, the |
| 1842 | * generated CBOR will be incorrect and the receiver may not be able |
| 1843 | * to handle it. |
| 1844 | * |
| 1845 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 1846 | * |
| 1847 | * See also QCBOREncode_AddTDayString(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1848 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1849 | static void |
| 1850 | QCBOREncode_AddTDateString(QCBOREncodeContext *pCtx, |
| 1851 | uint8_t uTagRequirement, |
| 1852 | const char *szDate); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1853 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1854 | static void |
| 1855 | QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pCtx, |
| 1856 | const char *szLabel, |
| 1857 | uint8_t uTagRequirement, |
| 1858 | const char *szDate); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1859 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1860 | static void |
| 1861 | QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pCtx, |
| 1862 | int64_t nLabel, |
| 1863 | uint8_t uTagRequirement, |
| 1864 | const char *szDate); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1865 | |
| 1866 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1867 | static void |
| 1868 | QCBOREncode_AddDateString(QCBOREncodeContext *pCtx, |
| 1869 | const char *szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1870 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1871 | static void |
| 1872 | QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pCtx, |
| 1873 | const char *szLabel, |
| 1874 | const char *szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1875 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1876 | static void |
| 1877 | QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pCtx, |
| 1878 | int64_t nLabel, |
| 1879 | const char *szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1880 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 1881 | |
| 1882 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1883 | * @brief Add a date-only string. |
| 1884 | * |
| 1885 | * @param[in] pCtx The encoding context to add the date to. |
| 1886 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1887 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1888 | * @param[in] szDate Null-terminated string with date to add. |
| 1889 | * |
| 1890 | * This date format is described in |
| 1891 | * [RFC 8943] (https://tools.ietf.org/html/rfc8943), but that mainly |
| 1892 | * references RFC 3339. The string szDate must be in the forrm |
| 1893 | * specified the ABNF for a full-date in |
| 1894 | * [RFC 3339] (https://tools.ietf.org/html/rfc3339). Examples of this |
| 1895 | * are "1985-04-12" and "1937-01-01". The time and the time zone are |
| 1896 | * never included. |
| 1897 | * |
| 1898 | * Note that this function doesn't validate the format of the date |
| 1899 | * string at all. If you add an incorrect format date string, the |
| 1900 | * generated CBOR will be incorrect and the receiver may not be able |
| 1901 | * to handle it. |
| 1902 | * |
| 1903 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 1904 | * |
| 1905 | * See also QCBOREncode_AddTDateString(). |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 1906 | */ |
| 1907 | static void |
| 1908 | QCBOREncode_AddTDaysString(QCBOREncodeContext *pCtx, |
| 1909 | uint8_t uTagRequirement, |
| 1910 | const char *szDate); |
| 1911 | |
| 1912 | static void |
| 1913 | QCBOREncode_AddTDaysStringToMapSZ(QCBOREncodeContext *pCtx, |
| 1914 | const char *szLabel, |
| 1915 | uint8_t uTagRequirement, |
| 1916 | const char *szDate); |
| 1917 | |
| 1918 | static void |
| 1919 | QCBOREncode_AddTDaysStringToMapN(QCBOREncodeContext *pCtx, |
| 1920 | int64_t nLabel, |
| 1921 | uint8_t uTagRequirement, |
| 1922 | const char *szDate); |
| 1923 | |
| 1924 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1925 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1926 | * @brief Add a standard Boolean. |
| 1927 | * |
| 1928 | * @param[in] pCtx The encoding context to add the Boolean to. |
| 1929 | * @param[in] b true or false from @c <stdbool.h>. |
| 1930 | * |
| 1931 | * Adds a Boolean value as CBOR major type 7. |
| 1932 | * |
| 1933 | * Error handling is the same as QCBOREncode_AddInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1934 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1935 | static void |
| 1936 | QCBOREncode_AddBool(QCBOREncodeContext *pCtx, bool b); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1937 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1938 | static void |
| 1939 | QCBOREncode_AddBoolToMap(QCBOREncodeContext *pCtx, const char *szLabel, bool b); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1940 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1941 | static void |
| 1942 | QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, bool b); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1943 | |
| 1944 | |
| 1945 | |
| 1946 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1947 | * @brief Add a NULL to the encoded output. |
| 1948 | * |
| 1949 | * @param[in] pCtx The encoding context to add the NULL to. |
| 1950 | * |
| 1951 | * Adds the NULL value as CBOR major type 7. |
| 1952 | * |
| 1953 | * This NULL doesn't have any special meaning in CBOR such as a |
| 1954 | * terminating value for a string or an empty value. |
| 1955 | * |
| 1956 | * Error handling is the same as QCBOREncode_AddInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1957 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1958 | static void |
| 1959 | QCBOREncode_AddNULL(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1960 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1961 | static void |
| 1962 | QCBOREncode_AddNULLToMap(QCBOREncodeContext *pCtx, const char *szLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1963 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1964 | static void |
| 1965 | QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1966 | |
| 1967 | |
| 1968 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1969 | * @brief Add an "undef" to the encoded output. |
| 1970 | * |
| 1971 | * @param[in] pCtx The encoding context to add the "undef" to. |
| 1972 | * |
| 1973 | * Adds the undef value as CBOR major type 7. |
| 1974 | * |
| 1975 | * Note that this value will not translate to JSON. |
| 1976 | * |
| 1977 | * This Undef doesn't have any special meaning in CBOR such as a |
| 1978 | * terminating value for a string or an empty value. |
| 1979 | * |
| 1980 | * Error handling is the same as QCBOREncode_AddInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1981 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1982 | static void |
| 1983 | QCBOREncode_AddUndef(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1984 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1985 | static void |
| 1986 | QCBOREncode_AddUndefToMap(QCBOREncodeContext *pCtx, const char *szLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1987 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1988 | static void |
| 1989 | QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1990 | |
| 1991 | |
| 1992 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1993 | * @brief Indicates that the next items added are in an array. |
| 1994 | * |
| 1995 | * @param[in] pCtx The encoding context to open the array in. |
| 1996 | * |
| 1997 | * Arrays are the basic CBOR aggregate or structure type. Call this |
| 1998 | * function to start or open an array. Then call the various |
| 1999 | * @c QCBOREncode_AddXxx() functions to add the items that go into the |
| 2000 | * array. Then call QCBOREncode_CloseArray() when all items have been |
| 2001 | * added. The data items in the array can be of any type and can be of |
| 2002 | * mixed types. |
| 2003 | * |
| 2004 | * Nesting of arrays and maps is allowed and supported just by calling |
| 2005 | * QCBOREncode_OpenArray() again before calling |
| 2006 | * QCBOREncode_CloseArray(). While CBOR has no limit on nesting, this |
| 2007 | * implementation does in order to keep it smaller and simpler. The |
| 2008 | * limit is @ref QCBOR_MAX_ARRAY_NESTING. This is the max number of |
| 2009 | * times this can be called without calling |
| 2010 | * QCBOREncode_CloseArray(). QCBOREncode_Finish() will return |
| 2011 | * @ref QCBOR_ERR_ARRAY_NESTING_TOO_DEEP when it is called as this |
| 2012 | * function just sets an error state and returns no value when this |
| 2013 | * occurs. |
| 2014 | * |
| 2015 | * If you try to add more than @ref QCBOR_MAX_ITEMS_IN_ARRAY items to |
| 2016 | * a single array or map, @ref QCBOR_ERR_ARRAY_TOO_LONG will be |
| 2017 | * returned when QCBOREncode_Finish() is called. |
| 2018 | * |
| 2019 | * An array itself must have a label if it is being added to a map. |
| 2020 | * Note that array elements do not have labels (but map elements do). |
| 2021 | * |
| 2022 | * An array itself may be tagged by calling QCBOREncode_AddTag() |
| 2023 | * before this call. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2024 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2025 | static void |
| 2026 | QCBOREncode_OpenArray(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2027 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2028 | static void |
| 2029 | QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pCtx, const char *szLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2030 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2031 | static void |
| 2032 | QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2033 | |
| 2034 | |
| 2035 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2036 | * @brief Close an open array. |
| 2037 | * |
| 2038 | * @param[in] pCtx The encoding context to close the array in. |
| 2039 | * |
| 2040 | * The closes an array opened by QCBOREncode_OpenArray(). It reduces |
| 2041 | * nesting level by one. All arrays (and maps) must be closed before |
| 2042 | * calling QCBOREncode_Finish(). |
| 2043 | * |
| 2044 | * When an error occurs as a result of this call, the encoder records |
| 2045 | * the error and enters the error state. The error will be returned |
| 2046 | * when QCBOREncode_Finish() is called. |
| 2047 | * |
| 2048 | * If this has been called more times than QCBOREncode_OpenArray(), then |
| 2049 | * @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when QCBOREncode_Finish() |
| 2050 | * is called. |
| 2051 | * |
| 2052 | * If this is called and it is not an array that is currently open, |
| 2053 | * @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when |
| 2054 | * QCBOREncode_Finish() is called. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2055 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2056 | static void |
| 2057 | QCBOREncode_CloseArray(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2058 | |
| 2059 | |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 2060 | |
| 2061 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2062 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2063 | * @brief Indicates that the next items added are in a map. |
| 2064 | * |
| 2065 | * @param[in] pCtx The encoding context to open the map in. |
| 2066 | * |
| 2067 | * See QCBOREncode_OpenArray() for more information, particularly |
| 2068 | * error handling. |
| 2069 | * |
| 2070 | * CBOR maps are an aggregate type where each item in the map consists |
| 2071 | * of a label and a value. They are similar to JSON objects. |
| 2072 | * |
| 2073 | * The value can be any CBOR type including another map. |
| 2074 | * |
| 2075 | * The label can also be any CBOR type, but in practice they are |
| 2076 | * typically, integers as this gives the most compact output. They |
| 2077 | * might also be text strings which gives readability and translation |
| 2078 | * to JSON. |
| 2079 | * |
| 2080 | * Every @c QCBOREncode_AddXxx() call has one version that ends with |
| 2081 | * @c InMap for adding items to maps with string labels and one that |
| 2082 | * ends with @c InMapN that is for adding with integer labels. |
| 2083 | * |
| 2084 | * RFC 8949 uses the term "key" instead of "label". |
| 2085 | * |
| 2086 | * If you wish to use map labels that are neither integer labels nor |
| 2087 | * text strings, then just call the QCBOREncode_AddXxx() function |
| 2088 | * explicitly to add the label. Then call it again to add the value. |
| 2089 | * |
| 2090 | * See the [RFC 8949] (https://tools.ietf.org/html/rfc8949) for a lot |
| 2091 | * more information on creating maps. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2092 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2093 | static void |
| 2094 | QCBOREncode_OpenMap(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2095 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2096 | static void |
| 2097 | QCBOREncode_OpenMapInMap(QCBOREncodeContext *pCtx, const char *szLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2098 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2099 | static void |
| 2100 | QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2101 | |
| 2102 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2103 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2104 | * @brief Close an open map. |
| 2105 | * |
| 2106 | * @param[in] pCtx The encoding context to close the map in. |
| 2107 | * |
| 2108 | * This closes a map opened by QCBOREncode_OpenMap(). It reduces |
| 2109 | * nesting level by one. |
| 2110 | * |
| 2111 | * When an error occurs as a result of this call, the encoder records |
| 2112 | * the error and enters the error state. The error will be returned |
| 2113 | * when QCBOREncode_Finish() is called. |
| 2114 | * |
| 2115 | * If this has been called more times than QCBOREncode_OpenMap(), then |
| 2116 | * @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when |
| 2117 | * QCBOREncode_Finish() is called. |
| 2118 | * |
| 2119 | * If this is called and it is not a map that is currently open, |
| 2120 | * @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when |
| 2121 | * QCBOREncode_Finish() is called. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2122 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2123 | static void |
| 2124 | QCBOREncode_CloseMap(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2125 | |
| 2126 | |
| 2127 | /** |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 2128 | * @brief Indicates that the next items added are in an indefinite length array. |
| 2129 | * |
| 2130 | * @param[in] pCtx The encoding context to open the array in. |
| 2131 | * |
| 2132 | * This is the same as QCBOREncode_OpenArray() except the array is |
| 2133 | * indefinite length. |
| 2134 | * |
| 2135 | * This must be closed with QCBOREncode_CloseArrayIndefiniteLength(). |
| 2136 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2137 | static void |
| 2138 | QCBOREncode_OpenArrayIndefiniteLength(QCBOREncodeContext *pCtx); |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 2139 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2140 | static void |
| 2141 | QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pCtx, |
| 2142 | const char *szLabel); |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 2143 | |
| 2144 | static void |
| 2145 | QCBOREncode_OpenArrayIndefiniteLengthInMapN(QCBOREncodeContext *pCtx, |
| 2146 | int64_t nLabel); |
| 2147 | |
| 2148 | |
| 2149 | /** |
| 2150 | * @brief Close an open indefinite length array. |
| 2151 | * |
| 2152 | * @param[in] pCtx The encoding context to close the array in. |
| 2153 | * |
| 2154 | * This is the same as QCBOREncode_CloseArray(), but the open array |
| 2155 | * that is being close must be of indefinite length. |
| 2156 | */ |
| 2157 | static void |
| 2158 | QCBOREncode_CloseArrayIndefiniteLength(QCBOREncodeContext *pCtx); |
| 2159 | |
| 2160 | |
| 2161 | /** |
| 2162 | * @brief Indicates that the next items added are in an indefinite length map. |
| 2163 | * |
| 2164 | * @param[in] pCtx The encoding context to open the map in. |
| 2165 | * |
| 2166 | * This is the same as QCBOREncode_OpenMap() except the array is |
| 2167 | * indefinite length. |
| 2168 | * |
| 2169 | * This must be closed with QCBOREncode_CloseMapIndefiniteLength(). |
| 2170 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2171 | static void |
| 2172 | QCBOREncode_OpenMapIndefiniteLength(QCBOREncodeContext *pCtx); |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 2173 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2174 | static void |
| 2175 | QCBOREncode_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pCtx, |
| 2176 | const char *szLabel); |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 2177 | |
| 2178 | static void |
| 2179 | QCBOREncode_OpenMapIndefiniteLengthInMapN(QCBOREncodeContext *pCtx, |
| 2180 | int64_t nLabel); |
| 2181 | |
| 2182 | |
| 2183 | /** |
| 2184 | * @brief Close an open indefinite length map. |
| 2185 | * |
| 2186 | * @param[in] pCtx The encoding context to close the map in. |
| 2187 | * |
| 2188 | * This is the same as QCBOREncode_CloseMap(), but the open map that |
| 2189 | * is being close must be of indefinite length. |
| 2190 | */ |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame^] | 2191 | static void |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 2192 | QCBOREncode_CloseMapIndefiniteLength(QCBOREncodeContext *pCtx); |
| 2193 | |
| 2194 | |
Laurence Lundblade | c92e416 | 2023-11-27 21:51:26 -0700 | [diff] [blame] | 2195 | /** |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 2196 | * @brief Close and sort an open map. |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 2197 | * |
| 2198 | * @param[in] pCtx The encoding context to close the map in . |
| 2199 | * |
| 2200 | * This is the same as QCBOREncode_CloseMap() except it sorts the map |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame^] | 2201 | * per RFC 8949 Section 4.2.1 and checks for duplicate map keys. This |
| 2202 | * sort is lexicographic of the CBOR-encoded map labels. |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 2203 | * |
| 2204 | * This is more expensive than most things in the encoder. It uses |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame^] | 2205 | * bubble sort which runs in n-squared time where @c n is the number |
| 2206 | * of map items. Sorting large maps on slow CPUs might be slow. This |
| 2207 | * is also increases the object code size of the encoder by about 30% |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 2208 | * (500-1000 bytes). |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 2209 | * |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame^] | 2210 | * Bubble sort was selected so as to not need require configuration of |
| 2211 | * a buffer to track map item offsets. Bubble sort works well even |
| 2212 | * though map items are not all the same size because it always swaps |
| 2213 | * adjacent items. |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 2214 | */ |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame^] | 2215 | void |
| 2216 | QCBOREncode_CloseAndSortMap(QCBOREncodeContext *pCtx); |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 2217 | |
Laurence Lundblade | dee0d4e | 2024-03-03 13:46:33 -0700 | [diff] [blame^] | 2218 | void |
| 2219 | QCBOREncode_CloseAndSortMapIndef(QCBOREncodeContext *pCtx); |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 2220 | |
| 2221 | |
| 2222 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2223 | * @brief Indicate start of encoded CBOR to be wrapped in a bstr. |
| 2224 | * |
| 2225 | * @param[in] pCtx The encoding context to open the bstr-wrapped CBOR in. |
| 2226 | * |
| 2227 | * All added encoded items between this call and a call to |
| 2228 | * QCBOREncode_CloseBstrWrap2() will be wrapped in a bstr. They will |
| 2229 | * appear in the final output as a byte string. That byte string will |
| 2230 | * contain encoded CBOR. This increases nesting level by one. |
| 2231 | * |
| 2232 | * The typical use case is for encoded CBOR that is to be |
| 2233 | * cryptographically hashed, as part of a [RFC 8152, COSE] |
| 2234 | * (https://tools.ietf.org/html/rfc8152) implementation. The wrapping |
| 2235 | * byte string is taken as input by the hash function (which is why it |
| 2236 | * is returned by QCBOREncode_CloseBstrWrap2()). It is also easy to |
| 2237 | * recover on decoding with standard CBOR decoders. |
| 2238 | * |
| 2239 | * Using QCBOREncode_BstrWrap() and QCBOREncode_CloseBstrWrap2() |
| 2240 | * avoids having to encode the items first in one buffer (e.g., the |
| 2241 | * COSE payload) and then add that buffer as a bstr to another |
| 2242 | * encoding (e.g. the COSE to-be-signed bytes, the @c Sig_structure) |
| 2243 | * potentially halving the memory needed. |
| 2244 | * |
| 2245 | * CBOR by nature must be decoded item by item in order from the |
| 2246 | * start. By wrapping some CBOR in a byte string, the decoding of |
| 2247 | * that wrapped CBOR can be skipped. This is another use of wrapping, |
| 2248 | * perhaps because the CBOR is large and deeply nested. Perhaps APIs |
| 2249 | * for handling one defined CBOR message that is being embedded in |
| 2250 | * another only take input as a byte string. Perhaps the desire is to |
| 2251 | * be able to decode the out layer even in the wrapped has errors. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2252 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2253 | static void |
| 2254 | QCBOREncode_BstrWrap(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2255 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2256 | static void |
| 2257 | QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pCtx, const char *szLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2258 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2259 | static void |
| 2260 | QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2261 | |
| 2262 | |
| 2263 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2264 | * @brief Close a wrapping bstr. |
| 2265 | * |
| 2266 | * @param[in] pCtx The encoding context to close of bstr wrapping in. |
| 2267 | * @param[in] bIncludeCBORHead Include the encoded CBOR head of the bstr |
| 2268 | * as well as the bytes in @c pWrappedCBOR. |
| 2269 | * @param[out] pWrappedCBOR A @ref UsefulBufC containing wrapped bytes. |
| 2270 | * |
| 2271 | * The closes a wrapping bstr opened by QCBOREncode_BstrWrap(). It reduces |
| 2272 | * nesting level by one. |
| 2273 | * |
| 2274 | * A pointer and length of the enclosed encoded CBOR is returned in @c |
| 2275 | * *pWrappedCBOR if it is not @c NULL. The main purpose of this is so |
| 2276 | * this data can be hashed (e.g., with SHA-256) as part of a [RFC |
| 2277 | * 8152, COSE] (https://tools.ietf.org/html/rfc8152) |
| 2278 | * implementation. **WARNING**, this pointer and length should be used |
| 2279 | * right away before any other calls to @c QCBOREncode_CloseXxx() as |
| 2280 | * they will move data around and the pointer and length will no |
| 2281 | * longer be to the correct encoded CBOR. |
| 2282 | * |
| 2283 | * When an error occurs as a result of this call, the encoder records |
| 2284 | * the error and enters the error state. The error will be returned |
| 2285 | * when QCBOREncode_Finish() is called. |
| 2286 | * |
| 2287 | * If this has been called more times than QCBOREncode_BstrWrap(), |
| 2288 | * then @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when |
| 2289 | * QCBOREncode_Finish() is called. |
| 2290 | * |
| 2291 | * If this is called and it is not a wrapping bstr that is currently |
| 2292 | * open, @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when |
| 2293 | * QCBOREncode_Finish() is called. |
| 2294 | * |
| 2295 | * QCBOREncode_CloseBstrWrap() is a deprecated version of this function |
| 2296 | * that is equivalent to the call with @c bIncludeCBORHead @c true. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2297 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2298 | void |
| 2299 | QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *pCtx, bool bIncludeCBORHead, UsefulBufC *pWrappedCBOR); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2300 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2301 | static void |
| 2302 | QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pCtx, UsefulBufC *pWrappedCBOR); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2303 | |
| 2304 | |
| 2305 | /** |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 2306 | * @brief Cancel byte string wrapping. |
| 2307 | * |
| 2308 | * @param[in] pCtx The encoding context. |
| 2309 | * |
| 2310 | * This cancels QCBOREncode_BstrWrap() making tghe encoding as if it |
| 2311 | * were never called. |
| 2312 | * |
| 2313 | * WARNING: This does not work on QCBOREncode_BstrWrapInMap() |
| 2314 | * or QCBOREncode_BstrWrapInMapN() and there is no error detection |
| 2315 | * of an attempt at their use. |
| 2316 | * |
| 2317 | * This only works if nothing has been added into the wrapped byte |
| 2318 | * string. If something has been added, this sets the error |
| 2319 | * @ref QCBOR_ERR_CANNOT_CANCEL. |
| 2320 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2321 | void |
| 2322 | QCBOREncode_CancelBstrWrap(QCBOREncodeContext *pCtx); |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 2323 | |
| 2324 | |
| 2325 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2326 | * @brief Add some already-encoded CBOR bytes. |
| 2327 | * |
| 2328 | * @param[in] pCtx The encoding context to add the already-encode CBOR to. |
| 2329 | * @param[in] Encoded The already-encoded CBOR to add to the context. |
| 2330 | * |
| 2331 | * The encoded CBOR being added must be fully conforming CBOR. It must |
| 2332 | * be complete with no arrays or maps that are incomplete. While this |
| 2333 | * encoder doesn't ever produce indefinite lengths, it is OK for the |
| 2334 | * raw CBOR added here to have indefinite lengths. |
| 2335 | * |
| 2336 | * The raw CBOR added here is not checked in anyway. If it is not |
| 2337 | * conforming or has open arrays or such, the final encoded CBOR |
| 2338 | * will probably be wrong or not what was intended. |
| 2339 | * |
| 2340 | * If the encoded CBOR being added here contains multiple items, they |
| 2341 | * must be enclosed in a map or array. At the top level the raw |
| 2342 | * CBOR must be a single data item. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2343 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2344 | static void |
| 2345 | QCBOREncode_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2346 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2347 | static void |
| 2348 | QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2349 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2350 | static void |
| 2351 | QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2352 | |
| 2353 | |
| 2354 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2355 | * @brief Get the encoded result. |
| 2356 | * |
| 2357 | * @param[in] pCtx The context to finish encoding with. |
| 2358 | * @param[out] pEncodedCBOR Structure in which the pointer and length of |
| 2359 | * the encoded CBOR is returned. |
| 2360 | * |
| 2361 | * @retval QCBOR_SUCCESS Encoded CBOR is returned. |
| 2362 | * |
| 2363 | * @retval QCBOR_ERR_TOO_MANY_CLOSES Nesting error |
| 2364 | * |
| 2365 | * @retval QCBOR_ERR_CLOSE_MISMATCH Nesting error |
| 2366 | * |
| 2367 | * @retval QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN Nesting error |
| 2368 | * |
| 2369 | * @retval QCBOR_ERR_BUFFER_TOO_LARGE Encoded output buffer size |
| 2370 | * |
| 2371 | * @retval QCBOR_ERR_BUFFER_TOO_SMALL Encoded output buffer size |
| 2372 | * |
| 2373 | * @retval QCBOR_ERR_ARRAY_NESTING_TOO_DEEP Implementation limit |
| 2374 | * |
| 2375 | * @retval QCBOR_ERR_ARRAY_TOO_LONG Implementation limit |
| 2376 | * |
| 2377 | * On success, the pointer and length of the encoded CBOR are returned |
| 2378 | * in @c *pEncodedCBOR. The pointer is the same pointer that was passed |
| 2379 | * in to QCBOREncode_Init(). Note that it is not const when passed to |
| 2380 | * QCBOREncode_Init(), but it is const when returned here. The length |
| 2381 | * will be smaller than or equal to the length passed in when |
| 2382 | * QCBOREncode_Init() as this is the length of the actual result, not |
| 2383 | * the size of the buffer it was written to. |
| 2384 | * |
| 2385 | * If a @c NULL was passed for @c Storage.ptr when QCBOREncode_Init() |
| 2386 | * was called, @c NULL will be returned here, but the length will be |
| 2387 | * that of the CBOR that would have been encoded. |
| 2388 | * |
| 2389 | * Encoding errors primarily manifest here as most other encoding function |
| 2390 | * do no return an error. They just set the error state in the encode |
| 2391 | * context after which no encoding function does anything. |
| 2392 | * |
| 2393 | * Three types of errors manifest here. The first type are nesting |
| 2394 | * errors where the number of @c QCBOREncode_OpenXxx() calls do not |
| 2395 | * match the number @c QCBOREncode_CloseXxx() calls. The solution is to |
| 2396 | * fix the calling code. |
| 2397 | * |
| 2398 | * The second type of error is because the buffer given is either too |
| 2399 | * small or too large. The remedy is to give a correctly sized buffer. |
| 2400 | * |
| 2401 | * The third type are due to limits in this implementation. |
| 2402 | * @ref QCBOR_ERR_ARRAY_NESTING_TOO_DEEP can be worked around by |
| 2403 | * encoding the CBOR in two (or more) phases and adding the CBOR from |
| 2404 | * the first phase to the second with @c QCBOREncode_AddEncoded(). |
| 2405 | * |
| 2406 | * If an error is returned, the buffer may have partially encoded |
| 2407 | * incorrect CBOR in it and it should not be used. Likewise, the length |
| 2408 | * may be incorrect and should not be used. |
| 2409 | * |
| 2410 | * Note that the error could have occurred in one of the many |
| 2411 | * @c QCBOREncode_AddXxx() calls long before QCBOREncode_Finish() was |
| 2412 | * called. This error handling reduces the CBOR implementation size |
| 2413 | * but makes debugging harder. |
| 2414 | * |
| 2415 | * This may be called multiple times. It will always return the |
| 2416 | * same. It can also be interleaved with calls to |
| 2417 | * QCBOREncode_FinishGetSize(). |
| 2418 | * |
| 2419 | * QCBOREncode_GetErrorState() can be called to get the current |
| 2420 | * error state in order to abort encoding early as an optimization, but |
| 2421 | * calling it is is never required. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2422 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2423 | QCBORError |
| 2424 | QCBOREncode_Finish(QCBOREncodeContext *pCtx, UsefulBufC *pEncodedCBOR); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2425 | |
| 2426 | |
| 2427 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2428 | * @brief Get the encoded CBOR and error status. |
| 2429 | * |
| 2430 | * @param[in] pCtx The context to finish encoding with. |
| 2431 | * @param[out] uEncodedLen The length of the encoded or potentially |
| 2432 | * encoded CBOR in bytes. |
| 2433 | * |
| 2434 | * @return The same errors as QCBOREncode_Finish(). |
| 2435 | * |
| 2436 | * This functions the same as QCBOREncode_Finish(), but only returns the |
| 2437 | * size of the encoded output. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2438 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2439 | QCBORError |
| 2440 | QCBOREncode_FinishGetSize(QCBOREncodeContext *pCtx, size_t *uEncodedLen); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2441 | |
| 2442 | |
| 2443 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2444 | * @brief Indicate whether output buffer is NULL or not. |
| 2445 | * |
| 2446 | * @param[in] pCtx The encoding context. |
| 2447 | * |
| 2448 | * @return 1 if the output buffer is @c NULL. |
| 2449 | * |
| 2450 | * Sometimes a @c NULL input buffer is given to QCBOREncode_Init() so |
| 2451 | * that the size of the generated CBOR can be calculated without |
| 2452 | * allocating a buffer for it. This returns 1 when the output buffer |
| 2453 | * is @c NULL and 0 when it is not. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2454 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2455 | static int |
| 2456 | QCBOREncode_IsBufferNULL(QCBOREncodeContext *pCtx); |
| 2457 | |
| 2458 | |
| 2459 | /** |
| 2460 | * @brief Get the encoding error state. |
| 2461 | * |
| 2462 | * @param[in] pCtx The encoding context. |
| 2463 | * |
| 2464 | * @return One of @ref QCBORError. See return values from |
| 2465 | * QCBOREncode_Finish() |
| 2466 | * |
| 2467 | * Normally encoding errors need only be handled at the end of |
| 2468 | * encoding when QCBOREncode_Finish() is called. This can be called to |
| 2469 | * get the error result before finish should there be a need to halt |
| 2470 | * encoding before QCBOREncode_Finish() is called. |
| 2471 | */ |
| 2472 | static QCBORError |
| 2473 | QCBOREncode_GetErrorState(QCBOREncodeContext *pCtx); |
| 2474 | |
| 2475 | |
| 2476 | /** |
| 2477 | * Encode the "head" of a CBOR data item. |
| 2478 | * |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2479 | * @param Buffer Buffer to output the encoded head to; must be |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2480 | * @ref QCBOR_HEAD_BUFFER_SIZE bytes in size. |
| 2481 | * @param uMajorType One of CBOR_MAJOR_TYPE_XX. |
| 2482 | * @param uMinLen The minimum number of bytes to encode uNumber. Almost |
| 2483 | * always this is 0 to use preferred |
| 2484 | * serialization. If this is 4, then even the |
| 2485 | * values 0xffff and smaller will be encoded in 4 |
| 2486 | * bytes. This is used primarily when encoding a |
| 2487 | * float or double put into uNumber as the leading |
| 2488 | * zero bytes for them must be encoded. |
| 2489 | * @param uNumber The numeric argument part of the CBOR head. |
| 2490 | * @return Pointer and length of the encoded head or |
| 2491 | * @ref NULLUsefulBufC if the output buffer is too small. |
| 2492 | * |
| 2493 | * Callers do not to need to call this for normal CBOR encoding. Note |
| 2494 | * that it doesn't even take a @ref QCBOREncodeContext argument. |
| 2495 | * |
| 2496 | * This encodes the major type and argument part of a data item. The |
| 2497 | * argument is an integer that is usually either the value or the length |
| 2498 | * of the data item. |
| 2499 | * |
| 2500 | * This is exposed in the public interface to allow hashing of some CBOR |
| 2501 | * data types, bstr in particular, a chunk at a time so the full CBOR |
| 2502 | * doesn't have to be encoded in a contiguous buffer. |
| 2503 | * |
| 2504 | * For example, if you have a 100,000 byte binary blob in a buffer that |
| 2505 | * needs to be a bstr encoded and then hashed. You could allocate a |
| 2506 | * 100,010 byte buffer and encode it normally. Alternatively, you can |
| 2507 | * encode the head in a 10 byte buffer with this function, hash that and |
| 2508 | * then hash the 100,000 bytes using the same hash context. |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2509 | */ |
| 2510 | UsefulBufC |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2511 | QCBOREncode_EncodeHead(UsefulBuf Buffer, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2512 | uint8_t uMajorType, |
| 2513 | uint8_t uMinLen, |
| 2514 | uint64_t uNumber); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2515 | |
| 2516 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2517 | |
| 2518 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2519 | /* ========================================================================= |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2520 | BEGINNING OF PRIVATE IMPLEMENTATION |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2521 | ========================================================================= */ |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2522 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2523 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2524 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2525 | QCBOREncode_Private_AddBuffer(QCBOREncodeContext *pCtx, |
| 2526 | uint8_t uMajorType, |
| 2527 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2528 | |
| 2529 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2530 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2531 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2532 | QCBOREncode_Private_OpenMapOrArray(QCBOREncodeContext *pCtx, |
| 2533 | uint8_t uMajorType); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2534 | |
| 2535 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2536 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2537 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2538 | QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx, |
| 2539 | uint8_t uMajorType); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2540 | |
| 2541 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2542 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2543 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2544 | QCBOREncode_Private_CloseMapOrArray(QCBOREncodeContext *pCtx, |
| 2545 | uint8_t uMajorType); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2546 | |
| 2547 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2548 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2549 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2550 | QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx, |
| 2551 | uint8_t uMajorType); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2552 | |
| 2553 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2554 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2555 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2556 | QCBOREncode_Private_AddType7(QCBOREncodeContext *pCtx, |
| 2557 | uint8_t uMinLen, |
| 2558 | uint64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2559 | |
| 2560 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2561 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2562 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2563 | QCBOREncode_Private_AddExpMantissa(QCBOREncodeContext *pCtx, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2564 | uint64_t uTag, |
| 2565 | UsefulBufC BigNumMantissa, |
| 2566 | bool bBigNumIsNegative, |
| 2567 | int64_t nMantissa, |
| 2568 | int64_t nExponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2569 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2570 | |
| 2571 | |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 2572 | |
| 2573 | static inline void |
| 2574 | QCBOREncode_SerializationCDE(QCBOREncodeContext *pMe) |
| 2575 | { |
| 2576 | /* The use of a function pointer here is a little trick to reduce |
| 2577 | * code linked for the common use cases that don't sort. If this |
| 2578 | * function is never linked, then QCBOREncode_CloseAndSortMap() is |
| 2579 | * never linked and the amount of code pulled in is small. If the |
| 2580 | * mode switch between sorting and not sorting were an if |
| 2581 | * statement, then QCBOREncode_CloseAndSortMap() would always be |
| 2582 | * linked even when not used. */ |
| 2583 | pMe->pfnCloseMap = QCBOREncode_CloseAndSortMap; |
| 2584 | pMe->uMode = QCBOR_ENCODE_MODE_CDE; |
| 2585 | } |
| 2586 | |
| 2587 | static inline void |
| 2588 | QCBOREncode_SerializationdCBOR(QCBOREncodeContext *pMe) |
| 2589 | { |
| 2590 | pMe->pfnCloseMap = QCBOREncode_CloseAndSortMap; |
| 2591 | pMe->uMode = QCBOR_ENCODE_MODE_DCBOR; |
| 2592 | } |
| 2593 | |
| 2594 | static inline void |
| 2595 | QCBOREncode_SerializationPreferred(QCBOREncodeContext *pMe) |
| 2596 | { |
| 2597 | pMe->uMode = QCBOR_ENCODE_MODE_PREFERRED; |
| 2598 | } |
| 2599 | |
| 2600 | static inline void |
| 2601 | QCBOREncode_Allow(QCBOREncodeContext *pMe, const uint8_t uAllow) |
| 2602 | { |
| 2603 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 2604 | pMe->uAllow = uAllow; |
| 2605 | #else |
| 2606 | (void)uAllow; |
| 2607 | (void)pMe; |
| 2608 | #endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 2609 | } |
| 2610 | |
| 2611 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2612 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2613 | QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pMe, |
| 2614 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2615 | const int64_t uNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2616 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2617 | /* Use _AddBuffer() because _AddSZString() is defined below, not above */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2618 | QCBOREncode_Private_AddBuffer(pMe, |
| 2619 | CBOR_MAJOR_TYPE_TEXT_STRING, |
| 2620 | UsefulBuf_FromSZ(szLabel)); |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2621 | QCBOREncode_AddInt64(pMe, uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2622 | } |
| 2623 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2624 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2625 | QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2626 | const int64_t nLabel, |
| 2627 | const int64_t uNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2628 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2629 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2630 | QCBOREncode_AddInt64(pMe, uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2634 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2635 | QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pMe, |
| 2636 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2637 | const uint64_t uNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2638 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2639 | /* Use _AddBuffer() because _AddSZString() is defined below, not above */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2640 | QCBOREncode_Private_AddBuffer(pMe, |
| 2641 | CBOR_MAJOR_TYPE_TEXT_STRING, |
| 2642 | UsefulBuf_FromSZ(szLabel)); |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2643 | QCBOREncode_AddUInt64(pMe, uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2644 | } |
| 2645 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2646 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2647 | QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2648 | const int64_t nLabel, |
| 2649 | const uint64_t uNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2650 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2651 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2652 | QCBOREncode_AddUInt64(pMe, uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2656 | static inline void |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 2657 | QCBOREncode_AddNegativeUInt64ToMap(QCBOREncodeContext *pMe, const char *szLabel, uint64_t uNum) |
| 2658 | { |
| 2659 | /* Use _AddBuffer() because _AddSZString() is defined below, not above */ |
| 2660 | QCBOREncode_Private_AddBuffer(pMe, |
| 2661 | CBOR_MAJOR_TYPE_TEXT_STRING, |
| 2662 | UsefulBuf_FromSZ(szLabel)); |
| 2663 | QCBOREncode_AddNegativeUInt64(pMe, uNum); |
| 2664 | } |
| 2665 | |
| 2666 | static inline void |
| 2667 | QCBOREncode_AddNegativeUInt64ToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint64_t uNum) |
| 2668 | { |
| 2669 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2670 | QCBOREncode_AddNegativeUInt64(pMe, uNum); |
| 2671 | } |
| 2672 | |
| 2673 | |
| 2674 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2675 | QCBOREncode_AddText(QCBOREncodeContext *pMe, const UsefulBufC Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2676 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2677 | QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_TYPE_TEXT_STRING, Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2678 | } |
| 2679 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2680 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2681 | QCBOREncode_AddTextToMap(QCBOREncodeContext *pMe, |
| 2682 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2683 | const UsefulBufC Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2684 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2685 | QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szLabel)); |
| 2686 | QCBOREncode_AddText(pMe, Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2687 | } |
| 2688 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2689 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2690 | QCBOREncode_AddTextToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2691 | const int64_t nLabel, |
| 2692 | const UsefulBufC Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2693 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2694 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2695 | QCBOREncode_AddText(pMe, Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2699 | inline static void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2700 | QCBOREncode_AddSZString(QCBOREncodeContext *pMe, const char *szString) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2701 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2702 | QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szString)); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2703 | } |
| 2704 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2705 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2706 | QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pMe, |
| 2707 | const char *szLabel, |
| 2708 | const char *szString) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2709 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2710 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2711 | QCBOREncode_AddSZString(pMe, szString); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2712 | } |
| 2713 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2714 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2715 | QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2716 | const int64_t nLabel, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2717 | const char *szString) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2718 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2719 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2720 | QCBOREncode_AddSZString(pMe, szString); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2724 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2725 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2726 | QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pMe, |
| 2727 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2728 | const double dNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2729 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2730 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2731 | QCBOREncode_AddDouble(pMe, dNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2732 | } |
| 2733 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2734 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2735 | QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2736 | const int64_t nLabel, |
| 2737 | const double dNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2738 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2739 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2740 | QCBOREncode_AddDouble(pMe, dNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2741 | } |
| 2742 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2743 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2744 | QCBOREncode_AddFloatToMap(QCBOREncodeContext *pMe, |
| 2745 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2746 | const float dNum) |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 2747 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2748 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2749 | QCBOREncode_AddFloat(pMe, dNum); |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 2750 | } |
| 2751 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2752 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2753 | QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pMe, |
| 2754 | const int64_t nLabel, |
| 2755 | const float fNum) |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 2756 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2757 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2758 | QCBOREncode_AddFloat(pMe, fNum); |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 2759 | } |
| 2760 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2761 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2762 | QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pMe, |
| 2763 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2764 | const double dNum) |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2765 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2766 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2767 | QCBOREncode_AddDoubleNoPreferred(pMe, dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2770 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2771 | QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2772 | const int64_t nLabel, |
| 2773 | const double dNum) |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2774 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2775 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2776 | QCBOREncode_AddDoubleNoPreferred(pMe, dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2777 | } |
| 2778 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2779 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2780 | QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pMe, |
| 2781 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2782 | const float dNum) |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2783 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2784 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2785 | QCBOREncode_AddFloatNoPreferred(pMe, dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2786 | } |
| 2787 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2788 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2789 | QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2790 | const int64_t nLabel, |
| 2791 | const float dNum) |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2792 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2793 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2794 | QCBOREncode_AddFloatNoPreferred(pMe, dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2795 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2796 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2797 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2798 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2799 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2800 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2801 | QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pMe, |
| 2802 | const uint8_t uTag, |
| 2803 | const int64_t nDate) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2804 | { |
| 2805 | if(uTag == QCBOR_ENCODE_AS_TAG) { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2806 | QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_EPOCH); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2807 | } |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2808 | QCBOREncode_AddInt64(pMe, nDate); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2809 | } |
| 2810 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2811 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2812 | QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pMe, |
| 2813 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2814 | const uint8_t uTag, |
| 2815 | const int64_t nDate) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2816 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2817 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2818 | QCBOREncode_AddTDateEpoch(pMe, uTag, nDate); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2819 | } |
| 2820 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2821 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2822 | QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2823 | const int64_t nLabel, |
| 2824 | const uint8_t uTag, |
| 2825 | const int64_t nDate) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2826 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2827 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2828 | QCBOREncode_AddTDateEpoch(pMe, uTag, nDate); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2829 | } |
| 2830 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2831 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2832 | QCBOREncode_AddDateEpoch(QCBOREncodeContext *pMe, |
| 2833 | const int64_t nDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2834 | { |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2835 | QCBOREncode_AddTDateEpoch(pMe, QCBOR_ENCODE_AS_TAG, nDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2836 | } |
| 2837 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2838 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2839 | QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pMe, |
| 2840 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2841 | const int64_t nDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2842 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2843 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2844 | QCBOREncode_AddDateEpoch(pMe, nDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2845 | } |
| 2846 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2847 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2848 | QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2849 | const int64_t nLabel, |
| 2850 | const int64_t nDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2851 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2852 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2853 | QCBOREncode_AddDateEpoch(pMe, nDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2857 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2858 | QCBOREncode_AddTDaysEpoch(QCBOREncodeContext *pMe, |
| 2859 | const uint8_t uTag, |
| 2860 | const int64_t nDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2861 | { |
| 2862 | if(uTag == QCBOR_ENCODE_AS_TAG) { |
| 2863 | QCBOREncode_AddTag(pMe, CBOR_TAG_DAYS_EPOCH); |
| 2864 | } |
| 2865 | QCBOREncode_AddInt64(pMe, nDays); |
| 2866 | } |
| 2867 | |
| 2868 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2869 | QCBOREncode_AddTDaysEpochToMapSZ(QCBOREncodeContext *pMe, |
| 2870 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2871 | const uint8_t uTag, |
| 2872 | const int64_t nDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2873 | { |
| 2874 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2875 | QCBOREncode_AddTDaysEpoch(pMe, uTag, nDays); |
| 2876 | } |
| 2877 | |
| 2878 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2879 | QCBOREncode_AddTDaysEpochToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2880 | const int64_t nLabel, |
| 2881 | const uint8_t uTag, |
| 2882 | const int64_t nDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2883 | { |
| 2884 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2885 | QCBOREncode_AddTDaysEpoch(pMe, uTag, nDays); |
| 2886 | } |
| 2887 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2888 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2889 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2890 | QCBOREncode_AddBytes(QCBOREncodeContext *pMe, |
| 2891 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2892 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2893 | QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2894 | } |
| 2895 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2896 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2897 | QCBOREncode_AddBytesToMap(QCBOREncodeContext *pMe, |
| 2898 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2899 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2900 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2901 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2902 | QCBOREncode_AddBytes(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2903 | } |
| 2904 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2905 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2906 | QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2907 | const int64_t nLabel, |
| 2908 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2909 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2910 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2911 | QCBOREncode_AddBytes(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2912 | } |
| 2913 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2914 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2915 | QCBOREncode_OpenBytesInMapSZ(QCBOREncodeContext *pMe, |
| 2916 | const char *szLabel, |
| 2917 | UsefulBuf *pPlace) |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 2918 | { |
| 2919 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2920 | QCBOREncode_OpenBytes(pMe, pPlace); |
| 2921 | } |
| 2922 | |
| 2923 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2924 | QCBOREncode_OpenBytesInMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2925 | const int64_t nLabel, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2926 | UsefulBuf *pPlace) |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 2927 | { |
| 2928 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2929 | QCBOREncode_OpenBytes(pMe, pPlace); |
| 2930 | } |
| 2931 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2932 | |
| 2933 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2934 | QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2935 | const uint8_t uTagRequirement, |
| 2936 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2937 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2938 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 2939 | QCBOREncode_AddTag(pMe, CBOR_TAG_BIN_UUID); |
| 2940 | } |
| 2941 | QCBOREncode_AddBytes(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2942 | } |
| 2943 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2944 | static inline void |
| 2945 | QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pMe, |
| 2946 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2947 | const uint8_t uTagRequirement, |
| 2948 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2949 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2950 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2951 | QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2952 | } |
| 2953 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2954 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2955 | QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2956 | const int64_t nLabel, |
| 2957 | const uint8_t uTagRequirement, |
| 2958 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2959 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2960 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2961 | QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2962 | } |
| 2963 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2964 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2965 | QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pMe, const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2966 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2967 | QCBOREncode_AddTBinaryUUID(pMe, QCBOR_ENCODE_AS_TAG, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2968 | } |
| 2969 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2970 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2971 | QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pMe, |
| 2972 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2973 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2974 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2975 | QCBOREncode_AddTBinaryUUIDToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2976 | } |
| 2977 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2978 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2979 | QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2980 | const int64_t nLabel, |
| 2981 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2982 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2983 | QCBOREncode_AddTBinaryUUIDToMapN(pMe, |
| 2984 | nLabel, |
| 2985 | QCBOR_ENCODE_AS_TAG, |
| 2986 | Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2990 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2991 | QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2992 | const uint8_t uTagRequirement, |
| 2993 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2994 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2995 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 2996 | QCBOREncode_AddTag(pMe, CBOR_TAG_POS_BIGNUM); |
| 2997 | } |
| 2998 | QCBOREncode_AddBytes(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2999 | } |
| 3000 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3001 | static inline void |
| 3002 | QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pMe, |
| 3003 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3004 | const uint8_t uTagRequirement, |
| 3005 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3006 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3007 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3008 | QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3009 | } |
| 3010 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3011 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3012 | QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3013 | const int64_t nLabel, |
| 3014 | const uint8_t uTagRequirement, |
| 3015 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3016 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3017 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3018 | QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3019 | } |
| 3020 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3021 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3022 | QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pMe, const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3023 | { |
| 3024 | QCBOREncode_AddTPositiveBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes); |
| 3025 | } |
| 3026 | |
| 3027 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3028 | QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pMe, |
| 3029 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3030 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3031 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3032 | QCBOREncode_AddTPositiveBignumToMapSZ(pMe, |
| 3033 | szLabel, |
| 3034 | QCBOR_ENCODE_AS_TAG, |
| 3035 | Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3039 | QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3040 | const int64_t nLabel, |
| 3041 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3042 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3043 | QCBOREncode_AddTPositiveBignumToMapN(pMe, |
| 3044 | nLabel, |
| 3045 | QCBOR_ENCODE_AS_TAG, |
| 3046 | Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | |
| 3050 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3051 | QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3052 | const uint8_t uTagRequirement, |
| 3053 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3054 | { |
| 3055 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3056 | QCBOREncode_AddTag(pMe, CBOR_TAG_NEG_BIGNUM); |
| 3057 | } |
| 3058 | QCBOREncode_AddBytes(pMe, Bytes); |
| 3059 | } |
| 3060 | |
| 3061 | static inline void |
| 3062 | QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pMe, |
| 3063 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3064 | const uint8_t uTagRequirement, |
| 3065 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3066 | { |
| 3067 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3068 | QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes); |
| 3069 | } |
| 3070 | |
| 3071 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3072 | QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3073 | const int64_t nLabel, |
| 3074 | const uint8_t uTagRequirement, |
| 3075 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3076 | { |
| 3077 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3078 | QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes); |
| 3079 | } |
| 3080 | |
| 3081 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3082 | QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pMe, const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3083 | { |
| 3084 | QCBOREncode_AddTNegativeBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes); |
| 3085 | } |
| 3086 | |
| 3087 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3088 | QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pMe, |
| 3089 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3090 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3091 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3092 | QCBOREncode_AddTNegativeBignumToMapSZ(pMe, |
| 3093 | szLabel, |
| 3094 | QCBOR_ENCODE_AS_TAG, |
| 3095 | Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3099 | QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3100 | const int64_t nLabel, |
| 3101 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3102 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3103 | QCBOREncode_AddTNegativeBignumToMapN(pMe, |
| 3104 | nLabel, |
| 3105 | QCBOR_ENCODE_AS_TAG, |
| 3106 | Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3110 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3111 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3112 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3113 | static inline void |
| 3114 | QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3115 | const uint8_t uTagRequirement, |
| 3116 | const int64_t nMantissa, |
| 3117 | const int64_t nBase10Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3118 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3119 | uint64_t uTag; |
| 3120 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3121 | uTag = CBOR_TAG_DECIMAL_FRACTION; |
| 3122 | } else { |
| 3123 | uTag = CBOR_TAG_INVALID64; |
| 3124 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3125 | QCBOREncode_Private_AddExpMantissa(pMe, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3126 | uTag, |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3127 | NULLUsefulBufC, |
| 3128 | false, |
| 3129 | nMantissa, |
| 3130 | nBase10Exponent); |
| 3131 | } |
| 3132 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3133 | static inline void |
| 3134 | QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pMe, |
| 3135 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3136 | const uint8_t uTagRequirement, |
| 3137 | const int64_t nMantissa, |
| 3138 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3139 | { |
| 3140 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3141 | QCBOREncode_AddTDecimalFraction(pMe, |
| 3142 | uTagRequirement, |
| 3143 | nMantissa, |
| 3144 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3145 | } |
| 3146 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3147 | static inline void |
| 3148 | QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3149 | const int64_t nLabel, |
| 3150 | const uint8_t uTagRequirement, |
| 3151 | const int64_t nMantissa, |
| 3152 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3153 | { |
| 3154 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3155 | QCBOREncode_AddTDecimalFraction(pMe, |
| 3156 | uTagRequirement, |
| 3157 | nMantissa, |
| 3158 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3159 | } |
| 3160 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3161 | static inline void |
| 3162 | QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3163 | const int64_t nMantissa, |
| 3164 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3165 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3166 | QCBOREncode_AddTDecimalFraction(pMe, |
| 3167 | QCBOR_ENCODE_AS_TAG, |
| 3168 | nMantissa, |
| 3169 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3170 | } |
| 3171 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3172 | static inline void |
| 3173 | QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pMe, |
| 3174 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3175 | const int64_t nMantissa, |
| 3176 | const int64_t nBase10Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3177 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3178 | QCBOREncode_AddTDecimalFractionToMapSZ(pMe, |
| 3179 | szLabel, |
| 3180 | QCBOR_ENCODE_AS_TAG, |
| 3181 | nMantissa, |
| 3182 | nBase10Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3183 | } |
| 3184 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3185 | static inline void |
| 3186 | QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3187 | const int64_t nLabel, |
| 3188 | const int64_t nMantissa, |
| 3189 | const int64_t nBase10Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3190 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3191 | QCBOREncode_AddTDecimalFractionToMapN(pMe, |
| 3192 | nLabel, |
| 3193 | QCBOR_ENCODE_AS_TAG, |
| 3194 | nMantissa, |
| 3195 | nBase10Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3196 | } |
| 3197 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3198 | |
| 3199 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3200 | static inline void |
| 3201 | QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3202 | const uint8_t uTagRequirement, |
| 3203 | const UsefulBufC Mantissa, |
| 3204 | const bool bIsNegative, |
| 3205 | const int64_t nBase10Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3206 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3207 | uint64_t uTag; |
| 3208 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3209 | uTag = CBOR_TAG_DECIMAL_FRACTION; |
| 3210 | } else { |
| 3211 | uTag = CBOR_TAG_INVALID64; |
| 3212 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3213 | QCBOREncode_Private_AddExpMantissa(pMe, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3214 | uTag, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3215 | Mantissa, |
| 3216 | bIsNegative, |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3217 | 0, |
| 3218 | nBase10Exponent); |
| 3219 | } |
| 3220 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3221 | static inline void |
| 3222 | QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe, |
| 3223 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3224 | const uint8_t uTagRequirement, |
| 3225 | const UsefulBufC Mantissa, |
| 3226 | const bool bIsNegative, |
| 3227 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3228 | { |
| 3229 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3230 | QCBOREncode_AddTDecimalFractionBigNum(pMe, |
| 3231 | uTagRequirement, |
| 3232 | Mantissa, |
| 3233 | bIsNegative, |
| 3234 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3235 | } |
| 3236 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3237 | static inline void |
| 3238 | QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3239 | const int64_t nLabel, |
| 3240 | const uint8_t uTagRequirement, |
| 3241 | const UsefulBufC Mantissa, |
| 3242 | const bool bIsNegative, |
| 3243 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3244 | { |
| 3245 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3246 | QCBOREncode_AddTDecimalFractionBigNum(pMe, |
| 3247 | uTagRequirement, |
| 3248 | Mantissa, |
| 3249 | bIsNegative, |
| 3250 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3251 | } |
| 3252 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3253 | static inline void |
| 3254 | QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3255 | const UsefulBufC Mantissa, |
| 3256 | const bool bIsNegative, |
| 3257 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3258 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3259 | QCBOREncode_AddTDecimalFractionBigNum(pMe, |
| 3260 | QCBOR_ENCODE_AS_TAG, |
| 3261 | Mantissa, |
| 3262 | bIsNegative, |
| 3263 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3264 | } |
| 3265 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3266 | static inline void |
| 3267 | QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe, |
| 3268 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3269 | const UsefulBufC Mantissa, |
| 3270 | const bool bIsNegative, |
| 3271 | const int64_t nBase10Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3272 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3273 | QCBOREncode_AddTDecimalFractionBigNumToMapSZ(pMe, |
| 3274 | szLabel, |
| 3275 | QCBOR_ENCODE_AS_TAG, |
| 3276 | Mantissa, |
| 3277 | bIsNegative, |
| 3278 | nBase10Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3279 | } |
| 3280 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3281 | static inline void |
| 3282 | QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3283 | const int64_t nLabel, |
| 3284 | const UsefulBufC Mantissa, |
| 3285 | const bool bIsNegative, |
| 3286 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3287 | { |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3288 | QCBOREncode_AddTDecimalFractionBigNumToMapN(pMe, |
| 3289 | nLabel, |
| 3290 | QCBOR_ENCODE_AS_TAG, |
| 3291 | Mantissa, |
| 3292 | bIsNegative, |
| 3293 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3294 | } |
| 3295 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3296 | |
| 3297 | |
| 3298 | |
| 3299 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3300 | static inline void |
| 3301 | QCBOREncode_AddTBigFloat(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3302 | const uint8_t uTagRequirement, |
| 3303 | const int64_t nMantissa, |
| 3304 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3305 | { |
| 3306 | uint64_t uTag; |
| 3307 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3308 | uTag = CBOR_TAG_BIGFLOAT; |
| 3309 | } else { |
| 3310 | uTag = CBOR_TAG_INVALID64; |
| 3311 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3312 | QCBOREncode_Private_AddExpMantissa(pMe, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3313 | uTag, |
| 3314 | NULLUsefulBufC, |
| 3315 | false, |
| 3316 | nMantissa, |
| 3317 | nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3318 | } |
| 3319 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3320 | static inline void |
| 3321 | QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pMe, |
| 3322 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3323 | const uint8_t uTagRequirement, |
| 3324 | const int64_t nMantissa, |
| 3325 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3326 | { |
| 3327 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3328 | QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent); |
| 3329 | } |
| 3330 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3331 | static inline void |
| 3332 | QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3333 | const int64_t nLabel, |
| 3334 | const uint8_t uTagRequirement, |
| 3335 | const int64_t nMantissa, |
| 3336 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3337 | { |
| 3338 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3339 | QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent); |
| 3340 | } |
| 3341 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3342 | static inline void |
| 3343 | QCBOREncode_AddBigFloat(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3344 | const int64_t nMantissa, |
| 3345 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3346 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3347 | QCBOREncode_AddTBigFloat(pMe, |
| 3348 | QCBOR_ENCODE_AS_TAG, |
| 3349 | nMantissa, |
| 3350 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3351 | } |
| 3352 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3353 | static inline void |
| 3354 | QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pMe, |
| 3355 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3356 | const int64_t nMantissa, |
| 3357 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3358 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3359 | QCBOREncode_AddTBigFloatToMapSZ(pMe, |
| 3360 | szLabel, |
| 3361 | QCBOR_ENCODE_AS_TAG, |
| 3362 | nMantissa, |
| 3363 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3364 | } |
| 3365 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3366 | static inline void |
| 3367 | QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3368 | const int64_t nLabel, |
| 3369 | const int64_t nMantissa, |
| 3370 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3371 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3372 | QCBOREncode_AddTBigFloatToMapN(pMe, |
| 3373 | nLabel, |
| 3374 | QCBOR_ENCODE_AS_TAG, |
| 3375 | nMantissa, |
| 3376 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3377 | } |
| 3378 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3379 | |
| 3380 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3381 | static inline void |
| 3382 | QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3383 | const uint8_t uTagRequirement, |
| 3384 | const UsefulBufC Mantissa, |
| 3385 | const bool bIsNegative, |
| 3386 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3387 | { |
| 3388 | uint64_t uTag; |
| 3389 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3390 | uTag = CBOR_TAG_BIGFLOAT; |
| 3391 | } else { |
| 3392 | uTag = CBOR_TAG_INVALID64; |
| 3393 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3394 | QCBOREncode_Private_AddExpMantissa(pMe, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3395 | uTag, |
| 3396 | Mantissa, |
| 3397 | bIsNegative, |
| 3398 | 0, |
| 3399 | nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3400 | } |
| 3401 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3402 | static inline void |
| 3403 | QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pMe, |
| 3404 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3405 | const uint8_t uTagRequirement, |
| 3406 | const UsefulBufC Mantissa, |
| 3407 | const bool bIsNegative, |
| 3408 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3409 | { |
| 3410 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3411 | QCBOREncode_AddTBigFloatBigNum(pMe, |
| 3412 | uTagRequirement, |
| 3413 | Mantissa, |
| 3414 | bIsNegative, |
| 3415 | nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3416 | } |
| 3417 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3418 | static inline void |
| 3419 | QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3420 | const int64_t nLabel, |
| 3421 | const uint8_t uTagRequirement, |
| 3422 | const UsefulBufC Mantissa, |
| 3423 | const bool bIsNegative, |
| 3424 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3425 | { |
| 3426 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3427 | QCBOREncode_AddTBigFloatBigNum(pMe, |
| 3428 | uTagRequirement, |
| 3429 | Mantissa, |
| 3430 | bIsNegative, |
| 3431 | nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3435 | static inline void |
| 3436 | QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3437 | const UsefulBufC Mantissa, |
| 3438 | const bool bIsNegative, |
| 3439 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3440 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3441 | QCBOREncode_AddTBigFloatBigNum(pMe, |
| 3442 | QCBOR_ENCODE_AS_TAG, |
| 3443 | Mantissa, bIsNegative, |
| 3444 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3445 | } |
| 3446 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3447 | static inline void |
| 3448 | QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pMe, |
| 3449 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3450 | const UsefulBufC Mantissa, |
| 3451 | const bool bIsNegative, |
| 3452 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3453 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3454 | QCBOREncode_AddTBigFloatBigNumToMapSZ(pMe, |
| 3455 | szLabel, |
| 3456 | QCBOR_ENCODE_AS_TAG, |
| 3457 | Mantissa, |
| 3458 | bIsNegative, |
| 3459 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3460 | } |
| 3461 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3462 | static inline void |
| 3463 | QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3464 | const int64_t nLabel, |
| 3465 | const UsefulBufC Mantissa, |
| 3466 | const bool bIsNegative, |
| 3467 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3468 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3469 | QCBOREncode_AddTBigFloatBigNumToMapN(pMe, |
| 3470 | nLabel, |
| 3471 | QCBOR_ENCODE_AS_TAG, |
| 3472 | Mantissa, |
| 3473 | bIsNegative, |
| 3474 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3475 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3476 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3477 | |
| 3478 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3479 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3480 | QCBOREncode_AddTURI(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3481 | const uint8_t uTagRequirement, |
| 3482 | const UsefulBufC URI) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3483 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3484 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3485 | QCBOREncode_AddTag(pMe, CBOR_TAG_URI); |
| 3486 | } |
| 3487 | QCBOREncode_AddText(pMe, URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3488 | } |
| 3489 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3490 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3491 | QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pMe, |
| 3492 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3493 | const uint8_t uTagRequirement, |
| 3494 | const UsefulBufC URI) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3495 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3496 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3497 | QCBOREncode_AddTURI(pMe, uTagRequirement, URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3498 | } |
| 3499 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3500 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3501 | QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3502 | const int64_t nLabel, |
| 3503 | const uint8_t uTagRequirement, |
| 3504 | const UsefulBufC URI) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3505 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3506 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3507 | QCBOREncode_AddTURI(pMe, uTagRequirement, URI); |
| 3508 | } |
| 3509 | |
| 3510 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3511 | QCBOREncode_AddURI(QCBOREncodeContext *pMe, const UsefulBufC URI) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3512 | { |
| 3513 | QCBOREncode_AddTURI(pMe, QCBOR_ENCODE_AS_TAG, URI); |
| 3514 | } |
| 3515 | |
| 3516 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3517 | QCBOREncode_AddURIToMap(QCBOREncodeContext *pMe, |
| 3518 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3519 | const UsefulBufC URI) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3520 | { |
| 3521 | QCBOREncode_AddTURIToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, URI); |
| 3522 | } |
| 3523 | |
| 3524 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3525 | QCBOREncode_AddURIToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3526 | const int64_t nLabel, |
| 3527 | const UsefulBufC URI) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3528 | { |
| 3529 | QCBOREncode_AddTURIToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3530 | } |
| 3531 | |
| 3532 | |
| 3533 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3534 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3535 | QCBOREncode_AddTB64Text(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3536 | const uint8_t uTagRequirement, |
| 3537 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3538 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3539 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3540 | QCBOREncode_AddTag(pMe, CBOR_TAG_B64); |
| 3541 | } |
| 3542 | QCBOREncode_AddText(pMe, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3543 | } |
| 3544 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3545 | static inline void |
| 3546 | QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pMe, |
| 3547 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3548 | const uint8_t uTagRequirement, |
| 3549 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3550 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3551 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3552 | QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3553 | } |
| 3554 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3555 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3556 | QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3557 | const int64_t nLabel, |
| 3558 | const uint8_t uTagRequirement, |
| 3559 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3560 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3561 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3562 | QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text); |
| 3563 | } |
| 3564 | |
| 3565 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3566 | QCBOREncode_AddB64Text(QCBOREncodeContext *pMe, const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3567 | { |
| 3568 | QCBOREncode_AddTB64Text(pMe, QCBOR_ENCODE_AS_TAG, B64Text); |
| 3569 | } |
| 3570 | |
| 3571 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3572 | QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pMe, |
| 3573 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3574 | const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3575 | { |
| 3576 | QCBOREncode_AddTB64TextToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, B64Text); |
| 3577 | } |
| 3578 | |
| 3579 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3580 | QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3581 | const int64_t nLabel, |
| 3582 | const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3583 | { |
| 3584 | QCBOREncode_AddTB64TextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3588 | |
| 3589 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3590 | QCBOREncode_AddTB64URLText(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3591 | const uint8_t uTagRequirement, |
| 3592 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3593 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3594 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3595 | QCBOREncode_AddTag(pMe, CBOR_TAG_B64URL); |
| 3596 | } |
| 3597 | QCBOREncode_AddText(pMe, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3598 | } |
| 3599 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3600 | static inline void |
| 3601 | QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pMe, |
| 3602 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3603 | const uint8_t uTagRequirement, |
| 3604 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3605 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3606 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3607 | QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3608 | } |
| 3609 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3610 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3611 | QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3612 | const int64_t nLabel, |
| 3613 | const uint8_t uTagRequirement, |
| 3614 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3615 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3616 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3617 | QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text); |
| 3618 | } |
| 3619 | |
| 3620 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3621 | QCBOREncode_AddB64URLText(QCBOREncodeContext *pMe, const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3622 | { |
| 3623 | QCBOREncode_AddTB64URLText(pMe, QCBOR_ENCODE_AS_TAG, B64Text); |
| 3624 | } |
| 3625 | |
| 3626 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3627 | QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pMe, |
| 3628 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3629 | const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3630 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3631 | QCBOREncode_AddTB64URLTextToMapSZ(pMe, |
| 3632 | szLabel, |
| 3633 | QCBOR_ENCODE_AS_TAG, |
| 3634 | B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3638 | QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3639 | const int64_t nLabel, |
| 3640 | const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3641 | { |
| 3642 | QCBOREncode_AddTB64URLTextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3646 | |
| 3647 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3648 | QCBOREncode_AddTRegex(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3649 | const uint8_t uTagRequirement, |
| 3650 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3651 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3652 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3653 | QCBOREncode_AddTag(pMe, CBOR_TAG_REGEX); |
| 3654 | } |
| 3655 | QCBOREncode_AddText(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3656 | } |
| 3657 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3658 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3659 | QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pMe, |
| 3660 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3661 | const uint8_t uTagRequirement, |
| 3662 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3663 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3664 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3665 | QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3666 | } |
| 3667 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3668 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3669 | QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3670 | const int64_t nLabel, |
| 3671 | const uint8_t uTagRequirement, |
| 3672 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3673 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3674 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3675 | QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes); |
| 3676 | } |
| 3677 | |
| 3678 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3679 | QCBOREncode_AddRegex(QCBOREncodeContext *pMe, const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3680 | { |
| 3681 | QCBOREncode_AddTRegex(pMe, QCBOR_ENCODE_AS_TAG, Bytes); |
| 3682 | } |
| 3683 | |
| 3684 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3685 | QCBOREncode_AddRegexToMap(QCBOREncodeContext *pMe, |
| 3686 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3687 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3688 | { |
| 3689 | QCBOREncode_AddTRegexToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes); |
| 3690 | } |
| 3691 | |
| 3692 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3693 | QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3694 | const int64_t nLabel, |
| 3695 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3696 | { |
| 3697 | QCBOREncode_AddTRegexToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes); |
| 3698 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3702 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3703 | QCBOREncode_AddTMIMEData(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3704 | const uint8_t uTagRequirement, |
| 3705 | const UsefulBufC MIMEData) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3706 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3707 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3708 | QCBOREncode_AddTag(pMe, CBOR_TAG_BINARY_MIME); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3709 | } |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3710 | QCBOREncode_AddBytes(pMe, MIMEData); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3711 | } |
| 3712 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3713 | static inline void |
| 3714 | QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pMe, |
| 3715 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3716 | const uint8_t uTagRequirement, |
| 3717 | const UsefulBufC MIMEData) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3718 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3719 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3720 | QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3721 | } |
| 3722 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3723 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3724 | QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3725 | const int64_t nLabel, |
| 3726 | const uint8_t uTagRequirement, |
| 3727 | const UsefulBufC MIMEData) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3728 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3729 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3730 | QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData); |
| 3731 | } |
| 3732 | |
| 3733 | static inline void |
| 3734 | QCBOREncode_AddMIMEData(QCBOREncodeContext *pMe, UsefulBufC MIMEData) |
| 3735 | { |
| 3736 | QCBOREncode_AddTMIMEData(pMe, QCBOR_ENCODE_AS_TAG, MIMEData); |
| 3737 | } |
| 3738 | |
| 3739 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3740 | QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pMe, |
| 3741 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3742 | const UsefulBufC MIMEData) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3743 | { |
| 3744 | QCBOREncode_AddTMIMEDataToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, MIMEData); |
| 3745 | } |
| 3746 | |
| 3747 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3748 | QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3749 | const int64_t nLabel, |
| 3750 | const UsefulBufC MIMEData) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3751 | { |
| 3752 | QCBOREncode_AddTMIMEDataToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, MIMEData); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3756 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3757 | QCBOREncode_AddTDateString(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3758 | const uint8_t uTagRequirement, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3759 | const char *szDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3760 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3761 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3762 | QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_STRING); |
| 3763 | } |
| 3764 | QCBOREncode_AddSZString(pMe, szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3765 | } |
| 3766 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3767 | static inline void |
| 3768 | QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pMe, |
| 3769 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3770 | const uint8_t uTagRequirement, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3771 | const char *szDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3772 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3773 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3774 | QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3775 | } |
| 3776 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3777 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3778 | QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3779 | const int64_t nLabel, |
| 3780 | const uint8_t uTagRequirement, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3781 | const char *szDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3782 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3783 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3784 | QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate); |
| 3785 | } |
| 3786 | |
| 3787 | static inline void |
| 3788 | QCBOREncode_AddDateString(QCBOREncodeContext *pMe, const char *szDate) |
| 3789 | { |
| 3790 | QCBOREncode_AddTDateString(pMe, QCBOR_ENCODE_AS_TAG, szDate); |
| 3791 | } |
| 3792 | |
| 3793 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3794 | QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pMe, |
| 3795 | const char *szLabel, |
| 3796 | const char *szDate) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3797 | { |
| 3798 | QCBOREncode_AddTDateStringToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, szDate); |
| 3799 | } |
| 3800 | |
| 3801 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3802 | QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3803 | const int64_t nLabel, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3804 | const char *szDate) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3805 | { |
| 3806 | QCBOREncode_AddTDateStringToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3810 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3811 | QCBOREncode_AddTDaysString(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3812 | const uint8_t uTagRequirement, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3813 | const char *szDate) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3814 | { |
| 3815 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3816 | QCBOREncode_AddTag(pMe, CBOR_TAG_DAYS_STRING); |
| 3817 | } |
| 3818 | QCBOREncode_AddSZString(pMe, szDate); |
| 3819 | } |
| 3820 | |
| 3821 | static inline void |
| 3822 | QCBOREncode_AddTDaysStringToMapSZ(QCBOREncodeContext *pMe, |
| 3823 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3824 | const uint8_t uTagRequirement, |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3825 | const char *szDate) |
| 3826 | { |
| 3827 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3828 | QCBOREncode_AddTDaysString(pMe, uTagRequirement, szDate); |
| 3829 | } |
| 3830 | |
| 3831 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3832 | QCBOREncode_AddTDaysStringToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3833 | const int64_t nLabel, |
| 3834 | const uint8_t uTagRequirement, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3835 | const char *szDate) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3836 | { |
| 3837 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3838 | QCBOREncode_AddTDaysString(pMe, uTagRequirement, szDate); |
| 3839 | } |
| 3840 | |
| 3841 | |
| 3842 | |
| 3843 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3844 | QCBOREncode_Private_AddSimple(QCBOREncodeContext *pMe, const uint64_t uNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3845 | { |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 3846 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 3847 | if(pMe->uMode >= QCBOR_ENCODE_MODE_DCBOR) { |
| 3848 | if(uNum < CBOR_SIMPLEV_FALSE || |
| 3849 | uNum > CBOR_SIMPLEV_NULL) { |
| 3850 | pMe->uError = QCBOR_ERR_NOT_PREFERRED; |
| 3851 | return; |
| 3852 | } |
| 3853 | } |
| 3854 | #endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 3855 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3856 | QCBOREncode_Private_AddType7(pMe, 0, uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3857 | } |
| 3858 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3859 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3860 | QCBOREncode_Private_AddSimpleToMap(QCBOREncodeContext *pMe, |
| 3861 | const char *szLabel, |
| 3862 | const uint8_t uSimple) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3863 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3864 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3865 | QCBOREncode_Private_AddSimple(pMe, uSimple); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3866 | } |
| 3867 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3868 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3869 | QCBOREncode_Private_AddSimpleToMapN(QCBOREncodeContext *pMe, |
| 3870 | const int64_t nLabel, |
| 3871 | const uint8_t uSimple) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3872 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3873 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3874 | QCBOREncode_Private_AddSimple(pMe, uSimple); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3878 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3879 | QCBOREncode_AddBool(QCBOREncodeContext *pMe, const bool b) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3880 | { |
| 3881 | uint8_t uSimple = CBOR_SIMPLEV_FALSE; |
| 3882 | if(b) { |
| 3883 | uSimple = CBOR_SIMPLEV_TRUE; |
| 3884 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3885 | QCBOREncode_Private_AddSimple(pMe, uSimple); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3886 | } |
| 3887 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3888 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3889 | QCBOREncode_AddBoolToMap(QCBOREncodeContext *pMe, const char *szLabel, const bool b) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3890 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3891 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3892 | QCBOREncode_AddBool(pMe, b); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3893 | } |
| 3894 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3895 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3896 | QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pMe, const int64_t nLabel, const bool b) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3897 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3898 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3899 | QCBOREncode_AddBool(pMe, b); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3903 | static inline void |
| 3904 | QCBOREncode_AddNULL(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3905 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3906 | QCBOREncode_Private_AddSimple(pMe, CBOR_SIMPLEV_NULL); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3907 | } |
| 3908 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3909 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3910 | QCBOREncode_AddNULLToMap(QCBOREncodeContext *pMe, const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3911 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3912 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3913 | QCBOREncode_AddNULL(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3914 | } |
| 3915 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3916 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3917 | QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pMe, const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3918 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3919 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3920 | QCBOREncode_AddNULL(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3924 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3925 | QCBOREncode_AddUndef(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3926 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3927 | QCBOREncode_Private_AddSimple(pMe, CBOR_SIMPLEV_UNDEF); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3928 | } |
| 3929 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3930 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3931 | QCBOREncode_AddUndefToMap(QCBOREncodeContext *pMe, const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3932 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3933 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3934 | QCBOREncode_AddUndef(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3935 | } |
| 3936 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3937 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3938 | QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pMe, const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3939 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3940 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3941 | QCBOREncode_AddUndef(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3942 | } |
| 3943 | |
| 3944 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3945 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3946 | QCBOREncode_OpenArray(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3947 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3948 | QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3949 | } |
| 3950 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3951 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3952 | QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pMe, const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3953 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3954 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3955 | QCBOREncode_OpenArray(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3956 | } |
| 3957 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3958 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3959 | QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pMe, const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3960 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3961 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3962 | QCBOREncode_OpenArray(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3963 | } |
| 3964 | |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 3965 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3966 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3967 | QCBOREncode_CloseArray(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3968 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3969 | QCBOREncode_Private_CloseMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3973 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3974 | QCBOREncode_OpenMap(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3975 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3976 | QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_MAP); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3977 | } |
| 3978 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3979 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3980 | QCBOREncode_OpenMapInMap(QCBOREncodeContext *pMe, const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3981 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3982 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3983 | QCBOREncode_OpenMap(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3984 | } |
| 3985 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3986 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3987 | QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pMe, const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3988 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3989 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3990 | QCBOREncode_OpenMap(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3991 | } |
| 3992 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3993 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3994 | QCBOREncode_CloseMap(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3995 | { |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 3996 | (pMe->pfnCloseMap)(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3997 | } |
| 3998 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3999 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4000 | QCBOREncode_OpenArrayIndefiniteLength(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4001 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4002 | QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4003 | } |
| 4004 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4005 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 4006 | QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pMe, |
| 4007 | const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4008 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4009 | QCBOREncode_AddSZString(pMe, szLabel); |
| 4010 | QCBOREncode_OpenArrayIndefiniteLength(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4011 | } |
| 4012 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4013 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 4014 | QCBOREncode_OpenArrayIndefiniteLengthInMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4015 | const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4016 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4017 | QCBOREncode_AddInt64(pMe, nLabel); |
| 4018 | QCBOREncode_OpenArrayIndefiniteLength(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4019 | } |
| 4020 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4021 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4022 | QCBOREncode_CloseArrayIndefiniteLength(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4023 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4024 | QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4028 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4029 | QCBOREncode_OpenMapIndefiniteLength(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4030 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4031 | QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4032 | } |
| 4033 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4034 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 4035 | QCBOREncode_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pMe, |
| 4036 | const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4037 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4038 | QCBOREncode_AddSZString(pMe, szLabel); |
| 4039 | QCBOREncode_OpenMapIndefiniteLength(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4040 | } |
| 4041 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4042 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 4043 | QCBOREncode_OpenMapIndefiniteLengthInMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4044 | const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4045 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4046 | QCBOREncode_AddInt64(pMe, nLabel); |
| 4047 | QCBOREncode_OpenMapIndefiniteLength(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4048 | } |
| 4049 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4050 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4051 | QCBOREncode_CloseMapIndefiniteLength(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4052 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4053 | QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4057 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4058 | QCBOREncode_BstrWrap(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4059 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4060 | QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_BYTE_STRING); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4061 | } |
| 4062 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4063 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4064 | QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pMe, const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4065 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4066 | QCBOREncode_AddSZString(pMe, szLabel); |
| 4067 | QCBOREncode_BstrWrap(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4068 | } |
| 4069 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4070 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4071 | QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pMe, const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4072 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4073 | QCBOREncode_AddInt64(pMe, nLabel); |
| 4074 | QCBOREncode_BstrWrap(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4075 | } |
| 4076 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4077 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4078 | QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pMe, UsefulBufC *pWrappedCBOR) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4079 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4080 | QCBOREncode_CloseBstrWrap2(pMe, true, pWrappedCBOR); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4084 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4085 | QCBOREncode_AddEncoded(QCBOREncodeContext *pMe, const UsefulBufC Encoded) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4086 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4087 | QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_NONE_TYPE_RAW, Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4088 | } |
| 4089 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4090 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 4091 | QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pMe, |
| 4092 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4093 | const UsefulBufC Encoded) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4094 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4095 | QCBOREncode_AddSZString(pMe, szLabel); |
| 4096 | QCBOREncode_AddEncoded(pMe, Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4097 | } |
| 4098 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4099 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 4100 | QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4101 | const int64_t nLabel, |
| 4102 | const UsefulBufC Encoded) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4103 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4104 | QCBOREncode_AddInt64(pMe, nLabel); |
| 4105 | QCBOREncode_AddEncoded(pMe, Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4106 | } |
| 4107 | |
| 4108 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4109 | static inline int |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4110 | QCBOREncode_IsBufferNULL(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4111 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4112 | return UsefulOutBuf_IsBufferNULL(&(pMe->OutBuf)); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4113 | } |
| 4114 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 4115 | static inline QCBORError |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4116 | QCBOREncode_GetErrorState(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4117 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4118 | if(UsefulOutBuf_GetError(&(pMe->OutBuf))) { |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4119 | // Items didn't fit in the buffer. |
| 4120 | // This check catches this condition for all the appends and inserts |
| 4121 | // so checks aren't needed when the appends and inserts are performed. |
| 4122 | // And of course UsefulBuf will never overrun the input buffer given |
| 4123 | // to it. No complex analysis of the error handling in this file is |
| 4124 | // needed to know that is true. Just read the UsefulBuf code. |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4125 | pMe->uError = QCBOR_ERR_BUFFER_TOO_SMALL; |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4126 | // QCBOR_ERR_BUFFER_TOO_SMALL masks other errors, but that is |
| 4127 | // OK. Once the caller fixes this, they'll be unmasked. |
| 4128 | } |
| 4129 | |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 4130 | return (QCBORError)pMe->uError; |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 4134 | /* ======================================================================== |
| 4135 | END OF PRIVATE INLINE IMPLEMENTATION |
| 4136 | ======================================================================== */ |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 4137 | |
| 4138 | #ifdef __cplusplus |
| 4139 | } |
| 4140 | #endif |
| 4141 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 4142 | #endif /* qcbor_encode_h */ |