blob: bc5842ab9143dae452836baf0395a77f0ce3f552 [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.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 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 Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundblade624405d2018-09-18 20:10:47 -070032
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080033
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#include "qcbor/qcbor_decode.h"
Laurence Lundblade12d32c52018-09-19 11:25:27 -070035#include "ieee754.h"
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070036
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070037
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +053038/*
39 This casts away the const-ness of a pointer, usually so it can be
40 freed or realloced.
41 */
42#define UNCONST_POINTER(ptr) ((void *)(ptr))
43
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070044
Laurence Lundbladeb69cad72018-09-13 11:09:01 -070045
Laurence Lundbladeee851742020-01-08 08:37:05 -080046/*===========================================================================
47 DecodeNesting -- Functions for tracking array/map nesting when decoding
48
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080049 See qcbor/qcbor_decode.h for definition of the object
50 used here: QCBORDecodeNesting
Laurence Lundbladeee851742020-01-08 08:37:05 -080051 ===========================================================================*/
52
Laurence Lundblade9c905e82020-04-25 11:31:38 -070053
54
55/*
56The main mode of decoding is a pre-order travesal of the tree of leaves (numbers, strings...)
57formed by intermediate nodes (arrays and maps). The cursor for the traversal
58 is the byte offset in the encoded input and a leaf counter for definite
59 length maps and arrays. Indefinite length maps and arrays are handled
60 by look ahead for the break.
61
62 The view presented to the caller has tags, labels and the chunks of
63 indefinite length strings aggregated into one decorated data item.
64
65The caller understands the nesting level in pre-order traversal by
66 the fact that a data item that is a map or array is presented to
67 the caller when it is first encountered in the pre-order traversal and that all data items are presented with its nesting level
68 and the nesting level of the next item.
69
70 The caller traverse maps and arrays in a special mode that often more convenient
71 that tracking by nesting level. When an array or map is expected or encountered
72 the EnterMap or EnteryArray can be called.
73
74 When entering a map or array like this, the cursor points to the first
75 item in the map or array. When exiting, it points to the item after
76 the map or array, regardless of whether the items in the map or array were
77 all traversed.
78
79 When in a map or array, the cursor functions as normal, but traversal
80 cannot go past the end of the map or array that was entered. If this
81 is attempted the QCBOR_ERR_NO_MORE_ITEMS error is returned. To
82 go past the end of the map or array ExitMap() or ExitArray() must
83 be called. It can be called any time regardless of the position
84 of the cursor.
85
86 When a map is entered, a special function allows fetching data items
87 by label. This call will traversal the whole map looking for the
88 labeled item. The whole map is traversed so as to detect duplicates.
89 This type of fetching items does not affect the normal traversal
90 cursor.
91
92
93
94
95
96
97
98
99
100
101When a data item is presented to the caller, the nesting level of the data
102 item is presented along with the nesting level of the item that would be
103 next consumed.
104
105
106
107
108
109
110
111
112
113 */
114
Laurence Lundblade6b249302020-04-30 12:38:12 -0700115inline static bool
116// TODO: test Map as array better?
Laurence Lundbladeee851742020-01-08 08:37:05 -0800117IsMapOrArray(uint8_t uDataType)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700118{
Laurence Lundblade6b249302020-04-30 12:38:12 -0700119 return uDataType == QCBOR_TYPE_MAP ||
120 uDataType == QCBOR_TYPE_ARRAY ||
121 uDataType == QCBOR_TYPE_MAP_AS_ARRAY;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700122}
123
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700124
125inline static uint8_t
126DecodeNesting_GetLevel(const QCBORDecodeNesting *pNesting)
127{
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700128 const ptrdiff_t nLevel = pNesting->pCurrent - &(pNesting->pMapsAndArrays[0]);
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700129 // Check in DecodeNesting_Descend and never having
130 // QCBOR_MAX_ARRAY_NESTING > 255 gaurantees cast is safe
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700131 return (uint8_t)nLevel;
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700132}
133
134
Laurence Lundblade0a042a92020-06-12 14:09:50 -0700135inline static bool DecodeNesting_InBoundedMode(const QCBORDecodeNesting *pNesting)
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700136{
137 return pNesting->pCurrent->uType & QCBOR_NEST_TYPE_IS_BOUND;
138}
139
140/*inline static bool IsArray(const QCBORDecodeNesting *pNesting)
141{
142 const unsigned uIndex = DecodeNesting_GetLevel(pNesting);
143
144 return (0x01ULL << ((uIndex * 3) + 1)) & pNesting->uTypeBitMap;
145}
146
147inline static bool IsBstr(const QCBORDecodeNesting *pNesting)
148{
149 const unsigned uIndex = DecodeNesting_GetLevel(pNesting);
150
151 return (0x01ULL << ((uIndex * 3) + 2)) & pNesting->uTypeBitMap;
152}*/
153
154
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700155inline static bool
156DecodeNesting_IsAtTop(const QCBORDecodeNesting *pNesting)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700157{
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700158 if(pNesting->pCurrent == &(pNesting->pMapsAndArrays[0])) {
159 return true;
160 } else {
161 return false;
162 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700163}
164
Laurence Lundblade937ea812020-05-08 11:38:23 -0700165// Determine if at the end of a map or array while in map mode
Laurence Lundblade3f9ef042020-04-14 13:15:51 -0700166inline static bool
167DecodeNesting_AtEnd(const QCBORDecodeNesting *pNesting)
168{
Laurence Lundblade0a042a92020-06-12 14:09:50 -0700169 if(pNesting->pCurrentMap && DecodeNesting_InBoundedMode(pNesting)) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700170 if(pNesting->pCurrentMap->uCount == 0) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700171 // In map mode and consumed all items, so it is the end
172 return true;
173 } else {
174 // In map mode, all items not consumed, so it is NOT the end
175 return false;
176 }
177 } else {
Laurence Lundblade937ea812020-05-08 11:38:23 -0700178 // Not in map mode. The end is determined in other ways.
Laurence Lundblade3f9ef042020-04-14 13:15:51 -0700179 return false;
180 }
Laurence Lundblade3f9ef042020-04-14 13:15:51 -0700181}
182
183
Laurence Lundbladeee851742020-01-08 08:37:05 -0800184inline static int
185DecodeNesting_IsIndefiniteLength(const QCBORDecodeNesting *pNesting)
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700186{
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700187 return pNesting->pCurrent->uCount == UINT16_MAX;
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700188 //return pNesting->pCurrent->uType & QCBOR_NEST_TYPE_IS_INDEFINITE;
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700189}
190
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800191
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700192inline static uint8_t
Laurence Lundblade0a042a92020-06-12 14:09:50 -0700193DecodeNesting_GetBoundedModeLevel(QCBORDecodeNesting *pNesting)
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700194{
195 // Check in DecodeNesting_Descend and never having
196 // QCBOR_MAX_ARRAY_NESTING > 255 gaurantees cast is safe
197 return (uint8_t)(pNesting->pCurrentMap - &(pNesting->pMapsAndArrays[0]));
198}
199
Laurence Lundbladeee851742020-01-08 08:37:05 -0800200inline static int
201DecodeNesting_TypeIsMap(const QCBORDecodeNesting *pNesting)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700202{
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700203 if(DecodeNesting_IsAtTop(pNesting)) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700204 return 0;
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800206
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700207 return CBOR_MAJOR_TYPE_MAP == pNesting->pCurrent->uMajorType;
208}
209
Laurence Lundblade0a042a92020-06-12 14:09:50 -0700210
211// return 1 if closed out an array or map
Laurence Lundbladed8c82c52020-06-12 22:15:52 -0700212inline static void
213DecodeNesting_DecrementX(QCBORDecodeNesting *pNesting)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700214{
Laurence Lundblade0a042a92020-06-12 14:09:50 -0700215 pNesting->pCurrent->uCount--;
Laurence Lundbladed8c82c52020-06-12 22:15:52 -0700216}
Laurence Lundblade0a042a92020-06-12 14:09:50 -0700217
Laurence Lundbladed8c82c52020-06-12 22:15:52 -0700218inline static bool
219DecodeNesting_IsEndOfDefiniteLengthMapOrArray(QCBORDecodeNesting *pNesting)
220{
221 if(pNesting->pCurrent->uCount == 0) {
222 return true;
223 } else {
224 return false;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700225 }
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800226}
227
Laurence Lundblade0a042a92020-06-12 14:09:50 -0700228inline static void
229DecodeNesting_Ascend(QCBORDecodeNesting *pNesting)
230{
231 pNesting->pCurrent--;
232}
233
234
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700235
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700236
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -0700237inline static void
Laurence Lundbladed8c82c52020-06-12 22:15:52 -0700238DecodeNesting_EnterBoundedMode(QCBORDecodeNesting *pNesting, size_t uOffset)
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -0700239{
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700240 /* Have descended into this is called. The job here is just to mark it in bounded mode */
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700241 pNesting->pCurrentMap = pNesting->pCurrent;
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700242 pNesting->pCurrentMap->uType |= QCBOR_NEST_TYPE_IS_BOUND;
243 // Cast to uint32_t is safe because QCBOR restricts encoded input to < UINT32_MAX
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700244 pNesting->pCurrentMap->uOffset = (uint32_t)uOffset;
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -0700245}
246
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700247
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -0700248
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700249
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700250inline static QCBORError
Laurence Lundblade0a042a92020-06-12 14:09:50 -0700251DecodeNesting_Descend(QCBORDecodeNesting *pNesting, uint8_t uQCBORType, uint64_t uCount, uint32_t uEndOffset)
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700252{
253 QCBORError nReturn = QCBOR_SUCCESS;
254
255 if(uCount == 0) {
256 // Nothing to do for empty definite lenth arrays. They are just are
257 // effectively the same as an item that is not a map or array
258 goto Done;
259 // Empty indefinite length maps and arrays are handled elsewhere
260 }
261
262 // Error out if arrays is too long to handle
263 if(uCount != UINT16_MAX && uCount > QCBOR_MAX_ITEMS_IN_ARRAY) {
264 nReturn = QCBOR_ERR_ARRAY_TOO_LONG;
265 goto Done;
266 }
267
268 // Error out if nesting is too deep
269 if(pNesting->pCurrent >= &(pNesting->pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING])) {
270 nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
271 goto Done;
272 }
273
274 // The actual descend
275 pNesting->pCurrent++;
276
277 // Fill in the new level fully
278 pNesting->pCurrent->uMajorType = uQCBORType;
279 pNesting->pCurrent->uCount = (uint16_t)uCount;
280 pNesting->pCurrent->uSaveCount = (uint16_t)uCount;
281 pNesting->pCurrent->uEndOffset = uEndOffset;
282 pNesting->pCurrent->uMapMode = 0;
283
284Done:
285 return nReturn;;
286}
287
288
289
Laurence Lundbladeee851742020-01-08 08:37:05 -0800290inline static void
291DecodeNesting_Init(QCBORDecodeNesting *pNesting)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700292{
293 pNesting->pCurrent = &(pNesting->pMapsAndArrays[0]);
294}
295
296
Laurence Lundbladeb340ba72020-05-14 11:41:10 -0700297static void DecodeNesting_PrepareForMapSearch(QCBORDecodeNesting *pNesting, QCBORDecodeNesting *pSave)
298{
299 *pSave = *pNesting;
300 pNesting->pCurrent = pNesting->pCurrentMap;
301
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700302 if(!DecodeNesting_IsIndefiniteLength(pNesting)) {
Laurence Lundbladeb340ba72020-05-14 11:41:10 -0700303 pNesting->pCurrent->uCount = pNesting->pCurrent->uSaveCount;
304 }
305}
306
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700307static inline void DecodeNesting_RestoreFromMapSearch(QCBORDecodeNesting *pNesting, QCBORDecodeNesting *pSave)
Laurence Lundbladeb340ba72020-05-14 11:41:10 -0700308{
309 *pNesting = *pSave;
310}
311
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700312QCBORError DecodeNesting_EnterBstr(QCBORDecodeNesting *pNesting, uint32_t uEndOffset)
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700313{
314 QCBORError uReturn ;
315
316 // Error out if nesting is too deep
317 if(pNesting->pCurrent >= &(pNesting->pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING])) {
318 uReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
319 goto Done;
320 }
321
322 // The actual descend
323 pNesting->pCurrent++;
324
325 // Record a few details for this nesting level
326 pNesting->pCurrent->uMajorType = 1; // TODO the right value for a bstr
327 pNesting->pCurrent->uCount = 0xffff;
328 pNesting->pCurrent->uSaveCount = 0xffff;
329 pNesting->pCurrent->uType = 0;
330
331 uReturn = QCBOR_SUCCESS;
332
333Done:
334 return uReturn;
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700335}
336
Laurence Lundbladeb340ba72020-05-14 11:41:10 -0700337
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700338
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700339
Laurence Lundbladeee851742020-01-08 08:37:05 -0800340/*===========================================================================
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800341 QCBORStringAllocate -- STRING ALLOCATOR INVOCATION
342
343 The following four functions are pretty wrappers for invocation of
344 the string allocator supplied by the caller.
345
Laurence Lundbladeee851742020-01-08 08:37:05 -0800346 ===========================================================================*/
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800347
Laurence Lundbladeee851742020-01-08 08:37:05 -0800348static inline void
349StringAllocator_Free(const QCORInternalAllocator *pMe, void *pMem)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800350{
351 (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, 0);
352}
353
Laurence Lundbladeee851742020-01-08 08:37:05 -0800354// StringAllocator_Reallocate called with pMem NULL is
355// equal to StringAllocator_Allocate()
356static inline UsefulBuf
357StringAllocator_Reallocate(const QCORInternalAllocator *pMe,
358 void *pMem,
359 size_t uSize)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800360{
361 return (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, uSize);
362}
363
Laurence Lundbladeee851742020-01-08 08:37:05 -0800364static inline UsefulBuf
365StringAllocator_Allocate(const QCORInternalAllocator *pMe, size_t uSize)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800366{
367 return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize);
368}
369
Laurence Lundbladeee851742020-01-08 08:37:05 -0800370static inline void
371StringAllocator_Destruct(const QCORInternalAllocator *pMe)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800372{
373 if(pMe->pfAllocator) {
374 (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0);
375 }
376}
377
378
379
Laurence Lundbladeee851742020-01-08 08:37:05 -0800380/*===========================================================================
381 QCBORDecode -- The main implementation of CBOR decoding
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700382
Laurence Lundblade844bb5c2020-03-01 17:27:25 -0800383 See qcbor/qcbor_decode.h for definition of the object
384 used here: QCBORDecodeContext
Laurence Lundbladeee851742020-01-08 08:37:05 -0800385 ===========================================================================*/
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700386/*
387 Public function, see header file
388 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800389void QCBORDecode_Init(QCBORDecodeContext *me,
390 UsefulBufC EncodedCBOR,
391 QCBORDecodeMode nDecodeMode)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700392{
393 memset(me, 0, sizeof(QCBORDecodeContext));
394 UsefulInputBuf_Init(&(me->InBuf), EncodedCBOR);
Laurence Lundbladeee851742020-01-08 08:37:05 -0800395 // Don't bother with error check on decode mode. If a bad value is
396 // passed it will just act as if the default normal mode of 0 was set.
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700397 me->uDecodeMode = (uint8_t)nDecodeMode;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700398 DecodeNesting_Init(&(me->nesting));
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700399 for(int i = 0; i < QCBOR_NUM_MAPPED_TAGS; i++) {
400 me->auMappedTags[i] = 0xffff;
401 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700402}
403
404
405/*
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700406 Public function, see header file
407 */
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800408void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe,
409 QCBORStringAllocate pfAllocateFunction,
410 void *pAllocateContext,
411 bool bAllStrings)
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700412{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800413 pMe->StringAllocator.pfAllocator = pfAllocateFunction;
414 pMe->StringAllocator.pAllocateCxt = pAllocateContext;
415 pMe->bStringAllocateAll = bAllStrings;
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700416}
417
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800418
419/*
420 Public function, see header file
421 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800422void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *me,
423 const QCBORTagListIn *pTagList)
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700424{
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700425 // This does nothing now. It is retained for backwards compatibility
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700426}
427
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700428
429/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800430 This decodes the fundamental part of a CBOR data item, the type and
431 number
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800432
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700433 This is the Counterpart to InsertEncodedTypeAndNumber().
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800434
Laurence Lundbladeee851742020-01-08 08:37:05 -0800435 This does the network->host byte order conversion. The conversion
436 here also results in the conversion for floats in addition to that
437 for lengths, tags and integer values.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800438
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700439 This returns:
440 pnMajorType -- the major type for the item
Laurence Lundbladeee851742020-01-08 08:37:05 -0800441
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800442 puArgument -- the "number" which is used a the value for integers,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800443 tags and floats and length for strings and arrays
444
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800445 pnAdditionalInfo -- Pass this along to know what kind of float or
Laurence Lundbladeee851742020-01-08 08:37:05 -0800446 if length is indefinite
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800447
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800448 The int type is preferred to uint8_t for some variables as this
449 avoids integer promotions, can reduce code size and makes
450 static analyzers happier.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700451 */
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800452inline static QCBORError DecodeTypeAndNumber(UsefulInputBuf *pUInBuf,
453 int *pnMajorType,
454 uint64_t *puArgument,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800455 int *pnAdditionalInfo)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700456{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700457 QCBORError nReturn;
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800458
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700459 // Get the initial byte that every CBOR data item has
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800460 const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800461
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700462 // Break down the initial byte
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800463 const int nTmpMajorType = nInitialByte >> 5;
464 const int nAdditionalInfo = nInitialByte & 0x1f;
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800465
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800466 // Where the number or argument accumulates
467 uint64_t uArgument;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800468
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800469 if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) {
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700470 // Need to get 1,2,4 or 8 additional argument bytes. Map
471 // LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800472 static const uint8_t aIterate[] = {1,2,4,8};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800473
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800474 // Loop getting all the bytes in the argument
475 uArgument = 0;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800476 for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) {
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800477 // This shift and add gives the endian conversion
478 uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf);
479 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800480 } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) {
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800481 // The reserved and thus-far unused additional info values
482 nReturn = QCBOR_ERR_UNSUPPORTED;
483 goto Done;
484 } else {
485 // Less than 24, additional info is argument or 31, an indefinite length
486 // No more bytes to get
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800487 uArgument = (uint64_t)nAdditionalInfo;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700488 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800489
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700490 if(UsefulInputBuf_GetError(pUInBuf)) {
491 nReturn = QCBOR_ERR_HIT_END;
492 goto Done;
493 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800494
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700495 // All successful if we got here.
496 nReturn = QCBOR_SUCCESS;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800497 *pnMajorType = nTmpMajorType;
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800498 *puArgument = uArgument;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800499 *pnAdditionalInfo = nAdditionalInfo;
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800500
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700501Done:
502 return nReturn;
503}
504
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800505
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700506/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800507 CBOR doesn't explicitly specify two's compliment for integers but all
508 CPUs use it these days and the test vectors in the RFC are so. All
509 integers in the CBOR structure are positive and the major type
510 indicates positive or negative. CBOR can express positive integers
511 up to 2^x - 1 where x is the number of bits and negative integers
512 down to 2^x. Note that negative numbers can be one more away from
513 zero than positive. Stdint, as far as I can tell, uses two's
514 compliment to represent negative integers.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800515
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700516 See http://www.unix.org/whitepapers/64bit.html for reasons int isn't
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800517 used carefully here, and in particular why it isn't used in the interface.
518 Also see
519 https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers
520
521 Int is used for values that need less than 16-bits and would be subject
522 to integer promotion and complaining by static analyzers.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700523 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800524inline static QCBORError
525DecodeInteger(int nMajorType, uint64_t uNumber, QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700526{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700527 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800528
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700529 if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) {
530 if (uNumber <= INT64_MAX) {
531 pDecodedItem->val.int64 = (int64_t)uNumber;
532 pDecodedItem->uDataType = QCBOR_TYPE_INT64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800533
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700534 } else {
535 pDecodedItem->val.uint64 = uNumber;
536 pDecodedItem->uDataType = QCBOR_TYPE_UINT64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800537
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700538 }
539 } else {
540 if(uNumber <= INT64_MAX) {
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800541 // CBOR's representation of negative numbers lines up with the
542 // two-compliment representation. A negative integer has one
543 // more in range than a positive integer. INT64_MIN is
544 // equal to (-INT64_MAX) - 1.
545 pDecodedItem->val.int64 = (-(int64_t)uNumber) - 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700546 pDecodedItem->uDataType = QCBOR_TYPE_INT64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800547
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700548 } else {
549 // C can't represent a negative integer in this range
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000550 // so it is an error.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700551 nReturn = QCBOR_ERR_INT_OVERFLOW;
552 }
553 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800554
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700555 return nReturn;
556}
557
558// Make sure #define value line up as DecodeSimple counts on this.
559#if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE
560#error QCBOR_TYPE_FALSE macro value wrong
561#endif
562
563#if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE
564#error QCBOR_TYPE_TRUE macro value wrong
565#endif
566
567#if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL
568#error QCBOR_TYPE_NULL macro value wrong
569#endif
570
571#if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF
572#error QCBOR_TYPE_UNDEF macro value wrong
573#endif
574
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700575#if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK
576#error QCBOR_TYPE_BREAK macro value wrong
577#endif
578
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700579#if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT
580#error QCBOR_TYPE_DOUBLE macro value wrong
581#endif
582
583#if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT
584#error QCBOR_TYPE_FLOAT macro value wrong
585#endif
586
587/*
588 Decode true, false, floats, break...
589 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800590inline static QCBORError
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800591DecodeSimple(int nAdditionalInfo, uint64_t uNumber, QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700592{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700593 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800594
Laurence Lundbladeee851742020-01-08 08:37:05 -0800595 // uAdditionalInfo is 5 bits from the initial byte compile time checks
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800596 // above make sure uAdditionalInfo values line up with uDataType values.
597 // DecodeTypeAndNumber never returns a major type > 1f so cast is safe
598 pDecodedItem->uDataType = (uint8_t)nAdditionalInfo;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800599
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800600 switch(nAdditionalInfo) {
Laurence Lundbladeee851742020-01-08 08:37:05 -0800601 // No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they are
602 // caught before this is called.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800603
Laurence Lundbladecc2ed342018-09-22 17:29:55 -0700604 case HALF_PREC_FLOAT:
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700605 pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uNumber);
606 pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700607 break;
Laurence Lundbladecc2ed342018-09-22 17:29:55 -0700608 case SINGLE_PREC_FLOAT:
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700609 pDecodedItem->val.dfnum = (double)UsefulBufUtil_CopyUint32ToFloat((uint32_t)uNumber);
610 pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700611 break;
612 case DOUBLE_PREC_FLOAT:
613 pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uNumber);
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700614 pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700615 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800616
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700617 case CBOR_SIMPLEV_FALSE: // 20
618 case CBOR_SIMPLEV_TRUE: // 21
619 case CBOR_SIMPLEV_NULL: // 22
620 case CBOR_SIMPLEV_UNDEF: // 23
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700621 case CBOR_SIMPLE_BREAK: // 31
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700622 break; // nothing to do
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800623
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700624 case CBOR_SIMPLEV_ONEBYTE: // 24
625 if(uNumber <= CBOR_SIMPLE_BREAK) {
626 // This takes out f8 00 ... f8 1f which should be encoded as e0 … f7
Laurence Lundblade077475f2019-04-26 09:06:33 -0700627 nReturn = QCBOR_ERR_BAD_TYPE_7;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700628 goto Done;
629 }
Laurence Lundblade5e390822019-01-06 12:35:01 -0800630 /* FALLTHROUGH */
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700631 // fall through intentionally
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800632
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700633 default: // 0-19
634 pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE;
Laurence Lundbladeee851742020-01-08 08:37:05 -0800635 /*
636 DecodeTypeAndNumber will make uNumber equal to
637 uAdditionalInfo when uAdditionalInfo is < 24 This cast is
638 safe because the 2, 4 and 8 byte lengths of uNumber are in
639 the double/float cases above
640 */
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700641 pDecodedItem->val.uSimple = (uint8_t)uNumber;
642 break;
643 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800644
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700645Done:
646 return nReturn;
647}
648
649
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700650/*
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530651 Decode text and byte strings. Call the string allocator if asked to.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700652 */
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800653inline static QCBORError DecodeBytes(const QCORInternalAllocator *pAllocator,
654 int nMajorType,
655 uint64_t uStrLen,
656 UsefulInputBuf *pUInBuf,
657 QCBORItem *pDecodedItem)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700658{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700659 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800660
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800661 // CBOR lengths can be 64 bits, but size_t is not 64 bits on all CPUs.
662 // This check makes the casts to size_t below safe.
663
664 // 4 bytes less than the largest sizeof() so this can be tested by
665 // putting a SIZE_MAX length in the CBOR test input (no one will
666 // care the limit on strings is 4 bytes shorter).
667 if(uStrLen > SIZE_MAX-4) {
668 nReturn = QCBOR_ERR_STRING_TOO_LONG;
669 goto Done;
670 }
671
672 const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen);
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530673 if(UsefulBuf_IsNULLC(Bytes)) {
674 // Failed to get the bytes for this string item
675 nReturn = QCBOR_ERR_HIT_END;
676 goto Done;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700677 }
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530678
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800679 if(pAllocator) {
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530680 // We are asked to use string allocator to make a copy
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800681 UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen);
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530682 if(UsefulBuf_IsNULL(NewMem)) {
Laurence Lundblade30816f22018-11-10 13:40:22 +0700683 nReturn = QCBOR_ERR_STRING_ALLOCATE;
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530684 goto Done;
685 }
686 pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800687 pDecodedItem->uDataAlloc = 1;
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530688 } else {
689 // Normal case with no string allocator
690 pDecodedItem->val.string = Bytes;
691 }
Laurence Lundbladeee851742020-01-08 08:37:05 -0800692 const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING);
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800693 // Cast because ternary operator causes promotion to integer
694 pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING
695 : QCBOR_TYPE_TEXT_STRING);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800696
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530697Done:
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700698 return nReturn;
699}
700
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700701
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800702
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700703
704
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700705
706
Laurence Lundbladeee851742020-01-08 08:37:05 -0800707// Make sure the constants align as this is assumed by
708// the GetAnItem() implementation
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700709#if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY
710#error QCBOR_TYPE_ARRAY value not lined up with major type
711#endif
712#if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP
713#error QCBOR_TYPE_MAP value not lined up with major type
714#endif
715
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700716/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800717 This gets a single data item and decodes it including preceding
718 optional tagging. This does not deal with arrays and maps and nesting
719 except to decode the data item introducing them. Arrays and maps are
720 handled at the next level up in GetNext().
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800721
Laurence Lundbladeee851742020-01-08 08:37:05 -0800722 Errors detected here include: an array that is too long to decode,
723 hit end of buffer unexpectedly, a few forms of invalid encoded CBOR
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700724 */
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800725static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf,
726 QCBORItem *pDecodedItem,
727 const QCORInternalAllocator *pAllocator)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700728{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700729 QCBORError nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800730
Laurence Lundbladeee851742020-01-08 08:37:05 -0800731 /*
732 Get the major type and the number. Number could be length of more
733 bytes or the value depending on the major type nAdditionalInfo is
734 an encoding of the length of the uNumber and is needed to decode
735 floats and doubles
736 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800737 int nMajorType;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700738 uint64_t uNumber;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800739 int nAdditionalInfo;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800740
Laurence Lundblade4b09f632019-10-09 14:34:59 -0700741 memset(pDecodedItem, 0, sizeof(QCBORItem));
742
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800743 nReturn = DecodeTypeAndNumber(pUInBuf, &nMajorType, &uNumber, &nAdditionalInfo);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800744
Laurence Lundbladeee851742020-01-08 08:37:05 -0800745 // Error out here if we got into trouble on the type and number. The
746 // code after this will not work if the type and number is not good.
Laurence Lundblade3a6042e2019-06-28 19:58:04 -0700747 if(nReturn) {
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700748 goto Done;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -0700749 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800750
Laurence Lundbladeee851742020-01-08 08:37:05 -0800751 // At this point the major type and the value are valid. We've got
752 // the type and the number that starts every CBOR data item.
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800753 switch (nMajorType) {
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700754 case CBOR_MAJOR_TYPE_POSITIVE_INT: // Major type 0
755 case CBOR_MAJOR_TYPE_NEGATIVE_INT: // Major type 1
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800756 if(nAdditionalInfo == LEN_IS_INDEFINITE) {
Laurence Lundblade3a6042e2019-06-28 19:58:04 -0700757 nReturn = QCBOR_ERR_BAD_INT;
758 } else {
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800759 nReturn = DecodeInteger(nMajorType, uNumber, pDecodedItem);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -0700760 }
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700761 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800762
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700763 case CBOR_MAJOR_TYPE_BYTE_STRING: // Major type 2
764 case CBOR_MAJOR_TYPE_TEXT_STRING: // Major type 3
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800765 if(nAdditionalInfo == LEN_IS_INDEFINITE) {
766 const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING);
767 pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING
768 : QCBOR_TYPE_TEXT_STRING);
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530769 pDecodedItem->val.string = (UsefulBufC){NULL, SIZE_MAX};
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700770 } else {
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800771 nReturn = DecodeBytes(pAllocator, nMajorType, uNumber, pUInBuf, pDecodedItem);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700772 }
773 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800774
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700775 case CBOR_MAJOR_TYPE_ARRAY: // Major type 4
776 case CBOR_MAJOR_TYPE_MAP: // Major type 5
777 // Record the number of items in the array or map
778 if(uNumber > QCBOR_MAX_ITEMS_IN_ARRAY) {
779 nReturn = QCBOR_ERR_ARRAY_TOO_LONG;
780 goto Done;
781 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800782 if(nAdditionalInfo == LEN_IS_INDEFINITE) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530783 pDecodedItem->val.uCount = UINT16_MAX; // Indicate indefinite length
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700784 } else {
Laurence Lundbladeee851742020-01-08 08:37:05 -0800785 // type conversion OK because of check above
786 pDecodedItem->val.uCount = (uint16_t)uNumber;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700787 }
Laurence Lundbladeee851742020-01-08 08:37:05 -0800788 // C preproc #if above makes sure constants for major types align
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800789 // DecodeTypeAndNumber never returns a major type > 7 so cast is safe
790 pDecodedItem->uDataType = (uint8_t)nMajorType;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700791 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800792
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700793 case CBOR_MAJOR_TYPE_OPTIONAL: // Major type 6, optional prepended tags
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800794 if(nAdditionalInfo == LEN_IS_INDEFINITE) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700795 nReturn = QCBOR_ERR_BAD_INT;
796 } else {
797 pDecodedItem->val.uTagV = uNumber;
798 pDecodedItem->uDataType = QCBOR_TYPE_OPTTAG;
799 }
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700800 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800801
Laurence Lundbladeee851742020-01-08 08:37:05 -0800802 case CBOR_MAJOR_TYPE_SIMPLE:
803 // Major type 7, float, double, true, false, null...
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800804 nReturn = DecodeSimple(nAdditionalInfo, uNumber, pDecodedItem);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700805 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800806
Laurence Lundbladeee851742020-01-08 08:37:05 -0800807 default:
808 // Never happens because DecodeTypeAndNumber() should never return > 7
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700809 nReturn = QCBOR_ERR_UNSUPPORTED;
810 break;
811 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800812
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700813Done:
814 return nReturn;
815}
816
817
818
819/*
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800820 This layer deals with indefinite length strings. It pulls all the
Laurence Lundbladeee851742020-01-08 08:37:05 -0800821 individual chunk items together into one QCBORItem using the string
822 allocator.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800823
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530824 Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700825 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800826static inline QCBORError
827GetNext_FullItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700828{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700829 // Stack usage; int/ptr 2 UsefulBuf 2 QCBORItem -- 96
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -0700830
831 // Get pointer to string allocator. First use is to pass it to
832 // GetNext_Item() when option is set to allocate for *every* string.
833 // Second use here is to allocate space to coallese indefinite
834 // length string items into one.
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800835 const QCORInternalAllocator *pAllocator = me->StringAllocator.pfAllocator ?
836 &(me->StringAllocator) :
837 NULL;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800838
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -0700839 QCBORError nReturn;
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800840 nReturn = GetNext_Item(&(me->InBuf),
841 pDecodedItem,
842 me->bStringAllocateAll ? pAllocator: NULL);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700843 if(nReturn) {
844 goto Done;
845 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800846
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700847 // To reduce code size by removing support for indefinite length strings, the
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530848 // code in this function from here down can be eliminated. Run tests, except
849 // indefinite length string tests, to be sure all is OK if this is removed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800850
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800851 // Only do indefinite length processing on strings
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -0700852 const uint8_t uStringType = pDecodedItem->uDataType;
853 if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700854 goto Done; // no need to do any work here on non-string types
855 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800856
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800857 // Is this a string with an indefinite length?
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530858 if(pDecodedItem->val.string.len != SIZE_MAX) {
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800859 goto Done; // length is not indefinite, so no work to do here
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700860 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800861
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530862 // Can't do indefinite length strings without a string allocator
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800863 if(pAllocator == NULL) {
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700864 nReturn = QCBOR_ERR_NO_STRING_ALLOCATOR;
865 goto Done;
866 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800867
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700868 // Loop getting chunk of indefinite string
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -0700869 UsefulBufC FullString = NULLUsefulBufC;
870
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700871 for(;;) {
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700872 // Get item for next chunk
873 QCBORItem StringChunkItem;
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -0700874 // NULL string allocator passed here. Do not need to allocate
875 // chunks even if bStringAllocateAll is set.
Laurence Lundbladefae26bf2019-02-18 11:15:43 -0800876 nReturn = GetNext_Item(&(me->InBuf), &StringChunkItem, NULL);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700877 if(nReturn) {
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700878 break; // Error getting the next chunk
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700879 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800880
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530881 // See if it is a marker at end of indefinite length string
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700882 if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) {
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800883 // String is complete
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700884 pDecodedItem->val.string = FullString;
Laurence Lundblade57dd1442018-10-15 20:26:28 +0530885 pDecodedItem->uDataAlloc = 1;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700886 break;
887 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800888
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700889 // Match data type of chunk to type at beginning.
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530890 // Also catches error of other non-string types that don't belong.
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700891 // Also catches indefinite length strings inside indefinite length strings
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800892 if(StringChunkItem.uDataType != uStringType ||
893 StringChunkItem.val.string.len == SIZE_MAX) {
Laurence Lundblade30816f22018-11-10 13:40:22 +0700894 nReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700895 break;
896 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800897
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530898 // Alloc new buffer or expand previously allocated buffer so it can fit
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800899 // The first time throurgh FullString.ptr is NULL and this is
900 // equivalent to StringAllocator_Allocate()
901 UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator,
902 UNCONST_POINTER(FullString.ptr),
903 FullString.len + StringChunkItem.val.string.len);
904
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700905 if(UsefulBuf_IsNULL(NewMem)) {
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530906 // Allocation of memory for the string failed
Laurence Lundblade30816f22018-11-10 13:40:22 +0700907 nReturn = QCBOR_ERR_STRING_ALLOCATE;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700908 break;
909 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800910
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700911 // Copy new string chunk at the end of string so far.
912 FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700913 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800914
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800915 if(nReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) {
916 // Getting the item failed, clean up the allocated memory
917 StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr));
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700918 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800919
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -0700920Done:
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700921 return nReturn;
922}
923
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700924
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700925uint64_t ConvertTag(QCBORDecodeContext *me, uint16_t uTagVal) {
926 if(uTagVal < 0xfff0) {
927 return uTagVal;
928 } else {
929 // TODO constant and error check
930 int x = uTagVal - 0xfff0;
931 return me->auMappedTags[x];
932 }
933}
934
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700935/*
Laurence Lundblade59289e52019-12-30 13:44:37 -0800936 Gets all optional tag data items preceding a data item that is not an
937 optional tag and records them as bits in the tag map.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700938 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800939static QCBORError
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700940GetNext_TaggedItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700941{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700942 // Stack usage: int/ptr: 3 -- 24
Laurence Lundblade30816f22018-11-10 13:40:22 +0700943 QCBORError nReturn;
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700944
945 uint16_t auTags[QCBOR_MAX_TAGS_PER_ITEM] = {0xffff, 0xffff, 0xffff, 0xffff};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700946
Laurence Lundblade59289e52019-12-30 13:44:37 -0800947 // Loop fetching items until the item fetched is not a tag
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700948 for(;;) {
949 nReturn = GetNext_FullItem(me, pDecodedItem);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700950 if(nReturn) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700951 goto Done; // Error out of the loop
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700952 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800953
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700954 if(pDecodedItem->uDataType != QCBOR_TYPE_OPTTAG) {
955 // Successful exit from loop; maybe got some tags, maybe not
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700956 memcpy(pDecodedItem->uTags, auTags, sizeof(auTags));
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700957 break;
958 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800959
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700960 // Is there room for the tag in the tags list?
961 size_t uTagIndex;
962 for(uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) {
963 if(auTags[uTagIndex] == 0xffff) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700964 break;
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700965 }
966 }
967 if(uTagIndex >= QCBOR_MAX_TAGS_PER_ITEM) {
968 return 99; // TODO error code
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800970
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700971 // Is the tag > 16 bits?
972 if(pDecodedItem->val.uTagV > 0xffff) {
973 size_t uTagMapIndex;
974 // Is there room in the tag map?
975 for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) {
976 if(me->auMappedTags[uTagMapIndex] == 0xffff) {
977 break;
978 }
979 if(me->auMappedTags[uTagMapIndex] == pDecodedItem->val.uTagV) {
980 break;
981 }
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700982 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700983 if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) {
984 // No room for the tag
985 return 97; // TODO error code
986 }
987
988 // Cover the case where tag is new and were it is already in the map
989 me->auMappedTags[uTagMapIndex] = pDecodedItem->val.uTagV;
990 auTags[uTagIndex] = (uint16_t)(uTagMapIndex + 0xfff0); // TODO proper constant and cast
991
992 } else {
993 auTags[uTagIndex] = (uint16_t)pDecodedItem->val.uTagV;
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700994 }
995 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800996
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700997Done:
998 return nReturn;
999}
1000
1001
1002/*
Laurence Lundbladeee851742020-01-08 08:37:05 -08001003 This layer takes care of map entries. It combines the label and data
1004 items into one QCBORItem.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001005 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08001006static inline QCBORError
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001007GetNext_MapEntry(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001008{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001009 // Stack use: int/ptr 1, QCBORItem -- 56
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001010 QCBORError nReturn = GetNext_TaggedItem(me, pDecodedItem);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001011 if(nReturn)
1012 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001013
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001014 if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001015 // Break can't be a map entry
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001016 goto Done;
1017 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001018
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001019 if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) {
1020 // In a map and caller wants maps decoded, not treated as arrays
1021
1022 if(DecodeNesting_TypeIsMap(&(me->nesting))) {
1023 // If in a map and the right decoding mode, get the label
1024
Laurence Lundbladeee851742020-01-08 08:37:05 -08001025 // Save label in pDecodedItem and get the next which will
1026 // be the real data
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 QCBORItem LabelItem = *pDecodedItem;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001028 nReturn = GetNext_TaggedItem(me, pDecodedItem);
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001029 if(nReturn)
1030 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001031
Laurence Lundblade57dd1442018-10-15 20:26:28 +05301032 pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001033
1034 if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) {
1035 // strings are always good labels
1036 pDecodedItem->label.string = LabelItem.val.string;
1037 pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING;
1038 } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08001039 // It's not a string and we only want strings
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001040 nReturn = QCBOR_ERR_MAP_LABEL_TYPE;
1041 goto Done;
1042 } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) {
1043 pDecodedItem->label.int64 = LabelItem.val.int64;
1044 pDecodedItem->uLabelType = QCBOR_TYPE_INT64;
1045 } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) {
1046 pDecodedItem->label.uint64 = LabelItem.val.uint64;
1047 pDecodedItem->uLabelType = QCBOR_TYPE_UINT64;
1048 } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) {
1049 pDecodedItem->label.string = LabelItem.val.string;
1050 pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc;
1051 pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING;
1052 } else {
1053 // label is not an int or a string. It is an arrray
1054 // or a float or such and this implementation doesn't handle that.
1055 // Also, tags on labels are ignored.
1056 nReturn = QCBOR_ERR_MAP_LABEL_TYPE;
1057 goto Done;
1058 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001059 }
1060 } else {
1061 if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001062 if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) {
1063 nReturn = QCBOR_ERR_ARRAY_TOO_LONG;
1064 goto Done;
1065 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001066 // Decoding a map as an array
1067 pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001068 // Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2
1069 // Cast is needed because of integer promotion
1070 pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001071 }
1072 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001073
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001074Done:
1075 return nReturn;
1076}
1077
1078
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001079static QCBORError
1080NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak)
1081{
1082 *pbNextIsBreak = false;
1083 if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) {
1084 // TODO: use the Peek method?
1085 QCBORItem Peek;
1086 size_t uPeek = UsefulInputBuf_Tell(pUIB);
1087 QCBORError uReturn = GetNext_Item(pUIB, &Peek, NULL);
1088 if(uReturn != QCBOR_SUCCESS) {
1089 return uReturn;
1090 }
1091 if(Peek.uDataType != QCBOR_TYPE_BREAK) {
1092 // It is not a break, rewind so it can be processed normally.
1093 UsefulInputBuf_Seek(pUIB, uPeek);
1094 } else {
1095 *pbNextIsBreak = true;
1096 }
1097 }
1098
1099 return QCBOR_SUCCESS;
1100}
1101
1102
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001103/*
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07001104 An item was just consumed, now figure out if it was the
1105 end of an array or map that can be closed out. That
1106 may in turn close out another map or array.
1107 */
1108static QCBORError Ascender(QCBORDecodeContext *pMe)
1109{
1110 QCBORError uReturn;
1111
1112 /* This loops ascending nesting levels as long as there is ascending to do */
1113 while(1) {
1114 if(!DecodeNesting_IsAtTop(&(pMe->nesting)) && !DecodeNesting_IsIndefiniteLength(&(pMe->nesting))) {
1115 /* 1st Case: in a definite length array (not a CBOR sequence). Simply
1116 decrement the item count. If it doesn't go to zero, then all is done.
1117 If it does go to zero, the bottom of the loop ascends one nesting level
1118 and the loop continues.
1119 */
1120 DecodeNesting_DecrementX(&(pMe->nesting));
1121 if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) {
1122 /* Didn't close out map or array; all work here is done */
1123 break;
1124 }
1125
1126 } else {
1127 /* 2nd, 3rd, 4th and 5th cases where a check for a following CBOR break must be checked for */
1128 bool bIsBreak = false;
1129 uReturn = NextIsBreak(&(pMe->InBuf), &bIsBreak);
1130 if(uReturn != QCBOR_SUCCESS) {
1131 goto Done;
1132 }
1133
1134 if(bIsBreak) {
1135 if(DecodeNesting_IsAtTop(&(pMe->nesting))) {
1136 /* 2nd case where a break occurs at the top level and thus
1137 in a CBOR sequence. Always an error because break is
1138 not inside an indefinite length map or array. */
1139 uReturn = QCBOR_ERR_BAD_BREAK;
1140 goto Done;
1141 } else {
1142 /* 3rd case, the normal end of an indefinite length map
1143 or array. The bottom of the loop ascends one nesting
1144 level and the loop continues. */
1145 }
1146 } else {
1147 /* 4th case where an indefinite length array is not closed out
1148 and 5th case which is just an item in a CBOR sequence. In either
1149 there is no close out so all work here is done.
1150 */
1151 break;
1152 }
1153 }
1154
1155 /* All items in the level have been consumed. */
1156
1157 /* But ascent in bounded mode is only by explicit call to QCBORDecode_ExitBoundedMode() */
1158 if(DecodeNesting_InBoundedMode(&(pMe->nesting))) {
1159 /* Set the count to zero for indefinite length arrays to indicate cursor is at end of bounded map / array */
1160 pMe->nesting.pCurrent->uCount = 0;
1161 break;
1162 }
1163
1164 /* Finally, actually ascend one level. */
1165 DecodeNesting_Ascend(&(pMe->nesting));
1166 }
1167
1168 uReturn = QCBOR_SUCCESS;
1169
1170Done:
1171 return uReturn;
1172}
1173
1174
1175/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001176 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundbladebb87be22020-04-09 19:15:32 -07001177 TODO: correct this comment
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001178 */
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001179static QCBORError
1180QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001181{
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001182 QCBORError uReturn;
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001183 /* === First figure out if at the end of traversal === */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001184
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001185 /* Case 1. Out of bytes to consume.
1186
1187 This is either the end of the top-level CBOR that was give
1188 to QCBORDecode_Init() or the end of a tag 24 bstr wrapped CBOR.
1189 It is detected by all bytes being consumed from the UsefulInputBuf.
1190
1191 To go back out of the tag 24 bstr wrapped item, the caller must
1192 explicitly call Exit() which will reset the UsefulInputBuf
1193 to the next highest bstr wrapped or the top level.
1194
1195 This is always the end condition that QCBORDecode_Finish()
1196 considers complete.
1197
1198 TODO: can the DecodeNesting_IsAtTop be removed? QCBORDecode_Finish()
1199 will perform this check.
1200
1201 */
Laurence Lundblade937ea812020-05-08 11:38:23 -07001202 /* For a pre-order traversal a non-error end occurs when there
1203 are no more bytes to consume and the nesting level is at the top.
1204 If it's not at the top, then the CBOR is not well formed. This error
1205 is caught elsewhere.
1206
1207 This handles the end of CBOR sequences as well as non-sequences. */
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001208 if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf)) == 0 && DecodeNesting_IsAtTop(&(me->nesting))) {
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001209 uReturn = QCBOR_ERR_NO_MORE_ITEMS;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001210 goto Done;
1211 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07001212
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001213
1214 /* Case 2. End of map or array in bounded mode
1215
1216 The caller is attempting traveral of a bounded map or array and
1217 has got to the end of it.
1218
1219 The caller must explicitly exit the bounded mode map or array
1220 to get past this condition.
1221
1222 To complete a decode of the full input CBOR, the caller must
1223 exit all maps and arrays in bounded mode and this is never
1224 the successful end of decoding.
1225
1226 */
Laurence Lundblade64b607e2020-05-13 13:05:57 -07001227 /* It is also an end of the input when in map mode and the cursor
Laurence Lundblade937ea812020-05-08 11:38:23 -07001228 is at the end of the map */
1229
1230
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001231 // This is to handle bounded mode
Laurence Lundblade937ea812020-05-08 11:38:23 -07001232 if(DecodeNesting_AtEnd(&(me->nesting))) {
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001233 uReturn = QCBOR_ERR_NO_MORE_ITEMS;
Laurence Lundblade3f9ef042020-04-14 13:15:51 -07001234 goto Done;
1235 }
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001236
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001237 /* === Not at the end; get another item === */
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001238 uReturn = GetNext_MapEntry(me, pDecodedItem);
1239 if(uReturn) {
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001240 goto Done;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001241 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05301242
Laurence Lundblade64b607e2020-05-13 13:05:57 -07001243 // Breaks ending arrays/maps are always processed at the end of this function.
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05301244 // They should never show up here.
Laurence Lundblade6de37062018-10-15 12:22:42 +05301245 if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) {
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001246 uReturn = QCBOR_ERR_BAD_BREAK;
Laurence Lundblade6de37062018-10-15 12:22:42 +05301247 goto Done;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05301248 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001249
Laurence Lundblade6de37062018-10-15 12:22:42 +05301250 // Record the nesting level for this data item before processing any of
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05301251 // decrementing and descending.
Laurence Lundblade6de37062018-10-15 12:22:42 +05301252 pDecodedItem->uNestingLevel = DecodeNesting_GetLevel(&(me->nesting));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001253
Laurence Lundblade6de37062018-10-15 12:22:42 +05301254 // Process the item just received for descent or decrement, and
Laurence Lundblade64b607e2020-05-13 13:05:57 -07001255 // ascend if decrements are enough to close out a definite length array/map
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001256 if(IsMapOrArray(pDecodedItem->uDataType) && pDecodedItem->val.uCount != 0) {
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001257 // If the new item is array or map, the nesting level descends
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001258 uReturn = DecodeNesting_Descend(&(me->nesting), pDecodedItem->uDataType, pDecodedItem->val.uCount, 0L);
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001259 // Maps and arrays do count in as items in the map/array that encloses
1260 // them so a decrement needs to be done for them too, but that is done
1261 // only when all the items in them have been processed, not when they
Laurence Lundblade9916b1b2019-09-07 22:33:25 -07001262 // are opened with the exception of an empty map or array.
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001263 if(uReturn != QCBOR_SUCCESS) {
1264 goto Done;
1265 }
Laurence Lundblade5e87da62020-06-07 03:24:28 -07001266 }
1267
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001268 if(!IsMapOrArray(pDecodedItem->uDataType) ||
1269 pDecodedItem->val.uCount == 0 || pDecodedItem->val.uCount == UINT16_MAX) {
1270 /* The following cases are handled here:
1271 - A non-aggregate like an integer or string
1272 - An empty definite length map or array
1273 - An indefinite length map or array that might be empty or might not.
1274 */
1275
Laurence Lundblade5e87da62020-06-07 03:24:28 -07001276
1277
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001278 /* === Figure out if item got closed out maps or arrays === */
1279
Laurence Lundblade5e87da62020-06-07 03:24:28 -07001280 /*
1281 This needs to decrement, check for end and ascend
1282 the tree until an an ascend is not possible or the bounded
1283 limit is reached or the end of the encoded CBOR input
1284 is reached. For
1285 definite length maps and arrays the end is by count. For
1286 indefinite it is by a break.
1287
1288 Also state needs to be set that can tell the code at the
1289 beginning of this function that the end was reached.
1290
1291 This is complicated...
1292
1293
1294 This will handle an indefinite length array
1295 inside a definte length array inside an indefinite
1296 length array...
1297
1298 */
1299
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001300 // Decrement the count of items in the enclosing map/array
1301 // If the count in the enclosing map/array goes to zero, that
Laurence Lundblade6de37062018-10-15 12:22:42 +05301302 // triggers a decrement in the map/array above that and
1303 // an ascend in nesting level.
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001304 /* If the just consumed item is at the end of a map or
1305 array ascend in the nesting tracking. That may
1306 in turn may be the end of the above nesting level
1307 and so on up to the end of the whole encoded CBOR.
1308
1309 Each level could be a definite or indefinte length
1310 map or array. These are handled very differently.
1311
1312 */
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07001313 uReturn = Ascender(me);
1314 if(uReturn) {
1315 goto Done;
Laurence Lundblade5e87da62020-06-07 03:24:28 -07001316 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05301317 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001318
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001319
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001320
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001321 /* === Tell the caller the nest level of the next item === */
1322
Laurence Lundblade6de37062018-10-15 12:22:42 +05301323 // Tell the caller what level is next. This tells them what maps/arrays
1324 // were closed out and makes it possible for them to reconstruct
1325 // the tree with just the information returned by GetNext
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07001326 // TODO: pull this into DecodeNesting_GetLevel
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001327 if(DecodeNesting_InBoundedMode(&(me->nesting)) && me->nesting.pCurrent->uCount == 0) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001328 // At end of a map / array in map mode, so next nest is 0 to
1329 // indicate this end.
1330 pDecodedItem->uNextNestLevel = 0;
1331 } else {
1332 pDecodedItem->uNextNestLevel = DecodeNesting_GetLevel(&(me->nesting));
1333 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001334
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001335Done:
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001336 if(uReturn != QCBOR_SUCCESS) {
Laurence Lundbladee9482dd2019-10-11 12:58:46 -07001337 // Make sure uDataType and uLabelType are QCBOR_TYPE_NONE
1338 memset(pDecodedItem, 0, sizeof(QCBORItem));
1339 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001340 return uReturn;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001341}
1342
1343
Laurence Lundblade59289e52019-12-30 13:44:37 -08001344/*
1345 Mostly just assign the right data type for the date string.
1346 */
1347inline static QCBORError DecodeDateString(QCBORItem *pDecodedItem)
1348{
1349 // Stack Use: UsefulBuf 1 16
1350 if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) {
1351 return QCBOR_ERR_BAD_OPT_TAG;
1352 }
1353
1354 const UsefulBufC Temp = pDecodedItem->val.string;
1355 pDecodedItem->val.dateString = Temp;
1356 pDecodedItem->uDataType = QCBOR_TYPE_DATE_STRING;
1357 return QCBOR_SUCCESS;
1358}
1359
1360
1361/*
1362 Mostly just assign the right data type for the bignum.
1363 */
1364inline static QCBORError DecodeBigNum(QCBORItem *pDecodedItem)
1365{
1366 // Stack Use: UsefulBuf 1 -- 16
1367 if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) {
1368 return QCBOR_ERR_BAD_OPT_TAG;
1369 }
1370 const UsefulBufC Temp = pDecodedItem->val.string;
1371 pDecodedItem->val.bigNum = Temp;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001372 const bool bIsPosBigNum = (bool)(pDecodedItem->uTags[0] == CBOR_TAG_POS_BIGNUM);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001373 pDecodedItem->uDataType = (uint8_t)(bIsPosBigNum ? QCBOR_TYPE_POSBIGNUM
1374 : QCBOR_TYPE_NEGBIGNUM);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001375 return QCBOR_SUCCESS;
1376}
1377
1378
1379/*
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001380 */
1381inline static QCBORError DecodeUUID(QCBORItem *pDecodedItem)
1382{
1383 if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) {
1384 return QCBOR_ERR_BAD_OPT_TAG;
1385 }
1386 pDecodedItem->uDataType = QCBOR_TYPE_UUID;
1387 return QCBOR_SUCCESS;
1388}
1389
1390
1391/*
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001392 */
1393inline static QCBORError DecodeURI(QCBORItem *pDecodedItem)
1394{
1395 if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) {
1396 return QCBOR_ERR_BAD_OPT_TAG;
1397 }
1398 pDecodedItem->uDataType = QCBOR_TYPE_URI;
1399 return QCBOR_SUCCESS;
1400}
1401
1402
1403inline static QCBORError DecodeRegex(QCBORItem *pDecodedItem)
1404{
1405 if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) {
1406 return QCBOR_ERR_BAD_OPT_TAG;
1407 }
1408 pDecodedItem->uDataType = QCBOR_TYPE_REGEX;
1409 return QCBOR_SUCCESS;
1410}
1411
1412
1413inline static QCBORError DecodeB64URL(QCBORItem *pDecodedItem)
1414{
1415 if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) {
1416 return QCBOR_ERR_BAD_OPT_TAG;
1417 }
1418 pDecodedItem->uDataType = QCBOR_TYPE_BASE64URL;
1419 return QCBOR_SUCCESS;
1420}
1421
1422
1423inline static QCBORError DecodeB64(QCBORItem *pDecodedItem)
1424{
1425 if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) {
1426 return QCBOR_ERR_BAD_OPT_TAG;
1427 }
1428 pDecodedItem->uDataType = QCBOR_TYPE_BASE64;
1429 return QCBOR_SUCCESS;
1430}
1431
1432
1433inline static QCBORError DecodeMIME(QCBORItem *pDecodedItem)
1434{
1435 if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING &&
1436 pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) {
1437 return QCBOR_ERR_BAD_OPT_TAG;
1438 }
1439 pDecodedItem->uDataType = QCBOR_TYPE_MIME;
1440 return QCBOR_SUCCESS;
1441}
1442
1443/*
Laurence Lundbladeee851742020-01-08 08:37:05 -08001444 The epoch formatted date. Turns lots of different forms of encoding
1445 date into uniform one
Laurence Lundblade59289e52019-12-30 13:44:37 -08001446 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001447static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001448{
1449 // Stack usage: 1
1450 QCBORError nReturn = QCBOR_SUCCESS;
1451
1452 pDecodedItem->val.epochDate.fSecondsFraction = 0;
1453
1454 switch (pDecodedItem->uDataType) {
1455
1456 case QCBOR_TYPE_INT64:
1457 pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64;
1458 break;
1459
1460 case QCBOR_TYPE_UINT64:
1461 if(pDecodedItem->val.uint64 > INT64_MAX) {
1462 nReturn = QCBOR_ERR_DATE_OVERFLOW;
1463 goto Done;
1464 }
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001465 pDecodedItem->val.epochDate.nSeconds = (int64_t)pDecodedItem->val.uint64;
Laurence Lundblade59289e52019-12-30 13:44:37 -08001466 break;
1467
1468 case QCBOR_TYPE_DOUBLE:
1469 {
1470 // This comparison needs to be done as a float before
1471 // conversion to an int64_t to be able to detect doubles
1472 // that are too large to fit into an int64_t. A double
1473 // has 52 bits of preceision. An int64_t has 63. Casting
1474 // INT64_MAX to a double actually causes a round up which
1475 // is bad and wrong for the comparison because it will
1476 // allow conversion of doubles that can't fit into a
1477 // uint64_t. To remedy this INT64_MAX - 0x7ff is used as
1478 // the cutoff point as if that rounds up in conversion to
1479 // double it will still be less than INT64_MAX. 0x7ff is
1480 // picked because it has 11 bits set.
1481 //
1482 // INT64_MAX seconds is on the order of 10 billion years,
1483 // and the earth is less than 5 billion years old, so for
1484 // most uses this conversion error won't occur even though
1485 // doubles can go much larger.
1486 //
1487 // Without the 0x7ff there is a ~30 minute range of time
1488 // values 10 billion years in the past and in the future
1489 // where this this code would go wrong.
1490 const double d = pDecodedItem->val.dfnum;
1491 if(d > (double)(INT64_MAX - 0x7ff)) {
1492 nReturn = QCBOR_ERR_DATE_OVERFLOW;
1493 goto Done;
1494 }
1495 pDecodedItem->val.epochDate.nSeconds = (int64_t)d;
1496 pDecodedItem->val.epochDate.fSecondsFraction = d - (double)pDecodedItem->val.epochDate.nSeconds;
1497 }
1498 break;
1499
1500 default:
1501 nReturn = QCBOR_ERR_BAD_OPT_TAG;
1502 goto Done;
1503 }
1504 pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH;
1505
1506Done:
1507 return nReturn;
1508}
1509
1510
1511#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
1512/*
1513 Decode decimal fractions and big floats.
1514
1515 When called pDecodedItem must be the array that is tagged as a big
1516 float or decimal fraction, the array that has the two members, the
1517 exponent and mantissa.
1518
1519 This will fetch and decode the exponent and mantissa and put the
1520 result back into pDecodedItem.
1521 */
1522inline static QCBORError
1523QCBORDecode_MantissaAndExponent(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
1524{
1525 QCBORError nReturn;
1526
1527 // --- Make sure it is an array; track nesting level of members ---
1528 if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) {
1529 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1530 goto Done;
1531 }
1532
1533 // A check for pDecodedItem->val.uCount == 2 would work for
Laurence Lundbladeee851742020-01-08 08:37:05 -08001534 // definite length arrays, but not for indefnite. Instead remember
1535 // the nesting level the two integers must be at, which is one
1536 // deeper than that of the array.
Laurence Lundblade59289e52019-12-30 13:44:37 -08001537 const int nNestLevel = pDecodedItem->uNestingLevel + 1;
1538
1539 // --- Is it a decimal fraction or a bigfloat? ---
1540 const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(me, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION);
1541 pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT;
1542
1543 // --- Get the exponent ---
1544 QCBORItem exponentItem;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001545 nReturn = QCBORDecode_GetNextMapOrArray(me, &exponentItem);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001546 if(nReturn != QCBOR_SUCCESS) {
1547 goto Done;
1548 }
1549 if(exponentItem.uNestingLevel != nNestLevel) {
1550 // Array is empty or a map/array encountered when expecting an int
1551 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1552 goto Done;
1553 }
1554 if(exponentItem.uDataType == QCBOR_TYPE_INT64) {
1555 // Data arriving as an unsigned int < INT64_MAX has been converted
1556 // to QCBOR_TYPE_INT64 and thus handled here. This is also means
1557 // that the only data arriving here of type QCBOR_TYPE_UINT64 data
1558 // will be too large for this to handle and thus an error that will
1559 // get handled in the next else.
1560 pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64;
1561 } else {
1562 // Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX
1563 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1564 goto Done;
1565 }
1566
1567 // --- Get the mantissa ---
1568 QCBORItem mantissaItem;
1569 nReturn = QCBORDecode_GetNextWithTags(me, &mantissaItem, NULL);
1570 if(nReturn != QCBOR_SUCCESS) {
1571 goto Done;
1572 }
1573 if(mantissaItem.uNestingLevel != nNestLevel) {
1574 // Mantissa missing or map/array encountered when expecting number
1575 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1576 goto Done;
1577 }
1578 if(mantissaItem.uDataType == QCBOR_TYPE_INT64) {
1579 // Data arriving as an unsigned int < INT64_MAX has been converted
1580 // to QCBOR_TYPE_INT64 and thus handled here. This is also means
1581 // that the only data arriving here of type QCBOR_TYPE_UINT64 data
1582 // will be too large for this to handle and thus an error that
1583 // will get handled in an else below.
1584 pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64;
1585 } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) {
1586 // Got a good big num mantissa
1587 pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum;
1588 // Depends on numbering of QCBOR_TYPE_XXX
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001589 pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType +
1590 mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM +
1591 1);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001592 } else {
1593 // Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX
1594 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1595 goto Done;
1596 }
1597
1598 // --- Check that array only has the two numbers ---
1599 if(mantissaItem.uNextNestLevel == nNestLevel) {
1600 // Extra items in the decimal fraction / big num
1601 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1602 goto Done;
1603 }
1604
1605Done:
1606
1607 return nReturn;
1608}
1609#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
1610
1611
1612/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001613 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade59289e52019-12-30 13:44:37 -08001614 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08001615QCBORError
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001616QCBORDecode_GetNext(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001617{
1618 QCBORError nReturn;
1619
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001620 nReturn = QCBORDecode_GetNextMapOrArray(me, pDecodedItem);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001621 if(nReturn != QCBOR_SUCCESS) {
1622 goto Done;
1623 }
1624
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001625 for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM; i++) {
1626 switch(pDecodedItem->uTags[i] ) {
1627 case 0xffff:
1628 // The end of the tag list or no tags
1629 // Successful exit from the loop.
1630 goto Done;
Laurence Lundblade59289e52019-12-30 13:44:37 -08001631
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001632 case CBOR_TAG_DATE_STRING:
Laurence Lundblade59289e52019-12-30 13:44:37 -08001633 nReturn = DecodeDateString(pDecodedItem);
1634 break;
1635
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001636 case CBOR_TAG_DATE_EPOCH:
Laurence Lundblade59289e52019-12-30 13:44:37 -08001637 nReturn = DecodeDateEpoch(pDecodedItem);
1638 break;
1639
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001640 case CBOR_TAG_POS_BIGNUM:
1641 case CBOR_TAG_NEG_BIGNUM:
Laurence Lundblade59289e52019-12-30 13:44:37 -08001642 nReturn = DecodeBigNum(pDecodedItem);
1643 break;
1644
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001645 #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
1646 case CBOR_TAG_DECIMAL_FRACTION:
1647 case CBOR_TAG_BIGFLOAT:
Laurence Lundblade59289e52019-12-30 13:44:37 -08001648 // For aggregate tagged types, what goes into pTags is only collected
1649 // from the surrounding data item, not the contents, so pTags is not
1650 // passed on here.
1651
1652 nReturn = QCBORDecode_MantissaAndExponent(me, pDecodedItem);
1653 break;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001654 #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade59289e52019-12-30 13:44:37 -08001655
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001656 case CBOR_TAG_BIN_UUID:
1657 nReturn = DecodeUUID(pDecodedItem);
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001658 break;
1659
1660 case CBOR_TAG_URI:
1661 nReturn = DecodeURI(pDecodedItem);
1662 break;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001663
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001664 case CBOR_TAG_REGEX:
1665 nReturn = DecodeRegex(pDecodedItem);
1666 break;
1667
1668
1669 case CBOR_TAG_B64:
1670 nReturn = DecodeB64(pDecodedItem);
1671 break;
1672
1673
1674 case CBOR_TAG_B64URL:
1675 nReturn = DecodeB64URL(pDecodedItem);
1676 break;
1677
1678 case CBOR_TAG_MIME:
1679 case CBOR_TAG_BINARY_MIME:
1680// TODO: should binary and text MIME be distinguished?
1681 nReturn = DecodeMIME(pDecodedItem);
1682 break;
1683
1684
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001685 default:
1686 // A tag that is not understood
1687 // A successful exit from the loop
1688 goto Done;
1689
1690 }
1691 if(nReturn != QCBOR_SUCCESS) {
1692 goto Done;
1693 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08001694 }
1695
1696Done:
1697 if(nReturn != QCBOR_SUCCESS) {
1698 pDecodedItem->uDataType = QCBOR_TYPE_NONE;
1699 pDecodedItem->uLabelType = QCBOR_TYPE_NONE;
1700 }
1701 return nReturn;
1702}
1703
1704
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001705QCBORError QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem)
1706{
1707 const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf));
1708
1709 QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem);
1710
1711 UsefulInputBuf_Seek(&(pMe->InBuf), uOffset);
1712
1713 return uErr;
1714}
1715
1716
Laurence Lundblade59289e52019-12-30 13:44:37 -08001717/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001718 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade59289e52019-12-30 13:44:37 -08001719 */
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001720QCBORError
1721QCBORDecode_GetNextWithTags(QCBORDecodeContext *me,
1722 QCBORItem *pDecodedItem,
1723 QCBORTagListOut *pTags)
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001724{
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001725 QCBORError nReturn;
1726
1727 nReturn = QCBORDecode_GetNext(me, pDecodedItem);
1728 if(nReturn != QCBOR_SUCCESS) {
1729 return nReturn;
1730 }
1731
1732 if(pTags != NULL) {
1733 pTags->uNumUsed = 0;
1734 for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM; i++) {
1735 if(pDecodedItem->uTags[i] == 0xffff) {
1736 break;
1737 }
1738 if(pTags->uNumUsed >= pTags->uNumAllocated) {
1739 return QCBOR_ERR_TOO_MANY_TAGS;
1740 }
1741 pTags->puTags[pTags->uNumUsed] = ConvertTag(me, pDecodedItem->uTags[i]);
1742 pTags->uNumUsed++;
1743 }
1744 }
1745
1746 return QCBOR_SUCCESS;
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001747}
1748
1749
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001750/*
Laurence Lundblade6de37062018-10-15 12:22:42 +05301751 Decoding items is done in 5 layered functions, one calling the
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301752 next one down. If a layer has no work to do for a particular item
1753 it returns quickly.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001754
Laurence Lundblade59289e52019-12-30 13:44:37 -08001755 - QCBORDecode_GetNext, GetNextWithTags -- The top layer processes
1756 tagged data items, turning them into the local C representation.
1757 For the most simple it is just associating a QCBOR_TYPE with the data. For
1758 the complex ones that an aggregate of data items, there is some further
1759 decoding and a little bit of recursion.
1760
1761 - QCBORDecode_GetNextMapOrArray - This manages the beginnings and
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301762 ends of maps and arrays. It tracks descending into and ascending
Laurence Lundblade6de37062018-10-15 12:22:42 +05301763 out of maps/arrays. It processes all breaks that terminate
Laurence Lundbladebb87be22020-04-09 19:15:32 -07001764 indefinite length maps and arrays.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001765
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301766 - GetNext_MapEntry -- This handles the combining of two
1767 items, the label and the data, that make up a map entry.
1768 It only does work on maps. It combines the label and data
1769 items into one labeled item.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001770
Laurence Lundblade59289e52019-12-30 13:44:37 -08001771 - GetNext_TaggedItem -- This decodes type 6 tagging. It turns the
1772 tags into bit flags associated with the data item. No actual decoding
1773 of the contents of the tagged item is performed here.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001774
Laurence Lundblade59289e52019-12-30 13:44:37 -08001775 - GetNext_FullItem -- This assembles the sub-items that make up
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301776 an indefinte length string into one string item. It uses the
Laurence Lundblade6de37062018-10-15 12:22:42 +05301777 string allocater to create contiguous space for the item. It
1778 processes all breaks that are part of indefinite length strings.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001779
Laurence Lundblade59289e52019-12-30 13:44:37 -08001780 - GetNext_Item -- This decodes the atomic data items in CBOR. Each
1781 atomic data item has a "major type", an integer "argument" and optionally
1782 some content. For text and byte strings, the content is the bytes
1783 that make up the string. These are the smallest data items that are
1784 considered to be well-formed. The content may also be other data items in
1785 the case of aggregate types. They are not handled in this layer.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001786
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001787 Roughly this takes 300 bytes of stack for vars. Need to
1788 evaluate this more carefully and correctly.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001789
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301790 */
1791
1792
1793/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001794 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001795 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08001796int QCBORDecode_IsTagged(QCBORDecodeContext *me,
1797 const QCBORItem *pItem,
1798 uint64_t uTag)
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001799{
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001800 for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM; i++ ) {
1801 if(pItem->uTags[i] == 0xffff) {
1802 break;
1803 }
1804 if(ConvertTag(me, pItem->uTags[i]) == uTag) {
1805 return 1;
1806 }
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001807 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001808
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001809 return 0;
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001810}
1811
1812
1813/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001814 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001815 */
Laurence Lundblade30816f22018-11-10 13:40:22 +07001816QCBORError QCBORDecode_Finish(QCBORDecodeContext *me)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001817{
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001818 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001819
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001820 // Error out if all the maps/arrays are not closed out
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001821 if(!DecodeNesting_IsAtTop(&(me->nesting))) {
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001822 nReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN;
1823 goto Done;
1824 }
1825
1826 // Error out if not all the bytes are consumed
1827 if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) {
1828 nReturn = QCBOR_ERR_EXTRA_BYTES;
1829 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001830
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001831Done:
Laurence Lundblade6de37062018-10-15 12:22:42 +05301832 // Call the destructor for the string allocator if there is one.
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001833 // Always called, even if there are errors; always have to clean up
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001834 StringAllocator_Destruct(&(me->StringAllocator));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001835
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001836 return nReturn;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001837}
1838
1839
1840
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001841/*
1842
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001843Decoder errors handled in this file
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001844
Laurence Lundbladeee851742020-01-08 08:37:05 -08001845 - Hit end of input before it was expected while decoding type and
1846 number QCBOR_ERR_HIT_END
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001847
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001848 - negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001849
Laurence Lundbladeee851742020-01-08 08:37:05 -08001850 - Hit end of input while decoding a text or byte string
1851 QCBOR_ERR_HIT_END
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001852
Laurence Lundbladeee851742020-01-08 08:37:05 -08001853 - Encountered conflicting tags -- e.g., an item is tagged both a date
1854 string and an epoch date QCBOR_ERR_UNSUPPORTED
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001855
Laurence Lundbladeee851742020-01-08 08:37:05 -08001856 - Encontered an array or mapp that has too many items
1857 QCBOR_ERR_ARRAY_TOO_LONG
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001858
Laurence Lundbladeee851742020-01-08 08:37:05 -08001859 - Encountered array/map nesting that is too deep
1860 QCBOR_ERR_ARRAY_NESTING_TOO_DEEP
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001861
Laurence Lundbladeee851742020-01-08 08:37:05 -08001862 - An epoch date > INT64_MAX or < INT64_MIN was encountered
1863 QCBOR_ERR_DATE_OVERFLOW
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001864
Laurence Lundbladeee851742020-01-08 08:37:05 -08001865 - The type of a map label is not a string or int
1866 QCBOR_ERR_MAP_LABEL_TYPE
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001867
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001868 - Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001869
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001870 */
1871
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001872
1873
Laurence Lundbladef6531662018-12-04 10:42:22 +09001874
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001875/* ===========================================================================
1876 MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001877
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001878 This implements a simple sting allocator for indefinite length
1879 strings that can be enabled by calling QCBORDecode_SetMemPool(). It
1880 implements the function type QCBORStringAllocate and allows easy
1881 use of it.
Laurence Lundbladef6531662018-12-04 10:42:22 +09001882
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001883 This particular allocator is built-in for convenience. The caller
1884 can implement their own. All of this following code will get
1885 dead-stripped if QCBORDecode_SetMemPool() is not called.
1886
1887 This is a very primitive memory allocator. It does not track
1888 individual allocations, only a high-water mark. A free or
1889 reallocation must be of the last chunk allocated.
1890
1891 The size of the pool and offset to free memory are packed into the
1892 first 8 bytes of the memory pool so we don't have to keep them in
1893 the decode context. Since the address of the pool may not be
1894 aligned, they have to be packed and unpacked as if they were
1895 serialized data of the wire or such.
1896
1897 The sizes packed in are uint32_t to be the same on all CPU types
1898 and simplify the code.
Laurence Lundbladeee851742020-01-08 08:37:05 -08001899 ========================================================================== */
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001900
1901
Laurence Lundbladeee851742020-01-08 08:37:05 -08001902static inline int
1903MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001904{
1905 // Use of UsefulInputBuf is overkill, but it is convenient.
1906 UsefulInputBuf UIB;
1907
Laurence Lundbladeee851742020-01-08 08:37:05 -08001908 // Just assume the size here. It was checked during SetUp so
1909 // the assumption is safe.
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001910 UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem, QCBOR_DECODE_MIN_MEM_POOL_SIZE});
1911 *puPoolSize = UsefulInputBuf_GetUint32(&UIB);
1912 *puFreeOffset = UsefulInputBuf_GetUint32(&UIB);
1913 return UsefulInputBuf_GetError(&UIB);
1914}
1915
1916
Laurence Lundbladeee851742020-01-08 08:37:05 -08001917static inline int
1918MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001919{
1920 // Use of UsefulOutBuf is overkill, but convenient. The
1921 // length check performed here is useful.
1922 UsefulOutBuf UOB;
1923
1924 UsefulOutBuf_Init(&UOB, Pool);
1925 UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool
1926 UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position
1927 return UsefulOutBuf_GetError(&UOB);
1928}
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001929
1930
1931/*
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001932 Internal function for an allocation, reallocation free and destuct.
1933
1934 Having only one function rather than one each per mode saves space in
1935 QCBORDecodeContext.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001936
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001937 Code Reviewers: THIS FUNCTION DOES POINTER MATH
1938 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08001939static UsefulBuf
1940MemPool_Function(void *pPool, void *pMem, size_t uNewSize)
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001941{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001942 UsefulBuf ReturnValue = NULLUsefulBuf;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001943
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001944 uint32_t uPoolSize;
1945 uint32_t uFreeOffset;
1946
1947 if(uNewSize > UINT32_MAX) {
1948 // This allocator is only good up to 4GB. This check should
1949 // optimize out if sizeof(size_t) == sizeof(uint32_t)
1950 goto Done;
1951 }
1952 const uint32_t uNewSize32 = (uint32_t)uNewSize;
1953
1954 if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) {
1955 goto Done;
1956 }
1957
1958 if(uNewSize) {
1959 if(pMem) {
1960 // REALLOCATION MODE
1961 // Calculate pointer to the end of the memory pool. It is
1962 // assumed that pPool + uPoolSize won't wrap around by
1963 // assuming the caller won't pass a pool buffer in that is
1964 // not in legitimate memory space.
1965 const void *pPoolEnd = (uint8_t *)pPool + uPoolSize;
1966
1967 // Check that the pointer for reallocation is in the range of the
1968 // pool. This also makes sure that pointer math further down
1969 // doesn't wrap under or over.
1970 if(pMem >= pPool && pMem < pPoolEnd) {
1971 // Offset to start of chunk for reallocation. This won't
1972 // wrap under because of check that pMem >= pPool. Cast
1973 // is safe because the pool is always less than UINT32_MAX
1974 // because of check in QCBORDecode_SetMemPool().
1975 const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool);
1976
1977 // Check to see if the allocation will fit. uPoolSize -
1978 // uMemOffset will not wrap under because of check that
1979 // pMem is in the range of the uPoolSize by check above.
1980 if(uNewSize <= uPoolSize - uMemOffset) {
1981 ReturnValue.ptr = pMem;
1982 ReturnValue.len = uNewSize;
1983
1984 // Addition won't wrap around over because uNewSize was
1985 // checked to be sure it is less than the pool size.
1986 uFreeOffset = uMemOffset + uNewSize32;
1987 }
1988 }
1989 } else {
1990 // ALLOCATION MODE
1991 // uPoolSize - uFreeOffset will not underflow because this
1992 // pool implementation makes sure uFreeOffset is always
1993 // smaller than uPoolSize through this check here and
1994 // reallocation case.
1995 if(uNewSize <= uPoolSize - uFreeOffset) {
1996 ReturnValue.len = uNewSize;
1997 ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset;
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001998 uFreeOffset += (uint32_t)uNewSize;
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001999 }
Laurence Lundblade041ffa52018-10-07 11:43:51 +07002000 }
2001 } else {
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002002 if(pMem) {
2003 // FREE MODE
2004 // Cast is safe because of limit on pool size in
2005 // QCBORDecode_SetMemPool()
2006 uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool);
2007 } else {
2008 // DESTRUCT MODE
2009 // Nothing to do for this allocator
Laurence Lundblade041ffa52018-10-07 11:43:51 +07002010 }
2011 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002012
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002013 UsefulBuf Pool = {pPool, uPoolSize};
2014 MemPool_Pack(Pool, uFreeOffset);
2015
2016Done:
2017 return ReturnValue;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07002018}
2019
Laurence Lundblade041ffa52018-10-07 11:43:51 +07002020
Laurence Lundbladef6531662018-12-04 10:42:22 +09002021/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08002022 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundbladef6531662018-12-04 10:42:22 +09002023 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002024QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe,
2025 UsefulBuf Pool,
2026 bool bAllStrings)
Laurence Lundblade041ffa52018-10-07 11:43:51 +07002027{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002028 // The pool size and free mem offset are packed into the beginning
2029 // of the pool memory. This compile time check make sure the
2030 // constant in the header is correct. This check should optimize
2031 // down to nothing.
2032 if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) {
Laurence Lundblade30816f22018-11-10 13:40:22 +07002033 return QCBOR_ERR_BUFFER_TOO_SMALL;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07002034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002035
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002036 // The pool size and free offset packed in to the beginning of pool
2037 // memory are only 32-bits. This check will optimize out on 32-bit
2038 // machines.
2039 if(Pool.len > UINT32_MAX) {
2040 return QCBOR_ERR_BUFFER_TOO_LARGE;
2041 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002042
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002043 // This checks that the pool buffer given is big enough.
2044 if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) {
2045 return QCBOR_ERR_BUFFER_TOO_SMALL;
2046 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002047
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002048 pMe->StringAllocator.pfAllocator = MemPool_Function;
2049 pMe->StringAllocator.pAllocateCxt = Pool.ptr;
2050 pMe->bStringAllocateAll = bAllStrings;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002051
Laurence Lundblade30816f22018-11-10 13:40:22 +07002052 return QCBOR_SUCCESS;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07002053}
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002054
Laurence Lundblade1341c592020-04-11 14:19:05 -07002055#include <stdio.h>
2056void printdecode(QCBORDecodeContext *pMe, const char *szName)
2057{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002058 printf("---%s--%d--%d--\nLevel Count Type Offset SaveCount MapMode\n",
2059 szName,
2060 (uint32_t)pMe->InBuf.cursor,
2061 (uint32_t)pMe->InBuf.UB.len);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07002062 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002063 if(&(pMe->nesting.pMapsAndArrays[i]) > pMe->nesting.pCurrent) {
2064 break;
2065 }
Laurence Lundblade64b607e2020-05-13 13:05:57 -07002066 printf("%2s %2d %5d %s %6u %2d %d\n",
2067 pMe->nesting.pCurrentMap == &(pMe->nesting.pMapsAndArrays[i]) ? "->": " ",
Laurence Lundblade1341c592020-04-11 14:19:05 -07002068 i,
2069 pMe->nesting.pMapsAndArrays[i].uCount,
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002070 pMe->nesting.pMapsAndArrays[i].uMajorType == QCBOR_TYPE_MAP ? " map" :
2071 (pMe->nesting.pMapsAndArrays[i].uMajorType == QCBOR_TYPE_ARRAY ? "array" :
2072 (pMe->nesting.pMapsAndArrays[i].uMajorType == QCBOR_TYPE_NONE ? " none" : "?????")),
Laurence Lundblade1341c592020-04-11 14:19:05 -07002073 pMe->nesting.pMapsAndArrays[i].uOffset,
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002074 pMe->nesting.pMapsAndArrays[i].uSaveCount,
2075 pMe->nesting.pMapsAndArrays[i].uMapMode
Laurence Lundblade1341c592020-04-11 14:19:05 -07002076 );
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002077
Laurence Lundblade1341c592020-04-11 14:19:05 -07002078 }
Laurence Lundblade64b607e2020-05-13 13:05:57 -07002079 printf("\n");
Laurence Lundblade1341c592020-04-11 14:19:05 -07002080}
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002081
2082
2083/*
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002084 *
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002085 */
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002086static inline QCBORError
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002087ConsumeItem(QCBORDecodeContext *pMe,
2088 const QCBORItem *pItemToConsume,
2089 uint_fast8_t *puNextNestLevel)
2090{
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002091 QCBORError uReturn;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002092 QCBORItem Item;
2093
2094 printdecode(pMe, "ConsumeItem");
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002095
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002096 if(IsMapOrArray(pItemToConsume->uDataType)) {
Laurence Lundblade1341c592020-04-11 14:19:05 -07002097 /* There is only real work to do for maps and arrays */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002098
Laurence Lundblade1341c592020-04-11 14:19:05 -07002099 /* This works for definite and indefinite length
2100 * maps and arrays by using the nesting level
2101 */
2102 do {
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002103 uReturn = QCBORDecode_GetNext(pMe, &Item);
2104 if(uReturn != QCBOR_SUCCESS) {
Laurence Lundblade1341c592020-04-11 14:19:05 -07002105 goto Done;
2106 }
2107 } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel);
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002108
Laurence Lundblade1341c592020-04-11 14:19:05 -07002109 if(puNextNestLevel != NULL) {
2110 *puNextNestLevel = Item.uNextNestLevel;
2111 }
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002112 uReturn = QCBOR_SUCCESS;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002113
Laurence Lundblade1341c592020-04-11 14:19:05 -07002114 } else {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002115 /* item_to_consume is not a map or array */
Laurence Lundblade1341c592020-04-11 14:19:05 -07002116 if(puNextNestLevel != NULL) {
2117 /* Just pass the nesting level through */
2118 *puNextNestLevel = pItemToConsume->uNextNestLevel;
2119 }
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002120 uReturn = QCBOR_SUCCESS;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002121 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002122
2123Done:
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002124 return uReturn;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002125}
2126
2127
Laurence Lundblade1341c592020-04-11 14:19:05 -07002128/* Return true if the labels in Item1 and Item2 are the same.
2129 Works only for integer and string labels. Returns false
2130 for any other type. */
2131static inline bool
2132MatchLabel(QCBORItem Item1, QCBORItem Item2)
2133{
2134 if(Item1.uLabelType == QCBOR_TYPE_INT64) {
2135 if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) {
2136 return true;
2137 }
2138 } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002139 if(Item2.uLabelType == QCBOR_TYPE_TEXT_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) {
Laurence Lundblade1341c592020-04-11 14:19:05 -07002140 return true;
2141 }
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002142 } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) {
Laurence Lundbladefb492ea2020-05-02 11:14:07 -07002143 if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) {
2144 return true;
2145 }
2146 } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) {
2147 if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) {
2148 return true;
2149 }
2150 }
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002151
Laurence Lundblade1341c592020-04-11 14:19:05 -07002152 /* Other label types are never matched */
2153 return false;
2154}
2155
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002156static inline bool
2157MatchType(QCBORItem Item1, QCBORItem Item2)
2158{
2159 if(Item1.uDataType == Item2.uDataType) {
2160 return true;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002161 } else if(Item1.uDataType == QCBOR_TYPE_ANY) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002162 return true;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002163 } else if(Item2.uDataType == QCBOR_TYPE_ANY) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002164 return true;
2165 }
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002166 return false;
2167}
2168
2169
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002170/**
2171 \brief Search a map for a set of items
2172
2173 @param[in] pMe The decode context to search.
2174 @param[in,out] pItemArray The items to search for and the items found.
2175 @param[in] pCBContext Context for the not-found item call back
2176 @param[in] pfCallback Function to call on items not matched in pItemArray
2177
2178 @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map
2179
2180 @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) were found for one of the labels being search for. This duplicate detection is only performed for items in pItemArray, not every item in the map.
2181
2182 @retval QCBOR_ERR_UNEXPECTED_TYPE The label was matched, but not the type.
2183
2184 @retval Also errors returned by QCBORDecode_GetNext().
2185
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002186 On input pItemArray contains a list of labels and data types
2187 of items to be found.
2188
2189 On output the fully retrieved items are filled in with
2190 values and such. The label was matched, so it never changes.
2191
2192 If an item was not found, its data type is set to none.
2193
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002194 */
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002195static QCBORError
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002196MapSearch(QCBORDecodeContext *pMe,
2197 QCBORItem *pItemArray,
2198 size_t *puOffset,
2199 size_t *puEndOffset,
2200 void *pCBContext,
2201 QCBORItemCallback pfCallback)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002202{
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002203 QCBORError uReturn;
Laurence Lundbladefb492ea2020-05-02 11:14:07 -07002204
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002205 QCBORDecodeNesting SaveNesting;
2206 DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002207
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002208 // Reposition to search from the start of the map / array
2209 UsefulInputBuf_Seek(&(pMe->InBuf), pMe->nesting.pCurrentMap->uOffset);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002210
2211 /* Loop over all the items in the map. They could be
2212 * deeply nested and this should handle both definite
2213 * and indefinite length maps and arrays, so this
2214 * adds some complexity. */
Laurence Lundblade0a042a92020-06-12 14:09:50 -07002215 const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting));
Laurence Lundblade1341c592020-04-11 14:19:05 -07002216
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002217 uint_fast8_t uNextNestLevel;
2218
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002219 uint64_t uFoundItemBitMap = 0;
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002220
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002221 /* Iterate over items in the map / array */
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002222 do {
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002223 /* Remember offset of the item because sometimes it has to be returned */
Laurence Lundblade1341c592020-04-11 14:19:05 -07002224 const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf));
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002225
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002226 /* Get the item */
2227 QCBORItem Item;
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002228 uReturn = QCBORDecode_GetNext(pMe, &Item);
2229 if(uReturn != QCBOR_SUCCESS) {
Laurence Lundblade1341c592020-04-11 14:19:05 -07002230 /* Got non-well-formed CBOR */
2231 goto Done;
2232 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002233
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002234 /* See if item has one of the labels that are of interest */
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002235 int nIndex;
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002236 QCBORItem *pIterator;
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002237 for(pIterator = pItemArray, nIndex = 0; pIterator->uLabelType != 0; pIterator++, nIndex++) {
Laurence Lundblade1341c592020-04-11 14:19:05 -07002238 if(MatchLabel(Item, *pIterator)) {
2239 // A label match has been found
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002240 if(uFoundItemBitMap & (0x01ULL << nIndex)) {
2241 uReturn = QCBOR_ERR_DUPLICATE_LABEL;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002242 goto Done;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002243 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002244 /* Also try to match its type */
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002245 if(!MatchType(Item, *pIterator)) {
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002246 uReturn = QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002247 goto Done;
2248 }
Laurence Lundblade1341c592020-04-11 14:19:05 -07002249
2250 /* Successful match. Return the item. */
2251 *pIterator = Item;
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002252 uFoundItemBitMap |= 0x01ULL << nIndex;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002253 if(puOffset) {
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002254 *puOffset = uOffset;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002255 }
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002256 } else {
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002257 /* Call the callback on unmatched labels */
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002258 /* It is tempting to do duplicate detection here, but that would
2259 require dynamic memory allocation because the number of labels
2260 that might be encountered is unbounded.
2261 */
2262 if(pfCallback) {
2263 uReturn = (*pfCallback)(pCBContext, &Item);
2264 if(uReturn != QCBOR_SUCCESS) {
2265 goto Done;
2266 }
2267 }
Laurence Lundblade1341c592020-04-11 14:19:05 -07002268 }
2269 }
2270
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002271 /* Consume the item whether matched or not. This
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002272 does the work of traversing maps and array and
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002273 everything in them. In this loop only the
2274 items at the current nesting level are examined
2275 to match the labels. */
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002276 uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel);
2277 if(uReturn) {
Laurence Lundblade1341c592020-04-11 14:19:05 -07002278 goto Done;
2279 }
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002280
2281 } while (uNextNestLevel >= uMapNestLevel);
2282
2283
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002284 uReturn = QCBOR_SUCCESS;
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002285
2286 const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf));
2287 // Cast OK because encoded CBOR is limited to UINT32_MAX
2288 pMe->uMapEndOffset = (uint32_t)uEndOffset;
2289 // TODO: is zero *puOffset OK?
2290 if(puEndOffset) {
2291 *puEndOffset = uEndOffset;
2292 }
2293
Laurence Lundbladefb492ea2020-05-02 11:14:07 -07002294 /* For all items not found, set the data type to QCBOR_TYPE_NONE */
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002295 int i;
2296 QCBORItem *pIterator;
2297 for(pIterator = pItemArray, i = 0; pIterator->uLabelType != 0; pIterator++, i++) {
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002298 if(!(uFoundItemBitMap & (0x01ULL << i))) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002299 pIterator->uDataType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002300 }
2301 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002302
2303Done:
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002304 DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting);
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002305
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002306 return uReturn;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002307}
2308
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002309
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002310void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe,
2311 int64_t nLabel,
2312 uint8_t uQcborType,
2313 QCBORItem *pItem)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002314{
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002315 if(pMe->uLastError != QCBOR_SUCCESS) {
2316 return;
2317 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002318
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002319 QCBORItem OneItemSeach[2];
2320 OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64;
2321 OneItemSeach[0].label.int64 = nLabel;
2322 OneItemSeach[0].uDataType = uQcborType;
2323 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array
Laurence Lundblade1341c592020-04-11 14:19:05 -07002324
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002325 QCBORError nReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL, NULL);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002326 if(nReturn) {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002327 pMe->uLastError = (uint8_t)nReturn;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002328 }
2329
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002330 if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) {
2331 pMe->uLastError = QCBOR_ERR_NOT_FOUND;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002332 }
2333
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002334 *pItem = OneItemSeach[0];
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002335}
2336
2337
Laurence Lundbladeda095972020-06-06 18:35:33 -07002338void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe,
2339 const char *szLabel,
2340 uint8_t uQcborType,
2341 QCBORItem *pItem)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002342{
Laurence Lundbladeda095972020-06-06 18:35:33 -07002343 if(pMe->uLastError != QCBOR_SUCCESS) {
2344 return;
2345 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002346
Laurence Lundbladed02ea8e2020-06-06 18:38:19 -07002347
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002348 QCBORItem OneItemSeach[2];
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002349
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002350 OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
2351 OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel);
2352 OneItemSeach[0].uDataType = uQcborType;
2353 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array
Laurence Lundblade1341c592020-04-11 14:19:05 -07002354
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002355 QCBORError nReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL, NULL);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002356 if(nReturn) {
Laurence Lundbladeda095972020-06-06 18:35:33 -07002357 pMe->uLastError = (uint8_t)nReturn;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002358 }
2359
Laurence Lundbladeda095972020-06-06 18:35:33 -07002360
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002361 if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) {
Laurence Lundbladeda095972020-06-06 18:35:33 -07002362 pMe->uLastError = QCBOR_ERR_NOT_FOUND;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002363 }
2364
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002365 *pItem = OneItemSeach[0];
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002366}
2367
2368
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002369static QCBORError CheckTagRequirement(const TagSpecification TagSpec, uint8_t uDataType)
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002370{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002371 if(TagSpec.uTagRequirement == QCBOR_TAGSPEC_MATCH_TAG) {
2372 /* Must match the tag */
2373 if(uDataType == TagSpec.uTaggedType) {
2374 return QCBOR_SUCCESS;
2375 }
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002376 } else {
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002377 /* QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE or QCBOR_TAGSPEC_MATCH_EITHER */
2378 /* Must check all the possible types for the tag content */
2379 for(size_t i = 0; i < sizeof(TagSpec.uAllowedContentTypes); i++) {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002380 if(uDataType == TagSpec.uAllowedContentTypes[i]) {
2381 return QCBOR_SUCCESS;
2382 }
2383 }
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002384 /* Didn't match any of the tag content types */
2385 /* Check the tag for the either case */
2386 if(TagSpec.uTagRequirement == QCBOR_TAGSPEC_MATCH_EITHER) {
2387 if(uDataType == TagSpec.uTaggedType) {
2388 return QCBOR_SUCCESS;
2389 }
2390 }
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002391 }
2392
2393 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002394}
2395
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002396
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002397void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe,
2398 int64_t nLabel,
2399 TagSpecification TagSpec,
2400 QCBORItem *pItem)
2401{
2402 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem);
2403 if(pMe->uLastError != QCBOR_SUCCESS) {
2404 return;
2405 }
2406
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07002407 pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem->uDataType);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002408}
2409
2410void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe,
2411 const char *szLabel,
2412 TagSpecification TagSpec,
2413 QCBORItem *pItem)
2414{
2415 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem);
2416 if(pMe->uLastError != QCBOR_SUCCESS) {
2417 return;
2418 }
2419
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07002420 pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem->uDataType);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002421}
2422
2423void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe,
2424 int64_t nLabel,
2425 TagSpecification TagSpec,
2426 UsefulBufC *pString)
2427{
2428 QCBORItem Item;
2429 QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item);
2430 if(pMe->uLastError == QCBOR_SUCCESS) {
2431 *pString = Item.val.string;
2432 }
2433}
2434
2435void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe,
2436 const char * szLabel,
2437 TagSpecification TagSpec,
2438 UsefulBufC *pString)
2439{
2440 QCBORItem Item;
2441 QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item);
2442 if(pMe->uLastError == QCBOR_SUCCESS) {
2443 *pString = Item.val.string;
2444 }
2445}
Laurence Lundblade1341c592020-04-11 14:19:05 -07002446
Laurence Lundblade1341c592020-04-11 14:19:05 -07002447
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002448QCBORError QCBORDecode_GetItemsInMap(QCBORDecodeContext *pCtx, QCBORItem *pItemList)
2449{
2450 return MapSearch(pCtx, pItemList, NULL, NULL, NULL, NULL);
2451}
2452
2453
2454QCBORError QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pCtx, QCBORItem *pItemList, void *pCallbackCtx, QCBORItemCallback pfCB)
2455{
2456 return MapSearch(pCtx, pItemList, NULL, NULL, pCallbackCtx, pfCB);
2457}
2458
2459
Laurence Lundblade34691b92020-05-18 22:25:25 -07002460static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[])
Laurence Lundblade1341c592020-04-11 14:19:05 -07002461{
Laurence Lundblade34691b92020-05-18 22:25:25 -07002462 if(pMe->uLastError != QCBOR_SUCCESS) {
2463 // Already in error state; do nothing.
2464 return;
2465 }
2466
2467 size_t uOffset;
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002468 pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL, NULL);
Laurence Lundblade34691b92020-05-18 22:25:25 -07002469 if(pMe->uLastError != QCBOR_SUCCESS) {
2470 return;
2471 }
2472
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002473 /* Need to get the current pre-order nesting level and cursor to be
2474 at the first item in the map/array just entered.
2475
2476 Also need to current map nesting level and start cursor to
2477 be at the right place.
2478
2479 The UsefulInBuf offset could be anywhere, so no assumption is
2480 made about it.
2481
2482 No assumption is made about the pre-order nesting level either.
2483
2484 However the map mode nesting level is assumed to be one above
2485 the map level that is being entered.
2486 */
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002487 /* Seek to the data item that is the map or array */
2488 UsefulInputBuf_Seek(&(pMe->InBuf), uOffset);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002489 pMe->nesting.pCurrent = pMe->nesting.pCurrentMap; // TODO: part of DecodeNesting
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002490
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002491 // TODO: check error?
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002492 QCBORDecode_EnterBoundedMode(pMe, pSearch->uDataType);
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002493
Laurence Lundblade34691b92020-05-18 22:25:25 -07002494 printdecode(pMe, "FinishEnter");
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002495}
2496
2497
Laurence Lundblade34691b92020-05-18 22:25:25 -07002498void QCBORDecode_EnterMapInMapN(QCBORDecodeContext *pMe, int64_t nLabel)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002499{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002500 QCBORItem OneItemSeach[2];
2501 OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64;
2502 OneItemSeach[0].label.int64 = nLabel;
2503 OneItemSeach[0].uDataType = QCBOR_TYPE_MAP;
2504 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002505
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002506 /* The map to enter was found, now finish of entering it. */
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002507 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002508}
2509
2510
Laurence Lundblade34691b92020-05-18 22:25:25 -07002511void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002512{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002513 QCBORItem OneItemSeach[2];
2514 OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
2515 OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel);
2516 OneItemSeach[0].uDataType = QCBOR_TYPE_MAP;
2517 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002518
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002519 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002520}
2521
2522
Laurence Lundblade34691b92020-05-18 22:25:25 -07002523void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel)
Laurence Lundblade1341c592020-04-11 14:19:05 -07002524{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002525 QCBORItem OneItemSeach[2];
2526 OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64;
2527 OneItemSeach[0].label.int64 = nLabel;
2528 OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY;
2529 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002530
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002531 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundblade34691b92020-05-18 22:25:25 -07002532}
2533
2534
2535void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel)
2536{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002537 QCBORItem OneItemSeach[2];
2538 OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
2539 OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel);
2540 OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY;
2541 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002542
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002543 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002544}
2545
2546
2547
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002548/* Next item must be map or this generates an error */
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002549void QCBORDecode_EnterBoundedMode(QCBORDecodeContext *pMe, uint8_t uType)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002550{
Laurence Lundblade34691b92020-05-18 22:25:25 -07002551 if(pMe->uLastError != QCBOR_SUCCESS) {
2552 // Already in error state; do nothing.
2553 return;
2554 }
Laurence Lundblade1341c592020-04-11 14:19:05 -07002555
2556 /* Get the data item that is the map that is being searched */
Laurence Lundblade34691b92020-05-18 22:25:25 -07002557 QCBORItem Item;
Laurence Lundblade986017c2020-05-23 19:25:02 -07002558 pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item);
Laurence Lundblade34691b92020-05-18 22:25:25 -07002559 if(pMe->uLastError != QCBOR_SUCCESS) {
2560 return;
Laurence Lundblade3f9ef042020-04-14 13:15:51 -07002561 }
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002562 if(Item.uDataType != uType) {
Laurence Lundblade34691b92020-05-18 22:25:25 -07002563 pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
2564 return;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002565 }
2566
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002567 DecodeNesting_EnterBoundedMode(&(pMe->nesting), UsefulInputBuf_Tell(&(pMe->InBuf)));
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002568
Laurence Lundblade34691b92020-05-18 22:25:25 -07002569 printdecode(pMe, "EnterMapModeDone");
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002570}
2571
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002572void QCBORDecode_ExitBoundedMode(QCBORDecodeContext *pMe, uint8_t uType)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002573{
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002574 QCBORError uErr;
2575 size_t uEndOffset;
2576
2577 (void)uType; // TODO: error check
2578
2579/*
2580 if(pMe->uMapEndOffset) {
2581 uEndOffset = pMe->uMapEndOffset;
2582 // It is only valid once.
2583 pMe->uMapEndOffset = 0;
2584 } else { */
2585 // Find offset of the end the bounded array / map
2586 QCBORItem Dummy;
2587
2588 Dummy.uLabelType = QCBOR_TYPE_NONE;
2589
2590 QCBORError nReturn = MapSearch(pMe, &Dummy, NULL, &uEndOffset, NULL, NULL);
2591
2592 (void)nReturn; // TODO:
2593// }
2594
2595 printdecode(pMe, "start exit");
2596
2597 /* Before acending mark this level as no longer in bound mode. */
2598 pMe->nesting.pCurrentMap->uType &= ~QCBOR_NEST_TYPE_IS_BOUND;
2599
2600
2601 /* Set the pre-order traversal state to just after
2602 the map or array that was exited. */
2603 UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset);
2604
2605 // Always go up one level
2606 // Need error check to know level is bounded mode and not at top level
2607 pMe->nesting.pCurrent = pMe->nesting.pCurrentMap - 1; // TODO error check
2608
2609 uErr = Ascender(pMe);
2610
2611 /* Also ascend to the next higest bounded mode level if
2612 there is one. */
2613 while(1) {
2614 pMe->nesting.pCurrentMap--;
2615 if(DecodeNesting_InBoundedMode(&(pMe->nesting))) {
2616 break;
2617 }
2618 if(pMe->nesting.pCurrentMap == &(pMe->nesting.pMapsAndArrays[0])) {
2619 pMe->nesting.pCurrentMap = NULL;
2620 break;
2621 }
2622 }
2623
2624 printdecode(pMe, "end exit");
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002625}
2626
2627
Laurence Lundblade1341c592020-04-11 14:19:05 -07002628void QCBORDecode_RewindMap(QCBORDecodeContext *pMe)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002629{
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002630 // TODO: check for map mode; test this
Laurence Lundblade1341c592020-04-11 14:19:05 -07002631 pMe->nesting.pCurrent->uCount = pMe->nesting.pCurrent->uSaveCount;
2632 UsefulInputBuf_Seek(&(pMe->InBuf), pMe->nesting.pCurrent->uOffset);
2633}
2634
2635
Laurence Lundblade1341c592020-04-11 14:19:05 -07002636
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002637void QCBORDecode_EnterBstr(QCBORDecodeContext *pMe)
2638{
2639 if(pMe->uLastError != QCBOR_SUCCESS) {
2640 // Already in error state; do nothing.
2641 return;
2642 }
2643
2644 /* Get the data item that is the map that is being searched */
2645 QCBORItem Item;
2646 pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item);
2647 if(pMe->uLastError != QCBOR_SUCCESS) {
2648 return;
2649 }
2650 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
2651 pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
2652 return;
2653 }
2654
2655 // TODO: check for tag 24
2656
2657 // Need to move UIB input cursor to the right place
2658
2659 // Really this is a subtraction and an assignment; not much code
2660 // There is a range check in the seek.
2661 const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf));
2662
2663 UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset - Item.val.string.len);
2664
2665 UsefulInputBuf_SetBufferLen(&(pMe->InBuf), uEndOffset);
2666
2667 // TODO: comment on cast
Laurence Lundblade0a042a92020-06-12 14:09:50 -07002668 pMe->uLastError = (uint8_t)DecodeNesting_Descend(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING, UINT16_MAX, (uint32_t)uEndOffset);
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002669}
2670
2671
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002672void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, uint8_t uTagRequirement, UsefulBufC *pBstr)
2673{
2674 QCBORItem Item;
2675 QCBORDecode_GetNext(pMe, &Item);
2676 // Need to set UIB cursor to start of bstr and UIB length to end of bstr
2677
2678 // TODO: combine with above
2679
2680}
2681
2682//void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pCtx, int64_t uLabel, UsefulBufC *pBstr);
2683
2684//void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pCtx, const char *szLabel, UsefulBufC *pBstr);
2685
2686void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pCtx)
2687{
2688 // Need to set the cursor to end of the bstr and length to the next length
2689 // above in the nesting tree (or the top level length).
2690
2691}
2692
Laurence Lundbladebf3c42d2020-04-14 19:08:51 -07002693
Laurence Lundbladee6430642020-03-14 21:15:44 -07002694
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002695
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002696
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002697
Laurence Lundblade11a064e2020-05-07 13:13:42 -07002698
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002699
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002700static QCBORError InterpretBool(const QCBORItem *pItem, bool *pBool)
2701{
2702 switch(pItem->uDataType) {
2703 case QCBOR_TYPE_TRUE:
2704 *pBool = true;
2705 return QCBOR_SUCCESS;
2706 break;
2707
2708 case QCBOR_TYPE_FALSE:
2709 *pBool = false;
2710 return QCBOR_SUCCESS;
2711 break;
2712
2713 default:
2714 return QCBOR_ERR_UNEXPECTED_TYPE;
2715 break;
2716 }
2717}
Laurence Lundbladee6430642020-03-14 21:15:44 -07002718
Laurence Lundbladec4537442020-04-14 18:53:22 -07002719void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002720{
Laurence Lundbladebf3c42d2020-04-14 19:08:51 -07002721 if(pMe->uLastError != QCBOR_SUCCESS) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07002722 // Already in error state, do nothing
Laurence Lundbladee6430642020-03-14 21:15:44 -07002723 return;
2724 }
2725
Laurence Lundbladec4537442020-04-14 18:53:22 -07002726 QCBORError nError;
2727 QCBORItem Item;
2728
2729 nError = QCBORDecode_GetNext(pMe, &Item);
2730 if(nError != QCBOR_SUCCESS) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002731 pMe->uLastError = (uint8_t)nError;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002732 return;
2733 }
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002734 pMe->uLastError = (uint8_t)InterpretBool(&Item, pValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002735}
2736
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002737void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002738{
2739 QCBORItem Item;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002740 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002741
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002742 pMe->uLastError = (uint8_t)InterpretBool(&Item, pValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002743}
2744
2745
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002746void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue)
2747{
2748 QCBORItem Item;
2749 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
2750
2751 pMe->uLastError = (uint8_t)InterpretBool(&Item, pValue);
2752}
2753
2754
2755
2756void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, TagSpecification TagSpec, UsefulBufC *pBstr)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002757{
Laurence Lundbladebf3c42d2020-04-14 19:08:51 -07002758 if(pMe->uLastError != QCBOR_SUCCESS) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07002759 // Already in error state, do nothing
2760 return;
2761 }
2762
2763 QCBORError nError;
2764 QCBORItem Item;
2765
2766 nError = QCBORDecode_GetNext(pMe, &Item);
2767 if(nError != QCBOR_SUCCESS) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002768 pMe->uLastError = (uint8_t)nError;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002769 return;
2770 }
2771
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002772 pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, Item.uDataType);
2773
2774 if(pMe->uLastError == QCBOR_SUCCESS) {
2775 *pBstr = Item.val.string;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002776 }
2777}
2778
Laurence Lundbladec4537442020-04-14 18:53:22 -07002779
2780
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002781
2782static QCBORError ConvertBigNum(const QCBORItem *pItem, UsefulBufC *pValue, bool *pbIsNegative)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002783{
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002784 *pbIsNegative = false;
2785
2786 bool bMustBeTagged = true; // TODO: fix this
2787
2788 switch(pItem->uDataType) {
2789 case QCBOR_TYPE_BYTE_STRING:
2790 // TODO: check that there is no tag here?
2791 if(bMustBeTagged) {
2792 return QCBOR_ERR_UNEXPECTED_TYPE;
2793 } else {
2794 *pValue = pItem->val.string;
2795 return QCBOR_SUCCESS;
2796 }
2797 break;
2798
2799 case QCBOR_TYPE_POSBIGNUM:
2800 *pValue = pItem->val.string;
2801 return QCBOR_SUCCESS;
2802 break;
2803
2804 case QCBOR_TYPE_NEGBIGNUM:
2805 *pbIsNegative = true;
2806 *pValue = pItem->val.string;
2807 return QCBOR_SUCCESS;
2808 break;
2809
2810 default:
2811 return QCBOR_ERR_UNEXPECTED_TYPE;
2812 break;
2813 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002814}
2815
2816
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002817/**
2818 @param[in] bMustBeTagged If \c true, then the data item must be tagged as either
2819 a positive or negative bignum. If \c false, then it only must be a byte string and bIsNegative
2820 will always be false on the asumption that it is positive, but it can be interpretted as
2821 negative if the the sign is know from other context.
2822 @param[out] pValue The bytes that make up the big num
2823 @param[out] pbIsNegative \c true if tagged as a negative big num. \c false otherwise.
2824
2825 if bMustBeTagged is false, then this will succeed if the data item is a plain byte string,
2826 a positive big num or a negative big num.
2827
2828 */
2829void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, bool bMustBeTagged, UsefulBufC *pValue, bool *pbIsNegative)
2830{
2831 if(pMe->uLastError != QCBOR_SUCCESS) {
2832 // Already in error state, do nothing
2833 return;
2834 }
2835
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002836 QCBORItem Item;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002837 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
2838 if(uError != QCBOR_SUCCESS) {
2839 pMe->uLastError = (uint8_t)uError;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002840 return;
2841 }
2842
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002843 pMe->uLastError = (uint8_t)ConvertBigNum(&Item, pValue, pbIsNegative);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002844}
2845
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002846void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool bMustBeTagged, UsefulBufC *pValue, bool *pbIsNegative)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002847{
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002848 QCBORItem Item;
2849 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002850
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07002851 pMe->uLastError = (uint8_t)ConvertBigNum(&Item, pValue, pbIsNegative);
Laurence Lundbladec4537442020-04-14 18:53:22 -07002852}
2853
Laurence Lundbladec4537442020-04-14 18:53:22 -07002854
2855
2856
Laurence Lundbladee6430642020-03-14 21:15:44 -07002857
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002858typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002859
2860
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002861// The main exponentiator that works on only positive numbers
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002862static QCBORError Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002863{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002864 uint64_t uResult = uMantissa;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002865
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002866 if(uResult != 0) {
2867 /* This loop will run a maximum of 19 times because
2868 * UINT64_MAX < 10 ^^ 19. More than that will cause
2869 * exit with the overflow error
2870 */
2871 for(; nExponent > 0; nExponent--) {
2872 if(uResult > UINT64_MAX / 10) {
2873 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow
2874 }
2875 uResult = uResult * 10;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002876 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002877
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002878 for(; nExponent < 0; nExponent++) {
2879 uResult = uResult / 10;
2880 if(uResult == 0) {
2881 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error
2882 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002883 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002884 }
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002885 /* else, mantissa is zero so this returns zero */
Laurence Lundbladec4537442020-04-14 18:53:22 -07002886
2887 *puResult = uResult;
2888
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002889 return QCBOR_SUCCESS;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002890}
2891
2892
Laurence Lundbladee6430642020-03-14 21:15:44 -07002893/* Convert a decimal fraction to an int64_t without using
2894 floating point or math libraries. Most decimal fractions
2895 will not fit in an int64_t and this will error out with
2896 under or overflow
2897 */
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002898static QCBORError Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002899{
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002900 uint64_t uResult;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002901
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002902 uResult = uMantissa;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002903
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002904 /* This loop will run a maximum of 64 times because
Laurence Lundbladee6430642020-03-14 21:15:44 -07002905 * INT64_MAX < 2^31. More than that will cause
2906 * exist with the overflow error
2907 */
2908 while(nExponent > 0) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002909 if(uResult > UINT64_MAX >> 1) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002910 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow
Laurence Lundbladee6430642020-03-14 21:15:44 -07002911 }
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002912 uResult = uResult << 1;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002913 nExponent--;
2914 }
2915
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002916 while(nExponent < 0 ) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002917 if(uResult == 0) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002918 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error
2919 }
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002920 uResult = uResult >> 1;
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002921 nExponent++;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002922 }
2923
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002924 *puResult = uResult;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002925
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002926 return QCBOR_SUCCESS;
2927}
2928
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002929/*
2930 Compute value with signed mantissa and signed result. Works with exponent of 2 or 10 based on exponentiator.
2931 */
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002932static inline QCBORError ExponentiateNN(int64_t nMantissa, int64_t nExponent, int64_t *pnResult, fExponentiator pfExp)
2933{
2934 uint64_t uResult;
2935
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002936 // Take the absolute value of the mantissa and convert to unsigned.
2937 // TODO: this should be possible in one intruction
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002938 uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa;
2939
2940 // Do the exponentiation of the positive mantissa
2941 QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult);
2942 if(uReturn) {
2943 return uReturn;
2944 }
2945
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002946
Laurence Lundblade983500d2020-05-14 11:49:34 -07002947 /* (uint64_t)INT64_MAX+1 is used to represent the absolute value
2948 of INT64_MIN. This assumes two's compliment representation where
2949 INT64_MIN is one increment farther from 0 than INT64_MAX.
2950 Trying to write -INT64_MIN doesn't work to get this because the
2951 compiler tries to work with an int64_t which can't represent
2952 -INT64_MIN.
2953 */
2954 uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1;
2955
2956 // Error out if too large
2957 if(uResult > uMax) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002958 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
2959 }
2960
2961 // Casts are safe because of checks above
2962 *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult;
2963
2964 return QCBOR_SUCCESS;
2965}
2966
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002967/*
2968 Compute value with signed mantissa and unsigned result. Works with exponent of 2 or 10 based on exponentiator.
2969 */
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002970static inline QCBORError ExponentitateNU(int64_t nMantissa, int64_t nExponent, uint64_t *puResult, fExponentiator pfExp)
2971{
2972 if(nMantissa < 0) {
2973 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
2974 }
2975
2976 // Cast to unsigned is OK because of check for negative
2977 // Cast to unsigned is OK because UINT64_MAX > INT64_MAX
2978 // Exponentiation is straight forward
2979 return (*pfExp)((uint64_t)nMantissa, nExponent, puResult);
2980}
2981
2982
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002983#include <math.h>
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002984
2985
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002986static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002987{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002988 uint64_t uResult;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002989
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002990 uResult = 0;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002991 const uint8_t *pByte = BigNum.ptr;
2992 size_t uLen = BigNum.len;
2993 while(uLen--) {
Laurence Lundblade313b2862020-05-16 01:23:06 -07002994 if(uResult > (uMax >> 8)) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07002995 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002996 }
Laurence Lundblade313b2862020-05-16 01:23:06 -07002997 uResult = (uResult << 8) + *pByte++;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002998 }
2999
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003000 *pResult = uResult;
3001 return QCBOR_SUCCESS;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003002}
3003
Laurence Lundblade887add82020-05-17 05:50:34 -07003004static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003005{
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003006 return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003007}
3008
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003009static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003010{
3011 uint64_t uResult;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003012 QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult);
3013 if(uError) {
3014 return uError;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003015 }
3016 /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */
3017 *pResult = (int64_t)uResult;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003018 return QCBOR_SUCCESS;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003019}
3020
3021
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003022static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003023{
3024 uint64_t uResult;
Laurence Lundbladeda095972020-06-06 18:35:33 -07003025 /* negaative int furthest from zero is INT64_MIN
3026 which is expressed as -INT64_MAX-1. The value of
3027 a negative bignum is -n-1, one further from zero
3028 than the positive bignum */
3029
3030 /* say INT64_MIN is -2; then INT64_MAX is 1.
3031 Then -n-1 <= INT64_MIN.
3032 Then -n -1 <= -INT64_MAX - 1
3033 THen n <= INT64_MAX. */
3034 QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003035 if(uError) {
3036 return uError;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003037 }
3038 /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */
Laurence Lundblade887add82020-05-17 05:50:34 -07003039 // TODO: this code is incorrect. See RFC 7049
Laurence Lundbladeda095972020-06-06 18:35:33 -07003040 uResult++; // this is the -1 in -n-1
Laurence Lundbladee6430642020-03-14 21:15:44 -07003041 *pResult = -(int64_t)uResult;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003042 return QCBOR_SUCCESS;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003043}
3044
Laurence Lundbladef6c86662020-05-12 02:08:00 -07003045#include "fenv.h"
Laurence Lundbladec4537442020-04-14 18:53:22 -07003046
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003047
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003048/*
3049Convert a integers and floats to an int64_t.
3050
3051\param[in] uOptions Bit mask list of conversion options.
3052
3053\retval QCBOR_ERR_CONVERSION_NOT_REQUESTED Conversion, possible, but not requested in uOptions.
3054
3055\retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
3056
3057\retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large or too small.
3058
3059*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003060static QCBORError ConvertInt64(const QCBORItem *pItem, uint32_t uOptions, int64_t *pnValue)
3061{
3062 switch(pItem->uDataType) {
3063 // TODO: float when ifdefs are set
3064 case QCBOR_TYPE_DOUBLE:
3065 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
3066 // TODO: what about under/overflow here?
3067 // Invokes the floating-point HW and/or compiler-added libraries
3068 feclearexcept(FE_ALL_EXCEPT);
3069 *pnValue = llround(pItem->val.dfnum);
3070 if(fetestexcept(FE_INVALID)) {
3071 // TODO: better error code
3072 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
3073 }
3074 } else {
3075 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3076 }
3077 break;
3078
3079 case QCBOR_TYPE_INT64:
3080 if(uOptions & QCBOR_CONVERT_TYPE_INT64) {
3081 *pnValue = pItem->val.int64;
3082 } else {
3083 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3084 }
3085 break;
3086
3087 case QCBOR_TYPE_UINT64:
3088 if(uOptions & QCBOR_CONVERT_TYPE_UINT64) {
3089 if(pItem->val.uint64 < INT64_MAX) {
3090 *pnValue = pItem->val.int64;
3091 } else {
3092 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
3093 }
3094 } else {
3095 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3096 }
3097 break;
3098
3099 default:
3100 return QCBOR_ERR_UNEXPECTED_TYPE;
3101 }
3102 return QCBOR_SUCCESS;
3103}
3104
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003105
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003106void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe,
3107 uint32_t uOptions,
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003108 int64_t *pnValue,
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003109 QCBORItem *pItem)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003110{
Laurence Lundbladebf3c42d2020-04-14 19:08:51 -07003111 if(pMe->uLastError != QCBOR_SUCCESS) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07003112 return;
3113 }
3114
Laurence Lundbladee6430642020-03-14 21:15:44 -07003115 QCBORItem Item;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003116 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
3117 if(uError) {
3118 pMe->uLastError = (uint8_t)uError;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003119 return;
3120 }
3121
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003122 if(pItem) {
3123 *pItem = Item;
3124 }
3125
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003126 pMe->uLastError = (uint8_t)ConvertInt64(&Item, uOptions, pnValue);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003127}
3128
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003129
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003130void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe,
3131 int64_t nLabel,
3132 uint32_t uOptions,
3133 int64_t *pnValue,
3134 QCBORItem *pItem)
3135{
3136 QCBORItem Item;
3137 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
3138
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003139 pMe->uLastError = (uint8_t)ConvertInt64(&Item, uOptions, pnValue);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003140}
3141
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003142
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003143void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe,
3144 const char * szLabel,
3145 uint32_t uOptions,
3146 int64_t *pnValue,
3147 QCBORItem *pItem)
3148{
3149 if(pMe->uLastError != QCBOR_SUCCESS) {
3150 return;
3151 }
3152
3153 QCBORItem Item;
3154 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
3155
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003156 pMe->uLastError = (uint8_t)ConvertInt64(&Item, uOptions, pnValue);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003157}
3158
3159
3160
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003161/*
3162 Convert a large variety of integer types to an int64_t.
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003163
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003164 \param[in] uOptions Bit mask list of conversion options.
3165
3166 \retval QCBOR_ERR_CONVERSION_NOT_REQUESTED Conversion, possible, but not requested in uOptions.
3167
3168 \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
3169
3170 \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large or too small.
3171
3172 */
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003173static QCBORError Int64ConvertAll(const QCBORItem *pItem, uint32_t uOptions, int64_t *pnValue)
3174{
3175 QCBORError uErr;
3176
3177 switch(pItem->uDataType) {
3178
3179 case QCBOR_TYPE_POSBIGNUM:
3180 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3181 return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003182 } else {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003183 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003184 }
3185 break;
3186
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003187 case QCBOR_TYPE_NEGBIGNUM:
3188 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3189 return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003190 } else {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003191 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003192 }
3193 break;
3194
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003195#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3196 case QCBOR_TYPE_DECIMAL_FRACTION:
3197 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3198 return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt,
3199 pItem->val.expAndMantissa.nExponent,
3200 pnValue,
3201 &Exponentitate10);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003202 } else {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003203 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3204 }
3205 break;
3206
3207 case QCBOR_TYPE_BIGFLOAT:
3208 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
3209 return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt,
3210 pItem->val.expAndMantissa.nExponent,
3211 pnValue,
3212 Exponentitate2);
3213 } else {
3214 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3215 }
3216 break;
3217
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003218 case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
3219 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3220 int64_t nMantissa;
3221 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3222 if(uErr) {
3223 return uErr;
3224 }
3225 return ExponentiateNN(nMantissa,
3226 pItem->val.expAndMantissa.nExponent,
3227 pnValue,
3228 Exponentitate10);
3229 } else {
3230 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3231 }
3232 break;
3233
3234 case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
3235 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3236 int64_t nMantissa;
3237 uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3238 if(uErr) {
3239 return uErr;
3240 }
3241 return ExponentiateNN(nMantissa,
3242 pItem->val.expAndMantissa.nExponent,
3243 pnValue,
3244 Exponentitate10);
3245 } else {
3246 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3247 }
3248 break;
3249
3250 case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
3251 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3252 int64_t nMantissa;
3253 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3254 if(uErr) {
3255 return uErr;
3256 }
3257 return ExponentiateNN(nMantissa,
3258 pItem->val.expAndMantissa.nExponent,
3259 pnValue,
3260 Exponentitate2);
3261 } else {
3262 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3263 }
3264 break;
3265
3266 case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
3267 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3268 int64_t nMantissa;
3269 uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3270 if(uErr) {
3271 return uErr;
3272 }
3273 return ExponentiateNN(nMantissa,
3274 pItem->val.expAndMantissa.nExponent,
3275 pnValue,
3276 Exponentitate2);
3277 } else {
3278 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003279 }
3280 break;
3281
Laurence Lundbladec4537442020-04-14 18:53:22 -07003282 default:
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003283 return QCBOR_ERR_UNEXPECTED_TYPE;
3284#endif
Laurence Lundbladec4537442020-04-14 18:53:22 -07003285 }
3286}
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003287
3288
Laurence Lundbladec4537442020-04-14 18:53:22 -07003289/*
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003290 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003291 */
3292void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, int64_t *pnValue)
Laurence Lundbladec4537442020-04-14 18:53:22 -07003293{
3294 QCBORItem Item;
3295
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003296 QCBORDecode_GetInt64ConvertInternal(pMe, uOptions, pnValue, &Item);
Laurence Lundbladec4537442020-04-14 18:53:22 -07003297
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003298 if(pMe->uLastError == QCBOR_SUCCESS) {
3299 // The above conversion succeeded
3300 return;
3301 }
3302
Laurence Lundbladef6c86662020-05-12 02:08:00 -07003303 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003304 // The above conversion failed in a way that code below can't correct
Laurence Lundbladec4537442020-04-14 18:53:22 -07003305 return;
3306 }
3307
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003308 pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uOptions, pnValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003309}
3310
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003311
3312/*
3313Public function, see header qcbor/qcbor_decode.h file
3314*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003315void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, int64_t nLabel, uint32_t uOptions, int64_t *pnValue)
3316{
3317 QCBORItem Item;
3318
3319 QCBORDecode_GetInt64ConvertInternalInMapN(pMe, nLabel, uOptions, pnValue, &Item);
3320
3321 if(pMe->uLastError == QCBOR_SUCCESS) {
3322 // The above conversion succeeded
3323 return;
3324 }
3325
3326 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3327 // The above conversion failed in a way that code below can't correct
3328 return;
3329 }
3330
3331 pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uOptions, pnValue);
3332}
3333
3334
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003335/*
3336Public function, see header qcbor/qcbor_decode.h file
3337*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003338void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, uint32_t uOptions, int64_t *pnValue)
3339{
3340 QCBORItem Item;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003341 QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, szLabel, uOptions, pnValue, &Item);
3342
3343 if(pMe->uLastError == QCBOR_SUCCESS) {
3344 // The above conversion succeeded
3345 return;
3346 }
3347
3348 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3349 // The above conversion failed in a way that code below can't correct
3350 return;
3351 }
3352
3353 pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uOptions, pnValue);
3354}
3355
3356
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003357static QCBORError ConvertUint64(const QCBORItem *pItem, uint32_t uOptions, uint64_t *puValue)
3358{
3359 switch(pItem->uDataType) {
3360 // TODO: type flaot
3361 case QCBOR_TYPE_DOUBLE:
3362 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
3363 feclearexcept(FE_ALL_EXCEPT);
3364 double dRounded = round(pItem->val.dfnum);
3365 // TODO: over/underflow
3366 if(fetestexcept(FE_INVALID)) {
3367 // TODO: better error code
3368 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
3369 } else if(isnan(dRounded)) {
3370 // TODO: better error code
3371 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
3372 } else if(dRounded >= 0) {
3373 *puValue = (uint64_t)dRounded;
3374 } else {
3375 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3376 }
3377 } else {
3378 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3379 }
3380 break;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003381
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003382 case QCBOR_TYPE_INT64:
3383 if(uOptions & QCBOR_CONVERT_TYPE_INT64) {
3384 if(pItem->val.int64 >= 0) {
3385 *puValue = (uint64_t)pItem->val.int64;
3386 } else {
3387 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3388 }
3389 } else {
3390 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3391 }
3392 break;
3393
3394 case QCBOR_TYPE_UINT64:
3395 if(uOptions & QCBOR_CONVERT_TYPE_UINT64) {
3396 *puValue = pItem->val.uint64;
3397 } else {
3398 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3399 }
3400 break;
3401
3402 default:
3403 return QCBOR_ERR_UNEXPECTED_TYPE;
3404 }
3405 return QCBOR_SUCCESS;
3406}
Laurence Lundbladec4537442020-04-14 18:53:22 -07003407
3408
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003409void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe,
3410 uint32_t uOptions,
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003411 uint64_t *puValue,
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003412 QCBORItem *pItem)
Laurence Lundbladec4537442020-04-14 18:53:22 -07003413{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003414 if(pMe->uLastError != QCBOR_SUCCESS) {
3415 return;
3416 }
3417
Laurence Lundbladec4537442020-04-14 18:53:22 -07003418 QCBORItem Item;
Laurence Lundbladec4537442020-04-14 18:53:22 -07003419
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003420 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
3421 if(uError) {
3422 pMe->uLastError = (uint8_t)uError;
Laurence Lundbladec4537442020-04-14 18:53:22 -07003423 return;
3424 }
3425
Laurence Lundbladea826c502020-05-10 21:07:00 -07003426 if(pItem) {
3427 *pItem = Item;
3428 }
3429
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003430 pMe->uLastError = (uint8_t)ConvertUint64(&Item, uOptions, puValue);
Laurence Lundbladec4537442020-04-14 18:53:22 -07003431}
3432
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003433
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003434void QCBORDecode_GetUint64ConvertInternalInMapN(QCBORDecodeContext *pMe,
3435 int64_t nLabel,
3436 uint32_t uOptions,
3437 uint64_t *puValue,
3438 QCBORItem *pItem)
3439{
3440 QCBORItem Item;
3441 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
3442
3443 pMe->uLastError = (uint8_t)ConvertUint64(&Item, uOptions, puValue);
3444}
3445
3446
3447void QCBORDecode_GetUint64ConvertInternalInMapSZ(QCBORDecodeContext *pMe,
3448 const char * szLabel,
3449 uint32_t uOptions,
3450 uint64_t *puValue,
3451 QCBORItem *pItem)
3452{
3453 if(pMe->uLastError != QCBOR_SUCCESS) {
3454 return;
3455 }
3456
3457 QCBORItem Item;
3458 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
3459
3460 pMe->uLastError = (uint8_t)ConvertUint64(&Item, uOptions, puValue);
3461}
3462
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003463/*
3464 Public function, see header qcbor/qcbor_decode.h file
3465*/
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003466static QCBORError Uint64ConvertAll(const QCBORItem *pItem, uint32_t uOptions, uint64_t *puValue)
3467{
3468 QCBORError uErr;
3469
3470 switch(pItem->uDataType) {
3471
3472 case QCBOR_TYPE_POSBIGNUM:
3473 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3474 return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue);
3475 } else {
3476 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3477 }
3478 break;
3479
3480 case QCBOR_TYPE_NEGBIGNUM:
3481 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3482 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3483 } else {
3484 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3485 }
3486 break;
3487
3488#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3489
3490 case QCBOR_TYPE_DECIMAL_FRACTION:
3491 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3492 return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt,
3493 pItem->val.expAndMantissa.nExponent,
3494 puValue,
3495 Exponentitate10);
3496 } else {
3497 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3498 }
3499 break;
3500
3501 case QCBOR_TYPE_BIGFLOAT:
3502 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
3503 return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt,
3504 pItem->val.expAndMantissa.nExponent,
3505 puValue,
3506 Exponentitate2);
3507 } else {
3508 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3509 }
3510 break;
3511
3512 case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
3513 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3514 // TODO: Would be better to convert to unsigned
3515 int64_t nMantissa;
3516 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3517 if(uErr != QCBOR_SUCCESS) {
3518 return uErr;
3519 }
3520 return ExponentitateNU(nMantissa,
3521 pItem->val.expAndMantissa.nExponent,
3522 puValue,
3523 Exponentitate10);
3524 } else {
3525 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3526 }
3527 break;
3528
3529 case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
3530 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3531 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3532 } else {
3533 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3534 }
3535 break;
3536
3537 case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
3538 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3539 // TODO: Would be better to convert to unsigned
3540 int64_t nMantissa;
3541 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3542 if(uErr != QCBOR_SUCCESS) {
3543 return uErr;
3544 }
3545 return ExponentitateNU(nMantissa,
3546 pItem->val.expAndMantissa.nExponent,
3547 puValue,
3548 Exponentitate2);
3549 } else {
3550 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3551 }
3552 break;
3553
3554 case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
3555 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3556 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3557 } else {
3558 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3559 }
3560 break;
3561#endif
3562 default:
3563 return QCBOR_ERR_UNEXPECTED_TYPE;
3564 }
3565}
3566
3567
3568void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, uint64_t *puValue)
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003569{
3570 QCBORItem Item;
3571
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003572 QCBORDecode_GetUInt64ConvertInternal(pMe, uOptions, puValue, &Item);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003573
Laurence Lundbladef6c86662020-05-12 02:08:00 -07003574 if(pMe->uLastError == QCBOR_SUCCESS) {
3575 // The above conversion succeeded
3576 return;
3577 }
3578
3579 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3580 // The above conversion failed in a way that code below can't correct
Laurence Lundbladee6430642020-03-14 21:15:44 -07003581 return;
3582 }
3583
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003584 pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uOptions, puValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003585}
3586
Laurence Lundbladec4537442020-04-14 18:53:22 -07003587
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003588/*
3589Public function, see header qcbor/qcbor_decode.h file
3590*/
3591void QCBORDecode_GetUint64ConvertAllInMapN(QCBORDecodeContext *pMe, int64_t nLabel, uint32_t uOptions, uint64_t *puValue)
3592{
3593 QCBORItem Item;
3594
3595 QCBORDecode_GetUint64ConvertInternalInMapN(pMe, nLabel, uOptions, puValue, &Item);
3596
3597 if(pMe->uLastError == QCBOR_SUCCESS) {
3598 // The above conversion succeeded
3599 return;
3600 }
3601
3602 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3603 // The above conversion failed in a way that code below can't correct
3604 return;
3605 }
3606
3607 pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uOptions, puValue);
3608}
3609
3610
3611/*
3612Public function, see header qcbor/qcbor_decode.h file
3613*/
3614void QCBORDecode_GetUint64ConvertAllInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, uint32_t uOptions, uint64_t *puValue)
3615{
3616 QCBORItem Item;
3617 QCBORDecode_GetUint64ConvertInternalInMapSZ(pMe, szLabel, uOptions, puValue, &Item);
3618
3619 if(pMe->uLastError == QCBOR_SUCCESS) {
3620 // The above conversion succeeded
3621 return;
3622 }
3623
3624 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3625 // The above conversion failed in a way that code below can't correct
3626 return;
3627 }
3628
3629 pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uOptions, puValue);
3630}
3631
3632
3633static QCBORError ConvertDouble(const QCBORItem *pItem, uint32_t uOptions, double *pdValue)
3634{
3635 switch(pItem->uDataType) {
3636 // TODO: float when ifdefs are set
3637 case QCBOR_TYPE_DOUBLE:
3638 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
3639 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
3640 *pdValue = pItem->val.dfnum;
3641 } else {
3642 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3643 }
3644 }
3645 break;
3646
3647 case QCBOR_TYPE_INT64:
3648 if(uOptions & QCBOR_CONVERT_TYPE_INT64) {
3649 // TODO: how does this work?
3650 *pdValue = (double)pItem->val.int64;
3651
3652 } else {
3653 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3654 }
3655 break;
3656
3657 case QCBOR_TYPE_UINT64:
3658 if(uOptions & QCBOR_CONVERT_TYPE_UINT64) {
3659 *pdValue = (double)pItem->val.uint64;
3660 } else {
3661 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3662 }
3663 break;
3664
3665 default:
3666 return QCBOR_ERR_UNEXPECTED_TYPE;
3667 }
3668
3669 return QCBOR_SUCCESS;
3670}
3671
3672
3673
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003674void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe,
3675 uint32_t uOptions,
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003676 double *pdValue,
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003677 QCBORItem *pItem)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003678{
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003679 if(pMe->uLastError != QCBOR_SUCCESS) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07003680 return;
3681 }
3682
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003683 QCBORItem Item;
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003684
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003685 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003686 if(uError) {
3687 pMe->uLastError = (uint8_t)uError;
3688 return;
3689 }
3690
3691 if(pItem) {
3692 *pItem = Item;
3693 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07003694
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003695 pMe->uLastError = (uint8_t)ConvertDouble(&Item, uOptions, pdValue);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003696}
Laurence Lundbladec4537442020-04-14 18:53:22 -07003697
Laurence Lundbladec4537442020-04-14 18:53:22 -07003698
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003699void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe,
3700 int64_t nLabel,
3701 uint32_t uOptions,
3702 double *pdValue,
3703 QCBORItem *pItem)
3704{
3705 QCBORItem Item;
3706 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
3707
3708 pMe->uLastError = (uint8_t)ConvertDouble(&Item, uOptions, pdValue);
3709}
3710
3711void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe,
3712 const char * szLabel,
3713 uint32_t uOptions,
3714 double *pdValue,
3715 QCBORItem *pItem)
3716{
3717 if(pMe->uLastError != QCBOR_SUCCESS) {
3718 return;
3719 }
3720
3721 QCBORItem Item;
3722 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
3723
3724 pMe->uLastError = (uint8_t)ConvertDouble(&Item, uOptions, pdValue);
3725}
3726
3727
3728
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07003729static double ConvertBigNumToDouble(const UsefulBufC BigNum)
3730{
3731 double dResult;
3732
3733 dResult = 0.0;
3734 const uint8_t *pByte = BigNum.ptr;
3735 size_t uLen = BigNum.len;
3736 /* This will overflow and become the float value INFINITY if the number
3737 is too large to fit. No error will be logged.
3738 TODO: should an error be logged? */
3739 while(uLen--) {
3740 dResult = (dResult * 256.0) + (double)*pByte++;
3741 }
3742
3743 return dResult;
3744}
3745
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003746static QCBORError DoubleConvertAll(const QCBORItem *pItem, uint32_t uOptions, double *pdValue)
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003747{
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003748 /*
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003749 https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
3750
3751 */
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003752 switch(pItem->uDataType) {
3753 // TODO: type float
3754 case QCBOR_TYPE_DECIMAL_FRACTION:
3755 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3756 // TODO: rounding and overflow errors
3757 *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt *
3758 pow(10.0, (double)pItem->val.expAndMantissa.nExponent);
3759 } else {
3760 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3761 }
3762 break;
3763
3764 case QCBOR_TYPE_BIGFLOAT:
3765 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT ) {
3766 *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt *
3767 exp2((double)pItem->val.expAndMantissa.nExponent);
3768 } else {
3769 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3770 }
3771 break;
3772
3773 case QCBOR_TYPE_POSBIGNUM:
3774 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3775 *pdValue = ConvertBigNumToDouble(pItem->val.bigNum);
3776 } else {
3777 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3778 }
3779 break;
3780
3781 case QCBOR_TYPE_NEGBIGNUM:
3782 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
Laurence Lundbladeda095972020-06-06 18:35:33 -07003783 *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum);
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003784 } else {
3785 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3786 }
3787 break;
3788
3789 case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
3790 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3791 double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
3792 *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent);
3793 } else {
3794 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3795 }
3796 break;
3797
3798 case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
3799 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3800 double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
3801 *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent);
3802 } else {
3803 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3804 }
3805 break;
3806
3807 case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
3808 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
3809 double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
3810 *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent);
3811 } else {
3812 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3813 }
3814 break;
3815
3816 case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
3817 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
Laurence Lundbladeda095972020-06-06 18:35:33 -07003818 double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003819 *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent);
3820 } else {
3821 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3822 }
3823 break;
3824
3825 default:
3826 return QCBOR_ERR_UNEXPECTED_TYPE;
3827 }
3828
3829 return QCBOR_SUCCESS;
3830}
3831
3832
3833/*
3834 Public function, see header qcbor/qcbor_decode.h file
3835*/
3836void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, double *pdValue)
3837{
3838
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003839 QCBORItem Item;
3840
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003841 QCBORDecode_GetDoubleConvertInternal(pMe, uOptions, pdValue, &Item);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003842
3843 if(pMe->uLastError == QCBOR_SUCCESS) {
3844 // The above conversion succeeded
3845 return;
3846 }
3847
3848 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3849 // The above conversion failed in a way that code below can't correct
3850 return;
3851 }
3852
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003853 pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uOptions, pdValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003854}
3855
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003856
3857/*
3858Public function, see header qcbor/qcbor_decode.h file
3859*/
3860void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, int64_t nLabel, uint32_t uOptions, double *pdValue)
3861{
3862 QCBORItem Item;
3863
3864 QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uOptions, pdValue, &Item);
3865
3866 if(pMe->uLastError == QCBOR_SUCCESS) {
3867 // The above conversion succeeded
3868 return;
3869 }
3870
3871 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3872 // The above conversion failed in a way that code below can't correct
3873 return;
3874 }
3875
3876 pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uOptions, pdValue);
3877}
3878
3879
3880/*
3881Public function, see header qcbor/qcbor_decode.h file
3882*/
3883void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, uint32_t uOptions, double *pdValue)
3884{
3885 QCBORItem Item;
3886 QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uOptions, pdValue, &Item);
3887
3888 if(pMe->uLastError == QCBOR_SUCCESS) {
3889 // The above conversion succeeded
3890 return;
3891 }
3892
3893 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3894 // The above conversion failed in a way that code below can't correct
3895 return;
3896 }
3897
3898 pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uOptions, pdValue);
3899}