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