blob: 8b2ed90ab68047877bc0a3aa4b426bd8aa4e8280 [file] [log] [blame]
Michael Eckel5c531332020-03-02 01:35:30 +01001/*==============================================================================
2 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeb9702452021-03-08 21:02:57 -08003 Copyright (c) 2018-2021, Laurence Lundblade.
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02004 Copyright (c) 2021, Arm Limited.
Michael Eckel5c531332020-03-02 01:35:30 +01005 All rights reserved.
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions are
9met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16 * Neither the name of The Linux Foundation nor the names of its
17 contributors, nor the name "Laurence Lundblade" may be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
20
21THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
24ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 =============================================================================*/
33
34
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080035#ifndef qcbor_encode_h
36#define qcbor_encode_h
Michael Eckel5c531332020-03-02 01:35:30 +010037
38
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080039#include "qcbor/qcbor_common.h"
40#include "qcbor/qcbor_private.h"
Michael Eckel5c531332020-03-02 01:35:30 +010041#include <stdbool.h>
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080042
Michael Eckel5c531332020-03-02 01:35:30 +010043
44#ifdef __cplusplus
45extern "C" {
Dave Thaler12b23752020-03-27 01:23:08 -070046#if 0
Michael Eckel5c531332020-03-02 01:35:30 +010047} // Keep editor indention formatting happy
48#endif
49#endif
50
Michael Eckel5c531332020-03-02 01:35:30 +010051
52/**
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080053 @file qcbor_encode.h
Michael Eckel5c531332020-03-02 01:35:30 +010054
Laurence Lundblade9a3b6252020-10-21 21:30:31 -070055 @anchor Overview
56
57 # QCBOR Overview
Michael Eckel5c531332020-03-02 01:35:30 +010058
59 This implements CBOR -- Concise Binary Object Representation as
Laurence Lundbladec02e13e2020-12-06 05:45:41 -080060 defined in [RFC 8949] (https://tools.ietf.org/html/rfc8949). More
Laurence Lundblade18e1d522020-10-22 02:18:41 -070061 information is at http://cbor.io. This is a near-complete implementation of
62 the specification. [RFC 8742] (https://tools.ietf.org/html/rfc8742) CBOR
63 Sequences is also supported. Limitations are listed further down.
Michael Eckel5c531332020-03-02 01:35:30 +010064
Laurence Lundblade9a3b6252020-10-21 21:30:31 -070065 See @ref Encoding for general discussion on encoding,
66 @ref BasicDecode for general discussion on the basic decode features
67 and @ref SpiffyDecode for general discussion on the easier-to-use
68 decoder functions.
69
Michael Eckel5c531332020-03-02 01:35:30 +010070 CBOR is intentionally designed to be translatable to JSON, but not
Laurence Lundbladec02e13e2020-12-06 05:45:41 -080071 all CBOR can convert to JSON. See RFC 8949 for more info on how to
Michael Eckel5c531332020-03-02 01:35:30 +010072 construct CBOR that is the most JSON friendly.
73
74 The memory model for encoding and decoding is that encoded CBOR must
75 be in a contiguous buffer in memory. During encoding the caller must
76 supply an output buffer and if the encoding would go off the end of
77 the buffer an error is returned. During decoding the caller supplies
78 the encoded CBOR in a contiguous buffer and the decoder returns
79 pointers and lengths into that buffer for strings.
80
81 This implementation does not require malloc. All data structures
82 passed in/out of the APIs can fit on the stack.
83
84 Decoding of indefinite-length strings is a special case that requires
85 a "string allocator" to allocate memory into which the segments of
86 the string are coalesced. Without this, decoding will error out if an
87 indefinite-length string is encountered (indefinite-length maps and
88 arrays do not require the string allocator). A simple string
89 allocator called MemPool is built-in and will work if supplied with a
90 block of memory to allocate. The string allocator can optionally use
91 malloc() or some other custom scheme.
92
93 Here are some terms and definitions:
94
95 - "Item", "Data Item": An integer or string or such. The basic "thing" that
96 CBOR is about. An array is an item itself that contains some items.
97
98 - "Array": An ordered sequence of items, the same as JSON.
99
100 - "Map": A collection of label/value pairs. Each pair is a data
101 item. A JSON "object" is the same as a CBOR "map".
102
103 - "Label": The data item in a pair in a map that names or identifies
104 the pair, not the value. This implementation refers to it as a
105 "label". JSON refers to it as the "name". The CBOR RFC refers to it
106 this as a "key". This implementation chooses label instead because
107 key is too easily confused with a cryptographic key. The COSE
108 standard, which uses CBOR, has also chosen to use the term "label"
109 rather than "key" for this same reason.
110
111 - "Key": See "Label" above.
112
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700113 - "Tag": A data item that is an explicitly labeled new data
114 type made up of the tagging integer and the tag content.
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700115 See @ref Tags-Overview and @ref Tag-Usage.
Michael Eckel5c531332020-03-02 01:35:30 +0100116
117 - "Initial Byte": The first byte of an encoded item. Encoding and
118 decoding of this byte is taken care of by the implementation.
119
120 - "Additional Info": In addition to the major type, all data items
121 have some other info. This is usually the length of the data but can
122 be several other things. Encoding and decoding of this is taken care
123 of by the implementation.
124
125 CBOR has two mechanisms for tagging and labeling the data values like
126 integers and strings. For example, an integer that represents
127 someone's birthday in epoch seconds since Jan 1, 1970 could be
128 encoded like this:
129
130 - First it is CBOR_MAJOR_TYPE_POSITIVE_INT (@ref QCBOR_TYPE_INT64),
131 the primitive positive integer.
132
133 - Next it has a "tag" @ref CBOR_TAG_DATE_EPOCH indicating the integer
134 represents a date in the form of the number of seconds since Jan 1,
135 1970.
136
137 - Last it has a string "label" like "BirthDate" indicating the
138 meaning of the data.
139
140 The encoded binary looks like this:
141
142 a1 # Map of 1 item
143 69 # Indicates text string of 9 bytes
144 426972746844617465 # The text "BirthDate"
145 c1 # Tags next integer as epoch date
146 1a # Indicates a 4-byte integer
147 580d4172 # unsigned integer date 1477263730
148
149 Implementors using this API will primarily work with
150 labels. Generally, tags are only needed for making up new data
151 types. This implementation covers most of the data types defined in
152 the RFC using tags. It also, allows for the use of custom tags if
153 necessary.
154
155 This implementation explicitly supports labels that are text strings
156 and integers. Text strings translate nicely into JSON objects and are
157 very readable. Integer labels are much less readable but can be very
158 compact. If they are in the range of 0 to 23, they take up only one
159 byte.
160
161 CBOR allows a label to be any type of data including an array or a
162 map. It is possible to use this API to construct and parse such
163 labels, but it is not explicitly supported.
164
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700165 @anchor Encoding
166
167 ## Encoding
168
Michael Eckel5c531332020-03-02 01:35:30 +0100169 A common encoding usage mode is to invoke the encoding twice. First
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700170 with the output buffer as @ref SizeCalculateUsefulBuf to compute the
171 length of the needed output buffer. The correct sized output buffer
172 is allocated. The encoder is invoked a second time with the allocated
173 output buffer.
Michael Eckel5c531332020-03-02 01:35:30 +0100174
175 The double invocation is not required if the maximum output buffer
176 size can be predicted. This is usually possible for simple CBOR
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700177 structures.
Michael Eckel5c531332020-03-02 01:35:30 +0100178
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
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200344 If USEFULBUF_DISABLE_ALL_FLOATis defined, then floating point support is
345 completely disabled. Decoding functions return @ref QCBOR_ERR_ALL_FLOAT_DISABLED
346 if a floating point value is encountered during decoding. Functions that are
347 encoding floating point values are not available.
348
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700349 ## Limitations
350
Michael Eckel5c531332020-03-02 01:35:30 +0100351 Summary Limits of this implementation:
352 - The entire encoded CBOR must fit into contiguous memory.
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700353 - Max size of encoded / decoded CBOR data is a few bytes less than @c UINT32_MAX (4GB).
Michael Eckel5c531332020-03-02 01:35:30 +0100354 - Max array / map nesting level when encoding / decoding is
355 @ref QCBOR_MAX_ARRAY_NESTING (this is typically 15).
356 - Max items in an array or map when encoding / decoding is
357 @ref QCBOR_MAX_ITEMS_IN_ARRAY (typically 65,536).
358 - Does not directly support labels in maps other than text strings & integers.
359 - Does not directly support integer labels greater than @c INT64_MAX.
360 - Epoch dates limited to @c INT64_MAX (+/- 292 billion years).
361 - Exponents for bigfloats and decimal integers are limited to @c INT64_MAX.
362 - Tags on labels are ignored during decoding.
Laurence Lundblade9a3b6252020-10-21 21:30:31 -0700363 - The maximum tag nesting is @c QCBOR_MAX_TAGS_PER_ITEM (typically 4).
Michael Eckel5c531332020-03-02 01:35:30 +0100364 - Works only on 32- and 64-bit CPUs (modifications could make it work
365 on 16-bit CPUs).
366
367 The public interface uses @c size_t for all lengths. Internally the
368 implementation uses 32-bit lengths by design to use less memory and
369 fit structures on the stack. This limits the encoded CBOR it can work
370 with to size @c UINT32_MAX (4GB) which should be enough.
371
372 This implementation assumes two's compliment integer machines. @c
373 <stdint.h> also requires this. It is possible to modify this
374 implementation for another integer representation, but all modern
375 machines seem to be two's compliment.
Michael Eckel5c531332020-03-02 01:35:30 +0100376 */
377
378
Laurence Lundblade825164e2020-10-22 20:18:06 -0700379/**
Michael Eckel5c531332020-03-02 01:35:30 +0100380 The size of the buffer to be passed to QCBOREncode_EncodeHead(). It is one
381 byte larger than sizeof(uint64_t) + 1, the actual maximum size of the
Laurence Lundblade825164e2020-10-22 20:18:06 -0700382 head of a CBOR data item because QCBOREncode_EncodeHead() needs
Michael Eckel5c531332020-03-02 01:35:30 +0100383 one extra byte to work.
384 */
385#define QCBOR_HEAD_BUFFER_SIZE (sizeof(uint64_t) + 2)
386
Michael Eckel5c531332020-03-02 01:35:30 +0100387
Laurence Lundblade9b334962020-08-27 10:55:53 -0700388/**
Laurence Lundblade825164e2020-10-22 20:18:06 -0700389 Output the full CBOR tag. See @ref CBORTags, @ref Tag-Usage and
390 @ref Tags-Overview.
Laurence Lundblade9b334962020-08-27 10:55:53 -0700391 */
392#define QCBOR_ENCODE_AS_TAG 0
393
394/**
Laurence Lundblade825164e2020-10-22 20:18:06 -0700395 Output only the 'borrowed' content format for the relevant tag.
396 See @ref CBORTags, @ref Tag-Usage and @ref Tags-Overview.
Laurence Lundblade9b334962020-08-27 10:55:53 -0700397 */
398#define QCBOR_ENCODE_AS_BORROWED 1
399
Michael Eckel5c531332020-03-02 01:35:30 +0100400
401/**
402 QCBOREncodeContext is the data type that holds context for all the
403 encoding functions. It is less than 200 bytes, so it can go on the
404 stack. The contents are opaque, and the caller should not access
405 internal members. A context may be re used serially as long as it is
406 re initialized.
407 */
408typedef struct _QCBOREncodeContext QCBOREncodeContext;
409
410
411/**
412 Initialize the encoder to prepare to encode some CBOR.
413
414 @param[in,out] pCtx The encoder context to initialize.
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800415 @param[in] Storage The buffer into which the encoded result
416 will be written.
Michael Eckel5c531332020-03-02 01:35:30 +0100417
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800418 Call this once at the start of an encoding of some CBOR. Then call
419 the many functions like QCBOREncode_AddInt64() and
420 QCBOREncode_AddText() to add the different data items. Finally, call
421 QCBOREncode_Finish() to get the pointer and length of the encoded
422 result.
Michael Eckel5c531332020-03-02 01:35:30 +0100423
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800424 The primary purpose of this function is to give the pointer and
425 length of the output buffer into which the encoded CBOR will be
426 written. This is done with a @ref UsefulBuf structure, which is just
427 a pointer and length (it is equivalent to two parameters, one a
428 pointer and one a length, but a little prettier).
Michael Eckel5c531332020-03-02 01:35:30 +0100429
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800430 The output buffer can be allocated any way (malloc, stack,
431 static). It is just some memory that QCBOR writes to. The length must
432 be the length of the allocated buffer. QCBOR will never write past
433 that length, but might write up to that length. If the buffer is too
434 small, encoding will go into an error state and not write anything
435 further.
436
437 If allocating on the stack the convenience macro
438 UsefulBuf_MAKE_STACK_UB() can be used, but its use is not required.
439
440 Since there is no reallocation or such, the output buffer must be
441 correctly sized when passed in here. It is OK, but wasteful if it is
442 too large. One way to pick the size is to figure out the maximum size
443 that will ever be needed and hard code a buffer of that size.
444
445 Another way to do it is to have QCBOR calculate it for you. To do
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700446 this, pass @ref SizeCalculateUsefulBuf for @c Storage.
447 Then call all the functions to add the CBOR exactly as if
448 encoding for real. Finally, call QCBOREncode_FinishGetSize().
449 Once the length is obtained, allocate a buffer of that
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800450 size, call QCBOREncode_Init() again with the real buffer. Call all
451 the add functions again and finally, QCBOREncode_Finish() to obtain
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700452 the final result. This uses twice the CPU time, but that is
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800453 usually not an issue.
454
455 See QCBOREncode_Finish() for how the pointer and length for the
456 encoded CBOR is returned.
457
Laurence Lundblade8ece3732021-09-21 21:47:23 -0700458 For practical purposes QCBOR can't output encoded CBOR larger than
459 @c UINT32_MAX (4GB) even on 64-bit CPUs because the internal offsets
460 used to track the start of an array/map are 32 bits to reduce the
461 size of the encoding context.
Michael Eckel5c531332020-03-02 01:35:30 +0100462
463 A @ref QCBOREncodeContext can be reused over and over as long as
Laurence Lundblade8510f8c2020-12-01 11:31:16 -0800464 QCBOREncode_Init() is called before each use.
Michael Eckel5c531332020-03-02 01:35:30 +0100465 */
466void QCBOREncode_Init(QCBOREncodeContext *pCtx, UsefulBuf Storage);
467
468
469/**
470 @brief Add a signed 64-bit integer to the encoded output.
471
472 @param[in] pCtx The encoding context to add the integer to.
473 @param[in] nNum The integer to add.
474
475 The integer will be encoded and added to the CBOR output.
476
477 This function figures out the size and the sign and encodes in the
478 correct minimal CBOR. Specifically, it will select CBOR major type 0
479 or 1 based on sign and will encode to 1, 2, 4 or 8 bytes depending on
480 the value of the integer. Values less than 24 effectively encode to
481 one byte because they are encoded in with the CBOR major type. This
482 is a neat and efficient characteristic of CBOR that can be taken
483 advantage of when designing CBOR-based protocols. If integers like
484 tags can be kept between -23 and 23 they will be encoded in one byte
485 including the major type.
486
487 If you pass a smaller int, say an @c int16_t or a small value, say
488 100, the encoding will still be CBOR's most compact that can
489 represent the value. For example, CBOR always encodes the value 0 as
490 one byte, 0x00. The representation as 0x00 includes identification of
491 the type as an integer too as the major type for an integer is 0. See
Laurence Lundbladec02e13e2020-12-06 05:45:41 -0800492 [RFC 8949] (https://tools.ietf.org/html/rfc8949) Appendix A for more
493 examples of CBOR encoding. This compact encoding is also preferred
494 serialization CBOR as per section 34.1 in RFC 8949.
Michael Eckel5c531332020-03-02 01:35:30 +0100495
496 There are no functions to add @c int16_t or @c int32_t because they
497 are not necessary because this always encodes to the smallest number
498 of bytes based on the value (If this code is running on a 32-bit
499 machine having a way to add 32-bit integers would reduce code size
500 some).
501
502 If the encoding context is in an error state, this will do
503 nothing. If an error occurs when adding this integer, the internal
504 error flag will be set, and the error will be returned when
505 QCBOREncode_Finish() is called.
506
507 See also QCBOREncode_AddUInt64().
508 */
509void QCBOREncode_AddInt64(QCBOREncodeContext *pCtx, int64_t nNum);
510
511static void QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t uNum);
512
513static void QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum);
514
515
516/**
517 @brief Add an unsigned 64-bit integer to the encoded output.
518
519 @param[in] pCtx The encoding context to add the integer to.
520 @param[in] uNum The integer to add.
521
522 The integer will be encoded and added to the CBOR output.
523
524 The only reason so use this function is for integers larger than @c
525 INT64_MAX and smaller than @c UINT64_MAX. Otherwise
526 QCBOREncode_AddInt64() will work fine.
527
528 Error handling is the same as for QCBOREncode_AddInt64().
529 */
530void QCBOREncode_AddUInt64(QCBOREncodeContext *pCtx, uint64_t uNum);
531
532static void QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum);
533
534static void QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum);
535
536
537/**
538 @brief Add a UTF-8 text string to the encoded output.
539
540 @param[in] pCtx The encoding context to add the text to.
541 @param[in] Text Pointer and length of text to add.
542
543 The text passed in must be unencoded UTF-8 according to [RFC 3629]
544 (https://tools.ietf.org/html/rfc3629). There is no NULL
545 termination. The text is added as CBOR major type 3.
546
547 If called with @c nBytesLen equal to 0, an empty string will be
548 added. When @c nBytesLen is 0, @c pBytes may be @c NULL.
549
550 Note that the restriction of the buffer length to a @c uint32_t is
551 entirely intentional as this encoder is not capable of encoding
552 lengths greater. This limit to 4GB for a text string should not be a
553 problem.
554
Laurence Lundbladeadaf5c22020-09-25 10:37:09 -0700555 Text lines in Internet protocols (on the wire) are delimited by
556 either a CRLF or just an LF. Officially many protocols specify CRLF,
557 but implementations often work with either. CBOR type 3 text can be
558 either line ending, even a mixture of both.
559
560 Operating systems usually have a line end convention. Windows uses
561 CRLF. Linux and MacOS use LF. Some applications on a given OS may
562 work with either and some may not.
563
564 The majority of use cases and CBOR protocols using type 3 text will
565 work with either line ending. However, some use cases or protocols
566 may not work with either in which case translation to and/or from the
567 local line end convention, typically that of the OS, is necessary.
568
569 QCBOR does no line ending translation for type 3 text when encoding
570 and decoding.
571
Michael Eckel5c531332020-03-02 01:35:30 +0100572 Error handling is the same as QCBOREncode_AddInt64().
573 */
574static void QCBOREncode_AddText(QCBOREncodeContext *pCtx, UsefulBufC Text);
575
576static void QCBOREncode_AddTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Text);
577
578static void QCBOREncode_AddTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Text);
579
580
581/**
582 @brief Add a UTF-8 text string to the encoded output.
583
584 @param[in] pCtx The encoding context to add the text to.
585 @param[in] szString Null-terminated text to add.
586
587 This works the same as QCBOREncode_AddText().
588 */
589static void QCBOREncode_AddSZString(QCBOREncodeContext *pCtx, const char *szString);
590
591static void QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szString);
592
593static void QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szString);
594
595
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200596#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Michael Eckel5c531332020-03-02 01:35:30 +0100597/**
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700598 @brief Add a double-precision floating-point number to the encoded output.
Michael Eckel5c531332020-03-02 01:35:30 +0100599
600 @param[in] pCtx The encoding context to add the double to.
601 @param[in] dNum The double-precision number to add.
602
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700603 This encodes and outputs a floating-point number. CBOR major type 7
604 is used.
Michael Eckel5c531332020-03-02 01:35:30 +0100605
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700606 This implements preferred serialization, selectively encoding the
607 double-precision floating-point number as either double-precision,
608 single-precision or half-precision. Infinity, NaN and 0 are always
609 encoded as half-precision. If no precision will be lost in the
610 conversion to half-precision, then it will be converted and
611 encoded. If not and no precision will be lost in conversion to
612 single-precision, then it will be converted and encoded. If not, then
613 no conversion is performed, and it encoded as a double-precision.
Michael Eckel5c531332020-03-02 01:35:30 +0100614
615 Half-precision floating-point numbers take up 2 bytes, half that of
616 single-precision, one quarter of double-precision
617
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700618 This automatically reduces the size of encoded CBOR, maybe even by
619 four if most of values are 0, infinity or NaN.
Michael Eckel5c531332020-03-02 01:35:30 +0100620
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700621 When decoded, QCBOR will usually return these values as
622 double-precision.
623
624 It is possible to disable this preferred serialization when compiling
625 QCBOR. In that case, this functions the same as
626 QCBOREncode_AddDoubleNoPreferred().
Michael Eckel5c531332020-03-02 01:35:30 +0100627
628 Error handling is the same as QCBOREncode_AddInt64().
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700629
630 See also QCBOREncode_AddDoubleNoPreferred(), QCBOREncode_AddFloat()
631 and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
Michael Eckel5c531332020-03-02 01:35:30 +0100632 */
633void QCBOREncode_AddDouble(QCBOREncodeContext *pCtx, double dNum);
634
635static void QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum);
636
637static void QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum);
638
639
640/**
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700641 @brief Add a single-precision floating-point number to the encoded output.
642
643 @param[in] pCtx The encoding context to add the double to.
644 @param[in] fNum The single-precision number to add.
645
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700646 This is identical to QCBOREncode_AddDouble() except the input is
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700647 single-precision.
648
649 See also QCBOREncode_AddDouble(), QCBOREncode_AddDoubleNoPreferred(),
650 and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
651*/
652void QCBOREncode_AddFloat(QCBOREncodeContext *pCtx, float fNum);
653
654static void QCBOREncode_AddFloatToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum);
655
656static void QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float dNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700657
658
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700659/**
660 @brief Add a double-precision floating-point number without preferred encoding.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700661
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700662 @param[in] pCtx The encoding context to add the double to.
663 @param[in] dNum The double-precision number to add.
664
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700665 This always outputs the number as a 64-bit double-precision.
666 Preferred serialization is not used.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700667
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700668 Error handling is the same as QCBOREncode_AddInt64().
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700669
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700670 See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
671 QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700672*/
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700673void QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pCtx, double dNum);
674
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700675static void QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum);
676
677static void QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum);
678
679
680/**
681 @brief Add a single-precision floating-point number without preferred encoding.
682
683 @param[in] pCtx The encoding context to add the double to.
684 @param[in] fNum The single-precision number to add.
685
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700686 This always outputs the number as a 32-bit single-precision.
687 Preferred serialization is not used.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700688
689 Error handling is the same as QCBOREncode_AddInt64().
690
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700691 See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
692 QCBOREncode_AddDoubleNoPreferred() and @ref Floating-Point.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700693*/
694void QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pCtx, float fNum);
695
696static void QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum);
697
698static void QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float fNum);
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200699#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700700
701
Michael Eckel5c531332020-03-02 01:35:30 +0100702/**
703 @brief Add an optional tag.
704
705 @param[in] pCtx The encoding context to add the tag to.
706 @param[in] uTag The tag to add
707
708 This outputs a CBOR major type 6 item that tags the next data item
709 that is output usually to indicate it is some new data type.
710
711 For many of the common standard tags, a function to encode data using
712 it is provided and this is not needed. For example,
713 QCBOREncode_AddDateEpoch() already exists to output integers
714 representing dates with the right tag.
715
716 The tag is applied to the next data item added to the encoded
717 output. That data item that is to be tagged can be of any major CBOR
718 type. Any number of tags can be added to a data item by calling this
719 multiple times before the data item is added.
720
721 See @ref Tags-Overview for discussion of creating new non-standard
722 tags. See QCBORDecode_GetNext() for discussion of decoding custom
723 tags.
724*/
Laurence Lundblade6416a0f2020-09-19 23:26:34 -0700725void QCBOREncode_AddTag(QCBOREncodeContext *pCtx, uint64_t uTag);
Michael Eckel5c531332020-03-02 01:35:30 +0100726
727
728/**
729 @brief Add an epoch-based date.
730
731 @param[in] pCtx The encoding context to add the date to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700732 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700733 @param[in] nDate Number of seconds since 1970-01-01T00:00Z in UTC time.
Michael Eckel5c531332020-03-02 01:35:30 +0100734
Laurence Lundbladec02e13e2020-12-06 05:45:41 -0800735 As per RFC 8949 this is similar to UNIX/Linux/POSIX dates. This is
Michael Eckel5c531332020-03-02 01:35:30 +0100736 the most compact way to specify a date and time in CBOR. Note that
737 this is always UTC and does not include the time zone. Use
738 QCBOREncode_AddDateString() if you want to include the time zone.
739
Laurence Lundblade46d63e92021-05-13 11:37:10 -0700740 The preferred integer encoding rules apply here so the date will be encoded in
Michael Eckel5c531332020-03-02 01:35:30 +0100741 a minimal number of bytes. Until about the year 2106 these dates will
742 encode in 6 bytes -- one byte for the tag, one byte for the type and
743 4 bytes for the integer. After that it will encode to 10 bytes.
744
745 Negative values are supported for dates before 1970.
746
747 If you care about leap-seconds and that level of accuracy, make sure
748 the system you are running this code on does it correctly. This code
749 just takes the value passed in.
750
751 This implementation cannot encode fractional seconds using float or
752 double even though that is allowed by CBOR, but you can encode them
Laurence Lundblade46d63e92021-05-13 11:37:10 -0700753 if you want to by calling QCBOREncode_AddTag() and QCBOREncode_AddDouble().
Michael Eckel5c531332020-03-02 01:35:30 +0100754
755 Error handling is the same as QCBOREncode_AddInt64().
Laurence Lundblade46d63e92021-05-13 11:37:10 -0700756
757 See also QCBOREncode_AddTDaysEpoch().
Michael Eckel5c531332020-03-02 01:35:30 +0100758 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700759static void QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pCtx,
760 uint8_t uTagRequirement,
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700761 int64_t nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -0700762
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700763static void QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pCtx,
764 const char *szLabel,
765 uint8_t uTagRequirement,
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700766 int64_t nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -0700767
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700768static void QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pCtx,
769 int64_t nLabel,
770 uint8_t uTagRequirement,
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700771 int64_t nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -0700772
773
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700774static void QCBOREncode_AddDateEpoch(QCBOREncodeContext *pCtx,
775 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100776
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700777static void QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pCtx,
778 const char *szLabel,
779 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100780
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700781static void QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pCtx,
782 int64_t nLabel,
783 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100784
785
Laurence Lundblade46d63e92021-05-13 11:37:10 -0700786
787/**
788 @brief Add an epoch-based day-count date.
789
790 @param[in] pCtx The encoding context to add the date to.
791 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
792 @ref QCBOR_ENCODE_AS_BORROWED.
793 @param[in] nDays Number of days before or after 1970-01-0.
794
795 This date format is described in
796 [RFC 8943] (https://tools.ietf.org/html/rfc8943).
797
798 The integer encoding rules apply here so the date will be encoded in
799 a minimal number of bytes. Until about the year 2149 these dates will
800 encode in 4 bytes -- one byte for the tag, one byte for the type and
801 2 bytes for the integer.
802
803 See also QCBOREncode_AddTDateEpoch().
804
805*/
806static void QCBOREncode_AddTDaysEpoch(QCBOREncodeContext *pCtx,
807 uint8_t uTagRequirement,
808 int64_t nDays);
809
810static void QCBOREncode_AddTDaysEpochToMapSZ(QCBOREncodeContext *pCtx,
811 const char *szLabel,
812 uint8_t uTagRequirement,
813 int64_t nDays);
814
815static void QCBOREncode_AddTDaysEpochToMapN(QCBOREncodeContext *pCtx,
816 int64_t nLabel,
817 uint8_t uTagRequirement,
818 int64_t nDays);
819
820
821
822
Michael Eckel5c531332020-03-02 01:35:30 +0100823/**
824 @brief Add a byte string to the encoded output.
825
826 @param[in] pCtx The encoding context to add the bytes to.
827 @param[in] Bytes Pointer and length of the input data.
828
829 Simply adds the bytes to the encoded output as CBOR major type 2.
830
831 If called with @c Bytes.len equal to 0, an empty string will be
832 added. When @c Bytes.len is 0, @c Bytes.ptr may be @c NULL.
833
834 Error handling is the same as QCBOREncode_AddInt64().
835 */
836static void QCBOREncode_AddBytes(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
837
838static void QCBOREncode_AddBytesToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
839
840static void QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
841
842
Michael Eckel5c531332020-03-02 01:35:30 +0100843/**
Laurence Lundbladeb24faef2022-04-26 11:03:08 -0600844 @brief Set up to write a byte string value directly to encoded output.
845
846 @param[in] pCtx The encoding context to add the bytes to.
847 @param[out] pPlace Pointer and length of place to write byte string value.
848
849 QCBOREncode_AddBytes() is the normal way to encode a byte string.
850 This is for special cases and by passes some of the pointer safety.
851
852 The purpose of this is to output the bytes that make up a byte string
853 value directly to the QCBOR output buffer so you don't need to have a
854 copy of it in memory. This is particularly useful if the byte string
855 is large, for example, the encrypted payload of a COSE_Encrypt
856 message. The payload encryption algorithm can output directly to the
857 encoded CBOR buffer, perhaps by making it the output buffer
858 for some function (e.g. symmetric encryption) or by multiple writes.
859
860 The pointer in \c pPlace is where to start writing. Writing is just
861 copying bytes to the location by the pointer in \c pPlace. Writing
862 past the length in \c pPlace will be writing off the end of the
863 output buffer.
864
865 If there is no room in the output buffer @ref NULLUsefulBuf will be
866 returned and there is no need to call QCBOREncode_CloseBytes().
867
868 The byte string must be closed by calling QCBOREncode_CloseBytes().
869
870 Warning: this bypasses some of the usual checks provided by QCBOR
871 against writing off the end of the encoded output buffer.
872 */
873void
874QCBOREncode_OpenBytes(QCBOREncodeContext *pCtx, UsefulBuf *pPlace);
875
876static void
877QCBOREncode_OpenBytesInMapSZ(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBuf *pPlace);
878
879static void
880QCBOREncode_OpenBytesInMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBuf *pPlace);
881
882
883/**
884 @brief Close out a byte string written directly to encoded output.
885
886 @param[in] pCtx The encoding context to add the bytes to.
887 @param[out] uAmount The number of bytes written, the length of the byte string.
888
889 This closes out a call to QCBOREncode_OpenBytes(). This inserts a
890 CBOR header at the front of the byte string value to make it a
891 well-formed byte string.
892
893 If there was no call to QCBOREncode_OpenBytes() then
894 @ref QCBOR_ERR_TOO_MANY_CLOSES is set.
895 */
896void QCBOREncode_CloseBytes(QCBOREncodeContext *pCtx, size_t uAmount);
897
898
899/**
Michael Eckel5c531332020-03-02 01:35:30 +0100900 @brief Add a binary UUID to the encoded output.
901
902 @param[in] pCtx The encoding context to add the UUID to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700903 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100904 @param[in] Bytes Pointer and length of the binary UUID.
905
906 A binary UUID as defined in [RFC 4122]
907 (https://tools.ietf.org/html/rfc4122) is added to the output.
908
909 It is output as CBOR major type 2, a binary string, with tag @ref
910 CBOR_TAG_BIN_UUID indicating the binary string is a UUID.
911 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700912static void QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pCtx,
913 uint8_t uTagRequirement,
914 UsefulBufC Bytes);
915
916static void QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pCtx,
917 const char *szLabel,
918 uint8_t uTagRequirement,
919 UsefulBufC Bytes);
920
921static void QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pCtx,
922 int64_t nLabel,
923 uint8_t uTagRequirement,
924 UsefulBufC Bytes);
925
926
Michael Eckel5c531332020-03-02 01:35:30 +0100927static void QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
928
929static void QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
930
931static void QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
932
933
934/**
935 @brief Add a positive big number to the encoded output.
936
937 @param[in] pCtx The encoding context to add the big number to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700938 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100939 @param[in] Bytes Pointer and length of the big number.
940
941 Big numbers are integers larger than 64-bits. Their format is
Laurence Lundbladec02e13e2020-12-06 05:45:41 -0800942 described in [RFC 8949] (https://tools.ietf.org/html/rfc8949).
Michael Eckel5c531332020-03-02 01:35:30 +0100943
944 It is output as CBOR major type 2, a binary string, with tag @ref
945 CBOR_TAG_POS_BIGNUM indicating the binary string is a positive big
946 number.
947
948 Often big numbers are used to represent cryptographic keys, however,
949 COSE which defines representations for keys chose not to use this
950 particular type.
951 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700952static void QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pCtx,
953 uint8_t uTagRequirement,
954 UsefulBufC Bytes);
955
956static void QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pCtx,
957 const char *szLabel,
958 uint8_t uTagRequirement,
959 UsefulBufC Bytes);
960
961static void QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pCtx,
962 int64_t nLabel,
963 uint8_t uTagRequirement,
964 UsefulBufC Bytes);
965
966
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700967static void QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx,
968 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100969
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700970static void QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx,
971 const char *szLabel,
972 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100973
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700974static void QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx,
975 int64_t nLabel,
976 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100977
978
979/**
980 @brief Add a negative big number to the encoded output.
981
982 @param[in] pCtx The encoding context to add the big number to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700983 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100984 @param[in] Bytes Pointer and length of the big number.
985
986 Big numbers are integers larger than 64-bits. Their format is
Laurence Lundbladec02e13e2020-12-06 05:45:41 -0800987 described in [RFC 8949] (https://tools.ietf.org/html/rfc8949).
Michael Eckel5c531332020-03-02 01:35:30 +0100988
989 It is output as CBOR major type 2, a binary string, with tag @ref
990 CBOR_TAG_NEG_BIGNUM indicating the binary string is a negative big
991 number.
992
993 Often big numbers are used to represent cryptographic keys, however,
994 COSE which defines representations for keys chose not to use this
995 particular type.
996 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700997static void QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pCtx,
998 uint8_t uTagRequirement,
999 UsefulBufC Bytes);
1000
1001static void QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pCtx,
1002 const char *szLabel,
1003 uint8_t uTagRequirement,
1004 UsefulBufC Bytes);
1005
1006static void QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pCtx,
1007 int64_t nLabel,
1008 uint8_t uTagRequirement,
1009 UsefulBufC Bytes);
1010
1011
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001012static void QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx,
1013 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001014
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001015static void QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx,
1016 const char *szLabel,
1017 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001018
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001019static void QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx,
1020 int64_t nLabel,
1021 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001022
1023
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07001024#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Michael Eckel5c531332020-03-02 01:35:30 +01001025/**
1026 @brief Add a decimal fraction to the encoded output.
1027
1028 @param[in] pCtx The encoding context to add the decimal fraction to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001029 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001030 @param[in] nMantissa The mantissa.
1031 @param[in] nBase10Exponent The exponent.
1032
1033 The value is nMantissa * 10 ^ nBase10Exponent.
1034
1035 A decimal fraction is good for exact representation of some values
1036 that can't be represented exactly with standard C (IEEE 754)
1037 floating-point numbers. Much larger and much smaller numbers can
1038 also be represented than floating-point because of the larger number
1039 of bits in the exponent.
1040
1041 The decimal fraction is conveyed as two integers, a mantissa and a
1042 base-10 scaling factor.
1043
1044 For example, 273.15 is represented by the two integers 27315 and -2.
1045
1046 The exponent and mantissa have the range from @c INT64_MIN to
1047 @c INT64_MAX for both encoding and decoding (CBOR allows @c -UINT64_MAX
1048 to @c UINT64_MAX, but this implementation doesn't support this range to
1049 reduce code size and interface complexity a little).
1050
1051 CBOR Preferred encoding of the integers is used, thus they will be encoded
1052 in the smallest number of bytes possible.
1053
1054 See also QCBOREncode_AddDecimalFractionBigNum() for a decimal
1055 fraction with arbitrarily large precision and QCBOREncode_AddBigFloat().
1056
1057 There is no representation of positive or negative infinity or NaN
1058 (Not a Number). Use QCBOREncode_AddDouble() to encode them.
1059
1060 See @ref expAndMantissa for decoded representation.
1061 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001062static void QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pCtx,
1063 uint8_t uTagRequirement,
1064 int64_t nMantissa,
1065 int64_t nBase10Exponent);
1066
1067static void QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07001068 const char *szLabel,
1069 uint8_t uTagRequirement,
1070 int64_t nMantissa,
1071 int64_t nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001072
1073static void QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pCtx,
1074 int64_t nLabel,
1075 uint8_t uTagRequirement,
1076 int64_t nMantissa,
1077 int64_t nBase10Exponent);
1078
1079
Michael Eckel5c531332020-03-02 01:35:30 +01001080static void QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pCtx,
1081 int64_t nMantissa,
1082 int64_t nBase10Exponent);
1083
1084static void QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx,
1085 const char *szLabel,
1086 int64_t nMantissa,
1087 int64_t nBase10Exponent);
1088
1089static void QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pCtx,
1090 int64_t nLabel,
1091 int64_t nMantissa,
1092 int64_t nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01001093/**
1094 @brief Add a decimal fraction with a big number mantissa to the encoded output.
1095
1096 @param[in] pCtx The encoding context to add the decimal fraction to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001097 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001098 @param[in] Mantissa The mantissa.
1099 @param[in] bIsNegative false if mantissa is positive, true if negative.
1100 @param[in] nBase10Exponent The exponent.
1101
1102 This is the same as QCBOREncode_AddDecimalFraction() except the
1103 mantissa is a big number (See QCBOREncode_AddPositiveBignum())
1104 allowing for arbitrarily large precision.
1105
1106 See @ref expAndMantissa for decoded representation.
1107 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001108static void QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pCtx,
1109 uint8_t uTagRequirement,
1110 UsefulBufC Mantissa,
1111 bool bIsNegative,
1112 int64_t nBase10Exponent);
1113
1114static void QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07001115 const char *szLabel,
1116 uint8_t uTagRequirement,
1117 UsefulBufC Mantissa,
1118 bool bIsNegative,
1119 int64_t nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001120
1121static void QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
1122 int64_t nLabel,
1123 uint8_t uTagRequirement,
1124 UsefulBufC Mantissa,
1125 bool bIsNegative,
1126 int64_t nBase10Exponent);
1127
1128
Michael Eckel5c531332020-03-02 01:35:30 +01001129static void QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pCtx,
1130 UsefulBufC Mantissa,
1131 bool bIsNegative,
1132 int64_t nBase10Exponent);
1133
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001134static void QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07001135 const char *szLabel,
1136 UsefulBufC Mantissa,
1137 bool bIsNegative,
1138 int64_t nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01001139
1140static void QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
1141 int64_t nLabel,
1142 UsefulBufC Mantissa,
1143 bool bIsNegative,
1144 int64_t nBase10Exponent);
1145
1146/**
1147 @brief Add a big floating-point number to the encoded output.
1148
1149 @param[in] pCtx The encoding context to add the bigfloat to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001150 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001151 @param[in] nMantissa The mantissa.
1152 @param[in] nBase2Exponent The exponent.
1153
1154 The value is nMantissa * 2 ^ nBase2Exponent.
1155
1156 "Bigfloats", as CBOR terms them, are similar to IEEE floating-point
1157 numbers in having a mantissa and base-2 exponent, but they are not
1158 supported by hardware or encoded the same. They explicitly use two
1159 CBOR-encoded integers to convey the mantissa and exponent, each of which
1160 can be 8, 16, 32 or 64 bits. With both the mantissa and exponent
1161 64 bits they can express more precision and a larger range than an
1162 IEEE double floating-point number. See
1163 QCBOREncode_AddBigFloatBigNum() for even more precision.
1164
1165 For example, 1.5 would be represented by a mantissa of 3 and an
1166 exponent of -1.
1167
1168 The exponent and mantissa have the range from @c INT64_MIN to
1169 @c INT64_MAX for both encoding and decoding (CBOR allows @c -UINT64_MAX
1170 to @c UINT64_MAX, but this implementation doesn't support this range to
1171 reduce code size and interface complexity a little).
1172
1173 CBOR Preferred encoding of the integers is used, thus they will be encoded
1174 in the smallest number of bytes possible.
1175
1176 This can also be used to represent floating-point numbers in
1177 environments that don't support IEEE 754.
1178
1179 See @ref expAndMantissa for decoded representation.
1180 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001181static void QCBOREncode_AddTBigFloat(QCBOREncodeContext *pCtx,
1182 uint8_t uTagRequirement,
1183 int64_t nMantissa,
1184 int64_t nBase2Exponent);
1185
1186static void QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07001187 const char *szLabel,
1188 uint8_t uTagRequirement,
1189 int64_t nMantissa,
1190 int64_t nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001191
1192static void QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pCtx,
1193 int64_t nLabel,
1194 uint8_t uTagRequirement,
1195 int64_t nMantissa,
1196 int64_t nBase2Exponent);
1197
1198
Michael Eckel5c531332020-03-02 01:35:30 +01001199static void QCBOREncode_AddBigFloat(QCBOREncodeContext *pCtx,
1200 int64_t nMantissa,
1201 int64_t nBase2Exponent);
1202
1203static void QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pCtx,
1204 const char *szLabel,
1205 int64_t nMantissa,
1206 int64_t nBase2Exponent);
1207
1208static void QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pCtx,
1209 int64_t nLabel,
1210 int64_t nMantissa,
1211 int64_t nBase2Exponent);
1212
Michael Eckel5c531332020-03-02 01:35:30 +01001213/**
1214 @brief Add a big floating-point number with a big number mantissa to
1215 the encoded output.
1216
1217 @param[in] pCtx The encoding context to add the bigfloat to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001218 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001219 @param[in] Mantissa The mantissa.
1220 @param[in] bIsNegative false if mantissa is positive, true if negative.
1221 @param[in] nBase2Exponent The exponent.
1222
1223 This is the same as QCBOREncode_AddBigFloat() except the mantissa is
1224 a big number (See QCBOREncode_AddPositiveBignum()) allowing for
1225 arbitrary precision.
1226
1227 See @ref expAndMantissa for decoded representation.
1228 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001229static void QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pCtx,
1230 uint8_t uTagRequirement,
1231 UsefulBufC Mantissa,
1232 bool bIsNegative,
1233 int64_t nBase2Exponent);
1234
1235static void QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07001236 const char *szLabel,
1237 uint8_t uTagRequirement,
1238 UsefulBufC Mantissa,
1239 bool bIsNegative,
1240 int64_t nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001241
1242static void QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
1243 int64_t nLabel,
1244 uint8_t uTagRequirement,
1245 UsefulBufC Mantissa,
1246 bool bIsNegative,
1247 int64_t nBase2Exponent);
1248
1249
Michael Eckel5c531332020-03-02 01:35:30 +01001250static void QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pCtx,
1251 UsefulBufC Mantissa,
1252 bool bIsNegative,
1253 int64_t nBase2Exponent);
1254
1255static void QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pCtx,
1256 const char *szLabel,
1257 UsefulBufC Mantissa,
1258 bool bIsNegative,
1259 int64_t nBase2Exponent);
1260
1261static void QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
1262 int64_t nLabel,
1263 UsefulBufC Mantissa,
1264 bool bIsNegative,
1265 int64_t nBase2Exponent);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07001266#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Michael Eckel5c531332020-03-02 01:35:30 +01001267
1268
1269/**
1270 @brief Add a text URI to the encoded output.
1271
1272 @param[in] pCtx The encoding context to add the URI to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001273 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001274 @param[in] URI Pointer and length of the URI.
1275
1276 The format of URI must be per [RFC 3986]
1277 (https://tools.ietf.org/html/rfc3986).
1278
1279 It is output as CBOR major type 3, a text string, with tag @ref
1280 CBOR_TAG_URI indicating the text string is a URI.
1281
1282 A URI in a NULL-terminated string, @c szURI, can be easily added with
1283 this code:
1284
1285 QCBOREncode_AddURI(pCtx, UsefulBuf_FromSZ(szURI));
1286 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001287static void QCBOREncode_AddTURI(QCBOREncodeContext *pCtx,
1288 uint8_t uTagRequirement,
1289 UsefulBufC URI);
1290
1291static void QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pCtx,
1292 const char *szLabel,
1293 uint8_t uTagRequirement,
1294 UsefulBufC URI);
1295
1296static void QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pCtx,
1297 int64_t nLabel,
1298 uint8_t uTagRequirement,
1299 UsefulBufC URI);
1300
1301
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001302static void QCBOREncode_AddURI(QCBOREncodeContext *pCtx,
1303 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001304
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001305static void QCBOREncode_AddURIToMap(QCBOREncodeContext *pCtx,
1306 const char *szLabel,
1307 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001308
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001309static void QCBOREncode_AddURIToMapN(QCBOREncodeContext *pCtx,
1310 int64_t nLabel,
1311 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001312
1313
1314/**
1315 @brief Add Base64-encoded text to encoded output.
1316
1317 @param[in] pCtx The encoding context to add the base-64 text to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001318 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001319 @param[in] B64Text Pointer and length of the base-64 encoded text.
1320
1321 The text content is Base64 encoded data per [RFC 4648]
1322 (https://tools.ietf.org/html/rfc4648).
1323
1324 It is output as CBOR major type 3, a text string, with tag @ref
1325 CBOR_TAG_B64 indicating the text string is Base64 encoded.
1326 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001327static void QCBOREncode_AddTB64Text(QCBOREncodeContext *pCtx,
1328 uint8_t uTagRequirement,
1329 UsefulBufC B64Text);
1330
1331static void QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pCtx,
1332 const char *szLabel,
1333 uint8_t uTagRequirement,
1334 UsefulBufC B64Text);
1335
1336static void QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pCtx,
1337 int64_t nLabel,
1338 uint8_t uTagRequirement,
1339 UsefulBufC B64Text);
1340
1341
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001342static void QCBOREncode_AddB64Text(QCBOREncodeContext *pCtx,
1343 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001344
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001345static void QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pCtx,
1346 const char *szLabel,
1347 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001348
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001349static void QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pCtx,
1350 int64_t nLabel,
1351 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001352
1353
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001354
Michael Eckel5c531332020-03-02 01:35:30 +01001355/**
1356 @brief Add base64url encoded data to encoded output.
1357
1358 @param[in] pCtx The encoding context to add the base64url to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001359 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001360 @param[in] B64Text Pointer and length of the base64url encoded text.
1361
1362 The text content is base64URL encoded text as per [RFC 4648]
1363 (https://tools.ietf.org/html/rfc4648).
1364
1365 It is output as CBOR major type 3, a text string, with tag @ref
1366 CBOR_TAG_B64URL indicating the text string is a Base64url encoded.
1367 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001368static void QCBOREncode_AddTB64URLText(QCBOREncodeContext *pCtx,
1369 uint8_t uTagRequirement,
1370 UsefulBufC B64Text);
1371
1372static void QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pCtx,
1373 const char *szLabel,
1374 uint8_t uTagRequirement,
1375 UsefulBufC B64Text);
1376
1377static void QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pCtx,
1378 int64_t nLabel,
1379 uint8_t uTagRequirement,
1380 UsefulBufC B64Text);
1381
1382
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001383static void QCBOREncode_AddB64URLText(QCBOREncodeContext *pCtx,
1384 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001385
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001386static void QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pCtx,
1387 const char *szLabel,
1388 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001389
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001390static void QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pCtx,
1391 int64_t nLabel,
1392 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001393
1394
1395/**
1396 @brief Add Perl Compatible Regular Expression.
1397
1398 @param[in] pCtx The encoding context to add the regular expression to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001399 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001400 @param[in] Regex Pointer and length of the regular expression.
1401
1402 The text content is Perl Compatible Regular
1403 Expressions (PCRE) / JavaScript syntax [ECMA262].
1404
1405 It is output as CBOR major type 3, a text string, with tag @ref
1406 CBOR_TAG_REGEX indicating the text string is a regular expression.
1407 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001408static void QCBOREncode_AddTRegex(QCBOREncodeContext *pCtx,
1409 uint8_t uTagRequirement,
1410 UsefulBufC Regex);
1411
1412static void QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pCtx,
1413 const char *szLabel,
1414 uint8_t uTagRequirement,
1415 UsefulBufC Regex);
1416
1417static void QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pCtx,
1418 int64_t nLabel,
1419 uint8_t uTagRequirement,
1420 UsefulBufC Regex);
1421
1422
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001423static void QCBOREncode_AddRegex(QCBOREncodeContext *pCtx,
1424 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001425
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001426static void QCBOREncode_AddRegexToMap(QCBOREncodeContext *pCtx,
1427 const char *szLabel,
1428 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001429
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001430static void QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pCtx,
1431 int64_t nLabel,
1432 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001433
1434
1435/**
Laurence Lundblade4982f412020-09-18 23:02:18 -07001436 @brief MIME encoded data to the encoded output.
Michael Eckel5c531332020-03-02 01:35:30 +01001437
Laurence Lundblade4982f412020-09-18 23:02:18 -07001438 @param[in] pCtx The encoding context to add the MIME data to.
1439 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1440 @ref QCBOR_ENCODE_AS_BORROWED.
1441 @param[in] MIMEData Pointer and length of the MIME data.
Michael Eckel5c531332020-03-02 01:35:30 +01001442
1443 The text content is in MIME format per [RFC 2045]
Laurence Lundblade4982f412020-09-18 23:02:18 -07001444 (https://tools.ietf.org/html/rfc2045) including the headers.
Michael Eckel5c531332020-03-02 01:35:30 +01001445
Laurence Lundblade4982f412020-09-18 23:02:18 -07001446 It is output as CBOR major type 2, a binary string, with tag @ref
1447 CBOR_TAG_BINARY_MIME indicating the string is MIME data. This
1448 outputs tag 257, not tag 36, as it can carry any type of MIME binary,
1449 7-bit, 8-bit, quoted-printable and base64 where tag 36 cannot.
1450
1451 Previous versions of QCBOR, those before spiffy decode, output tag
1452 36. Decoding supports both tag 36 and 257. (if the old behavior with
1453 tag 36 is needed, copy the inline functions below and change the tag
1454 number).
1455
1456 See also QCBORDecode_GetMIMEMessage() and
1457 @ref QCBOR_TYPE_BINARY_MIME.
Laurence Lundbladeadaf5c22020-09-25 10:37:09 -07001458
1459 This does no translation of line endings. See QCBOREncode_AddText()
1460 for a discussion of line endings in CBOR.
Michael Eckel5c531332020-03-02 01:35:30 +01001461 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001462static void QCBOREncode_AddTMIMEData(QCBOREncodeContext *pCtx,
1463 uint8_t uTagRequirement,
1464 UsefulBufC MIMEData);
1465
1466static void QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pCtx,
1467 const char *szLabel,
1468 uint8_t uTagRequirement,
1469 UsefulBufC MIMEData);
1470
1471static void QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pCtx,
1472 int64_t nLabel,
1473 uint8_t uTagRequirement,
1474 UsefulBufC MIMEData);
1475
1476
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001477static void QCBOREncode_AddMIMEData(QCBOREncodeContext *pCtx,
1478 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001479
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001480static void QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pCtx,
1481 const char *szLabel,
1482 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001483
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001484static void QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pCtx,
1485 int64_t nLabel,
1486 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001487
1488
1489/**
1490 @brief Add an RFC 3339 date string
1491
1492 @param[in] pCtx The encoding context to add the date to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001493 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001494 @param[in] szDate Null-terminated string with date to add.
1495
1496 The string szDate should be in the form of [RFC 3339]
1497 (https://tools.ietf.org/html/rfc3339) as defined by section 3.3 in
1498 [RFC 4287] (https://tools.ietf.org/html/rfc4287). This is as
Laurence Lundbladec02e13e2020-12-06 05:45:41 -08001499 described in section 3.4.1 in [RFC 8949]
1500 (https://tools.ietf.org/html/rfc8949).
Michael Eckel5c531332020-03-02 01:35:30 +01001501
1502 Note that this function doesn't validate the format of the date string
1503 at all. If you add an incorrect format date string, the generated
1504 CBOR will be incorrect and the receiver may not be able to handle it.
1505
1506 Error handling is the same as QCBOREncode_AddInt64().
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001507
1508 See also QCBOREncode_AddTDayString().
Michael Eckel5c531332020-03-02 01:35:30 +01001509 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001510static void QCBOREncode_AddTDateString(QCBOREncodeContext *pCtx,
1511 uint8_t uTagRequirement,
1512 const char *szDate);
1513
1514static void QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pCtx,
1515 const char *szLabel,
1516 uint8_t uTagRequirement,
1517 const char *szDate);
1518
1519static void QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pCtx,
1520 int64_t nLabel,
1521 uint8_t uTagRequirement,
1522 const char *szDate);
1523
1524
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001525static void QCBOREncode_AddDateString(QCBOREncodeContext *pCtx,
1526 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001527
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001528static void QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pCtx,
1529 const char *szLabel,
1530 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001531
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001532static void QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pCtx,
1533 int64_t nLabel,
1534 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001535
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001536
1537/**
1538 @brief Add a date-only string.
1539
1540 @param[in] pCtx The encoding context to add the date to.
1541 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1542 @ref QCBOR_ENCODE_AS_BORROWED.
1543 @param[in] szDate Null-terminated string with date to add.
1544
1545 This date format is described in
1546 [RFC 8943] (https://tools.ietf.org/html/rfc8943), but that mainly
1547 references RFC 3339. The string szDate must be in the forrm
1548 specified the ABNF for a full-date in
1549 [RFC 3339] (https://tools.ietf.org/html/rfc3339). Examples of this
1550 are "1985-04-12" and "1937-01-01". The time and the time zone are
1551 never included.
1552
1553 Note that this function doesn't validate the format of the date
1554 string at all. If you add an incorrect format date string, the
1555 generated CBOR will be incorrect and the receiver may not be able to
1556 handle it.
1557
1558 Error handling is the same as QCBOREncode_AddInt64().
1559
1560 See also QCBOREncode_AddTDateString().
1561 */
1562static void
1563QCBOREncode_AddTDaysString(QCBOREncodeContext *pCtx,
1564 uint8_t uTagRequirement,
1565 const char *szDate);
1566
1567static void
1568QCBOREncode_AddTDaysStringToMapSZ(QCBOREncodeContext *pCtx,
1569 const char *szLabel,
1570 uint8_t uTagRequirement,
1571 const char *szDate);
1572
1573static void
1574QCBOREncode_AddTDaysStringToMapN(QCBOREncodeContext *pCtx,
1575 int64_t nLabel,
1576 uint8_t uTagRequirement,
1577 const char *szDate);
1578
1579
Michael Eckel5c531332020-03-02 01:35:30 +01001580/**
1581 @brief Add a standard Boolean.
1582
1583 @param[in] pCtx The encoding context to add the Boolean to.
1584 @param[in] b true or false from @c <stdbool.h>.
1585
1586 Adds a Boolean value as CBOR major type 7.
1587
1588 Error handling is the same as QCBOREncode_AddInt64().
1589 */
1590static void QCBOREncode_AddBool(QCBOREncodeContext *pCtx, bool b);
1591
1592static void QCBOREncode_AddBoolToMap(QCBOREncodeContext *pCtx, const char *szLabel, bool b);
1593
1594static void QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, bool b);
1595
1596
1597
1598/**
1599 @brief Add a NULL to the encoded output.
1600
1601 @param[in] pCtx The encoding context to add the NULL to.
1602
1603 Adds the NULL value as CBOR major type 7.
1604
1605 This NULL doesn't have any special meaning in CBOR such as a
1606 terminating value for a string or an empty value.
1607
1608 Error handling is the same as QCBOREncode_AddInt64().
1609 */
1610static void QCBOREncode_AddNULL(QCBOREncodeContext *pCtx);
1611
1612static void QCBOREncode_AddNULLToMap(QCBOREncodeContext *pCtx, const char *szLabel);
1613
1614static void QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1615
1616
1617/**
1618 @brief Add an "undef" to the encoded output.
1619
1620 @param[in] pCtx The encoding context to add the "undef" to.
1621
1622 Adds the undef value as CBOR major type 7.
1623
1624 Note that this value will not translate to JSON.
1625
1626 This Undef doesn't have any special meaning in CBOR such as a
1627 terminating value for a string or an empty value.
1628
1629 Error handling is the same as QCBOREncode_AddInt64().
1630 */
1631static void QCBOREncode_AddUndef(QCBOREncodeContext *pCtx);
1632
1633static void QCBOREncode_AddUndefToMap(QCBOREncodeContext *pCtx, const char *szLabel);
1634
1635static void QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1636
1637
1638/**
1639 @brief Indicates that the next items added are in an array.
1640
1641 @param[in] pCtx The encoding context to open the array in.
1642
1643 Arrays are the basic CBOR aggregate or structure type. Call this
1644 function to start or open an array. Then call the various @c
1645 QCBOREncode_AddXxx() functions to add the items that go into the
1646 array. Then call QCBOREncode_CloseArray() when all items have been
1647 added. The data items in the array can be of any type and can be of
1648 mixed types.
1649
1650 Nesting of arrays and maps is allowed and supported just by calling
1651 QCBOREncode_OpenArray() again before calling
1652 QCBOREncode_CloseArray(). While CBOR has no limit on nesting, this
1653 implementation does in order to keep it smaller and simpler. The
1654 limit is @ref QCBOR_MAX_ARRAY_NESTING. This is the max number of
1655 times this can be called without calling
1656 QCBOREncode_CloseArray(). QCBOREncode_Finish() will return @ref
1657 QCBOR_ERR_ARRAY_NESTING_TOO_DEEP when it is called as this function
1658 just sets an error state and returns no value when this occurs.
1659
1660 If you try to add more than @ref QCBOR_MAX_ITEMS_IN_ARRAY items to a
1661 single array or map, @ref QCBOR_ERR_ARRAY_TOO_LONG will be returned
1662 when QCBOREncode_Finish() is called.
1663
1664 An array itself must have a label if it is being added to a map.
1665 Note that array elements do not have labels (but map elements do).
1666
1667 An array itself may be tagged by calling QCBOREncode_AddTag() before this call.
1668 */
1669static void QCBOREncode_OpenArray(QCBOREncodeContext *pCtx);
1670
1671static void QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pCtx, const char *szLabel);
1672
1673static void QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1674
1675
1676/**
1677 @brief Close an open array.
1678
1679 @param[in] pCtx The encoding context to close the array in.
1680
1681 The closes an array opened by QCBOREncode_OpenArray(). It reduces
1682 nesting level by one. All arrays (and maps) must be closed before
1683 calling QCBOREncode_Finish().
1684
1685 When an error occurs as a result of this call, the encoder records
1686 the error and enters the error state. The error will be returned when
1687 QCBOREncode_Finish() is called.
1688
1689 If this has been called more times than QCBOREncode_OpenArray(), then
1690 @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when QCBOREncode_Finish()
1691 is called.
1692
1693 If this is called and it is not an array that is currently open, @ref
1694 QCBOR_ERR_CLOSE_MISMATCH will be returned when QCBOREncode_Finish()
1695 is called.
1696 */
1697static void QCBOREncode_CloseArray(QCBOREncodeContext *pCtx);
1698
1699
1700/**
1701 @brief Indicates that the next items added are in a map.
1702
1703 @param[in] pCtx The encoding context to open the map in.
1704
1705 See QCBOREncode_OpenArray() for more information, particularly error
1706 handling.
1707
1708 CBOR maps are an aggregate type where each item in the map consists
1709 of a label and a value. They are similar to JSON objects.
1710
1711 The value can be any CBOR type including another map.
1712
1713 The label can also be any CBOR type, but in practice they are
1714 typically, integers as this gives the most compact output. They might
1715 also be text strings which gives readability and translation to JSON.
1716
1717 Every @c QCBOREncode_AddXxx() call has one version that ends with @c
1718 InMap for adding items to maps with string labels and one that ends
1719 with @c InMapN that is for adding with integer labels.
1720
Laurence Lundbladec02e13e2020-12-06 05:45:41 -08001721 RFC 8949 uses the term "key" instead of "label".
Michael Eckel5c531332020-03-02 01:35:30 +01001722
1723 If you wish to use map labels that are neither integer labels nor
1724 text strings, then just call the QCBOREncode_AddXxx() function
1725 explicitly to add the label. Then call it again to add the value.
1726
Laurence Lundbladec02e13e2020-12-06 05:45:41 -08001727 See the [RFC 8949] (https://tools.ietf.org/html/rfc8949) for a lot
Michael Eckel5c531332020-03-02 01:35:30 +01001728 more information on creating maps.
1729 */
1730static void QCBOREncode_OpenMap(QCBOREncodeContext *pCtx);
1731
1732static void QCBOREncode_OpenMapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
1733
1734static void QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1735
1736
Michael Eckel5c531332020-03-02 01:35:30 +01001737/**
1738 @brief Close an open map.
1739
1740 @param[in] pCtx The encoding context to close the map in .
1741
1742 This closes a map opened by QCBOREncode_OpenMap(). It reduces nesting
1743 level by one.
1744
1745 When an error occurs as a result of this call, the encoder records
1746 the error and enters the error state. The error will be returned when
1747 QCBOREncode_Finish() is called.
1748
1749 If this has been called more times than QCBOREncode_OpenMap(),
1750 then @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when
1751 QCBOREncode_Finish() is called.
1752
1753 If this is called and it is not a map that is currently open, @ref
1754 QCBOR_ERR_CLOSE_MISMATCH will be returned when QCBOREncode_Finish()
1755 is called.
1756 */
1757static void QCBOREncode_CloseMap(QCBOREncodeContext *pCtx);
1758
1759
1760/**
1761 @brief Indicate start of encoded CBOR to be wrapped in a bstr.
1762
1763 @param[in] pCtx The encoding context to open the bstr-wrapped CBOR in.
1764
1765 All added encoded items between this call and a call to
1766 QCBOREncode_CloseBstrWrap2() will be wrapped in a bstr. They will
1767 appear in the final output as a byte string. That byte string will
1768 contain encoded CBOR. This increases nesting level by one.
1769
1770 The typical use case is for encoded CBOR that is to be
1771 cryptographically hashed, as part of a [RFC 8152, COSE]
Laurence Lundbladec02e13e2020-12-06 05:45:41 -08001772 (https://tools.ietf.org/html/rfc8152) implementation. The
1773 wrapping byte string is taken as input by the hash function
1774 (which is why it is returned by QCBOREncode_CloseBstrWrap2()).
1775 It is also easy to recover on decoding with standard CBOR
1776 decoders.
Michael Eckel5c531332020-03-02 01:35:30 +01001777
1778 Using QCBOREncode_BstrWrap() and QCBOREncode_CloseBstrWrap2() avoids
1779 having to encode the items first in one buffer (e.g., the COSE
1780 payload) and then add that buffer as a bstr to another encoding
1781 (e.g. the COSE to-be-signed bytes, the @c Sig_structure) potentially
1782 halving the memory needed.
1783
Laurence Lundbladec02e13e2020-12-06 05:45:41 -08001784 CBOR by nature must be decoded item by item in order from the start.
1785 By wrapping some CBOR in a byte string, the decoding of that wrapped
1786 CBOR can be skipped. This is another use of wrapping, perhaps
1787 because the CBOR is large and deeply nested. Perhaps APIs for
1788 handling one defined CBOR message that is being embedded in another
1789 only take input as a byte string. Perhaps the desire is to be able
1790 to decode the out layer even in the wrapped has errors.
Michael Eckel5c531332020-03-02 01:35:30 +01001791 */
1792static void QCBOREncode_BstrWrap(QCBOREncodeContext *pCtx);
1793
1794static void QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
1795
1796static void QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1797
1798
1799/**
1800 @brief Close a wrapping bstr.
1801
1802 @param[in] pCtx The encoding context to close of bstr wrapping in.
1803 @param[in] bIncludeCBORHead Include the encoded CBOR head of the bstr
1804 as well as the bytes in @c pWrappedCBOR.
1805 @param[out] pWrappedCBOR A @ref UsefulBufC containing wrapped bytes.
1806
1807 The closes a wrapping bstr opened by QCBOREncode_BstrWrap(). It reduces
1808 nesting level by one.
1809
1810 A pointer and length of the enclosed encoded CBOR is returned in @c
1811 *pWrappedCBOR if it is not @c NULL. The main purpose of this is so
1812 this data can be hashed (e.g., with SHA-256) as part of a [RFC 8152,
1813 COSE] (https://tools.ietf.org/html/rfc8152)
1814 implementation. **WARNING**, this pointer and length should be used
1815 right away before any other calls to @c QCBOREncode_CloseXxx() as
1816 they will move data around and the pointer and length will no longer
1817 be to the correct encoded CBOR.
1818
1819 When an error occurs as a result of this call, the encoder records
1820 the error and enters the error state. The error will be returned when
1821 QCBOREncode_Finish() is called.
1822
1823 If this has been called more times than QCBOREncode_BstrWrap(), then
1824 @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when
1825 QCBOREncode_Finish() is called.
1826
1827 If this is called and it is not a wrapping bstr that is currently
1828 open, @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when
1829 QCBOREncode_Finish() is called.
1830
1831 QCBOREncode_CloseBstrWrap() is a deprecated version of this function
1832 that is equivalent to the call with @c bIncludeCBORHead @c true.
1833 */
1834void QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *pCtx, bool bIncludeCBORHead, UsefulBufC *pWrappedCBOR);
1835
1836static void QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pCtx, UsefulBufC *pWrappedCBOR);
1837
1838
1839/**
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07001840 * @brief Cancel byte string wrapping.
1841 *
1842 * @param[in] pCtx The encoding context.
1843 *
1844 * This cancels QCBOREncode_BstrWrap() making tghe encoding as if it
1845 * were never called.
1846 *
1847 * WARNING: This does not work on QCBOREncode_BstrWrapInMap()
1848 * or QCBOREncode_BstrWrapInMapN() and there is no error detection
1849 * of an attempt at their use.
1850 *
1851 * This only works if nothing has been added into the wrapped byte
1852 * string. If something has been added, this sets the error
1853 * @ref QCBOR_ERR_CANNOT_CANCEL.
1854 */
1855void QCBOREncode_CancelBstrWrap(QCBOREncodeContext *pCtx);
1856
1857
1858/**
Michael Eckel5c531332020-03-02 01:35:30 +01001859 @brief Add some already-encoded CBOR bytes.
1860
1861 @param[in] pCtx The encoding context to add the already-encode CBOR to.
1862 @param[in] Encoded The already-encoded CBOR to add to the context.
1863
1864 The encoded CBOR being added must be fully conforming CBOR. It must
1865 be complete with no arrays or maps that are incomplete. While this
1866 encoder doesn't ever produce indefinite lengths, it is OK for the
1867 raw CBOR added here to have indefinite lengths.
1868
1869 The raw CBOR added here is not checked in anyway. If it is not
1870 conforming or has open arrays or such, the final encoded CBOR
1871 will probably be wrong or not what was intended.
1872
1873 If the encoded CBOR being added here contains multiple items, they
1874 must be enclosed in a map or array. At the top level the raw
1875 CBOR must be a single data item.
1876 */
1877static void QCBOREncode_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded);
1878
1879static void QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Encoded);
1880
1881static void QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Encoded);
1882
1883
1884/**
1885 @brief Get the encoded result.
1886
1887 @param[in] pCtx The context to finish encoding with.
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08001888 @param[out] pEncodedCBOR Structure in which the pointer and length of the encoded
1889 CBOR is returned.
Michael Eckel5c531332020-03-02 01:35:30 +01001890
Laurence Lundbladeb9702452021-03-08 21:02:57 -08001891 @retval QCBOR_SUCCESS Encoded CBOR is returned.
1892
Michael Eckel5c531332020-03-02 01:35:30 +01001893 @retval QCBOR_ERR_TOO_MANY_CLOSES Nesting error
1894
1895 @retval QCBOR_ERR_CLOSE_MISMATCH Nesting error
1896
1897 @retval QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN Nesting error
1898
1899 @retval QCBOR_ERR_BUFFER_TOO_LARGE Encoded output buffer size
1900
1901 @retval QCBOR_ERR_BUFFER_TOO_SMALL Encoded output buffer size
1902
1903 @retval QCBOR_ERR_ARRAY_NESTING_TOO_DEEP Implementation limit
1904
1905 @retval QCBOR_ERR_ARRAY_TOO_LONG Implementation limit
1906
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08001907 On success, the pointer and length of the encoded CBOR are returned
1908 in @c *pEncodedCBOR. The pointer is the same pointer that was passed
1909 in to QCBOREncode_Init(). Note that it is not const when passed to
1910 QCBOREncode_Init(), but it is const when returned here. The length
1911 will be smaller than or equal to the length passed in when
1912 QCBOREncode_Init() as this is the length of the actual result, not
1913 the size of the buffer it was written to.
Michael Eckel5c531332020-03-02 01:35:30 +01001914
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08001915 If a @c NULL was passed for @c Storage.ptr when QCBOREncode_Init()
1916 was called, @c NULL will be returned here, but the length will be
1917 that of the CBOR that would have been encoded.
Michael Eckel5c531332020-03-02 01:35:30 +01001918
1919 Encoding errors primarily manifest here as most other encoding function
1920 do no return an error. They just set the error state in the encode
1921 context after which no encoding function does anything.
1922
1923 Three types of errors manifest here. The first type are nesting
1924 errors where the number of @c QCBOREncode_OpenXxx() calls do not
1925 match the number @c QCBOREncode_CloseXxx() calls. The solution is to
1926 fix the calling code.
1927
1928 The second type of error is because the buffer given is either too
1929 small or too large. The remedy is to give a correctly sized buffer.
1930
1931 The third type are due to limits in this implementation. @ref
1932 QCBOR_ERR_ARRAY_NESTING_TOO_DEEP can be worked around by encoding the
1933 CBOR in two (or more) phases and adding the CBOR from the first phase
1934 to the second with @c QCBOREncode_AddEncoded().
1935
1936 If an error is returned, the buffer may have partially encoded
1937 incorrect CBOR in it and it should not be used. Likewise, the length
1938 may be incorrect and should not be used.
1939
1940 Note that the error could have occurred in one of the many @c
1941 QCBOREncode_AddXxx() calls long before QCBOREncode_Finish() was
1942 called. This error handling reduces the CBOR implementation size but
1943 makes debugging harder.
1944
1945 This may be called multiple times. It will always return the same. It
1946 can also be interleaved with calls to QCBOREncode_FinishGetSize().
1947
1948 QCBOREncode_GetErrorState() can be called to get the current
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08001949 error state in order to abort encoding early as an optimization, but
1950 calling it is is never required.
Michael Eckel5c531332020-03-02 01:35:30 +01001951 */
1952QCBORError QCBOREncode_Finish(QCBOREncodeContext *pCtx, UsefulBufC *pEncodedCBOR);
1953
1954
1955/**
1956 @brief Get the encoded CBOR and error status.
1957
1958 @param[in] pCtx The context to finish encoding with.
1959 @param[out] uEncodedLen The length of the encoded or potentially
1960 encoded CBOR in bytes.
1961
1962 @return The same errors as QCBOREncode_Finish().
1963
1964 This functions the same as QCBOREncode_Finish(), but only returns the
1965 size of the encoded output.
1966 */
1967QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *pCtx, size_t *uEncodedLen);
1968
1969
1970/**
1971 @brief Indicate whether output buffer is NULL or not.
1972
1973 @param[in] pCtx The encoding context.
1974
1975 @return 1 if the output buffer is @c NULL.
1976
1977 Sometimes a @c NULL input buffer is given to QCBOREncode_Init() so
1978 that the size of the generated CBOR can be calculated without
1979 allocating a buffer for it. This returns 1 when the output buffer is
1980 NULL and 0 when it is not.
1981*/
1982static int QCBOREncode_IsBufferNULL(QCBOREncodeContext *pCtx);
1983
Laurence Lundblade825164e2020-10-22 20:18:06 -07001984
1985/**
Michael Eckel5c531332020-03-02 01:35:30 +01001986 @brief Get the encoding error state.
1987
1988 @param[in] pCtx The encoding context.
1989
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07001990 @return One of @ref QCBORError. See return values from
Michael Eckel5c531332020-03-02 01:35:30 +01001991 QCBOREncode_Finish()
1992
1993 Normally encoding errors need only be handled at the end of encoding
1994 when QCBOREncode_Finish() is called. This can be called to get the
1995 error result before finish should there be a need to halt encoding
1996 before QCBOREncode_Finish() is called.
1997*/
1998static QCBORError QCBOREncode_GetErrorState(QCBOREncodeContext *pCtx);
1999
2000
2001/**
2002 Encode the "head" of a CBOR data item.
2003
2004 @param buffer Buffer to output the encoded head to; must be
2005 @ref QCBOR_HEAD_BUFFER_SIZE bytes in size.
2006 @param uMajorType One of CBOR_MAJOR_TYPE_XX.
2007 @param uMinLen The minimum number of bytes to encode uNumber. Almost always
2008 this is 0 to use preferred minimal encoding. If this is 4,
2009 then even the values 0xffff and smaller will be encoded
Laurence Lundblade98427e92020-09-28 21:33:23 -07002010 in 4 bytes. This is used primarily when encoding a
Michael Eckel5c531332020-03-02 01:35:30 +01002011 float or double put into uNumber as the leading zero bytes
2012 for them must be encoded.
2013 @param uNumber The numeric argument part of the CBOR head.
2014 @return Pointer and length of the encoded head or
Laurence Lundblade9a3b6252020-10-21 21:30:31 -07002015 @ref NULLUsefulBufC if the output buffer is too small.
Michael Eckel5c531332020-03-02 01:35:30 +01002016
Laurence Lundblade98427e92020-09-28 21:33:23 -07002017 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 +01002018 take a @ref QCBOREncodeContext argument.
2019
2020 This encodes the major type and argument part of a data item. The
2021 argument is an integer that is usually either the value or the length
2022 of the data item.
2023
2024 This is exposed in the public interface to allow hashing of some CBOR
2025 data types, bstr in particular, a chunk at a time so the full CBOR
2026 doesn't have to be encoded in a contiguous buffer.
2027
2028 For example, if you have a 100,000 byte binary blob in a buffer that
2029 needs to be a bstr encoded and then hashed. You could allocate a
2030 100,010 byte buffer and encode it normally. Alternatively, you can
2031 encode the head in a 10 byte buffer with this function, hash that and
2032 then hash the 100,000 bytes using the same hash context.
2033
2034 See also QCBOREncode_AddBytesLenOnly();
2035 */
2036UsefulBufC QCBOREncode_EncodeHead(UsefulBuf buffer,
2037 uint8_t uMajorType,
2038 uint8_t uMinLen,
2039 uint64_t uNumber);
2040
2041
Michael Eckel5c531332020-03-02 01:35:30 +01002042
2043
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002044/* =========================================================================
2045 BEGINNING OF PRIVATE INLINE IMPLEMENTATION
2046 ========================================================================= */
Michael Eckel5c531332020-03-02 01:35:30 +01002047
2048/**
2049 @brief Semi-private method to add a buffer full of bytes to encoded output
2050
2051 @param[in] pCtx The encoding context to add the integer to.
2052 @param[in] uMajorType The CBOR major type of the bytes.
2053 @param[in] Bytes The bytes to add.
2054
2055 Use QCBOREncode_AddText() or QCBOREncode_AddBytes() or
2056 QCBOREncode_AddEncoded() instead. They are inline functions that call
2057 this and supply the correct major type. This function is public to
2058 make the inline functions work to keep the overall code size down and
2059 because the C language has no way to make it private.
2060
2061 If this is called the major type should be @c
2062 CBOR_MAJOR_TYPE_TEXT_STRING, @c CBOR_MAJOR_TYPE_BYTE_STRING or @c
2063 CBOR_MAJOR_NONE_TYPE_RAW. The last one is special for adding
2064 already-encoded CBOR.
2065 */
2066void QCBOREncode_AddBuffer(QCBOREncodeContext *pCtx, uint8_t uMajorType, UsefulBufC Bytes);
2067
2068
2069/**
2070 @brief Semi-private method to open a map, array or bstr-wrapped CBOR
2071
2072 @param[in] pCtx The context to add to.
2073 @param[in] uMajorType The major CBOR type to close
2074
2075 Call QCBOREncode_OpenArray(), QCBOREncode_OpenMap() or
2076 QCBOREncode_BstrWrap() instead of this.
2077 */
2078void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *pCtx, uint8_t uMajorType);
2079
2080
2081/**
2082 @brief Semi-private method to open a map, array with indefinite length
2083
2084 @param[in] pCtx The context to add to.
2085 @param[in] uMajorType The major CBOR type to close
2086
2087 Call QCBOREncode_OpenArrayIndefiniteLength() or
2088 QCBOREncode_OpenMapIndefiniteLength() instead of this.
2089 */
2090void QCBOREncode_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx, uint8_t uMajorType);
2091
2092
2093/**
2094 @brief Semi-private method to close a map, array or bstr wrapped CBOR
2095
2096 @param[in] pCtx The context to add to.
2097 @param[in] uMajorType The major CBOR type to close.
2098
2099 Call QCBOREncode_CloseArray() or QCBOREncode_CloseMap() instead of this.
2100 */
2101void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *pCtx, uint8_t uMajorType);
2102
2103
2104/**
2105 @brief Semi-private method to close a map, array with indefinite length
2106
2107 @param[in] pCtx The context to add to.
2108 @param[in] uMajorType The major CBOR type to close.
2109
2110 Call QCBOREncode_CloseArrayIndefiniteLength() or
2111 QCBOREncode_CloseMapIndefiniteLength() instead of this.
2112 */
2113void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx,
2114 uint8_t uMajorType);
2115
2116
2117/**
2118 @brief Semi-private method to add simple types.
2119
2120 @param[in] pCtx The encoding context to add the simple value to.
2121 @param[in] uMinLen Minimum encoding size for uNum. Usually 0.
2122 @param[in] uNum One of CBOR_SIMPLEV_FALSE through _UNDEF or other.
2123
2124 This is used to add simple types like true and false.
2125
2126 Call QCBOREncode_AddBool(), QCBOREncode_AddNULL(),
2127 QCBOREncode_AddUndef() instead of this.
2128
2129 This function can add simple values that are not defined by CBOR
2130 yet. This expansion point in CBOR should not be used unless they are
2131 standardized.
2132
2133 Error handling is the same as QCBOREncode_AddInt64().
2134 */
2135void QCBOREncode_AddType7(QCBOREncodeContext *pCtx, uint8_t uMinLen, uint64_t uNum);
2136
2137
2138/**
2139 @brief Semi-private method to add bigfloats and decimal fractions.
2140
Laurence Lundblade1fa579b2020-11-25 00:31:37 -08002141 @param[in] pCtx The encoding context to add the value to.
2142 @param[in] uTag The type 6 tag indicating what this is to be.
2143 @param[in] BigNumMantissa Is @ref NULLUsefulBufC if mantissa is an
2144 @c int64_t or the actual big number mantissa
2145 if not.
2146 @param[in] bBigNumIsNegative This is @c true if the big number is negative.
2147 @param[in] nMantissa The @c int64_t mantissa if it is not a big number.
2148 @param[in] nExponent The exponent.
Michael Eckel5c531332020-03-02 01:35:30 +01002149
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002150 This outputs either the @ref CBOR_TAG_DECIMAL_FRACTION or @ref
2151 CBOR_TAG_BIGFLOAT tag. if @c uTag is @ref CBOR_TAG_INVALID64, then
2152 this outputs the "borrowed" content format.
Michael Eckel5c531332020-03-02 01:35:30 +01002153
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002154 The tag content output by this is an array with two members, the
2155 exponent and then the mantissa. The mantissa can be either a big
2156 number or an @c int64_t.
2157
2158 This implementation cannot output an exponent further from 0 than
Laurence Lundblade9a3b6252020-10-21 21:30:31 -07002159 @c INT64_MAX.
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002160
Laurence Lundblade1fa579b2020-11-25 00:31:37 -08002161 To output a mantissa that is between INT64_MAX and UINT64_MAX from 0,
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002162 it must be as a big number.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002163
Michael Eckel5c531332020-03-02 01:35:30 +01002164 Typically, QCBOREncode_AddDecimalFraction(), QCBOREncode_AddBigFloat(),
2165 QCBOREncode_AddDecimalFractionBigNum() or QCBOREncode_AddBigFloatBigNum()
2166 is called instead of this.
2167 */
2168void QCBOREncode_AddExponentAndMantissa(QCBOREncodeContext *pCtx,
2169 uint64_t uTag,
2170 UsefulBufC BigNumMantissa,
2171 bool bBigNumIsNegative,
2172 int64_t nMantissa,
2173 int64_t nExponent);
2174
2175/**
2176 @brief Semi-private method to add only the type and length of a byte string.
2177
2178 @param[in] pCtx The context to initialize.
2179 @param[in] Bytes Pointer and length of the input data.
2180
2181 This is the same as QCBOREncode_AddBytes() except it only adds the
2182 CBOR encoding for the type and the length. It doesn't actually add
2183 the bytes. You can't actually produce correct CBOR with this and the
2184 rest of this API. It is only used for a special case where
2185 the valid CBOR is created manually by putting this type and length in
2186 and then adding the actual bytes. In particular, when only a hash of
2187 the encoded CBOR is needed, where the type and header are hashed
2188 separately and then the bytes is hashed. This makes it possible to
2189 implement COSE Sign1 with only one copy of the payload in the output
2190 buffer, rather than two, roughly cutting memory use in half.
2191
2192 This is only used for this odd case, but this is a supported
2193 tested function.
2194
2195 See also QCBOREncode_EncodeHead().
2196*/
2197static inline void QCBOREncode_AddBytesLenOnly(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
2198
2199static inline void QCBOREncode_AddBytesLenOnlyToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
2200
2201static inline void QCBOREncode_AddBytesLenOnlyToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
2202
2203
2204
2205
2206
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002207static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002208QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pMe, const char *szLabel, int64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002209{
2210 // Use _AddBuffer() because _AddSZString() is defined below, not above
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002211 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_TYPE_TEXT_STRING, UsefulBuf_FromSZ(szLabel));
2212 QCBOREncode_AddInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002213}
2214
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002215static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002216QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pMe, int64_t nLabel, int64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002217{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002218 QCBOREncode_AddInt64(pMe, nLabel);
2219 QCBOREncode_AddInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002220}
2221
2222
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002223static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002224QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pMe, const char *szLabel, uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002225{
2226 // Use _AddBuffer() because _AddSZString() is defined below, not above
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002227 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_TYPE_TEXT_STRING, UsefulBuf_FromSZ(szLabel));
2228 QCBOREncode_AddUInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002229}
2230
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002231static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002232QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002233{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002234 QCBOREncode_AddInt64(pMe, nLabel);
2235 QCBOREncode_AddUInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002236}
2237
2238
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002239static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002240QCBOREncode_AddText(QCBOREncodeContext *pMe, UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002241{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002242 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_TYPE_TEXT_STRING, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002243}
2244
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002245static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002246QCBOREncode_AddTextToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002247{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002248 QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szLabel));
2249 QCBOREncode_AddText(pMe, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002250}
2251
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002252static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002253QCBOREncode_AddTextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002254{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002255 QCBOREncode_AddInt64(pMe, nLabel);
2256 QCBOREncode_AddText(pMe, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002257}
2258
2259
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002260inline static void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002261QCBOREncode_AddSZString(QCBOREncodeContext *pMe, const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002262{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002263 QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szString));
Michael Eckel5c531332020-03-02 01:35:30 +01002264}
2265
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002266static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002267QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pMe, const char *szLabel, const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002268{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002269 QCBOREncode_AddSZString(pMe, szLabel);
2270 QCBOREncode_AddSZString(pMe, szString);
Michael Eckel5c531332020-03-02 01:35:30 +01002271}
2272
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002273static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002274QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pMe, int64_t nLabel, const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002275{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002276 QCBOREncode_AddInt64(pMe, nLabel);
2277 QCBOREncode_AddSZString(pMe, szString);
Michael Eckel5c531332020-03-02 01:35:30 +01002278}
2279
2280
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002281#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002282static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002283QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pMe, const char *szLabel, double dNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002284{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002285 QCBOREncode_AddSZString(pMe, szLabel);
2286 QCBOREncode_AddDouble(pMe, dNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002287}
2288
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002289static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002290QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pMe, int64_t nLabel, double dNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002291{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002292 QCBOREncode_AddInt64(pMe, nLabel);
2293 QCBOREncode_AddDouble(pMe, dNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002294}
2295
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002296static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002297QCBOREncode_AddFloatToMap(QCBOREncodeContext *pMe, const char *szLabel, float dNum)
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002298{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002299 QCBOREncode_AddSZString(pMe, szLabel);
2300 QCBOREncode_AddFloat(pMe, dNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002301}
2302
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002303static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002304QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pMe, int64_t nLabel, float fNum)
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002305{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002306 QCBOREncode_AddInt64(pMe, nLabel);
2307 QCBOREncode_AddFloat(pMe, fNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002308}
2309
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002310static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002311QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pMe, const char *szLabel, double dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002312{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002313 QCBOREncode_AddSZString(pMe, szLabel);
2314 QCBOREncode_AddDoubleNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002315}
2316
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002317static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002318QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pMe, int64_t nLabel, double dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002319{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002320 QCBOREncode_AddInt64(pMe, nLabel);
2321 QCBOREncode_AddDoubleNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002322}
2323
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002324static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002325QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pMe, const char *szLabel, float dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002326{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002327 QCBOREncode_AddSZString(pMe, szLabel);
2328 QCBOREncode_AddFloatNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002329}
2330
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002331static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002332QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pMe, int64_t nLabel, float dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002333{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002334 QCBOREncode_AddInt64(pMe, nLabel);
2335 QCBOREncode_AddFloatNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002336}
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002337#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002338
Michael Eckel5c531332020-03-02 01:35:30 +01002339
Laurence Lundblade9b334962020-08-27 10:55:53 -07002340
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002341static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002342QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pMe, uint8_t uTag, int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002343{
2344 if(uTag == QCBOR_ENCODE_AS_TAG) {
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002345 QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_EPOCH);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002346 }
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002347 QCBOREncode_AddInt64(pMe, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002348}
2349
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002350static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002351QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uTag, int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002352{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002353 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002354 QCBOREncode_AddTDateEpoch(pMe, uTag, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002355}
2356
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002357static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002358QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTag, int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002359{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002360 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002361 QCBOREncode_AddTDateEpoch(pMe, uTag, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002362}
2363
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002364static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002365QCBOREncode_AddDateEpoch(QCBOREncodeContext *pMe, int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002366{
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002367 QCBOREncode_AddTDateEpoch(pMe, QCBOR_ENCODE_AS_TAG, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002368}
2369
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002370static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002371QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pMe, const char *szLabel, int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002372{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002373 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002374 QCBOREncode_AddDateEpoch(pMe, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002375}
2376
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002377static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002378QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pMe, int64_t nLabel, int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002379{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002380 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002381 QCBOREncode_AddDateEpoch(pMe, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002382}
2383
2384
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002385static inline void
2386QCBOREncode_AddTDaysEpoch(QCBOREncodeContext *pMe, uint8_t uTag, int64_t nDays)
2387{
2388 if(uTag == QCBOR_ENCODE_AS_TAG) {
2389 QCBOREncode_AddTag(pMe, CBOR_TAG_DAYS_EPOCH);
2390 }
2391 QCBOREncode_AddInt64(pMe, nDays);
2392}
2393
2394static inline void
2395QCBOREncode_AddTDaysEpochToMapSZ(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uTag, int64_t nDays)
2396{
2397 QCBOREncode_AddSZString(pMe, szLabel);
2398 QCBOREncode_AddTDaysEpoch(pMe, uTag, nDays);
2399}
2400
2401static inline void
2402QCBOREncode_AddTDaysEpochToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTag, int64_t nDays)
2403{
2404 QCBOREncode_AddInt64(pMe, nLabel);
2405 QCBOREncode_AddTDaysEpoch(pMe, uTag, nDays);
2406}
2407
Laurence Lundblade9b334962020-08-27 10:55:53 -07002408
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002409static inline void
2410QCBOREncode_AddBytes(QCBOREncodeContext *pMe, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002411{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002412 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002413}
2414
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002415static inline void
2416QCBOREncode_AddBytesToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002417{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002418 QCBOREncode_AddSZString(pMe, szLabel);
2419 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002420}
2421
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002422static inline void
2423QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002424{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002425 QCBOREncode_AddInt64(pMe, nLabel);
2426 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002427}
2428
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002429static inline void
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002430QCBOREncode_OpenBytesInMapSZ(QCBOREncodeContext *pMe, const char *szLabel, UsefulBuf *pPlace)
2431{
2432 QCBOREncode_AddSZString(pMe, szLabel);
2433 QCBOREncode_OpenBytes(pMe, pPlace);
2434}
2435
2436static inline void
2437QCBOREncode_OpenBytesInMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBuf *pPlace)
2438{
2439 QCBOREncode_AddInt64(pMe, nLabel);
2440 QCBOREncode_OpenBytes(pMe, pPlace);
2441}
2442
2443static inline void
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002444QCBOREncode_AddBytesLenOnly(QCBOREncodeContext *pMe, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002445{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002446 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002447}
2448
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002449static inline void
2450QCBOREncode_AddBytesLenOnlyToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002451{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002452 QCBOREncode_AddSZString(pMe, szLabel);
2453 QCBOREncode_AddBytesLenOnly(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002454}
2455
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002456static inline void
2457QCBOREncode_AddBytesLenOnlyToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002458{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002459 QCBOREncode_AddInt64(pMe, nLabel);
2460 QCBOREncode_AddBytesLenOnly(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002461}
2462
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002463
2464static inline void
2465QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002466{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002467 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2468 QCBOREncode_AddTag(pMe, CBOR_TAG_BIN_UUID);
2469 }
2470 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002471}
2472
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002473static inline void
2474QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pMe,
2475 const char *szLabel,
2476 uint8_t uTagRequirement,
2477 UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002478{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002479 QCBOREncode_AddSZString(pMe, szLabel);
2480 QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002481}
2482
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002483static inline void
2484QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002485{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002486 QCBOREncode_AddInt64(pMe, nLabel);
2487 QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002488}
2489
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002490static inline void
2491QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pMe, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002492{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002493 QCBOREncode_AddTBinaryUUID(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002494}
2495
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002496static inline void
2497QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002498{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002499 QCBOREncode_AddTBinaryUUIDToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002500}
2501
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002502static inline void
2503QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002504{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002505 QCBOREncode_AddTBinaryUUIDToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002506}
2507
2508
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002509static inline void
2510QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002511{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002512 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2513 QCBOREncode_AddTag(pMe, CBOR_TAG_POS_BIGNUM);
2514 }
2515 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002516}
2517
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002518static inline void
2519QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pMe,
2520 const char *szLabel,
2521 uint8_t uTagRequirement,
2522 UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002523{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002524 QCBOREncode_AddSZString(pMe, szLabel);
2525 QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002526}
2527
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002528static inline void
2529QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002530{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002531 QCBOREncode_AddInt64(pMe, nLabel);
2532 QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002533}
2534
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002535static inline void
2536QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pMe, UsefulBufC Bytes)
2537{
2538 QCBOREncode_AddTPositiveBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
2539}
2540
2541static inline void
2542QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
2543{
2544 QCBOREncode_AddTPositiveBignumToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2545}
2546
2547static inline void
2548QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
2549{
2550 QCBOREncode_AddTPositiveBignumToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2551}
2552
2553
2554static inline void
2555QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
2556{
2557 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2558 QCBOREncode_AddTag(pMe, CBOR_TAG_NEG_BIGNUM);
2559 }
2560 QCBOREncode_AddBytes(pMe, Bytes);
2561}
2562
2563static inline void
2564QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pMe,
2565 const char *szLabel,
2566 uint8_t uTagRequirement,
2567 UsefulBufC Bytes)
2568{
2569 QCBOREncode_AddSZString(pMe, szLabel);
2570 QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
2571}
2572
2573static inline void
2574QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
2575{
2576 QCBOREncode_AddInt64(pMe, nLabel);
2577 QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
2578}
2579
2580static inline void
2581QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pMe, UsefulBufC Bytes)
2582{
2583 QCBOREncode_AddTNegativeBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
2584}
2585
2586static inline void
2587QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
2588{
2589 QCBOREncode_AddTNegativeBignumToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2590}
2591
2592static inline void
2593QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
2594{
2595 QCBOREncode_AddTNegativeBignumToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2596}
2597
2598
Michael Eckel5c531332020-03-02 01:35:30 +01002599
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002600#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Michael Eckel5c531332020-03-02 01:35:30 +01002601
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002602static inline void
2603QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pMe,
2604 uint8_t uTagRequirement,
2605 int64_t nMantissa,
2606 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002607{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002608 uint64_t uTag;
2609 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2610 uTag = CBOR_TAG_DECIMAL_FRACTION;
2611 } else {
2612 uTag = CBOR_TAG_INVALID64;
2613 }
2614 QCBOREncode_AddExponentAndMantissa(pMe,
2615 uTag,
Michael Eckel5c531332020-03-02 01:35:30 +01002616 NULLUsefulBufC,
2617 false,
2618 nMantissa,
2619 nBase10Exponent);
2620}
2621
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002622static inline void
2623QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pMe,
2624 const char *szLabel,
2625 uint8_t uTagRequirement,
2626 int64_t nMantissa,
2627 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002628{
2629 QCBOREncode_AddSZString(pMe, szLabel);
2630 QCBOREncode_AddTDecimalFraction(pMe, uTagRequirement, nMantissa, nBase10Exponent);
2631}
2632
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002633static inline void
2634QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pMe,
2635 int64_t nLabel,
2636 uint8_t uTagRequirement,
2637 int64_t nMantissa,
2638 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002639{
2640 QCBOREncode_AddInt64(pMe, nLabel);
2641 QCBOREncode_AddTDecimalFraction(pMe, uTagRequirement, nMantissa, nBase10Exponent);
2642}
2643
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002644static inline void
2645QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pMe,
2646 int64_t nMantissa,
2647 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002648{
2649 QCBOREncode_AddTDecimalFraction(pMe, QCBOR_ENCODE_AS_TAG, nMantissa, nBase10Exponent);
2650}
2651
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002652static inline void
2653QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pMe,
2654 const char *szLabel,
2655 int64_t nMantissa,
2656 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002657{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002658 QCBOREncode_AddTDecimalFractionToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002659}
2660
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002661static inline void
2662QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pMe,
2663 int64_t nLabel,
2664 int64_t nMantissa,
2665 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002666{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002667 QCBOREncode_AddTDecimalFractionToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002668}
2669
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002670
2671
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002672static inline void
2673QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pMe,
2674 uint8_t uTagRequirement,
2675 UsefulBufC Mantissa,
2676 bool bIsNegative,
2677 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002678{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002679 uint64_t uTag;
2680 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2681 uTag = CBOR_TAG_DECIMAL_FRACTION;
2682 } else {
2683 uTag = CBOR_TAG_INVALID64;
2684 }
2685 QCBOREncode_AddExponentAndMantissa(pMe,
2686 uTag,
Michael Eckel5c531332020-03-02 01:35:30 +01002687 Mantissa, bIsNegative,
2688 0,
2689 nBase10Exponent);
2690}
2691
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002692static inline void
2693QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
2694 const char *szLabel,
2695 uint8_t uTagRequirement,
2696 UsefulBufC Mantissa,
2697 bool bIsNegative,
2698 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002699{
2700 QCBOREncode_AddSZString(pMe, szLabel);
2701 QCBOREncode_AddTDecimalFractionBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase10Exponent);
2702}
2703
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002704static inline void
2705QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
2706 int64_t nLabel,
2707 uint8_t uTagRequirement,
2708 UsefulBufC Mantissa,
2709 bool bIsNegative,
2710 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002711{
2712 QCBOREncode_AddInt64(pMe, nLabel);
2713 QCBOREncode_AddTDecimalFractionBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase10Exponent);
2714}
2715
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002716static inline void
2717QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pMe,
2718 UsefulBufC Mantissa,
2719 bool bIsNegative,
2720 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002721{
2722 QCBOREncode_AddTDecimalFractionBigNum(pMe, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase10Exponent);
2723}
2724
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002725static inline void
2726QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
2727 const char *szLabel,
2728 UsefulBufC Mantissa,
2729 bool bIsNegative,
2730 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002731{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002732 QCBOREncode_AddTDecimalFractionBigNumToMapSZ(pMe,
2733 szLabel,
2734 QCBOR_ENCODE_AS_TAG,
2735 Mantissa,
2736 bIsNegative,
2737 nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002738}
2739
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002740static inline void
2741QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
2742 int64_t nLabel,
2743 UsefulBufC Mantissa,
2744 bool bIsNegative,
2745 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002746{
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002747 QCBOREncode_AddTDecimalFractionBigNumToMapN(pMe,
2748 nLabel,
2749 QCBOR_ENCODE_AS_TAG,
2750 Mantissa,
2751 bIsNegative,
2752 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002753}
2754
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002755
2756
2757
2758
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002759static inline void
2760QCBOREncode_AddTBigFloat(QCBOREncodeContext *pMe,
2761 uint8_t uTagRequirement,
2762 int64_t nMantissa,
2763 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002764{
2765 uint64_t uTag;
2766 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2767 uTag = CBOR_TAG_BIGFLOAT;
2768 } else {
2769 uTag = CBOR_TAG_INVALID64;
2770 }
2771 QCBOREncode_AddExponentAndMantissa(pMe, uTag, NULLUsefulBufC, false, nMantissa, nBase2Exponent);
2772}
2773
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002774static inline void
2775QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pMe,
2776 const char *szLabel,
2777 uint8_t uTagRequirement,
2778 int64_t nMantissa,
2779 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002780{
2781 QCBOREncode_AddSZString(pMe, szLabel);
2782 QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
2783}
2784
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002785static inline void
2786QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pMe,
2787 int64_t nLabel,
2788 uint8_t uTagRequirement,
2789 int64_t nMantissa,
2790 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002791{
2792 QCBOREncode_AddInt64(pMe, nLabel);
2793 QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
2794}
2795
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002796static inline void
2797QCBOREncode_AddBigFloat(QCBOREncodeContext *pMe,
2798 int64_t nMantissa,
2799 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002800{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002801 QCBOREncode_AddTBigFloat(pMe, QCBOR_ENCODE_AS_TAG, nMantissa, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002802}
2803
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002804static inline void
2805QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pMe,
2806 const char *szLabel,
2807 int64_t nMantissa,
2808 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002809{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002810 QCBOREncode_AddTBigFloatToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002811}
2812
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002813static inline void
2814QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pMe,
2815 int64_t nLabel,
2816 int64_t nMantissa,
2817 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002818{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002819 QCBOREncode_AddTBigFloatToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002820}
2821
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002822
2823
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002824static inline void
2825QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pMe,
2826 uint8_t uTagRequirement,
2827 UsefulBufC Mantissa,
2828 bool bIsNegative,
2829 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002830{
2831 uint64_t uTag;
2832 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2833 uTag = CBOR_TAG_BIGFLOAT;
2834 } else {
2835 uTag = CBOR_TAG_INVALID64;
2836 }
2837 QCBOREncode_AddExponentAndMantissa(pMe, uTag, Mantissa, bIsNegative, 0, nBase2Exponent);
2838}
2839
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002840static inline void
2841QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pMe,
2842 const char *szLabel,
2843 uint8_t uTagRequirement,
2844 UsefulBufC Mantissa,
2845 bool bIsNegative,
2846 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002847{
2848 QCBOREncode_AddSZString(pMe, szLabel);
2849 QCBOREncode_AddTBigFloatBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase2Exponent);
2850}
2851
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002852static inline void
2853QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
2854 int64_t nLabel,
2855 uint8_t uTagRequirement,
2856 UsefulBufC Mantissa,
2857 bool bIsNegative,
2858 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002859{
2860 QCBOREncode_AddInt64(pMe, nLabel);
2861 QCBOREncode_AddTBigFloatBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase2Exponent);
2862}
2863
2864
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002865static inline void
2866QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pMe,
2867 UsefulBufC Mantissa,
2868 bool bIsNegative,
2869 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002870{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002871 QCBOREncode_AddTBigFloatBigNum(pMe, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002872}
2873
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002874static inline void
2875QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pMe,
2876 const char *szLabel,
2877 UsefulBufC Mantissa,
2878 bool bIsNegative,
2879 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002880{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002881 QCBOREncode_AddTBigFloatBigNumToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002882}
2883
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002884static inline void
2885QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
2886 int64_t nLabel,
2887 UsefulBufC Mantissa,
2888 bool bIsNegative,
2889 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002890{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002891 QCBOREncode_AddTBigFloatBigNumToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002892}
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07002893#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Michael Eckel5c531332020-03-02 01:35:30 +01002894
2895
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002896static inline void
2897QCBOREncode_AddTURI(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01002898{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002899 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2900 QCBOREncode_AddTag(pMe, CBOR_TAG_URI);
2901 }
2902 QCBOREncode_AddText(pMe, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01002903}
2904
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002905static inline void
2906QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uTagRequirement, UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01002907{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002908 QCBOREncode_AddSZString(pMe, szLabel);
2909 QCBOREncode_AddTURI(pMe, uTagRequirement, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01002910}
2911
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002912static inline void
2913QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01002914{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002915 QCBOREncode_AddInt64(pMe, nLabel);
2916 QCBOREncode_AddTURI(pMe, uTagRequirement, URI);
2917}
2918
2919static inline void
2920QCBOREncode_AddURI(QCBOREncodeContext *pMe, UsefulBufC URI)
2921{
2922 QCBOREncode_AddTURI(pMe, QCBOR_ENCODE_AS_TAG, URI);
2923}
2924
2925static inline void
2926QCBOREncode_AddURIToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC URI)
2927{
2928 QCBOREncode_AddTURIToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, URI);
2929}
2930
2931static inline void
2932QCBOREncode_AddURIToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC URI)
2933{
2934 QCBOREncode_AddTURIToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01002935}
2936
2937
2938
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002939static inline void
2940QCBOREncode_AddTB64Text(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002941{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002942 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2943 QCBOREncode_AddTag(pMe, CBOR_TAG_B64);
2944 }
2945 QCBOREncode_AddText(pMe, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002946}
2947
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002948static inline void
2949QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pMe,
2950 const char *szLabel,
2951 uint8_t uTagRequirement,
2952 UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002953{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002954 QCBOREncode_AddSZString(pMe, szLabel);
2955 QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002956}
2957
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002958static inline void
2959QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002960{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002961 QCBOREncode_AddInt64(pMe, nLabel);
2962 QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text);
2963}
2964
2965static inline void
2966QCBOREncode_AddB64Text(QCBOREncodeContext *pMe, UsefulBufC B64Text)
2967{
2968 QCBOREncode_AddTB64Text(pMe, QCBOR_ENCODE_AS_TAG, B64Text);
2969}
2970
2971static inline void
2972QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC B64Text)
2973{
2974 QCBOREncode_AddTB64TextToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, B64Text);
2975}
2976
2977static inline void
2978QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC B64Text)
2979{
2980 QCBOREncode_AddTB64TextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002981}
2982
2983
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002984
2985static inline void
2986QCBOREncode_AddTB64URLText(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002987{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002988 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2989 QCBOREncode_AddTag(pMe, CBOR_TAG_B64URL);
2990 }
2991 QCBOREncode_AddText(pMe, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002992}
2993
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002994static inline void
2995QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pMe,
2996 const char *szLabel,
2997 uint8_t uTagRequirement,
2998 UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002999{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003000 QCBOREncode_AddSZString(pMe, szLabel);
3001 QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003002}
3003
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003004static inline void
3005QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003006{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003007 QCBOREncode_AddInt64(pMe, nLabel);
3008 QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text);
3009}
3010
3011static inline void
3012QCBOREncode_AddB64URLText(QCBOREncodeContext *pMe, UsefulBufC B64Text)
3013{
3014 QCBOREncode_AddTB64URLText(pMe, QCBOR_ENCODE_AS_TAG, B64Text);
3015}
3016
3017static inline void
3018QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC B64Text)
3019{
3020 QCBOREncode_AddTB64URLTextToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, B64Text);
3021}
3022
3023static inline void
3024QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC B64Text)
3025{
3026 QCBOREncode_AddTB64URLTextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003027}
3028
3029
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003030
3031static inline void
3032QCBOREncode_AddTRegex(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003033{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003034 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3035 QCBOREncode_AddTag(pMe, CBOR_TAG_REGEX);
3036 }
3037 QCBOREncode_AddText(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003038}
3039
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003040static inline void
3041QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003042{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003043 QCBOREncode_AddSZString(pMe, szLabel);
3044 QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003045}
3046
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003047static inline void
3048QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003049{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003050 QCBOREncode_AddInt64(pMe, nLabel);
3051 QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes);
3052}
3053
3054static inline void
3055QCBOREncode_AddRegex(QCBOREncodeContext *pMe, UsefulBufC Bytes)
3056{
3057 QCBOREncode_AddTRegex(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
3058}
3059
3060static inline void
3061QCBOREncode_AddRegexToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
3062{
3063 QCBOREncode_AddTRegexToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
3064}
3065
3066static inline void
3067QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
3068{
3069 QCBOREncode_AddTRegexToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
3070
Michael Eckel5c531332020-03-02 01:35:30 +01003071}
3072
3073
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003074static inline void
3075QCBOREncode_AddTMIMEData(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01003076{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003077 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
Laurence Lundblade4982f412020-09-18 23:02:18 -07003078 QCBOREncode_AddTag(pMe, CBOR_TAG_BINARY_MIME);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003079 }
Laurence Lundblade4982f412020-09-18 23:02:18 -07003080 QCBOREncode_AddBytes(pMe, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01003081}
3082
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003083static inline void
3084QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pMe,
3085 const char *szLabel,
3086 uint8_t uTagRequirement,
3087 UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01003088{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003089 QCBOREncode_AddSZString(pMe, szLabel);
3090 QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01003091}
3092
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003093static inline void
3094QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01003095{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003096 QCBOREncode_AddInt64(pMe, nLabel);
3097 QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData);
3098}
3099
3100static inline void
3101QCBOREncode_AddMIMEData(QCBOREncodeContext *pMe, UsefulBufC MIMEData)
3102{
3103 QCBOREncode_AddTMIMEData(pMe, QCBOR_ENCODE_AS_TAG, MIMEData);
3104}
3105
3106static inline void
3107QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC MIMEData)
3108{
3109 QCBOREncode_AddTMIMEDataToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, MIMEData);
3110}
3111
3112static inline void
3113QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC MIMEData)
3114{
3115 QCBOREncode_AddTMIMEDataToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01003116}
3117
3118
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003119static inline void
3120QCBOREncode_AddTDateString(QCBOREncodeContext *pMe, uint8_t uTagRequirement, const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01003121{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003122 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3123 QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_STRING);
3124 }
3125 QCBOREncode_AddSZString(pMe, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01003126}
3127
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003128static inline void
3129QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pMe,
3130 const char *szLabel,
3131 uint8_t uTagRequirement,
3132 const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01003133{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003134 QCBOREncode_AddSZString(pMe, szLabel);
3135 QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01003136}
3137
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003138static inline void
3139QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01003140{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003141 QCBOREncode_AddInt64(pMe, nLabel);
3142 QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate);
3143}
3144
3145static inline void
3146QCBOREncode_AddDateString(QCBOREncodeContext *pMe, const char *szDate)
3147{
3148 QCBOREncode_AddTDateString(pMe, QCBOR_ENCODE_AS_TAG, szDate);
3149}
3150
3151static inline void
3152QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pMe, const char *szLabel, const char *szDate)
3153{
3154 QCBOREncode_AddTDateStringToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, szDate);
3155}
3156
3157static inline void
3158QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pMe, int64_t nLabel, const char *szDate)
3159{
3160 QCBOREncode_AddTDateStringToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01003161}
3162
3163
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003164static inline void
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003165QCBOREncode_AddTDaysString(QCBOREncodeContext *pMe, uint8_t uTagRequirement, const char *szDate)
3166{
3167 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3168 QCBOREncode_AddTag(pMe, CBOR_TAG_DAYS_STRING);
3169 }
3170 QCBOREncode_AddSZString(pMe, szDate);
3171}
3172
3173static inline void
3174QCBOREncode_AddTDaysStringToMapSZ(QCBOREncodeContext *pMe,
3175 const char *szLabel,
3176 uint8_t uTagRequirement,
3177 const char *szDate)
3178{
3179 QCBOREncode_AddSZString(pMe, szLabel);
3180 QCBOREncode_AddTDaysString(pMe, uTagRequirement, szDate);
3181}
3182
3183static inline void
3184QCBOREncode_AddTDaysStringToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, const char *szDate)
3185{
3186 QCBOREncode_AddInt64(pMe, nLabel);
3187 QCBOREncode_AddTDaysString(pMe, uTagRequirement, szDate);
3188}
3189
3190
3191
3192static inline void
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003193QCBOREncode_AddSimple(QCBOREncodeContext *pMe, uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01003194{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003195 QCBOREncode_AddType7(pMe, 0, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01003196}
3197
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003198static inline void
3199QCBOREncode_AddSimpleToMap(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uSimple)
Michael Eckel5c531332020-03-02 01:35:30 +01003200{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003201 QCBOREncode_AddSZString(pMe, szLabel);
3202 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01003203}
3204
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003205static inline void
3206QCBOREncode_AddSimpleToMapN(QCBOREncodeContext *pMe, int nLabel, uint8_t uSimple)
Michael Eckel5c531332020-03-02 01:35:30 +01003207{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003208 QCBOREncode_AddInt64(pMe, nLabel);
3209 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01003210}
3211
3212
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003213static inline void
3214QCBOREncode_AddBool(QCBOREncodeContext *pMe, bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01003215{
3216 uint8_t uSimple = CBOR_SIMPLEV_FALSE;
3217 if(b) {
3218 uSimple = CBOR_SIMPLEV_TRUE;
3219 }
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003220 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01003221}
3222
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003223static inline void
3224QCBOREncode_AddBoolToMap(QCBOREncodeContext *pMe, const char *szLabel, bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01003225{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003226 QCBOREncode_AddSZString(pMe, szLabel);
3227 QCBOREncode_AddBool(pMe, b);
Michael Eckel5c531332020-03-02 01:35:30 +01003228}
3229
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003230static inline void
3231QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pMe, int64_t nLabel, bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01003232{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003233 QCBOREncode_AddInt64(pMe, nLabel);
3234 QCBOREncode_AddBool(pMe, b);
Michael Eckel5c531332020-03-02 01:35:30 +01003235}
3236
3237
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003238static inline void
3239QCBOREncode_AddNULL(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003240{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003241 QCBOREncode_AddSimple(pMe, CBOR_SIMPLEV_NULL);
Michael Eckel5c531332020-03-02 01:35:30 +01003242}
3243
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003244static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003245QCBOREncode_AddNULLToMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003246{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003247 QCBOREncode_AddSZString(pMe, szLabel);
3248 QCBOREncode_AddNULL(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003249}
3250
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003251static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003252QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003253{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003254 QCBOREncode_AddInt64(pMe, nLabel);
3255 QCBOREncode_AddNULL(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003256}
3257
3258
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003259static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003260QCBOREncode_AddUndef(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003261{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003262 QCBOREncode_AddSimple(pMe, CBOR_SIMPLEV_UNDEF);
Michael Eckel5c531332020-03-02 01:35:30 +01003263}
3264
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003265static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003266QCBOREncode_AddUndefToMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003267{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003268 QCBOREncode_AddSZString(pMe, szLabel);
3269 QCBOREncode_AddUndef(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003270}
3271
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003272static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003273QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003274{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003275 QCBOREncode_AddInt64(pMe, nLabel);
3276 QCBOREncode_AddUndef(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003277}
3278
3279
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003280static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003281QCBOREncode_OpenArray(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003282{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003283 QCBOREncode_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY);
Michael Eckel5c531332020-03-02 01:35:30 +01003284}
3285
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003286static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003287QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003288{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003289 QCBOREncode_AddSZString(pMe, szLabel);
3290 QCBOREncode_OpenArray(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003291}
3292
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003293static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003294QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003295{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003296 QCBOREncode_AddInt64(pMe, nLabel);
3297 QCBOREncode_OpenArray(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003298}
3299
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003300static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003301QCBOREncode_CloseArray(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003302{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003303 QCBOREncode_CloseMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY);
Michael Eckel5c531332020-03-02 01:35:30 +01003304}
3305
3306
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003307static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003308QCBOREncode_OpenMap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003309{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003310 QCBOREncode_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_MAP);
Michael Eckel5c531332020-03-02 01:35:30 +01003311}
3312
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003313static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003314QCBOREncode_OpenMapInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003315{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003316 QCBOREncode_AddSZString(pMe, szLabel);
3317 QCBOREncode_OpenMap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003318}
3319
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003320static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003321QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003322{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003323 QCBOREncode_AddInt64(pMe, nLabel);
3324 QCBOREncode_OpenMap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003325}
3326
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003327static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003328QCBOREncode_CloseMap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003329{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003330 QCBOREncode_CloseMapOrArray(pMe, CBOR_MAJOR_TYPE_MAP);
Michael Eckel5c531332020-03-02 01:35:30 +01003331}
3332
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003333static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003334QCBOREncode_OpenArrayIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003335{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003336 QCBOREncode_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01003337}
3338
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003339static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003340QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003341{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003342 QCBOREncode_AddSZString(pMe, szLabel);
3343 QCBOREncode_OpenArrayIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003344}
3345
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003346static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003347QCBOREncode_OpenArrayIndefiniteLengthInMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003348{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003349 QCBOREncode_AddInt64(pMe, nLabel);
3350 QCBOREncode_OpenArrayIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003351}
3352
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003353static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003354QCBOREncode_CloseArrayIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003355{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003356 QCBOREncode_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01003357}
3358
3359
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003360static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003361QCBOREncode_OpenMapIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003362{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003363 QCBOREncode_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01003364}
3365
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003366static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003367QCBOREncode_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003368{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003369 QCBOREncode_AddSZString(pMe, szLabel);
3370 QCBOREncode_OpenMapIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003371}
3372
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003373static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003374QCBOREncode_OpenMapIndefiniteLengthInMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003375{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003376 QCBOREncode_AddInt64(pMe, nLabel);
3377 QCBOREncode_OpenMapIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003378}
3379
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003380static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003381QCBOREncode_CloseMapIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003382{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003383 QCBOREncode_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01003384}
3385
3386
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003387static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003388QCBOREncode_BstrWrap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003389{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003390 QCBOREncode_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_BYTE_STRING);
Michael Eckel5c531332020-03-02 01:35:30 +01003391}
3392
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003393static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003394QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003395{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003396 QCBOREncode_AddSZString(pMe, szLabel);
3397 QCBOREncode_BstrWrap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003398}
3399
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003400static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003401QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pMe, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003402{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003403 QCBOREncode_AddInt64(pMe, nLabel);
3404 QCBOREncode_BstrWrap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003405}
3406
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003407static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003408QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pMe, UsefulBufC *pWrappedCBOR)
Michael Eckel5c531332020-03-02 01:35:30 +01003409{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003410 QCBOREncode_CloseBstrWrap2(pMe, true, pWrappedCBOR);
Michael Eckel5c531332020-03-02 01:35:30 +01003411}
3412
3413
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003414static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003415QCBOREncode_AddEncoded(QCBOREncodeContext *pMe, UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01003416{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003417 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_NONE_TYPE_RAW, Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01003418}
3419
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003420static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003421QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01003422{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003423 QCBOREncode_AddSZString(pMe, szLabel);
3424 QCBOREncode_AddEncoded(pMe, Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01003425}
3426
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003427static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003428QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01003429{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003430 QCBOREncode_AddInt64(pMe, nLabel);
3431 QCBOREncode_AddEncoded(pMe, Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01003432}
3433
3434
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003435static inline int
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003436QCBOREncode_IsBufferNULL(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003437{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003438 return UsefulOutBuf_IsBufferNULL(&(pMe->OutBuf));
Michael Eckel5c531332020-03-02 01:35:30 +01003439}
3440
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003441static inline QCBORError
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003442QCBOREncode_GetErrorState(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003443{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003444 if(UsefulOutBuf_GetError(&(pMe->OutBuf))) {
Michael Eckel5c531332020-03-02 01:35:30 +01003445 // Items didn't fit in the buffer.
3446 // This check catches this condition for all the appends and inserts
3447 // so checks aren't needed when the appends and inserts are performed.
3448 // And of course UsefulBuf will never overrun the input buffer given
3449 // to it. No complex analysis of the error handling in this file is
3450 // needed to know that is true. Just read the UsefulBuf code.
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003451 pMe->uError = QCBOR_ERR_BUFFER_TOO_SMALL;
Michael Eckel5c531332020-03-02 01:35:30 +01003452 // QCBOR_ERR_BUFFER_TOO_SMALL masks other errors, but that is
3453 // OK. Once the caller fixes this, they'll be unmasked.
3454 }
3455
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003456 return (QCBORError)pMe->uError;
Michael Eckel5c531332020-03-02 01:35:30 +01003457}
3458
3459
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003460/* ========================================================================
3461 END OF PRIVATE INLINE IMPLEMENTATION
3462 ======================================================================== */
Michael Eckel5c531332020-03-02 01:35:30 +01003463
3464#ifdef __cplusplus
3465}
3466#endif
3467
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08003468#endif /* qcbor_encode_h */