blob: 2460897a72ea325aa2ce96cdf2e0fc88529c5f40 [file] [log] [blame]
Michael Eckel5c531332020-03-02 01:35:30 +01001/*==============================================================================
2 Copyright (c) 2016-2018, The Linux Foundation.
3 Copyright (c) 2018-2020, Laurence Lundblade.
4 All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
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
20THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =============================================================================*/
32
33
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#ifndef qcbor_encode_h
35#define qcbor_encode_h
Michael Eckel5c531332020-03-02 01:35:30 +010036
37
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080038#include "qcbor/qcbor_common.h"
39#include "qcbor/qcbor_private.h"
Michael Eckel5c531332020-03-02 01:35:30 +010040#include <stdbool.h>
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080041
Michael Eckel5c531332020-03-02 01:35:30 +010042
43#ifdef __cplusplus
44extern "C" {
Dave Thaler12b23752020-03-27 01:23:08 -070045#if 0
Michael Eckel5c531332020-03-02 01:35:30 +010046} // Keep editor indention formatting happy
47#endif
48#endif
49
Michael Eckel5c531332020-03-02 01:35:30 +010050
51/**
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080052 @file qcbor_encode.h
Michael Eckel5c531332020-03-02 01:35:30 +010053
Laurence Lundblade9a3b6252020-10-21 21:30:31 -070054 @anchor Overview
55
56 # QCBOR Overview
Michael Eckel5c531332020-03-02 01:35:30 +010057
58 This implements CBOR -- Concise Binary Object Representation as
Laurence Lundbladec02e13e2020-12-06 05:45:41 -080059 defined in [RFC 8949] (https://tools.ietf.org/html/rfc8949). More
Laurence Lundblade18e1d522020-10-22 02:18:41 -070060 information is at http://cbor.io. This is a near-complete implementation of
61 the specification. [RFC 8742] (https://tools.ietf.org/html/rfc8742) CBOR
62 Sequences is also supported. Limitations are listed further down.
Michael Eckel5c531332020-03-02 01:35:30 +010063
Laurence Lundblade9a3b6252020-10-21 21:30:31 -070064 See @ref Encoding for general discussion on encoding,
65 @ref BasicDecode for general discussion on the basic decode features
66 and @ref SpiffyDecode for general discussion on the easier-to-use
67 decoder functions.
68
Michael Eckel5c531332020-03-02 01:35:30 +010069 CBOR is intentionally designed to be translatable to JSON, but not
Laurence Lundbladec02e13e2020-12-06 05:45:41 -080070 all CBOR can convert to JSON. See RFC 8949 for more info on how to
Michael Eckel5c531332020-03-02 01:35:30 +010071 construct CBOR that is the most JSON friendly.
72
73 The memory model for encoding and decoding is that encoded CBOR must
74 be in a contiguous buffer in memory. During encoding the caller must
75 supply an output buffer and if the encoding would go off the end of
76 the buffer an error is returned. During decoding the caller supplies
77 the encoded CBOR in a contiguous buffer and the decoder returns
78 pointers and lengths into that buffer for strings.
79
80 This implementation does not require malloc. All data structures
81 passed in/out of the APIs can fit on the stack.
82
83 Decoding of indefinite-length strings is a special case that requires
84 a "string allocator" to allocate memory into which the segments of
85 the string are coalesced. Without this, decoding will error out if an
86 indefinite-length string is encountered (indefinite-length maps and
87 arrays do not require the string allocator). A simple string
88 allocator called MemPool is built-in and will work if supplied with a
89 block of memory to allocate. The string allocator can optionally use
90 malloc() or some other custom scheme.
91
92 Here are some terms and definitions:
93
94 - "Item", "Data Item": An integer or string or such. The basic "thing" that
95 CBOR is about. An array is an item itself that contains some items.
96
97 - "Array": An ordered sequence of items, the same as JSON.
98
99 - "Map": A collection of label/value pairs. Each pair is a data
100 item. A JSON "object" is the same as a CBOR "map".
101
102 - "Label": The data item in a pair in a map that names or identifies
103 the pair, not the value. This implementation refers to it as a
104 "label". JSON refers to it as the "name". The CBOR RFC refers to it
105 this as a "key". This implementation chooses label instead because
106 key is too easily confused with a cryptographic key. The COSE
107 standard, which uses CBOR, has also chosen to use the term "label"
108 rather than "key" for this same reason.
109
110 - "Key": See "Label" above.
111
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700112 - "Tag": A data item that is an explicitly labeled new data
113 type made up of the tagging integer and the tag content.
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700114 See @ref Tags-Overview and @ref Tag-Usage.
Michael Eckel5c531332020-03-02 01:35:30 +0100115
116 - "Initial Byte": The first byte of an encoded item. Encoding and
117 decoding of this byte is taken care of by the implementation.
118
119 - "Additional Info": In addition to the major type, all data items
120 have some other info. This is usually the length of the data but can
121 be several other things. Encoding and decoding of this is taken care
122 of by the implementation.
123
124 CBOR has two mechanisms for tagging and labeling the data values like
125 integers and strings. For example, an integer that represents
126 someone's birthday in epoch seconds since Jan 1, 1970 could be
127 encoded like this:
128
129 - First it is CBOR_MAJOR_TYPE_POSITIVE_INT (@ref QCBOR_TYPE_INT64),
130 the primitive positive integer.
131
132 - Next it has a "tag" @ref CBOR_TAG_DATE_EPOCH indicating the integer
133 represents a date in the form of the number of seconds since Jan 1,
134 1970.
135
136 - Last it has a string "label" like "BirthDate" indicating the
137 meaning of the data.
138
139 The encoded binary looks like this:
140
141 a1 # Map of 1 item
142 69 # Indicates text string of 9 bytes
143 426972746844617465 # The text "BirthDate"
144 c1 # Tags next integer as epoch date
145 1a # Indicates a 4-byte integer
146 580d4172 # unsigned integer date 1477263730
147
148 Implementors using this API will primarily work with
149 labels. Generally, tags are only needed for making up new data
150 types. This implementation covers most of the data types defined in
151 the RFC using tags. It also, allows for the use of custom tags if
152 necessary.
153
154 This implementation explicitly supports labels that are text strings
155 and integers. Text strings translate nicely into JSON objects and are
156 very readable. Integer labels are much less readable but can be very
157 compact. If they are in the range of 0 to 23, they take up only one
158 byte.
159
160 CBOR allows a label to be any type of data including an array or a
161 map. It is possible to use this API to construct and parse such
162 labels, but it is not explicitly supported.
163
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700164 @anchor Encoding
165
166 ## Encoding
167
Michael Eckel5c531332020-03-02 01:35:30 +0100168 A common encoding usage mode is to invoke the encoding twice. First
169 with no output buffer to compute the length of the needed output
170 buffer. Then the correct sized output buffer is allocated. Last the
171 encoder is invoked again, this time with the output buffer.
172
173 The double invocation is not required if the maximum output buffer
174 size can be predicted. This is usually possible for simple CBOR
175 structures. If the double invocation is implemented, it can be in a
176 loop or function as in the example code so that the code doesn't have
177 to actually be written twice, saving code size.
178
179 If a buffer too small to hold the encoded output is given, the error
180 @ref QCBOR_ERR_BUFFER_TOO_SMALL will be returned. Data will never be
181 written off the end of the output buffer no matter which functions
182 here are called or what parameters are passed to them.
183
184 The encoding error handling is simple. The only possible errors are
185 trying to encode structures that are too large or too complex. There
186 are no internal malloc calls so there will be no failures for out of
187 memory. The error state is tracked internally, so there is no need
188 to check for errors when encoding. Only the return code from
189 QCBOREncode_Finish() need be checked as once an error happens, the
190 encoder goes into an error state and calls to it to add more data
191 will do nothing. An error check is not needed after every data item
192 is added.
193
194 Encoding generally proceeds by calling QCBOREncode_Init(), calling
195 lots of @c QCBOREncode_AddXxx() functions and calling
196 QCBOREncode_Finish(). There are many @c QCBOREncode_AddXxx()
197 functions for various data types. The input buffers need only to be
198 valid during the @c QCBOREncode_AddXxx() calls as the data is copied
199 into the output buffer.
200
201 There are three `Add` functions for each data type. The first / main
202 one for the type is for adding the data item to an array. The second
203 one's name ends in `ToMap`, is used for adding data items to maps and
204 takes a string argument that is its label in the map. The third one
205 ends in `ToMapN`, is also used for adding data items to maps, and
206 takes an integer argument that is its label in the map.
207
208 The simplest aggregate type is an array, which is a simple ordered
209 set of items without labels the same as JSON arrays. Call
210 QCBOREncode_OpenArray() to open a new array, then various @c
211 QCBOREncode_AddXxx() functions to put items in the array and then
212 QCBOREncode_CloseArray(). Nesting to the limit @ref
213 QCBOR_MAX_ARRAY_NESTING is allowed. All opens must be matched by
214 closes or an encoding error will be returned.
215
216 The other aggregate type is a map which does use labels. The `Add`
217 functions that end in `ToMap` and `ToMapN` are convenient ways to add
218 labeled data items to a map. You can also call any type of `Add`
219 function once to add a label of any time and then call any type of
220 `Add` again to add its value.
221
222 Note that when you nest arrays or maps in a map, the nested array or
223 map has a label.
Laurence Lundblade2feb1e12020-07-15 03:50:45 -0700224
Laurence Lundbladee3553422020-05-02 11:11:17 -0700225 Many CBOR-based protocols start with an array or map. This makes them
226 self-delimiting. No external length or end marker is needed to know
227 the end. It is also possible not start this way, in which case this
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700228 it is usually called a CBOR sequence which is described in
229 [RFC 8742] (https://tools.ietf.org/html/rfc8742). This encoder supports
230 either just by whether the first item added is an array, map or other.
Michael Eckel5c531332020-03-02 01:35:30 +0100231
Laurence Lundbladeda97bf42020-11-05 03:38:50 -0800232 If QCBOR is compiled with QCBOR_DISABLE_ENCODE_USAGE_GUARDS defined,
233 the errors QCBOR_ERR_CLOSE_MISMATCH, QCBOR_ERR_ARRAY_TOO_LONG,
234 QCBOR_ERR_TOO_MANY_CLOSES, QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN, and
235 QCBOR_ERR_ENCODE_UNSUPPORTED will never be returned. It is up to the
236 caller to make sure that opened maps, arrays and byte-string wrapping
237 is closed correctly and that QCBOREncode_AddType7() is called
238 correctly. With this defined, it is easier to make a mistake when
239 authoring the encoding of a protocol that will output not well formed
240 CBOR, but as long as the calling code is correct, it is safe to
241 disable these checks. Bounds checking that prevents security issues
242 in the code is still enforced. This define reduces the size of
243 encoding object code by about 150 bytes.
244
Michael Eckel5c531332020-03-02 01:35:30 +0100245 @anchor Tags-Overview
Laurence Lundblade9b334962020-08-27 10:55:53 -0700246
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700247 ## Tags Overview
248
249 Any CBOR data item can be made into a tag to add semantics, define a
250 new data type or such. Some tags are fully standardized and some are
251 just registered. Others are not registered and used in a proprietary
252 way.
Michael Eckel5c531332020-03-02 01:35:30 +0100253
254 Encoding and decoding of many of the registered tags is fully
255 implemented by QCBOR. It is also possible to encode and decode tags
256 that are not directly supported. For many use cases the built-in tag
257 support should be adequate.
258
259 For example, the registered epoch date tag is supported in encoding
260 by QCBOREncode_AddDateEpoch() and in decoding by @ref
261 QCBOR_TYPE_DATE_EPOCH and the @c epochDate member of @ref
262 QCBORItem. This is typical of the built-in tag support. There is an
263 API to encode data for it and a @c QCBOR_TYPE_XXX when it is decoded.
264
265 Tags are registered in the [IANA CBOR Tags Registry]
266 (https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml). There
267 are roughly three options to create a new tag. First, a public
268 specification can be created and the new tag registered with IANA.
269 This is the most formal. Second, the new tag can be registered with
270 IANA with just a short description rather than a full specification.
271 These tags must be greater than 256. Third, a tag can be used without
272 any IANA registration, though the registry should be checked to see
273 that the new value doesn't collide with one that is registered. The
274 value of these tags must be 256 or larger.
275
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700276 See also @ref CBORTags and @ref Tag-Usage
277
Michael Eckel5c531332020-03-02 01:35:30 +0100278 The encoding side of tags not built-in is handled by
279 QCBOREncode_AddTag() and is relatively simple. Tag decoding is more
280 complex and mainly handled by QCBORDecode_GetNext(). Decoding of the
281 structure of tagged data not built-in (if there is any) has to be
282 implemented by the caller.
283
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700284 @anchor Floating-Point
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700285
286 ## Floating-Point
287
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700288 By default QCBOR fully supports IEEE 754 floating-point:
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700289 - Encode/decode of double, single and half-precision
290 - CBOR preferred serialization of floating-point
291 - Floating-point epoch dates
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700292
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700293 For the most part, the type double is used in the interface for
294 floating-point values. In the default configuration, all decoded
295 floating-point values are returned as a double.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700296
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700297 With CBOR preferred serialization, the encoder outputs the smallest
298 representation of the double or float that preserves precision. Zero,
299 NaN and infinity are always output as a half-precision, each taking
300 just 2 bytes. This reduces the number of bytes needed to encode
Laurence Lundblade4b270642020-08-14 12:53:07 -0700301 double and single-precision, especially if zero, NaN and infinity are
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700302 frequently used.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700303
Laurence Lundblade4b270642020-08-14 12:53:07 -0700304 To avoid use of preferred serialization in the standard configuration
305 when encoding, use QCBOREncode_AddDoubleNoPreferred() or
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700306 QCBOREncode_AddFloatNoPreferred().
307
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700308 This implementation of preferred floating-point serialization and
309 half-precision does not depend on the CPU having floating-point HW or
310 the compiler bringing in a (sometimes large) library to compensate
Laurence Lundblade4b270642020-08-14 12:53:07 -0700311 for lack of CPU support. This implementation uses shifts and masks
312 rather than floating-point functions.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700313
Laurence Lundblade4b270642020-08-14 12:53:07 -0700314 To reduce overall object code by about 900 bytes, define
315 QCBOR_DISABLE_PREFERRED_FLOAT. This will eliminate all support for
316 preferred serialization and half-precision. An error will be returned
317 when attempting to decode half-precision. A float will always be
318 encoded and decoded as 32-bits and a double will always be encoded
319 and decoded as 64 bits.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700320
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700321 Note that even if QCBOR_DISABLE_PREFERRED_FLOAT is not defined all
322 the float-point encoding object code can be avoided by never calling
Laurence Lundbladefe09bbf2020-07-16 12:14:51 -0700323 any functions that encode double or float. Just not calling
324 floating-point functions will reduce object code by about 500 bytes.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700325
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700326 On CPUs that have no floating-point hardware,
327 QCBOR_DISABLE_FLOAT_HW_USE should be defined in most cases. If it is
328 not, then the compiler will bring in possibly large software
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700329 libraries to compensate. Defining QCBOR_DISABLE_FLOAT_HW_USE reduces
330 object code size on CPUs with floating-point hardware by a tiny
331 amount and eliminates the need for <math.h>
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700332
Laurence Lundblade4b270642020-08-14 12:53:07 -0700333 When QCBOR_DISABLE_FLOAT_HW_USE is defined, trying to decoding
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700334 floating-point dates will give error
335 @ref QCBOR_ERR_FLOAT_DATE_DISABLED and decoded single-precision
336 numbers will be returned as @ref QCBOR_TYPE_FLOAT instead of
337 converting them to double as usual.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700338
339 If both QCBOR_DISABLE_FLOAT_HW_USE and QCBOR_DISABLE_PREFERRED_FLOAT
Laurence Lundblade4b270642020-08-14 12:53:07 -0700340 are defined, then the only thing QCBOR can do is encode/decode a C
341 float type as 32-bits and a C double type as 64-bits. Floating-point
342 epoch dates will be unsupported.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700343
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700344 ## Limitations
345
Michael Eckel5c531332020-03-02 01:35:30 +0100346 Summary Limits of this implementation:
347 - The entire encoded CBOR must fit into contiguous memory.
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700348 - Max size of encoded / decoded CBOR data is a few bytes less than @c UINT32_MAX (4GB).
Michael Eckel5c531332020-03-02 01:35:30 +0100349 - Max array / map nesting level when encoding / decoding is
350 @ref QCBOR_MAX_ARRAY_NESTING (this is typically 15).
351 - Max items in an array or map when encoding / decoding is
352 @ref QCBOR_MAX_ITEMS_IN_ARRAY (typically 65,536).
353 - Does not directly support labels in maps other than text strings & integers.
354 - Does not directly support integer labels greater than @c INT64_MAX.
355 - Epoch dates limited to @c INT64_MAX (+/- 292 billion years).
356 - Exponents for bigfloats and decimal integers are limited to @c INT64_MAX.
357 - Tags on labels are ignored during decoding.
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700358 - The maximum tag nesting is @c QCBOR_MAX_TAGS_PER_ITEM (typically 4).
Michael Eckel5c531332020-03-02 01:35:30 +0100359 - Works only on 32- and 64-bit CPUs (modifications could make it work
360 on 16-bit CPUs).
361
362 The public interface uses @c size_t for all lengths. Internally the
363 implementation uses 32-bit lengths by design to use less memory and
364 fit structures on the stack. This limits the encoded CBOR it can work
365 with to size @c UINT32_MAX (4GB) which should be enough.
366
367 This implementation assumes two's compliment integer machines. @c
368 <stdint.h> also requires this. It is possible to modify this
369 implementation for another integer representation, but all modern
370 machines seem to be two's compliment.
Michael Eckel5c531332020-03-02 01:35:30 +0100371 */
372
373
Laurence Lundblade825164e2020-10-22 20:18:06 -0700374/**
Michael Eckel5c531332020-03-02 01:35:30 +0100375 The size of the buffer to be passed to QCBOREncode_EncodeHead(). It is one
376 byte larger than sizeof(uint64_t) + 1, the actual maximum size of the
Laurence Lundblade825164e2020-10-22 20:18:06 -0700377 head of a CBOR data item because QCBOREncode_EncodeHead() needs
Michael Eckel5c531332020-03-02 01:35:30 +0100378 one extra byte to work.
379 */
380#define QCBOR_HEAD_BUFFER_SIZE (sizeof(uint64_t) + 2)
381
Michael Eckel5c531332020-03-02 01:35:30 +0100382
Laurence Lundblade9b334962020-08-27 10:55:53 -0700383/**
Laurence Lundblade825164e2020-10-22 20:18:06 -0700384 Output the full CBOR tag. See @ref CBORTags, @ref Tag-Usage and
385 @ref Tags-Overview.
Laurence Lundblade9b334962020-08-27 10:55:53 -0700386 */
387#define QCBOR_ENCODE_AS_TAG 0
388
389/**
Laurence Lundblade825164e2020-10-22 20:18:06 -0700390 Output only the 'borrowed' content format for the relevant tag.
391 See @ref CBORTags, @ref Tag-Usage and @ref Tags-Overview.
Laurence Lundblade9b334962020-08-27 10:55:53 -0700392 */
393#define QCBOR_ENCODE_AS_BORROWED 1
394
Michael Eckel5c531332020-03-02 01:35:30 +0100395
396/**
397 QCBOREncodeContext is the data type that holds context for all the
398 encoding functions. It is less than 200 bytes, so it can go on the
399 stack. The contents are opaque, and the caller should not access
400 internal members. A context may be re used serially as long as it is
401 re initialized.
402 */
403typedef struct _QCBOREncodeContext QCBOREncodeContext;
404
405
406/**
407 Initialize the encoder to prepare to encode some CBOR.
408
409 @param[in,out] pCtx The encoder context to initialize.
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800410 @param[in] Storage The buffer into which the encoded result
411 will be written.
Michael Eckel5c531332020-03-02 01:35:30 +0100412
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800413 Call this once at the start of an encoding of some CBOR. Then call
414 the many functions like QCBOREncode_AddInt64() and
415 QCBOREncode_AddText() to add the different data items. Finally, call
416 QCBOREncode_Finish() to get the pointer and length of the encoded
417 result.
Michael Eckel5c531332020-03-02 01:35:30 +0100418
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800419 The primary purpose of this function is to give the pointer and
420 length of the output buffer into which the encoded CBOR will be
421 written. This is done with a @ref UsefulBuf structure, which is just
422 a pointer and length (it is equivalent to two parameters, one a
423 pointer and one a length, but a little prettier).
Michael Eckel5c531332020-03-02 01:35:30 +0100424
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800425 The output buffer can be allocated any way (malloc, stack,
426 static). It is just some memory that QCBOR writes to. The length must
427 be the length of the allocated buffer. QCBOR will never write past
428 that length, but might write up to that length. If the buffer is too
429 small, encoding will go into an error state and not write anything
430 further.
431
432 If allocating on the stack the convenience macro
433 UsefulBuf_MAKE_STACK_UB() can be used, but its use is not required.
434
435 Since there is no reallocation or such, the output buffer must be
436 correctly sized when passed in here. It is OK, but wasteful if it is
437 too large. One way to pick the size is to figure out the maximum size
438 that will ever be needed and hard code a buffer of that size.
439
440 Another way to do it is to have QCBOR calculate it for you. To do
441 this set @c Storage.ptr to @c NULL and @c Storage.len to @c
442 UINT32_MAX. Then call all the functions to add the CBOR exactly as if
443 encoding for real. Then call QCBOREncode_Finish(). The pointer
444 returned will be @c NULL, but the length returned is that of what would
445 be encoded. Once the length is obtained, allocate a buffer of that
446 size, call QCBOREncode_Init() again with the real buffer. Call all
447 the add functions again and finally, QCBOREncode_Finish() to obtain
448 the final result. This uses almost twice the CPU time, but that is
449 usually not an issue.
450
451 See QCBOREncode_Finish() for how the pointer and length for the
452 encoded CBOR is returned.
453
454 The maximum output buffer size allowed is @c UINT32_MAX (4GB). The
455 error @ref QCBOR_ERR_BUFFER_TOO_LARGE will be returned by
456 QCBOREncode_Finish() if a larger buffer length is passed in.
Michael Eckel5c531332020-03-02 01:35:30 +0100457
458 A @ref QCBOREncodeContext can be reused over and over as long as
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800459 QCBOREncode_Init() is called before each use.
Michael Eckel5c531332020-03-02 01:35:30 +0100460 */
461void QCBOREncode_Init(QCBOREncodeContext *pCtx, UsefulBuf Storage);
462
463
464/**
465 @brief Add a signed 64-bit integer to the encoded output.
466
467 @param[in] pCtx The encoding context to add the integer to.
468 @param[in] nNum The integer to add.
469
470 The integer will be encoded and added to the CBOR output.
471
472 This function figures out the size and the sign and encodes in the
473 correct minimal CBOR. Specifically, it will select CBOR major type 0
474 or 1 based on sign and will encode to 1, 2, 4 or 8 bytes depending on
475 the value of the integer. Values less than 24 effectively encode to
476 one byte because they are encoded in with the CBOR major type. This
477 is a neat and efficient characteristic of CBOR that can be taken
478 advantage of when designing CBOR-based protocols. If integers like
479 tags can be kept between -23 and 23 they will be encoded in one byte
480 including the major type.
481
482 If you pass a smaller int, say an @c int16_t or a small value, say
483 100, the encoding will still be CBOR's most compact that can
484 represent the value. For example, CBOR always encodes the value 0 as
485 one byte, 0x00. The representation as 0x00 includes identification of
486 the type as an integer too as the major type for an integer is 0. See
Laurence Lundbladec02e13e2020-12-06 05:45:41 -0800487 [RFC 8949] (https://tools.ietf.org/html/rfc8949) Appendix A for more
488 examples of CBOR encoding. This compact encoding is also preferred
489 serialization CBOR as per section 34.1 in RFC 8949.
Michael Eckel5c531332020-03-02 01:35:30 +0100490
491 There are no functions to add @c int16_t or @c int32_t because they
492 are not necessary because this always encodes to the smallest number
493 of bytes based on the value (If this code is running on a 32-bit
494 machine having a way to add 32-bit integers would reduce code size
495 some).
496
497 If the encoding context is in an error state, this will do
498 nothing. If an error occurs when adding this integer, the internal
499 error flag will be set, and the error will be returned when
500 QCBOREncode_Finish() is called.
501
502 See also QCBOREncode_AddUInt64().
503 */
504void QCBOREncode_AddInt64(QCBOREncodeContext *pCtx, int64_t nNum);
505
506static void QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t uNum);
507
508static void QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum);
509
510
511/**
512 @brief Add an unsigned 64-bit integer to the encoded output.
513
514 @param[in] pCtx The encoding context to add the integer to.
515 @param[in] uNum The integer to add.
516
517 The integer will be encoded and added to the CBOR output.
518
519 The only reason so use this function is for integers larger than @c
520 INT64_MAX and smaller than @c UINT64_MAX. Otherwise
521 QCBOREncode_AddInt64() will work fine.
522
523 Error handling is the same as for QCBOREncode_AddInt64().
524 */
525void QCBOREncode_AddUInt64(QCBOREncodeContext *pCtx, uint64_t uNum);
526
527static void QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum);
528
529static void QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum);
530
531
532/**
533 @brief Add a UTF-8 text string to the encoded output.
534
535 @param[in] pCtx The encoding context to add the text to.
536 @param[in] Text Pointer and length of text to add.
537
538 The text passed in must be unencoded UTF-8 according to [RFC 3629]
539 (https://tools.ietf.org/html/rfc3629). There is no NULL
540 termination. The text is added as CBOR major type 3.
541
542 If called with @c nBytesLen equal to 0, an empty string will be
543 added. When @c nBytesLen is 0, @c pBytes may be @c NULL.
544
545 Note that the restriction of the buffer length to a @c uint32_t is
546 entirely intentional as this encoder is not capable of encoding
547 lengths greater. This limit to 4GB for a text string should not be a
548 problem.
549
Laurence Lundbladeadaf5c22020-09-25 10:37:09 -0700550 Text lines in Internet protocols (on the wire) are delimited by
551 either a CRLF or just an LF. Officially many protocols specify CRLF,
552 but implementations often work with either. CBOR type 3 text can be
553 either line ending, even a mixture of both.
554
555 Operating systems usually have a line end convention. Windows uses
556 CRLF. Linux and MacOS use LF. Some applications on a given OS may
557 work with either and some may not.
558
559 The majority of use cases and CBOR protocols using type 3 text will
560 work with either line ending. However, some use cases or protocols
561 may not work with either in which case translation to and/or from the
562 local line end convention, typically that of the OS, is necessary.
563
564 QCBOR does no line ending translation for type 3 text when encoding
565 and decoding.
566
Michael Eckel5c531332020-03-02 01:35:30 +0100567 Error handling is the same as QCBOREncode_AddInt64().
568 */
569static void QCBOREncode_AddText(QCBOREncodeContext *pCtx, UsefulBufC Text);
570
571static void QCBOREncode_AddTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Text);
572
573static void QCBOREncode_AddTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Text);
574
575
576/**
577 @brief Add a UTF-8 text string to the encoded output.
578
579 @param[in] pCtx The encoding context to add the text to.
580 @param[in] szString Null-terminated text to add.
581
582 This works the same as QCBOREncode_AddText().
583 */
584static void QCBOREncode_AddSZString(QCBOREncodeContext *pCtx, const char *szString);
585
586static void QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szString);
587
588static void QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szString);
589
590
591/**
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700592 @brief Add a double-precision floating-point number to the encoded output.
Michael Eckel5c531332020-03-02 01:35:30 +0100593
594 @param[in] pCtx The encoding context to add the double to.
595 @param[in] dNum The double-precision number to add.
596
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700597 This encodes and outputs a floating-point number. CBOR major type 7
598 is used.
Michael Eckel5c531332020-03-02 01:35:30 +0100599
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700600 This implements preferred serialization, selectively encoding the
601 double-precision floating-point number as either double-precision,
602 single-precision or half-precision. Infinity, NaN and 0 are always
603 encoded as half-precision. If no precision will be lost in the
604 conversion to half-precision, then it will be converted and
605 encoded. If not and no precision will be lost in conversion to
606 single-precision, then it will be converted and encoded. If not, then
607 no conversion is performed, and it encoded as a double-precision.
Michael Eckel5c531332020-03-02 01:35:30 +0100608
609 Half-precision floating-point numbers take up 2 bytes, half that of
610 single-precision, one quarter of double-precision
611
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700612 This automatically reduces the size of encoded CBOR, maybe even by
613 four if most of values are 0, infinity or NaN.
Michael Eckel5c531332020-03-02 01:35:30 +0100614
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700615 When decoded, QCBOR will usually return these values as
616 double-precision.
617
618 It is possible to disable this preferred serialization when compiling
619 QCBOR. In that case, this functions the same as
620 QCBOREncode_AddDoubleNoPreferred().
Michael Eckel5c531332020-03-02 01:35:30 +0100621
622 Error handling is the same as QCBOREncode_AddInt64().
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700623
624 See also QCBOREncode_AddDoubleNoPreferred(), QCBOREncode_AddFloat()
625 and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
Michael Eckel5c531332020-03-02 01:35:30 +0100626 */
627void QCBOREncode_AddDouble(QCBOREncodeContext *pCtx, double dNum);
628
629static void QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum);
630
631static void QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum);
632
633
634/**
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700635 @brief Add a single-precision floating-point number to the encoded output.
636
637 @param[in] pCtx The encoding context to add the double to.
638 @param[in] fNum The single-precision number to add.
639
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700640 This is identical to QCBOREncode_AddDouble() except the input is
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700641 single-precision.
642
643 See also QCBOREncode_AddDouble(), QCBOREncode_AddDoubleNoPreferred(),
644 and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
645*/
646void QCBOREncode_AddFloat(QCBOREncodeContext *pCtx, float fNum);
647
648static void QCBOREncode_AddFloatToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum);
649
650static void QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float dNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700651
652
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700653/**
654 @brief Add a double-precision floating-point number without preferred encoding.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700655
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700656 @param[in] pCtx The encoding context to add the double to.
657 @param[in] dNum The double-precision number to add.
658
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700659 This always outputs the number as a 64-bit double-precision.
660 Preferred serialization is not used.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700661
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700662 Error handling is the same as QCBOREncode_AddInt64().
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700663
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700664 See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
665 QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700666*/
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700667void QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pCtx, double dNum);
668
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700669static void QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum);
670
671static void QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum);
672
673
674/**
675 @brief Add a single-precision floating-point number without preferred encoding.
676
677 @param[in] pCtx The encoding context to add the double to.
678 @param[in] fNum The single-precision number to add.
679
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700680 This always outputs the number as a 32-bit single-precision.
681 Preferred serialization is not used.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700682
683 Error handling is the same as QCBOREncode_AddInt64().
684
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700685 See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
686 QCBOREncode_AddDoubleNoPreferred() and @ref Floating-Point.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700687*/
688void QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pCtx, float fNum);
689
690static void QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum);
691
692static void QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float fNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700693
694
Michael Eckel5c531332020-03-02 01:35:30 +0100695/**
696 @brief Add an optional tag.
697
698 @param[in] pCtx The encoding context to add the tag to.
699 @param[in] uTag The tag to add
700
701 This outputs a CBOR major type 6 item that tags the next data item
702 that is output usually to indicate it is some new data type.
703
704 For many of the common standard tags, a function to encode data using
705 it is provided and this is not needed. For example,
706 QCBOREncode_AddDateEpoch() already exists to output integers
707 representing dates with the right tag.
708
709 The tag is applied to the next data item added to the encoded
710 output. That data item that is to be tagged can be of any major CBOR
711 type. Any number of tags can be added to a data item by calling this
712 multiple times before the data item is added.
713
714 See @ref Tags-Overview for discussion of creating new non-standard
715 tags. See QCBORDecode_GetNext() for discussion of decoding custom
716 tags.
717*/
Laurence Lundblade6416a0f2020-09-19 23:26:34 -0700718void QCBOREncode_AddTag(QCBOREncodeContext *pCtx, uint64_t uTag);
Michael Eckel5c531332020-03-02 01:35:30 +0100719
720
721/**
722 @brief Add an epoch-based date.
723
724 @param[in] pCtx The encoding context to add the date to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700725 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700726 @param[in] nDate Number of seconds since 1970-01-01T00:00Z in UTC time.
Michael Eckel5c531332020-03-02 01:35:30 +0100727
Laurence Lundbladec02e13e2020-12-06 05:45:41 -0800728 As per RFC 8949 this is similar to UNIX/Linux/POSIX dates. This is
Michael Eckel5c531332020-03-02 01:35:30 +0100729 the most compact way to specify a date and time in CBOR. Note that
730 this is always UTC and does not include the time zone. Use
731 QCBOREncode_AddDateString() if you want to include the time zone.
732
733 The integer encoding rules apply here so the date will be encoded in
734 a minimal number of bytes. Until about the year 2106 these dates will
735 encode in 6 bytes -- one byte for the tag, one byte for the type and
736 4 bytes for the integer. After that it will encode to 10 bytes.
737
738 Negative values are supported for dates before 1970.
739
740 If you care about leap-seconds and that level of accuracy, make sure
741 the system you are running this code on does it correctly. This code
742 just takes the value passed in.
743
744 This implementation cannot encode fractional seconds using float or
745 double even though that is allowed by CBOR, but you can encode them
746 if you want to by calling QCBOREncode_AddDouble() and
747 QCBOREncode_AddTag().
748
749 Error handling is the same as QCBOREncode_AddInt64().
750 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700751static void QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pCtx,
752 uint8_t uTagRequirement,
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700753 int64_t nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -0700754
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700755static void QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pCtx,
756 const char *szLabel,
757 uint8_t uTagRequirement,
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700758 int64_t nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -0700759
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700760static void QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pCtx,
761 int64_t nLabel,
762 uint8_t uTagRequirement,
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700763 int64_t nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -0700764
765
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700766static void QCBOREncode_AddDateEpoch(QCBOREncodeContext *pCtx,
767 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100768
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700769static void QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pCtx,
770 const char *szLabel,
771 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100772
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700773static void QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pCtx,
774 int64_t nLabel,
775 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100776
777
778/**
779 @brief Add a byte string to the encoded output.
780
781 @param[in] pCtx The encoding context to add the bytes to.
782 @param[in] Bytes Pointer and length of the input data.
783
784 Simply adds the bytes to the encoded output as CBOR major type 2.
785
786 If called with @c Bytes.len equal to 0, an empty string will be
787 added. When @c Bytes.len is 0, @c Bytes.ptr may be @c NULL.
788
789 Error handling is the same as QCBOREncode_AddInt64().
790 */
791static void QCBOREncode_AddBytes(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
792
793static void QCBOREncode_AddBytesToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
794
795static void QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
796
797
Michael Eckel5c531332020-03-02 01:35:30 +0100798/**
799 @brief Add a binary UUID to the encoded output.
800
801 @param[in] pCtx The encoding context to add the UUID to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700802 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100803 @param[in] Bytes Pointer and length of the binary UUID.
804
805 A binary UUID as defined in [RFC 4122]
806 (https://tools.ietf.org/html/rfc4122) is added to the output.
807
808 It is output as CBOR major type 2, a binary string, with tag @ref
809 CBOR_TAG_BIN_UUID indicating the binary string is a UUID.
810 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700811static void QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pCtx,
812 uint8_t uTagRequirement,
813 UsefulBufC Bytes);
814
815static void QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pCtx,
816 const char *szLabel,
817 uint8_t uTagRequirement,
818 UsefulBufC Bytes);
819
820static void QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pCtx,
821 int64_t nLabel,
822 uint8_t uTagRequirement,
823 UsefulBufC Bytes);
824
825
Michael Eckel5c531332020-03-02 01:35:30 +0100826static void QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
827
828static void QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
829
830static void QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
831
832
833/**
834 @brief Add a positive big number to the encoded output.
835
836 @param[in] pCtx The encoding context to add the big number to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700837 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100838 @param[in] Bytes Pointer and length of the big number.
839
840 Big numbers are integers larger than 64-bits. Their format is
Laurence Lundbladec02e13e2020-12-06 05:45:41 -0800841 described in [RFC 8949] (https://tools.ietf.org/html/rfc8949).
Michael Eckel5c531332020-03-02 01:35:30 +0100842
843 It is output as CBOR major type 2, a binary string, with tag @ref
844 CBOR_TAG_POS_BIGNUM indicating the binary string is a positive big
845 number.
846
847 Often big numbers are used to represent cryptographic keys, however,
848 COSE which defines representations for keys chose not to use this
849 particular type.
850 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700851static void QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pCtx,
852 uint8_t uTagRequirement,
853 UsefulBufC Bytes);
854
855static void QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pCtx,
856 const char *szLabel,
857 uint8_t uTagRequirement,
858 UsefulBufC Bytes);
859
860static void QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pCtx,
861 int64_t nLabel,
862 uint8_t uTagRequirement,
863 UsefulBufC Bytes);
864
865
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700866static void QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx,
867 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100868
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700869static void QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx,
870 const char *szLabel,
871 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100872
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700873static void QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx,
874 int64_t nLabel,
875 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100876
877
878/**
879 @brief Add a negative big number to the encoded output.
880
881 @param[in] pCtx The encoding context to add the big number to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700882 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100883 @param[in] Bytes Pointer and length of the big number.
884
885 Big numbers are integers larger than 64-bits. Their format is
Laurence Lundbladec02e13e2020-12-06 05:45:41 -0800886 described in [RFC 8949] (https://tools.ietf.org/html/rfc8949).
Michael Eckel5c531332020-03-02 01:35:30 +0100887
888 It is output as CBOR major type 2, a binary string, with tag @ref
889 CBOR_TAG_NEG_BIGNUM indicating the binary string is a negative big
890 number.
891
892 Often big numbers are used to represent cryptographic keys, however,
893 COSE which defines representations for keys chose not to use this
894 particular type.
895 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700896static void QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pCtx,
897 uint8_t uTagRequirement,
898 UsefulBufC Bytes);
899
900static void QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pCtx,
901 const char *szLabel,
902 uint8_t uTagRequirement,
903 UsefulBufC Bytes);
904
905static void QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pCtx,
906 int64_t nLabel,
907 uint8_t uTagRequirement,
908 UsefulBufC Bytes);
909
910
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700911static void QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx,
912 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100913
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700914static void QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx,
915 const char *szLabel,
916 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100917
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700918static void QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx,
919 int64_t nLabel,
920 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100921
922
923#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
924/**
925 @brief Add a decimal fraction to the encoded output.
926
927 @param[in] pCtx The encoding context to add the decimal fraction to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700928 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100929 @param[in] nMantissa The mantissa.
930 @param[in] nBase10Exponent The exponent.
931
932 The value is nMantissa * 10 ^ nBase10Exponent.
933
934 A decimal fraction is good for exact representation of some values
935 that can't be represented exactly with standard C (IEEE 754)
936 floating-point numbers. Much larger and much smaller numbers can
937 also be represented than floating-point because of the larger number
938 of bits in the exponent.
939
940 The decimal fraction is conveyed as two integers, a mantissa and a
941 base-10 scaling factor.
942
943 For example, 273.15 is represented by the two integers 27315 and -2.
944
945 The exponent and mantissa have the range from @c INT64_MIN to
946 @c INT64_MAX for both encoding and decoding (CBOR allows @c -UINT64_MAX
947 to @c UINT64_MAX, but this implementation doesn't support this range to
948 reduce code size and interface complexity a little).
949
950 CBOR Preferred encoding of the integers is used, thus they will be encoded
951 in the smallest number of bytes possible.
952
953 See also QCBOREncode_AddDecimalFractionBigNum() for a decimal
954 fraction with arbitrarily large precision and QCBOREncode_AddBigFloat().
955
956 There is no representation of positive or negative infinity or NaN
957 (Not a Number). Use QCBOREncode_AddDouble() to encode them.
958
959 See @ref expAndMantissa for decoded representation.
960 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700961static void QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pCtx,
962 uint8_t uTagRequirement,
963 int64_t nMantissa,
964 int64_t nBase10Exponent);
965
966static void QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -0700967 const char *szLabel,
968 uint8_t uTagRequirement,
969 int64_t nMantissa,
970 int64_t nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700971
972static void QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pCtx,
973 int64_t nLabel,
974 uint8_t uTagRequirement,
975 int64_t nMantissa,
976 int64_t nBase10Exponent);
977
978
Michael Eckel5c531332020-03-02 01:35:30 +0100979static void QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pCtx,
980 int64_t nMantissa,
981 int64_t nBase10Exponent);
982
983static void QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx,
984 const char *szLabel,
985 int64_t nMantissa,
986 int64_t nBase10Exponent);
987
988static void QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pCtx,
989 int64_t nLabel,
990 int64_t nMantissa,
991 int64_t nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +0100992/**
993 @brief Add a decimal fraction with a big number mantissa to the encoded output.
994
995 @param[in] pCtx The encoding context to add the decimal fraction to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700996 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100997 @param[in] Mantissa The mantissa.
998 @param[in] bIsNegative false if mantissa is positive, true if negative.
999 @param[in] nBase10Exponent The exponent.
1000
1001 This is the same as QCBOREncode_AddDecimalFraction() except the
1002 mantissa is a big number (See QCBOREncode_AddPositiveBignum())
1003 allowing for arbitrarily large precision.
1004
1005 See @ref expAndMantissa for decoded representation.
1006 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001007static void QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pCtx,
1008 uint8_t uTagRequirement,
1009 UsefulBufC Mantissa,
1010 bool bIsNegative,
1011 int64_t nBase10Exponent);
1012
1013static void QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07001014 const char *szLabel,
1015 uint8_t uTagRequirement,
1016 UsefulBufC Mantissa,
1017 bool bIsNegative,
1018 int64_t nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001019
1020static void QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
1021 int64_t nLabel,
1022 uint8_t uTagRequirement,
1023 UsefulBufC Mantissa,
1024 bool bIsNegative,
1025 int64_t nBase10Exponent);
1026
1027
Michael Eckel5c531332020-03-02 01:35:30 +01001028static void QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pCtx,
1029 UsefulBufC Mantissa,
1030 bool bIsNegative,
1031 int64_t nBase10Exponent);
1032
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001033static void QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07001034 const char *szLabel,
1035 UsefulBufC Mantissa,
1036 bool bIsNegative,
1037 int64_t nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01001038
1039static void QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
1040 int64_t nLabel,
1041 UsefulBufC Mantissa,
1042 bool bIsNegative,
1043 int64_t nBase10Exponent);
1044
1045/**
1046 @brief Add a big floating-point number to the encoded output.
1047
1048 @param[in] pCtx The encoding context to add the bigfloat to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001049 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001050 @param[in] nMantissa The mantissa.
1051 @param[in] nBase2Exponent The exponent.
1052
1053 The value is nMantissa * 2 ^ nBase2Exponent.
1054
1055 "Bigfloats", as CBOR terms them, are similar to IEEE floating-point
1056 numbers in having a mantissa and base-2 exponent, but they are not
1057 supported by hardware or encoded the same. They explicitly use two
1058 CBOR-encoded integers to convey the mantissa and exponent, each of which
1059 can be 8, 16, 32 or 64 bits. With both the mantissa and exponent
1060 64 bits they can express more precision and a larger range than an
1061 IEEE double floating-point number. See
1062 QCBOREncode_AddBigFloatBigNum() for even more precision.
1063
1064 For example, 1.5 would be represented by a mantissa of 3 and an
1065 exponent of -1.
1066
1067 The exponent and mantissa have the range from @c INT64_MIN to
1068 @c INT64_MAX for both encoding and decoding (CBOR allows @c -UINT64_MAX
1069 to @c UINT64_MAX, but this implementation doesn't support this range to
1070 reduce code size and interface complexity a little).
1071
1072 CBOR Preferred encoding of the integers is used, thus they will be encoded
1073 in the smallest number of bytes possible.
1074
1075 This can also be used to represent floating-point numbers in
1076 environments that don't support IEEE 754.
1077
1078 See @ref expAndMantissa for decoded representation.
1079 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001080static void QCBOREncode_AddTBigFloat(QCBOREncodeContext *pCtx,
1081 uint8_t uTagRequirement,
1082 int64_t nMantissa,
1083 int64_t nBase2Exponent);
1084
1085static void QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07001086 const char *szLabel,
1087 uint8_t uTagRequirement,
1088 int64_t nMantissa,
1089 int64_t nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001090
1091static void QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pCtx,
1092 int64_t nLabel,
1093 uint8_t uTagRequirement,
1094 int64_t nMantissa,
1095 int64_t nBase2Exponent);
1096
1097
Michael Eckel5c531332020-03-02 01:35:30 +01001098static void QCBOREncode_AddBigFloat(QCBOREncodeContext *pCtx,
1099 int64_t nMantissa,
1100 int64_t nBase2Exponent);
1101
1102static void QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pCtx,
1103 const char *szLabel,
1104 int64_t nMantissa,
1105 int64_t nBase2Exponent);
1106
1107static void QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pCtx,
1108 int64_t nLabel,
1109 int64_t nMantissa,
1110 int64_t nBase2Exponent);
1111
Michael Eckel5c531332020-03-02 01:35:30 +01001112/**
1113 @brief Add a big floating-point number with a big number mantissa to
1114 the encoded output.
1115
1116 @param[in] pCtx The encoding context to add the bigfloat to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001117 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001118 @param[in] Mantissa The mantissa.
1119 @param[in] bIsNegative false if mantissa is positive, true if negative.
1120 @param[in] nBase2Exponent The exponent.
1121
1122 This is the same as QCBOREncode_AddBigFloat() except the mantissa is
1123 a big number (See QCBOREncode_AddPositiveBignum()) allowing for
1124 arbitrary precision.
1125
1126 See @ref expAndMantissa for decoded representation.
1127 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001128static void QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pCtx,
1129 uint8_t uTagRequirement,
1130 UsefulBufC Mantissa,
1131 bool bIsNegative,
1132 int64_t nBase2Exponent);
1133
1134static void QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07001135 const char *szLabel,
1136 uint8_t uTagRequirement,
1137 UsefulBufC Mantissa,
1138 bool bIsNegative,
1139 int64_t nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001140
1141static void QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
1142 int64_t nLabel,
1143 uint8_t uTagRequirement,
1144 UsefulBufC Mantissa,
1145 bool bIsNegative,
1146 int64_t nBase2Exponent);
1147
1148
Michael Eckel5c531332020-03-02 01:35:30 +01001149static void QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pCtx,
1150 UsefulBufC Mantissa,
1151 bool bIsNegative,
1152 int64_t nBase2Exponent);
1153
1154static void QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pCtx,
1155 const char *szLabel,
1156 UsefulBufC Mantissa,
1157 bool bIsNegative,
1158 int64_t nBase2Exponent);
1159
1160static void QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
1161 int64_t nLabel,
1162 UsefulBufC Mantissa,
1163 bool bIsNegative,
1164 int64_t nBase2Exponent);
1165#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
1166
1167
1168/**
1169 @brief Add a text URI to the encoded output.
1170
1171 @param[in] pCtx The encoding context to add the URI to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001172 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001173 @param[in] URI Pointer and length of the URI.
1174
1175 The format of URI must be per [RFC 3986]
1176 (https://tools.ietf.org/html/rfc3986).
1177
1178 It is output as CBOR major type 3, a text string, with tag @ref
1179 CBOR_TAG_URI indicating the text string is a URI.
1180
1181 A URI in a NULL-terminated string, @c szURI, can be easily added with
1182 this code:
1183
1184 QCBOREncode_AddURI(pCtx, UsefulBuf_FromSZ(szURI));
1185 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001186static void QCBOREncode_AddTURI(QCBOREncodeContext *pCtx,
1187 uint8_t uTagRequirement,
1188 UsefulBufC URI);
1189
1190static void QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pCtx,
1191 const char *szLabel,
1192 uint8_t uTagRequirement,
1193 UsefulBufC URI);
1194
1195static void QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pCtx,
1196 int64_t nLabel,
1197 uint8_t uTagRequirement,
1198 UsefulBufC URI);
1199
1200
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001201static void QCBOREncode_AddURI(QCBOREncodeContext *pCtx,
1202 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001203
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001204static void QCBOREncode_AddURIToMap(QCBOREncodeContext *pCtx,
1205 const char *szLabel,
1206 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001207
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001208static void QCBOREncode_AddURIToMapN(QCBOREncodeContext *pCtx,
1209 int64_t nLabel,
1210 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001211
1212
1213/**
1214 @brief Add Base64-encoded text to encoded output.
1215
1216 @param[in] pCtx The encoding context to add the base-64 text to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001217 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001218 @param[in] B64Text Pointer and length of the base-64 encoded text.
1219
1220 The text content is Base64 encoded data per [RFC 4648]
1221 (https://tools.ietf.org/html/rfc4648).
1222
1223 It is output as CBOR major type 3, a text string, with tag @ref
1224 CBOR_TAG_B64 indicating the text string is Base64 encoded.
1225 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001226static void QCBOREncode_AddTB64Text(QCBOREncodeContext *pCtx,
1227 uint8_t uTagRequirement,
1228 UsefulBufC B64Text);
1229
1230static void QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pCtx,
1231 const char *szLabel,
1232 uint8_t uTagRequirement,
1233 UsefulBufC B64Text);
1234
1235static void QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pCtx,
1236 int64_t nLabel,
1237 uint8_t uTagRequirement,
1238 UsefulBufC B64Text);
1239
1240
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001241static void QCBOREncode_AddB64Text(QCBOREncodeContext *pCtx,
1242 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001243
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001244static void QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pCtx,
1245 const char *szLabel,
1246 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001247
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001248static void QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pCtx,
1249 int64_t nLabel,
1250 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001251
1252
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001253
Michael Eckel5c531332020-03-02 01:35:30 +01001254/**
1255 @brief Add base64url encoded data to encoded output.
1256
1257 @param[in] pCtx The encoding context to add the base64url to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001258 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001259 @param[in] B64Text Pointer and length of the base64url encoded text.
1260
1261 The text content is base64URL encoded text as per [RFC 4648]
1262 (https://tools.ietf.org/html/rfc4648).
1263
1264 It is output as CBOR major type 3, a text string, with tag @ref
1265 CBOR_TAG_B64URL indicating the text string is a Base64url encoded.
1266 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001267static void QCBOREncode_AddTB64URLText(QCBOREncodeContext *pCtx,
1268 uint8_t uTagRequirement,
1269 UsefulBufC B64Text);
1270
1271static void QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pCtx,
1272 const char *szLabel,
1273 uint8_t uTagRequirement,
1274 UsefulBufC B64Text);
1275
1276static void QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pCtx,
1277 int64_t nLabel,
1278 uint8_t uTagRequirement,
1279 UsefulBufC B64Text);
1280
1281
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001282static void QCBOREncode_AddB64URLText(QCBOREncodeContext *pCtx,
1283 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001284
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001285static void QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pCtx,
1286 const char *szLabel,
1287 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001288
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001289static void QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pCtx,
1290 int64_t nLabel,
1291 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001292
1293
1294/**
1295 @brief Add Perl Compatible Regular Expression.
1296
1297 @param[in] pCtx The encoding context to add the regular expression to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001298 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001299 @param[in] Regex Pointer and length of the regular expression.
1300
1301 The text content is Perl Compatible Regular
1302 Expressions (PCRE) / JavaScript syntax [ECMA262].
1303
1304 It is output as CBOR major type 3, a text string, with tag @ref
1305 CBOR_TAG_REGEX indicating the text string is a regular expression.
1306 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001307static void QCBOREncode_AddTRegex(QCBOREncodeContext *pCtx,
1308 uint8_t uTagRequirement,
1309 UsefulBufC Regex);
1310
1311static void QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pCtx,
1312 const char *szLabel,
1313 uint8_t uTagRequirement,
1314 UsefulBufC Regex);
1315
1316static void QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pCtx,
1317 int64_t nLabel,
1318 uint8_t uTagRequirement,
1319 UsefulBufC Regex);
1320
1321
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001322static void QCBOREncode_AddRegex(QCBOREncodeContext *pCtx,
1323 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001324
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001325static void QCBOREncode_AddRegexToMap(QCBOREncodeContext *pCtx,
1326 const char *szLabel,
1327 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001328
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001329static void QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pCtx,
1330 int64_t nLabel,
1331 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001332
1333
1334/**
Laurence Lundblade4982f412020-09-18 23:02:18 -07001335 @brief MIME encoded data to the encoded output.
Michael Eckel5c531332020-03-02 01:35:30 +01001336
Laurence Lundblade4982f412020-09-18 23:02:18 -07001337 @param[in] pCtx The encoding context to add the MIME data to.
1338 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1339 @ref QCBOR_ENCODE_AS_BORROWED.
1340 @param[in] MIMEData Pointer and length of the MIME data.
Michael Eckel5c531332020-03-02 01:35:30 +01001341
1342 The text content is in MIME format per [RFC 2045]
Laurence Lundblade4982f412020-09-18 23:02:18 -07001343 (https://tools.ietf.org/html/rfc2045) including the headers.
Michael Eckel5c531332020-03-02 01:35:30 +01001344
Laurence Lundblade4982f412020-09-18 23:02:18 -07001345 It is output as CBOR major type 2, a binary string, with tag @ref
1346 CBOR_TAG_BINARY_MIME indicating the string is MIME data. This
1347 outputs tag 257, not tag 36, as it can carry any type of MIME binary,
1348 7-bit, 8-bit, quoted-printable and base64 where tag 36 cannot.
1349
1350 Previous versions of QCBOR, those before spiffy decode, output tag
1351 36. Decoding supports both tag 36 and 257. (if the old behavior with
1352 tag 36 is needed, copy the inline functions below and change the tag
1353 number).
1354
1355 See also QCBORDecode_GetMIMEMessage() and
1356 @ref QCBOR_TYPE_BINARY_MIME.
Laurence Lundbladeadaf5c22020-09-25 10:37:09 -07001357
1358 This does no translation of line endings. See QCBOREncode_AddText()
1359 for a discussion of line endings in CBOR.
Michael Eckel5c531332020-03-02 01:35:30 +01001360 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001361static void QCBOREncode_AddTMIMEData(QCBOREncodeContext *pCtx,
1362 uint8_t uTagRequirement,
1363 UsefulBufC MIMEData);
1364
1365static void QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pCtx,
1366 const char *szLabel,
1367 uint8_t uTagRequirement,
1368 UsefulBufC MIMEData);
1369
1370static void QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pCtx,
1371 int64_t nLabel,
1372 uint8_t uTagRequirement,
1373 UsefulBufC MIMEData);
1374
1375
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001376static void QCBOREncode_AddMIMEData(QCBOREncodeContext *pCtx,
1377 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001378
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001379static void QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pCtx,
1380 const char *szLabel,
1381 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001382
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001383static void QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pCtx,
1384 int64_t nLabel,
1385 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001386
1387
1388/**
1389 @brief Add an RFC 3339 date string
1390
1391 @param[in] pCtx The encoding context to add the date to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001392 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001393 @param[in] szDate Null-terminated string with date to add.
1394
1395 The string szDate should be in the form of [RFC 3339]
1396 (https://tools.ietf.org/html/rfc3339) as defined by section 3.3 in
1397 [RFC 4287] (https://tools.ietf.org/html/rfc4287). This is as
Laurence Lundbladec02e13e2020-12-06 05:45:41 -08001398 described in section 3.4.1 in [RFC 8949]
1399 (https://tools.ietf.org/html/rfc8949).
Michael Eckel5c531332020-03-02 01:35:30 +01001400
1401 Note that this function doesn't validate the format of the date string
1402 at all. If you add an incorrect format date string, the generated
1403 CBOR will be incorrect and the receiver may not be able to handle it.
1404
1405 Error handling is the same as QCBOREncode_AddInt64().
1406 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001407static void QCBOREncode_AddTDateString(QCBOREncodeContext *pCtx,
1408 uint8_t uTagRequirement,
1409 const char *szDate);
1410
1411static void QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pCtx,
1412 const char *szLabel,
1413 uint8_t uTagRequirement,
1414 const char *szDate);
1415
1416static void QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pCtx,
1417 int64_t nLabel,
1418 uint8_t uTagRequirement,
1419 const char *szDate);
1420
1421
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001422static void QCBOREncode_AddDateString(QCBOREncodeContext *pCtx,
1423 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001424
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001425static void QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pCtx,
1426 const char *szLabel,
1427 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001428
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001429static void QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pCtx,
1430 int64_t nLabel,
1431 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001432
Michael Eckel5c531332020-03-02 01:35:30 +01001433/**
1434 @brief Add a standard Boolean.
1435
1436 @param[in] pCtx The encoding context to add the Boolean to.
1437 @param[in] b true or false from @c <stdbool.h>.
1438
1439 Adds a Boolean value as CBOR major type 7.
1440
1441 Error handling is the same as QCBOREncode_AddInt64().
1442 */
1443static void QCBOREncode_AddBool(QCBOREncodeContext *pCtx, bool b);
1444
1445static void QCBOREncode_AddBoolToMap(QCBOREncodeContext *pCtx, const char *szLabel, bool b);
1446
1447static void QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, bool b);
1448
1449
1450
1451/**
1452 @brief Add a NULL to the encoded output.
1453
1454 @param[in] pCtx The encoding context to add the NULL to.
1455
1456 Adds the NULL value as CBOR major type 7.
1457
1458 This NULL doesn't have any special meaning in CBOR such as a
1459 terminating value for a string or an empty value.
1460
1461 Error handling is the same as QCBOREncode_AddInt64().
1462 */
1463static void QCBOREncode_AddNULL(QCBOREncodeContext *pCtx);
1464
1465static void QCBOREncode_AddNULLToMap(QCBOREncodeContext *pCtx, const char *szLabel);
1466
1467static void QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1468
1469
1470/**
1471 @brief Add an "undef" to the encoded output.
1472
1473 @param[in] pCtx The encoding context to add the "undef" to.
1474
1475 Adds the undef value as CBOR major type 7.
1476
1477 Note that this value will not translate to JSON.
1478
1479 This Undef doesn't have any special meaning in CBOR such as a
1480 terminating value for a string or an empty value.
1481
1482 Error handling is the same as QCBOREncode_AddInt64().
1483 */
1484static void QCBOREncode_AddUndef(QCBOREncodeContext *pCtx);
1485
1486static void QCBOREncode_AddUndefToMap(QCBOREncodeContext *pCtx, const char *szLabel);
1487
1488static void QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1489
1490
1491/**
1492 @brief Indicates that the next items added are in an array.
1493
1494 @param[in] pCtx The encoding context to open the array in.
1495
1496 Arrays are the basic CBOR aggregate or structure type. Call this
1497 function to start or open an array. Then call the various @c
1498 QCBOREncode_AddXxx() functions to add the items that go into the
1499 array. Then call QCBOREncode_CloseArray() when all items have been
1500 added. The data items in the array can be of any type and can be of
1501 mixed types.
1502
1503 Nesting of arrays and maps is allowed and supported just by calling
1504 QCBOREncode_OpenArray() again before calling
1505 QCBOREncode_CloseArray(). While CBOR has no limit on nesting, this
1506 implementation does in order to keep it smaller and simpler. The
1507 limit is @ref QCBOR_MAX_ARRAY_NESTING. This is the max number of
1508 times this can be called without calling
1509 QCBOREncode_CloseArray(). QCBOREncode_Finish() will return @ref
1510 QCBOR_ERR_ARRAY_NESTING_TOO_DEEP when it is called as this function
1511 just sets an error state and returns no value when this occurs.
1512
1513 If you try to add more than @ref QCBOR_MAX_ITEMS_IN_ARRAY items to a
1514 single array or map, @ref QCBOR_ERR_ARRAY_TOO_LONG will be returned
1515 when QCBOREncode_Finish() is called.
1516
1517 An array itself must have a label if it is being added to a map.
1518 Note that array elements do not have labels (but map elements do).
1519
1520 An array itself may be tagged by calling QCBOREncode_AddTag() before this call.
1521 */
1522static void QCBOREncode_OpenArray(QCBOREncodeContext *pCtx);
1523
1524static void QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pCtx, const char *szLabel);
1525
1526static void QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1527
1528
1529/**
1530 @brief Close an open array.
1531
1532 @param[in] pCtx The encoding context to close the array in.
1533
1534 The closes an array opened by QCBOREncode_OpenArray(). It reduces
1535 nesting level by one. All arrays (and maps) must be closed before
1536 calling QCBOREncode_Finish().
1537
1538 When an error occurs as a result of this call, the encoder records
1539 the error and enters the error state. The error will be returned when
1540 QCBOREncode_Finish() is called.
1541
1542 If this has been called more times than QCBOREncode_OpenArray(), then
1543 @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when QCBOREncode_Finish()
1544 is called.
1545
1546 If this is called and it is not an array that is currently open, @ref
1547 QCBOR_ERR_CLOSE_MISMATCH will be returned when QCBOREncode_Finish()
1548 is called.
1549 */
1550static void QCBOREncode_CloseArray(QCBOREncodeContext *pCtx);
1551
1552
1553/**
1554 @brief Indicates that the next items added are in a map.
1555
1556 @param[in] pCtx The encoding context to open the map in.
1557
1558 See QCBOREncode_OpenArray() for more information, particularly error
1559 handling.
1560
1561 CBOR maps are an aggregate type where each item in the map consists
1562 of a label and a value. They are similar to JSON objects.
1563
1564 The value can be any CBOR type including another map.
1565
1566 The label can also be any CBOR type, but in practice they are
1567 typically, integers as this gives the most compact output. They might
1568 also be text strings which gives readability and translation to JSON.
1569
1570 Every @c QCBOREncode_AddXxx() call has one version that ends with @c
1571 InMap for adding items to maps with string labels and one that ends
1572 with @c InMapN that is for adding with integer labels.
1573
Laurence Lundbladec02e13e2020-12-06 05:45:41 -08001574 RFC 8949 uses the term "key" instead of "label".
Michael Eckel5c531332020-03-02 01:35:30 +01001575
1576 If you wish to use map labels that are neither integer labels nor
1577 text strings, then just call the QCBOREncode_AddXxx() function
1578 explicitly to add the label. Then call it again to add the value.
1579
Laurence Lundbladec02e13e2020-12-06 05:45:41 -08001580 See the [RFC 8949] (https://tools.ietf.org/html/rfc8949) for a lot
Michael Eckel5c531332020-03-02 01:35:30 +01001581 more information on creating maps.
1582 */
1583static void QCBOREncode_OpenMap(QCBOREncodeContext *pCtx);
1584
1585static void QCBOREncode_OpenMapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
1586
1587static void QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1588
1589
Michael Eckel5c531332020-03-02 01:35:30 +01001590/**
1591 @brief Close an open map.
1592
1593 @param[in] pCtx The encoding context to close the map in .
1594
1595 This closes a map opened by QCBOREncode_OpenMap(). It reduces nesting
1596 level by one.
1597
1598 When an error occurs as a result of this call, the encoder records
1599 the error and enters the error state. The error will be returned when
1600 QCBOREncode_Finish() is called.
1601
1602 If this has been called more times than QCBOREncode_OpenMap(),
1603 then @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when
1604 QCBOREncode_Finish() is called.
1605
1606 If this is called and it is not a map that is currently open, @ref
1607 QCBOR_ERR_CLOSE_MISMATCH will be returned when QCBOREncode_Finish()
1608 is called.
1609 */
1610static void QCBOREncode_CloseMap(QCBOREncodeContext *pCtx);
1611
1612
1613/**
1614 @brief Indicate start of encoded CBOR to be wrapped in a bstr.
1615
1616 @param[in] pCtx The encoding context to open the bstr-wrapped CBOR in.
1617
1618 All added encoded items between this call and a call to
1619 QCBOREncode_CloseBstrWrap2() will be wrapped in a bstr. They will
1620 appear in the final output as a byte string. That byte string will
1621 contain encoded CBOR. This increases nesting level by one.
1622
1623 The typical use case is for encoded CBOR that is to be
1624 cryptographically hashed, as part of a [RFC 8152, COSE]
Laurence Lundbladec02e13e2020-12-06 05:45:41 -08001625 (https://tools.ietf.org/html/rfc8152) implementation. The
1626 wrapping byte string is taken as input by the hash function
1627 (which is why it is returned by QCBOREncode_CloseBstrWrap2()).
1628 It is also easy to recover on decoding with standard CBOR
1629 decoders.
Michael Eckel5c531332020-03-02 01:35:30 +01001630
1631 Using QCBOREncode_BstrWrap() and QCBOREncode_CloseBstrWrap2() avoids
1632 having to encode the items first in one buffer (e.g., the COSE
1633 payload) and then add that buffer as a bstr to another encoding
1634 (e.g. the COSE to-be-signed bytes, the @c Sig_structure) potentially
1635 halving the memory needed.
1636
Laurence Lundbladec02e13e2020-12-06 05:45:41 -08001637 CBOR by nature must be decoded item by item in order from the start.
1638 By wrapping some CBOR in a byte string, the decoding of that wrapped
1639 CBOR can be skipped. This is another use of wrapping, perhaps
1640 because the CBOR is large and deeply nested. Perhaps APIs for
1641 handling one defined CBOR message that is being embedded in another
1642 only take input as a byte string. Perhaps the desire is to be able
1643 to decode the out layer even in the wrapped has errors.
Michael Eckel5c531332020-03-02 01:35:30 +01001644 */
1645static void QCBOREncode_BstrWrap(QCBOREncodeContext *pCtx);
1646
1647static void QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
1648
1649static void QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1650
1651
1652/**
1653 @brief Close a wrapping bstr.
1654
1655 @param[in] pCtx The encoding context to close of bstr wrapping in.
1656 @param[in] bIncludeCBORHead Include the encoded CBOR head of the bstr
1657 as well as the bytes in @c pWrappedCBOR.
1658 @param[out] pWrappedCBOR A @ref UsefulBufC containing wrapped bytes.
1659
1660 The closes a wrapping bstr opened by QCBOREncode_BstrWrap(). It reduces
1661 nesting level by one.
1662
1663 A pointer and length of the enclosed encoded CBOR is returned in @c
1664 *pWrappedCBOR if it is not @c NULL. The main purpose of this is so
1665 this data can be hashed (e.g., with SHA-256) as part of a [RFC 8152,
1666 COSE] (https://tools.ietf.org/html/rfc8152)
1667 implementation. **WARNING**, this pointer and length should be used
1668 right away before any other calls to @c QCBOREncode_CloseXxx() as
1669 they will move data around and the pointer and length will no longer
1670 be to the correct encoded CBOR.
1671
1672 When an error occurs as a result of this call, the encoder records
1673 the error and enters the error state. The error will be returned when
1674 QCBOREncode_Finish() is called.
1675
1676 If this has been called more times than QCBOREncode_BstrWrap(), then
1677 @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when
1678 QCBOREncode_Finish() is called.
1679
1680 If this is called and it is not a wrapping bstr that is currently
1681 open, @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when
1682 QCBOREncode_Finish() is called.
1683
1684 QCBOREncode_CloseBstrWrap() is a deprecated version of this function
1685 that is equivalent to the call with @c bIncludeCBORHead @c true.
1686 */
1687void QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *pCtx, bool bIncludeCBORHead, UsefulBufC *pWrappedCBOR);
1688
1689static void QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pCtx, UsefulBufC *pWrappedCBOR);
1690
1691
1692/**
1693 @brief Add some already-encoded CBOR bytes.
1694
1695 @param[in] pCtx The encoding context to add the already-encode CBOR to.
1696 @param[in] Encoded The already-encoded CBOR to add to the context.
1697
1698 The encoded CBOR being added must be fully conforming CBOR. It must
1699 be complete with no arrays or maps that are incomplete. While this
1700 encoder doesn't ever produce indefinite lengths, it is OK for the
1701 raw CBOR added here to have indefinite lengths.
1702
1703 The raw CBOR added here is not checked in anyway. If it is not
1704 conforming or has open arrays or such, the final encoded CBOR
1705 will probably be wrong or not what was intended.
1706
1707 If the encoded CBOR being added here contains multiple items, they
1708 must be enclosed in a map or array. At the top level the raw
1709 CBOR must be a single data item.
1710 */
1711static void QCBOREncode_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded);
1712
1713static void QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Encoded);
1714
1715static void QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Encoded);
1716
1717
1718/**
1719 @brief Get the encoded result.
1720
1721 @param[in] pCtx The context to finish encoding with.
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08001722 @param[out] pEncodedCBOR Structure in which the pointer and length of the encoded
1723 CBOR is returned.
Michael Eckel5c531332020-03-02 01:35:30 +01001724
1725 @retval QCBOR_ERR_TOO_MANY_CLOSES Nesting error
1726
1727 @retval QCBOR_ERR_CLOSE_MISMATCH Nesting error
1728
1729 @retval QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN Nesting error
1730
1731 @retval QCBOR_ERR_BUFFER_TOO_LARGE Encoded output buffer size
1732
1733 @retval QCBOR_ERR_BUFFER_TOO_SMALL Encoded output buffer size
1734
1735 @retval QCBOR_ERR_ARRAY_NESTING_TOO_DEEP Implementation limit
1736
1737 @retval QCBOR_ERR_ARRAY_TOO_LONG Implementation limit
1738
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08001739 On success, the pointer and length of the encoded CBOR are returned
1740 in @c *pEncodedCBOR. The pointer is the same pointer that was passed
1741 in to QCBOREncode_Init(). Note that it is not const when passed to
1742 QCBOREncode_Init(), but it is const when returned here. The length
1743 will be smaller than or equal to the length passed in when
1744 QCBOREncode_Init() as this is the length of the actual result, not
1745 the size of the buffer it was written to.
Michael Eckel5c531332020-03-02 01:35:30 +01001746
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08001747 If a @c NULL was passed for @c Storage.ptr when QCBOREncode_Init()
1748 was called, @c NULL will be returned here, but the length will be
1749 that of the CBOR that would have been encoded.
Michael Eckel5c531332020-03-02 01:35:30 +01001750
1751 Encoding errors primarily manifest here as most other encoding function
1752 do no return an error. They just set the error state in the encode
1753 context after which no encoding function does anything.
1754
1755 Three types of errors manifest here. The first type are nesting
1756 errors where the number of @c QCBOREncode_OpenXxx() calls do not
1757 match the number @c QCBOREncode_CloseXxx() calls. The solution is to
1758 fix the calling code.
1759
1760 The second type of error is because the buffer given is either too
1761 small or too large. The remedy is to give a correctly sized buffer.
1762
1763 The third type are due to limits in this implementation. @ref
1764 QCBOR_ERR_ARRAY_NESTING_TOO_DEEP can be worked around by encoding the
1765 CBOR in two (or more) phases and adding the CBOR from the first phase
1766 to the second with @c QCBOREncode_AddEncoded().
1767
1768 If an error is returned, the buffer may have partially encoded
1769 incorrect CBOR in it and it should not be used. Likewise, the length
1770 may be incorrect and should not be used.
1771
1772 Note that the error could have occurred in one of the many @c
1773 QCBOREncode_AddXxx() calls long before QCBOREncode_Finish() was
1774 called. This error handling reduces the CBOR implementation size but
1775 makes debugging harder.
1776
1777 This may be called multiple times. It will always return the same. It
1778 can also be interleaved with calls to QCBOREncode_FinishGetSize().
1779
1780 QCBOREncode_GetErrorState() can be called to get the current
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08001781 error state in order to abort encoding early as an optimization, but
1782 calling it is is never required.
Michael Eckel5c531332020-03-02 01:35:30 +01001783 */
1784QCBORError QCBOREncode_Finish(QCBOREncodeContext *pCtx, UsefulBufC *pEncodedCBOR);
1785
1786
1787/**
1788 @brief Get the encoded CBOR and error status.
1789
1790 @param[in] pCtx The context to finish encoding with.
1791 @param[out] uEncodedLen The length of the encoded or potentially
1792 encoded CBOR in bytes.
1793
1794 @return The same errors as QCBOREncode_Finish().
1795
1796 This functions the same as QCBOREncode_Finish(), but only returns the
1797 size of the encoded output.
1798 */
1799QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *pCtx, size_t *uEncodedLen);
1800
1801
1802/**
1803 @brief Indicate whether output buffer is NULL or not.
1804
1805 @param[in] pCtx The encoding context.
1806
1807 @return 1 if the output buffer is @c NULL.
1808
1809 Sometimes a @c NULL input buffer is given to QCBOREncode_Init() so
1810 that the size of the generated CBOR can be calculated without
1811 allocating a buffer for it. This returns 1 when the output buffer is
1812 NULL and 0 when it is not.
1813*/
1814static int QCBOREncode_IsBufferNULL(QCBOREncodeContext *pCtx);
1815
Laurence Lundblade825164e2020-10-22 20:18:06 -07001816
1817/**
Michael Eckel5c531332020-03-02 01:35:30 +01001818 @brief Get the encoding error state.
1819
1820 @param[in] pCtx The encoding context.
1821
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07001822 @return One of @ref QCBORError. See return values from
Michael Eckel5c531332020-03-02 01:35:30 +01001823 QCBOREncode_Finish()
1824
1825 Normally encoding errors need only be handled at the end of encoding
1826 when QCBOREncode_Finish() is called. This can be called to get the
1827 error result before finish should there be a need to halt encoding
1828 before QCBOREncode_Finish() is called.
1829*/
1830static QCBORError QCBOREncode_GetErrorState(QCBOREncodeContext *pCtx);
1831
1832
1833/**
1834 Encode the "head" of a CBOR data item.
1835
1836 @param buffer Buffer to output the encoded head to; must be
1837 @ref QCBOR_HEAD_BUFFER_SIZE bytes in size.
1838 @param uMajorType One of CBOR_MAJOR_TYPE_XX.
1839 @param uMinLen The minimum number of bytes to encode uNumber. Almost always
1840 this is 0 to use preferred minimal encoding. If this is 4,
1841 then even the values 0xffff and smaller will be encoded
Laurence Lundblade98427e92020-09-28 21:33:23 -07001842 in 4 bytes. This is used primarily when encoding a
Michael Eckel5c531332020-03-02 01:35:30 +01001843 float or double put into uNumber as the leading zero bytes
1844 for them must be encoded.
1845 @param uNumber The numeric argument part of the CBOR head.
1846 @return Pointer and length of the encoded head or
Laurence Lundblade9a3b6252020-10-21 21:30:31 -07001847 @ref NULLUsefulBufC if the output buffer is too small.
Michael Eckel5c531332020-03-02 01:35:30 +01001848
Laurence Lundblade98427e92020-09-28 21:33:23 -07001849 Callers do not to need to call this for normal CBOR encoding. Note that it doesn't even
Michael Eckel5c531332020-03-02 01:35:30 +01001850 take a @ref QCBOREncodeContext argument.
1851
1852 This encodes the major type and argument part of a data item. The
1853 argument is an integer that is usually either the value or the length
1854 of the data item.
1855
1856 This is exposed in the public interface to allow hashing of some CBOR
1857 data types, bstr in particular, a chunk at a time so the full CBOR
1858 doesn't have to be encoded in a contiguous buffer.
1859
1860 For example, if you have a 100,000 byte binary blob in a buffer that
1861 needs to be a bstr encoded and then hashed. You could allocate a
1862 100,010 byte buffer and encode it normally. Alternatively, you can
1863 encode the head in a 10 byte buffer with this function, hash that and
1864 then hash the 100,000 bytes using the same hash context.
1865
1866 See also QCBOREncode_AddBytesLenOnly();
1867 */
1868UsefulBufC QCBOREncode_EncodeHead(UsefulBuf buffer,
1869 uint8_t uMajorType,
1870 uint8_t uMinLen,
1871 uint64_t uNumber);
1872
1873
Michael Eckel5c531332020-03-02 01:35:30 +01001874
1875
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001876/* =========================================================================
1877 BEGINNING OF PRIVATE INLINE IMPLEMENTATION
1878 ========================================================================= */
Michael Eckel5c531332020-03-02 01:35:30 +01001879
1880/**
1881 @brief Semi-private method to add a buffer full of bytes to encoded output
1882
1883 @param[in] pCtx The encoding context to add the integer to.
1884 @param[in] uMajorType The CBOR major type of the bytes.
1885 @param[in] Bytes The bytes to add.
1886
1887 Use QCBOREncode_AddText() or QCBOREncode_AddBytes() or
1888 QCBOREncode_AddEncoded() instead. They are inline functions that call
1889 this and supply the correct major type. This function is public to
1890 make the inline functions work to keep the overall code size down and
1891 because the C language has no way to make it private.
1892
1893 If this is called the major type should be @c
1894 CBOR_MAJOR_TYPE_TEXT_STRING, @c CBOR_MAJOR_TYPE_BYTE_STRING or @c
1895 CBOR_MAJOR_NONE_TYPE_RAW. The last one is special for adding
1896 already-encoded CBOR.
1897 */
1898void QCBOREncode_AddBuffer(QCBOREncodeContext *pCtx, uint8_t uMajorType, UsefulBufC Bytes);
1899
1900
1901/**
1902 @brief Semi-private method to open a map, array or bstr-wrapped CBOR
1903
1904 @param[in] pCtx The context to add to.
1905 @param[in] uMajorType The major CBOR type to close
1906
1907 Call QCBOREncode_OpenArray(), QCBOREncode_OpenMap() or
1908 QCBOREncode_BstrWrap() instead of this.
1909 */
1910void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *pCtx, uint8_t uMajorType);
1911
1912
1913/**
1914 @brief Semi-private method to open a map, array with indefinite length
1915
1916 @param[in] pCtx The context to add to.
1917 @param[in] uMajorType The major CBOR type to close
1918
1919 Call QCBOREncode_OpenArrayIndefiniteLength() or
1920 QCBOREncode_OpenMapIndefiniteLength() instead of this.
1921 */
1922void QCBOREncode_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx, uint8_t uMajorType);
1923
1924
1925/**
1926 @brief Semi-private method to close a map, array or bstr wrapped CBOR
1927
1928 @param[in] pCtx The context to add to.
1929 @param[in] uMajorType The major CBOR type to close.
1930
1931 Call QCBOREncode_CloseArray() or QCBOREncode_CloseMap() instead of this.
1932 */
1933void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *pCtx, uint8_t uMajorType);
1934
1935
1936/**
1937 @brief Semi-private method to close a map, array with indefinite length
1938
1939 @param[in] pCtx The context to add to.
1940 @param[in] uMajorType The major CBOR type to close.
1941
1942 Call QCBOREncode_CloseArrayIndefiniteLength() or
1943 QCBOREncode_CloseMapIndefiniteLength() instead of this.
1944 */
1945void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx,
1946 uint8_t uMajorType);
1947
1948
1949/**
1950 @brief Semi-private method to add simple types.
1951
1952 @param[in] pCtx The encoding context to add the simple value to.
1953 @param[in] uMinLen Minimum encoding size for uNum. Usually 0.
1954 @param[in] uNum One of CBOR_SIMPLEV_FALSE through _UNDEF or other.
1955
1956 This is used to add simple types like true and false.
1957
1958 Call QCBOREncode_AddBool(), QCBOREncode_AddNULL(),
1959 QCBOREncode_AddUndef() instead of this.
1960
1961 This function can add simple values that are not defined by CBOR
1962 yet. This expansion point in CBOR should not be used unless they are
1963 standardized.
1964
1965 Error handling is the same as QCBOREncode_AddInt64().
1966 */
1967void QCBOREncode_AddType7(QCBOREncodeContext *pCtx, uint8_t uMinLen, uint64_t uNum);
1968
1969
1970/**
1971 @brief Semi-private method to add bigfloats and decimal fractions.
1972
Laurence Lundblade1fa579b2020-11-25 00:31:37 -08001973 @param[in] pCtx The encoding context to add the value to.
1974 @param[in] uTag The type 6 tag indicating what this is to be.
1975 @param[in] BigNumMantissa Is @ref NULLUsefulBufC if mantissa is an
1976 @c int64_t or the actual big number mantissa
1977 if not.
1978 @param[in] bBigNumIsNegative This is @c true if the big number is negative.
1979 @param[in] nMantissa The @c int64_t mantissa if it is not a big number.
1980 @param[in] nExponent The exponent.
Michael Eckel5c531332020-03-02 01:35:30 +01001981
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001982 This outputs either the @ref CBOR_TAG_DECIMAL_FRACTION or @ref
1983 CBOR_TAG_BIGFLOAT tag. if @c uTag is @ref CBOR_TAG_INVALID64, then
1984 this outputs the "borrowed" content format.
Michael Eckel5c531332020-03-02 01:35:30 +01001985
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001986 The tag content output by this is an array with two members, the
1987 exponent and then the mantissa. The mantissa can be either a big
1988 number or an @c int64_t.
1989
1990 This implementation cannot output an exponent further from 0 than
Laurence Lundblade9a3b6252020-10-21 21:30:31 -07001991 @c INT64_MAX.
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001992
Laurence Lundblade1fa579b2020-11-25 00:31:37 -08001993 To output a mantissa that is between INT64_MAX and UINT64_MAX from 0,
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001994 it must be as a big number.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001995
Michael Eckel5c531332020-03-02 01:35:30 +01001996 Typically, QCBOREncode_AddDecimalFraction(), QCBOREncode_AddBigFloat(),
1997 QCBOREncode_AddDecimalFractionBigNum() or QCBOREncode_AddBigFloatBigNum()
1998 is called instead of this.
1999 */
2000void QCBOREncode_AddExponentAndMantissa(QCBOREncodeContext *pCtx,
2001 uint64_t uTag,
2002 UsefulBufC BigNumMantissa,
2003 bool bBigNumIsNegative,
2004 int64_t nMantissa,
2005 int64_t nExponent);
2006
2007/**
2008 @brief Semi-private method to add only the type and length of a byte string.
2009
2010 @param[in] pCtx The context to initialize.
2011 @param[in] Bytes Pointer and length of the input data.
2012
2013 This is the same as QCBOREncode_AddBytes() except it only adds the
2014 CBOR encoding for the type and the length. It doesn't actually add
2015 the bytes. You can't actually produce correct CBOR with this and the
2016 rest of this API. It is only used for a special case where
2017 the valid CBOR is created manually by putting this type and length in
2018 and then adding the actual bytes. In particular, when only a hash of
2019 the encoded CBOR is needed, where the type and header are hashed
2020 separately and then the bytes is hashed. This makes it possible to
2021 implement COSE Sign1 with only one copy of the payload in the output
2022 buffer, rather than two, roughly cutting memory use in half.
2023
2024 This is only used for this odd case, but this is a supported
2025 tested function.
2026
2027 See also QCBOREncode_EncodeHead().
2028*/
2029static inline void QCBOREncode_AddBytesLenOnly(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
2030
2031static inline void QCBOREncode_AddBytesLenOnlyToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
2032
2033static inline void QCBOREncode_AddBytesLenOnlyToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
2034
2035
2036
2037
2038
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002039static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002040QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pMe, const char *szLabel, int64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002041{
2042 // Use _AddBuffer() because _AddSZString() is defined below, not above
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002043 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_TYPE_TEXT_STRING, UsefulBuf_FromSZ(szLabel));
2044 QCBOREncode_AddInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002045}
2046
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002047static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002048QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pMe, int64_t nLabel, int64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002049{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002050 QCBOREncode_AddInt64(pMe, nLabel);
2051 QCBOREncode_AddInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002052}
2053
2054
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002055static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002056QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pMe, const char *szLabel, uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002057{
2058 // Use _AddBuffer() because _AddSZString() is defined below, not above
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002059 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_TYPE_TEXT_STRING, UsefulBuf_FromSZ(szLabel));
2060 QCBOREncode_AddUInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002061}
2062
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002063static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002064QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002065{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002066 QCBOREncode_AddInt64(pMe, nLabel);
2067 QCBOREncode_AddUInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002068}
2069
2070
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002071static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002072QCBOREncode_AddText(QCBOREncodeContext *pMe, UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002073{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002074 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_TYPE_TEXT_STRING, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002075}
2076
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002077static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002078QCBOREncode_AddTextToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002079{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002080 QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szLabel));
2081 QCBOREncode_AddText(pMe, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002082}
2083
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002084static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002085QCBOREncode_AddTextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002086{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002087 QCBOREncode_AddInt64(pMe, nLabel);
2088 QCBOREncode_AddText(pMe, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002089}
2090
2091
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002092inline static void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002093QCBOREncode_AddSZString(QCBOREncodeContext *pMe, const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002094{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002095 QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szString));
Michael Eckel5c531332020-03-02 01:35:30 +01002096}
2097
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002098static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002099QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pMe, const char *szLabel, const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002100{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002101 QCBOREncode_AddSZString(pMe, szLabel);
2102 QCBOREncode_AddSZString(pMe, szString);
Michael Eckel5c531332020-03-02 01:35:30 +01002103}
2104
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002105static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002106QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pMe, int64_t nLabel, const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002107{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002108 QCBOREncode_AddInt64(pMe, nLabel);
2109 QCBOREncode_AddSZString(pMe, szString);
Michael Eckel5c531332020-03-02 01:35:30 +01002110}
2111
2112
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002113static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002114QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pMe, const char *szLabel, double dNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002115{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002116 QCBOREncode_AddSZString(pMe, szLabel);
2117 QCBOREncode_AddDouble(pMe, dNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002118}
2119
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002120static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002121QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pMe, int64_t nLabel, double dNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002122{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002123 QCBOREncode_AddInt64(pMe, nLabel);
2124 QCBOREncode_AddDouble(pMe, dNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002125}
2126
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002127static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002128QCBOREncode_AddFloatToMap(QCBOREncodeContext *pMe, const char *szLabel, float dNum)
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002129{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002130 QCBOREncode_AddSZString(pMe, szLabel);
2131 QCBOREncode_AddFloat(pMe, dNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002132}
2133
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002134static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002135QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pMe, int64_t nLabel, float fNum)
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002136{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002137 QCBOREncode_AddInt64(pMe, nLabel);
2138 QCBOREncode_AddFloat(pMe, fNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002139}
2140
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002141static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002142QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pMe, const char *szLabel, double dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002143{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002144 QCBOREncode_AddSZString(pMe, szLabel);
2145 QCBOREncode_AddDoubleNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002146}
2147
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002148static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002149QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pMe, int64_t nLabel, double dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002150{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002151 QCBOREncode_AddInt64(pMe, nLabel);
2152 QCBOREncode_AddDoubleNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002153}
2154
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002155static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002156QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pMe, const char *szLabel, float dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002157{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002158 QCBOREncode_AddSZString(pMe, szLabel);
2159 QCBOREncode_AddFloatNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002160}
2161
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002162static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002163QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pMe, int64_t nLabel, float dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002164{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002165 QCBOREncode_AddInt64(pMe, nLabel);
2166 QCBOREncode_AddFloatNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002167}
2168
Michael Eckel5c531332020-03-02 01:35:30 +01002169
Laurence Lundblade9b334962020-08-27 10:55:53 -07002170
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002171static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002172QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pMe, uint8_t uTag, int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002173{
2174 if(uTag == QCBOR_ENCODE_AS_TAG) {
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002175 QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_EPOCH);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002176 }
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002177 QCBOREncode_AddInt64(pMe, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002178}
2179
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002180static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002181QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uTag, int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002182{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002183 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002184 QCBOREncode_AddTDateEpoch(pMe, uTag, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002185}
2186
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002187static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002188QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTag, int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002189{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002190 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002191 QCBOREncode_AddTDateEpoch(pMe, uTag, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002192}
2193
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002194static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002195QCBOREncode_AddDateEpoch(QCBOREncodeContext *pMe, int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002196{
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002197 QCBOREncode_AddTDateEpoch(pMe, QCBOR_ENCODE_AS_TAG, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002198}
2199
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002200static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002201QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pMe, const char *szLabel, int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002202{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002203 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002204 QCBOREncode_AddDateEpoch(pMe, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002205}
2206
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002207static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002208QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pMe, int64_t nLabel, int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002209{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002210 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002211 QCBOREncode_AddDateEpoch(pMe, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002212}
2213
2214
Laurence Lundblade9b334962020-08-27 10:55:53 -07002215
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002216static inline void
2217QCBOREncode_AddBytes(QCBOREncodeContext *pMe, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002218{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002219 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002220}
2221
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002222static inline void
2223QCBOREncode_AddBytesToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002224{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002225 QCBOREncode_AddSZString(pMe, szLabel);
2226 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002227}
2228
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002229static inline void
2230QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002231{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002232 QCBOREncode_AddInt64(pMe, nLabel);
2233 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002234}
2235
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002236static inline void
2237QCBOREncode_AddBytesLenOnly(QCBOREncodeContext *pMe, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002238{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002239 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002240}
2241
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002242static inline void
2243QCBOREncode_AddBytesLenOnlyToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002244{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002245 QCBOREncode_AddSZString(pMe, szLabel);
2246 QCBOREncode_AddBytesLenOnly(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002247}
2248
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002249static inline void
2250QCBOREncode_AddBytesLenOnlyToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002251{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002252 QCBOREncode_AddInt64(pMe, nLabel);
2253 QCBOREncode_AddBytesLenOnly(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002254}
2255
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002256
2257static inline void
2258QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002259{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002260 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2261 QCBOREncode_AddTag(pMe, CBOR_TAG_BIN_UUID);
2262 }
2263 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002264}
2265
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002266static inline void
2267QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pMe,
2268 const char *szLabel,
2269 uint8_t uTagRequirement,
2270 UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002271{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002272 QCBOREncode_AddSZString(pMe, szLabel);
2273 QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002274}
2275
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002276static inline void
2277QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002278{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002279 QCBOREncode_AddInt64(pMe, nLabel);
2280 QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002281}
2282
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002283static inline void
2284QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pMe, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002285{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002286 QCBOREncode_AddTBinaryUUID(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002287}
2288
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002289static inline void
2290QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002291{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002292 QCBOREncode_AddTBinaryUUIDToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002293}
2294
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002295static inline void
2296QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002297{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002298 QCBOREncode_AddTBinaryUUIDToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002299}
2300
2301
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002302static inline void
2303QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002304{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002305 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2306 QCBOREncode_AddTag(pMe, CBOR_TAG_POS_BIGNUM);
2307 }
2308 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002309}
2310
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002311static inline void
2312QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pMe,
2313 const char *szLabel,
2314 uint8_t uTagRequirement,
2315 UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002316{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002317 QCBOREncode_AddSZString(pMe, szLabel);
2318 QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002319}
2320
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002321static inline void
2322QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002323{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002324 QCBOREncode_AddInt64(pMe, nLabel);
2325 QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002326}
2327
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002328static inline void
2329QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pMe, UsefulBufC Bytes)
2330{
2331 QCBOREncode_AddTPositiveBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
2332}
2333
2334static inline void
2335QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
2336{
2337 QCBOREncode_AddTPositiveBignumToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2338}
2339
2340static inline void
2341QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
2342{
2343 QCBOREncode_AddTPositiveBignumToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2344}
2345
2346
2347static inline void
2348QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
2349{
2350 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2351 QCBOREncode_AddTag(pMe, CBOR_TAG_NEG_BIGNUM);
2352 }
2353 QCBOREncode_AddBytes(pMe, Bytes);
2354}
2355
2356static inline void
2357QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pMe,
2358 const char *szLabel,
2359 uint8_t uTagRequirement,
2360 UsefulBufC Bytes)
2361{
2362 QCBOREncode_AddSZString(pMe, szLabel);
2363 QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
2364}
2365
2366static inline void
2367QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
2368{
2369 QCBOREncode_AddInt64(pMe, nLabel);
2370 QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
2371}
2372
2373static inline void
2374QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pMe, UsefulBufC Bytes)
2375{
2376 QCBOREncode_AddTNegativeBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
2377}
2378
2379static inline void
2380QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
2381{
2382 QCBOREncode_AddTNegativeBignumToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2383}
2384
2385static inline void
2386QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
2387{
2388 QCBOREncode_AddTNegativeBignumToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2389}
2390
2391
Michael Eckel5c531332020-03-02 01:35:30 +01002392
2393#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2394
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002395static inline void
2396QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pMe,
2397 uint8_t uTagRequirement,
2398 int64_t nMantissa,
2399 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002400{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002401 uint64_t uTag;
2402 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2403 uTag = CBOR_TAG_DECIMAL_FRACTION;
2404 } else {
2405 uTag = CBOR_TAG_INVALID64;
2406 }
2407 QCBOREncode_AddExponentAndMantissa(pMe,
2408 uTag,
Michael Eckel5c531332020-03-02 01:35:30 +01002409 NULLUsefulBufC,
2410 false,
2411 nMantissa,
2412 nBase10Exponent);
2413}
2414
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002415static inline void
2416QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pMe,
2417 const char *szLabel,
2418 uint8_t uTagRequirement,
2419 int64_t nMantissa,
2420 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002421{
2422 QCBOREncode_AddSZString(pMe, szLabel);
2423 QCBOREncode_AddTDecimalFraction(pMe, uTagRequirement, nMantissa, nBase10Exponent);
2424}
2425
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002426static inline void
2427QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pMe,
2428 int64_t nLabel,
2429 uint8_t uTagRequirement,
2430 int64_t nMantissa,
2431 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002432{
2433 QCBOREncode_AddInt64(pMe, nLabel);
2434 QCBOREncode_AddTDecimalFraction(pMe, uTagRequirement, nMantissa, nBase10Exponent);
2435}
2436
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002437static inline void
2438QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pMe,
2439 int64_t nMantissa,
2440 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002441{
2442 QCBOREncode_AddTDecimalFraction(pMe, QCBOR_ENCODE_AS_TAG, nMantissa, nBase10Exponent);
2443}
2444
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002445static inline void
2446QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pMe,
2447 const char *szLabel,
2448 int64_t nMantissa,
2449 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002450{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002451 QCBOREncode_AddTDecimalFractionToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002452}
2453
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002454static inline void
2455QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pMe,
2456 int64_t nLabel,
2457 int64_t nMantissa,
2458 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002459{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002460 QCBOREncode_AddTDecimalFractionToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002461}
2462
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002463
2464
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002465static inline void
2466QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pMe,
2467 uint8_t uTagRequirement,
2468 UsefulBufC Mantissa,
2469 bool bIsNegative,
2470 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002471{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002472 uint64_t uTag;
2473 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2474 uTag = CBOR_TAG_DECIMAL_FRACTION;
2475 } else {
2476 uTag = CBOR_TAG_INVALID64;
2477 }
2478 QCBOREncode_AddExponentAndMantissa(pMe,
2479 uTag,
Michael Eckel5c531332020-03-02 01:35:30 +01002480 Mantissa, bIsNegative,
2481 0,
2482 nBase10Exponent);
2483}
2484
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002485static inline void
2486QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
2487 const char *szLabel,
2488 uint8_t uTagRequirement,
2489 UsefulBufC Mantissa,
2490 bool bIsNegative,
2491 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002492{
2493 QCBOREncode_AddSZString(pMe, szLabel);
2494 QCBOREncode_AddTDecimalFractionBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase10Exponent);
2495}
2496
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002497static inline void
2498QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
2499 int64_t nLabel,
2500 uint8_t uTagRequirement,
2501 UsefulBufC Mantissa,
2502 bool bIsNegative,
2503 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002504{
2505 QCBOREncode_AddInt64(pMe, nLabel);
2506 QCBOREncode_AddTDecimalFractionBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase10Exponent);
2507}
2508
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002509static inline void
2510QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pMe,
2511 UsefulBufC Mantissa,
2512 bool bIsNegative,
2513 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002514{
2515 QCBOREncode_AddTDecimalFractionBigNum(pMe, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase10Exponent);
2516}
2517
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002518static inline void
2519QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
2520 const char *szLabel,
2521 UsefulBufC Mantissa,
2522 bool bIsNegative,
2523 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002524{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002525 QCBOREncode_AddTDecimalFractionBigNumToMapSZ(pMe,
2526 szLabel,
2527 QCBOR_ENCODE_AS_TAG,
2528 Mantissa,
2529 bIsNegative,
2530 nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002531}
2532
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002533static inline void
2534QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
2535 int64_t nLabel,
2536 UsefulBufC Mantissa,
2537 bool bIsNegative,
2538 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002539{
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002540 QCBOREncode_AddTDecimalFractionBigNumToMapN(pMe,
2541 nLabel,
2542 QCBOR_ENCODE_AS_TAG,
2543 Mantissa,
2544 bIsNegative,
2545 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002546}
2547
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002548
2549
2550
2551
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002552static inline void
2553QCBOREncode_AddTBigFloat(QCBOREncodeContext *pMe,
2554 uint8_t uTagRequirement,
2555 int64_t nMantissa,
2556 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002557{
2558 uint64_t uTag;
2559 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2560 uTag = CBOR_TAG_BIGFLOAT;
2561 } else {
2562 uTag = CBOR_TAG_INVALID64;
2563 }
2564 QCBOREncode_AddExponentAndMantissa(pMe, uTag, NULLUsefulBufC, false, nMantissa, nBase2Exponent);
2565}
2566
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002567static inline void
2568QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pMe,
2569 const char *szLabel,
2570 uint8_t uTagRequirement,
2571 int64_t nMantissa,
2572 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002573{
2574 QCBOREncode_AddSZString(pMe, szLabel);
2575 QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
2576}
2577
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002578static inline void
2579QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pMe,
2580 int64_t nLabel,
2581 uint8_t uTagRequirement,
2582 int64_t nMantissa,
2583 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002584{
2585 QCBOREncode_AddInt64(pMe, nLabel);
2586 QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
2587}
2588
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002589static inline void
2590QCBOREncode_AddBigFloat(QCBOREncodeContext *pMe,
2591 int64_t nMantissa,
2592 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002593{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002594 QCBOREncode_AddTBigFloat(pMe, QCBOR_ENCODE_AS_TAG, nMantissa, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002595}
2596
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002597static inline void
2598QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pMe,
2599 const char *szLabel,
2600 int64_t nMantissa,
2601 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002602{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002603 QCBOREncode_AddTBigFloatToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002604}
2605
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002606static inline void
2607QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pMe,
2608 int64_t nLabel,
2609 int64_t nMantissa,
2610 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002611{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002612 QCBOREncode_AddTBigFloatToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002613}
2614
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002615
2616
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002617static inline void
2618QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pMe,
2619 uint8_t uTagRequirement,
2620 UsefulBufC Mantissa,
2621 bool bIsNegative,
2622 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002623{
2624 uint64_t uTag;
2625 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2626 uTag = CBOR_TAG_BIGFLOAT;
2627 } else {
2628 uTag = CBOR_TAG_INVALID64;
2629 }
2630 QCBOREncode_AddExponentAndMantissa(pMe, uTag, Mantissa, bIsNegative, 0, nBase2Exponent);
2631}
2632
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002633static inline void
2634QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pMe,
2635 const char *szLabel,
2636 uint8_t uTagRequirement,
2637 UsefulBufC Mantissa,
2638 bool bIsNegative,
2639 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002640{
2641 QCBOREncode_AddSZString(pMe, szLabel);
2642 QCBOREncode_AddTBigFloatBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase2Exponent);
2643}
2644
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002645static inline void
2646QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
2647 int64_t nLabel,
2648 uint8_t uTagRequirement,
2649 UsefulBufC Mantissa,
2650 bool bIsNegative,
2651 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002652{
2653 QCBOREncode_AddInt64(pMe, nLabel);
2654 QCBOREncode_AddTBigFloatBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase2Exponent);
2655}
2656
2657
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002658static inline void
2659QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pMe,
2660 UsefulBufC Mantissa,
2661 bool bIsNegative,
2662 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002663{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002664 QCBOREncode_AddTBigFloatBigNum(pMe, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002665}
2666
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002667static inline void
2668QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pMe,
2669 const char *szLabel,
2670 UsefulBufC Mantissa,
2671 bool bIsNegative,
2672 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002673{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002674 QCBOREncode_AddTBigFloatBigNumToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002675}
2676
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002677static inline void
2678QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
2679 int64_t nLabel,
2680 UsefulBufC Mantissa,
2681 bool bIsNegative,
2682 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002683{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002684 QCBOREncode_AddTBigFloatBigNumToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002685}
2686#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
2687
2688
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002689static inline void
2690QCBOREncode_AddTURI(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01002691{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002692 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2693 QCBOREncode_AddTag(pMe, CBOR_TAG_URI);
2694 }
2695 QCBOREncode_AddText(pMe, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01002696}
2697
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002698static inline void
2699QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uTagRequirement, UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01002700{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002701 QCBOREncode_AddSZString(pMe, szLabel);
2702 QCBOREncode_AddTURI(pMe, uTagRequirement, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01002703}
2704
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002705static inline void
2706QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01002707{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002708 QCBOREncode_AddInt64(pMe, nLabel);
2709 QCBOREncode_AddTURI(pMe, uTagRequirement, URI);
2710}
2711
2712static inline void
2713QCBOREncode_AddURI(QCBOREncodeContext *pMe, UsefulBufC URI)
2714{
2715 QCBOREncode_AddTURI(pMe, QCBOR_ENCODE_AS_TAG, URI);
2716}
2717
2718static inline void
2719QCBOREncode_AddURIToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC URI)
2720{
2721 QCBOREncode_AddTURIToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, URI);
2722}
2723
2724static inline void
2725QCBOREncode_AddURIToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC URI)
2726{
2727 QCBOREncode_AddTURIToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01002728}
2729
2730
2731
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002732static inline void
2733QCBOREncode_AddTB64Text(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002734{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002735 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2736 QCBOREncode_AddTag(pMe, CBOR_TAG_B64);
2737 }
2738 QCBOREncode_AddText(pMe, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002739}
2740
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002741static inline void
2742QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pMe,
2743 const char *szLabel,
2744 uint8_t uTagRequirement,
2745 UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002746{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002747 QCBOREncode_AddSZString(pMe, szLabel);
2748 QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002749}
2750
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002751static inline void
2752QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002753{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002754 QCBOREncode_AddInt64(pMe, nLabel);
2755 QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text);
2756}
2757
2758static inline void
2759QCBOREncode_AddB64Text(QCBOREncodeContext *pMe, UsefulBufC B64Text)
2760{
2761 QCBOREncode_AddTB64Text(pMe, QCBOR_ENCODE_AS_TAG, B64Text);
2762}
2763
2764static inline void
2765QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC B64Text)
2766{
2767 QCBOREncode_AddTB64TextToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, B64Text);
2768}
2769
2770static inline void
2771QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC B64Text)
2772{
2773 QCBOREncode_AddTB64TextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002774}
2775
2776
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002777
2778static inline void
2779QCBOREncode_AddTB64URLText(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002780{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002781 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2782 QCBOREncode_AddTag(pMe, CBOR_TAG_B64URL);
2783 }
2784 QCBOREncode_AddText(pMe, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002785}
2786
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002787static inline void
2788QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pMe,
2789 const char *szLabel,
2790 uint8_t uTagRequirement,
2791 UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002792{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002793 QCBOREncode_AddSZString(pMe, szLabel);
2794 QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002795}
2796
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002797static inline void
2798QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002799{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002800 QCBOREncode_AddInt64(pMe, nLabel);
2801 QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text);
2802}
2803
2804static inline void
2805QCBOREncode_AddB64URLText(QCBOREncodeContext *pMe, UsefulBufC B64Text)
2806{
2807 QCBOREncode_AddTB64URLText(pMe, QCBOR_ENCODE_AS_TAG, B64Text);
2808}
2809
2810static inline void
2811QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC B64Text)
2812{
2813 QCBOREncode_AddTB64URLTextToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, B64Text);
2814}
2815
2816static inline void
2817QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC B64Text)
2818{
2819 QCBOREncode_AddTB64URLTextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002820}
2821
2822
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002823
2824static inline void
2825QCBOREncode_AddTRegex(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002826{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002827 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2828 QCBOREncode_AddTag(pMe, CBOR_TAG_REGEX);
2829 }
2830 QCBOREncode_AddText(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002831}
2832
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002833static inline void
2834QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002835{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002836 QCBOREncode_AddSZString(pMe, szLabel);
2837 QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002838}
2839
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002840static inline void
2841QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002842{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002843 QCBOREncode_AddInt64(pMe, nLabel);
2844 QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes);
2845}
2846
2847static inline void
2848QCBOREncode_AddRegex(QCBOREncodeContext *pMe, UsefulBufC Bytes)
2849{
2850 QCBOREncode_AddTRegex(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
2851}
2852
2853static inline void
2854QCBOREncode_AddRegexToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
2855{
2856 QCBOREncode_AddTRegexToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2857}
2858
2859static inline void
2860QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
2861{
2862 QCBOREncode_AddTRegexToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2863
Michael Eckel5c531332020-03-02 01:35:30 +01002864}
2865
2866
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002867static inline void
2868QCBOREncode_AddTMIMEData(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01002869{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002870 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
Laurence Lundblade4982f412020-09-18 23:02:18 -07002871 QCBOREncode_AddTag(pMe, CBOR_TAG_BINARY_MIME);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002872 }
Laurence Lundblade4982f412020-09-18 23:02:18 -07002873 QCBOREncode_AddBytes(pMe, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01002874}
2875
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002876static inline void
2877QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pMe,
2878 const char *szLabel,
2879 uint8_t uTagRequirement,
2880 UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01002881{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002882 QCBOREncode_AddSZString(pMe, szLabel);
2883 QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01002884}
2885
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002886static inline void
2887QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01002888{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002889 QCBOREncode_AddInt64(pMe, nLabel);
2890 QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData);
2891}
2892
2893static inline void
2894QCBOREncode_AddMIMEData(QCBOREncodeContext *pMe, UsefulBufC MIMEData)
2895{
2896 QCBOREncode_AddTMIMEData(pMe, QCBOR_ENCODE_AS_TAG, MIMEData);
2897}
2898
2899static inline void
2900QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC MIMEData)
2901{
2902 QCBOREncode_AddTMIMEDataToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, MIMEData);
2903}
2904
2905static inline void
2906QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC MIMEData)
2907{
2908 QCBOREncode_AddTMIMEDataToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01002909}
2910
2911
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002912static inline void
2913QCBOREncode_AddTDateString(QCBOREncodeContext *pMe, uint8_t uTagRequirement, const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002914{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002915 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2916 QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_STRING);
2917 }
2918 QCBOREncode_AddSZString(pMe, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002919}
2920
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002921static inline void
2922QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pMe,
2923 const char *szLabel,
2924 uint8_t uTagRequirement,
2925 const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002926{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002927 QCBOREncode_AddSZString(pMe, szLabel);
2928 QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002929}
2930
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002931static inline void
2932QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002933{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002934 QCBOREncode_AddInt64(pMe, nLabel);
2935 QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate);
2936}
2937
2938static inline void
2939QCBOREncode_AddDateString(QCBOREncodeContext *pMe, const char *szDate)
2940{
2941 QCBOREncode_AddTDateString(pMe, QCBOR_ENCODE_AS_TAG, szDate);
2942}
2943
2944static inline void
2945QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pMe, const char *szLabel, const char *szDate)
2946{
2947 QCBOREncode_AddTDateStringToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, szDate);
2948}
2949
2950static inline void
2951QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pMe, int64_t nLabel, const char *szDate)
2952{
2953 QCBOREncode_AddTDateStringToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002954}
2955
2956
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002957static inline void
2958QCBOREncode_AddSimple(QCBOREncodeContext *pMe, uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002959{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002960 QCBOREncode_AddType7(pMe, 0, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002961}
2962
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002963static inline void
2964QCBOREncode_AddSimpleToMap(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uSimple)
Michael Eckel5c531332020-03-02 01:35:30 +01002965{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002966 QCBOREncode_AddSZString(pMe, szLabel);
2967 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01002968}
2969
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002970static inline void
2971QCBOREncode_AddSimpleToMapN(QCBOREncodeContext *pMe, int nLabel, uint8_t uSimple)
Michael Eckel5c531332020-03-02 01:35:30 +01002972{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002973 QCBOREncode_AddInt64(pMe, nLabel);
2974 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01002975}
2976
2977
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002978static inline void
2979QCBOREncode_AddBool(QCBOREncodeContext *pMe, bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01002980{
2981 uint8_t uSimple = CBOR_SIMPLEV_FALSE;
2982 if(b) {
2983 uSimple = CBOR_SIMPLEV_TRUE;
2984 }
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002985 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01002986}
2987
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002988static inline void
2989QCBOREncode_AddBoolToMap(QCBOREncodeContext *pMe, const char *szLabel, bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01002990{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002991 QCBOREncode_AddSZString(pMe, szLabel);
2992 QCBOREncode_AddBool(pMe, b);
Michael Eckel5c531332020-03-02 01:35:30 +01002993}
2994
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002995static inline void
2996QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pMe, int64_t nLabel, bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01002997{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002998 QCBOREncode_AddInt64(pMe, nLabel);
2999 QCBOREncode_AddBool(pMe, b);
Michael Eckel5c531332020-03-02 01:35:30 +01003000}
3001
3002
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003003static inline void
3004QCBOREncode_AddNULL(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003005{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003006 QCBOREncode_AddSimple(pMe, CBOR_SIMPLEV_NULL);
Michael Eckel5c531332020-03-02 01:35:30 +01003007}
3008
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003009static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003010QCBOREncode_AddNULLToMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003011{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003012 QCBOREncode_AddSZString(pMe, szLabel);
3013 QCBOREncode_AddNULL(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003014}
3015
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003016static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003017QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003018{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003019 QCBOREncode_AddInt64(pMe, nLabel);
3020 QCBOREncode_AddNULL(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003021}
3022
3023
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003024static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003025QCBOREncode_AddUndef(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003026{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003027 QCBOREncode_AddSimple(pMe, CBOR_SIMPLEV_UNDEF);
Michael Eckel5c531332020-03-02 01:35:30 +01003028}
3029
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003030static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003031QCBOREncode_AddUndefToMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003032{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003033 QCBOREncode_AddSZString(pMe, szLabel);
3034 QCBOREncode_AddUndef(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003035}
3036
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003037static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003038QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003039{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003040 QCBOREncode_AddInt64(pMe, nLabel);
3041 QCBOREncode_AddUndef(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003042}
3043
3044
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003045static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003046QCBOREncode_OpenArray(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003047{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003048 QCBOREncode_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY);
Michael Eckel5c531332020-03-02 01:35:30 +01003049}
3050
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003051static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003052QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003053{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003054 QCBOREncode_AddSZString(pMe, szLabel);
3055 QCBOREncode_OpenArray(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003056}
3057
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003058static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003059QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003060{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003061 QCBOREncode_AddInt64(pMe, nLabel);
3062 QCBOREncode_OpenArray(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003063}
3064
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003065static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003066QCBOREncode_CloseArray(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003067{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003068 QCBOREncode_CloseMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY);
Michael Eckel5c531332020-03-02 01:35:30 +01003069}
3070
3071
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003072static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003073QCBOREncode_OpenMap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003074{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003075 QCBOREncode_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_MAP);
Michael Eckel5c531332020-03-02 01:35:30 +01003076}
3077
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003078static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003079QCBOREncode_OpenMapInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003080{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003081 QCBOREncode_AddSZString(pMe, szLabel);
3082 QCBOREncode_OpenMap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003083}
3084
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003085static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003086QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003087{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003088 QCBOREncode_AddInt64(pMe, nLabel);
3089 QCBOREncode_OpenMap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003090}
3091
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003092static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003093QCBOREncode_CloseMap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003094{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003095 QCBOREncode_CloseMapOrArray(pMe, CBOR_MAJOR_TYPE_MAP);
Michael Eckel5c531332020-03-02 01:35:30 +01003096}
3097
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003098static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003099QCBOREncode_OpenArrayIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003100{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003101 QCBOREncode_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01003102}
3103
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003104static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003105QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003106{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003107 QCBOREncode_AddSZString(pMe, szLabel);
3108 QCBOREncode_OpenArrayIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003109}
3110
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003111static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003112QCBOREncode_OpenArrayIndefiniteLengthInMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003113{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003114 QCBOREncode_AddInt64(pMe, nLabel);
3115 QCBOREncode_OpenArrayIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003116}
3117
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003118static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003119QCBOREncode_CloseArrayIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003120{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003121 QCBOREncode_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01003122}
3123
3124
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003125static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003126QCBOREncode_OpenMapIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003127{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003128 QCBOREncode_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01003129}
3130
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003131static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003132QCBOREncode_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003133{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003134 QCBOREncode_AddSZString(pMe, szLabel);
3135 QCBOREncode_OpenMapIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003136}
3137
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003138static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003139QCBOREncode_OpenMapIndefiniteLengthInMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003140{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003141 QCBOREncode_AddInt64(pMe, nLabel);
3142 QCBOREncode_OpenMapIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003143}
3144
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003145static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003146QCBOREncode_CloseMapIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003147{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003148 QCBOREncode_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01003149}
3150
3151
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003152static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003153QCBOREncode_BstrWrap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003154{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003155 QCBOREncode_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_BYTE_STRING);
Michael Eckel5c531332020-03-02 01:35:30 +01003156}
3157
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003158static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003159QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003160{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003161 QCBOREncode_AddSZString(pMe, szLabel);
3162 QCBOREncode_BstrWrap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003163}
3164
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003165static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003166QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003167{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003168 QCBOREncode_AddInt64(pMe, nLabel);
3169 QCBOREncode_BstrWrap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003170}
3171
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003172static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003173QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pMe, UsefulBufC *pWrappedCBOR)
Michael Eckel5c531332020-03-02 01:35:30 +01003174{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003175 QCBOREncode_CloseBstrWrap2(pMe, true, pWrappedCBOR);
Michael Eckel5c531332020-03-02 01:35:30 +01003176}
3177
3178
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003179static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003180QCBOREncode_AddEncoded(QCBOREncodeContext *pMe, UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01003181{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003182 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_NONE_TYPE_RAW, Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01003183}
3184
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003185static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003186QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01003187{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003188 QCBOREncode_AddSZString(pMe, szLabel);
3189 QCBOREncode_AddEncoded(pMe, Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01003190}
3191
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003192static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003193QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01003194{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003195 QCBOREncode_AddInt64(pMe, nLabel);
3196 QCBOREncode_AddEncoded(pMe, Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01003197}
3198
3199
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003200static inline int
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003201QCBOREncode_IsBufferNULL(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003202{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003203 return UsefulOutBuf_IsBufferNULL(&(pMe->OutBuf));
Michael Eckel5c531332020-03-02 01:35:30 +01003204}
3205
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003206static inline QCBORError
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003207QCBOREncode_GetErrorState(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003208{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003209 if(UsefulOutBuf_GetError(&(pMe->OutBuf))) {
Michael Eckel5c531332020-03-02 01:35:30 +01003210 // Items didn't fit in the buffer.
3211 // This check catches this condition for all the appends and inserts
3212 // so checks aren't needed when the appends and inserts are performed.
3213 // And of course UsefulBuf will never overrun the input buffer given
3214 // to it. No complex analysis of the error handling in this file is
3215 // needed to know that is true. Just read the UsefulBuf code.
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003216 pMe->uError = QCBOR_ERR_BUFFER_TOO_SMALL;
Michael Eckel5c531332020-03-02 01:35:30 +01003217 // QCBOR_ERR_BUFFER_TOO_SMALL masks other errors, but that is
3218 // OK. Once the caller fixes this, they'll be unmasked.
3219 }
3220
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003221 return (QCBORError)pMe->uError;
Michael Eckel5c531332020-03-02 01:35:30 +01003222}
3223
3224
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003225/* ========================================================================
3226 END OF PRIVATE INLINE IMPLEMENTATION
3227 ======================================================================== */
Michael Eckel5c531332020-03-02 01:35:30 +01003228
3229#ifdef __cplusplus
3230}
3231#endif
3232
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08003233#endif /* qcbor_encode_h */