blob: 0951bd5289e0228e01e4fe5fb549a08412a986dc [file] [log] [blame]
Michael Eckel5c531332020-03-02 01:35:30 +01001/*==============================================================================
2 Copyright (c) 2016-2018, The Linux Foundation.
3 Copyright (c) 2018-2020, Laurence Lundblade.
4 All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =============================================================================*/
32
33
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#ifndef qcbor_encode_h
35#define qcbor_encode_h
Michael Eckel5c531332020-03-02 01:35:30 +010036
37
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080038#include "qcbor/qcbor_common.h"
39#include "qcbor/qcbor_private.h"
Michael Eckel5c531332020-03-02 01:35:30 +010040#include <stdbool.h>
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080041
Michael Eckel5c531332020-03-02 01:35:30 +010042
43#ifdef __cplusplus
44extern "C" {
Dave Thaler12b23752020-03-27 01:23:08 -070045#if 0
Michael Eckel5c531332020-03-02 01:35:30 +010046} // Keep editor indention formatting happy
47#endif
48#endif
49
Michael Eckel5c531332020-03-02 01:35:30 +010050
51/**
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080052 @file qcbor_encode.h
Michael Eckel5c531332020-03-02 01:35:30 +010053
54 Q C B O R E n c o d e / D e c o d e
55
56 This implements CBOR -- Concise Binary Object Representation as
57 defined in [RFC 7049] (https://tools.ietf.org/html/rfc7049). More
58 info is at http://cbor.io. This is a near-complete implementation of
59 the specification. Limitations are listed further down.
60
61 CBOR is intentionally designed to be translatable to JSON, but not
62 all CBOR can convert to JSON. See RFC 7049 for more info on how to
63 construct CBOR that is the most JSON friendly.
64
65 The memory model for encoding and decoding is that encoded CBOR must
66 be in a contiguous buffer in memory. During encoding the caller must
67 supply an output buffer and if the encoding would go off the end of
68 the buffer an error is returned. During decoding the caller supplies
69 the encoded CBOR in a contiguous buffer and the decoder returns
70 pointers and lengths into that buffer for strings.
71
72 This implementation does not require malloc. All data structures
73 passed in/out of the APIs can fit on the stack.
74
75 Decoding of indefinite-length strings is a special case that requires
76 a "string allocator" to allocate memory into which the segments of
77 the string are coalesced. Without this, decoding will error out if an
78 indefinite-length string is encountered (indefinite-length maps and
79 arrays do not require the string allocator). A simple string
80 allocator called MemPool is built-in and will work if supplied with a
81 block of memory to allocate. The string allocator can optionally use
82 malloc() or some other custom scheme.
83
84 Here are some terms and definitions:
85
86 - "Item", "Data Item": An integer or string or such. The basic "thing" that
87 CBOR is about. An array is an item itself that contains some items.
88
89 - "Array": An ordered sequence of items, the same as JSON.
90
91 - "Map": A collection of label/value pairs. Each pair is a data
92 item. A JSON "object" is the same as a CBOR "map".
93
94 - "Label": The data item in a pair in a map that names or identifies
95 the pair, not the value. This implementation refers to it as a
96 "label". JSON refers to it as the "name". The CBOR RFC refers to it
97 this as a "key". This implementation chooses label instead because
98 key is too easily confused with a cryptographic key. The COSE
99 standard, which uses CBOR, has also chosen to use the term "label"
100 rather than "key" for this same reason.
101
102 - "Key": See "Label" above.
103
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700104 - "Tag": A data item that is an explicitly labeled new data
105 type made up of the tagging integer and the tag content.
106 See @Tags-Overview and @Tag-Usage.
Michael Eckel5c531332020-03-02 01:35:30 +0100107
108 - "Initial Byte": The first byte of an encoded item. Encoding and
109 decoding of this byte is taken care of by the implementation.
110
111 - "Additional Info": In addition to the major type, all data items
112 have some other info. This is usually the length of the data but can
113 be several other things. Encoding and decoding of this is taken care
114 of by the implementation.
115
116 CBOR has two mechanisms for tagging and labeling the data values like
117 integers and strings. For example, an integer that represents
118 someone's birthday in epoch seconds since Jan 1, 1970 could be
119 encoded like this:
120
121 - First it is CBOR_MAJOR_TYPE_POSITIVE_INT (@ref QCBOR_TYPE_INT64),
122 the primitive positive integer.
123
124 - Next it has a "tag" @ref CBOR_TAG_DATE_EPOCH indicating the integer
125 represents a date in the form of the number of seconds since Jan 1,
126 1970.
127
128 - Last it has a string "label" like "BirthDate" indicating the
129 meaning of the data.
130
131 The encoded binary looks like this:
132
133 a1 # Map of 1 item
134 69 # Indicates text string of 9 bytes
135 426972746844617465 # The text "BirthDate"
136 c1 # Tags next integer as epoch date
137 1a # Indicates a 4-byte integer
138 580d4172 # unsigned integer date 1477263730
139
140 Implementors using this API will primarily work with
141 labels. Generally, tags are only needed for making up new data
142 types. This implementation covers most of the data types defined in
143 the RFC using tags. It also, allows for the use of custom tags if
144 necessary.
145
146 This implementation explicitly supports labels that are text strings
147 and integers. Text strings translate nicely into JSON objects and are
148 very readable. Integer labels are much less readable but can be very
149 compact. If they are in the range of 0 to 23, they take up only one
150 byte.
151
152 CBOR allows a label to be any type of data including an array or a
153 map. It is possible to use this API to construct and parse such
154 labels, but it is not explicitly supported.
155
156 A common encoding usage mode is to invoke the encoding twice. First
157 with no output buffer to compute the length of the needed output
158 buffer. Then the correct sized output buffer is allocated. Last the
159 encoder is invoked again, this time with the output buffer.
160
161 The double invocation is not required if the maximum output buffer
162 size can be predicted. This is usually possible for simple CBOR
163 structures. If the double invocation is implemented, it can be in a
164 loop or function as in the example code so that the code doesn't have
165 to actually be written twice, saving code size.
166
167 If a buffer too small to hold the encoded output is given, the error
168 @ref QCBOR_ERR_BUFFER_TOO_SMALL will be returned. Data will never be
169 written off the end of the output buffer no matter which functions
170 here are called or what parameters are passed to them.
171
172 The encoding error handling is simple. The only possible errors are
173 trying to encode structures that are too large or too complex. There
174 are no internal malloc calls so there will be no failures for out of
175 memory. The error state is tracked internally, so there is no need
176 to check for errors when encoding. Only the return code from
177 QCBOREncode_Finish() need be checked as once an error happens, the
178 encoder goes into an error state and calls to it to add more data
179 will do nothing. An error check is not needed after every data item
180 is added.
181
182 Encoding generally proceeds by calling QCBOREncode_Init(), calling
183 lots of @c QCBOREncode_AddXxx() functions and calling
184 QCBOREncode_Finish(). There are many @c QCBOREncode_AddXxx()
185 functions for various data types. The input buffers need only to be
186 valid during the @c QCBOREncode_AddXxx() calls as the data is copied
187 into the output buffer.
188
189 There are three `Add` functions for each data type. The first / main
190 one for the type is for adding the data item to an array. The second
191 one's name ends in `ToMap`, is used for adding data items to maps and
192 takes a string argument that is its label in the map. The third one
193 ends in `ToMapN`, is also used for adding data items to maps, and
194 takes an integer argument that is its label in the map.
195
196 The simplest aggregate type is an array, which is a simple ordered
197 set of items without labels the same as JSON arrays. Call
198 QCBOREncode_OpenArray() to open a new array, then various @c
199 QCBOREncode_AddXxx() functions to put items in the array and then
200 QCBOREncode_CloseArray(). Nesting to the limit @ref
201 QCBOR_MAX_ARRAY_NESTING is allowed. All opens must be matched by
202 closes or an encoding error will be returned.
203
204 The other aggregate type is a map which does use labels. The `Add`
205 functions that end in `ToMap` and `ToMapN` are convenient ways to add
206 labeled data items to a map. You can also call any type of `Add`
207 function once to add a label of any time and then call any type of
208 `Add` again to add its value.
209
210 Note that when you nest arrays or maps in a map, the nested array or
211 map has a label.
Laurence Lundblade2feb1e12020-07-15 03:50:45 -0700212
Laurence Lundbladee3553422020-05-02 11:11:17 -0700213 Many CBOR-based protocols start with an array or map. This makes them
214 self-delimiting. No external length or end marker is needed to know
215 the end. It is also possible not start this way, in which case this
216 it is usually called a CBOR sequence which is described in [RFC 8742] (https://tools.ietf.org/html/rfc8742 ).
217 This encoder supports either just by whether the first item added is an
218 array, map or other.
Michael Eckel5c531332020-03-02 01:35:30 +0100219
220 @anchor Tags-Overview
Laurence Lundblade9b334962020-08-27 10:55:53 -0700221
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700222 Any CBOR data item can be made into a tag to add semantics, define a new data
Michael Eckel5c531332020-03-02 01:35:30 +0100223 type or such. Some tags are fully standardized and some are just
224 registered. Others are not registered and used in a proprietary way.
225
226 Encoding and decoding of many of the registered tags is fully
227 implemented by QCBOR. It is also possible to encode and decode tags
228 that are not directly supported. For many use cases the built-in tag
229 support should be adequate.
230
231 For example, the registered epoch date tag is supported in encoding
232 by QCBOREncode_AddDateEpoch() and in decoding by @ref
233 QCBOR_TYPE_DATE_EPOCH and the @c epochDate member of @ref
234 QCBORItem. This is typical of the built-in tag support. There is an
235 API to encode data for it and a @c QCBOR_TYPE_XXX when it is decoded.
236
237 Tags are registered in the [IANA CBOR Tags Registry]
238 (https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml). There
239 are roughly three options to create a new tag. First, a public
240 specification can be created and the new tag registered with IANA.
241 This is the most formal. Second, the new tag can be registered with
242 IANA with just a short description rather than a full specification.
243 These tags must be greater than 256. Third, a tag can be used without
244 any IANA registration, though the registry should be checked to see
245 that the new value doesn't collide with one that is registered. The
246 value of these tags must be 256 or larger.
247
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700248 See also @ref CBORTags and @ref Tag-Usage
249
Michael Eckel5c531332020-03-02 01:35:30 +0100250 The encoding side of tags not built-in is handled by
251 QCBOREncode_AddTag() and is relatively simple. Tag decoding is more
252 complex and mainly handled by QCBORDecode_GetNext(). Decoding of the
253 structure of tagged data not built-in (if there is any) has to be
254 implemented by the caller.
255
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700256 @anchor Floating-Point
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700257 By default QCBOR fully supports IEEE 754 floating-point:
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700258 - Encode/decode of double, single and half-precision
259 - CBOR preferred serialization of floating-point
260 - Floating-point epoch dates
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700261
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700262 For the most part, the type double is used in the interface for
263 floating-point values. In the default configuration, all decoded
264 floating-point values are returned as a double.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700265
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700266 With CBOR preferred serialization, the encoder outputs the smallest
267 representation of the double or float that preserves precision. Zero,
268 NaN and infinity are always output as a half-precision, each taking
269 just 2 bytes. This reduces the number of bytes needed to encode
Laurence Lundblade4b270642020-08-14 12:53:07 -0700270 double and single-precision, especially if zero, NaN and infinity are
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700271 frequently used.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700272
Laurence Lundblade4b270642020-08-14 12:53:07 -0700273 To avoid use of preferred serialization in the standard configuration
274 when encoding, use QCBOREncode_AddDoubleNoPreferred() or
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700275 QCBOREncode_AddFloatNoPreferred().
276
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700277 This implementation of preferred floating-point serialization and
278 half-precision does not depend on the CPU having floating-point HW or
279 the compiler bringing in a (sometimes large) library to compensate
Laurence Lundblade4b270642020-08-14 12:53:07 -0700280 for lack of CPU support. This implementation uses shifts and masks
281 rather than floating-point functions.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700282
Laurence Lundblade4b270642020-08-14 12:53:07 -0700283 To reduce overall object code by about 900 bytes, define
284 QCBOR_DISABLE_PREFERRED_FLOAT. This will eliminate all support for
285 preferred serialization and half-precision. An error will be returned
286 when attempting to decode half-precision. A float will always be
287 encoded and decoded as 32-bits and a double will always be encoded
288 and decoded as 64 bits.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700289
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700290 Note that even if QCBOR_DISABLE_PREFERRED_FLOAT is not defined all
291 the float-point encoding object code can be avoided by never calling
Laurence Lundbladefe09bbf2020-07-16 12:14:51 -0700292 any functions that encode double or float. Just not calling
293 floating-point functions will reduce object code by about 500 bytes.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700294
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700295 On CPUs that have no floating-point hardware,
296 QCBOR_DISABLE_FLOAT_HW_USE should be defined in most cases. If it is
297 not, then the compiler will bring in possibly large software
Laurence Lundblade4b270642020-08-14 12:53:07 -0700298 libraries to compensate. Defining
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700299 QCBOR_DISABLE_FLOAT_HW_USE reduces object code size on CPUs with
Laurence Lundblade4b270642020-08-14 12:53:07 -0700300 floating-point hardware by a tiny amount and eliminates the need for <math.h>
Laurence Lundblade5fb6ab42020-07-16 03:28:47 -0700301
Laurence Lundblade4b270642020-08-14 12:53:07 -0700302 When QCBOR_DISABLE_FLOAT_HW_USE is defined, trying to decoding
303 floating-point dates will give error @ref
304 QCBOR_ERR_FLOAT_DATE_DISABLED and decoded single-precision numbers
305 will be returned as @ref QCBOR_TYPE_FLOAT instead of converting them
306 to double as usual.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700307
308 If both QCBOR_DISABLE_FLOAT_HW_USE and QCBOR_DISABLE_PREFERRED_FLOAT
Laurence Lundblade4b270642020-08-14 12:53:07 -0700309 are defined, then the only thing QCBOR can do is encode/decode a C
310 float type as 32-bits and a C double type as 64-bits. Floating-point
311 epoch dates will be unsupported.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700312
Michael Eckel5c531332020-03-02 01:35:30 +0100313 Summary Limits of this implementation:
314 - The entire encoded CBOR must fit into contiguous memory.
315 - Max size of encoded / decoded CBOR data is @c UINT32_MAX (4GB).
316 - Max array / map nesting level when encoding / decoding is
317 @ref QCBOR_MAX_ARRAY_NESTING (this is typically 15).
318 - Max items in an array or map when encoding / decoding is
319 @ref QCBOR_MAX_ITEMS_IN_ARRAY (typically 65,536).
320 - Does not directly support labels in maps other than text strings & integers.
321 - Does not directly support integer labels greater than @c INT64_MAX.
322 - Epoch dates limited to @c INT64_MAX (+/- 292 billion years).
323 - Exponents for bigfloats and decimal integers are limited to @c INT64_MAX.
324 - Tags on labels are ignored during decoding.
325 - There is no duplicate detection of map labels (but duplicates are passed on).
326 - Works only on 32- and 64-bit CPUs (modifications could make it work
327 on 16-bit CPUs).
328
329 The public interface uses @c size_t for all lengths. Internally the
330 implementation uses 32-bit lengths by design to use less memory and
331 fit structures on the stack. This limits the encoded CBOR it can work
332 with to size @c UINT32_MAX (4GB) which should be enough.
333
334 This implementation assumes two's compliment integer machines. @c
335 <stdint.h> also requires this. It is possible to modify this
336 implementation for another integer representation, but all modern
337 machines seem to be two's compliment.
338
339 */
340
341
Michael Eckel5c531332020-03-02 01:35:30 +0100342/*
343 The size of the buffer to be passed to QCBOREncode_EncodeHead(). It is one
344 byte larger than sizeof(uint64_t) + 1, the actual maximum size of the
345 head of a CBOR data item. because QCBOREncode_EncodeHead() needs
346 one extra byte to work.
347 */
348#define QCBOR_HEAD_BUFFER_SIZE (sizeof(uint64_t) + 2)
349
Michael Eckel5c531332020-03-02 01:35:30 +0100350
Laurence Lundblade9b334962020-08-27 10:55:53 -0700351/**
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700352 Output the full CBOR tag. See @ref CBORTags, @ref Tag-Usage and @ref Tags-Overview.
Laurence Lundblade9b334962020-08-27 10:55:53 -0700353 */
354#define QCBOR_ENCODE_AS_TAG 0
355
356/**
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700357 Output only the 'borrowed' content format for the relevant tag.
358 See @ref CBORTags, @ref Tag-Usage and @ref Tags-Overview.
Laurence Lundblade9b334962020-08-27 10:55:53 -0700359 */
360#define QCBOR_ENCODE_AS_BORROWED 1
361
Michael Eckel5c531332020-03-02 01:35:30 +0100362
363/**
364 QCBOREncodeContext is the data type that holds context for all the
365 encoding functions. It is less than 200 bytes, so it can go on the
366 stack. The contents are opaque, and the caller should not access
367 internal members. A context may be re used serially as long as it is
368 re initialized.
369 */
370typedef struct _QCBOREncodeContext QCBOREncodeContext;
371
372
373/**
374 Initialize the encoder to prepare to encode some CBOR.
375
376 @param[in,out] pCtx The encoder context to initialize.
377 @param[in] Storage The buffer into which this encoded result
378 will be placed.
379
380 Call this once at the start of an encoding of a CBOR structure. Then
381 call the various @c QCBOREncode_AddXxx() functions to add the data
382 items. Then call QCBOREncode_Finish().
383
384 The maximum output buffer is @c UINT32_MAX (4GB). This is not a
385 practical limit in any way and reduces the memory needed by the
386 implementation. The error @ref QCBOR_ERR_BUFFER_TOO_LARGE will be
387 returned by QCBOREncode_Finish() if a larger buffer length is passed
388 in.
389
390 If this is called with @c Storage.ptr as @c NULL and @c Storage.len a
391 large value like @c UINT32_MAX, all the QCBOREncode_AddXxx()
392 functions and QCBOREncode_Finish() can still be called. No data will
393 be encoded, but the length of what would be encoded will be
394 calculated. The length of the encoded structure will be handed back
395 in the call to QCBOREncode_Finish(). You can then allocate a buffer
396 of that size and call all the encoding again, this time to fill in
397 the buffer.
398
399 A @ref QCBOREncodeContext can be reused over and over as long as
400 QCBOREncode_Init() is called.
401 */
402void QCBOREncode_Init(QCBOREncodeContext *pCtx, UsefulBuf Storage);
403
404
405/**
406 @brief Add a signed 64-bit integer to the encoded output.
407
408 @param[in] pCtx The encoding context to add the integer to.
409 @param[in] nNum The integer to add.
410
411 The integer will be encoded and added to the CBOR output.
412
413 This function figures out the size and the sign and encodes in the
414 correct minimal CBOR. Specifically, it will select CBOR major type 0
415 or 1 based on sign and will encode to 1, 2, 4 or 8 bytes depending on
416 the value of the integer. Values less than 24 effectively encode to
417 one byte because they are encoded in with the CBOR major type. This
418 is a neat and efficient characteristic of CBOR that can be taken
419 advantage of when designing CBOR-based protocols. If integers like
420 tags can be kept between -23 and 23 they will be encoded in one byte
421 including the major type.
422
423 If you pass a smaller int, say an @c int16_t or a small value, say
424 100, the encoding will still be CBOR's most compact that can
425 represent the value. For example, CBOR always encodes the value 0 as
426 one byte, 0x00. The representation as 0x00 includes identification of
427 the type as an integer too as the major type for an integer is 0. See
428 [RFC 7049] (https://tools.ietf.org/html/rfc7049) Appendix A for more
429 examples of CBOR encoding. This compact encoding is also canonical
430 CBOR as per section 3.9 in RFC 7049.
431
432 There are no functions to add @c int16_t or @c int32_t because they
433 are not necessary because this always encodes to the smallest number
434 of bytes based on the value (If this code is running on a 32-bit
435 machine having a way to add 32-bit integers would reduce code size
436 some).
437
438 If the encoding context is in an error state, this will do
439 nothing. If an error occurs when adding this integer, the internal
440 error flag will be set, and the error will be returned when
441 QCBOREncode_Finish() is called.
442
443 See also QCBOREncode_AddUInt64().
444 */
445void QCBOREncode_AddInt64(QCBOREncodeContext *pCtx, int64_t nNum);
446
447static void QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t uNum);
448
449static void QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum);
450
451
452/**
453 @brief Add an unsigned 64-bit integer to the encoded output.
454
455 @param[in] pCtx The encoding context to add the integer to.
456 @param[in] uNum The integer to add.
457
458 The integer will be encoded and added to the CBOR output.
459
460 The only reason so use this function is for integers larger than @c
461 INT64_MAX and smaller than @c UINT64_MAX. Otherwise
462 QCBOREncode_AddInt64() will work fine.
463
464 Error handling is the same as for QCBOREncode_AddInt64().
465 */
466void QCBOREncode_AddUInt64(QCBOREncodeContext *pCtx, uint64_t uNum);
467
468static void QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum);
469
470static void QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum);
471
472
473/**
474 @brief Add a UTF-8 text string to the encoded output.
475
476 @param[in] pCtx The encoding context to add the text to.
477 @param[in] Text Pointer and length of text to add.
478
479 The text passed in must be unencoded UTF-8 according to [RFC 3629]
480 (https://tools.ietf.org/html/rfc3629). There is no NULL
481 termination. The text is added as CBOR major type 3.
482
483 If called with @c nBytesLen equal to 0, an empty string will be
484 added. When @c nBytesLen is 0, @c pBytes may be @c NULL.
485
486 Note that the restriction of the buffer length to a @c uint32_t is
487 entirely intentional as this encoder is not capable of encoding
488 lengths greater. This limit to 4GB for a text string should not be a
489 problem.
490
491 Error handling is the same as QCBOREncode_AddInt64().
492 */
493static void QCBOREncode_AddText(QCBOREncodeContext *pCtx, UsefulBufC Text);
494
495static void QCBOREncode_AddTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Text);
496
497static void QCBOREncode_AddTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Text);
498
499
500/**
501 @brief Add a UTF-8 text string to the encoded output.
502
503 @param[in] pCtx The encoding context to add the text to.
504 @param[in] szString Null-terminated text to add.
505
506 This works the same as QCBOREncode_AddText().
507 */
508static void QCBOREncode_AddSZString(QCBOREncodeContext *pCtx, const char *szString);
509
510static void QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szString);
511
512static void QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szString);
513
514
515/**
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700516 @brief Add a double-precision floating-point number to the encoded output.
Michael Eckel5c531332020-03-02 01:35:30 +0100517
518 @param[in] pCtx The encoding context to add the double to.
519 @param[in] dNum The double-precision number to add.
520
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700521 This encodes and outputs a floating-point number. CBOR major type 7
522 is used.
Michael Eckel5c531332020-03-02 01:35:30 +0100523
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700524 This implements preferred serialization, selectively encoding the
525 double-precision floating-point number as either double-precision,
526 single-precision or half-precision. Infinity, NaN and 0 are always
527 encoded as half-precision. If no precision will be lost in the
528 conversion to half-precision, then it will be converted and
529 encoded. If not and no precision will be lost in conversion to
530 single-precision, then it will be converted and encoded. If not, then
531 no conversion is performed, and it encoded as a double-precision.
Michael Eckel5c531332020-03-02 01:35:30 +0100532
533 Half-precision floating-point numbers take up 2 bytes, half that of
534 single-precision, one quarter of double-precision
535
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700536 This automatically reduces the size of encoded CBOR, maybe even by
537 four if most of values are 0, infinity or NaN.
Michael Eckel5c531332020-03-02 01:35:30 +0100538
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700539 When decoded, QCBOR will usually return these values as
540 double-precision.
541
542 It is possible to disable this preferred serialization when compiling
543 QCBOR. In that case, this functions the same as
544 QCBOREncode_AddDoubleNoPreferred().
Michael Eckel5c531332020-03-02 01:35:30 +0100545
546 Error handling is the same as QCBOREncode_AddInt64().
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700547
548 See also QCBOREncode_AddDoubleNoPreferred(), QCBOREncode_AddFloat()
549 and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
Michael Eckel5c531332020-03-02 01:35:30 +0100550 */
551void QCBOREncode_AddDouble(QCBOREncodeContext *pCtx, double dNum);
552
553static void QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum);
554
555static void QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum);
556
557
558/**
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700559 @brief Add a single-precision floating-point number to the encoded output.
560
561 @param[in] pCtx The encoding context to add the double to.
562 @param[in] fNum The single-precision number to add.
563
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700564 This is identical to QCBOREncode_AddDouble() except the input is
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700565 single-precision.
566
567 See also QCBOREncode_AddDouble(), QCBOREncode_AddDoubleNoPreferred(),
568 and QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
569*/
570void QCBOREncode_AddFloat(QCBOREncodeContext *pCtx, float fNum);
571
572static void QCBOREncode_AddFloatToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum);
573
574static void QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float dNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700575
576
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700577/**
578 @brief Add a double-precision floating-point number without preferred encoding.
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700579
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700580 @param[in] pCtx The encoding context to add the double to.
581 @param[in] dNum The double-precision number to add.
582
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700583 This always outputs the number as a 64-bit double-precision.
584 Preferred serialization is not used.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700585
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700586 Error handling is the same as QCBOREncode_AddInt64().
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700587
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700588 See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
589 QCBOREncode_AddFloatNoPreferred() and @ref Floating-Point.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700590*/
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700591void QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pCtx, double dNum);
592
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700593static void QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum);
594
595static void QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum);
596
597
598/**
599 @brief Add a single-precision floating-point number without preferred encoding.
600
601 @param[in] pCtx The encoding context to add the double to.
602 @param[in] fNum The single-precision number to add.
603
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700604 This always outputs the number as a 32-bit single-precision.
605 Preferred serialization is not used.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700606
607 Error handling is the same as QCBOREncode_AddInt64().
608
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -0700609 See also QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), and
610 QCBOREncode_AddDoubleNoPreferred() and @ref Floating-Point.
Laurence Lundblade32f3e622020-07-13 20:35:11 -0700611*/
612void QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pCtx, float fNum);
613
614static void QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, float fNum);
615
616static void QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float fNum);
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -0700617
618
Michael Eckel5c531332020-03-02 01:35:30 +0100619
620/**
621 @brief Add an optional tag.
622
623 @param[in] pCtx The encoding context to add the tag to.
624 @param[in] uTag The tag to add
625
626 This outputs a CBOR major type 6 item that tags the next data item
627 that is output usually to indicate it is some new data type.
628
629 For many of the common standard tags, a function to encode data using
630 it is provided and this is not needed. For example,
631 QCBOREncode_AddDateEpoch() already exists to output integers
632 representing dates with the right tag.
633
634 The tag is applied to the next data item added to the encoded
635 output. That data item that is to be tagged can be of any major CBOR
636 type. Any number of tags can be added to a data item by calling this
637 multiple times before the data item is added.
638
639 See @ref Tags-Overview for discussion of creating new non-standard
640 tags. See QCBORDecode_GetNext() for discussion of decoding custom
641 tags.
642*/
643void QCBOREncode_AddTag(QCBOREncodeContext *pCtx,uint64_t uTag);
644
645
646/**
647 @brief Add an epoch-based date.
648
649 @param[in] pCtx The encoding context to add the date to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700650 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700651 @param[in] nDate Number of seconds since 1970-01-01T00:00Z in UTC time.
Michael Eckel5c531332020-03-02 01:35:30 +0100652
653 As per RFC 7049 this is similar to UNIX/Linux/POSIX dates. This is
654 the most compact way to specify a date and time in CBOR. Note that
655 this is always UTC and does not include the time zone. Use
656 QCBOREncode_AddDateString() if you want to include the time zone.
657
658 The integer encoding rules apply here so the date will be encoded in
659 a minimal number of bytes. Until about the year 2106 these dates will
660 encode in 6 bytes -- one byte for the tag, one byte for the type and
661 4 bytes for the integer. After that it will encode to 10 bytes.
662
663 Negative values are supported for dates before 1970.
664
665 If you care about leap-seconds and that level of accuracy, make sure
666 the system you are running this code on does it correctly. This code
667 just takes the value passed in.
668
669 This implementation cannot encode fractional seconds using float or
670 double even though that is allowed by CBOR, but you can encode them
671 if you want to by calling QCBOREncode_AddDouble() and
672 QCBOREncode_AddTag().
673
674 Error handling is the same as QCBOREncode_AddInt64().
675 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700676static void QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pCtx,
677 uint8_t uTagRequirement,
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700678 int64_t nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -0700679
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700680static void QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pCtx,
681 const char *szLabel,
682 uint8_t uTagRequirement,
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700683 int64_t nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -0700684
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700685static void QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pCtx,
686 int64_t nLabel,
687 uint8_t uTagRequirement,
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700688 int64_t nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -0700689
690
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700691static void QCBOREncode_AddDateEpoch(QCBOREncodeContext *pCtx,
692 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100693
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700694static void QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pCtx,
695 const char *szLabel,
696 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100697
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700698static void QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pCtx,
699 int64_t nLabel,
700 int64_t nDate);
Michael Eckel5c531332020-03-02 01:35:30 +0100701
702
703/**
704 @brief Add a byte string to the encoded output.
705
706 @param[in] pCtx The encoding context to add the bytes to.
707 @param[in] Bytes Pointer and length of the input data.
708
709 Simply adds the bytes to the encoded output as CBOR major type 2.
710
711 If called with @c Bytes.len equal to 0, an empty string will be
712 added. When @c Bytes.len is 0, @c Bytes.ptr may be @c NULL.
713
714 Error handling is the same as QCBOREncode_AddInt64().
715 */
716static void QCBOREncode_AddBytes(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
717
718static void QCBOREncode_AddBytesToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
719
720static void QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
721
722
723
724/**
725 @brief Add a binary UUID to the encoded output.
726
727 @param[in] pCtx The encoding context to add the UUID to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700728 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100729 @param[in] Bytes Pointer and length of the binary UUID.
730
731 A binary UUID as defined in [RFC 4122]
732 (https://tools.ietf.org/html/rfc4122) is added to the output.
733
734 It is output as CBOR major type 2, a binary string, with tag @ref
735 CBOR_TAG_BIN_UUID indicating the binary string is a UUID.
736 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700737static void QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pCtx,
738 uint8_t uTagRequirement,
739 UsefulBufC Bytes);
740
741static void QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pCtx,
742 const char *szLabel,
743 uint8_t uTagRequirement,
744 UsefulBufC Bytes);
745
746static void QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pCtx,
747 int64_t nLabel,
748 uint8_t uTagRequirement,
749 UsefulBufC Bytes);
750
751
Michael Eckel5c531332020-03-02 01:35:30 +0100752static void QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
753
754static void QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
755
756static void QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
757
758
759/**
760 @brief Add a positive big number to the encoded output.
761
762 @param[in] pCtx The encoding context to add the big number to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700763 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100764 @param[in] Bytes Pointer and length of the big number.
765
766 Big numbers are integers larger than 64-bits. Their format is
767 described in [RFC 7049] (https://tools.ietf.org/html/rfc7049).
768
769 It is output as CBOR major type 2, a binary string, with tag @ref
770 CBOR_TAG_POS_BIGNUM indicating the binary string is a positive big
771 number.
772
773 Often big numbers are used to represent cryptographic keys, however,
774 COSE which defines representations for keys chose not to use this
775 particular type.
776 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700777static void QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pCtx,
778 uint8_t uTagRequirement,
779 UsefulBufC Bytes);
780
781static void QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pCtx,
782 const char *szLabel,
783 uint8_t uTagRequirement,
784 UsefulBufC Bytes);
785
786static void QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pCtx,
787 int64_t nLabel,
788 uint8_t uTagRequirement,
789 UsefulBufC Bytes);
790
791
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700792static void QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pCtx,
793 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100794
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700795static void QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pCtx,
796 const char *szLabel,
797 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100798
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700799static void QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pCtx,
800 int64_t nLabel,
801 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100802
803
804/**
805 @brief Add a negative big number to the encoded output.
806
807 @param[in] pCtx The encoding context to add the big number to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700808 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100809 @param[in] Bytes Pointer and length of the big number.
810
811 Big numbers are integers larger than 64-bits. Their format is
812 described in [RFC 7049] (https://tools.ietf.org/html/rfc7049).
813
814 It is output as CBOR major type 2, a binary string, with tag @ref
815 CBOR_TAG_NEG_BIGNUM indicating the binary string is a negative big
816 number.
817
818 Often big numbers are used to represent cryptographic keys, however,
819 COSE which defines representations for keys chose not to use this
820 particular type.
821 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700822static void QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pCtx,
823 uint8_t uTagRequirement,
824 UsefulBufC Bytes);
825
826static void QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pCtx,
827 const char *szLabel,
828 uint8_t uTagRequirement,
829 UsefulBufC Bytes);
830
831static void QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pCtx,
832 int64_t nLabel,
833 uint8_t uTagRequirement,
834 UsefulBufC Bytes);
835
836
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700837static void QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pCtx,
838 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100839
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700840static void QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pCtx,
841 const char *szLabel,
842 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100843
Laurence Lundblade45d5e482020-09-15 21:15:15 -0700844static void QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pCtx,
845 int64_t nLabel,
846 UsefulBufC Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +0100847
848
849#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
850/**
851 @brief Add a decimal fraction to the encoded output.
852
853 @param[in] pCtx The encoding context to add the decimal fraction to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700854 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100855 @param[in] nMantissa The mantissa.
856 @param[in] nBase10Exponent The exponent.
857
858 The value is nMantissa * 10 ^ nBase10Exponent.
859
860 A decimal fraction is good for exact representation of some values
861 that can't be represented exactly with standard C (IEEE 754)
862 floating-point numbers. Much larger and much smaller numbers can
863 also be represented than floating-point because of the larger number
864 of bits in the exponent.
865
866 The decimal fraction is conveyed as two integers, a mantissa and a
867 base-10 scaling factor.
868
869 For example, 273.15 is represented by the two integers 27315 and -2.
870
871 The exponent and mantissa have the range from @c INT64_MIN to
872 @c INT64_MAX for both encoding and decoding (CBOR allows @c -UINT64_MAX
873 to @c UINT64_MAX, but this implementation doesn't support this range to
874 reduce code size and interface complexity a little).
875
876 CBOR Preferred encoding of the integers is used, thus they will be encoded
877 in the smallest number of bytes possible.
878
879 See also QCBOREncode_AddDecimalFractionBigNum() for a decimal
880 fraction with arbitrarily large precision and QCBOREncode_AddBigFloat().
881
882 There is no representation of positive or negative infinity or NaN
883 (Not a Number). Use QCBOREncode_AddDouble() to encode them.
884
885 See @ref expAndMantissa for decoded representation.
886 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700887static void QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pCtx,
888 uint8_t uTagRequirement,
889 int64_t nMantissa,
890 int64_t nBase10Exponent);
891
892static void QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pCtx,
893 const char *szLabel,
894 uint8_t uTagRequirement,
895 int64_t nMantissa,
896 int64_t nBase10Exponent);
897
898static void QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pCtx,
899 int64_t nLabel,
900 uint8_t uTagRequirement,
901 int64_t nMantissa,
902 int64_t nBase10Exponent);
903
904
Michael Eckel5c531332020-03-02 01:35:30 +0100905static void QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pCtx,
906 int64_t nMantissa,
907 int64_t nBase10Exponent);
908
909static void QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pCtx,
910 const char *szLabel,
911 int64_t nMantissa,
912 int64_t nBase10Exponent);
913
914static void QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pCtx,
915 int64_t nLabel,
916 int64_t nMantissa,
917 int64_t nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +0100918/**
919 @brief Add a decimal fraction with a big number mantissa to the encoded output.
920
921 @param[in] pCtx The encoding context to add the decimal fraction to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700922 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100923 @param[in] Mantissa The mantissa.
924 @param[in] bIsNegative false if mantissa is positive, true if negative.
925 @param[in] nBase10Exponent The exponent.
926
927 This is the same as QCBOREncode_AddDecimalFraction() except the
928 mantissa is a big number (See QCBOREncode_AddPositiveBignum())
929 allowing for arbitrarily large precision.
930
931 See @ref expAndMantissa for decoded representation.
932 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700933static void QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pCtx,
934 uint8_t uTagRequirement,
935 UsefulBufC Mantissa,
936 bool bIsNegative,
937 int64_t nBase10Exponent);
938
939static void QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
940 const char *szLabel,
941 uint8_t uTagRequirement,
942 UsefulBufC Mantissa,
943 bool bIsNegative,
944 int64_t nBase10Exponent);
945
946static void QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
947 int64_t nLabel,
948 uint8_t uTagRequirement,
949 UsefulBufC Mantissa,
950 bool bIsNegative,
951 int64_t nBase10Exponent);
952
953
Michael Eckel5c531332020-03-02 01:35:30 +0100954static void QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pCtx,
955 UsefulBufC Mantissa,
956 bool bIsNegative,
957 int64_t nBase10Exponent);
958
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700959static void QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pCtx,
Michael Eckel5c531332020-03-02 01:35:30 +0100960 const char *szLabel,
961 UsefulBufC Mantissa,
962 bool bIsNegative,
963 int64_t nBase10Exponent);
964
965static void QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pCtx,
966 int64_t nLabel,
967 UsefulBufC Mantissa,
968 bool bIsNegative,
969 int64_t nBase10Exponent);
970
971/**
972 @brief Add a big floating-point number to the encoded output.
973
974 @param[in] pCtx The encoding context to add the bigfloat to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -0700975 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +0100976 @param[in] nMantissa The mantissa.
977 @param[in] nBase2Exponent The exponent.
978
979 The value is nMantissa * 2 ^ nBase2Exponent.
980
981 "Bigfloats", as CBOR terms them, are similar to IEEE floating-point
982 numbers in having a mantissa and base-2 exponent, but they are not
983 supported by hardware or encoded the same. They explicitly use two
984 CBOR-encoded integers to convey the mantissa and exponent, each of which
985 can be 8, 16, 32 or 64 bits. With both the mantissa and exponent
986 64 bits they can express more precision and a larger range than an
987 IEEE double floating-point number. See
988 QCBOREncode_AddBigFloatBigNum() for even more precision.
989
990 For example, 1.5 would be represented by a mantissa of 3 and an
991 exponent of -1.
992
993 The exponent and mantissa have the range from @c INT64_MIN to
994 @c INT64_MAX for both encoding and decoding (CBOR allows @c -UINT64_MAX
995 to @c UINT64_MAX, but this implementation doesn't support this range to
996 reduce code size and interface complexity a little).
997
998 CBOR Preferred encoding of the integers is used, thus they will be encoded
999 in the smallest number of bytes possible.
1000
1001 This can also be used to represent floating-point numbers in
1002 environments that don't support IEEE 754.
1003
1004 See @ref expAndMantissa for decoded representation.
1005 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001006static void QCBOREncode_AddTBigFloat(QCBOREncodeContext *pCtx,
1007 uint8_t uTagRequirement,
1008 int64_t nMantissa,
1009 int64_t nBase2Exponent);
1010
1011static void QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pCtx,
1012 const char *szLabel,
1013 uint8_t uTagRequirement,
1014 int64_t nMantissa,
1015 int64_t nBase2Exponent);
1016
1017static void QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pCtx,
1018 int64_t nLabel,
1019 uint8_t uTagRequirement,
1020 int64_t nMantissa,
1021 int64_t nBase2Exponent);
1022
1023
Michael Eckel5c531332020-03-02 01:35:30 +01001024static void QCBOREncode_AddBigFloat(QCBOREncodeContext *pCtx,
1025 int64_t nMantissa,
1026 int64_t nBase2Exponent);
1027
1028static void QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pCtx,
1029 const char *szLabel,
1030 int64_t nMantissa,
1031 int64_t nBase2Exponent);
1032
1033static void QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pCtx,
1034 int64_t nLabel,
1035 int64_t nMantissa,
1036 int64_t nBase2Exponent);
1037
Michael Eckel5c531332020-03-02 01:35:30 +01001038/**
1039 @brief Add a big floating-point number with a big number mantissa to
1040 the encoded output.
1041
1042 @param[in] pCtx The encoding context to add the bigfloat to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001043 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001044 @param[in] Mantissa The mantissa.
1045 @param[in] bIsNegative false if mantissa is positive, true if negative.
1046 @param[in] nBase2Exponent The exponent.
1047
1048 This is the same as QCBOREncode_AddBigFloat() except the mantissa is
1049 a big number (See QCBOREncode_AddPositiveBignum()) allowing for
1050 arbitrary precision.
1051
1052 See @ref expAndMantissa for decoded representation.
1053 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001054static void QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pCtx,
1055 uint8_t uTagRequirement,
1056 UsefulBufC Mantissa,
1057 bool bIsNegative,
1058 int64_t nBase2Exponent);
1059
1060static void QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pCtx,
1061 const char *szLabel,
1062 uint8_t uTagRequirement,
1063 UsefulBufC Mantissa,
1064 bool bIsNegative,
1065 int64_t nBase2Exponent);
1066
1067static void QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
1068 int64_t nLabel,
1069 uint8_t uTagRequirement,
1070 UsefulBufC Mantissa,
1071 bool bIsNegative,
1072 int64_t nBase2Exponent);
1073
1074
Michael Eckel5c531332020-03-02 01:35:30 +01001075static void QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pCtx,
1076 UsefulBufC Mantissa,
1077 bool bIsNegative,
1078 int64_t nBase2Exponent);
1079
1080static void QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pCtx,
1081 const char *szLabel,
1082 UsefulBufC Mantissa,
1083 bool bIsNegative,
1084 int64_t nBase2Exponent);
1085
1086static void QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pCtx,
1087 int64_t nLabel,
1088 UsefulBufC Mantissa,
1089 bool bIsNegative,
1090 int64_t nBase2Exponent);
1091#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
1092
1093
1094/**
1095 @brief Add a text URI to the encoded output.
1096
1097 @param[in] pCtx The encoding context to add the URI to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001098 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001099 @param[in] URI Pointer and length of the URI.
1100
1101 The format of URI must be per [RFC 3986]
1102 (https://tools.ietf.org/html/rfc3986).
1103
1104 It is output as CBOR major type 3, a text string, with tag @ref
1105 CBOR_TAG_URI indicating the text string is a URI.
1106
1107 A URI in a NULL-terminated string, @c szURI, can be easily added with
1108 this code:
1109
1110 QCBOREncode_AddURI(pCtx, UsefulBuf_FromSZ(szURI));
1111 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001112static void QCBOREncode_AddTURI(QCBOREncodeContext *pCtx,
1113 uint8_t uTagRequirement,
1114 UsefulBufC URI);
1115
1116static void QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pCtx,
1117 const char *szLabel,
1118 uint8_t uTagRequirement,
1119 UsefulBufC URI);
1120
1121static void QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pCtx,
1122 int64_t nLabel,
1123 uint8_t uTagRequirement,
1124 UsefulBufC URI);
1125
1126
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001127static void QCBOREncode_AddURI(QCBOREncodeContext *pCtx,
1128 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001129
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001130static void QCBOREncode_AddURIToMap(QCBOREncodeContext *pCtx,
1131 const char *szLabel,
1132 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001133
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001134static void QCBOREncode_AddURIToMapN(QCBOREncodeContext *pCtx,
1135 int64_t nLabel,
1136 UsefulBufC URI);
Michael Eckel5c531332020-03-02 01:35:30 +01001137
1138
1139/**
1140 @brief Add Base64-encoded text to encoded output.
1141
1142 @param[in] pCtx The encoding context to add the base-64 text to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001143 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001144 @param[in] B64Text Pointer and length of the base-64 encoded text.
1145
1146 The text content is Base64 encoded data per [RFC 4648]
1147 (https://tools.ietf.org/html/rfc4648).
1148
1149 It is output as CBOR major type 3, a text string, with tag @ref
1150 CBOR_TAG_B64 indicating the text string is Base64 encoded.
1151 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001152static void QCBOREncode_AddTB64Text(QCBOREncodeContext *pCtx,
1153 uint8_t uTagRequirement,
1154 UsefulBufC B64Text);
1155
1156static void QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pCtx,
1157 const char *szLabel,
1158 uint8_t uTagRequirement,
1159 UsefulBufC B64Text);
1160
1161static void QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pCtx,
1162 int64_t nLabel,
1163 uint8_t uTagRequirement,
1164 UsefulBufC B64Text);
1165
1166
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001167static void QCBOREncode_AddB64Text(QCBOREncodeContext *pCtx,
1168 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001169
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001170static void QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pCtx,
1171 const char *szLabel,
1172 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001173
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001174static void QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pCtx,
1175 int64_t nLabel,
1176 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001177
1178
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001179
Michael Eckel5c531332020-03-02 01:35:30 +01001180/**
1181 @brief Add base64url encoded data to encoded output.
1182
1183 @param[in] pCtx The encoding context to add the base64url to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001184 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001185 @param[in] B64Text Pointer and length of the base64url encoded text.
1186
1187 The text content is base64URL encoded text as per [RFC 4648]
1188 (https://tools.ietf.org/html/rfc4648).
1189
1190 It is output as CBOR major type 3, a text string, with tag @ref
1191 CBOR_TAG_B64URL indicating the text string is a Base64url encoded.
1192 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001193static void QCBOREncode_AddTB64URLText(QCBOREncodeContext *pCtx,
1194 uint8_t uTagRequirement,
1195 UsefulBufC B64Text);
1196
1197static void QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pCtx,
1198 const char *szLabel,
1199 uint8_t uTagRequirement,
1200 UsefulBufC B64Text);
1201
1202static void QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pCtx,
1203 int64_t nLabel,
1204 uint8_t uTagRequirement,
1205 UsefulBufC B64Text);
1206
1207
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001208static void QCBOREncode_AddB64URLText(QCBOREncodeContext *pCtx,
1209 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001210
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001211static void QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pCtx,
1212 const char *szLabel,
1213 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001214
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001215static void QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pCtx,
1216 int64_t nLabel,
1217 UsefulBufC B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01001218
1219
1220/**
1221 @brief Add Perl Compatible Regular Expression.
1222
1223 @param[in] pCtx The encoding context to add the regular expression to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001224 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001225 @param[in] Regex Pointer and length of the regular expression.
1226
1227 The text content is Perl Compatible Regular
1228 Expressions (PCRE) / JavaScript syntax [ECMA262].
1229
1230 It is output as CBOR major type 3, a text string, with tag @ref
1231 CBOR_TAG_REGEX indicating the text string is a regular expression.
1232 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001233static void QCBOREncode_AddTRegex(QCBOREncodeContext *pCtx,
1234 uint8_t uTagRequirement,
1235 UsefulBufC Regex);
1236
1237static void QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pCtx,
1238 const char *szLabel,
1239 uint8_t uTagRequirement,
1240 UsefulBufC Regex);
1241
1242static void QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pCtx,
1243 int64_t nLabel,
1244 uint8_t uTagRequirement,
1245 UsefulBufC Regex);
1246
1247
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001248static void QCBOREncode_AddRegex(QCBOREncodeContext *pCtx,
1249 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001250
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001251static void QCBOREncode_AddRegexToMap(QCBOREncodeContext *pCtx,
1252 const char *szLabel,
1253 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001254
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001255static void QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pCtx,
1256 int64_t nLabel,
1257 UsefulBufC Regex);
Michael Eckel5c531332020-03-02 01:35:30 +01001258
1259
1260/**
1261 @brief MIME encoded text to the encoded output.
1262
1263 @param[in] pCtx The encoding context to add the MIME data to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001264 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001265 @param[in] MIMEData Pointer and length of the MIME Data.
Michael Eckel5c531332020-03-02 01:35:30 +01001266
1267 The text content is in MIME format per [RFC 2045]
1268 (https://tools.ietf.org/html/rfc2045) including the headers. Note
1269 that this only supports text-format MIME. Binary MIME is not
1270 supported.
1271
1272 It is output as CBOR major type 3, a text string, with tag
1273 @ref CBOR_TAG_MIME indicating the text string is MIME data.
1274 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001275static void QCBOREncode_AddTMIMEData(QCBOREncodeContext *pCtx,
1276 uint8_t uTagRequirement,
1277 UsefulBufC MIMEData);
1278
1279static void QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pCtx,
1280 const char *szLabel,
1281 uint8_t uTagRequirement,
1282 UsefulBufC MIMEData);
1283
1284static void QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pCtx,
1285 int64_t nLabel,
1286 uint8_t uTagRequirement,
1287 UsefulBufC MIMEData);
1288
1289
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001290static void QCBOREncode_AddMIMEData(QCBOREncodeContext *pCtx,
1291 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001292
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001293static void QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pCtx,
1294 const char *szLabel,
1295 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001296
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001297static void QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pCtx,
1298 int64_t nLabel,
1299 UsefulBufC MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01001300
1301
1302/**
1303 @brief Add an RFC 3339 date string
1304
1305 @param[in] pCtx The encoding context to add the date to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001306 @param[in] uTagRequirement Either @ref QCBOR_ENCODE_AS_TAG or @ref QCBOR_ENCODE_AS_BORROWED.
Michael Eckel5c531332020-03-02 01:35:30 +01001307 @param[in] szDate Null-terminated string with date to add.
1308
1309 The string szDate should be in the form of [RFC 3339]
1310 (https://tools.ietf.org/html/rfc3339) as defined by section 3.3 in
1311 [RFC 4287] (https://tools.ietf.org/html/rfc4287). This is as
1312 described in section 2.4.1 in [RFC 7049]
1313 (https://tools.ietf.org/html/rfc7049).
1314
1315 Note that this function doesn't validate the format of the date string
1316 at all. If you add an incorrect format date string, the generated
1317 CBOR will be incorrect and the receiver may not be able to handle it.
1318
1319 Error handling is the same as QCBOREncode_AddInt64().
1320 */
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001321static void QCBOREncode_AddTDateString(QCBOREncodeContext *pCtx,
1322 uint8_t uTagRequirement,
1323 const char *szDate);
1324
1325static void QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pCtx,
1326 const char *szLabel,
1327 uint8_t uTagRequirement,
1328 const char *szDate);
1329
1330static void QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pCtx,
1331 int64_t nLabel,
1332 uint8_t uTagRequirement,
1333 const char *szDate);
1334
1335
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001336static void QCBOREncode_AddDateString(QCBOREncodeContext *pCtx,
1337 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001338
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001339static void QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pCtx,
1340 const char *szLabel,
1341 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001342
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001343static void QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pCtx,
1344 int64_t nLabel,
1345 const char *szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01001346
Michael Eckel5c531332020-03-02 01:35:30 +01001347/**
1348 @brief Add a standard Boolean.
1349
1350 @param[in] pCtx The encoding context to add the Boolean to.
1351 @param[in] b true or false from @c <stdbool.h>.
1352
1353 Adds a Boolean value as CBOR major type 7.
1354
1355 Error handling is the same as QCBOREncode_AddInt64().
1356 */
1357static void QCBOREncode_AddBool(QCBOREncodeContext *pCtx, bool b);
1358
1359static void QCBOREncode_AddBoolToMap(QCBOREncodeContext *pCtx, const char *szLabel, bool b);
1360
1361static void QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, bool b);
1362
1363
1364
1365/**
1366 @brief Add a NULL to the encoded output.
1367
1368 @param[in] pCtx The encoding context to add the NULL to.
1369
1370 Adds the NULL value as CBOR major type 7.
1371
1372 This NULL doesn't have any special meaning in CBOR such as a
1373 terminating value for a string or an empty value.
1374
1375 Error handling is the same as QCBOREncode_AddInt64().
1376 */
1377static void QCBOREncode_AddNULL(QCBOREncodeContext *pCtx);
1378
1379static void QCBOREncode_AddNULLToMap(QCBOREncodeContext *pCtx, const char *szLabel);
1380
1381static void QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1382
1383
1384/**
1385 @brief Add an "undef" to the encoded output.
1386
1387 @param[in] pCtx The encoding context to add the "undef" to.
1388
1389 Adds the undef value as CBOR major type 7.
1390
1391 Note that this value will not translate to JSON.
1392
1393 This Undef doesn't have any special meaning in CBOR such as a
1394 terminating value for a string or an empty value.
1395
1396 Error handling is the same as QCBOREncode_AddInt64().
1397 */
1398static void QCBOREncode_AddUndef(QCBOREncodeContext *pCtx);
1399
1400static void QCBOREncode_AddUndefToMap(QCBOREncodeContext *pCtx, const char *szLabel);
1401
1402static void QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1403
1404
1405/**
1406 @brief Indicates that the next items added are in an array.
1407
1408 @param[in] pCtx The encoding context to open the array in.
1409
1410 Arrays are the basic CBOR aggregate or structure type. Call this
1411 function to start or open an array. Then call the various @c
1412 QCBOREncode_AddXxx() functions to add the items that go into the
1413 array. Then call QCBOREncode_CloseArray() when all items have been
1414 added. The data items in the array can be of any type and can be of
1415 mixed types.
1416
1417 Nesting of arrays and maps is allowed and supported just by calling
1418 QCBOREncode_OpenArray() again before calling
1419 QCBOREncode_CloseArray(). While CBOR has no limit on nesting, this
1420 implementation does in order to keep it smaller and simpler. The
1421 limit is @ref QCBOR_MAX_ARRAY_NESTING. This is the max number of
1422 times this can be called without calling
1423 QCBOREncode_CloseArray(). QCBOREncode_Finish() will return @ref
1424 QCBOR_ERR_ARRAY_NESTING_TOO_DEEP when it is called as this function
1425 just sets an error state and returns no value when this occurs.
1426
1427 If you try to add more than @ref QCBOR_MAX_ITEMS_IN_ARRAY items to a
1428 single array or map, @ref QCBOR_ERR_ARRAY_TOO_LONG will be returned
1429 when QCBOREncode_Finish() is called.
1430
1431 An array itself must have a label if it is being added to a map.
1432 Note that array elements do not have labels (but map elements do).
1433
1434 An array itself may be tagged by calling QCBOREncode_AddTag() before this call.
1435 */
1436static void QCBOREncode_OpenArray(QCBOREncodeContext *pCtx);
1437
1438static void QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pCtx, const char *szLabel);
1439
1440static void QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1441
1442
1443/**
1444 @brief Close an open array.
1445
1446 @param[in] pCtx The encoding context to close the array in.
1447
1448 The closes an array opened by QCBOREncode_OpenArray(). It reduces
1449 nesting level by one. All arrays (and maps) must be closed before
1450 calling QCBOREncode_Finish().
1451
1452 When an error occurs as a result of this call, the encoder records
1453 the error and enters the error state. The error will be returned when
1454 QCBOREncode_Finish() is called.
1455
1456 If this has been called more times than QCBOREncode_OpenArray(), then
1457 @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when QCBOREncode_Finish()
1458 is called.
1459
1460 If this is called and it is not an array that is currently open, @ref
1461 QCBOR_ERR_CLOSE_MISMATCH will be returned when QCBOREncode_Finish()
1462 is called.
1463 */
1464static void QCBOREncode_CloseArray(QCBOREncodeContext *pCtx);
1465
1466
1467/**
1468 @brief Indicates that the next items added are in a map.
1469
1470 @param[in] pCtx The encoding context to open the map in.
1471
1472 See QCBOREncode_OpenArray() for more information, particularly error
1473 handling.
1474
1475 CBOR maps are an aggregate type where each item in the map consists
1476 of a label and a value. They are similar to JSON objects.
1477
1478 The value can be any CBOR type including another map.
1479
1480 The label can also be any CBOR type, but in practice they are
1481 typically, integers as this gives the most compact output. They might
1482 also be text strings which gives readability and translation to JSON.
1483
1484 Every @c QCBOREncode_AddXxx() call has one version that ends with @c
1485 InMap for adding items to maps with string labels and one that ends
1486 with @c InMapN that is for adding with integer labels.
1487
1488 RFC 7049 uses the term "key" instead of "label".
1489
1490 If you wish to use map labels that are neither integer labels nor
1491 text strings, then just call the QCBOREncode_AddXxx() function
1492 explicitly to add the label. Then call it again to add the value.
1493
1494 See the [RFC 7049] (https://tools.ietf.org/html/rfc7049) for a lot
1495 more information on creating maps.
1496 */
1497static void QCBOREncode_OpenMap(QCBOREncodeContext *pCtx);
1498
1499static void QCBOREncode_OpenMapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
1500
1501static void QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1502
1503
1504
1505/**
1506 @brief Close an open map.
1507
1508 @param[in] pCtx The encoding context to close the map in .
1509
1510 This closes a map opened by QCBOREncode_OpenMap(). It reduces nesting
1511 level by one.
1512
1513 When an error occurs as a result of this call, the encoder records
1514 the error and enters the error state. The error will be returned when
1515 QCBOREncode_Finish() is called.
1516
1517 If this has been called more times than QCBOREncode_OpenMap(),
1518 then @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when
1519 QCBOREncode_Finish() is called.
1520
1521 If this is called and it is not a map that is currently open, @ref
1522 QCBOR_ERR_CLOSE_MISMATCH will be returned when QCBOREncode_Finish()
1523 is called.
1524 */
1525static void QCBOREncode_CloseMap(QCBOREncodeContext *pCtx);
1526
1527
1528/**
1529 @brief Indicate start of encoded CBOR to be wrapped in a bstr.
1530
1531 @param[in] pCtx The encoding context to open the bstr-wrapped CBOR in.
1532
1533 All added encoded items between this call and a call to
1534 QCBOREncode_CloseBstrWrap2() will be wrapped in a bstr. They will
1535 appear in the final output as a byte string. That byte string will
1536 contain encoded CBOR. This increases nesting level by one.
1537
1538 The typical use case is for encoded CBOR that is to be
1539 cryptographically hashed, as part of a [RFC 8152, COSE]
1540 (https://tools.ietf.org/html/rfc8152) implementation.
1541
1542 Using QCBOREncode_BstrWrap() and QCBOREncode_CloseBstrWrap2() avoids
1543 having to encode the items first in one buffer (e.g., the COSE
1544 payload) and then add that buffer as a bstr to another encoding
1545 (e.g. the COSE to-be-signed bytes, the @c Sig_structure) potentially
1546 halving the memory needed.
1547
1548 RFC 7049 states the purpose of this wrapping is to prevent code
1549 relaying the signed data but not verifying it from tampering with the
1550 signed data thus making the signature unverifiable. It is also quite
1551 beneficial for the signature verification code. Standard CBOR
1552 decoders usually do not give access to partially decoded CBOR as
1553 would be needed to check the signature of some CBOR. With this
1554 wrapping, standard CBOR decoders can be used to get to all the data
1555 needed for a signature verification.
1556 */
1557static void QCBOREncode_BstrWrap(QCBOREncodeContext *pCtx);
1558
1559static void QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pCtx, const char *szLabel);
1560
1561static void QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel);
1562
1563
1564/**
1565 @brief Close a wrapping bstr.
1566
1567 @param[in] pCtx The encoding context to close of bstr wrapping in.
1568 @param[in] bIncludeCBORHead Include the encoded CBOR head of the bstr
1569 as well as the bytes in @c pWrappedCBOR.
1570 @param[out] pWrappedCBOR A @ref UsefulBufC containing wrapped bytes.
1571
1572 The closes a wrapping bstr opened by QCBOREncode_BstrWrap(). It reduces
1573 nesting level by one.
1574
1575 A pointer and length of the enclosed encoded CBOR is returned in @c
1576 *pWrappedCBOR if it is not @c NULL. The main purpose of this is so
1577 this data can be hashed (e.g., with SHA-256) as part of a [RFC 8152,
1578 COSE] (https://tools.ietf.org/html/rfc8152)
1579 implementation. **WARNING**, this pointer and length should be used
1580 right away before any other calls to @c QCBOREncode_CloseXxx() as
1581 they will move data around and the pointer and length will no longer
1582 be to the correct encoded CBOR.
1583
1584 When an error occurs as a result of this call, the encoder records
1585 the error and enters the error state. The error will be returned when
1586 QCBOREncode_Finish() is called.
1587
1588 If this has been called more times than QCBOREncode_BstrWrap(), then
1589 @ref QCBOR_ERR_TOO_MANY_CLOSES will be returned when
1590 QCBOREncode_Finish() is called.
1591
1592 If this is called and it is not a wrapping bstr that is currently
1593 open, @ref QCBOR_ERR_CLOSE_MISMATCH will be returned when
1594 QCBOREncode_Finish() is called.
1595
1596 QCBOREncode_CloseBstrWrap() is a deprecated version of this function
1597 that is equivalent to the call with @c bIncludeCBORHead @c true.
1598 */
1599void QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *pCtx, bool bIncludeCBORHead, UsefulBufC *pWrappedCBOR);
1600
1601static void QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pCtx, UsefulBufC *pWrappedCBOR);
1602
1603
1604/**
1605 @brief Add some already-encoded CBOR bytes.
1606
1607 @param[in] pCtx The encoding context to add the already-encode CBOR to.
1608 @param[in] Encoded The already-encoded CBOR to add to the context.
1609
1610 The encoded CBOR being added must be fully conforming CBOR. It must
1611 be complete with no arrays or maps that are incomplete. While this
1612 encoder doesn't ever produce indefinite lengths, it is OK for the
1613 raw CBOR added here to have indefinite lengths.
1614
1615 The raw CBOR added here is not checked in anyway. If it is not
1616 conforming or has open arrays or such, the final encoded CBOR
1617 will probably be wrong or not what was intended.
1618
1619 If the encoded CBOR being added here contains multiple items, they
1620 must be enclosed in a map or array. At the top level the raw
1621 CBOR must be a single data item.
1622 */
1623static void QCBOREncode_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded);
1624
1625static void QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Encoded);
1626
1627static void QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Encoded);
1628
1629
1630/**
1631 @brief Get the encoded result.
1632
1633 @param[in] pCtx The context to finish encoding with.
1634 @param[out] pEncodedCBOR Pointer and length of encoded CBOR.
1635
1636 @retval QCBOR_ERR_TOO_MANY_CLOSES Nesting error
1637
1638 @retval QCBOR_ERR_CLOSE_MISMATCH Nesting error
1639
1640 @retval QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN Nesting error
1641
1642 @retval QCBOR_ERR_BUFFER_TOO_LARGE Encoded output buffer size
1643
1644 @retval QCBOR_ERR_BUFFER_TOO_SMALL Encoded output buffer size
1645
1646 @retval QCBOR_ERR_ARRAY_NESTING_TOO_DEEP Implementation limit
1647
1648 @retval QCBOR_ERR_ARRAY_TOO_LONG Implementation limit
1649
1650 If this returns success @ref QCBOR_SUCCESS the encoding was a success
1651 and the return length is correct and complete.
1652
1653 If no buffer was passed to QCBOREncode_Init(), then only the length
1654 was computed. If a buffer was passed, then the encoded CBOR is in the
1655 buffer.
1656
1657 Encoding errors primarily manifest here as most other encoding function
1658 do no return an error. They just set the error state in the encode
1659 context after which no encoding function does anything.
1660
1661 Three types of errors manifest here. The first type are nesting
1662 errors where the number of @c QCBOREncode_OpenXxx() calls do not
1663 match the number @c QCBOREncode_CloseXxx() calls. The solution is to
1664 fix the calling code.
1665
1666 The second type of error is because the buffer given is either too
1667 small or too large. The remedy is to give a correctly sized buffer.
1668
1669 The third type are due to limits in this implementation. @ref
1670 QCBOR_ERR_ARRAY_NESTING_TOO_DEEP can be worked around by encoding the
1671 CBOR in two (or more) phases and adding the CBOR from the first phase
1672 to the second with @c QCBOREncode_AddEncoded().
1673
1674 If an error is returned, the buffer may have partially encoded
1675 incorrect CBOR in it and it should not be used. Likewise, the length
1676 may be incorrect and should not be used.
1677
1678 Note that the error could have occurred in one of the many @c
1679 QCBOREncode_AddXxx() calls long before QCBOREncode_Finish() was
1680 called. This error handling reduces the CBOR implementation size but
1681 makes debugging harder.
1682
1683 This may be called multiple times. It will always return the same. It
1684 can also be interleaved with calls to QCBOREncode_FinishGetSize().
1685
1686 QCBOREncode_GetErrorState() can be called to get the current
1687 error state and abort encoding early as an optimization, but is
1688 is never required.
1689 */
1690QCBORError QCBOREncode_Finish(QCBOREncodeContext *pCtx, UsefulBufC *pEncodedCBOR);
1691
1692
1693/**
1694 @brief Get the encoded CBOR and error status.
1695
1696 @param[in] pCtx The context to finish encoding with.
1697 @param[out] uEncodedLen The length of the encoded or potentially
1698 encoded CBOR in bytes.
1699
1700 @return The same errors as QCBOREncode_Finish().
1701
1702 This functions the same as QCBOREncode_Finish(), but only returns the
1703 size of the encoded output.
1704 */
1705QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *pCtx, size_t *uEncodedLen);
1706
1707
1708/**
1709 @brief Indicate whether output buffer is NULL or not.
1710
1711 @param[in] pCtx The encoding context.
1712
1713 @return 1 if the output buffer is @c NULL.
1714
1715 Sometimes a @c NULL input buffer is given to QCBOREncode_Init() so
1716 that the size of the generated CBOR can be calculated without
1717 allocating a buffer for it. This returns 1 when the output buffer is
1718 NULL and 0 when it is not.
1719*/
1720static int QCBOREncode_IsBufferNULL(QCBOREncodeContext *pCtx);
1721
1722 /**
1723 @brief Get the encoding error state.
1724
1725 @param[in] pCtx The encoding context.
1726
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07001727 @return One of @ref QCBORError. See return values from
Michael Eckel5c531332020-03-02 01:35:30 +01001728 QCBOREncode_Finish()
1729
1730 Normally encoding errors need only be handled at the end of encoding
1731 when QCBOREncode_Finish() is called. This can be called to get the
1732 error result before finish should there be a need to halt encoding
1733 before QCBOREncode_Finish() is called.
1734*/
1735static QCBORError QCBOREncode_GetErrorState(QCBOREncodeContext *pCtx);
1736
1737
1738/**
1739 Encode the "head" of a CBOR data item.
1740
1741 @param buffer Buffer to output the encoded head to; must be
1742 @ref QCBOR_HEAD_BUFFER_SIZE bytes in size.
1743 @param uMajorType One of CBOR_MAJOR_TYPE_XX.
1744 @param uMinLen The minimum number of bytes to encode uNumber. Almost always
1745 this is 0 to use preferred minimal encoding. If this is 4,
1746 then even the values 0xffff and smaller will be encoded
1747 as in 4 bytes. This is used primarily when encoding a
1748 float or double put into uNumber as the leading zero bytes
1749 for them must be encoded.
1750 @param uNumber The numeric argument part of the CBOR head.
1751 @return Pointer and length of the encoded head or
1752 @NULLUsefulBufC if the output buffer is too small.
1753
1754 Callers to need to call this for normal CBOR encoding. Note that it doesn't even
1755 take a @ref QCBOREncodeContext argument.
1756
1757 This encodes the major type and argument part of a data item. The
1758 argument is an integer that is usually either the value or the length
1759 of the data item.
1760
1761 This is exposed in the public interface to allow hashing of some CBOR
1762 data types, bstr in particular, a chunk at a time so the full CBOR
1763 doesn't have to be encoded in a contiguous buffer.
1764
1765 For example, if you have a 100,000 byte binary blob in a buffer that
1766 needs to be a bstr encoded and then hashed. You could allocate a
1767 100,010 byte buffer and encode it normally. Alternatively, you can
1768 encode the head in a 10 byte buffer with this function, hash that and
1769 then hash the 100,000 bytes using the same hash context.
1770
1771 See also QCBOREncode_AddBytesLenOnly();
1772 */
1773UsefulBufC QCBOREncode_EncodeHead(UsefulBuf buffer,
1774 uint8_t uMajorType,
1775 uint8_t uMinLen,
1776 uint64_t uNumber);
1777
1778
Michael Eckel5c531332020-03-02 01:35:30 +01001779
1780
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001781/* =========================================================================
1782 BEGINNING OF PRIVATE INLINE IMPLEMENTATION
1783 ========================================================================= */
Michael Eckel5c531332020-03-02 01:35:30 +01001784
1785/**
1786 @brief Semi-private method to add a buffer full of bytes to encoded output
1787
1788 @param[in] pCtx The encoding context to add the integer to.
1789 @param[in] uMajorType The CBOR major type of the bytes.
1790 @param[in] Bytes The bytes to add.
1791
1792 Use QCBOREncode_AddText() or QCBOREncode_AddBytes() or
1793 QCBOREncode_AddEncoded() instead. They are inline functions that call
1794 this and supply the correct major type. This function is public to
1795 make the inline functions work to keep the overall code size down and
1796 because the C language has no way to make it private.
1797
1798 If this is called the major type should be @c
1799 CBOR_MAJOR_TYPE_TEXT_STRING, @c CBOR_MAJOR_TYPE_BYTE_STRING or @c
1800 CBOR_MAJOR_NONE_TYPE_RAW. The last one is special for adding
1801 already-encoded CBOR.
1802 */
1803void QCBOREncode_AddBuffer(QCBOREncodeContext *pCtx, uint8_t uMajorType, UsefulBufC Bytes);
1804
1805
1806/**
1807 @brief Semi-private method to open a map, array or bstr-wrapped CBOR
1808
1809 @param[in] pCtx The context to add to.
1810 @param[in] uMajorType The major CBOR type to close
1811
1812 Call QCBOREncode_OpenArray(), QCBOREncode_OpenMap() or
1813 QCBOREncode_BstrWrap() instead of this.
1814 */
1815void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *pCtx, uint8_t uMajorType);
1816
1817
1818/**
1819 @brief Semi-private method to open a map, array with indefinite length
1820
1821 @param[in] pCtx The context to add to.
1822 @param[in] uMajorType The major CBOR type to close
1823
1824 Call QCBOREncode_OpenArrayIndefiniteLength() or
1825 QCBOREncode_OpenMapIndefiniteLength() instead of this.
1826 */
1827void QCBOREncode_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx, uint8_t uMajorType);
1828
1829
1830/**
1831 @brief Semi-private method to close a map, array or bstr wrapped CBOR
1832
1833 @param[in] pCtx The context to add to.
1834 @param[in] uMajorType The major CBOR type to close.
1835
1836 Call QCBOREncode_CloseArray() or QCBOREncode_CloseMap() instead of this.
1837 */
1838void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *pCtx, uint8_t uMajorType);
1839
1840
1841/**
1842 @brief Semi-private method to close a map, array with indefinite length
1843
1844 @param[in] pCtx The context to add to.
1845 @param[in] uMajorType The major CBOR type to close.
1846
1847 Call QCBOREncode_CloseArrayIndefiniteLength() or
1848 QCBOREncode_CloseMapIndefiniteLength() instead of this.
1849 */
1850void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pCtx,
1851 uint8_t uMajorType);
1852
1853
1854/**
1855 @brief Semi-private method to add simple types.
1856
1857 @param[in] pCtx The encoding context to add the simple value to.
1858 @param[in] uMinLen Minimum encoding size for uNum. Usually 0.
1859 @param[in] uNum One of CBOR_SIMPLEV_FALSE through _UNDEF or other.
1860
1861 This is used to add simple types like true and false.
1862
1863 Call QCBOREncode_AddBool(), QCBOREncode_AddNULL(),
1864 QCBOREncode_AddUndef() instead of this.
1865
1866 This function can add simple values that are not defined by CBOR
1867 yet. This expansion point in CBOR should not be used unless they are
1868 standardized.
1869
1870 Error handling is the same as QCBOREncode_AddInt64().
1871 */
1872void QCBOREncode_AddType7(QCBOREncodeContext *pCtx, uint8_t uMinLen, uint64_t uNum);
1873
1874
1875/**
1876 @brief Semi-private method to add bigfloats and decimal fractions.
1877
1878 @param[in] pCtx The encoding context to add the value to.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001879 @param[in] uTag The type 6 tag indicating what this is to be.
Michael Eckel5c531332020-03-02 01:35:30 +01001880 @param[in] BigNumMantissa Is @ref NULLUsefulBufC if mantissa is an
1881 @c int64_t or the actual big number mantissa
1882 if not.
1883 @param[in] nMantissa The @c int64_t mantissa if it is not a big number.
1884 @param[in] nExponent The exponent.
1885
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001886 This outputs either the @ref CBOR_TAG_DECIMAL_FRACTION or @ref
1887 CBOR_TAG_BIGFLOAT tag. if @c uTag is @ref CBOR_TAG_INVALID64, then
1888 this outputs the "borrowed" content format.
Michael Eckel5c531332020-03-02 01:35:30 +01001889
Laurence Lundblade45d5e482020-09-15 21:15:15 -07001890 The tag content output by this is an array with two members, the
1891 exponent and then the mantissa. The mantissa can be either a big
1892 number or an @c int64_t.
1893
1894 This implementation cannot output an exponent further from 0 than
1895 INT64_MAX.
1896
1897 To output a mantissa that is bewteen INT64_MAX and UINT64_MAX from 0,
1898 it must be as a big number.
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001899
Michael Eckel5c531332020-03-02 01:35:30 +01001900 Typically, QCBOREncode_AddDecimalFraction(), QCBOREncode_AddBigFloat(),
1901 QCBOREncode_AddDecimalFractionBigNum() or QCBOREncode_AddBigFloatBigNum()
1902 is called instead of this.
1903 */
1904void QCBOREncode_AddExponentAndMantissa(QCBOREncodeContext *pCtx,
1905 uint64_t uTag,
1906 UsefulBufC BigNumMantissa,
1907 bool bBigNumIsNegative,
1908 int64_t nMantissa,
1909 int64_t nExponent);
1910
1911/**
1912 @brief Semi-private method to add only the type and length of a byte string.
1913
1914 @param[in] pCtx The context to initialize.
1915 @param[in] Bytes Pointer and length of the input data.
1916
1917 This is the same as QCBOREncode_AddBytes() except it only adds the
1918 CBOR encoding for the type and the length. It doesn't actually add
1919 the bytes. You can't actually produce correct CBOR with this and the
1920 rest of this API. It is only used for a special case where
1921 the valid CBOR is created manually by putting this type and length in
1922 and then adding the actual bytes. In particular, when only a hash of
1923 the encoded CBOR is needed, where the type and header are hashed
1924 separately and then the bytes is hashed. This makes it possible to
1925 implement COSE Sign1 with only one copy of the payload in the output
1926 buffer, rather than two, roughly cutting memory use in half.
1927
1928 This is only used for this odd case, but this is a supported
1929 tested function.
1930
1931 See also QCBOREncode_EncodeHead().
1932*/
1933static inline void QCBOREncode_AddBytesLenOnly(QCBOREncodeContext *pCtx, UsefulBufC Bytes);
1934
1935static inline void QCBOREncode_AddBytesLenOnlyToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Bytes);
1936
1937static inline void QCBOREncode_AddBytesLenOnlyToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Bytes);
1938
1939
1940
1941
1942
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001943static inline void
1944QCBOREncode_AddInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, int64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01001945{
1946 // Use _AddBuffer() because _AddSZString() is defined below, not above
1947 QCBOREncode_AddBuffer(pCtx, CBOR_MAJOR_TYPE_TEXT_STRING, UsefulBuf_FromSZ(szLabel));
1948 QCBOREncode_AddInt64(pCtx, uNum);
1949}
1950
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001951static inline void
1952QCBOREncode_AddInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, int64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01001953{
1954 QCBOREncode_AddInt64(pCtx, nLabel);
1955 QCBOREncode_AddInt64(pCtx, uNum);
1956}
1957
1958
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001959static inline void
1960QCBOREncode_AddUInt64ToMap(QCBOREncodeContext *pCtx, const char *szLabel, uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01001961{
1962 // Use _AddBuffer() because _AddSZString() is defined below, not above
1963 QCBOREncode_AddBuffer(pCtx, CBOR_MAJOR_TYPE_TEXT_STRING, UsefulBuf_FromSZ(szLabel));
1964 QCBOREncode_AddUInt64(pCtx, uNum);
1965}
1966
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001967static inline void
1968QCBOREncode_AddUInt64ToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01001969{
1970 QCBOREncode_AddInt64(pCtx, nLabel);
1971 QCBOREncode_AddUInt64(pCtx, uNum);
1972}
1973
1974
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001975static inline void
1976QCBOREncode_AddText(QCBOREncodeContext *pCtx, UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01001977{
1978 QCBOREncode_AddBuffer(pCtx, CBOR_MAJOR_TYPE_TEXT_STRING, Text);
1979}
1980
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001981static inline void
1982QCBOREncode_AddTextToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01001983{
1984 // Use _AddBuffer() because _AddSZString() is defined below, not above
1985 QCBOREncode_AddText(pCtx, UsefulBuf_FromSZ(szLabel));
1986 QCBOREncode_AddText(pCtx, Text);
1987}
1988
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001989static inline void
1990QCBOREncode_AddTextToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Text)
Michael Eckel5c531332020-03-02 01:35:30 +01001991{
1992 QCBOREncode_AddInt64(pCtx, nLabel);
1993 QCBOREncode_AddText(pCtx, Text);
1994}
1995
1996
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07001997inline static void
1998QCBOREncode_AddSZString(QCBOREncodeContext *pCtx, const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01001999{
2000 QCBOREncode_AddText(pCtx, UsefulBuf_FromSZ(szString));
2001}
2002
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002003static inline void
2004QCBOREncode_AddSZStringToMap(QCBOREncodeContext *pCtx, const char *szLabel, const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002005{
2006 QCBOREncode_AddSZString(pCtx, szLabel);
2007 QCBOREncode_AddSZString(pCtx, szString);
2008}
2009
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002010static inline void
2011QCBOREncode_AddSZStringToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, const char *szString)
Michael Eckel5c531332020-03-02 01:35:30 +01002012{
2013 QCBOREncode_AddInt64(pCtx, nLabel);
2014 QCBOREncode_AddSZString(pCtx, szString);
2015}
2016
2017
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002018static inline void
2019QCBOREncode_AddDoubleToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002020{
2021 QCBOREncode_AddSZString(pCtx, szLabel);
2022 QCBOREncode_AddDouble(pCtx, dNum);
2023}
2024
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002025static inline void
2026QCBOREncode_AddDoubleToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002027{
2028 QCBOREncode_AddInt64(pCtx, nLabel);
2029 QCBOREncode_AddDouble(pCtx, dNum);
2030}
2031
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002032static inline void
2033QCBOREncode_AddFloatToMap(QCBOREncodeContext *pCtx, const char *szLabel, float dNum)
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002034{
2035 QCBOREncode_AddSZString(pCtx, szLabel);
2036 QCBOREncode_AddFloat(pCtx, dNum);
2037}
2038
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002039static inline void
2040QCBOREncode_AddFloatToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float fNum)
Laurence Lundbladeb275cdc2020-07-12 12:34:38 -07002041{
2042 QCBOREncode_AddInt64(pCtx, nLabel);
2043 QCBOREncode_AddFloat(pCtx, fNum);
2044}
2045
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002046static inline void
2047QCBOREncode_AddDoubleNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, double dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002048{
2049 QCBOREncode_AddSZString(pCtx, szLabel);
2050 QCBOREncode_AddDoubleNoPreferred(pCtx, dNum);
2051}
2052
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002053static inline void
2054QCBOREncode_AddDoubleNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, double dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002055{
2056 QCBOREncode_AddInt64(pCtx, nLabel);
2057 QCBOREncode_AddDoubleNoPreferred(pCtx, dNum);
2058}
2059
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002060static inline void
2061QCBOREncode_AddFloatNoPreferredToMap(QCBOREncodeContext *pCtx, const char *szLabel, float dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002062{
2063 QCBOREncode_AddSZString(pCtx, szLabel);
2064 QCBOREncode_AddFloatNoPreferred(pCtx, dNum);
2065}
2066
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002067static inline void
2068QCBOREncode_AddFloatNoPreferredToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, float dNum)
Laurence Lundblade32f3e622020-07-13 20:35:11 -07002069{
2070 QCBOREncode_AddInt64(pCtx, nLabel);
2071 QCBOREncode_AddFloatNoPreferred(pCtx, dNum);
2072}
2073
Michael Eckel5c531332020-03-02 01:35:30 +01002074
Laurence Lundblade9b334962020-08-27 10:55:53 -07002075
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002076static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002077QCBOREncode_AddTDateEpoch(QCBOREncodeContext *pMe, uint8_t uTag, int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002078{
2079 if(uTag == QCBOR_ENCODE_AS_TAG) {
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002080 QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_EPOCH);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002081 }
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002082 QCBOREncode_AddInt64(pMe, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002083}
2084
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002085static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002086QCBOREncode_AddTDateEpochToMapSZ(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uTag, int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002087{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002088 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002089 QCBOREncode_AddTDateEpoch(pMe, uTag, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002090}
2091
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002092static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002093QCBOREncode_AddTDateEpochToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTag, int64_t nDate)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002094{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002095 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002096 QCBOREncode_AddTDateEpoch(pMe, uTag, nDate);
Laurence Lundblade9b334962020-08-27 10:55:53 -07002097}
2098
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002099static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002100QCBOREncode_AddDateEpoch(QCBOREncodeContext *pMe, int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002101{
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002102 QCBOREncode_AddTDateEpoch(pMe, QCBOR_ENCODE_AS_TAG, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002103}
2104
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002105static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002106QCBOREncode_AddDateEpochToMap(QCBOREncodeContext *pMe, const char *szLabel, int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002107{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002108 QCBOREncode_AddSZString(pMe, szLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002109 QCBOREncode_AddDateEpoch(pMe, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002110}
2111
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002112static inline void
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002113QCBOREncode_AddDateEpochToMapN(QCBOREncodeContext *pMe, int64_t nLabel, int64_t nDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002114{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002115 QCBOREncode_AddInt64(pMe, nLabel);
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002116 QCBOREncode_AddDateEpoch(pMe, nDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002117}
2118
2119
Laurence Lundblade9b334962020-08-27 10:55:53 -07002120
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002121static inline void
2122QCBOREncode_AddBytes(QCBOREncodeContext *pMe, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002123{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002124 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_TYPE_BYTE_STRING, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002125}
2126
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002127static inline void
2128QCBOREncode_AddBytesToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002129{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002130 QCBOREncode_AddSZString(pMe, szLabel);
2131 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002132}
2133
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002134static inline void
2135QCBOREncode_AddBytesToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002136{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002137 QCBOREncode_AddInt64(pMe, nLabel);
2138 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002139}
2140
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002141static inline void
2142QCBOREncode_AddBytesLenOnly(QCBOREncodeContext *pMe, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002143{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002144 QCBOREncode_AddBuffer(pMe, CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002145}
2146
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002147static inline void
2148QCBOREncode_AddBytesLenOnlyToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002149{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002150 QCBOREncode_AddSZString(pMe, szLabel);
2151 QCBOREncode_AddBytesLenOnly(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002152}
2153
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002154static inline void
2155QCBOREncode_AddBytesLenOnlyToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002156{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002157 QCBOREncode_AddInt64(pMe, nLabel);
2158 QCBOREncode_AddBytesLenOnly(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002159}
2160
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002161
2162static inline void
2163QCBOREncode_AddTBinaryUUID(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002164{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002165 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2166 QCBOREncode_AddTag(pMe, CBOR_TAG_BIN_UUID);
2167 }
2168 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002169}
2170
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002171static inline void
2172QCBOREncode_AddTBinaryUUIDToMapSZ(QCBOREncodeContext *pMe,
2173 const char *szLabel,
2174 uint8_t uTagRequirement,
2175 UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002176{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002177 QCBOREncode_AddSZString(pMe, szLabel);
2178 QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002179}
2180
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002181static inline void
2182QCBOREncode_AddTBinaryUUIDToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002183{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002184 QCBOREncode_AddInt64(pMe, nLabel);
2185 QCBOREncode_AddTBinaryUUID(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002186}
2187
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002188static inline void
2189QCBOREncode_AddBinaryUUID(QCBOREncodeContext *pMe, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002190{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002191 QCBOREncode_AddTBinaryUUID(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002192}
2193
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002194static inline void
2195QCBOREncode_AddBinaryUUIDToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002196{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002197 QCBOREncode_AddTBinaryUUIDToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002198}
2199
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002200static inline void
2201QCBOREncode_AddBinaryUUIDToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002202{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002203 QCBOREncode_AddTBinaryUUIDToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002204}
2205
2206
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002207static inline void
2208QCBOREncode_AddTPositiveBignum(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002209{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002210 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2211 QCBOREncode_AddTag(pMe, CBOR_TAG_POS_BIGNUM);
2212 }
2213 QCBOREncode_AddBytes(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002214}
2215
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002216static inline void
2217QCBOREncode_AddTPositiveBignumToMapSZ(QCBOREncodeContext *pMe,
2218 const char *szLabel,
2219 uint8_t uTagRequirement,
2220 UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002221{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002222 QCBOREncode_AddSZString(pMe, szLabel);
2223 QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002224}
2225
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002226static inline void
2227QCBOREncode_AddTPositiveBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002228{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002229 QCBOREncode_AddInt64(pMe, nLabel);
2230 QCBOREncode_AddTPositiveBignum(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002231}
2232
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002233static inline void
2234QCBOREncode_AddPositiveBignum(QCBOREncodeContext *pMe, UsefulBufC Bytes)
2235{
2236 QCBOREncode_AddTPositiveBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
2237}
2238
2239static inline void
2240QCBOREncode_AddPositiveBignumToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
2241{
2242 QCBOREncode_AddTPositiveBignumToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2243}
2244
2245static inline void
2246QCBOREncode_AddPositiveBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
2247{
2248 QCBOREncode_AddTPositiveBignumToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2249}
2250
2251
2252static inline void
2253QCBOREncode_AddTNegativeBignum(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
2254{
2255 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2256 QCBOREncode_AddTag(pMe, CBOR_TAG_NEG_BIGNUM);
2257 }
2258 QCBOREncode_AddBytes(pMe, Bytes);
2259}
2260
2261static inline void
2262QCBOREncode_AddTNegativeBignumToMapSZ(QCBOREncodeContext *pMe,
2263 const char *szLabel,
2264 uint8_t uTagRequirement,
2265 UsefulBufC Bytes)
2266{
2267 QCBOREncode_AddSZString(pMe, szLabel);
2268 QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
2269}
2270
2271static inline void
2272QCBOREncode_AddTNegativeBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
2273{
2274 QCBOREncode_AddInt64(pMe, nLabel);
2275 QCBOREncode_AddTNegativeBignum(pMe, uTagRequirement, Bytes);
2276}
2277
2278static inline void
2279QCBOREncode_AddNegativeBignum(QCBOREncodeContext *pMe, UsefulBufC Bytes)
2280{
2281 QCBOREncode_AddTNegativeBignum(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
2282}
2283
2284static inline void
2285QCBOREncode_AddNegativeBignumToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
2286{
2287 QCBOREncode_AddTNegativeBignumToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2288}
2289
2290static inline void
2291QCBOREncode_AddNegativeBignumToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
2292{
2293 QCBOREncode_AddTNegativeBignumToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2294}
2295
2296
Michael Eckel5c531332020-03-02 01:35:30 +01002297
2298#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
2299
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002300static inline void
2301QCBOREncode_AddTDecimalFraction(QCBOREncodeContext *pMe,
2302 uint8_t uTagRequirement,
2303 int64_t nMantissa,
2304 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002305{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002306 uint64_t uTag;
2307 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2308 uTag = CBOR_TAG_DECIMAL_FRACTION;
2309 } else {
2310 uTag = CBOR_TAG_INVALID64;
2311 }
2312 QCBOREncode_AddExponentAndMantissa(pMe,
2313 uTag,
Michael Eckel5c531332020-03-02 01:35:30 +01002314 NULLUsefulBufC,
2315 false,
2316 nMantissa,
2317 nBase10Exponent);
2318}
2319
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002320static inline void
2321QCBOREncode_AddTDecimalFractionToMapSZ(QCBOREncodeContext *pMe,
2322 const char *szLabel,
2323 uint8_t uTagRequirement,
2324 int64_t nMantissa,
2325 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002326{
2327 QCBOREncode_AddSZString(pMe, szLabel);
2328 QCBOREncode_AddTDecimalFraction(pMe, uTagRequirement, nMantissa, nBase10Exponent);
2329}
2330
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002331static inline void
2332QCBOREncode_AddTDecimalFractionToMapN(QCBOREncodeContext *pMe,
2333 int64_t nLabel,
2334 uint8_t uTagRequirement,
2335 int64_t nMantissa,
2336 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002337{
2338 QCBOREncode_AddInt64(pMe, nLabel);
2339 QCBOREncode_AddTDecimalFraction(pMe, uTagRequirement, nMantissa, nBase10Exponent);
2340}
2341
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002342static inline void
2343QCBOREncode_AddDecimalFraction(QCBOREncodeContext *pMe,
2344 int64_t nMantissa,
2345 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002346{
2347 QCBOREncode_AddTDecimalFraction(pMe, QCBOR_ENCODE_AS_TAG, nMantissa, nBase10Exponent);
2348}
2349
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002350static inline void
2351QCBOREncode_AddDecimalFractionToMap(QCBOREncodeContext *pMe,
2352 const char *szLabel,
2353 int64_t nMantissa,
2354 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002355{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002356 QCBOREncode_AddTDecimalFractionToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002357}
2358
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002359static inline void
2360QCBOREncode_AddDecimalFractionToMapN(QCBOREncodeContext *pMe,
2361 int64_t nLabel,
2362 int64_t nMantissa,
2363 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002364{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002365 QCBOREncode_AddTDecimalFractionToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002366}
2367
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002368
2369
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002370static inline void
2371QCBOREncode_AddTDecimalFractionBigNum(QCBOREncodeContext *pMe,
2372 uint8_t uTagRequirement,
2373 UsefulBufC Mantissa,
2374 bool bIsNegative,
2375 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002376{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002377 uint64_t uTag;
2378 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2379 uTag = CBOR_TAG_DECIMAL_FRACTION;
2380 } else {
2381 uTag = CBOR_TAG_INVALID64;
2382 }
2383 QCBOREncode_AddExponentAndMantissa(pMe,
2384 uTag,
Michael Eckel5c531332020-03-02 01:35:30 +01002385 Mantissa, bIsNegative,
2386 0,
2387 nBase10Exponent);
2388}
2389
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002390static inline void
2391QCBOREncode_AddTDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
2392 const char *szLabel,
2393 uint8_t uTagRequirement,
2394 UsefulBufC Mantissa,
2395 bool bIsNegative,
2396 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002397{
2398 QCBOREncode_AddSZString(pMe, szLabel);
2399 QCBOREncode_AddTDecimalFractionBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase10Exponent);
2400}
2401
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002402static inline void
2403QCBOREncode_AddTDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
2404 int64_t nLabel,
2405 uint8_t uTagRequirement,
2406 UsefulBufC Mantissa,
2407 bool bIsNegative,
2408 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002409{
2410 QCBOREncode_AddInt64(pMe, nLabel);
2411 QCBOREncode_AddTDecimalFractionBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase10Exponent);
2412}
2413
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002414static inline void
2415QCBOREncode_AddDecimalFractionBigNum(QCBOREncodeContext *pMe,
2416 UsefulBufC Mantissa,
2417 bool bIsNegative,
2418 int64_t nBase10Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002419{
2420 QCBOREncode_AddTDecimalFractionBigNum(pMe, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase10Exponent);
2421}
2422
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002423static inline void
2424QCBOREncode_AddDecimalFractionBigNumToMapSZ(QCBOREncodeContext *pMe,
2425 const char *szLabel,
2426 UsefulBufC Mantissa,
2427 bool bIsNegative,
2428 int64_t nBase10Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002429{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002430 QCBOREncode_AddTDecimalFractionBigNumToMapSZ(pMe,
2431 szLabel,
2432 QCBOR_ENCODE_AS_TAG,
2433 Mantissa,
2434 bIsNegative,
2435 nBase10Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002436}
2437
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002438static inline void
2439QCBOREncode_AddDecimalFractionBigNumToMapN(QCBOREncodeContext *pMe,
2440 int64_t nLabel,
2441 UsefulBufC Mantissa,
2442 bool bIsNegative,
2443 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002444{
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002445 QCBOREncode_AddTDecimalFractionBigNumToMapN(pMe,
2446 nLabel,
2447 QCBOR_ENCODE_AS_TAG,
2448 Mantissa,
2449 bIsNegative,
2450 nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002451}
2452
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002453
2454
2455
2456
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002457static inline void
2458QCBOREncode_AddTBigFloat(QCBOREncodeContext *pMe,
2459 uint8_t uTagRequirement,
2460 int64_t nMantissa,
2461 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002462{
2463 uint64_t uTag;
2464 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2465 uTag = CBOR_TAG_BIGFLOAT;
2466 } else {
2467 uTag = CBOR_TAG_INVALID64;
2468 }
2469 QCBOREncode_AddExponentAndMantissa(pMe, uTag, NULLUsefulBufC, false, nMantissa, nBase2Exponent);
2470}
2471
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002472static inline void
2473QCBOREncode_AddTBigFloatToMapSZ(QCBOREncodeContext *pMe,
2474 const char *szLabel,
2475 uint8_t uTagRequirement,
2476 int64_t nMantissa,
2477 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002478{
2479 QCBOREncode_AddSZString(pMe, szLabel);
2480 QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
2481}
2482
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002483static inline void
2484QCBOREncode_AddTBigFloatToMapN(QCBOREncodeContext *pMe,
2485 int64_t nLabel,
2486 uint8_t uTagRequirement,
2487 int64_t nMantissa,
2488 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002489{
2490 QCBOREncode_AddInt64(pMe, nLabel);
2491 QCBOREncode_AddTBigFloat(pMe, uTagRequirement, nMantissa, nBase2Exponent);
2492}
2493
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002494static inline void
2495QCBOREncode_AddBigFloat(QCBOREncodeContext *pMe,
2496 int64_t nMantissa,
2497 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002498{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002499 QCBOREncode_AddTBigFloat(pMe, QCBOR_ENCODE_AS_TAG, nMantissa, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002500}
2501
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002502static inline void
2503QCBOREncode_AddBigFloatToMap(QCBOREncodeContext *pMe,
2504 const char *szLabel,
2505 int64_t nMantissa,
2506 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002507{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002508 QCBOREncode_AddTBigFloatToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002509}
2510
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002511static inline void
2512QCBOREncode_AddBigFloatToMapN(QCBOREncodeContext *pMe,
2513 int64_t nLabel,
2514 int64_t nMantissa,
2515 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002516{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002517 QCBOREncode_AddTBigFloatToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, nMantissa, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002518}
2519
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002520
2521
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002522static inline void
2523QCBOREncode_AddTBigFloatBigNum(QCBOREncodeContext *pMe,
2524 uint8_t uTagRequirement,
2525 UsefulBufC Mantissa,
2526 bool bIsNegative,
2527 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002528{
2529 uint64_t uTag;
2530 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2531 uTag = CBOR_TAG_BIGFLOAT;
2532 } else {
2533 uTag = CBOR_TAG_INVALID64;
2534 }
2535 QCBOREncode_AddExponentAndMantissa(pMe, uTag, Mantissa, bIsNegative, 0, nBase2Exponent);
2536}
2537
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002538static inline void
2539QCBOREncode_AddTBigFloatBigNumToMapSZ(QCBOREncodeContext *pMe,
2540 const char *szLabel,
2541 uint8_t uTagRequirement,
2542 UsefulBufC Mantissa,
2543 bool bIsNegative,
2544 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002545{
2546 QCBOREncode_AddSZString(pMe, szLabel);
2547 QCBOREncode_AddTBigFloatBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase2Exponent);
2548}
2549
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002550static inline void
2551QCBOREncode_AddTBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
2552 int64_t nLabel,
2553 uint8_t uTagRequirement,
2554 UsefulBufC Mantissa,
2555 bool bIsNegative,
2556 int64_t nBase2Exponent)
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002557{
2558 QCBOREncode_AddInt64(pMe, nLabel);
2559 QCBOREncode_AddTBigFloatBigNum(pMe, uTagRequirement, Mantissa, bIsNegative, nBase2Exponent);
2560}
2561
2562
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002563static inline void
2564QCBOREncode_AddBigFloatBigNum(QCBOREncodeContext *pMe,
2565 UsefulBufC Mantissa,
2566 bool bIsNegative,
2567 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002568{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002569 QCBOREncode_AddTBigFloatBigNum(pMe, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002570}
2571
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002572static inline void
2573QCBOREncode_AddBigFloatBigNumToMap(QCBOREncodeContext *pMe,
2574 const char *szLabel,
2575 UsefulBufC Mantissa,
2576 bool bIsNegative,
2577 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002578{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002579 QCBOREncode_AddTBigFloatBigNumToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002580}
2581
Laurence Lundblade45d5e482020-09-15 21:15:15 -07002582static inline void
2583QCBOREncode_AddBigFloatBigNumToMapN(QCBOREncodeContext *pMe,
2584 int64_t nLabel,
2585 UsefulBufC Mantissa,
2586 bool bIsNegative,
2587 int64_t nBase2Exponent)
Michael Eckel5c531332020-03-02 01:35:30 +01002588{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002589 QCBOREncode_AddTBigFloatBigNumToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Mantissa, bIsNegative, nBase2Exponent);
Michael Eckel5c531332020-03-02 01:35:30 +01002590}
2591#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
2592
2593
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002594static inline void
2595QCBOREncode_AddTURI(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01002596{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002597 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2598 QCBOREncode_AddTag(pMe, CBOR_TAG_URI);
2599 }
2600 QCBOREncode_AddText(pMe, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01002601}
2602
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002603static inline void
2604QCBOREncode_AddTURIToMapSZ(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uTagRequirement, UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01002605{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002606 QCBOREncode_AddSZString(pMe, szLabel);
2607 QCBOREncode_AddTURI(pMe, uTagRequirement, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01002608}
2609
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002610static inline void
2611QCBOREncode_AddTURIToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC URI)
Michael Eckel5c531332020-03-02 01:35:30 +01002612{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002613 QCBOREncode_AddInt64(pMe, nLabel);
2614 QCBOREncode_AddTURI(pMe, uTagRequirement, URI);
2615}
2616
2617static inline void
2618QCBOREncode_AddURI(QCBOREncodeContext *pMe, UsefulBufC URI)
2619{
2620 QCBOREncode_AddTURI(pMe, QCBOR_ENCODE_AS_TAG, URI);
2621}
2622
2623static inline void
2624QCBOREncode_AddURIToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC URI)
2625{
2626 QCBOREncode_AddTURIToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, URI);
2627}
2628
2629static inline void
2630QCBOREncode_AddURIToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC URI)
2631{
2632 QCBOREncode_AddTURIToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, URI);
Michael Eckel5c531332020-03-02 01:35:30 +01002633}
2634
2635
2636
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002637static inline void
2638QCBOREncode_AddTB64Text(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002639{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002640 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2641 QCBOREncode_AddTag(pMe, CBOR_TAG_B64);
2642 }
2643 QCBOREncode_AddText(pMe, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002644}
2645
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002646static inline void
2647QCBOREncode_AddTB64TextToMapSZ(QCBOREncodeContext *pMe,
2648 const char *szLabel,
2649 uint8_t uTagRequirement,
2650 UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002651{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002652 QCBOREncode_AddSZString(pMe, szLabel);
2653 QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002654}
2655
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002656static inline void
2657QCBOREncode_AddTB64TextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002658{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002659 QCBOREncode_AddInt64(pMe, nLabel);
2660 QCBOREncode_AddTB64Text(pMe, uTagRequirement, B64Text);
2661}
2662
2663static inline void
2664QCBOREncode_AddB64Text(QCBOREncodeContext *pMe, UsefulBufC B64Text)
2665{
2666 QCBOREncode_AddTB64Text(pMe, QCBOR_ENCODE_AS_TAG, B64Text);
2667}
2668
2669static inline void
2670QCBOREncode_AddB64TextToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC B64Text)
2671{
2672 QCBOREncode_AddTB64TextToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, B64Text);
2673}
2674
2675static inline void
2676QCBOREncode_AddB64TextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC B64Text)
2677{
2678 QCBOREncode_AddTB64TextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002679}
2680
2681
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002682
2683static inline void
2684QCBOREncode_AddTB64URLText(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002685{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002686 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2687 QCBOREncode_AddTag(pMe, CBOR_TAG_B64URL);
2688 }
2689 QCBOREncode_AddText(pMe, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002690}
2691
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002692static inline void
2693QCBOREncode_AddTB64URLTextToMapSZ(QCBOREncodeContext *pMe,
2694 const char *szLabel,
2695 uint8_t uTagRequirement,
2696 UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002697{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002698 QCBOREncode_AddSZString(pMe, szLabel);
2699 QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002700}
2701
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002702static inline void
2703QCBOREncode_AddTB64URLTextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC B64Text)
Michael Eckel5c531332020-03-02 01:35:30 +01002704{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002705 QCBOREncode_AddInt64(pMe, nLabel);
2706 QCBOREncode_AddTB64URLText(pMe, uTagRequirement, B64Text);
2707}
2708
2709static inline void
2710QCBOREncode_AddB64URLText(QCBOREncodeContext *pMe, UsefulBufC B64Text)
2711{
2712 QCBOREncode_AddTB64URLText(pMe, QCBOR_ENCODE_AS_TAG, B64Text);
2713}
2714
2715static inline void
2716QCBOREncode_AddB64URLTextToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC B64Text)
2717{
2718 QCBOREncode_AddTB64URLTextToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, B64Text);
2719}
2720
2721static inline void
2722QCBOREncode_AddB64URLTextToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC B64Text)
2723{
2724 QCBOREncode_AddTB64URLTextToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, B64Text);
Michael Eckel5c531332020-03-02 01:35:30 +01002725}
2726
2727
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002728
2729static inline void
2730QCBOREncode_AddTRegex(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002731{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002732 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2733 QCBOREncode_AddTag(pMe, CBOR_TAG_REGEX);
2734 }
2735 QCBOREncode_AddText(pMe, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002736}
2737
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002738static inline void
2739QCBOREncode_AddTRegexToMapSZ(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002740{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002741 QCBOREncode_AddSZString(pMe, szLabel);
2742 QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes);
Michael Eckel5c531332020-03-02 01:35:30 +01002743}
2744
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002745static inline void
2746QCBOREncode_AddTRegexToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC Bytes)
Michael Eckel5c531332020-03-02 01:35:30 +01002747{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002748 QCBOREncode_AddInt64(pMe, nLabel);
2749 QCBOREncode_AddTRegex(pMe, uTagRequirement, Bytes);
2750}
2751
2752static inline void
2753QCBOREncode_AddRegex(QCBOREncodeContext *pMe, UsefulBufC Bytes)
2754{
2755 QCBOREncode_AddTRegex(pMe, QCBOR_ENCODE_AS_TAG, Bytes);
2756}
2757
2758static inline void
2759QCBOREncode_AddRegexToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC Bytes)
2760{
2761 QCBOREncode_AddTRegexToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2762}
2763
2764static inline void
2765QCBOREncode_AddRegexToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC Bytes)
2766{
2767 QCBOREncode_AddTRegexToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, Bytes);
2768
Michael Eckel5c531332020-03-02 01:35:30 +01002769}
2770
2771
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002772static inline void
2773QCBOREncode_AddTMIMEData(QCBOREncodeContext *pMe, uint8_t uTagRequirement, UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01002774{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002775 // TODO: add support for binary MIME.
2776 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2777 QCBOREncode_AddTag(pMe, CBOR_TAG_MIME);
2778 }
2779 QCBOREncode_AddText(pMe, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01002780}
2781
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002782static inline void
2783QCBOREncode_AddTMIMEDataToMapSZ(QCBOREncodeContext *pMe,
2784 const char *szLabel,
2785 uint8_t uTagRequirement,
2786 UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01002787{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002788 QCBOREncode_AddSZString(pMe, szLabel);
2789 QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01002790}
2791
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002792static inline void
2793QCBOREncode_AddTMIMEDataToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, UsefulBufC MIMEData)
Michael Eckel5c531332020-03-02 01:35:30 +01002794{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002795 QCBOREncode_AddInt64(pMe, nLabel);
2796 QCBOREncode_AddTMIMEData(pMe, uTagRequirement, MIMEData);
2797}
2798
2799static inline void
2800QCBOREncode_AddMIMEData(QCBOREncodeContext *pMe, UsefulBufC MIMEData)
2801{
2802 QCBOREncode_AddTMIMEData(pMe, QCBOR_ENCODE_AS_TAG, MIMEData);
2803}
2804
2805static inline void
2806QCBOREncode_AddMIMEDataToMap(QCBOREncodeContext *pMe, const char *szLabel, UsefulBufC MIMEData)
2807{
2808 QCBOREncode_AddTMIMEDataToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, MIMEData);
2809}
2810
2811static inline void
2812QCBOREncode_AddMIMEDataToMapN(QCBOREncodeContext *pMe, int64_t nLabel, UsefulBufC MIMEData)
2813{
2814 QCBOREncode_AddTMIMEDataToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, MIMEData);
Michael Eckel5c531332020-03-02 01:35:30 +01002815}
2816
2817
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002818static inline void
2819QCBOREncode_AddTDateString(QCBOREncodeContext *pMe, uint8_t uTagRequirement, const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002820{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002821 if(uTagRequirement == QCBOR_ENCODE_AS_TAG) {
2822 QCBOREncode_AddTag(pMe, CBOR_TAG_DATE_STRING);
2823 }
2824 QCBOREncode_AddSZString(pMe, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002825}
2826
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002827static inline void
2828QCBOREncode_AddTDateStringToMapSZ(QCBOREncodeContext *pMe,
2829 const char *szLabel,
2830 uint8_t uTagRequirement,
2831 const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002832{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002833 QCBOREncode_AddSZString(pMe, szLabel);
2834 QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002835}
2836
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002837static inline void
2838QCBOREncode_AddTDateStringToMapN(QCBOREncodeContext *pMe, int64_t nLabel, uint8_t uTagRequirement, const char *szDate)
Michael Eckel5c531332020-03-02 01:35:30 +01002839{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002840 QCBOREncode_AddInt64(pMe, nLabel);
2841 QCBOREncode_AddTDateString(pMe, uTagRequirement, szDate);
2842}
2843
2844static inline void
2845QCBOREncode_AddDateString(QCBOREncodeContext *pMe, const char *szDate)
2846{
2847 QCBOREncode_AddTDateString(pMe, QCBOR_ENCODE_AS_TAG, szDate);
2848}
2849
2850static inline void
2851QCBOREncode_AddDateStringToMap(QCBOREncodeContext *pMe, const char *szLabel, const char *szDate)
2852{
2853 QCBOREncode_AddTDateStringToMapSZ(pMe, szLabel, QCBOR_ENCODE_AS_TAG, szDate);
2854}
2855
2856static inline void
2857QCBOREncode_AddDateStringToMapN(QCBOREncodeContext *pMe, int64_t nLabel, const char *szDate)
2858{
2859 QCBOREncode_AddTDateStringToMapN(pMe, nLabel, QCBOR_ENCODE_AS_TAG, szDate);
Michael Eckel5c531332020-03-02 01:35:30 +01002860}
2861
2862
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002863static inline void
2864QCBOREncode_AddSimple(QCBOREncodeContext *pMe, uint64_t uNum)
Michael Eckel5c531332020-03-02 01:35:30 +01002865{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002866 QCBOREncode_AddType7(pMe, 0, uNum);
Michael Eckel5c531332020-03-02 01:35:30 +01002867}
2868
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002869static inline void
2870QCBOREncode_AddSimpleToMap(QCBOREncodeContext *pMe, const char *szLabel, uint8_t uSimple)
Michael Eckel5c531332020-03-02 01:35:30 +01002871{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002872 QCBOREncode_AddSZString(pMe, szLabel);
2873 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01002874}
2875
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002876static inline void
2877QCBOREncode_AddSimpleToMapN(QCBOREncodeContext *pMe, int nLabel, uint8_t uSimple)
Michael Eckel5c531332020-03-02 01:35:30 +01002878{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002879 QCBOREncode_AddInt64(pMe, nLabel);
2880 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01002881}
2882
2883
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002884static inline void
2885QCBOREncode_AddBool(QCBOREncodeContext *pMe, bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01002886{
2887 uint8_t uSimple = CBOR_SIMPLEV_FALSE;
2888 if(b) {
2889 uSimple = CBOR_SIMPLEV_TRUE;
2890 }
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002891 QCBOREncode_AddSimple(pMe, uSimple);
Michael Eckel5c531332020-03-02 01:35:30 +01002892}
2893
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002894static inline void
2895QCBOREncode_AddBoolToMap(QCBOREncodeContext *pMe, const char *szLabel, bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01002896{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002897 QCBOREncode_AddSZString(pMe, szLabel);
2898 QCBOREncode_AddBool(pMe, b);
Michael Eckel5c531332020-03-02 01:35:30 +01002899}
2900
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002901static inline void
2902QCBOREncode_AddBoolToMapN(QCBOREncodeContext *pMe, int64_t nLabel, bool b)
Michael Eckel5c531332020-03-02 01:35:30 +01002903{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002904 QCBOREncode_AddInt64(pMe, nLabel);
2905 QCBOREncode_AddBool(pMe, b);
Michael Eckel5c531332020-03-02 01:35:30 +01002906}
2907
2908
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002909static inline void
2910QCBOREncode_AddNULL(QCBOREncodeContext *pMe)
Michael Eckel5c531332020-03-02 01:35:30 +01002911{
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002912 QCBOREncode_AddSimple(pMe, CBOR_SIMPLEV_NULL);
Michael Eckel5c531332020-03-02 01:35:30 +01002913}
2914
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002915static inline void
2916QCBOREncode_AddNULLToMap(QCBOREncodeContext *pCtx, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01002917{
2918 QCBOREncode_AddSZString(pCtx, szLabel);
2919 QCBOREncode_AddNULL(pCtx);
2920}
2921
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002922static inline void
2923QCBOREncode_AddNULLToMapN(QCBOREncodeContext *pCtx, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01002924{
2925 QCBOREncode_AddInt64(pCtx, nLabel);
2926 QCBOREncode_AddNULL(pCtx);
2927}
2928
2929
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002930static inline void
2931QCBOREncode_AddUndef(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01002932{
2933 QCBOREncode_AddSimple(pCtx, CBOR_SIMPLEV_UNDEF);
2934}
2935
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002936static inline void
2937QCBOREncode_AddUndefToMap(QCBOREncodeContext *pCtx, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01002938{
2939 QCBOREncode_AddSZString(pCtx, szLabel);
2940 QCBOREncode_AddUndef(pCtx);
2941}
2942
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002943static inline void
2944QCBOREncode_AddUndefToMapN(QCBOREncodeContext *pCtx, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01002945{
2946 QCBOREncode_AddInt64(pCtx, nLabel);
2947 QCBOREncode_AddUndef(pCtx);
2948}
2949
2950
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002951static inline void
2952QCBOREncode_OpenArray(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01002953{
2954 QCBOREncode_OpenMapOrArray(pCtx, CBOR_MAJOR_TYPE_ARRAY);
2955}
2956
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002957static inline void
2958QCBOREncode_OpenArrayInMap(QCBOREncodeContext *pCtx, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01002959{
2960 QCBOREncode_AddSZString(pCtx, szLabel);
2961 QCBOREncode_OpenArray(pCtx);
2962}
2963
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002964static inline void
2965QCBOREncode_OpenArrayInMapN(QCBOREncodeContext *pCtx, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01002966{
2967 QCBOREncode_AddInt64(pCtx, nLabel);
2968 QCBOREncode_OpenArray(pCtx);
2969}
2970
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002971static inline void
2972QCBOREncode_CloseArray(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01002973{
2974 QCBOREncode_CloseMapOrArray(pCtx, CBOR_MAJOR_TYPE_ARRAY);
2975}
2976
2977
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002978static inline void
2979QCBOREncode_OpenMap(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01002980{
2981 QCBOREncode_OpenMapOrArray(pCtx, CBOR_MAJOR_TYPE_MAP);
2982}
2983
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002984static inline void
2985QCBOREncode_OpenMapInMap(QCBOREncodeContext *pCtx, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01002986{
2987 QCBOREncode_AddSZString(pCtx, szLabel);
2988 QCBOREncode_OpenMap(pCtx);
2989}
2990
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002991static inline void
2992QCBOREncode_OpenMapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01002993{
2994 QCBOREncode_AddInt64(pCtx, nLabel);
2995 QCBOREncode_OpenMap(pCtx);
2996}
2997
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07002998static inline void
2999QCBOREncode_CloseMap(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01003000{
3001 QCBOREncode_CloseMapOrArray(pCtx, CBOR_MAJOR_TYPE_MAP);
3002}
3003
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003004static inline void
3005QCBOREncode_OpenArrayIndefiniteLength(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01003006{
3007 QCBOREncode_OpenMapOrArrayIndefiniteLength(pCtx, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN);
3008}
3009
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003010static inline void
3011QCBOREncode_OpenArrayIndefiniteLengthInMap(QCBOREncodeContext *pCtx, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003012{
3013 QCBOREncode_AddSZString(pCtx, szLabel);
3014 QCBOREncode_OpenArrayIndefiniteLength(pCtx);
3015}
3016
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003017static inline void
3018QCBOREncode_OpenArrayIndefiniteLengthInMapN(QCBOREncodeContext *pCtx, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003019{
3020 QCBOREncode_AddInt64(pCtx, nLabel);
3021 QCBOREncode_OpenArrayIndefiniteLength(pCtx);
3022}
3023
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003024static inline void
3025QCBOREncode_CloseArrayIndefiniteLength(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01003026{
3027 QCBOREncode_CloseMapOrArrayIndefiniteLength(pCtx, CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN);
3028}
3029
3030
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003031static inline void
3032QCBOREncode_OpenMapIndefiniteLength(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01003033{
3034 QCBOREncode_OpenMapOrArrayIndefiniteLength(pCtx, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
3035}
3036
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003037static inline void
3038QCBOREncode_OpenMapIndefiniteLengthInMap(QCBOREncodeContext *pCtx, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003039{
3040 QCBOREncode_AddSZString(pCtx, szLabel);
3041 QCBOREncode_OpenMapIndefiniteLength(pCtx);
3042}
3043
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003044static inline void
3045QCBOREncode_OpenMapIndefiniteLengthInMapN(QCBOREncodeContext *pCtx, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003046{
3047 QCBOREncode_AddInt64(pCtx, nLabel);
3048 QCBOREncode_OpenMapIndefiniteLength(pCtx);
3049}
3050
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003051static inline void
3052QCBOREncode_CloseMapIndefiniteLength(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01003053{
3054 QCBOREncode_CloseMapOrArrayIndefiniteLength(pCtx, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN);
3055}
3056
3057
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003058static inline void
3059QCBOREncode_BstrWrap(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01003060{
3061 QCBOREncode_OpenMapOrArray(pCtx, CBOR_MAJOR_TYPE_BYTE_STRING);
3062}
3063
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003064static inline void
3065QCBOREncode_BstrWrapInMap(QCBOREncodeContext *pCtx, const char *szLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003066{
3067 QCBOREncode_AddSZString(pCtx, szLabel);
3068 QCBOREncode_BstrWrap(pCtx);
3069}
3070
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003071static inline void
3072QCBOREncode_BstrWrapInMapN(QCBOREncodeContext *pCtx, int64_t nLabel)
Michael Eckel5c531332020-03-02 01:35:30 +01003073{
3074 QCBOREncode_AddInt64(pCtx, nLabel);
3075 QCBOREncode_BstrWrap(pCtx);
3076}
3077
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003078static inline void
3079QCBOREncode_CloseBstrWrap(QCBOREncodeContext *pCtx, UsefulBufC *pWrappedCBOR)
Michael Eckel5c531332020-03-02 01:35:30 +01003080{
3081 QCBOREncode_CloseBstrWrap2(pCtx, true, pWrappedCBOR);
3082}
3083
3084
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003085static inline void
3086QCBOREncode_AddEncoded(QCBOREncodeContext *pCtx, UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01003087{
3088 QCBOREncode_AddBuffer(pCtx, CBOR_MAJOR_NONE_TYPE_RAW, Encoded);
3089}
3090
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003091static inline void
3092QCBOREncode_AddEncodedToMap(QCBOREncodeContext *pCtx, const char *szLabel, UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01003093{
3094 QCBOREncode_AddSZString(pCtx, szLabel);
3095 QCBOREncode_AddEncoded(pCtx, Encoded);
3096}
3097
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003098static inline void
3099QCBOREncode_AddEncodedToMapN(QCBOREncodeContext *pCtx, int64_t nLabel, UsefulBufC Encoded)
Michael Eckel5c531332020-03-02 01:35:30 +01003100{
3101 QCBOREncode_AddInt64(pCtx, nLabel);
3102 QCBOREncode_AddEncoded(pCtx, Encoded);
3103}
3104
3105
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003106static inline int
3107QCBOREncode_IsBufferNULL(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01003108{
3109 return UsefulOutBuf_IsBufferNULL(&(pCtx->OutBuf));
3110}
3111
Laurence Lundbladeae66d3f2020-09-14 18:12:08 -07003112static inline QCBORError
3113QCBOREncode_GetErrorState(QCBOREncodeContext *pCtx)
Michael Eckel5c531332020-03-02 01:35:30 +01003114{
3115 if(UsefulOutBuf_GetError(&(pCtx->OutBuf))) {
3116 // Items didn't fit in the buffer.
3117 // This check catches this condition for all the appends and inserts
3118 // so checks aren't needed when the appends and inserts are performed.
3119 // And of course UsefulBuf will never overrun the input buffer given
3120 // to it. No complex analysis of the error handling in this file is
3121 // needed to know that is true. Just read the UsefulBuf code.
3122 pCtx->uError = QCBOR_ERR_BUFFER_TOO_SMALL;
3123 // QCBOR_ERR_BUFFER_TOO_SMALL masks other errors, but that is
3124 // OK. Once the caller fixes this, they'll be unmasked.
3125 }
3126
3127 return (QCBORError)pCtx->uError;
3128}
3129
3130
Laurence Lundblade45d5e482020-09-15 21:15:15 -07003131/* ========================================================================
3132 END OF PRIVATE INLINE IMPLEMENTATION
3133 ======================================================================== */
Michael Eckel5c531332020-03-02 01:35:30 +01003134
3135#ifdef __cplusplus
3136}
3137#endif
3138
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08003139#endif /* qcbor_encode_h */