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