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 | * |
| 438 | * If allocating on the stack the convenience macro |
| 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 | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 472 | * @brief Add a signed 64-bit integer to the encoded output. |
| 473 | * |
| 474 | * @param[in] pCtx The encoding context to add the integer to. |
| 475 | * @param[in] nNum The integer to add. |
| 476 | * |
| 477 | * The integer will be encoded and added to the CBOR output. |
| 478 | * |
| 479 | * This function figures out the size and the sign and encodes in the |
| 480 | * correct minimal CBOR. Specifically, it will select CBOR major type |
| 481 | * 0 or 1 based on sign and will encode to 1, 2, 4 or 8 bytes |
| 482 | * depending on the value of the integer. Values less than 24 |
| 483 | * effectively encode to one byte because they are encoded in with the |
| 484 | * CBOR major type. This is a neat and efficient characteristic of |
| 485 | * CBOR that can be taken advantage of when designing CBOR-based |
| 486 | * protocols. If integers like tags can be kept between -23 and 23 |
| 487 | * they will be encoded in one byte including the major type. |
| 488 | * |
| 489 | * If you pass a smaller int, say an @c int16_t or a small value, say |
| 490 | * 100, the encoding will still be CBOR's most compact that can |
| 491 | * represent the value. For example, CBOR always encodes the value 0 |
| 492 | * as one byte, 0x00. The representation as 0x00 includes |
| 493 | * identification of the type as an integer too as the major type for |
| 494 | * an integer is 0. See [RFC 8949] |
| 495 | * (https://tools.ietf.org/html/rfc8949) Appendix A for more examples |
| 496 | * of CBOR encoding. This compact encoding is also preferred |
| 497 | * serialization CBOR as per section 34.1 in RFC 8949. |
| 498 | * |
| 499 | * There are no functions to add @c int16_t or @c int32_t because they |
| 500 | * are not necessary because this always encodes to the smallest |
| 501 | * number of bytes based on the value (If this code is running on a |
| 502 | * 32-bit machine having a way to add 32-bit integers would reduce |
| 503 | * code size some). |
| 504 | * |
| 505 | * If the encoding context is in an error state, this will do |
| 506 | * nothing. If an error occurs when adding this integer, the internal |
| 507 | * error flag will be set, and the error will be returned when |
| 508 | * QCBOREncode_Finish() is called. |
| 509 | * |
| 510 | * See also QCBOREncode_AddUInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 511 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 512 | void |
| 513 | QCBOREncode_AddInt64(QCBOREncodeContext *pCtx, int64_t nNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 514 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 515 | static void |
| 516 | QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 517 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 518 | static void |
| 519 | QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 520 | |
| 521 | |
| 522 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 523 | * @brief Add an unsigned 64-bit integer to the encoded output. |
| 524 | * |
| 525 | * @param[in] pCtx The encoding context to add the integer to. |
| 526 | * @param[in] uNum The integer to add. |
| 527 | * |
| 528 | * The integer will be encoded and added to the CBOR output. |
| 529 | * |
| 530 | * The only reason so use this function is for integers larger than |
| 531 | * @c INT64_MAX and smaller than @c UINT64_MAX. Otherwise |
| 532 | * QCBOREncode_AddInt64() will work fine. |
| 533 | * |
| 534 | * Error handling is the same as for QCBOREncode_AddInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 535 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 536 | void |
| 537 | QCBOREncode_AddUInt64(QCBOREncodeContext *pCtx, uint64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 538 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 539 | static void |
| 540 | QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 541 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 542 | static void |
| 543 | QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 544 | |
| 545 | |
| 546 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 547 | * @brief Add a UTF-8 text string to the encoded output. |
| 548 | * |
| 549 | * @param[in] pCtx The encoding context to add the text to. |
| 550 | * @param[in] Text Pointer and length of text to add. |
| 551 | * |
| 552 | * The text passed in must be unencoded UTF-8 according to [RFC 3629] |
| 553 | * (https://tools.ietf.org/html/rfc3629). There is no NULL |
| 554 | * termination. The text is added as CBOR major type 3. |
| 555 | * |
| 556 | * If called with @c nBytesLen equal to 0, an empty string will be |
| 557 | * added. When @c nBytesLen is 0, @c pBytes may be @c NULL. |
| 558 | * |
| 559 | * Note that the restriction of the buffer length to a @c uint32_t is |
| 560 | * entirely intentional as this encoder is not capable of encoding |
| 561 | * lengths greater. This limit to 4GB for a text string should not be |
| 562 | * a problem. |
| 563 | * |
| 564 | * Text lines in Internet protocols (on the wire) are delimited by |
| 565 | * either a CRLF or just an LF. Officially many protocols specify |
| 566 | * CRLF, but implementations often work with either. CBOR type 3 text |
| 567 | * can be either line ending, even a mixture of both. |
| 568 | * |
| 569 | * Operating systems usually have a line end convention. Windows uses |
| 570 | * CRLF. Linux and MacOS use LF. Some applications on a given OS may |
| 571 | * work with either and some may not. |
| 572 | * |
| 573 | * The majority of use cases and CBOR protocols using type 3 text will |
| 574 | * work with either line ending. However, some use cases or protocols |
| 575 | * may not work with either in which case translation to and/or from |
| 576 | * the local line end convention, typically that of the OS, is |
| 577 | * necessary. |
| 578 | * |
| 579 | * QCBOR does no line ending translation for type 3 text when encoding |
| 580 | * and decoding. |
| 581 | * |
| 582 | * Error handling is the same as QCBOREncode_AddInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 583 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 584 | static void |
| 585 | QCBOREncode_AddText(QCBOREncodeContext *pCtx, UsefulBufC Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 586 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 587 | static void |
| 588 | QCBOREncode_AddTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 589 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 590 | static void |
| 591 | QCBOREncode_AddTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 592 | |
| 593 | |
| 594 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 595 | * @brief Add a UTF-8 text string to the encoded output. |
| 596 | * |
| 597 | * @param[in] pCtx The encoding context to add the text to. |
| 598 | * @param[in] szString Null-terminated text to add. |
| 599 | * |
| 600 | * This works the same as QCBOREncode_AddText(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 601 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 602 | static void |
| 603 | QCBOREncode_AddSZString(QCBOREncodeContext *pCtx, const char *szString); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 604 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 605 | static void |
| 606 | QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szString); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 607 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 608 | static void |
| 609 | QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szString); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 610 | |
| 611 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 612 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 613 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 614 | * @brief Add a double-precision floating-point number to the encoded output. |
| 615 | * |
| 616 | * @param[in] pCtx The encoding context to add the double to. |
| 617 | * @param[in] dNum The double-precision number to add. |
| 618 | * |
| 619 | * This encodes and outputs a floating-point number. CBOR major type 7 |
| 620 | * is used. |
| 621 | * |
| 622 | * This implements preferred serialization, selectively encoding the |
| 623 | * double-precision floating-point number as either double-precision, |
| 624 | * single-precision or half-precision. Infinity, NaN and 0 are always |
| 625 | * encoded as half-precision. If no precision will be lost in the |
| 626 | * conversion to half-precision, then it will be converted and |
| 627 | * encoded. If not and no precision will be lost in conversion to |
| 628 | * single-precision, then it will be converted and encoded. If not, |
| 629 | * then no conversion is performed, and it encoded as a |
| 630 | * double-precision. |
| 631 | * |
| 632 | * Half-precision floating-point numbers take up 2 bytes, half that of |
| 633 | * single-precision, one quarter of double-precision |
| 634 | * |
| 635 | * This automatically reduces the size of encoded CBOR, maybe even by |
| 636 | * four if most of values are 0, infinity or NaN. |
| 637 | * |
| 638 | * When decoded, QCBOR will usually return these values as |
| 639 | * double-precision. |
| 640 | * |
| 641 | * It is possible to disable this preferred serialization when compiling |
| 642 | * QCBOR. In that case, this functions the same as |
| 643 | * QCBOREncode_AddDoubleNoPreferred(). |
| 644 | * |
| 645 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 646 | * |
| 647 | * See also QCBOREncode_AddDoubleNoPreferred(), QCBOREncode_AddFloat() |
| 648 | * and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point. |
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_AddDouble(QCBOREncodeContext *pCtx, double dNum); |
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_AddDoubleToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum); |
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_AddDoubleToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum); |
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 a single-precision floating-point number to the encoded output. |
| 662 | * |
| 663 | * @param[in] pCtx The encoding context to add the double to. |
| 664 | * @param[in] fNum The single-precision number to add. |
| 665 | * |
| 666 | * This is identical to QCBOREncode_AddDouble() except the input is |
| 667 | * single-precision. |
| 668 | * |
| 669 | * See also QCBOREncode_AddDouble(), QCBOREncode_AddDoubleNoPreferred(), |
| 670 | * and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point. |
| 671 | */ |
| 672 | void |
| 673 | QCBOREncode_AddFloat(QCBOREncodeContext *pCtx, float fNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 674 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 675 | static void |
| 676 | QCBOREncode_AddFloatToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 677 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 678 | static void |
| 679 | QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float dNum); |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 680 | |
| 681 | |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 682 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 683 | * @brief Add a double-precision floating-point number without preferred encoding. |
| 684 | * |
| 685 | * @param[in] pCtx The encoding context to add the double to. |
| 686 | * @param[in] dNum The double-precision number to add. |
| 687 | * |
| 688 | * This always outputs the number as a 64-bit double-precision. |
| 689 | * Preferred serialization is not used. |
| 690 | * |
| 691 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 692 | * |
| 693 | * See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and |
| 694 | * QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point. |
| 695 | */ |
| 696 | void |
| 697 | QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pCtx, double dNum); |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 698 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 699 | static void |
| 700 | QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 701 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 702 | static void |
| 703 | QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 704 | |
| 705 | |
| 706 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 707 | * @brief Add a single-precision floating-point number without preferred encoding. |
| 708 | * |
| 709 | * @param[in] pCtx The encoding context to add the double to. |
| 710 | * @param[in] fNum The single-precision number to add. |
| 711 | * |
| 712 | * This always outputs the number as a 32-bit single-precision. |
| 713 | * Preferred serialization is not used. |
| 714 | * |
| 715 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 716 | * |
| 717 | * See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and |
| 718 | * QCBOREncode_AddDoubleNoPreferred() and @ref Floating-Point. |
| 719 | */ |
| 720 | void |
| 721 | QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pCtx, float fNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 722 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 723 | static void |
| 724 | QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 725 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 726 | static void |
| 727 | 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] | 728 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 729 | |
| 730 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 731 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 732 | * @brief Add an optional tag. |
| 733 | * |
| 734 | * @param[in] pCtx The encoding context to add the tag to. |
| 735 | * @param[in] uTag The tag to add |
| 736 | * |
| 737 | * This outputs a CBOR major type 6 item that tags the next data item |
| 738 | * that is output usually to indicate it is some new data type. |
| 739 | * |
| 740 | * For many of the common standard tags, a function to encode data |
| 741 | * using it is provided and this is not needed. For example, |
| 742 | * QCBOREncode_AddDateEpoch() already exists to output integers |
| 743 | * representing dates with the right tag. |
| 744 | * |
| 745 | * The tag is applied to the next data item added to the encoded |
| 746 | * output. That data item that is to be tagged can be of any major |
| 747 | * CBOR type. Any number of tags can be added to a data item by |
| 748 | * calling this multiple times before the data item is added. |
| 749 | * |
| 750 | * See @ref Tags-Overview for discussion of creating new non-standard |
| 751 | * tags. See QCBORDecode_GetNext() for discussion of decoding custom |
| 752 | * tags. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 753 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 754 | void |
| 755 | QCBOREncode_AddTag(QCBOREncodeContext *pCtx, uint64_t uTag); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 756 | |
| 757 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 758 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 759 | * @brief Add an epoch-based date. |
| 760 | * |
| 761 | * @param[in] pCtx The encoding context to add the date to. |
| 762 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 763 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 764 | * @param[in] nDate Number of seconds since 1970-01-01T00:00Z |
| 765 | * in UTC time. |
| 766 | * |
| 767 | * As per RFC 8949 this is similar to UNIX/Linux/POSIX dates. This is |
| 768 | * the most compact way to specify a date and time in CBOR. Note that |
| 769 | * this is always UTC and does not include the time zone. Use |
| 770 | * QCBOREncode_AddDateString() if you want to include the time zone. |
| 771 | * |
| 772 | * The preferred integer serialization rules apply here so the date will be |
| 773 | * encoded in a minimal number of bytes. Until about the year 2106 |
| 774 | * these dates will encode in 6 bytes -- one byte for the tag, one |
| 775 | * byte for the type and 4 bytes for the integer. After that it will |
| 776 | * encode to 10 bytes. |
| 777 | * |
| 778 | * Negative values are supported for dates before 1970. |
| 779 | * |
| 780 | * If you care about leap-seconds and that level of accuracy, make sure |
| 781 | * the system you are running this code on does it correctly. This code |
| 782 | * just takes the value passed in. |
| 783 | * |
| 784 | * This implementation cannot encode fractional seconds using float or |
| 785 | * double even though that is allowed by CBOR, but you can encode them |
| 786 | * if you want to by calling QCBOREncode_AddTag() and QCBOREncode_AddDouble(). |
| 787 | * |
| 788 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 789 | * |
| 790 | * See also QCBOREncode_AddTDaysEpoch(). |
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_AddTDateEpoch(QCBOREncodeContext *pCtx, |
| 794 | uint8_t uTagRequirement, |
| 795 | int64_t nDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 796 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 797 | static void |
| 798 | QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pCtx, |
| 799 | const char *szLabel, |
| 800 | uint8_t uTagRequirement, |
| 801 | int64_t nDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 802 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 803 | static void |
| 804 | QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pCtx, |
| 805 | int64_t nLabel, |
| 806 | uint8_t uTagRequirement, |
| 807 | int64_t nDate); |
| 808 | |
| 809 | |
| 810 | static void |
| 811 | QCBOREncode_AddDateEpoch(QCBOREncodeContext *pCtx, |
| 812 | int64_t nDate); |
| 813 | |
| 814 | static void |
| 815 | QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pCtx, |
| 816 | const char *szLabel, |
| 817 | int64_t nDate); |
| 818 | |
| 819 | static void |
| 820 | QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pCtx, |
| 821 | int64_t nLabel, |
| 822 | int64_t nDate); |
| 823 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 824 | |
| 825 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 826 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 827 | * @brief Add an epoch-based day-count date. |
| 828 | * |
| 829 | * @param[in] pCtx The encoding context to add the date to. |
| 830 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 831 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 832 | * @param[in] nDays Number of days before or after 1970-01-0. |
| 833 | * |
| 834 | * This date format is described in |
| 835 | * [RFC 8943] (https://tools.ietf.org/html/rfc8943). |
| 836 | * |
| 837 | * The preferred integer serialization rules apply here so the date |
| 838 | * will be encoded in a minimal number of bytes. Until about the year |
| 839 | * 2149 these dates will encode in 4 bytes -- one byte for the tag, |
| 840 | * one byte for the type and 2 bytes for the integer. |
| 841 | * |
| 842 | * See also QCBOREncode_AddTDateEpoch(). |
| 843 | */ |
| 844 | static void |
| 845 | QCBOREncode_AddTDaysEpoch(QCBOREncodeContext *pCtx, |
| 846 | uint8_t uTagRequirement, |
| 847 | int64_t nDays); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 848 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 849 | static void |
| 850 | QCBOREncode_AddTDaysEpochToMapSZ(QCBOREncodeContext *pCtx, |
| 851 | const char *szLabel, |
| 852 | uint8_t uTagRequirement, |
| 853 | int64_t nDays); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 854 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 855 | static void |
| 856 | QCBOREncode_AddTDaysEpochToMapN(QCBOREncodeContext *pCtx, |
| 857 | int64_t nLabel, |
| 858 | uint8_t uTagRequirement, |
| 859 | int64_t nDays); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 860 | |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 861 | |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 862 | |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 863 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 864 | /** |
| 865 | * @brief Add a byte string to the encoded output. |
| 866 | * |
| 867 | * @param[in] pCtx The encoding context to add the bytes to. |
| 868 | * @param[in] Bytes Pointer and length of the input data. |
| 869 | * |
| 870 | * Simply adds the bytes to the encoded output as CBOR major type 2. |
| 871 | * |
| 872 | * If called with @c Bytes.len equal to 0, an empty string will be |
| 873 | * added. When @c Bytes.len is 0, @c Bytes.ptr may be @c NULL. |
| 874 | * |
| 875 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 876 | */ |
| 877 | static void |
| 878 | QCBOREncode_AddBytes(QCBOREncodeContext *pCtx, UsefulBufC Bytes); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 879 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 880 | static void |
| 881 | QCBOREncode_AddBytesToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes); |
| 882 | |
| 883 | static void |
| 884 | QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes); |
| 885 | |
| 886 | |
| 887 | /** |
| 888 | * @brief Set up to write a byte string value directly to encoded output. |
| 889 | * |
| 890 | * @param[in] pCtx The encoding context to add the bytes to. |
| 891 | * @param[out] pPlace Pointer and length of place to write byte string value. |
| 892 | * |
| 893 | * QCBOREncode_AddBytes() is the normal way to encode a byte string. |
| 894 | * This is for special cases and by passes some of the pointer safety. |
| 895 | * |
| 896 | * The purpose of this is to output the bytes that make up a byte |
| 897 | * string value directly to the QCBOR output buffer so you don't need |
| 898 | * to have a copy of it in memory. This is particularly useful if the |
| 899 | * byte string is large, for example, the encrypted payload of a |
| 900 | * COSE_Encrypt message. The payload encryption algorithm can output |
| 901 | * directly to the encoded CBOR buffer, perhaps by making it the |
| 902 | * output buffer for some function (e.g. symmetric encryption) or by |
| 903 | * multiple writes. |
| 904 | * |
| 905 | * The pointer in @c pPlace is where to start writing. Writing is just |
| 906 | * copying bytes to the location by the pointer in \c pPlace. Writing |
| 907 | * past the length in @c pPlace will be writing off the end of the |
| 908 | * output buffer. |
| 909 | * |
| 910 | * If there is no room in the output buffer @ref NULLUsefulBuf will be |
| 911 | * returned and there is no need to call QCBOREncode_CloseBytes(). |
| 912 | * |
| 913 | * The byte string must be closed by calling QCBOREncode_CloseBytes(). |
| 914 | * |
| 915 | * Warning: this bypasses some of the usual checks provided by QCBOR |
| 916 | * against writing off the end of the encoded output buffer. |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 917 | */ |
| 918 | void |
| 919 | QCBOREncode_OpenBytes(QCBOREncodeContext *pCtx, UsefulBuf *pPlace); |
| 920 | |
| 921 | static void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 922 | QCBOREncode_OpenBytesInMapSZ(QCBOREncodeContext *pCtx, |
| 923 | const char *szLabel, |
| 924 | UsefulBuf *pPlace); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 925 | |
| 926 | static void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 927 | QCBOREncode_OpenBytesInMapN(QCBOREncodeContext *pCtx, |
| 928 | int64_t nLabel, |
| 929 | UsefulBuf *pPlace); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 930 | |
| 931 | |
| 932 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 933 | * @brief Close out a byte string written directly to encoded output. |
| 934 | * |
| 935 | * @param[in] pCtx The encoding context to add the bytes to. |
| 936 | * @param[out] uAmount The number of bytes written, the length of the |
| 937 | * byte string. |
| 938 | * |
| 939 | * This closes out a call to QCBOREncode_OpenBytes(). This inserts a |
| 940 | * CBOR header at the front of the byte string value to make it a |
| 941 | * well-formed byte string. |
| 942 | * |
| 943 | * If there was no call to QCBOREncode_OpenBytes() then @ref |
| 944 | * QCBOR_ERR_TOO_MANY_CLOSES is set. |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 945 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 946 | void |
| 947 | QCBOREncode_CloseBytes(QCBOREncodeContext *pCtx, size_t uAmount); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 948 | |
| 949 | |
| 950 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 951 | * @brief Add a binary UUID to the encoded output. |
| 952 | * |
| 953 | * @param[in] pCtx The encoding context to add the UUID to. |
| 954 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 955 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 956 | * @param[in] Bytes Pointer and length of the binary UUID. |
| 957 | * |
| 958 | * A binary UUID as defined in [RFC 4122] |
| 959 | * (https://tools.ietf.org/html/rfc4122) is added to the output. |
| 960 | * |
| 961 | * It is output as CBOR major type 2, a binary string, with tag @ref |
| 962 | * CBOR_TAG_BIN_UUID indicating the binary string is a UUID. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 963 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 964 | static void |
| 965 | QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pCtx, |
| 966 | uint8_t uTagRequirement, |
| 967 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 968 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 969 | static void |
| 970 | QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pCtx, |
| 971 | const char *szLabel, |
| 972 | uint8_t uTagRequirement, |
| 973 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 974 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 975 | static void |
| 976 | QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pCtx, |
| 977 | int64_t nLabel, |
| 978 | uint8_t uTagRequirement, |
| 979 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 980 | |
| 981 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 982 | static void |
| 983 | QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pCtx, UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 984 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 985 | static void |
| 986 | QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes); |
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_AddBinaryUUIDToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 990 | |
| 991 | |
| 992 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 993 | * @brief Add a positive big number to the encoded output. |
| 994 | * |
| 995 | * @param[in] pCtx The encoding context to add the big number to. |
| 996 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 997 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 998 | * @param[in] Bytes Pointer and length of the big number. |
| 999 | * |
| 1000 | * Big numbers are integers larger than 64-bits. Their format is |
| 1001 | * described in [RFC 8949] (https://tools.ietf.org/html/rfc8949). |
| 1002 | * |
| 1003 | * It is output as CBOR major type 2, a binary string, with tag |
| 1004 | * @ref CBOR_TAG_POS_BIGNUM indicating the binary string is a positive |
| 1005 | * big number. |
| 1006 | * |
| 1007 | * Often big numbers are used to represent cryptographic keys, |
| 1008 | * however, COSE which defines representations for keys chose not to |
| 1009 | * use this particular type. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1010 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1011 | static void |
| 1012 | QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pCtx, |
| 1013 | uint8_t uTagRequirement, |
| 1014 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1015 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1016 | static void |
| 1017 | QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pCtx, |
| 1018 | const char *szLabel, |
| 1019 | uint8_t uTagRequirement, |
| 1020 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1021 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1022 | static void |
| 1023 | QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pCtx, |
| 1024 | int64_t nLabel, |
| 1025 | uint8_t uTagRequirement, |
| 1026 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1027 | |
| 1028 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1029 | static void |
| 1030 | QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx, |
| 1031 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1032 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1033 | static void |
| 1034 | QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx, |
| 1035 | const char *szLabel, |
| 1036 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1037 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1038 | static void |
| 1039 | QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx, |
| 1040 | int64_t nLabel, |
| 1041 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1042 | |
| 1043 | |
| 1044 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1045 | * @brief Add a negative big number to the encoded output. |
| 1046 | * |
| 1047 | * @param[in] pCtx The encoding context to add the big number to. |
| 1048 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1049 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1050 | * @param[in] Bytes Pointer and length of the big number. |
| 1051 | * |
| 1052 | * Big numbers are integers larger than 64-bits. Their format is |
| 1053 | * described in [RFC 8949] (https://tools.ietf.org/html/rfc8949). |
| 1054 | * |
| 1055 | * It is output as CBOR major type 2, a binary string, with tag |
| 1056 | * @ref CBOR_TAG_NEG_BIGNUM indicating the binary string is a negative |
| 1057 | * big number. |
| 1058 | * |
| 1059 | * Often big numbers are used to represent cryptographic keys, |
| 1060 | * however, COSE which defines representations for keys chose not to |
| 1061 | * use this particular type. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1062 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1063 | static void |
| 1064 | QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pCtx, |
| 1065 | uint8_t uTagRequirement, |
| 1066 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1067 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1068 | static void |
| 1069 | QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pCtx, |
| 1070 | const char *szLabel, |
| 1071 | uint8_t uTagRequirement, |
| 1072 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1073 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1074 | static void |
| 1075 | QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pCtx, |
| 1076 | int64_t nLabel, |
| 1077 | uint8_t uTagRequirement, |
| 1078 | UsefulBufC Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1079 | |
| 1080 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1081 | static void |
| 1082 | QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx, |
| 1083 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1084 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1085 | static void |
| 1086 | QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx, |
| 1087 | const char *szLabel, |
| 1088 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1089 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1090 | static void |
| 1091 | QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx, |
| 1092 | int64_t nLabel, |
| 1093 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1094 | |
| 1095 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 1096 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1097 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1098 | * @brief Add a decimal fraction to the encoded output. |
| 1099 | * |
| 1100 | * @param[in] pCtx Encoding context to add the decimal fraction to. |
| 1101 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1102 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1103 | * @param[in] nMantissa The mantissa. |
| 1104 | * @param[in] nBase10Exponent The exponent. |
| 1105 | * |
| 1106 | * The value is nMantissa * 10 ^ nBase10Exponent. |
| 1107 | * |
| 1108 | * A decimal fraction is good for exact representation of some values |
| 1109 | * that can't be represented exactly with standard C (IEEE 754) |
| 1110 | * floating-point numbers. Much larger and much smaller numbers can |
| 1111 | * also be represented than floating-point because of the larger |
| 1112 | * number of bits in the exponent. |
| 1113 | * |
| 1114 | * The decimal fraction is conveyed as two integers, a mantissa and a |
| 1115 | * base-10 scaling factor. |
| 1116 | * |
| 1117 | * For example, 273.15 is represented by the two integers 27315 and -2. |
| 1118 | * |
| 1119 | * The exponent and mantissa have the range from @c INT64_MIN to |
| 1120 | * @c INT64_MAX for both encoding and decoding (CBOR allows |
| 1121 | * @c -UINT64_MAX to @c UINT64_MAX, but this implementation doesn't |
| 1122 | * support this range to reduce code size and interface complexity a |
| 1123 | * little). |
| 1124 | * |
| 1125 | * CBOR Preferred serialization of the integers is used, thus they |
| 1126 | * will be encoded in the smallest number of bytes possible. |
| 1127 | * |
| 1128 | * See also QCBOREncode_AddDecimalFractionBigNum() for a decimal |
| 1129 | * fraction with arbitrarily large precision and |
| 1130 | * QCBOREncode_AddBigFloat(). |
| 1131 | * |
| 1132 | * There is no representation of positive or negative infinity or NaN |
| 1133 | * (Not a Number). Use QCBOREncode_AddDouble() to encode them. |
| 1134 | * |
| 1135 | * See @ref expAndMantissa for decoded representation. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1136 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1137 | static void |
| 1138 | QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pCtx, |
| 1139 | uint8_t uTagRequirement, |
| 1140 | int64_t nMantissa, |
| 1141 | int64_t nBase10Exponent); |
| 1142 | |
| 1143 | static void |
| 1144 | QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pCtx, |
| 1145 | const char *szLabel, |
| 1146 | uint8_t uTagRequirement, |
| 1147 | int64_t nMantissa, |
| 1148 | int64_t nBase10Exponent); |
| 1149 | |
| 1150 | static void |
| 1151 | QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pCtx, |
| 1152 | int64_t nLabel, |
| 1153 | uint8_t uTagRequirement, |
| 1154 | int64_t nMantissa, |
| 1155 | int64_t nBase10Exponent); |
| 1156 | |
| 1157 | |
| 1158 | static void |
| 1159 | QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pCtx, |
| 1160 | int64_t nMantissa, |
| 1161 | int64_t nBase10Exponent); |
| 1162 | |
| 1163 | static void |
| 1164 | QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx, |
| 1165 | const char *szLabel, |
| 1166 | int64_t nMantissa, |
| 1167 | int64_t nBase10Exponent); |
| 1168 | |
| 1169 | static void |
| 1170 | QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pCtx, |
| 1171 | int64_t nLabel, |
| 1172 | int64_t nMantissa, |
| 1173 | int64_t nBase10Exponent); |
| 1174 | |
| 1175 | |
| 1176 | /** |
| 1177 | * @brief Add a decimal fraction with a big number mantissa to the encoded output. |
| 1178 | * |
| 1179 | * @param[in] pCtx Encoding context to add the decimal fraction to. |
| 1180 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1181 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1182 | * @param[in] Mantissa The mantissa. |
| 1183 | * @param[in] bIsNegative false if mantissa is positive, true if negative. |
| 1184 | * @param[in] nBase10Exponent The exponent. |
| 1185 | * |
| 1186 | * This is the same as QCBOREncode_AddDecimalFraction() except the |
| 1187 | * mantissa is a big number (See QCBOREncode_AddPositiveBignum()) |
| 1188 | * allowing for arbitrarily large precision. |
| 1189 | * |
| 1190 | * See @ref expAndMantissa for decoded representation. |
| 1191 | */ |
| 1192 | static void |
| 1193 | QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pCtx, |
| 1194 | uint8_t uTagRequirement, |
| 1195 | UsefulBufC Mantissa, |
| 1196 | bool bIsNegative, |
| 1197 | int64_t nBase10Exponent); |
| 1198 | |
| 1199 | static void |
| 1200 | QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx, |
| 1201 | const char *szLabel, |
| 1202 | uint8_t uTagRequirement, |
| 1203 | UsefulBufC Mantissa, |
| 1204 | bool bIsNegative, |
| 1205 | int64_t nBase10Exponent); |
| 1206 | |
| 1207 | static void |
| 1208 | QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx, |
| 1209 | int64_t nLabel, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1210 | uint8_t uTagRequirement, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1211 | UsefulBufC Mantissa, |
| 1212 | bool bIsNegative, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1213 | int64_t nBase10Exponent); |
| 1214 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1215 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1216 | static void |
| 1217 | QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pCtx, |
| 1218 | UsefulBufC Mantissa, |
| 1219 | bool bIsNegative, |
| 1220 | int64_t nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1221 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1222 | static void |
| 1223 | QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx, |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 1224 | const char *szLabel, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1225 | UsefulBufC Mantissa, |
| 1226 | bool bIsNegative, |
| 1227 | int64_t nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1228 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1229 | static void |
| 1230 | QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1231 | int64_t nLabel, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1232 | UsefulBufC Mantissa, |
| 1233 | bool bIsNegative, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1234 | int64_t nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1235 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1236 | /** |
| 1237 | * @brief Add a big floating-point number to the encoded output. |
| 1238 | * |
| 1239 | * @param[in] pCtx The encoding context to add the bigfloat to. |
| 1240 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1241 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1242 | * @param[in] nMantissa The mantissa. |
| 1243 | * @param[in] nBase2Exponent The exponent. |
| 1244 | * |
| 1245 | * The value is nMantissa * 2 ^ nBase2Exponent. |
| 1246 | * |
| 1247 | * "Bigfloats", as CBOR terms them, are similar to IEEE floating-point |
| 1248 | * numbers in having a mantissa and base-2 exponent, but they are not |
| 1249 | * supported by hardware or encoded the same. They explicitly use two |
| 1250 | * CBOR-encoded integers to convey the mantissa and exponent, each of |
| 1251 | * which can be 8, 16, 32 or 64 bits. With both the mantissa and |
| 1252 | * exponent 64 bits they can express more precision and a larger range |
| 1253 | * than an IEEE double floating-point number. See |
| 1254 | * QCBOREncode_AddBigFloatBigNum() for even more precision. |
| 1255 | * |
| 1256 | * For example, 1.5 would be represented by a mantissa of 3 and an |
| 1257 | * exponent of -1. |
| 1258 | * |
| 1259 | * The exponent and mantissa have the range from @c INT64_MIN to |
| 1260 | * @c INT64_MAX for both encoding and decoding (CBOR allows @c |
| 1261 | * -UINT64_MAX to @c UINT64_MAX, but this implementation doesn't |
| 1262 | * support this range to reduce code size and interface complexity a |
| 1263 | * little). |
| 1264 | * |
| 1265 | * CBOR preferred serialization of the integers is used, thus they will |
| 1266 | * be encoded in the smallest number of bytes possible. |
| 1267 | * |
| 1268 | * This can also be used to represent floating-point numbers in |
| 1269 | * environments that don't support IEEE 754. |
| 1270 | * |
| 1271 | * See @ref expAndMantissa for decoded representation. |
| 1272 | */ |
| 1273 | static void |
| 1274 | QCBOREncode_AddTBigFloat(QCBOREncodeContext *pCtx, |
| 1275 | uint8_t uTagRequirement, |
| 1276 | int64_t nMantissa, |
| 1277 | int64_t nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1278 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1279 | static void |
| 1280 | QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pCtx, |
| 1281 | const char *szLabel, |
| 1282 | uint8_t uTagRequirement, |
| 1283 | int64_t nMantissa, |
| 1284 | int64_t nBase2Exponent); |
| 1285 | |
| 1286 | static void |
| 1287 | QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pCtx, |
| 1288 | int64_t nLabel, |
| 1289 | uint8_t uTagRequirement, |
| 1290 | int64_t nMantissa, |
| 1291 | int64_t nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1292 | |
| 1293 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1294 | static void |
| 1295 | QCBOREncode_AddBigFloat(QCBOREncodeContext *pCtx, |
| 1296 | int64_t nMantissa, |
| 1297 | int64_t nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1298 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1299 | static void |
| 1300 | QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pCtx, |
| 1301 | const char *szLabel, |
| 1302 | int64_t nMantissa, |
| 1303 | int64_t nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1304 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1305 | static void |
| 1306 | QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pCtx, |
| 1307 | int64_t nLabel, |
| 1308 | int64_t nMantissa, |
| 1309 | int64_t nBase2Exponent); |
| 1310 | |
| 1311 | /** |
| 1312 | * @brief Add a big floating-point number with a big number mantissa to |
| 1313 | * the encoded output. |
| 1314 | * |
| 1315 | * @param[in] pCtx The encoding context to add the bigfloat to. |
| 1316 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1317 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1318 | * @param[in] Mantissa The mantissa. |
| 1319 | * @param[in] bIsNegative false if mantissa is positive, true if negative. |
| 1320 | * @param[in] nBase2Exponent The exponent. |
| 1321 | * |
| 1322 | * This is the same as QCBOREncode_AddBigFloat() except the mantissa |
| 1323 | * is a big number (See QCBOREncode_AddPositiveBignum()) allowing for |
| 1324 | * arbitrary precision. |
| 1325 | * |
| 1326 | * See @ref expAndMantissa for decoded representation. |
| 1327 | */ |
| 1328 | static void |
| 1329 | QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pCtx, |
| 1330 | uint8_t uTagRequirement, |
| 1331 | UsefulBufC Mantissa, |
| 1332 | bool bIsNegative, |
| 1333 | int64_t nBase2Exponent); |
| 1334 | |
| 1335 | static void |
| 1336 | QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pCtx, |
| 1337 | const char *szLabel, |
| 1338 | uint8_t uTagRequirement, |
| 1339 | UsefulBufC Mantissa, |
| 1340 | bool bIsNegative, |
| 1341 | int64_t nBase2Exponent); |
| 1342 | |
| 1343 | static void |
| 1344 | QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pCtx, |
| 1345 | int64_t nLabel, |
| 1346 | uint8_t uTagRequirement, |
| 1347 | UsefulBufC Mantissa, |
| 1348 | bool bIsNegative, |
| 1349 | int64_t nBase2Exponent); |
| 1350 | |
| 1351 | |
| 1352 | static void |
| 1353 | QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pCtx, |
| 1354 | UsefulBufC Mantissa, |
| 1355 | bool bIsNegative, |
| 1356 | int64_t nBase2Exponent); |
| 1357 | |
| 1358 | static void |
| 1359 | QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pCtx, |
| 1360 | const char *szLabel, |
| 1361 | UsefulBufC Mantissa, |
| 1362 | bool bIsNegative, |
| 1363 | int64_t nBase2Exponent); |
| 1364 | |
| 1365 | static void |
| 1366 | QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pCtx, |
| 1367 | int64_t nLabel, |
| 1368 | UsefulBufC Mantissa, |
| 1369 | bool bIsNegative, |
| 1370 | int64_t nBase2Exponent); |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 1371 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1372 | |
| 1373 | |
| 1374 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1375 | * @brief Add a text URI to the encoded output. |
| 1376 | * |
| 1377 | * @param[in] pCtx The encoding context to add the URI to. |
| 1378 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1379 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1380 | * @param[in] URI Pointer and length of the URI. |
| 1381 | * |
| 1382 | * The format of URI must be per [RFC 3986] |
| 1383 | * (https://tools.ietf.org/html/rfc3986). |
| 1384 | * |
| 1385 | * It is output as CBOR major type 3, a text string, with tag @ref |
| 1386 | * CBOR_TAG_URI indicating the text string is a URI. |
| 1387 | * |
| 1388 | * A URI in a NULL-terminated string, @c szURI, can be easily added with |
| 1389 | * this code: |
| 1390 | * |
| 1391 | * QCBOREncode_AddURI(pCtx, UsefulBuf_FromSZ(szURI)); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1392 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1393 | static void |
| 1394 | QCBOREncode_AddTURI(QCBOREncodeContext *pCtx, |
| 1395 | uint8_t uTagRequirement, |
| 1396 | UsefulBufC URI); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1397 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1398 | static void |
| 1399 | QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pCtx, |
| 1400 | const char *szLabel, |
| 1401 | uint8_t uTagRequirement, |
| 1402 | UsefulBufC URI); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1403 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1404 | static void |
| 1405 | QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pCtx, |
| 1406 | int64_t nLabel, |
| 1407 | uint8_t uTagRequirement, |
| 1408 | UsefulBufC URI); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1409 | |
| 1410 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1411 | static void |
| 1412 | QCBOREncode_AddURI(QCBOREncodeContext *pCtx, |
| 1413 | UsefulBufC URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1414 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1415 | static void |
| 1416 | QCBOREncode_AddURIToMap(QCBOREncodeContext *pCtx, |
| 1417 | const char *szLabel, |
| 1418 | UsefulBufC URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1419 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1420 | static void |
| 1421 | QCBOREncode_AddURIToMapN(QCBOREncodeContext *pCtx, |
| 1422 | int64_t nLabel, |
| 1423 | UsefulBufC URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1424 | |
| 1425 | |
| 1426 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1427 | * @brief Add Base64-encoded text to encoded output. |
| 1428 | * |
| 1429 | * @param[in] pCtx The encoding context to add the base-64 text to. |
| 1430 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1431 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1432 | * @param[in] B64Text Pointer and length of the base-64 encoded text. |
| 1433 | * |
| 1434 | * The text content is Base64 encoded data per [RFC 4648] |
| 1435 | * (https://tools.ietf.org/html/rfc4648). |
| 1436 | * |
| 1437 | * It is output as CBOR major type 3, a text string, with tag @ref |
| 1438 | * CBOR_TAG_B64 indicating the text string is Base64 encoded. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1439 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1440 | static void |
| 1441 | QCBOREncode_AddTB64Text(QCBOREncodeContext *pCtx, |
| 1442 | uint8_t uTagRequirement, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1443 | UsefulBufC B64Text); |
| 1444 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1445 | static void |
| 1446 | QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pCtx, |
| 1447 | const char *szLabel, |
| 1448 | uint8_t uTagRequirement, |
| 1449 | UsefulBufC B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1450 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1451 | static void |
| 1452 | QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pCtx, |
| 1453 | int64_t nLabel, |
| 1454 | uint8_t uTagRequirement, |
| 1455 | UsefulBufC B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1456 | |
| 1457 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1458 | static void |
| 1459 | QCBOREncode_AddB64Text(QCBOREncodeContext *pCtx, |
| 1460 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1461 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1462 | static void |
| 1463 | QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pCtx, |
| 1464 | const char *szLabel, |
| 1465 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1466 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1467 | static void |
| 1468 | QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pCtx, |
| 1469 | int64_t nLabel, |
| 1470 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1471 | |
| 1472 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1473 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1474 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1475 | * @brief Add base64url encoded data to encoded output. |
| 1476 | * |
| 1477 | * @param[in] pCtx The encoding context to add the base64url to. |
| 1478 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1479 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1480 | * @param[in] B64Text Pointer and length of the base64url encoded text. |
| 1481 | * |
| 1482 | * The text content is base64URL encoded text as per [RFC 4648] |
| 1483 | * (https://tools.ietf.org/html/rfc4648). |
| 1484 | * |
| 1485 | * It is output as CBOR major type 3, a text string, with tag |
| 1486 | * @ref CBOR_TAG_B64URL indicating the text string is a Base64url |
| 1487 | * encoded. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1488 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1489 | static void |
| 1490 | QCBOREncode_AddTB64URLText(QCBOREncodeContext *pCtx, |
| 1491 | uint8_t uTagRequirement, |
| 1492 | UsefulBufC B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1493 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1494 | static void |
| 1495 | QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pCtx, |
| 1496 | const char *szLabel, |
| 1497 | uint8_t uTagRequirement, |
| 1498 | UsefulBufC B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1499 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1500 | static void |
| 1501 | QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pCtx, |
| 1502 | int64_t nLabel, |
| 1503 | uint8_t uTagRequirement, |
| 1504 | UsefulBufC B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1505 | |
| 1506 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1507 | static void |
| 1508 | QCBOREncode_AddB64URLText(QCBOREncodeContext *pCtx, |
| 1509 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1510 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1511 | static void |
| 1512 | QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pCtx, |
| 1513 | const char *szLabel, |
| 1514 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1515 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1516 | static void |
| 1517 | QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pCtx, |
| 1518 | int64_t nLabel, |
| 1519 | UsefulBufC B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1520 | |
| 1521 | |
| 1522 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1523 | * @brief Add Perl Compatible Regular Expression. |
| 1524 | * |
| 1525 | * @param[in] pCtx Encoding context to add the regular expression to. |
| 1526 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1527 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1528 | * @param[in] Regex Pointer and length of the regular expression. |
| 1529 | * |
| 1530 | * The text content is Perl Compatible Regular |
| 1531 | * Expressions (PCRE) / JavaScript syntax [ECMA262]. |
| 1532 | * |
| 1533 | * It is output as CBOR major type 3, a text string, with tag @ref |
| 1534 | * CBOR_TAG_REGEX indicating the text string is a regular expression. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1535 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1536 | static void |
| 1537 | QCBOREncode_AddTRegex(QCBOREncodeContext *pCtx, |
| 1538 | uint8_t uTagRequirement, |
| 1539 | UsefulBufC Regex); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1540 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1541 | static void |
| 1542 | QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pCtx, |
| 1543 | const char *szLabel, |
| 1544 | uint8_t uTagRequirement, |
| 1545 | UsefulBufC Regex); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1546 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1547 | static void |
| 1548 | QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pCtx, |
| 1549 | int64_t nLabel, |
| 1550 | uint8_t uTagRequirement, |
| 1551 | UsefulBufC Regex); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1552 | |
| 1553 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1554 | static void |
| 1555 | QCBOREncode_AddRegex(QCBOREncodeContext *pCtx, |
| 1556 | UsefulBufC Regex); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1557 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1558 | static void |
| 1559 | QCBOREncode_AddRegexToMap(QCBOREncodeContext *pCtx, |
| 1560 | const char *szLabel, |
| 1561 | UsefulBufC Regex); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1562 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1563 | static void |
| 1564 | QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pCtx, |
| 1565 | int64_t nLabel, |
| 1566 | UsefulBufC Regex); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1567 | |
| 1568 | |
| 1569 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1570 | * @brief MIME encoded data to the encoded output. |
| 1571 | * |
| 1572 | * @param[in] pCtx The encoding context to add the MIME data to. |
| 1573 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1574 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1575 | * @param[in] MIMEData Pointer and length of the MIME data. |
| 1576 | * |
| 1577 | * The text content is in MIME format per [RFC 2045] |
| 1578 | * (https://tools.ietf.org/html/rfc2045) including the headers. |
| 1579 | * |
| 1580 | * It is output as CBOR major type 2, a binary string, with tag |
| 1581 | * @ref CBOR_TAG_BINARY_MIME indicating the string is MIME data. This |
| 1582 | * outputs tag 257, not tag 36, as it can carry any type of MIME |
| 1583 | * binary, 7-bit, 8-bit, quoted-printable and base64 where tag 36 |
| 1584 | * cannot. |
| 1585 | * |
| 1586 | * Previous versions of QCBOR, those before spiffy decode, output tag |
| 1587 | * 36. Decoding supports both tag 36 and 257. (if the old behavior |
| 1588 | * with tag 36 is needed, copy the inline functions below and change |
| 1589 | * the tag number). |
| 1590 | * |
| 1591 | * See also QCBORDecode_GetMIMEMessage() and |
| 1592 | * @ref QCBOR_TYPE_BINARY_MIME. |
| 1593 | * |
| 1594 | * This does no translation of line endings. See QCBOREncode_AddText() |
| 1595 | * for a discussion of line endings in CBOR. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1596 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1597 | static void |
| 1598 | QCBOREncode_AddTMIMEData(QCBOREncodeContext *pCtx, |
| 1599 | uint8_t uTagRequirement, |
| 1600 | UsefulBufC MIMEData); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1601 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1602 | static void |
| 1603 | QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pCtx, |
| 1604 | const char *szLabel, |
| 1605 | uint8_t uTagRequirement, |
| 1606 | UsefulBufC MIMEData); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1607 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1608 | static void |
| 1609 | QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pCtx, |
| 1610 | int64_t nLabel, |
| 1611 | uint8_t uTagRequirement, |
| 1612 | UsefulBufC MIMEData); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1613 | |
| 1614 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1615 | static void |
| 1616 | QCBOREncode_AddMIMEData(QCBOREncodeContext *pCtx, |
| 1617 | UsefulBufC MIMEData); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1618 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1619 | static void |
| 1620 | QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pCtx, |
| 1621 | const char *szLabel, |
| 1622 | UsefulBufC MIMEData); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1623 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1624 | static void |
| 1625 | QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pCtx, |
| 1626 | int64_t nLabel, |
| 1627 | UsefulBufC MIMEData); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1628 | |
| 1629 | |
| 1630 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1631 | * @brief Add an RFC 3339 date string |
| 1632 | * |
| 1633 | * @param[in] pCtx The encoding context to add the date to. |
| 1634 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1635 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1636 | * @param[in] szDate Null-terminated string with date to add. |
| 1637 | * |
| 1638 | * The string szDate should be in the form of [RFC 3339] |
| 1639 | * (https://tools.ietf.org/html/rfc3339) as defined by section 3.3 in |
| 1640 | * [RFC 4287] (https://tools.ietf.org/html/rfc4287). This is as |
| 1641 | * described in section 3.4.1 in [RFC 8949] |
| 1642 | * (https://tools.ietf.org/html/rfc8949). |
| 1643 | * |
| 1644 | * Note that this function doesn't validate the format of the date |
| 1645 | * string at all. If you add an incorrect format date string, the |
| 1646 | * generated CBOR will be incorrect and the receiver may not be able |
| 1647 | * to handle it. |
| 1648 | * |
| 1649 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 1650 | * |
| 1651 | * See also QCBOREncode_AddTDayString(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1652 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1653 | static void |
| 1654 | QCBOREncode_AddTDateString(QCBOREncodeContext *pCtx, |
| 1655 | uint8_t uTagRequirement, |
| 1656 | const char *szDate); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1657 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1658 | static void |
| 1659 | QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pCtx, |
| 1660 | const char *szLabel, |
| 1661 | uint8_t uTagRequirement, |
| 1662 | const char *szDate); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1663 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1664 | static void |
| 1665 | QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pCtx, |
| 1666 | int64_t nLabel, |
| 1667 | uint8_t uTagRequirement, |
| 1668 | const char *szDate); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 1669 | |
| 1670 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1671 | static void |
| 1672 | QCBOREncode_AddDateString(QCBOREncodeContext *pCtx, |
| 1673 | const char *szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1674 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1675 | static void |
| 1676 | QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pCtx, |
| 1677 | const char *szLabel, |
| 1678 | const char *szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1679 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1680 | static void |
| 1681 | QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pCtx, |
| 1682 | int64_t nLabel, |
| 1683 | const char *szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1684 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 1685 | |
| 1686 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1687 | * @brief Add a date-only string. |
| 1688 | * |
| 1689 | * @param[in] pCtx The encoding context to add the date to. |
| 1690 | * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or |
| 1691 | * @ref QCBOR_ENCODE_AS_BORROWED. |
| 1692 | * @param[in] szDate Null-terminated string with date to add. |
| 1693 | * |
| 1694 | * This date format is described in |
| 1695 | * [RFC 8943] (https://tools.ietf.org/html/rfc8943), but that mainly |
| 1696 | * references RFC 3339. The string szDate must be in the forrm |
| 1697 | * specified the ABNF for a full-date in |
| 1698 | * [RFC 3339] (https://tools.ietf.org/html/rfc3339). Examples of this |
| 1699 | * are "1985-04-12" and "1937-01-01". The time and the time zone are |
| 1700 | * never included. |
| 1701 | * |
| 1702 | * Note that this function doesn't validate the format of the date |
| 1703 | * string at all. If you add an incorrect format date string, the |
| 1704 | * generated CBOR will be incorrect and the receiver may not be able |
| 1705 | * to handle it. |
| 1706 | * |
| 1707 | * Error handling is the same as QCBOREncode_AddInt64(). |
| 1708 | * |
| 1709 | * See also QCBOREncode_AddTDateString(). |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 1710 | */ |
| 1711 | static void |
| 1712 | QCBOREncode_AddTDaysString(QCBOREncodeContext *pCtx, |
| 1713 | uint8_t uTagRequirement, |
| 1714 | const char *szDate); |
| 1715 | |
| 1716 | static void |
| 1717 | QCBOREncode_AddTDaysStringToMapSZ(QCBOREncodeContext *pCtx, |
| 1718 | const char *szLabel, |
| 1719 | uint8_t uTagRequirement, |
| 1720 | const char *szDate); |
| 1721 | |
| 1722 | static void |
| 1723 | QCBOREncode_AddTDaysStringToMapN(QCBOREncodeContext *pCtx, |
| 1724 | int64_t nLabel, |
| 1725 | uint8_t uTagRequirement, |
| 1726 | const char *szDate); |
| 1727 | |
| 1728 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1729 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1730 | * @brief Add a standard Boolean. |
| 1731 | * |
| 1732 | * @param[in] pCtx The encoding context to add the Boolean to. |
| 1733 | * @param[in] b true or false from @c <stdbool.h>. |
| 1734 | * |
| 1735 | * Adds a Boolean value as CBOR major type 7. |
| 1736 | * |
| 1737 | * Error handling is the same as QCBOREncode_AddInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1738 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1739 | static void |
| 1740 | QCBOREncode_AddBool(QCBOREncodeContext *pCtx, bool b); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1741 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1742 | static void |
| 1743 | QCBOREncode_AddBoolToMap(QCBOREncodeContext *pCtx, const char *szLabel, bool b); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1744 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1745 | static void |
| 1746 | QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, bool b); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1747 | |
| 1748 | |
| 1749 | |
| 1750 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1751 | * @brief Add a NULL to the encoded output. |
| 1752 | * |
| 1753 | * @param[in] pCtx The encoding context to add the NULL to. |
| 1754 | * |
| 1755 | * Adds the NULL value as CBOR major type 7. |
| 1756 | * |
| 1757 | * This NULL doesn't have any special meaning in CBOR such as a |
| 1758 | * terminating value for a string or an empty value. |
| 1759 | * |
| 1760 | * Error handling is the same as QCBOREncode_AddInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1761 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1762 | static void |
| 1763 | QCBOREncode_AddNULL(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1764 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1765 | static void |
| 1766 | QCBOREncode_AddNULLToMap(QCBOREncodeContext *pCtx, const char *szLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1767 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1768 | static void |
| 1769 | QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1770 | |
| 1771 | |
| 1772 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1773 | * @brief Add an "undef" to the encoded output. |
| 1774 | * |
| 1775 | * @param[in] pCtx The encoding context to add the "undef" to. |
| 1776 | * |
| 1777 | * Adds the undef value as CBOR major type 7. |
| 1778 | * |
| 1779 | * Note that this value will not translate to JSON. |
| 1780 | * |
| 1781 | * This Undef doesn't have any special meaning in CBOR such as a |
| 1782 | * terminating value for a string or an empty value. |
| 1783 | * |
| 1784 | * Error handling is the same as QCBOREncode_AddInt64(). |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1785 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1786 | static void |
| 1787 | QCBOREncode_AddUndef(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1788 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1789 | static void |
| 1790 | QCBOREncode_AddUndefToMap(QCBOREncodeContext *pCtx, const char *szLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1791 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1792 | static void |
| 1793 | QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1794 | |
| 1795 | |
| 1796 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1797 | * @brief Indicates that the next items added are in an array. |
| 1798 | * |
| 1799 | * @param[in] pCtx The encoding context to open the array in. |
| 1800 | * |
| 1801 | * Arrays are the basic CBOR aggregate or structure type. Call this |
| 1802 | * function to start or open an array. Then call the various |
| 1803 | * @c QCBOREncode_AddXxx() functions to add the items that go into the |
| 1804 | * array. Then call QCBOREncode_CloseArray() when all items have been |
| 1805 | * added. The data items in the array can be of any type and can be of |
| 1806 | * mixed types. |
| 1807 | * |
| 1808 | * Nesting of arrays and maps is allowed and supported just by calling |
| 1809 | * QCBOREncode_OpenArray() again before calling |
| 1810 | * QCBOREncode_CloseArray(). While CBOR has no limit on nesting, this |
| 1811 | * implementation does in order to keep it smaller and simpler. The |
| 1812 | * limit is @ref QCBOR_MAX_ARRAY_NESTING. This is the max number of |
| 1813 | * times this can be called without calling |
| 1814 | * QCBOREncode_CloseArray(). QCBOREncode_Finish() will return |
| 1815 | * @ref QCBOR_ERR_ARRAY_NESTING_TOO_DEEP when it is called as this |
| 1816 | * function just sets an error state and returns no value when this |
| 1817 | * occurs. |
| 1818 | * |
| 1819 | * If you try to add more than @ref QCBOR_MAX_ITEMS_IN_ARRAY items to |
| 1820 | * a single array or map, @ref QCBOR_ERR_ARRAY_TOO_LONG will be |
| 1821 | * returned when QCBOREncode_Finish() is called. |
| 1822 | * |
| 1823 | * An array itself must have a label if it is being added to a map. |
| 1824 | * Note that array elements do not have labels (but map elements do). |
| 1825 | * |
| 1826 | * An array itself may be tagged by calling QCBOREncode_AddTag() |
| 1827 | * before this call. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1828 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1829 | static void |
| 1830 | QCBOREncode_OpenArray(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1831 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1832 | static void |
| 1833 | QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pCtx, const char *szLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1834 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1835 | static void |
| 1836 | QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1837 | |
| 1838 | |
| 1839 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1840 | * @brief Close an open array. |
| 1841 | * |
| 1842 | * @param[in] pCtx The encoding context to close the array in. |
| 1843 | * |
| 1844 | * The closes an array opened by QCBOREncode_OpenArray(). It reduces |
| 1845 | * nesting level by one. All arrays (and maps) must be closed before |
| 1846 | * calling QCBOREncode_Finish(). |
| 1847 | * |
| 1848 | * When an error occurs as a result of this call, the encoder records |
| 1849 | * the error and enters the error state. The error will be returned |
| 1850 | * when QCBOREncode_Finish() is called. |
| 1851 | * |
| 1852 | * If this has been called more times than QCBOREncode_OpenArray(), then |
| 1853 | * @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when QCBOREncode_Finish() |
| 1854 | * is called. |
| 1855 | * |
| 1856 | * If this is called and it is not an array that is currently open, |
| 1857 | * @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when |
| 1858 | * QCBOREncode_Finish() is called. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1859 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1860 | static void |
| 1861 | QCBOREncode_CloseArray(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1862 | |
| 1863 | |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 1864 | |
| 1865 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1866 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1867 | * @brief Indicates that the next items added are in a map. |
| 1868 | * |
| 1869 | * @param[in] pCtx The encoding context to open the map in. |
| 1870 | * |
| 1871 | * See QCBOREncode_OpenArray() for more information, particularly |
| 1872 | * error handling. |
| 1873 | * |
| 1874 | * CBOR maps are an aggregate type where each item in the map consists |
| 1875 | * of a label and a value. They are similar to JSON objects. |
| 1876 | * |
| 1877 | * The value can be any CBOR type including another map. |
| 1878 | * |
| 1879 | * The label can also be any CBOR type, but in practice they are |
| 1880 | * typically, integers as this gives the most compact output. They |
| 1881 | * might also be text strings which gives readability and translation |
| 1882 | * to JSON. |
| 1883 | * |
| 1884 | * Every @c QCBOREncode_AddXxx() call has one version that ends with |
| 1885 | * @c InMap for adding items to maps with string labels and one that |
| 1886 | * ends with @c InMapN that is for adding with integer labels. |
| 1887 | * |
| 1888 | * RFC 8949 uses the term "key" instead of "label". |
| 1889 | * |
| 1890 | * If you wish to use map labels that are neither integer labels nor |
| 1891 | * text strings, then just call the QCBOREncode_AddXxx() function |
| 1892 | * explicitly to add the label. Then call it again to add the value. |
| 1893 | * |
| 1894 | * See the [RFC 8949] (https://tools.ietf.org/html/rfc8949) for a lot |
| 1895 | * more information on creating maps. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1896 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1897 | static void |
| 1898 | QCBOREncode_OpenMap(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1899 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1900 | static void |
| 1901 | QCBOREncode_OpenMapInMap(QCBOREncodeContext *pCtx, const char *szLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1902 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1903 | static void |
| 1904 | QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1905 | |
| 1906 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1907 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1908 | * @brief Close an open map. |
| 1909 | * |
| 1910 | * @param[in] pCtx The encoding context to close the map in. |
| 1911 | * |
| 1912 | * This closes a map opened by QCBOREncode_OpenMap(). It reduces |
| 1913 | * nesting level by one. |
| 1914 | * |
| 1915 | * When an error occurs as a result of this call, the encoder records |
| 1916 | * the error and enters the error state. The error will be returned |
| 1917 | * when QCBOREncode_Finish() is called. |
| 1918 | * |
| 1919 | * If this has been called more times than QCBOREncode_OpenMap(), then |
| 1920 | * @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when |
| 1921 | * QCBOREncode_Finish() is called. |
| 1922 | * |
| 1923 | * If this is called and it is not a map that is currently open, |
| 1924 | * @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when |
| 1925 | * QCBOREncode_Finish() is called. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1926 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1927 | static void |
| 1928 | QCBOREncode_CloseMap(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 1929 | |
| 1930 | |
| 1931 | /** |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 1932 | * @brief Indicates that the next items added are in an indefinite length array. |
| 1933 | * |
| 1934 | * @param[in] pCtx The encoding context to open the array in. |
| 1935 | * |
| 1936 | * This is the same as QCBOREncode_OpenArray() except the array is |
| 1937 | * indefinite length. |
| 1938 | * |
| 1939 | * This must be closed with QCBOREncode_CloseArrayIndefiniteLength(). |
| 1940 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1941 | static void |
| 1942 | QCBOREncode_OpenArrayIndefiniteLength(QCBOREncodeContext *pCtx); |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 1943 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1944 | static void |
| 1945 | QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pCtx, |
| 1946 | const char *szLabel); |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 1947 | |
| 1948 | static void |
| 1949 | QCBOREncode_OpenArrayIndefiniteLengthInMapN(QCBOREncodeContext *pCtx, |
| 1950 | int64_t nLabel); |
| 1951 | |
| 1952 | |
| 1953 | /** |
| 1954 | * @brief Close an open indefinite length array. |
| 1955 | * |
| 1956 | * @param[in] pCtx The encoding context to close the array in. |
| 1957 | * |
| 1958 | * This is the same as QCBOREncode_CloseArray(), but the open array |
| 1959 | * that is being close must be of indefinite length. |
| 1960 | */ |
| 1961 | static void |
| 1962 | QCBOREncode_CloseArrayIndefiniteLength(QCBOREncodeContext *pCtx); |
| 1963 | |
| 1964 | |
| 1965 | /** |
| 1966 | * @brief Indicates that the next items added are in an indefinite length map. |
| 1967 | * |
| 1968 | * @param[in] pCtx The encoding context to open the map in. |
| 1969 | * |
| 1970 | * This is the same as QCBOREncode_OpenMap() except the array is |
| 1971 | * indefinite length. |
| 1972 | * |
| 1973 | * This must be closed with QCBOREncode_CloseMapIndefiniteLength(). |
| 1974 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1975 | static void |
| 1976 | QCBOREncode_OpenMapIndefiniteLength(QCBOREncodeContext *pCtx); |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 1977 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 1978 | static void |
| 1979 | QCBOREncode_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pCtx, |
| 1980 | const char *szLabel); |
Laurence Lundblade | 85a18ca | 2023-11-27 21:25:10 -0700 | [diff] [blame] | 1981 | |
| 1982 | static void |
| 1983 | QCBOREncode_OpenMapIndefiniteLengthInMapN(QCBOREncodeContext *pCtx, |
| 1984 | int64_t nLabel); |
| 1985 | |
| 1986 | |
| 1987 | /** |
| 1988 | * @brief Close an open indefinite length map. |
| 1989 | * |
| 1990 | * @param[in] pCtx The encoding context to close the map in. |
| 1991 | * |
| 1992 | * This is the same as QCBOREncode_CloseMap(), but the open map that |
| 1993 | * is being close must be of indefinite length. |
| 1994 | */ |
| 1995 | static void |
| 1996 | QCBOREncode_CloseMapIndefiniteLength(QCBOREncodeContext *pCtx); |
| 1997 | |
| 1998 | |
| 1999 | |
| 2000 | |
| 2001 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2002 | * @brief Indicate start of encoded CBOR to be wrapped in a bstr. |
| 2003 | * |
| 2004 | * @param[in] pCtx The encoding context to open the bstr-wrapped CBOR in. |
| 2005 | * |
| 2006 | * All added encoded items between this call and a call to |
| 2007 | * QCBOREncode_CloseBstrWrap2() will be wrapped in a bstr. They will |
| 2008 | * appear in the final output as a byte string. That byte string will |
| 2009 | * contain encoded CBOR. This increases nesting level by one. |
| 2010 | * |
| 2011 | * The typical use case is for encoded CBOR that is to be |
| 2012 | * cryptographically hashed, as part of a [RFC 8152, COSE] |
| 2013 | * (https://tools.ietf.org/html/rfc8152) implementation. The wrapping |
| 2014 | * byte string is taken as input by the hash function (which is why it |
| 2015 | * is returned by QCBOREncode_CloseBstrWrap2()). It is also easy to |
| 2016 | * recover on decoding with standard CBOR decoders. |
| 2017 | * |
| 2018 | * Using QCBOREncode_BstrWrap() and QCBOREncode_CloseBstrWrap2() |
| 2019 | * avoids having to encode the items first in one buffer (e.g., the |
| 2020 | * COSE payload) and then add that buffer as a bstr to another |
| 2021 | * encoding (e.g. the COSE to-be-signed bytes, the @c Sig_structure) |
| 2022 | * potentially halving the memory needed. |
| 2023 | * |
| 2024 | * CBOR by nature must be decoded item by item in order from the |
| 2025 | * start. By wrapping some CBOR in a byte string, the decoding of |
| 2026 | * that wrapped CBOR can be skipped. This is another use of wrapping, |
| 2027 | * perhaps because the CBOR is large and deeply nested. Perhaps APIs |
| 2028 | * for handling one defined CBOR message that is being embedded in |
| 2029 | * another only take input as a byte string. Perhaps the desire is to |
| 2030 | * 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] | 2031 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2032 | static void |
| 2033 | QCBOREncode_BstrWrap(QCBOREncodeContext *pCtx); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2034 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2035 | static void |
| 2036 | QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pCtx, const char *szLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2037 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2038 | static void |
| 2039 | QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2040 | |
| 2041 | |
| 2042 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2043 | * @brief Close a wrapping bstr. |
| 2044 | * |
| 2045 | * @param[in] pCtx The encoding context to close of bstr wrapping in. |
| 2046 | * @param[in] bIncludeCBORHead Include the encoded CBOR head of the bstr |
| 2047 | * as well as the bytes in @c pWrappedCBOR. |
| 2048 | * @param[out] pWrappedCBOR A @ref UsefulBufC containing wrapped bytes. |
| 2049 | * |
| 2050 | * The closes a wrapping bstr opened by QCBOREncode_BstrWrap(). It reduces |
| 2051 | * nesting level by one. |
| 2052 | * |
| 2053 | * A pointer and length of the enclosed encoded CBOR is returned in @c |
| 2054 | * *pWrappedCBOR if it is not @c NULL. The main purpose of this is so |
| 2055 | * this data can be hashed (e.g., with SHA-256) as part of a [RFC |
| 2056 | * 8152, COSE] (https://tools.ietf.org/html/rfc8152) |
| 2057 | * implementation. **WARNING**, this pointer and length should be used |
| 2058 | * right away before any other calls to @c QCBOREncode_CloseXxx() as |
| 2059 | * they will move data around and the pointer and length will no |
| 2060 | * longer be to the correct encoded CBOR. |
| 2061 | * |
| 2062 | * When an error occurs as a result of this call, the encoder records |
| 2063 | * the error and enters the error state. The error will be returned |
| 2064 | * when QCBOREncode_Finish() is called. |
| 2065 | * |
| 2066 | * If this has been called more times than QCBOREncode_BstrWrap(), |
| 2067 | * then @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when |
| 2068 | * QCBOREncode_Finish() is called. |
| 2069 | * |
| 2070 | * If this is called and it is not a wrapping bstr that is currently |
| 2071 | * open, @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when |
| 2072 | * QCBOREncode_Finish() is called. |
| 2073 | * |
| 2074 | * QCBOREncode_CloseBstrWrap() is a deprecated version of this function |
| 2075 | * that is equivalent to the call with @c bIncludeCBORHead @c true. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2076 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2077 | void |
| 2078 | QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *pCtx, bool bIncludeCBORHead, UsefulBufC *pWrappedCBOR); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2079 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2080 | static void |
| 2081 | QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pCtx, UsefulBufC *pWrappedCBOR); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2082 | |
| 2083 | |
| 2084 | /** |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 2085 | * @brief Cancel byte string wrapping. |
| 2086 | * |
| 2087 | * @param[in] pCtx The encoding context. |
| 2088 | * |
| 2089 | * This cancels QCBOREncode_BstrWrap() making tghe encoding as if it |
| 2090 | * were never called. |
| 2091 | * |
| 2092 | * WARNING: This does not work on QCBOREncode_BstrWrapInMap() |
| 2093 | * or QCBOREncode_BstrWrapInMapN() and there is no error detection |
| 2094 | * of an attempt at their use. |
| 2095 | * |
| 2096 | * This only works if nothing has been added into the wrapped byte |
| 2097 | * string. If something has been added, this sets the error |
| 2098 | * @ref QCBOR_ERR_CANNOT_CANCEL. |
| 2099 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2100 | void |
| 2101 | QCBOREncode_CancelBstrWrap(QCBOREncodeContext *pCtx); |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 2102 | |
| 2103 | |
| 2104 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2105 | * @brief Add some already-encoded CBOR bytes. |
| 2106 | * |
| 2107 | * @param[in] pCtx The encoding context to add the already-encode CBOR to. |
| 2108 | * @param[in] Encoded The already-encoded CBOR to add to the context. |
| 2109 | * |
| 2110 | * The encoded CBOR being added must be fully conforming CBOR. It must |
| 2111 | * be complete with no arrays or maps that are incomplete. While this |
| 2112 | * encoder doesn't ever produce indefinite lengths, it is OK for the |
| 2113 | * raw CBOR added here to have indefinite lengths. |
| 2114 | * |
| 2115 | * The raw CBOR added here is not checked in anyway. If it is not |
| 2116 | * conforming or has open arrays or such, the final encoded CBOR |
| 2117 | * will probably be wrong or not what was intended. |
| 2118 | * |
| 2119 | * If the encoded CBOR being added here contains multiple items, they |
| 2120 | * must be enclosed in a map or array. At the top level the raw |
| 2121 | * CBOR must be a single data item. |
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_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2125 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2126 | static void |
| 2127 | QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2128 | |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2129 | static void |
| 2130 | QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2131 | |
| 2132 | |
| 2133 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2134 | * @brief Get the encoded result. |
| 2135 | * |
| 2136 | * @param[in] pCtx The context to finish encoding with. |
| 2137 | * @param[out] pEncodedCBOR Structure in which the pointer and length of |
| 2138 | * the encoded CBOR is returned. |
| 2139 | * |
| 2140 | * @retval QCBOR_SUCCESS Encoded CBOR is returned. |
| 2141 | * |
| 2142 | * @retval QCBOR_ERR_TOO_MANY_CLOSES Nesting error |
| 2143 | * |
| 2144 | * @retval QCBOR_ERR_CLOSE_MISMATCH Nesting error |
| 2145 | * |
| 2146 | * @retval QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN Nesting error |
| 2147 | * |
| 2148 | * @retval QCBOR_ERR_BUFFER_TOO_LARGE Encoded output buffer size |
| 2149 | * |
| 2150 | * @retval QCBOR_ERR_BUFFER_TOO_SMALL Encoded output buffer size |
| 2151 | * |
| 2152 | * @retval QCBOR_ERR_ARRAY_NESTING_TOO_DEEP Implementation limit |
| 2153 | * |
| 2154 | * @retval QCBOR_ERR_ARRAY_TOO_LONG Implementation limit |
| 2155 | * |
| 2156 | * On success, the pointer and length of the encoded CBOR are returned |
| 2157 | * in @c *pEncodedCBOR. The pointer is the same pointer that was passed |
| 2158 | * in to QCBOREncode_Init(). Note that it is not const when passed to |
| 2159 | * QCBOREncode_Init(), but it is const when returned here. The length |
| 2160 | * will be smaller than or equal to the length passed in when |
| 2161 | * QCBOREncode_Init() as this is the length of the actual result, not |
| 2162 | * the size of the buffer it was written to. |
| 2163 | * |
| 2164 | * If a @c NULL was passed for @c Storage.ptr when QCBOREncode_Init() |
| 2165 | * was called, @c NULL will be returned here, but the length will be |
| 2166 | * that of the CBOR that would have been encoded. |
| 2167 | * |
| 2168 | * Encoding errors primarily manifest here as most other encoding function |
| 2169 | * do no return an error. They just set the error state in the encode |
| 2170 | * context after which no encoding function does anything. |
| 2171 | * |
| 2172 | * Three types of errors manifest here. The first type are nesting |
| 2173 | * errors where the number of @c QCBOREncode_OpenXxx() calls do not |
| 2174 | * match the number @c QCBOREncode_CloseXxx() calls. The solution is to |
| 2175 | * fix the calling code. |
| 2176 | * |
| 2177 | * The second type of error is because the buffer given is either too |
| 2178 | * small or too large. The remedy is to give a correctly sized buffer. |
| 2179 | * |
| 2180 | * The third type are due to limits in this implementation. |
| 2181 | * @ref QCBOR_ERR_ARRAY_NESTING_TOO_DEEP can be worked around by |
| 2182 | * encoding the CBOR in two (or more) phases and adding the CBOR from |
| 2183 | * the first phase to the second with @c QCBOREncode_AddEncoded(). |
| 2184 | * |
| 2185 | * If an error is returned, the buffer may have partially encoded |
| 2186 | * incorrect CBOR in it and it should not be used. Likewise, the length |
| 2187 | * may be incorrect and should not be used. |
| 2188 | * |
| 2189 | * Note that the error could have occurred in one of the many |
| 2190 | * @c QCBOREncode_AddXxx() calls long before QCBOREncode_Finish() was |
| 2191 | * called. This error handling reduces the CBOR implementation size |
| 2192 | * but makes debugging harder. |
| 2193 | * |
| 2194 | * This may be called multiple times. It will always return the |
| 2195 | * same. It can also be interleaved with calls to |
| 2196 | * QCBOREncode_FinishGetSize(). |
| 2197 | * |
| 2198 | * QCBOREncode_GetErrorState() can be called to get the current |
| 2199 | * error state in order to abort encoding early as an optimization, but |
| 2200 | * calling it is is never required. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2201 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2202 | QCBORError |
| 2203 | QCBOREncode_Finish(QCBOREncodeContext *pCtx, UsefulBufC *pEncodedCBOR); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2204 | |
| 2205 | |
| 2206 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2207 | * @brief Get the encoded CBOR and error status. |
| 2208 | * |
| 2209 | * @param[in] pCtx The context to finish encoding with. |
| 2210 | * @param[out] uEncodedLen The length of the encoded or potentially |
| 2211 | * encoded CBOR in bytes. |
| 2212 | * |
| 2213 | * @return The same errors as QCBOREncode_Finish(). |
| 2214 | * |
| 2215 | * This functions the same as QCBOREncode_Finish(), but only returns the |
| 2216 | * size of the encoded output. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2217 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2218 | QCBORError |
| 2219 | QCBOREncode_FinishGetSize(QCBOREncodeContext *pCtx, size_t *uEncodedLen); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2220 | |
| 2221 | |
| 2222 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2223 | * @brief Indicate whether output buffer is NULL or not. |
| 2224 | * |
| 2225 | * @param[in] pCtx The encoding context. |
| 2226 | * |
| 2227 | * @return 1 if the output buffer is @c NULL. |
| 2228 | * |
| 2229 | * Sometimes a @c NULL input buffer is given to QCBOREncode_Init() so |
| 2230 | * that the size of the generated CBOR can be calculated without |
| 2231 | * allocating a buffer for it. This returns 1 when the output buffer |
| 2232 | * is @c NULL and 0 when it is not. |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2233 | */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2234 | static int |
| 2235 | QCBOREncode_IsBufferNULL(QCBOREncodeContext *pCtx); |
| 2236 | |
| 2237 | |
| 2238 | /** |
| 2239 | * @brief Get the encoding error state. |
| 2240 | * |
| 2241 | * @param[in] pCtx The encoding context. |
| 2242 | * |
| 2243 | * @return One of @ref QCBORError. See return values from |
| 2244 | * QCBOREncode_Finish() |
| 2245 | * |
| 2246 | * Normally encoding errors need only be handled at the end of |
| 2247 | * encoding when QCBOREncode_Finish() is called. This can be called to |
| 2248 | * get the error result before finish should there be a need to halt |
| 2249 | * encoding before QCBOREncode_Finish() is called. |
| 2250 | */ |
| 2251 | static QCBORError |
| 2252 | QCBOREncode_GetErrorState(QCBOREncodeContext *pCtx); |
| 2253 | |
| 2254 | |
| 2255 | /** |
| 2256 | * Encode the "head" of a CBOR data item. |
| 2257 | * |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2258 | * @param Buffer Buffer to output the encoded head to; must be |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2259 | * @ref QCBOR_HEAD_BUFFER_SIZE bytes in size. |
| 2260 | * @param uMajorType One of CBOR_MAJOR_TYPE_XX. |
| 2261 | * @param uMinLen The minimum number of bytes to encode uNumber. Almost |
| 2262 | * always this is 0 to use preferred |
| 2263 | * serialization. If this is 4, then even the |
| 2264 | * values 0xffff and smaller will be encoded in 4 |
| 2265 | * bytes. This is used primarily when encoding a |
| 2266 | * float or double put into uNumber as the leading |
| 2267 | * zero bytes for them must be encoded. |
| 2268 | * @param uNumber The numeric argument part of the CBOR head. |
| 2269 | * @return Pointer and length of the encoded head or |
| 2270 | * @ref NULLUsefulBufC if the output buffer is too small. |
| 2271 | * |
| 2272 | * Callers do not to need to call this for normal CBOR encoding. Note |
| 2273 | * that it doesn't even take a @ref QCBOREncodeContext argument. |
| 2274 | * |
| 2275 | * This encodes the major type and argument part of a data item. The |
| 2276 | * argument is an integer that is usually either the value or the length |
| 2277 | * of the data item. |
| 2278 | * |
| 2279 | * This is exposed in the public interface to allow hashing of some CBOR |
| 2280 | * data types, bstr in particular, a chunk at a time so the full CBOR |
| 2281 | * doesn't have to be encoded in a contiguous buffer. |
| 2282 | * |
| 2283 | * For example, if you have a 100,000 byte binary blob in a buffer that |
| 2284 | * needs to be a bstr encoded and then hashed. You could allocate a |
| 2285 | * 100,010 byte buffer and encode it normally. Alternatively, you can |
| 2286 | * encode the head in a 10 byte buffer with this function, hash that and |
| 2287 | * then hash the 100,000 bytes using the same hash context. |
| 2288 | * |
| 2289 | * See also QCBOREncode_AddBytesLenOnly(); |
| 2290 | */ |
| 2291 | UsefulBufC |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2292 | QCBOREncode_EncodeHead(UsefulBuf Buffer, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2293 | uint8_t uMajorType, |
| 2294 | uint8_t uMinLen, |
| 2295 | uint64_t uNumber); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2296 | |
| 2297 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2298 | |
| 2299 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2300 | /* ========================================================================= |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2301 | BEGINNING OF PRIVATE IMPLEMENTATION |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2302 | ========================================================================= */ |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2303 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2304 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2305 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2306 | QCBOREncode_Private_AddBuffer(QCBOREncodeContext *pCtx, |
| 2307 | uint8_t uMajorType, |
| 2308 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2309 | |
| 2310 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2311 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2312 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2313 | QCBOREncode_Private_OpenMapOrArray(QCBOREncodeContext *pCtx, |
| 2314 | uint8_t uMajorType); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2315 | |
| 2316 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2317 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2318 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2319 | QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx, |
| 2320 | uint8_t uMajorType); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2321 | |
| 2322 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2323 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2324 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2325 | QCBOREncode_Private_CloseMapOrArray(QCBOREncodeContext *pCtx, |
| 2326 | uint8_t uMajorType); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2327 | |
| 2328 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2329 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2330 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2331 | QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx, |
| 2332 | uint8_t uMajorType); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2333 | |
| 2334 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2335 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2336 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2337 | QCBOREncode_Private_AddType7(QCBOREncodeContext *pCtx, |
| 2338 | uint8_t uMinLen, |
| 2339 | uint64_t uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2340 | |
| 2341 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2342 | /* Semi-private funcion used by public inline functions. See qcbor_encode.c */ |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2343 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2344 | QCBOREncode_Private_AddExpMantissa(QCBOREncodeContext *pCtx, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2345 | uint64_t uTag, |
| 2346 | UsefulBufC BigNumMantissa, |
| 2347 | bool bBigNumIsNegative, |
| 2348 | int64_t nMantissa, |
| 2349 | int64_t nExponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2350 | |
| 2351 | /** |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2352 | * @brief Semi-private method to add only the type and length of a byte string. |
| 2353 | * |
| 2354 | * @param[in] pCtx The context to initialize. |
| 2355 | * @param[in] Bytes Pointer and length of the input data. |
| 2356 | * |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2357 | * This will be removed in QCBOR 2.0. It was never a public function. |
| 2358 | * |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2359 | * This is the same as QCBOREncode_AddBytes() except it only adds the |
| 2360 | * CBOR encoding for the type and the length. It doesn't actually add |
| 2361 | * the bytes. You can't actually produce correct CBOR with this and |
| 2362 | * the rest of this API. It is only used for a special case where the |
| 2363 | * valid CBOR is created manually by putting this type and length in |
| 2364 | * and then adding the actual bytes. In particular, when only a hash |
| 2365 | * of the encoded CBOR is needed, where the type and header are hashed |
| 2366 | * separately and then the bytes is hashed. This makes it possible to |
| 2367 | * implement COSE Sign1 with only one copy of the payload in the |
| 2368 | * output buffer, rather than two, roughly cutting memory use in half. |
| 2369 | * |
| 2370 | * This is only used for this odd case, but this is a supported |
| 2371 | * tested function. |
| 2372 | * |
| 2373 | * See also QCBOREncode_EncodeHead(). |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2374 | */ |
| 2375 | static void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2376 | QCBOREncode_AddBytesLenOnly(QCBOREncodeContext *pCtx, |
| 2377 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2378 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2379 | static void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2380 | QCBOREncode_AddBytesLenOnlyToMap(QCBOREncodeContext *pCtx, |
| 2381 | const char *szLabel, |
| 2382 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2383 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2384 | static void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2385 | QCBOREncode_AddBytesLenOnlyToMapN(QCBOREncodeContext *pCtx, |
| 2386 | int64_t nLabel, |
| 2387 | UsefulBufC Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2388 | |
| 2389 | |
| 2390 | |
| 2391 | |
| 2392 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2393 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2394 | QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pMe, |
| 2395 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2396 | const int64_t uNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2397 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2398 | /* Use _AddBuffer() because _AddSZString() is defined below, not above */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2399 | QCBOREncode_Private_AddBuffer(pMe, |
| 2400 | CBOR_MAJOR_TYPE_TEXT_STRING, |
| 2401 | UsefulBuf_FromSZ(szLabel)); |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2402 | QCBOREncode_AddInt64(pMe, uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2403 | } |
| 2404 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2405 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2406 | QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2407 | const int64_t nLabel, |
| 2408 | const int64_t uNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2409 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2410 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2411 | QCBOREncode_AddInt64(pMe, uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2415 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2416 | QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pMe, |
| 2417 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2418 | const uint64_t uNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2419 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2420 | /* Use _AddBuffer() because _AddSZString() is defined below, not above */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2421 | QCBOREncode_Private_AddBuffer(pMe, |
| 2422 | CBOR_MAJOR_TYPE_TEXT_STRING, |
| 2423 | UsefulBuf_FromSZ(szLabel)); |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2424 | QCBOREncode_AddUInt64(pMe, uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2425 | } |
| 2426 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2427 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2428 | QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2429 | const int64_t nLabel, |
| 2430 | const uint64_t uNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2431 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2432 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2433 | QCBOREncode_AddUInt64(pMe, uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2434 | } |
| 2435 | |
| 2436 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2437 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2438 | QCBOREncode_AddText(QCBOREncodeContext *pMe, const UsefulBufC Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2439 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2440 | QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_TYPE_TEXT_STRING, Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2441 | } |
| 2442 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2443 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2444 | QCBOREncode_AddTextToMap(QCBOREncodeContext *pMe, |
| 2445 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2446 | const UsefulBufC Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2447 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2448 | QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szLabel)); |
| 2449 | QCBOREncode_AddText(pMe, Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2450 | } |
| 2451 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2452 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2453 | QCBOREncode_AddTextToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2454 | const int64_t nLabel, |
| 2455 | const UsefulBufC Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2456 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2457 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2458 | QCBOREncode_AddText(pMe, Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2462 | inline static void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2463 | QCBOREncode_AddSZString(QCBOREncodeContext *pMe, const char *szString) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2464 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2465 | QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szString)); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2466 | } |
| 2467 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2468 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2469 | QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pMe, |
| 2470 | const char *szLabel, |
| 2471 | const char *szString) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2472 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2473 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2474 | QCBOREncode_AddSZString(pMe, szString); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2475 | } |
| 2476 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2477 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2478 | QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2479 | const int64_t nLabel, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2480 | const char *szString) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2481 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2482 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2483 | QCBOREncode_AddSZString(pMe, szString); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2487 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2488 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2489 | QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pMe, |
| 2490 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2491 | const double dNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2492 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2493 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2494 | QCBOREncode_AddDouble(pMe, dNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2495 | } |
| 2496 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2497 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2498 | QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2499 | const int64_t nLabel, |
| 2500 | const double dNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2501 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2502 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2503 | QCBOREncode_AddDouble(pMe, dNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2504 | } |
| 2505 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2506 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2507 | QCBOREncode_AddFloatToMap(QCBOREncodeContext *pMe, |
| 2508 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2509 | const float dNum) |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 2510 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2511 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2512 | QCBOREncode_AddFloat(pMe, dNum); |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 2513 | } |
| 2514 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2515 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2516 | QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pMe, |
| 2517 | const int64_t nLabel, |
| 2518 | const float fNum) |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 2519 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2520 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2521 | QCBOREncode_AddFloat(pMe, fNum); |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 2522 | } |
| 2523 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2524 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2525 | QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pMe, |
| 2526 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2527 | const double dNum) |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2528 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2529 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2530 | QCBOREncode_AddDoubleNoPreferred(pMe, dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2531 | } |
| 2532 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2533 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2534 | QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2535 | const int64_t nLabel, |
| 2536 | const double dNum) |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2537 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2538 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2539 | QCBOREncode_AddDoubleNoPreferred(pMe, dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2540 | } |
| 2541 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2542 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2543 | QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pMe, |
| 2544 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2545 | const float dNum) |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2546 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2547 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2548 | QCBOREncode_AddFloatNoPreferred(pMe, dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2549 | } |
| 2550 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2551 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2552 | QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2553 | const int64_t nLabel, |
| 2554 | const float dNum) |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2555 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 2556 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2557 | QCBOREncode_AddFloatNoPreferred(pMe, dNum); |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2558 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2559 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 2560 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2561 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2562 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2563 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2564 | QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pMe, |
| 2565 | const uint8_t uTag, |
| 2566 | const int64_t nDate) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2567 | { |
| 2568 | if(uTag == QCBOR_ENCODE_AS_TAG) { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2569 | QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_EPOCH); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2570 | } |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2571 | QCBOREncode_AddInt64(pMe, nDate); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2572 | } |
| 2573 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2574 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2575 | QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pMe, |
| 2576 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2577 | const uint8_t uTag, |
| 2578 | const int64_t nDate) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2579 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2580 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2581 | QCBOREncode_AddTDateEpoch(pMe, uTag, nDate); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2582 | } |
| 2583 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2584 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2585 | QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2586 | const int64_t nLabel, |
| 2587 | const uint8_t uTag, |
| 2588 | const int64_t nDate) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2589 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2590 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2591 | QCBOREncode_AddTDateEpoch(pMe, uTag, nDate); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2592 | } |
| 2593 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2594 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2595 | QCBOREncode_AddDateEpoch(QCBOREncodeContext *pMe, |
| 2596 | const int64_t nDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2597 | { |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2598 | QCBOREncode_AddTDateEpoch(pMe, QCBOR_ENCODE_AS_TAG, nDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2599 | } |
| 2600 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2601 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2602 | QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pMe, |
| 2603 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2604 | const int64_t nDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2605 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2606 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2607 | QCBOREncode_AddDateEpoch(pMe, nDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2608 | } |
| 2609 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2610 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2611 | QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2612 | const int64_t nLabel, |
| 2613 | const int64_t nDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2614 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2615 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2616 | QCBOREncode_AddDateEpoch(pMe, nDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2620 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2621 | QCBOREncode_AddTDaysEpoch(QCBOREncodeContext *pMe, |
| 2622 | const uint8_t uTag, |
| 2623 | const int64_t nDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2624 | { |
| 2625 | if(uTag == QCBOR_ENCODE_AS_TAG) { |
| 2626 | QCBOREncode_AddTag(pMe, CBOR_TAG_DAYS_EPOCH); |
| 2627 | } |
| 2628 | QCBOREncode_AddInt64(pMe, nDays); |
| 2629 | } |
| 2630 | |
| 2631 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2632 | QCBOREncode_AddTDaysEpochToMapSZ(QCBOREncodeContext *pMe, |
| 2633 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2634 | const uint8_t uTag, |
| 2635 | const int64_t nDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2636 | { |
| 2637 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2638 | QCBOREncode_AddTDaysEpoch(pMe, uTag, nDays); |
| 2639 | } |
| 2640 | |
| 2641 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2642 | QCBOREncode_AddTDaysEpochToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2643 | const int64_t nLabel, |
| 2644 | const uint8_t uTag, |
| 2645 | const int64_t nDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2646 | { |
| 2647 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2648 | QCBOREncode_AddTDaysEpoch(pMe, uTag, nDays); |
| 2649 | } |
| 2650 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2651 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2652 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2653 | QCBOREncode_AddBytes(QCBOREncodeContext *pMe, |
| 2654 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2655 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2656 | QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2657 | } |
| 2658 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2659 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2660 | QCBOREncode_AddBytesToMap(QCBOREncodeContext *pMe, |
| 2661 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2662 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2663 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2664 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2665 | QCBOREncode_AddBytes(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2666 | } |
| 2667 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2668 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2669 | QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2670 | const int64_t nLabel, |
| 2671 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2672 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2673 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2674 | QCBOREncode_AddBytes(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2675 | } |
| 2676 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2677 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2678 | QCBOREncode_OpenBytesInMapSZ(QCBOREncodeContext *pMe, |
| 2679 | const char *szLabel, |
| 2680 | UsefulBuf *pPlace) |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 2681 | { |
| 2682 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2683 | QCBOREncode_OpenBytes(pMe, pPlace); |
| 2684 | } |
| 2685 | |
| 2686 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2687 | QCBOREncode_OpenBytesInMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2688 | const int64_t nLabel, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2689 | UsefulBuf *pPlace) |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 2690 | { |
| 2691 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2692 | QCBOREncode_OpenBytes(pMe, pPlace); |
| 2693 | } |
| 2694 | |
| 2695 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2696 | QCBOREncode_AddBytesLenOnly(QCBOREncodeContext *pMe, const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2697 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2698 | QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2699 | } |
| 2700 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2701 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2702 | QCBOREncode_AddBytesLenOnlyToMap(QCBOREncodeContext *pMe, |
| 2703 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2704 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2705 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2706 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2707 | QCBOREncode_AddBytesLenOnly(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2708 | } |
| 2709 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2710 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2711 | QCBOREncode_AddBytesLenOnlyToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2712 | const int64_t nLabel, |
| 2713 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2714 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2715 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2716 | QCBOREncode_AddBytesLenOnly(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2717 | } |
| 2718 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2719 | |
| 2720 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2721 | QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2722 | const uint8_t uTagRequirement, |
| 2723 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2724 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2725 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 2726 | QCBOREncode_AddTag(pMe, CBOR_TAG_BIN_UUID); |
| 2727 | } |
| 2728 | QCBOREncode_AddBytes(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2729 | } |
| 2730 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2731 | static inline void |
| 2732 | QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pMe, |
| 2733 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2734 | const uint8_t uTagRequirement, |
| 2735 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2736 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2737 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2738 | QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2739 | } |
| 2740 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2741 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2742 | QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2743 | const int64_t nLabel, |
| 2744 | const uint8_t uTagRequirement, |
| 2745 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2746 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2747 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2748 | QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2749 | } |
| 2750 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2751 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2752 | QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pMe, const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2753 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2754 | QCBOREncode_AddTBinaryUUID(pMe, QCBOR_ENCODE_AS_TAG, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2755 | } |
| 2756 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2757 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2758 | QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pMe, |
| 2759 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2760 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2761 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2762 | QCBOREncode_AddTBinaryUUIDToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2763 | } |
| 2764 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2765 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2766 | QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2767 | const int64_t nLabel, |
| 2768 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2769 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2770 | QCBOREncode_AddTBinaryUUIDToMapN(pMe, |
| 2771 | nLabel, |
| 2772 | QCBOR_ENCODE_AS_TAG, |
| 2773 | Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2777 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2778 | QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2779 | const uint8_t uTagRequirement, |
| 2780 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2781 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2782 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 2783 | QCBOREncode_AddTag(pMe, CBOR_TAG_POS_BIGNUM); |
| 2784 | } |
| 2785 | QCBOREncode_AddBytes(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2786 | } |
| 2787 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2788 | static inline void |
| 2789 | QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pMe, |
| 2790 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2791 | const uint8_t uTagRequirement, |
| 2792 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2793 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2794 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2795 | QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2796 | } |
| 2797 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2798 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2799 | QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2800 | const int64_t nLabel, |
| 2801 | const uint8_t uTagRequirement, |
| 2802 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2803 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2804 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2805 | QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2806 | } |
| 2807 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2808 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2809 | QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pMe, const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2810 | { |
| 2811 | QCBOREncode_AddTPositiveBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes); |
| 2812 | } |
| 2813 | |
| 2814 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2815 | QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pMe, |
| 2816 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2817 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2818 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2819 | QCBOREncode_AddTPositiveBignumToMapSZ(pMe, |
| 2820 | szLabel, |
| 2821 | QCBOR_ENCODE_AS_TAG, |
| 2822 | Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2826 | QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2827 | const int64_t nLabel, |
| 2828 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2829 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2830 | QCBOREncode_AddTPositiveBignumToMapN(pMe, |
| 2831 | nLabel, |
| 2832 | QCBOR_ENCODE_AS_TAG, |
| 2833 | Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
| 2836 | |
| 2837 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2838 | QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2839 | const uint8_t uTagRequirement, |
| 2840 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2841 | { |
| 2842 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 2843 | QCBOREncode_AddTag(pMe, CBOR_TAG_NEG_BIGNUM); |
| 2844 | } |
| 2845 | QCBOREncode_AddBytes(pMe, Bytes); |
| 2846 | } |
| 2847 | |
| 2848 | static inline void |
| 2849 | QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pMe, |
| 2850 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2851 | const uint8_t uTagRequirement, |
| 2852 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2853 | { |
| 2854 | QCBOREncode_AddSZString(pMe, szLabel); |
| 2855 | QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes); |
| 2856 | } |
| 2857 | |
| 2858 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2859 | QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2860 | const int64_t nLabel, |
| 2861 | const uint8_t uTagRequirement, |
| 2862 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2863 | { |
| 2864 | QCBOREncode_AddInt64(pMe, nLabel); |
| 2865 | QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes); |
| 2866 | } |
| 2867 | |
| 2868 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2869 | QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pMe, const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2870 | { |
| 2871 | QCBOREncode_AddTNegativeBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes); |
| 2872 | } |
| 2873 | |
| 2874 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2875 | QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pMe, |
| 2876 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2877 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2878 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2879 | QCBOREncode_AddTNegativeBignumToMapSZ(pMe, |
| 2880 | szLabel, |
| 2881 | QCBOR_ENCODE_AS_TAG, |
| 2882 | Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2886 | QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2887 | const int64_t nLabel, |
| 2888 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2889 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2890 | QCBOREncode_AddTNegativeBignumToMapN(pMe, |
| 2891 | nLabel, |
| 2892 | QCBOR_ENCODE_AS_TAG, |
| 2893 | Bytes); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2897 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2898 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2899 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2900 | static inline void |
| 2901 | QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2902 | const uint8_t uTagRequirement, |
| 2903 | const int64_t nMantissa, |
| 2904 | const int64_t nBase10Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2905 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2906 | uint64_t uTag; |
| 2907 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 2908 | uTag = CBOR_TAG_DECIMAL_FRACTION; |
| 2909 | } else { |
| 2910 | uTag = CBOR_TAG_INVALID64; |
| 2911 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2912 | QCBOREncode_Private_AddExpMantissa(pMe, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2913 | uTag, |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2914 | NULLUsefulBufC, |
| 2915 | false, |
| 2916 | nMantissa, |
| 2917 | nBase10Exponent); |
| 2918 | } |
| 2919 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2920 | static inline void |
| 2921 | QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pMe, |
| 2922 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2923 | const uint8_t uTagRequirement, |
| 2924 | const int64_t nMantissa, |
| 2925 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2926 | { |
| 2927 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2928 | QCBOREncode_AddTDecimalFraction(pMe, |
| 2929 | uTagRequirement, |
| 2930 | nMantissa, |
| 2931 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2932 | } |
| 2933 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2934 | static inline void |
| 2935 | QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2936 | const int64_t nLabel, |
| 2937 | const uint8_t uTagRequirement, |
| 2938 | const int64_t nMantissa, |
| 2939 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2940 | { |
| 2941 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2942 | QCBOREncode_AddTDecimalFraction(pMe, |
| 2943 | uTagRequirement, |
| 2944 | nMantissa, |
| 2945 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2946 | } |
| 2947 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2948 | static inline void |
| 2949 | QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2950 | const int64_t nMantissa, |
| 2951 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2952 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2953 | QCBOREncode_AddTDecimalFraction(pMe, |
| 2954 | QCBOR_ENCODE_AS_TAG, |
| 2955 | nMantissa, |
| 2956 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2957 | } |
| 2958 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2959 | static inline void |
| 2960 | QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pMe, |
| 2961 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2962 | const int64_t nMantissa, |
| 2963 | const int64_t nBase10Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2964 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2965 | QCBOREncode_AddTDecimalFractionToMapSZ(pMe, |
| 2966 | szLabel, |
| 2967 | QCBOR_ENCODE_AS_TAG, |
| 2968 | nMantissa, |
| 2969 | nBase10Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2970 | } |
| 2971 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2972 | static inline void |
| 2973 | QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2974 | const int64_t nLabel, |
| 2975 | const int64_t nMantissa, |
| 2976 | const int64_t nBase10Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2977 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 2978 | QCBOREncode_AddTDecimalFractionToMapN(pMe, |
| 2979 | nLabel, |
| 2980 | QCBOR_ENCODE_AS_TAG, |
| 2981 | nMantissa, |
| 2982 | nBase10Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2983 | } |
| 2984 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2985 | |
| 2986 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 2987 | static inline void |
| 2988 | QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2989 | const uint8_t uTagRequirement, |
| 2990 | const UsefulBufC Mantissa, |
| 2991 | const bool bIsNegative, |
| 2992 | const int64_t nBase10Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 2993 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 2994 | uint64_t uTag; |
| 2995 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 2996 | uTag = CBOR_TAG_DECIMAL_FRACTION; |
| 2997 | } else { |
| 2998 | uTag = CBOR_TAG_INVALID64; |
| 2999 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3000 | QCBOREncode_Private_AddExpMantissa(pMe, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3001 | uTag, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3002 | Mantissa, |
| 3003 | bIsNegative, |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3004 | 0, |
| 3005 | nBase10Exponent); |
| 3006 | } |
| 3007 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3008 | static inline void |
| 3009 | QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe, |
| 3010 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3011 | const uint8_t uTagRequirement, |
| 3012 | const UsefulBufC Mantissa, |
| 3013 | const bool bIsNegative, |
| 3014 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3015 | { |
| 3016 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3017 | QCBOREncode_AddTDecimalFractionBigNum(pMe, |
| 3018 | uTagRequirement, |
| 3019 | Mantissa, |
| 3020 | bIsNegative, |
| 3021 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3022 | } |
| 3023 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3024 | static inline void |
| 3025 | QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3026 | const int64_t nLabel, |
| 3027 | const uint8_t uTagRequirement, |
| 3028 | const UsefulBufC Mantissa, |
| 3029 | const bool bIsNegative, |
| 3030 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3031 | { |
| 3032 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3033 | QCBOREncode_AddTDecimalFractionBigNum(pMe, |
| 3034 | uTagRequirement, |
| 3035 | Mantissa, |
| 3036 | bIsNegative, |
| 3037 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3038 | } |
| 3039 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3040 | static inline void |
| 3041 | QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3042 | const UsefulBufC Mantissa, |
| 3043 | const bool bIsNegative, |
| 3044 | const int64_t nBase10Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3045 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3046 | QCBOREncode_AddTDecimalFractionBigNum(pMe, |
| 3047 | QCBOR_ENCODE_AS_TAG, |
| 3048 | Mantissa, |
| 3049 | bIsNegative, |
| 3050 | nBase10Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3051 | } |
| 3052 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3053 | static inline void |
| 3054 | QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe, |
| 3055 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3056 | const UsefulBufC Mantissa, |
| 3057 | const bool bIsNegative, |
| 3058 | const int64_t nBase10Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3059 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3060 | QCBOREncode_AddTDecimalFractionBigNumToMapSZ(pMe, |
| 3061 | szLabel, |
| 3062 | QCBOR_ENCODE_AS_TAG, |
| 3063 | Mantissa, |
| 3064 | bIsNegative, |
| 3065 | nBase10Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3066 | } |
| 3067 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3068 | static inline void |
| 3069 | QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3070 | const int64_t nLabel, |
| 3071 | const UsefulBufC Mantissa, |
| 3072 | const bool bIsNegative, |
| 3073 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3074 | { |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3075 | QCBOREncode_AddTDecimalFractionBigNumToMapN(pMe, |
| 3076 | nLabel, |
| 3077 | QCBOR_ENCODE_AS_TAG, |
| 3078 | Mantissa, |
| 3079 | bIsNegative, |
| 3080 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3081 | } |
| 3082 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3083 | |
| 3084 | |
| 3085 | |
| 3086 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3087 | static inline void |
| 3088 | QCBOREncode_AddTBigFloat(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3089 | const uint8_t uTagRequirement, |
| 3090 | const int64_t nMantissa, |
| 3091 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3092 | { |
| 3093 | uint64_t uTag; |
| 3094 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3095 | uTag = CBOR_TAG_BIGFLOAT; |
| 3096 | } else { |
| 3097 | uTag = CBOR_TAG_INVALID64; |
| 3098 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3099 | QCBOREncode_Private_AddExpMantissa(pMe, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3100 | uTag, |
| 3101 | NULLUsefulBufC, |
| 3102 | false, |
| 3103 | nMantissa, |
| 3104 | nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3105 | } |
| 3106 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3107 | static inline void |
| 3108 | QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pMe, |
| 3109 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3110 | const uint8_t uTagRequirement, |
| 3111 | const int64_t nMantissa, |
| 3112 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3113 | { |
| 3114 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3115 | QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent); |
| 3116 | } |
| 3117 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3118 | static inline void |
| 3119 | QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3120 | const int64_t nLabel, |
| 3121 | const uint8_t uTagRequirement, |
| 3122 | const int64_t nMantissa, |
| 3123 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3124 | { |
| 3125 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3126 | QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent); |
| 3127 | } |
| 3128 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3129 | static inline void |
| 3130 | QCBOREncode_AddBigFloat(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3131 | const int64_t nMantissa, |
| 3132 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3133 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3134 | QCBOREncode_AddTBigFloat(pMe, |
| 3135 | QCBOR_ENCODE_AS_TAG, |
| 3136 | nMantissa, |
| 3137 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3138 | } |
| 3139 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3140 | static inline void |
| 3141 | QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pMe, |
| 3142 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3143 | const int64_t nMantissa, |
| 3144 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3145 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3146 | QCBOREncode_AddTBigFloatToMapSZ(pMe, |
| 3147 | szLabel, |
| 3148 | QCBOR_ENCODE_AS_TAG, |
| 3149 | nMantissa, |
| 3150 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3151 | } |
| 3152 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3153 | static inline void |
| 3154 | QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3155 | const int64_t nLabel, |
| 3156 | const int64_t nMantissa, |
| 3157 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3158 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3159 | QCBOREncode_AddTBigFloatToMapN(pMe, |
| 3160 | nLabel, |
| 3161 | QCBOR_ENCODE_AS_TAG, |
| 3162 | nMantissa, |
| 3163 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3164 | } |
| 3165 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3166 | |
| 3167 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3168 | static inline void |
| 3169 | QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3170 | const uint8_t uTagRequirement, |
| 3171 | const UsefulBufC Mantissa, |
| 3172 | const bool bIsNegative, |
| 3173 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3174 | { |
| 3175 | uint64_t uTag; |
| 3176 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3177 | uTag = CBOR_TAG_BIGFLOAT; |
| 3178 | } else { |
| 3179 | uTag = CBOR_TAG_INVALID64; |
| 3180 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3181 | QCBOREncode_Private_AddExpMantissa(pMe, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3182 | uTag, |
| 3183 | Mantissa, |
| 3184 | bIsNegative, |
| 3185 | 0, |
| 3186 | nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3187 | } |
| 3188 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3189 | static inline void |
| 3190 | QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pMe, |
| 3191 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3192 | const uint8_t uTagRequirement, |
| 3193 | const UsefulBufC Mantissa, |
| 3194 | const bool bIsNegative, |
| 3195 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3196 | { |
| 3197 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3198 | QCBOREncode_AddTBigFloatBigNum(pMe, |
| 3199 | uTagRequirement, |
| 3200 | Mantissa, |
| 3201 | bIsNegative, |
| 3202 | nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3203 | } |
| 3204 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3205 | static inline void |
| 3206 | QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3207 | const int64_t nLabel, |
| 3208 | const uint8_t uTagRequirement, |
| 3209 | const UsefulBufC Mantissa, |
| 3210 | const bool bIsNegative, |
| 3211 | const int64_t nBase2Exponent) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3212 | { |
| 3213 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3214 | QCBOREncode_AddTBigFloatBigNum(pMe, |
| 3215 | uTagRequirement, |
| 3216 | Mantissa, |
| 3217 | bIsNegative, |
| 3218 | nBase2Exponent); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3219 | } |
| 3220 | |
| 3221 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3222 | static inline void |
| 3223 | QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3224 | const UsefulBufC Mantissa, |
| 3225 | const bool bIsNegative, |
| 3226 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3227 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3228 | QCBOREncode_AddTBigFloatBigNum(pMe, |
| 3229 | QCBOR_ENCODE_AS_TAG, |
| 3230 | Mantissa, bIsNegative, |
| 3231 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3232 | } |
| 3233 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3234 | static inline void |
| 3235 | QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pMe, |
| 3236 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3237 | const UsefulBufC Mantissa, |
| 3238 | const bool bIsNegative, |
| 3239 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3240 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3241 | QCBOREncode_AddTBigFloatBigNumToMapSZ(pMe, |
| 3242 | szLabel, |
| 3243 | QCBOR_ENCODE_AS_TAG, |
| 3244 | Mantissa, |
| 3245 | bIsNegative, |
| 3246 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3247 | } |
| 3248 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3249 | static inline void |
| 3250 | QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3251 | const int64_t nLabel, |
| 3252 | const UsefulBufC Mantissa, |
| 3253 | const bool bIsNegative, |
| 3254 | const int64_t nBase2Exponent) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3255 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3256 | QCBOREncode_AddTBigFloatBigNumToMapN(pMe, |
| 3257 | nLabel, |
| 3258 | QCBOR_ENCODE_AS_TAG, |
| 3259 | Mantissa, |
| 3260 | bIsNegative, |
| 3261 | nBase2Exponent); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3262 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 3263 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3264 | |
| 3265 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3266 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3267 | QCBOREncode_AddTURI(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3268 | const uint8_t uTagRequirement, |
| 3269 | const UsefulBufC URI) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3270 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3271 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3272 | QCBOREncode_AddTag(pMe, CBOR_TAG_URI); |
| 3273 | } |
| 3274 | QCBOREncode_AddText(pMe, URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3275 | } |
| 3276 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3277 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3278 | QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pMe, |
| 3279 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3280 | const uint8_t uTagRequirement, |
| 3281 | const UsefulBufC URI) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3282 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3283 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3284 | QCBOREncode_AddTURI(pMe, uTagRequirement, URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3285 | } |
| 3286 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3287 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3288 | QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3289 | const int64_t nLabel, |
| 3290 | const uint8_t uTagRequirement, |
| 3291 | const UsefulBufC URI) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3292 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3293 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3294 | QCBOREncode_AddTURI(pMe, uTagRequirement, URI); |
| 3295 | } |
| 3296 | |
| 3297 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3298 | QCBOREncode_AddURI(QCBOREncodeContext *pMe, const UsefulBufC URI) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3299 | { |
| 3300 | QCBOREncode_AddTURI(pMe, QCBOR_ENCODE_AS_TAG, URI); |
| 3301 | } |
| 3302 | |
| 3303 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3304 | QCBOREncode_AddURIToMap(QCBOREncodeContext *pMe, |
| 3305 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3306 | const UsefulBufC URI) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3307 | { |
| 3308 | QCBOREncode_AddTURIToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, URI); |
| 3309 | } |
| 3310 | |
| 3311 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3312 | QCBOREncode_AddURIToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3313 | const int64_t nLabel, |
| 3314 | const UsefulBufC URI) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3315 | { |
| 3316 | QCBOREncode_AddTURIToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, URI); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3317 | } |
| 3318 | |
| 3319 | |
| 3320 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3321 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3322 | QCBOREncode_AddTB64Text(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3323 | const uint8_t uTagRequirement, |
| 3324 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3325 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3326 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3327 | QCBOREncode_AddTag(pMe, CBOR_TAG_B64); |
| 3328 | } |
| 3329 | QCBOREncode_AddText(pMe, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3330 | } |
| 3331 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3332 | static inline void |
| 3333 | QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pMe, |
| 3334 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3335 | const uint8_t uTagRequirement, |
| 3336 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3337 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3338 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3339 | QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3340 | } |
| 3341 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3342 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3343 | QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3344 | const int64_t nLabel, |
| 3345 | const uint8_t uTagRequirement, |
| 3346 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3347 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3348 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3349 | QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text); |
| 3350 | } |
| 3351 | |
| 3352 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3353 | QCBOREncode_AddB64Text(QCBOREncodeContext *pMe, const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3354 | { |
| 3355 | QCBOREncode_AddTB64Text(pMe, QCBOR_ENCODE_AS_TAG, B64Text); |
| 3356 | } |
| 3357 | |
| 3358 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3359 | QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pMe, |
| 3360 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3361 | const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3362 | { |
| 3363 | QCBOREncode_AddTB64TextToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, B64Text); |
| 3364 | } |
| 3365 | |
| 3366 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3367 | QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3368 | const int64_t nLabel, |
| 3369 | const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3370 | { |
| 3371 | QCBOREncode_AddTB64TextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3375 | |
| 3376 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3377 | QCBOREncode_AddTB64URLText(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3378 | const uint8_t uTagRequirement, |
| 3379 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3380 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3381 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3382 | QCBOREncode_AddTag(pMe, CBOR_TAG_B64URL); |
| 3383 | } |
| 3384 | QCBOREncode_AddText(pMe, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3385 | } |
| 3386 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3387 | static inline void |
| 3388 | QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pMe, |
| 3389 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3390 | const uint8_t uTagRequirement, |
| 3391 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3392 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3393 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3394 | QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3395 | } |
| 3396 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3397 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3398 | QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3399 | const int64_t nLabel, |
| 3400 | const uint8_t uTagRequirement, |
| 3401 | const UsefulBufC B64Text) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3402 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3403 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3404 | QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text); |
| 3405 | } |
| 3406 | |
| 3407 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3408 | QCBOREncode_AddB64URLText(QCBOREncodeContext *pMe, const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3409 | { |
| 3410 | QCBOREncode_AddTB64URLText(pMe, QCBOR_ENCODE_AS_TAG, B64Text); |
| 3411 | } |
| 3412 | |
| 3413 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3414 | QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pMe, |
| 3415 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3416 | const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3417 | { |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3418 | QCBOREncode_AddTB64URLTextToMapSZ(pMe, |
| 3419 | szLabel, |
| 3420 | QCBOR_ENCODE_AS_TAG, |
| 3421 | B64Text); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3425 | QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3426 | const int64_t nLabel, |
| 3427 | const UsefulBufC B64Text) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3428 | { |
| 3429 | QCBOREncode_AddTB64URLTextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3433 | |
| 3434 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3435 | QCBOREncode_AddTRegex(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3436 | const uint8_t uTagRequirement, |
| 3437 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3438 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3439 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3440 | QCBOREncode_AddTag(pMe, CBOR_TAG_REGEX); |
| 3441 | } |
| 3442 | QCBOREncode_AddText(pMe, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3443 | } |
| 3444 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3445 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3446 | QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pMe, |
| 3447 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3448 | const uint8_t uTagRequirement, |
| 3449 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3450 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3451 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3452 | QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3453 | } |
| 3454 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3455 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3456 | QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3457 | const int64_t nLabel, |
| 3458 | const uint8_t uTagRequirement, |
| 3459 | const UsefulBufC Bytes) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3460 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3461 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3462 | QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes); |
| 3463 | } |
| 3464 | |
| 3465 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3466 | QCBOREncode_AddRegex(QCBOREncodeContext *pMe, const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3467 | { |
| 3468 | QCBOREncode_AddTRegex(pMe, QCBOR_ENCODE_AS_TAG, Bytes); |
| 3469 | } |
| 3470 | |
| 3471 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3472 | QCBOREncode_AddRegexToMap(QCBOREncodeContext *pMe, |
| 3473 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3474 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3475 | { |
| 3476 | QCBOREncode_AddTRegexToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes); |
| 3477 | } |
| 3478 | |
| 3479 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3480 | QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3481 | const int64_t nLabel, |
| 3482 | const UsefulBufC Bytes) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3483 | { |
| 3484 | QCBOREncode_AddTRegexToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes); |
| 3485 | |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3489 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3490 | QCBOREncode_AddTMIMEData(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3491 | const uint8_t uTagRequirement, |
| 3492 | const UsefulBufC MIMEData) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3493 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3494 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3495 | QCBOREncode_AddTag(pMe, CBOR_TAG_BINARY_MIME); |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3496 | } |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 3497 | QCBOREncode_AddBytes(pMe, MIMEData); |
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 |
| 3501 | QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pMe, |
| 3502 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3503 | const uint8_t uTagRequirement, |
| 3504 | const UsefulBufC MIMEData) |
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_AddSZString(pMe, szLabel); |
| 3507 | QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3508 | } |
| 3509 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3510 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3511 | QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3512 | const int64_t nLabel, |
| 3513 | const uint8_t uTagRequirement, |
| 3514 | const UsefulBufC MIMEData) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3515 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3516 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3517 | QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData); |
| 3518 | } |
| 3519 | |
| 3520 | static inline void |
| 3521 | QCBOREncode_AddMIMEData(QCBOREncodeContext *pMe, UsefulBufC MIMEData) |
| 3522 | { |
| 3523 | QCBOREncode_AddTMIMEData(pMe, QCBOR_ENCODE_AS_TAG, MIMEData); |
| 3524 | } |
| 3525 | |
| 3526 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3527 | QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pMe, |
| 3528 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3529 | const UsefulBufC MIMEData) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3530 | { |
| 3531 | QCBOREncode_AddTMIMEDataToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, MIMEData); |
| 3532 | } |
| 3533 | |
| 3534 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3535 | QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3536 | const int64_t nLabel, |
| 3537 | const UsefulBufC MIMEData) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3538 | { |
| 3539 | QCBOREncode_AddTMIMEDataToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, MIMEData); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3543 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3544 | QCBOREncode_AddTDateString(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3545 | const uint8_t uTagRequirement, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3546 | const char *szDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3547 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3548 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3549 | QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_STRING); |
| 3550 | } |
| 3551 | QCBOREncode_AddSZString(pMe, szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3552 | } |
| 3553 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3554 | static inline void |
| 3555 | QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pMe, |
| 3556 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3557 | const uint8_t uTagRequirement, |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3558 | const char *szDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3559 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3560 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3561 | QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3562 | } |
| 3563 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3564 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3565 | QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3566 | const int64_t nLabel, |
| 3567 | const uint8_t uTagRequirement, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3568 | const char *szDate) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3569 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3570 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3571 | QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate); |
| 3572 | } |
| 3573 | |
| 3574 | static inline void |
| 3575 | QCBOREncode_AddDateString(QCBOREncodeContext *pMe, const char *szDate) |
| 3576 | { |
| 3577 | QCBOREncode_AddTDateString(pMe, QCBOR_ENCODE_AS_TAG, szDate); |
| 3578 | } |
| 3579 | |
| 3580 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3581 | QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pMe, |
| 3582 | const char *szLabel, |
| 3583 | const char *szDate) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3584 | { |
| 3585 | QCBOREncode_AddTDateStringToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, szDate); |
| 3586 | } |
| 3587 | |
| 3588 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3589 | QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3590 | const int64_t nLabel, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3591 | const char *szDate) |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3592 | { |
| 3593 | QCBOREncode_AddTDateStringToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, szDate); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3597 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3598 | QCBOREncode_AddTDaysString(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3599 | const uint8_t uTagRequirement, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3600 | const char *szDate) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3601 | { |
| 3602 | if(uTagRequirement == QCBOR_ENCODE_AS_TAG) { |
| 3603 | QCBOREncode_AddTag(pMe, CBOR_TAG_DAYS_STRING); |
| 3604 | } |
| 3605 | QCBOREncode_AddSZString(pMe, szDate); |
| 3606 | } |
| 3607 | |
| 3608 | static inline void |
| 3609 | QCBOREncode_AddTDaysStringToMapSZ(QCBOREncodeContext *pMe, |
| 3610 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3611 | const uint8_t uTagRequirement, |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3612 | const char *szDate) |
| 3613 | { |
| 3614 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3615 | QCBOREncode_AddTDaysString(pMe, uTagRequirement, szDate); |
| 3616 | } |
| 3617 | |
| 3618 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3619 | QCBOREncode_AddTDaysStringToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3620 | const int64_t nLabel, |
| 3621 | const uint8_t uTagRequirement, |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3622 | const char *szDate) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 3623 | { |
| 3624 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3625 | QCBOREncode_AddTDaysString(pMe, uTagRequirement, szDate); |
| 3626 | } |
| 3627 | |
| 3628 | |
| 3629 | |
| 3630 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3631 | QCBOREncode_Private_AddSimple(QCBOREncodeContext *pMe, const uint64_t uNum) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3632 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3633 | QCBOREncode_Private_AddType7(pMe, 0, uNum); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3634 | } |
| 3635 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3636 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3637 | QCBOREncode_Private_AddSimpleToMap(QCBOREncodeContext *pMe, |
| 3638 | const char *szLabel, |
| 3639 | const uint8_t uSimple) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3640 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3641 | QCBOREncode_AddSZString(pMe, szLabel); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3642 | QCBOREncode_Private_AddSimple(pMe, uSimple); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3643 | } |
| 3644 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3645 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3646 | QCBOREncode_Private_AddSimpleToMapN(QCBOREncodeContext *pMe, |
| 3647 | const int64_t nLabel, |
| 3648 | const uint8_t uSimple) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3649 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3650 | QCBOREncode_AddInt64(pMe, nLabel); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3651 | QCBOREncode_Private_AddSimple(pMe, uSimple); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3655 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3656 | QCBOREncode_AddBool(QCBOREncodeContext *pMe, const bool b) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3657 | { |
| 3658 | uint8_t uSimple = CBOR_SIMPLEV_FALSE; |
| 3659 | if(b) { |
| 3660 | uSimple = CBOR_SIMPLEV_TRUE; |
| 3661 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3662 | QCBOREncode_Private_AddSimple(pMe, uSimple); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3663 | } |
| 3664 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3665 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3666 | QCBOREncode_AddBoolToMap(QCBOREncodeContext *pMe, const char *szLabel, const bool b) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3667 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3668 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3669 | QCBOREncode_AddBool(pMe, b); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3670 | } |
| 3671 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3672 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3673 | QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pMe, const int64_t nLabel, const bool b) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3674 | { |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3675 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3676 | QCBOREncode_AddBool(pMe, b); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3677 | } |
| 3678 | |
| 3679 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3680 | static inline void |
| 3681 | QCBOREncode_AddNULL(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3682 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3683 | QCBOREncode_Private_AddSimple(pMe, CBOR_SIMPLEV_NULL); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3684 | } |
| 3685 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3686 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3687 | QCBOREncode_AddNULLToMap(QCBOREncodeContext *pMe, const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3688 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3689 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3690 | QCBOREncode_AddNULL(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3691 | } |
| 3692 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3693 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3694 | QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pMe, const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3695 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3696 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3697 | QCBOREncode_AddNULL(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3701 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3702 | QCBOREncode_AddUndef(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3703 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3704 | QCBOREncode_Private_AddSimple(pMe, CBOR_SIMPLEV_UNDEF); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3705 | } |
| 3706 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3707 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3708 | QCBOREncode_AddUndefToMap(QCBOREncodeContext *pMe, const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3709 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3710 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3711 | QCBOREncode_AddUndef(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3712 | } |
| 3713 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3714 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3715 | QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pMe, const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3716 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3717 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3718 | QCBOREncode_AddUndef(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3722 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3723 | QCBOREncode_OpenArray(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3724 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3725 | QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3726 | } |
| 3727 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3728 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3729 | QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pMe, const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3730 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3731 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3732 | QCBOREncode_OpenArray(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3733 | } |
| 3734 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3735 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3736 | QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pMe, const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3737 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3738 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3739 | QCBOREncode_OpenArray(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3740 | } |
| 3741 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3742 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3743 | QCBOREncode_CloseArray(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3744 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3745 | QCBOREncode_Private_CloseMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3749 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3750 | QCBOREncode_OpenMap(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3751 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3752 | QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_MAP); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3753 | } |
| 3754 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3755 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3756 | QCBOREncode_OpenMapInMap(QCBOREncodeContext *pMe, const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3757 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3758 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3759 | QCBOREncode_OpenMap(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3760 | } |
| 3761 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3762 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3763 | QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pMe, const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3764 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3765 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3766 | QCBOREncode_OpenMap(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3767 | } |
| 3768 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3769 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3770 | QCBOREncode_CloseMap(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3771 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3772 | QCBOREncode_Private_CloseMapOrArray(pMe, CBOR_MAJOR_TYPE_MAP); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3773 | } |
| 3774 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3775 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3776 | QCBOREncode_OpenArrayIndefiniteLength(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3777 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3778 | QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3779 | } |
| 3780 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3781 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3782 | QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pMe, |
| 3783 | const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3784 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3785 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3786 | QCBOREncode_OpenArrayIndefiniteLength(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3787 | } |
| 3788 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3789 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3790 | QCBOREncode_OpenArrayIndefiniteLengthInMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3791 | const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3792 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3793 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3794 | QCBOREncode_OpenArrayIndefiniteLength(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3795 | } |
| 3796 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3797 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3798 | QCBOREncode_CloseArrayIndefiniteLength(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3799 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3800 | QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3801 | } |
| 3802 | |
| 3803 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3804 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3805 | QCBOREncode_OpenMapIndefiniteLength(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3806 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3807 | QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 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_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pMe, |
| 3812 | const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3813 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3814 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3815 | QCBOREncode_OpenMapIndefiniteLength(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3816 | } |
| 3817 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3818 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3819 | QCBOREncode_OpenMapIndefiniteLengthInMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3820 | const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3821 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3822 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3823 | QCBOREncode_OpenMapIndefiniteLength(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3824 | } |
| 3825 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3826 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3827 | QCBOREncode_CloseMapIndefiniteLength(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3828 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3829 | QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3833 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3834 | QCBOREncode_BstrWrap(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3835 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3836 | QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_BYTE_STRING); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3837 | } |
| 3838 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3839 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3840 | QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pMe, const char *szLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3841 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3842 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3843 | QCBOREncode_BstrWrap(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3844 | } |
| 3845 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3846 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3847 | QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pMe, const int64_t nLabel) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3848 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3849 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3850 | QCBOREncode_BstrWrap(pMe); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3851 | } |
| 3852 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3853 | static inline void |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3854 | QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pMe, UsefulBufC *pWrappedCBOR) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3855 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3856 | QCBOREncode_CloseBstrWrap2(pMe, true, pWrappedCBOR); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3860 | static inline void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3861 | QCBOREncode_AddEncoded(QCBOREncodeContext *pMe, const UsefulBufC Encoded) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3862 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3863 | QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_NONE_TYPE_RAW, Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3864 | } |
| 3865 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3866 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3867 | QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pMe, |
| 3868 | const char *szLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3869 | const UsefulBufC Encoded) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3870 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3871 | QCBOREncode_AddSZString(pMe, szLabel); |
| 3872 | QCBOREncode_AddEncoded(pMe, Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3873 | } |
| 3874 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3875 | static inline void |
Laurence Lundblade | 3eead48 | 2023-12-16 20:53:22 -0700 | [diff] [blame] | 3876 | QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3877 | const int64_t nLabel, |
| 3878 | const UsefulBufC Encoded) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3879 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3880 | QCBOREncode_AddInt64(pMe, nLabel); |
| 3881 | QCBOREncode_AddEncoded(pMe, Encoded); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3882 | } |
| 3883 | |
| 3884 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3885 | static inline int |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3886 | QCBOREncode_IsBufferNULL(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3887 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3888 | return UsefulOutBuf_IsBufferNULL(&(pMe->OutBuf)); |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3889 | } |
| 3890 | |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 3891 | static inline QCBORError |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3892 | QCBOREncode_GetErrorState(QCBOREncodeContext *pMe) |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3893 | { |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3894 | if(UsefulOutBuf_GetError(&(pMe->OutBuf))) { |
Laurence Lundblade | f2f0c3f | 2024-04-12 13:01:54 -0700 | [diff] [blame^] | 3895 | /* Items didn't fit in the buffer. This check catches this |
| 3896 | * condition for all the appends and inserts so checks aren't |
| 3897 | * needed when the appends and inserts are performed. And of |
| 3898 | * course UsefulBuf will never overrun the input buffer given to |
| 3899 | * it. No complex analysis of the error handling in this file is |
| 3900 | * needed to know that is true. Just read the UsefulBuf code. |
| 3901 | */ |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3902 | pMe->uError = QCBOR_ERR_BUFFER_TOO_SMALL; |
Laurence Lundblade | f2f0c3f | 2024-04-12 13:01:54 -0700 | [diff] [blame^] | 3903 | /* QCBOR_ERR_BUFFER_TOO_SMALL masks other errors, but that is |
| 3904 | * OK. Once the caller fixes this, they'll be unmasked. |
| 3905 | */ |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3906 | } |
| 3907 | |
Laurence Lundblade | 6416a0f | 2020-09-19 23:26:34 -0700 | [diff] [blame] | 3908 | return (QCBORError)pMe->uError; |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | |
Laurence Lundblade | 45d5e48 | 2020-09-15 21:15:15 -0700 | [diff] [blame] | 3912 | /* ======================================================================== |
| 3913 | END OF PRIVATE INLINE IMPLEMENTATION |
| 3914 | ======================================================================== */ |
Michael Eckel | 5c53133 | 2020-03-02 01:35:30 +0100 | [diff] [blame] | 3915 | |
| 3916 | #ifdef __cplusplus |
| 3917 | } |
| 3918 | #endif |
| 3919 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 3920 | #endif /* qcbor_encode_h */ |