blob: 5a456bec5c798aa59787ff663176a10832f9e86c [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{
128 // Check in DecodeNesting_Descend and never having
129 // QCBOR_MAX_ARRAY_NESTING > 255 gaurantees cast is safe
130 return (uint8_t)(pNesting->pCurrent - &(pNesting->pMapsAndArrays[0]));
131}
132
133
134inline static bool InBoundMode(const QCBORDecodeNesting *pNesting)
135{
136 return pNesting->pCurrent->uType & QCBOR_NEST_TYPE_IS_BOUND;
137}
138
139/*inline static bool IsArray(const QCBORDecodeNesting *pNesting)
140{
141 const unsigned uIndex = DecodeNesting_GetLevel(pNesting);
142
143 return (0x01ULL << ((uIndex * 3) + 1)) & pNesting->uTypeBitMap;
144}
145
146inline static bool IsBstr(const QCBORDecodeNesting *pNesting)
147{
148 const unsigned uIndex = DecodeNesting_GetLevel(pNesting);
149
150 return (0x01ULL << ((uIndex * 3) + 2)) & pNesting->uTypeBitMap;
151}*/
152
153
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700154inline static bool
155DecodeNesting_IsAtTop(const QCBORDecodeNesting *pNesting)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700156{
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700157 if(pNesting->pCurrent == &(pNesting->pMapsAndArrays[0])) {
158 return true;
159 } else {
160 return false;
161 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700162}
163
Laurence Lundblade937ea812020-05-08 11:38:23 -0700164// Determine if at the end of a map or array while in map mode
Laurence Lundblade3f9ef042020-04-14 13:15:51 -0700165inline static bool
166DecodeNesting_AtEnd(const QCBORDecodeNesting *pNesting)
167{
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700168 if(pNesting->pCurrentMap && InBoundMode(pNesting)) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700169 if(pNesting->pCurrentMap->uCount == 0) {
Laurence Lundblade937ea812020-05-08 11:38:23 -0700170 // TODO: won't work for indefinite length
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 Lundblade82c2a8f2020-04-29 12:40:19 -0700191inline static int
192DecodeNesting_InMapMode(const QCBORDecodeNesting *pNesting)
193{
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700194 return (bool)InBoundMode(pNesting);
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -0700195}
196
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700197
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800198
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700199inline static uint8_t
200DecodeNesting_GetMapModeLevel(QCBORDecodeNesting *pNesting)
201{
202 // Check in DecodeNesting_Descend and never having
203 // QCBOR_MAX_ARRAY_NESTING > 255 gaurantees cast is safe
204 return (uint8_t)(pNesting->pCurrentMap - &(pNesting->pMapsAndArrays[0]));
205}
206
Laurence Lundbladeee851742020-01-08 08:37:05 -0800207inline static int
208DecodeNesting_TypeIsMap(const QCBORDecodeNesting *pNesting)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700209{
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700210 if(DecodeNesting_IsAtTop(pNesting)) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700211 return 0;
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800213
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700214 return CBOR_MAJOR_TYPE_MAP == pNesting->pCurrent->uMajorType;
215}
216
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800217// Process a break. This will either ascend the nesting or error out
Laurence Lundbladeee851742020-01-08 08:37:05 -0800218inline static QCBORError
219DecodeNesting_BreakAscend(QCBORDecodeNesting *pNesting)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700220{
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800221 // breaks must always occur when there is nesting
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700222 if(DecodeNesting_IsAtTop(pNesting)) {
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800223 return QCBOR_ERR_BAD_BREAK;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800225
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800226 // breaks can only occur when the map/array is indefinite length
227 if(!DecodeNesting_IsIndefiniteLength(pNesting)) {
228 return QCBOR_ERR_BAD_BREAK;
229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800230
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800231 // if all OK, the break reduces the level of nesting
232 pNesting->pCurrent--;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800233
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800234 return QCBOR_SUCCESS;
235}
236
Laurence Lundblade3f9ef042020-04-14 13:15:51 -0700237// Called on every single item except breaks including decode of a map/array
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700238/* Decrements the map/array counter if possible. If decrement
239 closed out a map or array, then level up in nesting and decrement
240 again, until, the top is reached or the end of a map mode is reached
241 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800242inline static void
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -0700243DecodeNesting_DecrementCount(QCBORDecodeNesting *pNesting)
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800244{
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700245 while(!DecodeNesting_IsAtTop(pNesting)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700246 // Not at the top level, so there is decrementing to be done.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800247
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800248 if(!DecodeNesting_IsIndefiniteLength(pNesting)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700249 // Decrement the current nesting level if it is not indefinite.
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800250 pNesting->pCurrent->uCount--;
251 }
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700252
253 if(pNesting->pCurrent->uCount != 0) {
254 // Did not close out an array or map, so nothing further
255 break;
256 }
Laurence Lundblade3f9ef042020-04-14 13:15:51 -0700257
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700258 if(InBoundMode(pNesting)) {
Laurence Lundblade3f9ef042020-04-14 13:15:51 -0700259 // In map mode the level-up must be done explicitly
260 break;
261 }
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700262
263 // Closed out an array or map so level up
264 pNesting->pCurrent--;
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700265 /*if(pNesting->pCurrent->uMapMode) {
266 // Bring the current map level along if new level is a map
267 // TODO: must search up until a mapmode level is found.
268 pNesting->pCurrentMap = pNesting->pCurrent;
269 } */
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700270
271 // Continue with loop to see if closing out this doesn't close out more
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700272 }
273}
274
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -0700275inline static void
276DecodeNesting_EnterMapMode(QCBORDecodeNesting *pNesting, size_t uOffset)
277{
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700278 /* Have descended into this is called. The job here is just to mark it in bounded mode */
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700279 pNesting->pCurrentMap = pNesting->pCurrent;
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700280 pNesting->pCurrentMap->uType |= QCBOR_NEST_TYPE_IS_BOUND;
281 // Cast to uint32_t is safe because QCBOR restricts encoded input to < UINT32_MAX
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700282 pNesting->pCurrentMap->uOffset = (uint32_t)uOffset;
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -0700283}
284
285inline static void
286DecodeNesting_Exit(QCBORDecodeNesting *pNesting)
287{
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700288 pNesting->pCurrentMap->uType &= ~QCBOR_NEST_TYPE_IS_BOUND;
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700289 pNesting->pCurrent = pNesting->pCurrentMap - 1; // TODO error check
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -0700290
291 DecodeNesting_DecrementCount(pNesting);
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700292
293 while(1) {
294 pNesting->pCurrentMap--;
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700295 if(InBoundMode(pNesting)) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -0700296 break;
297 }
298 if(pNesting->pCurrentMap == &(pNesting->pMapsAndArrays[0])) {
299 break;
300 }
301 }
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -0700302}
303
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800304// Called on every map/array
Laurence Lundbladeee851742020-01-08 08:37:05 -0800305inline static QCBORError
306DecodeNesting_Descend(QCBORDecodeNesting *pNesting, QCBORItem *pItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700307{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700308 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800309
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800310 if(pItem->val.uCount == 0) {
311 // Nothing to do for empty definite lenth arrays. They are just are
312 // effectively the same as an item that is not a map or array
313 goto Done;
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530314 // Empty indefinite length maps and arrays are handled elsewhere
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800315 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800316
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800317 // Error out if arrays is too long to handle
318 if(pItem->val.uCount != UINT16_MAX && pItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY) {
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700319 nReturn = QCBOR_ERR_ARRAY_TOO_LONG;
320 goto Done;
321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800322
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800323 // Error out if nesting is too deep
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700324 if(pNesting->pCurrent >= &(pNesting->pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING])) {
325 nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
326 goto Done;
327 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800328
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800329 // The actual descend
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700330 pNesting->pCurrent++;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800331
Laurence Lundblade3a760b02018-10-08 13:46:03 +0800332 // Record a few details for this nesting level
333 pNesting->pCurrent->uMajorType = pItem->uDataType;
334 pNesting->pCurrent->uCount = pItem->val.uCount;
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -0700335 pNesting->pCurrent->uSaveCount = pItem->val.uCount;
Laurence Lundblade9c905e82020-04-25 11:31:38 -0700336 pNesting->pCurrent->uMapMode = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800337
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700338Done:
339 return nReturn;;
340}
341
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700342inline static QCBORError
343DecodeNesting_Descend2(QCBORDecodeNesting *pNesting, uint8_t uQCBORType, uint64_t uCount, uint32_t uEndOffset)
344{
345 QCBORError nReturn = QCBOR_SUCCESS;
346
347 if(uCount == 0) {
348 // Nothing to do for empty definite lenth arrays. They are just are
349 // effectively the same as an item that is not a map or array
350 goto Done;
351 // Empty indefinite length maps and arrays are handled elsewhere
352 }
353
354 // Error out if arrays is too long to handle
355 if(uCount != UINT16_MAX && uCount > QCBOR_MAX_ITEMS_IN_ARRAY) {
356 nReturn = QCBOR_ERR_ARRAY_TOO_LONG;
357 goto Done;
358 }
359
360 // Error out if nesting is too deep
361 if(pNesting->pCurrent >= &(pNesting->pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING])) {
362 nReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
363 goto Done;
364 }
365
366 // The actual descend
367 pNesting->pCurrent++;
368
369 // Fill in the new level fully
370 pNesting->pCurrent->uMajorType = uQCBORType;
371 pNesting->pCurrent->uCount = (uint16_t)uCount;
372 pNesting->pCurrent->uSaveCount = (uint16_t)uCount;
373 pNesting->pCurrent->uEndOffset = uEndOffset;
374 pNesting->pCurrent->uMapMode = 0;
375
376Done:
377 return nReturn;;
378}
379
380
381
Laurence Lundbladeee851742020-01-08 08:37:05 -0800382inline static void
383DecodeNesting_Init(QCBORDecodeNesting *pNesting)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700384{
385 pNesting->pCurrent = &(pNesting->pMapsAndArrays[0]);
386}
387
388
Laurence Lundbladeb340ba72020-05-14 11:41:10 -0700389static void DecodeNesting_PrepareForMapSearch(QCBORDecodeNesting *pNesting, QCBORDecodeNesting *pSave)
390{
391 *pSave = *pNesting;
392 pNesting->pCurrent = pNesting->pCurrentMap;
393
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700394 if(!DecodeNesting_IsIndefiniteLength(pNesting)) {
Laurence Lundbladeb340ba72020-05-14 11:41:10 -0700395 pNesting->pCurrent->uCount = pNesting->pCurrent->uSaveCount;
396 }
397}
398
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700399static inline void DecodeNesting_RestoreFromMapSearch(QCBORDecodeNesting *pNesting, QCBORDecodeNesting *pSave)
Laurence Lundbladeb340ba72020-05-14 11:41:10 -0700400{
401 *pNesting = *pSave;
402}
403
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700404inline static QCBORError DecodeNesting_EnterBstr(QCBORDecodeNesting *pNesting, uint32_t uEndOffset)
405{
406 QCBORError uReturn ;
407
408 // Error out if nesting is too deep
409 if(pNesting->pCurrent >= &(pNesting->pMapsAndArrays[QCBOR_MAX_ARRAY_NESTING])) {
410 uReturn = QCBOR_ERR_ARRAY_NESTING_TOO_DEEP;
411 goto Done;
412 }
413
414 // The actual descend
415 pNesting->pCurrent++;
416
417 // Record a few details for this nesting level
418 pNesting->pCurrent->uMajorType = 1; // TODO the right value for a bstr
419 pNesting->pCurrent->uCount = 0xffff;
420 pNesting->pCurrent->uSaveCount = 0xffff;
421 pNesting->pCurrent->uType = 0;
422
423 uReturn = QCBOR_SUCCESS;
424
425Done:
426 return uReturn;
427
428
429}
430
Laurence Lundbladeb340ba72020-05-14 11:41:10 -0700431
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700432
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700433
Laurence Lundbladeee851742020-01-08 08:37:05 -0800434/*===========================================================================
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800435 QCBORStringAllocate -- STRING ALLOCATOR INVOCATION
436
437 The following four functions are pretty wrappers for invocation of
438 the string allocator supplied by the caller.
439
Laurence Lundbladeee851742020-01-08 08:37:05 -0800440 ===========================================================================*/
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800441
Laurence Lundbladeee851742020-01-08 08:37:05 -0800442static inline void
443StringAllocator_Free(const QCORInternalAllocator *pMe, void *pMem)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800444{
445 (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, 0);
446}
447
Laurence Lundbladeee851742020-01-08 08:37:05 -0800448// StringAllocator_Reallocate called with pMem NULL is
449// equal to StringAllocator_Allocate()
450static inline UsefulBuf
451StringAllocator_Reallocate(const QCORInternalAllocator *pMe,
452 void *pMem,
453 size_t uSize)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800454{
455 return (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, uSize);
456}
457
Laurence Lundbladeee851742020-01-08 08:37:05 -0800458static inline UsefulBuf
459StringAllocator_Allocate(const QCORInternalAllocator *pMe, size_t uSize)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800460{
461 return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize);
462}
463
Laurence Lundbladeee851742020-01-08 08:37:05 -0800464static inline void
465StringAllocator_Destruct(const QCORInternalAllocator *pMe)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800466{
467 if(pMe->pfAllocator) {
468 (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0);
469 }
470}
471
472
473
Laurence Lundbladeee851742020-01-08 08:37:05 -0800474/*===========================================================================
475 QCBORDecode -- The main implementation of CBOR decoding
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700476
Laurence Lundblade844bb5c2020-03-01 17:27:25 -0800477 See qcbor/qcbor_decode.h for definition of the object
478 used here: QCBORDecodeContext
Laurence Lundbladeee851742020-01-08 08:37:05 -0800479 ===========================================================================*/
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700480/*
481 Public function, see header file
482 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800483void QCBORDecode_Init(QCBORDecodeContext *me,
484 UsefulBufC EncodedCBOR,
485 QCBORDecodeMode nDecodeMode)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700486{
487 memset(me, 0, sizeof(QCBORDecodeContext));
488 UsefulInputBuf_Init(&(me->InBuf), EncodedCBOR);
Laurence Lundbladeee851742020-01-08 08:37:05 -0800489 // Don't bother with error check on decode mode. If a bad value is
490 // passed it will just act as if the default normal mode of 0 was set.
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700491 me->uDecodeMode = (uint8_t)nDecodeMode;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700492 DecodeNesting_Init(&(me->nesting));
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700493 for(int i = 0; i < QCBOR_NUM_MAPPED_TAGS; i++) {
494 me->auMappedTags[i] = 0xffff;
495 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700496}
497
498
499/*
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700500 Public function, see header file
501 */
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800502void QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe,
503 QCBORStringAllocate pfAllocateFunction,
504 void *pAllocateContext,
505 bool bAllStrings)
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700506{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800507 pMe->StringAllocator.pfAllocator = pfAllocateFunction;
508 pMe->StringAllocator.pAllocateCxt = pAllocateContext;
509 pMe->bStringAllocateAll = bAllStrings;
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700510}
511
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800512
513/*
514 Public function, see header file
515 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800516void QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *me,
517 const QCBORTagListIn *pTagList)
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700518{
Laurence Lundblade830fbf92020-05-31 17:22:33 -0700519 // This does nothing now. It is retained for backwards compatibility
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700520}
521
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700522
523/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800524 This decodes the fundamental part of a CBOR data item, the type and
525 number
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800526
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700527 This is the Counterpart to InsertEncodedTypeAndNumber().
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800528
Laurence Lundbladeee851742020-01-08 08:37:05 -0800529 This does the network->host byte order conversion. The conversion
530 here also results in the conversion for floats in addition to that
531 for lengths, tags and integer values.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800532
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700533 This returns:
534 pnMajorType -- the major type for the item
Laurence Lundbladeee851742020-01-08 08:37:05 -0800535
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800536 puArgument -- the "number" which is used a the value for integers,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800537 tags and floats and length for strings and arrays
538
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800539 pnAdditionalInfo -- Pass this along to know what kind of float or
Laurence Lundbladeee851742020-01-08 08:37:05 -0800540 if length is indefinite
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800541
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800542 The int type is preferred to uint8_t for some variables as this
543 avoids integer promotions, can reduce code size and makes
544 static analyzers happier.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700545 */
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800546inline static QCBORError DecodeTypeAndNumber(UsefulInputBuf *pUInBuf,
547 int *pnMajorType,
548 uint64_t *puArgument,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800549 int *pnAdditionalInfo)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700550{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700551 QCBORError nReturn;
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800552
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700553 // Get the initial byte that every CBOR data item has
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800554 const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800555
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700556 // Break down the initial byte
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800557 const int nTmpMajorType = nInitialByte >> 5;
558 const int nAdditionalInfo = nInitialByte & 0x1f;
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800559
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800560 // Where the number or argument accumulates
561 uint64_t uArgument;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800562
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800563 if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) {
Laurence Lundblade24d509a2020-06-06 18:43:15 -0700564 // Need to get 1,2,4 or 8 additional argument bytes. Map
565 // LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800566 static const uint8_t aIterate[] = {1,2,4,8};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800567
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800568 // Loop getting all the bytes in the argument
569 uArgument = 0;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800570 for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) {
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800571 // This shift and add gives the endian conversion
572 uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf);
573 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800574 } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) {
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800575 // The reserved and thus-far unused additional info values
576 nReturn = QCBOR_ERR_UNSUPPORTED;
577 goto Done;
578 } else {
579 // Less than 24, additional info is argument or 31, an indefinite length
580 // No more bytes to get
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800581 uArgument = (uint64_t)nAdditionalInfo;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700582 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800583
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700584 if(UsefulInputBuf_GetError(pUInBuf)) {
585 nReturn = QCBOR_ERR_HIT_END;
586 goto Done;
587 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800588
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700589 // All successful if we got here.
590 nReturn = QCBOR_SUCCESS;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800591 *pnMajorType = nTmpMajorType;
Laurence Lundblade4c0cf842019-01-12 03:25:44 -0800592 *puArgument = uArgument;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800593 *pnAdditionalInfo = nAdditionalInfo;
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800594
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700595Done:
596 return nReturn;
597}
598
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800599
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700600/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800601 CBOR doesn't explicitly specify two's compliment for integers but all
602 CPUs use it these days and the test vectors in the RFC are so. All
603 integers in the CBOR structure are positive and the major type
604 indicates positive or negative. CBOR can express positive integers
605 up to 2^x - 1 where x is the number of bits and negative integers
606 down to 2^x. Note that negative numbers can be one more away from
607 zero than positive. Stdint, as far as I can tell, uses two's
608 compliment to represent negative integers.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800609
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700610 See http://www.unix.org/whitepapers/64bit.html for reasons int isn't
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800611 used carefully here, and in particular why it isn't used in the interface.
612 Also see
613 https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers
614
615 Int is used for values that need less than 16-bits and would be subject
616 to integer promotion and complaining by static analyzers.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700617 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800618inline static QCBORError
619DecodeInteger(int nMajorType, uint64_t uNumber, QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700620{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700621 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800622
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700623 if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) {
624 if (uNumber <= INT64_MAX) {
625 pDecodedItem->val.int64 = (int64_t)uNumber;
626 pDecodedItem->uDataType = QCBOR_TYPE_INT64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800627
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700628 } else {
629 pDecodedItem->val.uint64 = uNumber;
630 pDecodedItem->uDataType = QCBOR_TYPE_UINT64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800631
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700632 }
633 } else {
634 if(uNumber <= INT64_MAX) {
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800635 // CBOR's representation of negative numbers lines up with the
636 // two-compliment representation. A negative integer has one
637 // more in range than a positive integer. INT64_MIN is
638 // equal to (-INT64_MAX) - 1.
639 pDecodedItem->val.int64 = (-(int64_t)uNumber) - 1;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700640 pDecodedItem->uDataType = QCBOR_TYPE_INT64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800641
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700642 } else {
643 // C can't represent a negative integer in this range
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000644 // so it is an error.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700645 nReturn = QCBOR_ERR_INT_OVERFLOW;
646 }
647 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800648
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700649 return nReturn;
650}
651
652// Make sure #define value line up as DecodeSimple counts on this.
653#if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE
654#error QCBOR_TYPE_FALSE macro value wrong
655#endif
656
657#if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE
658#error QCBOR_TYPE_TRUE macro value wrong
659#endif
660
661#if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL
662#error QCBOR_TYPE_NULL macro value wrong
663#endif
664
665#if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF
666#error QCBOR_TYPE_UNDEF macro value wrong
667#endif
668
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700669#if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK
670#error QCBOR_TYPE_BREAK macro value wrong
671#endif
672
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700673#if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT
674#error QCBOR_TYPE_DOUBLE macro value wrong
675#endif
676
677#if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT
678#error QCBOR_TYPE_FLOAT macro value wrong
679#endif
680
681/*
682 Decode true, false, floats, break...
683 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800684inline static QCBORError
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800685DecodeSimple(int nAdditionalInfo, uint64_t uNumber, QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700686{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700687 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800688
Laurence Lundbladeee851742020-01-08 08:37:05 -0800689 // uAdditionalInfo is 5 bits from the initial byte compile time checks
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800690 // above make sure uAdditionalInfo values line up with uDataType values.
691 // DecodeTypeAndNumber never returns a major type > 1f so cast is safe
692 pDecodedItem->uDataType = (uint8_t)nAdditionalInfo;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800693
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800694 switch(nAdditionalInfo) {
Laurence Lundbladeee851742020-01-08 08:37:05 -0800695 // No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they are
696 // caught before this is called.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800697
Laurence Lundbladecc2ed342018-09-22 17:29:55 -0700698 case HALF_PREC_FLOAT:
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700699 pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uNumber);
700 pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700701 break;
Laurence Lundbladecc2ed342018-09-22 17:29:55 -0700702 case SINGLE_PREC_FLOAT:
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700703 pDecodedItem->val.dfnum = (double)UsefulBufUtil_CopyUint32ToFloat((uint32_t)uNumber);
704 pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700705 break;
706 case DOUBLE_PREC_FLOAT:
707 pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uNumber);
Laurence Lundblade67bd5512018-11-02 21:44:06 +0700708 pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE;
Laurence Lundblade12d32c52018-09-19 11:25:27 -0700709 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800710
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700711 case CBOR_SIMPLEV_FALSE: // 20
712 case CBOR_SIMPLEV_TRUE: // 21
713 case CBOR_SIMPLEV_NULL: // 22
714 case CBOR_SIMPLEV_UNDEF: // 23
Laurence Lundblade0f99d692018-09-26 14:39:28 -0700715 case CBOR_SIMPLE_BREAK: // 31
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700716 break; // nothing to do
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800717
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700718 case CBOR_SIMPLEV_ONEBYTE: // 24
719 if(uNumber <= CBOR_SIMPLE_BREAK) {
720 // This takes out f8 00 ... f8 1f which should be encoded as e0 … f7
Laurence Lundblade077475f2019-04-26 09:06:33 -0700721 nReturn = QCBOR_ERR_BAD_TYPE_7;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700722 goto Done;
723 }
Laurence Lundblade5e390822019-01-06 12:35:01 -0800724 /* FALLTHROUGH */
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700725 // fall through intentionally
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800726
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700727 default: // 0-19
728 pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE;
Laurence Lundbladeee851742020-01-08 08:37:05 -0800729 /*
730 DecodeTypeAndNumber will make uNumber equal to
731 uAdditionalInfo when uAdditionalInfo is < 24 This cast is
732 safe because the 2, 4 and 8 byte lengths of uNumber are in
733 the double/float cases above
734 */
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700735 pDecodedItem->val.uSimple = (uint8_t)uNumber;
736 break;
737 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800738
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700739Done:
740 return nReturn;
741}
742
743
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700744/*
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530745 Decode text and byte strings. Call the string allocator if asked to.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700746 */
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800747inline static QCBORError DecodeBytes(const QCORInternalAllocator *pAllocator,
748 int nMajorType,
749 uint64_t uStrLen,
750 UsefulInputBuf *pUInBuf,
751 QCBORItem *pDecodedItem)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700752{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700753 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800754
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800755 // CBOR lengths can be 64 bits, but size_t is not 64 bits on all CPUs.
756 // This check makes the casts to size_t below safe.
757
758 // 4 bytes less than the largest sizeof() so this can be tested by
759 // putting a SIZE_MAX length in the CBOR test input (no one will
760 // care the limit on strings is 4 bytes shorter).
761 if(uStrLen > SIZE_MAX-4) {
762 nReturn = QCBOR_ERR_STRING_TOO_LONG;
763 goto Done;
764 }
765
766 const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen);
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530767 if(UsefulBuf_IsNULLC(Bytes)) {
768 // Failed to get the bytes for this string item
769 nReturn = QCBOR_ERR_HIT_END;
770 goto Done;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700771 }
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530772
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800773 if(pAllocator) {
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530774 // We are asked to use string allocator to make a copy
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800775 UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen);
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530776 if(UsefulBuf_IsNULL(NewMem)) {
Laurence Lundblade30816f22018-11-10 13:40:22 +0700777 nReturn = QCBOR_ERR_STRING_ALLOCATE;
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530778 goto Done;
779 }
780 pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800781 pDecodedItem->uDataAlloc = 1;
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530782 } else {
783 // Normal case with no string allocator
784 pDecodedItem->val.string = Bytes;
785 }
Laurence Lundbladeee851742020-01-08 08:37:05 -0800786 const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING);
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800787 // Cast because ternary operator causes promotion to integer
788 pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING
789 : QCBOR_TYPE_TEXT_STRING);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800790
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530791Done:
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700792 return nReturn;
793}
794
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700795
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800796
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700797
798
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700799
800
Laurence Lundbladeee851742020-01-08 08:37:05 -0800801// Make sure the constants align as this is assumed by
802// the GetAnItem() implementation
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700803#if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY
804#error QCBOR_TYPE_ARRAY value not lined up with major type
805#endif
806#if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP
807#error QCBOR_TYPE_MAP value not lined up with major type
808#endif
809
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700810/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800811 This gets a single data item and decodes it including preceding
812 optional tagging. This does not deal with arrays and maps and nesting
813 except to decode the data item introducing them. Arrays and maps are
814 handled at the next level up in GetNext().
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800815
Laurence Lundbladeee851742020-01-08 08:37:05 -0800816 Errors detected here include: an array that is too long to decode,
817 hit end of buffer unexpectedly, a few forms of invalid encoded CBOR
Laurence Lundbladeb69cad72018-09-13 11:09:01 -0700818 */
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800819static QCBORError GetNext_Item(UsefulInputBuf *pUInBuf,
820 QCBORItem *pDecodedItem,
821 const QCORInternalAllocator *pAllocator)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700822{
Laurence Lundblade30816f22018-11-10 13:40:22 +0700823 QCBORError nReturn;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800824
Laurence Lundbladeee851742020-01-08 08:37:05 -0800825 /*
826 Get the major type and the number. Number could be length of more
827 bytes or the value depending on the major type nAdditionalInfo is
828 an encoding of the length of the uNumber and is needed to decode
829 floats and doubles
830 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800831 int nMajorType;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700832 uint64_t uNumber;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800833 int nAdditionalInfo;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800834
Laurence Lundblade4b09f632019-10-09 14:34:59 -0700835 memset(pDecodedItem, 0, sizeof(QCBORItem));
836
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800837 nReturn = DecodeTypeAndNumber(pUInBuf, &nMajorType, &uNumber, &nAdditionalInfo);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800838
Laurence Lundbladeee851742020-01-08 08:37:05 -0800839 // Error out here if we got into trouble on the type and number. The
840 // code after this will not work if the type and number is not good.
Laurence Lundblade3a6042e2019-06-28 19:58:04 -0700841 if(nReturn) {
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700842 goto Done;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -0700843 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800844
Laurence Lundbladeee851742020-01-08 08:37:05 -0800845 // At this point the major type and the value are valid. We've got
846 // the type and the number that starts every CBOR data item.
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800847 switch (nMajorType) {
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700848 case CBOR_MAJOR_TYPE_POSITIVE_INT: // Major type 0
849 case CBOR_MAJOR_TYPE_NEGATIVE_INT: // Major type 1
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800850 if(nAdditionalInfo == LEN_IS_INDEFINITE) {
Laurence Lundblade3a6042e2019-06-28 19:58:04 -0700851 nReturn = QCBOR_ERR_BAD_INT;
852 } else {
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800853 nReturn = DecodeInteger(nMajorType, uNumber, pDecodedItem);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -0700854 }
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700855 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800856
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700857 case CBOR_MAJOR_TYPE_BYTE_STRING: // Major type 2
858 case CBOR_MAJOR_TYPE_TEXT_STRING: // Major type 3
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800859 if(nAdditionalInfo == LEN_IS_INDEFINITE) {
860 const bool bIsBstr = (nMajorType == CBOR_MAJOR_TYPE_BYTE_STRING);
861 pDecodedItem->uDataType = (uint8_t)(bIsBstr ? QCBOR_TYPE_BYTE_STRING
862 : QCBOR_TYPE_TEXT_STRING);
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530863 pDecodedItem->val.string = (UsefulBufC){NULL, SIZE_MAX};
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700864 } else {
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800865 nReturn = DecodeBytes(pAllocator, nMajorType, uNumber, pUInBuf, pDecodedItem);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700866 }
867 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800868
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700869 case CBOR_MAJOR_TYPE_ARRAY: // Major type 4
870 case CBOR_MAJOR_TYPE_MAP: // Major type 5
871 // Record the number of items in the array or map
872 if(uNumber > QCBOR_MAX_ITEMS_IN_ARRAY) {
873 nReturn = QCBOR_ERR_ARRAY_TOO_LONG;
874 goto Done;
875 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800876 if(nAdditionalInfo == LEN_IS_INDEFINITE) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530877 pDecodedItem->val.uCount = UINT16_MAX; // Indicate indefinite length
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700878 } else {
Laurence Lundbladeee851742020-01-08 08:37:05 -0800879 // type conversion OK because of check above
880 pDecodedItem->val.uCount = (uint16_t)uNumber;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700881 }
Laurence Lundbladeee851742020-01-08 08:37:05 -0800882 // C preproc #if above makes sure constants for major types align
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800883 // DecodeTypeAndNumber never returns a major type > 7 so cast is safe
884 pDecodedItem->uDataType = (uint8_t)nMajorType;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700885 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800886
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700887 case CBOR_MAJOR_TYPE_OPTIONAL: // Major type 6, optional prepended tags
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800888 if(nAdditionalInfo == LEN_IS_INDEFINITE) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700889 nReturn = QCBOR_ERR_BAD_INT;
890 } else {
891 pDecodedItem->val.uTagV = uNumber;
892 pDecodedItem->uDataType = QCBOR_TYPE_OPTTAG;
893 }
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700894 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800895
Laurence Lundbladeee851742020-01-08 08:37:05 -0800896 case CBOR_MAJOR_TYPE_SIMPLE:
897 // Major type 7, float, double, true, false, null...
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800898 nReturn = DecodeSimple(nAdditionalInfo, uNumber, pDecodedItem);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700899 break;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800900
Laurence Lundbladeee851742020-01-08 08:37:05 -0800901 default:
902 // Never happens because DecodeTypeAndNumber() should never return > 7
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700903 nReturn = QCBOR_ERR_UNSUPPORTED;
904 break;
905 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800906
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700907Done:
908 return nReturn;
909}
910
911
912
913/*
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800914 This layer deals with indefinite length strings. It pulls all the
Laurence Lundbladeee851742020-01-08 08:37:05 -0800915 individual chunk items together into one QCBORItem using the string
916 allocator.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800917
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530918 Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700919 */
Laurence Lundbladeee851742020-01-08 08:37:05 -0800920static inline QCBORError
921GetNext_FullItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700922{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +0700923 // Stack usage; int/ptr 2 UsefulBuf 2 QCBORItem -- 96
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -0700924
925 // Get pointer to string allocator. First use is to pass it to
926 // GetNext_Item() when option is set to allocate for *every* string.
927 // Second use here is to allocate space to coallese indefinite
928 // length string items into one.
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800929 const QCORInternalAllocator *pAllocator = me->StringAllocator.pfAllocator ?
930 &(me->StringAllocator) :
931 NULL;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800932
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -0700933 QCBORError nReturn;
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800934 nReturn = GetNext_Item(&(me->InBuf),
935 pDecodedItem,
936 me->bStringAllocateAll ? pAllocator: NULL);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700937 if(nReturn) {
938 goto Done;
939 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800940
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700941 // To reduce code size by removing support for indefinite length strings, the
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530942 // code in this function from here down can be eliminated. Run tests, except
943 // indefinite length string tests, to be sure all is OK if this is removed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800944
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800945 // Only do indefinite length processing on strings
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -0700946 const uint8_t uStringType = pDecodedItem->uDataType;
947 if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700948 goto Done; // no need to do any work here on non-string types
949 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800950
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800951 // Is this a string with an indefinite length?
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530952 if(pDecodedItem->val.string.len != SIZE_MAX) {
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800953 goto Done; // length is not indefinite, so no work to do here
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700954 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800955
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530956 // Can't do indefinite length strings without a string allocator
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800957 if(pAllocator == NULL) {
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700958 nReturn = QCBOR_ERR_NO_STRING_ALLOCATOR;
959 goto Done;
960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800961
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700962 // Loop getting chunk of indefinite string
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -0700963 UsefulBufC FullString = NULLUsefulBufC;
964
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700965 for(;;) {
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700966 // Get item for next chunk
967 QCBORItem StringChunkItem;
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -0700968 // NULL string allocator passed here. Do not need to allocate
969 // chunks even if bStringAllocateAll is set.
Laurence Lundbladefae26bf2019-02-18 11:15:43 -0800970 nReturn = GetNext_Item(&(me->InBuf), &StringChunkItem, NULL);
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700971 if(nReturn) {
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700972 break; // Error getting the next chunk
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700973 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800974
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530975 // See if it is a marker at end of indefinite length string
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700976 if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) {
Laurence Lundblade20b533d2018-10-08 20:44:53 +0800977 // String is complete
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700978 pDecodedItem->val.string = FullString;
Laurence Lundblade57dd1442018-10-15 20:26:28 +0530979 pDecodedItem->uDataAlloc = 1;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700980 break;
981 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800982
Laurence Lundblade2a6850e2018-10-28 20:13:44 +0700983 // Match data type of chunk to type at beginning.
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +0530984 // Also catches error of other non-string types that don't belong.
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700985 // Also catches indefinite length strings inside indefinite length strings
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800986 if(StringChunkItem.uDataType != uStringType ||
987 StringChunkItem.val.string.len == SIZE_MAX) {
Laurence Lundblade30816f22018-11-10 13:40:22 +0700988 nReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK;
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700989 break;
990 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800991
Laurence Lundblade471a3fd2018-10-18 21:27:45 +0530992 // Alloc new buffer or expand previously allocated buffer so it can fit
Laurence Lundblade1d7eb632019-02-17 17:23:38 -0800993 // The first time throurgh FullString.ptr is NULL and this is
994 // equivalent to StringAllocator_Allocate()
995 UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator,
996 UNCONST_POINTER(FullString.ptr),
997 FullString.len + StringChunkItem.val.string.len);
998
Laurence Lundblade041ffa52018-10-07 11:43:51 +0700999 if(UsefulBuf_IsNULL(NewMem)) {
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05301000 // Allocation of memory for the string failed
Laurence Lundblade30816f22018-11-10 13:40:22 +07001001 nReturn = QCBOR_ERR_STRING_ALLOCATE;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001002 break;
1003 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001004
Laurence Lundblade2a6850e2018-10-28 20:13:44 +07001005 // Copy new string chunk at the end of string so far.
1006 FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string);
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001007 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001008
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001009 if(nReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) {
1010 // Getting the item failed, clean up the allocated memory
1011 StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr));
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001012 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001013
Laurence Lundbladed6dfe6d2020-04-09 10:21:36 -07001014Done:
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001015 return nReturn;
1016}
1017
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001018
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001019uint64_t ConvertTag(QCBORDecodeContext *me, uint16_t uTagVal) {
1020 if(uTagVal < 0xfff0) {
1021 return uTagVal;
1022 } else {
1023 // TODO constant and error check
1024 int x = uTagVal - 0xfff0;
1025 return me->auMappedTags[x];
1026 }
1027}
1028
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001029/*
Laurence Lundblade59289e52019-12-30 13:44:37 -08001030 Gets all optional tag data items preceding a data item that is not an
1031 optional tag and records them as bits in the tag map.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001032 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08001033static QCBORError
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001034GetNext_TaggedItem(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001035{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001036 // Stack usage: int/ptr: 3 -- 24
Laurence Lundblade30816f22018-11-10 13:40:22 +07001037 QCBORError nReturn;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001038
1039 uint16_t auTags[QCBOR_MAX_TAGS_PER_ITEM] = {0xffff, 0xffff, 0xffff, 0xffff};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001040
Laurence Lundblade59289e52019-12-30 13:44:37 -08001041 // Loop fetching items until the item fetched is not a tag
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001042 for(;;) {
1043 nReturn = GetNext_FullItem(me, pDecodedItem);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001044 if(nReturn) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001045 goto Done; // Error out of the loop
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001046 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001047
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001048 if(pDecodedItem->uDataType != QCBOR_TYPE_OPTTAG) {
1049 // Successful exit from loop; maybe got some tags, maybe not
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001050 memcpy(pDecodedItem->uTags, auTags, sizeof(auTags));
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001051 break;
1052 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001053
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001054 // Is there room for the tag in the tags list?
1055 size_t uTagIndex;
1056 for(uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) {
1057 if(auTags[uTagIndex] == 0xffff) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001058 break;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001059 }
1060 }
1061 if(uTagIndex >= QCBOR_MAX_TAGS_PER_ITEM) {
1062 return 99; // TODO error code
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001063 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001064
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001065 // Is the tag > 16 bits?
1066 if(pDecodedItem->val.uTagV > 0xffff) {
1067 size_t uTagMapIndex;
1068 // Is there room in the tag map?
1069 for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) {
1070 if(me->auMappedTags[uTagMapIndex] == 0xffff) {
1071 break;
1072 }
1073 if(me->auMappedTags[uTagMapIndex] == pDecodedItem->val.uTagV) {
1074 break;
1075 }
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001076 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001077 if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) {
1078 // No room for the tag
1079 return 97; // TODO error code
1080 }
1081
1082 // Cover the case where tag is new and were it is already in the map
1083 me->auMappedTags[uTagMapIndex] = pDecodedItem->val.uTagV;
1084 auTags[uTagIndex] = (uint16_t)(uTagMapIndex + 0xfff0); // TODO proper constant and cast
1085
1086 } else {
1087 auTags[uTagIndex] = (uint16_t)pDecodedItem->val.uTagV;
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001088 }
1089 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001090
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001091Done:
1092 return nReturn;
1093}
1094
1095
1096/*
Laurence Lundbladeee851742020-01-08 08:37:05 -08001097 This layer takes care of map entries. It combines the label and data
1098 items into one QCBORItem.
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001099 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08001100static inline QCBORError
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001101GetNext_MapEntry(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001102{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001103 // Stack use: int/ptr 1, QCBORItem -- 56
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001104 QCBORError nReturn = GetNext_TaggedItem(me, pDecodedItem);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001105 if(nReturn)
1106 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001107
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001108 if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001109 // Break can't be a map entry
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001110 goto Done;
1111 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001112
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001113 if(me->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) {
1114 // In a map and caller wants maps decoded, not treated as arrays
1115
1116 if(DecodeNesting_TypeIsMap(&(me->nesting))) {
1117 // If in a map and the right decoding mode, get the label
1118
Laurence Lundbladeee851742020-01-08 08:37:05 -08001119 // Save label in pDecodedItem and get the next which will
1120 // be the real data
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001121 QCBORItem LabelItem = *pDecodedItem;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001122 nReturn = GetNext_TaggedItem(me, pDecodedItem);
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001123 if(nReturn)
1124 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001125
Laurence Lundblade57dd1442018-10-15 20:26:28 +05301126 pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001127
1128 if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) {
1129 // strings are always good labels
1130 pDecodedItem->label.string = LabelItem.val.string;
1131 pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING;
1132 } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == me->uDecodeMode) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08001133 // It's not a string and we only want strings
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001134 nReturn = QCBOR_ERR_MAP_LABEL_TYPE;
1135 goto Done;
1136 } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) {
1137 pDecodedItem->label.int64 = LabelItem.val.int64;
1138 pDecodedItem->uLabelType = QCBOR_TYPE_INT64;
1139 } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) {
1140 pDecodedItem->label.uint64 = LabelItem.val.uint64;
1141 pDecodedItem->uLabelType = QCBOR_TYPE_UINT64;
1142 } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) {
1143 pDecodedItem->label.string = LabelItem.val.string;
1144 pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc;
1145 pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING;
1146 } else {
1147 // label is not an int or a string. It is an arrray
1148 // or a float or such and this implementation doesn't handle that.
1149 // Also, tags on labels are ignored.
1150 nReturn = QCBOR_ERR_MAP_LABEL_TYPE;
1151 goto Done;
1152 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001153 }
1154 } else {
1155 if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001156 if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) {
1157 nReturn = QCBOR_ERR_ARRAY_TOO_LONG;
1158 goto Done;
1159 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001160 // Decoding a map as an array
1161 pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001162 // Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2
1163 // Cast is needed because of integer promotion
1164 pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2);
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001165 }
1166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001167
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001168Done:
1169 return nReturn;
1170}
1171
1172
1173/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001174 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundbladebb87be22020-04-09 19:15:32 -07001175 TODO: correct this comment
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001176 */
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001177static QCBORError
1178QCBORDecode_GetNextMapOrArray(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001179{
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001180 // Stack ptr/int: 2, QCBORItem : 64
1181
Laurence Lundblade30816f22018-11-10 13:40:22 +07001182 QCBORError nReturn;
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 Lundbladebb1062e2019-08-12 23:28:54 -07001209 nReturn = QCBOR_ERR_NO_MORE_ITEMS;
1210 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 Lundblade3f9ef042020-04-14 13:15:51 -07001231 // This is to handle map and array mode
Laurence Lundblade937ea812020-05-08 11:38:23 -07001232 if(DecodeNesting_AtEnd(&(me->nesting))) {
Laurence Lundblade3f9ef042020-04-14 13:15:51 -07001233 nReturn = QCBOR_ERR_NO_MORE_ITEMS;
1234 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 Lundblade830fbf92020-05-31 17:22:33 -07001238 nReturn = GetNext_MapEntry(me, pDecodedItem);
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001239 if(nReturn) {
1240 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 Lundblade6de37062018-10-15 12:22:42 +05301246 nReturn = QCBOR_ERR_BAD_BREAK;
1247 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 Lundblade24d509a2020-06-06 18:43:15 -07001258 nReturn = DecodeNesting_Descend2(&(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 Lundblade9e3651c2018-10-10 11:49:55 +08001263 } else {
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001264 /* === Figure out if item got closed out maps or arrays === */
1265
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001266 // Decrement the count of items in the enclosing map/array
1267 // If the count in the enclosing map/array goes to zero, that
Laurence Lundblade6de37062018-10-15 12:22:42 +05301268 // triggers a decrement in the map/array above that and
1269 // an ascend in nesting level.
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001270 while(1) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07001271 DecodeNesting_DecrementCount(&(me->nesting));
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001272 if(nReturn) {
1273 goto Done;
1274 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001275
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001276 // For indefinite length maps/arrays, looking at any and
1277 // all breaks that might terminate them. The equivalent
1278 // for definite length maps/arrays happens in
1279 // DecodeNesting_DecrementCount().
1280 if(!DecodeNesting_IsAtTop(&(me->nesting)) && DecodeNesting_IsIndefiniteLength(&(me->nesting))) {
1281 while(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) {
1282 // Peek forward one item to see if it is a break.
1283 QCBORItem Peek;
1284 size_t uPeek = UsefulInputBuf_Tell(&(me->InBuf));
1285 nReturn = GetNext_Item(&(me->InBuf), &Peek, NULL);
1286 if(nReturn) {
1287 goto Done;
1288 }
1289 if(Peek.uDataType != QCBOR_TYPE_BREAK) {
1290 // It is not a break, rewind so it can be processed normally.
1291 UsefulInputBuf_Seek(&(me->InBuf), uPeek);
1292 break;
1293 }
1294 // It is a break. Ascend one nesting level.
1295 // The break is consumed.
1296 nReturn = DecodeNesting_BreakAscend(&(me->nesting));
1297 if(nReturn) {
1298 // break occured outside of an indefinite length array/map
1299 goto Done;
1300 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05301301 }
1302 }
1303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001304
Laurence Lundblade24d509a2020-06-06 18:43:15 -07001305
1306 /* === Tell the caller the nest level of the next item === */
1307
Laurence Lundblade6de37062018-10-15 12:22:42 +05301308 // Tell the caller what level is next. This tells them what maps/arrays
1309 // were closed out and makes it possible for them to reconstruct
1310 // the tree with just the information returned by GetNext
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07001311 // TODO: pull this into DecodeNesting_GetLevel
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001312 if(me->nesting.pCurrent->uMapMode && me->nesting.pCurrent->uCount == 0) {
1313 // At end of a map / array in map mode, so next nest is 0 to
1314 // indicate this end.
1315 pDecodedItem->uNextNestLevel = 0;
1316 } else {
1317 pDecodedItem->uNextNestLevel = DecodeNesting_GetLevel(&(me->nesting));
1318 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001319
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001320Done:
Laurence Lundbladee9482dd2019-10-11 12:58:46 -07001321 if(nReturn != QCBOR_SUCCESS) {
1322 // Make sure uDataType and uLabelType are QCBOR_TYPE_NONE
1323 memset(pDecodedItem, 0, sizeof(QCBORItem));
1324 }
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001325 return nReturn;
1326}
1327
1328
Laurence Lundblade59289e52019-12-30 13:44:37 -08001329/*
1330 Mostly just assign the right data type for the date string.
1331 */
1332inline static QCBORError DecodeDateString(QCBORItem *pDecodedItem)
1333{
1334 // Stack Use: UsefulBuf 1 16
1335 if(pDecodedItem->uDataType != QCBOR_TYPE_TEXT_STRING) {
1336 return QCBOR_ERR_BAD_OPT_TAG;
1337 }
1338
1339 const UsefulBufC Temp = pDecodedItem->val.string;
1340 pDecodedItem->val.dateString = Temp;
1341 pDecodedItem->uDataType = QCBOR_TYPE_DATE_STRING;
1342 return QCBOR_SUCCESS;
1343}
1344
1345
1346/*
1347 Mostly just assign the right data type for the bignum.
1348 */
1349inline static QCBORError DecodeBigNum(QCBORItem *pDecodedItem)
1350{
1351 // Stack Use: UsefulBuf 1 -- 16
1352 if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) {
1353 return QCBOR_ERR_BAD_OPT_TAG;
1354 }
1355 const UsefulBufC Temp = pDecodedItem->val.string;
1356 pDecodedItem->val.bigNum = Temp;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001357 const bool bIsPosBigNum = (bool)(pDecodedItem->uTags[0] == CBOR_TAG_POS_BIGNUM);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001358 pDecodedItem->uDataType = (uint8_t)(bIsPosBigNum ? QCBOR_TYPE_POSBIGNUM
1359 : QCBOR_TYPE_NEGBIGNUM);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001360 return QCBOR_SUCCESS;
1361}
1362
1363
1364/*
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001365 Mostly just assign the right data type for the bignum.
1366 */
1367inline static QCBORError DecodeUUID(QCBORItem *pDecodedItem)
1368{
1369 if(pDecodedItem->uDataType != QCBOR_TYPE_BYTE_STRING) {
1370 return QCBOR_ERR_BAD_OPT_TAG;
1371 }
1372 pDecodedItem->uDataType = QCBOR_TYPE_UUID;
1373 return QCBOR_SUCCESS;
1374}
1375
1376
1377/*
Laurence Lundbladeee851742020-01-08 08:37:05 -08001378 The epoch formatted date. Turns lots of different forms of encoding
1379 date into uniform one
Laurence Lundblade59289e52019-12-30 13:44:37 -08001380 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001381static QCBORError DecodeDateEpoch(QCBORItem *pDecodedItem)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001382{
1383 // Stack usage: 1
1384 QCBORError nReturn = QCBOR_SUCCESS;
1385
1386 pDecodedItem->val.epochDate.fSecondsFraction = 0;
1387
1388 switch (pDecodedItem->uDataType) {
1389
1390 case QCBOR_TYPE_INT64:
1391 pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64;
1392 break;
1393
1394 case QCBOR_TYPE_UINT64:
1395 if(pDecodedItem->val.uint64 > INT64_MAX) {
1396 nReturn = QCBOR_ERR_DATE_OVERFLOW;
1397 goto Done;
1398 }
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001399 pDecodedItem->val.epochDate.nSeconds = (int64_t)pDecodedItem->val.uint64;
Laurence Lundblade59289e52019-12-30 13:44:37 -08001400 break;
1401
1402 case QCBOR_TYPE_DOUBLE:
1403 {
1404 // This comparison needs to be done as a float before
1405 // conversion to an int64_t to be able to detect doubles
1406 // that are too large to fit into an int64_t. A double
1407 // has 52 bits of preceision. An int64_t has 63. Casting
1408 // INT64_MAX to a double actually causes a round up which
1409 // is bad and wrong for the comparison because it will
1410 // allow conversion of doubles that can't fit into a
1411 // uint64_t. To remedy this INT64_MAX - 0x7ff is used as
1412 // the cutoff point as if that rounds up in conversion to
1413 // double it will still be less than INT64_MAX. 0x7ff is
1414 // picked because it has 11 bits set.
1415 //
1416 // INT64_MAX seconds is on the order of 10 billion years,
1417 // and the earth is less than 5 billion years old, so for
1418 // most uses this conversion error won't occur even though
1419 // doubles can go much larger.
1420 //
1421 // Without the 0x7ff there is a ~30 minute range of time
1422 // values 10 billion years in the past and in the future
1423 // where this this code would go wrong.
1424 const double d = pDecodedItem->val.dfnum;
1425 if(d > (double)(INT64_MAX - 0x7ff)) {
1426 nReturn = QCBOR_ERR_DATE_OVERFLOW;
1427 goto Done;
1428 }
1429 pDecodedItem->val.epochDate.nSeconds = (int64_t)d;
1430 pDecodedItem->val.epochDate.fSecondsFraction = d - (double)pDecodedItem->val.epochDate.nSeconds;
1431 }
1432 break;
1433
1434 default:
1435 nReturn = QCBOR_ERR_BAD_OPT_TAG;
1436 goto Done;
1437 }
1438 pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH;
1439
1440Done:
1441 return nReturn;
1442}
1443
1444
1445#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
1446/*
1447 Decode decimal fractions and big floats.
1448
1449 When called pDecodedItem must be the array that is tagged as a big
1450 float or decimal fraction, the array that has the two members, the
1451 exponent and mantissa.
1452
1453 This will fetch and decode the exponent and mantissa and put the
1454 result back into pDecodedItem.
1455 */
1456inline static QCBORError
1457QCBORDecode_MantissaAndExponent(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
1458{
1459 QCBORError nReturn;
1460
1461 // --- Make sure it is an array; track nesting level of members ---
1462 if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) {
1463 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1464 goto Done;
1465 }
1466
1467 // A check for pDecodedItem->val.uCount == 2 would work for
Laurence Lundbladeee851742020-01-08 08:37:05 -08001468 // definite length arrays, but not for indefnite. Instead remember
1469 // the nesting level the two integers must be at, which is one
1470 // deeper than that of the array.
Laurence Lundblade59289e52019-12-30 13:44:37 -08001471 const int nNestLevel = pDecodedItem->uNestingLevel + 1;
1472
1473 // --- Is it a decimal fraction or a bigfloat? ---
1474 const bool bIsTaggedDecimalFraction = QCBORDecode_IsTagged(me, pDecodedItem, CBOR_TAG_DECIMAL_FRACTION);
1475 pDecodedItem->uDataType = bIsTaggedDecimalFraction ? QCBOR_TYPE_DECIMAL_FRACTION : QCBOR_TYPE_BIGFLOAT;
1476
1477 // --- Get the exponent ---
1478 QCBORItem exponentItem;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001479 nReturn = QCBORDecode_GetNextMapOrArray(me, &exponentItem);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001480 if(nReturn != QCBOR_SUCCESS) {
1481 goto Done;
1482 }
1483 if(exponentItem.uNestingLevel != nNestLevel) {
1484 // Array is empty or a map/array encountered when expecting an int
1485 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1486 goto Done;
1487 }
1488 if(exponentItem.uDataType == QCBOR_TYPE_INT64) {
1489 // Data arriving as an unsigned int < INT64_MAX has been converted
1490 // to QCBOR_TYPE_INT64 and thus handled here. This is also means
1491 // that the only data arriving here of type QCBOR_TYPE_UINT64 data
1492 // will be too large for this to handle and thus an error that will
1493 // get handled in the next else.
1494 pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64;
1495 } else {
1496 // Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX
1497 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1498 goto Done;
1499 }
1500
1501 // --- Get the mantissa ---
1502 QCBORItem mantissaItem;
1503 nReturn = QCBORDecode_GetNextWithTags(me, &mantissaItem, NULL);
1504 if(nReturn != QCBOR_SUCCESS) {
1505 goto Done;
1506 }
1507 if(mantissaItem.uNestingLevel != nNestLevel) {
1508 // Mantissa missing or map/array encountered when expecting number
1509 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1510 goto Done;
1511 }
1512 if(mantissaItem.uDataType == QCBOR_TYPE_INT64) {
1513 // Data arriving as an unsigned int < INT64_MAX has been converted
1514 // to QCBOR_TYPE_INT64 and thus handled here. This is also means
1515 // that the only data arriving here of type QCBOR_TYPE_UINT64 data
1516 // will be too large for this to handle and thus an error that
1517 // will get handled in an else below.
1518 pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64;
1519 } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) {
1520 // Got a good big num mantissa
1521 pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum;
1522 // Depends on numbering of QCBOR_TYPE_XXX
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001523 pDecodedItem->uDataType = (uint8_t)(pDecodedItem->uDataType +
1524 mantissaItem.uDataType - QCBOR_TYPE_POSBIGNUM +
1525 1);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001526 } else {
1527 // Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX
1528 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1529 goto Done;
1530 }
1531
1532 // --- Check that array only has the two numbers ---
1533 if(mantissaItem.uNextNestLevel == nNestLevel) {
1534 // Extra items in the decimal fraction / big num
1535 nReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA;
1536 goto Done;
1537 }
1538
1539Done:
1540
1541 return nReturn;
1542}
1543#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
1544
1545
1546/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001547 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade59289e52019-12-30 13:44:37 -08001548 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08001549QCBORError
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001550QCBORDecode_GetNext(QCBORDecodeContext *me, QCBORItem *pDecodedItem)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001551{
1552 QCBORError nReturn;
1553
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001554 nReturn = QCBORDecode_GetNextMapOrArray(me, pDecodedItem);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001555 if(nReturn != QCBOR_SUCCESS) {
1556 goto Done;
1557 }
1558
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001559 for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM; i++) {
1560 switch(pDecodedItem->uTags[i] ) {
1561 case 0xffff:
1562 // The end of the tag list or no tags
1563 // Successful exit from the loop.
1564 goto Done;
Laurence Lundblade59289e52019-12-30 13:44:37 -08001565
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001566 case CBOR_TAG_DATE_STRING:
Laurence Lundblade59289e52019-12-30 13:44:37 -08001567 nReturn = DecodeDateString(pDecodedItem);
1568 break;
1569
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001570 case CBOR_TAG_DATE_EPOCH:
Laurence Lundblade59289e52019-12-30 13:44:37 -08001571 nReturn = DecodeDateEpoch(pDecodedItem);
1572 break;
1573
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001574 case CBOR_TAG_POS_BIGNUM:
1575 case CBOR_TAG_NEG_BIGNUM:
Laurence Lundblade59289e52019-12-30 13:44:37 -08001576 nReturn = DecodeBigNum(pDecodedItem);
1577 break;
1578
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001579 #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
1580 case CBOR_TAG_DECIMAL_FRACTION:
1581 case CBOR_TAG_BIGFLOAT:
Laurence Lundblade59289e52019-12-30 13:44:37 -08001582 // For aggregate tagged types, what goes into pTags is only collected
1583 // from the surrounding data item, not the contents, so pTags is not
1584 // passed on here.
1585
1586 nReturn = QCBORDecode_MantissaAndExponent(me, pDecodedItem);
1587 break;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001588 #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade59289e52019-12-30 13:44:37 -08001589
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001590 case CBOR_TAG_BIN_UUID:
1591 nReturn = DecodeUUID(pDecodedItem);
1592
1593 default:
1594 // A tag that is not understood
1595 // A successful exit from the loop
1596 goto Done;
1597
1598 }
1599 if(nReturn != QCBOR_SUCCESS) {
1600 goto Done;
1601 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08001602 }
1603
1604Done:
1605 if(nReturn != QCBOR_SUCCESS) {
1606 pDecodedItem->uDataType = QCBOR_TYPE_NONE;
1607 pDecodedItem->uLabelType = QCBOR_TYPE_NONE;
1608 }
1609 return nReturn;
1610}
1611
1612
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001613QCBORError QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem)
1614{
1615 const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf));
1616
1617 QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem);
1618
1619 UsefulInputBuf_Seek(&(pMe->InBuf), uOffset);
1620
1621 return uErr;
1622}
1623
1624
Laurence Lundblade59289e52019-12-30 13:44:37 -08001625/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001626 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade59289e52019-12-30 13:44:37 -08001627 */
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001628QCBORError
1629QCBORDecode_GetNextWithTags(QCBORDecodeContext *me,
1630 QCBORItem *pDecodedItem,
1631 QCBORTagListOut *pTags)
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001632{
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001633 QCBORError nReturn;
1634
1635 nReturn = QCBORDecode_GetNext(me, pDecodedItem);
1636 if(nReturn != QCBOR_SUCCESS) {
1637 return nReturn;
1638 }
1639
1640 if(pTags != NULL) {
1641 pTags->uNumUsed = 0;
1642 for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM; i++) {
1643 if(pDecodedItem->uTags[i] == 0xffff) {
1644 break;
1645 }
1646 if(pTags->uNumUsed >= pTags->uNumAllocated) {
1647 return QCBOR_ERR_TOO_MANY_TAGS;
1648 }
1649 pTags->puTags[pTags->uNumUsed] = ConvertTag(me, pDecodedItem->uTags[i]);
1650 pTags->uNumUsed++;
1651 }
1652 }
1653
1654 return QCBOR_SUCCESS;
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001655}
1656
1657
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001658/*
Laurence Lundblade6de37062018-10-15 12:22:42 +05301659 Decoding items is done in 5 layered functions, one calling the
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301660 next one down. If a layer has no work to do for a particular item
1661 it returns quickly.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001662
Laurence Lundblade59289e52019-12-30 13:44:37 -08001663 - QCBORDecode_GetNext, GetNextWithTags -- The top layer processes
1664 tagged data items, turning them into the local C representation.
1665 For the most simple it is just associating a QCBOR_TYPE with the data. For
1666 the complex ones that an aggregate of data items, there is some further
1667 decoding and a little bit of recursion.
1668
1669 - QCBORDecode_GetNextMapOrArray - This manages the beginnings and
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301670 ends of maps and arrays. It tracks descending into and ascending
Laurence Lundblade6de37062018-10-15 12:22:42 +05301671 out of maps/arrays. It processes all breaks that terminate
Laurence Lundbladebb87be22020-04-09 19:15:32 -07001672 indefinite length maps and arrays.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001673
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301674 - GetNext_MapEntry -- This handles the combining of two
1675 items, the label and the data, that make up a map entry.
1676 It only does work on maps. It combines the label and data
1677 items into one labeled item.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001678
Laurence Lundblade59289e52019-12-30 13:44:37 -08001679 - GetNext_TaggedItem -- This decodes type 6 tagging. It turns the
1680 tags into bit flags associated with the data item. No actual decoding
1681 of the contents of the tagged item is performed here.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001682
Laurence Lundblade59289e52019-12-30 13:44:37 -08001683 - GetNext_FullItem -- This assembles the sub-items that make up
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301684 an indefinte length string into one string item. It uses the
Laurence Lundblade6de37062018-10-15 12:22:42 +05301685 string allocater to create contiguous space for the item. It
1686 processes all breaks that are part of indefinite length strings.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001687
Laurence Lundblade59289e52019-12-30 13:44:37 -08001688 - GetNext_Item -- This decodes the atomic data items in CBOR. Each
1689 atomic data item has a "major type", an integer "argument" and optionally
1690 some content. For text and byte strings, the content is the bytes
1691 that make up the string. These are the smallest data items that are
1692 considered to be well-formed. The content may also be other data items in
1693 the case of aggregate types. They are not handled in this layer.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001694
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001695 Roughly this takes 300 bytes of stack for vars. Need to
1696 evaluate this more carefully and correctly.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001697
Laurence Lundblade0fb2f642018-10-11 19:33:35 +05301698 */
1699
1700
1701/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001702 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001703 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08001704int QCBORDecode_IsTagged(QCBORDecodeContext *me,
1705 const QCBORItem *pItem,
1706 uint64_t uTag)
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001707{
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001708 for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM; i++ ) {
1709 if(pItem->uTags[i] == 0xffff) {
1710 break;
1711 }
1712 if(ConvertTag(me, pItem->uTags[i]) == uTag) {
1713 return 1;
1714 }
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001715 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001716
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001717 return 0;
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001718}
1719
1720
1721/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001722 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001723 */
Laurence Lundblade30816f22018-11-10 13:40:22 +07001724QCBORError QCBORDecode_Finish(QCBORDecodeContext *me)
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001725{
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001726 QCBORError nReturn = QCBOR_SUCCESS;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001727
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001728 // Error out if all the maps/arrays are not closed out
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001729 if(!DecodeNesting_IsAtTop(&(me->nesting))) {
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001730 nReturn = QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN;
1731 goto Done;
1732 }
1733
1734 // Error out if not all the bytes are consumed
1735 if(UsefulInputBuf_BytesUnconsumed(&(me->InBuf))) {
1736 nReturn = QCBOR_ERR_EXTRA_BYTES;
1737 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001738
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001739Done:
Laurence Lundblade6de37062018-10-15 12:22:42 +05301740 // Call the destructor for the string allocator if there is one.
Laurence Lundblade20b533d2018-10-08 20:44:53 +08001741 // Always called, even if there are errors; always have to clean up
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001742 StringAllocator_Destruct(&(me->StringAllocator));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001743
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001744 return nReturn;
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001745}
1746
1747
1748
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001749/*
1750
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001751Decoder errors handled in this file
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001752
Laurence Lundbladeee851742020-01-08 08:37:05 -08001753 - Hit end of input before it was expected while decoding type and
1754 number QCBOR_ERR_HIT_END
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001755
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001756 - negative integer that is too large for C QCBOR_ERR_INT_OVERFLOW
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001757
Laurence Lundbladeee851742020-01-08 08:37:05 -08001758 - Hit end of input while decoding a text or byte string
1759 QCBOR_ERR_HIT_END
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001760
Laurence Lundbladeee851742020-01-08 08:37:05 -08001761 - Encountered conflicting tags -- e.g., an item is tagged both a date
1762 string and an epoch date QCBOR_ERR_UNSUPPORTED
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001763
Laurence Lundbladeee851742020-01-08 08:37:05 -08001764 - Encontered an array or mapp that has too many items
1765 QCBOR_ERR_ARRAY_TOO_LONG
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001766
Laurence Lundbladeee851742020-01-08 08:37:05 -08001767 - Encountered array/map nesting that is too deep
1768 QCBOR_ERR_ARRAY_NESTING_TOO_DEEP
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001769
Laurence Lundbladeee851742020-01-08 08:37:05 -08001770 - An epoch date > INT64_MAX or < INT64_MIN was encountered
1771 QCBOR_ERR_DATE_OVERFLOW
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001772
Laurence Lundbladeee851742020-01-08 08:37:05 -08001773 - The type of a map label is not a string or int
1774 QCBOR_ERR_MAP_LABEL_TYPE
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001775
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001776 - Hit end with arrays or maps still open -- QCBOR_ERR_EXTRA_BYTES
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001777
Laurence Lundbladeb69cad72018-09-13 11:09:01 -07001778 */
1779
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001780
1781
Laurence Lundbladef6531662018-12-04 10:42:22 +09001782
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001783/* ===========================================================================
1784 MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001785
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001786 This implements a simple sting allocator for indefinite length
1787 strings that can be enabled by calling QCBORDecode_SetMemPool(). It
1788 implements the function type QCBORStringAllocate and allows easy
1789 use of it.
Laurence Lundbladef6531662018-12-04 10:42:22 +09001790
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001791 This particular allocator is built-in for convenience. The caller
1792 can implement their own. All of this following code will get
1793 dead-stripped if QCBORDecode_SetMemPool() is not called.
1794
1795 This is a very primitive memory allocator. It does not track
1796 individual allocations, only a high-water mark. A free or
1797 reallocation must be of the last chunk allocated.
1798
1799 The size of the pool and offset to free memory are packed into the
1800 first 8 bytes of the memory pool so we don't have to keep them in
1801 the decode context. Since the address of the pool may not be
1802 aligned, they have to be packed and unpacked as if they were
1803 serialized data of the wire or such.
1804
1805 The sizes packed in are uint32_t to be the same on all CPU types
1806 and simplify the code.
Laurence Lundbladeee851742020-01-08 08:37:05 -08001807 ========================================================================== */
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001808
1809
Laurence Lundbladeee851742020-01-08 08:37:05 -08001810static inline int
1811MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001812{
1813 // Use of UsefulInputBuf is overkill, but it is convenient.
1814 UsefulInputBuf UIB;
1815
Laurence Lundbladeee851742020-01-08 08:37:05 -08001816 // Just assume the size here. It was checked during SetUp so
1817 // the assumption is safe.
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001818 UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem, QCBOR_DECODE_MIN_MEM_POOL_SIZE});
1819 *puPoolSize = UsefulInputBuf_GetUint32(&UIB);
1820 *puFreeOffset = UsefulInputBuf_GetUint32(&UIB);
1821 return UsefulInputBuf_GetError(&UIB);
1822}
1823
1824
Laurence Lundbladeee851742020-01-08 08:37:05 -08001825static inline int
1826MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001827{
1828 // Use of UsefulOutBuf is overkill, but convenient. The
1829 // length check performed here is useful.
1830 UsefulOutBuf UOB;
1831
1832 UsefulOutBuf_Init(&UOB, Pool);
1833 UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool
1834 UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position
1835 return UsefulOutBuf_GetError(&UOB);
1836}
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001837
1838
1839/*
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001840 Internal function for an allocation, reallocation free and destuct.
1841
1842 Having only one function rather than one each per mode saves space in
1843 QCBORDecodeContext.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001844
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001845 Code Reviewers: THIS FUNCTION DOES POINTER MATH
1846 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08001847static UsefulBuf
1848MemPool_Function(void *pPool, void *pMem, size_t uNewSize)
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001849{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001850 UsefulBuf ReturnValue = NULLUsefulBuf;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001851
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001852 uint32_t uPoolSize;
1853 uint32_t uFreeOffset;
1854
1855 if(uNewSize > UINT32_MAX) {
1856 // This allocator is only good up to 4GB. This check should
1857 // optimize out if sizeof(size_t) == sizeof(uint32_t)
1858 goto Done;
1859 }
1860 const uint32_t uNewSize32 = (uint32_t)uNewSize;
1861
1862 if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) {
1863 goto Done;
1864 }
1865
1866 if(uNewSize) {
1867 if(pMem) {
1868 // REALLOCATION MODE
1869 // Calculate pointer to the end of the memory pool. It is
1870 // assumed that pPool + uPoolSize won't wrap around by
1871 // assuming the caller won't pass a pool buffer in that is
1872 // not in legitimate memory space.
1873 const void *pPoolEnd = (uint8_t *)pPool + uPoolSize;
1874
1875 // Check that the pointer for reallocation is in the range of the
1876 // pool. This also makes sure that pointer math further down
1877 // doesn't wrap under or over.
1878 if(pMem >= pPool && pMem < pPoolEnd) {
1879 // Offset to start of chunk for reallocation. This won't
1880 // wrap under because of check that pMem >= pPool. Cast
1881 // is safe because the pool is always less than UINT32_MAX
1882 // because of check in QCBORDecode_SetMemPool().
1883 const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool);
1884
1885 // Check to see if the allocation will fit. uPoolSize -
1886 // uMemOffset will not wrap under because of check that
1887 // pMem is in the range of the uPoolSize by check above.
1888 if(uNewSize <= uPoolSize - uMemOffset) {
1889 ReturnValue.ptr = pMem;
1890 ReturnValue.len = uNewSize;
1891
1892 // Addition won't wrap around over because uNewSize was
1893 // checked to be sure it is less than the pool size.
1894 uFreeOffset = uMemOffset + uNewSize32;
1895 }
1896 }
1897 } else {
1898 // ALLOCATION MODE
1899 // uPoolSize - uFreeOffset will not underflow because this
1900 // pool implementation makes sure uFreeOffset is always
1901 // smaller than uPoolSize through this check here and
1902 // reallocation case.
1903 if(uNewSize <= uPoolSize - uFreeOffset) {
1904 ReturnValue.len = uNewSize;
1905 ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset;
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001906 uFreeOffset += (uint32_t)uNewSize;
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001907 }
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001908 }
1909 } else {
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001910 if(pMem) {
1911 // FREE MODE
1912 // Cast is safe because of limit on pool size in
1913 // QCBORDecode_SetMemPool()
1914 uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool);
1915 } else {
1916 // DESTRUCT MODE
1917 // Nothing to do for this allocator
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001918 }
1919 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001920
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001921 UsefulBuf Pool = {pPool, uPoolSize};
1922 MemPool_Pack(Pool, uFreeOffset);
1923
1924Done:
1925 return ReturnValue;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001926}
1927
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001928
Laurence Lundbladef6531662018-12-04 10:42:22 +09001929/*
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001930 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundbladef6531662018-12-04 10:42:22 +09001931 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08001932QCBORError QCBORDecode_SetMemPool(QCBORDecodeContext *pMe,
1933 UsefulBuf Pool,
1934 bool bAllStrings)
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001935{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001936 // The pool size and free mem offset are packed into the beginning
1937 // of the pool memory. This compile time check make sure the
1938 // constant in the header is correct. This check should optimize
1939 // down to nothing.
1940 if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) {
Laurence Lundblade30816f22018-11-10 13:40:22 +07001941 return QCBOR_ERR_BUFFER_TOO_SMALL;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001942 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001943
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001944 // The pool size and free offset packed in to the beginning of pool
1945 // memory are only 32-bits. This check will optimize out on 32-bit
1946 // machines.
1947 if(Pool.len > UINT32_MAX) {
1948 return QCBOR_ERR_BUFFER_TOO_LARGE;
1949 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001950
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001951 // This checks that the pool buffer given is big enough.
1952 if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) {
1953 return QCBOR_ERR_BUFFER_TOO_SMALL;
1954 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001955
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08001956 pMe->StringAllocator.pfAllocator = MemPool_Function;
1957 pMe->StringAllocator.pAllocateCxt = Pool.ptr;
1958 pMe->bStringAllocateAll = bAllStrings;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001959
Laurence Lundblade30816f22018-11-10 13:40:22 +07001960 return QCBOR_SUCCESS;
Laurence Lundblade041ffa52018-10-07 11:43:51 +07001961}
Laurence Lundbladebb87be22020-04-09 19:15:32 -07001962
Laurence Lundblade1341c592020-04-11 14:19:05 -07001963#include <stdio.h>
1964void printdecode(QCBORDecodeContext *pMe, const char *szName)
1965{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001966 printf("---%s--%d--%d--\nLevel Count Type Offset SaveCount MapMode\n",
1967 szName,
1968 (uint32_t)pMe->InBuf.cursor,
1969 (uint32_t)pMe->InBuf.UB.len);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07001970 for(int i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001971 if(&(pMe->nesting.pMapsAndArrays[i]) > pMe->nesting.pCurrent) {
1972 break;
1973 }
Laurence Lundblade64b607e2020-05-13 13:05:57 -07001974 printf("%2s %2d %5d %s %6u %2d %d\n",
1975 pMe->nesting.pCurrentMap == &(pMe->nesting.pMapsAndArrays[i]) ? "->": " ",
Laurence Lundblade1341c592020-04-11 14:19:05 -07001976 i,
1977 pMe->nesting.pMapsAndArrays[i].uCount,
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001978 pMe->nesting.pMapsAndArrays[i].uMajorType == QCBOR_TYPE_MAP ? " map" :
1979 (pMe->nesting.pMapsAndArrays[i].uMajorType == QCBOR_TYPE_ARRAY ? "array" :
1980 (pMe->nesting.pMapsAndArrays[i].uMajorType == QCBOR_TYPE_NONE ? " none" : "?????")),
Laurence Lundblade1341c592020-04-11 14:19:05 -07001981 pMe->nesting.pMapsAndArrays[i].uOffset,
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001982 pMe->nesting.pMapsAndArrays[i].uSaveCount,
1983 pMe->nesting.pMapsAndArrays[i].uMapMode
Laurence Lundblade1341c592020-04-11 14:19:05 -07001984 );
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001985
Laurence Lundblade1341c592020-04-11 14:19:05 -07001986 }
Laurence Lundblade64b607e2020-05-13 13:05:57 -07001987 printf("\n");
Laurence Lundblade1341c592020-04-11 14:19:05 -07001988}
Laurence Lundbladebb87be22020-04-09 19:15:32 -07001989
1990
1991/*
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001992 *
Laurence Lundbladebb87be22020-04-09 19:15:32 -07001993 */
Laurence Lundblade9c905e82020-04-25 11:31:38 -07001994static inline QCBORError
Laurence Lundbladebb87be22020-04-09 19:15:32 -07001995ConsumeItem(QCBORDecodeContext *pMe,
1996 const QCBORItem *pItemToConsume,
1997 uint_fast8_t *puNextNestLevel)
1998{
Laurence Lundblade1341c592020-04-11 14:19:05 -07001999 QCBORError nReturn;
2000 QCBORItem Item;
2001
2002 printdecode(pMe, "ConsumeItem");
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002003
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002004 if(IsMapOrArray(pItemToConsume->uDataType)) {
Laurence Lundblade1341c592020-04-11 14:19:05 -07002005 /* There is only real work to do for maps and arrays */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002006
Laurence Lundblade1341c592020-04-11 14:19:05 -07002007 /* This works for definite and indefinite length
2008 * maps and arrays by using the nesting level
2009 */
2010 do {
2011 nReturn = QCBORDecode_GetNext(pMe, &Item);
2012 if(nReturn != QCBOR_SUCCESS) {
2013 goto Done;
2014 }
2015 } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel);
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002016
Laurence Lundblade1341c592020-04-11 14:19:05 -07002017 if(puNextNestLevel != NULL) {
2018 *puNextNestLevel = Item.uNextNestLevel;
2019 }
2020 nReturn = QCBOR_SUCCESS;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002021
Laurence Lundblade1341c592020-04-11 14:19:05 -07002022 } else {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002023 /* item_to_consume is not a map or array */
Laurence Lundblade1341c592020-04-11 14:19:05 -07002024 if(puNextNestLevel != NULL) {
2025 /* Just pass the nesting level through */
2026 *puNextNestLevel = pItemToConsume->uNextNestLevel;
2027 }
2028 nReturn = QCBOR_SUCCESS;
2029 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002030
2031Done:
2032 return nReturn;
2033}
2034
2035
Laurence Lundblade1341c592020-04-11 14:19:05 -07002036/* Return true if the labels in Item1 and Item2 are the same.
2037 Works only for integer and string labels. Returns false
2038 for any other type. */
2039static inline bool
2040MatchLabel(QCBORItem Item1, QCBORItem Item2)
2041{
2042 if(Item1.uLabelType == QCBOR_TYPE_INT64) {
2043 if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) {
2044 return true;
2045 }
2046 } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002047 if(Item2.uLabelType == QCBOR_TYPE_TEXT_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) {
Laurence Lundblade1341c592020-04-11 14:19:05 -07002048 return true;
2049 }
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002050 } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) {
Laurence Lundbladefb492ea2020-05-02 11:14:07 -07002051 if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) {
2052 return true;
2053 }
2054 } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) {
2055 if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) {
2056 return true;
2057 }
2058 }
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002059
Laurence Lundblade1341c592020-04-11 14:19:05 -07002060 /* Other label types are never matched */
2061 return false;
2062}
2063
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002064static inline bool
2065MatchType(QCBORItem Item1, QCBORItem Item2)
2066{
2067 if(Item1.uDataType == Item2.uDataType) {
2068 return true;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002069 } else if(Item1.uDataType == QCBOR_TYPE_ANY) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002070 return true;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002071 } else if(Item2.uDataType == QCBOR_TYPE_ANY) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002072 return true;
2073 }
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002074 return false;
2075}
2076
2077
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002078/**
2079 \brief Search a map for a set of items
2080
2081 @param[in] pMe The decode context to search.
2082 @param[in,out] pItemArray The items to search for and the items found.
2083 @param[in] pCBContext Context for the not-found item call back
2084 @param[in] pfCallback Function to call on items not matched in pItemArray
2085
2086 @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map
2087
2088 @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.
2089
2090 @retval QCBOR_ERR_UNEXPECTED_TYPE The label was matched, but not the type.
2091
2092 @retval Also errors returned by QCBORDecode_GetNext().
2093
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002094 On input pItemArray contains a list of labels and data types
2095 of items to be found.
2096
2097 On output the fully retrieved items are filled in with
2098 values and such. The label was matched, so it never changes.
2099
2100 If an item was not found, its data type is set to none.
2101
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002102 */
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002103static QCBORError
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002104MapSearch(QCBORDecodeContext *pMe,
2105 QCBORItem *pItemArray,
2106 size_t *puOffset,
2107 size_t *puEndOffset,
2108 void *pCBContext,
2109 QCBORItemCallback pfCallback)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002110{
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002111 QCBORError uReturn;
Laurence Lundbladefb492ea2020-05-02 11:14:07 -07002112
2113 // TODO: what if pre-order cursor is not at the same level as map? This should be OK.
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002114 if(!DecodeNesting_InMapMode(&(pMe->nesting))) {
2115 return QCBOR_ERR_NOT_ENTERED;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002116 }
2117
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002118 QCBORDecodeNesting SaveNesting;
2119 DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002120
2121 UsefulInputBuf_Seek(&(pMe->InBuf), pMe->nesting.pCurrent->uOffset);
2122
2123 /* Loop over all the items in the map. They could be
2124 * deeply nested and this should handle both definite
2125 * and indefinite length maps and arrays, so this
2126 * adds some complexity. */
Laurence Lundblade64b607e2020-05-13 13:05:57 -07002127 const uint8_t uMapNestLevel = DecodeNesting_GetMapModeLevel(&(pMe->nesting));
Laurence Lundblade1341c592020-04-11 14:19:05 -07002128
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002129 uint_fast8_t uNextNestLevel;
2130
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002131 uint64_t uFoundItemBitMap = 0;
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002132
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002133 /* Iterate over items in the map / array */
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002134 do {
2135 /* Remember offset because sometims we have to return it */
Laurence Lundblade1341c592020-04-11 14:19:05 -07002136 const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf));
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002137
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002138 /* Get the item */
2139 QCBORItem Item;
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002140 uReturn = QCBORDecode_GetNext(pMe, &Item);
2141 if(uReturn != QCBOR_SUCCESS) {
Laurence Lundblade1341c592020-04-11 14:19:05 -07002142 /* Got non-well-formed CBOR */
2143 goto Done;
2144 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002145
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002146 /* See if item has one of the labels that are of interest */
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002147 int nIndex;
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002148 QCBORItem *pIterator;
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002149 for(pIterator = pItemArray, nIndex = 0; pIterator->uLabelType != 0; pIterator++, nIndex++) {
Laurence Lundblade1341c592020-04-11 14:19:05 -07002150 if(MatchLabel(Item, *pIterator)) {
2151 // A label match has been found
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002152 if(uFoundItemBitMap & (0x01ULL << nIndex)) {
2153 uReturn = QCBOR_ERR_DUPLICATE_LABEL;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002154 goto Done;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002155 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002156 /* Also try to match its type */
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002157 if(!MatchType(Item, *pIterator)) {
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002158 uReturn = QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002159 goto Done;
2160 }
Laurence Lundblade1341c592020-04-11 14:19:05 -07002161
2162 /* Successful match. Return the item. */
2163 *pIterator = Item;
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002164 uFoundItemBitMap |= 0x01ULL << nIndex;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002165 if(puOffset) {
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002166 *puOffset = uOffset;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002167 }
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002168 } else {
2169 /* Call the call back on unmatched labels */
2170 /* It is tempting to do duplicate detection here, but that would
2171 require dynamic memory allocation because the number of labels
2172 that might be encountered is unbounded.
2173 */
2174 if(pfCallback) {
2175 uReturn = (*pfCallback)(pCBContext, &Item);
2176 if(uReturn != QCBOR_SUCCESS) {
2177 goto Done;
2178 }
2179 }
Laurence Lundblade1341c592020-04-11 14:19:05 -07002180 }
2181 }
2182
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002183 /* Consume the item whether matched or not. This
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002184 does the work of traversing maps and array and
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002185 everything in them. In this loop only the
2186 items at the current nesting level are examined
2187 to match the labels. */
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002188 uReturn = ConsumeItem(pMe, &Item, &uNextNestLevel);
2189 if(uReturn) {
Laurence Lundblade1341c592020-04-11 14:19:05 -07002190 goto Done;
2191 }
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002192
2193 } while (uNextNestLevel >= uMapNestLevel);
2194
2195
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002196 uReturn = QCBOR_SUCCESS;
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002197
2198 const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf));
2199 // Cast OK because encoded CBOR is limited to UINT32_MAX
2200 pMe->uMapEndOffset = (uint32_t)uEndOffset;
2201 // TODO: is zero *puOffset OK?
2202 if(puEndOffset) {
2203 *puEndOffset = uEndOffset;
2204 }
2205
Laurence Lundbladefb492ea2020-05-02 11:14:07 -07002206 /* For all items not found, set the data type to QCBOR_TYPE_NONE */
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002207 int i;
2208 QCBORItem *pIterator;
2209 for(pIterator = pItemArray, i = 0; pIterator->uLabelType != 0; pIterator++, i++) {
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002210 if(!(uFoundItemBitMap & (0x01ULL << i))) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002211 pIterator->uDataType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002212 }
2213 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002214
2215Done:
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002216 DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting);
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002217
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002218 return uReturn;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002219}
2220
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002221
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002222
2223
Laurence Lundblade34691b92020-05-18 22:25:25 -07002224void QCBORDecode_ExitMapMode(QCBORDecodeContext *pMe, uint8_t uType)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002225{
Laurence Lundblade1341c592020-04-11 14:19:05 -07002226 size_t uEndOffset;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002227
Laurence Lundblade34691b92020-05-18 22:25:25 -07002228 (void)uType; // TODO: error check
2229
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002230/*
Laurence Lundblade1341c592020-04-11 14:19:05 -07002231 if(pMe->uMapEndOffset) {
2232 uEndOffset = pMe->uMapEndOffset;
2233 // It is only valid once.
2234 pMe->uMapEndOffset = 0;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002235 } else { */
Laurence Lundblade1341c592020-04-11 14:19:05 -07002236 QCBORItem Dummy;
2237
2238 Dummy.uLabelType = QCBOR_TYPE_NONE;
2239
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002240 QCBORError nReturn = MapSearch(pMe, &Dummy, NULL, &uEndOffset, NULL, NULL);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002241
2242 (void)nReturn; // TODO:
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002243// }
Laurence Lundblade1341c592020-04-11 14:19:05 -07002244
2245 printdecode(pMe, "start exit");
2246 UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset);
2247
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002248 DecodeNesting_Exit(&(pMe->nesting));
Laurence Lundblade1341c592020-04-11 14:19:05 -07002249 printdecode(pMe, "end exit");
2250
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002251}
2252
2253
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002254void QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, UsefulBufC *pBstr)
2255{
2256 QCBORItem Item;
2257 QCBORDecode_GetNext(pMe, &Item);
2258 // Need to set UIB cursor to start of bstr and UIB length to end of bstr
2259
2260}
2261
2262//void QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pCtx, int64_t uLabel, UsefulBufC *pBstr);
2263
2264//void QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pCtx, const char *szLabel, UsefulBufC *pBstr);
2265
2266void QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pCtx)
2267{
2268 // Need to set the cursor to end of the bstr and length to the next length
2269 // above in the nesting tree (or the top level length).
2270
2271}
2272
2273
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002274void QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe,
2275 int64_t nLabel,
2276 uint8_t uQcborType,
2277 QCBORItem *pItem)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002278{
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002279 if(pMe->uLastError != QCBOR_SUCCESS) {
2280 return;
2281 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002282
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002283 QCBORItem OneItemSeach[2];
2284 OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64;
2285 OneItemSeach[0].label.int64 = nLabel;
2286 OneItemSeach[0].uDataType = uQcborType;
2287 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array
Laurence Lundblade1341c592020-04-11 14:19:05 -07002288
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002289 QCBORError nReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL, NULL);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002290 if(nReturn) {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002291 pMe->uLastError = (uint8_t)nReturn;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002292 }
2293
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002294 if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) {
2295 pMe->uLastError = QCBOR_ERR_NOT_FOUND;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002296 }
2297
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002298 *pItem = OneItemSeach[0];
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002299}
2300
2301
2302QCBORError QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe,
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002303 const char *szLabel,
2304 uint8_t uQcborType,
2305 QCBORItem *pItem)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002306{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002307 QCBORItem OneItemSeach[2];
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002308
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002309 OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
2310 OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel);
2311 OneItemSeach[0].uDataType = uQcborType;
2312 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array
Laurence Lundblade1341c592020-04-11 14:19:05 -07002313
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002314 QCBORError nReturn = MapSearch(pMe, OneItemSeach, NULL, NULL, NULL, NULL);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002315 if(nReturn) {
2316 return nReturn;
2317 }
2318
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002319 if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002320 return QCBOR_ERR_NOT_FOUND;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002321 }
2322
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002323 *pItem = OneItemSeach[0];
Laurence Lundblade1341c592020-04-11 14:19:05 -07002324
2325 return QCBOR_SUCCESS;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002326}
2327
2328
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002329static QCBORError CheckTagRequirement(const TagSpecification TagSpec, uint8_t uDataType)
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002330{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002331 if(TagSpec.uTagRequirement == QCBOR_TAGSPEC_MATCH_TAG) {
2332 /* Must match the tag */
2333 if(uDataType == TagSpec.uTaggedType) {
2334 return QCBOR_SUCCESS;
2335 }
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002336 } else {
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002337 /* QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE or QCBOR_TAGSPEC_MATCH_EITHER */
2338 /* Must check all the possible types for the tag content */
2339 for(size_t i = 0; i < sizeof(TagSpec.uAllowedContentTypes); i++) {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002340 if(uDataType == TagSpec.uAllowedContentTypes[i]) {
2341 return QCBOR_SUCCESS;
2342 }
2343 }
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002344 /* Didn't match any of the tag content types */
2345 /* Check the tag for the either case */
2346 if(TagSpec.uTagRequirement == QCBOR_TAGSPEC_MATCH_EITHER) {
2347 if(uDataType == TagSpec.uTaggedType) {
2348 return QCBOR_SUCCESS;
2349 }
2350 }
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002351 }
2352
2353 return QCBOR_ERR_UNEXPECTED_TYPE;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002354}
2355
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002356
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002357void QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe,
2358 int64_t nLabel,
2359 TagSpecification TagSpec,
2360 QCBORItem *pItem)
2361{
2362 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem);
2363 if(pMe->uLastError != QCBOR_SUCCESS) {
2364 return;
2365 }
2366
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07002367 pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem->uDataType);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002368}
2369
2370void QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe,
2371 const char *szLabel,
2372 TagSpecification TagSpec,
2373 QCBORItem *pItem)
2374{
2375 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem);
2376 if(pMe->uLastError != QCBOR_SUCCESS) {
2377 return;
2378 }
2379
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07002380 pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, pItem->uDataType);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002381}
2382
2383void QCBORDecode_GetTaggedStringInMapN(QCBORDecodeContext *pMe,
2384 int64_t nLabel,
2385 TagSpecification TagSpec,
2386 UsefulBufC *pString)
2387{
2388 QCBORItem Item;
2389 QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item);
2390 if(pMe->uLastError == QCBOR_SUCCESS) {
2391 *pString = Item.val.string;
2392 }
2393}
2394
2395void QCBORDecode_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe,
2396 const char * szLabel,
2397 TagSpecification TagSpec,
2398 UsefulBufC *pString)
2399{
2400 QCBORItem Item;
2401 QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item);
2402 if(pMe->uLastError == QCBOR_SUCCESS) {
2403 *pString = Item.val.string;
2404 }
2405}
Laurence Lundblade1341c592020-04-11 14:19:05 -07002406
Laurence Lundblade1341c592020-04-11 14:19:05 -07002407
Laurence Lundblade34691b92020-05-18 22:25:25 -07002408static void SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[])
Laurence Lundblade1341c592020-04-11 14:19:05 -07002409{
Laurence Lundblade34691b92020-05-18 22:25:25 -07002410 if(pMe->uLastError != QCBOR_SUCCESS) {
2411 // Already in error state; do nothing.
2412 return;
2413 }
2414
2415 size_t uOffset;
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002416 pMe->uLastError = (uint8_t)MapSearch(pMe, pSearch, &uOffset, NULL, NULL, NULL);
Laurence Lundblade34691b92020-05-18 22:25:25 -07002417 if(pMe->uLastError != QCBOR_SUCCESS) {
2418 return;
2419 }
2420
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002421 /* Need to get the current pre-order nesting level and cursor to be
2422 at the first item in the map/array just entered.
2423
2424 Also need to current map nesting level and start cursor to
2425 be at the right place.
2426
2427 The UsefulInBuf offset could be anywhere, so no assumption is
2428 made about it.
2429
2430 No assumption is made about the pre-order nesting level either.
2431
2432 However the map mode nesting level is assumed to be one above
2433 the map level that is being entered.
2434 */
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002435 /* Seek to the data item that is the map or array */
2436 UsefulInputBuf_Seek(&(pMe->InBuf), uOffset);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002437 pMe->nesting.pCurrent = pMe->nesting.pCurrentMap; // TODO: part of DecodeNesting
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002438
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002439 // TODO: check error?
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002440 QCBORDecode_EnterBoundedMode(pMe, pSearch->uDataType);
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002441
Laurence Lundblade34691b92020-05-18 22:25:25 -07002442 printdecode(pMe, "FinishEnter");
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002443}
2444
2445
Laurence Lundblade34691b92020-05-18 22:25:25 -07002446void QCBORDecode_EnterMapInMapN(QCBORDecodeContext *pMe, int64_t nLabel)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002447{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002448 QCBORItem OneItemSeach[2];
2449 OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64;
2450 OneItemSeach[0].label.int64 = nLabel;
2451 OneItemSeach[0].uDataType = QCBOR_TYPE_MAP;
2452 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002453
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002454 /* The map to enter was found, now finish of entering it. */
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002455 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002456}
2457
2458
Laurence Lundblade34691b92020-05-18 22:25:25 -07002459void QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002460{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002461 QCBORItem OneItemSeach[2];
2462 OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
2463 OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel);
2464 OneItemSeach[0].uDataType = QCBOR_TYPE_MAP;
2465 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002466
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002467 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002468}
2469
2470
Laurence Lundblade34691b92020-05-18 22:25:25 -07002471void QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel)
Laurence Lundblade1341c592020-04-11 14:19:05 -07002472{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002473 QCBORItem OneItemSeach[2];
2474 OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64;
2475 OneItemSeach[0].label.int64 = nLabel;
2476 OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY;
2477 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002478
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002479 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundblade34691b92020-05-18 22:25:25 -07002480}
2481
2482
2483void QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel)
2484{
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002485 QCBORItem OneItemSeach[2];
2486 OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING;
2487 OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel);
2488 OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY;
2489 OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002490
Laurence Lundbladeb90f5362020-05-25 12:17:40 -07002491 SearchAndEnter(pMe, OneItemSeach);
Laurence Lundblade1341c592020-04-11 14:19:05 -07002492}
2493
2494
2495
2496
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002497
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002498/* Next item must be map or this generates an error */
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002499void QCBORDecode_EnterBoundedMode(QCBORDecodeContext *pMe, uint8_t uType)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002500{
Laurence Lundblade34691b92020-05-18 22:25:25 -07002501 if(pMe->uLastError != QCBOR_SUCCESS) {
2502 // Already in error state; do nothing.
2503 return;
2504 }
Laurence Lundblade1341c592020-04-11 14:19:05 -07002505
2506 /* Get the data item that is the map that is being searched */
Laurence Lundblade34691b92020-05-18 22:25:25 -07002507 QCBORItem Item;
Laurence Lundblade986017c2020-05-23 19:25:02 -07002508 pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item);
Laurence Lundblade34691b92020-05-18 22:25:25 -07002509 if(pMe->uLastError != QCBOR_SUCCESS) {
2510 return;
Laurence Lundblade3f9ef042020-04-14 13:15:51 -07002511 }
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07002512 if(Item.uDataType != uType) {
Laurence Lundblade34691b92020-05-18 22:25:25 -07002513 pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
2514 return;
Laurence Lundblade1341c592020-04-11 14:19:05 -07002515 }
2516
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002517 DecodeNesting_EnterMapMode(&(pMe->nesting), UsefulInputBuf_Tell(&(pMe->InBuf)));
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002518
Laurence Lundblade34691b92020-05-18 22:25:25 -07002519 printdecode(pMe, "EnterMapModeDone");
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002520}
2521
2522
2523
2524QCBORError QCBORDecode_GetItemsInMap(QCBORDecodeContext *pCtx, QCBORItem *pItemList)
2525{
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002526 return MapSearch(pCtx, pItemList, NULL, NULL, NULL, NULL);
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002527}
2528
2529
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002530QCBORError QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pCtx, QCBORItem *pItemList, void *pCallbackCtx, QCBORItemCallback pfCB)
2531{
2532 return MapSearch(pCtx, pItemList, NULL, NULL, pCallbackCtx, pfCB);
2533}
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002534
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002535
2536
Laurence Lundblade1341c592020-04-11 14:19:05 -07002537void QCBORDecode_RewindMap(QCBORDecodeContext *pMe)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07002538{
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002539 // TODO: check for map mode; test this
Laurence Lundblade1341c592020-04-11 14:19:05 -07002540 pMe->nesting.pCurrent->uCount = pMe->nesting.pCurrent->uSaveCount;
2541 UsefulInputBuf_Seek(&(pMe->InBuf), pMe->nesting.pCurrent->uOffset);
2542}
2543
2544
Laurence Lundblade1341c592020-04-11 14:19:05 -07002545
Laurence Lundblade24d509a2020-06-06 18:43:15 -07002546void QCBORDecode_EnterBstr(QCBORDecodeContext *pMe)
2547{
2548 if(pMe->uLastError != QCBOR_SUCCESS) {
2549 // Already in error state; do nothing.
2550 return;
2551 }
2552
2553 /* Get the data item that is the map that is being searched */
2554 QCBORItem Item;
2555 pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item);
2556 if(pMe->uLastError != QCBOR_SUCCESS) {
2557 return;
2558 }
2559 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
2560 pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE;
2561 return;
2562 }
2563
2564 // TODO: check for tag 24
2565
2566 // Need to move UIB input cursor to the right place
2567
2568 // Really this is a subtraction and an assignment; not much code
2569 // There is a range check in the seek.
2570 const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf));
2571
2572 UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset - Item.val.string.len);
2573
2574 UsefulInputBuf_SetBufferLen(&(pMe->InBuf), uEndOffset);
2575
2576 // TODO: comment on cast
2577 pMe->uLastError = (uint8_t)DecodeNesting_Descend2(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING, UINT16_MAX, (uint32_t)uEndOffset);
2578}
2579
2580
Laurence Lundbladebf3c42d2020-04-14 19:08:51 -07002581
Laurence Lundbladee6430642020-03-14 21:15:44 -07002582
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002583
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002584
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002585
Laurence Lundblade11a064e2020-05-07 13:13:42 -07002586
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002587
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002588static QCBORError InterpretBool(const QCBORItem *pItem, bool *pBool)
2589{
2590 switch(pItem->uDataType) {
2591 case QCBOR_TYPE_TRUE:
2592 *pBool = true;
2593 return QCBOR_SUCCESS;
2594 break;
2595
2596 case QCBOR_TYPE_FALSE:
2597 *pBool = false;
2598 return QCBOR_SUCCESS;
2599 break;
2600
2601 default:
2602 return QCBOR_ERR_UNEXPECTED_TYPE;
2603 break;
2604 }
2605}
Laurence Lundbladee6430642020-03-14 21:15:44 -07002606
Laurence Lundbladec4537442020-04-14 18:53:22 -07002607void QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002608{
Laurence Lundbladebf3c42d2020-04-14 19:08:51 -07002609 if(pMe->uLastError != QCBOR_SUCCESS) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07002610 // Already in error state, do nothing
Laurence Lundbladee6430642020-03-14 21:15:44 -07002611 return;
2612 }
2613
Laurence Lundbladec4537442020-04-14 18:53:22 -07002614 QCBORError nError;
2615 QCBORItem Item;
2616
2617 nError = QCBORDecode_GetNext(pMe, &Item);
2618 if(nError != QCBOR_SUCCESS) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002619 pMe->uLastError = (uint8_t)nError;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002620 return;
2621 }
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002622 pMe->uLastError = (uint8_t)InterpretBool(&Item, pValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002623}
2624
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002625void QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool *pValue)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002626{
2627 QCBORItem Item;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002628 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002629
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002630 pMe->uLastError = (uint8_t)InterpretBool(&Item, pValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002631}
2632
2633
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002634void QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, bool *pValue)
2635{
2636 QCBORItem Item;
2637 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
2638
2639 pMe->uLastError = (uint8_t)InterpretBool(&Item, pValue);
2640}
2641
2642
2643
2644void QCBORDecode_GetTaggedStringInternal(QCBORDecodeContext *pMe, TagSpecification TagSpec, UsefulBufC *pBstr)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002645{
Laurence Lundbladebf3c42d2020-04-14 19:08:51 -07002646 if(pMe->uLastError != QCBOR_SUCCESS) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07002647 // Already in error state, do nothing
2648 return;
2649 }
2650
2651 QCBORError nError;
2652 QCBORItem Item;
2653
2654 nError = QCBORDecode_GetNext(pMe, &Item);
2655 if(nError != QCBOR_SUCCESS) {
Laurence Lundblade82c2a8f2020-04-29 12:40:19 -07002656 pMe->uLastError = (uint8_t)nError;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002657 return;
2658 }
2659
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002660 pMe->uLastError = (uint8_t)CheckTagRequirement(TagSpec, Item.uDataType);
2661
2662 if(pMe->uLastError == QCBOR_SUCCESS) {
2663 *pBstr = Item.val.string;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002664 }
2665}
2666
Laurence Lundbladec4537442020-04-14 18:53:22 -07002667
2668
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002669
2670static QCBORError ConvertBigNum(const QCBORItem *pItem, UsefulBufC *pValue, bool *pbIsNegative)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002671{
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002672 *pbIsNegative = false;
2673
2674 bool bMustBeTagged = true; // TODO: fix this
2675
2676 switch(pItem->uDataType) {
2677 case QCBOR_TYPE_BYTE_STRING:
2678 // TODO: check that there is no tag here?
2679 if(bMustBeTagged) {
2680 return QCBOR_ERR_UNEXPECTED_TYPE;
2681 } else {
2682 *pValue = pItem->val.string;
2683 return QCBOR_SUCCESS;
2684 }
2685 break;
2686
2687 case QCBOR_TYPE_POSBIGNUM:
2688 *pValue = pItem->val.string;
2689 return QCBOR_SUCCESS;
2690 break;
2691
2692 case QCBOR_TYPE_NEGBIGNUM:
2693 *pbIsNegative = true;
2694 *pValue = pItem->val.string;
2695 return QCBOR_SUCCESS;
2696 break;
2697
2698 default:
2699 return QCBOR_ERR_UNEXPECTED_TYPE;
2700 break;
2701 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002702}
2703
2704
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002705/**
2706 @param[in] bMustBeTagged If \c true, then the data item must be tagged as either
2707 a positive or negative bignum. If \c false, then it only must be a byte string and bIsNegative
2708 will always be false on the asumption that it is positive, but it can be interpretted as
2709 negative if the the sign is know from other context.
2710 @param[out] pValue The bytes that make up the big num
2711 @param[out] pbIsNegative \c true if tagged as a negative big num. \c false otherwise.
2712
2713 if bMustBeTagged is false, then this will succeed if the data item is a plain byte string,
2714 a positive big num or a negative big num.
2715
2716 */
2717void QCBORDecode_GetBignum(QCBORDecodeContext *pMe, bool bMustBeTagged, UsefulBufC *pValue, bool *pbIsNegative)
2718{
2719 if(pMe->uLastError != QCBOR_SUCCESS) {
2720 // Already in error state, do nothing
2721 return;
2722 }
2723
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002724 QCBORItem Item;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002725 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
2726 if(uError != QCBOR_SUCCESS) {
2727 pMe->uLastError = (uint8_t)uError;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002728 return;
2729 }
2730
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002731 pMe->uLastError = (uint8_t)ConvertBigNum(&Item, pValue, pbIsNegative);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002732}
2733
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002734void QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, int64_t nLabel, bool bMustBeTagged, UsefulBufC *pValue, bool *pbIsNegative)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002735{
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002736 QCBORItem Item;
2737 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002738
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07002739 pMe->uLastError = (uint8_t)ConvertBigNum(&Item, pValue, pbIsNegative);
Laurence Lundbladec4537442020-04-14 18:53:22 -07002740}
2741
Laurence Lundbladec4537442020-04-14 18:53:22 -07002742
2743
2744
Laurence Lundbladee6430642020-03-14 21:15:44 -07002745
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002746typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002747
2748
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002749// The main exponentiator that works on only positive numbers
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002750static QCBORError Exponentitate10(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult)
Laurence Lundbladec4537442020-04-14 18:53:22 -07002751{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002752 uint64_t uResult = uMantissa;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002753
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002754 if(uResult != 0) {
2755 /* This loop will run a maximum of 19 times because
2756 * UINT64_MAX < 10 ^^ 19. More than that will cause
2757 * exit with the overflow error
2758 */
2759 for(; nExponent > 0; nExponent--) {
2760 if(uResult > UINT64_MAX / 10) {
2761 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow
2762 }
2763 uResult = uResult * 10;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002764 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002765
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002766 for(; nExponent < 0; nExponent++) {
2767 uResult = uResult / 10;
2768 if(uResult == 0) {
2769 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error
2770 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002771 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07002772 }
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002773 /* else, mantissa is zero so this returns zero */
Laurence Lundbladec4537442020-04-14 18:53:22 -07002774
2775 *puResult = uResult;
2776
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002777 return QCBOR_SUCCESS;
Laurence Lundbladec4537442020-04-14 18:53:22 -07002778}
2779
2780
Laurence Lundbladee6430642020-03-14 21:15:44 -07002781/* Convert a decimal fraction to an int64_t without using
2782 floating point or math libraries. Most decimal fractions
2783 will not fit in an int64_t and this will error out with
2784 under or overflow
2785 */
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002786static QCBORError Exponentitate2(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002787{
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002788 uint64_t uResult;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002789
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002790 uResult = uMantissa;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002791
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002792 /* This loop will run a maximum of 64 times because
Laurence Lundbladee6430642020-03-14 21:15:44 -07002793 * INT64_MAX < 2^31. More than that will cause
2794 * exist with the overflow error
2795 */
2796 while(nExponent > 0) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002797 if(uResult > UINT64_MAX >> 1) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002798 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Error overflow
Laurence Lundbladee6430642020-03-14 21:15:44 -07002799 }
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002800 uResult = uResult << 1;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002801 nExponent--;
2802 }
2803
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002804 while(nExponent < 0 ) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002805 if(uResult == 0) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002806 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; // Underflow error
2807 }
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002808 uResult = uResult >> 1;
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002809 nExponent++;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002810 }
2811
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002812 *puResult = uResult;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002813
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002814 return QCBOR_SUCCESS;
2815}
2816
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002817/*
2818 Compute value with signed mantissa and signed result. Works with exponent of 2 or 10 based on exponentiator.
2819 */
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002820static inline QCBORError ExponentiateNN(int64_t nMantissa, int64_t nExponent, int64_t *pnResult, fExponentiator pfExp)
2821{
2822 uint64_t uResult;
2823
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002824 // Take the absolute value of the mantissa and convert to unsigned.
2825 // TODO: this should be possible in one intruction
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002826 uint64_t uMantissa = nMantissa > 0 ? (uint64_t)nMantissa : (uint64_t)-nMantissa;
2827
2828 // Do the exponentiation of the positive mantissa
2829 QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult);
2830 if(uReturn) {
2831 return uReturn;
2832 }
2833
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002834
Laurence Lundblade983500d2020-05-14 11:49:34 -07002835 /* (uint64_t)INT64_MAX+1 is used to represent the absolute value
2836 of INT64_MIN. This assumes two's compliment representation where
2837 INT64_MIN is one increment farther from 0 than INT64_MAX.
2838 Trying to write -INT64_MIN doesn't work to get this because the
2839 compiler tries to work with an int64_t which can't represent
2840 -INT64_MIN.
2841 */
2842 uint64_t uMax = nMantissa > 0 ? INT64_MAX : (uint64_t)INT64_MAX+1;
2843
2844 // Error out if too large
2845 if(uResult > uMax) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002846 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
2847 }
2848
2849 // Casts are safe because of checks above
2850 *pnResult = nMantissa > 0 ? (int64_t)uResult : -(int64_t)uResult;
2851
2852 return QCBOR_SUCCESS;
2853}
2854
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002855/*
2856 Compute value with signed mantissa and unsigned result. Works with exponent of 2 or 10 based on exponentiator.
2857 */
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002858static inline QCBORError ExponentitateNU(int64_t nMantissa, int64_t nExponent, uint64_t *puResult, fExponentiator pfExp)
2859{
2860 if(nMantissa < 0) {
2861 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
2862 }
2863
2864 // Cast to unsigned is OK because of check for negative
2865 // Cast to unsigned is OK because UINT64_MAX > INT64_MAX
2866 // Exponentiation is straight forward
2867 return (*pfExp)((uint64_t)nMantissa, nExponent, puResult);
2868}
2869
2870
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002871#include <math.h>
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002872
2873
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07002874static QCBORError ConvertBigNumToUnsigned(const UsefulBufC BigNum, uint64_t uMax, uint64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002875{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002876 uint64_t uResult;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002877
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002878 uResult = 0;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002879 const uint8_t *pByte = BigNum.ptr;
2880 size_t uLen = BigNum.len;
2881 while(uLen--) {
Laurence Lundblade313b2862020-05-16 01:23:06 -07002882 if(uResult > (uMax >> 8)) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07002883 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002884 }
Laurence Lundblade313b2862020-05-16 01:23:06 -07002885 uResult = (uResult << 8) + *pByte++;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002886 }
2887
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002888 *pResult = uResult;
2889 return QCBOR_SUCCESS;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002890}
2891
Laurence Lundblade887add82020-05-17 05:50:34 -07002892static inline QCBORError ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, uint64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002893{
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002894 return ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult);
Laurence Lundbladee6430642020-03-14 21:15:44 -07002895}
2896
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002897static inline QCBORError ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002898{
2899 uint64_t uResult;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002900 QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult);
2901 if(uError) {
2902 return uError;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002903 }
2904 /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */
2905 *pResult = (int64_t)uResult;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002906 return QCBOR_SUCCESS;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002907}
2908
2909
Laurence Lundblade9c905e82020-04-25 11:31:38 -07002910static inline QCBORError ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, int64_t *pResult)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002911{
2912 uint64_t uResult;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002913 QCBORError uError = ConvertBigNumToUnsigned(BigNum, INT64_MAX-1, &uResult);
2914 if(uError) {
2915 return uError;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002916 }
2917 /* Cast is safe because ConvertBigNum is told to limit to INT64_MAX */
Laurence Lundblade887add82020-05-17 05:50:34 -07002918 // TODO: this code is incorrect. See RFC 7049
Laurence Lundbladee6430642020-03-14 21:15:44 -07002919 *pResult = -(int64_t)uResult;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002920 return QCBOR_SUCCESS;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002921}
2922
Laurence Lundbladef6c86662020-05-12 02:08:00 -07002923#include "fenv.h"
Laurence Lundbladec4537442020-04-14 18:53:22 -07002924
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002925
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07002926/*
2927Convert a integers and floats to an int64_t.
2928
2929\param[in] uOptions Bit mask list of conversion options.
2930
2931\retval QCBOR_ERR_CONVERSION_NOT_REQUESTED Conversion, possible, but not requested in uOptions.
2932
2933\retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
2934
2935\retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large or too small.
2936
2937*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002938static QCBORError ConvertInt64(const QCBORItem *pItem, uint32_t uOptions, int64_t *pnValue)
2939{
2940 switch(pItem->uDataType) {
2941 // TODO: float when ifdefs are set
2942 case QCBOR_TYPE_DOUBLE:
2943 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
2944 // TODO: what about under/overflow here?
2945 // Invokes the floating-point HW and/or compiler-added libraries
2946 feclearexcept(FE_ALL_EXCEPT);
2947 *pnValue = llround(pItem->val.dfnum);
2948 if(fetestexcept(FE_INVALID)) {
2949 // TODO: better error code
2950 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
2951 }
2952 } else {
2953 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
2954 }
2955 break;
2956
2957 case QCBOR_TYPE_INT64:
2958 if(uOptions & QCBOR_CONVERT_TYPE_INT64) {
2959 *pnValue = pItem->val.int64;
2960 } else {
2961 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
2962 }
2963 break;
2964
2965 case QCBOR_TYPE_UINT64:
2966 if(uOptions & QCBOR_CONVERT_TYPE_UINT64) {
2967 if(pItem->val.uint64 < INT64_MAX) {
2968 *pnValue = pItem->val.int64;
2969 } else {
2970 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
2971 }
2972 } else {
2973 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
2974 }
2975 break;
2976
2977 default:
2978 return QCBOR_ERR_UNEXPECTED_TYPE;
2979 }
2980 return QCBOR_SUCCESS;
2981}
2982
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07002983
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002984void QCBORDecode_GetInt64ConvertInternal(QCBORDecodeContext *pMe,
2985 uint32_t uOptions,
Laurence Lundblade843a10c2020-05-23 13:57:00 -07002986 int64_t *pnValue,
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002987 QCBORItem *pItem)
Laurence Lundbladee6430642020-03-14 21:15:44 -07002988{
Laurence Lundbladebf3c42d2020-04-14 19:08:51 -07002989 if(pMe->uLastError != QCBOR_SUCCESS) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07002990 return;
2991 }
2992
Laurence Lundbladee6430642020-03-14 21:15:44 -07002993 QCBORItem Item;
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07002994 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
2995 if(uError) {
2996 pMe->uLastError = (uint8_t)uError;
Laurence Lundbladee6430642020-03-14 21:15:44 -07002997 return;
2998 }
2999
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003000 if(pItem) {
3001 *pItem = Item;
3002 }
3003
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003004 pMe->uLastError = (uint8_t)ConvertInt64(&Item, uOptions, pnValue);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003005}
3006
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003007
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003008void QCBORDecode_GetInt64ConvertInternalInMapN(QCBORDecodeContext *pMe,
3009 int64_t nLabel,
3010 uint32_t uOptions,
3011 int64_t *pnValue,
3012 QCBORItem *pItem)
3013{
3014 QCBORItem Item;
3015 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
3016
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003017 pMe->uLastError = (uint8_t)ConvertInt64(&Item, uOptions, pnValue);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003018}
3019
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003020
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003021void QCBORDecode_GetInt64ConvertInternalInMapSZ(QCBORDecodeContext *pMe,
3022 const char * szLabel,
3023 uint32_t uOptions,
3024 int64_t *pnValue,
3025 QCBORItem *pItem)
3026{
3027 if(pMe->uLastError != QCBOR_SUCCESS) {
3028 return;
3029 }
3030
3031 QCBORItem Item;
3032 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
3033
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003034 pMe->uLastError = (uint8_t)ConvertInt64(&Item, uOptions, pnValue);
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003035}
3036
3037
3038
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003039/*
3040 Convert a large variety of integer types to an int64_t.
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003041
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003042 \param[in] uOptions Bit mask list of conversion options.
3043
3044 \retval QCBOR_ERR_CONVERSION_NOT_REQUESTED Conversion, possible, but not requested in uOptions.
3045
3046 \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted
3047
3048 \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large or too small.
3049
3050 */
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003051static QCBORError Int64ConvertAll(const QCBORItem *pItem, uint32_t uOptions, int64_t *pnValue)
3052{
3053 QCBORError uErr;
3054
3055 switch(pItem->uDataType) {
3056
3057 case QCBOR_TYPE_POSBIGNUM:
3058 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3059 return ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003060 } else {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003061 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003062 }
3063 break;
3064
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003065 case QCBOR_TYPE_NEGBIGNUM:
3066 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3067 return ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003068 } else {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003069 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003070 }
3071 break;
3072
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003073#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3074 case QCBOR_TYPE_DECIMAL_FRACTION:
3075 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3076 return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt,
3077 pItem->val.expAndMantissa.nExponent,
3078 pnValue,
3079 &Exponentitate10);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003080 } else {
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003081 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3082 }
3083 break;
3084
3085 case QCBOR_TYPE_BIGFLOAT:
3086 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
3087 return ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt,
3088 pItem->val.expAndMantissa.nExponent,
3089 pnValue,
3090 Exponentitate2);
3091 } else {
3092 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3093 }
3094 break;
3095
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003096 case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
3097 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3098 int64_t nMantissa;
3099 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3100 if(uErr) {
3101 return uErr;
3102 }
3103 return ExponentiateNN(nMantissa,
3104 pItem->val.expAndMantissa.nExponent,
3105 pnValue,
3106 Exponentitate10);
3107 } else {
3108 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3109 }
3110 break;
3111
3112 case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
3113 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3114 int64_t nMantissa;
3115 uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3116 if(uErr) {
3117 return uErr;
3118 }
3119 return ExponentiateNN(nMantissa,
3120 pItem->val.expAndMantissa.nExponent,
3121 pnValue,
3122 Exponentitate10);
3123 } else {
3124 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3125 }
3126 break;
3127
3128 case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
3129 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3130 int64_t nMantissa;
3131 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3132 if(uErr) {
3133 return uErr;
3134 }
3135 return ExponentiateNN(nMantissa,
3136 pItem->val.expAndMantissa.nExponent,
3137 pnValue,
3138 Exponentitate2);
3139 } else {
3140 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3141 }
3142 break;
3143
3144 case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
3145 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3146 int64_t nMantissa;
3147 uErr = ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3148 if(uErr) {
3149 return uErr;
3150 }
3151 return ExponentiateNN(nMantissa,
3152 pItem->val.expAndMantissa.nExponent,
3153 pnValue,
3154 Exponentitate2);
3155 } else {
3156 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
Laurence Lundbladee6430642020-03-14 21:15:44 -07003157 }
3158 break;
3159
Laurence Lundbladec4537442020-04-14 18:53:22 -07003160 default:
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003161 return QCBOR_ERR_UNEXPECTED_TYPE;
3162#endif
Laurence Lundbladec4537442020-04-14 18:53:22 -07003163 }
3164}
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003165
3166
Laurence Lundbladec4537442020-04-14 18:53:22 -07003167/*
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003168 Public function, see header qcbor/qcbor_decode.h file
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003169 */
3170void QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, int64_t *pnValue)
Laurence Lundbladec4537442020-04-14 18:53:22 -07003171{
3172 QCBORItem Item;
3173
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003174 QCBORDecode_GetInt64ConvertInternal(pMe, uOptions, pnValue, &Item);
Laurence Lundbladec4537442020-04-14 18:53:22 -07003175
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003176 if(pMe->uLastError == QCBOR_SUCCESS) {
3177 // The above conversion succeeded
3178 return;
3179 }
3180
Laurence Lundbladef6c86662020-05-12 02:08:00 -07003181 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003182 // The above conversion failed in a way that code below can't correct
Laurence Lundbladec4537442020-04-14 18:53:22 -07003183 return;
3184 }
3185
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003186 pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uOptions, pnValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003187}
3188
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003189
3190/*
3191Public function, see header qcbor/qcbor_decode.h file
3192*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003193void QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, int64_t nLabel, uint32_t uOptions, int64_t *pnValue)
3194{
3195 QCBORItem Item;
3196
3197 QCBORDecode_GetInt64ConvertInternalInMapN(pMe, nLabel, uOptions, pnValue, &Item);
3198
3199 if(pMe->uLastError == QCBOR_SUCCESS) {
3200 // The above conversion succeeded
3201 return;
3202 }
3203
3204 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3205 // The above conversion failed in a way that code below can't correct
3206 return;
3207 }
3208
3209 pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uOptions, pnValue);
3210}
3211
3212
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003213/*
3214Public function, see header qcbor/qcbor_decode.h file
3215*/
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003216void QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, uint32_t uOptions, int64_t *pnValue)
3217{
3218 QCBORItem Item;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003219 QCBORDecode_GetInt64ConvertInternalInMapSZ(pMe, szLabel, uOptions, pnValue, &Item);
3220
3221 if(pMe->uLastError == QCBOR_SUCCESS) {
3222 // The above conversion succeeded
3223 return;
3224 }
3225
3226 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3227 // The above conversion failed in a way that code below can't correct
3228 return;
3229 }
3230
3231 pMe->uLastError = (uint8_t)Int64ConvertAll(&Item, uOptions, pnValue);
3232}
3233
3234
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003235static QCBORError ConvertUint64(const QCBORItem *pItem, uint32_t uOptions, uint64_t *puValue)
3236{
3237 switch(pItem->uDataType) {
3238 // TODO: type flaot
3239 case QCBOR_TYPE_DOUBLE:
3240 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
3241 feclearexcept(FE_ALL_EXCEPT);
3242 double dRounded = round(pItem->val.dfnum);
3243 // TODO: over/underflow
3244 if(fetestexcept(FE_INVALID)) {
3245 // TODO: better error code
3246 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
3247 } else if(isnan(dRounded)) {
3248 // TODO: better error code
3249 return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW;
3250 } else if(dRounded >= 0) {
3251 *puValue = (uint64_t)dRounded;
3252 } else {
3253 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3254 }
3255 } else {
3256 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3257 }
3258 break;
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003259
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003260 case QCBOR_TYPE_INT64:
3261 if(uOptions & QCBOR_CONVERT_TYPE_INT64) {
3262 if(pItem->val.int64 >= 0) {
3263 *puValue = (uint64_t)pItem->val.int64;
3264 } else {
3265 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3266 }
3267 } else {
3268 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3269 }
3270 break;
3271
3272 case QCBOR_TYPE_UINT64:
3273 if(uOptions & QCBOR_CONVERT_TYPE_UINT64) {
3274 *puValue = pItem->val.uint64;
3275 } else {
3276 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3277 }
3278 break;
3279
3280 default:
3281 return QCBOR_ERR_UNEXPECTED_TYPE;
3282 }
3283 return QCBOR_SUCCESS;
3284}
Laurence Lundbladec4537442020-04-14 18:53:22 -07003285
3286
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003287void QCBORDecode_GetUInt64ConvertInternal(QCBORDecodeContext *pMe,
3288 uint32_t uOptions,
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003289 uint64_t *puValue,
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003290 QCBORItem *pItem)
Laurence Lundbladec4537442020-04-14 18:53:22 -07003291{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003292 if(pMe->uLastError != QCBOR_SUCCESS) {
3293 return;
3294 }
3295
Laurence Lundbladec4537442020-04-14 18:53:22 -07003296 QCBORItem Item;
Laurence Lundbladec4537442020-04-14 18:53:22 -07003297
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003298 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
3299 if(uError) {
3300 pMe->uLastError = (uint8_t)uError;
Laurence Lundbladec4537442020-04-14 18:53:22 -07003301 return;
3302 }
3303
Laurence Lundbladea826c502020-05-10 21:07:00 -07003304 if(pItem) {
3305 *pItem = Item;
3306 }
3307
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003308 pMe->uLastError = (uint8_t)ConvertUint64(&Item, uOptions, puValue);
Laurence Lundbladec4537442020-04-14 18:53:22 -07003309}
3310
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003311
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003312void QCBORDecode_GetUint64ConvertInternalInMapN(QCBORDecodeContext *pMe,
3313 int64_t nLabel,
3314 uint32_t uOptions,
3315 uint64_t *puValue,
3316 QCBORItem *pItem)
3317{
3318 QCBORItem Item;
3319 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
3320
3321 pMe->uLastError = (uint8_t)ConvertUint64(&Item, uOptions, puValue);
3322}
3323
3324
3325void QCBORDecode_GetUint64ConvertInternalInMapSZ(QCBORDecodeContext *pMe,
3326 const char * szLabel,
3327 uint32_t uOptions,
3328 uint64_t *puValue,
3329 QCBORItem *pItem)
3330{
3331 if(pMe->uLastError != QCBOR_SUCCESS) {
3332 return;
3333 }
3334
3335 QCBORItem Item;
3336 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
3337
3338 pMe->uLastError = (uint8_t)ConvertUint64(&Item, uOptions, puValue);
3339}
3340
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003341/*
3342 Public function, see header qcbor/qcbor_decode.h file
3343*/
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003344static QCBORError Uint64ConvertAll(const QCBORItem *pItem, uint32_t uOptions, uint64_t *puValue)
3345{
3346 QCBORError uErr;
3347
3348 switch(pItem->uDataType) {
3349
3350 case QCBOR_TYPE_POSBIGNUM:
3351 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3352 return ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue);
3353 } else {
3354 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3355 }
3356 break;
3357
3358 case QCBOR_TYPE_NEGBIGNUM:
3359 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3360 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3361 } else {
3362 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3363 }
3364 break;
3365
3366#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3367
3368 case QCBOR_TYPE_DECIMAL_FRACTION:
3369 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3370 return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt,
3371 pItem->val.expAndMantissa.nExponent,
3372 puValue,
3373 Exponentitate10);
3374 } else {
3375 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3376 }
3377 break;
3378
3379 case QCBOR_TYPE_BIGFLOAT:
3380 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
3381 return ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt,
3382 pItem->val.expAndMantissa.nExponent,
3383 puValue,
3384 Exponentitate2);
3385 } else {
3386 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3387 }
3388 break;
3389
3390 case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
3391 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3392 // TODO: Would be better to convert to unsigned
3393 int64_t nMantissa;
3394 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3395 if(uErr != QCBOR_SUCCESS) {
3396 return uErr;
3397 }
3398 return ExponentitateNU(nMantissa,
3399 pItem->val.expAndMantissa.nExponent,
3400 puValue,
3401 Exponentitate10);
3402 } else {
3403 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3404 }
3405 break;
3406
3407 case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
3408 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3409 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3410 } else {
3411 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3412 }
3413 break;
3414
3415 case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
3416 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3417 // TODO: Would be better to convert to unsigned
3418 int64_t nMantissa;
3419 uErr = ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa);
3420 if(uErr != QCBOR_SUCCESS) {
3421 return uErr;
3422 }
3423 return ExponentitateNU(nMantissa,
3424 pItem->val.expAndMantissa.nExponent,
3425 puValue,
3426 Exponentitate2);
3427 } else {
3428 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3429 }
3430 break;
3431
3432 case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
3433 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3434 return QCBOR_ERR_NUMBER_SIGN_CONVERSION;
3435 } else {
3436 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3437 }
3438 break;
3439#endif
3440 default:
3441 return QCBOR_ERR_UNEXPECTED_TYPE;
3442 }
3443}
3444
3445
3446void QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, uint64_t *puValue)
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003447{
3448 QCBORItem Item;
3449
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003450 QCBORDecode_GetUInt64ConvertInternal(pMe, uOptions, puValue, &Item);
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003451
Laurence Lundbladef6c86662020-05-12 02:08:00 -07003452 if(pMe->uLastError == QCBOR_SUCCESS) {
3453 // The above conversion succeeded
3454 return;
3455 }
3456
3457 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3458 // The above conversion failed in a way that code below can't correct
Laurence Lundbladee6430642020-03-14 21:15:44 -07003459 return;
3460 }
3461
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003462 pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uOptions, puValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003463}
3464
Laurence Lundbladec4537442020-04-14 18:53:22 -07003465
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003466/*
3467Public function, see header qcbor/qcbor_decode.h file
3468*/
3469void QCBORDecode_GetUint64ConvertAllInMapN(QCBORDecodeContext *pMe, int64_t nLabel, uint32_t uOptions, uint64_t *puValue)
3470{
3471 QCBORItem Item;
3472
3473 QCBORDecode_GetUint64ConvertInternalInMapN(pMe, nLabel, uOptions, puValue, &Item);
3474
3475 if(pMe->uLastError == QCBOR_SUCCESS) {
3476 // The above conversion succeeded
3477 return;
3478 }
3479
3480 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3481 // The above conversion failed in a way that code below can't correct
3482 return;
3483 }
3484
3485 pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uOptions, puValue);
3486}
3487
3488
3489/*
3490Public function, see header qcbor/qcbor_decode.h file
3491*/
3492void QCBORDecode_GetUint64ConvertAllInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, uint32_t uOptions, uint64_t *puValue)
3493{
3494 QCBORItem Item;
3495 QCBORDecode_GetUint64ConvertInternalInMapSZ(pMe, szLabel, uOptions, puValue, &Item);
3496
3497 if(pMe->uLastError == QCBOR_SUCCESS) {
3498 // The above conversion succeeded
3499 return;
3500 }
3501
3502 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3503 // The above conversion failed in a way that code below can't correct
3504 return;
3505 }
3506
3507 pMe->uLastError = (uint8_t)Uint64ConvertAll(&Item, uOptions, puValue);
3508}
3509
3510
3511static QCBORError ConvertDouble(const QCBORItem *pItem, uint32_t uOptions, double *pdValue)
3512{
3513 switch(pItem->uDataType) {
3514 // TODO: float when ifdefs are set
3515 case QCBOR_TYPE_DOUBLE:
3516 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
3517 if(uOptions & QCBOR_CONVERT_TYPE_FLOAT) {
3518 *pdValue = pItem->val.dfnum;
3519 } else {
3520 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3521 }
3522 }
3523 break;
3524
3525 case QCBOR_TYPE_INT64:
3526 if(uOptions & QCBOR_CONVERT_TYPE_INT64) {
3527 // TODO: how does this work?
3528 *pdValue = (double)pItem->val.int64;
3529
3530 } else {
3531 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3532 }
3533 break;
3534
3535 case QCBOR_TYPE_UINT64:
3536 if(uOptions & QCBOR_CONVERT_TYPE_UINT64) {
3537 *pdValue = (double)pItem->val.uint64;
3538 } else {
3539 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3540 }
3541 break;
3542
3543 default:
3544 return QCBOR_ERR_UNEXPECTED_TYPE;
3545 }
3546
3547 return QCBOR_SUCCESS;
3548}
3549
3550
3551
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003552void QCBORDecode_GetDoubleConvertInternal(QCBORDecodeContext *pMe,
3553 uint32_t uOptions,
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003554 double *pdValue,
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003555 QCBORItem *pItem)
Laurence Lundbladee6430642020-03-14 21:15:44 -07003556{
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003557 if(pMe->uLastError != QCBOR_SUCCESS) {
Laurence Lundbladec4537442020-04-14 18:53:22 -07003558 return;
3559 }
3560
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003561 QCBORItem Item;
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003562
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003563 QCBORError uError = QCBORDecode_GetNext(pMe, &Item);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003564 if(uError) {
3565 pMe->uLastError = (uint8_t)uError;
3566 return;
3567 }
3568
3569 if(pItem) {
3570 *pItem = Item;
3571 }
Laurence Lundbladec4537442020-04-14 18:53:22 -07003572
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003573 pMe->uLastError = (uint8_t)ConvertDouble(&Item, uOptions, pdValue);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003574}
Laurence Lundbladec4537442020-04-14 18:53:22 -07003575
Laurence Lundbladec4537442020-04-14 18:53:22 -07003576
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003577void QCBORDecode_GetDoubleConvertInternalInMapN(QCBORDecodeContext *pMe,
3578 int64_t nLabel,
3579 uint32_t uOptions,
3580 double *pdValue,
3581 QCBORItem *pItem)
3582{
3583 QCBORItem Item;
3584 QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item);
3585
3586 pMe->uLastError = (uint8_t)ConvertDouble(&Item, uOptions, pdValue);
3587}
3588
3589void QCBORDecode_GetDoubleConvertInternalInMapSZ(QCBORDecodeContext *pMe,
3590 const char * szLabel,
3591 uint32_t uOptions,
3592 double *pdValue,
3593 QCBORItem *pItem)
3594{
3595 if(pMe->uLastError != QCBOR_SUCCESS) {
3596 return;
3597 }
3598
3599 QCBORItem Item;
3600 QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item);
3601
3602 pMe->uLastError = (uint8_t)ConvertDouble(&Item, uOptions, pdValue);
3603}
3604
3605
3606
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07003607static double ConvertBigNumToDouble(const UsefulBufC BigNum)
3608{
3609 double dResult;
3610
3611 dResult = 0.0;
3612 const uint8_t *pByte = BigNum.ptr;
3613 size_t uLen = BigNum.len;
3614 /* This will overflow and become the float value INFINITY if the number
3615 is too large to fit. No error will be logged.
3616 TODO: should an error be logged? */
3617 while(uLen--) {
3618 dResult = (dResult * 256.0) + (double)*pByte++;
3619 }
3620
3621 return dResult;
3622}
3623
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003624static QCBORError DoubleConvertAll(const QCBORItem *pItem, uint32_t uOptions, double *pdValue)
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003625{
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003626 /*
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07003627 https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
3628
3629 */
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003630 switch(pItem->uDataType) {
3631 // TODO: type float
3632 case QCBOR_TYPE_DECIMAL_FRACTION:
3633 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3634 // TODO: rounding and overflow errors
3635 *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt *
3636 pow(10.0, (double)pItem->val.expAndMantissa.nExponent);
3637 } else {
3638 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3639 }
3640 break;
3641
3642 case QCBOR_TYPE_BIGFLOAT:
3643 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT ) {
3644 *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt *
3645 exp2((double)pItem->val.expAndMantissa.nExponent);
3646 } else {
3647 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3648 }
3649 break;
3650
3651 case QCBOR_TYPE_POSBIGNUM:
3652 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3653 *pdValue = ConvertBigNumToDouble(pItem->val.bigNum);
3654 } else {
3655 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3656 }
3657 break;
3658
3659 case QCBOR_TYPE_NEGBIGNUM:
3660 if(uOptions & QCBOR_CONVERT_TYPE_BIG_NUM) {
3661 *pdValue = -ConvertBigNumToDouble(pItem->val.bigNum);
3662 } else {
3663 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3664 }
3665 break;
3666
3667 case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM:
3668 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3669 double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
3670 *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent);
3671 } else {
3672 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3673 }
3674 break;
3675
3676 case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM:
3677 if(uOptions & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) {
3678 double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
3679 *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent);
3680 } else {
3681 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3682 }
3683 break;
3684
3685 case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM:
3686 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
3687 double dMantissa = ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
3688 *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent);
3689 } else {
3690 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3691 }
3692 break;
3693
3694 case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM:
3695 if(uOptions & QCBOR_CONVERT_TYPE_BIGFLOAT) {
3696 double dMantissa = -ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum);
3697 *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent);
3698 } else {
3699 return QCBOR_ERR_CONVERSION_NOT_REQUESTED;
3700 }
3701 break;
3702
3703 default:
3704 return QCBOR_ERR_UNEXPECTED_TYPE;
3705 }
3706
3707 return QCBOR_SUCCESS;
3708}
3709
3710
3711/*
3712 Public function, see header qcbor/qcbor_decode.h file
3713*/
3714void QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, uint32_t uOptions, double *pdValue)
3715{
3716
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003717 QCBORItem Item;
3718
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003719 QCBORDecode_GetDoubleConvertInternal(pMe, uOptions, pdValue, &Item);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07003720
3721 if(pMe->uLastError == QCBOR_SUCCESS) {
3722 // The above conversion succeeded
3723 return;
3724 }
3725
3726 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3727 // The above conversion failed in a way that code below can't correct
3728 return;
3729 }
3730
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003731 pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uOptions, pdValue);
Laurence Lundbladee6430642020-03-14 21:15:44 -07003732}
3733
Laurence Lundblade7e5be1d2020-05-24 21:17:28 -07003734
3735/*
3736Public function, see header qcbor/qcbor_decode.h file
3737*/
3738void QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, int64_t nLabel, uint32_t uOptions, double *pdValue)
3739{
3740 QCBORItem Item;
3741
3742 QCBORDecode_GetDoubleConvertInternalInMapN(pMe, nLabel, uOptions, pdValue, &Item);
3743
3744 if(pMe->uLastError == QCBOR_SUCCESS) {
3745 // The above conversion succeeded
3746 return;
3747 }
3748
3749 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3750 // The above conversion failed in a way that code below can't correct
3751 return;
3752 }
3753
3754 pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uOptions, pdValue);
3755}
3756
3757
3758/*
3759Public function, see header qcbor/qcbor_decode.h file
3760*/
3761void QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, uint32_t uOptions, double *pdValue)
3762{
3763 QCBORItem Item;
3764 QCBORDecode_GetDoubleConvertInternalInMapSZ(pMe, szLabel, uOptions, pdValue, &Item);
3765
3766 if(pMe->uLastError == QCBOR_SUCCESS) {
3767 // The above conversion succeeded
3768 return;
3769 }
3770
3771 if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) {
3772 // The above conversion failed in a way that code below can't correct
3773 return;
3774 }
3775
3776 pMe->uLastError = (uint8_t)DoubleConvertAll(&Item, uOptions, pdValue);
3777}