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