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_decode.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 | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 38 | /* |
| 39 | This casts away the const-ness of a pointer, usually so it can be |
| 40 | freed or realloced. |
| 41 | */ |
| 42 | #define UNCONST_POINTER(ptr) ((void *)(ptr)) |
| 43 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 44 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 45 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 46 | /*=========================================================================== |
| 47 | DecodeNesting -- Functions for tracking array/map nesting when decoding |
| 48 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 49 | See qcbor/qcbor_decode.h for definition of the object |
| 50 | used here: QCBORDecodeNesting |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 51 | ===========================================================================*/ |
| 52 | |
| 53 | inline static int |
| 54 | IsMapOrArray(uint8_t uDataType) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 55 | { |
| 56 | return uDataType == QCBOR_TYPE_MAP || uDataType == QCBOR_TYPE_ARRAY; |
| 57 | } |
| 58 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 59 | inline static int |
| 60 | DecodeNesting_IsNested(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 61 | { |
| 62 | return pNesting->pCurrent != &(pNesting->pMapsAndArrays[0]); |
| 63 | } |
| 64 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 65 | inline static int |
| 66 | DecodeNesting_IsIndefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 67 | { |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 68 | return pNesting->pCurrent->uCount == UINT16_MAX; |
| 69 | } |
| 70 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 71 | inline static uint8_t |
| 72 | DecodeNesting_GetLevel(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 73 | { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 74 | // Check in DecodeNesting_Descend and never having |
| 75 | // QCBOR_MAX_ARRAY_NESTING > 255 gaurantee cast is safe |
| 76 | return (uint8_t)(pNesting->pCurrent - &(pNesting->pMapsAndArrays[0])); |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 77 | } |
| 78 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 79 | inline static int |
| 80 | DecodeNesting_TypeIsMap(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 81 | { |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 82 | if(!DecodeNesting_IsNested(pNesting)) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 83 | return 0; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 84 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 85 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 86 | return CBOR_MAJOR_TYPE_MAP == pNesting->pCurrent->uMajorType; |
| 87 | } |
| 88 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 89 | // Process a break. This will either ascend the nesting or error out |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 90 | inline static QCBORError |
| 91 | DecodeNesting_BreakAscend(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 92 | { |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 93 | // breaks must always occur when there is nesting |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 94 | if(!DecodeNesting_IsNested(pNesting)) { |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 95 | return QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 96 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 97 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 98 | // breaks can only occur when the map/array is indefinite length |
| 99 | if(!DecodeNesting_IsIndefiniteLength(pNesting)) { |
| 100 | return QCBOR_ERR_BAD_BREAK; |
| 101 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 102 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 103 | // if all OK, the break reduces the level of nesting |
| 104 | pNesting->pCurrent--; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 105 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 106 | return QCBOR_SUCCESS; |
| 107 | } |
| 108 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 109 | // Called on every single item except breaks including open of a map/array |
| 110 | inline static void |
| 111 | DecodeNesting_DecrementCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 112 | { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 113 | while(DecodeNesting_IsNested(pNesting)) { |
| 114 | // Not at the top level, so there is decrementing to be done. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 115 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 116 | if(!DecodeNesting_IsIndefiniteLength(pNesting)) { |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 117 | // Decrement the current nesting level if it is not indefinite. |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 118 | pNesting->pCurrent->uCount--; |
| 119 | } |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 120 | |
| 121 | if(pNesting->pCurrent->uCount != 0) { |
| 122 | // Did not close out an array or map, so nothing further |
| 123 | break; |
| 124 | } |
| 125 | |
| 126 | // Closed out an array or map so level up |
| 127 | pNesting->pCurrent--; |
| 128 | |
| 129 | // Continue with loop to see if closing out this doesn't close out more |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 133 | // Called on every map/array |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 134 | inline static QCBORError |
| 135 | DecodeNesting_Descend(QCBORDecodeNesting *pNesting, QCBORItem *pItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 136 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 137 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 138 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 139 | if(pItem->val.uCount == 0) { |
| 140 | // Nothing to do for empty definite lenth arrays. They are just are |
| 141 | // effectively the same as an item that is not a map or array |
| 142 | goto Done; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 143 | // Empty indefinite length maps and arrays are handled elsewhere |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 144 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 145 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 146 | // Error out if arrays is too long to handle |
| 147 | if(pItem->val.uCount != UINT16_MAX && pItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 148 | nReturn = QCBOR_ERR_ARRAY_TOO_LONG; |
| 149 | goto Done; |
| 150 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 151 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 152 | // Error out if nesting is too deep |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 153 | if(pNesting->pCurrent >= &(pNesting->pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING])) { |
| 154 | nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP; |
| 155 | goto Done; |
| 156 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 157 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 158 | // The actual descend |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 159 | pNesting->pCurrent++; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 160 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 161 | // Record a few details for this nesting level |
| 162 | pNesting->pCurrent->uMajorType = pItem->uDataType; |
| 163 | pNesting->pCurrent->uCount = pItem->val.uCount; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 164 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 165 | Done: |
| 166 | return nReturn;; |
| 167 | } |
| 168 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 169 | inline static void |
| 170 | DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 171 | { |
| 172 | pNesting->pCurrent = &(pNesting->pMapsAndArrays[0]); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 177 | /* |
| 178 | This list of built-in tags. Only add tags here that are |
| 179 | clearly established and useful. Once a tag is added here |
| 180 | it can't be taken out as that would break backwards compatibility. |
| 181 | There are only 48 slots available forever. |
| 182 | */ |
| 183 | static const uint16_t spBuiltInTagMap[] = { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 184 | CBOR_TAG_DATE_STRING, // See TAG_MAPPER_FIRST_SIX |
| 185 | CBOR_TAG_DATE_EPOCH, // See TAG_MAPPER_FIRST_SIX |
| 186 | CBOR_TAG_POS_BIGNUM, // See TAG_MAPPER_FIRST_SIX |
| 187 | CBOR_TAG_NEG_BIGNUM, // See TAG_MAPPER_FIRST_SIX |
| 188 | CBOR_TAG_DECIMAL_FRACTION, // See TAG_MAPPER_FIRST_SIX |
| 189 | CBOR_TAG_BIGFLOAT, // See TAG_MAPPER_FIRST_SIX |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 190 | CBOR_TAG_COSE_ENCRYPTO, |
| 191 | CBOR_TAG_COSE_MAC0, |
| 192 | CBOR_TAG_COSE_SIGN1, |
| 193 | CBOR_TAG_ENC_AS_B64URL, |
| 194 | CBOR_TAG_ENC_AS_B64, |
| 195 | CBOR_TAG_ENC_AS_B16, |
| 196 | CBOR_TAG_CBOR, |
| 197 | CBOR_TAG_URI, |
| 198 | CBOR_TAG_B64URL, |
| 199 | CBOR_TAG_B64, |
| 200 | CBOR_TAG_REGEX, |
| 201 | CBOR_TAG_MIME, |
| 202 | CBOR_TAG_BIN_UUID, |
| 203 | CBOR_TAG_CWT, |
| 204 | CBOR_TAG_ENCRYPT, |
| 205 | CBOR_TAG_MAC, |
| 206 | CBOR_TAG_SIGN, |
| 207 | CBOR_TAG_GEO_COORD, |
| 208 | CBOR_TAG_CBOR_MAGIC |
| 209 | }; |
| 210 | |
| 211 | // This is used in a bit of cleverness in GetNext_TaggedItem() to |
| 212 | // keep code size down and switch for the internal processing of |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 213 | // these types. This will break if the first six items in |
| 214 | // spBuiltInTagMap don't have values 0,1,2,3,4,5. That is the |
| 215 | // mapping is 0 to 0, 1 to 1, 2 to 2 and 3 to 3.... |
| 216 | #define QCBOR_TAGFLAG_DATE_STRING (0x01LL << CBOR_TAG_DATE_STRING) |
| 217 | #define QCBOR_TAGFLAG_DATE_EPOCH (0x01LL << CBOR_TAG_DATE_EPOCH) |
| 218 | #define QCBOR_TAGFLAG_POS_BIGNUM (0x01LL << CBOR_TAG_POS_BIGNUM) |
| 219 | #define QCBOR_TAGFLAG_NEG_BIGNUM (0x01LL << CBOR_TAG_NEG_BIGNUM) |
| 220 | #define QCBOR_TAGFLAG_DECIMAL_FRACTION (0x01LL << CBOR_TAG_DECIMAL_FRACTION) |
| 221 | #define QCBOR_TAGFLAG_BIGFLOAT (0x01LL << CBOR_TAG_BIGFLOAT) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 222 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 223 | #define TAG_MAPPER_FIRST_SIX (QCBOR_TAGFLAG_DATE_STRING |\ |
| 224 | QCBOR_TAGFLAG_DATE_EPOCH |\ |
| 225 | QCBOR_TAGFLAG_POS_BIGNUM |\ |
| 226 | QCBOR_TAGFLAG_NEG_BIGNUM |\ |
| 227 | QCBOR_TAGFLAG_DECIMAL_FRACTION |\ |
| 228 | QCBOR_TAGFLAG_BIGFLOAT) |
| 229 | |
| 230 | #define TAG_MAPPER_FIRST_FOUR (QCBOR_TAGFLAG_DATE_STRING |\ |
| 231 | QCBOR_TAGFLAG_DATE_EPOCH |\ |
| 232 | QCBOR_TAGFLAG_POS_BIGNUM |\ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 233 | QCBOR_TAGFLAG_NEG_BIGNUM) |
| 234 | |
| 235 | #define TAG_MAPPER_TOTAL_TAG_BITS 64 // Number of bits in a uint64_t |
| 236 | #define TAG_MAPPER_CUSTOM_TAGS_BASE_INDEX (TAG_MAPPER_TOTAL_TAG_BITS - QCBOR_MAX_CUSTOM_TAGS) // 48 |
| 237 | #define TAG_MAPPER_MAX_SIZE_BUILT_IN_TAGS (TAG_MAPPER_TOTAL_TAG_BITS - QCBOR_MAX_CUSTOM_TAGS ) // 48 |
| 238 | |
| 239 | static inline int TagMapper_LookupBuiltIn(uint64_t uTag) |
| 240 | { |
| 241 | if(sizeof(spBuiltInTagMap)/sizeof(uint16_t) > TAG_MAPPER_MAX_SIZE_BUILT_IN_TAGS) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 242 | /* |
| 243 | This is a cross-check to make sure the above array doesn't |
| 244 | accidentally get made too big. In normal conditions the above |
| 245 | test should optimize out as all the values are known at compile |
| 246 | time. |
| 247 | */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 248 | return -1; |
| 249 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 250 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 251 | if(uTag > UINT16_MAX) { |
| 252 | // This tag map works only on 16-bit tags |
| 253 | return -1; |
| 254 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 255 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 256 | for(int nTagBitIndex = 0; nTagBitIndex < (int)(sizeof(spBuiltInTagMap)/sizeof(uint16_t)); nTagBitIndex++) { |
| 257 | if(spBuiltInTagMap[nTagBitIndex] == uTag) { |
| 258 | return nTagBitIndex; |
| 259 | } |
| 260 | } |
| 261 | return -1; // Indicates no match |
| 262 | } |
| 263 | |
| 264 | static inline int TagMapper_LookupCallerConfigured(const QCBORTagListIn *pCallerConfiguredTagMap, uint64_t uTag) |
| 265 | { |
| 266 | for(int nTagBitIndex = 0; nTagBitIndex < pCallerConfiguredTagMap->uNumTags; nTagBitIndex++) { |
| 267 | if(pCallerConfiguredTagMap->puTags[nTagBitIndex] == uTag) { |
| 268 | return nTagBitIndex + TAG_MAPPER_CUSTOM_TAGS_BASE_INDEX; |
| 269 | } |
| 270 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 271 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 272 | return -1; // Indicates no match |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | Find the tag bit index for a given tag value, or error out |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 277 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 278 | This and the above functions could probably be optimized and made |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 279 | clearer and neater. |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 280 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 281 | static QCBORError |
| 282 | TagMapper_Lookup(const QCBORTagListIn *pCallerConfiguredTagMap, |
| 283 | uint64_t uTag, |
| 284 | uint8_t *puTagBitIndex) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 285 | { |
| 286 | int nTagBitIndex = TagMapper_LookupBuiltIn(uTag); |
| 287 | if(nTagBitIndex >= 0) { |
| 288 | // Cast is safe because TagMapper_LookupBuiltIn never returns > 47 |
| 289 | *puTagBitIndex = (uint8_t)nTagBitIndex; |
| 290 | return QCBOR_SUCCESS; |
| 291 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 292 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 293 | if(pCallerConfiguredTagMap) { |
| 294 | if(pCallerConfiguredTagMap->uNumTags > QCBOR_MAX_CUSTOM_TAGS) { |
| 295 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 296 | } |
| 297 | nTagBitIndex = TagMapper_LookupCallerConfigured(pCallerConfiguredTagMap, uTag); |
| 298 | if(nTagBitIndex >= 0) { |
| 299 | // Cast is safe because TagMapper_LookupBuiltIn never returns > 63 |
| 300 | |
| 301 | *puTagBitIndex = (uint8_t)nTagBitIndex; |
| 302 | return QCBOR_SUCCESS; |
| 303 | } |
| 304 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 305 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 306 | return QCBOR_ERR_BAD_OPT_TAG; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 311 | /*=========================================================================== |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 312 | QCBORStringAllocate -- STRING ALLOCATOR INVOCATION |
| 313 | |
| 314 | The following four functions are pretty wrappers for invocation of |
| 315 | the string allocator supplied by the caller. |
| 316 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 317 | ===========================================================================*/ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 318 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 319 | static inline void |
| 320 | StringAllocator_Free(const QCORInternalAllocator *pMe, void *pMem) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 321 | { |
| 322 | (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, 0); |
| 323 | } |
| 324 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 325 | // StringAllocator_Reallocate called with pMem NULL is |
| 326 | // equal to StringAllocator_Allocate() |
| 327 | static inline UsefulBuf |
| 328 | StringAllocator_Reallocate(const QCORInternalAllocator *pMe, |
| 329 | void *pMem, |
| 330 | size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 331 | { |
| 332 | return (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, uSize); |
| 333 | } |
| 334 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 335 | static inline UsefulBuf |
| 336 | StringAllocator_Allocate(const QCORInternalAllocator *pMe, size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 337 | { |
| 338 | return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize); |
| 339 | } |
| 340 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 341 | static inline void |
| 342 | StringAllocator_Destruct(const QCORInternalAllocator *pMe) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 343 | { |
| 344 | if(pMe->pfAllocator) { |
| 345 | (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | |
| 350 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 351 | /*=========================================================================== |
| 352 | QCBORDecode -- The main implementation of CBOR decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 353 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 354 | See qcbor/qcbor_decode.h for definition of the object |
| 355 | used here: QCBORDecodeContext |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 356 | ===========================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 357 | /* |
| 358 | Public function, see header file |
| 359 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 360 | void QCBORDecode_Init(QCBORDecodeContext *me, |
| 361 | UsefulBufC EncodedCBOR, |
| 362 | QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 363 | { |
| 364 | memset(me, 0, sizeof(QCBORDecodeContext)); |
| 365 | UsefulInputBuf_Init(&(me->InBuf), EncodedCBOR); |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 366 | // Don't bother with error check on decode mode. If a bad value is |
| 367 | // passed it will just act as if the default normal mode of 0 was set. |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 368 | me->uDecodeMode = (uint8_t)nDecodeMode; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 369 | DecodeNesting_Init(&(me->nesting)); |
| 370 | } |
| 371 | |
| 372 | |
| 373 | /* |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 374 | Public function, see header file |
| 375 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 376 | void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe, |
| 377 | QCBORStringAllocate pfAllocateFunction, |
| 378 | void *pAllocateContext, |
| 379 | bool bAllStrings) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 380 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 381 | pMe->StringAllocator.pfAllocator = pfAllocateFunction; |
| 382 | pMe->StringAllocator.pAllocateCxt = pAllocateContext; |
| 383 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 386 | |
| 387 | /* |
| 388 | Public function, see header file |
| 389 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 390 | void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *me, |
| 391 | const QCBORTagListIn *pTagList) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 392 | { |
| 393 | me->pCallerConfiguredTagList = pTagList; |
| 394 | } |
| 395 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 396 | |
| 397 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 398 | This decodes the fundamental part of a CBOR data item, the type and |
| 399 | number |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 400 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 401 | This is the Counterpart to InsertEncodedTypeAndNumber(). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 402 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 403 | This does the network->host byte order conversion. The conversion |
| 404 | here also results in the conversion for floats in addition to that |
| 405 | for lengths, tags and integer values. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 406 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 407 | This returns: |
| 408 | pnMajorType -- the major type for the item |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 409 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 410 | puArgument -- the "number" which is used a the value for integers, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 411 | tags and floats and length for strings and arrays |
| 412 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 413 | pnAdditionalInfo -- Pass this along to know what kind of float or |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 414 | if length is indefinite |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 415 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 416 | The int type is preferred to uint8_t for some variables as this |
| 417 | avoids integer promotions, can reduce code size and makes |
| 418 | static analyzers happier. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 419 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 420 | inline static QCBORError DecodeTypeAndNumber(UsefulInputBuf *pUInBuf, |
| 421 | int *pnMajorType, |
| 422 | uint64_t *puArgument, |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 423 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 424 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 425 | QCBORError nReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 426 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 427 | // Get the initial byte that every CBOR data item has |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 428 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 429 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 430 | // Break down the initial byte |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 431 | const int nTmpMajorType = nInitialByte >> 5; |
| 432 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 433 | |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 434 | // Where the number or argument accumulates |
| 435 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 436 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 437 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 438 | // Need to get 1,2,4 or 8 additional argument bytes Map |
| 439 | // LEN_IS_ONE_BYTE.. LEN_IS_EIGHT_BYTES to actual length |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 440 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 441 | |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 442 | // Loop getting all the bytes in the argument |
| 443 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 444 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 445 | // This shift and add gives the endian conversion |
| 446 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 447 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 448 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 449 | // The reserved and thus-far unused additional info values |
| 450 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 451 | goto Done; |
| 452 | } else { |
| 453 | // Less than 24, additional info is argument or 31, an indefinite length |
| 454 | // No more bytes to get |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 455 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 456 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 457 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 458 | if(UsefulInputBuf_GetError(pUInBuf)) { |
| 459 | nReturn = QCBOR_ERR_HIT_END; |
| 460 | goto Done; |
| 461 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 462 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 463 | // All successful if we got here. |
| 464 | nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 465 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 466 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 467 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 468 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 469 | Done: |
| 470 | return nReturn; |
| 471 | } |
| 472 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 473 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 474 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 475 | CBOR doesn't explicitly specify two's compliment for integers but all |
| 476 | CPUs use it these days and the test vectors in the RFC are so. All |
| 477 | integers in the CBOR structure are positive and the major type |
| 478 | indicates positive or negative. CBOR can express positive integers |
| 479 | up to 2^x - 1 where x is the number of bits and negative integers |
| 480 | down to 2^x. Note that negative numbers can be one more away from |
| 481 | zero than positive. Stdint, as far as I can tell, uses two's |
| 482 | compliment to represent negative integers. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 483 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 484 | See http://www.unix.org/whitepapers/64bit.html for reasons int isn't |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 485 | used carefully here, and in particular why it isn't used in the interface. |
| 486 | Also see |
| 487 | https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 488 | |
| 489 | Int is used for values that need less than 16-bits and would be subject |
| 490 | to integer promotion and complaining by static analyzers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 491 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 492 | inline static QCBORError |
| 493 | DecodeInteger(int nMajorType, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 494 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 495 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 496 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 497 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
| 498 | if (uNumber <= INT64_MAX) { |
| 499 | pDecodedItem->val.int64 = (int64_t)uNumber; |
| 500 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 501 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 502 | } else { |
| 503 | pDecodedItem->val.uint64 = uNumber; |
| 504 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 505 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 506 | } |
| 507 | } else { |
| 508 | if(uNumber <= INT64_MAX) { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 509 | // CBOR's representation of negative numbers lines up with the |
| 510 | // two-compliment representation. A negative integer has one |
| 511 | // more in range than a positive integer. INT64_MIN is |
| 512 | // equal to (-INT64_MAX) - 1. |
| 513 | pDecodedItem->val.int64 = (-(int64_t)uNumber) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 514 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 515 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 516 | } else { |
| 517 | // C can't represent a negative integer in this range |
Laurence Lundblade | 21d1d81 | 2019-09-28 22:47:49 -1000 | [diff] [blame] | 518 | // so it is an error. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 519 | nReturn = QCBOR_ERR_INT_OVERFLOW; |
| 520 | } |
| 521 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 522 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 523 | return nReturn; |
| 524 | } |
| 525 | |
| 526 | // Make sure #define value line up as DecodeSimple counts on this. |
| 527 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 528 | #error QCBOR_TYPE_FALSE macro value wrong |
| 529 | #endif |
| 530 | |
| 531 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 532 | #error QCBOR_TYPE_TRUE macro value wrong |
| 533 | #endif |
| 534 | |
| 535 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 536 | #error QCBOR_TYPE_NULL macro value wrong |
| 537 | #endif |
| 538 | |
| 539 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 540 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 541 | #endif |
| 542 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 543 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 544 | #error QCBOR_TYPE_BREAK macro value wrong |
| 545 | #endif |
| 546 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 547 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 548 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 549 | #endif |
| 550 | |
| 551 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 552 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 553 | #endif |
| 554 | |
| 555 | /* |
| 556 | Decode true, false, floats, break... |
| 557 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 558 | inline static QCBORError |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 559 | DecodeSimple(int nAdditionalInfo, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 560 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 561 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 562 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 563 | // uAdditionalInfo is 5 bits from the initial byte compile time checks |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 564 | // above make sure uAdditionalInfo values line up with uDataType values. |
| 565 | // DecodeTypeAndNumber never returns a major type > 1f so cast is safe |
| 566 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 567 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 568 | switch(nAdditionalInfo) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 569 | // No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they are |
| 570 | // caught before this is called. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 571 | |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 572 | case HALF_PREC_FLOAT: |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 573 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uNumber); |
| 574 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 575 | break; |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 576 | case SINGLE_PREC_FLOAT: |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 577 | pDecodedItem->val.dfnum = (double)UsefulBufUtil_CopyUint32ToFloat((uint32_t)uNumber); |
| 578 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 579 | break; |
| 580 | case DOUBLE_PREC_FLOAT: |
| 581 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uNumber); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 582 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 583 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 584 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 585 | case CBOR_SIMPLEV_FALSE: // 20 |
| 586 | case CBOR_SIMPLEV_TRUE: // 21 |
| 587 | case CBOR_SIMPLEV_NULL: // 22 |
| 588 | case CBOR_SIMPLEV_UNDEF: // 23 |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 589 | case CBOR_SIMPLE_BREAK: // 31 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 590 | break; // nothing to do |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 591 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 592 | case CBOR_SIMPLEV_ONEBYTE: // 24 |
| 593 | if(uNumber <= CBOR_SIMPLE_BREAK) { |
| 594 | // This takes out f8 00 ... f8 1f which should be encoded as e0 … f7 |
Laurence Lundblade | 077475f | 2019-04-26 09:06:33 -0700 | [diff] [blame] | 595 | nReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 596 | goto Done; |
| 597 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 598 | /* FALLTHROUGH */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 599 | // fall through intentionally |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 600 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 601 | default: // 0-19 |
| 602 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 603 | /* |
| 604 | DecodeTypeAndNumber will make uNumber equal to |
| 605 | uAdditionalInfo when uAdditionalInfo is < 24 This cast is |
| 606 | safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 607 | the double/float cases above |
| 608 | */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 609 | pDecodedItem->val.uSimple = (uint8_t)uNumber; |
| 610 | break; |
| 611 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 612 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 613 | Done: |
| 614 | return nReturn; |
| 615 | } |
| 616 | |
| 617 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 618 | /* |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 619 | Decode text and byte strings. Call the string allocator if asked to. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 620 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 621 | inline static QCBORError DecodeBytes(const QCORInternalAllocator *pAllocator, |
| 622 | int nMajorType, |
| 623 | uint64_t uStrLen, |
| 624 | UsefulInputBuf *pUInBuf, |
| 625 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 626 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 627 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 628 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 629 | // CBOR lengths can be 64 bits, but size_t is not 64 bits on all CPUs. |
| 630 | // This check makes the casts to size_t below safe. |
| 631 | |
| 632 | // 4 bytes less than the largest sizeof() so this can be tested by |
| 633 | // putting a SIZE_MAX length in the CBOR test input (no one will |
| 634 | // care the limit on strings is 4 bytes shorter). |
| 635 | if(uStrLen > SIZE_MAX-4) { |
| 636 | nReturn = QCBOR_ERR_STRING_TOO_LONG; |
| 637 | goto Done; |
| 638 | } |
| 639 | |
| 640 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 641 | if(UsefulBuf_IsNULLC(Bytes)) { |
| 642 | // Failed to get the bytes for this string item |
| 643 | nReturn = QCBOR_ERR_HIT_END; |
| 644 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 645 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 646 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 647 | if(pAllocator) { |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 648 | // We are asked to use string allocator to make a copy |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 649 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 650 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 651 | nReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 652 | goto Done; |
| 653 | } |
| 654 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 655 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 656 | } else { |
| 657 | // Normal case with no string allocator |
| 658 | pDecodedItem->val.string = Bytes; |
| 659 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 660 | const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 661 | // Cast because ternary operator causes promotion to integer |
| 662 | pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING |
| 663 | : QCBOR_TYPE_TEXT_STRING); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 664 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 665 | Done: |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 666 | return nReturn; |
| 667 | } |
| 668 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 669 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 670 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 671 | |
| 672 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 673 | |
| 674 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 675 | // Make sure the constants align as this is assumed by |
| 676 | // the GetAnItem() implementation |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 677 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 678 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 679 | #endif |
| 680 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 681 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 682 | #endif |
| 683 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 684 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 685 | This gets a single data item and decodes it including preceding |
| 686 | optional tagging. This does not deal with arrays and maps and nesting |
| 687 | except to decode the data item introducing them. Arrays and maps are |
| 688 | handled at the next level up in GetNext(). |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 689 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 690 | Errors detected here include: an array that is too long to decode, |
| 691 | hit end of buffer unexpectedly, a few forms of invalid encoded CBOR |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 692 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 693 | static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf, |
| 694 | QCBORItem *pDecodedItem, |
| 695 | const QCORInternalAllocator *pAllocator) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 696 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 697 | QCBORError nReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 698 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 699 | /* |
| 700 | Get the major type and the number. Number could be length of more |
| 701 | bytes or the value depending on the major type nAdditionalInfo is |
| 702 | an encoding of the length of the uNumber and is needed to decode |
| 703 | floats and doubles |
| 704 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 705 | int nMajorType; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 706 | uint64_t uNumber; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 707 | int nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 708 | |
Laurence Lundblade | 4b09f63 | 2019-10-09 14:34:59 -0700 | [diff] [blame] | 709 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 710 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 711 | nReturn = DecodeTypeAndNumber(pUInBuf, &nMajorType, &uNumber, &nAdditionalInfo); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 712 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 713 | // Error out here if we got into trouble on the type and number. The |
| 714 | // code after this will not work if the type and number is not good. |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 715 | if(nReturn) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 716 | goto Done; |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 717 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 718 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 719 | // At this point the major type and the value are valid. We've got |
| 720 | // the type and the number that starts every CBOR data item. |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 721 | switch (nMajorType) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 722 | case CBOR_MAJOR_TYPE_POSITIVE_INT: // Major type 0 |
| 723 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: // Major type 1 |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 724 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 725 | nReturn = QCBOR_ERR_BAD_INT; |
| 726 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 727 | nReturn = DecodeInteger(nMajorType, uNumber, pDecodedItem); |
Laurence Lundblade | 3a6042e | 2019-06-28 19:58:04 -0700 | [diff] [blame] | 728 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 729 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 730 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 731 | case CBOR_MAJOR_TYPE_BYTE_STRING: // Major type 2 |
| 732 | case CBOR_MAJOR_TYPE_TEXT_STRING: // Major type 3 |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 733 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 734 | const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING); |
| 735 | pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING |
| 736 | : QCBOR_TYPE_TEXT_STRING); |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 737 | pDecodedItem->val.string = (UsefulBufC){NULL, SIZE_MAX}; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 738 | } else { |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 739 | nReturn = DecodeBytes(pAllocator, nMajorType, uNumber, pUInBuf, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 740 | } |
| 741 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 742 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 743 | case CBOR_MAJOR_TYPE_ARRAY: // Major type 4 |
| 744 | case CBOR_MAJOR_TYPE_MAP: // Major type 5 |
| 745 | // Record the number of items in the array or map |
| 746 | if(uNumber > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 747 | nReturn = QCBOR_ERR_ARRAY_TOO_LONG; |
| 748 | goto Done; |
| 749 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 750 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 751 | pDecodedItem->val.uCount = UINT16_MAX; // Indicate indefinite length |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 752 | } else { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 753 | // type conversion OK because of check above |
| 754 | pDecodedItem->val.uCount = (uint16_t)uNumber; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 755 | } |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 756 | // C preproc #if above makes sure constants for major types align |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 757 | // DecodeTypeAndNumber never returns a major type > 7 so cast is safe |
| 758 | pDecodedItem->uDataType = (uint8_t)nMajorType; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 759 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 760 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 761 | case CBOR_MAJOR_TYPE_OPTIONAL: // Major type 6, optional prepended tags |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 762 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 763 | nReturn = QCBOR_ERR_BAD_INT; |
| 764 | } else { |
| 765 | pDecodedItem->val.uTagV = uNumber; |
| 766 | pDecodedItem->uDataType = QCBOR_TYPE_OPTTAG; |
| 767 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 768 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 769 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 770 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 771 | // Major type 7, float, double, true, false, null... |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 772 | nReturn = DecodeSimple(nAdditionalInfo, uNumber, pDecodedItem); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 773 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 774 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 775 | default: |
| 776 | // Never happens because DecodeTypeAndNumber() should never return > 7 |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 777 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 778 | break; |
| 779 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 780 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 781 | Done: |
| 782 | return nReturn; |
| 783 | } |
| 784 | |
| 785 | |
| 786 | |
| 787 | /* |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 788 | This layer deals with indefinite length strings. It pulls all the |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 789 | individual chunk items together into one QCBORItem using the string |
| 790 | allocator. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 791 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 792 | Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 793 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 794 | static inline QCBORError |
| 795 | GetNext_FullItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 796 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 797 | // Stack usage; int/ptr 2 UsefulBuf 2 QCBORItem -- 96 |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame^] | 798 | |
| 799 | // Get pointer to string allocator. First use is to pass it to |
| 800 | // GetNext_Item() when option is set to allocate for *every* string. |
| 801 | // Second use here is to allocate space to coallese indefinite |
| 802 | // length string items into one. |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 803 | const QCORInternalAllocator *pAllocator = me->StringAllocator.pfAllocator ? |
| 804 | &(me->StringAllocator) : |
| 805 | NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 806 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame^] | 807 | QCBORError nReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 808 | nReturn = GetNext_Item(&(me->InBuf), |
| 809 | pDecodedItem, |
| 810 | me->bStringAllocateAll ? pAllocator: NULL); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 811 | if(nReturn) { |
| 812 | goto Done; |
| 813 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 814 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 815 | // To reduce code size by removing support for indefinite length strings, the |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 816 | // code in this function from here down can be eliminated. Run tests, except |
| 817 | // indefinite length string tests, to be sure all is OK if this is removed. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 818 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 819 | // Only do indefinite length processing on strings |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame^] | 820 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 821 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 822 | goto Done; // no need to do any work here on non-string types |
| 823 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 824 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 825 | // Is this a string with an indefinite length? |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 826 | if(pDecodedItem->val.string.len != SIZE_MAX) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 827 | goto Done; // length is not indefinite, so no work to do here |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 828 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 829 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 830 | // Can't do indefinite length strings without a string allocator |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 831 | if(pAllocator == NULL) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 832 | nReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 833 | goto Done; |
| 834 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 835 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 836 | // Loop getting chunk of indefinite string |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame^] | 837 | UsefulBufC FullString = NULLUsefulBufC; |
| 838 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 839 | for(;;) { |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 840 | // Get item for next chunk |
| 841 | QCBORItem StringChunkItem; |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame^] | 842 | // NULL string allocator passed here. Do not need to allocate |
| 843 | // chunks even if bStringAllocateAll is set. |
Laurence Lundblade | fae26bf | 2019-02-18 11:15:43 -0800 | [diff] [blame] | 844 | nReturn = GetNext_Item(&(me->InBuf), &StringChunkItem, NULL); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 845 | if(nReturn) { |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 846 | break; // Error getting the next chunk |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 847 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 848 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 849 | // See if it is a marker at end of indefinite length string |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 850 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 851 | // String is complete |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 852 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 853 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 854 | break; |
| 855 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 856 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 857 | // Match data type of chunk to type at beginning. |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 858 | // Also catches error of other non-string types that don't belong. |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 859 | // Also catches indefinite length strings inside indefinite length strings |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 860 | if(StringChunkItem.uDataType != uStringType || |
| 861 | StringChunkItem.val.string.len == SIZE_MAX) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 862 | nReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 863 | break; |
| 864 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 865 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 866 | // Alloc new buffer or expand previously allocated buffer so it can fit |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 867 | // The first time throurgh FullString.ptr is NULL and this is |
| 868 | // equivalent to StringAllocator_Allocate() |
| 869 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
| 870 | UNCONST_POINTER(FullString.ptr), |
| 871 | FullString.len + StringChunkItem.val.string.len); |
| 872 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 873 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 874 | // Allocation of memory for the string failed |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 875 | nReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 876 | break; |
| 877 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 878 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 879 | // Copy new string chunk at the end of string so far. |
| 880 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 881 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 882 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 883 | if(nReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 884 | // Getting the item failed, clean up the allocated memory |
| 885 | StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr)); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 886 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 887 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame^] | 888 | Done: |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 889 | return nReturn; |
| 890 | } |
| 891 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 892 | |
| 893 | /* |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 894 | Gets all optional tag data items preceding a data item that is not an |
| 895 | optional tag and records them as bits in the tag map. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 896 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 897 | static QCBORError |
| 898 | GetNext_TaggedItem(QCBORDecodeContext *me, |
| 899 | QCBORItem *pDecodedItem, |
| 900 | QCBORTagListOut *pTags) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 901 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 902 | // Stack usage: int/ptr: 3 -- 24 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 903 | QCBORError nReturn; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 904 | uint64_t uTagBits = 0; |
| 905 | if(pTags) { |
| 906 | pTags->uNumUsed = 0; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 907 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 908 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 909 | // Loop fetching items until the item fetched is not a tag |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 910 | for(;;) { |
| 911 | nReturn = GetNext_FullItem(me, pDecodedItem); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 912 | if(nReturn) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 913 | goto Done; // Error out of the loop |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 914 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 915 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 916 | if(pDecodedItem->uDataType != QCBOR_TYPE_OPTTAG) { |
| 917 | // Successful exit from loop; maybe got some tags, maybe not |
| 918 | pDecodedItem->uTagBits = uTagBits; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 919 | break; |
| 920 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 921 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 922 | uint8_t uTagBitIndex; |
| 923 | // Tag was mapped, tag was not mapped, error with tag list |
| 924 | switch(TagMapper_Lookup(me->pCallerConfiguredTagList, pDecodedItem->val.uTagV, &uTagBitIndex)) { |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 925 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 926 | case QCBOR_SUCCESS: |
| 927 | // Successfully mapped the tag |
| 928 | uTagBits |= 0x01ULL << uTagBitIndex; |
| 929 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 930 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 931 | case QCBOR_ERR_BAD_OPT_TAG: |
| 932 | // Tag is not recognized. Do nothing |
| 933 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 934 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 935 | default: |
| 936 | // Error Condition |
| 937 | goto Done; |
| 938 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 939 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 940 | if(pTags) { |
| 941 | // Caller wants all tags recorded in the provided buffer |
| 942 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 943 | nReturn = QCBOR_ERR_TOO_MANY_TAGS; |
| 944 | goto Done; |
| 945 | } |
| 946 | pTags->puTags[pTags->uNumUsed] = pDecodedItem->val.uTagV; |
| 947 | pTags->uNumUsed++; |
| 948 | } |
| 949 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 950 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 951 | Done: |
| 952 | return nReturn; |
| 953 | } |
| 954 | |
| 955 | |
| 956 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 957 | This layer takes care of map entries. It combines the label and data |
| 958 | items into one QCBORItem. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 959 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 960 | static inline QCBORError |
| 961 | GetNext_MapEntry(QCBORDecodeContext *me, |
| 962 | QCBORItem *pDecodedItem, |
| 963 | QCBORTagListOut *pTags) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 964 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 965 | // Stack use: int/ptr 1, QCBORItem -- 56 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 966 | QCBORError nReturn = GetNext_TaggedItem(me, pDecodedItem, pTags); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 967 | if(nReturn) |
| 968 | goto Done; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 969 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 970 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 971 | // Break can't be a map entry |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 972 | goto Done; |
| 973 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 974 | |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 975 | if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
| 976 | // In a map and caller wants maps decoded, not treated as arrays |
| 977 | |
| 978 | if(DecodeNesting_TypeIsMap(&(me->nesting))) { |
| 979 | // If in a map and the right decoding mode, get the label |
| 980 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 981 | // Save label in pDecodedItem and get the next which will |
| 982 | // be the real data |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 983 | QCBORItem LabelItem = *pDecodedItem; |
| 984 | nReturn = GetNext_TaggedItem(me, pDecodedItem, pTags); |
| 985 | if(nReturn) |
| 986 | goto Done; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 987 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 988 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 989 | |
| 990 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 991 | // strings are always good labels |
| 992 | pDecodedItem->label.string = LabelItem.val.string; |
| 993 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 994 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) { |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 995 | // It's not a string and we only want strings |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 996 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 997 | goto Done; |
| 998 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 999 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1000 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1001 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1002 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1003 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1004 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1005 | pDecodedItem->label.string = LabelItem.val.string; |
| 1006 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1007 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1008 | } else { |
| 1009 | // label is not an int or a string. It is an arrray |
| 1010 | // or a float or such and this implementation doesn't handle that. |
| 1011 | // Also, tags on labels are ignored. |
| 1012 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1013 | goto Done; |
| 1014 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1015 | } |
| 1016 | } else { |
| 1017 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1018 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
| 1019 | nReturn = QCBOR_ERR_ARRAY_TOO_LONG; |
| 1020 | goto Done; |
| 1021 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1022 | // Decoding a map as an array |
| 1023 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1024 | // Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2 |
| 1025 | // Cast is needed because of integer promotion |
| 1026 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1027 | } |
| 1028 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1029 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1030 | Done: |
| 1031 | return nReturn; |
| 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1036 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1037 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1038 | QCBORError QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me, |
| 1039 | QCBORItem *pDecodedItem, |
| 1040 | QCBORTagListOut *pTags) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1041 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1042 | // Stack ptr/int: 2, QCBORItem : 64 |
| 1043 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1044 | // The public entry point for fetching and parsing the next QCBORItem. |
| 1045 | // All the CBOR parsing work is here and in subordinate calls. |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1046 | QCBORError nReturn; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1047 | |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1048 | // Check if there are an |
| 1049 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf)) == 0 && !DecodeNesting_IsNested(&(me->nesting))) { |
| 1050 | nReturn = QCBOR_ERR_NO_MORE_ITEMS; |
| 1051 | goto Done; |
| 1052 | } |
| 1053 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1054 | nReturn = GetNext_MapEntry(me, pDecodedItem, pTags); |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1055 | if(nReturn) { |
| 1056 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1057 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1058 | |
| 1059 | // Break ending arrays/maps are always processed at the end of this function. |
| 1060 | // They should never show up here. |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1061 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1062 | nReturn = QCBOR_ERR_BAD_BREAK; |
| 1063 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1064 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1065 | |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1066 | // Record the nesting level for this data item before processing any of |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1067 | // decrementing and descending. |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1068 | pDecodedItem->uNestingLevel = DecodeNesting_GetLevel(&(me->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1069 | |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1070 | // Process the item just received for descent or decrement, and |
| 1071 | // ascent if decrements are enough to close out a definite length array/map |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 1072 | if(IsMapOrArray(pDecodedItem->uDataType)) { |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1073 | // If the new item is array or map, the nesting level descends |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 1074 | nReturn = DecodeNesting_Descend(&(me->nesting), pDecodedItem); |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1075 | // Maps and arrays do count in as items in the map/array that encloses |
| 1076 | // them so a decrement needs to be done for them too, but that is done |
| 1077 | // only when all the items in them have been processed, not when they |
Laurence Lundblade | 9916b1b | 2019-09-07 22:33:25 -0700 | [diff] [blame] | 1078 | // are opened with the exception of an empty map or array. |
| 1079 | if(pDecodedItem->val.uCount == 0) { |
| 1080 | DecodeNesting_DecrementCount(&(me->nesting)); |
| 1081 | } |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1082 | } else { |
| 1083 | // Decrement the count of items in the enclosing map/array |
| 1084 | // If the count in the enclosing map/array goes to zero, that |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1085 | // triggers a decrement in the map/array above that and |
| 1086 | // an ascend in nesting level. |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1087 | DecodeNesting_DecrementCount(&(me->nesting)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1088 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1089 | if(nReturn) { |
| 1090 | goto Done; |
| 1091 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1092 | |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1093 | // For indefinite length maps/arrays, looking at any and |
| 1094 | // all breaks that might terminate them. The equivalent |
| 1095 | // for definite length maps/arrays happens in |
| 1096 | // DecodeNesting_DecrementCount(). |
| 1097 | if(DecodeNesting_IsNested(&(me->nesting)) && DecodeNesting_IsIndefiniteLength(&(me->nesting))) { |
| 1098 | while(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) { |
| 1099 | // Peek forward one item to see if it is a break. |
| 1100 | QCBORItem Peek; |
| 1101 | size_t uPeek = UsefulInputBuf_Tell(&(me->InBuf)); |
| 1102 | nReturn = GetNext_Item(&(me->InBuf), &Peek, NULL); |
| 1103 | if(nReturn) { |
| 1104 | goto Done; |
| 1105 | } |
| 1106 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
| 1107 | // It is not a break, rewind so it can be processed normally. |
| 1108 | UsefulInputBuf_Seek(&(me->InBuf), uPeek); |
| 1109 | break; |
| 1110 | } |
| 1111 | // It is a break. Ascend one nesting level. |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1112 | // The break is consumed. |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1113 | nReturn = DecodeNesting_BreakAscend(&(me->nesting)); |
| 1114 | if(nReturn) { |
| 1115 | // break occured outside of an indefinite length array/map |
| 1116 | goto Done; |
| 1117 | } |
| 1118 | } |
| 1119 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1120 | |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1121 | // Tell the caller what level is next. This tells them what maps/arrays |
| 1122 | // were closed out and makes it possible for them to reconstruct |
| 1123 | // the tree with just the information returned by GetNext |
| 1124 | pDecodedItem->uNextNestLevel = DecodeNesting_GetLevel(&(me->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1125 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1126 | Done: |
Laurence Lundblade | e9482dd | 2019-10-11 12:58:46 -0700 | [diff] [blame] | 1127 | if(nReturn != QCBOR_SUCCESS) { |
| 1128 | // Make sure uDataType and uLabelType are QCBOR_TYPE_NONE |
| 1129 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1130 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1131 | return nReturn; |
| 1132 | } |
| 1133 | |
| 1134 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1135 | /* |
| 1136 | Mostly just assign the right data type for the date string. |
| 1137 | */ |
| 1138 | inline static QCBORError DecodeDateString(QCBORItem *pDecodedItem) |
| 1139 | { |
| 1140 | // Stack Use: UsefulBuf 1 16 |
| 1141 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1142 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1143 | } |
| 1144 | |
| 1145 | const UsefulBufC Temp = pDecodedItem->val.string; |
| 1146 | pDecodedItem->val.dateString = Temp; |
| 1147 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_STRING; |
| 1148 | return QCBOR_SUCCESS; |
| 1149 | } |
| 1150 | |
| 1151 | |
| 1152 | /* |
| 1153 | Mostly just assign the right data type for the bignum. |
| 1154 | */ |
| 1155 | inline static QCBORError DecodeBigNum(QCBORItem *pDecodedItem) |
| 1156 | { |
| 1157 | // Stack Use: UsefulBuf 1 -- 16 |
| 1158 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 1159 | return QCBOR_ERR_BAD_OPT_TAG; |
| 1160 | } |
| 1161 | const UsefulBufC Temp = pDecodedItem->val.string; |
| 1162 | pDecodedItem->val.bigNum = Temp; |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1163 | const bool bIsPosBigNum = (bool)(pDecodedItem->uTagBits & QCBOR_TAGFLAG_POS_BIGNUM); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1164 | pDecodedItem->uDataType = (uint8_t)(bIsPosBigNum ? QCBOR_TYPE_POSBIGNUM |
| 1165 | : QCBOR_TYPE_NEGBIGNUM); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1166 | return QCBOR_SUCCESS; |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | /* |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1171 | The epoch formatted date. Turns lots of different forms of encoding |
| 1172 | date into uniform one |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1173 | */ |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1174 | static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1175 | { |
| 1176 | // Stack usage: 1 |
| 1177 | QCBORError nReturn = QCBOR_SUCCESS; |
| 1178 | |
| 1179 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
| 1180 | |
| 1181 | switch (pDecodedItem->uDataType) { |
| 1182 | |
| 1183 | case QCBOR_TYPE_INT64: |
| 1184 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 1185 | break; |
| 1186 | |
| 1187 | case QCBOR_TYPE_UINT64: |
| 1188 | if(pDecodedItem->val.uint64 > INT64_MAX) { |
| 1189 | nReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1190 | goto Done; |
| 1191 | } |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1192 | pDecodedItem->val.epochDate.nSeconds = (int64_t)pDecodedItem->val.uint64; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1193 | break; |
| 1194 | |
| 1195 | case QCBOR_TYPE_DOUBLE: |
| 1196 | { |
| 1197 | // This comparison needs to be done as a float before |
| 1198 | // conversion to an int64_t to be able to detect doubles |
| 1199 | // that are too large to fit into an int64_t. A double |
| 1200 | // has 52 bits of preceision. An int64_t has 63. Casting |
| 1201 | // INT64_MAX to a double actually causes a round up which |
| 1202 | // is bad and wrong for the comparison because it will |
| 1203 | // allow conversion of doubles that can't fit into a |
| 1204 | // uint64_t. To remedy this INT64_MAX - 0x7ff is used as |
| 1205 | // the cutoff point as if that rounds up in conversion to |
| 1206 | // double it will still be less than INT64_MAX. 0x7ff is |
| 1207 | // picked because it has 11 bits set. |
| 1208 | // |
| 1209 | // INT64_MAX seconds is on the order of 10 billion years, |
| 1210 | // and the earth is less than 5 billion years old, so for |
| 1211 | // most uses this conversion error won't occur even though |
| 1212 | // doubles can go much larger. |
| 1213 | // |
| 1214 | // Without the 0x7ff there is a ~30 minute range of time |
| 1215 | // values 10 billion years in the past and in the future |
| 1216 | // where this this code would go wrong. |
| 1217 | const double d = pDecodedItem->val.dfnum; |
| 1218 | if(d > (double)(INT64_MAX - 0x7ff)) { |
| 1219 | nReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 1220 | goto Done; |
| 1221 | } |
| 1222 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
| 1223 | pDecodedItem->val.epochDate.fSecondsFraction = d - (double)pDecodedItem->val.epochDate.nSeconds; |
| 1224 | } |
| 1225 | break; |
| 1226 | |
| 1227 | default: |
| 1228 | nReturn = QCBOR_ERR_BAD_OPT_TAG; |
| 1229 | goto Done; |
| 1230 | } |
| 1231 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 1232 | |
| 1233 | Done: |
| 1234 | return nReturn; |
| 1235 | } |
| 1236 | |
| 1237 | |
| 1238 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 1239 | /* |
| 1240 | Decode decimal fractions and big floats. |
| 1241 | |
| 1242 | When called pDecodedItem must be the array that is tagged as a big |
| 1243 | float or decimal fraction, the array that has the two members, the |
| 1244 | exponent and mantissa. |
| 1245 | |
| 1246 | This will fetch and decode the exponent and mantissa and put the |
| 1247 | result back into pDecodedItem. |
| 1248 | */ |
| 1249 | inline static QCBORError |
| 1250 | QCBORDecode_MantissaAndExponent(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
| 1251 | { |
| 1252 | QCBORError nReturn; |
| 1253 | |
| 1254 | // --- Make sure it is an array; track nesting level of members --- |
| 1255 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
| 1256 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1257 | goto Done; |
| 1258 | } |
| 1259 | |
| 1260 | // A check for pDecodedItem->val.uCount == 2 would work for |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1261 | // definite length arrays, but not for indefnite. Instead remember |
| 1262 | // the nesting level the two integers must be at, which is one |
| 1263 | // deeper than that of the array. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1264 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 1265 | |
| 1266 | // --- Is it a decimal fraction or a bigfloat? --- |
| 1267 | const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(me, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION); |
| 1268 | pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT; |
| 1269 | |
| 1270 | // --- Get the exponent --- |
| 1271 | QCBORItem exponentItem; |
| 1272 | nReturn = QCBORDecode_GetNextMapOrArray(me, &exponentItem, NULL); |
| 1273 | if(nReturn != QCBOR_SUCCESS) { |
| 1274 | goto Done; |
| 1275 | } |
| 1276 | if(exponentItem.uNestingLevel != nNestLevel) { |
| 1277 | // Array is empty or a map/array encountered when expecting an int |
| 1278 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1279 | goto Done; |
| 1280 | } |
| 1281 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
| 1282 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1283 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1284 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1285 | // will be too large for this to handle and thus an error that will |
| 1286 | // get handled in the next else. |
| 1287 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 1288 | } else { |
| 1289 | // Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1290 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1291 | goto Done; |
| 1292 | } |
| 1293 | |
| 1294 | // --- Get the mantissa --- |
| 1295 | QCBORItem mantissaItem; |
| 1296 | nReturn = QCBORDecode_GetNextWithTags(me, &mantissaItem, NULL); |
| 1297 | if(nReturn != QCBOR_SUCCESS) { |
| 1298 | goto Done; |
| 1299 | } |
| 1300 | if(mantissaItem.uNestingLevel != nNestLevel) { |
| 1301 | // Mantissa missing or map/array encountered when expecting number |
| 1302 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1303 | goto Done; |
| 1304 | } |
| 1305 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
| 1306 | // Data arriving as an unsigned int < INT64_MAX has been converted |
| 1307 | // to QCBOR_TYPE_INT64 and thus handled here. This is also means |
| 1308 | // that the only data arriving here of type QCBOR_TYPE_UINT64 data |
| 1309 | // will be too large for this to handle and thus an error that |
| 1310 | // will get handled in an else below. |
| 1311 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
| 1312 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 1313 | // Got a good big num mantissa |
| 1314 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
| 1315 | // Depends on numbering of QCBOR_TYPE_XXX |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1316 | pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType + |
| 1317 | mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM + |
| 1318 | 1); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1319 | } else { |
| 1320 | // Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX |
| 1321 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1322 | goto Done; |
| 1323 | } |
| 1324 | |
| 1325 | // --- Check that array only has the two numbers --- |
| 1326 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
| 1327 | // Extra items in the decimal fraction / big num |
| 1328 | nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
| 1329 | goto Done; |
| 1330 | } |
| 1331 | |
| 1332 | Done: |
| 1333 | |
| 1334 | return nReturn; |
| 1335 | } |
| 1336 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 1337 | |
| 1338 | |
| 1339 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1340 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1341 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1342 | QCBORError |
| 1343 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *me, |
| 1344 | QCBORItem *pDecodedItem, |
| 1345 | QCBORTagListOut *pTags) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1346 | { |
| 1347 | QCBORError nReturn; |
| 1348 | |
| 1349 | nReturn = QCBORDecode_GetNextMapOrArray(me, pDecodedItem, pTags); |
| 1350 | if(nReturn != QCBOR_SUCCESS) { |
| 1351 | goto Done; |
| 1352 | } |
| 1353 | |
| 1354 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 1355 | #define TAG_MAPPER_FIRST_XXX TAG_MAPPER_FIRST_SIX |
| 1356 | #else |
| 1357 | #define TAG_MAPPER_FIRST_XXX TAG_MAPPER_FIRST_FOUR |
| 1358 | #endif |
| 1359 | |
| 1360 | // Only pay attention to tags this code knows how to decode. |
| 1361 | switch(pDecodedItem->uTagBits & TAG_MAPPER_FIRST_XXX) { |
| 1362 | case 0: |
| 1363 | // No tags at all or none we know about. Nothing to do. |
| 1364 | // This is the pass-through path of this function |
| 1365 | // that will mostly be taken when decoding any item. |
| 1366 | break; |
| 1367 | |
| 1368 | case QCBOR_TAGFLAG_DATE_STRING: |
| 1369 | nReturn = DecodeDateString(pDecodedItem); |
| 1370 | break; |
| 1371 | |
| 1372 | case QCBOR_TAGFLAG_DATE_EPOCH: |
| 1373 | nReturn = DecodeDateEpoch(pDecodedItem); |
| 1374 | break; |
| 1375 | |
| 1376 | case QCBOR_TAGFLAG_POS_BIGNUM: |
| 1377 | case QCBOR_TAGFLAG_NEG_BIGNUM: |
| 1378 | nReturn = DecodeBigNum(pDecodedItem); |
| 1379 | break; |
| 1380 | |
| 1381 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 1382 | case QCBOR_TAGFLAG_DECIMAL_FRACTION: |
| 1383 | case QCBOR_TAGFLAG_BIGFLOAT: |
| 1384 | // For aggregate tagged types, what goes into pTags is only collected |
| 1385 | // from the surrounding data item, not the contents, so pTags is not |
| 1386 | // passed on here. |
| 1387 | |
| 1388 | nReturn = QCBORDecode_MantissaAndExponent(me, pDecodedItem); |
| 1389 | break; |
| 1390 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
| 1391 | |
| 1392 | default: |
| 1393 | // Encountering some mixed-up CBOR like something that |
| 1394 | // is tagged as both a string and integer date. |
| 1395 | nReturn = QCBOR_ERR_BAD_OPT_TAG; |
| 1396 | } |
| 1397 | |
| 1398 | Done: |
| 1399 | if(nReturn != QCBOR_SUCCESS) { |
| 1400 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 1401 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 1402 | } |
| 1403 | return nReturn; |
| 1404 | } |
| 1405 | |
| 1406 | |
| 1407 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1408 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1409 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1410 | QCBORError QCBORDecode_GetNext(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1411 | { |
| 1412 | return QCBORDecode_GetNextWithTags(me, pDecodedItem, NULL); |
| 1413 | } |
| 1414 | |
| 1415 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1416 | /* |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1417 | Decoding items is done in 5 layered functions, one calling the |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1418 | next one down. If a layer has no work to do for a particular item |
| 1419 | it returns quickly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1420 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1421 | - QCBORDecode_GetNext, GetNextWithTags -- The top layer processes |
| 1422 | tagged data items, turning them into the local C representation. |
| 1423 | For the most simple it is just associating a QCBOR_TYPE with the data. For |
| 1424 | the complex ones that an aggregate of data items, there is some further |
| 1425 | decoding and a little bit of recursion. |
| 1426 | |
| 1427 | - QCBORDecode_GetNextMapOrArray - This manages the beginnings and |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1428 | ends of maps and arrays. It tracks descending into and ascending |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1429 | out of maps/arrays. It processes all breaks that terminate |
| 1430 | maps and arrays. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1431 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1432 | - GetNext_MapEntry -- This handles the combining of two |
| 1433 | items, the label and the data, that make up a map entry. |
| 1434 | It only does work on maps. It combines the label and data |
| 1435 | items into one labeled item. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1436 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1437 | - GetNext_TaggedItem -- This decodes type 6 tagging. It turns the |
| 1438 | tags into bit flags associated with the data item. No actual decoding |
| 1439 | of the contents of the tagged item is performed here. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1440 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1441 | - GetNext_FullItem -- This assembles the sub-items that make up |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1442 | an indefinte length string into one string item. It uses the |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1443 | string allocater to create contiguous space for the item. It |
| 1444 | processes all breaks that are part of indefinite length strings. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1445 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 1446 | - GetNext_Item -- This decodes the atomic data items in CBOR. Each |
| 1447 | atomic data item has a "major type", an integer "argument" and optionally |
| 1448 | some content. For text and byte strings, the content is the bytes |
| 1449 | that make up the string. These are the smallest data items that are |
| 1450 | considered to be well-formed. The content may also be other data items in |
| 1451 | the case of aggregate types. They are not handled in this layer. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1452 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1453 | Roughly this takes 300 bytes of stack for vars. Need to |
| 1454 | evaluate this more carefully and correctly. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1455 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1456 | */ |
| 1457 | |
| 1458 | |
| 1459 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1460 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1461 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1462 | int QCBORDecode_IsTagged(QCBORDecodeContext *me, |
| 1463 | const QCBORItem *pItem, |
| 1464 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1465 | { |
| 1466 | const QCBORTagListIn *pCallerConfiguredTagMap = me->pCallerConfiguredTagList; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1467 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1468 | uint8_t uTagBitIndex; |
| 1469 | // Do not care about errors in pCallerConfiguredTagMap here. They are |
| 1470 | // caught during GetNext() before this is called. |
| 1471 | if(TagMapper_Lookup(pCallerConfiguredTagMap, uTag, &uTagBitIndex)) { |
| 1472 | return 0; |
| 1473 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1474 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1475 | const uint64_t uTagBit = 0x01ULL << uTagBitIndex; |
| 1476 | return (uTagBit & pItem->uTagBits) != 0; |
| 1477 | } |
| 1478 | |
| 1479 | |
| 1480 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1481 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1482 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1483 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *me) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1484 | { |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1485 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1486 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1487 | // Error out if all the maps/arrays are not closed out |
| 1488 | if(DecodeNesting_IsNested(&(me->nesting))) { |
| 1489 | nReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN; |
| 1490 | goto Done; |
| 1491 | } |
| 1492 | |
| 1493 | // Error out if not all the bytes are consumed |
| 1494 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) { |
| 1495 | nReturn = QCBOR_ERR_EXTRA_BYTES; |
| 1496 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1497 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1498 | Done: |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1499 | // Call the destructor for the string allocator if there is one. |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1500 | // Always called, even if there are errors; always have to clean up |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1501 | StringAllocator_Destruct(&(me->StringAllocator)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1502 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1503 | return nReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | |
| 1507 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1508 | /* |
| 1509 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1510 | Decoder errors handled in this file |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1511 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1512 | - Hit end of input before it was expected while decoding type and |
| 1513 | number QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1514 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1515 | - negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1516 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1517 | - Hit end of input while decoding a text or byte string |
| 1518 | QCBOR_ERR_HIT_END |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1519 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1520 | - Encountered conflicting tags -- e.g., an item is tagged both a date |
| 1521 | string and an epoch date QCBOR_ERR_UNSUPPORTED |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1522 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1523 | - Encontered an array or mapp that has too many items |
| 1524 | QCBOR_ERR_ARRAY_TOO_LONG |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1525 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1526 | - Encountered array/map nesting that is too deep |
| 1527 | QCBOR_ERR_ARRAY_NESTING_TOO_DEEP |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1528 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1529 | - An epoch date > INT64_MAX or < INT64_MIN was encountered |
| 1530 | QCBOR_ERR_DATE_OVERFLOW |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1531 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1532 | - The type of a map label is not a string or int |
| 1533 | QCBOR_ERR_MAP_LABEL_TYPE |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1534 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1535 | - Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1536 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1537 | */ |
| 1538 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1539 | |
| 1540 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1541 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1542 | /* =========================================================================== |
| 1543 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1544 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1545 | This implements a simple sting allocator for indefinite length |
| 1546 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 1547 | implements the function type QCBORStringAllocate and allows easy |
| 1548 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1549 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1550 | This particular allocator is built-in for convenience. The caller |
| 1551 | can implement their own. All of this following code will get |
| 1552 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 1553 | |
| 1554 | This is a very primitive memory allocator. It does not track |
| 1555 | individual allocations, only a high-water mark. A free or |
| 1556 | reallocation must be of the last chunk allocated. |
| 1557 | |
| 1558 | The size of the pool and offset to free memory are packed into the |
| 1559 | first 8 bytes of the memory pool so we don't have to keep them in |
| 1560 | the decode context. Since the address of the pool may not be |
| 1561 | aligned, they have to be packed and unpacked as if they were |
| 1562 | serialized data of the wire or such. |
| 1563 | |
| 1564 | The sizes packed in are uint32_t to be the same on all CPU types |
| 1565 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1566 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1567 | |
| 1568 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1569 | static inline int |
| 1570 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1571 | { |
| 1572 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 1573 | UsefulInputBuf UIB; |
| 1574 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1575 | // Just assume the size here. It was checked during SetUp so |
| 1576 | // the assumption is safe. |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1577 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem, QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
| 1578 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 1579 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 1580 | return UsefulInputBuf_GetError(&UIB); |
| 1581 | } |
| 1582 | |
| 1583 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1584 | static inline int |
| 1585 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1586 | { |
| 1587 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 1588 | // length check performed here is useful. |
| 1589 | UsefulOutBuf UOB; |
| 1590 | |
| 1591 | UsefulOutBuf_Init(&UOB, Pool); |
| 1592 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 1593 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 1594 | return UsefulOutBuf_GetError(&UOB); |
| 1595 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1596 | |
| 1597 | |
| 1598 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1599 | Internal function for an allocation, reallocation free and destuct. |
| 1600 | |
| 1601 | Having only one function rather than one each per mode saves space in |
| 1602 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1603 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1604 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 1605 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1606 | static UsefulBuf |
| 1607 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1608 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1609 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1610 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1611 | uint32_t uPoolSize; |
| 1612 | uint32_t uFreeOffset; |
| 1613 | |
| 1614 | if(uNewSize > UINT32_MAX) { |
| 1615 | // This allocator is only good up to 4GB. This check should |
| 1616 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 1617 | goto Done; |
| 1618 | } |
| 1619 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 1620 | |
| 1621 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 1622 | goto Done; |
| 1623 | } |
| 1624 | |
| 1625 | if(uNewSize) { |
| 1626 | if(pMem) { |
| 1627 | // REALLOCATION MODE |
| 1628 | // Calculate pointer to the end of the memory pool. It is |
| 1629 | // assumed that pPool + uPoolSize won't wrap around by |
| 1630 | // assuming the caller won't pass a pool buffer in that is |
| 1631 | // not in legitimate memory space. |
| 1632 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 1633 | |
| 1634 | // Check that the pointer for reallocation is in the range of the |
| 1635 | // pool. This also makes sure that pointer math further down |
| 1636 | // doesn't wrap under or over. |
| 1637 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 1638 | // Offset to start of chunk for reallocation. This won't |
| 1639 | // wrap under because of check that pMem >= pPool. Cast |
| 1640 | // is safe because the pool is always less than UINT32_MAX |
| 1641 | // because of check in QCBORDecode_SetMemPool(). |
| 1642 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 1643 | |
| 1644 | // Check to see if the allocation will fit. uPoolSize - |
| 1645 | // uMemOffset will not wrap under because of check that |
| 1646 | // pMem is in the range of the uPoolSize by check above. |
| 1647 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 1648 | ReturnValue.ptr = pMem; |
| 1649 | ReturnValue.len = uNewSize; |
| 1650 | |
| 1651 | // Addition won't wrap around over because uNewSize was |
| 1652 | // checked to be sure it is less than the pool size. |
| 1653 | uFreeOffset = uMemOffset + uNewSize32; |
| 1654 | } |
| 1655 | } |
| 1656 | } else { |
| 1657 | // ALLOCATION MODE |
| 1658 | // uPoolSize - uFreeOffset will not underflow because this |
| 1659 | // pool implementation makes sure uFreeOffset is always |
| 1660 | // smaller than uPoolSize through this check here and |
| 1661 | // reallocation case. |
| 1662 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 1663 | ReturnValue.len = uNewSize; |
| 1664 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 1665 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1666 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1667 | } |
| 1668 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1669 | if(pMem) { |
| 1670 | // FREE MODE |
| 1671 | // Cast is safe because of limit on pool size in |
| 1672 | // QCBORDecode_SetMemPool() |
| 1673 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 1674 | } else { |
| 1675 | // DESTRUCT MODE |
| 1676 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1677 | } |
| 1678 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1679 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1680 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 1681 | MemPool_Pack(Pool, uFreeOffset); |
| 1682 | |
| 1683 | Done: |
| 1684 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1687 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1688 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 1689 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1690 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1691 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 1692 | UsefulBuf Pool, |
| 1693 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1694 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1695 | // The pool size and free mem offset are packed into the beginning |
| 1696 | // of the pool memory. This compile time check make sure the |
| 1697 | // constant in the header is correct. This check should optimize |
| 1698 | // down to nothing. |
| 1699 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1700 | return QCBOR_ERR_BUFFER_TOO_SMALL; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1701 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1702 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1703 | // The pool size and free offset packed in to the beginning of pool |
| 1704 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 1705 | // machines. |
| 1706 | if(Pool.len > UINT32_MAX) { |
| 1707 | return QCBOR_ERR_BUFFER_TOO_LARGE; |
| 1708 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1709 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1710 | // This checks that the pool buffer given is big enough. |
| 1711 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
| 1712 | return QCBOR_ERR_BUFFER_TOO_SMALL; |
| 1713 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1714 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1715 | pMe->StringAllocator.pfAllocator = MemPool_Function; |
| 1716 | pMe->StringAllocator.pAllocateCxt = Pool.ptr; |
| 1717 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1718 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1719 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1720 | } |