blob: b015dd82f6badaae52597aefc97dd8d89f7256f9 [file] [log] [blame]
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
3 Copyright (c) 2018, Laurence Lundblade.
4 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
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 Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundblade624405d2018-09-18 20:10:47 -070031 ==============================================================================*/
32
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070033/*===================================================================================
34 FILE: qcbor_decode.c
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080035
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070036 DESCRIPTION: This file contains the implementation of QCBOR.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080037
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070038 EDIT HISTORY FOR FILE:
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080039
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070040 This section contains comments describing changes made to the module.
41 Notice that changes are listed in reverse chronological order.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080042
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070043 when who what, where, why
44 -------- ---- ---------------------------------------------------
Laurence Lundblade8b06e2e2018-12-04 12:26:51 +090045 11/9/18 llundblade Error codes are now enums.
46 11/2/18 llundblade Simplify float decoding and align with preferred
47 float encoding
48 10/31/18 llundblade Switch to one license that is almost BSD-3.
49 10/28/18 llundblade Reworked tag decoding
50 10/15/18 llundblade Indefinite length maps and arrays supported
51 10/8/18 llundblade Indefinite length strings supported
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070052 02/04/17 llundbla Work on CPUs that don's require pointer alignment
53 by making use of changes in UsefulBuf
54 03/01/17 llundbla More data types; decoding improvements and fixes
55 11/13/16 llundbla Integrate most TZ changes back into github version.
56 09/30/16 gkanike Porting to TZ.
57 03/15/16 llundbla Initial Version.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080058
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070059 =====================================================================================*/
60
61#include "qcbor.h"
Laurence Lundblade12d32c52018-09-19 11:25:27 -070062#include "ieee754.h"
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070063
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070064
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +053065/*
66 This casts away the const-ness of a pointer, usually so it can be
67 freed or realloced.
68 */
69#define UNCONST_POINTER(ptr) ((void *)(ptr))
70
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070071
72/*
Laurence Lundblade3a760b02018-10-08 13:46:03 +080073 Collection of functions to track the map/array nesting for decoding
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070074 */
75
76inline static int IsMapOrArray(uint8_t uDataType)
77{
78 return uDataType == QCBOR_TYPE_MAP || uDataType == QCBOR_TYPE_ARRAY;
79}
80
81inline static int DecodeNesting_IsNested(const QCBORDecodeNesting *pNesting)
82{
83 return pNesting->pCurrent != &(pNesting->pMapsAndArrays[0]);
84}
85
Laurence Lundblade041ffa52018-10-07 11:43:51 +070086inline static int DecodeNesting_IsIndefiniteLength(const QCBORDecodeNesting *pNesting)
Laurence Lundblade0f99d692018-09-26 14:39:28 -070087{
Laurence Lundblade0f99d692018-09-26 14:39:28 -070088 return pNesting->pCurrent->uCount == UINT16_MAX;
89}
90
Laurence Lundblade3a760b02018-10-08 13:46:03 +080091inline static uint8_t DecodeNesting_GetLevel(QCBORDecodeNesting *pNesting)
92{
93 return pNesting->pCurrent - &(pNesting->pMapsAndArrays[0]);
94}
95
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070096inline static int DecodeNesting_TypeIsMap(const QCBORDecodeNesting *pNesting)
97{
Laurence Lundblade0f99d692018-09-26 14:39:28 -070098 if(!DecodeNesting_IsNested(pNesting)) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070099 return 0;
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700100 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800101
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700102 return CBOR_MAJOR_TYPE_MAP == pNesting->pCurrent->uMajorType;
103}
104
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800105// Process a break. This will either ascend the nesting or error out
Laurence Lundblade30816f22018-11-10 13:40:22 +0700106inline static QCBORError DecodeNesting_BreakAscend(QCBORDecodeNesting *pNesting)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700107{
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800108 // breaks must always occur when there is nesting
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700109 if(!DecodeNesting_IsNested(pNesting)) {
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800110 return QCBOR_ERR_BAD_BREAK;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700111 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800112
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800113 // breaks can only occur when the map/array is indefinite length
114 if(!DecodeNesting_IsIndefiniteLength(pNesting)) {
115 return QCBOR_ERR_BAD_BREAK;
116 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800117
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800118 // if all OK, the break reduces the level of nesting
119 pNesting->pCurrent--;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800120
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800121 return QCBOR_SUCCESS;
122}
123
124// Called on every single item except breaks including the opening of a map/array
125inline static void DecodeNesting_DecrementCount(QCBORDecodeNesting *pNesting)
126{
127 if(!DecodeNesting_IsNested(pNesting)) {
128 // at top level where there is no tracking
129 return;
130 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800131
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800132 if(DecodeNesting_IsIndefiniteLength(pNesting)) {
133 // There is no count for indefinite length arrays/maps
134 return;
135 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800136
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800137 // Decrement the count of items in this array/map
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700138 pNesting->pCurrent->uCount--;
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700139
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800140 // Pop up nesting levels if the counts at the levels are zero
141 while(DecodeNesting_IsNested(pNesting) && 0 == pNesting->pCurrent->uCount) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700142 pNesting->pCurrent--;
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800143 if(!DecodeNesting_IsIndefiniteLength(pNesting)) {
144 pNesting->pCurrent->uCount--;
145 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700146 }
147}
148
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800149// Called on every map/array
Laurence Lundblade30816f22018-11-10 13:40:22 +0700150inline static QCBORError DecodeNesting_Descend(QCBORDecodeNesting *pNesting, QCBORItem *pItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700151{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700152 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800153
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800154 if(pItem->val.uCount == 0) {
155 // Nothing to do for empty definite lenth arrays. They are just are
156 // effectively the same as an item that is not a map or array
157 goto Done;
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530158 // Empty indefinite length maps and arrays are handled elsewhere
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800159 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800160
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800161 // Error out if arrays is too long to handle
162 if(pItem->val.uCount != UINT16_MAX && pItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700163 nReturn = QCBOR_ERR_ARRAY_TOO_LONG;
164 goto Done;
165 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800166
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800167 // Error out if nesting is too deep
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700168 if(pNesting->pCurrent >= &(pNesting->pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING])) {
169 nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
170 goto Done;
171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800173 // The actual descend
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700174 pNesting->pCurrent++;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800175
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800176 // Record a few details for this nesting level
177 pNesting->pCurrent->uMajorType = pItem->uDataType;
178 pNesting->pCurrent->uCount = pItem->val.uCount;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800179
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700180Done:
181 return nReturn;;
182}
183
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700184inline static void DecodeNesting_Init(QCBORDecodeNesting *pNesting)
185{
186 pNesting->pCurrent = &(pNesting->pMapsAndArrays[0]);
187}
188
189
190
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700191/*
192 This list of built-in tags. Only add tags here that are
193 clearly established and useful. Once a tag is added here
194 it can't be taken out as that would break backwards compatibility.
195 There are only 48 slots available forever.
196 */
197static const uint16_t spBuiltInTagMap[] = {
198 CBOR_TAG_DATE_STRING, // See TAG_MAPPER_FIRST_FOUR
199 CBOR_TAG_DATE_EPOCH, // See TAG_MAPPER_FIRST_FOUR
200 CBOR_TAG_POS_BIGNUM, // See TAG_MAPPER_FIRST_FOUR
201 CBOR_TAG_NEG_BIGNUM, // See TAG_MAPPER_FIRST_FOUR
202 CBOR_TAG_FRACTION,
203 CBOR_TAG_BIGFLOAT,
204 CBOR_TAG_COSE_ENCRYPTO,
205 CBOR_TAG_COSE_MAC0,
206 CBOR_TAG_COSE_SIGN1,
207 CBOR_TAG_ENC_AS_B64URL,
208 CBOR_TAG_ENC_AS_B64,
209 CBOR_TAG_ENC_AS_B16,
210 CBOR_TAG_CBOR,
211 CBOR_TAG_URI,
212 CBOR_TAG_B64URL,
213 CBOR_TAG_B64,
214 CBOR_TAG_REGEX,
215 CBOR_TAG_MIME,
216 CBOR_TAG_BIN_UUID,
217 CBOR_TAG_CWT,
218 CBOR_TAG_ENCRYPT,
219 CBOR_TAG_MAC,
220 CBOR_TAG_SIGN,
221 CBOR_TAG_GEO_COORD,
222 CBOR_TAG_CBOR_MAGIC
223};
224
225// This is used in a bit of cleverness in GetNext_TaggedItem() to
226// keep code size down and switch for the internal processing of
227// these types. This will break if the first four items in
228// spBuiltInTagMap don't have values 0,1,2,3. That is the
229// mapping is 0 to 0, 1 to 1, 2 to 2 and 3 to 3.
230#define QCBOR_TAGFLAG_DATE_STRING (0x01LL << CBOR_TAG_DATE_STRING)
231#define QCBOR_TAGFLAG_DATE_EPOCH (0x01LL << CBOR_TAG_DATE_EPOCH)
232#define QCBOR_TAGFLAG_POS_BIGNUM (0x01LL << CBOR_TAG_POS_BIGNUM)
233#define QCBOR_TAGFLAG_NEG_BIGNUM (0x01LL << CBOR_TAG_NEG_BIGNUM)
234
235#define TAG_MAPPER_FIRST_FOUR (QCBOR_TAGFLAG_DATE_STRING |\
236 QCBOR_TAGFLAG_DATE_EPOCH |\
237 QCBOR_TAGFLAG_POS_BIGNUM |\
238 QCBOR_TAGFLAG_NEG_BIGNUM)
239
240#define TAG_MAPPER_TOTAL_TAG_BITS 64 // Number of bits in a uint64_t
241#define TAG_MAPPER_CUSTOM_TAGS_BASE_INDEX (TAG_MAPPER_TOTAL_TAG_BITS - QCBOR_MAX_CUSTOM_TAGS) // 48
242#define TAG_MAPPER_MAX_SIZE_BUILT_IN_TAGS (TAG_MAPPER_TOTAL_TAG_BITS - QCBOR_MAX_CUSTOM_TAGS ) // 48
243
244static inline int TagMapper_LookupBuiltIn(uint64_t uTag)
245{
246 if(sizeof(spBuiltInTagMap)/sizeof(uint16_t) > TAG_MAPPER_MAX_SIZE_BUILT_IN_TAGS) {
247 // This is a cross-check to make sure the above array doesn't
248 // accidentally get made too big.
249 // In normal conditions the above test should optimize out
250 // as all the values are known at compile time.
251 return -1;
252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700254 if(uTag > UINT16_MAX) {
255 // This tag map works only on 16-bit tags
256 return -1;
257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800258
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700259 for(int nTagBitIndex = 0; nTagBitIndex < (int)(sizeof(spBuiltInTagMap)/sizeof(uint16_t)); nTagBitIndex++) {
260 if(spBuiltInTagMap[nTagBitIndex] == uTag) {
261 return nTagBitIndex;
262 }
263 }
264 return -1; // Indicates no match
265}
266
267static inline int TagMapper_LookupCallerConfigured(const QCBORTagListIn *pCallerConfiguredTagMap, uint64_t uTag)
268{
269 for(int nTagBitIndex = 0; nTagBitIndex < pCallerConfiguredTagMap->uNumTags; nTagBitIndex++) {
270 if(pCallerConfiguredTagMap->puTags[nTagBitIndex] == uTag) {
271 return nTagBitIndex + TAG_MAPPER_CUSTOM_TAGS_BASE_INDEX;
272 }
273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800274
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700275 return -1; // Indicates no match
276}
277
278/*
279 Find the tag bit index for a given tag value, or error out
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800280
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700281 This and the above functions could probably be optimized and made
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800282 clearer and neater.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700283 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700284static QCBORError TagMapper_Lookup(const QCBORTagListIn *pCallerConfiguredTagMap, uint64_t uTag, uint8_t *puTagBitIndex)
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700285{
286 int nTagBitIndex = TagMapper_LookupBuiltIn(uTag);
287 if(nTagBitIndex >= 0) {
288 // Cast is safe because TagMapper_LookupBuiltIn never returns > 47
289 *puTagBitIndex = (uint8_t)nTagBitIndex;
290 return QCBOR_SUCCESS;
291 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800292
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700293 if(pCallerConfiguredTagMap) {
294 if(pCallerConfiguredTagMap->uNumTags > QCBOR_MAX_CUSTOM_TAGS) {
295 return QCBOR_ERR_TOO_MANY_TAGS;
296 }
297 nTagBitIndex = TagMapper_LookupCallerConfigured(pCallerConfiguredTagMap, uTag);
298 if(nTagBitIndex >= 0) {
299 // Cast is safe because TagMapper_LookupBuiltIn never returns > 63
300
301 *puTagBitIndex = (uint8_t)nTagBitIndex;
302 return QCBOR_SUCCESS;
303 }
304 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800305
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700306 return QCBOR_ERR_BAD_OPT_TAG;
307}
308
309
310
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700311
312/*
313 Public function, see header file
314 */
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800315void QCBORDecode_Init(QCBORDecodeContext *me, UsefulBufC EncodedCBOR, QCBORDecodeMode nDecodeMode)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700316{
317 memset(me, 0, sizeof(QCBORDecodeContext));
318 UsefulInputBuf_Init(&(me->InBuf), EncodedCBOR);
319 // Don't bother with error check on decode mode. If a bad value is passed it will just act as
320 // if the default normal mode of 0 was set.
321 me->uDecodeMode = nDecodeMode;
322 DecodeNesting_Init(&(me->nesting));
323}
324
325
326/*
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700327 Public function, see header file
328 */
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530329void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pCtx, const QCBORStringAllocator *pAllocator, bool bAllocAll)
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700330{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700331 pCtx->pStringAllocator = (void *)pAllocator;
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530332 pCtx->bStringAllocateAll = bAllocAll;
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700333}
334
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700335void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *me, const QCBORTagListIn *pTagList)
336{
337 me->pCallerConfiguredTagList = pTagList;
338}
339
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700340
341/*
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700342 This decodes the fundamental part of a CBOR data item, the type and number
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800343
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700344 This is the Counterpart to InsertEncodedTypeAndNumber().
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800345
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700346 This does the network->host byte order conversion. The conversion here
347 also results in the conversion for floats in addition to that for
348 lengths, tags and integer values.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800349
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700350 This returns:
351 pnMajorType -- the major type for the item
352 puNumber -- the "number" which is used a the value for integers, tags and floats and length for strings and arrays
353 puAdditionalInfo -- Pass this along to know what kind of float or if length is indefinite
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800354
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700355 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700356inline static QCBORError DecodeTypeAndNumber(UsefulInputBuf *pUInBuf, int *pnMajorType, uint64_t *puNumber, uint8_t *puAdditionalInfo)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700357{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700358 // Stack usage: int/ptr 5 -- 40
Laurence Lundblade30816f22018-11-10 13:40:22 +0700359 QCBORError nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800360
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700361 // Get the initial byte that every CBOR data item has
362 const uint8_t InitialByte = UsefulInputBuf_GetByte(pUInBuf);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800363
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700364 // Break down the initial byte
365 const uint8_t uTmpMajorType = InitialByte >> 5;
366 const uint8_t uAdditionalInfo = InitialByte & 0x1f;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800367
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700368 // Get the integer that follows the major type. Do not know if this is a length, value, float or tag at this point
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700369 // Also convert from network byte order.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700370 uint64_t uTmpValue;
371 switch(uAdditionalInfo) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800372
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700373 case LEN_IS_ONE_BYTE:
374 uTmpValue = UsefulInputBuf_GetByte(pUInBuf);
375 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800376
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700377 case LEN_IS_TWO_BYTES:
378 uTmpValue = UsefulInputBuf_GetUint16(pUInBuf);
379 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800380
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700381 case LEN_IS_FOUR_BYTES:
382 uTmpValue = UsefulInputBuf_GetUint32(pUInBuf);
383 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800384
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700385 case LEN_IS_EIGHT_BYTES:
386 uTmpValue = UsefulInputBuf_GetUint64(pUInBuf);
387 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800388
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700389 case ADDINFO_RESERVED1: // reserved by CBOR spec
390 case ADDINFO_RESERVED2: // reserved by CBOR spec
391 case ADDINFO_RESERVED3: // reserved by CBOR spec
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700392 nReturn = QCBOR_ERR_UNSUPPORTED;
393 goto Done;
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700394
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700395 default:
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700396 // This is when the "number" is in the additional info
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700397 uTmpValue = uAdditionalInfo;
398 break;
399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800400
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700401 // If any of the UsefulInputBuf_Get calls fail we will get here with uTmpValue as 0.
402 // There is no harm in this. This following check takes care of catching all of
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800403 // these errors.
404
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700405 if(UsefulInputBuf_GetError(pUInBuf)) {
406 nReturn = QCBOR_ERR_HIT_END;
407 goto Done;
408 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800409
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700410 // All successful if we got here.
411 nReturn = QCBOR_SUCCESS;
412 *pnMajorType = uTmpMajorType;
413 *puNumber = uTmpValue;
414 *puAdditionalInfo = uAdditionalInfo;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800415
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700416Done:
417 return nReturn;
418}
419
420
421/*
422 CBOR doesn't explicitly specify two's compliment for integers but all CPUs
423 use it these days and the test vectors in the RFC are so. All integers in the CBOR
424 structure are positive and the major type indicates positive or negative.
425 CBOR can express positive integers up to 2^x - 1 where x is the number of bits
426 and negative integers down to 2^x. Note that negative numbers can be one
427 more away from zero than positive.
428 Stdint, as far as I can tell, uses two's compliment to represent
429 negative integers.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800430
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700431 See http://www.unix.org/whitepapers/64bit.html for reasons int isn't
432 used here in any way including in the interface
433 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700434inline static QCBORError DecodeInteger(int nMajorType, uint64_t uNumber, QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700435{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700436 // Stack usage: int/ptr 1 -- 8
Laurence Lundblade30816f22018-11-10 13:40:22 +0700437 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800438
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700439 if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) {
440 if (uNumber <= INT64_MAX) {
441 pDecodedItem->val.int64 = (int64_t)uNumber;
442 pDecodedItem->uDataType = QCBOR_TYPE_INT64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800443
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700444 } else {
445 pDecodedItem->val.uint64 = uNumber;
446 pDecodedItem->uDataType = QCBOR_TYPE_UINT64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800447
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700448 }
449 } else {
450 if(uNumber <= INT64_MAX) {
451 pDecodedItem->val.int64 = -uNumber-1;
452 pDecodedItem->uDataType = QCBOR_TYPE_INT64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800453
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700454 } else {
455 // C can't represent a negative integer in this range
456 // so it is an error. todo -- test this condition
457 nReturn = QCBOR_ERR_INT_OVERFLOW;
458 }
459 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800460
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700461 return nReturn;
462}
463
464// Make sure #define value line up as DecodeSimple counts on this.
465#if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE
466#error QCBOR_TYPE_FALSE macro value wrong
467#endif
468
469#if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE
470#error QCBOR_TYPE_TRUE macro value wrong
471#endif
472
473#if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL
474#error QCBOR_TYPE_NULL macro value wrong
475#endif
476
477#if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF
478#error QCBOR_TYPE_UNDEF macro value wrong
479#endif
480
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700481#if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK
482#error QCBOR_TYPE_BREAK macro value wrong
483#endif
484
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700485#if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT
486#error QCBOR_TYPE_DOUBLE macro value wrong
487#endif
488
489#if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT
490#error QCBOR_TYPE_FLOAT macro value wrong
491#endif
492
493/*
494 Decode true, false, floats, break...
495 */
496
Laurence Lundblade30816f22018-11-10 13:40:22 +0700497inline static QCBORError DecodeSimple(uint8_t uAdditionalInfo, uint64_t uNumber, QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700498{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700499 // Stack usage: 0
Laurence Lundblade30816f22018-11-10 13:40:22 +0700500 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800501
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700502 // uAdditionalInfo is 5 bits from the initial byte
503 // compile time checks above make sure uAdditionalInfo values line up with uDataType values
504 pDecodedItem->uDataType = uAdditionalInfo;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800505
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700506 switch(uAdditionalInfo) {
507 case ADDINFO_RESERVED1: // 28
508 case ADDINFO_RESERVED2: // 29
509 case ADDINFO_RESERVED3: // 30
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700510 nReturn = QCBOR_ERR_UNSUPPORTED;
511 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800512
Laurence Lundbladecc2ed342018-09-22 17:29:55 -0700513 case HALF_PREC_FLOAT:
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700514 pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uNumber);
515 pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700516 break;
Laurence Lundbladecc2ed342018-09-22 17:29:55 -0700517 case SINGLE_PREC_FLOAT:
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700518 pDecodedItem->val.dfnum = (double)UsefulBufUtil_CopyUint32ToFloat((uint32_t)uNumber);
519 pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700520 break;
521 case DOUBLE_PREC_FLOAT:
522 pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uNumber);
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700523 pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700524 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800525
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700526 case CBOR_SIMPLEV_FALSE: // 20
527 case CBOR_SIMPLEV_TRUE: // 21
528 case CBOR_SIMPLEV_NULL: // 22
529 case CBOR_SIMPLEV_UNDEF: // 23
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700530 case CBOR_SIMPLE_BREAK: // 31
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700531 break; // nothing to do
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800532
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700533 case CBOR_SIMPLEV_ONEBYTE: // 24
534 if(uNumber <= CBOR_SIMPLE_BREAK) {
535 // This takes out f8 00 ... f8 1f which should be encoded as e0 … f7
536 nReturn = QCBOR_ERR_INVALID_CBOR;
537 goto Done;
538 }
539 // fall through intentionally
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800540
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700541 default: // 0-19
542 pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE;
543 // DecodeTypeAndNumber will make uNumber equal to uAdditionalInfo when uAdditionalInfo is < 24
544 // This cast is safe because the 2, 4 and 8 byte lengths of uNumber are in the double/float cases above
545 pDecodedItem->val.uSimple = (uint8_t)uNumber;
546 break;
547 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800548
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700549Done:
550 return nReturn;
551}
552
553
554
555/*
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530556 Decode text and byte strings. Call the string allocator if asked to.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700557 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700558inline static QCBORError DecodeBytes(const QCBORStringAllocator *pAlloc, int nMajorType, uint64_t uStrLen, UsefulInputBuf *pUInBuf, QCBORItem *pDecodedItem)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700559{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700560 // Stack usage: UsefulBuf 2, int/ptr 1 40
Laurence Lundblade30816f22018-11-10 13:40:22 +0700561 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800562
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800563 const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, uStrLen);
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530564 if(UsefulBuf_IsNULLC(Bytes)) {
565 // Failed to get the bytes for this string item
566 nReturn = QCBOR_ERR_HIT_END;
567 goto Done;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700568 }
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530569
570 if(pAlloc) {
571 // We are asked to use string allocator to make a copy
572 UsefulBuf NewMem = pAlloc->fAllocate(pAlloc->pAllocaterContext, NULL, uStrLen);
573 if(UsefulBuf_IsNULL(NewMem)) {
Laurence Lundblade30816f22018-11-10 13:40:22 +0700574 nReturn = QCBOR_ERR_STRING_ALLOCATE;
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530575 goto Done;
576 }
577 pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes);
578 } else {
579 // Normal case with no string allocator
580 pDecodedItem->val.string = Bytes;
581 }
582 pDecodedItem->uDataType = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING) ? QCBOR_TYPE_BYTE_STRING : QCBOR_TYPE_TEXT_STRING;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800583
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530584Done:
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700585 return nReturn;
586}
587
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700588
589/*
590 Mostly just assign the right data type for the date string.
591 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700592inline static QCBORError DecodeDateString(QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700593{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700594 // Stack Use: UsefulBuf 1 16
595 if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700596 return QCBOR_ERR_BAD_OPT_TAG;
597 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800598
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800599 const UsefulBufC Temp = pDecodedItem->val.string;
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700600 pDecodedItem->val.dateString = Temp;
601 pDecodedItem->uDataType = QCBOR_TYPE_DATE_STRING;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700602 return QCBOR_SUCCESS;
603}
604
605
606/*
607 Mostly just assign the right data type for the bignum.
608 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700609inline static QCBORError DecodeBigNum(QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700610{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700611 // Stack Use: UsefulBuf 1 -- 16
612 if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700613 return QCBOR_ERR_BAD_OPT_TAG;
614 }
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -0800615 const UsefulBufC Temp = pDecodedItem->val.string;
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700616 pDecodedItem->val.bigNum = Temp;
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700617 pDecodedItem->uDataType = pDecodedItem->uTagBits & QCBOR_TAGFLAG_POS_BIGNUM ? QCBOR_TYPE_POSBIGNUM : QCBOR_TYPE_NEGBIGNUM;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700618 return QCBOR_SUCCESS;
619}
620
621
622/*
623 The epoch formatted date. Turns lots of different forms of encoding date into uniform one
624 */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700625static int DecodeDateEpoch(QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700626{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700627 // Stack usage: 1
Laurence Lundblade30816f22018-11-10 13:40:22 +0700628 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800629
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700630 pDecodedItem->val.epochDate.fSecondsFraction = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800631
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700632 switch (pDecodedItem->uDataType) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800633
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700634 case QCBOR_TYPE_INT64:
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700635 pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700636 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800637
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700638 case QCBOR_TYPE_UINT64:
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700639 if(pDecodedItem->val.uint64 > INT64_MAX) {
640 nReturn = QCBOR_ERR_DATE_OVERFLOW;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700641 goto Done;
642 }
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700643 pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.uint64;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700644 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800645
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800646 case QCBOR_TYPE_DOUBLE:
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700647 {
648 const double d = pDecodedItem->val.dfnum;
649 if(d > INT64_MAX) {
650 nReturn = QCBOR_ERR_DATE_OVERFLOW;
651 goto Done;
652 }
653 pDecodedItem->val.epochDate.nSeconds = d; // Float to integer conversion happening here.
654 pDecodedItem->val.epochDate.fSecondsFraction = d - pDecodedItem->val.epochDate.nSeconds;
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800655 }
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800656 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800657
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700658 default:
659 nReturn = QCBOR_ERR_BAD_OPT_TAG;
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700660 goto Done;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700661 }
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700662 pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800663
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700664Done:
665 return nReturn;
666}
667
668
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700669
670
671// Make sure the constants align as this is assumed by the GetAnItem() implementation
672#if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY
673#error QCBOR_TYPE_ARRAY value not lined up with major type
674#endif
675#if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP
676#error QCBOR_TYPE_MAP value not lined up with major type
677#endif
678
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700679/*
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700680 This gets a single data item and decodes it including preceding optional tagging. This does not
681 deal with arrays and maps and nesting except to decode the data item introducing them. Arrays and
682 maps are handled at the next level up in GetNext().
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800683
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700684 Errors detected here include: an array that is too long to decode, hit end of buffer unexpectedly,
685 a few forms of invalid encoded CBOR
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700686 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700687static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf, QCBORItem *pDecodedItem, const QCBORStringAllocator *pAlloc)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700688{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700689 // Stack usage: int/ptr 3 -- 24
Laurence Lundblade30816f22018-11-10 13:40:22 +0700690 QCBORError nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800691
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700692 // Get the major type and the number. Number could be length of more bytes or the value depending on the major type
693 // nAdditionalInfo is an encoding of the length of the uNumber and is needed to decode floats and doubles
694 int uMajorType;
695 uint64_t uNumber;
696 uint8_t uAdditionalInfo;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800697
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700698 nReturn = DecodeTypeAndNumber(pUInBuf, &uMajorType, &uNumber, &uAdditionalInfo);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800699
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700700 // Error out here if we got into trouble on the type and number.
701 // The code after this will not work if the type and number is not good.
702 if(nReturn)
703 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800704
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530705 memset(pDecodedItem, 0, sizeof(QCBORItem));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800706
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700707 // At this point the major type and the value are valid. We've got the type and the number that
708 // starts every CBOR data item.
709 switch (uMajorType) {
710 case CBOR_MAJOR_TYPE_POSITIVE_INT: // Major type 0
711 case CBOR_MAJOR_TYPE_NEGATIVE_INT: // Major type 1
712 nReturn = DecodeInteger(uMajorType, uNumber, pDecodedItem);
713 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800714
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700715 case CBOR_MAJOR_TYPE_BYTE_STRING: // Major type 2
716 case CBOR_MAJOR_TYPE_TEXT_STRING: // Major type 3
717 if(uAdditionalInfo == LEN_IS_INDEFINITE) {
718 pDecodedItem->uDataType = (uMajorType == CBOR_MAJOR_TYPE_BYTE_STRING) ? QCBOR_TYPE_BYTE_STRING : QCBOR_TYPE_TEXT_STRING;
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530719 pDecodedItem->val.string = (UsefulBufC){NULL, SIZE_MAX};
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700720 } else {
721 nReturn = DecodeBytes(pAlloc, uMajorType, uNumber, pUInBuf, pDecodedItem);
722 }
723 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800724
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700725 case CBOR_MAJOR_TYPE_ARRAY: // Major type 4
726 case CBOR_MAJOR_TYPE_MAP: // Major type 5
727 // Record the number of items in the array or map
728 if(uNumber > QCBOR_MAX_ITEMS_IN_ARRAY) {
729 nReturn = QCBOR_ERR_ARRAY_TOO_LONG;
730 goto Done;
731 }
732 if(uAdditionalInfo == LEN_IS_INDEFINITE) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530733 pDecodedItem->val.uCount = UINT16_MAX; // Indicate indefinite length
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700734 } else {
735 pDecodedItem->val.uCount = (uint16_t)uNumber; // type conversion OK because of check above
736 }
737 pDecodedItem->uDataType = uMajorType; // C preproc #if above makes sure constants align
738 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800739
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700740 case CBOR_MAJOR_TYPE_OPTIONAL: // Major type 6, optional prepended tags
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700741 pDecodedItem->val.uTagV = uNumber;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700742 pDecodedItem->uDataType = QCBOR_TYPE_OPTTAG;
743 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800744
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700745 case CBOR_MAJOR_TYPE_SIMPLE: // Major type 7, float, double, true, false, null...
746 nReturn = DecodeSimple(uAdditionalInfo, uNumber, pDecodedItem);
747 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800748
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700749 default: // Should never happen because DecodeTypeAndNumber() should never return > 7
750 nReturn = QCBOR_ERR_UNSUPPORTED;
751 break;
752 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800753
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700754Done:
755 return nReturn;
756}
757
758
759
760/*
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800761 This layer deals with indefinite length strings. It pulls all the
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700762 individual chunk items together into one QCBORItem using the
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530763 string allocator.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800764
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530765 Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700766 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700767static inline QCBORError GetNext_FullItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700768{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700769 // Stack usage; int/ptr 2 UsefulBuf 2 QCBORItem -- 96
Laurence Lundblade30816f22018-11-10 13:40:22 +0700770 QCBORError nReturn;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +0530771 QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)me->pStringAllocator;
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530772 UsefulBufC FullString = NULLUsefulBufC;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800773
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700774 nReturn = GetNext_Item(&(me->InBuf), pDecodedItem, me->bStringAllocateAll ? pAlloc: NULL);
775 if(nReturn) {
776 goto Done;
777 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800778
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700779 // To reduce code size by removing support for indefinite length strings, the
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530780 // code in this function from here down can be eliminated. Run tests, except
781 // indefinite length string tests, to be sure all is OK if this is removed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800782
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800783 // Only do indefinite length processing on strings
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700784 if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING && pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) {
785 goto Done; // no need to do any work here on non-string types
786 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800787
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800788 // Is this a string with an indefinite length?
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530789 if(pDecodedItem->val.string.len != SIZE_MAX) {
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800790 goto Done; // length is not indefinite, so no work to do here
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700791 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800792
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530793 // Can't do indefinite length strings without a string allocator
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700794 if(pAlloc == NULL) {
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700795 nReturn = QCBOR_ERR_NO_STRING_ALLOCATOR;
796 goto Done;
797 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800798
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700799 // There is an indefinite length string to work on...
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800800 // Track which type of string it is
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700801 const uint8_t uStringType = pDecodedItem->uDataType;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800802
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700803 // Loop getting chunk of indefinite string
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700804 for(;;) {
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700805 // Get item for next chunk
806 QCBORItem StringChunkItem;
807 // NULL passed to never string alloc chunk of indefinite length strings
808 nReturn = GetNext_Item(&(me->InBuf), &StringChunkItem, NULL);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700809 if(nReturn) {
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700810 break; // Error getting the next chunk
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700811 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800812
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530813 // See if it is a marker at end of indefinite length string
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700814 if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) {
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800815 // String is complete
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700816 pDecodedItem->val.string = FullString;
Laurence Lundblade57dd1442018-10-15 20:26:28 +0530817 pDecodedItem->uDataAlloc = 1;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700818 break;
819 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800820
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700821 // Match data type of chunk to type at beginning.
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530822 // Also catches error of other non-string types that don't belong.
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700823 if(StringChunkItem.uDataType != uStringType) {
Laurence Lundblade30816f22018-11-10 13:40:22 +0700824 nReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700825 break;
826 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800827
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530828 // Alloc new buffer or expand previously allocated buffer so it can fit
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530829 UsefulBuf NewMem = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext,
830 UNCONST_POINTER(FullString.ptr),
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700831 FullString.len + StringChunkItem.val.string.len);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700832 if(UsefulBuf_IsNULL(NewMem)) {
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530833 // Allocation of memory for the string failed
Laurence Lundblade30816f22018-11-10 13:40:22 +0700834 nReturn = QCBOR_ERR_STRING_ALLOCATE;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700835 break;
836 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800837
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700838 // Copy new string chunk at the end of string so far.
839 FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700840 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800841
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700842Done:
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530843 if(pAlloc && nReturn && !UsefulBuf_IsNULLC(FullString)) {
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700844 // Getting item failed, clean up the allocated memory
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530845 (pAlloc->fFree)(pAlloc->pAllocaterContext, UNCONST_POINTER(FullString.ptr));
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700846 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800847
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700848 return nReturn;
849}
850
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700851
852/*
853 Returns an error if there was something wrong with the optional item or it couldn't
854 be handled.
855 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700856static QCBORError GetNext_TaggedItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem, QCBORTagListOut *pTags)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700857{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700858 // Stack usage: int/ptr: 3 -- 24
Laurence Lundblade30816f22018-11-10 13:40:22 +0700859 QCBORError nReturn;
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700860 uint64_t uTagBits = 0;
861 if(pTags) {
862 pTags->uNumUsed = 0;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700863 }
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700864
865 for(;;) {
866 nReturn = GetNext_FullItem(me, pDecodedItem);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700867 if(nReturn) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700868 goto Done; // Error out of the loop
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700869 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800870
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700871 if(pDecodedItem->uDataType != QCBOR_TYPE_OPTTAG) {
872 // Successful exit from loop; maybe got some tags, maybe not
873 pDecodedItem->uTagBits = uTagBits;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700874 break;
875 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800876
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700877 uint8_t uTagBitIndex;
878 // Tag was mapped, tag was not mapped, error with tag list
879 switch(TagMapper_Lookup(me->pCallerConfiguredTagList, pDecodedItem->val.uTagV, &uTagBitIndex)) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800880
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700881 case QCBOR_SUCCESS:
882 // Successfully mapped the tag
883 uTagBits |= 0x01ULL << uTagBitIndex;
884 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800885
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700886 case QCBOR_ERR_BAD_OPT_TAG:
887 // Tag is not recognized. Do nothing
888 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800889
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700890 default:
891 // Error Condition
892 goto Done;
893 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800894
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700895 if(pTags) {
896 // Caller wants all tags recorded in the provided buffer
897 if(pTags->uNumUsed >= pTags->uNumAllocated) {
898 nReturn = QCBOR_ERR_TOO_MANY_TAGS;
899 goto Done;
900 }
901 pTags->puTags[pTags->uNumUsed] = pDecodedItem->val.uTagV;
902 pTags->uNumUsed++;
903 }
904 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800905
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700906 switch(pDecodedItem->uTagBits & TAG_MAPPER_FIRST_FOUR) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700907 case 0:
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700908 // No tags at all or none we know about. Nothing to do.
909 // This is part of the pass-through path of this function
910 // that will mostly be taken when decoding any item.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700911 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800912
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700913 case QCBOR_TAGFLAG_DATE_STRING:
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700914 nReturn = DecodeDateString(pDecodedItem);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700915 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800916
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700917 case QCBOR_TAGFLAG_DATE_EPOCH:
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700918 nReturn = DecodeDateEpoch(pDecodedItem);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700919 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800920
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700921 case QCBOR_TAGFLAG_POS_BIGNUM:
922 case QCBOR_TAGFLAG_NEG_BIGNUM:
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700923 nReturn = DecodeBigNum(pDecodedItem);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700924 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800925
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700926 default:
927 // Encountering some mixed up CBOR like something that
928 // is tagged as both a string and integer date.
Laurence Lundblade30816f22018-11-10 13:40:22 +0700929 nReturn = QCBOR_ERR_BAD_OPT_TAG;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700930 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800931
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700932Done:
933 return nReturn;
934}
935
936
937/*
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800938 This layer takes care of map entries. It combines the label and data items into one QCBORItem.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700939 */
Laurence Lundblade30816f22018-11-10 13:40:22 +0700940static inline QCBORError GetNext_MapEntry(QCBORDecodeContext *me, QCBORItem *pDecodedItem, QCBORTagListOut *pTags)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700941{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700942 // Stack use: int/ptr 1, QCBORItem -- 56
Laurence Lundblade30816f22018-11-10 13:40:22 +0700943 QCBORError nReturn = GetNext_TaggedItem(me, pDecodedItem, pTags);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700944 if(nReturn)
945 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800946
Laurence Lundblade742df4a2018-10-13 20:07:17 +0800947 if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700948 // Break can't be a map entry
Laurence Lundblade742df4a2018-10-13 20:07:17 +0800949 goto Done;
950 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800951
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800952 if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) {
953 // In a map and caller wants maps decoded, not treated as arrays
954
955 if(DecodeNesting_TypeIsMap(&(me->nesting))) {
956 // If in a map and the right decoding mode, get the label
957
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900958 // Get the next item which will be the real data; Item will be the label
959 QCBORItem LabelItem = *pDecodedItem;
960 nReturn = GetNext_TaggedItem(me, pDecodedItem, pTags);
961 if(nReturn)
962 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800963
Laurence Lundblade57dd1442018-10-15 20:26:28 +0530964 pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900965
966 if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) {
967 // strings are always good labels
968 pDecodedItem->label.string = LabelItem.val.string;
969 pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING;
970 } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) {
971 // It's not a string and we only want strings, probably for easy translation to JSON
972 nReturn = QCBOR_ERR_MAP_LABEL_TYPE;
973 goto Done;
974 } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) {
975 pDecodedItem->label.int64 = LabelItem.val.int64;
976 pDecodedItem->uLabelType = QCBOR_TYPE_INT64;
977 } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) {
978 pDecodedItem->label.uint64 = LabelItem.val.uint64;
979 pDecodedItem->uLabelType = QCBOR_TYPE_UINT64;
980 } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) {
981 pDecodedItem->label.string = LabelItem.val.string;
982 pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc;
983 pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING;
984 } else {
985 // label is not an int or a string. It is an arrray
986 // or a float or such and this implementation doesn't handle that.
987 // Also, tags on labels are ignored.
988 nReturn = QCBOR_ERR_MAP_LABEL_TYPE;
989 goto Done;
990 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800991 }
992 } else {
993 if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) {
994 // Decoding a map as an array
995 pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY;
996 pDecodedItem->val.uCount *= 2;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700997 }
998 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800999
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001000Done:
1001 return nReturn;
1002}
1003
1004
1005/*
1006 Public function, see header qcbor.h file
1007 */
Laurence Lundblade30816f22018-11-10 13:40:22 +07001008QCBORError QCBORDecode_GetNextWithTags(QCBORDecodeContext *me, QCBORItem *pDecodedItem, QCBORTagListOut *pTags)
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001009{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001010 // Stack ptr/int: 2, QCBORItem : 64
1011
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05301012 // The public entry point for fetching and parsing the next QCBORItem.
1013 // All the CBOR parsing work is here and in subordinate calls.
Laurence Lundblade30816f22018-11-10 13:40:22 +07001014 QCBORError nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001015
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001016 nReturn = GetNext_MapEntry(me, pDecodedItem, pTags);
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001017 if(nReturn) {
1018 goto Done;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001019 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05301020
1021 // Break ending arrays/maps are always processed at the end of this function.
1022 // They should never show up here.
Laurence Lundblade6de37062018-10-15 12:22:42 +05301023 if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05301024 nReturn = QCBOR_ERR_BAD_BREAK;
1025 goto Done;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05301026 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001027
Laurence Lundblade6de37062018-10-15 12:22:42 +05301028 // Record the nesting level for this data item before processing any of
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05301029 // decrementing and descending.
Laurence Lundblade6de37062018-10-15 12:22:42 +05301030 pDecodedItem->uNestingLevel = DecodeNesting_GetLevel(&(me->nesting));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001031
Laurence Lundblade6de37062018-10-15 12:22:42 +05301032 // Process the item just received for descent or decrement, and
1033 // ascent if decrements are enough to close out a definite length array/map
Laurence Lundblade3a760b02018-10-08 13:46:03 +08001034 if(IsMapOrArray(pDecodedItem->uDataType)) {
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001035 // If the new item is array or map, the nesting level descends
Laurence Lundblade3a760b02018-10-08 13:46:03 +08001036 nReturn = DecodeNesting_Descend(&(me->nesting), pDecodedItem);
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001037 // Maps and arrays do count in as items in the map/array that encloses
1038 // them so a decrement needs to be done for them too, but that is done
1039 // only when all the items in them have been processed, not when they
1040 // are opened.
1041 } else {
1042 // Decrement the count of items in the enclosing map/array
1043 // If the count in the enclosing map/array goes to zero, that
Laurence Lundblade6de37062018-10-15 12:22:42 +05301044 // triggers a decrement in the map/array above that and
1045 // an ascend in nesting level.
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001046 DecodeNesting_DecrementCount(&(me->nesting));
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001047 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05301048 if(nReturn) {
1049 goto Done;
1050 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001051
Laurence Lundblade6de37062018-10-15 12:22:42 +05301052 // For indefinite length maps/arrays, looking at any and
1053 // all breaks that might terminate them. The equivalent
1054 // for definite length maps/arrays happens in
1055 // DecodeNesting_DecrementCount().
1056 if(DecodeNesting_IsNested(&(me->nesting)) && DecodeNesting_IsIndefiniteLength(&(me->nesting))) {
1057 while(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) {
1058 // Peek forward one item to see if it is a break.
1059 QCBORItem Peek;
1060 size_t uPeek = UsefulInputBuf_Tell(&(me->InBuf));
1061 nReturn = GetNext_Item(&(me->InBuf), &Peek, NULL);
1062 if(nReturn) {
1063 goto Done;
1064 }
1065 if(Peek.uDataType != QCBOR_TYPE_BREAK) {
1066 // It is not a break, rewind so it can be processed normally.
1067 UsefulInputBuf_Seek(&(me->InBuf), uPeek);
1068 break;
1069 }
1070 // It is a break. Ascend one nesting level.
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05301071 // The break is consumed.
Laurence Lundblade6de37062018-10-15 12:22:42 +05301072 nReturn = DecodeNesting_BreakAscend(&(me->nesting));
1073 if(nReturn) {
1074 // break occured outside of an indefinite length array/map
1075 goto Done;
1076 }
1077 }
1078 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001079
Laurence Lundblade6de37062018-10-15 12:22:42 +05301080 // Tell the caller what level is next. This tells them what maps/arrays
1081 // were closed out and makes it possible for them to reconstruct
1082 // the tree with just the information returned by GetNext
1083 pDecodedItem->uNextNestLevel = DecodeNesting_GetLevel(&(me->nesting));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001084
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001085Done:
1086 return nReturn;
1087}
1088
1089
Laurence Lundblade30816f22018-11-10 13:40:22 +07001090QCBORError QCBORDecode_GetNext(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001091{
1092 return QCBORDecode_GetNextWithTags(me, pDecodedItem, NULL);
1093}
1094
1095
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001096/*
Laurence Lundblade6de37062018-10-15 12:22:42 +05301097 Decoding items is done in 5 layered functions, one calling the
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301098 next one down. If a layer has no work to do for a particular item
1099 it returns quickly.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001100
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301101 - QCBORDecode_GetNext -- The top layer manages the beginnings and
1102 ends of maps and arrays. It tracks descending into and ascending
Laurence Lundblade6de37062018-10-15 12:22:42 +05301103 out of maps/arrays. It processes all breaks that terminate
1104 maps and arrays.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001105
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301106 - GetNext_MapEntry -- This handles the combining of two
1107 items, the label and the data, that make up a map entry.
1108 It only does work on maps. It combines the label and data
1109 items into one labeled item.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001110
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301111 - GetNext_TaggedItem -- This handles the type 6 tagged items.
1112 It accumulates all the tags and combines them with the following
1113 non-tagged item. If the tagged item is something that is understood
1114 like a date, the decoding of that item is invoked.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001115
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301116 - GetNext_FullItem -- This assembles the sub items that make up
1117 an indefinte length string into one string item. It uses the
Laurence Lundblade6de37062018-10-15 12:22:42 +05301118 string allocater to create contiguous space for the item. It
1119 processes all breaks that are part of indefinite length strings.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001120
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301121 - GetNext_Item -- This gets and decodes the most atomic
1122 item in CBOR, the thing with an initial byte containing
1123 the major type.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001124
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001125 Roughly this takes 300 bytes of stack for vars. Need to
1126 evaluate this more carefully and correctly.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001127
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301128 */
1129
1130
1131/*
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001132 Public function, see header qcbor.h file
1133 */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001134int QCBORDecode_IsTagged(QCBORDecodeContext *me, const QCBORItem *pItem, uint64_t uTag)
1135{
1136 const QCBORTagListIn *pCallerConfiguredTagMap = me->pCallerConfiguredTagList;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001137
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001138 uint8_t uTagBitIndex;
1139 // Do not care about errors in pCallerConfiguredTagMap here. They are
1140 // caught during GetNext() before this is called.
1141 if(TagMapper_Lookup(pCallerConfiguredTagMap, uTag, &uTagBitIndex)) {
1142 return 0;
1143 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001144
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001145 const uint64_t uTagBit = 0x01ULL << uTagBitIndex;
1146 return (uTagBit & pItem->uTagBits) != 0;
1147}
1148
1149
1150/*
1151 Public function, see header qcbor.h file
1152 */
Laurence Lundblade30816f22018-11-10 13:40:22 +07001153QCBORError QCBORDecode_Finish(QCBORDecodeContext *me)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001154{
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001155 int nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001156
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001157 // Error out if all the maps/arrays are not closed out
1158 if(DecodeNesting_IsNested(&(me->nesting))) {
1159 nReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN;
1160 goto Done;
1161 }
1162
1163 // Error out if not all the bytes are consumed
1164 if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) {
1165 nReturn = QCBOR_ERR_EXTRA_BYTES;
1166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001167
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001168Done:
Laurence Lundblade6de37062018-10-15 12:22:42 +05301169 // Call the destructor for the string allocator if there is one.
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001170 // Always called, even if there are errors; always have to clean up
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001171 if(me->pStringAllocator) {
1172 QCBORStringAllocator *pAllocator = (QCBORStringAllocator *)me->pStringAllocator;
1173 if(pAllocator->fDestructor) {
1174 (pAllocator->fDestructor)(pAllocator->pAllocaterContext);
1175 }
1176 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001177
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001178 return nReturn;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001179}
1180
1181
1182
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001183/*
1184
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001185Decoder errors handled in this file
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001186
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001187 - Hit end of input before it was expected while decoding type and number QCBOR_ERR_HIT_END
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001188
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001189 - negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001190
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001191 - Hit end of input while decoding a text or byte string QCBOR_ERR_HIT_END
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001192
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001193 - Encountered conflicting tags -- e.g., an item is tagged both a date string and an epoch date QCBOR_ERR_UNSUPPORTED
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001194
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001195 - Encontered an array or mapp that has too many items QCBOR_ERR_ARRAY_TOO_LONG
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001196
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001197 - Encountered array/map nesting that is too deep QCBOR_ERR_ARRAY_NESTING_TOO_DEEP
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001198
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001199 - An epoch date > INT64_MAX or < INT64_MIN was encountered QCBOR_ERR_DATE_OVERFLOW
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001200
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001201 - The type of a map label is not a string or int QCBOR_ERR_MAP_LABEL_TYPE
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001202
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001203 - Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001204
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001205 */
1206
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001207
1208
Laurence Lundbladef6531662018-12-04 10:42:22 +09001209
1210/*
1211 This is a very primitive memory allocator. It does not track individual
1212 allocations, only a high-water mark. A free or reallotcation must be of
1213 the last chunk allocated.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001214
Laurence Lundbladef6531662018-12-04 10:42:22 +09001215 All of this following code will get dead-stripped if QCBORDecode_SetMemPool()
1216 is not called.
1217 */
1218
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001219typedef struct {
1220 QCBORStringAllocator StringAllocator;
Laurence Lundbladef6531662018-12-04 10:42:22 +09001221 uint8_t *pStart; // First byte that can be allocated
1222 uint8_t *pEnd; // One past the last byte that can be allocated
1223 uint8_t *pFree; // Where the next free chunk is
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001224} MemPool;
1225
1226
1227/*
Laurence Lundbladef6531662018-12-04 10:42:22 +09001228 Internal function for an allocation
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001229
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001230 Code Reviewers: THIS FUNCTION DOES POINTER MATH
1231 */
1232static UsefulBuf MemPool_Alloc(void *ctx, void *pMem, size_t uNewSize)
1233{
Laurence Lundbladef6531662018-12-04 10:42:22 +09001234 MemPool *me = (MemPool *)ctx;
1235 void *pReturn = NULL;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001236
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001237 if(pMem) {
1238 // Realloc case
Laurence Lundbladef6531662018-12-04 10:42:22 +09001239 // This check will work even if uNewSize is a super-large value like UINT64_MAX
1240 if((uNewSize <= (size_t)(me->pEnd - (uint8_t *)pMem)) && ((uint8_t *)pMem >= me->pStart)) {
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001241 me->pFree = (uint8_t *)pMem + uNewSize;
Laurence Lundbladef6531662018-12-04 10:42:22 +09001242 pReturn = pMem;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001243 }
1244 } else {
1245 // New chunk case
Laurence Lundbladef6531662018-12-04 10:42:22 +09001246 // This check will work even if uNewSize is a super large value like UINT64_MAX
1247 if(uNewSize <= (size_t)(me->pEnd - me->pFree)) {
1248 pReturn = me->pFree;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001249 me->pFree += uNewSize;
1250 }
1251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001252
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001253 return (UsefulBuf){pReturn, uNewSize};
1254}
1255
Laurence Lundbladef6531662018-12-04 10:42:22 +09001256/*
1257 Internal function to free memory
1258 */
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001259static void MemPool_Free(void *ctx, void *pOldMem)
1260{
1261 MemPool *me = (MemPool *)ctx;
Laurence Lundbladef6531662018-12-04 10:42:22 +09001262 me->pFree = pOldMem;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001263}
1264
Laurence Lundbladef6531662018-12-04 10:42:22 +09001265/*
1266 Public function, see header qcbor.h file
1267 */
Laurence Lundblade30816f22018-11-10 13:40:22 +07001268QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *me, UsefulBuf Pool, bool bAllStrings)
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001269{
Laurence Lundbladef6531662018-12-04 10:42:22 +09001270 // The first bytes of the Pool passed in are used
1271 // as the context (vtable of sorts) for the memory pool
1272 // allocator.
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001273 if(Pool.len < sizeof(MemPool)+1) {
Laurence Lundblade30816f22018-11-10 13:40:22 +07001274 return QCBOR_ERR_BUFFER_TOO_SMALL;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001275 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001276
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001277 MemPool *pMP = (MemPool *)Pool.ptr;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001278
Laurence Lundbladef6531662018-12-04 10:42:22 +09001279 // Fill in the "vtable"
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001280 pMP->StringAllocator.fAllocate = MemPool_Alloc;
1281 pMP->StringAllocator.fFree = MemPool_Free;
1282 pMP->StringAllocator.fDestructor = NULL;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001283
Laurence Lundbladef6531662018-12-04 10:42:22 +09001284 // Set up the pointers to the memory to be allocated
Laurence Lundblade570fab52018-10-13 18:28:27 +08001285 pMP->pStart = (uint8_t *)Pool.ptr + sizeof(MemPool);
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001286 pMP->pFree = pMP->pStart;
Laurence Lundblade570fab52018-10-13 18:28:27 +08001287 pMP->pEnd = (uint8_t *)Pool.ptr + Pool.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001288
Laurence Lundbladef6531662018-12-04 10:42:22 +09001289 // More book keeping of context
1290 pMP->StringAllocator.pAllocaterContext = pMP;
1291 me->pStringAllocator = pMP;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001292
Laurence Lundbladef6531662018-12-04 10:42:22 +09001293 // The flag indicating when to use the allocator
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001294 me->bStringAllocateAll = bAllStrings;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001295
Laurence Lundblade30816f22018-11-10 13:40:22 +07001296 return QCBOR_SUCCESS;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001297}
1298
Laurence Lundbladef6531662018-12-04 10:42:22 +09001299
1300/*
1301 Extra little hook to make MemPool testing work right
1302 without adding any code size or overhead to non-test
1303 uses. This will get dead-stripped for non-test use.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001304
1305 This is not a public function.
Laurence Lundbladef6531662018-12-04 10:42:22 +09001306 */
1307size_t MemPoolTestHook_GetPoolSize(void *ctx)
1308{
1309 MemPool *me = (MemPool *)ctx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001310
Laurence Lundbladef6531662018-12-04 10:42:22 +09001311 return me->pEnd - me->pStart;
1312}
1313