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