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