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 || |
| 108 | uDataType == QCBOR_TYPE_ARRAY || |
| 109 | uDataType == QCBOR_TYPE_MAP_AS_ARRAY; |
| 110 | } |
| 111 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 112 | static bool |
| 113 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(const QCBORItem Item) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 114 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 115 | if(!QCBORItem_IsMapOrArray(Item)){ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 119 | if(Item.val.uCount != 0) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 120 | return false; |
| 121 | } |
| 122 | return true; |
| 123 | } |
| 124 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 125 | static bool |
| 126 | QCBORItem_IsIndefiniteLengthMapOrArray(const QCBORItem Item) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 127 | { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 128 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 129 | if(!QCBORItem_IsMapOrArray(Item)){ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 133 | if(Item.val.uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | return true; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 137 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 138 | (void)Item; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 139 | return false; |
| 140 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 143 | /* Return true if the labels in Item1 and Item2 are the same. |
| 144 | Works only for integer and string labels. Returns false |
| 145 | for any other type. */ |
| 146 | static bool |
| 147 | QCBORItem_MatchLabel(const QCBORItem Item1, const QCBORItem Item2) |
| 148 | { |
| 149 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 150 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 151 | return true; |
| 152 | } |
| 153 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
| 154 | if(Item2.uLabelType == QCBOR_TYPE_TEXT_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 155 | return true; |
| 156 | } |
| 157 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
| 158 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 159 | return true; |
| 160 | } |
| 161 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 162 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* Other label types are never matched */ |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /* |
| 173 | Returns true if Item1 and Item2 are the same type |
| 174 | or if either are of QCBOR_TYPE_ANY. |
| 175 | */ |
| 176 | static bool |
| 177 | QCBORItem_MatchType(const QCBORItem Item1, const QCBORItem Item2) |
| 178 | { |
| 179 | if(Item1.uDataType == Item2.uDataType) { |
| 180 | return true; |
| 181 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
| 182 | return true; |
| 183 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
| 184 | return true; |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 189 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 190 | /*=========================================================================== |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 191 | DecodeNesting -- Tracking array/map/sequence/bstr-wrapped nesting |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 192 | ===========================================================================*/ |
| 193 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 194 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 195 | * See comments about and typedef of QCBORDecodeNesting in qcbor_private.h, |
| 196 | * the data structure all these functions work on. |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 197 | */ |
| 198 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 199 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 200 | static uint8_t |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 201 | DecodeNesting_GetCurrentLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 202 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 203 | const ptrdiff_t nLevel = pNesting->pCurrent - &(pNesting->pLevels[0]); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 204 | /* Limit in DecodeNesting_Descend against more than |
| 205 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 206 | */ |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 207 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 210 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 211 | static uint8_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 212 | DecodeNesting_GetBoundedModeLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 213 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 214 | const ptrdiff_t nLevel = pNesting->pCurrentBounded - &(pNesting->pLevels[0]); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 215 | /* Limit in DecodeNesting_Descend against more than |
| 216 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 217 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 218 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 221 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 222 | static uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 223 | DecodeNesting_GetMapOrArrayStart(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 224 | { |
| 225 | return pNesting->pCurrentBounded->u.ma.uStartOffset; |
| 226 | } |
| 227 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 228 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 229 | static bool |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 230 | DecodeNesting_IsBoundedEmpty(const QCBORDecodeNesting *pNesting) |
| 231 | { |
| 232 | if(pNesting->pCurrentBounded->u.ma.uCountCursor == QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 233 | return true; |
| 234 | } else { |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 240 | static bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 241 | DecodeNesting_IsCurrentAtTop(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 242 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 243 | if(pNesting->pCurrent == &(pNesting->pLevels[0])) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 244 | return true; |
| 245 | } else { |
| 246 | return false; |
| 247 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 250 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 251 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 252 | DecodeNesting_IsCurrentDefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 253 | { |
| 254 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 255 | /* Not a map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 256 | return false; |
| 257 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 258 | |
| 259 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 260 | if(pNesting->pCurrent->u.ma.uCountTotal == QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 261 | /* Is indefinite */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 262 | return false; |
| 263 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 264 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 265 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 266 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 267 | /* All checks passed; is a definte length map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 268 | return true; |
| 269 | } |
| 270 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 271 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 272 | DecodeNesting_IsCurrentBstrWrapped(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 273 | { |
| 274 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 275 | /* is a byte string */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 276 | return true; |
| 277 | } |
| 278 | return false; |
| 279 | } |
| 280 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 281 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 282 | static bool |
| 283 | DecodeNesting_IsCurrentBounded(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 284 | { |
| 285 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
| 286 | return true; |
| 287 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 288 | if(pNesting->pCurrent->u.ma.uStartOffset != QCBOR_NON_BOUNDED_OFFSET) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 289 | return true; |
| 290 | } |
| 291 | return false; |
| 292 | } |
| 293 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 294 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 295 | static void |
| 296 | DecodeNesting_SetMapOrArrayBoundedMode(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uStart) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 297 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 298 | /* Should be only called on maps and arrays */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 299 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 300 | * DecodeNesting_EnterBoundedMode() checks to be sure uStart is not |
| 301 | * larger than DecodeNesting_EnterBoundedMode which keeps it less than |
| 302 | * uin32_t so the cast is safe. |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 303 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 304 | pNesting->pCurrent->u.ma.uStartOffset = (uint32_t)uStart; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 305 | |
| 306 | if(bIsEmpty) { |
| 307 | pNesting->pCurrent->u.ma.uCountCursor = QCBOR_COUNT_INDICATES_ZERO_LENGTH; |
| 308 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 311 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 312 | static void |
| 313 | DecodeNesting_ClearBoundedMode(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 314 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 315 | pNesting->pCurrent->u.ma.uStartOffset = QCBOR_NON_BOUNDED_OFFSET; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 318 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 319 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 320 | DecodeNesting_IsAtEndOfBoundedLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 321 | { |
| 322 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 323 | /* No bounded map or array set up */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 324 | return false; |
| 325 | } |
| 326 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 327 | /* Not a map or array; end of those is by byte count */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 328 | return false; |
| 329 | } |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 330 | if(!DecodeNesting_IsCurrentBounded(pNesting)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 331 | /* In a traveral at a level deeper than the bounded level */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 332 | return false; |
| 333 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 334 | /* Works for both definite- and indefinitelength maps/arrays */ |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 335 | if(pNesting->pCurrentBounded->u.ma.uCountCursor != 0 && |
| 336 | pNesting->pCurrentBounded->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 337 | /* Count is not zero, still unconsumed item */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 338 | return false; |
| 339 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 340 | /* All checks passed, got to the end of an array or map*/ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 341 | return true; |
| 342 | } |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 343 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 344 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 345 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 346 | DecodeNesting_IsEndOfDefiniteLengthMapOrArray(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 347 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 348 | /* Must only be called on map / array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 349 | if(pNesting->pCurrent->u.ma.uCountCursor == 0) { |
| 350 | return true; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 351 | } else { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 352 | return false; |
| 353 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 356 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 357 | static bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 358 | DecodeNesting_IsCurrentTypeMap(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 359 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 360 | if(pNesting->pCurrent->uLevelType == CBOR_MAJOR_TYPE_MAP) { |
| 361 | return true; |
| 362 | } else { |
| 363 | return false; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 364 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 367 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 368 | static bool |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 369 | DecodeNesting_IsBoundedType(const QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 370 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 371 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 372 | return false; |
| 373 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 374 | |
| 375 | if(pNesting->pCurrentBounded->uLevelType != uType) { |
| 376 | return false; |
| 377 | } |
| 378 | |
| 379 | return true; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 382 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 383 | static void |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 384 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 385 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 386 | /* Only call on a definite-length array / map */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 387 | pNesting->pCurrent->u.ma.uCountCursor--; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 388 | } |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 389 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 390 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 391 | static void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 392 | DecodeNesting_ReverseDecrement(QCBORDecodeNesting *pNesting) |
| 393 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 394 | /* Only call on a definite-length array / map */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 395 | pNesting->pCurrent->u.ma.uCountCursor++; |
| 396 | } |
| 397 | |
| 398 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 399 | static void |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 400 | DecodeNesting_Ascend(QCBORDecodeNesting *pNesting) |
| 401 | { |
| 402 | pNesting->pCurrent--; |
| 403 | } |
| 404 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 405 | |
| 406 | static QCBORError |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 407 | DecodeNesting_Descend(QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 408 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 409 | /* Error out if nesting is too deep */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 410 | if(pNesting->pCurrent >= &(pNesting->pLevels[QCBOR_MAX_ARRAY_NESTING])) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 411 | return QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 414 | /* The actual descend */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 415 | pNesting->pCurrent++; |
| 416 | |
| 417 | pNesting->pCurrent->uLevelType = uType; |
| 418 | |
| 419 | return QCBOR_SUCCESS; |
| 420 | } |
| 421 | |
| 422 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 423 | static QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 424 | DecodeNesting_EnterBoundedMapOrArray(QCBORDecodeNesting *pNesting, |
| 425 | bool bIsEmpty, |
| 426 | size_t uOffset) |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 427 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 428 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 429 | * Should only be called on map/array. |
| 430 | * |
| 431 | * Have descended into this before this is called. The job here is |
| 432 | * just to mark it in bounded mode. |
| 433 | * |
| 434 | * Check against QCBOR_MAX_DECODE_INPUT_SIZE make sure that |
| 435 | * uOffset doesn't collide with QCBOR_NON_BOUNDED_OFFSET. |
| 436 | * |
| 437 | * 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] | 438 | */ |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 439 | if((uint32_t)uOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 440 | return QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 443 | pNesting->pCurrentBounded = pNesting->pCurrent; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 444 | |
| 445 | DecodeNesting_SetMapOrArrayBoundedMode(pNesting, bIsEmpty, uOffset); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 446 | |
| 447 | return QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 450 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 451 | static QCBORError |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 452 | DecodeNesting_DescendMapOrArray(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 453 | uint8_t uQCBORType, |
| 454 | uint64_t uCount) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 455 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 456 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 457 | |
| 458 | if(uCount == 0) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 459 | /* Nothing to do for empty definite-length arrays. They are just are |
| 460 | * effectively the same as an item that is not a map or array. |
| 461 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 462 | goto Done; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 463 | /* Empty indefinite-length maps and arrays are handled elsewhere */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 466 | /* Error out if arrays is too long to handle */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 467 | if(uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH && |
| 468 | uCount > QCBOR_MAX_ITEMS_IN_ARRAY) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 469 | uError = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 470 | goto Done; |
| 471 | } |
| 472 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 473 | uError = DecodeNesting_Descend(pNesting, uQCBORType); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 474 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 475 | goto Done; |
| 476 | } |
| 477 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 478 | /* Fill in the new map/array level. Check above makes casts OK. */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 479 | pNesting->pCurrent->u.ma.uCountCursor = (uint16_t)uCount; |
| 480 | pNesting->pCurrent->u.ma.uCountTotal = (uint16_t)uCount; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 481 | |
| 482 | DecodeNesting_ClearBoundedMode(pNesting); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 483 | |
| 484 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 485 | return uError;; |
| 486 | } |
| 487 | |
| 488 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 489 | static void |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 490 | DecodeNesting_LevelUpCurrent(QCBORDecodeNesting *pNesting) |
| 491 | { |
| 492 | pNesting->pCurrent = pNesting->pCurrentBounded - 1; |
| 493 | } |
| 494 | |
| 495 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 496 | static void |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 497 | DecodeNesting_LevelUpBounded(QCBORDecodeNesting *pNesting) |
| 498 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 499 | while(pNesting->pCurrentBounded != &(pNesting->pLevels[0])) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 500 | pNesting->pCurrentBounded--; |
| 501 | if(DecodeNesting_IsCurrentBounded(pNesting)) { |
| 502 | break; |
| 503 | } |
| 504 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 507 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 508 | static void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 509 | DecodeNesting_SetCurrentToBoundedLevel(QCBORDecodeNesting *pNesting) |
| 510 | { |
| 511 | pNesting->pCurrent = pNesting->pCurrentBounded; |
| 512 | } |
| 513 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 514 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 515 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 516 | DecodeNesting_DescendIntoBstrWrapped(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 517 | uint32_t uEndOffset, |
| 518 | uint32_t uStartOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 519 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 520 | QCBORError uError; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 521 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 522 | uError = DecodeNesting_Descend(pNesting, QCBOR_TYPE_BYTE_STRING); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 523 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 524 | goto Done; |
| 525 | } |
| 526 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 527 | /* Fill in the new byte string level */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 528 | pNesting->pCurrent->u.bs.uSavedEndOffset = uEndOffset; |
| 529 | pNesting->pCurrent->u.bs.uBstrStartOffset = uStartOffset; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 530 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 531 | /* Bstr wrapped levels are always bounded */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 532 | pNesting->pCurrentBounded = pNesting->pCurrent; |
| 533 | |
| 534 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 535 | return uError;; |
| 536 | } |
| 537 | |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 538 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 539 | static void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 540 | DecodeNesting_ZeroMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 541 | { |
| 542 | pNesting->pCurrent->u.ma.uCountCursor = 0; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 545 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 546 | static void |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 547 | DecodeNesting_ResetMapOrArrayCount(QCBORDecodeNesting *pNesting) |
| 548 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 549 | if(pNesting->pCurrent->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 550 | pNesting->pCurrentBounded->u.ma.uCountCursor = pNesting->pCurrentBounded->u.ma.uCountTotal; |
| 551 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 555 | static void |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 556 | DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 557 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 558 | /* Assumes that *pNesting has been zero'd before this call. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 559 | pNesting->pLevels[0].uLevelType = QCBOR_TYPE_BYTE_STRING; |
| 560 | pNesting->pCurrent = &(pNesting->pLevels[0]); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 564 | static void |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 565 | DecodeNesting_PrepareForMapSearch(QCBORDecodeNesting *pNesting, |
| 566 | QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 567 | { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 568 | *pSave = *pNesting; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 571 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 572 | static void |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 573 | DecodeNesting_RestoreFromMapSearch(QCBORDecodeNesting *pNesting, |
| 574 | const QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 575 | { |
| 576 | *pNesting = *pSave; |
| 577 | } |
| 578 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 579 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 580 | static uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 581 | DecodeNesting_GetPreviousBoundedEnd(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 582 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 583 | return pMe->pCurrentBounded->u.bs.uSavedEndOffset; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 587 | |
| 588 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 589 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 590 | /*=========================================================================== |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 591 | QCBORStringAllocate -- STRING ALLOCATOR INVOCATION |
| 592 | |
| 593 | The following four functions are pretty wrappers for invocation of |
| 594 | the string allocator supplied by the caller. |
| 595 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 596 | ===========================================================================*/ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 597 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 598 | static void |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 599 | StringAllocator_Free(const QCBORInternalAllocator *pMe, const void *pMem) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 600 | { |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 601 | /* This cast to uintptr_t suppresses the "-Wcast-qual" warnings. |
| 602 | * This is the one place where the const needs to be cast away so const can |
| 603 | * be use in the rest of the code. |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 604 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 605 | (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)(uintptr_t)pMem, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 606 | } |
| 607 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 608 | // StringAllocator_Reallocate called with pMem NULL is |
| 609 | // equal to StringAllocator_Allocate() |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 610 | static UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 611 | StringAllocator_Reallocate(const QCBORInternalAllocator *pMe, |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 612 | const void *pMem, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 613 | size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 614 | { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 615 | /* See comment in StringAllocator_Free() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 616 | return (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)(uintptr_t)pMem, uSize); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 617 | } |
| 618 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 619 | static UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 620 | StringAllocator_Allocate(const QCBORInternalAllocator *pMe, size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 621 | { |
| 622 | return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize); |
| 623 | } |
| 624 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 625 | static void |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 626 | StringAllocator_Destruct(const QCBORInternalAllocator *pMe) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 627 | { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 628 | /* See comment in StringAllocator_Free() */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 629 | if(pMe->pfAllocator) { |
| 630 | (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0); |
| 631 | } |
| 632 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 633 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 634 | |
| 635 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 636 | |
| 637 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 638 | /*=========================================================================== |
| 639 | QCBORDecode -- The main implementation of CBOR decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 640 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 641 | See qcbor/qcbor_decode.h for definition of the object |
| 642 | used here: QCBORDecodeContext |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 643 | ===========================================================================*/ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 644 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 645 | * Public function, see header file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 646 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 647 | void |
| 648 | QCBORDecode_Init(QCBORDecodeContext *pMe, |
| 649 | UsefulBufC EncodedCBOR, |
| 650 | QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 651 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 652 | memset(pMe, 0, sizeof(QCBORDecodeContext)); |
| 653 | UsefulInputBuf_Init(&(pMe->InBuf), EncodedCBOR); |
| 654 | /* Don't bother with error check on decode mode. If a bad value is |
| 655 | * passed it will just act as if the default normal mode of 0 was set. |
| 656 | */ |
| 657 | pMe->uDecodeMode = (uint8_t)nDecodeMode; |
| 658 | DecodeNesting_Init(&(pMe->nesting)); |
| 659 | |
| 660 | /* Inialize me->auMappedTags to CBOR_TAG_INVALID16. See |
| 661 | * GetNext_TaggedItem() and MapTagNumber(). */ |
| 662 | memset(pMe->auMappedTags, 0xff, sizeof(pMe->auMappedTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 666 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 667 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 668 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 669 | * Public function, see header file |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 670 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 671 | void |
| 672 | QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe, |
| 673 | QCBORStringAllocate pfAllocateFunction, |
| 674 | void *pAllocateContext, |
| 675 | bool bAllStrings) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 676 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 677 | pMe->StringAllocator.pfAllocator = pfAllocateFunction; |
| 678 | pMe->StringAllocator.pAllocateCxt = pAllocateContext; |
| 679 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 680 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 681 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 682 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 683 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 684 | |
| 685 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 686 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 687 | * Deprecated public function, see header file |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 688 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 689 | void |
| 690 | QCBORDecode_SetCallerConfiguredTagList(QCBORDecodeContext *pMe, |
| 691 | const QCBORTagListIn *pTagList) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 692 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 693 | /* This does nothing now. It is retained for backwards compatibility */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 694 | (void)pMe; |
| 695 | (void)pTagList; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 696 | } |
| 697 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 698 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 699 | |
| 700 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 701 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 702 | * Decoding items is done in six layers, one calling the next one |
| 703 | * down. If a layer has no work to do for a particular item, it |
| 704 | * returns quickly. |
| 705 | * |
| 706 | * 1. QCBORDecode_GetNextTagContent - The top layer processes tagged |
| 707 | * data items, turning them into the local C representation. For the |
| 708 | * most simple it is just associating a QCBOR_TYPE with the data. For |
| 709 | * the complex ones that an aggregate of data items, there is some |
| 710 | * further decoding and some limited recursion. |
| 711 | * |
| 712 | * 2. QCBORDecode_GetNextMapOrArray - This manages the beginnings and |
| 713 | * ends of maps and arrays. It tracks descending into and ascending |
| 714 | * out of maps/arrays. It processes breaks that terminate |
| 715 | * indefinite-length maps and arrays. |
| 716 | * |
| 717 | * 3. QCBORDecode_GetNextMapEntry - This handles the combining of two |
| 718 | * items, the label and the data, that make up a map entry. It only |
| 719 | * does work on maps. It combines the label and data items into one |
| 720 | * labeled item. |
| 721 | * |
| 722 | * 4. QCBORDecode_GetNextTagNumber - This decodes type 6 tag |
| 723 | * numbers. It turns the tag numbers into bit flags associated with |
| 724 | * the data item. No actual decoding of the contents of the tag is |
| 725 | * performed here. |
| 726 | * |
| 727 | * 5. QCBORDecode_GetNextFullString - This assembles the sub-items |
| 728 | * that make up an indefinite-length string into one string item. It |
| 729 | * uses the string allocator to create contiguous space for the |
| 730 | * item. It processes all breaks that are part of indefinite-length |
| 731 | * strings. |
| 732 | * |
| 733 | * 6. DecodeAtomicDataItem - This decodes the atomic data items in |
| 734 | * CBOR. Each atomic data item has a "major type", an integer |
| 735 | * "argument" and optionally some content. For text and byte strings, |
| 736 | * the content is the bytes that make up the string. These are the |
| 737 | * smallest data items that are considered to be well-formed. The |
| 738 | * content may also be other data items in the case of aggregate |
| 739 | * types. They are not handled in this layer. |
| 740 | * |
| 741 | * Roughly this takes 300 bytes of stack for vars. TODO: evaluate this |
| 742 | * more carefully and correctly. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 743 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 744 | |
| 745 | |
| 746 | /* |
| 747 | * Note about use of int and unsigned variables. |
| 748 | * |
| 749 | * See http://www.unix.org/whitepapers/64bit.html for reasons int is |
| 750 | * used carefully here, and in particular why it isn't used in the |
| 751 | * public interface. Also see |
| 752 | * https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 753 | * |
| 754 | * Int is used for values that need less than 16-bits and would be |
| 755 | * subject to integer promotion and result in complaining from static |
| 756 | * analyzers. |
| 757 | */ |
| 758 | |
| 759 | |
| 760 | /** |
| 761 | * @brief Decode the CBOR head, the type and argument. |
| 762 | * |
| 763 | * @param[in] pUInBuf The input buffer to read from. |
| 764 | * @param[out] pnMajorType The decoded major type. |
| 765 | * @param[out] puArgument The decoded argument. |
| 766 | * @param[out] pnAdditionalInfo The decoded Lower 5 bits of initial byte. |
| 767 | * |
| 768 | * @retval QCBOR_ERR_UNSUPPORTED |
| 769 | * @retval QCBOR_ERR_HIT_END |
| 770 | * |
| 771 | * This decodes the CBOR "head" that every CBOR data item has. See |
| 772 | * longer explaination of the head in documentation for |
| 773 | * QCBOREncode_EncodeHead(). |
| 774 | * |
| 775 | * This does the network->host byte order conversion. The conversion |
| 776 | * here also results in the conversion for floats in addition to that |
| 777 | * for lengths, tags and integer values. |
| 778 | * |
| 779 | * The int type is preferred to uint8_t for some variables as this |
| 780 | * avoids integer promotions, can reduce code size and makes static |
| 781 | * analyzers happier. |
| 782 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 783 | static QCBORError |
| 784 | QCBOR_Private_DecodeHead(UsefulInputBuf *pUInBuf, |
| 785 | int *pnMajorType, |
| 786 | uint64_t *puArgument, |
| 787 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 788 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 789 | QCBORError uReturn; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 790 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 791 | /* Get the initial byte that every CBOR data item has and break it |
| 792 | * down. */ |
| 793 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 794 | const int nTmpMajorType = nInitialByte >> 5; |
| 795 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 796 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 797 | /* Where the argument accumulates */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 798 | uint64_t uArgument; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 799 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 800 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 801 | /* Need to get 1,2,4 or 8 additional argument bytes. Map |
| 802 | * LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length. |
| 803 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 804 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 805 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 806 | /* Loop getting all the bytes in the argument */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 807 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 808 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 809 | /* This shift and add gives the endian conversion. */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 810 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 811 | } |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 812 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 813 | /* The reserved and thus-far unused additional info values */ |
| 814 | uReturn = QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 815 | goto Done; |
| 816 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 817 | /* Less than 24, additional info is argument or 31, an |
| 818 | * indefinite-length. No more bytes to get. |
| 819 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 820 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 821 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 822 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 823 | if(UsefulInputBuf_GetError(pUInBuf)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 824 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 825 | goto Done; |
| 826 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 827 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 828 | /* All successful if arrived here. */ |
| 829 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 830 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 831 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 832 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 833 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 834 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 835 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 838 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 839 | /** |
| 840 | * @brief Decode integer types, major types 0 and 1. |
| 841 | * |
| 842 | * @param[in] nMajorType The CBOR major type (0 or 1). |
| 843 | * @param[in] uArgument The argument from the head. |
| 844 | * @param[out] pDecodedItem The filled in decoded item. |
| 845 | * |
| 846 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 847 | * |
| 848 | * Must only be called when major type is 0 or 1. |
| 849 | * |
| 850 | * CBOR doesn't explicitly specify two's compliment for integers but |
| 851 | * all CPUs use it these days and the test vectors in the RFC are |
| 852 | * so. All integers in the CBOR structure are positive and the major |
| 853 | * type indicates positive or negative. CBOR can express positive |
| 854 | * integers up to 2^x - 1 where x is the number of bits and negative |
| 855 | * integers down to 2^x. Note that negative numbers can be one more |
| 856 | * away from zero than positive. Stdint, as far as I can tell, uses |
| 857 | * two's compliment to represent negative integers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 858 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 859 | static QCBORError |
| 860 | QCBOR_Private_DecodeInteger(const int nMajorType, |
| 861 | const uint64_t uArgument, |
| 862 | QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 863 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 864 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 865 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 866 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 867 | if (uArgument <= INT64_MAX) { |
| 868 | pDecodedItem->val.int64 = (int64_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 869 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 870 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 871 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 872 | pDecodedItem->val.uint64 = uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 873 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 874 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 875 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 876 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 877 | if(uArgument <= INT64_MAX) { |
| 878 | /* CBOR's representation of negative numbers lines up with |
| 879 | * the two-compliment representation. A negative integer has |
| 880 | * one more in range than a positive integer. INT64_MIN is |
| 881 | * equal to (-INT64_MAX) - 1. |
| 882 | */ |
| 883 | pDecodedItem->val.int64 = (-(int64_t)uArgument) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 884 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 885 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 886 | } else { |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame^] | 887 | pDecodedItem->val.uint64 = uArgument; |
| 888 | pDecodedItem->uDataType = QCBOR_TYPE_65BIT_NEG_INT; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 889 | } |
| 890 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 891 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 892 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 893 | } |
| 894 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 895 | |
| 896 | /* Make sure #define value line up as DecodeSimple counts on this. */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 897 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 898 | #error QCBOR_TYPE_FALSE macro value wrong |
| 899 | #endif |
| 900 | |
| 901 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 902 | #error QCBOR_TYPE_TRUE macro value wrong |
| 903 | #endif |
| 904 | |
| 905 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 906 | #error QCBOR_TYPE_NULL macro value wrong |
| 907 | #endif |
| 908 | |
| 909 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 910 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 911 | #endif |
| 912 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 913 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 914 | #error QCBOR_TYPE_BREAK macro value wrong |
| 915 | #endif |
| 916 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 917 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 918 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 919 | #endif |
| 920 | |
| 921 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 922 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 923 | #endif |
| 924 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 925 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 926 | /** |
| 927 | * @brief Decode major type 7 -- true, false, floating-point, break... |
| 928 | * |
| 929 | * @param[in] nAdditionalInfo The lower five bits from the initial byte. |
| 930 | * @param[in] uArgument The argument from the head. |
| 931 | * @param[out] pDecodedItem The filled in decoded item. |
| 932 | * |
| 933 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 934 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 935 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 936 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 937 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 938 | static QCBORError |
| 939 | QCBOR_Private_DecodeType7(const int nAdditionalInfo, |
| 940 | const uint64_t uArgument, |
| 941 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 942 | { |
| 943 | QCBORError uReturn = QCBOR_SUCCESS; |
| 944 | |
| 945 | /* uAdditionalInfo is 5 bits from the initial byte. Compile time |
| 946 | * checks above make sure uAdditionalInfo values line up with |
| 947 | * uDataType values. DecodeHead() never returns an AdditionalInfo |
| 948 | * > 0x1f so cast is safe. |
| 949 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 950 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 951 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 952 | switch(nAdditionalInfo) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 953 | /* No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they |
| 954 | * are caught before this is called. |
| 955 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 956 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 957 | case HALF_PREC_FLOAT: /* 25 */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 958 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 959 | /* Half-precision is returned as a double. The cast to |
| 960 | * uint16_t is safe because the encoded value was 16 bits. It |
| 961 | * was widened to 64 bits to be passed in here. |
| 962 | */ |
| 963 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 964 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 965 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 966 | uReturn = FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 967 | break; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 968 | case SINGLE_PREC_FLOAT: /* 26 */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 969 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 970 | /* Single precision is normally returned as a double since |
| 971 | * double is widely supported, there is no loss of precision, |
| 972 | * it makes it easy for the caller in most cases and it can |
| 973 | * be converted back to single with no loss of precision |
| 974 | * |
| 975 | * The cast to uint32_t is safe because the encoded value was |
| 976 | * 32 bits. It was widened to 64 bits to be passed in here. |
| 977 | */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 978 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 979 | const float f = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uArgument); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 980 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 981 | /* In the normal case, use HW to convert float to |
| 982 | * double. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 983 | pDecodedItem->val.dfnum = (double)f; |
| 984 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 985 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 986 | /* Use of float HW is disabled, return as a float. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 987 | pDecodedItem->val.fnum = f; |
| 988 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 989 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 990 | /* IEEE754_FloatToDouble() could be used here to return as |
| 991 | * a double, but it adds object code and most likely |
| 992 | * anyone disabling FLOAT HW use doesn't care about floats |
| 993 | * and wants to save object code. |
| 994 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 995 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 996 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 997 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 998 | uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 999 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 1000 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1001 | case DOUBLE_PREC_FLOAT: /* 27 */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1002 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1003 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1004 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1005 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 1006 | uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 1007 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1008 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1009 | case CBOR_SIMPLEV_FALSE: /* 20 */ |
| 1010 | case CBOR_SIMPLEV_TRUE: /* 21 */ |
| 1011 | case CBOR_SIMPLEV_NULL: /* 22 */ |
| 1012 | case CBOR_SIMPLEV_UNDEF: /* 23 */ |
| 1013 | case CBOR_SIMPLE_BREAK: /* 31 */ |
| 1014 | break; /* nothing to do */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1015 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1016 | case CBOR_SIMPLEV_ONEBYTE: /* 24 */ |
| 1017 | if(uArgument <= CBOR_SIMPLE_BREAK) { |
| 1018 | /* This takes out f8 00 ... f8 1f which should be encoded |
| 1019 | * as e0 … f7 |
| 1020 | */ |
| 1021 | uReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1022 | goto Done; |
| 1023 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 1024 | /* FALLTHROUGH */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1025 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1026 | default: /* 0-19 */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1027 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1028 | /* DecodeHead() will make uArgument equal to |
| 1029 | * nAdditionalInfo when nAdditionalInfo is < 24. This cast is |
| 1030 | * safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 1031 | * the double/float cases above |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1032 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1033 | pDecodedItem->val.uSimple = (uint8_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1034 | break; |
| 1035 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1036 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1037 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1038 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1042 | /** |
| 1043 | * @brief Decode text and byte strings |
| 1044 | * |
| 1045 | * @param[in] pAllocator The string allocator or NULL. |
| 1046 | * @param[in] uStrLen The length of the string. |
| 1047 | * @param[in] pUInBuf The surce from which to read the string's bytes. |
| 1048 | * @param[out] pDecodedItem The filled in decoded item. |
| 1049 | * |
| 1050 | * @retval QCBOR_ERR_HIT_END |
| 1051 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1052 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1053 | * |
| 1054 | * The reads @c uStrlen bytes from @c pUInBuf and fills in @c |
| 1055 | * pDecodedItem. If @c pAllocator is not NULL then memory for the |
| 1056 | * string is allocated. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1057 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1058 | static QCBORError |
| 1059 | QCBOR_Private_DecodeBytes(const QCBORInternalAllocator *pAllocator, |
| 1060 | const uint64_t uStrLen, |
| 1061 | UsefulInputBuf *pUInBuf, |
| 1062 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1063 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1064 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1065 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1066 | /* CBOR lengths can be 64 bits, but size_t is not 64 bits on all |
| 1067 | * CPUs. This check makes the casts to size_t below safe. |
| 1068 | * |
| 1069 | * The max is 4 bytes less than the largest sizeof() so this can be |
| 1070 | * tested by putting a SIZE_MAX length in the CBOR test input (no |
| 1071 | * one will care the limit on strings is 4 bytes shorter). |
| 1072 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1073 | if(uStrLen > SIZE_MAX-4) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1074 | uReturn = QCBOR_ERR_STRING_TOO_LONG; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1075 | goto Done; |
| 1076 | } |
| 1077 | |
| 1078 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1079 | if(UsefulBuf_IsNULLC(Bytes)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1080 | /* Failed to get the bytes for this string item */ |
| 1081 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1082 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1083 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1084 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1085 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1086 | /* Note that this is not where allocation to coalesce |
| 1087 | * indefinite-length strings is done. This is for when the caller |
| 1088 | * has requested all strings be allocated. Disabling indefinite |
| 1089 | * length strings also disables this allocate-all option. |
| 1090 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1091 | if(pAllocator) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1092 | /* request to use the string allocator to make a copy */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1093 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1094 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1095 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1096 | goto Done; |
| 1097 | } |
| 1098 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1099 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1100 | goto Done; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1101 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1102 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1103 | (void)pAllocator; |
| 1104 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1105 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1106 | /* Normal case with no string allocator */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1107 | pDecodedItem->val.string = Bytes; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1108 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1109 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1110 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1111 | } |
| 1112 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1113 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1114 | /** |
| 1115 | * @brief Map the CBOR major types for strings to the QCBOR types. |
| 1116 | * |
| 1117 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1118 | * @retturns QCBOR type number. |
| 1119 | * |
| 1120 | * This only works for the two string types. |
| 1121 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1122 | static uint8_t |
| 1123 | QCBOR_Private_ConvertStringMajorTypes(int nCBORMajorType) |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1124 | { |
| 1125 | #if CBOR_MAJOR_TYPE_BYTE_STRING + 4 != QCBOR_TYPE_BYTE_STRING |
| 1126 | #error QCBOR_TYPE_BYTE_STRING no lined up with major type |
| 1127 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1128 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1129 | #if CBOR_MAJOR_TYPE_TEXT_STRING + 4 != QCBOR_TYPE_TEXT_STRING |
| 1130 | #error QCBOR_TYPE_TEXT_STRING no lined up with major type |
| 1131 | #endif |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1132 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1133 | return (uint8_t)(nCBORMajorType + 4); |
| 1134 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1135 | |
| 1136 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1137 | /** |
| 1138 | * @brief Map the CBOR major types for arrays/maps to the QCBOR types. |
| 1139 | * |
| 1140 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1141 | * @retturns QCBOR type number. |
| 1142 | * |
| 1143 | * This only works for the two aggregate types. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1144 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1145 | static uint8_t |
| 1146 | QCBORDecode_Private_ConvertArrayOrMapType(int nCBORMajorType) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1147 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1148 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 1149 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 1150 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1151 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1152 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 1153 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 1154 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1155 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1156 | return (uint8_t)(nCBORMajorType); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1160 | /** |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1161 | * @brief Decode a single primitive data item (decode layer 6). |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1162 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1163 | * @param[in] pUInBuf Input buffer to read data item from. |
| 1164 | * @param[out] pDecodedItem The filled-in decoded item. |
| 1165 | * @param[in] pAllocator The allocator to use for strings or NULL. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1166 | * |
| 1167 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1168 | * @retval QCBOR_ERR_HIT_END |
| 1169 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1170 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1171 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1172 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1173 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1174 | * @retval QCBOR_ERR_BAD_TYPE_7 |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1175 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1176 | * |
| 1177 | * This decodes the most primitive / atomic data item. It does |
| 1178 | * no combing of data items. |
| 1179 | */ |
| 1180 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1181 | QCBOR_Private_DecodeAtomicDataItem(UsefulInputBuf *pUInBuf, |
| 1182 | QCBORItem *pDecodedItem, |
| 1183 | const QCBORInternalAllocator *pAllocator) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1184 | { |
| 1185 | QCBORError uReturn; |
| 1186 | |
| 1187 | /* Get the major type and the argument. The argument could be |
| 1188 | * length of more bytes or the value depending on the major |
| 1189 | * type. nAdditionalInfo is an encoding of the length of the |
| 1190 | * uNumber and is needed to decode floats and doubles. |
| 1191 | */ |
| 1192 | int nMajorType = 0; |
| 1193 | uint64_t uArgument = 0; |
| 1194 | int nAdditionalInfo = 0; |
| 1195 | |
| 1196 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1197 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1198 | uReturn = QCBOR_Private_DecodeHead(pUInBuf, &nMajorType, &uArgument, &nAdditionalInfo); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1199 | if(uReturn) { |
| 1200 | goto Done; |
| 1201 | } |
| 1202 | |
| 1203 | /* At this point the major type and the argument are valid. We've |
| 1204 | * got the type and the argument that starts every CBOR data item. |
| 1205 | */ |
| 1206 | switch (nMajorType) { |
| 1207 | case CBOR_MAJOR_TYPE_POSITIVE_INT: /* Major type 0 */ |
| 1208 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: /* Major type 1 */ |
| 1209 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1210 | uReturn = QCBOR_ERR_BAD_INT; |
| 1211 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1212 | uReturn = QCBOR_Private_DecodeInteger(nMajorType, uArgument, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1213 | } |
| 1214 | break; |
| 1215 | |
| 1216 | case CBOR_MAJOR_TYPE_BYTE_STRING: /* Major type 2 */ |
| 1217 | case CBOR_MAJOR_TYPE_TEXT_STRING: /* Major type 3 */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1218 | pDecodedItem->uDataType = QCBOR_Private_ConvertStringMajorTypes(nMajorType); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1219 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1220 | pDecodedItem->val.string = (UsefulBufC){NULL, QCBOR_STRING_LENGTH_INDEFINITE}; |
| 1221 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1222 | uReturn = QCBOR_Private_DecodeBytes(pAllocator, uArgument, pUInBuf, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1223 | } |
| 1224 | break; |
| 1225 | |
| 1226 | case CBOR_MAJOR_TYPE_ARRAY: /* Major type 4 */ |
| 1227 | case CBOR_MAJOR_TYPE_MAP: /* Major type 5 */ |
| 1228 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1229 | /* Indefinite-length string. */ |
| 1230 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 1231 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
| 1232 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1233 | uReturn = QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED; |
| 1234 | break; |
| 1235 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1236 | } else { |
| 1237 | /* Definite-length string. */ |
| 1238 | if(uArgument > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 1239 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
| 1240 | goto Done; |
| 1241 | } |
| 1242 | /* cast OK because of check above */ |
| 1243 | pDecodedItem->val.uCount = (uint16_t)uArgument; |
| 1244 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1245 | pDecodedItem->uDataType = QCBORDecode_Private_ConvertArrayOrMapType(nMajorType); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1246 | break; |
| 1247 | |
| 1248 | case CBOR_MAJOR_TYPE_TAG: /* Major type 6, tag numbers */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1249 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1250 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1251 | uReturn = QCBOR_ERR_BAD_INT; |
| 1252 | } else { |
| 1253 | pDecodedItem->val.uTagV = uArgument; |
| 1254 | pDecodedItem->uDataType = QCBOR_TYPE_TAG; |
| 1255 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1256 | #else /* QCBOR_DISABLE_TAGS */ |
| 1257 | uReturn = QCBOR_ERR_TAGS_DISABLED; |
| 1258 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1259 | break; |
| 1260 | |
| 1261 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 1262 | /* Major type 7: float, double, true, false, null... */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1263 | uReturn = QCBOR_Private_DecodeType7(nAdditionalInfo, uArgument, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1264 | break; |
| 1265 | |
| 1266 | default: |
| 1267 | /* Never happens because DecodeHead() should never return > 7 */ |
| 1268 | uReturn = QCBOR_ERR_UNSUPPORTED; |
| 1269 | break; |
| 1270 | } |
| 1271 | |
| 1272 | Done: |
| 1273 | return uReturn; |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | /** |
| 1278 | * @brief Process indefinite-length strings (decode layer 5). |
| 1279 | * |
| 1280 | * @param[in] pMe Decoder context |
| 1281 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1282 | * |
| 1283 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1284 | * @retval QCBOR_ERR_HIT_END |
| 1285 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1286 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1287 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1288 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1289 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1290 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1291 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1292 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1293 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1294 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1295 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1296 | * If @c pDecodedItem is not an indefinite-length string, this does nothing. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1297 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1298 | * If it is, this loops getting the subsequent chunk data items that |
| 1299 | * make up the string. The string allocator is used to make a |
| 1300 | * contiguous buffer for the chunks. When this completes @c |
| 1301 | * pDecodedItem contains the put-together string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1302 | * |
| 1303 | * Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1304 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1305 | static QCBORError |
| 1306 | QCBORDecode_Private_GetNextFullString(QCBORDecodeContext *pMe, |
| 1307 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1308 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1309 | /* Aproximate stack usage |
| 1310 | * 64-bit 32-bit |
| 1311 | * local vars 32 16 |
| 1312 | * 2 UsefulBufs 32 16 |
| 1313 | * QCBORItem 56 52 |
| 1314 | * TOTAL 120 74 |
| 1315 | */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1316 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1317 | /* The string allocator is used here for two purposes: 1) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1318 | * coalescing the chunks of an indefinite-length string, 2) |
| 1319 | * allocating storage for every string returned when requested. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1320 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1321 | * The first use is below in this function. Indefinite-length |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1322 | * strings cannot be processed at all without a string allocator. |
| 1323 | * |
| 1324 | * The second used is in DecodeBytes() which is called by |
| 1325 | * GetNext_Item() below. This second use unneccessary for most use |
| 1326 | * and only happens when requested in the call to |
| 1327 | * QCBORDecode_SetMemPool(). If the second use not requested then |
| 1328 | * NULL is passed for the string allocator to GetNext_Item(). |
| 1329 | * |
| 1330 | * QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS disables the string |
| 1331 | * allocator altogether and thus both of these uses. It reduced the |
| 1332 | * decoder object code by about 400 bytes. |
| 1333 | */ |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1334 | const QCBORInternalAllocator *pAllocatorForGetNext = NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1335 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1336 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1337 | const QCBORInternalAllocator *pAllocator = NULL; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1338 | |
| 1339 | if(pMe->StringAllocator.pfAllocator) { |
| 1340 | pAllocator = &(pMe->StringAllocator); |
| 1341 | if(pMe->bStringAllocateAll) { |
| 1342 | pAllocatorForGetNext = pAllocator; |
| 1343 | } |
| 1344 | } |
| 1345 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1346 | |
| 1347 | QCBORError uReturn; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1348 | uReturn = QCBOR_Private_DecodeAtomicDataItem(&(pMe->InBuf), pDecodedItem, pAllocatorForGetNext); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1349 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1350 | goto Done; |
| 1351 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1352 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1353 | /* Only do indefinite-length processing on strings */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1354 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 1355 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1356 | goto Done; |
| 1357 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1358 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1359 | /* Is this a string with an indefinite length? */ |
| 1360 | if(pDecodedItem->val.string.len != QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1361 | goto Done; |
| 1362 | } |
| 1363 | |
| 1364 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1365 | /* Can't decode indefinite-length strings without a string allocator */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1366 | if(pAllocator == NULL) { |
| 1367 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1368 | goto Done; |
| 1369 | } |
| 1370 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1371 | /* Loop getting chunks of the indefinite-length string */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1372 | UsefulBufC FullString = NULLUsefulBufC; |
| 1373 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1374 | for(;;) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1375 | /* Get QCBORItem for next chunk */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1376 | QCBORItem StringChunkItem; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1377 | /* Pass a NULL string allocator to GetNext_Item() because the |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1378 | * individual string chunks in an indefinite-length should not |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1379 | * be allocated. They are always copied in the the contiguous |
| 1380 | * buffer allocated here. |
| 1381 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1382 | uReturn = QCBOR_Private_DecodeAtomicDataItem(&(pMe->InBuf), &StringChunkItem, NULL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1383 | if(uReturn) { |
| 1384 | break; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1385 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1386 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1387 | /* Is item is the marker for end of the indefinite-length string? */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1388 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1389 | /* String is complete */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1390 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1391 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1392 | break; |
| 1393 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1394 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1395 | /* 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] | 1396 | * that introduces the indefinite-length string. This also |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1397 | * 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] | 1398 | * indefinite-length string inside an indefinite-length string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1399 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1400 | if(StringChunkItem.uDataType != uStringType || |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1401 | StringChunkItem.val.string.len == QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1402 | uReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1403 | break; |
| 1404 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1405 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1406 | if (StringChunkItem.val.string.len > 0) { |
| 1407 | /* The first time throurgh FullString.ptr is NULL and this is |
| 1408 | * equivalent to StringAllocator_Allocate(). Subsequently it is |
| 1409 | * not NULL and a reallocation happens. |
| 1410 | */ |
| 1411 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
| 1412 | FullString.ptr, |
| 1413 | FullString.len + StringChunkItem.val.string.len); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1414 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1415 | if(UsefulBuf_IsNULL(NewMem)) { |
| 1416 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
| 1417 | break; |
| 1418 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1419 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1420 | /* Copy new string chunk to the end of accumulated string */ |
| 1421 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1422 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1423 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1424 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1425 | if(uReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1426 | /* Getting the item failed, clean up the allocated memory */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 1427 | StringAllocator_Free(pAllocator, FullString.ptr); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1428 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1429 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1430 | uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED; |
| 1431 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1432 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1433 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1434 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1435 | } |
| 1436 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1437 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1438 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1439 | /** |
| 1440 | * @brief This converts a tag number to a shorter mapped value for storage. |
| 1441 | * |
| 1442 | * @param[in] pMe The decode context. |
| 1443 | * @param[in] uUnMappedTag The tag number to map |
| 1444 | * @param[out] puMappedTagNumer The stored tag number. |
| 1445 | * |
| 1446 | * @return error code. |
| 1447 | * |
| 1448 | * The main point of mapping tag numbers is make QCBORItem |
| 1449 | * smaller. With this mapping storage of 4 tags takes up 8 |
| 1450 | * bytes. Without, it would take up 32 bytes. |
| 1451 | * |
| 1452 | * This maps tag numbers greater than QCBOR_LAST_UNMAPPED_TAG. |
| 1453 | * QCBOR_LAST_UNMAPPED_TAG is a little smaller than MAX_UINT16. |
| 1454 | * |
| 1455 | * See also UnMapTagNumber() and @ref QCBORItem. |
| 1456 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1457 | static QCBORError |
| 1458 | QCBORDecode_Private_MapTagNumber(QCBORDecodeContext *pMe, |
| 1459 | const uint64_t uUnMappedTag, |
| 1460 | uint16_t *puMappedTagNumer) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1461 | { |
| 1462 | if(uUnMappedTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 1463 | unsigned uTagMapIndex; |
| 1464 | /* Is there room in the tag map, or is it in it already? */ |
| 1465 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
| 1466 | if(pMe->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID64) { |
| 1467 | break; |
| 1468 | } |
| 1469 | if(pMe->auMappedTags[uTagMapIndex] == uUnMappedTag) { |
| 1470 | break; |
| 1471 | } |
| 1472 | } |
| 1473 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1474 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 1475 | } |
| 1476 | |
| 1477 | /* Covers the cases where tag is new and were it is already in the map */ |
| 1478 | pMe->auMappedTags[uTagMapIndex] = uUnMappedTag; |
| 1479 | *puMappedTagNumer = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1480 | |
| 1481 | } else { |
| 1482 | *puMappedTagNumer = (uint16_t)uUnMappedTag; |
| 1483 | } |
| 1484 | |
| 1485 | return QCBOR_SUCCESS; |
| 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | /** |
| 1490 | * @brief This converts a mapped tag number to the actual tag number. |
| 1491 | * |
| 1492 | * @param[in] pMe The decode context. |
| 1493 | * @param[in] uMappedTagNumber The stored tag number. |
| 1494 | * |
| 1495 | * @return The actual tag number is returned or |
| 1496 | * @ref CBOR_TAG_INVALID64 on error. |
| 1497 | * |
| 1498 | * This is the reverse of MapTagNumber() |
| 1499 | */ |
| 1500 | static uint64_t |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1501 | QCBORDecode_Private_UnMapTagNumber(const QCBORDecodeContext *pMe, |
| 1502 | const uint16_t uMappedTagNumber) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1503 | { |
| 1504 | if(uMappedTagNumber <= QCBOR_LAST_UNMAPPED_TAG) { |
| 1505 | return uMappedTagNumber; |
| 1506 | } else if(uMappedTagNumber == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1507 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1508 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1509 | /* This won't be negative because of code below in |
| 1510 | * MapTagNumber() |
| 1511 | */ |
| 1512 | const unsigned uIndex = uMappedTagNumber - (QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1513 | return pMe->auMappedTags[uIndex]; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1514 | } |
| 1515 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1516 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1517 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1518 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1519 | /** |
| 1520 | * @brief Aggregate all tags wrapping a data item (decode layer 4). |
| 1521 | * |
| 1522 | * @param[in] pMe Decoder context |
| 1523 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1524 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1525 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1526 | * @retval QCBOR_ERR_HIT_END |
| 1527 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1528 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1529 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1530 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1531 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1532 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1533 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1534 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1535 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1536 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1537 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1538 | * |
| 1539 | * This loops getting atomic data items until one is not a tag |
| 1540 | * number. Usually this is largely pass-through because most |
| 1541 | * item are not tag numbers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1542 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1543 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1544 | QCBORDecode_Private_GetNextTagNumber(QCBORDecodeContext *pMe, |
| 1545 | QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1546 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1547 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1548 | /* Accummulate the tags from multiple items here and then copy them |
| 1549 | * into the last item, the non-tag item. |
| 1550 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1551 | uint16_t auItemsTags[QCBOR_MAX_TAGS_PER_ITEM]; |
| 1552 | |
| 1553 | /* Initialize to CBOR_TAG_INVALID16 */ |
| 1554 | #if CBOR_TAG_INVALID16 != 0xffff |
| 1555 | /* Be sure the memset does the right thing. */ |
| 1556 | #err CBOR_TAG_INVALID16 tag not defined as expected |
| 1557 | #endif |
| 1558 | memset(auItemsTags, 0xff, sizeof(auItemsTags)); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1559 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1560 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1561 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1562 | /* Loop fetching data items until the item fetched is not a tag */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1563 | for(;;) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1564 | QCBORError uErr = QCBORDecode_Private_GetNextFullString(pMe, pDecodedItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1565 | if(uErr != QCBOR_SUCCESS) { |
| 1566 | uReturn = uErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1567 | goto Done; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1568 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1569 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1570 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1571 | /* Successful exit from loop; maybe got some tags, maybe not */ |
| 1572 | memcpy(pDecodedItem->uTags, auItemsTags, sizeof(auItemsTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1573 | break; |
| 1574 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1575 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1576 | if(auItemsTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1577 | /* No room in the tag list */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1578 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1579 | /* Continue on to get all tags wrapping this item even though |
| 1580 | * it is erroring out in the end. This allows decoding to |
| 1581 | * continue. This is a resource limit error, not a problem |
| 1582 | * with being well-formed CBOR. |
| 1583 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1584 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1585 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1586 | /* Slide tags over one in the array to make room at index 0. |
| 1587 | * Must use memmove because the move source and destination |
| 1588 | * overlap. |
| 1589 | */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1590 | memmove(&auItemsTags[1], |
| 1591 | auItemsTags, |
| 1592 | sizeof(auItemsTags) - sizeof(auItemsTags[0])); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1593 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1594 | /* Map the tag */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1595 | uint16_t uMappedTagNumber = 0; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1596 | uReturn = QCBORDecode_Private_MapTagNumber(pMe, pDecodedItem->val.uTagV, &uMappedTagNumber); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1597 | /* Continue even on error so as to consume all tags wrapping |
| 1598 | * this data item so decoding can go on. If MapTagNumber() |
| 1599 | * errors once it will continue to error. |
| 1600 | */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1601 | auItemsTags[0] = uMappedTagNumber; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1602 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1603 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1604 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1605 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1606 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1607 | #else /* QCBOR_DISABLE_TAGS */ |
| 1608 | |
| 1609 | return QCBORDecode_GetNextFullString(pMe, pDecodedItem); |
| 1610 | |
| 1611 | #endif /* QCBOR_DISABLE_TAGS */ |
| 1612 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1613 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1614 | /** |
| 1615 | * @brief Combine a map entry label and value into one item (decode layer 3). |
| 1616 | * |
| 1617 | * @param[in] pMe Decoder context |
| 1618 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1619 | * |
| 1620 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1621 | * @retval QCBOR_ERR_HIT_END |
| 1622 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1623 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1624 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1625 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1626 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1627 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1628 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1629 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1630 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1631 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1632 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1633 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
| 1634 | * @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1635 | * |
| 1636 | * If a the current nesting level is a map, then this |
| 1637 | * combines pairs of items into one data item with a label |
| 1638 | * and value. |
| 1639 | * |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 1640 | * This is passthrough if the current nesting level is |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1641 | * not a map. |
| 1642 | * |
| 1643 | * This also implements maps-as-array mode where a map |
| 1644 | * is treated like an array to allow caller to do their |
| 1645 | * own label processing. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1646 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1647 | static QCBORError |
| 1648 | QCBORDecode_Private_GetNextMapEntry(QCBORDecodeContext *pMe, |
| 1649 | QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1650 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1651 | QCBORError uReturn = QCBORDecode_Private_GetNextTagNumber(pMe, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1652 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1653 | goto Done; |
| 1654 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1655 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1656 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
| 1657 | /* Break can't be a map entry */ |
| 1658 | goto Done; |
| 1659 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1660 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1661 | if(pMe->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
| 1662 | /* Normal decoding of maps -- combine label and value into one item. */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1663 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1664 | if(DecodeNesting_IsCurrentTypeMap(&(pMe->nesting))) { |
| 1665 | /* Save label in pDecodedItem and get the next which will |
| 1666 | * be the real data item. |
| 1667 | */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1668 | QCBORItem LabelItem = *pDecodedItem; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1669 | uReturn = QCBORDecode_Private_GetNextTagNumber(pMe, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1670 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1671 | goto Done; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1672 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1673 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1674 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1675 | |
| 1676 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1677 | /* strings are always good labels */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1678 | pDecodedItem->label.string = LabelItem.val.string; |
| 1679 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1680 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == pMe->uDecodeMode) { |
| 1681 | /* It's not a string and we only want strings */ |
| 1682 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1683 | goto Done; |
| 1684 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1685 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1686 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1687 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1688 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1689 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1690 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1691 | pDecodedItem->label.string = LabelItem.val.string; |
| 1692 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1693 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1694 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1695 | /* label is not an int or a string. It is an arrray |
| 1696 | * or a float or such and this implementation doesn't handle that. |
| 1697 | * Also, tags on labels are ignored. |
| 1698 | */ |
| 1699 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1700 | goto Done; |
| 1701 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1702 | } |
| 1703 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1704 | /* Decoding of maps as arrays to let the caller decide what to do |
| 1705 | * about labels, particularly lables that are not integers or |
| 1706 | * strings. |
| 1707 | */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1708 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1709 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1710 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1711 | goto Done; |
| 1712 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1713 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1714 | /* Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2. |
| 1715 | * Cast is needed because of integer promotion. |
| 1716 | */ |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1717 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1718 | } |
| 1719 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1720 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1721 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1722 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1726 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1727 | /** |
| 1728 | * @brief Peek and see if next data item is a break; |
| 1729 | * |
| 1730 | * @param[in] pUIB UsefulInputBuf to read from. |
| 1731 | * @param[out] pbNextIsBreak Indicate if next was a break or not. |
| 1732 | * |
| 1733 | * @return Any decoding error. |
| 1734 | * |
| 1735 | * See if next item is a CBOR break. If it is, it is consumed, |
| 1736 | * if not it is not consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1737 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1738 | static QCBORError |
| 1739 | QCBOR_Private_NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak) |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1740 | { |
| 1741 | *pbNextIsBreak = false; |
| 1742 | if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1743 | QCBORItem Peek; |
| 1744 | size_t uPeek = UsefulInputBuf_Tell(pUIB); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1745 | QCBORError uReturn = QCBOR_Private_DecodeAtomicDataItem(pUIB, &Peek, NULL); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1746 | if(uReturn != QCBOR_SUCCESS) { |
| 1747 | return uReturn; |
| 1748 | } |
| 1749 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1750 | /* It is not a break, rewind so it can be processed normally. */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1751 | UsefulInputBuf_Seek(pUIB, uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1752 | } else { |
| 1753 | *pbNextIsBreak = true; |
| 1754 | } |
| 1755 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1756 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1757 | return QCBOR_SUCCESS; |
| 1758 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1759 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1760 | |
| 1761 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1762 | /** |
| 1763 | * @brief Ascend up nesting levels if all items in them have been consumed. |
| 1764 | * |
| 1765 | * @param[in] pMe The decode context. |
| 1766 | * @param[in] bMarkEnd If true mark end of maps/arrays with count of zero. |
| 1767 | * |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1768 | * An item was just consumed, now figure out if it was the |
| 1769 | * end of an array/map map that can be closed out. That |
| 1770 | * may in turn close out the above array/map... |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1771 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1772 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1773 | QCBORDecode_Private_NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1774 | { |
| 1775 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1776 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1777 | /* Loop ascending nesting levels as long as there is ascending to do */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1778 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
| 1779 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1780 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
| 1781 | /* Nesting level is bstr-wrapped CBOR */ |
| 1782 | |
| 1783 | /* Ascent for bstr-wrapped CBOR is always by explicit call |
| 1784 | * so no further ascending can happen. |
| 1785 | */ |
| 1786 | break; |
| 1787 | |
| 1788 | } else if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 1789 | /* Level is a definite-length array/map */ |
| 1790 | |
| 1791 | /* Decrement the item count the definite-length array/map */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1792 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1793 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1794 | /* Didn't close out array/map, so all work here is done */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1795 | break; |
| 1796 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1797 | /* All items in a definite-length array were consumed so it |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1798 | * is time to ascend one level. This happens below. |
| 1799 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1800 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1801 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1802 | } else { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1803 | /* Level is an indefinite-length array/map. */ |
| 1804 | |
| 1805 | /* Check for a break which is what ends indefinite-length arrays/maps */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1806 | bool bIsBreak = false; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1807 | uReturn = QCBOR_Private_NextIsBreak(&(pMe->InBuf), &bIsBreak); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1808 | if(uReturn != QCBOR_SUCCESS) { |
| 1809 | goto Done; |
| 1810 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1811 | |
| 1812 | if(!bIsBreak) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1813 | /* 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] | 1814 | break; |
| 1815 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1816 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1817 | /* It was a break in an indefinitelength map / array so |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1818 | * it is time to ascend one level. |
| 1819 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1820 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1821 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1822 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1823 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1824 | |
| 1825 | /* All items in the array/map have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1826 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1827 | /* But ascent in bounded mode is only by explicit call to |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1828 | * QCBORDecode_ExitBoundedMode(). |
| 1829 | */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1830 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1831 | /* Set the count to zero for definite-length arrays to indicate |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1832 | * cursor is at end of bounded array/map */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1833 | if(bMarkEnd) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1834 | /* Used for definite and indefinite to signal end */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1835 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1836 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1837 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1838 | break; |
| 1839 | } |
| 1840 | |
| 1841 | /* Finally, actually ascend one level. */ |
| 1842 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1843 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1844 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1845 | uReturn = QCBOR_SUCCESS; |
| 1846 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1847 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1848 | Done: |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1849 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1850 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1851 | return uReturn; |
| 1852 | } |
| 1853 | |
| 1854 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1855 | /** |
| 1856 | * @brief Ascending & Descending out of nesting levels (decode layer 2). |
| 1857 | * |
| 1858 | * @param[in] pMe Decoder context |
| 1859 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1860 | * |
| 1861 | * @retval QCBOR_ERR_UNSUPPORTED |
| 1862 | * @retval QCBOR_ERR_HIT_END |
| 1863 | * @retval QCBOR_ERR_INT_OVERFLOW |
| 1864 | * @retval QCBOR_ERR_STRING_ALLOCATE |
| 1865 | * @retval QCBOR_ERR_STRING_TOO_LONG |
| 1866 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1867 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1868 | * @retval QCBOR_ERR_BAD_TYPE_7 |
| 1869 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED |
| 1870 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR |
| 1871 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK |
| 1872 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED |
| 1873 | * @retval QCBOR_ERR_TOO_MANY_TAGS |
| 1874 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG |
| 1875 | * @retval QCBOR_ERR_MAP_LABEL_TYPE |
| 1876 | * @retval QCBOR_ERR_NO_MORE_ITEMS |
| 1877 | * @retval QCBOR_ERR_BAD_BREAK |
| 1878 | * @retval QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP |
| 1879 | * |
| 1880 | * This handles the traversal descending into and asecnding out of |
| 1881 | * maps, arrays and bstr-wrapped CBOR. It figures out the ends of |
| 1882 | * definite- and indefinte-length maps and arrays by looking at the |
| 1883 | * item count or finding CBOR breaks. It detects the ends of the |
| 1884 | * top-level sequence and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1885 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1886 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1887 | QCBORDecode_Private_GetNextMapOrArray(QCBORDecodeContext *pMe, |
| 1888 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1889 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1890 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1891 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1892 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1893 | /* If out of bytes to consume, it is either the end of the |
| 1894 | * top-level sequence of some bstr-wrapped CBOR that was entered. |
| 1895 | * |
| 1896 | * In the case of bstr-wrapped CBOR, the length of the |
| 1897 | * UsefulInputBuf was set to that of the bstr-wrapped CBOR. When |
| 1898 | * the bstr-wrapped CBOR is exited, the length is set back to the |
| 1899 | * top-level's length or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1900 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1901 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1902 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1903 | goto Done; |
| 1904 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 1905 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1906 | /* Check to see if at the end of a bounded definite-length map or |
| 1907 | * array. The check for a break ending indefinite-length array is |
| 1908 | * later in QCBORDecode_NestLevelAscender(). |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1909 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1910 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1911 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1912 | goto Done; |
| 1913 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1914 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1915 | /* ==== Next: not at the end, so get another item ==== */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1916 | uReturn = QCBORDecode_Private_GetNextMapEntry(pMe, pDecodedItem); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1917 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 1918 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1919 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1920 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1921 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1922 | /* Breaks ending arrays/maps are processed later in the call to |
| 1923 | * QCBORDecode_NestLevelAscender(). They should never show up here. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1924 | */ |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1925 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1926 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1927 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1928 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1929 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1930 | /* Record the nesting level for this data item before processing |
| 1931 | * any of decrementing and descending. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1932 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1933 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1934 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1935 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1936 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1937 | if(QCBORItem_IsMapOrArray(*pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1938 | /* If the new item is a map or array, descend. |
| 1939 | * |
| 1940 | * Empty indefinite-length maps and arrays are descended into, |
| 1941 | * but then ascended out of in the next chunk of code. |
| 1942 | * |
| 1943 | * Maps and arrays do count as items in the map/array that |
| 1944 | * encloses them so a decrement needs to be done for them too, |
| 1945 | * but that is done only when all the items in them have been |
| 1946 | * processed, not when they are opened with the exception of an |
| 1947 | * empty map or array. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1948 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1949 | QCBORError uDescendErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1950 | uDescendErr = DecodeNesting_DescendMapOrArray(&(pMe->nesting), |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1951 | pDecodedItem->uDataType, |
| 1952 | pDecodedItem->val.uCount); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1953 | if(uDescendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1954 | /* This error is probably a traversal error and it overrides |
| 1955 | * the non-traversal error. |
| 1956 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1957 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1958 | goto Done; |
| 1959 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1962 | if(!QCBORItem_IsMapOrArray(*pDecodedItem) || |
| 1963 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(*pDecodedItem) || |
| 1964 | QCBORItem_IsIndefiniteLengthMapOrArray(*pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1965 | /* The following cases are handled here: |
| 1966 | * - A non-aggregate item like an integer or string |
| 1967 | * - An empty definite-length map or array |
| 1968 | * - An indefinite-length map or array that might be empty or might not. |
| 1969 | * |
| 1970 | * QCBORDecode_NestLevelAscender() does the work of decrementing the count |
| 1971 | * for an definite-length map/array and break detection for an |
| 1972 | * indefinite-0length map/array. If the end of the map/array was |
| 1973 | * reached, then it ascends nesting levels, possibly all the way |
| 1974 | * to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1975 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1976 | QCBORError uAscendErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1977 | uAscendErr = QCBORDecode_Private_NestLevelAscender(pMe, true); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1978 | if(uAscendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1979 | /* This error is probably a traversal error and it overrides |
| 1980 | * the non-traversal error. |
| 1981 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1982 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1983 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 1984 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1985 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1986 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1987 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1988 | /* Tell the caller what level is next. This tells them what |
| 1989 | * maps/arrays were closed out and makes it possible for them to |
| 1990 | * reconstruct the tree with just the information returned in a |
| 1991 | * QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1992 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1993 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1994 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1995 | pDecodedItem->uNextNestLevel = 0; |
| 1996 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1997 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 1998 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1999 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2000 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2001 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2002 | } |
| 2003 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2004 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2005 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2006 | /** |
| 2007 | * @brief Shift 0th tag out of the tag list. |
| 2008 | * |
| 2009 | * pDecodedItem[in,out] The data item to convert. |
| 2010 | * |
| 2011 | * The 0th tag is discarded. \ref CBOR_TAG_INVALID16 is |
| 2012 | * shifted into empty slot at the end of the tag list. |
| 2013 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2014 | static void |
| 2015 | QCBOR_Private_ShiftTags(QCBORItem *pDecodedItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2016 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2017 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM-1; i++) { |
| 2018 | pDecodedItem->uTags[i] = pDecodedItem->uTags[i+1]; |
| 2019 | } |
| 2020 | pDecodedItem->uTags[QCBOR_MAX_TAGS_PER_ITEM-1] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2023 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2024 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2025 | /** |
| 2026 | * @brief Convert different epoch date formats in to the QCBOR epoch date format |
| 2027 | * |
| 2028 | * pDecodedItem[in,out] The data item to convert. |
| 2029 | * |
| 2030 | * @retval QCBOR_ERR_DATE_OVERFLOW |
| 2031 | * @retval QCBOR_ERR_FLOAT_DATE_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2032 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2033 | * @retval QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2034 | * |
| 2035 | * The epoch date tag defined in QCBOR allows for floating-point |
| 2036 | * dates. It even allows a protocol to flop between date formats when |
| 2037 | * ever it wants. Floating-point dates aren't that useful as they are |
| 2038 | * only needed for dates beyond the age of the earth. |
| 2039 | * |
| 2040 | * This converts all the date formats into one format of an unsigned |
| 2041 | * integer plus a floating-point fraction. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2042 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2043 | static QCBORError |
| 2044 | QCBOR_Private_DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2045 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2046 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2047 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2048 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2049 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2050 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2051 | |
| 2052 | switch (pDecodedItem->uDataType) { |
| 2053 | |
| 2054 | case QCBOR_TYPE_INT64: |
| 2055 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 2056 | break; |
| 2057 | |
| 2058 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2059 | /* This only happens for CBOR type 0 > INT64_MAX so it is |
| 2060 | * always an overflow. |
| 2061 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2062 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 2063 | goto Done; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2064 | break; |
| 2065 | |
| 2066 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 2067 | case QCBOR_TYPE_FLOAT: |
| 2068 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2069 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2070 | /* Convert working value to double if input was a float */ |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2071 | const double d = pDecodedItem->uDataType == QCBOR_TYPE_DOUBLE ? |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2072 | pDecodedItem->val.dfnum : |
| 2073 | (double)pDecodedItem->val.fnum; |
| 2074 | |
| 2075 | /* The conversion from float to integer requires overflow |
| 2076 | * detection since floats can be much larger than integers. |
| 2077 | * This implementation errors out on these large float values |
| 2078 | * since they are beyond the age of the earth. |
| 2079 | * |
| 2080 | * These constants for the overflow check are computed by the |
| 2081 | * compiler. They are not computed at run time. |
| 2082 | * |
| 2083 | * The factor of 0x7ff is added/subtracted to avoid a |
| 2084 | * rounding error in the wrong direction when the compiler |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2085 | * computes these constants. There is rounding because a |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2086 | * 64-bit integer has 63 bits of precision where a double |
| 2087 | * only has 53 bits. Without the 0x7ff factor, the compiler |
| 2088 | * may round up and produce a double for the bounds check |
| 2089 | * that is larger than can be stored in a 64-bit integer. The |
| 2090 | * amount of 0x7ff is picked because it has 11 bits set. |
| 2091 | * |
| 2092 | * Without the 0x7ff there is a ~30 minute range of time |
| 2093 | * values 10 billion years in the past and in the future |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2094 | * where this code could go wrong. Some compilers |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2095 | * generate a warning or error without the 0x7ff. |
| 2096 | */ |
| 2097 | const double dDateMax = (double)(INT64_MAX - 0x7ff); |
| 2098 | const double dDateMin = (double)(INT64_MIN + 0x7ff); |
| 2099 | |
| 2100 | if(isnan(d) || d > dDateMax || d < dDateMin) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2101 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2102 | goto Done; |
| 2103 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2104 | |
| 2105 | /* The actual conversion */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2106 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 2107 | pDecodedItem->val.epochDate.fSecondsFraction = |
| 2108 | d - (double)pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2109 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2110 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2111 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2112 | uReturn = QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2113 | goto Done; |
| 2114 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 2115 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2116 | break; |
| 2117 | |
| 2118 | default: |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2119 | /* It's the arrays and maps that are unrecoverable because |
| 2120 | * they are not consumed here. Since this is just an error |
| 2121 | * condition, no extra code is added here to make the error |
| 2122 | * recoverable for non-arrays and maps like strings. */ |
| 2123 | uReturn = QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2124 | goto Done; |
| 2125 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2126 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2127 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 2128 | |
| 2129 | Done: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2130 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2134 | /** |
| 2135 | * @brief Convert the days epoch date. |
| 2136 | * |
| 2137 | * pDecodedItem[in,out] The data item to convert. |
| 2138 | * |
| 2139 | * @retval QCBOR_ERR_DATE_OVERFLOW |
| 2140 | * @retval QCBOR_ERR_FLOAT_DATE_DISABLED |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2141 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2142 | * @retval QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2143 | * |
| 2144 | * This is much simpler than the other epoch date format because |
| 2145 | * floating-porint is not allowed. This is mostly a simple type check. |
| 2146 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2147 | static QCBORError |
| 2148 | QCBOR_Private_DecodeDaysEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2149 | { |
| 2150 | QCBORError uReturn = QCBOR_SUCCESS; |
| 2151 | |
| 2152 | switch (pDecodedItem->uDataType) { |
| 2153 | |
| 2154 | case QCBOR_TYPE_INT64: |
| 2155 | pDecodedItem->val.epochDays = pDecodedItem->val.int64; |
| 2156 | break; |
| 2157 | |
| 2158 | case QCBOR_TYPE_UINT64: |
| 2159 | /* This only happens for CBOR type 0 > INT64_MAX so it is |
| 2160 | * always an overflow. |
| 2161 | */ |
| 2162 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 2163 | goto Done; |
| 2164 | break; |
| 2165 | |
| 2166 | default: |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2167 | /* It's the arrays and maps that are unrecoverable because |
| 2168 | * they are not consumed here. Since this is just an error |
| 2169 | * condition, no extra code is added here to make the error |
| 2170 | * recoverable for non-arrays and maps like strings. */ |
| 2171 | uReturn = QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2172 | goto Done; |
| 2173 | break; |
| 2174 | } |
| 2175 | |
| 2176 | pDecodedItem->uDataType = QCBOR_TYPE_DAYS_EPOCH; |
| 2177 | |
| 2178 | Done: |
| 2179 | return uReturn; |
| 2180 | } |
| 2181 | |
| 2182 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2183 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2184 | |
| 2185 | /* Forward declaration is necessary for |
| 2186 | * QCBORDecode_MantissaAndExponent(). to be able to decode bignum |
| 2187 | * tags in the mantissa. If the mantissa is a decimal fraction or big |
| 2188 | * float in error, this will result in a recurive call to |
| 2189 | * QCBORDecode_MantissaAndExponent(), but the recursion will unwined |
| 2190 | * correctly and the correct error is returned. |
| 2191 | */ |
| 2192 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2193 | QCBORDecode_Private_GetNextTagContent(QCBORDecodeContext *pMe, |
| 2194 | QCBORItem *pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2195 | |
| 2196 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2197 | /** |
| 2198 | * @brief Decode decimal fractions and big floats. |
| 2199 | * |
| 2200 | * @param[in] pMe The decode context. |
| 2201 | * @param[in,out] pDecodedItem On input the array data item that |
| 2202 | * holds the mantissa and exponent. On |
| 2203 | * output the decoded mantissa and |
| 2204 | * exponent. |
| 2205 | * |
| 2206 | * @returns Decoding errors from getting primitive data items or |
| 2207 | * \ref QCBOR_ERR_BAD_EXP_AND_MANTISSA. |
| 2208 | * |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2209 | * When called pDecodedItem must be the array with two members, the |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2210 | * exponent and mantissa. |
| 2211 | * |
| 2212 | * This will fetch and decode the exponent and mantissa and put the |
| 2213 | * result back into pDecodedItem. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2214 | * |
| 2215 | * This does no checking or processing of tag numbers. That is to be |
| 2216 | * done by the code that calls this. |
| 2217 | * |
| 2218 | * This stuffs the type of the mantissa into pDecodedItem with the expectation |
| 2219 | * the caller will process it. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2220 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2221 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2222 | QCBORDecode_Private_ExpMantissa(QCBORDecodeContext *pMe, |
| 2223 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2224 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2225 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2226 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2227 | /* --- Make sure it is an array; track nesting level of members --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2228 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2229 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2230 | goto Done; |
| 2231 | } |
| 2232 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2233 | /* A check for pDecodedItem->val.uCount == 2 would work for |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2234 | * definite-length arrays, but not for indefinite. Instead remember |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2235 | * the nesting level the two integers must be at, which is one |
| 2236 | * deeper than that of the array. |
| 2237 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2238 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 2239 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2240 | /* --- Get the exponent --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2241 | QCBORItem exponentItem; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2242 | uReturn = QCBORDecode_Private_GetNextMapOrArray(pMe, &exponentItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2243 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2244 | goto Done; |
| 2245 | } |
| 2246 | if(exponentItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2247 | /* Array is empty or a map/array encountered when expecting an int */ |
| 2248 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2249 | goto Done; |
| 2250 | } |
| 2251 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2252 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2253 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2254 | * also means that the only data arriving here of type |
| 2255 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2256 | * and thus an error that will get handled in the next else. |
| 2257 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2258 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 2259 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2260 | /* Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2261 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2262 | goto Done; |
| 2263 | } |
| 2264 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2265 | /* --- Get the mantissa --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2266 | QCBORItem mantissaItem; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2267 | uReturn = QCBORDecode_Private_GetNextTagContent(pMe, &mantissaItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2268 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2269 | goto Done; |
| 2270 | } |
| 2271 | if(mantissaItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2272 | /* Mantissa missing or map/array encountered when expecting number */ |
| 2273 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2274 | goto Done; |
| 2275 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2276 | /* Stuff the mantissa data type into the item to send it up to the |
| 2277 | * the next level. */ |
| 2278 | pDecodedItem->uDataType = mantissaItem.uDataType; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2279 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2280 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2281 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2282 | * also means that the only data arriving here of type |
| 2283 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2284 | * and thus an error that will get handled in an else below. |
| 2285 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2286 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2287 | #ifndef QCBOR_DISABLE_TAGS |
| 2288 | /* With tags fully disabled a big number mantissa will error out |
| 2289 | * in the call to QCBORDecode_GetNextWithTags() because it has |
| 2290 | * a tag number. |
| 2291 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2292 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || |
| 2293 | mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2294 | /* Got a good big num mantissa */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2295 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2296 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2297 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2298 | /* Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2299 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2300 | goto Done; |
| 2301 | } |
| 2302 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2303 | /* --- Check that array only has the two numbers --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2304 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2305 | /* Extra items in the decimal fraction / big float */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2306 | /* Improvement: this should probably be an unrecoverable error. */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2307 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2308 | goto Done; |
| 2309 | } |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2310 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2311 | |
| 2312 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2313 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2314 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2315 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2316 | |
| 2317 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2318 | #ifndef QCBOR_DISABLE_TAGS |
| 2319 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2320 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2321 | /** |
| 2322 | * @brief Decode the MIME type tag |
| 2323 | * |
| 2324 | * @param[in,out] pDecodedItem The item to decode. |
| 2325 | * |
| 2326 | * Handle the text and binary MIME type tags. Slightly too complicated |
| 2327 | * f or ProcessTaggedString() because the RFC 7049 MIME type was |
| 2328 | * incorreclty text-only. |
| 2329 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2330 | static QCBORError |
| 2331 | QCBOR_Private_DecodeMIME(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2332 | { |
| 2333 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 2334 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 2335 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2336 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 2337 | } else { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2338 | /* It's the arrays and maps that are unrecoverable because |
| 2339 | * they are not consumed here. Since this is just an error |
| 2340 | * condition, no extra code is added here to make the error |
| 2341 | * recoverable for non-arrays and maps like strings. */ |
| 2342 | return QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2343 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2344 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2345 | return QCBOR_SUCCESS; |
| 2346 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2347 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2348 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2349 | /** |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2350 | * Table of CBOR tags whose content is either a text string or a byte |
| 2351 | * string. The table maps the CBOR tag to the QCBOR type. The high-bit |
| 2352 | * of uQCBORtype indicates the content should be a byte string rather |
| 2353 | * than a text string |
| 2354 | */ |
| 2355 | struct StringTagMapEntry { |
| 2356 | uint16_t uTagNumber; |
| 2357 | uint8_t uQCBORtype; |
| 2358 | }; |
| 2359 | |
| 2360 | #define IS_BYTE_STRING_BIT 0x80 |
| 2361 | #define QCBOR_TYPE_MASK ~IS_BYTE_STRING_BIT |
| 2362 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2363 | static const struct StringTagMapEntry QCBOR_Private_StringTagMap[] = { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2364 | {CBOR_TAG_DATE_STRING, QCBOR_TYPE_DATE_STRING}, |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2365 | {CBOR_TAG_DAYS_STRING, QCBOR_TYPE_DAYS_STRING}, |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2366 | {CBOR_TAG_POS_BIGNUM, QCBOR_TYPE_POSBIGNUM | IS_BYTE_STRING_BIT}, |
| 2367 | {CBOR_TAG_NEG_BIGNUM, QCBOR_TYPE_NEGBIGNUM | IS_BYTE_STRING_BIT}, |
| 2368 | {CBOR_TAG_CBOR, QBCOR_TYPE_WRAPPED_CBOR | IS_BYTE_STRING_BIT}, |
| 2369 | {CBOR_TAG_URI, QCBOR_TYPE_URI}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2370 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2371 | {CBOR_TAG_B64URL, QCBOR_TYPE_BASE64URL}, |
| 2372 | {CBOR_TAG_B64, QCBOR_TYPE_BASE64}, |
| 2373 | {CBOR_TAG_REGEX, QCBOR_TYPE_REGEX}, |
| 2374 | {CBOR_TAG_BIN_UUID, QCBOR_TYPE_UUID | IS_BYTE_STRING_BIT}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2375 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2376 | {CBOR_TAG_CBOR_SEQUENCE, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE | IS_BYTE_STRING_BIT}, |
| 2377 | {CBOR_TAG_INVALID16, QCBOR_TYPE_NONE} |
| 2378 | }; |
| 2379 | |
| 2380 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2381 | /** |
| 2382 | * @brief Process standard CBOR tags whose content is a string |
| 2383 | * |
| 2384 | * @param[in] uTag The tag. |
| 2385 | * @param[in,out] pDecodedItem The data item. |
| 2386 | * |
| 2387 | * @returns This returns QCBOR_SUCCESS if the tag was procssed, |
| 2388 | * \ref QCBOR_ERR_UNSUPPORTED if the tag was not processed and |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2389 | * \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] | 2390 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2391 | * Process the CBOR tags that whose content is a byte string or a text |
| 2392 | * string and for which the string is just passed on to the caller. |
| 2393 | * |
| 2394 | * This maps the CBOR tag to the QCBOR type and checks the content |
| 2395 | * type. Nothing more. It may not be the most important |
Laurence Lundblade | c02e13e | 2020-12-06 05:45:41 -0800 | [diff] [blame] | 2396 | * functionality, but it part of implementing as much of RFC 8949 as |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2397 | * possible. |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2398 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2399 | static QCBORError |
| 2400 | QCBOR_Private_ProcessTaggedString(uint16_t uTag, QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2401 | { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2402 | /* This only works on tags that were not mapped; no need for other yet */ |
| 2403 | if(uTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 2404 | return QCBOR_ERR_UNSUPPORTED; |
| 2405 | } |
| 2406 | |
| 2407 | unsigned uIndex; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2408 | for(uIndex = 0; QCBOR_Private_StringTagMap[uIndex].uTagNumber != CBOR_TAG_INVALID16; uIndex++) { |
| 2409 | if(QCBOR_Private_StringTagMap[uIndex].uTagNumber == uTag) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2410 | break; |
| 2411 | } |
| 2412 | } |
| 2413 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2414 | const uint8_t uQCBORType = QCBOR_Private_StringTagMap[uIndex].uQCBORtype; |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2415 | if(uQCBORType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2416 | /* repurpose this error to mean not handled here */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2417 | return QCBOR_ERR_UNSUPPORTED; |
| 2418 | } |
| 2419 | |
| 2420 | uint8_t uExpectedType = QCBOR_TYPE_TEXT_STRING; |
| 2421 | if(uQCBORType & IS_BYTE_STRING_BIT) { |
| 2422 | uExpectedType = QCBOR_TYPE_BYTE_STRING; |
| 2423 | } |
| 2424 | |
| 2425 | if(pDecodedItem->uDataType != uExpectedType) { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2426 | /* It's the arrays and maps that are unrecoverable because |
| 2427 | * they are not consumed here. Since this is just an error |
| 2428 | * condition, no extra code is added here to make the error |
| 2429 | * recoverable for non-arrays and maps like strings. */ |
| 2430 | return QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2431 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2432 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2433 | pDecodedItem->uDataType = (uint8_t)(uQCBORType & QCBOR_TYPE_MASK); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2434 | return QCBOR_SUCCESS; |
| 2435 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2436 | #endif /* QCBOR_DISABLE_TAGS */ |
| 2437 | |
| 2438 | |
| 2439 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
| 2440 | /* |
| 2441 | * This returns the QCBOR_TYPE for a mantissa and exponent. |
| 2442 | |
| 2443 | Called in one context where there is always a tag |
| 2444 | |
| 2445 | Called in another context where there might be a tag or the caller might say what they are expecting. |
| 2446 | |
| 2447 | 6 possible outputs |
| 2448 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2449 | static uint8_t |
| 2450 | QCBOR_Private_ExpMantissaDataType(const uint16_t uTagToProcess, |
| 2451 | const QCBORItem *pDecodedItem) |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2452 | { |
| 2453 | uint8_t uBase = uTagToProcess == CBOR_TAG_DECIMAL_FRACTION ? |
| 2454 | QCBOR_TYPE_DECIMAL_FRACTION : |
| 2455 | QCBOR_TYPE_BIGFLOAT; |
| 2456 | if(pDecodedItem->uDataType != QCBOR_TYPE_INT64) { |
| 2457 | uBase = (uint8_t)(uBase + pDecodedItem->uDataType - QCBOR_TYPE_POSBIGNUM + 1); |
| 2458 | } |
| 2459 | return uBase; |
| 2460 | } |
| 2461 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2462 | |
| 2463 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2464 | /** |
| 2465 | * @brief Decode tag content for select tags (decoding layer 1). |
| 2466 | * |
| 2467 | * @param[in] pMe The decode context. |
| 2468 | * @param[out] pDecodedItem The decoded item. |
| 2469 | * |
| 2470 | * @return Decoding error code. |
| 2471 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2472 | * CBOR tag numbers for the item were decoded in GetNext_TaggedItem(), |
| 2473 | * but the whole tag was not decoded. Here, the whole tags (tag number |
| 2474 | * and tag content) that are supported by QCBOR are decoded. This is a |
| 2475 | * quick pass through for items that are not tags. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2476 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2477 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2478 | QCBORDecode_Private_GetNextTagContent(QCBORDecodeContext *pMe, |
| 2479 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2480 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2481 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2482 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2483 | uReturn = QCBORDecode_Private_GetNextMapOrArray(pMe, pDecodedItem); |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2484 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2485 | goto Done; |
| 2486 | } |
| 2487 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2488 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2489 | /* When there are no tag numbers for the item, this exits first |
| 2490 | * thing and effectively does nothing. |
| 2491 | * |
| 2492 | * This loops over all the tag numbers accumulated for this item |
| 2493 | * trying to decode and interpret them. This stops at the end of |
| 2494 | * the list or at the first tag number that can't be interpreted by |
| 2495 | * this code. This is effectively a recursive processing of the |
| 2496 | * tags number list that handles nested tags. |
| 2497 | */ |
| 2498 | while(1) { |
| 2499 | /* Don't bother to unmap tags via QCBORITem.uTags since this |
| 2500 | * code only works on tags less than QCBOR_LAST_UNMAPPED_TAG. |
| 2501 | */ |
| 2502 | const uint16_t uTagToProcess = pDecodedItem->uTags[0]; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2503 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2504 | if(uTagToProcess == CBOR_TAG_INVALID16) { |
| 2505 | /* Hit the end of the tag list. A successful exit. */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2506 | break; |
| 2507 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2508 | } else if(uTagToProcess == CBOR_TAG_DATE_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2509 | uReturn = QCBOR_Private_DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2510 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2511 | } else if(uTagToProcess == CBOR_TAG_DAYS_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2512 | uReturn = QCBOR_Private_DecodeDaysEpoch(pDecodedItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2513 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2514 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2515 | } else if(uTagToProcess == CBOR_TAG_DECIMAL_FRACTION || |
| 2516 | uTagToProcess == CBOR_TAG_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2517 | uReturn = QCBORDecode_Private_ExpMantissa(pMe, pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2518 | /* --- Which is it, decimal fraction or a bigfloat? --- */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2519 | pDecodedItem->uDataType = QCBOR_Private_ExpMantissaDataType(uTagToProcess, pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2520 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2521 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2522 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2523 | } else if(uTagToProcess == CBOR_TAG_MIME || |
| 2524 | uTagToProcess == CBOR_TAG_BINARY_MIME) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2525 | uReturn = QCBOR_Private_DecodeMIME(pDecodedItem); |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2526 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2527 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2528 | } else { |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 2529 | /* 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] | 2530 | uReturn = QCBOR_Private_ProcessTaggedString(pDecodedItem->uTags[0], pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2531 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2532 | if(uReturn == QCBOR_ERR_UNSUPPORTED) { |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 2533 | /* It wasn't a passthrough byte/text string tag so it is |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2534 | * an unknown tag. This is the exit from the loop on the |
| 2535 | * first unknown tag. It is a successful exit. |
| 2536 | */ |
| 2537 | uReturn = QCBOR_SUCCESS; |
| 2538 | break; |
| 2539 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2540 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2541 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2542 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2543 | /* Error exit from the loop */ |
| 2544 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2545 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2546 | |
| 2547 | /* A tag was successfully processed, shift it out of the list of |
| 2548 | * tags returned. This is the loop increment. |
| 2549 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2550 | QCBOR_Private_ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2551 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2552 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2553 | |
| 2554 | Done: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2555 | return uReturn; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2556 | } |
| 2557 | |
| 2558 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2559 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2560 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2561 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2562 | QCBORError |
| 2563 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2564 | { |
| 2565 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2566 | uErr = QCBORDecode_Private_GetNextTagContent(pMe, pDecodedItem); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2567 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2568 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2569 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2570 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2571 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | |
| 2575 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2576 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2577 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2578 | QCBORError |
| 2579 | QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2580 | { |
| 2581 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2582 | const UsefulInputBuf Save = pMe->InBuf; |
| 2583 | |
| 2584 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2585 | |
| 2586 | pMe->nesting = SaveNesting; |
| 2587 | pMe->InBuf = Save; |
| 2588 | |
| 2589 | return uErr; |
| 2590 | } |
| 2591 | |
| 2592 | |
| 2593 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2594 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2595 | */ |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2596 | void |
| 2597 | QCBORDecode_VPeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2598 | { |
| 2599 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2600 | return; |
| 2601 | } |
| 2602 | |
| 2603 | pMe->uLastError = (uint8_t)QCBORDecode_PeekNext(pMe, pDecodedItem); |
| 2604 | } |
| 2605 | |
| 2606 | |
| 2607 | /* |
| 2608 | * Public function, see header qcbor/qcbor_decode.h file |
| 2609 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2610 | void |
| 2611 | QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2612 | { |
| 2613 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2614 | return; |
| 2615 | } |
| 2616 | |
| 2617 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2618 | } |
| 2619 | |
| 2620 | |
| 2621 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2622 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2623 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2624 | QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2625 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *pMe, |
| 2626 | QCBORItem *pDecodedItem, |
| 2627 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2628 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2629 | #ifndef QCBOR_DISABLE_TAGS |
| 2630 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2631 | QCBORError uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2632 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2633 | uReturn = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2634 | if(uReturn != QCBOR_SUCCESS) { |
| 2635 | return uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | if(pTags != NULL) { |
| 2639 | pTags->uNumUsed = 0; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2640 | /* Reverse the order because pTags is reverse of QCBORItem.uTags. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2641 | for(int nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >=0; nTagIndex--) { |
| 2642 | if(pDecodedItem->uTags[nTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2643 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2644 | } |
| 2645 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2646 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2647 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2648 | pTags->puTags[pTags->uNumUsed] = QCBORDecode_Private_UnMapTagNumber(pMe,pDecodedItem->uTags[nTagIndex]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2649 | pTags->uNumUsed++; |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | return QCBOR_SUCCESS; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2654 | |
| 2655 | #else /* QCBOR_DISABLE_TAGS */ |
| 2656 | (void)pMe; |
| 2657 | (void)pDecodedItem; |
| 2658 | (void)pTags; |
| 2659 | return QCBOR_ERR_TAGS_DISABLED; |
| 2660 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2664 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2665 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2666 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2667 | bool |
| 2668 | QCBORDecode_IsTagged(QCBORDecodeContext *pMe, |
| 2669 | const QCBORItem *pItem, |
| 2670 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2671 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2672 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2673 | for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
| 2674 | if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2675 | break; |
| 2676 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2677 | if(QCBORDecode_Private_UnMapTagNumber(pMe, pItem->uTags[uTagIndex]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2678 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2679 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2680 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2681 | #else /* QCBOR_TAGS_DISABLED */ |
| 2682 | (void)pMe; |
| 2683 | (void)pItem; |
| 2684 | (void)uTag; |
| 2685 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2686 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2687 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2688 | } |
| 2689 | |
| 2690 | |
| 2691 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2692 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2693 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2694 | QCBORError |
| 2695 | QCBORDecode_PartialFinish(QCBORDecodeContext *pMe, size_t *puConsumed) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2696 | { |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2697 | if(puConsumed != NULL) { |
| 2698 | *puConsumed = pMe->InBuf.cursor; |
| 2699 | } |
| 2700 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2701 | QCBORError uReturn = pMe->uLastError; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2702 | |
| 2703 | if(uReturn != QCBOR_SUCCESS) { |
| 2704 | goto Done; |
| 2705 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2706 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2707 | /* Error out if all the maps/arrays are not closed out */ |
| 2708 | if(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2709 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2710 | goto Done; |
| 2711 | } |
| 2712 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2713 | /* Error out if not all the bytes are consumed */ |
| 2714 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2715 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2716 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2717 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2718 | Done: |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2719 | return uReturn; |
| 2720 | } |
| 2721 | |
| 2722 | |
| 2723 | /* |
| 2724 | * Public function, see header qcbor/qcbor_decode.h file |
| 2725 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2726 | QCBORError |
| 2727 | QCBORDecode_Finish(QCBORDecodeContext *pMe) |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2728 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2729 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2730 | /* Call the destructor for the string allocator if there is one. |
| 2731 | * Always called, even if there are errors; always have to clean up. |
| 2732 | */ |
| 2733 | StringAllocator_Destruct(&(pMe->StringAllocator)); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2734 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2735 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2736 | return QCBORDecode_PartialFinish(pMe, NULL); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2740 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2741 | * Public function, see header qcbor/qcbor_decode.h file |
| 2742 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2743 | // Improvement: make these inline? TODO: |
| 2744 | uint64_t |
| 2745 | QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2746 | const QCBORItem *pItem, |
| 2747 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2748 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2749 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2750 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2751 | return CBOR_TAG_INVALID64; |
| 2752 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2753 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2754 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2755 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2756 | return QCBORDecode_Private_UnMapTagNumber(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2757 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2758 | #else /* QCBOR_DISABLE_TAGS */ |
| 2759 | (void)pMe; |
| 2760 | (void)pItem; |
| 2761 | (void)uIndex; |
| 2762 | |
| 2763 | return CBOR_TAG_INVALID64; |
| 2764 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2765 | } |
| 2766 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2767 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2768 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2769 | * Public function, see header qcbor/qcbor_decode.h file |
| 2770 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2771 | uint64_t |
| 2772 | QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2773 | uint32_t uIndex) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2774 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2775 | #ifndef QCBOR_DISABLE_TAGS |
| 2776 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2777 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2778 | return CBOR_TAG_INVALID64; |
| 2779 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2780 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2781 | return CBOR_TAG_INVALID64; |
| 2782 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2783 | return QCBORDecode_Private_UnMapTagNumber(pMe, pMe->uLastTags[uIndex]); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2784 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2785 | #else /* QCBOR_DISABLE_TAGS */ |
| 2786 | (void)pMe; |
| 2787 | (void)uIndex; |
| 2788 | |
| 2789 | return CBOR_TAG_INVALID64; |
| 2790 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2791 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2792 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2793 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2794 | |
| 2795 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2796 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2797 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2798 | /* =========================================================================== |
| 2799 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2800 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2801 | This implements a simple sting allocator for indefinite-length |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2802 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2803 | implements the function type QCBORStringAllocate and allows easy |
| 2804 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2805 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2806 | This particular allocator is built-in for convenience. The caller |
| 2807 | can implement their own. All of this following code will get |
| 2808 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2809 | |
| 2810 | This is a very primitive memory allocator. It does not track |
| 2811 | individual allocations, only a high-water mark. A free or |
| 2812 | reallocation must be of the last chunk allocated. |
| 2813 | |
| 2814 | The size of the pool and offset to free memory are packed into the |
| 2815 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2816 | the decode context. Since the address of the pool may not be |
| 2817 | aligned, they have to be packed and unpacked as if they were |
| 2818 | serialized data of the wire or such. |
| 2819 | |
| 2820 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2821 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2822 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2823 | |
| 2824 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2825 | static int |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2826 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2827 | { |
| 2828 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2829 | UsefulInputBuf UIB; |
| 2830 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2831 | // Just assume the size here. It was checked during SetUp so |
| 2832 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2833 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2834 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2835 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2836 | return UsefulInputBuf_GetError(&UIB); |
| 2837 | } |
| 2838 | |
| 2839 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2840 | static int |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2841 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2842 | { |
| 2843 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2844 | // length check performed here is useful. |
| 2845 | UsefulOutBuf UOB; |
| 2846 | |
| 2847 | UsefulOutBuf_Init(&UOB, Pool); |
| 2848 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2849 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2850 | return UsefulOutBuf_GetError(&UOB); |
| 2851 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2852 | |
| 2853 | |
| 2854 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2855 | Internal function for an allocation, reallocation free and destuct. |
| 2856 | |
| 2857 | Having only one function rather than one each per mode saves space in |
| 2858 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2859 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2860 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2861 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2862 | static UsefulBuf |
| 2863 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2864 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2865 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2866 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2867 | uint32_t uPoolSize; |
| 2868 | uint32_t uFreeOffset; |
| 2869 | |
| 2870 | if(uNewSize > UINT32_MAX) { |
| 2871 | // This allocator is only good up to 4GB. This check should |
| 2872 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2873 | goto Done; |
| 2874 | } |
| 2875 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2876 | |
| 2877 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2878 | goto Done; |
| 2879 | } |
| 2880 | |
| 2881 | if(uNewSize) { |
| 2882 | if(pMem) { |
| 2883 | // REALLOCATION MODE |
| 2884 | // Calculate pointer to the end of the memory pool. It is |
| 2885 | // assumed that pPool + uPoolSize won't wrap around by |
| 2886 | // assuming the caller won't pass a pool buffer in that is |
| 2887 | // not in legitimate memory space. |
| 2888 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2889 | |
| 2890 | // Check that the pointer for reallocation is in the range of the |
| 2891 | // pool. This also makes sure that pointer math further down |
| 2892 | // doesn't wrap under or over. |
| 2893 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2894 | // Offset to start of chunk for reallocation. This won't |
| 2895 | // wrap under because of check that pMem >= pPool. Cast |
| 2896 | // is safe because the pool is always less than UINT32_MAX |
| 2897 | // because of check in QCBORDecode_SetMemPool(). |
| 2898 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2899 | |
| 2900 | // Check to see if the allocation will fit. uPoolSize - |
| 2901 | // uMemOffset will not wrap under because of check that |
| 2902 | // pMem is in the range of the uPoolSize by check above. |
| 2903 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2904 | ReturnValue.ptr = pMem; |
| 2905 | ReturnValue.len = uNewSize; |
| 2906 | |
| 2907 | // Addition won't wrap around over because uNewSize was |
| 2908 | // checked to be sure it is less than the pool size. |
| 2909 | uFreeOffset = uMemOffset + uNewSize32; |
| 2910 | } |
| 2911 | } |
| 2912 | } else { |
| 2913 | // ALLOCATION MODE |
| 2914 | // uPoolSize - uFreeOffset will not underflow because this |
| 2915 | // pool implementation makes sure uFreeOffset is always |
| 2916 | // smaller than uPoolSize through this check here and |
| 2917 | // reallocation case. |
| 2918 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2919 | ReturnValue.len = uNewSize; |
| 2920 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2921 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2922 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2923 | } |
| 2924 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2925 | if(pMem) { |
| 2926 | // FREE MODE |
| 2927 | // Cast is safe because of limit on pool size in |
| 2928 | // QCBORDecode_SetMemPool() |
| 2929 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2930 | } else { |
| 2931 | // DESTRUCT MODE |
| 2932 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2933 | } |
| 2934 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2935 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2936 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2937 | MemPool_Pack(Pool, uFreeOffset); |
| 2938 | |
| 2939 | Done: |
| 2940 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2941 | } |
| 2942 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2943 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2944 | /* |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 2945 | Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2946 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2947 | QCBORError |
| 2948 | QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 2949 | UsefulBuf Pool, |
| 2950 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2951 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2952 | // The pool size and free mem offset are packed into the beginning |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 2953 | // of the pool memory. This compile time check makes sure the |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2954 | // constant in the header is correct. This check should optimize |
| 2955 | // down to nothing. |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 2956 | #ifdef _MSC_VER |
| 2957 | #pragma warning(push) |
| 2958 | #pragma warning(disable:4127) // conditional expression is constant |
| 2959 | #endif |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2960 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2961 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2962 | } |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 2963 | #ifdef _MSC_VER |
| 2964 | #pragma warning(pop) |
| 2965 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2966 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2967 | // The pool size and free offset packed in to the beginning of pool |
| 2968 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 2969 | // machines. |
| 2970 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2971 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2972 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2973 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2974 | // This checks that the pool buffer given is big enough. |
| 2975 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2976 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2977 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2978 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2979 | QCBORDecode_SetUpAllocator(pMe, MemPool_Function, Pool.ptr, bAllStrings); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2980 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 2981 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2982 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2983 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2984 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 2985 | |
| 2986 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2987 | static void |
| 2988 | QCBORDecode_Private_CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2989 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2990 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2991 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2992 | #else |
| 2993 | (void)pMe; |
| 2994 | (void)pItem; |
| 2995 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2996 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2997 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 2998 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2999 | /** |
| 3000 | * @brief Consume an entire map or array including its contents. |
| 3001 | * |
| 3002 | * @param[in] pMe The decoder context. |
| 3003 | * @param[in] pItemToConsume The array/map whose contents are to be |
| 3004 | * consumed. |
| 3005 | * @param[out] puNextNestLevel The next nesting level after the item was |
| 3006 | * fully consumed. |
| 3007 | * |
| 3008 | * This may be called when @c pItemToConsume is not an array or |
| 3009 | * map. In that case, this is just a pass through for @c puNextNestLevel |
| 3010 | * since there is nothing to do. |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3011 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3012 | static QCBORError |
| 3013 | QCBORDecode_Private_ConsumeItem(QCBORDecodeContext *pMe, |
| 3014 | const QCBORItem *pItemToConsume, |
| 3015 | uint8_t *puNextNestLevel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3016 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3017 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3018 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3019 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3020 | /* 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] | 3021 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 3022 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3023 | if(QCBORItem_IsMapOrArray(*pItemToConsume) && !bIsEmpty) { |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3024 | /* 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] | 3025 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3026 | /* This works for definite- and indefinite-length maps and |
| 3027 | * arrays by using the nesting level |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3028 | */ |
| 3029 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3030 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3031 | if(QCBORDecode_IsUnrecoverableError(uReturn) || |
| 3032 | uReturn == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3033 | goto Done; |
| 3034 | } |
| 3035 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3036 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3037 | *puNextNestLevel = Item.uNextNestLevel; |
| 3038 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3039 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3040 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3041 | } else { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3042 | /* pItemToConsume is not a map or array. Just pass the nesting |
| 3043 | * level through. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3044 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 3045 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3046 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3047 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3048 | |
| 3049 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3050 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3051 | } |
| 3052 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3053 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3054 | /* |
| 3055 | * Public function, see header qcbor/qcbor_decode.h file |
| 3056 | */ |
| 3057 | void |
| 3058 | QCBORDecode_VGetNextConsume(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3059 | { |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3060 | QCBORDecode_VGetNext(pMe, pDecodedItem); |
| 3061 | |
| 3062 | if(pMe->uLastError == QCBOR_SUCCESS) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3063 | pMe->uLastError = (uint8_t)QCBORDecode_Private_ConsumeItem(pMe, pDecodedItem, |
Máté Tóth-Pál | c6d5968 | 2021-05-26 18:55:30 +0200 | [diff] [blame] | 3064 | &pDecodedItem->uNextNestLevel); |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3065 | } |
| 3066 | } |
| 3067 | |
| 3068 | |
| 3069 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3070 | /* Call only on maps and arrays. Rewinds the cursor |
| 3071 | * to the start as if it was just entered. |
| 3072 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3073 | static void |
| 3074 | QCBORDecode_Private_RewindMapOrArray(QCBORDecodeContext *pMe) |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3075 | { |
| 3076 | /* Reset nesting tracking to the deepest bounded level */ |
| 3077 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 3078 | |
| 3079 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3080 | |
| 3081 | /* Reposition traversal cursor to the start of the map/array */ |
| 3082 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 3083 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
| 3084 | } |
| 3085 | |
| 3086 | |
| 3087 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3088 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3089 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3090 | void |
| 3091 | QCBORDecode_Rewind(QCBORDecodeContext *pMe) |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3092 | { |
| 3093 | if(pMe->nesting.pCurrentBounded != NULL) { |
| 3094 | /* In a bounded map, array or bstr-wrapped CBOR */ |
| 3095 | |
| 3096 | if(DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
| 3097 | /* In bstr-wrapped CBOR. */ |
| 3098 | |
| 3099 | /* Reposition traversal cursor to start of wrapping byte string */ |
| 3100 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 3101 | pMe->nesting.pCurrentBounded->u.bs.uBstrStartOffset); |
| 3102 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 3103 | |
| 3104 | } else { |
| 3105 | /* In a map or array */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3106 | QCBORDecode_Private_RewindMapOrArray(pMe); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | } else { |
| 3110 | /* Not in anything bounded */ |
| 3111 | |
| 3112 | /* Reposition traversal cursor to the start of input CBOR */ |
| 3113 | UsefulInputBuf_Seek(&(pMe->InBuf), 0ULL); |
| 3114 | |
| 3115 | /* Reset nesting tracking to beginning of input. */ |
| 3116 | DecodeNesting_Init(&(pMe->nesting)); |
| 3117 | } |
| 3118 | |
| 3119 | pMe->uLastError = QCBOR_SUCCESS; |
| 3120 | } |
| 3121 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3122 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3123 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3124 | |
| 3125 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3126 | /** |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3127 | @brief Search a map for a set of items. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3128 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3129 | @param[in] pMe The decode context to search. |
| 3130 | @param[in,out] pItemArray The items to search for and the items found. |
| 3131 | @param[out] puOffset Byte offset of last item matched. |
| 3132 | @param[in] pCBContext Context for the not-found item call back. |
| 3133 | @param[in] pfCallback Function to call on items not matched in pItemArray. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3134 | |
| 3135 | @retval QCBOR_ERR_NOT_ENTERED Trying to search without having entered a map |
| 3136 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3137 | @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 3138 | were found for one of the labels being |
| 3139 | search for. This duplicate detection is |
| 3140 | only performed for items in pItemArray, |
| 3141 | not every item in the map. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3142 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3143 | @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 3144 | wrong for the matchd label. |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3145 | |
| 3146 | @retval Also errors returned by QCBORDecode_GetNext(). |
| 3147 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3148 | On input pItemArray contains a list of labels and data types |
| 3149 | of items to be found. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3150 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3151 | On output the fully retrieved items are filled in with |
| 3152 | values and such. The label was matched, so it never changes. |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3153 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3154 | If an item was not found, its data type is set to QCBOR_TYPE_NONE. |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3155 | |
| 3156 | 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] | 3157 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3158 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3159 | QCBORDecode_Private_MapSearch(QCBORDecodeContext *pMe, |
| 3160 | QCBORItem *pItemArray, |
| 3161 | size_t *puOffset, |
| 3162 | void *pCBContext, |
| 3163 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3164 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3165 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3166 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 3167 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3168 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3169 | uReturn = pMe->uLastError; |
| 3170 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3171 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3172 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3173 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3174 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 3175 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 3176 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3177 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 3178 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3179 | } |
| 3180 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3181 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 3182 | // It is an empty bounded array or map |
| 3183 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 3184 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3185 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3186 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3187 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3188 | // Nothing is ever found in an empty array or map. All items |
| 3189 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3190 | uReturn = QCBOR_SUCCESS; |
| 3191 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3192 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3193 | } |
| 3194 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3195 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3196 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 3197 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3198 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3199 | QCBORDecode_Private_RewindMapOrArray(pMe); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3200 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3201 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3202 | Loop over all the items in the map or array. Each item |
| 3203 | could be a map or array, but label matching is only at |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 3204 | the main level. This handles definite- and indefinite- |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3205 | length maps and arrays. The only reason this is ever |
| 3206 | called on arrays is to find their end position. |
| 3207 | |
| 3208 | This will always run over all items in order to do |
| 3209 | duplicate detection. |
| 3210 | |
| 3211 | This will exit with failure if it encounters an |
| 3212 | unrecoverable error, but continue on for recoverable |
| 3213 | errors. |
| 3214 | |
| 3215 | If a recoverable error occurs on a matched item, then |
| 3216 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3217 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 3218 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Máté Tóth-Pál | 2f2aa5f | 2021-05-17 21:35:09 +0200 | [diff] [blame] | 3219 | uint8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3220 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3221 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3222 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3223 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3224 | /* Get the item */ |
| 3225 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3226 | QCBORError uResult = QCBORDecode_Private_GetNextTagContent(pMe, &Item); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3227 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3228 | /* Unrecoverable error so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3229 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3230 | goto Done; |
| 3231 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3232 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3233 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3234 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3235 | goto Done; |
| 3236 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3237 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3238 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3239 | bool bMatched = false; |
| 3240 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3241 | if(QCBORItem_MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3242 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3243 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 3244 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3245 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3246 | } |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3247 | if(uResult != QCBOR_SUCCESS) { |
| 3248 | /* The label matches, but the data item is in error */ |
| 3249 | uReturn = uResult; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3250 | goto Done; |
| 3251 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3252 | if(!QCBORItem_MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3253 | /* The data item is not of the type(s) requested */ |
| 3254 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3255 | goto Done; |
| 3256 | } |
| 3257 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3258 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3259 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3260 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3261 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3262 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3263 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3264 | bMatched = true; |
| 3265 | } |
| 3266 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3267 | |
| 3268 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3269 | if(!bMatched && pfCallback != NULL) { |
| 3270 | /* |
| 3271 | Call the callback on unmatched labels. |
| 3272 | (It is tempting to do duplicate detection here, but that would |
| 3273 | require dynamic memory allocation because the number of labels |
| 3274 | that might be encountered is unbounded.) |
| 3275 | */ |
| 3276 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 3277 | if(uReturn != QCBOR_SUCCESS) { |
| 3278 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3279 | } |
| 3280 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3281 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3282 | /* |
| 3283 | Consume the item whether matched or not. This |
| 3284 | does the work of traversing maps and array and |
| 3285 | everything in them. In this loop only the |
| 3286 | items at the current nesting level are examined |
| 3287 | to match the labels. |
| 3288 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3289 | uReturn = QCBORDecode_Private_ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3290 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3291 | goto Done; |
| 3292 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3293 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3294 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3295 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3296 | |
| 3297 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3298 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3299 | // Check here makes sure that this won't accidentally be |
| 3300 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3301 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3302 | // Cast to uint32_t to possibly address cases where SIZE_MAX < UINT32_MAX |
| 3303 | if((uint32_t)uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3304 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3305 | goto Done; |
| 3306 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3307 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 3308 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3309 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3310 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3311 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 3312 | |
| 3313 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3314 | /* 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] | 3315 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3316 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3317 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 3318 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3319 | } |
| 3320 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3321 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3322 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3323 | } |
| 3324 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3325 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3326 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3327 | * Public function, see header qcbor/qcbor_decode.h file |
| 3328 | */ |
| 3329 | void |
| 3330 | QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 3331 | int64_t nLabel, |
| 3332 | uint8_t uQcborType, |
| 3333 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3334 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3335 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3336 | return; |
| 3337 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3338 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3339 | QCBORItem OneItemSeach[2]; |
| 3340 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3341 | OneItemSeach[0].label.int64 = nLabel; |
| 3342 | OneItemSeach[0].uDataType = uQcborType; |
| 3343 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3344 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3345 | QCBORError uReturn = QCBORDecode_Private_MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3346 | |
| 3347 | *pItem = OneItemSeach[0]; |
| 3348 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3349 | if(uReturn != QCBOR_SUCCESS) { |
| 3350 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3351 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3352 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3353 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3354 | } |
| 3355 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3356 | Done: |
| 3357 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3361 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3362 | * Public function, see header qcbor/qcbor_decode.h file |
| 3363 | */ |
| 3364 | void |
| 3365 | QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 3366 | const char *szLabel, |
| 3367 | uint8_t uQcborType, |
| 3368 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3369 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3370 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3371 | return; |
| 3372 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3373 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3374 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3375 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3376 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3377 | OneItemSeach[0].uDataType = uQcborType; |
| 3378 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3379 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3380 | QCBORError uReturn = QCBORDecode_Private_MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3381 | if(uReturn != QCBOR_SUCCESS) { |
| 3382 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3383 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3384 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3385 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3386 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3387 | } |
| 3388 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3389 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3390 | |
| 3391 | Done: |
| 3392 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3396 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3397 | QCBOR_Private_CheckTypeList(const int uDataType, |
| 3398 | const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3399 | { |
| 3400 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3401 | if(uDataType == puTypeList[i]) { /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3402 | return QCBOR_SUCCESS; |
| 3403 | } |
| 3404 | } |
| 3405 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3406 | } |
| 3407 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 3408 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3409 | /** |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3410 | * Match a tag/type specification against the type of the item. |
| 3411 | * |
| 3412 | * @param[in] TagSpec Specification for matching tags. |
| 3413 | * @param[in] pItem The item to check. |
| 3414 | * |
| 3415 | * @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 3416 | * @retval QCBOR_ERR_UNEXPECTED_TYPE \c uDataType is not allowed by @c TagSpec |
| 3417 | * |
| 3418 | * This checks the item data type of untagged items as well as of |
| 3419 | * tagged items against a specification to see if decoding should |
| 3420 | * proceed. |
| 3421 | * |
| 3422 | * This relies on the automatic tag decoding done by QCBOR that turns |
| 3423 | * tag numbers into particular QCBOR_TYPEs so there is no actual |
| 3424 | * comparsion of tag numbers, just of QCBOR_TYPEs. |
| 3425 | * |
| 3426 | * This checks the data item type as possibly representing the tag |
| 3427 | * number or as the tag content type. |
| 3428 | * |
| 3429 | * If QCBOR_DISABLE_TAGS is #defined, this primarily checks the item |
| 3430 | * data type against the allowed tag content types. It will also error out |
| 3431 | * if the caller tries to require a tag because there is no way that can |
| 3432 | * ever be fulfilled. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3433 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3434 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3435 | QCBOR_Private_CheckTagRequirement(const QCBOR_Private_TagSpec TagSpec, |
| 3436 | const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3437 | { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3438 | const int nItemType = pItem->uDataType; /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3439 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 3440 | |
| 3441 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3442 | /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3443 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 3444 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 3445 | /* There are tags that QCBOR couldn't process on this item and |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3446 | * the caller has told us there should not be. |
| 3447 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3448 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3449 | } |
| 3450 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3451 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3452 | /* Must match the tag number and only the tag */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3453 | return QCBOR_Private_CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3454 | } |
| 3455 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3456 | QCBORError uReturn = QCBOR_Private_CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3457 | if(uReturn == QCBOR_SUCCESS) { |
| 3458 | return QCBOR_SUCCESS; |
| 3459 | } |
| 3460 | |
| 3461 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 3462 | /* Must match the content type and only the content type. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3463 | * There was no match just above so it is a fail. */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3464 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3465 | } |
| 3466 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3467 | /* QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG: If here it can match either the tag or the content |
| 3468 | * and it hasn't matched the content, so the end |
| 3469 | * result is whether it matches the tag. This is |
| 3470 | * the tag optional case that the CBOR standard discourages. |
| 3471 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3472 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3473 | return QCBOR_Private_CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3474 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3475 | #else /* QCBOR_DISABLE_TAGS */ |
| 3476 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 3477 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3478 | } |
| 3479 | |
| 3480 | return CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 3481 | |
| 3482 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3483 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3484 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3485 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3486 | // This could be semi-private if need be |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3487 | // TODO: decide what to do |
| 3488 | static void |
| 3489 | QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 3490 | const int64_t nLabel, |
| 3491 | const QCBOR_Private_TagSpec TagSpec, |
| 3492 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3493 | { |
| 3494 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 3495 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3496 | return; |
| 3497 | } |
| 3498 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3499 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3500 | } |
| 3501 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3502 | |
| 3503 | // This could be semi-private if need be |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3504 | static void |
| 3505 | QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 3506 | const char *szLabel, |
| 3507 | const QCBOR_Private_TagSpec TagSpec, |
| 3508 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3509 | { |
| 3510 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 3511 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3512 | return; |
| 3513 | } |
| 3514 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3515 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3516 | } |
| 3517 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3518 | // Semi-private |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3519 | void |
| 3520 | QCBORDecode_Private_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3521 | const int64_t nLabel, |
| 3522 | const QCBOR_Private_TagSpec TagSpec, |
| 3523 | UsefulBufC *pString) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3524 | { |
| 3525 | QCBORItem Item; |
| 3526 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 3527 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3528 | *pString = Item.val.string; |
| 3529 | } |
| 3530 | } |
| 3531 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3532 | // Semi-private |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3533 | void |
| 3534 | QCBORDecode_Private_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3535 | const char * szLabel, |
| 3536 | const QCBOR_Private_TagSpec TagSpec, |
| 3537 | UsefulBufC *pString) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3538 | { |
| 3539 | QCBORItem Item; |
| 3540 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 3541 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3542 | *pString = Item.val.string; |
| 3543 | } |
| 3544 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3545 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3546 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3547 | * Public function, see header qcbor/qcbor_decode.h file |
| 3548 | */ |
| 3549 | void |
| 3550 | QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3551 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3552 | QCBORError uErr = QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, NULL, NULL); |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3553 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3554 | } |
| 3555 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3556 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3557 | * Public function, see header qcbor/qcbor_decode.h file |
| 3558 | */ |
| 3559 | void |
| 3560 | QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 3561 | QCBORItem *pItemList, |
| 3562 | void *pCallbackCtx, |
| 3563 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3564 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3565 | QCBORError uErr = QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3566 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3567 | } |
| 3568 | |
| 3569 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3570 | /** |
| 3571 | * @brief Search for a map/array by label and enter it |
| 3572 | * |
| 3573 | * @param[in] pMe The decode context. |
| 3574 | * @param[in] pSearch The map/array to search for. |
| 3575 | * |
| 3576 | * @c pSearch is expected to contain one item of type map or array |
| 3577 | * with the label specified. The current bounded map will be searched for |
| 3578 | * this and if found will be entered. |
| 3579 | * |
| 3580 | * If the label is not found, or the item found is not a map or array, |
| 3581 | * the error state is set. |
| 3582 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3583 | static void |
| 3584 | QCBORDecode_Private_SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3585 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3586 | // The first item in pSearch is the one that is to be |
| 3587 | // entered. It should be the only one filled in. Any other |
| 3588 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3589 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3590 | return; |
| 3591 | } |
| 3592 | |
| 3593 | size_t uOffset; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3594 | pMe->uLastError = (uint8_t)QCBORDecode_Private_MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3595 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3596 | return; |
| 3597 | } |
| 3598 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3599 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3600 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3601 | return; |
| 3602 | } |
| 3603 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3604 | |
| 3605 | /* The map or array was found. Now enter it. |
| 3606 | * |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3607 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 3608 | * next item for the pre-order traversal cursor to be the map/array |
| 3609 | * found by MapSearch(). The next few lines of code force the |
| 3610 | * cursor to that. |
| 3611 | * |
| 3612 | * There is no need to retain the old cursor because |
| 3613 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 3614 | * beginning of the map/array being entered. |
| 3615 | * |
| 3616 | * The cursor is forced by: 1) setting the input buffer position to |
| 3617 | * the item offset found by MapSearch(), 2) setting the map/array |
| 3618 | * counter to the total in the map/array, 3) setting the nesting |
| 3619 | * level. Setting the map/array counter to the total is not |
| 3620 | * strictly correct, but this is OK because this cursor only needs |
| 3621 | * to be used to get one item and MapSearch() has already found it |
| 3622 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3623 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3624 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3625 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3626 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3627 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3628 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3629 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3630 | QCBORDecode_Private_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3631 | } |
| 3632 | |
| 3633 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3634 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3635 | * Public function, see header qcbor/qcbor_decode.h file |
| 3636 | */ |
| 3637 | void |
| 3638 | QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3639 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3640 | QCBORItem OneItemSeach[2]; |
| 3641 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3642 | OneItemSeach[0].label.int64 = nLabel; |
| 3643 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3644 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3645 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3646 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3647 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3648 | } |
| 3649 | |
| 3650 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3651 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3652 | * Public function, see header qcbor/qcbor_decode.h file |
| 3653 | */ |
| 3654 | void |
| 3655 | QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3656 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3657 | QCBORItem OneItemSeach[2]; |
| 3658 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3659 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3660 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3661 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3662 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3663 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3664 | } |
| 3665 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3666 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3667 | * Public function, see header qcbor/qcbor_decode.h file |
| 3668 | */ |
| 3669 | void |
| 3670 | QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3671 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3672 | QCBORItem OneItemSeach[2]; |
| 3673 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3674 | OneItemSeach[0].label.int64 = nLabel; |
| 3675 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3676 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3677 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3678 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3679 | } |
| 3680 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3681 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3682 | * Public function, see header qcbor/qcbor_decode.h file |
| 3683 | */ |
| 3684 | void |
| 3685 | QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3686 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3687 | QCBORItem OneItemSeach[2]; |
| 3688 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3689 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3690 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3691 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3692 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3693 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3694 | } |
| 3695 | |
| 3696 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3697 | // Semi-private function |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3698 | void |
| 3699 | QCBORDecode_Private_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, |
| 3700 | const uint8_t uType, |
| 3701 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3702 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3703 | QCBORError uErr; |
| 3704 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3705 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3706 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3707 | // Already in error state; do nothing. |
| 3708 | return; |
| 3709 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3710 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3711 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3712 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3713 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3714 | if(uErr != QCBOR_SUCCESS) { |
| 3715 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3716 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3717 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3718 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3719 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3720 | } |
| 3721 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3722 | QCBORDecode_Private_CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3723 | |
| 3724 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3725 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3726 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3727 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3728 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3729 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3730 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3731 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3732 | // Special case to increment nesting level for zero-length maps |
| 3733 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3734 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3735 | } |
| 3736 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3737 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3738 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3739 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3740 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3741 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3742 | if(pItem != NULL) { |
| 3743 | *pItem = Item; |
| 3744 | } |
| 3745 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3746 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3747 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3748 | } |
| 3749 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3750 | |
| 3751 | /* |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3752 | This is the common work for exiting a level that is a bounded map, |
| 3753 | array or bstr wrapped CBOR. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3754 | |
| 3755 | One chunk of work is to set up the pre-order traversal so it is at |
| 3756 | the item just after the bounded map, array or bstr that is being |
| 3757 | exited. This is somewhat complex. |
| 3758 | |
| 3759 | The other work is to level-up the bounded mode to next higest bounded |
| 3760 | mode or the top level if there isn't one. |
| 3761 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3762 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3763 | QCBORDecode_Private_ExitBoundedLevel(QCBORDecodeContext *pMe, |
| 3764 | const uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3765 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3766 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3767 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3768 | /* |
| 3769 | First the pre-order-traversal byte offset is positioned to the |
| 3770 | item just after the bounded mode item that was just consumed. |
| 3771 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3772 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3773 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3774 | /* |
| 3775 | Next, set the current nesting level to one above the bounded level |
| 3776 | that was just exited. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3777 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3778 | DecodeNesting_CheckBoundedType() is always called before this and |
| 3779 | makes sure pCurrentBounded is valid. |
| 3780 | */ |
| 3781 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3782 | |
| 3783 | /* |
| 3784 | This does the complex work of leveling up the pre-order traversal |
| 3785 | when the end of a map or array or another bounded level is |
| 3786 | reached. It may do nothing, or ascend all the way to the top |
| 3787 | level. |
| 3788 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3789 | uErr = QCBORDecode_Private_NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3790 | if(uErr != QCBOR_SUCCESS) { |
| 3791 | goto Done; |
| 3792 | } |
| 3793 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3794 | /* |
| 3795 | This makes the next highest bounded level the current bounded |
| 3796 | level. If there is no next highest level, then no bounded mode is |
| 3797 | in effect. |
| 3798 | */ |
| 3799 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3800 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3801 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3802 | |
| 3803 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3804 | return uErr; |
| 3805 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3806 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3807 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3808 | // Semi-private function |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3809 | void |
| 3810 | QCBORDecode_Private_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, |
| 3811 | const uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3812 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3813 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3814 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3815 | return; |
| 3816 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3817 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3818 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3819 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3820 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3821 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3822 | goto Done; |
| 3823 | } |
| 3824 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3825 | /* |
| 3826 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3827 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3828 | from previous map search, then do a dummy search. |
| 3829 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3830 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3831 | QCBORItem Dummy; |
| 3832 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3833 | uErr = QCBORDecode_Private_MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3834 | if(uErr != QCBOR_SUCCESS) { |
| 3835 | goto Done; |
| 3836 | } |
| 3837 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3838 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3839 | uErr = QCBORDecode_Private_ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3840 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3841 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3842 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3846 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3847 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3848 | QCBORDecode_Private_EnterBstrWrapped(QCBORDecodeContext *pMe, |
| 3849 | const QCBORItem *pItem, |
| 3850 | const uint8_t uTagRequirement, |
| 3851 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3852 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3853 | if(pBstr) { |
| 3854 | *pBstr = NULLUsefulBufC; |
| 3855 | } |
| 3856 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3857 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3858 | /* Already in error state; do nothing. */ |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3859 | return pMe->uLastError; |
| 3860 | } |
| 3861 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3862 | QCBORError uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3863 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3864 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 3865 | { |
| 3866 | uTagRequirement, |
| 3867 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 3868 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 3869 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3870 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3871 | uError = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3872 | if(uError != QCBOR_SUCCESS) { |
| 3873 | goto Done; |
| 3874 | } |
| 3875 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3876 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3877 | /* Reverse the decrement done by GetNext() for the bstr so the |
| 3878 | * increment in QCBORDecode_NestLevelAscender() called by |
| 3879 | * ExitBoundedLevel() will work right. |
| 3880 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3881 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 3882 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3883 | |
| 3884 | if(pBstr) { |
| 3885 | *pBstr = pItem->val.string; |
| 3886 | } |
| 3887 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3888 | /* This saves the current length of the UsefulInputBuf and then |
| 3889 | * narrows the UsefulInputBuf to start and length of the wrapped |
| 3890 | * CBOR that is being entered. |
| 3891 | * |
| 3892 | * Most of these calls are simple inline accessors so this doesn't |
| 3893 | * amount to much code. |
| 3894 | */ |
| 3895 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3896 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3897 | /* This check makes the cast of uPreviousLength to uint32_t below safe. */ |
| 3898 | if(uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3899 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 3900 | goto Done; |
| 3901 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3902 | |
| 3903 | const size_t uStartOfBstr = UsefulInputBuf_PointerToOffset(&(pMe->InBuf), |
| 3904 | pItem->val.string.ptr); |
| 3905 | /* This check makes the cast of uStartOfBstr to uint32_t below safe. */ |
| 3906 | if(uStartOfBstr == SIZE_MAX || uStartOfBstr > QCBOR_MAX_DECODE_INPUT_SIZE) { |
| 3907 | /* This should never happen because pItem->val.string.ptr should |
| 3908 | * always be valid since it was just returned. |
| 3909 | */ |
| 3910 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3911 | goto Done; |
| 3912 | } |
| 3913 | |
| 3914 | const size_t uEndOfBstr = uStartOfBstr + pItem->val.string.len; |
| 3915 | |
| 3916 | UsefulInputBuf_Seek(&(pMe->InBuf), uStartOfBstr); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 3917 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3918 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3919 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 3920 | (uint32_t)uPreviousLength, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3921 | (uint32_t)uStartOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3922 | Done: |
| 3923 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3927 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3928 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3929 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3930 | void |
| 3931 | QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
| 3932 | const uint8_t uTagRequirement, |
| 3933 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3934 | { |
| 3935 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3936 | // Already in error state; do nothing. |
| 3937 | return; |
| 3938 | } |
| 3939 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3940 | /* Get the data item that is the byte string being entered */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 3941 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3942 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 3943 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3944 | return; |
| 3945 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3946 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3947 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 3948 | &Item, |
| 3949 | uTagRequirement, |
| 3950 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3951 | } |
| 3952 | |
| 3953 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3954 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3955 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3956 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3957 | void |
| 3958 | QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
| 3959 | const int64_t nLabel, |
| 3960 | const uint8_t uTagRequirement, |
| 3961 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3962 | { |
| 3963 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3964 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3965 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3966 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 3967 | &Item, |
| 3968 | uTagRequirement, |
| 3969 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3970 | } |
| 3971 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3972 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3973 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3974 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3975 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3976 | void |
| 3977 | QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
| 3978 | const char *szLabel, |
| 3979 | const uint8_t uTagRequirement, |
| 3980 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3981 | { |
| 3982 | QCBORItem Item; |
| 3983 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 3984 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3985 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 3986 | &Item, |
| 3987 | uTagRequirement, |
| 3988 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 3989 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3990 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3991 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3992 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3993 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3994 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3995 | void |
| 3996 | QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3997 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3998 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3999 | // Already in error state; do nothing. |
| 4000 | return; |
| 4001 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4002 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 4003 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4004 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4005 | return; |
| 4006 | } |
| 4007 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4008 | const uint32_t uEndOfBstr = (uint32_t)UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
| 4009 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4010 | /* |
| 4011 | Reset the length of the UsefulInputBuf to what it was before |
| 4012 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4013 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 4014 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4015 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4016 | |
| 4017 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4018 | QCBORError uErr = QCBORDecode_Private_ExitBoundedLevel(pMe, uEndOfBstr); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4019 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4020 | } |
| 4021 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4022 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4023 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 4024 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4025 | static void |
| 4026 | QCBORDecode_Private_ProcessBool(QCBORDecodeContext *pMe, |
| 4027 | const QCBORItem *pItem, |
| 4028 | bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4029 | { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4030 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4031 | /* Already in error state, do nothing */ |
| 4032 | return; |
| 4033 | } |
| 4034 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4035 | switch(pItem->uDataType) { |
| 4036 | case QCBOR_TYPE_TRUE: |
| 4037 | *pBool = true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4038 | break; |
| 4039 | |
| 4040 | case QCBOR_TYPE_FALSE: |
| 4041 | *pBool = false; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4042 | break; |
| 4043 | |
| 4044 | default: |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4045 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4046 | break; |
| 4047 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4048 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4049 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4050 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4051 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4052 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4053 | * Public function, see header qcbor/qcbor_decode.h file |
| 4054 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4055 | void |
| 4056 | QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4057 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4058 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4059 | /* Already in error state, do nothing */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4060 | return; |
| 4061 | } |
| 4062 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4063 | QCBORItem Item; |
| 4064 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4065 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4066 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4067 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4068 | } |
| 4069 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4070 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4071 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4072 | * Public function, see header qcbor/qcbor_decode.h file |
| 4073 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4074 | void |
| 4075 | QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, |
| 4076 | const int64_t nLabel, |
| 4077 | bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4078 | { |
| 4079 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4080 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4081 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4082 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4085 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4086 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4087 | * Public function, see header qcbor/qcbor_decode.h file |
| 4088 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4089 | void |
| 4090 | QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, |
| 4091 | const char *szLabel, |
| 4092 | bool *pValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4093 | { |
| 4094 | QCBORItem Item; |
| 4095 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 4096 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4097 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4098 | } |
| 4099 | |
| 4100 | |
| 4101 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4102 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4103 | static void |
| 4104 | QCBORDecode_Private_ProcessEpochDate(QCBORDecodeContext *pMe, |
| 4105 | QCBORItem *pItem, |
| 4106 | const uint8_t uTagRequirement, |
| 4107 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4108 | { |
| 4109 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4110 | // Already in error state, do nothing |
| 4111 | return; |
| 4112 | } |
| 4113 | |
| 4114 | QCBORError uErr; |
| 4115 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4116 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4117 | { |
| 4118 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4119 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4120 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4121 | }; |
| 4122 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4123 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4124 | if(uErr != QCBOR_SUCCESS) { |
| 4125 | goto Done; |
| 4126 | } |
| 4127 | |
| 4128 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4129 | uErr = QCBOR_Private_DecodeDateEpoch(pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4130 | if(uErr != QCBOR_SUCCESS) { |
| 4131 | goto Done; |
| 4132 | } |
| 4133 | } |
| 4134 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4135 | // Save the tags in the last item's tags in the decode context |
| 4136 | // for QCBORDecode_GetNthTagOfLast() |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4137 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4138 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4139 | *pnTime = pItem->val.epochDate.nSeconds; |
| 4140 | |
| 4141 | Done: |
| 4142 | pMe->uLastError = (uint8_t)uErr; |
| 4143 | } |
| 4144 | |
| 4145 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4146 | void |
| 4147 | QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
| 4148 | uint8_t uTagRequirement, |
| 4149 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4150 | { |
| 4151 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4152 | // Already in error state, do nothing |
| 4153 | return; |
| 4154 | } |
| 4155 | |
| 4156 | QCBORItem Item; |
| 4157 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4158 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4159 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | |
| 4163 | void |
| 4164 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 4165 | int64_t nLabel, |
| 4166 | uint8_t uTagRequirement, |
| 4167 | int64_t *pnTime) |
| 4168 | { |
| 4169 | QCBORItem Item; |
| 4170 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4171 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | |
| 4175 | void |
| 4176 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 4177 | const char *szLabel, |
| 4178 | uint8_t uTagRequirement, |
| 4179 | int64_t *pnTime) |
| 4180 | { |
| 4181 | QCBORItem Item; |
| 4182 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4183 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4184 | } |
| 4185 | |
| 4186 | |
| 4187 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4188 | /* |
| 4189 | * Common processing for the RFC 8943 day-count tag. Mostly |
| 4190 | * make sure the tag content is correct and copy forward any |
| 4191 | * further other tag numbers. |
| 4192 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4193 | static void |
| 4194 | QCBORDecode_Private_ProcessEpochDays(QCBORDecodeContext *pMe, |
| 4195 | QCBORItem *pItem, |
| 4196 | uint8_t uTagRequirement, |
| 4197 | int64_t *pnDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4198 | { |
| 4199 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4200 | /* Already in error state, do nothing */ |
| 4201 | return; |
| 4202 | } |
| 4203 | |
| 4204 | QCBORError uErr; |
| 4205 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4206 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4207 | { |
| 4208 | uTagRequirement, |
| 4209 | {QCBOR_TYPE_DAYS_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4210 | {QCBOR_TYPE_INT64, QCBOR_TYPE_UINT64, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 4211 | }; |
| 4212 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4213 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4214 | if(uErr != QCBOR_SUCCESS) { |
| 4215 | goto Done; |
| 4216 | } |
| 4217 | |
| 4218 | if(pItem->uDataType != QCBOR_TYPE_DAYS_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4219 | uErr = QCBOR_Private_DecodeDaysEpoch(pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4220 | if(uErr != QCBOR_SUCCESS) { |
| 4221 | goto Done; |
| 4222 | } |
| 4223 | } |
| 4224 | |
| 4225 | /* Save the tags in the last item's tags in the decode context |
| 4226 | * for QCBORDecode_GetNthTagOfLast() |
| 4227 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4228 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4229 | |
| 4230 | *pnDays = pItem->val.epochDays; |
| 4231 | |
| 4232 | Done: |
| 4233 | pMe->uLastError = (uint8_t)uErr; |
| 4234 | } |
| 4235 | |
| 4236 | |
| 4237 | /* |
| 4238 | * Public function, see header qcbor/qcbor_decode.h |
| 4239 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4240 | void |
| 4241 | QCBORDecode_GetEpochDays(QCBORDecodeContext *pMe, |
| 4242 | uint8_t uTagRequirement, |
| 4243 | int64_t *pnDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4244 | { |
| 4245 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4246 | /* Already in error state, do nothing */ |
| 4247 | return; |
| 4248 | } |
| 4249 | |
| 4250 | QCBORItem Item; |
| 4251 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4252 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4253 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4254 | } |
| 4255 | |
| 4256 | |
| 4257 | /* |
| 4258 | * Public function, see header qcbor/qcbor_decode.h |
| 4259 | */ |
| 4260 | void |
| 4261 | QCBORDecode_GetEpochDaysInMapN(QCBORDecodeContext *pMe, |
| 4262 | int64_t nLabel, |
| 4263 | uint8_t uTagRequirement, |
| 4264 | int64_t *pnDays) |
| 4265 | { |
| 4266 | QCBORItem Item; |
| 4267 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4268 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4269 | } |
| 4270 | |
| 4271 | |
| 4272 | /* |
| 4273 | * Public function, see header qcbor/qcbor_decode.h |
| 4274 | */ |
| 4275 | void |
| 4276 | QCBORDecode_GetEpochDaysInMapSZ(QCBORDecodeContext *pMe, |
| 4277 | const char *szLabel, |
| 4278 | uint8_t uTagRequirement, |
| 4279 | int64_t *pnDays) |
| 4280 | { |
| 4281 | QCBORItem Item; |
| 4282 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4283 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | |
| 4287 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4288 | /* |
| 4289 | * @brief Get a string that matches the type/tag specification. |
| 4290 | */ |
| 4291 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4292 | QCBORDecode_Private_GetTaggedString(QCBORDecodeContext *pMe, |
| 4293 | const QCBOR_Private_TagSpec TagSpec, |
| 4294 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4295 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4296 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4297 | /* Already in error state, do nothing */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4298 | return; |
| 4299 | } |
| 4300 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4301 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4302 | QCBORItem Item; |
| 4303 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4304 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 4305 | if(uError != QCBOR_SUCCESS) { |
| 4306 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4307 | return; |
| 4308 | } |
| 4309 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4310 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4311 | |
| 4312 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4313 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 4314 | } else { |
| 4315 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4316 | } |
| 4317 | } |
| 4318 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4319 | |
| 4320 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4321 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4322 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4323 | QCBOR_Private_ProcessBigNum(const uint8_t uTagRequirement, |
| 4324 | const QCBORItem *pItem, |
| 4325 | UsefulBufC *pValue, |
| 4326 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4327 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4328 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4329 | { |
| 4330 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4331 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4332 | {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] | 4333 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4334 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4335 | QCBORError uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4336 | if(uErr != QCBOR_SUCCESS) { |
| 4337 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4338 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4339 | |
| 4340 | *pValue = pItem->val.string; |
| 4341 | |
| 4342 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 4343 | *pbIsNegative = false; |
| 4344 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 4345 | *pbIsNegative = true; |
| 4346 | } |
| 4347 | |
| 4348 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4352 | /* |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4353 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4354 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4355 | void |
| 4356 | QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 4357 | const uint8_t uTagRequirement, |
| 4358 | UsefulBufC *pValue, |
| 4359 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4360 | { |
| 4361 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4362 | // Already in error state, do nothing |
| 4363 | return; |
| 4364 | } |
| 4365 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4366 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4367 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4368 | if(uError != QCBOR_SUCCESS) { |
| 4369 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4370 | return; |
| 4371 | } |
| 4372 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4373 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4374 | &Item, |
| 4375 | pValue, |
| 4376 | pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4377 | } |
| 4378 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4379 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4380 | /* |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4381 | Public function, see header qcbor/qcbor_decode.h |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4382 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4383 | void |
| 4384 | QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 4385 | const int64_t nLabel, |
| 4386 | const uint8_t uTagRequirement, |
| 4387 | UsefulBufC *pValue, |
| 4388 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4389 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4390 | QCBORItem Item; |
| 4391 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4392 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4393 | return; |
| 4394 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4395 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4396 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4397 | &Item, |
| 4398 | pValue, |
| 4399 | pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4400 | } |
| 4401 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4402 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4403 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4404 | * Public function, see header qcbor/qcbor_decode.h |
| 4405 | */ |
| 4406 | void |
| 4407 | QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 4408 | const char *szLabel, |
| 4409 | const uint8_t uTagRequirement, |
| 4410 | UsefulBufC *pValue, |
| 4411 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4412 | { |
| 4413 | QCBORItem Item; |
| 4414 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4415 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4416 | return; |
| 4417 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4418 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4419 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4420 | &Item, |
| 4421 | pValue, |
| 4422 | pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4423 | } |
| 4424 | |
| 4425 | |
| 4426 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4427 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4428 | // Semi private |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4429 | QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4430 | QCBORDecode_Private_GetMIME(const uint8_t uTagRequirement, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4431 | const QCBORItem *pItem, |
| 4432 | UsefulBufC *pMessage, |
| 4433 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4434 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4435 | const QCBOR_Private_TagSpec TagSpecText = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4436 | { |
| 4437 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4438 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4439 | {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] | 4440 | }; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4441 | const QCBOR_Private_TagSpec TagSpecBinary = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4442 | { |
| 4443 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4444 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4445 | {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] | 4446 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4447 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4448 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4449 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4450 | if(QCBOR_Private_CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4451 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4452 | if(pbIsTag257 != NULL) { |
| 4453 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4454 | } |
| 4455 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4456 | } else if(QCBOR_Private_CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4457 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4458 | if(pbIsTag257 != NULL) { |
| 4459 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4460 | } |
| 4461 | uReturn = QCBOR_SUCCESS; |
| 4462 | |
| 4463 | } else { |
| 4464 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 4465 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4466 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4467 | return uReturn; |
| 4468 | } |
| 4469 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4470 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 4471 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4472 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4473 | |
| 4474 | |
| 4475 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4476 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4477 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4478 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4479 | |
| 4480 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4481 | /** |
| 4482 | * @brief Base 10 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 4483 | * |
| 4484 | * @param[in] uMantissa The unsigned integer mantissa. |
| 4485 | * @param[in] nExponent The signed integer exponent. |
| 4486 | * @param[out] puResult Place to return the unsigned integer result. |
| 4487 | * |
| 4488 | * This computes: mantissa * 10 ^^ exponent as for a decimal fraction. The output is a 64-bit |
| 4489 | * unsigned integer. |
| 4490 | * |
| 4491 | * There are many inputs for which the result will not fit in the |
| 4492 | * 64-bit integer and \ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
| 4493 | * be returned. |
| 4494 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4495 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4496 | QCBOR_Private_Exponentitate10(const uint64_t uMantissa, |
| 4497 | int64_t nExponent, |
| 4498 | uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4499 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4500 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4501 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4502 | if(uResult != 0) { |
| 4503 | /* This loop will run a maximum of 19 times because |
| 4504 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 4505 | * exit with the overflow error |
| 4506 | */ |
| 4507 | for(; nExponent > 0; nExponent--) { |
| 4508 | if(uResult > UINT64_MAX / 10) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4509 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4510 | } |
| 4511 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4512 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4513 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4514 | for(; nExponent < 0; nExponent++) { |
| 4515 | uResult = uResult / 10; |
| 4516 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4517 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4518 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4519 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4520 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4521 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4522 | |
| 4523 | *puResult = uResult; |
| 4524 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4525 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4529 | /** |
| 4530 | * @brief Base 2 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 4531 | * |
| 4532 | * @param[in] uMantissa The unsigned integer mantissa. |
| 4533 | * @param[in] nExponent The signed integer exponent. |
| 4534 | * @param[out] puResult Place to return the unsigned integer result. |
| 4535 | * |
| 4536 | * This computes: mantissa * 2 ^^ exponent as for a big float. The |
| 4537 | * output is a 64-bit unsigned integer. |
| 4538 | * |
| 4539 | * There are many inputs for which the result will not fit in the |
| 4540 | * 64-bit integer and \ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
| 4541 | * be returned. |
| 4542 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4543 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4544 | QCBOR_Private_Exponentitate2(const uint64_t uMantissa, |
| 4545 | int64_t nExponent, |
| 4546 | uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4547 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4548 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4549 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4550 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4551 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4552 | /* This loop will run a maximum of 64 times because INT64_MAX < |
| 4553 | * 2^31. More than that will cause exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4554 | */ |
| 4555 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4556 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4557 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4558 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4559 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4560 | nExponent--; |
| 4561 | } |
| 4562 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4563 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4564 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4565 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4566 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4567 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4568 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4569 | } |
| 4570 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4571 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4572 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4573 | return QCBOR_SUCCESS; |
| 4574 | } |
| 4575 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4576 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4577 | /** |
| 4578 | * @brief Exponentiate a signed mantissa and signed exponent to produce a signed result. |
| 4579 | * |
| 4580 | * @param[in] nMantissa Signed integer mantissa. |
| 4581 | * @param[in] nExponent Signed integer exponent. |
| 4582 | * @param[out] pnResult Place to put the signed integer result. |
| 4583 | * @param[in] pfExp Exponentiation function. |
| 4584 | * |
| 4585 | * @returns Error code |
| 4586 | * |
| 4587 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4588 | * produces an unsigned result. This converts the mantissa from signed |
| 4589 | * and converts the result to signed. The exponentiation function is |
| 4590 | * 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] | 4591 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4592 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4593 | QCBOR_Private_ExponentiateNN(const int64_t nMantissa, |
| 4594 | const int64_t nExponent, |
| 4595 | int64_t *pnResult, |
| 4596 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4597 | { |
| 4598 | uint64_t uResult; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4599 | uint64_t uMantissa; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4600 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4601 | /* Take the absolute value and put it into an unsigned. */ |
| 4602 | if(nMantissa >= 0) { |
| 4603 | /* Positive case is straightforward */ |
| 4604 | uMantissa = (uint64_t)nMantissa; |
| 4605 | } else if(nMantissa != INT64_MIN) { |
| 4606 | /* The common negative case. See next. */ |
| 4607 | uMantissa = (uint64_t)-nMantissa; |
| 4608 | } else { |
| 4609 | /* int64_t and uint64_t are always two's complement per the |
| 4610 | * C standard (and since QCBOR uses these it only works with |
| 4611 | * two's complement, which is pretty much universal these |
| 4612 | * days). The range of a negative two's complement integer is |
| 4613 | * one more that than a positive, so the simple code above might |
| 4614 | * not work all the time because you can't simply negate the |
| 4615 | * value INT64_MIN because it can't be represented in an |
| 4616 | * int64_t. -INT64_MIN can however be represented in a |
| 4617 | * uint64_t. Some compilers seem to recognize this case for the |
| 4618 | * above code and put the correct value in uMantissa, however |
| 4619 | * they are not required to do this by the C standard. This next |
| 4620 | * line does however work for all compilers. |
| 4621 | * |
| 4622 | * This does assume two's complement where -INT64_MIN == |
| 4623 | * INT64_MAX + 1 (which wouldn't be true for one's complement or |
| 4624 | * sign and magnitude (but we know we're using two's complement |
| 4625 | * because int64_t requires it)). |
| 4626 | * |
| 4627 | * See these, particularly the detailed commentary: |
| 4628 | * https://stackoverflow.com/questions/54915742/does-c99-mandate-a-int64-t-type-be-available-always |
| 4629 | * https://stackoverflow.com/questions/37301078/is-negating-int-min-undefined-behaviour |
| 4630 | */ |
| 4631 | uMantissa = (uint64_t)INT64_MAX+1; |
| 4632 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4633 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4634 | /* Call the exponentiator passed for either base 2 or base 10. |
| 4635 | * Here is where most of the overflow errors are caught. */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4636 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 4637 | if(uReturn) { |
| 4638 | return uReturn; |
| 4639 | } |
| 4640 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4641 | /* Convert back to the sign of the original mantissa */ |
| 4642 | if(nMantissa >= 0) { |
| 4643 | if(uResult > INT64_MAX) { |
| 4644 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4645 | } |
| 4646 | *pnResult = (int64_t)uResult; |
| 4647 | } else { |
| 4648 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 4649 | * of INT64_MIN. This assumes two's compliment representation |
| 4650 | * where INT64_MIN is one increment farther from 0 than |
| 4651 | * INT64_MAX. Trying to write -INT64_MIN doesn't work to get |
| 4652 | * this because the compiler makes it an int64_t which can't |
| 4653 | * represent -INT64_MIN. Also see above. |
| 4654 | */ |
| 4655 | if(uResult > (uint64_t)INT64_MAX+1) { |
| 4656 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4657 | } |
| 4658 | *pnResult = -(int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4659 | } |
| 4660 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4661 | return QCBOR_SUCCESS; |
| 4662 | } |
| 4663 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4664 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4665 | /** |
| 4666 | * @brief Exponentiate an unsigned mantissa and signed exponent to produce an unsigned result. |
| 4667 | * |
| 4668 | * @param[in] nMantissa Signed integer mantissa. |
| 4669 | * @param[in] nExponent Signed integer exponent. |
| 4670 | * @param[out] puResult Place to put the signed integer result. |
| 4671 | * @param[in] pfExp Exponentiation function. |
| 4672 | * |
| 4673 | * @returns Error code |
| 4674 | * |
| 4675 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4676 | * produces an unsigned result. This errors out if the mantissa |
| 4677 | * is negative because the output is unsigned. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4678 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4679 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4680 | QCBOR_Private_ExponentitateNU(const int64_t nMantissa, |
| 4681 | const int64_t nExponent, |
| 4682 | uint64_t *puResult, |
| 4683 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4684 | { |
| 4685 | if(nMantissa < 0) { |
| 4686 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4687 | } |
| 4688 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4689 | /* Cast to unsigned is OK because of check for negative. |
| 4690 | * Cast to unsigned is OK because UINT64_MAX > INT64_MAX. |
| 4691 | * Exponentiation is straight forward |
| 4692 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4693 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 4694 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4695 | |
| 4696 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4697 | /** |
| 4698 | * @brief Exponentiate an usnigned mantissa and unsigned exponent to produce an unsigned result. |
| 4699 | * |
| 4700 | * @param[in] uMantissa Unsigned integer mantissa. |
| 4701 | * @param[in] nExponent Unsigned integer exponent. |
| 4702 | * @param[out] puResult Place to put the unsigned integer result. |
| 4703 | * @param[in] pfExp Exponentiation function. |
| 4704 | * |
| 4705 | * @returns Error code |
| 4706 | * |
| 4707 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4708 | * produces an unsigned result so this is just a wrapper that does |
| 4709 | * nothing (and is likely inlined). |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4710 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4711 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4712 | QCBOR_Private_ExponentitateUU(const uint64_t uMantissa, |
| 4713 | const int64_t nExponent, |
| 4714 | uint64_t *puResult, |
| 4715 | fExponentiator pfExp) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4716 | { |
| 4717 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 4718 | } |
| 4719 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4720 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4721 | |
| 4722 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4723 | |
| 4724 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4725 | /** |
| 4726 | * @brief Convert a CBOR big number to a uint64_t. |
| 4727 | * |
| 4728 | * @param[in] BigNum Bytes of the big number to convert. |
| 4729 | * @param[in] uMax Maximum value allowed for the result. |
| 4730 | * @param[out] pResult Place to put the unsigned integer result. |
| 4731 | * |
| 4732 | * @returns Error code |
| 4733 | * |
| 4734 | * Many values will overflow because a big num can represent a much |
| 4735 | * larger range than uint64_t. |
| 4736 | */ |
| 4737 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4738 | QCBOR_Private_ConvertBigNumToUnsigned(const UsefulBufC BigNum, |
| 4739 | const uint64_t uMax, |
| 4740 | uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4741 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4742 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4743 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4744 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4745 | const uint8_t *pByte = BigNum.ptr; |
| 4746 | size_t uLen = BigNum.len; |
| 4747 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4748 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4749 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4750 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4751 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4752 | } |
| 4753 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4754 | *pResult = uResult; |
| 4755 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4756 | } |
| 4757 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4758 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4759 | /** |
| 4760 | * @brief Convert a CBOR postive big number to a uint64_t. |
| 4761 | * |
| 4762 | * @param[in] BigNum Bytes of the big number to convert. |
| 4763 | * @param[out] pResult Place to put the unsigned integer result. |
| 4764 | * |
| 4765 | * @returns Error code |
| 4766 | * |
| 4767 | * Many values will overflow because a big num can represent a much |
| 4768 | * larger range than uint64_t. |
| 4769 | */ |
| 4770 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4771 | QCBOR_Private_ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, |
| 4772 | uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4773 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4774 | return QCBOR_Private_ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4775 | } |
| 4776 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4777 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4778 | /** |
| 4779 | * @brief Convert a CBOR positive big number to an int64_t. |
| 4780 | * |
| 4781 | * @param[in] BigNum Bytes of the big number to convert. |
| 4782 | * @param[out] pResult Place to put the signed integer result. |
| 4783 | * |
| 4784 | * @returns Error code |
| 4785 | * |
| 4786 | * Many values will overflow because a big num can represent a much |
| 4787 | * larger range than int64_t. |
| 4788 | */ |
| 4789 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4790 | QCBOR_Private_ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, |
| 4791 | int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4792 | { |
| 4793 | uint64_t uResult; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4794 | QCBORError uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, |
| 4795 | INT64_MAX, |
| 4796 | &uResult); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4797 | if(uError) { |
| 4798 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4799 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4800 | /* Cast is safe because ConvertBigNumToUnsigned limits to INT64_MAX */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4801 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4802 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4806 | /** |
| 4807 | * @brief Convert a CBOR negative big number to an int64_t. |
| 4808 | * |
| 4809 | * @param[in] BigNum Bytes of the big number to convert. |
| 4810 | * @param[out] pnResult Place to put the signed integer result. |
| 4811 | * |
| 4812 | * @returns Error code |
| 4813 | * |
| 4814 | * Many values will overflow because a big num can represent a much |
| 4815 | * larger range than int64_t. |
| 4816 | */ |
| 4817 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4818 | QCBOR_Private_ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, |
| 4819 | int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4820 | { |
| 4821 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4822 | /* The negative integer furthest from zero for a C int64_t is |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4823 | * INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 4824 | * negative number in CBOR is computed as -n - 1 where n is the |
| 4825 | * encoded integer, where n is what is in the variable BigNum. When |
| 4826 | * converting BigNum to a uint64_t, the maximum value is thus |
| 4827 | * INT64_MAX, so that when it -n - 1 is applied to it the result |
| 4828 | * will never be further from 0 than INT64_MIN. |
| 4829 | * |
| 4830 | * -n - 1 <= INT64_MIN. |
| 4831 | * -n - 1 <= -INT64_MAX - 1 |
| 4832 | * n <= INT64_MAX. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4833 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4834 | QCBORError uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, |
| 4835 | INT64_MAX, |
| 4836 | &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4837 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4838 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4839 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4840 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4841 | /* Now apply -n - 1. The cast is safe because |
| 4842 | * ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 4843 | * is the largest positive integer that an int64_t can |
| 4844 | * represent. */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4845 | *pnResult = -(int64_t)uResult - 1; |
| 4846 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4847 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4848 | } |
| 4849 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4850 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4851 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4852 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4853 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4854 | /* |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 4855 | Convert integers and floats to an int64_t. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4856 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4857 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4858 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4859 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 4860 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4861 | |
| 4862 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 4863 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4864 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 4865 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4866 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4867 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4868 | QCBOR_Private_ConvertInt64(const QCBORItem *pItem, |
| 4869 | const uint32_t uConvertTypes, |
| 4870 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4871 | { |
| 4872 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4873 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4874 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4875 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4876 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4877 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 4878 | http://www.cplusplus.com/reference/cmath/llround/ |
| 4879 | */ |
| 4880 | // Not interested in FE_INEXACT |
| 4881 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4882 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 4883 | *pnValue = llround(pItem->val.dfnum); |
| 4884 | } else { |
| 4885 | *pnValue = lroundf(pItem->val.fnum); |
| 4886 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 4887 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 4888 | // llround() shouldn't result in divide by zero, but catch |
| 4889 | // it here in case it unexpectedly does. Don't try to |
| 4890 | // distinguish between the various exceptions because it seems |
| 4891 | // they vary by CPU, compiler and OS. |
| 4892 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4893 | } |
| 4894 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4895 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4896 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 4897 | #else |
| 4898 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 4899 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4900 | break; |
| 4901 | |
| 4902 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4903 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4904 | *pnValue = pItem->val.int64; |
| 4905 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4906 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4907 | } |
| 4908 | break; |
| 4909 | |
| 4910 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4911 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4912 | if(pItem->val.uint64 < INT64_MAX) { |
| 4913 | *pnValue = pItem->val.int64; |
| 4914 | } else { |
| 4915 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4916 | } |
| 4917 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 4918 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4919 | } |
| 4920 | break; |
| 4921 | |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame^] | 4922 | case QCBOR_TYPE_65BIT_NEG_INT: |
| 4923 | /* This type occurs if the value won't fit into int64_t |
| 4924 | * so this is always an error. */ |
| 4925 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4926 | break; |
| 4927 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4928 | default: |
| 4929 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4930 | } |
| 4931 | return QCBOR_SUCCESS; |
| 4932 | } |
| 4933 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4934 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4935 | void |
| 4936 | QCBORDecode_Private_GetInt64Convert(QCBORDecodeContext *pMe, |
| 4937 | uint32_t uConvertTypes, |
| 4938 | int64_t *pnValue, |
| 4939 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4940 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4941 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4942 | return; |
| 4943 | } |
| 4944 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4945 | QCBORItem Item; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4946 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4947 | if(uError) { |
| 4948 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4949 | return; |
| 4950 | } |
| 4951 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4952 | if(pItem) { |
| 4953 | *pItem = Item; |
| 4954 | } |
| 4955 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4956 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(&Item, |
| 4957 | uConvertTypes, |
| 4958 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4959 | } |
| 4960 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4961 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4962 | void |
| 4963 | QCBORDecode_Private_GetInt64ConvertInMapN(QCBORDecodeContext *pMe, |
| 4964 | int64_t nLabel, |
| 4965 | uint32_t uConvertTypes, |
| 4966 | int64_t *pnValue, |
| 4967 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4968 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4969 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4970 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4971 | return; |
| 4972 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4973 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4974 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
| 4975 | uConvertTypes, |
| 4976 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4977 | } |
| 4978 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4979 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4980 | void |
| 4981 | QCBORDecode_Private_GetInt64ConvertInMapSZ(QCBORDecodeContext *pMe, |
| 4982 | const char * szLabel, |
| 4983 | uint32_t uConvertTypes, |
| 4984 | int64_t *pnValue, |
| 4985 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4986 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4987 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4988 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4989 | return; |
| 4990 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4991 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4992 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
| 4993 | uConvertTypes, |
| 4994 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4995 | } |
| 4996 | |
| 4997 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 4998 | /* |
| 4999 | Convert a large variety of integer types to an int64_t. |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5000 | |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5001 | \param[in] uConvertTypes Bit mask list of conversion options. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5002 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5003 | \retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5004 | in uConvertTypes. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5005 | |
| 5006 | \retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5007 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5008 | \retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5009 | or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5010 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5011 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5012 | QCBOR_Private_Int64ConvertAll(const QCBORItem *pItem, |
| 5013 | const uint32_t uConvertTypes, |
| 5014 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5015 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5016 | switch(pItem->uDataType) { |
| 5017 | |
| 5018 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5019 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5020 | return QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5021 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5022 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5023 | } |
| 5024 | break; |
| 5025 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5026 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5027 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5028 | return QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5029 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5030 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5031 | } |
| 5032 | break; |
| 5033 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5034 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5035 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5036 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5037 | return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5038 | pItem->val.expAndMantissa.nExponent, |
| 5039 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5040 | &QCBOR_Private_Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5041 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5042 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5043 | } |
| 5044 | break; |
| 5045 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5046 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5047 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5048 | return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5049 | pItem->val.expAndMantissa.nExponent, |
| 5050 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5051 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5052 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5053 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5054 | } |
| 5055 | break; |
| 5056 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5057 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5058 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5059 | int64_t nMantissa; |
| 5060 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5061 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5062 | if(uErr) { |
| 5063 | return uErr; |
| 5064 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5065 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5066 | pItem->val.expAndMantissa.nExponent, |
| 5067 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5068 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5069 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5070 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5071 | } |
| 5072 | break; |
| 5073 | |
| 5074 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5075 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5076 | int64_t nMantissa; |
| 5077 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5078 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5079 | if(uErr) { |
| 5080 | return uErr; |
| 5081 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5082 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5083 | pItem->val.expAndMantissa.nExponent, |
| 5084 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5085 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5086 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5087 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5088 | } |
| 5089 | break; |
| 5090 | |
| 5091 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5092 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5093 | int64_t nMantissa; |
| 5094 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5095 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5096 | if(uErr) { |
| 5097 | return uErr; |
| 5098 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5099 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5100 | pItem->val.expAndMantissa.nExponent, |
| 5101 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5102 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5103 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5104 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5105 | } |
| 5106 | break; |
| 5107 | |
| 5108 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5109 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5110 | int64_t nMantissa; |
| 5111 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5112 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5113 | if(uErr) { |
| 5114 | return uErr; |
| 5115 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5116 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5117 | pItem->val.expAndMantissa.nExponent, |
| 5118 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5119 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5120 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5121 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5122 | } |
| 5123 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5124 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5125 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5126 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5127 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5128 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5129 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5130 | |
| 5131 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5132 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5133 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5134 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5135 | void |
| 5136 | QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, |
| 5137 | const uint32_t uConvertTypes, |
| 5138 | int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5139 | { |
| 5140 | QCBORItem Item; |
| 5141 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5142 | QCBORDecode_Private_GetInt64Convert(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5143 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5144 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5145 | // The above conversion succeeded |
| 5146 | return; |
| 5147 | } |
| 5148 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5149 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5150 | // 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] | 5151 | return; |
| 5152 | } |
| 5153 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5154 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5155 | uConvertTypes, |
| 5156 | pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5157 | } |
| 5158 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5159 | |
| 5160 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5161 | * Public function, see header qcbor/qcbor_decode.h file |
| 5162 | */ |
| 5163 | void |
| 5164 | QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5165 | const int64_t nLabel, |
| 5166 | const uint32_t uConvertTypes, |
| 5167 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5168 | { |
| 5169 | QCBORItem Item; |
| 5170 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5171 | QCBORDecode_Private_GetInt64ConvertInMapN(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5172 | nLabel, |
| 5173 | uConvertTypes, |
| 5174 | pnValue, |
| 5175 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5176 | |
| 5177 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5178 | // The above conversion succeeded |
| 5179 | return; |
| 5180 | } |
| 5181 | |
| 5182 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5183 | // The above conversion failed in a way that code below can't correct |
| 5184 | return; |
| 5185 | } |
| 5186 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5187 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5188 | uConvertTypes, |
| 5189 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5190 | } |
| 5191 | |
| 5192 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5193 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5194 | * Public function, see header qcbor/qcbor_decode.h file |
| 5195 | */ |
| 5196 | void |
| 5197 | QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5198 | const char *szLabel, |
| 5199 | const uint32_t uConvertTypes, |
| 5200 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5201 | { |
| 5202 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5203 | QCBORDecode_Private_GetInt64ConvertInMapSZ(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5204 | szLabel, |
| 5205 | uConvertTypes, |
| 5206 | pnValue, |
| 5207 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5208 | |
| 5209 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5210 | // The above conversion succeeded |
| 5211 | return; |
| 5212 | } |
| 5213 | |
| 5214 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5215 | // The above conversion failed in a way that code below can't correct |
| 5216 | return; |
| 5217 | } |
| 5218 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5219 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5220 | uConvertTypes, |
| 5221 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5222 | } |
| 5223 | |
| 5224 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5225 | static QCBORError |
| 5226 | QCBOR_Private_ConvertUInt64(const QCBORItem *pItem, |
| 5227 | const uint32_t uConvertTypes, |
| 5228 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5229 | { |
| 5230 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5231 | case QCBOR_TYPE_DOUBLE: |
| 5232 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5233 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5234 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5235 | // Can't use llround here because it will not convert values |
| 5236 | // greater than INT64_MAX and less than UINT64_MAX that |
| 5237 | // need to be converted so it is more complicated. |
| 5238 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 5239 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 5240 | if(isnan(pItem->val.dfnum)) { |
| 5241 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5242 | } else if(pItem->val.dfnum < 0) { |
| 5243 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5244 | } else { |
| 5245 | double dRounded = round(pItem->val.dfnum); |
| 5246 | // See discussion in DecodeDateEpoch() for |
| 5247 | // explanation of - 0x7ff |
| 5248 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 5249 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5250 | } |
| 5251 | *puValue = (uint64_t)dRounded; |
| 5252 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5253 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5254 | if(isnan(pItem->val.fnum)) { |
| 5255 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5256 | } else if(pItem->val.fnum < 0) { |
| 5257 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5258 | } else { |
| 5259 | float fRounded = roundf(pItem->val.fnum); |
| 5260 | // See discussion in DecodeDateEpoch() for |
| 5261 | // explanation of - 0x7ff |
| 5262 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 5263 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5264 | } |
| 5265 | *puValue = (uint64_t)fRounded; |
| 5266 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5267 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5268 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 5269 | // round() and roundf() shouldn't result in exceptions here, but |
| 5270 | // catch them to be robust and thorough. Don't try to |
| 5271 | // distinguish between the various exceptions because it seems |
| 5272 | // they vary by CPU, compiler and OS. |
| 5273 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5274 | } |
| 5275 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5276 | } else { |
| 5277 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5278 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5279 | #else |
| 5280 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5281 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5282 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5283 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5284 | case QCBOR_TYPE_INT64: |
| 5285 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 5286 | if(pItem->val.int64 >= 0) { |
| 5287 | *puValue = (uint64_t)pItem->val.int64; |
| 5288 | } else { |
| 5289 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5290 | } |
| 5291 | } else { |
| 5292 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5293 | } |
| 5294 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5295 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5296 | case QCBOR_TYPE_UINT64: |
| 5297 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame^] | 5298 | *puValue = pItem->val.uint64; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5299 | } else { |
| 5300 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5301 | } |
| 5302 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5303 | |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame^] | 5304 | case QCBOR_TYPE_65BIT_NEG_INT: |
| 5305 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5306 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5307 | default: |
| 5308 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5309 | } |
| 5310 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5311 | return QCBOR_SUCCESS; |
| 5312 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5313 | |
| 5314 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5315 | void |
| 5316 | QCBORDecode_Private_GetUInt64Convert(QCBORDecodeContext *pMe, |
| 5317 | const uint32_t uConvertTypes, |
| 5318 | uint64_t *puValue, |
| 5319 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5320 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5321 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5322 | return; |
| 5323 | } |
| 5324 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5325 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5326 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5327 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5328 | if(uError) { |
| 5329 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5330 | return; |
| 5331 | } |
| 5332 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 5333 | if(pItem) { |
| 5334 | *pItem = Item; |
| 5335 | } |
| 5336 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5337 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(&Item, |
| 5338 | uConvertTypes, |
| 5339 | puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5340 | } |
| 5341 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5342 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5343 | void |
| 5344 | QCBORDecode_Private_GetUInt64ConvertInMapN(QCBORDecodeContext *pMe, |
| 5345 | const int64_t nLabel, |
| 5346 | const uint32_t uConvertTypes, |
| 5347 | uint64_t *puValue, |
| 5348 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5349 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5350 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5351 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5352 | return; |
| 5353 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5354 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5355 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem, |
| 5356 | uConvertTypes, |
| 5357 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5358 | } |
| 5359 | |
| 5360 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5361 | void |
| 5362 | QCBORDecode_Private_GetUInt64ConvertInMapSZ(QCBORDecodeContext *pMe, |
| 5363 | const char *szLabel, |
| 5364 | const uint32_t uConvertTypes, |
| 5365 | uint64_t *puValue, |
| 5366 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5367 | { |
| 5368 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5369 | return; |
| 5370 | } |
| 5371 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5372 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5373 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5374 | return; |
| 5375 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5376 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5377 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem, |
| 5378 | uConvertTypes, |
| 5379 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5380 | } |
| 5381 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5382 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5383 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5384 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5385 | QCBOR_Private_UInt64ConvertAll(const QCBORItem *pItem, |
| 5386 | const uint32_t uConvertTypes, |
| 5387 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5388 | { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 5389 | switch(pItem->uDataType) { /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5390 | |
| 5391 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5392 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5393 | return QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5394 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5395 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5396 | } |
| 5397 | break; |
| 5398 | |
| 5399 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5400 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5401 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5402 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5403 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5404 | } |
| 5405 | break; |
| 5406 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5407 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5408 | |
| 5409 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5410 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5411 | return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5412 | pItem->val.expAndMantissa.nExponent, |
| 5413 | puValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5414 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5415 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5416 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5417 | } |
| 5418 | break; |
| 5419 | |
| 5420 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5421 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5422 | return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5423 | pItem->val.expAndMantissa.nExponent, |
| 5424 | puValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5425 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5426 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5427 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5428 | } |
| 5429 | break; |
| 5430 | |
| 5431 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5432 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5433 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5434 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5435 | uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5436 | if(uErr != QCBOR_SUCCESS) { |
| 5437 | return uErr; |
| 5438 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5439 | return QCBOR_Private_ExponentitateUU(uMantissa, |
| 5440 | pItem->val.expAndMantissa.nExponent, |
| 5441 | puValue, |
| 5442 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5443 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5444 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5445 | } |
| 5446 | break; |
| 5447 | |
| 5448 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5449 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5450 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5451 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5452 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5453 | } |
| 5454 | break; |
| 5455 | |
| 5456 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5457 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5458 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5459 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5460 | uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, |
| 5461 | &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5462 | if(uErr != QCBOR_SUCCESS) { |
| 5463 | return uErr; |
| 5464 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5465 | return QCBOR_Private_ExponentitateUU(uMantissa, |
| 5466 | pItem->val.expAndMantissa.nExponent, |
| 5467 | puValue, |
| 5468 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5469 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5470 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5471 | } |
| 5472 | break; |
| 5473 | |
| 5474 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5475 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5476 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5477 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5478 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5479 | } |
| 5480 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5481 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5482 | default: |
| 5483 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5484 | } |
| 5485 | } |
| 5486 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5487 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5488 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5489 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5490 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5491 | void |
| 5492 | QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, |
| 5493 | const uint32_t uConvertTypes, |
| 5494 | uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5495 | { |
| 5496 | QCBORItem Item; |
| 5497 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5498 | QCBORDecode_Private_GetUInt64Convert(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5499 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5500 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5501 | // The above conversion succeeded |
| 5502 | return; |
| 5503 | } |
| 5504 | |
| 5505 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5506 | // 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] | 5507 | return; |
| 5508 | } |
| 5509 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5510 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 5511 | uConvertTypes, |
| 5512 | puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5513 | } |
| 5514 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5515 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5516 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5517 | * Public function, see header qcbor/qcbor_decode.h file |
| 5518 | */ |
| 5519 | void |
| 5520 | QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5521 | const int64_t nLabel, |
| 5522 | const uint32_t uConvertTypes, |
| 5523 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5524 | { |
| 5525 | QCBORItem Item; |
| 5526 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5527 | QCBORDecode_Private_GetUInt64ConvertInMapN(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5528 | nLabel, |
| 5529 | uConvertTypes, |
| 5530 | puValue, |
| 5531 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5532 | |
| 5533 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5534 | // The above conversion succeeded |
| 5535 | return; |
| 5536 | } |
| 5537 | |
| 5538 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5539 | // The above conversion failed in a way that code below can't correct |
| 5540 | return; |
| 5541 | } |
| 5542 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5543 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 5544 | uConvertTypes, |
| 5545 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5546 | } |
| 5547 | |
| 5548 | |
| 5549 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5550 | * Public function, see header qcbor/qcbor_decode.h file |
| 5551 | */ |
| 5552 | void |
| 5553 | QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5554 | const char *szLabel, |
| 5555 | const uint32_t uConvertTypes, |
| 5556 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5557 | { |
| 5558 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5559 | QCBORDecode_Private_GetUInt64ConvertInMapSZ(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5560 | szLabel, |
| 5561 | uConvertTypes, |
| 5562 | puValue, |
| 5563 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5564 | |
| 5565 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5566 | // The above conversion succeeded |
| 5567 | return; |
| 5568 | } |
| 5569 | |
| 5570 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5571 | // The above conversion failed in a way that code below can't correct |
| 5572 | return; |
| 5573 | } |
| 5574 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5575 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 5576 | uConvertTypes, |
| 5577 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5581 | |
| 5582 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5583 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5584 | static QCBORError |
| 5585 | QCBOR_Private_ConvertDouble(const QCBORItem *pItem, |
| 5586 | const uint32_t uConvertTypes, |
| 5587 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5588 | { |
| 5589 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5590 | case QCBOR_TYPE_FLOAT: |
| 5591 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5592 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 5593 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5594 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5595 | *pdValue = (double)pItem->val.fnum; |
| 5596 | } else { |
| 5597 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5598 | } |
| 5599 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5600 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5601 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5602 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5603 | break; |
| 5604 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5605 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5606 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 5607 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5608 | *pdValue = pItem->val.dfnum; |
| 5609 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5610 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5611 | } |
| 5612 | } |
| 5613 | break; |
| 5614 | |
| 5615 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5616 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5617 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5618 | // A simple cast seems to do the job with no worry of exceptions. |
| 5619 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5620 | *pdValue = (double)pItem->val.int64; |
| 5621 | |
| 5622 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5623 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5624 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5625 | #else |
| 5626 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5627 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5628 | break; |
| 5629 | |
| 5630 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5631 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5632 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5633 | // A simple cast seems to do the job with no worry of exceptions. |
| 5634 | // There will be precision loss for some values. |
| 5635 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5636 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5637 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5638 | } |
| 5639 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5640 | #else |
| 5641 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5642 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5643 | |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame^] | 5644 | case QCBOR_TYPE_65BIT_NEG_INT: |
| 5645 | *pdValue = -(double)pItem->val.uint64 - 1; |
| 5646 | break; |
| 5647 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5648 | default: |
| 5649 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5650 | } |
| 5651 | |
| 5652 | return QCBOR_SUCCESS; |
| 5653 | } |
| 5654 | |
| 5655 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5656 | void |
| 5657 | QCBORDecode_Private_GetDoubleConvert(QCBORDecodeContext *pMe, |
| 5658 | const uint32_t uConvertTypes, |
| 5659 | double *pdValue, |
| 5660 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5661 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5662 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5663 | return; |
| 5664 | } |
| 5665 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5666 | QCBORItem Item; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5667 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5668 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5669 | if(uError) { |
| 5670 | pMe->uLastError = (uint8_t)uError; |
| 5671 | return; |
| 5672 | } |
| 5673 | |
| 5674 | if(pItem) { |
| 5675 | *pItem = Item; |
| 5676 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5677 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5678 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(&Item, |
| 5679 | uConvertTypes, |
| 5680 | pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5681 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5682 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5683 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5684 | void |
| 5685 | QCBORDecode_Private_GetDoubleConvertInMapN(QCBORDecodeContext *pMe, |
| 5686 | const int64_t nLabel, |
| 5687 | const uint32_t uConvertTypes, |
| 5688 | double *pdValue, |
| 5689 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5690 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5691 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5692 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5693 | return; |
| 5694 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5695 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5696 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
| 5697 | uConvertTypes, |
| 5698 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5699 | } |
| 5700 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5701 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5702 | void |
| 5703 | QCBORDecode_Private_GetDoubleConvertInMapSZ(QCBORDecodeContext *pMe, |
| 5704 | const char *szLabel, |
| 5705 | const uint32_t uConvertTypes, |
| 5706 | double *pdValue, |
| 5707 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5708 | { |
| 5709 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5710 | return; |
| 5711 | } |
| 5712 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5713 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5714 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5715 | return; |
| 5716 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5717 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5718 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
| 5719 | uConvertTypes, |
| 5720 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5721 | } |
| 5722 | |
| 5723 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5724 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5725 | static double |
| 5726 | QCBOR_Private_ConvertBigNumToDouble(const UsefulBufC BigNum) |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5727 | { |
| 5728 | double dResult; |
| 5729 | |
| 5730 | dResult = 0.0; |
| 5731 | const uint8_t *pByte = BigNum.ptr; |
| 5732 | size_t uLen = BigNum.len; |
| 5733 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 11fd78b | 2020-09-01 22:13:27 -0700 | [diff] [blame] | 5734 | is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5735 | while(uLen--) { |
| 5736 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 5737 | } |
| 5738 | |
| 5739 | return dResult; |
| 5740 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5741 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5742 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5743 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5744 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5745 | QCBOR_Private_DoubleConvertAll(const QCBORItem *pItem, |
| 5746 | const uint32_t uConvertTypes, |
| 5747 | double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5748 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5749 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5750 | /* |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 5751 | * What Every Computer Scientist Should Know About Floating-Point Arithmetic |
| 5752 | * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 5753 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5754 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5755 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5756 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5757 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5758 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 5759 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5760 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 5761 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 5762 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5763 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5764 | } |
| 5765 | break; |
| 5766 | |
| 5767 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5768 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 5769 | // Underflow gives 0, overflow gives infinity |
| 5770 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 5771 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5772 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5773 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5774 | } |
| 5775 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5776 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5777 | |
| 5778 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5779 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5780 | *pdValue = QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5781 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5782 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5783 | } |
| 5784 | break; |
| 5785 | |
| 5786 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5787 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5788 | *pdValue = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5789 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5790 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5791 | } |
| 5792 | break; |
| 5793 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5794 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5795 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5796 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5797 | double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5798 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5799 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5800 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5801 | } |
| 5802 | break; |
| 5803 | |
| 5804 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5805 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5806 | double dMantissa = -QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5807 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 5808 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5809 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5810 | } |
| 5811 | break; |
| 5812 | |
| 5813 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5814 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5815 | double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5816 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5817 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5818 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5819 | } |
| 5820 | break; |
| 5821 | |
| 5822 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5823 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5824 | double dMantissa = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5825 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 5826 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5827 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5828 | } |
| 5829 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5830 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 5831 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5832 | default: |
| 5833 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5834 | } |
| 5835 | |
| 5836 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5837 | |
| 5838 | #else |
| 5839 | (void)pItem; |
| 5840 | (void)uConvertTypes; |
| 5841 | (void)pdValue; |
| 5842 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5843 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 5844 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5845 | } |
| 5846 | |
| 5847 | |
| 5848 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5849 | *Public function, see header qcbor/qcbor_decode.h file |
| 5850 | */ |
| 5851 | void |
| 5852 | QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 5853 | const uint32_t uConvertTypes, |
| 5854 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5855 | { |
| 5856 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5857 | QCBORItem Item; |
| 5858 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5859 | QCBORDecode_Private_GetDoubleConvert(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5860 | |
| 5861 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5862 | // The above conversion succeeded |
| 5863 | return; |
| 5864 | } |
| 5865 | |
| 5866 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5867 | // The above conversion failed in a way that code below can't correct |
| 5868 | return; |
| 5869 | } |
| 5870 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5871 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 5872 | uConvertTypes, |
| 5873 | pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5874 | } |
| 5875 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5876 | |
| 5877 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5878 | * Public function, see header qcbor/qcbor_decode.h file |
| 5879 | */ |
| 5880 | void |
| 5881 | QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5882 | const int64_t nLabel, |
| 5883 | const uint32_t uConvertTypes, |
| 5884 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5885 | { |
| 5886 | QCBORItem Item; |
| 5887 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5888 | QCBORDecode_Private_GetDoubleConvertInMapN(pMe, |
| 5889 | nLabel, |
| 5890 | uConvertTypes, |
| 5891 | pdValue, |
| 5892 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5893 | |
| 5894 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5895 | // The above conversion succeeded |
| 5896 | return; |
| 5897 | } |
| 5898 | |
| 5899 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5900 | // The above conversion failed in a way that code below can't correct |
| 5901 | return; |
| 5902 | } |
| 5903 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5904 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 5905 | uConvertTypes, |
| 5906 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5907 | } |
| 5908 | |
| 5909 | |
| 5910 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5911 | * Public function, see header qcbor/qcbor_decode.h file |
| 5912 | */ |
| 5913 | void |
| 5914 | QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5915 | const char *szLabel, |
| 5916 | const uint32_t uConvertTypes, |
| 5917 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5918 | { |
| 5919 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5920 | QCBORDecode_Private_GetDoubleConvertInMapSZ(pMe, |
| 5921 | szLabel, |
| 5922 | uConvertTypes, |
| 5923 | pdValue, |
| 5924 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5925 | |
| 5926 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5927 | // The above conversion succeeded |
| 5928 | return; |
| 5929 | } |
| 5930 | |
| 5931 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5932 | // The above conversion failed in a way that code below can't correct |
| 5933 | return; |
| 5934 | } |
| 5935 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5936 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 5937 | uConvertTypes, |
| 5938 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5939 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5940 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5941 | |
| 5942 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5943 | |
| 5944 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5945 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5946 | static UsefulBufC |
| 5947 | QCBOR_Private_ConvertIntToBigNum(uint64_t uInt, const UsefulBuf Buffer) |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5948 | { |
| 5949 | while((uInt & 0xff00000000000000UL) == 0) { |
| 5950 | uInt = uInt << 8; |
| 5951 | }; |
| 5952 | |
| 5953 | UsefulOutBuf UOB; |
| 5954 | |
| 5955 | UsefulOutBuf_Init(&UOB, Buffer); |
| 5956 | |
| 5957 | while(uInt) { |
| 5958 | const uint64_t xx = uInt & 0xff00000000000000UL; |
| 5959 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 5960 | uInt = uInt << 8; |
| 5961 | (void)xx; |
| 5962 | } |
| 5963 | |
| 5964 | return UsefulOutBuf_OutUBuf(&UOB); |
| 5965 | } |
| 5966 | |
| 5967 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5968 | /** |
| 5969 | * @brief Check and/or complete mantissa and exponent item. |
| 5970 | * |
| 5971 | * @param[in] pMe The decoder context |
| 5972 | * @param[in] TagSpec Expected type(s) |
| 5973 | * @param[in,out] pItem See below |
| 5974 | * |
| 5975 | * This is for decimal fractions and big floats, both of which are a |
| 5976 | * mantissa and exponent. |
| 5977 | * |
| 5978 | * The input item is either a fully decoded decimal faction or big |
| 5979 | * float, or a just the decoded first item of a decimal fraction or |
| 5980 | * big float. |
| 5981 | * |
| 5982 | * On output, the item is always a fully decoded decimal fraction or |
| 5983 | * big float. |
| 5984 | * |
| 5985 | * This errors out if the input type does not meet the TagSpec. |
| 5986 | */ |
| 5987 | // TODO: document and see tests for the bug that was fixed by this rewrite |
| 5988 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5989 | QCBOR_Private_ExpMantissaTypeHandler(QCBORDecodeContext *pMe, |
| 5990 | const QCBOR_Private_TagSpec TagSpec, |
| 5991 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5992 | { |
| 5993 | QCBORError uErr; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 5994 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5995 | /* pItem could either be an auto-decoded mantissa and exponent or |
| 5996 | * the opening array of an undecoded mantissa and exponent. This |
| 5997 | * check will succeed on either, but doesn't say which it was. |
| 5998 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5999 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6000 | if(uErr != QCBOR_SUCCESS) { |
| 6001 | goto Done; |
| 6002 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6003 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6004 | if(pItem->uDataType == QCBOR_TYPE_ARRAY) { |
| 6005 | /* The item is an array, which means is is an undecoded mantissa |
| 6006 | * and exponent. This call consumes the items in the array and |
| 6007 | * results in a decoded mantissa and exponent in pItem. This is |
| 6008 | * the case where there was no tag. |
| 6009 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6010 | uErr = QCBORDecode_Private_ExpMantissa(pMe, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6011 | if(uErr != QCBOR_SUCCESS) { |
| 6012 | goto Done; |
| 6013 | } |
| 6014 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6015 | /* The above decode didn't determine whether it is a decimal |
| 6016 | * fraction or big num. Which of these two depends on what the |
| 6017 | * caller wants it decoded as since there is no tag, so fish the |
| 6018 | * type out of the TagSpec. */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6019 | pItem->uDataType = QCBOR_Private_ExpMantissaDataType(TagSpec.uTaggedTypes[0], pItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6020 | |
| 6021 | /* No need to check the type again. All that we need to know was |
| 6022 | * that it decoded correctly as a mantissa and exponent. The |
| 6023 | * QCBOR type is set out by what was requested. |
| 6024 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6025 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6026 | |
| 6027 | /* If the item was not an array and the check passed, then |
| 6028 | * it is a fully decoded big float or decimal fraction and |
| 6029 | * matches what is requested. |
| 6030 | */ |
| 6031 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6032 | Done: |
| 6033 | return uErr; |
| 6034 | } |
| 6035 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6036 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6037 | /* Some notes from the work to disable tags. |
| 6038 | * |
| 6039 | * The API for big floats and decimal fractions seems good. |
| 6040 | * If there's any issue with it it's that the code size to |
| 6041 | * implement is a bit large because of the conversion |
| 6042 | * to/from int and bignum that is required. There is no API |
| 6043 | * that doesn't do the conversion so dead stripping will never |
| 6044 | * leave that code out. |
| 6045 | * |
| 6046 | * The implementation itself seems correct, but not as clean |
| 6047 | * and neat as it could be. It could probably be smaller too. |
| 6048 | * |
| 6049 | * The implementation has three main parts / functions |
| 6050 | * - The decoding of the array of two |
| 6051 | * - All the tag and type checking for the various API functions |
| 6052 | * - Conversion to/from bignum and int |
| 6053 | * |
| 6054 | * The type checking seems like it wastes the most code for |
| 6055 | * what it needs to do. |
| 6056 | * |
| 6057 | * The inlining for the conversion is probably making the |
| 6058 | * overall code base larger. |
| 6059 | * |
| 6060 | * The tests cases could be organized a lot better and be |
| 6061 | * more thorough. |
| 6062 | * |
| 6063 | * Seems also like there could be more common code in the |
| 6064 | * first tier part of the public API. Some functions only |
| 6065 | * vary by a TagSpec. |
| 6066 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6067 | static void |
| 6068 | QCBOR_Private_ProcessExpMantissa(QCBORDecodeContext *pMe, |
| 6069 | const QCBOR_Private_TagSpec TagSpec, |
| 6070 | QCBORItem *pItem, |
| 6071 | int64_t *pnMantissa, |
| 6072 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6073 | { |
| 6074 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6075 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6076 | uErr = QCBOR_Private_ExpMantissaTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6077 | if(uErr != QCBOR_SUCCESS) { |
| 6078 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6079 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6080 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6081 | switch (pItem->uDataType) { |
| 6082 | |
| 6083 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 6084 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6085 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6086 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6087 | break; |
| 6088 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6089 | #ifndef QCBOR_DISABLE_TAGS |
| 6090 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6091 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 6092 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 6093 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6094 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6095 | break; |
| 6096 | |
| 6097 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 6098 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 6099 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6100 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6101 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6102 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6103 | |
| 6104 | default: |
| 6105 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 6106 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6107 | |
| 6108 | Done: |
| 6109 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6110 | } |
| 6111 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6112 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6113 | static void |
| 6114 | QCBOR_Private_ProcessExpMantissaBig(QCBORDecodeContext *pMe, |
| 6115 | const QCBOR_Private_TagSpec TagSpec, |
| 6116 | QCBORItem *pItem, |
| 6117 | const UsefulBuf BufferForMantissa, |
| 6118 | UsefulBufC *pMantissa, |
| 6119 | bool *pbIsNegative, |
| 6120 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6121 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6122 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6123 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6124 | uErr = QCBOR_Private_ExpMantissaTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6125 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6126 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6127 | } |
| 6128 | |
| 6129 | uint64_t uMantissa; |
| 6130 | |
| 6131 | switch (pItem->uDataType) { |
| 6132 | |
| 6133 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6134 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6135 | /* See comments in ExponentiateNN() on handling INT64_MIN */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6136 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 6137 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 6138 | *pbIsNegative = false; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6139 | } else if(pItem->val.expAndMantissa.Mantissa.nInt != INT64_MIN) { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6140 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 6141 | *pbIsNegative = true; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6142 | } else { |
| 6143 | uMantissa = (uint64_t)INT64_MAX+1; |
| 6144 | *pbIsNegative = true; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6145 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6146 | *pMantissa = QCBOR_Private_ConvertIntToBigNum(uMantissa, |
| 6147 | BufferForMantissa); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6148 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6149 | break; |
| 6150 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6151 | #ifndef QCBOR_DISABLE_TAGS |
| 6152 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6153 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6154 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6155 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6156 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 6157 | *pbIsNegative = false; |
| 6158 | break; |
| 6159 | |
| 6160 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6161 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6162 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6163 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 6164 | *pbIsNegative = true; |
| 6165 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6166 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6167 | |
| 6168 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6169 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6170 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6171 | |
| 6172 | Done: |
| 6173 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6174 | } |
| 6175 | |
| 6176 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6177 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6178 | * Public function, see header qcbor/qcbor_decode.h file |
| 6179 | */ |
| 6180 | void |
| 6181 | QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 6182 | const uint8_t uTagRequirement, |
| 6183 | int64_t *pnMantissa, |
| 6184 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6185 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6186 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6187 | return; |
| 6188 | } |
| 6189 | |
| 6190 | QCBORItem Item; |
| 6191 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6192 | if(uError) { |
| 6193 | pMe->uLastError = (uint8_t)uError; |
| 6194 | return; |
| 6195 | } |
| 6196 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6197 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6198 | { |
| 6199 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6200 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6201 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6202 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6203 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6204 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6205 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6206 | TagSpec, |
| 6207 | &Item, |
| 6208 | pnMantissa, |
| 6209 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6210 | } |
| 6211 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6212 | |
| 6213 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6214 | * Public function, see header qcbor/qcbor_decode.h file |
| 6215 | */ |
| 6216 | void |
| 6217 | QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
| 6218 | const int64_t nLabel, |
| 6219 | const uint8_t uTagRequirement, |
| 6220 | int64_t *pnMantissa, |
| 6221 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6222 | { |
| 6223 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6224 | return; |
| 6225 | } |
| 6226 | |
| 6227 | QCBORItem Item; |
| 6228 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6229 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6230 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6231 | { |
| 6232 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6233 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6234 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6235 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6236 | }; |
| 6237 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6238 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6239 | TagSpec, |
| 6240 | &Item, |
| 6241 | pnMantissa, |
| 6242 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6243 | } |
| 6244 | |
| 6245 | |
| 6246 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6247 | * Public function, see header qcbor/qcbor_decode.h file |
| 6248 | */ |
| 6249 | void |
| 6250 | QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
| 6251 | const char *szLabel, |
| 6252 | const uint8_t uTagRequirement, |
| 6253 | int64_t *pnMantissa, |
| 6254 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6255 | { |
| 6256 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6257 | return; |
| 6258 | } |
| 6259 | |
| 6260 | QCBORItem Item; |
| 6261 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6262 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6263 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6264 | { |
| 6265 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6266 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6267 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6268 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6269 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6270 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6271 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6272 | TagSpec, |
| 6273 | &Item, |
| 6274 | pnMantissa, |
| 6275 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6276 | } |
| 6277 | |
| 6278 | |
| 6279 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6280 | * Public function, see header qcbor/qcbor_decode.h file |
| 6281 | */ |
| 6282 | void |
| 6283 | QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 6284 | const uint8_t uTagRequirement, |
| 6285 | const UsefulBuf MantissaBuffer, |
| 6286 | UsefulBufC *pMantissa, |
| 6287 | bool *pbMantissaIsNegative, |
| 6288 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6289 | { |
| 6290 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6291 | return; |
| 6292 | } |
| 6293 | |
| 6294 | QCBORItem Item; |
| 6295 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6296 | if(uError) { |
| 6297 | pMe->uLastError = (uint8_t)uError; |
| 6298 | return; |
| 6299 | } |
| 6300 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6301 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6302 | { |
| 6303 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6304 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6305 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6306 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6307 | }; |
| 6308 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6309 | QCBOR_Private_ProcessExpMantissaBig(pMe, |
| 6310 | TagSpec, |
| 6311 | &Item, |
| 6312 | MantissaBuffer, |
| 6313 | pMantissa, |
| 6314 | pbMantissaIsNegative, |
| 6315 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6316 | } |
| 6317 | |
| 6318 | |
| 6319 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6320 | * Public function, see header qcbor/qcbor_decode.h file |
| 6321 | */ |
| 6322 | void |
| 6323 | QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
| 6324 | const int64_t nLabel, |
| 6325 | const uint8_t uTagRequirement, |
| 6326 | const UsefulBuf BufferForMantissa, |
| 6327 | UsefulBufC *pMantissa, |
| 6328 | bool *pbIsNegative, |
| 6329 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6330 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6331 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6332 | return; |
| 6333 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6334 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6335 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6336 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6337 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6338 | return; |
| 6339 | } |
| 6340 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6341 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6342 | { |
| 6343 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6344 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6345 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6346 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6347 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6348 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6349 | QCBOR_Private_ProcessExpMantissaBig(pMe, |
| 6350 | TagSpec, |
| 6351 | &Item, |
| 6352 | BufferForMantissa, |
| 6353 | pMantissa, |
| 6354 | pbIsNegative, |
| 6355 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6356 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6357 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6358 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6359 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6360 | * Public function, see header qcbor/qcbor_decode.h file |
| 6361 | */ |
| 6362 | void |
| 6363 | QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
| 6364 | const char *szLabel, |
| 6365 | const uint8_t uTagRequirement, |
| 6366 | const UsefulBuf BufferForMantissa, |
| 6367 | UsefulBufC *pMantissa, |
| 6368 | bool *pbIsNegative, |
| 6369 | int64_t *pnExponent) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6370 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6371 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6372 | return; |
| 6373 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6374 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6375 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6376 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6377 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6378 | return; |
| 6379 | } |
| 6380 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6381 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6382 | { |
| 6383 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6384 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6385 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6386 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6387 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6388 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6389 | QCBOR_Private_ProcessExpMantissaBig(pMe, |
| 6390 | TagSpec, |
| 6391 | &Item, |
| 6392 | BufferForMantissa, |
| 6393 | pMantissa, |
| 6394 | pbIsNegative, |
| 6395 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6396 | } |
| 6397 | |
| 6398 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6399 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6400 | * Public function, see header qcbor/qcbor_decode.h file |
| 6401 | */ |
| 6402 | void |
| 6403 | QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 6404 | const uint8_t uTagRequirement, |
| 6405 | int64_t *pnMantissa, |
| 6406 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6407 | { |
| 6408 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6409 | return; |
| 6410 | } |
| 6411 | |
| 6412 | QCBORItem Item; |
| 6413 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6414 | if(uError) { |
| 6415 | pMe->uLastError = (uint8_t)uError; |
| 6416 | return; |
| 6417 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6418 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6419 | { |
| 6420 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6421 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6422 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6423 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6424 | }; |
| 6425 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6426 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6427 | TagSpec, |
| 6428 | &Item, |
| 6429 | pnMantissa, |
| 6430 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6431 | } |
| 6432 | |
| 6433 | |
| 6434 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6435 | * Public function, see header qcbor/qcbor_decode.h file |
| 6436 | */ |
| 6437 | void |
| 6438 | QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
| 6439 | const int64_t nLabel, |
| 6440 | const uint8_t uTagRequirement, |
| 6441 | int64_t *pnMantissa, |
| 6442 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6443 | { |
| 6444 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6445 | return; |
| 6446 | } |
| 6447 | |
| 6448 | QCBORItem Item; |
| 6449 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6450 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6451 | return; |
| 6452 | } |
| 6453 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6454 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6455 | { |
| 6456 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6457 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6458 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6459 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6460 | }; |
| 6461 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6462 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6463 | TagSpec, |
| 6464 | &Item, |
| 6465 | pnMantissa, |
| 6466 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6467 | } |
| 6468 | |
| 6469 | |
| 6470 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6471 | * Public function, see header qcbor/qcbor_decode.h file |
| 6472 | */ |
| 6473 | void |
| 6474 | QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
| 6475 | const char *szLabel, |
| 6476 | const uint8_t uTagRequirement, |
| 6477 | int64_t *pnMantissa, |
| 6478 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6479 | { |
| 6480 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6481 | return; |
| 6482 | } |
| 6483 | |
| 6484 | QCBORItem Item; |
| 6485 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6486 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6487 | return; |
| 6488 | } |
| 6489 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6490 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6491 | { |
| 6492 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6493 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6494 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6495 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6496 | }; |
| 6497 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6498 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6499 | TagSpec, |
| 6500 | &Item, |
| 6501 | pnMantissa, |
| 6502 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6503 | } |
| 6504 | |
| 6505 | |
| 6506 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6507 | * Public function, see header qcbor/qcbor_decode.h file |
| 6508 | */ |
| 6509 | void |
| 6510 | QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 6511 | const uint8_t uTagRequirement, |
| 6512 | const UsefulBuf MantissaBuffer, |
| 6513 | UsefulBufC *pMantissa, |
| 6514 | bool *pbMantissaIsNegative, |
| 6515 | int64_t *pnExponent) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6516 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6517 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6518 | return; |
| 6519 | } |
| 6520 | |
| 6521 | QCBORItem Item; |
| 6522 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6523 | if(uError) { |
| 6524 | pMe->uLastError = (uint8_t)uError; |
| 6525 | return; |
| 6526 | } |
| 6527 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6528 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6529 | { |
| 6530 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6531 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6532 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6533 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6534 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6535 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6536 | QCBOR_Private_ProcessExpMantissaBig(pMe, |
| 6537 | TagSpec, |
| 6538 | &Item, |
| 6539 | MantissaBuffer, |
| 6540 | pMantissa, |
| 6541 | pbMantissaIsNegative, |
| 6542 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6543 | } |
| 6544 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6545 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6546 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6547 | * Public function, see header qcbor/qcbor_decode.h file |
| 6548 | */ |
| 6549 | void |
| 6550 | QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
| 6551 | const int64_t nLabel, |
| 6552 | const uint8_t uTagRequirement, |
| 6553 | const UsefulBuf BufferForMantissa, |
| 6554 | UsefulBufC *pMantissa, |
| 6555 | bool *pbIsNegative, |
| 6556 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6557 | { |
| 6558 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6559 | return; |
| 6560 | } |
| 6561 | |
| 6562 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6563 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6564 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6565 | return; |
| 6566 | } |
| 6567 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6568 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6569 | { |
| 6570 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6571 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6572 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6573 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6574 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6575 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6576 | QCBOR_Private_ProcessExpMantissaBig(pMe, |
| 6577 | TagSpec, |
| 6578 | &Item, |
| 6579 | BufferForMantissa, |
| 6580 | pMantissa, |
| 6581 | pbIsNegative, |
| 6582 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6583 | } |
| 6584 | |
| 6585 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6586 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6587 | * Public function, see header qcbor/qcbor_decode.h file |
| 6588 | */ |
| 6589 | void |
| 6590 | QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
| 6591 | const char *szLabel, |
| 6592 | const uint8_t uTagRequirement, |
| 6593 | const UsefulBuf BufferForMantissa, |
| 6594 | UsefulBufC *pMantissa, |
| 6595 | bool *pbIsNegative, |
| 6596 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6597 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6598 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6599 | return; |
| 6600 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6601 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6602 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6603 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6604 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6605 | return; |
| 6606 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6607 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6608 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6609 | { |
| 6610 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6611 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6612 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6613 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6614 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6615 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6616 | QCBOR_Private_ProcessExpMantissaBig(pMe, |
| 6617 | TagSpec, |
| 6618 | &Item, |
| 6619 | BufferForMantissa, |
| 6620 | pMantissa, |
| 6621 | pbIsNegative, |
| 6622 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6623 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6624 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6625 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |