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