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 | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 37 | #include "qcbor/qcbor_tag_decode.h" |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 38 | #include "ieee754.h" /* Does not use math.h */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 39 | |
| 40 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 41 | |
| 42 | #include <math.h> /* For isnan(), llround(), llroudf(), round(), roundf(), |
| 43 | * pow(), exp2() |
| 44 | */ |
| 45 | #include <fenv.h> /* feclearexcept(), fetestexcept() */ |
| 46 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 47 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 48 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 49 | |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 50 | #if (defined(__GNUC__) && !defined(__clang__)) |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 51 | /* |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 52 | * This is how the -Wmaybe-uninitialized compiler warning is |
| 53 | * handled. It can’t be ignored because some version of gcc enable it |
| 54 | * with -Wall which is a common and useful gcc warning option. It also |
| 55 | * can’t be ignored because it is the goal of QCBOR to compile clean |
| 56 | * out of the box in all environments. |
| 57 | * |
| 58 | * The big problem with -Wmaybe-uninitialized is that it generates |
| 59 | * false positives. It complains things are uninitialized when they |
| 60 | * are not. This is because it is not a thorough static analyzer. This |
| 61 | * is why “maybe” is in its name. The problem is it is just not |
| 62 | * thorough enough to understand all the code (and someone saw fit to |
| 63 | * put it in gcc and worse to enable it with -Wall). |
| 64 | * |
| 65 | * One solution would be to change the code so -Wmaybe-uninitialized |
| 66 | * doesn’t get confused, for example adding an unnecessary extra |
| 67 | * initialization to zero. (If variables were truly uninitialized, the |
| 68 | * correct path is to understand the code thoroughly and set them to |
| 69 | * the correct value at the correct time; in essence this is already |
| 70 | * done; -Wmaybe-uninitialized just can’t tell). This path is not |
| 71 | * taken because it makes the code bigger and is kind of the tail |
| 72 | * wagging the dog. |
| 73 | * |
| 74 | * The solution here is to just use a pragma to disable it for the |
| 75 | * whole file. Disabling it for each line makes the code fairly ugly |
| 76 | * requiring #pragma to push, pop and ignore. Another reason is the |
| 77 | * warnings issues vary by version of gcc and which optimization |
| 78 | * optimizations are selected. Another reason is that compilers other |
| 79 | * than gcc don’t have -Wmaybe-uninitialized. |
| 80 | * |
| 81 | * One may ask how to be sure these warnings are false positives and |
| 82 | * not real issues. 1) The code has been read carefully to check. 2) |
| 83 | * Testing is pretty thorough. 3) This code has been run through |
| 84 | * thorough high-quality static analyzers. |
| 85 | * |
| 86 | * In particularly, most of the warnings are about |
| 87 | * Item.Item->uDataType being uninitialized. QCBORDecode_GetNext() |
| 88 | * *always* sets this value and test case confirm |
| 89 | * this. -Wmaybe-uninitialized just can't tell. |
| 90 | * |
| 91 | * https://stackoverflow.com/questions/5080848/disable-gcc-may-be-used-uninitialized-on-a-particular-variable |
| 92 | */ |
| 93 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 94 | #endif |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 95 | |
| 96 | |
| 97 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 98 | |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 99 | #define SIZEOF_C_ARRAY(array,type) (sizeof(array)/sizeof(type)) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 100 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 101 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 102 | |
| 103 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 104 | static bool |
| 105 | QCBORItem_IsMapOrArray(const QCBORItem Item) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 106 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 107 | const uint8_t uDataType = Item.uDataType; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 108 | return uDataType == QCBOR_TYPE_MAP || |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 109 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 110 | uDataType == QCBOR_TYPE_MAP_AS_ARRAY || |
| 111 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 112 | uDataType == QCBOR_TYPE_ARRAY; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 115 | static bool |
| 116 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(const QCBORItem Item) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 117 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 118 | if(!QCBORItem_IsMapOrArray(Item)){ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 122 | if(Item.val.uCount != 0) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | return true; |
| 126 | } |
| 127 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 128 | static bool |
| 129 | QCBORItem_IsIndefiniteLengthMapOrArray(const QCBORItem Item) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 130 | { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 131 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 132 | if(!QCBORItem_IsMapOrArray(Item)){ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 136 | if(Item.val.uCount != QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | return true; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 140 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 141 | (void)Item; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 142 | return false; |
| 143 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 146 | /* Return true if the labels in Item1 and Item2 are the same. |
| 147 | Works only for integer and string labels. Returns false |
| 148 | for any other type. */ |
| 149 | static bool |
| 150 | QCBORItem_MatchLabel(const QCBORItem Item1, const QCBORItem Item2) |
| 151 | { |
| 152 | if(Item1.uLabelType == QCBOR_TYPE_INT64) { |
| 153 | if(Item2.uLabelType == QCBOR_TYPE_INT64 && Item1.label.int64 == Item2.label.int64) { |
| 154 | return true; |
| 155 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 156 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 157 | } else if(Item1.uLabelType == QCBOR_TYPE_TEXT_STRING) { |
| 158 | if(Item2.uLabelType == QCBOR_TYPE_TEXT_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 159 | return true; |
| 160 | } |
| 161 | } else if(Item1.uLabelType == QCBOR_TYPE_BYTE_STRING) { |
| 162 | if(Item2.uLabelType == QCBOR_TYPE_BYTE_STRING && !UsefulBuf_Compare(Item1.label.string, Item2.label.string)) { |
| 163 | return true; |
| 164 | } |
| 165 | } else if(Item1.uLabelType == QCBOR_TYPE_UINT64) { |
| 166 | if(Item2.uLabelType == QCBOR_TYPE_UINT64 && Item1.label.uint64 == Item2.label.uint64) { |
| 167 | return true; |
| 168 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 169 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* Other label types are never matched */ |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /* |
| 178 | Returns true if Item1 and Item2 are the same type |
| 179 | or if either are of QCBOR_TYPE_ANY. |
| 180 | */ |
| 181 | static bool |
| 182 | QCBORItem_MatchType(const QCBORItem Item1, const QCBORItem Item2) |
| 183 | { |
| 184 | if(Item1.uDataType == Item2.uDataType) { |
| 185 | return true; |
| 186 | } else if(Item1.uDataType == QCBOR_TYPE_ANY) { |
| 187 | return true; |
| 188 | } else if(Item2.uDataType == QCBOR_TYPE_ANY) { |
| 189 | return true; |
| 190 | } |
| 191 | return false; |
| 192 | } |
| 193 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 194 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 195 | /*=========================================================================== |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 196 | DecodeNesting -- Tracking array/map/sequence/bstr-wrapped nesting |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 197 | ===========================================================================*/ |
| 198 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 199 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 200 | * See comments about and typedef of QCBORDecodeNesting in qcbor_private.h, |
| 201 | * the data structure all these functions work on. |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 202 | */ |
| 203 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 204 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 205 | static uint8_t |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 206 | DecodeNesting_GetCurrentLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 207 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 208 | const ptrdiff_t nLevel = pNesting->pCurrent - &(pNesting->pLevels[0]); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 209 | /* Limit in DecodeNesting_Descend against more than |
| 210 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 211 | */ |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 212 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 215 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 216 | static uint8_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 217 | DecodeNesting_GetBoundedModeLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 218 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 219 | const ptrdiff_t nLevel = pNesting->pCurrentBounded - &(pNesting->pLevels[0]); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 220 | /* Limit in DecodeNesting_Descend against more than |
| 221 | * QCBOR_MAX_ARRAY_NESTING gaurantees cast is safe |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 222 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 223 | return (uint8_t)nLevel; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 226 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 227 | static uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 228 | DecodeNesting_GetMapOrArrayStart(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 229 | { |
| 230 | return pNesting->pCurrentBounded->u.ma.uStartOffset; |
| 231 | } |
| 232 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 233 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 234 | static bool |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 235 | DecodeNesting_IsBoundedEmpty(const QCBORDecodeNesting *pNesting) |
| 236 | { |
| 237 | if(pNesting->pCurrentBounded->u.ma.uCountCursor == QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 238 | return true; |
| 239 | } else { |
| 240 | return false; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 245 | static bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 246 | DecodeNesting_IsCurrentAtTop(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 247 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 248 | if(pNesting->pCurrent == &(pNesting->pLevels[0])) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 249 | return true; |
| 250 | } else { |
| 251 | return false; |
| 252 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 255 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 256 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 257 | DecodeNesting_IsCurrentDefiniteLength(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 258 | { |
| 259 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 260 | /* Not a map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 261 | return false; |
| 262 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 263 | |
| 264 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 265 | if(pNesting->pCurrent->u.ma.uCountTotal == QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 266 | /* Is indefinite */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 267 | return false; |
| 268 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 269 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 270 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 271 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 272 | /* All checks passed; is a definte length map or array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 273 | return true; |
| 274 | } |
| 275 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 276 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 277 | DecodeNesting_IsCurrentBstrWrapped(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 278 | { |
| 279 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 280 | /* is a byte string */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 281 | return true; |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 286 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 287 | static bool |
| 288 | DecodeNesting_IsCurrentBounded(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 289 | { |
| 290 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
| 291 | return true; |
| 292 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 293 | if(pNesting->pCurrent->u.ma.uStartOffset != QCBOR_NON_BOUNDED_OFFSET) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 294 | return true; |
| 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 299 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 300 | static void |
| 301 | DecodeNesting_SetMapOrArrayBoundedMode(QCBORDecodeNesting *pNesting, bool bIsEmpty, size_t uStart) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 302 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 303 | /* Should be only called on maps and arrays */ |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 304 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 305 | * DecodeNesting_EnterBoundedMode() checks to be sure uStart is not |
| 306 | * larger than DecodeNesting_EnterBoundedMode which keeps it less than |
| 307 | * uin32_t so the cast is safe. |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 308 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 309 | pNesting->pCurrent->u.ma.uStartOffset = (uint32_t)uStart; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 310 | |
| 311 | if(bIsEmpty) { |
| 312 | pNesting->pCurrent->u.ma.uCountCursor = QCBOR_COUNT_INDICATES_ZERO_LENGTH; |
| 313 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 316 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 317 | static void |
| 318 | DecodeNesting_ClearBoundedMode(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 319 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 320 | pNesting->pCurrent->u.ma.uStartOffset = QCBOR_NON_BOUNDED_OFFSET; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 323 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 324 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 325 | DecodeNesting_IsAtEndOfBoundedLevel(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 326 | { |
| 327 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 328 | /* No bounded map or array set up */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 329 | return false; |
| 330 | } |
| 331 | if(pNesting->pCurrent->uLevelType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 332 | /* Not a map or array; end of those is by byte count */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 333 | return false; |
| 334 | } |
Laurence Lundblade | 6ab01fb | 2020-10-01 13:04:49 -0700 | [diff] [blame] | 335 | if(!DecodeNesting_IsCurrentBounded(pNesting)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 336 | /* In a traveral at a level deeper than the bounded level */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 337 | return false; |
| 338 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 339 | /* Works for both definite- and indefinitelength maps/arrays */ |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 340 | if(pNesting->pCurrentBounded->u.ma.uCountCursor != 0 && |
| 341 | pNesting->pCurrentBounded->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 342 | /* Count is not zero, still unconsumed item */ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 343 | return false; |
| 344 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 345 | /* All checks passed, got to the end of an array or map*/ |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 346 | return true; |
| 347 | } |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 348 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 349 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 350 | static bool |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 351 | DecodeNesting_IsEndOfDefiniteLengthMapOrArray(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 352 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 353 | /* Must only be called on map / array */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 354 | if(pNesting->pCurrent->u.ma.uCountCursor == 0) { |
| 355 | return true; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 356 | } else { |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 357 | return false; |
| 358 | } |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 361 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 362 | static bool |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 363 | DecodeNesting_IsCurrentTypeMap(const QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 364 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 365 | if(pNesting->pCurrent->uLevelType == CBOR_MAJOR_TYPE_MAP) { |
| 366 | return true; |
| 367 | } else { |
| 368 | return false; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 369 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 372 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 373 | static bool |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 374 | DecodeNesting_IsBoundedType(const QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 375 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 376 | if(pNesting->pCurrentBounded == NULL) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 377 | return false; |
| 378 | } |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 379 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 380 | uint8_t uItemDataType = pNesting->pCurrentBounded->uLevelType; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 381 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 382 | if(uItemDataType == QCBOR_TYPE_MAP_AS_ARRAY) { |
| 383 | uItemDataType = QCBOR_TYPE_ARRAY; |
| 384 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 385 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 386 | |
| 387 | if(uItemDataType != uType) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 388 | return false; |
| 389 | } |
| 390 | |
| 391 | return true; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 394 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 395 | static void |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 396 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 397 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 398 | /* Only call on a definite-length array / map */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 399 | pNesting->pCurrent->u.ma.uCountCursor--; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 400 | } |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 401 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 402 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 403 | static void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 404 | DecodeNesting_ReverseDecrement(QCBORDecodeNesting *pNesting) |
| 405 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 406 | /* Only call on a definite-length array / map */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 407 | pNesting->pCurrent->u.ma.uCountCursor++; |
| 408 | } |
| 409 | |
| 410 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 411 | static void |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 412 | DecodeNesting_Ascend(QCBORDecodeNesting *pNesting) |
| 413 | { |
| 414 | pNesting->pCurrent--; |
| 415 | } |
| 416 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 417 | |
| 418 | static QCBORError |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 419 | DecodeNesting_Descend(QCBORDecodeNesting *pNesting, uint8_t uType) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 420 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 421 | /* Error out if nesting is too deep */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 422 | if(pNesting->pCurrent >= &(pNesting->pLevels[QCBOR_MAX_ARRAY_NESTING])) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 423 | return QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 426 | /* The actual descend */ |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 427 | pNesting->pCurrent++; |
| 428 | |
| 429 | pNesting->pCurrent->uLevelType = uType; |
| 430 | |
| 431 | return QCBOR_SUCCESS; |
| 432 | } |
| 433 | |
| 434 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 435 | static QCBORError |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 436 | DecodeNesting_EnterBoundedMapOrArray(QCBORDecodeNesting *pNesting, |
| 437 | bool bIsEmpty, |
| 438 | size_t uOffset) |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 439 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 440 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 441 | * Should only be called on map/array. |
| 442 | * |
| 443 | * Have descended into this before this is called. The job here is |
| 444 | * just to mark it in bounded mode. |
| 445 | * |
| 446 | * Check against QCBOR_MAX_DECODE_INPUT_SIZE make sure that |
| 447 | * uOffset doesn't collide with QCBOR_NON_BOUNDED_OFFSET. |
| 448 | * |
| 449 | * 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] | 450 | */ |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 451 | if((uint32_t)uOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 452 | return QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 455 | pNesting->pCurrentBounded = pNesting->pCurrent; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 456 | |
| 457 | DecodeNesting_SetMapOrArrayBoundedMode(pNesting, bIsEmpty, uOffset); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 458 | |
| 459 | return QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Laurence Lundblade | 64b607e | 2020-05-13 13:05:57 -0700 | [diff] [blame] | 462 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 463 | static QCBORError |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 464 | DecodeNesting_DescendMapOrArray(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | b95afec | 2024-08-26 10:51:28 -0700 | [diff] [blame] | 465 | const uint8_t uQCBORType, |
| 466 | const uint16_t uCount) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 467 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 468 | QCBORError uError = QCBOR_SUCCESS; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 469 | |
| 470 | if(uCount == 0) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 471 | /* Nothing to do for empty definite-length arrays. They are just are |
| 472 | * effectively the same as an item that is not a map or array. |
| 473 | */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 474 | goto Done; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 475 | /* Empty indefinite-length maps and arrays are handled elsewhere */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Laurence Lundblade | b95afec | 2024-08-26 10:51:28 -0700 | [diff] [blame] | 478 | /* Rely on check in QCBOR_Private_DecodeArrayOrMap() for definite-length |
| 479 | * arrays and maps that are too long */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 480 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 481 | uError = DecodeNesting_Descend(pNesting, uQCBORType); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 482 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 483 | goto Done; |
| 484 | } |
| 485 | |
Laurence Lundblade | b95afec | 2024-08-26 10:51:28 -0700 | [diff] [blame] | 486 | pNesting->pCurrent->u.ma.uCountCursor = uCount; |
| 487 | pNesting->pCurrent->u.ma.uCountTotal = uCount; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 488 | |
| 489 | DecodeNesting_ClearBoundedMode(pNesting); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 490 | |
| 491 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 492 | return uError;; |
| 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_LevelUpCurrent(QCBORDecodeNesting *pNesting) |
| 498 | { |
| 499 | pNesting->pCurrent = pNesting->pCurrentBounded - 1; |
| 500 | } |
| 501 | |
| 502 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 503 | static void |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 504 | DecodeNesting_LevelUpBounded(QCBORDecodeNesting *pNesting) |
| 505 | { |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 506 | while(pNesting->pCurrentBounded != &(pNesting->pLevels[0])) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 507 | pNesting->pCurrentBounded--; |
| 508 | if(DecodeNesting_IsCurrentBounded(pNesting)) { |
| 509 | break; |
| 510 | } |
| 511 | } |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 514 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 515 | static void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 516 | DecodeNesting_SetCurrentToBoundedLevel(QCBORDecodeNesting *pNesting) |
| 517 | { |
| 518 | pNesting->pCurrent = pNesting->pCurrentBounded; |
| 519 | } |
| 520 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 521 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 522 | static QCBORError |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 523 | DecodeNesting_DescendIntoBstrWrapped(QCBORDecodeNesting *pNesting, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 524 | uint32_t uEndOffset, |
| 525 | uint32_t uStartOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 526 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 527 | QCBORError uError; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 528 | |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 529 | uError = DecodeNesting_Descend(pNesting, QCBOR_TYPE_BYTE_STRING); |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 530 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 531 | goto Done; |
| 532 | } |
| 533 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 534 | /* Fill in the new byte string level */ |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 535 | pNesting->pCurrent->u.bs.uSavedEndOffset = uEndOffset; |
| 536 | pNesting->pCurrent->u.bs.uBstrStartOffset = uStartOffset; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 537 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 538 | /* Bstr wrapped levels are always bounded */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 539 | pNesting->pCurrentBounded = pNesting->pCurrent; |
| 540 | |
| 541 | Done: |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 542 | return uError;; |
| 543 | } |
| 544 | |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 545 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 546 | static void |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 547 | DecodeNesting_ZeroMapOrArrayCount(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 548 | { |
| 549 | pNesting->pCurrent->u.ma.uCountCursor = 0; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 552 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 553 | static void |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 554 | DecodeNesting_ResetMapOrArrayCount(QCBORDecodeNesting *pNesting) |
| 555 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 556 | if(pNesting->pCurrent->u.ma.uCountCursor != QCBOR_COUNT_INDICATES_ZERO_LENGTH) { |
| 557 | pNesting->pCurrentBounded->u.ma.uCountCursor = pNesting->pCurrentBounded->u.ma.uCountTotal; |
| 558 | } |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 562 | static void |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 563 | DecodeNesting_Init(QCBORDecodeNesting *pNesting) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 564 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 565 | /* Assumes that *pNesting has been zero'd before this call. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 566 | pNesting->pLevels[0].uLevelType = QCBOR_TYPE_BYTE_STRING; |
| 567 | pNesting->pCurrent = &(pNesting->pLevels[0]); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 571 | static void |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 572 | DecodeNesting_PrepareForMapSearch(QCBORDecodeNesting *pNesting, |
| 573 | QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 574 | { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 575 | *pSave = *pNesting; |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 578 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 579 | static void |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 580 | DecodeNesting_RestoreFromMapSearch(QCBORDecodeNesting *pNesting, |
| 581 | const QCBORDecodeNesting *pSave) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 582 | { |
| 583 | *pNesting = *pSave; |
| 584 | } |
| 585 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 586 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 587 | static uint32_t |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 588 | DecodeNesting_GetPreviousBoundedEnd(const QCBORDecodeNesting *pMe) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 589 | { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 590 | return pMe->pCurrentBounded->u.bs.uSavedEndOffset; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 594 | |
| 595 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 596 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 597 | /*=========================================================================== |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 598 | QCBORStringAllocate -- STRING ALLOCATOR INVOCATION |
| 599 | |
| 600 | The following four functions are pretty wrappers for invocation of |
| 601 | the string allocator supplied by the caller. |
| 602 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 603 | ===========================================================================*/ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 604 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 605 | static void |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 606 | StringAllocator_Free(const QCBORInternalAllocator *pMe, const void *pMem) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 607 | { |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 608 | /* This cast to uintptr_t suppresses the "-Wcast-qual" warnings. |
| 609 | * This is the one place where the const needs to be cast away so const can |
| 610 | * be use in the rest of the code. |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 611 | */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 612 | (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)(uintptr_t)pMem, 0); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 613 | } |
| 614 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 615 | // StringAllocator_Reallocate called with pMem NULL is |
| 616 | // equal to StringAllocator_Allocate() |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 617 | static UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 618 | StringAllocator_Reallocate(const QCBORInternalAllocator *pMe, |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 619 | const void *pMem, |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 620 | size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 621 | { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 622 | /* See comment in StringAllocator_Free() */ |
Maxim Zhukov | d538f0a | 2022-12-20 20:40:38 +0300 | [diff] [blame] | 623 | return (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)(uintptr_t)pMem, uSize); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 626 | static UsefulBuf |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 627 | StringAllocator_Allocate(const QCBORInternalAllocator *pMe, size_t uSize) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 628 | { |
| 629 | return (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, uSize); |
| 630 | } |
| 631 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 632 | static void |
Laurence Lundblade | 3a691a0 | 2020-12-28 04:15:16 -0800 | [diff] [blame] | 633 | StringAllocator_Destruct(const QCBORInternalAllocator *pMe) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 634 | { |
Laurence Lundblade | b970245 | 2021-03-08 21:02:57 -0800 | [diff] [blame] | 635 | /* See comment in StringAllocator_Free() */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 636 | if(pMe->pfAllocator) { |
| 637 | (pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0); |
| 638 | } |
| 639 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 640 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 641 | |
| 642 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 643 | |
| 644 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 645 | /*=========================================================================== |
| 646 | QCBORDecode -- The main implementation of CBOR decoding |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 647 | |
Laurence Lundblade | 844bb5c | 2020-03-01 17:27:25 -0800 | [diff] [blame] | 648 | See qcbor/qcbor_decode.h for definition of the object |
| 649 | used here: QCBORDecodeContext |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 650 | ===========================================================================*/ |
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 | * Public function, see header file |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 653 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 654 | void |
| 655 | QCBORDecode_Init(QCBORDecodeContext *pMe, |
| 656 | UsefulBufC EncodedCBOR, |
| 657 | QCBORDecodeMode nDecodeMode) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 658 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 659 | memset(pMe, 0, sizeof(QCBORDecodeContext)); |
| 660 | UsefulInputBuf_Init(&(pMe->InBuf), EncodedCBOR); |
| 661 | /* Don't bother with error check on decode mode. If a bad value is |
| 662 | * passed it will just act as if the default normal mode of 0 was set. |
| 663 | */ |
| 664 | pMe->uDecodeMode = (uint8_t)nDecodeMode; |
| 665 | DecodeNesting_Init(&(pMe->nesting)); |
| 666 | |
| 667 | /* Inialize me->auMappedTags to CBOR_TAG_INVALID16. See |
| 668 | * GetNext_TaggedItem() and MapTagNumber(). */ |
| 669 | memset(pMe->auMappedTags, 0xff, sizeof(pMe->auMappedTags)); |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 670 | |
| 671 | pMe->uTagNumberCheckOffset = SIZE_MAX; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 675 | /* |
| 676 | * Public function, see header file |
| 677 | */ |
| 678 | void |
| 679 | QCBORDecode_CompatibilityV1(QCBORDecodeContext *pMe) |
| 680 | { |
| 681 | pMe->uDecodeMode |= QCBOR_DECODE_UNPROCESSED_TAG_NUMBERS; |
| 682 | #ifndef QCBOR_DISABLE_TAGS |
| 683 | QCBORDecode_InstallTagDecoders(pMe, QCBORDecode_TagDecoderTablev1, NULL); |
| 684 | #endif /* ! QCBOR_DISABLE_TAGS */ |
| 685 | } |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 690 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 691 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 692 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 693 | * Public function, see header file |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 694 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 695 | void |
| 696 | QCBORDecode_SetUpAllocator(QCBORDecodeContext *pMe, |
| 697 | QCBORStringAllocate pfAllocateFunction, |
| 698 | void *pAllocateContext, |
| 699 | bool bAllStrings) |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 700 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 701 | pMe->StringAllocator.pfAllocator = pfAllocateFunction; |
| 702 | pMe->StringAllocator.pAllocateCxt = pAllocateContext; |
| 703 | pMe->bStringAllocateAll = bAllStrings; |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 704 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 705 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 706 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 707 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 708 | |
| 709 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 710 | |
| 711 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 712 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 713 | * Decoding items is done in six layers, one calling the next one |
| 714 | * down. If a layer has no work to do for a particular item, it |
| 715 | * returns quickly. |
| 716 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 717 | * 1. QCBORDecode_Private_GetNextTagContent - The top layer processes |
| 718 | * tagged data items, turning them into the local C representation. |
| 719 | * For the most simple it is just associating a QCBOR_TYPE with the |
| 720 | * data. For the complex ones that an aggregate of data items, there |
| 721 | * is some further decoding and some limited recursion. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 722 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 723 | * 2. QCBORDecode_Private_GetNextMapOrArray - This manages the |
| 724 | * beginnings and ends of maps and arrays. It tracks descending into |
| 725 | * and ascending out of maps/arrays. It processes breaks that |
| 726 | * terminate indefinite-length maps and arrays. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 727 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 728 | * 3. QCBORDecode_Private_GetNextMapEntry - This handles the combining |
| 729 | * of two items, the label and the data, that make up a map entry. It |
| 730 | * only does work on maps. It combines the label and data items into |
| 731 | * one labeled item. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 732 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 733 | * 4. QCBORDecode_Private_GetNextTagNumber - This decodes type 6 tag |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 734 | * numbers. It turns the tag numbers into bit flags associated with |
| 735 | * the data item. No actual decoding of the contents of the tag is |
| 736 | * performed here. |
| 737 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 738 | * 5. QCBORDecode_Private_GetNextFullString - This assembles the |
| 739 | * sub-items that make up an indefinite-length string into one string |
| 740 | * item. It uses the string allocator to create contiguous space for |
| 741 | * the item. It processes all breaks that are part of |
| 742 | * indefinite-length strings. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 743 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 744 | * 6. QCBOR_Private_DecodeAtomicDataItem - This decodes the atomic |
| 745 | * data items in CBOR. Each atomic data item has a "major type", an |
| 746 | * integer "argument" and optionally some content. For text and byte |
| 747 | * strings, the content is the bytes that make up the string. These |
| 748 | * are the smallest data items that are considered to be well-formed. |
| 749 | * The content may also be other data items in the case of aggregate |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 750 | * types. They are not handled in this layer. |
| 751 | * |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 752 | * This uses about 350 bytes of stack. This number comes from |
| 753 | * instrumenting (printf address of stack variables) the code on x86 |
| 754 | * compiled for size optimization. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 755 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 756 | |
| 757 | |
| 758 | /* |
| 759 | * Note about use of int and unsigned variables. |
| 760 | * |
| 761 | * See http://www.unix.org/whitepapers/64bit.html for reasons int is |
| 762 | * used carefully here, and in particular why it isn't used in the |
| 763 | * public interface. Also see |
| 764 | * https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers |
| 765 | * |
| 766 | * Int is used for values that need less than 16-bits and would be |
| 767 | * subject to integer promotion and result in complaining from static |
| 768 | * analyzers. |
| 769 | */ |
| 770 | |
| 771 | |
| 772 | /** |
| 773 | * @brief Decode the CBOR head, the type and argument. |
| 774 | * |
| 775 | * @param[in] pUInBuf The input buffer to read from. |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 776 | * @param[in] bRequirePreferred Require preferred serialization for argument. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 777 | * @param[out] pnMajorType The decoded major type. |
| 778 | * @param[out] puArgument The decoded argument. |
| 779 | * @param[out] pnAdditionalInfo The decoded Lower 5 bits of initial byte. |
| 780 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 781 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved features |
| 782 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 783 | * |
| 784 | * This decodes the CBOR "head" that every CBOR data item has. See |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 785 | * longer description in QCBOREncode_EncodeHead(). |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 786 | * |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 787 | * This does the network to host byte order conversion. The conversion |
| 788 | * here also provides the conversion for floats in addition to that |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 789 | * for lengths, tags and integer values. |
| 790 | * |
| 791 | * The int type is preferred to uint8_t for some variables as this |
| 792 | * avoids integer promotions, can reduce code size and makes static |
| 793 | * analyzers happier. |
| 794 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 795 | static QCBORError |
| 796 | QCBOR_Private_DecodeHead(UsefulInputBuf *pUInBuf, |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 797 | #ifndef QCBOR_DISABLE_DECODE_CONFORMANCE |
| 798 | bool bRequirePreferred, |
| 799 | #endif |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 800 | int *pnMajorType, |
| 801 | uint64_t *puArgument, |
| 802 | int *pnAdditionalInfo) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 803 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 804 | QCBORError uReturn; |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 805 | uint64_t uArgument; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 806 | |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 807 | /* Get and break down initial byte that every CBOR data item has */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 808 | const int nInitialByte = (int)UsefulInputBuf_GetByte(pUInBuf); |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 809 | const int nTmpMajorType = nInitialByte >> 5; |
| 810 | const int nAdditionalInfo = nInitialByte & 0x1f; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 811 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 812 | if(nAdditionalInfo >= LEN_IS_ONE_BYTE && nAdditionalInfo <= LEN_IS_EIGHT_BYTES) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 813 | /* Need to get 1,2,4 or 8 additional argument bytes. Map |
| 814 | * LEN_IS_ONE_BYTE..LEN_IS_EIGHT_BYTES to actual length. |
| 815 | */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 816 | static const uint8_t aIterate[] = {1,2,4,8}; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 817 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 818 | /* Loop getting all the bytes in the argument */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 819 | uArgument = 0; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 820 | for(int i = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE]; i; i--) { |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 821 | /* This shift-and-add gives the endian conversion. */ |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 822 | uArgument = (uArgument << 8) + UsefulInputBuf_GetByte(pUInBuf); |
| 823 | } |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 824 | |
| 825 | #ifndef QCBOR_DISABLE_DECODE_CONFORMANCE |
| 826 | /* If requested, check that argument is in preferred form */ |
| 827 | if(bRequirePreferred) { |
| 828 | uint64_t uMinArgument; |
| 829 | |
| 830 | if(nAdditionalInfo == LEN_IS_ONE_BYTE) { |
| 831 | if(uArgument < 24) { |
| 832 | uReturn = QCBOR_ERR_PREFERRED_CONFORMANCE; |
| 833 | goto Done; |
| 834 | } |
| 835 | } else { |
| 836 | if(nTmpMajorType != CBOR_MAJOR_TYPE_SIMPLE) { |
| 837 | /* Check only if not a floating-point number */ |
| 838 | int nArgLen = aIterate[nAdditionalInfo - LEN_IS_ONE_BYTE - 1]; |
| 839 | uMinArgument = UINT64_MAX >> ((int)sizeof(uint64_t) - nArgLen) * 8; |
| 840 | if(uArgument <= uMinArgument) { |
| 841 | uReturn = QCBOR_ERR_PREFERRED_CONFORMANCE; |
| 842 | goto Done; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | #endif /* ! QCBOR_DISABLE_DECODE_CONFORMANCE */ |
| 848 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 849 | } else if(nAdditionalInfo >= ADDINFO_RESERVED1 && nAdditionalInfo <= ADDINFO_RESERVED3) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 850 | /* The reserved and thus-far unused additional info values */ |
| 851 | uReturn = QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 852 | goto Done; |
| 853 | } else { |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 854 | #ifndef QCBOR_DISABLE_DECODE_CONFORMANCE |
| 855 | if(bRequirePreferred && nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 856 | uReturn = QCBOR_ERR_PREFERRED_CONFORMANCE; |
| 857 | goto Done; |
| 858 | } |
| 859 | #endif /* ! QCBOR_DISABLE_DECODE_CONFORMANCE */ |
| 860 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 861 | /* Less than 24, additional info is argument or 31, an |
| 862 | * indefinite-length. No more bytes to get. |
| 863 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 864 | uArgument = (uint64_t)nAdditionalInfo; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 865 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 866 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 867 | if(UsefulInputBuf_GetError(pUInBuf)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 868 | uReturn = QCBOR_ERR_HIT_END; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 869 | goto Done; |
| 870 | } |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 871 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 872 | /* All successful if arrived here. */ |
| 873 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 874 | *pnMajorType = nTmpMajorType; |
Laurence Lundblade | 4c0cf84 | 2019-01-12 03:25:44 -0800 | [diff] [blame] | 875 | *puArgument = uArgument; |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 876 | *pnAdditionalInfo = nAdditionalInfo; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 877 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 878 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 879 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 882 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 883 | /** |
| 884 | * @brief Decode integer types, major types 0 and 1. |
| 885 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 886 | * @param[in] nMajorType The CBOR major type (0 or 1). |
| 887 | * @param[in] uArgument The argument from the head. |
| 888 | * @param[in] nAdditionalInfo So it can be error-checked. |
| 889 | * @param[out] pDecodedItem The filled in decoded item. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 890 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 891 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered. |
| 892 | * @retval QCBOR_ERR_BAD_INT nAdditionalInfo indicated indefinte. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 893 | * |
| 894 | * Must only be called when major type is 0 or 1. |
| 895 | * |
| 896 | * CBOR doesn't explicitly specify two's compliment for integers but |
| 897 | * all CPUs use it these days and the test vectors in the RFC are |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 898 | * so. All integers in encoded CBOR are unsigned and the CBOR major |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 899 | * type indicates positive or negative. CBOR can express positive |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 900 | * integers up to 2^64 - 1 negative integers down to -2^64. Note that |
| 901 | * negative numbers can be one more |
| 902 | * away from zero than positive because there is no negative zero. |
| 903 | * |
| 904 | * The "65-bit negs" are values CBOR can encode that can't fit |
| 905 | * into an int64_t or uint64_t. They decoded as a special type |
| 906 | * QCBOR_TYPE_65BIT_NEG_INT. Not that this type does NOT |
| 907 | * take into account the offset of one for CBOR negative integers. |
| 908 | * It must be applied to get the correct value. Applying this offset |
| 909 | * would overflow a uint64_t. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 910 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 911 | static QCBORError |
| 912 | QCBOR_Private_DecodeInteger(const int nMajorType, |
| 913 | const uint64_t uArgument, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 914 | const int nAdditionalInfo, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 915 | QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 916 | { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 917 | QCBORError uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 918 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 919 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 920 | uReturn = QCBOR_ERR_BAD_INT; |
| 921 | goto Done; |
| 922 | } |
| 923 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 924 | if(nMajorType == CBOR_MAJOR_TYPE_POSITIVE_INT) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 925 | if (uArgument <= INT64_MAX) { |
| 926 | pDecodedItem->val.int64 = (int64_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 927 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 928 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 929 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 930 | pDecodedItem->val.uint64 = uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 931 | pDecodedItem->uDataType = QCBOR_TYPE_UINT64; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 932 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 933 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 934 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 935 | if(uArgument <= INT64_MAX) { |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 936 | /* INT64_MIN is one further away from 0 than INT64_MAX |
| 937 | * so the -1 here doesn't overflow. */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 938 | pDecodedItem->val.int64 = (-(int64_t)uArgument) - 1; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 939 | pDecodedItem->uDataType = QCBOR_TYPE_INT64; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 940 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 941 | } else { |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 942 | pDecodedItem->val.uint64 = uArgument; |
| 943 | pDecodedItem->uDataType = QCBOR_TYPE_65BIT_NEG_INT; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 944 | } |
| 945 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 946 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 947 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 948 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 951 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 952 | /** |
| 953 | * @brief Decode text and byte strings |
| 954 | * |
| 955 | * @param[in] pMe Decoder context. |
| 956 | * @param[in] bAllocate Whether to allocate and copy string. |
| 957 | * @param[in] nMajorType Whether it is a byte or text string. |
| 958 | * @param[in] uStrLen The length of the string. |
| 959 | * @param[in] nAdditionalInfo Whether it is an indefinite-length string. |
| 960 | * @param[out] pDecodedItem The filled-in decoded item. |
| 961 | * |
| 962 | * @retval QCBOR_ERR_HIT_END Unexpected end of input. |
| 963 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 964 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 965 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Allocation requested, but no allocator |
| 966 | * |
| 967 | * This reads @c uStrlen bytes from the input and fills in @c |
| 968 | * pDecodedItem. If @c bAllocate is true, then memory for the string |
| 969 | * is allocated. |
| 970 | */ |
| 971 | static QCBORError |
| 972 | QCBOR_Private_DecodeString(QCBORDecodeContext *pMe, |
| 973 | const bool bAllocate, |
| 974 | const int nMajorType, |
| 975 | const uint64_t uStrLen, |
| 976 | const int nAdditionalInfo, |
| 977 | QCBORItem *pDecodedItem) |
| 978 | { |
| 979 | QCBORError uReturn = QCBOR_SUCCESS; |
| 980 | |
| 981 | /* ---- Figure out the major type ---- */ |
| 982 | #if CBOR_MAJOR_TYPE_BYTE_STRING + 4 != QCBOR_TYPE_BYTE_STRING |
| 983 | #error QCBOR_TYPE_BYTE_STRING not lined up with major type |
| 984 | #endif |
| 985 | |
| 986 | #if CBOR_MAJOR_TYPE_TEXT_STRING + 4 != QCBOR_TYPE_TEXT_STRING |
| 987 | #error QCBOR_TYPE_TEXT_STRING not lined up with major type |
| 988 | #endif |
| 989 | pDecodedItem->uDataType = (uint8_t)(nMajorType + 4); |
| 990 | |
| 991 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 992 | /* --- Just the head of an indefinite-length string --- */ |
| 993 | pDecodedItem->val.string = (UsefulBufC){NULL, QCBOR_STRING_LENGTH_INDEFINITE}; |
| 994 | |
| 995 | } else { |
| 996 | /* --- A definite-length string --- */ |
| 997 | /* --- (which might be a chunk of an indefinte-length string) --- */ |
| 998 | |
| 999 | /* CBOR lengths can be 64 bits, but size_t is not 64 bits on all |
| 1000 | * CPUs. This check makes the casts to size_t below safe. |
| 1001 | * |
| 1002 | * The max is 4 bytes less than the largest sizeof() so this can be |
| 1003 | * tested by putting a SIZE_MAX length in the CBOR test input (no |
| 1004 | * one will care the limit on strings is 4 bytes shorter). |
| 1005 | */ |
| 1006 | if(uStrLen > SIZE_MAX-4) { |
| 1007 | uReturn = QCBOR_ERR_STRING_TOO_LONG; |
| 1008 | goto Done; |
| 1009 | } |
| 1010 | |
| 1011 | const UsefulBufC Bytes = UsefulInputBuf_GetUsefulBuf(&(pMe->InBuf), (size_t)uStrLen); |
| 1012 | if(UsefulBuf_IsNULLC(Bytes)) { |
| 1013 | /* Failed to get the bytes for this string item */ |
| 1014 | uReturn = QCBOR_ERR_HIT_END; |
| 1015 | goto Done; |
| 1016 | } |
| 1017 | |
| 1018 | if(bAllocate) { |
| 1019 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
| 1020 | /* --- Put string in allocated memory --- */ |
| 1021 | |
| 1022 | /* Note that this is not where allocation to coalesce |
| 1023 | * indefinite-length strings is done. This is for when the |
| 1024 | * caller has requested all strings be allocated. Disabling |
| 1025 | * indefinite length strings also disables this allocate-all |
| 1026 | * option. |
| 1027 | */ |
| 1028 | |
| 1029 | if(pMe->StringAllocator.pfAllocator == NULL) { |
| 1030 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1031 | goto Done; |
| 1032 | } |
| 1033 | UsefulBuf NewMem = StringAllocator_Allocate(&(pMe->StringAllocator), (size_t)uStrLen); |
| 1034 | if(UsefulBuf_IsNULL(NewMem)) { |
| 1035 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
| 1036 | goto Done; |
| 1037 | } |
| 1038 | pDecodedItem->val.string = UsefulBuf_Copy(NewMem, Bytes); |
| 1039 | pDecodedItem->uDataAlloc = 1; |
| 1040 | #else |
| 1041 | uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED; |
| 1042 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1043 | } else { |
| 1044 | /* --- Normal case with no string allocator --- */ |
| 1045 | pDecodedItem->val.string = Bytes; |
| 1046 | } |
| 1047 | } |
| 1048 | |
| 1049 | Done: |
| 1050 | return uReturn; |
| 1051 | } |
| 1052 | |
| 1053 | |
| 1054 | /** |
| 1055 | * @brief Decode array or map. |
| 1056 | * |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1057 | * @param[in] uDecodeMode3Bit Decoder mode. |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1058 | * @param[in] nMajorType Whether it is a byte or text string. |
| 1059 | * @param[in] uItemCount The length of the string. |
| 1060 | * @param[in] nAdditionalInfo Whether it is an indefinite-length. |
| 1061 | * @param[out] pDecodedItem The filled-in decoded item. |
| 1062 | * |
| 1063 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinites disabled. |
| 1064 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG Too many items in array/map. |
| 1065 | * |
| 1066 | * Not much to do for arrays and maps. Just the type item count (but a |
| 1067 | * little messy because of ifdefs for indefinite-lengths and |
| 1068 | * map-as-array decoding). |
| 1069 | * |
| 1070 | * This also does the bulk of the work for @ref |
| 1071 | * QCBOR_DECODE_MODE_MAP_AS_ARRAY, a special mode to handle |
| 1072 | * arbitrarily complex map labels. This ifdefs out with |
| 1073 | * QCBOR_DISABLE_NON_INTEGER_LABELS. |
| 1074 | */ |
| 1075 | static QCBORError |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1076 | QCBOR_Private_DecodeArrayOrMap(const uint8_t uDecodeMode3Bit, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1077 | const int nMajorType, |
Laurence Lundblade | b95afec | 2024-08-26 10:51:28 -0700 | [diff] [blame] | 1078 | uint64_t uItemCount, |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1079 | const int nAdditionalInfo, |
| 1080 | QCBORItem *pDecodedItem) |
| 1081 | { |
| 1082 | QCBORError uReturn; |
| 1083 | |
| 1084 | /* ------ Sort out the data type ------ */ |
| 1085 | #if QCBOR_TYPE_ARRAY != CBOR_MAJOR_TYPE_ARRAY |
| 1086 | #error QCBOR_TYPE_ARRAY value not lined up with major type |
| 1087 | #endif |
| 1088 | |
| 1089 | #if QCBOR_TYPE_MAP != CBOR_MAJOR_TYPE_MAP |
| 1090 | #error QCBOR_TYPE_MAP value not lined up with major type |
| 1091 | #endif |
| 1092 | pDecodedItem->uDataType = (uint8_t)nMajorType; |
| 1093 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1094 | if(uDecodeMode3Bit == QCBOR_DECODE_MODE_MAP_AS_ARRAY && nMajorType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1095 | pDecodedItem->uDataType = QCBOR_TYPE_MAP_AS_ARRAY; |
| 1096 | } |
| 1097 | #else |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1098 | (void)uDecodeMode3Bit; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1099 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 1100 | |
| 1101 | uReturn = QCBOR_SUCCESS; |
| 1102 | |
| 1103 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1104 | /* ------ Indefinite-length array/map ----- */ |
| 1105 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
| 1106 | pDecodedItem->val.uCount = QCBOR_COUNT_INDICATES_INDEFINITE_LENGTH; |
| 1107 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1108 | uReturn = QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED; |
| 1109 | #endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 1110 | } else { |
Laurence Lundblade | b95afec | 2024-08-26 10:51:28 -0700 | [diff] [blame] | 1111 | /* ----- Definite-length array/map ----- */ |
| 1112 | if(uItemCount > (nMajorType == QCBOR_TYPE_MAP ? QCBOR_MAX_ITEMS_IN_MAP : QCBOR_MAX_ITEMS_IN_ARRAY)) { |
| 1113 | uReturn = QCBOR_ERR_ARRAY_DECODE_TOO_LONG; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1114 | |
Laurence Lundblade | b95afec | 2024-08-26 10:51:28 -0700 | [diff] [blame] | 1115 | } else { |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1116 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1117 | if(uDecodeMode3Bit == QCBOR_DECODE_MODE_MAP_AS_ARRAY && nMajorType == QCBOR_TYPE_MAP) { |
Laurence Lundblade | b95afec | 2024-08-26 10:51:28 -0700 | [diff] [blame] | 1118 | /* ------ Map as array ------ */ |
| 1119 | uItemCount *= 2; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1120 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1121 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | b95afec | 2024-08-26 10:51:28 -0700 | [diff] [blame] | 1122 | |
| 1123 | /* cast OK because of check above */ |
| 1124 | pDecodedItem->val.uCount = (uint16_t)uItemCount; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | return uReturn; |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | /** |
| 1133 | * @brief Decode a tag number. |
| 1134 | * |
| 1135 | * @param[in] uTagNumber The length of the string. |
| 1136 | * @param[in] nAdditionalInfo So this can be error-checked. |
| 1137 | * @param[out] pDecodedItem The filled-in decoded item. |
| 1138 | * |
| 1139 | * @retval QCBOR_ERR_BAD_INT nAdditionalInfo is LEN_IS_INDEFINITE. |
| 1140 | * @retval QCBOR_ERR_TAGS_DISABLED QCBOR_DISABLE_TAGS is defined. |
| 1141 | * |
| 1142 | * Not much to do for tags, but fill in pDecodedItem and check for |
| 1143 | * error in nAdditionalInfo. |
| 1144 | */ |
| 1145 | static QCBORError |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1146 | QCBOR_Private_DecodeTagNumber(const uint64_t uTagNumber, |
| 1147 | const int nAdditionalInfo, |
| 1148 | QCBORItem *pDecodedItem) |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1149 | { |
| 1150 | #ifndef QCBOR_DISABLE_TAGS |
| 1151 | if(nAdditionalInfo == LEN_IS_INDEFINITE) { |
| 1152 | return QCBOR_ERR_BAD_INT; |
| 1153 | } else { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1154 | pDecodedItem->val.uTagNumber = uTagNumber; |
| 1155 | pDecodedItem->uDataType = QCBOR_TYPE_TAG_NUMBER; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1156 | return QCBOR_SUCCESS; |
| 1157 | } |
| 1158 | #else /* QCBOR_DISABLE_TAGS */ |
| 1159 | (void)nAdditionalInfo; |
Laurence Lundblade | 6c9a824 | 2024-06-12 20:34:52 -0700 | [diff] [blame] | 1160 | (void)uTagNumber; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1161 | (void)pDecodedItem; |
| 1162 | return QCBOR_ERR_TAGS_DISABLED; |
| 1163 | #endif /* QCBOR_DISABLE_TAGS */ |
| 1164 | } |
| 1165 | |
| 1166 | |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 1167 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
| 1168 | |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1169 | #if !defined(QCBOR_DISABLE_DECODE_CONFORMANCE) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT) |
| 1170 | |
| 1171 | static QCBORError |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1172 | QCBORDecode_Private_HalfConformance(const double d, const uint8_t uDecodeMode3Bit) |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1173 | { |
| 1174 | struct IEEE754_ToInt ToInt; |
| 1175 | |
| 1176 | /* Only need to check for conversion to integer because |
| 1177 | * half-precision is always preferred serialization. Don't |
| 1178 | * need special checker for half-precision because whole |
| 1179 | * numbers always convert perfectly from half to double. |
| 1180 | * |
| 1181 | * This catches half-precision with NaN payload too. |
| 1182 | * |
| 1183 | * The only thing allowed here is a double/half-precision that |
| 1184 | * can't be converted to anything but a double. |
| 1185 | */ |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1186 | if(uDecodeMode3Bit >= QCBOR_DECODE_MODE_DCBOR) { |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1187 | ToInt = IEEE754_DoubleToInt(d); |
| 1188 | if(ToInt.type != QCBOR_TYPE_DOUBLE) { |
| 1189 | return QCBOR_ERR_DCBOR_CONFORMANCE; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | return QCBOR_SUCCESS; |
| 1194 | } |
| 1195 | |
| 1196 | |
| 1197 | static QCBORError |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1198 | QCBORDecode_Private_SingleConformance(const float f, const uint8_t uDecodeMode3Bit) |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1199 | { |
| 1200 | struct IEEE754_ToInt ToInt; |
| 1201 | IEEE754_union ToSmaller; |
| 1202 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1203 | if(uDecodeMode3Bit >= QCBOR_DECODE_MODE_DCBOR) { |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1204 | /* See if it could have been encoded as an integer */ |
| 1205 | ToInt = IEEE754_SingleToInt(f); |
| 1206 | if(ToInt.type == IEEE754_ToInt_IS_INT || ToInt.type == IEEE754_ToInt_IS_UINT) { |
| 1207 | return QCBOR_ERR_DCBOR_CONFORMANCE; |
| 1208 | } |
| 1209 | |
| 1210 | /* Make sure there is no NaN payload */ |
| 1211 | if(IEEE754_SingleHasNaNPayload(f)) { |
| 1212 | return QCBOR_ERR_DCBOR_CONFORMANCE; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | /* See if it could have been encoded shorter */ |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1217 | if(uDecodeMode3Bit >= QCBOR_DECODE_MODE_PREFERRED) { |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1218 | ToSmaller = IEEE754_SingleToHalf(f, true); |
| 1219 | if(ToSmaller.uSize != sizeof(float)) { |
| 1220 | return QCBOR_ERR_PREFERRED_CONFORMANCE; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | return QCBOR_SUCCESS; |
| 1225 | } |
| 1226 | |
| 1227 | |
| 1228 | static QCBORError |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1229 | QCBORDecode_Private_DoubleConformance(const double d, uint8_t uDecodeMode3Bit) |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1230 | { |
| 1231 | struct IEEE754_ToInt ToInt; |
| 1232 | IEEE754_union ToSmaller; |
| 1233 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1234 | if(uDecodeMode3Bit >= QCBOR_DECODE_MODE_DCBOR) { |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1235 | /* See if it could have been encoded as an integer */ |
| 1236 | ToInt = IEEE754_DoubleToInt(d); |
| 1237 | if(ToInt.type == IEEE754_ToInt_IS_INT || ToInt.type == IEEE754_ToInt_IS_UINT) { |
| 1238 | return QCBOR_ERR_DCBOR_CONFORMANCE; |
| 1239 | } |
| 1240 | /* Make sure there is no NaN payload */ |
| 1241 | if(IEEE754_DoubleHasNaNPayload(d)) { |
| 1242 | return QCBOR_ERR_DCBOR_CONFORMANCE; |
| 1243 | } |
| 1244 | } |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1245 | |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1246 | /* See if it could have been encoded shorter */ |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1247 | if(uDecodeMode3Bit >= QCBOR_DECODE_MODE_PREFERRED) { |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1248 | ToSmaller = IEEE754_DoubleToSmaller(d, true, true); |
| 1249 | if(ToSmaller.uSize != sizeof(double)) { |
| 1250 | return QCBOR_ERR_PREFERRED_CONFORMANCE; |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | return QCBOR_SUCCESS; |
| 1255 | } |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 1256 | #else /* ! QCBOR_DISABLE_DECODE_CONFORMANCE && ! QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1257 | |
| 1258 | static QCBORError |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1259 | QCBORDecode_Private_SingleConformance(const float f, uint8_t uDecodeMode3Bit) |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1260 | { |
| 1261 | (void)f; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1262 | if(uDecodeMode3Bit>= QCBOR_DECODE_MODE_PREFERRED) { |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1263 | return QCBOR_ERR_CANT_CHECK_FLOAT_CONFORMANCE; |
| 1264 | } else { |
| 1265 | return QCBOR_SUCCESS; |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | static QCBORError |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1270 | QCBORDecode_Private_DoubleConformance(const double d, uint8_t uDecodeMode3Bit) |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1271 | { |
| 1272 | (void)d; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1273 | if(uDecodeMode3Bit>= QCBOR_DECODE_MODE_PREFERRED) { |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1274 | return QCBOR_ERR_CANT_CHECK_FLOAT_CONFORMANCE; |
| 1275 | } else { |
| 1276 | return QCBOR_SUCCESS; |
| 1277 | } |
| 1278 | } |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1279 | #endif /* ! QCBOR_DISABLE_DECODE_CONFORMANCE && ! QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 1280 | |
| 1281 | |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1282 | /* |
| 1283 | * Decode a float |
| 1284 | */ |
| 1285 | static QCBORError |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1286 | QCBOR_Private_DecodeFloat(const uint8_t uDecodeMode3Bit, |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1287 | const int nAdditionalInfo, |
| 1288 | const uint64_t uArgument, |
| 1289 | QCBORItem *pDecodedItem) |
| 1290 | { |
| 1291 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1292 | float single; |
| 1293 | |
| 1294 | (void)single; /* Avoid unused error from various #ifndefs */ |
| 1295 | |
| 1296 | switch(nAdditionalInfo) { |
| 1297 | case HALF_PREC_FLOAT: /* 25 */ |
| 1298 | #ifndef QCBOR_DISABLE_PREFERRED_FLOAT |
| 1299 | /* Half-precision is returned as a double. The cast to |
| 1300 | * uint16_t is safe because the encoded value was 16 bits. It |
| 1301 | * was widened to 64 bits to be passed in here. |
| 1302 | */ |
| 1303 | pDecodedItem->val.dfnum = IEEE754_HalfToDouble((uint16_t)uArgument); |
| 1304 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
| 1305 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1306 | uReturn = QCBORDecode_Private_HalfConformance(pDecodedItem->val.dfnum, uDecodeMode3Bit); |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1307 | if(uReturn != QCBOR_SUCCESS) { |
| 1308 | break; |
| 1309 | } |
| 1310 | #endif /* ! QCBOR_DISABLE_PREFERRED_FLOAT */ |
| 1311 | uReturn = FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS); |
| 1312 | break; |
| 1313 | |
| 1314 | case SINGLE_PREC_FLOAT: /* 26 */ |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1315 | /* Single precision is normally returned as a double since |
| 1316 | * double is widely supported, there is no loss of precision, |
| 1317 | * it makes it easy for the caller in most cases and it can |
| 1318 | * be converted back to single with no loss of precision |
| 1319 | * |
| 1320 | * The cast to uint32_t is safe because the encoded value was |
| 1321 | * 32 bits. It was widened to 64 bits to be passed in here. |
| 1322 | */ |
| 1323 | single = UsefulBufUtil_CopyUint32ToFloat((uint32_t)uArgument); |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1324 | uReturn = QCBORDecode_Private_SingleConformance(single, uDecodeMode3Bit); |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1325 | if(uReturn != QCBOR_SUCCESS) { |
| 1326 | break; |
| 1327 | } |
| 1328 | |
| 1329 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 1330 | /* In the normal case, use HW to convert float to |
| 1331 | * double. */ |
| 1332 | pDecodedItem->val.dfnum = (double)single; |
| 1333 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
| 1334 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 1335 | /* Use of float HW is disabled, return as a float. */ |
| 1336 | pDecodedItem->val.fnum = single; |
| 1337 | pDecodedItem->uDataType = QCBOR_TYPE_FLOAT; |
| 1338 | |
| 1339 | /* IEEE754_FloatToDouble() could be used here to return as |
| 1340 | * a double, but it adds object code and most likely |
| 1341 | * anyone disabling FLOAT HW use doesn't care about floats |
| 1342 | * and wants to save object code. |
| 1343 | */ |
| 1344 | #endif /* ! QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1345 | uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS); |
| 1346 | break; |
| 1347 | |
| 1348 | case DOUBLE_PREC_FLOAT: /* 27 */ |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1349 | pDecodedItem->val.dfnum = UsefulBufUtil_CopyUint64ToDouble(uArgument); |
| 1350 | pDecodedItem->uDataType = QCBOR_TYPE_DOUBLE; |
| 1351 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1352 | uReturn = QCBORDecode_Private_DoubleConformance(pDecodedItem->val.dfnum, uDecodeMode3Bit); |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1353 | if(uReturn != QCBOR_SUCCESS) { |
| 1354 | break; |
| 1355 | } |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1356 | uReturn = FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS); |
| 1357 | break; |
| 1358 | } |
| 1359 | |
| 1360 | return uReturn; |
| 1361 | } |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 1362 | #endif /* ! USEFULBUF_DISABLE_ALL_FLOAT */ |
| 1363 | |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1364 | |
| 1365 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1366 | /* Make sure #define value line up as DecodeSimple counts on this. */ |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1367 | #if QCBOR_TYPE_FALSE != CBOR_SIMPLEV_FALSE |
| 1368 | #error QCBOR_TYPE_FALSE macro value wrong |
| 1369 | #endif |
| 1370 | |
| 1371 | #if QCBOR_TYPE_TRUE != CBOR_SIMPLEV_TRUE |
| 1372 | #error QCBOR_TYPE_TRUE macro value wrong |
| 1373 | #endif |
| 1374 | |
| 1375 | #if QCBOR_TYPE_NULL != CBOR_SIMPLEV_NULL |
| 1376 | #error QCBOR_TYPE_NULL macro value wrong |
| 1377 | #endif |
| 1378 | |
| 1379 | #if QCBOR_TYPE_UNDEF != CBOR_SIMPLEV_UNDEF |
| 1380 | #error QCBOR_TYPE_UNDEF macro value wrong |
| 1381 | #endif |
| 1382 | |
Laurence Lundblade | 0f99d69 | 2018-09-26 14:39:28 -0700 | [diff] [blame] | 1383 | #if QCBOR_TYPE_BREAK != CBOR_SIMPLE_BREAK |
| 1384 | #error QCBOR_TYPE_BREAK macro value wrong |
| 1385 | #endif |
| 1386 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1387 | #if QCBOR_TYPE_DOUBLE != DOUBLE_PREC_FLOAT |
| 1388 | #error QCBOR_TYPE_DOUBLE macro value wrong |
| 1389 | #endif |
| 1390 | |
| 1391 | #if QCBOR_TYPE_FLOAT != SINGLE_PREC_FLOAT |
| 1392 | #error QCBOR_TYPE_FLOAT macro value wrong |
| 1393 | #endif |
| 1394 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1395 | /** |
| 1396 | * @brief Decode major type 7 -- true, false, floating-point, break... |
| 1397 | * |
| 1398 | * @param[in] nAdditionalInfo The lower five bits from the initial byte. |
| 1399 | * @param[in] uArgument The argument from the head. |
| 1400 | * @param[out] pDecodedItem The filled in decoded item. |
| 1401 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1402 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1403 | * of half-precision disabled |
| 1404 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all float |
| 1405 | * decode is disabled. |
| 1406 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of simple |
| 1407 | * type in input. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1408 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1409 | static QCBORError |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1410 | QCBOR_Private_DecodeType7(const uint8_t uDecodeMode3Bit, |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1411 | const int nAdditionalInfo, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1412 | const uint64_t uArgument, |
| 1413 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1414 | { |
| 1415 | QCBORError uReturn = QCBOR_SUCCESS; |
| 1416 | |
| 1417 | /* uAdditionalInfo is 5 bits from the initial byte. Compile time |
| 1418 | * checks above make sure uAdditionalInfo values line up with |
| 1419 | * uDataType values. DecodeHead() never returns an AdditionalInfo |
| 1420 | * > 0x1f so cast is safe. |
| 1421 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1422 | pDecodedItem->uDataType = (uint8_t)nAdditionalInfo; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1423 | |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1424 | switch(nAdditionalInfo) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1425 | /* No check for ADDINFO_RESERVED1 - ADDINFO_RESERVED3 as they |
| 1426 | * are caught before this is called. |
| 1427 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1428 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1429 | case HALF_PREC_FLOAT: /* 25 */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1430 | case SINGLE_PREC_FLOAT: /* 26 */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1431 | case DOUBLE_PREC_FLOAT: /* 27 */ |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 1432 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1433 | uReturn = QCBOR_Private_DecodeFloat(uDecodeMode3Bit, nAdditionalInfo, uArgument, pDecodedItem); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 1434 | #else |
| 1435 | uReturn = QCBOR_ERR_ALL_FLOAT_DISABLED; |
| 1436 | #endif /* ! USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 12d32c5 | 2018-09-19 11:25:27 -0700 | [diff] [blame] | 1437 | break; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1438 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1439 | case CBOR_SIMPLEV_FALSE: /* 20 */ |
| 1440 | case CBOR_SIMPLEV_TRUE: /* 21 */ |
| 1441 | case CBOR_SIMPLEV_NULL: /* 22 */ |
| 1442 | case CBOR_SIMPLEV_UNDEF: /* 23 */ |
| 1443 | case CBOR_SIMPLE_BREAK: /* 31 */ |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1444 | #ifndef QCBOR_DISABLE_DECODE_CONFORMANCE |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1445 | if(uDecodeMode3Bit >= QCBOR_ENCODE_MODE_DCBOR && |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1446 | nAdditionalInfo == CBOR_SIMPLEV_UNDEF) { |
| 1447 | uReturn = QCBOR_ERR_DCBOR_CONFORMANCE; |
| 1448 | goto Done; |
| 1449 | } |
| 1450 | #endif /* ! QCBOR_DISABLE_DECODE_CONFORMANCE */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1451 | break; /* nothing to do */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1452 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1453 | case CBOR_SIMPLEV_ONEBYTE: /* 24 */ |
| 1454 | if(uArgument <= CBOR_SIMPLE_BREAK) { |
| 1455 | /* This takes out f8 00 ... f8 1f which should be encoded |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1456 | * as e0 … f7 -- preferred serialization check for simple values. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1457 | */ |
| 1458 | uReturn = QCBOR_ERR_BAD_TYPE_7; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1459 | goto Done; |
| 1460 | } |
Laurence Lundblade | 5e39082 | 2019-01-06 12:35:01 -0800 | [diff] [blame] | 1461 | /* FALLTHROUGH */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1462 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1463 | default: /* 0-19 */ |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1464 | #ifndef QCBOR_DISABLE_DECODE_CONFORMANCE |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1465 | if(uDecodeMode3Bit >= QCBOR_ENCODE_MODE_DCBOR && |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1466 | (uArgument < CBOR_SIMPLEV_FALSE || uArgument > CBOR_SIMPLEV_NULL)) { |
| 1467 | uReturn = QCBOR_ERR_DCBOR_CONFORMANCE; |
| 1468 | goto Done; |
| 1469 | } |
| 1470 | #endif /* !QCBOR_DISABLE_DECODE_CONFORMANCE */ |
| 1471 | |
| 1472 | pDecodedItem->uDataType = QCBOR_TYPE_UKNOWN_SIMPLE; |
| 1473 | /* QCBOR_Private_DecodeHead() will make uArgument equal to |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1474 | * nAdditionalInfo when nAdditionalInfo is < 24. This cast is |
| 1475 | * safe because the 2, 4 and 8 byte lengths of uNumber are in |
| 1476 | * the double/float cases above |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1477 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1478 | pDecodedItem->val.uSimple = (uint8_t)uArgument; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1479 | break; |
| 1480 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1481 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1482 | Done: |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1483 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1487 | /** |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1488 | * @brief Decode a single primitive data item (decode layer 6). |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1489 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1490 | * @param[in] pMe Decoder context. |
| 1491 | * @param[in] bAllocateStrings If true, use allocator for strings. |
| 1492 | * @param[out] pDecodedItem The filled-in decoded item. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1493 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1494 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1495 | * features |
| 1496 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1497 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1498 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1499 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1500 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Allocation requested, but no allocator |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1501 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1502 | * of half-precision disabled |
| 1503 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1504 | * float decode is disabled. |
| 1505 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1506 | * simple type in input. |
| 1507 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1508 | * in input, but indefinite |
| 1509 | * lengths disabled. |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1510 | * @retval QCBOR_ERR_BAD_INT nAdditionalInfo indicated indefinte. |
| 1511 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG Too many items in array/map. |
| 1512 | * @retval QCBOR_ERR_TAGS_DISABLED QCBOR_DISABLE_TAGS is defined. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1513 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1514 | * This decodes the most primitive/atomic data item. It does no |
| 1515 | * combining of data items. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1516 | */ |
| 1517 | static QCBORError |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1518 | QCBOR_Private_DecodeAtomicDataItem(QCBORDecodeContext *pMe, |
| 1519 | const bool bAllocateStrings, |
| 1520 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1521 | { |
| 1522 | QCBORError uReturn; |
Laurence Lundblade | de2e150 | 2024-08-26 11:37:05 -0700 | [diff] [blame] | 1523 | int nMajorType = 0; |
| 1524 | uint64_t uArgument = 0; |
| 1525 | int nAdditionalInfo = 0; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1526 | uint8_t uDecodeMode3Bit = pMe->uDecodeMode & QCBOR_DECODE_MODE_MASK; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1527 | |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 1528 | memset(pDecodedItem, 0, sizeof(QCBORItem)); |
| 1529 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1530 | /* Decode the "head" that every CBOR item has into the major type, |
| 1531 | * argument and the additional info. |
| 1532 | */ |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1533 | uReturn = QCBOR_Private_DecodeHead(&(pMe->InBuf), |
| 1534 | #ifndef QCBOR_DISABLE_DECODE_CONFORMANCE |
| 1535 | // TODO: make this prettier; will optimizer take out stuff without ifdef? |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1536 | uDecodeMode3Bit >= QCBOR_DECODE_MODE_PREFERRED, |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1537 | #endif /* !QCBOR_DISABLE_DECODE_CONFORMANCE */ |
| 1538 | &nMajorType, |
| 1539 | &uArgument, |
| 1540 | &nAdditionalInfo); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 1541 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1542 | if(uReturn != QCBOR_SUCCESS) { |
| 1543 | return uReturn; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1544 | } |
| 1545 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1546 | /* All the functions below get inlined by the optimizer. This code |
| 1547 | * is easier to read with them all being similar functions, even if |
| 1548 | * some functions don't do much. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1549 | */ |
| 1550 | switch (nMajorType) { |
| 1551 | case CBOR_MAJOR_TYPE_POSITIVE_INT: /* Major type 0 */ |
| 1552 | case CBOR_MAJOR_TYPE_NEGATIVE_INT: /* Major type 1 */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1553 | return QCBOR_Private_DecodeInteger(nMajorType, uArgument, nAdditionalInfo, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1554 | break; |
| 1555 | |
| 1556 | case CBOR_MAJOR_TYPE_BYTE_STRING: /* Major type 2 */ |
| 1557 | case CBOR_MAJOR_TYPE_TEXT_STRING: /* Major type 3 */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1558 | return QCBOR_Private_DecodeString(pMe, bAllocateStrings, nMajorType, uArgument, nAdditionalInfo, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1559 | break; |
| 1560 | |
| 1561 | case CBOR_MAJOR_TYPE_ARRAY: /* Major type 4 */ |
| 1562 | case CBOR_MAJOR_TYPE_MAP: /* Major type 5 */ |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1563 | return QCBOR_Private_DecodeArrayOrMap(uDecodeMode3Bit, nMajorType, uArgument, nAdditionalInfo, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1564 | break; |
| 1565 | |
| 1566 | case CBOR_MAJOR_TYPE_TAG: /* Major type 6, tag numbers */ |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1567 | return QCBOR_Private_DecodeTagNumber(uArgument, nAdditionalInfo, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1568 | break; |
| 1569 | |
| 1570 | case CBOR_MAJOR_TYPE_SIMPLE: |
| 1571 | /* Major type 7: float, double, true, false, null... */ |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1572 | return QCBOR_Private_DecodeType7(uDecodeMode3Bit, nAdditionalInfo, uArgument, pDecodedItem); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1573 | break; |
| 1574 | |
| 1575 | default: |
| 1576 | /* Never happens because DecodeHead() should never return > 7 */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1577 | return QCBOR_ERR_UNSUPPORTED; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1578 | break; |
| 1579 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | |
| 1583 | /** |
| 1584 | * @brief Process indefinite-length strings (decode layer 5). |
| 1585 | * |
| 1586 | * @param[in] pMe Decoder context |
| 1587 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1588 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1589 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1590 | * features |
| 1591 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1592 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1593 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1594 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1595 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1596 | * of half-precision disabled |
| 1597 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1598 | * float decode is disabled. |
| 1599 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1600 | * simple type in input. |
| 1601 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1602 | * in input, but indefinite |
| 1603 | * lengths disabled. |
| 1604 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 1605 | * but no string allocator. |
| 1606 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 1607 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 1608 | * input, but indefinite-length |
| 1609 | * strings are disabled. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1610 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1611 | * If @c pDecodedItem is not an indefinite-length string, this does nothing. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1612 | * |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1613 | * If it is, this loops getting the subsequent chunk data items that |
| 1614 | * make up the string. The string allocator is used to make a |
| 1615 | * contiguous buffer for the chunks. When this completes @c |
| 1616 | * pDecodedItem contains the put-together string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1617 | * |
| 1618 | * Code Reviewers: THIS FUNCTION DOES A LITTLE POINTER MATH |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1619 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1620 | static QCBORError |
| 1621 | QCBORDecode_Private_GetNextFullString(QCBORDecodeContext *pMe, |
| 1622 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1623 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1624 | /* Aproximate stack usage |
| 1625 | * 64-bit 32-bit |
| 1626 | * local vars 32 16 |
| 1627 | * 2 UsefulBufs 32 16 |
| 1628 | * QCBORItem 56 52 |
| 1629 | * TOTAL 120 74 |
| 1630 | */ |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1631 | QCBORError uReturn; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1632 | |
| 1633 | /* A note about string allocation -- Memory for strings is |
| 1634 | * allocated either because 1) indefinte-length string chunks are |
| 1635 | * being coalecsed or 2) caller has requested all strings be |
| 1636 | * allocated. The first case is handed below here. The second case |
| 1637 | * is handled in DecodeString if the bAllocate is true. That |
| 1638 | * boolean originates here with pMe->bStringAllocateAll immediately |
| 1639 | * below. That is, QCBOR_Private_DecodeAtomicDataItem() is called |
| 1640 | * in two different contexts here 1) main-line processing which is |
| 1641 | * where definite-length strings need to be allocated if |
| 1642 | * bStringAllocateAll is true and 2) processing chunks of |
| 1643 | * indefinite-lengths strings in in which case there must be no |
| 1644 | * allocation. |
| 1645 | */ |
| 1646 | |
| 1647 | |
| 1648 | uReturn = QCBOR_Private_DecodeAtomicDataItem(pMe, pMe->bStringAllocateAll, pDecodedItem); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1649 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1650 | goto Done; |
| 1651 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1652 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1653 | /* This is where out-of-place break is detected for the whole |
| 1654 | * decoding stack. Break is an error for everything that calls |
| 1655 | * QCBORDecode_Private_GetNextFullString(), so the check is |
| 1656 | * centralized here. |
| 1657 | */ |
| 1658 | if(pDecodedItem->uDataType == QCBOR_TYPE_BREAK) { |
| 1659 | uReturn = QCBOR_ERR_BAD_BREAK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1660 | goto Done; |
| 1661 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1662 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1663 | |
| 1664 | /* Skip out if not an indefinite-length string */ |
| 1665 | const uint8_t uStringType = pDecodedItem->uDataType; |
| 1666 | if(uStringType != QCBOR_TYPE_BYTE_STRING && |
| 1667 | uStringType != QCBOR_TYPE_TEXT_STRING) { |
| 1668 | goto Done; |
| 1669 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1670 | if(pDecodedItem->val.string.len != QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1671 | goto Done; |
| 1672 | } |
| 1673 | |
| 1674 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1675 | /* Can't decode indefinite-length strings without a string allocator */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1676 | if(!pMe->StringAllocator.pfAllocator) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1677 | uReturn = QCBOR_ERR_NO_STRING_ALLOCATOR; |
| 1678 | goto Done; |
| 1679 | } |
| 1680 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1681 | /* Loop getting chunks of the indefinite-length string */ |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1682 | UsefulBufC FullString = NULLUsefulBufC; |
| 1683 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1684 | for(;;) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1685 | /* Get QCBORItem for next chunk */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1686 | QCBORItem StringChunkItem; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1687 | /* Pass false to DecodeAtomicDataItem() because the individual |
| 1688 | * string chunks in an indefinite-length must not be |
| 1689 | * allocated. They are always copied into the allocated |
| 1690 | * contiguous buffer allocated here. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1691 | */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1692 | uReturn = QCBOR_Private_DecodeAtomicDataItem(pMe, false, &StringChunkItem); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1693 | if(uReturn) { |
| 1694 | break; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1695 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1696 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1697 | /* Is item is the marker for end of the indefinite-length string? */ |
Laurence Lundblade | 2a6850e | 2018-10-28 20:13:44 +0700 | [diff] [blame] | 1698 | if(StringChunkItem.uDataType == QCBOR_TYPE_BREAK) { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1699 | /* String is complete */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1700 | pDecodedItem->val.string = FullString; |
Laurence Lundblade | 57dd144 | 2018-10-15 20:26:28 +0530 | [diff] [blame] | 1701 | pDecodedItem->uDataAlloc = 1; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1702 | break; |
| 1703 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1704 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1705 | /* 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] | 1706 | * that introduces the indefinite-length string. This also |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1707 | * 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] | 1708 | * indefinite-length string inside an indefinite-length string. |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1709 | */ |
Laurence Lundblade | c5fef68 | 2020-01-25 11:38:45 -0800 | [diff] [blame] | 1710 | if(StringChunkItem.uDataType != uStringType || |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1711 | StringChunkItem.val.string.len == QCBOR_STRING_LENGTH_INDEFINITE) { |
| 1712 | uReturn = QCBOR_ERR_INDEFINITE_STRING_CHUNK; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1713 | break; |
| 1714 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1715 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1716 | if (StringChunkItem.val.string.len > 0) { |
| 1717 | /* The first time throurgh FullString.ptr is NULL and this is |
| 1718 | * equivalent to StringAllocator_Allocate(). Subsequently it is |
| 1719 | * not NULL and a reallocation happens. |
| 1720 | */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1721 | UsefulBuf NewMem = StringAllocator_Reallocate(&(pMe->StringAllocator), |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1722 | FullString.ptr, |
| 1723 | FullString.len + StringChunkItem.val.string.len); |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1724 | if(UsefulBuf_IsNULL(NewMem)) { |
| 1725 | uReturn = QCBOR_ERR_STRING_ALLOCATE; |
| 1726 | break; |
| 1727 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1728 | |
David Navarro | 9123e5b | 2022-03-28 16:04:03 +0200 | [diff] [blame] | 1729 | /* Copy new string chunk to the end of accumulated string */ |
| 1730 | FullString = UsefulBuf_CopyOffset(NewMem, FullString.len, StringChunkItem.val.string); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1731 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1732 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1733 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1734 | if(uReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) { |
| 1735 | /* Getting the item failed, clean up the allocated memory */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1736 | StringAllocator_Free(&(pMe->StringAllocator), FullString.ptr); |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1737 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1738 | #else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
| 1739 | uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED; |
| 1740 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1741 | |
Laurence Lundblade | d6dfe6d | 2020-04-09 10:21:36 -0700 | [diff] [blame] | 1742 | Done: |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1743 | return uReturn; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 1744 | } |
| 1745 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 1746 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1747 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1748 | /** |
| 1749 | * @brief This converts a tag number to a shorter mapped value for storage. |
| 1750 | * |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1751 | * @param[in] pMe The decode context. |
| 1752 | * @param[in] uUnMappedTag The tag number to map |
| 1753 | * @param[out] puMappedTagNumber The stored tag number. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1754 | * |
| 1755 | * @return error code. |
| 1756 | * |
| 1757 | * The main point of mapping tag numbers is make QCBORItem |
| 1758 | * smaller. With this mapping storage of 4 tags takes up 8 |
| 1759 | * bytes. Without, it would take up 32 bytes. |
| 1760 | * |
| 1761 | * This maps tag numbers greater than QCBOR_LAST_UNMAPPED_TAG. |
| 1762 | * QCBOR_LAST_UNMAPPED_TAG is a little smaller than MAX_UINT16. |
| 1763 | * |
| 1764 | * See also UnMapTagNumber() and @ref QCBORItem. |
| 1765 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1766 | static QCBORError |
| 1767 | QCBORDecode_Private_MapTagNumber(QCBORDecodeContext *pMe, |
| 1768 | const uint64_t uUnMappedTag, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1769 | uint16_t *puMappedTagNumber) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1770 | { |
| 1771 | if(uUnMappedTag > QCBOR_LAST_UNMAPPED_TAG) { |
| 1772 | unsigned uTagMapIndex; |
| 1773 | /* Is there room in the tag map, or is it in it already? */ |
| 1774 | for(uTagMapIndex = 0; uTagMapIndex < QCBOR_NUM_MAPPED_TAGS; uTagMapIndex++) { |
| 1775 | if(pMe->auMappedTags[uTagMapIndex] == CBOR_TAG_INVALID64) { |
| 1776 | break; |
| 1777 | } |
| 1778 | if(pMe->auMappedTags[uTagMapIndex] == uUnMappedTag) { |
| 1779 | break; |
| 1780 | } |
| 1781 | } |
| 1782 | if(uTagMapIndex >= QCBOR_NUM_MAPPED_TAGS) { |
| 1783 | return QCBOR_ERR_TOO_MANY_TAGS; |
| 1784 | } |
| 1785 | |
| 1786 | /* Covers the cases where tag is new and were it is already in the map */ |
| 1787 | pMe->auMappedTags[uTagMapIndex] = uUnMappedTag; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1788 | *puMappedTagNumber = (uint16_t)(uTagMapIndex + QCBOR_LAST_UNMAPPED_TAG + 1); |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1789 | |
| 1790 | } else { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1791 | *puMappedTagNumber = (uint16_t)uUnMappedTag; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | return QCBOR_SUCCESS; |
| 1795 | } |
| 1796 | |
| 1797 | |
| 1798 | /** |
| 1799 | * @brief This converts a mapped tag number to the actual tag number. |
| 1800 | * |
| 1801 | * @param[in] pMe The decode context. |
| 1802 | * @param[in] uMappedTagNumber The stored tag number. |
| 1803 | * |
| 1804 | * @return The actual tag number is returned or |
| 1805 | * @ref CBOR_TAG_INVALID64 on error. |
| 1806 | * |
| 1807 | * This is the reverse of MapTagNumber() |
| 1808 | */ |
| 1809 | static uint64_t |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1810 | QCBORDecode_Private_UnMapTagNumber(const QCBORDecodeContext *pMe, |
| 1811 | const uint16_t uMappedTagNumber) |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1812 | { |
| 1813 | if(uMappedTagNumber <= QCBOR_LAST_UNMAPPED_TAG) { |
| 1814 | return uMappedTagNumber; |
| 1815 | } else if(uMappedTagNumber == CBOR_TAG_INVALID16) { |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1816 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1817 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1818 | /* This won't be negative because of code below in |
| 1819 | * MapTagNumber() |
| 1820 | */ |
| 1821 | const unsigned uIndex = uMappedTagNumber - (QCBOR_LAST_UNMAPPED_TAG + 1); |
| 1822 | return pMe->auMappedTags[uIndex]; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1823 | } |
| 1824 | } |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1825 | |
| 1826 | |
| 1827 | static const struct QCBORTagDecoderEntry * |
| 1828 | QCBORDecode_Private_LookUpTagDecoder(const struct QCBORTagDecoderEntry *pTagContentTable, |
| 1829 | const uint64_t uTagNumber) |
| 1830 | { |
| 1831 | const struct QCBORTagDecoderEntry *pTE; |
| 1832 | |
| 1833 | if(pTagContentTable == NULL) { |
| 1834 | return NULL; |
| 1835 | } |
| 1836 | |
| 1837 | for(pTE = pTagContentTable; pTE->uTagNumber != CBOR_TAG_INVALID64; pTE++) { |
| 1838 | if(pTE->uTagNumber == uTagNumber || pTE->uTagNumber == CBOR_TAG_ANY) { |
| 1839 | break; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | if(pTE->uTagNumber == CBOR_TAG_INVALID64) { |
| 1844 | return NULL; |
| 1845 | } |
| 1846 | |
| 1847 | return pTE; |
| 1848 | } |
| 1849 | #endif /* ! QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1850 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1851 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1852 | /** |
| 1853 | * @brief Aggregate all tags wrapping a data item (decode layer 4). |
| 1854 | * |
| 1855 | * @param[in] pMe Decoder context |
| 1856 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1857 | * |
| 1858 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1859 | * features |
| 1860 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1861 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1862 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1863 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1864 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1865 | * of half-precision disabled |
| 1866 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1867 | * float decode is disabled. |
| 1868 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1869 | * simple type in input. |
| 1870 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1871 | * in input, but indefinite |
| 1872 | * lengths disabled. |
| 1873 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 1874 | * but no string allocator. |
| 1875 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 1876 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 1877 | * input, but indefinite-length |
| 1878 | * strings are disabled. |
| 1879 | * @retval QCBOR_ERR_TOO_MANY_TAGS Too many tag numbers on item. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1880 | * |
| 1881 | * This loops getting atomic data items until one is not a tag |
| 1882 | * number. Usually this is largely pass-through because most |
| 1883 | * item are not tag numbers. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1884 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 1885 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1886 | QCBORDecode_Private_GetNextTagNumber(QCBORDecodeContext *pMe, |
| 1887 | QCBORItem *pDecodedItem) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1888 | { |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1889 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1890 | int nIndex; |
| 1891 | QCBORError uErr; |
| 1892 | uint16_t uMappedTagNumber; |
| 1893 | QCBORError uReturn; |
| 1894 | |
| 1895 | /* Accummulate the tag numbers from multiple items here and then |
| 1896 | * copy them into the last item, the non-tag-number item. |
Laurence Lundblade | a9e7ab9 | 2022-03-23 10:28:48 +0100 | [diff] [blame] | 1897 | */ |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1898 | QCBORMappedTagNumbers auTagNumbers;; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1899 | |
| 1900 | /* Initialize to CBOR_TAG_INVALID16 */ |
| 1901 | #if CBOR_TAG_INVALID16 != 0xffff |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1902 | /* Be sure the memset is initializing to CBOR_TAG_INVALID16 */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1903 | #err CBOR_TAG_INVALID16 tag not defined as expected |
| 1904 | #endif |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1905 | memset(auTagNumbers, 0xff, sizeof(auTagNumbers)); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1906 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1907 | /* Loop fetching data items until the item fetched is not a tag number */ |
| 1908 | uReturn = QCBOR_SUCCESS; |
| 1909 | for(nIndex = 0; ; nIndex++) { |
| 1910 | uErr = QCBORDecode_Private_GetNextFullString(pMe, pDecodedItem); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1911 | if(uErr != QCBOR_SUCCESS) { |
| 1912 | uReturn = uErr; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1913 | break; |
| 1914 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1915 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1916 | if(pDecodedItem->uDataType != QCBOR_TYPE_TAG_NUMBER) { |
| 1917 | /* Successful exit from loop; maybe got some tags, maybe not */ |
| 1918 | memcpy(pDecodedItem->auTagNumbers, auTagNumbers, sizeof(auTagNumbers)); |
| 1919 | break; |
| 1920 | } |
| 1921 | |
| 1922 | if(nIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 1923 | /* No room in the item's tag number array */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1924 | uReturn = QCBOR_ERR_TOO_MANY_TAGS; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1925 | /* Continue on to get all tag numbers wrapping this item even |
| 1926 | * though it is erroring out in the end. This allows decoding |
| 1927 | * to continue. This is a QCBOR resource limit error, not a |
| 1928 | * problem with being well-formed CBOR. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1929 | */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1930 | continue; |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 1931 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1932 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1933 | /* Map the tag number */ |
| 1934 | uMappedTagNumber = 0; |
| 1935 | uReturn = QCBORDecode_Private_MapTagNumber(pMe, pDecodedItem->val.uTagNumber, &uMappedTagNumber); |
| 1936 | /* Continue even on error so as to consume all tag numbers |
| 1937 | * wrapping this data item so decoding can go on. If |
| 1938 | * QCBORDecode_Private_MapTagNumber() errors once it will |
| 1939 | * continue to error. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1940 | */ |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1941 | |
| 1942 | auTagNumbers[nIndex] = uMappedTagNumber; |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 1943 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 1944 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 1945 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1946 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1947 | #else /* ! QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1948 | |
Laurence Lundblade | 0f01ac6 | 2024-02-18 23:07:07 -0700 | [diff] [blame] | 1949 | return QCBORDecode_Private_GetNextFullString(pMe, pDecodedItem); |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1950 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 1951 | #endif /* ! QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 1952 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1953 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1954 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1955 | /** |
| 1956 | * @brief Combine a map entry label and value into one item (decode layer 3). |
| 1957 | * |
| 1958 | * @param[in] pMe Decoder context |
| 1959 | * @param[out] pDecodedItem The decoded item that work is done on. |
| 1960 | * |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 1961 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 1962 | * features |
| 1963 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 1964 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 1965 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 1966 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 1967 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 1968 | * of half-precision disabled |
| 1969 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 1970 | * float decode is disabled. |
| 1971 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 1972 | * simple type in input. |
| 1973 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 1974 | * in input, but indefinite |
| 1975 | * lengths disabled. |
| 1976 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 1977 | * but no string allocator. |
| 1978 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 1979 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 1980 | * input, but indefinite-length |
| 1981 | * strings are disabled. |
| 1982 | * @retval QCBOR_ERR_TOO_MANY_TAGS Too many tag numbers on item. |
| 1983 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG Too many items in array. |
| 1984 | * @retval QCBOR_ERR_MAP_LABEL_TYPE Map label not string or integer. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1985 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1986 | * If the current nesting level is a map, then this combines pairs of |
| 1987 | * items into one data item with a label and value. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1988 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1989 | * This is passthrough if the current nesting level is not a map. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 1990 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1991 | * This also implements maps-as-array mode where a map is treated like |
| 1992 | * an array to allow caller to do their own label processing. |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1993 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 1994 | static QCBORError |
| 1995 | QCBORDecode_Private_GetNextMapEntry(QCBORDecodeContext *pMe, |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 1996 | QCBORItem *pDecodedItem, |
| 1997 | uint32_t *puLabelEndOffset) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 1998 | { |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 1999 | QCBORItem LabelItem; |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2000 | QCBORError uErr, uErr2; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2001 | |
| 2002 | uErr = QCBORDecode_Private_GetNextTagNumber(pMe, pDecodedItem); |
| 2003 | if(QCBORDecode_IsUnrecoverableError(uErr)) { |
Laurence Lundblade | 742df4a | 2018-10-13 20:07:17 +0800 | [diff] [blame] | 2004 | goto Done; |
| 2005 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2006 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2007 | if(!DecodeNesting_IsCurrentTypeMap(&(pMe->nesting))) { |
| 2008 | /* Not decoding a map. Nothing to do. */ |
| 2009 | /* When decoding maps-as-arrays, the type will be |
| 2010 | * QCBOR_TYPE_MAP_AS_ARRAY and this function will exit |
| 2011 | * here. This is now map processing for maps-as-arrays is not |
| 2012 | * done. */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2013 | goto Done; |
| 2014 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 2015 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2016 | /* Decoding a map entry, so the item decoded above was the label */ |
| 2017 | LabelItem = *pDecodedItem; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2018 | |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2019 | if(puLabelEndOffset != NULL) { |
| 2020 | /* Cast is OK because lengths are all 32-bit in QCBOR */ |
| 2021 | *puLabelEndOffset = (uint32_t)UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 2022 | } |
Laurence Lundblade | d61cbf3 | 2018-12-09 11:42:21 -0800 | [diff] [blame] | 2023 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2024 | /* Get the value of the map item */ |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2025 | uErr2 = QCBORDecode_Private_GetNextTagNumber(pMe, pDecodedItem); |
| 2026 | if(QCBORDecode_IsUnrecoverableError(uErr2)) { |
| 2027 | uErr = uErr2; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2028 | goto Done; |
| 2029 | } |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2030 | if(uErr2 != QCBOR_SUCCESS) { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2031 | /* The recoverable error for the value overrides the recoverable |
| 2032 | * error for the label, if there was an error for the label */ |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2033 | uErr = uErr2; |
| 2034 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2035 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2036 | /* Combine the label item and value item into one */ |
| 2037 | pDecodedItem->uLabelAlloc = LabelItem.uDataAlloc; |
| 2038 | pDecodedItem->uLabelType = LabelItem.uDataType; |
Laurence Lundblade | ccfb8cd | 2018-12-07 21:11:30 +0900 | [diff] [blame] | 2039 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2040 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2041 | /* TODO: QCBOR_DECODE_MODE_MAP_STRINGS_ONLY might have been a bad idea. Maybe |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2042 | * get rid of it in QCBOR 2.0 |
| 2043 | */ |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2044 | if((pMe->uDecodeMode & QCBOR_DECODE_MODE_MASK) == QCBOR_DECODE_MODE_MAP_STRINGS_ONLY && |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2045 | LabelItem.uDataType != QCBOR_TYPE_TEXT_STRING) { |
| 2046 | uErr = QCBOR_ERR_MAP_LABEL_TYPE; |
| 2047 | goto Done; |
| 2048 | } |
| 2049 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 2050 | |
| 2051 | switch(LabelItem.uDataType) { |
| 2052 | case QCBOR_TYPE_INT64: |
| 2053 | pDecodedItem->label.int64 = LabelItem.val.int64; |
| 2054 | break; |
| 2055 | |
| 2056 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 2057 | case QCBOR_TYPE_UINT64: |
| 2058 | pDecodedItem->label.uint64 = LabelItem.val.uint64; |
| 2059 | break; |
| 2060 | |
| 2061 | case QCBOR_TYPE_TEXT_STRING: |
| 2062 | case QCBOR_TYPE_BYTE_STRING: |
| 2063 | pDecodedItem->label.string = LabelItem.val.string; |
| 2064 | break; |
| 2065 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 2066 | |
| 2067 | default: |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2068 | /* It is possible to skip over labels that are non-aggregate |
| 2069 | * types like floats, but not to skip over labels that are |
| 2070 | * arrays or maps. We might eventually handle more label |
| 2071 | * types like floats as they are not too hard and we now |
| 2072 | * have QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 2073 | if(!pMe->bAllowAllLabels || QCBORItem_IsMapOrArray(LabelItem)) { |
| 2074 | uErr = QCBOR_ERR_MAP_LABEL_TYPE; |
| 2075 | goto Done; |
| 2076 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2077 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2078 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2079 | Done: |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2080 | return uErr; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2084 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2085 | /** |
| 2086 | * @brief Peek and see if next data item is a break; |
| 2087 | * |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2088 | * param[in] pUIB UsefulInputBuf to read from. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2089 | * @param[out] pbNextIsBreak Indicate if next was a break or not. |
| 2090 | * |
| 2091 | * @return Any decoding error. |
| 2092 | * |
| 2093 | * See if next item is a CBOR break. If it is, it is consumed, |
| 2094 | * if not it is not consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2095 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2096 | static QCBORError |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2097 | QCBOR_Private_NextIsBreak(QCBORDecodeContext *pMe, bool *pbNextIsBreak) |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2098 | { |
| 2099 | *pbNextIsBreak = false; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2100 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf)) != 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2101 | QCBORItem Peek; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2102 | size_t uPeek = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 2103 | QCBORError uReturn = QCBOR_Private_DecodeAtomicDataItem(pMe, false, &Peek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2104 | if(uReturn != QCBOR_SUCCESS) { |
| 2105 | return uReturn; |
| 2106 | } |
| 2107 | if(Peek.uDataType != QCBOR_TYPE_BREAK) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2108 | /* It is not a break, rewind so it can be processed normally. */ |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2109 | UsefulInputBuf_Seek(&(pMe->InBuf), uPeek); |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2110 | } else { |
| 2111 | *pbNextIsBreak = true; |
| 2112 | } |
| 2113 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2114 | |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2115 | return QCBOR_SUCCESS; |
| 2116 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2117 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2118 | |
| 2119 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2120 | /** |
| 2121 | * @brief Ascend up nesting levels if all items in them have been consumed. |
| 2122 | * |
| 2123 | * @param[in] pMe The decode context. |
| 2124 | * @param[in] bMarkEnd If true mark end of maps/arrays with count of zero. |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2125 | * @param[out] pbBreak Set to true if extra break was consumed. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2126 | * |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2127 | * An item was just consumed, now figure out if it was the |
| 2128 | * end of an array/map map that can be closed out. That |
| 2129 | * may in turn close out the above array/map... |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2130 | * |
| 2131 | * When ascending indefinite-length arrays and maps, this will correctly |
| 2132 | * consume the break for the level above. This is a problem for the |
| 2133 | * implementation of QCBORDecode_GetArray() that must not return |
| 2134 | * that break. @c pbBreak is set to true to indicate that one |
| 2135 | * byte should be removed. |
| 2136 | * |
| 2137 | * Improvement: this could reduced further if indef is disabled |
| 2138 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2139 | static QCBORError |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2140 | QCBORDecode_Private_NestLevelAscender(QCBORDecodeContext *pMe, bool bMarkEnd, bool *pbBreak) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2141 | { |
| 2142 | QCBORError uReturn; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 2143 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2144 | /* Loop ascending nesting levels as long as there is ascending to do */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2145 | while(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2146 | if(pbBreak) { |
| 2147 | *pbBreak = false; |
| 2148 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2149 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2150 | if(DecodeNesting_IsCurrentBstrWrapped(&(pMe->nesting))) { |
| 2151 | /* Nesting level is bstr-wrapped CBOR */ |
| 2152 | |
| 2153 | /* Ascent for bstr-wrapped CBOR is always by explicit call |
| 2154 | * so no further ascending can happen. |
| 2155 | */ |
| 2156 | break; |
| 2157 | |
| 2158 | } else if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 2159 | /* Level is a definite-length array/map */ |
| 2160 | |
| 2161 | /* Decrement the item count the definite-length array/map */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2162 | DecodeNesting_DecrementDefiniteLengthMapOrArrayCount(&(pMe->nesting)); |
| 2163 | if(!DecodeNesting_IsEndOfDefiniteLengthMapOrArray(&(pMe->nesting))) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2164 | /* Didn't close out array/map, so all work here is done */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2165 | break; |
| 2166 | } |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2167 | /* All items in a definite-length array were consumed so it |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2168 | * is time to ascend one level. This happens below. |
| 2169 | */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2170 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2171 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2172 | } else { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2173 | /* Level is an indefinite-length array/map. */ |
| 2174 | |
| 2175 | /* Check for a break which is what ends indefinite-length arrays/maps */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2176 | bool bIsBreak = false; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 2177 | uReturn = QCBOR_Private_NextIsBreak(pMe, &bIsBreak); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2178 | if(uReturn != QCBOR_SUCCESS) { |
| 2179 | goto Done; |
| 2180 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2181 | |
| 2182 | if(!bIsBreak) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2183 | /* 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] | 2184 | break; |
| 2185 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2186 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2187 | /* It was a break in an indefinitelength map / array so |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2188 | * it is time to ascend one level. |
| 2189 | */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2190 | if(pbBreak) { |
| 2191 | *pbBreak = true; |
| 2192 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2193 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2194 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2195 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2196 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2197 | |
| 2198 | /* All items in the array/map have been consumed. */ |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2199 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 2200 | /* But ascent in bounded mode is only by explicit call to |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2201 | * QCBORDecode_ExitBoundedMode(). |
| 2202 | */ |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 2203 | if(DecodeNesting_IsCurrentBounded(&(pMe->nesting))) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2204 | /* Set the count to zero for definite-length arrays to indicate |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2205 | * cursor is at end of bounded array/map */ |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 2206 | if(bMarkEnd) { |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2207 | /* Used for definite and indefinite to signal end */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 2208 | DecodeNesting_ZeroMapOrArrayCount(&(pMe->nesting)); |
Laurence Lundblade | d030493 | 2020-06-27 10:59:38 -0700 | [diff] [blame] | 2209 | |
Laurence Lundblade | 2c1faf9 | 2020-06-26 22:43:56 -0700 | [diff] [blame] | 2210 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2211 | break; |
| 2212 | } |
| 2213 | |
| 2214 | /* Finally, actually ascend one level. */ |
| 2215 | DecodeNesting_Ascend(&(pMe->nesting)); |
| 2216 | } |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2217 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2218 | uReturn = QCBOR_SUCCESS; |
| 2219 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2220 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2221 | Done: |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2222 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */ |
| 2223 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2224 | return uReturn; |
| 2225 | } |
| 2226 | |
| 2227 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2228 | /** |
| 2229 | * @brief Ascending & Descending out of nesting levels (decode layer 2). |
| 2230 | * |
| 2231 | * @param[in] pMe Decoder context |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2232 | * @param[out] pbBreak Set to true if extra break was consumed. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2233 | * @param[out] pDecodedItem The decoded item that work is done on. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 2234 | |
| 2235 | * @retval QCBOR_ERR_UNSUPPORTED Encountered unsupported/reserved |
| 2236 | * features |
| 2237 | * @retval QCBOR_ERR_HIT_END Unexpected end of input |
| 2238 | * @retval QCBOR_ERR_INT_OVERFLOW Too-large negative encountered |
| 2239 | * @retval QCBOR_ERR_STRING_ALLOCATE Out of memory. |
| 2240 | * @retval QCBOR_ERR_STRING_TOO_LONG String longer than SIZE_MAX - 4. |
| 2241 | * @retval QCBOR_ERR_HALF_PRECISION_DISABLED Half-precision in input, but decode |
| 2242 | * of half-precision disabled |
| 2243 | * @retval QCBOR_ERR_ALL_FLOAT_DISABLED Float-point in input, but all |
| 2244 | * float decode is disabled. |
| 2245 | * @retval QCBOR_ERR_BAD_TYPE_7 Not-allowed representation of |
| 2246 | * simple type in input. |
| 2247 | * @retval QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED Indefinite length map/array |
| 2248 | * in input, but indefinite |
| 2249 | * lengths disabled. |
| 2250 | * @retval QCBOR_ERR_NO_STRING_ALLOCATOR Indefinite-length string in input, |
| 2251 | * but no string allocator. |
| 2252 | * @retval QCBOR_ERR_INDEFINITE_STRING_CHUNK Error in indefinite-length string. |
| 2253 | * @retval QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED Indefinite-length string in |
| 2254 | * input, but indefinite-length |
| 2255 | * strings are disabled. |
| 2256 | * @retval QCBOR_ERR_TOO_MANY_TAGS Too many tag numbers on item. |
| 2257 | * @retval QCBOR_ERR_ARRAY_DECODE_TOO_LONG Too many items in array. |
| 2258 | * @retval QCBOR_ERR_MAP_LABEL_TYPE Map label not string or integer. |
| 2259 | * @retval QCBOR_ERR_NO_MORE_ITEMS Need more items for map or array. |
| 2260 | * @retval QCBOR_ERR_BAD_BREAK Indefinite-length break in wrong |
| 2261 | * place. |
| 2262 | * @retval QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP Nesting deeper than QCBOR |
| 2263 | * can handle. |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2264 | * |
| 2265 | * This handles the traversal descending into and asecnding out of |
| 2266 | * maps, arrays and bstr-wrapped CBOR. It figures out the ends of |
| 2267 | * definite- and indefinte-length maps and arrays by looking at the |
| 2268 | * item count or finding CBOR breaks. It detects the ends of the |
| 2269 | * top-level sequence and of bstr-wrapped CBOR by byte count. |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2270 | */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2271 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2272 | QCBORDecode_Private_GetNextMapOrArray(QCBORDecodeContext *pMe, |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2273 | bool *pbBreak, |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2274 | QCBORItem *pDecodedItem, |
| 2275 | uint32_t *puLabelEndOffset) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2276 | { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2277 | QCBORError uReturn; |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 2278 | /* ==== First: figure out if at the end of a traversal ==== */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2279 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2280 | /* If out of bytes to consume, it is either the end of the |
| 2281 | * top-level sequence of some bstr-wrapped CBOR that was entered. |
| 2282 | * |
| 2283 | * In the case of bstr-wrapped CBOR, the length of the |
| 2284 | * UsefulInputBuf was set to that of the bstr-wrapped CBOR. When |
| 2285 | * the bstr-wrapped CBOR is exited, the length is set back to the |
| 2286 | * top-level's length or to the next highest bstr-wrapped CBOR. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2287 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2288 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf)) == 0) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2289 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2290 | goto Done; |
| 2291 | } |
Laurence Lundblade | 937ea81 | 2020-05-08 11:38:23 -0700 | [diff] [blame] | 2292 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2293 | /* Check to see if at the end of a bounded definite-length map or |
| 2294 | * array. The check for a break ending indefinite-length array is |
| 2295 | * later in QCBORDecode_NestLevelAscender(). |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 2296 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2297 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2298 | uReturn = QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 2299 | goto Done; |
| 2300 | } |
Laurence Lundblade | bb1062e | 2019-08-12 23:28:54 -0700 | [diff] [blame] | 2301 | |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 2302 | /* ==== Next: not at the end, so get another item ==== */ |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2303 | uReturn = QCBORDecode_Private_GetNextMapEntry(pMe, pDecodedItem, puLabelEndOffset); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2304 | if(QCBORDecode_IsUnrecoverableError(uReturn)) { |
| 2305 | /* Error is so bad that traversal is not possible. */ |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2306 | goto Done; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2307 | } |
Laurence Lundblade | 7e0d13b | 2018-10-16 19:54:13 +0530 | [diff] [blame] | 2308 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2309 | /* Record the nesting level for this data item before processing |
| 2310 | * any of decrementing and descending. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2311 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2312 | pDecodedItem->uNestingLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2313 | |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2314 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 2315 | /* ==== Next: Process the item for descent, ascent, decrement... ==== */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2316 | if(QCBORItem_IsMapOrArray(*pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2317 | /* If the new item is a map or array, descend. |
| 2318 | * |
| 2319 | * Empty indefinite-length maps and arrays are descended into, |
| 2320 | * but then ascended out of in the next chunk of code. |
| 2321 | * |
| 2322 | * Maps and arrays do count as items in the map/array that |
| 2323 | * encloses them so a decrement needs to be done for them too, |
| 2324 | * but that is done only when all the items in them have been |
| 2325 | * processed, not when they are opened with the exception of an |
| 2326 | * empty map or array. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2327 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2328 | QCBORError uDescendErr; |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2329 | uDescendErr = DecodeNesting_DescendMapOrArray(&(pMe->nesting), |
Laurence Lundblade | b95afec | 2024-08-26 10:51:28 -0700 | [diff] [blame] | 2330 | pDecodedItem->uDataType, |
| 2331 | pDecodedItem->val.uCount); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2332 | if(uDescendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2333 | /* This error is probably a traversal error and it overrides |
| 2334 | * the non-traversal error. |
| 2335 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2336 | uReturn = uDescendErr; |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2337 | goto Done; |
| 2338 | } |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 2339 | } |
| 2340 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2341 | if(!QCBORItem_IsMapOrArray(*pDecodedItem) || |
| 2342 | QCBORItem_IsEmptyDefiniteLengthMapOrArray(*pDecodedItem) || |
| 2343 | QCBORItem_IsIndefiniteLengthMapOrArray(*pDecodedItem)) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2344 | /* The following cases are handled here: |
| 2345 | * - A non-aggregate item like an integer or string |
| 2346 | * - An empty definite-length map or array |
| 2347 | * - An indefinite-length map or array that might be empty or might not. |
| 2348 | * |
| 2349 | * QCBORDecode_NestLevelAscender() does the work of decrementing the count |
| 2350 | * for an definite-length map/array and break detection for an |
| 2351 | * indefinite-0length map/array. If the end of the map/array was |
| 2352 | * reached, then it ascends nesting levels, possibly all the way |
| 2353 | * to the top level. |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2354 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2355 | QCBORError uAscendErr; |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 2356 | uAscendErr = QCBORDecode_Private_NestLevelAscender(pMe, true, pbBreak); |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2357 | if(uAscendErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2358 | /* This error is probably a traversal error and it overrides |
| 2359 | * the non-traversal error. |
| 2360 | */ |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2361 | uReturn = uAscendErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 2362 | goto Done; |
Laurence Lundblade | 5e87da6 | 2020-06-07 03:24:28 -0700 | [diff] [blame] | 2363 | } |
Laurence Lundblade | 6de3706 | 2018-10-15 12:22:42 +0530 | [diff] [blame] | 2364 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2365 | |
Laurence Lundblade | eaa75a5 | 2020-07-09 18:13:23 -0700 | [diff] [blame] | 2366 | /* ==== Last: tell the caller the nest level of the next item ==== */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2367 | /* Tell the caller what level is next. This tells them what |
| 2368 | * maps/arrays were closed out and makes it possible for them to |
| 2369 | * reconstruct the tree with just the information returned in a |
| 2370 | * QCBORItem. |
Laurence Lundblade | 642282a | 2020-06-23 12:00:33 -0700 | [diff] [blame] | 2371 | */ |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2372 | if(DecodeNesting_IsAtEndOfBoundedLevel(&(pMe->nesting))) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 2373 | /* At end of a bounded map/array; uNextNestLevel 0 to indicate this */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2374 | pDecodedItem->uNextNestLevel = 0; |
| 2375 | } else { |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2376 | pDecodedItem->uNextNestLevel = DecodeNesting_GetCurrentLevel(&(pMe->nesting)); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 2377 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2378 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2379 | Done: |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 2380 | return uReturn; |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2381 | } |
| 2382 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2383 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2384 | /** |
| 2385 | * @brief Decode tag content for select tags (decoding layer 1). |
| 2386 | * |
| 2387 | * @param[in] pMe The decode context. |
| 2388 | * @param[out] pDecodedItem The decoded item. |
| 2389 | * |
| 2390 | * @return Decoding error code. |
| 2391 | * |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2392 | * CBOR tag numbers for the item were decoded in GetNext_TaggedItem(), |
| 2393 | * but the whole tag was not decoded. Here, the whole tags (tag number |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2394 | * and tag content) are decoded. This is a |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2395 | * quick pass through for items that are not tags. |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2396 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2397 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2398 | QCBORDecode_Private_GetNextTagContent(QCBORDecodeContext *pMe, |
| 2399 | QCBORItem *pDecodedItem) |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2400 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2401 | QCBORError uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2402 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2403 | uErr = QCBORDecode_Private_GetNextMapOrArray(pMe, NULL, pDecodedItem, NULL); |
| 2404 | |
| 2405 | #ifndef QCBOR_DISABLE_TAGS |
| 2406 | uint64_t uTagNumber; |
| 2407 | int nTagIndex; |
| 2408 | const struct QCBORTagDecoderEntry *pTagDecoder; |
| 2409 | |
| 2410 | if(uErr != QCBOR_SUCCESS) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2411 | goto Done; |
| 2412 | } |
| 2413 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2414 | /* Loop over tag numbers in reverse, those closest to content first */ |
| 2415 | for(nTagIndex = QCBOR_MAX_TAGS_PER_ITEM-1; nTagIndex >= 0; nTagIndex--) { |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2416 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2417 | if(pDecodedItem->auTagNumbers[nTagIndex] == CBOR_TAG_INVALID16) { |
| 2418 | continue; /* Empty slot, skip to next */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2419 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2420 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2421 | /* See if there's a content decoder for it */ |
| 2422 | uTagNumber = QCBORDecode_Private_UnMapTagNumber(pMe, pDecodedItem->auTagNumbers[nTagIndex]); |
| 2423 | pTagDecoder = QCBORDecode_Private_LookUpTagDecoder(pMe->pTagDecoderTable, uTagNumber); |
| 2424 | if(pTagDecoder == NULL) { |
| 2425 | break; /* Successful exist -- a tag that we can't decode */ |
Laurence Lundblade | 830fbf9 | 2020-05-31 17:22:33 -0700 | [diff] [blame] | 2426 | } |
Laurence Lundblade | 9961530 | 2020-11-29 11:19:47 -0800 | [diff] [blame] | 2427 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2428 | /* Call the content decoder */ |
| 2429 | uErr = pTagDecoder->pfContentDecoder(pMe, pMe->pTagDecodersContext, pTagDecoder->uTagNumber, pDecodedItem); |
| 2430 | if(uErr != QCBOR_SUCCESS) { |
| 2431 | break; /* Error exit from the loop */ |
| 2432 | } |
| 2433 | |
| 2434 | /* Remove tag number from list since its content was decoded */ |
| 2435 | pDecodedItem->auTagNumbers[nTagIndex] = CBOR_TAG_INVALID16; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | Done: |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2439 | #endif /* ! QCBOR_DISABLE_TAGS */ |
| 2440 | |
| 2441 | return uErr; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2445 | /** |
| 2446 | * @brief Consume an entire map or array including its contents. |
| 2447 | * |
| 2448 | * @param[in] pMe The decoder context. |
| 2449 | * @param[in] pItemToConsume The array/map whose contents are to be |
| 2450 | * consumed. |
| 2451 | * @param[out] puNextNestLevel The next nesting level after the item was |
| 2452 | * fully consumed. |
| 2453 | * |
| 2454 | * This may be called when @c pItemToConsume is not an array or |
| 2455 | * map. In that case, this is just a pass through for @c puNextNestLevel |
| 2456 | * since there is nothing to do. |
| 2457 | */ |
| 2458 | static QCBORError |
| 2459 | QCBORDecode_Private_ConsumeItem(QCBORDecodeContext *pMe, |
| 2460 | const QCBORItem *pItemToConsume, |
| 2461 | bool *pbBreak, |
| 2462 | uint8_t *puNextNestLevel) |
| 2463 | { |
| 2464 | QCBORError uReturn; |
| 2465 | QCBORItem Item; |
| 2466 | |
| 2467 | /* If it is a map or array, this will tell if it is empty. */ |
| 2468 | const bool bIsEmpty = (pItemToConsume->uNextNestLevel <= pItemToConsume->uNestingLevel); |
| 2469 | |
| 2470 | if(QCBORItem_IsMapOrArray(*pItemToConsume) && !bIsEmpty) { |
| 2471 | /* There is only real work to do for non-empty maps and arrays */ |
| 2472 | |
| 2473 | /* This works for definite- and indefinite-length maps and |
| 2474 | * arrays by using the nesting level |
| 2475 | */ |
| 2476 | do { |
| 2477 | uReturn = QCBORDecode_Private_GetNextMapOrArray(pMe, pbBreak, &Item, NULL); |
| 2478 | if(QCBORDecode_IsUnrecoverableError(uReturn) || |
| 2479 | uReturn == QCBOR_ERR_NO_MORE_ITEMS) { |
| 2480 | goto Done; |
| 2481 | } |
| 2482 | } while(Item.uNextNestLevel >= pItemToConsume->uNextNestLevel); |
| 2483 | |
| 2484 | *puNextNestLevel = Item.uNextNestLevel; |
| 2485 | |
| 2486 | uReturn = QCBOR_SUCCESS; |
| 2487 | |
| 2488 | } else { |
| 2489 | /* pItemToConsume is not a map or array. Just pass the nesting |
| 2490 | * level through. */ |
| 2491 | *puNextNestLevel = pItemToConsume->uNextNestLevel; |
| 2492 | |
| 2493 | uReturn = QCBOR_SUCCESS; |
| 2494 | } |
| 2495 | |
| 2496 | Done: |
| 2497 | return uReturn; |
| 2498 | } |
| 2499 | |
| 2500 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2501 | #ifndef QCBOR_DISABLE_DECODE_CONFORMANCE |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2502 | /* |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2503 | * This consumes the next item. It returns the starting position of |
| 2504 | * the label and the length of the label. It also returns the nest |
| 2505 | * level of the item consumed. |
| 2506 | */ |
| 2507 | static QCBORError |
| 2508 | QCBORDecode_Private_GetLabelAndConsume(QCBORDecodeContext *pMe, |
| 2509 | uint8_t *puNestLevel, |
| 2510 | size_t *puLabelStart, |
| 2511 | size_t *puLabelLen) |
| 2512 | { |
| 2513 | QCBORError uErr; |
| 2514 | QCBORItem Item; |
| 2515 | uint8_t uLevel; |
| 2516 | uint32_t uLabelOffset; |
| 2517 | |
| 2518 | /* Get the label and consume it should it be complex */ |
| 2519 | *puLabelStart = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 2520 | |
| 2521 | uErr = QCBORDecode_Private_GetNextMapOrArray(pMe, NULL, &Item, &uLabelOffset); |
| 2522 | if(uErr != QCBOR_SUCCESS) { |
| 2523 | goto Done; |
| 2524 | } |
| 2525 | *puLabelLen = uLabelOffset - *puLabelStart; |
| 2526 | *puNestLevel = Item.uNestingLevel; |
| 2527 | uErr = QCBORDecode_Private_ConsumeItem(pMe, &Item, NULL, &uLevel); |
| 2528 | |
| 2529 | Done: |
| 2530 | return uErr; |
| 2531 | } |
| 2532 | |
| 2533 | |
| 2534 | /* Loop over items in a map until the end of the map looking for |
| 2535 | * duplicates. This starts at the current position in the map, not at |
| 2536 | * the beginning of the map. |
| 2537 | * |
| 2538 | * This saves and restores the traversal cursor and nest tracking so |
| 2539 | * they are the same on exit as they were on entry. |
| 2540 | */ |
| 2541 | static QCBORError |
| 2542 | QCBORDecode_Private_CheckDups(QCBORDecodeContext *pMe, |
| 2543 | const uint8_t uNestLevel, |
| 2544 | const size_t uCompareLabelStart, |
| 2545 | const size_t uCompareLabelLen) |
| 2546 | { |
| 2547 | QCBORError uErr; |
| 2548 | size_t uLabelStart; |
| 2549 | size_t uLabelLen; |
| 2550 | uint8_t uLevel; |
| 2551 | int nCompare; |
| 2552 | |
| 2553 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2554 | const UsefulInputBuf Save = pMe->InBuf; |
| 2555 | |
| 2556 | do { |
| 2557 | uErr = QCBORDecode_Private_GetLabelAndConsume(pMe, &uLevel, &uLabelStart, &uLabelLen); |
| 2558 | if(uErr != QCBOR_SUCCESS) { |
| 2559 | if(uErr == QCBOR_ERR_NO_MORE_ITEMS) { |
| 2560 | uErr = QCBOR_SUCCESS; /* Successful end */ |
| 2561 | } |
| 2562 | break; |
| 2563 | } |
| 2564 | |
| 2565 | if(uLevel != uNestLevel) { |
| 2566 | break; /* Successful end of loop */ |
| 2567 | } |
| 2568 | |
| 2569 | /* This check for dups works for labels that are preferred |
| 2570 | * serialization and are not maps. If the labels are not in |
| 2571 | * preferred serialization, then the check has to be more |
| 2572 | * complicated and is type-specific because it uses the decoded |
| 2573 | * value, not the encoded CBOR. It is further complicated for |
| 2574 | * maps because the order of items in a map that is a label |
| 2575 | * doesn't matter when checking that is is the duplicate of |
| 2576 | * another map that is a label. QCBOR so far only turns on this |
| 2577 | * dup checking as part of CDE checking which requires preferred |
| 2578 | * serialization. See 5.6 in RFC 8949. |
| 2579 | */ |
| 2580 | nCompare = UsefulInputBuf_Compare(&(pMe->InBuf), |
| 2581 | uCompareLabelStart, uCompareLabelLen, |
| 2582 | uLabelStart, uLabelLen); |
| 2583 | if(nCompare == 0) { |
| 2584 | uErr = QCBOR_ERR_DUPLICATE_LABEL; |
| 2585 | break; |
| 2586 | } |
| 2587 | } while (1); |
| 2588 | |
| 2589 | pMe->nesting = SaveNesting; |
| 2590 | pMe->InBuf = Save; |
| 2591 | |
| 2592 | return uErr; |
| 2593 | } |
| 2594 | |
| 2595 | |
| 2596 | /* This does sort order and duplicate detection on a map. The and all |
| 2597 | * it's members must be in preferred serialization so the comparisons |
| 2598 | * work correctly. |
| 2599 | */ |
| 2600 | static QCBORError |
| 2601 | QCBORDecode_Private_CheckMap(QCBORDecodeContext *pMe, const QCBORItem *pMapToCheck) |
| 2602 | { |
| 2603 | QCBORError uErr; |
| 2604 | uint8_t uNestLevel; |
| 2605 | size_t offset2, offset1, length2, length1; |
| 2606 | |
| 2607 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2608 | const UsefulInputBuf Save = pMe->InBuf; |
| 2609 | pMe->bAllowAllLabels = 1; |
| 2610 | |
| 2611 | /* This loop runs over all the items in the map once, comparing |
| 2612 | * each adjacent pair for correct ordering. It also calls CheckDup |
| 2613 | * on each one which also runs over the remaining items in the map |
| 2614 | * checking for duplicates. So duplicate checking runs in n^2. |
| 2615 | */ |
| 2616 | |
| 2617 | offset2 = SIZE_MAX; |
| 2618 | length2 = SIZE_MAX; // To avoid uninitialized warning |
| 2619 | while(1) { |
| 2620 | uErr = QCBORDecode_Private_GetLabelAndConsume(pMe, &uNestLevel, &offset1, &length1); |
| 2621 | if(uErr != QCBOR_SUCCESS) { |
| 2622 | if(uErr == QCBOR_ERR_NO_MORE_ITEMS) { |
| 2623 | uErr = QCBOR_SUCCESS; /* Successful exit from loop */ |
| 2624 | } |
| 2625 | break; |
| 2626 | } |
| 2627 | |
| 2628 | if(uNestLevel < pMapToCheck->uNextNestLevel) { |
| 2629 | break; /* Successful exit from loop */ |
| 2630 | } |
| 2631 | |
| 2632 | if(offset2 != SIZE_MAX) { |
| 2633 | /* Check that the labels are ordered. Check is not done the |
| 2634 | * first time through the loop when offset2 is unset. Since |
| 2635 | * this does comparison of the items in encoded form they |
| 2636 | * must be preferred serialization encoded. See RFC 8949 |
| 2637 | * 4.2.1. |
| 2638 | */ |
| 2639 | if(UsefulInputBuf_Compare(&(pMe->InBuf), offset2, length2, offset1, length1) > 0) { |
| 2640 | uErr = QCBOR_ERR_UNSORTED; |
| 2641 | break; |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | uErr = QCBORDecode_Private_CheckDups(pMe, pMapToCheck->uNextNestLevel, offset1, length1); |
| 2646 | if(uErr != QCBOR_SUCCESS) { |
| 2647 | break; |
| 2648 | } |
| 2649 | |
| 2650 | offset2 = offset1; |
| 2651 | length2 = length1; |
| 2652 | } |
| 2653 | |
| 2654 | pMe->bAllowAllLabels = 0; |
| 2655 | pMe->nesting = SaveNesting; |
| 2656 | pMe->InBuf = Save; |
| 2657 | |
| 2658 | return uErr; |
| 2659 | } |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2660 | #endif /* ! QCBOR_DISABLE_DECODE_CONFORMANCE */ |
| 2661 | |
| 2662 | static QCBORError |
| 2663 | QCBORDecode_Private_GetItemChecks(QCBORDecodeContext *pMe, |
| 2664 | QCBORError uErr, |
| 2665 | const size_t uOffset, |
| 2666 | QCBORItem *pDecodedItem) |
| 2667 | { |
| 2668 | (void)pMe; /* Avoid warning for next two ifdefs */ |
| 2669 | (void)uOffset; |
| 2670 | |
| 2671 | #ifndef QCBOR_DISABLE_DECODE_CONFORMANCE |
| 2672 | if(uErr == QCBOR_SUCCESS && |
| 2673 | (pMe->uDecodeMode & QCBOR_DECODE_MODE_MASK) >= QCBOR_ENCODE_MODE_CDE && |
| 2674 | pDecodedItem->uDataType == QCBOR_TYPE_MAP) { |
| 2675 | /* Traverse map checking sort order and for duplicates */ |
| 2676 | uErr = QCBORDecode_Private_CheckMap(pMe, pDecodedItem); |
| 2677 | } |
| 2678 | #endif /* ! QCBOR_DISABLE_CONFORMANCE */ |
| 2679 | |
| 2680 | #ifndef QCBOR_DISABLE_TAGS |
| 2681 | if(uErr == QCBOR_SUCCESS && |
| 2682 | !(pMe->uDecodeMode & QCBOR_DECODE_UNPROCESSED_TAG_NUMBERS) && |
| 2683 | pDecodedItem->auTagNumbers[0] != CBOR_TAG_INVALID16) { |
| 2684 | /* Not QCBOR v1; there are tag numbers -- check they were consumed */ |
| 2685 | if(uOffset != pMe->uTagNumberCheckOffset || pMe->uTagNumberIndex != 255) { |
| 2686 | uErr = QCBOR_ERR_UNPROCESSED_TAG_NUMBER; |
| 2687 | } |
| 2688 | } |
| 2689 | #endif /* ! QCBOR_DISABLE_TAGS */ |
| 2690 | |
| 2691 | if(uErr != QCBOR_SUCCESS) { |
| 2692 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2693 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
| 2694 | } |
| 2695 | |
| 2696 | return uErr; |
| 2697 | } |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2698 | |
| 2699 | |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2700 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2701 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2702 | */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2703 | QCBORError |
| 2704 | QCBORDecode_GetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2705 | { |
| 2706 | QCBORError uErr; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2707 | size_t uOffset; |
| 2708 | |
| 2709 | uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 2710 | uErr = QCBORDecode_Private_GetNextTagContent(pMe, pDecodedItem); |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2711 | uErr = QCBORDecode_Private_GetItemChecks(pMe, uErr, uOffset, pDecodedItem); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 2712 | return uErr; |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | |
| 2716 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2717 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 59289e5 | 2019-12-30 13:44:37 -0800 | [diff] [blame] | 2718 | */ |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2719 | QCBORError |
| 2720 | QCBORDecode_PeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2721 | { |
| 2722 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2723 | const UsefulInputBuf Save = pMe->InBuf; |
| 2724 | |
| 2725 | QCBORError uErr = QCBORDecode_GetNext(pMe, pDecodedItem); |
| 2726 | |
| 2727 | pMe->nesting = SaveNesting; |
| 2728 | pMe->InBuf = Save; |
| 2729 | |
| 2730 | return uErr; |
| 2731 | } |
| 2732 | |
| 2733 | |
| 2734 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2735 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 2a26abb | 2020-11-05 19:06:54 -0800 | [diff] [blame] | 2736 | */ |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2737 | void |
| 2738 | QCBORDecode_VPeekNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
| 2739 | { |
| 2740 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 2741 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2742 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2743 | return; |
| 2744 | } |
| 2745 | |
| 2746 | pMe->uLastError = (uint8_t)QCBORDecode_PeekNext(pMe, pDecodedItem); |
| 2747 | } |
| 2748 | |
| 2749 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2750 | static void |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2751 | QCBORDecode_Private_SaveTagNumbers(QCBORDecodeContext *pMe, const QCBORItem *pItem) |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2752 | { |
| 2753 | #ifndef QCBOR_DISABLE_TAGS |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2754 | memcpy(pMe->auLastTags, pItem->auTagNumbers, sizeof(pItem->auTagNumbers)); |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 2755 | #else |
| 2756 | (void)pMe; |
| 2757 | (void)pItem; |
| 2758 | #endif |
| 2759 | } |
| 2760 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 2761 | /* |
| 2762 | * Public function, see header qcbor/qcbor_decode.h file |
| 2763 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2764 | void |
| 2765 | QCBORDecode_VGetNext(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2766 | { |
| 2767 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | f00b8be | 2024-03-08 10:34:33 -0800 | [diff] [blame] | 2768 | pDecodedItem->uDataType = QCBOR_TYPE_NONE; |
| 2769 | pDecodedItem->uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 6f3f78e | 2020-08-31 13:09:14 -0700 | [diff] [blame] | 2770 | return; |
| 2771 | } |
| 2772 | |
| 2773 | pMe->uLastError = (uint8_t)QCBORDecode_GetNext(pMe, pDecodedItem); |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2774 | QCBORDecode_Private_SaveTagNumbers(pMe, pDecodedItem); |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2775 | } |
| 2776 | |
| 2777 | |
| 2778 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2779 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | dbe6f21 | 2018-10-28 11:37:53 +0700 | [diff] [blame] | 2780 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2781 | QCBORError |
| 2782 | QCBORDecode_PartialFinish(QCBORDecodeContext *pMe, size_t *puConsumed) |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2783 | { |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2784 | if(puConsumed != NULL) { |
| 2785 | *puConsumed = pMe->InBuf.cursor; |
| 2786 | } |
| 2787 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2788 | QCBORError uReturn = pMe->uLastError; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2789 | |
| 2790 | if(uReturn != QCBOR_SUCCESS) { |
| 2791 | goto Done; |
| 2792 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2793 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2794 | /* Error out if all the maps/arrays are not closed out */ |
| 2795 | if(!DecodeNesting_IsCurrentAtTop(&(pMe->nesting))) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 2796 | uReturn = QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2797 | goto Done; |
| 2798 | } |
| 2799 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2800 | /* Error out if not all the bytes are consumed */ |
| 2801 | if(UsefulInputBuf_BytesUnconsumed(&(pMe->InBuf))) { |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 2802 | uReturn = QCBOR_ERR_EXTRA_BYTES; |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2803 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2804 | |
Laurence Lundblade | 20b533d | 2018-10-08 20:44:53 +0800 | [diff] [blame] | 2805 | Done: |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2806 | return uReturn; |
| 2807 | } |
| 2808 | |
| 2809 | |
| 2810 | /* |
| 2811 | * Public function, see header qcbor/qcbor_decode.h file |
| 2812 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2813 | QCBORError |
| 2814 | QCBORDecode_Finish(QCBORDecodeContext *pMe) |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2815 | { |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2816 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2817 | /* Call the destructor for the string allocator if there is one. |
| 2818 | * Always called, even if there are errors; always have to clean up. |
| 2819 | */ |
| 2820 | StringAllocator_Destruct(&(pMe->StringAllocator)); |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2821 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2822 | |
Laurence Lundblade | 8749573 | 2021-02-26 10:05:55 -0700 | [diff] [blame] | 2823 | return QCBORDecode_PartialFinish(pMe, NULL); |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2827 | #ifndef QCBOR_DISABLE_TAGS |
| 2828 | /* |
| 2829 | * Public function, see header qcbor/qcbor_decode.h file |
| 2830 | */ |
| 2831 | uint64_t |
| 2832 | QCBORDecode_GetNthTagNumber(const QCBORDecodeContext *pMe, |
| 2833 | const QCBORItem *pItem, |
| 2834 | uint8_t uIndex) |
| 2835 | { |
| 2836 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2837 | return CBOR_TAG_INVALID64; |
| 2838 | } |
| 2839 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2840 | return CBOR_TAG_INVALID64; |
| 2841 | } |
| 2842 | |
| 2843 | return QCBORDecode_Private_UnMapTagNumber(pMe, pItem->auTagNumbers[uIndex]); |
| 2844 | } |
| 2845 | |
| 2846 | |
| 2847 | /* |
| 2848 | * Public function, see header qcbor/qcbor_decode.h file |
| 2849 | */ |
| 2850 | uint64_t |
| 2851 | QCBORDecode_GetNthTagNumberOfLast(QCBORDecodeContext *pMe, |
| 2852 | uint8_t uIndex) |
| 2853 | { |
| 2854 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2855 | return CBOR_TAG_INVALID64; |
| 2856 | } |
| 2857 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2858 | return CBOR_TAG_INVALID64; |
| 2859 | } |
| 2860 | |
| 2861 | return QCBORDecode_Private_UnMapTagNumber(pMe, pMe->auLastTags[uIndex]); |
| 2862 | } |
| 2863 | |
| 2864 | |
| 2865 | /* |
| 2866 | * Public function, see header qcbor/qcbor_decode.h file |
| 2867 | */ |
| 2868 | static uint64_t |
| 2869 | QCBORDecode_Private_GetNthTagNumberReverse(const QCBORDecodeContext *pMe, |
| 2870 | const uint16_t puTagNumbers[], |
| 2871 | const uint32_t uIndex) |
| 2872 | { |
| 2873 | uint32_t uArrayIndex; |
| 2874 | |
| 2875 | /* Find number of tag numbers */ |
| 2876 | for(uArrayIndex = QCBOR_MAX_TAGS_PER_ITEM-1; uArrayIndex > 0; uArrayIndex--) { |
| 2877 | if(puTagNumbers[uArrayIndex] != CBOR_TAG_INVALID16) { |
| 2878 | break; |
| 2879 | } |
| 2880 | } |
| 2881 | if(uIndex > uArrayIndex) { |
| 2882 | return CBOR_TAG_INVALID64; |
| 2883 | } |
| 2884 | |
| 2885 | return QCBORDecode_Private_UnMapTagNumber(pMe, puTagNumbers[uArrayIndex - uIndex]); |
| 2886 | } |
| 2887 | |
| 2888 | |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2889 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2890 | * Public function, see header qcbor/qcbor_decode.h file |
| 2891 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2892 | uint64_t |
| 2893 | QCBORDecode_GetNthTag(QCBORDecodeContext *pMe, |
| 2894 | const QCBORItem *pItem, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2895 | const uint32_t uIndex) |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2896 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2897 | if(pItem->uDataType == QCBOR_TYPE_NONE) { |
| 2898 | return CBOR_TAG_INVALID64; |
| 2899 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2900 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2901 | return QCBORDecode_Private_GetNthTagNumberReverse(pMe, pItem->auTagNumbers, uIndex); |
Laurence Lundblade | c75e68b | 2020-06-15 20:34:46 -0700 | [diff] [blame] | 2902 | } |
| 2903 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2904 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2905 | /* |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2906 | * Public function, see header qcbor/qcbor_decode.h file |
| 2907 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 2908 | uint64_t |
| 2909 | QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pMe, |
| 2910 | uint32_t uIndex) |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2911 | { |
Laurence Lundblade | 88e9db2 | 2020-11-02 03:56:33 -0800 | [diff] [blame] | 2912 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 2913 | return CBOR_TAG_INVALID64; |
| 2914 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2915 | if(uIndex >= QCBOR_MAX_TAGS_PER_ITEM) { |
| 2916 | return CBOR_TAG_INVALID64; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2917 | } |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 2918 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2919 | return QCBORDecode_Private_GetNthTagNumberReverse(pMe, pMe->auLastTags, uIndex); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 2920 | } |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2921 | |
Laurence Lundblade | b69cad7 | 2018-09-13 11:09:01 -0700 | [diff] [blame] | 2922 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2923 | /* |
| 2924 | * Public function, see header qcbor/qcbor_decode.h file |
| 2925 | */ |
| 2926 | QCBORError |
| 2927 | QCBORDecode_GetNextTagNumber(QCBORDecodeContext *pMe, uint64_t *puTagNumber) |
| 2928 | { |
| 2929 | QCBORItem Item; |
| 2930 | size_t uOffset; |
| 2931 | QCBORError uErr; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2932 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 2933 | const QCBORDecodeNesting SaveNesting = pMe->nesting; |
| 2934 | const UsefulInputBuf Save = pMe->InBuf; |
| 2935 | |
| 2936 | uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 2937 | if(uOffset == pMe->uTagNumberCheckOffset) { |
| 2938 | pMe->uTagNumberIndex++; |
| 2939 | } else { |
| 2940 | pMe->uTagNumberIndex = 0; |
| 2941 | } |
| 2942 | |
| 2943 | *puTagNumber = CBOR_TAG_INVALID64; |
| 2944 | uErr = QCBORDecode_Private_GetNextTagContent(pMe, &Item); |
| 2945 | if(uErr) { |
| 2946 | return uErr; |
| 2947 | } |
| 2948 | |
| 2949 | *puTagNumber = QCBORDecode_GetNthTagNumber(pMe, &Item, pMe->uTagNumberIndex); |
| 2950 | if(*puTagNumber == CBOR_TAG_INVALID64 || |
| 2951 | QCBORDecode_GetNthTagNumber(pMe, &Item, pMe->uTagNumberIndex+1) == CBOR_TAG_INVALID64 ) { |
| 2952 | pMe->uTagNumberIndex = QCBOR_ALL_TAGS_PROCESSED; |
| 2953 | } |
| 2954 | pMe->uTagNumberCheckOffset = uOffset; |
| 2955 | |
| 2956 | pMe->nesting = SaveNesting; |
| 2957 | pMe->InBuf = Save; |
| 2958 | |
| 2959 | return QCBOR_SUCCESS; |
| 2960 | } |
| 2961 | |
| 2962 | |
| 2963 | /* |
| 2964 | * Public function, see header qcbor/qcbor_decode.h file |
| 2965 | */ |
| 2966 | void |
| 2967 | QCBORDecode_VGetNextTagNumber(QCBORDecodeContext *pMe, uint64_t *puTagNumber) |
| 2968 | { |
| 2969 | pMe->uLastError = (uint8_t)QCBORDecode_GetNextTagNumber(pMe, puTagNumber); |
| 2970 | } |
| 2971 | |
| 2972 | #endif /* ! QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 2973 | |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 2974 | #ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2975 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2976 | /* =========================================================================== |
| 2977 | MemPool -- BUILT-IN SIMPLE STRING ALLOCATOR |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 2978 | |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 2979 | This implements a simple sting allocator for indefinite-length |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2980 | strings that can be enabled by calling QCBORDecode_SetMemPool(). It |
| 2981 | implements the function type QCBORStringAllocate and allows easy |
| 2982 | use of it. |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 2983 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 2984 | This particular allocator is built-in for convenience. The caller |
| 2985 | can implement their own. All of this following code will get |
| 2986 | dead-stripped if QCBORDecode_SetMemPool() is not called. |
| 2987 | |
| 2988 | This is a very primitive memory allocator. It does not track |
| 2989 | individual allocations, only a high-water mark. A free or |
| 2990 | reallocation must be of the last chunk allocated. |
| 2991 | |
| 2992 | The size of the pool and offset to free memory are packed into the |
| 2993 | first 8 bytes of the memory pool so we don't have to keep them in |
| 2994 | the decode context. Since the address of the pool may not be |
| 2995 | aligned, they have to be packed and unpacked as if they were |
| 2996 | serialized data of the wire or such. |
| 2997 | |
| 2998 | The sizes packed in are uint32_t to be the same on all CPU types |
| 2999 | and simplify the code. |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3000 | ========================================================================== */ |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3001 | |
| 3002 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3003 | static int |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3004 | MemPool_Unpack(const void *pMem, uint32_t *puPoolSize, uint32_t *puFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3005 | { |
| 3006 | // Use of UsefulInputBuf is overkill, but it is convenient. |
| 3007 | UsefulInputBuf UIB; |
| 3008 | |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3009 | // Just assume the size here. It was checked during SetUp so |
| 3010 | // the assumption is safe. |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 3011 | UsefulInputBuf_Init(&UIB, (UsefulBufC){pMem,QCBOR_DECODE_MIN_MEM_POOL_SIZE}); |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3012 | *puPoolSize = UsefulInputBuf_GetUint32(&UIB); |
| 3013 | *puFreeOffset = UsefulInputBuf_GetUint32(&UIB); |
| 3014 | return UsefulInputBuf_GetError(&UIB); |
| 3015 | } |
| 3016 | |
| 3017 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3018 | static int |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3019 | MemPool_Pack(UsefulBuf Pool, uint32_t uFreeOffset) |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3020 | { |
| 3021 | // Use of UsefulOutBuf is overkill, but convenient. The |
| 3022 | // length check performed here is useful. |
| 3023 | UsefulOutBuf UOB; |
| 3024 | |
| 3025 | UsefulOutBuf_Init(&UOB, Pool); |
| 3026 | UsefulOutBuf_AppendUint32(&UOB, (uint32_t)Pool.len); // size of pool |
| 3027 | UsefulOutBuf_AppendUint32(&UOB, uFreeOffset); // first free position |
| 3028 | return UsefulOutBuf_GetError(&UOB); |
| 3029 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3030 | |
| 3031 | |
| 3032 | /* |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3033 | Internal function for an allocation, reallocation free and destuct. |
| 3034 | |
| 3035 | Having only one function rather than one each per mode saves space in |
| 3036 | QCBORDecodeContext. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3037 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3038 | Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 3039 | */ |
Laurence Lundblade | ee85174 | 2020-01-08 08:37:05 -0800 | [diff] [blame] | 3040 | static UsefulBuf |
| 3041 | MemPool_Function(void *pPool, void *pMem, size_t uNewSize) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3042 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3043 | UsefulBuf ReturnValue = NULLUsefulBuf; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3044 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3045 | uint32_t uPoolSize; |
| 3046 | uint32_t uFreeOffset; |
| 3047 | |
| 3048 | if(uNewSize > UINT32_MAX) { |
| 3049 | // This allocator is only good up to 4GB. This check should |
| 3050 | // optimize out if sizeof(size_t) == sizeof(uint32_t) |
| 3051 | goto Done; |
| 3052 | } |
| 3053 | const uint32_t uNewSize32 = (uint32_t)uNewSize; |
| 3054 | |
| 3055 | if(MemPool_Unpack(pPool, &uPoolSize, &uFreeOffset)) { |
| 3056 | goto Done; |
| 3057 | } |
| 3058 | |
| 3059 | if(uNewSize) { |
| 3060 | if(pMem) { |
| 3061 | // REALLOCATION MODE |
| 3062 | // Calculate pointer to the end of the memory pool. It is |
| 3063 | // assumed that pPool + uPoolSize won't wrap around by |
| 3064 | // assuming the caller won't pass a pool buffer in that is |
| 3065 | // not in legitimate memory space. |
| 3066 | const void *pPoolEnd = (uint8_t *)pPool + uPoolSize; |
| 3067 | |
| 3068 | // Check that the pointer for reallocation is in the range of the |
| 3069 | // pool. This also makes sure that pointer math further down |
| 3070 | // doesn't wrap under or over. |
| 3071 | if(pMem >= pPool && pMem < pPoolEnd) { |
| 3072 | // Offset to start of chunk for reallocation. This won't |
| 3073 | // wrap under because of check that pMem >= pPool. Cast |
| 3074 | // is safe because the pool is always less than UINT32_MAX |
| 3075 | // because of check in QCBORDecode_SetMemPool(). |
| 3076 | const uint32_t uMemOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 3077 | |
| 3078 | // Check to see if the allocation will fit. uPoolSize - |
| 3079 | // uMemOffset will not wrap under because of check that |
| 3080 | // pMem is in the range of the uPoolSize by check above. |
| 3081 | if(uNewSize <= uPoolSize - uMemOffset) { |
| 3082 | ReturnValue.ptr = pMem; |
| 3083 | ReturnValue.len = uNewSize; |
| 3084 | |
| 3085 | // Addition won't wrap around over because uNewSize was |
| 3086 | // checked to be sure it is less than the pool size. |
| 3087 | uFreeOffset = uMemOffset + uNewSize32; |
| 3088 | } |
| 3089 | } |
| 3090 | } else { |
| 3091 | // ALLOCATION MODE |
| 3092 | // uPoolSize - uFreeOffset will not underflow because this |
| 3093 | // pool implementation makes sure uFreeOffset is always |
| 3094 | // smaller than uPoolSize through this check here and |
| 3095 | // reallocation case. |
| 3096 | if(uNewSize <= uPoolSize - uFreeOffset) { |
| 3097 | ReturnValue.len = uNewSize; |
| 3098 | ReturnValue.ptr = (uint8_t *)pPool + uFreeOffset; |
Laurence Lundblade | 06350ea | 2020-01-27 19:32:40 -0800 | [diff] [blame] | 3099 | uFreeOffset += (uint32_t)uNewSize; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3100 | } |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3101 | } |
| 3102 | } else { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3103 | if(pMem) { |
| 3104 | // FREE MODE |
| 3105 | // Cast is safe because of limit on pool size in |
| 3106 | // QCBORDecode_SetMemPool() |
| 3107 | uFreeOffset = (uint32_t)((uint8_t *)pMem - (uint8_t *)pPool); |
| 3108 | } else { |
| 3109 | // DESTRUCT MODE |
| 3110 | // Nothing to do for this allocator |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3111 | } |
| 3112 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3113 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3114 | UsefulBuf Pool = {pPool, uPoolSize}; |
| 3115 | MemPool_Pack(Pool, uFreeOffset); |
| 3116 | |
| 3117 | Done: |
| 3118 | return ReturnValue; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3119 | } |
| 3120 | |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3121 | |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3122 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3123 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f653166 | 2018-12-04 10:42:22 +0900 | [diff] [blame] | 3124 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3125 | QCBORError |
| 3126 | QCBORDecode_SetMemPool(QCBORDecodeContext *pMe, |
| 3127 | UsefulBuf Pool, |
| 3128 | bool bAllStrings) |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3129 | { |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3130 | // The pool size and free mem offset are packed into the beginning |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3131 | // of the pool memory. This compile time check makes sure the |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3132 | // constant in the header is correct. This check should optimize |
| 3133 | // down to nothing. |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3134 | #ifdef _MSC_VER |
| 3135 | #pragma warning(push) |
| 3136 | #pragma warning(disable:4127) // conditional expression is constant |
| 3137 | #endif |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3138 | if(QCBOR_DECODE_MIN_MEM_POOL_SIZE < 2 * sizeof(uint32_t)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3139 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3140 | } |
Dave Thaler | 93c0118 | 2022-08-06 15:08:35 -0400 | [diff] [blame] | 3141 | #ifdef _MSC_VER |
| 3142 | #pragma warning(pop) |
| 3143 | #endif |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3144 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3145 | // The pool size and free offset packed in to the beginning of pool |
| 3146 | // memory are only 32-bits. This check will optimize out on 32-bit |
| 3147 | // machines. |
| 3148 | if(Pool.len > UINT32_MAX) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3149 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3150 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3151 | |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3152 | // This checks that the pool buffer given is big enough. |
| 3153 | if(MemPool_Pack(Pool, QCBOR_DECODE_MIN_MEM_POOL_SIZE)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3154 | return QCBOR_ERR_MEM_POOL_SIZE; |
Laurence Lundblade | 1d7eb63 | 2019-02-17 17:23:38 -0800 | [diff] [blame] | 3155 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3156 | |
Laurence Lundblade | 3427dee | 2021-06-20 11:11:24 -0700 | [diff] [blame] | 3157 | QCBORDecode_SetUpAllocator(pMe, MemPool_Function, Pool.ptr, bAllStrings); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 3158 | |
Laurence Lundblade | 30816f2 | 2018-11-10 13:40:22 +0700 | [diff] [blame] | 3159 | return QCBOR_SUCCESS; |
Laurence Lundblade | 041ffa5 | 2018-10-07 11:43:51 +0700 | [diff] [blame] | 3160 | } |
Laurence Lundblade | f6da33c | 2020-11-26 18:15:05 -0800 | [diff] [blame] | 3161 | #endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3162 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 3163 | |
| 3164 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3165 | /* |
| 3166 | * Public function, see header qcbor/qcbor_decode.h file |
| 3167 | */ |
| 3168 | void |
| 3169 | QCBORDecode_VGetNextConsume(QCBORDecodeContext *pMe, QCBORItem *pDecodedItem) |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3170 | { |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3171 | QCBORDecode_VGetNext(pMe, pDecodedItem); |
| 3172 | |
| 3173 | if(pMe->uLastError == QCBOR_SUCCESS) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3174 | pMe->uLastError = (uint8_t)QCBORDecode_Private_ConsumeItem(pMe, pDecodedItem, NULL, |
Máté Tóth-Pál | c6d5968 | 2021-05-26 18:55:30 +0200 | [diff] [blame] | 3175 | &pDecodedItem->uNextNestLevel); |
Laurence Lundblade | 732e52d | 2021-02-22 20:11:01 -0700 | [diff] [blame] | 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 3180 | /* |
| 3181 | * Public function, see header qcbor/qcbor_decode.h file |
| 3182 | */ |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 3183 | QCBORError |
| 3184 | QCBORDecode_EndCheck(QCBORDecodeContext *pMe) |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 3185 | { |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 3186 | size_t uCursorOffset; |
| 3187 | QCBORError uErr; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 3188 | |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 3189 | uErr = QCBORDecode_GetError(pMe); |
| 3190 | if(uErr != QCBOR_SUCCESS) { |
| 3191 | return uErr; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | uCursorOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3195 | |
| 3196 | if(uCursorOffset == UsefulInputBuf_GetBufferLength(&(pMe->InBuf))) { |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 3197 | return QCBOR_ERR_NO_MORE_ITEMS; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 3198 | } |
Laurence Lundblade | 535bb5e | 2024-06-16 12:06:32 -0700 | [diff] [blame] | 3199 | |
| 3200 | return QCBOR_SUCCESS; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3204 | /** |
| 3205 | * @brief Rewind cursor to start as if map or array were just entered. |
| 3206 | * |
| 3207 | * @param[in] pMe The decoding context |
| 3208 | * |
| 3209 | * This affects the nesting tracking and the UsefulInputBuf. |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3210 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3211 | static void |
| 3212 | QCBORDecode_Private_RewindMapOrArray(QCBORDecodeContext *pMe) |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3213 | { |
| 3214 | /* Reset nesting tracking to the deepest bounded level */ |
| 3215 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 3216 | |
| 3217 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3218 | |
| 3219 | /* Reposition traversal cursor to the start of the map/array */ |
| 3220 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 3221 | DecodeNesting_GetMapOrArrayStart(&(pMe->nesting))); |
| 3222 | } |
| 3223 | |
| 3224 | |
| 3225 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3226 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3227 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3228 | void |
| 3229 | QCBORDecode_Rewind(QCBORDecodeContext *pMe) |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3230 | { |
| 3231 | if(pMe->nesting.pCurrentBounded != NULL) { |
| 3232 | /* In a bounded map, array or bstr-wrapped CBOR */ |
| 3233 | |
| 3234 | if(DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
| 3235 | /* In bstr-wrapped CBOR. */ |
| 3236 | |
| 3237 | /* Reposition traversal cursor to start of wrapping byte string */ |
| 3238 | UsefulInputBuf_Seek(&(pMe->InBuf), |
| 3239 | pMe->nesting.pCurrentBounded->u.bs.uBstrStartOffset); |
| 3240 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
| 3241 | |
| 3242 | } else { |
| 3243 | /* In a map or array */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3244 | QCBORDecode_Private_RewindMapOrArray(pMe); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | } else { |
| 3248 | /* Not in anything bounded */ |
| 3249 | |
| 3250 | /* Reposition traversal cursor to the start of input CBOR */ |
| 3251 | UsefulInputBuf_Seek(&(pMe->InBuf), 0ULL); |
| 3252 | |
| 3253 | /* Reset nesting tracking to beginning of input. */ |
| 3254 | DecodeNesting_Init(&(pMe->nesting)); |
| 3255 | } |
| 3256 | |
| 3257 | pMe->uLastError = QCBOR_SUCCESS; |
| 3258 | } |
| 3259 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3260 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3261 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3262 | |
| 3263 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3264 | typedef struct { |
| 3265 | void *pCBContext; |
| 3266 | QCBORItemCallback pfCallback; |
| 3267 | } MapSearchCallBack; |
| 3268 | |
| 3269 | typedef struct { |
| 3270 | size_t uStartOffset; |
| 3271 | uint16_t uItemCount; |
| 3272 | } MapSearchInfo; |
| 3273 | |
| 3274 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3275 | /** |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3276 | * @brief Search a map for a set of items. |
| 3277 | * |
| 3278 | * @param[in] pMe The decode context to search. |
| 3279 | * @param[in,out] pItemArray The items to search for and the items found. |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3280 | * @param[out] pInfo Several bits of meta-info returned by search. |
| 3281 | * @param[in] pCallBack Callback object or @c NULL. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3282 | * |
| 3283 | * @retval QCBOR_ERR_NOT_ENTERED Trying to search without entering a map. |
| 3284 | * |
| 3285 | * @retval QCBOR_ERR_DUPLICATE_LABEL Duplicate items (items with the same label) |
| 3286 | * were found for one of the labels being |
| 3287 | * search for. This duplicate detection is |
| 3288 | * only performed for items in pItemArray, |
| 3289 | * not every item in the map. |
| 3290 | * |
| 3291 | * @retval QCBOR_ERR_UNEXPECTED_TYPE A label was matched, but the type was |
| 3292 | * wrong for the matchd label. |
| 3293 | * |
| 3294 | * @retval Also errors returned by QCBORDecode_GetNext(). |
| 3295 | * |
Laurence Lundblade | 88ba566 | 2024-11-03 02:10:24 -0800 | [diff] [blame] | 3296 | * On input, @c pItemArray contains a list of labels and data types of |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3297 | * items to be found. |
| 3298 | * |
| 3299 | * On output, the fully retrieved items are filled in with values and |
| 3300 | * such. The label was matched, so it never changes. |
| 3301 | * |
| 3302 | * If an item was not found, its data type is set to @ref QCBOR_TYPE_NONE. |
| 3303 | * |
| 3304 | * 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] | 3305 | */ |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 3306 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3307 | QCBORDecode_Private_MapSearch(QCBORDecodeContext *pMe, |
| 3308 | QCBORItem *pItemArray, |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3309 | MapSearchInfo *pInfo, |
| 3310 | MapSearchCallBack *pCallBack) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3311 | { |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 3312 | QCBORError uReturn; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3313 | uint64_t uFoundItemBitMap = 0; |
Laurence Lundblade | fb492ea | 2020-05-02 11:14:07 -0700 | [diff] [blame] | 3314 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3315 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3316 | uReturn = pMe->uLastError; |
| 3317 | goto Done2; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3318 | } |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3319 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3320 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_MAP) && |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3321 | pItemArray->uLabelType != QCBOR_TYPE_NONE) { |
| 3322 | /* QCBOR_TYPE_NONE as first item indicates just looking |
| 3323 | for the end of an array, so don't give error. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3324 | uReturn = QCBOR_ERR_MAP_NOT_ENTERED; |
| 3325 | goto Done2; |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 3326 | } |
| 3327 | |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3328 | if(DecodeNesting_IsBoundedEmpty(&(pMe->nesting))) { |
| 3329 | // It is an empty bounded array or map |
| 3330 | if(pItemArray->uLabelType == QCBOR_TYPE_NONE) { |
| 3331 | // Just trying to find the end of the map or array |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3332 | pMe->uMapEndOffsetCache = DecodeNesting_GetMapOrArrayStart(&(pMe->nesting)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3333 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3334 | } else { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 3335 | // Nothing is ever found in an empty array or map. All items |
| 3336 | // are marked as not found below. |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3337 | uReturn = QCBOR_SUCCESS; |
| 3338 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3339 | goto Done2; |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3340 | } |
| 3341 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3342 | QCBORDecodeNesting SaveNesting; |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 3343 | size_t uSavePos = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 3344 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 3345 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3346 | /* Reposition to search from the start of the map / array */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3347 | QCBORDecode_Private_RewindMapOrArray(pMe); |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3348 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3349 | /* |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3350 | Loop over all the items in the map or array. Each item |
| 3351 | could be a map or array, but label matching is only at |
Laurence Lundblade | 3f1318a | 2021-01-04 18:26:44 -0800 | [diff] [blame] | 3352 | the main level. This handles definite- and indefinite- |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3353 | length maps and arrays. The only reason this is ever |
| 3354 | called on arrays is to find their end position. |
| 3355 | |
| 3356 | This will always run over all items in order to do |
| 3357 | duplicate detection. |
| 3358 | |
| 3359 | This will exit with failure if it encounters an |
| 3360 | unrecoverable error, but continue on for recoverable |
| 3361 | errors. |
| 3362 | |
| 3363 | If a recoverable error occurs on a matched item, then |
| 3364 | that error code is returned. |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3365 | */ |
Laurence Lundblade | 0a042a9 | 2020-06-12 14:09:50 -0700 | [diff] [blame] | 3366 | const uint8_t uMapNestLevel = DecodeNesting_GetBoundedModeLevel(&(pMe->nesting)); |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3367 | if(pInfo) { |
| 3368 | pInfo->uItemCount = 0; |
| 3369 | } |
Máté Tóth-Pál | 2f2aa5f | 2021-05-17 21:35:09 +0200 | [diff] [blame] | 3370 | uint8_t uNextNestLevel; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3371 | do { |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3372 | /* Remember offset of the item because sometimes it has to be returned */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3373 | const size_t uOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3374 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3375 | /* Get the item */ |
| 3376 | QCBORItem Item; |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 3377 | /* QCBORDecode_Private_GetNextTagContent() rather than GetNext() |
| 3378 | * because a label match is performed on recoverable errors to |
| 3379 | * be able to return the the error code for the found item. */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3380 | QCBORError uResult = QCBORDecode_Private_GetNextTagContent(pMe, &Item); |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3381 | if(QCBORDecode_IsUnrecoverableError(uResult)) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3382 | /* The map/array can't be decoded when unrecoverable errors occur */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3383 | uReturn = uResult; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3384 | goto Done; |
| 3385 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3386 | if(uResult == QCBOR_ERR_NO_MORE_ITEMS) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3387 | /* Unexpected end of map or array. */ |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3388 | uReturn = uResult; |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 3389 | goto Done; |
| 3390 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3391 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3392 | /* See if item has one of the labels that are of interest */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3393 | bool bMatched = false; |
| 3394 | for(int nIndex = 0; pItemArray[nIndex].uLabelType != QCBOR_TYPE_NONE; nIndex++) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3395 | if(QCBORItem_MatchLabel(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3396 | /* A label match has been found */ |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3397 | if(uFoundItemBitMap & (0x01ULL << nIndex)) { |
| 3398 | uReturn = QCBOR_ERR_DUPLICATE_LABEL; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3399 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3400 | } |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3401 | if(uResult != QCBOR_SUCCESS) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3402 | /* The label matches, but the data item is in error. |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 3403 | * It is OK to have recoverable errors on items that |
| 3404 | * are not matched. */ |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3405 | uReturn = uResult; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3406 | goto Done; |
| 3407 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3408 | if(!QCBORItem_MatchType(Item, pItemArray[nIndex])) { |
Laurence Lundblade | 6392605 | 2021-03-29 16:05:51 -0700 | [diff] [blame] | 3409 | /* The data item is not of the type(s) requested */ |
| 3410 | uReturn = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3411 | goto Done; |
| 3412 | } |
| 3413 | |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3414 | /* Successful match. Return the item. */ |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3415 | pItemArray[nIndex] = Item; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3416 | uFoundItemBitMap |= 0x01ULL << nIndex; |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3417 | if(pInfo) { |
| 3418 | pInfo->uStartOffset = uOffset; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3419 | } |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3420 | bMatched = true; |
| 3421 | } |
| 3422 | } |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3423 | |
| 3424 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3425 | if(!bMatched && pCallBack != NULL) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3426 | /* |
| 3427 | Call the callback on unmatched labels. |
| 3428 | (It is tempting to do duplicate detection here, but that would |
| 3429 | require dynamic memory allocation because the number of labels |
| 3430 | that might be encountered is unbounded.) |
| 3431 | */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3432 | uReturn = (*(pCallBack->pfCallback))(pCallBack->pCBContext, &Item); |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3433 | if(uReturn != QCBOR_SUCCESS) { |
| 3434 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3435 | } |
| 3436 | } |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3437 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3438 | /* |
| 3439 | Consume the item whether matched or not. This |
| 3440 | does the work of traversing maps and array and |
| 3441 | everything in them. In this loop only the |
| 3442 | items at the current nesting level are examined |
| 3443 | to match the labels. |
| 3444 | */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3445 | uReturn = QCBORDecode_Private_ConsumeItem(pMe, &Item, NULL, &uNextNestLevel); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 3446 | if(uReturn != QCBOR_SUCCESS) { |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3447 | goto Done; |
| 3448 | } |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3449 | |
| 3450 | if(pInfo) { |
| 3451 | pInfo->uItemCount++; |
| 3452 | } |
| 3453 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3454 | } while (uNextNestLevel >= uMapNestLevel); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 3455 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3456 | uReturn = QCBOR_SUCCESS; |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3457 | |
| 3458 | const size_t uEndOffset = UsefulInputBuf_Tell(&(pMe->InBuf)); |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3459 | |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3460 | // Check here makes sure that this won't accidentally be |
| 3461 | // QCBOR_MAP_OFFSET_CACHE_INVALID which is larger than |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3462 | // QCBOR_MAX_DECODE_INPUT_SIZE. |
Laurence Lundblade | 9c1b7b4 | 2020-12-12 11:44:16 -0800 | [diff] [blame] | 3463 | // Cast to uint32_t to possibly address cases where SIZE_MAX < UINT32_MAX |
| 3464 | if((uint32_t)uEndOffset >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 3465 | uReturn = QCBOR_ERR_INPUT_TOO_LARGE; |
| 3466 | goto Done; |
| 3467 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 3468 | /* Cast OK because encoded CBOR is limited to UINT32_MAX */ |
| 3469 | pMe->uMapEndOffsetCache = (uint32_t)uEndOffset; |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3470 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 3471 | Done: |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3472 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
Laurence Lundblade | 1165491 | 2024-05-09 11:49:24 -0700 | [diff] [blame] | 3473 | UsefulInputBuf_Seek(&(pMe->InBuf), uSavePos); |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 3474 | |
| 3475 | Done2: |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3476 | /* 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] | 3477 | for(int i = 0; pItemArray[i].uLabelType != 0; i++) { |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3478 | if(!(uFoundItemBitMap & (0x01ULL << i))) { |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3479 | pItemArray[i].uDataType = QCBOR_TYPE_NONE; |
| 3480 | pItemArray[i].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3481 | } |
| 3482 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3483 | |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 3484 | return uReturn; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3485 | } |
| 3486 | |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 3487 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3488 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3489 | * Public function, see header qcbor/qcbor_decode.h file |
| 3490 | */ |
| 3491 | void |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3492 | QCBORDecode_SeekToLabelN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3493 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3494 | MapSearchInfo Info; |
| 3495 | QCBORItem OneItemSeach[2]; |
| 3496 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3497 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3498 | return; |
| 3499 | } |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3500 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3501 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3502 | OneItemSeach[0].label.int64 = nLabel; |
| 3503 | OneItemSeach[0].uDataType = QCBOR_TYPE_ANY; |
| 3504 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
| 3505 | |
| 3506 | pMe->uLastError = (uint8_t)QCBORDecode_Private_MapSearch(pMe, OneItemSeach, &Info, NULL); |
| 3507 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3508 | UsefulInputBuf_Seek(&(pMe->InBuf), Info.uStartOffset); |
| 3509 | } |
| 3510 | } |
| 3511 | |
| 3512 | |
| 3513 | void |
| 3514 | QCBORDecode_SeekToLabelSZ(QCBORDecodeContext *pMe, const char *szLabel) |
| 3515 | { |
| 3516 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 3517 | MapSearchInfo Info; |
| 3518 | QCBORItem OneItemSeach[2]; |
| 3519 | |
| 3520 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3521 | return; |
| 3522 | } |
| 3523 | |
| 3524 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3525 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3526 | OneItemSeach[0].uDataType = QCBOR_TYPE_ANY; |
| 3527 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
| 3528 | |
| 3529 | pMe->uLastError = (uint8_t)QCBORDecode_Private_MapSearch(pMe, OneItemSeach, &Info, NULL); |
| 3530 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3531 | UsefulInputBuf_Seek(&(pMe->InBuf), Info.uStartOffset); |
| 3532 | } |
| 3533 | #else |
| 3534 | (void)pMe; |
| 3535 | (void)szLabel; |
| 3536 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
| 3537 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 3538 | } |
| 3539 | |
| 3540 | |
| 3541 | void |
| 3542 | QCBORDecode_Private_GetItemInMapNoCheck(QCBORDecodeContext *pMe, |
| 3543 | QCBORItem *OneItemSeach, |
| 3544 | QCBORItem *pItem, |
| 3545 | size_t *puOffset) |
| 3546 | { |
| 3547 | QCBORError uErr; |
| 3548 | MapSearchInfo SearchInfo; |
| 3549 | |
| 3550 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3551 | return; |
| 3552 | } |
| 3553 | |
| 3554 | uErr = QCBORDecode_Private_MapSearch(pMe, OneItemSeach, &SearchInfo, NULL); |
| 3555 | |
| 3556 | if(uErr == QCBOR_SUCCESS && OneItemSeach[0].uDataType == QCBOR_TYPE_NONE) { |
| 3557 | uErr = QCBOR_ERR_LABEL_NOT_FOUND; |
| 3558 | } |
| 3559 | *pItem = OneItemSeach[0]; |
| 3560 | *puOffset = SearchInfo.uStartOffset; |
| 3561 | |
| 3562 | if(uErr == QCBOR_SUCCESS) { |
| 3563 | QCBORDecode_Private_SaveTagNumbers(pMe, pItem); |
| 3564 | } |
| 3565 | |
| 3566 | pMe->uLastError = (uint8_t)uErr; |
| 3567 | } |
| 3568 | |
| 3569 | |
| 3570 | static void |
| 3571 | QCBORDecode_Private_GetItemInMap(QCBORDecodeContext *pMe, QCBORItem *OneItemSeach, QCBORItem *pItem) |
| 3572 | { |
| 3573 | QCBORError uErr; |
| 3574 | size_t uOffset; |
| 3575 | |
| 3576 | QCBORDecode_Private_GetItemInMapNoCheck(pMe, OneItemSeach, pItem, &uOffset); |
| 3577 | |
| 3578 | uErr = QCBORDecode_Private_GetItemChecks(pMe, pMe->uLastError, uOffset, pItem); |
| 3579 | if(uErr != QCBOR_SUCCESS) { |
| 3580 | goto Done; |
| 3581 | } |
| 3582 | |
| 3583 | QCBORDecode_Private_SaveTagNumbers(pMe, pItem); |
| 3584 | |
| 3585 | Done: |
| 3586 | pMe->uLastError = (uint8_t)uErr; |
| 3587 | } |
| 3588 | |
| 3589 | |
| 3590 | /* |
| 3591 | * Public function, see header qcbor/qcbor_decode.h file |
| 3592 | */ |
| 3593 | void |
| 3594 | QCBORDecode_GetItemInMapN(QCBORDecodeContext *pMe, |
| 3595 | const int64_t nLabel, |
| 3596 | const uint8_t uQcborType, |
| 3597 | QCBORItem *pItem) |
| 3598 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3599 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3600 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3601 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3602 | OneItemSeach[0].label.int64 = nLabel; |
| 3603 | OneItemSeach[0].uDataType = uQcborType; |
| 3604 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3605 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3606 | QCBORDecode_Private_GetItemInMap(pMe, OneItemSeach, pItem); |
| 3607 | } |
Laurence Lundblade | 93d3f53 | 2020-09-28 21:09:12 -0700 | [diff] [blame] | 3608 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 3609 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3610 | /** |
| 3611 | * @brief Get an item by label by type. |
| 3612 | * |
| 3613 | * @param[in] pMe The decode context. |
| 3614 | * @param[in] nLabel The label to search map for. |
| 3615 | * @param[in] uQcborType The QCBOR type to look for. |
| 3616 | * @param[out] pItem The item found. |
| 3617 | * @param[out] puOffset The offset of item for tag consumption check. |
| 3618 | * |
| 3619 | * This finds the item with the given label in currently open |
| 3620 | * map. This does not call QCBORDecode_Private_GetItemChecks() |
| 3621 | * to check tag number consumption or decode conformance. |
| 3622 | */ |
| 3623 | static void |
| 3624 | QCBORDecode_GetItemInMapNoCheckN(QCBORDecodeContext *pMe, |
| 3625 | const int64_t nLabel, |
| 3626 | const uint8_t uQcborType, |
| 3627 | QCBORItem *pItem, |
| 3628 | size_t *puOffset) |
| 3629 | { |
| 3630 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3631 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3632 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3633 | OneItemSeach[0].label.int64 = nLabel; |
| 3634 | OneItemSeach[0].uDataType = uQcborType; |
| 3635 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 3636 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3637 | QCBORDecode_Private_GetItemInMapNoCheck(pMe, OneItemSeach, pItem, puOffset); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3641 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3642 | * Public function, see header qcbor/qcbor_decode.h file |
| 3643 | */ |
| 3644 | void |
| 3645 | QCBORDecode_GetItemInMapSZ(QCBORDecodeContext *pMe, |
| 3646 | const char *szLabel, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3647 | const uint8_t uQcborType, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3648 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3649 | { |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 3650 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3651 | QCBORItem OneItemSeach[2]; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3652 | |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 3653 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3654 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3655 | OneItemSeach[0].uDataType = uQcborType; |
| 3656 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3657 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3658 | QCBORDecode_Private_GetItemInMap(pMe, OneItemSeach, pItem); |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3659 | |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 3660 | #else |
| 3661 | (void)pMe; |
| 3662 | (void)szLabel; |
| 3663 | (void)uQcborType; |
| 3664 | (void)pItem; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3665 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 3666 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3667 | } |
| 3668 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3669 | /** |
| 3670 | * @brief Get an item by string label of a particular type |
| 3671 | * |
| 3672 | * @param[in] pMe The decode context. |
| 3673 | * @param[in] szLabel The label to search map for. |
| 3674 | * @param[in] uQcborType The QCBOR type to look for. |
| 3675 | * @param[out] pItem The item found. |
| 3676 | * @param[out] puOffset The offset of item for tag consumption check. |
| 3677 | * |
| 3678 | * This finds the item with the given label in currently open |
| 3679 | * map. This does not call QCBORDecode_Private_GetItemChecks() |
| 3680 | * to check tag number consumption or decode conformance. |
| 3681 | */ |
| 3682 | static void |
| 3683 | QCBORDecode_GetItemInMapNoCheckSZ(QCBORDecodeContext *pMe, |
| 3684 | const char *szLabel, |
| 3685 | const uint8_t uQcborType, |
| 3686 | QCBORItem *pItem, |
| 3687 | size_t *puOffset) |
| 3688 | { |
| 3689 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 3690 | QCBORItem OneItemSeach[2]; |
| 3691 | |
| 3692 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 3693 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 3694 | OneItemSeach[0].uDataType = uQcborType; |
| 3695 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
| 3696 | |
| 3697 | QCBORDecode_Private_GetItemInMapNoCheck(pMe, OneItemSeach, pItem, puOffset); |
| 3698 | |
| 3699 | #else |
| 3700 | (void)pMe; |
| 3701 | (void)szLabel; |
| 3702 | (void)uQcborType; |
| 3703 | (void)pItem; |
| 3704 | (void)puOffset; |
| 3705 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
| 3706 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 3707 | } |
| 3708 | |
| 3709 | |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 3710 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3711 | |
| 3712 | /** |
| 3713 | * @brief Semi-private. Get pointer, length and item for an array or map. |
| 3714 | * |
| 3715 | * @param[in] pMe The decode context. |
| 3716 | * @param[in] uType CBOR major type, either array/map. |
| 3717 | * @param[out] pItem The item for the array/map. |
| 3718 | * @param[out] pEncodedCBOR Pointer and length of the encoded map or array. |
| 3719 | * |
Laurence Lundblade | 88ba566 | 2024-11-03 02:10:24 -0800 | [diff] [blame] | 3720 | * The next item to be decoded must be a map or array as specified by @c uType. |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3721 | * |
Laurence Lundblade | 88ba566 | 2024-11-03 02:10:24 -0800 | [diff] [blame] | 3722 | * @c pItem will be filled in with the label and tags of the array or map |
| 3723 | * in addition to @c pEncodedCBOR giving the pointer and length of the |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3724 | * encoded CBOR. |
| 3725 | * |
| 3726 | * When this is complete, the traversal cursor is at the end of the array or |
| 3727 | * map that was retrieved. |
| 3728 | */ |
| 3729 | void |
| 3730 | QCBORDecode_Private_GetArrayOrMap(QCBORDecodeContext *pMe, |
| 3731 | const uint8_t uType, |
| 3732 | QCBORItem *pItem, |
| 3733 | UsefulBufC *pEncodedCBOR) |
| 3734 | { |
| 3735 | QCBORError uErr; |
| 3736 | uint8_t uNestLevel; |
| 3737 | size_t uStartingCursor; |
| 3738 | size_t uStartOfReturned; |
| 3739 | size_t uEndOfReturned; |
| 3740 | size_t uTempSaveCursor; |
| 3741 | bool bInMap; |
| 3742 | QCBORItem LabelItem; |
| 3743 | bool EndedByBreak; |
| 3744 | |
| 3745 | uStartingCursor = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3746 | bInMap = DecodeNesting_IsCurrentTypeMap(&(pMe->nesting)); |
| 3747 | |
| 3748 | /* Could call GetNext here, but don't need to because this |
Laurence Lundblade | 9b2ae8a | 2024-07-12 11:00:20 -0700 | [diff] [blame] | 3749 | * is only interested in arrays and maps. TODO: switch to GetNext()? */ |
| 3750 | uErr = QCBORDecode_Private_GetNextMapOrArray(pMe, NULL, pItem, NULL); |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3751 | if(uErr != QCBOR_SUCCESS) { |
| 3752 | pMe->uLastError = (uint8_t)uErr; |
| 3753 | return; |
| 3754 | } |
| 3755 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 3756 | uint8_t uItemDataType = pItem->uDataType; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 3757 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 3758 | if(uItemDataType == QCBOR_TYPE_MAP_AS_ARRAY) { |
| 3759 | uItemDataType = QCBOR_TYPE_ARRAY; |
| 3760 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 3761 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 3762 | |
| 3763 | if(uItemDataType != uType) { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3764 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 3765 | return; |
| 3766 | } |
| 3767 | |
| 3768 | if(bInMap) { |
| 3769 | /* If the item is in a map, the start of the array/map |
| 3770 | * itself, not the label, must be found. Do this by |
| 3771 | * rewinding to the starting position and fetching |
| 3772 | * just the label data item. QCBORDecode_Private_GetNextTagNumber() |
| 3773 | * doesn't do any of the array/map item counting or nesting |
| 3774 | * level tracking. Used here it will just fetech the label |
| 3775 | * data item. |
| 3776 | * |
| 3777 | * Have to save the cursor and put it back to the position |
| 3778 | * after the full item once the label as been fetched by |
| 3779 | * itself. |
| 3780 | */ |
| 3781 | uTempSaveCursor = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3782 | UsefulInputBuf_Seek(&(pMe->InBuf), uStartingCursor); |
| 3783 | |
| 3784 | /* Item has been fetched once so safe to ignore error */ |
| 3785 | (void)QCBORDecode_Private_GetNextTagNumber(pMe, &LabelItem); |
| 3786 | |
| 3787 | uStartOfReturned = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3788 | UsefulInputBuf_Seek(&(pMe->InBuf), uTempSaveCursor); |
| 3789 | } else { |
| 3790 | uStartOfReturned = uStartingCursor; |
| 3791 | } |
| 3792 | |
| 3793 | /* Consume the entire array/map to find the end */ |
| 3794 | uErr = QCBORDecode_Private_ConsumeItem(pMe, pItem, &EndedByBreak, &uNestLevel); |
| 3795 | if(uErr != QCBOR_SUCCESS) { |
| 3796 | pMe->uLastError = (uint8_t)uErr; |
| 3797 | goto Done; |
| 3798 | } |
| 3799 | |
| 3800 | /* Fill in returned values */ |
| 3801 | uEndOfReturned = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3802 | if(EndedByBreak) { |
| 3803 | /* When ascending nesting levels, a break for the level above |
| 3804 | * was consumed. That break is not a part of what is consumed here. */ |
| 3805 | uEndOfReturned--; |
| 3806 | } |
| 3807 | pEncodedCBOR->ptr = UsefulInputBuf_OffsetToPointer(&(pMe->InBuf), uStartOfReturned); |
| 3808 | pEncodedCBOR->len = uEndOfReturned - uStartOfReturned; |
| 3809 | |
| 3810 | Done: |
| 3811 | return; |
| 3812 | } |
| 3813 | |
| 3814 | |
| 3815 | /** |
| 3816 | * @brief Semi-private. Get pointer, length and item count of an array or map. |
| 3817 | * |
| 3818 | * @param[in] pMe The decode context. |
| 3819 | * @param[in] pTarget The label and type of the array or map to retrieve. |
| 3820 | * @param[out] pItem The item for the array/map. |
| 3821 | * @param[out] pEncodedCBOR Pointer and length of the encoded map or array. |
| 3822 | * |
Laurence Lundblade | 88ba566 | 2024-11-03 02:10:24 -0800 | [diff] [blame] | 3823 | * The next item to be decoded must be a map or array as specified by @c uType. |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3824 | * |
| 3825 | * When this is complete, the traversal cursor is unchanged. |
| 3826 | */void |
| 3827 | QCBORDecode_Private_SearchAndGetArrayOrMap(QCBORDecodeContext *pMe, |
| 3828 | QCBORItem *pTarget, |
| 3829 | QCBORItem *pItem, |
| 3830 | UsefulBufC *pEncodedCBOR) |
| 3831 | { |
| 3832 | MapSearchInfo Info; |
| 3833 | QCBORDecodeNesting SaveNesting; |
| 3834 | size_t uSaveCursor; |
| 3835 | |
| 3836 | pMe->uLastError = (uint8_t)QCBORDecode_Private_MapSearch(pMe, pTarget, &Info, NULL); |
| 3837 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3838 | return; |
| 3839 | } |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3840 | pMe->uLastError = (uint8_t)QCBORDecode_Private_GetItemChecks(pMe, pMe->uLastError, Info.uStartOffset, pItem); |
| 3841 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3842 | return; |
| 3843 | } |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3844 | |
| 3845 | /* Save the whole position of things so they can be restored. |
| 3846 | * so the cursor position is unchanged by this operation, like |
| 3847 | * all the other GetXxxxInMap() operations. */ |
| 3848 | DecodeNesting_PrepareForMapSearch(&(pMe->nesting), &SaveNesting); |
| 3849 | uSaveCursor = UsefulInputBuf_Tell(&(pMe->InBuf)); |
| 3850 | |
| 3851 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 3852 | UsefulInputBuf_Seek(&(pMe->InBuf), Info.uStartOffset); |
| 3853 | QCBORDecode_Private_GetArrayOrMap(pMe, pTarget[0].uDataType, pItem, pEncodedCBOR); |
| 3854 | |
| 3855 | UsefulInputBuf_Seek(&(pMe->InBuf), uSaveCursor); |
| 3856 | DecodeNesting_RestoreFromMapSearch(&(pMe->nesting), &SaveNesting); |
| 3857 | } |
| 3858 | |
| 3859 | |
| 3860 | |
| 3861 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3862 | static void |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3863 | QCBORDecode_Private_ProcessTagOne(QCBORDecodeContext *pMe, |
| 3864 | QCBORItem *pItem, |
| 3865 | const uint8_t uTagRequirement, |
| 3866 | const uint8_t uQCBORType, |
| 3867 | const uint64_t uTagNumber, |
| 3868 | QCBORTagContentCallBack *pfCB, |
| 3869 | size_t uOffset); |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3870 | |
| 3871 | /** |
| 3872 | * @brief Semi-private to get an string by label to match a tag specification. |
| 3873 | * |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3874 | * @param[in] pMe The decode context. |
| 3875 | * @param[in] nLabel Label to search map for. |
| 3876 | * @param[in] uTagRequirement Whether or not tag number is required. |
| 3877 | * See @ref QCBOR_TAG_REQUIREMENT_TAG. |
| 3878 | * @param[in] uQCBOR_Type QCBOR type to search for. |
| 3879 | * @param[in] uTagNumber Tag number to match. |
| 3880 | * @param[out] pString The string found. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3881 | * |
| 3882 | * This finds the string with the given label in currently open |
| 3883 | * map. Then checks that its tag number and types matches the tag |
| 3884 | * specification. If not, an error is set in the decode context. |
| 3885 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3886 | void |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3887 | QCBORDecode_Private_GetTaggedStringInMapN(QCBORDecodeContext *pMe, |
| 3888 | const int64_t nLabel, |
| 3889 | const uint8_t uTagRequirement, |
| 3890 | const uint8_t uQCBOR_Type, |
| 3891 | const uint64_t uTagNumber, |
| 3892 | UsefulBufC *pString) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3893 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3894 | QCBORItem Item; |
| 3895 | size_t uOffset; |
| 3896 | |
| 3897 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 3898 | QCBORDecode_Private_ProcessTagOne(pMe, |
| 3899 | &Item, |
| 3900 | uTagRequirement, |
| 3901 | uQCBOR_Type, |
| 3902 | uTagNumber, |
| 3903 | QCBORDecode_StringsTagCB, |
| 3904 | uOffset); |
| 3905 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3906 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3907 | *pString = Item.val.string; |
| 3908 | } |
| 3909 | } |
| 3910 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3911 | |
| 3912 | /** |
| 3913 | * @brief Semi-private to get an string by label to match a tag specification. |
| 3914 | * |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3915 | * @param[in] pMe The decode context. |
| 3916 | * @param[in] szLabel Label to search map for. |
| 3917 | * @param[in] uTagRequirement Whether or not tag number is required. |
| 3918 | * See @ref QCBOR_TAG_REQUIREMENT_TAG. |
| 3919 | * @param[in] uQCBOR_Type QCBOR type to search for. |
| 3920 | * @param[in] uTagNumber Tag number to match. |
| 3921 | * @param[out] pString The string found. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3922 | * |
| 3923 | * This finds the string with the given label in currently open |
| 3924 | * map. Then checks that its tag number and types matches the tag |
| 3925 | * specification. If not, an error is set in the decode context. |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3926 | */ |
| 3927 | void |
| 3928 | QCBORDecode_Private_GetTaggedStringInMapSZ(QCBORDecodeContext *pMe, |
| 3929 | const char *szLabel, |
| 3930 | uint8_t uTagRequirement, |
| 3931 | uint8_t uQCBOR_Type, |
| 3932 | uint64_t uTagNumber, |
| 3933 | UsefulBufC *pString) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3934 | { |
| 3935 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3936 | size_t uOffset; |
| 3937 | |
| 3938 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 3939 | QCBORDecode_Private_ProcessTagOne(pMe, |
| 3940 | &Item, |
| 3941 | uTagRequirement, |
| 3942 | uQCBOR_Type, |
| 3943 | uTagNumber, |
| 3944 | QCBORDecode_StringsTagCB, |
| 3945 | uOffset); |
| 3946 | |
| 3947 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 3948 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 3949 | *pString = Item.val.string; |
| 3950 | } |
| 3951 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 3952 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 3953 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3954 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3955 | * Public function, see header qcbor/qcbor_decode.h file |
| 3956 | */ |
| 3957 | void |
| 3958 | QCBORDecode_GetItemsInMap(QCBORDecodeContext *pMe, QCBORItem *pItemList) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3959 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3960 | pMe->uLastError = (uint8_t)QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, NULL); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3961 | } |
| 3962 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 3963 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 3964 | * Public function, see header qcbor/qcbor_decode.h file |
| 3965 | */ |
| 3966 | void |
| 3967 | QCBORDecode_GetItemsInMapWithCallback(QCBORDecodeContext *pMe, |
| 3968 | QCBORItem *pItemList, |
| 3969 | void *pCallbackCtx, |
| 3970 | QCBORItemCallback pfCB) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3971 | { |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3972 | MapSearchCallBack CallBack; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3973 | |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 3974 | CallBack.pCBContext = pCallbackCtx; |
| 3975 | CallBack.pfCallback = pfCB; |
| 3976 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3977 | pMe->uLastError = (uint8_t)QCBORDecode_Private_MapSearch(pMe, pItemList, NULL, &CallBack); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 3978 | } |
| 3979 | |
| 3980 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 3981 | #ifndef QCBOR_DISABLE_TAGS |
| 3982 | /* |
| 3983 | * Public function, see header qcbor/qcbor_decode.h file |
| 3984 | */ |
| 3985 | QCBORError |
| 3986 | QCBORDecode_GetNextTagNumberInMapN(QCBORDecodeContext *pMe, const int64_t nLabel, uint64_t *puTagNumber) |
| 3987 | { |
| 3988 | size_t uOffset; |
| 3989 | MapSearchInfo Info; |
| 3990 | QCBORItem OneItemSeach[2]; |
| 3991 | |
| 3992 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 3993 | return pMe->uLastError; |
| 3994 | } |
| 3995 | |
| 3996 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 3997 | OneItemSeach[0].label.int64 = nLabel; |
| 3998 | OneItemSeach[0].uDataType = QCBOR_TYPE_ANY; |
| 3999 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
| 4000 | |
| 4001 | QCBORError uReturn = QCBORDecode_Private_MapSearch(pMe, OneItemSeach, &Info, NULL); |
| 4002 | |
| 4003 | uOffset = Info.uStartOffset; |
| 4004 | if(uOffset == pMe->uTagNumberCheckOffset) { |
| 4005 | pMe->uTagNumberIndex++; |
| 4006 | } else { |
| 4007 | pMe->uTagNumberIndex = 0; |
| 4008 | } |
| 4009 | |
| 4010 | *puTagNumber = CBOR_TAG_INVALID64; |
| 4011 | |
| 4012 | *puTagNumber = QCBORDecode_GetNthTagNumber(pMe, &OneItemSeach[0], pMe->uTagNumberIndex); |
| 4013 | if(*puTagNumber == CBOR_TAG_INVALID64 || |
| 4014 | QCBORDecode_GetNthTagNumber(pMe, &OneItemSeach[0], pMe->uTagNumberIndex+1) == CBOR_TAG_INVALID64 ) { |
| 4015 | pMe->uTagNumberIndex = QCBOR_ALL_TAGS_PROCESSED; |
| 4016 | } |
| 4017 | pMe->uTagNumberCheckOffset = uOffset; |
| 4018 | |
| 4019 | return uReturn; |
| 4020 | } |
| 4021 | |
| 4022 | |
| 4023 | /* |
| 4024 | * Public function, see header qcbor/qcbor_decode.h file |
| 4025 | */ |
| 4026 | QCBORError |
| 4027 | QCBORDecode_GetNextTagNumberInMapSZ(QCBORDecodeContext *pMe, const char *szLabel, uint64_t *puTagNumber) |
| 4028 | { |
| 4029 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
| 4030 | size_t uOffset; |
| 4031 | MapSearchInfo Info; |
| 4032 | QCBORItem OneItemSeach[2]; |
| 4033 | |
| 4034 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4035 | return pMe->uLastError; |
| 4036 | } |
| 4037 | |
| 4038 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 4039 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 4040 | OneItemSeach[0].uDataType = QCBOR_TYPE_ANY; |
| 4041 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; // Indicates end of array |
| 4042 | |
| 4043 | QCBORError uReturn = QCBORDecode_Private_MapSearch(pMe, OneItemSeach, &Info, NULL); |
| 4044 | |
| 4045 | |
| 4046 | uOffset = Info.uStartOffset; |
| 4047 | if(uOffset == pMe->uTagNumberCheckOffset) { |
| 4048 | pMe->uTagNumberIndex++; |
| 4049 | } else { |
| 4050 | pMe->uTagNumberIndex = 0; |
| 4051 | } |
| 4052 | |
| 4053 | *puTagNumber = CBOR_TAG_INVALID64; |
| 4054 | |
| 4055 | *puTagNumber = QCBORDecode_GetNthTagNumber(pMe, &OneItemSeach[0], pMe->uTagNumberIndex); |
| 4056 | if(*puTagNumber == CBOR_TAG_INVALID64 || |
| 4057 | QCBORDecode_GetNthTagNumber(pMe, &OneItemSeach[0], pMe->uTagNumberIndex+1) == CBOR_TAG_INVALID64 ) { |
| 4058 | pMe->uTagNumberIndex = 255; /* All tags clear for this item */ |
| 4059 | } |
| 4060 | pMe->uTagNumberCheckOffset = uOffset; |
| 4061 | |
| 4062 | return uReturn; |
| 4063 | #else |
| 4064 | (void)pMe; |
| 4065 | (void)szLabel; |
| 4066 | (void)puTagNumber; |
| 4067 | return QCBOR_ERR_LABEL_NOT_FOUND; |
| 4068 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 4069 | } |
| 4070 | #endif /* ! QCBOR_DISABLE_TAGS */ |
| 4071 | |
| 4072 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4073 | /** |
| 4074 | * @brief Search for a map/array by label and enter it |
| 4075 | * |
| 4076 | * @param[in] pMe The decode context. |
| 4077 | * @param[in] pSearch The map/array to search for. |
| 4078 | * |
| 4079 | * @c pSearch is expected to contain one item of type map or array |
| 4080 | * with the label specified. The current bounded map will be searched for |
| 4081 | * this and if found will be entered. |
| 4082 | * |
| 4083 | * If the label is not found, or the item found is not a map or array, |
| 4084 | * the error state is set. |
| 4085 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4086 | static void |
| 4087 | QCBORDecode_Private_SearchAndEnter(QCBORDecodeContext *pMe, QCBORItem pSearch[]) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4088 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4089 | QCBORError uErr; |
| 4090 | MapSearchInfo SearchInfo; |
| 4091 | |
Laurence Lundblade | 323f8a9 | 2020-09-06 19:43:09 -0700 | [diff] [blame] | 4092 | // The first item in pSearch is the one that is to be |
| 4093 | // entered. It should be the only one filled in. Any other |
| 4094 | // will be ignored unless it causes an error. |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4095 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4096 | return; |
| 4097 | } |
| 4098 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4099 | uErr = QCBORDecode_Private_MapSearch(pMe, pSearch, &SearchInfo, NULL); |
| 4100 | |
| 4101 | pMe->uLastError = (uint8_t)QCBORDecode_Private_GetItemChecks(pMe, uErr, SearchInfo.uStartOffset, pSearch); |
| 4102 | |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4103 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4104 | return; |
| 4105 | } |
| 4106 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4107 | if(pSearch->uDataType == QCBOR_TYPE_NONE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4108 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4109 | return; |
| 4110 | } |
| 4111 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4112 | |
| 4113 | /* The map or array was found. Now enter it. |
| 4114 | * |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4115 | * QCBORDecode_EnterBoundedMapOrArray() used here, requires the |
| 4116 | * next item for the pre-order traversal cursor to be the map/array |
| 4117 | * found by MapSearch(). The next few lines of code force the |
| 4118 | * cursor to that. |
| 4119 | * |
| 4120 | * There is no need to retain the old cursor because |
| 4121 | * QCBORDecode_EnterBoundedMapOrArray() will set it to the |
| 4122 | * beginning of the map/array being entered. |
| 4123 | * |
| 4124 | * The cursor is forced by: 1) setting the input buffer position to |
| 4125 | * the item offset found by MapSearch(), 2) setting the map/array |
| 4126 | * counter to the total in the map/array, 3) setting the nesting |
| 4127 | * level. Setting the map/array counter to the total is not |
| 4128 | * strictly correct, but this is OK because this cursor only needs |
| 4129 | * to be used to get one item and MapSearch() has already found it |
| 4130 | * confirming it exists. |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 4131 | */ |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4132 | UsefulInputBuf_Seek(&(pMe->InBuf), SearchInfo.uStartOffset); |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4133 | |
Laurence Lundblade | ddebabe | 2020-11-22 11:32:17 -0800 | [diff] [blame] | 4134 | DecodeNesting_ResetMapOrArrayCount(&(pMe->nesting)); |
| 4135 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4136 | DecodeNesting_SetCurrentToBoundedLevel(&(pMe->nesting)); |
Laurence Lundblade | 82c2a8f | 2020-04-29 12:40:19 -0700 | [diff] [blame] | 4137 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4138 | QCBORDecode_Private_EnterBoundedMapOrArray(pMe, pSearch->uDataType, NULL); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4142 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4143 | * Public function, see header qcbor/qcbor_decode.h file |
| 4144 | */ |
| 4145 | void |
| 4146 | QCBORDecode_EnterMapFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4147 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 4148 | QCBORItem OneItemSeach[2]; |
| 4149 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 4150 | OneItemSeach[0].label.int64 = nLabel; |
| 4151 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 4152 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4153 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4154 | /* The map to enter was found, now finish off entering it. */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4155 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4159 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4160 | * Public function, see header qcbor/qcbor_decode.h file |
| 4161 | */ |
| 4162 | void |
| 4163 | QCBORDecode_EnterMapFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4164 | { |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4165 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 4166 | QCBORItem OneItemSeach[2]; |
| 4167 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 4168 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 4169 | OneItemSeach[0].uDataType = QCBOR_TYPE_MAP; |
| 4170 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 4171 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4172 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4173 | #else |
| 4174 | (void)szLabel; |
| 4175 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
| 4176 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4177 | } |
| 4178 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4179 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4180 | * Public function, see header qcbor/qcbor_decode.h file |
| 4181 | */ |
| 4182 | void |
| 4183 | QCBORDecode_EnterArrayFromMapN(QCBORDecodeContext *pMe, int64_t nLabel) |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4184 | { |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 4185 | QCBORItem OneItemSeach[2]; |
| 4186 | OneItemSeach[0].uLabelType = QCBOR_TYPE_INT64; |
| 4187 | OneItemSeach[0].label.int64 = nLabel; |
| 4188 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 4189 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4190 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4191 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4192 | } |
| 4193 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4194 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4195 | * Public function, see header qcbor/qcbor_decode.h file |
| 4196 | */ |
| 4197 | void |
| 4198 | QCBORDecode_EnterArrayFromMapSZ(QCBORDecodeContext *pMe, const char *szLabel) |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4199 | { |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4200 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | b90f536 | 2020-05-25 12:17:40 -0700 | [diff] [blame] | 4201 | QCBORItem OneItemSeach[2]; |
| 4202 | OneItemSeach[0].uLabelType = QCBOR_TYPE_TEXT_STRING; |
| 4203 | OneItemSeach[0].label.string = UsefulBuf_FromSZ(szLabel); |
| 4204 | OneItemSeach[0].uDataType = QCBOR_TYPE_ARRAY; |
| 4205 | OneItemSeach[1].uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4206 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4207 | QCBORDecode_Private_SearchAndEnter(pMe, OneItemSeach); |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4208 | #else |
| 4209 | (void)szLabel; |
| 4210 | pMe->uLastError = QCBOR_ERR_LABEL_NOT_FOUND; |
| 4211 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4215 | /** |
| 4216 | * @brief Semi-private to do the the work for EnterMap() and EnterArray(). |
| 4217 | * |
| 4218 | * @param[in] pMe The decode context |
| 4219 | * @param[in] uType QCBOR_TYPE_MAP or QCBOR_TYPE_ARRAY. |
| 4220 | * @param[out] pItem The data item for the map or array entered. |
| 4221 | * |
| 4222 | * The next item in the traversal must be a map or array. This |
| 4223 | * consumes that item and does the book keeping to enter the map or |
| 4224 | * array. |
| 4225 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4226 | void |
| 4227 | QCBORDecode_Private_EnterBoundedMapOrArray(QCBORDecodeContext *pMe, |
| 4228 | const uint8_t uType, |
| 4229 | QCBORItem *pItem) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4230 | { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4231 | QCBORError uErr; |
| 4232 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4233 | /* Must only be called on maps and arrays. */ |
Laurence Lundblade | 34691b9 | 2020-05-18 22:25:25 -0700 | [diff] [blame] | 4234 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4235 | // Already in error state; do nothing. |
| 4236 | return; |
| 4237 | } |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4238 | |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 4239 | /* Get the data item that is the map or array being entered. */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4240 | QCBORItem Item; |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4241 | uErr = QCBORDecode_GetNext(pMe, &Item); |
| 4242 | if(uErr != QCBOR_SUCCESS) { |
| 4243 | goto Done; |
Laurence Lundblade | 3f9ef04 | 2020-04-14 13:15:51 -0700 | [diff] [blame] | 4244 | } |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 4245 | |
| 4246 | uint8_t uItemDataType = Item.uDataType; |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4247 | |
| 4248 | #ifndef QCBOR_DISABLE_NON_INTEGER_LABELS |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 4249 | if(uItemDataType == QCBOR_TYPE_MAP_AS_ARRAY ) { |
| 4250 | uItemDataType = QCBOR_TYPE_ARRAY; |
| 4251 | } |
Laurence Lundblade | ec290b8 | 2024-06-10 11:10:54 -0700 | [diff] [blame] | 4252 | #endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */ |
| 4253 | |
Laurence Lundblade | 5db34da | 2024-05-30 03:14:35 -0700 | [diff] [blame] | 4254 | if(uItemDataType != uType) { |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4255 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 4256 | goto Done; |
Laurence Lundblade | 1341c59 | 2020-04-11 14:19:05 -0700 | [diff] [blame] | 4257 | } |
| 4258 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4259 | QCBORDecode_Private_SaveTagNumbers(pMe, &Item); |
Laurence Lundblade | d40951e | 2020-08-28 11:11:14 -0700 | [diff] [blame] | 4260 | |
| 4261 | |
Laurence Lundblade | f049950 | 2020-08-01 11:55:57 -0700 | [diff] [blame] | 4262 | const bool bIsEmpty = (Item.uNextNestLevel <= Item.uNestingLevel); |
Laurence Lundblade | 085d795 | 2020-07-24 10:26:30 -0700 | [diff] [blame] | 4263 | if(bIsEmpty) { |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4264 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
| 4265 | // Undo decrement done by QCBORDecode_GetNext() so the the |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4266 | // the decrement when exiting the map/array works correctly |
Laurence Lundblade | 5f4e871 | 2020-07-25 11:44:43 -0700 | [diff] [blame] | 4267 | pMe->nesting.pCurrent->u.ma.uCountCursor++; |
| 4268 | } |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 4269 | // Special case to increment nesting level for zero-length maps |
| 4270 | // and arrays entered in bounded mode. |
Laurence Lundblade | e6f1511 | 2020-07-23 18:44:16 -0700 | [diff] [blame] | 4271 | DecodeNesting_Descend(&(pMe->nesting), uType); |
| 4272 | } |
| 4273 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 4274 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4275 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4276 | uErr = DecodeNesting_EnterBoundedMapOrArray(&(pMe->nesting), bIsEmpty, |
| 4277 | UsefulInputBuf_Tell(&(pMe->InBuf))); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4278 | |
Laurence Lundblade | 6545d1b | 2020-10-14 11:13:13 -0700 | [diff] [blame] | 4279 | if(pItem != NULL) { |
| 4280 | *pItem = Item; |
| 4281 | } |
| 4282 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4283 | Done: |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 4284 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4285 | } |
| 4286 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4287 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4288 | /** |
| 4289 | * @brief Exit a bounded map, array or bstr (semi-private). |
| 4290 | * |
| 4291 | * @param[in] pMe Decode context. |
| 4292 | * @param[in] uEndOffset The input buffer offset of the end of item exited. |
| 4293 | * |
| 4294 | * @returns QCBOR_SUCCESS or an error code. |
| 4295 | * |
| 4296 | * This is the common work for exiting a level that is a bounded map, |
| 4297 | * array or bstr wrapped CBOR. |
| 4298 | * |
| 4299 | * One chunk of work is to set up the pre-order traversal so it is at |
| 4300 | * the item just after the bounded map, array or bstr that is being |
| 4301 | * exited. This is somewhat complex. |
| 4302 | * |
| 4303 | * The other work is to level-up the bounded mode to next higest |
| 4304 | * bounded mode or the top level if there isn't one. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4305 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4306 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4307 | QCBORDecode_Private_ExitBoundedLevel(QCBORDecodeContext *pMe, |
| 4308 | const uint32_t uEndOffset) |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4309 | { |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4310 | QCBORError uErr; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4311 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4312 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4313 | * First the pre-order-traversal byte offset is positioned to the |
| 4314 | * item just after the bounded mode item that was just consumed. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4315 | */ |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4316 | UsefulInputBuf_Seek(&(pMe->InBuf), uEndOffset); |
| 4317 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4318 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4319 | * Next, set the current nesting level to one above the bounded |
| 4320 | * level that was just exited. |
| 4321 | * |
| 4322 | * DecodeNesting_CheckBoundedType() is always called before this |
| 4323 | * and makes sure pCurrentBounded is valid. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4324 | */ |
| 4325 | DecodeNesting_LevelUpCurrent(&(pMe->nesting)); |
| 4326 | |
| 4327 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4328 | * This does the complex work of leveling up the pre-order |
| 4329 | * traversal when the end of a map or array or another bounded |
| 4330 | * level is reached. It may do nothing, or ascend all the way to |
| 4331 | * the top level. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4332 | */ |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 4333 | uErr = QCBORDecode_Private_NestLevelAscender(pMe, NULL, false); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4334 | if(uErr != QCBOR_SUCCESS) { |
| 4335 | goto Done; |
| 4336 | } |
| 4337 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4338 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4339 | * This makes the next highest bounded level the current bounded |
| 4340 | * level. If there is no next highest level, then no bounded mode |
| 4341 | * is in effect. |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4342 | */ |
| 4343 | DecodeNesting_LevelUpBounded(&(pMe->nesting)); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4344 | |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 4345 | pMe->uMapEndOffsetCache = QCBOR_MAP_OFFSET_CACHE_INVALID; |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4346 | |
| 4347 | Done: |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4348 | return uErr; |
| 4349 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4350 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4351 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4352 | /** |
| 4353 | * @brief Get started exiting a map or array (semi-private) |
| 4354 | * |
| 4355 | * @param[in] pMe The decode context |
| 4356 | * @param[in] uType QCBOR_TYPE_ARRAY or QCBOR_TYPE_MAP |
| 4357 | * |
| 4358 | * This does some work for map and array exiting (but not |
| 4359 | * bstr exiting). Then QCBORDecode_Private_ExitBoundedLevel() |
| 4360 | * is called to do the rest. |
| 4361 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4362 | void |
| 4363 | QCBORDecode_Private_ExitBoundedMapOrArray(QCBORDecodeContext *pMe, |
| 4364 | const uint8_t uType) |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4365 | { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4366 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4367 | /* Already in error state; do nothing. */ |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4368 | return; |
| 4369 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4370 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4371 | QCBORError uErr; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4372 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 4373 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), uType)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4374 | uErr = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4375 | goto Done; |
| 4376 | } |
| 4377 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4378 | /* |
| 4379 | Have to set the offset to the end of the map/array |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4380 | that is being exited. If there is no cached value, |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4381 | from previous map search, then do a dummy search. |
| 4382 | */ |
Laurence Lundblade | bfbf494 | 2020-09-16 23:31:00 -0700 | [diff] [blame] | 4383 | if(pMe->uMapEndOffsetCache == QCBOR_MAP_OFFSET_CACHE_INVALID) { |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4384 | QCBORItem Dummy; |
| 4385 | Dummy.uLabelType = QCBOR_TYPE_NONE; |
Laurence Lundblade | a29f45a | 2024-05-14 15:55:19 -0700 | [diff] [blame] | 4386 | uErr = QCBORDecode_Private_MapSearch(pMe, &Dummy, NULL, NULL); |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4387 | if(uErr != QCBOR_SUCCESS) { |
| 4388 | goto Done; |
| 4389 | } |
| 4390 | } |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4391 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4392 | uErr = QCBORDecode_Private_ExitBoundedLevel(pMe, pMe->uMapEndOffsetCache); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4393 | |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4394 | Done: |
Laurence Lundblade | 2b843b5 | 2020-06-16 20:51:03 -0700 | [diff] [blame] | 4395 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | bb87be2 | 2020-04-09 19:15:32 -0700 | [diff] [blame] | 4396 | } |
| 4397 | |
| 4398 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4399 | // TODO: re order this file with tags stuff last. bstr is a tag thing |
| 4400 | static QCBORError |
| 4401 | QCBORDecode_Private_CheckTagNType(QCBORDecodeContext *pMe, |
| 4402 | const QCBORItem *pItem, |
| 4403 | const size_t uOffset, |
| 4404 | const uint8_t *uQCBORTypes, |
| 4405 | const uint64_t *uTagNumbers, |
| 4406 | const uint8_t uTagRequirement, |
| 4407 | bool *bTypeMatched); |
| 4408 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4409 | /** |
| 4410 | * @brief The main work of entering some byte-string wrapped CBOR. |
| 4411 | * |
| 4412 | * @param[in] pMe The decode context. |
| 4413 | * @param[in] pItem The byte string item. |
| 4414 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX |
| 4415 | * @param[out] pBstr Pointer and length of byte string entered. |
| 4416 | * |
| 4417 | * This is called once the byte string item has been decoded to do all |
| 4418 | * the book keeping work for descending a nesting level into the |
| 4419 | * nested CBOR. |
| 4420 | * |
| 4421 | * See QCBORDecode_EnterBstrWrapped() for details on uTagRequirement. |
| 4422 | */ |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 4423 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4424 | QCBORDecode_Private_EnterBstrWrapped(QCBORDecodeContext *pMe, |
| 4425 | const QCBORItem *pItem, |
| 4426 | const uint8_t uTagRequirement, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4427 | const size_t uOffset, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4428 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4429 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4430 | bool bTypeMatched; |
| 4431 | QCBORError uError; |
| 4432 | |
| 4433 | const uint8_t uTypes[] = {QBCOR_TYPE_WRAPPED_CBOR, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE, QCBOR_TYPE_NONE}; |
| 4434 | const uint64_t uTagNumbers[] = {CBOR_TAG_CBOR, CBOR_TAG_CBOR_SEQUENCE, CBOR_TAG_INVALID64}; |
| 4435 | |
| 4436 | |
Laurence Lundblade | c45b567 | 2020-07-25 23:16:36 -0700 | [diff] [blame] | 4437 | if(pBstr) { |
| 4438 | *pBstr = NULLUsefulBufC; |
| 4439 | } |
| 4440 | |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4441 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4442 | return pMe->uLastError; |
| 4443 | } |
| 4444 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4445 | if(pItem->uDataAlloc) { |
| 4446 | return QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4447 | } |
| 4448 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4449 | uError = QCBORDecode_Private_CheckTagNType(pMe, |
| 4450 | pItem, |
| 4451 | uOffset, |
| 4452 | uTypes, // TODO: maybe this should be empty |
| 4453 | uTagNumbers, |
| 4454 | uTagRequirement, |
| 4455 | &bTypeMatched); |
| 4456 | |
| 4457 | if(pItem->uDataType != QCBOR_TYPE_BYTE_STRING) { |
| 4458 | uError = QCBOR_ERR_BAD_TAG_CONTENT; // TODO: error |
| 4459 | } |
| 4460 | |
| 4461 | |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4462 | if(DecodeNesting_IsCurrentDefiniteLength(&(pMe->nesting))) { |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4463 | /* Reverse the decrement done by GetNext() for the bstr so the |
| 4464 | * increment in QCBORDecode_NestLevelAscender() called by |
| 4465 | * ExitBoundedLevel() will work right. |
| 4466 | */ |
Laurence Lundblade | a8edadb | 2020-06-27 22:35:37 -0700 | [diff] [blame] | 4467 | DecodeNesting_ReverseDecrement(&(pMe->nesting)); |
Laurence Lundblade | 6c8a444 | 2020-06-22 22:16:11 -0700 | [diff] [blame] | 4468 | } |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4469 | |
| 4470 | if(pBstr) { |
| 4471 | *pBstr = pItem->val.string; |
| 4472 | } |
| 4473 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4474 | /* This saves the current length of the UsefulInputBuf and then |
| 4475 | * narrows the UsefulInputBuf to start and length of the wrapped |
| 4476 | * CBOR that is being entered. |
| 4477 | * |
| 4478 | * Most of these calls are simple inline accessors so this doesn't |
| 4479 | * amount to much code. |
| 4480 | */ |
| 4481 | |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 4482 | const size_t uPreviousLength = UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4483 | /* This check makes the cast of uPreviousLength to uint32_t below safe. */ |
| 4484 | if(uPreviousLength >= QCBOR_MAX_DECODE_INPUT_SIZE) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4485 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
Laurence Lundblade | f76a262 | 2020-08-06 19:51:03 -0700 | [diff] [blame] | 4486 | goto Done; |
| 4487 | } |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4488 | |
| 4489 | const size_t uStartOfBstr = UsefulInputBuf_PointerToOffset(&(pMe->InBuf), |
| 4490 | pItem->val.string.ptr); |
| 4491 | /* This check makes the cast of uStartOfBstr to uint32_t below safe. */ |
| 4492 | if(uStartOfBstr == SIZE_MAX || uStartOfBstr > QCBOR_MAX_DECODE_INPUT_SIZE) { |
| 4493 | /* This should never happen because pItem->val.string.ptr should |
| 4494 | * always be valid since it was just returned. |
| 4495 | */ |
| 4496 | uError = QCBOR_ERR_INPUT_TOO_LARGE; |
| 4497 | goto Done; |
| 4498 | } |
| 4499 | |
| 4500 | const size_t uEndOfBstr = uStartOfBstr + pItem->val.string.len; |
| 4501 | |
| 4502 | UsefulInputBuf_Seek(&(pMe->InBuf), uStartOfBstr); |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 4503 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), uEndOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4504 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4505 | uError = DecodeNesting_DescendIntoBstrWrapped(&(pMe->nesting), |
Laurence Lundblade | a4308a8 | 2020-10-03 18:08:57 -0700 | [diff] [blame] | 4506 | (uint32_t)uPreviousLength, |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4507 | (uint32_t)uStartOfBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4508 | Done: |
| 4509 | return uError; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4510 | } |
| 4511 | |
| 4512 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4513 | static void |
| 4514 | QCBORDecode_Private_GetAndTell(QCBORDecodeContext *pMe, QCBORItem *Item, size_t *uOffset) |
| 4515 | { |
| 4516 | #ifndef QCBOR_DISABLE_TAGS |
| 4517 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4518 | return; |
| 4519 | } |
| 4520 | |
| 4521 | *uOffset = QCBORDecode_Tell(pMe); |
| 4522 | #else |
| 4523 | *uOffset = SIZE_MAX; |
| 4524 | |
| 4525 | #endif /* ! QCBOR_DISABLE_TAGS */ |
| 4526 | pMe->uLastError = (uint8_t)QCBORDecode_Private_GetNextTagContent(pMe, Item); |
| 4527 | } |
| 4528 | |
| 4529 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4530 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4531 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4532 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4533 | void |
| 4534 | QCBORDecode_EnterBstrWrapped(QCBORDecodeContext *pMe, |
| 4535 | const uint8_t uTagRequirement, |
| 4536 | UsefulBufC *pBstr) |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4537 | { |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4538 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4539 | size_t uOffset; |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4540 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4541 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4542 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4543 | &Item, |
| 4544 | uTagRequirement, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4545 | uOffset, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4546 | pBstr); |
Laurence Lundblade | 24d509a | 2020-06-06 18:43:15 -0700 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4550 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4551 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4552 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4553 | void |
| 4554 | QCBORDecode_EnterBstrWrappedFromMapN(QCBORDecodeContext *pMe, |
| 4555 | const int64_t nLabel, |
| 4556 | const uint8_t uTagRequirement, |
| 4557 | UsefulBufC *pBstr) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4558 | { |
| 4559 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4560 | size_t uOffset; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4561 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4562 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_BYTE_STRING, &Item, &uOffset); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4563 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4564 | &Item, |
| 4565 | uTagRequirement, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4566 | uOffset, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4567 | pBstr); |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4568 | } |
| 4569 | |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4570 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4571 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4572 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4573 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4574 | void |
| 4575 | QCBORDecode_EnterBstrWrappedFromMapSZ(QCBORDecodeContext *pMe, |
| 4576 | const char *szLabel, |
| 4577 | const uint8_t uTagRequirement, |
| 4578 | UsefulBufC *pBstr) |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4579 | { |
| 4580 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4581 | size_t uOffset; |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4582 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4583 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_BYTE_STRING, &Item, &uOffset); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4584 | pMe->uLastError = (uint8_t)QCBORDecode_Private_EnterBstrWrapped(pMe, |
| 4585 | &Item, |
| 4586 | uTagRequirement, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4587 | uOffset, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4588 | pBstr); |
Laurence Lundblade | aa965d7 | 2020-06-17 22:29:22 -0700 | [diff] [blame] | 4589 | } |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4590 | |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4591 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4592 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4593 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4594 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4595 | void |
| 4596 | QCBORDecode_ExitBstrWrapped(QCBORDecodeContext *pMe) |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4597 | { |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4598 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4599 | // Already in error state; do nothing. |
| 4600 | return; |
| 4601 | } |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4602 | |
Laurence Lundblade | 2cd4b0d | 2020-07-31 08:29:07 -0700 | [diff] [blame] | 4603 | if(!DecodeNesting_IsBoundedType(&(pMe->nesting), QCBOR_TYPE_BYTE_STRING)) { |
Laurence Lundblade | a9489f8 | 2020-09-12 13:50:56 -0700 | [diff] [blame] | 4604 | pMe->uLastError = QCBOR_ERR_EXIT_MISMATCH; |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4605 | return; |
| 4606 | } |
| 4607 | |
Laurence Lundblade | cf41c52 | 2021-02-20 10:19:07 -0700 | [diff] [blame] | 4608 | const uint32_t uEndOfBstr = (uint32_t)UsefulInputBuf_GetBufferLength(&(pMe->InBuf)); |
| 4609 | |
Laurence Lundblade | 02625d4 | 2020-06-25 14:41:41 -0700 | [diff] [blame] | 4610 | /* |
| 4611 | Reset the length of the UsefulInputBuf to what it was before |
| 4612 | the bstr wrapped CBOR was entered. |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4613 | */ |
Laurence Lundblade | 1ba100d | 2020-09-19 21:41:02 -0700 | [diff] [blame] | 4614 | UsefulInputBuf_SetBufferLength(&(pMe->InBuf), |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4615 | DecodeNesting_GetPreviousBoundedEnd(&(pMe->nesting))); |
Laurence Lundblade | 0750fc4 | 2020-06-20 21:02:34 -0700 | [diff] [blame] | 4616 | |
| 4617 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4618 | QCBORError uErr = QCBORDecode_Private_ExitBoundedLevel(pMe, uEndOfBstr); |
Laurence Lundblade | 1d85d52 | 2020-06-22 13:24:59 -0700 | [diff] [blame] | 4619 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d8c82c5 | 2020-06-12 22:15:52 -0700 | [diff] [blame] | 4620 | } |
| 4621 | |
Laurence Lundblade | bf3c42d | 2020-04-14 19:08:51 -0700 | [diff] [blame] | 4622 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4623 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4624 | /** |
| 4625 | * @brief Process simple type true and false, a boolean |
| 4626 | * |
| 4627 | * @param[in] pMe The decode context. |
| 4628 | * @param[in] pItem The item with either true or false. |
| 4629 | * @param[out] pBool The boolean value output. |
| 4630 | * |
| 4631 | * Sets the internal error if the item isn't a true or a false. Also |
| 4632 | * records any tag numbers as the tag numbers of the last item. |
| 4633 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4634 | static void |
| 4635 | QCBORDecode_Private_ProcessBool(QCBORDecodeContext *pMe, |
| 4636 | const QCBORItem *pItem, |
| 4637 | bool *pBool) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4638 | { |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4639 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4640 | /* Already in error state, do nothing */ |
| 4641 | return; |
| 4642 | } |
| 4643 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4644 | switch(pItem->uDataType) { |
| 4645 | case QCBOR_TYPE_TRUE: |
| 4646 | *pBool = true; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4647 | break; |
| 4648 | |
| 4649 | case QCBOR_TYPE_FALSE: |
| 4650 | *pBool = false; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4651 | break; |
| 4652 | |
| 4653 | default: |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4654 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4655 | break; |
| 4656 | } |
| 4657 | } |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4658 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4659 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4660 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4661 | * Public function, see header qcbor/qcbor_decode.h file |
| 4662 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4663 | void |
| 4664 | QCBORDecode_GetBool(QCBORDecodeContext *pMe, bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4665 | { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 4666 | QCBORItem Item; |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 4667 | QCBORDecode_VGetNext(pMe, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4668 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4669 | } |
| 4670 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4671 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4672 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4673 | * Public function, see header qcbor/qcbor_decode.h file |
| 4674 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4675 | void |
| 4676 | QCBORDecode_GetBoolInMapN(QCBORDecodeContext *pMe, |
| 4677 | const int64_t nLabel, |
| 4678 | bool *pValue) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4679 | { |
| 4680 | QCBORItem Item; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4681 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4682 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 4683 | } |
| 4684 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 4685 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 4686 | /* |
Laurence Lundblade | 9f9c373 | 2021-03-23 09:38:46 -0700 | [diff] [blame] | 4687 | * Public function, see header qcbor/qcbor_decode.h file |
| 4688 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4689 | void |
| 4690 | QCBORDecode_GetBoolInMapSZ(QCBORDecodeContext *pMe, |
| 4691 | const char *szLabel, |
| 4692 | bool *pValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4693 | { |
| 4694 | QCBORItem Item; |
| 4695 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 4696 | QCBORDecode_Private_ProcessBool(pMe, &Item, pValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4697 | } |
| 4698 | |
| 4699 | |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 4700 | /** |
| 4701 | * @brief Process simple values. |
| 4702 | * |
| 4703 | * @param[in] pMe The decode context. |
| 4704 | * @param[in] pItem The item with the simple value. |
| 4705 | * @param[out] puSimple The simple value output. |
| 4706 | * |
| 4707 | * Sets the internal error if the item isn't a true or a false. Also |
| 4708 | * records any tag numbers as the tag numbers of the last item. |
| 4709 | */ |
| 4710 | static void |
| 4711 | QCBORDecode_Private_ProcessSimple(QCBORDecodeContext *pMe, |
| 4712 | const QCBORItem *pItem, |
| 4713 | uint8_t *puSimple) |
| 4714 | { |
| 4715 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 4716 | return; |
| 4717 | } |
| 4718 | |
| 4719 | /* It's kind of lame to remap true...undef back to simple values, but |
| 4720 | * this function isn't used much and to not do it would require |
| 4721 | * changing GetNext() behavior in an incompatible way. |
| 4722 | */ |
| 4723 | switch(pItem->uDataType) { |
| 4724 | case QCBOR_TYPE_UKNOWN_SIMPLE: |
| 4725 | *puSimple = pItem->val.uSimple; |
| 4726 | break; |
| 4727 | |
| 4728 | case QCBOR_TYPE_TRUE: |
| 4729 | *puSimple = CBOR_SIMPLEV_TRUE; |
| 4730 | break; |
| 4731 | |
| 4732 | case QCBOR_TYPE_FALSE: |
| 4733 | *puSimple = CBOR_SIMPLEV_FALSE; |
| 4734 | break; |
| 4735 | |
| 4736 | case QCBOR_TYPE_NULL: |
| 4737 | *puSimple = CBOR_SIMPLEV_NULL; |
| 4738 | break; |
| 4739 | |
| 4740 | case QCBOR_TYPE_UNDEF: |
| 4741 | *puSimple = CBOR_SIMPLEV_UNDEF; |
| 4742 | break; |
| 4743 | |
| 4744 | default: |
| 4745 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 4746 | return; |
| 4747 | } |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | /* |
| 4751 | * Public function, see header qcbor/qcbor_decode.h file |
| 4752 | */ |
| 4753 | void |
| 4754 | QCBORDecode_GetSimple(QCBORDecodeContext *pMe, uint8_t *puSimple) |
| 4755 | { |
| 4756 | QCBORItem Item; |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 4757 | QCBORDecode_VGetNext(pMe, &Item); |
| 4758 | QCBORDecode_Private_ProcessSimple(pMe, &Item, puSimple); |
| 4759 | } |
| 4760 | |
| 4761 | /* |
| 4762 | * Public function, see header qcbor/qcbor_decode.h file |
| 4763 | */ |
| 4764 | void |
| 4765 | QCBORDecode_GetSimpleInMapN(QCBORDecodeContext *pMe, |
| 4766 | int64_t nLabel, |
| 4767 | uint8_t *puSimpleValue) |
| 4768 | { |
| 4769 | QCBORItem Item; |
| 4770 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 4771 | QCBORDecode_Private_ProcessSimple(pMe, &Item, puSimpleValue); |
| 4772 | } |
| 4773 | |
| 4774 | /* |
| 4775 | * Public function, see header qcbor/qcbor_decode.h file |
| 4776 | */ |
| 4777 | void |
| 4778 | QCBORDecode_GetSimpleInMapSZ(QCBORDecodeContext *pMe, |
| 4779 | const char *szLabel, |
| 4780 | uint8_t *puSimpleValue) |
| 4781 | { |
| 4782 | QCBORItem Item; |
| 4783 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item); |
Laurence Lundblade | 3888f00 | 2024-06-12 21:20:56 -0700 | [diff] [blame] | 4784 | QCBORDecode_Private_ProcessSimple(pMe, &Item, puSimpleValue); |
| 4785 | } |
| 4786 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 4787 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4788 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4789 | |
| 4790 | #ifndef QCBOR_DISABLE_TAGS |
| 4791 | // TODO: uTagNumber might be better a list than calling this multiple times |
| 4792 | static QCBORError |
| 4793 | QCBORDecode_Private_Check1TagNumber(const QCBORDecodeContext *pMe, |
| 4794 | const QCBORItem *pItem, |
| 4795 | const uint64_t uTagNumber, |
| 4796 | const size_t uOffset) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4797 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4798 | if(pItem->auTagNumbers[0] == CBOR_TAG_INVALID16) { |
| 4799 | /* There are no tag numbers at all, so no unprocessed */ |
| 4800 | return QCBOR_SUCCESS; |
| 4801 | } |
| 4802 | |
| 4803 | /* There are some tag numbers, so keep checking. This check passes |
| 4804 | * if there is one and only one tag number that matches uTagNumber |
| 4805 | */ |
| 4806 | |
| 4807 | // TODO: behave different in v1 and v2? |
| 4808 | |
| 4809 | const uint64_t uInnerTag = QCBORDecode_GetNthTagNumber(pMe, pItem, 0); |
| 4810 | |
| 4811 | if(uInnerTag == uTagNumber && pItem->auTagNumbers[1] == CBOR_TAG_INVALID16 ) { |
| 4812 | /* The only tag number is the one we are processing so no unprocessed */ |
| 4813 | return QCBOR_SUCCESS; |
| 4814 | } |
| 4815 | |
| 4816 | if(uOffset != pMe->uTagNumberCheckOffset) { |
| 4817 | /* processed tag numbers are for some other item, not us */ |
| 4818 | return QCBOR_ERR_UNPROCESSED_TAG_NUMBER; |
| 4819 | } |
| 4820 | |
| 4821 | if(pMe->uTagNumberIndex != 1) { |
| 4822 | return QCBOR_ERR_UNPROCESSED_TAG_NUMBER; |
| 4823 | } |
| 4824 | |
| 4825 | return QCBOR_SUCCESS; |
| 4826 | } |
| 4827 | #endif |
| 4828 | |
| 4829 | |
| 4830 | static QCBORError |
| 4831 | QCBORDecode_Private_CheckTagNType(QCBORDecodeContext *pMe, |
| 4832 | const QCBORItem *pItem, |
| 4833 | const size_t uOffset, |
| 4834 | const uint8_t *uQCBORTypes, |
| 4835 | const uint64_t *uTagNumbers, |
| 4836 | const uint8_t uTagRequirement, |
| 4837 | bool *bTypeMatched) |
| 4838 | { |
| 4839 | const int nTagReq = uTagRequirement & ~QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS; |
| 4840 | |
| 4841 | *bTypeMatched = false; |
| 4842 | for(const uint8_t *pTNum = uQCBORTypes; *pTNum != QCBOR_TYPE_NONE; pTNum++) { |
| 4843 | if(pItem->uDataType == *pTNum) { |
| 4844 | *bTypeMatched = true; |
| 4845 | break; |
| 4846 | } |
| 4847 | } |
| 4848 | |
| 4849 | #ifndef QCBOR_DISABLE_TAGS |
| 4850 | bool bTagNumberMatched; |
| 4851 | QCBORError uErr; |
| 4852 | const uint64_t uInnerTag = QCBORDecode_GetNthTagNumber(pMe, pItem, 0); |
| 4853 | |
| 4854 | bTagNumberMatched = false; |
| 4855 | for(const uint64_t *pQType = uTagNumbers; *pQType != CBOR_TAG_INVALID64; pQType++) { |
| 4856 | if(uInnerTag == *pQType) { |
| 4857 | bTagNumberMatched = true; |
| 4858 | break; |
| 4859 | } |
| 4860 | } |
| 4861 | |
| 4862 | |
| 4863 | if(nTagReq == QCBOR_TAG_REQUIREMENT_TAG) { |
| 4864 | /* There must be a tag number */ |
| 4865 | if(!bTagNumberMatched && !*bTypeMatched) { |
| 4866 | return QCBOR_ERR_UNEXPECTED_TYPE; // TODO: error code |
| 4867 | } |
| 4868 | |
| 4869 | } else if(nTagReq == QCBOR_TAG_REQUIREMENT_NOT_A_TAG) { |
| 4870 | if(bTagNumberMatched || *bTypeMatched) { |
| 4871 | return QCBOR_ERR_UNEXPECTED_TYPE; // TODO: error code |
| 4872 | } |
| 4873 | |
| 4874 | } else if(nTagReq == QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG) { |
| 4875 | /* No check necessary */ |
| 4876 | } |
| 4877 | |
| 4878 | /* Now check if there are extra tags and if there's an error in them */ |
| 4879 | if(!(uTagRequirement & QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS)) { |
| 4880 | /* The flag to ignore extra is not set, so keep checking */ |
| 4881 | for(const uint64_t *pTNum = uTagNumbers; *pTNum != CBOR_TAG_INVALID64; pTNum++) { |
| 4882 | uErr = QCBORDecode_Private_Check1TagNumber(pMe, pItem, *pTNum, uOffset); |
| 4883 | if(uErr != QCBOR_SUCCESS) { |
| 4884 | return uErr; |
| 4885 | } |
| 4886 | } |
| 4887 | } |
| 4888 | |
| 4889 | return QCBOR_SUCCESS; |
| 4890 | #else |
| 4891 | (void)pMe; |
| 4892 | (void)uOffset; |
| 4893 | (void)uTagNumbers; |
| 4894 | |
| 4895 | if(nTagReq != QCBOR_TAG_REQUIREMENT_TAG && bTypeMatched) { |
| 4896 | return QCBOR_SUCCESS; |
| 4897 | } else { |
| 4898 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 4899 | } |
| 4900 | |
| 4901 | #endif |
| 4902 | |
| 4903 | } |
| 4904 | |
| 4905 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 4906 | static void |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4907 | QCBORDecode_Private_ProcessTagItemMulti(QCBORDecodeContext *pMe, |
| 4908 | QCBORItem *pItem, |
| 4909 | const uint8_t uTagRequirement, |
| 4910 | const uint8_t uQCBORTypes[], |
| 4911 | const uint64_t uTagNumbers[], |
| 4912 | QCBORTagContentCallBack *pfCB, |
| 4913 | size_t uOffset) |
| 4914 | { |
| 4915 | QCBORError uErr; |
| 4916 | bool bTypeMatched; |
| 4917 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4918 | if(pMe->uLastError != QCBOR_SUCCESS) { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4919 | return; |
| 4920 | } |
| 4921 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4922 | uErr = QCBORDecode_Private_CheckTagNType(pMe, |
| 4923 | pItem, |
| 4924 | uOffset, |
| 4925 | uQCBORTypes, |
| 4926 | uTagNumbers, |
| 4927 | uTagRequirement, |
| 4928 | &bTypeMatched); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4929 | if(uErr != QCBOR_SUCCESS) { |
| 4930 | goto Done; |
| 4931 | } |
| 4932 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4933 | if(!bTypeMatched) { |
| 4934 | /* Tag content wasn't previously processed, do it now */ |
| 4935 | uErr = (*pfCB)(pMe, NULL, uTagNumbers[0], pItem); |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4936 | if(uErr != QCBOR_SUCCESS) { |
| 4937 | goto Done; |
| 4938 | } |
| 4939 | } |
| 4940 | |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 4941 | Done: |
| 4942 | pMe->uLastError = (uint8_t)uErr; |
| 4943 | } |
| 4944 | |
| 4945 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4946 | /* |
| 4947 | **/ |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 4948 | static void |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4949 | QCBORDecode_Private_ProcessTagItem(QCBORDecodeContext *pMe, |
| 4950 | QCBORItem *pItem, |
| 4951 | const uint8_t uTagRequirement, |
| 4952 | const uint8_t uQCBORTypes[], |
| 4953 | const uint64_t uTagNumber, |
| 4954 | QCBORTagContentCallBack *pfCB, |
| 4955 | size_t uOffset) |
| 4956 | { |
| 4957 | uint64_t auTagNumbers[2]; |
| 4958 | |
| 4959 | auTagNumbers[0] = uTagNumber; |
| 4960 | auTagNumbers[1] = CBOR_TAG_INVALID64; |
| 4961 | |
| 4962 | QCBORDecode_Private_ProcessTagItemMulti(pMe, |
| 4963 | pItem, |
| 4964 | uTagRequirement, |
| 4965 | uQCBORTypes, |
| 4966 | auTagNumbers, |
| 4967 | pfCB, |
| 4968 | uOffset); |
| 4969 | } |
| 4970 | |
| 4971 | |
| 4972 | static void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 4973 | QCBORDecode_Private_ProcessTagOne(QCBORDecodeContext *pMe, |
| 4974 | QCBORItem *pItem, |
| 4975 | const uint8_t uTagRequirement, |
| 4976 | const uint8_t uQCBORType, |
| 4977 | const uint64_t uTagNumber, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4978 | QCBORTagContentCallBack *pfCB, |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 4979 | const size_t uOffset) |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 4980 | { |
| 4981 | uint8_t auQCBORType[2]; |
| 4982 | |
| 4983 | auQCBORType[0] = uQCBORType; |
| 4984 | auQCBORType[1] = QCBOR_TYPE_NONE; |
| 4985 | |
| 4986 | QCBORDecode_Private_ProcessTagItem(pMe, |
| 4987 | pItem, |
| 4988 | uTagRequirement, |
| 4989 | auQCBORType, |
| 4990 | uTagNumber, |
| 4991 | pfCB, |
| 4992 | uOffset); |
| 4993 | } |
| 4994 | |
| 4995 | |
| 4996 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 4997 | |
| 4998 | /* |
| 4999 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 5000 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5001 | void |
| 5002 | QCBORDecode_GetEpochDate(QCBORDecodeContext *pMe, |
| 5003 | uint8_t uTagRequirement, |
| 5004 | int64_t *pnTime) |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 5005 | { |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 5006 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5007 | size_t uOffset; |
| 5008 | |
| 5009 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
| 5010 | QCBORDecode_Private_ProcessTagOne(pMe, |
| 5011 | &Item, |
| 5012 | uTagRequirement, |
| 5013 | QCBOR_TYPE_DATE_EPOCH, |
| 5014 | CBOR_TAG_DATE_EPOCH, |
| 5015 | QCBORDecode_DateEpochTagCB, |
| 5016 | uOffset); |
| 5017 | *pnTime = Item.val.epochDate.nSeconds; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 5018 | } |
| 5019 | |
| 5020 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5021 | /* |
| 5022 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 5023 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 5024 | void |
| 5025 | QCBORDecode_GetEpochDateInMapN(QCBORDecodeContext *pMe, |
| 5026 | int64_t nLabel, |
| 5027 | uint8_t uTagRequirement, |
| 5028 | int64_t *pnTime) |
| 5029 | { |
| 5030 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5031 | size_t uOffset; |
| 5032 | |
| 5033 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 5034 | QCBORDecode_Private_ProcessTagOne(pMe, |
| 5035 | &Item, |
| 5036 | uTagRequirement, |
| 5037 | QCBOR_TYPE_DATE_EPOCH, |
| 5038 | CBOR_TAG_DATE_EPOCH, |
| 5039 | QCBORDecode_DateEpochTagCB, |
| 5040 | uOffset); |
| 5041 | *pnTime = Item.val.epochDate.nSeconds; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 5042 | } |
| 5043 | |
| 5044 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5045 | /* |
| 5046 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 5047 | */ |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 5048 | void |
| 5049 | QCBORDecode_GetEpochDateInMapSZ(QCBORDecodeContext *pMe, |
| 5050 | const char *szLabel, |
| 5051 | uint8_t uTagRequirement, |
| 5052 | int64_t *pnTime) |
| 5053 | { |
| 5054 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5055 | size_t uOffset; |
Laurence Lundblade | c711472 | 2020-08-13 05:11:40 -0700 | [diff] [blame] | 5056 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5057 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 5058 | QCBORDecode_Private_ProcessTagOne(pMe, |
| 5059 | &Item, |
| 5060 | uTagRequirement, |
| 5061 | QCBOR_TYPE_DATE_EPOCH, |
| 5062 | CBOR_TAG_DATE_EPOCH, |
| 5063 | QCBORDecode_DateEpochTagCB, |
| 5064 | uOffset); |
| 5065 | *pnTime = Item.val.epochDate.nSeconds; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 5066 | } |
| 5067 | |
| 5068 | |
| 5069 | /* |
| 5070 | * Public function, see header qcbor/qcbor_decode.h |
| 5071 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5072 | void |
| 5073 | QCBORDecode_GetEpochDays(QCBORDecodeContext *pMe, |
| 5074 | uint8_t uTagRequirement, |
| 5075 | int64_t *pnDays) |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 5076 | { |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 5077 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5078 | size_t uOffset; |
| 5079 | |
| 5080 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
| 5081 | QCBORDecode_Private_ProcessTagOne(pMe, |
| 5082 | &Item, |
| 5083 | uTagRequirement, |
| 5084 | QCBOR_TYPE_DAYS_EPOCH, |
| 5085 | CBOR_TAG_DAYS_EPOCH, |
| 5086 | QCBORDecode_DaysEpochTagCB, |
| 5087 | uOffset); |
| 5088 | *pnDays = Item.val.epochDays; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 5089 | } |
| 5090 | |
| 5091 | |
| 5092 | /* |
| 5093 | * Public function, see header qcbor/qcbor_decode.h |
| 5094 | */ |
| 5095 | void |
| 5096 | QCBORDecode_GetEpochDaysInMapN(QCBORDecodeContext *pMe, |
| 5097 | int64_t nLabel, |
| 5098 | uint8_t uTagRequirement, |
| 5099 | int64_t *pnDays) |
| 5100 | { |
| 5101 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5102 | size_t uOffset; |
| 5103 | |
| 5104 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 5105 | QCBORDecode_Private_ProcessTagOne(pMe, |
| 5106 | &Item, |
| 5107 | uTagRequirement, |
| 5108 | QCBOR_TYPE_DAYS_EPOCH, |
| 5109 | CBOR_TAG_DAYS_EPOCH, |
| 5110 | QCBORDecode_DaysEpochTagCB, |
| 5111 | uOffset); |
| 5112 | *pnDays = Item.val.epochDays; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 5113 | } |
| 5114 | |
| 5115 | |
| 5116 | /* |
| 5117 | * Public function, see header qcbor/qcbor_decode.h |
| 5118 | */ |
| 5119 | void |
| 5120 | QCBORDecode_GetEpochDaysInMapSZ(QCBORDecodeContext *pMe, |
| 5121 | const char *szLabel, |
| 5122 | uint8_t uTagRequirement, |
| 5123 | int64_t *pnDays) |
| 5124 | { |
| 5125 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5126 | size_t uOffset; |
| 5127 | |
| 5128 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 5129 | QCBORDecode_Private_ProcessTagOne(pMe, |
| 5130 | &Item, |
| 5131 | uTagRequirement, |
| 5132 | QCBOR_TYPE_DAYS_EPOCH, |
| 5133 | CBOR_TAG_DAYS_EPOCH, |
| 5134 | QCBORDecode_DaysEpochTagCB, |
| 5135 | uOffset); |
| 5136 | *pnDays = Item.val.epochDays; |
Laurence Lundblade | 46d63e9 | 2021-05-13 11:37:10 -0700 | [diff] [blame] | 5137 | } |
| 5138 | |
| 5139 | |
| 5140 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5141 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 5142 | void |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5143 | QCBORDecode_Private_GetTaggedString(QCBORDecodeContext *pMe, |
| 5144 | const uint8_t uTagRequirement, |
| 5145 | const uint8_t uQCBOR_Type, |
| 5146 | const uint64_t uTagNumber, |
| 5147 | UsefulBufC *pStr) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5148 | { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5149 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5150 | size_t uOffset; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5151 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5152 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
| 5153 | QCBORDecode_Private_ProcessTagOne(pMe, |
| 5154 | &Item, |
| 5155 | uTagRequirement, |
| 5156 | uQCBOR_Type, |
| 5157 | uTagNumber, |
| 5158 | QCBORDecode_StringsTagCB, |
| 5159 | uOffset); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5160 | |
| 5161 | if(pMe->uLastError == QCBOR_SUCCESS) { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5162 | *pStr = Item.val.string; |
Laurence Lundblade | ab40c6f | 2020-08-28 11:24:58 -0700 | [diff] [blame] | 5163 | } else { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5164 | *pStr = NULLUsefulBufC; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5165 | } |
| 5166 | } |
| 5167 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5168 | |
| 5169 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5170 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5171 | /** |
| 5172 | * @brief Common processing for a big number tag. |
| 5173 | * |
| 5174 | * @param[in] uTagRequirement One of @c QCBOR_TAG_REQUIREMENT_XXX. |
| 5175 | * @param[in] pItem The item with the date. |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 5176 | * @param[out] pBignumber The returned big number |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5177 | * @param[out] pbIsNegative The returned sign of the big number. |
| 5178 | * |
| 5179 | * Common processing for the big number tag. Mostly make sure |
| 5180 | * the tag content is correct and copy forward any further other tag |
| 5181 | * numbers. |
| 5182 | */ |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5183 | static void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 5184 | QCBORDecode_Private_BigNumberRawMain(QCBORDecodeContext *pMe, |
| 5185 | const uint8_t uTagRequirement, |
| 5186 | QCBORItem *pItem, |
| 5187 | UsefulBufC *pBignumber, |
| 5188 | bool *pbIsNegative, |
| 5189 | size_t uOffset) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5190 | { |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 5191 | // TODO: refer to the static const ones instead |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5192 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5193 | const uint8_t puTypes[] = {QCBOR_TYPE_POSBIGNUM,QCBOR_TYPE_NEGBIGNUM, QCBOR_TYPE_NONE}; |
| 5194 | |
| 5195 | const uint64_t puTNs[] = {CBOR_TAG_POS_BIGNUM, CBOR_TAG_NEG_BIGNUM, CBOR_TAG_INVALID64}; |
| 5196 | |
| 5197 | QCBORDecode_Private_ProcessTagItemMulti(pMe, |
| 5198 | pItem, |
| 5199 | uTagRequirement, |
| 5200 | puTypes, |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 5201 | puTNs, |
| 5202 | QCBORDecode_StringsTagCB, |
| 5203 | uOffset); |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5204 | if(pMe->uLastError) { |
| 5205 | return; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5206 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5207 | |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5208 | if(pItem->uDataType == QCBOR_TYPE_POSBIGNUM) { |
| 5209 | *pbIsNegative = false; |
| 5210 | } else if(pItem->uDataType == QCBOR_TYPE_NEGBIGNUM) { |
| 5211 | *pbIsNegative = true; |
| 5212 | } |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 5213 | *pBignumber = pItem->val.bigNum; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | |
| 5217 | static void |
| 5218 | QCBORDecode_Private_GetMIME(QCBORDecodeContext *pMe, |
| 5219 | const uint8_t uTagRequirement, |
| 5220 | QCBORItem *pItem, |
| 5221 | UsefulBufC *pValue, |
| 5222 | bool *pbIsTag257, |
| 5223 | size_t uOffset) |
| 5224 | { |
| 5225 | QCBORError uErr; |
| 5226 | |
| 5227 | const uint8_t puTypes[] = {QCBOR_TYPE_MIME, QCBOR_TYPE_BINARY_MIME, QCBOR_TYPE_NONE}; |
| 5228 | |
| 5229 | const uint64_t puTNs[] = {CBOR_TAG_MIME, CBOR_TAG_BINARY_MIME, CBOR_TAG_INVALID64}; |
| 5230 | |
| 5231 | QCBORDecode_Private_ProcessTagItemMulti(pMe, |
| 5232 | pItem, |
| 5233 | uTagRequirement, |
| 5234 | puTypes, |
| 5235 | puTNs, |
| 5236 | QCBORDecode_MIMETagCB, |
| 5237 | uOffset); |
| 5238 | if(pMe->uLastError) { |
| 5239 | return; |
| 5240 | } |
| 5241 | |
| 5242 | if(pItem->uDataType == QCBOR_TYPE_MIME) { |
| 5243 | *pbIsTag257 = false; |
| 5244 | } else if(pItem->uDataType == QCBOR_TYPE_BINARY_MIME) { |
| 5245 | *pbIsTag257 = true; |
| 5246 | } |
| 5247 | *pValue = pItem->val.string; |
| 5248 | |
| 5249 | |
| 5250 | uErr = QCBOR_SUCCESS; |
| 5251 | |
| 5252 | pMe->uLastError = (uint8_t)uErr; |
| 5253 | } |
| 5254 | |
| 5255 | |
| 5256 | void |
| 5257 | QCBORDecode_GetMIMEMessage(QCBORDecodeContext *pMe, |
| 5258 | const uint8_t uTagRequirement, |
| 5259 | UsefulBufC *pMessage, |
| 5260 | bool *pbIsTag257) |
| 5261 | { |
| 5262 | QCBORItem Item; |
| 5263 | size_t uOffset; |
| 5264 | |
| 5265 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
| 5266 | QCBORDecode_Private_GetMIME(pMe, |
| 5267 | uTagRequirement, |
| 5268 | &Item, |
| 5269 | pMessage, |
| 5270 | pbIsTag257, |
| 5271 | uOffset); |
| 5272 | } |
| 5273 | |
| 5274 | void |
| 5275 | QCBORDecode_GetMIMEMessageInMapN(QCBORDecodeContext *pMe, |
| 5276 | const int64_t nLabel, |
| 5277 | const uint8_t uTagRequirement, |
| 5278 | UsefulBufC *pMessage, |
| 5279 | bool *pbIsTag257) |
| 5280 | { |
| 5281 | QCBORItem Item; |
| 5282 | size_t uOffset; |
| 5283 | |
| 5284 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 5285 | QCBORDecode_Private_GetMIME(pMe, |
| 5286 | uTagRequirement, |
| 5287 | &Item, |
| 5288 | pMessage, |
| 5289 | pbIsTag257, |
| 5290 | uOffset); |
| 5291 | } |
| 5292 | |
| 5293 | void |
| 5294 | QCBORDecode_GetMIMEMessageInMapSZ(QCBORDecodeContext *pMe, |
| 5295 | const char *szLabel, |
| 5296 | const uint8_t uTagRequirement, |
| 5297 | UsefulBufC *pMessage, |
| 5298 | bool *pbIsTag257) |
| 5299 | { |
| 5300 | QCBORItem Item; |
| 5301 | size_t uOffset; |
| 5302 | |
| 5303 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 5304 | QCBORDecode_Private_GetMIME(pMe, |
| 5305 | uTagRequirement, |
| 5306 | &Item, |
| 5307 | pMessage, |
| 5308 | pbIsTag257, |
| 5309 | uOffset); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5310 | } |
| 5311 | |
| 5312 | |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 5313 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5314 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5315 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5316 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 5317 | QCBORDecode_GetTBigNumberRaw(QCBORDecodeContext *pMe, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5318 | const uint8_t uTagRequirement, |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 5319 | UsefulBufC *pBignumber, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5320 | bool *pbIsNegative) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5321 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5322 | QCBORItem Item; |
| 5323 | size_t uOffset; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5324 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 5325 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
| 5326 | QCBORDecode_Private_BigNumberRawMain(pMe, |
| 5327 | uTagRequirement, |
| 5328 | &Item, |
| 5329 | pBignumber, |
| 5330 | pbIsNegative, |
| 5331 | uOffset); |
| 5332 | } |
| 5333 | |
| 5334 | /* |
| 5335 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
| 5336 | */ |
| 5337 | void |
| 5338 | QCBORDecode_GetTBigNumberRawInMapN(QCBORDecodeContext *pMe, |
| 5339 | const int64_t nLabel, |
| 5340 | const uint8_t uTagRequirement, |
| 5341 | UsefulBufC *pBigNumber, |
| 5342 | bool *pbIsNegative) |
| 5343 | { |
| 5344 | QCBORItem Item; |
| 5345 | size_t uOffset; |
| 5346 | |
| 5347 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 5348 | QCBORDecode_Private_BigNumberRawMain(pMe, |
| 5349 | uTagRequirement, |
| 5350 | &Item, |
| 5351 | pBigNumber, |
| 5352 | pbIsNegative, |
| 5353 | uOffset); |
| 5354 | } |
| 5355 | |
| 5356 | /* |
| 5357 | * Public function, see header qcbor/qcbor_spiffy_decode.h |
| 5358 | */ |
| 5359 | void |
| 5360 | QCBORDecode_GetTBigNumberRawInMapSZ(QCBORDecodeContext *pMe, |
| 5361 | const char *szLabel, |
| 5362 | const uint8_t uTagRequirement, |
| 5363 | UsefulBufC *pBigNumber, |
| 5364 | bool *pbIsNegative) |
| 5365 | { |
| 5366 | QCBORItem Item; |
| 5367 | size_t uOffset; |
| 5368 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 5369 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 5370 | QCBORDecode_Private_BigNumberRawMain(pMe, |
| 5371 | uTagRequirement, |
| 5372 | &Item, |
| 5373 | pBigNumber, |
| 5374 | pbIsNegative, |
| 5375 | uOffset); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5376 | } |
| 5377 | |
| 5378 | |
| 5379 | |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5380 | // Improvement: add methods for wrapped CBOR, a simple alternate |
| 5381 | // to EnterBstrWrapped |
Laurence Lundblade | 9bb039a | 2020-08-05 12:25:15 -0700 | [diff] [blame] | 5382 | |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 5383 | |
| 5384 | |
| 5385 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5386 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5387 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5388 | /** |
| 5389 | * @brief Prototype for conversion of exponent and mantissa to unsigned integer. |
| 5390 | * |
| 5391 | * @param[in] uMantissa The mantissa. |
| 5392 | * @param[in] nExponent The exponent. |
| 5393 | * @param[out] puResult The resulting integer. |
| 5394 | * |
| 5395 | * Concrete implementations of this are for exponent base 10 and 2 supporting |
| 5396 | * decimal fractions and big floats. |
| 5397 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5398 | typedef QCBORError (*fExponentiator)(uint64_t uMantissa, int64_t nExponent, uint64_t *puResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5399 | |
| 5400 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5401 | /** |
| 5402 | * @brief Base 10 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 5403 | * |
| 5404 | * @param[in] uMantissa The unsigned integer mantissa. |
| 5405 | * @param[in] nExponent The signed integer exponent. |
| 5406 | * @param[out] puResult Place to return the unsigned integer result. |
| 5407 | * |
| 5408 | * This computes: mantissa * 10 ^^ exponent as for a decimal fraction. The output is a 64-bit |
| 5409 | * unsigned integer. |
| 5410 | * |
| 5411 | * There are many inputs for which the result will not fit in the |
Laurence Lundblade | 88ba566 | 2024-11-03 02:10:24 -0800 | [diff] [blame] | 5412 | * 64-bit integer and @ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5413 | * be returned. |
| 5414 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5415 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5416 | QCBOR_Private_Exponentitate10(const uint64_t uMantissa, |
| 5417 | int64_t nExponent, |
| 5418 | uint64_t *puResult) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5419 | { |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5420 | uint64_t uResult = uMantissa; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5421 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5422 | if(uResult != 0) { |
| 5423 | /* This loop will run a maximum of 19 times because |
| 5424 | * UINT64_MAX < 10 ^^ 19. More than that will cause |
| 5425 | * exit with the overflow error |
| 5426 | */ |
| 5427 | for(; nExponent > 0; nExponent--) { |
| 5428 | if(uResult > UINT64_MAX / 10) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5429 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5430 | } |
| 5431 | uResult = uResult * 10; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5432 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5433 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5434 | for(; nExponent < 0; nExponent++) { |
| 5435 | uResult = uResult / 10; |
| 5436 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5437 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5438 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5439 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5440 | } |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5441 | /* else, mantissa is zero so this returns zero */ |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5442 | |
| 5443 | *puResult = uResult; |
| 5444 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5445 | return QCBOR_SUCCESS; |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5449 | /** |
| 5450 | * @brief Base 2 exponentiate a mantissa and exponent into an unsigned 64-bit integer. |
| 5451 | * |
| 5452 | * @param[in] uMantissa The unsigned integer mantissa. |
| 5453 | * @param[in] nExponent The signed integer exponent. |
| 5454 | * @param[out] puResult Place to return the unsigned integer result. |
| 5455 | * |
| 5456 | * This computes: mantissa * 2 ^^ exponent as for a big float. The |
| 5457 | * output is a 64-bit unsigned integer. |
| 5458 | * |
| 5459 | * There are many inputs for which the result will not fit in the |
Laurence Lundblade | 88ba566 | 2024-11-03 02:10:24 -0800 | [diff] [blame] | 5460 | * 64-bit integer and @ref QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW will |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5461 | * be returned. |
| 5462 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5463 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5464 | QCBOR_Private_Exponentitate2(const uint64_t uMantissa, |
| 5465 | int64_t nExponent, |
| 5466 | uint64_t *puResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5467 | { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5468 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5469 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5470 | uResult = uMantissa; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5471 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5472 | /* This loop will run a maximum of 64 times because INT64_MAX < |
| 5473 | * 2^31. More than that will cause exit with the overflow error |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5474 | */ |
| 5475 | while(nExponent > 0) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5476 | if(uResult > UINT64_MAX >> 1) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5477 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5478 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5479 | uResult = uResult << 1; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5480 | nExponent--; |
| 5481 | } |
| 5482 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5483 | while(nExponent < 0 ) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5484 | if(uResult == 0) { |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5485 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5486 | } |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5487 | uResult = uResult >> 1; |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5488 | nExponent++; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5489 | } |
| 5490 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5491 | *puResult = uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5492 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5493 | return QCBOR_SUCCESS; |
| 5494 | } |
| 5495 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5496 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5497 | /** |
| 5498 | * @brief Exponentiate a signed mantissa and signed exponent to produce a signed result. |
| 5499 | * |
| 5500 | * @param[in] nMantissa Signed integer mantissa. |
| 5501 | * @param[in] nExponent Signed integer exponent. |
| 5502 | * @param[out] pnResult Place to put the signed integer result. |
| 5503 | * @param[in] pfExp Exponentiation function. |
| 5504 | * |
| 5505 | * @returns Error code |
| 5506 | * |
Laurence Lundblade | 88ba566 | 2024-11-03 02:10:24 -0800 | [diff] [blame] | 5507 | * @c pfExp performs exponentiation on and unsigned mantissa and |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5508 | * produces an unsigned result. This converts the mantissa from signed |
| 5509 | * and converts the result to signed. The exponentiation function is |
| 5510 | * 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] | 5511 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5512 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5513 | QCBOR_Private_ExponentiateNN(const int64_t nMantissa, |
| 5514 | const int64_t nExponent, |
| 5515 | int64_t *pnResult, |
| 5516 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5517 | { |
| 5518 | uint64_t uResult; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5519 | uint64_t uMantissa; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5520 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5521 | /* Take the absolute value and put it into an unsigned. */ |
| 5522 | if(nMantissa >= 0) { |
| 5523 | /* Positive case is straightforward */ |
| 5524 | uMantissa = (uint64_t)nMantissa; |
| 5525 | } else if(nMantissa != INT64_MIN) { |
| 5526 | /* The common negative case. See next. */ |
| 5527 | uMantissa = (uint64_t)-nMantissa; |
| 5528 | } else { |
| 5529 | /* int64_t and uint64_t are always two's complement per the |
| 5530 | * C standard (and since QCBOR uses these it only works with |
| 5531 | * two's complement, which is pretty much universal these |
| 5532 | * days). The range of a negative two's complement integer is |
| 5533 | * one more that than a positive, so the simple code above might |
| 5534 | * not work all the time because you can't simply negate the |
| 5535 | * value INT64_MIN because it can't be represented in an |
| 5536 | * int64_t. -INT64_MIN can however be represented in a |
| 5537 | * uint64_t. Some compilers seem to recognize this case for the |
| 5538 | * above code and put the correct value in uMantissa, however |
| 5539 | * they are not required to do this by the C standard. This next |
| 5540 | * line does however work for all compilers. |
| 5541 | * |
| 5542 | * This does assume two's complement where -INT64_MIN == |
| 5543 | * INT64_MAX + 1 (which wouldn't be true for one's complement or |
| 5544 | * sign and magnitude (but we know we're using two's complement |
| 5545 | * because int64_t requires it)). |
| 5546 | * |
| 5547 | * See these, particularly the detailed commentary: |
| 5548 | * https://stackoverflow.com/questions/54915742/does-c99-mandate-a-int64-t-type-be-available-always |
| 5549 | * https://stackoverflow.com/questions/37301078/is-negating-int-min-undefined-behaviour |
| 5550 | */ |
| 5551 | uMantissa = (uint64_t)INT64_MAX+1; |
| 5552 | } |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5553 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5554 | /* Call the exponentiator passed for either base 2 or base 10. |
| 5555 | * Here is where most of the overflow errors are caught. */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5556 | QCBORError uReturn = (*pfExp)(uMantissa, nExponent, &uResult); |
| 5557 | if(uReturn) { |
| 5558 | return uReturn; |
| 5559 | } |
| 5560 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5561 | /* Convert back to the sign of the original mantissa */ |
| 5562 | if(nMantissa >= 0) { |
| 5563 | if(uResult > INT64_MAX) { |
| 5564 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5565 | } |
| 5566 | *pnResult = (int64_t)uResult; |
| 5567 | } else { |
| 5568 | /* (uint64_t)INT64_MAX+1 is used to represent the absolute value |
| 5569 | * of INT64_MIN. This assumes two's compliment representation |
| 5570 | * where INT64_MIN is one increment farther from 0 than |
| 5571 | * INT64_MAX. Trying to write -INT64_MIN doesn't work to get |
| 5572 | * this because the compiler makes it an int64_t which can't |
| 5573 | * represent -INT64_MIN. Also see above. |
| 5574 | */ |
| 5575 | if(uResult > (uint64_t)INT64_MAX+1) { |
| 5576 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5577 | } |
| 5578 | *pnResult = -(int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5579 | } |
| 5580 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5581 | return QCBOR_SUCCESS; |
| 5582 | } |
| 5583 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5584 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5585 | /** |
| 5586 | * @brief Exponentiate an unsigned mantissa and signed exponent to produce an unsigned result. |
| 5587 | * |
| 5588 | * @param[in] nMantissa Signed integer mantissa. |
| 5589 | * @param[in] nExponent Signed integer exponent. |
| 5590 | * @param[out] puResult Place to put the signed integer result. |
| 5591 | * @param[in] pfExp Exponentiation function. |
| 5592 | * |
| 5593 | * @returns Error code |
| 5594 | * |
Laurence Lundblade | 88ba566 | 2024-11-03 02:10:24 -0800 | [diff] [blame] | 5595 | * @c pfExp performs exponentiation on and unsigned mantissa and |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5596 | * produces an unsigned result. This errors out if the mantissa |
| 5597 | * is negative because the output is unsigned. |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 5598 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5599 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5600 | QCBOR_Private_ExponentitateNU(const int64_t nMantissa, |
| 5601 | const int64_t nExponent, |
| 5602 | uint64_t *puResult, |
| 5603 | fExponentiator pfExp) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5604 | { |
| 5605 | if(nMantissa < 0) { |
| 5606 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 5607 | } |
| 5608 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5609 | /* Cast to unsigned is OK because of check for negative. |
| 5610 | * Cast to unsigned is OK because UINT64_MAX > INT64_MAX. |
| 5611 | * Exponentiation is straight forward |
| 5612 | */ |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5613 | return (*pfExp)((uint64_t)nMantissa, nExponent, puResult); |
| 5614 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5615 | |
| 5616 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5617 | /** |
| 5618 | * @brief Exponentiate an usnigned mantissa and unsigned exponent to produce an unsigned result. |
| 5619 | * |
| 5620 | * @param[in] uMantissa Unsigned integer mantissa. |
| 5621 | * @param[in] nExponent Unsigned integer exponent. |
| 5622 | * @param[out] puResult Place to put the unsigned integer result. |
| 5623 | * @param[in] pfExp Exponentiation function. |
| 5624 | * |
| 5625 | * @returns Error code |
| 5626 | * |
Laurence Lundblade | 88ba566 | 2024-11-03 02:10:24 -0800 | [diff] [blame] | 5627 | * @c pfExp performs exponentiation on and unsigned mantissa and |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5628 | * produces an unsigned result so this is just a wrapper that does |
| 5629 | * nothing (and is likely inlined). |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5630 | */ |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5631 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5632 | QCBOR_Private_ExponentitateUU(const uint64_t uMantissa, |
| 5633 | const int64_t nExponent, |
| 5634 | uint64_t *puResult, |
| 5635 | fExponentiator pfExp) |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5636 | { |
| 5637 | return (*pfExp)(uMantissa, nExponent, puResult); |
| 5638 | } |
| 5639 | |
Laurence Lundblade | 6658c95 | 2024-11-14 05:04:37 -0800 | [diff] [blame^] | 5640 | #endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5641 | |
| 5642 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5643 | |
| 5644 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5645 | /** |
| 5646 | * @brief Convert a CBOR big number to a uint64_t. |
| 5647 | * |
| 5648 | * @param[in] BigNum Bytes of the big number to convert. |
| 5649 | * @param[in] uMax Maximum value allowed for the result. |
| 5650 | * @param[out] pResult Place to put the unsigned integer result. |
| 5651 | * |
| 5652 | * @returns Error code |
| 5653 | * |
| 5654 | * Many values will overflow because a big num can represent a much |
| 5655 | * larger range than uint64_t. |
| 5656 | */ |
| 5657 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5658 | QCBOR_Private_ConvertBigNumToUnsigned(const UsefulBufC BigNum, |
| 5659 | const uint64_t uMax, |
| 5660 | uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5661 | { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5662 | uint64_t uResult; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5663 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5664 | uResult = 0; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5665 | const uint8_t *pByte = BigNum.ptr; |
| 5666 | size_t uLen = BigNum.len; |
| 5667 | while(uLen--) { |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5668 | if(uResult > (uMax >> 8)) { |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 5669 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5670 | } |
Laurence Lundblade | 313b286 | 2020-05-16 01:23:06 -0700 | [diff] [blame] | 5671 | uResult = (uResult << 8) + *pByte++; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5672 | } |
| 5673 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5674 | *pResult = uResult; |
| 5675 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5676 | } |
| 5677 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5678 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5679 | /** |
| 5680 | * @brief Convert a CBOR postive big number to a uint64_t. |
| 5681 | * |
| 5682 | * @param[in] BigNum Bytes of the big number to convert. |
| 5683 | * @param[out] pResult Place to put the unsigned integer result. |
| 5684 | * |
| 5685 | * @returns Error code |
| 5686 | * |
| 5687 | * Many values will overflow because a big num can represent a much |
| 5688 | * larger range than uint64_t. |
| 5689 | */ |
| 5690 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5691 | QCBOR_Private_ConvertPositiveBigNumToUnsigned(const UsefulBufC BigNum, |
| 5692 | uint64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5693 | { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5694 | return QCBOR_Private_ConvertBigNumToUnsigned(BigNum, UINT64_MAX, pResult); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5695 | } |
| 5696 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5697 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5698 | /** |
| 5699 | * @brief Convert a CBOR positive big number to an int64_t. |
| 5700 | * |
| 5701 | * @param[in] BigNum Bytes of the big number to convert. |
| 5702 | * @param[out] pResult Place to put the signed integer result. |
| 5703 | * |
| 5704 | * @returns Error code |
| 5705 | * |
| 5706 | * Many values will overflow because a big num can represent a much |
| 5707 | * larger range than int64_t. |
| 5708 | */ |
| 5709 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5710 | QCBOR_Private_ConvertPositiveBigNumToSigned(const UsefulBufC BigNum, |
| 5711 | int64_t *pResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5712 | { |
| 5713 | uint64_t uResult; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5714 | QCBORError uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, |
| 5715 | INT64_MAX, |
| 5716 | &uResult); |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5717 | if(uError) { |
| 5718 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5719 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5720 | /* Cast is safe because ConvertBigNumToUnsigned limits to INT64_MAX */ |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5721 | *pResult = (int64_t)uResult; |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 5722 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5723 | } |
| 5724 | |
| 5725 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5726 | /** |
| 5727 | * @brief Convert a CBOR negative big number to an int64_t. |
| 5728 | * |
| 5729 | * @param[in] BigNum Bytes of the big number to convert. |
| 5730 | * @param[out] pnResult Place to put the signed integer result. |
| 5731 | * |
| 5732 | * @returns Error code |
| 5733 | * |
| 5734 | * Many values will overflow because a big num can represent a much |
| 5735 | * larger range than int64_t. |
| 5736 | */ |
| 5737 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5738 | QCBOR_Private_ConvertNegativeBigNumToSigned(const UsefulBufC BigNum, |
| 5739 | int64_t *pnResult) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5740 | { |
| 5741 | uint64_t uResult; |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 5742 | QCBORError uError; |
| 5743 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5744 | /* The negative integer furthest from zero for a C int64_t is |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5745 | * INT64_MIN which is expressed as -INT64_MAX - 1. The value of a |
| 5746 | * negative number in CBOR is computed as -n - 1 where n is the |
| 5747 | * encoded integer, where n is what is in the variable BigNum. When |
| 5748 | * converting BigNum to a uint64_t, the maximum value is thus |
| 5749 | * INT64_MAX, so that when it -n - 1 is applied to it the result |
| 5750 | * will never be further from 0 than INT64_MIN. |
| 5751 | * |
| 5752 | * -n - 1 <= INT64_MIN. |
| 5753 | * -n - 1 <= -INT64_MAX - 1 |
| 5754 | * n <= INT64_MAX. |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5755 | */ |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 5756 | uError = QCBOR_Private_ConvertBigNumToUnsigned(BigNum, INT64_MAX, &uResult); |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5757 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5758 | return uError; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5759 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5760 | |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 5761 | /* Now apply -n - 1. The cast is safe because |
| 5762 | * ConvertBigNumToUnsigned() is limited to INT64_MAX which does fit |
| 5763 | * is the largest positive integer that an int64_t can |
| 5764 | * represent. */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5765 | *pnResult = -(int64_t)uResult - 1; |
| 5766 | |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 5767 | return QCBOR_SUCCESS; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5768 | } |
| 5769 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5770 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5771 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 5772 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5773 | /** |
| 5774 | * @brief Convert integers and floats to an int64_t. |
| 5775 | * |
| 5776 | * @param[in] pItem The item to convert. |
| 5777 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5778 | * @param[out] pnValue The resulting converted value. |
| 5779 | * |
| 5780 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5781 | * in uConvertTypes. |
| 5782 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5783 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5784 | * or too small. |
| 5785 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5786 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5787 | QCBOR_Private_ConvertInt64(const QCBORItem *pItem, |
| 5788 | const uint32_t uConvertTypes, |
| 5789 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5790 | { |
| 5791 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5792 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5793 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5794 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5795 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5796 | /* https://pubs.opengroup.org/onlinepubs/009695399/functions/llround.html |
| 5797 | http://www.cplusplus.com/reference/cmath/llround/ |
| 5798 | */ |
| 5799 | // Not interested in FE_INEXACT |
| 5800 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5801 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 5802 | *pnValue = llround(pItem->val.dfnum); |
| 5803 | } else { |
| 5804 | *pnValue = lroundf(pItem->val.fnum); |
| 5805 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 5806 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 5807 | // llround() shouldn't result in divide by zero, but catch |
| 5808 | // it here in case it unexpectedly does. Don't try to |
| 5809 | // distinguish between the various exceptions because it seems |
| 5810 | // they vary by CPU, compiler and OS. |
| 5811 | return QCBOR_ERR_FLOAT_EXCEPTION; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5812 | } |
| 5813 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5814 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5815 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5816 | #else |
| 5817 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 5818 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5819 | break; |
| 5820 | |
| 5821 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5822 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5823 | *pnValue = pItem->val.int64; |
| 5824 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5825 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5826 | } |
| 5827 | break; |
| 5828 | |
| 5829 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5830 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5831 | if(pItem->val.uint64 < INT64_MAX) { |
| 5832 | *pnValue = pItem->val.int64; |
| 5833 | } else { |
| 5834 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5835 | } |
| 5836 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5837 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5838 | } |
| 5839 | break; |
| 5840 | |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 5841 | case QCBOR_TYPE_65BIT_NEG_INT: |
| 5842 | /* This type occurs if the value won't fit into int64_t |
| 5843 | * so this is always an error. */ |
| 5844 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 5845 | break; |
| 5846 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5847 | default: |
| 5848 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 5849 | } |
| 5850 | return QCBOR_SUCCESS; |
| 5851 | } |
| 5852 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5853 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5854 | /** |
| 5855 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5856 | * |
| 5857 | * @param[in] pMe The decode context. |
| 5858 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5859 | * @param[out] pnValue Result of the conversion. |
| 5860 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5861 | * |
| 5862 | * See QCBORDecode_GetInt64Convert(). |
| 5863 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5864 | void |
| 5865 | QCBORDecode_Private_GetInt64Convert(QCBORDecodeContext *pMe, |
| 5866 | uint32_t uConvertTypes, |
| 5867 | int64_t *pnValue, |
| 5868 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5869 | { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 5870 | QCBORDecode_VGetNext(pMe, pItem); |
| 5871 | if(pMe->uLastError) { |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5872 | return; |
| 5873 | } |
| 5874 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5875 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5876 | uConvertTypes, |
| 5877 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5878 | } |
| 5879 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5880 | /** |
| 5881 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5882 | * |
| 5883 | * @param[in] pMe The decode context. |
| 5884 | * @param[in] nLabel Label to find in map. |
| 5885 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5886 | * @param[out] pnValue Result of the conversion. |
| 5887 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5888 | * |
| 5889 | * See QCBORDecode_GetInt64ConvertInMapN(). |
| 5890 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5891 | void |
| 5892 | QCBORDecode_Private_GetInt64ConvertInMapN(QCBORDecodeContext *pMe, |
| 5893 | int64_t nLabel, |
| 5894 | uint32_t uConvertTypes, |
| 5895 | int64_t *pnValue, |
| 5896 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5897 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5898 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5899 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5900 | return; |
| 5901 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5902 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5903 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
| 5904 | uConvertTypes, |
| 5905 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5906 | } |
| 5907 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5908 | /** |
| 5909 | * @brief Almost-public method to decode a number and convert to int64_t (semi-private). |
| 5910 | * |
| 5911 | * @param[in] pMe The decode context. |
| 5912 | * @param[in] szLabel Label to find in map. |
| 5913 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5914 | * @param[out] pnValue Result of the conversion. |
| 5915 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 5916 | * |
| 5917 | * See QCBORDecode_GetInt64ConvertInMapSZ(). |
| 5918 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5919 | void |
| 5920 | QCBORDecode_Private_GetInt64ConvertInMapSZ(QCBORDecodeContext *pMe, |
| 5921 | const char * szLabel, |
| 5922 | uint32_t uConvertTypes, |
| 5923 | int64_t *pnValue, |
| 5924 | QCBORItem *pItem) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5925 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 5926 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 5927 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 5928 | return; |
| 5929 | } |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5930 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5931 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertInt64(pItem, |
| 5932 | uConvertTypes, |
| 5933 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5934 | } |
| 5935 | |
| 5936 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 5937 | /** |
| 5938 | * @brief Convert many number types to an int64_t. |
| 5939 | * |
| 5940 | * @param[in] pItem The item to convert. |
| 5941 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 5942 | * @param[out] pnValue The resulting converted value. |
| 5943 | * |
| 5944 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 5945 | * in uConvertTypes. |
| 5946 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 5947 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 5948 | * or too small. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 5949 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 5950 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5951 | QCBOR_Private_Int64ConvertAll(const QCBORItem *pItem, |
| 5952 | const uint32_t uConvertTypes, |
| 5953 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5954 | { |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5955 | switch(pItem->uDataType) { |
| 5956 | |
| 5957 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5958 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5959 | return QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5960 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5961 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5962 | } |
| 5963 | break; |
| 5964 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5965 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5966 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5967 | return QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.bigNum, pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5968 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5969 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5970 | } |
| 5971 | break; |
| 5972 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 5973 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5974 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5975 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5976 | return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5977 | pItem->val.expAndMantissa.nExponent, |
| 5978 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5979 | &QCBOR_Private_Exponentitate10); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 5980 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5981 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5982 | } |
| 5983 | break; |
| 5984 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5985 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5986 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5987 | return QCBOR_Private_ExponentiateNN(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 5988 | pItem->val.expAndMantissa.nExponent, |
| 5989 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 5990 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5991 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5992 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5993 | } |
| 5994 | break; |
| 5995 | |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 5996 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 5997 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 5998 | int64_t nMantissa; |
| 5999 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6000 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6001 | if(uErr) { |
| 6002 | return uErr; |
| 6003 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6004 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6005 | pItem->val.expAndMantissa.nExponent, |
| 6006 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6007 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6008 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6009 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6010 | } |
| 6011 | break; |
| 6012 | |
| 6013 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6014 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6015 | int64_t nMantissa; |
| 6016 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6017 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6018 | if(uErr) { |
| 6019 | return uErr; |
| 6020 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6021 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6022 | pItem->val.expAndMantissa.nExponent, |
| 6023 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6024 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6025 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6026 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6027 | } |
| 6028 | break; |
| 6029 | |
| 6030 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6031 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6032 | int64_t nMantissa; |
| 6033 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6034 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6035 | if(uErr) { |
| 6036 | return uErr; |
| 6037 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6038 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6039 | pItem->val.expAndMantissa.nExponent, |
| 6040 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6041 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6042 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6043 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6044 | } |
| 6045 | break; |
| 6046 | |
| 6047 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6048 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6049 | int64_t nMantissa; |
| 6050 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6051 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, &nMantissa); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6052 | if(uErr) { |
| 6053 | return uErr; |
| 6054 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6055 | return QCBOR_Private_ExponentiateNN(nMantissa, |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6056 | pItem->val.expAndMantissa.nExponent, |
| 6057 | pnValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6058 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6059 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6060 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6061 | } |
| 6062 | break; |
Laurence Lundblade | 6658c95 | 2024-11-14 05:04:37 -0800 | [diff] [blame^] | 6063 | #endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6064 | |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6065 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6066 | default: |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6067 | return QCBOR_ERR_UNEXPECTED_TYPE; } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6068 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6069 | |
| 6070 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6071 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6072 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6073 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6074 | void |
| 6075 | QCBORDecode_GetInt64ConvertAll(QCBORDecodeContext *pMe, |
| 6076 | const uint32_t uConvertTypes, |
| 6077 | int64_t *pnValue) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6078 | { |
| 6079 | QCBORItem Item; |
| 6080 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6081 | QCBORDecode_Private_GetInt64Convert(pMe, uConvertTypes, pnValue, &Item); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6082 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6083 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6084 | // The above conversion succeeded |
| 6085 | return; |
| 6086 | } |
| 6087 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6088 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6089 | // 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] | 6090 | return; |
| 6091 | } |
| 6092 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6093 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 6094 | uConvertTypes, |
| 6095 | pnValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6096 | } |
| 6097 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6098 | |
| 6099 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6100 | * Public function, see header qcbor/qcbor_decode.h file |
| 6101 | */ |
| 6102 | void |
| 6103 | QCBORDecode_GetInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 6104 | const int64_t nLabel, |
| 6105 | const uint32_t uConvertTypes, |
| 6106 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6107 | { |
| 6108 | QCBORItem Item; |
| 6109 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6110 | QCBORDecode_Private_GetInt64ConvertInMapN(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6111 | nLabel, |
| 6112 | uConvertTypes, |
| 6113 | pnValue, |
| 6114 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6115 | |
| 6116 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6117 | // The above conversion succeeded |
| 6118 | return; |
| 6119 | } |
| 6120 | |
| 6121 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6122 | // The above conversion failed in a way that code below can't correct |
| 6123 | return; |
| 6124 | } |
| 6125 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6126 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 6127 | uConvertTypes, |
| 6128 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6129 | } |
| 6130 | |
| 6131 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6132 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6133 | * Public function, see header qcbor/qcbor_decode.h file |
| 6134 | */ |
| 6135 | void |
| 6136 | QCBORDecode_GetInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 6137 | const char *szLabel, |
| 6138 | const uint32_t uConvertTypes, |
| 6139 | int64_t *pnValue) |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6140 | { |
| 6141 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6142 | QCBORDecode_Private_GetInt64ConvertInMapSZ(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6143 | szLabel, |
| 6144 | uConvertTypes, |
| 6145 | pnValue, |
| 6146 | &Item); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6147 | |
| 6148 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6149 | // The above conversion succeeded |
| 6150 | return; |
| 6151 | } |
| 6152 | |
| 6153 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6154 | // The above conversion failed in a way that code below can't correct |
| 6155 | return; |
| 6156 | } |
| 6157 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6158 | pMe->uLastError = (uint8_t)QCBOR_Private_Int64ConvertAll(&Item, |
| 6159 | uConvertTypes, |
| 6160 | pnValue); |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6164 | /** |
| 6165 | * @brief Convert many number types to an uint64_t. |
| 6166 | * |
| 6167 | * @param[in] pItem The item to convert. |
| 6168 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 6169 | * @param[out] puValue The resulting converted value. |
| 6170 | * |
| 6171 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 6172 | * in uConvertTypes. |
| 6173 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 6174 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 6175 | * or too small. |
| 6176 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6177 | static QCBORError |
| 6178 | QCBOR_Private_ConvertUInt64(const QCBORItem *pItem, |
| 6179 | const uint32_t uConvertTypes, |
| 6180 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6181 | { |
| 6182 | switch(pItem->uDataType) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6183 | case QCBOR_TYPE_DOUBLE: |
| 6184 | case QCBOR_TYPE_FLOAT: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6185 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6186 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6187 | // Can't use llround here because it will not convert values |
| 6188 | // greater than INT64_MAX and less than UINT64_MAX that |
| 6189 | // need to be converted so it is more complicated. |
| 6190 | feclearexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO); |
| 6191 | if(pItem->uDataType == QCBOR_TYPE_DOUBLE) { |
| 6192 | if(isnan(pItem->val.dfnum)) { |
| 6193 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 6194 | } else if(pItem->val.dfnum < 0) { |
| 6195 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 6196 | } else { |
| 6197 | double dRounded = round(pItem->val.dfnum); |
| 6198 | // See discussion in DecodeDateEpoch() for |
| 6199 | // explanation of - 0x7ff |
| 6200 | if(dRounded > (double)(UINT64_MAX- 0x7ff)) { |
| 6201 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 6202 | } |
| 6203 | *puValue = (uint64_t)dRounded; |
| 6204 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6205 | } else { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6206 | if(isnan(pItem->val.fnum)) { |
| 6207 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 6208 | } else if(pItem->val.fnum < 0) { |
| 6209 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 6210 | } else { |
| 6211 | float fRounded = roundf(pItem->val.fnum); |
| 6212 | // See discussion in DecodeDateEpoch() for |
| 6213 | // explanation of - 0x7ff |
| 6214 | if(fRounded > (float)(UINT64_MAX- 0x7ff)) { |
| 6215 | return QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 6216 | } |
| 6217 | *puValue = (uint64_t)fRounded; |
| 6218 | } |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6219 | } |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6220 | if(fetestexcept(FE_INVALID|FE_OVERFLOW|FE_UNDERFLOW|FE_DIVBYZERO)) { |
| 6221 | // round() and roundf() shouldn't result in exceptions here, but |
| 6222 | // catch them to be robust and thorough. Don't try to |
| 6223 | // distinguish between the various exceptions because it seems |
| 6224 | // they vary by CPU, compiler and OS. |
| 6225 | return QCBOR_ERR_FLOAT_EXCEPTION; |
| 6226 | } |
| 6227 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6228 | } else { |
| 6229 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6230 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6231 | #else |
| 6232 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 6233 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6234 | break; |
Laurence Lundblade | 843a10c | 2020-05-23 13:57:00 -0700 | [diff] [blame] | 6235 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6236 | case QCBOR_TYPE_INT64: |
| 6237 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
| 6238 | if(pItem->val.int64 >= 0) { |
| 6239 | *puValue = (uint64_t)pItem->val.int64; |
| 6240 | } else { |
| 6241 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 6242 | } |
| 6243 | } else { |
| 6244 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6245 | } |
| 6246 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6247 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6248 | case QCBOR_TYPE_UINT64: |
| 6249 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 6250 | *puValue = pItem->val.uint64; |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6251 | } else { |
| 6252 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6253 | } |
| 6254 | break; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6255 | |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 6256 | case QCBOR_TYPE_65BIT_NEG_INT: |
| 6257 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 6258 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6259 | default: |
| 6260 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6261 | } |
| 6262 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6263 | return QCBOR_SUCCESS; |
| 6264 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6265 | |
| 6266 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6267 | /** |
| 6268 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 6269 | * |
| 6270 | * @param[in] pMe The decode context. |
| 6271 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 6272 | * @param[out] puValue Result of the conversion. |
| 6273 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6274 | * |
| 6275 | * See QCBORDecode_GetUInt64Convert(). |
| 6276 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6277 | void |
| 6278 | QCBORDecode_Private_GetUInt64Convert(QCBORDecodeContext *pMe, |
| 6279 | const uint32_t uConvertTypes, |
| 6280 | uint64_t *puValue, |
| 6281 | QCBORItem *pItem) |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6282 | { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 6283 | QCBORDecode_VGetNext(pMe, pItem); |
| 6284 | if(pMe->uLastError) { |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6285 | return; |
| 6286 | } |
| 6287 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 6288 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6289 | uConvertTypes, |
| 6290 | puValue); |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6291 | } |
| 6292 | |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6293 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6294 | /** |
| 6295 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 6296 | * |
| 6297 | * @param[in] pMe The decode context. |
| 6298 | * @param[in] nLabel Label to find in map. |
| 6299 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 6300 | * @param[out] puValue Result of the conversion. |
| 6301 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6302 | * |
| 6303 | * See QCBORDecode_GetUInt64ConvertInMapN(). |
| 6304 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6305 | void |
| 6306 | QCBORDecode_Private_GetUInt64ConvertInMapN(QCBORDecodeContext *pMe, |
| 6307 | const int64_t nLabel, |
| 6308 | const uint32_t uConvertTypes, |
| 6309 | uint64_t *puValue, |
| 6310 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6311 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6312 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6313 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6314 | return; |
| 6315 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6316 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6317 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem, |
| 6318 | uConvertTypes, |
| 6319 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6320 | } |
| 6321 | |
| 6322 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6323 | /** |
| 6324 | * @brief Almost-public method to decode a number and convert to uint64_t (semi-private). |
| 6325 | * |
| 6326 | * @param[in] pMe The decode context. |
| 6327 | * @param[in] szLabel Label to find in map. |
| 6328 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 6329 | * @param[out] puValue Result of the conversion. |
| 6330 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6331 | * |
| 6332 | * See QCBORDecode_GetUInt64ConvertInMapSZ(). |
| 6333 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6334 | void |
| 6335 | QCBORDecode_Private_GetUInt64ConvertInMapSZ(QCBORDecodeContext *pMe, |
| 6336 | const char *szLabel, |
| 6337 | const uint32_t uConvertTypes, |
| 6338 | uint64_t *puValue, |
| 6339 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6340 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6341 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6342 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6343 | return; |
| 6344 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6345 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6346 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertUInt64(pItem, |
| 6347 | uConvertTypes, |
| 6348 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6349 | } |
| 6350 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6351 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6352 | /** |
| 6353 | * @brief Convert many number types to an unt64_t. |
| 6354 | * |
| 6355 | * @param[in] pItem The item to convert. |
| 6356 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 6357 | * @param[out] puValue The resulting converted value. |
| 6358 | * |
| 6359 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 6360 | * in uConvertTypes. |
| 6361 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 6362 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 6363 | * or too small. |
| 6364 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6365 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6366 | QCBOR_Private_UInt64ConvertAll(const QCBORItem *pItem, |
| 6367 | const uint32_t uConvertTypes, |
| 6368 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6369 | { |
Laurence Lundblade | 68cb61c | 2023-02-12 13:21:44 -0800 | [diff] [blame] | 6370 | switch(pItem->uDataType) { /* -Wmaybe-uninitialized falsly warns here */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6371 | |
| 6372 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6373 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6374 | return QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.bigNum, puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6375 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6376 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6377 | } |
| 6378 | break; |
| 6379 | |
| 6380 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6381 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6382 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 6383 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6384 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6385 | } |
| 6386 | break; |
| 6387 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6388 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6389 | |
| 6390 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6391 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6392 | return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6393 | pItem->val.expAndMantissa.nExponent, |
| 6394 | puValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6395 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6396 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6397 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6398 | } |
| 6399 | break; |
| 6400 | |
| 6401 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6402 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6403 | return QCBOR_Private_ExponentitateNU(pItem->val.expAndMantissa.Mantissa.nInt, |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6404 | pItem->val.expAndMantissa.nExponent, |
| 6405 | puValue, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6406 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6407 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6408 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6409 | } |
| 6410 | break; |
| 6411 | |
| 6412 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6413 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6414 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6415 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6416 | uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6417 | if(uErr != QCBOR_SUCCESS) { |
| 6418 | return uErr; |
| 6419 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6420 | return QCBOR_Private_ExponentitateUU(uMantissa, |
| 6421 | pItem->val.expAndMantissa.nExponent, |
| 6422 | puValue, |
| 6423 | QCBOR_Private_Exponentitate10); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6424 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6425 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6426 | } |
| 6427 | break; |
| 6428 | |
| 6429 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6430 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6431 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 6432 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6433 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6434 | } |
| 6435 | break; |
| 6436 | |
| 6437 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6438 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6439 | uint64_t uMantissa; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6440 | QCBORError uErr; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6441 | uErr = QCBOR_Private_ConvertPositiveBigNumToUnsigned(pItem->val.expAndMantissa.Mantissa.bigNum, |
| 6442 | &uMantissa); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6443 | if(uErr != QCBOR_SUCCESS) { |
| 6444 | return uErr; |
| 6445 | } |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6446 | return QCBOR_Private_ExponentitateUU(uMantissa, |
| 6447 | pItem->val.expAndMantissa.nExponent, |
| 6448 | puValue, |
| 6449 | QCBOR_Private_Exponentitate2); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6450 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6451 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6452 | } |
| 6453 | break; |
| 6454 | |
| 6455 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6456 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6457 | return QCBOR_ERR_NUMBER_SIGN_CONVERSION; |
| 6458 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6459 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6460 | } |
| 6461 | break; |
Laurence Lundblade | 6658c95 | 2024-11-14 05:04:37 -0800 | [diff] [blame^] | 6462 | #endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6463 | default: |
| 6464 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6465 | } |
| 6466 | } |
| 6467 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6468 | |
Laurence Lundblade | 4e2da00 | 2020-06-13 23:08:31 -0700 | [diff] [blame] | 6469 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6470 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6471 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6472 | void |
| 6473 | QCBORDecode_GetUInt64ConvertAll(QCBORDecodeContext *pMe, |
| 6474 | const uint32_t uConvertTypes, |
| 6475 | uint64_t *puValue) |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6476 | { |
| 6477 | QCBORItem Item; |
| 6478 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6479 | QCBORDecode_Private_GetUInt64Convert(pMe, uConvertTypes, puValue, &Item); |
Laurence Lundblade | 9c905e8 | 2020-04-25 11:31:38 -0700 | [diff] [blame] | 6480 | |
Laurence Lundblade | f6c8666 | 2020-05-12 02:08:00 -0700 | [diff] [blame] | 6481 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6482 | // The above conversion succeeded |
| 6483 | return; |
| 6484 | } |
| 6485 | |
| 6486 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6487 | // 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] | 6488 | return; |
| 6489 | } |
| 6490 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6491 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 6492 | uConvertTypes, |
| 6493 | puValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6494 | } |
| 6495 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6496 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6497 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6498 | * Public function, see header qcbor/qcbor_decode.h file |
| 6499 | */ |
| 6500 | void |
| 6501 | QCBORDecode_GetUInt64ConvertAllInMapN(QCBORDecodeContext *pMe, |
| 6502 | const int64_t nLabel, |
| 6503 | const uint32_t uConvertTypes, |
| 6504 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6505 | { |
| 6506 | QCBORItem Item; |
| 6507 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6508 | QCBORDecode_Private_GetUInt64ConvertInMapN(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6509 | nLabel, |
| 6510 | uConvertTypes, |
| 6511 | puValue, |
| 6512 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6513 | |
| 6514 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6515 | // The above conversion succeeded |
| 6516 | return; |
| 6517 | } |
| 6518 | |
| 6519 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6520 | // The above conversion failed in a way that code below can't correct |
| 6521 | return; |
| 6522 | } |
| 6523 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6524 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 6525 | uConvertTypes, |
| 6526 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6527 | } |
| 6528 | |
| 6529 | |
| 6530 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6531 | * Public function, see header qcbor/qcbor_decode.h file |
| 6532 | */ |
| 6533 | void |
| 6534 | QCBORDecode_GetUInt64ConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 6535 | const char *szLabel, |
| 6536 | const uint32_t uConvertTypes, |
| 6537 | uint64_t *puValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6538 | { |
| 6539 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6540 | QCBORDecode_Private_GetUInt64ConvertInMapSZ(pMe, |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6541 | szLabel, |
| 6542 | uConvertTypes, |
| 6543 | puValue, |
| 6544 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6545 | |
| 6546 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6547 | // The above conversion succeeded |
| 6548 | return; |
| 6549 | } |
| 6550 | |
| 6551 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6552 | // The above conversion failed in a way that code below can't correct |
| 6553 | return; |
| 6554 | } |
| 6555 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6556 | pMe->uLastError = (uint8_t)QCBOR_Private_UInt64ConvertAll(&Item, |
| 6557 | uConvertTypes, |
| 6558 | puValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6559 | } |
| 6560 | |
| 6561 | |
Laurence Lundblade | f7a70bc | 2020-10-24 12:23:25 -0700 | [diff] [blame] | 6562 | |
| 6563 | |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 6564 | #ifndef USEFULBUF_DISABLE_ALL_FLOAT |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6565 | /** |
| 6566 | * @brief Basic conversions to a double. |
| 6567 | * |
| 6568 | * @param[in] pItem The item to convert |
| 6569 | * @param[in] uConvertTypes Bit flags indicating source types for conversion |
| 6570 | * @param[out] pdValue The value converted to a double |
| 6571 | * |
| 6572 | * This does the conversions that don't need much object code, |
| 6573 | * the conversions from int, uint and float to double. |
| 6574 | * |
| 6575 | * See QCBOR_Private_DoubleConvertAll() for the full set |
| 6576 | * of conversions. |
| 6577 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6578 | static QCBORError |
| 6579 | QCBOR_Private_ConvertDouble(const QCBORItem *pItem, |
| 6580 | const uint32_t uConvertTypes, |
| 6581 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6582 | { |
| 6583 | switch(pItem->uDataType) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6584 | case QCBOR_TYPE_FLOAT: |
| 6585 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
| 6586 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 6587 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6588 | // Simple cast does the job. |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6589 | *pdValue = (double)pItem->val.fnum; |
| 6590 | } else { |
| 6591 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6592 | } |
| 6593 | } |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6594 | #else /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6595 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
Laurence Lundblade | e2c893c | 2020-12-26 17:41:53 -0800 | [diff] [blame] | 6596 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6597 | break; |
| 6598 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6599 | case QCBOR_TYPE_DOUBLE: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6600 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
| 6601 | if(uConvertTypes & QCBOR_CONVERT_TYPE_FLOAT) { |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6602 | *pdValue = pItem->val.dfnum; |
| 6603 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6604 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6605 | } |
| 6606 | } |
| 6607 | break; |
| 6608 | |
| 6609 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6610 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6611 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6612 | // A simple cast seems to do the job with no worry of exceptions. |
| 6613 | // There will be precision loss for some values. |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6614 | *pdValue = (double)pItem->val.int64; |
| 6615 | |
| 6616 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6617 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6618 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6619 | #else |
| 6620 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 6621 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6622 | break; |
| 6623 | |
| 6624 | case QCBOR_TYPE_UINT64: |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6625 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6626 | if(uConvertTypes & QCBOR_CONVERT_TYPE_XINT64) { |
Laurence Lundblade | dfd49fc | 2020-09-01 14:17:16 -0700 | [diff] [blame] | 6627 | // A simple cast seems to do the job with no worry of exceptions. |
| 6628 | // There will be precision loss for some values. |
| 6629 | *pdValue = (double)pItem->val.uint64; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6630 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6631 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6632 | } |
| 6633 | break; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6634 | #else |
| 6635 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 6636 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6637 | |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 6638 | case QCBOR_TYPE_65BIT_NEG_INT: |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 6639 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 6640 | // TODO: don't use float HW. We have the function to do it. |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 6641 | *pdValue = -(double)pItem->val.uint64 - 1; |
| 6642 | break; |
Laurence Lundblade | eb3cdef | 2024-02-17 20:38:55 -0800 | [diff] [blame] | 6643 | #else |
| 6644 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 6645 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
Laurence Lundblade | 2d49300 | 2024-02-01 11:09:17 -0700 | [diff] [blame] | 6646 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6647 | default: |
| 6648 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6649 | } |
| 6650 | |
| 6651 | return QCBOR_SUCCESS; |
| 6652 | } |
| 6653 | |
| 6654 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6655 | /** |
| 6656 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 6657 | * |
| 6658 | * @param[in] pMe The decode context. |
| 6659 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 6660 | * @param[out] pdValue The output of the conversion. |
| 6661 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6662 | * |
| 6663 | * See QCBORDecode_GetDoubleConvert(). |
| 6664 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6665 | void |
| 6666 | QCBORDecode_Private_GetDoubleConvert(QCBORDecodeContext *pMe, |
| 6667 | const uint32_t uConvertTypes, |
| 6668 | double *pdValue, |
| 6669 | QCBORItem *pItem) |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6670 | { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 6671 | QCBORDecode_VGetNext(pMe, pItem); |
| 6672 | if(pMe->uLastError) { |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6673 | return; |
| 6674 | } |
| 6675 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6676 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6677 | uConvertTypes, |
| 6678 | pdValue); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6679 | } |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6680 | |
Laurence Lundblade | c453744 | 2020-04-14 18:53:22 -0700 | [diff] [blame] | 6681 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6682 | /** |
| 6683 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 6684 | * |
| 6685 | * @param[in] pMe The decode context. |
| 6686 | * @param[in] nLabel Label to find in map. |
| 6687 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 6688 | * @param[out] pdValue The output of the conversion. |
| 6689 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6690 | * |
| 6691 | * See QCBORDecode_GetDoubleConvertInMapN(). |
| 6692 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6693 | void |
| 6694 | QCBORDecode_Private_GetDoubleConvertInMapN(QCBORDecodeContext *pMe, |
| 6695 | const int64_t nLabel, |
| 6696 | const uint32_t uConvertTypes, |
| 6697 | double *pdValue, |
| 6698 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6699 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6700 | QCBORDecode_GetItemInMapN(pMe, nLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6701 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6702 | return; |
| 6703 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6704 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6705 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
| 6706 | uConvertTypes, |
| 6707 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6708 | } |
| 6709 | |
Laurence Lundblade | 784b54b | 2020-08-10 01:24:52 -0700 | [diff] [blame] | 6710 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6711 | /** |
| 6712 | * @brief Almost-public method to decode a number and convert to double (semi-private). |
| 6713 | * |
| 6714 | * @param[in] pMe The decode context. |
| 6715 | * @param[in] szLabel Label to find in map. |
| 6716 | * @param[in] uConvertTypes Bit mask list of conversion options |
| 6717 | * @param[out] pdValue The output of the conversion. |
| 6718 | * @param[in,out] pItem Temporary space to store Item, returned item. |
| 6719 | * |
| 6720 | * See QCBORDecode_GetDoubleConvertInMapSZ(). |
| 6721 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6722 | void |
| 6723 | QCBORDecode_Private_GetDoubleConvertInMapSZ(QCBORDecodeContext *pMe, |
| 6724 | const char *szLabel, |
| 6725 | const uint32_t uConvertTypes, |
| 6726 | double *pdValue, |
| 6727 | QCBORItem *pItem) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6728 | { |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 6729 | QCBORDecode_GetItemInMapSZ(pMe, szLabel, QCBOR_TYPE_ANY, pItem); |
Laurence Lundblade | abf5c57 | 2020-06-29 21:21:29 -0700 | [diff] [blame] | 6730 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 6731 | return; |
| 6732 | } |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6733 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6734 | pMe->uLastError = (uint8_t)QCBOR_Private_ConvertDouble(pItem, |
| 6735 | uConvertTypes, |
| 6736 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6737 | } |
| 6738 | |
| 6739 | |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6740 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6741 | /** |
| 6742 | * @brief Convert a big number to double-precision float. |
| 6743 | * |
| 6744 | * @param[in] BigNum The big number to convert |
| 6745 | * |
| 6746 | * @returns The double value. |
| 6747 | * |
| 6748 | * This will always succeed. It will lose precision for larger |
| 6749 | * numbers. If the big number is too large to fit (more than |
| 6750 | * 1.7976931348623157E+308) infinity will be returned. NaN is never |
| 6751 | * returned. |
| 6752 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6753 | static double |
| 6754 | QCBOR_Private_ConvertBigNumToDouble(const UsefulBufC BigNum) |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6755 | { |
| 6756 | double dResult; |
| 6757 | |
| 6758 | dResult = 0.0; |
| 6759 | const uint8_t *pByte = BigNum.ptr; |
| 6760 | size_t uLen = BigNum.len; |
| 6761 | /* This will overflow and become the float value INFINITY if the number |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6762 | * is too large to fit. */ |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6763 | while(uLen--) { |
| 6764 | dResult = (dResult * 256.0) + (double)*pByte++; |
| 6765 | } |
| 6766 | |
| 6767 | return dResult; |
| 6768 | } |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6769 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 6770 | |
Laurence Lundblade | 9ab5abb | 2020-05-20 12:10:45 -0700 | [diff] [blame] | 6771 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6772 | |
| 6773 | |
| 6774 | /** |
| 6775 | * @brief Convert many number types to a double. |
| 6776 | * |
| 6777 | * @param[in] pItem The item to convert. |
| 6778 | * @param[in] uConvertTypes Bit mask list of conversion options. |
| 6779 | * @param[out] pdValue The resulting converted value. |
| 6780 | * |
| 6781 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Conversion, possible, but not requested |
| 6782 | * in uConvertTypes. |
| 6783 | * @retval QCBOR_ERR_UNEXPECTED_TYPE Of a type that can't be converted |
| 6784 | * @retval QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW Conversion result is too large |
| 6785 | * or too small. |
| 6786 | */ |
Laurence Lundblade | 93d8947 | 2020-10-03 22:30:50 -0700 | [diff] [blame] | 6787 | static QCBORError |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6788 | QCBOR_Private_DoubleConvertAll(const QCBORItem *pItem, |
| 6789 | const uint32_t uConvertTypes, |
| 6790 | double *pdValue) |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6791 | { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6792 | #ifndef QCBOR_DISABLE_FLOAT_HW_USE |
Laurence Lundblade | 54cd99c | 2020-05-15 02:25:32 -0700 | [diff] [blame] | 6793 | /* |
Laurence Lundblade | 16a207a | 2021-09-18 17:22:46 -0700 | [diff] [blame] | 6794 | * What Every Computer Scientist Should Know About Floating-Point Arithmetic |
| 6795 | * https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 6796 | */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6797 | switch(pItem->uDataType) { |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 6798 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6799 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6800 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6801 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6802 | // Underflow gives 0, overflow gives infinity |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6803 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 6804 | pow(10.0, (double)pItem->val.expAndMantissa.nExponent); |
| 6805 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6806 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6807 | } |
| 6808 | break; |
| 6809 | |
| 6810 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6811 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT ) { |
Laurence Lundblade | 51722fd | 2020-09-02 13:01:33 -0700 | [diff] [blame] | 6812 | // Underflow gives 0, overflow gives infinity |
| 6813 | *pdValue = (double)pItem->val.expAndMantissa.Mantissa.nInt * |
| 6814 | exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6815 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6816 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6817 | } |
| 6818 | break; |
Laurence Lundblade | 20fba4f | 2024-10-30 07:52:33 -0700 | [diff] [blame] | 6819 | #endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6820 | |
| 6821 | case QCBOR_TYPE_POSBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6822 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6823 | *pdValue = QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6824 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6825 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6826 | } |
| 6827 | break; |
| 6828 | |
| 6829 | case QCBOR_TYPE_NEGBIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6830 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIG_NUM) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6831 | *pdValue = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6832 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6833 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6834 | } |
| 6835 | break; |
| 6836 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6837 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6838 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6839 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6840 | double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6841 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
| 6842 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6843 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6844 | } |
| 6845 | break; |
| 6846 | |
| 6847 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | 20fba4f | 2024-10-30 07:52:33 -0700 | [diff] [blame] | 6848 | if(uConvertTypes & QCBOR_CONVERT_TYPE_DECIMAL_FRACTION) { |
| 6849 | /* Must subtract 1 for CBOR negative integer offset */ |
| 6850 | double dMantissa = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 6851 | *pdValue = dMantissa * pow(10, (double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6852 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6853 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6854 | } |
| 6855 | break; |
| 6856 | |
| 6857 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | 20fba4f | 2024-10-30 07:52:33 -0700 | [diff] [blame] | 6858 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
| 6859 | double dMantissa = QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 6860 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6861 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6862 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6863 | } |
| 6864 | break; |
| 6865 | |
| 6866 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | 20fba4f | 2024-10-30 07:52:33 -0700 | [diff] [blame] | 6867 | if(uConvertTypes & QCBOR_CONVERT_TYPE_BIGFLOAT) { |
| 6868 | double dMantissa = -1-QCBOR_Private_ConvertBigNumToDouble(pItem->val.expAndMantissa.Mantissa.bigNum); |
| 6869 | *pdValue = dMantissa * exp2((double)pItem->val.expAndMantissa.nExponent); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6870 | } else { |
Laurence Lundblade | 78f7b93 | 2020-07-28 20:02:25 -0700 | [diff] [blame] | 6871 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6872 | } |
| 6873 | break; |
Laurence Lundblade | 20fba4f | 2024-10-30 07:52:33 -0700 | [diff] [blame] | 6874 | #endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | 410c7e0 | 2020-06-25 23:35:29 -0700 | [diff] [blame] | 6875 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6876 | default: |
| 6877 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 6878 | } |
| 6879 | |
| 6880 | return QCBOR_SUCCESS; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 6881 | |
| 6882 | #else |
| 6883 | (void)pItem; |
| 6884 | (void)uConvertTypes; |
| 6885 | (void)pdValue; |
| 6886 | return QCBOR_ERR_HW_FLOAT_DISABLED; |
| 6887 | #endif /* QCBOR_DISABLE_FLOAT_HW_USE */ |
| 6888 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6889 | } |
| 6890 | |
| 6891 | |
| 6892 | /* |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6893 | * Public function, see header qcbor/qcbor_decode.h file |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6894 | */ |
| 6895 | void |
| 6896 | QCBORDecode_GetDoubleConvertAll(QCBORDecodeContext *pMe, |
| 6897 | const uint32_t uConvertTypes, |
| 6898 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6899 | { |
| 6900 | |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6901 | QCBORItem Item; |
| 6902 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6903 | QCBORDecode_Private_GetDoubleConvert(pMe, uConvertTypes, pdValue, &Item); |
Laurence Lundblade | b340ba7 | 2020-05-14 11:41:10 -0700 | [diff] [blame] | 6904 | |
| 6905 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6906 | // The above conversion succeeded |
| 6907 | return; |
| 6908 | } |
| 6909 | |
| 6910 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6911 | // The above conversion failed in a way that code below can't correct |
| 6912 | return; |
| 6913 | } |
| 6914 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6915 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6916 | uConvertTypes, |
| 6917 | pdValue); |
Laurence Lundblade | e643064 | 2020-03-14 21:15:44 -0700 | [diff] [blame] | 6918 | } |
| 6919 | |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6920 | |
| 6921 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6922 | * Public function, see header qcbor/qcbor_decode.h file |
| 6923 | */ |
| 6924 | void |
| 6925 | QCBORDecode_GetDoubleConvertAllInMapN(QCBORDecodeContext *pMe, |
| 6926 | const int64_t nLabel, |
| 6927 | const uint32_t uConvertTypes, |
| 6928 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6929 | { |
| 6930 | QCBORItem Item; |
| 6931 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6932 | QCBORDecode_Private_GetDoubleConvertInMapN(pMe, |
| 6933 | nLabel, |
| 6934 | uConvertTypes, |
| 6935 | pdValue, |
| 6936 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6937 | |
| 6938 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6939 | // The above conversion succeeded |
| 6940 | return; |
| 6941 | } |
| 6942 | |
| 6943 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6944 | // The above conversion failed in a way that code below can't correct |
| 6945 | return; |
| 6946 | } |
| 6947 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6948 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6949 | uConvertTypes, |
| 6950 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6951 | } |
| 6952 | |
| 6953 | |
| 6954 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6955 | * Public function, see header qcbor/qcbor_decode.h file |
| 6956 | */ |
| 6957 | void |
| 6958 | QCBORDecode_GetDoubleConvertAllInMapSZ(QCBORDecodeContext *pMe, |
| 6959 | const char *szLabel, |
| 6960 | const uint32_t uConvertTypes, |
| 6961 | double *pdValue) |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6962 | { |
| 6963 | QCBORItem Item; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6964 | QCBORDecode_Private_GetDoubleConvertInMapSZ(pMe, |
| 6965 | szLabel, |
| 6966 | uConvertTypes, |
| 6967 | pdValue, |
| 6968 | &Item); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6969 | |
| 6970 | if(pMe->uLastError == QCBOR_SUCCESS) { |
| 6971 | // The above conversion succeeded |
| 6972 | return; |
| 6973 | } |
| 6974 | |
| 6975 | if(pMe->uLastError != QCBOR_ERR_UNEXPECTED_TYPE) { |
| 6976 | // The above conversion failed in a way that code below can't correct |
| 6977 | return; |
| 6978 | } |
| 6979 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 6980 | pMe->uLastError = (uint8_t)QCBOR_Private_DoubleConvertAll(&Item, |
| 6981 | uConvertTypes, |
| 6982 | pdValue); |
Laurence Lundblade | 7e5be1d | 2020-05-24 21:17:28 -0700 | [diff] [blame] | 6983 | } |
Máté Tóth-Pál | ef5f07a | 2021-09-17 19:31:37 +0200 | [diff] [blame] | 6984 | #endif /* USEFULBUF_DISABLE_ALL_FLOAT */ |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 6985 | |
| 6986 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 6987 | |
| 6988 | |
Laurence Lundblade | dd6e76e | 2021-03-10 01:54:01 -0700 | [diff] [blame] | 6989 | #ifndef QCBOR_DISABLE_EXP_AND_MANTISSA |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 6990 | /** |
| 6991 | * @brief Convert an integer to a big number |
| 6992 | * |
| 6993 | * @param[in] uInt The integer to convert. |
| 6994 | * @param[in] Buffer The buffer to output the big number to. |
| 6995 | * |
| 6996 | * @returns The big number or NULLUsefulBufC is the buffer is to small. |
| 6997 | * |
| 6998 | * This always succeeds unless the buffer is too small. |
| 6999 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7000 | static UsefulBufC |
| 7001 | QCBOR_Private_ConvertIntToBigNum(uint64_t uInt, const UsefulBuf Buffer) |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 7002 | { |
| 7003 | while((uInt & 0xff00000000000000UL) == 0) { |
| 7004 | uInt = uInt << 8; |
| 7005 | }; |
| 7006 | |
| 7007 | UsefulOutBuf UOB; |
| 7008 | |
| 7009 | UsefulOutBuf_Init(&UOB, Buffer); |
| 7010 | |
| 7011 | while(uInt) { |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 7012 | UsefulOutBuf_AppendByte(&UOB, (uint8_t)((uInt & 0xff00000000000000UL) >> 56)); |
| 7013 | uInt = uInt << 8; |
Laurence Lundblade | f7c0adb | 2020-08-08 20:20:58 -0700 | [diff] [blame] | 7014 | } |
| 7015 | |
| 7016 | return UsefulOutBuf_OutUBuf(&UOB); |
| 7017 | } |
| 7018 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7019 | /* Some notes from the work to disable tags. |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7020 | * Some are out of date since tag refactoring. |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7021 | * |
| 7022 | * The API for big floats and decimal fractions seems good. |
| 7023 | * If there's any issue with it it's that the code size to |
| 7024 | * implement is a bit large because of the conversion |
| 7025 | * to/from int and bignum that is required. There is no API |
| 7026 | * that doesn't do the conversion so dead stripping will never |
| 7027 | * leave that code out. |
| 7028 | * |
| 7029 | * The implementation itself seems correct, but not as clean |
| 7030 | * and neat as it could be. It could probably be smaller too. |
| 7031 | * |
| 7032 | * The implementation has three main parts / functions |
| 7033 | * - The decoding of the array of two |
| 7034 | * - All the tag and type checking for the various API functions |
| 7035 | * - Conversion to/from bignum and int |
| 7036 | * |
| 7037 | * The type checking seems like it wastes the most code for |
| 7038 | * what it needs to do. |
| 7039 | * |
| 7040 | * The inlining for the conversion is probably making the |
| 7041 | * overall code base larger. |
| 7042 | * |
| 7043 | * The tests cases could be organized a lot better and be |
| 7044 | * more thorough. |
| 7045 | * |
| 7046 | * Seems also like there could be more common code in the |
| 7047 | * first tier part of the public API. Some functions only |
| 7048 | * vary by a TagSpec. |
| 7049 | */ |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7050 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7051 | |
| 7052 | static const uint8_t QCBORDecode_Private_DecimalFractionTypes[] = { |
| 7053 | QCBOR_TYPE_DECIMAL_FRACTION, |
| 7054 | QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM, |
| 7055 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM, |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7056 | QCBOR_TYPE_DECIMAL_FRACTION_POS_U64, |
| 7057 | QCBOR_TYPE_DECIMAL_FRACTION_NEG_U64, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7058 | QCBOR_TYPE_NONE}; |
| 7059 | |
| 7060 | static const uint8_t QCBORDecode_Private_BigFloatTypes[] = { |
| 7061 | QCBOR_TYPE_BIGFLOAT, |
| 7062 | QCBOR_TYPE_BIGFLOAT_POS_BIGNUM, |
| 7063 | QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM, |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7064 | QCBOR_TYPE_BIGFLOAT_POS_U64, |
| 7065 | QCBOR_TYPE_BIGFLOAT_NEG_U64, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7066 | QCBOR_TYPE_NONE}; |
| 7067 | |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7068 | /** |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7069 | * @brief Common processor for exponent and int64_t mantissa. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7070 | * |
| 7071 | * @param[in] pMe The decode context. |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7072 | * @param[in] uTagRequirement Whether tag number must be present or not. |
| 7073 | * @param[in] uTagNumber The tag number for which content is expected. |
| 7074 | * @param[in] uOffset Cursor offset for tag number consumption checking. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7075 | * @param[in] pItem The data item to process. |
| 7076 | * @param[out] pnMantissa The returned mantissa as an int64_t. |
| 7077 | * @param[out] pnExponent The returned exponent as an int64_t. |
| 7078 | * |
| 7079 | * This handles exponent and mantissa for base 2 and 10. This |
| 7080 | * is limited to a mantissa that is an int64_t. See also |
| 7081 | * QCBORDecode_Private_ProcessExpMantissaBig(). |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7082 | * |
| 7083 | * On output, the item is always a fully decoded decimal fraction or |
| 7084 | * big float. |
| 7085 | * |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7086 | * This errors out if the input tag and type aren't as required. |
| 7087 | * |
| 7088 | * This always provides the correctly offset mantissa, even when the |
| 7089 | * input CBOR is a negative big number. This works the |
| 7090 | * same in QCBOR v1 and v2. |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7091 | */ |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7092 | static void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7093 | QCBORDecode_Private_ExpIntMantissaMain(QCBORDecodeContext *pMe, |
| 7094 | const uint8_t uTagRequirement, |
| 7095 | const uint64_t uTagNumber, |
| 7096 | const size_t uOffset, |
| 7097 | QCBORItem *pItem, |
| 7098 | int64_t *pnMantissa, |
| 7099 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7100 | { |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7101 | QCBORError uErr; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7102 | const uint8_t *qTypes; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7103 | |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 7104 | if(pMe->uLastError) { |
| 7105 | return; |
| 7106 | } |
| 7107 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7108 | if(uTagNumber == CBOR_TAG_BIGFLOAT) { |
| 7109 | qTypes = QCBORDecode_Private_BigFloatTypes; |
| 7110 | } else { |
| 7111 | qTypes = QCBORDecode_Private_DecimalFractionTypes; |
| 7112 | } |
| 7113 | |
| 7114 | QCBORDecode_Private_ProcessTagItem(pMe, |
| 7115 | pItem, |
| 7116 | uTagRequirement, |
| 7117 | qTypes, |
| 7118 | uTagNumber, |
| 7119 | QCBORDecode_ExpMantissaTagCB, |
| 7120 | uOffset); |
| 7121 | |
| 7122 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7123 | return; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7124 | } |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7125 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7126 | uErr = QCBOR_SUCCESS; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7127 | switch (pItem->uDataType) { |
| 7128 | |
| 7129 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 7130 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7131 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7132 | *pnMantissa = pItem->val.expAndMantissa.Mantissa.nInt; |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7133 | break; |
| 7134 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7135 | #ifndef QCBOR_DISABLE_TAGS |
| 7136 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7137 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 7138 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 7139 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7140 | uErr = QCBOR_Private_ConvertPositiveBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7141 | break; |
| 7142 | |
| 7143 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 7144 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 7145 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7146 | uErr = QCBOR_Private_ConvertNegativeBigNumToSigned(pItem->val.expAndMantissa.Mantissa.bigNum, pnMantissa); |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7147 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7148 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7149 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7150 | case QCBOR_TYPE_BIGFLOAT_NEG_U64: |
| 7151 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_U64: |
| 7152 | case QCBOR_TYPE_BIGFLOAT_POS_U64: |
| 7153 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_U64: |
| 7154 | uErr = QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW; |
| 7155 | break; |
| 7156 | |
Laurence Lundblade | 9b33496 | 2020-08-27 10:55:53 -0700 | [diff] [blame] | 7157 | default: |
| 7158 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
| 7159 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7160 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7161 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7162 | } |
| 7163 | |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7164 | static void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7165 | QCBORDecode_Private_ExpBigMantissaRawMain(QCBORDecodeContext *pMe, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7166 | const uint8_t uTagRequirement, |
| 7167 | const uint64_t uTagNumber, |
| 7168 | const size_t uOffset, |
| 7169 | QCBORItem *pItem, |
| 7170 | const UsefulBuf BufferForMantissa, |
| 7171 | UsefulBufC *pMantissa, |
| 7172 | bool *pbIsNegative, |
| 7173 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7174 | { |
Laurence Lundblade | 033574f | 2024-11-03 17:42:35 -0800 | [diff] [blame] | 7175 | QCBORError uErr; |
| 7176 | uint64_t uMantissa; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7177 | const uint8_t *qTypes; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7178 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7179 | if(pMe->uLastError) { |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 7180 | return; |
| 7181 | } |
| 7182 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7183 | if(uTagNumber == CBOR_TAG_BIGFLOAT) { |
| 7184 | qTypes = QCBORDecode_Private_BigFloatTypes; |
| 7185 | } else { |
| 7186 | qTypes = QCBORDecode_Private_DecimalFractionTypes; |
| 7187 | } |
| 7188 | |
| 7189 | QCBORDecode_Private_ProcessTagItem(pMe, |
| 7190 | pItem, |
| 7191 | uTagRequirement, |
| 7192 | qTypes, |
| 7193 | uTagNumber, |
| 7194 | QCBORDecode_ExpMantissaTagCB, |
| 7195 | uOffset); |
| 7196 | |
| 7197 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7198 | return; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7199 | } |
| 7200 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7201 | uErr = QCBOR_SUCCESS; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7202 | |
| 7203 | switch (pItem->uDataType) { |
| 7204 | |
| 7205 | case QCBOR_TYPE_DECIMAL_FRACTION: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7206 | case QCBOR_TYPE_BIGFLOAT: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7207 | if(pItem->val.expAndMantissa.Mantissa.nInt >= 0) { |
| 7208 | uMantissa = (uint64_t)pItem->val.expAndMantissa.Mantissa.nInt; |
| 7209 | *pbIsNegative = false; |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 7210 | } else { |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7211 | if(pItem->val.expAndMantissa.Mantissa.nInt != INT64_MIN) { |
| 7212 | uMantissa = (uint64_t)-pItem->val.expAndMantissa.Mantissa.nInt; |
| 7213 | } else { |
| 7214 | /* Can't negate like above when int64_t is INT64_MIN because it |
| 7215 | * will overflow. See ExponentNN() */ |
| 7216 | uMantissa = (uint64_t)INT64_MAX+1; |
| 7217 | } |
Laurence Lundblade | 4e808ba | 2022-12-29 12:45:20 -0700 | [diff] [blame] | 7218 | *pbIsNegative = true; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7219 | } |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7220 | /* Reverse the offset by 1 for type 1 negative value to be consistent |
| 7221 | * with big num case below which don't offset because it requires |
| 7222 | * big number arithmetic. This is a bug fix for QCBOR v1.5. |
| 7223 | */ |
| 7224 | uMantissa--; |
| 7225 | *pMantissa = QCBOR_Private_ConvertIntToBigNum(uMantissa, BufferForMantissa); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7226 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 7227 | break; |
| 7228 | |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7229 | #ifndef QCBOR_DISABLE_TAGS |
| 7230 | /* If tags are disabled, mantissas can never be big nums */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7231 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7232 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7233 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 7234 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 7235 | *pbIsNegative = false; |
| 7236 | break; |
| 7237 | |
| 7238 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7239 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7240 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 7241 | *pMantissa = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 7242 | *pbIsNegative = true; |
| 7243 | break; |
Laurence Lundblade | 37286c0 | 2022-09-03 10:05:02 -0700 | [diff] [blame] | 7244 | #endif /* QCBOR_DISABLE_TAGS */ |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7245 | |
| 7246 | default: |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7247 | uErr = QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7248 | } |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7249 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7250 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7251 | } |
| 7252 | |
| 7253 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7254 | /** |
| 7255 | * @brief Decode exponent and mantissa into a big number with negative offset of 1. |
| 7256 | * |
| 7257 | * @param[in] pMe The decode context. |
| 7258 | * @param[in] uTagRequirement Whether a tag number must be present or not. |
| 7259 | * @param[in] pItem Item to decode and convert. |
| 7260 | * @param[in] BufferForMantissa Buffer to output mantissa into. |
| 7261 | * @param[out] pMantissa The output mantissa. |
| 7262 | * @param[out] pbIsNegative The sign of the output. |
| 7263 | * @param[out] pnExponent The mantissa of the output. |
| 7264 | * |
| 7265 | * This is the common processing of a decimal fraction or a big float |
| 7266 | * into a big number. This will decode and consume all the CBOR items |
| 7267 | * that make up the decimal fraction or big float. |
| 7268 | * |
| 7269 | * This performs the subtraction of 1 from the negative value so the |
| 7270 | * caller doesn't need to. This links more object code than QCBORDecode_Private_ProcessExpMantissaBig(). |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7271 | */ |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7272 | static void |
| 7273 | QCBORDecode_Private_ExpBigMantissaMain(QCBORDecodeContext *pMe, |
| 7274 | const uint8_t uTagRequirement, |
| 7275 | const uint64_t uTagNumber, |
| 7276 | const size_t uOffset, |
| 7277 | QCBORItem *pItem, |
| 7278 | const UsefulBuf BufferForMantissa, |
| 7279 | UsefulBufC *pMantissa, |
| 7280 | bool *pbIsNegative, |
| 7281 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7282 | { |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7283 | QCBORError uErr; |
| 7284 | QCBORItem TempMantissa; |
| 7285 | const uint8_t *qTypes; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7286 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7287 | if(pMe->uLastError) { |
| 7288 | return; |
| 7289 | } |
| 7290 | |
| 7291 | if(uTagNumber == CBOR_TAG_BIGFLOAT) { |
| 7292 | qTypes = QCBORDecode_Private_BigFloatTypes; |
| 7293 | } else { |
| 7294 | qTypes = QCBORDecode_Private_DecimalFractionTypes; |
| 7295 | } |
| 7296 | |
| 7297 | QCBORDecode_Private_ProcessTagItem(pMe, |
| 7298 | pItem, |
| 7299 | uTagRequirement, |
| 7300 | qTypes, |
| 7301 | uTagNumber, |
| 7302 | QCBORDecode_ExpMantissaTagCB, |
| 7303 | uOffset); |
| 7304 | |
| 7305 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7306 | return; |
| 7307 | } |
| 7308 | |
| 7309 | memset(&TempMantissa, 0, sizeof(TempMantissa)); |
| 7310 | |
| 7311 | switch (pItem->uDataType) { |
| 7312 | |
| 7313 | case QCBOR_TYPE_DECIMAL_FRACTION: |
| 7314 | case QCBOR_TYPE_BIGFLOAT: |
| 7315 | TempMantissa.uDataType = QCBOR_TYPE_INT64; |
| 7316 | TempMantissa.val.int64 = pItem->val.expAndMantissa.Mantissa.nInt; |
| 7317 | break; |
| 7318 | |
| 7319 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_U64: |
| 7320 | case QCBOR_TYPE_BIGFLOAT_POS_U64: |
| 7321 | TempMantissa.uDataType = QCBOR_TYPE_UINT64; |
| 7322 | TempMantissa.val.uint64 = pItem->val.expAndMantissa.Mantissa.uInt; |
| 7323 | break; |
| 7324 | |
| 7325 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_U64: |
| 7326 | case QCBOR_TYPE_BIGFLOAT_NEG_U64: |
| 7327 | TempMantissa.uDataType = QCBOR_TYPE_65BIT_NEG_INT; |
| 7328 | TempMantissa.val.uint64 = pItem->val.expAndMantissa.Mantissa.uInt; |
| 7329 | break; |
| 7330 | |
| 7331 | #ifndef QCBOR_DISABLE_TAGS |
| 7332 | /* If tags are disabled, mantissas can never be big nums */ |
| 7333 | case QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM: |
| 7334 | case QCBOR_TYPE_BIGFLOAT_POS_BIGNUM: |
| 7335 | TempMantissa.uDataType = QCBOR_TYPE_BYTE_STRING; |
| 7336 | TempMantissa.val.bigNum = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 7337 | *pbIsNegative = false; |
| 7338 | break; |
| 7339 | |
| 7340 | case QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM: |
| 7341 | case QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM: |
| 7342 | TempMantissa.uDataType = QCBOR_TYPE_BYTE_STRING; |
| 7343 | TempMantissa.val.bigNum = pItem->val.expAndMantissa.Mantissa.bigNum; |
| 7344 | *pbIsNegative = true; |
| 7345 | break; |
| 7346 | #endif /* ! QCBOR_DISABLE_TAGS */ |
| 7347 | } |
| 7348 | |
| 7349 | *pnExponent = pItem->val.expAndMantissa.nExponent; |
| 7350 | uErr = QCBORDecode_ProcessBigNumber(TempMantissa, BufferForMantissa, pMantissa, pbIsNegative); |
| 7351 | |
| 7352 | pMe->uLastError = (uint8_t)uErr; |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7353 | } |
| 7354 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7355 | |
| 7356 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7357 | * Public function, see header qcbor/qcbor_decode.h file |
| 7358 | */ |
| 7359 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7360 | QCBORDecode_GetTDecimalFraction(QCBORDecodeContext *pMe, |
| 7361 | const uint8_t uTagRequirement, |
| 7362 | int64_t *pnMantissa, |
| 7363 | int64_t *pnExponent) |
| 7364 | { |
| 7365 | QCBORItem Item; |
| 7366 | size_t uOffset; |
| 7367 | |
| 7368 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
| 7369 | QCBORDecode_Private_ExpIntMantissaMain(pMe, |
| 7370 | uTagRequirement, |
| 7371 | CBOR_TAG_DECIMAL_FRACTION, |
| 7372 | uOffset, |
| 7373 | &Item, |
| 7374 | pnMantissa, |
| 7375 | pnExponent); |
| 7376 | } |
| 7377 | |
| 7378 | |
| 7379 | /* |
| 7380 | * Public function, see header qcbor/qcbor_decode.h file |
| 7381 | */ |
| 7382 | void |
| 7383 | QCBORDecode_GetTDecimalFractionInMapN(QCBORDecodeContext *pMe, |
| 7384 | const int64_t nLabel, |
| 7385 | const uint8_t uTagRequirement, |
| 7386 | int64_t *pnMantissa, |
| 7387 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7388 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7389 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7390 | size_t uOffset; |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7391 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7392 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7393 | QCBORDecode_Private_ExpIntMantissaMain(pMe, |
| 7394 | uTagRequirement, |
| 7395 | CBOR_TAG_DECIMAL_FRACTION, |
| 7396 | uOffset, |
| 7397 | &Item, |
| 7398 | pnMantissa, |
| 7399 | pnExponent); |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7400 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7401 | } |
| 7402 | |
| 7403 | |
| 7404 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7405 | * Public function, see header qcbor/qcbor_decode.h file |
| 7406 | */ |
| 7407 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7408 | QCBORDecode_GetTDecimalFractionInMapSZ(QCBORDecodeContext *pMe, |
| 7409 | const char *szLabel, |
| 7410 | const uint8_t uTagRequirement, |
| 7411 | int64_t *pnMantissa, |
| 7412 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7413 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7414 | QCBORItem Item; |
| 7415 | size_t uOffset; |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7416 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7417 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7418 | QCBORDecode_Private_ExpIntMantissaMain(pMe, |
| 7419 | uTagRequirement, |
| 7420 | CBOR_TAG_DECIMAL_FRACTION, |
| 7421 | uOffset, |
| 7422 | &Item, |
| 7423 | pnMantissa, |
| 7424 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7425 | } |
| 7426 | |
| 7427 | |
| 7428 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7429 | * Public function, see header qcbor/qcbor_decode.h file |
| 7430 | */ |
| 7431 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7432 | QCBORDecode_GetTDecimalFractionBigMantissa(QCBORDecodeContext *pMe, |
| 7433 | const uint8_t uTagRequirement, |
| 7434 | const UsefulBuf MantissaBuffer, |
| 7435 | UsefulBufC *pMantissa, |
| 7436 | bool *pbMantissaIsNegative, |
| 7437 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7438 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7439 | QCBORItem Item; |
| 7440 | size_t uOffset; |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7441 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7442 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7443 | QCBORDecode_Private_ExpBigMantissaMain(pMe, |
| 7444 | uTagRequirement, |
| 7445 | CBOR_TAG_DECIMAL_FRACTION, |
| 7446 | uOffset, |
| 7447 | &Item, |
| 7448 | MantissaBuffer, |
| 7449 | pMantissa, |
| 7450 | pbMantissaIsNegative, |
| 7451 | pnExponent); |
| 7452 | } |
| 7453 | |
| 7454 | |
| 7455 | /* |
| 7456 | * Public function, see header qcbor/qcbor_decode.h file |
| 7457 | */ |
| 7458 | void |
| 7459 | QCBORDecode_GetTDecimalFractionBigMantissaInMapN(QCBORDecodeContext *pMe, |
| 7460 | const int64_t nLabel, |
| 7461 | const uint8_t uTagRequirement, |
| 7462 | const UsefulBuf BufferForMantissa, |
| 7463 | UsefulBufC *pMantissa, |
| 7464 | bool *pbIsNegative, |
| 7465 | int64_t *pnExponent) |
| 7466 | { |
| 7467 | QCBORItem Item; |
| 7468 | size_t uOffset; |
| 7469 | |
| 7470 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 7471 | QCBORDecode_Private_ExpBigMantissaMain(pMe, |
| 7472 | uTagRequirement, |
| 7473 | CBOR_TAG_DECIMAL_FRACTION, |
| 7474 | uOffset, |
| 7475 | &Item, |
| 7476 | BufferForMantissa, |
| 7477 | pMantissa, |
| 7478 | pbIsNegative, |
| 7479 | pnExponent); |
| 7480 | } |
| 7481 | |
| 7482 | |
| 7483 | /* |
| 7484 | * Public function, see header qcbor/qcbor_decode.h file |
| 7485 | */ |
| 7486 | void |
| 7487 | QCBORDecode_GetTDecimalFractionBigMantissaInMapSZ(QCBORDecodeContext *pMe, |
| 7488 | const char *szLabel, |
| 7489 | const uint8_t uTagRequirement, |
| 7490 | const UsefulBuf BufferForMantissa, |
| 7491 | UsefulBufC *pMantissa, |
| 7492 | bool *pbIsNegative, |
| 7493 | int64_t *pnExponent) |
| 7494 | { |
| 7495 | QCBORItem Item; |
| 7496 | size_t uOffset; |
| 7497 | |
| 7498 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 7499 | QCBORDecode_Private_ExpBigMantissaMain(pMe, |
| 7500 | uTagRequirement, |
| 7501 | CBOR_TAG_DECIMAL_FRACTION, |
| 7502 | uOffset, |
| 7503 | &Item, |
| 7504 | BufferForMantissa, |
| 7505 | pMantissa, |
| 7506 | pbIsNegative, |
| 7507 | pnExponent); |
| 7508 | } |
| 7509 | |
| 7510 | /* |
| 7511 | * Public function, see header qcbor/qcbor_decode.h file |
| 7512 | */ |
| 7513 | void |
| 7514 | QCBORDecode_GetTDecimalFractionBigMantissaRaw(QCBORDecodeContext *pMe, |
| 7515 | const uint8_t uTagRequirement, |
| 7516 | const UsefulBuf MantissaBuffer, |
| 7517 | UsefulBufC *pMantissa, |
| 7518 | bool *pbMantissaIsNegative, |
| 7519 | int64_t *pnExponent) |
| 7520 | { |
| 7521 | QCBORItem Item; |
| 7522 | size_t uOffset; |
| 7523 | |
| 7524 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
| 7525 | QCBORDecode_Private_ExpBigMantissaRawMain(pMe, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7526 | uTagRequirement, |
| 7527 | CBOR_TAG_DECIMAL_FRACTION, |
| 7528 | uOffset, |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7529 | &Item, |
| 7530 | MantissaBuffer, |
| 7531 | pMantissa, |
| 7532 | pbMantissaIsNegative, |
| 7533 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7534 | } |
| 7535 | |
| 7536 | |
| 7537 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7538 | * Public function, see header qcbor/qcbor_decode.h file |
| 7539 | */ |
| 7540 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7541 | QCBORDecode_GetTDecimalFractionBigMantissaRawInMapN(QCBORDecodeContext *pMe, |
| 7542 | const int64_t nLabel, |
| 7543 | const uint8_t uTagRequirement, |
| 7544 | const UsefulBuf BufferForMantissa, |
| 7545 | UsefulBufC *pMantissa, |
| 7546 | bool *pbIsNegative, |
| 7547 | int64_t *pnExponent) |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7548 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7549 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7550 | size_t uOffset; |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 7551 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7552 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7553 | QCBORDecode_Private_ExpBigMantissaRawMain(pMe, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7554 | uTagRequirement, |
| 7555 | CBOR_TAG_DECIMAL_FRACTION, |
| 7556 | uOffset, |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7557 | &Item, |
| 7558 | BufferForMantissa, |
| 7559 | pMantissa, |
| 7560 | pbIsNegative, |
| 7561 | pnExponent); |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7562 | } |
Laurence Lundblade | 3cd26eb | 2020-06-29 23:33:13 -0700 | [diff] [blame] | 7563 | |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7564 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7565 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7566 | * Public function, see header qcbor/qcbor_decode.h file |
| 7567 | */ |
| 7568 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7569 | QCBORDecode_GetTDecimalFractionBigMantissaRawInMapSZ(QCBORDecodeContext *pMe, |
| 7570 | const char *szLabel, |
| 7571 | const uint8_t uTagRequirement, |
| 7572 | const UsefulBuf BufferForMantissa, |
| 7573 | UsefulBufC *pMantissa, |
| 7574 | bool *pbIsNegative, |
| 7575 | int64_t *pnExponent) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7576 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7577 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7578 | size_t uOffset; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7579 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7580 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7581 | QCBORDecode_Private_ExpBigMantissaRawMain(pMe, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7582 | uTagRequirement, |
| 7583 | CBOR_TAG_DECIMAL_FRACTION, |
| 7584 | uOffset, |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7585 | &Item, |
| 7586 | BufferForMantissa, |
| 7587 | pMantissa, |
| 7588 | pbIsNegative, |
| 7589 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7590 | } |
| 7591 | |
| 7592 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7593 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7594 | * Public function, see header qcbor/qcbor_decode.h file |
| 7595 | */ |
| 7596 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7597 | QCBORDecode_GetTBigFloat(QCBORDecodeContext *pMe, |
| 7598 | const uint8_t uTagRequirement, |
| 7599 | int64_t *pnMantissa, |
| 7600 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7601 | { |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7602 | QCBORItem Item; |
| 7603 | size_t uOffset; |
Laurence Lundblade | cdbbc19 | 2024-06-28 15:13:04 -0700 | [diff] [blame] | 7604 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7605 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7606 | QCBORDecode_Private_ExpIntMantissaMain(pMe, |
| 7607 | uTagRequirement, |
| 7608 | CBOR_TAG_BIGFLOAT, |
| 7609 | uOffset, |
| 7610 | &Item, |
| 7611 | pnMantissa, |
| 7612 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7613 | } |
| 7614 | |
| 7615 | |
| 7616 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7617 | * Public function, see header qcbor/qcbor_decode.h file |
| 7618 | */ |
| 7619 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7620 | QCBORDecode_GetTBigFloatInMapN(QCBORDecodeContext *pMe, |
| 7621 | const int64_t nLabel, |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7622 | const uint8_t uTagRequirement, |
| 7623 | int64_t *pnMantissa, |
| 7624 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7625 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7626 | QCBORItem Item; |
| 7627 | size_t uOffset; |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7628 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7629 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 7630 | QCBORDecode_Private_ExpIntMantissaMain(pMe, |
| 7631 | uTagRequirement, |
| 7632 | CBOR_TAG_BIGFLOAT, |
| 7633 | uOffset, |
| 7634 | &Item, |
| 7635 | pnMantissa, |
| 7636 | pnExponent); |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7637 | } |
| 7638 | |
| 7639 | |
| 7640 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7641 | * Public function, see header qcbor/qcbor_decode.h file |
| 7642 | */ |
| 7643 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7644 | QCBORDecode_GetTBigFloatInMapSZ(QCBORDecodeContext *pMe, |
| 7645 | const char *szLabel, |
| 7646 | const uint8_t uTagRequirement, |
| 7647 | int64_t *pnMantissa, |
| 7648 | int64_t *pnExponent) |
| 7649 | { |
| 7650 | QCBORItem Item; |
| 7651 | size_t uOffset; |
| 7652 | |
| 7653 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 7654 | QCBORDecode_Private_ExpIntMantissaMain(pMe, |
| 7655 | uTagRequirement, |
| 7656 | CBOR_TAG_BIGFLOAT, |
| 7657 | uOffset, |
| 7658 | &Item, |
| 7659 | pnMantissa, |
| 7660 | pnExponent); |
| 7661 | } |
| 7662 | |
| 7663 | |
| 7664 | /* |
| 7665 | * Public function, see header qcbor/qcbor_decode.h file |
| 7666 | */ |
| 7667 | void |
| 7668 | QCBORDecode_GetTBigFloatBigMantissa(QCBORDecodeContext *pMe, |
| 7669 | const uint8_t uTagRequirement, |
| 7670 | const UsefulBuf MantissaBuffer, |
| 7671 | UsefulBufC *pMantissa, |
| 7672 | bool *pbMantissaIsNegative, |
| 7673 | int64_t *pnExponent) |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7674 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7675 | QCBORItem Item; |
| 7676 | size_t uOffset; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7677 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7678 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7679 | QCBORDecode_Private_ExpBigMantissaMain(pMe, |
| 7680 | uTagRequirement, |
| 7681 | CBOR_TAG_BIGFLOAT, |
| 7682 | uOffset, |
| 7683 | &Item, |
| 7684 | MantissaBuffer, |
| 7685 | pMantissa, |
| 7686 | pbMantissaIsNegative, |
| 7687 | pnExponent); |
| 7688 | } |
| 7689 | |
| 7690 | |
| 7691 | |
| 7692 | /* |
| 7693 | * Public function, see header qcbor/qcbor_decode.h file |
| 7694 | */ |
| 7695 | void |
| 7696 | QCBORDecode_GetTBigFloatBigMantissaInMapN(QCBORDecodeContext *pMe, |
| 7697 | const int64_t nLabel, |
| 7698 | const uint8_t uTagRequirement, |
| 7699 | const UsefulBuf BufferForMantissa, |
| 7700 | UsefulBufC *pMantissa, |
| 7701 | bool *pbIsNegative, |
| 7702 | int64_t *pnExponent) |
| 7703 | { |
| 7704 | QCBORItem Item; |
| 7705 | size_t uOffset; |
| 7706 | |
| 7707 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 7708 | QCBORDecode_Private_ExpBigMantissaMain(pMe, |
| 7709 | uTagRequirement, |
| 7710 | CBOR_TAG_BIGFLOAT, |
| 7711 | uOffset, |
| 7712 | &Item, |
| 7713 | BufferForMantissa, |
| 7714 | pMantissa, |
| 7715 | pbIsNegative, |
| 7716 | pnExponent); |
| 7717 | } |
| 7718 | |
| 7719 | |
| 7720 | /* |
| 7721 | * Public function, see header qcbor/qcbor_decode.h file |
| 7722 | */ |
| 7723 | void |
| 7724 | QCBORDecode_GetTBigFloatBigMantissaInMapSZ(QCBORDecodeContext *pMe, |
| 7725 | const char *szLabel, |
| 7726 | const uint8_t uTagRequirement, |
| 7727 | const UsefulBuf BufferForMantissa, |
| 7728 | UsefulBufC *pMantissa, |
| 7729 | bool *pbIsNegative, |
| 7730 | int64_t *pnExponent) |
| 7731 | { |
| 7732 | QCBORItem Item; |
| 7733 | size_t uOffset; |
| 7734 | |
| 7735 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 7736 | QCBORDecode_Private_ExpBigMantissaMain(pMe, |
| 7737 | uTagRequirement, |
| 7738 | CBOR_TAG_BIGFLOAT, |
| 7739 | uOffset, |
| 7740 | &Item, |
| 7741 | BufferForMantissa, |
| 7742 | pMantissa, |
| 7743 | pbIsNegative, |
| 7744 | pnExponent); |
| 7745 | } |
| 7746 | |
| 7747 | |
| 7748 | void |
| 7749 | QCBORDecode_GetTBigFloatBigMantissaRaw(QCBORDecodeContext *pMe, |
| 7750 | const uint8_t uTagRequirement, |
| 7751 | const UsefulBuf MantissaBuffer, |
| 7752 | UsefulBufC *pMantissa, |
| 7753 | bool *pbMantissaIsNegative, |
| 7754 | int64_t *pnExponent) |
| 7755 | { |
| 7756 | QCBORItem Item; |
| 7757 | size_t uOffset; |
| 7758 | |
| 7759 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
| 7760 | QCBORDecode_Private_ExpBigMantissaRawMain(pMe, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7761 | uTagRequirement, |
| 7762 | CBOR_TAG_BIGFLOAT, |
| 7763 | uOffset, |
Laurence Lundblade | 4ae4ef9 | 2024-02-05 19:21:31 -0700 | [diff] [blame] | 7764 | &Item, |
| 7765 | MantissaBuffer, |
| 7766 | pMantissa, |
| 7767 | pbMantissaIsNegative, |
| 7768 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7769 | } |
| 7770 | |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7771 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7772 | |
| 7773 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7774 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7775 | * Public function, see header qcbor/qcbor_decode.h file |
| 7776 | */ |
| 7777 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7778 | QCBORDecode_GetTBigFloatBigMantissaRawInMapN(QCBORDecodeContext *pMe, |
| 7779 | const int64_t nLabel, |
| 7780 | const uint8_t uTagRequirement, |
| 7781 | const UsefulBuf BufferForMantissa, |
| 7782 | UsefulBufC *pMantissa, |
| 7783 | bool *pbIsNegative, |
| 7784 | int64_t *pnExponent) |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7785 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7786 | QCBORItem Item; |
| 7787 | size_t uOffset; |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7788 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7789 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7790 | QCBORDecode_Private_ExpBigMantissaRawMain(pMe, |
| 7791 | uTagRequirement, |
| 7792 | CBOR_TAG_BIGFLOAT, |
| 7793 | uOffset, |
| 7794 | &Item, |
| 7795 | BufferForMantissa, |
| 7796 | pMantissa, |
| 7797 | pbIsNegative, |
| 7798 | pnExponent); |
Laurence Lundblade | b3683da | 2020-08-02 17:44:54 -0700 | [diff] [blame] | 7799 | } |
| 7800 | |
| 7801 | |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7802 | /* |
Laurence Lundblade | 8e36f81 | 2024-01-26 10:59:29 -0700 | [diff] [blame] | 7803 | * Public function, see header qcbor/qcbor_decode.h file |
| 7804 | */ |
| 7805 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7806 | QCBORDecode_GetTBigFloatBigMantissaRawInMapSZ(QCBORDecodeContext *pMe, |
| 7807 | const char *szLabel, |
| 7808 | const uint8_t uTagRequirement, |
| 7809 | const UsefulBuf BufferForMantissa, |
| 7810 | UsefulBufC *pMantissa, |
| 7811 | bool *pbIsNegative, |
| 7812 | int64_t *pnExponent) |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7813 | { |
Laurence Lundblade | faec39f | 2020-08-02 21:53:53 -0700 | [diff] [blame] | 7814 | QCBORItem Item; |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7815 | size_t uOffset; |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7816 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7817 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7818 | QCBORDecode_Private_ExpBigMantissaRawMain(pMe, |
| 7819 | uTagRequirement, |
| 7820 | CBOR_TAG_BIGFLOAT, |
| 7821 | uOffset, |
| 7822 | &Item, |
| 7823 | BufferForMantissa, |
| 7824 | pMantissa, |
| 7825 | pbIsNegative, |
| 7826 | pnExponent); |
Laurence Lundblade | 91853ae | 2020-06-15 19:35:58 -0700 | [diff] [blame] | 7827 | } |
Laurence Lundblade | d4cd723 | 2020-07-03 19:30:48 -0700 | [diff] [blame] | 7828 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7829 | |
| 7830 | #endif /* ! QCBOR_DISABLE_EXP_AND_MANTISSA */ |
Laurence Lundblade | d883ad3 | 2024-03-23 22:37:37 -0700 | [diff] [blame] | 7831 | |
| 7832 | |
| 7833 | #if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT) |
| 7834 | /* |
| 7835 | * Public function, see header qcbor/qcbor_spiffy_decode.h file |
| 7836 | */ |
| 7837 | void |
| 7838 | QCBORDecode_GetNumberConvertPrecisely(QCBORDecodeContext *pMe, |
| 7839 | QCBORItem *pNumber) |
| 7840 | { |
| 7841 | QCBORItem Item; |
| 7842 | struct IEEE754_ToInt ToInt; |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7843 | double dNum; |
Laurence Lundblade | d883ad3 | 2024-03-23 22:37:37 -0700 | [diff] [blame] | 7844 | QCBORError uError; |
| 7845 | |
| 7846 | if(pMe->uLastError != QCBOR_SUCCESS) { |
| 7847 | return; |
| 7848 | } |
| 7849 | |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7850 | // TODO:VGetNext? |
Laurence Lundblade | d883ad3 | 2024-03-23 22:37:37 -0700 | [diff] [blame] | 7851 | uError = QCBORDecode_GetNext(pMe, &Item); |
| 7852 | if(uError != QCBOR_SUCCESS) { |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7853 | *pNumber = Item; |
Laurence Lundblade | d883ad3 | 2024-03-23 22:37:37 -0700 | [diff] [blame] | 7854 | pMe->uLastError = (uint8_t)uError; |
| 7855 | return; |
| 7856 | } |
| 7857 | |
| 7858 | switch(Item.uDataType) { |
| 7859 | case QCBOR_TYPE_INT64: |
| 7860 | case QCBOR_TYPE_UINT64: |
| 7861 | *pNumber = Item; |
| 7862 | break; |
| 7863 | |
| 7864 | case QCBOR_TYPE_DOUBLE: |
| 7865 | ToInt = IEEE754_DoubleToInt(Item.val.dfnum); |
| 7866 | if(ToInt.type == IEEE754_ToInt_IS_INT) { |
| 7867 | pNumber->uDataType = QCBOR_TYPE_INT64; |
| 7868 | pNumber->val.int64 = ToInt.integer.is_signed; |
| 7869 | } else if(ToInt.type == IEEE754_ToInt_IS_UINT) { |
| 7870 | if(ToInt.integer.un_signed <= INT64_MAX) { |
| 7871 | /* Do the same as base QCBOR integer decoding */ |
| 7872 | pNumber->uDataType = QCBOR_TYPE_INT64; |
| 7873 | pNumber->val.int64 = (int64_t)ToInt.integer.un_signed; |
| 7874 | } else { |
| 7875 | pNumber->uDataType = QCBOR_TYPE_UINT64; |
| 7876 | pNumber->val.uint64 = ToInt.integer.un_signed; |
| 7877 | } |
| 7878 | } else { |
| 7879 | *pNumber = Item; |
| 7880 | } |
| 7881 | break; |
| 7882 | |
| 7883 | case QCBOR_TYPE_FLOAT: |
| 7884 | ToInt = IEEE754_SingleToInt(Item.val.fnum); |
| 7885 | if(ToInt.type == IEEE754_ToInt_IS_INT) { |
| 7886 | pNumber->uDataType = QCBOR_TYPE_INT64; |
| 7887 | pNumber->val.int64 = ToInt.integer.is_signed; |
| 7888 | } else if(ToInt.type == IEEE754_ToInt_IS_UINT) { |
| 7889 | if(ToInt.integer.un_signed <= INT64_MAX) { |
| 7890 | /* Do the same as base QCBOR integer decoding */ |
| 7891 | pNumber->uDataType = QCBOR_TYPE_INT64; |
| 7892 | pNumber->val.int64 = (int64_t)ToInt.integer.un_signed; |
| 7893 | } else { |
| 7894 | pNumber->uDataType = QCBOR_TYPE_UINT64; |
| 7895 | pNumber->val.uint64 = ToInt.integer.un_signed; |
| 7896 | } |
| 7897 | } else { |
| 7898 | *pNumber = Item; |
| 7899 | } |
| 7900 | break; |
| 7901 | |
Laurence Lundblade | d883ad3 | 2024-03-23 22:37:37 -0700 | [diff] [blame] | 7902 | case QCBOR_TYPE_65BIT_NEG_INT: |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7903 | if(Item.val.uint64 == UINT64_MAX) { |
| 7904 | /* The value -18446744073709551616 is encoded as an |
| 7905 | * unsigned 18446744073709551615. It's a whole number that |
| 7906 | * needs to be returned as a double. It can't be handled |
| 7907 | * by IEEE754_UintToDouble because 18446744073709551616 |
| 7908 | * doesn't fit into a uint64_t. You can't get it by adding |
| 7909 | * 1 to 18446744073709551615. |
| 7910 | */ |
| 7911 | pNumber->val.dfnum = -18446744073709551616.0; |
Laurence Lundblade | d883ad3 | 2024-03-23 22:37:37 -0700 | [diff] [blame] | 7912 | pNumber->uDataType = QCBOR_TYPE_DOUBLE; |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7913 | } else { |
| 7914 | dNum = IEEE754_UintToDouble(Item.val.uint64 + 1, 1); |
| 7915 | if(dNum == IEEE754_UINT_TO_DOUBLE_OOB) { |
| 7916 | *pNumber = Item; |
| 7917 | } else { |
| 7918 | pNumber->val.dfnum = dNum; |
| 7919 | pNumber->uDataType = QCBOR_TYPE_DOUBLE; |
| 7920 | } |
Laurence Lundblade | d883ad3 | 2024-03-23 22:37:37 -0700 | [diff] [blame] | 7921 | } |
| 7922 | break; |
| 7923 | |
| 7924 | default: |
| 7925 | pMe->uLastError = QCBOR_ERR_UNEXPECTED_TYPE; |
| 7926 | pNumber->uDataType = QCBOR_TYPE_NONE; |
| 7927 | break; |
| 7928 | } |
| 7929 | } |
| 7930 | |
| 7931 | #endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */ |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7932 | |
| 7933 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7934 | |
| 7935 | |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7936 | static UsefulBufC |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7937 | QCBORDecode_IntToBigNumber(uint64_t uNum, |
| 7938 | const UsefulBuf BigNumberBuf) |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7939 | { |
| 7940 | UsefulOutBuf OB; |
| 7941 | |
| 7942 | /* With a UsefulOutBuf, there's no pointer math here. */ |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7943 | UsefulOutBuf_Init(&OB, BigNumberBuf); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7944 | |
| 7945 | /* Must copy one byte even if zero. The loop, mask and shift |
| 7946 | * algorithm provides endian conversion. |
| 7947 | */ |
| 7948 | do { |
| 7949 | UsefulOutBuf_InsertByte(&OB, uNum & 0xff, 0); |
| 7950 | uNum >>= 8; |
| 7951 | } while(uNum); |
| 7952 | |
| 7953 | return UsefulOutBuf_OutUBuf(&OB); |
| 7954 | } |
| 7955 | |
| 7956 | |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7957 | /* Add one to the big number and put the result in a new UsefulBufC |
| 7958 | * from storage in UsefulBuf. |
| 7959 | * |
| 7960 | * Leading zeros must be removed before calling this. |
| 7961 | * |
| 7962 | * Code Reviewers: THIS FUNCTION DOES POINTER MATH |
| 7963 | */ |
| 7964 | static UsefulBufC |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7965 | QCBORDecode_BigNumberCopyPlusOne(UsefulBufC BigNumber, |
| 7966 | UsefulBuf BigNumberBuf) |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7967 | { |
| 7968 | uint8_t uCarry; |
| 7969 | uint8_t uSourceValue; |
| 7970 | const uint8_t *pSource; |
| 7971 | uint8_t *pDest; |
| 7972 | ptrdiff_t uDestBytesLeft; |
| 7973 | |
| 7974 | /* Start adding at the LSB */ |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7975 | pSource = &((const uint8_t *)BigNumber.ptr)[BigNumber.len-1]; |
| 7976 | pDest = &((uint8_t *)BigNumberBuf.ptr)[BigNumberBuf.len-1]; |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7977 | |
| 7978 | uCarry = 1; /* Gets set back to zero if add the next line doesn't wrap */ |
| 7979 | *pDest = *pSource + 1; |
| 7980 | while(1) { |
| 7981 | /* Wrap around from 0xff to 0 is a defined operation for |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 7982 | * unsigned addition in C.*/ |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7983 | if(*pDest != 0) { |
| 7984 | /* The add operation didn't wrap so no more carry. This |
| 7985 | * funciton only adds one, so when there is no more carry, |
| 7986 | * carrying is over to the end. |
| 7987 | */ |
| 7988 | uCarry = 0; |
| 7989 | } |
| 7990 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7991 | uDestBytesLeft = pDest - (uint8_t *)BigNumberBuf.ptr; |
| 7992 | if(pSource <= (const uint8_t *)BigNumber.ptr && uCarry == 0) { |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7993 | break; /* Successful exit */ |
| 7994 | } |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 7995 | if(pSource > (const uint8_t *)BigNumber.ptr) { |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 7996 | uSourceValue = *--pSource; |
| 7997 | } else { |
| 7998 | /* All source bytes processed, but not the last carry */ |
| 7999 | uSourceValue = 0; |
| 8000 | } |
| 8001 | |
| 8002 | pDest--; |
| 8003 | if(uDestBytesLeft < 0) { |
| 8004 | return NULLUsefulBufC; /* Not enough space in destination buffer */ |
| 8005 | } |
| 8006 | |
| 8007 | *pDest = uSourceValue + uCarry; |
| 8008 | } |
| 8009 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8010 | return (UsefulBufC){pDest, BigNumberBuf.len - (size_t)uDestBytesLeft}; |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8011 | } |
| 8012 | |
| 8013 | |
| 8014 | /* This returns 1 when uNum is 0 */ |
| 8015 | static size_t |
| 8016 | QCBORDecode_Private_CountNonZeroBytes(uint64_t uNum) |
| 8017 | { |
| 8018 | size_t uCount = 0; |
| 8019 | do { |
| 8020 | uCount++; |
| 8021 | uNum >>= 8; |
| 8022 | } while(uNum); |
| 8023 | |
| 8024 | return uCount; |
| 8025 | } |
| 8026 | |
| 8027 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8028 | |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8029 | /* |
| 8030 | * Public function, see header qcbor/qcbor_decode.h |
| 8031 | */ |
| 8032 | QCBORError |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8033 | QCBORDecode_ProcessBigNumberNoPreferred(const QCBORItem Item, |
| 8034 | const UsefulBuf BigNumberBuf, |
| 8035 | UsefulBufC *pBigNumber, |
| 8036 | bool *pbIsNegative) |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8037 | { |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8038 | size_t uLen; |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8039 | UsefulBufC BigNumber; |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8040 | int uType; |
| 8041 | |
| 8042 | uType = Item.uDataType; |
| 8043 | if(uType == QCBOR_TYPE_BYTE_STRING) { |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8044 | uType = *pbIsNegative ? QCBOR_TYPE_NEGBIGNUM : QCBOR_TYPE_POSBIGNUM; |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8045 | } |
| 8046 | |
| 8047 | static const uint8_t Zero[] = {0x00}; |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8048 | BigNumber = UsefulBuf_SkipLeading(Item.val.bigNum, 0); |
| 8049 | if(BigNumber.len == 0) { |
| 8050 | BigNumber = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(Zero); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8051 | } |
| 8052 | |
| 8053 | /* Compute required length so it can be returned if buffer is too small */ |
| 8054 | switch(uType) { |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8055 | |
| 8056 | case QCBOR_TYPE_POSBIGNUM: |
| 8057 | uLen = BigNumber.len; |
| 8058 | break; |
| 8059 | |
| 8060 | case QCBOR_TYPE_NEGBIGNUM: |
| 8061 | uLen = BigNumber.len; |
| 8062 | if(UsefulBuf_IsValue(UsefulBuf_SkipLeading(BigNumber, 0), 0xff) == SIZE_MAX) { |
| 8063 | uLen++; |
| 8064 | } |
| 8065 | break; |
| 8066 | |
| 8067 | default: |
| 8068 | return QCBOR_ERR_UNEXPECTED_TYPE; |
| 8069 | } |
| 8070 | |
| 8071 | *pBigNumber = (UsefulBufC){NULL, uLen}; |
| 8072 | |
| 8073 | if(BigNumberBuf.len < uLen || uLen == 0 || BigNumberBuf.ptr == NULL) { |
| 8074 | return BigNumberBuf.ptr == NULL ? QCBOR_SUCCESS : QCBOR_ERR_BUFFER_TOO_SMALL; |
| 8075 | /* Buffer is too short or type is wrong */ |
| 8076 | } |
| 8077 | |
| 8078 | |
| 8079 | if(uType == QCBOR_TYPE_POSBIGNUM) { |
| 8080 | *pBigNumber = UsefulBuf_Copy(BigNumberBuf, BigNumber); |
| 8081 | *pbIsNegative = false; |
| 8082 | } else if(uType == QCBOR_TYPE_NEGBIGNUM) { |
| 8083 | /* The messy one. Take the stuff in the buffer and copy it to |
| 8084 | * the new buffer, adding one to it. This might be one byte |
| 8085 | * bigger than the original because of the carry from adding |
| 8086 | * one.*/ |
| 8087 | *pbIsNegative = true; |
| 8088 | *pBigNumber = QCBORDecode_BigNumberCopyPlusOne(BigNumber, BigNumberBuf); |
| 8089 | } |
| 8090 | |
| 8091 | return QCBOR_SUCCESS; |
| 8092 | } |
| 8093 | |
| 8094 | |
| 8095 | /* |
| 8096 | * Public function, see header qcbor/qcbor_decode.h |
| 8097 | */ |
| 8098 | QCBORError |
| 8099 | QCBORDecode_ProcessBigNumber(const QCBORItem Item, |
| 8100 | UsefulBuf BigNumberBuf, |
| 8101 | UsefulBufC *pBigNumber, |
| 8102 | bool *pbIsNegative) |
| 8103 | { |
| 8104 | QCBORError uResult; |
| 8105 | size_t uLen; |
| 8106 | int uType; |
| 8107 | |
| 8108 | uType = Item.uDataType; |
| 8109 | |
| 8110 | switch(uType) { |
| 8111 | case QCBOR_TYPE_POSBIGNUM: |
| 8112 | case QCBOR_TYPE_NEGBIGNUM: |
| 8113 | case QCBOR_TYPE_BYTE_STRING: |
| 8114 | return QCBORDecode_ProcessBigNumberNoPreferred(Item, BigNumberBuf, pBigNumber, pbIsNegative); |
| 8115 | break; |
| 8116 | |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8117 | case QCBOR_TYPE_INT64: |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8118 | uLen = QCBORDecode_Private_CountNonZeroBytes((uint64_t)ABSOLUTE_VALUE(Item.val.int64)); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8119 | break; |
| 8120 | |
| 8121 | case QCBOR_TYPE_UINT64: |
| 8122 | uLen = QCBORDecode_Private_CountNonZeroBytes(Item.val.uint64); |
| 8123 | break; |
| 8124 | |
| 8125 | case QCBOR_TYPE_65BIT_NEG_INT: |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8126 | uLen = Item.val.uint64 == UINT64_MAX ? 9 : QCBORDecode_Private_CountNonZeroBytes(Item.val.uint64); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8127 | break; |
| 8128 | |
| 8129 | default: |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8130 | return QCBOR_ERR_UNEXPECTED_TYPE; |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8131 | } |
| 8132 | |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8133 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8134 | *pBigNumber = (UsefulBufC){NULL, uLen}; |
| 8135 | |
| 8136 | if(BigNumberBuf.len < uLen || uLen == 0 || BigNumberBuf.ptr == NULL) { |
| 8137 | return BigNumberBuf.ptr == NULL ? QCBOR_SUCCESS : QCBOR_ERR_BUFFER_TOO_SMALL; |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8138 | /* Buffer is too short or type is wrong */ |
| 8139 | } |
| 8140 | |
| 8141 | uResult = QCBOR_SUCCESS; |
| 8142 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8143 | if(uType == QCBOR_TYPE_UINT64) { |
| 8144 | *pBigNumber = QCBORDecode_IntToBigNumber(Item.val.uint64, BigNumberBuf); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8145 | *pbIsNegative = false; |
| 8146 | } else if(uType == QCBOR_TYPE_INT64) { |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8147 | /* Offset of 1 for negative numbers already performed */ |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8148 | *pbIsNegative = Item.val.int64 < 0; |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8149 | *pBigNumber = QCBORDecode_IntToBigNumber((uint64_t)(*pbIsNegative ? -Item.val.int64 : Item.val.int64), BigNumberBuf); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8150 | } else if(uType == QCBOR_TYPE_65BIT_NEG_INT) { |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8151 | /* Offset of 1 for negative numbers NOT already performed */ |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8152 | *pbIsNegative = true; |
| 8153 | if(Item.val.uint64 == UINT64_MAX) { |
| 8154 | /* The one value that can't be done with a computation |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8155 | * because it would overflow a uint64_t */ |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8156 | static const uint8_t TwoToThe64[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8157 | *pBigNumber = UsefulBuf_Copy(BigNumberBuf, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(TwoToThe64)); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8158 | } else { |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8159 | // TODO: why + 1; test it; document it |
| 8160 | *pBigNumber = QCBORDecode_IntToBigNumber(Item.val.uint64 + 1, BigNumberBuf); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8161 | } |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8162 | } |
| 8163 | |
| 8164 | return uResult; |
| 8165 | } |
| 8166 | |
| 8167 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8168 | static const uint64_t QCBORDecode_Private_BigNumberTagNumbers[] = { |
| 8169 | CBOR_TAG_POS_BIGNUM, |
| 8170 | CBOR_TAG_NEG_BIGNUM, |
| 8171 | CBOR_TAG_INVALID64}; |
| 8172 | |
| 8173 | static const uint8_t QCBORDecode_Private_BigNumberTypes[] = { |
| 8174 | QCBOR_TYPE_INT64, |
| 8175 | QCBOR_TYPE_UINT64, |
| 8176 | QCBOR_TYPE_65BIT_NEG_INT, |
| 8177 | QCBOR_TYPE_POSBIGNUM, |
| 8178 | QCBOR_TYPE_NEGBIGNUM, |
| 8179 | QCBOR_TYPE_NONE}; |
| 8180 | |
| 8181 | #define QCBORDecode_Private_BigNumberTypesNoPreferred &QCBORDecode_Private_BigNumberTypes[3] |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8182 | |
| 8183 | |
| 8184 | static void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8185 | QCBORDecode_Private_BigNumberNoPreferredMain(QCBORDecodeContext *pMe, |
| 8186 | const uint8_t uTagRequirement, |
| 8187 | QCBORItem *pItem, |
| 8188 | const size_t uOffset, |
| 8189 | UsefulBuf BigNumberBuf, |
| 8190 | UsefulBufC *pBigNumber, |
| 8191 | bool *pbIsNegative) |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8192 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8193 | QCBORDecode_Private_ProcessTagItemMulti(pMe, |
| 8194 | pItem, |
| 8195 | uTagRequirement, |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8196 | QCBORDecode_Private_BigNumberTypesNoPreferred, |
| 8197 | QCBORDecode_Private_BigNumberTagNumbers, |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8198 | QCBORDecode_StringsTagCB, |
| 8199 | uOffset); |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8200 | if(pMe->uLastError) { |
| 8201 | return; |
| 8202 | } |
| 8203 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8204 | pMe->uLastError = (uint8_t)QCBORDecode_ProcessBigNumberNoPreferred(*pItem, BigNumberBuf, pBigNumber, pbIsNegative); |
| 8205 | } |
| 8206 | |
| 8207 | |
| 8208 | static void |
| 8209 | QCBORDecode_Private_BigNumberMain(QCBORDecodeContext *pMe, |
| 8210 | const uint8_t uTagRequirement, |
| 8211 | QCBORItem *pItem, |
| 8212 | const size_t uOffset, |
| 8213 | UsefulBuf BigNumberBuf, |
| 8214 | UsefulBufC *pBigNumber, |
| 8215 | bool *pbIsNegative) |
| 8216 | { |
| 8217 | QCBORDecode_Private_ProcessTagItemMulti(pMe, |
| 8218 | pItem, |
| 8219 | uTagRequirement, |
| 8220 | QCBORDecode_Private_BigNumberTypes, |
| 8221 | QCBORDecode_Private_BigNumberTagNumbers, |
| 8222 | QCBORDecode_StringsTagCB, |
| 8223 | uOffset); |
| 8224 | if(pMe->uLastError) { |
| 8225 | return; |
| 8226 | } |
| 8227 | |
| 8228 | pMe->uLastError = (uint8_t)QCBORDecode_ProcessBigNumber(*pItem, BigNumberBuf, pBigNumber, pbIsNegative); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8229 | } |
| 8230 | |
| 8231 | |
| 8232 | /* |
| 8233 | * Public function, see header qcbor/qcbor_decode.h |
| 8234 | */ |
| 8235 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8236 | QCBORDecode_GetTBigNumber(QCBORDecodeContext *pMe, |
| 8237 | const uint8_t uTagRequirement, |
| 8238 | UsefulBuf BigNumberBuf, |
| 8239 | UsefulBufC *pBigNumber, |
| 8240 | bool *pbIsNegative) |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8241 | { |
| 8242 | QCBORItem Item; |
| 8243 | size_t uOffset; |
| 8244 | |
| 8245 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8246 | QCBORDecode_Private_BigNumberMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8247 | } |
| 8248 | |
| 8249 | |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8250 | |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8251 | /* |
| 8252 | * Public function, see header qcbor/qcbor_decode.h |
| 8253 | */ |
| 8254 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8255 | QCBORDecode_GetTBigNumberInMapN(QCBORDecodeContext *pMe, |
| 8256 | const int64_t nLabel, |
| 8257 | const uint8_t uTagRequirement, |
| 8258 | UsefulBuf BigNumberBuf, |
| 8259 | UsefulBufC *pBigNumber, |
| 8260 | bool *pbIsNegative) |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8261 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8262 | QCBORItem Item; |
| 8263 | size_t uOffset; |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8264 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8265 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8266 | QCBORDecode_Private_BigNumberMain(pMe, |
| 8267 | uTagRequirement, |
| 8268 | &Item, |
| 8269 | uOffset, |
| 8270 | BigNumberBuf, |
| 8271 | pBigNumber, |
| 8272 | pbIsNegative); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8273 | } |
| 8274 | |
| 8275 | /* |
| 8276 | * Public function, see header qcbor/qcbor_decode.h |
| 8277 | */ |
| 8278 | void |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8279 | QCBORDecode_GetTBigNumberInMapSZ(QCBORDecodeContext *pMe, |
| 8280 | const char *szLabel, |
| 8281 | const uint8_t uTagRequirement, |
| 8282 | UsefulBuf BigNumberBuf, |
| 8283 | UsefulBufC *pBigNumber, |
| 8284 | bool *pbIsNegative) |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8285 | { |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8286 | QCBORItem Item; |
| 8287 | size_t uOffset; |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8288 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8289 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
Laurence Lundblade | 6876933 | 2024-11-03 13:09:20 -0800 | [diff] [blame] | 8290 | QCBORDecode_Private_BigNumberMain(pMe, |
| 8291 | uTagRequirement, |
| 8292 | &Item, |
| 8293 | uOffset, |
| 8294 | BigNumberBuf, |
| 8295 | pBigNumber, |
| 8296 | pbIsNegative); |
| 8297 | } |
| 8298 | |
| 8299 | |
| 8300 | /* |
| 8301 | * Public function, see header qcbor/qcbor_decode.h |
| 8302 | */ |
| 8303 | void |
| 8304 | QCBORDecode_GetTBigNumberNoPreferred(QCBORDecodeContext *pMe, |
| 8305 | const uint8_t uTagRequirement, |
| 8306 | UsefulBuf BigNumberBuf, |
| 8307 | UsefulBufC *pBigNumber, |
| 8308 | bool *pbIsNegative) |
| 8309 | { |
| 8310 | QCBORItem Item; |
| 8311 | size_t uOffset; |
| 8312 | |
| 8313 | QCBORDecode_Private_GetAndTell(pMe, &Item, &uOffset); |
| 8314 | QCBORDecode_Private_BigNumberNoPreferredMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative); |
| 8315 | } |
| 8316 | |
| 8317 | /* |
| 8318 | * Public function, see header qcbor/qcbor_decode.h |
| 8319 | */ |
| 8320 | void |
| 8321 | QCBORDecode_GetTBigNumberNoPreferredInMapN(QCBORDecodeContext *pMe, |
| 8322 | const int64_t nLabel, |
| 8323 | const uint8_t uTagRequirement, |
| 8324 | UsefulBuf BigNumberBuf, |
| 8325 | UsefulBufC *pBigNumber, |
| 8326 | bool *pbIsNegative) |
| 8327 | { |
| 8328 | QCBORItem Item; |
| 8329 | size_t uOffset; |
| 8330 | |
| 8331 | QCBORDecode_GetItemInMapNoCheckN(pMe, nLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 8332 | QCBORDecode_Private_BigNumberNoPreferredMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative); |
| 8333 | |
| 8334 | } |
| 8335 | |
| 8336 | /* |
| 8337 | * Public function, see header qcbor/qcbor_decode.h |
| 8338 | */ |
| 8339 | void |
| 8340 | QCBORDecode_GetTBigNumberNoPreferredInMapSZ(QCBORDecodeContext *pMe, |
| 8341 | const char *szLabel, |
| 8342 | const uint8_t uTagRequirement, |
| 8343 | UsefulBuf BigNumberBuf, |
| 8344 | UsefulBufC *pBigNumber, |
| 8345 | bool *pbIsNegative) |
| 8346 | { |
| 8347 | QCBORItem Item; |
| 8348 | size_t uOffset; |
| 8349 | |
| 8350 | QCBORDecode_GetItemInMapNoCheckSZ(pMe, szLabel, QCBOR_TYPE_ANY, &Item, &uOffset); |
| 8351 | QCBORDecode_Private_BigNumberNoPreferredMain(pMe, uTagRequirement, &Item, uOffset, BigNumberBuf, pBigNumber, pbIsNegative); |
Laurence Lundblade | 14ce228 | 2024-07-24 22:13:35 -0700 | [diff] [blame] | 8352 | } |
| 8353 | |
Laurence Lundblade | 721b56e | 2024-10-22 03:02:04 -0700 | [diff] [blame] | 8354 | // TODO: re order above functions in tag number order |