blob: c72f4d9f8bb09dc17495d7a36022d277faf000b2 [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 *
Laurence Lundblade475c2722024-05-08 11:17:47 -0700344 * If USEFULBUF_DISABLE_ALL_FLOAT is defined, then floating point
Laurence Lundblade3eead482023-12-16 20:53:22 -0700345 * 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 *
Laurence Lundblade475c2722024-05-08 11:17:47 -0700352 * Summary limitations:
Laurence Lundblade3eead482023-12-16 20:53:22 -0700353 * - The entire encoded CBOR must fit into contiguous memory.
Laurence Lundblade475c2722024-05-08 11:17:47 -0700354 * - Max size of encoded CBOR data is a few bytes less than
355 * @c UINT32_MAX (4GB).
356 * - Max array / map nesting level when encoding or decoding is
Laurence Lundblade3eead482023-12-16 20:53:22 -0700357 * @ref QCBOR_MAX_ARRAY_NESTING (this is typically 15).
Laurence Lundblade475c2722024-05-08 11:17:47 -0700358 * - Max items in an array or map when encoding or decoding is
Laurence Lundblade3eead482023-12-16 20:53:22 -0700359 * @ref QCBOR_MAX_ITEMS_IN_ARRAY (typically 65,536).
360 * - Does not directly support labels in maps other than text strings & integers.
Laurence Lundblade475c2722024-05-08 11:17:47 -0700361 * - Does not directly support integer labels beyond whats fits in @c int64_t
362 * or @c uint64_t.
Laurence Lundblade3eead482023-12-16 20:53:22 -0700363 * - Epoch dates limited to @c INT64_MAX (+/- 292 billion years).
Laurence Lundblade475c2722024-05-08 11:17:47 -0700364 * - Exponents for bigfloats and decimal integers are limited to whats fits in
365 * @c int64_t.
Laurence Lundblade3eead482023-12-16 20:53:22 -0700366 * - Tags on labels are ignored during decoding.
367 * - The maximum tag nesting is @c QCBOR_MAX_TAGS_PER_ITEM (typically 4).
Laurence Lundblade475c2722024-05-08 11:17:47 -0700368 * - Works only on 32- and 64-bit CPUs.
Laurence Lundblade31fddb72024-05-13 13:03:35 -0700369 * - QCBORDecode_EnterBstrWrapped() doesn't work on indefinite-length strings.
Laurence Lundblade3eead482023-12-16 20:53:22 -0700370 *
371 * The public interface uses @c size_t for all lengths. Internally the
372 * implementation uses 32-bit lengths by design to use less memory and
373 * fit structures on the stack. This limits the encoded CBOR it can
Laurence Lundblade475c2722024-05-08 11:17:47 -0700374 * work with to size @c UINT32_MAX (4GB).
Laurence Lundblade3eead482023-12-16 20:53:22 -0700375 *
Laurence Lundblade475c2722024-05-08 11:17:47 -0700376 * This implementation requires two's compliment integers. While
377 * C doesn't require two's compliment, <stdint.h> does. Other
378 * parts of this implementation may also require two's compliment.
Michael Eckel5c531332020-03-02 01:35:30 +0100379 */
380
381
Laurence Lundblade825164e2020-10-22 20:18:06 -0700382/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700383 * The size of the buffer to be passed to QCBOREncode_EncodeHead(). It
384 * is one byte larger than sizeof(uint64_t) + 1, the actual maximum
385 * size of the head of a CBOR data item because
386 * QCBOREncode_EncodeHead() needs one extra byte to work.
Michael Eckel5c531332020-03-02 01:35:30 +0100387 */
388#define QCBOR_HEAD_BUFFER_SIZE (sizeof(uint64_t) + 2)
389
Michael Eckel5c531332020-03-02 01:35:30 +0100390
Laurence Lundblade9b334962020-08-27 10:55:53 -0700391/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700392 * Output the full CBOR tag. See @ref CBORTags, @ref Tag-Usage and
393 * @ref Tags-Overview.
Laurence Lundblade9b334962020-08-27 10:55:53 -0700394 */
395#define QCBOR_ENCODE_AS_TAG 0
396
397/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700398 * Output only the 'borrowed' content format for the relevant tag.
399 * See @ref CBORTags, @ref Tag-Usage and @ref Tags-Overview.
Laurence Lundblade9b334962020-08-27 10:55:53 -0700400 */
401#define QCBOR_ENCODE_AS_BORROWED 1
402
Michael Eckel5c531332020-03-02 01:35:30 +0100403
404/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700405 * QCBOREncodeContext is the data type that holds context for all the
406 * encoding functions. It is less than 200 bytes, so it can go on the
407 * stack. The contents are opaque, and the caller should not access
408 * internal members. A context may be re used serially as long as it is
409 * re initialized.
Michael Eckel5c531332020-03-02 01:35:30 +0100410 */
411typedef struct _QCBOREncodeContext QCBOREncodeContext;
412
413
414/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700415 * Initialize the encoder to prepare to encode some CBOR.
416 *
417 * @param[in,out] pCtx The encoder context to initialize.
418 * @param[in] Storage The buffer into which the encoded result
419 * will be written.
420 *
421 * Call this once at the start of an encoding of some CBOR. Then call
422 * the many functions like QCBOREncode_AddInt64() and
423 * QCBOREncode_AddText() to add the different data items. Finally,
424 * call QCBOREncode_Finish() to get the pointer and length of the
425 * encoded result.
426 *
427 * The primary purpose of this function is to give the pointer and
428 * length of the output buffer into which the encoded CBOR will be
429 * written. This is done with a @ref UsefulBuf structure, which is
430 * just a pointer and length (it is equivalent to two parameters, one
431 * a pointer and one a length, but a little prettier).
432 *
433 * The output buffer can be allocated any way (malloc, stack,
434 * static). It is just some memory that QCBOR writes to. The length
435 * must be the length of the allocated buffer. QCBOR will never write
436 * past that length, but might write up to that length. If the buffer
437 * is too small, encoding will go into an error state and not write
438 * anything further.
439 *
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800440 * If allocating on the stack, the convenience macro
Laurence Lundblade3eead482023-12-16 20:53:22 -0700441 * UsefulBuf_MAKE_STACK_UB() can be used, but its use is not required.
442 *
443 * Since there is no reallocation or such, the output buffer must be
444 * correctly sized when passed in here. It is OK, but wasteful if it
445 * is too large. One way to pick the size is to figure out the maximum
446 * size that will ever be needed and hard code a buffer of that size.
447 *
448 * Another way to do it is to have QCBOR calculate it for you. To do
449 * this, pass @ref SizeCalculateUsefulBuf for @c Storage. Then call
450 * all the functions to add the CBOR exactly as if encoding for
451 * real. Finally, call QCBOREncode_FinishGetSize(). Once the length
452 * is obtained, allocate a buffer of that size, call
453 * QCBOREncode_Init() again with the real buffer. Call all the add
454 * functions again and finally, QCBOREncode_Finish() to obtain the
455 * final result. This uses twice the CPU time, but that is usually not
456 * an issue.
457 *
458 * See QCBOREncode_Finish() for how the pointer and length for the
459 * encoded CBOR is returned.
460 *
461 * For practical purposes QCBOR can't output encoded CBOR larger than
462 * @c UINT32_MAX (4GB) even on 64-bit CPUs because the internal
463 * offsets used to track the start of an array/map are 32 bits to
464 * reduce the size of the encoding context.
465 *
466 * A @ref QCBOREncodeContext can be reused over and over as long as
467 * QCBOREncode_Init() is called before each use.
Michael Eckel5c531332020-03-02 01:35:30 +0100468 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700469void
470QCBOREncode_Init(QCBOREncodeContext *pCtx, UsefulBuf Storage);
Michael Eckel5c531332020-03-02 01:35:30 +0100471
472
473/**
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800474 * @brief Select preferred serialization mode.
475 *
476 * @param[in] pCtx The encoding context for mode set.
477 *
478 * Setting this mode will cause QCBOR to return an error if an attempt
479 * is made to use one of the methods that produce non-preferred
480 * serialization. It doesn't change anything else as QCBOR produces
481 * preferred serialization by default.
482 *
483 * The non-preferred methods are: QCBOREncode_AddFloatNoPreferred(),
484 * QCBOREncode_AddDoubleNoPreferred(),
485 * QCBOREncode_OpenArrayIndefiniteLength(),
486 * QCBOREncode_CloseArrayIndefiniteLength(),
487 * QCBOREncode_OpenMapIndefiniteLength(),
488 * QCBOREncode_CloseMapIndefiniteLength(), plus those derived from the
489 * above listed.
490 *
491 * This mode is just a user guard to prevent accidentally calling
492 * something that produces non-preferred serialization. It doesn't do
493 * anything but causes errors to occur on attempts to call the above
494 * listed functions. This does nothing if the library is compiled
495 * QCBOR_DISABLE_ENCODE_USAGE_GUARDS.
496 *
497 * See @ref Serialization. It is usually not necessary to set this
498 * mode, but there is usually no disadvantage to setting it. Preferred
499 * Serialization is defined in RFC 8949, section 4.1.
500 */
501static void
502QCBOREncode_SerializationPreferred(QCBOREncodeContext *pCtx);
503
504
505/**
506 * @brief Select CBOR deterministic encoding mode.
507 *
508 * @param[in] pCtx The encoding context for mode set.
509
510 * This causes QCBOR to produce CBOR Deterministic Encoding (CDE).
511 * With CDE, two distant unrelated CBOR encoders will produce exactly
512 * the same encoded CBOR for a given input.
513 *
514 * In addition to doing everything
515 * QCBOREncode_SerializationPreferred() does (including exclusion of
516 * indefinite lengths), this causes maps to be sorted. The map is
517 * sorted automatically when QCBOREncode_CloseMap() is called.
518 * QCBOREncode_CloseMap() becomes equivalent to
519 * QCBOREncode_CloseAndSortMap().
520 *
521 * Note that linking this function causese about 30% more code from
522 * the QCBOR library to be linked. Also, QCBOREncode_CloseMap() runs
523 * slower, but this is probably only of consequence in very
524 * constrained environments.
525 *
526 * See @ref Serialization. It is usually not necessary to set this
527 * mode as determinism is very rarely needed. However it will
528 * usually work with most protocols. CDE is defined in
529 * draft-ietf-cbor-cde.
530 */
531static void
532QCBOREncode_SerializationCDE(QCBOREncodeContext *pCtx);
533
534
535/**
536 * @brief Select "dCBOR" encoding mode.
537 *
538 * @param[in] pCtx The encoding context for mode set.
539 *
540 * This is a superset of CDE. This function does everything
541 * QCBOREncode_SerializationCDE() does. Also it is a super set of
542 * preferred serialization and does everything
543 * QCBOREncode_SerializationPreferred() does.
544 *
545 * The main feature of dCBOR is that there is only one way to serialize a
546 * particular numeric value. This changes the behavior of functions
547 * that add floating-point numbers. If the floating-point number is
548 * whole, it will be encoded as an integer, not a floating-point number.
549 * 0.000 will be encoded as 0x00. Precision is never lost in this
550 * conversion.
551 *
552 * dCBOR also disallows NaN payloads. QCBOR will allow NaN payloads if
553 * you pass a NaN to one of the floating-point encoding functions.
554 * This mode forces all NaNs to the half-precision queit NaN. Also see
555 * QCBOREncode_Allow().
556 *
557 * dCBOR also disallows 65-bit negative integers.
558 *
559 * dCBOR disallows use of any simple type other than true, false and
560 * NULL. In particular it disallows use of "undef" produced by
561 * QCBOREncode_AddUndef().
562 *
563 * See @ref Serialization. Set this mode only if the protocol you are
564 * implementing requires dCBOR. This mode is usually not compatible
565 * with protocols that don't use dCBOR. dCBOR is defined in
566 * draft-mcnally-deterministic-cbor.
567 */
568static void
569QCBOREncode_SerializationdCBOR(QCBOREncodeContext *pCtx);
570
571
572
573
574/** Bit flag to be passed to QCBOREncode_Allow() to allow NaN payloads
575 * to be output by QCBOREncode_AddDouble(),
576 * QCBOREncode_AddDoubleNoPreferred(), QCBORENcode_AddFloat() and
577 * QCBOREncode_AddSingleleNoPreferred. */
578#define QCBOR_ENCODE_ALLOW_NAN_PAYLOAD 0x01
579
580/** Bit flag to be passed to QCBOREncode_Allow() to allow use of
581 * QCBOREncode_AddNegativeUInt64(). */
582#define QCBOR_ENCODE_ALLOW_65_BIG_NEG 0x02
583
584/** Bit flag to be passed to QCBOREncode_Allow() output of less
585 * interoperable values. See @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD, and
586 * @ref QCBOR_ENCODE_ALLOW_65_BIG_NEG. */
587#define QCBOR_ENCODE_ALLOW_ALL 0xFF
588
589
590/**
591 * @brief Allow encoding of less-interoperable values.
592 *
593 * @param[in] pCtx The encoding context.
594 * @param[in] uAllow Bit flags indicating what to allow.
595 *
596 * There are a few things in the CBOR standard that are often not
597 * supported and are thus not very interoperable. By default QCBOR
598 * will error if you attempt to output them. This disables that
599 * error.
600 *
601 * See @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD and
602 * @ref QCBOR_ENCODE_ALLOW_65_BIG_NEG.
603 *
604 * This does nothing if the library is compiled
605 * QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
606static void
607QCBOREncode_Allow(QCBOREncodeContext *pCtx, uint8_t uAllow);
608
609
610
611/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700612 * @brief Add a signed 64-bit integer to the encoded output.
613 *
614 * @param[in] pCtx The encoding context to add the integer to.
615 * @param[in] nNum The integer to add.
616 *
617 * The integer will be encoded and added to the CBOR output.
618 *
619 * This function figures out the size and the sign and encodes in the
620 * correct minimal CBOR. Specifically, it will select CBOR major type
621 * 0 or 1 based on sign and will encode to 1, 2, 4 or 8 bytes
622 * depending on the value of the integer. Values less than 24
623 * effectively encode to one byte because they are encoded in with the
624 * CBOR major type. This is a neat and efficient characteristic of
625 * CBOR that can be taken advantage of when designing CBOR-based
626 * protocols. If integers like tags can be kept between -23 and 23
627 * they will be encoded in one byte including the major type.
628 *
629 * If you pass a smaller int, say an @c int16_t or a small value, say
630 * 100, the encoding will still be CBOR's most compact that can
631 * represent the value. For example, CBOR always encodes the value 0
632 * as one byte, 0x00. The representation as 0x00 includes
633 * identification of the type as an integer too as the major type for
634 * an integer is 0. See [RFC 8949]
635 * (https://tools.ietf.org/html/rfc8949) Appendix A for more examples
636 * of CBOR encoding. This compact encoding is also preferred
637 * serialization CBOR as per section 34.1 in RFC 8949.
638 *
639 * There are no functions to add @c int16_t or @c int32_t because they
640 * are not necessary because this always encodes to the smallest
641 * number of bytes based on the value (If this code is running on a
642 * 32-bit machine having a way to add 32-bit integers would reduce
643 * code size some).
644 *
645 * If the encoding context is in an error state, this will do
646 * nothing. If an error occurs when adding this integer, the internal
647 * error flag will be set, and the error will be returned when
648 * QCBOREncode_Finish() is called.
649 *
650 * See also QCBOREncode_AddUInt64().
Michael Eckel5c531332020-03-02 01:35:30 +0100651 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700652void
653QCBOREncode_AddInt64(QCBOREncodeContext *pCtx, int64_t nNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100654
Laurence Lundblade3eead482023-12-16 20:53:22 -0700655static void
656QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t uNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100657
Laurence Lundblade3eead482023-12-16 20:53:22 -0700658static void
659QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100660
661
662/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700663 * @brief Add an unsigned 64-bit integer to the encoded output.
664 *
665 * @param[in] pCtx The encoding context to add the integer to.
666 * @param[in] uNum The integer to add.
667 *
668 * The integer will be encoded and added to the CBOR output.
669 *
670 * The only reason so use this function is for integers larger than
671 * @c INT64_MAX and smaller than @c UINT64_MAX. Otherwise
672 * QCBOREncode_AddInt64() will work fine.
673 *
674 * Error handling is the same as for QCBOREncode_AddInt64().
Michael Eckel5c531332020-03-02 01:35:30 +0100675 */
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700676static void
Laurence Lundblade3eead482023-12-16 20:53:22 -0700677QCBOREncode_AddUInt64(QCBOREncodeContext *pCtx, uint64_t uNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100678
Laurence Lundblade3eead482023-12-16 20:53:22 -0700679static void
680QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100681
Laurence Lundblade3eead482023-12-16 20:53:22 -0700682static void
683QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100684
685
686/**
Laurence Lundblade2d493002024-02-01 11:09:17 -0700687 * @brief Add a negative 64-bit integer to encoded output
688 *
689 * @param[in] pCtx The encoding context to add the integer to.
690 * @param[in] uNum The integer to add.
691 *
692 * QCBOREncode_AddInt64() is much better to encode negative integers
693 * than this. What this can do is add integers with one more
694 * significant bit than an int64_t (a "65-bit" integer if you count
695 * the sign as a bit) which is possible because CBOR happens to
696 * support such integers.
697 *
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800698 * Because use of this is discouraged. It must be explicitly allowed
699 * by passing @ref QCBOR_ENCODE_ALLOW_65_BIG_NEG to a call to
700 * QCBOREncode_Allow().
701 *
Laurence Lundblade2d493002024-02-01 11:09:17 -0700702 * The actual value encoded is -uNum - 1. That is, give 0 for uNum to
703 * transmit -1, give 1 to transmit -2 and give UINT64_MAX to transmit
704 * -UINT64_MAX-1 (18446744073709551616). The interface is odd like
705 * this so all negative values CBOR can represent can be encoded by
706 * QCBOR (making this a complete CBOR implementation).
707 *
708 * The most negative value QCBOREncode_AddInt64() can encode is
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800709 * -9223372036854775808 which is -2^63 or negative 0x800000000000.
710 * This can encode from -9223372036854775809 to -18446744073709551616
711 * or -2^63 - 1 to -2^64. Note that it is not possible to represent
712 * positive or negative 18446744073709551616 in any standard C data
713 * type.
Laurence Lundblade2d493002024-02-01 11:09:17 -0700714 *
715 * Negative integers are normally decoded in QCBOR with type
716 * @ref QCBOR_TYPE_INT64. Integers in the range of -9223372036854775809
717 * to -18446744073709551616 are returned as @ref QCBOR_TYPE_65BIT_NEG_INT.
718 *
719 * WARNING: some CBOR decoders will be unable to decode -2^63 - 1 to
720 * -2^64. Also, most CPUs do not have registers that can represent
721 * this range. If you need 65-bit negative integers, you likely need
722 * negative 66, 67 and 68-bit negative integers so it is likely better
723 * to use CBOR big numbers where you can have any number of bits. See
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800724 * QCBOREncode_AddTNegativeBignum() and @ref Serialization.
Laurence Lundblade2d493002024-02-01 11:09:17 -0700725 */
726void
727QCBOREncode_AddNegativeUInt64(QCBOREncodeContext *pCtx, uint64_t uNum);
728
729static void
730QCBOREncode_AddNegativeUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum);
731
732static void
733QCBOREncode_AddNegativeUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum);
734
735/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700736 * @brief Add a UTF-8 text string to the encoded output.
737 *
738 * @param[in] pCtx The encoding context to add the text to.
739 * @param[in] Text Pointer and length of text to add.
740 *
741 * The text passed in must be unencoded UTF-8 according to [RFC 3629]
742 * (https://tools.ietf.org/html/rfc3629). There is no NULL
743 * termination. The text is added as CBOR major type 3.
744 *
745 * If called with @c nBytesLen equal to 0, an empty string will be
746 * added. When @c nBytesLen is 0, @c pBytes may be @c NULL.
747 *
748 * Note that the restriction of the buffer length to a @c uint32_t is
749 * entirely intentional as this encoder is not capable of encoding
750 * lengths greater. This limit to 4GB for a text string should not be
751 * a problem.
752 *
753 * Text lines in Internet protocols (on the wire) are delimited by
754 * either a CRLF or just an LF. Officially many protocols specify
755 * CRLF, but implementations often work with either. CBOR type 3 text
756 * can be either line ending, even a mixture of both.
757 *
758 * Operating systems usually have a line end convention. Windows uses
759 * CRLF. Linux and MacOS use LF. Some applications on a given OS may
760 * work with either and some may not.
761 *
762 * The majority of use cases and CBOR protocols using type 3 text will
763 * work with either line ending. However, some use cases or protocols
764 * may not work with either in which case translation to and/or from
765 * the local line end convention, typically that of the OS, is
766 * necessary.
767 *
768 * QCBOR does no line ending translation for type 3 text when encoding
769 * and decoding.
770 *
771 * Error handling is the same as QCBOREncode_AddInt64().
Michael Eckel5c531332020-03-02 01:35:30 +0100772 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700773static void
774QCBOREncode_AddText(QCBOREncodeContext *pCtx, UsefulBufC Text);
Michael Eckel5c531332020-03-02 01:35:30 +0100775
Laurence Lundblade3eead482023-12-16 20:53:22 -0700776static void
777QCBOREncode_AddTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Text);
Michael Eckel5c531332020-03-02 01:35:30 +0100778
Laurence Lundblade3eead482023-12-16 20:53:22 -0700779static void
780QCBOREncode_AddTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Text);
Michael Eckel5c531332020-03-02 01:35:30 +0100781
782
783/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700784 * @brief Add a UTF-8 text string to the encoded output.
785 *
786 * @param[in] pCtx The encoding context to add the text to.
787 * @param[in] szString Null-terminated text to add.
788 *
789 * This works the same as QCBOREncode_AddText().
Michael Eckel5c531332020-03-02 01:35:30 +0100790 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700791static void
792QCBOREncode_AddSZString(QCBOREncodeContext *pCtx, const char *szString);
Michael Eckel5c531332020-03-02 01:35:30 +0100793
Laurence Lundblade3eead482023-12-16 20:53:22 -0700794static void
795QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szString);
Michael Eckel5c531332020-03-02 01:35:30 +0100796
Laurence Lundblade3eead482023-12-16 20:53:22 -0700797static void
798QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szString);
Michael Eckel5c531332020-03-02 01:35:30 +0100799
800
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200801#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Michael Eckel5c531332020-03-02 01:35:30 +0100802/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700803 * @brief Add a double-precision floating-point number to the encoded output.
804 *
805 * @param[in] pCtx The encoding context to add the double to.
806 * @param[in] dNum The double-precision number to add.
807 *
808 * This encodes and outputs a floating-point number. CBOR major type 7
809 * is used.
810 *
811 * This implements preferred serialization, selectively encoding the
812 * double-precision floating-point number as either double-precision,
813 * single-precision or half-precision. Infinity, NaN and 0 are always
814 * encoded as half-precision. If no precision will be lost in the
815 * conversion to half-precision, then it will be converted and
816 * encoded. If not and no precision will be lost in conversion to
817 * single-precision, then it will be converted and encoded. If not,
818 * then no conversion is performed, and it encoded as a
819 * double-precision.
820 *
821 * Half-precision floating-point numbers take up 2 bytes, half that of
822 * single-precision, one quarter of double-precision
823 *
824 * This automatically reduces the size of encoded CBOR, maybe even by
825 * four if most of values are 0, infinity or NaN.
826 *
827 * When decoded, QCBOR will usually return these values as
828 * double-precision.
829 *
830 * It is possible to disable this preferred serialization when compiling
831 * QCBOR. In that case, this functions the same as
832 * QCBOREncode_AddDoubleNoPreferred().
833 *
834 * Error handling is the same as QCBOREncode_AddInt64().
835 *
836 * See also QCBOREncode_AddDoubleNoPreferred(), QCBOREncode_AddFloat()
837 * and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -0800838 *
839 * By default, this will error out on an attempt to encode a NaN with
840 * a payload. See QCBOREncode_Allow() and @ref QCBOR_ENCODE_ALLOW_NAN_PAYLOAD.
Michael Eckel5c531332020-03-02 01:35:30 +0100841 */
Laurence Lundblade8d9e0cd2024-05-25 18:12:19 -0700842static void
Laurence Lundblade3eead482023-12-16 20:53:22 -0700843QCBOREncode_AddDouble(QCBOREncodeContext *pCtx, double dNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100844
Laurence Lundblade3eead482023-12-16 20:53:22 -0700845static void
846QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100847
Laurence Lundblade3eead482023-12-16 20:53:22 -0700848static void
849QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum);
Michael Eckel5c531332020-03-02 01:35:30 +0100850
851
852/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700853 * @brief Add a single-precision floating-point number to the encoded output.
854 *
855 * @param[in] pCtx The encoding context to add the double to.
856 * @param[in] fNum The single-precision number to add.
857 *
858 * This is identical to QCBOREncode_AddDouble() except the input is
859 * single-precision.
860 *
861 * See also QCBOREncode_AddDouble(), QCBOREncode_AddDoubleNoPreferred(),
862 * and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
863 */
Laurence Lundblade8d9e0cd2024-05-25 18:12:19 -0700864static void
Laurence Lundblade3eead482023-12-16 20:53:22 -0700865QCBOREncode_AddFloat(QCBOREncodeContext *pCtx, float fNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700866
Laurence Lundblade3eead482023-12-16 20:53:22 -0700867static void
868QCBOREncode_AddFloatToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700869
Laurence Lundblade3eead482023-12-16 20:53:22 -0700870static void
871QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float dNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700872
873
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700874/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700875 * @brief Add a double-precision floating-point number without preferred encoding.
876 *
877 * @param[in] pCtx The encoding context to add the double to.
878 * @param[in] dNum The double-precision number to add.
879 *
Laurence Lundblade70fc1252024-05-31 10:57:28 -0700880 * Output a double-precision float straight-through with no checking or
881 * processing for preferred serializtion, dCBOR or other.
Laurence Lundblade3eead482023-12-16 20:53:22 -0700882 *
883 * Error handling is the same as QCBOREncode_AddInt64().
884 *
885 * See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
886 * QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
887 */
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700888static void
Laurence Lundblade3eead482023-12-16 20:53:22 -0700889QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pCtx, double dNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700890
Laurence Lundblade3eead482023-12-16 20:53:22 -0700891static void
892QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700893
Laurence Lundblade3eead482023-12-16 20:53:22 -0700894static void
895QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700896
897
898/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700899 * @brief Add a single-precision floating-point number without preferred encoding.
900 *
901 * @param[in] pCtx The encoding context to add the double to.
902 * @param[in] fNum The single-precision number to add.
903 *
Laurence Lundblade70fc1252024-05-31 10:57:28 -0700904 * Output a single-precision float straight-through with no checking or
905 * processing for preferred serializtion, dCBOR or other.
Laurence Lundblade3eead482023-12-16 20:53:22 -0700906 *
907 * Error handling is the same as QCBOREncode_AddInt64().
908 *
909 * See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
910 * QCBOREncode_AddDoubleNoPreferred() and @ref Floating-Point.
911 */
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700912static void
Laurence Lundblade3eead482023-12-16 20:53:22 -0700913QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pCtx, float fNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700914
Laurence Lundblade3eead482023-12-16 20:53:22 -0700915static void
916QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700917
Laurence Lundblade3eead482023-12-16 20:53:22 -0700918static void
919QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float fNum);
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +0200920#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700921
922
Michael Eckel5c531332020-03-02 01:35:30 +0100923/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700924 * @brief Add an optional tag.
925 *
926 * @param[in] pCtx The encoding context to add the tag to.
927 * @param[in] uTag The tag to add
928 *
929 * This outputs a CBOR major type 6 item that tags the next data item
930 * that is output usually to indicate it is some new data type.
931 *
932 * For many of the common standard tags, a function to encode data
933 * using it is provided and this is not needed. For example,
934 * QCBOREncode_AddDateEpoch() already exists to output integers
935 * representing dates with the right tag.
936 *
937 * The tag is applied to the next data item added to the encoded
938 * output. That data item that is to be tagged can be of any major
939 * CBOR type. Any number of tags can be added to a data item by
940 * calling this multiple times before the data item is added.
941 *
942 * See @ref Tags-Overview for discussion of creating new non-standard
943 * tags. See QCBORDecode_GetNext() for discussion of decoding custom
944 * tags.
Michael Eckel5c531332020-03-02 01:35:30 +0100945 */
Laurence Lundbladecbd7d132024-05-19 11:11:22 -0700946static void
Laurence Lundblade3eead482023-12-16 20:53:22 -0700947QCBOREncode_AddTag(QCBOREncodeContext *pCtx, uint64_t uTag);
Laurence Lundblade46d63e92021-05-13 11:37:10 -0700948
949
Michael Eckel5c531332020-03-02 01:35:30 +0100950/**
Laurence Lundblade3eead482023-12-16 20:53:22 -0700951 * @brief Add an epoch-based date.
952 *
953 * @param[in] pCtx The encoding context to add the date to.
954 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
955 * @ref QCBOR_ENCODE_AS_BORROWED.
956 * @param[in] nDate Number of seconds since 1970-01-01T00:00Z
957 * in UTC time.
958 *
959 * As per RFC 8949 this is similar to UNIX/Linux/POSIX dates. This is
960 * the most compact way to specify a date and time in CBOR. Note that
961 * this is always UTC and does not include the time zone. Use
962 * QCBOREncode_AddDateString() if you want to include the time zone.
963 *
964 * The preferred integer serialization rules apply here so the date will be
965 * encoded in a minimal number of bytes. Until about the year 2106
966 * these dates will encode in 6 bytes -- one byte for the tag, one
967 * byte for the type and 4 bytes for the integer. After that it will
968 * encode to 10 bytes.
969 *
970 * Negative values are supported for dates before 1970.
971 *
972 * If you care about leap-seconds and that level of accuracy, make sure
973 * the system you are running this code on does it correctly. This code
974 * just takes the value passed in.
975 *
976 * This implementation cannot encode fractional seconds using float or
977 * double even though that is allowed by CBOR, but you can encode them
978 * if you want to by calling QCBOREncode_AddTag() and QCBOREncode_AddDouble().
979 *
980 * Error handling is the same as QCBOREncode_AddInt64().
981 *
982 * See also QCBOREncode_AddTDaysEpoch().
Michael Eckel5c531332020-03-02 01:35:30 +0100983 */
Laurence Lundblade3eead482023-12-16 20:53:22 -0700984static void
985QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pCtx,
986 uint8_t uTagRequirement,
987 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100988
Laurence Lundblade3eead482023-12-16 20:53:22 -0700989static void
990QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pCtx,
991 const char *szLabel,
992 uint8_t uTagRequirement,
993 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100994
Laurence Lundblade3eead482023-12-16 20:53:22 -0700995static void
996QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pCtx,
997 int64_t nLabel,
998 uint8_t uTagRequirement,
999 int64_t nDate);
1000
1001
1002static void
1003QCBOREncode_AddDateEpoch(QCBOREncodeContext *pCtx,
1004 int64_t nDate);
1005
1006static void
1007QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pCtx,
1008 const char *szLabel,
1009 int64_t nDate);
1010
1011static void
1012QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pCtx,
1013 int64_t nLabel,
1014 int64_t nDate);
1015
Michael Eckel5c531332020-03-02 01:35:30 +01001016
1017
Michael Eckel5c531332020-03-02 01:35:30 +01001018/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001019 * @brief Add an epoch-based day-count date.
1020 *
1021 * @param[in] pCtx The encoding context to add the date to.
1022 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1023 * @ref QCBOR_ENCODE_AS_BORROWED.
1024 * @param[in] nDays Number of days before or after 1970-01-0.
1025 *
1026 * This date format is described in
1027 * [RFC 8943] (https://tools.ietf.org/html/rfc8943).
1028 *
1029 * The preferred integer serialization rules apply here so the date
1030 * will be encoded in a minimal number of bytes. Until about the year
1031 * 2149 these dates will encode in 4 bytes -- one byte for the tag,
1032 * one byte for the type and 2 bytes for the integer.
1033 *
1034 * See also QCBOREncode_AddTDateEpoch().
1035 */
1036static void
1037QCBOREncode_AddTDaysEpoch(QCBOREncodeContext *pCtx,
1038 uint8_t uTagRequirement,
1039 int64_t nDays);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001040
Laurence Lundblade3eead482023-12-16 20:53:22 -07001041static void
1042QCBOREncode_AddTDaysEpochToMapSZ(QCBOREncodeContext *pCtx,
1043 const char *szLabel,
1044 uint8_t uTagRequirement,
1045 int64_t nDays);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001046
Laurence Lundblade3eead482023-12-16 20:53:22 -07001047static void
1048QCBOREncode_AddTDaysEpochToMapN(QCBOREncodeContext *pCtx,
1049 int64_t nLabel,
1050 uint8_t uTagRequirement,
1051 int64_t nDays);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001052
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001053
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001054
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001055
Laurence Lundblade3eead482023-12-16 20:53:22 -07001056/**
1057 * @brief Add a byte string to the encoded output.
1058 *
1059 * @param[in] pCtx The encoding context to add the bytes to.
1060 * @param[in] Bytes Pointer and length of the input data.
1061 *
1062 * Simply adds the bytes to the encoded output as CBOR major type 2.
1063 *
1064 * If called with @c Bytes.len equal to 0, an empty string will be
1065 * added. When @c Bytes.len is 0, @c Bytes.ptr may be @c NULL.
1066 *
1067 * Error handling is the same as QCBOREncode_AddInt64().
1068 */
1069static void
1070QCBOREncode_AddBytes(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001071
Laurence Lundblade3eead482023-12-16 20:53:22 -07001072static void
1073QCBOREncode_AddBytesToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
1074
1075static void
1076QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
1077
1078
1079/**
1080 * @brief Set up to write a byte string value directly to encoded output.
1081 *
1082 * @param[in] pCtx The encoding context to add the bytes to.
1083 * @param[out] pPlace Pointer and length of place to write byte string value.
1084 *
1085 * QCBOREncode_AddBytes() is the normal way to encode a byte string.
1086 * This is for special cases and by passes some of the pointer safety.
1087 *
1088 * The purpose of this is to output the bytes that make up a byte
1089 * string value directly to the QCBOR output buffer so you don't need
1090 * to have a copy of it in memory. This is particularly useful if the
1091 * byte string is large, for example, the encrypted payload of a
1092 * COSE_Encrypt message. The payload encryption algorithm can output
1093 * directly to the encoded CBOR buffer, perhaps by making it the
1094 * output buffer for some function (e.g. symmetric encryption) or by
1095 * multiple writes.
1096 *
1097 * The pointer in @c pPlace is where to start writing. Writing is just
1098 * copying bytes to the location by the pointer in \c pPlace. Writing
1099 * past the length in @c pPlace will be writing off the end of the
1100 * output buffer.
1101 *
1102 * If there is no room in the output buffer @ref NULLUsefulBuf will be
1103 * returned and there is no need to call QCBOREncode_CloseBytes().
1104 *
1105 * The byte string must be closed by calling QCBOREncode_CloseBytes().
1106 *
1107 * Warning: this bypasses some of the usual checks provided by QCBOR
1108 * against writing off the end of the encoded output buffer.
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001109 */
1110void
1111QCBOREncode_OpenBytes(QCBOREncodeContext *pCtx, UsefulBuf *pPlace);
1112
1113static void
Laurence Lundblade3eead482023-12-16 20:53:22 -07001114QCBOREncode_OpenBytesInMapSZ(QCBOREncodeContext *pCtx,
1115 const char *szLabel,
1116 UsefulBuf *pPlace);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001117
1118static void
Laurence Lundblade3eead482023-12-16 20:53:22 -07001119QCBOREncode_OpenBytesInMapN(QCBOREncodeContext *pCtx,
1120 int64_t nLabel,
1121 UsefulBuf *pPlace);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001122
1123
1124/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001125 * @brief Close out a byte string written directly to encoded output.
1126 *
1127 * @param[in] pCtx The encoding context to add the bytes to.
1128 * @param[out] uAmount The number of bytes written, the length of the
1129 * byte string.
1130 *
1131 * This closes out a call to QCBOREncode_OpenBytes(). This inserts a
1132 * CBOR header at the front of the byte string value to make it a
1133 * well-formed byte string.
1134 *
1135 * If there was no call to QCBOREncode_OpenBytes() then @ref
1136 * QCBOR_ERR_TOO_MANY_CLOSES is set.
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001137 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001138void
1139QCBOREncode_CloseBytes(QCBOREncodeContext *pCtx, size_t uAmount);
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06001140
1141
1142/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001143 * @brief Add a binary UUID to the encoded output.
1144 *
1145 * @param[in] pCtx The encoding context to add the UUID to.
1146 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1147 * @ref QCBOR_ENCODE_AS_BORROWED.
1148 * @param[in] Bytes Pointer and length of the binary UUID.
1149 *
1150 * A binary UUID as defined in [RFC 4122]
1151 * (https://tools.ietf.org/html/rfc4122) is added to the output.
1152 *
1153 * It is output as CBOR major type 2, a binary string, with tag @ref
1154 * CBOR_TAG_BIN_UUID indicating the binary string is a UUID.
Michael Eckel5c531332020-03-02 01:35:30 +01001155 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001156static void
1157QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pCtx,
1158 uint8_t uTagRequirement,
1159 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001160
Laurence Lundblade3eead482023-12-16 20:53:22 -07001161static void
1162QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pCtx,
1163 const char *szLabel,
1164 uint8_t uTagRequirement,
1165 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001166
Laurence Lundblade3eead482023-12-16 20:53:22 -07001167static void
1168QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pCtx,
1169 int64_t nLabel,
1170 uint8_t uTagRequirement,
1171 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001172
1173
Laurence Lundblade3eead482023-12-16 20:53:22 -07001174static void
1175QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001176
Laurence Lundblade3eead482023-12-16 20:53:22 -07001177static void
1178QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001179
Laurence Lundblade3eead482023-12-16 20:53:22 -07001180static void
1181QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001182
1183
1184/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001185 * @brief Add a positive big number to the encoded output.
1186 *
1187 * @param[in] pCtx The encoding context to add the big number to.
1188 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1189 * @ref QCBOR_ENCODE_AS_BORROWED.
1190 * @param[in] Bytes Pointer and length of the big number.
1191 *
1192 * Big numbers are integers larger than 64-bits. Their format is
1193 * described in [RFC 8949] (https://tools.ietf.org/html/rfc8949).
1194 *
1195 * It is output as CBOR major type 2, a binary string, with tag
1196 * @ref CBOR_TAG_POS_BIGNUM indicating the binary string is a positive
1197 * big number.
1198 *
1199 * Often big numbers are used to represent cryptographic keys,
1200 * however, COSE which defines representations for keys chose not to
1201 * use this particular type.
Michael Eckel5c531332020-03-02 01:35:30 +01001202 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001203static void
1204QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pCtx,
1205 uint8_t uTagRequirement,
1206 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001207
Laurence Lundblade3eead482023-12-16 20:53:22 -07001208static void
1209QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pCtx,
1210 const char *szLabel,
1211 uint8_t uTagRequirement,
1212 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001213
Laurence Lundblade3eead482023-12-16 20:53:22 -07001214static void
1215QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pCtx,
1216 int64_t nLabel,
1217 uint8_t uTagRequirement,
1218 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001219
1220
Laurence Lundblade3eead482023-12-16 20:53:22 -07001221static void
1222QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx,
1223 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001224
Laurence Lundblade3eead482023-12-16 20:53:22 -07001225static void
1226QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx,
1227 const char *szLabel,
1228 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001229
Laurence Lundblade3eead482023-12-16 20:53:22 -07001230static void
1231QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx,
1232 int64_t nLabel,
1233 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001234
1235
1236/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001237 * @brief Add a negative big number to the encoded output.
1238 *
1239 * @param[in] pCtx The encoding context to add the big number to.
1240 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1241 * @ref QCBOR_ENCODE_AS_BORROWED.
1242 * @param[in] Bytes Pointer and length of the big number.
1243 *
1244 * Big numbers are integers larger than 64-bits. Their format is
1245 * described in [RFC 8949] (https://tools.ietf.org/html/rfc8949).
1246 *
1247 * It is output as CBOR major type 2, a binary string, with tag
1248 * @ref CBOR_TAG_NEG_BIGNUM indicating the binary string is a negative
1249 * big number.
1250 *
1251 * Often big numbers are used to represent cryptographic keys,
1252 * however, COSE which defines representations for keys chose not to
1253 * use this particular type.
Michael Eckel5c531332020-03-02 01:35:30 +01001254 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001255static void
1256QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pCtx,
1257 uint8_t uTagRequirement,
1258 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001259
Laurence Lundblade3eead482023-12-16 20:53:22 -07001260static void
1261QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pCtx,
1262 const char *szLabel,
1263 uint8_t uTagRequirement,
1264 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001265
Laurence Lundblade3eead482023-12-16 20:53:22 -07001266static void
1267QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pCtx,
1268 int64_t nLabel,
1269 uint8_t uTagRequirement,
1270 UsefulBufC Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001271
1272
Laurence Lundblade3eead482023-12-16 20:53:22 -07001273static void
1274QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx,
1275 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001276
Laurence Lundblade3eead482023-12-16 20:53:22 -07001277static void
1278QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx,
1279 const char *szLabel,
1280 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001281
Laurence Lundblade3eead482023-12-16 20:53:22 -07001282static void
1283QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx,
1284 int64_t nLabel,
1285 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01001286
1287
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07001288#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Michael Eckel5c531332020-03-02 01:35:30 +01001289/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001290 * @brief Add a decimal fraction to the encoded output.
1291 *
1292 * @param[in] pCtx Encoding context to add the decimal fraction to.
1293 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1294 * @ref QCBOR_ENCODE_AS_BORROWED.
1295 * @param[in] nMantissa The mantissa.
1296 * @param[in] nBase10Exponent The exponent.
1297 *
1298 * The value is nMantissa * 10 ^ nBase10Exponent.
1299 *
1300 * A decimal fraction is good for exact representation of some values
1301 * that can't be represented exactly with standard C (IEEE 754)
1302 * floating-point numbers. Much larger and much smaller numbers can
1303 * also be represented than floating-point because of the larger
1304 * number of bits in the exponent.
1305 *
1306 * The decimal fraction is conveyed as two integers, a mantissa and a
1307 * base-10 scaling factor.
1308 *
1309 * For example, 273.15 is represented by the two integers 27315 and -2.
1310 *
1311 * The exponent and mantissa have the range from @c INT64_MIN to
1312 * @c INT64_MAX for both encoding and decoding (CBOR allows
1313 * @c -UINT64_MAX to @c UINT64_MAX, but this implementation doesn't
1314 * support this range to reduce code size and interface complexity a
1315 * little).
1316 *
1317 * CBOR Preferred serialization of the integers is used, thus they
1318 * will be encoded in the smallest number of bytes possible.
1319 *
1320 * See also QCBOREncode_AddDecimalFractionBigNum() for a decimal
1321 * fraction with arbitrarily large precision and
1322 * QCBOREncode_AddBigFloat().
1323 *
1324 * There is no representation of positive or negative infinity or NaN
1325 * (Not a Number). Use QCBOREncode_AddDouble() to encode them.
1326 *
1327 * See @ref expAndMantissa for decoded representation.
Michael Eckel5c531332020-03-02 01:35:30 +01001328 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001329static void
1330QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pCtx,
1331 uint8_t uTagRequirement,
1332 int64_t nMantissa,
1333 int64_t nBase10Exponent);
1334
1335static void
1336QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pCtx,
1337 const char *szLabel,
1338 uint8_t uTagRequirement,
1339 int64_t nMantissa,
1340 int64_t nBase10Exponent);
1341
1342static void
1343QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pCtx,
1344 int64_t nLabel,
1345 uint8_t uTagRequirement,
1346 int64_t nMantissa,
1347 int64_t nBase10Exponent);
1348
1349
1350static void
1351QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pCtx,
1352 int64_t nMantissa,
1353 int64_t nBase10Exponent);
1354
1355static void
1356QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx,
1357 const char *szLabel,
1358 int64_t nMantissa,
1359 int64_t nBase10Exponent);
1360
1361static void
1362QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pCtx,
1363 int64_t nLabel,
1364 int64_t nMantissa,
1365 int64_t nBase10Exponent);
1366
1367
1368/**
1369 * @brief Add a decimal fraction with a big number mantissa to the encoded output.
1370 *
1371 * @param[in] pCtx Encoding context to add the decimal fraction to.
1372 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1373 * @ref QCBOR_ENCODE_AS_BORROWED.
1374 * @param[in] Mantissa The mantissa.
1375 * @param[in] bIsNegative false if mantissa is positive, true if negative.
1376 * @param[in] nBase10Exponent The exponent.
1377 *
1378 * This is the same as QCBOREncode_AddDecimalFraction() except the
1379 * mantissa is a big number (See QCBOREncode_AddPositiveBignum())
1380 * allowing for arbitrarily large precision.
1381 *
1382 * See @ref expAndMantissa for decoded representation.
1383 */
1384static void
1385QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pCtx,
1386 uint8_t uTagRequirement,
1387 UsefulBufC Mantissa,
1388 bool bIsNegative,
1389 int64_t nBase10Exponent);
1390
1391static void
1392QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
1393 const char *szLabel,
1394 uint8_t uTagRequirement,
1395 UsefulBufC Mantissa,
1396 bool bIsNegative,
1397 int64_t nBase10Exponent);
1398
1399static void
1400QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
1401 int64_t nLabel,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001402 uint8_t uTagRequirement,
Laurence Lundblade3eead482023-12-16 20:53:22 -07001403 UsefulBufC Mantissa,
1404 bool bIsNegative,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001405 int64_t nBase10Exponent);
1406
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001407
Laurence Lundblade3eead482023-12-16 20:53:22 -07001408static void
1409QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pCtx,
1410 UsefulBufC Mantissa,
1411 bool bIsNegative,
1412 int64_t nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001413
Laurence Lundblade3eead482023-12-16 20:53:22 -07001414static void
1415QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07001416 const char *szLabel,
Laurence Lundblade3eead482023-12-16 20:53:22 -07001417 UsefulBufC Mantissa,
1418 bool bIsNegative,
1419 int64_t nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001420
Laurence Lundblade3eead482023-12-16 20:53:22 -07001421static void
1422QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001423 int64_t nLabel,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001424 UsefulBufC Mantissa,
1425 bool bIsNegative,
Laurence Lundblade3eead482023-12-16 20:53:22 -07001426 int64_t nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001427
Laurence Lundblade3eead482023-12-16 20:53:22 -07001428/**
1429 * @brief Add a big floating-point number to the encoded output.
1430 *
1431 * @param[in] pCtx The encoding context to add the bigfloat to.
1432 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1433 * @ref QCBOR_ENCODE_AS_BORROWED.
1434 * @param[in] nMantissa The mantissa.
1435 * @param[in] nBase2Exponent The exponent.
1436 *
1437 * The value is nMantissa * 2 ^ nBase2Exponent.
1438 *
1439 * "Bigfloats", as CBOR terms them, are similar to IEEE floating-point
1440 * numbers in having a mantissa and base-2 exponent, but they are not
1441 * supported by hardware or encoded the same. They explicitly use two
1442 * CBOR-encoded integers to convey the mantissa and exponent, each of
1443 * which can be 8, 16, 32 or 64 bits. With both the mantissa and
1444 * exponent 64 bits they can express more precision and a larger range
1445 * than an IEEE double floating-point number. See
1446 * QCBOREncode_AddBigFloatBigNum() for even more precision.
1447 *
1448 * For example, 1.5 would be represented by a mantissa of 3 and an
1449 * exponent of -1.
1450 *
1451 * The exponent and mantissa have the range from @c INT64_MIN to
1452 * @c INT64_MAX for both encoding and decoding (CBOR allows @c
1453 * -UINT64_MAX to @c UINT64_MAX, but this implementation doesn't
1454 * support this range to reduce code size and interface complexity a
1455 * little).
1456 *
1457 * CBOR preferred serialization of the integers is used, thus they will
1458 * be encoded in the smallest number of bytes possible.
1459 *
1460 * This can also be used to represent floating-point numbers in
1461 * environments that don't support IEEE 754.
1462 *
1463 * See @ref expAndMantissa for decoded representation.
1464 */
1465static void
1466QCBOREncode_AddTBigFloat(QCBOREncodeContext *pCtx,
1467 uint8_t uTagRequirement,
1468 int64_t nMantissa,
1469 int64_t nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001470
Laurence Lundblade3eead482023-12-16 20:53:22 -07001471static void
1472QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pCtx,
1473 const char *szLabel,
1474 uint8_t uTagRequirement,
1475 int64_t nMantissa,
1476 int64_t nBase2Exponent);
1477
1478static void
1479QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pCtx,
1480 int64_t nLabel,
1481 uint8_t uTagRequirement,
1482 int64_t nMantissa,
1483 int64_t nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001484
1485
Laurence Lundblade3eead482023-12-16 20:53:22 -07001486static void
1487QCBOREncode_AddBigFloat(QCBOREncodeContext *pCtx,
1488 int64_t nMantissa,
1489 int64_t nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01001490
Laurence Lundblade3eead482023-12-16 20:53:22 -07001491static void
1492QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pCtx,
1493 const char *szLabel,
1494 int64_t nMantissa,
1495 int64_t nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01001496
Laurence Lundblade3eead482023-12-16 20:53:22 -07001497static void
1498QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pCtx,
1499 int64_t nLabel,
1500 int64_t nMantissa,
1501 int64_t nBase2Exponent);
1502
1503/**
1504 * @brief Add a big floating-point number with a big number mantissa to
1505 * the encoded output.
1506 *
1507 * @param[in] pCtx The encoding context to add the bigfloat to.
1508 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1509 * @ref QCBOR_ENCODE_AS_BORROWED.
1510 * @param[in] Mantissa The mantissa.
1511 * @param[in] bIsNegative false if mantissa is positive, true if negative.
1512 * @param[in] nBase2Exponent The exponent.
1513 *
1514 * This is the same as QCBOREncode_AddBigFloat() except the mantissa
1515 * is a big number (See QCBOREncode_AddPositiveBignum()) allowing for
1516 * arbitrary precision.
1517 *
1518 * See @ref expAndMantissa for decoded representation.
1519 */
1520static void
1521QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pCtx,
1522 uint8_t uTagRequirement,
1523 UsefulBufC Mantissa,
1524 bool bIsNegative,
1525 int64_t nBase2Exponent);
1526
1527static void
1528QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pCtx,
1529 const char *szLabel,
1530 uint8_t uTagRequirement,
1531 UsefulBufC Mantissa,
1532 bool bIsNegative,
1533 int64_t nBase2Exponent);
1534
1535static void
1536QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
1537 int64_t nLabel,
1538 uint8_t uTagRequirement,
1539 UsefulBufC Mantissa,
1540 bool bIsNegative,
1541 int64_t nBase2Exponent);
1542
1543
1544static void
1545QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pCtx,
1546 UsefulBufC Mantissa,
1547 bool bIsNegative,
1548 int64_t nBase2Exponent);
1549
1550static void
1551QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pCtx,
1552 const char *szLabel,
1553 UsefulBufC Mantissa,
1554 bool bIsNegative,
1555 int64_t nBase2Exponent);
1556
1557static void
1558QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
1559 int64_t nLabel,
1560 UsefulBufC Mantissa,
1561 bool bIsNegative,
1562 int64_t nBase2Exponent);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07001563#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Michael Eckel5c531332020-03-02 01:35:30 +01001564
1565
1566/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001567 * @brief Add a text URI to the encoded output.
1568 *
1569 * @param[in] pCtx The encoding context to add the URI to.
1570 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1571 * @ref QCBOR_ENCODE_AS_BORROWED.
1572 * @param[in] URI Pointer and length of the URI.
1573 *
1574 * The format of URI must be per [RFC 3986]
1575 * (https://tools.ietf.org/html/rfc3986).
1576 *
1577 * It is output as CBOR major type 3, a text string, with tag @ref
1578 * CBOR_TAG_URI indicating the text string is a URI.
1579 *
1580 * A URI in a NULL-terminated string, @c szURI, can be easily added with
1581 * this code:
1582 *
1583 * QCBOREncode_AddURI(pCtx, UsefulBuf_FromSZ(szURI));
Michael Eckel5c531332020-03-02 01:35:30 +01001584 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001585static void
1586QCBOREncode_AddTURI(QCBOREncodeContext *pCtx,
1587 uint8_t uTagRequirement,
1588 UsefulBufC URI);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001589
Laurence Lundblade3eead482023-12-16 20:53:22 -07001590static void
1591QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pCtx,
1592 const char *szLabel,
1593 uint8_t uTagRequirement,
1594 UsefulBufC URI);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001595
Laurence Lundblade3eead482023-12-16 20:53:22 -07001596static void
1597QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pCtx,
1598 int64_t nLabel,
1599 uint8_t uTagRequirement,
1600 UsefulBufC URI);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001601
1602
Laurence Lundblade3eead482023-12-16 20:53:22 -07001603static void
1604QCBOREncode_AddURI(QCBOREncodeContext *pCtx,
1605 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001606
Laurence Lundblade3eead482023-12-16 20:53:22 -07001607static void
1608QCBOREncode_AddURIToMap(QCBOREncodeContext *pCtx,
1609 const char *szLabel,
1610 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001611
Laurence Lundblade3eead482023-12-16 20:53:22 -07001612static void
1613QCBOREncode_AddURIToMapN(QCBOREncodeContext *pCtx,
1614 int64_t nLabel,
1615 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001616
1617
1618/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001619 * @brief Add Base64-encoded text to encoded output.
1620 *
1621 * @param[in] pCtx The encoding context to add the base-64 text to.
1622 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1623 * @ref QCBOR_ENCODE_AS_BORROWED.
1624 * @param[in] B64Text Pointer and length of the base-64 encoded text.
1625 *
1626 * The text content is Base64 encoded data per [RFC 4648]
1627 * (https://tools.ietf.org/html/rfc4648).
1628 *
1629 * It is output as CBOR major type 3, a text string, with tag @ref
1630 * CBOR_TAG_B64 indicating the text string is Base64 encoded.
Michael Eckel5c531332020-03-02 01:35:30 +01001631 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001632static void
1633QCBOREncode_AddTB64Text(QCBOREncodeContext *pCtx,
1634 uint8_t uTagRequirement,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001635 UsefulBufC B64Text);
1636
Laurence Lundblade3eead482023-12-16 20:53:22 -07001637static void
1638QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pCtx,
1639 const char *szLabel,
1640 uint8_t uTagRequirement,
1641 UsefulBufC B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001642
Laurence Lundblade3eead482023-12-16 20:53:22 -07001643static void
1644QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pCtx,
1645 int64_t nLabel,
1646 uint8_t uTagRequirement,
1647 UsefulBufC B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001648
1649
Laurence Lundblade3eead482023-12-16 20:53:22 -07001650static void
1651QCBOREncode_AddB64Text(QCBOREncodeContext *pCtx,
1652 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001653
Laurence Lundblade3eead482023-12-16 20:53:22 -07001654static void
1655QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pCtx,
1656 const char *szLabel,
1657 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001658
Laurence Lundblade3eead482023-12-16 20:53:22 -07001659static void
1660QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pCtx,
1661 int64_t nLabel,
1662 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001663
1664
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001665
Michael Eckel5c531332020-03-02 01:35:30 +01001666/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001667 * @brief Add base64url encoded data to encoded output.
1668 *
1669 * @param[in] pCtx The encoding context to add the base64url to.
1670 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1671 * @ref QCBOR_ENCODE_AS_BORROWED.
1672 * @param[in] B64Text Pointer and length of the base64url encoded text.
1673 *
1674 * The text content is base64URL encoded text as per [RFC 4648]
1675 * (https://tools.ietf.org/html/rfc4648).
1676 *
1677 * It is output as CBOR major type 3, a text string, with tag
1678 * @ref CBOR_TAG_B64URL indicating the text string is a Base64url
1679 * encoded.
Michael Eckel5c531332020-03-02 01:35:30 +01001680 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001681static void
1682QCBOREncode_AddTB64URLText(QCBOREncodeContext *pCtx,
1683 uint8_t uTagRequirement,
1684 UsefulBufC B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001685
Laurence Lundblade3eead482023-12-16 20:53:22 -07001686static void
1687QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pCtx,
1688 const char *szLabel,
1689 uint8_t uTagRequirement,
1690 UsefulBufC B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001691
Laurence Lundblade3eead482023-12-16 20:53:22 -07001692static void
1693QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pCtx,
1694 int64_t nLabel,
1695 uint8_t uTagRequirement,
1696 UsefulBufC B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001697
1698
Laurence Lundblade3eead482023-12-16 20:53:22 -07001699static void
1700QCBOREncode_AddB64URLText(QCBOREncodeContext *pCtx,
1701 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001702
Laurence Lundblade3eead482023-12-16 20:53:22 -07001703static void
1704QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pCtx,
1705 const char *szLabel,
1706 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001707
Laurence Lundblade3eead482023-12-16 20:53:22 -07001708static void
1709QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pCtx,
1710 int64_t nLabel,
1711 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001712
1713
1714/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001715 * @brief Add Perl Compatible Regular Expression.
1716 *
1717 * @param[in] pCtx Encoding context to add the regular expression to.
1718 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1719 * @ref QCBOR_ENCODE_AS_BORROWED.
1720 * @param[in] Regex Pointer and length of the regular expression.
1721 *
1722 * The text content is Perl Compatible Regular
1723 * Expressions (PCRE) / JavaScript syntax [ECMA262].
1724 *
1725 * It is output as CBOR major type 3, a text string, with tag @ref
1726 * CBOR_TAG_REGEX indicating the text string is a regular expression.
Michael Eckel5c531332020-03-02 01:35:30 +01001727 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001728static void
1729QCBOREncode_AddTRegex(QCBOREncodeContext *pCtx,
1730 uint8_t uTagRequirement,
1731 UsefulBufC Regex);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001732
Laurence Lundblade3eead482023-12-16 20:53:22 -07001733static void
1734QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pCtx,
1735 const char *szLabel,
1736 uint8_t uTagRequirement,
1737 UsefulBufC Regex);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001738
Laurence Lundblade3eead482023-12-16 20:53:22 -07001739static void
1740QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pCtx,
1741 int64_t nLabel,
1742 uint8_t uTagRequirement,
1743 UsefulBufC Regex);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001744
1745
Laurence Lundblade3eead482023-12-16 20:53:22 -07001746static void
1747QCBOREncode_AddRegex(QCBOREncodeContext *pCtx,
1748 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001749
Laurence Lundblade3eead482023-12-16 20:53:22 -07001750static void
1751QCBOREncode_AddRegexToMap(QCBOREncodeContext *pCtx,
1752 const char *szLabel,
1753 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001754
Laurence Lundblade3eead482023-12-16 20:53:22 -07001755static void
1756QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pCtx,
1757 int64_t nLabel,
1758 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001759
1760
1761/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001762 * @brief MIME encoded data to the encoded output.
1763 *
1764 * @param[in] pCtx The encoding context to add the MIME data to.
1765 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1766 * @ref QCBOR_ENCODE_AS_BORROWED.
1767 * @param[in] MIMEData Pointer and length of the MIME data.
1768 *
1769 * The text content is in MIME format per [RFC 2045]
1770 * (https://tools.ietf.org/html/rfc2045) including the headers.
1771 *
1772 * It is output as CBOR major type 2, a binary string, with tag
1773 * @ref CBOR_TAG_BINARY_MIME indicating the string is MIME data. This
1774 * outputs tag 257, not tag 36, as it can carry any type of MIME
1775 * binary, 7-bit, 8-bit, quoted-printable and base64 where tag 36
1776 * cannot.
1777 *
1778 * Previous versions of QCBOR, those before spiffy decode, output tag
1779 * 36. Decoding supports both tag 36 and 257. (if the old behavior
1780 * with tag 36 is needed, copy the inline functions below and change
1781 * the tag number).
1782 *
1783 * See also QCBORDecode_GetMIMEMessage() and
1784 * @ref QCBOR_TYPE_BINARY_MIME.
1785 *
1786 * This does no translation of line endings. See QCBOREncode_AddText()
1787 * for a discussion of line endings in CBOR.
Michael Eckel5c531332020-03-02 01:35:30 +01001788 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001789static void
1790QCBOREncode_AddTMIMEData(QCBOREncodeContext *pCtx,
1791 uint8_t uTagRequirement,
1792 UsefulBufC MIMEData);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001793
Laurence Lundblade3eead482023-12-16 20:53:22 -07001794static void
1795QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pCtx,
1796 const char *szLabel,
1797 uint8_t uTagRequirement,
1798 UsefulBufC MIMEData);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001799
Laurence Lundblade3eead482023-12-16 20:53:22 -07001800static void
1801QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pCtx,
1802 int64_t nLabel,
1803 uint8_t uTagRequirement,
1804 UsefulBufC MIMEData);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001805
1806
Laurence Lundblade3eead482023-12-16 20:53:22 -07001807static void
1808QCBOREncode_AddMIMEData(QCBOREncodeContext *pCtx,
1809 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001810
Laurence Lundblade3eead482023-12-16 20:53:22 -07001811static void
1812QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pCtx,
1813 const char *szLabel,
1814 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001815
Laurence Lundblade3eead482023-12-16 20:53:22 -07001816static void
1817QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pCtx,
1818 int64_t nLabel,
1819 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001820
1821
1822/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001823 * @brief Add an RFC 3339 date string
1824 *
1825 * @param[in] pCtx The encoding context to add the date to.
1826 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1827 * @ref QCBOR_ENCODE_AS_BORROWED.
1828 * @param[in] szDate Null-terminated string with date to add.
1829 *
1830 * The string szDate should be in the form of [RFC 3339]
1831 * (https://tools.ietf.org/html/rfc3339) as defined by section 3.3 in
1832 * [RFC 4287] (https://tools.ietf.org/html/rfc4287). This is as
1833 * described in section 3.4.1 in [RFC 8949]
1834 * (https://tools.ietf.org/html/rfc8949).
1835 *
1836 * Note that this function doesn't validate the format of the date
1837 * string at all. If you add an incorrect format date string, the
1838 * generated CBOR will be incorrect and the receiver may not be able
1839 * to handle it.
1840 *
1841 * Error handling is the same as QCBOREncode_AddInt64().
1842 *
1843 * See also QCBOREncode_AddTDayString().
Michael Eckel5c531332020-03-02 01:35:30 +01001844 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001845static void
1846QCBOREncode_AddTDateString(QCBOREncodeContext *pCtx,
1847 uint8_t uTagRequirement,
1848 const char *szDate);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001849
Laurence Lundblade3eead482023-12-16 20:53:22 -07001850static void
1851QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pCtx,
1852 const char *szLabel,
1853 uint8_t uTagRequirement,
1854 const char *szDate);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001855
Laurence Lundblade3eead482023-12-16 20:53:22 -07001856static void
1857QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pCtx,
1858 int64_t nLabel,
1859 uint8_t uTagRequirement,
1860 const char *szDate);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001861
1862
Laurence Lundblade3eead482023-12-16 20:53:22 -07001863static void
1864QCBOREncode_AddDateString(QCBOREncodeContext *pCtx,
1865 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001866
Laurence Lundblade3eead482023-12-16 20:53:22 -07001867static void
1868QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pCtx,
1869 const char *szLabel,
1870 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001871
Laurence Lundblade3eead482023-12-16 20:53:22 -07001872static void
1873QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pCtx,
1874 int64_t nLabel,
1875 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001876
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001877
1878/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001879 * @brief Add a date-only string.
1880 *
1881 * @param[in] pCtx The encoding context to add the date to.
1882 * @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or
1883 * @ref QCBOR_ENCODE_AS_BORROWED.
1884 * @param[in] szDate Null-terminated string with date to add.
1885 *
1886 * This date format is described in
1887 * [RFC 8943] (https://tools.ietf.org/html/rfc8943), but that mainly
1888 * references RFC 3339. The string szDate must be in the forrm
1889 * specified the ABNF for a full-date in
1890 * [RFC 3339] (https://tools.ietf.org/html/rfc3339). Examples of this
1891 * are "1985-04-12" and "1937-01-01". The time and the time zone are
1892 * never included.
1893 *
1894 * Note that this function doesn't validate the format of the date
1895 * string at all. If you add an incorrect format date string, the
1896 * generated CBOR will be incorrect and the receiver may not be able
1897 * to handle it.
1898 *
1899 * Error handling is the same as QCBOREncode_AddInt64().
1900 *
1901 * See also QCBOREncode_AddTDateString().
Laurence Lundblade46d63e92021-05-13 11:37:10 -07001902 */
1903static void
1904QCBOREncode_AddTDaysString(QCBOREncodeContext *pCtx,
1905 uint8_t uTagRequirement,
1906 const char *szDate);
1907
1908static void
1909QCBOREncode_AddTDaysStringToMapSZ(QCBOREncodeContext *pCtx,
1910 const char *szLabel,
1911 uint8_t uTagRequirement,
1912 const char *szDate);
1913
1914static void
1915QCBOREncode_AddTDaysStringToMapN(QCBOREncodeContext *pCtx,
1916 int64_t nLabel,
1917 uint8_t uTagRequirement,
1918 const char *szDate);
1919
1920
Michael Eckel5c531332020-03-02 01:35:30 +01001921/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001922 * @brief Add a standard Boolean.
1923 *
1924 * @param[in] pCtx The encoding context to add the Boolean to.
1925 * @param[in] b true or false from @c <stdbool.h>.
1926 *
1927 * Adds a Boolean value as CBOR major type 7.
1928 *
1929 * Error handling is the same as QCBOREncode_AddInt64().
Michael Eckel5c531332020-03-02 01:35:30 +01001930 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001931static void
1932QCBOREncode_AddBool(QCBOREncodeContext *pCtx, bool b);
Michael Eckel5c531332020-03-02 01:35:30 +01001933
Laurence Lundblade3eead482023-12-16 20:53:22 -07001934static void
1935QCBOREncode_AddBoolToMap(QCBOREncodeContext *pCtx, const char *szLabel, bool b);
Michael Eckel5c531332020-03-02 01:35:30 +01001936
Laurence Lundblade3eead482023-12-16 20:53:22 -07001937static void
1938QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, bool b);
Michael Eckel5c531332020-03-02 01:35:30 +01001939
1940
Michael Eckel5c531332020-03-02 01:35:30 +01001941/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001942 * @brief Add a NULL to the encoded output.
1943 *
1944 * @param[in] pCtx The encoding context to add the NULL to.
1945 *
1946 * Adds the NULL value as CBOR major type 7.
1947 *
1948 * This NULL doesn't have any special meaning in CBOR such as a
1949 * terminating value for a string or an empty value.
1950 *
1951 * Error handling is the same as QCBOREncode_AddInt64().
Michael Eckel5c531332020-03-02 01:35:30 +01001952 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001953static void
1954QCBOREncode_AddNULL(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01001955
Laurence Lundblade3eead482023-12-16 20:53:22 -07001956static void
1957QCBOREncode_AddNULLToMap(QCBOREncodeContext *pCtx, const char *szLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01001958
Laurence Lundblade3eead482023-12-16 20:53:22 -07001959static void
1960QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01001961
1962
1963/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07001964 * @brief Add an "undef" to the encoded output.
1965 *
1966 * @param[in] pCtx The encoding context to add the "undef" to.
1967 *
1968 * Adds the undef value as CBOR major type 7.
1969 *
1970 * Note that this value will not translate to JSON.
1971 *
1972 * This Undef doesn't have any special meaning in CBOR such as a
1973 * terminating value for a string or an empty value.
1974 *
1975 * Error handling is the same as QCBOREncode_AddInt64().
Michael Eckel5c531332020-03-02 01:35:30 +01001976 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07001977static void
1978QCBOREncode_AddUndef(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01001979
Laurence Lundblade3eead482023-12-16 20:53:22 -07001980static void
1981QCBOREncode_AddUndefToMap(QCBOREncodeContext *pCtx, const char *szLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01001982
Laurence Lundblade3eead482023-12-16 20:53:22 -07001983static void
1984QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01001985
1986
1987/**
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07001988 * @brief Add a simple value.
1989 *
1990 * @param[in] pMe The encode context.
1991 * @param[in] uNum The simple value.
1992 *
Laurence Lundblade3888f002024-06-12 21:20:56 -07001993 * QCBOREncode_AddBool(), QCBOREncode_AddUndef() and
1994 * QCBOREncode_AddNull() are preferred to this for the simple values
1995 * defined in RFC 8949, but this can be used for them too.
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07001996 *
Laurence Lundblade3888f002024-06-12 21:20:56 -07001997 * The main purpose of this is to add simple values beyond those in
1998 * defined RFC 8949. Note that simple values must be registered with
1999 * IANA. Those in the range of 0 to 19 must be standardized. Those in
2000 * the range of 32 to 255 do not require a standard, but must be
2001 * publically specified. There is no range of values for proprietary
2002 * use. See
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002003 * https://www.iana.org/assignments/cbor-simple-values/cbor-simple-values.xhtml
2004 */
2005static void
Laurence Lundblade3888f002024-06-12 21:20:56 -07002006QCBOREncode_AddSimple(QCBOREncodeContext *pMe, const uint8_t uNum);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002007
2008static void
2009QCBOREncode_AddSimpleToMap(QCBOREncodeContext *pMe,
2010 const char *szLabel,
2011 const uint8_t uSimple);
2012
2013static void
2014QCBOREncode_AddSimpleToMapN(QCBOREncodeContext *pMe,
2015 const int64_t nLabel,
2016 const uint8_t uSimple);
2017
2018
2019/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002020 * @brief Indicates that the next items added are in an array.
2021 *
2022 * @param[in] pCtx The encoding context to open the array in.
2023 *
2024 * Arrays are the basic CBOR aggregate or structure type. Call this
2025 * function to start or open an array. Then call the various
2026 * @c QCBOREncode_AddXxx() functions to add the items that go into the
2027 * array. Then call QCBOREncode_CloseArray() when all items have been
2028 * added. The data items in the array can be of any type and can be of
2029 * mixed types.
2030 *
2031 * Nesting of arrays and maps is allowed and supported just by calling
2032 * QCBOREncode_OpenArray() again before calling
2033 * QCBOREncode_CloseArray(). While CBOR has no limit on nesting, this
2034 * implementation does in order to keep it smaller and simpler. The
2035 * limit is @ref QCBOR_MAX_ARRAY_NESTING. This is the max number of
2036 * times this can be called without calling
2037 * QCBOREncode_CloseArray(). QCBOREncode_Finish() will return
2038 * @ref QCBOR_ERR_ARRAY_NESTING_TOO_DEEP when it is called as this
2039 * function just sets an error state and returns no value when this
2040 * occurs.
2041 *
2042 * If you try to add more than @ref QCBOR_MAX_ITEMS_IN_ARRAY items to
2043 * a single array or map, @ref QCBOR_ERR_ARRAY_TOO_LONG will be
2044 * returned when QCBOREncode_Finish() is called.
2045 *
2046 * An array itself must have a label if it is being added to a map.
2047 * Note that array elements do not have labels (but map elements do).
2048 *
2049 * An array itself may be tagged by calling QCBOREncode_AddTag()
2050 * before this call.
Michael Eckel5c531332020-03-02 01:35:30 +01002051 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002052static void
2053QCBOREncode_OpenArray(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01002054
Laurence Lundblade3eead482023-12-16 20:53:22 -07002055static void
2056QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pCtx, const char *szLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002057
Laurence Lundblade3eead482023-12-16 20:53:22 -07002058static void
2059QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002060
2061
2062/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002063 * @brief Close an open array.
2064 *
2065 * @param[in] pCtx The encoding context to close the array in.
2066 *
2067 * The closes an array opened by QCBOREncode_OpenArray(). It reduces
2068 * nesting level by one. All arrays (and maps) must be closed before
2069 * calling QCBOREncode_Finish().
2070 *
2071 * When an error occurs as a result of this call, the encoder records
2072 * the error and enters the error state. The error will be returned
2073 * when QCBOREncode_Finish() is called.
2074 *
2075 * If this has been called more times than QCBOREncode_OpenArray(), then
2076 * @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when QCBOREncode_Finish()
2077 * is called.
2078 *
2079 * If this is called and it is not an array that is currently open,
2080 * @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when
2081 * QCBOREncode_Finish() is called.
Michael Eckel5c531332020-03-02 01:35:30 +01002082 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002083static void
2084QCBOREncode_CloseArray(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01002085
2086
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002087
2088
Michael Eckel5c531332020-03-02 01:35:30 +01002089/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002090 * @brief Indicates that the next items added are in a map.
2091 *
2092 * @param[in] pCtx The encoding context to open the map in.
2093 *
2094 * See QCBOREncode_OpenArray() for more information, particularly
2095 * error handling.
2096 *
2097 * CBOR maps are an aggregate type where each item in the map consists
2098 * of a label and a value. They are similar to JSON objects.
2099 *
2100 * The value can be any CBOR type including another map.
2101 *
2102 * The label can also be any CBOR type, but in practice they are
2103 * typically, integers as this gives the most compact output. They
2104 * might also be text strings which gives readability and translation
2105 * to JSON.
2106 *
2107 * Every @c QCBOREncode_AddXxx() call has one version that ends with
2108 * @c InMap for adding items to maps with string labels and one that
2109 * ends with @c InMapN that is for adding with integer labels.
2110 *
2111 * RFC 8949 uses the term "key" instead of "label".
2112 *
2113 * If you wish to use map labels that are neither integer labels nor
2114 * text strings, then just call the QCBOREncode_AddXxx() function
2115 * explicitly to add the label. Then call it again to add the value.
2116 *
2117 * See the [RFC 8949] (https://tools.ietf.org/html/rfc8949) for a lot
2118 * more information on creating maps.
Michael Eckel5c531332020-03-02 01:35:30 +01002119 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002120static void
2121QCBOREncode_OpenMap(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01002122
Laurence Lundblade3eead482023-12-16 20:53:22 -07002123static void
2124QCBOREncode_OpenMapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002125
Laurence Lundblade3eead482023-12-16 20:53:22 -07002126static void
2127QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002128
2129
Michael Eckel5c531332020-03-02 01:35:30 +01002130/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002131 * @brief Close an open map.
2132 *
2133 * @param[in] pCtx The encoding context to close the map in.
2134 *
2135 * This closes a map opened by QCBOREncode_OpenMap(). It reduces
2136 * nesting level by one.
2137 *
2138 * When an error occurs as a result of this call, the encoder records
2139 * the error and enters the error state. The error will be returned
2140 * when QCBOREncode_Finish() is called.
2141 *
2142 * If this has been called more times than QCBOREncode_OpenMap(), then
2143 * @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when
2144 * QCBOREncode_Finish() is called.
2145 *
2146 * If this is called and it is not a map that is currently open,
2147 * @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when
2148 * QCBOREncode_Finish() is called.
Michael Eckel5c531332020-03-02 01:35:30 +01002149 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002150static void
2151QCBOREncode_CloseMap(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01002152
2153
2154/**
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002155 * @brief Indicates that the next items added are in an indefinite length array.
2156 *
2157 * @param[in] pCtx The encoding context to open the array in.
2158 *
2159 * This is the same as QCBOREncode_OpenArray() except the array is
2160 * indefinite length.
2161 *
2162 * This must be closed with QCBOREncode_CloseArrayIndefiniteLength().
2163 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002164static void
2165QCBOREncode_OpenArrayIndefiniteLength(QCBOREncodeContext *pCtx);
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002166
Laurence Lundblade3eead482023-12-16 20:53:22 -07002167static void
2168QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pCtx,
2169 const char *szLabel);
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002170
2171static void
2172QCBOREncode_OpenArrayIndefiniteLengthInMapN(QCBOREncodeContext *pCtx,
2173 int64_t nLabel);
2174
2175
2176/**
2177 * @brief Close an open indefinite length array.
2178 *
2179 * @param[in] pCtx The encoding context to close the array in.
2180 *
2181 * This is the same as QCBOREncode_CloseArray(), but the open array
2182 * that is being close must be of indefinite length.
2183 */
2184static void
2185QCBOREncode_CloseArrayIndefiniteLength(QCBOREncodeContext *pCtx);
2186
2187
2188/**
2189 * @brief Indicates that the next items added are in an indefinite length map.
2190 *
2191 * @param[in] pCtx The encoding context to open the map in.
2192 *
2193 * This is the same as QCBOREncode_OpenMap() except the array is
2194 * indefinite length.
2195 *
2196 * This must be closed with QCBOREncode_CloseMapIndefiniteLength().
2197 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002198static void
2199QCBOREncode_OpenMapIndefiniteLength(QCBOREncodeContext *pCtx);
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002200
Laurence Lundblade3eead482023-12-16 20:53:22 -07002201static void
2202QCBOREncode_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pCtx,
2203 const char *szLabel);
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002204
2205static void
2206QCBOREncode_OpenMapIndefiniteLengthInMapN(QCBOREncodeContext *pCtx,
2207 int64_t nLabel);
2208
2209
2210/**
2211 * @brief Close an open indefinite length map.
2212 *
2213 * @param[in] pCtx The encoding context to close the map in.
2214 *
2215 * This is the same as QCBOREncode_CloseMap(), but the open map that
2216 * is being close must be of indefinite length.
2217 */
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07002218static void
Laurence Lundblade85a18ca2023-11-27 21:25:10 -07002219QCBOREncode_CloseMapIndefiniteLength(QCBOREncodeContext *pCtx);
2220
2221
Laurence Lundbladec92e4162023-11-27 21:51:26 -07002222/**
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08002223 * @brief Close and sort an open map.
Laurence Lundbladed6e13022023-11-26 10:14:02 -07002224 *
2225 * @param[in] pCtx The encoding context to close the map in .
2226 *
2227 * This is the same as QCBOREncode_CloseMap() except it sorts the map
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07002228 * per RFC 8949 Section 4.2.1 and checks for duplicate map keys. This
2229 * sort is lexicographic of the CBOR-encoded map labels.
Laurence Lundbladed6e13022023-11-26 10:14:02 -07002230 *
2231 * This is more expensive than most things in the encoder. It uses
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07002232 * bubble sort which runs in n-squared time where @c n is the number
2233 * of map items. Sorting large maps on slow CPUs might be slow. This
2234 * is also increases the object code size of the encoder by about 30%
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08002235 * (500-1000 bytes).
Laurence Lundbladed6e13022023-11-26 10:14:02 -07002236 *
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07002237 * Bubble sort was selected so as to not need require configuration of
2238 * a buffer to track map item offsets. Bubble sort works well even
2239 * though map items are not all the same size because it always swaps
2240 * adjacent items.
Laurence Lundbladed6e13022023-11-26 10:14:02 -07002241 */
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07002242void
2243QCBOREncode_CloseAndSortMap(QCBOREncodeContext *pCtx);
Laurence Lundbladed6e13022023-11-26 10:14:02 -07002244
Laurence Lundbladedee0d4e2024-03-03 13:46:33 -07002245void
2246QCBOREncode_CloseAndSortMapIndef(QCBOREncodeContext *pCtx);
Laurence Lundbladed6e13022023-11-26 10:14:02 -07002247
2248
2249/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002250 * @brief Indicate start of encoded CBOR to be wrapped in a bstr.
2251 *
2252 * @param[in] pCtx The encoding context to open the bstr-wrapped CBOR in.
2253 *
2254 * All added encoded items between this call and a call to
2255 * QCBOREncode_CloseBstrWrap2() will be wrapped in a bstr. They will
2256 * appear in the final output as a byte string. That byte string will
2257 * contain encoded CBOR. This increases nesting level by one.
2258 *
2259 * The typical use case is for encoded CBOR that is to be
2260 * cryptographically hashed, as part of a [RFC 8152, COSE]
2261 * (https://tools.ietf.org/html/rfc8152) implementation. The wrapping
2262 * byte string is taken as input by the hash function (which is why it
2263 * is returned by QCBOREncode_CloseBstrWrap2()). It is also easy to
2264 * recover on decoding with standard CBOR decoders.
2265 *
2266 * Using QCBOREncode_BstrWrap() and QCBOREncode_CloseBstrWrap2()
2267 * avoids having to encode the items first in one buffer (e.g., the
2268 * COSE payload) and then add that buffer as a bstr to another
2269 * encoding (e.g. the COSE to-be-signed bytes, the @c Sig_structure)
2270 * potentially halving the memory needed.
2271 *
2272 * CBOR by nature must be decoded item by item in order from the
2273 * start. By wrapping some CBOR in a byte string, the decoding of
2274 * that wrapped CBOR can be skipped. This is another use of wrapping,
2275 * perhaps because the CBOR is large and deeply nested. Perhaps APIs
2276 * for handling one defined CBOR message that is being embedded in
2277 * another only take input as a byte string. Perhaps the desire is to
2278 * be able to decode the out layer even in the wrapped has errors.
Michael Eckel5c531332020-03-02 01:35:30 +01002279 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002280static void
2281QCBOREncode_BstrWrap(QCBOREncodeContext *pCtx);
Michael Eckel5c531332020-03-02 01:35:30 +01002282
Laurence Lundblade3eead482023-12-16 20:53:22 -07002283static void
2284QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002285
Laurence Lundblade3eead482023-12-16 20:53:22 -07002286static void
2287QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
Michael Eckel5c531332020-03-02 01:35:30 +01002288
2289
2290/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002291 * @brief Close a wrapping bstr.
2292 *
2293 * @param[in] pCtx The encoding context to close of bstr wrapping in.
2294 * @param[in] bIncludeCBORHead Include the encoded CBOR head of the bstr
2295 * as well as the bytes in @c pWrappedCBOR.
2296 * @param[out] pWrappedCBOR A @ref UsefulBufC containing wrapped bytes.
2297 *
2298 * The closes a wrapping bstr opened by QCBOREncode_BstrWrap(). It reduces
2299 * nesting level by one.
2300 *
2301 * A pointer and length of the enclosed encoded CBOR is returned in @c
2302 * *pWrappedCBOR if it is not @c NULL. The main purpose of this is so
2303 * this data can be hashed (e.g., with SHA-256) as part of a [RFC
2304 * 8152, COSE] (https://tools.ietf.org/html/rfc8152)
2305 * implementation. **WARNING**, this pointer and length should be used
2306 * right away before any other calls to @c QCBOREncode_CloseXxx() as
2307 * they will move data around and the pointer and length will no
2308 * longer be to the correct encoded CBOR.
2309 *
2310 * When an error occurs as a result of this call, the encoder records
2311 * the error and enters the error state. The error will be returned
2312 * when QCBOREncode_Finish() is called.
2313 *
2314 * If this has been called more times than QCBOREncode_BstrWrap(),
2315 * then @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when
2316 * QCBOREncode_Finish() is called.
2317 *
2318 * If this is called and it is not a wrapping bstr that is currently
2319 * open, @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when
2320 * QCBOREncode_Finish() is called.
2321 *
2322 * QCBOREncode_CloseBstrWrap() is a deprecated version of this function
2323 * that is equivalent to the call with @c bIncludeCBORHead @c true.
Michael Eckel5c531332020-03-02 01:35:30 +01002324 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002325void
2326QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *pCtx, bool bIncludeCBORHead, UsefulBufC *pWrappedCBOR);
Michael Eckel5c531332020-03-02 01:35:30 +01002327
Laurence Lundblade3eead482023-12-16 20:53:22 -07002328static void
2329QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pCtx, UsefulBufC *pWrappedCBOR);
Michael Eckel5c531332020-03-02 01:35:30 +01002330
2331
2332/**
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07002333 * @brief Cancel byte string wrapping.
2334 *
2335 * @param[in] pCtx The encoding context.
2336 *
2337 * This cancels QCBOREncode_BstrWrap() making tghe encoding as if it
2338 * were never called.
2339 *
2340 * WARNING: This does not work on QCBOREncode_BstrWrapInMap()
2341 * or QCBOREncode_BstrWrapInMapN() and there is no error detection
2342 * of an attempt at their use.
2343 *
2344 * This only works if nothing has been added into the wrapped byte
2345 * string. If something has been added, this sets the error
2346 * @ref QCBOR_ERR_CANNOT_CANCEL.
2347 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002348void
2349QCBOREncode_CancelBstrWrap(QCBOREncodeContext *pCtx);
Laurence Lundblade8d3b8552021-06-10 11:11:54 -07002350
2351
2352/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002353 * @brief Add some already-encoded CBOR bytes.
2354 *
2355 * @param[in] pCtx The encoding context to add the already-encode CBOR to.
2356 * @param[in] Encoded The already-encoded CBOR to add to the context.
2357 *
2358 * The encoded CBOR being added must be fully conforming CBOR. It must
2359 * be complete with no arrays or maps that are incomplete. While this
2360 * encoder doesn't ever produce indefinite lengths, it is OK for the
2361 * raw CBOR added here to have indefinite lengths.
2362 *
2363 * The raw CBOR added here is not checked in anyway. If it is not
2364 * conforming or has open arrays or such, the final encoded CBOR
2365 * will probably be wrong or not what was intended.
2366 *
2367 * If the encoded CBOR being added here contains multiple items, they
2368 * must be enclosed in a map or array. At the top level the raw
2369 * CBOR must be a single data item.
Michael Eckel5c531332020-03-02 01:35:30 +01002370 */
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002371void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002372QCBOREncode_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01002373
Laurence Lundblade3eead482023-12-16 20:53:22 -07002374static void
2375QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01002376
Laurence Lundblade3eead482023-12-16 20:53:22 -07002377static void
2378QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01002379
2380
2381/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002382 * @brief Get the encoded result.
2383 *
2384 * @param[in] pCtx The context to finish encoding with.
2385 * @param[out] pEncodedCBOR Structure in which the pointer and length of
2386 * the encoded CBOR is returned.
2387 *
2388 * @retval QCBOR_SUCCESS Encoded CBOR is returned.
2389 *
2390 * @retval QCBOR_ERR_TOO_MANY_CLOSES Nesting error
2391 *
2392 * @retval QCBOR_ERR_CLOSE_MISMATCH Nesting error
2393 *
2394 * @retval QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN Nesting error
2395 *
2396 * @retval QCBOR_ERR_BUFFER_TOO_LARGE Encoded output buffer size
2397 *
2398 * @retval QCBOR_ERR_BUFFER_TOO_SMALL Encoded output buffer size
2399 *
2400 * @retval QCBOR_ERR_ARRAY_NESTING_TOO_DEEP Implementation limit
2401 *
2402 * @retval QCBOR_ERR_ARRAY_TOO_LONG Implementation limit
2403 *
2404 * On success, the pointer and length of the encoded CBOR are returned
2405 * in @c *pEncodedCBOR. The pointer is the same pointer that was passed
2406 * in to QCBOREncode_Init(). Note that it is not const when passed to
2407 * QCBOREncode_Init(), but it is const when returned here. The length
2408 * will be smaller than or equal to the length passed in when
2409 * QCBOREncode_Init() as this is the length of the actual result, not
2410 * the size of the buffer it was written to.
2411 *
2412 * If a @c NULL was passed for @c Storage.ptr when QCBOREncode_Init()
2413 * was called, @c NULL will be returned here, but the length will be
2414 * that of the CBOR that would have been encoded.
2415 *
2416 * Encoding errors primarily manifest here as most other encoding function
2417 * do no return an error. They just set the error state in the encode
2418 * context after which no encoding function does anything.
2419 *
2420 * Three types of errors manifest here. The first type are nesting
2421 * errors where the number of @c QCBOREncode_OpenXxx() calls do not
2422 * match the number @c QCBOREncode_CloseXxx() calls. The solution is to
2423 * fix the calling code.
2424 *
2425 * The second type of error is because the buffer given is either too
2426 * small or too large. The remedy is to give a correctly sized buffer.
2427 *
2428 * The third type are due to limits in this implementation.
2429 * @ref QCBOR_ERR_ARRAY_NESTING_TOO_DEEP can be worked around by
2430 * encoding the CBOR in two (or more) phases and adding the CBOR from
2431 * the first phase to the second with @c QCBOREncode_AddEncoded().
2432 *
2433 * If an error is returned, the buffer may have partially encoded
2434 * incorrect CBOR in it and it should not be used. Likewise, the length
2435 * may be incorrect and should not be used.
2436 *
2437 * Note that the error could have occurred in one of the many
2438 * @c QCBOREncode_AddXxx() calls long before QCBOREncode_Finish() was
2439 * called. This error handling reduces the CBOR implementation size
2440 * but makes debugging harder.
2441 *
2442 * This may be called multiple times. It will always return the
2443 * same. It can also be interleaved with calls to
2444 * QCBOREncode_FinishGetSize().
2445 *
2446 * QCBOREncode_GetErrorState() can be called to get the current
2447 * error state in order to abort encoding early as an optimization, but
2448 * calling it is is never required.
Michael Eckel5c531332020-03-02 01:35:30 +01002449 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002450QCBORError
2451QCBOREncode_Finish(QCBOREncodeContext *pCtx, UsefulBufC *pEncodedCBOR);
Michael Eckel5c531332020-03-02 01:35:30 +01002452
2453
2454/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002455 * @brief Get the encoded CBOR and error status.
2456 *
2457 * @param[in] pCtx The context to finish encoding with.
2458 * @param[out] uEncodedLen The length of the encoded or potentially
2459 * encoded CBOR in bytes.
2460 *
2461 * @return The same errors as QCBOREncode_Finish().
2462 *
2463 * This functions the same as QCBOREncode_Finish(), but only returns the
2464 * size of the encoded output.
Michael Eckel5c531332020-03-02 01:35:30 +01002465 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002466QCBORError
2467QCBOREncode_FinishGetSize(QCBOREncodeContext *pCtx, size_t *uEncodedLen);
Michael Eckel5c531332020-03-02 01:35:30 +01002468
2469
2470/**
Laurence Lundblade3eead482023-12-16 20:53:22 -07002471 * @brief Indicate whether output buffer is NULL or not.
2472 *
2473 * @param[in] pCtx The encoding context.
2474 *
2475 * @return 1 if the output buffer is @c NULL.
2476 *
2477 * Sometimes a @c NULL input buffer is given to QCBOREncode_Init() so
2478 * that the size of the generated CBOR can be calculated without
2479 * allocating a buffer for it. This returns 1 when the output buffer
2480 * is @c NULL and 0 when it is not.
Michael Eckel5c531332020-03-02 01:35:30 +01002481 */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002482static int
2483QCBOREncode_IsBufferNULL(QCBOREncodeContext *pCtx);
2484
2485
2486/**
2487 * @brief Get the encoding error state.
2488 *
2489 * @param[in] pCtx The encoding context.
2490 *
2491 * @return One of @ref QCBORError. See return values from
2492 * QCBOREncode_Finish()
2493 *
2494 * Normally encoding errors need only be handled at the end of
2495 * encoding when QCBOREncode_Finish() is called. This can be called to
2496 * get the error result before finish should there be a need to halt
2497 * encoding before QCBOREncode_Finish() is called.
2498 */
2499static QCBORError
2500QCBOREncode_GetErrorState(QCBOREncodeContext *pCtx);
2501
2502
2503/**
2504 * Encode the "head" of a CBOR data item.
2505 *
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002506 * @param Buffer Buffer to output the encoded head to; must be
Laurence Lundblade3eead482023-12-16 20:53:22 -07002507 * @ref QCBOR_HEAD_BUFFER_SIZE bytes in size.
2508 * @param uMajorType One of CBOR_MAJOR_TYPE_XX.
2509 * @param uMinLen The minimum number of bytes to encode uNumber. Almost
2510 * always this is 0 to use preferred
2511 * serialization. If this is 4, then even the
2512 * values 0xffff and smaller will be encoded in 4
2513 * bytes. This is used primarily when encoding a
2514 * float or double put into uNumber as the leading
2515 * zero bytes for them must be encoded.
2516 * @param uNumber The numeric argument part of the CBOR head.
2517 * @return Pointer and length of the encoded head or
2518 * @ref NULLUsefulBufC if the output buffer is too small.
2519 *
2520 * Callers do not to need to call this for normal CBOR encoding. Note
2521 * that it doesn't even take a @ref QCBOREncodeContext argument.
2522 *
2523 * This encodes the major type and argument part of a data item. The
2524 * argument is an integer that is usually either the value or the length
2525 * of the data item.
2526 *
2527 * This is exposed in the public interface to allow hashing of some CBOR
2528 * data types, bstr in particular, a chunk at a time so the full CBOR
2529 * doesn't have to be encoded in a contiguous buffer.
2530 *
2531 * For example, if you have a 100,000 byte binary blob in a buffer that
2532 * needs to be a bstr encoded and then hashed. You could allocate a
2533 * 100,010 byte buffer and encode it normally. Alternatively, you can
2534 * encode the head in a 10 byte buffer with this function, hash that and
2535 * then hash the 100,000 bytes using the same hash context.
Laurence Lundblade3eead482023-12-16 20:53:22 -07002536 */
2537UsefulBufC
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002538QCBOREncode_EncodeHead(UsefulBuf Buffer,
Laurence Lundblade3eead482023-12-16 20:53:22 -07002539 uint8_t uMajorType,
2540 uint8_t uMinLen,
2541 uint64_t uNumber);
Michael Eckel5c531332020-03-02 01:35:30 +01002542
2543
Michael Eckel5c531332020-03-02 01:35:30 +01002544
2545
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002546/* =========================================================================
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002547 BEGINNING OF PRIVATE IMPLEMENTATION
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002548 ========================================================================= */
Michael Eckel5c531332020-03-02 01:35:30 +01002549
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002550/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002551void QCBOREncode_Private_AppendCBORHead(QCBOREncodeContext *pMe,
2552 const uint8_t uMajorType,
2553 const uint64_t uArgument,
2554 const uint8_t uMinLen);
2555
2556
2557/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002558void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002559QCBOREncode_Private_AddBuffer(QCBOREncodeContext *pCtx,
2560 uint8_t uMajorType,
2561 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002562
2563
Laurence Lundblade8d9e0cd2024-05-25 18:12:19 -07002564/* Semi-private function for adding a double with preferred encoding. See qcbor_encode.c */
2565void
2566QCBOREncode_Private_AddPreferredDouble(QCBOREncodeContext *pMe, const double dNum);
2567
2568
2569/* Semi-private function for adding a float with preferred encoding. See qcbor_encode.c */
2570void
2571QCBOREncode_Private_AddPreferredFloat(QCBOREncodeContext *pMe, const float fNum);
2572
2573
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002574/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002575void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002576QCBOREncode_Private_OpenMapOrArray(QCBOREncodeContext *pCtx,
2577 uint8_t uMajorType);
Michael Eckel5c531332020-03-02 01:35:30 +01002578
2579
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002580/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002581void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002582QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx,
2583 uint8_t uMajorType);
Michael Eckel5c531332020-03-02 01:35:30 +01002584
2585
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002586/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002587void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002588QCBOREncode_Private_CloseMapOrArray(QCBOREncodeContext *pCtx,
2589 uint8_t uMajorType);
Michael Eckel5c531332020-03-02 01:35:30 +01002590
2591
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002592/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002593void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002594QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx,
2595 uint8_t uMajorType);
Michael Eckel5c531332020-03-02 01:35:30 +01002596
2597
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002598/* Semi-private funcion used by public inline functions. See qcbor_encode.c */
Laurence Lundblade3eead482023-12-16 20:53:22 -07002599void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002600QCBOREncode_Private_AddExpMantissa(QCBOREncodeContext *pCtx,
Laurence Lundblade3eead482023-12-16 20:53:22 -07002601 uint64_t uTag,
2602 UsefulBufC BigNumMantissa,
2603 bool bBigNumIsNegative,
2604 int64_t nMantissa,
2605 int64_t nExponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002606
Michael Eckel5c531332020-03-02 01:35:30 +01002607
2608
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002609/**
2610 * @brief Semi-private method to add simple items and floating-point.
2611 *
2612 * @param[in] pMe The encoding context.
2613 * @param[in] uMinLen Minimum encoding size for uNum. Usually 0.
2614 * @param[in] uArgument The value to add.
2615 *
2616 * This is used to add simple types like true and false and float-point
2617 * values, both of which are type 7.
2618 *
2619 * Call QCBOREncode_AddBool(), QCBOREncode_AddNULL(),
2620 * QCBOREncode_AddUndef() QCBOREncode_AddDouble() instead of this.
2621 *
2622 * Error handling is the same as QCBOREncode_AddInt64().
2623 */
2624static inline void
2625QCBOREncode_Private_AddType7(QCBOREncodeContext *pMe,
2626 const uint8_t uMinLen,
2627 const uint64_t uArgument)
2628{
2629 QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_SIMPLE, uArgument, uMinLen);
2630}
2631
2632
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002633/* Forward declaration */
2634static void
2635QCBOREncode_AddSZString(QCBOREncodeContext *pMe, const char *szString);
2636
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08002637
2638static inline void
2639QCBOREncode_SerializationCDE(QCBOREncodeContext *pMe)
2640{
2641 /* The use of a function pointer here is a little trick to reduce
2642 * code linked for the common use cases that don't sort. If this
2643 * function is never linked, then QCBOREncode_CloseAndSortMap() is
2644 * never linked and the amount of code pulled in is small. If the
2645 * mode switch between sorting and not sorting were an if
2646 * statement, then QCBOREncode_CloseAndSortMap() would always be
2647 * linked even when not used. */
2648 pMe->pfnCloseMap = QCBOREncode_CloseAndSortMap;
2649 pMe->uMode = QCBOR_ENCODE_MODE_CDE;
2650}
2651
2652static inline void
2653QCBOREncode_SerializationdCBOR(QCBOREncodeContext *pMe)
2654{
2655 pMe->pfnCloseMap = QCBOREncode_CloseAndSortMap;
2656 pMe->uMode = QCBOR_ENCODE_MODE_DCBOR;
2657}
2658
2659static inline void
2660QCBOREncode_SerializationPreferred(QCBOREncodeContext *pMe)
2661{
2662 pMe->uMode = QCBOR_ENCODE_MODE_PREFERRED;
2663}
2664
2665static inline void
2666QCBOREncode_Allow(QCBOREncodeContext *pMe, const uint8_t uAllow)
2667{
2668#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
2669 pMe->uAllow = uAllow;
2670#else
2671 (void)uAllow;
2672 (void)pMe;
2673#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
2674}
2675
2676
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002677static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002678QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pMe,
2679 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002680 const int64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002681{
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002682 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002683 QCBOREncode_AddInt64(pMe, uNum);
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_AddInt64ToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002688 const int64_t nLabel,
2689 const int64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002690{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002691 QCBOREncode_AddInt64(pMe, nLabel);
2692 QCBOREncode_AddInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002693}
2694
2695
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002696static inline void
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002697QCBOREncode_AddUInt64(QCBOREncodeContext *pMe, const uint64_t uValue)
2698{
2699 QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_POSITIVE_INT, uValue, 0);
2700}
2701
2702
2703static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002704QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pMe,
2705 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002706 const uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002707{
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002708 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002709 QCBOREncode_AddUInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002710}
2711
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002712static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002713QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002714 const int64_t nLabel,
2715 const uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002716{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002717 QCBOREncode_AddInt64(pMe, nLabel);
2718 QCBOREncode_AddUInt64(pMe, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002719}
2720
2721
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002722static inline void
Laurence Lundblade2d493002024-02-01 11:09:17 -07002723QCBOREncode_AddNegativeUInt64ToMap(QCBOREncodeContext *pMe, const char *szLabel, uint64_t uNum)
2724{
2725 /* Use _AddBuffer() because _AddSZString() is defined below, not above */
2726 QCBOREncode_Private_AddBuffer(pMe,
2727 CBOR_MAJOR_TYPE_TEXT_STRING,
2728 UsefulBuf_FromSZ(szLabel));
2729 QCBOREncode_AddNegativeUInt64(pMe, uNum);
2730}
2731
2732static inline void
2733QCBOREncode_AddNegativeUInt64ToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint64_t uNum)
2734{
2735 QCBOREncode_AddInt64(pMe, nLabel);
2736 QCBOREncode_AddNegativeUInt64(pMe, uNum);
2737}
2738
2739
2740static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002741QCBOREncode_AddText(QCBOREncodeContext *pMe, const UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002742{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002743 QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_TYPE_TEXT_STRING, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002744}
2745
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002746static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002747QCBOREncode_AddTextToMap(QCBOREncodeContext *pMe,
2748 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002749 const UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002750{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002751 QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szLabel));
2752 QCBOREncode_AddText(pMe, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002753}
2754
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002755static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002756QCBOREncode_AddTextToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002757 const int64_t nLabel,
2758 const UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002759{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002760 QCBOREncode_AddInt64(pMe, nLabel);
2761 QCBOREncode_AddText(pMe, Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002762}
2763
2764
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002765inline static void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002766QCBOREncode_AddSZString(QCBOREncodeContext *pMe, const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002767{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002768 QCBOREncode_AddText(pMe, UsefulBuf_FromSZ(szString));
Michael Eckel5c531332020-03-02 01:35:30 +01002769}
2770
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002771static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002772QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pMe,
2773 const char *szLabel,
2774 const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002775{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002776 QCBOREncode_AddSZString(pMe, szLabel);
2777 QCBOREncode_AddSZString(pMe, szString);
Michael Eckel5c531332020-03-02 01:35:30 +01002778}
2779
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002780static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002781QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002782 const int64_t nLabel,
Laurence Lundblade3eead482023-12-16 20:53:22 -07002783 const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002784{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002785 QCBOREncode_AddInt64(pMe, nLabel);
2786 QCBOREncode_AddSZString(pMe, szString);
Michael Eckel5c531332020-03-02 01:35:30 +01002787}
2788
2789
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002790
2791/*
2792 * Public functions for adding a tag. See qcbor/qcbor_encode.h
2793 */
2794static inline void
2795QCBOREncode_AddTag(QCBOREncodeContext *pMe, const uint64_t uTag)
2796{
2797 QCBOREncode_Private_AppendCBORHead(pMe, CBOR_MAJOR_TYPE_TAG, uTag, 0);
2798}
2799
2800
2801
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002802#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002803
2804static inline void
2805QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pMe, const double dNum)
2806{
2807 QCBOREncode_Private_AddType7(pMe,
2808 sizeof(uint64_t),
2809 UsefulBufUtil_CopyDoubleToUint64(dNum));
2810}
2811
2812static inline void
2813QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pMe, const float fNum)
2814{
2815 QCBOREncode_Private_AddType7(pMe,
2816 sizeof(uint32_t),
2817 UsefulBufUtil_CopyFloatToUint32(fNum));
2818}
2819
2820
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002821static inline void
Laurence Lundblade8d9e0cd2024-05-25 18:12:19 -07002822QCBOREncode_AddDouble(QCBOREncodeContext *pMe, const double dNum)
2823{
2824#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2825 QCBOREncode_Private_AddPreferredDouble(pMe, dNum);
2826#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2827 QCBOREncode_AddDoubleNoPreferred(pMe, dNum);
2828#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2829}
2830
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002831static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002832QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pMe,
2833 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002834 const double dNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002835{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002836 QCBOREncode_AddSZString(pMe, szLabel);
2837 QCBOREncode_AddDouble(pMe, dNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002838}
2839
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002840static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002841QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002842 const int64_t nLabel,
2843 const double dNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002844{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002845 QCBOREncode_AddInt64(pMe, nLabel);
2846 QCBOREncode_AddDouble(pMe, dNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002847}
2848
Laurence Lundblade8d9e0cd2024-05-25 18:12:19 -07002849
2850static inline void
2851QCBOREncode_AddFloat(QCBOREncodeContext *pMe, const float fNum)
2852{
2853#ifndef QCBOR_DISABLE_PREFERRED_FLOAT
2854 QCBOREncode_Private_AddPreferredFloat(pMe, fNum);
2855#else /* QCBOR_DISABLE_PREFERRED_FLOAT */
2856 QCBOREncode_AddFloatNoPreferred(pMe, fNum);
2857#endif /* QCBOR_DISABLE_PREFERRED_FLOAT */
2858}
2859
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002860static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002861QCBOREncode_AddFloatToMap(QCBOREncodeContext *pMe,
2862 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002863 const float dNum)
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002864{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002865 QCBOREncode_AddSZString(pMe, szLabel);
2866 QCBOREncode_AddFloat(pMe, dNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002867}
2868
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002869static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002870QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pMe,
2871 const int64_t nLabel,
2872 const float fNum)
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002873{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002874 QCBOREncode_AddInt64(pMe, nLabel);
2875 QCBOREncode_AddFloat(pMe, fNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002876}
2877
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002878static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002879QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pMe,
2880 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002881 const double dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002882{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002883 QCBOREncode_AddSZString(pMe, szLabel);
2884 QCBOREncode_AddDoubleNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002885}
2886
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002887static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002888QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002889 const int64_t nLabel,
2890 const double dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002891{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002892 QCBOREncode_AddInt64(pMe, nLabel);
2893 QCBOREncode_AddDoubleNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002894}
2895
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002896static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002897QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pMe,
2898 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002899 const float dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002900{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002901 QCBOREncode_AddSZString(pMe, szLabel);
2902 QCBOREncode_AddFloatNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002903}
2904
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002905static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002906QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002907 const int64_t nLabel,
2908 const float dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002909{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07002910 QCBOREncode_AddInt64(pMe, nLabel);
2911 QCBOREncode_AddFloatNoPreferred(pMe, dNum);
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002912}
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002913#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002914
Michael Eckel5c531332020-03-02 01:35:30 +01002915
Laurence Lundblade9b334962020-08-27 10:55:53 -07002916
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07002917
2918
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002919static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002920QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pMe,
2921 const uint8_t uTag,
2922 const int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002923{
2924 if(uTag == QCBOR_ENCODE_AS_TAG) {
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002925 QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_EPOCH);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002926 }
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002927 QCBOREncode_AddInt64(pMe, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002928}
2929
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002930static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002931QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pMe,
2932 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002933 const uint8_t uTag,
2934 const int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002935{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002936 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002937 QCBOREncode_AddTDateEpoch(pMe, uTag, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002938}
2939
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002940static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002941QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002942 const int64_t nLabel,
2943 const uint8_t uTag,
2944 const int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002945{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002946 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002947 QCBOREncode_AddTDateEpoch(pMe, uTag, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002948}
2949
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002950static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002951QCBOREncode_AddDateEpoch(QCBOREncodeContext *pMe,
2952 const int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002953{
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002954 QCBOREncode_AddTDateEpoch(pMe, QCBOR_ENCODE_AS_TAG, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002955}
2956
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002957static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002958QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pMe,
2959 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002960 const int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002961{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002962 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002963 QCBOREncode_AddDateEpoch(pMe, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002964}
2965
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002966static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002967QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002968 const int64_t nLabel,
2969 const int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002970{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002971 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002972 QCBOREncode_AddDateEpoch(pMe, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002973}
2974
2975
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002976static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002977QCBOREncode_AddTDaysEpoch(QCBOREncodeContext *pMe,
2978 const uint8_t uTag,
2979 const int64_t nDays)
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002980{
2981 if(uTag == QCBOR_ENCODE_AS_TAG) {
2982 QCBOREncode_AddTag(pMe, CBOR_TAG_DAYS_EPOCH);
2983 }
2984 QCBOREncode_AddInt64(pMe, nDays);
2985}
2986
2987static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002988QCBOREncode_AddTDaysEpochToMapSZ(QCBOREncodeContext *pMe,
2989 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002990 const uint8_t uTag,
2991 const int64_t nDays)
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002992{
2993 QCBOREncode_AddSZString(pMe, szLabel);
2994 QCBOREncode_AddTDaysEpoch(pMe, uTag, nDays);
2995}
2996
2997static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07002998QCBOREncode_AddTDaysEpochToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07002999 const int64_t nLabel,
3000 const uint8_t uTag,
3001 const int64_t nDays)
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003002{
3003 QCBOREncode_AddInt64(pMe, nLabel);
3004 QCBOREncode_AddTDaysEpoch(pMe, uTag, nDays);
3005}
3006
Laurence Lundblade9b334962020-08-27 10:55:53 -07003007
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003008static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003009QCBOREncode_AddBytes(QCBOREncodeContext *pMe,
3010 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003011{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003012 QCBOREncode_Private_AddBuffer(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003013}
3014
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003015static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003016QCBOREncode_AddBytesToMap(QCBOREncodeContext *pMe,
3017 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003018 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003019{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003020 QCBOREncode_AddSZString(pMe, szLabel);
3021 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003022}
3023
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003024static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003025QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003026 const int64_t nLabel,
3027 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003028{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003029 QCBOREncode_AddInt64(pMe, nLabel);
3030 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003031}
3032
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003033static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003034QCBOREncode_OpenBytesInMapSZ(QCBOREncodeContext *pMe,
3035 const char *szLabel,
3036 UsefulBuf *pPlace)
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003037{
3038 QCBOREncode_AddSZString(pMe, szLabel);
3039 QCBOREncode_OpenBytes(pMe, pPlace);
3040}
3041
3042static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003043QCBOREncode_OpenBytesInMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003044 const int64_t nLabel,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003045 UsefulBuf *pPlace)
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003046{
3047 QCBOREncode_AddInt64(pMe, nLabel);
3048 QCBOREncode_OpenBytes(pMe, pPlace);
3049}
3050
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003051
3052static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003053QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003054 const uint8_t uTagRequirement,
3055 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003056{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003057 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3058 QCBOREncode_AddTag(pMe, CBOR_TAG_BIN_UUID);
3059 }
3060 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003061}
3062
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003063static inline void
3064QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pMe,
3065 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003066 const uint8_t uTagRequirement,
3067 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003068{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003069 QCBOREncode_AddSZString(pMe, szLabel);
3070 QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003071}
3072
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003073static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003074QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003075 const int64_t nLabel,
3076 const uint8_t uTagRequirement,
3077 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003078{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003079 QCBOREncode_AddInt64(pMe, nLabel);
3080 QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003081}
3082
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003083static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003084QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pMe, const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003085{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003086 QCBOREncode_AddTBinaryUUID(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003087}
3088
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003089static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003090QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pMe,
3091 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003092 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003093{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003094 QCBOREncode_AddTBinaryUUIDToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003095}
3096
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003097static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003098QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003099 const int64_t nLabel,
3100 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003101{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003102 QCBOREncode_AddTBinaryUUIDToMapN(pMe,
3103 nLabel,
3104 QCBOR_ENCODE_AS_TAG,
3105 Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003106}
3107
3108
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003109static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003110QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003111 const uint8_t uTagRequirement,
3112 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003113{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003114 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3115 QCBOREncode_AddTag(pMe, CBOR_TAG_POS_BIGNUM);
3116 }
3117 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003118}
3119
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003120static inline void
3121QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pMe,
3122 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003123 const uint8_t uTagRequirement,
3124 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003125{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003126 QCBOREncode_AddSZString(pMe, szLabel);
3127 QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003128}
3129
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003130static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003131QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003132 const int64_t nLabel,
3133 const uint8_t uTagRequirement,
3134 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003135{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003136 QCBOREncode_AddInt64(pMe, nLabel);
3137 QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003138}
3139
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003140static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003141QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pMe, const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003142{
3143 QCBOREncode_AddTPositiveBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
3144}
3145
3146static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003147QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pMe,
3148 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003149 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003150{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003151 QCBOREncode_AddTPositiveBignumToMapSZ(pMe,
3152 szLabel,
3153 QCBOR_ENCODE_AS_TAG,
3154 Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003155}
3156
3157static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003158QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003159 const int64_t nLabel,
3160 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003161{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003162 QCBOREncode_AddTPositiveBignumToMapN(pMe,
3163 nLabel,
3164 QCBOR_ENCODE_AS_TAG,
3165 Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003166}
3167
3168
3169static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003170QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003171 const uint8_t uTagRequirement,
3172 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003173{
3174 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3175 QCBOREncode_AddTag(pMe, CBOR_TAG_NEG_BIGNUM);
3176 }
3177 QCBOREncode_AddBytes(pMe, Bytes);
3178}
3179
3180static inline void
3181QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pMe,
3182 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003183 const uint8_t uTagRequirement,
3184 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003185{
3186 QCBOREncode_AddSZString(pMe, szLabel);
3187 QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
3188}
3189
3190static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003191QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003192 const int64_t nLabel,
3193 const uint8_t uTagRequirement,
3194 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003195{
3196 QCBOREncode_AddInt64(pMe, nLabel);
3197 QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
3198}
3199
3200static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003201QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pMe, const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003202{
3203 QCBOREncode_AddTNegativeBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
3204}
3205
3206static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003207QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pMe,
3208 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003209 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003210{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003211 QCBOREncode_AddTNegativeBignumToMapSZ(pMe,
3212 szLabel,
3213 QCBOR_ENCODE_AS_TAG,
3214 Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003215}
3216
3217static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003218QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003219 const int64_t nLabel,
3220 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003221{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003222 QCBOREncode_AddTNegativeBignumToMapN(pMe,
3223 nLabel,
3224 QCBOR_ENCODE_AS_TAG,
3225 Bytes);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003226}
3227
3228
Michael Eckel5c531332020-03-02 01:35:30 +01003229
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003230#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Michael Eckel5c531332020-03-02 01:35:30 +01003231
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003232static inline void
3233QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003234 const uint8_t uTagRequirement,
3235 const int64_t nMantissa,
3236 const int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003237{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003238 uint64_t uTag;
3239 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3240 uTag = CBOR_TAG_DECIMAL_FRACTION;
3241 } else {
3242 uTag = CBOR_TAG_INVALID64;
3243 }
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003244 QCBOREncode_Private_AddExpMantissa(pMe,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003245 uTag,
Michael Eckel5c531332020-03-02 01:35:30 +01003246 NULLUsefulBufC,
3247 false,
3248 nMantissa,
3249 nBase10Exponent);
3250}
3251
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003252static inline void
3253QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pMe,
3254 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003255 const uint8_t uTagRequirement,
3256 const int64_t nMantissa,
3257 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003258{
3259 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003260 QCBOREncode_AddTDecimalFraction(pMe,
3261 uTagRequirement,
3262 nMantissa,
3263 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003264}
3265
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003266static inline void
3267QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003268 const int64_t nLabel,
3269 const uint8_t uTagRequirement,
3270 const int64_t nMantissa,
3271 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003272{
3273 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003274 QCBOREncode_AddTDecimalFraction(pMe,
3275 uTagRequirement,
3276 nMantissa,
3277 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003278}
3279
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003280static inline void
3281QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003282 const int64_t nMantissa,
3283 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003284{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003285 QCBOREncode_AddTDecimalFraction(pMe,
3286 QCBOR_ENCODE_AS_TAG,
3287 nMantissa,
3288 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003289}
3290
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003291static inline void
3292QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pMe,
3293 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003294 const int64_t nMantissa,
3295 const int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003296{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003297 QCBOREncode_AddTDecimalFractionToMapSZ(pMe,
3298 szLabel,
3299 QCBOR_ENCODE_AS_TAG,
3300 nMantissa,
3301 nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003302}
3303
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003304static inline void
3305QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003306 const int64_t nLabel,
3307 const int64_t nMantissa,
3308 const int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003309{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003310 QCBOREncode_AddTDecimalFractionToMapN(pMe,
3311 nLabel,
3312 QCBOR_ENCODE_AS_TAG,
3313 nMantissa,
3314 nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003315}
3316
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003317
3318
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003319static inline void
3320QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003321 const uint8_t uTagRequirement,
3322 const UsefulBufC Mantissa,
3323 const bool bIsNegative,
3324 const int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003325{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003326 uint64_t uTag;
3327 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3328 uTag = CBOR_TAG_DECIMAL_FRACTION;
3329 } else {
3330 uTag = CBOR_TAG_INVALID64;
3331 }
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003332 QCBOREncode_Private_AddExpMantissa(pMe,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003333 uTag,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003334 Mantissa,
3335 bIsNegative,
Michael Eckel5c531332020-03-02 01:35:30 +01003336 0,
3337 nBase10Exponent);
3338}
3339
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003340static inline void
3341QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
3342 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003343 const uint8_t uTagRequirement,
3344 const UsefulBufC Mantissa,
3345 const bool bIsNegative,
3346 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003347{
3348 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003349 QCBOREncode_AddTDecimalFractionBigNum(pMe,
3350 uTagRequirement,
3351 Mantissa,
3352 bIsNegative,
3353 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003354}
3355
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003356static inline void
3357QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003358 const int64_t nLabel,
3359 const uint8_t uTagRequirement,
3360 const UsefulBufC Mantissa,
3361 const bool bIsNegative,
3362 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003363{
3364 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003365 QCBOREncode_AddTDecimalFractionBigNum(pMe,
3366 uTagRequirement,
3367 Mantissa,
3368 bIsNegative,
3369 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003370}
3371
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003372static inline void
3373QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003374 const UsefulBufC Mantissa,
3375 const bool bIsNegative,
3376 const int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003377{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003378 QCBOREncode_AddTDecimalFractionBigNum(pMe,
3379 QCBOR_ENCODE_AS_TAG,
3380 Mantissa,
3381 bIsNegative,
3382 nBase10Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003383}
3384
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003385static inline void
3386QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
3387 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003388 const UsefulBufC Mantissa,
3389 const bool bIsNegative,
3390 const int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003391{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003392 QCBOREncode_AddTDecimalFractionBigNumToMapSZ(pMe,
3393 szLabel,
3394 QCBOR_ENCODE_AS_TAG,
3395 Mantissa,
3396 bIsNegative,
3397 nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003398}
3399
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003400static inline void
3401QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003402 const int64_t nLabel,
3403 const UsefulBufC Mantissa,
3404 const bool bIsNegative,
3405 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003406{
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003407 QCBOREncode_AddTDecimalFractionBigNumToMapN(pMe,
3408 nLabel,
3409 QCBOR_ENCODE_AS_TAG,
3410 Mantissa,
3411 bIsNegative,
3412 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003413}
3414
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003415
3416
3417
3418
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003419static inline void
3420QCBOREncode_AddTBigFloat(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003421 const uint8_t uTagRequirement,
3422 const int64_t nMantissa,
3423 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003424{
3425 uint64_t uTag;
3426 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3427 uTag = CBOR_TAG_BIGFLOAT;
3428 } else {
3429 uTag = CBOR_TAG_INVALID64;
3430 }
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003431 QCBOREncode_Private_AddExpMantissa(pMe,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003432 uTag,
3433 NULLUsefulBufC,
3434 false,
3435 nMantissa,
3436 nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003437}
3438
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003439static inline void
3440QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pMe,
3441 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003442 const uint8_t uTagRequirement,
3443 const int64_t nMantissa,
3444 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003445{
3446 QCBOREncode_AddSZString(pMe, szLabel);
3447 QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
3448}
3449
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003450static inline void
3451QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003452 const int64_t nLabel,
3453 const uint8_t uTagRequirement,
3454 const int64_t nMantissa,
3455 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003456{
3457 QCBOREncode_AddInt64(pMe, nLabel);
3458 QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
3459}
3460
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003461static inline void
3462QCBOREncode_AddBigFloat(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003463 const int64_t nMantissa,
3464 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003465{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003466 QCBOREncode_AddTBigFloat(pMe,
3467 QCBOR_ENCODE_AS_TAG,
3468 nMantissa,
3469 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003470}
3471
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003472static inline void
3473QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pMe,
3474 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003475 const int64_t nMantissa,
3476 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003477{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003478 QCBOREncode_AddTBigFloatToMapSZ(pMe,
3479 szLabel,
3480 QCBOR_ENCODE_AS_TAG,
3481 nMantissa,
3482 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003483}
3484
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003485static inline void
3486QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003487 const int64_t nLabel,
3488 const int64_t nMantissa,
3489 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003490{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003491 QCBOREncode_AddTBigFloatToMapN(pMe,
3492 nLabel,
3493 QCBOR_ENCODE_AS_TAG,
3494 nMantissa,
3495 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003496}
3497
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003498
3499
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003500static inline void
3501QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003502 const uint8_t uTagRequirement,
3503 const UsefulBufC Mantissa,
3504 const bool bIsNegative,
3505 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003506{
3507 uint64_t uTag;
3508 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3509 uTag = CBOR_TAG_BIGFLOAT;
3510 } else {
3511 uTag = CBOR_TAG_INVALID64;
3512 }
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003513 QCBOREncode_Private_AddExpMantissa(pMe,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003514 uTag,
3515 Mantissa,
3516 bIsNegative,
3517 0,
3518 nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003519}
3520
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003521static inline void
3522QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pMe,
3523 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003524 const uint8_t uTagRequirement,
3525 const UsefulBufC Mantissa,
3526 const bool bIsNegative,
3527 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003528{
3529 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003530 QCBOREncode_AddTBigFloatBigNum(pMe,
3531 uTagRequirement,
3532 Mantissa,
3533 bIsNegative,
3534 nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003535}
3536
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003537static inline void
3538QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003539 const int64_t nLabel,
3540 const uint8_t uTagRequirement,
3541 const UsefulBufC Mantissa,
3542 const bool bIsNegative,
3543 const int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003544{
3545 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade3eead482023-12-16 20:53:22 -07003546 QCBOREncode_AddTBigFloatBigNum(pMe,
3547 uTagRequirement,
3548 Mantissa,
3549 bIsNegative,
3550 nBase2Exponent);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003551}
3552
3553
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003554static inline void
3555QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003556 const UsefulBufC Mantissa,
3557 const bool bIsNegative,
3558 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003559{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003560 QCBOREncode_AddTBigFloatBigNum(pMe,
3561 QCBOR_ENCODE_AS_TAG,
3562 Mantissa, bIsNegative,
3563 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003564}
3565
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003566static inline void
3567QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pMe,
3568 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003569 const UsefulBufC Mantissa,
3570 const bool bIsNegative,
3571 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003572{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003573 QCBOREncode_AddTBigFloatBigNumToMapSZ(pMe,
3574 szLabel,
3575 QCBOR_ENCODE_AS_TAG,
3576 Mantissa,
3577 bIsNegative,
3578 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003579}
3580
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003581static inline void
3582QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003583 const int64_t nLabel,
3584 const UsefulBufC Mantissa,
3585 const bool bIsNegative,
3586 const int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01003587{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003588 QCBOREncode_AddTBigFloatBigNumToMapN(pMe,
3589 nLabel,
3590 QCBOR_ENCODE_AS_TAG,
3591 Mantissa,
3592 bIsNegative,
3593 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01003594}
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003595#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Michael Eckel5c531332020-03-02 01:35:30 +01003596
3597
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003598static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003599QCBOREncode_AddTURI(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003600 const uint8_t uTagRequirement,
3601 const UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01003602{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003603 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3604 QCBOREncode_AddTag(pMe, CBOR_TAG_URI);
3605 }
3606 QCBOREncode_AddText(pMe, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01003607}
3608
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003609static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003610QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pMe,
3611 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003612 const uint8_t uTagRequirement,
3613 const UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01003614{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003615 QCBOREncode_AddSZString(pMe, szLabel);
3616 QCBOREncode_AddTURI(pMe, uTagRequirement, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01003617}
3618
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003619static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003620QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003621 const int64_t nLabel,
3622 const uint8_t uTagRequirement,
3623 const UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01003624{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003625 QCBOREncode_AddInt64(pMe, nLabel);
3626 QCBOREncode_AddTURI(pMe, uTagRequirement, URI);
3627}
3628
3629static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003630QCBOREncode_AddURI(QCBOREncodeContext *pMe, const UsefulBufC URI)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003631{
3632 QCBOREncode_AddTURI(pMe, QCBOR_ENCODE_AS_TAG, URI);
3633}
3634
3635static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003636QCBOREncode_AddURIToMap(QCBOREncodeContext *pMe,
3637 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003638 const UsefulBufC URI)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003639{
3640 QCBOREncode_AddTURIToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, URI);
3641}
3642
3643static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003644QCBOREncode_AddURIToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003645 const int64_t nLabel,
3646 const UsefulBufC URI)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003647{
3648 QCBOREncode_AddTURIToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01003649}
3650
3651
3652
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003653static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003654QCBOREncode_AddTB64Text(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003655 const uint8_t uTagRequirement,
3656 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003657{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003658 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3659 QCBOREncode_AddTag(pMe, CBOR_TAG_B64);
3660 }
3661 QCBOREncode_AddText(pMe, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003662}
3663
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003664static inline void
3665QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pMe,
3666 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003667 const uint8_t uTagRequirement,
3668 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003669{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003670 QCBOREncode_AddSZString(pMe, szLabel);
3671 QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003672}
3673
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003674static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003675QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003676 const int64_t nLabel,
3677 const uint8_t uTagRequirement,
3678 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003679{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003680 QCBOREncode_AddInt64(pMe, nLabel);
3681 QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text);
3682}
3683
3684static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003685QCBOREncode_AddB64Text(QCBOREncodeContext *pMe, const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003686{
3687 QCBOREncode_AddTB64Text(pMe, QCBOR_ENCODE_AS_TAG, B64Text);
3688}
3689
3690static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003691QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pMe,
3692 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003693 const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003694{
3695 QCBOREncode_AddTB64TextToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, B64Text);
3696}
3697
3698static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003699QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003700 const int64_t nLabel,
3701 const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003702{
3703 QCBOREncode_AddTB64TextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003704}
3705
3706
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003707
3708static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003709QCBOREncode_AddTB64URLText(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003710 const uint8_t uTagRequirement,
3711 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003712{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003713 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3714 QCBOREncode_AddTag(pMe, CBOR_TAG_B64URL);
3715 }
3716 QCBOREncode_AddText(pMe, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003717}
3718
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003719static inline void
3720QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pMe,
3721 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003722 const uint8_t uTagRequirement,
3723 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003724{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003725 QCBOREncode_AddSZString(pMe, szLabel);
3726 QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003727}
3728
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003729static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003730QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003731 const int64_t nLabel,
3732 const uint8_t uTagRequirement,
3733 const UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01003734{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003735 QCBOREncode_AddInt64(pMe, nLabel);
3736 QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text);
3737}
3738
3739static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003740QCBOREncode_AddB64URLText(QCBOREncodeContext *pMe, const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003741{
3742 QCBOREncode_AddTB64URLText(pMe, QCBOR_ENCODE_AS_TAG, B64Text);
3743}
3744
3745static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003746QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pMe,
3747 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003748 const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003749{
Laurence Lundblade3eead482023-12-16 20:53:22 -07003750 QCBOREncode_AddTB64URLTextToMapSZ(pMe,
3751 szLabel,
3752 QCBOR_ENCODE_AS_TAG,
3753 B64Text);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003754}
3755
3756static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003757QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003758 const int64_t nLabel,
3759 const UsefulBufC B64Text)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003760{
3761 QCBOREncode_AddTB64URLTextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01003762}
3763
3764
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003765
3766static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003767QCBOREncode_AddTRegex(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003768 const uint8_t uTagRequirement,
3769 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003770{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003771 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3772 QCBOREncode_AddTag(pMe, CBOR_TAG_REGEX);
3773 }
3774 QCBOREncode_AddText(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003775}
3776
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003777static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003778QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pMe,
3779 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003780 const uint8_t uTagRequirement,
3781 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003782{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003783 QCBOREncode_AddSZString(pMe, szLabel);
3784 QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01003785}
3786
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003787static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003788QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003789 const int64_t nLabel,
3790 const uint8_t uTagRequirement,
3791 const UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01003792{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003793 QCBOREncode_AddInt64(pMe, nLabel);
3794 QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes);
3795}
3796
3797static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003798QCBOREncode_AddRegex(QCBOREncodeContext *pMe, const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003799{
3800 QCBOREncode_AddTRegex(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
3801}
3802
3803static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003804QCBOREncode_AddRegexToMap(QCBOREncodeContext *pMe,
3805 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003806 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003807{
3808 QCBOREncode_AddTRegexToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
3809}
3810
3811static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003812QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003813 const int64_t nLabel,
3814 const UsefulBufC Bytes)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003815{
3816 QCBOREncode_AddTRegexToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
3817
Michael Eckel5c531332020-03-02 01:35:30 +01003818}
3819
3820
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003821static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003822QCBOREncode_AddTMIMEData(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003823 const uint8_t uTagRequirement,
3824 const UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01003825{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003826 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
Laurence Lundblade4982f412020-09-18 23:02:18 -07003827 QCBOREncode_AddTag(pMe, CBOR_TAG_BINARY_MIME);
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003828 }
Laurence Lundblade4982f412020-09-18 23:02:18 -07003829 QCBOREncode_AddBytes(pMe, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01003830}
3831
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003832static inline void
3833QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pMe,
3834 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003835 const uint8_t uTagRequirement,
3836 const UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01003837{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003838 QCBOREncode_AddSZString(pMe, szLabel);
3839 QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01003840}
3841
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003842static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003843QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003844 const int64_t nLabel,
3845 const uint8_t uTagRequirement,
3846 const UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01003847{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003848 QCBOREncode_AddInt64(pMe, nLabel);
3849 QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData);
3850}
3851
3852static inline void
3853QCBOREncode_AddMIMEData(QCBOREncodeContext *pMe, UsefulBufC MIMEData)
3854{
3855 QCBOREncode_AddTMIMEData(pMe, QCBOR_ENCODE_AS_TAG, MIMEData);
3856}
3857
3858static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003859QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pMe,
3860 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003861 const UsefulBufC MIMEData)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003862{
3863 QCBOREncode_AddTMIMEDataToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, MIMEData);
3864}
3865
3866static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003867QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003868 const int64_t nLabel,
3869 const UsefulBufC MIMEData)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003870{
3871 QCBOREncode_AddTMIMEDataToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01003872}
3873
3874
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003875static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003876QCBOREncode_AddTDateString(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003877 const uint8_t uTagRequirement,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003878 const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01003879{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003880 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3881 QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_STRING);
3882 }
3883 QCBOREncode_AddSZString(pMe, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01003884}
3885
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003886static inline void
3887QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pMe,
3888 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003889 const uint8_t uTagRequirement,
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003890 const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01003891{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003892 QCBOREncode_AddSZString(pMe, szLabel);
3893 QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01003894}
3895
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003896static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003897QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003898 const int64_t nLabel,
3899 const uint8_t uTagRequirement,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003900 const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01003901{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003902 QCBOREncode_AddInt64(pMe, nLabel);
3903 QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate);
3904}
3905
3906static inline void
3907QCBOREncode_AddDateString(QCBOREncodeContext *pMe, const char *szDate)
3908{
3909 QCBOREncode_AddTDateString(pMe, QCBOR_ENCODE_AS_TAG, szDate);
3910}
3911
3912static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003913QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pMe,
3914 const char *szLabel,
3915 const char *szDate)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003916{
3917 QCBOREncode_AddTDateStringToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, szDate);
3918}
3919
3920static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003921QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003922 const int64_t nLabel,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003923 const char *szDate)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003924{
3925 QCBOREncode_AddTDateStringToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01003926}
3927
3928
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003929static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003930QCBOREncode_AddTDaysString(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003931 const uint8_t uTagRequirement,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003932 const char *szDate)
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003933{
3934 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
3935 QCBOREncode_AddTag(pMe, CBOR_TAG_DAYS_STRING);
3936 }
3937 QCBOREncode_AddSZString(pMe, szDate);
3938}
3939
3940static inline void
3941QCBOREncode_AddTDaysStringToMapSZ(QCBOREncodeContext *pMe,
3942 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003943 const uint8_t uTagRequirement,
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003944 const char *szDate)
3945{
3946 QCBOREncode_AddSZString(pMe, szLabel);
3947 QCBOREncode_AddTDaysString(pMe, uTagRequirement, szDate);
3948}
3949
3950static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07003951QCBOREncode_AddTDaysStringToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003952 const int64_t nLabel,
3953 const uint8_t uTagRequirement,
Laurence Lundblade3eead482023-12-16 20:53:22 -07003954 const char *szDate)
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003955{
3956 QCBOREncode_AddInt64(pMe, nLabel);
3957 QCBOREncode_AddTDaysString(pMe, uTagRequirement, szDate);
3958}
3959
3960
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003961static inline void
Laurence Lundblade3888f002024-06-12 21:20:56 -07003962QCBOREncode_AddSimple(QCBOREncodeContext *pMe, const uint8_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01003963{
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003964#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
3965 if(pMe->uMode >= QCBOR_ENCODE_MODE_DCBOR) {
3966 if(uNum < CBOR_SIMPLEV_FALSE ||
3967 uNum > CBOR_SIMPLEV_NULL) {
3968 pMe->uError = QCBOR_ERR_NOT_PREFERRED;
3969 return;
3970 }
3971 }
Laurence Lundblade70fc1252024-05-31 10:57:28 -07003972 /* This check often is optimized out because uNum is known at compile time. */
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07003973 if(uNum >= CBOR_SIMPLEV_RESERVED_START && uNum <= CBOR_SIMPLEV_RESERVED_END) {
3974 pMe->uError = QCBOR_ERR_ENCODE_UNSUPPORTED;
3975 return;
3976 }
3977#endif /* !QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08003978
Laurence Lundblade8e36f812024-01-26 10:59:29 -07003979 QCBOREncode_Private_AddType7(pMe, 0, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01003980}
3981
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003982static inline void
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07003983QCBOREncode_AddSimpleToMap(QCBOREncodeContext *pMe,
Laurence Lundblade3888f002024-06-12 21:20:56 -07003984 const char *szLabel,
3985 const uint8_t uSimple)
Michael Eckel5c531332020-03-02 01:35:30 +01003986{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003987 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07003988 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01003989}
3990
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003991static inline void
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07003992QCBOREncode_AddSimpleToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade3888f002024-06-12 21:20:56 -07003993 const int64_t nLabel,
3994 const uint8_t uSimple)
Michael Eckel5c531332020-03-02 01:35:30 +01003995{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003996 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07003997 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01003998}
3999
4000
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004001static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004002QCBOREncode_AddBool(QCBOREncodeContext *pMe, const bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01004003{
4004 uint8_t uSimple = CBOR_SIMPLEV_FALSE;
4005 if(b) {
4006 uSimple = CBOR_SIMPLEV_TRUE;
4007 }
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07004008 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01004009}
4010
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004011static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004012QCBOREncode_AddBoolToMap(QCBOREncodeContext *pMe, const char *szLabel, const bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01004013{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004014 QCBOREncode_AddSZString(pMe, szLabel);
4015 QCBOREncode_AddBool(pMe, b);
Michael Eckel5c531332020-03-02 01:35:30 +01004016}
4017
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004018static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004019QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pMe, const int64_t nLabel, const bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01004020{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004021 QCBOREncode_AddInt64(pMe, nLabel);
4022 QCBOREncode_AddBool(pMe, b);
Michael Eckel5c531332020-03-02 01:35:30 +01004023}
4024
4025
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004026static inline void
4027QCBOREncode_AddNULL(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004028{
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07004029 QCBOREncode_AddSimple(pMe, CBOR_SIMPLEV_NULL);
Michael Eckel5c531332020-03-02 01:35:30 +01004030}
4031
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004032static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004033QCBOREncode_AddNULLToMap(QCBOREncodeContext *pMe, 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_AddNULL(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004037}
4038
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004039static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004040QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pMe, const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004041{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004042 QCBOREncode_AddInt64(pMe, nLabel);
4043 QCBOREncode_AddNULL(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004044}
4045
4046
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004047static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004048QCBOREncode_AddUndef(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004049{
Laurence Lundbladecbd7d132024-05-19 11:11:22 -07004050 QCBOREncode_AddSimple(pMe, CBOR_SIMPLEV_UNDEF);
Michael Eckel5c531332020-03-02 01:35:30 +01004051}
4052
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004053static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004054QCBOREncode_AddUndefToMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004055{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004056 QCBOREncode_AddSZString(pMe, szLabel);
4057 QCBOREncode_AddUndef(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004058}
4059
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004060static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004061QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pMe, const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004062{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004063 QCBOREncode_AddInt64(pMe, nLabel);
4064 QCBOREncode_AddUndef(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004065}
4066
4067
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004068static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004069QCBOREncode_OpenArray(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004070{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004071 QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY);
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_OpenArrayInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004076{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004077 QCBOREncode_AddSZString(pMe, szLabel);
4078 QCBOREncode_OpenArray(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004079}
4080
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004081static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004082QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pMe, const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004083{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004084 QCBOREncode_AddInt64(pMe, nLabel);
4085 QCBOREncode_OpenArray(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004086}
4087
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08004088
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004089static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004090QCBOREncode_CloseArray(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004091{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004092 QCBOREncode_Private_CloseMapOrArray(pMe, CBOR_MAJOR_TYPE_ARRAY);
Michael Eckel5c531332020-03-02 01:35:30 +01004093}
4094
4095
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004096static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004097QCBOREncode_OpenMap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004098{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004099 QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_MAP);
Michael Eckel5c531332020-03-02 01:35:30 +01004100}
4101
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004102static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004103QCBOREncode_OpenMapInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004104{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004105 QCBOREncode_AddSZString(pMe, szLabel);
4106 QCBOREncode_OpenMap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004107}
4108
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004109static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004110QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pMe, const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004111{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004112 QCBOREncode_AddInt64(pMe, nLabel);
4113 QCBOREncode_OpenMap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004114}
4115
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004116static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004117QCBOREncode_CloseMap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004118{
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08004119 (pMe->pfnCloseMap)(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004120}
4121
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004122static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004123QCBOREncode_OpenArrayIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004124{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004125 QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01004126}
4127
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004128static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004129QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pMe,
4130 const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004131{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004132 QCBOREncode_AddSZString(pMe, szLabel);
4133 QCBOREncode_OpenArrayIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004134}
4135
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004136static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004137QCBOREncode_OpenArrayIndefiniteLengthInMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004138 const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004139{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004140 QCBOREncode_AddInt64(pMe, nLabel);
4141 QCBOREncode_OpenArrayIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004142}
4143
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004144static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004145QCBOREncode_CloseArrayIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004146{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004147 QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01004148}
4149
4150
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004151static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004152QCBOREncode_OpenMapIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004153{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004154 QCBOREncode_Private_OpenMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01004155}
4156
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004157static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004158QCBOREncode_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pMe,
4159 const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004160{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004161 QCBOREncode_AddSZString(pMe, szLabel);
4162 QCBOREncode_OpenMapIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004163}
4164
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004165static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004166QCBOREncode_OpenMapIndefiniteLengthInMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004167 const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004168{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004169 QCBOREncode_AddInt64(pMe, nLabel);
4170 QCBOREncode_OpenMapIndefiniteLength(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004171}
4172
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004173static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004174QCBOREncode_CloseMapIndefiniteLength(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004175{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004176 QCBOREncode_Private_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
Michael Eckel5c531332020-03-02 01:35:30 +01004177}
4178
4179
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004180static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004181QCBOREncode_BstrWrap(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004182{
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004183 QCBOREncode_Private_OpenMapOrArray(pMe, CBOR_MAJOR_TYPE_BYTE_STRING);
Michael Eckel5c531332020-03-02 01:35:30 +01004184}
4185
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004186static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004187QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pMe, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004188{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004189 QCBOREncode_AddSZString(pMe, szLabel);
4190 QCBOREncode_BstrWrap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004191}
4192
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004193static inline void
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004194QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pMe, const int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01004195{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004196 QCBOREncode_AddInt64(pMe, nLabel);
4197 QCBOREncode_BstrWrap(pMe);
Michael Eckel5c531332020-03-02 01:35:30 +01004198}
4199
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004200static inline void
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004201QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pMe, UsefulBufC *pWrappedCBOR)
Michael Eckel5c531332020-03-02 01:35:30 +01004202{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004203 QCBOREncode_CloseBstrWrap2(pMe, true, pWrappedCBOR);
Michael Eckel5c531332020-03-02 01:35:30 +01004204}
4205
4206
Michael Eckel5c531332020-03-02 01:35:30 +01004207
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004208static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004209QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pMe,
4210 const char *szLabel,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004211 const UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01004212{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004213 QCBOREncode_AddSZString(pMe, szLabel);
4214 QCBOREncode_AddEncoded(pMe, Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01004215}
4216
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004217static inline void
Laurence Lundblade3eead482023-12-16 20:53:22 -07004218QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pMe,
Laurence Lundblade8e36f812024-01-26 10:59:29 -07004219 const int64_t nLabel,
4220 const UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01004221{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004222 QCBOREncode_AddInt64(pMe, nLabel);
4223 QCBOREncode_AddEncoded(pMe, Encoded);
Michael Eckel5c531332020-03-02 01:35:30 +01004224}
4225
4226
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004227static inline int
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004228QCBOREncode_IsBufferNULL(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004229{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004230 return UsefulOutBuf_IsBufferNULL(&(pMe->OutBuf));
Michael Eckel5c531332020-03-02 01:35:30 +01004231}
4232
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07004233static inline QCBORError
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004234QCBOREncode_GetErrorState(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01004235{
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004236 if(UsefulOutBuf_GetError(&(pMe->OutBuf))) {
Laurence Lundbladef2f0c3f2024-04-12 13:01:54 -07004237 /* Items didn't fit in the buffer. This check catches this
4238 * condition for all the appends and inserts so checks aren't
4239 * needed when the appends and inserts are performed. And of
4240 * course UsefulBuf will never overrun the input buffer given to
4241 * it. No complex analysis of the error handling in this file is
4242 * needed to know that is true. Just read the UsefulBuf code.
4243 */
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004244 pMe->uError = QCBOR_ERR_BUFFER_TOO_SMALL;
Laurence Lundbladef2f0c3f2024-04-12 13:01:54 -07004245 /* QCBOR_ERR_BUFFER_TOO_SMALL masks other errors, but that is
4246 * OK. Once the caller fixes this, they'll be unmasked.
4247 */
Michael Eckel5c531332020-03-02 01:35:30 +01004248 }
4249
Laurence Lundblade6416a0f2020-09-19 23:26:34 -07004250 return (QCBORError)pMe->uError;
Michael Eckel5c531332020-03-02 01:35:30 +01004251}
4252
4253
Laurence Lundblade45d5e482020-09-15 21:15:15 -07004254/* ========================================================================
4255 END OF PRIVATE INLINE IMPLEMENTATION
4256 ======================================================================== */
Michael Eckel5c531332020-03-02 01:35:30 +01004257
4258#ifdef __cplusplus
4259}
4260#endif
4261
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08004262#endif /* qcbor_encode_h */