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 | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 3 | Copyright (c) 2018-2024, Laurence Lundblade. |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 4 | Copyright (c) 2021, Arm Limited. |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 5 | All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 6 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 7 | Redistribution and use in source and binary forms, with or without |
| 8 | modification, are permitted provided that the following conditions are |
| 9 | met: |
| 10 | * Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
| 12 | * Redistributions in binary form must reproduce the above |
| 13 | copyright notice, this list of conditions and the following |
| 14 | disclaimer in the documentation and/or other materials provided |
| 15 | with the distribution. |
| 16 | * Neither the name of The Linux Foundation nor the names of its |
| 17 | contributors, nor the name "Laurence Lundblade" may be used to |
| 18 | endorse or promote products derived from this software without |
| 19 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 20 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 21 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 22 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 25 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 30 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 31 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 32 | =============================================================================*/ |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 33 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 34 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 35 | #include "qcbor/qcbor_encode.h" |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 36 | #include "ieee754.h" |
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 | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 39 | /** |
| 40 | * @file qcbor_encode.c |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 41 | * |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 42 | * The entire implementation of the QCBOR encoder. |
| 43 | */ |
| 44 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 45 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 46 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 47 | * == Nesting Tracking == |
| 48 | * |
| 49 | * The following functions and data type QCBORTrackNesting implement |
| 50 | * the nesting management for encoding. |
| 51 | * |
| 52 | * CBOR's two nesting types, arrays and maps, are tracked here. There |
| 53 | * is a limit of QCBOR_MAX_ARRAY_NESTING to the number of arrays and |
| 54 | * maps that can be nested in one encoding so the encoding context |
| 55 | * stays small enough to fit on the stack. |
| 56 | * |
| 57 | * When an array/map is opened, pCurrentNesting points to the element |
| 58 | * in pArrays that records the type, start position and accumulates a |
| 59 | * count of the number of items added. When closed the start position |
| 60 | * is used to go back and fill in the type and number of items in the |
| 61 | * array/map. |
| 62 | * |
| 63 | * Encoded output can be a CBOR Sequence (RFC 8742) in which case |
| 64 | * there is no top-level array or map. It starts out with a string, |
| 65 | * integer or other non-aggregate type. It may have an array or map |
| 66 | * other than at the start, in which case that nesting is tracked |
| 67 | * here. |
| 68 | * |
| 69 | * QCBOR has a special feature to allow constructing byte string |
| 70 | * wrapped CBOR directly into the output buffer, so no extra buffer is |
| 71 | * needed for byte string wrapping. This is implemented as nesting |
| 72 | * with the type CBOR_MAJOR_TYPE_BYTE_STRING and is tracked here. Byte |
| 73 | * string wrapped CBOR is used by COSE for data that is to be hashed. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 74 | */ |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 75 | static inline void |
| 76 | Nesting_Init(QCBORTrackNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 77 | { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 78 | /* Assumes pNesting has been zeroed. */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 79 | pNesting->pCurrentNesting = &pNesting->pArrays[0]; |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 80 | /* Implied CBOR array at the top nesting level. This is never |
| 81 | * returned, but makes the item count work correctly. |
| 82 | */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 83 | pNesting->pCurrentNesting->uMajorType = CBOR_MAJOR_TYPE_ARRAY; |
| 84 | } |
| 85 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 86 | static inline uint8_t |
| 87 | Nesting_Increase(QCBORTrackNesting *pNesting, |
| 88 | uint8_t uMajorType, |
| 89 | uint32_t uPos) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 90 | { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 91 | if(pNesting->pCurrentNesting == &pNesting->pArrays[QCBOR_MAX_ARRAY_NESTING]) { |
Laurence Lundblade | 29497c0 | 2020-07-11 15:44:03 -0700 | [diff] [blame] | 92 | return QCBOR_ERR_ARRAY_NESTING_TOO_DEEP; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 93 | } else { |
| 94 | pNesting->pCurrentNesting++; |
| 95 | pNesting->pCurrentNesting->uCount = 0; |
| 96 | pNesting->pCurrentNesting->uStart = uPos; |
| 97 | pNesting->pCurrentNesting->uMajorType = uMajorType; |
Laurence Lundblade | 29497c0 | 2020-07-11 15:44:03 -0700 | [diff] [blame] | 98 | return QCBOR_SUCCESS; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 99 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 102 | static inline void |
| 103 | Nesting_Decrease(QCBORTrackNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 104 | { |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 105 | if(pNesting->pCurrentNesting > &pNesting->pArrays[0]) { |
| 106 | pNesting->pCurrentNesting--; |
| 107 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 110 | static inline uint8_t |
| 111 | Nesting_Increment(QCBORTrackNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 112 | { |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 113 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 114 | if(1 >= QCBOR_MAX_ITEMS_IN_ARRAY - pNesting->pCurrentNesting->uCount) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 115 | return QCBOR_ERR_ARRAY_TOO_LONG; |
| 116 | } |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 117 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 118 | |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 119 | pNesting->pCurrentNesting->uCount++; |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 120 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 121 | return QCBOR_SUCCESS; |
| 122 | } |
| 123 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 124 | static inline void |
| 125 | Nesting_Decrement(QCBORTrackNesting *pNesting) |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 126 | { |
| 127 | /* No error check for going below 0 here needed because this |
| 128 | * is only used by QCBOREncode_CancelBstrWrap() and it checks |
| 129 | * the nesting level before calling this. */ |
| 130 | pNesting->pCurrentNesting->uCount--; |
| 131 | } |
| 132 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 133 | static inline uint16_t |
| 134 | Nesting_GetCount(QCBORTrackNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 135 | { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 136 | /* The nesting count recorded is always the actual number of |
| 137 | * individual data items in the array or map. For arrays CBOR uses |
| 138 | * the actual item count. For maps, CBOR uses the number of pairs. |
| 139 | * This function returns the number needed for the CBOR encoding, |
| 140 | * so it divides the number of items by two for maps to get the |
| 141 | * number of pairs. |
| 142 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 143 | if(pNesting->pCurrentNesting->uMajorType == CBOR_MAJOR_TYPE_MAP) { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 144 | /* Cast back to uint16_t after integer promotion from bit shift */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 145 | return (uint16_t)(pNesting->pCurrentNesting->uCount >> 1); |
| 146 | } else { |
| 147 | return pNesting->pCurrentNesting->uCount; |
| 148 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 151 | static inline uint32_t |
| 152 | Nesting_GetStartPos(QCBORTrackNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 153 | { |
| 154 | return pNesting->pCurrentNesting->uStart; |
| 155 | } |
| 156 | |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 157 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 158 | static inline uint8_t |
| 159 | Nesting_GetMajorType(QCBORTrackNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 160 | { |
| 161 | return pNesting->pCurrentNesting->uMajorType; |
| 162 | } |
| 163 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 164 | static inline bool |
| 165 | Nesting_IsInNest(QCBORTrackNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 166 | { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 167 | return pNesting->pCurrentNesting == &pNesting->pArrays[0] ? false : true; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 168 | } |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 169 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 170 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 171 | |
| 172 | |
| 173 | |
| 174 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 175 | * == Major CBOR Types == |
| 176 | * |
| 177 | * Encoding of the major CBOR types is by these functions: |
| 178 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 179 | * CBOR Major Type Public Function |
| 180 | * 0 QCBOREncode_AddUInt64() |
| 181 | * 0, 1 QCBOREncode_AddUInt64(), QCBOREncode_AddInt64() |
| 182 | * 2, 3 QCBOREncode_AddBuffer() |
| 183 | * 4, 5 QCBOREncode_OpenMapOrArray(), QCBOREncode_CloseMapOrArray(), |
| 184 | * QCBOREncode_OpenMapOrArrayIndefiniteLength(), |
| 185 | * QCBOREncode_CloseMapOrArrayIndefiniteLength() |
| 186 | * 6 QCBOREncode_AddTag() |
| 187 | * 7 QCBOREncode_AddDouble(), QCBOREncode_AddFloat(), |
| 188 | * QCBOREncode_AddDoubleNoPreferred(), |
| 189 | * QCBOREncode_AddFloatNoPreferred(), QCBOREncode_AddType7() |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 190 | * |
| 191 | * Additionally, encoding of decimal fractions and bigfloats is by |
| 192 | * QCBOREncode_AddExponentAndMantissa() and byte strings that wrap |
| 193 | * encoded CBOR are handled by QCBOREncode_OpenMapOrArray() and |
| 194 | * QCBOREncode_CloseBstrWrap2(). |
| 195 | * |
| 196 | * |
| 197 | * == Error Tracking Plan == |
| 198 | * |
| 199 | * Errors are tracked internally and not returned until |
| 200 | * QCBOREncode_Finish() or QCBOREncode_GetErrorState() is called. The |
| 201 | * CBOR errors are in me->uError. UsefulOutBuf also tracks whether |
| 202 | * the buffer is full or not in its context. Once either of these |
| 203 | * errors is set they are never cleared. Only QCBOREncode_Init() |
| 204 | * resets them. Or said another way, they must never be cleared or |
| 205 | * we'll tell the caller all is good when it is not. |
| 206 | * |
| 207 | * Only one error code is reported by QCBOREncode_Finish() even if |
| 208 | * there are multiple errors. The last one set wins. The caller might |
| 209 | * have to fix one error to reveal the next one they have to fix. |
| 210 | * This is OK. |
| 211 | * |
| 212 | * The buffer full error tracked by UsefulBuf is only pulled out of |
| 213 | * UsefulBuf in QCBOREncode_Finish() so it is the one that usually |
| 214 | * wins. UsefulBuf will never go off the end of the buffer even if it |
| 215 | * is called again and again when full. |
| 216 | * |
| 217 | * QCBOR_DISABLE_ENCODE_USAGE_GUARDS disables about half of the error |
| 218 | * checks here to reduce code size by about 150 bytes leaving only the |
| 219 | * checks for size to avoid buffer overflow. If the calling code is |
| 220 | * completely correct, checks are completely unnecessary. For |
| 221 | * example, there is no need to check that all the opens are matched |
| 222 | * by a close. |
| 223 | * |
| 224 | * QCBOR_DISABLE_ENCODE_USAGE_GUARDS also disables the check for more |
| 225 | * than QCBOR_MAX_ITEMS_IN_ARRAY in an array. Since |
| 226 | * QCBOR_MAX_ITEMS_IN_ARRAY is very large (65,535) it is very unlikely |
| 227 | * to be reached. If it is reached, the count will wrap around to zero |
| 228 | * and CBOR that is not well formed will be produced, but there will |
| 229 | * be no buffers overrun and new security issues in the code. |
| 230 | * |
| 231 | * The 8 errors returned here fall into three categories: |
| 232 | * |
| 233 | * Sizes |
| 234 | * QCBOR_ERR_BUFFER_TOO_LARGE -- Encoded output exceeded UINT32_MAX |
| 235 | * QCBOR_ERR_BUFFER_TOO_SMALL -- Output buffer too small |
| 236 | * QCBOR_ERR_ARRAY_NESTING_TOO_DEEP -- Nesting > QCBOR_MAX_ARRAY_NESTING1 |
| 237 | * QCBOR_ERR_ARRAY_TOO_LONG -- Too many items added to an array/map [1] |
| 238 | * |
| 239 | * Nesting constructed incorrectly |
| 240 | * QCBOR_ERR_TOO_MANY_CLOSES -- More close calls than opens [1] |
| 241 | * QCBOR_ERR_CLOSE_MISMATCH -- Type of close does not match open [1] |
| 242 | * QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN -- Finish called without enough closes [1] |
| 243 | * |
| 244 | * Would generate not-well-formed CBOR |
| 245 | * QCBOR_ERR_ENCODE_UNSUPPORTED -- Simple type between 24 and 31 [1] |
| 246 | * |
| 247 | * [1] indicated disabled by QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 248 | */ |
| 249 | |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 250 | void QCBOREncode_CloseMapUnsorted(QCBOREncodeContext *pMe); // TODO: relocate/doc |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 251 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 252 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 253 | Public function for initialization. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 254 | */ |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 255 | void QCBOREncode_Init(QCBOREncodeContext *pMe, UsefulBuf Storage) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 256 | { |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 257 | memset(pMe, 0, sizeof(QCBOREncodeContext)); |
| 258 | UsefulOutBuf_Init(&(pMe->OutBuf), Storage); |
| 259 | Nesting_Init(&(pMe->nesting)); |
| 260 | pMe->pfnCloseMap = QCBOREncode_CloseMapUnsorted; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 264 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 265 | * Public function to encode a CBOR head. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 266 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 267 | UsefulBufC QCBOREncode_EncodeHead(UsefulBuf buffer, |
| 268 | uint8_t uMajorType, |
| 269 | uint8_t uMinLen, |
| 270 | uint64_t uArgument) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 271 | { |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 272 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 273 | * == Description of the CBOR Head == |
| 274 | * |
| 275 | * The head of a CBOR data item |
| 276 | * +---+-----+ +--------+ +--------+ +--------+ +--------+ |
| 277 | * |M T| A R G U M E N T . . . | |
| 278 | * +---+-----+ +--------+ +--------+ +--------+ ... +--------+ |
| 279 | * |
| 280 | * Every CBOR data item has a "head". It is made up of the "major |
| 281 | * type" and the "argument". |
| 282 | * |
| 283 | * The major type indicates whether the data item is an integer, |
| 284 | * string, array or such. It is encoded in 3 bits giving it a range |
| 285 | * from 0 to 7. 0 indicates the major type is a positive integer, |
| 286 | * 1 a negative integer, 2 a byte string and so on. |
| 287 | * |
| 288 | * These 3 bits are the first part of the "initial byte" in a data |
| 289 | * item. Every data item has an initial byte, and some only have |
| 290 | * the initial byte. |
| 291 | * |
| 292 | * The argument is essentially a number between 0 and UINT64_MAX |
| 293 | * (18446744073709551615). This number is interpreted to mean |
| 294 | * different things for the different major types. For major type |
| 295 | * 0, a positive integer, it is value of the data item. For major |
| 296 | * type 2, a byte string, it is the length in bytes of the byte |
| 297 | * string. For major type 4, an array, it is the number of data |
| 298 | * items in the array. |
| 299 | * |
| 300 | * Special encoding is used so that the argument values less than |
| 301 | * 24 can be encoded very compactly in the same byte as the major |
| 302 | * type is encoded. When the lower 5 bits of the initial byte have |
| 303 | * a value less than 24, then that is the value of the argument. |
| 304 | * |
| 305 | * If the lower 5 bits of the initial byte are less than 24, then |
| 306 | * they are the value of the argument. This allows integer values 0 |
| 307 | * - 23 to be CBOR encoded in just one byte. |
| 308 | * |
| 309 | * When the value of lower 5 bits are 24, 25, 26, or 27 the |
| 310 | * argument is encoded in 1, 2, 4 or 8 bytes following the initial |
| 311 | * byte in network byte order (bit endian). The cases when it is |
| 312 | * 28, 29 and 30 are reserved for future use. The value 31 is a |
| 313 | * special indicator for indefinite length strings, arrays and |
| 314 | * maps. |
| 315 | * |
| 316 | * The lower 5 bits are called the "additional information." |
| 317 | * |
| 318 | * Thus the CBOR head may be 1, 2, 3, 5 or 9 bytes long. |
| 319 | * |
| 320 | * It is legal in CBOR to encode the argument using any of these |
| 321 | * lengths even if it could be encoded in a shorter length. For |
| 322 | * example it is legal to encode a data item representing the |
| 323 | * positive integer 0 in 9 bytes even though it could be encoded in |
| 324 | * only 0. This is legal to allow for for very simple code or even |
| 325 | * hardware-only implementations that just output a register |
| 326 | * directly. |
| 327 | * |
| 328 | * CBOR defines preferred encoding as the encoding of the argument |
| 329 | * in the smallest number of bytes needed to encode it. |
| 330 | * |
| 331 | * This function takes the major type and argument as inputs and |
| 332 | * outputs the encoded CBOR head for them. It does conversion to |
| 333 | * network byte order. It implements CBOR preferred encoding, |
| 334 | * outputting the shortest representation of the argument. |
| 335 | * |
| 336 | * == Endian Conversion == |
| 337 | * |
| 338 | * This code does endian conversion without hton() or knowing the |
| 339 | * endianness of the machine by using masks and shifts. This avoids |
| 340 | * the dependency on hton() and the mess of figuring out how to |
| 341 | * find the machine's endianness. |
| 342 | * |
| 343 | * This is a good efficient implementation on little-endian |
| 344 | * machines. A faster and smaller implementation is possible on |
| 345 | * big-endian machines because CBOR/network byte order is |
| 346 | * big-endian. However big-endian machines are uncommon. |
| 347 | * |
| 348 | * On x86, this is about 150 bytes instead of 500 bytes for the |
| 349 | * original, more formal unoptimized code. |
| 350 | * |
| 351 | * This also does the CBOR preferred shortest encoding for integers |
| 352 | * and is called to do endian conversion for floats. |
| 353 | * |
| 354 | * It works backwards from the least significant byte to the most |
| 355 | * significant byte. |
| 356 | * |
| 357 | * == Floating Point == |
| 358 | * |
| 359 | * When the major type is 7 and the 5 lower bits have the values |
| 360 | * 25, 26 or 27, the argument is a floating-point number that is |
| 361 | * half, single or double-precision. Note that it is not the |
| 362 | * conversion from a floating-point value to an integer value like |
| 363 | * converting 0x00 to 0.00, it is the interpretation of the bits in |
| 364 | * the argument as an IEEE 754 float-point number. |
| 365 | * |
| 366 | * Floating-point numbers must be converted to network byte |
| 367 | * order. That is accomplished here by exactly the same code that |
| 368 | * converts integer arguments to network byte order. |
| 369 | * |
| 370 | * There is preferred encoding for floating-point numbers in CBOR, |
| 371 | * but it is very different than for integers and it is not |
| 372 | * implemented here. Half-precision is preferred to |
| 373 | * single-precision which is preferred to double-precision only if |
| 374 | * the conversion can be performed without loss of precision. Zero |
| 375 | * and infinity can always be converted to half-precision, without |
| 376 | * loss but 3.141592653589 cannot. |
| 377 | * |
| 378 | * The way this function knows to not do preferred encoding on the |
| 379 | * argument passed here when it is a floating point number is the |
| 380 | * uMinLen parameter. It should be 2, 4 or 8 for half, single and |
| 381 | * double precision floating point values. This prevents and the |
| 382 | * incorrect removal of leading zeros when encoding arguments that |
| 383 | * are floating-point numbers. |
| 384 | * |
| 385 | * == Use of Type int and Static Analyzers == |
| 386 | * |
| 387 | * The type int is used here for several variables because of the |
| 388 | * way integer promotion works in C for variables that are uint8_t |
| 389 | * or uint16_t. The basic rule is that they will always be promoted |
| 390 | * to int if they will fit. These integer variables here need only |
| 391 | * hold values less than 255 so they will always fit into an int. |
| 392 | * |
| 393 | * Most of values stored are never negative, so one might think |
| 394 | * that unsigned int would be more correct than int. However the C |
| 395 | * integer promotion rules only promote to unsigned int if the |
| 396 | * result won't fit into an int even if the promotion is for an |
| 397 | * unsigned variable like uint8_t. |
| 398 | * |
| 399 | * By declaring these int, there are few implicit conversions and |
| 400 | * fewer casts needed. Code size is reduced a little. It makes |
| 401 | * static analyzers happier. |
| 402 | * |
| 403 | * Note also that declaring these uint8_t won't stop integer wrap |
| 404 | * around if the code is wrong. It won't make the code more |
| 405 | * correct. |
| 406 | * |
| 407 | * https://stackoverflow.com/questions/46073295/implicit-type-promotion-rules |
| 408 | * https://stackoverflow.com/questions/589575/what-does-the-c-standard-state-the-size-of-int-long-type-to-be |
| 409 | * |
| 410 | * Code Reviewers: THIS FUNCTION DOES POINTER MATH |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 411 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 412 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 413 | /* The buffer must have room for the largest CBOR HEAD + one |
| 414 | * extra. The one extra is needed for this code to work as it does |
| 415 | * a pre-decrement. |
| 416 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 417 | if(buffer.len < QCBOR_HEAD_BUFFER_SIZE) { |
| 418 | return NULLUsefulBufC; |
| 419 | } |
| 420 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 421 | /* Pointer to last valid byte in the buffer */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 422 | uint8_t * const pBufferEnd = &((uint8_t *)buffer.ptr)[QCBOR_HEAD_BUFFER_SIZE-1]; |
| 423 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 424 | /* Point to the last byte and work backwards */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 425 | uint8_t *pByte = pBufferEnd; |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 426 | /* 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] | 427 | int nAdditionalInfo; |
Laurence Lundblade | 2c40ab8 | 2018-12-30 14:20:29 -0800 | [diff] [blame] | 428 | |
Laurence Lundblade | 8c858ab | 2020-11-02 19:53:49 -0800 | [diff] [blame] | 429 | if(uMajorType > QCBOR_INDEFINITE_LEN_TYPE_MODIFIER) { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 430 | /* Special case for start & end of indefinite length */ |
Laurence Lundblade | 8c858ab | 2020-11-02 19:53:49 -0800 | [diff] [blame] | 431 | uMajorType = uMajorType - QCBOR_INDEFINITE_LEN_TYPE_MODIFIER; |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 432 | /* This takes advantage of design of CBOR where additional info |
| 433 | * is 31 for both opening and closing indefinite length |
| 434 | * maps and arrays. |
| 435 | */ |
| 436 | #if CBOR_SIMPLE_BREAK != LEN_IS_INDEFINITE |
| 437 | #error additional info for opening array not the same as for closing |
| 438 | #endif |
Laurence Lundblade | 8c858ab | 2020-11-02 19:53:49 -0800 | [diff] [blame] | 439 | nAdditionalInfo = CBOR_SIMPLE_BREAK; |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 440 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 441 | } else if (uArgument < CBOR_TWENTY_FOUR && uMinLen == 0) { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 442 | /* Simple case where argument is < 24 */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 443 | nAdditionalInfo = (int)uArgument; |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 444 | |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 445 | } else { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 446 | /* This encodes the argument in 1,2,4 or 8 bytes. The outer loop |
| 447 | * runs once for 1 byte and 4 times for 8 bytes. The inner loop |
| 448 | * runs 1, 2 or 4 times depending on outer loop counter. This |
| 449 | * works backwards shifting 8 bits off the argument being |
| 450 | * encoded at a time until all bits from uArgument have been |
| 451 | * encoded and the minimum encoding size is reached. Minimum |
| 452 | * encoding size is for floating-point numbers that have some |
| 453 | * zero-value bytes that must be output. |
Laurence Lundblade | e9b0032 | 2018-12-30 10:33:26 -0800 | [diff] [blame] | 454 | */ |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 455 | static const uint8_t aIterate[] = {1,1,2,4}; |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 456 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 457 | /* uMinLen passed in is unsigned, but goes negative in the loop |
| 458 | * so it must be converted to a signed value. |
| 459 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 460 | int nMinLen = (int)uMinLen; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 461 | int i; |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 462 | for(i = 0; uArgument || nMinLen > 0; i++) { |
| 463 | const int nIterations = (int)aIterate[i]; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 464 | for(int j = 0; j < nIterations; j++) { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 465 | *--pByte = (uint8_t)(uArgument & 0xff); |
| 466 | uArgument = uArgument >> 8; |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 467 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 468 | nMinLen -= nIterations; |
Laurence Lundblade | 04a859b | 2018-12-11 12:13:02 -0800 | [diff] [blame] | 469 | } |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 470 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 471 | nAdditionalInfo = LEN_IS_ONE_BYTE-1 + i; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 472 | } |
Laurence Lundblade | f970f1d | 2018-12-14 01:44:23 -0800 | [diff] [blame] | 473 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 474 | /* This expression integer-promotes to type int. The code above in |
| 475 | * function guarantees that nAdditionalInfo will never be larger |
| 476 | * than 0x1f. The caller may pass in a too-large uMajor type. The |
| 477 | * conversion to unint8_t will cause an integer wrap around and |
| 478 | * incorrect CBOR will be generated, but no security issue will |
| 479 | * occur. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 480 | */ |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 481 | const int nInitialByte = (uMajorType << 5) + nAdditionalInfo; |
| 482 | *--pByte = (uint8_t)nInitialByte; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 483 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 484 | #ifdef EXTRA_ENCODE_HEAD_CHECK |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 485 | /* This is a sanity check that can be turned on to verify the |
| 486 | * pointer math in this function is not going wrong. Turn it on and |
| 487 | * run the whole test suite to perform the check. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 488 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 489 | if(pBufferEnd - pByte > 9 || pBufferEnd - pByte < 1 || pByte < (uint8_t *)buffer.ptr) { |
| 490 | return NULLUsefulBufC; |
| 491 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 492 | #endif /* EXTRA_ENCODE_HEAD_CHECK */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 493 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 494 | /* Length will not go negative because the loops run for at most 8 decrements |
| 495 | * of pByte, only one other decrement is made, and the array is sized |
| 496 | * for this. |
| 497 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 498 | return (UsefulBufC){pByte, (size_t)(pBufferEnd - pByte)}; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 502 | /** |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 503 | * @brief Append the CBOR head, the major type and argument |
| 504 | * |
| 505 | * @param me Encoder context. |
| 506 | * @param uMajorType Major type to insert. |
| 507 | * @param uArgument The argument (an integer value or a length). |
| 508 | * @param uMinLen The minimum number of bytes for encoding the CBOR argument. |
| 509 | * |
| 510 | * This formats the CBOR "head" and appends it to the output. |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 511 | */ |
| 512 | 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] | 513 | { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 514 | /* A stack buffer large enough for a CBOR head */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 515 | UsefulBuf_MAKE_STACK_UB (pBufferForEncodedHead, QCBOR_HEAD_BUFFER_SIZE); |
| 516 | |
| 517 | UsefulBufC EncodedHead = QCBOREncode_EncodeHead(pBufferForEncodedHead, |
| 518 | uMajorType, |
| 519 | uMinLen, |
| 520 | uArgument); |
| 521 | |
| 522 | /* No check for EncodedHead == NULLUsefulBufC is performed here to |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 523 | * save object code. It is very clear that pBufferForEncodedHead is |
| 524 | * the correct size. If EncodedHead == NULLUsefulBufC then |
| 525 | * UsefulOutBuf_AppendUsefulBuf() will do nothing so there is no |
| 526 | * security hole introduced. |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 527 | */ |
| 528 | |
| 529 | UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), EncodedHead); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 533 | /** |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 534 | * @brief Check for errors when decreasing nesting. |
| 535 | * |
| 536 | * @param pMe QCBOR encoding context. |
| 537 | * @param uMajorType The major type of the nesting. |
| 538 | * |
| 539 | * Check that there is no previous error, that there is actually some |
| 540 | * nesting and that the major type of the opening of the nesting |
| 541 | * matches the major type of the nesting being closed. |
| 542 | * |
| 543 | * This is called when closing maps, arrays, byte string wrapping and |
| 544 | * open/close of byte strings. |
| 545 | */ |
| 546 | bool |
| 547 | CheckDecreaseNesting(QCBOREncodeContext *pMe, uint8_t uMajorType) |
| 548 | { |
| 549 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 550 | if(pMe->uError != QCBOR_SUCCESS) { |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | if(!Nesting_IsInNest(&(pMe->nesting))) { |
| 555 | pMe->uError = QCBOR_ERR_TOO_MANY_CLOSES; |
| 556 | return true; |
| 557 | } |
| 558 | |
| 559 | if(Nesting_GetMajorType(&(pMe->nesting)) != uMajorType) { |
| 560 | pMe->uError = QCBOR_ERR_CLOSE_MISMATCH; |
| 561 | return true; |
| 562 | } |
| 563 | |
| 564 | #else |
| 565 | /* None of these checks are performed if the encode guards are |
| 566 | * turned off as they all relate to correct calling. |
| 567 | * |
| 568 | * Turning off all these checks does not turn off any checking for |
| 569 | * buffer overflows or pointer issues. |
| 570 | */ |
| 571 | |
| 572 | (void)uMajorType; |
| 573 | (void)pMe; |
| 574 | #endif |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 575 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 576 | return false; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | /** |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 581 | * @brief Insert the CBOR head for a map, array or wrapped bstr |
| 582 | * |
| 583 | * @param me QCBOR encoding context. |
| 584 | * @param uMajorType One of CBOR_MAJOR_TYPE_XXXX. |
| 585 | * @param uLen The length of the data item. |
| 586 | * |
| 587 | * When an array, map or bstr was opened, nothing was done but note |
| 588 | * the position. This function goes back to that position and inserts |
| 589 | * the CBOR Head with the major type and length. |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 590 | */ |
| 591 | static void InsertCBORHead(QCBOREncodeContext *me, uint8_t uMajorType, size_t uLen) |
| 592 | { |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 593 | if(CheckDecreaseNesting(me, uMajorType)) { |
| 594 | return; |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 595 | } |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 596 | |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 597 | if(uMajorType == CBOR_MAJOR_NONE_TYPE_OPEN_BSTR) { |
| 598 | uMajorType = CBOR_MAJOR_TYPE_BYTE_STRING; |
| 599 | } |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 600 | |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 601 | /* A stack buffer large enough for a CBOR head (9 bytes) */ |
Laurence Lundblade | d8e1c51 | 2020-11-04 23:03:44 -0800 | [diff] [blame] | 602 | UsefulBuf_MAKE_STACK_UB(pBufferForEncodedHead, QCBOR_HEAD_BUFFER_SIZE); |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 603 | |
| 604 | UsefulBufC EncodedHead = QCBOREncode_EncodeHead(pBufferForEncodedHead, |
| 605 | uMajorType, |
| 606 | 0, |
| 607 | uLen); |
| 608 | |
| 609 | /* No check for EncodedHead == NULLUsefulBufC is performed here to |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 610 | * save object code. It is very clear that pBufferForEncodedHead is |
| 611 | * the correct size. If EncodedHead == NULLUsefulBufC then |
| 612 | * UsefulOutBuf_InsertUsefulBuf() will do nothing so there is no |
Laurence Lundblade | 9e2f708 | 2021-05-17 02:10:48 -0700 | [diff] [blame] | 613 | * security hole introduced. |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 614 | */ |
| 615 | UsefulOutBuf_InsertUsefulBuf(&(me->OutBuf), |
| 616 | EncodedHead, |
| 617 | Nesting_GetStartPos(&(me->nesting))); |
| 618 | |
| 619 | Nesting_Decrease(&(me->nesting)); |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 620 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 621 | |
Laurence Lundblade | 241705e | 2018-12-30 18:56:14 -0800 | [diff] [blame] | 622 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 623 | /** |
| 624 | * @brief Increment item counter for maps and arrays. |
| 625 | * |
| 626 | * @param pMe QCBOR encoding context. |
| 627 | * |
| 628 | * This is mostly a separate function to make code more readable and |
| 629 | * to have fewer occurrences of #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | 3e0a45c | 2020-11-05 11:12:04 -0800 | [diff] [blame] | 630 | */ |
| 631 | static inline void IncrementMapOrArrayCount(QCBOREncodeContext *pMe) |
| 632 | { |
| 633 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 634 | if(pMe->uError == QCBOR_SUCCESS) { |
| 635 | pMe->uError = Nesting_Increment(&(pMe->nesting)); |
| 636 | } |
| 637 | #else |
| 638 | (void)Nesting_Increment(&(pMe->nesting)); |
| 639 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 640 | } |
| 641 | |
| 642 | |
| 643 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 644 | * Public functions for adding unsigned integers. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 645 | */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 646 | void QCBOREncode_AddUInt64(QCBOREncodeContext *me, uint64_t uValue) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 647 | { |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 648 | AppendCBORHead(me, CBOR_MAJOR_TYPE_POSITIVE_INT, uValue, 0); |
| 649 | |
Laurence Lundblade | 3e0a45c | 2020-11-05 11:12:04 -0800 | [diff] [blame] | 650 | IncrementMapOrArrayCount(me); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame] | 653 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 654 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 655 | * Public functions for adding signed integers. See qcbor/qcbor_encode.h |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 656 | */ |
| 657 | void QCBOREncode_AddInt64(QCBOREncodeContext *me, int64_t nNum) |
| 658 | { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 659 | uint8_t uMajorType; |
| 660 | uint64_t uValue; |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 661 | |
| 662 | if(nNum < 0) { |
Laurence Lundblade | 9c5c0ef | 2022-12-23 17:56:27 -0700 | [diff] [blame] | 663 | /* In CBOR -1 encodes as 0x00 with major type negative int. |
| 664 | * First add one as a signed integer because that will not |
| 665 | * overflow. Then change the sign as needed for encoding. (The |
| 666 | * opposite order, changing the sign and subtracting, can cause |
| 667 | * an overflow when encoding INT64_MIN. */ |
| 668 | int64_t nTmp = nNum + 1; |
| 669 | uValue = (uint64_t)-nTmp; |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 670 | uMajorType = CBOR_MAJOR_TYPE_NEGATIVE_INT; |
| 671 | } else { |
| 672 | uValue = (uint64_t)nNum; |
| 673 | uMajorType = CBOR_MAJOR_TYPE_POSITIVE_INT; |
| 674 | } |
| 675 | AppendCBORHead(me, uMajorType, uValue, 0); |
| 676 | |
Laurence Lundblade | 3e0a45c | 2020-11-05 11:12:04 -0800 | [diff] [blame] | 677 | IncrementMapOrArrayCount(me); |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | |
| 681 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 682 | * Semi-private function. It is exposed to user of the interface, but |
| 683 | * one of its inline wrappers will usually be called instead of this. |
| 684 | * |
| 685 | * See qcbor/qcbor_encode.h |
| 686 | * |
| 687 | * This does the work of adding actual strings bytes to the CBOR |
| 688 | * output (as opposed to adding numbers and opening / closing |
| 689 | * aggregate types). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 690 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 691 | * There are four use cases: |
| 692 | * CBOR_MAJOR_TYPE_BYTE_STRING -- Byte strings |
| 693 | * CBOR_MAJOR_TYPE_TEXT_STRING -- Text strings |
| 694 | * CBOR_MAJOR_NONE_TYPE_RAW -- Already-encoded CBOR |
| 695 | * CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY -- Special case |
| 696 | * |
| 697 | * The first two add the head plus the actual bytes. The third just |
| 698 | * adds the bytes as the heas is presumed to be in the bytes. The |
| 699 | * fourth just adds the head for the very special case of |
| 700 | * QCBOREncode_AddBytesLenOnly(). |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 701 | */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 702 | void QCBOREncode_AddBuffer(QCBOREncodeContext *me, uint8_t uMajorType, UsefulBufC Bytes) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 703 | { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 704 | /* If it is not Raw CBOR, add the type and the length */ |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 705 | if(uMajorType != CBOR_MAJOR_NONE_TYPE_RAW) { |
| 706 | uint8_t uRealMajorType = uMajorType; |
| 707 | if(uRealMajorType == CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) { |
| 708 | uRealMajorType = CBOR_MAJOR_TYPE_BYTE_STRING; |
| 709 | } |
| 710 | AppendCBORHead(me, uRealMajorType, Bytes.len, 0); |
| 711 | } |
| 712 | |
| 713 | if(uMajorType != CBOR_MAJOR_NONE_TYPE_BSTR_LEN_ONLY) { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 714 | /* Actually add the bytes */ |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 715 | UsefulOutBuf_AppendUsefulBuf(&(me->OutBuf), Bytes); |
| 716 | } |
| 717 | |
Laurence Lundblade | 3e0a45c | 2020-11-05 11:12:04 -0800 | [diff] [blame] | 718 | IncrementMapOrArrayCount(me); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Laurence Lundblade | cafcfe1 | 2018-10-31 21:59:50 +0700 | [diff] [blame] | 721 | |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 722 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 723 | * Public functions for adding a tag. See qcbor/qcbor_encode.h |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 724 | */ |
| 725 | void QCBOREncode_AddTag(QCBOREncodeContext *me, uint64_t uTag) |
| 726 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 727 | AppendCBORHead(me, CBOR_MAJOR_TYPE_TAG, uTag, 0); |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame] | 731 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 732 | * Semi-private function. It is exposed to user of the interface, but |
| 733 | * one of its inline wrappers will usually be called instead of this. |
| 734 | * |
| 735 | * See header qcbor/qcbor_encode.h |
Laurence Lundblade | 56230d1 | 2018-11-01 11:14:51 +0700 | [diff] [blame] | 736 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 737 | void QCBOREncode_AddType7(QCBOREncodeContext *me, uint8_t uMinLen, uint64_t uNum) |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 738 | { |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 739 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 740 | if(me->uError == QCBOR_SUCCESS) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 741 | if(uNum >= CBOR_SIMPLEV_RESERVED_START && uNum <= CBOR_SIMPLEV_RESERVED_END) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 742 | me->uError = QCBOR_ERR_ENCODE_UNSUPPORTED; |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 743 | return; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 744 | } |
Laurence Lundblade | 487930f | 2018-11-30 11:01:45 -0800 | [diff] [blame] | 745 | } |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 746 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 747 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 748 | /* AppendCBORHead() does endian swapping for the float / double */ |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 749 | AppendCBORHead(me, CBOR_MAJOR_TYPE_SIMPLE, uNum, uMinLen); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 750 | |
Laurence Lundblade | 3e0a45c | 2020-11-05 11:12:04 -0800 | [diff] [blame] | 751 | IncrementMapOrArrayCount(me); |
Laurence Lundblade | 55a2483 | 2018-10-30 04:35:08 +0700 | [diff] [blame] | 752 | } |
| 753 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 754 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 755 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 756 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 757 | * Public functions for adding a double. See qcbor/qcbor_encode.h |
| 758 | */ |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 759 | void QCBOREncode_AddDoubleNoPreferred(QCBOREncodeContext *pMe, double dNum) |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 760 | { |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 761 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 762 | if(pMe->uMode >= QCBOR_ENCODE_MODE_PREFERRED) { |
| 763 | pMe->uError = QCBOR_ERR_NOT_PREFERRED; |
| 764 | return; |
| 765 | } |
| 766 | #endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 767 | |
| 768 | QCBOREncode_AddType7(pMe, |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 769 | sizeof(uint64_t), |
| 770 | UsefulBufUtil_CopyDoubleToUint64(dNum)); |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 773 | #include <math.h> // For NaN. Maybe a better way? TODO: |
Laurence Lundblade | 32f3e62 | 2020-07-13 20:35:11 -0700 | [diff] [blame] | 774 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 775 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 776 | * Public functions for adding a double. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 777 | */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 778 | void QCBOREncode_AddDouble(QCBOREncodeContext *me, double dNum) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 779 | { |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 780 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 781 | IEEE754_union FloatResult; |
| 782 | bool bNoNaNPayload; |
| 783 | struct IEEE754_ToInt IntResult; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 784 | |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 785 | if(me->uMode == QCBOR_ENCODE_MODE_DCBOR) { |
| 786 | IntResult = IEEE754_DoubleToInt(dNum); |
| 787 | switch(IntResult.type) { |
| 788 | case IEEE754_ToInt_IS_INT: |
| 789 | QCBOREncode_AddInt64(me, IntResult.integer.is_signed); |
| 790 | return; |
| 791 | case IEEE754_ToInt_IS_UINT: |
| 792 | QCBOREncode_AddUInt64(me, IntResult.integer.un_signed); |
| 793 | return; |
| 794 | case IEEE754_To_int_NaN: |
Laurence Lundblade | e026f4f | 2024-01-18 13:48:34 -0700 | [diff] [blame^] | 795 | dNum = NAN; |
| 796 | bNoNaNPayload = true; |
| 797 | break; |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 798 | case IEEE754_ToInt_NO_CONVERSION: |
| 799 | bNoNaNPayload = true; |
| 800 | } |
| 801 | } else { |
| 802 | bNoNaNPayload = false; |
| 803 | } |
| 804 | |
| 805 | FloatResult = IEEE754_DoubleToSmaller(dNum, true, bNoNaNPayload); |
| 806 | |
| 807 | QCBOREncode_AddType7(me, (uint8_t)FloatResult.uSize, FloatResult.uValue); |
| 808 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 809 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 810 | QCBOREncode_AddDoubleNoPreferred(me, dNum); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 811 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 812 | } |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 813 | |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 814 | |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 815 | |
| 816 | |
| 817 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 818 | * Public functions for adding a float. See qcbor/qcbor_encode.h |
| 819 | */ |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 820 | void QCBOREncode_AddFloatNoPreferred(QCBOREncodeContext *pMe, float fNum) |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 821 | { |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 822 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 823 | if(pMe->uMode >= QCBOR_ENCODE_MODE_PREFERRED) { |
| 824 | pMe->uError = QCBOR_ERR_NOT_PREFERRED; |
| 825 | return; |
| 826 | } |
| 827 | #endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 828 | QCBOREncode_AddType7(pMe, |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 829 | sizeof(uint32_t), |
| 830 | UsefulBufUtil_CopyFloatToUint32(fNum)); |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | |
| 834 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 835 | * Public functions for adding a float. See qcbor/qcbor_encode.h |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 836 | */ |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 837 | void QCBOREncode_AddFloat(QCBOREncodeContext *pMe, float fNum) |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 838 | { |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 839 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 840 | IEEE754_union FloatResult; |
| 841 | bool bNoNaNPayload; |
| 842 | struct IEEE754_ToInt IntResult; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 843 | |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 844 | if(pMe->uMode == QCBOR_ENCODE_MODE_DCBOR) { |
| 845 | IntResult = IEEE754_SingleToInt(fNum); |
| 846 | switch(IntResult.type) { |
| 847 | case IEEE754_ToInt_IS_INT: |
| 848 | QCBOREncode_AddInt64(pMe, IntResult.integer.is_signed); |
| 849 | return; |
Laurence Lundblade | e026f4f | 2024-01-18 13:48:34 -0700 | [diff] [blame^] | 850 | case IEEE754_ToInt_IS_UINT: |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 851 | QCBOREncode_AddUInt64(pMe, IntResult.integer.un_signed); |
| 852 | return; |
| 853 | case IEEE754_To_int_NaN: |
Laurence Lundblade | e026f4f | 2024-01-18 13:48:34 -0700 | [diff] [blame^] | 854 | fNum = NAN; |
| 855 | bNoNaNPayload = true; |
| 856 | break; |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 857 | case IEEE754_ToInt_NO_CONVERSION: |
| 858 | bNoNaNPayload = true; |
| 859 | } |
| 860 | } else { |
| 861 | bNoNaNPayload = false; |
| 862 | } |
| 863 | |
| 864 | FloatResult = IEEE754_SingleToHalf(fNum, bNoNaNPayload); |
| 865 | |
| 866 | QCBOREncode_AddType7(pMe, (uint8_t)FloatResult.uSize, FloatResult.uValue); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 867 | #else /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 868 | QCBOREncode_AddFloatNoPreferred(me, fNum); |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 869 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 870 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 871 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 872 | |
| 873 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 874 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 875 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 876 | * Semi-public function. It is exposed to the user of the interface, |
| 877 | * but one of the inline wrappers will usually be called rather than |
| 878 | * this. |
| 879 | * |
| 880 | * See qcbor/qcbor_encode.h |
| 881 | * |
| 882 | * Improvement: create another version of this that only takes a big |
| 883 | * number mantissa and converts the output to a type 0 or 1 integer |
| 884 | * when mantissa is small enough. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 885 | */ |
| 886 | void QCBOREncode_AddExponentAndMantissa(QCBOREncodeContext *pMe, |
| 887 | uint64_t uTag, |
| 888 | UsefulBufC BigNumMantissa, |
| 889 | bool bBigNumIsNegative, |
| 890 | int64_t nMantissa, |
| 891 | int64_t nExponent) |
| 892 | { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 893 | /* This is for encoding either a big float or a decimal fraction, |
| 894 | * both of which are an array of two items, an exponent and a |
| 895 | * mantissa. The difference between the two is that the exponent |
| 896 | * is base-2 for big floats and base-10 for decimal fractions, but |
| 897 | * that has no effect on the code here. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 898 | */ |
Laurence Lundblade | ae66d3f | 2020-09-14 18:12:08 -0700 | [diff] [blame] | 899 | if(uTag != CBOR_TAG_INVALID64) { |
| 900 | QCBOREncode_AddTag(pMe, uTag); |
| 901 | } |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 902 | QCBOREncode_OpenArray(pMe); |
| 903 | QCBOREncode_AddInt64(pMe, nExponent); |
| 904 | if(!UsefulBuf_IsNULLC(BigNumMantissa)) { |
| 905 | if(bBigNumIsNegative) { |
| 906 | QCBOREncode_AddNegativeBignum(pMe, BigNumMantissa); |
| 907 | } else { |
| 908 | QCBOREncode_AddPositiveBignum(pMe, BigNumMantissa); |
| 909 | } |
| 910 | } else { |
| 911 | QCBOREncode_AddInt64(pMe, nMantissa); |
| 912 | } |
| 913 | QCBOREncode_CloseArray(pMe); |
| 914 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 915 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 916 | |
| 917 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 918 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 919 | * Semi-public function. It is exposed to the user of the interface, |
| 920 | * but one of the inline wrappers will usually be called rather than |
| 921 | * this. |
| 922 | * |
| 923 | * See qcbor/qcbor_encode.h |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 924 | */ |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 925 | void QCBOREncode_OpenMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType) |
| 926 | { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 927 | /* Add one item to the nesting level we are in for the new map or array */ |
Laurence Lundblade | 3e0a45c | 2020-11-05 11:12:04 -0800 | [diff] [blame] | 928 | IncrementMapOrArrayCount(me); |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 929 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 930 | /* The offset where the length of an array or map will get written |
| 931 | * is stored in a uint32_t, not a size_t to keep stack usage |
| 932 | * smaller. This checks to be sure there is no wrap around when |
| 933 | * recording the offset. Note that on 64-bit machines CBOR larger |
| 934 | * than 4GB can be encoded as long as no array/map offsets occur |
| 935 | * past the 4GB mark, but the public interface says that the |
| 936 | * maximum is 4GB to keep the discussion simpler. |
| 937 | */ |
Laurence Lundblade | 3e0a45c | 2020-11-05 11:12:04 -0800 | [diff] [blame] | 938 | size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf)); |
Laurence Lundblade | d39cd39 | 2019-01-11 18:17:38 -0800 | [diff] [blame] | 939 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 940 | /* QCBOR_MAX_ARRAY_OFFSET is slightly less than UINT32_MAX so this |
| 941 | * code can run on a 32-bit machine and tests can pass on a 32-bit |
| 942 | * machine. If it was exactly UINT32_MAX, then this code would not |
| 943 | * compile or run on a 32-bit machine and an #ifdef or some machine |
| 944 | * size detection would be needed reducing portability. |
| 945 | */ |
Laurence Lundblade | 3e0a45c | 2020-11-05 11:12:04 -0800 | [diff] [blame] | 946 | if(uEndPosition >= QCBOR_MAX_ARRAY_OFFSET) { |
| 947 | me->uError = QCBOR_ERR_BUFFER_TOO_LARGE; |
| 948 | |
| 949 | } else { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 950 | /* Increase nesting level because this is a map or array. Cast |
| 951 | * from size_t to uin32_t is safe because of check above. |
| 952 | */ |
Laurence Lundblade | 3e0a45c | 2020-11-05 11:12:04 -0800 | [diff] [blame] | 953 | me->uError = Nesting_Increase(&(me->nesting), uMajorType, (uint32_t)uEndPosition); |
Laurence Lundblade | 1ef8b2d | 2018-12-14 23:13:34 -0800 | [diff] [blame] | 954 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 957 | |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 958 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 959 | * Semi-public function. It is exposed to the user of the interface, |
| 960 | * but one of the inline wrappers will usually be called rather than |
| 961 | * this. |
| 962 | * |
| 963 | * See qcbor/qcbor_encode.h |
| 964 | */ |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 965 | void QCBOREncode_OpenMapOrArrayIndefiniteLength(QCBOREncodeContext *pMe, uint8_t uMajorType) |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 966 | { |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 967 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
| 968 | if(pMe->uMode >= QCBOR_ENCODE_MODE_PREFERRED) { |
| 969 | pMe->uError = QCBOR_ERR_NOT_PREFERRED; |
| 970 | return; |
| 971 | } |
| 972 | #endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 973 | /* Insert the indefinite length marker (0x9f for arrays, 0xbf for maps) */ |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 974 | AppendCBORHead(pMe, uMajorType, 0, 0); |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 975 | |
| 976 | /* Call the definite-length opener just to do the bookkeeping for |
| 977 | * nesting. It will record the position of the opening item in the |
| 978 | * encoded output but this is not used when closing this open. |
| 979 | */ |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 980 | QCBOREncode_OpenMapOrArray(pMe, uMajorType); |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 981 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 982 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 983 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 984 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 985 | * Public functions for closing arrays and maps. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 986 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 987 | void QCBOREncode_CloseMapOrArray(QCBOREncodeContext *me, uint8_t uMajorType) |
Laurence Lundblade | a954db9 | 2018-09-28 19:27:31 -0700 | [diff] [blame] | 988 | { |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 989 | InsertCBORHead(me, uMajorType, Nesting_GetCount(&(me->nesting))); |
| 990 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 991 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 992 | |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 993 | void |
| 994 | QCBOREncode_CloseArray(QCBOREncodeContext *pMe) |
| 995 | { |
| 996 | InsertCBORHead(pMe, CBOR_MAJOR_TYPE_ARRAY, Nesting_GetCount(&(pMe->nesting))); |
| 997 | } |
| 998 | |
| 999 | void |
| 1000 | QCBOREncode_CloseMapUnsorted(QCBOREncodeContext *pMe) |
| 1001 | { |
| 1002 | InsertCBORHead(pMe, CBOR_MAJOR_TYPE_MAP, Nesting_GetCount(&(pMe->nesting))); |
| 1003 | } |
| 1004 | |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 1005 | |
| 1006 | /** |
| 1007 | * @brief Decode a CBOR item head. |
| 1008 | * |
| 1009 | * @param[in] pUInBuf UsefulInputBuf to read from. |
| 1010 | * @param[out] pnMajorType Major type of decoded head. |
| 1011 | * @param[out] puArgument Argument of decoded head. |
| 1012 | * @param[out] pnAdditionalInfo Additional info from decoded head. |
| 1013 | * |
| 1014 | * @return SUCCESS if a head was decoded |
| 1015 | * HIT_END if there were not enough bytes to decode a head |
| 1016 | * UNSUPPORTED if the decoded item is not one that is supported |
| 1017 | * |
| 1018 | * This is copied from qcbor_decode.c rather than referenced. This |
| 1019 | * makes the core decoder 60 bytes smaller because it gets inlined. |
| 1020 | * It would not get inlined if it was referenced. It is important to |
| 1021 | * make the core decoder as small as possible. The copy here does make |
| 1022 | * map sorting 200 bytes bigger, but map sorting is rarely used in |
| 1023 | * environments that need small object code. It would also make |
| 1024 | * qcbor_encode.c depend on qcbor_decode.c |
| 1025 | * |
| 1026 | * This is also super stable and tested. It implements the very |
| 1027 | * well-defined part of CBOR that will never change. So this won't |
| 1028 | * change. |
| 1029 | */ |
| 1030 | static QCBORError |
| 1031 | QCBOREncodePriv_DecodeHead(UsefulInputBuf *pUInBuf, |
| 1032 | int *pnMajorType, |
| 1033 | uint64_t *puArgument, |
| 1034 | int *pnAdditionalInfo) |
| 1035 | { |
| 1036 | QCBORError uReturn; |
| 1037 | |
| 1038 | /* Get the initial byte that every CBOR data item has and break it |
| 1039 | * down. */ |
| 1040 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
| 1041 | const int nTmpMajorType = nInitialByte >> 5; |
| 1042 | const int nAdditionalInfo = nInitialByte & 0x1f; |
| 1043 | |
| 1044 | /* Where the argument accumulates */ |
| 1045 | uint64_t uArgument; |
| 1046 | |
| 1047 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
| 1048 | /* Need to get 1,2,4 or 8 additional argument bytes. Map |
| 1049 | * LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length. |
| 1050 | */ |
| 1051 | static const uint8_t aIterate[] = {1,2,4,8}; |
| 1052 | |
| 1053 | /* Loop getting all the bytes in the argument */ |
| 1054 | uArgument = 0; |
| 1055 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
| 1056 | /* This shift and add gives the endian conversion. */ |
| 1057 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 1058 | } |
| 1059 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
| 1060 | /* The reserved and thus-far unused additional info values */ |
| 1061 | uReturn = QCBOR_ERR_UNSUPPORTED; |
| 1062 | goto Done; |
| 1063 | } else { |
| 1064 | /* Less than 24, additional info is argument or 31, an |
| 1065 | * indefinite-length. No more bytes to get. |
| 1066 | */ |
| 1067 | uArgument = (uint64_t)nAdditionalInfo; |
| 1068 | } |
| 1069 | |
| 1070 | if(UsefulInputBuf_GetError(pUInBuf)) { |
| 1071 | uReturn = QCBOR_ERR_HIT_END; |
| 1072 | goto Done; |
| 1073 | } |
| 1074 | |
| 1075 | /* All successful if arrived here. */ |
| 1076 | uReturn = QCBOR_SUCCESS; |
| 1077 | *pnMajorType = nTmpMajorType; |
| 1078 | *puArgument = uArgument; |
| 1079 | *pnAdditionalInfo = nAdditionalInfo; |
| 1080 | |
| 1081 | Done: |
| 1082 | return uReturn; |
| 1083 | } |
| 1084 | |
| 1085 | |
| 1086 | /** |
| 1087 | * @brief Consume the next item from a UsefulInputBuf. |
| 1088 | * |
| 1089 | * @param[in] pInBuf UsefulInputBuf from which to consume item. |
| 1090 | * |
| 1091 | * Recursive, but stack usage is light and encoding depth limit |
| 1092 | */ |
| 1093 | static QCBORError |
| 1094 | QCBOREncodePriv_ConsumeNext(UsefulInputBuf *pInBuf) |
| 1095 | { |
| 1096 | int nMajor; |
| 1097 | uint64_t uArgument; |
| 1098 | int nAdditional; |
| 1099 | uint16_t uItemCount; |
| 1100 | uint16_t uMul; |
| 1101 | uint16_t i; |
| 1102 | QCBORError uCBORError; |
| 1103 | |
| 1104 | uCBORError = QCBOREncodePriv_DecodeHead(pInBuf, &nMajor, &uArgument, &nAdditional); |
| 1105 | if(uCBORError != QCBOR_SUCCESS) { |
| 1106 | return uCBORError; |
| 1107 | } |
| 1108 | |
| 1109 | uMul = 1; |
| 1110 | |
| 1111 | switch(nMajor) { |
| 1112 | case CBOR_MAJOR_TYPE_POSITIVE_INT: /* Major type 0 */ |
| 1113 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: /* Major type 1 */ |
| 1114 | break; |
| 1115 | |
| 1116 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 1117 | return uArgument == CBOR_SIMPLE_BREAK ? 1 : 0; |
| 1118 | break; |
| 1119 | |
| 1120 | case CBOR_MAJOR_TYPE_BYTE_STRING: |
| 1121 | case CBOR_MAJOR_TYPE_TEXT_STRING: |
| 1122 | if(nAdditional == LEN_IS_INDEFINITE) { |
| 1123 | /* Segments of indefinite length */ |
| 1124 | while(QCBOREncodePriv_ConsumeNext(pInBuf) == 0); |
| 1125 | } |
| 1126 | (void)UsefulInputBuf_GetBytes(pInBuf, uArgument); |
| 1127 | break; |
| 1128 | |
| 1129 | case CBOR_MAJOR_TYPE_TAG: |
| 1130 | QCBOREncodePriv_ConsumeNext(pInBuf); |
| 1131 | break; |
| 1132 | |
| 1133 | case CBOR_MAJOR_TYPE_MAP: |
| 1134 | uMul = 2; |
| 1135 | /* Fallthrough */ |
| 1136 | case CBOR_MAJOR_TYPE_ARRAY: |
| 1137 | uItemCount = (uint16_t)uArgument * uMul; |
| 1138 | if(nAdditional == LEN_IS_INDEFINITE) { |
| 1139 | uItemCount = UINT16_MAX; |
| 1140 | } |
| 1141 | for(i = uItemCount; i > 0; i--) { |
| 1142 | if(QCBOREncodePriv_ConsumeNext(pInBuf)) { |
| 1143 | /* End of indefinite length */ |
| 1144 | break; |
| 1145 | } |
| 1146 | } |
| 1147 | break; |
| 1148 | } |
| 1149 | |
| 1150 | return QCBOR_SUCCESS; |
| 1151 | } |
| 1152 | |
| 1153 | |
| 1154 | /** |
| 1155 | * @brief Decoded next item to get its length. |
| 1156 | * |
| 1157 | * Decode the next item in map no matter what type it is. It works |
| 1158 | * recursively when an item is a map or array It returns offset just |
| 1159 | * past the item decoded or zero there are no more items in the output |
| 1160 | * buffer. |
| 1161 | * |
| 1162 | * This doesn't distinguish between end of the input and an error |
| 1163 | * because it is used to decode stuff we encoded into a buffer, not |
| 1164 | * stuff that came in from outside. We still want a check for safety |
| 1165 | * in case of bugs here, but it is OK to report end of input on error. |
| 1166 | */ |
| 1167 | static uint32_t |
| 1168 | QCBOREncodePriv_DecodeNextInMap(QCBOREncodeContext *pMe, uint32_t uStart) |
| 1169 | { |
| 1170 | UsefulInputBuf InBuf; |
| 1171 | UsefulBufC EncodedMapBytes; |
| 1172 | QCBORError uCBORError; |
| 1173 | |
| 1174 | EncodedMapBytes = UsefulOutBuf_OutUBufOffset(&(pMe->OutBuf), uStart); |
| 1175 | if(UsefulBuf_IsNULLC(EncodedMapBytes)) { |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
| 1179 | UsefulInputBuf_Init(&InBuf, EncodedMapBytes); |
| 1180 | |
| 1181 | /* This is always used on maps, so consume two, the label and the value */ |
| 1182 | uCBORError = QCBOREncodePriv_ConsumeNext(&InBuf); |
| 1183 | if(uCBORError) { |
| 1184 | return 0; |
| 1185 | } |
| 1186 | uCBORError = QCBOREncodePriv_ConsumeNext(&InBuf); |
| 1187 | if(uCBORError) { |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | /* Cast is safe because this is QCBOR which limits sizes to UINT32_MAX */ |
| 1192 | return (uint32_t)UsefulInputBuf_Tell(&InBuf); |
| 1193 | } |
| 1194 | |
| 1195 | |
| 1196 | /** |
| 1197 | * @brief Sort items lexographically by encoded labels. |
| 1198 | * |
| 1199 | * @param[in] pMe Encoding context. |
| 1200 | * @param[in] uStart Offset in outbuf of first item for sorting. |
| 1201 | * |
| 1202 | * This reaches into the UsefulOutBuf in the encoding context and |
| 1203 | * sorts encoded CBOR items. The byte offset start of the items is at |
| 1204 | * @c uStart and it goes to the end of valid bytes in the |
| 1205 | * UsefulOutBuf. |
| 1206 | */ |
| 1207 | static void |
| 1208 | QCBOREncodePriv_SortMap(QCBOREncodeContext *pMe, uint32_t uStart) |
| 1209 | { |
| 1210 | bool bSwapped; |
| 1211 | int nComparison; |
| 1212 | uint32_t uLen2; |
| 1213 | uint32_t uLen1; |
| 1214 | uint32_t uStart1; |
| 1215 | uint32_t uStart2; |
| 1216 | |
| 1217 | if(pMe->uError != QCBOR_SUCCESS) { |
| 1218 | return; |
| 1219 | } |
| 1220 | |
| 1221 | /* Bubble sort because the sizes of all the items are not the |
| 1222 | * same. It works with adjacent pairs so the swap is not too |
| 1223 | * difficult even though sizes are different. |
| 1224 | * |
| 1225 | * While bubble sort is n-squared, it seems OK here because n will |
| 1226 | * usually be small and the comparison and swap functions aren't |
| 1227 | * too CPU intensive. |
| 1228 | * |
| 1229 | * Another approach would be to have an array of offsets to the |
| 1230 | * items. However this requires memory allocation and the swap |
| 1231 | * operation for quick sort or such is complicated because the item |
| 1232 | * sizes are not the same and overlap may occur in the bytes being |
| 1233 | * swapped. |
| 1234 | */ |
| 1235 | do { |
| 1236 | uLen1 = QCBOREncodePriv_DecodeNextInMap(pMe, uStart); |
| 1237 | if(uLen1 == 0) { |
| 1238 | /* It's an empty map. Nothing to do. */ |
| 1239 | break; |
| 1240 | } |
| 1241 | uStart1 = uStart; |
| 1242 | uStart2 = uStart1 + uLen1; |
| 1243 | bSwapped = false; |
| 1244 | |
| 1245 | while(1) { |
| 1246 | uLen2 = QCBOREncodePriv_DecodeNextInMap(pMe, uStart2); |
| 1247 | if(uLen2 == 0) { |
| 1248 | break; |
| 1249 | } |
| 1250 | |
| 1251 | nComparison = UsefulOutBuf_Compare(&(pMe->OutBuf), uStart1, uStart2); |
| 1252 | if(nComparison < 0) { |
| 1253 | UsefulOutBuf_Swap(&(pMe->OutBuf), uStart1, uStart2, uStart2 + uLen2); |
| 1254 | uStart1 = uStart1 + uLen2; |
| 1255 | bSwapped = true; |
| 1256 | } else { |
| 1257 | uStart1 = uStart2; |
| 1258 | } |
| 1259 | uStart2 = uStart2 + uLen2; |
| 1260 | } |
| 1261 | } while(bSwapped); |
| 1262 | } |
| 1263 | |
| 1264 | |
| 1265 | /* |
| 1266 | * Public functions for closing sorted maps. See qcbor/qcbor_encode.h |
| 1267 | */ |
| 1268 | void QCBOREncode_CloseAndSortMap(QCBOREncodeContext *pMe) |
| 1269 | { |
| 1270 | uint32_t uStart; |
| 1271 | |
| 1272 | /* The Header for the map we are about to sort hasn't been |
| 1273 | * inserted yet, so uStart is the position of the first item |
| 1274 | * and the end out the UsefulOutBuf data is the end of the |
| 1275 | * items we are about to sort. |
| 1276 | */ |
| 1277 | uStart = Nesting_GetStartPos(&(pMe->nesting)); |
| 1278 | QCBOREncodePriv_SortMap(pMe, uStart); |
| 1279 | |
Laurence Lundblade | 240ca82 | 2024-01-16 11:11:00 -0700 | [diff] [blame] | 1280 | InsertCBORHead(pMe, CBOR_MAJOR_TYPE_MAP, Nesting_GetCount(&(pMe->nesting))); |
Laurence Lundblade | d6e1302 | 2023-11-26 10:14:02 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | /* |
| 1285 | * Public functions for closing sorted maps. See qcbor/qcbor_encode.h |
| 1286 | */ |
| 1287 | void QCBOREncode_CloseAndSortMapIndef(QCBOREncodeContext *pMe) |
| 1288 | { |
| 1289 | uint32_t uStart; |
| 1290 | |
| 1291 | uStart = Nesting_GetStartPos(&(pMe->nesting)); |
| 1292 | QCBOREncodePriv_SortMap(pMe, uStart); |
| 1293 | |
| 1294 | QCBOREncode_CloseMapOrArrayIndefiniteLength(pMe, CBOR_MAJOR_NONE_TYPE_MAP_INDEFINITE_LEN); |
| 1295 | } |
| 1296 | |
| 1297 | |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1298 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 1299 | * Public functions for closing bstr wrapping. See qcbor/qcbor_encode.h |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1300 | */ |
| 1301 | void QCBOREncode_CloseBstrWrap2(QCBOREncodeContext *me, bool bIncludeCBORHead, UsefulBufC *pWrappedCBOR) |
| 1302 | { |
| 1303 | const size_t uInsertPosition = Nesting_GetStartPos(&(me->nesting)); |
| 1304 | const size_t uEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1305 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 1306 | /* This subtraction can't go negative because the UsefulOutBuf |
| 1307 | * always only grows and never shrinks. UsefulOutBut itself also |
| 1308 | * has defenses such that it won't write where it should not even |
| 1309 | * if given incorrect input lengths. |
| 1310 | */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1311 | const size_t uBstrLen = uEndPosition - uInsertPosition; |
| 1312 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 1313 | /* Actually insert */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1314 | InsertCBORHead(me, CBOR_MAJOR_TYPE_BYTE_STRING, uBstrLen); |
| 1315 | |
| 1316 | if(pWrappedCBOR) { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 1317 | /* Return pointer and length to the enclosed encoded CBOR. The |
| 1318 | * intended use is for it to be hashed (e.g., SHA-256) in a COSE |
| 1319 | * implementation. This must be used right away, as the pointer |
| 1320 | * and length go invalid on any subsequent calls to this |
| 1321 | * function because there might be calls to |
| 1322 | * InsertEncodedTypeAndNumber() that slides data to the right. |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1323 | */ |
| 1324 | size_t uStartOfNew = uInsertPosition; |
| 1325 | if(!bIncludeCBORHead) { |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 1326 | /* Skip over the CBOR head to just get the inserted bstr */ |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1327 | const size_t uNewEndPosition = UsefulOutBuf_GetEndPosition(&(me->OutBuf)); |
| 1328 | uStartOfNew += uNewEndPosition - uEndPosition; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1329 | } |
Laurence Lundblade | c9f0fbc | 2020-02-07 10:48:33 +0000 | [diff] [blame] | 1330 | const UsefulBufC PartialResult = UsefulOutBuf_OutUBuf(&(me->OutBuf)); |
| 1331 | *pWrappedCBOR = UsefulBuf_Tail(PartialResult, uStartOfNew); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1332 | } |
| 1333 | } |
| 1334 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1335 | |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 1336 | /* |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 1337 | * Public function for canceling a bstr wrap. See qcbor/qcbor_encode.h |
| 1338 | */ |
| 1339 | void QCBOREncode_CancelBstrWrap(QCBOREncodeContext *pMe) |
| 1340 | { |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 1341 | if(CheckDecreaseNesting(pMe, CBOR_MAJOR_TYPE_BYTE_STRING)) { |
| 1342 | return; |
| 1343 | } |
| 1344 | |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 1345 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 1346 | const size_t uCurrent = UsefulOutBuf_GetEndPosition(&(pMe->OutBuf)); |
| 1347 | if(pMe->nesting.pCurrentNesting->uStart != uCurrent) { |
| 1348 | pMe->uError = QCBOR_ERR_CANNOT_CANCEL; |
| 1349 | return; |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 1350 | } |
| 1351 | /* QCBOREncode_CancelBstrWrap() can't correctly undo |
| 1352 | * QCBOREncode_BstrWrapInMap() or QCBOREncode_BstrWrapInMapN(). It |
| 1353 | * can't undo the labels they add. It also doesn't catch the error |
| 1354 | * of using it this way. QCBOREncode_CancelBstrWrap() is used |
| 1355 | * infrequently and the the result is incorrect CBOR, not a |
| 1356 | * security hole, so no extra code or state is added to handle this |
| 1357 | * condition. |
| 1358 | */ |
| 1359 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 1360 | |
| 1361 | Nesting_Decrease(&(pMe->nesting)); |
| 1362 | Nesting_Decrement(&(pMe->nesting)); |
| 1363 | } |
| 1364 | |
| 1365 | |
| 1366 | /* |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1367 | * Public function for opening a byte string. See qcbor/qcbor_encode.h |
| 1368 | */ |
| 1369 | void QCBOREncode_OpenBytes(QCBOREncodeContext *pMe, UsefulBuf *pPlace) |
| 1370 | { |
| 1371 | *pPlace = UsefulOutBuf_GetOutPlace(&(pMe->OutBuf)); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1372 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 1373 | // TODO: is this right? |
| 1374 | uint8_t uMajorType = Nesting_GetMajorType(&(pMe->nesting)); |
| 1375 | if(uMajorType == CBOR_MAJOR_NONE_TYPE_OPEN_BSTR) { |
| 1376 | pMe->uError = QCBOR_ERR_OPEN_BYTE_STRING; |
| 1377 | return; |
| 1378 | } |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1379 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
| 1380 | |
Paul Liétar | 7978977 | 2022-07-26 20:33:18 +0100 | [diff] [blame] | 1381 | QCBOREncode_OpenMapOrArray(pMe, CBOR_MAJOR_NONE_TYPE_OPEN_BSTR); |
Laurence Lundblade | b24faef | 2022-04-26 11:03:08 -0600 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | |
| 1385 | /* |
| 1386 | * Public function for closing a byte string. See qcbor/qcbor_encode.h |
| 1387 | */ |
| 1388 | void QCBOREncode_CloseBytes(QCBOREncodeContext *pMe, const size_t uAmount) |
| 1389 | { |
| 1390 | UsefulOutBuf_Advance(&(pMe->OutBuf), uAmount); |
| 1391 | if(UsefulOutBuf_GetError(&(pMe->OutBuf))) { |
| 1392 | /* Advance too far. Normal off-end error handling in effect here. */ |
| 1393 | return; |
| 1394 | } |
| 1395 | |
| 1396 | InsertCBORHead(pMe, CBOR_MAJOR_NONE_TYPE_OPEN_BSTR, uAmount); |
| 1397 | } |
| 1398 | |
| 1399 | |
| 1400 | /* |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 1401 | * Public function for closing arrays and maps. See qcbor/qcbor_encode.h |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 1402 | */ |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 1403 | void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *pMe, uint8_t uMajorType) |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 1404 | { |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 1405 | if(CheckDecreaseNesting(pMe, uMajorType)) { |
| 1406 | return; |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 1407 | } |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 1408 | |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 1409 | /* Append the break marker (0xff for both arrays and maps) */ |
Laurence Lundblade | 274ddef | 2022-05-17 09:12:23 -0700 | [diff] [blame] | 1410 | AppendCBORHead(pMe, CBOR_MAJOR_NONE_TYPE_SIMPLE_BREAK, CBOR_SIMPLE_BREAK, 0); |
| 1411 | Nesting_Decrease(&(pMe->nesting)); |
Jan Jongboom | 4a93a66 | 2019-07-25 08:44:58 +0200 | [diff] [blame] | 1412 | } |
| 1413 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1414 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1415 | /* |
Laurence Lundblade | 8d3b855 | 2021-06-10 11:11:54 -0700 | [diff] [blame] | 1416 | * Public function to finish and get the encoded result. See qcbor/qcbor_encode.h |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1417 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1418 | QCBORError QCBOREncode_Finish(QCBOREncodeContext *me, UsefulBufC *pEncodedCBOR) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1419 | { |
Laurence Lundblade | f607a2a | 2019-07-05 21:25:25 -0700 | [diff] [blame] | 1420 | QCBORError uReturn = QCBOREncode_GetErrorState(me); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1421 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 1422 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1423 | goto Done; |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 1424 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1425 | |
Laurence Lundblade | daefdec | 2020-11-02 20:22:03 -0800 | [diff] [blame] | 1426 | #ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS |
Laurence Lundblade | 3e0a45c | 2020-11-05 11:12:04 -0800 | [diff] [blame] | 1427 | if(Nesting_IsInNest(&(me->nesting))) { |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 1428 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1429 | goto Done; |
| 1430 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1431 | #endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1432 | |
Laurence Lundblade | da3f082 | 2018-09-18 19:49:02 -0700 | [diff] [blame] | 1433 | *pEncodedCBOR = UsefulOutBuf_OutUBuf(&(me->OutBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1434 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1435 | Done: |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 1436 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1437 | } |
| 1438 | |
Laurence Lundblade | 0595e93 | 2018-11-02 22:22:47 +0700 | [diff] [blame] | 1439 | |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 1440 | /* |
Laurence Lundblade | 1fa579b | 2020-11-25 00:31:37 -0800 | [diff] [blame] | 1441 | * Public functions to get size of the encoded result. See qcbor/qcbor_encode.h |
Laurence Lundblade | 067035b | 2018-11-28 17:35:25 -0800 | [diff] [blame] | 1442 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1443 | QCBORError QCBOREncode_FinishGetSize(QCBOREncodeContext *me, size_t *puEncodedLen) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1444 | { |
Laurence Lundblade | da3f082 | 2018-09-18 19:49:02 -0700 | [diff] [blame] | 1445 | UsefulBufC Enc; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1446 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1447 | QCBORError nReturn = QCBOREncode_Finish(me, &Enc); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1448 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1449 | if(nReturn == QCBOR_SUCCESS) { |
Laurence Lundblade | da3f082 | 2018-09-18 19:49:02 -0700 | [diff] [blame] | 1450 | *puEncodedLen = Enc.len; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1451 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1452 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1453 | return nReturn; |
| 1454 | } |