blob: d5c46340f8dda52ec43d5352575f0ab256177298 [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 Lundblade4e2da002020-06-13 23:08:31 -07002310/*
2311Public function, see header qcbor/qcbor_decode.h file
2312*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002313void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe,
2314 int64_t nLabel,
2315 uint8_t uQcborType,
2316 QCBORItem *pItem)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002317{
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002318 if(pMe->uLastError != QCBOR_SUCCESS) {
2319 return;
2320 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002321
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002322 QCBORItem OneItemSeach[2];
2323 OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64;
2324 OneItemSeach[0].label.int64 = nLabel;
2325 OneItemSeach[0].uDataType = uQcborType;
2326 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array
Laurence Lundblade1341c592020-04-11 14:19:05 -07002327
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002328 QCBORError nReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL, NULL);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002329 if(nReturn) {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002330 pMe->uLastError = (uint8_t)nReturn;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002331 }
2332
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002333 if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) {
2334 pMe->uLastError = QCBOR_ERR_NOT_FOUND;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002335 }
2336
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002337 *pItem = OneItemSeach[0];
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002338}
2339
2340
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002341/*
2342Public function, see header qcbor/qcbor_decode.h file
2343*/
Laurence Lundbladeda095972020-06-06 18:35:33 -07002344void QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe,
2345 const char *szLabel,
2346 uint8_t uQcborType,
2347 QCBORItem *pItem)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002348{
Laurence Lundbladeda095972020-06-06 18:35:33 -07002349 if(pMe->uLastError != QCBOR_SUCCESS) {
2350 return;
2351 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002352
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002353 QCBORItem OneItemSeach[2];
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002354 OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
2355 OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel);
2356 OneItemSeach[0].uDataType = uQcborType;
2357 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array
Laurence Lundblade1341c592020-04-11 14:19:05 -07002358
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002359 QCBORError nReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL, NULL);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002360 if(nReturn) {
Laurence Lundbladeda095972020-06-06 18:35:33 -07002361 pMe->uLastError = (uint8_t)nReturn;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002362 }
2363
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002364 if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) {
Laurence Lundbladeda095972020-06-06 18:35:33 -07002365 pMe->uLastError = QCBOR_ERR_NOT_FOUND;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002366 }
2367
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002368 *pItem = OneItemSeach[0];
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002369}
2370
2371
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002372/**
2373 @param[in] TagSpec Specification for matching tags.
2374 @param[in] uDataType A QCBOR data type
2375
2376 @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec
2377 @retval QCBOR_ERR_UNEXPECTED_TYPE \c uDataType is not allowed by @c TagSpec
2378
2379 The data type must be one of the QCBOR_TYPEs, not the IETF CBOR Registered tag value.
2380 */
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002381static QCBORError CheckTagRequirement(const TagSpecification TagSpec, uint8_t uDataType)
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002382{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002383 if(TagSpec.uTagRequirement == QCBOR_TAGSPEC_MATCH_TAG) {
2384 /* Must match the tag */
2385 if(uDataType == TagSpec.uTaggedType) {
2386 return QCBOR_SUCCESS;
2387 }
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002388 } else {
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002389 /* QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE or QCBOR_TAGSPEC_MATCH_EITHER */
2390 /* Must check all the possible types for the tag content */
2391 for(size_t i = 0; i < sizeof(TagSpec.uAllowedContentTypes); i++) {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002392 if(uDataType == TagSpec.uAllowedContentTypes[i]) {
2393 return QCBOR_SUCCESS;
2394 }
2395 }
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002396 /* Didn't match any of the tag content types */
2397 /* Check the tag for the either case */
2398 if(TagSpec.uTagRequirement == QCBOR_TAGSPEC_MATCH_EITHER) {
2399 if(uDataType == TagSpec.uTaggedType) {
2400 return QCBOR_SUCCESS;
2401 }
2402 }
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002403 }
2404
2405 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002406}
2407
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002408
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002409// Semi-private
2410// TODO: inline or collapse with QCBORDecode_GetTaggedStringInMapN?
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002411void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe,
2412 int64_t nLabel,
2413 TagSpecification TagSpec,
2414 QCBORItem *pItem)
2415{
2416 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem);
2417 if(pMe->uLastError != QCBOR_SUCCESS) {
2418 return;
2419 }
2420
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07002421 pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem->uDataType);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002422}
2423
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002424// Semi-private
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002425void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe,
2426 const char *szLabel,
2427 TagSpecification TagSpec,
2428 QCBORItem *pItem)
2429{
2430 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem);
2431 if(pMe->uLastError != QCBOR_SUCCESS) {
2432 return;
2433 }
2434
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07002435 pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem->uDataType);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002436}
2437
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002438// Semi-private
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002439void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe,
2440 int64_t nLabel,
2441 TagSpecification TagSpec,
2442 UsefulBufC *pString)
2443{
2444 QCBORItem Item;
2445 QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item);
2446 if(pMe->uLastError == QCBOR_SUCCESS) {
2447 *pString = Item.val.string;
2448 }
2449}
2450
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002451// Semi-private
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002452void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe,
2453 const char * szLabel,
2454 TagSpecification TagSpec,
2455 UsefulBufC *pString)
2456{
2457 QCBORItem Item;
2458 QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item);
2459 if(pMe->uLastError == QCBOR_SUCCESS) {
2460 *pString = Item.val.string;
2461 }
2462}
Laurence Lundblade1341c592020-04-11 14:19:05 -07002463
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002464/*
2465Public function, see header qcbor/qcbor_decode.h file
2466*/
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002467QCBORError QCBORDecode_GetItemsInMap(QCBORDecodeContext *pCtx, QCBORItem *pItemList)
2468{
2469 return MapSearch(pCtx, pItemList, NULL, NULL, NULL, NULL);
2470}
2471
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002472/*
2473Public function, see header qcbor/qcbor_decode.h file
2474*/
2475QCBORError QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pCtx,
2476 QCBORItem *pItemList,
2477 void *pCallbackCtx,
2478 QCBORItemCallback pfCB)
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002479{
2480 return MapSearch(pCtx, pItemList, NULL, NULL, pCallbackCtx, pfCB);
2481}
2482
2483
Laurence Lundblade34691b92020-05-18 22:25:25 -07002484static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[])
Laurence Lundblade1341c592020-04-11 14:19:05 -07002485{
Laurence Lundblade34691b92020-05-18 22:25:25 -07002486 if(pMe->uLastError != QCBOR_SUCCESS) {
2487 // Already in error state; do nothing.
2488 return;
2489 }
2490
2491 size_t uOffset;
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002492 pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL, NULL);
Laurence Lundblade34691b92020-05-18 22:25:25 -07002493 if(pMe->uLastError != QCBOR_SUCCESS) {
2494 return;
2495 }
2496
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002497 /* Need to get the current pre-order nesting level and cursor to be
2498 at the first item in the map/array just entered.
2499
2500 Also need to current map nesting level and start cursor to
2501 be at the right place.
2502
2503 The UsefulInBuf offset could be anywhere, so no assumption is
2504 made about it.
2505
2506 No assumption is made about the pre-order nesting level either.
2507
2508 However the map mode nesting level is assumed to be one above
2509 the map level that is being entered.
2510 */
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002511 /* Seek to the data item that is the map or array */
2512 UsefulInputBuf_Seek(&(pMe->InBuf), uOffset);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002513 pMe->nesting.pCurrent = pMe->nesting.pCurrentMap; // TODO: part of DecodeNesting
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002514
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002515 // TODO: check error?
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002516 QCBORDecode_EnterBoundedMode(pMe, pSearch->uDataType);
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002517
Laurence Lundblade34691b92020-05-18 22:25:25 -07002518 printdecode(pMe, "FinishEnter");
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002519}
2520
2521
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002522/*
2523Public function, see header qcbor/qcbor_decode.h file
2524*/
Laurence Lundblade34691b92020-05-18 22:25:25 -07002525void QCBORDecode_EnterMapInMapN(QCBORDecodeContext *pMe, int64_t nLabel)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002526{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002527 QCBORItem OneItemSeach[2];
2528 OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64;
2529 OneItemSeach[0].label.int64 = nLabel;
2530 OneItemSeach[0].uDataType = QCBOR_TYPE_MAP;
2531 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002532
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002533 /* The map to enter was found, now finish of entering it. */
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002534 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002535}
2536
2537
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002538/*
2539Public function, see header qcbor/qcbor_decode.h file
2540*/
Laurence Lundblade34691b92020-05-18 22:25:25 -07002541void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002542{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002543 QCBORItem OneItemSeach[2];
2544 OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
2545 OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel);
2546 OneItemSeach[0].uDataType = QCBOR_TYPE_MAP;
2547 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002548
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002549 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002550}
2551
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002552/*
2553Public function, see header qcbor/qcbor_decode.h file
2554*/
Laurence Lundblade34691b92020-05-18 22:25:25 -07002555void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel)
Laurence Lundblade1341c592020-04-11 14:19:05 -07002556{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002557 QCBORItem OneItemSeach[2];
2558 OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64;
2559 OneItemSeach[0].label.int64 = nLabel;
2560 OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY;
2561 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002562
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002563 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundblade34691b92020-05-18 22:25:25 -07002564}
2565
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002566/*
2567Public function, see header qcbor/qcbor_decode.h file
2568*/
Laurence Lundblade34691b92020-05-18 22:25:25 -07002569void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel)
2570{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002571 QCBORItem OneItemSeach[2];
2572 OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
2573 OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel);
2574 OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY;
2575 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002576
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002577 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002578}
2579
2580
2581
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002582/* Next item must be map or this generates an error */
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002583void QCBORDecode_EnterBoundedMode(QCBORDecodeContext *pMe, uint8_t uType)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002584{
Laurence Lundblade34691b92020-05-18 22:25:25 -07002585 if(pMe->uLastError != QCBOR_SUCCESS) {
2586 // Already in error state; do nothing.
2587 return;
2588 }
Laurence Lundblade1341c592020-04-11 14:19:05 -07002589
2590 /* Get the data item that is the map that is being searched */
Laurence Lundblade34691b92020-05-18 22:25:25 -07002591 QCBORItem Item;
Laurence Lundblade986017c2020-05-23 19:25:02 -07002592 pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item);
Laurence Lundblade34691b92020-05-18 22:25:25 -07002593 if(pMe->uLastError != QCBOR_SUCCESS) {
2594 return;
Laurence Lundblade3f9ef042020-04-14 13:15:51 -07002595 }
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002596 if(Item.uDataType != uType) {
Laurence Lundblade34691b92020-05-18 22:25:25 -07002597 pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
2598 return;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002599 }
2600
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002601 DecodeNesting_EnterBoundedMode(&(pMe->nesting), UsefulInputBuf_Tell(&(pMe->InBuf)));
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002602
Laurence Lundblade34691b92020-05-18 22:25:25 -07002603 printdecode(pMe, "EnterMapModeDone");
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002604}
2605
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002606void QCBORDecode_ExitBoundedMode(QCBORDecodeContext *pMe, uint8_t uType)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002607{
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002608 QCBORError uErr;
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002609 size_t uEndOffset;
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002610
2611 (void)uType; // TODO: error check
2612
2613/*
2614 if(pMe->uMapEndOffset) {
2615 uEndOffset = pMe->uMapEndOffset;
2616 // It is only valid once.
2617 pMe->uMapEndOffset = 0;
2618 } else { */
2619 // Find offset of the end the bounded array / map
2620 QCBORItem Dummy;
2621
2622 Dummy.uLabelType = QCBOR_TYPE_NONE;
2623
2624 QCBORError nReturn = MapSearch(pMe, &Dummy, NULL, &uEndOffset, NULL, NULL);
2625
2626 (void)nReturn; // TODO:
2627// }
2628
2629 printdecode(pMe, "start exit");
2630
2631 /* Before acending mark this level as no longer in bound mode. */
2632 pMe->nesting.pCurrentMap->uType &= ~QCBOR_NEST_TYPE_IS_BOUND;
2633
2634
2635 /* Set the pre-order traversal state to just after
2636 the map or array that was exited. */
2637 UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset);
2638
2639 // Always go up one level
2640 // Need error check to know level is bounded mode and not at top level
2641 pMe->nesting.pCurrent = pMe->nesting.pCurrentMap - 1; // TODO error check
2642
2643 uErr = Ascender(pMe);
2644
2645 /* Also ascend to the next higest bounded mode level if
2646 there is one. */
2647 while(1) {
2648 pMe->nesting.pCurrentMap--;
2649 if(DecodeNesting_InBoundedMode(&(pMe->nesting))) {
2650 break;
2651 }
2652 if(pMe->nesting.pCurrentMap == &(pMe->nesting.pMapsAndArrays[0])) {
2653 pMe->nesting.pCurrentMap = NULL;
2654 break;
2655 }
2656 }
2657
2658 printdecode(pMe, "end exit");
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002659}
2660
2661
Laurence Lundblade1341c592020-04-11 14:19:05 -07002662void QCBORDecode_RewindMap(QCBORDecodeContext *pMe)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002663{
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002664 // TODO: check for map mode; test this
Laurence Lundblade1341c592020-04-11 14:19:05 -07002665 pMe->nesting.pCurrent->uCount = pMe->nesting.pCurrent->uSaveCount;
2666 UsefulInputBuf_Seek(&(pMe->InBuf), pMe->nesting.pCurrent->uOffset);
2667}
2668
2669
Laurence Lundblade1341c592020-04-11 14:19:05 -07002670
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002671void QCBORDecode_EnterBstr(QCBORDecodeContext *pMe)
2672{
2673 if(pMe->uLastError != QCBOR_SUCCESS) {
2674 // Already in error state; do nothing.
2675 return;
2676 }
2677
2678 /* Get the data item that is the map that is being searched */
2679 QCBORItem Item;
2680 pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item);
2681 if(pMe->uLastError != QCBOR_SUCCESS) {
2682 return;
2683 }
2684 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
2685 pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
2686 return;
2687 }
2688
2689 // TODO: check for tag 24
2690
2691 // Need to move UIB input cursor to the right place
2692
2693 // Really this is a subtraction and an assignment; not much code
2694 // There is a range check in the seek.
2695 const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf));
2696
2697 UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset - Item.val.string.len);
2698
2699 UsefulInputBuf_SetBufferLen(&(pMe->InBuf), uEndOffset);
2700
2701 // TODO: comment on cast
Laurence Lundblade0a042a92020-06-12 14:09:50 -07002702 pMe->uLastError = (uint8_t)DecodeNesting_Descend(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING, UINT16_MAX, (uint32_t)uEndOffset);
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002703}
2704
2705
Laurence Lundbladed8c82c52020-06-12 22:15:52 -07002706void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, uint8_t uTagRequirement, UsefulBufC *pBstr)
2707{
2708 QCBORItem Item;
2709 QCBORDecode_GetNext(pMe, &Item);
2710 // Need to set UIB cursor to start of bstr and UIB length to end of bstr
2711
2712 // TODO: combine with above
2713
2714}
2715
2716//void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pCtx, int64_t uLabel, UsefulBufC *pBstr);
2717
2718//void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pCtx, const char *szLabel, UsefulBufC *pBstr);
2719
2720void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pCtx)
2721{
2722 // Need to set the cursor to end of the bstr and length to the next length
2723 // above in the nesting tree (or the top level length).
2724
2725}
2726
Laurence Lundbladebf3c42d2020-04-14 19:08:51 -07002727
Laurence Lundbladee6430642020-03-14 21:15:44 -07002728
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002729
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002730
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002731
Laurence Lundblade11a064e2020-05-07 13:13:42 -07002732
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002733
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002734static QCBORError InterpretBool(const QCBORItem *pItem, bool *pBool)
2735{
2736 switch(pItem->uDataType) {
2737 case QCBOR_TYPE_TRUE:
2738 *pBool = true;
2739 return QCBOR_SUCCESS;
2740 break;
2741
2742 case QCBOR_TYPE_FALSE:
2743 *pBool = false;
2744 return QCBOR_SUCCESS;
2745 break;
2746
2747 default:
2748 return QCBOR_ERR_UNEXPECTED_TYPE;
2749 break;
2750 }
2751}
Laurence Lundbladee6430642020-03-14 21:15:44 -07002752
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002753/*
2754Public function, see header qcbor/qcbor_decode.h file
2755*/
Laurence Lundbladec4537442020-04-14 18:53:22 -07002756void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue)
Laurence Lundbladee6430642020-03-14 21:15:44 -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
Laurence Lundbladee6430642020-03-14 21:15:44 -07002760 return;
2761 }
2762
Laurence Lundbladec4537442020-04-14 18:53:22 -07002763 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 }
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002771 pMe->uLastError = (uint8_t)InterpretBool(&Item, pValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002772}
2773
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002774/*
2775Public function, see header qcbor/qcbor_decode.h file
2776*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002777void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002778{
2779 QCBORItem Item;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002780 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002781
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002782 pMe->uLastError = (uint8_t)InterpretBool(&Item, pValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002783}
2784
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002785/*
2786Public function, see header qcbor/qcbor_decode.h file
2787*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002788void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue)
2789{
2790 QCBORItem Item;
2791 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
2792
2793 pMe->uLastError = (uint8_t)InterpretBool(&Item, pValue);
2794}
2795
2796
2797
2798void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, TagSpecification TagSpec, UsefulBufC *pBstr)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002799{
Laurence Lundbladebf3c42d2020-04-14 19:08:51 -07002800 if(pMe->uLastError != QCBOR_SUCCESS) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07002801 // Already in error state, do nothing
2802 return;
2803 }
2804
2805 QCBORError nError;
2806 QCBORItem Item;
2807
2808 nError = QCBORDecode_GetNext(pMe, &Item);
2809 if(nError != QCBOR_SUCCESS) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002810 pMe->uLastError = (uint8_t)nError;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002811 return;
2812 }
2813
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002814 pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, Item.uDataType);
2815
2816 if(pMe->uLastError == QCBOR_SUCCESS) {
2817 *pBstr = Item.val.string;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002818 }
2819}
2820
Laurence Lundbladec4537442020-04-14 18:53:22 -07002821
2822
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002823
2824static QCBORError ConvertBigNum(const QCBORItem *pItem, UsefulBufC *pValue, bool *pbIsNegative)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002825{
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002826 *pbIsNegative = false;
2827
2828 bool bMustBeTagged = true; // TODO: fix this
2829
2830 switch(pItem->uDataType) {
2831 case QCBOR_TYPE_BYTE_STRING:
2832 // TODO: check that there is no tag here?
2833 if(bMustBeTagged) {
2834 return QCBOR_ERR_UNEXPECTED_TYPE;
2835 } else {
2836 *pValue = pItem->val.string;
2837 return QCBOR_SUCCESS;
2838 }
2839 break;
2840
2841 case QCBOR_TYPE_POSBIGNUM:
2842 *pValue = pItem->val.string;
2843 return QCBOR_SUCCESS;
2844 break;
2845
2846 case QCBOR_TYPE_NEGBIGNUM:
2847 *pbIsNegative = true;
2848 *pValue = pItem->val.string;
2849 return QCBOR_SUCCESS;
2850 break;
2851
2852 default:
2853 return QCBOR_ERR_UNEXPECTED_TYPE;
2854 break;
2855 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002856}
2857
2858
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002859/**
2860 @param[in] bMustBeTagged If \c true, then the data item must be tagged as either
2861 a positive or negative bignum. If \c false, then it only must be a byte string and bIsNegative
2862 will always be false on the asumption that it is positive, but it can be interpretted as
2863 negative if the the sign is know from other context.
2864 @param[out] pValue The bytes that make up the big num
2865 @param[out] pbIsNegative \c true if tagged as a negative big num. \c false otherwise.
2866
2867 if bMustBeTagged is false, then this will succeed if the data item is a plain byte string,
2868 a positive big num or a negative big num.
2869
2870 */
2871void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, bool bMustBeTagged, UsefulBufC *pValue, bool *pbIsNegative)
2872{
2873 if(pMe->uLastError != QCBOR_SUCCESS) {
2874 // Already in error state, do nothing
2875 return;
2876 }
2877
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002878 QCBORItem Item;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002879 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
2880 if(uError != QCBOR_SUCCESS) {
2881 pMe->uLastError = (uint8_t)uError;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002882 return;
2883 }
2884
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002885 pMe->uLastError = (uint8_t)ConvertBigNum(&Item, pValue, pbIsNegative);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002886}
2887
Laurence Lundblade4e2da002020-06-13 23:08:31 -07002888/*
2889Public function, see header qcbor/qcbor_decode.h file
2890*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002891void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool bMustBeTagged, UsefulBufC *pValue, bool *pbIsNegative)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002892{
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002893 QCBORItem Item;
2894 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002895
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07002896 pMe->uLastError = (uint8_t)ConvertBigNum(&Item, pValue, pbIsNegative);
Laurence Lundbladec4537442020-04-14 18:53:22 -07002897}
2898
Laurence Lundbladec4537442020-04-14 18:53:22 -07002899
2900
2901
Laurence Lundbladee6430642020-03-14 21:15:44 -07002902
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002903typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002904
2905
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002906// The main exponentiator that works on only positive numbers
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002907static QCBORError Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002908{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002909 uint64_t uResult = uMantissa;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002910
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002911 if(uResult != 0) {
2912 /* This loop will run a maximum of 19 times because
2913 * UINT64_MAX < 10 ^^ 19. More than that will cause
2914 * exit with the overflow error
2915 */
2916 for(; nExponent > 0; nExponent--) {
2917 if(uResult > UINT64_MAX / 10) {
2918 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow
2919 }
2920 uResult = uResult * 10;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002921 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002922
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002923 for(; nExponent < 0; nExponent++) {
2924 uResult = uResult / 10;
2925 if(uResult == 0) {
2926 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error
2927 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002928 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002929 }
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002930 /* else, mantissa is zero so this returns zero */
Laurence Lundbladec4537442020-04-14 18:53:22 -07002931
2932 *puResult = uResult;
2933
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002934 return QCBOR_SUCCESS;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002935}
2936
2937
Laurence Lundbladee6430642020-03-14 21:15:44 -07002938/* Convert a decimal fraction to an int64_t without using
2939 floating point or math libraries. Most decimal fractions
2940 will not fit in an int64_t and this will error out with
2941 under or overflow
2942 */
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002943static QCBORError Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002944{
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002945 uint64_t uResult;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002946
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002947 uResult = uMantissa;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002948
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002949 /* This loop will run a maximum of 64 times because
Laurence Lundbladee6430642020-03-14 21:15:44 -07002950 * INT64_MAX < 2^31. More than that will cause
2951 * exist with the overflow error
2952 */
2953 while(nExponent > 0) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002954 if(uResult > UINT64_MAX >> 1) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002955 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow
Laurence Lundbladee6430642020-03-14 21:15:44 -07002956 }
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002957 uResult = uResult << 1;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002958 nExponent--;
2959 }
2960
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002961 while(nExponent < 0 ) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002962 if(uResult == 0) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002963 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error
2964 }
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002965 uResult = uResult >> 1;
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002966 nExponent++;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002967 }
2968
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002969 *puResult = uResult;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002970
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002971 return QCBOR_SUCCESS;
2972}
2973
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002974/*
2975 Compute value with signed mantissa and signed result. Works with exponent of 2 or 10 based on exponentiator.
2976 */
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002977static inline QCBORError ExponentiateNN(int64_t nMantissa, int64_t nExponent, int64_t *pnResult, fExponentiator pfExp)
2978{
2979 uint64_t uResult;
2980
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002981 // Take the absolute value of the mantissa and convert to unsigned.
2982 // TODO: this should be possible in one intruction
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002983 uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa;
2984
2985 // Do the exponentiation of the positive mantissa
2986 QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult);
2987 if(uReturn) {
2988 return uReturn;
2989 }
2990
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002991
Laurence Lundblade983500d2020-05-14 11:49:34 -07002992 /* (uint64_t)INT64_MAX+1 is used to represent the absolute value
2993 of INT64_MIN. This assumes two's compliment representation where
2994 INT64_MIN is one increment farther from 0 than INT64_MAX.
2995 Trying to write -INT64_MIN doesn't work to get this because the
2996 compiler tries to work with an int64_t which can't represent
2997 -INT64_MIN.
2998 */
2999 uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1;
3000
3001 // Error out if too large
3002 if(uResult > uMax) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003003 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
3004 }
3005
3006 // Casts are safe because of checks above
3007 *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult;
3008
3009 return QCBOR_SUCCESS;
3010}
3011
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07003012/*
3013 Compute value with signed mantissa and unsigned result. Works with exponent of 2 or 10 based on exponentiator.
3014 */
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003015static inline QCBORError ExponentitateNU(int64_t nMantissa, int64_t nExponent, uint64_t *puResult, fExponentiator pfExp)
3016{
3017 if(nMantissa < 0) {
3018 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3019 }
3020
3021 // Cast to unsigned is OK because of check for negative
3022 // Cast to unsigned is OK because UINT64_MAX > INT64_MAX
3023 // Exponentiation is straight forward
3024 return (*pfExp)((uint64_t)nMantissa, nExponent, puResult);
3025}
3026
3027
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003028#include <math.h>
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003029
3030
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07003031static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003032{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003033 uint64_t uResult;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003034
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003035 uResult = 0;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003036 const uint8_t *pByte = BigNum.ptr;
3037 size_t uLen = BigNum.len;
3038 while(uLen--) {
Laurence Lundblade313b2862020-05-16 01:23:06 -07003039 if(uResult > (uMax >> 8)) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07003040 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003041 }
Laurence Lundblade313b2862020-05-16 01:23:06 -07003042 uResult = (uResult << 8) + *pByte++;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003043 }
3044
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003045 *pResult = uResult;
3046 return QCBOR_SUCCESS;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003047}
3048
Laurence Lundblade887add82020-05-17 05:50:34 -07003049static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003050{
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003051 return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003052}
3053
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003054static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003055{
3056 uint64_t uResult;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003057 QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult);
3058 if(uError) {
3059 return uError;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003060 }
3061 /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */
3062 *pResult = (int64_t)uResult;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003063 return QCBOR_SUCCESS;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003064}
3065
3066
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003067static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003068{
3069 uint64_t uResult;
Laurence Lundbladeda095972020-06-06 18:35:33 -07003070 /* negaative int furthest from zero is INT64_MIN
3071 which is expressed as -INT64_MAX-1. The value of
3072 a negative bignum is -n-1, one further from zero
3073 than the positive bignum */
3074
3075 /* say INT64_MIN is -2; then INT64_MAX is 1.
3076 Then -n-1 <= INT64_MIN.
3077 Then -n -1 <= -INT64_MAX - 1
3078 THen n <= INT64_MAX. */
3079 QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003080 if(uError) {
3081 return uError;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003082 }
3083 /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */
Laurence Lundblade887add82020-05-17 05:50:34 -07003084 // TODO: this code is incorrect. See RFC 7049
Laurence Lundbladeda095972020-06-06 18:35:33 -07003085 uResult++; // this is the -1 in -n-1
Laurence Lundbladee6430642020-03-14 21:15:44 -07003086 *pResult = -(int64_t)uResult;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003087 return QCBOR_SUCCESS;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003088}
3089
Laurence Lundbladef6c86662020-05-12 02:08:00 -07003090#include "fenv.h"
Laurence Lundbladec4537442020-04-14 18:53:22 -07003091
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003092
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003093/*
3094Convert a integers and floats to an int64_t.
3095
3096\param[in] uOptions Bit mask list of conversion options.
3097
3098\retval QCBOR_ERR_CONVERSION_NOT_REQUESTED Conversion, possible, but not requested in uOptions.
3099
3100\retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
3101
3102\retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large or too small.
3103
3104*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003105static QCBORError ConvertInt64(const QCBORItem *pItem, uint32_t uOptions, int64_t *pnValue)
3106{
3107 switch(pItem->uDataType) {
3108 // TODO: float when ifdefs are set
3109 case QCBOR_TYPE_DOUBLE:
3110 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
3111 // TODO: what about under/overflow here?
3112 // Invokes the floating-point HW and/or compiler-added libraries
3113 feclearexcept(FE_ALL_EXCEPT);
3114 *pnValue = llround(pItem->val.dfnum);
3115 if(fetestexcept(FE_INVALID)) {
3116 // TODO: better error code
3117 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
3118 }
3119 } else {
3120 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3121 }
3122 break;
3123
3124 case QCBOR_TYPE_INT64:
3125 if(uOptions & QCBOR_CONVERT_TYPE_INT64) {
3126 *pnValue = pItem->val.int64;
3127 } else {
3128 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3129 }
3130 break;
3131
3132 case QCBOR_TYPE_UINT64:
3133 if(uOptions & QCBOR_CONVERT_TYPE_UINT64) {
3134 if(pItem->val.uint64 < INT64_MAX) {
3135 *pnValue = pItem->val.int64;
3136 } else {
3137 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
3138 }
3139 } else {
3140 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3141 }
3142 break;
3143
3144 default:
3145 return QCBOR_ERR_UNEXPECTED_TYPE;
3146 }
3147 return QCBOR_SUCCESS;
3148}
3149
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003150
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003151void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe,
3152 uint32_t uOptions,
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003153 int64_t *pnValue,
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003154 QCBORItem *pItem)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003155{
Laurence Lundbladebf3c42d2020-04-14 19:08:51 -07003156 if(pMe->uLastError != QCBOR_SUCCESS) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07003157 return;
3158 }
3159
Laurence Lundbladee6430642020-03-14 21:15:44 -07003160 QCBORItem Item;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003161 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
3162 if(uError) {
3163 pMe->uLastError = (uint8_t)uError;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003164 return;
3165 }
3166
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003167 if(pItem) {
3168 *pItem = Item;
3169 }
3170
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003171 pMe->uLastError = (uint8_t)ConvertInt64(&Item, uOptions, pnValue);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003172}
3173
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003174
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003175void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe,
3176 int64_t nLabel,
3177 uint32_t uOptions,
3178 int64_t *pnValue,
3179 QCBORItem *pItem)
3180{
3181 QCBORItem Item;
3182 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
3183
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003184 pMe->uLastError = (uint8_t)ConvertInt64(&Item, uOptions, pnValue);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003185}
3186
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003187
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003188void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe,
3189 const char * szLabel,
3190 uint32_t uOptions,
3191 int64_t *pnValue,
3192 QCBORItem *pItem)
3193{
3194 if(pMe->uLastError != QCBOR_SUCCESS) {
3195 return;
3196 }
3197
3198 QCBORItem Item;
3199 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
3200
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003201 pMe->uLastError = (uint8_t)ConvertInt64(&Item, uOptions, pnValue);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003202}
3203
3204
3205
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003206/*
3207 Convert a large variety of integer types to an int64_t.
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003208
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003209 \param[in] uOptions Bit mask list of conversion options.
3210
3211 \retval QCBOR_ERR_CONVERSION_NOT_REQUESTED Conversion, possible, but not requested in uOptions.
3212
3213 \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
3214
3215 \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large or too small.
3216
3217 */
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003218static QCBORError Int64ConvertAll(const QCBORItem *pItem, uint32_t uOptions, int64_t *pnValue)
3219{
3220 QCBORError uErr;
3221
3222 switch(pItem->uDataType) {
3223
3224 case QCBOR_TYPE_POSBIGNUM:
3225 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3226 return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003227 } else {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003228 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003229 }
3230 break;
3231
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003232 case QCBOR_TYPE_NEGBIGNUM:
3233 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3234 return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003235 } else {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003236 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003237 }
3238 break;
3239
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003240#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3241 case QCBOR_TYPE_DECIMAL_FRACTION:
3242 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3243 return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt,
3244 pItem->val.expAndMantissa.nExponent,
3245 pnValue,
3246 &Exponentitate10);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003247 } else {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003248 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3249 }
3250 break;
3251
3252 case QCBOR_TYPE_BIGFLOAT:
3253 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
3254 return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt,
3255 pItem->val.expAndMantissa.nExponent,
3256 pnValue,
3257 Exponentitate2);
3258 } else {
3259 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3260 }
3261 break;
3262
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003263 case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
3264 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3265 int64_t nMantissa;
3266 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3267 if(uErr) {
3268 return uErr;
3269 }
3270 return ExponentiateNN(nMantissa,
3271 pItem->val.expAndMantissa.nExponent,
3272 pnValue,
3273 Exponentitate10);
3274 } else {
3275 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3276 }
3277 break;
3278
3279 case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
3280 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3281 int64_t nMantissa;
3282 uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3283 if(uErr) {
3284 return uErr;
3285 }
3286 return ExponentiateNN(nMantissa,
3287 pItem->val.expAndMantissa.nExponent,
3288 pnValue,
3289 Exponentitate10);
3290 } else {
3291 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3292 }
3293 break;
3294
3295 case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
3296 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3297 int64_t nMantissa;
3298 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3299 if(uErr) {
3300 return uErr;
3301 }
3302 return ExponentiateNN(nMantissa,
3303 pItem->val.expAndMantissa.nExponent,
3304 pnValue,
3305 Exponentitate2);
3306 } else {
3307 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3308 }
3309 break;
3310
3311 case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
3312 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3313 int64_t nMantissa;
3314 uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3315 if(uErr) {
3316 return uErr;
3317 }
3318 return ExponentiateNN(nMantissa,
3319 pItem->val.expAndMantissa.nExponent,
3320 pnValue,
3321 Exponentitate2);
3322 } else {
3323 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003324 }
3325 break;
3326
Laurence Lundbladec4537442020-04-14 18:53:22 -07003327 default:
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003328 return QCBOR_ERR_UNEXPECTED_TYPE;
3329#endif
Laurence Lundbladec4537442020-04-14 18:53:22 -07003330 }
3331}
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003332
3333
Laurence Lundbladec4537442020-04-14 18:53:22 -07003334/*
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003335 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003336 */
3337void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, int64_t *pnValue)
Laurence Lundbladec4537442020-04-14 18:53:22 -07003338{
3339 QCBORItem Item;
3340
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003341 QCBORDecode_GetInt64ConvertInternal(pMe, uOptions, pnValue, &Item);
Laurence Lundbladec4537442020-04-14 18:53:22 -07003342
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003343 if(pMe->uLastError == QCBOR_SUCCESS) {
3344 // The above conversion succeeded
3345 return;
3346 }
3347
Laurence Lundbladef6c86662020-05-12 02:08:00 -07003348 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003349 // The above conversion failed in a way that code below can't correct
Laurence Lundbladec4537442020-04-14 18:53:22 -07003350 return;
3351 }
3352
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003353 pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uOptions, pnValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003354}
3355
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003356
3357/*
3358Public function, see header qcbor/qcbor_decode.h file
3359*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003360void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, int64_t nLabel, uint32_t uOptions, int64_t *pnValue)
3361{
3362 QCBORItem Item;
3363
3364 QCBORDecode_GetInt64ConvertInternalInMapN(pMe, nLabel, uOptions, pnValue, &Item);
3365
3366 if(pMe->uLastError == QCBOR_SUCCESS) {
3367 // The above conversion succeeded
3368 return;
3369 }
3370
3371 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3372 // The above conversion failed in a way that code below can't correct
3373 return;
3374 }
3375
3376 pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uOptions, pnValue);
3377}
3378
3379
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003380/*
3381Public function, see header qcbor/qcbor_decode.h file
3382*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003383void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, uint32_t uOptions, int64_t *pnValue)
3384{
3385 QCBORItem Item;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003386 QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, szLabel, uOptions, pnValue, &Item);
3387
3388 if(pMe->uLastError == QCBOR_SUCCESS) {
3389 // The above conversion succeeded
3390 return;
3391 }
3392
3393 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3394 // The above conversion failed in a way that code below can't correct
3395 return;
3396 }
3397
3398 pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uOptions, pnValue);
3399}
3400
3401
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003402static QCBORError ConvertUint64(const QCBORItem *pItem, uint32_t uOptions, uint64_t *puValue)
3403{
3404 switch(pItem->uDataType) {
3405 // TODO: type flaot
3406 case QCBOR_TYPE_DOUBLE:
3407 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
3408 feclearexcept(FE_ALL_EXCEPT);
3409 double dRounded = round(pItem->val.dfnum);
3410 // TODO: over/underflow
3411 if(fetestexcept(FE_INVALID)) {
3412 // TODO: better error code
3413 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
3414 } else if(isnan(dRounded)) {
3415 // TODO: better error code
3416 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
3417 } else if(dRounded >= 0) {
3418 *puValue = (uint64_t)dRounded;
3419 } else {
3420 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3421 }
3422 } else {
3423 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3424 }
3425 break;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003426
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003427 case QCBOR_TYPE_INT64:
3428 if(uOptions & QCBOR_CONVERT_TYPE_INT64) {
3429 if(pItem->val.int64 >= 0) {
3430 *puValue = (uint64_t)pItem->val.int64;
3431 } else {
3432 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3433 }
3434 } else {
3435 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3436 }
3437 break;
3438
3439 case QCBOR_TYPE_UINT64:
3440 if(uOptions & QCBOR_CONVERT_TYPE_UINT64) {
3441 *puValue = pItem->val.uint64;
3442 } else {
3443 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3444 }
3445 break;
3446
3447 default:
3448 return QCBOR_ERR_UNEXPECTED_TYPE;
3449 }
3450 return QCBOR_SUCCESS;
3451}
Laurence Lundbladec4537442020-04-14 18:53:22 -07003452
3453
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003454void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe,
3455 uint32_t uOptions,
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003456 uint64_t *puValue,
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003457 QCBORItem *pItem)
Laurence Lundbladec4537442020-04-14 18:53:22 -07003458{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003459 if(pMe->uLastError != QCBOR_SUCCESS) {
3460 return;
3461 }
3462
Laurence Lundbladec4537442020-04-14 18:53:22 -07003463 QCBORItem Item;
Laurence Lundbladec4537442020-04-14 18:53:22 -07003464
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003465 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
3466 if(uError) {
3467 pMe->uLastError = (uint8_t)uError;
Laurence Lundbladec4537442020-04-14 18:53:22 -07003468 return;
3469 }
3470
Laurence Lundbladea826c502020-05-10 21:07:00 -07003471 if(pItem) {
3472 *pItem = Item;
3473 }
3474
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003475 pMe->uLastError = (uint8_t)ConvertUint64(&Item, uOptions, puValue);
Laurence Lundbladec4537442020-04-14 18:53:22 -07003476}
3477
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003478
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003479void QCBORDecode_GetUint64ConvertInternalInMapN(QCBORDecodeContext *pMe,
3480 int64_t nLabel,
3481 uint32_t uOptions,
3482 uint64_t *puValue,
3483 QCBORItem *pItem)
3484{
3485 QCBORItem Item;
3486 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
3487
3488 pMe->uLastError = (uint8_t)ConvertUint64(&Item, uOptions, puValue);
3489}
3490
3491
3492void QCBORDecode_GetUint64ConvertInternalInMapSZ(QCBORDecodeContext *pMe,
3493 const char * szLabel,
3494 uint32_t uOptions,
3495 uint64_t *puValue,
3496 QCBORItem *pItem)
3497{
3498 if(pMe->uLastError != QCBOR_SUCCESS) {
3499 return;
3500 }
3501
3502 QCBORItem Item;
3503 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
3504
3505 pMe->uLastError = (uint8_t)ConvertUint64(&Item, uOptions, puValue);
3506}
3507
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003508/*
3509 Public function, see header qcbor/qcbor_decode.h file
3510*/
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003511static QCBORError Uint64ConvertAll(const QCBORItem *pItem, uint32_t uOptions, uint64_t *puValue)
3512{
3513 QCBORError uErr;
3514
3515 switch(pItem->uDataType) {
3516
3517 case QCBOR_TYPE_POSBIGNUM:
3518 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3519 return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue);
3520 } else {
3521 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3522 }
3523 break;
3524
3525 case QCBOR_TYPE_NEGBIGNUM:
3526 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3527 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3528 } else {
3529 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3530 }
3531 break;
3532
3533#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3534
3535 case QCBOR_TYPE_DECIMAL_FRACTION:
3536 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3537 return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt,
3538 pItem->val.expAndMantissa.nExponent,
3539 puValue,
3540 Exponentitate10);
3541 } else {
3542 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3543 }
3544 break;
3545
3546 case QCBOR_TYPE_BIGFLOAT:
3547 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
3548 return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt,
3549 pItem->val.expAndMantissa.nExponent,
3550 puValue,
3551 Exponentitate2);
3552 } else {
3553 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3554 }
3555 break;
3556
3557 case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
3558 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3559 // TODO: Would be better to convert to unsigned
3560 int64_t nMantissa;
3561 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3562 if(uErr != QCBOR_SUCCESS) {
3563 return uErr;
3564 }
3565 return ExponentitateNU(nMantissa,
3566 pItem->val.expAndMantissa.nExponent,
3567 puValue,
3568 Exponentitate10);
3569 } else {
3570 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3571 }
3572 break;
3573
3574 case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
3575 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3576 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3577 } else {
3578 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3579 }
3580 break;
3581
3582 case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
3583 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3584 // TODO: Would be better to convert to unsigned
3585 int64_t nMantissa;
3586 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3587 if(uErr != QCBOR_SUCCESS) {
3588 return uErr;
3589 }
3590 return ExponentitateNU(nMantissa,
3591 pItem->val.expAndMantissa.nExponent,
3592 puValue,
3593 Exponentitate2);
3594 } else {
3595 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3596 }
3597 break;
3598
3599 case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
3600 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3601 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3602 } else {
3603 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3604 }
3605 break;
3606#endif
3607 default:
3608 return QCBOR_ERR_UNEXPECTED_TYPE;
3609 }
3610}
3611
Laurence Lundblade4e2da002020-06-13 23:08:31 -07003612/*
3613 Public function, see header qcbor/qcbor_decode.h file
3614*/
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003615void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, uint64_t *puValue)
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003616{
3617 QCBORItem Item;
3618
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003619 QCBORDecode_GetUInt64ConvertInternal(pMe, uOptions, puValue, &Item);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003620
Laurence Lundbladef6c86662020-05-12 02:08:00 -07003621 if(pMe->uLastError == QCBOR_SUCCESS) {
3622 // The above conversion succeeded
3623 return;
3624 }
3625
3626 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3627 // The above conversion failed in a way that code below can't correct
Laurence Lundbladee6430642020-03-14 21:15:44 -07003628 return;
3629 }
3630
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003631 pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uOptions, puValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003632}
3633
Laurence Lundbladec4537442020-04-14 18:53:22 -07003634
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003635/*
Laurence Lundblade4e2da002020-06-13 23:08:31 -07003636 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003637*/
3638void QCBORDecode_GetUint64ConvertAllInMapN(QCBORDecodeContext *pMe, int64_t nLabel, uint32_t uOptions, uint64_t *puValue)
3639{
3640 QCBORItem Item;
3641
3642 QCBORDecode_GetUint64ConvertInternalInMapN(pMe, nLabel, uOptions, puValue, &Item);
3643
3644 if(pMe->uLastError == QCBOR_SUCCESS) {
3645 // The above conversion succeeded
3646 return;
3647 }
3648
3649 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3650 // The above conversion failed in a way that code below can't correct
3651 return;
3652 }
3653
3654 pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uOptions, puValue);
3655}
3656
3657
3658/*
Laurence Lundblade4e2da002020-06-13 23:08:31 -07003659 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003660*/
3661void QCBORDecode_GetUint64ConvertAllInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, uint32_t uOptions, uint64_t *puValue)
3662{
3663 QCBORItem Item;
3664 QCBORDecode_GetUint64ConvertInternalInMapSZ(pMe, szLabel, uOptions, puValue, &Item);
3665
3666 if(pMe->uLastError == QCBOR_SUCCESS) {
3667 // The above conversion succeeded
3668 return;
3669 }
3670
3671 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3672 // The above conversion failed in a way that code below can't correct
3673 return;
3674 }
3675
3676 pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uOptions, puValue);
3677}
3678
3679
3680static QCBORError ConvertDouble(const QCBORItem *pItem, uint32_t uOptions, double *pdValue)
3681{
3682 switch(pItem->uDataType) {
3683 // TODO: float when ifdefs are set
3684 case QCBOR_TYPE_DOUBLE:
3685 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
3686 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
3687 *pdValue = pItem->val.dfnum;
3688 } else {
3689 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3690 }
3691 }
3692 break;
3693
3694 case QCBOR_TYPE_INT64:
3695 if(uOptions & QCBOR_CONVERT_TYPE_INT64) {
3696 // TODO: how does this work?
3697 *pdValue = (double)pItem->val.int64;
3698
3699 } else {
3700 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3701 }
3702 break;
3703
3704 case QCBOR_TYPE_UINT64:
3705 if(uOptions & QCBOR_CONVERT_TYPE_UINT64) {
3706 *pdValue = (double)pItem->val.uint64;
3707 } else {
3708 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3709 }
3710 break;
3711
3712 default:
3713 return QCBOR_ERR_UNEXPECTED_TYPE;
3714 }
3715
3716 return QCBOR_SUCCESS;
3717}
3718
3719
3720
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003721void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe,
3722 uint32_t uOptions,
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003723 double *pdValue,
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003724 QCBORItem *pItem)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003725{
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003726 if(pMe->uLastError != QCBOR_SUCCESS) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07003727 return;
3728 }
3729
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003730 QCBORItem Item;
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003731
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003732 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003733 if(uError) {
3734 pMe->uLastError = (uint8_t)uError;
3735 return;
3736 }
3737
3738 if(pItem) {
3739 *pItem = Item;
3740 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07003741
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003742 pMe->uLastError = (uint8_t)ConvertDouble(&Item, uOptions, pdValue);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003743}
Laurence Lundbladec4537442020-04-14 18:53:22 -07003744
Laurence Lundbladec4537442020-04-14 18:53:22 -07003745
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003746void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe,
3747 int64_t nLabel,
3748 uint32_t uOptions,
3749 double *pdValue,
3750 QCBORItem *pItem)
3751{
3752 QCBORItem Item;
3753 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
3754
3755 pMe->uLastError = (uint8_t)ConvertDouble(&Item, uOptions, pdValue);
3756}
3757
3758void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe,
3759 const char * szLabel,
3760 uint32_t uOptions,
3761 double *pdValue,
3762 QCBORItem *pItem)
3763{
3764 if(pMe->uLastError != QCBOR_SUCCESS) {
3765 return;
3766 }
3767
3768 QCBORItem Item;
3769 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
3770
3771 pMe->uLastError = (uint8_t)ConvertDouble(&Item, uOptions, pdValue);
3772}
3773
3774
3775
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07003776static double ConvertBigNumToDouble(const UsefulBufC BigNum)
3777{
3778 double dResult;
3779
3780 dResult = 0.0;
3781 const uint8_t *pByte = BigNum.ptr;
3782 size_t uLen = BigNum.len;
3783 /* This will overflow and become the float value INFINITY if the number
3784 is too large to fit. No error will be logged.
3785 TODO: should an error be logged? */
3786 while(uLen--) {
3787 dResult = (dResult * 256.0) + (double)*pByte++;
3788 }
3789
3790 return dResult;
3791}
3792
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003793static QCBORError DoubleConvertAll(const QCBORItem *pItem, uint32_t uOptions, double *pdValue)
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003794{
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003795 /*
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003796 https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
3797
3798 */
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003799 switch(pItem->uDataType) {
3800 // TODO: type float
3801 case QCBOR_TYPE_DECIMAL_FRACTION:
3802 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3803 // TODO: rounding and overflow errors
3804 *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt *
3805 pow(10.0, (double)pItem->val.expAndMantissa.nExponent);
3806 } else {
3807 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3808 }
3809 break;
3810
3811 case QCBOR_TYPE_BIGFLOAT:
3812 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT ) {
3813 *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt *
3814 exp2((double)pItem->val.expAndMantissa.nExponent);
3815 } else {
3816 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3817 }
3818 break;
3819
3820 case QCBOR_TYPE_POSBIGNUM:
3821 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3822 *pdValue = ConvertBigNumToDouble(pItem->val.bigNum);
3823 } else {
3824 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3825 }
3826 break;
3827
3828 case QCBOR_TYPE_NEGBIGNUM:
3829 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
Laurence Lundbladeda095972020-06-06 18:35:33 -07003830 *pdValue = -1-ConvertBigNumToDouble(pItem->val.bigNum);
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003831 } else {
3832 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3833 }
3834 break;
3835
3836 case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
3837 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3838 double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
3839 *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent);
3840 } else {
3841 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3842 }
3843 break;
3844
3845 case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
3846 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3847 double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
3848 *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent);
3849 } else {
3850 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3851 }
3852 break;
3853
3854 case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
3855 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
3856 double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
3857 *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent);
3858 } else {
3859 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3860 }
3861 break;
3862
3863 case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
3864 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
Laurence Lundbladeda095972020-06-06 18:35:33 -07003865 double dMantissa = -1-ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003866 *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent);
3867 } else {
3868 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3869 }
3870 break;
3871
3872 default:
3873 return QCBOR_ERR_UNEXPECTED_TYPE;
3874 }
3875
3876 return QCBOR_SUCCESS;
3877}
3878
3879
3880/*
Laurence Lundblade4e2da002020-06-13 23:08:31 -07003881 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003882*/
3883void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, double *pdValue)
3884{
3885
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003886 QCBORItem Item;
3887
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003888 QCBORDecode_GetDoubleConvertInternal(pMe, uOptions, pdValue, &Item);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003889
3890 if(pMe->uLastError == QCBOR_SUCCESS) {
3891 // The above conversion succeeded
3892 return;
3893 }
3894
3895 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3896 // The above conversion failed in a way that code below can't correct
3897 return;
3898 }
3899
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003900 pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uOptions, pdValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003901}
3902
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003903
3904/*
Laurence Lundblade4e2da002020-06-13 23:08:31 -07003905 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003906*/
3907void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, int64_t nLabel, uint32_t uOptions, double *pdValue)
3908{
3909 QCBORItem Item;
3910
3911 QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uOptions, pdValue, &Item);
3912
3913 if(pMe->uLastError == QCBOR_SUCCESS) {
3914 // The above conversion succeeded
3915 return;
3916 }
3917
3918 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3919 // The above conversion failed in a way that code below can't correct
3920 return;
3921 }
3922
3923 pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uOptions, pdValue);
3924}
3925
3926
3927/*
Laurence Lundblade4e2da002020-06-13 23:08:31 -07003928 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003929*/
3930void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, uint32_t uOptions, double *pdValue)
3931{
3932 QCBORItem Item;
3933 QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uOptions, pdValue, &Item);
3934
3935 if(pMe->uLastError == QCBOR_SUCCESS) {
3936 // The above conversion succeeded
3937 return;
3938 }
3939
3940 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3941 // The above conversion failed in a way that code below can't correct
3942 return;
3943 }
3944
3945 pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uOptions, pdValue);
3946}