Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 2 | Copyright (c) 2016-2018, The Linux Foundation. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3 | Copyright (c) 2018-2020, Laurence Lundblade. |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 4 | All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are |
| 8 | met: |
| 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 Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 19 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 20 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 31 | =============================================================================*/ |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 32 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 33 | /*============================================================================= |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 34 | FILE: qcbor_encode.c |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 35 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 36 | DESCRIPTION: This file contains the implementation of QCBOR. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 37 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 38 | EDIT HISTORY FOR FILE: |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 39 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 40 | This section contains comments describing changes made to the module. |
| 41 | Notice that changes are listed in reverse chronological order. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 42 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 43 | when who what, where, why |
| 44 | -------- ---- --------------------------------------------------- |
| 45 | 01/08/2020 llundblade Documentation corrections & improved code formatting. |
| 46 | 12/30/19 llundblade Add support for decimal fractions and bigfloats. |
| 47 | 8/7/19 llundblade Prevent encoding simple type reserved values 24..31 |
| 48 | 7/25/19 janjongboom Add indefinite length encoding for maps and arrays |
| 49 | 4/6/19 llundblade Wrapped bstr returned now includes the wrapping bstr |
| 50 | 12/30/18 llundblade Small efficient clever encode of type & argument. |
| 51 | 11/29/18 llundblade Rework to simpler handling of tags and labels. |
| 52 | 11/9/18 llundblade Error codes are now enums. |
| 53 | 11/1/18 llundblade Floating support. |
| 54 | 10/31/18 llundblade Switch to one license that is almost BSD-3. |
| 55 | 09/28/18 llundblade Added bstr wrapping feature for COSE implementation. |
| 56 | 02/05/18 llundbla Works on CPUs which require integer alignment. |
| 57 | Requires new version of UsefulBuf. |
| 58 | 07/05/17 llundbla Add bstr wrapping of maps/arrays for COSE |
| 59 | 03/01/17 llundbla More data types |
| 60 | 11/13/16 llundbla Integrate most TZ changes back into github version. |
| 61 | 09/30/16 gkanike Porting to TZ. |
| 62 | 03/15/16 llundbla Initial Version. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 63 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 64 | =============================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 65 | |
| 66 | #include "qcbor.h" |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 67 | #include "ieee754.h" |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 68 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 69 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 70 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 71 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 72 | Nesting -- This tracks the nesting of maps and arrays. |
| 73 | |
| 74 | The following functions and data type QCBORTrackNesting implement the |
| 75 | nesting management for encoding. |
| 76 | |
| 77 | CBOR's two nesting types, arrays and maps, are tracked here. There is |
| 78 | a limit of QCBOR_MAX_ARRAY_NESTING to the number of arrays and maps |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 79 | that can be nested in one encoding so the encoding context stays |
| 80 | small enough to fit on the stack. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 81 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 82 | When an array / map is opened, pCurrentNesting points to the element |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 83 | in pArrays that records the type, start position and accumulates a |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 84 | count of the number of items added. When closed the start position is |
| 85 | used to go back and fill in the type and number of items in the array |
| 86 | / map. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 87 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 88 | Encoded output can be just items like ints and strings that are not |
| 89 | part of any array / map. That is, the first thing encoded does not |
| 90 | have to be an array or a map. |
| 91 | |
| 92 | QCBOR has a special feature to allow constructing bstr-wrapped CBOR |
| 93 | directly into the output buffer, so an extra buffer for it is not |
| 94 | needed. This is implemented as nesting with type |
| 95 | CBOR_MAJOR_TYPE_BYTE_STRING and uses this code. Bstr-wrapped CBOR is |
| 96 | used by COSE for data that is to be hashed. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 97 | */ |
| 98 | inline static void Nesting_Init(QCBORTrackNesting *pNesting) |
| 99 | { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 100 | // Assumes pNesting has been zeroed |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 101 | pNesting->pCurrentNesting = &pNesting->pArrays[0]; |
| 102 | // Implied CBOR array at the top nesting level. This is never returned, |
| 103 | // but makes the item count work correctly. |
| 104 | pNesting->pCurrentNesting->uMajorType = CBOR_MAJOR_TYPE_ARRAY; |
| 105 | } |
| 106 | |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 107 | inline static QCBORError Nesting_Increase(QCBORTrackNesting *pNesting, |
| 108 | uint8_t uMajorType, |
| 109 | uint32_t uPos) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 110 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 111 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 112 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 113 | if(pNesting->pCurrentNesting == &pNesting->pArrays[QCBOR_MAX_ARRAY_NESTING]) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 114 | // Trying to open one too many |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 115 | nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP; |
| 116 | } else { |
| 117 | pNesting->pCurrentNesting++; |
| 118 | pNesting->pCurrentNesting->uCount = 0; |
| 119 | pNesting->pCurrentNesting->uStart = uPos; |
| 120 | pNesting->pCurrentNesting->uMajorType = uMajorType; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 121 | } |
| 122 | return nReturn; |
| 123 | } |
| 124 | |
| 125 | inline static void Nesting_Decrease(QCBORTrackNesting *pNesting) |
| 126 | { |
| 127 | pNesting->pCurrentNesting--; |
| 128 | } |
| 129 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 130 | inline static QCBORError Nesting_Increment(QCBORTrackNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 131 | { |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 132 | if(1 >= QCBOR_MAX_ITEMS_IN_ARRAY - pNesting->pCurrentNesting->uCount) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 133 | return QCBOR_ERR_ARRAY_TOO_LONG; |
| 134 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 135 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 136 | pNesting->pCurrentNesting->uCount += 1; |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 137 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 138 | return QCBOR_SUCCESS; |
| 139 | } |
| 140 | |
| 141 | inline static uint16_t Nesting_GetCount(QCBORTrackNesting *pNesting) |
| 142 | { |
| 143 | // The nesting count recorded is always the actual number of individiual |
| 144 | // data items in the array or map. For arrays CBOR uses the actual item |
| 145 | // count. For maps, CBOR uses the number of pairs. This function returns |
| 146 | // the number needed for the CBOR encoding, so it divides the number of |
| 147 | // items by two for maps to get the number of pairs. This implementation |
| 148 | // takes advantage of the map major type being one larger the array major |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 149 | // type, hence uDivisor is either 1 or 2. |
| 150 | const uint16_t uDivisor = pNesting->pCurrentNesting->uMajorType - CBOR_MAJOR_TYPE_ARRAY+1; |
| 151 | |
| 152 | return pNesting->pCurrentNesting->uCount / uDivisor; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | inline static uint32_t Nesting_GetStartPos(QCBORTrackNesting *pNesting) |
| 156 | { |
| 157 | return pNesting->pCurrentNesting->uStart; |
| 158 | } |
| 159 | |
| 160 | inline static uint8_t Nesting_GetMajorType(QCBORTrackNesting *pNesting) |
| 161 | { |
| 162 | return pNesting->pCurrentNesting->uMajorType; |
| 163 | } |
| 164 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 165 | inline static bool Nesting_IsInNest(QCBORTrackNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 166 | { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 167 | return pNesting->pCurrentNesting == &pNesting->pArrays[0] ? false : true; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 170 | |
| 171 | |
| 172 | |
| 173 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 174 | Encoding of the major CBOR types is by these functions: |
| 175 | |
| 176 | CBOR Major Type Public Function |
| 177 | 0 QCBOREncode_AddUInt64() |
| 178 | 0, 1 QCBOREncode_AddUInt64(), QCBOREncode_AddInt64() |
| 179 | 2, 3 QCBOREncode_AddBuffer(), Also QCBOREncode_OpenMapOrArray(), |
| 180 | QCBOREncode_CloseMapOrArray() |
| 181 | 4, 5 QCBOREncode_OpenMapOrArray(), QCBOREncode_CloseMapOrArray(), |
| 182 | QCBOREncode_OpenMapOrArrayIndefiniteLength(), |
| 183 | QCBOREncode_CloseMapOrArrayIndefiniteLength() |
| 184 | 6 QCBOREncode_AddTag() |
| 185 | 7 QCBOREncode_AddDouble(), QCBOREncode_AddType7() |
| 186 | |
| 187 | Additionally, encoding of decimal fractions and bigfloats is by |
| 188 | QCBOREncode_AddExponentAndMantissa() |
| 189 | */ |
| 190 | |
| 191 | /* |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 192 | Error tracking plan -- Errors are tracked internally and not returned |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 193 | until QCBOREncode_Finish is called. The CBOR errors are in me->uError. |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 194 | UsefulOutBuf also tracks whether the buffer is full or not in its |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 195 | context. Once either of these errors is set they are never |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 196 | cleared. Only QCBOREncode_Init() resets them. Or said another way, they must |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 197 | never be cleared or we'll tell the caller all is good when it is not. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 198 | |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 199 | Only one error code is reported by QCBOREncode_Finish() even if there are |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 200 | multiple errors. The last one set wins. The caller might have to fix |
| 201 | one error to reveal the next one they have to fix. This is OK. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 202 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 203 | The buffer full error tracked by UsefulBuf is only pulled out of |
| 204 | UsefulBuf in Finish() so it is the one that usually wins. UsefulBuf |
| 205 | will never go off the end of the buffer even if it is called again |
| 206 | and again when full. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 207 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 208 | It is really tempting to not check for overflow on the count in the |
| 209 | number of items in an array. It would save a lot of code, it is |
| 210 | extremely unlikely that any one will every put 65,000 items in an |
| 211 | array, and the only bad thing that would happen is the CBOR would be |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 212 | bogus. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 213 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 214 | Since this does not parse any input, you could in theory remove all |
| 215 | error checks in this code if you knew the caller called it |
| 216 | correctly. Maybe someday CDDL or some such language will be able to |
| 217 | generate the code to call this and the calling code would always be |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame] | 218 | correct. This could also automatically size some of the data |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 219 | structures like array/map nesting resulting in some stack memory |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 220 | savings. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 221 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 222 | The 8 errors returned here fall into three categories: |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 223 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 224 | Sizes |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 225 | QCBOR_ERR_BUFFER_TOO_LARGE -- Encoded output exceeded UINT32_MAX |
| 226 | QCBOR_ERR_BUFFER_TOO_SMALL -- Output buffer too small |
| 227 | QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -- Nesting > QCBOR_MAX_ARRAY_NESTING1 |
| 228 | QCBOR_ERR_ARRAY_TOO_LONG -- Too many things added to an array/map |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 229 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 230 | Nesting constructed incorrectly |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 231 | QCBOR_ERR_TOO_MANY_CLOSES -- More close calls than opens |
| 232 | QCBOR_ERR_CLOSE_MISMATCH -- Type of close does not match open |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 233 | QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN -- Finish called without enough closes |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 234 | |
| 235 | Would generate not-well-formed CBOR |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 236 | QCBOR_ERR_UNSUPPORTED -- Simple type between 24 and 31 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 237 | */ |
| 238 | |
| 239 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 240 | /* |
| 241 | Public function for initialization. See header qcbor.h |
| 242 | */ |
Laurence Lundblade | 2296db5 | 2018-09-14 18:08:39 -0700 | [diff] [blame] | 243 | void QCBOREncode_Init(QCBOREncodeContext *me, UsefulBuf Storage) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 244 | { |
| 245 | memset(me, 0, sizeof(QCBOREncodeContext)); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 246 | UsefulOutBuf_Init(&(me->OutBuf), Storage); |
| 247 | Nesting_Init(&(me->nesting)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 251 | /** |
| 252 | @brief Encode a data item, the most atomic part of CBOR |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 253 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 254 | @param[in,out] me Encoding context including output buffer |
| 255 | @param[in] uMajorType One of CBOR_MAJOR_TYPE_XX |
| 256 | @param[in] nMinLen Include zero bytes up to this length. If 0 include |
| 257 | no zero bytes. Non-zero to encode floats and doubles. |
| 258 | @param[in] uNumber The number to encode, the argument. |
| 259 | @param[in] uPos The position in the output buffer (which is inside |
| 260 | the encoding context) to insert the result. This is |
| 261 | usually at the end, an append. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 262 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 263 | All CBOR data items have a type and an "argument". The argument is |
| 264 | either the value of the item for integer types, the length of the |
| 265 | content for string, byte, array and map types, a tag for major type |
| 266 | 6, and has several uses for major type 7. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 267 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 268 | This function encodes the type and the argument. There are several |
| 269 | encodings for the argument depending on how large it is and how it is |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 270 | used. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 271 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 272 | Every encoding of the type and argument has at least one byte, the |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 273 | "initial byte". |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 274 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 275 | The top three bits of the initial byte are the major type for the |
| 276 | CBOR data item. The eight major types defined by the standard are |
| 277 | defined as CBOR_MAJOR_TYPE_xxxx in qcbor.h. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 278 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 279 | The remaining five bits, known as "additional information", and |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 280 | possibly more bytes encode the argument. If the argument is less than |
| 281 | 24, then it is encoded entirely in the five bits. This is neat |
| 282 | because it allows you to encode an entire CBOR data item in 1 byte |
| 283 | for many values and types (integers 0-23, true, false, and tags). |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 284 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 285 | If the argument is larger than 24, then it is encoded in 1,2,4 or 8 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 286 | additional bytes, with the number of these bytes indicated by the |
| 287 | values of the 5 bits 24, 25, 25 and 27. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 288 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 289 | It is possible to encode a particular argument in many ways with this |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 290 | representation. This implementation always uses the smallest |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 291 | possible representation. This conforms with CBOR preferred encoding. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 292 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 293 | This function inserts them into the output buffer at the specified |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 294 | position. AppendEncodedTypeAndNumber() appends to the end. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 295 | |
| 296 | This function takes care of converting to network byte order. |
| 297 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 298 | This function is also used to insert floats and doubles. Before this |
| 299 | function is called the float or double must be copied into a |
| 300 | uint64_t. That is how they are passed in. They are then converted to |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 301 | network byte order correctly. The uMinLen parameter makes sure that |
| 302 | even if all the digits of a half, float or double are 0 it is still |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 303 | correctly encoded in 2, 4 or 8 bytes. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 304 | */ |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 305 | static void InsertEncodedTypeAndNumber(QCBOREncodeContext *me, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 306 | uint8_t uMajorType, |
| 307 | int nMinLen, |
| 308 | uint64_t uNumber, |
| 309 | size_t uPos) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 310 | { |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 311 | /* |
| 312 | This code does endian conversion without hton or knowing the |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 313 | endianness of the machine using masks and shifts. This avoids the |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 314 | dependency on hton and the mess of figuring out how to find the |
| 315 | machine's endianness. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 316 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 317 | This is a good efficient implementation on little-endian machines. |
| 318 | A faster and small implementation is possible on big-endian |
| 319 | machines because CBOR/network byte order is big endian. However |
| 320 | big endian machines are uncommon. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 321 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 322 | On x86, it is about 200 bytes instead of 500 bytes for the more |
| 323 | formal unoptimized code. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 324 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 325 | This also does the CBOR preferred shortest encoding for integers |
| 326 | and is called to do endian conversion for floats. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 327 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 328 | It works backwards from the LSB to the MSB as needed. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 329 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 330 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 331 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 332 | |
| 333 | // Holds up to 9 bytes of type and argument plus one extra so pointer |
| 334 | // always points to valid bytes. |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 335 | uint8_t bytes[sizeof(uint64_t)+2]; |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 336 | // Point to the last bytes and work backwards |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 337 | uint8_t *pByte = &bytes[sizeof(bytes)-1]; |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 338 | // This is the 5 bits in the initial byte that is not the major type |
| 339 | uint8_t uAdditionalInfo; |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 340 | |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 341 | if (uMajorType == CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN) { |
| 342 | uMajorType = CBOR_MAJOR_TYPE_ARRAY; |
| 343 | uAdditionalInfo = LEN_IS_INDEFINITE; |
| 344 | } else if (uMajorType == CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN) { |
| 345 | uMajorType = CBOR_MAJOR_TYPE_MAP; |
| 346 | uAdditionalInfo = LEN_IS_INDEFINITE; |
| 347 | } else if (uNumber < CBOR_TWENTY_FOUR && nMinLen == 0) { |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 348 | // Simple case where argument is < 24 |
| 349 | uAdditionalInfo = uNumber; |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 350 | } else if (uMajorType == CBOR_MAJOR_TYPE_SIMPLE && uNumber == CBOR_SIMPLE_BREAK) { |
| 351 | // Break statement can be encoded in single byte too (0xff) |
| 352 | uAdditionalInfo = uNumber; |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 353 | } else { |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 354 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 355 | Encode argument in 1,2,4 or 8 bytes. Outer loop runs once for 1 |
| 356 | byte and 4 times for 8 bytes. Inner loop runs 1, 2 or 4 times |
| 357 | depending on outer loop counter. This works backwards taking 8 |
| 358 | bits off the argument being encoded at a time until all bits |
| 359 | from uNumber have been encoded and the minimum encoding size is |
| 360 | reached. Minimum encoding size is for floating-point numbers |
| 361 | with zero bytes. |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 362 | */ |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 363 | static const uint8_t aIterate[] = {1,1,2,4}; |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 364 | uint8_t i; |
| 365 | for(i = 0; uNumber || nMinLen > 0; i++) { |
| 366 | const uint8_t uIterations = aIterate[i]; |
| 367 | for(int j = 0; j < uIterations; j++) { |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 368 | *--pByte = uNumber & 0xff; |
| 369 | uNumber = uNumber >> 8; |
| 370 | } |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 371 | nMinLen -= uIterations; |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 372 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 373 | // Additional info is the encoding of the number of additional |
| 374 | // bytes to encode argument. |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 375 | uAdditionalInfo = LEN_IS_ONE_BYTE-1 + i; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 376 | } |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 377 | *--pByte = (uMajorType << 5) + uAdditionalInfo; |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 378 | |
| 379 | UsefulOutBuf_InsertData(&(me->OutBuf), pByte, &bytes[sizeof(bytes)-1] - pByte, uPos); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | |
| 383 | /* |
| 384 | Append the type and number info to the end of the buffer. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 385 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 386 | See InsertEncodedTypeAndNumber() function above for details. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 387 | */ |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 388 | inline static void AppendEncodedTypeAndNumber(QCBOREncodeContext *me, |
| 389 | uint8_t uMajorType, |
| 390 | uint64_t uNumber) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 391 | { |
| 392 | // An append is an insert at the end. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 393 | InsertEncodedTypeAndNumber(me, |
| 394 | uMajorType, |
| 395 | 0, |
| 396 | uNumber, |
| 397 | UsefulOutBuf_GetEndPosition(&(me->OutBuf))); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 401 | |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 402 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 403 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 404 | Public functions for closing arrays and maps. See qcbor.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 405 | */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 406 | void QCBOREncode_AddUInt64(QCBOREncodeContext *me, uint64_t uValue) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 407 | { |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 408 | if(me->uError == QCBOR_SUCCESS) { |
| 409 | AppendEncodedTypeAndNumber(me, CBOR_MAJOR_TYPE_POSITIVE_INT, uValue); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 410 | me->uError = Nesting_Increment(&(me->nesting)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 411 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame] | 414 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 415 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 416 | Public functions for closing arrays and maps. See qcbor.h |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 417 | */ |
| 418 | void QCBOREncode_AddInt64(QCBOREncodeContext *me, int64_t nNum) |
| 419 | { |
| 420 | if(me->uError == QCBOR_SUCCESS) { |
| 421 | uint8_t uMajorType; |
| 422 | uint64_t uValue; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 423 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 424 | if(nNum < 0) { |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 425 | // In CBOR -1 encodes as 0x00 with major type negative int. |
| 426 | uValue = (uint64_t)(-nNum - 1); |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 427 | uMajorType = CBOR_MAJOR_TYPE_NEGATIVE_INT; |
| 428 | } else { |
| 429 | uValue = (uint64_t)nNum; |
| 430 | uMajorType = CBOR_MAJOR_TYPE_POSITIVE_INT; |
| 431 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 432 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 433 | AppendEncodedTypeAndNumber(me, uMajorType, uValue); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 434 | me->uError = Nesting_Increment(&(me->nesting)); |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | |
| 439 | /* |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 440 | Semi-private function. It is exposed to user of the interface, but |
| 441 | they will usually call one of the inline wrappers rather than this. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 442 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 443 | See qcbor.h |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 444 | |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 445 | Does the work of adding actual strings bytes to the CBOR output (as |
| 446 | opposed to numbers and opening / closing aggregate types). |
| 447 | |
| 448 | There are four use cases: |
| 449 | CBOR_MAJOR_TYPE_BYTE_STRING -- Byte strings |
| 450 | CBOR_MAJOR_TYPE_TEXT_STRING -- Text strings |
| 451 | CBOR_MAJOR_NONE_TYPE_RAW -- Already-encoded CBOR |
| 452 | CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY -- Special case |
| 453 | |
| 454 | The first two add the type and length plus the actual bytes. The |
| 455 | third just adds the bytes as the type and length are presumed to be |
| 456 | in the bytes. The fourth just adds the type and length for the very |
| 457 | special case of QCBOREncode_AddBytesLenOnly(). |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 458 | */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 459 | void QCBOREncode_AddBuffer(QCBOREncodeContext *me, uint8_t uMajorType, UsefulBufC Bytes) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 460 | { |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 461 | if(me->uError == QCBOR_SUCCESS) { |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 462 | // If it is not Raw CBOR, add the type and the length |
| 463 | if(uMajorType != CBOR_MAJOR_NONE_TYPE_RAW) { |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 464 | uint8_t uRealMajorType = uMajorType; |
| 465 | if(uRealMajorType == CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) { |
| 466 | uRealMajorType = CBOR_MAJOR_TYPE_BYTE_STRING; |
| 467 | } |
| 468 | AppendEncodedTypeAndNumber(me, uRealMajorType, Bytes.len); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 469 | } |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 470 | |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 471 | if(uMajorType != CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) { |
| 472 | // Actually add the bytes |
| 473 | UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), Bytes); |
| 474 | } |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 475 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 476 | // Update the array counting if there is any nesting at all |
| 477 | me->uError = Nesting_Increment(&(me->nesting)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 481 | |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 482 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 483 | Public functions for closing arrays and maps. See qcbor.h |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 484 | */ |
| 485 | void QCBOREncode_AddTag(QCBOREncodeContext *me, uint64_t uTag) |
| 486 | { |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 487 | AppendEncodedTypeAndNumber(me, CBOR_MAJOR_TYPE_OPTIONAL, uTag); |
| 488 | } |
| 489 | |
| 490 | |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame] | 491 | /* |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 492 | Semi-private function. It is exposed to user of the interface, |
| 493 | but they will usually call one of the inline wrappers rather than this. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 494 | |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 495 | See header qcbor.h |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame] | 496 | */ |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 497 | void QCBOREncode_AddType7(QCBOREncodeContext *me, size_t uSize, uint64_t uNum) |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 498 | { |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 499 | if(me->uError == QCBOR_SUCCESS) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 500 | if(uNum >= CBOR_SIMPLEV_RESERVED_START && uNum <= CBOR_SIMPLEV_RESERVED_END) { |
| 501 | me->uError = QCBOR_ERR_UNSUPPORTED; |
| 502 | } else { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 503 | // This call takes care of endian swapping for the float / double |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 504 | InsertEncodedTypeAndNumber(me, |
| 505 | // The major type for floats and doubles |
| 506 | CBOR_MAJOR_TYPE_SIMPLE, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 507 | // Must pass size to ensure floats |
| 508 | // with zero bytes encode correctly |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 509 | (int)uSize, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 510 | // The floating-point number as a uint |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 511 | uNum, |
| 512 | // end position because this is append |
| 513 | UsefulOutBuf_GetEndPosition(&(me->OutBuf))); |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 514 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 515 | me->uError = Nesting_Increment(&(me->nesting)); |
| 516 | } |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 517 | } |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 518 | } |
| 519 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 520 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 521 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 522 | Public functions for closing arrays and maps. See qcbor.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 523 | */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 524 | void QCBOREncode_AddDouble(QCBOREncodeContext *me, double dNum) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 525 | { |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 526 | const IEEE754_union uNum = IEEE754_DoubleToSmallest(dNum); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 527 | |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 528 | QCBOREncode_AddType7(me, uNum.uSize, uNum.uValue); |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 532 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 533 | /* |
| 534 | Semi-public function. It is exposed to the user of the interface, but |
| 535 | one of the inline wrappers will usually be called rather than this. |
| 536 | |
| 537 | See qcbor.h |
| 538 | */ |
| 539 | void QCBOREncode_AddExponentAndMantissa(QCBOREncodeContext *pMe, |
| 540 | uint64_t uTag, |
| 541 | UsefulBufC BigNumMantissa, |
| 542 | bool bBigNumIsNegative, |
| 543 | int64_t nMantissa, |
| 544 | int64_t nExponent) |
| 545 | { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 546 | /* |
| 547 | This is for encoding either a big float or a decimal fraction, |
| 548 | both of which are an array of two items, an exponent and a |
| 549 | mantissa. The difference between the two is that the exponent is |
| 550 | base-2 for big floats and base-10 for decimal fractions, but that |
| 551 | has no effect on the code here. |
| 552 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 553 | QCBOREncode_AddTag(pMe, uTag); |
| 554 | QCBOREncode_OpenArray(pMe); |
| 555 | QCBOREncode_AddInt64(pMe, nExponent); |
| 556 | if(!UsefulBuf_IsNULLC(BigNumMantissa)) { |
| 557 | if(bBigNumIsNegative) { |
| 558 | QCBOREncode_AddNegativeBignum(pMe, BigNumMantissa); |
| 559 | } else { |
| 560 | QCBOREncode_AddPositiveBignum(pMe, BigNumMantissa); |
| 561 | } |
| 562 | } else { |
| 563 | QCBOREncode_AddInt64(pMe, nMantissa); |
| 564 | } |
| 565 | QCBOREncode_CloseArray(pMe); |
| 566 | } |
| 567 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 568 | |
| 569 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 570 | /* |
| 571 | Semi-public function. It is exposed to user of the interface, |
| 572 | but they will usually call one of the inline wrappers rather than this. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 573 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 574 | See header qcbor.h |
| 575 | */ |
| 576 | void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType) |
| 577 | { |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 578 | // Add one item to the nesting level we are in for the new map or array |
| 579 | me->uError = Nesting_Increment(&(me->nesting)); |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 580 | if(me->uError == QCBOR_SUCCESS) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 581 | /* |
| 582 | The offset where the length of an array or map will get written |
| 583 | is stored in a uint32_t, not a size_t to keep stack usage |
| 584 | smaller. This checks to be sure there is no wrap around when |
| 585 | recording the offset. Note that on 64-bit machines CBOR larger |
| 586 | than 4GB can be encoded as long as no array / map offsets occur |
| 587 | past the 4GB mark, but the public interface says that the |
| 588 | maximum is 4GB to keep the discussion simpler. |
| 589 | */ |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 590 | size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf)); |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 591 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 592 | /* |
| 593 | QCBOR_MAX_ARRAY_OFFSET is slightly less than UINT32_MAX so this |
| 594 | code can run on a 32-bit machine and tests can pass on a 32-bit |
| 595 | machine. If it was exactly UINT32_MAX, then this code would not |
| 596 | compile or run on a 32-bit machine and an #ifdef or some |
| 597 | machine size detection would be needed reducing portability. |
| 598 | */ |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 599 | if(uEndPosition >= QCBOR_MAX_ARRAY_OFFSET) { |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 600 | me->uError = QCBOR_ERR_BUFFER_TOO_LARGE; |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 601 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 602 | } else { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 603 | // Increase nesting level because this is a map or array. Cast |
| 604 | // from size_t to uin32_t is safe because of check above |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 605 | me->uError = Nesting_Increase(&(me->nesting), uMajorType, (uint32_t)uEndPosition); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 606 | } |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 607 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 610 | |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 611 | /* |
| 612 | Semi-public function. It is exposed to user of the interface, |
| 613 | but they will usually call one of the inline wrappers rather than this. |
| 614 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 615 | See qcbor.h |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 616 | */ |
| 617 | void QCBOREncode_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType) |
| 618 | { |
| 619 | // insert the indefinite length marker (0x9f for arrays, 0xbf for maps) |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 620 | InsertEncodedTypeAndNumber(me, uMajorType, 0, 0, UsefulOutBuf_GetEndPosition(&(me->OutBuf))); |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 621 | |
| 622 | QCBOREncode_OpenMapOrArray(me, uMajorType); |
| 623 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 624 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 625 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 626 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 627 | Public functions for closing arrays and maps. See qcbor.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 628 | */ |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 629 | void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *me, |
| 630 | uint8_t uMajorType, |
| 631 | UsefulBufC *pWrappedCBOR) |
Laurence Lundblade | a954db9 | 2018-09-28 19:27:31 -0700 | [diff] [blame] | 632 | { |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 633 | if(me->uError == QCBOR_SUCCESS) { |
Laurence Lundblade | a954db9 | 2018-09-28 19:27:31 -0700 | [diff] [blame] | 634 | if(!Nesting_IsInNest(&(me->nesting))) { |
| 635 | me->uError = QCBOR_ERR_TOO_MANY_CLOSES; |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 636 | } else if(Nesting_GetMajorType(&(me->nesting)) != uMajorType) { |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 637 | me->uError = QCBOR_ERR_CLOSE_MISMATCH; |
Laurence Lundblade | a954db9 | 2018-09-28 19:27:31 -0700 | [diff] [blame] | 638 | } else { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 639 | /* |
| 640 | When the array, map or bstr wrap was started, nothing was |
| 641 | gone except note the position of the start of it. This code |
| 642 | goes back and inserts the actual CBOR array, map or bstr and |
| 643 | its length. That means all the data that is in the array, |
| 644 | map or wrapped needs to be slid to the right. This is done |
| 645 | by UsefulOutBuf's insert function that is called from inside |
| 646 | InsertEncodedTypeAndNumber() |
| 647 | */ |
| 648 | const size_t uInsertPosition = Nesting_GetStartPos(&(me->nesting)); |
| 649 | const size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf)); |
| 650 | /* |
| 651 | This can't go negative because the UsefulOutBuf always only |
| 652 | grows and never shrinks. UsefulOutBut itself also has |
| 653 | defenses such that it won't write were it should not even if |
| 654 | given hostile input lengths |
| 655 | */ |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame] | 656 | const size_t uLenOfEncodedMapOrArray = uEndPosition - uInsertPosition; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 657 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 658 | // Number of bytes for a bstr or number of items a for map & array |
| 659 | const bool bIsBstr = uMajorType == CBOR_MAJOR_TYPE_BYTE_STRING; |
| 660 | const size_t uLength = bIsBstr ? uLenOfEncodedMapOrArray |
| 661 | : Nesting_GetCount(&(me->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 662 | |
Laurence Lundblade | a954db9 | 2018-09-28 19:27:31 -0700 | [diff] [blame] | 663 | // Actually insert |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 664 | InsertEncodedTypeAndNumber(me, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 665 | uMajorType, // bstr, array or map |
| 666 | 0, // no minimum length |
| 667 | uLength, // either len of bstr or |
| 668 | // num map / array items |
Laurence Lundblade | a954db9 | 2018-09-28 19:27:31 -0700 | [diff] [blame] | 669 | uInsertPosition); // position in out buffer |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 670 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 671 | /* |
| 672 | Return pointer and length to the enclosed encoded CBOR. The |
| 673 | intended use is for it to be hashed (e.g., SHA-256) in a |
| 674 | COSE implementation. This must be used right away, as the |
| 675 | pointer and length go invalid on any subsequent calls to |
| 676 | this function because there might be calls to |
| 677 | InsertEncodedTypeAndNumber() that slides data to the right. |
| 678 | */ |
Laurence Lundblade | a954db9 | 2018-09-28 19:27:31 -0700 | [diff] [blame] | 679 | if(pWrappedCBOR) { |
Laurence Lundblade | 25c6c0a | 2018-12-17 13:21:59 -0800 | [diff] [blame] | 680 | const UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf)); |
Laurence Lundblade | 83f5b7f | 2019-04-06 11:22:37 -0700 | [diff] [blame] | 681 | *pWrappedCBOR = UsefulBuf_Tail(PartialResult, uInsertPosition); |
Laurence Lundblade | a954db9 | 2018-09-28 19:27:31 -0700 | [diff] [blame] | 682 | } |
| 683 | Nesting_Decrease(&(me->nesting)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 684 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 688 | |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 689 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 690 | Public functions for closing arrays and maps. See qcbor.h |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 691 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 692 | void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *me, |
| 693 | uint8_t uMajorType, |
| 694 | UsefulBufC *pWrappedCBOR) |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 695 | { |
| 696 | if(me->uError == QCBOR_SUCCESS) { |
| 697 | if(!Nesting_IsInNest(&(me->nesting))) { |
| 698 | me->uError = QCBOR_ERR_TOO_MANY_CLOSES; |
| 699 | } else if(Nesting_GetMajorType(&(me->nesting)) != uMajorType) { |
| 700 | me->uError = QCBOR_ERR_CLOSE_MISMATCH; |
| 701 | } else { |
| 702 | // insert the break marker (0xff for both arrays and maps) |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 703 | InsertEncodedTypeAndNumber(me, |
| 704 | CBOR_MAJOR_TYPE_SIMPLE, |
| 705 | 0, |
| 706 | CBOR_SIMPLE_BREAK, |
| 707 | UsefulOutBuf_GetEndPosition(&(me->OutBuf))); |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 708 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 709 | /* |
| 710 | Return pointer and length to the enclosed encoded CBOR. The |
| 711 | intended use is for it to be hashed (e.g., SHA-256) in a |
| 712 | COSE implementation. This must be used right away, as the |
| 713 | pointer and length go invalid on any subsequent calls to |
| 714 | this function because there might be calls to |
| 715 | InsertEncodedTypeAndNumber() that slides data to the right. |
| 716 | */ |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 717 | if(pWrappedCBOR) { |
| 718 | const UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf)); |
| 719 | *pWrappedCBOR = UsefulBuf_Tail(PartialResult, UsefulOutBuf_GetEndPosition(&(me->OutBuf))); |
| 720 | } |
| 721 | |
| 722 | // Decrease nesting level |
| 723 | Nesting_Decrease(&(me->nesting)); |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 728 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 729 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 730 | Public functions to finish and get the encoded result. See qcbor.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 731 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 732 | QCBORError QCBOREncode_Finish(QCBOREncodeContext *me, UsefulBufC *pEncodedCBOR) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 733 | { |
Laurence Lundblade | f607a2a | 2019-07-05 21:25:25 -0700 | [diff] [blame] | 734 | QCBORError uReturn = QCBOREncode_GetErrorState(me); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 735 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 736 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 737 | goto Done; |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 738 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 739 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 740 | if (Nesting_IsInNest(&(me->nesting))) { |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 741 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 742 | goto Done; |
| 743 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 744 | |
Laurence Lundblade | da3f082 | 2018-09-18 19:49:02 -0700 | [diff] [blame] | 745 | *pEncodedCBOR = UsefulOutBuf_OutUBuf(&(me->OutBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 746 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 747 | Done: |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 748 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 751 | |
Laurence Lundblade | f607a2a | 2019-07-05 21:25:25 -0700 | [diff] [blame] | 752 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 753 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 754 | Public functions to finish and get the encoded result. See qcbor.h |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 755 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 756 | QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *me, size_t *puEncodedLen) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 757 | { |
Laurence Lundblade | da3f082 | 2018-09-18 19:49:02 -0700 | [diff] [blame] | 758 | UsefulBufC Enc; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 759 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 760 | QCBORError nReturn = QCBOREncode_Finish(me, &Enc); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 761 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 762 | if(nReturn == QCBOR_SUCCESS) { |
Laurence Lundblade | da3f082 | 2018-09-18 19:49:02 -0700 | [diff] [blame] | 763 | *puEncodedLen = Enc.len; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 764 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 765 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 766 | return nReturn; |
| 767 | } |
| 768 | |
| 769 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 770 | |
| 771 | |
| 772 | /* |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 773 | Object code sizes on X86 with LLVM compiler and -Os (Dec 30, 2018) |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 774 | |
Laurence Lundblade | 9c09739 | 2018-12-30 13:52:24 -0800 | [diff] [blame] | 775 | _QCBOREncode_Init 69 |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 776 | _QCBOREncode_AddUInt64 76 |
| 777 | _QCBOREncode_AddInt64 87 |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 778 | _QCBOREncode_AddBuffer 113 |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 779 | _QCBOREncode_AddTag 27 |
Laurence Lundblade | 9c09739 | 2018-12-30 13:52:24 -0800 | [diff] [blame] | 780 | _QCBOREncode_AddType7 87 |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 781 | _QCBOREncode_AddDouble 36 |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 782 | _QCBOREncode_OpenMapOrArray 103 |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 783 | _QCBOREncode_CloseMapOrArray 181 |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 784 | _InsertEncodedTypeAndNumber 190 |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 785 | _QCBOREncode_Finish 72 |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 786 | _QCBOREncode_FinishGetSize 70 |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 787 | |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 788 | Total is about 1.1KB |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 789 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 790 | _QCBOREncode_CloseMapOrArray is larger because it has a lot |
| 791 | of nesting tracking to do and much of Nesting_ inlines |
| 792 | into it. It probably can't be reduced much. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 793 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 794 | If the error returned by Nesting_Increment() can be ignored |
| 795 | because the limit is so high and the consequence of exceeding |
| 796 | is proved to be inconsequential, then a lot of if(me->uError) |
| 797 | instance can be removed, saving some code. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 798 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 799 | */ |