Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1 | /*============================================================================== |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 2 | Copyright (c) 2016-2018, The Linux Foundation. |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3 | Copyright (c) 2018-2024, Laurence Lundblade. |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 4 | Copyright (c) 2021, Arm Limited. |
Laurence Lundblade | d92a616 | 2018-11-01 11:38:35 +0700 | [diff] [blame] | 5 | All rights reserved. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 6 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 7 | Redistribution and use in source and binary forms, with or without |
| 8 | modification, are permitted provided that the following conditions are |
| 9 | met: |
| 10 | * Redistributions of source code must retain the above copyright |
| 11 | notice, this list of conditions and the following disclaimer. |
| 12 | * Redistributions in binary form must reproduce the above |
| 13 | copyright notice, this list of conditions and the following |
| 14 | disclaimer in the documentation and/or other materials provided |
| 15 | with the distribution. |
| 16 | * Neither the name of The Linux Foundation nor the names of its |
| 17 | contributors, nor the name "Laurence Lundblade" may be used to |
| 18 | endorse or promote products derived from this software without |
| 19 | specific prior written permission. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 20 | |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 21 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 22 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 25 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 30 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 31 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 32 | =============================================================================*/ |
Laurence Lundblade | 624405d | 2018-09-18 20:10:47 -0700 | [diff] [blame] | 33 | |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 34 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 35 | #include "qcbor/qcbor_decode.h" |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 36 | #include "qcbor/qcbor_spiffy_decode.h" |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 37 | #include "ieee754.h" /* Does not use math.h */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 38 | |
| 39 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 40 | |
| 41 | #include <math.h> /* For isnan(), llround(), llroudf(), round(), roundf(), |
| 42 | * pow(), exp2() |
| 43 | */ |
| 44 | #include <fenv.h> /* feclearexcept(), fetestexcept() */ |
| 45 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 46 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 47 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 48 | |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 49 | #if (defined(__GNUC__) && !defined(__clang__)) |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 50 | /* |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 51 | * This is how the -Wmaybe-uninitialized compiler warning is |
| 52 | * handled. It can’t be ignored because some version of gcc enable it |
| 53 | * with -Wall which is a common and useful gcc warning option. It also |
| 54 | * can’t be ignored because it is the goal of QCBOR to compile clean |
| 55 | * out of the box in all environments. |
| 56 | * |
| 57 | * The big problem with -Wmaybe-uninitialized is that it generates |
| 58 | * false positives. It complains things are uninitialized when they |
| 59 | * are not. This is because it is not a thorough static analyzer. This |
| 60 | * is why “maybe” is in its name. The problem is it is just not |
| 61 | * thorough enough to understand all the code (and someone saw fit to |
| 62 | * put it in gcc and worse to enable it with -Wall). |
| 63 | * |
| 64 | * One solution would be to change the code so -Wmaybe-uninitialized |
| 65 | * doesn’t get confused, for example adding an unnecessary extra |
| 66 | * initialization to zero. (If variables were truly uninitialized, the |
| 67 | * correct path is to understand the code thoroughly and set them to |
| 68 | * the correct value at the correct time; in essence this is already |
| 69 | * done; -Wmaybe-uninitialized just can’t tell). This path is not |
| 70 | * taken because it makes the code bigger and is kind of the tail |
| 71 | * wagging the dog. |
| 72 | * |
| 73 | * The solution here is to just use a pragma to disable it for the |
| 74 | * whole file. Disabling it for each line makes the code fairly ugly |
| 75 | * requiring #pragma to push, pop and ignore. Another reason is the |
| 76 | * warnings issues vary by version of gcc and which optimization |
| 77 | * optimizations are selected. Another reason is that compilers other |
| 78 | * than gcc don’t have -Wmaybe-uninitialized. |
| 79 | * |
| 80 | * One may ask how to be sure these warnings are false positives and |
| 81 | * not real issues. 1) The code has been read carefully to check. 2) |
| 82 | * Testing is pretty thorough. 3) This code has been run through |
| 83 | * thorough high-quality static analyzers. |
| 84 | * |
| 85 | * In particularly, most of the warnings are about |
| 86 | * Item.Item->uDataType being uninitialized. QCBORDecode_GetNext() |
| 87 | * *always* sets this value and test case confirm |
| 88 | * this. -Wmaybe-uninitialized just can't tell. |
| 89 | * |
| 90 | * https://stackoverflow.com/questions/5080848/disable-gcc-may-be-used-uninitialized-on-a-particular-variable |
| 91 | */ |
| 92 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 93 | #endif |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 94 | |
| 95 | |
| 96 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 97 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 98 | #define SIZEOF_C_ARRAY(array,type) (sizeof(array)/sizeof(type)) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 99 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 100 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 101 | |
| 102 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 103 | static bool |
| 104 | QCBORItem_IsMapOrArray(const QCBORItem Item) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 105 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 106 | const uint8_t uDataType = Item.uDataType; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 107 | return uDataType == QCBOR_TYPE_MAP || |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 108 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 109 | uDataType == QCBOR_TYPE_MAP_AS_ARRAY || |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 110 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 111 | uDataType == QCBOR_TYPE_ARRAY; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 114 | static bool |
| 115 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(const QCBORItem Item) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 116 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 117 | if(!QCBORItem_IsMapOrArray(Item)){ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 121 | if(Item.val.uCount != 0) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | return true; |
| 125 | } |
| 126 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 127 | static bool |
| 128 | QCBORItem_IsIndefiniteLengthMapOrArray(const QCBORItem Item) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 129 | { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 130 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 131 | if(!QCBORItem_IsMapOrArray(Item)){ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 135 | if(Item.val.uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | return true; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 139 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 140 | (void)Item; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 141 | return false; |
| 142 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 145 | /* Return true if the labels in Item1 and Item2 are the same. |
| 146 | Works only for integer and string labels. Returns false |
| 147 | for any other type. */ |
| 148 | static bool |
| 149 | QCBORItem_MatchLabel(const QCBORItem Item1, const QCBORItem Item2) |
| 150 | { |
| 151 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 152 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 153 | return true; |
| 154 | } |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 155 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 156 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
| 157 | if(Item2.uLabelType == QCBOR_TYPE_TEXT_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 158 | return true; |
| 159 | } |
| 160 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
| 161 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 162 | return true; |
| 163 | } |
| 164 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 165 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 166 | return true; |
| 167 | } |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 168 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* Other label types are never matched */ |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | Returns true if Item1 and Item2 are the same type |
| 178 | or if either are of QCBOR_TYPE_ANY. |
| 179 | */ |
| 180 | static bool |
| 181 | QCBORItem_MatchType(const QCBORItem Item1, const QCBORItem Item2) |
| 182 | { |
| 183 | if(Item1.uDataType == Item2.uDataType) { |
| 184 | return true; |
| 185 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
| 186 | return true; |
| 187 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
| 188 | return true; |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 193 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 194 | /*=========================================================================== |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 195 | DecodeNesting -- Tracking array/map/sequence/bstr-wrapped nesting |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 196 | ===========================================================================*/ |
| 197 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 198 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 199 | * See comments about and typedef of QCBORDecodeNesting in qcbor_private.h, |
| 200 | * the data structure all these functions work on. |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 201 | */ |
| 202 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 203 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 204 | static uint8_t |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 205 | DecodeNesting_GetCurrentLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 206 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 207 | const ptrdiff_t nLevel = pNesting->pCurrent - &(pNesting->pLevels[0]); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 208 | /* Limit in DecodeNesting_Descend against more than |
| 209 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 210 | */ |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 211 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 214 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 215 | static uint8_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 216 | DecodeNesting_GetBoundedModeLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 217 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 218 | const ptrdiff_t nLevel = pNesting->pCurrentBounded - &(pNesting->pLevels[0]); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 219 | /* Limit in DecodeNesting_Descend against more than |
| 220 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 221 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 222 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 225 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 226 | static uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 227 | DecodeNesting_GetMapOrArrayStart(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 228 | { |
| 229 | return pNesting->pCurrentBounded->u.ma.uStartOffset; |
| 230 | } |
| 231 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 232 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 233 | static bool |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 234 | DecodeNesting_IsBoundedEmpty(const QCBORDecodeNesting *pNesting) |
| 235 | { |
| 236 | if(pNesting->pCurrentBounded->u.ma.uCountCursor == QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 237 | return true; |
| 238 | } else { |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 244 | static bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 245 | DecodeNesting_IsCurrentAtTop(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 246 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 247 | if(pNesting->pCurrent == &(pNesting->pLevels[0])) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 248 | return true; |
| 249 | } else { |
| 250 | return false; |
| 251 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 254 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 255 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 256 | DecodeNesting_IsCurrentDefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 257 | { |
| 258 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 259 | /* Not a map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 260 | return false; |
| 261 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 262 | |
| 263 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 264 | if(pNesting->pCurrent->u.ma.uCountTotal == QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 265 | /* Is indefinite */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 266 | return false; |
| 267 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 268 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 269 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 270 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 271 | /* All checks passed; is a definte length map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 272 | return true; |
| 273 | } |
| 274 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 275 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 276 | DecodeNesting_IsCurrentBstrWrapped(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 277 | { |
| 278 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 279 | /* is a byte string */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 280 | return true; |
| 281 | } |
| 282 | return false; |
| 283 | } |
| 284 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 285 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 286 | static bool |
| 287 | DecodeNesting_IsCurrentBounded(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 288 | { |
| 289 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
| 290 | return true; |
| 291 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 292 | if(pNesting->pCurrent->u.ma.uStartOffset != QCBOR_NON_BOUNDED_OFFSET) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 293 | return true; |
| 294 | } |
| 295 | return false; |
| 296 | } |
| 297 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 298 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 299 | static void |
| 300 | DecodeNesting_SetMapOrArrayBoundedMode(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uStart) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 301 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 302 | /* Should be only called on maps and arrays */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 303 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 304 | * DecodeNesting_EnterBoundedMode() checks to be sure uStart is not |
| 305 | * larger than DecodeNesting_EnterBoundedMode which keeps it less than |
| 306 | * uin32_t so the cast is safe. |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 307 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 308 | pNesting->pCurrent->u.ma.uStartOffset = (uint32_t)uStart; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 309 | |
| 310 | if(bIsEmpty) { |
| 311 | pNesting->pCurrent->u.ma.uCountCursor = QCBOR_COUNT_INDICATES_ZERO_LENGTH; |
| 312 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 315 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 316 | static void |
| 317 | DecodeNesting_ClearBoundedMode(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 318 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 319 | pNesting->pCurrent->u.ma.uStartOffset = QCBOR_NON_BOUNDED_OFFSET; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 322 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 323 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 324 | DecodeNesting_IsAtEndOfBoundedLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 325 | { |
| 326 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 327 | /* No bounded map or array set up */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 328 | return false; |
| 329 | } |
| 330 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 331 | /* Not a map or array; end of those is by byte count */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 332 | return false; |
| 333 | } |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 334 | if(!DecodeNesting_IsCurrentBounded(pNesting)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 335 | /* In a traveral at a level deeper than the bounded level */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 336 | return false; |
| 337 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 338 | /* Works for both definite- and indefinitelength maps/arrays */ |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 339 | if(pNesting->pCurrentBounded->u.ma.uCountCursor != 0 && |
| 340 | pNesting->pCurrentBounded->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 341 | /* Count is not zero, still unconsumed item */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 342 | return false; |
| 343 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 344 | /* All checks passed, got to the end of an array or map*/ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 345 | return true; |
| 346 | } |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 347 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 348 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 349 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 350 | DecodeNesting_IsEndOfDefiniteLengthMapOrArray(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 351 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 352 | /* Must only be called on map / array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 353 | if(pNesting->pCurrent->u.ma.uCountCursor == 0) { |
| 354 | return true; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 355 | } else { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 356 | return false; |
| 357 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 360 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 361 | static bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 362 | DecodeNesting_IsCurrentTypeMap(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 363 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 364 | if(pNesting->pCurrent->uLevelType == CBOR_MAJOR_TYPE_MAP) { |
| 365 | return true; |
| 366 | } else { |
| 367 | return false; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 368 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 371 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 372 | static bool |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 373 | DecodeNesting_IsBoundedType(const QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 374 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 375 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 376 | return false; |
| 377 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 378 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 379 | uint8_t uItemDataType = pNesting->pCurrentBounded->uLevelType; |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 380 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 381 | if(uItemDataType == QCBOR_TYPE_MAP_AS_ARRAY) { |
| 382 | uItemDataType = QCBOR_TYPE_ARRAY; |
| 383 | } |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 384 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 385 | |
| 386 | if(uItemDataType != uType) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 387 | return false; |
| 388 | } |
| 389 | |
| 390 | return true; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 393 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 394 | static void |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 395 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 396 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 397 | /* Only call on a definite-length array / map */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 398 | pNesting->pCurrent->u.ma.uCountCursor--; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 399 | } |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 400 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 401 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 402 | static void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 403 | DecodeNesting_ReverseDecrement(QCBORDecodeNesting *pNesting) |
| 404 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 405 | /* Only call on a definite-length array / map */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 406 | pNesting->pCurrent->u.ma.uCountCursor++; |
| 407 | } |
| 408 | |
| 409 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 410 | static void |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 411 | DecodeNesting_Ascend(QCBORDecodeNesting *pNesting) |
| 412 | { |
| 413 | pNesting->pCurrent--; |
| 414 | } |
| 415 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 416 | |
| 417 | static QCBORError |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 418 | DecodeNesting_Descend(QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 419 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 420 | /* Error out if nesting is too deep */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 421 | if(pNesting->pCurrent >= &(pNesting->pLevels[QCBOR_MAX_ARRAY_NESTING])) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 422 | return QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 425 | /* The actual descend */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 426 | pNesting->pCurrent++; |
| 427 | |
| 428 | pNesting->pCurrent->uLevelType = uType; |
| 429 | |
| 430 | return QCBOR_SUCCESS; |
| 431 | } |
| 432 | |
| 433 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 434 | static QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 435 | DecodeNesting_EnterBoundedMapOrArray(QCBORDecodeNesting *pNesting, |
| 436 | bool bIsEmpty, |
| 437 | size_t uOffset) |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 438 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 439 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 440 | * Should only be called on map/array. |
| 441 | * |
| 442 | * Have descended into this before this is called. The job here is |
| 443 | * just to mark it in bounded mode. |
| 444 | * |
| 445 | * Check against QCBOR_MAX_DECODE_INPUT_SIZE make sure that |
| 446 | * uOffset doesn't collide with QCBOR_NON_BOUNDED_OFFSET. |
| 447 | * |
| 448 | * Cast of uOffset to uint32_t for cases where SIZE_MAX < UINT32_MAX. |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 449 | */ |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 450 | if((uint32_t)uOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 451 | return QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 454 | pNesting->pCurrentBounded = pNesting->pCurrent; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 455 | |
| 456 | DecodeNesting_SetMapOrArrayBoundedMode(pNesting, bIsEmpty, uOffset); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 457 | |
| 458 | return QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 461 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 462 | static QCBORError |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 463 | DecodeNesting_DescendMapOrArray(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 464 | uint8_t uQCBORType, |
| 465 | uint64_t uCount) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 466 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 467 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 468 | |
| 469 | if(uCount == 0) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 470 | /* Nothing to do for empty definite-length arrays. They are just are |
| 471 | * effectively the same as an item that is not a map or array. |
| 472 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 473 | goto Done; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 474 | /* Empty indefinite-length maps and arrays are handled elsewhere */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 477 | /* Error out if arrays is too long to handle */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 478 | if(uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH && |
| 479 | uCount > QCBOR_MAX_ITEMS_IN_ARRAY) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 480 | uError = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 481 | goto Done; |
| 482 | } |
| 483 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 484 | uError = DecodeNesting_Descend(pNesting, uQCBORType); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 485 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 486 | goto Done; |
| 487 | } |
| 488 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 489 | /* Fill in the new map/array level. Check above makes casts OK. */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 490 | pNesting->pCurrent->u.ma.uCountCursor = (uint16_t)uCount; |
| 491 | pNesting->pCurrent->u.ma.uCountTotal = (uint16_t)uCount; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 492 | |
| 493 | DecodeNesting_ClearBoundedMode(pNesting); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 494 | |
| 495 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 496 | return uError;; |
| 497 | } |
| 498 | |
| 499 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 500 | static void |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 501 | DecodeNesting_LevelUpCurrent(QCBORDecodeNesting *pNesting) |
| 502 | { |
| 503 | pNesting->pCurrent = pNesting->pCurrentBounded - 1; |
| 504 | } |
| 505 | |
| 506 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 507 | static void |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 508 | DecodeNesting_LevelUpBounded(QCBORDecodeNesting *pNesting) |
| 509 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 510 | while(pNesting->pCurrentBounded != &(pNesting->pLevels[0])) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 511 | pNesting->pCurrentBounded--; |
| 512 | if(DecodeNesting_IsCurrentBounded(pNesting)) { |
| 513 | break; |
| 514 | } |
| 515 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 518 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 519 | static void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 520 | DecodeNesting_SetCurrentToBoundedLevel(QCBORDecodeNesting *pNesting) |
| 521 | { |
| 522 | pNesting->pCurrent = pNesting->pCurrentBounded; |
| 523 | } |
| 524 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 525 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 526 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 527 | DecodeNesting_DescendIntoBstrWrapped(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 528 | uint32_t uEndOffset, |
| 529 | uint32_t uStartOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 530 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 531 | QCBORError uError; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 532 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 533 | uError = DecodeNesting_Descend(pNesting, QCBOR_TYPE_BYTE_STRING); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 534 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 535 | goto Done; |
| 536 | } |
| 537 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 538 | /* Fill in the new byte string level */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 539 | pNesting->pCurrent->u.bs.uSavedEndOffset = uEndOffset; |
| 540 | pNesting->pCurrent->u.bs.uBstrStartOffset = uStartOffset; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 541 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 542 | /* Bstr wrapped levels are always bounded */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 543 | pNesting->pCurrentBounded = pNesting->pCurrent; |
| 544 | |
| 545 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 546 | return uError;; |
| 547 | } |
| 548 | |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 549 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 550 | static void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 551 | DecodeNesting_ZeroMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 552 | { |
| 553 | pNesting->pCurrent->u.ma.uCountCursor = 0; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 556 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 557 | static void |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 558 | DecodeNesting_ResetMapOrArrayCount(QCBORDecodeNesting *pNesting) |
| 559 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 560 | if(pNesting->pCurrent->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 561 | pNesting->pCurrentBounded->u.ma.uCountCursor = pNesting->pCurrentBounded->u.ma.uCountTotal; |
| 562 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 566 | static void |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 567 | DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 568 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 569 | /* Assumes that *pNesting has been zero'd before this call. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 570 | pNesting->pLevels[0].uLevelType = QCBOR_TYPE_BYTE_STRING; |
| 571 | pNesting->pCurrent = &(pNesting->pLevels[0]); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 575 | static void |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 576 | DecodeNesting_PrepareForMapSearch(QCBORDecodeNesting *pNesting, |
| 577 | QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 578 | { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 579 | *pSave = *pNesting; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 582 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 583 | static void |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 584 | DecodeNesting_RestoreFromMapSearch(QCBORDecodeNesting *pNesting, |
| 585 | const QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 586 | { |
| 587 | *pNesting = *pSave; |
| 588 | } |
| 589 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 590 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 591 | static uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 592 | DecodeNesting_GetPreviousBoundedEnd(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 593 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 594 | return pMe->pCurrentBounded->u.bs.uSavedEndOffset; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 598 | |
| 599 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 600 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 601 | /*=========================================================================== |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 602 | QCBORStringAllocate -- STRING ALLOCATOR INVOCATION |
| 603 | |
| 604 | The following four functions are pretty wrappers for invocation of |
| 605 | the string allocator supplied by the caller. |
| 606 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 607 | ===========================================================================*/ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 608 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 609 | static void |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 610 | StringAllocator_Free(const QCBORInternalAllocator *pMe, const void *pMem) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 611 | { |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 612 | /* This cast to uintptr_t suppresses the "-Wcast-qual" warnings. |
| 613 | * This is the one place where the const needs to be cast away so const can |
| 614 | * be use in the rest of the code. |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 615 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 616 | (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)(uintptr_t)pMem, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 619 | // StringAllocator_Reallocate called with pMem NULL is |
| 620 | // equal to StringAllocator_Allocate() |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 621 | static UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 622 | StringAllocator_Reallocate(const QCBORInternalAllocator *pMe, |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 623 | const void *pMem, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 624 | size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 625 | { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 626 | /* See comment in StringAllocator_Free() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 627 | return (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)(uintptr_t)pMem, uSize); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 628 | } |
| 629 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 630 | static UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 631 | StringAllocator_Allocate(const QCBORInternalAllocator *pMe, size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 632 | { |
| 633 | return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize); |
| 634 | } |
| 635 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 636 | static void |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 637 | StringAllocator_Destruct(const QCBORInternalAllocator *pMe) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 638 | { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 639 | /* See comment in StringAllocator_Free() */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 640 | if(pMe->pfAllocator) { |
| 641 | (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0); |
| 642 | } |
| 643 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 644 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 645 | |
| 646 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 647 | |
| 648 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 649 | /*=========================================================================== |
| 650 | QCBORDecode -- The main implementation of CBOR decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 651 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 652 | See qcbor/qcbor_decode.h for definition of the object |
| 653 | used here: QCBORDecodeContext |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 654 | ===========================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 655 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 656 | * Public function, see header file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 657 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 658 | void |
| 659 | QCBORDecode_Init(QCBORDecodeContext *pMe, |
| 660 | UsefulBufC EncodedCBOR, |
| 661 | QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 662 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 663 | memset(pMe, 0, sizeof(QCBORDecodeContext)); |
| 664 | UsefulInputBuf_Init(&(pMe->InBuf), EncodedCBOR); |
| 665 | /* Don't bother with error check on decode mode. If a bad value is |
| 666 | * passed it will just act as if the default normal mode of 0 was set. |
| 667 | */ |
| 668 | pMe->uDecodeMode = (uint8_t)nDecodeMode; |
| 669 | DecodeNesting_Init(&(pMe->nesting)); |
| 670 | |
| 671 | /* Inialize me->auMappedTags to CBOR_TAG_INVALID16. See |
| 672 | * GetNext_TaggedItem() and MapTagNumber(). */ |
| 673 | memset(pMe->auMappedTags, 0xff, sizeof(pMe->auMappedTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 677 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 678 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 679 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 680 | * Public function, see header file |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 681 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 682 | void |
| 683 | QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe, |
| 684 | QCBORStringAllocate pfAllocateFunction, |
| 685 | void *pAllocateContext, |
| 686 | bool bAllStrings) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 687 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 688 | pMe->StringAllocator.pfAllocator = pfAllocateFunction; |
| 689 | pMe->StringAllocator.pAllocateCxt = pAllocateContext; |
| 690 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 691 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 692 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 693 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 694 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 695 | |
| 696 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 697 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 698 | * Deprecated public function, see header file |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 699 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 700 | void |
| 701 | QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *pMe, |
| 702 | const QCBORTagListIn *pTagList) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 703 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 704 | /* This does nothing now. It is retained for backwards compatibility */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 705 | (void)pMe; |
| 706 | (void)pTagList; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 707 | } |
| 708 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 709 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 710 | |
| 711 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 712 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 713 | * Decoding items is done in six layers, one calling the next one |
| 714 | * down. If a layer has no work to do for a particular item, it |
| 715 | * returns quickly. |
| 716 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 717 | * 1. QCBORDecode_Private_GetNextTagContent - The top layer processes |
| 718 | * tagged data items, turning them into the local C representation. |
| 719 | * For the most simple it is just associating a QCBOR_TYPE with the |
| 720 | * data. For the complex ones that an aggregate of data items, there |
| 721 | * is some further decoding and some limited recursion. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 722 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 723 | * 2. QCBORDecode_Private_GetNextMapOrArray - This manages the |
| 724 | * beginnings and ends of maps and arrays. It tracks descending into |
| 725 | * and ascending out of maps/arrays. It processes breaks that |
| 726 | * terminate indefinite-length maps and arrays. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 727 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 728 | * 3. QCBORDecode_Private_GetNextMapEntry - This handles the combining |
| 729 | * of two items, the label and the data, that make up a map entry. It |
| 730 | * only does work on maps. It combines the label and data items into |
| 731 | * one labeled item. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 732 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 733 | * 4. QCBORDecode_Private_GetNextTagNumber - This decodes type 6 tag |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 734 | * numbers. It turns the tag numbers into bit flags associated with |
| 735 | * the data item. No actual decoding of the contents of the tag is |
| 736 | * performed here. |
| 737 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 738 | * 5. QCBORDecode_Private_GetNextFullString - This assembles the |
| 739 | * sub-items that make up an indefinite-length string into one string |
| 740 | * item. It uses the string allocator to create contiguous space for |
| 741 | * the item. It processes all breaks that are part of |
| 742 | * indefinite-length strings. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 743 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 744 | * 6. QCBOR_Private_DecodeAtomicDataItem - This decodes the atomic |
| 745 | * data items in CBOR. Each atomic data item has a "major type", an |
| 746 | * integer "argument" and optionally some content. For text and byte |
| 747 | * strings, the content is the bytes that make up the string. These |
| 748 | * are the smallest data items that are considered to be well-formed. |
| 749 | * The content may also be other data items in the case of aggregate |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 750 | * types. They are not handled in this layer. |
| 751 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 752 | * This uses about 350 bytes of stack. This number comes from |
| 753 | * instrumenting (printf address of stack variables) the code on x86 |
| 754 | * compiled for size optimization. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 755 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 756 | |
| 757 | |
| 758 | /* |
| 759 | * Note about use of int and unsigned variables. |
| 760 | * |
| 761 | * See http://www.unix.org/whitepapers/64bit.html for reasons int is |
| 762 | * used carefully here, and in particular why it isn't used in the |
| 763 | * public interface. Also see |
| 764 | * https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 765 | * |
| 766 | * Int is used for values that need less than 16-bits and would be |
| 767 | * subject to integer promotion and result in complaining from static |
| 768 | * analyzers. |
| 769 | */ |
| 770 | |
| 771 | |
| 772 | /** |
| 773 | * @brief Decode the CBOR head, the type and argument. |
| 774 | * |
| 775 | * @param[in] pUInBuf The input buffer to read from. |
| 776 | * @param[out] pnMajorType The decoded major type. |
| 777 | * @param[out] puArgument The decoded argument. |
| 778 | * @param[out] pnAdditionalInfo The decoded Lower 5 bits of initial byte. |
| 779 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 780 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved features |
| 781 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 782 | * |
| 783 | * This decodes the CBOR "head" that every CBOR data item has. See |
| 784 | * longer explaination of the head in documentation for |
| 785 | * QCBOREncode_EncodeHead(). |
| 786 | * |
| 787 | * This does the network->host byte order conversion. The conversion |
| 788 | * here also results in the conversion for floats in addition to that |
| 789 | * for lengths, tags and integer values. |
| 790 | * |
| 791 | * The int type is preferred to uint8_t for some variables as this |
| 792 | * avoids integer promotions, can reduce code size and makes static |
| 793 | * analyzers happier. |
| 794 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 795 | static QCBORError |
| 796 | QCBOR_Private_DecodeHead(UsefulInputBuf *pUInBuf, |
| 797 | int *pnMajorType, |
| 798 | uint64_t *puArgument, |
| 799 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 800 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 801 | QCBORError uReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 802 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 803 | /* Get the initial byte that every CBOR data item has and break it |
| 804 | * down. */ |
| 805 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 806 | const int nTmpMajorType = nInitialByte >> 5; |
| 807 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 808 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 809 | /* Where the argument accumulates */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 810 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 811 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 812 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 813 | /* Need to get 1,2,4 or 8 additional argument bytes. Map |
| 814 | * LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length. |
| 815 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 816 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 817 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 818 | /* Loop getting all the bytes in the argument */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 819 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 820 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 821 | /* This shift and add gives the endian conversion. */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 822 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 823 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 824 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 825 | /* The reserved and thus-far unused additional info values */ |
| 826 | uReturn = QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 827 | goto Done; |
| 828 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 829 | /* Less than 24, additional info is argument or 31, an |
| 830 | * indefinite-length. No more bytes to get. |
| 831 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 832 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 833 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 834 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 835 | if(UsefulInputBuf_GetError(pUInBuf)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 836 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 837 | goto Done; |
| 838 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 839 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 840 | /* All successful if arrived here. */ |
| 841 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 842 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 843 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 844 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 845 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 846 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 847 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 850 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 851 | /** |
| 852 | * @brief Decode integer types, major types 0 and 1. |
| 853 | * |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 854 | * @param[in] nMajorType The CBOR major type (0 or 1). |
| 855 | * @param[in] uArgument The argument from the head. |
| 856 | * @param[in] nAdditionalInfo So it can be error-checked. |
| 857 | * @param[out] pDecodedItem The filled in decoded item. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 858 | * |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 859 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered. |
| 860 | * @retval QCBOR_ERR_BAD_INT nAdditionalInfo indicated indefinte. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 861 | * |
| 862 | * Must only be called when major type is 0 or 1. |
| 863 | * |
| 864 | * CBOR doesn't explicitly specify two's compliment for integers but |
| 865 | * all CPUs use it these days and the test vectors in the RFC are |
| 866 | * so. All integers in the CBOR structure are positive and the major |
| 867 | * type indicates positive or negative. CBOR can express positive |
| 868 | * integers up to 2^x - 1 where x is the number of bits and negative |
| 869 | * integers down to 2^x. Note that negative numbers can be one more |
| 870 | * away from zero than positive. Stdint, as far as I can tell, uses |
| 871 | * two's compliment to represent negative integers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 872 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 873 | static QCBORError |
| 874 | QCBOR_Private_DecodeInteger(const int nMajorType, |
| 875 | const uint64_t uArgument, |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 876 | const int nAdditionalInfo, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 877 | QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 878 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 879 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 880 | |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 881 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 882 | uReturn = QCBOR_ERR_BAD_INT; |
| 883 | goto Done; |
| 884 | } |
| 885 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 886 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 887 | if (uArgument <= INT64_MAX) { |
| 888 | pDecodedItem->val.int64 = (int64_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 889 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 890 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 891 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 892 | pDecodedItem->val.uint64 = uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 893 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 894 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 895 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 896 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 897 | if(uArgument <= INT64_MAX) { |
| 898 | /* CBOR's representation of negative numbers lines up with |
| 899 | * the two-compliment representation. A negative integer has |
| 900 | * one more in range than a positive integer. INT64_MIN is |
| 901 | * equal to (-INT64_MAX) - 1. |
| 902 | */ |
| 903 | pDecodedItem->val.int64 = (-(int64_t)uArgument) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 904 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 905 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 906 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 907 | /* C can't represent a negative integer in this range so it |
| 908 | * is an error. |
| 909 | */ |
| 910 | uReturn = QCBOR_ERR_INT_OVERFLOW; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 911 | } |
| 912 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 913 | |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 914 | Done: |
| 915 | return uReturn; |
| 916 | } |
| 917 | |
| 918 | |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 919 | /** |
| 920 | * @brief Decode text and byte strings |
| 921 | * |
| 922 | * @param[in] pMe Decoder context. |
| 923 | * @param[in] bAllocate Whether to allocate and copy string. |
| 924 | * @param[in] nMajorType Whether it is a byte or text string. |
| 925 | * @param[in] uStrLen The length of the string. |
| 926 | * @param[in] nAdditionalInfo Whether it is an indefinite-length string. |
| 927 | * @param[out] pDecodedItem The filled-in decoded item. |
| 928 | * |
| 929 | * @retval QCBOR_ERR_HIT_END Unexpected end of input. |
| 930 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 931 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 932 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Allocation requested, but no allocator |
| 933 | * |
| 934 | * This reads @c uStrlen bytes from the input and fills in @c |
| 935 | * pDecodedItem. If @c bAllocate is true, then memory for the string |
| 936 | * is allocated. |
| 937 | */ |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 938 | static QCBORError |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 939 | QCBOR_Private_DecodeString(QCBORDecodeContext *pMe, |
| 940 | const bool bAllocate, |
| 941 | const int nMajorType, |
| 942 | const uint64_t uStrLen, |
| 943 | const int nAdditionalInfo, |
| 944 | QCBORItem *pDecodedItem) |
| 945 | { |
| 946 | QCBORError uReturn = QCBOR_SUCCESS; |
| 947 | |
| 948 | /* ---- Figure out the major type ---- */ |
| 949 | #if CBOR_MAJOR_TYPE_BYTE_STRING + 4 != QCBOR_TYPE_BYTE_STRING |
| 950 | #error QCBOR_TYPE_BYTE_STRING not lined up with major type |
| 951 | #endif |
| 952 | |
| 953 | #if CBOR_MAJOR_TYPE_TEXT_STRING + 4 != QCBOR_TYPE_TEXT_STRING |
| 954 | #error QCBOR_TYPE_TEXT_STRING not lined up with major type |
| 955 | #endif |
| 956 | pDecodedItem->uDataType = (uint8_t)(nMajorType + 4); |
| 957 | |
| 958 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 959 | /* --- Just the head of an indefinite-length string --- */ |
| 960 | pDecodedItem->val.string = (UsefulBufC){NULL, QCBOR_STRING_LENGTH_INDEFINITE}; |
| 961 | |
| 962 | } else { |
| 963 | /* --- A definite-length string --- */ |
| 964 | /* --- (which might be a chunk of an indefinte-length string) --- */ |
| 965 | |
| 966 | /* CBOR lengths can be 64 bits, but size_t is not 64 bits on all |
| 967 | * CPUs. This check makes the casts to size_t below safe. |
| 968 | * |
| 969 | * The max is 4 bytes less than the largest sizeof() so this can be |
| 970 | * tested by putting a SIZE_MAX length in the CBOR test input (no |
| 971 | * one will care the limit on strings is 4 bytes shorter). |
| 972 | */ |
| 973 | if(uStrLen > SIZE_MAX-4) { |
| 974 | uReturn = QCBOR_ERR_STRING_TOO_LONG; |
| 975 | goto Done; |
| 976 | } |
| 977 | |
| 978 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(&(pMe->InBuf), (size_t)uStrLen); |
| 979 | if(UsefulBuf_IsNULLC(Bytes)) { |
| 980 | /* Failed to get the bytes for this string item */ |
| 981 | uReturn = QCBOR_ERR_HIT_END; |
| 982 | goto Done; |
| 983 | } |
| 984 | |
| 985 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 986 | if(bAllocate) { |
| 987 | /* --- Put string in allocated memory --- */ |
| 988 | |
| 989 | /* Note that this is not where allocation to coalesce |
| 990 | * indefinite-length strings is done. This is for when the |
| 991 | * caller has requested all strings be allocated. Disabling |
| 992 | * indefinite length strings also disables this allocate-all |
| 993 | * option. |
| 994 | */ |
| 995 | |
| 996 | if(pMe->StringAllocator.pfAllocator == NULL) { |
| 997 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 998 | goto Done; |
| 999 | } |
| 1000 | UsefulBuf NewMem = StringAllocator_Allocate(&(pMe->StringAllocator), (size_t)uStrLen); |
| 1001 | if(UsefulBuf_IsNULL(NewMem)) { |
| 1002 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
| 1003 | goto Done; |
| 1004 | } |
| 1005 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
| 1006 | pDecodedItem->uDataAlloc = 1; |
| 1007 | goto Done; |
| 1008 | } |
| 1009 | #else |
| 1010 | (void)bAllocate; |
| 1011 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1012 | |
| 1013 | /* --- Normal case with no string allocator --- */ |
| 1014 | pDecodedItem->val.string = Bytes; |
| 1015 | } |
| 1016 | |
| 1017 | Done: |
| 1018 | return uReturn; |
| 1019 | } |
| 1020 | |
| 1021 | |
| 1022 | /** |
| 1023 | * @brief Decode array or map. |
| 1024 | * |
| 1025 | * @param[in] uMode Decoder mode. |
| 1026 | * @param[in] nMajorType Whether it is a byte or text string. |
| 1027 | * @param[in] uItemCount The length of the string. |
| 1028 | * @param[in] nAdditionalInfo Whether it is an indefinite-length. |
| 1029 | * @param[out] pDecodedItem The filled-in decoded item. |
| 1030 | * |
| 1031 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinites disabled. |
| 1032 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG Too many items in array/map. |
| 1033 | * |
| 1034 | * Not much to do for arrays and maps. Just the type item count (but a |
| 1035 | * little messy because of ifdefs for indefinite-lengths and |
| 1036 | * map-as-array decoding). |
| 1037 | * |
| 1038 | * This also does the bulk of the work for @ref |
| 1039 | * QCBOR_DECODE_MODE_MAP_AS_ARRAY, a special mode to handle |
| 1040 | * arbitrarily complex map labels. This ifdefs out with |
| 1041 | * QCBOR_DISABLE_NON_INTEGER_LABELS. |
| 1042 | */ |
| 1043 | static QCBORError |
| 1044 | QCBOR_Private_DecodeArrayOrMap(const uint8_t uMode, |
| 1045 | const int nMajorType, |
| 1046 | const uint64_t uItemCount, |
| 1047 | const int nAdditionalInfo, |
| 1048 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1049 | { |
| 1050 | QCBORError uReturn; |
| 1051 | |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1052 | /* ------ Sort out the data type ------ */ |
| 1053 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 1054 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 1055 | #endif |
| 1056 | |
| 1057 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 1058 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 1059 | #endif |
| 1060 | pDecodedItem->uDataType = (uint8_t)nMajorType; |
| 1061 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 1062 | if(uMode == QCBOR_DECODE_MODE_MAP_AS_ARRAY && nMajorType == QCBOR_TYPE_MAP) { |
| 1063 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
| 1064 | } |
| 1065 | #else |
| 1066 | (void)uMode; |
| 1067 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 1068 | |
| 1069 | uReturn = QCBOR_SUCCESS; |
| 1070 | |
| 1071 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1072 | /* ------ Indefinite-length array/map ----- */ |
| 1073 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 1074 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
| 1075 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1076 | uReturn = QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED; |
| 1077 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1078 | } else { |
| 1079 | |
| 1080 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 1081 | if(uMode == QCBOR_DECODE_MODE_MAP_AS_ARRAY && nMajorType == QCBOR_TYPE_MAP) { |
| 1082 | /* ------ Definite-length map as array ------ */ |
| 1083 | |
| 1084 | if(uItemCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
| 1085 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
| 1086 | } else { |
| 1087 | /* cast OK because of check above */ |
| 1088 | pDecodedItem->val.uCount = (uint16_t)uItemCount*2; |
| 1089 | } |
| 1090 | |
| 1091 | } else |
| 1092 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 1093 | { |
| 1094 | /* ------ Definite-length array/map ------ */ |
| 1095 | if(uItemCount > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 1096 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
| 1097 | } else { |
| 1098 | /* cast OK because of check above */ |
| 1099 | pDecodedItem->val.uCount = (uint16_t)uItemCount; |
| 1100 | } |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1101 | } |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | return uReturn; |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | /** |
| 1109 | * @brief Decode a tag number. |
| 1110 | * |
| 1111 | * @param[in] uTagNumber The length of the string. |
| 1112 | * @param[in] nAdditionalInfo So this can be error-checked. |
| 1113 | * @param[out] pDecodedItem The filled-in decoded item. |
| 1114 | * |
| 1115 | * @retval QCBOR_ERR_BAD_INT nAdditionalInfo is LEN_IS_INDEFINITE. |
| 1116 | * @retval QCBOR_ERR_TAGS_DISABLED QCBOR_DISABLE_TAGS is defined. |
| 1117 | * |
| 1118 | * Not much to do for tags, but fill in pDecodedItem and check for |
| 1119 | * error in nAdditionalInfo. |
| 1120 | */ |
| 1121 | static QCBORError |
| 1122 | QCBOR_Private_DecodeTag(const uint64_t uTagNumber, |
| 1123 | const int nAdditionalInfo, |
| 1124 | QCBORItem *pDecodedItem) |
| 1125 | { |
| 1126 | #ifndef QCBOR_DISABLE_TAGS |
| 1127 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1128 | return QCBOR_ERR_BAD_INT; |
| 1129 | } else { |
| 1130 | pDecodedItem->val.uTagV = uTagNumber; |
| 1131 | pDecodedItem->uDataType = QCBOR_TYPE_TAG; |
| 1132 | return QCBOR_SUCCESS; |
| 1133 | } |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1134 | #else /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | cfd8a2c | 2024-06-06 21:01:42 -0700 | [diff] [blame] | 1135 | (void)nAdditionalInfo; |
| 1136 | (void)uArgument; |
| 1137 | (void)pDecodedItem; |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1138 | return QCBOR_ERR_TAGS_DISABLED; |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1139 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1142 | |
| 1143 | /* Make sure #define value line up as DecodeSimple counts on this. */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1144 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 1145 | #error QCBOR_TYPE_FALSE macro value wrong |
| 1146 | #endif |
| 1147 | |
| 1148 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 1149 | #error QCBOR_TYPE_TRUE macro value wrong |
| 1150 | #endif |
| 1151 | |
| 1152 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 1153 | #error QCBOR_TYPE_NULL macro value wrong |
| 1154 | #endif |
| 1155 | |
| 1156 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 1157 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 1158 | #endif |
| 1159 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 1160 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 1161 | #error QCBOR_TYPE_BREAK macro value wrong |
| 1162 | #endif |
| 1163 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1164 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 1165 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 1166 | #endif |
| 1167 | |
| 1168 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 1169 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 1170 | #endif |
| 1171 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1172 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1173 | /** |
| 1174 | * @brief Decode major type 7 -- true, false, floating-point, break... |
| 1175 | * |
| 1176 | * @param[in] nAdditionalInfo The lower five bits from the initial byte. |
| 1177 | * @param[in] uArgument The argument from the head. |
| 1178 | * @param[out] pDecodedItem The filled in decoded item. |
| 1179 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1180 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1181 | * of half-precision disabled |
| 1182 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all float |
| 1183 | * decode is disabled. |
| 1184 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of simple |
| 1185 | * type in input. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1186 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1187 | static QCBORError |
| 1188 | QCBOR_Private_DecodeType7(const int nAdditionalInfo, |
| 1189 | const uint64_t uArgument, |
| 1190 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1191 | { |
| 1192 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1193 | |
| 1194 | /* uAdditionalInfo is 5 bits from the initial byte. Compile time |
| 1195 | * checks above make sure uAdditionalInfo values line up with |
| 1196 | * uDataType values. DecodeHead() never returns an AdditionalInfo |
| 1197 | * > 0x1f so cast is safe. |
| 1198 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1199 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1200 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1201 | switch(nAdditionalInfo) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1202 | /* No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they |
| 1203 | * are caught before this is called. |
| 1204 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1205 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1206 | case HALF_PREC_FLOAT: /* 25 */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 1207 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1208 | /* Half-precision is returned as a double. The cast to |
| 1209 | * uint16_t is safe because the encoded value was 16 bits. It |
| 1210 | * was widened to 64 bits to be passed in here. |
| 1211 | */ |
| 1212 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1213 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1214 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1215 | uReturn = FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 1216 | break; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1217 | case SINGLE_PREC_FLOAT: /* 26 */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1218 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1219 | /* Single precision is normally returned as a double since |
| 1220 | * double is widely supported, there is no loss of precision, |
| 1221 | * it makes it easy for the caller in most cases and it can |
| 1222 | * be converted back to single with no loss of precision |
| 1223 | * |
| 1224 | * The cast to uint32_t is safe because the encoded value was |
| 1225 | * 32 bits. It was widened to 64 bits to be passed in here. |
| 1226 | */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 1227 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1228 | const float f = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uArgument); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 1229 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1230 | /* In the normal case, use HW to convert float to |
| 1231 | * double. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 1232 | pDecodedItem->val.dfnum = (double)f; |
| 1233 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1234 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1235 | /* Use of float HW is disabled, return as a float. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 1236 | pDecodedItem->val.fnum = f; |
| 1237 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 1238 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1239 | /* IEEE754_FloatToDouble() could be used here to return as |
| 1240 | * a double, but it adds object code and most likely |
| 1241 | * anyone disabling FLOAT HW use doesn't care about floats |
| 1242 | * and wants to save object code. |
| 1243 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1244 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 1245 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1246 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 1247 | uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 1248 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 1249 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1250 | case DOUBLE_PREC_FLOAT: /* 27 */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1251 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1252 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1253 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1254 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 1255 | uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 1256 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1257 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1258 | case CBOR_SIMPLEV_FALSE: /* 20 */ |
| 1259 | case CBOR_SIMPLEV_TRUE: /* 21 */ |
| 1260 | case CBOR_SIMPLEV_NULL: /* 22 */ |
| 1261 | case CBOR_SIMPLEV_UNDEF: /* 23 */ |
| 1262 | case CBOR_SIMPLE_BREAK: /* 31 */ |
| 1263 | break; /* nothing to do */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1264 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1265 | case CBOR_SIMPLEV_ONEBYTE: /* 24 */ |
| 1266 | if(uArgument <= CBOR_SIMPLE_BREAK) { |
| 1267 | /* This takes out f8 00 ... f8 1f which should be encoded |
| 1268 | * as e0 … f7 |
| 1269 | */ |
| 1270 | uReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1271 | goto Done; |
| 1272 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 1273 | /* FALLTHROUGH */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1274 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1275 | default: /* 0-19 */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1276 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1277 | /* DecodeHead() will make uArgument equal to |
| 1278 | * nAdditionalInfo when nAdditionalInfo is < 24. This cast is |
| 1279 | * safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 1280 | * the double/float cases above |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1281 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1282 | pDecodedItem->val.uSimple = (uint8_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1283 | break; |
| 1284 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1285 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1286 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1287 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1291 | /** |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1292 | * @brief Decode a single primitive data item (decode layer 6). |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1293 | * |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1294 | * @param[in] pMe Decoder context. |
| 1295 | * @param[in] bAllocateStrings If true, use allocator for strings. |
| 1296 | * @param[out] pDecodedItem The filled-in decoded item. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1297 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1298 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1299 | * features |
| 1300 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1301 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1302 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1303 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1304 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Allocation requested, but no allocator |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1305 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1306 | * of half-precision disabled |
| 1307 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1308 | * float decode is disabled. |
| 1309 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1310 | * simple type in input. |
| 1311 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1312 | * in input, but indefinite |
| 1313 | * lengths disabled. |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1314 | * @retval QCBOR_ERR_BAD_INT nAdditionalInfo indicated indefinte. |
| 1315 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG Too many items in array/map. |
| 1316 | * @retval QCBOR_ERR_TAGS_DISABLED QCBOR_DISABLE_TAGS is defined. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1317 | * |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1318 | * This decodes the most primitive / atomic data item. It does no |
| 1319 | * combining of data items. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1320 | */ |
| 1321 | static QCBORError |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1322 | QCBOR_Private_DecodeAtomicDataItem(QCBORDecodeContext *pMe, |
| 1323 | const bool bAllocateStrings, |
| 1324 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1325 | { |
| 1326 | QCBORError uReturn; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1327 | int nMajorType = 0; |
| 1328 | uint64_t uArgument = 0; |
| 1329 | int nAdditionalInfo = 0; |
| 1330 | |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1331 | /* Decode the "head" that every CBOR item has into the major type, |
| 1332 | * length and the additional info. |
| 1333 | */ |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1334 | uReturn = QCBOR_Private_DecodeHead(&(pMe->InBuf), &nMajorType, &uArgument, &nAdditionalInfo); |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1335 | if(uReturn != QCBOR_SUCCESS) { |
| 1336 | return uReturn; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1337 | } |
| 1338 | |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1339 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1340 | |
| 1341 | /* All the functions below get inlined by the optimizer. This code |
| 1342 | * is easier to read with them all being similar functions, even if |
| 1343 | * some functions don't do much. |
| 1344 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1345 | switch (nMajorType) { |
| 1346 | case CBOR_MAJOR_TYPE_POSITIVE_INT: /* Major type 0 */ |
| 1347 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: /* Major type 1 */ |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1348 | return QCBOR_Private_DecodeInteger(nMajorType, uArgument, nAdditionalInfo, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1349 | break; |
| 1350 | |
| 1351 | case CBOR_MAJOR_TYPE_BYTE_STRING: /* Major type 2 */ |
| 1352 | case CBOR_MAJOR_TYPE_TEXT_STRING: /* Major type 3 */ |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1353 | return QCBOR_Private_DecodeString(pMe, bAllocateStrings, nMajorType, uArgument, nAdditionalInfo, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1354 | break; |
| 1355 | |
| 1356 | case CBOR_MAJOR_TYPE_ARRAY: /* Major type 4 */ |
| 1357 | case CBOR_MAJOR_TYPE_MAP: /* Major type 5 */ |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1358 | return QCBOR_Private_DecodeArrayOrMap(pMe->uDecodeMode, nMajorType, uArgument, nAdditionalInfo, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1359 | break; |
| 1360 | |
| 1361 | case CBOR_MAJOR_TYPE_TAG: /* Major type 6, tag numbers */ |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1362 | return QCBOR_Private_DecodeTag(uArgument, nAdditionalInfo, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1363 | break; |
| 1364 | |
| 1365 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 1366 | /* Major type 7: float, double, true, false, null... */ |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1367 | return QCBOR_Private_DecodeType7(nAdditionalInfo, uArgument, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1368 | break; |
| 1369 | |
| 1370 | default: |
| 1371 | /* Never happens because DecodeHead() should never return > 7 */ |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 1372 | return QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1373 | break; |
| 1374 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | |
| 1378 | /** |
| 1379 | * @brief Process indefinite-length strings (decode layer 5). |
| 1380 | * |
| 1381 | * @param[in] pMe Decoder context |
| 1382 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1383 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1384 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1385 | * features |
| 1386 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1387 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1388 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1389 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1390 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1391 | * of half-precision disabled |
| 1392 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1393 | * float decode is disabled. |
| 1394 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1395 | * simple type in input. |
| 1396 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1397 | * in input, but indefinite |
| 1398 | * lengths disabled. |
| 1399 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 1400 | * but no string allocator. |
| 1401 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 1402 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 1403 | * input, but indefinite-length |
| 1404 | * strings are disabled. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1405 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1406 | * If @c pDecodedItem is not an indefinite-length string, this does nothing. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1407 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1408 | * If it is, this loops getting the subsequent chunk data items that |
| 1409 | * make up the string. The string allocator is used to make a |
| 1410 | * contiguous buffer for the chunks. When this completes @c |
| 1411 | * pDecodedItem contains the put-together string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1412 | * |
| 1413 | * Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1414 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1415 | static QCBORError |
| 1416 | QCBORDecode_Private_GetNextFullString(QCBORDecodeContext *pMe, |
| 1417 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1418 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1419 | /* Aproximate stack usage |
| 1420 | * 64-bit 32-bit |
| 1421 | * local vars 32 16 |
| 1422 | * 2 UsefulBufs 32 16 |
| 1423 | * QCBORItem 56 52 |
| 1424 | * TOTAL 120 74 |
| 1425 | */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1426 | QCBORError uReturn; |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 1427 | |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1428 | /* A note about string allocation -- Memory for strings is |
| 1429 | * allocated either because 1) indefinte-length string chunks are |
| 1430 | * being coalecsed or 2) caller has requested all strings be |
| 1431 | * allocated. The first case is handed below here. The second case |
| 1432 | * is handled in DecodeString if the bAllocate is true. That |
| 1433 | * boolean originates here with pMe->bStringAllocateAll immediately |
| 1434 | * below. That is, QCBOR_Private_DecodeAtomicDataItem() is called |
| 1435 | * in two different contexts here 1) main-line processing which is |
| 1436 | * where definite-length strings need to be allocated if |
| 1437 | * bStringAllocateAll is true and 2) processing chunks of |
| 1438 | * indefinite-lengths strings in in which case there must be no |
| 1439 | * allocation. |
| 1440 | */ |
| 1441 | |
| 1442 | |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 1443 | uReturn = QCBOR_Private_DecodeAtomicDataItem(pMe, pMe->bStringAllocateAll, pDecodedItem); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1444 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1445 | goto Done; |
| 1446 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1447 | |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1448 | |
| 1449 | /* This is where out-of-place break is detected for the whole |
| 1450 | * decoding stack. Break is an error for everything that calls |
| 1451 | * QCBORDecode_Private_GetNextFullString(), so the check is |
| 1452 | * centralized here. |
| 1453 | */ |
| 1454 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
| 1455 | uReturn = QCBOR_ERR_BAD_BREAK; |
| 1456 | goto Done; |
| 1457 | } |
| 1458 | |
| 1459 | |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 1460 | /* Skip out if not an indefinite-length string */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1461 | const uint8_t uStringType = pDecodedItem->uDataType; |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 1462 | if(uStringType != QCBOR_TYPE_BYTE_STRING && |
| 1463 | uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1464 | goto Done; |
| 1465 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1466 | if(pDecodedItem->val.string.len != QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1467 | goto Done; |
| 1468 | } |
| 1469 | |
| 1470 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1471 | /* Can't decode indefinite-length strings without a string allocator */ |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 1472 | if(!pMe->StringAllocator.pfAllocator) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1473 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1474 | goto Done; |
| 1475 | } |
| 1476 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1477 | /* Loop getting chunks of the indefinite-length string */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1478 | UsefulBufC FullString = NULLUsefulBufC; |
| 1479 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1480 | for(;;) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1481 | /* Get QCBORItem for next chunk */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1482 | QCBORItem StringChunkItem; |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1483 | /* Pass false to DecodeAtomicDataItem() because the individual |
| 1484 | * string chunks in an indefinite-length must not be |
| 1485 | * allocated. They are always copied into the allocated |
| 1486 | * contiguous buffer allocated here. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1487 | */ |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 1488 | uReturn = QCBOR_Private_DecodeAtomicDataItem(pMe, false, &StringChunkItem); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1489 | if(uReturn) { |
| 1490 | break; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1491 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1492 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1493 | /* Is item is the marker for end of the indefinite-length string? */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1494 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1495 | /* String is complete */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1496 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1497 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1498 | break; |
| 1499 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1500 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1501 | /* All chunks must be of the same type, the type of the item |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1502 | * that introduces the indefinite-length string. This also |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1503 | * catches errors where the chunk is not a string at all and an |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1504 | * indefinite-length string inside an indefinite-length string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1505 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1506 | if(StringChunkItem.uDataType != uStringType || |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1507 | StringChunkItem.val.string.len == QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1508 | uReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1509 | break; |
| 1510 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1511 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1512 | if (StringChunkItem.val.string.len > 0) { |
| 1513 | /* The first time throurgh FullString.ptr is NULL and this is |
| 1514 | * equivalent to StringAllocator_Allocate(). Subsequently it is |
| 1515 | * not NULL and a reallocation happens. |
| 1516 | */ |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 1517 | UsefulBuf NewMem = StringAllocator_Reallocate(&(pMe->StringAllocator), |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1518 | FullString.ptr, |
| 1519 | FullString.len + StringChunkItem.val.string.len); |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1520 | if(UsefulBuf_IsNULL(NewMem)) { |
| 1521 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
| 1522 | break; |
| 1523 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1524 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1525 | /* Copy new string chunk to the end of accumulated string */ |
| 1526 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1527 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1528 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1529 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1530 | if(uReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1531 | /* Getting the item failed, clean up the allocated memory */ |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 1532 | StringAllocator_Free(&(pMe->StringAllocator), FullString.ptr); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1533 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1534 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1535 | uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED; |
| 1536 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1537 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1538 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1539 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1540 | } |
| 1541 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1542 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1543 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1544 | /** |
| 1545 | * @brief This converts a tag number to a shorter mapped value for storage. |
| 1546 | * |
| 1547 | * @param[in] pMe The decode context. |
| 1548 | * @param[in] uUnMappedTag The tag number to map |
| 1549 | * @param[out] puMappedTagNumer The stored tag number. |
| 1550 | * |
| 1551 | * @return error code. |
| 1552 | * |
| 1553 | * The main point of mapping tag numbers is make QCBORItem |
| 1554 | * smaller. With this mapping storage of 4 tags takes up 8 |
| 1555 | * bytes. Without, it would take up 32 bytes. |
| 1556 | * |
| 1557 | * This maps tag numbers greater than QCBOR_LAST_UNMAPPED_TAG. |
| 1558 | * QCBOR_LAST_UNMAPPED_TAG is a little smaller than MAX_UINT16. |
| 1559 | * |
| 1560 | * See also UnMapTagNumber() and @ref QCBORItem. |
| 1561 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1562 | static QCBORError |
| 1563 | QCBORDecode_Private_MapTagNumber(QCBORDecodeContext *pMe, |
| 1564 | const uint64_t uUnMappedTag, |
| 1565 | uint16_t *puMappedTagNumer) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1566 | { |
| 1567 | if(uUnMappedTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 1568 | unsigned uTagMapIndex; |
| 1569 | /* Is there room in the tag map, or is it in it already? */ |
| 1570 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
| 1571 | if(pMe->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID64) { |
| 1572 | break; |
| 1573 | } |
| 1574 | if(pMe->auMappedTags[uTagMapIndex] == uUnMappedTag) { |
| 1575 | break; |
| 1576 | } |
| 1577 | } |
| 1578 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1579 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 1580 | } |
| 1581 | |
| 1582 | /* Covers the cases where tag is new and were it is already in the map */ |
| 1583 | pMe->auMappedTags[uTagMapIndex] = uUnMappedTag; |
| 1584 | *puMappedTagNumer = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1585 | |
| 1586 | } else { |
| 1587 | *puMappedTagNumer = (uint16_t)uUnMappedTag; |
| 1588 | } |
| 1589 | |
| 1590 | return QCBOR_SUCCESS; |
| 1591 | } |
| 1592 | |
| 1593 | |
| 1594 | /** |
| 1595 | * @brief This converts a mapped tag number to the actual tag number. |
| 1596 | * |
| 1597 | * @param[in] pMe The decode context. |
| 1598 | * @param[in] uMappedTagNumber The stored tag number. |
| 1599 | * |
| 1600 | * @return The actual tag number is returned or |
| 1601 | * @ref CBOR_TAG_INVALID64 on error. |
| 1602 | * |
| 1603 | * This is the reverse of MapTagNumber() |
| 1604 | */ |
| 1605 | static uint64_t |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1606 | QCBORDecode_Private_UnMapTagNumber(const QCBORDecodeContext *pMe, |
| 1607 | const uint16_t uMappedTagNumber) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1608 | { |
| 1609 | if(uMappedTagNumber <= QCBOR_LAST_UNMAPPED_TAG) { |
| 1610 | return uMappedTagNumber; |
| 1611 | } else if(uMappedTagNumber == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1612 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1613 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1614 | /* This won't be negative because of code below in |
| 1615 | * MapTagNumber() |
| 1616 | */ |
| 1617 | const unsigned uIndex = uMappedTagNumber - (QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1618 | return pMe->auMappedTags[uIndex]; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1619 | } |
| 1620 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1621 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1622 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1623 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1624 | /** |
| 1625 | * @brief Aggregate all tags wrapping a data item (decode layer 4). |
| 1626 | * |
| 1627 | * @param[in] pMe Decoder context |
| 1628 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1629 | * |
| 1630 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1631 | * features |
| 1632 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1633 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1634 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1635 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1636 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1637 | * of half-precision disabled |
| 1638 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1639 | * float decode is disabled. |
| 1640 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1641 | * simple type in input. |
| 1642 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1643 | * in input, but indefinite |
| 1644 | * lengths disabled. |
| 1645 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 1646 | * but no string allocator. |
| 1647 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 1648 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 1649 | * input, but indefinite-length |
| 1650 | * strings are disabled. |
| 1651 | * @retval QCBOR_ERR_TOO_MANY_TAGS Too many tag numbers on item. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1652 | * |
| 1653 | * This loops getting atomic data items until one is not a tag |
| 1654 | * number. Usually this is largely pass-through because most |
| 1655 | * item are not tag numbers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1656 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1657 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1658 | QCBORDecode_Private_GetNextTagNumber(QCBORDecodeContext *pMe, |
| 1659 | QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1660 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1661 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1662 | /* Accummulate the tags from multiple items here and then copy them |
| 1663 | * into the last item, the non-tag item. |
| 1664 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1665 | uint16_t auItemsTags[QCBOR_MAX_TAGS_PER_ITEM]; |
| 1666 | |
| 1667 | /* Initialize to CBOR_TAG_INVALID16 */ |
| 1668 | #if CBOR_TAG_INVALID16 != 0xffff |
| 1669 | /* Be sure the memset does the right thing. */ |
| 1670 | #err CBOR_TAG_INVALID16 tag not defined as expected |
| 1671 | #endif |
| 1672 | memset(auItemsTags, 0xff, sizeof(auItemsTags)); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1673 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1674 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1675 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1676 | /* Loop fetching data items until the item fetched is not a tag */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1677 | for(;;) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1678 | QCBORError uErr = QCBORDecode_Private_GetNextFullString(pMe, pDecodedItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1679 | if(uErr != QCBOR_SUCCESS) { |
| 1680 | uReturn = uErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1681 | goto Done; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1682 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1683 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1684 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1685 | /* Successful exit from loop; maybe got some tags, maybe not */ |
| 1686 | memcpy(pDecodedItem->uTags, auItemsTags, sizeof(auItemsTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1687 | break; |
| 1688 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1689 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1690 | if(auItemsTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1691 | /* No room in the tag list */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1692 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1693 | /* Continue on to get all tags wrapping this item even though |
| 1694 | * it is erroring out in the end. This allows decoding to |
| 1695 | * continue. This is a resource limit error, not a problem |
| 1696 | * with being well-formed CBOR. |
| 1697 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1698 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1699 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1700 | /* Slide tags over one in the array to make room at index 0. |
| 1701 | * Must use memmove because the move source and destination |
| 1702 | * overlap. |
| 1703 | */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1704 | memmove(&auItemsTags[1], |
| 1705 | auItemsTags, |
| 1706 | sizeof(auItemsTags) - sizeof(auItemsTags[0])); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1707 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1708 | /* Map the tag */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1709 | uint16_t uMappedTagNumber = 0; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1710 | uReturn = QCBORDecode_Private_MapTagNumber(pMe, pDecodedItem->val.uTagV, &uMappedTagNumber); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1711 | /* Continue even on error so as to consume all tags wrapping |
| 1712 | * this data item so decoding can go on. If MapTagNumber() |
| 1713 | * errors once it will continue to error. |
| 1714 | */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1715 | auItemsTags[0] = uMappedTagNumber; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1716 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1717 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1718 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1719 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1720 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1721 | #else /* QCBOR_DISABLE_TAGS */ |
| 1722 | |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 1723 | return QCBORDecode_Private_GetNextFullString(pMe, pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1724 | |
| 1725 | #endif /* QCBOR_DISABLE_TAGS */ |
| 1726 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1727 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1728 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1729 | /** |
| 1730 | * @brief Combine a map entry label and value into one item (decode layer 3). |
| 1731 | * |
| 1732 | * @param[in] pMe Decoder context |
| 1733 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1734 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1735 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1736 | * features |
| 1737 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1738 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1739 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1740 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1741 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1742 | * of half-precision disabled |
| 1743 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1744 | * float decode is disabled. |
| 1745 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1746 | * simple type in input. |
| 1747 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1748 | * in input, but indefinite |
| 1749 | * lengths disabled. |
| 1750 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 1751 | * but no string allocator. |
| 1752 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 1753 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 1754 | * input, but indefinite-length |
| 1755 | * strings are disabled. |
| 1756 | * @retval QCBOR_ERR_TOO_MANY_TAGS Too many tag numbers on item. |
| 1757 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG Too many items in array. |
| 1758 | * @retval QCBOR_ERR_MAP_LABEL_TYPE Map label not string or integer. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1759 | * |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1760 | * If the current nesting level is a map, then this combines pairs of |
| 1761 | * items into one data item with a label and value. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1762 | * |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1763 | * This is passthrough if the current nesting level is not a map. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1764 | * |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1765 | * This also implements maps-as-array mode where a map is treated like |
| 1766 | * an array to allow caller to do their own label processing. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1767 | */ |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1768 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1769 | static QCBORError |
| 1770 | QCBORDecode_Private_GetNextMapEntry(QCBORDecodeContext *pMe, |
| 1771 | QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1772 | { |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1773 | QCBORItem LabelItem; |
| 1774 | QCBORError uErr; |
| 1775 | |
| 1776 | uErr = QCBORDecode_Private_GetNextTagNumber(pMe, pDecodedItem); |
| 1777 | if(QCBORDecode_IsUnrecoverableError(uErr)) { |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1778 | goto Done; |
| 1779 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1780 | |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1781 | if(!DecodeNesting_IsCurrentTypeMap(&(pMe->nesting))) { |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1782 | /* Not decoding a map. Nothing to do. */ |
| 1783 | /* When decoding maps-as-arrays, the type will be |
| 1784 | * QCBOR_TYPE_MAP_AS_ARRAY and this function will exit |
| 1785 | * here. This is now map processing for maps-as-arrays is not |
| 1786 | * done. */ |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1787 | goto Done; |
| 1788 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1789 | |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1790 | /* Decoding a map entry, so the item decoded above was the label */ |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1791 | LabelItem = *pDecodedItem; |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1792 | |
| 1793 | /* Get the value of the map item */ |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1794 | uErr = QCBORDecode_Private_GetNextTagNumber(pMe, pDecodedItem); |
| 1795 | if(QCBORDecode_IsUnrecoverableError(uErr)) { |
| 1796 | goto Done; |
| 1797 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1798 | |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1799 | /* Combine the label item and value item into one */ |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1800 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1801 | pDecodedItem->uLabelType = LabelItem.uDataType; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1802 | |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1803 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1804 | /* QCBOR_DECODE_MODE_MAP_STRINGS_ONLY might have been a bad idea. Maybe |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1805 | * get rid of it in QCBOR 2.0 |
| 1806 | */ |
| 1807 | if(pMe->uDecodeMode == QCBOR_DECODE_MODE_MAP_STRINGS_ONLY && |
| 1808 | LabelItem.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 1809 | uErr = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1810 | goto Done; |
| 1811 | } |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1812 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1813 | |
| 1814 | switch(LabelItem.uDataType) { |
| 1815 | case QCBOR_TYPE_INT64: |
| 1816 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1817 | break; |
| 1818 | |
| 1819 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 1820 | case QCBOR_TYPE_UINT64: |
| 1821 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1822 | break; |
| 1823 | |
| 1824 | case QCBOR_TYPE_TEXT_STRING: |
| 1825 | case QCBOR_TYPE_BYTE_STRING: |
| 1826 | pDecodedItem->label.string = LabelItem.val.string; |
| 1827 | break; |
Laurence Lundblade | 38964bd | 2024-06-08 11:49:35 -0700 | [diff] [blame^] | 1828 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1829 | |
| 1830 | default: |
| 1831 | uErr = QCBOR_ERR_MAP_LABEL_TYPE; |
| 1832 | goto Done; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1833 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1834 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1835 | Done: |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1836 | return uErr; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1840 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1841 | /** |
| 1842 | * @brief Peek and see if next data item is a break; |
| 1843 | * |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1844 | * param[in] pUIB UsefulInputBuf to read from. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1845 | * @param[out] pbNextIsBreak Indicate if next was a break or not. |
| 1846 | * |
| 1847 | * @return Any decoding error. |
| 1848 | * |
| 1849 | * See if next item is a CBOR break. If it is, it is consumed, |
| 1850 | * if not it is not consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1851 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1852 | static QCBORError |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1853 | QCBOR_Private_NextIsBreak(QCBORDecodeContext *pMe, bool *pbNextIsBreak) |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1854 | { |
| 1855 | *pbNextIsBreak = false; |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1856 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf)) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1857 | QCBORItem Peek; |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1858 | size_t uPeek = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 1859 | QCBORError uReturn = QCBOR_Private_DecodeAtomicDataItem(pMe, false, &Peek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1860 | if(uReturn != QCBOR_SUCCESS) { |
| 1861 | return uReturn; |
| 1862 | } |
| 1863 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1864 | /* It is not a break, rewind so it can be processed normally. */ |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1865 | UsefulInputBuf_Seek(&(pMe->InBuf), uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1866 | } else { |
| 1867 | *pbNextIsBreak = true; |
| 1868 | } |
| 1869 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1870 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1871 | return QCBOR_SUCCESS; |
| 1872 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1873 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1874 | |
| 1875 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1876 | /** |
| 1877 | * @brief Ascend up nesting levels if all items in them have been consumed. |
| 1878 | * |
| 1879 | * @param[in] pMe The decode context. |
| 1880 | * @param[in] bMarkEnd If true mark end of maps/arrays with count of zero. |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 1881 | * @param[out] pbBreak Set to true if extra break was consumed. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1882 | * |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1883 | * An item was just consumed, now figure out if it was the |
| 1884 | * end of an array/map map that can be closed out. That |
| 1885 | * may in turn close out the above array/map... |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 1886 | * |
| 1887 | * When ascending indefinite-length arrays and maps, this will correctly |
| 1888 | * consume the break for the level above. This is a problem for the |
| 1889 | * implementation of QCBORDecode_GetArray() that must not return |
| 1890 | * that break. @c pbBreak is set to true to indicate that one |
| 1891 | * byte should be removed. |
| 1892 | * |
| 1893 | * Improvement: this could reduced further if indef is disabled |
| 1894 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1895 | static QCBORError |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 1896 | QCBORDecode_Private_NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd, bool *pbBreak) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1897 | { |
| 1898 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1899 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1900 | /* Loop ascending nesting levels as long as there is ascending to do */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1901 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 1902 | if(pbBreak) { |
| 1903 | *pbBreak = false; |
| 1904 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1905 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1906 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
| 1907 | /* Nesting level is bstr-wrapped CBOR */ |
| 1908 | |
| 1909 | /* Ascent for bstr-wrapped CBOR is always by explicit call |
| 1910 | * so no further ascending can happen. |
| 1911 | */ |
| 1912 | break; |
| 1913 | |
| 1914 | } else if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 1915 | /* Level is a definite-length array/map */ |
| 1916 | |
| 1917 | /* Decrement the item count the definite-length array/map */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1918 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1919 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1920 | /* Didn't close out array/map, so all work here is done */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1921 | break; |
| 1922 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1923 | /* All items in a definite-length array were consumed so it |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1924 | * is time to ascend one level. This happens below. |
| 1925 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1926 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1927 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1928 | } else { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1929 | /* Level is an indefinite-length array/map. */ |
| 1930 | |
| 1931 | /* Check for a break which is what ends indefinite-length arrays/maps */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1932 | bool bIsBreak = false; |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 1933 | uReturn = QCBOR_Private_NextIsBreak(pMe, &bIsBreak); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1934 | if(uReturn != QCBOR_SUCCESS) { |
| 1935 | goto Done; |
| 1936 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1937 | |
| 1938 | if(!bIsBreak) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1939 | /* Not a break so array/map does not close out. All work is done */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1940 | break; |
| 1941 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1942 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1943 | /* It was a break in an indefinitelength map / array so |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1944 | * it is time to ascend one level. |
| 1945 | */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 1946 | if(pbBreak) { |
| 1947 | *pbBreak = true; |
| 1948 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1949 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1950 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1951 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1952 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1953 | |
| 1954 | /* All items in the array/map have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1955 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1956 | /* But ascent in bounded mode is only by explicit call to |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1957 | * QCBORDecode_ExitBoundedMode(). |
| 1958 | */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1959 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1960 | /* Set the count to zero for definite-length arrays to indicate |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1961 | * cursor is at end of bounded array/map */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1962 | if(bMarkEnd) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1963 | /* Used for definite and indefinite to signal end */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1964 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1965 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1966 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1967 | break; |
| 1968 | } |
| 1969 | |
| 1970 | /* Finally, actually ascend one level. */ |
| 1971 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1972 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1973 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1974 | uReturn = QCBOR_SUCCESS; |
| 1975 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1976 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1977 | Done: |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1978 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1979 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1980 | return uReturn; |
| 1981 | } |
| 1982 | |
| 1983 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1984 | /** |
| 1985 | * @brief Ascending & Descending out of nesting levels (decode layer 2). |
| 1986 | * |
| 1987 | * @param[in] pMe Decoder context |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 1988 | * @param[out] pbBreak Set to true if extra break was consumed. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1989 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1990 | |
| 1991 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1992 | * features |
| 1993 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1994 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1995 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1996 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1997 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1998 | * of half-precision disabled |
| 1999 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 2000 | * float decode is disabled. |
| 2001 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 2002 | * simple type in input. |
| 2003 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 2004 | * in input, but indefinite |
| 2005 | * lengths disabled. |
| 2006 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 2007 | * but no string allocator. |
| 2008 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 2009 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 2010 | * input, but indefinite-length |
| 2011 | * strings are disabled. |
| 2012 | * @retval QCBOR_ERR_TOO_MANY_TAGS Too many tag numbers on item. |
| 2013 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG Too many items in array. |
| 2014 | * @retval QCBOR_ERR_MAP_LABEL_TYPE Map label not string or integer. |
| 2015 | * @retval QCBOR_ERR_NO_MORE_ITEMS Need more items for map or array. |
| 2016 | * @retval QCBOR_ERR_BAD_BREAK Indefinite-length break in wrong |
| 2017 | * place. |
| 2018 | * @retval QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP Nesting deeper than QCBOR |
| 2019 | * can handle. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2020 | * |
| 2021 | * This handles the traversal descending into and asecnding out of |
| 2022 | * maps, arrays and bstr-wrapped CBOR. It figures out the ends of |
| 2023 | * definite- and indefinte-length maps and arrays by looking at the |
| 2024 | * item count or finding CBOR breaks. It detects the ends of the |
| 2025 | * top-level sequence and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2026 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2027 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2028 | QCBORDecode_Private_GetNextMapOrArray(QCBORDecodeContext *pMe, |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2029 | bool *pbBreak, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2030 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2031 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2032 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 2033 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2034 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2035 | /* If out of bytes to consume, it is either the end of the |
| 2036 | * top-level sequence of some bstr-wrapped CBOR that was entered. |
| 2037 | * |
| 2038 | * In the case of bstr-wrapped CBOR, the length of the |
| 2039 | * UsefulInputBuf was set to that of the bstr-wrapped CBOR. When |
| 2040 | * the bstr-wrapped CBOR is exited, the length is set back to the |
| 2041 | * top-level's length or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2042 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2043 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2044 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2045 | goto Done; |
| 2046 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 2047 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2048 | /* Check to see if at the end of a bounded definite-length map or |
| 2049 | * array. The check for a break ending indefinite-length array is |
| 2050 | * later in QCBORDecode_NestLevelAscender(). |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2051 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2052 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2053 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 2054 | goto Done; |
| 2055 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2056 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2057 | /* ==== Next: not at the end, so get another item ==== */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2058 | uReturn = QCBORDecode_Private_GetNextMapEntry(pMe, pDecodedItem); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2059 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 2060 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2061 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2062 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 2063 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2064 | /* Record the nesting level for this data item before processing |
| 2065 | * any of decrementing and descending. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2066 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2067 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2068 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2069 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 2070 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2071 | if(QCBORItem_IsMapOrArray(*pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2072 | /* If the new item is a map or array, descend. |
| 2073 | * |
| 2074 | * Empty indefinite-length maps and arrays are descended into, |
| 2075 | * but then ascended out of in the next chunk of code. |
| 2076 | * |
| 2077 | * Maps and arrays do count as items in the map/array that |
| 2078 | * encloses them so a decrement needs to be done for them too, |
| 2079 | * but that is done only when all the items in them have been |
| 2080 | * processed, not when they are opened with the exception of an |
| 2081 | * empty map or array. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2082 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2083 | QCBORError uDescendErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2084 | uDescendErr = DecodeNesting_DescendMapOrArray(&(pMe->nesting), |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2085 | pDecodedItem->uDataType, |
| 2086 | pDecodedItem->val.uCount); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2087 | if(uDescendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2088 | /* This error is probably a traversal error and it overrides |
| 2089 | * the non-traversal error. |
| 2090 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2091 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2092 | goto Done; |
| 2093 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2096 | if(!QCBORItem_IsMapOrArray(*pDecodedItem) || |
| 2097 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(*pDecodedItem) || |
| 2098 | QCBORItem_IsIndefiniteLengthMapOrArray(*pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2099 | /* The following cases are handled here: |
| 2100 | * - A non-aggregate item like an integer or string |
| 2101 | * - An empty definite-length map or array |
| 2102 | * - An indefinite-length map or array that might be empty or might not. |
| 2103 | * |
| 2104 | * QCBORDecode_NestLevelAscender() does the work of decrementing the count |
| 2105 | * for an definite-length map/array and break detection for an |
| 2106 | * indefinite-0length map/array. If the end of the map/array was |
| 2107 | * reached, then it ascends nesting levels, possibly all the way |
| 2108 | * to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2109 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2110 | QCBORError uAscendErr; |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2111 | uAscendErr = QCBORDecode_Private_NestLevelAscender(pMe, true, pbBreak); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2112 | if(uAscendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2113 | /* This error is probably a traversal error and it overrides |
| 2114 | * the non-traversal error. |
| 2115 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2116 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2117 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 2118 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2119 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2120 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 2121 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2122 | /* Tell the caller what level is next. This tells them what |
| 2123 | * maps/arrays were closed out and makes it possible for them to |
| 2124 | * reconstruct the tree with just the information returned in a |
| 2125 | * QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2126 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2127 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2128 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2129 | pDecodedItem->uNextNestLevel = 0; |
| 2130 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2131 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2132 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2133 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2134 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2135 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2136 | } |
| 2137 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2138 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2139 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2140 | /** |
| 2141 | * @brief Shift 0th tag out of the tag list. |
| 2142 | * |
| 2143 | * pDecodedItem[in,out] The data item to convert. |
| 2144 | * |
| 2145 | * The 0th tag is discarded. \ref CBOR_TAG_INVALID16 is |
| 2146 | * shifted into empty slot at the end of the tag list. |
| 2147 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2148 | static void |
| 2149 | QCBOR_Private_ShiftTags(QCBORItem *pDecodedItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2150 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2151 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM-1; i++) { |
| 2152 | pDecodedItem->uTags[i] = pDecodedItem->uTags[i+1]; |
| 2153 | } |
| 2154 | pDecodedItem->uTags[QCBOR_MAX_TAGS_PER_ITEM-1] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2155 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2156 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2157 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 2158 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2159 | /** |
| 2160 | * @brief Convert different epoch date formats in to the QCBOR epoch date format |
| 2161 | * |
| 2162 | * pDecodedItem[in,out] The data item to convert. |
| 2163 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 2164 | * @retval QCBOR_ERR_DATE_OVERFLOW 65-bit negative integer. |
| 2165 | * @retval QCBOR_ERR_FLOAT_DATE_DISABLED Float-point date in input, |
| 2166 | * floating-point date disabled. |
| 2167 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point date in input, |
| 2168 | * all floating-point disabled. |
| 2169 | * @retval QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT Unexpected and unrecoverable |
| 2170 | * error decoding date. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2171 | * |
| 2172 | * The epoch date tag defined in QCBOR allows for floating-point |
| 2173 | * dates. It even allows a protocol to flop between date formats when |
| 2174 | * ever it wants. Floating-point dates aren't that useful as they are |
| 2175 | * only needed for dates beyond the age of the earth. |
| 2176 | * |
| 2177 | * This converts all the date formats into one format of an unsigned |
| 2178 | * integer plus a floating-point fraction. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2179 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2180 | static QCBORError |
| 2181 | QCBOR_Private_DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2182 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2183 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2184 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2185 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2186 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2187 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2188 | |
| 2189 | switch (pDecodedItem->uDataType) { |
| 2190 | |
| 2191 | case QCBOR_TYPE_INT64: |
| 2192 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 2193 | break; |
| 2194 | |
| 2195 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2196 | /* This only happens for CBOR type 0 > INT64_MAX so it is |
| 2197 | * always an overflow. |
| 2198 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2199 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 2200 | goto Done; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2201 | break; |
| 2202 | |
| 2203 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 2204 | case QCBOR_TYPE_FLOAT: |
| 2205 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2206 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2207 | /* Convert working value to double if input was a float */ |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2208 | const double d = pDecodedItem->uDataType == QCBOR_TYPE_DOUBLE ? |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2209 | pDecodedItem->val.dfnum : |
| 2210 | (double)pDecodedItem->val.fnum; |
| 2211 | |
| 2212 | /* The conversion from float to integer requires overflow |
| 2213 | * detection since floats can be much larger than integers. |
| 2214 | * This implementation errors out on these large float values |
| 2215 | * since they are beyond the age of the earth. |
| 2216 | * |
| 2217 | * These constants for the overflow check are computed by the |
| 2218 | * compiler. They are not computed at run time. |
| 2219 | * |
| 2220 | * The factor of 0x7ff is added/subtracted to avoid a |
| 2221 | * rounding error in the wrong direction when the compiler |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2222 | * computes these constants. There is rounding because a |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2223 | * 64-bit integer has 63 bits of precision where a double |
| 2224 | * only has 53 bits. Without the 0x7ff factor, the compiler |
| 2225 | * may round up and produce a double for the bounds check |
| 2226 | * that is larger than can be stored in a 64-bit integer. The |
| 2227 | * amount of 0x7ff is picked because it has 11 bits set. |
| 2228 | * |
| 2229 | * Without the 0x7ff there is a ~30 minute range of time |
| 2230 | * values 10 billion years in the past and in the future |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2231 | * where this code could go wrong. Some compilers |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2232 | * generate a warning or error without the 0x7ff. |
| 2233 | */ |
| 2234 | const double dDateMax = (double)(INT64_MAX - 0x7ff); |
| 2235 | const double dDateMin = (double)(INT64_MIN + 0x7ff); |
| 2236 | |
| 2237 | if(isnan(d) || d > dDateMax || d < dDateMin) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2238 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2239 | goto Done; |
| 2240 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2241 | |
| 2242 | /* The actual conversion */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2243 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 2244 | pDecodedItem->val.epochDate.fSecondsFraction = |
| 2245 | d - (double)pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2246 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2247 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2248 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2249 | uReturn = QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2250 | goto Done; |
| 2251 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 2252 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2253 | break; |
| 2254 | |
| 2255 | default: |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2256 | /* It's the arrays and maps that are unrecoverable because |
| 2257 | * they are not consumed here. Since this is just an error |
| 2258 | * condition, no extra code is added here to make the error |
| 2259 | * recoverable for non-arrays and maps like strings. */ |
| 2260 | uReturn = QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2261 | goto Done; |
| 2262 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2263 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2264 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 2265 | |
| 2266 | Done: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2267 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2271 | /** |
| 2272 | * @brief Convert the days epoch date. |
| 2273 | * |
| 2274 | * pDecodedItem[in,out] The data item to convert. |
| 2275 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 2276 | * @retval QCBOR_ERR_DATE_OVERFLOW 65-bit negative integer. |
| 2277 | * @retval QCBOR_ERR_FLOAT_DATE_DISABLED Float-point date in input, |
| 2278 | * floating-point date disabled. |
| 2279 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point date in input, |
| 2280 | * all floating-point disabled. |
| 2281 | * @retval QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT Unexpected and unrecoverable |
| 2282 | * error decoding date. |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2283 | * |
| 2284 | * This is much simpler than the other epoch date format because |
| 2285 | * floating-porint is not allowed. This is mostly a simple type check. |
| 2286 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2287 | static QCBORError |
| 2288 | QCBOR_Private_DecodeDaysEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2289 | { |
| 2290 | QCBORError uReturn = QCBOR_SUCCESS; |
| 2291 | |
| 2292 | switch (pDecodedItem->uDataType) { |
| 2293 | |
| 2294 | case QCBOR_TYPE_INT64: |
| 2295 | pDecodedItem->val.epochDays = pDecodedItem->val.int64; |
| 2296 | break; |
| 2297 | |
| 2298 | case QCBOR_TYPE_UINT64: |
| 2299 | /* This only happens for CBOR type 0 > INT64_MAX so it is |
| 2300 | * always an overflow. |
| 2301 | */ |
| 2302 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 2303 | goto Done; |
| 2304 | break; |
| 2305 | |
| 2306 | default: |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2307 | /* It's the arrays and maps that are unrecoverable because |
| 2308 | * they are not consumed here. Since this is just an error |
| 2309 | * condition, no extra code is added here to make the error |
| 2310 | * recoverable for non-arrays and maps like strings. */ |
| 2311 | uReturn = QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2312 | goto Done; |
| 2313 | break; |
| 2314 | } |
| 2315 | |
| 2316 | pDecodedItem->uDataType = QCBOR_TYPE_DAYS_EPOCH; |
| 2317 | |
| 2318 | Done: |
| 2319 | return uReturn; |
| 2320 | } |
| 2321 | |
| 2322 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2323 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2324 | |
| 2325 | /* Forward declaration is necessary for |
| 2326 | * QCBORDecode_MantissaAndExponent(). to be able to decode bignum |
| 2327 | * tags in the mantissa. If the mantissa is a decimal fraction or big |
| 2328 | * float in error, this will result in a recurive call to |
| 2329 | * QCBORDecode_MantissaAndExponent(), but the recursion will unwined |
| 2330 | * correctly and the correct error is returned. |
| 2331 | */ |
| 2332 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2333 | QCBORDecode_Private_GetNextTagContent(QCBORDecodeContext *pMe, |
| 2334 | QCBORItem *pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2335 | |
| 2336 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2337 | /** |
| 2338 | * @brief Decode decimal fractions and big floats. |
| 2339 | * |
| 2340 | * @param[in] pMe The decode context. |
| 2341 | * @param[in,out] pDecodedItem On input the array data item that |
| 2342 | * holds the mantissa and exponent. On |
| 2343 | * output the decoded mantissa and |
| 2344 | * exponent. |
| 2345 | * |
| 2346 | * @returns Decoding errors from getting primitive data items or |
| 2347 | * \ref QCBOR_ERR_BAD_EXP_AND_MANTISSA. |
| 2348 | * |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2349 | * When called pDecodedItem must be the array with two members, the |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2350 | * exponent and mantissa. |
| 2351 | * |
| 2352 | * This will fetch and decode the exponent and mantissa and put the |
| 2353 | * result back into pDecodedItem. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2354 | * |
| 2355 | * This does no checking or processing of tag numbers. That is to be |
| 2356 | * done by the code that calls this. |
| 2357 | * |
| 2358 | * This stuffs the type of the mantissa into pDecodedItem with the expectation |
| 2359 | * the caller will process it. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2360 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2361 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2362 | QCBORDecode_Private_ExpMantissa(QCBORDecodeContext *pMe, |
| 2363 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2364 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2365 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2366 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2367 | /* --- Make sure it is an array; track nesting level of members --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2368 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2369 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2370 | goto Done; |
| 2371 | } |
| 2372 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2373 | /* A check for pDecodedItem->val.uCount == 2 would work for |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2374 | * definite-length arrays, but not for indefinite. Instead remember |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2375 | * the nesting level the two integers must be at, which is one |
| 2376 | * deeper than that of the array. |
| 2377 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2378 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 2379 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2380 | /* --- Get the exponent --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2381 | QCBORItem exponentItem; |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2382 | uReturn = QCBORDecode_Private_GetNextMapOrArray(pMe, NULL, &exponentItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2383 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2384 | goto Done; |
| 2385 | } |
| 2386 | if(exponentItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2387 | /* Array is empty or a map/array encountered when expecting an int */ |
| 2388 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2389 | goto Done; |
| 2390 | } |
| 2391 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2392 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2393 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2394 | * also means that the only data arriving here of type |
| 2395 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2396 | * and thus an error that will get handled in the next else. |
| 2397 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2398 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 2399 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2400 | /* Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2401 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2402 | goto Done; |
| 2403 | } |
| 2404 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2405 | /* --- Get the mantissa --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2406 | QCBORItem mantissaItem; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2407 | uReturn = QCBORDecode_Private_GetNextTagContent(pMe, &mantissaItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2408 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2409 | goto Done; |
| 2410 | } |
| 2411 | if(mantissaItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2412 | /* Mantissa missing or map/array encountered when expecting number */ |
| 2413 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2414 | goto Done; |
| 2415 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2416 | /* Stuff the mantissa data type into the item to send it up to the |
| 2417 | * the next level. */ |
| 2418 | pDecodedItem->uDataType = mantissaItem.uDataType; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2419 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2420 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2421 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2422 | * also means that the only data arriving here of type |
| 2423 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2424 | * and thus an error that will get handled in an else below. |
| 2425 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2426 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2427 | #ifndef QCBOR_DISABLE_TAGS |
| 2428 | /* With tags fully disabled a big number mantissa will error out |
| 2429 | * in the call to QCBORDecode_GetNextWithTags() because it has |
| 2430 | * a tag number. |
| 2431 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2432 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || |
| 2433 | mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2434 | /* Got a good big num mantissa */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2435 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2436 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2437 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2438 | /* Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2439 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2440 | goto Done; |
| 2441 | } |
| 2442 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2443 | /* --- Check that array only has the two numbers --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2444 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2445 | /* Extra items in the decimal fraction / big float */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2446 | /* Improvement: this should probably be an unrecoverable error. */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2447 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2448 | goto Done; |
| 2449 | } |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2450 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2451 | |
| 2452 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2453 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2454 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2455 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2456 | |
| 2457 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2458 | #ifndef QCBOR_DISABLE_TAGS |
| 2459 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2460 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2461 | /** |
| 2462 | * @brief Decode the MIME type tag |
| 2463 | * |
| 2464 | * @param[in,out] pDecodedItem The item to decode. |
| 2465 | * |
| 2466 | * Handle the text and binary MIME type tags. Slightly too complicated |
| 2467 | * f or ProcessTaggedString() because the RFC 7049 MIME type was |
| 2468 | * incorreclty text-only. |
| 2469 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2470 | static QCBORError |
| 2471 | QCBOR_Private_DecodeMIME(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2472 | { |
| 2473 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 2474 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 2475 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2476 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 2477 | } else { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2478 | /* It's the arrays and maps that are unrecoverable because |
| 2479 | * they are not consumed here. Since this is just an error |
| 2480 | * condition, no extra code is added here to make the error |
| 2481 | * recoverable for non-arrays and maps like strings. */ |
| 2482 | return QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2483 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2484 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2485 | return QCBOR_SUCCESS; |
| 2486 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2487 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2488 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2489 | /** |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2490 | * Table of CBOR tags whose content is either a text string or a byte |
| 2491 | * string. The table maps the CBOR tag to the QCBOR type. The high-bit |
| 2492 | * of uQCBORtype indicates the content should be a byte string rather |
| 2493 | * than a text string |
| 2494 | */ |
| 2495 | struct StringTagMapEntry { |
| 2496 | uint16_t uTagNumber; |
| 2497 | uint8_t uQCBORtype; |
| 2498 | }; |
| 2499 | |
| 2500 | #define IS_BYTE_STRING_BIT 0x80 |
| 2501 | #define QCBOR_TYPE_MASK ~IS_BYTE_STRING_BIT |
| 2502 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2503 | static const struct StringTagMapEntry QCBOR_Private_StringTagMap[] = { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2504 | {CBOR_TAG_DATE_STRING, QCBOR_TYPE_DATE_STRING}, |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2505 | {CBOR_TAG_DAYS_STRING, QCBOR_TYPE_DAYS_STRING}, |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2506 | {CBOR_TAG_POS_BIGNUM, QCBOR_TYPE_POSBIGNUM | IS_BYTE_STRING_BIT}, |
| 2507 | {CBOR_TAG_NEG_BIGNUM, QCBOR_TYPE_NEGBIGNUM | IS_BYTE_STRING_BIT}, |
| 2508 | {CBOR_TAG_CBOR, QBCOR_TYPE_WRAPPED_CBOR | IS_BYTE_STRING_BIT}, |
| 2509 | {CBOR_TAG_URI, QCBOR_TYPE_URI}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2510 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2511 | {CBOR_TAG_B64URL, QCBOR_TYPE_BASE64URL}, |
| 2512 | {CBOR_TAG_B64, QCBOR_TYPE_BASE64}, |
| 2513 | {CBOR_TAG_REGEX, QCBOR_TYPE_REGEX}, |
| 2514 | {CBOR_TAG_BIN_UUID, QCBOR_TYPE_UUID | IS_BYTE_STRING_BIT}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2515 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2516 | {CBOR_TAG_CBOR_SEQUENCE, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE | IS_BYTE_STRING_BIT}, |
| 2517 | {CBOR_TAG_INVALID16, QCBOR_TYPE_NONE} |
| 2518 | }; |
| 2519 | |
| 2520 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2521 | /** |
| 2522 | * @brief Process standard CBOR tags whose content is a string |
| 2523 | * |
| 2524 | * @param[in] uTag The tag. |
| 2525 | * @param[in,out] pDecodedItem The data item. |
| 2526 | * |
| 2527 | * @returns This returns QCBOR_SUCCESS if the tag was procssed, |
| 2528 | * \ref QCBOR_ERR_UNSUPPORTED if the tag was not processed and |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2529 | * \ref QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT if the content type was wrong for the tag. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2530 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2531 | * Process the CBOR tags that whose content is a byte string or a text |
| 2532 | * string and for which the string is just passed on to the caller. |
| 2533 | * |
| 2534 | * This maps the CBOR tag to the QCBOR type and checks the content |
| 2535 | * type. Nothing more. It may not be the most important |
Laurence Lundblade | c02e13e | 2020-12-06 05:45:41 -0800 | [diff] [blame] | 2536 | * functionality, but it part of implementing as much of RFC 8949 as |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2537 | * possible. |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2538 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2539 | static QCBORError |
| 2540 | QCBOR_Private_ProcessTaggedString(uint16_t uTag, QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2541 | { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2542 | /* This only works on tags that were not mapped; no need for other yet */ |
| 2543 | if(uTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 2544 | return QCBOR_ERR_UNSUPPORTED; |
| 2545 | } |
| 2546 | |
| 2547 | unsigned uIndex; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2548 | for(uIndex = 0; QCBOR_Private_StringTagMap[uIndex].uTagNumber != CBOR_TAG_INVALID16; uIndex++) { |
| 2549 | if(QCBOR_Private_StringTagMap[uIndex].uTagNumber == uTag) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2550 | break; |
| 2551 | } |
| 2552 | } |
| 2553 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2554 | const uint8_t uQCBORType = QCBOR_Private_StringTagMap[uIndex].uQCBORtype; |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2555 | if(uQCBORType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2556 | /* repurpose this error to mean not handled here */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2557 | return QCBOR_ERR_UNSUPPORTED; |
| 2558 | } |
| 2559 | |
| 2560 | uint8_t uExpectedType = QCBOR_TYPE_TEXT_STRING; |
| 2561 | if(uQCBORType & IS_BYTE_STRING_BIT) { |
| 2562 | uExpectedType = QCBOR_TYPE_BYTE_STRING; |
| 2563 | } |
| 2564 | |
| 2565 | if(pDecodedItem->uDataType != uExpectedType) { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2566 | /* It's the arrays and maps that are unrecoverable because |
| 2567 | * they are not consumed here. Since this is just an error |
| 2568 | * condition, no extra code is added here to make the error |
| 2569 | * recoverable for non-arrays and maps like strings. */ |
| 2570 | return QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2571 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2572 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2573 | pDecodedItem->uDataType = (uint8_t)(uQCBORType & QCBOR_TYPE_MASK); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2574 | return QCBOR_SUCCESS; |
| 2575 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2576 | #endif /* QCBOR_DISABLE_TAGS */ |
| 2577 | |
| 2578 | |
| 2579 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 2580 | /** |
| 2581 | * @brief Figures out data type for exponent mantissa tags. |
| 2582 | * |
| 2583 | * @param[in] uTagToProcess Either @ref CBOR_TAG_DECIMAL_FRACTION or |
| 2584 | * @ref CBOR_TAG_BIG_FLOAT. |
| 2585 | * @param[in] pDecodedItem Item being decoded. |
| 2586 | * |
| 2587 | * @returns One of the 6 values between \ref QCBOR_TYPE_DECIMAL_FRACTION |
| 2588 | * and @ref QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM. |
| 2589 | * |
| 2590 | * Does mapping between a CBOR tag number and a QCBOR type. with a |
| 2591 | * little bit of logic and arithmatic. |
| 2592 | * |
| 2593 | * Used in serveral contexts. Does the work where sometimes the data |
| 2594 | * item is explicitly tagged and sometimes not. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2595 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2596 | static uint8_t |
| 2597 | QCBOR_Private_ExpMantissaDataType(const uint16_t uTagToProcess, |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 2598 | const QCBORItem *pDecodedItem) |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2599 | { |
| 2600 | uint8_t uBase = uTagToProcess == CBOR_TAG_DECIMAL_FRACTION ? |
| 2601 | QCBOR_TYPE_DECIMAL_FRACTION : |
| 2602 | QCBOR_TYPE_BIGFLOAT; |
| 2603 | if(pDecodedItem->uDataType != QCBOR_TYPE_INT64) { |
| 2604 | uBase = (uint8_t)(uBase + pDecodedItem->uDataType - QCBOR_TYPE_POSBIGNUM + 1); |
| 2605 | } |
| 2606 | return uBase; |
| 2607 | } |
| 2608 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2609 | |
| 2610 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2611 | /** |
| 2612 | * @brief Decode tag content for select tags (decoding layer 1). |
| 2613 | * |
| 2614 | * @param[in] pMe The decode context. |
| 2615 | * @param[out] pDecodedItem The decoded item. |
| 2616 | * |
| 2617 | * @return Decoding error code. |
| 2618 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2619 | * CBOR tag numbers for the item were decoded in GetNext_TaggedItem(), |
| 2620 | * but the whole tag was not decoded. Here, the whole tags (tag number |
| 2621 | * and tag content) that are supported by QCBOR are decoded. This is a |
| 2622 | * quick pass through for items that are not tags. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2623 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2624 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2625 | QCBORDecode_Private_GetNextTagContent(QCBORDecodeContext *pMe, |
| 2626 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2627 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2628 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2629 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2630 | uReturn = QCBORDecode_Private_GetNextMapOrArray(pMe, NULL, pDecodedItem); |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2631 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2632 | goto Done; |
| 2633 | } |
| 2634 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2635 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2636 | /* When there are no tag numbers for the item, this exits first |
| 2637 | * thing and effectively does nothing. |
| 2638 | * |
| 2639 | * This loops over all the tag numbers accumulated for this item |
| 2640 | * trying to decode and interpret them. This stops at the end of |
| 2641 | * the list or at the first tag number that can't be interpreted by |
| 2642 | * this code. This is effectively a recursive processing of the |
| 2643 | * tags number list that handles nested tags. |
| 2644 | */ |
| 2645 | while(1) { |
| 2646 | /* Don't bother to unmap tags via QCBORITem.uTags since this |
| 2647 | * code only works on tags less than QCBOR_LAST_UNMAPPED_TAG. |
| 2648 | */ |
| 2649 | const uint16_t uTagToProcess = pDecodedItem->uTags[0]; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2650 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2651 | if(uTagToProcess == CBOR_TAG_INVALID16) { |
| 2652 | /* Hit the end of the tag list. A successful exit. */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2653 | break; |
| 2654 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2655 | } else if(uTagToProcess == CBOR_TAG_DATE_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2656 | uReturn = QCBOR_Private_DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2657 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2658 | } else if(uTagToProcess == CBOR_TAG_DAYS_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2659 | uReturn = QCBOR_Private_DecodeDaysEpoch(pDecodedItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2660 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2661 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2662 | } else if(uTagToProcess == CBOR_TAG_DECIMAL_FRACTION || |
| 2663 | uTagToProcess == CBOR_TAG_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2664 | uReturn = QCBORDecode_Private_ExpMantissa(pMe, pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2665 | /* --- Which is it, decimal fraction or a bigfloat? --- */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2666 | pDecodedItem->uDataType = QCBOR_Private_ExpMantissaDataType(uTagToProcess, pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2667 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2668 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2669 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2670 | } else if(uTagToProcess == CBOR_TAG_MIME || |
| 2671 | uTagToProcess == CBOR_TAG_BINARY_MIME) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2672 | uReturn = QCBOR_Private_DecodeMIME(pDecodedItem); |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2673 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2674 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2675 | } else { |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 2676 | /* See if it is a passthrough byte/text string tag; process if so */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2677 | uReturn = QCBOR_Private_ProcessTaggedString(pDecodedItem->uTags[0], pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2678 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2679 | if(uReturn == QCBOR_ERR_UNSUPPORTED) { |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 2680 | /* It wasn't a passthrough byte/text string tag so it is |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2681 | * an unknown tag. This is the exit from the loop on the |
| 2682 | * first unknown tag. It is a successful exit. |
| 2683 | */ |
| 2684 | uReturn = QCBOR_SUCCESS; |
| 2685 | break; |
| 2686 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2687 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2688 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2689 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2690 | /* Error exit from the loop */ |
| 2691 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2692 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2693 | |
| 2694 | /* A tag was successfully processed, shift it out of the list of |
| 2695 | * tags returned. This is the loop increment. |
| 2696 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2697 | QCBOR_Private_ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2698 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2699 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2700 | |
| 2701 | Done: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2702 | return uReturn; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2703 | } |
| 2704 | |
| 2705 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2706 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2707 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2708 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2709 | QCBORError |
| 2710 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2711 | { |
| 2712 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2713 | uErr = QCBORDecode_Private_GetNextTagContent(pMe, pDecodedItem); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2714 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2715 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2716 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2717 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2718 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | |
| 2722 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2723 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2724 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2725 | QCBORError |
| 2726 | QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2727 | { |
| 2728 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2729 | const UsefulInputBuf Save = pMe->InBuf; |
| 2730 | |
| 2731 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2732 | |
| 2733 | pMe->nesting = SaveNesting; |
| 2734 | pMe->InBuf = Save; |
| 2735 | |
| 2736 | return uErr; |
| 2737 | } |
| 2738 | |
| 2739 | |
| 2740 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2741 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2742 | */ |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2743 | void |
| 2744 | QCBORDecode_VPeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2745 | { |
| 2746 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 2747 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2748 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2749 | return; |
| 2750 | } |
| 2751 | |
| 2752 | pMe->uLastError = (uint8_t)QCBORDecode_PeekNext(pMe, pDecodedItem); |
| 2753 | } |
| 2754 | |
| 2755 | |
| 2756 | /* |
| 2757 | * Public function, see header qcbor/qcbor_decode.h file |
| 2758 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2759 | void |
| 2760 | QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2761 | { |
| 2762 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 2763 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2764 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2765 | return; |
| 2766 | } |
| 2767 | |
| 2768 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2769 | } |
| 2770 | |
| 2771 | |
| 2772 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2773 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2774 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2775 | QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2776 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *pMe, |
| 2777 | QCBORItem *pDecodedItem, |
| 2778 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2779 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2780 | #ifndef QCBOR_DISABLE_TAGS |
| 2781 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2782 | QCBORError uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2783 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2784 | uReturn = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2785 | if(uReturn != QCBOR_SUCCESS) { |
| 2786 | return uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | if(pTags != NULL) { |
| 2790 | pTags->uNumUsed = 0; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2791 | /* Reverse the order because pTags is reverse of QCBORItem.uTags. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2792 | for(int nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >=0; nTagIndex--) { |
| 2793 | if(pDecodedItem->uTags[nTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2794 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2795 | } |
| 2796 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2797 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2798 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2799 | pTags->puTags[pTags->uNumUsed] = QCBORDecode_Private_UnMapTagNumber(pMe,pDecodedItem->uTags[nTagIndex]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2800 | pTags->uNumUsed++; |
| 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | return QCBOR_SUCCESS; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2805 | |
| 2806 | #else /* QCBOR_DISABLE_TAGS */ |
| 2807 | (void)pMe; |
| 2808 | (void)pDecodedItem; |
| 2809 | (void)pTags; |
| 2810 | return QCBOR_ERR_TAGS_DISABLED; |
| 2811 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2815 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2816 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2817 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2818 | bool |
| 2819 | QCBORDecode_IsTagged(QCBORDecodeContext *pMe, |
| 2820 | const QCBORItem *pItem, |
| 2821 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2822 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2823 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2824 | for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
| 2825 | if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2826 | break; |
| 2827 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2828 | if(QCBORDecode_Private_UnMapTagNumber(pMe, pItem->uTags[uTagIndex]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2829 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2830 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2831 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2832 | #else /* QCBOR_TAGS_DISABLED */ |
| 2833 | (void)pMe; |
| 2834 | (void)pItem; |
| 2835 | (void)uTag; |
| 2836 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2837 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2838 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | |
| 2842 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2843 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2844 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2845 | QCBORError |
| 2846 | QCBORDecode_PartialFinish(QCBORDecodeContext *pMe, size_t *puConsumed) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2847 | { |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2848 | if(puConsumed != NULL) { |
| 2849 | *puConsumed = pMe->InBuf.cursor; |
| 2850 | } |
| 2851 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2852 | QCBORError uReturn = pMe->uLastError; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2853 | |
| 2854 | if(uReturn != QCBOR_SUCCESS) { |
| 2855 | goto Done; |
| 2856 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2857 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2858 | /* Error out if all the maps/arrays are not closed out */ |
| 2859 | if(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2860 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2861 | goto Done; |
| 2862 | } |
| 2863 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2864 | /* Error out if not all the bytes are consumed */ |
| 2865 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2866 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2867 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2868 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2869 | Done: |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2870 | return uReturn; |
| 2871 | } |
| 2872 | |
| 2873 | |
| 2874 | /* |
| 2875 | * Public function, see header qcbor/qcbor_decode.h file |
| 2876 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2877 | QCBORError |
| 2878 | QCBORDecode_Finish(QCBORDecodeContext *pMe) |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2879 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2880 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2881 | /* Call the destructor for the string allocator if there is one. |
| 2882 | * Always called, even if there are errors; always have to clean up. |
| 2883 | */ |
| 2884 | StringAllocator_Destruct(&(pMe->StringAllocator)); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2885 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2886 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2887 | return QCBORDecode_PartialFinish(pMe, NULL); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
| 2890 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2891 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2892 | * Public function, see header qcbor/qcbor_decode.h file |
| 2893 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2894 | uint64_t |
| 2895 | QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2896 | const QCBORItem *pItem, |
| 2897 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2898 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2899 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2900 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2901 | return CBOR_TAG_INVALID64; |
| 2902 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2903 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2904 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2905 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2906 | return QCBORDecode_Private_UnMapTagNumber(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2907 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2908 | #else /* QCBOR_DISABLE_TAGS */ |
| 2909 | (void)pMe; |
| 2910 | (void)pItem; |
| 2911 | (void)uIndex; |
| 2912 | |
| 2913 | return CBOR_TAG_INVALID64; |
| 2914 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2915 | } |
| 2916 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2917 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2918 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2919 | * Public function, see header qcbor/qcbor_decode.h file |
| 2920 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2921 | uint64_t |
| 2922 | QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2923 | uint32_t uIndex) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2924 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2925 | #ifndef QCBOR_DISABLE_TAGS |
| 2926 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2927 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2928 | return CBOR_TAG_INVALID64; |
| 2929 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2930 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2931 | return CBOR_TAG_INVALID64; |
| 2932 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2933 | return QCBORDecode_Private_UnMapTagNumber(pMe, pMe->uLastTags[uIndex]); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2934 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2935 | #else /* QCBOR_DISABLE_TAGS */ |
| 2936 | (void)pMe; |
| 2937 | (void)uIndex; |
| 2938 | |
| 2939 | return CBOR_TAG_INVALID64; |
| 2940 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2941 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2942 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2943 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2944 | |
| 2945 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2946 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2947 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2948 | /* =========================================================================== |
| 2949 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2950 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2951 | This implements a simple sting allocator for indefinite-length |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2952 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2953 | implements the function type QCBORStringAllocate and allows easy |
| 2954 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2955 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2956 | This particular allocator is built-in for convenience. The caller |
| 2957 | can implement their own. All of this following code will get |
| 2958 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2959 | |
| 2960 | This is a very primitive memory allocator. It does not track |
| 2961 | individual allocations, only a high-water mark. A free or |
| 2962 | reallocation must be of the last chunk allocated. |
| 2963 | |
| 2964 | The size of the pool and offset to free memory are packed into the |
| 2965 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2966 | the decode context. Since the address of the pool may not be |
| 2967 | aligned, they have to be packed and unpacked as if they were |
| 2968 | serialized data of the wire or such. |
| 2969 | |
| 2970 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2971 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2972 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2973 | |
| 2974 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2975 | static int |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2976 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2977 | { |
| 2978 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2979 | UsefulInputBuf UIB; |
| 2980 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2981 | // Just assume the size here. It was checked during SetUp so |
| 2982 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2983 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2984 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2985 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2986 | return UsefulInputBuf_GetError(&UIB); |
| 2987 | } |
| 2988 | |
| 2989 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2990 | static int |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2991 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2992 | { |
| 2993 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2994 | // length check performed here is useful. |
| 2995 | UsefulOutBuf UOB; |
| 2996 | |
| 2997 | UsefulOutBuf_Init(&UOB, Pool); |
| 2998 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2999 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 3000 | return UsefulOutBuf_GetError(&UOB); |
| 3001 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3002 | |
| 3003 | |
| 3004 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3005 | Internal function for an allocation, reallocation free and destuct. |
| 3006 | |
| 3007 | Having only one function rather than one each per mode saves space in |
| 3008 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3009 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3010 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 3011 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3012 | static UsefulBuf |
| 3013 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3014 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3015 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3016 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3017 | uint32_t uPoolSize; |
| 3018 | uint32_t uFreeOffset; |
| 3019 | |
| 3020 | if(uNewSize > UINT32_MAX) { |
| 3021 | // This allocator is only good up to 4GB. This check should |
| 3022 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 3023 | goto Done; |
| 3024 | } |
| 3025 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 3026 | |
| 3027 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 3028 | goto Done; |
| 3029 | } |
| 3030 | |
| 3031 | if(uNewSize) { |
| 3032 | if(pMem) { |
| 3033 | // REALLOCATION MODE |
| 3034 | // Calculate pointer to the end of the memory pool. It is |
| 3035 | // assumed that pPool + uPoolSize won't wrap around by |
| 3036 | // assuming the caller won't pass a pool buffer in that is |
| 3037 | // not in legitimate memory space. |
| 3038 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 3039 | |
| 3040 | // Check that the pointer for reallocation is in the range of the |
| 3041 | // pool. This also makes sure that pointer math further down |
| 3042 | // doesn't wrap under or over. |
| 3043 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 3044 | // Offset to start of chunk for reallocation. This won't |
| 3045 | // wrap under because of check that pMem >= pPool. Cast |
| 3046 | // is safe because the pool is always less than UINT32_MAX |
| 3047 | // because of check in QCBORDecode_SetMemPool(). |
| 3048 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 3049 | |
| 3050 | // Check to see if the allocation will fit. uPoolSize - |
| 3051 | // uMemOffset will not wrap under because of check that |
| 3052 | // pMem is in the range of the uPoolSize by check above. |
| 3053 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 3054 | ReturnValue.ptr = pMem; |
| 3055 | ReturnValue.len = uNewSize; |
| 3056 | |
| 3057 | // Addition won't wrap around over because uNewSize was |
| 3058 | // checked to be sure it is less than the pool size. |
| 3059 | uFreeOffset = uMemOffset + uNewSize32; |
| 3060 | } |
| 3061 | } |
| 3062 | } else { |
| 3063 | // ALLOCATION MODE |
| 3064 | // uPoolSize - uFreeOffset will not underflow because this |
| 3065 | // pool implementation makes sure uFreeOffset is always |
| 3066 | // smaller than uPoolSize through this check here and |
| 3067 | // reallocation case. |
| 3068 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 3069 | ReturnValue.len = uNewSize; |
| 3070 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 3071 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3072 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3073 | } |
| 3074 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3075 | if(pMem) { |
| 3076 | // FREE MODE |
| 3077 | // Cast is safe because of limit on pool size in |
| 3078 | // QCBORDecode_SetMemPool() |
| 3079 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 3080 | } else { |
| 3081 | // DESTRUCT MODE |
| 3082 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3083 | } |
| 3084 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3085 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3086 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 3087 | MemPool_Pack(Pool, uFreeOffset); |
| 3088 | |
| 3089 | Done: |
| 3090 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3091 | } |
| 3092 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3093 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3094 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3095 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3096 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3097 | QCBORError |
| 3098 | QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 3099 | UsefulBuf Pool, |
| 3100 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3101 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3102 | // The pool size and free mem offset are packed into the beginning |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3103 | // of the pool memory. This compile time check makes sure the |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3104 | // constant in the header is correct. This check should optimize |
| 3105 | // down to nothing. |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3106 | #ifdef _MSC_VER |
| 3107 | #pragma warning(push) |
| 3108 | #pragma warning(disable:4127) // conditional expression is constant |
| 3109 | #endif |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3110 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3111 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3112 | } |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3113 | #ifdef _MSC_VER |
| 3114 | #pragma warning(pop) |
| 3115 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3116 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3117 | // The pool size and free offset packed in to the beginning of pool |
| 3118 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 3119 | // machines. |
| 3120 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3121 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3122 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3123 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3124 | // This checks that the pool buffer given is big enough. |
| 3125 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3126 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3127 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3128 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 3129 | QCBORDecode_SetUpAllocator(pMe, MemPool_Function, Pool.ptr, bAllStrings); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3130 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3131 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3132 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 3133 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3134 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3135 | |
| 3136 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3137 | static void |
| 3138 | QCBORDecode_Private_CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3139 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3140 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3141 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3142 | #else |
| 3143 | (void)pMe; |
| 3144 | (void)pItem; |
| 3145 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3146 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3147 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3148 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3149 | /** |
| 3150 | * @brief Consume an entire map or array including its contents. |
| 3151 | * |
| 3152 | * @param[in] pMe The decoder context. |
| 3153 | * @param[in] pItemToConsume The array/map whose contents are to be |
| 3154 | * consumed. |
| 3155 | * @param[out] puNextNestLevel The next nesting level after the item was |
| 3156 | * fully consumed. |
| 3157 | * |
| 3158 | * This may be called when @c pItemToConsume is not an array or |
| 3159 | * map. In that case, this is just a pass through for @c puNextNestLevel |
| 3160 | * since there is nothing to do. |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3161 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3162 | static QCBORError |
| 3163 | QCBORDecode_Private_ConsumeItem(QCBORDecodeContext *pMe, |
| 3164 | const QCBORItem *pItemToConsume, |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3165 | bool *pbBreak, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3166 | uint8_t *puNextNestLevel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3167 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3168 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3169 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3170 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3171 | /* If it is a map or array, this will tell if it is empty. */ |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3172 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 3173 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3174 | if(QCBORItem_IsMapOrArray(*pItemToConsume) && !bIsEmpty) { |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3175 | /* There is only real work to do for non-empty maps and arrays */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3176 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3177 | /* This works for definite- and indefinite-length maps and |
| 3178 | * arrays by using the nesting level |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3179 | */ |
| 3180 | do { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3181 | uReturn = QCBORDecode_Private_GetNextMapOrArray(pMe, pbBreak, &Item); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3182 | if(QCBORDecode_IsUnrecoverableError(uReturn) || |
| 3183 | uReturn == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3184 | goto Done; |
| 3185 | } |
| 3186 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3187 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3188 | *puNextNestLevel = Item.uNextNestLevel; |
| 3189 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3190 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3191 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3192 | } else { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3193 | /* pItemToConsume is not a map or array. Just pass the nesting |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3194 | * level through. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3195 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 3196 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3197 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3198 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3199 | |
| 3200 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3201 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3202 | } |
| 3203 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3204 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3205 | /* |
| 3206 | * Public function, see header qcbor/qcbor_decode.h file |
| 3207 | */ |
| 3208 | void |
| 3209 | QCBORDecode_VGetNextConsume(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3210 | { |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3211 | QCBORDecode_VGetNext(pMe, pDecodedItem); |
| 3212 | |
| 3213 | if(pMe->uLastError == QCBOR_SUCCESS) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3214 | pMe->uLastError = (uint8_t)QCBORDecode_Private_ConsumeItem(pMe, pDecodedItem, NULL, |
Máté Tóth-Pál | c6d5968 | 2021-05-26 18:55:30 +0200 | [diff] [blame] | 3215 | &pDecodedItem->uNextNestLevel); |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3216 | } |
| 3217 | } |
| 3218 | |
| 3219 | |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 3220 | /* |
| 3221 | * Public function, see header qcbor/qcbor_decode.h file |
| 3222 | */ |
| 3223 | uint32_t |
| 3224 | QCBORDecode_Tell(QCBORDecodeContext *pMe) |
| 3225 | { |
| 3226 | size_t uCursorOffset; |
| 3227 | |
| 3228 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3229 | return UINT32_MAX; |
| 3230 | } |
| 3231 | |
| 3232 | uCursorOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3233 | |
| 3234 | if(uCursorOffset == UsefulInputBuf_GetBufferLength(&(pMe->InBuf))) { |
| 3235 | return UINT32_MAX; |
| 3236 | } else { |
| 3237 | /* Cast is safe because decoder input size is restricted. */ |
| 3238 | return (uint32_t)uCursorOffset; |
| 3239 | } |
| 3240 | } |
| 3241 | |
| 3242 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3243 | /** |
| 3244 | * @brief Rewind cursor to start as if map or array were just entered. |
| 3245 | * |
| 3246 | * @param[in] pMe The decoding context |
| 3247 | * |
| 3248 | * This affects the nesting tracking and the UsefulInputBuf. |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3249 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3250 | static void |
| 3251 | QCBORDecode_Private_RewindMapOrArray(QCBORDecodeContext *pMe) |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3252 | { |
| 3253 | /* Reset nesting tracking to the deepest bounded level */ |
| 3254 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 3255 | |
| 3256 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3257 | |
| 3258 | /* Reposition traversal cursor to the start of the map/array */ |
| 3259 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 3260 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
| 3261 | } |
| 3262 | |
| 3263 | |
| 3264 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3265 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3266 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3267 | void |
| 3268 | QCBORDecode_Rewind(QCBORDecodeContext *pMe) |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3269 | { |
| 3270 | if(pMe->nesting.pCurrentBounded != NULL) { |
| 3271 | /* In a bounded map, array or bstr-wrapped CBOR */ |
| 3272 | |
| 3273 | if(DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
| 3274 | /* In bstr-wrapped CBOR. */ |
| 3275 | |
| 3276 | /* Reposition traversal cursor to start of wrapping byte string */ |
| 3277 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 3278 | pMe->nesting.pCurrentBounded->u.bs.uBstrStartOffset); |
| 3279 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 3280 | |
| 3281 | } else { |
| 3282 | /* In a map or array */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3283 | QCBORDecode_Private_RewindMapOrArray(pMe); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | } else { |
| 3287 | /* Not in anything bounded */ |
| 3288 | |
| 3289 | /* Reposition traversal cursor to the start of input CBOR */ |
| 3290 | UsefulInputBuf_Seek(&(pMe->InBuf), 0ULL); |
| 3291 | |
| 3292 | /* Reset nesting tracking to beginning of input. */ |
| 3293 | DecodeNesting_Init(&(pMe->nesting)); |
| 3294 | } |
| 3295 | |
| 3296 | pMe->uLastError = QCBOR_SUCCESS; |
| 3297 | } |
| 3298 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3299 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3300 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3301 | |
| 3302 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3303 | typedef struct { |
| 3304 | void *pCBContext; |
| 3305 | QCBORItemCallback pfCallback; |
| 3306 | } MapSearchCallBack; |
| 3307 | |
| 3308 | typedef struct { |
| 3309 | size_t uStartOffset; |
| 3310 | uint16_t uItemCount; |
| 3311 | } MapSearchInfo; |
| 3312 | |
| 3313 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3314 | /** |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3315 | * @brief Search a map for a set of items. |
| 3316 | * |
| 3317 | * @param[in] pMe The decode context to search. |
| 3318 | * @param[in,out] pItemArray The items to search for and the items found. |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3319 | * @param[out] pInfo Several bits of meta-info returned by search. |
| 3320 | * @param[in] pCallBack Callback object or @c NULL. |
| 3321 | * TODO: fix params |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3322 | * |
| 3323 | * @retval QCBOR_ERR_NOT_ENTERED Trying to search without entering a map. |
| 3324 | * |
| 3325 | * @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 3326 | * were found for one of the labels being |
| 3327 | * search for. This duplicate detection is |
| 3328 | * only performed for items in pItemArray, |
| 3329 | * not every item in the map. |
| 3330 | * |
| 3331 | * @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 3332 | * wrong for the matchd label. |
| 3333 | * |
| 3334 | * @retval Also errors returned by QCBORDecode_GetNext(). |
| 3335 | * |
| 3336 | * On input, \c pItemArray contains a list of labels and data types of |
| 3337 | * items to be found. |
| 3338 | * |
| 3339 | * On output, the fully retrieved items are filled in with values and |
| 3340 | * such. The label was matched, so it never changes. |
| 3341 | * |
| 3342 | * If an item was not found, its data type is set to @ref QCBOR_TYPE_NONE. |
| 3343 | * |
| 3344 | * This also finds the ends of maps and arrays when they are exited. |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3345 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3346 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3347 | QCBORDecode_Private_MapSearch(QCBORDecodeContext *pMe, |
| 3348 | QCBORItem *pItemArray, |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3349 | MapSearchInfo *pInfo, |
| 3350 | MapSearchCallBack *pCallBack) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3351 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3352 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3353 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 3354 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3355 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3356 | uReturn = pMe->uLastError; |
| 3357 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3358 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3359 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3360 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3361 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 3362 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 3363 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3364 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 3365 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3366 | } |
| 3367 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3368 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 3369 | // It is an empty bounded array or map |
| 3370 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 3371 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3372 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3373 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3374 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3375 | // Nothing is ever found in an empty array or map. All items |
| 3376 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3377 | uReturn = QCBOR_SUCCESS; |
| 3378 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3379 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3380 | } |
| 3381 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3382 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 3383 | size_t uSavePos = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3384 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 3385 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3386 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3387 | QCBORDecode_Private_RewindMapOrArray(pMe); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3388 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3389 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3390 | Loop over all the items in the map or array. Each item |
| 3391 | could be a map or array, but label matching is only at |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 3392 | the main level. This handles definite- and indefinite- |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3393 | length maps and arrays. The only reason this is ever |
| 3394 | called on arrays is to find their end position. |
| 3395 | |
| 3396 | This will always run over all items in order to do |
| 3397 | duplicate detection. |
| 3398 | |
| 3399 | This will exit with failure if it encounters an |
| 3400 | unrecoverable error, but continue on for recoverable |
| 3401 | errors. |
| 3402 | |
| 3403 | If a recoverable error occurs on a matched item, then |
| 3404 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3405 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 3406 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3407 | if(pInfo) { |
| 3408 | pInfo->uItemCount = 0; |
| 3409 | } |
Máté Tóth-Pál | 2f2aa5f | 2021-05-17 21:35:09 +0200 | [diff] [blame] | 3410 | uint8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3411 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3412 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3413 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3414 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3415 | /* Get the item */ |
| 3416 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3417 | QCBORError uResult = QCBORDecode_Private_GetNextTagContent(pMe, &Item); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3418 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3419 | /* The map/array can't be decoded when unrecoverable errors occur */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3420 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3421 | goto Done; |
| 3422 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3423 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3424 | /* Unexpected end of map or array. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3425 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3426 | goto Done; |
| 3427 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3428 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3429 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3430 | bool bMatched = false; |
| 3431 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3432 | if(QCBORItem_MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3433 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3434 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 3435 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3436 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3437 | } |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3438 | if(uResult != QCBOR_SUCCESS) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3439 | /* The label matches, but the data item is in error. |
| 3440 | * It is OK to have recoverable errors on items that are not |
| 3441 | * matched. */ |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3442 | uReturn = uResult; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3443 | goto Done; |
| 3444 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3445 | if(!QCBORItem_MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3446 | /* The data item is not of the type(s) requested */ |
| 3447 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3448 | goto Done; |
| 3449 | } |
| 3450 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3451 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3452 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3453 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3454 | if(pInfo) { |
| 3455 | pInfo->uStartOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3456 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3457 | bMatched = true; |
| 3458 | } |
| 3459 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3460 | |
| 3461 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3462 | if(!bMatched && pCallBack != NULL) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3463 | /* |
| 3464 | Call the callback on unmatched labels. |
| 3465 | (It is tempting to do duplicate detection here, but that would |
| 3466 | require dynamic memory allocation because the number of labels |
| 3467 | that might be encountered is unbounded.) |
| 3468 | */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3469 | uReturn = (*(pCallBack->pfCallback))(pCallBack->pCBContext, &Item); |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3470 | if(uReturn != QCBOR_SUCCESS) { |
| 3471 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3472 | } |
| 3473 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3474 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3475 | /* |
| 3476 | Consume the item whether matched or not. This |
| 3477 | does the work of traversing maps and array and |
| 3478 | everything in them. In this loop only the |
| 3479 | items at the current nesting level are examined |
| 3480 | to match the labels. |
| 3481 | */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3482 | uReturn = QCBORDecode_Private_ConsumeItem(pMe, &Item, NULL, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3483 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3484 | goto Done; |
| 3485 | } |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3486 | |
| 3487 | if(pInfo) { |
| 3488 | pInfo->uItemCount++; |
| 3489 | } |
| 3490 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3491 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3492 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3493 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3494 | |
| 3495 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3496 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3497 | // Check here makes sure that this won't accidentally be |
| 3498 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3499 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3500 | // Cast to uint32_t to possibly address cases where SIZE_MAX < UINT32_MAX |
| 3501 | if((uint32_t)uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3502 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3503 | goto Done; |
| 3504 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3505 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 3506 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3507 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3508 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3509 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 3510 | UsefulInputBuf_Seek(&(pMe->InBuf), uSavePos); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3511 | |
| 3512 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3513 | /* For all items not found, set the data and label type to QCBOR_TYPE_NONE */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3514 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3515 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3516 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 3517 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3518 | } |
| 3519 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3520 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3521 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3522 | } |
| 3523 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3524 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3525 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3526 | * Public function, see header qcbor/qcbor_decode.h file |
| 3527 | */ |
| 3528 | void |
| 3529 | QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 3530 | int64_t nLabel, |
| 3531 | uint8_t uQcborType, |
| 3532 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3533 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3534 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3535 | return; |
| 3536 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3537 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3538 | QCBORItem OneItemSeach[2]; |
| 3539 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3540 | OneItemSeach[0].label.int64 = nLabel; |
| 3541 | OneItemSeach[0].uDataType = uQcborType; |
| 3542 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3543 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3544 | QCBORError uReturn = QCBORDecode_Private_MapSearch(pMe, OneItemSeach, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3545 | |
| 3546 | *pItem = OneItemSeach[0]; |
| 3547 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3548 | if(uReturn != QCBOR_SUCCESS) { |
| 3549 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3550 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3551 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3552 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3553 | } |
| 3554 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3555 | Done: |
| 3556 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3560 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3561 | * Public function, see header qcbor/qcbor_decode.h file |
| 3562 | */ |
| 3563 | void |
| 3564 | QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 3565 | const char *szLabel, |
| 3566 | uint8_t uQcborType, |
| 3567 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3568 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3569 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3570 | return; |
| 3571 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3572 | |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 3573 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3574 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3575 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3576 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3577 | OneItemSeach[0].uDataType = uQcborType; |
| 3578 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3579 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3580 | QCBORError uReturn = QCBORDecode_Private_MapSearch(pMe, OneItemSeach, NULL, NULL); |
| 3581 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3582 | if(uReturn != QCBOR_SUCCESS) { |
| 3583 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3584 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3585 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3586 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3587 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3588 | } |
| 3589 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3590 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3591 | Done: |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 3592 | |
| 3593 | #else |
Laurence Lundblade | 9533c48 | 2024-06-06 21:41:10 -0700 | [diff] [blame] | 3594 | (void)pMe; |
| 3595 | (void)szLabel; |
| 3596 | (void)uQcborType; |
| 3597 | (void)pItem; |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 3598 | QCBORError uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
| 3599 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 3600 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3601 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3602 | } |
| 3603 | |
| 3604 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3605 | |
| 3606 | /** |
| 3607 | * @brief Semi-private. Get pointer, length and item for an array or map. |
| 3608 | * |
| 3609 | * @param[in] pMe The decode context. |
| 3610 | * @param[in] uType CBOR major type, either array/map. |
| 3611 | * @param[out] pItem The item for the array/map. |
| 3612 | * @param[out] pEncodedCBOR Pointer and length of the encoded map or array. |
| 3613 | * |
| 3614 | * The next item to be decoded must be a map or array as specified by \c uType. |
| 3615 | * |
| 3616 | * \c pItem will be filled in with the label and tags of the array or map |
| 3617 | * in addition to \c pEncodedCBOR giving the pointer and length of the |
| 3618 | * encoded CBOR. |
| 3619 | * |
| 3620 | * When this is complete, the traversal cursor is at the end of the array or |
| 3621 | * map that was retrieved. |
| 3622 | */ |
| 3623 | void |
| 3624 | QCBORDecode_Private_GetArrayOrMap(QCBORDecodeContext *pMe, |
| 3625 | const uint8_t uType, |
| 3626 | QCBORItem *pItem, |
| 3627 | UsefulBufC *pEncodedCBOR) |
| 3628 | { |
| 3629 | QCBORError uErr; |
| 3630 | uint8_t uNestLevel; |
| 3631 | size_t uStartingCursor; |
| 3632 | size_t uStartOfReturned; |
| 3633 | size_t uEndOfReturned; |
| 3634 | size_t uTempSaveCursor; |
| 3635 | bool bInMap; |
| 3636 | QCBORItem LabelItem; |
| 3637 | bool EndedByBreak; |
| 3638 | |
| 3639 | uStartingCursor = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3640 | bInMap = DecodeNesting_IsCurrentTypeMap(&(pMe->nesting)); |
| 3641 | |
| 3642 | /* Could call GetNext here, but don't need to because this |
| 3643 | * is only interested in arrays and maps. */ |
| 3644 | uErr = QCBORDecode_Private_GetNextMapOrArray(pMe, NULL, pItem); |
| 3645 | if(uErr != QCBOR_SUCCESS) { |
| 3646 | pMe->uLastError = (uint8_t)uErr; |
| 3647 | return; |
| 3648 | } |
| 3649 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 3650 | uint8_t uItemDataType = pItem->uDataType; |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 3651 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 3652 | if(uItemDataType == QCBOR_TYPE_MAP_AS_ARRAY) { |
| 3653 | uItemDataType = QCBOR_TYPE_ARRAY; |
| 3654 | } |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 3655 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 3656 | |
| 3657 | if(uItemDataType != uType) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3658 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3659 | return; |
| 3660 | } |
| 3661 | |
| 3662 | if(bInMap) { |
| 3663 | /* If the item is in a map, the start of the array/map |
| 3664 | * itself, not the label, must be found. Do this by |
| 3665 | * rewinding to the starting position and fetching |
| 3666 | * just the label data item. QCBORDecode_Private_GetNextTagNumber() |
| 3667 | * doesn't do any of the array/map item counting or nesting |
| 3668 | * level tracking. Used here it will just fetech the label |
| 3669 | * data item. |
| 3670 | * |
| 3671 | * Have to save the cursor and put it back to the position |
| 3672 | * after the full item once the label as been fetched by |
| 3673 | * itself. |
| 3674 | */ |
| 3675 | uTempSaveCursor = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3676 | UsefulInputBuf_Seek(&(pMe->InBuf), uStartingCursor); |
| 3677 | |
| 3678 | /* Item has been fetched once so safe to ignore error */ |
| 3679 | (void)QCBORDecode_Private_GetNextTagNumber(pMe, &LabelItem); |
| 3680 | |
| 3681 | uStartOfReturned = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3682 | UsefulInputBuf_Seek(&(pMe->InBuf), uTempSaveCursor); |
| 3683 | } else { |
| 3684 | uStartOfReturned = uStartingCursor; |
| 3685 | } |
| 3686 | |
| 3687 | /* Consume the entire array/map to find the end */ |
| 3688 | uErr = QCBORDecode_Private_ConsumeItem(pMe, pItem, &EndedByBreak, &uNestLevel); |
| 3689 | if(uErr != QCBOR_SUCCESS) { |
| 3690 | pMe->uLastError = (uint8_t)uErr; |
| 3691 | goto Done; |
| 3692 | } |
| 3693 | |
| 3694 | /* Fill in returned values */ |
| 3695 | uEndOfReturned = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3696 | if(EndedByBreak) { |
| 3697 | /* When ascending nesting levels, a break for the level above |
| 3698 | * was consumed. That break is not a part of what is consumed here. */ |
| 3699 | uEndOfReturned--; |
| 3700 | } |
| 3701 | pEncodedCBOR->ptr = UsefulInputBuf_OffsetToPointer(&(pMe->InBuf), uStartOfReturned); |
| 3702 | pEncodedCBOR->len = uEndOfReturned - uStartOfReturned; |
| 3703 | |
| 3704 | Done: |
| 3705 | return; |
| 3706 | } |
| 3707 | |
| 3708 | |
| 3709 | /** |
| 3710 | * @brief Semi-private. Get pointer, length and item count of an array or map. |
| 3711 | * |
| 3712 | * @param[in] pMe The decode context. |
| 3713 | * @param[in] pTarget The label and type of the array or map to retrieve. |
| 3714 | * @param[out] pItem The item for the array/map. |
| 3715 | * @param[out] pEncodedCBOR Pointer and length of the encoded map or array. |
| 3716 | * |
| 3717 | * The next item to be decoded must be a map or array as specified by \c uType. |
| 3718 | * |
| 3719 | * When this is complete, the traversal cursor is unchanged. |
| 3720 | */void |
| 3721 | QCBORDecode_Private_SearchAndGetArrayOrMap(QCBORDecodeContext *pMe, |
| 3722 | QCBORItem *pTarget, |
| 3723 | QCBORItem *pItem, |
| 3724 | UsefulBufC *pEncodedCBOR) |
| 3725 | { |
| 3726 | MapSearchInfo Info; |
| 3727 | QCBORDecodeNesting SaveNesting; |
| 3728 | size_t uSaveCursor; |
| 3729 | |
| 3730 | pMe->uLastError = (uint8_t)QCBORDecode_Private_MapSearch(pMe, pTarget, &Info, NULL); |
| 3731 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3732 | return; |
| 3733 | } |
| 3734 | |
| 3735 | /* Save the whole position of things so they can be restored. |
| 3736 | * so the cursor position is unchanged by this operation, like |
| 3737 | * all the other GetXxxxInMap() operations. */ |
| 3738 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 3739 | uSaveCursor = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3740 | |
| 3741 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3742 | UsefulInputBuf_Seek(&(pMe->InBuf), Info.uStartOffset); |
| 3743 | QCBORDecode_Private_GetArrayOrMap(pMe, pTarget[0].uDataType, pItem, pEncodedCBOR); |
| 3744 | |
| 3745 | UsefulInputBuf_Seek(&(pMe->InBuf), uSaveCursor); |
| 3746 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 3747 | } |
| 3748 | |
| 3749 | |
| 3750 | |
| 3751 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3752 | /** |
| 3753 | * @brief Is a QCBOR_TYPE in the type list? |
| 3754 | * |
| 3755 | * @param[in] uDataType Type to check for. |
| 3756 | * @param[in] puTypeList List to check. |
| 3757 | * |
| 3758 | * @retval QCBOR_SUCCESS If in the list. |
| 3759 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Not in the list. |
| 3760 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3761 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3762 | QCBOR_Private_CheckTypeList(const int uDataType, |
| 3763 | const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3764 | { |
| 3765 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3766 | if(uDataType == puTypeList[i]) { /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3767 | return QCBOR_SUCCESS; |
| 3768 | } |
| 3769 | } |
| 3770 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3771 | } |
| 3772 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 3773 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3774 | /** |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3775 | * Match a tag/type specification against the type of the item. |
| 3776 | * |
| 3777 | * @param[in] TagSpec Specification for matching tags. |
| 3778 | * @param[in] pItem The item to check. |
| 3779 | * |
| 3780 | * @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 3781 | * @retval QCBOR_ERR_UNEXPECTED_TYPE \c uDataType is not allowed by @c TagSpec |
| 3782 | * |
| 3783 | * This checks the item data type of untagged items as well as of |
| 3784 | * tagged items against a specification to see if decoding should |
| 3785 | * proceed. |
| 3786 | * |
| 3787 | * This relies on the automatic tag decoding done by QCBOR that turns |
| 3788 | * tag numbers into particular QCBOR_TYPEs so there is no actual |
| 3789 | * comparsion of tag numbers, just of QCBOR_TYPEs. |
| 3790 | * |
| 3791 | * This checks the data item type as possibly representing the tag |
| 3792 | * number or as the tag content type. |
| 3793 | * |
| 3794 | * If QCBOR_DISABLE_TAGS is #defined, this primarily checks the item |
| 3795 | * data type against the allowed tag content types. It will also error out |
| 3796 | * if the caller tries to require a tag because there is no way that can |
| 3797 | * ever be fulfilled. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3798 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3799 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3800 | QCBOR_Private_CheckTagRequirement(const QCBOR_Private_TagSpec TagSpec, |
| 3801 | const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3802 | { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3803 | const int nItemType = pItem->uDataType; /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3804 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 3805 | |
| 3806 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3807 | /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3808 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 3809 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 3810 | /* There are tags that QCBOR couldn't process on this item and |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3811 | * the caller has told us there should not be. |
| 3812 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3813 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3814 | } |
| 3815 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3816 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3817 | /* Must match the tag number and only the tag */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3818 | return QCBOR_Private_CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3819 | } |
| 3820 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3821 | QCBORError uReturn = QCBOR_Private_CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3822 | if(uReturn == QCBOR_SUCCESS) { |
| 3823 | return QCBOR_SUCCESS; |
| 3824 | } |
| 3825 | |
| 3826 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 3827 | /* Must match the content type and only the content type. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3828 | * There was no match just above so it is a fail. */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3829 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3830 | } |
| 3831 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3832 | /* QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG: If here it can match either the tag or the content |
| 3833 | * and it hasn't matched the content, so the end |
| 3834 | * result is whether it matches the tag. This is |
| 3835 | * the tag optional case that the CBOR standard discourages. |
| 3836 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3837 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3838 | return QCBOR_Private_CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3839 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3840 | #else /* QCBOR_DISABLE_TAGS */ |
| 3841 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 3842 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3843 | } |
| 3844 | |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 3845 | return QCBOR_Private_CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3846 | |
| 3847 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3848 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3849 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3850 | |
| 3851 | /** |
| 3852 | * @brief Get an item by label to match a tag specification. |
| 3853 | * |
| 3854 | * @param[in] pMe The decode context. |
| 3855 | * @param[in] nLabel The label to search map for. |
| 3856 | * @param[in] TagSpec The tag number specification to match. |
| 3857 | * @param[out] pItem The item found. |
| 3858 | * |
| 3859 | * This finds the item with the given label in currently open |
| 3860 | * map. Then checks that its tag number and types matches the tag |
| 3861 | * specification. If not, an error is set in the decode context. |
| 3862 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3863 | static void |
| 3864 | QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 3865 | const int64_t nLabel, |
| 3866 | const QCBOR_Private_TagSpec TagSpec, |
| 3867 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3868 | { |
| 3869 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 3870 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3871 | return; |
| 3872 | } |
| 3873 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3874 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3875 | } |
| 3876 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3877 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3878 | /** |
| 3879 | * @brief Get an item by label to match a tag specification. |
| 3880 | * |
| 3881 | * @param[in] pMe The decode context. |
| 3882 | * @param[in] szLabel The label to search map for. |
| 3883 | * @param[in] TagSpec The tag number specification to match. |
| 3884 | * @param[out] pItem The item found. |
| 3885 | * |
| 3886 | * This finds the item with the given label in currently open |
| 3887 | * map. Then checks that its tag number and types matches the tag |
| 3888 | * specification. If not, an error is set in the decode context. |
| 3889 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3890 | static void |
| 3891 | QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 3892 | const char *szLabel, |
| 3893 | const QCBOR_Private_TagSpec TagSpec, |
| 3894 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3895 | { |
| 3896 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 3897 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3898 | return; |
| 3899 | } |
| 3900 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3901 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3902 | } |
| 3903 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3904 | |
| 3905 | /** |
| 3906 | * @brief Semi-private to get an string by label to match a tag specification. |
| 3907 | * |
| 3908 | * @param[in] pMe The decode context. |
| 3909 | * @param[in] nLabel The label to search map for. |
| 3910 | * @param[in] TagSpec The tag number specification to match. |
| 3911 | * @param[out] pString The string found. |
| 3912 | * |
| 3913 | * This finds the string with the given label in currently open |
| 3914 | * map. Then checks that its tag number and types matches the tag |
| 3915 | * specification. If not, an error is set in the decode context. |
| 3916 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3917 | void |
| 3918 | QCBORDecode_Private_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3919 | const int64_t nLabel, |
| 3920 | const QCBOR_Private_TagSpec TagSpec, |
| 3921 | UsefulBufC *pString) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3922 | { |
| 3923 | QCBORItem Item; |
| 3924 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 3925 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3926 | *pString = Item.val.string; |
| 3927 | } |
| 3928 | } |
| 3929 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3930 | |
| 3931 | /** |
| 3932 | * @brief Semi-private to get an string by label to match a tag specification. |
| 3933 | * |
| 3934 | * @param[in] pMe The decode context. |
| 3935 | * @param[in] szLabel The label to search map for. |
| 3936 | * @param[in] TagSpec The tag number specification to match. |
| 3937 | * @param[out] pString The string found. |
| 3938 | * |
| 3939 | * This finds the string with the given label in currently open |
| 3940 | * map. Then checks that its tag number and types matches the tag |
| 3941 | * specification. If not, an error is set in the decode context. |
| 3942 | */void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3943 | QCBORDecode_Private_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3944 | const char * szLabel, |
| 3945 | const QCBOR_Private_TagSpec TagSpec, |
| 3946 | UsefulBufC *pString) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3947 | { |
| 3948 | QCBORItem Item; |
| 3949 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 3950 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3951 | *pString = Item.val.string; |
| 3952 | } |
| 3953 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3954 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3955 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3956 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3957 | * Public function, see header qcbor/qcbor_decode.h file |
| 3958 | */ |
| 3959 | void |
| 3960 | QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3961 | { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3962 | QCBORError uErr = QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, NULL); |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3963 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3964 | } |
| 3965 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3966 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3967 | * Public function, see header qcbor/qcbor_decode.h file |
| 3968 | */ |
| 3969 | void |
| 3970 | QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 3971 | QCBORItem *pItemList, |
| 3972 | void *pCallbackCtx, |
| 3973 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3974 | { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3975 | MapSearchCallBack CallBack; |
| 3976 | CallBack.pCBContext = pCallbackCtx; |
| 3977 | CallBack.pfCallback = pfCB; |
| 3978 | |
| 3979 | QCBORError uErr = QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, &CallBack); |
| 3980 | |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3981 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3985 | /** |
| 3986 | * @brief Search for a map/array by label and enter it |
| 3987 | * |
| 3988 | * @param[in] pMe The decode context. |
| 3989 | * @param[in] pSearch The map/array to search for. |
| 3990 | * |
| 3991 | * @c pSearch is expected to contain one item of type map or array |
| 3992 | * with the label specified. The current bounded map will be searched for |
| 3993 | * this and if found will be entered. |
| 3994 | * |
| 3995 | * If the label is not found, or the item found is not a map or array, |
| 3996 | * the error state is set. |
| 3997 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3998 | static void |
| 3999 | QCBORDecode_Private_SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4000 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 4001 | // The first item in pSearch is the one that is to be |
| 4002 | // entered. It should be the only one filled in. Any other |
| 4003 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4004 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4005 | return; |
| 4006 | } |
| 4007 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 4008 | MapSearchInfo Info; |
| 4009 | pMe->uLastError = (uint8_t)QCBORDecode_Private_MapSearch(pMe, pSearch, &Info, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4010 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4011 | return; |
| 4012 | } |
| 4013 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4014 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4015 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4016 | return; |
| 4017 | } |
| 4018 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4019 | |
| 4020 | /* The map or array was found. Now enter it. |
| 4021 | * |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4022 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 4023 | * next item for the pre-order traversal cursor to be the map/array |
| 4024 | * found by MapSearch(). The next few lines of code force the |
| 4025 | * cursor to that. |
| 4026 | * |
| 4027 | * There is no need to retain the old cursor because |
| 4028 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 4029 | * beginning of the map/array being entered. |
| 4030 | * |
| 4031 | * The cursor is forced by: 1) setting the input buffer position to |
| 4032 | * the item offset found by MapSearch(), 2) setting the map/array |
| 4033 | * counter to the total in the map/array, 3) setting the nesting |
| 4034 | * level. Setting the map/array counter to the total is not |
| 4035 | * strictly correct, but this is OK because this cursor only needs |
| 4036 | * to be used to get one item and MapSearch() has already found it |
| 4037 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4038 | */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 4039 | UsefulInputBuf_Seek(&(pMe->InBuf), Info.uStartOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4040 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4041 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 4042 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4043 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 4044 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4045 | QCBORDecode_Private_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4046 | } |
| 4047 | |
| 4048 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4049 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4050 | * Public function, see header qcbor/qcbor_decode.h file |
| 4051 | */ |
| 4052 | void |
| 4053 | QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4054 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 4055 | QCBORItem OneItemSeach[2]; |
| 4056 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 4057 | OneItemSeach[0].label.int64 = nLabel; |
| 4058 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 4059 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4060 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4061 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4062 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4066 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4067 | * Public function, see header qcbor/qcbor_decode.h file |
| 4068 | */ |
| 4069 | void |
| 4070 | QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4071 | { |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 4072 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 4073 | QCBORItem OneItemSeach[2]; |
| 4074 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 4075 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 4076 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 4077 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4078 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4079 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 4080 | #else |
Laurence Lundblade | 9533c48 | 2024-06-06 21:41:10 -0700 | [diff] [blame] | 4081 | (void)szLabel; |
| 4082 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 4083 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4084 | } |
| 4085 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4086 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4087 | * Public function, see header qcbor/qcbor_decode.h file |
| 4088 | */ |
| 4089 | void |
| 4090 | QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4091 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 4092 | QCBORItem OneItemSeach[2]; |
| 4093 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 4094 | OneItemSeach[0].label.int64 = nLabel; |
| 4095 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 4096 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4097 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4098 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4099 | } |
| 4100 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4101 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4102 | * Public function, see header qcbor/qcbor_decode.h file |
| 4103 | */ |
| 4104 | void |
| 4105 | QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4106 | { |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 4107 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 4108 | QCBORItem OneItemSeach[2]; |
| 4109 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 4110 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 4111 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 4112 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4113 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4114 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 4115 | #else |
Laurence Lundblade | 9533c48 | 2024-06-06 21:41:10 -0700 | [diff] [blame] | 4116 | (void)szLabel; |
| 4117 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 4118 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4122 | /** |
| 4123 | * @brief Semi-private to do the the work for EnterMap() and EnterArray(). |
| 4124 | * |
| 4125 | * @param[in] pMe The decode context |
| 4126 | * @param[in] uType QCBOR_TYPE_MAP or QCBOR_TYPE_ARRAY. |
| 4127 | * @param[out] pItem The data item for the map or array entered. |
| 4128 | * |
| 4129 | * The next item in the traversal must be a map or array. This |
| 4130 | * consumes that item and does the book keeping to enter the map or |
| 4131 | * array. |
| 4132 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4133 | void |
| 4134 | QCBORDecode_Private_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, |
| 4135 | const uint8_t uType, |
| 4136 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4137 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4138 | QCBORError uErr; |
| 4139 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4140 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4141 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4142 | // Already in error state; do nothing. |
| 4143 | return; |
| 4144 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4145 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 4146 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4147 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4148 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 4149 | if(uErr != QCBOR_SUCCESS) { |
| 4150 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 4151 | } |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 4152 | |
| 4153 | uint8_t uItemDataType = Item.uDataType; |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 4154 | |
Laurence Lundblade | 62cae93 | 2024-06-03 13:16:17 -0700 | [diff] [blame] | 4155 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 4156 | if(uItemDataType == QCBOR_TYPE_MAP_AS_ARRAY ) { |
| 4157 | uItemDataType = QCBOR_TYPE_ARRAY; |
| 4158 | } |
Laurence Lundblade | 78a6613 | 2024-06-06 12:19:59 -0700 | [diff] [blame] | 4159 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 9560697 | 2024-06-07 11:32:12 -0700 | [diff] [blame] | 4160 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 4161 | if(uItemDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4162 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 4163 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4164 | } |
| 4165 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4166 | QCBORDecode_Private_CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 4167 | |
| 4168 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 4169 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 4170 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4171 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 4172 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4173 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4174 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 4175 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4176 | // Special case to increment nesting level for zero-length maps |
| 4177 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 4178 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 4179 | } |
| 4180 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 4181 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4182 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4183 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 4184 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4185 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4186 | if(pItem != NULL) { |
| 4187 | *pItem = Item; |
| 4188 | } |
| 4189 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4190 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4191 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4192 | } |
| 4193 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4194 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4195 | /** |
| 4196 | * @brief Exit a bounded map, array or bstr (semi-private). |
| 4197 | * |
| 4198 | * @param[in] pMe Decode context. |
| 4199 | * @param[in] uEndOffset The input buffer offset of the end of item exited. |
| 4200 | * |
| 4201 | * @returns QCBOR_SUCCESS or an error code. |
| 4202 | * |
| 4203 | * This is the common work for exiting a level that is a bounded map, |
| 4204 | * array or bstr wrapped CBOR. |
| 4205 | * |
| 4206 | * One chunk of work is to set up the pre-order traversal so it is at |
| 4207 | * the item just after the bounded map, array or bstr that is being |
| 4208 | * exited. This is somewhat complex. |
| 4209 | * |
| 4210 | * The other work is to level-up the bounded mode to next higest |
| 4211 | * bounded mode or the top level if there isn't one. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4212 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4213 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4214 | QCBORDecode_Private_ExitBoundedLevel(QCBORDecodeContext *pMe, |
| 4215 | const uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4216 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4217 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4218 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4219 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4220 | * First the pre-order-traversal byte offset is positioned to the |
| 4221 | * item just after the bounded mode item that was just consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4222 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4223 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 4224 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4225 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4226 | * Next, set the current nesting level to one above the bounded |
| 4227 | * level that was just exited. |
| 4228 | * |
| 4229 | * DecodeNesting_CheckBoundedType() is always called before this |
| 4230 | * and makes sure pCurrentBounded is valid. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4231 | */ |
| 4232 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 4233 | |
| 4234 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4235 | * This does the complex work of leveling up the pre-order |
| 4236 | * traversal when the end of a map or array or another bounded |
| 4237 | * level is reached. It may do nothing, or ascend all the way to |
| 4238 | * the top level. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4239 | */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 4240 | uErr = QCBORDecode_Private_NestLevelAscender(pMe, NULL, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4241 | if(uErr != QCBOR_SUCCESS) { |
| 4242 | goto Done; |
| 4243 | } |
| 4244 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4245 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4246 | * This makes the next highest bounded level the current bounded |
| 4247 | * level. If there is no next highest level, then no bounded mode |
| 4248 | * is in effect. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4249 | */ |
| 4250 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4251 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 4252 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4253 | |
| 4254 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4255 | return uErr; |
| 4256 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4257 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4258 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4259 | /** |
| 4260 | * @brief Get started exiting a map or array (semi-private) |
| 4261 | * |
| 4262 | * @param[in] pMe The decode context |
| 4263 | * @param[in] uType QCBOR_TYPE_ARRAY or QCBOR_TYPE_MAP |
| 4264 | * |
| 4265 | * This does some work for map and array exiting (but not |
| 4266 | * bstr exiting). Then QCBORDecode_Private_ExitBoundedLevel() |
| 4267 | * is called to do the rest. |
| 4268 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4269 | void |
| 4270 | QCBORDecode_Private_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, |
| 4271 | const uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4272 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4273 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4274 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4275 | return; |
| 4276 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4277 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4278 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4279 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 4280 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4281 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4282 | goto Done; |
| 4283 | } |
| 4284 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4285 | /* |
| 4286 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4287 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4288 | from previous map search, then do a dummy search. |
| 4289 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 4290 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4291 | QCBORItem Dummy; |
| 4292 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 4293 | uErr = QCBORDecode_Private_MapSearch(pMe, &Dummy, NULL, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4294 | if(uErr != QCBOR_SUCCESS) { |
| 4295 | goto Done; |
| 4296 | } |
| 4297 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4298 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4299 | uErr = QCBORDecode_Private_ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4300 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4301 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4302 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4306 | /** |
| 4307 | * @brief The main work of entering some byte-string wrapped CBOR. |
| 4308 | * |
| 4309 | * @param[in] pMe The decode context. |
| 4310 | * @param[in] pItem The byte string item. |
| 4311 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX |
| 4312 | * @param[out] pBstr Pointer and length of byte string entered. |
| 4313 | * |
| 4314 | * This is called once the byte string item has been decoded to do all |
| 4315 | * the book keeping work for descending a nesting level into the |
| 4316 | * nested CBOR. |
| 4317 | * |
| 4318 | * See QCBORDecode_EnterBstrWrapped() for details on uTagRequirement. |
| 4319 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4320 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4321 | QCBORDecode_Private_EnterBstrWrapped(QCBORDecodeContext *pMe, |
| 4322 | const QCBORItem *pItem, |
| 4323 | const uint8_t uTagRequirement, |
| 4324 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4325 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4326 | if(pBstr) { |
| 4327 | *pBstr = NULLUsefulBufC; |
| 4328 | } |
| 4329 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4330 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4331 | /* Already in error state; do nothing. */ |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4332 | return pMe->uLastError; |
| 4333 | } |
| 4334 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4335 | QCBORError uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4336 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4337 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4338 | { |
| 4339 | uTagRequirement, |
| 4340 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 4341 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 4342 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4343 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4344 | uError = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4345 | if(uError != QCBOR_SUCCESS) { |
| 4346 | goto Done; |
| 4347 | } |
| 4348 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4349 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4350 | /* Reverse the decrement done by GetNext() for the bstr so the |
| 4351 | * increment in QCBORDecode_NestLevelAscender() called by |
| 4352 | * ExitBoundedLevel() will work right. |
| 4353 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4354 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 4355 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4356 | |
| 4357 | if(pBstr) { |
| 4358 | *pBstr = pItem->val.string; |
| 4359 | } |
| 4360 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4361 | /* This saves the current length of the UsefulInputBuf and then |
| 4362 | * narrows the UsefulInputBuf to start and length of the wrapped |
| 4363 | * CBOR that is being entered. |
| 4364 | * |
| 4365 | * Most of these calls are simple inline accessors so this doesn't |
| 4366 | * amount to much code. |
| 4367 | */ |
| 4368 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 4369 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4370 | /* This check makes the cast of uPreviousLength to uint32_t below safe. */ |
| 4371 | if(uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4372 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 4373 | goto Done; |
| 4374 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4375 | |
| 4376 | const size_t uStartOfBstr = UsefulInputBuf_PointerToOffset(&(pMe->InBuf), |
| 4377 | pItem->val.string.ptr); |
| 4378 | /* This check makes the cast of uStartOfBstr to uint32_t below safe. */ |
| 4379 | if(uStartOfBstr == SIZE_MAX || uStartOfBstr > QCBOR_MAX_DECODE_INPUT_SIZE) { |
| 4380 | /* This should never happen because pItem->val.string.ptr should |
| 4381 | * always be valid since it was just returned. |
| 4382 | */ |
| 4383 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
| 4384 | goto Done; |
| 4385 | } |
| 4386 | |
| 4387 | const size_t uEndOfBstr = uStartOfBstr + pItem->val.string.len; |
| 4388 | |
| 4389 | UsefulInputBuf_Seek(&(pMe->InBuf), uStartOfBstr); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 4390 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4391 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4392 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 4393 | (uint32_t)uPreviousLength, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4394 | (uint32_t)uStartOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4395 | Done: |
| 4396 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4397 | } |
| 4398 | |
| 4399 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4400 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4401 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4402 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4403 | void |
| 4404 | QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
| 4405 | const uint8_t uTagRequirement, |
| 4406 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4407 | { |
| 4408 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4409 | // Already in error state; do nothing. |
| 4410 | return; |
| 4411 | } |
| 4412 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4413 | /* Get the data item that is the byte string being entered */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4414 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4415 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4416 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4417 | return; |
| 4418 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4419 | |
Laurence Lundblade | 31fddb7 | 2024-05-13 13:03:35 -0700 | [diff] [blame] | 4420 | if(Item.uDataAlloc) { |
| 4421 | pMe->uLastError = QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING; |
| 4422 | return; |
| 4423 | } |
| 4424 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4425 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4426 | &Item, |
| 4427 | uTagRequirement, |
| 4428 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4429 | } |
| 4430 | |
| 4431 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4432 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4433 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4434 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4435 | void |
| 4436 | QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
| 4437 | const int64_t nLabel, |
| 4438 | const uint8_t uTagRequirement, |
| 4439 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4440 | { |
| 4441 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4442 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4443 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4444 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4445 | &Item, |
| 4446 | uTagRequirement, |
| 4447 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4448 | } |
| 4449 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4450 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4451 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4452 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4453 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4454 | void |
| 4455 | QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
| 4456 | const char *szLabel, |
| 4457 | const uint8_t uTagRequirement, |
| 4458 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4459 | { |
| 4460 | QCBORItem Item; |
| 4461 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 4462 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4463 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4464 | &Item, |
| 4465 | uTagRequirement, |
| 4466 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4467 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4468 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4469 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4470 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4471 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4472 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4473 | void |
| 4474 | QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4475 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4476 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4477 | // Already in error state; do nothing. |
| 4478 | return; |
| 4479 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4480 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 4481 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4482 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4483 | return; |
| 4484 | } |
| 4485 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4486 | const uint32_t uEndOfBstr = (uint32_t)UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
| 4487 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4488 | /* |
| 4489 | Reset the length of the UsefulInputBuf to what it was before |
| 4490 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4491 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 4492 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4493 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4494 | |
| 4495 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4496 | QCBORError uErr = QCBORDecode_Private_ExitBoundedLevel(pMe, uEndOfBstr); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4497 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4498 | } |
| 4499 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4500 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4501 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4502 | /** |
| 4503 | * @brief Process simple type true and false, a boolean |
| 4504 | * |
| 4505 | * @param[in] pMe The decode context. |
| 4506 | * @param[in] pItem The item with either true or false. |
| 4507 | * @param[out] pBool The boolean value output. |
| 4508 | * |
| 4509 | * Sets the internal error if the item isn't a true or a false. Also |
| 4510 | * records any tag numbers as the tag numbers of the last item. |
| 4511 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4512 | static void |
| 4513 | QCBORDecode_Private_ProcessBool(QCBORDecodeContext *pMe, |
| 4514 | const QCBORItem *pItem, |
| 4515 | bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4516 | { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4517 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4518 | /* Already in error state, do nothing */ |
| 4519 | return; |
| 4520 | } |
| 4521 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4522 | switch(pItem->uDataType) { |
| 4523 | case QCBOR_TYPE_TRUE: |
| 4524 | *pBool = true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4525 | break; |
| 4526 | |
| 4527 | case QCBOR_TYPE_FALSE: |
| 4528 | *pBool = false; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4529 | break; |
| 4530 | |
| 4531 | default: |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4532 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4533 | break; |
| 4534 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4535 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4536 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4537 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4538 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4539 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4540 | * Public function, see header qcbor/qcbor_decode.h file |
| 4541 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4542 | void |
| 4543 | QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4544 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4545 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4546 | /* Already in error state, do nothing */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4547 | return; |
| 4548 | } |
| 4549 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4550 | QCBORItem Item; |
| 4551 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4552 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4553 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4554 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4555 | } |
| 4556 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4557 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4558 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4559 | * Public function, see header qcbor/qcbor_decode.h file |
| 4560 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4561 | void |
| 4562 | QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, |
| 4563 | const int64_t nLabel, |
| 4564 | bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4565 | { |
| 4566 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4567 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4568 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4569 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4570 | } |
| 4571 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4572 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4573 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4574 | * Public function, see header qcbor/qcbor_decode.h file |
| 4575 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4576 | void |
| 4577 | QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, |
| 4578 | const char *szLabel, |
| 4579 | bool *pValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4580 | { |
| 4581 | QCBORItem Item; |
| 4582 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 4583 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4584 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4585 | } |
| 4586 | |
| 4587 | |
| 4588 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4589 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4590 | /** |
| 4591 | * @brief Common processing for an epoch date. |
| 4592 | * |
| 4593 | * @param[in] pMe The decode context. |
| 4594 | * @param[in] pItem The item with the date. |
| 4595 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4596 | * @param[out] pnTime The returned date. |
| 4597 | * |
| 4598 | * Common processing for the date tag. Mostly make sure the tag |
| 4599 | * content is correct and copy forward any further other tag numbers. |
| 4600 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4601 | static void |
| 4602 | QCBORDecode_Private_ProcessEpochDate(QCBORDecodeContext *pMe, |
| 4603 | QCBORItem *pItem, |
| 4604 | const uint8_t uTagRequirement, |
| 4605 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4606 | { |
| 4607 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4608 | // Already in error state, do nothing |
| 4609 | return; |
| 4610 | } |
| 4611 | |
| 4612 | QCBORError uErr; |
| 4613 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4614 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4615 | { |
| 4616 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4617 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4618 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4619 | }; |
| 4620 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4621 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4622 | if(uErr != QCBOR_SUCCESS) { |
| 4623 | goto Done; |
| 4624 | } |
| 4625 | |
| 4626 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4627 | uErr = QCBOR_Private_DecodeDateEpoch(pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4628 | if(uErr != QCBOR_SUCCESS) { |
| 4629 | goto Done; |
| 4630 | } |
| 4631 | } |
| 4632 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4633 | // Save the tags in the last item's tags in the decode context |
| 4634 | // for QCBORDecode_GetNthTagOfLast() |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4635 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4636 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4637 | *pnTime = pItem->val.epochDate.nSeconds; |
| 4638 | |
| 4639 | Done: |
| 4640 | pMe->uLastError = (uint8_t)uErr; |
| 4641 | } |
| 4642 | |
| 4643 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4644 | |
| 4645 | /* |
| 4646 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 4647 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4648 | void |
| 4649 | QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
| 4650 | uint8_t uTagRequirement, |
| 4651 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4652 | { |
| 4653 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4654 | // Already in error state, do nothing |
| 4655 | return; |
| 4656 | } |
| 4657 | |
| 4658 | QCBORItem Item; |
| 4659 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4660 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4661 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4662 | } |
| 4663 | |
| 4664 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4665 | /* |
| 4666 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 4667 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4668 | void |
| 4669 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 4670 | int64_t nLabel, |
| 4671 | uint8_t uTagRequirement, |
| 4672 | int64_t *pnTime) |
| 4673 | { |
| 4674 | QCBORItem Item; |
| 4675 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4676 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4677 | } |
| 4678 | |
| 4679 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4680 | /* |
| 4681 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 4682 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4683 | void |
| 4684 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 4685 | const char *szLabel, |
| 4686 | uint8_t uTagRequirement, |
| 4687 | int64_t *pnTime) |
| 4688 | { |
| 4689 | QCBORItem Item; |
| 4690 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4691 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | |
| 4695 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4696 | /** |
| 4697 | * @brief Common processing for an epoch date. |
| 4698 | * |
| 4699 | * @param[in] pMe The decode context. |
| 4700 | * @param[in] pItem The item with the date. |
| 4701 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4702 | * @param[out] pnDays The returned day count. |
| 4703 | * |
| 4704 | * Common processing for the RFC 8943 day-count tag. Mostly make sure |
| 4705 | * the tag content is correct and copy forward any further other tag |
| 4706 | * numbers. |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4707 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4708 | static void |
| 4709 | QCBORDecode_Private_ProcessEpochDays(QCBORDecodeContext *pMe, |
| 4710 | QCBORItem *pItem, |
| 4711 | uint8_t uTagRequirement, |
| 4712 | int64_t *pnDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4713 | { |
| 4714 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4715 | /* Already in error state, do nothing */ |
| 4716 | return; |
| 4717 | } |
| 4718 | |
| 4719 | QCBORError uErr; |
| 4720 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4721 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4722 | { |
| 4723 | uTagRequirement, |
| 4724 | {QCBOR_TYPE_DAYS_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4725 | {QCBOR_TYPE_INT64, QCBOR_TYPE_UINT64, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 4726 | }; |
| 4727 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4728 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4729 | if(uErr != QCBOR_SUCCESS) { |
| 4730 | goto Done; |
| 4731 | } |
| 4732 | |
| 4733 | if(pItem->uDataType != QCBOR_TYPE_DAYS_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4734 | uErr = QCBOR_Private_DecodeDaysEpoch(pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4735 | if(uErr != QCBOR_SUCCESS) { |
| 4736 | goto Done; |
| 4737 | } |
| 4738 | } |
| 4739 | |
| 4740 | /* Save the tags in the last item's tags in the decode context |
| 4741 | * for QCBORDecode_GetNthTagOfLast() |
| 4742 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4743 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4744 | |
| 4745 | *pnDays = pItem->val.epochDays; |
| 4746 | |
| 4747 | Done: |
| 4748 | pMe->uLastError = (uint8_t)uErr; |
| 4749 | } |
| 4750 | |
| 4751 | |
| 4752 | /* |
| 4753 | * Public function, see header qcbor/qcbor_decode.h |
| 4754 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4755 | void |
| 4756 | QCBORDecode_GetEpochDays(QCBORDecodeContext *pMe, |
| 4757 | uint8_t uTagRequirement, |
| 4758 | int64_t *pnDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4759 | { |
| 4760 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4761 | /* Already in error state, do nothing */ |
| 4762 | return; |
| 4763 | } |
| 4764 | |
| 4765 | QCBORItem Item; |
| 4766 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4767 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4768 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | |
| 4772 | /* |
| 4773 | * Public function, see header qcbor/qcbor_decode.h |
| 4774 | */ |
| 4775 | void |
| 4776 | QCBORDecode_GetEpochDaysInMapN(QCBORDecodeContext *pMe, |
| 4777 | int64_t nLabel, |
| 4778 | uint8_t uTagRequirement, |
| 4779 | int64_t *pnDays) |
| 4780 | { |
| 4781 | QCBORItem Item; |
| 4782 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4783 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4784 | } |
| 4785 | |
| 4786 | |
| 4787 | /* |
| 4788 | * Public function, see header qcbor/qcbor_decode.h |
| 4789 | */ |
| 4790 | void |
| 4791 | QCBORDecode_GetEpochDaysInMapSZ(QCBORDecodeContext *pMe, |
| 4792 | const char *szLabel, |
| 4793 | uint8_t uTagRequirement, |
| 4794 | int64_t *pnDays) |
| 4795 | { |
| 4796 | QCBORItem Item; |
| 4797 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4798 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | |
| 4802 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4803 | /* |
| 4804 | * @brief Get a string that matches the type/tag specification. |
| 4805 | */ |
| 4806 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4807 | QCBORDecode_Private_GetTaggedString(QCBORDecodeContext *pMe, |
| 4808 | const QCBOR_Private_TagSpec TagSpec, |
| 4809 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4810 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4811 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4812 | /* Already in error state, do nothing */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4813 | return; |
| 4814 | } |
| 4815 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4816 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4817 | QCBORItem Item; |
| 4818 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4819 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 4820 | if(uError != QCBOR_SUCCESS) { |
| 4821 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4822 | return; |
| 4823 | } |
| 4824 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4825 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4826 | |
| 4827 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4828 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 4829 | } else { |
| 4830 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4831 | } |
| 4832 | } |
| 4833 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4834 | |
| 4835 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4836 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4837 | /** |
| 4838 | * @brief Common processing for a big number tag. |
| 4839 | * |
| 4840 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4841 | * @param[in] pItem The item with the date. |
| 4842 | * @param[out] pValue The returned big number |
| 4843 | * @param[out] pbIsNegative The returned sign of the big number. |
| 4844 | * |
| 4845 | * Common processing for the big number tag. Mostly make sure |
| 4846 | * the tag content is correct and copy forward any further other tag |
| 4847 | * numbers. |
| 4848 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4849 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4850 | QCBOR_Private_ProcessBigNum(const uint8_t uTagRequirement, |
| 4851 | const QCBORItem *pItem, |
| 4852 | UsefulBufC *pValue, |
| 4853 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4854 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4855 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4856 | { |
| 4857 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4858 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4859 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4860 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4861 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4862 | QCBORError uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4863 | if(uErr != QCBOR_SUCCESS) { |
| 4864 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4865 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4866 | |
| 4867 | *pValue = pItem->val.string; |
| 4868 | |
| 4869 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 4870 | *pbIsNegative = false; |
| 4871 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 4872 | *pbIsNegative = true; |
| 4873 | } |
| 4874 | |
| 4875 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4879 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4880 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4881 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4882 | void |
| 4883 | QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 4884 | const uint8_t uTagRequirement, |
| 4885 | UsefulBufC *pValue, |
| 4886 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4887 | { |
| 4888 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4889 | // Already in error state, do nothing |
| 4890 | return; |
| 4891 | } |
| 4892 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4893 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4894 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4895 | if(uError != QCBOR_SUCCESS) { |
| 4896 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4897 | return; |
| 4898 | } |
| 4899 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4900 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4901 | &Item, |
| 4902 | pValue, |
| 4903 | pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4904 | } |
| 4905 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4906 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4907 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4908 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
| 4909 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4910 | void |
| 4911 | QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 4912 | const int64_t nLabel, |
| 4913 | const uint8_t uTagRequirement, |
| 4914 | UsefulBufC *pValue, |
| 4915 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4916 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4917 | QCBORItem Item; |
| 4918 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4919 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4920 | return; |
| 4921 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4922 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4923 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4924 | &Item, |
| 4925 | pValue, |
| 4926 | pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4927 | } |
| 4928 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4929 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4930 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4931 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4932 | */ |
| 4933 | void |
| 4934 | QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 4935 | const char *szLabel, |
| 4936 | const uint8_t uTagRequirement, |
| 4937 | UsefulBufC *pValue, |
| 4938 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4939 | { |
| 4940 | QCBORItem Item; |
| 4941 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4942 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4943 | return; |
| 4944 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4945 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4946 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4947 | &Item, |
| 4948 | pValue, |
| 4949 | pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | |
| 4953 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4954 | /** |
| 4955 | * @brief Common processing for MIME tag (semi-private). |
| 4956 | * |
| 4957 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4958 | * @param[in] pItem The item with the date. |
| 4959 | * @param[out] pMessage The returned MIME message. |
| 4960 | * @param[out] pbIsTag257 If true, binary MIME, if not, text MIME. |
| 4961 | * |
| 4962 | * Common processing for the MIME tag. Mostly make sure the tag |
| 4963 | * content is correct and copy forward any further other tag |
| 4964 | * numbers. See QCBORDecode_GetMIMEMessage(). |
| 4965 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4966 | QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4967 | QCBORDecode_Private_GetMIME(const uint8_t uTagRequirement, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4968 | const QCBORItem *pItem, |
| 4969 | UsefulBufC *pMessage, |
| 4970 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4971 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4972 | const QCBOR_Private_TagSpec TagSpecText = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4973 | { |
| 4974 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4975 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4976 | {QCBOR_TYPE_TEXT_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4977 | }; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4978 | const QCBOR_Private_TagSpec TagSpecBinary = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4979 | { |
| 4980 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4981 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4982 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4983 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4984 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4985 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4986 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4987 | if(QCBOR_Private_CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4988 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4989 | if(pbIsTag257 != NULL) { |
| 4990 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4991 | } |
| 4992 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4993 | } else if(QCBOR_Private_CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4994 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4995 | if(pbIsTag257 != NULL) { |
| 4996 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4997 | } |
| 4998 | uReturn = QCBOR_SUCCESS; |
| 4999 | |
| 5000 | } else { |
| 5001 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 5002 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 5003 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5004 | return uReturn; |
| 5005 | } |
| 5006 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5007 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 5008 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 5009 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5010 | |
| 5011 | |
| 5012 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5013 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5014 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5015 | /** |
| 5016 | * @brief Prototype for conversion of exponent and mantissa to unsigned integer. |
| 5017 | * |
| 5018 | * @param[in] uMantissa The mantissa. |
| 5019 | * @param[in] nExponent The exponent. |
| 5020 | * @param[out] puResult The resulting integer. |
| 5021 | * |
| 5022 | * Concrete implementations of this are for exponent base 10 and 2 supporting |
| 5023 | * decimal fractions and big floats. |
| 5024 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5025 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5026 | |
| 5027 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5028 | /** |
| 5029 | * @brief Base 10 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 5030 | * |
| 5031 | * @param[in] uMantissa The unsigned integer mantissa. |
| 5032 | * @param[in] nExponent The signed integer exponent. |
| 5033 | * @param[out] puResult Place to return the unsigned integer result. |
| 5034 | * |
| 5035 | * This computes: mantissa * 10 ^^ exponent as for a decimal fraction. The output is a 64-bit |
| 5036 | * unsigned integer. |
| 5037 | * |
| 5038 | * There are many inputs for which the result will not fit in the |
| 5039 | * 64-bit integer and \ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
| 5040 | * be returned. |
| 5041 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5042 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5043 | QCBOR_Private_Exponentitate10(const uint64_t uMantissa, |
| 5044 | int64_t nExponent, |
| 5045 | uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5046 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5047 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5048 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5049 | if(uResult != 0) { |
| 5050 | /* This loop will run a maximum of 19 times because |
| 5051 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 5052 | * exit with the overflow error |
| 5053 | */ |
| 5054 | for(; nExponent > 0; nExponent--) { |
| 5055 | if(uResult > UINT64_MAX / 10) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5056 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5057 | } |
| 5058 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5059 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5060 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5061 | for(; nExponent < 0; nExponent++) { |
| 5062 | uResult = uResult / 10; |
| 5063 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5064 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5065 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5066 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5067 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5068 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5069 | |
| 5070 | *puResult = uResult; |
| 5071 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5072 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5073 | } |
| 5074 | |
| 5075 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5076 | /** |
| 5077 | * @brief Base 2 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 5078 | * |
| 5079 | * @param[in] uMantissa The unsigned integer mantissa. |
| 5080 | * @param[in] nExponent The signed integer exponent. |
| 5081 | * @param[out] puResult Place to return the unsigned integer result. |
| 5082 | * |
| 5083 | * This computes: mantissa * 2 ^^ exponent as for a big float. The |
| 5084 | * output is a 64-bit unsigned integer. |
| 5085 | * |
| 5086 | * There are many inputs for which the result will not fit in the |
| 5087 | * 64-bit integer and \ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
| 5088 | * be returned. |
| 5089 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5090 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5091 | QCBOR_Private_Exponentitate2(const uint64_t uMantissa, |
| 5092 | int64_t nExponent, |
| 5093 | uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5094 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5095 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5096 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5097 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5098 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5099 | /* This loop will run a maximum of 64 times because INT64_MAX < |
| 5100 | * 2^31. More than that will cause exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5101 | */ |
| 5102 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5103 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5104 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5105 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5106 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5107 | nExponent--; |
| 5108 | } |
| 5109 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5110 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5111 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5112 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5113 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5114 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5115 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5116 | } |
| 5117 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5118 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5119 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5120 | return QCBOR_SUCCESS; |
| 5121 | } |
| 5122 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5123 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5124 | /** |
| 5125 | * @brief Exponentiate a signed mantissa and signed exponent to produce a signed result. |
| 5126 | * |
| 5127 | * @param[in] nMantissa Signed integer mantissa. |
| 5128 | * @param[in] nExponent Signed integer exponent. |
| 5129 | * @param[out] pnResult Place to put the signed integer result. |
| 5130 | * @param[in] pfExp Exponentiation function. |
| 5131 | * |
| 5132 | * @returns Error code |
| 5133 | * |
| 5134 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 5135 | * produces an unsigned result. This converts the mantissa from signed |
| 5136 | * and converts the result to signed. The exponentiation function is |
| 5137 | * either for base 2 or base 10 (and could be other if needed). |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5138 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5139 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5140 | QCBOR_Private_ExponentiateNN(const int64_t nMantissa, |
| 5141 | const int64_t nExponent, |
| 5142 | int64_t *pnResult, |
| 5143 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5144 | { |
| 5145 | uint64_t uResult; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5146 | uint64_t uMantissa; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5147 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5148 | /* Take the absolute value and put it into an unsigned. */ |
| 5149 | if(nMantissa >= 0) { |
| 5150 | /* Positive case is straightforward */ |
| 5151 | uMantissa = (uint64_t)nMantissa; |
| 5152 | } else if(nMantissa != INT64_MIN) { |
| 5153 | /* The common negative case. See next. */ |
| 5154 | uMantissa = (uint64_t)-nMantissa; |
| 5155 | } else { |
| 5156 | /* int64_t and uint64_t are always two's complement per the |
| 5157 | * C standard (and since QCBOR uses these it only works with |
| 5158 | * two's complement, which is pretty much universal these |
| 5159 | * days). The range of a negative two's complement integer is |
| 5160 | * one more that than a positive, so the simple code above might |
| 5161 | * not work all the time because you can't simply negate the |
| 5162 | * value INT64_MIN because it can't be represented in an |
| 5163 | * int64_t. -INT64_MIN can however be represented in a |
| 5164 | * uint64_t. Some compilers seem to recognize this case for the |
| 5165 | * above code and put the correct value in uMantissa, however |
| 5166 | * they are not required to do this by the C standard. This next |
| 5167 | * line does however work for all compilers. |
| 5168 | * |
| 5169 | * This does assume two's complement where -INT64_MIN == |
| 5170 | * INT64_MAX + 1 (which wouldn't be true for one's complement or |
| 5171 | * sign and magnitude (but we know we're using two's complement |
| 5172 | * because int64_t requires it)). |
| 5173 | * |
| 5174 | * See these, particularly the detailed commentary: |
| 5175 | * https://stackoverflow.com/questions/54915742/does-c99-mandate-a-int64-t-type-be-available-always |
| 5176 | * https://stackoverflow.com/questions/37301078/is-negating-int-min-undefined-behaviour |
| 5177 | */ |
| 5178 | uMantissa = (uint64_t)INT64_MAX+1; |
| 5179 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5180 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5181 | /* Call the exponentiator passed for either base 2 or base 10. |
| 5182 | * Here is where most of the overflow errors are caught. */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5183 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 5184 | if(uReturn) { |
| 5185 | return uReturn; |
| 5186 | } |
| 5187 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5188 | /* Convert back to the sign of the original mantissa */ |
| 5189 | if(nMantissa >= 0) { |
| 5190 | if(uResult > INT64_MAX) { |
| 5191 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5192 | } |
| 5193 | *pnResult = (int64_t)uResult; |
| 5194 | } else { |
| 5195 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 5196 | * of INT64_MIN. This assumes two's compliment representation |
| 5197 | * where INT64_MIN is one increment farther from 0 than |
| 5198 | * INT64_MAX. Trying to write -INT64_MIN doesn't work to get |
| 5199 | * this because the compiler makes it an int64_t which can't |
| 5200 | * represent -INT64_MIN. Also see above. |
| 5201 | */ |
| 5202 | if(uResult > (uint64_t)INT64_MAX+1) { |
| 5203 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5204 | } |
| 5205 | *pnResult = -(int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5206 | } |
| 5207 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5208 | return QCBOR_SUCCESS; |
| 5209 | } |
| 5210 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5211 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5212 | /** |
| 5213 | * @brief Exponentiate an unsigned mantissa and signed exponent to produce an unsigned result. |
| 5214 | * |
| 5215 | * @param[in] nMantissa Signed integer mantissa. |
| 5216 | * @param[in] nExponent Signed integer exponent. |
| 5217 | * @param[out] puResult Place to put the signed integer result. |
| 5218 | * @param[in] pfExp Exponentiation function. |
| 5219 | * |
| 5220 | * @returns Error code |
| 5221 | * |
| 5222 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 5223 | * produces an unsigned result. This errors out if the mantissa |
| 5224 | * is negative because the output is unsigned. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5225 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5226 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5227 | QCBOR_Private_ExponentitateNU(const int64_t nMantissa, |
| 5228 | const int64_t nExponent, |
| 5229 | uint64_t *puResult, |
| 5230 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5231 | { |
| 5232 | if(nMantissa < 0) { |
| 5233 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5234 | } |
| 5235 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5236 | /* Cast to unsigned is OK because of check for negative. |
| 5237 | * Cast to unsigned is OK because UINT64_MAX > INT64_MAX. |
| 5238 | * Exponentiation is straight forward |
| 5239 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5240 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 5241 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5242 | |
| 5243 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5244 | /** |
| 5245 | * @brief Exponentiate an usnigned mantissa and unsigned exponent to produce an unsigned result. |
| 5246 | * |
| 5247 | * @param[in] uMantissa Unsigned integer mantissa. |
| 5248 | * @param[in] nExponent Unsigned integer exponent. |
| 5249 | * @param[out] puResult Place to put the unsigned integer result. |
| 5250 | * @param[in] pfExp Exponentiation function. |
| 5251 | * |
| 5252 | * @returns Error code |
| 5253 | * |
| 5254 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 5255 | * produces an unsigned result so this is just a wrapper that does |
| 5256 | * nothing (and is likely inlined). |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5257 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5258 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5259 | QCBOR_Private_ExponentitateUU(const uint64_t uMantissa, |
| 5260 | const int64_t nExponent, |
| 5261 | uint64_t *puResult, |
| 5262 | fExponentiator pfExp) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5263 | { |
| 5264 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 5265 | } |
| 5266 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5267 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5268 | |
| 5269 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5270 | |
| 5271 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5272 | /** |
| 5273 | * @brief Convert a CBOR big number to a uint64_t. |
| 5274 | * |
| 5275 | * @param[in] BigNum Bytes of the big number to convert. |
| 5276 | * @param[in] uMax Maximum value allowed for the result. |
| 5277 | * @param[out] pResult Place to put the unsigned integer result. |
| 5278 | * |
| 5279 | * @returns Error code |
| 5280 | * |
| 5281 | * Many values will overflow because a big num can represent a much |
| 5282 | * larger range than uint64_t. |
| 5283 | */ |
| 5284 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5285 | QCBOR_Private_ConvertBigNumToUnsigned(const UsefulBufC BigNum, |
| 5286 | const uint64_t uMax, |
| 5287 | uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5288 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5289 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5290 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5291 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5292 | const uint8_t *pByte = BigNum.ptr; |
| 5293 | size_t uLen = BigNum.len; |
| 5294 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5295 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5296 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5297 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5298 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5299 | } |
| 5300 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5301 | *pResult = uResult; |
| 5302 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5303 | } |
| 5304 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5305 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5306 | /** |
| 5307 | * @brief Convert a CBOR postive big number to a uint64_t. |
| 5308 | * |
| 5309 | * @param[in] BigNum Bytes of the big number to convert. |
| 5310 | * @param[out] pResult Place to put the unsigned integer result. |
| 5311 | * |
| 5312 | * @returns Error code |
| 5313 | * |
| 5314 | * Many values will overflow because a big num can represent a much |
| 5315 | * larger range than uint64_t. |
| 5316 | */ |
| 5317 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5318 | QCBOR_Private_ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, |
| 5319 | uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5320 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5321 | return QCBOR_Private_ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5322 | } |
| 5323 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5324 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5325 | /** |
| 5326 | * @brief Convert a CBOR positive big number to an int64_t. |
| 5327 | * |
| 5328 | * @param[in] BigNum Bytes of the big number to convert. |
| 5329 | * @param[out] pResult Place to put the signed integer result. |
| 5330 | * |
| 5331 | * @returns Error code |
| 5332 | * |
| 5333 | * Many values will overflow because a big num can represent a much |
| 5334 | * larger range than int64_t. |
| 5335 | */ |
| 5336 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5337 | QCBOR_Private_ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, |
| 5338 | int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5339 | { |
| 5340 | uint64_t uResult; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5341 | QCBORError uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, |
| 5342 | INT64_MAX, |
| 5343 | &uResult); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5344 | if(uError) { |
| 5345 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5346 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5347 | /* Cast is safe because ConvertBigNumToUnsigned limits to INT64_MAX */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5348 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5349 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5350 | } |
| 5351 | |
| 5352 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5353 | /** |
| 5354 | * @brief Convert a CBOR negative big number to an int64_t. |
| 5355 | * |
| 5356 | * @param[in] BigNum Bytes of the big number to convert. |
| 5357 | * @param[out] pnResult Place to put the signed integer result. |
| 5358 | * |
| 5359 | * @returns Error code |
| 5360 | * |
| 5361 | * Many values will overflow because a big num can represent a much |
| 5362 | * larger range than int64_t. |
| 5363 | */ |
| 5364 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5365 | QCBOR_Private_ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, |
| 5366 | int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5367 | { |
| 5368 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5369 | /* The negative integer furthest from zero for a C int64_t is |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5370 | * INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 5371 | * negative number in CBOR is computed as -n - 1 where n is the |
| 5372 | * encoded integer, where n is what is in the variable BigNum. When |
| 5373 | * converting BigNum to a uint64_t, the maximum value is thus |
| 5374 | * INT64_MAX, so that when it -n - 1 is applied to it the result |
| 5375 | * will never be further from 0 than INT64_MIN. |
| 5376 | * |
| 5377 | * -n - 1 <= INT64_MIN. |
| 5378 | * -n - 1 <= -INT64_MAX - 1 |
| 5379 | * n <= INT64_MAX. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5380 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5381 | QCBORError uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, |
| 5382 | INT64_MAX, |
| 5383 | &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5384 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5385 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5386 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5387 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5388 | /* Now apply -n - 1. The cast is safe because |
| 5389 | * ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 5390 | * is the largest positive integer that an int64_t can |
| 5391 | * represent. */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5392 | *pnResult = -(int64_t)uResult - 1; |
| 5393 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5394 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5395 | } |
| 5396 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5397 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5398 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5399 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5400 | /** |
| 5401 | * @brief Convert integers and floats to an int64_t. |
| 5402 | * |
| 5403 | * @param[in] pItem The item to convert. |
| 5404 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5405 | * @param[out] pnValue The resulting converted value. |
| 5406 | * |
| 5407 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5408 | * in uConvertTypes. |
| 5409 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5410 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5411 | * or too small. |
| 5412 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5413 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5414 | QCBOR_Private_ConvertInt64(const QCBORItem *pItem, |
| 5415 | const uint32_t uConvertTypes, |
| 5416 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5417 | { |
| 5418 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5419 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5420 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5421 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5422 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5423 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 5424 | http://www.cplusplus.com/reference/cmath/llround/ |
| 5425 | */ |
| 5426 | // Not interested in FE_INEXACT |
| 5427 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5428 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 5429 | *pnValue = llround(pItem->val.dfnum); |
| 5430 | } else { |
| 5431 | *pnValue = lroundf(pItem->val.fnum); |
| 5432 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5433 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 5434 | // llround() shouldn't result in divide by zero, but catch |
| 5435 | // it here in case it unexpectedly does. Don't try to |
| 5436 | // distinguish between the various exceptions because it seems |
| 5437 | // they vary by CPU, compiler and OS. |
| 5438 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5439 | } |
| 5440 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5441 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5442 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5443 | #else |
| 5444 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5445 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5446 | break; |
| 5447 | |
| 5448 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5449 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5450 | *pnValue = pItem->val.int64; |
| 5451 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5452 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5453 | } |
| 5454 | break; |
| 5455 | |
| 5456 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5457 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5458 | if(pItem->val.uint64 < INT64_MAX) { |
| 5459 | *pnValue = pItem->val.int64; |
| 5460 | } else { |
| 5461 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5462 | } |
| 5463 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5464 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5465 | } |
| 5466 | break; |
| 5467 | |
| 5468 | default: |
| 5469 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5470 | } |
| 5471 | return QCBOR_SUCCESS; |
| 5472 | } |
| 5473 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5474 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5475 | /** |
| 5476 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5477 | * |
| 5478 | * @param[in] pMe The decode context. |
| 5479 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5480 | * @param[out] pnValue Result of the conversion. |
| 5481 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5482 | * |
| 5483 | * See QCBORDecode_GetInt64Convert(). |
| 5484 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5485 | void |
| 5486 | QCBORDecode_Private_GetInt64Convert(QCBORDecodeContext *pMe, |
| 5487 | uint32_t uConvertTypes, |
| 5488 | int64_t *pnValue, |
| 5489 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5490 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 5491 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5492 | return; |
| 5493 | } |
| 5494 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5495 | QCBORError uError = QCBORDecode_GetNext(pMe, pItem); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5496 | if(uError) { |
| 5497 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5498 | return; |
| 5499 | } |
| 5500 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5501 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5502 | uConvertTypes, |
| 5503 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5504 | } |
| 5505 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5506 | /** |
| 5507 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5508 | * |
| 5509 | * @param[in] pMe The decode context. |
| 5510 | * @param[in] nLabel Label to find in map. |
| 5511 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5512 | * @param[out] pnValue Result of the conversion. |
| 5513 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5514 | * |
| 5515 | * See QCBORDecode_GetInt64ConvertInMapN(). |
| 5516 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5517 | void |
| 5518 | QCBORDecode_Private_GetInt64ConvertInMapN(QCBORDecodeContext *pMe, |
| 5519 | int64_t nLabel, |
| 5520 | uint32_t uConvertTypes, |
| 5521 | int64_t *pnValue, |
| 5522 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5523 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5524 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5525 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5526 | return; |
| 5527 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5528 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5529 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
| 5530 | uConvertTypes, |
| 5531 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5532 | } |
| 5533 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5534 | /** |
| 5535 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5536 | * |
| 5537 | * @param[in] pMe The decode context. |
| 5538 | * @param[in] szLabel Label to find in map. |
| 5539 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5540 | * @param[out] pnValue Result of the conversion. |
| 5541 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5542 | * |
| 5543 | * See QCBORDecode_GetInt64ConvertInMapSZ(). |
| 5544 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5545 | void |
| 5546 | QCBORDecode_Private_GetInt64ConvertInMapSZ(QCBORDecodeContext *pMe, |
| 5547 | const char * szLabel, |
| 5548 | uint32_t uConvertTypes, |
| 5549 | int64_t *pnValue, |
| 5550 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5551 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5552 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5553 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5554 | return; |
| 5555 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5556 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5557 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
| 5558 | uConvertTypes, |
| 5559 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5560 | } |
| 5561 | |
| 5562 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5563 | /** |
| 5564 | * @brief Convert many number types to an int64_t. |
| 5565 | * |
| 5566 | * @param[in] pItem The item to convert. |
| 5567 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5568 | * @param[out] pnValue The resulting converted value. |
| 5569 | * |
| 5570 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5571 | * in uConvertTypes. |
| 5572 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5573 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5574 | * or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5575 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5576 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5577 | QCBOR_Private_Int64ConvertAll(const QCBORItem *pItem, |
| 5578 | const uint32_t uConvertTypes, |
| 5579 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5580 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5581 | switch(pItem->uDataType) { |
| 5582 | |
| 5583 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5584 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5585 | return QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5586 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5587 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5588 | } |
| 5589 | break; |
| 5590 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5591 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5592 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5593 | return QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5594 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5595 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5596 | } |
| 5597 | break; |
| 5598 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5599 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5600 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5601 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5602 | return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5603 | pItem->val.expAndMantissa.nExponent, |
| 5604 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5605 | &QCBOR_Private_Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5606 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5607 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5608 | } |
| 5609 | break; |
| 5610 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5611 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5612 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5613 | return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5614 | pItem->val.expAndMantissa.nExponent, |
| 5615 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5616 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5617 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5618 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5619 | } |
| 5620 | break; |
| 5621 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5622 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5623 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5624 | int64_t nMantissa; |
| 5625 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5626 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5627 | if(uErr) { |
| 5628 | return uErr; |
| 5629 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5630 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5631 | pItem->val.expAndMantissa.nExponent, |
| 5632 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5633 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5634 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5635 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5636 | } |
| 5637 | break; |
| 5638 | |
| 5639 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5640 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5641 | int64_t nMantissa; |
| 5642 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5643 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5644 | if(uErr) { |
| 5645 | return uErr; |
| 5646 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5647 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5648 | pItem->val.expAndMantissa.nExponent, |
| 5649 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5650 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5651 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5652 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5653 | } |
| 5654 | break; |
| 5655 | |
| 5656 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5657 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5658 | int64_t nMantissa; |
| 5659 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5660 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5661 | if(uErr) { |
| 5662 | return uErr; |
| 5663 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5664 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5665 | pItem->val.expAndMantissa.nExponent, |
| 5666 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5667 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5668 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5669 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5670 | } |
| 5671 | break; |
| 5672 | |
| 5673 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5674 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5675 | int64_t nMantissa; |
| 5676 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5677 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5678 | if(uErr) { |
| 5679 | return uErr; |
| 5680 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5681 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5682 | pItem->val.expAndMantissa.nExponent, |
| 5683 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5684 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5685 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5686 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5687 | } |
| 5688 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5689 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5690 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5691 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5692 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5693 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5694 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5695 | |
| 5696 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5697 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5698 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5699 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5700 | void |
| 5701 | QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, |
| 5702 | const uint32_t uConvertTypes, |
| 5703 | int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5704 | { |
| 5705 | QCBORItem Item; |
| 5706 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5707 | QCBORDecode_Private_GetInt64Convert(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5708 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5709 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5710 | // The above conversion succeeded |
| 5711 | return; |
| 5712 | } |
| 5713 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5714 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5715 | // The above conversion failed in a way that code below can't correct |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5716 | return; |
| 5717 | } |
| 5718 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5719 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5720 | uConvertTypes, |
| 5721 | pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5722 | } |
| 5723 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5724 | |
| 5725 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5726 | * Public function, see header qcbor/qcbor_decode.h file |
| 5727 | */ |
| 5728 | void |
| 5729 | QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5730 | const int64_t nLabel, |
| 5731 | const uint32_t uConvertTypes, |
| 5732 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5733 | { |
| 5734 | QCBORItem Item; |
| 5735 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5736 | QCBORDecode_Private_GetInt64ConvertInMapN(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5737 | nLabel, |
| 5738 | uConvertTypes, |
| 5739 | pnValue, |
| 5740 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5741 | |
| 5742 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5743 | // The above conversion succeeded |
| 5744 | return; |
| 5745 | } |
| 5746 | |
| 5747 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5748 | // The above conversion failed in a way that code below can't correct |
| 5749 | return; |
| 5750 | } |
| 5751 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5752 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5753 | uConvertTypes, |
| 5754 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5755 | } |
| 5756 | |
| 5757 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5758 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5759 | * Public function, see header qcbor/qcbor_decode.h file |
| 5760 | */ |
| 5761 | void |
| 5762 | QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5763 | const char *szLabel, |
| 5764 | const uint32_t uConvertTypes, |
| 5765 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5766 | { |
| 5767 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5768 | QCBORDecode_Private_GetInt64ConvertInMapSZ(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5769 | szLabel, |
| 5770 | uConvertTypes, |
| 5771 | pnValue, |
| 5772 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5773 | |
| 5774 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5775 | // The above conversion succeeded |
| 5776 | return; |
| 5777 | } |
| 5778 | |
| 5779 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5780 | // The above conversion failed in a way that code below can't correct |
| 5781 | return; |
| 5782 | } |
| 5783 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5784 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5785 | uConvertTypes, |
| 5786 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5787 | } |
| 5788 | |
| 5789 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5790 | /** |
| 5791 | * @brief Convert many number types to an uint64_t. |
| 5792 | * |
| 5793 | * @param[in] pItem The item to convert. |
| 5794 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5795 | * @param[out] puValue The resulting converted value. |
| 5796 | * |
| 5797 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5798 | * in uConvertTypes. |
| 5799 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5800 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5801 | * or too small. |
| 5802 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5803 | static QCBORError |
| 5804 | QCBOR_Private_ConvertUInt64(const QCBORItem *pItem, |
| 5805 | const uint32_t uConvertTypes, |
| 5806 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5807 | { |
| 5808 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5809 | case QCBOR_TYPE_DOUBLE: |
| 5810 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5811 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5812 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5813 | // Can't use llround here because it will not convert values |
| 5814 | // greater than INT64_MAX and less than UINT64_MAX that |
| 5815 | // need to be converted so it is more complicated. |
| 5816 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 5817 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 5818 | if(isnan(pItem->val.dfnum)) { |
| 5819 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5820 | } else if(pItem->val.dfnum < 0) { |
| 5821 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5822 | } else { |
| 5823 | double dRounded = round(pItem->val.dfnum); |
| 5824 | // See discussion in DecodeDateEpoch() for |
| 5825 | // explanation of - 0x7ff |
| 5826 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 5827 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5828 | } |
| 5829 | *puValue = (uint64_t)dRounded; |
| 5830 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5831 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5832 | if(isnan(pItem->val.fnum)) { |
| 5833 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5834 | } else if(pItem->val.fnum < 0) { |
| 5835 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5836 | } else { |
| 5837 | float fRounded = roundf(pItem->val.fnum); |
| 5838 | // See discussion in DecodeDateEpoch() for |
| 5839 | // explanation of - 0x7ff |
| 5840 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 5841 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5842 | } |
| 5843 | *puValue = (uint64_t)fRounded; |
| 5844 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5845 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5846 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 5847 | // round() and roundf() shouldn't result in exceptions here, but |
| 5848 | // catch them to be robust and thorough. Don't try to |
| 5849 | // distinguish between the various exceptions because it seems |
| 5850 | // they vary by CPU, compiler and OS. |
| 5851 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5852 | } |
| 5853 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5854 | } else { |
| 5855 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5856 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5857 | #else |
| 5858 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5859 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5860 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5861 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5862 | case QCBOR_TYPE_INT64: |
| 5863 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 5864 | if(pItem->val.int64 >= 0) { |
| 5865 | *puValue = (uint64_t)pItem->val.int64; |
| 5866 | } else { |
| 5867 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5868 | } |
| 5869 | } else { |
| 5870 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5871 | } |
| 5872 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5873 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5874 | case QCBOR_TYPE_UINT64: |
| 5875 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 5876 | *puValue = pItem->val.uint64; |
| 5877 | } else { |
| 5878 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5879 | } |
| 5880 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5881 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5882 | default: |
| 5883 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5884 | } |
| 5885 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5886 | return QCBOR_SUCCESS; |
| 5887 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5888 | |
| 5889 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5890 | /** |
| 5891 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 5892 | * |
| 5893 | * @param[in] pMe The decode context. |
| 5894 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5895 | * @param[out] puValue Result of the conversion. |
| 5896 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5897 | * |
| 5898 | * See QCBORDecode_GetUInt64Convert(). |
| 5899 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5900 | void |
| 5901 | QCBORDecode_Private_GetUInt64Convert(QCBORDecodeContext *pMe, |
| 5902 | const uint32_t uConvertTypes, |
| 5903 | uint64_t *puValue, |
| 5904 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5905 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5906 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5907 | return; |
| 5908 | } |
| 5909 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5910 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5911 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5912 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5913 | if(uError) { |
| 5914 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5915 | return; |
| 5916 | } |
| 5917 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 5918 | if(pItem) { |
| 5919 | *pItem = Item; |
| 5920 | } |
| 5921 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5922 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(&Item, |
| 5923 | uConvertTypes, |
| 5924 | puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5925 | } |
| 5926 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5927 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5928 | /** |
| 5929 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 5930 | * |
| 5931 | * @param[in] pMe The decode context. |
| 5932 | * @param[in] nLabel Label to find in map. |
| 5933 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5934 | * @param[out] puValue Result of the conversion. |
| 5935 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5936 | * |
| 5937 | * See QCBORDecode_GetUInt64ConvertInMapN(). |
| 5938 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5939 | void |
| 5940 | QCBORDecode_Private_GetUInt64ConvertInMapN(QCBORDecodeContext *pMe, |
| 5941 | const int64_t nLabel, |
| 5942 | const uint32_t uConvertTypes, |
| 5943 | uint64_t *puValue, |
| 5944 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5945 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5946 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5947 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5948 | return; |
| 5949 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5950 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5951 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem, |
| 5952 | uConvertTypes, |
| 5953 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5954 | } |
| 5955 | |
| 5956 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5957 | /** |
| 5958 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 5959 | * |
| 5960 | * @param[in] pMe The decode context. |
| 5961 | * @param[in] szLabel Label to find in map. |
| 5962 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5963 | * @param[out] puValue Result of the conversion. |
| 5964 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5965 | * |
| 5966 | * See QCBORDecode_GetUInt64ConvertInMapSZ(). |
| 5967 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5968 | void |
| 5969 | QCBORDecode_Private_GetUInt64ConvertInMapSZ(QCBORDecodeContext *pMe, |
| 5970 | const char *szLabel, |
| 5971 | const uint32_t uConvertTypes, |
| 5972 | uint64_t *puValue, |
| 5973 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5974 | { |
| 5975 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5976 | return; |
| 5977 | } |
| 5978 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5979 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5980 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5981 | return; |
| 5982 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5983 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5984 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem, |
| 5985 | uConvertTypes, |
| 5986 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5987 | } |
| 5988 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5989 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5990 | /** |
| 5991 | * @brief Convert many number types to an unt64_t. |
| 5992 | * |
| 5993 | * @param[in] pItem The item to convert. |
| 5994 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5995 | * @param[out] puValue The resulting converted value. |
| 5996 | * |
| 5997 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5998 | * in uConvertTypes. |
| 5999 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 6000 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 6001 | * or too small. |
| 6002 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6003 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6004 | QCBOR_Private_UInt64ConvertAll(const QCBORItem *pItem, |
| 6005 | const uint32_t uConvertTypes, |
| 6006 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6007 | { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 6008 | switch(pItem->uDataType) { /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6009 | |
| 6010 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6011 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6012 | return QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6013 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6014 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6015 | } |
| 6016 | break; |
| 6017 | |
| 6018 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6019 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6020 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 6021 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6022 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6023 | } |
| 6024 | break; |
| 6025 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6026 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6027 | |
| 6028 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6029 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6030 | return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6031 | pItem->val.expAndMantissa.nExponent, |
| 6032 | puValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6033 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6034 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6035 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6036 | } |
| 6037 | break; |
| 6038 | |
| 6039 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6040 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6041 | return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6042 | pItem->val.expAndMantissa.nExponent, |
| 6043 | puValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6044 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6045 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6046 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6047 | } |
| 6048 | break; |
| 6049 | |
| 6050 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6051 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6052 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6053 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6054 | uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6055 | if(uErr != QCBOR_SUCCESS) { |
| 6056 | return uErr; |
| 6057 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6058 | return QCBOR_Private_ExponentitateUU(uMantissa, |
| 6059 | pItem->val.expAndMantissa.nExponent, |
| 6060 | puValue, |
| 6061 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6062 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6063 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6064 | } |
| 6065 | break; |
| 6066 | |
| 6067 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6068 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6069 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 6070 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6071 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6072 | } |
| 6073 | break; |
| 6074 | |
| 6075 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6076 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6077 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6078 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6079 | uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, |
| 6080 | &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6081 | if(uErr != QCBOR_SUCCESS) { |
| 6082 | return uErr; |
| 6083 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6084 | return QCBOR_Private_ExponentitateUU(uMantissa, |
| 6085 | pItem->val.expAndMantissa.nExponent, |
| 6086 | puValue, |
| 6087 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6088 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6089 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6090 | } |
| 6091 | break; |
| 6092 | |
| 6093 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6094 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6095 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 6096 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6097 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6098 | } |
| 6099 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6100 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6101 | default: |
| 6102 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6103 | } |
| 6104 | } |
| 6105 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6106 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 6107 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6108 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6109 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6110 | void |
| 6111 | QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, |
| 6112 | const uint32_t uConvertTypes, |
| 6113 | uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6114 | { |
| 6115 | QCBORItem Item; |
| 6116 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6117 | QCBORDecode_Private_GetUInt64Convert(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6118 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6119 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6120 | // The above conversion succeeded |
| 6121 | return; |
| 6122 | } |
| 6123 | |
| 6124 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6125 | // The above conversion failed in a way that code below can't correct |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6126 | return; |
| 6127 | } |
| 6128 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6129 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 6130 | uConvertTypes, |
| 6131 | puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6132 | } |
| 6133 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6134 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6135 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6136 | * Public function, see header qcbor/qcbor_decode.h file |
| 6137 | */ |
| 6138 | void |
| 6139 | QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 6140 | const int64_t nLabel, |
| 6141 | const uint32_t uConvertTypes, |
| 6142 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6143 | { |
| 6144 | QCBORItem Item; |
| 6145 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6146 | QCBORDecode_Private_GetUInt64ConvertInMapN(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6147 | nLabel, |
| 6148 | uConvertTypes, |
| 6149 | puValue, |
| 6150 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6151 | |
| 6152 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6153 | // The above conversion succeeded |
| 6154 | return; |
| 6155 | } |
| 6156 | |
| 6157 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6158 | // The above conversion failed in a way that code below can't correct |
| 6159 | return; |
| 6160 | } |
| 6161 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6162 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 6163 | uConvertTypes, |
| 6164 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6165 | } |
| 6166 | |
| 6167 | |
| 6168 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6169 | * Public function, see header qcbor/qcbor_decode.h file |
| 6170 | */ |
| 6171 | void |
| 6172 | QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 6173 | const char *szLabel, |
| 6174 | const uint32_t uConvertTypes, |
| 6175 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6176 | { |
| 6177 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6178 | QCBORDecode_Private_GetUInt64ConvertInMapSZ(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6179 | szLabel, |
| 6180 | uConvertTypes, |
| 6181 | puValue, |
| 6182 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6183 | |
| 6184 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6185 | // The above conversion succeeded |
| 6186 | return; |
| 6187 | } |
| 6188 | |
| 6189 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6190 | // The above conversion failed in a way that code below can't correct |
| 6191 | return; |
| 6192 | } |
| 6193 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6194 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 6195 | uConvertTypes, |
| 6196 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6197 | } |
| 6198 | |
| 6199 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6200 | |
| 6201 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 6202 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6203 | /** |
| 6204 | * @brief Basic conversions to a double. |
| 6205 | * |
| 6206 | * @param[in] pItem The item to convert |
| 6207 | * @param[in] uConvertTypes Bit flags indicating source types for conversion |
| 6208 | * @param[out] pdValue The value converted to a double |
| 6209 | * |
| 6210 | * This does the conversions that don't need much object code, |
| 6211 | * the conversions from int, uint and float to double. |
| 6212 | * |
| 6213 | * See QCBOR_Private_DoubleConvertAll() for the full set |
| 6214 | * of conversions. |
| 6215 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6216 | static QCBORError |
| 6217 | QCBOR_Private_ConvertDouble(const QCBORItem *pItem, |
| 6218 | const uint32_t uConvertTypes, |
| 6219 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6220 | { |
| 6221 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6222 | case QCBOR_TYPE_FLOAT: |
| 6223 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 6224 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 6225 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6226 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6227 | *pdValue = (double)pItem->val.fnum; |
| 6228 | } else { |
| 6229 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6230 | } |
| 6231 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6232 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6233 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6234 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6235 | break; |
| 6236 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6237 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6238 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 6239 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6240 | *pdValue = pItem->val.dfnum; |
| 6241 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6242 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6243 | } |
| 6244 | } |
| 6245 | break; |
| 6246 | |
| 6247 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6248 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6249 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6250 | // A simple cast seems to do the job with no worry of exceptions. |
| 6251 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6252 | *pdValue = (double)pItem->val.int64; |
| 6253 | |
| 6254 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6255 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6256 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6257 | #else |
| 6258 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 6259 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6260 | break; |
| 6261 | |
| 6262 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6263 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6264 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6265 | // A simple cast seems to do the job with no worry of exceptions. |
| 6266 | // There will be precision loss for some values. |
| 6267 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6268 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6269 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6270 | } |
| 6271 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6272 | #else |
| 6273 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 6274 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6275 | |
| 6276 | default: |
| 6277 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6278 | } |
| 6279 | |
| 6280 | return QCBOR_SUCCESS; |
| 6281 | } |
| 6282 | |
| 6283 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6284 | /** |
| 6285 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 6286 | * |
| 6287 | * @param[in] pMe The decode context. |
| 6288 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 6289 | * @param[out] pdValue The output of the conversion. |
| 6290 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6291 | * |
| 6292 | * See QCBORDecode_GetDoubleConvert(). |
| 6293 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6294 | void |
| 6295 | QCBORDecode_Private_GetDoubleConvert(QCBORDecodeContext *pMe, |
| 6296 | const uint32_t uConvertTypes, |
| 6297 | double *pdValue, |
| 6298 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6299 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6300 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6301 | return; |
| 6302 | } |
| 6303 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6304 | QCBORError uError = QCBORDecode_GetNext(pMe, pItem); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6305 | if(uError) { |
| 6306 | pMe->uLastError = (uint8_t)uError; |
| 6307 | return; |
| 6308 | } |
| 6309 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6310 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6311 | uConvertTypes, |
| 6312 | pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6313 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6314 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6315 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6316 | /** |
| 6317 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 6318 | * |
| 6319 | * @param[in] pMe The decode context. |
| 6320 | * @param[in] nLabel Label to find in map. |
| 6321 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 6322 | * @param[out] pdValue The output of the conversion. |
| 6323 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6324 | * |
| 6325 | * See QCBORDecode_GetDoubleConvertInMapN(). |
| 6326 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6327 | void |
| 6328 | QCBORDecode_Private_GetDoubleConvertInMapN(QCBORDecodeContext *pMe, |
| 6329 | const int64_t nLabel, |
| 6330 | const uint32_t uConvertTypes, |
| 6331 | double *pdValue, |
| 6332 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6333 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6334 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6335 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6336 | return; |
| 6337 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6338 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6339 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
| 6340 | uConvertTypes, |
| 6341 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6342 | } |
| 6343 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6344 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6345 | /** |
| 6346 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 6347 | * |
| 6348 | * @param[in] pMe The decode context. |
| 6349 | * @param[in] szLabel Label to find in map. |
| 6350 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 6351 | * @param[out] pdValue The output of the conversion. |
| 6352 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6353 | * |
| 6354 | * See QCBORDecode_GetDoubleConvertInMapSZ(). |
| 6355 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6356 | void |
| 6357 | QCBORDecode_Private_GetDoubleConvertInMapSZ(QCBORDecodeContext *pMe, |
| 6358 | const char *szLabel, |
| 6359 | const uint32_t uConvertTypes, |
| 6360 | double *pdValue, |
| 6361 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6362 | { |
| 6363 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6364 | return; |
| 6365 | } |
| 6366 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6367 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6368 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6369 | return; |
| 6370 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6371 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6372 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
| 6373 | uConvertTypes, |
| 6374 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6375 | } |
| 6376 | |
| 6377 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6378 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6379 | /** |
| 6380 | * @brief Convert a big number to double-precision float. |
| 6381 | * |
| 6382 | * @param[in] BigNum The big number to convert |
| 6383 | * |
| 6384 | * @returns The double value. |
| 6385 | * |
| 6386 | * This will always succeed. It will lose precision for larger |
| 6387 | * numbers. If the big number is too large to fit (more than |
| 6388 | * 1.7976931348623157E+308) infinity will be returned. NaN is never |
| 6389 | * returned. |
| 6390 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6391 | static double |
| 6392 | QCBOR_Private_ConvertBigNumToDouble(const UsefulBufC BigNum) |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6393 | { |
| 6394 | double dResult; |
| 6395 | |
| 6396 | dResult = 0.0; |
| 6397 | const uint8_t *pByte = BigNum.ptr; |
| 6398 | size_t uLen = BigNum.len; |
| 6399 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6400 | * is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6401 | while(uLen--) { |
| 6402 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 6403 | } |
| 6404 | |
| 6405 | return dResult; |
| 6406 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6407 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 6408 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6409 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6410 | |
| 6411 | |
| 6412 | /** |
| 6413 | * @brief Convert many number types to a double. |
| 6414 | * |
| 6415 | * @param[in] pItem The item to convert. |
| 6416 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 6417 | * @param[out] pdValue The resulting converted value. |
| 6418 | * |
| 6419 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 6420 | * in uConvertTypes. |
| 6421 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 6422 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 6423 | * or too small. |
| 6424 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6425 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6426 | QCBOR_Private_DoubleConvertAll(const QCBORItem *pItem, |
| 6427 | const uint32_t uConvertTypes, |
| 6428 | double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6429 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6430 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6431 | /* |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6432 | * What Every Computer Scientist Should Know About Floating-Point Arithmetic |
| 6433 | * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 6434 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6435 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 6436 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6437 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6438 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6439 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6440 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6441 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 6442 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 6443 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6444 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6445 | } |
| 6446 | break; |
| 6447 | |
| 6448 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6449 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6450 | // Underflow gives 0, overflow gives infinity |
| 6451 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 6452 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6453 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6454 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6455 | } |
| 6456 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6457 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6458 | |
| 6459 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6460 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6461 | *pdValue = QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6462 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6463 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6464 | } |
| 6465 | break; |
| 6466 | |
| 6467 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6468 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6469 | *pdValue = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6470 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6471 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6472 | } |
| 6473 | break; |
| 6474 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6475 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6476 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6477 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6478 | double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6479 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 6480 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6481 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6482 | } |
| 6483 | break; |
| 6484 | |
| 6485 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6486 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6487 | double dMantissa = -QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6488 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 6489 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6490 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6491 | } |
| 6492 | break; |
| 6493 | |
| 6494 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6495 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6496 | double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6497 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 6498 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6499 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6500 | } |
| 6501 | break; |
| 6502 | |
| 6503 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6504 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6505 | double dMantissa = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6506 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 6507 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6508 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6509 | } |
| 6510 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6511 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 6512 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6513 | default: |
| 6514 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6515 | } |
| 6516 | |
| 6517 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6518 | |
| 6519 | #else |
| 6520 | (void)pItem; |
| 6521 | (void)uConvertTypes; |
| 6522 | (void)pdValue; |
| 6523 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 6524 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 6525 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6526 | } |
| 6527 | |
| 6528 | |
| 6529 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6530 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6531 | */ |
| 6532 | void |
| 6533 | QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 6534 | const uint32_t uConvertTypes, |
| 6535 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6536 | { |
| 6537 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6538 | QCBORItem Item; |
| 6539 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6540 | QCBORDecode_Private_GetDoubleConvert(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6541 | |
| 6542 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6543 | // The above conversion succeeded |
| 6544 | return; |
| 6545 | } |
| 6546 | |
| 6547 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6548 | // The above conversion failed in a way that code below can't correct |
| 6549 | return; |
| 6550 | } |
| 6551 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6552 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6553 | uConvertTypes, |
| 6554 | pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6555 | } |
| 6556 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6557 | |
| 6558 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6559 | * Public function, see header qcbor/qcbor_decode.h file |
| 6560 | */ |
| 6561 | void |
| 6562 | QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 6563 | const int64_t nLabel, |
| 6564 | const uint32_t uConvertTypes, |
| 6565 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6566 | { |
| 6567 | QCBORItem Item; |
| 6568 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6569 | QCBORDecode_Private_GetDoubleConvertInMapN(pMe, |
| 6570 | nLabel, |
| 6571 | uConvertTypes, |
| 6572 | pdValue, |
| 6573 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6574 | |
| 6575 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6576 | // The above conversion succeeded |
| 6577 | return; |
| 6578 | } |
| 6579 | |
| 6580 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6581 | // The above conversion failed in a way that code below can't correct |
| 6582 | return; |
| 6583 | } |
| 6584 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6585 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6586 | uConvertTypes, |
| 6587 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | |
| 6591 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6592 | * Public function, see header qcbor/qcbor_decode.h file |
| 6593 | */ |
| 6594 | void |
| 6595 | QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 6596 | const char *szLabel, |
| 6597 | const uint32_t uConvertTypes, |
| 6598 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6599 | { |
| 6600 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6601 | QCBORDecode_Private_GetDoubleConvertInMapSZ(pMe, |
| 6602 | szLabel, |
| 6603 | uConvertTypes, |
| 6604 | pdValue, |
| 6605 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6606 | |
| 6607 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6608 | // The above conversion succeeded |
| 6609 | return; |
| 6610 | } |
| 6611 | |
| 6612 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6613 | // The above conversion failed in a way that code below can't correct |
| 6614 | return; |
| 6615 | } |
| 6616 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6617 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6618 | uConvertTypes, |
| 6619 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6620 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 6621 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6622 | |
| 6623 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6624 | |
| 6625 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6626 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6627 | /** |
| 6628 | * @brief Convert an integer to a big number |
| 6629 | * |
| 6630 | * @param[in] uInt The integer to convert. |
| 6631 | * @param[in] Buffer The buffer to output the big number to. |
| 6632 | * |
| 6633 | * @returns The big number or NULLUsefulBufC is the buffer is to small. |
| 6634 | * |
| 6635 | * This always succeeds unless the buffer is too small. |
| 6636 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6637 | static UsefulBufC |
| 6638 | QCBOR_Private_ConvertIntToBigNum(uint64_t uInt, const UsefulBuf Buffer) |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6639 | { |
| 6640 | while((uInt & 0xff00000000000000UL) == 0) { |
| 6641 | uInt = uInt << 8; |
| 6642 | }; |
| 6643 | |
| 6644 | UsefulOutBuf UOB; |
| 6645 | |
| 6646 | UsefulOutBuf_Init(&UOB, Buffer); |
| 6647 | |
| 6648 | while(uInt) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6649 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 6650 | uInt = uInt << 8; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6651 | } |
| 6652 | |
| 6653 | return UsefulOutBuf_OutUBuf(&UOB); |
| 6654 | } |
| 6655 | |
| 6656 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6657 | /** |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame] | 6658 | * @brief Check and/or complete exponent and mantissa item. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6659 | * |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame] | 6660 | * @param[in] pMe The decoder context. |
| 6661 | * @param[in] TagSpec Expected type(s). |
| 6662 | * @param[in,out] pItem See below. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6663 | * |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame] | 6664 | * This is for decimal fractions and big floats, both of which are an |
| 6665 | * exponent and mantissa. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6666 | * |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame] | 6667 | * If the item item had a tag number indicating it was a |
| 6668 | * decimal fraction or big float, then the input @c pItem will |
| 6669 | * have been decoded as exponent and mantissa. If there was |
| 6670 | * no tag number, the caller is asking this be decoded as a |
| 6671 | * big float or decimal fraction and @c pItem just has the |
| 6672 | * first item in an exponent and mantissa. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6673 | * |
| 6674 | * On output, the item is always a fully decoded decimal fraction or |
| 6675 | * big float. |
| 6676 | * |
| 6677 | * This errors out if the input type does not meet the TagSpec. |
| 6678 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6679 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6680 | QCBOR_Private_ExpMantissaTypeHandler(QCBORDecodeContext *pMe, |
| 6681 | const QCBOR_Private_TagSpec TagSpec, |
| 6682 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6683 | { |
| 6684 | QCBORError uErr; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6685 | |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame] | 6686 | /* pItem could either be a decoded exponent and mantissa or |
| 6687 | * the opening array of an undecoded exponent and mantissa. This |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6688 | * check will succeed on either, but doesn't say which it was. |
| 6689 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6690 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6691 | if(uErr != QCBOR_SUCCESS) { |
| 6692 | goto Done; |
| 6693 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6694 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6695 | if(pItem->uDataType == QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame] | 6696 | /* The item is an array, which means is is an undecoded exponent |
| 6697 | * and mantissa. This call consumes the items in the array and |
| 6698 | * results in a decoded exponent and mantissa in pItem. This is |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6699 | * the case where there was no tag. |
| 6700 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6701 | uErr = QCBORDecode_Private_ExpMantissa(pMe, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6702 | if(uErr != QCBOR_SUCCESS) { |
| 6703 | goto Done; |
| 6704 | } |
| 6705 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6706 | /* The above decode didn't determine whether it is a decimal |
| 6707 | * fraction or big num. Which of these two depends on what the |
| 6708 | * caller wants it decoded as since there is no tag, so fish the |
| 6709 | * type out of the TagSpec. */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6710 | pItem->uDataType = QCBOR_Private_ExpMantissaDataType(TagSpec.uTaggedTypes[0], pItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6711 | |
| 6712 | /* No need to check the type again. All that we need to know was |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame] | 6713 | * that it decoded correctly as a exponent and mantissa. The |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6714 | * QCBOR type is set out by what was requested. |
| 6715 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6716 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6717 | |
| 6718 | /* If the item was not an array and the check passed, then |
| 6719 | * it is a fully decoded big float or decimal fraction and |
| 6720 | * matches what is requested. |
| 6721 | */ |
| 6722 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6723 | Done: |
| 6724 | return uErr; |
| 6725 | } |
| 6726 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6727 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6728 | /* Some notes from the work to disable tags. |
| 6729 | * |
| 6730 | * The API for big floats and decimal fractions seems good. |
| 6731 | * If there's any issue with it it's that the code size to |
| 6732 | * implement is a bit large because of the conversion |
| 6733 | * to/from int and bignum that is required. There is no API |
| 6734 | * that doesn't do the conversion so dead stripping will never |
| 6735 | * leave that code out. |
| 6736 | * |
| 6737 | * The implementation itself seems correct, but not as clean |
| 6738 | * and neat as it could be. It could probably be smaller too. |
| 6739 | * |
| 6740 | * The implementation has three main parts / functions |
| 6741 | * - The decoding of the array of two |
| 6742 | * - All the tag and type checking for the various API functions |
| 6743 | * - Conversion to/from bignum and int |
| 6744 | * |
| 6745 | * The type checking seems like it wastes the most code for |
| 6746 | * what it needs to do. |
| 6747 | * |
| 6748 | * The inlining for the conversion is probably making the |
| 6749 | * overall code base larger. |
| 6750 | * |
| 6751 | * The tests cases could be organized a lot better and be |
| 6752 | * more thorough. |
| 6753 | * |
| 6754 | * Seems also like there could be more common code in the |
| 6755 | * first tier part of the public API. Some functions only |
| 6756 | * vary by a TagSpec. |
| 6757 | */ |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6758 | |
| 6759 | /** |
| 6760 | * @brief Common processor for exponent and mantissa. |
| 6761 | * |
| 6762 | * @param[in] pMe The decode context. |
| 6763 | * @param[in] TagSpec The expected/allowed tags. |
| 6764 | * @param[in] pItem The data item to process. |
| 6765 | * @param[out] pnMantissa The returned mantissa as an int64_t. |
| 6766 | * @param[out] pnExponent The returned exponent as an int64_t. |
| 6767 | * |
| 6768 | * This handles exponent and mantissa for base 2 and 10. This |
| 6769 | * is limited to a mantissa that is an int64_t. See also |
| 6770 | * QCBORDecode_Private_ProcessExpMantissaBig(). |
| 6771 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6772 | static void |
| 6773 | QCBOR_Private_ProcessExpMantissa(QCBORDecodeContext *pMe, |
| 6774 | const QCBOR_Private_TagSpec TagSpec, |
| 6775 | QCBORItem *pItem, |
| 6776 | int64_t *pnMantissa, |
| 6777 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6778 | { |
| 6779 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6780 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6781 | uErr = QCBOR_Private_ExpMantissaTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6782 | if(uErr != QCBOR_SUCCESS) { |
| 6783 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6784 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6785 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6786 | switch (pItem->uDataType) { |
| 6787 | |
| 6788 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 6789 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6790 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6791 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6792 | break; |
| 6793 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6794 | #ifndef QCBOR_DISABLE_TAGS |
| 6795 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6796 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 6797 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 6798 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6799 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6800 | break; |
| 6801 | |
| 6802 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 6803 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 6804 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6805 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6806 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6807 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6808 | |
| 6809 | default: |
| 6810 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 6811 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6812 | |
| 6813 | Done: |
| 6814 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6815 | } |
| 6816 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6817 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6818 | /** |
| 6819 | * @brief Decode exponent and mantissa into a big number. |
| 6820 | * |
| 6821 | * @param[in] pMe The decode context. |
| 6822 | * @param[in] TagSpec The expected/allowed tags. |
| 6823 | * @param[in] pItem Item to decode and convert. |
| 6824 | * @param[in] BufferForMantissa Buffer to output mantissa into. |
| 6825 | * @param[out] pMantissa The output mantissa. |
| 6826 | * @param[out] pbIsNegative The sign of the output. |
| 6827 | * @param[out] pnExponent The mantissa of the output. |
| 6828 | * |
| 6829 | * This is the common processing of a decimal fraction or a big float |
| 6830 | * into a big number. This will decode and consume all the CBOR items |
| 6831 | * that make up the decimal fraction or big float. |
| 6832 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6833 | static void |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6834 | QCBORDecode_Private_ProcessExpMantissaBig(QCBORDecodeContext *pMe, |
| 6835 | const QCBOR_Private_TagSpec TagSpec, |
| 6836 | QCBORItem *pItem, |
| 6837 | const UsefulBuf BufferForMantissa, |
| 6838 | UsefulBufC *pMantissa, |
| 6839 | bool *pbIsNegative, |
| 6840 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6841 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6842 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6843 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6844 | uErr = QCBOR_Private_ExpMantissaTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6845 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6846 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6847 | } |
| 6848 | |
| 6849 | uint64_t uMantissa; |
| 6850 | |
| 6851 | switch (pItem->uDataType) { |
| 6852 | |
| 6853 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6854 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6855 | /* See comments in ExponentiateNN() on handling INT64_MIN */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6856 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 6857 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 6858 | *pbIsNegative = false; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6859 | } else if(pItem->val.expAndMantissa.Mantissa.nInt != INT64_MIN) { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6860 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 6861 | *pbIsNegative = true; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6862 | } else { |
| 6863 | uMantissa = (uint64_t)INT64_MAX+1; |
| 6864 | *pbIsNegative = true; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6865 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6866 | *pMantissa = QCBOR_Private_ConvertIntToBigNum(uMantissa, |
| 6867 | BufferForMantissa); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6868 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6869 | break; |
| 6870 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6871 | #ifndef QCBOR_DISABLE_TAGS |
| 6872 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6873 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6874 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6875 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6876 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 6877 | *pbIsNegative = false; |
| 6878 | break; |
| 6879 | |
| 6880 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6881 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6882 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6883 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 6884 | *pbIsNegative = true; |
| 6885 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6886 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6887 | |
| 6888 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6889 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6890 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6891 | |
| 6892 | Done: |
| 6893 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6894 | } |
| 6895 | |
| 6896 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6897 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6898 | * Public function, see header qcbor/qcbor_decode.h file |
| 6899 | */ |
| 6900 | void |
| 6901 | QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 6902 | const uint8_t uTagRequirement, |
| 6903 | int64_t *pnMantissa, |
| 6904 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6905 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6906 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6907 | return; |
| 6908 | } |
| 6909 | |
| 6910 | QCBORItem Item; |
| 6911 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6912 | if(uError) { |
| 6913 | pMe->uLastError = (uint8_t)uError; |
| 6914 | return; |
| 6915 | } |
| 6916 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6917 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6918 | { |
| 6919 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6920 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6921 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6922 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6923 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6924 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6925 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6926 | TagSpec, |
| 6927 | &Item, |
| 6928 | pnMantissa, |
| 6929 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6930 | } |
| 6931 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6932 | |
| 6933 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6934 | * Public function, see header qcbor/qcbor_decode.h file |
| 6935 | */ |
| 6936 | void |
| 6937 | QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
| 6938 | const int64_t nLabel, |
| 6939 | const uint8_t uTagRequirement, |
| 6940 | int64_t *pnMantissa, |
| 6941 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6942 | { |
| 6943 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6944 | return; |
| 6945 | } |
| 6946 | |
| 6947 | QCBORItem Item; |
| 6948 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6949 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6950 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6951 | { |
| 6952 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6953 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6954 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6955 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6956 | }; |
| 6957 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6958 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6959 | TagSpec, |
| 6960 | &Item, |
| 6961 | pnMantissa, |
| 6962 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6963 | } |
| 6964 | |
| 6965 | |
| 6966 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6967 | * Public function, see header qcbor/qcbor_decode.h file |
| 6968 | */ |
| 6969 | void |
| 6970 | QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
| 6971 | const char *szLabel, |
| 6972 | const uint8_t uTagRequirement, |
| 6973 | int64_t *pnMantissa, |
| 6974 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6975 | { |
| 6976 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6977 | return; |
| 6978 | } |
| 6979 | |
| 6980 | QCBORItem Item; |
| 6981 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6982 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6983 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6984 | { |
| 6985 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6986 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6987 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6988 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6989 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6990 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6991 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6992 | TagSpec, |
| 6993 | &Item, |
| 6994 | pnMantissa, |
| 6995 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6996 | } |
| 6997 | |
| 6998 | |
| 6999 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7000 | * Public function, see header qcbor/qcbor_decode.h file |
| 7001 | */ |
| 7002 | void |
| 7003 | QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 7004 | const uint8_t uTagRequirement, |
| 7005 | const UsefulBuf MantissaBuffer, |
| 7006 | UsefulBufC *pMantissa, |
| 7007 | bool *pbMantissaIsNegative, |
| 7008 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7009 | { |
| 7010 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7011 | return; |
| 7012 | } |
| 7013 | |
| 7014 | QCBORItem Item; |
| 7015 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 7016 | if(uError) { |
| 7017 | pMe->uLastError = (uint8_t)uError; |
| 7018 | return; |
| 7019 | } |
| 7020 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7021 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7022 | { |
| 7023 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 7024 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 7025 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 7026 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7027 | }; |
| 7028 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7029 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 7030 | TagSpec, |
| 7031 | &Item, |
| 7032 | MantissaBuffer, |
| 7033 | pMantissa, |
| 7034 | pbMantissaIsNegative, |
| 7035 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7036 | } |
| 7037 | |
| 7038 | |
| 7039 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7040 | * Public function, see header qcbor/qcbor_decode.h file |
| 7041 | */ |
| 7042 | void |
| 7043 | QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
| 7044 | const int64_t nLabel, |
| 7045 | const uint8_t uTagRequirement, |
| 7046 | const UsefulBuf BufferForMantissa, |
| 7047 | UsefulBufC *pMantissa, |
| 7048 | bool *pbIsNegative, |
| 7049 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7050 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7051 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7052 | return; |
| 7053 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 7054 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7055 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7056 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 7057 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7058 | return; |
| 7059 | } |
| 7060 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7061 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7062 | { |
| 7063 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 7064 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 7065 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 7066 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7067 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7068 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7069 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 7070 | TagSpec, |
| 7071 | &Item, |
| 7072 | BufferForMantissa, |
| 7073 | pMantissa, |
| 7074 | pbIsNegative, |
| 7075 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7076 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 7077 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7078 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7079 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7080 | * Public function, see header qcbor/qcbor_decode.h file |
| 7081 | */ |
| 7082 | void |
| 7083 | QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
| 7084 | const char *szLabel, |
| 7085 | const uint8_t uTagRequirement, |
| 7086 | const UsefulBuf BufferForMantissa, |
| 7087 | UsefulBufC *pMantissa, |
| 7088 | bool *pbIsNegative, |
| 7089 | int64_t *pnExponent) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7090 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7091 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7092 | return; |
| 7093 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7094 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7095 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7096 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 7097 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7098 | return; |
| 7099 | } |
| 7100 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7101 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7102 | { |
| 7103 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 7104 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 7105 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 7106 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7107 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7108 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7109 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 7110 | TagSpec, |
| 7111 | &Item, |
| 7112 | BufferForMantissa, |
| 7113 | pMantissa, |
| 7114 | pbIsNegative, |
| 7115 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7116 | } |
| 7117 | |
| 7118 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7119 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7120 | * Public function, see header qcbor/qcbor_decode.h file |
| 7121 | */ |
| 7122 | void |
| 7123 | QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 7124 | const uint8_t uTagRequirement, |
| 7125 | int64_t *pnMantissa, |
| 7126 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7127 | { |
| 7128 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7129 | return; |
| 7130 | } |
| 7131 | |
| 7132 | QCBORItem Item; |
| 7133 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 7134 | if(uError) { |
| 7135 | pMe->uLastError = (uint8_t)uError; |
| 7136 | return; |
| 7137 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7138 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7139 | { |
| 7140 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 7141 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 7142 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 7143 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7144 | }; |
| 7145 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7146 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 7147 | TagSpec, |
| 7148 | &Item, |
| 7149 | pnMantissa, |
| 7150 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7151 | } |
| 7152 | |
| 7153 | |
| 7154 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7155 | * Public function, see header qcbor/qcbor_decode.h file |
| 7156 | */ |
| 7157 | void |
| 7158 | QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
| 7159 | const int64_t nLabel, |
| 7160 | const uint8_t uTagRequirement, |
| 7161 | int64_t *pnMantissa, |
| 7162 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7163 | { |
| 7164 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7165 | return; |
| 7166 | } |
| 7167 | |
| 7168 | QCBORItem Item; |
| 7169 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 7170 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7171 | return; |
| 7172 | } |
| 7173 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7174 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7175 | { |
| 7176 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 7177 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 7178 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 7179 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7180 | }; |
| 7181 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7182 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 7183 | TagSpec, |
| 7184 | &Item, |
| 7185 | pnMantissa, |
| 7186 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7187 | } |
| 7188 | |
| 7189 | |
| 7190 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7191 | * Public function, see header qcbor/qcbor_decode.h file |
| 7192 | */ |
| 7193 | void |
| 7194 | QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
| 7195 | const char *szLabel, |
| 7196 | const uint8_t uTagRequirement, |
| 7197 | int64_t *pnMantissa, |
| 7198 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7199 | { |
| 7200 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7201 | return; |
| 7202 | } |
| 7203 | |
| 7204 | QCBORItem Item; |
| 7205 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 7206 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7207 | return; |
| 7208 | } |
| 7209 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7210 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7211 | { |
| 7212 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 7213 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 7214 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 7215 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7216 | }; |
| 7217 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7218 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 7219 | TagSpec, |
| 7220 | &Item, |
| 7221 | pnMantissa, |
| 7222 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7223 | } |
| 7224 | |
| 7225 | |
| 7226 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7227 | * Public function, see header qcbor/qcbor_decode.h file |
| 7228 | */ |
| 7229 | void |
| 7230 | QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 7231 | const uint8_t uTagRequirement, |
| 7232 | const UsefulBuf MantissaBuffer, |
| 7233 | UsefulBufC *pMantissa, |
| 7234 | bool *pbMantissaIsNegative, |
| 7235 | int64_t *pnExponent) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7236 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7237 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7238 | return; |
| 7239 | } |
| 7240 | |
| 7241 | QCBORItem Item; |
| 7242 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 7243 | if(uError) { |
| 7244 | pMe->uLastError = (uint8_t)uError; |
| 7245 | return; |
| 7246 | } |
| 7247 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7248 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7249 | { |
| 7250 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 7251 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 7252 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 7253 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7254 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7255 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7256 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 7257 | TagSpec, |
| 7258 | &Item, |
| 7259 | MantissaBuffer, |
| 7260 | pMantissa, |
| 7261 | pbMantissaIsNegative, |
| 7262 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7263 | } |
| 7264 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7265 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7266 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7267 | * Public function, see header qcbor/qcbor_decode.h file |
| 7268 | */ |
| 7269 | void |
| 7270 | QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
| 7271 | const int64_t nLabel, |
| 7272 | const uint8_t uTagRequirement, |
| 7273 | const UsefulBuf BufferForMantissa, |
| 7274 | UsefulBufC *pMantissa, |
| 7275 | bool *pbIsNegative, |
| 7276 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7277 | { |
| 7278 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7279 | return; |
| 7280 | } |
| 7281 | |
| 7282 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7283 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 7284 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7285 | return; |
| 7286 | } |
| 7287 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7288 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7289 | { |
| 7290 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 7291 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 7292 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 7293 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7294 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7295 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7296 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 7297 | TagSpec, |
| 7298 | &Item, |
| 7299 | BufferForMantissa, |
| 7300 | pMantissa, |
| 7301 | pbIsNegative, |
| 7302 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7303 | } |
| 7304 | |
| 7305 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7306 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7307 | * Public function, see header qcbor/qcbor_decode.h file |
| 7308 | */ |
| 7309 | void |
| 7310 | QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
| 7311 | const char *szLabel, |
| 7312 | const uint8_t uTagRequirement, |
| 7313 | const UsefulBuf BufferForMantissa, |
| 7314 | UsefulBufC *pMantissa, |
| 7315 | bool *pbIsNegative, |
| 7316 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7317 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7318 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7319 | return; |
| 7320 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7321 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7322 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7323 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 7324 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 7325 | return; |
| 7326 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7327 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7328 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7329 | { |
| 7330 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 7331 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 7332 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 7333 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7334 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7335 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7336 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 7337 | TagSpec, |
| 7338 | &Item, |
| 7339 | BufferForMantissa, |
| 7340 | pMantissa, |
| 7341 | pbIsNegative, |
| 7342 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7343 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7344 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 7345 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |