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