blob: a5244a6c971829e693a74cb6af2d4a31afbb19ab [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.
3 Copyright (c) 2018, Laurence Lundblade.
4 All rights reserved.
Laurence Lundblade624405d2018-09-18 20:10:47 -07005
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 Lundblade624405d2018-09-18 20:10:47 -070019
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
35
36 DESCRIPTION: This file contains the implementation of QCBOR.
37
38 EDIT HISTORY FOR FILE:
39
40 This section contains comments describing changes made to the module.
41 Notice that changes are listed in reverse chronological order.
42
43 when who what, where, why
44 -------- ---- ---------------------------------------------------
Laurence Lundblade067035b2018-11-28 17:35:25 -080045 11/29/18 llundblade Rework to simpler handling of tags and labels.
46 11/9/18 llundblade Error codes are now enums.
47 11/1/18 llundblade Floating support.
48 10/31/18 llundblade Switch to one license that is almost BSD-3.
49 09/28/18 llundblade Added bstr wrapping feature for COSE implementation.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070050 02/05/18 llundbla Works on CPUs which require integer alignment.
51 Requires new version of UsefulBuf.
52 07/05/17 llundbla Add bstr wrapping of maps/arrays for COSE
53 03/01/17 llundbla More data types
54 11/13/16 llundbla Integrate most TZ changes back into github version.
55 09/30/16 gkanike Porting to TZ.
56 03/15/16 llundbla Initial Version.
57
58 =====================================================================================*/
59
60#include "qcbor.h"
Laurence Lundblade12d32c52018-09-19 11:25:27 -070061#include "ieee754.h"
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070062
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070063
64/*...... This is a ruler that is 80 characters long...........................*/
65
66
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070067/*
68 CBOR's two nesting types, arrays and maps, are tracked here. There is a
69 limit of QCBOR_MAX_ARRAY_NESTING to the number of arrays and maps
70 that can be nested in one encoding so the encoding context stays
71 small enough to fit on the stack.
72
73 When an array / map is opened, pCurrentNesting points to the element
74 in pArrays that records the type, start position and accumluates a
75 count of the number of items added. When closed the start position is
76 used to go back and fill in the type and number of items in the array
77 / map.
78
79 Encoded output be just items like ints and strings that are
80 not part of any array / map. That is, the first thing encoded
81 does not have to be an array or a map.
82 */
83inline static void Nesting_Init(QCBORTrackNesting *pNesting)
84{
85 // assumes pNesting has been zeroed
86 pNesting->pCurrentNesting = &pNesting->pArrays[0];
87 // Implied CBOR array at the top nesting level. This is never returned,
88 // but makes the item count work correctly.
89 pNesting->pCurrentNesting->uMajorType = CBOR_MAJOR_TYPE_ARRAY;
90}
91
Laurence Lundblade30816f22018-11-10 13:40:22 +070092inline static QCBORError Nesting_Increase(QCBORTrackNesting *pNesting, uint8_t uMajorType, uint32_t uPos)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070093{
Laurence Lundblade30816f22018-11-10 13:40:22 +070094 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070095
96 if(pNesting->pCurrentNesting == &pNesting->pArrays[QCBOR_MAX_ARRAY_NESTING]) {
97 // trying to open one too many
98 nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
99 } else {
100 pNesting->pCurrentNesting++;
101 pNesting->pCurrentNesting->uCount = 0;
102 pNesting->pCurrentNesting->uStart = uPos;
103 pNesting->pCurrentNesting->uMajorType = uMajorType;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700104 }
105 return nReturn;
106}
107
108inline static void Nesting_Decrease(QCBORTrackNesting *pNesting)
109{
110 pNesting->pCurrentNesting--;
111}
112
Laurence Lundblade30816f22018-11-10 13:40:22 +0700113inline static QCBORError Nesting_Increment(QCBORTrackNesting *pNesting, uint16_t uAmount)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700114{
115 if(uAmount >= QCBOR_MAX_ITEMS_IN_ARRAY - pNesting->pCurrentNesting->uCount) {
116 return QCBOR_ERR_ARRAY_TOO_LONG;
117 }
118
119 pNesting->pCurrentNesting->uCount += uAmount;
120 return QCBOR_SUCCESS;
121}
122
123inline static uint16_t Nesting_GetCount(QCBORTrackNesting *pNesting)
124{
125 // The nesting count recorded is always the actual number of individiual
126 // data items in the array or map. For arrays CBOR uses the actual item
127 // count. For maps, CBOR uses the number of pairs. This function returns
128 // the number needed for the CBOR encoding, so it divides the number of
129 // items by two for maps to get the number of pairs. This implementation
130 // takes advantage of the map major type being one larger the array major
131 // type, hence the subtraction returns either 1 or 2.
132 return pNesting->pCurrentNesting->uCount / (pNesting->pCurrentNesting->uMajorType - CBOR_MAJOR_TYPE_ARRAY+1);
133}
134
135inline static uint32_t Nesting_GetStartPos(QCBORTrackNesting *pNesting)
136{
137 return pNesting->pCurrentNesting->uStart;
138}
139
140inline static uint8_t Nesting_GetMajorType(QCBORTrackNesting *pNesting)
141{
142 return pNesting->pCurrentNesting->uMajorType;
143}
144
145inline static int Nesting_IsInNest(QCBORTrackNesting *pNesting)
146{
147 return pNesting->pCurrentNesting == &pNesting->pArrays[0] ? 0 : 1;
148}
149
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700150
151
152
153/*
154 Error tracking plan -- Errors are tracked internally and not returned
155 until Finish is called. The CBOR errors are in me->uError.
Laurence Lundblade067035b2018-11-28 17:35:25 -0800156 UsefulOutBuf also tracks whether the buffer is full or not in its
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700157 context. Once either of these errors is set they are never
158 cleared. Only Init() resets them. Or said another way, they must
159 never be cleared or we'll tell the caller all is good when it is not.
160
161 Only one error code is reported by Finish() even if there are
162 multiple errors. The last one set wins. The caller might have to fix
163 one error to reveal the next one they have to fix. This is OK.
164
165 The buffer full error tracked by UsefulBuf is only pulled out of
166 UsefulBuf in Finish() so it is the one that usually wins. UsefulBuf
167 will never go off the end of the buffer even if it is called again
168 and again when full.
169
170 It is really tempting to not check for overflow on the count in the
171 number of items in an array. It would save a lot of code, it is
172 extremely unlikely that any one will every put 65,000 items in an
173 array, and the only bad thing that would happen is the CBOR would be
174 bogus. Once we prove that is the only consequence, then we can make
175 the change.
176
177 Since this does not parse any input, you could in theory remove all
178 error checks in this code if you knew the caller called it
179 correctly. Maybe someday CDDL or some such language will be able to
180 generate the code to call this and the calling code would always be
Laurence Lundblade56230d12018-11-01 11:14:51 +0700181 correct. This could also automatically size some of the data
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700182 structures like array/map nesting resulting in some good memory
183 savings.
Laurence Lundblade067035b2018-11-28 17:35:25 -0800184
185 Errors returned here fall into three categories:
186
187 Sizes
188 QCBOR_ERR_BUFFER_TOO_LARGE -- A buffer passed in > UINT32_MAX
189 QCBOR_ERR_BUFFER_TOO_SMALL -- output buffer too small
190
191 QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -- Too many opens without closes
192 QCBOR_ERR_ARRAY_TOO_LONG -- Too many things added to an array/map
193
194 Nesting constructed incorrectly
195 QCBOR_ERR_TOO_MANY_CLOSES -- more close calls than opens
196 QCBOR_ERR_CLOSE_MISMATCH -- Type of close does not match open
197 QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN -- Finish called without enough closes
198
199 Bad data
200 QCBOR_ERR_BAD_SIMPLE -- Simple value integer not valid
201
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700202 */
203
204
205
206
207/*
208 Public function for initialization. See header qcbor.h
209 */
Laurence Lundblade2296db52018-09-14 18:08:39 -0700210void QCBOREncode_Init(QCBOREncodeContext *me, UsefulBuf Storage)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700211{
212 memset(me, 0, sizeof(QCBOREncodeContext));
Laurence Lundblade2296db52018-09-14 18:08:39 -0700213 if(Storage.len > UINT32_MAX) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700214 me->uError = QCBOR_ERR_BUFFER_TOO_LARGE;
215 } else {
Laurence Lundblade2296db52018-09-14 18:08:39 -0700216 UsefulOutBuf_Init(&(me->OutBuf), Storage);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700217 Nesting_Init(&(me->nesting));
218 }
219}
220
221
222
223
224/*
225 All CBOR data items have a type and a number. The number is either
226 the value of the item for integer types, the length of the content
227 for string, byte, array and map types, a tag for major type 6, and
228 has serveral uses for major type 7.
229
230 This function encodes the type and the number. There are several
231 encodings for the number depending on how large it is and how it is
232 used.
233
234 Every encoding of the type and number has at least one byte, the
235 "initial byte".
236
237 The top three bits of the initial byte are the major type for the
238 CBOR data item. The eight major types defined by the standard are
239 defined as CBOR_MAJOR_TYPE_xxxx in qcbor.h.
240
241 The remaining five bits, known as "additional information", and
242 possibly more bytes encode the number. If the number is less than 24,
243 then it is encoded entirely in the five bits. This is neat because it
244 allows you to encode an entire CBOR data item in 1 byte for many
245 values and types (integers 0-23, true, false, and tags).
246
247 If the number is larger than 24, then it is encoded in 1,2,4 or 8
248 additional bytes, with the number of these bytes indicated by the
249 values of the 5 bits 24, 25, 25 and 27.
250
251 It is possible to encode a particular number in many ways with this
252 representation. This implementation always uses the smallest
253 possible representation. This is also the suggestion made in the RFC
254 for cannonical CBOR.
255
256 This function inserts them into the output buffer at the specified
Laurence Lundblade067035b2018-11-28 17:35:25 -0800257 position. AppendEncodedTypeAndNumber() appends to the end.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700258
259 This function takes care of converting to network byte order.
260
261 This function is also used to insert floats and doubles. Before this
262 function is called the float or double must be copied into a
263 uint64_t. That is how they are passed in. They are then converted to
264 network byte order correctly. The uMinLen param makes sure that even
Laurence Lundblade067035b2018-11-28 17:35:25 -0800265 if all the digits of a halft, float or double are 0 it is still correctly
266 encoded in 2, 4 or 8 bytes.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700267
268 */
Laurence Lundblade351bfea2018-12-07 21:34:31 +0900269#ifdef FORMAL
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700270static void InsertEncodedTypeAndNumber(QCBOREncodeContext *me, uint8_t uMajorType, size_t uMinLen, uint64_t uNumber, size_t uPos)
271{
272 // No need to worry about integer overflow here because a) uMajorType is
273 // always generated internally, not by the caller, b) this is for CBOR
274 // _generation_, not parsing c) a mistake will result in bad CBOR generation,
275 // not a security vulnerability.
Laurence Lundblade56230d12018-11-01 11:14:51 +0700276 uMajorType <<= 5;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700277
278 if(uNumber > 0xffffffff || uMinLen >= 8) {
279 UsefulOutBuf_InsertByte(&(me->OutBuf), uMajorType + LEN_IS_EIGHT_BYTES, uPos);
280 UsefulOutBuf_InsertUint64(&(me->OutBuf), (uint64_t)uNumber, uPos+1);
281
282 } else if(uNumber > 0xffff || uMinLen >= 4) {
283 UsefulOutBuf_InsertByte(&(me->OutBuf), uMajorType + LEN_IS_FOUR_BYTES, uPos);
284 UsefulOutBuf_InsertUint32(&(me->OutBuf), (uint32_t)uNumber, uPos+1);
285
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700286 } else if (uNumber > 0xff || uMinLen>= 2) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700287 // Between 0 and 65535
288 UsefulOutBuf_InsertByte(&(me->OutBuf), uMajorType + LEN_IS_TWO_BYTES, uPos);
289 UsefulOutBuf_InsertUint16(&(me->OutBuf), (uint16_t)uNumber, uPos+1);
290
291 } else if(uNumber >= 24) {
292 // Between 0 and 255, but only between 24 and 255 is ever encoded here
293 UsefulOutBuf_InsertByte(&(me->OutBuf), uMajorType + LEN_IS_ONE_BYTE, uPos);
294 UsefulOutBuf_InsertByte(&(me->OutBuf), (uint8_t)uNumber, uPos+1);
295
296 } else {
297 // Between 0 and 23
298 UsefulOutBuf_InsertByte(&(me->OutBuf), uMajorType + (uint8_t)uNumber, uPos);
299 }
300}
Laurence Lundblade351bfea2018-12-07 21:34:31 +0900301#else
302
303#include <arpa/inet.h>
304
305
306static void InsertEncodedTypeAndNumber(QCBOREncodeContext *me, uint8_t uMajorType, size_t uMinLen, uint64_t uNumber, size_t uPos)
307{
308 uint8_t bytes[9];
309 size_t bytesLen;
310 // No need to worry about integer overflow here because a) uMajorType is
311 // always generated internally, not by the caller, b) this is for CBOR
312 // _generation_, not parsing c) a mistake will result in bad CBOR generation,
313 // not a security vulnerability.
314 uMajorType <<= 5;
315
316 if(uNumber > 0xffffffff || uMinLen >= 8) {
317 bytes[0] = uMajorType + LEN_IS_EIGHT_BYTES;
318 uNumber = htonll(uNumber);
319 memcpy(&bytes[1], &uNumber, sizeof(uint64_t));
320 bytesLen = 1 + sizeof(uint64_t);
321
322 } else if(uNumber > 0xffff || uMinLen >= 4) {
323 bytes[0] = uMajorType + LEN_IS_FOUR_BYTES;
324 uint32_t uNumber32 = htonl(uNumber);
325 memcpy(&bytes[1], &uNumber32, sizeof(uint32_t));
326 bytesLen = 1 + sizeof(uint32_t);
327
328 } else if (uNumber > 0xff || uMinLen>= 2) {
329 // Between 0 and 65535
330 bytes[0] = uMajorType + LEN_IS_TWO_BYTES;
331 uint16_t uNumber16 = htons(uNumber);
332 memcpy(&bytes[1], &uNumber16, sizeof(uint16_t));
333 bytesLen = sizeof(uint16_t) + 1;
334
335 } else if(uNumber >= 24) {
336 // Between 0 and 255, but only between 24 and 255 is ever encoded here
337 bytes[0] = uMajorType + LEN_IS_ONE_BYTE;
338 uint8_t uNumber8 = uNumber;
339 memcpy(&bytes[1], &uNumber8, sizeof(uint8_t));
340 bytesLen = sizeof(uint8_t) + 1;
341
342 } else {
343 // Between 0 and 23
344 bytes[0] = uMajorType + (uint8_t)uNumber;
345 bytesLen = 1;
346 }
347 UsefulOutBuf_InsertData(&(me->OutBuf), bytes, bytesLen, uPos);
348}
349#endif
350
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700351
352
353/*
354 Append the type and number info to the end of the buffer.
355
356 See InsertEncodedTypeAndNumber() function above for details
357*/
358inline static void AppendEncodedTypeAndNumber(QCBOREncodeContext *me, uint8_t uMajorType, uint64_t uNumber)
359{
360 // An append is an insert at the end.
361 InsertEncodedTypeAndNumber(me, uMajorType, 0, uNumber, UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
362}
363
364
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700365
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700366/*
Laurence Lundblade067035b2018-11-28 17:35:25 -0800367 Public functions for closing arrays and maps. See header qcbor.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700368 */
Laurence Lundblade067035b2018-11-28 17:35:25 -0800369void QCBOREncode_AddUInt64(QCBOREncodeContext *me, uint64_t uValue)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700370{
Laurence Lundblade067035b2018-11-28 17:35:25 -0800371 if(me->uError == QCBOR_SUCCESS) {
372 AppendEncodedTypeAndNumber(me, CBOR_MAJOR_TYPE_POSITIVE_INT, uValue);
373 me->uError = Nesting_Increment(&(me->nesting), 1);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700374 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700375}
376
Laurence Lundblade56230d12018-11-01 11:14:51 +0700377
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700378/*
Laurence Lundblade067035b2018-11-28 17:35:25 -0800379 Public functions for closing arrays and maps. See header qcbor.h
380 */
381void QCBOREncode_AddInt64(QCBOREncodeContext *me, int64_t nNum)
382{
383 if(me->uError == QCBOR_SUCCESS) {
384 uint8_t uMajorType;
385 uint64_t uValue;
386
387 if(nNum < 0) {
388 uValue = (uint64_t)(-nNum - 1); // This is the way negative ints work in CBOR. -1 encodes as 0x00 with major type negative int.
389 uMajorType = CBOR_MAJOR_TYPE_NEGATIVE_INT;
390 } else {
391 uValue = (uint64_t)nNum;
392 uMajorType = CBOR_MAJOR_TYPE_POSITIVE_INT;
393 }
394
395 AppendEncodedTypeAndNumber(me, uMajorType, uValue);
396 me->uError = Nesting_Increment(&(me->nesting), 1);
397 }
398}
399
400
401/*
402 Semi-private function. It is exposed to user of the interface,
403 but they will usually call one of the inline wrappers rather than this.
404
405 See header qcbor.h
406
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700407 Does the work of adding some bytes to the CBOR output. Works for a
408 byte and text strings, which are the same in in CBOR though they have
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700409 different major types. This is also used to insert raw
410 pre-encoded CBOR.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700411 */
Laurence Lundblade067035b2018-11-28 17:35:25 -0800412void QCBOREncode_AddBuffer(QCBOREncodeContext *me, uint8_t uMajorType, UsefulBufC Bytes)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700413{
414 if(Bytes.len >= UINT32_MAX) {
Laurence Lundblade56230d12018-11-01 11:14:51 +0700415 // This implementation doesn't allow buffers larger than UINT32_MAX.
416 // This is primarily because QCBORTrackNesting.pArrays[].uStart is
417 // an uint32 rather than size_t to keep the stack usage down. Also
418 // it is entirely impractical to create tokens bigger than 4GB in
419 // contiguous RAM
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700420 me->uError = QCBOR_ERR_BUFFER_TOO_LARGE;
421
422 } else {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700423 if(!me->uError) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700424 // If it is not Raw CBOR, add the type and the length
425 if(uMajorType != CBOR_MAJOR_NONE_TYPE_RAW) {
Laurence Lundblade067035b2018-11-28 17:35:25 -0800426 AppendEncodedTypeAndNumber(me, uMajorType, Bytes.len);
Laurence Lundblade56230d12018-11-01 11:14:51 +0700427 // The increment in uPos is to account for bytes added for
428 // type and number so the buffer being added goes to the
429 // right place
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700430 }
431
432 // Actually add the bytes
Laurence Lundblade067035b2018-11-28 17:35:25 -0800433 UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), Bytes);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700434
435 // Update the array counting if there is any nesting at all
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700436 me->uError = Nesting_Increment(&(me->nesting), 1);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700437 }
438 }
439}
440
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700441
Laurence Lundblade55a24832018-10-30 04:35:08 +0700442/*
Laurence Lundblade067035b2018-11-28 17:35:25 -0800443 Public functions for closing arrays and maps. See header qcbor.h
Laurence Lundblade55a24832018-10-30 04:35:08 +0700444 */
445void QCBOREncode_AddTag(QCBOREncodeContext *me, uint64_t uTag)
446{
Laurence Lundblade55a24832018-10-30 04:35:08 +0700447 AppendEncodedTypeAndNumber(me, CBOR_MAJOR_TYPE_OPTIONAL, uTag);
448}
449
450
Laurence Lundblade487930f2018-11-30 11:01:45 -0800451
452
Laurence Lundblade56230d12018-11-01 11:14:51 +0700453/*
Laurence Lundblade487930f2018-11-30 11:01:45 -0800454 Semi-private function. It is exposed to user of the interface,
455 but they will usually call one of the inline wrappers rather than this.
456
457 See header qcbor.h
Laurence Lundblade56230d12018-11-01 11:14:51 +0700458 */
Laurence Lundblade487930f2018-11-30 11:01:45 -0800459void QCBOREncode_AddType7(QCBOREncodeContext *me, size_t uSize, uint64_t uNum)
Laurence Lundblade55a24832018-10-30 04:35:08 +0700460{
Laurence Lundblade487930f2018-11-30 11:01:45 -0800461 if(me->uError == QCBOR_SUCCESS) {
462 // This function call takes care of endian swapping for the float / double
463 InsertEncodedTypeAndNumber(me,
464 CBOR_MAJOR_TYPE_SIMPLE, // The major type for
465 // floats and doubles
466 uSize, // min size / tells
467 // encoder to do it right
468 uNum, // Bytes of the floating
469 // point number as a uint
470 UsefulOutBuf_GetEndPosition(&(me->OutBuf))); // end position for append
471
472 me->uError = Nesting_Increment(&(me->nesting), 1);
473 }
Laurence Lundblade55a24832018-10-30 04:35:08 +0700474}
475
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700476
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700477/*
Laurence Lundblade067035b2018-11-28 17:35:25 -0800478 Public functions for closing arrays and maps. See header qcbor.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700479 */
Laurence Lundblade067035b2018-11-28 17:35:25 -0800480void QCBOREncode_AddDouble(QCBOREncodeContext *me, double dNum)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700481{
Laurence Lundblade067035b2018-11-28 17:35:25 -0800482 const IEEE754_union uNum = IEEE754_DoubleToSmallest(dNum);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700483
Laurence Lundblade487930f2018-11-30 11:01:45 -0800484 QCBOREncode_AddType7(me, uNum.uSize, uNum.uValue);
Laurence Lundblade067035b2018-11-28 17:35:25 -0800485}
486
487
488/*
489 Semi-public function. It is exposed to user of the interface,
490 but they will usually call one of the inline wrappers rather than this.
491
492 See header qcbor.h
493*/
494void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType)
495{
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700496 // Add one item to the nesting level we are in for the new map or array
497 me->uError = Nesting_Increment(&(me->nesting), 1);
498 if(!me->uError) {
499 // Increase nesting level because this is a map or array
500 // Cast from size_t to uin32_t is safe because the UsefulOutBuf
501 // size is limited to UINT32_MAX in QCBOR_Init().
Laurence Lundbladea954db92018-09-28 19:27:31 -0700502 me->uError = Nesting_Increase(&(me->nesting), uMajorType, (uint32_t)UsefulOutBuf_GetEndPosition(&(me->OutBuf)));
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700503 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700504}
505
506
507/*
Laurence Lundbladecafcfe12018-10-31 21:59:50 +0700508 Public functions for closing arrays and maps. See header qcbor.h
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700509 */
Laurence Lundblade067035b2018-11-28 17:35:25 -0800510void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType, UsefulBufC *pWrappedCBOR)
Laurence Lundbladea954db92018-09-28 19:27:31 -0700511{
512 if(!me->uError) {
513 if(!Nesting_IsInNest(&(me->nesting))) {
514 me->uError = QCBOR_ERR_TOO_MANY_CLOSES;
515 } else if( Nesting_GetMajorType(&(me->nesting)) != uMajorType) {
Laurence Lundblade067035b2018-11-28 17:35:25 -0800516 me->uError = QCBOR_ERR_CLOSE_MISMATCH;
Laurence Lundbladea954db92018-09-28 19:27:31 -0700517 } else {
Laurence Lundblade56230d12018-11-01 11:14:51 +0700518 // When the array, map or bstr wrap was started, nothing was done
519 // except note the position of the start of it. This code goes back
520 // and inserts the actual CBOR array, map or bstr and its length.
521 // That means all the data that is in the array, map or wrapped
522 // needs to be slid to the right. This is done by UsefulOutBuf's
523 // insert function that is called from inside
524 // InsertEncodedTypeAndNumber()
525 const size_t uInsertPosition = Nesting_GetStartPos(&(me->nesting));
526 const size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf));
527 // This can't go negative because the UsefulOutBuf always only grows
528 // and never shrinks. UsefulOutBut itself also has defenses such that
529 // it won't write were it should not even if given hostile input lengths
530 const size_t uLenOfEncodedMapOrArray = uEndPosition - uInsertPosition;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700531
Laurence Lundblade56230d12018-11-01 11:14:51 +0700532 // Length is number of bytes for a bstr and number of items a for map & array
533 const size_t uLength = uMajorType == CBOR_MAJOR_TYPE_BYTE_STRING ?
Laurence Lundbladea954db92018-09-28 19:27:31 -0700534 uLenOfEncodedMapOrArray : Nesting_GetCount(&(me->nesting));
535
536 // Actually insert
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700537 InsertEncodedTypeAndNumber(me,
Laurence Lundbladea954db92018-09-28 19:27:31 -0700538 uMajorType, // major type bstr, array or map
539 0, // no minimum length for encoding
540 uLength, // either len of bstr or num items in array or map
541 uInsertPosition); // position in out buffer
542
543 // Return pointer and length to the enclosed encoded CBOR. The intended
544 // use is for it to be hashed (e.g., SHA-256) in a COSE implementation.
545 // This must be used right away, as the pointer and length go invalid
546 // on any subsequent calls to this function because of the
547 // InsertEncodedTypeAndNumber() call that slides data to the right.
548 if(pWrappedCBOR) {
549 UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf));
Laurence Lundblade56230d12018-11-01 11:14:51 +0700550 size_t uBstrLen = UsefulOutBuf_GetEndPosition(&(me->OutBuf)) - uEndPosition;
Laurence Lundbladea954db92018-09-28 19:27:31 -0700551 *pWrappedCBOR = UsefulBuf_Tail(PartialResult, uInsertPosition+uBstrLen);
552 }
553 Nesting_Decrease(&(me->nesting));
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700554 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700555 }
556}
557
558
Laurence Lundblade56230d12018-11-01 11:14:51 +0700559
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700560/*
561 Public functions to finish and get the encoded result. See header qcbor.h
562 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700563QCBORError QCBOREncode_Finish(QCBOREncodeContext *me, UsefulBufC *pEncodedCBOR)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700564{
Laurence Lundblade067035b2018-11-28 17:35:25 -0800565 QCBORError uReturn = me->uError;
566
567 if(uReturn != QCBOR_SUCCESS) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700568 goto Done;
Laurence Lundblade067035b2018-11-28 17:35:25 -0800569 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700570
571 if (Nesting_IsInNest(&(me->nesting))) {
Laurence Lundblade067035b2018-11-28 17:35:25 -0800572 uReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700573 goto Done;
574 }
575
576 if(UsefulOutBuf_GetError(&(me->OutBuf))) {
577 // Stuff didn't fit in the buffer.
Laurence Lundblade56230d12018-11-01 11:14:51 +0700578 // This check catches this condition for all the appends and inserts
579 // so checks aren't needed when the appends and inserts are performed.
580 // And of course UsefulBuf will never overrun the input buffer given
581 // to it. No complex analysis of the error handling in this file is
582 // needed to know that is true. Just read the UsefulBuf code.
Laurence Lundblade067035b2018-11-28 17:35:25 -0800583 uReturn = QCBOR_ERR_BUFFER_TOO_SMALL;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700584 goto Done;
585 }
Laurence Lundblade2296db52018-09-14 18:08:39 -0700586
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700587 *pEncodedCBOR = UsefulOutBuf_OutUBuf(&(me->OutBuf));
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700588
589Done:
Laurence Lundblade067035b2018-11-28 17:35:25 -0800590 return uReturn;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700591}
592
Laurence Lundblade0595e932018-11-02 22:22:47 +0700593
Laurence Lundblade067035b2018-11-28 17:35:25 -0800594/*
595 Public functions to finish and get the encoded result. See header qcbor.h
596 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700597QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *me, size_t *puEncodedLen)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700598{
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700599 UsefulBufC Enc;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700600
Laurence Lundblade30816f22018-11-10 13:40:22 +0700601 QCBORError nReturn = QCBOREncode_Finish(me, &Enc);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700602
603 if(nReturn == QCBOR_SUCCESS) {
Laurence Lundbladeda3f0822018-09-18 19:49:02 -0700604 *puEncodedLen = Enc.len;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700605 }
606
607 return nReturn;
608}
609
610
Laurence Lundblade067035b2018-11-28 17:35:25 -0800611
612
613/*
614 Notes on the code
615
616 CBOR Major Type Public Function
617 0 QCBOREncode_AddUInt64
618 0, 1 QCBOREncode_AddUInt64, QCBOREncode_AddInt64
619 2, 3 QCBOREncode_AddBuffer, Also QCBOREncode_OpenMapOrArray
620 4, 5 QCBOREncode_OpenMapOrArray
621 6 QCBOREncode_AddTag
622 7 QCBOREncode_AddDouble, QCBOREncode_AddSimple
623
624 Object code sizes on X86 with LLVM compiler and -Os (Nov 27, 2018)
625
626 _QCBOREncode_Init 84
627 _QCBOREncode_AddUInt64 76
628 _QCBOREncode_AddInt64 87
629 _QCBOREncode_AddBuffer 131
630 _QCBOREncode_AddSimple 30
631 _AppendType7 83
632 _QCBOREncode_OpenMapOrArray 89
633 _QCBOREncode_CloseMapOrArray 181
634 _InsertEncodedTypeAndNumber 480
635 _QCBOREncode_Finish 72
636
637 Total is about 1.4KB (including FinishGetSize and AddTag and AddDouble)
638
639 _InsertEncodedTypeAndNumber is large because a lot of UsefulBuf
640 code inlines into it including the conversion to network byte
641 order. This could be optimized to at least half the size, but
642 code would probably not be quite as clean.
643
644 _QCBOREncode_CloseMapOrArray is larger because it has a lot
645 of nesting tracking to do and much of Nesting_ inlines
646 into it. It probably can't be reduced much.
647
648 If the error returned by Nesting_Increment() can be ignored
649 because the limit is so high and the consequence of exceeding
650 is proved to be inconsequential, then a lot of if(me->uError)
651 instance can be removed, saving some code.
652
653 */
654
655
Laurence Lundblade351bfea2018-12-07 21:34:31 +0900656/*
657 _InsertEncodedTypeAndNumber: ## @InsertEncodedTypeAndNumber
658Lfunc_begin10:
659 .loc 7 307 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:307:0
660 .cfi_startproc
661## BB#0:
662 pushq %rbp
663Lcfi30:
664 .cfi_def_cfa_offset 16
665Lcfi31:
666 .cfi_offset %rbp, -16
667 movq %rsp, %rbp
668Lcfi32:
669 subq $80, %rsp
670 movb %sil, %al
671 movl $4294967295, %esi ## imm = 0xFFFFFFFF
672 movl %esi, %r9d
673 movq (%r10), %r10
674 movq %r10, -8(%rbp)
675 movq %rdi, -32(%rbp)
676 movb %al, -33(%rbp)
677 movq %rdx, -48(%rbp)
678 movq %rcx, -56(%rbp)
679 movq %r8, -64(%rbp)
680Ltmp50:
681 .loc 7 314 15 prologue_end ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:314:15
682 movzbl -33(%rbp), %esi
683 shll $5, %esi
684 movb %sil, %al
685 movb %al, -33(%rbp)
686Ltmp51:
687 .loc 7 316 15 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:316:15
688 cmpq %r9, -56(%rbp)
689 .loc 7 316 28 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:316:28
690 ja LBB10_2
691## BB#1:
692 .loc 7 316 39 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:316:39
693 cmpq $8, -48(%rbp)
694Ltmp52:
695 .loc 7 316 7 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:316:7
696 jb LBB10_3
697LBB10_2:
698Ltmp53:
699 .loc 7 317 19 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:317:19
700 movzbl -33(%rbp), %eax
701 .loc 7 317 30 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:317:30
702 addl $27, %eax
703 .loc 7 317 19 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:317:19
704 movb %al, %cl
705 .loc 7 317 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:317:16
706 movb %cl, -17(%rbp)
707 .loc 7 318 17 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:318:17
708 movq -56(%rbp), %rdi
709 callq __OSSwapInt64
710 .loc 7 318 15 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:318:15
711 movq %rax, -56(%rbp)
712 .loc 7 319 7 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:319:7
713 movq -56(%rbp), %rax
714 movq %rax, -16(%rbp)
715 .loc 7 320 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:320:16
716 movq $9, -72(%rbp)
717 .loc 7 322 4 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:322:4
718 jmp LBB10_15
719Ltmp54:
720LBB10_3:
721 .loc 7 322 22 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:322:22
722 cmpq $65535, -56(%rbp) ## imm = 0xFFFF
723 .loc 7 322 31 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:322:31
724 ja LBB10_5
725## BB#4:
726 .loc 7 322 42 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:322:42
727 cmpq $4, -48(%rbp)
728Ltmp55:
729 .loc 7 322 14 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:322:14
730 jb LBB10_6
731LBB10_5:
732Ltmp56:
733 .loc 7 323 19 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:323:19
734 movzbl -33(%rbp), %eax
735 .loc 7 323 30 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:323:30
736 addl $26, %eax
737 .loc 7 323 19 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:323:19
738 movb %al, %cl
739 .loc 7 323 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:323:16
740 movb %cl, -17(%rbp)
741 .loc 7 324 28 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:324:28
742 movq -56(%rbp), %rdx
743 movl %edx, %eax
744 movl %eax, %edi
745 callq __OSSwapInt32
746 .loc 7 324 16 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:324:16
747 movl %eax, -76(%rbp)
748 .loc 7 325 7 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:325:7
749 movl -76(%rbp), %eax
750 movl %eax, -16(%rbp)
751 .loc 7 326 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:326:16
752 movq $5, -72(%rbp)
753 .loc 7 328 4 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:328:4
754 jmp LBB10_14
755Ltmp57:
756LBB10_6:
757 .loc 7 328 23 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:328:23
758 cmpq $255, -56(%rbp)
759 .loc 7 328 30 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:328:30
760 ja LBB10_8
761## BB#7:
762 .loc 7 328 40 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:328:40
763 cmpq $2, -48(%rbp)
764Ltmp58:
765 .loc 7 328 15 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:328:15
766 jb LBB10_9
767LBB10_8:
768Ltmp59:
769 .loc 7 330 19 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:330:19
770 movzbl -33(%rbp), %eax
771 .loc 7 330 30 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:330:30
772 addl $25, %eax
773 .loc 7 330 19 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:330:19
774 movb %al, %cl
775 .loc 7 330 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:330:16
776 movb %cl, -17(%rbp)
777 .loc 7 331 28 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:331:28
778 movq -56(%rbp), %rdx
779 movw %dx, %si
780 movzwl %si, %edi
781 callq __OSSwapInt16
782 movzwl %ax, %edi
783 movw %di, %ax
784 .loc 7 331 16 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:331:16
785 movw %ax, -78(%rbp)
786 .loc 7 332 7 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:332:7
787 movw -78(%rbp), %ax
788 movw %ax, -16(%rbp)
789 .loc 7 333 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:333:16
790 movq $3, -72(%rbp)
791 .loc 7 335 4 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:335:4
792 jmp LBB10_13
793Ltmp60:
794LBB10_9:
795 .loc 7 335 22 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:335:22
796 cmpq $24, -56(%rbp)
797Ltmp61:
798 .loc 7 335 14 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:335:14
799 jb LBB10_11
800## BB#10:
801Ltmp62:
802 .loc 7 337 19 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:337:19
803 movzbl -33(%rbp), %eax
804 .loc 7 337 30 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:337:30
805 addl $24, %eax
806 .loc 7 337 19 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:337:19
807 movb %al, %cl
808 .loc 7 337 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:337:16
809 movb %cl, -17(%rbp)
810 .loc 7 338 26 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:338:26
811 movq -56(%rbp), %rdx
812 movb %dl, %cl
813 .loc 7 338 15 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:338:15
814 movb %cl, -79(%rbp)
815 .loc 7 339 7 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:339:7
816 movb -79(%rbp), %cl
817 movb %cl, -16(%rbp)
818 .loc 7 340 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:340:16
819 movq $2, -72(%rbp)
820 .loc 7 342 4 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:342:4
821 jmp LBB10_12
822Ltmp63:
823LBB10_11:
824 .loc 7 344 18 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:18
825 movzbl -33(%rbp), %eax
826 .loc 7 344 40 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:40
827 movq -56(%rbp), %rcx
828 .loc 7 344 31 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:31
829 movb %cl, %dl
830 movzbl %dl, %esi
831 .loc 7 344 29 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:29
832 addl %esi, %eax
833 .loc 7 344 18 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:18
834 movb %al, %dl
835 .loc 7 344 16 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:344:16
836 movb %dl, -17(%rbp)
837 .loc 7 345 16 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:345:16
838 movq $1, -72(%rbp)
839Ltmp64:
840LBB10_12:
841 .loc 7 0 16 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:0:16
842 jmp LBB10_13
843LBB10_13:
844 jmp LBB10_14
845LBB10_14:
846 jmp LBB10_15
847LBB10_15:
848 leaq -17(%rbp), %rsi
849 .loc 7 347 30 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:347:30
850 movq -32(%rbp), %rdi
851 .loc 7 347 50 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:347:50
852 movq -72(%rbp), %rdx
853 .loc 7 347 60 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:347:60
854 movq -64(%rbp), %rcx
855 .loc 7 347 4 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:347:4
856 callq _UsefulOutBuf_InsertData
857 movq ___stack_chk_guard@GOTPCREL(%rip), %rcx
858 movq (%rcx), %rcx
859 movq -8(%rbp), %rdx
860 cmpq %rdx, %rcx
861 jne LBB10_17
862## BB#16:
863 .loc 7 348 1 is_stmt 1 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:348:1
864 addq $80, %rsp
865 popq %rbp
866 retq
867LBB10_17:
868 .loc 7 0 0 is_stmt 0 ## /Users/laurencelundblade/Code/QCBOR/master/src/qcbor_encode.c:0:0
869 callq ___stack_chk_fail
870Ltmp65:
871Lfunc_end10:
872 .cfi_endproc
Laurence Lundblade067035b2018-11-28 17:35:25 -0800873
874
875
876
877