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