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 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 768 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved features |
| 769 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 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 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 846 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 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 | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 887 | /* C can't represent a negative integer in this range so it |
| 888 | * is an error. |
| 889 | */ |
| 890 | uReturn = QCBOR_ERR_INT_OVERFLOW; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 891 | } |
| 892 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 893 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 894 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 897 | |
| 898 | /* Make sure #define value line up as DecodeSimple counts on this. */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 899 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 900 | #error QCBOR_TYPE_FALSE macro value wrong |
| 901 | #endif |
| 902 | |
| 903 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 904 | #error QCBOR_TYPE_TRUE macro value wrong |
| 905 | #endif |
| 906 | |
| 907 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 908 | #error QCBOR_TYPE_NULL macro value wrong |
| 909 | #endif |
| 910 | |
| 911 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 912 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 913 | #endif |
| 914 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 915 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 916 | #error QCBOR_TYPE_BREAK macro value wrong |
| 917 | #endif |
| 918 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 919 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 920 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 921 | #endif |
| 922 | |
| 923 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 924 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 925 | #endif |
| 926 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 927 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 928 | /** |
| 929 | * @brief Decode major type 7 -- true, false, floating-point, break... |
| 930 | * |
| 931 | * @param[in] nAdditionalInfo The lower five bits from the initial byte. |
| 932 | * @param[in] uArgument The argument from the head. |
| 933 | * @param[out] pDecodedItem The filled in decoded item. |
| 934 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 935 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 936 | * of half-precision disabled |
| 937 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all float |
| 938 | * decode is disabled. |
| 939 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of simple |
| 940 | * type in input. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 941 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 942 | static QCBORError |
| 943 | QCBOR_Private_DecodeType7(const int nAdditionalInfo, |
| 944 | const uint64_t uArgument, |
| 945 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 946 | { |
| 947 | QCBORError uReturn = QCBOR_SUCCESS; |
| 948 | |
| 949 | /* uAdditionalInfo is 5 bits from the initial byte. Compile time |
| 950 | * checks above make sure uAdditionalInfo values line up with |
| 951 | * uDataType values. DecodeHead() never returns an AdditionalInfo |
| 952 | * > 0x1f so cast is safe. |
| 953 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 954 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 955 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 956 | switch(nAdditionalInfo) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 957 | /* No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they |
| 958 | * are caught before this is called. |
| 959 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 960 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 961 | case HALF_PREC_FLOAT: /* 25 */ |
Laurence Lundblade | b275cdc | 2020-07-12 12:34:38 -0700 | [diff] [blame] | 962 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 963 | /* Half-precision is returned as a double. The cast to |
| 964 | * uint16_t is safe because the encoded value was 16 bits. It |
| 965 | * was widened to 64 bits to be passed in here. |
| 966 | */ |
| 967 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 968 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 969 | #endif /* QCBOR_DISABLE_PREFERRED_FLOAT */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 970 | uReturn = FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 971 | break; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 972 | case SINGLE_PREC_FLOAT: /* 26 */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 973 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 974 | /* Single precision is normally returned as a double since |
| 975 | * double is widely supported, there is no loss of precision, |
| 976 | * it makes it easy for the caller in most cases and it can |
| 977 | * be converted back to single with no loss of precision |
| 978 | * |
| 979 | * The cast to uint32_t is safe because the encoded value was |
| 980 | * 32 bits. It was widened to 64 bits to be passed in here. |
| 981 | */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 982 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 983 | const float f = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uArgument); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 984 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 985 | /* In the normal case, use HW to convert float to |
| 986 | * double. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 987 | pDecodedItem->val.dfnum = (double)f; |
| 988 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 989 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 990 | /* Use of float HW is disabled, return as a float. */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 991 | pDecodedItem->val.fnum = f; |
| 992 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 993 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 994 | /* IEEE754_FloatToDouble() could be used here to return as |
| 995 | * a double, but it adds object code and most likely |
| 996 | * anyone disabling FLOAT HW use doesn't care about floats |
| 997 | * and wants to save object code. |
| 998 | */ |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 999 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 1000 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1001 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 1002 | uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 1003 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 1004 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1005 | case DOUBLE_PREC_FLOAT: /* 27 */ |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1006 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1007 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uArgument); |
Laurence Lundblade | 67bd551 | 2018-11-02 21:44:06 +0700 | [diff] [blame] | 1008 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 1009 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
| 1010 | uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS); |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 1011 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1012 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1013 | case CBOR_SIMPLEV_FALSE: /* 20 */ |
| 1014 | case CBOR_SIMPLEV_TRUE: /* 21 */ |
| 1015 | case CBOR_SIMPLEV_NULL: /* 22 */ |
| 1016 | case CBOR_SIMPLEV_UNDEF: /* 23 */ |
| 1017 | case CBOR_SIMPLE_BREAK: /* 31 */ |
| 1018 | break; /* nothing to do */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1019 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1020 | case CBOR_SIMPLEV_ONEBYTE: /* 24 */ |
| 1021 | if(uArgument <= CBOR_SIMPLE_BREAK) { |
| 1022 | /* This takes out f8 00 ... f8 1f which should be encoded |
| 1023 | * as e0 … f7 |
| 1024 | */ |
| 1025 | uReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1026 | goto Done; |
| 1027 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 1028 | /* FALLTHROUGH */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1029 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1030 | default: /* 0-19 */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1031 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1032 | /* DecodeHead() will make uArgument equal to |
| 1033 | * nAdditionalInfo when nAdditionalInfo is < 24. This cast is |
| 1034 | * safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 1035 | * the double/float cases above |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1036 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1037 | pDecodedItem->val.uSimple = (uint8_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1038 | break; |
| 1039 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1040 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1041 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1042 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1046 | /** |
| 1047 | * @brief Decode text and byte strings |
| 1048 | * |
| 1049 | * @param[in] pAllocator The string allocator or NULL. |
| 1050 | * @param[in] uStrLen The length of the string. |
| 1051 | * @param[in] pUInBuf The surce from which to read the string's bytes. |
| 1052 | * @param[out] pDecodedItem The filled in decoded item. |
| 1053 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 1054 | * @retval QCBOR_ERR_HIT_END Unexpected end of input. |
| 1055 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1056 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1057 | * |
| 1058 | * The reads @c uStrlen bytes from @c pUInBuf and fills in @c |
| 1059 | * pDecodedItem. If @c pAllocator is not NULL then memory for the |
| 1060 | * string is allocated. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1061 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1062 | static QCBORError |
| 1063 | QCBOR_Private_DecodeBytes(const QCBORInternalAllocator *pAllocator, |
| 1064 | const uint64_t uStrLen, |
| 1065 | UsefulInputBuf *pUInBuf, |
| 1066 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1067 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1068 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1069 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1070 | /* CBOR lengths can be 64 bits, but size_t is not 64 bits on all |
| 1071 | * CPUs. This check makes the casts to size_t below safe. |
| 1072 | * |
| 1073 | * The max is 4 bytes less than the largest sizeof() so this can be |
| 1074 | * tested by putting a SIZE_MAX length in the CBOR test input (no |
| 1075 | * one will care the limit on strings is 4 bytes shorter). |
| 1076 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1077 | if(uStrLen > SIZE_MAX-4) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1078 | uReturn = QCBOR_ERR_STRING_TOO_LONG; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1079 | goto Done; |
| 1080 | } |
| 1081 | |
| 1082 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(pUInBuf, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1083 | if(UsefulBuf_IsNULLC(Bytes)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1084 | /* Failed to get the bytes for this string item */ |
| 1085 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1086 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1087 | } |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1088 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1089 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1090 | /* Note that this is not where allocation to coalesce |
| 1091 | * indefinite-length strings is done. This is for when the caller |
| 1092 | * has requested all strings be allocated. Disabling indefinite |
| 1093 | * length strings also disables this allocate-all option. |
| 1094 | */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1095 | if(pAllocator) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1096 | /* request to use the string allocator to make a copy */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1097 | UsefulBuf NewMem = StringAllocator_Allocate(pAllocator, (size_t)uStrLen); |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1098 | if(UsefulBuf_IsNULL(NewMem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1099 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1100 | goto Done; |
| 1101 | } |
| 1102 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1103 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1104 | goto Done; |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1105 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1106 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1107 | (void)pAllocator; |
| 1108 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1109 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1110 | /* Normal case with no string allocator */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1111 | pDecodedItem->val.string = Bytes; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1112 | |
Laurence Lundblade | 471a3fd | 2018-10-18 21:27:45 +0530 | [diff] [blame] | 1113 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1114 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1115 | } |
| 1116 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1117 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1118 | /** |
| 1119 | * @brief Map the CBOR major types for strings to the QCBOR types. |
| 1120 | * |
| 1121 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1122 | * @retturns QCBOR type number. |
| 1123 | * |
| 1124 | * This only works for the two string types. |
| 1125 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1126 | static uint8_t |
| 1127 | QCBOR_Private_ConvertStringMajorTypes(int nCBORMajorType) |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1128 | { |
| 1129 | #if CBOR_MAJOR_TYPE_BYTE_STRING + 4 != QCBOR_TYPE_BYTE_STRING |
| 1130 | #error QCBOR_TYPE_BYTE_STRING no lined up with major type |
| 1131 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1132 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1133 | #if CBOR_MAJOR_TYPE_TEXT_STRING + 4 != QCBOR_TYPE_TEXT_STRING |
| 1134 | #error QCBOR_TYPE_TEXT_STRING no lined up with major type |
| 1135 | #endif |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1136 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1137 | return (uint8_t)(nCBORMajorType + 4); |
| 1138 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1139 | |
| 1140 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1141 | /** |
| 1142 | * @brief Map the CBOR major types for arrays/maps to the QCBOR types. |
| 1143 | * |
| 1144 | * @param[in] nCBORMajorType The CBOR major type to convert. |
| 1145 | * @retturns QCBOR type number. |
| 1146 | * |
| 1147 | * This only works for the two aggregate types. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1148 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1149 | static uint8_t |
| 1150 | QCBORDecode_Private_ConvertArrayOrMapType(int nCBORMajorType) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1151 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1152 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 1153 | #error QCBOR_TYPE_ARRAY 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 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 1157 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 1158 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1159 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1160 | return (uint8_t)(nCBORMajorType); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1164 | /** |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1165 | * @brief Decode a single primitive data item (decode layer 6). |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1166 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1167 | * @param[in] pUInBuf Input buffer to read data item from. |
| 1168 | * @param[out] pDecodedItem The filled-in decoded item. |
| 1169 | * @param[in] pAllocator The allocator to use for strings or NULL. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1170 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 1171 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1172 | * features |
| 1173 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1174 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1175 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1176 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1177 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1178 | * of half-precision disabled |
| 1179 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1180 | * float decode is disabled. |
| 1181 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1182 | * simple type in input. |
| 1183 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1184 | * in input, but indefinite |
| 1185 | * lengths disabled. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1186 | * |
| 1187 | * This decodes the most primitive / atomic data item. It does |
| 1188 | * no combing of data items. |
| 1189 | */ |
| 1190 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1191 | QCBOR_Private_DecodeAtomicDataItem(UsefulInputBuf *pUInBuf, |
| 1192 | QCBORItem *pDecodedItem, |
| 1193 | const QCBORInternalAllocator *pAllocator) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1194 | { |
| 1195 | QCBORError uReturn; |
| 1196 | |
| 1197 | /* Get the major type and the argument. The argument could be |
| 1198 | * length of more bytes or the value depending on the major |
| 1199 | * type. nAdditionalInfo is an encoding of the length of the |
| 1200 | * uNumber and is needed to decode floats and doubles. |
| 1201 | */ |
| 1202 | int nMajorType = 0; |
| 1203 | uint64_t uArgument = 0; |
| 1204 | int nAdditionalInfo = 0; |
| 1205 | |
| 1206 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1207 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1208 | uReturn = QCBOR_Private_DecodeHead(pUInBuf, &nMajorType, &uArgument, &nAdditionalInfo); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1209 | if(uReturn) { |
| 1210 | goto Done; |
| 1211 | } |
| 1212 | |
| 1213 | /* At this point the major type and the argument are valid. We've |
| 1214 | * got the type and the argument that starts every CBOR data item. |
| 1215 | */ |
| 1216 | switch (nMajorType) { |
| 1217 | case CBOR_MAJOR_TYPE_POSITIVE_INT: /* Major type 0 */ |
| 1218 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: /* Major type 1 */ |
| 1219 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1220 | uReturn = QCBOR_ERR_BAD_INT; |
| 1221 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1222 | uReturn = QCBOR_Private_DecodeInteger(nMajorType, uArgument, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1223 | } |
| 1224 | break; |
| 1225 | |
| 1226 | case CBOR_MAJOR_TYPE_BYTE_STRING: /* Major type 2 */ |
| 1227 | case CBOR_MAJOR_TYPE_TEXT_STRING: /* Major type 3 */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1228 | pDecodedItem->uDataType = QCBOR_Private_ConvertStringMajorTypes(nMajorType); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1229 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1230 | pDecodedItem->val.string = (UsefulBufC){NULL, QCBOR_STRING_LENGTH_INDEFINITE}; |
| 1231 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1232 | uReturn = QCBOR_Private_DecodeBytes(pAllocator, uArgument, pUInBuf, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1233 | } |
| 1234 | break; |
| 1235 | |
| 1236 | case CBOR_MAJOR_TYPE_ARRAY: /* Major type 4 */ |
| 1237 | case CBOR_MAJOR_TYPE_MAP: /* Major type 5 */ |
| 1238 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1239 | /* Indefinite-length string. */ |
| 1240 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 1241 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
| 1242 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1243 | uReturn = QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED; |
| 1244 | break; |
| 1245 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1246 | } else { |
| 1247 | /* Definite-length string. */ |
| 1248 | if(uArgument > QCBOR_MAX_ITEMS_IN_ARRAY) { |
| 1249 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
| 1250 | goto Done; |
| 1251 | } |
| 1252 | /* cast OK because of check above */ |
| 1253 | pDecodedItem->val.uCount = (uint16_t)uArgument; |
| 1254 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1255 | pDecodedItem->uDataType = QCBORDecode_Private_ConvertArrayOrMapType(nMajorType); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1256 | break; |
| 1257 | |
| 1258 | case CBOR_MAJOR_TYPE_TAG: /* Major type 6, tag numbers */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1259 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1260 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1261 | uReturn = QCBOR_ERR_BAD_INT; |
| 1262 | } else { |
| 1263 | pDecodedItem->val.uTagV = uArgument; |
| 1264 | pDecodedItem->uDataType = QCBOR_TYPE_TAG; |
| 1265 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1266 | #else /* QCBOR_DISABLE_TAGS */ |
| 1267 | uReturn = QCBOR_ERR_TAGS_DISABLED; |
| 1268 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1269 | break; |
| 1270 | |
| 1271 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 1272 | /* Major type 7: float, double, true, false, null... */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1273 | uReturn = QCBOR_Private_DecodeType7(nAdditionalInfo, uArgument, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1274 | break; |
| 1275 | |
| 1276 | default: |
| 1277 | /* Never happens because DecodeHead() should never return > 7 */ |
| 1278 | uReturn = QCBOR_ERR_UNSUPPORTED; |
| 1279 | break; |
| 1280 | } |
| 1281 | |
| 1282 | Done: |
| 1283 | return uReturn; |
| 1284 | } |
| 1285 | |
| 1286 | |
| 1287 | /** |
| 1288 | * @brief Process indefinite-length strings (decode layer 5). |
| 1289 | * |
| 1290 | * @param[in] pMe Decoder context |
| 1291 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1292 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 1293 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1294 | * features |
| 1295 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1296 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1297 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1298 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1299 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1300 | * of half-precision disabled |
| 1301 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1302 | * float decode is disabled. |
| 1303 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1304 | * simple type in input. |
| 1305 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1306 | * in input, but indefinite |
| 1307 | * lengths disabled. |
| 1308 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 1309 | * but no string allocator. |
| 1310 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 1311 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 1312 | * input, but indefinite-length |
| 1313 | * strings are disabled. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1314 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1315 | * If @c pDecodedItem is not an indefinite-length string, this does nothing. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1316 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1317 | * If it is, this loops getting the subsequent chunk data items that |
| 1318 | * make up the string. The string allocator is used to make a |
| 1319 | * contiguous buffer for the chunks. When this completes @c |
| 1320 | * pDecodedItem contains the put-together string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1321 | * |
| 1322 | * Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1323 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1324 | static QCBORError |
| 1325 | QCBORDecode_Private_GetNextFullString(QCBORDecodeContext *pMe, |
| 1326 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1327 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1328 | /* Aproximate stack usage |
| 1329 | * 64-bit 32-bit |
| 1330 | * local vars 32 16 |
| 1331 | * 2 UsefulBufs 32 16 |
| 1332 | * QCBORItem 56 52 |
| 1333 | * TOTAL 120 74 |
| 1334 | */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1335 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1336 | /* The string allocator is used here for two purposes: 1) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1337 | * coalescing the chunks of an indefinite-length string, 2) |
| 1338 | * allocating storage for every string returned when requested. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1339 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1340 | * The first use is below in this function. Indefinite-length |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1341 | * strings cannot be processed at all without a string allocator. |
| 1342 | * |
| 1343 | * The second used is in DecodeBytes() which is called by |
| 1344 | * GetNext_Item() below. This second use unneccessary for most use |
| 1345 | * and only happens when requested in the call to |
| 1346 | * QCBORDecode_SetMemPool(). If the second use not requested then |
| 1347 | * NULL is passed for the string allocator to GetNext_Item(). |
| 1348 | * |
| 1349 | * QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS disables the string |
| 1350 | * allocator altogether and thus both of these uses. It reduced the |
| 1351 | * decoder object code by about 400 bytes. |
| 1352 | */ |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1353 | const QCBORInternalAllocator *pAllocatorForGetNext = NULL; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1354 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1355 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 1356 | const QCBORInternalAllocator *pAllocator = NULL; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1357 | |
| 1358 | if(pMe->StringAllocator.pfAllocator) { |
| 1359 | pAllocator = &(pMe->StringAllocator); |
| 1360 | if(pMe->bStringAllocateAll) { |
| 1361 | pAllocatorForGetNext = pAllocator; |
| 1362 | } |
| 1363 | } |
| 1364 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1365 | |
| 1366 | QCBORError uReturn; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1367 | uReturn = QCBOR_Private_DecodeAtomicDataItem(&(pMe->InBuf), pDecodedItem, pAllocatorForGetNext); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1368 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1369 | goto Done; |
| 1370 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1371 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1372 | /* Only do indefinite-length processing on strings */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1373 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 1374 | if(uStringType!= QCBOR_TYPE_BYTE_STRING && uStringType != QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1375 | goto Done; |
| 1376 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1377 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1378 | /* Is this a string with an indefinite length? */ |
| 1379 | if(pDecodedItem->val.string.len != QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1380 | goto Done; |
| 1381 | } |
| 1382 | |
| 1383 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1384 | /* Can't decode indefinite-length strings without a string allocator */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1385 | if(pAllocator == NULL) { |
| 1386 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1387 | goto Done; |
| 1388 | } |
| 1389 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1390 | /* Loop getting chunks of the indefinite-length string */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1391 | UsefulBufC FullString = NULLUsefulBufC; |
| 1392 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1393 | for(;;) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1394 | /* Get QCBORItem for next chunk */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1395 | QCBORItem StringChunkItem; |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1396 | /* Pass a NULL string allocator to GetNext_Item() because the |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1397 | * individual string chunks in an indefinite-length should not |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1398 | * be allocated. They are always copied in the the contiguous |
| 1399 | * buffer allocated here. |
| 1400 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1401 | uReturn = QCBOR_Private_DecodeAtomicDataItem(&(pMe->InBuf), &StringChunkItem, NULL); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1402 | if(uReturn) { |
| 1403 | break; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1404 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1405 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1406 | /* Is item is the marker for end of the indefinite-length string? */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1407 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1408 | /* String is complete */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1409 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1410 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1411 | break; |
| 1412 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1413 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1414 | /* 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] | 1415 | * that introduces the indefinite-length string. This also |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1416 | * 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] | 1417 | * indefinite-length string inside an indefinite-length string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1418 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1419 | if(StringChunkItem.uDataType != uStringType || |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1420 | StringChunkItem.val.string.len == QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1421 | uReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1422 | break; |
| 1423 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1424 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1425 | if (StringChunkItem.val.string.len > 0) { |
| 1426 | /* The first time throurgh FullString.ptr is NULL and this is |
| 1427 | * equivalent to StringAllocator_Allocate(). Subsequently it is |
| 1428 | * not NULL and a reallocation happens. |
| 1429 | */ |
| 1430 | UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator, |
| 1431 | FullString.ptr, |
| 1432 | FullString.len + StringChunkItem.val.string.len); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 1433 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1434 | if(UsefulBuf_IsNULL(NewMem)) { |
| 1435 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
| 1436 | break; |
| 1437 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1438 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1439 | /* Copy new string chunk to the end of accumulated string */ |
| 1440 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1441 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1442 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1443 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1444 | if(uReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1445 | /* Getting the item failed, clean up the allocated memory */ |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 1446 | StringAllocator_Free(pAllocator, FullString.ptr); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1447 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1448 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1449 | uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED; |
| 1450 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1451 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1452 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1453 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1454 | } |
| 1455 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1456 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1457 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1458 | /** |
| 1459 | * @brief This converts a tag number to a shorter mapped value for storage. |
| 1460 | * |
| 1461 | * @param[in] pMe The decode context. |
| 1462 | * @param[in] uUnMappedTag The tag number to map |
| 1463 | * @param[out] puMappedTagNumer The stored tag number. |
| 1464 | * |
| 1465 | * @return error code. |
| 1466 | * |
| 1467 | * The main point of mapping tag numbers is make QCBORItem |
| 1468 | * smaller. With this mapping storage of 4 tags takes up 8 |
| 1469 | * bytes. Without, it would take up 32 bytes. |
| 1470 | * |
| 1471 | * This maps tag numbers greater than QCBOR_LAST_UNMAPPED_TAG. |
| 1472 | * QCBOR_LAST_UNMAPPED_TAG is a little smaller than MAX_UINT16. |
| 1473 | * |
| 1474 | * See also UnMapTagNumber() and @ref QCBORItem. |
| 1475 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1476 | static QCBORError |
| 1477 | QCBORDecode_Private_MapTagNumber(QCBORDecodeContext *pMe, |
| 1478 | const uint64_t uUnMappedTag, |
| 1479 | uint16_t *puMappedTagNumer) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1480 | { |
| 1481 | if(uUnMappedTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 1482 | unsigned uTagMapIndex; |
| 1483 | /* Is there room in the tag map, or is it in it already? */ |
| 1484 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
| 1485 | if(pMe->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID64) { |
| 1486 | break; |
| 1487 | } |
| 1488 | if(pMe->auMappedTags[uTagMapIndex] == uUnMappedTag) { |
| 1489 | break; |
| 1490 | } |
| 1491 | } |
| 1492 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1493 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 1494 | } |
| 1495 | |
| 1496 | /* Covers the cases where tag is new and were it is already in the map */ |
| 1497 | pMe->auMappedTags[uTagMapIndex] = uUnMappedTag; |
| 1498 | *puMappedTagNumer = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1499 | |
| 1500 | } else { |
| 1501 | *puMappedTagNumer = (uint16_t)uUnMappedTag; |
| 1502 | } |
| 1503 | |
| 1504 | return QCBOR_SUCCESS; |
| 1505 | } |
| 1506 | |
| 1507 | |
| 1508 | /** |
| 1509 | * @brief This converts a mapped tag number to the actual tag number. |
| 1510 | * |
| 1511 | * @param[in] pMe The decode context. |
| 1512 | * @param[in] uMappedTagNumber The stored tag number. |
| 1513 | * |
| 1514 | * @return The actual tag number is returned or |
| 1515 | * @ref CBOR_TAG_INVALID64 on error. |
| 1516 | * |
| 1517 | * This is the reverse of MapTagNumber() |
| 1518 | */ |
| 1519 | static uint64_t |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1520 | QCBORDecode_Private_UnMapTagNumber(const QCBORDecodeContext *pMe, |
| 1521 | const uint16_t uMappedTagNumber) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1522 | { |
| 1523 | if(uMappedTagNumber <= QCBOR_LAST_UNMAPPED_TAG) { |
| 1524 | return uMappedTagNumber; |
| 1525 | } else if(uMappedTagNumber == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1526 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1527 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1528 | /* This won't be negative because of code below in |
| 1529 | * MapTagNumber() |
| 1530 | */ |
| 1531 | const unsigned uIndex = uMappedTagNumber - (QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1532 | return pMe->auMappedTags[uIndex]; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1533 | } |
| 1534 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1535 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1536 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1537 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1538 | /** |
| 1539 | * @brief Aggregate all tags wrapping a data item (decode layer 4). |
| 1540 | * |
| 1541 | * @param[in] pMe Decoder context |
| 1542 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 1543 | * |
| 1544 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1545 | * features |
| 1546 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1547 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1548 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1549 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1550 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1551 | * of half-precision disabled |
| 1552 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1553 | * float decode is disabled. |
| 1554 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1555 | * simple type in input. |
| 1556 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1557 | * in input, but indefinite |
| 1558 | * lengths disabled. |
| 1559 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 1560 | * but no string allocator. |
| 1561 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 1562 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 1563 | * input, but indefinite-length |
| 1564 | * strings are disabled. |
| 1565 | * @retval QCBOR_ERR_TOO_MANY_TAGS Too many tag numbers on item. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1566 | * |
| 1567 | * This loops getting atomic data items until one is not a tag |
| 1568 | * number. Usually this is largely pass-through because most |
| 1569 | * item are not tag numbers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1570 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1571 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1572 | QCBORDecode_Private_GetNextTagNumber(QCBORDecodeContext *pMe, |
| 1573 | QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1574 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1575 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1576 | /* Accummulate the tags from multiple items here and then copy them |
| 1577 | * into the last item, the non-tag item. |
| 1578 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1579 | uint16_t auItemsTags[QCBOR_MAX_TAGS_PER_ITEM]; |
| 1580 | |
| 1581 | /* Initialize to CBOR_TAG_INVALID16 */ |
| 1582 | #if CBOR_TAG_INVALID16 != 0xffff |
| 1583 | /* Be sure the memset does the right thing. */ |
| 1584 | #err CBOR_TAG_INVALID16 tag not defined as expected |
| 1585 | #endif |
| 1586 | memset(auItemsTags, 0xff, sizeof(auItemsTags)); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1587 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1588 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1589 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1590 | /* Loop fetching data items until the item fetched is not a tag */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1591 | for(;;) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1592 | QCBORError uErr = QCBORDecode_Private_GetNextFullString(pMe, pDecodedItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1593 | if(uErr != QCBOR_SUCCESS) { |
| 1594 | uReturn = uErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1595 | goto Done; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1596 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1597 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1598 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1599 | /* Successful exit from loop; maybe got some tags, maybe not */ |
| 1600 | memcpy(pDecodedItem->uTags, auItemsTags, sizeof(auItemsTags)); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1601 | break; |
| 1602 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1603 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1604 | if(auItemsTags[QCBOR_MAX_TAGS_PER_ITEM - 1] != CBOR_TAG_INVALID16) { |
| 1605 | /* No room in the tag list */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1606 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1607 | /* Continue on to get all tags wrapping this item even though |
| 1608 | * it is erroring out in the end. This allows decoding to |
| 1609 | * continue. This is a resource limit error, not a problem |
| 1610 | * with being well-formed CBOR. |
| 1611 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1612 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1613 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1614 | /* Slide tags over one in the array to make room at index 0. |
| 1615 | * Must use memmove because the move source and destination |
| 1616 | * overlap. |
| 1617 | */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1618 | memmove(&auItemsTags[1], |
| 1619 | auItemsTags, |
| 1620 | sizeof(auItemsTags) - sizeof(auItemsTags[0])); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1621 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1622 | /* Map the tag */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1623 | uint16_t uMappedTagNumber = 0; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1624 | uReturn = QCBORDecode_Private_MapTagNumber(pMe, pDecodedItem->val.uTagV, &uMappedTagNumber); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1625 | /* Continue even on error so as to consume all tags wrapping |
| 1626 | * this data item so decoding can go on. If MapTagNumber() |
| 1627 | * errors once it will continue to error. |
| 1628 | */ |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1629 | auItemsTags[0] = uMappedTagNumber; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1630 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1631 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1632 | Done: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1633 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1634 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1635 | #else /* QCBOR_DISABLE_TAGS */ |
| 1636 | |
| 1637 | return QCBORDecode_GetNextFullString(pMe, pDecodedItem); |
| 1638 | |
| 1639 | #endif /* QCBOR_DISABLE_TAGS */ |
| 1640 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1641 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 1642 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1643 | /** |
| 1644 | * @brief Combine a map entry label and value into one item (decode layer 3). |
| 1645 | * |
| 1646 | * @param[in] pMe Decoder context |
| 1647 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1648 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 1649 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1650 | * features |
| 1651 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1652 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1653 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1654 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1655 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1656 | * of half-precision disabled |
| 1657 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1658 | * float decode is disabled. |
| 1659 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1660 | * simple type in input. |
| 1661 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1662 | * in input, but indefinite |
| 1663 | * lengths disabled. |
| 1664 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 1665 | * but no string allocator. |
| 1666 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 1667 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 1668 | * input, but indefinite-length |
| 1669 | * strings are disabled. |
| 1670 | * @retval QCBOR_ERR_TOO_MANY_TAGS Too many tag numbers on item. |
| 1671 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG Too many items in array. |
| 1672 | * @retval QCBOR_ERR_MAP_LABEL_TYPE Map label not string or integer. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1673 | * |
| 1674 | * If a the current nesting level is a map, then this |
| 1675 | * combines pairs of items into one data item with a label |
| 1676 | * and value. |
| 1677 | * |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 1678 | * This is passthrough if the current nesting level is |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1679 | * not a map. |
| 1680 | * |
| 1681 | * This also implements maps-as-array mode where a map |
| 1682 | * is treated like an array to allow caller to do their |
| 1683 | * own label processing. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1684 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1685 | static QCBORError |
| 1686 | QCBORDecode_Private_GetNextMapEntry(QCBORDecodeContext *pMe, |
| 1687 | QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1688 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1689 | QCBORError uReturn = QCBORDecode_Private_GetNextTagNumber(pMe, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1690 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 1691 | goto Done; |
| 1692 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1693 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1694 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
| 1695 | /* Break can't be a map entry */ |
| 1696 | goto Done; |
| 1697 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1698 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1699 | if(pMe->uDecodeMode != QCBOR_DECODE_MODE_MAP_AS_ARRAY) { |
| 1700 | /* Normal decoding of maps -- combine label and value into one item. */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1701 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1702 | if(DecodeNesting_IsCurrentTypeMap(&(pMe->nesting))) { |
| 1703 | /* Save label in pDecodedItem and get the next which will |
| 1704 | * be the real data item. |
| 1705 | */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1706 | QCBORItem LabelItem = *pDecodedItem; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1707 | uReturn = QCBORDecode_Private_GetNextTagNumber(pMe, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1708 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1709 | goto Done; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 1710 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1711 | |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1712 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1713 | |
| 1714 | if(LabelItem.uDataType == QCBOR_TYPE_TEXT_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1715 | /* strings are always good labels */ |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1716 | pDecodedItem->label.string = LabelItem.val.string; |
| 1717 | pDecodedItem->uLabelType = QCBOR_TYPE_TEXT_STRING; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1718 | } else if (QCBOR_DECODE_MODE_MAP_STRINGS_ONLY == pMe->uDecodeMode) { |
| 1719 | /* It's not a string and we only want strings */ |
| 1720 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1721 | goto Done; |
| 1722 | } else if(LabelItem.uDataType == QCBOR_TYPE_INT64) { |
| 1723 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 1724 | pDecodedItem->uLabelType = QCBOR_TYPE_INT64; |
| 1725 | } else if(LabelItem.uDataType == QCBOR_TYPE_UINT64) { |
| 1726 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 1727 | pDecodedItem->uLabelType = QCBOR_TYPE_UINT64; |
| 1728 | } else if(LabelItem.uDataType == QCBOR_TYPE_BYTE_STRING) { |
| 1729 | pDecodedItem->label.string = LabelItem.val.string; |
| 1730 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 1731 | pDecodedItem->uLabelType = QCBOR_TYPE_BYTE_STRING; |
| 1732 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1733 | /* label is not an int or a string. It is an arrray |
| 1734 | * or a float or such and this implementation doesn't handle that. |
| 1735 | * Also, tags on labels are ignored. |
| 1736 | */ |
| 1737 | uReturn = QCBOR_ERR_MAP_LABEL_TYPE; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 1738 | goto Done; |
| 1739 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1740 | } |
| 1741 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1742 | /* Decoding of maps as arrays to let the caller decide what to do |
| 1743 | * about labels, particularly lables that are not integers or |
| 1744 | * strings. |
| 1745 | */ |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1746 | if(pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1747 | if(pDecodedItem->val.uCount > QCBOR_MAX_ITEMS_IN_ARRAY/2) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1748 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1749 | goto Done; |
| 1750 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 1751 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1752 | /* Cast is safe because of check against QCBOR_MAX_ITEMS_IN_ARRAY/2. |
| 1753 | * Cast is needed because of integer promotion. |
| 1754 | */ |
Laurence Lundblade | e6bcef1 | 2020-04-01 10:56:27 -0700 | [diff] [blame] | 1755 | pDecodedItem->val.uCount = (uint16_t)(pDecodedItem->val.uCount * 2); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1756 | } |
| 1757 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1758 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1759 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1760 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1764 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1765 | /** |
| 1766 | * @brief Peek and see if next data item is a break; |
| 1767 | * |
| 1768 | * @param[in] pUIB UsefulInputBuf to read from. |
| 1769 | * @param[out] pbNextIsBreak Indicate if next was a break or not. |
| 1770 | * |
| 1771 | * @return Any decoding error. |
| 1772 | * |
| 1773 | * See if next item is a CBOR break. If it is, it is consumed, |
| 1774 | * if not it is not consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1775 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1776 | static QCBORError |
| 1777 | QCBOR_Private_NextIsBreak(UsefulInputBuf *pUIB, bool *pbNextIsBreak) |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1778 | { |
| 1779 | *pbNextIsBreak = false; |
| 1780 | if(UsefulInputBuf_BytesUnconsumed(pUIB) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1781 | QCBORItem Peek; |
| 1782 | size_t uPeek = UsefulInputBuf_Tell(pUIB); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1783 | QCBORError uReturn = QCBOR_Private_DecodeAtomicDataItem(pUIB, &Peek, NULL); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1784 | if(uReturn != QCBOR_SUCCESS) { |
| 1785 | return uReturn; |
| 1786 | } |
| 1787 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1788 | /* It is not a break, rewind so it can be processed normally. */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 1789 | UsefulInputBuf_Seek(pUIB, uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1790 | } else { |
| 1791 | *pbNextIsBreak = true; |
| 1792 | } |
| 1793 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1794 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1795 | return QCBOR_SUCCESS; |
| 1796 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1797 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1798 | |
| 1799 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1800 | /** |
| 1801 | * @brief Ascend up nesting levels if all items in them have been consumed. |
| 1802 | * |
| 1803 | * @param[in] pMe The decode context. |
| 1804 | * @param[in] bMarkEnd If true mark end of maps/arrays with count of zero. |
| 1805 | * |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1806 | * An item was just consumed, now figure out if it was the |
| 1807 | * end of an array/map map that can be closed out. That |
| 1808 | * may in turn close out the above array/map... |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1809 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1810 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1811 | QCBORDecode_Private_NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1812 | { |
| 1813 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 1814 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1815 | /* Loop ascending nesting levels as long as there is ascending to do */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1816 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
| 1817 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1818 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
| 1819 | /* Nesting level is bstr-wrapped CBOR */ |
| 1820 | |
| 1821 | /* Ascent for bstr-wrapped CBOR is always by explicit call |
| 1822 | * so no further ascending can happen. |
| 1823 | */ |
| 1824 | break; |
| 1825 | |
| 1826 | } else if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 1827 | /* Level is a definite-length array/map */ |
| 1828 | |
| 1829 | /* Decrement the item count the definite-length array/map */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1830 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 1831 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1832 | /* Didn't close out array/map, so all work here is done */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1833 | break; |
| 1834 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1835 | /* All items in a definite-length array were consumed so it |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1836 | * is time to ascend one level. This happens below. |
| 1837 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1838 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1839 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1840 | } else { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1841 | /* Level is an indefinite-length array/map. */ |
| 1842 | |
| 1843 | /* Check for a break which is what ends indefinite-length arrays/maps */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1844 | bool bIsBreak = false; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1845 | uReturn = QCBOR_Private_NextIsBreak(&(pMe->InBuf), &bIsBreak); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1846 | if(uReturn != QCBOR_SUCCESS) { |
| 1847 | goto Done; |
| 1848 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1849 | |
| 1850 | if(!bIsBreak) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1851 | /* 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] | 1852 | break; |
| 1853 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1854 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1855 | /* It was a break in an indefinitelength map / array so |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1856 | * it is time to ascend one level. |
| 1857 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1858 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1859 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1860 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1861 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1862 | |
| 1863 | /* All items in the array/map have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1864 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 1865 | /* But ascent in bounded mode is only by explicit call to |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1866 | * QCBORDecode_ExitBoundedMode(). |
| 1867 | */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 1868 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1869 | /* Set the count to zero for definite-length arrays to indicate |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1870 | * cursor is at end of bounded array/map */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1871 | if(bMarkEnd) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1872 | /* Used for definite and indefinite to signal end */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 1873 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 1874 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 1875 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1876 | break; |
| 1877 | } |
| 1878 | |
| 1879 | /* Finally, actually ascend one level. */ |
| 1880 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 1881 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1882 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1883 | uReturn = QCBOR_SUCCESS; |
| 1884 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1885 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1886 | Done: |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1887 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1888 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 1889 | return uReturn; |
| 1890 | } |
| 1891 | |
| 1892 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1893 | /** |
| 1894 | * @brief Ascending & Descending out of nesting levels (decode layer 2). |
| 1895 | * |
| 1896 | * @param[in] pMe Decoder context |
| 1897 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 1898 | |
| 1899 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1900 | * features |
| 1901 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1902 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1903 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1904 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1905 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1906 | * of half-precision disabled |
| 1907 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1908 | * float decode is disabled. |
| 1909 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1910 | * simple type in input. |
| 1911 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1912 | * in input, but indefinite |
| 1913 | * lengths disabled. |
| 1914 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 1915 | * but no string allocator. |
| 1916 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 1917 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 1918 | * input, but indefinite-length |
| 1919 | * strings are disabled. |
| 1920 | * @retval QCBOR_ERR_TOO_MANY_TAGS Too many tag numbers on item. |
| 1921 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG Too many items in array. |
| 1922 | * @retval QCBOR_ERR_MAP_LABEL_TYPE Map label not string or integer. |
| 1923 | * @retval QCBOR_ERR_NO_MORE_ITEMS Need more items for map or array. |
| 1924 | * @retval QCBOR_ERR_BAD_BREAK Indefinite-length break in wrong |
| 1925 | * place. |
| 1926 | * @retval QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP Nesting deeper than QCBOR |
| 1927 | * can handle. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1928 | * |
| 1929 | * This handles the traversal descending into and asecnding out of |
| 1930 | * maps, arrays and bstr-wrapped CBOR. It figures out the ends of |
| 1931 | * definite- and indefinte-length maps and arrays by looking at the |
| 1932 | * item count or finding CBOR breaks. It detects the ends of the |
| 1933 | * top-level sequence and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1934 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1935 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1936 | QCBORDecode_Private_GetNextMapOrArray(QCBORDecodeContext *pMe, |
| 1937 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1938 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1939 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1940 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1941 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1942 | /* If out of bytes to consume, it is either the end of the |
| 1943 | * top-level sequence of some bstr-wrapped CBOR that was entered. |
| 1944 | * |
| 1945 | * In the case of bstr-wrapped CBOR, the length of the |
| 1946 | * UsefulInputBuf was set to that of the bstr-wrapped CBOR. When |
| 1947 | * the bstr-wrapped CBOR is exited, the length is set back to the |
| 1948 | * top-level's length or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1949 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1950 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1951 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1952 | goto Done; |
| 1953 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 1954 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1955 | /* Check to see if at the end of a bounded definite-length map or |
| 1956 | * array. The check for a break ending indefinite-length array is |
| 1957 | * later in QCBORDecode_NestLevelAscender(). |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 1958 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1959 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1960 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 1961 | goto Done; |
| 1962 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 1963 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 1964 | /* ==== Next: not at the end, so get another item ==== */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1965 | uReturn = QCBORDecode_Private_GetNextMapEntry(pMe, pDecodedItem); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1966 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 1967 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 1968 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1969 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 1970 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1971 | /* Breaks ending arrays/maps are processed later in the call to |
| 1972 | * QCBORDecode_NestLevelAscender(). They should never show up here. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1973 | */ |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1974 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 1975 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 1976 | goto Done; |
Laurence Lundblade | 5b8c585 | 2018-10-14 21:11:42 +0530 | [diff] [blame] | 1977 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1978 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1979 | /* Record the nesting level for this data item before processing |
| 1980 | * any of decrementing and descending. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1981 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1982 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1983 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1984 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 1985 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1986 | if(QCBORItem_IsMapOrArray(*pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1987 | /* If the new item is a map or array, descend. |
| 1988 | * |
| 1989 | * Empty indefinite-length maps and arrays are descended into, |
| 1990 | * but then ascended out of in the next chunk of code. |
| 1991 | * |
| 1992 | * Maps and arrays do count as items in the map/array that |
| 1993 | * encloses them so a decrement needs to be done for them too, |
| 1994 | * but that is done only when all the items in them have been |
| 1995 | * processed, not when they are opened with the exception of an |
| 1996 | * empty map or array. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 1997 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 1998 | QCBORError uDescendErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1999 | uDescendErr = DecodeNesting_DescendMapOrArray(&(pMe->nesting), |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2000 | pDecodedItem->uDataType, |
| 2001 | pDecodedItem->val.uCount); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2002 | if(uDescendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2003 | /* This error is probably a traversal error and it overrides |
| 2004 | * the non-traversal error. |
| 2005 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2006 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2007 | goto Done; |
| 2008 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2011 | if(!QCBORItem_IsMapOrArray(*pDecodedItem) || |
| 2012 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(*pDecodedItem) || |
| 2013 | QCBORItem_IsIndefiniteLengthMapOrArray(*pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2014 | /* The following cases are handled here: |
| 2015 | * - A non-aggregate item like an integer or string |
| 2016 | * - An empty definite-length map or array |
| 2017 | * - An indefinite-length map or array that might be empty or might not. |
| 2018 | * |
| 2019 | * QCBORDecode_NestLevelAscender() does the work of decrementing the count |
| 2020 | * for an definite-length map/array and break detection for an |
| 2021 | * indefinite-0length map/array. If the end of the map/array was |
| 2022 | * reached, then it ascends nesting levels, possibly all the way |
| 2023 | * to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2024 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2025 | QCBORError uAscendErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2026 | uAscendErr = QCBORDecode_Private_NestLevelAscender(pMe, true); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2027 | if(uAscendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2028 | /* This error is probably a traversal error and it overrides |
| 2029 | * the non-traversal error. |
| 2030 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2031 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2032 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 2033 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2034 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2035 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 2036 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2037 | /* Tell the caller what level is next. This tells them what |
| 2038 | * maps/arrays were closed out and makes it possible for them to |
| 2039 | * reconstruct the tree with just the information returned in a |
| 2040 | * QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2041 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2042 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2043 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2044 | pDecodedItem->uNextNestLevel = 0; |
| 2045 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2046 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2047 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2048 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2049 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2050 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2053 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2054 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2055 | /** |
| 2056 | * @brief Shift 0th tag out of the tag list. |
| 2057 | * |
| 2058 | * pDecodedItem[in,out] The data item to convert. |
| 2059 | * |
| 2060 | * The 0th tag is discarded. \ref CBOR_TAG_INVALID16 is |
| 2061 | * shifted into empty slot at the end of the tag list. |
| 2062 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2063 | static void |
| 2064 | QCBOR_Private_ShiftTags(QCBORItem *pDecodedItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2065 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2066 | for(int i = 0; i < QCBOR_MAX_TAGS_PER_ITEM-1; i++) { |
| 2067 | pDecodedItem->uTags[i] = pDecodedItem->uTags[i+1]; |
| 2068 | } |
| 2069 | pDecodedItem->uTags[QCBOR_MAX_TAGS_PER_ITEM-1] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2070 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2071 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2072 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 2073 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2074 | /** |
| 2075 | * @brief Convert different epoch date formats in to the QCBOR epoch date format |
| 2076 | * |
| 2077 | * pDecodedItem[in,out] The data item to convert. |
| 2078 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 2079 | * @retval QCBOR_ERR_DATE_OVERFLOW 65-bit negative integer. |
| 2080 | * @retval QCBOR_ERR_FLOAT_DATE_DISABLED Float-point date in input, |
| 2081 | * floating-point date disabled. |
| 2082 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point date in input, |
| 2083 | * all floating-point disabled. |
| 2084 | * @retval QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT Unexpected and unrecoverable |
| 2085 | * error decoding date. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2086 | * |
| 2087 | * The epoch date tag defined in QCBOR allows for floating-point |
| 2088 | * dates. It even allows a protocol to flop between date formats when |
| 2089 | * ever it wants. Floating-point dates aren't that useful as they are |
| 2090 | * only needed for dates beyond the age of the earth. |
| 2091 | * |
| 2092 | * This converts all the date formats into one format of an unsigned |
| 2093 | * integer plus a floating-point fraction. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2094 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2095 | static QCBORError |
| 2096 | QCBOR_Private_DecodeDateEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2097 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2098 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2099 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2100 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2101 | pDecodedItem->val.epochDate.fSecondsFraction = 0; |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 2102 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2103 | |
| 2104 | switch (pDecodedItem->uDataType) { |
| 2105 | |
| 2106 | case QCBOR_TYPE_INT64: |
| 2107 | pDecodedItem->val.epochDate.nSeconds = pDecodedItem->val.int64; |
| 2108 | break; |
| 2109 | |
| 2110 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2111 | /* This only happens for CBOR type 0 > INT64_MAX so it is |
| 2112 | * always an overflow. |
| 2113 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2114 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 2115 | goto Done; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2116 | break; |
| 2117 | |
| 2118 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 2119 | case QCBOR_TYPE_FLOAT: |
| 2120 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2121 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2122 | /* Convert working value to double if input was a float */ |
Laurence Lundblade | 3ed0bca | 2020-07-14 22:50:10 -0700 | [diff] [blame] | 2123 | const double d = pDecodedItem->uDataType == QCBOR_TYPE_DOUBLE ? |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2124 | pDecodedItem->val.dfnum : |
| 2125 | (double)pDecodedItem->val.fnum; |
| 2126 | |
| 2127 | /* The conversion from float to integer requires overflow |
| 2128 | * detection since floats can be much larger than integers. |
| 2129 | * This implementation errors out on these large float values |
| 2130 | * since they are beyond the age of the earth. |
| 2131 | * |
| 2132 | * These constants for the overflow check are computed by the |
| 2133 | * compiler. They are not computed at run time. |
| 2134 | * |
| 2135 | * The factor of 0x7ff is added/subtracted to avoid a |
| 2136 | * rounding error in the wrong direction when the compiler |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2137 | * computes these constants. There is rounding because a |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2138 | * 64-bit integer has 63 bits of precision where a double |
| 2139 | * only has 53 bits. Without the 0x7ff factor, the compiler |
| 2140 | * may round up and produce a double for the bounds check |
| 2141 | * that is larger than can be stored in a 64-bit integer. The |
| 2142 | * amount of 0x7ff is picked because it has 11 bits set. |
| 2143 | * |
| 2144 | * Without the 0x7ff there is a ~30 minute range of time |
| 2145 | * values 10 billion years in the past and in the future |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2146 | * where this code could go wrong. Some compilers |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2147 | * generate a warning or error without the 0x7ff. |
| 2148 | */ |
| 2149 | const double dDateMax = (double)(INT64_MAX - 0x7ff); |
| 2150 | const double dDateMin = (double)(INT64_MIN + 0x7ff); |
| 2151 | |
| 2152 | if(isnan(d) || d > dDateMax || d < dDateMin) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2153 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2154 | goto Done; |
| 2155 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2156 | |
| 2157 | /* The actual conversion */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2158 | pDecodedItem->val.epochDate.nSeconds = (int64_t)d; |
Laurence Lundblade | 2feb1e1 | 2020-07-15 03:50:45 -0700 | [diff] [blame] | 2159 | pDecodedItem->val.epochDate.fSecondsFraction = |
| 2160 | d - (double)pDecodedItem->val.epochDate.nSeconds; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2161 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2162 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2163 | |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 2164 | uReturn = QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | 4b27064 | 2020-08-14 12:53:07 -0700 | [diff] [blame] | 2165 | goto Done; |
| 2166 | |
Laurence Lundblade | 9682a53 | 2020-06-06 18:33:04 -0700 | [diff] [blame] | 2167 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2168 | break; |
| 2169 | |
| 2170 | default: |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2171 | /* It's the arrays and maps that are unrecoverable because |
| 2172 | * they are not consumed here. Since this is just an error |
| 2173 | * condition, no extra code is added here to make the error |
| 2174 | * recoverable for non-arrays and maps like strings. */ |
| 2175 | uReturn = QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2176 | goto Done; |
| 2177 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2178 | |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2179 | pDecodedItem->uDataType = QCBOR_TYPE_DATE_EPOCH; |
| 2180 | |
| 2181 | Done: |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 2182 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2183 | } |
| 2184 | |
| 2185 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2186 | /** |
| 2187 | * @brief Convert the days epoch date. |
| 2188 | * |
| 2189 | * pDecodedItem[in,out] The data item to convert. |
| 2190 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 2191 | * @retval QCBOR_ERR_DATE_OVERFLOW 65-bit negative integer. |
| 2192 | * @retval QCBOR_ERR_FLOAT_DATE_DISABLED Float-point date in input, |
| 2193 | * floating-point date disabled. |
| 2194 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point date in input, |
| 2195 | * all floating-point disabled. |
| 2196 | * @retval QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT Unexpected and unrecoverable |
| 2197 | * error decoding date. |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2198 | * |
| 2199 | * This is much simpler than the other epoch date format because |
| 2200 | * floating-porint is not allowed. This is mostly a simple type check. |
| 2201 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2202 | static QCBORError |
| 2203 | QCBOR_Private_DecodeDaysEpoch(QCBORItem *pDecodedItem) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2204 | { |
| 2205 | QCBORError uReturn = QCBOR_SUCCESS; |
| 2206 | |
| 2207 | switch (pDecodedItem->uDataType) { |
| 2208 | |
| 2209 | case QCBOR_TYPE_INT64: |
| 2210 | pDecodedItem->val.epochDays = pDecodedItem->val.int64; |
| 2211 | break; |
| 2212 | |
| 2213 | case QCBOR_TYPE_UINT64: |
| 2214 | /* This only happens for CBOR type 0 > INT64_MAX so it is |
| 2215 | * always an overflow. |
| 2216 | */ |
| 2217 | uReturn = QCBOR_ERR_DATE_OVERFLOW; |
| 2218 | goto Done; |
| 2219 | break; |
| 2220 | |
| 2221 | default: |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2222 | /* It's the arrays and maps that are unrecoverable because |
| 2223 | * they are not consumed here. Since this is just an error |
| 2224 | * condition, no extra code is added here to make the error |
| 2225 | * recoverable for non-arrays and maps like strings. */ |
| 2226 | uReturn = QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2227 | goto Done; |
| 2228 | break; |
| 2229 | } |
| 2230 | |
| 2231 | pDecodedItem->uDataType = QCBOR_TYPE_DAYS_EPOCH; |
| 2232 | |
| 2233 | Done: |
| 2234 | return uReturn; |
| 2235 | } |
| 2236 | |
| 2237 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2238 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2239 | |
| 2240 | /* Forward declaration is necessary for |
| 2241 | * QCBORDecode_MantissaAndExponent(). to be able to decode bignum |
| 2242 | * tags in the mantissa. If the mantissa is a decimal fraction or big |
| 2243 | * float in error, this will result in a recurive call to |
| 2244 | * QCBORDecode_MantissaAndExponent(), but the recursion will unwined |
| 2245 | * correctly and the correct error is returned. |
| 2246 | */ |
| 2247 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2248 | QCBORDecode_Private_GetNextTagContent(QCBORDecodeContext *pMe, |
| 2249 | QCBORItem *pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2250 | |
| 2251 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2252 | /** |
| 2253 | * @brief Decode decimal fractions and big floats. |
| 2254 | * |
| 2255 | * @param[in] pMe The decode context. |
| 2256 | * @param[in,out] pDecodedItem On input the array data item that |
| 2257 | * holds the mantissa and exponent. On |
| 2258 | * output the decoded mantissa and |
| 2259 | * exponent. |
| 2260 | * |
| 2261 | * @returns Decoding errors from getting primitive data items or |
| 2262 | * \ref QCBOR_ERR_BAD_EXP_AND_MANTISSA. |
| 2263 | * |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2264 | * When called pDecodedItem must be the array with two members, the |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2265 | * exponent and mantissa. |
| 2266 | * |
| 2267 | * This will fetch and decode the exponent and mantissa and put the |
| 2268 | * result back into pDecodedItem. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2269 | * |
| 2270 | * This does no checking or processing of tag numbers. That is to be |
| 2271 | * done by the code that calls this. |
| 2272 | * |
| 2273 | * This stuffs the type of the mantissa into pDecodedItem with the expectation |
| 2274 | * the caller will process it. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2275 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2276 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2277 | QCBORDecode_Private_ExpMantissa(QCBORDecodeContext *pMe, |
| 2278 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2279 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2280 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2281 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2282 | /* --- Make sure it is an array; track nesting level of members --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2283 | if(pDecodedItem->uDataType != QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2284 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2285 | goto Done; |
| 2286 | } |
| 2287 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2288 | /* A check for pDecodedItem->val.uCount == 2 would work for |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2289 | * definite-length arrays, but not for indefinite. Instead remember |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2290 | * the nesting level the two integers must be at, which is one |
| 2291 | * deeper than that of the array. |
| 2292 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2293 | const int nNestLevel = pDecodedItem->uNestingLevel + 1; |
| 2294 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2295 | /* --- Get the exponent --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2296 | QCBORItem exponentItem; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2297 | uReturn = QCBORDecode_Private_GetNextMapOrArray(pMe, &exponentItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2298 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2299 | goto Done; |
| 2300 | } |
| 2301 | if(exponentItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2302 | /* Array is empty or a map/array encountered when expecting an int */ |
| 2303 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2304 | goto Done; |
| 2305 | } |
| 2306 | if(exponentItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2307 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2308 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2309 | * also means that the only data arriving here of type |
| 2310 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2311 | * and thus an error that will get handled in the next else. |
| 2312 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2313 | pDecodedItem->val.expAndMantissa.nExponent = exponentItem.val.int64; |
| 2314 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2315 | /* Wrong type of exponent or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2316 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2317 | goto Done; |
| 2318 | } |
| 2319 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2320 | /* --- Get the mantissa --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2321 | QCBORItem mantissaItem; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2322 | uReturn = QCBORDecode_Private_GetNextTagContent(pMe, &mantissaItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2323 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2324 | goto Done; |
| 2325 | } |
| 2326 | if(mantissaItem.uNestingLevel != nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2327 | /* Mantissa missing or map/array encountered when expecting number */ |
| 2328 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2329 | goto Done; |
| 2330 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2331 | /* Stuff the mantissa data type into the item to send it up to the |
| 2332 | * the next level. */ |
| 2333 | pDecodedItem->uDataType = mantissaItem.uDataType; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2334 | if(mantissaItem.uDataType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2335 | /* Data arriving as an unsigned int < INT64_MAX has been |
| 2336 | * converted to QCBOR_TYPE_INT64 and thus handled here. This is |
| 2337 | * also means that the only data arriving here of type |
| 2338 | * QCBOR_TYPE_UINT64 data will be too large for this to handle |
| 2339 | * and thus an error that will get handled in an else below. |
| 2340 | */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2341 | pDecodedItem->val.expAndMantissa.Mantissa.nInt = mantissaItem.val.int64; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2342 | #ifndef QCBOR_DISABLE_TAGS |
| 2343 | /* With tags fully disabled a big number mantissa will error out |
| 2344 | * in the call to QCBORDecode_GetNextWithTags() because it has |
| 2345 | * a tag number. |
| 2346 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2347 | } else if(mantissaItem.uDataType == QCBOR_TYPE_POSBIGNUM || |
| 2348 | mantissaItem.uDataType == QCBOR_TYPE_NEGBIGNUM) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2349 | /* Got a good big num mantissa */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2350 | pDecodedItem->val.expAndMantissa.Mantissa.bigNum = mantissaItem.val.bigNum; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2351 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2352 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2353 | /* Wrong type of mantissa or a QCBOR_TYPE_UINT64 > INT64_MAX */ |
| 2354 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2355 | goto Done; |
| 2356 | } |
| 2357 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2358 | /* --- Check that array only has the two numbers --- */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2359 | if(mantissaItem.uNextNestLevel == nNestLevel) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2360 | /* Extra items in the decimal fraction / big float */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2361 | /* Improvement: this should probably be an unrecoverable error. */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2362 | uReturn = QCBOR_ERR_BAD_EXP_AND_MANTISSA; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2363 | goto Done; |
| 2364 | } |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2365 | pDecodedItem->uNextNestLevel = mantissaItem.uNextNestLevel; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2366 | |
| 2367 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2368 | return uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2369 | } |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2370 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2371 | |
| 2372 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2373 | #ifndef QCBOR_DISABLE_TAGS |
| 2374 | |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2375 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2376 | /** |
| 2377 | * @brief Decode the MIME type tag |
| 2378 | * |
| 2379 | * @param[in,out] pDecodedItem The item to decode. |
| 2380 | * |
| 2381 | * Handle the text and binary MIME type tags. Slightly too complicated |
| 2382 | * f or ProcessTaggedString() because the RFC 7049 MIME type was |
| 2383 | * incorreclty text-only. |
| 2384 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2385 | static QCBORError |
| 2386 | QCBOR_Private_DecodeMIME(QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2387 | { |
| 2388 | if(pDecodedItem->uDataType == QCBOR_TYPE_TEXT_STRING) { |
| 2389 | pDecodedItem->uDataType = QCBOR_TYPE_MIME; |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 2390 | } else if(pDecodedItem->uDataType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2391 | pDecodedItem->uDataType = QCBOR_TYPE_BINARY_MIME; |
| 2392 | } else { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2393 | /* It's the arrays and maps that are unrecoverable because |
| 2394 | * they are not consumed here. Since this is just an error |
| 2395 | * condition, no extra code is added here to make the error |
| 2396 | * recoverable for non-arrays and maps like strings. */ |
| 2397 | return QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2398 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2399 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2400 | return QCBOR_SUCCESS; |
| 2401 | } |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2402 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2403 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2404 | /** |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2405 | * Table of CBOR tags whose content is either a text string or a byte |
| 2406 | * string. The table maps the CBOR tag to the QCBOR type. The high-bit |
| 2407 | * of uQCBORtype indicates the content should be a byte string rather |
| 2408 | * than a text string |
| 2409 | */ |
| 2410 | struct StringTagMapEntry { |
| 2411 | uint16_t uTagNumber; |
| 2412 | uint8_t uQCBORtype; |
| 2413 | }; |
| 2414 | |
| 2415 | #define IS_BYTE_STRING_BIT 0x80 |
| 2416 | #define QCBOR_TYPE_MASK ~IS_BYTE_STRING_BIT |
| 2417 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2418 | static const struct StringTagMapEntry QCBOR_Private_StringTagMap[] = { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2419 | {CBOR_TAG_DATE_STRING, QCBOR_TYPE_DATE_STRING}, |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2420 | {CBOR_TAG_DAYS_STRING, QCBOR_TYPE_DAYS_STRING}, |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2421 | {CBOR_TAG_POS_BIGNUM, QCBOR_TYPE_POSBIGNUM | IS_BYTE_STRING_BIT}, |
| 2422 | {CBOR_TAG_NEG_BIGNUM, QCBOR_TYPE_NEGBIGNUM | IS_BYTE_STRING_BIT}, |
| 2423 | {CBOR_TAG_CBOR, QBCOR_TYPE_WRAPPED_CBOR | IS_BYTE_STRING_BIT}, |
| 2424 | {CBOR_TAG_URI, QCBOR_TYPE_URI}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2425 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2426 | {CBOR_TAG_B64URL, QCBOR_TYPE_BASE64URL}, |
| 2427 | {CBOR_TAG_B64, QCBOR_TYPE_BASE64}, |
| 2428 | {CBOR_TAG_REGEX, QCBOR_TYPE_REGEX}, |
| 2429 | {CBOR_TAG_BIN_UUID, QCBOR_TYPE_UUID | IS_BYTE_STRING_BIT}, |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2430 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2431 | {CBOR_TAG_CBOR_SEQUENCE, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE | IS_BYTE_STRING_BIT}, |
| 2432 | {CBOR_TAG_INVALID16, QCBOR_TYPE_NONE} |
| 2433 | }; |
| 2434 | |
| 2435 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2436 | /** |
| 2437 | * @brief Process standard CBOR tags whose content is a string |
| 2438 | * |
| 2439 | * @param[in] uTag The tag. |
| 2440 | * @param[in,out] pDecodedItem The data item. |
| 2441 | * |
| 2442 | * @returns This returns QCBOR_SUCCESS if the tag was procssed, |
| 2443 | * \ref QCBOR_ERR_UNSUPPORTED if the tag was not processed and |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2444 | * \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] | 2445 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2446 | * Process the CBOR tags that whose content is a byte string or a text |
| 2447 | * string and for which the string is just passed on to the caller. |
| 2448 | * |
| 2449 | * This maps the CBOR tag to the QCBOR type and checks the content |
| 2450 | * type. Nothing more. It may not be the most important |
Laurence Lundblade | c02e13e | 2020-12-06 05:45:41 -0800 | [diff] [blame] | 2451 | * functionality, but it part of implementing as much of RFC 8949 as |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2452 | * possible. |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2453 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2454 | static QCBORError |
| 2455 | QCBOR_Private_ProcessTaggedString(uint16_t uTag, QCBORItem *pDecodedItem) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2456 | { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2457 | /* This only works on tags that were not mapped; no need for other yet */ |
| 2458 | if(uTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 2459 | return QCBOR_ERR_UNSUPPORTED; |
| 2460 | } |
| 2461 | |
| 2462 | unsigned uIndex; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2463 | for(uIndex = 0; QCBOR_Private_StringTagMap[uIndex].uTagNumber != CBOR_TAG_INVALID16; uIndex++) { |
| 2464 | if(QCBOR_Private_StringTagMap[uIndex].uTagNumber == uTag) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2465 | break; |
| 2466 | } |
| 2467 | } |
| 2468 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2469 | const uint8_t uQCBORType = QCBOR_Private_StringTagMap[uIndex].uQCBORtype; |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2470 | if(uQCBORType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2471 | /* repurpose this error to mean not handled here */ |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2472 | return QCBOR_ERR_UNSUPPORTED; |
| 2473 | } |
| 2474 | |
| 2475 | uint8_t uExpectedType = QCBOR_TYPE_TEXT_STRING; |
| 2476 | if(uQCBORType & IS_BYTE_STRING_BIT) { |
| 2477 | uExpectedType = QCBOR_TYPE_BYTE_STRING; |
| 2478 | } |
| 2479 | |
| 2480 | if(pDecodedItem->uDataType != uExpectedType) { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 2481 | /* It's the arrays and maps that are unrecoverable because |
| 2482 | * they are not consumed here. Since this is just an error |
| 2483 | * condition, no extra code is added here to make the error |
| 2484 | * recoverable for non-arrays and maps like strings. */ |
| 2485 | return QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2486 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2487 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2488 | pDecodedItem->uDataType = (uint8_t)(uQCBORType & QCBOR_TYPE_MASK); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2489 | return QCBOR_SUCCESS; |
| 2490 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2491 | #endif /* QCBOR_DISABLE_TAGS */ |
| 2492 | |
| 2493 | |
| 2494 | #ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 2495 | /** |
| 2496 | * @brief Figures out data type for exponent mantissa tags. |
| 2497 | * |
| 2498 | * @param[in] uTagToProcess Either @ref CBOR_TAG_DECIMAL_FRACTION or |
| 2499 | * @ref CBOR_TAG_BIG_FLOAT. |
| 2500 | * @param[in] pDecodedItem Item being decoded. |
| 2501 | * |
| 2502 | * @returns One of the 6 values between \ref QCBOR_TYPE_DECIMAL_FRACTION |
| 2503 | * and @ref QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM. |
| 2504 | * |
| 2505 | * Does mapping between a CBOR tag number and a QCBOR type. with a |
| 2506 | * little bit of logic and arithmatic. |
| 2507 | * |
| 2508 | * Used in serveral contexts. Does the work where sometimes the data |
| 2509 | * item is explicitly tagged and sometimes not. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2510 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2511 | static uint8_t |
| 2512 | QCBOR_Private_ExpMantissaDataType(const uint16_t uTagToProcess, |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 2513 | const QCBORItem *pDecodedItem) |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2514 | { |
| 2515 | uint8_t uBase = uTagToProcess == CBOR_TAG_DECIMAL_FRACTION ? |
| 2516 | QCBOR_TYPE_DECIMAL_FRACTION : |
| 2517 | QCBOR_TYPE_BIGFLOAT; |
| 2518 | if(pDecodedItem->uDataType != QCBOR_TYPE_INT64) { |
| 2519 | uBase = (uint8_t)(uBase + pDecodedItem->uDataType - QCBOR_TYPE_POSBIGNUM + 1); |
| 2520 | } |
| 2521 | return uBase; |
| 2522 | } |
| 2523 | #endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2524 | |
| 2525 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2526 | /** |
| 2527 | * @brief Decode tag content for select tags (decoding layer 1). |
| 2528 | * |
| 2529 | * @param[in] pMe The decode context. |
| 2530 | * @param[out] pDecodedItem The decoded item. |
| 2531 | * |
| 2532 | * @return Decoding error code. |
| 2533 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2534 | * CBOR tag numbers for the item were decoded in GetNext_TaggedItem(), |
| 2535 | * but the whole tag was not decoded. Here, the whole tags (tag number |
| 2536 | * and tag content) that are supported by QCBOR are decoded. This is a |
| 2537 | * quick pass through for items that are not tags. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2538 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2539 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2540 | QCBORDecode_Private_GetNextTagContent(QCBORDecodeContext *pMe, |
| 2541 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2542 | { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2543 | QCBORError uReturn; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2544 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2545 | uReturn = QCBORDecode_Private_GetNextMapOrArray(pMe, pDecodedItem); |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2546 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2547 | goto Done; |
| 2548 | } |
| 2549 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2550 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2551 | /* When there are no tag numbers for the item, this exits first |
| 2552 | * thing and effectively does nothing. |
| 2553 | * |
| 2554 | * This loops over all the tag numbers accumulated for this item |
| 2555 | * trying to decode and interpret them. This stops at the end of |
| 2556 | * the list or at the first tag number that can't be interpreted by |
| 2557 | * this code. This is effectively a recursive processing of the |
| 2558 | * tags number list that handles nested tags. |
| 2559 | */ |
| 2560 | while(1) { |
| 2561 | /* Don't bother to unmap tags via QCBORITem.uTags since this |
| 2562 | * code only works on tags less than QCBOR_LAST_UNMAPPED_TAG. |
| 2563 | */ |
| 2564 | const uint16_t uTagToProcess = pDecodedItem->uTags[0]; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2565 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2566 | if(uTagToProcess == CBOR_TAG_INVALID16) { |
| 2567 | /* Hit the end of the tag list. A successful exit. */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2568 | break; |
| 2569 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2570 | } else if(uTagToProcess == CBOR_TAG_DATE_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2571 | uReturn = QCBOR_Private_DecodeDateEpoch(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2572 | |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2573 | } else if(uTagToProcess == CBOR_TAG_DAYS_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2574 | uReturn = QCBOR_Private_DecodeDaysEpoch(pDecodedItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 2575 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2576 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2577 | } else if(uTagToProcess == CBOR_TAG_DECIMAL_FRACTION || |
| 2578 | uTagToProcess == CBOR_TAG_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2579 | uReturn = QCBORDecode_Private_ExpMantissa(pMe, pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2580 | /* --- Which is it, decimal fraction or a bigfloat? --- */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2581 | pDecodedItem->uDataType = QCBOR_Private_ExpMantissaDataType(uTagToProcess, pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2582 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 2583 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2584 | #ifndef QCBOR_DISABLE_UNCOMMON_TAGS |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2585 | } else if(uTagToProcess == CBOR_TAG_MIME || |
| 2586 | uTagToProcess == CBOR_TAG_BINARY_MIME) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2587 | uReturn = QCBOR_Private_DecodeMIME(pDecodedItem); |
Laurence Lundblade | 24bd7e1 | 2021-01-09 00:05:11 -0800 | [diff] [blame] | 2588 | #endif /* QCBOR_DISABLE_UNCOMMON_TAGS */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 2589 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2590 | } else { |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 2591 | /* 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] | 2592 | uReturn = QCBOR_Private_ProcessTaggedString(pDecodedItem->uTags[0], pDecodedItem); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2593 | |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2594 | if(uReturn == QCBOR_ERR_UNSUPPORTED) { |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 2595 | /* It wasn't a passthrough byte/text string tag so it is |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2596 | * an unknown tag. This is the exit from the loop on the |
| 2597 | * first unknown tag. It is a successful exit. |
| 2598 | */ |
| 2599 | uReturn = QCBOR_SUCCESS; |
| 2600 | break; |
| 2601 | } |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2602 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2603 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2604 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2605 | /* Error exit from the loop */ |
| 2606 | break; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2607 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2608 | |
| 2609 | /* A tag was successfully processed, shift it out of the list of |
| 2610 | * tags returned. This is the loop increment. |
| 2611 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2612 | QCBOR_Private_ShiftTags(pDecodedItem); |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2613 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2614 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2615 | |
| 2616 | Done: |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2617 | return uReturn; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 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 | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2623 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2624 | QCBORError |
| 2625 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2626 | { |
| 2627 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2628 | uErr = QCBORDecode_Private_GetNextTagContent(pMe, pDecodedItem); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2629 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2630 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2631 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2632 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2633 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | |
| 2637 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2638 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2639 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2640 | QCBORError |
| 2641 | QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2642 | { |
| 2643 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2644 | const UsefulInputBuf Save = pMe->InBuf; |
| 2645 | |
| 2646 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2647 | |
| 2648 | pMe->nesting = SaveNesting; |
| 2649 | pMe->InBuf = Save; |
| 2650 | |
| 2651 | return uErr; |
| 2652 | } |
| 2653 | |
| 2654 | |
| 2655 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2656 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2657 | */ |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2658 | void |
| 2659 | QCBORDecode_VPeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2660 | { |
| 2661 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2662 | return; |
| 2663 | } |
| 2664 | |
| 2665 | pMe->uLastError = (uint8_t)QCBORDecode_PeekNext(pMe, pDecodedItem); |
| 2666 | } |
| 2667 | |
| 2668 | |
| 2669 | /* |
| 2670 | * Public function, see header qcbor/qcbor_decode.h file |
| 2671 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2672 | void |
| 2673 | QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2674 | { |
| 2675 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2676 | return; |
| 2677 | } |
| 2678 | |
| 2679 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2680 | } |
| 2681 | |
| 2682 | |
| 2683 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2684 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2685 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2686 | QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2687 | QCBORDecode_GetNextWithTags(QCBORDecodeContext *pMe, |
| 2688 | QCBORItem *pDecodedItem, |
| 2689 | QCBORTagListOut *pTags) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2690 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2691 | #ifndef QCBOR_DISABLE_TAGS |
| 2692 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2693 | QCBORError uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2694 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2695 | uReturn = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2696 | if(uReturn != QCBOR_SUCCESS) { |
| 2697 | return uReturn; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | if(pTags != NULL) { |
| 2701 | pTags->uNumUsed = 0; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2702 | /* Reverse the order because pTags is reverse of QCBORItem.uTags. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2703 | for(int nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >=0; nTagIndex--) { |
| 2704 | if(pDecodedItem->uTags[nTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2705 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2706 | } |
| 2707 | if(pTags->uNumUsed >= pTags->uNumAllocated) { |
| 2708 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 2709 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2710 | pTags->puTags[pTags->uNumUsed] = QCBORDecode_Private_UnMapTagNumber(pMe,pDecodedItem->uTags[nTagIndex]); |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2711 | pTags->uNumUsed++; |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | return QCBOR_SUCCESS; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2716 | |
| 2717 | #else /* QCBOR_DISABLE_TAGS */ |
| 2718 | (void)pMe; |
| 2719 | (void)pDecodedItem; |
| 2720 | (void)pTags; |
| 2721 | return QCBOR_ERR_TAGS_DISABLED; |
| 2722 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2723 | } |
| 2724 | |
| 2725 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2726 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2727 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 0fb2f64 | 2018-10-11 19:33:35 +0530 | [diff] [blame] | 2728 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2729 | bool |
| 2730 | QCBORDecode_IsTagged(QCBORDecodeContext *pMe, |
| 2731 | const QCBORItem *pItem, |
| 2732 | uint64_t uTag) |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2733 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2734 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 2735 | for(unsigned uTagIndex = 0; uTagIndex < QCBOR_MAX_TAGS_PER_ITEM; uTagIndex++) { |
| 2736 | if(pItem->uTags[uTagIndex] == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2737 | break; |
| 2738 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2739 | if(QCBORDecode_Private_UnMapTagNumber(pMe, pItem->uTags[uTagIndex]) == uTag) { |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2740 | return true; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2741 | } |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2742 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2743 | #else /* QCBOR_TAGS_DISABLED */ |
| 2744 | (void)pMe; |
| 2745 | (void)pItem; |
| 2746 | (void)uTag; |
| 2747 | #endif /* QCBOR_TAGS_DISABLED */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2748 | |
Laurence Lundblade | f71e162 | 2020-08-06 18:52:13 -0700 | [diff] [blame] | 2749 | return false; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2750 | } |
| 2751 | |
| 2752 | |
| 2753 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2754 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2755 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2756 | QCBORError |
| 2757 | QCBORDecode_PartialFinish(QCBORDecodeContext *pMe, size_t *puConsumed) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2758 | { |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2759 | if(puConsumed != NULL) { |
| 2760 | *puConsumed = pMe->InBuf.cursor; |
| 2761 | } |
| 2762 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2763 | QCBORError uReturn = pMe->uLastError; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2764 | |
| 2765 | if(uReturn != QCBOR_SUCCESS) { |
| 2766 | goto Done; |
| 2767 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2768 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2769 | /* Error out if all the maps/arrays are not closed out */ |
| 2770 | if(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2771 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2772 | goto Done; |
| 2773 | } |
| 2774 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2775 | /* Error out if not all the bytes are consumed */ |
| 2776 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2777 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2778 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2779 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2780 | Done: |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2781 | return uReturn; |
| 2782 | } |
| 2783 | |
| 2784 | |
| 2785 | /* |
| 2786 | * Public function, see header qcbor/qcbor_decode.h file |
| 2787 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2788 | QCBORError |
| 2789 | QCBORDecode_Finish(QCBORDecodeContext *pMe) |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2790 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2791 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2792 | /* Call the destructor for the string allocator if there is one. |
| 2793 | * Always called, even if there are errors; always have to clean up. |
| 2794 | */ |
| 2795 | StringAllocator_Destruct(&(pMe->StringAllocator)); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2796 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2797 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2798 | return QCBORDecode_PartialFinish(pMe, NULL); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2799 | } |
| 2800 | |
| 2801 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2802 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2803 | * Public function, see header qcbor/qcbor_decode.h file |
| 2804 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2805 | // Improvement: make these inline? TODO: |
| 2806 | uint64_t |
| 2807 | QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2808 | const QCBORItem *pItem, |
| 2809 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2810 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2811 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2812 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2813 | return CBOR_TAG_INVALID64; |
| 2814 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2815 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2816 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2817 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2818 | return QCBORDecode_Private_UnMapTagNumber(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2819 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2820 | #else /* QCBOR_DISABLE_TAGS */ |
| 2821 | (void)pMe; |
| 2822 | (void)pItem; |
| 2823 | (void)uIndex; |
| 2824 | |
| 2825 | return CBOR_TAG_INVALID64; |
| 2826 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2827 | } |
| 2828 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2829 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2830 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2831 | * Public function, see header qcbor/qcbor_decode.h file |
| 2832 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2833 | uint64_t |
| 2834 | QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2835 | uint32_t uIndex) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2836 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2837 | #ifndef QCBOR_DISABLE_TAGS |
| 2838 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2839 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2840 | return CBOR_TAG_INVALID64; |
| 2841 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2842 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2843 | return CBOR_TAG_INVALID64; |
| 2844 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2845 | return QCBORDecode_Private_UnMapTagNumber(pMe, pMe->uLastTags[uIndex]); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2846 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2847 | #else /* QCBOR_DISABLE_TAGS */ |
| 2848 | (void)pMe; |
| 2849 | (void)uIndex; |
| 2850 | |
| 2851 | return CBOR_TAG_INVALID64; |
| 2852 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2853 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2854 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2855 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2856 | |
| 2857 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2858 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2859 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2860 | /* =========================================================================== |
| 2861 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2862 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2863 | This implements a simple sting allocator for indefinite-length |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2864 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2865 | implements the function type QCBORStringAllocate and allows easy |
| 2866 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2867 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2868 | This particular allocator is built-in for convenience. The caller |
| 2869 | can implement their own. All of this following code will get |
| 2870 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2871 | |
| 2872 | This is a very primitive memory allocator. It does not track |
| 2873 | individual allocations, only a high-water mark. A free or |
| 2874 | reallocation must be of the last chunk allocated. |
| 2875 | |
| 2876 | The size of the pool and offset to free memory are packed into the |
| 2877 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2878 | the decode context. Since the address of the pool may not be |
| 2879 | aligned, they have to be packed and unpacked as if they were |
| 2880 | serialized data of the wire or such. |
| 2881 | |
| 2882 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2883 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2884 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2885 | |
| 2886 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2887 | static int |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2888 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2889 | { |
| 2890 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2891 | UsefulInputBuf UIB; |
| 2892 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2893 | // Just assume the size here. It was checked during SetUp so |
| 2894 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2895 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2896 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2897 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2898 | return UsefulInputBuf_GetError(&UIB); |
| 2899 | } |
| 2900 | |
| 2901 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2902 | static int |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2903 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2904 | { |
| 2905 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2906 | // length check performed here is useful. |
| 2907 | UsefulOutBuf UOB; |
| 2908 | |
| 2909 | UsefulOutBuf_Init(&UOB, Pool); |
| 2910 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2911 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2912 | return UsefulOutBuf_GetError(&UOB); |
| 2913 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2914 | |
| 2915 | |
| 2916 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2917 | Internal function for an allocation, reallocation free and destuct. |
| 2918 | |
| 2919 | Having only one function rather than one each per mode saves space in |
| 2920 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2921 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2922 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2923 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2924 | static UsefulBuf |
| 2925 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2926 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2927 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2928 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2929 | uint32_t uPoolSize; |
| 2930 | uint32_t uFreeOffset; |
| 2931 | |
| 2932 | if(uNewSize > UINT32_MAX) { |
| 2933 | // This allocator is only good up to 4GB. This check should |
| 2934 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2935 | goto Done; |
| 2936 | } |
| 2937 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2938 | |
| 2939 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2940 | goto Done; |
| 2941 | } |
| 2942 | |
| 2943 | if(uNewSize) { |
| 2944 | if(pMem) { |
| 2945 | // REALLOCATION MODE |
| 2946 | // Calculate pointer to the end of the memory pool. It is |
| 2947 | // assumed that pPool + uPoolSize won't wrap around by |
| 2948 | // assuming the caller won't pass a pool buffer in that is |
| 2949 | // not in legitimate memory space. |
| 2950 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2951 | |
| 2952 | // Check that the pointer for reallocation is in the range of the |
| 2953 | // pool. This also makes sure that pointer math further down |
| 2954 | // doesn't wrap under or over. |
| 2955 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2956 | // Offset to start of chunk for reallocation. This won't |
| 2957 | // wrap under because of check that pMem >= pPool. Cast |
| 2958 | // is safe because the pool is always less than UINT32_MAX |
| 2959 | // because of check in QCBORDecode_SetMemPool(). |
| 2960 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2961 | |
| 2962 | // Check to see if the allocation will fit. uPoolSize - |
| 2963 | // uMemOffset will not wrap under because of check that |
| 2964 | // pMem is in the range of the uPoolSize by check above. |
| 2965 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2966 | ReturnValue.ptr = pMem; |
| 2967 | ReturnValue.len = uNewSize; |
| 2968 | |
| 2969 | // Addition won't wrap around over because uNewSize was |
| 2970 | // checked to be sure it is less than the pool size. |
| 2971 | uFreeOffset = uMemOffset + uNewSize32; |
| 2972 | } |
| 2973 | } |
| 2974 | } else { |
| 2975 | // ALLOCATION MODE |
| 2976 | // uPoolSize - uFreeOffset will not underflow because this |
| 2977 | // pool implementation makes sure uFreeOffset is always |
| 2978 | // smaller than uPoolSize through this check here and |
| 2979 | // reallocation case. |
| 2980 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2981 | ReturnValue.len = uNewSize; |
| 2982 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2983 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2984 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2985 | } |
| 2986 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2987 | if(pMem) { |
| 2988 | // FREE MODE |
| 2989 | // Cast is safe because of limit on pool size in |
| 2990 | // QCBORDecode_SetMemPool() |
| 2991 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2992 | } else { |
| 2993 | // DESTRUCT MODE |
| 2994 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2995 | } |
| 2996 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2997 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2998 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2999 | MemPool_Pack(Pool, uFreeOffset); |
| 3000 | |
| 3001 | Done: |
| 3002 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3003 | } |
| 3004 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3005 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3006 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3007 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3008 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3009 | QCBORError |
| 3010 | QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 3011 | UsefulBuf Pool, |
| 3012 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3013 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3014 | // The pool size and free mem offset are packed into the beginning |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3015 | // of the pool memory. This compile time check makes sure the |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3016 | // constant in the header is correct. This check should optimize |
| 3017 | // down to nothing. |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3018 | #ifdef _MSC_VER |
| 3019 | #pragma warning(push) |
| 3020 | #pragma warning(disable:4127) // conditional expression is constant |
| 3021 | #endif |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3022 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3023 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3024 | } |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3025 | #ifdef _MSC_VER |
| 3026 | #pragma warning(pop) |
| 3027 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3028 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3029 | // The pool size and free offset packed in to the beginning of pool |
| 3030 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 3031 | // machines. |
| 3032 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3033 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3034 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3035 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3036 | // This checks that the pool buffer given is big enough. |
| 3037 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3038 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3039 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3040 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 3041 | QCBORDecode_SetUpAllocator(pMe, MemPool_Function, Pool.ptr, bAllStrings); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3042 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3043 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3044 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 3045 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3046 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3047 | |
| 3048 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3049 | static void |
| 3050 | QCBORDecode_Private_CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3051 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3052 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3053 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3054 | #else |
| 3055 | (void)pMe; |
| 3056 | (void)pItem; |
| 3057 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3058 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3059 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3060 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3061 | /** |
| 3062 | * @brief Consume an entire map or array including its contents. |
| 3063 | * |
| 3064 | * @param[in] pMe The decoder context. |
| 3065 | * @param[in] pItemToConsume The array/map whose contents are to be |
| 3066 | * consumed. |
| 3067 | * @param[out] puNextNestLevel The next nesting level after the item was |
| 3068 | * fully consumed. |
| 3069 | * |
| 3070 | * This may be called when @c pItemToConsume is not an array or |
| 3071 | * map. In that case, this is just a pass through for @c puNextNestLevel |
| 3072 | * since there is nothing to do. |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3073 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3074 | static QCBORError |
| 3075 | QCBORDecode_Private_ConsumeItem(QCBORDecodeContext *pMe, |
| 3076 | const QCBORItem *pItemToConsume, |
| 3077 | uint8_t *puNextNestLevel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3078 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3079 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3080 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3081 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3082 | /* 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] | 3083 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 3084 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3085 | if(QCBORItem_IsMapOrArray(*pItemToConsume) && !bIsEmpty) { |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3086 | /* 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] | 3087 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3088 | /* This works for definite- and indefinite-length maps and |
| 3089 | * arrays by using the nesting level |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3090 | */ |
| 3091 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3092 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3093 | if(QCBORDecode_IsUnrecoverableError(uReturn) || |
| 3094 | uReturn == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3095 | goto Done; |
| 3096 | } |
| 3097 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3098 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3099 | *puNextNestLevel = Item.uNextNestLevel; |
| 3100 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3101 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3102 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3103 | } else { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3104 | /* pItemToConsume is not a map or array. Just pass the nesting |
| 3105 | * level through. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3106 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 3107 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3108 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3109 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3110 | |
| 3111 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3112 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3113 | } |
| 3114 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3115 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3116 | /* |
| 3117 | * Public function, see header qcbor/qcbor_decode.h file |
| 3118 | */ |
| 3119 | void |
| 3120 | QCBORDecode_VGetNextConsume(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3121 | { |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3122 | QCBORDecode_VGetNext(pMe, pDecodedItem); |
| 3123 | |
| 3124 | if(pMe->uLastError == QCBOR_SUCCESS) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3125 | 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] | 3126 | &pDecodedItem->uNextNestLevel); |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3127 | } |
| 3128 | } |
| 3129 | |
| 3130 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3131 | /** |
| 3132 | * @brief Rewind cursor to start as if map or array were just entered. |
| 3133 | * |
| 3134 | * @param[in] pMe The decoding context |
| 3135 | * |
| 3136 | * This affects the nesting tracking and the UsefulInputBuf. |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3137 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3138 | static void |
| 3139 | QCBORDecode_Private_RewindMapOrArray(QCBORDecodeContext *pMe) |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3140 | { |
| 3141 | /* Reset nesting tracking to the deepest bounded level */ |
| 3142 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 3143 | |
| 3144 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3145 | |
| 3146 | /* Reposition traversal cursor to the start of the map/array */ |
| 3147 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 3148 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
| 3149 | } |
| 3150 | |
| 3151 | |
| 3152 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3153 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3154 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3155 | void |
| 3156 | QCBORDecode_Rewind(QCBORDecodeContext *pMe) |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3157 | { |
| 3158 | if(pMe->nesting.pCurrentBounded != NULL) { |
| 3159 | /* In a bounded map, array or bstr-wrapped CBOR */ |
| 3160 | |
| 3161 | if(DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
| 3162 | /* In bstr-wrapped CBOR. */ |
| 3163 | |
| 3164 | /* Reposition traversal cursor to start of wrapping byte string */ |
| 3165 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 3166 | pMe->nesting.pCurrentBounded->u.bs.uBstrStartOffset); |
| 3167 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 3168 | |
| 3169 | } else { |
| 3170 | /* In a map or array */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3171 | QCBORDecode_Private_RewindMapOrArray(pMe); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | } else { |
| 3175 | /* Not in anything bounded */ |
| 3176 | |
| 3177 | /* Reposition traversal cursor to the start of input CBOR */ |
| 3178 | UsefulInputBuf_Seek(&(pMe->InBuf), 0ULL); |
| 3179 | |
| 3180 | /* Reset nesting tracking to beginning of input. */ |
| 3181 | DecodeNesting_Init(&(pMe->nesting)); |
| 3182 | } |
| 3183 | |
| 3184 | pMe->uLastError = QCBOR_SUCCESS; |
| 3185 | } |
| 3186 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3187 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3188 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3189 | |
| 3190 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3191 | /** |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3192 | * @brief Search a map for a set of items. |
| 3193 | * |
| 3194 | * @param[in] pMe The decode context to search. |
| 3195 | * @param[in,out] pItemArray The items to search for and the items found. |
| 3196 | * @param[out] puOffset Byte offset of last item matched. |
| 3197 | * @param[in] pCBContext Context for the not-found item call back. |
| 3198 | * @param[in] pfCallback Function to call on items not matched in pItemArray. |
| 3199 | * |
| 3200 | * @retval QCBOR_ERR_NOT_ENTERED Trying to search without entering a map. |
| 3201 | * |
| 3202 | * @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 3203 | * were found for one of the labels being |
| 3204 | * search for. This duplicate detection is |
| 3205 | * only performed for items in pItemArray, |
| 3206 | * not every item in the map. |
| 3207 | * |
| 3208 | * @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 3209 | * wrong for the matchd label. |
| 3210 | * |
| 3211 | * @retval Also errors returned by QCBORDecode_GetNext(). |
| 3212 | * |
| 3213 | * On input, \c pItemArray contains a list of labels and data types of |
| 3214 | * items to be found. |
| 3215 | * |
| 3216 | * On output, the fully retrieved items are filled in with values and |
| 3217 | * such. The label was matched, so it never changes. |
| 3218 | * |
| 3219 | * If an item was not found, its data type is set to @ref QCBOR_TYPE_NONE. |
| 3220 | * |
| 3221 | * 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] | 3222 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3223 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3224 | QCBORDecode_Private_MapSearch(QCBORDecodeContext *pMe, |
| 3225 | QCBORItem *pItemArray, |
| 3226 | size_t *puOffset, |
| 3227 | void *pCBContext, |
| 3228 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3229 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3230 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3231 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 3232 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3233 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3234 | uReturn = pMe->uLastError; |
| 3235 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3236 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3237 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3238 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3239 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 3240 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 3241 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3242 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 3243 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3244 | } |
| 3245 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3246 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 3247 | // It is an empty bounded array or map |
| 3248 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 3249 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3250 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3251 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3252 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3253 | // Nothing is ever found in an empty array or map. All items |
| 3254 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3255 | uReturn = QCBOR_SUCCESS; |
| 3256 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3257 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3258 | } |
| 3259 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3260 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3261 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 3262 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3263 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3264 | QCBORDecode_Private_RewindMapOrArray(pMe); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3265 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3266 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3267 | Loop over all the items in the map or array. Each item |
| 3268 | could be a map or array, but label matching is only at |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 3269 | the main level. This handles definite- and indefinite- |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3270 | length maps and arrays. The only reason this is ever |
| 3271 | called on arrays is to find their end position. |
| 3272 | |
| 3273 | This will always run over all items in order to do |
| 3274 | duplicate detection. |
| 3275 | |
| 3276 | This will exit with failure if it encounters an |
| 3277 | unrecoverable error, but continue on for recoverable |
| 3278 | errors. |
| 3279 | |
| 3280 | If a recoverable error occurs on a matched item, then |
| 3281 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3282 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 3283 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Máté Tóth-Pál | 2f2aa5f | 2021-05-17 21:35:09 +0200 | [diff] [blame] | 3284 | uint8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3285 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3286 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3287 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3288 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3289 | /* Get the item */ |
| 3290 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3291 | QCBORError uResult = QCBORDecode_Private_GetNextTagContent(pMe, &Item); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3292 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3293 | /* Unrecoverable error so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3294 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3295 | goto Done; |
| 3296 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3297 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3298 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3299 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3300 | goto Done; |
| 3301 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3302 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3303 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3304 | bool bMatched = false; |
| 3305 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3306 | if(QCBORItem_MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3307 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3308 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 3309 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3310 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3311 | } |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3312 | if(uResult != QCBOR_SUCCESS) { |
| 3313 | /* The label matches, but the data item is in error */ |
| 3314 | uReturn = uResult; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3315 | goto Done; |
| 3316 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3317 | if(!QCBORItem_MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3318 | /* The data item is not of the type(s) requested */ |
| 3319 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3320 | goto Done; |
| 3321 | } |
| 3322 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3323 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3324 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3325 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3326 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3327 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3328 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3329 | bMatched = true; |
| 3330 | } |
| 3331 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3332 | |
| 3333 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3334 | if(!bMatched && pfCallback != NULL) { |
| 3335 | /* |
| 3336 | Call the callback on unmatched labels. |
| 3337 | (It is tempting to do duplicate detection here, but that would |
| 3338 | require dynamic memory allocation because the number of labels |
| 3339 | that might be encountered is unbounded.) |
| 3340 | */ |
| 3341 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 3342 | if(uReturn != QCBOR_SUCCESS) { |
| 3343 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3344 | } |
| 3345 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3346 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3347 | /* |
| 3348 | Consume the item whether matched or not. This |
| 3349 | does the work of traversing maps and array and |
| 3350 | everything in them. In this loop only the |
| 3351 | items at the current nesting level are examined |
| 3352 | to match the labels. |
| 3353 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3354 | uReturn = QCBORDecode_Private_ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3355 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3356 | goto Done; |
| 3357 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3358 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3359 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3360 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3361 | |
| 3362 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3363 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3364 | // Check here makes sure that this won't accidentally be |
| 3365 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3366 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3367 | // Cast to uint32_t to possibly address cases where SIZE_MAX < UINT32_MAX |
| 3368 | if((uint32_t)uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3369 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3370 | goto Done; |
| 3371 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3372 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 3373 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3374 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3375 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3376 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 3377 | |
| 3378 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3379 | /* 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] | 3380 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3381 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3382 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 3383 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3384 | } |
| 3385 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3386 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3387 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3388 | } |
| 3389 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3390 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3391 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3392 | * Public function, see header qcbor/qcbor_decode.h file |
| 3393 | */ |
| 3394 | void |
| 3395 | QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 3396 | int64_t nLabel, |
| 3397 | uint8_t uQcborType, |
| 3398 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3399 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3400 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3401 | return; |
| 3402 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3403 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3404 | QCBORItem OneItemSeach[2]; |
| 3405 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3406 | OneItemSeach[0].label.int64 = nLabel; |
| 3407 | OneItemSeach[0].uDataType = uQcborType; |
| 3408 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3409 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3410 | QCBORError uReturn = QCBORDecode_Private_MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3411 | |
| 3412 | *pItem = OneItemSeach[0]; |
| 3413 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3414 | if(uReturn != QCBOR_SUCCESS) { |
| 3415 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3416 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3417 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3418 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3419 | } |
| 3420 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3421 | Done: |
| 3422 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3426 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3427 | * Public function, see header qcbor/qcbor_decode.h file |
| 3428 | */ |
| 3429 | void |
| 3430 | QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 3431 | const char *szLabel, |
| 3432 | uint8_t uQcborType, |
| 3433 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3434 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3435 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3436 | return; |
| 3437 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3438 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3439 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3440 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3441 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3442 | OneItemSeach[0].uDataType = uQcborType; |
| 3443 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3444 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3445 | QCBORError uReturn = QCBORDecode_Private_MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3446 | if(uReturn != QCBOR_SUCCESS) { |
| 3447 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3448 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3449 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3450 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3451 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3452 | } |
| 3453 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3454 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3455 | |
| 3456 | Done: |
| 3457 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3461 | /** |
| 3462 | * @brief Is a QCBOR_TYPE in the type list? |
| 3463 | * |
| 3464 | * @param[in] uDataType Type to check for. |
| 3465 | * @param[in] puTypeList List to check. |
| 3466 | * |
| 3467 | * @retval QCBOR_SUCCESS If in the list. |
| 3468 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Not in the list. |
| 3469 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3470 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3471 | QCBOR_Private_CheckTypeList(const int uDataType, |
| 3472 | const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3473 | { |
| 3474 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3475 | if(uDataType == puTypeList[i]) { /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3476 | return QCBOR_SUCCESS; |
| 3477 | } |
| 3478 | } |
| 3479 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3480 | } |
| 3481 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 3482 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3483 | /** |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3484 | * Match a tag/type specification against the type of the item. |
| 3485 | * |
| 3486 | * @param[in] TagSpec Specification for matching tags. |
| 3487 | * @param[in] pItem The item to check. |
| 3488 | * |
| 3489 | * @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 3490 | * @retval QCBOR_ERR_UNEXPECTED_TYPE \c uDataType is not allowed by @c TagSpec |
| 3491 | * |
| 3492 | * This checks the item data type of untagged items as well as of |
| 3493 | * tagged items against a specification to see if decoding should |
| 3494 | * proceed. |
| 3495 | * |
| 3496 | * This relies on the automatic tag decoding done by QCBOR that turns |
| 3497 | * tag numbers into particular QCBOR_TYPEs so there is no actual |
| 3498 | * comparsion of tag numbers, just of QCBOR_TYPEs. |
| 3499 | * |
| 3500 | * This checks the data item type as possibly representing the tag |
| 3501 | * number or as the tag content type. |
| 3502 | * |
| 3503 | * If QCBOR_DISABLE_TAGS is #defined, this primarily checks the item |
| 3504 | * data type against the allowed tag content types. It will also error out |
| 3505 | * if the caller tries to require a tag because there is no way that can |
| 3506 | * ever be fulfilled. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3507 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3508 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3509 | QCBOR_Private_CheckTagRequirement(const QCBOR_Private_TagSpec TagSpec, |
| 3510 | const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3511 | { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3512 | const int nItemType = pItem->uDataType; /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3513 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 3514 | |
| 3515 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3516 | /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3517 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 3518 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 3519 | /* There are tags that QCBOR couldn't process on this item and |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3520 | * the caller has told us there should not be. |
| 3521 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3522 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3523 | } |
| 3524 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3525 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3526 | /* Must match the tag number and only the tag */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3527 | return QCBOR_Private_CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3528 | } |
| 3529 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3530 | QCBORError uReturn = QCBOR_Private_CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3531 | if(uReturn == QCBOR_SUCCESS) { |
| 3532 | return QCBOR_SUCCESS; |
| 3533 | } |
| 3534 | |
| 3535 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 3536 | /* Must match the content type and only the content type. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3537 | * There was no match just above so it is a fail. */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3538 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3539 | } |
| 3540 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3541 | /* QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG: If here it can match either the tag or the content |
| 3542 | * and it hasn't matched the content, so the end |
| 3543 | * result is whether it matches the tag. This is |
| 3544 | * the tag optional case that the CBOR standard discourages. |
| 3545 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3546 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3547 | return QCBOR_Private_CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3548 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3549 | #else /* QCBOR_DISABLE_TAGS */ |
| 3550 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 3551 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3552 | } |
| 3553 | |
| 3554 | return CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 3555 | |
| 3556 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3557 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3558 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3559 | // This could be semi-private if need be |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3560 | // TODO: decide what to do |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3561 | |
| 3562 | /** |
| 3563 | * @brief Get an item by label to match a tag specification. |
| 3564 | * |
| 3565 | * @param[in] pMe The decode context. |
| 3566 | * @param[in] nLabel The label to search map for. |
| 3567 | * @param[in] TagSpec The tag number specification to match. |
| 3568 | * @param[out] pItem The item found. |
| 3569 | * |
| 3570 | * This finds the item with the given label in currently open |
| 3571 | * map. Then checks that its tag number and types matches the tag |
| 3572 | * specification. If not, an error is set in the decode context. |
| 3573 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3574 | static void |
| 3575 | QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 3576 | const int64_t nLabel, |
| 3577 | const QCBOR_Private_TagSpec TagSpec, |
| 3578 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3579 | { |
| 3580 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 3581 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3582 | return; |
| 3583 | } |
| 3584 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3585 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3586 | } |
| 3587 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3588 | |
| 3589 | // This could be semi-private if need be |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3590 | /** |
| 3591 | * @brief Get an item by label to match a tag specification. |
| 3592 | * |
| 3593 | * @param[in] pMe The decode context. |
| 3594 | * @param[in] szLabel The label to search map for. |
| 3595 | * @param[in] TagSpec The tag number specification to match. |
| 3596 | * @param[out] pItem The item found. |
| 3597 | * |
| 3598 | * This finds the item with the given label in currently open |
| 3599 | * map. Then checks that its tag number and types matches the tag |
| 3600 | * specification. If not, an error is set in the decode context. |
| 3601 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3602 | static void |
| 3603 | QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 3604 | const char *szLabel, |
| 3605 | const QCBOR_Private_TagSpec TagSpec, |
| 3606 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3607 | { |
| 3608 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 3609 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3610 | return; |
| 3611 | } |
| 3612 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3613 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3614 | } |
| 3615 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3616 | |
| 3617 | /** |
| 3618 | * @brief Semi-private to get an string by label to match a tag specification. |
| 3619 | * |
| 3620 | * @param[in] pMe The decode context. |
| 3621 | * @param[in] nLabel The label to search map for. |
| 3622 | * @param[in] TagSpec The tag number specification to match. |
| 3623 | * @param[out] pString The string found. |
| 3624 | * |
| 3625 | * This finds the string with the given label in currently open |
| 3626 | * map. Then checks that its tag number and types matches the tag |
| 3627 | * specification. If not, an error is set in the decode context. |
| 3628 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3629 | void |
| 3630 | QCBORDecode_Private_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3631 | const int64_t nLabel, |
| 3632 | const QCBOR_Private_TagSpec TagSpec, |
| 3633 | UsefulBufC *pString) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3634 | { |
| 3635 | QCBORItem Item; |
| 3636 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 3637 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3638 | *pString = Item.val.string; |
| 3639 | } |
| 3640 | } |
| 3641 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3642 | |
| 3643 | /** |
| 3644 | * @brief Semi-private to get an string by label to match a tag specification. |
| 3645 | * |
| 3646 | * @param[in] pMe The decode context. |
| 3647 | * @param[in] szLabel The label to search map for. |
| 3648 | * @param[in] TagSpec The tag number specification to match. |
| 3649 | * @param[out] pString The string found. |
| 3650 | * |
| 3651 | * This finds the string with the given label in currently open |
| 3652 | * map. Then checks that its tag number and types matches the tag |
| 3653 | * specification. If not, an error is set in the decode context. |
| 3654 | */void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3655 | QCBORDecode_Private_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3656 | const char * szLabel, |
| 3657 | const QCBOR_Private_TagSpec TagSpec, |
| 3658 | UsefulBufC *pString) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3659 | { |
| 3660 | QCBORItem Item; |
| 3661 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 3662 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3663 | *pString = Item.val.string; |
| 3664 | } |
| 3665 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3666 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3667 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3668 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3669 | * Public function, see header qcbor/qcbor_decode.h file |
| 3670 | */ |
| 3671 | void |
| 3672 | QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3673 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3674 | QCBORError uErr = QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, NULL, NULL); |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3675 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3676 | } |
| 3677 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3678 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3679 | * Public function, see header qcbor/qcbor_decode.h file |
| 3680 | */ |
| 3681 | void |
| 3682 | QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 3683 | QCBORItem *pItemList, |
| 3684 | void *pCallbackCtx, |
| 3685 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3686 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3687 | QCBORError uErr = QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3688 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3689 | } |
| 3690 | |
| 3691 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3692 | /** |
| 3693 | * @brief Search for a map/array by label and enter it |
| 3694 | * |
| 3695 | * @param[in] pMe The decode context. |
| 3696 | * @param[in] pSearch The map/array to search for. |
| 3697 | * |
| 3698 | * @c pSearch is expected to contain one item of type map or array |
| 3699 | * with the label specified. The current bounded map will be searched for |
| 3700 | * this and if found will be entered. |
| 3701 | * |
| 3702 | * If the label is not found, or the item found is not a map or array, |
| 3703 | * the error state is set. |
| 3704 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3705 | static void |
| 3706 | QCBORDecode_Private_SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3707 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3708 | // The first item in pSearch is the one that is to be |
| 3709 | // entered. It should be the only one filled in. Any other |
| 3710 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3711 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3712 | return; |
| 3713 | } |
| 3714 | |
| 3715 | size_t uOffset; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3716 | pMe->uLastError = (uint8_t)QCBORDecode_Private_MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3717 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3718 | return; |
| 3719 | } |
| 3720 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3721 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3722 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3723 | return; |
| 3724 | } |
| 3725 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3726 | |
| 3727 | /* The map or array was found. Now enter it. |
| 3728 | * |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3729 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 3730 | * next item for the pre-order traversal cursor to be the map/array |
| 3731 | * found by MapSearch(). The next few lines of code force the |
| 3732 | * cursor to that. |
| 3733 | * |
| 3734 | * There is no need to retain the old cursor because |
| 3735 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 3736 | * beginning of the map/array being entered. |
| 3737 | * |
| 3738 | * The cursor is forced by: 1) setting the input buffer position to |
| 3739 | * the item offset found by MapSearch(), 2) setting the map/array |
| 3740 | * counter to the total in the map/array, 3) setting the nesting |
| 3741 | * level. Setting the map/array counter to the total is not |
| 3742 | * strictly correct, but this is OK because this cursor only needs |
| 3743 | * to be used to get one item and MapSearch() has already found it |
| 3744 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3745 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3746 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3747 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3748 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3749 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3750 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3751 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3752 | QCBORDecode_Private_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3756 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3757 | * Public function, see header qcbor/qcbor_decode.h file |
| 3758 | */ |
| 3759 | void |
| 3760 | QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3761 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3762 | QCBORItem OneItemSeach[2]; |
| 3763 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3764 | OneItemSeach[0].label.int64 = nLabel; |
| 3765 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3766 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3767 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3768 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3769 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3773 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3774 | * Public function, see header qcbor/qcbor_decode.h file |
| 3775 | */ |
| 3776 | void |
| 3777 | QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3778 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3779 | QCBORItem OneItemSeach[2]; |
| 3780 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3781 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3782 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3783 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3784 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3785 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3786 | } |
| 3787 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3788 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3789 | * Public function, see header qcbor/qcbor_decode.h file |
| 3790 | */ |
| 3791 | void |
| 3792 | QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3793 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3794 | QCBORItem OneItemSeach[2]; |
| 3795 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3796 | OneItemSeach[0].label.int64 = nLabel; |
| 3797 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3798 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3799 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3800 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3801 | } |
| 3802 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3803 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3804 | * Public function, see header qcbor/qcbor_decode.h file |
| 3805 | */ |
| 3806 | void |
| 3807 | QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3808 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3809 | QCBORItem OneItemSeach[2]; |
| 3810 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3811 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3812 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3813 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3814 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3815 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3819 | /** |
| 3820 | * @brief Semi-private to do the the work for EnterMap() and EnterArray(). |
| 3821 | * |
| 3822 | * @param[in] pMe The decode context |
| 3823 | * @param[in] uType QCBOR_TYPE_MAP or QCBOR_TYPE_ARRAY. |
| 3824 | * @param[out] pItem The data item for the map or array entered. |
| 3825 | * |
| 3826 | * The next item in the traversal must be a map or array. This |
| 3827 | * consumes that item and does the book keeping to enter the map or |
| 3828 | * array. |
| 3829 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3830 | void |
| 3831 | QCBORDecode_Private_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, |
| 3832 | const uint8_t uType, |
| 3833 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3834 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3835 | QCBORError uErr; |
| 3836 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3837 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3838 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3839 | // Already in error state; do nothing. |
| 3840 | return; |
| 3841 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3842 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3843 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3844 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3845 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3846 | if(uErr != QCBOR_SUCCESS) { |
| 3847 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3848 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3849 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3850 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3851 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3852 | } |
| 3853 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3854 | QCBORDecode_Private_CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3855 | |
| 3856 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3857 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3858 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3859 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3860 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3861 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3862 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3863 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3864 | // Special case to increment nesting level for zero-length maps |
| 3865 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3866 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3867 | } |
| 3868 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3869 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3870 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3871 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3872 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3873 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3874 | if(pItem != NULL) { |
| 3875 | *pItem = Item; |
| 3876 | } |
| 3877 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3878 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3879 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3880 | } |
| 3881 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3882 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3883 | /** |
| 3884 | * @brief Exit a bounded map, array or bstr (semi-private). |
| 3885 | * |
| 3886 | * @param[in] pMe Decode context. |
| 3887 | * @param[in] uEndOffset The input buffer offset of the end of item exited. |
| 3888 | * |
| 3889 | * @returns QCBOR_SUCCESS or an error code. |
| 3890 | * |
| 3891 | * This is the common work for exiting a level that is a bounded map, |
| 3892 | * array or bstr wrapped CBOR. |
| 3893 | * |
| 3894 | * One chunk of work is to set up the pre-order traversal so it is at |
| 3895 | * the item just after the bounded map, array or bstr that is being |
| 3896 | * exited. This is somewhat complex. |
| 3897 | * |
| 3898 | * The other work is to level-up the bounded mode to next higest |
| 3899 | * bounded mode or the top level if there isn't one. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3900 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3901 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3902 | QCBORDecode_Private_ExitBoundedLevel(QCBORDecodeContext *pMe, |
| 3903 | const uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3904 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3905 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3906 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3907 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3908 | * First the pre-order-traversal byte offset is positioned to the |
| 3909 | * item just after the bounded mode item that was just consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3910 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3911 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3912 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3913 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3914 | * Next, set the current nesting level to one above the bounded |
| 3915 | * level that was just exited. |
| 3916 | * |
| 3917 | * DecodeNesting_CheckBoundedType() is always called before this |
| 3918 | * and makes sure pCurrentBounded is valid. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3919 | */ |
| 3920 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3921 | |
| 3922 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3923 | * This does the complex work of leveling up the pre-order |
| 3924 | * traversal when the end of a map or array or another bounded |
| 3925 | * level is reached. It may do nothing, or ascend all the way to |
| 3926 | * the top level. |
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 | uErr = QCBORDecode_Private_NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3929 | if(uErr != QCBOR_SUCCESS) { |
| 3930 | goto Done; |
| 3931 | } |
| 3932 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3933 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3934 | * This makes the next highest bounded level the current bounded |
| 3935 | * level. If there is no next highest level, then no bounded mode |
| 3936 | * is in effect. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3937 | */ |
| 3938 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3939 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3940 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3941 | |
| 3942 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3943 | return uErr; |
| 3944 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3945 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3946 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3947 | /** |
| 3948 | * @brief Get started exiting a map or array (semi-private) |
| 3949 | * |
| 3950 | * @param[in] pMe The decode context |
| 3951 | * @param[in] uType QCBOR_TYPE_ARRAY or QCBOR_TYPE_MAP |
| 3952 | * |
| 3953 | * This does some work for map and array exiting (but not |
| 3954 | * bstr exiting). Then QCBORDecode_Private_ExitBoundedLevel() |
| 3955 | * is called to do the rest. |
| 3956 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3957 | void |
| 3958 | QCBORDecode_Private_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, |
| 3959 | const uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3960 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3961 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3962 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3963 | return; |
| 3964 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3965 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3966 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3967 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3968 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3969 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3970 | goto Done; |
| 3971 | } |
| 3972 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3973 | /* |
| 3974 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3975 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3976 | from previous map search, then do a dummy search. |
| 3977 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3978 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3979 | QCBORItem Dummy; |
| 3980 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3981 | uErr = QCBORDecode_Private_MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3982 | if(uErr != QCBOR_SUCCESS) { |
| 3983 | goto Done; |
| 3984 | } |
| 3985 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3986 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3987 | uErr = QCBORDecode_Private_ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3988 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3989 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3990 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3991 | } |
| 3992 | |
| 3993 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 3994 | /** |
| 3995 | * @brief The main work of entering some byte-string wrapped CBOR. |
| 3996 | * |
| 3997 | * @param[in] pMe The decode context. |
| 3998 | * @param[in] pItem The byte string item. |
| 3999 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX |
| 4000 | * @param[out] pBstr Pointer and length of byte string entered. |
| 4001 | * |
| 4002 | * This is called once the byte string item has been decoded to do all |
| 4003 | * the book keeping work for descending a nesting level into the |
| 4004 | * nested CBOR. |
| 4005 | * |
| 4006 | * See QCBORDecode_EnterBstrWrapped() for details on uTagRequirement. |
| 4007 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4008 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4009 | QCBORDecode_Private_EnterBstrWrapped(QCBORDecodeContext *pMe, |
| 4010 | const QCBORItem *pItem, |
| 4011 | const uint8_t uTagRequirement, |
| 4012 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4013 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4014 | if(pBstr) { |
| 4015 | *pBstr = NULLUsefulBufC; |
| 4016 | } |
| 4017 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4018 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4019 | /* Already in error state; do nothing. */ |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4020 | return pMe->uLastError; |
| 4021 | } |
| 4022 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4023 | QCBORError uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4024 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4025 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4026 | { |
| 4027 | uTagRequirement, |
| 4028 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 4029 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 4030 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4031 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4032 | uError = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4033 | if(uError != QCBOR_SUCCESS) { |
| 4034 | goto Done; |
| 4035 | } |
| 4036 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4037 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4038 | /* Reverse the decrement done by GetNext() for the bstr so the |
| 4039 | * increment in QCBORDecode_NestLevelAscender() called by |
| 4040 | * ExitBoundedLevel() will work right. |
| 4041 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4042 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 4043 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4044 | |
| 4045 | if(pBstr) { |
| 4046 | *pBstr = pItem->val.string; |
| 4047 | } |
| 4048 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4049 | /* This saves the current length of the UsefulInputBuf and then |
| 4050 | * narrows the UsefulInputBuf to start and length of the wrapped |
| 4051 | * CBOR that is being entered. |
| 4052 | * |
| 4053 | * Most of these calls are simple inline accessors so this doesn't |
| 4054 | * amount to much code. |
| 4055 | */ |
| 4056 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 4057 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4058 | /* This check makes the cast of uPreviousLength to uint32_t below safe. */ |
| 4059 | if(uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4060 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 4061 | goto Done; |
| 4062 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4063 | |
| 4064 | const size_t uStartOfBstr = UsefulInputBuf_PointerToOffset(&(pMe->InBuf), |
| 4065 | pItem->val.string.ptr); |
| 4066 | /* This check makes the cast of uStartOfBstr to uint32_t below safe. */ |
| 4067 | if(uStartOfBstr == SIZE_MAX || uStartOfBstr > QCBOR_MAX_DECODE_INPUT_SIZE) { |
| 4068 | /* This should never happen because pItem->val.string.ptr should |
| 4069 | * always be valid since it was just returned. |
| 4070 | */ |
| 4071 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
| 4072 | goto Done; |
| 4073 | } |
| 4074 | |
| 4075 | const size_t uEndOfBstr = uStartOfBstr + pItem->val.string.len; |
| 4076 | |
| 4077 | UsefulInputBuf_Seek(&(pMe->InBuf), uStartOfBstr); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 4078 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4079 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4080 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 4081 | (uint32_t)uPreviousLength, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4082 | (uint32_t)uStartOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4083 | Done: |
| 4084 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4085 | } |
| 4086 | |
| 4087 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4088 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4089 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4090 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4091 | void |
| 4092 | QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
| 4093 | const uint8_t uTagRequirement, |
| 4094 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4095 | { |
| 4096 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4097 | // Already in error state; do nothing. |
| 4098 | return; |
| 4099 | } |
| 4100 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4101 | /* Get the data item that is the byte string being entered */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4102 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4103 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4104 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4105 | return; |
| 4106 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4107 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4108 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4109 | &Item, |
| 4110 | uTagRequirement, |
| 4111 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4112 | } |
| 4113 | |
| 4114 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4115 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4116 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4117 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4118 | void |
| 4119 | QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
| 4120 | const int64_t nLabel, |
| 4121 | const uint8_t uTagRequirement, |
| 4122 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4123 | { |
| 4124 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4125 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4126 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4127 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4128 | &Item, |
| 4129 | uTagRequirement, |
| 4130 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4131 | } |
| 4132 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4133 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4134 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4135 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4136 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4137 | void |
| 4138 | QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
| 4139 | const char *szLabel, |
| 4140 | const uint8_t uTagRequirement, |
| 4141 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4142 | { |
| 4143 | QCBORItem Item; |
| 4144 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 4145 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4146 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4147 | &Item, |
| 4148 | uTagRequirement, |
| 4149 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4150 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4151 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4152 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4153 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4154 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4155 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4156 | void |
| 4157 | QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4158 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4159 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4160 | // Already in error state; do nothing. |
| 4161 | return; |
| 4162 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4163 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 4164 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4165 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4166 | return; |
| 4167 | } |
| 4168 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4169 | const uint32_t uEndOfBstr = (uint32_t)UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
| 4170 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4171 | /* |
| 4172 | Reset the length of the UsefulInputBuf to what it was before |
| 4173 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4174 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 4175 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4176 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4177 | |
| 4178 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4179 | QCBORError uErr = QCBORDecode_Private_ExitBoundedLevel(pMe, uEndOfBstr); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4180 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4181 | } |
| 4182 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4183 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4184 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4185 | /** |
| 4186 | * @brief Process simple type true and false, a boolean |
| 4187 | * |
| 4188 | * @param[in] pMe The decode context. |
| 4189 | * @param[in] pItem The item with either true or false. |
| 4190 | * @param[out] pBool The boolean value output. |
| 4191 | * |
| 4192 | * Sets the internal error if the item isn't a true or a false. Also |
| 4193 | * records any tag numbers as the tag numbers of the last item. |
| 4194 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4195 | static void |
| 4196 | QCBORDecode_Private_ProcessBool(QCBORDecodeContext *pMe, |
| 4197 | const QCBORItem *pItem, |
| 4198 | bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4199 | { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4200 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4201 | /* Already in error state, do nothing */ |
| 4202 | return; |
| 4203 | } |
| 4204 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4205 | switch(pItem->uDataType) { |
| 4206 | case QCBOR_TYPE_TRUE: |
| 4207 | *pBool = true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4208 | break; |
| 4209 | |
| 4210 | case QCBOR_TYPE_FALSE: |
| 4211 | *pBool = false; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4212 | break; |
| 4213 | |
| 4214 | default: |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4215 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4216 | break; |
| 4217 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4218 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4219 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4220 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4221 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4222 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4223 | * Public function, see header qcbor/qcbor_decode.h file |
| 4224 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4225 | void |
| 4226 | QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4227 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4228 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4229 | /* Already in error state, do nothing */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4230 | return; |
| 4231 | } |
| 4232 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4233 | QCBORItem Item; |
| 4234 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4235 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4236 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4237 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4238 | } |
| 4239 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4240 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4241 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4242 | * Public function, see header qcbor/qcbor_decode.h file |
| 4243 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4244 | void |
| 4245 | QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, |
| 4246 | const int64_t nLabel, |
| 4247 | bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4248 | { |
| 4249 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4250 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4251 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4252 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4253 | } |
| 4254 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4255 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4256 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4257 | * Public function, see header qcbor/qcbor_decode.h file |
| 4258 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4259 | void |
| 4260 | QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, |
| 4261 | const char *szLabel, |
| 4262 | bool *pValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4263 | { |
| 4264 | QCBORItem Item; |
| 4265 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 4266 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4267 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | |
| 4271 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4272 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4273 | /** |
| 4274 | * @brief Common processing for an epoch date. |
| 4275 | * |
| 4276 | * @param[in] pMe The decode context. |
| 4277 | * @param[in] pItem The item with the date. |
| 4278 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4279 | * @param[out] pnTime The returned date. |
| 4280 | * |
| 4281 | * Common processing for the date tag. Mostly make sure the tag |
| 4282 | * content is correct and copy forward any further other tag numbers. |
| 4283 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4284 | static void |
| 4285 | QCBORDecode_Private_ProcessEpochDate(QCBORDecodeContext *pMe, |
| 4286 | QCBORItem *pItem, |
| 4287 | const uint8_t uTagRequirement, |
| 4288 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4289 | { |
| 4290 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4291 | // Already in error state, do nothing |
| 4292 | return; |
| 4293 | } |
| 4294 | |
| 4295 | QCBORError uErr; |
| 4296 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4297 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4298 | { |
| 4299 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4300 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4301 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4302 | }; |
| 4303 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4304 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4305 | if(uErr != QCBOR_SUCCESS) { |
| 4306 | goto Done; |
| 4307 | } |
| 4308 | |
| 4309 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4310 | uErr = QCBOR_Private_DecodeDateEpoch(pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4311 | if(uErr != QCBOR_SUCCESS) { |
| 4312 | goto Done; |
| 4313 | } |
| 4314 | } |
| 4315 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4316 | // Save the tags in the last item's tags in the decode context |
| 4317 | // for QCBORDecode_GetNthTagOfLast() |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4318 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4319 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4320 | *pnTime = pItem->val.epochDate.nSeconds; |
| 4321 | |
| 4322 | Done: |
| 4323 | pMe->uLastError = (uint8_t)uErr; |
| 4324 | } |
| 4325 | |
| 4326 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4327 | |
| 4328 | /* |
| 4329 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 4330 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4331 | void |
| 4332 | QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
| 4333 | uint8_t uTagRequirement, |
| 4334 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4335 | { |
| 4336 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4337 | // Already in error state, do nothing |
| 4338 | return; |
| 4339 | } |
| 4340 | |
| 4341 | QCBORItem Item; |
| 4342 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4343 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4344 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4348 | /* |
| 4349 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 4350 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4351 | void |
| 4352 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 4353 | int64_t nLabel, |
| 4354 | uint8_t uTagRequirement, |
| 4355 | int64_t *pnTime) |
| 4356 | { |
| 4357 | QCBORItem Item; |
| 4358 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4359 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4360 | } |
| 4361 | |
| 4362 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4363 | /* |
| 4364 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 4365 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4366 | void |
| 4367 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 4368 | const char *szLabel, |
| 4369 | uint8_t uTagRequirement, |
| 4370 | int64_t *pnTime) |
| 4371 | { |
| 4372 | QCBORItem Item; |
| 4373 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4374 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | |
| 4378 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4379 | /** |
| 4380 | * @brief Common processing for an epoch date. |
| 4381 | * |
| 4382 | * @param[in] pMe The decode context. |
| 4383 | * @param[in] pItem The item with the date. |
| 4384 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4385 | * @param[out] pnDays The returned day count. |
| 4386 | * |
| 4387 | * Common processing for the RFC 8943 day-count tag. Mostly make sure |
| 4388 | * the tag content is correct and copy forward any further other tag |
| 4389 | * numbers. |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4390 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4391 | static void |
| 4392 | QCBORDecode_Private_ProcessEpochDays(QCBORDecodeContext *pMe, |
| 4393 | QCBORItem *pItem, |
| 4394 | uint8_t uTagRequirement, |
| 4395 | int64_t *pnDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4396 | { |
| 4397 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4398 | /* Already in error state, do nothing */ |
| 4399 | return; |
| 4400 | } |
| 4401 | |
| 4402 | QCBORError uErr; |
| 4403 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4404 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4405 | { |
| 4406 | uTagRequirement, |
| 4407 | {QCBOR_TYPE_DAYS_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4408 | {QCBOR_TYPE_INT64, QCBOR_TYPE_UINT64, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 4409 | }; |
| 4410 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4411 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4412 | if(uErr != QCBOR_SUCCESS) { |
| 4413 | goto Done; |
| 4414 | } |
| 4415 | |
| 4416 | if(pItem->uDataType != QCBOR_TYPE_DAYS_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4417 | uErr = QCBOR_Private_DecodeDaysEpoch(pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4418 | if(uErr != QCBOR_SUCCESS) { |
| 4419 | goto Done; |
| 4420 | } |
| 4421 | } |
| 4422 | |
| 4423 | /* Save the tags in the last item's tags in the decode context |
| 4424 | * for QCBORDecode_GetNthTagOfLast() |
| 4425 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4426 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4427 | |
| 4428 | *pnDays = pItem->val.epochDays; |
| 4429 | |
| 4430 | Done: |
| 4431 | pMe->uLastError = (uint8_t)uErr; |
| 4432 | } |
| 4433 | |
| 4434 | |
| 4435 | /* |
| 4436 | * Public function, see header qcbor/qcbor_decode.h |
| 4437 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4438 | void |
| 4439 | QCBORDecode_GetEpochDays(QCBORDecodeContext *pMe, |
| 4440 | uint8_t uTagRequirement, |
| 4441 | int64_t *pnDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4442 | { |
| 4443 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4444 | /* Already in error state, do nothing */ |
| 4445 | return; |
| 4446 | } |
| 4447 | |
| 4448 | QCBORItem Item; |
| 4449 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4450 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4451 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4452 | } |
| 4453 | |
| 4454 | |
| 4455 | /* |
| 4456 | * Public function, see header qcbor/qcbor_decode.h |
| 4457 | */ |
| 4458 | void |
| 4459 | QCBORDecode_GetEpochDaysInMapN(QCBORDecodeContext *pMe, |
| 4460 | int64_t nLabel, |
| 4461 | uint8_t uTagRequirement, |
| 4462 | int64_t *pnDays) |
| 4463 | { |
| 4464 | QCBORItem Item; |
| 4465 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4466 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4467 | } |
| 4468 | |
| 4469 | |
| 4470 | /* |
| 4471 | * Public function, see header qcbor/qcbor_decode.h |
| 4472 | */ |
| 4473 | void |
| 4474 | QCBORDecode_GetEpochDaysInMapSZ(QCBORDecodeContext *pMe, |
| 4475 | const char *szLabel, |
| 4476 | uint8_t uTagRequirement, |
| 4477 | int64_t *pnDays) |
| 4478 | { |
| 4479 | QCBORItem Item; |
| 4480 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4481 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4482 | } |
| 4483 | |
| 4484 | |
| 4485 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4486 | /* |
| 4487 | * @brief Get a string that matches the type/tag specification. |
| 4488 | */ |
| 4489 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4490 | QCBORDecode_Private_GetTaggedString(QCBORDecodeContext *pMe, |
| 4491 | const QCBOR_Private_TagSpec TagSpec, |
| 4492 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4493 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4494 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4495 | /* Already in error state, do nothing */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4496 | return; |
| 4497 | } |
| 4498 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4499 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4500 | QCBORItem Item; |
| 4501 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4502 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 4503 | if(uError != QCBOR_SUCCESS) { |
| 4504 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4505 | return; |
| 4506 | } |
| 4507 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4508 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4509 | |
| 4510 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4511 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 4512 | } else { |
| 4513 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4514 | } |
| 4515 | } |
| 4516 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4517 | |
| 4518 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4519 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4520 | /** |
| 4521 | * @brief Common processing for a big number tag. |
| 4522 | * |
| 4523 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4524 | * @param[in] pItem The item with the date. |
| 4525 | * @param[out] pValue The returned big number |
| 4526 | * @param[out] pbIsNegative The returned sign of the big number. |
| 4527 | * |
| 4528 | * Common processing for the big number tag. Mostly make sure |
| 4529 | * the tag content is correct and copy forward any further other tag |
| 4530 | * numbers. |
| 4531 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4532 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4533 | QCBOR_Private_ProcessBigNum(const uint8_t uTagRequirement, |
| 4534 | const QCBORItem *pItem, |
| 4535 | UsefulBufC *pValue, |
| 4536 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4537 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4538 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4539 | { |
| 4540 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4541 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4542 | {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] | 4543 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4544 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4545 | QCBORError uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4546 | if(uErr != QCBOR_SUCCESS) { |
| 4547 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4548 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4549 | |
| 4550 | *pValue = pItem->val.string; |
| 4551 | |
| 4552 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 4553 | *pbIsNegative = false; |
| 4554 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 4555 | *pbIsNegative = true; |
| 4556 | } |
| 4557 | |
| 4558 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4559 | } |
| 4560 | |
| 4561 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4562 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4563 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4564 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4565 | void |
| 4566 | QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 4567 | const uint8_t uTagRequirement, |
| 4568 | UsefulBufC *pValue, |
| 4569 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4570 | { |
| 4571 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4572 | // Already in error state, do nothing |
| 4573 | return; |
| 4574 | } |
| 4575 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4576 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4577 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4578 | if(uError != QCBOR_SUCCESS) { |
| 4579 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4580 | return; |
| 4581 | } |
| 4582 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4583 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4584 | &Item, |
| 4585 | pValue, |
| 4586 | pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4587 | } |
| 4588 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4589 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4590 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4591 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
| 4592 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4593 | void |
| 4594 | QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 4595 | const int64_t nLabel, |
| 4596 | const uint8_t uTagRequirement, |
| 4597 | UsefulBufC *pValue, |
| 4598 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4599 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4600 | QCBORItem Item; |
| 4601 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4602 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4603 | return; |
| 4604 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4605 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4606 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4607 | &Item, |
| 4608 | pValue, |
| 4609 | pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4610 | } |
| 4611 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4612 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4613 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4614 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4615 | */ |
| 4616 | void |
| 4617 | QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 4618 | const char *szLabel, |
| 4619 | const uint8_t uTagRequirement, |
| 4620 | UsefulBufC *pValue, |
| 4621 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4622 | { |
| 4623 | QCBORItem Item; |
| 4624 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4625 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4626 | return; |
| 4627 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4628 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4629 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4630 | &Item, |
| 4631 | pValue, |
| 4632 | pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | |
| 4636 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4637 | /** |
| 4638 | * @brief Common processing for MIME tag (semi-private). |
| 4639 | * |
| 4640 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4641 | * @param[in] pItem The item with the date. |
| 4642 | * @param[out] pMessage The returned MIME message. |
| 4643 | * @param[out] pbIsTag257 If true, binary MIME, if not, text MIME. |
| 4644 | * |
| 4645 | * Common processing for the MIME tag. Mostly make sure the tag |
| 4646 | * content is correct and copy forward any further other tag |
| 4647 | * numbers. See QCBORDecode_GetMIMEMessage(). |
| 4648 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4649 | QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4650 | QCBORDecode_Private_GetMIME(const uint8_t uTagRequirement, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4651 | const QCBORItem *pItem, |
| 4652 | UsefulBufC *pMessage, |
| 4653 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4654 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4655 | const QCBOR_Private_TagSpec TagSpecText = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4656 | { |
| 4657 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4658 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4659 | {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] | 4660 | }; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4661 | const QCBOR_Private_TagSpec TagSpecBinary = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4662 | { |
| 4663 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4664 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4665 | {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] | 4666 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4667 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4668 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4669 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4670 | if(QCBOR_Private_CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4671 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4672 | if(pbIsTag257 != NULL) { |
| 4673 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4674 | } |
| 4675 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4676 | } else if(QCBOR_Private_CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4677 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4678 | if(pbIsTag257 != NULL) { |
| 4679 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4680 | } |
| 4681 | uReturn = QCBOR_SUCCESS; |
| 4682 | |
| 4683 | } else { |
| 4684 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 4685 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4686 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4687 | return uReturn; |
| 4688 | } |
| 4689 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4690 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 4691 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4692 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4693 | |
| 4694 | |
| 4695 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4696 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4697 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 4698 | /** |
| 4699 | * @brief Prototype for conversion of exponent and mantissa to unsigned integer. |
| 4700 | * |
| 4701 | * @param[in] uMantissa The mantissa. |
| 4702 | * @param[in] nExponent The exponent. |
| 4703 | * @param[out] puResult The resulting integer. |
| 4704 | * |
| 4705 | * Concrete implementations of this are for exponent base 10 and 2 supporting |
| 4706 | * decimal fractions and big floats. |
| 4707 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4708 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4709 | |
| 4710 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4711 | /** |
| 4712 | * @brief Base 10 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 4713 | * |
| 4714 | * @param[in] uMantissa The unsigned integer mantissa. |
| 4715 | * @param[in] nExponent The signed integer exponent. |
| 4716 | * @param[out] puResult Place to return the unsigned integer result. |
| 4717 | * |
| 4718 | * This computes: mantissa * 10 ^^ exponent as for a decimal fraction. The output is a 64-bit |
| 4719 | * unsigned integer. |
| 4720 | * |
| 4721 | * There are many inputs for which the result will not fit in the |
| 4722 | * 64-bit integer and \ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
| 4723 | * be returned. |
| 4724 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4725 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4726 | QCBOR_Private_Exponentitate10(const uint64_t uMantissa, |
| 4727 | int64_t nExponent, |
| 4728 | uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4729 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4730 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4731 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4732 | if(uResult != 0) { |
| 4733 | /* This loop will run a maximum of 19 times because |
| 4734 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 4735 | * exit with the overflow error |
| 4736 | */ |
| 4737 | for(; nExponent > 0; nExponent--) { |
| 4738 | if(uResult > UINT64_MAX / 10) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4739 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4740 | } |
| 4741 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4742 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4743 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4744 | for(; nExponent < 0; nExponent++) { |
| 4745 | uResult = uResult / 10; |
| 4746 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4747 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4748 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4749 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4750 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4751 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4752 | |
| 4753 | *puResult = uResult; |
| 4754 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4755 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4759 | /** |
| 4760 | * @brief Base 2 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 4761 | * |
| 4762 | * @param[in] uMantissa The unsigned integer mantissa. |
| 4763 | * @param[in] nExponent The signed integer exponent. |
| 4764 | * @param[out] puResult Place to return the unsigned integer result. |
| 4765 | * |
| 4766 | * This computes: mantissa * 2 ^^ exponent as for a big float. The |
| 4767 | * output is a 64-bit unsigned integer. |
| 4768 | * |
| 4769 | * There are many inputs for which the result will not fit in the |
| 4770 | * 64-bit integer and \ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
| 4771 | * be returned. |
| 4772 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4773 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4774 | QCBOR_Private_Exponentitate2(const uint64_t uMantissa, |
| 4775 | int64_t nExponent, |
| 4776 | uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4777 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4778 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4779 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4780 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4781 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4782 | /* This loop will run a maximum of 64 times because INT64_MAX < |
| 4783 | * 2^31. More than that will cause exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4784 | */ |
| 4785 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4786 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4787 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4788 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4789 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4790 | nExponent--; |
| 4791 | } |
| 4792 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4793 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4794 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4795 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4796 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4797 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4798 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4799 | } |
| 4800 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4801 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4802 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4803 | return QCBOR_SUCCESS; |
| 4804 | } |
| 4805 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4806 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4807 | /** |
| 4808 | * @brief Exponentiate a signed mantissa and signed exponent to produce a signed result. |
| 4809 | * |
| 4810 | * @param[in] nMantissa Signed integer mantissa. |
| 4811 | * @param[in] nExponent Signed integer exponent. |
| 4812 | * @param[out] pnResult Place to put the signed integer result. |
| 4813 | * @param[in] pfExp Exponentiation function. |
| 4814 | * |
| 4815 | * @returns Error code |
| 4816 | * |
| 4817 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4818 | * produces an unsigned result. This converts the mantissa from signed |
| 4819 | * and converts the result to signed. The exponentiation function is |
| 4820 | * 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] | 4821 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4822 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4823 | QCBOR_Private_ExponentiateNN(const int64_t nMantissa, |
| 4824 | const int64_t nExponent, |
| 4825 | int64_t *pnResult, |
| 4826 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4827 | { |
| 4828 | uint64_t uResult; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4829 | uint64_t uMantissa; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4830 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4831 | /* Take the absolute value and put it into an unsigned. */ |
| 4832 | if(nMantissa >= 0) { |
| 4833 | /* Positive case is straightforward */ |
| 4834 | uMantissa = (uint64_t)nMantissa; |
| 4835 | } else if(nMantissa != INT64_MIN) { |
| 4836 | /* The common negative case. See next. */ |
| 4837 | uMantissa = (uint64_t)-nMantissa; |
| 4838 | } else { |
| 4839 | /* int64_t and uint64_t are always two's complement per the |
| 4840 | * C standard (and since QCBOR uses these it only works with |
| 4841 | * two's complement, which is pretty much universal these |
| 4842 | * days). The range of a negative two's complement integer is |
| 4843 | * one more that than a positive, so the simple code above might |
| 4844 | * not work all the time because you can't simply negate the |
| 4845 | * value INT64_MIN because it can't be represented in an |
| 4846 | * int64_t. -INT64_MIN can however be represented in a |
| 4847 | * uint64_t. Some compilers seem to recognize this case for the |
| 4848 | * above code and put the correct value in uMantissa, however |
| 4849 | * they are not required to do this by the C standard. This next |
| 4850 | * line does however work for all compilers. |
| 4851 | * |
| 4852 | * This does assume two's complement where -INT64_MIN == |
| 4853 | * INT64_MAX + 1 (which wouldn't be true for one's complement or |
| 4854 | * sign and magnitude (but we know we're using two's complement |
| 4855 | * because int64_t requires it)). |
| 4856 | * |
| 4857 | * See these, particularly the detailed commentary: |
| 4858 | * https://stackoverflow.com/questions/54915742/does-c99-mandate-a-int64-t-type-be-available-always |
| 4859 | * https://stackoverflow.com/questions/37301078/is-negating-int-min-undefined-behaviour |
| 4860 | */ |
| 4861 | uMantissa = (uint64_t)INT64_MAX+1; |
| 4862 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4863 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4864 | /* Call the exponentiator passed for either base 2 or base 10. |
| 4865 | * Here is where most of the overflow errors are caught. */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4866 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 4867 | if(uReturn) { |
| 4868 | return uReturn; |
| 4869 | } |
| 4870 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4871 | /* Convert back to the sign of the original mantissa */ |
| 4872 | if(nMantissa >= 0) { |
| 4873 | if(uResult > INT64_MAX) { |
| 4874 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4875 | } |
| 4876 | *pnResult = (int64_t)uResult; |
| 4877 | } else { |
| 4878 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 4879 | * of INT64_MIN. This assumes two's compliment representation |
| 4880 | * where INT64_MIN is one increment farther from 0 than |
| 4881 | * INT64_MAX. Trying to write -INT64_MIN doesn't work to get |
| 4882 | * this because the compiler makes it an int64_t which can't |
| 4883 | * represent -INT64_MIN. Also see above. |
| 4884 | */ |
| 4885 | if(uResult > (uint64_t)INT64_MAX+1) { |
| 4886 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4887 | } |
| 4888 | *pnResult = -(int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4889 | } |
| 4890 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4891 | return QCBOR_SUCCESS; |
| 4892 | } |
| 4893 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4894 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4895 | /** |
| 4896 | * @brief Exponentiate an unsigned mantissa and signed exponent to produce an unsigned result. |
| 4897 | * |
| 4898 | * @param[in] nMantissa Signed integer mantissa. |
| 4899 | * @param[in] nExponent Signed integer exponent. |
| 4900 | * @param[out] puResult Place to put the signed integer result. |
| 4901 | * @param[in] pfExp Exponentiation function. |
| 4902 | * |
| 4903 | * @returns Error code |
| 4904 | * |
| 4905 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4906 | * produces an unsigned result. This errors out if the mantissa |
| 4907 | * is negative because the output is unsigned. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4908 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4909 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4910 | QCBOR_Private_ExponentitateNU(const int64_t nMantissa, |
| 4911 | const int64_t nExponent, |
| 4912 | uint64_t *puResult, |
| 4913 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4914 | { |
| 4915 | if(nMantissa < 0) { |
| 4916 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4917 | } |
| 4918 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4919 | /* Cast to unsigned is OK because of check for negative. |
| 4920 | * Cast to unsigned is OK because UINT64_MAX > INT64_MAX. |
| 4921 | * Exponentiation is straight forward |
| 4922 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4923 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 4924 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4925 | |
| 4926 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4927 | /** |
| 4928 | * @brief Exponentiate an usnigned mantissa and unsigned exponent to produce an unsigned result. |
| 4929 | * |
| 4930 | * @param[in] uMantissa Unsigned integer mantissa. |
| 4931 | * @param[in] nExponent Unsigned integer exponent. |
| 4932 | * @param[out] puResult Place to put the unsigned integer result. |
| 4933 | * @param[in] pfExp Exponentiation function. |
| 4934 | * |
| 4935 | * @returns Error code |
| 4936 | * |
| 4937 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4938 | * produces an unsigned result so this is just a wrapper that does |
| 4939 | * nothing (and is likely inlined). |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4940 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4941 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4942 | QCBOR_Private_ExponentitateUU(const uint64_t uMantissa, |
| 4943 | const int64_t nExponent, |
| 4944 | uint64_t *puResult, |
| 4945 | fExponentiator pfExp) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4946 | { |
| 4947 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 4948 | } |
| 4949 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4950 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4951 | |
| 4952 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4953 | |
| 4954 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4955 | /** |
| 4956 | * @brief Convert a CBOR big number to a uint64_t. |
| 4957 | * |
| 4958 | * @param[in] BigNum Bytes of the big number to convert. |
| 4959 | * @param[in] uMax Maximum value allowed for the result. |
| 4960 | * @param[out] pResult Place to put the unsigned integer result. |
| 4961 | * |
| 4962 | * @returns Error code |
| 4963 | * |
| 4964 | * Many values will overflow because a big num can represent a much |
| 4965 | * larger range than uint64_t. |
| 4966 | */ |
| 4967 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4968 | QCBOR_Private_ConvertBigNumToUnsigned(const UsefulBufC BigNum, |
| 4969 | const uint64_t uMax, |
| 4970 | uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4971 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4972 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4973 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4974 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4975 | const uint8_t *pByte = BigNum.ptr; |
| 4976 | size_t uLen = BigNum.len; |
| 4977 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4978 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4979 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4980 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4981 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4982 | } |
| 4983 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4984 | *pResult = uResult; |
| 4985 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4986 | } |
| 4987 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4988 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4989 | /** |
| 4990 | * @brief Convert a CBOR postive big number to a uint64_t. |
| 4991 | * |
| 4992 | * @param[in] BigNum Bytes of the big number to convert. |
| 4993 | * @param[out] pResult Place to put the unsigned integer result. |
| 4994 | * |
| 4995 | * @returns Error code |
| 4996 | * |
| 4997 | * Many values will overflow because a big num can represent a much |
| 4998 | * larger range than uint64_t. |
| 4999 | */ |
| 5000 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5001 | QCBOR_Private_ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, |
| 5002 | uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5003 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5004 | return QCBOR_Private_ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5005 | } |
| 5006 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5007 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5008 | /** |
| 5009 | * @brief Convert a CBOR positive big number to an int64_t. |
| 5010 | * |
| 5011 | * @param[in] BigNum Bytes of the big number to convert. |
| 5012 | * @param[out] pResult Place to put the signed integer result. |
| 5013 | * |
| 5014 | * @returns Error code |
| 5015 | * |
| 5016 | * Many values will overflow because a big num can represent a much |
| 5017 | * larger range than int64_t. |
| 5018 | */ |
| 5019 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5020 | QCBOR_Private_ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, |
| 5021 | int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5022 | { |
| 5023 | uint64_t uResult; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5024 | QCBORError uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, |
| 5025 | INT64_MAX, |
| 5026 | &uResult); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5027 | if(uError) { |
| 5028 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5029 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5030 | /* Cast is safe because ConvertBigNumToUnsigned limits to INT64_MAX */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5031 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5032 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5036 | /** |
| 5037 | * @brief Convert a CBOR negative big number to an int64_t. |
| 5038 | * |
| 5039 | * @param[in] BigNum Bytes of the big number to convert. |
| 5040 | * @param[out] pnResult Place to put the signed integer result. |
| 5041 | * |
| 5042 | * @returns Error code |
| 5043 | * |
| 5044 | * Many values will overflow because a big num can represent a much |
| 5045 | * larger range than int64_t. |
| 5046 | */ |
| 5047 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5048 | QCBOR_Private_ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, |
| 5049 | int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5050 | { |
| 5051 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5052 | /* The negative integer furthest from zero for a C int64_t is |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5053 | * INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 5054 | * negative number in CBOR is computed as -n - 1 where n is the |
| 5055 | * encoded integer, where n is what is in the variable BigNum. When |
| 5056 | * converting BigNum to a uint64_t, the maximum value is thus |
| 5057 | * INT64_MAX, so that when it -n - 1 is applied to it the result |
| 5058 | * will never be further from 0 than INT64_MIN. |
| 5059 | * |
| 5060 | * -n - 1 <= INT64_MIN. |
| 5061 | * -n - 1 <= -INT64_MAX - 1 |
| 5062 | * n <= INT64_MAX. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5063 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5064 | QCBORError uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, |
| 5065 | INT64_MAX, |
| 5066 | &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5067 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5068 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5069 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5070 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5071 | /* Now apply -n - 1. The cast is safe because |
| 5072 | * ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 5073 | * is the largest positive integer that an int64_t can |
| 5074 | * represent. */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5075 | *pnResult = -(int64_t)uResult - 1; |
| 5076 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5077 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5078 | } |
| 5079 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5080 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5081 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5082 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5083 | /** |
| 5084 | * @brief Convert integers and floats to an int64_t. |
| 5085 | * |
| 5086 | * @param[in] pItem The item to convert. |
| 5087 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5088 | * @param[out] pnValue The resulting converted value. |
| 5089 | * |
| 5090 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5091 | * in uConvertTypes. |
| 5092 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5093 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5094 | * or too small. |
| 5095 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5096 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5097 | QCBOR_Private_ConvertInt64(const QCBORItem *pItem, |
| 5098 | const uint32_t uConvertTypes, |
| 5099 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5100 | { |
| 5101 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5102 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5103 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5104 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5105 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5106 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 5107 | http://www.cplusplus.com/reference/cmath/llround/ |
| 5108 | */ |
| 5109 | // Not interested in FE_INEXACT |
| 5110 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5111 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 5112 | *pnValue = llround(pItem->val.dfnum); |
| 5113 | } else { |
| 5114 | *pnValue = lroundf(pItem->val.fnum); |
| 5115 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5116 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 5117 | // llround() shouldn't result in divide by zero, but catch |
| 5118 | // it here in case it unexpectedly does. Don't try to |
| 5119 | // distinguish between the various exceptions because it seems |
| 5120 | // they vary by CPU, compiler and OS. |
| 5121 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5122 | } |
| 5123 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5124 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5125 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5126 | #else |
| 5127 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5128 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5129 | break; |
| 5130 | |
| 5131 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5132 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5133 | *pnValue = pItem->val.int64; |
| 5134 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5135 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5136 | } |
| 5137 | break; |
| 5138 | |
| 5139 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5140 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5141 | if(pItem->val.uint64 < INT64_MAX) { |
| 5142 | *pnValue = pItem->val.int64; |
| 5143 | } else { |
| 5144 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5145 | } |
| 5146 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5147 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5148 | } |
| 5149 | break; |
| 5150 | |
| 5151 | default: |
| 5152 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5153 | } |
| 5154 | return QCBOR_SUCCESS; |
| 5155 | } |
| 5156 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5157 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5158 | /** |
| 5159 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5160 | * |
| 5161 | * @param[in] pMe The decode context. |
| 5162 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5163 | * @param[out] pnValue Result of the conversion. |
| 5164 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5165 | * |
| 5166 | * See QCBORDecode_GetInt64Convert(). |
| 5167 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5168 | void |
| 5169 | QCBORDecode_Private_GetInt64Convert(QCBORDecodeContext *pMe, |
| 5170 | uint32_t uConvertTypes, |
| 5171 | int64_t *pnValue, |
| 5172 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5173 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 5174 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5175 | return; |
| 5176 | } |
| 5177 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5178 | QCBORError uError = QCBORDecode_GetNext(pMe, pItem); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5179 | if(uError) { |
| 5180 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5181 | return; |
| 5182 | } |
| 5183 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5184 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5185 | uConvertTypes, |
| 5186 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5187 | } |
| 5188 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5189 | /** |
| 5190 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5191 | * |
| 5192 | * @param[in] pMe The decode context. |
| 5193 | * @param[in] nLabel Label to find in map. |
| 5194 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5195 | * @param[out] pnValue Result of the conversion. |
| 5196 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5197 | * |
| 5198 | * See QCBORDecode_GetInt64ConvertInMapN(). |
| 5199 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5200 | void |
| 5201 | QCBORDecode_Private_GetInt64ConvertInMapN(QCBORDecodeContext *pMe, |
| 5202 | int64_t nLabel, |
| 5203 | uint32_t uConvertTypes, |
| 5204 | int64_t *pnValue, |
| 5205 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5206 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5207 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5208 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5209 | return; |
| 5210 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5211 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5212 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
| 5213 | uConvertTypes, |
| 5214 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5215 | } |
| 5216 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5217 | /** |
| 5218 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5219 | * |
| 5220 | * @param[in] pMe The decode context. |
| 5221 | * @param[in] szLabel Label to find in map. |
| 5222 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5223 | * @param[out] pnValue Result of the conversion. |
| 5224 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5225 | * |
| 5226 | * See QCBORDecode_GetInt64ConvertInMapSZ(). |
| 5227 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5228 | void |
| 5229 | QCBORDecode_Private_GetInt64ConvertInMapSZ(QCBORDecodeContext *pMe, |
| 5230 | const char * szLabel, |
| 5231 | uint32_t uConvertTypes, |
| 5232 | int64_t *pnValue, |
| 5233 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5234 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5235 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5236 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5237 | return; |
| 5238 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5239 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5240 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
| 5241 | uConvertTypes, |
| 5242 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5246 | /** |
| 5247 | * @brief Convert many number types to an int64_t. |
| 5248 | * |
| 5249 | * @param[in] pItem The item to convert. |
| 5250 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5251 | * @param[out] pnValue The resulting converted value. |
| 5252 | * |
| 5253 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5254 | * in uConvertTypes. |
| 5255 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5256 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5257 | * or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5258 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5259 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5260 | QCBOR_Private_Int64ConvertAll(const QCBORItem *pItem, |
| 5261 | const uint32_t uConvertTypes, |
| 5262 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5263 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5264 | switch(pItem->uDataType) { |
| 5265 | |
| 5266 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5267 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5268 | return QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5269 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5270 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5271 | } |
| 5272 | break; |
| 5273 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5274 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5275 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5276 | return QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5277 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5278 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5279 | } |
| 5280 | break; |
| 5281 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5282 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5283 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5284 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5285 | return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5286 | pItem->val.expAndMantissa.nExponent, |
| 5287 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5288 | &QCBOR_Private_Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5289 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5290 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5291 | } |
| 5292 | break; |
| 5293 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5294 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5295 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5296 | return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5297 | pItem->val.expAndMantissa.nExponent, |
| 5298 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5299 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5300 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5301 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5302 | } |
| 5303 | break; |
| 5304 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5305 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5306 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5307 | int64_t nMantissa; |
| 5308 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5309 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5310 | if(uErr) { |
| 5311 | return uErr; |
| 5312 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5313 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5314 | pItem->val.expAndMantissa.nExponent, |
| 5315 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5316 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5317 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5318 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5319 | } |
| 5320 | break; |
| 5321 | |
| 5322 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5323 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5324 | int64_t nMantissa; |
| 5325 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5326 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5327 | if(uErr) { |
| 5328 | return uErr; |
| 5329 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5330 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5331 | pItem->val.expAndMantissa.nExponent, |
| 5332 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5333 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5334 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5335 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5336 | } |
| 5337 | break; |
| 5338 | |
| 5339 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5340 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5341 | int64_t nMantissa; |
| 5342 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5343 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5344 | if(uErr) { |
| 5345 | return uErr; |
| 5346 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5347 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5348 | pItem->val.expAndMantissa.nExponent, |
| 5349 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5350 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5351 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5352 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5353 | } |
| 5354 | break; |
| 5355 | |
| 5356 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5357 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5358 | int64_t nMantissa; |
| 5359 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5360 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5361 | if(uErr) { |
| 5362 | return uErr; |
| 5363 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5364 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5365 | pItem->val.expAndMantissa.nExponent, |
| 5366 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5367 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5368 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5369 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5370 | } |
| 5371 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5372 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5373 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5374 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5375 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5376 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5377 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5378 | |
| 5379 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5380 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5381 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5382 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5383 | void |
| 5384 | QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, |
| 5385 | const uint32_t uConvertTypes, |
| 5386 | int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5387 | { |
| 5388 | QCBORItem Item; |
| 5389 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5390 | QCBORDecode_Private_GetInt64Convert(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5391 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5392 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5393 | // The above conversion succeeded |
| 5394 | return; |
| 5395 | } |
| 5396 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5397 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5398 | // 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] | 5399 | return; |
| 5400 | } |
| 5401 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5402 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5403 | uConvertTypes, |
| 5404 | pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5405 | } |
| 5406 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5407 | |
| 5408 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5409 | * Public function, see header qcbor/qcbor_decode.h file |
| 5410 | */ |
| 5411 | void |
| 5412 | QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5413 | const int64_t nLabel, |
| 5414 | const uint32_t uConvertTypes, |
| 5415 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5416 | { |
| 5417 | QCBORItem Item; |
| 5418 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5419 | QCBORDecode_Private_GetInt64ConvertInMapN(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5420 | nLabel, |
| 5421 | uConvertTypes, |
| 5422 | pnValue, |
| 5423 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5424 | |
| 5425 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5426 | // The above conversion succeeded |
| 5427 | return; |
| 5428 | } |
| 5429 | |
| 5430 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5431 | // The above conversion failed in a way that code below can't correct |
| 5432 | return; |
| 5433 | } |
| 5434 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5435 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5436 | uConvertTypes, |
| 5437 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5438 | } |
| 5439 | |
| 5440 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5441 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5442 | * Public function, see header qcbor/qcbor_decode.h file |
| 5443 | */ |
| 5444 | void |
| 5445 | QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5446 | const char *szLabel, |
| 5447 | const uint32_t uConvertTypes, |
| 5448 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5449 | { |
| 5450 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5451 | QCBORDecode_Private_GetInt64ConvertInMapSZ(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5452 | szLabel, |
| 5453 | uConvertTypes, |
| 5454 | pnValue, |
| 5455 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5456 | |
| 5457 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5458 | // The above conversion succeeded |
| 5459 | return; |
| 5460 | } |
| 5461 | |
| 5462 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5463 | // The above conversion failed in a way that code below can't correct |
| 5464 | return; |
| 5465 | } |
| 5466 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5467 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5468 | uConvertTypes, |
| 5469 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5470 | } |
| 5471 | |
| 5472 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5473 | /** |
| 5474 | * @brief Convert many number types to an uint64_t. |
| 5475 | * |
| 5476 | * @param[in] pItem The item to convert. |
| 5477 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5478 | * @param[out] puValue The resulting converted value. |
| 5479 | * |
| 5480 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5481 | * in uConvertTypes. |
| 5482 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5483 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5484 | * or too small. |
| 5485 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5486 | static QCBORError |
| 5487 | QCBOR_Private_ConvertUInt64(const QCBORItem *pItem, |
| 5488 | const uint32_t uConvertTypes, |
| 5489 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5490 | { |
| 5491 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5492 | case QCBOR_TYPE_DOUBLE: |
| 5493 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5494 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5495 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5496 | // Can't use llround here because it will not convert values |
| 5497 | // greater than INT64_MAX and less than UINT64_MAX that |
| 5498 | // need to be converted so it is more complicated. |
| 5499 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 5500 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 5501 | if(isnan(pItem->val.dfnum)) { |
| 5502 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5503 | } else if(pItem->val.dfnum < 0) { |
| 5504 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5505 | } else { |
| 5506 | double dRounded = round(pItem->val.dfnum); |
| 5507 | // See discussion in DecodeDateEpoch() for |
| 5508 | // explanation of - 0x7ff |
| 5509 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 5510 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5511 | } |
| 5512 | *puValue = (uint64_t)dRounded; |
| 5513 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5514 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5515 | if(isnan(pItem->val.fnum)) { |
| 5516 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5517 | } else if(pItem->val.fnum < 0) { |
| 5518 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5519 | } else { |
| 5520 | float fRounded = roundf(pItem->val.fnum); |
| 5521 | // See discussion in DecodeDateEpoch() for |
| 5522 | // explanation of - 0x7ff |
| 5523 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 5524 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5525 | } |
| 5526 | *puValue = (uint64_t)fRounded; |
| 5527 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5528 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5529 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 5530 | // round() and roundf() shouldn't result in exceptions here, but |
| 5531 | // catch them to be robust and thorough. Don't try to |
| 5532 | // distinguish between the various exceptions because it seems |
| 5533 | // they vary by CPU, compiler and OS. |
| 5534 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5535 | } |
| 5536 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5537 | } else { |
| 5538 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5539 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5540 | #else |
| 5541 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5542 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5543 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5544 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5545 | case QCBOR_TYPE_INT64: |
| 5546 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 5547 | if(pItem->val.int64 >= 0) { |
| 5548 | *puValue = (uint64_t)pItem->val.int64; |
| 5549 | } else { |
| 5550 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5551 | } |
| 5552 | } else { |
| 5553 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5554 | } |
| 5555 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5556 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5557 | case QCBOR_TYPE_UINT64: |
| 5558 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 5559 | *puValue = pItem->val.uint64; |
| 5560 | } else { |
| 5561 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5562 | } |
| 5563 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5564 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5565 | default: |
| 5566 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5567 | } |
| 5568 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5569 | return QCBOR_SUCCESS; |
| 5570 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5571 | |
| 5572 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5573 | /** |
| 5574 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 5575 | * |
| 5576 | * @param[in] pMe The decode context. |
| 5577 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5578 | * @param[out] puValue Result of the conversion. |
| 5579 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5580 | * |
| 5581 | * See QCBORDecode_GetUInt64Convert(). |
| 5582 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5583 | void |
| 5584 | QCBORDecode_Private_GetUInt64Convert(QCBORDecodeContext *pMe, |
| 5585 | const uint32_t uConvertTypes, |
| 5586 | uint64_t *puValue, |
| 5587 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5588 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5589 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5590 | return; |
| 5591 | } |
| 5592 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5593 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5594 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5595 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5596 | if(uError) { |
| 5597 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5598 | return; |
| 5599 | } |
| 5600 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 5601 | if(pItem) { |
| 5602 | *pItem = Item; |
| 5603 | } |
| 5604 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5605 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(&Item, |
| 5606 | uConvertTypes, |
| 5607 | puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5608 | } |
| 5609 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5610 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5611 | /** |
| 5612 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 5613 | * |
| 5614 | * @param[in] pMe The decode context. |
| 5615 | * @param[in] nLabel Label to find in map. |
| 5616 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5617 | * @param[out] puValue Result of the conversion. |
| 5618 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5619 | * |
| 5620 | * See QCBORDecode_GetUInt64ConvertInMapN(). |
| 5621 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5622 | void |
| 5623 | QCBORDecode_Private_GetUInt64ConvertInMapN(QCBORDecodeContext *pMe, |
| 5624 | const int64_t nLabel, |
| 5625 | const uint32_t uConvertTypes, |
| 5626 | uint64_t *puValue, |
| 5627 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5628 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5629 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5630 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5631 | return; |
| 5632 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5633 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5634 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem, |
| 5635 | uConvertTypes, |
| 5636 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5637 | } |
| 5638 | |
| 5639 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5640 | /** |
| 5641 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 5642 | * |
| 5643 | * @param[in] pMe The decode context. |
| 5644 | * @param[in] szLabel Label to find in map. |
| 5645 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5646 | * @param[out] puValue Result of the conversion. |
| 5647 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5648 | * |
| 5649 | * See QCBORDecode_GetUInt64ConvertInMapSZ(). |
| 5650 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5651 | void |
| 5652 | QCBORDecode_Private_GetUInt64ConvertInMapSZ(QCBORDecodeContext *pMe, |
| 5653 | const char *szLabel, |
| 5654 | const uint32_t uConvertTypes, |
| 5655 | uint64_t *puValue, |
| 5656 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5657 | { |
| 5658 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5659 | return; |
| 5660 | } |
| 5661 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5662 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5663 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5664 | return; |
| 5665 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5666 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5667 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem, |
| 5668 | uConvertTypes, |
| 5669 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5670 | } |
| 5671 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5672 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5673 | /** |
| 5674 | * @brief Convert many number types to an unt64_t. |
| 5675 | * |
| 5676 | * @param[in] pItem The item to convert. |
| 5677 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5678 | * @param[out] puValue The resulting converted value. |
| 5679 | * |
| 5680 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5681 | * in uConvertTypes. |
| 5682 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5683 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5684 | * or too small. |
| 5685 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5686 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5687 | QCBOR_Private_UInt64ConvertAll(const QCBORItem *pItem, |
| 5688 | const uint32_t uConvertTypes, |
| 5689 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5690 | { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 5691 | switch(pItem->uDataType) { /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5692 | |
| 5693 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5694 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5695 | return QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5696 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5697 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5698 | } |
| 5699 | break; |
| 5700 | |
| 5701 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5702 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5703 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5704 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5705 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5706 | } |
| 5707 | break; |
| 5708 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5709 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5710 | |
| 5711 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5712 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5713 | return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5714 | pItem->val.expAndMantissa.nExponent, |
| 5715 | puValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5716 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5717 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5718 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5719 | } |
| 5720 | break; |
| 5721 | |
| 5722 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5723 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5724 | return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5725 | pItem->val.expAndMantissa.nExponent, |
| 5726 | puValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5727 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5728 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5729 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5730 | } |
| 5731 | break; |
| 5732 | |
| 5733 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5734 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5735 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5736 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5737 | uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5738 | if(uErr != QCBOR_SUCCESS) { |
| 5739 | return uErr; |
| 5740 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5741 | return QCBOR_Private_ExponentitateUU(uMantissa, |
| 5742 | pItem->val.expAndMantissa.nExponent, |
| 5743 | puValue, |
| 5744 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5745 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5746 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5747 | } |
| 5748 | break; |
| 5749 | |
| 5750 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5751 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5752 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5753 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5754 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5755 | } |
| 5756 | break; |
| 5757 | |
| 5758 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5759 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5760 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5761 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5762 | uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, |
| 5763 | &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5764 | if(uErr != QCBOR_SUCCESS) { |
| 5765 | return uErr; |
| 5766 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5767 | return QCBOR_Private_ExponentitateUU(uMantissa, |
| 5768 | pItem->val.expAndMantissa.nExponent, |
| 5769 | puValue, |
| 5770 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5771 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5772 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5773 | } |
| 5774 | break; |
| 5775 | |
| 5776 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5777 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5778 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5779 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5780 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5781 | } |
| 5782 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5783 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5784 | default: |
| 5785 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5786 | } |
| 5787 | } |
| 5788 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5789 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5790 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5791 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5792 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5793 | void |
| 5794 | QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, |
| 5795 | const uint32_t uConvertTypes, |
| 5796 | uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5797 | { |
| 5798 | QCBORItem Item; |
| 5799 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5800 | QCBORDecode_Private_GetUInt64Convert(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5801 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5802 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5803 | // The above conversion succeeded |
| 5804 | return; |
| 5805 | } |
| 5806 | |
| 5807 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5808 | // 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] | 5809 | return; |
| 5810 | } |
| 5811 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5812 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 5813 | uConvertTypes, |
| 5814 | puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5815 | } |
| 5816 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5817 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5818 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5819 | * Public function, see header qcbor/qcbor_decode.h file |
| 5820 | */ |
| 5821 | void |
| 5822 | QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5823 | const int64_t nLabel, |
| 5824 | const uint32_t uConvertTypes, |
| 5825 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5826 | { |
| 5827 | QCBORItem Item; |
| 5828 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5829 | QCBORDecode_Private_GetUInt64ConvertInMapN(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5830 | nLabel, |
| 5831 | uConvertTypes, |
| 5832 | puValue, |
| 5833 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5834 | |
| 5835 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5836 | // The above conversion succeeded |
| 5837 | return; |
| 5838 | } |
| 5839 | |
| 5840 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5841 | // The above conversion failed in a way that code below can't correct |
| 5842 | return; |
| 5843 | } |
| 5844 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5845 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 5846 | uConvertTypes, |
| 5847 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5848 | } |
| 5849 | |
| 5850 | |
| 5851 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5852 | * Public function, see header qcbor/qcbor_decode.h file |
| 5853 | */ |
| 5854 | void |
| 5855 | QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5856 | const char *szLabel, |
| 5857 | const uint32_t uConvertTypes, |
| 5858 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5859 | { |
| 5860 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5861 | QCBORDecode_Private_GetUInt64ConvertInMapSZ(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5862 | szLabel, |
| 5863 | uConvertTypes, |
| 5864 | puValue, |
| 5865 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5866 | |
| 5867 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5868 | // The above conversion succeeded |
| 5869 | return; |
| 5870 | } |
| 5871 | |
| 5872 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5873 | // The above conversion failed in a way that code below can't correct |
| 5874 | return; |
| 5875 | } |
| 5876 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5877 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 5878 | uConvertTypes, |
| 5879 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5880 | } |
| 5881 | |
| 5882 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5883 | |
| 5884 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5885 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5886 | /** |
| 5887 | * @brief Basic conversions to a double. |
| 5888 | * |
| 5889 | * @param[in] pItem The item to convert |
| 5890 | * @param[in] uConvertTypes Bit flags indicating source types for conversion |
| 5891 | * @param[out] pdValue The value converted to a double |
| 5892 | * |
| 5893 | * This does the conversions that don't need much object code, |
| 5894 | * the conversions from int, uint and float to double. |
| 5895 | * |
| 5896 | * See QCBOR_Private_DoubleConvertAll() for the full set |
| 5897 | * of conversions. |
| 5898 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5899 | static QCBORError |
| 5900 | QCBOR_Private_ConvertDouble(const QCBORItem *pItem, |
| 5901 | const uint32_t uConvertTypes, |
| 5902 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5903 | { |
| 5904 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5905 | case QCBOR_TYPE_FLOAT: |
| 5906 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5907 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 5908 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5909 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5910 | *pdValue = (double)pItem->val.fnum; |
| 5911 | } else { |
| 5912 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5913 | } |
| 5914 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5915 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5916 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5917 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5918 | break; |
| 5919 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5920 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5921 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 5922 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5923 | *pdValue = pItem->val.dfnum; |
| 5924 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5925 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5926 | } |
| 5927 | } |
| 5928 | break; |
| 5929 | |
| 5930 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5931 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5932 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5933 | // A simple cast seems to do the job with no worry of exceptions. |
| 5934 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5935 | *pdValue = (double)pItem->val.int64; |
| 5936 | |
| 5937 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5938 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5939 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5940 | #else |
| 5941 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5942 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5943 | break; |
| 5944 | |
| 5945 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5946 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5947 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5948 | // A simple cast seems to do the job with no worry of exceptions. |
| 5949 | // There will be precision loss for some values. |
| 5950 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5951 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5952 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5953 | } |
| 5954 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5955 | #else |
| 5956 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5957 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5958 | |
| 5959 | default: |
| 5960 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5961 | } |
| 5962 | |
| 5963 | return QCBOR_SUCCESS; |
| 5964 | } |
| 5965 | |
| 5966 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5967 | /** |
| 5968 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 5969 | * |
| 5970 | * @param[in] pMe The decode context. |
| 5971 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 5972 | * @param[out] pdValue The output of the conversion. |
| 5973 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5974 | * |
| 5975 | * See QCBORDecode_GetDoubleConvert(). |
| 5976 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5977 | void |
| 5978 | QCBORDecode_Private_GetDoubleConvert(QCBORDecodeContext *pMe, |
| 5979 | const uint32_t uConvertTypes, |
| 5980 | double *pdValue, |
| 5981 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5982 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5983 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5984 | return; |
| 5985 | } |
| 5986 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5987 | QCBORError uError = QCBORDecode_GetNext(pMe, pItem); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5988 | if(uError) { |
| 5989 | pMe->uLastError = (uint8_t)uError; |
| 5990 | return; |
| 5991 | } |
| 5992 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5993 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5994 | uConvertTypes, |
| 5995 | pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5996 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5997 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5998 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 5999 | /** |
| 6000 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 6001 | * |
| 6002 | * @param[in] pMe The decode context. |
| 6003 | * @param[in] nLabel Label to find in map. |
| 6004 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 6005 | * @param[out] pdValue The output of the conversion. |
| 6006 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6007 | * |
| 6008 | * See QCBORDecode_GetDoubleConvertInMapN(). |
| 6009 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6010 | void |
| 6011 | QCBORDecode_Private_GetDoubleConvertInMapN(QCBORDecodeContext *pMe, |
| 6012 | const int64_t nLabel, |
| 6013 | const uint32_t uConvertTypes, |
| 6014 | double *pdValue, |
| 6015 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6016 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6017 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6018 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6019 | return; |
| 6020 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6021 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6022 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
| 6023 | uConvertTypes, |
| 6024 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6025 | } |
| 6026 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6027 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6028 | /** |
| 6029 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 6030 | * |
| 6031 | * @param[in] pMe The decode context. |
| 6032 | * @param[in] szLabel Label to find in map. |
| 6033 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 6034 | * @param[out] pdValue The output of the conversion. |
| 6035 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6036 | * |
| 6037 | * See QCBORDecode_GetDoubleConvertInMapSZ(). |
| 6038 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6039 | void |
| 6040 | QCBORDecode_Private_GetDoubleConvertInMapSZ(QCBORDecodeContext *pMe, |
| 6041 | const char *szLabel, |
| 6042 | const uint32_t uConvertTypes, |
| 6043 | double *pdValue, |
| 6044 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6045 | { |
| 6046 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6047 | return; |
| 6048 | } |
| 6049 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6050 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6051 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6052 | return; |
| 6053 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6054 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6055 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
| 6056 | uConvertTypes, |
| 6057 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6058 | } |
| 6059 | |
| 6060 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6061 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6062 | /** |
| 6063 | * @brief Convert a big number to double-precision float. |
| 6064 | * |
| 6065 | * @param[in] BigNum The big number to convert |
| 6066 | * |
| 6067 | * @returns The double value. |
| 6068 | * |
| 6069 | * This will always succeed. It will lose precision for larger |
| 6070 | * numbers. If the big number is too large to fit (more than |
| 6071 | * 1.7976931348623157E+308) infinity will be returned. NaN is never |
| 6072 | * returned. |
| 6073 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6074 | static double |
| 6075 | QCBOR_Private_ConvertBigNumToDouble(const UsefulBufC BigNum) |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6076 | { |
| 6077 | double dResult; |
| 6078 | |
| 6079 | dResult = 0.0; |
| 6080 | const uint8_t *pByte = BigNum.ptr; |
| 6081 | size_t uLen = BigNum.len; |
| 6082 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6083 | * is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6084 | while(uLen--) { |
| 6085 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 6086 | } |
| 6087 | |
| 6088 | return dResult; |
| 6089 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6090 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 6091 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6092 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6093 | |
| 6094 | |
| 6095 | /** |
| 6096 | * @brief Convert many number types to a double. |
| 6097 | * |
| 6098 | * @param[in] pItem The item to convert. |
| 6099 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 6100 | * @param[out] pdValue The resulting converted value. |
| 6101 | * |
| 6102 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 6103 | * in uConvertTypes. |
| 6104 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 6105 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 6106 | * or too small. |
| 6107 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6108 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6109 | QCBOR_Private_DoubleConvertAll(const QCBORItem *pItem, |
| 6110 | const uint32_t uConvertTypes, |
| 6111 | double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6112 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6113 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6114 | /* |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6115 | * What Every Computer Scientist Should Know About Floating-Point Arithmetic |
| 6116 | * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 6117 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6118 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 6119 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6120 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6121 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6122 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6123 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6124 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 6125 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 6126 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6127 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6128 | } |
| 6129 | break; |
| 6130 | |
| 6131 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6132 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6133 | // Underflow gives 0, overflow gives infinity |
| 6134 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 6135 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6136 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6137 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6138 | } |
| 6139 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6140 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6141 | |
| 6142 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6143 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6144 | *pdValue = QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6145 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6146 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6147 | } |
| 6148 | break; |
| 6149 | |
| 6150 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6151 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6152 | *pdValue = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6153 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6154 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6155 | } |
| 6156 | break; |
| 6157 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6158 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6159 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6160 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6161 | double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6162 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 6163 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6164 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6165 | } |
| 6166 | break; |
| 6167 | |
| 6168 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6169 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6170 | double dMantissa = -QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6171 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 6172 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6173 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6174 | } |
| 6175 | break; |
| 6176 | |
| 6177 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6178 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6179 | double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6180 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 6181 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6182 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6183 | } |
| 6184 | break; |
| 6185 | |
| 6186 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6187 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6188 | double dMantissa = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6189 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 6190 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6191 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6192 | } |
| 6193 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6194 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 6195 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6196 | default: |
| 6197 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6198 | } |
| 6199 | |
| 6200 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6201 | |
| 6202 | #else |
| 6203 | (void)pItem; |
| 6204 | (void)uConvertTypes; |
| 6205 | (void)pdValue; |
| 6206 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 6207 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 6208 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6209 | } |
| 6210 | |
| 6211 | |
| 6212 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6213 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6214 | */ |
| 6215 | void |
| 6216 | QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 6217 | const uint32_t uConvertTypes, |
| 6218 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6219 | { |
| 6220 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6221 | QCBORItem Item; |
| 6222 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6223 | QCBORDecode_Private_GetDoubleConvert(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6224 | |
| 6225 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6226 | // The above conversion succeeded |
| 6227 | return; |
| 6228 | } |
| 6229 | |
| 6230 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6231 | // The above conversion failed in a way that code below can't correct |
| 6232 | return; |
| 6233 | } |
| 6234 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6235 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6236 | uConvertTypes, |
| 6237 | pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6238 | } |
| 6239 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6240 | |
| 6241 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6242 | * Public function, see header qcbor/qcbor_decode.h file |
| 6243 | */ |
| 6244 | void |
| 6245 | QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 6246 | const int64_t nLabel, |
| 6247 | const uint32_t uConvertTypes, |
| 6248 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6249 | { |
| 6250 | QCBORItem Item; |
| 6251 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6252 | QCBORDecode_Private_GetDoubleConvertInMapN(pMe, |
| 6253 | nLabel, |
| 6254 | uConvertTypes, |
| 6255 | pdValue, |
| 6256 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6257 | |
| 6258 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6259 | // The above conversion succeeded |
| 6260 | return; |
| 6261 | } |
| 6262 | |
| 6263 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6264 | // The above conversion failed in a way that code below can't correct |
| 6265 | return; |
| 6266 | } |
| 6267 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6268 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6269 | uConvertTypes, |
| 6270 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6271 | } |
| 6272 | |
| 6273 | |
| 6274 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6275 | * Public function, see header qcbor/qcbor_decode.h file |
| 6276 | */ |
| 6277 | void |
| 6278 | QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 6279 | const char *szLabel, |
| 6280 | const uint32_t uConvertTypes, |
| 6281 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6282 | { |
| 6283 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6284 | QCBORDecode_Private_GetDoubleConvertInMapSZ(pMe, |
| 6285 | szLabel, |
| 6286 | uConvertTypes, |
| 6287 | pdValue, |
| 6288 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6289 | |
| 6290 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6291 | // The above conversion succeeded |
| 6292 | return; |
| 6293 | } |
| 6294 | |
| 6295 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6296 | // The above conversion failed in a way that code below can't correct |
| 6297 | return; |
| 6298 | } |
| 6299 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6300 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6301 | uConvertTypes, |
| 6302 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6303 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 6304 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6305 | |
| 6306 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6307 | |
| 6308 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6309 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6310 | /** |
| 6311 | * @brief Convert an integer to a big number |
| 6312 | * |
| 6313 | * @param[in] uInt The integer to convert. |
| 6314 | * @param[in] Buffer The buffer to output the big number to. |
| 6315 | * |
| 6316 | * @returns The big number or NULLUsefulBufC is the buffer is to small. |
| 6317 | * |
| 6318 | * This always succeeds unless the buffer is too small. |
| 6319 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6320 | static UsefulBufC |
| 6321 | QCBOR_Private_ConvertIntToBigNum(uint64_t uInt, const UsefulBuf Buffer) |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6322 | { |
| 6323 | while((uInt & 0xff00000000000000UL) == 0) { |
| 6324 | uInt = uInt << 8; |
| 6325 | }; |
| 6326 | |
| 6327 | UsefulOutBuf UOB; |
| 6328 | |
| 6329 | UsefulOutBuf_Init(&UOB, Buffer); |
| 6330 | |
| 6331 | while(uInt) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6332 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 6333 | uInt = uInt << 8; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6334 | } |
| 6335 | |
| 6336 | return UsefulOutBuf_OutUBuf(&UOB); |
| 6337 | } |
| 6338 | |
| 6339 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6340 | /** |
| 6341 | * @brief Check and/or complete mantissa and exponent item. |
| 6342 | * |
| 6343 | * @param[in] pMe The decoder context |
| 6344 | * @param[in] TagSpec Expected type(s) |
| 6345 | * @param[in,out] pItem See below |
| 6346 | * |
| 6347 | * This is for decimal fractions and big floats, both of which are a |
| 6348 | * mantissa and exponent. |
| 6349 | * |
| 6350 | * The input item is either a fully decoded decimal faction or big |
| 6351 | * float, or a just the decoded first item of a decimal fraction or |
| 6352 | * big float. |
| 6353 | * |
| 6354 | * On output, the item is always a fully decoded decimal fraction or |
| 6355 | * big float. |
| 6356 | * |
| 6357 | * This errors out if the input type does not meet the TagSpec. |
| 6358 | */ |
| 6359 | // TODO: document and see tests for the bug that was fixed by this rewrite |
| 6360 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6361 | QCBOR_Private_ExpMantissaTypeHandler(QCBORDecodeContext *pMe, |
| 6362 | const QCBOR_Private_TagSpec TagSpec, |
| 6363 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6364 | { |
| 6365 | QCBORError uErr; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6366 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6367 | /* pItem could either be an auto-decoded mantissa and exponent or |
| 6368 | * the opening array of an undecoded mantissa and exponent. This |
| 6369 | * check will succeed on either, but doesn't say which it was. |
| 6370 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6371 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6372 | if(uErr != QCBOR_SUCCESS) { |
| 6373 | goto Done; |
| 6374 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6375 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6376 | if(pItem->uDataType == QCBOR_TYPE_ARRAY) { |
| 6377 | /* The item is an array, which means is is an undecoded mantissa |
| 6378 | * and exponent. This call consumes the items in the array and |
| 6379 | * results in a decoded mantissa and exponent in pItem. This is |
| 6380 | * the case where there was no tag. |
| 6381 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6382 | uErr = QCBORDecode_Private_ExpMantissa(pMe, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6383 | if(uErr != QCBOR_SUCCESS) { |
| 6384 | goto Done; |
| 6385 | } |
| 6386 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6387 | /* The above decode didn't determine whether it is a decimal |
| 6388 | * fraction or big num. Which of these two depends on what the |
| 6389 | * caller wants it decoded as since there is no tag, so fish the |
| 6390 | * type out of the TagSpec. */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6391 | pItem->uDataType = QCBOR_Private_ExpMantissaDataType(TagSpec.uTaggedTypes[0], pItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6392 | |
| 6393 | /* No need to check the type again. All that we need to know was |
| 6394 | * that it decoded correctly as a mantissa and exponent. The |
| 6395 | * QCBOR type is set out by what was requested. |
| 6396 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6397 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6398 | |
| 6399 | /* If the item was not an array and the check passed, then |
| 6400 | * it is a fully decoded big float or decimal fraction and |
| 6401 | * matches what is requested. |
| 6402 | */ |
| 6403 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6404 | Done: |
| 6405 | return uErr; |
| 6406 | } |
| 6407 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6408 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6409 | /* Some notes from the work to disable tags. |
| 6410 | * |
| 6411 | * The API for big floats and decimal fractions seems good. |
| 6412 | * If there's any issue with it it's that the code size to |
| 6413 | * implement is a bit large because of the conversion |
| 6414 | * to/from int and bignum that is required. There is no API |
| 6415 | * that doesn't do the conversion so dead stripping will never |
| 6416 | * leave that code out. |
| 6417 | * |
| 6418 | * The implementation itself seems correct, but not as clean |
| 6419 | * and neat as it could be. It could probably be smaller too. |
| 6420 | * |
| 6421 | * The implementation has three main parts / functions |
| 6422 | * - The decoding of the array of two |
| 6423 | * - All the tag and type checking for the various API functions |
| 6424 | * - Conversion to/from bignum and int |
| 6425 | * |
| 6426 | * The type checking seems like it wastes the most code for |
| 6427 | * what it needs to do. |
| 6428 | * |
| 6429 | * The inlining for the conversion is probably making the |
| 6430 | * overall code base larger. |
| 6431 | * |
| 6432 | * The tests cases could be organized a lot better and be |
| 6433 | * more thorough. |
| 6434 | * |
| 6435 | * Seems also like there could be more common code in the |
| 6436 | * first tier part of the public API. Some functions only |
| 6437 | * vary by a TagSpec. |
| 6438 | */ |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6439 | |
| 6440 | /** |
| 6441 | * @brief Common processor for exponent and mantissa. |
| 6442 | * |
| 6443 | * @param[in] pMe The decode context. |
| 6444 | * @param[in] TagSpec The expected/allowed tags. |
| 6445 | * @param[in] pItem The data item to process. |
| 6446 | * @param[out] pnMantissa The returned mantissa as an int64_t. |
| 6447 | * @param[out] pnExponent The returned exponent as an int64_t. |
| 6448 | * |
| 6449 | * This handles exponent and mantissa for base 2 and 10. This |
| 6450 | * is limited to a mantissa that is an int64_t. See also |
| 6451 | * QCBORDecode_Private_ProcessExpMantissaBig(). |
| 6452 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6453 | static void |
| 6454 | QCBOR_Private_ProcessExpMantissa(QCBORDecodeContext *pMe, |
| 6455 | const QCBOR_Private_TagSpec TagSpec, |
| 6456 | QCBORItem *pItem, |
| 6457 | int64_t *pnMantissa, |
| 6458 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6459 | { |
| 6460 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6461 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6462 | uErr = QCBOR_Private_ExpMantissaTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6463 | if(uErr != QCBOR_SUCCESS) { |
| 6464 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6465 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6466 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6467 | switch (pItem->uDataType) { |
| 6468 | |
| 6469 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 6470 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6471 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6472 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6473 | break; |
| 6474 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6475 | #ifndef QCBOR_DISABLE_TAGS |
| 6476 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6477 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 6478 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 6479 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6480 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6481 | break; |
| 6482 | |
| 6483 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 6484 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 6485 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6486 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6487 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6488 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6489 | |
| 6490 | default: |
| 6491 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 6492 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6493 | |
| 6494 | Done: |
| 6495 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6496 | } |
| 6497 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6498 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6499 | /** |
| 6500 | * @brief Decode exponent and mantissa into a big number. |
| 6501 | * |
| 6502 | * @param[in] pMe The decode context. |
| 6503 | * @param[in] TagSpec The expected/allowed tags. |
| 6504 | * @param[in] pItem Item to decode and convert. |
| 6505 | * @param[in] BufferForMantissa Buffer to output mantissa into. |
| 6506 | * @param[out] pMantissa The output mantissa. |
| 6507 | * @param[out] pbIsNegative The sign of the output. |
| 6508 | * @param[out] pnExponent The mantissa of the output. |
| 6509 | * |
| 6510 | * This is the common processing of a decimal fraction or a big float |
| 6511 | * into a big number. This will decode and consume all the CBOR items |
| 6512 | * that make up the decimal fraction or big float. |
| 6513 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6514 | static void |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6515 | QCBORDecode_Private_ProcessExpMantissaBig(QCBORDecodeContext *pMe, |
| 6516 | const QCBOR_Private_TagSpec TagSpec, |
| 6517 | QCBORItem *pItem, |
| 6518 | const UsefulBuf BufferForMantissa, |
| 6519 | UsefulBufC *pMantissa, |
| 6520 | bool *pbIsNegative, |
| 6521 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6522 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6523 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6524 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6525 | uErr = QCBOR_Private_ExpMantissaTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6526 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6527 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6528 | } |
| 6529 | |
| 6530 | uint64_t uMantissa; |
| 6531 | |
| 6532 | switch (pItem->uDataType) { |
| 6533 | |
| 6534 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6535 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6536 | /* See comments in ExponentiateNN() on handling INT64_MIN */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6537 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 6538 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 6539 | *pbIsNegative = false; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6540 | } else if(pItem->val.expAndMantissa.Mantissa.nInt != INT64_MIN) { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6541 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 6542 | *pbIsNegative = true; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6543 | } else { |
| 6544 | uMantissa = (uint64_t)INT64_MAX+1; |
| 6545 | *pbIsNegative = true; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6546 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6547 | *pMantissa = QCBOR_Private_ConvertIntToBigNum(uMantissa, |
| 6548 | BufferForMantissa); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6549 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6550 | break; |
| 6551 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6552 | #ifndef QCBOR_DISABLE_TAGS |
| 6553 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6554 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6555 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6556 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6557 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 6558 | *pbIsNegative = false; |
| 6559 | break; |
| 6560 | |
| 6561 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6562 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6563 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6564 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 6565 | *pbIsNegative = true; |
| 6566 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6567 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6568 | |
| 6569 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6570 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6571 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6572 | |
| 6573 | Done: |
| 6574 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6575 | } |
| 6576 | |
| 6577 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6578 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6579 | * Public function, see header qcbor/qcbor_decode.h file |
| 6580 | */ |
| 6581 | void |
| 6582 | QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 6583 | const uint8_t uTagRequirement, |
| 6584 | int64_t *pnMantissa, |
| 6585 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6586 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6587 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6588 | return; |
| 6589 | } |
| 6590 | |
| 6591 | QCBORItem Item; |
| 6592 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6593 | if(uError) { |
| 6594 | pMe->uLastError = (uint8_t)uError; |
| 6595 | return; |
| 6596 | } |
| 6597 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6598 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6599 | { |
| 6600 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6601 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6602 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6603 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6604 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6605 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6606 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6607 | TagSpec, |
| 6608 | &Item, |
| 6609 | pnMantissa, |
| 6610 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6611 | } |
| 6612 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6613 | |
| 6614 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6615 | * Public function, see header qcbor/qcbor_decode.h file |
| 6616 | */ |
| 6617 | void |
| 6618 | QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
| 6619 | const int64_t nLabel, |
| 6620 | const uint8_t uTagRequirement, |
| 6621 | int64_t *pnMantissa, |
| 6622 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6623 | { |
| 6624 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6625 | return; |
| 6626 | } |
| 6627 | |
| 6628 | QCBORItem Item; |
| 6629 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6630 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6631 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6632 | { |
| 6633 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6634 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6635 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6636 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6637 | }; |
| 6638 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6639 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6640 | TagSpec, |
| 6641 | &Item, |
| 6642 | pnMantissa, |
| 6643 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6644 | } |
| 6645 | |
| 6646 | |
| 6647 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6648 | * Public function, see header qcbor/qcbor_decode.h file |
| 6649 | */ |
| 6650 | void |
| 6651 | QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
| 6652 | const char *szLabel, |
| 6653 | const uint8_t uTagRequirement, |
| 6654 | int64_t *pnMantissa, |
| 6655 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6656 | { |
| 6657 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6658 | return; |
| 6659 | } |
| 6660 | |
| 6661 | QCBORItem Item; |
| 6662 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6663 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6664 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6665 | { |
| 6666 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6667 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6668 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6669 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6670 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6671 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6672 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6673 | TagSpec, |
| 6674 | &Item, |
| 6675 | pnMantissa, |
| 6676 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6677 | } |
| 6678 | |
| 6679 | |
| 6680 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6681 | * Public function, see header qcbor/qcbor_decode.h file |
| 6682 | */ |
| 6683 | void |
| 6684 | QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 6685 | const uint8_t uTagRequirement, |
| 6686 | const UsefulBuf MantissaBuffer, |
| 6687 | UsefulBufC *pMantissa, |
| 6688 | bool *pbMantissaIsNegative, |
| 6689 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6690 | { |
| 6691 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6692 | return; |
| 6693 | } |
| 6694 | |
| 6695 | QCBORItem Item; |
| 6696 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6697 | if(uError) { |
| 6698 | pMe->uLastError = (uint8_t)uError; |
| 6699 | return; |
| 6700 | } |
| 6701 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6702 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6703 | { |
| 6704 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6705 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6706 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6707 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6708 | }; |
| 6709 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6710 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 6711 | TagSpec, |
| 6712 | &Item, |
| 6713 | MantissaBuffer, |
| 6714 | pMantissa, |
| 6715 | pbMantissaIsNegative, |
| 6716 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6717 | } |
| 6718 | |
| 6719 | |
| 6720 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6721 | * Public function, see header qcbor/qcbor_decode.h file |
| 6722 | */ |
| 6723 | void |
| 6724 | QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
| 6725 | const int64_t nLabel, |
| 6726 | const uint8_t uTagRequirement, |
| 6727 | const UsefulBuf BufferForMantissa, |
| 6728 | UsefulBufC *pMantissa, |
| 6729 | bool *pbIsNegative, |
| 6730 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6731 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6732 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6733 | return; |
| 6734 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6735 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6736 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6737 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6738 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6739 | return; |
| 6740 | } |
| 6741 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6742 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6743 | { |
| 6744 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6745 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6746 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6747 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6748 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6749 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6750 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 6751 | TagSpec, |
| 6752 | &Item, |
| 6753 | BufferForMantissa, |
| 6754 | pMantissa, |
| 6755 | pbIsNegative, |
| 6756 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6757 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6758 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6759 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6760 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6761 | * Public function, see header qcbor/qcbor_decode.h file |
| 6762 | */ |
| 6763 | void |
| 6764 | QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
| 6765 | const char *szLabel, |
| 6766 | const uint8_t uTagRequirement, |
| 6767 | const UsefulBuf BufferForMantissa, |
| 6768 | UsefulBufC *pMantissa, |
| 6769 | bool *pbIsNegative, |
| 6770 | int64_t *pnExponent) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6771 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6772 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6773 | return; |
| 6774 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6775 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6776 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6777 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6778 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6779 | return; |
| 6780 | } |
| 6781 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6782 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6783 | { |
| 6784 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6785 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6786 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6787 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6788 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6789 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6790 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 6791 | TagSpec, |
| 6792 | &Item, |
| 6793 | BufferForMantissa, |
| 6794 | pMantissa, |
| 6795 | pbIsNegative, |
| 6796 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6797 | } |
| 6798 | |
| 6799 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6800 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6801 | * Public function, see header qcbor/qcbor_decode.h file |
| 6802 | */ |
| 6803 | void |
| 6804 | QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 6805 | const uint8_t uTagRequirement, |
| 6806 | int64_t *pnMantissa, |
| 6807 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6808 | { |
| 6809 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6810 | return; |
| 6811 | } |
| 6812 | |
| 6813 | QCBORItem Item; |
| 6814 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6815 | if(uError) { |
| 6816 | pMe->uLastError = (uint8_t)uError; |
| 6817 | return; |
| 6818 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6819 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6820 | { |
| 6821 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6822 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6823 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6824 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6825 | }; |
| 6826 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6827 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6828 | TagSpec, |
| 6829 | &Item, |
| 6830 | pnMantissa, |
| 6831 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6832 | } |
| 6833 | |
| 6834 | |
| 6835 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6836 | * Public function, see header qcbor/qcbor_decode.h file |
| 6837 | */ |
| 6838 | void |
| 6839 | QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
| 6840 | const int64_t nLabel, |
| 6841 | const uint8_t uTagRequirement, |
| 6842 | int64_t *pnMantissa, |
| 6843 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6844 | { |
| 6845 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6846 | return; |
| 6847 | } |
| 6848 | |
| 6849 | QCBORItem Item; |
| 6850 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6851 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6852 | return; |
| 6853 | } |
| 6854 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6855 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6856 | { |
| 6857 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6858 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6859 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6860 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6861 | }; |
| 6862 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6863 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6864 | TagSpec, |
| 6865 | &Item, |
| 6866 | pnMantissa, |
| 6867 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6868 | } |
| 6869 | |
| 6870 | |
| 6871 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6872 | * Public function, see header qcbor/qcbor_decode.h file |
| 6873 | */ |
| 6874 | void |
| 6875 | QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
| 6876 | const char *szLabel, |
| 6877 | const uint8_t uTagRequirement, |
| 6878 | int64_t *pnMantissa, |
| 6879 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6880 | { |
| 6881 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6882 | return; |
| 6883 | } |
| 6884 | |
| 6885 | QCBORItem Item; |
| 6886 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6887 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6888 | return; |
| 6889 | } |
| 6890 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6891 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6892 | { |
| 6893 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6894 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6895 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6896 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6897 | }; |
| 6898 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6899 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6900 | TagSpec, |
| 6901 | &Item, |
| 6902 | pnMantissa, |
| 6903 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | |
| 6907 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6908 | * Public function, see header qcbor/qcbor_decode.h file |
| 6909 | */ |
| 6910 | void |
| 6911 | QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 6912 | const uint8_t uTagRequirement, |
| 6913 | const UsefulBuf MantissaBuffer, |
| 6914 | UsefulBufC *pMantissa, |
| 6915 | bool *pbMantissaIsNegative, |
| 6916 | int64_t *pnExponent) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6917 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6918 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6919 | return; |
| 6920 | } |
| 6921 | |
| 6922 | QCBORItem Item; |
| 6923 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6924 | if(uError) { |
| 6925 | pMe->uLastError = (uint8_t)uError; |
| 6926 | return; |
| 6927 | } |
| 6928 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6929 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6930 | { |
| 6931 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6932 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6933 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6934 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6935 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6936 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6937 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 6938 | TagSpec, |
| 6939 | &Item, |
| 6940 | MantissaBuffer, |
| 6941 | pMantissa, |
| 6942 | pbMantissaIsNegative, |
| 6943 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6944 | } |
| 6945 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6946 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6947 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6948 | * Public function, see header qcbor/qcbor_decode.h file |
| 6949 | */ |
| 6950 | void |
| 6951 | QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
| 6952 | const int64_t nLabel, |
| 6953 | const uint8_t uTagRequirement, |
| 6954 | const UsefulBuf BufferForMantissa, |
| 6955 | UsefulBufC *pMantissa, |
| 6956 | bool *pbIsNegative, |
| 6957 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6958 | { |
| 6959 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6960 | return; |
| 6961 | } |
| 6962 | |
| 6963 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6964 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6965 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6966 | return; |
| 6967 | } |
| 6968 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6969 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6970 | { |
| 6971 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6972 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6973 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6974 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6975 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6976 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 6977 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 6978 | TagSpec, |
| 6979 | &Item, |
| 6980 | BufferForMantissa, |
| 6981 | pMantissa, |
| 6982 | pbIsNegative, |
| 6983 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6984 | } |
| 6985 | |
| 6986 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6987 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6988 | * Public function, see header qcbor/qcbor_decode.h file |
| 6989 | */ |
| 6990 | void |
| 6991 | QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
| 6992 | const char *szLabel, |
| 6993 | const uint8_t uTagRequirement, |
| 6994 | const UsefulBuf BufferForMantissa, |
| 6995 | UsefulBufC *pMantissa, |
| 6996 | bool *pbIsNegative, |
| 6997 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6998 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6999 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7000 | return; |
| 7001 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7002 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7003 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7004 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 7005 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 7006 | return; |
| 7007 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7008 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7009 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7010 | { |
| 7011 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 7012 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 7013 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 7014 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7015 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7016 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame^] | 7017 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 7018 | TagSpec, |
| 7019 | &Item, |
| 7020 | BufferForMantissa, |
| 7021 | pMantissa, |
| 7022 | pbIsNegative, |
| 7023 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7024 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7025 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 7026 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |