blob: f27af1ff58095eb16b7b7bc4174310e1c757bb32 [file] [log] [blame]
Laurence Lundblade3eead482023-12-16 20:53:22 -07001/* ===========================================================================
2 * Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003 * Copyright (c) 2018-2024, Laurence Lundblade.
Laurence Lundblade3eead482023-12-16 20:53:22 -07004 * Copyright (c) 2021, Arm Limited.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 * * Neither the name of The Linux Foundation nor the names of its
17 * contributors, nor the name "Laurence Lundblade" may be used to
18 * endorse or promote products derived from this software without
19 * specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ========================================================================= */
Michael Eckel5c531332020-03-02 01:35:30 +010033
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 Lundblade3eead482023-12-16 20:53:22 -070053 * @file qcbor_encode.h
54 *
55 * @anchor Overview
56 *
57 * # QCBOR Overview
58 *
59 * This implements CBOR -- Concise Binary Object Representation as
60 * defined in [RFC 8949] (https://tools.ietf.org/html/rfc8949). More
61 * information is at http://cbor.io. This is a near-complete implementation of
62 * the specification. [RFC 8742] (https://tools.ietf.org/html/rfc8742) CBOR
63 * Sequences is also supported. Limitations are listed further down.
64 *
65 * See @ref Encoding for general discussion on encoding,
66 * @ref BasicDecode for general discussion on the basic decode features
67 * and @ref SpiffyDecode for general discussion on the easier-to-use
68 * decoder functions.
69 *
70 * CBOR is intentionally designed to be translatable to JSON, but not
71 * all CBOR can convert to JSON. See RFC 8949 for more info on how to
72 * construct CBOR that is the most JSON friendly.
73 *
74 * The memory model for encoding and decoding is that encoded CBOR must
75 * be in a contiguous buffer in memory. During encoding the caller must
76 * supply an output buffer and if the encoding would go off the end of
77 * the buffer an error is returned. During decoding the caller supplies
78 * the encoded CBOR in a contiguous buffer and the decoder returns
79 * pointers and lengths into that buffer for strings.
80 *
81 * This implementation does not require malloc. All data structures
82 * passed in/out of the APIs can fit on the stack.
83 *
84 * Decoding of indefinite-length strings is a special case that requires
85 * a "string allocator" to allocate memory into which the segments of
86 * the string are coalesced. Without this, decoding will error out if an
87 * indefinite-length string is encountered (indefinite-length maps and
88 * arrays do not require the string allocator). A simple string
89 * allocator called MemPool is built-in and will work if supplied with a
90 * block of memory to allocate. The string allocator can optionally use
91 * malloc() or some other custom scheme.
92 *
93 * Here are some terms and definitions:
94 *
95 * - "Item", "Data Item": An integer or string or such. The basic "thing" that
96 * CBOR is about. An array is an item itself that contains some items.
97 *
98 * - "Array": An ordered sequence of items, the same as JSON.
99 *
100 * - "Map": A collection of label/value pairs. Each pair is a data
101 * item. A JSON "object" is the same as a CBOR "map".
102 *
103 * - "Label": The data item in a pair in a map that names or identifies
104 * the pair, not the value. This implementation refers to it as a
105 * "label". JSON refers to it as the "name". The CBOR RFC refers to it
106 * this as a "key". This implementation chooses label instead because
107 * key is too easily confused with a cryptographic key. The COSE
108 * standard, which uses CBOR, has also chosen to use the term "label"
109 * rather than "key" for this same reason.
110 *
111 * - "Key": See "Label" above.
112 *
113 * - "Tag": A data item that is an explicitly labeled new data
114 * type made up of the tagging integer and the tag content.
115 * See @ref Tags-Overview and @ref Tag-Usage.
116 *
117 * - "Initial Byte": The first byte of an encoded item. Encoding and
118 * decoding of this byte is taken care of by the implementation.
119 *
120 * - "Additional Info": In addition to the major type, all data items
121 * have some other info. This is usually the length of the data but can
122 * be several other things. Encoding and decoding of this is taken care
123 * of by the implementation.
124 *
125 * CBOR has two mechanisms for tagging and labeling the data values like
126 * integers and strings. For example, an integer that represents
127 * someone's birthday in epoch seconds since Jan 1, 1970 could be
128 * encoded like this:
129 *
130 * - First it is CBOR_MAJOR_TYPE_POSITIVE_INT (@ref QCBOR_TYPE_INT64),
131 * the primitive positive integer.
132 *
133 * - Next it has a "tag" @ref CBOR_TAG_DATE_EPOCH indicating the integer
134 * represents a date in the form of the number of seconds since Jan 1,
135 * 1970.
136 *
137 * - Last it has a string "label" like "BirthDate" indicating the
138 * meaning of the data.
139 *
140 * The encoded binary looks like this:
141 *
142 * a1 # Map of 1 item
143 * 69 # Indicates text string of 9 bytes
144 * 426972746844617465 # The text "BirthDate"
145 * c1 # Tags next integer as epoch date
146 * 1a # Indicates a 4-byte integer
147 * 580d4172 # unsigned integer date 1477263730
148 *
149 * Implementors using this API will primarily work with
150 * labels. Generally, tags are only needed for making up new data
151 * types. This implementation covers most of the data types defined in
152 * the RFC using tags. It also, allows for the use of custom tags if
153 * necessary.
154 *
155 * This implementation explicitly supports labels that are text strings
156 * and integers. Text strings translate nicely into JSON objects and are
157 * very readable. Integer labels are much less readable but can be very
158 * compact. If they are in the range of 0 to 23, they take up only one
159 * byte.
160 *
161 * CBOR allows a label to be any type of data including an array or a
162 * map. It is possible to use this API to construct and parse such
163 * labels, but it is not explicitly supported.
164 *
165 * @anchor Encoding
166 *
167 * ## Encoding
168 *
169 * A common encoding usage mode is to invoke the encoding twice. First
170 * with the output buffer as @ref SizeCalculateUsefulBuf to compute the
171 * length of the needed output buffer. The correct sized output buffer
172 * is allocated. The encoder is invoked a second time with the allocated
173 * output buffer.
174 *
175 * The double invocation is not required if the maximum output buffer
176 * size can be predicted. This is usually possible for simple CBOR
177 * structures.
178 *
179 * If a buffer too small to hold the encoded output is given, the error
180 * @ref QCBOR_ERR_BUFFER_TOO_SMALL will be returned. Data will never be
181 * written off the end of the output buffer no matter which functions
182 * here are called or what parameters are passed to them.
183 *
184 * The encoding error handling is simple. The only possible errors are
185 * trying to encode structures that are too large or too complex. There
186 * are no internal malloc calls so there will be no failures for out of
187 * memory. The error state is tracked internally, so there is no need
188 * to check for errors when encoding. Only the return code from
189 * QCBOREncode_Finish() need be checked as once an error happens, the
190 * encoder goes into an error state and calls to it to add more data
191 * will do nothing. An error check is not needed after every data item
192 * is added.
193 *
194 * Encoding generally proceeds by calling QCBOREncode_Init(), calling
195 * lots of @c QCBOREncode_AddXxx() functions and calling
196 * QCBOREncode_Finish(). There are many @c QCBOREncode_AddXxx()
197 * functions for various data types. The input buffers need only to be
198 * valid during the @c QCBOREncode_AddXxx() calls as the data is copied
199 * into the output buffer.
200 *
201 * There are three `Add` functions for each data type. The first / main
202 * one for the type is for adding the data item to an array. The second
203 * one's name ends in `ToMap`, is used for adding data items to maps and
204 * takes a string argument that is its label in the map. The third one
205 * ends in `ToMapN`, is also used for adding data items to maps, and
206 * takes an integer argument that is its label in the map.
207 *
208 * The simplest aggregate type is an array, which is a simple ordered
209 * set of items without labels the same as JSON arrays. Call
210 * QCBOREncode_OpenArray() to open a new array, then various @c
211 * QCBOREncode_AddXxx() functions to put items in the array and then
212 * QCBOREncode_CloseArray(). Nesting to the limit @ref
213 * QCBOR_MAX_ARRAY_NESTING is allowed. All opens must be matched by
214 * closes or an encoding error will be returned.
215 *
216 * The other aggregate type is a map which does use labels. The `Add`
217 * functions that end in `ToMap` and `ToMapN` are convenient ways to add
218 * labeled data items to a map. You can also call any type of `Add`
219 * function once to add a label of any type and then call any type of
220 * `Add` again to add its value.
221 *
222 * Note that when you nest arrays or maps in a map, the nested array or
223 * map has a label.
224 *
225 * Many CBOR-based protocols start with an array or map. This makes them
226 * self-delimiting. No external length or end marker is needed to know
227 * the end. It is also possible not start this way, in which case this
228 * it is usually called a CBOR sequence which is described in
229 * [RFC 8742] (https://tools.ietf.org/html/rfc8742). This encoder supports
230 * either just by whether the first item added is an array, map or other.
231 *
232 * If QCBOR is compiled with QCBOR_DISABLE_ENCODE_USAGE_GUARDS defined,
233 * the errors QCBOR_ERR_CLOSE_MISMATCH, QCBOR_ERR_ARRAY_TOO_LONG,
234 * QCBOR_ERR_TOO_MANY_CLOSES, QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN, and
235 * QCBOR_ERR_ENCODE_UNSUPPORTED will never be returned. It is up to the
236 * caller to make sure that opened maps, arrays and byte-string wrapping
237 * is closed correctly and that QCBOREncode_AddType7() is called
238 * correctly. With this defined, it is easier to make a mistake when
239 * authoring the encoding of a protocol that will output not well formed
240 * CBOR, but as long as the calling code is correct, it is safe to
241 * disable these checks. Bounds checking that prevents security issues
242 * in the code is still enforced. This define reduces the size of
243 * encoding object code by about 150 bytes.
244 *
245 * @anchor Tags-Overview
246 *
247 * ## Tags Overview
248 *
249 * Any CBOR data item can be made into a tag to add semantics, define a
250 * new data type or such. Some tags are fully standardized and some are
251 * just registered. Others are not registered and used in a proprietary
252 * way.
253 *
254 * Encoding and decoding of many of the registered tags is fully
255 * implemented by QCBOR. It is also possible to encode and decode tags
256 * that are not directly supported. For many use cases the built-in tag
257 * support should be adequate.
258 *
259 * For example, the registered epoch date tag is supported in encoding
260 * by QCBOREncode_AddDateEpoch() and in decoding by @ref
261 * QCBOR_TYPE_DATE_EPOCH and the @c epochDate member of @ref
262 * QCBORItem. This is typical of the built-in tag support. There is an
263 * API to encode data for it and a @c QCBOR_TYPE_XXX when it is decoded.
264 *
265 * Tags are registered in the [IANA CBOR Tags Registry]
266 * (https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml). There
267 * are roughly three options to create a new tag. First, a public
268 * specification can be created and the new tag registered with IANA.
269 * This is the most formal. Second, the new tag can be registered with
270 * IANA with just a short description rather than a full specification.
271 * These tags must be greater than 256. Third, a tag can be used without
272 * any IANA registration, though the registry should be checked to see
273 * that the new value doesn't collide with one that is registered. The
274 * value of these tags must be 256 or larger.
275 *
276 * See also @ref CBORTags and @ref Tag-Usage
277 *
278 * The encoding side of tags not built-in is handled by
279 * QCBOREncode_AddTag() and is relatively simple. Tag decoding is more
280 * complex and mainly handled by QCBORDecode_GetNext(). Decoding of the
281 * structure of tagged data not built-in (if there is any) has to be
282 * implemented by the caller.
283 *
284 * @anchor Floating-Point
285 *
286 * ## Floating-Point
287 *
288 * By default QCBOR fully supports IEEE 754 floating-point:
289 * - Encode/decode of double, single and half-precision
290 * - CBOR preferred serialization of floating-point
291 * - Floating-point epoch dates
292 *
293 * For the most part, the type double is used in the interface for
294 * floating-point values. In the default configuration, all decoded
295 * floating-point values are returned as a double.
296 *
297 * With CBOR preferred serialization, the encoder outputs the smallest
298 * representation of the double or float that preserves precision. Zero,
299 * NaN and infinity are always output as a half-precision, each taking
300 * just 2 bytes. This reduces the number of bytes needed to encode
301 * double and single-precision, especially if zero, NaN and infinity are
302 * frequently used.
303 *
304 * To avoid use of preferred serialization in the standard configuration
305 * when encoding, use QCBOREncode_AddDoubleNoPreferred() or
306 * QCBOREncode_AddFloatNoPreferred().
307 *
308 * This implementation of preferred floating-point serialization and
309 * half-precision does not depend on the CPU having floating-point HW or
310 * the compiler bringing in a (sometimes large) library to compensate
311 * for lack of CPU support. This implementation uses shifts and masks
312 * rather than floating-point functions.
313 *
314 * To reduce overall object code by about 900 bytes, define
315 * QCBOR_DISABLE_PREFERRED_FLOAT. This will eliminate all support for
316 * preferred serialization and half-precision. An error will be returned
317 * when attempting to decode half-precision. A float will always be
318 * encoded and decoded as 32-bits and a double will always be encoded
319 * and decoded as 64 bits.
320 *
321 * Note that even if QCBOR_DISABLE_PREFERRED_FLOAT is not defined all
322 * the float-point encoding object code can be avoided by never calling
323 * any functions that encode double or float. Just not calling
324 * floating-point functions will reduce object code by about 500 bytes.
325 *
326 * On CPUs that have no floating-point hardware,
327 * QCBOR_DISABLE_FLOAT_HW_USE should be defined in most cases. If it is
328 * not, then the compiler will bring in possibly large software
329 * libraries to compensate. Defining QCBOR_DISABLE_FLOAT_HW_USE reduces
330 * object code size on CPUs with floating-point hardware by a tiny
331 * amount and eliminates the need for <math.h>
332 *
333 * When QCBOR_DISABLE_FLOAT_HW_USE is defined, trying to decoding
334 * floating-point dates will give error
335 * @ref QCBOR_ERR_FLOAT_DATE_DISABLED and decoded single-precision
336 * numbers will be returned as @ref QCBOR_TYPE_FLOAT instead of
337 * converting them to double as usual.
338 *
339 * If both QCBOR_DISABLE_FLOAT_HW_USE and QCBOR_DISABLE_PREFERRED_FLOAT
340 * are defined, then the only thing QCBOR can do is encode/decode a C
341 * float type as 32-bits and a C double type as 64-bits. Floating-point
342 * epoch dates will be unsupported.
343 *
344 * If USEFULBUF_DISABLE_ALL_FLOATis defined, then floating point
345 * support is completely disabled. Decoding functions return
346 * @ref QCBOR_ERR_ALL_FLOAT_DISABLED if a floating point value is
347 * encountered during decoding. Functions that are encoding floating
348 * point values are not available.
349 *
350 * ## Limitations
351 *
352 * Summary Limits of this implementation:
353 * - The entire encoded CBOR must fit into contiguous memory.
354 * - Max size of encoded / decoded CBOR data is a few bytes less than @c UINT32_MAX (4GB).
355 * - Max array / map nesting level when encoding / decoding is
356 * @ref QCBOR_MAX_ARRAY_NESTING (this is typically 15).
357 * - Max items in an array or map when encoding / decoding is
358 * @ref QCBOR_MAX_ITEMS_IN_ARRAY (typically 65,536).
359 * - Does not directly support labels in maps other than text strings & integers.
360 * - Does not directly support integer labels greater than @c INT64_MAX.
361 * - Epoch dates limited to @c INT64_MAX (+/- 292 billion years).
362 * - Exponents for bigfloats and decimal integers are limited to @c INT64_MAX.
363 * - Tags on labels are ignored during decoding.
364 * - The maximum tag nesting is @c QCBOR_MAX_TAGS_PER_ITEM (typically 4).
365 * - Works only on 32- and 64-bit CPUs (modifications could make it work
366 * on 16-bit CPUs).
367 *
368 * The public interface uses @c size_t for all lengths. Internally the
369 * implementation uses 32-bit lengths by design to use less memory and
370 * fit structures on the stack. This limits the encoded CBOR it can
371 * work with to size @c UINT32_MAX (4GB) which should be enough.
372 *
373 * This implementation assumes two's compliment integer machines.
374 * @c <stdint.h> also requires this. It is possible to modify this
375 * implementation for another integer representation, but all modern
376 * machines seem to be two's compliment.
Michael Eckel5c531332020-03-02 01:35:30 +0100377 */
378
379
Laurence Lundblade825164e2020-10-22 20:18:06 -0700380/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700381 * The size of the buffer to be passed to QCBOREncode_EncodeHead(). It
382 * is one byte larger than sizeof(uint64_t) + 1, the actual maximum
383 * size of the head of a CBOR data item because
384 * QCBOREncode_EncodeHead() needs one extra byte to work.
Michael Eckel5c531332020-03-02 01:35:30 +0100385 */
386#define QCBOR_HEAD_BUFFER_SIZE (sizeof(uint64_t) + 2)
387
Michael Eckel5c531332020-03-02 01:35:30 +0100388
Laurence Lundblade9b334962020-08-27 10:55:53 -0700389/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700390 * Output the full CBOR tag. See @ref CBORTags, @ref Tag-Usage and
391 * @ref Tags-Overview.
Laurence Lundblade9b334962020-08-27 10:55:53 -0700392 */
393#define QCBOR_ENCODE_AS_TAG 0
394
395/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700396 * Output only the 'borrowed' content format for the relevant tag.
397 * See @ref CBORTags, @ref Tag-Usage and @ref Tags-Overview.
Laurence Lundblade9b334962020-08-27 10:55:53 -0700398 */
399#define QCBOR_ENCODE_AS_BORROWED 1
400
Michael Eckel5c531332020-03-02 01:35:30 +0100401
402/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700403 * QCBOREncodeContext is the data type that holds context for all the
404 * encoding functions. It is less than 200 bytes, so it can go on the
405 * stack. The contents are opaque, and the caller should not access
406 * internal members. A context may be re used serially as long as it is
407 * re initialized.
Michael Eckel5c531332020-03-02 01:35:30 +0100408 */
409typedef struct _QCBOREncodeContext QCBOREncodeContext;
410
411
412/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700413 * Initialize the encoder to prepare to encode some CBOR.
414 *
415 * @param[in,out] pCtx The encoder context to initialize.
416 * @param[in] Storage The buffer into which the encoded result
417 * will be written.
418 *
419 * Call this once at the start of an encoding of some CBOR. Then call
420 * the many functions like QCBOREncode_AddInt64() and
421 * QCBOREncode_AddText() to add the different data items. Finally,
422 * call QCBOREncode_Finish() to get the pointer and length of the
423 * encoded result.
424 *
425 * The primary purpose of this function is to give the pointer and
426 * length of the output buffer into which the encoded CBOR will be
427 * written. This is done with a @ref UsefulBuf structure, which is
428 * just a pointer and length (it is equivalent to two parameters, one
429 * a pointer and one a length, but a little prettier).
430 *
431 * The output buffer can be allocated any way (malloc, stack,
432 * static). It is just some memory that QCBOR writes to. The length
433 * must be the length of the allocated buffer. QCBOR will never write
434 * past that length, but might write up to that length. If the buffer
435 * is too small, encoding will go into an error state and not write
436 * anything further.
437 *
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800438 * If allocating on the stack, the convenience macro
Laurence Lundblade3eead482023-12-16 20:53:22 -0700439 * UsefulBuf_MAKE_STACK_UB() can be used, but its use is not required.
440 *
441 * Since there is no reallocation or such, the output buffer must be
442 * correctly sized when passed in here. It is OK, but wasteful if it
443 * is too large. One way to pick the size is to figure out the maximum
444 * size that will ever be needed and hard code a buffer of that size.
445 *
446 * Another way to do it is to have QCBOR calculate it for you. To do
447 * this, pass @ref SizeCalculateUsefulBuf for @c Storage. Then call
448 * all the functions to add the CBOR exactly as if encoding for
449 * real. Finally, call QCBOREncode_FinishGetSize(). Once the length
450 * is obtained, allocate a buffer of that size, call
451 * QCBOREncode_Init() again with the real buffer. Call all the add
452 * functions again and finally, QCBOREncode_Finish() to obtain the
453 * final result. This uses twice the CPU time, but that is usually not
454 * an issue.
455 *
456 * See QCBOREncode_Finish() for how the pointer and length for the
457 * encoded CBOR is returned.
458 *
459 * For practical purposes QCBOR can't output encoded CBOR larger than
460 * @c UINT32_MAX (4GB) even on 64-bit CPUs because the internal
461 * offsets used to track the start of an array/map are 32 bits to
462 * reduce the size of the encoding context.
463 *
464 * A @ref QCBOREncodeContext can be reused over and over as long as
465 * QCBOREncode_Init() is called before each use.
Michael Eckel5c531332020-03-02 01:35:30 +0100466 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700467void
468QCBOREncode_Init(QCBOREncodeContext *pCtx, UsefulBuf Storage);
Michael Eckel5c531332020-03-02 01:35:30 +0100469
470
471/**
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800472 * @brief Select preferred serialization mode.
473 *
474 * @param[in] pCtx The encoding context for mode set.
475 *
476 * Setting this mode will cause QCBOR to return an error if an attempt
477 * is made to use one of the methods that produce non-preferred
478 * serialization. It doesn't change anything else as QCBOR produces
479 * preferred serialization by default.
480 *
481 * The non-preferred methods are: QCBOREncode_AddFloatNoPreferred(),
482 * QCBOREncode_AddDoubleNoPreferred(),
483 * QCBOREncode_OpenArrayIndefiniteLength(),
484 * QCBOREncode_CloseArrayIndefiniteLength(),
485 * QCBOREncode_OpenMapIndefiniteLength(),
486 * QCBOREncode_CloseMapIndefiniteLength(), plus those derived from the
487 * above listed.
488 *
489 * This mode is just a user guard to prevent accidentally calling
490 * something that produces non-preferred serialization. It doesn't do
491 * anything but causes errors to occur on attempts to call the above
492 * listed functions. This does nothing if the library is compiled
493 * QCBOR_DISABLE_ENCODE_USAGE_GUARDS.
494 *
495 * See @ref Serialization. It is usually not necessary to set this
496 * mode, but there is usually no disadvantage to setting it. Preferred
497 * Serialization is defined in RFC 8949, section 4.1.
498 */
499static void
500QCBOREncode_SerializationPreferred(QCBOREncodeContext *pCtx);
501
502
503/**
504 * @brief Select CBOR deterministic encoding mode.
505 *
506 * @param[in] pCtx The encoding context for mode set.
507
508 * This causes QCBOR to produce CBOR Deterministic Encoding (CDE).
509 * With CDE, two distant unrelated CBOR encoders will produce exactly
510 * the same encoded CBOR for a given input.
511 *
512 * In addition to doing everything
513 * QCBOREncode_SerializationPreferred() does (including exclusion of
514 * indefinite lengths), this causes maps to be sorted. The map is
515 * sorted automatically when QCBOREncode_CloseMap() is called.
516 * QCBOREncode_CloseMap() becomes equivalent to
517 * QCBOREncode_CloseAndSortMap().
518 *
519 * Note that linking this function causese about 30% more code from
520 * the QCBOR library to be linked. Also, QCBOREncode_CloseMap() runs
521 * slower, but this is probably only of consequence in very
522 * constrained environments.
523 *
524 * See @ref Serialization. It is usually not necessary to set this
525 * mode as determinism is very rarely needed. However it will
526 * usually work with most protocols. CDE is defined in
527 * draft-ietf-cbor-cde.
528 */
529static void
530QCBOREncode_SerializationCDE(QCBOREncodeContext *pCtx);
531
532
533/**
534 * @brief Select "dCBOR" encoding mode.
535 *
536 * @param[in] pCtx The encoding context for mode set.
537 *
538 * This is a superset of CDE. This function does everything
539 * QCBOREncode_SerializationCDE() does. Also it is a super set of
540 * preferred serialization and does everything
541 * QCBOREncode_SerializationPreferred() does.
542 *
543 * The main feature of dCBOR is that there is only one way to serialize a
544 * particular numeric value. This changes the behavior of functions
545 * that add floating-point numbers. If the floating-point number is
546 * whole, it will be encoded as an integer, not a floating-point number.
547 * 0.000 will be encoded as 0x00. Precision is never lost in this
548 * conversion.
549 *
550 * dCBOR also disallows NaN payloads. QCBOR will allow NaN payloads if
551 * you pass a NaN to one of the floating-point encoding functions.
552 * This mode forces all NaNs to the half-precision queit NaN. Also see
553 * QCBOREncode_Allow().
554 *
555 * dCBOR also disallows 65-bit negative integers.
556 *
557 * dCBOR disallows use of any simple type other than true, false and
558 * NULL. In particular it disallows use of "undef" produced by
559 * QCBOREncode_AddUndef().
560 *
561 * See @ref Serialization. Set this mode only if the protocol you are
562 * implementing requires dCBOR. This mode is usually not compatible
563 * with protocols that don't use dCBOR. dCBOR is defined in
564 * draft-mcnally-deterministic-cbor.
565 */
566static void
567QCBOREncode_SerializationdCBOR(QCBOREncodeContext *pCtx);
568
569
570
571
572/** Bit flag to be passed to QCBOREncode_Allow() to allow NaN payloads
573 * to be output by QCBOREncode_AddDouble(),
574 * QCBOREncode_AddDoubleNoPreferred(), QCBORENcode_AddFloat() and
575 * QCBOREncode_AddSingleleNoPreferred. */
576#define QCBOR_ENCODE_ALLOW_NAN_PAYLOAD 0x01
577
578/** Bit flag to be passed to QCBOREncode_Allow() to allow use of
579 * QCBOREncode_AddNegativeUInt64(). */
580#define QCBOR_ENCODE_ALLOW_65_BIG_NEG 0x02
581
582/** Bit flag to be passed to QCBOREncode_Allow() output of less
583 * interoperable values. See @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD, and
584 * @ref QCBOR_ENCODE_ALLOW_65_BIG_NEG. */
585#define QCBOR_ENCODE_ALLOW_ALL 0xFF
586
587
588/**
589 * @brief Allow encoding of less-interoperable values.
590 *
591 * @param[in] pCtx The encoding context.
592 * @param[in] uAllow Bit flags indicating what to allow.
593 *
594 * There are a few things in the CBOR standard that are often not
595 * supported and are thus not very interoperable. By default QCBOR
596 * will error if you attempt to output them. This disables that
597 * error.
598 *
599 * See @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD and
600 * @ref QCBOR_ENCODE_ALLOW_65_BIG_NEG.
601 *
602 * This does nothing if the library is compiled
603 * QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
604static void
605QCBOREncode_Allow(QCBOREncodeContext *pCtx, uint8_t uAllow);
606
607
608
609/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700610 * @brief Add a signed 64-bit integer to the encoded output.
611 *
612 * @param[in] pCtx The encoding context to add the integer to.
613 * @param[in] nNum The integer to add.
614 *
615 * The integer will be encoded and added to the CBOR output.
616 *
617 * This function figures out the size and the sign and encodes in the
618 * correct minimal CBOR. Specifically, it will select CBOR major type
619 * 0 or 1 based on sign and will encode to 1, 2, 4 or 8 bytes
620 * depending on the value of the integer. Values less than 24
621 * effectively encode to one byte because they are encoded in with the
622 * CBOR major type. This is a neat and efficient characteristic of
623 * CBOR that can be taken advantage of when designing CBOR-based
624 * protocols. If integers like tags can be kept between -23 and 23
625 * they will be encoded in one byte including the major type.
626 *
627 * If you pass a smaller int, say an @c int16_t or a small value, say
628 * 100, the encoding will still be CBOR's most compact that can
629 * represent the value. For example, CBOR always encodes the value 0
630 * as one byte, 0x00. The representation as 0x00 includes
631 * identification of the type as an integer too as the major type for
632 * an integer is 0. See [RFC 8949]
633 * (https://tools.ietf.org/html/rfc8949) Appendix A for more examples
634 * of CBOR encoding. This compact encoding is also preferred
635 * serialization CBOR as per section 34.1 in RFC 8949.
636 *
637 * There are no functions to add @c int16_t or @c int32_t because they
638 * are not necessary because this always encodes to the smallest
639 * number of bytes based on the value (If this code is running on a
640 * 32-bit machine having a way to add 32-bit integers would reduce
641 * code size some).
642 *
643 * If the encoding context is in an error state, this will do
644 * nothing. If an error occurs when adding this integer, the internal
645 * error flag will be set, and the error will be returned when
646 * QCBOREncode_Finish() is called.
647 *
648 * See also QCBOREncode_AddUInt64().
Michael Eckel5c531332020-03-02 01:35:30 +0100649 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700650void
651QCBOREncode_AddInt64(QCBOREncodeContext *pCtx, int64_t nNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100652
Laurence Lundblade3eead482023-12-16 20:53:22 -0700653static void
654QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t uNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100655
Laurence Lundblade3eead482023-12-16 20:53:22 -0700656static void
657QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100658
659
660/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700661 * @brief Add an unsigned 64-bit integer to the encoded output.
662 *
663 * @param[in] pCtx The encoding context to add the integer to.
664 * @param[in] uNum The integer to add.
665 *
666 * The integer will be encoded and added to the CBOR output.
667 *
668 * The only reason so use this function is for integers larger than
669 * @c INT64_MAX and smaller than @c UINT64_MAX. Otherwise
670 * QCBOREncode_AddInt64() will work fine.
671 *
672 * Error handling is the same as for QCBOREncode_AddInt64().
Michael Eckel5c531332020-03-02 01:35:30 +0100673 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700674void
675QCBOREncode_AddUInt64(QCBOREncodeContext *pCtx, uint64_t uNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100676
Laurence Lundblade3eead482023-12-16 20:53:22 -0700677static void
678QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100679
Laurence Lundblade3eead482023-12-16 20:53:22 -0700680static void
681QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100682
683
684/**
Laurence Lundblade2d493002024-02-01 11:09:17 -0700685 * @brief Add a negative 64-bit integer to encoded output
686 *
687 * @param[in] pCtx The encoding context to add the integer to.
688 * @param[in] uNum The integer to add.
689 *
690 * QCBOREncode_AddInt64() is much better to encode negative integers
691 * than this. What this can do is add integers with one more
692 * significant bit than an int64_t (a "65-bit" integer if you count
693 * the sign as a bit) which is possible because CBOR happens to
694 * support such integers.
695 *
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800696 * Because use of this is discouraged. It must be explicitly allowed
697 * by passing @ref QCBOR_ENCODE_ALLOW_65_BIG_NEG to a call to
698 * QCBOREncode_Allow().
699 *
Laurence Lundblade2d493002024-02-01 11:09:17 -0700700 * The actual value encoded is -uNum - 1. That is, give 0 for uNum to
701 * transmit -1, give 1 to transmit -2 and give UINT64_MAX to transmit
702 * -UINT64_MAX-1 (18446744073709551616). The interface is odd like
703 * this so all negative values CBOR can represent can be encoded by
704 * QCBOR (making this a complete CBOR implementation).
705 *
706 * The most negative value QCBOREncode_AddInt64() can encode is
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800707 * -9223372036854775808 which is -2^63 or negative 0x800000000000.
708 * This can encode from -9223372036854775809 to -18446744073709551616
709 * or -2^63 - 1 to -2^64. Note that it is not possible to represent
710 * positive or negative 18446744073709551616 in any standard C data
711 * type.
Laurence Lundblade2d493002024-02-01 11:09:17 -0700712 *
713 * Negative integers are normally decoded in QCBOR with type
714 * @ref QCBOR_TYPE_INT64. Integers in the range of -9223372036854775809
715 * to -18446744073709551616 are returned as @ref QCBOR_TYPE_65BIT_NEG_INT.
716 *
717 * WARNING: some CBOR decoders will be unable to decode -2^63 - 1 to
718 * -2^64. Also, most CPUs do not have registers that can represent
719 * this range. If you need 65-bit negative integers, you likely need
720 * negative 66, 67 and 68-bit negative integers so it is likely better
721 * to use CBOR big numbers where you can have any number of bits. See
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800722 * QCBOREncode_AddTNegativeBignum() and @ref Serialization.
Laurence Lundblade2d493002024-02-01 11:09:17 -0700723 */
724void
725QCBOREncode_AddNegativeUInt64(QCBOREncodeContext *pCtx, uint64_t uNum);
726
727static void
728QCBOREncode_AddNegativeUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum);
729
730static void
731QCBOREncode_AddNegativeUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum);
732
733/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700734 * @brief Add a UTF-8 text string to the encoded output.
735 *
736 * @param[in] pCtx The encoding context to add the text to.
737 * @param[in] Text Pointer and length of text to add.
738 *
739 * The text passed in must be unencoded UTF-8 according to [RFC 3629]
740 * (https://tools.ietf.org/html/rfc3629). There is no NULL
741 * termination. The text is added as CBOR major type 3.
742 *
743 * If called with @c nBytesLen equal to 0, an empty string will be
744 * added. When @c nBytesLen is 0, @c pBytes may be @c NULL.
745 *
746 * Note that the restriction of the buffer length to a @c uint32_t is
747 * entirely intentional as this encoder is not capable of encoding
748 * lengths greater. This limit to 4GB for a text string should not be
749 * a problem.
750 *
751 * Text lines in Internet protocols (on the wire) are delimited by
752 * either a CRLF or just an LF. Officially many protocols specify
753 * CRLF, but implementations often work with either. CBOR type 3 text
754 * can be either line ending, even a mixture of both.
755 *
756 * Operating systems usually have a line end convention. Windows uses
757 * CRLF. Linux and MacOS use LF. Some applications on a given OS may
758 * work with either and some may not.
759 *
760 * The majority of use cases and CBOR protocols using type 3 text will
761 * work with either line ending. However, some use cases or protocols
762 * may not work with either in which case translation to and/or from
763 * the local line end convention, typically that of the OS, is
764 * necessary.
765 *
766 * QCBOR does no line ending translation for type 3 text when encoding
767 * and decoding.
768 *
769 * Error handling is the same as QCBOREncode_AddInt64().
Michael Eckel5c531332020-03-02 01:35:30 +0100770 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700771static void
772QCBOREncode_AddText(QCBOREncodeContext *pCtx, UsefulBufC Text);
Michael Eckel5c531332020-03-02 01:35:30 +0100773
Laurence Lundblade3eead482023-12-16 20:53:22 -0700774static void
775QCBOREncode_AddTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Text);
Michael Eckel5c531332020-03-02 01:35:30 +0100776
Laurence Lundblade3eead482023-12-16 20:53:22 -0700777static void
778QCBOREncode_AddTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Text);
Michael Eckel5c531332020-03-02 01:35:30 +0100779
780
781/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700782 * @brief Add a UTF-8 text string to the encoded output.
783 *
784 * @param[in] pCtx The encoding context to add the text to.
785 * @param[in] szString Null-terminated text to add.
786 *
787 * This works the same as QCBOREncode_AddText().
Michael Eckel5c531332020-03-02 01:35:30 +0100788 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700789static void
790QCBOREncode_AddSZString(QCBOREncodeContext *pCtx, const char *szString);
Michael Eckel5c531332020-03-02 01:35:30 +0100791
Laurence Lundblade3eead482023-12-16 20:53:22 -0700792static void
793QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szString);
Michael Eckel5c531332020-03-02 01:35:30 +0100794
Laurence Lundblade3eead482023-12-16 20:53:22 -0700795static void
796QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szString);
Michael Eckel5c531332020-03-02 01:35:30 +0100797
798
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200799#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Michael Eckel5c531332020-03-02 01:35:30 +0100800/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700801 * @brief Add a double-precision floating-point number to the encoded output.
802 *
803 * @param[in] pCtx The encoding context to add the double to.
804 * @param[in] dNum The double-precision number to add.
805 *
806 * This encodes and outputs a floating-point number. CBOR major type 7
807 * is used.
808 *
809 * This implements preferred serialization, selectively encoding the
810 * double-precision floating-point number as either double-precision,
811 * single-precision or half-precision. Infinity, NaN and 0 are always
812 * encoded as half-precision. If no precision will be lost in the
813 * conversion to half-precision, then it will be converted and
814 * encoded. If not and no precision will be lost in conversion to
815 * single-precision, then it will be converted and encoded. If not,
816 * then no conversion is performed, and it encoded as a
817 * double-precision.
818 *
819 * Half-precision floating-point numbers take up 2 bytes, half that of
820 * single-precision, one quarter of double-precision
821 *
822 * This automatically reduces the size of encoded CBOR, maybe even by
823 * four if most of values are 0, infinity or NaN.
824 *
825 * When decoded, QCBOR will usually return these values as
826 * double-precision.
827 *
828 * It is possible to disable this preferred serialization when compiling
829 * QCBOR. In that case, this functions the same as
830 * QCBOREncode_AddDoubleNoPreferred().
831 *
832 * Error handling is the same as QCBOREncode_AddInt64().
833 *
834 * See also QCBOREncode_AddDoubleNoPreferred(), QCBOREncode_AddFloat()
835 * and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800836 *
837 * By default, this will error out on an attempt to encode a NaN with
838 * a payload. See QCBOREncode_Allow() and @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD.
Michael Eckel5c531332020-03-02 01:35:30 +0100839 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700840void
841QCBOREncode_AddDouble(QCBOREncodeContext *pCtx, double dNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100842
Laurence Lundblade3eead482023-12-16 20:53:22 -0700843static void
844QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100845
Laurence Lundblade3eead482023-12-16 20:53:22 -0700846static void
847QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100848
849
850/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700851 * @brief Add a single-precision floating-point number to the encoded output.
852 *
853 * @param[in] pCtx The encoding context to add the double to.
854 * @param[in] fNum The single-precision number to add.
855 *
856 * This is identical to QCBOREncode_AddDouble() except the input is
857 * single-precision.
858 *
859 * See also QCBOREncode_AddDouble(), QCBOREncode_AddDoubleNoPreferred(),
860 * and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
861 */
862void
863QCBOREncode_AddFloat(QCBOREncodeContext *pCtx, float fNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700864
Laurence Lundblade3eead482023-12-16 20:53:22 -0700865static void
866QCBOREncode_AddFloatToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700867
Laurence Lundblade3eead482023-12-16 20:53:22 -0700868static void
869QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float dNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700870
871
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700872/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700873 * @brief Add a double-precision floating-point number without preferred encoding.
874 *
875 * @param[in] pCtx The encoding context to add the double to.
876 * @param[in] dNum The double-precision number to add.
877 *
878 * This always outputs the number as a 64-bit double-precision.
879 * Preferred serialization is not used.
880 *
881 * Error handling is the same as QCBOREncode_AddInt64().
882 *
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800883 * By default, this will error out on an attempt to encode a NaN with
884 * a payload. See QCBOREncode_Allow() and @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD.
885 *
Laurence Lundblade3eead482023-12-16 20:53:22 -0700886 * See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
887 * QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
888 */
889void
890QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pCtx, double dNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700891
Laurence Lundblade3eead482023-12-16 20:53:22 -0700892static void
893QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700894
Laurence Lundblade3eead482023-12-16 20:53:22 -0700895static void
896QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700897
898
899/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700900 * @brief Add a single-precision floating-point number without preferred encoding.
901 *
902 * @param[in] pCtx The encoding context to add the double to.
903 * @param[in] fNum The single-precision number to add.
904 *
905 * This always outputs the number as a 32-bit single-precision.
906 * Preferred serialization is not used.
907 *
908 * Error handling is the same as QCBOREncode_AddInt64().
909 *
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800910 * By default, this will error out on an attempt to encode a NaN with
911 * a payload. See QCBOREncode_Allow() and @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD.
912 *
Laurence Lundblade3eead482023-12-16 20:53:22 -0700913 * See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
914 * QCBOREncode_AddDoubleNoPreferred() and @ref Floating-Point.
915 */
916void
917QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pCtx, float fNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700918
Laurence Lundblade3eead482023-12-16 20:53:22 -0700919static void
920QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700921
Laurence Lundblade3eead482023-12-16 20:53:22 -0700922static void
923QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float fNum);
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200924#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700925
926
Michael Eckel5c531332020-03-02 01:35:30 +0100927/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700928 * @brief Add an optional tag.
929 *
930 * @param[in] pCtx The encoding context to add the tag to.
931 * @param[in] uTag The tag to add
932 *
933 * This outputs a CBOR major type 6 item that tags the next data item
934 * that is output usually to indicate it is some new data type.
935 *
936 * For many of the common standard tags, a function to encode data
937 * using it is provided and this is not needed. For example,
938 * QCBOREncode_AddDateEpoch() already exists to output integers
939 * representing dates with the right tag.
940 *
941 * The tag is applied to the next data item added to the encoded
942 * output. That data item that is to be tagged can be of any major
943 * CBOR type. Any number of tags can be added to a data item by
944 * calling this multiple times before the data item is added.
945 *
946 * See @ref Tags-Overview for discussion of creating new non-standard
947 * tags. See QCBORDecode_GetNext() for discussion of decoding custom
948 * tags.
Michael Eckel5c531332020-03-02 01:35:30 +0100949 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700950void
951QCBOREncode_AddTag(QCBOREncodeContext *pCtx, uint64_t uTag);
Laurence Lundblade46d63e92021-05-13 11:37:10 -0700952
953
Michael Eckel5c531332020-03-02 01:35:30 +0100954/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700955 * @brief Add an epoch-based date.
956 *
957 * @param[in] pCtx The encoding context to add the date to.
958 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
959 * @ref QCBOR_ENCODE_AS_BORROWED.
960 * @param[in] nDate Number of seconds since 1970-01-01T00:00Z
961 * in UTC time.
962 *
963 * As per RFC 8949 this is similar to UNIX/Linux/POSIX dates. This is
964 * the most compact way to specify a date and time in CBOR. Note that
965 * this is always UTC and does not include the time zone. Use
966 * QCBOREncode_AddDateString() if you want to include the time zone.
967 *
968 * The preferred integer serialization rules apply here so the date will be
969 * encoded in a minimal number of bytes. Until about the year 2106
970 * these dates will encode in 6 bytes -- one byte for the tag, one
971 * byte for the type and 4 bytes for the integer. After that it will
972 * encode to 10 bytes.
973 *
974 * Negative values are supported for dates before 1970.
975 *
976 * If you care about leap-seconds and that level of accuracy, make sure
977 * the system you are running this code on does it correctly. This code
978 * just takes the value passed in.
979 *
980 * This implementation cannot encode fractional seconds using float or
981 * double even though that is allowed by CBOR, but you can encode them
982 * if you want to by calling QCBOREncode_AddTag() and QCBOREncode_AddDouble().
983 *
984 * Error handling is the same as QCBOREncode_AddInt64().
985 *
986 * See also QCBOREncode_AddTDaysEpoch().
Michael Eckel5c531332020-03-02 01:35:30 +0100987 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700988static void
989QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pCtx,
990 uint8_t uTagRequirement,
991 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100992
Laurence Lundblade3eead482023-12-16 20:53:22 -0700993static void
994QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pCtx,
995 const char *szLabel,
996 uint8_t uTagRequirement,
997 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100998
Laurence Lundblade3eead482023-12-16 20:53:22 -0700999static void
1000QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pCtx,
1001 int64_t nLabel,
1002 uint8_t uTagRequirement,
1003 int64_t nDate);
1004
1005
1006static void
1007QCBOREncode_AddDateEpoch(QCBOREncodeContext *pCtx,
1008 int64_t nDate);
1009
1010static void
1011QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pCtx,
1012 const char *szLabel,
1013 int64_t nDate);
1014
1015static void
1016QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pCtx,
1017 int64_t nLabel,
1018 int64_t nDate);
1019
Michael Eckel5c531332020-03-02 01:35:30 +01001020
1021
Michael Eckel5c531332020-03-02 01:35:30 +01001022/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001023 * @brief Add an epoch-based day-count date.
1024 *
1025 * @param[in] pCtx The encoding context to add the date to.
1026 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1027 * @ref QCBOR_ENCODE_AS_BORROWED.
1028 * @param[in] nDays Number of days before or after 1970-01-0.
1029 *
1030 * This date format is described in
1031 * [RFC 8943] (https://tools.ietf.org/html/rfc8943).
1032 *
1033 * The preferred integer serialization rules apply here so the date
1034 * will be encoded in a minimal number of bytes. Until about the year
1035 * 2149 these dates will encode in 4 bytes -- one byte for the tag,
1036 * one byte for the type and 2 bytes for the integer.
1037 *
1038 * See also QCBOREncode_AddTDateEpoch().
1039 */
1040static void
1041QCBOREncode_AddTDaysEpoch(QCBOREncodeContext *pCtx,
1042 uint8_t uTagRequirement,
1043 int64_t nDays);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001044
Laurence Lundblade3eead482023-12-16 20:53:22 -07001045static void
1046QCBOREncode_AddTDaysEpochToMapSZ(QCBOREncodeContext *pCtx,
1047 const char *szLabel,
1048 uint8_t uTagRequirement,
1049 int64_t nDays);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001050
Laurence Lundblade3eead482023-12-16 20:53:22 -07001051static void
1052QCBOREncode_AddTDaysEpochToMapN(QCBOREncodeContext *pCtx,
1053 int64_t nLabel,
1054 uint8_t uTagRequirement,
1055 int64_t nDays);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001056
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001057
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001058
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001059
Laurence Lundblade3eead482023-12-16 20:53:22 -07001060/**
1061 * @brief Add a byte string to the encoded output.
1062 *
1063 * @param[in] pCtx The encoding context to add the bytes to.
1064 * @param[in] Bytes Pointer and length of the input data.
1065 *
1066 * Simply adds the bytes to the encoded output as CBOR major type 2.
1067 *
1068 * If called with @c Bytes.len equal to 0, an empty string will be
1069 * added. When @c Bytes.len is 0, @c Bytes.ptr may be @c NULL.
1070 *
1071 * Error handling is the same as QCBOREncode_AddInt64().
1072 */
1073static void
1074QCBOREncode_AddBytes(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001075
Laurence Lundblade3eead482023-12-16 20:53:22 -07001076static void
1077QCBOREncode_AddBytesToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
1078
1079static void
1080QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
1081
1082
1083/**
1084 * @brief Set up to write a byte string value directly to encoded output.
1085 *
1086 * @param[in] pCtx The encoding context to add the bytes to.
1087 * @param[out] pPlace Pointer and length of place to write byte string value.
1088 *
1089 * QCBOREncode_AddBytes() is the normal way to encode a byte string.
1090 * This is for special cases and by passes some of the pointer safety.
1091 *
1092 * The purpose of this is to output the bytes that make up a byte
1093 * string value directly to the QCBOR output buffer so you don't need
1094 * to have a copy of it in memory. This is particularly useful if the
1095 * byte string is large, for example, the encrypted payload of a
1096 * COSE_Encrypt message. The payload encryption algorithm can output
1097 * directly to the encoded CBOR buffer, perhaps by making it the
1098 * output buffer for some function (e.g. symmetric encryption) or by
1099 * multiple writes.
1100 *
1101 * The pointer in @c pPlace is where to start writing. Writing is just
1102 * copying bytes to the location by the pointer in \c pPlace. Writing
1103 * past the length in @c pPlace will be writing off the end of the
1104 * output buffer.
1105 *
1106 * If there is no room in the output buffer @ref NULLUsefulBuf will be
1107 * returned and there is no need to call QCBOREncode_CloseBytes().
1108 *
1109 * The byte string must be closed by calling QCBOREncode_CloseBytes().
1110 *
1111 * Warning: this bypasses some of the usual checks provided by QCBOR
1112 * against writing off the end of the encoded output buffer.
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001113 */
1114void
1115QCBOREncode_OpenBytes(QCBOREncodeContext *pCtx, UsefulBuf *pPlace);
1116
1117static void
Laurence Lundblade3eead482023-12-16 20:53:22 -07001118QCBOREncode_OpenBytesInMapSZ(QCBOREncodeContext *pCtx,
1119 const char *szLabel,
1120 UsefulBuf *pPlace);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001121
1122static void
Laurence Lundblade3eead482023-12-16 20:53:22 -07001123QCBOREncode_OpenBytesInMapN(QCBOREncodeContext *pCtx,
1124 int64_t nLabel,
1125 UsefulBuf *pPlace);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001126
1127
1128/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001129 * @brief Close out a byte string written directly to encoded output.
1130 *
1131 * @param[in] pCtx The encoding context to add the bytes to.
1132 * @param[out] uAmount The number of bytes written, the length of the
1133 * byte string.
1134 *
1135 * This closes out a call to QCBOREncode_OpenBytes(). This inserts a
1136 * CBOR header at the front of the byte string value to make it a
1137 * well-formed byte string.
1138 *
1139 * If there was no call to QCBOREncode_OpenBytes() then @ref
1140 * QCBOR_ERR_TOO_MANY_CLOSES is set.
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001141 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001142void
1143QCBOREncode_CloseBytes(QCBOREncodeContext *pCtx, size_t uAmount);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001144
1145
1146/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001147 * @brief Add a binary UUID to the encoded output.
1148 *
1149 * @param[in] pCtx The encoding context to add the UUID to.
1150 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1151 * @ref QCBOR_ENCODE_AS_BORROWED.
1152 * @param[in] Bytes Pointer and length of the binary UUID.
1153 *
1154 * A binary UUID as defined in [RFC 4122]
1155 * (https://tools.ietf.org/html/rfc4122) is added to the output.
1156 *
1157 * It is output as CBOR major type 2, a binary string, with tag @ref
1158 * CBOR_TAG_BIN_UUID indicating the binary string is a UUID.
Michael Eckel5c531332020-03-02 01:35:30 +01001159 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001160static void
1161QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pCtx,
1162 uint8_t uTagRequirement,
1163 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001164
Laurence Lundblade3eead482023-12-16 20:53:22 -07001165static void
1166QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pCtx,
1167 const char *szLabel,
1168 uint8_t uTagRequirement,
1169 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001170
Laurence Lundblade3eead482023-12-16 20:53:22 -07001171static void
1172QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pCtx,
1173 int64_t nLabel,
1174 uint8_t uTagRequirement,
1175 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001176
1177
Laurence Lundblade3eead482023-12-16 20:53:22 -07001178static void
1179QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001180
Laurence Lundblade3eead482023-12-16 20:53:22 -07001181static void
1182QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001183
Laurence Lundblade3eead482023-12-16 20:53:22 -07001184static void
1185QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001186
1187
1188/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001189 * @brief Add a positive big number to the encoded output.
1190 *
1191 * @param[in] pCtx The encoding context to add the big number to.
1192 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1193 * @ref QCBOR_ENCODE_AS_BORROWED.
1194 * @param[in] Bytes Pointer and length of the big number.
1195 *
1196 * Big numbers are integers larger than 64-bits. Their format is
1197 * described in [RFC 8949] (https://tools.ietf.org/html/rfc8949).
1198 *
1199 * It is output as CBOR major type 2, a binary string, with tag
1200 * @ref CBOR_TAG_POS_BIGNUM indicating the binary string is a positive
1201 * big number.
1202 *
1203 * Often big numbers are used to represent cryptographic keys,
1204 * however, COSE which defines representations for keys chose not to
1205 * use this particular type.
Michael Eckel5c531332020-03-02 01:35:30 +01001206 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001207static void
1208QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pCtx,
1209 uint8_t uTagRequirement,
1210 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001211
Laurence Lundblade3eead482023-12-16 20:53:22 -07001212static void
1213QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pCtx,
1214 const char *szLabel,
1215 uint8_t uTagRequirement,
1216 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001217
Laurence Lundblade3eead482023-12-16 20:53:22 -07001218static void
1219QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pCtx,
1220 int64_t nLabel,
1221 uint8_t uTagRequirement,
1222 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001223
1224
Laurence Lundblade3eead482023-12-16 20:53:22 -07001225static void
1226QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx,
1227 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001228
Laurence Lundblade3eead482023-12-16 20:53:22 -07001229static void
1230QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx,
1231 const char *szLabel,
1232 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001233
Laurence Lundblade3eead482023-12-16 20:53:22 -07001234static void
1235QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx,
1236 int64_t nLabel,
1237 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001238
1239
1240/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001241 * @brief Add a negative big number to the encoded output.
1242 *
1243 * @param[in] pCtx The encoding context to add the big number to.
1244 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1245 * @ref QCBOR_ENCODE_AS_BORROWED.
1246 * @param[in] Bytes Pointer and length of the big number.
1247 *
1248 * Big numbers are integers larger than 64-bits. Their format is
1249 * described in [RFC 8949] (https://tools.ietf.org/html/rfc8949).
1250 *
1251 * It is output as CBOR major type 2, a binary string, with tag
1252 * @ref CBOR_TAG_NEG_BIGNUM indicating the binary string is a negative
1253 * big number.
1254 *
1255 * Often big numbers are used to represent cryptographic keys,
1256 * however, COSE which defines representations for keys chose not to
1257 * use this particular type.
Michael Eckel5c531332020-03-02 01:35:30 +01001258 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001259static void
1260QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pCtx,
1261 uint8_t uTagRequirement,
1262 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001263
Laurence Lundblade3eead482023-12-16 20:53:22 -07001264static void
1265QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pCtx,
1266 const char *szLabel,
1267 uint8_t uTagRequirement,
1268 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001269
Laurence Lundblade3eead482023-12-16 20:53:22 -07001270static void
1271QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pCtx,
1272 int64_t nLabel,
1273 uint8_t uTagRequirement,
1274 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001275
1276
Laurence Lundblade3eead482023-12-16 20:53:22 -07001277static void
1278QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx,
1279 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001280
Laurence Lundblade3eead482023-12-16 20:53:22 -07001281static void
1282QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx,
1283 const char *szLabel,
1284 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001285
Laurence Lundblade3eead482023-12-16 20:53:22 -07001286static void
1287QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx,
1288 int64_t nLabel,
1289 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001290
1291
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07001292#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Michael Eckel5c531332020-03-02 01:35:30 +01001293/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001294 * @brief Add a decimal fraction to the encoded output.
1295 *
1296 * @param[in] pCtx Encoding context to add the decimal fraction to.
1297 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1298 * @ref QCBOR_ENCODE_AS_BORROWED.
1299 * @param[in] nMantissa The mantissa.
1300 * @param[in] nBase10Exponent The exponent.
1301 *
1302 * The value is nMantissa * 10 ^ nBase10Exponent.
1303 *
1304 * A decimal fraction is good for exact representation of some values
1305 * that can't be represented exactly with standard C (IEEE 754)
1306 * floating-point numbers. Much larger and much smaller numbers can
1307 * also be represented than floating-point because of the larger
1308 * number of bits in the exponent.
1309 *
1310 * The decimal fraction is conveyed as two integers, a mantissa and a
1311 * base-10 scaling factor.
1312 *
1313 * For example, 273.15 is represented by the two integers 27315 and -2.
1314 *
1315 * The exponent and mantissa have the range from @c INT64_MIN to
1316 * @c INT64_MAX for both encoding and decoding (CBOR allows
1317 * @c -UINT64_MAX to @c UINT64_MAX, but this implementation doesn't
1318 * support this range to reduce code size and interface complexity a
1319 * little).
1320 *
1321 * CBOR Preferred serialization of the integers is used, thus they
1322 * will be encoded in the smallest number of bytes possible.
1323 *
1324 * See also QCBOREncode_AddDecimalFractionBigNum() for a decimal
1325 * fraction with arbitrarily large precision and
1326 * QCBOREncode_AddBigFloat().
1327 *
1328 * There is no representation of positive or negative infinity or NaN
1329 * (Not a Number). Use QCBOREncode_AddDouble() to encode them.
1330 *
1331 * See @ref expAndMantissa for decoded representation.
Michael Eckel5c531332020-03-02 01:35:30 +01001332 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001333static void
1334QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pCtx,
1335 uint8_t uTagRequirement,
1336 int64_t nMantissa,
1337 int64_t nBase10Exponent);
1338
1339static void
1340QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pCtx,
1341 const char *szLabel,
1342 uint8_t uTagRequirement,
1343 int64_t nMantissa,
1344 int64_t nBase10Exponent);
1345
1346static void
1347QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pCtx,
1348 int64_t nLabel,
1349 uint8_t uTagRequirement,
1350 int64_t nMantissa,
1351 int64_t nBase10Exponent);
1352
1353
1354static void
1355QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pCtx,
1356 int64_t nMantissa,
1357 int64_t nBase10Exponent);
1358
1359static void
1360QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx,
1361 const char *szLabel,
1362 int64_t nMantissa,
1363 int64_t nBase10Exponent);
1364
1365static void
1366QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pCtx,
1367 int64_t nLabel,
1368 int64_t nMantissa,
1369 int64_t nBase10Exponent);
1370
1371
1372/**
1373 * @brief Add a decimal fraction with a big number mantissa to the encoded output.
1374 *
1375 * @param[in] pCtx Encoding context to add the decimal fraction to.
1376 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1377 * @ref QCBOR_ENCODE_AS_BORROWED.
1378 * @param[in] Mantissa The mantissa.
1379 * @param[in] bIsNegative false if mantissa is positive, true if negative.
1380 * @param[in] nBase10Exponent The exponent.
1381 *
1382 * This is the same as QCBOREncode_AddDecimalFraction() except the
1383 * mantissa is a big number (See QCBOREncode_AddPositiveBignum())
1384 * allowing for arbitrarily large precision.
1385 *
1386 * See @ref expAndMantissa for decoded representation.
1387 */
1388static void
1389QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pCtx,
1390 uint8_t uTagRequirement,
1391 UsefulBufC Mantissa,
1392 bool bIsNegative,
1393 int64_t nBase10Exponent);
1394
1395static void
1396QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
1397 const char *szLabel,
1398 uint8_t uTagRequirement,
1399 UsefulBufC Mantissa,
1400 bool bIsNegative,
1401 int64_t nBase10Exponent);
1402
1403static void
1404QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
1405 int64_t nLabel,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001406 uint8_t uTagRequirement,
Laurence Lundblade3eead482023-12-16 20:53:22 -07001407 UsefulBufC Mantissa,
1408 bool bIsNegative,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001409 int64_t nBase10Exponent);
1410
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001411
Laurence Lundblade3eead482023-12-16 20:53:22 -07001412static void
1413QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pCtx,
1414 UsefulBufC Mantissa,
1415 bool bIsNegative,
1416 int64_t nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001417
Laurence Lundblade3eead482023-12-16 20:53:22 -07001418static void
1419QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07001420 const char *szLabel,
Laurence Lundblade3eead482023-12-16 20:53:22 -07001421 UsefulBufC Mantissa,
1422 bool bIsNegative,
1423 int64_t nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001424
Laurence Lundblade3eead482023-12-16 20:53:22 -07001425static void
1426QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001427 int64_t nLabel,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001428 UsefulBufC Mantissa,
1429 bool bIsNegative,
Laurence Lundblade3eead482023-12-16 20:53:22 -07001430 int64_t nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001431
Laurence Lundblade3eead482023-12-16 20:53:22 -07001432/**
1433 * @brief Add a big floating-point number to the encoded output.
1434 *
1435 * @param[in] pCtx The encoding context to add the bigfloat to.
1436 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1437 * @ref QCBOR_ENCODE_AS_BORROWED.
1438 * @param[in] nMantissa The mantissa.
1439 * @param[in] nBase2Exponent The exponent.
1440 *
1441 * The value is nMantissa * 2 ^ nBase2Exponent.
1442 *
1443 * "Bigfloats", as CBOR terms them, are similar to IEEE floating-point
1444 * numbers in having a mantissa and base-2 exponent, but they are not
1445 * supported by hardware or encoded the same. They explicitly use two
1446 * CBOR-encoded integers to convey the mantissa and exponent, each of
1447 * which can be 8, 16, 32 or 64 bits. With both the mantissa and
1448 * exponent 64 bits they can express more precision and a larger range
1449 * than an IEEE double floating-point number. See
1450 * QCBOREncode_AddBigFloatBigNum() for even more precision.
1451 *
1452 * For example, 1.5 would be represented by a mantissa of 3 and an
1453 * exponent of -1.
1454 *
1455 * The exponent and mantissa have the range from @c INT64_MIN to
1456 * @c INT64_MAX for both encoding and decoding (CBOR allows @c
1457 * -UINT64_MAX to @c UINT64_MAX, but this implementation doesn't
1458 * support this range to reduce code size and interface complexity a
1459 * little).
1460 *
1461 * CBOR preferred serialization of the integers is used, thus they will
1462 * be encoded in the smallest number of bytes possible.
1463 *
1464 * This can also be used to represent floating-point numbers in
1465 * environments that don't support IEEE 754.
1466 *
1467 * See @ref expAndMantissa for decoded representation.
1468 */
1469static void
1470QCBOREncode_AddTBigFloat(QCBOREncodeContext *pCtx,
1471 uint8_t uTagRequirement,
1472 int64_t nMantissa,
1473 int64_t nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001474
Laurence Lundblade3eead482023-12-16 20:53:22 -07001475static void
1476QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pCtx,
1477 const char *szLabel,
1478 uint8_t uTagRequirement,
1479 int64_t nMantissa,
1480 int64_t nBase2Exponent);
1481
1482static void
1483QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pCtx,
1484 int64_t nLabel,
1485 uint8_t uTagRequirement,
1486 int64_t nMantissa,
1487 int64_t nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001488
1489
Laurence Lundblade3eead482023-12-16 20:53:22 -07001490static void
1491QCBOREncode_AddBigFloat(QCBOREncodeContext *pCtx,
1492 int64_t nMantissa,
1493 int64_t nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01001494
Laurence Lundblade3eead482023-12-16 20:53:22 -07001495static void
1496QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pCtx,
1497 const char *szLabel,
1498 int64_t nMantissa,
1499 int64_t nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01001500
Laurence Lundblade3eead482023-12-16 20:53:22 -07001501static void
1502QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pCtx,
1503 int64_t nLabel,
1504 int64_t nMantissa,
1505 int64_t nBase2Exponent);
1506
1507/**
1508 * @brief Add a big floating-point number with a big number mantissa to
1509 * the encoded output.
1510 *
1511 * @param[in] pCtx The encoding context to add the bigfloat to.
1512 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1513 * @ref QCBOR_ENCODE_AS_BORROWED.
1514 * @param[in] Mantissa The mantissa.
1515 * @param[in] bIsNegative false if mantissa is positive, true if negative.
1516 * @param[in] nBase2Exponent The exponent.
1517 *
1518 * This is the same as QCBOREncode_AddBigFloat() except the mantissa
1519 * is a big number (See QCBOREncode_AddPositiveBignum()) allowing for
1520 * arbitrary precision.
1521 *
1522 * See @ref expAndMantissa for decoded representation.
1523 */
1524static void
1525QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pCtx,
1526 uint8_t uTagRequirement,
1527 UsefulBufC Mantissa,
1528 bool bIsNegative,
1529 int64_t nBase2Exponent);
1530
1531static void
1532QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pCtx,
1533 const char *szLabel,
1534 uint8_t uTagRequirement,
1535 UsefulBufC Mantissa,
1536 bool bIsNegative,
1537 int64_t nBase2Exponent);
1538
1539static void
1540QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
1541 int64_t nLabel,
1542 uint8_t uTagRequirement,
1543 UsefulBufC Mantissa,
1544 bool bIsNegative,
1545 int64_t nBase2Exponent);
1546
1547
1548static void
1549QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pCtx,
1550 UsefulBufC Mantissa,
1551 bool bIsNegative,
1552 int64_t nBase2Exponent);
1553
1554static void
1555QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pCtx,
1556 const char *szLabel,
1557 UsefulBufC Mantissa,
1558 bool bIsNegative,
1559 int64_t nBase2Exponent);
1560
1561static void
1562QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
1563 int64_t nLabel,
1564 UsefulBufC Mantissa,
1565 bool bIsNegative,
1566 int64_t nBase2Exponent);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07001567#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Michael Eckel5c531332020-03-02 01:35:30 +01001568
1569
1570/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001571 * @brief Add a text URI to the encoded output.
1572 *
1573 * @param[in] pCtx The encoding context to add the URI to.
1574 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1575 * @ref QCBOR_ENCODE_AS_BORROWED.
1576 * @param[in] URI Pointer and length of the URI.
1577 *
1578 * The format of URI must be per [RFC 3986]
1579 * (https://tools.ietf.org/html/rfc3986).
1580 *
1581 * It is output as CBOR major type 3, a text string, with tag @ref
1582 * CBOR_TAG_URI indicating the text string is a URI.
1583 *
1584 * A URI in a NULL-terminated string, @c szURI, can be easily added with
1585 * this code:
1586 *
1587 * QCBOREncode_AddURI(pCtx, UsefulBuf_FromSZ(szURI));
Michael Eckel5c531332020-03-02 01:35:30 +01001588 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001589static void
1590QCBOREncode_AddTURI(QCBOREncodeContext *pCtx,
1591 uint8_t uTagRequirement,
1592 UsefulBufC URI);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001593
Laurence Lundblade3eead482023-12-16 20:53:22 -07001594static void
1595QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pCtx,
1596 const char *szLabel,
1597 uint8_t uTagRequirement,
1598 UsefulBufC URI);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001599
Laurence Lundblade3eead482023-12-16 20:53:22 -07001600static void
1601QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pCtx,
1602 int64_t nLabel,
1603 uint8_t uTagRequirement,
1604 UsefulBufC URI);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001605
1606
Laurence Lundblade3eead482023-12-16 20:53:22 -07001607static void
1608QCBOREncode_AddURI(QCBOREncodeContext *pCtx,
1609 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001610
Laurence Lundblade3eead482023-12-16 20:53:22 -07001611static void
1612QCBOREncode_AddURIToMap(QCBOREncodeContext *pCtx,
1613 const char *szLabel,
1614 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001615
Laurence Lundblade3eead482023-12-16 20:53:22 -07001616static void
1617QCBOREncode_AddURIToMapN(QCBOREncodeContext *pCtx,
1618 int64_t nLabel,
1619 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001620
1621
1622/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001623 * @brief Add Base64-encoded text to encoded output.
1624 *
1625 * @param[in] pCtx The encoding context to add the base-64 text to.
1626 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1627 * @ref QCBOR_ENCODE_AS_BORROWED.
1628 * @param[in] B64Text Pointer and length of the base-64 encoded text.
1629 *
1630 * The text content is Base64 encoded data per [RFC 4648]
1631 * (https://tools.ietf.org/html/rfc4648).
1632 *
1633 * It is output as CBOR major type 3, a text string, with tag @ref
1634 * CBOR_TAG_B64 indicating the text string is Base64 encoded.
Michael Eckel5c531332020-03-02 01:35:30 +01001635 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001636static void
1637QCBOREncode_AddTB64Text(QCBOREncodeContext *pCtx,
1638 uint8_t uTagRequirement,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001639 UsefulBufC B64Text);
1640
Laurence Lundblade3eead482023-12-16 20:53:22 -07001641static void
1642QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pCtx,
1643 const char *szLabel,
1644 uint8_t uTagRequirement,
1645 UsefulBufC B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001646
Laurence Lundblade3eead482023-12-16 20:53:22 -07001647static void
1648QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pCtx,
1649 int64_t nLabel,
1650 uint8_t uTagRequirement,
1651 UsefulBufC B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001652
1653
Laurence Lundblade3eead482023-12-16 20:53:22 -07001654static void
1655QCBOREncode_AddB64Text(QCBOREncodeContext *pCtx,
1656 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001657
Laurence Lundblade3eead482023-12-16 20:53:22 -07001658static void
1659QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pCtx,
1660 const char *szLabel,
1661 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001662
Laurence Lundblade3eead482023-12-16 20:53:22 -07001663static void
1664QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pCtx,
1665 int64_t nLabel,
1666 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001667
1668
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001669
Michael Eckel5c531332020-03-02 01:35:30 +01001670/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001671 * @brief Add base64url encoded data to encoded output.
1672 *
1673 * @param[in] pCtx The encoding context to add the base64url to.
1674 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1675 * @ref QCBOR_ENCODE_AS_BORROWED.
1676 * @param[in] B64Text Pointer and length of the base64url encoded text.
1677 *
1678 * The text content is base64URL encoded text as per [RFC 4648]
1679 * (https://tools.ietf.org/html/rfc4648).
1680 *
1681 * It is output as CBOR major type 3, a text string, with tag
1682 * @ref CBOR_TAG_B64URL indicating the text string is a Base64url
1683 * encoded.
Michael Eckel5c531332020-03-02 01:35:30 +01001684 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001685static void
1686QCBOREncode_AddTB64URLText(QCBOREncodeContext *pCtx,
1687 uint8_t uTagRequirement,
1688 UsefulBufC B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001689
Laurence Lundblade3eead482023-12-16 20:53:22 -07001690static void
1691QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pCtx,
1692 const char *szLabel,
1693 uint8_t uTagRequirement,
1694 UsefulBufC B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001695
Laurence Lundblade3eead482023-12-16 20:53:22 -07001696static void
1697QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pCtx,
1698 int64_t nLabel,
1699 uint8_t uTagRequirement,
1700 UsefulBufC B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001701
1702
Laurence Lundblade3eead482023-12-16 20:53:22 -07001703static void
1704QCBOREncode_AddB64URLText(QCBOREncodeContext *pCtx,
1705 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001706
Laurence Lundblade3eead482023-12-16 20:53:22 -07001707static void
1708QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pCtx,
1709 const char *szLabel,
1710 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001711
Laurence Lundblade3eead482023-12-16 20:53:22 -07001712static void
1713QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pCtx,
1714 int64_t nLabel,
1715 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001716
1717
1718/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001719 * @brief Add Perl Compatible Regular Expression.
1720 *
1721 * @param[in] pCtx Encoding context to add the regular expression to.
1722 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1723 * @ref QCBOR_ENCODE_AS_BORROWED.
1724 * @param[in] Regex Pointer and length of the regular expression.
1725 *
1726 * The text content is Perl Compatible Regular
1727 * Expressions (PCRE) / JavaScript syntax [ECMA262].
1728 *
1729 * It is output as CBOR major type 3, a text string, with tag @ref
1730 * CBOR_TAG_REGEX indicating the text string is a regular expression.
Michael Eckel5c531332020-03-02 01:35:30 +01001731 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001732static void
1733QCBOREncode_AddTRegex(QCBOREncodeContext *pCtx,
1734 uint8_t uTagRequirement,
1735 UsefulBufC Regex);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001736
Laurence Lundblade3eead482023-12-16 20:53:22 -07001737static void
1738QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pCtx,
1739 const char *szLabel,
1740 uint8_t uTagRequirement,
1741 UsefulBufC Regex);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001742
Laurence Lundblade3eead482023-12-16 20:53:22 -07001743static void
1744QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pCtx,
1745 int64_t nLabel,
1746 uint8_t uTagRequirement,
1747 UsefulBufC Regex);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001748
1749
Laurence Lundblade3eead482023-12-16 20:53:22 -07001750static void
1751QCBOREncode_AddRegex(QCBOREncodeContext *pCtx,
1752 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001753
Laurence Lundblade3eead482023-12-16 20:53:22 -07001754static void
1755QCBOREncode_AddRegexToMap(QCBOREncodeContext *pCtx,
1756 const char *szLabel,
1757 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001758
Laurence Lundblade3eead482023-12-16 20:53:22 -07001759static void
1760QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pCtx,
1761 int64_t nLabel,
1762 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001763
1764
1765/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001766 * @brief MIME encoded data to the encoded output.
1767 *
1768 * @param[in] pCtx The encoding context to add the MIME data to.
1769 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1770 * @ref QCBOR_ENCODE_AS_BORROWED.
1771 * @param[in] MIMEData Pointer and length of the MIME data.
1772 *
1773 * The text content is in MIME format per [RFC 2045]
1774 * (https://tools.ietf.org/html/rfc2045) including the headers.
1775 *
1776 * It is output as CBOR major type 2, a binary string, with tag
1777 * @ref CBOR_TAG_BINARY_MIME indicating the string is MIME data. This
1778 * outputs tag 257, not tag 36, as it can carry any type of MIME
1779 * binary, 7-bit, 8-bit, quoted-printable and base64 where tag 36
1780 * cannot.
1781 *
1782 * Previous versions of QCBOR, those before spiffy decode, output tag
1783 * 36. Decoding supports both tag 36 and 257. (if the old behavior
1784 * with tag 36 is needed, copy the inline functions below and change
1785 * the tag number).
1786 *
1787 * See also QCBORDecode_GetMIMEMessage() and
1788 * @ref QCBOR_TYPE_BINARY_MIME.
1789 *
1790 * This does no translation of line endings. See QCBOREncode_AddText()
1791 * for a discussion of line endings in CBOR.
Michael Eckel5c531332020-03-02 01:35:30 +01001792 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001793static void
1794QCBOREncode_AddTMIMEData(QCBOREncodeContext *pCtx,
1795 uint8_t uTagRequirement,
1796 UsefulBufC MIMEData);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001797
Laurence Lundblade3eead482023-12-16 20:53:22 -07001798static void
1799QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pCtx,
1800 const char *szLabel,
1801 uint8_t uTagRequirement,
1802 UsefulBufC MIMEData);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001803
Laurence Lundblade3eead482023-12-16 20:53:22 -07001804static void
1805QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pCtx,
1806 int64_t nLabel,
1807 uint8_t uTagRequirement,
1808 UsefulBufC MIMEData);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001809
1810
Laurence Lundblade3eead482023-12-16 20:53:22 -07001811static void
1812QCBOREncode_AddMIMEData(QCBOREncodeContext *pCtx,
1813 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001814
Laurence Lundblade3eead482023-12-16 20:53:22 -07001815static void
1816QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pCtx,
1817 const char *szLabel,
1818 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001819
Laurence Lundblade3eead482023-12-16 20:53:22 -07001820static void
1821QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pCtx,
1822 int64_t nLabel,
1823 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001824
1825
1826/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001827 * @brief Add an RFC 3339 date string
1828 *
1829 * @param[in] pCtx The encoding context to add the date to.
1830 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1831 * @ref QCBOR_ENCODE_AS_BORROWED.
1832 * @param[in] szDate Null-terminated string with date to add.
1833 *
1834 * The string szDate should be in the form of [RFC 3339]
1835 * (https://tools.ietf.org/html/rfc3339) as defined by section 3.3 in
1836 * [RFC 4287] (https://tools.ietf.org/html/rfc4287). This is as
1837 * described in section 3.4.1 in [RFC 8949]
1838 * (https://tools.ietf.org/html/rfc8949).
1839 *
1840 * Note that this function doesn't validate the format of the date
1841 * string at all. If you add an incorrect format date string, the
1842 * generated CBOR will be incorrect and the receiver may not be able
1843 * to handle it.
1844 *
1845 * Error handling is the same as QCBOREncode_AddInt64().
1846 *
1847 * See also QCBOREncode_AddTDayString().
Michael Eckel5c531332020-03-02 01:35:30 +01001848 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001849static void
1850QCBOREncode_AddTDateString(QCBOREncodeContext *pCtx,
1851 uint8_t uTagRequirement,
1852 const char *szDate);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001853
Laurence Lundblade3eead482023-12-16 20:53:22 -07001854static void
1855QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pCtx,
1856 const char *szLabel,
1857 uint8_t uTagRequirement,
1858 const char *szDate);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001859
Laurence Lundblade3eead482023-12-16 20:53:22 -07001860static void
1861QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pCtx,
1862 int64_t nLabel,
1863 uint8_t uTagRequirement,
1864 const char *szDate);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001865
1866
Laurence Lundblade3eead482023-12-16 20:53:22 -07001867static void
1868QCBOREncode_AddDateString(QCBOREncodeContext *pCtx,
1869 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001870
Laurence Lundblade3eead482023-12-16 20:53:22 -07001871static void
1872QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pCtx,
1873 const char *szLabel,
1874 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001875
Laurence Lundblade3eead482023-12-16 20:53:22 -07001876static void
1877QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pCtx,
1878 int64_t nLabel,
1879 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001880
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001881
1882/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001883 * @brief Add a date-only string.
1884 *
1885 * @param[in] pCtx The encoding context to add the date to.
1886 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1887 * @ref QCBOR_ENCODE_AS_BORROWED.
1888 * @param[in] szDate Null-terminated string with date to add.
1889 *
1890 * This date format is described in
1891 * [RFC 8943] (https://tools.ietf.org/html/rfc8943), but that mainly
1892 * references RFC 3339. The string szDate must be in the forrm
1893 * specified the ABNF for a full-date in
1894 * [RFC 3339] (https://tools.ietf.org/html/rfc3339). Examples of this
1895 * are "1985-04-12" and "1937-01-01". The time and the time zone are
1896 * never included.
1897 *
1898 * Note that this function doesn't validate the format of the date
1899 * string at all. If you add an incorrect format date string, the
1900 * generated CBOR will be incorrect and the receiver may not be able
1901 * to handle it.
1902 *
1903 * Error handling is the same as QCBOREncode_AddInt64().
1904 *
1905 * See also QCBOREncode_AddTDateString().
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001906 */
1907static void
1908QCBOREncode_AddTDaysString(QCBOREncodeContext *pCtx,
1909 uint8_t uTagRequirement,
1910 const char *szDate);
1911
1912static void
1913QCBOREncode_AddTDaysStringToMapSZ(QCBOREncodeContext *pCtx,
1914 const char *szLabel,
1915 uint8_t uTagRequirement,
1916 const char *szDate);
1917
1918static void
1919QCBOREncode_AddTDaysStringToMapN(QCBOREncodeContext *pCtx,
1920 int64_t nLabel,
1921 uint8_t uTagRequirement,
1922 const char *szDate);
1923
1924
Michael Eckel5c531332020-03-02 01:35:30 +01001925/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001926 * @brief Add a standard Boolean.
1927 *
1928 * @param[in] pCtx The encoding context to add the Boolean to.
1929 * @param[in] b true or false from @c <stdbool.h>.
1930 *
1931 * Adds a Boolean value as CBOR major type 7.
1932 *
1933 * Error handling is the same as QCBOREncode_AddInt64().
Michael Eckel5c531332020-03-02 01:35:30 +01001934 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001935static void
1936QCBOREncode_AddBool(QCBOREncodeContext *pCtx, bool b);
Michael Eckel5c531332020-03-02 01:35:30 +01001937
Laurence Lundblade3eead482023-12-16 20:53:22 -07001938static void
1939QCBOREncode_AddBoolToMap(QCBOREncodeContext *pCtx, const char *szLabel, bool b);
Michael Eckel5c531332020-03-02 01:35:30 +01001940
Laurence Lundblade3eead482023-12-16 20:53:22 -07001941static void
1942QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, bool b);
Michael Eckel5c531332020-03-02 01:35:30 +01001943
1944
1945
1946/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001947 * @brief Add a NULL to the encoded output.
1948 *
1949 * @param[in] pCtx The encoding context to add the NULL to.
1950 *
1951 * Adds the NULL value as CBOR major type 7.
1952 *
1953 * This NULL doesn't have any special meaning in CBOR such as a
1954 * terminating value for a string or an empty value.
1955 *
1956 * Error handling is the same as QCBOREncode_AddInt64().
Michael Eckel5c531332020-03-02 01:35:30 +01001957 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001958static void
1959QCBOREncode_AddNULL(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01001960
Laurence Lundblade3eead482023-12-16 20:53:22 -07001961static void
1962QCBOREncode_AddNULLToMap(QCBOREncodeContext *pCtx, const char *szLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01001963
Laurence Lundblade3eead482023-12-16 20:53:22 -07001964static void
1965QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01001966
1967
1968/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001969 * @brief Add an "undef" to the encoded output.
1970 *
1971 * @param[in] pCtx The encoding context to add the "undef" to.
1972 *
1973 * Adds the undef value as CBOR major type 7.
1974 *
1975 * Note that this value will not translate to JSON.
1976 *
1977 * This Undef doesn't have any special meaning in CBOR such as a
1978 * terminating value for a string or an empty value.
1979 *
1980 * Error handling is the same as QCBOREncode_AddInt64().
Michael Eckel5c531332020-03-02 01:35:30 +01001981 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001982static void
1983QCBOREncode_AddUndef(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01001984
Laurence Lundblade3eead482023-12-16 20:53:22 -07001985static void
1986QCBOREncode_AddUndefToMap(QCBOREncodeContext *pCtx, const char *szLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01001987
Laurence Lundblade3eead482023-12-16 20:53:22 -07001988static void
1989QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01001990
1991
1992/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001993 * @brief Indicates that the next items added are in an array.
1994 *
1995 * @param[in] pCtx The encoding context to open the array in.
1996 *
1997 * Arrays are the basic CBOR aggregate or structure type. Call this
1998 * function to start or open an array. Then call the various
1999 * @c QCBOREncode_AddXxx() functions to add the items that go into the
2000 * array. Then call QCBOREncode_CloseArray() when all items have been
2001 * added. The data items in the array can be of any type and can be of
2002 * mixed types.
2003 *
2004 * Nesting of arrays and maps is allowed and supported just by calling
2005 * QCBOREncode_OpenArray() again before calling
2006 * QCBOREncode_CloseArray(). While CBOR has no limit on nesting, this
2007 * implementation does in order to keep it smaller and simpler. The
2008 * limit is @ref QCBOR_MAX_ARRAY_NESTING. This is the max number of
2009 * times this can be called without calling
2010 * QCBOREncode_CloseArray(). QCBOREncode_Finish() will return
2011 * @ref QCBOR_ERR_ARRAY_NESTING_TOO_DEEP when it is called as this
2012 * function just sets an error state and returns no value when this
2013 * occurs.
2014 *
2015 * If you try to add more than @ref QCBOR_MAX_ITEMS_IN_ARRAY items to
2016 * a single array or map, @ref QCBOR_ERR_ARRAY_TOO_LONG will be
2017 * returned when QCBOREncode_Finish() is called.
2018 *
2019 * An array itself must have a label if it is being added to a map.
2020 * Note that array elements do not have labels (but map elements do).
2021 *
2022 * An array itself may be tagged by calling QCBOREncode_AddTag()
2023 * before this call.
Michael Eckel5c531332020-03-02 01:35:30 +01002024 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002025static void
2026QCBOREncode_OpenArray(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01002027
Laurence Lundblade3eead482023-12-16 20:53:22 -07002028static void
2029QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pCtx, const char *szLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002030
Laurence Lundblade3eead482023-12-16 20:53:22 -07002031static void
2032QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002033
2034
2035/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002036 * @brief Close an open array.
2037 *
2038 * @param[in] pCtx The encoding context to close the array in.
2039 *
2040 * The closes an array opened by QCBOREncode_OpenArray(). It reduces
2041 * nesting level by one. All arrays (and maps) must be closed before
2042 * calling QCBOREncode_Finish().
2043 *
2044 * When an error occurs as a result of this call, the encoder records
2045 * the error and enters the error state. The error will be returned
2046 * when QCBOREncode_Finish() is called.
2047 *
2048 * If this has been called more times than QCBOREncode_OpenArray(), then
2049 * @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when QCBOREncode_Finish()
2050 * is called.
2051 *
2052 * If this is called and it is not an array that is currently open,
2053 * @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when
2054 * QCBOREncode_Finish() is called.
Michael Eckel5c531332020-03-02 01:35:30 +01002055 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002056static void
2057QCBOREncode_CloseArray(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01002058
2059
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002060
2061
Michael Eckel5c531332020-03-02 01:35:30 +01002062/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002063 * @brief Indicates that the next items added are in a map.
2064 *
2065 * @param[in] pCtx The encoding context to open the map in.
2066 *
2067 * See QCBOREncode_OpenArray() for more information, particularly
2068 * error handling.
2069 *
2070 * CBOR maps are an aggregate type where each item in the map consists
2071 * of a label and a value. They are similar to JSON objects.
2072 *
2073 * The value can be any CBOR type including another map.
2074 *
2075 * The label can also be any CBOR type, but in practice they are
2076 * typically, integers as this gives the most compact output. They
2077 * might also be text strings which gives readability and translation
2078 * to JSON.
2079 *
2080 * Every @c QCBOREncode_AddXxx() call has one version that ends with
2081 * @c InMap for adding items to maps with string labels and one that
2082 * ends with @c InMapN that is for adding with integer labels.
2083 *
2084 * RFC 8949 uses the term "key" instead of "label".
2085 *
2086 * If you wish to use map labels that are neither integer labels nor
2087 * text strings, then just call the QCBOREncode_AddXxx() function
2088 * explicitly to add the label. Then call it again to add the value.
2089 *
2090 * See the [RFC 8949] (https://tools.ietf.org/html/rfc8949) for a lot
2091 * more information on creating maps.
Michael Eckel5c531332020-03-02 01:35:30 +01002092 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002093static void
2094QCBOREncode_OpenMap(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01002095
Laurence Lundblade3eead482023-12-16 20:53:22 -07002096static void
2097QCBOREncode_OpenMapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002098
Laurence Lundblade3eead482023-12-16 20:53:22 -07002099static void
2100QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002101
2102
Michael Eckel5c531332020-03-02 01:35:30 +01002103/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002104 * @brief Close an open map.
2105 *
2106 * @param[in] pCtx The encoding context to close the map in.
2107 *
2108 * This closes a map opened by QCBOREncode_OpenMap(). It reduces
2109 * nesting level by one.
2110 *
2111 * When an error occurs as a result of this call, the encoder records
2112 * the error and enters the error state. The error will be returned
2113 * when QCBOREncode_Finish() is called.
2114 *
2115 * If this has been called more times than QCBOREncode_OpenMap(), then
2116 * @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when
2117 * QCBOREncode_Finish() is called.
2118 *
2119 * If this is called and it is not a map that is currently open,
2120 * @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when
2121 * QCBOREncode_Finish() is called.
Michael Eckel5c531332020-03-02 01:35:30 +01002122 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002123static void
2124QCBOREncode_CloseMap(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01002125
2126
2127/**
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002128 * @brief Indicates that the next items added are in an indefinite length array.
2129 *
2130 * @param[in] pCtx The encoding context to open the array in.
2131 *
2132 * This is the same as QCBOREncode_OpenArray() except the array is
2133 * indefinite length.
2134 *
2135 * This must be closed with QCBOREncode_CloseArrayIndefiniteLength().
2136 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002137static void
2138QCBOREncode_OpenArrayIndefiniteLength(QCBOREncodeContext *pCtx);
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002139
Laurence Lundblade3eead482023-12-16 20:53:22 -07002140static void
2141QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pCtx,
2142 const char *szLabel);
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002143
2144static void
2145QCBOREncode_OpenArrayIndefiniteLengthInMapN(QCBOREncodeContext *pCtx,
2146 int64_t nLabel);
2147
2148
2149/**
2150 * @brief Close an open indefinite length array.
2151 *
2152 * @param[in] pCtx The encoding context to close the array in.
2153 *
2154 * This is the same as QCBOREncode_CloseArray(), but the open array
2155 * that is being close must be of indefinite length.
2156 */
2157static void
2158QCBOREncode_CloseArrayIndefiniteLength(QCBOREncodeContext *pCtx);
2159
2160
2161/**
2162 * @brief Indicates that the next items added are in an indefinite length map.
2163 *
2164 * @param[in] pCtx The encoding context to open the map in.
2165 *
2166 * This is the same as QCBOREncode_OpenMap() except the array is
2167 * indefinite length.
2168 *
2169 * This must be closed with QCBOREncode_CloseMapIndefiniteLength().
2170 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002171static void
2172QCBOREncode_OpenMapIndefiniteLength(QCBOREncodeContext *pCtx);
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002173
Laurence Lundblade3eead482023-12-16 20:53:22 -07002174static void
2175QCBOREncode_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pCtx,
2176 const char *szLabel);
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002177
2178static void
2179QCBOREncode_OpenMapIndefiniteLengthInMapN(QCBOREncodeContext *pCtx,
2180 int64_t nLabel);
2181
2182
2183/**
2184 * @brief Close an open indefinite length map.
2185 *
2186 * @param[in] pCtx The encoding context to close the map in.
2187 *
2188 * This is the same as QCBOREncode_CloseMap(), but the open map that
2189 * is being close must be of indefinite length.
2190 */
2191static void
2192QCBOREncode_CloseMapIndefiniteLength(QCBOREncodeContext *pCtx);
2193
2194
Laurence Lundbladec92e4162023-11-27 21:51:26 -07002195/**
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08002196 * @brief Close and sort an open map.
Laurence Lundbladed6e13022023-11-26 10:14:02 -07002197 *
2198 * @param[in] pCtx The encoding context to close the map in .
2199 *
2200 * This is the same as QCBOREncode_CloseMap() except it sorts the map
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08002201 * per RFC 8949 Section 4.2.1. This sort is lexicographic of the CBOR-encoded
2202 * map labels.
Laurence Lundbladed6e13022023-11-26 10:14:02 -07002203 *
2204 * This is more expensive than most things in the encoder. It uses
2205 * bubble sort which runs in n-squared time where n is the number of
2206 * map items. Sorting large maps on slow CPUs might be slow. This is
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08002207 * also increases the object code size of the encoder by about 30%
2208 * (500-1000 bytes).
Laurence Lundbladed6e13022023-11-26 10:14:02 -07002209 *
2210 * Bubble sort was selected so as to not need an extra buffer to track
2211 * map item offsets. Bubble sort works well even though map items are
2212 * not all the same size because it always swaps adjacent items.
2213 */
2214void QCBOREncode_CloseAndSortMap(QCBOREncodeContext *pCtx);
2215
2216void QCBOREncode_CloseAndSortMapIndef(QCBOREncodeContext *pCtx);
2217
2218
2219/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002220 * @brief Indicate start of encoded CBOR to be wrapped in a bstr.
2221 *
2222 * @param[in] pCtx The encoding context to open the bstr-wrapped CBOR in.
2223 *
2224 * All added encoded items between this call and a call to
2225 * QCBOREncode_CloseBstrWrap2() will be wrapped in a bstr. They will
2226 * appear in the final output as a byte string. That byte string will
2227 * contain encoded CBOR. This increases nesting level by one.
2228 *
2229 * The typical use case is for encoded CBOR that is to be
2230 * cryptographically hashed, as part of a [RFC 8152, COSE]
2231 * (https://tools.ietf.org/html/rfc8152) implementation. The wrapping
2232 * byte string is taken as input by the hash function (which is why it
2233 * is returned by QCBOREncode_CloseBstrWrap2()). It is also easy to
2234 * recover on decoding with standard CBOR decoders.
2235 *
2236 * Using QCBOREncode_BstrWrap() and QCBOREncode_CloseBstrWrap2()
2237 * avoids having to encode the items first in one buffer (e.g., the
2238 * COSE payload) and then add that buffer as a bstr to another
2239 * encoding (e.g. the COSE to-be-signed bytes, the @c Sig_structure)
2240 * potentially halving the memory needed.
2241 *
2242 * CBOR by nature must be decoded item by item in order from the
2243 * start. By wrapping some CBOR in a byte string, the decoding of
2244 * that wrapped CBOR can be skipped. This is another use of wrapping,
2245 * perhaps because the CBOR is large and deeply nested. Perhaps APIs
2246 * for handling one defined CBOR message that is being embedded in
2247 * another only take input as a byte string. Perhaps the desire is to
2248 * be able to decode the out layer even in the wrapped has errors.
Michael Eckel5c531332020-03-02 01:35:30 +01002249 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002250static void
2251QCBOREncode_BstrWrap(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01002252
Laurence Lundblade3eead482023-12-16 20:53:22 -07002253static void
2254QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002255
Laurence Lundblade3eead482023-12-16 20:53:22 -07002256static void
2257QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002258
2259
2260/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002261 * @brief Close a wrapping bstr.
2262 *
2263 * @param[in] pCtx The encoding context to close of bstr wrapping in.
2264 * @param[in] bIncludeCBORHead Include the encoded CBOR head of the bstr
2265 * as well as the bytes in @c pWrappedCBOR.
2266 * @param[out] pWrappedCBOR A @ref UsefulBufC containing wrapped bytes.
2267 *
2268 * The closes a wrapping bstr opened by QCBOREncode_BstrWrap(). It reduces
2269 * nesting level by one.
2270 *
2271 * A pointer and length of the enclosed encoded CBOR is returned in @c
2272 * *pWrappedCBOR if it is not @c NULL. The main purpose of this is so
2273 * this data can be hashed (e.g., with SHA-256) as part of a [RFC
2274 * 8152, COSE] (https://tools.ietf.org/html/rfc8152)
2275 * implementation. **WARNING**, this pointer and length should be used
2276 * right away before any other calls to @c QCBOREncode_CloseXxx() as
2277 * they will move data around and the pointer and length will no
2278 * longer be to the correct encoded CBOR.
2279 *
2280 * When an error occurs as a result of this call, the encoder records
2281 * the error and enters the error state. The error will be returned
2282 * when QCBOREncode_Finish() is called.
2283 *
2284 * If this has been called more times than QCBOREncode_BstrWrap(),
2285 * then @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when
2286 * QCBOREncode_Finish() is called.
2287 *
2288 * If this is called and it is not a wrapping bstr that is currently
2289 * open, @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when
2290 * QCBOREncode_Finish() is called.
2291 *
2292 * QCBOREncode_CloseBstrWrap() is a deprecated version of this function
2293 * that is equivalent to the call with @c bIncludeCBORHead @c true.
Michael Eckel5c531332020-03-02 01:35:30 +01002294 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002295void
2296QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *pCtx, bool bIncludeCBORHead, UsefulBufC *pWrappedCBOR);
Michael Eckel5c531332020-03-02 01:35:30 +01002297
Laurence Lundblade3eead482023-12-16 20:53:22 -07002298static void
2299QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pCtx, UsefulBufC *pWrappedCBOR);
Michael Eckel5c531332020-03-02 01:35:30 +01002300
2301
2302/**
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07002303 * @brief Cancel byte string wrapping.
2304 *
2305 * @param[in] pCtx The encoding context.
2306 *
2307 * This cancels QCBOREncode_BstrWrap() making tghe encoding as if it
2308 * were never called.
2309 *
2310 * WARNING: This does not work on QCBOREncode_BstrWrapInMap()
2311 * or QCBOREncode_BstrWrapInMapN() and there is no error detection
2312 * of an attempt at their use.
2313 *
2314 * This only works if nothing has been added into the wrapped byte
2315 * string. If something has been added, this sets the error
2316 * @ref QCBOR_ERR_CANNOT_CANCEL.
2317 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002318void
2319QCBOREncode_CancelBstrWrap(QCBOREncodeContext *pCtx);
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07002320
2321
2322/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002323 * @brief Add some already-encoded CBOR bytes.
2324 *
2325 * @param[in] pCtx The encoding context to add the already-encode CBOR to.
2326 * @param[in] Encoded The already-encoded CBOR to add to the context.
2327 *
2328 * The encoded CBOR being added must be fully conforming CBOR. It must
2329 * be complete with no arrays or maps that are incomplete. While this
2330 * encoder doesn't ever produce indefinite lengths, it is OK for the
2331 * raw CBOR added here to have indefinite lengths.
2332 *
2333 * The raw CBOR added here is not checked in anyway. If it is not
2334 * conforming or has open arrays or such, the final encoded CBOR
2335 * will probably be wrong or not what was intended.
2336 *
2337 * If the encoded CBOR being added here contains multiple items, they
2338 * must be enclosed in a map or array. At the top level the raw
2339 * CBOR must be a single data item.
Michael Eckel5c531332020-03-02 01:35:30 +01002340 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002341static void
2342QCBOREncode_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01002343
Laurence Lundblade3eead482023-12-16 20:53:22 -07002344static void
2345QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01002346
Laurence Lundblade3eead482023-12-16 20:53:22 -07002347static void
2348QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01002349
2350
2351/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002352 * @brief Get the encoded result.
2353 *
2354 * @param[in] pCtx The context to finish encoding with.
2355 * @param[out] pEncodedCBOR Structure in which the pointer and length of
2356 * the encoded CBOR is returned.
2357 *
2358 * @retval QCBOR_SUCCESS Encoded CBOR is returned.
2359 *
2360 * @retval QCBOR_ERR_TOO_MANY_CLOSES Nesting error
2361 *
2362 * @retval QCBOR_ERR_CLOSE_MISMATCH Nesting error
2363 *
2364 * @retval QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN Nesting error
2365 *
2366 * @retval QCBOR_ERR_BUFFER_TOO_LARGE Encoded output buffer size
2367 *
2368 * @retval QCBOR_ERR_BUFFER_TOO_SMALL Encoded output buffer size
2369 *
2370 * @retval QCBOR_ERR_ARRAY_NESTING_TOO_DEEP Implementation limit
2371 *
2372 * @retval QCBOR_ERR_ARRAY_TOO_LONG Implementation limit
2373 *
2374 * On success, the pointer and length of the encoded CBOR are returned
2375 * in @c *pEncodedCBOR. The pointer is the same pointer that was passed
2376 * in to QCBOREncode_Init(). Note that it is not const when passed to
2377 * QCBOREncode_Init(), but it is const when returned here. The length
2378 * will be smaller than or equal to the length passed in when
2379 * QCBOREncode_Init() as this is the length of the actual result, not
2380 * the size of the buffer it was written to.
2381 *
2382 * If a @c NULL was passed for @c Storage.ptr when QCBOREncode_Init()
2383 * was called, @c NULL will be returned here, but the length will be
2384 * that of the CBOR that would have been encoded.
2385 *
2386 * Encoding errors primarily manifest here as most other encoding function
2387 * do no return an error. They just set the error state in the encode
2388 * context after which no encoding function does anything.
2389 *
2390 * Three types of errors manifest here. The first type are nesting
2391 * errors where the number of @c QCBOREncode_OpenXxx() calls do not
2392 * match the number @c QCBOREncode_CloseXxx() calls. The solution is to
2393 * fix the calling code.
2394 *
2395 * The second type of error is because the buffer given is either too
2396 * small or too large. The remedy is to give a correctly sized buffer.
2397 *
2398 * The third type are due to limits in this implementation.
2399 * @ref QCBOR_ERR_ARRAY_NESTING_TOO_DEEP can be worked around by
2400 * encoding the CBOR in two (or more) phases and adding the CBOR from
2401 * the first phase to the second with @c QCBOREncode_AddEncoded().
2402 *
2403 * If an error is returned, the buffer may have partially encoded
2404 * incorrect CBOR in it and it should not be used. Likewise, the length
2405 * may be incorrect and should not be used.
2406 *
2407 * Note that the error could have occurred in one of the many
2408 * @c QCBOREncode_AddXxx() calls long before QCBOREncode_Finish() was
2409 * called. This error handling reduces the CBOR implementation size
2410 * but makes debugging harder.
2411 *
2412 * This may be called multiple times. It will always return the
2413 * same. It can also be interleaved with calls to
2414 * QCBOREncode_FinishGetSize().
2415 *
2416 * QCBOREncode_GetErrorState() can be called to get the current
2417 * error state in order to abort encoding early as an optimization, but
2418 * calling it is is never required.
Michael Eckel5c531332020-03-02 01:35:30 +01002419 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002420QCBORError
2421QCBOREncode_Finish(QCBOREncodeContext *pCtx, UsefulBufC *pEncodedCBOR);
Michael Eckel5c531332020-03-02 01:35:30 +01002422
2423
2424/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002425 * @brief Get the encoded CBOR and error status.
2426 *
2427 * @param[in] pCtx The context to finish encoding with.
2428 * @param[out] uEncodedLen The length of the encoded or potentially
2429 * encoded CBOR in bytes.
2430 *
2431 * @return The same errors as QCBOREncode_Finish().
2432 *
2433 * This functions the same as QCBOREncode_Finish(), but only returns the
2434 * size of the encoded output.
Michael Eckel5c531332020-03-02 01:35:30 +01002435 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002436QCBORError
2437QCBOREncode_FinishGetSize(QCBOREncodeContext *pCtx, size_t *uEncodedLen);
Michael Eckel5c531332020-03-02 01:35:30 +01002438
2439
2440/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002441 * @brief Indicate whether output buffer is NULL or not.
2442 *
2443 * @param[in] pCtx The encoding context.
2444 *
2445 * @return 1 if the output buffer is @c NULL.
2446 *
2447 * Sometimes a @c NULL input buffer is given to QCBOREncode_Init() so
2448 * that the size of the generated CBOR can be calculated without
2449 * allocating a buffer for it. This returns 1 when the output buffer
2450 * is @c NULL and 0 when it is not.
Michael Eckel5c531332020-03-02 01:35:30 +01002451 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002452static int
2453QCBOREncode_IsBufferNULL(QCBOREncodeContext *pCtx);
2454
2455
2456/**
2457 * @brief Get the encoding error state.
2458 *
2459 * @param[in] pCtx The encoding context.
2460 *
2461 * @return One of @ref QCBORError. See return values from
2462 * QCBOREncode_Finish()
2463 *
2464 * Normally encoding errors need only be handled at the end of
2465 * encoding when QCBOREncode_Finish() is called. This can be called to
2466 * get the error result before finish should there be a need to halt
2467 * encoding before QCBOREncode_Finish() is called.
2468 */
2469static QCBORError
2470QCBOREncode_GetErrorState(QCBOREncodeContext *pCtx);
2471
2472
2473/**
2474 * Encode the "head" of a CBOR data item.
2475 *
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002476 * @param Buffer Buffer to output the encoded head to; must be
Laurence Lundblade3eead482023-12-16 20:53:22 -07002477 * @ref QCBOR_HEAD_BUFFER_SIZE bytes in size.
2478 * @param uMajorType One of CBOR_MAJOR_TYPE_XX.
2479 * @param uMinLen The minimum number of bytes to encode uNumber. Almost
2480 * always this is 0 to use preferred
2481 * serialization. If this is 4, then even the
2482 * values 0xffff and smaller will be encoded in 4
2483 * bytes. This is used primarily when encoding a
2484 * float or double put into uNumber as the leading
2485 * zero bytes for them must be encoded.
2486 * @param uNumber The numeric argument part of the CBOR head.
2487 * @return Pointer and length of the encoded head or
2488 * @ref NULLUsefulBufC if the output buffer is too small.
2489 *
2490 * Callers do not to need to call this for normal CBOR encoding. Note
2491 * that it doesn't even take a @ref QCBOREncodeContext argument.
2492 *
2493 * This encodes the major type and argument part of a data item. The
2494 * argument is an integer that is usually either the value or the length
2495 * of the data item.
2496 *
2497 * This is exposed in the public interface to allow hashing of some CBOR
2498 * data types, bstr in particular, a chunk at a time so the full CBOR
2499 * doesn't have to be encoded in a contiguous buffer.
2500 *
2501 * For example, if you have a 100,000 byte binary blob in a buffer that
2502 * needs to be a bstr encoded and then hashed. You could allocate a
2503 * 100,010 byte buffer and encode it normally. Alternatively, you can
2504 * encode the head in a 10 byte buffer with this function, hash that and
2505 * then hash the 100,000 bytes using the same hash context.
Laurence Lundblade3eead482023-12-16 20:53:22 -07002506 */
2507UsefulBufC
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002508QCBOREncode_EncodeHead(UsefulBuf Buffer,
Laurence Lundblade3eead482023-12-16 20:53:22 -07002509 uint8_t uMajorType,
2510 uint8_t uMinLen,
2511 uint64_t uNumber);
Michael Eckel5c531332020-03-02 01:35:30 +01002512
2513
Michael Eckel5c531332020-03-02 01:35:30 +01002514
2515
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002516/* =========================================================================
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002517 BEGINNING OF PRIVATE IMPLEMENTATION
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002518 ========================================================================= */
Michael Eckel5c531332020-03-02 01:35:30 +01002519
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002520/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002521void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002522QCBOREncode_Private_AddBuffer(QCBOREncodeContext *pCtx,
2523 uint8_t uMajorType,
2524 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002525
2526
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002527/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002528void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002529QCBOREncode_Private_OpenMapOrArray(QCBOREncodeContext *pCtx,
2530 uint8_t uMajorType);
Michael Eckel5c531332020-03-02 01:35:30 +01002531
2532
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002533/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002534void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002535QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx,
2536 uint8_t uMajorType);
Michael Eckel5c531332020-03-02 01:35:30 +01002537
2538
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002539/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002540void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002541QCBOREncode_Private_CloseMapOrArray(QCBOREncodeContext *pCtx,
2542 uint8_t uMajorType);
Michael Eckel5c531332020-03-02 01:35:30 +01002543
2544
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002545/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002546void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002547QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx,
2548 uint8_t uMajorType);
Michael Eckel5c531332020-03-02 01:35:30 +01002549
2550
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002551/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002552void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002553QCBOREncode_Private_AddType7(QCBOREncodeContext *pCtx,
2554 uint8_t uMinLen,
2555 uint64_t uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002556
2557
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002558/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002559void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002560QCBOREncode_Private_AddExpMantissa(QCBOREncodeContext *pCtx,
Laurence Lundblade3eead482023-12-16 20:53:22 -07002561 uint64_t uTag,
2562 UsefulBufC BigNumMantissa,
2563 bool bBigNumIsNegative,
2564 int64_t nMantissa,
2565 int64_t nExponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002566
Michael Eckel5c531332020-03-02 01:35:30 +01002567
2568
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08002569
2570static inline void
2571QCBOREncode_SerializationCDE(QCBOREncodeContext *pMe)
2572{
2573 /* The use of a function pointer here is a little trick to reduce
2574 * code linked for the common use cases that don't sort. If this
2575 * function is never linked, then QCBOREncode_CloseAndSortMap() is
2576 * never linked and the amount of code pulled in is small. If the
2577 * mode switch between sorting and not sorting were an if
2578 * statement, then QCBOREncode_CloseAndSortMap() would always be
2579 * linked even when not used. */
2580 pMe->pfnCloseMap = QCBOREncode_CloseAndSortMap;
2581 pMe->uMode = QCBOR_ENCODE_MODE_CDE;
2582}
2583
2584static inline void
2585QCBOREncode_SerializationdCBOR(QCBOREncodeContext *pMe)
2586{
2587 pMe->pfnCloseMap = QCBOREncode_CloseAndSortMap;
2588 pMe->uMode = QCBOR_ENCODE_MODE_DCBOR;
2589}
2590
2591static inline void
2592QCBOREncode_SerializationPreferred(QCBOREncodeContext *pMe)
2593{
2594 pMe->uMode = QCBOR_ENCODE_MODE_PREFERRED;
2595}
2596
2597static inline void
2598QCBOREncode_Allow(QCBOREncodeContext *pMe, const uint8_t uAllow)
2599{
2600#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2601 pMe->uAllow = uAllow;
2602#else
2603 (void)uAllow;
2604 (void)pMe;
2605#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2606}
2607
2608
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002609static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002610QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pMe,
2611 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002612 const int64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002613{
Laurence Lundblade3eead482023-12-16 20:53:22 -07002614 /* Use _AddBuffer() because _AddSZString() is defined below, not above */
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002615 QCBOREncode_Private_AddBuffer(pMe,
2616 CBOR_MAJOR_TYPE_TEXT_STRING,
2617 UsefulBuf_FromSZ(szLabel));
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002618 QCBOREncode_AddInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002619}
2620
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002621static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002622QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002623 const int64_t nLabel,
2624 const int64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002625{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002626 QCBOREncode_AddInt64(pMe, nLabel);
2627 QCBOREncode_AddInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002628}
2629
2630
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002631static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002632QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pMe,
2633 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002634 const uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002635{
Laurence Lundblade3eead482023-12-16 20:53:22 -07002636 /* Use _AddBuffer() because _AddSZString() is defined below, not above */
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002637 QCBOREncode_Private_AddBuffer(pMe,
2638 CBOR_MAJOR_TYPE_TEXT_STRING,
2639 UsefulBuf_FromSZ(szLabel));
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002640 QCBOREncode_AddUInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002641}
2642
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002643static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002644QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002645 const int64_t nLabel,
2646 const uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002647{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002648 QCBOREncode_AddInt64(pMe, nLabel);
2649 QCBOREncode_AddUInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002650}
2651
2652
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002653static inline void
Laurence Lundblade2d493002024-02-01 11:09:17 -07002654QCBOREncode_AddNegativeUInt64ToMap(QCBOREncodeContext *pMe, const char *szLabel, uint64_t uNum)
2655{
2656 /* Use _AddBuffer() because _AddSZString() is defined below, not above */
2657 QCBOREncode_Private_AddBuffer(pMe,
2658 CBOR_MAJOR_TYPE_TEXT_STRING,
2659 UsefulBuf_FromSZ(szLabel));
2660 QCBOREncode_AddNegativeUInt64(pMe, uNum);
2661}
2662
2663static inline void
2664QCBOREncode_AddNegativeUInt64ToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint64_t uNum)
2665{
2666 QCBOREncode_AddInt64(pMe, nLabel);
2667 QCBOREncode_AddNegativeUInt64(pMe, uNum);
2668}
2669
2670
2671static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002672QCBOREncode_AddText(QCBOREncodeContext *pMe, const UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002673{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002674 QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_TYPE_TEXT_STRING, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002675}
2676
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002677static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002678QCBOREncode_AddTextToMap(QCBOREncodeContext *pMe,
2679 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002680 const UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002681{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002682 QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szLabel));
2683 QCBOREncode_AddText(pMe, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002684}
2685
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002686static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002687QCBOREncode_AddTextToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002688 const int64_t nLabel,
2689 const UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002690{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002691 QCBOREncode_AddInt64(pMe, nLabel);
2692 QCBOREncode_AddText(pMe, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002693}
2694
2695
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002696inline static void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002697QCBOREncode_AddSZString(QCBOREncodeContext *pMe, const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002698{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002699 QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szString));
Michael Eckel5c531332020-03-02 01:35:30 +01002700}
2701
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002702static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002703QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pMe,
2704 const char *szLabel,
2705 const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002706{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002707 QCBOREncode_AddSZString(pMe, szLabel);
2708 QCBOREncode_AddSZString(pMe, szString);
Michael Eckel5c531332020-03-02 01:35:30 +01002709}
2710
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002711static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002712QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002713 const int64_t nLabel,
Laurence Lundblade3eead482023-12-16 20:53:22 -07002714 const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002715{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002716 QCBOREncode_AddInt64(pMe, nLabel);
2717 QCBOREncode_AddSZString(pMe, szString);
Michael Eckel5c531332020-03-02 01:35:30 +01002718}
2719
2720
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002721#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002722static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002723QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pMe,
2724 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002725 const double dNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002726{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002727 QCBOREncode_AddSZString(pMe, szLabel);
2728 QCBOREncode_AddDouble(pMe, dNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002729}
2730
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002731static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002732QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002733 const int64_t nLabel,
2734 const double dNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002735{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002736 QCBOREncode_AddInt64(pMe, nLabel);
2737 QCBOREncode_AddDouble(pMe, dNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002738}
2739
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002740static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002741QCBOREncode_AddFloatToMap(QCBOREncodeContext *pMe,
2742 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002743 const float dNum)
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002744{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002745 QCBOREncode_AddSZString(pMe, szLabel);
2746 QCBOREncode_AddFloat(pMe, dNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002747}
2748
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002749static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002750QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pMe,
2751 const int64_t nLabel,
2752 const float fNum)
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002753{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002754 QCBOREncode_AddInt64(pMe, nLabel);
2755 QCBOREncode_AddFloat(pMe, fNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002756}
2757
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002758static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002759QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pMe,
2760 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002761 const double dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002762{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002763 QCBOREncode_AddSZString(pMe, szLabel);
2764 QCBOREncode_AddDoubleNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002765}
2766
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002767static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002768QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002769 const int64_t nLabel,
2770 const double dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002771{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002772 QCBOREncode_AddInt64(pMe, nLabel);
2773 QCBOREncode_AddDoubleNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002774}
2775
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002776static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002777QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pMe,
2778 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002779 const float dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002780{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002781 QCBOREncode_AddSZString(pMe, szLabel);
2782 QCBOREncode_AddFloatNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002783}
2784
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002785static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002786QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002787 const int64_t nLabel,
2788 const float dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002789{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002790 QCBOREncode_AddInt64(pMe, nLabel);
2791 QCBOREncode_AddFloatNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002792}
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002793#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002794
Michael Eckel5c531332020-03-02 01:35:30 +01002795
Laurence Lundblade9b334962020-08-27 10:55:53 -07002796
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002797static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002798QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pMe,
2799 const uint8_t uTag,
2800 const int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002801{
2802 if(uTag == QCBOR_ENCODE_AS_TAG) {
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002803 QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_EPOCH);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002804 }
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002805 QCBOREncode_AddInt64(pMe, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002806}
2807
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002808static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002809QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pMe,
2810 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002811 const uint8_t uTag,
2812 const int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002813{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002814 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002815 QCBOREncode_AddTDateEpoch(pMe, uTag, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002816}
2817
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002818static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002819QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002820 const int64_t nLabel,
2821 const uint8_t uTag,
2822 const int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002823{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002824 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002825 QCBOREncode_AddTDateEpoch(pMe, uTag, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002826}
2827
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002828static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002829QCBOREncode_AddDateEpoch(QCBOREncodeContext *pMe,
2830 const int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002831{
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002832 QCBOREncode_AddTDateEpoch(pMe, QCBOR_ENCODE_AS_TAG, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002833}
2834
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002835static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002836QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pMe,
2837 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002838 const int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002839{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002840 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002841 QCBOREncode_AddDateEpoch(pMe, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002842}
2843
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002844static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002845QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002846 const int64_t nLabel,
2847 const int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002848{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002849 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002850 QCBOREncode_AddDateEpoch(pMe, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002851}
2852
2853
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002854static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002855QCBOREncode_AddTDaysEpoch(QCBOREncodeContext *pMe,
2856 const uint8_t uTag,
2857 const int64_t nDays)
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002858{
2859 if(uTag == QCBOR_ENCODE_AS_TAG) {
2860 QCBOREncode_AddTag(pMe, CBOR_TAG_DAYS_EPOCH);
2861 }
2862 QCBOREncode_AddInt64(pMe, nDays);
2863}
2864
2865static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002866QCBOREncode_AddTDaysEpochToMapSZ(QCBOREncodeContext *pMe,
2867 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002868 const uint8_t uTag,
2869 const int64_t nDays)
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002870{
2871 QCBOREncode_AddSZString(pMe, szLabel);
2872 QCBOREncode_AddTDaysEpoch(pMe, uTag, nDays);
2873}
2874
2875static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002876QCBOREncode_AddTDaysEpochToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002877 const int64_t nLabel,
2878 const uint8_t uTag,
2879 const int64_t nDays)
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002880{
2881 QCBOREncode_AddInt64(pMe, nLabel);
2882 QCBOREncode_AddTDaysEpoch(pMe, uTag, nDays);
2883}
2884
Laurence Lundblade9b334962020-08-27 10:55:53 -07002885
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002886static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002887QCBOREncode_AddBytes(QCBOREncodeContext *pMe,
2888 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002889{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002890 QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002891}
2892
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002893static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002894QCBOREncode_AddBytesToMap(QCBOREncodeContext *pMe,
2895 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002896 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002897{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002898 QCBOREncode_AddSZString(pMe, szLabel);
2899 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002900}
2901
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002902static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002903QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002904 const int64_t nLabel,
2905 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002906{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002907 QCBOREncode_AddInt64(pMe, nLabel);
2908 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002909}
2910
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002911static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002912QCBOREncode_OpenBytesInMapSZ(QCBOREncodeContext *pMe,
2913 const char *szLabel,
2914 UsefulBuf *pPlace)
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002915{
2916 QCBOREncode_AddSZString(pMe, szLabel);
2917 QCBOREncode_OpenBytes(pMe, pPlace);
2918}
2919
2920static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002921QCBOREncode_OpenBytesInMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002922 const int64_t nLabel,
Laurence Lundblade3eead482023-12-16 20:53:22 -07002923 UsefulBuf *pPlace)
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06002924{
2925 QCBOREncode_AddInt64(pMe, nLabel);
2926 QCBOREncode_OpenBytes(pMe, pPlace);
2927}
2928
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002929
2930static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002931QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002932 const uint8_t uTagRequirement,
2933 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002934{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002935 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2936 QCBOREncode_AddTag(pMe, CBOR_TAG_BIN_UUID);
2937 }
2938 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002939}
2940
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002941static inline void
2942QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pMe,
2943 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002944 const uint8_t uTagRequirement,
2945 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002946{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002947 QCBOREncode_AddSZString(pMe, szLabel);
2948 QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002949}
2950
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002951static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002952QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002953 const int64_t nLabel,
2954 const uint8_t uTagRequirement,
2955 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002956{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002957 QCBOREncode_AddInt64(pMe, nLabel);
2958 QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002959}
2960
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002961static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002962QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pMe, const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002963{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002964 QCBOREncode_AddTBinaryUUID(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002965}
2966
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002967static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002968QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pMe,
2969 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002970 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002971{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002972 QCBOREncode_AddTBinaryUUIDToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002973}
2974
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002975static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002976QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002977 const int64_t nLabel,
2978 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002979{
Laurence Lundblade3eead482023-12-16 20:53:22 -07002980 QCBOREncode_AddTBinaryUUIDToMapN(pMe,
2981 nLabel,
2982 QCBOR_ENCODE_AS_TAG,
2983 Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002984}
2985
2986
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002987static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002988QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002989 const uint8_t uTagRequirement,
2990 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002991{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002992 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2993 QCBOREncode_AddTag(pMe, CBOR_TAG_POS_BIGNUM);
2994 }
2995 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002996}
2997
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002998static inline void
2999QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pMe,
3000 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003001 const uint8_t uTagRequirement,
3002 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003003{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003004 QCBOREncode_AddSZString(pMe, szLabel);
3005 QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003006}
3007
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003008static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003009QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003010 const int64_t nLabel,
3011 const uint8_t uTagRequirement,
3012 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003013{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003014 QCBOREncode_AddInt64(pMe, nLabel);
3015 QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003016}
3017
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003018static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003019QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pMe, const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003020{
3021 QCBOREncode_AddTPositiveBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
3022}
3023
3024static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003025QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pMe,
3026 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003027 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003028{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003029 QCBOREncode_AddTPositiveBignumToMapSZ(pMe,
3030 szLabel,
3031 QCBOR_ENCODE_AS_TAG,
3032 Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003033}
3034
3035static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003036QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003037 const int64_t nLabel,
3038 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003039{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003040 QCBOREncode_AddTPositiveBignumToMapN(pMe,
3041 nLabel,
3042 QCBOR_ENCODE_AS_TAG,
3043 Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003044}
3045
3046
3047static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003048QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003049 const uint8_t uTagRequirement,
3050 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003051{
3052 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3053 QCBOREncode_AddTag(pMe, CBOR_TAG_NEG_BIGNUM);
3054 }
3055 QCBOREncode_AddBytes(pMe, Bytes);
3056}
3057
3058static inline void
3059QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pMe,
3060 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003061 const uint8_t uTagRequirement,
3062 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003063{
3064 QCBOREncode_AddSZString(pMe, szLabel);
3065 QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
3066}
3067
3068static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003069QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003070 const int64_t nLabel,
3071 const uint8_t uTagRequirement,
3072 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003073{
3074 QCBOREncode_AddInt64(pMe, nLabel);
3075 QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
3076}
3077
3078static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003079QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pMe, const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003080{
3081 QCBOREncode_AddTNegativeBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
3082}
3083
3084static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003085QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pMe,
3086 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003087 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003088{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003089 QCBOREncode_AddTNegativeBignumToMapSZ(pMe,
3090 szLabel,
3091 QCBOR_ENCODE_AS_TAG,
3092 Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003093}
3094
3095static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003096QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003097 const int64_t nLabel,
3098 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003099{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003100 QCBOREncode_AddTNegativeBignumToMapN(pMe,
3101 nLabel,
3102 QCBOR_ENCODE_AS_TAG,
3103 Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003104}
3105
3106
Michael Eckel5c531332020-03-02 01:35:30 +01003107
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003108#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Michael Eckel5c531332020-03-02 01:35:30 +01003109
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003110static inline void
3111QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003112 const uint8_t uTagRequirement,
3113 const int64_t nMantissa,
3114 const int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003115{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003116 uint64_t uTag;
3117 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3118 uTag = CBOR_TAG_DECIMAL_FRACTION;
3119 } else {
3120 uTag = CBOR_TAG_INVALID64;
3121 }
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003122 QCBOREncode_Private_AddExpMantissa(pMe,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003123 uTag,
Michael Eckel5c531332020-03-02 01:35:30 +01003124 NULLUsefulBufC,
3125 false,
3126 nMantissa,
3127 nBase10Exponent);
3128}
3129
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003130static inline void
3131QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pMe,
3132 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003133 const uint8_t uTagRequirement,
3134 const int64_t nMantissa,
3135 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003136{
3137 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003138 QCBOREncode_AddTDecimalFraction(pMe,
3139 uTagRequirement,
3140 nMantissa,
3141 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003142}
3143
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003144static inline void
3145QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003146 const int64_t nLabel,
3147 const uint8_t uTagRequirement,
3148 const int64_t nMantissa,
3149 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003150{
3151 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003152 QCBOREncode_AddTDecimalFraction(pMe,
3153 uTagRequirement,
3154 nMantissa,
3155 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003156}
3157
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003158static inline void
3159QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003160 const int64_t nMantissa,
3161 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003162{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003163 QCBOREncode_AddTDecimalFraction(pMe,
3164 QCBOR_ENCODE_AS_TAG,
3165 nMantissa,
3166 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003167}
3168
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003169static inline void
3170QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pMe,
3171 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003172 const int64_t nMantissa,
3173 const int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003174{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003175 QCBOREncode_AddTDecimalFractionToMapSZ(pMe,
3176 szLabel,
3177 QCBOR_ENCODE_AS_TAG,
3178 nMantissa,
3179 nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003180}
3181
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003182static inline void
3183QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003184 const int64_t nLabel,
3185 const int64_t nMantissa,
3186 const int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003187{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003188 QCBOREncode_AddTDecimalFractionToMapN(pMe,
3189 nLabel,
3190 QCBOR_ENCODE_AS_TAG,
3191 nMantissa,
3192 nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003193}
3194
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003195
3196
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003197static inline void
3198QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003199 const uint8_t uTagRequirement,
3200 const UsefulBufC Mantissa,
3201 const bool bIsNegative,
3202 const int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003203{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003204 uint64_t uTag;
3205 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3206 uTag = CBOR_TAG_DECIMAL_FRACTION;
3207 } else {
3208 uTag = CBOR_TAG_INVALID64;
3209 }
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003210 QCBOREncode_Private_AddExpMantissa(pMe,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003211 uTag,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003212 Mantissa,
3213 bIsNegative,
Michael Eckel5c531332020-03-02 01:35:30 +01003214 0,
3215 nBase10Exponent);
3216}
3217
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003218static inline void
3219QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
3220 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003221 const uint8_t uTagRequirement,
3222 const UsefulBufC Mantissa,
3223 const bool bIsNegative,
3224 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003225{
3226 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003227 QCBOREncode_AddTDecimalFractionBigNum(pMe,
3228 uTagRequirement,
3229 Mantissa,
3230 bIsNegative,
3231 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003232}
3233
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003234static inline void
3235QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003236 const int64_t nLabel,
3237 const uint8_t uTagRequirement,
3238 const UsefulBufC Mantissa,
3239 const bool bIsNegative,
3240 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003241{
3242 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003243 QCBOREncode_AddTDecimalFractionBigNum(pMe,
3244 uTagRequirement,
3245 Mantissa,
3246 bIsNegative,
3247 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003248}
3249
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003250static inline void
3251QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003252 const UsefulBufC Mantissa,
3253 const bool bIsNegative,
3254 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003255{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003256 QCBOREncode_AddTDecimalFractionBigNum(pMe,
3257 QCBOR_ENCODE_AS_TAG,
3258 Mantissa,
3259 bIsNegative,
3260 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003261}
3262
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003263static inline void
3264QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
3265 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003266 const UsefulBufC Mantissa,
3267 const bool bIsNegative,
3268 const int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003269{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003270 QCBOREncode_AddTDecimalFractionBigNumToMapSZ(pMe,
3271 szLabel,
3272 QCBOR_ENCODE_AS_TAG,
3273 Mantissa,
3274 bIsNegative,
3275 nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003276}
3277
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003278static inline void
3279QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003280 const int64_t nLabel,
3281 const UsefulBufC Mantissa,
3282 const bool bIsNegative,
3283 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003284{
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003285 QCBOREncode_AddTDecimalFractionBigNumToMapN(pMe,
3286 nLabel,
3287 QCBOR_ENCODE_AS_TAG,
3288 Mantissa,
3289 bIsNegative,
3290 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003291}
3292
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003293
3294
3295
3296
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003297static inline void
3298QCBOREncode_AddTBigFloat(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003299 const uint8_t uTagRequirement,
3300 const int64_t nMantissa,
3301 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003302{
3303 uint64_t uTag;
3304 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3305 uTag = CBOR_TAG_BIGFLOAT;
3306 } else {
3307 uTag = CBOR_TAG_INVALID64;
3308 }
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003309 QCBOREncode_Private_AddExpMantissa(pMe,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003310 uTag,
3311 NULLUsefulBufC,
3312 false,
3313 nMantissa,
3314 nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003315}
3316
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003317static inline void
3318QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pMe,
3319 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003320 const uint8_t uTagRequirement,
3321 const int64_t nMantissa,
3322 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003323{
3324 QCBOREncode_AddSZString(pMe, szLabel);
3325 QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
3326}
3327
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003328static inline void
3329QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003330 const int64_t nLabel,
3331 const uint8_t uTagRequirement,
3332 const int64_t nMantissa,
3333 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003334{
3335 QCBOREncode_AddInt64(pMe, nLabel);
3336 QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
3337}
3338
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003339static inline void
3340QCBOREncode_AddBigFloat(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003341 const int64_t nMantissa,
3342 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003343{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003344 QCBOREncode_AddTBigFloat(pMe,
3345 QCBOR_ENCODE_AS_TAG,
3346 nMantissa,
3347 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003348}
3349
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003350static inline void
3351QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pMe,
3352 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003353 const int64_t nMantissa,
3354 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003355{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003356 QCBOREncode_AddTBigFloatToMapSZ(pMe,
3357 szLabel,
3358 QCBOR_ENCODE_AS_TAG,
3359 nMantissa,
3360 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003361}
3362
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003363static inline void
3364QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003365 const int64_t nLabel,
3366 const int64_t nMantissa,
3367 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003368{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003369 QCBOREncode_AddTBigFloatToMapN(pMe,
3370 nLabel,
3371 QCBOR_ENCODE_AS_TAG,
3372 nMantissa,
3373 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003374}
3375
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003376
3377
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003378static inline void
3379QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003380 const uint8_t uTagRequirement,
3381 const UsefulBufC Mantissa,
3382 const bool bIsNegative,
3383 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003384{
3385 uint64_t uTag;
3386 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3387 uTag = CBOR_TAG_BIGFLOAT;
3388 } else {
3389 uTag = CBOR_TAG_INVALID64;
3390 }
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003391 QCBOREncode_Private_AddExpMantissa(pMe,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003392 uTag,
3393 Mantissa,
3394 bIsNegative,
3395 0,
3396 nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003397}
3398
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003399static inline void
3400QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pMe,
3401 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003402 const uint8_t uTagRequirement,
3403 const UsefulBufC Mantissa,
3404 const bool bIsNegative,
3405 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003406{
3407 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003408 QCBOREncode_AddTBigFloatBigNum(pMe,
3409 uTagRequirement,
3410 Mantissa,
3411 bIsNegative,
3412 nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003413}
3414
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003415static inline void
3416QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003417 const int64_t nLabel,
3418 const uint8_t uTagRequirement,
3419 const UsefulBufC Mantissa,
3420 const bool bIsNegative,
3421 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003422{
3423 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003424 QCBOREncode_AddTBigFloatBigNum(pMe,
3425 uTagRequirement,
3426 Mantissa,
3427 bIsNegative,
3428 nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003429}
3430
3431
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003432static inline void
3433QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003434 const UsefulBufC Mantissa,
3435 const bool bIsNegative,
3436 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003437{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003438 QCBOREncode_AddTBigFloatBigNum(pMe,
3439 QCBOR_ENCODE_AS_TAG,
3440 Mantissa, bIsNegative,
3441 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003442}
3443
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003444static inline void
3445QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pMe,
3446 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003447 const UsefulBufC Mantissa,
3448 const bool bIsNegative,
3449 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003450{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003451 QCBOREncode_AddTBigFloatBigNumToMapSZ(pMe,
3452 szLabel,
3453 QCBOR_ENCODE_AS_TAG,
3454 Mantissa,
3455 bIsNegative,
3456 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003457}
3458
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003459static inline void
3460QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003461 const int64_t nLabel,
3462 const UsefulBufC Mantissa,
3463 const bool bIsNegative,
3464 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003465{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003466 QCBOREncode_AddTBigFloatBigNumToMapN(pMe,
3467 nLabel,
3468 QCBOR_ENCODE_AS_TAG,
3469 Mantissa,
3470 bIsNegative,
3471 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003472}
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003473#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Michael Eckel5c531332020-03-02 01:35:30 +01003474
3475
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003476static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003477QCBOREncode_AddTURI(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003478 const uint8_t uTagRequirement,
3479 const UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01003480{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003481 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3482 QCBOREncode_AddTag(pMe, CBOR_TAG_URI);
3483 }
3484 QCBOREncode_AddText(pMe, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01003485}
3486
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003487static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003488QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pMe,
3489 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003490 const uint8_t uTagRequirement,
3491 const UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01003492{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003493 QCBOREncode_AddSZString(pMe, szLabel);
3494 QCBOREncode_AddTURI(pMe, uTagRequirement, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01003495}
3496
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003497static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003498QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003499 const int64_t nLabel,
3500 const uint8_t uTagRequirement,
3501 const UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01003502{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003503 QCBOREncode_AddInt64(pMe, nLabel);
3504 QCBOREncode_AddTURI(pMe, uTagRequirement, URI);
3505}
3506
3507static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003508QCBOREncode_AddURI(QCBOREncodeContext *pMe, const UsefulBufC URI)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003509{
3510 QCBOREncode_AddTURI(pMe, QCBOR_ENCODE_AS_TAG, URI);
3511}
3512
3513static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003514QCBOREncode_AddURIToMap(QCBOREncodeContext *pMe,
3515 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003516 const UsefulBufC URI)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003517{
3518 QCBOREncode_AddTURIToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, URI);
3519}
3520
3521static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003522QCBOREncode_AddURIToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003523 const int64_t nLabel,
3524 const UsefulBufC URI)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003525{
3526 QCBOREncode_AddTURIToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01003527}
3528
3529
3530
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003531static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003532QCBOREncode_AddTB64Text(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003533 const uint8_t uTagRequirement,
3534 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003535{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003536 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3537 QCBOREncode_AddTag(pMe, CBOR_TAG_B64);
3538 }
3539 QCBOREncode_AddText(pMe, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003540}
3541
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003542static inline void
3543QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pMe,
3544 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003545 const uint8_t uTagRequirement,
3546 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003547{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003548 QCBOREncode_AddSZString(pMe, szLabel);
3549 QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003550}
3551
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003552static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003553QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003554 const int64_t nLabel,
3555 const uint8_t uTagRequirement,
3556 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003557{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003558 QCBOREncode_AddInt64(pMe, nLabel);
3559 QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text);
3560}
3561
3562static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003563QCBOREncode_AddB64Text(QCBOREncodeContext *pMe, const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003564{
3565 QCBOREncode_AddTB64Text(pMe, QCBOR_ENCODE_AS_TAG, B64Text);
3566}
3567
3568static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003569QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pMe,
3570 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003571 const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003572{
3573 QCBOREncode_AddTB64TextToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, B64Text);
3574}
3575
3576static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003577QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003578 const int64_t nLabel,
3579 const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003580{
3581 QCBOREncode_AddTB64TextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003582}
3583
3584
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003585
3586static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003587QCBOREncode_AddTB64URLText(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003588 const uint8_t uTagRequirement,
3589 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003590{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003591 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3592 QCBOREncode_AddTag(pMe, CBOR_TAG_B64URL);
3593 }
3594 QCBOREncode_AddText(pMe, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003595}
3596
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003597static inline void
3598QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pMe,
3599 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003600 const uint8_t uTagRequirement,
3601 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003602{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003603 QCBOREncode_AddSZString(pMe, szLabel);
3604 QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003605}
3606
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003607static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003608QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003609 const int64_t nLabel,
3610 const uint8_t uTagRequirement,
3611 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003612{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003613 QCBOREncode_AddInt64(pMe, nLabel);
3614 QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text);
3615}
3616
3617static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003618QCBOREncode_AddB64URLText(QCBOREncodeContext *pMe, const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003619{
3620 QCBOREncode_AddTB64URLText(pMe, QCBOR_ENCODE_AS_TAG, B64Text);
3621}
3622
3623static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003624QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pMe,
3625 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003626 const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003627{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003628 QCBOREncode_AddTB64URLTextToMapSZ(pMe,
3629 szLabel,
3630 QCBOR_ENCODE_AS_TAG,
3631 B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003632}
3633
3634static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003635QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003636 const int64_t nLabel,
3637 const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003638{
3639 QCBOREncode_AddTB64URLTextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003640}
3641
3642
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003643
3644static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003645QCBOREncode_AddTRegex(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003646 const uint8_t uTagRequirement,
3647 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003648{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003649 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3650 QCBOREncode_AddTag(pMe, CBOR_TAG_REGEX);
3651 }
3652 QCBOREncode_AddText(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003653}
3654
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003655static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003656QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pMe,
3657 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003658 const uint8_t uTagRequirement,
3659 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003660{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003661 QCBOREncode_AddSZString(pMe, szLabel);
3662 QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003663}
3664
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003665static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003666QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003667 const int64_t nLabel,
3668 const uint8_t uTagRequirement,
3669 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003670{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003671 QCBOREncode_AddInt64(pMe, nLabel);
3672 QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes);
3673}
3674
3675static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003676QCBOREncode_AddRegex(QCBOREncodeContext *pMe, const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003677{
3678 QCBOREncode_AddTRegex(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
3679}
3680
3681static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003682QCBOREncode_AddRegexToMap(QCBOREncodeContext *pMe,
3683 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003684 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003685{
3686 QCBOREncode_AddTRegexToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
3687}
3688
3689static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003690QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003691 const int64_t nLabel,
3692 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003693{
3694 QCBOREncode_AddTRegexToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
3695
Michael Eckel5c531332020-03-02 01:35:30 +01003696}
3697
3698
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003699static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003700QCBOREncode_AddTMIMEData(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003701 const uint8_t uTagRequirement,
3702 const UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01003703{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003704 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
Laurence Lundblade4982f412020-09-18 23:02:18 -07003705 QCBOREncode_AddTag(pMe, CBOR_TAG_BINARY_MIME);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003706 }
Laurence Lundblade4982f412020-09-18 23:02:18 -07003707 QCBOREncode_AddBytes(pMe, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01003708}
3709
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003710static inline void
3711QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pMe,
3712 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003713 const uint8_t uTagRequirement,
3714 const UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01003715{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003716 QCBOREncode_AddSZString(pMe, szLabel);
3717 QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01003718}
3719
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003720static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003721QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003722 const int64_t nLabel,
3723 const uint8_t uTagRequirement,
3724 const UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01003725{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003726 QCBOREncode_AddInt64(pMe, nLabel);
3727 QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData);
3728}
3729
3730static inline void
3731QCBOREncode_AddMIMEData(QCBOREncodeContext *pMe, UsefulBufC MIMEData)
3732{
3733 QCBOREncode_AddTMIMEData(pMe, QCBOR_ENCODE_AS_TAG, MIMEData);
3734}
3735
3736static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003737QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pMe,
3738 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003739 const UsefulBufC MIMEData)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003740{
3741 QCBOREncode_AddTMIMEDataToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, MIMEData);
3742}
3743
3744static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003745QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003746 const int64_t nLabel,
3747 const UsefulBufC MIMEData)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003748{
3749 QCBOREncode_AddTMIMEDataToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01003750}
3751
3752
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003753static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003754QCBOREncode_AddTDateString(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003755 const uint8_t uTagRequirement,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003756 const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01003757{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003758 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3759 QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_STRING);
3760 }
3761 QCBOREncode_AddSZString(pMe, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01003762}
3763
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003764static inline void
3765QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pMe,
3766 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003767 const uint8_t uTagRequirement,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003768 const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01003769{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003770 QCBOREncode_AddSZString(pMe, szLabel);
3771 QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01003772}
3773
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003774static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003775QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003776 const int64_t nLabel,
3777 const uint8_t uTagRequirement,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003778 const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01003779{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003780 QCBOREncode_AddInt64(pMe, nLabel);
3781 QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate);
3782}
3783
3784static inline void
3785QCBOREncode_AddDateString(QCBOREncodeContext *pMe, const char *szDate)
3786{
3787 QCBOREncode_AddTDateString(pMe, QCBOR_ENCODE_AS_TAG, szDate);
3788}
3789
3790static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003791QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pMe,
3792 const char *szLabel,
3793 const char *szDate)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003794{
3795 QCBOREncode_AddTDateStringToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, szDate);
3796}
3797
3798static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003799QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003800 const int64_t nLabel,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003801 const char *szDate)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003802{
3803 QCBOREncode_AddTDateStringToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01003804}
3805
3806
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003807static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003808QCBOREncode_AddTDaysString(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003809 const uint8_t uTagRequirement,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003810 const char *szDate)
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003811{
3812 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3813 QCBOREncode_AddTag(pMe, CBOR_TAG_DAYS_STRING);
3814 }
3815 QCBOREncode_AddSZString(pMe, szDate);
3816}
3817
3818static inline void
3819QCBOREncode_AddTDaysStringToMapSZ(QCBOREncodeContext *pMe,
3820 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003821 const uint8_t uTagRequirement,
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003822 const char *szDate)
3823{
3824 QCBOREncode_AddSZString(pMe, szLabel);
3825 QCBOREncode_AddTDaysString(pMe, uTagRequirement, szDate);
3826}
3827
3828static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003829QCBOREncode_AddTDaysStringToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003830 const int64_t nLabel,
3831 const uint8_t uTagRequirement,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003832 const char *szDate)
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003833{
3834 QCBOREncode_AddInt64(pMe, nLabel);
3835 QCBOREncode_AddTDaysString(pMe, uTagRequirement, szDate);
3836}
3837
3838
3839
3840static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003841QCBOREncode_Private_AddSimple(QCBOREncodeContext *pMe, const uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01003842{
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003843#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3844 if(pMe->uMode >= QCBOR_ENCODE_MODE_DCBOR) {
3845 if(uNum < CBOR_SIMPLEV_FALSE ||
3846 uNum > CBOR_SIMPLEV_NULL) {
3847 pMe->uError = QCBOR_ERR_NOT_PREFERRED;
3848 return;
3849 }
3850 }
3851#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
3852
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003853 QCBOREncode_Private_AddType7(pMe, 0, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01003854}
3855
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003856static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003857QCBOREncode_Private_AddSimpleToMap(QCBOREncodeContext *pMe,
3858 const char *szLabel,
3859 const uint8_t uSimple)
Michael Eckel5c531332020-03-02 01:35:30 +01003860{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003861 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003862 QCBOREncode_Private_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01003863}
3864
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003865static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003866QCBOREncode_Private_AddSimpleToMapN(QCBOREncodeContext *pMe,
3867 const int64_t nLabel,
3868 const uint8_t uSimple)
Michael Eckel5c531332020-03-02 01:35:30 +01003869{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003870 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003871 QCBOREncode_Private_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01003872}
3873
3874
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003875static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003876QCBOREncode_AddBool(QCBOREncodeContext *pMe, const bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01003877{
3878 uint8_t uSimple = CBOR_SIMPLEV_FALSE;
3879 if(b) {
3880 uSimple = CBOR_SIMPLEV_TRUE;
3881 }
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003882 QCBOREncode_Private_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01003883}
3884
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003885static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003886QCBOREncode_AddBoolToMap(QCBOREncodeContext *pMe, const char *szLabel, const bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01003887{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003888 QCBOREncode_AddSZString(pMe, szLabel);
3889 QCBOREncode_AddBool(pMe, b);
Michael Eckel5c531332020-03-02 01:35:30 +01003890}
3891
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003892static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003893QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pMe, const int64_t nLabel, const bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01003894{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003895 QCBOREncode_AddInt64(pMe, nLabel);
3896 QCBOREncode_AddBool(pMe, b);
Michael Eckel5c531332020-03-02 01:35:30 +01003897}
3898
3899
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003900static inline void
3901QCBOREncode_AddNULL(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003902{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003903 QCBOREncode_Private_AddSimple(pMe, CBOR_SIMPLEV_NULL);
Michael Eckel5c531332020-03-02 01:35:30 +01003904}
3905
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003906static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003907QCBOREncode_AddNULLToMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003908{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003909 QCBOREncode_AddSZString(pMe, szLabel);
3910 QCBOREncode_AddNULL(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003911}
3912
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003913static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003914QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pMe, const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003915{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003916 QCBOREncode_AddInt64(pMe, nLabel);
3917 QCBOREncode_AddNULL(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003918}
3919
3920
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003921static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003922QCBOREncode_AddUndef(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003923{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003924 QCBOREncode_Private_AddSimple(pMe, CBOR_SIMPLEV_UNDEF);
Michael Eckel5c531332020-03-02 01:35:30 +01003925}
3926
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003927static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003928QCBOREncode_AddUndefToMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003929{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003930 QCBOREncode_AddSZString(pMe, szLabel);
3931 QCBOREncode_AddUndef(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003932}
3933
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003934static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003935QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pMe, const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003936{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003937 QCBOREncode_AddInt64(pMe, nLabel);
3938 QCBOREncode_AddUndef(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003939}
3940
3941
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003942static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003943QCBOREncode_OpenArray(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003944{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003945 QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY);
Michael Eckel5c531332020-03-02 01:35:30 +01003946}
3947
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003948static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003949QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003950{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003951 QCBOREncode_AddSZString(pMe, szLabel);
3952 QCBOREncode_OpenArray(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003953}
3954
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003955static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003956QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pMe, const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003957{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003958 QCBOREncode_AddInt64(pMe, nLabel);
3959 QCBOREncode_OpenArray(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003960}
3961
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003962
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003963static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003964QCBOREncode_CloseArray(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003965{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003966 QCBOREncode_Private_CloseMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY);
Michael Eckel5c531332020-03-02 01:35:30 +01003967}
3968
3969
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003970static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003971QCBOREncode_OpenMap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003972{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003973 QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_MAP);
Michael Eckel5c531332020-03-02 01:35:30 +01003974}
3975
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003976static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003977QCBOREncode_OpenMapInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003978{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003979 QCBOREncode_AddSZString(pMe, szLabel);
3980 QCBOREncode_OpenMap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003981}
3982
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003983static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003984QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pMe, const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003985{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003986 QCBOREncode_AddInt64(pMe, nLabel);
3987 QCBOREncode_OpenMap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003988}
3989
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003990static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003991QCBOREncode_CloseMap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003992{
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003993 (pMe->pfnCloseMap)(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01003994}
3995
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003996static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07003997QCBOREncode_OpenArrayIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01003998{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003999 QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01004000}
4001
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004002static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004003QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pMe,
4004 const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004005{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004006 QCBOREncode_AddSZString(pMe, szLabel);
4007 QCBOREncode_OpenArrayIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004008}
4009
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004010static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004011QCBOREncode_OpenArrayIndefiniteLengthInMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004012 const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004013{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004014 QCBOREncode_AddInt64(pMe, nLabel);
4015 QCBOREncode_OpenArrayIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004016}
4017
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004018static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004019QCBOREncode_CloseArrayIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004020{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004021 QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01004022}
4023
4024
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004025static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004026QCBOREncode_OpenMapIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004027{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004028 QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01004029}
4030
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004031static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004032QCBOREncode_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pMe,
4033 const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004034{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004035 QCBOREncode_AddSZString(pMe, szLabel);
4036 QCBOREncode_OpenMapIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004037}
4038
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004039static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004040QCBOREncode_OpenMapIndefiniteLengthInMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004041 const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004042{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004043 QCBOREncode_AddInt64(pMe, nLabel);
4044 QCBOREncode_OpenMapIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004045}
4046
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004047static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004048QCBOREncode_CloseMapIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004049{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004050 QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01004051}
4052
4053
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004054static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004055QCBOREncode_BstrWrap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004056{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004057 QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_BYTE_STRING);
Michael Eckel5c531332020-03-02 01:35:30 +01004058}
4059
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004060static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004061QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004062{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004063 QCBOREncode_AddSZString(pMe, szLabel);
4064 QCBOREncode_BstrWrap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004065}
4066
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004067static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004068QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pMe, const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004069{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004070 QCBOREncode_AddInt64(pMe, nLabel);
4071 QCBOREncode_BstrWrap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004072}
4073
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004074static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004075QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pMe, UsefulBufC *pWrappedCBOR)
Michael Eckel5c531332020-03-02 01:35:30 +01004076{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004077 QCBOREncode_CloseBstrWrap2(pMe, true, pWrappedCBOR);
Michael Eckel5c531332020-03-02 01:35:30 +01004078}
4079
4080
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004081static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004082QCBOREncode_AddEncoded(QCBOREncodeContext *pMe, const UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01004083{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004084 QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_NONE_TYPE_RAW, Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01004085}
4086
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004087static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004088QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pMe,
4089 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004090 const UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01004091{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004092 QCBOREncode_AddSZString(pMe, szLabel);
4093 QCBOREncode_AddEncoded(pMe, Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01004094}
4095
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004096static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004097QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004098 const int64_t nLabel,
4099 const UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01004100{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004101 QCBOREncode_AddInt64(pMe, nLabel);
4102 QCBOREncode_AddEncoded(pMe, Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01004103}
4104
4105
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004106static inline int
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004107QCBOREncode_IsBufferNULL(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004108{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004109 return UsefulOutBuf_IsBufferNULL(&(pMe->OutBuf));
Michael Eckel5c531332020-03-02 01:35:30 +01004110}
4111
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004112static inline QCBORError
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004113QCBOREncode_GetErrorState(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004114{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004115 if(UsefulOutBuf_GetError(&(pMe->OutBuf))) {
Michael Eckel5c531332020-03-02 01:35:30 +01004116 // Items didn't fit in the buffer.
4117 // This check catches this condition for all the appends and inserts
4118 // so checks aren't needed when the appends and inserts are performed.
4119 // And of course UsefulBuf will never overrun the input buffer given
4120 // to it. No complex analysis of the error handling in this file is
4121 // needed to know that is true. Just read the UsefulBuf code.
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004122 pMe->uError = QCBOR_ERR_BUFFER_TOO_SMALL;
Michael Eckel5c531332020-03-02 01:35:30 +01004123 // QCBOR_ERR_BUFFER_TOO_SMALL masks other errors, but that is
4124 // OK. Once the caller fixes this, they'll be unmasked.
4125 }
4126
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004127 return (QCBORError)pMe->uError;
Michael Eckel5c531332020-03-02 01:35:30 +01004128}
4129
4130
Laurence Lundblade45d5e482020-09-15 21:15:15 -07004131/* ========================================================================
4132 END OF PRIVATE INLINE IMPLEMENTATION
4133 ======================================================================== */
Michael Eckel5c531332020-03-02 01:35:30 +01004134
4135#ifdef __cplusplus
4136}
4137#endif
4138
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08004139#endif /* qcbor_encode_h */