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