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