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