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 | uint64_t |
| 2806 | QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2807 | const QCBORItem *pItem, |
| 2808 | uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2809 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2810 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2811 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2812 | return CBOR_TAG_INVALID64; |
| 2813 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2814 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2815 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2816 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2817 | return QCBORDecode_Private_UnMapTagNumber(pMe, pItem->uTags[uIndex]); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2818 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2819 | #else /* QCBOR_DISABLE_TAGS */ |
| 2820 | (void)pMe; |
| 2821 | (void)pItem; |
| 2822 | (void)uIndex; |
| 2823 | |
| 2824 | return CBOR_TAG_INVALID64; |
| 2825 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2826 | } |
| 2827 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2828 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2829 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2830 | * Public function, see header qcbor/qcbor_decode.h file |
| 2831 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2832 | uint64_t |
| 2833 | QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2834 | uint32_t uIndex) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2835 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2836 | #ifndef QCBOR_DISABLE_TAGS |
| 2837 | |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2838 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2839 | return CBOR_TAG_INVALID64; |
| 2840 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2841 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2842 | return CBOR_TAG_INVALID64; |
| 2843 | } else { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2844 | return QCBORDecode_Private_UnMapTagNumber(pMe, pMe->uLastTags[uIndex]); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2845 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2846 | #else /* QCBOR_DISABLE_TAGS */ |
| 2847 | (void)pMe; |
| 2848 | (void)uIndex; |
| 2849 | |
| 2850 | return CBOR_TAG_INVALID64; |
| 2851 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2852 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2853 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2854 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2855 | |
| 2856 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2857 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2858 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2859 | /* =========================================================================== |
| 2860 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2861 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2862 | This implements a simple sting allocator for indefinite-length |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2863 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2864 | implements the function type QCBORStringAllocate and allows easy |
| 2865 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2866 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2867 | This particular allocator is built-in for convenience. The caller |
| 2868 | can implement their own. All of this following code will get |
| 2869 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2870 | |
| 2871 | This is a very primitive memory allocator. It does not track |
| 2872 | individual allocations, only a high-water mark. A free or |
| 2873 | reallocation must be of the last chunk allocated. |
| 2874 | |
| 2875 | The size of the pool and offset to free memory are packed into the |
| 2876 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2877 | the decode context. Since the address of the pool may not be |
| 2878 | aligned, they have to be packed and unpacked as if they were |
| 2879 | serialized data of the wire or such. |
| 2880 | |
| 2881 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2882 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2883 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2884 | |
| 2885 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2886 | static int |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2887 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2888 | { |
| 2889 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 2890 | UsefulInputBuf UIB; |
| 2891 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2892 | // Just assume the size here. It was checked during SetUp so |
| 2893 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2894 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2895 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 2896 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 2897 | return UsefulInputBuf_GetError(&UIB); |
| 2898 | } |
| 2899 | |
| 2900 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2901 | static int |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2902 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2903 | { |
| 2904 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 2905 | // length check performed here is useful. |
| 2906 | UsefulOutBuf UOB; |
| 2907 | |
| 2908 | UsefulOutBuf_Init(&UOB, Pool); |
| 2909 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 2910 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 2911 | return UsefulOutBuf_GetError(&UOB); |
| 2912 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2913 | |
| 2914 | |
| 2915 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2916 | Internal function for an allocation, reallocation free and destuct. |
| 2917 | |
| 2918 | Having only one function rather than one each per mode saves space in |
| 2919 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2920 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2921 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 2922 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 2923 | static UsefulBuf |
| 2924 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2925 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2926 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2927 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2928 | uint32_t uPoolSize; |
| 2929 | uint32_t uFreeOffset; |
| 2930 | |
| 2931 | if(uNewSize > UINT32_MAX) { |
| 2932 | // This allocator is only good up to 4GB. This check should |
| 2933 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 2934 | goto Done; |
| 2935 | } |
| 2936 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 2937 | |
| 2938 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 2939 | goto Done; |
| 2940 | } |
| 2941 | |
| 2942 | if(uNewSize) { |
| 2943 | if(pMem) { |
| 2944 | // REALLOCATION MODE |
| 2945 | // Calculate pointer to the end of the memory pool. It is |
| 2946 | // assumed that pPool + uPoolSize won't wrap around by |
| 2947 | // assuming the caller won't pass a pool buffer in that is |
| 2948 | // not in legitimate memory space. |
| 2949 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 2950 | |
| 2951 | // Check that the pointer for reallocation is in the range of the |
| 2952 | // pool. This also makes sure that pointer math further down |
| 2953 | // doesn't wrap under or over. |
| 2954 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 2955 | // Offset to start of chunk for reallocation. This won't |
| 2956 | // wrap under because of check that pMem >= pPool. Cast |
| 2957 | // is safe because the pool is always less than UINT32_MAX |
| 2958 | // because of check in QCBORDecode_SetMemPool(). |
| 2959 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2960 | |
| 2961 | // Check to see if the allocation will fit. uPoolSize - |
| 2962 | // uMemOffset will not wrap under because of check that |
| 2963 | // pMem is in the range of the uPoolSize by check above. |
| 2964 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 2965 | ReturnValue.ptr = pMem; |
| 2966 | ReturnValue.len = uNewSize; |
| 2967 | |
| 2968 | // Addition won't wrap around over because uNewSize was |
| 2969 | // checked to be sure it is less than the pool size. |
| 2970 | uFreeOffset = uMemOffset + uNewSize32; |
| 2971 | } |
| 2972 | } |
| 2973 | } else { |
| 2974 | // ALLOCATION MODE |
| 2975 | // uPoolSize - uFreeOffset will not underflow because this |
| 2976 | // pool implementation makes sure uFreeOffset is always |
| 2977 | // smaller than uPoolSize through this check here and |
| 2978 | // reallocation case. |
| 2979 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 2980 | ReturnValue.len = uNewSize; |
| 2981 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 2982 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2983 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2984 | } |
| 2985 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2986 | if(pMem) { |
| 2987 | // FREE MODE |
| 2988 | // Cast is safe because of limit on pool size in |
| 2989 | // QCBORDecode_SetMemPool() |
| 2990 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 2991 | } else { |
| 2992 | // DESTRUCT MODE |
| 2993 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2994 | } |
| 2995 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2996 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2997 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 2998 | MemPool_Pack(Pool, uFreeOffset); |
| 2999 | |
| 3000 | Done: |
| 3001 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3002 | } |
| 3003 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3004 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3005 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3006 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3007 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3008 | QCBORError |
| 3009 | QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 3010 | UsefulBuf Pool, |
| 3011 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3012 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3013 | // The pool size and free mem offset are packed into the beginning |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3014 | // of the pool memory. This compile time check makes sure the |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3015 | // constant in the header is correct. This check should optimize |
| 3016 | // down to nothing. |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3017 | #ifdef _MSC_VER |
| 3018 | #pragma warning(push) |
| 3019 | #pragma warning(disable:4127) // conditional expression is constant |
| 3020 | #endif |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3021 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3022 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3023 | } |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3024 | #ifdef _MSC_VER |
| 3025 | #pragma warning(pop) |
| 3026 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3027 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3028 | // The pool size and free offset packed in to the beginning of pool |
| 3029 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 3030 | // machines. |
| 3031 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3032 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3033 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3034 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3035 | // This checks that the pool buffer given is big enough. |
| 3036 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3037 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3038 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3039 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 3040 | QCBORDecode_SetUpAllocator(pMe, MemPool_Function, Pool.ptr, bAllStrings); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3041 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3042 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3043 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 3044 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3045 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3046 | |
| 3047 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3048 | static void |
| 3049 | QCBORDecode_Private_CopyTags(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3050 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3051 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3052 | memcpy(pMe->uLastTags, pItem->uTags, sizeof(pItem->uTags)); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3053 | #else |
| 3054 | (void)pMe; |
| 3055 | (void)pItem; |
| 3056 | #endif |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3057 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3058 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3059 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3060 | /** |
| 3061 | * @brief Consume an entire map or array including its contents. |
| 3062 | * |
| 3063 | * @param[in] pMe The decoder context. |
| 3064 | * @param[in] pItemToConsume The array/map whose contents are to be |
| 3065 | * consumed. |
| 3066 | * @param[out] puNextNestLevel The next nesting level after the item was |
| 3067 | * fully consumed. |
| 3068 | * |
| 3069 | * This may be called when @c pItemToConsume is not an array or |
| 3070 | * map. In that case, this is just a pass through for @c puNextNestLevel |
| 3071 | * since there is nothing to do. |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3072 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3073 | static QCBORError |
| 3074 | QCBORDecode_Private_ConsumeItem(QCBORDecodeContext *pMe, |
| 3075 | const QCBORItem *pItemToConsume, |
| 3076 | uint8_t *puNextNestLevel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3077 | { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3078 | QCBORError uReturn; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3079 | QCBORItem Item; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3080 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3081 | /* 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] | 3082 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 3083 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3084 | if(QCBORItem_IsMapOrArray(*pItemToConsume) && !bIsEmpty) { |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3085 | /* 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] | 3086 | |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3087 | /* This works for definite- and indefinite-length maps and |
| 3088 | * arrays by using the nesting level |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3089 | */ |
| 3090 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3091 | uReturn = QCBORDecode_GetNext(pMe, &Item); |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3092 | if(QCBORDecode_IsUnrecoverableError(uReturn) || |
| 3093 | uReturn == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3094 | goto Done; |
| 3095 | } |
| 3096 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3097 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3098 | *puNextNestLevel = Item.uNextNestLevel; |
| 3099 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3100 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3101 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3102 | } else { |
Laurence Lundblade | af5921e | 2022-07-15 09:06:30 -0700 | [diff] [blame] | 3103 | /* pItemToConsume is not a map or array. Just pass the nesting |
| 3104 | * level through. */ |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3105 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 3106 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3107 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3108 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3109 | |
| 3110 | Done: |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3111 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3112 | } |
| 3113 | |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3114 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3115 | /* |
| 3116 | * Public function, see header qcbor/qcbor_decode.h file |
| 3117 | */ |
| 3118 | void |
| 3119 | QCBORDecode_VGetNextConsume(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3120 | { |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3121 | QCBORDecode_VGetNext(pMe, pDecodedItem); |
| 3122 | |
| 3123 | if(pMe->uLastError == QCBOR_SUCCESS) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3124 | 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] | 3125 | &pDecodedItem->uNextNestLevel); |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3130 | /** |
| 3131 | * @brief Rewind cursor to start as if map or array were just entered. |
| 3132 | * |
| 3133 | * @param[in] pMe The decoding context |
| 3134 | * |
| 3135 | * This affects the nesting tracking and the UsefulInputBuf. |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3136 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3137 | static void |
| 3138 | QCBORDecode_Private_RewindMapOrArray(QCBORDecodeContext *pMe) |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3139 | { |
| 3140 | /* Reset nesting tracking to the deepest bounded level */ |
| 3141 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 3142 | |
| 3143 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3144 | |
| 3145 | /* Reposition traversal cursor to the start of the map/array */ |
| 3146 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 3147 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
| 3148 | } |
| 3149 | |
| 3150 | |
| 3151 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3152 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3153 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3154 | void |
| 3155 | QCBORDecode_Rewind(QCBORDecodeContext *pMe) |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3156 | { |
| 3157 | if(pMe->nesting.pCurrentBounded != NULL) { |
| 3158 | /* In a bounded map, array or bstr-wrapped CBOR */ |
| 3159 | |
| 3160 | if(DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
| 3161 | /* In bstr-wrapped CBOR. */ |
| 3162 | |
| 3163 | /* Reposition traversal cursor to start of wrapping byte string */ |
| 3164 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 3165 | pMe->nesting.pCurrentBounded->u.bs.uBstrStartOffset); |
| 3166 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 3167 | |
| 3168 | } else { |
| 3169 | /* In a map or array */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3170 | QCBORDecode_Private_RewindMapOrArray(pMe); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | } else { |
| 3174 | /* Not in anything bounded */ |
| 3175 | |
| 3176 | /* Reposition traversal cursor to the start of input CBOR */ |
| 3177 | UsefulInputBuf_Seek(&(pMe->InBuf), 0ULL); |
| 3178 | |
| 3179 | /* Reset nesting tracking to beginning of input. */ |
| 3180 | DecodeNesting_Init(&(pMe->nesting)); |
| 3181 | } |
| 3182 | |
| 3183 | pMe->uLastError = QCBOR_SUCCESS; |
| 3184 | } |
| 3185 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3186 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3187 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3188 | |
| 3189 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3190 | /** |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3191 | * @brief Search a map for a set of items. |
| 3192 | * |
| 3193 | * @param[in] pMe The decode context to search. |
| 3194 | * @param[in,out] pItemArray The items to search for and the items found. |
| 3195 | * @param[out] puOffset Byte offset of last item matched. |
| 3196 | * @param[in] pCBContext Context for the not-found item call back. |
| 3197 | * @param[in] pfCallback Function to call on items not matched in pItemArray. |
| 3198 | * |
| 3199 | * @retval QCBOR_ERR_NOT_ENTERED Trying to search without entering a map. |
| 3200 | * |
| 3201 | * @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 3202 | * were found for one of the labels being |
| 3203 | * search for. This duplicate detection is |
| 3204 | * only performed for items in pItemArray, |
| 3205 | * not every item in the map. |
| 3206 | * |
| 3207 | * @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 3208 | * wrong for the matchd label. |
| 3209 | * |
| 3210 | * @retval Also errors returned by QCBORDecode_GetNext(). |
| 3211 | * |
| 3212 | * On input, \c pItemArray contains a list of labels and data types of |
| 3213 | * items to be found. |
| 3214 | * |
| 3215 | * On output, the fully retrieved items are filled in with values and |
| 3216 | * such. The label was matched, so it never changes. |
| 3217 | * |
| 3218 | * If an item was not found, its data type is set to @ref QCBOR_TYPE_NONE. |
| 3219 | * |
| 3220 | * 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] | 3221 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3222 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3223 | QCBORDecode_Private_MapSearch(QCBORDecodeContext *pMe, |
| 3224 | QCBORItem *pItemArray, |
| 3225 | size_t *puOffset, |
| 3226 | void *pCBContext, |
| 3227 | QCBORItemCallback pfCallback) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3228 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3229 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3230 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 3231 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3232 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3233 | uReturn = pMe->uLastError; |
| 3234 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3235 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3236 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3237 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3238 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 3239 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 3240 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3241 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 3242 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3243 | } |
| 3244 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3245 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 3246 | // It is an empty bounded array or map |
| 3247 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 3248 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3249 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3250 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3251 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3252 | // Nothing is ever found in an empty array or map. All items |
| 3253 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3254 | uReturn = QCBOR_SUCCESS; |
| 3255 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3256 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3257 | } |
| 3258 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3259 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3260 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 3261 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3262 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3263 | QCBORDecode_Private_RewindMapOrArray(pMe); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3264 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3265 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3266 | Loop over all the items in the map or array. Each item |
| 3267 | could be a map or array, but label matching is only at |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 3268 | the main level. This handles definite- and indefinite- |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3269 | length maps and arrays. The only reason this is ever |
| 3270 | called on arrays is to find their end position. |
| 3271 | |
| 3272 | This will always run over all items in order to do |
| 3273 | duplicate detection. |
| 3274 | |
| 3275 | This will exit with failure if it encounters an |
| 3276 | unrecoverable error, but continue on for recoverable |
| 3277 | errors. |
| 3278 | |
| 3279 | If a recoverable error occurs on a matched item, then |
| 3280 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3281 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 3282 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Máté Tóth-Pál | 2f2aa5f | 2021-05-17 21:35:09 +0200 | [diff] [blame] | 3283 | uint8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3284 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3285 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3286 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3287 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3288 | /* Get the item */ |
| 3289 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3290 | QCBORError uResult = QCBORDecode_Private_GetNextTagContent(pMe, &Item); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3291 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 3292 | /* Unrecoverable error so map can't even be decoded. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3293 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3294 | goto Done; |
| 3295 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3296 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3297 | // Unexpected end of map or array. |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3298 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3299 | goto Done; |
| 3300 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3301 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3302 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3303 | bool bMatched = false; |
| 3304 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3305 | if(QCBORItem_MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3306 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3307 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 3308 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3309 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3310 | } |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3311 | if(uResult != QCBOR_SUCCESS) { |
| 3312 | /* The label matches, but the data item is in error */ |
| 3313 | uReturn = uResult; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3314 | goto Done; |
| 3315 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3316 | if(!QCBORItem_MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3317 | /* The data item is not of the type(s) requested */ |
| 3318 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3319 | goto Done; |
| 3320 | } |
| 3321 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3322 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3323 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3324 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3325 | if(puOffset) { |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3326 | *puOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3327 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3328 | bMatched = true; |
| 3329 | } |
| 3330 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3331 | |
| 3332 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3333 | if(!bMatched && pfCallback != NULL) { |
| 3334 | /* |
| 3335 | Call the callback on unmatched labels. |
| 3336 | (It is tempting to do duplicate detection here, but that would |
| 3337 | require dynamic memory allocation because the number of labels |
| 3338 | that might be encountered is unbounded.) |
| 3339 | */ |
| 3340 | uReturn = (*pfCallback)(pCBContext, &Item); |
| 3341 | if(uReturn != QCBOR_SUCCESS) { |
| 3342 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3343 | } |
| 3344 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3345 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3346 | /* |
| 3347 | Consume the item whether matched or not. This |
| 3348 | does the work of traversing maps and array and |
| 3349 | everything in them. In this loop only the |
| 3350 | items at the current nesting level are examined |
| 3351 | to match the labels. |
| 3352 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3353 | uReturn = QCBORDecode_Private_ConsumeItem(pMe, &Item, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3354 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3355 | goto Done; |
| 3356 | } |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3357 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3358 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3359 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3360 | |
| 3361 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3362 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3363 | // Check here makes sure that this won't accidentally be |
| 3364 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3365 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3366 | // Cast to uint32_t to possibly address cases where SIZE_MAX < UINT32_MAX |
| 3367 | if((uint32_t)uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3368 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3369 | goto Done; |
| 3370 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3371 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 3372 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3373 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3374 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3375 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 3376 | |
| 3377 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3378 | /* 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] | 3379 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3380 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3381 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 3382 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3383 | } |
| 3384 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3385 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3386 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3387 | } |
| 3388 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3389 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3390 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3391 | * Public function, see header qcbor/qcbor_decode.h file |
| 3392 | */ |
| 3393 | void |
| 3394 | QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 3395 | int64_t nLabel, |
| 3396 | uint8_t uQcborType, |
| 3397 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3398 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3399 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3400 | return; |
| 3401 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3402 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3403 | QCBORItem OneItemSeach[2]; |
| 3404 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3405 | OneItemSeach[0].label.int64 = nLabel; |
| 3406 | OneItemSeach[0].uDataType = uQcborType; |
| 3407 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3408 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3409 | QCBORError uReturn = QCBORDecode_Private_MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3410 | |
| 3411 | *pItem = OneItemSeach[0]; |
| 3412 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3413 | if(uReturn != QCBOR_SUCCESS) { |
| 3414 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3415 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3416 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3417 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3418 | } |
| 3419 | |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3420 | Done: |
| 3421 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3422 | } |
| 3423 | |
| 3424 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3425 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3426 | * Public function, see header qcbor/qcbor_decode.h file |
| 3427 | */ |
| 3428 | void |
| 3429 | QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 3430 | const char *szLabel, |
| 3431 | uint8_t uQcborType, |
| 3432 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3433 | { |
Laurence Lundblade | da09597 | 2020-06-06 18:35:33 -0700 | [diff] [blame] | 3434 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3435 | return; |
| 3436 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3437 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3438 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3439 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3440 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3441 | OneItemSeach[0].uDataType = uQcborType; |
| 3442 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3443 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3444 | QCBORError uReturn = QCBORDecode_Private_MapSearch(pMe, OneItemSeach, NULL, NULL, NULL); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3445 | if(uReturn != QCBOR_SUCCESS) { |
| 3446 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3447 | } |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3448 | if(OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3449 | uReturn = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3450 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3451 | } |
| 3452 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3453 | *pItem = OneItemSeach[0]; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3454 | |
| 3455 | Done: |
| 3456 | pMe->uLastError = (uint8_t)uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3460 | /** |
| 3461 | * @brief Is a QCBOR_TYPE in the type list? |
| 3462 | * |
| 3463 | * @param[in] uDataType Type to check for. |
| 3464 | * @param[in] puTypeList List to check. |
| 3465 | * |
| 3466 | * @retval QCBOR_SUCCESS If in the list. |
| 3467 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Not in the list. |
| 3468 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3469 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3470 | QCBOR_Private_CheckTypeList(const int uDataType, |
| 3471 | const uint8_t puTypeList[QCBOR_TAGSPEC_NUM_TYPES]) |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3472 | { |
| 3473 | for(size_t i = 0; i < QCBOR_TAGSPEC_NUM_TYPES; i++) { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3474 | if(uDataType == puTypeList[i]) { /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 3475 | return QCBOR_SUCCESS; |
| 3476 | } |
| 3477 | } |
| 3478 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3479 | } |
| 3480 | |
Laurence Lundblade | 67257dc | 2020-07-27 03:33:37 -0700 | [diff] [blame] | 3481 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3482 | /** |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3483 | * Match a tag/type specification against the type of the item. |
| 3484 | * |
| 3485 | * @param[in] TagSpec Specification for matching tags. |
| 3486 | * @param[in] pItem The item to check. |
| 3487 | * |
| 3488 | * @retval QCBOR_SUCCESS \c uDataType is allowed by @c TagSpec |
| 3489 | * @retval QCBOR_ERR_UNEXPECTED_TYPE \c uDataType is not allowed by @c TagSpec |
| 3490 | * |
| 3491 | * This checks the item data type of untagged items as well as of |
| 3492 | * tagged items against a specification to see if decoding should |
| 3493 | * proceed. |
| 3494 | * |
| 3495 | * This relies on the automatic tag decoding done by QCBOR that turns |
| 3496 | * tag numbers into particular QCBOR_TYPEs so there is no actual |
| 3497 | * comparsion of tag numbers, just of QCBOR_TYPEs. |
| 3498 | * |
| 3499 | * This checks the data item type as possibly representing the tag |
| 3500 | * number or as the tag content type. |
| 3501 | * |
| 3502 | * If QCBOR_DISABLE_TAGS is #defined, this primarily checks the item |
| 3503 | * data type against the allowed tag content types. It will also error out |
| 3504 | * if the caller tries to require a tag because there is no way that can |
| 3505 | * ever be fulfilled. |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3506 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3507 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3508 | QCBOR_Private_CheckTagRequirement(const QCBOR_Private_TagSpec TagSpec, |
| 3509 | const QCBORItem *pItem) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3510 | { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3511 | const int nItemType = pItem->uDataType; /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3512 | const int nTagReq = TagSpec.uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 3513 | |
| 3514 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 3515 | /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3516 | if(!(TagSpec.uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS) && |
| 3517 | pItem->uTags[0] != CBOR_TAG_INVALID16) { |
| 3518 | /* There are tags that QCBOR couldn't process on this item and |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3519 | * the caller has told us there should not be. |
| 3520 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3521 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3522 | } |
| 3523 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3524 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3525 | /* Must match the tag number and only the tag */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3526 | return QCBOR_Private_CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3527 | } |
| 3528 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3529 | QCBORError uReturn = QCBOR_Private_CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3530 | if(uReturn == QCBOR_SUCCESS) { |
| 3531 | return QCBOR_SUCCESS; |
| 3532 | } |
| 3533 | |
| 3534 | if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 3535 | /* Must match the content type and only the content type. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3536 | * There was no match just above so it is a fail. */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3537 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3538 | } |
| 3539 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3540 | /* QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG: If here it can match either the tag or the content |
| 3541 | * and it hasn't matched the content, so the end |
| 3542 | * result is whether it matches the tag. This is |
| 3543 | * the tag optional case that the CBOR standard discourages. |
| 3544 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3545 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3546 | return QCBOR_Private_CheckTypeList(nItemType, TagSpec.uTaggedTypes); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3547 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 3548 | #else /* QCBOR_DISABLE_TAGS */ |
| 3549 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 3550 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 3551 | } |
| 3552 | |
| 3553 | return CheckTypeList(nItemType, TagSpec.uAllowedContentTypes); |
| 3554 | |
| 3555 | #endif /* QCBOR_DISABLE_TAGS */ |
| 3556 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3557 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3558 | |
| 3559 | /** |
| 3560 | * @brief Get an item by label to match a tag specification. |
| 3561 | * |
| 3562 | * @param[in] pMe The decode context. |
| 3563 | * @param[in] nLabel The label to search map for. |
| 3564 | * @param[in] TagSpec The tag number specification to match. |
| 3565 | * @param[out] pItem The item found. |
| 3566 | * |
| 3567 | * This finds the item with the given label in currently open |
| 3568 | * map. Then checks that its tag number and types matches the tag |
| 3569 | * specification. If not, an error is set in the decode context. |
| 3570 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3571 | static void |
| 3572 | QCBORDecode_GetTaggedItemInMapN(QCBORDecodeContext *pMe, |
| 3573 | const int64_t nLabel, |
| 3574 | const QCBOR_Private_TagSpec TagSpec, |
| 3575 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3576 | { |
| 3577 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
| 3578 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3579 | return; |
| 3580 | } |
| 3581 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3582 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3583 | } |
| 3584 | |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 3585 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3586 | /** |
| 3587 | * @brief Get an item by label to match a tag specification. |
| 3588 | * |
| 3589 | * @param[in] pMe The decode context. |
| 3590 | * @param[in] szLabel The label to search map for. |
| 3591 | * @param[in] TagSpec The tag number specification to match. |
| 3592 | * @param[out] pItem The item found. |
| 3593 | * |
| 3594 | * This finds the item with the given label in currently open |
| 3595 | * map. Then checks that its tag number and types matches the tag |
| 3596 | * specification. If not, an error is set in the decode context. |
| 3597 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3598 | static void |
| 3599 | QCBORDecode_GetTaggedItemInMapSZ(QCBORDecodeContext *pMe, |
| 3600 | const char *szLabel, |
| 3601 | const QCBOR_Private_TagSpec TagSpec, |
| 3602 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3603 | { |
| 3604 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
| 3605 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3606 | return; |
| 3607 | } |
| 3608 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3609 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3610 | } |
| 3611 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3612 | |
| 3613 | /** |
| 3614 | * @brief Semi-private to get an string by label to match a tag specification. |
| 3615 | * |
| 3616 | * @param[in] pMe The decode context. |
| 3617 | * @param[in] nLabel The label to search map for. |
| 3618 | * @param[in] TagSpec The tag number specification to match. |
| 3619 | * @param[out] pString The string found. |
| 3620 | * |
| 3621 | * This finds the string with the given label in currently open |
| 3622 | * map. Then checks that its tag number and types matches the tag |
| 3623 | * specification. If not, an error is set in the decode context. |
| 3624 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3625 | void |
| 3626 | QCBORDecode_Private_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3627 | const int64_t nLabel, |
| 3628 | const QCBOR_Private_TagSpec TagSpec, |
| 3629 | UsefulBufC *pString) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3630 | { |
| 3631 | QCBORItem Item; |
| 3632 | QCBORDecode_GetTaggedItemInMapN(pMe, nLabel, TagSpec, &Item); |
| 3633 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3634 | *pString = Item.val.string; |
| 3635 | } |
| 3636 | } |
| 3637 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3638 | |
| 3639 | /** |
| 3640 | * @brief Semi-private to get an string by label to match a tag specification. |
| 3641 | * |
| 3642 | * @param[in] pMe The decode context. |
| 3643 | * @param[in] szLabel The label to search map for. |
| 3644 | * @param[in] TagSpec The tag number specification to match. |
| 3645 | * @param[out] pString The string found. |
| 3646 | * |
| 3647 | * This finds the string with the given label in currently open |
| 3648 | * map. Then checks that its tag number and types matches the tag |
| 3649 | * specification. If not, an error is set in the decode context. |
| 3650 | */void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3651 | QCBORDecode_Private_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3652 | const char * szLabel, |
| 3653 | const QCBOR_Private_TagSpec TagSpec, |
| 3654 | UsefulBufC *pString) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3655 | { |
| 3656 | QCBORItem Item; |
| 3657 | QCBORDecode_GetTaggedItemInMapSZ(pMe, szLabel, TagSpec, &Item); |
| 3658 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3659 | *pString = Item.val.string; |
| 3660 | } |
| 3661 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3662 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3663 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3664 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3665 | * Public function, see header qcbor/qcbor_decode.h file |
| 3666 | */ |
| 3667 | void |
| 3668 | QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3669 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3670 | QCBORError uErr = QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, NULL, NULL); |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3671 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3672 | } |
| 3673 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3674 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3675 | * Public function, see header qcbor/qcbor_decode.h file |
| 3676 | */ |
| 3677 | void |
| 3678 | QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 3679 | QCBORItem *pItemList, |
| 3680 | void *pCallbackCtx, |
| 3681 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3682 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3683 | QCBORError uErr = QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, pCallbackCtx, pfCB); |
Laurence Lundblade | 5f53f83 | 2020-09-03 12:00:14 -0700 | [diff] [blame] | 3684 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3685 | } |
| 3686 | |
| 3687 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3688 | /** |
| 3689 | * @brief Search for a map/array by label and enter it |
| 3690 | * |
| 3691 | * @param[in] pMe The decode context. |
| 3692 | * @param[in] pSearch The map/array to search for. |
| 3693 | * |
| 3694 | * @c pSearch is expected to contain one item of type map or array |
| 3695 | * with the label specified. The current bounded map will be searched for |
| 3696 | * this and if found will be entered. |
| 3697 | * |
| 3698 | * If the label is not found, or the item found is not a map or array, |
| 3699 | * the error state is set. |
| 3700 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3701 | static void |
| 3702 | QCBORDecode_Private_SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3703 | { |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 3704 | // The first item in pSearch is the one that is to be |
| 3705 | // entered. It should be the only one filled in. Any other |
| 3706 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3707 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3708 | return; |
| 3709 | } |
| 3710 | |
| 3711 | size_t uOffset; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3712 | pMe->uLastError = (uint8_t)QCBORDecode_Private_MapSearch(pMe, pSearch, &uOffset, NULL, NULL); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3713 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3714 | return; |
| 3715 | } |
| 3716 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3717 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3718 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3719 | return; |
| 3720 | } |
| 3721 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3722 | |
| 3723 | /* The map or array was found. Now enter it. |
| 3724 | * |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3725 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 3726 | * next item for the pre-order traversal cursor to be the map/array |
| 3727 | * found by MapSearch(). The next few lines of code force the |
| 3728 | * cursor to that. |
| 3729 | * |
| 3730 | * There is no need to retain the old cursor because |
| 3731 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 3732 | * beginning of the map/array being entered. |
| 3733 | * |
| 3734 | * The cursor is forced by: 1) setting the input buffer position to |
| 3735 | * the item offset found by MapSearch(), 2) setting the map/array |
| 3736 | * counter to the total in the map/array, 3) setting the nesting |
| 3737 | * level. Setting the map/array counter to the total is not |
| 3738 | * strictly correct, but this is OK because this cursor only needs |
| 3739 | * to be used to get one item and MapSearch() has already found it |
| 3740 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3741 | */ |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3742 | UsefulInputBuf_Seek(&(pMe->InBuf), uOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3743 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 3744 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3745 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3746 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3747 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3748 | QCBORDecode_Private_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3749 | } |
| 3750 | |
| 3751 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3752 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3753 | * Public function, see header qcbor/qcbor_decode.h file |
| 3754 | */ |
| 3755 | void |
| 3756 | QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3757 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3758 | QCBORItem OneItemSeach[2]; |
| 3759 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3760 | OneItemSeach[0].label.int64 = nLabel; |
| 3761 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3762 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3763 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3764 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3765 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3769 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3770 | * Public function, see header qcbor/qcbor_decode.h file |
| 3771 | */ |
| 3772 | void |
| 3773 | QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3774 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3775 | QCBORItem OneItemSeach[2]; |
| 3776 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3777 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3778 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 3779 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3780 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3781 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3782 | } |
| 3783 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3784 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3785 | * Public function, see header qcbor/qcbor_decode.h file |
| 3786 | */ |
| 3787 | void |
| 3788 | QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3789 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3790 | QCBORItem OneItemSeach[2]; |
| 3791 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3792 | OneItemSeach[0].label.int64 = nLabel; |
| 3793 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3794 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3795 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3796 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3797 | } |
| 3798 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3799 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3800 | * Public function, see header qcbor/qcbor_decode.h file |
| 3801 | */ |
| 3802 | void |
| 3803 | QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3804 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3805 | QCBORItem OneItemSeach[2]; |
| 3806 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3807 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3808 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 3809 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3810 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3811 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3815 | /** |
| 3816 | * @brief Semi-private to do the the work for EnterMap() and EnterArray(). |
| 3817 | * |
| 3818 | * @param[in] pMe The decode context |
| 3819 | * @param[in] uType QCBOR_TYPE_MAP or QCBOR_TYPE_ARRAY. |
| 3820 | * @param[out] pItem The data item for the map or array entered. |
| 3821 | * |
| 3822 | * The next item in the traversal must be a map or array. This |
| 3823 | * consumes that item and does the book keeping to enter the map or |
| 3824 | * array. |
| 3825 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3826 | void |
| 3827 | QCBORDecode_Private_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, |
| 3828 | const uint8_t uType, |
| 3829 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3830 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3831 | QCBORError uErr; |
| 3832 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3833 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 3834 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3835 | // Already in error state; do nothing. |
| 3836 | return; |
| 3837 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3838 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3839 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 3840 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3841 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 3842 | if(uErr != QCBOR_SUCCESS) { |
| 3843 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 3844 | } |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3845 | if(Item.uDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3846 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3847 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3848 | } |
| 3849 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3850 | QCBORDecode_Private_CopyTags(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 3851 | |
| 3852 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 3853 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3854 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3855 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 3856 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3857 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3858 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 3859 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3860 | // Special case to increment nesting level for zero-length maps |
| 3861 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3862 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 3863 | } |
| 3864 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3865 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3866 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3867 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 3868 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3869 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 3870 | if(pItem != NULL) { |
| 3871 | *pItem = Item; |
| 3872 | } |
| 3873 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3874 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3875 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3876 | } |
| 3877 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3878 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3879 | /** |
| 3880 | * @brief Exit a bounded map, array or bstr (semi-private). |
| 3881 | * |
| 3882 | * @param[in] pMe Decode context. |
| 3883 | * @param[in] uEndOffset The input buffer offset of the end of item exited. |
| 3884 | * |
| 3885 | * @returns QCBOR_SUCCESS or an error code. |
| 3886 | * |
| 3887 | * This is the common work for exiting a level that is a bounded map, |
| 3888 | * array or bstr wrapped CBOR. |
| 3889 | * |
| 3890 | * One chunk of work is to set up the pre-order traversal so it is at |
| 3891 | * the item just after the bounded map, array or bstr that is being |
| 3892 | * exited. This is somewhat complex. |
| 3893 | * |
| 3894 | * The other work is to level-up the bounded mode to next higest |
| 3895 | * bounded mode or the top level if there isn't one. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3896 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3897 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3898 | QCBORDecode_Private_ExitBoundedLevel(QCBORDecodeContext *pMe, |
| 3899 | const uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3900 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3901 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3902 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3903 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3904 | * First the pre-order-traversal byte offset is positioned to the |
| 3905 | * item just after the bounded mode item that was just consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3906 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3907 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 3908 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3909 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3910 | * Next, set the current nesting level to one above the bounded |
| 3911 | * level that was just exited. |
| 3912 | * |
| 3913 | * DecodeNesting_CheckBoundedType() is always called before this |
| 3914 | * and makes sure pCurrentBounded is valid. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3915 | */ |
| 3916 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 3917 | |
| 3918 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3919 | * This does the complex work of leveling up the pre-order |
| 3920 | * traversal when the end of a map or array or another bounded |
| 3921 | * level is reached. It may do nothing, or ascend all the way to |
| 3922 | * the top level. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3923 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3924 | uErr = QCBORDecode_Private_NestLevelAscender(pMe, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3925 | if(uErr != QCBOR_SUCCESS) { |
| 3926 | goto Done; |
| 3927 | } |
| 3928 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3929 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3930 | * This makes the next highest bounded level the current bounded |
| 3931 | * level. If there is no next highest level, then no bounded mode |
| 3932 | * is in effect. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3933 | */ |
| 3934 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3935 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3936 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3937 | |
| 3938 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3939 | return uErr; |
| 3940 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3941 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3942 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3943 | /** |
| 3944 | * @brief Get started exiting a map or array (semi-private) |
| 3945 | * |
| 3946 | * @param[in] pMe The decode context |
| 3947 | * @param[in] uType QCBOR_TYPE_ARRAY or QCBOR_TYPE_MAP |
| 3948 | * |
| 3949 | * This does some work for map and array exiting (but not |
| 3950 | * bstr exiting). Then QCBORDecode_Private_ExitBoundedLevel() |
| 3951 | * is called to do the rest. |
| 3952 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3953 | void |
| 3954 | QCBORDecode_Private_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, |
| 3955 | const uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3956 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3957 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3958 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3959 | return; |
| 3960 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3961 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3962 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3963 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3964 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3965 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3966 | goto Done; |
| 3967 | } |
| 3968 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3969 | /* |
| 3970 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3971 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 3972 | from previous map search, then do a dummy search. |
| 3973 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3974 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3975 | QCBORItem Dummy; |
| 3976 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3977 | uErr = QCBORDecode_Private_MapSearch(pMe, &Dummy, NULL, NULL, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3978 | if(uErr != QCBOR_SUCCESS) { |
| 3979 | goto Done; |
| 3980 | } |
| 3981 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3982 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3983 | uErr = QCBORDecode_Private_ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3984 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3985 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3986 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3987 | } |
| 3988 | |
| 3989 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3990 | /** |
| 3991 | * @brief The main work of entering some byte-string wrapped CBOR. |
| 3992 | * |
| 3993 | * @param[in] pMe The decode context. |
| 3994 | * @param[in] pItem The byte string item. |
| 3995 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX |
| 3996 | * @param[out] pBstr Pointer and length of byte string entered. |
| 3997 | * |
| 3998 | * This is called once the byte string item has been decoded to do all |
| 3999 | * the book keeping work for descending a nesting level into the |
| 4000 | * nested CBOR. |
| 4001 | * |
| 4002 | * See QCBORDecode_EnterBstrWrapped() for details on uTagRequirement. |
| 4003 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4004 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4005 | QCBORDecode_Private_EnterBstrWrapped(QCBORDecodeContext *pMe, |
| 4006 | const QCBORItem *pItem, |
| 4007 | const uint8_t uTagRequirement, |
| 4008 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4009 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4010 | if(pBstr) { |
| 4011 | *pBstr = NULLUsefulBufC; |
| 4012 | } |
| 4013 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4014 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4015 | /* Already in error state; do nothing. */ |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4016 | return pMe->uLastError; |
| 4017 | } |
| 4018 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4019 | QCBORError uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4020 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4021 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4022 | { |
| 4023 | uTagRequirement, |
| 4024 | {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}, |
| 4025 | {QCBOR_TYPE_BYTE_STRING, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 4026 | }; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4027 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4028 | uError = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4029 | if(uError != QCBOR_SUCCESS) { |
| 4030 | goto Done; |
| 4031 | } |
| 4032 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4033 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4034 | /* Reverse the decrement done by GetNext() for the bstr so the |
| 4035 | * increment in QCBORDecode_NestLevelAscender() called by |
| 4036 | * ExitBoundedLevel() will work right. |
| 4037 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4038 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 4039 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4040 | |
| 4041 | if(pBstr) { |
| 4042 | *pBstr = pItem->val.string; |
| 4043 | } |
| 4044 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4045 | /* This saves the current length of the UsefulInputBuf and then |
| 4046 | * narrows the UsefulInputBuf to start and length of the wrapped |
| 4047 | * CBOR that is being entered. |
| 4048 | * |
| 4049 | * Most of these calls are simple inline accessors so this doesn't |
| 4050 | * amount to much code. |
| 4051 | */ |
| 4052 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 4053 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4054 | /* This check makes the cast of uPreviousLength to uint32_t below safe. */ |
| 4055 | if(uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4056 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 4057 | goto Done; |
| 4058 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4059 | |
| 4060 | const size_t uStartOfBstr = UsefulInputBuf_PointerToOffset(&(pMe->InBuf), |
| 4061 | pItem->val.string.ptr); |
| 4062 | /* This check makes the cast of uStartOfBstr to uint32_t below safe. */ |
| 4063 | if(uStartOfBstr == SIZE_MAX || uStartOfBstr > QCBOR_MAX_DECODE_INPUT_SIZE) { |
| 4064 | /* This should never happen because pItem->val.string.ptr should |
| 4065 | * always be valid since it was just returned. |
| 4066 | */ |
| 4067 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
| 4068 | goto Done; |
| 4069 | } |
| 4070 | |
| 4071 | const size_t uEndOfBstr = uStartOfBstr + pItem->val.string.len; |
| 4072 | |
| 4073 | UsefulInputBuf_Seek(&(pMe->InBuf), uStartOfBstr); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 4074 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4075 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4076 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 4077 | (uint32_t)uPreviousLength, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4078 | (uint32_t)uStartOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4079 | Done: |
| 4080 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4084 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4085 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4086 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4087 | void |
| 4088 | QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
| 4089 | const uint8_t uTagRequirement, |
| 4090 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4091 | { |
| 4092 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4093 | // Already in error state; do nothing. |
| 4094 | return; |
| 4095 | } |
| 4096 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4097 | /* Get the data item that is the byte string being entered */ |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4098 | QCBORItem Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4099 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4100 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4101 | return; |
| 4102 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4103 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4104 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4105 | &Item, |
| 4106 | uTagRequirement, |
| 4107 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4111 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4112 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4113 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4114 | void |
| 4115 | QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
| 4116 | const int64_t nLabel, |
| 4117 | const uint8_t uTagRequirement, |
| 4118 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4119 | { |
| 4120 | QCBORItem Item; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4121 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4122 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4123 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4124 | &Item, |
| 4125 | uTagRequirement, |
| 4126 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4127 | } |
| 4128 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4129 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4130 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4131 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4132 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4133 | void |
| 4134 | QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
| 4135 | const char *szLabel, |
| 4136 | const uint8_t uTagRequirement, |
| 4137 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4138 | { |
| 4139 | QCBORItem Item; |
| 4140 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 4141 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4142 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4143 | &Item, |
| 4144 | uTagRequirement, |
| 4145 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4146 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4147 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4148 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4149 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4150 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4151 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4152 | void |
| 4153 | QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4154 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4155 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4156 | // Already in error state; do nothing. |
| 4157 | return; |
| 4158 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4159 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 4160 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4161 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4162 | return; |
| 4163 | } |
| 4164 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4165 | const uint32_t uEndOfBstr = (uint32_t)UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
| 4166 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4167 | /* |
| 4168 | Reset the length of the UsefulInputBuf to what it was before |
| 4169 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4170 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 4171 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4172 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4173 | |
| 4174 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4175 | QCBORError uErr = QCBORDecode_Private_ExitBoundedLevel(pMe, uEndOfBstr); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4176 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4177 | } |
| 4178 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4179 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4180 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4181 | /** |
| 4182 | * @brief Process simple type true and false, a boolean |
| 4183 | * |
| 4184 | * @param[in] pMe The decode context. |
| 4185 | * @param[in] pItem The item with either true or false. |
| 4186 | * @param[out] pBool The boolean value output. |
| 4187 | * |
| 4188 | * Sets the internal error if the item isn't a true or a false. Also |
| 4189 | * records any tag numbers as the tag numbers of the last item. |
| 4190 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4191 | static void |
| 4192 | QCBORDecode_Private_ProcessBool(QCBORDecodeContext *pMe, |
| 4193 | const QCBORItem *pItem, |
| 4194 | bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4195 | { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4196 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4197 | /* Already in error state, do nothing */ |
| 4198 | return; |
| 4199 | } |
| 4200 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4201 | switch(pItem->uDataType) { |
| 4202 | case QCBOR_TYPE_TRUE: |
| 4203 | *pBool = true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4204 | break; |
| 4205 | |
| 4206 | case QCBOR_TYPE_FALSE: |
| 4207 | *pBool = false; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4208 | break; |
| 4209 | |
| 4210 | default: |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4211 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4212 | break; |
| 4213 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4214 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4215 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4216 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4217 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4218 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4219 | * Public function, see header qcbor/qcbor_decode.h file |
| 4220 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4221 | void |
| 4222 | QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4223 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4224 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4225 | /* Already in error state, do nothing */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4226 | return; |
| 4227 | } |
| 4228 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4229 | QCBORItem Item; |
| 4230 | |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4231 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4232 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4233 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4234 | } |
| 4235 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4236 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4237 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4238 | * Public function, see header qcbor/qcbor_decode.h file |
| 4239 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4240 | void |
| 4241 | QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, |
| 4242 | const int64_t nLabel, |
| 4243 | bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4244 | { |
| 4245 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4246 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4247 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4248 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4249 | } |
| 4250 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4251 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4252 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4253 | * Public function, see header qcbor/qcbor_decode.h file |
| 4254 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4255 | void |
| 4256 | QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, |
| 4257 | const char *szLabel, |
| 4258 | bool *pValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4259 | { |
| 4260 | QCBORItem Item; |
| 4261 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 4262 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4263 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | |
| 4267 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4268 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4269 | /** |
| 4270 | * @brief Common processing for an epoch date. |
| 4271 | * |
| 4272 | * @param[in] pMe The decode context. |
| 4273 | * @param[in] pItem The item with the date. |
| 4274 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4275 | * @param[out] pnTime The returned date. |
| 4276 | * |
| 4277 | * Common processing for the date tag. Mostly make sure the tag |
| 4278 | * content is correct and copy forward any further other tag numbers. |
| 4279 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4280 | static void |
| 4281 | QCBORDecode_Private_ProcessEpochDate(QCBORDecodeContext *pMe, |
| 4282 | QCBORItem *pItem, |
| 4283 | const uint8_t uTagRequirement, |
| 4284 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4285 | { |
| 4286 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4287 | // Already in error state, do nothing |
| 4288 | return; |
| 4289 | } |
| 4290 | |
| 4291 | QCBORError uErr; |
| 4292 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4293 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4294 | { |
| 4295 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4296 | {QCBOR_TYPE_DATE_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4297 | {QCBOR_TYPE_INT64, QCBOR_TYPE_DOUBLE, QCBOR_TYPE_FLOAT, QCBOR_TYPE_UINT64} |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4298 | }; |
| 4299 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4300 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4301 | if(uErr != QCBOR_SUCCESS) { |
| 4302 | goto Done; |
| 4303 | } |
| 4304 | |
| 4305 | if(pItem->uDataType != QCBOR_TYPE_DATE_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4306 | uErr = QCBOR_Private_DecodeDateEpoch(pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4307 | if(uErr != QCBOR_SUCCESS) { |
| 4308 | goto Done; |
| 4309 | } |
| 4310 | } |
| 4311 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4312 | // Save the tags in the last item's tags in the decode context |
| 4313 | // for QCBORDecode_GetNthTagOfLast() |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4314 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4315 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4316 | *pnTime = pItem->val.epochDate.nSeconds; |
| 4317 | |
| 4318 | Done: |
| 4319 | pMe->uLastError = (uint8_t)uErr; |
| 4320 | } |
| 4321 | |
| 4322 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4323 | |
| 4324 | /* |
| 4325 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 4326 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4327 | void |
| 4328 | QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
| 4329 | uint8_t uTagRequirement, |
| 4330 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4331 | { |
| 4332 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4333 | // Already in error state, do nothing |
| 4334 | return; |
| 4335 | } |
| 4336 | |
| 4337 | QCBORItem Item; |
| 4338 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4339 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4340 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4341 | } |
| 4342 | |
| 4343 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4344 | /* |
| 4345 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 4346 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4347 | void |
| 4348 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 4349 | int64_t nLabel, |
| 4350 | uint8_t uTagRequirement, |
| 4351 | int64_t *pnTime) |
| 4352 | { |
| 4353 | QCBORItem Item; |
| 4354 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4355 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4359 | /* |
| 4360 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 4361 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4362 | void |
| 4363 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 4364 | const char *szLabel, |
| 4365 | uint8_t uTagRequirement, |
| 4366 | int64_t *pnTime) |
| 4367 | { |
| 4368 | QCBORItem Item; |
| 4369 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4370 | QCBORDecode_Private_ProcessEpochDate(pMe, &Item, uTagRequirement, pnTime); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | |
| 4374 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4375 | /** |
| 4376 | * @brief Common processing for an epoch date. |
| 4377 | * |
| 4378 | * @param[in] pMe The decode context. |
| 4379 | * @param[in] pItem The item with the date. |
| 4380 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4381 | * @param[out] pnDays The returned day count. |
| 4382 | * |
| 4383 | * Common processing for the RFC 8943 day-count tag. Mostly make sure |
| 4384 | * the tag content is correct and copy forward any further other tag |
| 4385 | * numbers. |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4386 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4387 | static void |
| 4388 | QCBORDecode_Private_ProcessEpochDays(QCBORDecodeContext *pMe, |
| 4389 | QCBORItem *pItem, |
| 4390 | uint8_t uTagRequirement, |
| 4391 | int64_t *pnDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4392 | { |
| 4393 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4394 | /* Already in error state, do nothing */ |
| 4395 | return; |
| 4396 | } |
| 4397 | |
| 4398 | QCBORError uErr; |
| 4399 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4400 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4401 | { |
| 4402 | uTagRequirement, |
| 4403 | {QCBOR_TYPE_DAYS_EPOCH, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4404 | {QCBOR_TYPE_INT64, QCBOR_TYPE_UINT64, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
| 4405 | }; |
| 4406 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4407 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4408 | if(uErr != QCBOR_SUCCESS) { |
| 4409 | goto Done; |
| 4410 | } |
| 4411 | |
| 4412 | if(pItem->uDataType != QCBOR_TYPE_DAYS_EPOCH) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4413 | uErr = QCBOR_Private_DecodeDaysEpoch(pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4414 | if(uErr != QCBOR_SUCCESS) { |
| 4415 | goto Done; |
| 4416 | } |
| 4417 | } |
| 4418 | |
| 4419 | /* Save the tags in the last item's tags in the decode context |
| 4420 | * for QCBORDecode_GetNthTagOfLast() |
| 4421 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4422 | QCBORDecode_Private_CopyTags(pMe, pItem); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4423 | |
| 4424 | *pnDays = pItem->val.epochDays; |
| 4425 | |
| 4426 | Done: |
| 4427 | pMe->uLastError = (uint8_t)uErr; |
| 4428 | } |
| 4429 | |
| 4430 | |
| 4431 | /* |
| 4432 | * Public function, see header qcbor/qcbor_decode.h |
| 4433 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4434 | void |
| 4435 | QCBORDecode_GetEpochDays(QCBORDecodeContext *pMe, |
| 4436 | uint8_t uTagRequirement, |
| 4437 | int64_t *pnDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4438 | { |
| 4439 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4440 | /* Already in error state, do nothing */ |
| 4441 | return; |
| 4442 | } |
| 4443 | |
| 4444 | QCBORItem Item; |
| 4445 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, &Item); |
| 4446 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4447 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | |
| 4451 | /* |
| 4452 | * Public function, see header qcbor/qcbor_decode.h |
| 4453 | */ |
| 4454 | void |
| 4455 | QCBORDecode_GetEpochDaysInMapN(QCBORDecodeContext *pMe, |
| 4456 | int64_t nLabel, |
| 4457 | uint8_t uTagRequirement, |
| 4458 | int64_t *pnDays) |
| 4459 | { |
| 4460 | QCBORItem Item; |
| 4461 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4462 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4463 | } |
| 4464 | |
| 4465 | |
| 4466 | /* |
| 4467 | * Public function, see header qcbor/qcbor_decode.h |
| 4468 | */ |
| 4469 | void |
| 4470 | QCBORDecode_GetEpochDaysInMapSZ(QCBORDecodeContext *pMe, |
| 4471 | const char *szLabel, |
| 4472 | uint8_t uTagRequirement, |
| 4473 | int64_t *pnDays) |
| 4474 | { |
| 4475 | QCBORItem Item; |
| 4476 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4477 | QCBORDecode_Private_ProcessEpochDays(pMe, &Item, uTagRequirement, pnDays); |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 4478 | } |
| 4479 | |
| 4480 | |
| 4481 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4482 | /* |
| 4483 | * @brief Get a string that matches the type/tag specification. |
| 4484 | */ |
| 4485 | void |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4486 | QCBORDecode_Private_GetTaggedString(QCBORDecodeContext *pMe, |
| 4487 | const QCBOR_Private_TagSpec TagSpec, |
| 4488 | UsefulBufC *pBstr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4489 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4490 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4491 | /* Already in error state, do nothing */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4492 | return; |
| 4493 | } |
| 4494 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4495 | QCBORError uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4496 | QCBORItem Item; |
| 4497 | |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4498 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 4499 | if(uError != QCBOR_SUCCESS) { |
| 4500 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4501 | return; |
| 4502 | } |
| 4503 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4504 | pMe->uLastError = (uint8_t)QCBOR_Private_CheckTagRequirement(TagSpec, &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4505 | |
| 4506 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 4507 | *pBstr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 4508 | } else { |
| 4509 | *pBstr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4510 | } |
| 4511 | } |
| 4512 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4513 | |
| 4514 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4515 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4516 | /** |
| 4517 | * @brief Common processing for a big number tag. |
| 4518 | * |
| 4519 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4520 | * @param[in] pItem The item with the date. |
| 4521 | * @param[out] pValue The returned big number |
| 4522 | * @param[out] pbIsNegative The returned sign of the big number. |
| 4523 | * |
| 4524 | * Common processing for the big number tag. Mostly make sure |
| 4525 | * the tag content is correct and copy forward any further other tag |
| 4526 | * numbers. |
| 4527 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4528 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4529 | QCBOR_Private_ProcessBigNum(const uint8_t uTagRequirement, |
| 4530 | const QCBORItem *pItem, |
| 4531 | UsefulBufC *pValue, |
| 4532 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4533 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4534 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4535 | { |
| 4536 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4537 | {QCBOR_TYPE_POSBIGNUM, QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4538 | {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] | 4539 | }; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4540 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4541 | QCBORError uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4542 | if(uErr != QCBOR_SUCCESS) { |
| 4543 | return uErr; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4544 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4545 | |
| 4546 | *pValue = pItem->val.string; |
| 4547 | |
| 4548 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 4549 | *pbIsNegative = false; |
| 4550 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 4551 | *pbIsNegative = true; |
| 4552 | } |
| 4553 | |
| 4554 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4558 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4559 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4560 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4561 | void |
| 4562 | QCBORDecode_GetBignum(QCBORDecodeContext *pMe, |
| 4563 | const uint8_t uTagRequirement, |
| 4564 | UsefulBufC *pValue, |
| 4565 | bool *pbIsNegative) |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4566 | { |
| 4567 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4568 | // Already in error state, do nothing |
| 4569 | return; |
| 4570 | } |
| 4571 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4572 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4573 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 4574 | if(uError != QCBOR_SUCCESS) { |
| 4575 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4576 | return; |
| 4577 | } |
| 4578 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4579 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4580 | &Item, |
| 4581 | pValue, |
| 4582 | pbIsNegative); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4583 | } |
| 4584 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4585 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4586 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4587 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
| 4588 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4589 | void |
| 4590 | QCBORDecode_GetBignumInMapN(QCBORDecodeContext *pMe, |
| 4591 | const int64_t nLabel, |
| 4592 | const uint8_t uTagRequirement, |
| 4593 | UsefulBufC *pValue, |
| 4594 | bool *pbIsNegative) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4595 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4596 | QCBORItem Item; |
| 4597 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4598 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4599 | return; |
| 4600 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4601 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4602 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4603 | &Item, |
| 4604 | pValue, |
| 4605 | pbIsNegative); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4606 | } |
| 4607 | |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4608 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4609 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4610 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4611 | */ |
| 4612 | void |
| 4613 | QCBORDecode_GetBignumInMapSZ(QCBORDecodeContext *pMe, |
| 4614 | const char *szLabel, |
| 4615 | const uint8_t uTagRequirement, |
| 4616 | UsefulBufC *pValue, |
| 4617 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4618 | { |
| 4619 | QCBORItem Item; |
| 4620 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 2f5e16d | 2020-08-04 20:35:23 -0700 | [diff] [blame] | 4621 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4622 | return; |
| 4623 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4624 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4625 | pMe->uLastError = (uint8_t)QCBOR_Private_ProcessBigNum(uTagRequirement, |
| 4626 | &Item, |
| 4627 | pValue, |
| 4628 | pbIsNegative); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | |
| 4632 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4633 | /** |
| 4634 | * @brief Common processing for MIME tag (semi-private). |
| 4635 | * |
| 4636 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 4637 | * @param[in] pItem The item with the date. |
| 4638 | * @param[out] pMessage The returned MIME message. |
| 4639 | * @param[out] pbIsTag257 If true, binary MIME, if not, text MIME. |
| 4640 | * |
| 4641 | * Common processing for the MIME tag. Mostly make sure the tag |
| 4642 | * content is correct and copy forward any further other tag |
| 4643 | * numbers. See QCBORDecode_GetMIMEMessage(). |
| 4644 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4645 | QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4646 | QCBORDecode_Private_GetMIME(const uint8_t uTagRequirement, |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4647 | const QCBORItem *pItem, |
| 4648 | UsefulBufC *pMessage, |
| 4649 | bool *pbIsTag257) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4650 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4651 | const QCBOR_Private_TagSpec TagSpecText = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4652 | { |
| 4653 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4654 | {QCBOR_TYPE_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4655 | {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] | 4656 | }; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4657 | const QCBOR_Private_TagSpec TagSpecBinary = |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4658 | { |
| 4659 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 4660 | {QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE}, |
| 4661 | {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] | 4662 | }; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 4663 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4664 | QCBORError uReturn; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4665 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4666 | if(QCBOR_Private_CheckTagRequirement(TagSpecText, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4667 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4668 | if(pbIsTag257 != NULL) { |
| 4669 | *pbIsTag257 = false; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4670 | } |
| 4671 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4672 | } else if(QCBOR_Private_CheckTagRequirement(TagSpecBinary, pItem) == QCBOR_SUCCESS) { |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4673 | *pMessage = pItem->val.string; |
Laurence Lundblade | 4982f41 | 2020-09-18 23:02:18 -0700 | [diff] [blame] | 4674 | if(pbIsTag257 != NULL) { |
| 4675 | *pbIsTag257 = true; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4676 | } |
| 4677 | uReturn = QCBOR_SUCCESS; |
| 4678 | |
| 4679 | } else { |
| 4680 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
| 4681 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4682 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4683 | return uReturn; |
| 4684 | } |
| 4685 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4686 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 4687 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4688 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 4689 | |
| 4690 | |
| 4691 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4692 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4693 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4694 | /** |
| 4695 | * @brief Prototype for conversion of exponent and mantissa to unsigned integer. |
| 4696 | * |
| 4697 | * @param[in] uMantissa The mantissa. |
| 4698 | * @param[in] nExponent The exponent. |
| 4699 | * @param[out] puResult The resulting integer. |
| 4700 | * |
| 4701 | * Concrete implementations of this are for exponent base 10 and 2 supporting |
| 4702 | * decimal fractions and big floats. |
| 4703 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4704 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4705 | |
| 4706 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4707 | /** |
| 4708 | * @brief Base 10 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 4709 | * |
| 4710 | * @param[in] uMantissa The unsigned integer mantissa. |
| 4711 | * @param[in] nExponent The signed integer exponent. |
| 4712 | * @param[out] puResult Place to return the unsigned integer result. |
| 4713 | * |
| 4714 | * This computes: mantissa * 10 ^^ exponent as for a decimal fraction. The output is a 64-bit |
| 4715 | * unsigned integer. |
| 4716 | * |
| 4717 | * There are many inputs for which the result will not fit in the |
| 4718 | * 64-bit integer and \ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
| 4719 | * be returned. |
| 4720 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4721 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4722 | QCBOR_Private_Exponentitate10(const uint64_t uMantissa, |
| 4723 | int64_t nExponent, |
| 4724 | uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4725 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4726 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4727 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4728 | if(uResult != 0) { |
| 4729 | /* This loop will run a maximum of 19 times because |
| 4730 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 4731 | * exit with the overflow error |
| 4732 | */ |
| 4733 | for(; nExponent > 0; nExponent--) { |
| 4734 | if(uResult > UINT64_MAX / 10) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4735 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4736 | } |
| 4737 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4738 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4739 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4740 | for(; nExponent < 0; nExponent++) { |
| 4741 | uResult = uResult / 10; |
| 4742 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4743 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4744 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4745 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4746 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4747 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4748 | |
| 4749 | *puResult = uResult; |
| 4750 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4751 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4755 | /** |
| 4756 | * @brief Base 2 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 4757 | * |
| 4758 | * @param[in] uMantissa The unsigned integer mantissa. |
| 4759 | * @param[in] nExponent The signed integer exponent. |
| 4760 | * @param[out] puResult Place to return the unsigned integer result. |
| 4761 | * |
| 4762 | * This computes: mantissa * 2 ^^ exponent as for a big float. The |
| 4763 | * output is a 64-bit unsigned integer. |
| 4764 | * |
| 4765 | * There are many inputs for which the result will not fit in the |
| 4766 | * 64-bit integer and \ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
| 4767 | * be returned. |
| 4768 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4769 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4770 | QCBOR_Private_Exponentitate2(const uint64_t uMantissa, |
| 4771 | int64_t nExponent, |
| 4772 | uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4773 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4774 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4775 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4776 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4777 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4778 | /* This loop will run a maximum of 64 times because INT64_MAX < |
| 4779 | * 2^31. More than that will cause exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4780 | */ |
| 4781 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4782 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4783 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4784 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4785 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4786 | nExponent--; |
| 4787 | } |
| 4788 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4789 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4790 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4791 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4792 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4793 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4794 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4795 | } |
| 4796 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 4797 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4798 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4799 | return QCBOR_SUCCESS; |
| 4800 | } |
| 4801 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4802 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4803 | /** |
| 4804 | * @brief Exponentiate a signed mantissa and signed exponent to produce a signed result. |
| 4805 | * |
| 4806 | * @param[in] nMantissa Signed integer mantissa. |
| 4807 | * @param[in] nExponent Signed integer exponent. |
| 4808 | * @param[out] pnResult Place to put the signed integer result. |
| 4809 | * @param[in] pfExp Exponentiation function. |
| 4810 | * |
| 4811 | * @returns Error code |
| 4812 | * |
| 4813 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4814 | * produces an unsigned result. This converts the mantissa from signed |
| 4815 | * and converts the result to signed. The exponentiation function is |
| 4816 | * 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] | 4817 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4818 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4819 | QCBOR_Private_ExponentiateNN(const int64_t nMantissa, |
| 4820 | const int64_t nExponent, |
| 4821 | int64_t *pnResult, |
| 4822 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4823 | { |
| 4824 | uint64_t uResult; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4825 | uint64_t uMantissa; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4826 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4827 | /* Take the absolute value and put it into an unsigned. */ |
| 4828 | if(nMantissa >= 0) { |
| 4829 | /* Positive case is straightforward */ |
| 4830 | uMantissa = (uint64_t)nMantissa; |
| 4831 | } else if(nMantissa != INT64_MIN) { |
| 4832 | /* The common negative case. See next. */ |
| 4833 | uMantissa = (uint64_t)-nMantissa; |
| 4834 | } else { |
| 4835 | /* int64_t and uint64_t are always two's complement per the |
| 4836 | * C standard (and since QCBOR uses these it only works with |
| 4837 | * two's complement, which is pretty much universal these |
| 4838 | * days). The range of a negative two's complement integer is |
| 4839 | * one more that than a positive, so the simple code above might |
| 4840 | * not work all the time because you can't simply negate the |
| 4841 | * value INT64_MIN because it can't be represented in an |
| 4842 | * int64_t. -INT64_MIN can however be represented in a |
| 4843 | * uint64_t. Some compilers seem to recognize this case for the |
| 4844 | * above code and put the correct value in uMantissa, however |
| 4845 | * they are not required to do this by the C standard. This next |
| 4846 | * line does however work for all compilers. |
| 4847 | * |
| 4848 | * This does assume two's complement where -INT64_MIN == |
| 4849 | * INT64_MAX + 1 (which wouldn't be true for one's complement or |
| 4850 | * sign and magnitude (but we know we're using two's complement |
| 4851 | * because int64_t requires it)). |
| 4852 | * |
| 4853 | * See these, particularly the detailed commentary: |
| 4854 | * https://stackoverflow.com/questions/54915742/does-c99-mandate-a-int64-t-type-be-available-always |
| 4855 | * https://stackoverflow.com/questions/37301078/is-negating-int-min-undefined-behaviour |
| 4856 | */ |
| 4857 | uMantissa = (uint64_t)INT64_MAX+1; |
| 4858 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4859 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4860 | /* Call the exponentiator passed for either base 2 or base 10. |
| 4861 | * Here is where most of the overflow errors are caught. */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4862 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 4863 | if(uReturn) { |
| 4864 | return uReturn; |
| 4865 | } |
| 4866 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4867 | /* Convert back to the sign of the original mantissa */ |
| 4868 | if(nMantissa >= 0) { |
| 4869 | if(uResult > INT64_MAX) { |
| 4870 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4871 | } |
| 4872 | *pnResult = (int64_t)uResult; |
| 4873 | } else { |
| 4874 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 4875 | * of INT64_MIN. This assumes two's compliment representation |
| 4876 | * where INT64_MIN is one increment farther from 0 than |
| 4877 | * INT64_MAX. Trying to write -INT64_MIN doesn't work to get |
| 4878 | * this because the compiler makes it an int64_t which can't |
| 4879 | * represent -INT64_MIN. Also see above. |
| 4880 | */ |
| 4881 | if(uResult > (uint64_t)INT64_MAX+1) { |
| 4882 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 4883 | } |
| 4884 | *pnResult = -(int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4885 | } |
| 4886 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4887 | return QCBOR_SUCCESS; |
| 4888 | } |
| 4889 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4890 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4891 | /** |
| 4892 | * @brief Exponentiate an unsigned mantissa and signed exponent to produce an unsigned result. |
| 4893 | * |
| 4894 | * @param[in] nMantissa Signed integer mantissa. |
| 4895 | * @param[in] nExponent Signed integer exponent. |
| 4896 | * @param[out] puResult Place to put the signed integer result. |
| 4897 | * @param[in] pfExp Exponentiation function. |
| 4898 | * |
| 4899 | * @returns Error code |
| 4900 | * |
| 4901 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4902 | * produces an unsigned result. This errors out if the mantissa |
| 4903 | * is negative because the output is unsigned. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 4904 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4905 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4906 | QCBOR_Private_ExponentitateNU(const int64_t nMantissa, |
| 4907 | const int64_t nExponent, |
| 4908 | uint64_t *puResult, |
| 4909 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4910 | { |
| 4911 | if(nMantissa < 0) { |
| 4912 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 4913 | } |
| 4914 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4915 | /* Cast to unsigned is OK because of check for negative. |
| 4916 | * Cast to unsigned is OK because UINT64_MAX > INT64_MAX. |
| 4917 | * Exponentiation is straight forward |
| 4918 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4919 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 4920 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4921 | |
| 4922 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4923 | /** |
| 4924 | * @brief Exponentiate an usnigned mantissa and unsigned exponent to produce an unsigned result. |
| 4925 | * |
| 4926 | * @param[in] uMantissa Unsigned integer mantissa. |
| 4927 | * @param[in] nExponent Unsigned integer exponent. |
| 4928 | * @param[out] puResult Place to put the unsigned integer result. |
| 4929 | * @param[in] pfExp Exponentiation function. |
| 4930 | * |
| 4931 | * @returns Error code |
| 4932 | * |
| 4933 | * \c pfExp performs exponentiation on and unsigned mantissa and |
| 4934 | * produces an unsigned result so this is just a wrapper that does |
| 4935 | * nothing (and is likely inlined). |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4936 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4937 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4938 | QCBOR_Private_ExponentitateUU(const uint64_t uMantissa, |
| 4939 | const int64_t nExponent, |
| 4940 | uint64_t *puResult, |
| 4941 | fExponentiator pfExp) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4942 | { |
| 4943 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 4944 | } |
| 4945 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 4946 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 4947 | |
| 4948 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4949 | |
| 4950 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4951 | /** |
| 4952 | * @brief Convert a CBOR big number to a uint64_t. |
| 4953 | * |
| 4954 | * @param[in] BigNum Bytes of the big number to convert. |
| 4955 | * @param[in] uMax Maximum value allowed for the result. |
| 4956 | * @param[out] pResult Place to put the unsigned integer result. |
| 4957 | * |
| 4958 | * @returns Error code |
| 4959 | * |
| 4960 | * Many values will overflow because a big num can represent a much |
| 4961 | * larger range than uint64_t. |
| 4962 | */ |
| 4963 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4964 | QCBOR_Private_ConvertBigNumToUnsigned(const UsefulBufC BigNum, |
| 4965 | const uint64_t uMax, |
| 4966 | uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4967 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4968 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4969 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4970 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4971 | const uint8_t *pByte = BigNum.ptr; |
| 4972 | size_t uLen = BigNum.len; |
| 4973 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4974 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4975 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4976 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 4977 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4978 | } |
| 4979 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 4980 | *pResult = uResult; |
| 4981 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4982 | } |
| 4983 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 4984 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 4985 | /** |
| 4986 | * @brief Convert a CBOR postive big number to a uint64_t. |
| 4987 | * |
| 4988 | * @param[in] BigNum Bytes of the big number to convert. |
| 4989 | * @param[out] pResult Place to put the unsigned integer result. |
| 4990 | * |
| 4991 | * @returns Error code |
| 4992 | * |
| 4993 | * Many values will overflow because a big num can represent a much |
| 4994 | * larger range than uint64_t. |
| 4995 | */ |
| 4996 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4997 | QCBOR_Private_ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, |
| 4998 | uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4999 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5000 | return QCBOR_Private_ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5001 | } |
| 5002 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5003 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5004 | /** |
| 5005 | * @brief Convert a CBOR positive big number to an int64_t. |
| 5006 | * |
| 5007 | * @param[in] BigNum Bytes of the big number to convert. |
| 5008 | * @param[out] pResult Place to put the signed integer result. |
| 5009 | * |
| 5010 | * @returns Error code |
| 5011 | * |
| 5012 | * Many values will overflow because a big num can represent a much |
| 5013 | * larger range than int64_t. |
| 5014 | */ |
| 5015 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5016 | QCBOR_Private_ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, |
| 5017 | int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5018 | { |
| 5019 | uint64_t uResult; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5020 | QCBORError uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, |
| 5021 | INT64_MAX, |
| 5022 | &uResult); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5023 | if(uError) { |
| 5024 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5025 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5026 | /* Cast is safe because ConvertBigNumToUnsigned limits to INT64_MAX */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5027 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5028 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5029 | } |
| 5030 | |
| 5031 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5032 | /** |
| 5033 | * @brief Convert a CBOR negative big number to an int64_t. |
| 5034 | * |
| 5035 | * @param[in] BigNum Bytes of the big number to convert. |
| 5036 | * @param[out] pnResult Place to put the signed integer result. |
| 5037 | * |
| 5038 | * @returns Error code |
| 5039 | * |
| 5040 | * Many values will overflow because a big num can represent a much |
| 5041 | * larger range than int64_t. |
| 5042 | */ |
| 5043 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5044 | QCBOR_Private_ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, |
| 5045 | int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5046 | { |
| 5047 | uint64_t uResult; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5048 | /* The negative integer furthest from zero for a C int64_t is |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5049 | * INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 5050 | * negative number in CBOR is computed as -n - 1 where n is the |
| 5051 | * encoded integer, where n is what is in the variable BigNum. When |
| 5052 | * converting BigNum to a uint64_t, the maximum value is thus |
| 5053 | * INT64_MAX, so that when it -n - 1 is applied to it the result |
| 5054 | * will never be further from 0 than INT64_MIN. |
| 5055 | * |
| 5056 | * -n - 1 <= INT64_MIN. |
| 5057 | * -n - 1 <= -INT64_MAX - 1 |
| 5058 | * n <= INT64_MAX. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5059 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5060 | QCBORError uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, |
| 5061 | INT64_MAX, |
| 5062 | &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5063 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5064 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5065 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5066 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5067 | /* Now apply -n - 1. The cast is safe because |
| 5068 | * ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 5069 | * is the largest positive integer that an int64_t can |
| 5070 | * represent. */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5071 | *pnResult = -(int64_t)uResult - 1; |
| 5072 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5073 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5074 | } |
| 5075 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5076 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5077 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5078 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5079 | /** |
| 5080 | * @brief Convert integers and floats to an int64_t. |
| 5081 | * |
| 5082 | * @param[in] pItem The item to convert. |
| 5083 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5084 | * @param[out] pnValue The resulting converted value. |
| 5085 | * |
| 5086 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5087 | * in uConvertTypes. |
| 5088 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5089 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5090 | * or too small. |
| 5091 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5092 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5093 | QCBOR_Private_ConvertInt64(const QCBORItem *pItem, |
| 5094 | const uint32_t uConvertTypes, |
| 5095 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5096 | { |
| 5097 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5098 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5099 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5100 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5101 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5102 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 5103 | http://www.cplusplus.com/reference/cmath/llround/ |
| 5104 | */ |
| 5105 | // Not interested in FE_INEXACT |
| 5106 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5107 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 5108 | *pnValue = llround(pItem->val.dfnum); |
| 5109 | } else { |
| 5110 | *pnValue = lroundf(pItem->val.fnum); |
| 5111 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5112 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 5113 | // llround() shouldn't result in divide by zero, but catch |
| 5114 | // it here in case it unexpectedly does. Don't try to |
| 5115 | // distinguish between the various exceptions because it seems |
| 5116 | // they vary by CPU, compiler and OS. |
| 5117 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5118 | } |
| 5119 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5120 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5121 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5122 | #else |
| 5123 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5124 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5125 | break; |
| 5126 | |
| 5127 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5128 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5129 | *pnValue = pItem->val.int64; |
| 5130 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5131 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5132 | } |
| 5133 | break; |
| 5134 | |
| 5135 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5136 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5137 | if(pItem->val.uint64 < INT64_MAX) { |
| 5138 | *pnValue = pItem->val.int64; |
| 5139 | } else { |
| 5140 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5141 | } |
| 5142 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5143 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5144 | } |
| 5145 | break; |
| 5146 | |
| 5147 | default: |
| 5148 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5149 | } |
| 5150 | return QCBOR_SUCCESS; |
| 5151 | } |
| 5152 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5153 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5154 | /** |
| 5155 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5156 | * |
| 5157 | * @param[in] pMe The decode context. |
| 5158 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5159 | * @param[out] pnValue Result of the conversion. |
| 5160 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5161 | * |
| 5162 | * See QCBORDecode_GetInt64Convert(). |
| 5163 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5164 | void |
| 5165 | QCBORDecode_Private_GetInt64Convert(QCBORDecodeContext *pMe, |
| 5166 | uint32_t uConvertTypes, |
| 5167 | int64_t *pnValue, |
| 5168 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5169 | { |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 5170 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5171 | return; |
| 5172 | } |
| 5173 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5174 | QCBORError uError = QCBORDecode_GetNext(pMe, pItem); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5175 | if(uError) { |
| 5176 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5177 | return; |
| 5178 | } |
| 5179 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5180 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5181 | uConvertTypes, |
| 5182 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5183 | } |
| 5184 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5185 | /** |
| 5186 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5187 | * |
| 5188 | * @param[in] pMe The decode context. |
| 5189 | * @param[in] nLabel Label to find in map. |
| 5190 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5191 | * @param[out] pnValue Result of the conversion. |
| 5192 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5193 | * |
| 5194 | * See QCBORDecode_GetInt64ConvertInMapN(). |
| 5195 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5196 | void |
| 5197 | QCBORDecode_Private_GetInt64ConvertInMapN(QCBORDecodeContext *pMe, |
| 5198 | int64_t nLabel, |
| 5199 | uint32_t uConvertTypes, |
| 5200 | int64_t *pnValue, |
| 5201 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5202 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5203 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5204 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5205 | return; |
| 5206 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5207 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5208 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
| 5209 | uConvertTypes, |
| 5210 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5211 | } |
| 5212 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5213 | /** |
| 5214 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5215 | * |
| 5216 | * @param[in] pMe The decode context. |
| 5217 | * @param[in] szLabel Label to find in map. |
| 5218 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5219 | * @param[out] pnValue Result of the conversion. |
| 5220 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5221 | * |
| 5222 | * See QCBORDecode_GetInt64ConvertInMapSZ(). |
| 5223 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5224 | void |
| 5225 | QCBORDecode_Private_GetInt64ConvertInMapSZ(QCBORDecodeContext *pMe, |
| 5226 | const char * szLabel, |
| 5227 | uint32_t uConvertTypes, |
| 5228 | int64_t *pnValue, |
| 5229 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5230 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5231 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5232 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5233 | return; |
| 5234 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5235 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5236 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
| 5237 | uConvertTypes, |
| 5238 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5239 | } |
| 5240 | |
| 5241 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5242 | /** |
| 5243 | * @brief Convert many number types to an int64_t. |
| 5244 | * |
| 5245 | * @param[in] pItem The item to convert. |
| 5246 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5247 | * @param[out] pnValue The resulting converted value. |
| 5248 | * |
| 5249 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5250 | * in uConvertTypes. |
| 5251 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5252 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5253 | * or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5254 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5255 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5256 | QCBOR_Private_Int64ConvertAll(const QCBORItem *pItem, |
| 5257 | const uint32_t uConvertTypes, |
| 5258 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5259 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5260 | switch(pItem->uDataType) { |
| 5261 | |
| 5262 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5263 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5264 | return QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5265 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5266 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5267 | } |
| 5268 | break; |
| 5269 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5270 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5271 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5272 | return QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5273 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5274 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5275 | } |
| 5276 | break; |
| 5277 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5278 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5279 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5280 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5281 | return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5282 | pItem->val.expAndMantissa.nExponent, |
| 5283 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5284 | &QCBOR_Private_Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5285 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5286 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5287 | } |
| 5288 | break; |
| 5289 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5290 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5291 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5292 | return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5293 | pItem->val.expAndMantissa.nExponent, |
| 5294 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5295 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5296 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5297 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5298 | } |
| 5299 | break; |
| 5300 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5301 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5302 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5303 | int64_t nMantissa; |
| 5304 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5305 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5306 | if(uErr) { |
| 5307 | return uErr; |
| 5308 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5309 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5310 | pItem->val.expAndMantissa.nExponent, |
| 5311 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5312 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5313 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5314 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5315 | } |
| 5316 | break; |
| 5317 | |
| 5318 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5319 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5320 | int64_t nMantissa; |
| 5321 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5322 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5323 | if(uErr) { |
| 5324 | return uErr; |
| 5325 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5326 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5327 | pItem->val.expAndMantissa.nExponent, |
| 5328 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5329 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5330 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5331 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5332 | } |
| 5333 | break; |
| 5334 | |
| 5335 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5336 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5337 | int64_t nMantissa; |
| 5338 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5339 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5340 | if(uErr) { |
| 5341 | return uErr; |
| 5342 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5343 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5344 | pItem->val.expAndMantissa.nExponent, |
| 5345 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5346 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5347 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5348 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5349 | } |
| 5350 | break; |
| 5351 | |
| 5352 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5353 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5354 | int64_t nMantissa; |
| 5355 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5356 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5357 | if(uErr) { |
| 5358 | return uErr; |
| 5359 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5360 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5361 | pItem->val.expAndMantissa.nExponent, |
| 5362 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5363 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5364 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5365 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5366 | } |
| 5367 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5368 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5369 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5370 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5371 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5372 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5373 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5374 | |
| 5375 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5376 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5377 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5378 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5379 | void |
| 5380 | QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, |
| 5381 | const uint32_t uConvertTypes, |
| 5382 | int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5383 | { |
| 5384 | QCBORItem Item; |
| 5385 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5386 | QCBORDecode_Private_GetInt64Convert(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5387 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5388 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5389 | // The above conversion succeeded |
| 5390 | return; |
| 5391 | } |
| 5392 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5393 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5394 | // 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] | 5395 | return; |
| 5396 | } |
| 5397 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5398 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5399 | uConvertTypes, |
| 5400 | pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5401 | } |
| 5402 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5403 | |
| 5404 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5405 | * Public function, see header qcbor/qcbor_decode.h file |
| 5406 | */ |
| 5407 | void |
| 5408 | QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5409 | const int64_t nLabel, |
| 5410 | const uint32_t uConvertTypes, |
| 5411 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5412 | { |
| 5413 | QCBORItem Item; |
| 5414 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5415 | QCBORDecode_Private_GetInt64ConvertInMapN(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5416 | nLabel, |
| 5417 | uConvertTypes, |
| 5418 | pnValue, |
| 5419 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5420 | |
| 5421 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5422 | // The above conversion succeeded |
| 5423 | return; |
| 5424 | } |
| 5425 | |
| 5426 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5427 | // The above conversion failed in a way that code below can't correct |
| 5428 | return; |
| 5429 | } |
| 5430 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5431 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5432 | uConvertTypes, |
| 5433 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5434 | } |
| 5435 | |
| 5436 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5437 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5438 | * Public function, see header qcbor/qcbor_decode.h file |
| 5439 | */ |
| 5440 | void |
| 5441 | QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5442 | const char *szLabel, |
| 5443 | const uint32_t uConvertTypes, |
| 5444 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5445 | { |
| 5446 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5447 | QCBORDecode_Private_GetInt64ConvertInMapSZ(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5448 | szLabel, |
| 5449 | uConvertTypes, |
| 5450 | pnValue, |
| 5451 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5452 | |
| 5453 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5454 | // The above conversion succeeded |
| 5455 | return; |
| 5456 | } |
| 5457 | |
| 5458 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5459 | // The above conversion failed in a way that code below can't correct |
| 5460 | return; |
| 5461 | } |
| 5462 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5463 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 5464 | uConvertTypes, |
| 5465 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5469 | /** |
| 5470 | * @brief Convert many number types to an uint64_t. |
| 5471 | * |
| 5472 | * @param[in] pItem The item to convert. |
| 5473 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5474 | * @param[out] puValue The resulting converted value. |
| 5475 | * |
| 5476 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5477 | * in uConvertTypes. |
| 5478 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5479 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5480 | * or too small. |
| 5481 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5482 | static QCBORError |
| 5483 | QCBOR_Private_ConvertUInt64(const QCBORItem *pItem, |
| 5484 | const uint32_t uConvertTypes, |
| 5485 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5486 | { |
| 5487 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5488 | case QCBOR_TYPE_DOUBLE: |
| 5489 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5490 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5491 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5492 | // Can't use llround here because it will not convert values |
| 5493 | // greater than INT64_MAX and less than UINT64_MAX that |
| 5494 | // need to be converted so it is more complicated. |
| 5495 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 5496 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 5497 | if(isnan(pItem->val.dfnum)) { |
| 5498 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5499 | } else if(pItem->val.dfnum < 0) { |
| 5500 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5501 | } else { |
| 5502 | double dRounded = round(pItem->val.dfnum); |
| 5503 | // See discussion in DecodeDateEpoch() for |
| 5504 | // explanation of - 0x7ff |
| 5505 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 5506 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5507 | } |
| 5508 | *puValue = (uint64_t)dRounded; |
| 5509 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5510 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5511 | if(isnan(pItem->val.fnum)) { |
| 5512 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5513 | } else if(pItem->val.fnum < 0) { |
| 5514 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5515 | } else { |
| 5516 | float fRounded = roundf(pItem->val.fnum); |
| 5517 | // See discussion in DecodeDateEpoch() for |
| 5518 | // explanation of - 0x7ff |
| 5519 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 5520 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5521 | } |
| 5522 | *puValue = (uint64_t)fRounded; |
| 5523 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5524 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5525 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 5526 | // round() and roundf() shouldn't result in exceptions here, but |
| 5527 | // catch them to be robust and thorough. Don't try to |
| 5528 | // distinguish between the various exceptions because it seems |
| 5529 | // they vary by CPU, compiler and OS. |
| 5530 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 5531 | } |
| 5532 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5533 | } else { |
| 5534 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5535 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5536 | #else |
| 5537 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5538 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5539 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5540 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5541 | case QCBOR_TYPE_INT64: |
| 5542 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 5543 | if(pItem->val.int64 >= 0) { |
| 5544 | *puValue = (uint64_t)pItem->val.int64; |
| 5545 | } else { |
| 5546 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5547 | } |
| 5548 | } else { |
| 5549 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5550 | } |
| 5551 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5552 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5553 | case QCBOR_TYPE_UINT64: |
| 5554 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 5555 | *puValue = pItem->val.uint64; |
| 5556 | } else { |
| 5557 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5558 | } |
| 5559 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5560 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5561 | default: |
| 5562 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5563 | } |
| 5564 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5565 | return QCBOR_SUCCESS; |
| 5566 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5567 | |
| 5568 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5569 | /** |
| 5570 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 5571 | * |
| 5572 | * @param[in] pMe The decode context. |
| 5573 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5574 | * @param[out] puValue Result of the conversion. |
| 5575 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5576 | * |
| 5577 | * See QCBORDecode_GetUInt64Convert(). |
| 5578 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5579 | void |
| 5580 | QCBORDecode_Private_GetUInt64Convert(QCBORDecodeContext *pMe, |
| 5581 | const uint32_t uConvertTypes, |
| 5582 | uint64_t *puValue, |
| 5583 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5584 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5585 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5586 | return; |
| 5587 | } |
| 5588 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5589 | QCBORItem Item; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5590 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5591 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 5592 | if(uError) { |
| 5593 | pMe->uLastError = (uint8_t)uError; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5594 | return; |
| 5595 | } |
| 5596 | |
Laurence Lundblade | a826c50 | 2020-05-10 21:07:00 -0700 | [diff] [blame] | 5597 | if(pItem) { |
| 5598 | *pItem = Item; |
| 5599 | } |
| 5600 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5601 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(&Item, |
| 5602 | uConvertTypes, |
| 5603 | puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5604 | } |
| 5605 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5606 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5607 | /** |
| 5608 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 5609 | * |
| 5610 | * @param[in] pMe The decode context. |
| 5611 | * @param[in] nLabel Label to find in map. |
| 5612 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5613 | * @param[out] puValue Result of the conversion. |
| 5614 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5615 | * |
| 5616 | * See QCBORDecode_GetUInt64ConvertInMapN(). |
| 5617 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5618 | void |
| 5619 | QCBORDecode_Private_GetUInt64ConvertInMapN(QCBORDecodeContext *pMe, |
| 5620 | const int64_t nLabel, |
| 5621 | const uint32_t uConvertTypes, |
| 5622 | uint64_t *puValue, |
| 5623 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5624 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5625 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5626 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5627 | return; |
| 5628 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5629 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5630 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem, |
| 5631 | uConvertTypes, |
| 5632 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5636 | /** |
| 5637 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 5638 | * |
| 5639 | * @param[in] pMe The decode context. |
| 5640 | * @param[in] szLabel Label to find in map. |
| 5641 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5642 | * @param[out] puValue Result of the conversion. |
| 5643 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5644 | * |
| 5645 | * See QCBORDecode_GetUInt64ConvertInMapSZ(). |
| 5646 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5647 | void |
| 5648 | QCBORDecode_Private_GetUInt64ConvertInMapSZ(QCBORDecodeContext *pMe, |
| 5649 | const char *szLabel, |
| 5650 | const uint32_t uConvertTypes, |
| 5651 | uint64_t *puValue, |
| 5652 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5653 | { |
| 5654 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5655 | return; |
| 5656 | } |
| 5657 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5658 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5659 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5660 | return; |
| 5661 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5662 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5663 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem, |
| 5664 | uConvertTypes, |
| 5665 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5666 | } |
| 5667 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5668 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5669 | /** |
| 5670 | * @brief Convert many number types to an unt64_t. |
| 5671 | * |
| 5672 | * @param[in] pItem The item to convert. |
| 5673 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5674 | * @param[out] puValue The resulting converted value. |
| 5675 | * |
| 5676 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5677 | * in uConvertTypes. |
| 5678 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5679 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5680 | * or too small. |
| 5681 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5682 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5683 | QCBOR_Private_UInt64ConvertAll(const QCBORItem *pItem, |
| 5684 | const uint32_t uConvertTypes, |
| 5685 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5686 | { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 5687 | switch(pItem->uDataType) { /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5688 | |
| 5689 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5690 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5691 | return QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5692 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5693 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5694 | } |
| 5695 | break; |
| 5696 | |
| 5697 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5698 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5699 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5700 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5701 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5702 | } |
| 5703 | break; |
| 5704 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5705 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5706 | |
| 5707 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5708 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5709 | return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5710 | pItem->val.expAndMantissa.nExponent, |
| 5711 | puValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5712 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5713 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5714 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5715 | } |
| 5716 | break; |
| 5717 | |
| 5718 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5719 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5720 | return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5721 | pItem->val.expAndMantissa.nExponent, |
| 5722 | puValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5723 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5724 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5725 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5726 | } |
| 5727 | break; |
| 5728 | |
| 5729 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5730 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5731 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5732 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5733 | uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5734 | if(uErr != QCBOR_SUCCESS) { |
| 5735 | return uErr; |
| 5736 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5737 | return QCBOR_Private_ExponentitateUU(uMantissa, |
| 5738 | pItem->val.expAndMantissa.nExponent, |
| 5739 | puValue, |
| 5740 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5741 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5742 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5743 | } |
| 5744 | break; |
| 5745 | |
| 5746 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5747 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5748 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5749 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5750 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5751 | } |
| 5752 | break; |
| 5753 | |
| 5754 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5755 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5756 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5757 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5758 | uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, |
| 5759 | &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5760 | if(uErr != QCBOR_SUCCESS) { |
| 5761 | return uErr; |
| 5762 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5763 | return QCBOR_Private_ExponentitateUU(uMantissa, |
| 5764 | pItem->val.expAndMantissa.nExponent, |
| 5765 | puValue, |
| 5766 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5767 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5768 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5769 | } |
| 5770 | break; |
| 5771 | |
| 5772 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5773 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5774 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5775 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5776 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5777 | } |
| 5778 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5779 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5780 | default: |
| 5781 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5782 | } |
| 5783 | } |
| 5784 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5785 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 5786 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5787 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5788 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5789 | void |
| 5790 | QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, |
| 5791 | const uint32_t uConvertTypes, |
| 5792 | uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5793 | { |
| 5794 | QCBORItem Item; |
| 5795 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5796 | QCBORDecode_Private_GetUInt64Convert(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5797 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 5798 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5799 | // The above conversion succeeded |
| 5800 | return; |
| 5801 | } |
| 5802 | |
| 5803 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5804 | // 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] | 5805 | return; |
| 5806 | } |
| 5807 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5808 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 5809 | uConvertTypes, |
| 5810 | puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5811 | } |
| 5812 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5813 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5814 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5815 | * Public function, see header qcbor/qcbor_decode.h file |
| 5816 | */ |
| 5817 | void |
| 5818 | QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 5819 | const int64_t nLabel, |
| 5820 | const uint32_t uConvertTypes, |
| 5821 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5822 | { |
| 5823 | QCBORItem Item; |
| 5824 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5825 | QCBORDecode_Private_GetUInt64ConvertInMapN(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5826 | nLabel, |
| 5827 | uConvertTypes, |
| 5828 | puValue, |
| 5829 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5830 | |
| 5831 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5832 | // The above conversion succeeded |
| 5833 | return; |
| 5834 | } |
| 5835 | |
| 5836 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5837 | // The above conversion failed in a way that code below can't correct |
| 5838 | return; |
| 5839 | } |
| 5840 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5841 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 5842 | uConvertTypes, |
| 5843 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5844 | } |
| 5845 | |
| 5846 | |
| 5847 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5848 | * Public function, see header qcbor/qcbor_decode.h file |
| 5849 | */ |
| 5850 | void |
| 5851 | QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 5852 | const char *szLabel, |
| 5853 | const uint32_t uConvertTypes, |
| 5854 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5855 | { |
| 5856 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5857 | QCBORDecode_Private_GetUInt64ConvertInMapSZ(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5858 | szLabel, |
| 5859 | uConvertTypes, |
| 5860 | puValue, |
| 5861 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5862 | |
| 5863 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 5864 | // The above conversion succeeded |
| 5865 | return; |
| 5866 | } |
| 5867 | |
| 5868 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 5869 | // The above conversion failed in a way that code below can't correct |
| 5870 | return; |
| 5871 | } |
| 5872 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5873 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 5874 | uConvertTypes, |
| 5875 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5876 | } |
| 5877 | |
| 5878 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 5879 | |
| 5880 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 5881 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5882 | /** |
| 5883 | * @brief Basic conversions to a double. |
| 5884 | * |
| 5885 | * @param[in] pItem The item to convert |
| 5886 | * @param[in] uConvertTypes Bit flags indicating source types for conversion |
| 5887 | * @param[out] pdValue The value converted to a double |
| 5888 | * |
| 5889 | * This does the conversions that don't need much object code, |
| 5890 | * the conversions from int, uint and float to double. |
| 5891 | * |
| 5892 | * See QCBOR_Private_DoubleConvertAll() for the full set |
| 5893 | * of conversions. |
| 5894 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5895 | static QCBORError |
| 5896 | QCBOR_Private_ConvertDouble(const QCBORItem *pItem, |
| 5897 | const uint32_t uConvertTypes, |
| 5898 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5899 | { |
| 5900 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5901 | case QCBOR_TYPE_FLOAT: |
| 5902 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 5903 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 5904 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5905 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5906 | *pdValue = (double)pItem->val.fnum; |
| 5907 | } else { |
| 5908 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5909 | } |
| 5910 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5911 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5912 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 5913 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5914 | break; |
| 5915 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5916 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5917 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 5918 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5919 | *pdValue = pItem->val.dfnum; |
| 5920 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5921 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5922 | } |
| 5923 | } |
| 5924 | break; |
| 5925 | |
| 5926 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5927 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5928 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5929 | // A simple cast seems to do the job with no worry of exceptions. |
| 5930 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5931 | *pdValue = (double)pItem->val.int64; |
| 5932 | |
| 5933 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5934 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5935 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5936 | #else |
| 5937 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5938 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5939 | break; |
| 5940 | |
| 5941 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5942 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5943 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5944 | // A simple cast seems to do the job with no worry of exceptions. |
| 5945 | // There will be precision loss for some values. |
| 5946 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5947 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5948 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5949 | } |
| 5950 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5951 | #else |
| 5952 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5953 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5954 | |
| 5955 | default: |
| 5956 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5957 | } |
| 5958 | |
| 5959 | return QCBOR_SUCCESS; |
| 5960 | } |
| 5961 | |
| 5962 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5963 | /** |
| 5964 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 5965 | * |
| 5966 | * @param[in] pMe The decode context. |
| 5967 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 5968 | * @param[out] pdValue The output of the conversion. |
| 5969 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5970 | * |
| 5971 | * See QCBORDecode_GetDoubleConvert(). |
| 5972 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5973 | void |
| 5974 | QCBORDecode_Private_GetDoubleConvert(QCBORDecodeContext *pMe, |
| 5975 | const uint32_t uConvertTypes, |
| 5976 | double *pdValue, |
| 5977 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5978 | { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5979 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5980 | return; |
| 5981 | } |
| 5982 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5983 | QCBORError uError = QCBORDecode_GetNext(pMe, pItem); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5984 | if(uError) { |
| 5985 | pMe->uLastError = (uint8_t)uError; |
| 5986 | return; |
| 5987 | } |
| 5988 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5989 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5990 | uConvertTypes, |
| 5991 | pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 5992 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5993 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5994 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5995 | /** |
| 5996 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 5997 | * |
| 5998 | * @param[in] pMe The decode context. |
| 5999 | * @param[in] nLabel Label to find in map. |
| 6000 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 6001 | * @param[out] pdValue The output of the conversion. |
| 6002 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6003 | * |
| 6004 | * See QCBORDecode_GetDoubleConvertInMapN(). |
| 6005 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6006 | void |
| 6007 | QCBORDecode_Private_GetDoubleConvertInMapN(QCBORDecodeContext *pMe, |
| 6008 | const int64_t nLabel, |
| 6009 | const uint32_t uConvertTypes, |
| 6010 | double *pdValue, |
| 6011 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6012 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6013 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6014 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6015 | return; |
| 6016 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6017 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6018 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
| 6019 | uConvertTypes, |
| 6020 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6021 | } |
| 6022 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6023 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6024 | /** |
| 6025 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 6026 | * |
| 6027 | * @param[in] pMe The decode context. |
| 6028 | * @param[in] szLabel Label to find in map. |
| 6029 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 6030 | * @param[out] pdValue The output of the conversion. |
| 6031 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6032 | * |
| 6033 | * See QCBORDecode_GetDoubleConvertInMapSZ(). |
| 6034 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6035 | void |
| 6036 | QCBORDecode_Private_GetDoubleConvertInMapSZ(QCBORDecodeContext *pMe, |
| 6037 | const char *szLabel, |
| 6038 | const uint32_t uConvertTypes, |
| 6039 | double *pdValue, |
| 6040 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6041 | { |
| 6042 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6043 | return; |
| 6044 | } |
| 6045 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6046 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6047 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6048 | return; |
| 6049 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6050 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6051 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
| 6052 | uConvertTypes, |
| 6053 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6054 | } |
| 6055 | |
| 6056 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6057 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6058 | /** |
| 6059 | * @brief Convert a big number to double-precision float. |
| 6060 | * |
| 6061 | * @param[in] BigNum The big number to convert |
| 6062 | * |
| 6063 | * @returns The double value. |
| 6064 | * |
| 6065 | * This will always succeed. It will lose precision for larger |
| 6066 | * numbers. If the big number is too large to fit (more than |
| 6067 | * 1.7976931348623157E+308) infinity will be returned. NaN is never |
| 6068 | * returned. |
| 6069 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6070 | static double |
| 6071 | QCBOR_Private_ConvertBigNumToDouble(const UsefulBufC BigNum) |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6072 | { |
| 6073 | double dResult; |
| 6074 | |
| 6075 | dResult = 0.0; |
| 6076 | const uint8_t *pByte = BigNum.ptr; |
| 6077 | size_t uLen = BigNum.len; |
| 6078 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6079 | * is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6080 | while(uLen--) { |
| 6081 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 6082 | } |
| 6083 | |
| 6084 | return dResult; |
| 6085 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6086 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 6087 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6088 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6089 | |
| 6090 | |
| 6091 | /** |
| 6092 | * @brief Convert many number types to a double. |
| 6093 | * |
| 6094 | * @param[in] pItem The item to convert. |
| 6095 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 6096 | * @param[out] pdValue The resulting converted value. |
| 6097 | * |
| 6098 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 6099 | * in uConvertTypes. |
| 6100 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 6101 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 6102 | * or too small. |
| 6103 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6104 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6105 | QCBOR_Private_DoubleConvertAll(const QCBORItem *pItem, |
| 6106 | const uint32_t uConvertTypes, |
| 6107 | double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6108 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6109 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6110 | /* |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6111 | * What Every Computer Scientist Should Know About Floating-Point Arithmetic |
| 6112 | * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 6113 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6114 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 6115 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6116 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6117 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6118 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6119 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6120 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 6121 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 6122 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6123 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6124 | } |
| 6125 | break; |
| 6126 | |
| 6127 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6128 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6129 | // Underflow gives 0, overflow gives infinity |
| 6130 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 6131 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6132 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6133 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6134 | } |
| 6135 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6136 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6137 | |
| 6138 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6139 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6140 | *pdValue = QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6141 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6142 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6143 | } |
| 6144 | break; |
| 6145 | |
| 6146 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6147 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6148 | *pdValue = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6149 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6150 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6151 | } |
| 6152 | break; |
| 6153 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6154 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6155 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6156 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6157 | double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6158 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 6159 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6160 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6161 | } |
| 6162 | break; |
| 6163 | |
| 6164 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6165 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6166 | double dMantissa = -QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6167 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 6168 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6169 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6170 | } |
| 6171 | break; |
| 6172 | |
| 6173 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6174 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6175 | double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6176 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 6177 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6178 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6179 | } |
| 6180 | break; |
| 6181 | |
| 6182 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6183 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6184 | double dMantissa = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6185 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
| 6186 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6187 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6188 | } |
| 6189 | break; |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6190 | #endif /* ndef QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 6191 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6192 | default: |
| 6193 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6194 | } |
| 6195 | |
| 6196 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6197 | |
| 6198 | #else |
| 6199 | (void)pItem; |
| 6200 | (void)uConvertTypes; |
| 6201 | (void)pdValue; |
| 6202 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 6203 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 6204 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | |
| 6208 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6209 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6210 | */ |
| 6211 | void |
| 6212 | QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 6213 | const uint32_t uConvertTypes, |
| 6214 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6215 | { |
| 6216 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6217 | QCBORItem Item; |
| 6218 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6219 | QCBORDecode_Private_GetDoubleConvert(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6220 | |
| 6221 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6222 | // The above conversion succeeded |
| 6223 | return; |
| 6224 | } |
| 6225 | |
| 6226 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6227 | // The above conversion failed in a way that code below can't correct |
| 6228 | return; |
| 6229 | } |
| 6230 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6231 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6232 | uConvertTypes, |
| 6233 | pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6234 | } |
| 6235 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6236 | |
| 6237 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6238 | * Public function, see header qcbor/qcbor_decode.h file |
| 6239 | */ |
| 6240 | void |
| 6241 | QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 6242 | const int64_t nLabel, |
| 6243 | const uint32_t uConvertTypes, |
| 6244 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6245 | { |
| 6246 | QCBORItem Item; |
| 6247 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6248 | QCBORDecode_Private_GetDoubleConvertInMapN(pMe, |
| 6249 | nLabel, |
| 6250 | uConvertTypes, |
| 6251 | pdValue, |
| 6252 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6253 | |
| 6254 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6255 | // The above conversion succeeded |
| 6256 | return; |
| 6257 | } |
| 6258 | |
| 6259 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6260 | // The above conversion failed in a way that code below can't correct |
| 6261 | return; |
| 6262 | } |
| 6263 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6264 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6265 | uConvertTypes, |
| 6266 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6267 | } |
| 6268 | |
| 6269 | |
| 6270 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6271 | * Public function, see header qcbor/qcbor_decode.h file |
| 6272 | */ |
| 6273 | void |
| 6274 | QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 6275 | const char *szLabel, |
| 6276 | const uint32_t uConvertTypes, |
| 6277 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6278 | { |
| 6279 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6280 | QCBORDecode_Private_GetDoubleConvertInMapSZ(pMe, |
| 6281 | szLabel, |
| 6282 | uConvertTypes, |
| 6283 | pdValue, |
| 6284 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6285 | |
| 6286 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6287 | // The above conversion succeeded |
| 6288 | return; |
| 6289 | } |
| 6290 | |
| 6291 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6292 | // The above conversion failed in a way that code below can't correct |
| 6293 | return; |
| 6294 | } |
| 6295 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6296 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6297 | uConvertTypes, |
| 6298 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6299 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 6300 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6301 | |
| 6302 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6303 | |
| 6304 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6305 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6306 | /** |
| 6307 | * @brief Convert an integer to a big number |
| 6308 | * |
| 6309 | * @param[in] uInt The integer to convert. |
| 6310 | * @param[in] Buffer The buffer to output the big number to. |
| 6311 | * |
| 6312 | * @returns The big number or NULLUsefulBufC is the buffer is to small. |
| 6313 | * |
| 6314 | * This always succeeds unless the buffer is too small. |
| 6315 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6316 | static UsefulBufC |
| 6317 | QCBOR_Private_ConvertIntToBigNum(uint64_t uInt, const UsefulBuf Buffer) |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6318 | { |
| 6319 | while((uInt & 0xff00000000000000UL) == 0) { |
| 6320 | uInt = uInt << 8; |
| 6321 | }; |
| 6322 | |
| 6323 | UsefulOutBuf UOB; |
| 6324 | |
| 6325 | UsefulOutBuf_Init(&UOB, Buffer); |
| 6326 | |
| 6327 | while(uInt) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6328 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 6329 | uInt = uInt << 8; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6330 | } |
| 6331 | |
| 6332 | return UsefulOutBuf_OutUBuf(&UOB); |
| 6333 | } |
| 6334 | |
| 6335 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6336 | /** |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame^] | 6337 | * @brief Check and/or complete exponent and mantissa item. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6338 | * |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame^] | 6339 | * @param[in] pMe The decoder context. |
| 6340 | * @param[in] TagSpec Expected type(s). |
| 6341 | * @param[in,out] pItem See below. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6342 | * |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame^] | 6343 | * This is for decimal fractions and big floats, both of which are an |
| 6344 | * exponent and mantissa. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6345 | * |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame^] | 6346 | * If the item item had a tag number indicating it was a |
| 6347 | * decimal fraction or big float, then the input @c pItem will |
| 6348 | * have been decoded as exponent and mantissa. If there was |
| 6349 | * no tag number, the caller is asking this be decoded as a |
| 6350 | * big float or decimal fraction and @c pItem just has the |
| 6351 | * first item in an exponent and mantissa. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6352 | * |
| 6353 | * On output, the item is always a fully decoded decimal fraction or |
| 6354 | * big float. |
| 6355 | * |
| 6356 | * This errors out if the input type does not meet the TagSpec. |
| 6357 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6358 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6359 | QCBOR_Private_ExpMantissaTypeHandler(QCBORDecodeContext *pMe, |
| 6360 | const QCBOR_Private_TagSpec TagSpec, |
| 6361 | QCBORItem *pItem) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6362 | { |
| 6363 | QCBORError uErr; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6364 | |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame^] | 6365 | /* pItem could either be a decoded exponent and mantissa or |
| 6366 | * the opening array of an undecoded exponent and mantissa. This |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6367 | * check will succeed on either, but doesn't say which it was. |
| 6368 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6369 | uErr = QCBOR_Private_CheckTagRequirement(TagSpec, pItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6370 | if(uErr != QCBOR_SUCCESS) { |
| 6371 | goto Done; |
| 6372 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6373 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6374 | if(pItem->uDataType == QCBOR_TYPE_ARRAY) { |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame^] | 6375 | /* The item is an array, which means is is an undecoded exponent |
| 6376 | * and mantissa. This call consumes the items in the array and |
| 6377 | * results in a decoded exponent and mantissa in pItem. This is |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6378 | * the case where there was no tag. |
| 6379 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6380 | uErr = QCBORDecode_Private_ExpMantissa(pMe, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6381 | if(uErr != QCBOR_SUCCESS) { |
| 6382 | goto Done; |
| 6383 | } |
| 6384 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6385 | /* The above decode didn't determine whether it is a decimal |
| 6386 | * fraction or big num. Which of these two depends on what the |
| 6387 | * caller wants it decoded as since there is no tag, so fish the |
| 6388 | * type out of the TagSpec. */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6389 | pItem->uDataType = QCBOR_Private_ExpMantissaDataType(TagSpec.uTaggedTypes[0], pItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6390 | |
| 6391 | /* No need to check the type again. All that we need to know was |
Laurence Lundblade | 23b4ce8 | 2024-02-18 21:28:30 -0700 | [diff] [blame^] | 6392 | * that it decoded correctly as a exponent and mantissa. The |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6393 | * QCBOR type is set out by what was requested. |
| 6394 | */ |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6395 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6396 | |
| 6397 | /* If the item was not an array and the check passed, then |
| 6398 | * it is a fully decoded big float or decimal fraction and |
| 6399 | * matches what is requested. |
| 6400 | */ |
| 6401 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6402 | Done: |
| 6403 | return uErr; |
| 6404 | } |
| 6405 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6406 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6407 | /* Some notes from the work to disable tags. |
| 6408 | * |
| 6409 | * The API for big floats and decimal fractions seems good. |
| 6410 | * If there's any issue with it it's that the code size to |
| 6411 | * implement is a bit large because of the conversion |
| 6412 | * to/from int and bignum that is required. There is no API |
| 6413 | * that doesn't do the conversion so dead stripping will never |
| 6414 | * leave that code out. |
| 6415 | * |
| 6416 | * The implementation itself seems correct, but not as clean |
| 6417 | * and neat as it could be. It could probably be smaller too. |
| 6418 | * |
| 6419 | * The implementation has three main parts / functions |
| 6420 | * - The decoding of the array of two |
| 6421 | * - All the tag and type checking for the various API functions |
| 6422 | * - Conversion to/from bignum and int |
| 6423 | * |
| 6424 | * The type checking seems like it wastes the most code for |
| 6425 | * what it needs to do. |
| 6426 | * |
| 6427 | * The inlining for the conversion is probably making the |
| 6428 | * overall code base larger. |
| 6429 | * |
| 6430 | * The tests cases could be organized a lot better and be |
| 6431 | * more thorough. |
| 6432 | * |
| 6433 | * Seems also like there could be more common code in the |
| 6434 | * first tier part of the public API. Some functions only |
| 6435 | * vary by a TagSpec. |
| 6436 | */ |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6437 | |
| 6438 | /** |
| 6439 | * @brief Common processor for exponent and mantissa. |
| 6440 | * |
| 6441 | * @param[in] pMe The decode context. |
| 6442 | * @param[in] TagSpec The expected/allowed tags. |
| 6443 | * @param[in] pItem The data item to process. |
| 6444 | * @param[out] pnMantissa The returned mantissa as an int64_t. |
| 6445 | * @param[out] pnExponent The returned exponent as an int64_t. |
| 6446 | * |
| 6447 | * This handles exponent and mantissa for base 2 and 10. This |
| 6448 | * is limited to a mantissa that is an int64_t. See also |
| 6449 | * QCBORDecode_Private_ProcessExpMantissaBig(). |
| 6450 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6451 | static void |
| 6452 | QCBOR_Private_ProcessExpMantissa(QCBORDecodeContext *pMe, |
| 6453 | const QCBOR_Private_TagSpec TagSpec, |
| 6454 | QCBORItem *pItem, |
| 6455 | int64_t *pnMantissa, |
| 6456 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6457 | { |
| 6458 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6459 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6460 | uErr = QCBOR_Private_ExpMantissaTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6461 | if(uErr != QCBOR_SUCCESS) { |
| 6462 | goto Done; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6463 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6464 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6465 | switch (pItem->uDataType) { |
| 6466 | |
| 6467 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 6468 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6469 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6470 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6471 | break; |
| 6472 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6473 | #ifndef QCBOR_DISABLE_TAGS |
| 6474 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6475 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 6476 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 6477 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6478 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6479 | break; |
| 6480 | |
| 6481 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 6482 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 6483 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6484 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6485 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6486 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6487 | |
| 6488 | default: |
| 6489 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 6490 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6491 | |
| 6492 | Done: |
| 6493 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6494 | } |
| 6495 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6496 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6497 | /** |
| 6498 | * @brief Decode exponent and mantissa into a big number. |
| 6499 | * |
| 6500 | * @param[in] pMe The decode context. |
| 6501 | * @param[in] TagSpec The expected/allowed tags. |
| 6502 | * @param[in] pItem Item to decode and convert. |
| 6503 | * @param[in] BufferForMantissa Buffer to output mantissa into. |
| 6504 | * @param[out] pMantissa The output mantissa. |
| 6505 | * @param[out] pbIsNegative The sign of the output. |
| 6506 | * @param[out] pnExponent The mantissa of the output. |
| 6507 | * |
| 6508 | * This is the common processing of a decimal fraction or a big float |
| 6509 | * into a big number. This will decode and consume all the CBOR items |
| 6510 | * that make up the decimal fraction or big float. |
| 6511 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6512 | static void |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6513 | QCBORDecode_Private_ProcessExpMantissaBig(QCBORDecodeContext *pMe, |
| 6514 | const QCBOR_Private_TagSpec TagSpec, |
| 6515 | QCBORItem *pItem, |
| 6516 | const UsefulBuf BufferForMantissa, |
| 6517 | UsefulBufC *pMantissa, |
| 6518 | bool *pbIsNegative, |
| 6519 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6520 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6521 | QCBORError uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6522 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6523 | uErr = QCBOR_Private_ExpMantissaTypeHandler(pMe, TagSpec, pItem); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6524 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6525 | goto Done; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6526 | } |
| 6527 | |
| 6528 | uint64_t uMantissa; |
| 6529 | |
| 6530 | switch (pItem->uDataType) { |
| 6531 | |
| 6532 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6533 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6534 | /* See comments in ExponentiateNN() on handling INT64_MIN */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6535 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 6536 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 6537 | *pbIsNegative = false; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6538 | } else if(pItem->val.expAndMantissa.Mantissa.nInt != INT64_MIN) { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6539 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 6540 | *pbIsNegative = true; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 6541 | } else { |
| 6542 | uMantissa = (uint64_t)INT64_MAX+1; |
| 6543 | *pbIsNegative = true; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6544 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6545 | *pMantissa = QCBOR_Private_ConvertIntToBigNum(uMantissa, |
| 6546 | BufferForMantissa); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6547 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6548 | break; |
| 6549 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6550 | #ifndef QCBOR_DISABLE_TAGS |
| 6551 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6552 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6553 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6554 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6555 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 6556 | *pbIsNegative = false; |
| 6557 | break; |
| 6558 | |
| 6559 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6560 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6561 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 6562 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 6563 | *pbIsNegative = true; |
| 6564 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 6565 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6566 | |
| 6567 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6568 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6569 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6570 | |
| 6571 | Done: |
| 6572 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6573 | } |
| 6574 | |
| 6575 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6576 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6577 | * Public function, see header qcbor/qcbor_decode.h file |
| 6578 | */ |
| 6579 | void |
| 6580 | QCBORDecode_GetDecimalFraction(QCBORDecodeContext *pMe, |
| 6581 | const uint8_t uTagRequirement, |
| 6582 | int64_t *pnMantissa, |
| 6583 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6584 | { |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6585 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6586 | return; |
| 6587 | } |
| 6588 | |
| 6589 | QCBORItem Item; |
| 6590 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6591 | if(uError) { |
| 6592 | pMe->uLastError = (uint8_t)uError; |
| 6593 | return; |
| 6594 | } |
| 6595 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6596 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6597 | { |
| 6598 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6599 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6600 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6601 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6602 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6603 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6604 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6605 | TagSpec, |
| 6606 | &Item, |
| 6607 | pnMantissa, |
| 6608 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6609 | } |
| 6610 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6611 | |
| 6612 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6613 | * Public function, see header qcbor/qcbor_decode.h file |
| 6614 | */ |
| 6615 | void |
| 6616 | QCBORDecode_GetDecimalFractionInMapN(QCBORDecodeContext *pMe, |
| 6617 | const int64_t nLabel, |
| 6618 | const uint8_t uTagRequirement, |
| 6619 | int64_t *pnMantissa, |
| 6620 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6621 | { |
| 6622 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6623 | return; |
| 6624 | } |
| 6625 | |
| 6626 | QCBORItem Item; |
| 6627 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6628 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6629 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6630 | { |
| 6631 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6632 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6633 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6634 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6635 | }; |
| 6636 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6637 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6638 | TagSpec, |
| 6639 | &Item, |
| 6640 | pnMantissa, |
| 6641 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6642 | } |
| 6643 | |
| 6644 | |
| 6645 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6646 | * Public function, see header qcbor/qcbor_decode.h file |
| 6647 | */ |
| 6648 | void |
| 6649 | QCBORDecode_GetDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
| 6650 | const char *szLabel, |
| 6651 | const uint8_t uTagRequirement, |
| 6652 | int64_t *pnMantissa, |
| 6653 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6654 | { |
| 6655 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6656 | return; |
| 6657 | } |
| 6658 | |
| 6659 | QCBORItem Item; |
| 6660 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6661 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6662 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6663 | { |
| 6664 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6665 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6666 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6667 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6668 | }; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 6669 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6670 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6671 | TagSpec, |
| 6672 | &Item, |
| 6673 | pnMantissa, |
| 6674 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6675 | } |
| 6676 | |
| 6677 | |
| 6678 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6679 | * Public function, see header qcbor/qcbor_decode.h file |
| 6680 | */ |
| 6681 | void |
| 6682 | QCBORDecode_GetDecimalFractionBig(QCBORDecodeContext *pMe, |
| 6683 | const uint8_t uTagRequirement, |
| 6684 | const UsefulBuf MantissaBuffer, |
| 6685 | UsefulBufC *pMantissa, |
| 6686 | bool *pbMantissaIsNegative, |
| 6687 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6688 | { |
| 6689 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6690 | return; |
| 6691 | } |
| 6692 | |
| 6693 | QCBORItem Item; |
| 6694 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6695 | if(uError) { |
| 6696 | pMe->uLastError = (uint8_t)uError; |
| 6697 | return; |
| 6698 | } |
| 6699 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6700 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6701 | { |
| 6702 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6703 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6704 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6705 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6706 | }; |
| 6707 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6708 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 6709 | TagSpec, |
| 6710 | &Item, |
| 6711 | MantissaBuffer, |
| 6712 | pMantissa, |
| 6713 | pbMantissaIsNegative, |
| 6714 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6715 | } |
| 6716 | |
| 6717 | |
| 6718 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6719 | * Public function, see header qcbor/qcbor_decode.h file |
| 6720 | */ |
| 6721 | void |
| 6722 | QCBORDecode_GetDecimalFractionBigInMapN(QCBORDecodeContext *pMe, |
| 6723 | const int64_t nLabel, |
| 6724 | const uint8_t uTagRequirement, |
| 6725 | const UsefulBuf BufferForMantissa, |
| 6726 | UsefulBufC *pMantissa, |
| 6727 | bool *pbIsNegative, |
| 6728 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6729 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6730 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6731 | return; |
| 6732 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6733 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6734 | QCBORItem Item; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6735 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6736 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6737 | return; |
| 6738 | } |
| 6739 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6740 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6741 | { |
| 6742 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6743 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6744 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6745 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6746 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6747 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6748 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 6749 | TagSpec, |
| 6750 | &Item, |
| 6751 | BufferForMantissa, |
| 6752 | pMantissa, |
| 6753 | pbIsNegative, |
| 6754 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6755 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6756 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6757 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6758 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6759 | * Public function, see header qcbor/qcbor_decode.h file |
| 6760 | */ |
| 6761 | void |
| 6762 | QCBORDecode_GetDecimalFractionBigInMapSZ(QCBORDecodeContext *pMe, |
| 6763 | const char *szLabel, |
| 6764 | const uint8_t uTagRequirement, |
| 6765 | const UsefulBuf BufferForMantissa, |
| 6766 | UsefulBufC *pMantissa, |
| 6767 | bool *pbIsNegative, |
| 6768 | int64_t *pnExponent) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6769 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6770 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6771 | return; |
| 6772 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6773 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6774 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6775 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6776 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6777 | return; |
| 6778 | } |
| 6779 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6780 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6781 | { |
| 6782 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6783 | {QCBOR_TYPE_DECIMAL_FRACTION, QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 6784 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6785 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6786 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6787 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6788 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 6789 | TagSpec, |
| 6790 | &Item, |
| 6791 | BufferForMantissa, |
| 6792 | pMantissa, |
| 6793 | pbIsNegative, |
| 6794 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6795 | } |
| 6796 | |
| 6797 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6798 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6799 | * Public function, see header qcbor/qcbor_decode.h file |
| 6800 | */ |
| 6801 | void |
| 6802 | QCBORDecode_GetBigFloat(QCBORDecodeContext *pMe, |
| 6803 | const uint8_t uTagRequirement, |
| 6804 | int64_t *pnMantissa, |
| 6805 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6806 | { |
| 6807 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6808 | return; |
| 6809 | } |
| 6810 | |
| 6811 | QCBORItem Item; |
| 6812 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6813 | if(uError) { |
| 6814 | pMe->uLastError = (uint8_t)uError; |
| 6815 | return; |
| 6816 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6817 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6818 | { |
| 6819 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6820 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6821 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6822 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6823 | }; |
| 6824 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6825 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6826 | TagSpec, |
| 6827 | &Item, |
| 6828 | pnMantissa, |
| 6829 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6830 | } |
| 6831 | |
| 6832 | |
| 6833 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6834 | * Public function, see header qcbor/qcbor_decode.h file |
| 6835 | */ |
| 6836 | void |
| 6837 | QCBORDecode_GetBigFloatInMapN(QCBORDecodeContext *pMe, |
| 6838 | const int64_t nLabel, |
| 6839 | const uint8_t uTagRequirement, |
| 6840 | int64_t *pnMantissa, |
| 6841 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6842 | { |
| 6843 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6844 | return; |
| 6845 | } |
| 6846 | |
| 6847 | QCBORItem Item; |
| 6848 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6849 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6850 | return; |
| 6851 | } |
| 6852 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6853 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6854 | { |
| 6855 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6856 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6857 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6858 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6859 | }; |
| 6860 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6861 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6862 | TagSpec, |
| 6863 | &Item, |
| 6864 | pnMantissa, |
| 6865 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6866 | } |
| 6867 | |
| 6868 | |
| 6869 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6870 | * Public function, see header qcbor/qcbor_decode.h file |
| 6871 | */ |
| 6872 | void |
| 6873 | QCBORDecode_GetBigFloatInMapSZ(QCBORDecodeContext *pMe, |
| 6874 | const char *szLabel, |
| 6875 | const uint8_t uTagRequirement, |
| 6876 | int64_t *pnMantissa, |
| 6877 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6878 | { |
| 6879 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6880 | return; |
| 6881 | } |
| 6882 | |
| 6883 | QCBORItem Item; |
| 6884 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 6885 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6886 | return; |
| 6887 | } |
| 6888 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6889 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6890 | { |
| 6891 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6892 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6893 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6894 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6895 | }; |
| 6896 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6897 | QCBOR_Private_ProcessExpMantissa(pMe, |
| 6898 | TagSpec, |
| 6899 | &Item, |
| 6900 | pnMantissa, |
| 6901 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6902 | } |
| 6903 | |
| 6904 | |
| 6905 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6906 | * Public function, see header qcbor/qcbor_decode.h file |
| 6907 | */ |
| 6908 | void |
| 6909 | QCBORDecode_GetBigFloatBig(QCBORDecodeContext *pMe, |
| 6910 | const uint8_t uTagRequirement, |
| 6911 | const UsefulBuf MantissaBuffer, |
| 6912 | UsefulBufC *pMantissa, |
| 6913 | bool *pbMantissaIsNegative, |
| 6914 | int64_t *pnExponent) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6915 | { |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6916 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6917 | return; |
| 6918 | } |
| 6919 | |
| 6920 | QCBORItem Item; |
| 6921 | QCBORError uError = QCBORDecode_GetNext(pMe, &Item); |
| 6922 | if(uError) { |
| 6923 | pMe->uLastError = (uint8_t)uError; |
| 6924 | return; |
| 6925 | } |
| 6926 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6927 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6928 | { |
| 6929 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6930 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6931 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6932 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6933 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6934 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6935 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 6936 | TagSpec, |
| 6937 | &Item, |
| 6938 | MantissaBuffer, |
| 6939 | pMantissa, |
| 6940 | pbMantissaIsNegative, |
| 6941 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6942 | } |
| 6943 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6944 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6945 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6946 | * Public function, see header qcbor/qcbor_decode.h file |
| 6947 | */ |
| 6948 | void |
| 6949 | QCBORDecode_GetBigFloatBigInMapN(QCBORDecodeContext *pMe, |
| 6950 | const int64_t nLabel, |
| 6951 | const uint8_t uTagRequirement, |
| 6952 | const UsefulBuf BufferForMantissa, |
| 6953 | UsefulBufC *pMantissa, |
| 6954 | bool *pbIsNegative, |
| 6955 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6956 | { |
| 6957 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6958 | return; |
| 6959 | } |
| 6960 | |
| 6961 | QCBORItem Item; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6962 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
| 6963 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6964 | return; |
| 6965 | } |
| 6966 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6967 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6968 | { |
| 6969 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 6970 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 6971 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 6972 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6973 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6974 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6975 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 6976 | TagSpec, |
| 6977 | &Item, |
| 6978 | BufferForMantissa, |
| 6979 | pMantissa, |
| 6980 | pbIsNegative, |
| 6981 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 6982 | } |
| 6983 | |
| 6984 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6985 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6986 | * Public function, see header qcbor/qcbor_decode.h file |
| 6987 | */ |
| 6988 | void |
| 6989 | QCBORDecode_GetBigFloatBigInMapSZ(QCBORDecodeContext *pMe, |
| 6990 | const char *szLabel, |
| 6991 | const uint8_t uTagRequirement, |
| 6992 | const UsefulBuf BufferForMantissa, |
| 6993 | UsefulBufC *pMantissa, |
| 6994 | bool *pbIsNegative, |
| 6995 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 6996 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6997 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6998 | return; |
| 6999 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7000 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7001 | QCBORItem Item; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7002 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
| 7003 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 7004 | return; |
| 7005 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7006 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7007 | const QCBOR_Private_TagSpec TagSpec = |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7008 | { |
| 7009 | uTagRequirement, |
Laurence Lundblade | c384d4e | 2020-10-07 09:46:10 -0700 | [diff] [blame] | 7010 | {QCBOR_TYPE_BIGFLOAT, QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 7011 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, QCBOR_TYPE_NONE}, |
| 7012 | {QCBOR_TYPE_ARRAY, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE, QCBOR_TYPE_NONE} |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7013 | }; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7014 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7015 | QCBORDecode_Private_ProcessExpMantissaBig(pMe, |
| 7016 | TagSpec, |
| 7017 | &Item, |
| 7018 | BufferForMantissa, |
| 7019 | pMantissa, |
| 7020 | pbIsNegative, |
| 7021 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7022 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7023 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 7024 | #endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */ |