blob: 28fb225ff5b2f4cfcea88a619bb7c6dcdbbcfed1 [file] [log] [blame]
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladed39cd392019-01-11 18:17:38 -08003 Copyright (c) 2018-2019, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution 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.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS 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.
Laurence Lundblade624405d2018-09-18 20:10:47 -070031 ==============================================================================*/
32
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070033/*===================================================================================
34 FILE: qcbor_encode.c
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080035
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070036 DESCRIPTION: This file contains the implementation of QCBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080037
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070038 EDIT HISTORY FOR FILE:
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080039
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070040 This section contains comments describing changes made to the module.
41 Notice that changes are listed in reverse chronological order.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080042
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070043 when who what, where, why
44 -------- ---- ---------------------------------------------------
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070045 8/7/19 llundblade Prevent encoding simple type reserved values 24..31
Jan Jongboom4a93a662019-07-25 08:44:58 +020046 7/25/19 janjongboom Add indefinite length encoding for maps and arrays
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -070047 4/6/19 llundblade Wrapped bstr returned now includes the wrapping bstr
Laurence Lundblade9c097392018-12-30 13:52:24 -080048 12/30/18 llundblade Small efficient clever encode of type & argument.
Laurence Lundblade067035b2018-11-28 17:35:25 -080049 11/29/18 llundblade Rework to simpler handling of tags and labels.
50 11/9/18 llundblade Error codes are now enums.
51 11/1/18 llundblade Floating support.
52 10/31/18 llundblade Switch to one license that is almost BSD-3.
53 09/28/18 llundblade Added bstr wrapping feature for COSE implementation.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080054 02/05/18 llundbla Works on CPUs which require integer alignment.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070055 Requires new version of UsefulBuf.
56 07/05/17 llundbla Add bstr wrapping of maps/arrays for COSE
57 03/01/17 llundbla More data types
58 11/13/16 llundbla Integrate most TZ changes back into github version.
59 09/30/16 gkanike Porting to TZ.
60 03/15/16 llundbla Initial Version.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080061
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070062 =====================================================================================*/
63
64#include "qcbor.h"
Laurence Lundblade12d32c52018-09-19 11:25:27 -070065#include "ieee754.h"
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070066
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070067
68/*...... This is a ruler that is 80 characters long...........................*/
69
70
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070071/*
72 CBOR's two nesting types, arrays and maps, are tracked here. There is a
73 limit of QCBOR_MAX_ARRAY_NESTING to the number of arrays and maps
74 that can be nested in one encoding so the encoding context stays
75 small enough to fit on the stack.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080076
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070077 When an array / map is opened, pCurrentNesting points to the element
78 in pArrays that records the type, start position and accumluates a
79 count of the number of items added. When closed the start position is
80 used to go back and fill in the type and number of items in the array
81 / map.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080082
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070083 Encoded output be just items like ints and strings that are
84 not part of any array / map. That is, the first thing encoded
85 does not have to be an array or a map.
86 */
87inline static void Nesting_Init(QCBORTrackNesting *pNesting)
88{
89 // assumes pNesting has been zeroed
90 pNesting->pCurrentNesting = &pNesting->pArrays[0];
91 // Implied CBOR array at the top nesting level. This is never returned,
92 // but makes the item count work correctly.
93 pNesting->pCurrentNesting->uMajorType = CBOR_MAJOR_TYPE_ARRAY;
94}
95
Laurence Lundblade2c40ab82018-12-30 14:20:29 -080096inline static QCBORError Nesting_Increase(QCBORTrackNesting *pNesting,
97 uint8_t uMajorType,
98 uint32_t uPos)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070099{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700100 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800101
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700102 if(pNesting->pCurrentNesting == &pNesting->pArrays[QCBOR_MAX_ARRAY_NESTING]) {
103 // trying to open one too many
104 nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
105 } else {
106 pNesting->pCurrentNesting++;
107 pNesting->pCurrentNesting->uCount = 0;
108 pNesting->pCurrentNesting->uStart = uPos;
109 pNesting->pCurrentNesting->uMajorType = uMajorType;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700110 }
111 return nReturn;
112}
113
114inline static void Nesting_Decrease(QCBORTrackNesting *pNesting)
115{
116 pNesting->pCurrentNesting--;
117}
118
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800119inline static QCBORError Nesting_Increment(QCBORTrackNesting *pNesting)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700120{
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800121 if(1 >= QCBOR_MAX_ITEMS_IN_ARRAY - pNesting->pCurrentNesting->uCount) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700122 return QCBOR_ERR_ARRAY_TOO_LONG;
123 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800124
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800125 pNesting->pCurrentNesting->uCount += 1;
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800126
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700127 return QCBOR_SUCCESS;
128}
129
130inline static uint16_t Nesting_GetCount(QCBORTrackNesting *pNesting)
131{
132 // The nesting count recorded is always the actual number of individiual
133 // data items in the array or map. For arrays CBOR uses the actual item
134 // count. For maps, CBOR uses the number of pairs. This function returns
135 // the number needed for the CBOR encoding, so it divides the number of
136 // items by two for maps to get the number of pairs. This implementation
137 // takes advantage of the map major type being one larger the array major
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800138 // type, hence uDivisor is either 1 or 2.
139 const uint16_t uDivisor = pNesting->pCurrentNesting->uMajorType - CBOR_MAJOR_TYPE_ARRAY+1;
140
141 return pNesting->pCurrentNesting->uCount / uDivisor;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700142}
143
144inline static uint32_t Nesting_GetStartPos(QCBORTrackNesting *pNesting)
145{
146 return pNesting->pCurrentNesting->uStart;
147}
148
149inline static uint8_t Nesting_GetMajorType(QCBORTrackNesting *pNesting)
150{
151 return pNesting->pCurrentNesting->uMajorType;
152}
153
154inline static int Nesting_IsInNest(QCBORTrackNesting *pNesting)
155{
156 return pNesting->pCurrentNesting == &pNesting->pArrays[0] ? 0 : 1;
157}
158
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700159
160
161
162/*
163 Error tracking plan -- Errors are tracked internally and not returned
164 until Finish is called. The CBOR errors are in me->uError.
Laurence Lundblade067035b2018-11-28 17:35:25 -0800165 UsefulOutBuf also tracks whether the buffer is full or not in its
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700166 context. Once either of these errors is set they are never
Laurence Lundblade241705e2018-12-30 18:56:14 -0800167 cleared. Only QCBOREncode_Init() resets them. Or said another way, they must
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700168 never be cleared or we'll tell the caller all is good when it is not.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800169
Laurence Lundblade241705e2018-12-30 18:56:14 -0800170 Only one error code is reported by QCBOREncode_Finish() even if there are
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700171 multiple errors. The last one set wins. The caller might have to fix
172 one error to reveal the next one they have to fix. This is OK.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800173
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700174 The buffer full error tracked by UsefulBuf is only pulled out of
175 UsefulBuf in Finish() so it is the one that usually wins. UsefulBuf
176 will never go off the end of the buffer even if it is called again
177 and again when full.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800178
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700179 It is really tempting to not check for overflow on the count in the
180 number of items in an array. It would save a lot of code, it is
181 extremely unlikely that any one will every put 65,000 items in an
182 array, and the only bad thing that would happen is the CBOR would be
Laurence Lundblade241705e2018-12-30 18:56:14 -0800183 bogus.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800184
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700185 Since this does not parse any input, you could in theory remove all
186 error checks in this code if you knew the caller called it
187 correctly. Maybe someday CDDL or some such language will be able to
188 generate the code to call this and the calling code would always be
Laurence Lundblade56230d12018-11-01 11:14:51 +0700189 correct. This could also automatically size some of the data
Laurence Lundblade241705e2018-12-30 18:56:14 -0800190 structures like array/map nesting resulting in some stack memory
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700191 savings.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800192
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700193 Errors returned here fall into three categories:
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800194
Laurence Lundblade067035b2018-11-28 17:35:25 -0800195 Sizes
Laurence Lundblade241705e2018-12-30 18:56:14 -0800196 QCBOR_ERR_BUFFER_TOO_LARGE -- Encoded output exceeded UINT32_MAX
Laurence Lundblade067035b2018-11-28 17:35:25 -0800197 QCBOR_ERR_BUFFER_TOO_SMALL -- output buffer too small
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800198
Laurence Lundblade241705e2018-12-30 18:56:14 -0800199 QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -- Array/map nesting > QCBOR_MAX_ARRAY_NESTING1
Laurence Lundblade067035b2018-11-28 17:35:25 -0800200 QCBOR_ERR_ARRAY_TOO_LONG -- Too many things added to an array/map
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800201
Laurence Lundblade067035b2018-11-28 17:35:25 -0800202 Nesting constructed incorrectly
203 QCBOR_ERR_TOO_MANY_CLOSES -- more close calls than opens
204 QCBOR_ERR_CLOSE_MISMATCH -- Type of close does not match open
205 QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN -- Finish called without enough closes
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700206
207 Would generate not-well-formed CBOR
208 QCBOR_ERR_UNSUPPORTED -- Simple type between 24 and 31
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700209 */
210
211
212
213
214/*
215 Public function for initialization. See header qcbor.h
216 */
Laurence Lundblade2296db52018-09-14 18:08:39 -0700217void QCBOREncode_Init(QCBOREncodeContext *me, UsefulBuf Storage)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700218{
219 memset(me, 0, sizeof(QCBOREncodeContext));
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800220 UsefulOutBuf_Init(&(me->OutBuf), Storage);
221 Nesting_Init(&(me->nesting));
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700222}
223
224
225
226
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800227/*
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800228 All CBOR data items have a type and an "argument". The argument is
229 either the value of the item for integer types, the length of the
230 content for string, byte, array and map types, a tag for major type
231 6, and has several uses for major type 7.
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800232
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800233 This function encodes the type and the argument. There are several
234 encodings for the argument depending on how large it is and how it is
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700235 used.
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800236
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800237 Every encoding of the type and argument has at least one byte, the
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700238 "initial byte".
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800239
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700240 The top three bits of the initial byte are the major type for the
241 CBOR data item. The eight major types defined by the standard are
242 defined as CBOR_MAJOR_TYPE_xxxx in qcbor.h.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800243
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700244 The remaining five bits, known as "additional information", and
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800245 possibly more bytes encode the argument. If the argument is less than
246 24, then it is encoded entirely in the five bits. This is neat
247 because it allows you to encode an entire CBOR data item in 1 byte
248 for many values and types (integers 0-23, true, false, and tags).
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800249
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800250 If the argument is larger than 24, then it is encoded in 1,2,4 or 8
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700251 additional bytes, with the number of these bytes indicated by the
252 values of the 5 bits 24, 25, 25 and 27.
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800253
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800254 It is possible to encode a particular argument in many ways with this
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700255 representation. This implementation always uses the smallest
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800256 possible representation. This conforms with CBOR preferred encoding.
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800257
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700258 This function inserts them into the output buffer at the specified
Laurence Lundblade067035b2018-11-28 17:35:25 -0800259 position. AppendEncodedTypeAndNumber() appends to the end.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800260
261 This function takes care of converting to network byte order.
262
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700263 This function is also used to insert floats and doubles. Before this
264 function is called the float or double must be copied into a
265 uint64_t. That is how they are passed in. They are then converted to
266 network byte order correctly. The uMinLen param makes sure that even
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800267
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800268 if all the digits of a half, float or double are 0 it is still
269 correctly encoded in 2, 4 or 8 bytes.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700270 */
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800271
272static void InsertEncodedTypeAndNumber(QCBOREncodeContext *me,
273 uint8_t uMajorType,
274 int nMinLen,
275 uint64_t uNumber,
276 size_t uPos)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700277{
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800278 /*
279 This code does endian conversion without hton or knowing the
Laurence Lundblade241705e2018-12-30 18:56:14 -0800280 endianness of the machine using masks and shifts. This avoids the
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800281 dependency on hton and the mess of figuring out how to find the
282 machine's endianness.
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800283
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800284 This is a good efficient implementation on little-endian machines.
285 A faster and small implementation is possible on big-endian
286 machines because CBOR/network byte order is big endian. However
287 big endian machines are uncommon.
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800288
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800289 On x86, it is about 200 bytes instead of 500 bytes for the more
290 formal unoptimized code.
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800291
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800292 This also does the CBOR preferred shortest encoding for integers
293 and is called to do endian conversion for floats.
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800294
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800295 It works backwards from the LSB to the MSB as needed.
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800296
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800297 Code Reviewers: THIS FUNCTION DOES POINTER MATH
298 */
299 // Holds up to 9 bytes of type and argument
300 // plus one extra so pointer always points to
301 // valid bytes.
Laurence Lundblade04a859b2018-12-11 12:13:02 -0800302 uint8_t bytes[sizeof(uint64_t)+2];
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800303 // Point to the last bytes and work backwards
Laurence Lundbladef970f1d2018-12-14 01:44:23 -0800304 uint8_t *pByte = &bytes[sizeof(bytes)-1];
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800305 // This is the 5 bits in the initial byte that is not the major type
306 uint8_t uAdditionalInfo;
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800307
Jan Jongboom5d827882019-08-07 12:51:15 +0200308 if (uMajorType == CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN) {
309 uMajorType = CBOR_MAJOR_TYPE_ARRAY;
310 uAdditionalInfo = LEN_IS_INDEFINITE;
311 } else if (uMajorType == CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN) {
312 uMajorType = CBOR_MAJOR_TYPE_MAP;
313 uAdditionalInfo = LEN_IS_INDEFINITE;
314 } else if (uNumber < CBOR_TWENTY_FOUR && nMinLen == 0) {
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800315 // Simple case where argument is < 24
316 uAdditionalInfo = uNumber;
Jan Jongboom4a93a662019-07-25 08:44:58 +0200317 } else if (uMajorType == CBOR_MAJOR_TYPE_SIMPLE && uNumber == CBOR_SIMPLE_BREAK) {
318 // Break statement can be encoded in single byte too (0xff)
319 uAdditionalInfo = uNumber;
Laurence Lundblade04a859b2018-12-11 12:13:02 -0800320 } else {
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800321 /*
322 Encode argument in 1,2,4 or 8 bytes. Outer loop
323 runs once for 1 byte and 4 times for 8 bytes.
324 Inner loop runs 1, 2 or 4 times depending on
325 outer loop counter. This works backwards taking
326 8 bits off the argument being encoded at a time
327 until all bits from uNumber have been encoded
328 and the minimum encoding size is reached.
329 Minimum encoding size is for floating point
Laurence Lundblade241705e2018-12-30 18:56:14 -0800330 numbers with zero bytes.
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800331 */
Laurence Lundblade04a859b2018-12-11 12:13:02 -0800332 static const uint8_t aIterate[] = {1,1,2,4};
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800333 uint8_t i;
334 for(i = 0; uNumber || nMinLen > 0; i++) {
335 const uint8_t uIterations = aIterate[i];
336 for(int j = 0; j < uIterations; j++) {
Laurence Lundblade04a859b2018-12-11 12:13:02 -0800337 *--pByte = uNumber & 0xff;
338 uNumber = uNumber >> 8;
339 }
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800340 nMinLen -= uIterations;
Laurence Lundblade04a859b2018-12-11 12:13:02 -0800341 }
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800342 // Additional info is the encoding of the
343 // number of additional bytes to encode
344 // argument.
345 uAdditionalInfo = LEN_IS_ONE_BYTE-1 + i;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700346 }
Laurence Lundbladee9b00322018-12-30 10:33:26 -0800347 *--pByte = (uMajorType << 5) + uAdditionalInfo;
Laurence Lundbladef970f1d2018-12-14 01:44:23 -0800348
349 UsefulOutBuf_InsertData(&(me->OutBuf), pByte, &bytes[sizeof(bytes)-1] - pByte, uPos);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700350}
351
352
353/*
354 Append the type and number info to the end of the buffer.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800355
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700356 See InsertEncodedTypeAndNumber() function above for details
357*/
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800358inline static void AppendEncodedTypeAndNumber(QCBOREncodeContext *me,
359 uint8_t uMajorType,
360 uint64_t uNumber)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700361{
362 // An append is an insert at the end.
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800363 InsertEncodedTypeAndNumber(me,
364 uMajorType,
365 0,
366 uNumber,
367 UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700368}
369
370
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700371
Laurence Lundblade241705e2018-12-30 18:56:14 -0800372
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700373/*
Laurence Lundblade067035b2018-11-28 17:35:25 -0800374 Public functions for closing arrays and maps. See header qcbor.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700375 */
Laurence Lundblade067035b2018-11-28 17:35:25 -0800376void QCBOREncode_AddUInt64(QCBOREncodeContext *me, uint64_t uValue)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700377{
Laurence Lundblade067035b2018-11-28 17:35:25 -0800378 if(me->uError == QCBOR_SUCCESS) {
379 AppendEncodedTypeAndNumber(me, CBOR_MAJOR_TYPE_POSITIVE_INT, uValue);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800380 me->uError = Nesting_Increment(&(me->nesting));
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700381 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700382}
383
Laurence Lundblade56230d12018-11-01 11:14:51 +0700384
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700385/*
Laurence Lundblade067035b2018-11-28 17:35:25 -0800386 Public functions for closing arrays and maps. See header qcbor.h
387 */
388void QCBOREncode_AddInt64(QCBOREncodeContext *me, int64_t nNum)
389{
390 if(me->uError == QCBOR_SUCCESS) {
391 uint8_t uMajorType;
392 uint64_t uValue;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800393
Laurence Lundblade067035b2018-11-28 17:35:25 -0800394 if(nNum < 0) {
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800395 // In CBOR -1 encodes as 0x00 with major type negative int.
396 uValue = (uint64_t)(-nNum - 1);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800397 uMajorType = CBOR_MAJOR_TYPE_NEGATIVE_INT;
398 } else {
399 uValue = (uint64_t)nNum;
400 uMajorType = CBOR_MAJOR_TYPE_POSITIVE_INT;
401 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800402
Laurence Lundblade067035b2018-11-28 17:35:25 -0800403 AppendEncodedTypeAndNumber(me, uMajorType, uValue);
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800404 me->uError = Nesting_Increment(&(me->nesting));
Laurence Lundblade067035b2018-11-28 17:35:25 -0800405 }
406}
407
408
409/*
Laurence Lundbladeda532272019-04-07 11:40:17 -0700410 Semi-private function. It is exposed to user of the interface, but
411 they will usually call one of the inline wrappers rather than this.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800412
Laurence Lundblade067035b2018-11-28 17:35:25 -0800413 See header qcbor.h
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800414
Laurence Lundbladeda532272019-04-07 11:40:17 -0700415 Does the work of adding actual strings bytes to the CBOR output (as
416 opposed to numbers and opening / closing aggregate types).
417
418 There are four use cases:
419 CBOR_MAJOR_TYPE_BYTE_STRING -- Byte strings
420 CBOR_MAJOR_TYPE_TEXT_STRING -- Text strings
421 CBOR_MAJOR_NONE_TYPE_RAW -- Already-encoded CBOR
422 CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY -- Special case
423
424 The first two add the type and length plus the actual bytes. The
425 third just adds the bytes as the type and length are presumed to be
426 in the bytes. The fourth just adds the type and length for the very
427 special case of QCBOREncode_AddBytesLenOnly().
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700428 */
Laurence Lundblade067035b2018-11-28 17:35:25 -0800429void QCBOREncode_AddBuffer(QCBOREncodeContext *me, uint8_t uMajorType, UsefulBufC Bytes)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700430{
Laurence Lundblade241705e2018-12-30 18:56:14 -0800431 if(me->uError == QCBOR_SUCCESS) {
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800432 // If it is not Raw CBOR, add the type and the length
433 if(uMajorType != CBOR_MAJOR_NONE_TYPE_RAW) {
Laurence Lundbladeda532272019-04-07 11:40:17 -0700434 uint8_t uRealMajorType = uMajorType;
435 if(uRealMajorType == CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) {
436 uRealMajorType = CBOR_MAJOR_TYPE_BYTE_STRING;
437 }
438 AppendEncodedTypeAndNumber(me, uRealMajorType, Bytes.len);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700439 }
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800440
Laurence Lundbladeda532272019-04-07 11:40:17 -0700441 if(uMajorType != CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) {
442 // Actually add the bytes
443 UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), Bytes);
444 }
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800445
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800446 // Update the array counting if there is any nesting at all
447 me->uError = Nesting_Increment(&(me->nesting));
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700448 }
449}
450
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700451
Laurence Lundblade55a24832018-10-30 04:35:08 +0700452/*
Laurence Lundblade067035b2018-11-28 17:35:25 -0800453 Public functions for closing arrays and maps. See header qcbor.h
Laurence Lundblade55a24832018-10-30 04:35:08 +0700454 */
455void QCBOREncode_AddTag(QCBOREncodeContext *me, uint64_t uTag)
456{
Laurence Lundblade55a24832018-10-30 04:35:08 +0700457 AppendEncodedTypeAndNumber(me, CBOR_MAJOR_TYPE_OPTIONAL, uTag);
458}
459
460
Laurence Lundblade56230d12018-11-01 11:14:51 +0700461/*
Laurence Lundblade487930f2018-11-30 11:01:45 -0800462 Semi-private function. It is exposed to user of the interface,
463 but they will usually call one of the inline wrappers rather than this.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800464
Laurence Lundblade487930f2018-11-30 11:01:45 -0800465 See header qcbor.h
Laurence Lundblade56230d12018-11-01 11:14:51 +0700466 */
Laurence Lundblade487930f2018-11-30 11:01:45 -0800467void QCBOREncode_AddType7(QCBOREncodeContext *me, size_t uSize, uint64_t uNum)
Laurence Lundblade55a24832018-10-30 04:35:08 +0700468{
Laurence Lundblade487930f2018-11-30 11:01:45 -0800469 if(me->uError == QCBOR_SUCCESS) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700470 if(uNum >= CBOR_SIMPLEV_RESERVED_START && uNum <= CBOR_SIMPLEV_RESERVED_END) {
471 me->uError = QCBOR_ERR_UNSUPPORTED;
472 } else {
473 // This function call takes care of endian swapping for the float / double
474 InsertEncodedTypeAndNumber(me,
475 // The major type for floats and doubles
476 CBOR_MAJOR_TYPE_SIMPLE,
477 // size makes sure floats with zeros encode correctly
478 (int)uSize,
479 // Bytes of the floating point number as a uint
480 uNum,
481 // end position because this is append
482 UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800483
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700484 me->uError = Nesting_Increment(&(me->nesting));
485 }
Laurence Lundblade487930f2018-11-30 11:01:45 -0800486 }
Laurence Lundblade55a24832018-10-30 04:35:08 +0700487}
488
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700489
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700490/*
Laurence Lundblade067035b2018-11-28 17:35:25 -0800491 Public functions for closing arrays and maps. See header qcbor.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700492 */
Laurence Lundblade067035b2018-11-28 17:35:25 -0800493void QCBOREncode_AddDouble(QCBOREncodeContext *me, double dNum)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700494{
Laurence Lundblade067035b2018-11-28 17:35:25 -0800495 const IEEE754_union uNum = IEEE754_DoubleToSmallest(dNum);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800496
Laurence Lundblade487930f2018-11-30 11:01:45 -0800497 QCBOREncode_AddType7(me, uNum.uSize, uNum.uValue);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800498}
499
500
501/*
502 Semi-public function. It is exposed to user of the interface,
503 but they will usually call one of the inline wrappers rather than this.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800504
Laurence Lundblade067035b2018-11-28 17:35:25 -0800505 See header qcbor.h
506*/
507void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType)
508{
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800509 // Add one item to the nesting level we are in for the new map or array
510 me->uError = Nesting_Increment(&(me->nesting));
Laurence Lundblade241705e2018-12-30 18:56:14 -0800511 if(me->uError == QCBOR_SUCCESS) {
Laurence Lundbladed39cd392019-01-11 18:17:38 -0800512 // The offset where the length of an array or map will get written
513 // is stored in a uint32_t, not a size_t to keep stack usage smaller. This
514 // checks to be sure there is no wrap around when recording the offset.
515 // Note that on 64-bit machines CBOR larger than 4GB can be encoded as long as no
516 // array / map offsets occur past the 4GB mark, but the public interface
517 // says that the maximum is 4GB to keep the discussion simpler.
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800518 size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
Laurence Lundbladed39cd392019-01-11 18:17:38 -0800519
520 // QCBOR_MAX_ARRAY_OFFSET is slightly less than UINT32_MAX so this
521 // code can run on a 32-bit machine and tests can pass on a 32-bit
522 // machine. If it was exactly UINT32_MAX, then this code would
523 // not compile or run on a 32-bit machine and an #ifdef or some
524 // machine size detection would be needed reducing portability.
525 if(uEndPosition >= QCBOR_MAX_ARRAY_OFFSET) {
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800526 me->uError = QCBOR_ERR_BUFFER_TOO_LARGE;
Laurence Lundbladed39cd392019-01-11 18:17:38 -0800527
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800528 } else {
Laurence Lundbladed39cd392019-01-11 18:17:38 -0800529 // Increase nesting level because this is a map or array.
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800530 // Cast from size_t to uin32_t is safe because of check above
531 me->uError = Nesting_Increase(&(me->nesting), uMajorType, (uint32_t)uEndPosition);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700532 }
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800533 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700534}
535
Jan Jongboom4a93a662019-07-25 08:44:58 +0200536/*
537 Semi-public function. It is exposed to user of the interface,
538 but they will usually call one of the inline wrappers rather than this.
539
540 See header qcbor.h
541*/
542void QCBOREncode_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType)
543{
544 // insert the indefinite length marker (0x9f for arrays, 0xbf for maps)
Jan Jongboom5d827882019-08-07 12:51:15 +0200545 InsertEncodedTypeAndNumber(me, uMajorType, 0, 0, UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
Jan Jongboom4a93a662019-07-25 08:44:58 +0200546
547 QCBOREncode_OpenMapOrArray(me, uMajorType);
548}
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700549
550/*
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700551 Public functions for closing arrays and maps. See header qcbor.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700552 */
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800553void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *me,
554 uint8_t uMajorType,
555 UsefulBufC *pWrappedCBOR)
Laurence Lundbladea954db92018-09-28 19:27:31 -0700556{
Laurence Lundblade241705e2018-12-30 18:56:14 -0800557 if(me->uError == QCBOR_SUCCESS) {
Laurence Lundbladea954db92018-09-28 19:27:31 -0700558 if(!Nesting_IsInNest(&(me->nesting))) {
559 me->uError = QCBOR_ERR_TOO_MANY_CLOSES;
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800560 } else if(Nesting_GetMajorType(&(me->nesting)) != uMajorType) {
Laurence Lundblade067035b2018-11-28 17:35:25 -0800561 me->uError = QCBOR_ERR_CLOSE_MISMATCH;
Laurence Lundbladea954db92018-09-28 19:27:31 -0700562 } else {
Laurence Lundblade56230d12018-11-01 11:14:51 +0700563 // When the array, map or bstr wrap was started, nothing was done
564 // except note the position of the start of it. This code goes back
565 // and inserts the actual CBOR array, map or bstr and its length.
566 // That means all the data that is in the array, map or wrapped
567 // needs to be slid to the right. This is done by UsefulOutBuf's
568 // insert function that is called from inside
569 // InsertEncodedTypeAndNumber()
570 const size_t uInsertPosition = Nesting_GetStartPos(&(me->nesting));
571 const size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
572 // This can't go negative because the UsefulOutBuf always only grows
573 // and never shrinks. UsefulOutBut itself also has defenses such that
574 // it won't write were it should not even if given hostile input lengths
575 const size_t uLenOfEncodedMapOrArray = uEndPosition - uInsertPosition;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800576
Laurence Lundblade56230d12018-11-01 11:14:51 +0700577 // Length is number of bytes for a bstr and number of items a for map & array
578 const size_t uLength = uMajorType == CBOR_MAJOR_TYPE_BYTE_STRING ?
Laurence Lundbladea954db92018-09-28 19:27:31 -0700579 uLenOfEncodedMapOrArray : Nesting_GetCount(&(me->nesting));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800580
Laurence Lundbladea954db92018-09-28 19:27:31 -0700581 // Actually insert
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700582 InsertEncodedTypeAndNumber(me,
Laurence Lundbladea954db92018-09-28 19:27:31 -0700583 uMajorType, // major type bstr, array or map
584 0, // no minimum length for encoding
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800585 uLength, // either len of bstr or num map / array items
Laurence Lundbladea954db92018-09-28 19:27:31 -0700586 uInsertPosition); // position in out buffer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800587
Laurence Lundbladea954db92018-09-28 19:27:31 -0700588 // Return pointer and length to the enclosed encoded CBOR. The intended
589 // use is for it to be hashed (e.g., SHA-256) in a COSE implementation.
590 // This must be used right away, as the pointer and length go invalid
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -0700591 // on any subsequent calls to this function because there might be calls to
592 // InsertEncodedTypeAndNumber() that slides data to the right.
Laurence Lundbladea954db92018-09-28 19:27:31 -0700593 if(pWrappedCBOR) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800594 const UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf));
Laurence Lundblade83f5b7f2019-04-06 11:22:37 -0700595 *pWrappedCBOR = UsefulBuf_Tail(PartialResult, uInsertPosition);
Laurence Lundbladea954db92018-09-28 19:27:31 -0700596 }
597 Nesting_Decrease(&(me->nesting));
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700598 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700599 }
600}
601
Jan Jongboom4a93a662019-07-25 08:44:58 +0200602/*
603 Public functions for closing arrays and maps. See header qcbor.h
604 */
605void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType, UsefulBufC *pWrappedCBOR)
606{
607 if(me->uError == QCBOR_SUCCESS) {
608 if(!Nesting_IsInNest(&(me->nesting))) {
609 me->uError = QCBOR_ERR_TOO_MANY_CLOSES;
610 } else if(Nesting_GetMajorType(&(me->nesting)) != uMajorType) {
611 me->uError = QCBOR_ERR_CLOSE_MISMATCH;
612 } else {
613 // insert the break marker (0xff for both arrays and maps)
614 InsertEncodedTypeAndNumber(me, CBOR_MAJOR_TYPE_SIMPLE, 0, CBOR_SIMPLE_BREAK, UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
615
616 // Return pointer and length to the enclosed encoded CBOR. The intended
617 // use is for it to be hashed (e.g., SHA-256) in a COSE implementation.
618 // This must be used right away, as the pointer and length go invalid
619 // on any subsequent calls to this function because there might be calls to
620 // InsertEncodedTypeAndNumber() that slides data to the right.
621 if(pWrappedCBOR) {
622 const UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf));
623 *pWrappedCBOR = UsefulBuf_Tail(PartialResult, UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
624 }
625
626 // Decrease nesting level
627 Nesting_Decrease(&(me->nesting));
628 }
629 }
630}
631
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700632
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700633/*
634 Public functions to finish and get the encoded result. See header qcbor.h
635 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700636QCBORError QCBOREncode_Finish(QCBOREncodeContext *me, UsefulBufC *pEncodedCBOR)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700637{
Laurence Lundbladef607a2a2019-07-05 21:25:25 -0700638 QCBORError uReturn = QCBOREncode_GetErrorState(me);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800639
Laurence Lundblade067035b2018-11-28 17:35:25 -0800640 if(uReturn != QCBOR_SUCCESS) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700641 goto Done;
Laurence Lundblade067035b2018-11-28 17:35:25 -0800642 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800643
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700644 if (Nesting_IsInNest(&(me->nesting))) {
Laurence Lundblade067035b2018-11-28 17:35:25 -0800645 uReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700646 goto Done;
647 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800648
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700649 *pEncodedCBOR = UsefulOutBuf_OutUBuf(&(me->OutBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800650
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700651Done:
Laurence Lundblade067035b2018-11-28 17:35:25 -0800652 return uReturn;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700653}
654
Laurence Lundblade0595e932018-11-02 22:22:47 +0700655
Laurence Lundbladef607a2a2019-07-05 21:25:25 -0700656
Laurence Lundblade067035b2018-11-28 17:35:25 -0800657/*
658 Public functions to finish and get the encoded result. See header qcbor.h
659 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700660QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *me, size_t *puEncodedLen)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700661{
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700662 UsefulBufC Enc;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800663
Laurence Lundblade30816f22018-11-10 13:40:22 +0700664 QCBORError nReturn = QCBOREncode_Finish(me, &Enc);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800665
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700666 if(nReturn == QCBOR_SUCCESS) {
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700667 *puEncodedLen = Enc.len;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700668 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800669
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700670 return nReturn;
671}
672
673
Laurence Lundblade067035b2018-11-28 17:35:25 -0800674
675
676/*
677 Notes on the code
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800678
Laurence Lundblade067035b2018-11-28 17:35:25 -0800679 CBOR Major Type Public Function
680 0 QCBOREncode_AddUInt64
681 0, 1 QCBOREncode_AddUInt64, QCBOREncode_AddInt64
682 2, 3 QCBOREncode_AddBuffer, Also QCBOREncode_OpenMapOrArray
683 4, 5 QCBOREncode_OpenMapOrArray
684 6 QCBOREncode_AddTag
Laurence Lundblade4e7bc682018-12-14 23:21:04 -0800685 7 QCBOREncode_AddDouble, QCBOREncode_AddType7
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800686
Laurence Lundblade241705e2018-12-30 18:56:14 -0800687 Object code sizes on X86 with LLVM compiler and -Os (Dec 30, 2018)
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800688
Laurence Lundblade9c097392018-12-30 13:52:24 -0800689 _QCBOREncode_Init 69
Laurence Lundblade067035b2018-11-28 17:35:25 -0800690 _QCBOREncode_AddUInt64 76
691 _QCBOREncode_AddInt64 87
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800692 _QCBOREncode_AddBuffer 113
Laurence Lundbladef970f1d2018-12-14 01:44:23 -0800693 _QCBOREncode_AddTag 27
Laurence Lundblade9c097392018-12-30 13:52:24 -0800694 _QCBOREncode_AddType7 87
Laurence Lundbladef970f1d2018-12-14 01:44:23 -0800695 _QCBOREncode_AddDouble 36
Laurence Lundblade1ef8b2d2018-12-14 23:13:34 -0800696 _QCBOREncode_OpenMapOrArray 103
Laurence Lundblade067035b2018-11-28 17:35:25 -0800697 _QCBOREncode_CloseMapOrArray 181
Laurence Lundbladef970f1d2018-12-14 01:44:23 -0800698 _InsertEncodedTypeAndNumber 190
Laurence Lundblade067035b2018-11-28 17:35:25 -0800699 _QCBOREncode_Finish 72
Laurence Lundbladef970f1d2018-12-14 01:44:23 -0800700 _QCBOREncode_FinishGetSize 70
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800701
Laurence Lundbladef970f1d2018-12-14 01:44:23 -0800702 Total is about 1.1KB
Laurence Lundblade2c40ab82018-12-30 14:20:29 -0800703
Laurence Lundblade067035b2018-11-28 17:35:25 -0800704 _QCBOREncode_CloseMapOrArray is larger because it has a lot
705 of nesting tracking to do and much of Nesting_ inlines
706 into it. It probably can't be reduced much.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800707
Laurence Lundblade067035b2018-11-28 17:35:25 -0800708 If the error returned by Nesting_Increment() can be ignored
709 because the limit is so high and the consequence of exceeding
710 is proved to be inconsequential, then a lot of if(me->uError)
711 instance can be removed, saving some code.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800712
Laurence Lundblade067035b2018-11-28 17:35:25 -0800713 */