Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 2 | Copyright (c) 2016-2018, The Linux Foundation. |
| 3 | Copyright (c) 2018, Laurence Lundblade. |
| 4 | All rights reserved. |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [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 | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 19 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 20 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 21 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 23 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 24 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 29 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 30 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 31 | ==============================================================================*/ |
| 32 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 33 | /*=================================================================================== |
| 34 | FILE: qcbor_decode.c |
| 35 | |
| 36 | DESCRIPTION: This file contains the implementation of QCBOR. |
| 37 | |
| 38 | EDIT HISTORY FOR FILE: |
| 39 | |
| 40 | This section contains comments describing changes made to the module. |
| 41 | Notice that changes are listed in reverse chronological order. |
| 42 | |
| 43 | when who what, where, why |
| 44 | -------- ---- --------------------------------------------------- |
Laurence Lundblade | 8b06e2e | 2018-12-04 12:26:51 +0900 | [diff] [blame] | 45 | 11/9/18 llundblade Error codes are now enums. |
| 46 | 11/2/18 llundblade Simplify float decoding and align with preferred |
| 47 | float encoding |
| 48 | 10/31/18 llundblade Switch to one license that is almost BSD-3. |
| 49 | 10/28/18 llundblade Reworked tag decoding |
| 50 | 10/15/18 llundblade Indefinite length maps and arrays supported |
| 51 | 10/8/18 llundblade Indefinite length strings supported |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 52 | 02/04/17 llundbla Work on CPUs that don's require pointer alignment |
| 53 | by making use of changes in UsefulBuf |
| 54 | 03/01/17 llundbla More data types; decoding improvements and fixes |
| 55 | 11/13/16 llundbla Integrate most TZ changes back into github version. |
| 56 | 09/30/16 gkanike Porting to TZ. |
| 57 | 03/15/16 llundbla Initial Version. |
| 58 | |
| 59 | =====================================================================================*/ |
| 60 | |
| 61 | #include "qcbor.h" |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 62 | #include "ieee754.h" |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 63 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 64 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 65 | /* |
| 66 | This casts away the const-ness of a pointer, usually so it can be |
| 67 | freed or realloced. |
| 68 | */ |
| 69 | #define UNCONST_POINTER(ptr) ((void *)(ptr)) |
| 70 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 71 | |
| 72 | /* |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 73 | Collection of functions to track the map/array nesting for decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 74 | */ |
| 75 | |
| 76 | inline static int IsMapOrArray(uint8_t uDataType) |
| 77 | { |
| 78 | return uDataType == QCBOR_TYPE_MAP || uDataType == QCBOR_TYPE_ARRAY; |
| 79 | } |
| 80 | |
| 81 | inline static int DecodeNesting_IsNested(const QCBORDecodeNesting *pNesting) |
| 82 | { |
| 83 | return pNesting->pCurrent != &(pNesting->pMapsAndArrays[0]); |
| 84 | } |
| 85 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 86 | inline static int DecodeNesting_IsIndefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 87 | { |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 88 | return pNesting->pCurrent->uCount == UINT16_MAX; |
| 89 | } |
| 90 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 91 | inline static uint8_t DecodeNesting_GetLevel(QCBORDecodeNesting *pNesting) |
| 92 | { |
| 93 | return pNesting->pCurrent - &(pNesting->pMapsAndArrays[0]); |
| 94 | } |
| 95 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 96 | inline static int DecodeNesting_TypeIsMap(const QCBORDecodeNesting *pNesting) |
| 97 | { |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 98 | if(!DecodeNesting_IsNested(pNesting)) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 99 | return 0; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 100 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 101 | |
| 102 | return CBOR_MAJOR_TYPE_MAP == pNesting->pCurrent->uMajorType; |
| 103 | } |
| 104 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 105 | // Process a break. This will either ascend the nesting or error out |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 106 | inline static QCBORError DecodeNesting_BreakAscend(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 107 | { |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 108 | // breaks must always occur when there is nesting |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 109 | if(!DecodeNesting_IsNested(pNesting)) { |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 110 | return QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 113 | // breaks can only occur when the map/array is indefinite length |
| 114 | if(!DecodeNesting_IsIndefiniteLength(pNesting)) { |
| 115 | return QCBOR_ERR_BAD_BREAK; |
| 116 | } |
| 117 | |
| 118 | // if all OK, the break reduces the level of nesting |
| 119 | pNesting->pCurrent--; |
| 120 | |
| 121 | return QCBOR_SUCCESS; |
| 122 | } |
| 123 | |
| 124 | // Called on every single item except breaks including the opening of a map/array |
| 125 | inline static void DecodeNesting_DecrementCount(QCBORDecodeNesting *pNesting) |
| 126 | { |
| 127 | if(!DecodeNesting_IsNested(pNesting)) { |
| 128 | // at top level where there is no tracking |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | if(DecodeNesting_IsIndefiniteLength(pNesting)) { |
| 133 | // There is no count for indefinite length arrays/maps |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | // Decrement the count of items in this array/map |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 138 | pNesting->pCurrent->uCount--; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 139 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 140 | // Pop up nesting levels if the counts at the levels are zero |
| 141 | while(DecodeNesting_IsNested(pNesting) && 0 == pNesting->pCurrent->uCount) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 142 | pNesting->pCurrent--; |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 143 | if(!DecodeNesting_IsIndefiniteLength(pNesting)) { |
| 144 | pNesting->pCurrent->uCount--; |
| 145 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 149 | // Called on every map/array |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 150 | inline static QCBORError DecodeNesting_Descend(QCBORDecodeNesting *pNesting, QCBORItem *pItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 151 | { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 152 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 153 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 154 | if(pItem->val.uCount == 0) { |
| 155 | // Nothing to do for empty definite lenth arrays. They are just are |
| 156 | // effectively the same as an item that is not a map or array |
| 157 | goto Done; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 158 | // Empty indefinite length maps and arrays are handled elsewhere |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Error out if arrays is too long to handle |
| 162 | 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] | 163 | nReturn = QCBOR_ERR_ARRAY_TOO_LONG; |
| 164 | goto Done; |
| 165 | } |
| 166 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 167 | // Error out if nesting is too deep |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 168 | if(pNesting->pCurrent >= &(pNesting->pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING])) { |
| 169 | nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP; |
| 170 | goto Done; |
| 171 | } |
| 172 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 173 | // The actual descend |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 174 | pNesting->pCurrent++; |
| 175 | |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 176 | // Record a few details for this nesting level |
| 177 | pNesting->pCurrent->uMajorType = pItem->uDataType; |
| 178 | pNesting->pCurrent->uCount = pItem->val.uCount; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 179 | |
| 180 | Done: |
| 181 | return nReturn;; |
| 182 | } |
| 183 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 184 | inline static void DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
| 185 | { |
| 186 | pNesting->pCurrent = &(pNesting->pMapsAndArrays[0]); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 191 | /* |
| 192 | This list of built-in tags. Only add tags here that are |
| 193 | clearly established and useful. Once a tag is added here |
| 194 | it can't be taken out as that would break backwards compatibility. |
| 195 | There are only 48 slots available forever. |
| 196 | */ |
| 197 | static const uint16_t spBuiltInTagMap[] = { |
| 198 | CBOR_TAG_DATE_STRING, // See TAG_MAPPER_FIRST_FOUR |
| 199 | CBOR_TAG_DATE_EPOCH, // See TAG_MAPPER_FIRST_FOUR |
| 200 | CBOR_TAG_POS_BIGNUM, // See TAG_MAPPER_FIRST_FOUR |
| 201 | CBOR_TAG_NEG_BIGNUM, // See TAG_MAPPER_FIRST_FOUR |
| 202 | CBOR_TAG_FRACTION, |
| 203 | CBOR_TAG_BIGFLOAT, |
| 204 | CBOR_TAG_COSE_ENCRYPTO, |
| 205 | CBOR_TAG_COSE_MAC0, |
| 206 | CBOR_TAG_COSE_SIGN1, |
| 207 | CBOR_TAG_ENC_AS_B64URL, |
| 208 | CBOR_TAG_ENC_AS_B64, |
| 209 | CBOR_TAG_ENC_AS_B16, |
| 210 | CBOR_TAG_CBOR, |
| 211 | CBOR_TAG_URI, |
| 212 | CBOR_TAG_B64URL, |
| 213 | CBOR_TAG_B64, |
| 214 | CBOR_TAG_REGEX, |
| 215 | CBOR_TAG_MIME, |
| 216 | CBOR_TAG_BIN_UUID, |
| 217 | CBOR_TAG_CWT, |
| 218 | CBOR_TAG_ENCRYPT, |
| 219 | CBOR_TAG_MAC, |
| 220 | CBOR_TAG_SIGN, |
| 221 | CBOR_TAG_GEO_COORD, |
| 222 | CBOR_TAG_CBOR_MAGIC |
| 223 | }; |
| 224 | |
| 225 | // This is used in a bit of cleverness in GetNext_TaggedItem() to |
| 226 | // keep code size down and switch for the internal processing of |
| 227 | // these types. This will break if the first four items in |
| 228 | // spBuiltInTagMap don't have values 0,1,2,3. That is the |
| 229 | // mapping is 0 to 0, 1 to 1, 2 to 2 and 3 to 3. |
| 230 | #define QCBOR_TAGFLAG_DATE_STRING (0x01LL << CBOR_TAG_DATE_STRING) |
| 231 | #define QCBOR_TAGFLAG_DATE_EPOCH (0x01LL << CBOR_TAG_DATE_EPOCH) |
| 232 | #define QCBOR_TAGFLAG_POS_BIGNUM (0x01LL << CBOR_TAG_POS_BIGNUM) |
| 233 | #define QCBOR_TAGFLAG_NEG_BIGNUM (0x01LL << CBOR_TAG_NEG_BIGNUM) |
| 234 | |
| 235 | #define TAG_MAPPER_FIRST_FOUR (QCBOR_TAGFLAG_DATE_STRING |\ |
| 236 | QCBOR_TAGFLAG_DATE_EPOCH |\ |
| 237 | QCBOR_TAGFLAG_POS_BIGNUM |\ |
| 238 | QCBOR_TAGFLAG_NEG_BIGNUM) |
| 239 | |
| 240 | #define TAG_MAPPER_TOTAL_TAG_BITS 64 // Number of bits in a uint64_t |
| 241 | #define TAG_MAPPER_CUSTOM_TAGS_BASE_INDEX (TAG_MAPPER_TOTAL_TAG_BITS - QCBOR_MAX_CUSTOM_TAGS) // 48 |
| 242 | #define TAG_MAPPER_MAX_SIZE_BUILT_IN_TAGS (TAG_MAPPER_TOTAL_TAG_BITS - QCBOR_MAX_CUSTOM_TAGS ) // 48 |
| 243 | |
| 244 | static inline int TagMapper_LookupBuiltIn(uint64_t uTag) |
| 245 | { |
| 246 | if(sizeof(spBuiltInTagMap)/sizeof(uint16_t) > TAG_MAPPER_MAX_SIZE_BUILT_IN_TAGS) { |
| 247 | // This is a cross-check to make sure the above array doesn't |
| 248 | // accidentally get made too big. |
| 249 | // In normal conditions the above test should optimize out |
| 250 | // as all the values are known at compile time. |
| 251 | return -1; |
| 252 | } |
| 253 | |
| 254 | if(uTag > UINT16_MAX) { |
| 255 | // This tag map works only on 16-bit tags |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | for(int nTagBitIndex = 0; nTagBitIndex < (int)(sizeof(spBuiltInTagMap)/sizeof(uint16_t)); nTagBitIndex++) { |
| 260 | if(spBuiltInTagMap[nTagBitIndex] == uTag) { |
| 261 | return nTagBitIndex; |
| 262 | } |
| 263 | } |
| 264 | return -1; // Indicates no match |
| 265 | } |
| 266 | |
| 267 | static inline int TagMapper_LookupCallerConfigured(const QCBORTagListIn *pCallerConfiguredTagMap, uint64_t uTag) |
| 268 | { |
| 269 | for(int nTagBitIndex = 0; nTagBitIndex < pCallerConfiguredTagMap->uNumTags; nTagBitIndex++) { |
| 270 | if(pCallerConfiguredTagMap->puTags[nTagBitIndex] == uTag) { |
| 271 | return nTagBitIndex + TAG_MAPPER_CUSTOM_TAGS_BASE_INDEX; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | return -1; // Indicates no match |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | Find the tag bit index for a given tag value, or error out |
| 280 | |
| 281 | This and the above functions could probably be optimized and made |
| 282 | clearer and neater. |
| 283 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 284 | static QCBORError TagMapper_Lookup(const QCBORTagListIn *pCallerConfiguredTagMap, uint64_t uTag, 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 | } |
| 292 | |
| 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 | } |
| 305 | |
| 306 | return QCBOR_ERR_BAD_OPT_TAG; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 311 | |
| 312 | /* |
| 313 | Public function, see header file |
| 314 | */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame^] | 315 | void QCBORDecode_Init(QCBORDecodeContext *me, UsefulBufC EncodedCBOR, QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 316 | { |
| 317 | memset(me, 0, sizeof(QCBORDecodeContext)); |
| 318 | UsefulInputBuf_Init(&(me->InBuf), EncodedCBOR); |
| 319 | // Don't bother with error check on decode mode. If a bad value is passed it will just act as |
| 320 | // if the default normal mode of 0 was set. |
| 321 | me->uDecodeMode = nDecodeMode; |
| 322 | DecodeNesting_Init(&(me->nesting)); |
| 323 | } |
| 324 | |
| 325 | |
| 326 | /* |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 327 | Public function, see header file |
| 328 | */ |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 329 | void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pCtx, const QCBORStringAllocator *pAllocator, bool bAllocAll) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 330 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 331 | pCtx->pStringAllocator = (void *)pAllocator; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 332 | pCtx->bStringAllocateAll = bAllocAll; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 335 | void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *me, const QCBORTagListIn *pTagList) |
| 336 | { |
| 337 | me->pCallerConfiguredTagList = pTagList; |
| 338 | } |
| 339 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 340 | |
| 341 | /* |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 342 | This decodes the fundamental part of a CBOR data item, the type and number |
| 343 | |
| 344 | This is the Counterpart to InsertEncodedTypeAndNumber(). |
| 345 | |
| 346 | This does the network->host byte order conversion. The conversion here |
| 347 | also results in the conversion for floats in addition to that for |
| 348 | lengths, tags and integer values. |
| 349 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 350 | This returns: |
| 351 | pnMajorType -- the major type for the item |
| 352 | puNumber -- the "number" which is used a the value for integers, tags and floats and length for strings and arrays |
| 353 | puAdditionalInfo -- Pass this along to know what kind of float or if length is indefinite |
| 354 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 355 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 356 | inline static QCBORError DecodeTypeAndNumber(UsefulInputBuf *pUInBuf, int *pnMajorType, uint64_t *puNumber, uint8_t *puAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 357 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 358 | // Stack usage: int/ptr 5 -- 40 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 359 | QCBORError nReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 360 | |
| 361 | // Get the initial byte that every CBOR data item has |
| 362 | const uint8_t InitialByte = UsefulInputBuf_GetByte(pUInBuf); |
| 363 | |
| 364 | // Break down the initial byte |
| 365 | const uint8_t uTmpMajorType = InitialByte >> 5; |
| 366 | const uint8_t uAdditionalInfo = InitialByte & 0x1f; |
| 367 | |
| 368 | // Get the integer that follows the major type. Do not know if this is a length, value, float or tag at this point |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 369 | // Also convert from network byte order. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 370 | uint64_t uTmpValue; |
| 371 | switch(uAdditionalInfo) { |
| 372 | |
| 373 | case LEN_IS_ONE_BYTE: |
| 374 | uTmpValue = UsefulInputBuf_GetByte(pUInBuf); |
| 375 | break; |
| 376 | |
| 377 | case LEN_IS_TWO_BYTES: |
| 378 | uTmpValue = UsefulInputBuf_GetUint16(pUInBuf); |
| 379 | break; |
| 380 | |
| 381 | case LEN_IS_FOUR_BYTES: |
| 382 | uTmpValue = UsefulInputBuf_GetUint32(pUInBuf); |
| 383 | break; |
| 384 | |
| 385 | case LEN_IS_EIGHT_BYTES: |
| 386 | uTmpValue = UsefulInputBuf_GetUint64(pUInBuf); |
| 387 | break; |
| 388 | |
| 389 | case ADDINFO_RESERVED1: // reserved by CBOR spec |
| 390 | case ADDINFO_RESERVED2: // reserved by CBOR spec |
| 391 | case ADDINFO_RESERVED3: // reserved by CBOR spec |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 392 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 393 | goto Done; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 394 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 395 | default: |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 396 | // This is when the "number" is in the additional info |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 397 | uTmpValue = uAdditionalInfo; |
| 398 | break; |
| 399 | } |
| 400 | |
| 401 | // If any of the UsefulInputBuf_Get calls fail we will get here with uTmpValue as 0. |
| 402 | // There is no harm in this. This following check takes care of catching all of |
| 403 | // these errors. |
| 404 | |
| 405 | if(UsefulInputBuf_GetError(pUInBuf)) { |
| 406 | nReturn = QCBOR_ERR_HIT_END; |
| 407 | goto Done; |
| 408 | } |
| 409 | |
| 410 | // All successful if we got here. |
| 411 | nReturn = QCBOR_SUCCESS; |
| 412 | *pnMajorType = uTmpMajorType; |
| 413 | *puNumber = uTmpValue; |
| 414 | *puAdditionalInfo = uAdditionalInfo; |
| 415 | |
| 416 | Done: |
| 417 | return nReturn; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | /* |
| 422 | CBOR doesn't explicitly specify two's compliment for integers but all CPUs |
| 423 | use it these days and the test vectors in the RFC are so. All integers in the CBOR |
| 424 | structure are positive and the major type indicates positive or negative. |
| 425 | CBOR can express positive integers up to 2^x - 1 where x is the number of bits |
| 426 | and negative integers down to 2^x. Note that negative numbers can be one |
| 427 | more away from zero than positive. |
| 428 | Stdint, as far as I can tell, uses two's compliment to represent |
| 429 | negative integers. |
| 430 | |
| 431 | See http://www.unix.org/whitepapers/64bit.html for reasons int isn't |
| 432 | used here in any way including in the interface |
| 433 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 434 | inline static QCBORError DecodeInteger(int nMajorType, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 435 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 436 | // Stack usage: int/ptr 1 -- 8 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 437 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 438 | |
| 439 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
| 440 | if (uNumber <= INT64_MAX) { |
| 441 | pDecodedItem->val.int64 = (int64_t)uNumber; |
| 442 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
| 443 | |
| 444 | } else { |
| 445 | pDecodedItem->val.uint64 = uNumber; |
| 446 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
| 447 | |
| 448 | } |
| 449 | } else { |
| 450 | if(uNumber <= INT64_MAX) { |
| 451 | pDecodedItem->val.int64 = -uNumber-1; |
| 452 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
| 453 | |
| 454 | } else { |
| 455 | // C can't represent a negative integer in this range |
| 456 | // so it is an error. todo -- test this condition |
| 457 | nReturn = QCBOR_ERR_INT_OVERFLOW; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return nReturn; |
| 462 | } |
| 463 | |
| 464 | // Make sure #define value line up as DecodeSimple counts on this. |
| 465 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 466 | #error QCBOR_TYPE_FALSE macro value wrong |
| 467 | #endif |
| 468 | |
| 469 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 470 | #error QCBOR_TYPE_TRUE macro value wrong |
| 471 | #endif |
| 472 | |
| 473 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 474 | #error QCBOR_TYPE_NULL macro value wrong |
| 475 | #endif |
| 476 | |
| 477 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 478 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 479 | #endif |
| 480 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 481 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 482 | #error QCBOR_TYPE_BREAK macro value wrong |
| 483 | #endif |
| 484 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 485 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 486 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 487 | #endif |
| 488 | |
| 489 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 490 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 491 | #endif |
| 492 | |
| 493 | /* |
| 494 | Decode true, false, floats, break... |
| 495 | */ |
| 496 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 497 | inline static QCBORError DecodeSimple(uint8_t uAdditionalInfo, uint64_t uNumber, QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 498 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 499 | // Stack usage: 0 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 500 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 501 | |
| 502 | // uAdditionalInfo is 5 bits from the initial byte |
| 503 | // compile time checks above make sure uAdditionalInfo values line up with uDataType values |
| 504 | pDecodedItem->uDataType = uAdditionalInfo; |
| 505 | |
| 506 | switch(uAdditionalInfo) { |
| 507 | case ADDINFO_RESERVED1: // 28 |
| 508 | case ADDINFO_RESERVED2: // 29 |
| 509 | case ADDINFO_RESERVED3: // 30 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 510 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 511 | break; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 512 | |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 513 | case HALF_PREC_FLOAT: |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 514 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uNumber); |
| 515 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 516 | break; |
Laurence Lundblade | cc2ed34 | 2018-09-22 17:29:55 -0700 | [diff] [blame] | 517 | case SINGLE_PREC_FLOAT: |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 518 | pDecodedItem->val.dfnum = (double)UsefulBufUtil_CopyUint32ToFloat((uint32_t)uNumber); |
| 519 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 520 | break; |
| 521 | case DOUBLE_PREC_FLOAT: |
| 522 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uNumber); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 523 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 524 | break; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 525 | |
| 526 | case CBOR_SIMPLEV_FALSE: // 20 |
| 527 | case CBOR_SIMPLEV_TRUE: // 21 |
| 528 | case CBOR_SIMPLEV_NULL: // 22 |
| 529 | case CBOR_SIMPLEV_UNDEF: // 23 |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 530 | case CBOR_SIMPLE_BREAK: // 31 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 531 | break; // nothing to do |
| 532 | |
| 533 | case CBOR_SIMPLEV_ONEBYTE: // 24 |
| 534 | if(uNumber <= CBOR_SIMPLE_BREAK) { |
| 535 | // This takes out f8 00 ... f8 1f which should be encoded as e0 … f7 |
| 536 | nReturn = QCBOR_ERR_INVALID_CBOR; |
| 537 | goto Done; |
| 538 | } |
| 539 | // fall through intentionally |
| 540 | |
| 541 | default: // 0-19 |
| 542 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
| 543 | // DecodeTypeAndNumber will make uNumber equal to uAdditionalInfo when uAdditionalInfo is < 24 |
| 544 | // This cast is safe because the 2, 4 and 8 byte lengths of uNumber are in the double/float cases above |
| 545 | pDecodedItem->val.uSimple = (uint8_t)uNumber; |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | Done: |
| 550 | return nReturn; |
| 551 | } |
| 552 | |
| 553 | |
| 554 | |
| 555 | /* |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 556 | Decode text and byte strings. Call the string allocator if asked to. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 557 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 558 | inline static QCBORError DecodeBytes(const QCBORStringAllocator *pAlloc, int nMajorType, uint64_t uStrLen, UsefulInputBuf *pUInBuf, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 559 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 560 | // Stack usage: UsefulBuf 2, int/ptr 1 40 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 561 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 562 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 563 | UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, uStrLen); |
| 564 | if(UsefulBuf_IsNULLC(Bytes)) { |
| 565 | // Failed to get the bytes for this string item |
| 566 | nReturn = QCBOR_ERR_HIT_END; |
| 567 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 568 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 569 | |
| 570 | if(pAlloc) { |
| 571 | // We are asked to use string allocator to make a copy |
| 572 | UsefulBuf NewMem = pAlloc->fAllocate(pAlloc->pAllocaterContext, NULL, uStrLen); |
| 573 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 574 | nReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 575 | goto Done; |
| 576 | } |
| 577 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
| 578 | } else { |
| 579 | // Normal case with no string allocator |
| 580 | pDecodedItem->val.string = Bytes; |
| 581 | } |
| 582 | pDecodedItem->uDataType = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING) ? QCBOR_TYPE_BYTE_STRING : QCBOR_TYPE_TEXT_STRING; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 583 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 584 | Done: |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 585 | return nReturn; |
| 586 | } |
| 587 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 588 | |
| 589 | /* |
| 590 | Mostly just assign the right data type for the date string. |
| 591 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 592 | inline static QCBORError DecodeDateString(QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 593 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 594 | // Stack Use: UsefulBuf 1 16 |
| 595 | if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 596 | return QCBOR_ERR_BAD_OPT_TAG; |
| 597 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 598 | |
| 599 | UsefulBufC Temp = pDecodedItem->val.string; |
| 600 | pDecodedItem->val.dateString = Temp; |
| 601 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_STRING; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 602 | return QCBOR_SUCCESS; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | /* |
| 607 | Mostly just assign the right data type for the bignum. |
| 608 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 609 | inline static QCBORError DecodeBigNum(QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 610 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 611 | // Stack Use: UsefulBuf 1 -- 16 |
| 612 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 613 | return QCBOR_ERR_BAD_OPT_TAG; |
| 614 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 615 | UsefulBufC Temp = pDecodedItem->val.string; |
| 616 | pDecodedItem->val.bigNum = Temp; |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 617 | pDecodedItem->uDataType = pDecodedItem->uTagBits & QCBOR_TAGFLAG_POS_BIGNUM ? QCBOR_TYPE_POSBIGNUM : QCBOR_TYPE_NEGBIGNUM; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 618 | return QCBOR_SUCCESS; |
| 619 | } |
| 620 | |
| 621 | |
| 622 | /* |
| 623 | The epoch formatted date. Turns lots of different forms of encoding date into uniform one |
| 624 | */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 625 | static int DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 626 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 627 | // Stack usage: 1 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 628 | QCBORError nReturn = QCBOR_SUCCESS; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 629 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 630 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
| 631 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 632 | switch (pDecodedItem->uDataType) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 633 | |
| 634 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 635 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 636 | break; |
| 637 | |
| 638 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 639 | if(pDecodedItem->val.uint64 > INT64_MAX) { |
| 640 | nReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 641 | goto Done; |
| 642 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 643 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.uint64; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 644 | break; |
| 645 | |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 646 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 647 | { |
| 648 | const double d = pDecodedItem->val.dfnum; |
| 649 | if(d > INT64_MAX) { |
| 650 | nReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 651 | goto Done; |
| 652 | } |
| 653 | pDecodedItem->val.epochDate.nSeconds = d; // Float to integer conversion happening here. |
| 654 | pDecodedItem->val.epochDate.fSecondsFraction = d - pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 655 | } |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 656 | break; |
| 657 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 658 | default: |
| 659 | nReturn = QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 660 | goto Done; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 661 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 662 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 663 | |
| 664 | Done: |
| 665 | return nReturn; |
| 666 | } |
| 667 | |
| 668 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 669 | |
| 670 | |
| 671 | // Make sure the constants align as this is assumed by the GetAnItem() implementation |
| 672 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 673 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 674 | #endif |
| 675 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 676 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 677 | #endif |
| 678 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 679 | /* |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 680 | This gets a single data item and decodes it including preceding optional tagging. This does not |
| 681 | deal with arrays and maps and nesting except to decode the data item introducing them. Arrays and |
| 682 | maps are handled at the next level up in GetNext(). |
| 683 | |
| 684 | Errors detected here include: an array that is too long to decode, hit end of buffer unexpectedly, |
| 685 | a few forms of invalid encoded CBOR |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 686 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 687 | static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf, QCBORItem *pDecodedItem, const QCBORStringAllocator *pAlloc) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 688 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 689 | // Stack usage: int/ptr 3 -- 24 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 690 | QCBORError nReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 691 | |
| 692 | // Get the major type and the number. Number could be length of more bytes or the value depending on the major type |
| 693 | // nAdditionalInfo is an encoding of the length of the uNumber and is needed to decode floats and doubles |
| 694 | int uMajorType; |
| 695 | uint64_t uNumber; |
| 696 | uint8_t uAdditionalInfo; |
| 697 | |
| 698 | nReturn = DecodeTypeAndNumber(pUInBuf, &uMajorType, &uNumber, &uAdditionalInfo); |
| 699 | |
| 700 | // Error out here if we got into trouble on the type and number. |
| 701 | // The code after this will not work if the type and number is not good. |
| 702 | if(nReturn) |
| 703 | goto Done; |
| 704 | |
Laurence Lundblade | fab1b52 | 2018-10-19 13:40:52 +0530 | [diff] [blame] | 705 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 706 | |
| 707 | // At this point the major type and the value are valid. We've got the type and the number that |
| 708 | // starts every CBOR data item. |
| 709 | switch (uMajorType) { |
| 710 | case CBOR_MAJOR_TYPE_POSITIVE_INT: // Major type 0 |
| 711 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: // Major type 1 |
| 712 | nReturn = DecodeInteger(uMajorType, uNumber, pDecodedItem); |
| 713 | break; |
| 714 | |
| 715 | case CBOR_MAJOR_TYPE_BYTE_STRING: // Major type 2 |
| 716 | case CBOR_MAJOR_TYPE_TEXT_STRING: // Major type 3 |
| 717 | if(uAdditionalInfo == LEN_IS_INDEFINITE) { |
| 718 | pDecodedItem->uDataType = (uMajorType == CBOR_MAJOR_TYPE_BYTE_STRING) ? QCBOR_TYPE_BYTE_STRING : QCBOR_TYPE_TEXT_STRING; |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 719 | pDecodedItem->val.string = (UsefulBufC){NULL, SIZE_MAX}; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 720 | } else { |
| 721 | nReturn = DecodeBytes(pAlloc, uMajorType, uNumber, pUInBuf, pDecodedItem); |
| 722 | } |
| 723 | break; |
| 724 | |
| 725 | case CBOR_MAJOR_TYPE_ARRAY: // Major type 4 |
| 726 | case CBOR_MAJOR_TYPE_MAP: // Major type 5 |
| 727 | // Record the number of items in the array or map |
| 728 | if(uNumber > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 729 | nReturn = QCBOR_ERR_ARRAY_TOO_LONG; |
| 730 | goto Done; |
| 731 | } |
| 732 | if(uAdditionalInfo == LEN_IS_INDEFINITE) { |
Laurence Lundblade | a44d506 | 2018-10-17 18:45:12 +0530 | [diff] [blame] | 733 | pDecodedItem->val.uCount = UINT16_MAX; // Indicate indefinite length |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 734 | } else { |
| 735 | pDecodedItem->val.uCount = (uint16_t)uNumber; // type conversion OK because of check above |
| 736 | } |
| 737 | pDecodedItem->uDataType = uMajorType; // C preproc #if above makes sure constants align |
| 738 | break; |
| 739 | |
| 740 | case CBOR_MAJOR_TYPE_OPTIONAL: // Major type 6, optional prepended tags |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 741 | pDecodedItem->val.uTagV = uNumber; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 742 | pDecodedItem->uDataType = QCBOR_TYPE_OPTTAG; |
| 743 | break; |
| 744 | |
| 745 | case CBOR_MAJOR_TYPE_SIMPLE: // Major type 7, float, double, true, false, null... |
| 746 | nReturn = DecodeSimple(uAdditionalInfo, uNumber, pDecodedItem); |
| 747 | break; |
| 748 | |
| 749 | default: // Should never happen because DecodeTypeAndNumber() should never return > 7 |
| 750 | nReturn = QCBOR_ERR_UNSUPPORTED; |
| 751 | break; |
| 752 | } |
| 753 | |
| 754 | Done: |
| 755 | return nReturn; |
| 756 | } |
| 757 | |
| 758 | |
| 759 | |
| 760 | /* |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 761 | This layer deals with indefinite length strings. It pulls all the |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 762 | individual chunk items together into one QCBORItem using the |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 763 | string allocator. |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 764 | |
| 765 | Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 766 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 767 | static inline QCBORError GetNext_FullItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 768 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 769 | // Stack usage; int/ptr 2 UsefulBuf 2 QCBORItem -- 96 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 770 | QCBORError nReturn; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 771 | QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)me->pStringAllocator; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 772 | UsefulBufC FullString = NULLUsefulBufC; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 773 | |
| 774 | nReturn = GetNext_Item(&(me->InBuf), pDecodedItem, me->bStringAllocateAll ? pAlloc: NULL); |
| 775 | if(nReturn) { |
| 776 | goto Done; |
| 777 | } |
| 778 | |
| 779 | // To reduce code size by removing support for indefinite length strings, the |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 780 | // code in this function from here down can be eliminated. Run tests, except |
| 781 | // indefinite length string tests, to be sure all is OK if this is removed. |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 782 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 783 | // Only do indefinite length processing on strings |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 784 | if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING && pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 785 | goto Done; // no need to do any work here on non-string types |
| 786 | } |
| 787 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 788 | // Is this a string with an indefinite length? |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 789 | if(pDecodedItem->val.string.len != SIZE_MAX) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 790 | goto Done; // length is not indefinite, so no work to do here |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 791 | } |
| 792 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 793 | // Can't do indefinite length strings without a string allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 794 | if(pAlloc == NULL) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 795 | nReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 796 | goto Done; |
| 797 | } |
| 798 | |
| 799 | // There is an indefinite length string to work on... |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 800 | // Track which type of string it is |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 801 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 802 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 803 | // Loop getting chunk of indefinite string |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 804 | for(;;) { |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 805 | // Get item for next chunk |
| 806 | QCBORItem StringChunkItem; |
| 807 | // NULL passed to never string alloc chunk of indefinite length strings |
| 808 | nReturn = GetNext_Item(&(me->InBuf), &StringChunkItem, NULL); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 809 | if(nReturn) { |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 810 | break; // Error getting the next chunk |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 811 | } |
| 812 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 813 | // See if it is a marker at end of indefinite length string |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 814 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 815 | // String is complete |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 816 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 817 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 818 | break; |
| 819 | } |
| 820 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 821 | // Match data type of chunk to type at beginning. |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 822 | // Also catches error of other non-string types that don't belong. |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 823 | if(StringChunkItem.uDataType != uStringType) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 824 | nReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 825 | break; |
| 826 | } |
| 827 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 828 | // Alloc new buffer or expand previously allocated buffer so it can fit |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 829 | UsefulBuf NewMem = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, |
| 830 | UNCONST_POINTER(FullString.ptr), |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 831 | FullString.len + StringChunkItem.val.string.len); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 832 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 833 | // Allocation of memory for the string failed |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 834 | nReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 835 | break; |
| 836 | } |
| 837 | |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 838 | // Copy new string chunk at the end of string so far. |
| 839 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | Done: |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 843 | if(pAlloc && nReturn && !UsefulBuf_IsNULLC(FullString)) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 844 | // Getting item failed, clean up the allocated memory |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 845 | (pAlloc->fFree)(pAlloc->pAllocaterContext, UNCONST_POINTER(FullString.ptr)); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | return nReturn; |
| 849 | } |
| 850 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 851 | |
| 852 | /* |
| 853 | Returns an error if there was something wrong with the optional item or it couldn't |
| 854 | be handled. |
| 855 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 856 | static QCBORError GetNext_TaggedItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem, QCBORTagListOut *pTags) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 857 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 858 | // Stack usage: int/ptr: 3 -- 24 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 859 | QCBORError nReturn; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 860 | uint64_t uTagBits = 0; |
| 861 | if(pTags) { |
| 862 | pTags->uNumUsed = 0; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 863 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 864 | |
| 865 | for(;;) { |
| 866 | nReturn = GetNext_FullItem(me, pDecodedItem); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 867 | if(nReturn) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 868 | goto Done; // Error out of the loop |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 871 | if(pDecodedItem->uDataType != QCBOR_TYPE_OPTTAG) { |
| 872 | // Successful exit from loop; maybe got some tags, maybe not |
| 873 | pDecodedItem->uTagBits = uTagBits; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 874 | break; |
| 875 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 876 | |
| 877 | uint8_t uTagBitIndex; |
| 878 | // Tag was mapped, tag was not mapped, error with tag list |
| 879 | switch(TagMapper_Lookup(me->pCallerConfiguredTagList, pDecodedItem->val.uTagV, &uTagBitIndex)) { |
| 880 | |
| 881 | case QCBOR_SUCCESS: |
| 882 | // Successfully mapped the tag |
| 883 | uTagBits |= 0x01ULL << uTagBitIndex; |
| 884 | break; |
| 885 | |
| 886 | case QCBOR_ERR_BAD_OPT_TAG: |
| 887 | // Tag is not recognized. Do nothing |
| 888 | break; |
| 889 | |
| 890 | default: |
| 891 | // Error Condition |
| 892 | goto Done; |
| 893 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 894 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 895 | if(pTags) { |
| 896 | // Caller wants all tags recorded in the provided buffer |
| 897 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 898 | nReturn = QCBOR_ERR_TOO_MANY_TAGS; |
| 899 | goto Done; |
| 900 | } |
| 901 | pTags->puTags[pTags->uNumUsed] = pDecodedItem->val.uTagV; |
| 902 | pTags->uNumUsed++; |
| 903 | } |
| 904 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 905 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 906 | switch(pDecodedItem->uTagBits & TAG_MAPPER_FIRST_FOUR) { |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 907 | case 0: |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 908 | // No tags at all or none we know about. Nothing to do. |
| 909 | // This is part of the pass-through path of this function |
| 910 | // that will mostly be taken when decoding any item. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 911 | break; |
| 912 | |
| 913 | case QCBOR_TAGFLAG_DATE_STRING: |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 914 | nReturn = DecodeDateString(pDecodedItem); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 915 | break; |
| 916 | |
| 917 | case QCBOR_TAGFLAG_DATE_EPOCH: |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 918 | nReturn = DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 919 | break; |
| 920 | |
| 921 | case QCBOR_TAGFLAG_POS_BIGNUM: |
| 922 | case QCBOR_TAGFLAG_NEG_BIGNUM: |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 923 | nReturn = DecodeBigNum(pDecodedItem); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 924 | break; |
| 925 | |
| 926 | default: |
| 927 | // Encountering some mixed up CBOR like something that |
| 928 | // is tagged as both a string and integer date. |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 929 | nReturn = QCBOR_ERR_BAD_OPT_TAG; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | Done: |
| 933 | return nReturn; |
| 934 | } |
| 935 | |
| 936 | |
| 937 | /* |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 938 | This layer takes care of map entries. It combines the label and data items into one QCBORItem. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 939 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 940 | static inline QCBORError GetNext_MapEntry(QCBORDecodeContext *me, QCBORItem *pDecodedItem, QCBORTagListOut *pTags) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 941 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 942 | // Stack use: int/ptr 1, QCBORItem -- 56 |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 943 | QCBORError nReturn = GetNext_TaggedItem(me, pDecodedItem, pTags); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 944 | if(nReturn) |
| 945 | goto Done; |
| 946 | |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 947 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 948 | // Break can't be a map entry |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 949 | goto Done; |
| 950 | } |
| 951 | |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame^] | 952 | if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
| 953 | // In a map and caller wants maps decoded, not treated as arrays |
| 954 | |
| 955 | if(DecodeNesting_TypeIsMap(&(me->nesting))) { |
| 956 | // If in a map and the right decoding mode, get the label |
| 957 | |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 958 | // Get the next item which will be the real data; Item will be the label |
| 959 | QCBORItem LabelItem = *pDecodedItem; |
| 960 | nReturn = GetNext_TaggedItem(me, pDecodedItem, pTags); |
| 961 | if(nReturn) |
| 962 | goto Done; |
| 963 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 964 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 965 | |
| 966 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 967 | // strings are always good labels |
| 968 | pDecodedItem->label.string = LabelItem.val.string; |
| 969 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 970 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) { |
| 971 | // It's not a string and we only want strings, probably for easy translation to JSON |
| 972 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 973 | goto Done; |
| 974 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 975 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 976 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 977 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 978 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 979 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 980 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 981 | pDecodedItem->label.string = LabelItem.val.string; |
| 982 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 983 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 984 | } else { |
| 985 | // label is not an int or a string. It is an arrray |
| 986 | // or a float or such and this implementation doesn't handle that. |
| 987 | // Also, tags on labels are ignored. |
| 988 | nReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
| 989 | goto Done; |
| 990 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame^] | 991 | } |
| 992 | } else { |
| 993 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
| 994 | // Decoding a map as an array |
| 995 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
| 996 | pDecodedItem->val.uCount *= 2; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1000 | Done: |
| 1001 | return nReturn; |
| 1002 | } |
| 1003 | |
| 1004 | |
| 1005 | /* |
| 1006 | Public function, see header qcbor.h file |
| 1007 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1008 | QCBORError QCBORDecode_GetNextWithTags(QCBORDecodeContext *me, QCBORItem *pDecodedItem, QCBORTagListOut *pTags) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1009 | { |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1010 | // Stack ptr/int: 2, QCBORItem : 64 |
| 1011 | |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1012 | // The public entry point for fetching and parsing the next QCBORItem. |
| 1013 | // All the CBOR parsing work is here and in subordinate calls. |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1014 | QCBORError nReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1015 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1016 | nReturn = GetNext_MapEntry(me, pDecodedItem, pTags); |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1017 | if(nReturn) { |
| 1018 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1019 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1020 | |
| 1021 | // Break ending arrays/maps are always processed at the end of this function. |
| 1022 | // They should never show up here. |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1023 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1024 | nReturn = QCBOR_ERR_BAD_BREAK; |
| 1025 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1026 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1027 | |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1028 | // Record the nesting level for this data item before processing any of |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1029 | // decrementing and descending. |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1030 | pDecodedItem->uNestingLevel = DecodeNesting_GetLevel(&(me->nesting)); |
| 1031 | |
| 1032 | // Process the item just received for descent or decrement, and |
| 1033 | // 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] | 1034 | if(IsMapOrArray(pDecodedItem->uDataType)) { |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1035 | // If the new item is array or map, the nesting level descends |
Laurence Lundblade | 3a760b0 | 2018-10-08 13:46:03 +0800 | [diff] [blame] | 1036 | nReturn = DecodeNesting_Descend(&(me->nesting), pDecodedItem); |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1037 | // Maps and arrays do count in as items in the map/array that encloses |
| 1038 | // them so a decrement needs to be done for them too, but that is done |
| 1039 | // only when all the items in them have been processed, not when they |
| 1040 | // are opened. |
| 1041 | } else { |
| 1042 | // Decrement the count of items in the enclosing map/array |
| 1043 | // If the count in the enclosing map/array goes to zero, that |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1044 | // triggers a decrement in the map/array above that and |
| 1045 | // an ascend in nesting level. |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1046 | DecodeNesting_DecrementCount(&(me->nesting)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1047 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1048 | if(nReturn) { |
| 1049 | goto Done; |
| 1050 | } |
| 1051 | |
| 1052 | // For indefinite length maps/arrays, looking at any and |
| 1053 | // all breaks that might terminate them. The equivalent |
| 1054 | // for definite length maps/arrays happens in |
| 1055 | // DecodeNesting_DecrementCount(). |
| 1056 | if(DecodeNesting_IsNested(&(me->nesting)) && DecodeNesting_IsIndefiniteLength(&(me->nesting))) { |
| 1057 | while(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) { |
| 1058 | // Peek forward one item to see if it is a break. |
| 1059 | QCBORItem Peek; |
| 1060 | size_t uPeek = UsefulInputBuf_Tell(&(me->InBuf)); |
| 1061 | nReturn = GetNext_Item(&(me->InBuf), &Peek, NULL); |
| 1062 | if(nReturn) { |
| 1063 | goto Done; |
| 1064 | } |
| 1065 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
| 1066 | // It is not a break, rewind so it can be processed normally. |
| 1067 | UsefulInputBuf_Seek(&(me->InBuf), uPeek); |
| 1068 | break; |
| 1069 | } |
| 1070 | // It is a break. Ascend one nesting level. |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1071 | // The break is consumed. |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1072 | nReturn = DecodeNesting_BreakAscend(&(me->nesting)); |
| 1073 | if(nReturn) { |
| 1074 | // break occured outside of an indefinite length array/map |
| 1075 | goto Done; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | // Tell the caller what level is next. This tells them what maps/arrays |
| 1081 | // were closed out and makes it possible for them to reconstruct |
| 1082 | // the tree with just the information returned by GetNext |
| 1083 | pDecodedItem->uNextNestLevel = DecodeNesting_GetLevel(&(me->nesting)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1084 | |
| 1085 | Done: |
| 1086 | return nReturn; |
| 1087 | } |
| 1088 | |
| 1089 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1090 | QCBORError QCBORDecode_GetNext(QCBORDecodeContext *me, QCBORItem *pDecodedItem) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1091 | { |
| 1092 | return QCBORDecode_GetNextWithTags(me, pDecodedItem, NULL); |
| 1093 | } |
| 1094 | |
| 1095 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1096 | /* |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1097 | Decoding items is done in 5 layered functions, one calling the |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1098 | next one down. If a layer has no work to do for a particular item |
| 1099 | it returns quickly. |
| 1100 | |
| 1101 | - QCBORDecode_GetNext -- The top layer manages the beginnings and |
| 1102 | ends of maps and arrays. It tracks descending into and ascending |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1103 | out of maps/arrays. It processes all breaks that terminate |
| 1104 | maps and arrays. |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1105 | |
| 1106 | - GetNext_MapEntry -- This handles the combining of two |
| 1107 | items, the label and the data, that make up a map entry. |
| 1108 | It only does work on maps. It combines the label and data |
| 1109 | items into one labeled item. |
| 1110 | |
| 1111 | - GetNext_TaggedItem -- This handles the type 6 tagged items. |
| 1112 | It accumulates all the tags and combines them with the following |
| 1113 | non-tagged item. If the tagged item is something that is understood |
| 1114 | like a date, the decoding of that item is invoked. |
| 1115 | |
| 1116 | - GetNext_FullItem -- This assembles the sub items that make up |
| 1117 | an indefinte length string into one string item. It uses the |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1118 | string allocater to create contiguous space for the item. It |
| 1119 | processes all breaks that are part of indefinite length strings. |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1120 | |
| 1121 | - GetNext_Item -- This gets and decodes the most atomic |
| 1122 | item in CBOR, the thing with an initial byte containing |
| 1123 | the major type. |
| 1124 | |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1125 | Roughly this takes 300 bytes of stack for vars. Need to |
| 1126 | evaluate this more carefully and correctly. |
| 1127 | |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 1128 | */ |
| 1129 | |
| 1130 | |
| 1131 | /* |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1132 | Public function, see header qcbor.h file |
| 1133 | */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1134 | int QCBORDecode_IsTagged(QCBORDecodeContext *me, const QCBORItem *pItem, uint64_t uTag) |
| 1135 | { |
| 1136 | const QCBORTagListIn *pCallerConfiguredTagMap = me->pCallerConfiguredTagList; |
| 1137 | |
| 1138 | uint8_t uTagBitIndex; |
| 1139 | // Do not care about errors in pCallerConfiguredTagMap here. They are |
| 1140 | // caught during GetNext() before this is called. |
| 1141 | if(TagMapper_Lookup(pCallerConfiguredTagMap, uTag, &uTagBitIndex)) { |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | const uint64_t uTagBit = 0x01ULL << uTagBitIndex; |
| 1146 | return (uTagBit & pItem->uTagBits) != 0; |
| 1147 | } |
| 1148 | |
| 1149 | |
| 1150 | /* |
| 1151 | Public function, see header qcbor.h file |
| 1152 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1153 | QCBORError QCBORDecode_Finish(QCBORDecodeContext *me) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1154 | { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1155 | int nReturn = QCBOR_SUCCESS; |
| 1156 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1157 | // Error out if all the maps/arrays are not closed out |
| 1158 | if(DecodeNesting_IsNested(&(me->nesting))) { |
| 1159 | nReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN; |
| 1160 | goto Done; |
| 1161 | } |
| 1162 | |
| 1163 | // Error out if not all the bytes are consumed |
| 1164 | if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) { |
| 1165 | nReturn = QCBOR_ERR_EXTRA_BYTES; |
| 1166 | } |
| 1167 | |
| 1168 | Done: |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1169 | // Call the destructor for the string allocator if there is one. |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1170 | // Always called, even if there are errors; always have to clean up |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1171 | if(me->pStringAllocator) { |
| 1172 | QCBORStringAllocator *pAllocator = (QCBORStringAllocator *)me->pStringAllocator; |
| 1173 | if(pAllocator->fDestructor) { |
| 1174 | (pAllocator->fDestructor)(pAllocator->pAllocaterContext); |
| 1175 | } |
| 1176 | } |
| 1177 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1178 | return nReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | |
| 1182 | |
| 1183 | /* |
| 1184 | |
| 1185 | Decoder errors handled in this file |
| 1186 | |
| 1187 | - Hit end of input before it was expected while decoding type and number QCBOR_ERR_HIT_END |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1188 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1189 | - negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW |
| 1190 | |
| 1191 | - Hit end of input while decoding a text or byte string QCBOR_ERR_HIT_END |
| 1192 | |
| 1193 | - Encountered conflicting tags -- e.g., an item is tagged both a date string and an epoch date QCBOR_ERR_UNSUPPORTED |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1194 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1195 | - Encontered an array or mapp that has too many items QCBOR_ERR_ARRAY_TOO_LONG |
| 1196 | |
| 1197 | - Encountered array/map nesting that is too deep QCBOR_ERR_ARRAY_NESTING_TOO_DEEP |
| 1198 | |
| 1199 | - An epoch date > INT64_MAX or < INT64_MIN was encountered QCBOR_ERR_DATE_OVERFLOW |
| 1200 | |
| 1201 | - The type of a map label is not a string or int QCBOR_ERR_MAP_LABEL_TYPE |
| 1202 | |
| 1203 | - Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES |
| 1204 | |
| 1205 | */ |
| 1206 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1207 | |
| 1208 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1209 | |
| 1210 | /* |
| 1211 | This is a very primitive memory allocator. It does not track individual |
| 1212 | allocations, only a high-water mark. A free or reallotcation must be of |
| 1213 | the last chunk allocated. |
| 1214 | |
| 1215 | All of this following code will get dead-stripped if QCBORDecode_SetMemPool() |
| 1216 | is not called. |
| 1217 | */ |
| 1218 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1219 | typedef struct { |
| 1220 | QCBORStringAllocator StringAllocator; |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1221 | uint8_t *pStart; // First byte that can be allocated |
| 1222 | uint8_t *pEnd; // One past the last byte that can be allocated |
| 1223 | uint8_t *pFree; // Where the next free chunk is |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1224 | } MemPool; |
| 1225 | |
| 1226 | |
| 1227 | /* |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1228 | Internal function for an allocation |
| 1229 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1230 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 1231 | */ |
| 1232 | static UsefulBuf MemPool_Alloc(void *ctx, void *pMem, size_t uNewSize) |
| 1233 | { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1234 | MemPool *me = (MemPool *)ctx; |
| 1235 | void *pReturn = NULL; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1236 | |
| 1237 | if(pMem) { |
| 1238 | // Realloc case |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1239 | // This check will work even if uNewSize is a super-large value like UINT64_MAX |
| 1240 | if((uNewSize <= (size_t)(me->pEnd - (uint8_t *)pMem)) && ((uint8_t *)pMem >= me->pStart)) { |
Laurence Lundblade | 9e3651c | 2018-10-10 11:49:55 +0800 | [diff] [blame] | 1241 | me->pFree = (uint8_t *)pMem + uNewSize; |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1242 | pReturn = pMem; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1243 | } |
| 1244 | } else { |
| 1245 | // New chunk case |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1246 | // This check will work even if uNewSize is a super large value like UINT64_MAX |
| 1247 | if(uNewSize <= (size_t)(me->pEnd - me->pFree)) { |
| 1248 | pReturn = me->pFree; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1249 | me->pFree += uNewSize; |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | return (UsefulBuf){pReturn, uNewSize}; |
| 1254 | } |
| 1255 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1256 | /* |
| 1257 | Internal function to free memory |
| 1258 | */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1259 | static void MemPool_Free(void *ctx, void *pOldMem) |
| 1260 | { |
| 1261 | MemPool *me = (MemPool *)ctx; |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1262 | me->pFree = pOldMem; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1265 | /* |
| 1266 | Public function, see header qcbor.h file |
| 1267 | */ |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1268 | QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *me, UsefulBuf Pool, bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1269 | { |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1270 | // The first bytes of the Pool passed in are used |
| 1271 | // as the context (vtable of sorts) for the memory pool |
| 1272 | // allocator. |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1273 | if(Pool.len < sizeof(MemPool)+1) { |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1274 | return QCBOR_ERR_BUFFER_TOO_SMALL; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | MemPool *pMP = (MemPool *)Pool.ptr; |
| 1278 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1279 | // Fill in the "vtable" |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1280 | pMP->StringAllocator.fAllocate = MemPool_Alloc; |
| 1281 | pMP->StringAllocator.fFree = MemPool_Free; |
| 1282 | pMP->StringAllocator.fDestructor = NULL; |
| 1283 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1284 | // Set up the pointers to the memory to be allocated |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 1285 | pMP->pStart = (uint8_t *)Pool.ptr + sizeof(MemPool); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1286 | pMP->pFree = pMP->pStart; |
Laurence Lundblade | 570fab5 | 2018-10-13 18:28:27 +0800 | [diff] [blame] | 1287 | pMP->pEnd = (uint8_t *)Pool.ptr + Pool.len; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1288 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1289 | // More book keeping of context |
| 1290 | pMP->StringAllocator.pAllocaterContext = pMP; |
| 1291 | me->pStringAllocator = pMP; |
| 1292 | |
| 1293 | // The flag indicating when to use the allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1294 | me->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1295 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 1296 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1297 | } |
| 1298 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 1299 | |
| 1300 | /* |
| 1301 | Extra little hook to make MemPool testing work right |
| 1302 | without adding any code size or overhead to non-test |
| 1303 | uses. This will get dead-stripped for non-test use. |
| 1304 | |
| 1305 | This is not a public function. |
| 1306 | */ |
| 1307 | size_t MemPoolTestHook_GetPoolSize(void *ctx) |
| 1308 | { |
| 1309 | MemPool *me = (MemPool *)ctx; |
| 1310 | |
| 1311 | return me->pEnd - me->pStart; |
| 1312 | } |
| 1313 | |