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