Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 2 | Copyright (c) 2016-2018, The Linux Foundation. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3 | Copyright (c) 2018-2020, Laurence Lundblade. |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 4 | All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 5 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are |
| 8 | met: |
| 9 | * Redistributions of source code must retain the above copyright |
| 10 | notice, this list of conditions and the following disclaimer. |
| 11 | * Redistributions in binary form must reproduce the above |
| 12 | copyright notice, this list of conditions and the following |
| 13 | disclaimer in the documentation and/or other materials provided |
| 14 | with the distribution. |
| 15 | * Neither the name of The Linux Foundation nor the names of its |
| 16 | contributors, nor the name "Laurence Lundblade" may be used to |
| 17 | endorse or promote products derived from this software without |
| 18 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 19 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 20 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 31 | =============================================================================*/ |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 32 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 33 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 34 | #include "qcbor/qcbor_encode.h" |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 35 | #include "ieee754.h" |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 36 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 37 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 38 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 39 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 40 | Nesting -- This tracks the nesting of maps and arrays. |
| 41 | |
| 42 | The following functions and data type QCBORTrackNesting implement the |
| 43 | nesting management for encoding. |
| 44 | |
| 45 | CBOR's two nesting types, arrays and maps, are tracked here. There is |
| 46 | a limit of QCBOR_MAX_ARRAY_NESTING to the number of arrays and maps |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 47 | that can be nested in one encoding so the encoding context stays |
| 48 | small enough to fit on the stack. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 49 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 50 | When an array / map is opened, pCurrentNesting points to the element |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 51 | in pArrays that records the type, start position and accumulates a |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 52 | count of the number of items added. When closed the start position is |
| 53 | used to go back and fill in the type and number of items in the array |
| 54 | / map. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 55 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 56 | Encoded output can be just items like ints and strings that are not |
| 57 | part of any array / map. That is, the first thing encoded does not |
| 58 | have to be an array or a map. |
| 59 | |
| 60 | QCBOR has a special feature to allow constructing bstr-wrapped CBOR |
| 61 | directly into the output buffer, so an extra buffer for it is not |
| 62 | needed. This is implemented as nesting with type |
| 63 | CBOR_MAJOR_TYPE_BYTE_STRING and uses this code. Bstr-wrapped CBOR is |
| 64 | used by COSE for data that is to be hashed. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 65 | */ |
| 66 | inline static void Nesting_Init(QCBORTrackNesting *pNesting) |
| 67 | { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 68 | // Assumes pNesting has been zeroed |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 69 | pNesting->pCurrentNesting = &pNesting->pArrays[0]; |
| 70 | // Implied CBOR array at the top nesting level. This is never returned, |
| 71 | // but makes the item count work correctly. |
| 72 | pNesting->pCurrentNesting->uMajorType = CBOR_MAJOR_TYPE_ARRAY; |
| 73 | } |
| 74 | |
Laurence Lundblade | 29497c0 | 2020-07-11 15:44:03 -0700 | [diff] [blame] | 75 | inline static uint8_t Nesting_Increase(QCBORTrackNesting *pNesting, |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 76 | uint8_t uMajorType, |
| 77 | uint32_t uPos) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 78 | { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 79 | if(pNesting->pCurrentNesting == &pNesting->pArrays[QCBOR_MAX_ARRAY_NESTING]) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 80 | // Trying to open one too many |
Laurence Lundblade | 29497c0 | 2020-07-11 15:44:03 -0700 | [diff] [blame] | 81 | return QCBOR_ERR_ARRAY_NESTING_TOO_DEEP; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 82 | } else { |
| 83 | pNesting->pCurrentNesting++; |
| 84 | pNesting->pCurrentNesting->uCount = 0; |
| 85 | pNesting->pCurrentNesting->uStart = uPos; |
| 86 | pNesting->pCurrentNesting->uMajorType = uMajorType; |
Laurence Lundblade | 29497c0 | 2020-07-11 15:44:03 -0700 | [diff] [blame] | 87 | return QCBOR_SUCCESS; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 88 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | inline static void Nesting_Decrease(QCBORTrackNesting *pNesting) |
| 92 | { |
| 93 | pNesting->pCurrentNesting--; |
| 94 | } |
| 95 | |
Laurence Lundblade | 29497c0 | 2020-07-11 15:44:03 -0700 | [diff] [blame] | 96 | inline static uint8_t Nesting_Increment(QCBORTrackNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 97 | { |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 98 | if(1 >= QCBOR_MAX_ITEMS_IN_ARRAY - pNesting->pCurrentNesting->uCount) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 99 | return QCBOR_ERR_ARRAY_TOO_LONG; |
| 100 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 101 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 102 | pNesting->pCurrentNesting->uCount++; |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 103 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 104 | return QCBOR_SUCCESS; |
| 105 | } |
| 106 | |
| 107 | inline static uint16_t Nesting_GetCount(QCBORTrackNesting *pNesting) |
| 108 | { |
| 109 | // The nesting count recorded is always the actual number of individiual |
| 110 | // data items in the array or map. For arrays CBOR uses the actual item |
| 111 | // count. For maps, CBOR uses the number of pairs. This function returns |
| 112 | // the number needed for the CBOR encoding, so it divides the number of |
| 113 | // items by two for maps to get the number of pairs. This implementation |
| 114 | // 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] | 115 | // type, hence uDivisor is either 1 or 2. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 116 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 117 | if(pNesting->pCurrentNesting->uMajorType == CBOR_MAJOR_TYPE_MAP) { |
| 118 | // Cast back to uint16_t after integer promotion for bit shift |
| 119 | return (uint16_t)(pNesting->pCurrentNesting->uCount >> 1); |
| 120 | } else { |
| 121 | return pNesting->pCurrentNesting->uCount; |
| 122 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | inline static uint32_t Nesting_GetStartPos(QCBORTrackNesting *pNesting) |
| 126 | { |
| 127 | return pNesting->pCurrentNesting->uStart; |
| 128 | } |
| 129 | |
| 130 | inline static uint8_t Nesting_GetMajorType(QCBORTrackNesting *pNesting) |
| 131 | { |
| 132 | return pNesting->pCurrentNesting->uMajorType; |
| 133 | } |
| 134 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 135 | inline static bool Nesting_IsInNest(QCBORTrackNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 136 | { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 137 | return pNesting->pCurrentNesting == &pNesting->pArrays[0] ? false : true; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 140 | |
| 141 | |
| 142 | |
| 143 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 144 | Encoding of the major CBOR types is by these functions: |
| 145 | |
| 146 | CBOR Major Type Public Function |
| 147 | 0 QCBOREncode_AddUInt64() |
| 148 | 0, 1 QCBOREncode_AddUInt64(), QCBOREncode_AddInt64() |
| 149 | 2, 3 QCBOREncode_AddBuffer(), Also QCBOREncode_OpenMapOrArray(), |
| 150 | QCBOREncode_CloseMapOrArray() |
| 151 | 4, 5 QCBOREncode_OpenMapOrArray(), QCBOREncode_CloseMapOrArray(), |
| 152 | QCBOREncode_OpenMapOrArrayIndefiniteLength(), |
| 153 | QCBOREncode_CloseMapOrArrayIndefiniteLength() |
| 154 | 6 QCBOREncode_AddTag() |
| 155 | 7 QCBOREncode_AddDouble(), QCBOREncode_AddType7() |
| 156 | |
| 157 | Additionally, encoding of decimal fractions and bigfloats is by |
| 158 | QCBOREncode_AddExponentAndMantissa() |
| 159 | */ |
| 160 | |
| 161 | /* |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 162 | Error tracking plan -- Errors are tracked internally and not returned |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 163 | until QCBOREncode_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 | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 192 | The 8 errors returned here fall into three 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 | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 195 | QCBOR_ERR_BUFFER_TOO_LARGE -- Encoded output exceeded UINT32_MAX |
| 196 | QCBOR_ERR_BUFFER_TOO_SMALL -- Output buffer too small |
| 197 | QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -- Nesting > QCBOR_MAX_ARRAY_NESTING1 |
| 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 |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 201 | QCBOR_ERR_TOO_MANY_CLOSES -- More close calls than opens |
| 202 | QCBOR_ERR_CLOSE_MISMATCH -- Type of close does not match open |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 203 | QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN -- Finish called without enough closes |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 204 | |
| 205 | Would generate not-well-formed CBOR |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 206 | QCBOR_ERR_UNSUPPORTED -- Simple type between 24 and 31 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 207 | */ |
| 208 | |
| 209 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 210 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 211 | Public function for initialization. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 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 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 221 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 222 | Public function to encode a CBOR head. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 223 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 224 | UsefulBufC QCBOREncode_EncodeHead(UsefulBuf buffer, |
| 225 | uint8_t uMajorType, |
| 226 | uint8_t uMinLen, |
| 227 | uint64_t uArgument) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 228 | { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 229 | /** |
| 230 | All CBOR data items have a type and an "argument". The argument is |
| 231 | either the value of the item for integer types, the length of the |
| 232 | content for string, byte, array and map types, a tag for major type |
| 233 | 6, and has several uses for major type 7. |
| 234 | |
| 235 | This function encodes the type and the argument. There are several |
| 236 | encodings for the argument depending on how large it is and how it is |
| 237 | used. |
| 238 | |
| 239 | Every encoding of the type and argument has at least one byte, the |
| 240 | "initial byte". |
| 241 | |
| 242 | The top three bits of the initial byte are the major type for the |
| 243 | CBOR data item. The eight major types defined by the standard are |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 244 | defined as CBOR_MAJOR_TYPE_xxxx in qcbor/qcbor_common.h. |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 245 | |
| 246 | The remaining five bits, known as "additional information", and |
| 247 | possibly more bytes encode the argument. If the argument is less than |
| 248 | 24, then it is encoded entirely in the five bits. This is neat |
| 249 | because it allows you to encode an entire CBOR data item in 1 byte |
| 250 | for many values and types (integers 0-23, true, false, and tags). |
| 251 | |
| 252 | If the argument is larger than 24, then it is encoded in 1,2,4 or 8 |
| 253 | additional bytes, with the number of these bytes indicated by the |
| 254 | values of the 5 bits 24, 25, 25 and 27. |
| 255 | |
| 256 | It is possible to encode a particular argument in many ways with this |
| 257 | representation. This implementation always uses the smallest |
| 258 | possible representation. This conforms with CBOR preferred encoding. |
| 259 | |
| 260 | This function inserts them into the output buffer at the specified |
| 261 | position. AppendEncodedTypeAndNumber() appends to the end. |
| 262 | |
| 263 | This function takes care of converting to network byte order. |
| 264 | |
| 265 | This function is also used to insert floats and doubles. Before this |
| 266 | function is called the float or double must be copied into a |
| 267 | uint64_t. That is how they are passed in. They are then converted to |
| 268 | network byte order correctly. The uMinLen parameter makes sure that |
| 269 | even if all the digits of a half, float or double are 0 it is still |
| 270 | correctly encoded in 2, 4 or 8 bytes. |
| 271 | */ |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 272 | /* |
| 273 | This code does endian conversion without hton or knowing the |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 274 | endianness of the machine using masks and shifts. This avoids the |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 275 | dependency on hton and the mess of figuring out how to find the |
| 276 | machine's endianness. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 277 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 278 | This is a good efficient implementation on little-endian machines. |
| 279 | A faster and small implementation is possible on big-endian |
| 280 | machines because CBOR/network byte order is big endian. However |
| 281 | big endian machines are uncommon. |
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 | On x86, it is about 200 bytes instead of 500 bytes for the more |
| 284 | formal unoptimized code. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 285 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 286 | This also does the CBOR preferred shortest encoding for integers |
| 287 | and is called to do endian conversion for floats. |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 288 | |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 289 | It works backwards from the LSB to the MSB as needed. |
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 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 292 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 293 | /* |
| 294 | The type int is used here for several variables because of the way |
| 295 | integer promotion works in C for integer variables that are |
| 296 | uint8_t or uint16_t. The basic rule is that they will always be |
| 297 | promoted to int if they will fit. All of these integer variables |
| 298 | need only hold values less than 255 or are promoted from uint8_t, |
| 299 | so they will always fit into an int. Note that promotion is only |
| 300 | to unsigned int if the value won't fit into an int even if the |
| 301 | promotion is for an unsigned like uint8_t. |
| 302 | |
| 303 | By declaring them int, there are few implicit conversions and fewer |
| 304 | casts needed. Code size is reduced a little. It also makes static |
| 305 | analyzers happier. |
| 306 | |
| 307 | Note also that declaring them uint8_t won't stop integer wrap |
| 308 | around if the code is wrong. It won't make the code more correct. |
| 309 | |
| 310 | https://stackoverflow.com/questions/46073295/implicit-type-promotion-rules |
| 311 | https://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be |
| 312 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 313 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 314 | // Buffer must have room for the largest CBOR HEAD + one extra as the |
| 315 | // one extra is needed for this code to work as it does a pre-decrement. |
| 316 | if(buffer.len < QCBOR_HEAD_BUFFER_SIZE) { |
| 317 | return NULLUsefulBufC; |
| 318 | } |
| 319 | |
| 320 | // Pointer to last valid byte in the buffer |
| 321 | uint8_t * const pBufferEnd = &((uint8_t *)buffer.ptr)[QCBOR_HEAD_BUFFER_SIZE-1]; |
| 322 | |
| 323 | // Point to the last byte and work backwards |
| 324 | uint8_t *pByte = pBufferEnd; |
| 325 | // The 5 bits in the initial byte that are not the major type |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 326 | int nAdditionalInfo; |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 327 | |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 328 | if (uMajorType == CBOR_MAJOR_NONE_TYPE_ARRAY_INDEFINITE_LEN) { |
| 329 | uMajorType = CBOR_MAJOR_TYPE_ARRAY; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 330 | nAdditionalInfo = LEN_IS_INDEFINITE; |
Jan Jongboom | 5d82788 | 2019-08-07 12:51:15 +0200 | [diff] [blame] | 331 | } else if (uMajorType == CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN) { |
| 332 | uMajorType = CBOR_MAJOR_TYPE_MAP; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 333 | nAdditionalInfo = LEN_IS_INDEFINITE; |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 334 | } else if (uArgument < CBOR_TWENTY_FOUR && uMinLen == 0) { |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 335 | // Simple case where argument is < 24 |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 336 | nAdditionalInfo = (int)uArgument; |
| 337 | } else if (uMajorType == CBOR_MAJOR_TYPE_SIMPLE && uArgument == CBOR_SIMPLE_BREAK) { |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 338 | // Break statement can be encoded in single byte too (0xff) |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 339 | nAdditionalInfo = (int)uArgument; |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 340 | } else { |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 341 | /* |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 342 | Encode argument in 1,2,4 or 8 bytes. Outer loop |
| 343 | runs once for 1 byte and 4 times for 8 bytes. |
| 344 | Inner loop runs 1, 2 or 4 times depending on |
| 345 | outer loop counter. This works backwards taking |
| 346 | 8 bits off the argument being encoded at a time |
| 347 | until all bits from uNumber have been encoded |
| 348 | and the minimum encoding size is reached. |
| 349 | Minimum encoding size is for floating point |
| 350 | numbers with zero bytes. |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 351 | */ |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 352 | static const uint8_t aIterate[] = {1,1,2,4}; |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 353 | |
| 354 | // The parameter passed in is unsigned, but goes negative in the loop |
| 355 | // so it must be converted to a signed value. |
| 356 | int nMinLen = (int)uMinLen; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 357 | int i; |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 358 | for(i = 0; uArgument || nMinLen > 0; i++) { |
| 359 | const int nIterations = (int)aIterate[i]; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 360 | for(int j = 0; j < nIterations; j++) { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 361 | *--pByte = (uint8_t)(uArgument & 0xff); |
| 362 | uArgument = uArgument >> 8; |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 363 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 364 | nMinLen -= nIterations; |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 365 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 366 | // Additional info is the encoding of the number of additional |
| 367 | // bytes to encode argument. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 368 | nAdditionalInfo = LEN_IS_ONE_BYTE-1 + i; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 369 | } |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 370 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 371 | /* |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 372 | This expression integer-promotes to type int. The code above in |
| 373 | function guarantees that nAdditionalInfo will never be larger than |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 374 | 0x1f. The caller may pass in a too-large uMajor type. The |
| 375 | conversion to unint8_t will cause an integer wrap around and |
| 376 | incorrect CBOR will be generated, but no security issue will |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 377 | occur. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 378 | */ |
| 379 | *--pByte = (uint8_t)((uMajorType << 5) + nAdditionalInfo); |
| 380 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 381 | #ifdef EXTRA_ENCODE_HEAD_CHECK |
| 382 | /* This is a sanity check that can be turned on to verify the pointer |
| 383 | * math in this function is not going wrong. Turn it on and run the |
| 384 | * whole test suite to perform the check. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 385 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 386 | if(pBufferEnd - pByte > 9 || pBufferEnd - pByte < 1 || pByte < (uint8_t *)buffer.ptr) { |
| 387 | return NULLUsefulBufC; |
| 388 | } |
| 389 | #endif |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 390 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 391 | // Length will not go negative because the loops run for at most 8 decrements |
| 392 | // of pByte, only one other decrement is made, and the array is sized |
| 393 | // for this. |
| 394 | return (UsefulBufC){pByte, (size_t)(pBufferEnd - pByte)}; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 398 | /** |
| 399 | @brief Append the CBOR head, the major type and argument |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 400 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 401 | @param me Encoder context. |
| 402 | @param uMajorType Major type to insert. |
| 403 | @param uArgument The argument (an integer value or a length). |
| 404 | @param uMinLen The minimum number of bytes for encoding the CBOR argument. |
| 405 | |
| 406 | This formats the CBOR "head" and appends it to the output. |
| 407 | */ |
| 408 | static void AppendCBORHead(QCBOREncodeContext *me, uint8_t uMajorType, uint64_t uArgument, uint8_t uMinLen) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 409 | { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 410 | // A stack buffer large enough for a CBOR head |
| 411 | UsefulBuf_MAKE_STACK_UB (pBufferForEncodedHead, QCBOR_HEAD_BUFFER_SIZE); |
| 412 | |
| 413 | UsefulBufC EncodedHead = QCBOREncode_EncodeHead(pBufferForEncodedHead, |
| 414 | uMajorType, |
| 415 | uMinLen, |
| 416 | uArgument); |
| 417 | |
| 418 | /* No check for EncodedHead == NULLUsefulBufC is performed here to |
| 419 | * save object code. It is very clear that pBufferForEncodedHead |
| 420 | * is the correct size. If EncodedHead == NULLUsefulBufC then |
| 421 | * UsefulOutBuf_AppendUsefulBuf() will do nothing so there is |
| 422 | * no security hole introduced. |
| 423 | */ |
| 424 | |
| 425 | UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), EncodedHead); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 429 | /** |
| 430 | @brief Insert the CBOR head for a map, array or wrapped bstr |
| 431 | |
| 432 | @param me QCBOR encoding context. |
| 433 | @param uMajorType One of CBOR_MAJOR_TYPE_XXXX. |
| 434 | @param uLen The length of the data item. |
| 435 | |
| 436 | When an array, map or bstr was opened, nothing was done but note |
| 437 | the position. This function goes back to that position and inserts |
| 438 | the CBOR Head with the major type and length. |
| 439 | */ |
| 440 | static void InsertCBORHead(QCBOREncodeContext *me, uint8_t uMajorType, size_t uLen) |
| 441 | { |
| 442 | if(me->uError == QCBOR_SUCCESS) { |
| 443 | if(!Nesting_IsInNest(&(me->nesting))) { |
| 444 | me->uError = QCBOR_ERR_TOO_MANY_CLOSES; |
| 445 | } else if(Nesting_GetMajorType(&(me->nesting)) != uMajorType) { |
| 446 | me->uError = QCBOR_ERR_CLOSE_MISMATCH; |
| 447 | } else { |
| 448 | // A stack buffer large enough for a CBOR head |
| 449 | UsefulBuf_MAKE_STACK_UB (pBufferForEncodedHead,QCBOR_HEAD_BUFFER_SIZE); |
| 450 | |
| 451 | UsefulBufC EncodedHead = QCBOREncode_EncodeHead(pBufferForEncodedHead, |
| 452 | uMajorType, |
| 453 | 0, |
| 454 | uLen); |
| 455 | |
| 456 | /* No check for EncodedHead == NULLUsefulBufC is performed here to |
| 457 | * save object code. It is very clear that pBufferForEncodedHead |
| 458 | * is the correct size. If EncodedHead == NULLUsefulBufC then |
| 459 | * UsefulOutBuf_InsertUsefulBuf() will do nothing so there is |
| 460 | * no security whole introduced. |
| 461 | */ |
| 462 | UsefulOutBuf_InsertUsefulBuf(&(me->OutBuf), EncodedHead, Nesting_GetStartPos(&(me->nesting)) ); |
| 463 | |
| 464 | Nesting_Decrease(&(me->nesting)); |
| 465 | } |
| 466 | } |
| 467 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 468 | |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 469 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 470 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 471 | Public functions for adding integers. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 472 | */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 473 | void QCBOREncode_AddUInt64(QCBOREncodeContext *me, uint64_t uValue) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 474 | { |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 475 | if(me->uError == QCBOR_SUCCESS) { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 476 | AppendCBORHead(me, CBOR_MAJOR_TYPE_POSITIVE_INT, uValue, 0); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 477 | me->uError = Nesting_Increment(&(me->nesting)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 478 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame] | 481 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 482 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 483 | Public functions for adding unsigned. See qcbor/qcbor_encode.h |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 484 | */ |
| 485 | void QCBOREncode_AddInt64(QCBOREncodeContext *me, int64_t nNum) |
| 486 | { |
| 487 | if(me->uError == QCBOR_SUCCESS) { |
| 488 | uint8_t uMajorType; |
| 489 | uint64_t uValue; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 490 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 491 | if(nNum < 0) { |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 492 | // In CBOR -1 encodes as 0x00 with major type negative int. |
| 493 | uValue = (uint64_t)(-nNum - 1); |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 494 | uMajorType = CBOR_MAJOR_TYPE_NEGATIVE_INT; |
| 495 | } else { |
| 496 | uValue = (uint64_t)nNum; |
| 497 | uMajorType = CBOR_MAJOR_TYPE_POSITIVE_INT; |
| 498 | } |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 499 | AppendCBORHead(me, uMajorType, uValue, 0); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 500 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 501 | me->uError = Nesting_Increment(&(me->nesting)); |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | |
| 506 | /* |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 507 | Semi-private function. It is exposed to user of the interface, but |
| 508 | they will usually call one of the inline wrappers rather than this. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 509 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 510 | See qcbor/qcbor_encode.h |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 511 | |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 512 | Does the work of adding actual strings bytes to the CBOR output (as |
| 513 | opposed to numbers and opening / closing aggregate types). |
| 514 | |
| 515 | There are four use cases: |
| 516 | CBOR_MAJOR_TYPE_BYTE_STRING -- Byte strings |
| 517 | CBOR_MAJOR_TYPE_TEXT_STRING -- Text strings |
| 518 | CBOR_MAJOR_NONE_TYPE_RAW -- Already-encoded CBOR |
| 519 | CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY -- Special case |
| 520 | |
| 521 | The first two add the type and length plus the actual bytes. The |
| 522 | third just adds the bytes as the type and length are presumed to be |
| 523 | in the bytes. The fourth just adds the type and length for the very |
| 524 | special case of QCBOREncode_AddBytesLenOnly(). |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 525 | */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 526 | void QCBOREncode_AddBuffer(QCBOREncodeContext *me, uint8_t uMajorType, UsefulBufC Bytes) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 527 | { |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 528 | if(me->uError == QCBOR_SUCCESS) { |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 529 | // If it is not Raw CBOR, add the type and the length |
| 530 | if(uMajorType != CBOR_MAJOR_NONE_TYPE_RAW) { |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 531 | uint8_t uRealMajorType = uMajorType; |
| 532 | if(uRealMajorType == CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) { |
| 533 | uRealMajorType = CBOR_MAJOR_TYPE_BYTE_STRING; |
| 534 | } |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 535 | AppendCBORHead(me, uRealMajorType, Bytes.len, 0); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 536 | } |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 537 | |
Laurence Lundblade | da53227 | 2019-04-07 11:40:17 -0700 | [diff] [blame] | 538 | if(uMajorType != CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) { |
| 539 | // Actually add the bytes |
| 540 | UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), Bytes); |
| 541 | } |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 542 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 543 | // Update the array counting if there is any nesting at all |
| 544 | me->uError = Nesting_Increment(&(me->nesting)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 548 | |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 549 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 550 | Public functions for adding a tag. See qcbor/qcbor_encode.h |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 551 | */ |
| 552 | void QCBOREncode_AddTag(QCBOREncodeContext *me, uint64_t uTag) |
| 553 | { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 554 | AppendCBORHead(me, CBOR_MAJOR_TYPE_OPTIONAL, uTag, 0); |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame] | 558 | /* |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 559 | Semi-private function. It is exposed to user of the interface, |
| 560 | 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] | 561 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 562 | See header qcbor/qcbor_encode.h |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame] | 563 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 564 | void QCBOREncode_AddType7(QCBOREncodeContext *me, uint8_t uMinLen, uint64_t uNum) |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 565 | { |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 566 | if(me->uError == QCBOR_SUCCESS) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 567 | if(uNum >= CBOR_SIMPLEV_RESERVED_START && uNum <= CBOR_SIMPLEV_RESERVED_END) { |
| 568 | me->uError = QCBOR_ERR_UNSUPPORTED; |
| 569 | } else { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 570 | // AppendHead() does endian swapping for the float / double |
| 571 | AppendCBORHead(me, CBOR_MAJOR_TYPE_SIMPLE, uNum, uMinLen); |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 572 | me->uError = Nesting_Increment(&(me->nesting)); |
| 573 | } |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 574 | } |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 575 | } |
| 576 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 577 | |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 578 | /* |
| 579 | Public functions for adding a double. See qcbor/qcbor_encode.h |
| 580 | */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 581 | void QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *me, double dNum) |
| 582 | { |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame^] | 583 | QCBOREncode_AddType7(me, |
| 584 | sizeof(uint64_t), |
| 585 | UsefulBufUtil_CopyDoubleToUint64(dNum)); |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 588 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 589 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 590 | Public functions for adding a double. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 591 | */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 592 | void QCBOREncode_AddDouble(QCBOREncodeContext *me, double dNum) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 593 | { |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 594 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 595 | const IEEE754_union uNum = IEEE754_DoubleToSmallest(dNum); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame^] | 596 | |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 597 | QCBOREncode_AddType7(me, uNum.uSize, uNum.uValue); |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 598 | #else |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 599 | QCBOREncode_AddDoubleNoPreferred(me, dNum); |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 600 | #endif |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 601 | } |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 602 | |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 603 | |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 604 | /* |
| 605 | Public functions for adding a float. See qcbor/qcbor_encode.h |
| 606 | */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 607 | void QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *me, float fNum) |
| 608 | { |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame^] | 609 | QCBOREncode_AddType7(me, |
| 610 | sizeof(uint32_t), |
| 611 | UsefulBufUtil_CopyFloatToUint32(fNum)); |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
| 615 | /* |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 616 | Public functions for adding a float. See qcbor/qcbor_encode.h |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 617 | */ |
| 618 | void QCBOREncode_AddFloat(QCBOREncodeContext *me, float fNum) |
| 619 | { |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 620 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 621 | const IEEE754_union uNum = IEEE754_FloatToSmallest(fNum); |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame^] | 622 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 623 | QCBOREncode_AddType7(me, uNum.uSize, uNum.uValue); |
| 624 | #else |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 625 | QCBOREncode_AddFloatNoPreferred(me, fNum); |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 626 | #endif |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 630 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 631 | /* |
| 632 | Semi-public function. It is exposed to the user of the interface, but |
| 633 | one of the inline wrappers will usually be called rather than this. |
| 634 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 635 | See qcbor/qcbor_encode.h |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 636 | */ |
| 637 | void QCBOREncode_AddExponentAndMantissa(QCBOREncodeContext *pMe, |
| 638 | uint64_t uTag, |
| 639 | UsefulBufC BigNumMantissa, |
| 640 | bool bBigNumIsNegative, |
| 641 | int64_t nMantissa, |
| 642 | int64_t nExponent) |
| 643 | { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 644 | /* |
| 645 | This is for encoding either a big float or a decimal fraction, |
| 646 | both of which are an array of two items, an exponent and a |
| 647 | mantissa. The difference between the two is that the exponent is |
| 648 | base-2 for big floats and base-10 for decimal fractions, but that |
| 649 | has no effect on the code here. |
| 650 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 651 | QCBOREncode_AddTag(pMe, uTag); |
| 652 | QCBOREncode_OpenArray(pMe); |
| 653 | QCBOREncode_AddInt64(pMe, nExponent); |
| 654 | if(!UsefulBuf_IsNULLC(BigNumMantissa)) { |
| 655 | if(bBigNumIsNegative) { |
| 656 | QCBOREncode_AddNegativeBignum(pMe, BigNumMantissa); |
| 657 | } else { |
| 658 | QCBOREncode_AddPositiveBignum(pMe, BigNumMantissa); |
| 659 | } |
| 660 | } else { |
| 661 | QCBOREncode_AddInt64(pMe, nMantissa); |
| 662 | } |
| 663 | QCBOREncode_CloseArray(pMe); |
| 664 | } |
| 665 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 666 | |
| 667 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 668 | /* |
| 669 | Semi-public function. It is exposed to user of the interface, |
| 670 | 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] | 671 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 672 | See qcbor/qcbor_encode.h |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 673 | */ |
| 674 | void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType) |
| 675 | { |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 676 | // Add one item to the nesting level we are in for the new map or array |
| 677 | me->uError = Nesting_Increment(&(me->nesting)); |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 678 | if(me->uError == QCBOR_SUCCESS) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 679 | /* |
| 680 | The offset where the length of an array or map will get written |
| 681 | is stored in a uint32_t, not a size_t to keep stack usage |
| 682 | smaller. This checks to be sure there is no wrap around when |
| 683 | recording the offset. Note that on 64-bit machines CBOR larger |
| 684 | than 4GB can be encoded as long as no array / map offsets occur |
| 685 | past the 4GB mark, but the public interface says that the |
| 686 | maximum is 4GB to keep the discussion simpler. |
| 687 | */ |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 688 | size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf)); |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 689 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 690 | /* |
| 691 | QCBOR_MAX_ARRAY_OFFSET is slightly less than UINT32_MAX so this |
| 692 | code can run on a 32-bit machine and tests can pass on a 32-bit |
| 693 | machine. If it was exactly UINT32_MAX, then this code would not |
| 694 | compile or run on a 32-bit machine and an #ifdef or some |
| 695 | machine size detection would be needed reducing portability. |
| 696 | */ |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 697 | if(uEndPosition >= QCBOR_MAX_ARRAY_OFFSET) { |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 698 | me->uError = QCBOR_ERR_BUFFER_TOO_LARGE; |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 699 | |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 700 | } else { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 701 | // Increase nesting level because this is a map or array. Cast |
| 702 | // from size_t to uin32_t is safe because of check above |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 703 | me->uError = Nesting_Increase(&(me->nesting), uMajorType, (uint32_t)uEndPosition); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 704 | } |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 705 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 708 | |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 709 | /* |
| 710 | Semi-public function. It is exposed to user of the interface, |
| 711 | but they will usually call one of the inline wrappers rather than this. |
| 712 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 713 | See qcbor/qcbor_encode.h |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 714 | */ |
| 715 | void QCBOREncode_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType) |
| 716 | { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 717 | // Insert the indefinite length marker (0x9f for arrays, 0xbf for maps) |
| 718 | AppendCBORHead(me, uMajorType, 0, 0); |
| 719 | // Call the definite-length opener just to do the bookkeeping for |
| 720 | // nesting. It will record the position of the opening item in |
| 721 | // the encoded output but this is not used when closing this open. |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 722 | QCBOREncode_OpenMapOrArray(me, uMajorType); |
| 723 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 724 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 725 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 726 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 727 | Public functions for closing arrays and maps. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 728 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 729 | void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType) |
Laurence Lundblade | a954db9 | 2018-09-28 19:27:31 -0700 | [diff] [blame] | 730 | { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 731 | InsertCBORHead(me, uMajorType, Nesting_GetCount(&(me->nesting))); |
| 732 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 733 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 734 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 735 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 736 | Public functions for closing bstr wrapping. See qcbor/qcbor_encode.h |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 737 | */ |
| 738 | void QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *me, bool bIncludeCBORHead, UsefulBufC *pWrappedCBOR) |
| 739 | { |
| 740 | const size_t uInsertPosition = Nesting_GetStartPos(&(me->nesting)); |
| 741 | const size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 742 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 743 | // This can't go negative because the UsefulOutBuf always only grows |
| 744 | // and never shrinks. UsefulOutBut itself also has defenses such that |
| 745 | // it won't write where it should not even if given hostile input lengths. |
| 746 | const size_t uBstrLen = uEndPosition - uInsertPosition; |
| 747 | |
| 748 | // Actually insert |
| 749 | InsertCBORHead(me, CBOR_MAJOR_TYPE_BYTE_STRING, uBstrLen); |
| 750 | |
| 751 | if(pWrappedCBOR) { |
| 752 | /* |
| 753 | Return pointer and length to the enclosed encoded CBOR. The |
| 754 | intended use is for it to be hashed (e.g., SHA-256) in a COSE |
| 755 | implementation. This must be used right away, as the pointer |
| 756 | and length go invalid on any subsequent calls to this function |
| 757 | because there might be calls to InsertEncodedTypeAndNumber() |
| 758 | that slides data to the right. |
| 759 | */ |
| 760 | size_t uStartOfNew = uInsertPosition; |
| 761 | if(!bIncludeCBORHead) { |
| 762 | // Skip over the CBOR head to just get the inserted bstr |
| 763 | const size_t uNewEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf)); |
| 764 | uStartOfNew += uNewEndPosition - uEndPosition; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 765 | } |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 766 | const UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf)); |
| 767 | *pWrappedCBOR = UsefulBuf_Tail(PartialResult, uStartOfNew); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 771 | |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 772 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 773 | Public functions for closing arrays and maps. See qcbor/qcbor_encode.h |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 774 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 775 | void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType) |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 776 | { |
| 777 | if(me->uError == QCBOR_SUCCESS) { |
| 778 | if(!Nesting_IsInNest(&(me->nesting))) { |
| 779 | me->uError = QCBOR_ERR_TOO_MANY_CLOSES; |
| 780 | } else if(Nesting_GetMajorType(&(me->nesting)) != uMajorType) { |
| 781 | me->uError = QCBOR_ERR_CLOSE_MISMATCH; |
| 782 | } else { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 783 | // Append the break marker (0xff for both arrays and maps) |
| 784 | AppendCBORHead(me, CBOR_MAJOR_TYPE_SIMPLE, CBOR_SIMPLE_BREAK, 0); |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 785 | |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 786 | Nesting_Decrease(&(me->nesting)); |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 791 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 792 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 793 | Public functions to finish and get the encoded result. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 794 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 795 | QCBORError QCBOREncode_Finish(QCBOREncodeContext *me, UsefulBufC *pEncodedCBOR) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 796 | { |
Laurence Lundblade | f607a2a | 2019-07-05 21:25:25 -0700 | [diff] [blame] | 797 | QCBORError uReturn = QCBOREncode_GetErrorState(me); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 798 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 799 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 800 | goto Done; |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 801 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 802 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 803 | if (Nesting_IsInNest(&(me->nesting))) { |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 804 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 805 | goto Done; |
| 806 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 807 | |
Laurence Lundblade | da3f082 | 2018-09-18 19:49:02 -0700 | [diff] [blame] | 808 | *pEncodedCBOR = UsefulOutBuf_OutUBuf(&(me->OutBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 809 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 810 | Done: |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 811 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 812 | } |
| 813 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 814 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 815 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 816 | Public functions to finish and get the encoded result. See qcbor/qcbor_encode.h |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 817 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 818 | QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *me, size_t *puEncodedLen) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 819 | { |
Laurence Lundblade | da3f082 | 2018-09-18 19:49:02 -0700 | [diff] [blame] | 820 | UsefulBufC Enc; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 821 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 822 | QCBORError nReturn = QCBOREncode_Finish(me, &Enc); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 823 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 824 | if(nReturn == QCBOR_SUCCESS) { |
Laurence Lundblade | da3f082 | 2018-09-18 19:49:02 -0700 | [diff] [blame] | 825 | *puEncodedLen = Enc.len; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 826 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 827 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 828 | return nReturn; |
| 829 | } |
| 830 | |
| 831 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 832 | |
| 833 | |
| 834 | /* |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 835 | Object code sizes on 64-bit x86 with GCC -Os Jan 2020. GCC compiles smaller |
| 836 | than LLVM and optimizations have been made to decrease code size. Bigfloat, |
| 837 | Decimal fractions and indefinite length encoding were added to increase code |
| 838 | size. Bstr wrapping is now separate which means if you don't use it, it gets |
| 839 | dead stripped. |
| 840 | |
| 841 | _QCBOREncode_EncodeHead 187 |
| 842 | _QCBOREncode_CloseBstrWrap2: 154 |
| 843 | _QCBOREncode_AddExponentAndMantissa: 144 |
| 844 | _QCBOREncode_AddBuffer 105 |
| 845 | _QCBOREncode_OpenMapOrArray 101 |
| 846 | _QCBOREncode_CloseMapOrArrayIndefiniteLength: 72 |
| 847 | _QCBOREncode_Finish 71 |
| 848 | _InsertCBORHead.part.0 66 |
| 849 | _QCBOREncode_CloseMapOrArray 64 |
| 850 | _QCBOREncode_AddType7 58 |
| 851 | _QCBOREncode_AddInt64 57 |
| 852 | _AppendCBORHead 54 |
| 853 | _QCBOREncode_AddUInt64 40 |
| 854 | _QCBOREncode_Init 38 |
| 855 | _Nesting_Increment.isra.0 36 |
| 856 | _QCBOREncode_FinishGetSize: 34 |
| 857 | _QCBOREncode_AddDouble: 26 |
| 858 | _QCBOREncode_AddTag: 15 |
| 859 | Total 1322 |
| 860 | Min_encode use case 776 |
| 861 | |
| 862 | |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 863 | 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] | 864 | |
Laurence Lundblade | 9c09739 | 2018-12-30 13:52:24 -0800 | [diff] [blame] | 865 | _QCBOREncode_Init 69 |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 866 | _QCBOREncode_AddUInt64 76 |
| 867 | _QCBOREncode_AddInt64 87 |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 868 | _QCBOREncode_AddBuffer 113 |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 869 | _QCBOREncode_AddTag 27 |
Laurence Lundblade | 9c09739 | 2018-12-30 13:52:24 -0800 | [diff] [blame] | 870 | _QCBOREncode_AddType7 87 |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 871 | _QCBOREncode_AddDouble 36 |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 872 | _QCBOREncode_OpenMapOrArray 103 |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 873 | _QCBOREncode_CloseMapOrArray 181 |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 874 | _InsertEncodedTypeAndNumber 190 |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 875 | _QCBOREncode_Finish 72 |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 876 | _QCBOREncode_FinishGetSize 70 |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 877 | |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 878 | Total is about 1.1KB |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 879 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 880 | _QCBOREncode_CloseMapOrArray is larger because it has a lot |
| 881 | of nesting tracking to do and much of Nesting_ inlines |
| 882 | into it. It probably can't be reduced much. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 883 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 884 | If the error returned by Nesting_Increment() can be ignored |
| 885 | because the limit is so high and the consequence of exceeding |
| 886 | is proved to be inconsequential, then a lot of if(me->uError) |
| 887 | instance can be removed, saving some code. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 888 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 889 | */ |